Whamcloud - gitweb
LU-15971 llite: implicit default LMV inherit
[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,
2645                                   int comp_idx, bool is_dir)
2646 {
2647         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2648         struct lod_layout_component *entry;
2649
2650         if (is_dir)
2651                 return  0;
2652
2653         entry = &lo->ldo_comp_entries[comp_idx];
2654         if (lod_comp_inited(entry))
2655                 return entry->llc_stripe_count;
2656         else if ((__u16)-1 == entry->llc_stripe_count)
2657                 return lod->lod_ost_count;
2658         else
2659                 return lod_get_stripe_count(lod, lo, comp_idx,
2660                                             entry->llc_stripe_count,
2661                                             entry->llc_pattern &
2662                                             LOV_PATTERN_OVERSTRIPING);
2663 }
2664
2665 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2666 {
2667         int magic, size = 0, i;
2668         struct lod_layout_component *comp_entries;
2669         __u16 comp_cnt;
2670         bool is_composite, is_foreign = false;
2671
2672         if (is_dir) {
2673                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2674                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2675                 is_composite =
2676                         lo->ldo_def_striping->lds_def_striping_is_composite;
2677         } else {
2678                 comp_cnt = lo->ldo_comp_cnt;
2679                 comp_entries = lo->ldo_comp_entries;
2680                 is_composite = lo->ldo_is_composite;
2681                 is_foreign = lo->ldo_is_foreign;
2682         }
2683
2684         if (is_foreign)
2685                 return lo->ldo_foreign_lov_size;
2686
2687         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2688         if (is_composite) {
2689                 size = sizeof(struct lov_comp_md_v1) +
2690                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2691                 LASSERT(size % sizeof(__u64) == 0);
2692         }
2693
2694         for (i = 0; i < comp_cnt; i++) {
2695                 __u16 stripe_count;
2696
2697                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2698                 stripe_count = lod_comp_entry_stripe_count(lo, i, is_dir);
2699                 if (!is_dir && is_composite)
2700                         lod_comp_shrink_stripe_count(&comp_entries[i],
2701                                                      &stripe_count);
2702
2703                 size += lov_user_md_size(stripe_count, magic);
2704                 LASSERT(size % sizeof(__u64) == 0);
2705         }
2706         return size;
2707 }
2708
2709 /**
2710  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2711  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2712  * to be added.
2713   *
2714  * \param[in] env       execution environment
2715  * \param[in] dt        dt_object to add components on
2716  * \param[in] buf       buffer contains components to be added
2717  * \parem[in] th        thandle
2718  *
2719  * \retval      0 on success
2720  * \retval      negative errno on failure
2721  */
2722 static int lod_declare_layout_add(const struct lu_env *env,
2723                                   struct dt_object *dt,
2724                                   const struct lu_buf *buf,
2725                                   struct thandle *th)
2726 {
2727         struct lod_thread_info  *info = lod_env_info(env);
2728         struct lod_layout_component *comp_array, *lod_comp, *old_array;
2729         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2730         struct dt_object *next = dt_object_child(dt);
2731         struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
2732         struct lod_object *lo = lod_dt_obj(dt);
2733         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2734         __u32 magic;
2735         int i, rc, array_cnt, old_array_cnt;
2736         ENTRY;
2737
2738         LASSERT(lo->ldo_is_composite);
2739
2740         if (lo->ldo_flr_state != LCM_FL_NONE)
2741                 RETURN(-EBUSY);
2742
2743         rc = lod_verify_striping(env, d, lo, buf, false);
2744         if (rc != 0)
2745                 RETURN(rc);
2746
2747         magic = comp_v1->lcm_magic;
2748         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2749                 lustre_swab_lov_comp_md_v1(comp_v1);
2750                 magic = comp_v1->lcm_magic;
2751         }
2752
2753         if (magic != LOV_USER_MAGIC_COMP_V1)
2754                 RETURN(-EINVAL);
2755
2756         mutex_lock(&lo->ldo_layout_mutex);
2757
2758         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2759         OBD_ALLOC_PTR_ARRAY(comp_array, array_cnt);
2760         if (comp_array == NULL) {
2761                 mutex_unlock(&lo->ldo_layout_mutex);
2762                 RETURN(-ENOMEM);
2763         }
2764
2765
2766         memcpy(comp_array, lo->ldo_comp_entries,
2767                sizeof(*comp_array) * lo->ldo_comp_cnt);
2768
2769         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2770                 struct lov_user_md_v1 *v1;
2771                 struct lu_extent *ext;
2772
2773                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2774                                 comp_v1->lcm_entries[i].lcme_offset);
2775                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2776
2777                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2778                 lod_comp->llc_extent.e_start = ext->e_start;
2779                 lod_comp->llc_extent.e_end = ext->e_end;
2780                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2781                 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2782
2783                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2784                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2785                 lod_adjust_stripe_info(lod_comp, desc, 0);
2786
2787                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2788                         struct lov_user_md_v3 *v3 = (typeof(*v3) *) v1;
2789
2790                         if (v3->lmm_pool_name[0] != '\0' &&
2791                             !lov_pool_is_ignored(v3->lmm_pool_name)) {
2792                                 rc = lod_set_pool(&lod_comp->llc_pool,
2793                                                   v3->lmm_pool_name);
2794                                 if (rc)
2795                                         GOTO(error, rc);
2796                         }
2797                 }
2798         }
2799
2800         old_array = lo->ldo_comp_entries;
2801         old_array_cnt = lo->ldo_comp_cnt;
2802
2803         lo->ldo_comp_entries = comp_array;
2804         lo->ldo_comp_cnt = array_cnt;
2805
2806         /* No need to increase layout generation here, it will be increased
2807          * later when generating component ID for the new components */
2808
2809         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2810         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2811                                               XATTR_NAME_LOV, 0, th);
2812         if (rc) {
2813                 lo->ldo_comp_entries = old_array;
2814                 lo->ldo_comp_cnt = old_array_cnt;
2815                 GOTO(error, rc);
2816         }
2817
2818         OBD_FREE_PTR_ARRAY(old_array, old_array_cnt);
2819
2820         LASSERT(lo->ldo_mirror_count == 1);
2821         lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2822
2823         mutex_unlock(&lo->ldo_layout_mutex);
2824
2825         RETURN(0);
2826
2827 error:
2828         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2829                 lod_comp = &comp_array[i];
2830                 if (lod_comp->llc_pool != NULL) {
2831                         OBD_FREE(lod_comp->llc_pool,
2832                                  strlen(lod_comp->llc_pool) + 1);
2833                         lod_comp->llc_pool = NULL;
2834                 }
2835         }
2836         OBD_FREE_PTR_ARRAY(comp_array, array_cnt);
2837         mutex_unlock(&lo->ldo_layout_mutex);
2838
2839         RETURN(rc);
2840 }
2841
2842 /**
2843  * lod_last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2844  * @mirror_id: Mirror id to be checked.
2845  * @lo:        LOD object.
2846  *
2847  * This function checks if a mirror with specified @mirror_id is the last
2848  * non-stale mirror of a LOD object @lo.
2849  *
2850  * Return: true or false.
2851  */
2852 static inline
2853 bool lod_last_non_stale_mirror(__u16 mirror_id, struct lod_object *lo)
2854 {
2855         struct lod_layout_component *lod_comp;
2856         bool has_stale_flag;
2857         int i;
2858
2859         for (i = 0; i < lo->ldo_mirror_count; i++) {
2860                 if (lo->ldo_mirrors[i].lme_id == mirror_id ||
2861                     lo->ldo_mirrors[i].lme_stale)
2862                         continue;
2863
2864                 has_stale_flag = false;
2865                 lod_foreach_mirror_comp(lod_comp, lo, i) {
2866                         if (lod_comp->llc_flags & LCME_FL_STALE) {
2867                                 has_stale_flag = true;
2868                                 break;
2869                         }
2870                 }
2871                 if (!has_stale_flag)
2872                         return false;
2873         }
2874
2875         return true;
2876 }
2877
2878 /**
2879  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2880  * the '$field' can only be 'flags' now. The xattr value is binary
2881  * lov_comp_md_v1 which contains the component ID(s) and the value of
2882  * the field to be modified.
2883  * Please update allowed_lustre_lov macro if $field groks more values
2884  * in the future.
2885  *
2886  * \param[in] env       execution environment
2887  * \param[in] dt        dt_object to be modified
2888  * \param[in] op        operation string, like "set.flags"
2889  * \param[in] buf       buffer contains components to be set
2890  * \parem[in] th        thandle
2891  *
2892  * \retval      0 on success
2893  * \retval      negative errno on failure
2894  */
2895 static int lod_declare_layout_set(const struct lu_env *env,
2896                                   struct dt_object *dt,
2897                                   char *op, const struct lu_buf *buf,
2898                                   struct thandle *th)
2899 {
2900         struct lod_layout_component     *lod_comp;
2901         struct lod_thread_info  *info = lod_env_info(env);
2902         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2903         struct lod_object       *lo = lod_dt_obj(dt);
2904         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2905         __u32   magic;
2906         int     i, j, rc;
2907         bool    changed = false;
2908         ENTRY;
2909
2910         /* Please update allowed_lustre_lov macro if op
2911          * groks more values in the future
2912          */
2913         if (strcmp(op, "set.flags") != 0) {
2914                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2915                        lod2obd(d)->obd_name, op);
2916                 RETURN(-ENOTSUPP);
2917         }
2918
2919         magic = comp_v1->lcm_magic;
2920         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2921                 lustre_swab_lov_comp_md_v1(comp_v1);
2922                 magic = comp_v1->lcm_magic;
2923         }
2924
2925         if (magic != LOV_USER_MAGIC_COMP_V1)
2926                 RETURN(-EINVAL);
2927
2928         if (comp_v1->lcm_entry_count == 0) {
2929                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2930                        lod2obd(d)->obd_name);
2931                 RETURN(-EINVAL);
2932         }
2933
2934         mutex_lock(&lo->ldo_layout_mutex);
2935         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2936                 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2937                 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2938                 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2939                 __u16 mirror_id = mirror_id_of(id);
2940                 bool neg = flags & LCME_FL_NEG;
2941
2942                 if (flags & LCME_FL_INIT) {
2943                         if (changed)
2944                                 lod_striping_free_nolock(env, lo);
2945                         mutex_unlock(&lo->ldo_layout_mutex);
2946                         RETURN(-EINVAL);
2947                 }
2948
2949                 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2950                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2951                         lod_comp = &lo->ldo_comp_entries[j];
2952
2953                         /* lfs only put one flag in each entry */
2954                         if ((flags && id != lod_comp->llc_id) ||
2955                             (mirror_flag && mirror_id !=
2956                                             mirror_id_of(lod_comp->llc_id)))
2957                                 continue;
2958
2959                         if (neg) {
2960                                 if (flags)
2961                                         lod_comp->llc_flags &= ~flags;
2962                                 if (mirror_flag)
2963                                         lod_comp->llc_flags &= ~mirror_flag;
2964                         } else {
2965                                 if (flags) {
2966                                         if ((flags & LCME_FL_STALE) &&
2967                                             lod_last_non_stale_mirror(mirror_id,
2968                                                                       lo)) {
2969                                                 mutex_unlock(
2970                                                         &lo->ldo_layout_mutex);
2971                                                 RETURN(-EUCLEAN);
2972                                         }
2973                                         lod_comp->llc_flags |= flags;
2974                                 }
2975                                 if (mirror_flag) {
2976                                         lod_comp->llc_flags |= mirror_flag;
2977                                         if (mirror_flag & LCME_FL_NOSYNC)
2978                                                 lod_comp->llc_timestamp =
2979                                                        ktime_get_real_seconds();
2980                                 }
2981                         }
2982                         changed = true;
2983                 }
2984         }
2985         mutex_unlock(&lo->ldo_layout_mutex);
2986
2987         if (!changed) {
2988                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2989                        lod2obd(d)->obd_name);
2990                 RETURN(-EINVAL);
2991         }
2992
2993         lod_obj_inc_layout_gen(lo);
2994
2995         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2996         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2997                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2998         RETURN(rc);
2999 }
3000
3001 /**
3002  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
3003  * and the xattr value is a unique component ID or a special lcme_id.
3004  *
3005  * \param[in] env       execution environment
3006  * \param[in] dt        dt_object to be operated on
3007  * \param[in] buf       buffer contains component ID or lcme_id
3008  * \parem[in] th        thandle
3009  *
3010  * \retval      0 on success
3011  * \retval      negative errno on failure
3012  */
3013 static int lod_declare_layout_del(const struct lu_env *env,
3014                                   struct dt_object *dt,
3015                                   const struct lu_buf *buf,
3016                                   struct thandle *th)
3017 {
3018         struct lod_thread_info  *info = lod_env_info(env);
3019         struct dt_object *next = dt_object_child(dt);
3020         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3021         struct lod_object *lo = lod_dt_obj(dt);
3022         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
3023         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3024         __u32 magic, id, flags, neg_flags = 0;
3025         int rc, i, j, left;
3026         ENTRY;
3027
3028         LASSERT(lo->ldo_is_composite);
3029
3030         if (lo->ldo_flr_state != LCM_FL_NONE)
3031                 RETURN(-EBUSY);
3032
3033         magic = comp_v1->lcm_magic;
3034         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3035                 lustre_swab_lov_comp_md_v1(comp_v1);
3036                 magic = comp_v1->lcm_magic;
3037         }
3038
3039         if (magic != LOV_USER_MAGIC_COMP_V1)
3040                 RETURN(-EINVAL);
3041
3042         id = comp_v1->lcm_entries[0].lcme_id;
3043         flags = comp_v1->lcm_entries[0].lcme_flags;
3044
3045         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
3046                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
3047                        lod2obd(d)->obd_name, id, flags);
3048                 RETURN(-EINVAL);
3049         }
3050
3051         if (id != LCME_ID_INVAL && flags != 0) {
3052                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
3053                        lod2obd(d)->obd_name);
3054                 RETURN(-EINVAL);
3055         }
3056
3057         if (id == LCME_ID_INVAL && !flags) {
3058                 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
3059                        lod2obd(d)->obd_name);
3060                 RETURN(-EINVAL);
3061         }
3062
3063         if (flags & LCME_FL_NEG) {
3064                 neg_flags = flags & ~LCME_FL_NEG;
3065                 flags = 0;
3066         }
3067
3068         mutex_lock(&lo->ldo_layout_mutex);
3069
3070         left = lo->ldo_comp_cnt;
3071         if (left <= 0) {
3072                 mutex_unlock(&lo->ldo_layout_mutex);
3073                 RETURN(-EINVAL);
3074         }
3075
3076         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3077                 struct lod_layout_component *lod_comp;
3078
3079                 lod_comp = &lo->ldo_comp_entries[i];
3080
3081                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
3082                         continue;
3083                 else if (flags && !(flags & lod_comp->llc_flags))
3084                         continue;
3085                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
3086                         continue;
3087
3088                 if (left != (i + 1)) {
3089                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
3090                                "a hole.\n", lod2obd(d)->obd_name);
3091                         mutex_unlock(&lo->ldo_layout_mutex);
3092                         RETURN(-EINVAL);
3093                 }
3094                 left--;
3095
3096                 /* Mark the component as deleted */
3097                 lod_comp->llc_id = LCME_ID_INVAL;
3098
3099                 /* Not instantiated component */
3100                 if (lod_comp->llc_stripe == NULL)
3101                         continue;
3102
3103                 LASSERT(lod_comp->llc_stripe_count > 0);
3104                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3105                         struct dt_object *obj = lod_comp->llc_stripe[j];
3106
3107                         if (obj == NULL)
3108                                 continue;
3109                         rc = lod_sub_declare_destroy(env, obj, th);
3110                         if (rc) {
3111                                 mutex_unlock(&lo->ldo_layout_mutex);
3112                                 RETURN(rc);
3113                         }
3114                 }
3115         }
3116
3117         LASSERTF(left >= 0, "left = %d\n", left);
3118         if (left == lo->ldo_comp_cnt) {
3119                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
3120                        lod2obd(d)->obd_name, id);
3121                 mutex_unlock(&lo->ldo_layout_mutex);
3122                 RETURN(-EINVAL);
3123         }
3124
3125         mutex_unlock(&lo->ldo_layout_mutex);
3126
3127         memset(attr, 0, sizeof(*attr));
3128         attr->la_valid = LA_SIZE;
3129         rc = lod_sub_declare_attr_set(env, next, attr, th);
3130         if (rc)
3131                 RETURN(rc);
3132
3133         if (left > 0) {
3134                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
3135                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
3136                                                XATTR_NAME_LOV, 0, th);
3137         } else {
3138                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
3139         }
3140
3141         RETURN(rc);
3142 }
3143
3144 /**
3145  * Declare layout add/set/del operations issued by special xattr names:
3146  *
3147  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
3148  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
3149  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
3150  *
3151  * \param[in] env       execution environment
3152  * \param[in] dt        object
3153  * \param[in] name      name of xattr
3154  * \param[in] buf       lu_buf contains xattr value
3155  * \param[in] th        transaction handle
3156  *
3157  * \retval              0 on success
3158  * \retval              negative if failed
3159  */
3160 static int lod_declare_modify_layout(const struct lu_env *env,
3161                                      struct dt_object *dt,
3162                                      const char *name,
3163                                      const struct lu_buf *buf,
3164                                      struct thandle *th)
3165 {
3166         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3167         struct lod_object *lo = lod_dt_obj(dt);
3168         char *op;
3169         int rc, len = strlen(XATTR_LUSTRE_LOV);
3170         ENTRY;
3171
3172         LASSERT(dt_object_exists(dt));
3173
3174         if (strlen(name) <= len || name[len] != '.') {
3175                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3176                        lod2obd(d)->obd_name, name);
3177                 RETURN(-EINVAL);
3178         }
3179         len++;
3180
3181         rc = lod_striping_load(env, lo);
3182         if (rc)
3183                 GOTO(unlock, rc);
3184
3185         /* the layout to be modified must be a composite layout */
3186         if (!lo->ldo_is_composite) {
3187                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3188                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3189                 GOTO(unlock, rc = -EINVAL);
3190         }
3191
3192         op = (char *)name + len;
3193         if (strcmp(op, "add") == 0) {
3194                 rc = lod_declare_layout_add(env, dt, buf, th);
3195         } else if (strcmp(op, "del") == 0) {
3196                 rc = lod_declare_layout_del(env, dt, buf, th);
3197         } else if (strncmp(op, "set", strlen("set")) == 0) {
3198                 rc = lod_declare_layout_set(env, dt, op, buf, th);
3199         } else  {
3200                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3201                        lod2obd(d)->obd_name, name);
3202                 GOTO(unlock, rc = -ENOTSUPP);
3203         }
3204 unlock:
3205         if (rc)
3206                 lod_striping_free(env, lo);
3207
3208         RETURN(rc);
3209 }
3210
3211 /**
3212  * Convert a plain file lov_mds_md to a composite layout.
3213  *
3214  * \param[in,out] info  the thread info::lti_ea_store buffer contains little
3215  *                      endian plain file layout
3216  *
3217  * \retval              0 on success, <0 on failure
3218  */
3219 static int lod_layout_convert(struct lod_thread_info *info)
3220 {
3221         struct lov_mds_md *lmm = info->lti_ea_store;
3222         struct lov_mds_md *lmm_save;
3223         struct lov_comp_md_v1 *lcm;
3224         struct lov_comp_md_entry_v1 *lcme;
3225         size_t size;
3226         __u32 blob_size;
3227         int rc = 0;
3228         ENTRY;
3229
3230         /* realloc buffer to a composite layout which contains one component */
3231         blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3232                                     le32_to_cpu(lmm->lmm_magic));
3233         size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3234
3235         OBD_ALLOC_LARGE(lmm_save, blob_size);
3236         if (!lmm_save)
3237                 GOTO(out, rc = -ENOMEM);
3238
3239         memcpy(lmm_save, lmm, blob_size);
3240
3241         if (info->lti_ea_store_size < size) {
3242                 rc = lod_ea_store_resize(info, size);
3243                 if (rc)
3244                         GOTO(out, rc);
3245         }
3246
3247         lcm = info->lti_ea_store;
3248         memset(lcm, 0, sizeof(*lcm) + sizeof(*lcme));
3249         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3250         lcm->lcm_size = cpu_to_le32(size);
3251         lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3252                                                 lmm_save->lmm_layout_gen));
3253         lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3254         lcm->lcm_entry_count = cpu_to_le16(1);
3255
3256         lcme = &lcm->lcm_entries[0];
3257         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3258         lcme->lcme_extent.e_start = 0;
3259         lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3260         lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3261         lcme->lcme_size = cpu_to_le32(blob_size);
3262
3263         memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3264
3265         EXIT;
3266 out:
3267         if (lmm_save)
3268                 OBD_FREE_LARGE(lmm_save, blob_size);
3269         return rc;
3270 }
3271
3272 /**
3273  * Merge layouts to form a mirrored file.
3274  */
3275 static int lod_declare_layout_merge(const struct lu_env *env,
3276                 struct dt_object *dt, const struct lu_buf *mbuf,
3277                 struct thandle *th)
3278 {
3279         struct lod_thread_info *info = lod_env_info(env);
3280         struct lu_attr *layout_attr = &info->lti_layout_attr;
3281         struct lu_buf *buf = &info->lti_buf;
3282         struct lod_object *lo = lod_dt_obj(dt);
3283         struct lov_comp_md_v1 *lcm;
3284         struct lov_comp_md_v1 *cur_lcm;
3285         struct lov_comp_md_v1 *merge_lcm;
3286         struct lov_comp_md_entry_v1 *lcme;
3287         struct lov_mds_md_v1 *lmm;
3288         size_t size = 0;
3289         size_t offset;
3290         __u16 cur_entry_count;
3291         __u16 merge_entry_count;
3292         __u32 id = 0;
3293         __u16 mirror_id = 0;
3294         __u32 mirror_count;
3295         int rc, i;
3296         bool merge_has_dom;
3297
3298         ENTRY;
3299
3300         merge_lcm = mbuf->lb_buf;
3301         if (mbuf->lb_len < sizeof(*merge_lcm))
3302                 RETURN(-EINVAL);
3303
3304         /* must be an existing layout from disk */
3305         if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3306                 RETURN(-EINVAL);
3307
3308         merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3309
3310         /* do not allow to merge two mirrored files */
3311         if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3312                 RETURN(-EBUSY);
3313
3314         /* verify the target buffer */
3315         rc = lod_get_lov_ea(env, lo);
3316         if (rc <= 0)
3317                 RETURN(rc ? : -ENODATA);
3318
3319         cur_lcm = info->lti_ea_store;
3320         switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3321         case LOV_MAGIC_V1:
3322         case LOV_MAGIC_V3:
3323                 rc = lod_layout_convert(info);
3324                 break;
3325         case LOV_MAGIC_COMP_V1:
3326         case LOV_MAGIC_SEL:
3327                 rc = 0;
3328                 break;
3329         default:
3330                 rc = -EINVAL;
3331         }
3332         if (rc)
3333                 RETURN(rc);
3334
3335         /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3336         cur_lcm = info->lti_ea_store;
3337         cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3338
3339         /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3340         mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3341         if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3342                 RETURN(-ERANGE);
3343
3344         /* size of new layout */
3345         size = le32_to_cpu(cur_lcm->lcm_size) +
3346                le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3347
3348         memset(buf, 0, sizeof(*buf));
3349         lu_buf_alloc(buf, size);
3350         if (buf->lb_buf == NULL)
3351                 RETURN(-ENOMEM);
3352
3353         lcm = buf->lb_buf;
3354         memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3355
3356         offset = sizeof(*lcm) +
3357                  sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3358         for (i = 0; i < cur_entry_count; i++) {
3359                 struct lov_comp_md_entry_v1 *cur_lcme;
3360
3361                 lcme = &lcm->lcm_entries[i];
3362                 cur_lcme = &cur_lcm->lcm_entries[i];
3363
3364                 lcme->lcme_offset = cpu_to_le32(offset);
3365                 memcpy((char *)lcm + offset,
3366                        (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3367                        le32_to_cpu(lcme->lcme_size));
3368
3369                 offset += le32_to_cpu(lcme->lcme_size);
3370
3371                 if (mirror_count == 1 &&
3372                     mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3373                         /* Add mirror from a non-flr file, create new mirror ID.
3374                          * Otherwise, keep existing mirror's component ID, used
3375                          * for mirror extension.
3376                          */
3377                         id = pflr_id(1, i + 1);
3378                         lcme->lcme_id = cpu_to_le32(id);
3379                 }
3380
3381                 id = max(le32_to_cpu(lcme->lcme_id), id);
3382         }
3383
3384         mirror_id = mirror_id_of(id) + 1;
3385
3386         /* check if first entry in new layout is DOM */
3387         lmm = (struct lov_mds_md_v1 *)((char *)merge_lcm +
3388                                         merge_lcm->lcm_entries[0].lcme_offset);
3389         merge_has_dom = lov_pattern(le32_to_cpu(lmm->lmm_pattern)) ==
3390                         LOV_PATTERN_MDT;
3391
3392         for (i = 0; i < merge_entry_count; i++) {
3393                 struct lov_comp_md_entry_v1 *merge_lcme;
3394
3395                 merge_lcme = &merge_lcm->lcm_entries[i];
3396                 lcme = &lcm->lcm_entries[cur_entry_count + i];
3397
3398                 *lcme = *merge_lcme;
3399                 lcme->lcme_offset = cpu_to_le32(offset);
3400                 if (merge_has_dom && i == 0)
3401                         lcme->lcme_flags |= cpu_to_le32(LCME_FL_STALE);
3402
3403                 id = pflr_id(mirror_id, i + 1);
3404                 lcme->lcme_id = cpu_to_le32(id);
3405
3406                 memcpy((char *)lcm + offset,
3407                        (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3408                        le32_to_cpu(lcme->lcme_size));
3409
3410                 offset += le32_to_cpu(lcme->lcme_size);
3411         }
3412
3413         /* fixup layout information */
3414         lcm->lcm_size = cpu_to_le32(size);
3415         lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3416         lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3417         if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3418                 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3419
3420         rc = lod_striping_reload(env, lo, buf, 0);
3421         if (rc)
3422                 GOTO(out, rc);
3423
3424         lod_obj_inc_layout_gen(lo);
3425         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3426
3427         /* transfer layout version to OST objects. */
3428         if (lo->ldo_mirror_count > 1) {
3429                 struct lod_obj_stripe_cb_data data = { {0} };
3430
3431                 layout_attr->la_valid = LA_LAYOUT_VERSION;
3432                 layout_attr->la_layout_version = 0;
3433                 data.locd_attr = layout_attr;
3434                 data.locd_declare = true;
3435                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3436                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3437                 if (rc)
3438                         GOTO(out, rc);
3439         }
3440
3441         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3442                                         XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3443
3444 out:
3445         lu_buf_free(buf);
3446         RETURN(rc);
3447 }
3448
3449 /**
3450  * Split layouts, just set the LOVEA with the layout from mbuf.
3451  */
3452 static int lod_declare_layout_split(const struct lu_env *env,
3453                 struct dt_object *dt, const struct lu_buf *mbuf,
3454                 struct thandle *th)
3455 {
3456         struct lod_thread_info *info = lod_env_info(env);
3457         struct lu_attr *layout_attr = &info->lti_layout_attr;
3458         struct lod_object *lo = lod_dt_obj(dt);
3459         struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3460         int rc;
3461         ENTRY;
3462
3463         rc = lod_striping_reload(env, lo, mbuf, LVF_ALL_STALE);
3464         if (rc)
3465                 RETURN(rc);
3466
3467         lod_obj_inc_layout_gen(lo);
3468         /* fix on-disk layout gen */
3469         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3470
3471         /* transfer layout version to OST objects. */
3472         if (lo->ldo_mirror_count > 1) {
3473                 struct lod_obj_stripe_cb_data data = { {0} };
3474
3475                 layout_attr->la_valid = LA_LAYOUT_VERSION;
3476                 layout_attr->la_layout_version = 0;
3477                 data.locd_attr = layout_attr;
3478                 data.locd_declare = true;
3479                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
3480                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
3481                 if (rc)
3482                         RETURN(rc);
3483         }
3484
3485         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3486                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3487         RETURN(rc);
3488 }
3489
3490 static int lod_layout_declare_or_purge_mirror(const struct lu_env *env,
3491                         struct dt_object *dt, const struct lu_buf *buf,
3492                         struct thandle *th, bool declare)
3493 {
3494         struct lod_thread_info *info = lod_env_info(env);
3495         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3496         struct lod_object *lo = lod_dt_obj(dt);
3497         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3498         struct lov_comp_md_entry_v1 *entry;
3499         struct lov_mds_md_v1 *lmm;
3500         struct dt_object **sub_objs = NULL;
3501         int rc = 0, i, k, array_count = 0;
3502
3503         ENTRY;
3504
3505         /**
3506          * other ops (like lod_declare_destroy) could destroying sub objects
3507          * as well.
3508          */
3509         mutex_lock(&lo->ldo_layout_mutex);
3510
3511         if (!declare) {
3512                 /* prepare sub-objects array */
3513                 for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3514                         entry = &comp_v1->lcm_entries[i];
3515
3516                         if (!(entry->lcme_flags & LCME_FL_INIT))
3517                                 continue;
3518
3519                         lmm = (struct lov_mds_md_v1 *)
3520                                         ((char *)comp_v1 + entry->lcme_offset);
3521                         array_count += lmm->lmm_stripe_count;
3522                 }
3523                 OBD_ALLOC_PTR_ARRAY(sub_objs, array_count);
3524                 if (sub_objs == NULL) {
3525                         mutex_unlock(&lo->ldo_layout_mutex);
3526                         RETURN(-ENOMEM);
3527                 }
3528         }
3529
3530         k = 0;  /* sub_objs index */
3531         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
3532                 struct lov_ost_data_v1 *objs;
3533                 struct lu_object *o, *n;
3534                 struct dt_object *dto;
3535                 struct lu_device *nd;
3536                 struct lov_mds_md_v3 *v3;
3537                 __u32 idx;
3538                 int j;
3539
3540                 entry = &comp_v1->lcm_entries[i];
3541
3542                 if (!(entry->lcme_flags & LCME_FL_INIT))
3543                         continue;
3544
3545                 lmm = (struct lov_mds_md_v1 *)
3546                                 ((char *)comp_v1 + entry->lcme_offset);
3547                 v3 = (struct lov_mds_md_v3 *)lmm;
3548                 if (lmm->lmm_magic == LOV_MAGIC_V3)
3549                         objs = &v3->lmm_objects[0];
3550                 else
3551                         objs = &lmm->lmm_objects[0];
3552
3553                 for (j = 0; j < lmm->lmm_stripe_count; j++) {
3554                         idx = objs[j].l_ost_idx;
3555                         rc = ostid_to_fid(&info->lti_fid, &objs[j].l_ost_oi,
3556                                           idx);
3557                         if (rc)
3558                                 GOTO(out, rc);
3559
3560                         if (!fid_is_sane(&info->lti_fid)) {
3561                                 CERROR("%s: sub-object insane fid "DFID"\n",
3562                                        lod2obd(d)->obd_name,
3563                                        PFID(&info->lti_fid));
3564                                 GOTO(out, rc = -EINVAL);
3565                         }
3566
3567                         lod_getref(&d->lod_ost_descs);
3568
3569                         rc = validate_lod_and_idx(d, idx);
3570                         if (unlikely(rc)) {
3571                                 lod_putref(d, &d->lod_ost_descs);
3572                                 GOTO(out, rc);
3573                         }
3574
3575                         nd = &OST_TGT(d, idx)->ltd_tgt->dd_lu_dev;
3576                         lod_putref(d, &d->lod_ost_descs);
3577
3578                         o = lu_object_find_at(env, nd, &info->lti_fid, NULL);
3579                         if (IS_ERR(o))
3580                                 GOTO(out, rc = PTR_ERR(o));
3581
3582                         n = lu_object_locate(o->lo_header, nd->ld_type);
3583                         if (unlikely(!n)) {
3584                                 lu_object_put(env, n);
3585                                 GOTO(out, rc = -ENOENT);
3586                         }
3587
3588                         dto = container_of(n, struct dt_object, do_lu);
3589
3590                         if (declare) {
3591                                 rc = lod_sub_declare_destroy(env, dto, th);
3592                                 dt_object_put(env, dto);
3593                                 if (rc)
3594                                         GOTO(out, rc);
3595                         } else {
3596                                 /**
3597                                  * collect to-be-destroyed sub objects, the
3598                                  * reference would be released after actual
3599                                  * deletion.
3600                                  */
3601                                 sub_objs[k] = dto;
3602                                 k++;
3603                         }
3604                 } /* for each stripe */
3605         } /* for each component in the mirror */
3606 out:
3607         if (!declare) {
3608                 i = 0;
3609                 if (!rc) {
3610                         /* destroy the sub objects */
3611                         for (; i < k; i++) {
3612                                 rc = lod_sub_destroy(env, sub_objs[i], th);
3613                                 if (rc)
3614                                         break;
3615                                 dt_object_put(env, sub_objs[i]);
3616                         }
3617                 }
3618                 /**
3619                  * if a sub object destroy failed, we'd release sub objects
3620                  * reference get from above sub_objs collection.
3621                  */
3622                 for (; i < k; i++)
3623                         dt_object_put(env, sub_objs[i]);
3624
3625                 OBD_FREE_PTR_ARRAY(sub_objs, array_count);
3626         }
3627         mutex_unlock(&lo->ldo_layout_mutex);
3628
3629         RETURN(rc);
3630 }
3631
3632 /**
3633  * Purge layouts, delete sub objects in the mirror stored in the vic_buf,
3634  * and set the LOVEA with the layout from mbuf.
3635  */
3636 static int lod_declare_layout_purge(const struct lu_env *env,
3637                 struct dt_object *dt, const struct lu_buf *buf,
3638                 struct thandle *th)
3639 {
3640         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3641         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
3642         int rc;
3643
3644         ENTRY;
3645
3646         if (le32_to_cpu(comp_v1->lcm_magic) != LOV_MAGIC_COMP_V1) {
3647                 CERROR("%s: invalid layout magic %#x != %#x\n",
3648                        lod2obd(d)->obd_name, le32_to_cpu(comp_v1->lcm_magic),
3649                        LOV_MAGIC_COMP_V1);
3650                 RETURN(-EINVAL);
3651         }
3652
3653         if (cpu_to_le32(LOV_MAGIC_COMP_V1) != LOV_MAGIC_COMP_V1)
3654                 lustre_swab_lov_comp_md_v1(comp_v1);
3655
3656         /* from now on, @buf contains cpu endian data */
3657
3658         if (comp_v1->lcm_mirror_count != 0) {
3659                 CERROR("%s: can only purge one mirror from "DFID"\n",
3660                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3661                 RETURN(-EINVAL);
3662         }
3663
3664         /* delcare sub objects deletion in the mirror stored in @buf */
3665         rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, true);
3666         RETURN(rc);
3667 }
3668
3669 /* delete sub objects from the mirror stored in @buf */
3670 static int lod_layout_purge(const struct lu_env *env, struct dt_object *dt,
3671                             const struct lu_buf *buf, struct thandle *th)
3672 {
3673         int rc;
3674
3675         ENTRY;
3676         rc = lod_layout_declare_or_purge_mirror(env, dt, buf, th, false);
3677         RETURN(rc);
3678 }
3679
3680 /**
3681  * Implementation of dt_object_operations::do_declare_xattr_set.
3682  *
3683  * \see dt_object_operations::do_declare_xattr_set() in the API description
3684  * for details.
3685  *
3686  * the extension to the API:
3687  *   - declaring LOVEA requests striping creation
3688  *   - LU_XATTR_REPLACE means layout swap
3689  */
3690 static int lod_declare_xattr_set(const struct lu_env *env,
3691                                  struct dt_object *dt,
3692                                  const struct lu_buf *buf,
3693                                  const char *name, int fl,
3694                                  struct thandle *th)
3695 {
3696         struct dt_object *next = dt_object_child(dt);
3697         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
3698         __u32             mode;
3699         int               rc;
3700         ENTRY;
3701
3702         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3703         if ((S_ISREG(mode) || mode == 0) &&
3704             !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT |
3705                     LU_XATTR_PURGE)) &&
3706             (strcmp(name, XATTR_NAME_LOV) == 0 ||
3707              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3708                 /*
3709                  * this is a request to create object's striping.
3710                  *
3711                  * allow to declare predefined striping on a new (!mode) object
3712                  * which is supposed to be replay of regular file creation
3713                  * (when LOV setting is declared)
3714                  *
3715                  * LU_XATTR_REPLACE is set to indicate a layout swap
3716                  */
3717                 if (dt_object_exists(dt)) {
3718                         rc = dt_attr_get(env, next, attr);
3719                         if (rc)
3720                                 RETURN(rc);
3721                 } else {
3722                         memset(attr, 0, sizeof(*attr));
3723                         attr->la_valid = LA_TYPE | LA_MODE;
3724                         attr->la_mode = S_IFREG;
3725                 }
3726                 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3727         } else if (fl & LU_XATTR_MERGE) {
3728                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3729                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3730                 rc = lod_declare_layout_merge(env, dt, buf, th);
3731         } else if (fl & LU_XATTR_SPLIT) {
3732                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3733                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3734                 rc = lod_declare_layout_split(env, dt, buf, th);
3735         } else if (fl & LU_XATTR_PURGE) {
3736                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3737                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3738                 rc = lod_declare_layout_purge(env, dt, buf, th);
3739         } else if (S_ISREG(mode) &&
3740                    strlen(name) >= sizeof(XATTR_LUSTRE_LOV) + 3 &&
3741                    allowed_lustre_lov(name)) {
3742                 /*
3743                  * this is a request to modify object's striping.
3744                  * add/set/del component(s).
3745                  */
3746                 if (!dt_object_exists(dt))
3747                         RETURN(-ENOENT);
3748
3749                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3750         } else if (S_ISDIR(mode)) {
3751                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3752         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3753                 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3754         } else {
3755                 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3756         }
3757
3758         RETURN(rc);
3759 }
3760
3761 /**
3762  * Apply xattr changes to the object.
3763  *
3764  * Applies xattr changes to the object and the stripes if the latter exist.
3765  *
3766  * \param[in] env       execution environment
3767  * \param[in] dt        object
3768  * \param[in] buf       buffer pointing to the new value of xattr
3769  * \param[in] name      name of xattr
3770  * \param[in] fl        flags
3771  * \param[in] th        transaction handle
3772  *
3773  * \retval              0 on success
3774  * \retval              negative if failed
3775  */
3776 static int lod_xattr_set_internal(const struct lu_env *env,
3777                                   struct dt_object *dt,
3778                                   const struct lu_buf *buf,
3779                                   const char *name, int fl,
3780                                   struct thandle *th)
3781 {
3782         struct dt_object        *next = dt_object_child(dt);
3783         struct lod_object       *lo = lod_dt_obj(dt);
3784         int                     rc;
3785         int                     i;
3786         ENTRY;
3787
3788         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3789         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3790                 RETURN(rc);
3791
3792         /* Note: Do not set LinkEA on sub-stripes, otherwise
3793          * it will confuse the fid2path process(see mdt_path_current()).
3794          * The linkEA between master and sub-stripes is set in
3795          * lod_xattr_set_lmv(). */
3796         if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3797                 RETURN(0);
3798
3799         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3800                 if (!lo->ldo_stripe[i])
3801                         continue;
3802
3803                 if (!dt_object_exists(lo->ldo_stripe[i]))
3804                         continue;
3805
3806                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3807                                        fl, th);
3808                 if (rc != 0)
3809                         break;
3810         }
3811
3812         RETURN(rc);
3813 }
3814
3815 /**
3816  * Delete an extended attribute.
3817  *
3818  * Deletes specified xattr from the object and the stripes if the latter exist.
3819  *
3820  * \param[in] env       execution environment
3821  * \param[in] dt        object
3822  * \param[in] name      name of xattr
3823  * \param[in] th        transaction handle
3824  *
3825  * \retval              0 on success
3826  * \retval              negative if failed
3827  */
3828 static int lod_xattr_del_internal(const struct lu_env *env,
3829                                   struct dt_object *dt,
3830                                   const char *name, struct thandle *th)
3831 {
3832         struct dt_object *next = dt_object_child(dt);
3833         struct lod_object *lo = lod_dt_obj(dt);
3834         int i;
3835         int rc;
3836
3837         ENTRY;
3838
3839         rc = lod_sub_xattr_del(env, next, name, th);
3840         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3841                 RETURN(rc);
3842
3843         if (lo->ldo_dir_stripe_count == 0)
3844                 RETURN(rc);
3845
3846         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3847                 if (!lo->ldo_stripe[i])
3848                         continue;
3849
3850                 if (!dt_object_exists(lo->ldo_stripe[i]))
3851                         continue;
3852
3853                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3854                 if (rc != 0)
3855                         break;
3856         }
3857
3858         RETURN(rc);
3859 }
3860
3861 /**
3862  * Set default striping on a directory.
3863  *
3864  * Sets specified striping on a directory object unless it matches the default
3865  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3866  * EA. This striping will be used when regular file is being created in this
3867  * directory.
3868  *
3869  * \param[in] env       execution environment
3870  * \param[in] dt        the striped object
3871  * \param[in] buf       buffer with the striping
3872  * \param[in] name      name of EA
3873  * \param[in] fl        xattr flag (see OSD API description)
3874  * \param[in] th        transaction handle
3875  *
3876  * \retval              0 on success
3877  * \retval              negative if failed
3878  */
3879 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3880                                     struct dt_object *dt,
3881                                     const struct lu_buf *buf,
3882                                     const char *name, int fl,
3883                                     struct thandle *th)
3884 {
3885         struct lov_user_md_v1   *lum;
3886         struct lov_user_md_v3   *v3 = NULL;
3887         const char              *pool_name = NULL;
3888         int                      rc;
3889         bool                     is_del;
3890         ENTRY;
3891
3892         LASSERT(buf != NULL && buf->lb_buf != NULL);
3893         lum = buf->lb_buf;
3894
3895         switch (lum->lmm_magic) {
3896         case LOV_USER_MAGIC_SPECIFIC:
3897         case LOV_USER_MAGIC_V3:
3898                 v3 = buf->lb_buf;
3899                 if (lov_pool_is_reserved(v3->lmm_pool_name))
3900                         memset(v3->lmm_pool_name, 0, sizeof(v3->lmm_pool_name));
3901                 else if (v3->lmm_pool_name[0] != '\0')
3902                         pool_name = v3->lmm_pool_name;
3903                 fallthrough;
3904         case LOV_USER_MAGIC_V1:
3905                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3906                  * (i.e. all default values specified) then delete default
3907                  * striping from dir. */
3908                 CDEBUG(D_LAYOUT,
3909                        "set default striping: sz %u # %u offset %d %s %s\n",
3910                        (unsigned)lum->lmm_stripe_size,
3911                        (unsigned)lum->lmm_stripe_count,
3912                        (int)lum->lmm_stripe_offset,
3913                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3914
3915                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3916                                              lum->lmm_stripe_count,
3917                                              lum->lmm_stripe_offset,
3918                                              pool_name);
3919                 break;
3920         case LOV_USER_MAGIC_COMP_V1:
3921         {
3922                 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lum;
3923                 struct lov_comp_md_entry_v1 *lcme;
3924                 int i, comp_cnt;
3925
3926                 comp_cnt = le16_to_cpu(lcm->lcm_entry_count);
3927                 for (i = 0; i < comp_cnt; i++) {
3928                         lcme = &lcm->lcm_entries[i];
3929                         if (lcme->lcme_flags & cpu_to_le32(LCME_FL_EXTENSION)) {
3930                                 lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_SEL);
3931                                 break;
3932                         }
3933                 }
3934
3935                 is_del = false;
3936                 break;
3937         }
3938         default:
3939                 CERROR("Invalid magic %x\n", lum->lmm_magic);
3940                 RETURN(-EINVAL);
3941         }
3942
3943         if (is_del) {
3944                 rc = lod_xattr_del_internal(env, dt, name, th);
3945                 if (rc == -ENODATA)
3946                         rc = 0;
3947         } else {
3948                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3949         }
3950
3951         RETURN(rc);
3952 }
3953
3954 static int lod_get_default_lov_striping(const struct lu_env *env,
3955                                        struct lod_object *lo,
3956                                        struct lod_default_striping *lds,
3957                                        struct dt_allocation_hint *ah);
3958
3959 /**
3960  * Helper function to convert compound layout to compound layout with
3961  * pool
3962  *
3963  * Copy lcm_entries array of \a src to \a tgt. Replace lov_user_md_v1
3964  * components of \a src with lov_user_md_v3 using \a pool.
3965  *
3966  * \param[in] src       source layout
3967  * \param[in] pool      pool to use in \a tgt
3968  * \param[out] tgt      target layout
3969  */
3970 static void embed_pool_to_comp_v1(const struct lov_comp_md_v1 *src,
3971                                   const char *pool,
3972                                   struct lov_comp_md_v1 *tgt)
3973 {
3974         size_t shift;
3975         struct lov_user_md_v1 *lum;
3976         struct lov_user_md_v3 *lum3;
3977         struct lov_comp_md_entry_v1 *entry;
3978         int i;
3979         __u32 offset;
3980
3981         entry = tgt->lcm_entries;
3982         shift = 0;
3983         for (i = 0; i < le16_to_cpu(src->lcm_entry_count); i++, entry++) {
3984                 *entry = src->lcm_entries[i];
3985                 offset = le32_to_cpu(src->lcm_entries[i].lcme_offset);
3986                 entry->lcme_offset = cpu_to_le32(offset + shift);
3987
3988                 lum = (struct lov_user_md_v1 *)((char *)src + offset);
3989                 lum3 = (struct lov_user_md_v3 *)((char *)tgt + offset + shift);
3990                 *(struct lov_user_md_v1 *)lum3 = *lum;
3991                 if (lum->lmm_pattern == cpu_to_le32(LOV_PATTERN_MDT)) {
3992                         lum3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
3993                 } else {
3994                         lum3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
3995                         entry->lcme_size = cpu_to_le32(sizeof(*lum3));
3996                         strlcpy(lum3->lmm_pool_name, pool,
3997                                 sizeof(lum3->lmm_pool_name));
3998                         shift += sizeof(*lum3) - sizeof(*lum);
3999                 }
4000         }
4001 }
4002
4003 /**
4004  * Set default striping on a directory.
4005  *
4006  * Sets specified striping on a directory object unless it matches the default
4007  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
4008  * EA. This striping will be used when regular file is being created in this
4009  * directory.
4010  * If current default striping includes a pool but specifed striping
4011  * does not - retain the pool if it exists.
4012  *
4013  * \param[in] env       execution environment
4014  * \param[in] dt        the striped object
4015  * \param[in] buf       buffer with the striping
4016  * \param[in] name      name of EA
4017  * \param[in] fl        xattr flag (see OSD API description)
4018  * \param[in] th        transaction handle
4019  *
4020  * \retval              0 on success
4021  * \retval              negative if failed
4022  */
4023 static int lod_xattr_set_default_lov_on_dir(const struct lu_env *env,
4024                                             struct dt_object *dt,
4025                                             const struct lu_buf *buf,
4026                                             const char *name, int fl,
4027                                             struct thandle *th)
4028 {
4029         struct lod_default_striping     *lds = lod_lds_buf_get(env);
4030         struct lov_user_md_v1           *v1 = buf->lb_buf;
4031         char                             pool[LOV_MAXPOOLNAME + 1];
4032         bool                             is_del;
4033         int                              rc;
4034
4035         ENTRY;
4036
4037         /* get existing striping config */
4038         rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds, NULL);
4039         if (rc)
4040                 RETURN(rc);
4041
4042         memset(pool, 0, sizeof(pool));
4043         if (lds->lds_def_striping_set == 1)
4044                 lod_layout_get_pool(lds->lds_def_comp_entries,
4045                                     lds->lds_def_comp_cnt, pool,
4046                                     sizeof(pool));
4047
4048         is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4049                                      v1->lmm_stripe_count,
4050                                      v1->lmm_stripe_offset,
4051                                      NULL);
4052
4053         /* Retain the pool name if it is not given */
4054         if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4055             !is_del) {
4056                 struct lod_thread_info *info = lod_env_info(env);
4057                 struct lov_user_md_v3 *v3  = info->lti_ea_store;
4058
4059                 memset(v3, 0, sizeof(*v3));
4060                 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4061                 v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4062                 v3->lmm_stripe_count = cpu_to_le32(v1->lmm_stripe_count);
4063                 v3->lmm_stripe_offset = cpu_to_le32(v1->lmm_stripe_offset);
4064                 v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4065
4066                 strlcpy(v3->lmm_pool_name, pool, sizeof(v3->lmm_pool_name));
4067
4068                 info->lti_buf.lb_buf = v3;
4069                 info->lti_buf.lb_len = sizeof(*v3);
4070                 rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4071                                               name, fl, th);
4072         } else if (v1->lmm_magic == LOV_USER_MAGIC_COMP_V1 &&
4073                    pool[0] != '\0' && !is_del) {
4074                 /*
4075                  * try to retain the pool from default layout if the
4076                  * specified component layout does not provide pool
4077                  * info explicitly
4078                  */
4079                 struct lod_thread_info *info = lod_env_info(env);
4080                 struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
4081                 struct lov_comp_md_v1 *comp_v1p;
4082                 struct lov_user_md_v1 *lum;
4083                 int entry_count;
4084                 int i;
4085                 __u32 offset;
4086                 struct lov_comp_md_entry_v1 *entry;
4087                 int size;
4088
4089                 entry_count = le16_to_cpu(comp_v1->lcm_entry_count);
4090                 size = sizeof(*comp_v1) +
4091                         entry_count * sizeof(comp_v1->lcm_entries[0]);
4092                 entry = comp_v1->lcm_entries;
4093                 for (i = 0; i < entry_count; i++, entry++) {
4094                         offset = le32_to_cpu(entry->lcme_offset);
4095                         lum = (struct lov_user_md_v1 *)((char *)comp_v1 +
4096                                                         offset);
4097                         if (le32_to_cpu(lum->lmm_magic) != LOV_USER_MAGIC_V1)
4098                                 /* the i-th component includes pool info */
4099                                 break;
4100                         if (lum->lmm_pattern == cpu_to_le32(LOV_PATTERN_MDT))
4101                                 size += sizeof(struct lov_user_md_v1);
4102                         else
4103                                 size += sizeof(struct lov_user_md_v3);
4104                 }
4105
4106                 if (i == entry_count) {
4107                         /*
4108                          * re-compose the layout to include the pool for
4109                          * each component
4110                          */
4111                         if (info->lti_ea_store_size < size)
4112                                 rc = lod_ea_store_resize(info, size);
4113
4114                         if (rc == 0) {
4115                                 comp_v1p = info->lti_ea_store;
4116                                 *comp_v1p = *comp_v1;
4117                                 comp_v1p->lcm_size = cpu_to_le32(size);
4118                                 embed_pool_to_comp_v1(comp_v1, pool, comp_v1p);
4119
4120                                 info->lti_buf.lb_buf = comp_v1p;
4121                                 info->lti_buf.lb_len = size;
4122                                 rc = lod_xattr_set_lov_on_dir(env, dt,
4123                                                               &info->lti_buf,
4124                                                               name, fl, th);
4125                         }
4126                 } else {
4127                         rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl,
4128                                                       th);
4129                 }
4130         } else {
4131                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
4132         }
4133
4134         if (lds->lds_def_striping_set == 1 && lds->lds_def_comp_entries != NULL)
4135                 lod_free_def_comp_entries(lds);
4136
4137         RETURN(rc);
4138 }
4139
4140 /**
4141  * Set default striping on a directory object.
4142  *
4143  * Sets specified striping on a directory object unless it matches the default
4144  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
4145  * EA. This striping will be used when a new directory is being created in the
4146  * directory.
4147  *
4148  * \param[in] env       execution environment
4149  * \param[in] dt        the striped object
4150  * \param[in] buf       buffer with the striping
4151  * \param[in] name      name of EA
4152  * \param[in] fl        xattr flag (see OSD API description)
4153  * \param[in] th        transaction handle
4154  *
4155  * \retval              0 on success
4156  * \retval              negative if failed
4157  */
4158 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
4159                                             struct dt_object *dt,
4160                                             const struct lu_buf *buf,
4161                                             const char *name, int fl,
4162                                             struct thandle *th)
4163 {
4164         struct lmv_user_md_v1 *lum;
4165         int rc;
4166
4167         ENTRY;
4168
4169         LASSERT(buf != NULL && buf->lb_buf != NULL);
4170         lum = buf->lb_buf;
4171
4172         CDEBUG(D_INFO,
4173                "set default stripe_count # %u stripe_offset %d hash %u\n",
4174               le32_to_cpu(lum->lum_stripe_count),
4175               (int)le32_to_cpu(lum->lum_stripe_offset),
4176               le32_to_cpu(lum->lum_hash_type));
4177
4178         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
4179                                  le32_to_cpu(lum->lum_stripe_offset)) &&
4180             le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
4181                 rc = lod_xattr_del_internal(env, dt, name, th);
4182                 if (rc == -ENODATA)
4183                         rc = 0;
4184         } else {
4185                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4186                 if (rc != 0)
4187                         RETURN(rc);
4188         }
4189
4190         RETURN(rc);
4191 }
4192
4193 /**
4194  * Turn directory into a striped directory.
4195  *
4196  * During replay the client sends the striping created before MDT
4197  * failure, then the layer above LOD sends this defined striping
4198  * using ->do_xattr_set(), so LOD uses this method to replay creation
4199  * of the stripes. Notice the original information for the striping
4200  * (#stripes, FIDs, etc) was transferred in declare path.
4201  *
4202  * \param[in] env       execution environment
4203  * \param[in] dt        the striped object
4204  * \param[in] buf       not used currently
4205  * \param[in] name      not used currently
4206  * \param[in] fl        xattr flag (see OSD API description)
4207  * \param[in] th        transaction handle
4208  *
4209  * \retval              0 on success
4210  * \retval              negative if failed
4211  */
4212 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
4213                              const struct lu_buf *buf, const char *name,
4214                              int fl, struct thandle *th)
4215 {
4216         struct lod_object       *lo = lod_dt_obj(dt);
4217         struct lod_thread_info  *info = lod_env_info(env);
4218         struct lu_attr          *attr = &info->lti_attr;
4219         struct dt_object_format *dof = &info->lti_format;
4220         struct lu_buf           lmv_buf;
4221         struct lu_buf           slave_lmv_buf;
4222         struct lmv_mds_md_v1    *lmm;
4223         struct lmv_mds_md_v1    *slave_lmm = NULL;
4224         struct dt_insert_rec    *rec = &info->lti_dt_rec;
4225         int                     i;
4226         int                     rc;
4227         ENTRY;
4228
4229         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4230                 RETURN(-ENOTDIR);
4231
4232         /* The stripes are supposed to be allocated in declare phase,
4233          * if there are no stripes being allocated, it will skip */
4234         if (lo->ldo_dir_stripe_count == 0) {
4235                 if (lo->ldo_is_foreign) {
4236                         rc = lod_sub_xattr_set(env, dt_object_child(dt), buf,
4237                                                XATTR_NAME_LMV, fl, th);
4238                         if (rc != 0)
4239                                 RETURN(rc);
4240                 }
4241                 RETURN(0);
4242         }
4243
4244         rc = dt_attr_get(env, dt_object_child(dt), attr);
4245         if (rc != 0)
4246                 RETURN(rc);
4247
4248         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_FLAGS |
4249                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
4250         dof->dof_type = DFT_DIR;
4251
4252         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
4253         if (rc != 0)
4254                 RETURN(rc);
4255         lmm = lmv_buf.lb_buf;
4256
4257         OBD_ALLOC_PTR(slave_lmm);
4258         if (slave_lmm == NULL)
4259                 RETURN(-ENOMEM);
4260
4261         lod_prep_slave_lmv_md(slave_lmm, lmm);
4262         slave_lmv_buf.lb_buf = slave_lmm;
4263         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
4264
4265         rec->rec_type = S_IFDIR;
4266         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4267                 struct dt_object *dto = lo->ldo_stripe[i];
4268                 char *stripe_name = info->lti_key;
4269                 struct lu_name *sname;
4270                 struct linkea_data ldata = { NULL };
4271                 struct lu_buf linkea_buf;
4272
4273                 /* OBD_FAIL_MDS_STRIPE_FID may leave stripe uninitialized */
4274                 if (!dto)
4275                         continue;
4276
4277                 /* fail a remote stripe creation */
4278                 if (i && CFS_FAIL_CHECK(OBD_FAIL_MDS_STRIPE_CREATE))
4279                         continue;
4280
4281                 /* don't create stripe if:
4282                  * 1. it's source stripe of migrating directory
4283                  * 2. it's existed stripe of splitting directory
4284                  */
4285                 if ((lod_is_migrating(lo) && i >= lo->ldo_dir_migrate_offset) ||
4286                     (lod_is_splitting(lo) && i < lo->ldo_dir_split_offset)) {
4287                         if (!dt_object_exists(dto))
4288                                 GOTO(out, rc = -EINVAL);
4289                 } else {
4290                         dt_write_lock(env, dto, DT_TGT_CHILD);
4291                         rc = lod_sub_create(env, dto, attr, NULL, dof, th);
4292                         if (rc != 0) {
4293                                 dt_write_unlock(env, dto);
4294                                 GOTO(out, rc);
4295                         }
4296
4297                         rc = lod_sub_ref_add(env, dto, th);
4298                         dt_write_unlock(env, dto);
4299                         if (rc != 0)
4300                                 GOTO(out, rc);
4301
4302                         rec->rec_fid = lu_object_fid(&dto->do_lu);
4303                         rc = lod_sub_insert(env, dto,
4304                                             (const struct dt_rec *)rec,
4305                                             (const struct dt_key *)dot, th);
4306                         if (rc != 0)
4307                                 GOTO(out, rc);
4308                 }
4309
4310                 if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
4311                     cfs_fail_val != i) {
4312                         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
4313                             cfs_fail_val == i)
4314                                 slave_lmm->lmv_master_mdt_index =
4315                                                         cpu_to_le32(i + 1);
4316                         else
4317                                 slave_lmm->lmv_master_mdt_index =
4318                                                         cpu_to_le32(i);
4319
4320                         rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
4321                                                XATTR_NAME_LMV, 0, th);
4322                         if (rc != 0)
4323                                 GOTO(out, rc);
4324                 }
4325
4326                 /* don't insert stripe if it's existed stripe of splitting
4327                  * directory (this directory is striped).
4328                  * NB, plain directory will insert itself as the first
4329                  * stripe in target.
4330                  */
4331                 if (lod_is_splitting(lo) && lo->ldo_dir_split_offset > 1 &&
4332                     lo->ldo_dir_split_offset > i)
4333                         continue;
4334
4335                 rec->rec_fid = lu_object_fid(&dt->do_lu);
4336                 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
4337                                     (const struct dt_key *)dotdot, th);
4338                 if (rc != 0)
4339                         GOTO(out, rc);
4340
4341                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
4342                     cfs_fail_val == i)
4343                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4344                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
4345                 else
4346                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4347                                  PFID(lu_object_fid(&dto->do_lu)), i);
4348
4349                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
4350                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
4351                                       sname, lu_object_fid(&dt->do_lu));
4352                 if (rc != 0)
4353                         GOTO(out, rc);
4354
4355                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
4356                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
4357                 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
4358                                        XATTR_NAME_LINK, 0, th);
4359                 if (rc != 0)
4360                         GOTO(out, rc);
4361
4362                 rec->rec_fid = lu_object_fid(&dto->do_lu);
4363                 rc = lod_sub_insert(env, dt_object_child(dt),
4364                                     (const struct dt_rec *)rec,
4365                                     (const struct dt_key *)stripe_name, th);
4366                 if (rc != 0)
4367                         GOTO(out, rc);
4368
4369                 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
4370                 if (rc != 0)
4371                         GOTO(out, rc);
4372         }
4373
4374         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
4375                 rc = lod_sub_xattr_set(env, dt_object_child(dt),
4376                                        &lmv_buf, XATTR_NAME_LMV, fl, th);
4377 out:
4378         if (slave_lmm != NULL)
4379                 OBD_FREE_PTR(slave_lmm);
4380
4381         RETURN(rc);
4382 }
4383
4384 /**
4385  * Helper function to declare/execute creation of a striped directory
4386  *
4387  * Called in declare/create object path, prepare striping for a directory
4388  * and prepare defaults data striping for the objects to be created in
4389  * that directory. Notice the function calls "declaration" or "execution"
4390  * methods depending on \a declare param. This is a consequence of the
4391  * current approach while we don't have natural distributed transactions:
4392  * we basically execute non-local updates in the declare phase. So, the
4393  * arguments for the both phases are the same and this is the reason for
4394  * this function to exist.
4395  *
4396  * \param[in] env       execution environment
4397  * \param[in] dt        object
4398  * \param[in] attr      attributes the stripes will be created with
4399  * \param[in] lmu       lmv_user_md if MDT indices are specified
4400  * \param[in] dof       format of stripes (see OSD API description)
4401  * \param[in] th        transaction handle
4402  * \param[in] declare   where to call "declare" or "execute" methods
4403  *
4404  * \retval              0 on success
4405  * \retval              negative if failed
4406  */
4407 static int lod_dir_striping_create_internal(const struct lu_env *env,
4408                                             struct dt_object *dt,
4409                                             struct lu_attr *attr,
4410                                             const struct lu_buf *lmu,
4411                                             struct dt_object_format *dof,
4412                                             struct thandle *th,
4413                                             bool declare)
4414 {
4415         struct lod_thread_info *info = lod_env_info(env);
4416         struct lod_object *lo = lod_dt_obj(dt);
4417         const struct lod_default_striping *lds = lo->ldo_def_striping;
4418         int rc;
4419         ENTRY;
4420
4421         LASSERT(ergo(lds != NULL,
4422                      lds->lds_def_striping_set ||
4423                      lds->lds_dir_def_striping_set));
4424
4425         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
4426                                  lo->ldo_dir_stripe_offset)) {
4427                 if (!lmu) {
4428                         struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4429                         int stripe_count = lo->ldo_dir_stripe_count;
4430
4431                         if (info->lti_ea_store_size < sizeof(*v1)) {
4432                                 rc = lod_ea_store_resize(info, sizeof(*v1));
4433                                 if (rc != 0)
4434                                         RETURN(rc);
4435                                 v1 = info->lti_ea_store;
4436                         }
4437
4438                         memset(v1, 0, sizeof(*v1));
4439                         v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4440                         v1->lum_stripe_count = cpu_to_le32(stripe_count);
4441                         v1->lum_stripe_offset =
4442                                         cpu_to_le32(lo->ldo_dir_stripe_offset);
4443
4444                         info->lti_buf.lb_buf = v1;
4445                         info->lti_buf.lb_len = sizeof(*v1);
4446                         lmu = &info->lti_buf;
4447                 }
4448
4449                 if (declare)
4450                         rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
4451                                                        th);
4452                 else
4453                         rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
4454                                                th);
4455                 if (rc != 0)
4456                         RETURN(rc);
4457         } else {
4458                 /* foreign LMV EA case */
4459                 if (lmu) {
4460                         struct lmv_foreign_md *lfm = lmu->lb_buf;
4461
4462                         if (lfm->lfm_magic == LMV_MAGIC_FOREIGN) {
4463                                 rc = lod_declare_xattr_set_lmv(env, dt, attr,
4464                                                                lmu, dof, th);
4465                         }
4466                 } else {
4467                         if (lo->ldo_is_foreign) {
4468                                 LASSERT(lo->ldo_foreign_lmv != NULL &&
4469                                         lo->ldo_foreign_lmv_size > 0);
4470                                 info->lti_buf.lb_buf = lo->ldo_foreign_lmv;
4471                                 info->lti_buf.lb_len = lo->ldo_foreign_lmv_size;
4472                                 lmu = &info->lti_buf;
4473                                 rc = lod_xattr_set_lmv(env, dt, lmu,
4474                                                        XATTR_NAME_LMV, 0, th);
4475                         }
4476                 }
4477         }
4478
4479         /* Transfer default LMV striping from the parent */
4480         if (lds != NULL && lds->lds_dir_def_striping_set &&
4481             lds->lds_dir_def_max_inherit != LMV_INHERIT_END &&
4482             lds->lds_dir_def_max_inherit != LMV_INHERIT_NONE &&
4483             !(LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
4484                                  lds->lds_dir_def_stripe_offset) &&
4485               le32_to_cpu(lds->lds_dir_def_hash_type) !=
4486               LMV_HASH_TYPE_UNKNOWN)) {
4487                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
4488
4489                 if (info->lti_ea_store_size < sizeof(*v1)) {
4490                         rc = lod_ea_store_resize(info, sizeof(*v1));
4491                         if (rc != 0)
4492                                 RETURN(rc);
4493                         v1 = info->lti_ea_store;
4494                 }
4495
4496                 memset(v1, 0, sizeof(*v1));
4497                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
4498                 v1->lum_stripe_count =
4499                         cpu_to_le32(lds->lds_dir_def_stripe_count);
4500                 v1->lum_stripe_offset =
4501                         cpu_to_le32(lds->lds_dir_def_stripe_offset);
4502                 v1->lum_hash_type =
4503                         cpu_to_le32(lds->lds_dir_def_hash_type);
4504                 v1->lum_max_inherit =
4505                         lmv_inherit_next(lds->lds_dir_def_max_inherit);
4506                 v1->lum_max_inherit_rr =
4507                         lmv_inherit_rr_next(lds->lds_dir_def_max_inherit_rr);
4508
4509                 info->lti_buf.lb_buf = v1;
4510                 info->lti_buf.lb_len = sizeof(*v1);
4511                 if (declare)
4512                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4513                                                        XATTR_NAME_DEFAULT_LMV,
4514                                                        0, th);
4515                 else
4516                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
4517                                                   &info->lti_buf,
4518                                                   XATTR_NAME_DEFAULT_LMV, 0,
4519                                                   th);
4520                 if (rc != 0)
4521                         RETURN(rc);
4522         }
4523
4524         /* Transfer default LOV striping from the parent */
4525         if (lds != NULL && lds->lds_def_striping_set &&
4526             lds->lds_def_comp_cnt != 0) {
4527                 struct lov_mds_md *lmm;
4528                 int lmm_size = lod_comp_md_size(lo, true);
4529
4530                 if (info->lti_ea_store_size < lmm_size) {
4531                         rc = lod_ea_store_resize(info, lmm_size);
4532                         if (rc != 0)
4533                                 RETURN(rc);
4534                 }
4535                 lmm = info->lti_ea_store;
4536
4537                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
4538                 if (rc != 0)
4539                         RETURN(rc);
4540
4541                 info->lti_buf.lb_buf = lmm;
4542                 info->lti_buf.lb_len = lmm_size;
4543
4544                 if (declare)
4545                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
4546                                                        XATTR_NAME_LOV, 0, th);
4547                 else
4548                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4549                                                       XATTR_NAME_LOV, 0, th);
4550                 if (rc != 0)
4551                         RETURN(rc);
4552         }
4553
4554         /* ldo_def_striping is not allocated, clear after use, in case directory
4555          * layout is changed later.
4556          */
4557         if (!declare)
4558                 lo->ldo_def_striping = NULL;
4559
4560         RETURN(0);
4561 }
4562
4563 static int lod_declare_dir_striping_create(const struct lu_env *env,
4564                                            struct dt_object *dt,
4565                                            struct lu_attr *attr,
4566                                            struct lu_buf *lmu,
4567                                            struct dt_object_format *dof,
4568                                            struct thandle *th)
4569 {
4570         return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
4571                                                 true);
4572 }
4573
4574 static int lod_dir_striping_create(const struct lu_env *env,
4575                                    struct dt_object *dt,
4576                                    struct lu_attr *attr,
4577                                    struct dt_object_format *dof,
4578                                    struct thandle *th)
4579 {
4580         return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
4581                                                 false);
4582 }
4583
4584 /**
4585  * Make LOV EA for striped object.
4586  *
4587  * Generate striping information and store it in the LOV EA of the given
4588  * object. The caller must ensure nobody else is calling the function
4589  * against the object concurrently. The transaction must be started.
4590  * FLDB service must be running as well; it's used to map FID to the target,
4591  * which is stored in LOV EA.
4592  *
4593  * \param[in] env               execution environment for this thread
4594  * \param[in] lo                LOD object
4595  * \param[in] th                transaction handle
4596  *
4597  * \retval                      0 if LOV EA is stored successfully
4598  * \retval                      negative error number on failure
4599  */
4600 static int lod_generate_and_set_lovea(const struct lu_env *env,
4601                                       struct lod_object *lo,
4602                                       struct thandle *th)
4603 {
4604         struct lod_thread_info  *info = lod_env_info(env);
4605         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
4606         struct lov_mds_md_v1    *lmm;
4607         int                      rc, lmm_size;
4608         ENTRY;
4609
4610         LASSERT(lo);
4611
4612         if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
4613                 lod_striping_free_nolock(env, lo);
4614                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
4615                 RETURN(rc);
4616         }
4617
4618         lmm_size = lod_comp_md_size(lo, false);
4619         if (info->lti_ea_store_size < lmm_size) {
4620                 rc = lod_ea_store_resize(info, lmm_size);
4621                 if (rc)
4622                         RETURN(rc);
4623         }
4624         lmm = info->lti_ea_store;
4625
4626         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
4627         if (rc)
4628                 RETURN(rc);
4629
4630         info->lti_buf.lb_buf = lmm;
4631         info->lti_buf.lb_len = lmm_size;
4632         rc = lod_sub_xattr_set(env, next, &info->lti_buf,
4633                                XATTR_NAME_LOV, 0, th);
4634         RETURN(rc);
4635 }
4636
4637 static __u32 lod_gen_component_id(struct lod_object *lo,
4638                                   int mirror_id, int comp_idx);
4639
4640 /**
4641  * Repeat an existing component
4642  *
4643  * Creates a new layout by replicating an existing component.  Uses striping
4644  * policy from previous component as a template for the striping for the new
4645  * new component.
4646  *
4647  * New component starts with zero length, will be extended (or removed) before
4648  * returning layout to client.
4649  *
4650  * NB: Reallocates layout components array (lo->ldo_comp_entries), invalidating
4651  * any pre-existing pointers to components.  Handle with care.
4652  *
4653  * \param[in] env       execution environment for this thread
4654  * \param[in,out] lo    object to update the layout of
4655  * \param[in] index     index of component to copy
4656  *
4657  * \retval      0 on success
4658  * \retval      negative errno on error
4659  */
4660 static int lod_layout_repeat_comp(const struct lu_env *env,
4661                                   struct lod_object *lo, int index)
4662 {
4663         struct lod_layout_component *lod_comp;
4664         struct lod_layout_component *new_comp = NULL;
4665         struct lod_layout_component *comp_array;
4666         int rc = 0, i, new_cnt = lo->ldo_comp_cnt + 1;
4667         __u16 mirror_id;
4668         int offset = 0;
4669         ENTRY;
4670
4671         lod_comp = &lo->ldo_comp_entries[index];
4672         LASSERT(lod_comp_inited(lod_comp) && lod_comp->llc_id != LCME_ID_INVAL);
4673
4674         CDEBUG(D_LAYOUT, "repeating component %d\n", index);
4675
4676         OBD_ALLOC_PTR_ARRAY(comp_array, new_cnt);
4677         if (comp_array == NULL)
4678                 GOTO(out, rc = -ENOMEM);
4679
4680         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4681                 memcpy(&comp_array[i + offset], &lo->ldo_comp_entries[i],
4682                        sizeof(*comp_array));
4683
4684                 /* Duplicate this component in to the next slot */
4685                 if (i == index) {
4686                         new_comp = &comp_array[i + 1];
4687                         memcpy(&comp_array[i + 1], &lo->ldo_comp_entries[i],
4688                                sizeof(*comp_array));
4689                         /* We must now skip this new component when copying */
4690                         offset = 1;
4691                 }
4692         }
4693
4694         /* Set up copied component */
4695         new_comp->llc_flags &= ~LCME_FL_INIT;
4696         new_comp->llc_stripe = NULL;
4697         new_comp->llc_stripes_allocated = 0;
4698         new_comp->llc_ost_indices = NULL;
4699         new_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4700         /* for uninstantiated components, layout gen stores default stripe
4701          * offset */
4702         new_comp->llc_layout_gen = lod_comp->llc_stripe_offset;
4703         /* This makes the repeated component zero-length, placed at the end of
4704          * the preceding component */
4705         new_comp->llc_extent.e_start = new_comp->llc_extent.e_end;
4706         new_comp->llc_timestamp = lod_comp->llc_timestamp;
4707         new_comp->llc_pool = NULL;
4708
4709         rc = lod_set_pool(&new_comp->llc_pool, lod_comp->llc_pool);
4710         if (rc)
4711                 GOTO(out, rc);
4712
4713         if (new_comp->llc_ostlist.op_array) {
4714                 __u32 *op_array = NULL;
4715
4716                 OBD_ALLOC(op_array, new_comp->llc_ostlist.op_size);
4717                 if (!op_array)
4718                         GOTO(out, rc = -ENOMEM);
4719                 memcpy(op_array, &new_comp->llc_ostlist.op_array,
4720                        new_comp->llc_ostlist.op_size);
4721                 new_comp->llc_ostlist.op_array = op_array;
4722         }
4723
4724         OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4725         lo->ldo_comp_entries = comp_array;
4726         lo->ldo_comp_cnt = new_cnt;
4727
4728         /* Generate an id for the new component */
4729         mirror_id = mirror_id_of(new_comp->llc_id);
4730         new_comp->llc_id = LCME_ID_INVAL;
4731         new_comp->llc_id = lod_gen_component_id(lo, mirror_id, index + 1);
4732         if (new_comp->llc_id == LCME_ID_INVAL)
4733                 GOTO(out, rc = -ERANGE);
4734
4735         EXIT;
4736 out:
4737         if (rc)
4738                 OBD_FREE_PTR_ARRAY(comp_array, new_cnt);
4739
4740         return rc;
4741 }
4742
4743 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
4744 {
4745         ENTRY;
4746
4747         /* clear memory region that will be used for layout change */
4748         memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
4749         info->lti_count = 0;
4750
4751         if (info->lti_comp_size >= comp_cnt)
4752                 RETURN(0);
4753
4754         if (info->lti_comp_size > 0) {
4755                 OBD_FREE_PTR_ARRAY(info->lti_comp_idx, info->lti_comp_size);
4756                 info->lti_comp_size = 0;
4757         }
4758
4759         OBD_ALLOC_PTR_ARRAY(info->lti_comp_idx, comp_cnt);
4760         if (!info->lti_comp_idx)
4761                 RETURN(-ENOMEM);
4762
4763         info->lti_comp_size = comp_cnt;
4764         RETURN(0);
4765 }
4766
4767 /**
4768  * Prepare new layout minus deleted components
4769  *
4770  * Removes components marked for deletion (LCME_ID_INVAL) by copying to a new
4771  * layout and skipping those components.  Removes stripe objects if any exist.
4772  *
4773  * NB:
4774  * Reallocates layout components array (lo->ldo_comp_entries), invalidating
4775  * any pre-existing pointers to components.
4776  *
4777  * Caller is responsible for updating mirror end (ldo_mirror[].lme_end).
4778  *
4779  * \param[in] env       execution environment for this thread
4780  * \param[in,out] lo    object to update the layout of
4781  * \param[in] th        transaction handle for this operation
4782  *
4783  * \retval      # of components deleted
4784  * \retval      negative errno on error
4785  */
4786 static int lod_layout_del_prep_layout(const struct lu_env *env,
4787                                       struct lod_object *lo,
4788                                       struct thandle *th)
4789 {
4790         struct lod_layout_component     *lod_comp;
4791         struct lod_thread_info  *info = lod_env_info(env);
4792         int rc = 0, i, j, deleted = 0;
4793
4794         ENTRY;
4795
4796         LASSERT(lo->ldo_is_composite);
4797         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
4798
4799         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
4800         if (rc)
4801                 RETURN(rc);
4802
4803         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4804                 lod_comp = &lo->ldo_comp_entries[i];
4805
4806                 if (lod_comp->llc_id != LCME_ID_INVAL) {
4807                         /* Build array of things to keep */
4808                         info->lti_comp_idx[info->lti_count++] = i;
4809                         continue;
4810                 }
4811
4812                 lod_obj_set_pool(lo, i, NULL);
4813                 if (lod_comp->llc_ostlist.op_array) {
4814                         OBD_FREE(lod_comp->llc_ostlist.op_array,
4815                                  lod_comp->llc_ostlist.op_size);
4816                         lod_comp->llc_ostlist.op_array = NULL;
4817                         lod_comp->llc_ostlist.op_size = 0;
4818                 }
4819
4820                 deleted++;
4821                 CDEBUG(D_LAYOUT, "deleting comp %d, left %d\n", i,
4822                        lo->ldo_comp_cnt - deleted);
4823
4824                 /* No striping info for this component */
4825                 if (lod_comp->llc_stripe == NULL)
4826                         continue;
4827
4828                 LASSERT(lod_comp->llc_stripe_count > 0);
4829                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4830                         struct dt_object *obj = lod_comp->llc_stripe[j];
4831
4832                         if (obj == NULL)
4833                                 continue;
4834
4835                         /* components which are not init have no sub objects
4836                          * to destroy */
4837                         if (lod_comp_inited(lod_comp)) {
4838                                 rc = lod_sub_destroy(env, obj, th);
4839                                 if (rc)
4840                                         GOTO(out, rc);
4841                         }
4842
4843                         lu_object_put(env, &obj->do_lu);
4844                         lod_comp->llc_stripe[j] = NULL;
4845                 }
4846                 OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
4847                                    lod_comp->llc_stripes_allocated);
4848                 lod_comp->llc_stripe = NULL;
4849                 OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
4850                                    lod_comp->llc_stripes_allocated);
4851                 lod_comp->llc_ost_indices = NULL;
4852                 lod_comp->llc_stripes_allocated = 0;
4853         }
4854
4855         /* info->lti_count has the amount of left components */
4856         LASSERTF(info->lti_count >= 0 && info->lti_count < lo->ldo_comp_cnt,
4857                  "left = %d, lo->ldo_comp_cnt %d\n", (int)info->lti_count,
4858                  (int)lo->ldo_comp_cnt);
4859
4860         if (info->lti_count > 0) {
4861                 struct lod_layout_component *comp_array;
4862
4863                 OBD_ALLOC_PTR_ARRAY(comp_array, info->lti_count);
4864                 if (comp_array == NULL)
4865                         GOTO(out, rc = -ENOMEM);
4866
4867                 for (i = 0; i < info->lti_count; i++) {
4868                         memcpy(&comp_array[i],
4869                                &lo->ldo_comp_entries[info->lti_comp_idx[i]],
4870                                sizeof(*comp_array));
4871                 }
4872
4873                 OBD_FREE_PTR_ARRAY(lo->ldo_comp_entries, lo->ldo_comp_cnt);
4874                 lo->ldo_comp_entries = comp_array;
4875                 lo->ldo_comp_cnt = info->lti_count;
4876         } else {
4877                 lod_free_comp_entries(lo);
4878         }
4879
4880         EXIT;
4881 out:
4882         return rc ? rc : deleted;
4883 }
4884
4885 /**
4886  * Delete layout component(s)
4887  *
4888  * This function sets up the layout data in the env and does the setattrs
4889  * required to write out the new layout.  The layout itself is modified in
4890  * lod_layout_del_prep_layout.
4891  *
4892  * \param[in] env       execution environment for this thread
4893  * \param[in] dt        object
4894  * \param[in] th        transaction handle
4895  *
4896  * \retval      0 on success
4897  * \retval      negative error number on failure
4898  */
4899 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
4900                           struct thandle *th)
4901 {
4902         struct lod_object *lo = lod_dt_obj(dt);
4903         struct dt_object *next = dt_object_child(dt);
4904         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4905         int rc;
4906
4907         LASSERT(lo->ldo_mirror_count == 1);
4908
4909         mutex_lock(&lo->ldo_layout_mutex);
4910
4911         rc = lod_layout_del_prep_layout(env, lo, th);
4912         if (rc < 0)
4913                 GOTO(out, rc);
4914
4915         /* Only do this if we didn't delete all components */
4916         if (lo->ldo_comp_cnt > 0) {
4917                 lo->ldo_mirrors[0].lme_end = lo->ldo_comp_cnt - 1;
4918                 lod_obj_inc_layout_gen(lo);
4919         }
4920
4921         LASSERT(dt_object_exists(dt));
4922         rc = dt_attr_get(env, next, attr);
4923         if (rc)
4924                 GOTO(out, rc);
4925
4926         if (attr->la_size > 0) {
4927                 attr->la_size = 0;
4928                 attr->la_valid = LA_SIZE;
4929                 rc = lod_sub_attr_set(env, next, attr, th);
4930                 if (rc)
4931                         GOTO(out, rc);
4932         }
4933
4934         rc = lod_generate_and_set_lovea(env, lo, th);
4935         EXIT;
4936 out:
4937         if (rc)
4938                 lod_striping_free_nolock(env, lo);
4939
4940         mutex_unlock(&lo->ldo_layout_mutex);
4941
4942         return rc;
4943 }
4944
4945
4946 /**
4947  * Implementation of dt_object_operations::do_xattr_set.
4948  *
4949  * Sets specified extended attribute on the object. Three types of EAs are
4950  * special:
4951  *   LOV EA - stores striping for a regular file or default striping (when set
4952  *            on a directory)
4953  *   LMV EA - stores a marker for the striped directories
4954  *   DMV EA - stores default directory striping
4955  *
4956  * When striping is applied to a non-striped existing object (this is called
4957  * late striping), then LOD notices the caller wants to turn the object into a
4958  * striped one. The stripe objects are created and appropriate EA is set:
4959  * LOV EA storing all the stripes directly or LMV EA storing just a small header
4960  * with striping configuration.
4961  *
4962  * \see dt_object_operations::do_xattr_set() in the API description for details.
4963  */
4964 static int lod_xattr_set(const struct lu_env *env,
4965                          struct dt_object *dt, const struct lu_buf *buf,
4966                          const char *name, int fl, struct thandle *th)
4967 {
4968         struct dt_object *next = dt_object_child(dt);
4969         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
4970         struct lod_object *lo = lod_dt_obj(dt);
4971         struct lod_obj_stripe_cb_data data = { {0} };
4972         int rc = 0;
4973
4974         ENTRY;
4975
4976         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4977             !strcmp(name, XATTR_NAME_LMV)) {
4978                 switch (fl) {
4979                 case LU_XATTR_CREATE:
4980                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4981                         break;
4982                 case 0:
4983                 case LU_XATTR_REPLACE:
4984                         rc = lod_dir_layout_set(env, dt, buf, fl, th);
4985                         break;
4986                 default:
4987                         LBUG();
4988                 }
4989
4990                 RETURN(rc);
4991         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4992                    strcmp(name, XATTR_NAME_LOV) == 0) {
4993                 rc = lod_xattr_set_default_lov_on_dir(env, dt, buf, name, fl,
4994                                                       th);
4995                 RETURN(rc);
4996         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4997                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4998                 /* default LMVEA */
4999                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
5000                                                       th);
5001                 RETURN(rc);
5002         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5003                    (strcmp(name, XATTR_NAME_LOV) == 0 ||
5004                     strcmp(name, XATTR_LUSTRE_LOV) == 0 ||
5005                     allowed_lustre_lov(name))) {
5006                 /* in case of lov EA swap, just set it
5007                  * if not, it is a replay so check striping match what we
5008                  * already have during req replay, declare_xattr_set()
5009                  * defines striping, then create() does the work */
5010                 if (fl & LU_XATTR_REPLACE) {
5011                         /* free stripes, then update disk */
5012                         lod_striping_free(env, lod_dt_obj(dt));
5013
5014                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
5015                 } else if (fl & LU_XATTR_SPLIT) {
5016                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
5017                         if (rc)
5018                                 RETURN(rc);
5019
5020                         rc = lod_striping_reload(env, lo, buf, LVF_ALL_STALE);
5021                         if (rc)
5022                                 RETURN(rc);
5023
5024                         if (lo->ldo_mirror_count > 1 &&
5025                             layout_attr->la_valid & LA_LAYOUT_VERSION) {
5026                                 /* mirror split */
5027                                 layout_attr->la_layout_version =
5028                                                 lo->ldo_layout_gen;
5029                                 data.locd_attr = layout_attr;
5030                                 data.locd_declare = false;
5031                                 data.locd_stripe_cb =
5032                                                 lod_obj_stripe_attr_set_cb;
5033                                 rc = lod_obj_for_each_stripe(env, lo, th,
5034                                                              &data);
5035                                 if (rc)
5036                                         RETURN(rc);
5037                         }
5038                 } else if (fl & LU_XATTR_PURGE) {
5039                         rc = lod_layout_purge(env, dt, buf, th);
5040                 } else if (dt_object_remote(dt)) {
5041                         /* This only happens during migration, see
5042                          * mdd_migrate_create(), in which Master MDT will
5043                          * create a remote target object, and only set
5044                          * (migrating) stripe EA on the remote object,
5045                          * and does not need creating each stripes. */
5046                         rc = lod_sub_xattr_set(env, next, buf, name,
5047                                                       fl, th);
5048                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
5049                         /* delete component(s) */
5050                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
5051                         rc = lod_layout_del(env, dt, th);
5052                 } else {
5053                         /*
5054                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
5055                          * it's going to create create file with specified
5056                          * component(s), the striping must have not being
5057                          * cached in this case;
5058                          *
5059                          * Otherwise, it's going to add/change component(s) to
5060                          * an existing file, the striping must have been cached
5061                          * in this case.
5062                          */
5063                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
5064                                      !strcmp(name, XATTR_NAME_LOV),
5065                                 !lod_dt_obj(dt)->ldo_comp_cached));
5066
5067                         rc = lod_striped_create(env, dt, NULL, NULL, th);
5068                         if (rc)
5069                                 RETURN(rc);
5070
5071                         if (fl & LU_XATTR_MERGE && lo->ldo_mirror_count > 1 &&
5072                             layout_attr->la_valid & LA_LAYOUT_VERSION) {
5073                                 /* mirror merge exec phase */
5074                                 layout_attr->la_layout_version =
5075                                                 lo->ldo_layout_gen;
5076                                 data.locd_attr = layout_attr;
5077                                 data.locd_declare = false;
5078                                 data.locd_stripe_cb =
5079                                                 lod_obj_stripe_attr_set_cb;
5080                                 rc = lod_obj_for_each_stripe(env, lo, th,
5081                                                              &data);
5082                                 if (rc)
5083                                         RETURN(rc);
5084                         }
5085                 }
5086                 RETURN(rc);
5087         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
5088                 rc = lod_replace_parent_fid(env, dt, buf, th, false);
5089
5090                 RETURN(rc);
5091         }
5092
5093         /* then all other xattr */
5094         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
5095
5096         RETURN(rc);
5097 }
5098
5099 /**
5100  * Implementation of dt_object_operations::do_declare_xattr_del.
5101  *
5102  * \see dt_object_operations::do_declare_xattr_del() in the API description
5103  * for details.
5104  */
5105 static int lod_declare_xattr_del(const struct lu_env *env,
5106                                  struct dt_object *dt, const char *name,
5107                                  struct thandle *th)
5108 {
5109         struct lod_object *lo = lod_dt_obj(dt);
5110         struct dt_object *next = dt_object_child(dt);
5111         int i;
5112         int rc;
5113         ENTRY;
5114
5115         rc = lod_sub_declare_xattr_del(env, next, name, th);
5116         if (rc != 0)
5117                 RETURN(rc);
5118
5119         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5120                 RETURN(0);
5121
5122         /* NB: don't delete stripe LMV, because when we do this, normally we
5123          * will remove stripes, besides, if directory LMV is corrupt, this will
5124          * prevent deleting its LMV and fixing it (via LFSCK).
5125          */
5126         if (!strcmp(name, XATTR_NAME_LMV))
5127                 RETURN(0);
5128
5129         rc = lod_striping_load(env, lo);
5130         if (rc != 0)
5131                 RETURN(rc);
5132
5133         if (lo->ldo_dir_stripe_count == 0)
5134                 RETURN(0);
5135
5136         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5137                 struct dt_object *dto = lo->ldo_stripe[i];
5138
5139                 if (!dto)
5140                         continue;
5141
5142                 if (!dt_object_exists(dto))
5143                         continue;
5144
5145                 rc = lod_sub_declare_xattr_del(env, dto, name, th);
5146                 if (rc != 0)
5147                         break;
5148         }
5149
5150         RETURN(rc);
5151 }
5152
5153 /**
5154  * Implementation of dt_object_operations::do_xattr_del.
5155  *
5156  * If EA storing a regular striping is being deleted, then release
5157  * all the references to the stripe objects in core.
5158  *
5159  * \see dt_object_operations::do_xattr_del() in the API description for details.
5160  */
5161 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
5162                          const char *name, struct thandle *th)
5163 {
5164         int rc;
5165
5166         ENTRY;
5167
5168         if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
5169                 lod_striping_free(env, lod_dt_obj(dt));
5170
5171         rc = lod_xattr_del_internal(env, dt, name, th);
5172
5173         RETURN(rc);
5174 }
5175
5176 /**
5177  * Implementation of dt_object_operations::do_xattr_list.
5178  *
5179  * \see dt_object_operations::do_xattr_list() in the API description
5180  * for details.
5181  */
5182 static int lod_xattr_list(const struct lu_env *env,
5183                           struct dt_object *dt, const struct lu_buf *buf)
5184 {
5185         return dt_xattr_list(env, dt_object_child(dt), buf);
5186 }
5187
5188 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
5189 {
5190         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
5191 }
5192
5193 /**
5194  * Copy OST list from layout provided by user.
5195  *
5196  * \param[in] lod_comp          layout_component to be filled
5197  * \param[in] v3                LOV EA V3 user data
5198  *
5199  * \retval              0 on success
5200  * \retval              negative if failed
5201  */
5202 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
5203                             struct lov_user_md_v3 *v3)
5204 {
5205         int j;
5206
5207         ENTRY;
5208
5209         if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
5210                 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
5211
5212         if (lod_comp->llc_ostlist.op_array) {
5213                 if (lod_comp->llc_ostlist.op_size >=
5214                     v3->lmm_stripe_count * sizeof(__u32))  {
5215                         lod_comp->llc_ostlist.op_count =
5216                                         v3->lmm_stripe_count;
5217                         goto skip;
5218                 }
5219                 OBD_FREE(lod_comp->llc_ostlist.op_array,
5220                          lod_comp->llc_ostlist.op_size);
5221         }
5222
5223         /* copy ost list from lmm */
5224         lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
5225         lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
5226         OBD_ALLOC(lod_comp->llc_ostlist.op_array,
5227                   lod_comp->llc_ostlist.op_size);
5228         if (!lod_comp->llc_ostlist.op_array)
5229                 RETURN(-ENOMEM);
5230 skip:
5231         for (j = 0; j < v3->lmm_stripe_count; j++) {
5232                 lod_comp->llc_ostlist.op_array[j] =
5233                         v3->lmm_objects[j].l_ost_idx;
5234         }
5235
5236         RETURN(0);
5237 }
5238
5239
5240 /**
5241  * Get default striping.
5242  *
5243  * \param[in] env               execution environment
5244  * \param[in] lo                object
5245  * \param[out] lds              default striping
5246  *
5247  * \retval              0 on success
5248  * \retval              negative if failed
5249  */
5250 static int lod_get_default_lov_striping(const struct lu_env *env,
5251                                         struct lod_object *lo,
5252                                         struct lod_default_striping *lds,
5253                                         struct dt_allocation_hint *dah)
5254 {
5255         struct lod_thread_info *info = lod_env_info(env);
5256         struct lov_user_md_v1 *v1 = NULL;
5257         struct lov_user_md_v3 *v3 = NULL;
5258         struct lov_comp_md_v1 *lcm = NULL;
5259         __u32 magic;
5260         int append_stripe_count = dah != NULL ? dah->dah_append_stripe_count : 0;
5261         const char *append_pool = (dah != NULL &&
5262                                    dah->dah_append_pool != NULL &&
5263                                    dah->dah_append_pool[0] != '\0') ?
5264                                   dah->dah_append_pool : NULL;
5265         __u16 entry_count = 1;
5266         __u16 mirror_count = 0;
5267         bool want_composite = false;
5268         int rc, i, j;
5269
5270         ENTRY;
5271
5272         lds->lds_def_striping_set = 0;
5273
5274         rc = lod_get_lov_ea(env, lo);
5275         if (rc < 0)
5276                 RETURN(rc);
5277
5278         if (rc < (typeof(rc))sizeof(struct lov_user_md))
5279                 RETURN(0);
5280
5281         magic = *(__u32 *)info->lti_ea_store;
5282         if (magic == __swab32(LOV_USER_MAGIC_V1)) {
5283                 lustre_swab_lov_user_md_v1(info->lti_ea_store);
5284         } else if (magic == __swab32(LOV_USER_MAGIC_V3)) {
5285                 lustre_swab_lov_user_md_v3(info->lti_ea_store);
5286         } else if (magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
5287                 v3 = (struct lov_user_md_v3 *)info->lti_ea_store;
5288                 lustre_swab_lov_user_md_v3(v3);
5289                 lustre_swab_lov_user_md_objects(v3->lmm_objects,
5290                                                 v3->lmm_stripe_count);
5291         } else if (magic == __swab32(LOV_USER_MAGIC_COMP_V1) ||
5292                    magic == __swab32(LOV_USER_MAGIC_SEL)) {
5293                 lustre_swab_lov_comp_md_v1(info->lti_ea_store);
5294         }
5295
5296         switch (magic) {
5297         case LOV_MAGIC_V1:
5298         case LOV_MAGIC_V3:
5299         case LOV_USER_MAGIC_SPECIFIC:
5300                 v1 = info->lti_ea_store;
5301                 break;
5302         case LOV_MAGIC_COMP_V1:
5303         case LOV_MAGIC_SEL:
5304                 lcm = info->lti_ea_store;
5305                 entry_count = lcm->lcm_entry_count;
5306                 if (entry_count == 0)
5307                         RETURN(-EINVAL);
5308
5309                 mirror_count = lcm->lcm_mirror_count + 1;
5310                 want_composite = true;
5311                 break;
5312         default:
5313                 RETURN(-ENOTSUPP);
5314         }
5315
5316         if (append_stripe_count != 0 || append_pool != NULL) {
5317                 entry_count = 1;
5318                 mirror_count = 0;
5319                 want_composite = false;
5320         }
5321
5322         /* realloc default comp entries if necessary */
5323         rc = lod_def_striping_comp_resize(lds, entry_count);
5324         if (rc < 0)
5325                 RETURN(rc);
5326
5327         lds->lds_def_comp_cnt = entry_count;
5328         lds->lds_def_striping_is_composite = want_composite;
5329         lds->lds_def_mirror_cnt = mirror_count;
5330
5331         for (i = 0; i < entry_count; i++) {
5332                 struct lod_layout_component *llc = &lds->lds_def_comp_entries[i];
5333                 const char *pool;
5334
5335                 /*
5336                  * reset llc values, llc_stripes is always NULL in the
5337                  * default striping template, llc_pool will be reset
5338                  * later below using lod_set_pool().
5339                  *
5340                  * XXX At this point llc_pool may point to valid (!)
5341                  * kmalloced strings from previous RPCs.
5342                  */
5343                 memset(llc, 0, offsetof(typeof(*llc), llc_pool));
5344
5345                 if (lcm != NULL) {
5346                         v1 = (struct lov_user_md *)((char *)lcm +
5347                                                     lcm->lcm_entries[i].lcme_offset);
5348
5349                         if (want_composite) {
5350                                 llc->llc_extent = lcm->lcm_entries[i].lcme_extent;
5351                                 /* We only inherit certain flags from the layout */
5352                                 llc->llc_flags = lcm->lcm_entries[i].lcme_flags &
5353                                         LCME_TEMPLATE_FLAGS;
5354                         }
5355                 }
5356
5357                 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",
5358                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5359                        v1->lmm_magic,
5360                        v1->lmm_pattern,
5361                        v1->lmm_stripe_count,
5362                        v1->lmm_stripe_size,
5363                        v1->lmm_stripe_offset,
5364                        append_pool ?: "",
5365                        append_stripe_count);
5366
5367                 if (!lov_pattern_supported(v1->lmm_pattern) &&
5368                     !(v1->lmm_pattern & LOV_PATTERN_F_RELEASED)) {
5369                         lod_free_def_comp_entries(lds);
5370                         RETURN(-EINVAL);
5371                 }
5372
5373                 llc->llc_stripe_count = v1->lmm_stripe_count;
5374                 llc->llc_stripe_size = v1->lmm_stripe_size;
5375                 llc->llc_stripe_offset = v1->lmm_stripe_offset;
5376                 llc->llc_pattern = v1->lmm_pattern;
5377
5378                 if (append_stripe_count != 0 || append_pool != NULL)
5379                         llc->llc_pattern = LOV_PATTERN_RAID0;
5380
5381                 if (append_stripe_count != 0)
5382                         llc->llc_stripe_count = append_stripe_count;
5383
5384                 pool = NULL;
5385                 if (append_pool != NULL) {
5386                         pool = append_pool;
5387                 } else if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
5388                         /* XXX: sanity check here */
5389                         v3 = (struct lov_user_md_v3 *)v1;
5390                         if (v3->lmm_pool_name[0] != '\0')
5391                                 pool = v3->lmm_pool_name;
5392                 }
5393
5394                 lod_set_pool(&llc->llc_pool, pool);
5395
5396                 if (append_stripe_count != 0 || append_pool != NULL) {
5397                         /* Ignore specific striping for append. */
5398                 } else if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
5399                         v3 = (struct lov_user_md_v3 *)v1;
5400                         rc = lod_comp_copy_ost_lists(llc, v3);
5401                         if (rc)
5402                                 RETURN(rc);
5403                 } else if (llc->llc_ostlist.op_array &&
5404                            llc->llc_ostlist.op_count) {
5405                         for (j = 0; j < llc->llc_ostlist.op_count; j++)
5406                                 llc->llc_ostlist.op_array[j] = -1;
5407                         llc->llc_ostlist.op_count = 0;
5408                 }
5409         }
5410
5411         lds->lds_def_striping_set = 1;
5412         RETURN(rc);
5413 }
5414
5415 static inline void lod_lum2lds(struct lod_default_striping *lds,
5416                                const struct lmv_user_md *lum)
5417 {
5418         lds->lds_dir_def_stripe_count = le32_to_cpu(lum->lum_stripe_count);
5419         lds->lds_dir_def_stripe_offset = le32_to_cpu(lum->lum_stripe_offset);
5420         lds->lds_dir_def_hash_type = le32_to_cpu(lum->lum_hash_type);
5421         lds->lds_dir_def_max_inherit = lum->lum_max_inherit;
5422         lds->lds_dir_def_max_inherit_rr = lum->lum_max_inherit_rr;
5423         lds->lds_dir_def_striping_set = 1;
5424 }
5425
5426 /**
5427  * Get default directory striping.
5428  *
5429  * \param[in] env               execution environment
5430  * \param[in] lo                object
5431  * \param[out] lds              default striping
5432  *
5433  * \retval              0 on success
5434  * \retval              negative if failed
5435  */
5436 static int lod_get_default_lmv_striping(const struct lu_env *env,
5437                                         struct lod_object *lo,
5438                                         struct lod_default_striping *lds)
5439 {
5440         struct lmv_user_md *lmu;
5441         int rc;
5442
5443         lds->lds_dir_def_striping_set = 0;
5444
5445         rc = lod_get_default_lmv_ea(env, lo);
5446         if (rc < 0)
5447                 return rc;
5448
5449         if (rc >= (int)sizeof(*lmu)) {
5450                 struct lod_thread_info *info = lod_env_info(env);
5451
5452                 lmu = info->lti_ea_store;
5453                 lod_lum2lds(lds, lmu);
5454         }
5455
5456         return 0;
5457 }
5458
5459 /**
5460  * Get default striping in the object.
5461  *
5462  * Get object default striping and default directory striping.
5463  *
5464  * \param[in] env               execution environment
5465  * \param[in] lo                object
5466  * \param[out] lds              default striping
5467  *
5468  * \retval              0 on success
5469  * \retval              negative if failed
5470  */
5471 static int lod_get_default_striping(const struct lu_env *env,
5472                                     struct lod_object *lo,
5473                                     struct dt_allocation_hint *ah,
5474                                     struct lod_default_striping *lds)
5475 {
5476         int rc, rc1;
5477
5478         rc = lod_get_default_lov_striping(env, lo, lds, NULL);
5479         if (lds->lds_def_striping_set) {
5480                 struct lod_thread_info *info = lod_env_info(env);
5481                 struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5482
5483                 rc = lod_verify_striping(env, d, lo, &info->lti_buf, false);
5484                 if (rc)
5485                         lds->lds_def_striping_set = 0;
5486         }
5487
5488         if (ah->dah_eadata_is_dmv) {
5489                 lod_lum2lds(lds, ah->dah_eadata);
5490         } else if (ah->dah_dmv_imp_inherit) {
5491                 lds->lds_dir_def_striping_set = 0;
5492         } else {
5493                 rc1 = lod_get_default_lmv_striping(env, lo, lds);
5494                 if (rc == 0 && rc1 < 0)
5495                         rc = rc1;
5496         }
5497
5498         return rc;
5499 }
5500
5501 /**
5502  * Apply default striping on object.
5503  *
5504  * If object striping pattern is not set, set to the one in default striping.
5505  * The default striping is from parent or fs.
5506  *
5507  * \param[in] lo                new object
5508  * \param[in] lds               default striping
5509  * \param[in] mode              new object's mode
5510  */
5511 static void lod_striping_from_default(struct lod_object *lo,
5512                                       const struct lod_default_striping *lds,
5513                                       umode_t mode)
5514 {
5515         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5516         int i, rc;
5517
5518         if (lds->lds_def_striping_set && S_ISREG(mode)) {
5519                 struct lov_desc *desc = &d->lod_ost_descs.ltd_lov_desc;
5520
5521                 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
5522                                             lds->lds_def_comp_cnt);
5523                 if (rc != 0)
5524                         return;
5525
5526                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
5527                 if (lds->lds_def_mirror_cnt > 1)
5528                         lo->ldo_flr_state = LCM_FL_RDONLY;
5529
5530                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5531                         struct lod_layout_component *obj_comp =
5532                                                 &lo->ldo_comp_entries[i];
5533                         struct lod_layout_component *def_comp =
5534                                                 &lds->lds_def_comp_entries[i];
5535
5536                         CDEBUG(D_LAYOUT,
5537                                "inherit "DFID" file layout from default: flags=%#x size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
5538                                PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5539                                def_comp->llc_flags,
5540                                def_comp->llc_stripe_size,
5541                                def_comp->llc_stripe_count,
5542                                def_comp->llc_stripe_offset,
5543                                def_comp->llc_pattern,
5544                                def_comp->llc_pool ?: "");
5545
5546                         *obj_comp = *def_comp;
5547                         if (def_comp->llc_pool != NULL) {
5548                                 /* pointer was copied from def_comp */
5549                                 obj_comp->llc_pool = NULL;
5550                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
5551                         }
5552
5553                         /* copy ost list */
5554                         if (def_comp->llc_ostlist.op_array &&
5555                             def_comp->llc_ostlist.op_count) {
5556                                 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
5557                                           obj_comp->llc_ostlist.op_size);
5558                                 if (!obj_comp->llc_ostlist.op_array)
5559                                         return;
5560                                 memcpy(obj_comp->llc_ostlist.op_array,
5561                                        def_comp->llc_ostlist.op_array,
5562                                        obj_comp->llc_ostlist.op_size);
5563                         } else if (def_comp->llc_ostlist.op_array) {
5564                                 obj_comp->llc_ostlist.op_array = NULL;
5565                         }
5566
5567                         /*
5568                          * Don't initialize these fields for plain layout
5569                          * (v1/v3) here, they are inherited in the order of
5570                          * 'parent' -> 'fs default (root)' -> 'global default
5571                          * values for stripe_count & stripe_size'.
5572                          *
5573                          * see lod_ah_init().
5574                          */
5575                         if (!lo->ldo_is_composite)
5576                                 continue;
5577
5578                         lod_adjust_stripe_info(obj_comp, desc, 0);
5579                 }
5580         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
5581                 if (lo->ldo_dir_stripe_count == 0)
5582                         lo->ldo_dir_stripe_count =
5583                                 lds->lds_dir_def_stripe_count;
5584                 if (lo->ldo_dir_stripe_offset == -1)
5585                         lo->ldo_dir_stripe_offset =
5586                                 lds->lds_dir_def_stripe_offset;
5587                 if (lo->ldo_dir_hash_type == LMV_HASH_TYPE_UNKNOWN)
5588                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
5589
5590                 CDEBUG(D_LAYOUT,
5591                        "inherit "DFID" dir layout from default: count=%hu offset=%u hash_type=%x\n",
5592                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
5593                        lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
5594                        lo->ldo_dir_hash_type);
5595         }
5596 }
5597
5598 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root,
5599                                          const char *append_pool)
5600 {
5601         struct lod_layout_component *lod_comp;
5602
5603         if (lo->ldo_comp_cnt == 0)
5604                 return true;
5605
5606         if (lo->ldo_is_composite)
5607                 return false;
5608
5609         lod_comp = &lo->ldo_comp_entries[0];
5610
5611         if (lod_comp->llc_stripe_count <= 0 ||
5612             lod_comp->llc_stripe_size <= 0)
5613                 return true;
5614
5615         if (from_root && (lod_comp->llc_pool == NULL ||
5616                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
5617                 return true;
5618
5619         if (append_pool && append_pool[0])
5620                 return true;
5621
5622         return false;
5623 }
5624
5625 /**
5626  * Implementation of dt_object_operations::do_ah_init.
5627  *
5628  * This method is used to make a decision on the striping configuration for the
5629  * object being created. It can be taken from the \a parent object if it exists,
5630  * or filesystem's default. The resulting configuration (number of stripes,
5631  * stripe size/offset, pool name, hash_type, etc.) is stored in the object
5632  * itself and will be used by the methods like ->doo_declare_create().
5633  *
5634  * \see dt_object_operations::do_ah_init() in the API description for details.
5635  */
5636 static void lod_ah_init(const struct lu_env *env,
5637                         struct dt_allocation_hint *ah,
5638                         struct dt_object *parent,
5639                         struct dt_object *child,
5640                         umode_t child_mode)
5641 {
5642         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
5643         struct lod_thread_info *info = lod_env_info(env);
5644         struct lod_default_striping *lds = lod_lds_buf_get(env);
5645         struct dt_object *nextp = NULL;
5646         struct dt_object *nextc;
5647         struct lod_object *lp = NULL;
5648         struct lod_object *lc;
5649         struct lov_desc *desc;
5650         struct lod_layout_component *lod_comp;
5651         int rc;
5652         ENTRY;
5653
5654         LASSERT(child);
5655
5656         if (ah->dah_append_stripe_count == -1)
5657                 ah->dah_append_stripe_count =
5658                         d->lod_ost_descs.ltd_lov_desc.ld_tgt_count;
5659
5660         if (likely(parent)) {
5661                 nextp = dt_object_child(parent);
5662                 lp = lod_dt_obj(parent);
5663         }
5664
5665         nextc = dt_object_child(child);
5666         lc = lod_dt_obj(child);
5667
5668         LASSERT(!lod_obj_is_striped(child));
5669         /* default layout template may have been set on the regular file
5670          * when this is called from mdd_create_data() */
5671         if (S_ISREG(child_mode))
5672                 lod_free_comp_entries(lc);
5673
5674         if (!dt_object_exists(nextc))
5675                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
5676
5677         if (S_ISDIR(child_mode)) {
5678                 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
5679
5680                 /* other default values are 0 */
5681                 lc->ldo_dir_stripe_offset = -1;
5682
5683                 /* no default striping configuration is needed for
5684                  * foreign dirs
5685                  */
5686                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
5687                     le32_to_cpu(lum1->lum_magic) == LMV_MAGIC_FOREIGN) {
5688                         lc->ldo_is_foreign = true;
5689                         /* keep stripe_count 0 and stripe_offset -1 */
5690                         CDEBUG(D_INFO, "no default striping for foreign dir\n");
5691                         RETURN_EXIT;
5692                 }
5693
5694                 if (likely(lp != NULL))
5695                         lod_get_default_striping(env, lp, ah, lds);
5696
5697                 /* It should always honour the specified stripes */
5698                 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
5699                  * will have old magic. In this case, we should ignore the
5700                  * stripe count and try to create dir by default stripe.
5701                  */
5702                 if (ah->dah_eadata && ah->dah_eadata_len &&
5703                     !ah->dah_eadata_is_dmv &&
5704                     (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
5705                      le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
5706                         lc->ldo_dir_stripe_count =
5707                                 le32_to_cpu(lum1->lum_stripe_count);
5708                         lc->ldo_dir_stripe_offset =
5709                                 le32_to_cpu(lum1->lum_stripe_offset);
5710                         lc->ldo_dir_hash_type =
5711                                 le32_to_cpu(lum1->lum_hash_type);
5712                         CDEBUG(D_INFO,
5713                                "set dirstripe: count %hu, offset %d, hash %x\n",
5714                                 lc->ldo_dir_stripe_count,
5715                                 (int)lc->ldo_dir_stripe_offset,
5716                                 lc->ldo_dir_hash_type);
5717
5718                         if (d->lod_mdt_descs.ltd_lmv_desc.ld_active_tgt_count &&
5719                             lc->ldo_dir_stripe_count < 2 &&
5720                             lum1->lum_max_inherit != LMV_INHERIT_NONE) {
5721                                 /* when filesystem-wide default LMV is set, dirs
5722                                  * will be created on MDT by space usage, but if
5723                                  * dir is created with "lfs mkdir -c 1 ...", its
5724                                  * subdirs should be kept on the same MDT. To
5725                                  * guarantee this, set default LMV for such dir.
5726                                  */
5727                                 lds->lds_dir_def_stripe_count =
5728                                         le32_to_cpu(lum1->lum_stripe_count);
5729                                 /* if "-1" stripe offset is set, save current
5730                                  * MDT index in default LMV.
5731                                  */
5732                                 if (le32_to_cpu(lum1->lum_stripe_offset) ==
5733                                     LMV_OFFSET_DEFAULT)
5734                                         lds->lds_dir_def_stripe_offset =
5735                                                 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5736                                 else
5737                                         lds->lds_dir_def_stripe_offset =
5738                                                 le32_to_cpu(lum1->lum_stripe_offset);
5739                                 lds->lds_dir_def_hash_type =
5740                                         le32_to_cpu(lum1->lum_hash_type);
5741                                 lds->lds_dir_def_max_inherit =
5742                                         lum1->lum_max_inherit;
5743                                 /* it will be decreased by 1 later in setting */
5744                                 if (lum1->lum_max_inherit >= LMV_INHERIT_END &&
5745                                     lum1->lum_max_inherit < LMV_INHERIT_MAX)
5746                                         lds->lds_dir_def_max_inherit++;
5747                                 lds->lds_dir_def_max_inherit_rr =
5748                                         lum1->lum_max_inherit_rr;
5749                                 lds->lds_dir_def_striping_set = 1;
5750                                 /* don't inherit LOV from ROOT */
5751                                 if (lds->lds_def_striping_set &&
5752                                     fid_is_root(lod_object_fid(lp)))
5753                                         lds->lds_def_striping_set = 0;
5754                                 lc->ldo_def_striping = lds;
5755                         } else if (lds->lds_def_striping_set &&
5756                                    !fid_is_root(lod_object_fid(lp))) {
5757                                 /* don't inherit default LMV for "lfs mkdir" */
5758                                 lds->lds_dir_def_striping_set = 0;
5759                                 lc->ldo_def_striping = lds;
5760                         }
5761                 } else {
5762                         /* inherit default striping except ROOT */
5763                         if ((lds->lds_def_striping_set ||
5764                              lds->lds_dir_def_striping_set) &&
5765                             !fid_is_root(lod_object_fid(lp)))
5766                                 lc->ldo_def_striping = lds;
5767
5768                         /* transfer defaults LMV to new directory */
5769                         lod_striping_from_default(lc, lds, child_mode);
5770
5771                         /* set count 0 to create normal directory */
5772                         if (lc->ldo_dir_stripe_count == 1)
5773                                 lc->ldo_dir_stripe_count = 0;
5774
5775                         /* do not save default LMV on server */
5776                         if (ah->dah_dmv_imp_inherit) {
5777                                 lds->lds_dir_def_striping_set = 0;
5778                                 if (!lds->lds_def_striping_set)
5779                                         lc->ldo_def_striping = NULL;
5780                         }
5781                 }
5782
5783                 /* shrink the stripe count to max_mdt_stripecount if it is -1
5784                  * and max_mdt_stripecount is not 0
5785                  */
5786                 if (lc->ldo_dir_stripe_count == (__u16)(-1) &&
5787                     d->lod_max_mdt_stripecount)
5788                         lc->ldo_dir_stripe_count = d->lod_max_mdt_stripecount;
5789
5790                 /* shrink the stripe_count to the avaible MDT count */
5791                 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
5792                     !CFS_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
5793                         lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
5794                         if (lc->ldo_dir_stripe_count == 1)
5795                                 lc->ldo_dir_stripe_count = 0;
5796                 }
5797
5798                 if (!lmv_is_known_hash_type(lc->ldo_dir_hash_type))
5799                         lc->ldo_dir_hash_type =
5800                                 (lc->ldo_dir_hash_type & LMV_HASH_FLAG_KNOWN) |
5801                                 d->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
5802
5803                 /* make sure all fscrypt metadata stays on same mdt */
5804                 if (child->do_lu.lo_header->loh_attr & LOHA_FSCRYPT_MD) {
5805                         lc->ldo_dir_stripe_count = 0;
5806                         lds->lds_dir_def_stripe_offset =
5807                                 lod2lu_dev(d)->ld_site->ld_seq_site->ss_node_id;
5808                         lds->lds_dir_def_striping_set = 1;
5809                         lc->ldo_def_striping = lds;
5810                 }
5811
5812                 CDEBUG(D_INFO, "final dir stripe_count=%hu offset=%d hash=%u\n",
5813                        lc->ldo_dir_stripe_count,
5814                        (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
5815
5816                 RETURN_EXIT;
5817         }
5818
5819         /* child object regular file*/
5820
5821         if (!lod_object_will_be_striped(S_ISREG(child_mode),
5822                                         lu_object_fid(&child->do_lu)))
5823                 RETURN_EXIT;
5824
5825         /* If object is going to be striped over OSTs, transfer default
5826          * striping information to the child, so that we can use it
5827          * during declaration and creation.
5828          *
5829          * Try from the parent first.
5830          */
5831         if (likely(lp != NULL)) {
5832                 rc = lod_get_default_lov_striping(env, lp, lds, ah);
5833                 if (rc == 0 && lds->lds_def_striping_set) {
5834                         rc = lod_verify_striping(env, d, lp, &info->lti_buf,
5835                                                  false);
5836                         if (rc == 0)
5837                                 lod_striping_from_default(lc, lds, child_mode);
5838                 }
5839         }
5840
5841         /* Initialize lod_device::lod_md_root object reference */
5842         if (d->lod_md_root == NULL) {
5843                 struct dt_object *root;
5844                 struct lod_object *lroot;
5845
5846                 lu_root_fid(&info->lti_fid);
5847                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
5848                 if (!IS_ERR(root)) {
5849                         lroot = lod_dt_obj(root);
5850
5851                         spin_lock(&d->lod_lock);
5852                         if (d->lod_md_root != NULL)
5853                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
5854                         d->lod_md_root = lroot;
5855                         spin_unlock(&d->lod_lock);
5856                 }
5857         }
5858
5859         /* try inherit layout from the root object (fs default) when:
5860          *  - parent does not have default layout; or
5861          *  - parent has plain(v1/v3) default layout, and some attributes
5862          *    are not specified in the default layout;
5863          */
5864         if (d->lod_md_root != NULL &&
5865             lod_need_inherit_more(lc, true, ah->dah_append_pool)) {
5866                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds,
5867                                                   ah);
5868                 if (rc || !lds->lds_def_striping_set)
5869                         goto out;
5870
5871                 rc = lod_verify_striping(env, d, d->lod_md_root, &info->lti_buf,
5872                                          false);
5873                 if (rc)
5874                         goto out;
5875
5876                 if (lc->ldo_comp_cnt == 0) {
5877                         lod_striping_from_default(lc, lds, child_mode);
5878                 } else if (!lds->lds_def_striping_is_composite) {
5879                         struct lod_layout_component *def_comp;
5880
5881                         LASSERT(!lc->ldo_is_composite);
5882                         lod_comp = &lc->ldo_comp_entries[0];
5883                         def_comp = &lds->lds_def_comp_entries[0];
5884
5885                         if (lod_comp->llc_stripe_count <= 0)
5886                                 lod_comp->llc_stripe_count =
5887                                         def_comp->llc_stripe_count;
5888                         if (lod_comp->llc_stripe_size <= 0)
5889                                 lod_comp->llc_stripe_size =
5890                                         def_comp->llc_stripe_size;
5891                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
5892                             (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
5893                                 lod_comp->llc_stripe_offset =
5894                                         def_comp->llc_stripe_offset;
5895                         if (lod_comp->llc_pool == NULL)
5896                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
5897                 }
5898         }
5899 out:
5900         /*
5901          * fs default striping may not be explicitly set, or historically set
5902          * in config log, use them.
5903          */
5904         if (lod_need_inherit_more(lc, false, ah->dah_append_pool)) {
5905                 if (lc->ldo_comp_cnt == 0) {
5906                         rc = lod_alloc_comp_entries(lc, 0, 1);
5907                         if (rc)
5908                                 /* fail to allocate memory, will create a
5909                                  * non-striped file. */
5910                                 RETURN_EXIT;
5911                         lc->ldo_is_composite = 0;
5912                         lod_comp = &lc->ldo_comp_entries[0];
5913                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
5914                 }
5915                 LASSERT(!lc->ldo_is_composite);
5916                 lod_comp = &lc->ldo_comp_entries[0];
5917                 desc = &d->lod_ost_descs.ltd_lov_desc;
5918                 lod_adjust_stripe_info(lod_comp, desc,
5919                                        ah->dah_append_stripe_count);
5920                 if (ah->dah_append_pool && ah->dah_append_pool[0])
5921                         lod_obj_set_pool(lc, 0, ah->dah_append_pool);
5922         }
5923
5924         EXIT;
5925 }
5926
5927 /**
5928  * Size initialization on late striping.
5929  *
5930  * Propagate the size of a truncated object to a deferred striping.
5931  * This function handles a special case when truncate was done on a
5932  * non-striped object and now while the striping is being created
5933  * we can't lose that size, so we have to propagate it to the stripes
5934  * being created.
5935  *
5936  * \param[in] env       execution environment
5937  * \param[in] dt        object
5938  * \param[in] th        transaction handle
5939  *
5940  * \retval              0 on success
5941  * \retval              negative if failed
5942  */
5943 static int lod_declare_init_size(const struct lu_env *env,
5944                                  struct dt_object *dt, struct thandle *th)
5945 {
5946         struct dt_object        *next = dt_object_child(dt);
5947         struct lod_object       *lo = lod_dt_obj(dt);
5948         struct dt_object        **objects = NULL;
5949         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
5950         uint64_t        size, offs;
5951         int     i, rc, stripe, stripe_count = 0, stripe_size = 0;
5952         struct lu_extent size_ext;
5953         ENTRY;
5954
5955         if (!lod_obj_is_striped(dt))
5956                 RETURN(0);
5957
5958         rc = dt_attr_get(env, next, attr);
5959         LASSERT(attr->la_valid & LA_SIZE);
5960         if (rc)
5961                 RETURN(rc);
5962
5963         size = attr->la_size;
5964         if (size == 0)
5965                 RETURN(0);
5966
5967         size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
5968         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5969                 struct lod_layout_component *lod_comp;
5970                 struct lu_extent *extent;
5971
5972                 lod_comp = &lo->ldo_comp_entries[i];
5973
5974                 if (lod_comp->llc_stripe == NULL)
5975                         continue;
5976
5977                 extent = &lod_comp->llc_extent;
5978                 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
5979                 if (!lo->ldo_is_composite ||
5980                     lu_extent_is_overlapped(extent, &size_ext)) {
5981                         objects = lod_comp->llc_stripe;
5982                         stripe_count = lod_comp->llc_stripe_count;
5983                         stripe_size = lod_comp->llc_stripe_size;
5984
5985                         /* next mirror */
5986                         if (stripe_count == 0)
5987                                 continue;
5988
5989                         LASSERT(objects != NULL && stripe_size != 0);
5990                         do_div(size, stripe_size);
5991                         stripe = do_div(size, stripe_count);
5992                         LASSERT(objects[stripe] != NULL);
5993
5994                         size = size * stripe_size;
5995                         offs = attr->la_size;
5996                         size += do_div(offs, stripe_size);
5997
5998                         attr->la_valid = LA_SIZE;
5999                         attr->la_size = size;
6000
6001                         rc = lod_sub_declare_attr_set(env, objects[stripe],
6002                                                       attr, th);
6003                 }
6004         }
6005
6006         RETURN(rc);
6007 }
6008
6009 /**
6010  * Declare creation of striped object.
6011  *
6012  * The function declares creation stripes for a regular object. The function
6013  * also declares whether the stripes will be created with non-zero size if
6014  * previously size was set non-zero on the master object. If object \a dt is
6015  * not local, then only fully defined striping can be applied in \a lovea.
6016  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
6017  * for the details.
6018  *
6019  * \param[in] env       execution environment
6020  * \param[in] dt        object
6021  * \param[in] attr      attributes the stripes will be created with
6022  * \param[in] lovea     a buffer containing striping description
6023  * \param[in] th        transaction handle
6024  *
6025  * \retval              0 on success
6026  * \retval              negative if failed
6027  */
6028 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
6029                                struct lu_attr *attr,
6030                                const struct lu_buf *lovea, struct thandle *th)
6031 {
6032         struct lod_thread_info  *info = lod_env_info(env);
6033         struct dt_object        *next = dt_object_child(dt);
6034         struct lod_object       *lo = lod_dt_obj(dt);
6035         int                      rc;
6036         ENTRY;
6037
6038         if (CFS_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
6039                 GOTO(out, rc = -ENOMEM);
6040
6041         if (!dt_object_remote(next)) {
6042                 /* choose OST and generate appropriate objects */
6043                 rc = lod_prepare_create(env, lo, attr, lovea, th);
6044                 if (rc)
6045                         GOTO(out, rc);
6046
6047                 /*
6048                  * declare storage for striping data
6049                  */
6050                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6051         } else {
6052                 /* LOD can not choose OST objects for remote objects, i.e.
6053                  * stripes must be ready before that. Right now, it can only
6054                  * happen during migrate, i.e. migrate process needs to create
6055                  * remote regular file (mdd_migrate_create), then the migrate
6056                  * process will provide stripeEA. */
6057                 LASSERT(lovea != NULL);
6058                 info->lti_buf = *lovea;
6059         }
6060
6061         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
6062                                        XATTR_NAME_LOV, 0, th);
6063         if (rc)
6064                 GOTO(out, rc);
6065
6066         /*
6067          * if striping is created with local object's size > 0,
6068          * we have to propagate this size to specific object
6069          * the case is possible only when local object was created previously
6070          */
6071         if (dt_object_exists(next))
6072                 rc = lod_declare_init_size(env, dt, th);
6073
6074 out:
6075         /* failed to create striping or to set initial size, let's reset
6076          * config so that others don't get confused */
6077         if (rc)
6078                 lod_striping_free(env, lo);
6079
6080         RETURN(rc);
6081 }
6082
6083 /*
6084  * Whether subdirectories under \a dt should be created on MDTs by space QoS
6085  *
6086  * If LMV_HASH_FLAG_SPACE is set on directory default layout, its subdirectories
6087  * should be created on MDT by space QoS.
6088  *
6089  * \param[in] env       execution environment
6090  * \param[in] dev       lu device
6091  * \param[in] dt        object
6092  *
6093  * \retval              1 if directory should create subdir by space usage
6094  * \retval              0 if not
6095  * \retval              -ev if failed
6096  */
6097 static inline int dt_object_qos_mkdir(const struct lu_env *env,
6098                                       struct lu_device *dev,
6099                                       struct dt_object *dt)
6100 {
6101         struct lod_thread_info *info = lod_env_info(env);
6102         struct lu_object *obj;
6103         struct lod_object *lo;
6104         struct lmv_user_md *lmu;
6105         int rc;
6106
6107         obj = lu_object_find_slice(env, dev, lu_object_fid(&dt->do_lu), NULL);
6108         if (IS_ERR(obj))
6109                 return PTR_ERR(obj);
6110
6111         lo = lu2lod_obj(obj);
6112
6113         rc = lod_get_default_lmv_ea(env, lo);
6114         dt_object_put(env, dt);
6115         if (rc <= 0)
6116                 return rc;
6117
6118         if (rc < (int)sizeof(*lmu))
6119                 return -EINVAL;
6120
6121         lmu = info->lti_ea_store;
6122         return le32_to_cpu(lmu->lum_stripe_offset) == LMV_OFFSET_DEFAULT;
6123 }
6124
6125 /**
6126  * Implementation of dt_object_operations::do_declare_create.
6127  *
6128  * The method declares creation of a new object. If the object will be striped,
6129  * then helper functions are called to find FIDs for the stripes, declare
6130  * creation of the stripes and declare initialization of the striping
6131  * information to be stored in the master object.
6132  *
6133  * \see dt_object_operations::do_declare_create() in the API description
6134  * for details.
6135  */
6136 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
6137                               struct lu_attr *attr,
6138                               struct dt_allocation_hint *hint,
6139                               struct dt_object_format *dof, struct thandle *th)
6140 {
6141         struct dt_object   *next = dt_object_child(dt);
6142         struct lod_object  *lo = lod_dt_obj(dt);
6143         int                 rc;
6144         ENTRY;
6145
6146         LASSERT(dof);
6147         LASSERT(attr);
6148         LASSERT(th);
6149
6150         /*
6151          * first of all, we declare creation of local object
6152          */
6153         rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
6154         if (rc != 0)
6155                 GOTO(out, rc);
6156
6157         /*
6158          * it's lod_ah_init() that has decided the object will be striped
6159          */
6160         if (dof->dof_type == DFT_REGULAR) {
6161                 /* callers don't want stripes */
6162                 /* XXX: all tricky interactions with ->ah_make_hint() decided
6163                  * to use striping, then ->declare_create() behaving differently
6164                  * should be cleaned */
6165                 if (dof->u.dof_reg.striped != 0)
6166                         rc = lod_declare_striped_create(env, dt, attr,
6167                                                         NULL, th);
6168         } else if (dof->dof_type == DFT_DIR) {
6169                 struct seq_server_site *ss;
6170                 struct lu_buf buf = { NULL };
6171                 struct lu_buf *lmu = NULL;
6172
6173                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
6174
6175                 /* If the parent has default stripeEA, and client
6176                  * did not find it before sending create request,
6177                  * then MDT will return -EREMOTE, and client will
6178                  * retrieve the default stripeEA and re-create the
6179                  * sub directory.
6180                  *
6181                  * Note: if dah_eadata != NULL, it means creating the
6182                  * striped directory with specified stripeEA, then it
6183                  * should ignore the default stripeEA */
6184                 if (hint != NULL && hint->dah_eadata == NULL) {
6185                         if (CFS_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
6186                                 GOTO(out, rc = -EREMOTE);
6187
6188                         if (lo->ldo_dir_stripe_offset != LMV_OFFSET_DEFAULT &&
6189                             lo->ldo_dir_stripe_offset != ss->ss_node_id) {
6190                                 struct lod_device *lod;
6191                                 struct lu_tgt_desc *mdt = NULL;
6192                                 bool found_mdt = false;
6193
6194                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6195                                 lod_foreach_mdt(lod, mdt) {
6196                                         if (mdt->ltd_index ==
6197                                                 lo->ldo_dir_stripe_offset) {
6198                                                 found_mdt = true;
6199                                                 break;
6200                                         }
6201                                 }
6202
6203                                 /* If the MDT indicated by stripe_offset can be
6204                                  * found, then tell client to resend the create
6205                                  * request to the correct MDT, otherwise return
6206                                  * error to client */
6207                                 if (found_mdt)
6208                                         GOTO(out, rc = -EREMOTE);
6209                                 else
6210                                         GOTO(out, rc = -EINVAL);
6211                         }
6212                 } else if (hint && hint->dah_eadata) {
6213                         lmu = &buf;
6214                         lmu->lb_buf = (void *)hint->dah_eadata;
6215                         lmu->lb_len = hint->dah_eadata_len;
6216                 }
6217
6218                 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
6219                                                      th);
6220         }
6221 out:
6222         /* failed to create striping or to set initial size, let's reset
6223          * config so that others don't get confused */
6224         if (rc)
6225                 lod_striping_free(env, lo);
6226         RETURN(rc);
6227 }
6228
6229 /**
6230  * Generate component ID for new created component.
6231  *
6232  * \param[in] lo                LOD object
6233  * \param[in] comp_idx          index of ldo_comp_entries
6234  *
6235  * \retval                      component ID on success
6236  * \retval                      LCME_ID_INVAL on failure
6237  */
6238 static __u32 lod_gen_component_id(struct lod_object *lo,
6239                                   int mirror_id, int comp_idx)
6240 {
6241         struct lod_layout_component *lod_comp;
6242         __u32   id, start, end;
6243         int     i;
6244
6245         LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
6246
6247         lod_obj_inc_layout_gen(lo);
6248         id = lo->ldo_layout_gen;
6249         if (likely(id <= SEQ_ID_MAX))
6250                 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
6251
6252         /* Layout generation wraps, need to check collisions. */
6253         start = id & SEQ_ID_MASK;
6254         end = SEQ_ID_MAX;
6255 again:
6256         for (id = start; id <= end; id++) {
6257                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6258                         lod_comp = &lo->ldo_comp_entries[i];
6259                         if (pflr_id(mirror_id, id) == lod_comp->llc_id)
6260                                 break;
6261                 }
6262                 /* Found the ununsed ID */
6263                 if (i == lo->ldo_comp_cnt)
6264                         RETURN(pflr_id(mirror_id, id));
6265         }
6266         if (end == LCME_ID_MAX) {
6267                 start = 1;
6268                 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
6269                           (__u32)(LCME_ID_MAX - 1));
6270                 goto again;
6271         }
6272
6273         RETURN(LCME_ID_INVAL);
6274 }
6275
6276 /**
6277  * Creation of a striped regular object.
6278  *
6279  * The function is called to create the stripe objects for a regular
6280  * striped file. This can happen at the initial object creation or
6281  * when the caller asks LOD to do so using ->do_xattr_set() method
6282  * (so called late striping). Notice all the information are already
6283  * prepared in the form of the list of objects (ldo_stripe field).
6284  * This is done during declare phase.
6285  *
6286  * \param[in] env       execution environment
6287  * \param[in] dt        object
6288  * \param[in] attr      attributes the stripes will be created with
6289  * \param[in] dof       format of stripes (see OSD API description)
6290  * \param[in] th        transaction handle
6291  *
6292  * \retval              0 on success
6293  * \retval              negative if failed
6294  */
6295 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
6296                        struct lu_attr *attr, struct dt_object_format *dof,
6297                        struct thandle *th)
6298 {
6299         struct lod_layout_component     *lod_comp;
6300         struct lod_object       *lo = lod_dt_obj(dt);
6301         __u16   mirror_id;
6302         int     rc = 0, i, j;
6303         ENTRY;
6304
6305         mutex_lock(&lo->ldo_layout_mutex);
6306
6307         LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
6308                 lo->ldo_is_foreign);
6309
6310         mirror_id = 0; /* non-flr file's mirror_id is 0 */
6311         if (lo->ldo_mirror_count > 1) {
6312                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6313                         lod_comp = &lo->ldo_comp_entries[i];
6314                         if (lod_comp->llc_id != LCME_ID_INVAL &&
6315                             mirror_id_of(lod_comp->llc_id) > mirror_id)
6316                                 mirror_id = mirror_id_of(lod_comp->llc_id);
6317                 }
6318         }
6319
6320         /* create all underlying objects */
6321         for (i = 0; i < lo->ldo_comp_cnt; i++) {
6322                 lod_comp = &lo->ldo_comp_entries[i];
6323
6324                 if (lod_comp->llc_id == LCME_ID_INVAL) {
6325                         /* only the component of FLR layout with more than 1
6326                          * mirror has mirror ID in its component ID.
6327                          */
6328                         if (lod_comp->llc_extent.e_start == 0 &&
6329                             lo->ldo_mirror_count > 1)
6330                                 ++mirror_id;
6331
6332                         lod_comp->llc_id = lod_gen_component_id(lo,
6333                                                                 mirror_id, i);
6334                         if (lod_comp->llc_id == LCME_ID_INVAL)
6335                                 GOTO(out, rc = -ERANGE);
6336                 }
6337
6338                 if (lod_comp_inited(lod_comp))
6339                         continue;
6340
6341                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
6342                         lod_comp_set_init(lod_comp);
6343
6344                 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
6345                         lod_comp_set_init(lod_comp);
6346
6347                 if (lod_comp->llc_stripe == NULL)
6348                         continue;
6349
6350                 LASSERT(lod_comp->llc_stripe_count);
6351                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6352                         struct dt_object *object = lod_comp->llc_stripe[j];
6353                         LASSERT(object != NULL);
6354                         rc = lod_sub_create(env, object, attr, NULL, dof, th);
6355                         if (rc)
6356                                 GOTO(out, rc);
6357                 }
6358                 lod_comp_set_init(lod_comp);
6359         }
6360
6361         rc = lod_fill_mirrors(lo);
6362         if (rc)
6363                 GOTO(out, rc);
6364
6365         lo->ldo_comp_cached = 1;
6366
6367         rc = lod_generate_and_set_lovea(env, lo, th);
6368         if (rc)
6369                 GOTO(out, rc);
6370
6371         mutex_unlock(&lo->ldo_layout_mutex);
6372
6373         RETURN(0);
6374
6375 out:
6376         lod_striping_free_nolock(env, lo);
6377         mutex_unlock(&lo->ldo_layout_mutex);
6378
6379         RETURN(rc);
6380 }
6381
6382 static inline bool lod_obj_is_dom(struct dt_object *dt)
6383 {
6384         struct lod_object *lo = lod_dt_obj(dt);
6385
6386         if (!dt_object_exists(dt_object_child(dt)))
6387                 return false;
6388
6389         if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
6390                 return false;
6391
6392         if (!lo->ldo_comp_cnt)
6393                 return false;
6394
6395         return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
6396                 LOV_PATTERN_MDT);
6397 }
6398
6399 /**
6400  * Implementation of dt_object_operations::do_create.
6401  *
6402  * If any of preceeding methods (like ->do_declare_create(),
6403  * ->do_ah_init(), etc) chose to create a striped object,
6404  * then this method will create the master and the stripes.
6405  *
6406  * \see dt_object_operations::do_create() in the API description for details.
6407  */
6408 static int lod_create(const struct lu_env *env, struct dt_object *dt,
6409                       struct lu_attr *attr, struct dt_allocation_hint *hint,
6410                       struct dt_object_format *dof, struct thandle *th)
6411 {
6412         int                 rc;
6413         ENTRY;
6414
6415         /* create local object */
6416         rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
6417         if (rc != 0)
6418                 RETURN(rc);
6419
6420         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
6421             (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
6422             dof->u.dof_reg.striped != 0) {
6423                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
6424                 rc = lod_striped_create(env, dt, attr, dof, th);
6425         }
6426
6427         RETURN(rc);
6428 }
6429
6430 static inline int
6431 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
6432                           struct dt_object *dt, struct thandle *th,
6433                           int comp_idx, int stripe_idx,
6434                           struct lod_obj_stripe_cb_data *data)
6435 {
6436         if (data->locd_declare)
6437                 return lod_sub_declare_destroy(env, dt, th);
6438
6439         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6440             stripe_idx == cfs_fail_val)
6441                 return lod_sub_destroy(env, dt, th);
6442
6443         return 0;
6444 }
6445
6446 /**
6447  * Implementation of dt_object_operations::do_declare_destroy.
6448  *
6449  * If the object is a striped directory, then the function declares reference
6450  * removal from the master object (this is an index) to the stripes and declares
6451  * destroy of all the stripes. In all the cases, it declares an intention to
6452  * destroy the object itself.
6453  *
6454  * \see dt_object_operations::do_declare_destroy() in the API description
6455  * for details.
6456  */
6457 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
6458                                struct thandle *th)
6459 {
6460         struct dt_object *next = dt_object_child(dt);
6461         struct lod_object *lo = lod_dt_obj(dt);
6462         struct lod_thread_info *info = lod_env_info(env);
6463         struct dt_object *stripe;
6464         char *stripe_name = info->lti_key;
6465         int rc, i;
6466
6467         ENTRY;
6468
6469         /*
6470          * load striping information, notice we don't do this when object
6471          * is being initialized as we don't need this information till
6472          * few specific cases like destroy, chown
6473          */
6474         rc = lod_striping_load(env, lo);
6475         if (rc)
6476                 RETURN(rc);
6477
6478         /* declare destroy for all underlying objects */
6479         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6480                 rc = next->do_ops->do_index_try(env, next,
6481                                                 &dt_directory_features);
6482                 if (rc != 0)
6483                         RETURN(rc);
6484
6485                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6486                         stripe = lo->ldo_stripe[i];
6487                         if (!stripe)
6488                                 continue;
6489
6490                         rc = lod_sub_declare_ref_del(env, next, th);
6491                         if (rc != 0)
6492                                 RETURN(rc);
6493
6494                         snprintf(stripe_name, sizeof(info->lti_key),
6495                                  DFID":%d",
6496                                  PFID(lu_object_fid(&stripe->do_lu)), i);
6497                         rc = lod_sub_declare_delete(env, next,
6498                                         (const struct dt_key *)stripe_name, th);
6499                         if (rc != 0)
6500                                 RETURN(rc);
6501                 }
6502         }
6503
6504         /*
6505          * we declare destroy for the local object
6506          */
6507         rc = lod_sub_declare_destroy(env, next, th);
6508         if (rc)
6509                 RETURN(rc);
6510
6511         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6512             CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6513                 RETURN(0);
6514
6515         if (!lod_obj_is_striped(dt))
6516                 RETURN(0);
6517
6518         /* declare destroy all striped objects */
6519         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6520                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6521                         stripe = lo->ldo_stripe[i];
6522                         if (!stripe)
6523                                 continue;
6524
6525                         if (!dt_object_exists(stripe))
6526                                 continue;
6527
6528                         rc = lod_sub_declare_ref_del(env, stripe, th);
6529                         if (rc != 0)
6530                                 break;
6531
6532                         rc = lod_sub_declare_destroy(env, stripe, th);
6533                         if (rc != 0)
6534                                 break;
6535                 }
6536         } else {
6537                 struct lod_obj_stripe_cb_data data = { { 0 } };
6538
6539                 data.locd_declare = true;
6540                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6541                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6542         }
6543
6544         RETURN(rc);
6545 }
6546
6547 /**
6548  * Implementation of dt_object_operations::do_destroy.
6549  *
6550  * If the object is a striped directory, then the function removes references
6551  * from the master object (this is an index) to the stripes and destroys all
6552  * the stripes. In all the cases, the function destroys the object itself.
6553  *
6554  * \see dt_object_operations::do_destroy() in the API description for details.
6555  */
6556 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
6557                        struct thandle *th)
6558 {
6559         struct dt_object  *next = dt_object_child(dt);
6560         struct lod_object *lo = lod_dt_obj(dt);
6561         struct lod_thread_info *info = lod_env_info(env);
6562         char *stripe_name = info->lti_key;
6563         struct dt_object *stripe;
6564         unsigned int i;
6565         int rc;
6566
6567         ENTRY;
6568
6569         /* destroy sub-stripe of master object */
6570         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6571                 rc = next->do_ops->do_index_try(env, next,
6572                                                 &dt_directory_features);
6573                 if (rc != 0)
6574                         RETURN(rc);
6575
6576                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6577                         stripe = lo->ldo_stripe[i];
6578                         if (!stripe)
6579                                 continue;
6580
6581                         rc = lod_sub_ref_del(env, next, th);
6582                         if (rc != 0)
6583                                 RETURN(rc);
6584
6585                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
6586                                 PFID(lu_object_fid(&stripe->do_lu)), i);
6587
6588                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
6589                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
6590                                PFID(lu_object_fid(&stripe->do_lu)));
6591
6592                         rc = lod_sub_delete(env, next,
6593                                        (const struct dt_key *)stripe_name, th);
6594                         if (rc != 0)
6595                                 RETURN(rc);
6596                 }
6597         }
6598
6599         rc = lod_sub_destroy(env, next, th);
6600         if (rc != 0)
6601                 RETURN(rc);
6602
6603         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
6604             CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
6605                 RETURN(0);
6606
6607         if (!lod_obj_is_striped(dt))
6608                 RETURN(0);
6609
6610         /* destroy all striped objects */
6611         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
6612                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6613                         stripe = lo->ldo_stripe[i];
6614                         if (!stripe)
6615                                 continue;
6616
6617                         if (!dt_object_exists(stripe))
6618                                 continue;
6619
6620                         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
6621                             i == cfs_fail_val) {
6622                                 dt_write_lock(env, stripe, DT_TGT_CHILD);
6623                                 rc = lod_sub_ref_del(env, stripe, th);
6624                                 dt_write_unlock(env, stripe);
6625                                 if (rc != 0)
6626                                         break;
6627
6628                                 rc = lod_sub_destroy(env, stripe, th);
6629                                 if (rc != 0)
6630                                         break;
6631                         }
6632                 }
6633         } else {
6634                 struct lod_obj_stripe_cb_data data = { { 0 } };
6635
6636                 data.locd_declare = false;
6637                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
6638                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
6639         }
6640
6641         RETURN(rc);
6642 }
6643
6644 /**
6645  * Implementation of dt_object_operations::do_declare_ref_add.
6646  *
6647  * \see dt_object_operations::do_declare_ref_add() in the API description
6648  * for details.
6649  */
6650 static int lod_declare_ref_add(const struct lu_env *env,
6651                                struct dt_object *dt, struct thandle *th)
6652 {
6653         return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
6654 }
6655
6656 /**
6657  * Implementation of dt_object_operations::do_ref_add.
6658  *
6659  * \see dt_object_operations::do_ref_add() in the API description for details.
6660  */
6661 static int lod_ref_add(const struct lu_env *env,
6662                        struct dt_object *dt, struct thandle *th)
6663 {
6664         return lod_sub_ref_add(env, dt_object_child(dt), th);
6665 }
6666
6667 /**
6668  * Implementation of dt_object_operations::do_declare_ref_del.
6669  *
6670  * \see dt_object_operations::do_declare_ref_del() in the API description
6671  * for details.
6672  */
6673 static int lod_declare_ref_del(const struct lu_env *env,
6674                                struct dt_object *dt, struct thandle *th)
6675 {
6676         return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
6677 }
6678
6679 /**
6680  * Implementation of dt_object_operations::do_ref_del
6681  *
6682  * \see dt_object_operations::do_ref_del() in the API description for details.
6683  */
6684 static int lod_ref_del(const struct lu_env *env,
6685                        struct dt_object *dt, struct thandle *th)
6686 {
6687         return lod_sub_ref_del(env, dt_object_child(dt), th);
6688 }
6689
6690 /**
6691  * Implementation of dt_object_operations::do_object_sync.
6692  *
6693  * \see dt_object_operations::do_object_sync() in the API description
6694  * for details.
6695  */
6696 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
6697                            __u64 start, __u64 end)
6698 {
6699         return dt_object_sync(env, dt_object_child(dt), start, end);
6700 }
6701
6702 /**
6703  * Implementation of dt_object_operations::do_object_unlock.
6704  *
6705  * Used to release LDLM lock(s).
6706  *
6707  * \see dt_object_operations::do_object_unlock() in the API description
6708  * for details.
6709  */
6710 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
6711                              struct ldlm_enqueue_info *einfo,
6712                              union ldlm_policy_data *policy)
6713 {
6714         struct lod_object *lo = lod_dt_obj(dt);
6715         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
6716         int slave_locks_size;
6717         int i;
6718         ENTRY;
6719
6720         if (slave_locks == NULL)
6721                 RETURN(0);
6722
6723         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
6724         /* Note: for remote lock for single stripe dir, MDT will cancel
6725          * the lock by lockh directly */
6726         LASSERT(!dt_object_remote(dt_object_child(dt)));
6727
6728         /* locks were unlocked in MDT layer */
6729         for (i = 0; i < slave_locks->ha_count; i++)
6730                 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
6731
6732         /*
6733          * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
6734          * layout may change, e.g., shrink dir layout after migration.
6735          */
6736         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6737                 if (lo->ldo_stripe[i])
6738                         dt_invalidate(env, lo->ldo_stripe[i]);
6739         }
6740
6741         slave_locks_size = offsetof(typeof(*slave_locks),
6742                                     ha_handles[slave_locks->ha_count]);
6743         OBD_FREE(slave_locks, slave_locks_size);
6744         einfo->ei_cbdata = NULL;
6745
6746         RETURN(0);
6747 }
6748
6749 /**
6750  * Implementation of dt_object_operations::do_object_lock.
6751  *
6752  * Used to get LDLM lock on the non-striped and striped objects.
6753  *
6754  * \see dt_object_operations::do_object_lock() in the API description
6755  * for details.
6756  */
6757 static int lod_object_lock(const struct lu_env *env,
6758                            struct dt_object *dt,
6759                            struct lustre_handle *lh,
6760                            struct ldlm_enqueue_info *einfo,
6761                            union ldlm_policy_data *policy)
6762 {
6763         struct lod_object *lo = lod_dt_obj(dt);
6764         int slave_locks_size;
6765         struct lustre_handle_array *slave_locks = NULL;
6766         int i;
6767         int rc;
6768         ENTRY;
6769
6770         /* remote object lock */
6771         if (!einfo->ei_enq_slave) {
6772                 LASSERT(dt_object_remote(dt));
6773                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
6774                                       policy);
6775         }
6776
6777         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
6778                 RETURN(-ENOTDIR);
6779
6780         rc = lod_striping_load(env, lo);
6781         if (rc != 0)
6782                 RETURN(rc);
6783
6784         /* No stripes */
6785         if (lo->ldo_dir_stripe_count <= 1)
6786                 RETURN(0);
6787
6788         slave_locks_size = offsetof(typeof(*slave_locks),
6789                                     ha_handles[lo->ldo_dir_stripe_count]);
6790         /* Freed in lod_object_unlock */
6791         OBD_ALLOC(slave_locks, slave_locks_size);
6792         if (!slave_locks)
6793                 RETURN(-ENOMEM);
6794         slave_locks->ha_count = lo->ldo_dir_stripe_count;
6795
6796         /* striped directory lock */
6797         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6798                 struct lustre_handle lockh;
6799                 struct ldlm_res_id *res_id;
6800                 struct dt_object *stripe;
6801
6802                 stripe = lo->ldo_stripe[i];
6803                 if (!stripe)
6804                         continue;
6805
6806                 res_id = &lod_env_info(env)->lti_res_id;
6807                 fid_build_reg_res_name(lu_object_fid(&stripe->do_lu), res_id);
6808                 einfo->ei_res_id = res_id;
6809
6810                 if (dt_object_remote(stripe)) {
6811                         set_bit(i, (void *)slave_locks->ha_map);
6812                         rc = dt_object_lock(env, stripe, &lockh, einfo, policy);
6813                 } else {
6814                         struct ldlm_namespace *ns = einfo->ei_namespace;
6815                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
6816                         ldlm_completion_callback completion = einfo->ei_cb_cp;
6817                         __u64 dlmflags = LDLM_FL_ATOMIC_CB;
6818
6819                         if (einfo->ei_mode == LCK_PW ||
6820                             einfo->ei_mode == LCK_EX)
6821                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
6822
6823                         LASSERT(ns != NULL);
6824                         rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
6825                                                     policy, einfo->ei_mode,
6826                                                     &dlmflags, blocking,
6827                                                     completion, NULL,
6828                                                     NULL, 0, LVB_T_NONE,
6829                                                     NULL, &lockh);
6830                 }
6831                 if (rc) {
6832                         while (i--)
6833                                 ldlm_lock_decref_and_cancel(
6834                                                 &slave_locks->ha_handles[i],
6835                                                 einfo->ei_mode);
6836                         OBD_FREE(slave_locks, slave_locks_size);
6837                         RETURN(rc);
6838                 }
6839                 slave_locks->ha_handles[i] = lockh;
6840         }
6841         einfo->ei_cbdata = slave_locks;
6842
6843         RETURN(0);
6844 }
6845
6846 /**
6847  * Implementation of dt_object_operations::do_invalidate.
6848  *
6849  * \see dt_object_operations::do_invalidate() in the API description for details
6850  */
6851 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
6852 {
6853         return dt_invalidate(env, dt_object_child(dt));
6854 }
6855
6856 static int lod_declare_instantiate_components(const struct lu_env *env,
6857                                               struct lod_object *lo,
6858                                               struct thandle *th,
6859                                               __u64 reserve)
6860 {
6861         struct lod_thread_info *info = lod_env_info(env);
6862         int i;
6863         int rc = 0;
6864         ENTRY;
6865
6866         LASSERT(info->lti_count < lo->ldo_comp_cnt);
6867
6868         for (i = 0; i < info->lti_count; i++) {
6869                 rc = lod_qos_prep_create(env, lo, NULL, th,
6870                                          info->lti_comp_idx[i], reserve);
6871                 if (rc)
6872                         break;
6873         }
6874
6875         if (!rc) {
6876                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6877                 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6878                                 &info->lti_buf, XATTR_NAME_LOV, 0, th);
6879         }
6880
6881         RETURN(rc);
6882 }
6883
6884 /**
6885  * Check OSTs for an existing component for further extension
6886  *
6887  * Checks if OSTs are still healthy and not out of space.  Gets free space
6888  * on OSTs (relative to allocation watermark rmb_low) and compares to
6889  * the proposed new_end for this component.
6890  *
6891  * Decides whether or not to extend a component on its current OSTs.
6892  *
6893  * \param[in] env               execution environment for this thread
6894  * \param[in] lo                object we're checking
6895  * \param[in] index             index of this component
6896  * \param[in] extension_size    extension size for this component
6897  * \param[in] extent            layout extent for requested operation
6898  * \param[in] comp_extent       extension component extent
6899  * \param[in] write             if this is write operation
6900  *
6901  * \retval      true - OK to extend on current OSTs
6902  * \retval      false - do not extend on current OSTs
6903  */
6904 static bool lod_sel_osts_allowed(const struct lu_env *env,
6905                                  struct lod_object *lo,
6906                                  int index, __u64 reserve,
6907                                  struct lu_extent *extent,
6908                                  struct lu_extent *comp_extent, int write)
6909 {
6910         struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[index];
6911         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6912         struct lod_thread_info *tinfo = lod_env_info(env);
6913         struct obd_statfs *sfs = &tinfo->lti_osfs;
6914         __u64 available = 0;
6915         bool ret = true;
6916         int i, rc;
6917
6918         ENTRY;
6919
6920         LASSERT(lod_comp->llc_stripe_count != 0);
6921
6922         lod_getref(&lod->lod_ost_descs);
6923         for (i = 0; i < lod_comp->llc_stripe_count; i++) {
6924                 int index = lod_comp->llc_ost_indices[i];
6925                 struct lod_tgt_desc *ost = OST_TGT(lod, index);
6926                 struct obd_statfs_info info = { 0 };
6927                 int j, repeated = 0;
6928
6929                 LASSERT(ost);
6930
6931                 /* Get the number of times this OST repeats in this component.
6932                  * Note: inter-component repeats are not counted as this is
6933                  * considered as a rare case: we try to not repeat OST in other
6934                  * components if possible. */
6935                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6936                         if (index != lod_comp->llc_ost_indices[j])
6937                                 continue;
6938
6939                         /* already handled */
6940                         if (j < i)
6941                                 break;
6942
6943                         repeated++;
6944                 }
6945                 if (j < lod_comp->llc_stripe_count)
6946                         continue;
6947
6948                 if (!test_bit(index, lod->lod_ost_bitmap)) {
6949                         CDEBUG(D_LAYOUT, "ost %d no longer present\n", index);
6950                         ret = false;
6951                         break;
6952                 }
6953
6954                 rc = dt_statfs_info(env, ost->ltd_tgt, sfs, &info);
6955                 if (rc) {
6956                         CDEBUG(D_LAYOUT, "statfs failed for ost %d, error %d\n",
6957                                index, rc);
6958                         ret = false;
6959                         break;
6960                 }
6961
6962                 if (sfs->os_state & OS_STATFS_ENOSPC ||
6963                     sfs->os_state & OS_STATFS_READONLY ||
6964                     sfs->os_state & OS_STATFS_DEGRADED) {
6965                         CDEBUG(D_LAYOUT, "ost %d is not availble for SEL "
6966                                "extension, state %u\n", index, sfs->os_state);
6967                         ret = false;
6968                         break;
6969                 }
6970
6971                 /* In bytes */
6972                 available = sfs->os_bavail * sfs->os_bsize;
6973                 /* 'available' is relative to the allocation threshold */
6974                 available -= (__u64) info.os_reserved_mb_low << 20;
6975
6976                 CDEBUG(D_LAYOUT, "ost %d lowwm: %d highwm: %d, "
6977                        "%llu %% blocks available, %llu %% blocks free\n",
6978                        index, info.os_reserved_mb_low, info.os_reserved_mb_high,
6979                        (100ull * sfs->os_bavail) / sfs->os_blocks,
6980                        (100ull * sfs->os_bfree) / sfs->os_blocks);
6981
6982                 if (reserve * repeated > available) {
6983                         ret = false;
6984                         CDEBUG(D_LAYOUT, "low space on ost %d, available %llu "
6985                                "< extension size %llu repeated %d\n", index,
6986                                available, reserve, repeated);
6987                         break;
6988                 }
6989         }
6990         lod_putref(lod, &lod->lod_ost_descs);
6991
6992         RETURN(ret);
6993 }
6994
6995 /**
6996  * Adjust extents after component removal
6997  *
6998  * When we remove an extension component, we move the start of the next
6999  * component to match the start of the extension component, so no space is left
7000  * without layout.
7001  *
7002  * \param[in] env       execution environment for this thread
7003  * \param[in] lo        object
7004  * \param[in] max_comp  layout component
7005  * \param[in] index     index of this component
7006  *
7007  * \retval              0 on success
7008  * \retval              negative errno on error
7009  */
7010 static void lod_sel_adjust_extents(const struct lu_env *env,
7011                                    struct lod_object *lo,
7012                                    int max_comp, int index)
7013 {
7014         struct lod_layout_component *lod_comp = NULL;
7015         struct lod_layout_component *next = NULL;
7016         struct lod_layout_component *prev = NULL;
7017         __u64 new_start = 0;
7018         __u64 start;
7019         int i;
7020
7021         /* Extension space component */
7022         lod_comp = &lo->ldo_comp_entries[index];
7023         next = &lo->ldo_comp_entries[index + 1];
7024         prev = &lo->ldo_comp_entries[index - 1];
7025
7026         LASSERT(lod_comp != NULL && prev != NULL && next != NULL);
7027         LASSERT(lod_comp->llc_flags & LCME_FL_EXTENSION);
7028
7029         /* Previous is being removed */
7030         if (prev && prev->llc_id == LCME_ID_INVAL)
7031                 new_start = prev->llc_extent.e_start;
7032         else
7033                 new_start = lod_comp->llc_extent.e_start;
7034
7035         for (i = index + 1; i < max_comp; i++) {
7036                 lod_comp = &lo->ldo_comp_entries[i];
7037
7038                 start = lod_comp->llc_extent.e_start;
7039                 lod_comp->llc_extent.e_start = new_start;
7040
7041                 /* We only move zero length extendable components */
7042                 if (!(start == lod_comp->llc_extent.e_end))
7043                         break;
7044
7045                 LASSERT(!(lod_comp->llc_flags & LCME_FL_INIT));
7046
7047                 lod_comp->llc_extent.e_end = new_start;
7048         }
7049 }
7050
7051 /* Calculate the proposed 'new end' for a component we're extending */
7052 static __u64 lod_extension_new_end(__u64 extension_size, __u64 extent_end,
7053                                    __u32 stripe_size, __u64 component_end,
7054                                    __u64 extension_end)
7055 {
7056         __u64 new_end;
7057
7058         LASSERT(extension_size != 0 && stripe_size != 0);
7059
7060         /* Round up to extension size */
7061         if (extent_end == OBD_OBJECT_EOF) {
7062                 new_end = OBD_OBJECT_EOF;
7063         } else {
7064                 /* Add at least extension_size to the previous component_end,
7065                  * covering the req layout extent */
7066                 new_end = max(extent_end - component_end, extension_size);
7067                 new_end = roundup(new_end, extension_size);
7068                 new_end += component_end;
7069
7070                 /* Component end must be min stripe size aligned */
7071                 if (new_end % stripe_size) {
7072                         CDEBUG(D_LAYOUT, "new component end is not aligned "
7073                                "by the stripe size %u: [%llu, %llu) ext size "
7074                                "%llu new end %llu, aligning\n",
7075                                stripe_size, component_end, extent_end,
7076                                extension_size, new_end);
7077                         new_end = roundup(new_end, stripe_size);
7078                 }
7079
7080                 /* Overflow */
7081                 if (new_end < extent_end)
7082                         new_end = OBD_OBJECT_EOF;
7083         }
7084
7085         /* Don't extend past the end of the extension component */
7086         if (new_end > extension_end)
7087                 new_end = extension_end;
7088
7089         return new_end;
7090 }
7091
7092 /**
7093  * Calculate the exact reservation (per-OST extension_size) on the OSTs being
7094  * instantiated. It needs to be calculated in advance and taken into account at
7095  * the instantiation time, because otherwise lod_statfs_and_check() may consider
7096  * an OST as OK, but SEL needs its extension_size to fit the free space and the
7097  * OST may turn out to be low-on-space, thus inappropriate OST may be used and
7098  * ENOSPC occurs.
7099  *
7100  * \param[in] lod_comp          lod component we are checking
7101  *
7102  * \retval      size to reserved on each OST of lod_comp's stripe.
7103  */
7104 static __u64 lod_sel_stripe_reserved(struct lod_layout_component *lod_comp)
7105 {
7106         /* extension_size is file level, so we must divide by stripe count to
7107          * compare it to available space on a single OST */
7108         return  lod_comp->llc_stripe_size * SEL_UNIT_SIZE /
7109                 lod_comp->llc_stripe_count;
7110 }
7111
7112 /* As lod_sel_handler() could be re-entered for the same component several
7113  * times, this is the data for the next call. Fields could be changed to
7114  * component indexes when needed, (e.g. if there is no need to instantiate
7115  * all the previous components up to the current position) to tell the caller
7116  * where to start over from. */
7117 struct sel_data {
7118         int sd_force;
7119         int sd_repeat;
7120 };
7121
7122 /**
7123  * Process extent updates for a particular layout component
7124  *
7125  * Handle layout updates for a particular extension space component touched by
7126  * a layout update operation.  Core function of self-extending PFL feature.
7127  *
7128  * In general, this function processes exactly *one* stage of an extension
7129  * operation, modifying the layout accordingly, then returns to the caller.
7130  * The caller is responsible for restarting processing with the new layout,
7131  * which may repeatedly return to this function until the extension updates
7132  * are complete.
7133  *
7134  * This function does one of a few things to the layout:
7135  * 1. Extends the component before the current extension space component to
7136  * allow it to accomodate the requested operation (if space/policy permit that
7137  * component to continue on its current OSTs)
7138  *
7139  * 2. If extension of the existing component fails, we do one of two things:
7140  *    a. If there is a component after the extension space, we remove the
7141  *       extension space component, move the start of the next component down
7142  *       accordingly, then notify the caller to restart processing w/the new
7143  *       layout.
7144  *    b. If there is no following component, we try repeating the current
7145  *       component, creating a new component using the current one as a
7146  *       template (keeping its stripe properties but not specific striping),
7147  *       and try assigning striping for this component.  If there is sufficient
7148  *       free space on the OSTs chosen for this component, it is instantiated
7149  *       and i/o continues there.
7150  *
7151  *       If there is not sufficient space on the new OSTs, we remove this new
7152  *       component & extend the current component.
7153  *
7154  * Note further that uninited components followed by extension space can be zero
7155  * length meaning that we will try to extend them before initializing them, and
7156  * if that fails, they will be removed without initialization.
7157  *
7158  * 3. If we extend to/beyond the end of an extension space component, that
7159  * component is exhausted (all of its range has been given to real components),
7160  * so we remove it and restart processing.
7161  *
7162  * \param[in] env               execution environment for this thread
7163  * \param[in,out] lo            object to update the layout of
7164  * \param[in] extent            layout extent for requested operation, update
7165  *                              layout to fit this operation
7166  * \param[in] th                transaction handle for this operation
7167  * \param[in,out] max_comp      the highest comp for the portion of the layout
7168  *                              we are operating on (For FLR, the chosen
7169  *                              replica).  Updated because we may remove
7170  *                              components.
7171  * \param[in] index             index of the extension space component we're
7172  *                              working on
7173  * \param[in] write             if this is write op
7174  * \param[in,out] force         if the extension is to be forced; set here
7175                                 to force it on the 2nd call for the same
7176                                 extension component
7177  *
7178  * \retval      0 on success
7179  * \retval      negative errno on error
7180  */
7181 static int lod_sel_handler(const struct lu_env *env,
7182                           struct lod_object *lo,
7183                           struct lu_extent *extent,
7184                           struct thandle *th, int *max_comp,
7185                           int index, int write,
7186                           struct sel_data *sd)
7187 {
7188         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7189         struct lod_thread_info *info = lod_env_info(env);
7190         struct lod_layout_component *lod_comp;
7191         struct lod_layout_component *prev;
7192         struct lod_layout_component *next = NULL;
7193         __u64 extension_size, reserve;
7194         __u64 new_end = 0;
7195         bool repeated;
7196         int change = 0;
7197         int rc = 0;
7198         ENTRY;
7199
7200         /* First component cannot be extension space */
7201         if (index == 0) {
7202                 CERROR("%s: "DFID" first component cannot be extension space\n",
7203                        lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7204                 RETURN(-EINVAL);
7205         }
7206
7207         lod_comp = &lo->ldo_comp_entries[index];
7208         prev = &lo->ldo_comp_entries[index - 1];
7209         if ((index + 1) < *max_comp)
7210                 next = &lo->ldo_comp_entries[index + 1];
7211
7212         /* extension size uses the stripe size field as KiB */
7213         extension_size = lod_comp->llc_stripe_size * SEL_UNIT_SIZE;
7214
7215         CDEBUG(D_LAYOUT, "prev start %llu, extension start %llu, extension end"
7216                " %llu, extension size %llu\n", prev->llc_extent.e_start,
7217                lod_comp->llc_extent.e_start, lod_comp->llc_extent.e_end,
7218                extension_size);
7219
7220         /* Two extension space components cannot be adjacent & extension space
7221          * components cannot be init */
7222         if ((prev->llc_flags & LCME_FL_EXTENSION) ||
7223             !(ergo(next, !(next->llc_flags & LCME_FL_EXTENSION))) ||
7224              lod_comp_inited(lod_comp)) {
7225                 CERROR("%s: "DFID" invalid extension space components\n",
7226                        lod2obd(d)->obd_name, PFID(lod_object_fid(lo)));
7227                 RETURN(-EINVAL);
7228         }
7229
7230         reserve = lod_sel_stripe_reserved(lod_comp);
7231
7232         if (!prev->llc_stripe) {
7233                 CDEBUG(D_LAYOUT, "Previous component not inited\n");
7234                 info->lti_count = 1;
7235                 info->lti_comp_idx[0] = index - 1;
7236                 rc = lod_declare_instantiate_components(env, lo, th, reserve);
7237                 /* ENOSPC tells us we can't use this component.  If there is
7238                  * a next or we are repeating, we either spill over (next) or
7239                  * extend the original comp (repeat).  Otherwise, return the
7240                  * error to the user. */
7241                 if (rc == -ENOSPC && (next || sd->sd_repeat))
7242                         rc = 1;
7243                 if (rc < 0)
7244                         RETURN(rc);
7245         }
7246
7247         if (sd->sd_force == 0 && rc == 0)
7248                 rc = !lod_sel_osts_allowed(env, lo, index - 1, reserve, extent,
7249                                            &lod_comp->llc_extent, write);
7250
7251         repeated = !!(sd->sd_repeat);
7252         sd->sd_repeat = 0;
7253         sd->sd_force = 0;
7254
7255         /* Extend previous component */
7256         if (rc == 0) {
7257                 new_end = lod_extension_new_end(extension_size, extent->e_end,
7258                                                 prev->llc_stripe_size,
7259                                                 prev->llc_extent.e_end,
7260                                                 lod_comp->llc_extent.e_end);
7261
7262                 CDEBUG(D_LAYOUT, "new end %llu\n", new_end);
7263                 lod_comp->llc_extent.e_start = new_end;
7264                 prev->llc_extent.e_end = new_end;
7265
7266                 if (prev->llc_extent.e_end == lod_comp->llc_extent.e_end) {
7267                         CDEBUG(D_LAYOUT, "Extension component exhausted\n");
7268                         lod_comp->llc_id = LCME_ID_INVAL;
7269                         change--;
7270                 }
7271         } else {
7272                 /* rc == 1, failed to extend current component */
7273                 LASSERT(rc == 1);
7274                 if (next) {
7275                         /* Normal 'spillover' case - Remove the extension
7276                          * space component & bring down the start of the next
7277                          * component. */
7278                         lod_comp->llc_id = LCME_ID_INVAL;
7279                         change--;
7280                         if (!(prev->llc_flags & LCME_FL_INIT)) {
7281                                 prev->llc_id = LCME_ID_INVAL;
7282                                 change--;
7283                         }
7284                         lod_sel_adjust_extents(env, lo, *max_comp, index);
7285                 } else if (lod_comp_inited(prev)) {
7286                         /* If there is no next, and the previous component is
7287                          * INIT'ed, try repeating the previous component. */
7288                         LASSERT(repeated == 0);
7289                         rc = lod_layout_repeat_comp(env, lo, index - 1);
7290                         if (rc < 0)
7291                                 RETURN(rc);
7292                         change++;
7293                         /* The previous component is a repeated component.
7294                          * Record this so we don't keep trying to repeat it. */
7295                         sd->sd_repeat = 1;
7296                 } else {
7297                         /* If the previous component is not INIT'ed, this may
7298                          * be a component we have just instantiated but failed
7299                          * to extend. Or even a repeated component we failed
7300                          * to prepare a striping for. Do not repeat but instead
7301                          * remove the repeated component & force the extention
7302                          * of the original one */
7303                         sd->sd_force = 1;
7304                         if (repeated) {
7305                                 prev->llc_id = LCME_ID_INVAL;
7306                                 change--;
7307                         }
7308                 }
7309         }
7310
7311         if (change < 0) {
7312                 rc = lod_layout_del_prep_layout(env, lo, NULL);
7313                 if (rc < 0)
7314                         RETURN(rc);
7315                 LASSERTF(-rc == change,
7316                          "number deleted %d != requested %d\n", -rc,
7317                          change);
7318         }
7319         *max_comp = *max_comp + change;
7320
7321         /* lod_del_prep_layout reallocates ldo_comp_entries, so we must
7322          * refresh these pointers before using them */
7323         lod_comp = &lo->ldo_comp_entries[index];
7324         prev = &lo->ldo_comp_entries[index - 1];
7325         CDEBUG(D_LAYOUT, "After extent updates: prev start %llu, current start "
7326                "%llu, current end %llu max_comp %d ldo_comp_cnt %d\n",
7327                prev->llc_extent.e_start, lod_comp->llc_extent.e_start,
7328                lod_comp->llc_extent.e_end, *max_comp, lo->ldo_comp_cnt);
7329
7330         /* Layout changed successfully */
7331         RETURN(0);
7332 }
7333
7334 /**
7335  * Declare layout extent updates
7336  *
7337  * Handles extensions.  Identifies extension components touched by current
7338  * operation and passes them to processing function.
7339  *
7340  * Restarts with updated layouts from the processing function until the current
7341  * operation no longer touches an extension space component.
7342  *
7343  * \param[in] env       execution environment for this thread
7344  * \param[in,out] lo    object to update the layout of
7345  * \param[in] extent    layout extent for requested operation, update layout to
7346  *                      fit this operation
7347  * \param[in] th        transaction handle for this operation
7348  * \param[in] pick      identifies chosen mirror for FLR layouts
7349  * \param[in] write     if this is write op
7350  *
7351  * \retval      1 on layout changed, 0 on no change
7352  * \retval      negative errno on error
7353  */
7354 static int lod_declare_update_extents(const struct lu_env *env,
7355                 struct lod_object *lo, struct lu_extent *extent,
7356                 struct thandle *th, int pick, int write)
7357 {
7358         struct lod_thread_info *info = lod_env_info(env);
7359         struct lod_layout_component *lod_comp;
7360         bool layout_changed = false;
7361         struct sel_data sd = { 0 };
7362         int start_index;
7363         int i = 0;
7364         int max_comp = 0;
7365         int rc = 0, rc2;
7366         int change = 0;
7367         ENTRY;
7368
7369         /* This makes us work on the components of the chosen mirror */
7370         start_index = lo->ldo_mirrors[pick].lme_start;
7371         max_comp = lo->ldo_mirrors[pick].lme_end + 1;
7372         if (lo->ldo_flr_state == LCM_FL_NONE)
7373                 LASSERT(start_index == 0 && max_comp == lo->ldo_comp_cnt);
7374
7375         CDEBUG(D_LAYOUT, "extent->e_start %llu, extent->e_end %llu\n",
7376                extent->e_start, extent->e_end);
7377         for (i = start_index; i < max_comp; i++) {
7378                 lod_comp = &lo->ldo_comp_entries[i];
7379
7380                 /* We've passed all components of interest */
7381                 if (lod_comp->llc_extent.e_start >= extent->e_end)
7382                         break;
7383
7384                 if (lod_comp->llc_flags & LCME_FL_EXTENSION) {
7385                         layout_changed = true;
7386                         rc = lod_sel_handler(env, lo, extent, th, &max_comp,
7387                                              i, write, &sd);
7388                         if (rc < 0)
7389                                 GOTO(out, rc);
7390
7391                         /* Nothing has changed behind the prev one */
7392                         i -= 2;
7393                         continue;
7394                 }
7395         }
7396
7397         /* We may have added or removed components.  If so, we must update the
7398          * start & ends of all the mirrors after the current one, and the end
7399          * of the current mirror. */
7400         change = max_comp - 1 - lo->ldo_mirrors[pick].lme_end;
7401         if (change) {
7402                 lo->ldo_mirrors[pick].lme_end += change;
7403                 for (i = pick + 1; i < lo->ldo_mirror_count; i++) {
7404                         lo->ldo_mirrors[i].lme_start += change;
7405                         lo->ldo_mirrors[i].lme_end += change;
7406                 }
7407         }
7408
7409         EXIT;
7410 out:
7411         /* The amount of components has changed, adjust the lti_comp_idx */
7412         rc2 = lod_layout_data_init(info, lo->ldo_comp_cnt);
7413
7414         return rc < 0 ? rc : rc2 < 0 ? rc2 : layout_changed;
7415 }
7416
7417 /* If striping is already instantiated or INIT'ed DOM? */
7418 static bool lod_is_instantiation_needed(struct lod_layout_component *comp)
7419 {
7420         return !(((lov_pattern(comp->llc_pattern) == LOV_PATTERN_MDT) &&
7421                   lod_comp_inited(comp)) || comp->llc_stripe);
7422 }
7423
7424 /**
7425  * Declare layout update for a non-FLR layout.
7426  *
7427  * \param[in] env       execution environment for this thread
7428  * \param[in,out] lo    object to update the layout of
7429  * \param[in] layout    layout intent for requested operation, "update" is
7430  *                      a process of reacting to this
7431  * \param[in] buf       buffer containing lov ea (see comment on usage inline)
7432  * \param[in] th        transaction handle for this operation
7433  *
7434  * \retval      0 on success
7435  * \retval      negative errno on error
7436  */
7437 static int lod_declare_update_plain(const struct lu_env *env,
7438                 struct lod_object *lo, struct layout_intent *layout,
7439                 const struct lu_buf *buf, struct thandle *th)
7440 {
7441         struct lod_thread_info *info = lod_env_info(env);
7442         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7443         struct lod_layout_component *lod_comp;
7444         struct lov_comp_md_v1 *comp_v1 = NULL;
7445         bool layout_changed = false;
7446         bool replay = false;
7447         int i, rc;
7448         ENTRY;
7449
7450         LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
7451
7452         /*
7453          * In case the client is passing lovea, which only happens during
7454          * the replay of layout intent write RPC for now, we may need to
7455          * parse the lovea and apply new layout configuration.
7456          */
7457         if (buf && buf->lb_len)  {
7458                 struct lov_user_md_v1 *v1 = buf->lb_buf;
7459
7460                 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
7461                     v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
7462                                               LOV_MAGIC_COMP_V1)) {
7463                         CERROR("%s: the replay buffer of layout extend "
7464                                "(magic %#x) does not contain expected "
7465                                "composite layout.\n",
7466                                lod2obd(d)->obd_name, v1->lmm_magic);
7467                         GOTO(out, rc = -EINVAL);
7468                 }
7469
7470                 rc = lod_use_defined_striping(env, lo, buf);
7471                 if (rc)
7472                         GOTO(out, rc);
7473                 lo->ldo_comp_cached = 1;
7474
7475                 rc = lod_get_lov_ea(env, lo);
7476                 if (rc <= 0)
7477                         GOTO(out, rc);
7478                 /* old on-disk EA is stored in info->lti_buf */
7479                 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
7480                 replay = true;
7481                 layout_changed = true;
7482
7483                 rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
7484                 if (rc)
7485                         GOTO(out, rc);
7486         } else {
7487                 /* non replay path */
7488                 rc = lod_striping_load(env, lo);
7489                 if (rc)
7490                         GOTO(out, rc);
7491         }
7492
7493         /* Make sure defined layout covers the requested write range. */
7494         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
7495         if (lo->ldo_comp_cnt > 1 &&
7496             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
7497             lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
7498                 CDEBUG_LIMIT(replay ? D_ERROR : D_LAYOUT,
7499                              "%s: the defined layout [0, %#llx) does not "
7500                              "covers the write range "DEXT"\n",
7501                              lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
7502                              PEXT(&layout->li_extent));
7503                 GOTO(out, rc = -EINVAL);
7504         }
7505
7506         CDEBUG(D_LAYOUT, "%s: "DFID": update components "DEXT"\n",
7507                lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
7508                PEXT(&layout->li_extent));
7509
7510         if (!replay) {
7511                 rc = lod_declare_update_extents(env, lo, &layout->li_extent,
7512                                 th, 0, layout->li_opc == LAYOUT_INTENT_WRITE);
7513                 if (rc < 0)
7514                         GOTO(out, rc);
7515                 else if (rc)
7516                         layout_changed = true;
7517         }
7518
7519         /*
7520          * Iterate ld->ldo_comp_entries, find the component whose extent under
7521          * the write range and not instantianted.
7522          */
7523         for (i = 0; i < lo->ldo_comp_cnt; i++) {
7524                 lod_comp = &lo->ldo_comp_entries[i];
7525
7526                 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
7527                         break;
7528
7529                 if (!replay) {
7530                         /* If striping is instantiated or INIT'ed DOM skip */
7531                         if (!lod_is_instantiation_needed(lod_comp))
7532                                 continue;
7533                 } else {
7534                         /**
7535                          * In replay path, lod_comp is the EA passed by
7536                          * client replay buffer,  comp_v1 is the pre-recovery
7537                          * on-disk EA, we'd sift out those components which
7538                          * were init-ed in the on-disk EA.
7539                          */
7540                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
7541                             LCME_FL_INIT)
7542                                 continue;
7543                 }
7544                 /*
7545                  * this component hasn't instantiated in normal path, or during
7546                  * replay it needs replay the instantiation.
7547                  */
7548
7549                 /* A released component is being extended */
7550                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
7551                         GOTO(out, rc = -EINVAL);
7552
7553                 LASSERT(info->lti_comp_idx != NULL);
7554                 info->lti_comp_idx[info->lti_count++] = i;
7555                 layout_changed = true;
7556         }
7557
7558         if (!layout_changed)
7559                 RETURN(-EALREADY);
7560
7561         lod_obj_inc_layout_gen(lo);
7562         rc = lod_declare_instantiate_components(env, lo, th, 0);
7563         EXIT;
7564 out:
7565         if (rc)
7566                 lod_striping_free(env, lo);
7567         return rc;
7568 }
7569
7570 static inline int lod_comp_index(struct lod_object *lo,
7571                                  struct lod_layout_component *lod_comp)
7572 {
7573         LASSERT(lod_comp >= lo->ldo_comp_entries &&
7574                 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
7575
7576         return lod_comp - lo->ldo_comp_entries;
7577 }
7578
7579 /**
7580  * Stale other mirrors by writing extent.
7581  */
7582 static int lod_stale_components(const struct lu_env *env, struct lod_object *lo,
7583                                 int primary, struct lu_extent *extent,
7584                                 struct thandle *th)
7585 {
7586         struct lod_layout_component *pri_comp, *lod_comp;
7587         struct lu_extent pri_extent;
7588         int rc = 0;
7589         int i;
7590         ENTRY;
7591
7592         /* The writing extent decides which components in the primary
7593          * are affected... */
7594         CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
7595
7596 restart:
7597         lod_foreach_mirror_comp(pri_comp, lo, primary) {
7598                 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
7599                         continue;
7600
7601                 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
7602                        lod_comp_index(lo, pri_comp),
7603                        PEXT(&pri_comp->llc_extent));
7604
7605                 pri_extent.e_start = pri_comp->llc_extent.e_start;
7606                 pri_extent.e_end = pri_comp->llc_extent.e_end;
7607
7608                 for (i = 0; i < lo->ldo_mirror_count; i++) {
7609                         if (i == primary)
7610                                 continue;
7611                         rc = lod_declare_update_extents(env, lo, &pri_extent,
7612                                                         th, i, 0);
7613                         /* if update_extents changed the layout, it may have
7614                          * reallocated the component array, so start over to
7615                          * avoid using stale pointers */
7616                         if (rc == 1)
7617                                 goto restart;
7618                         if (rc < 0)
7619                                 RETURN(rc);
7620
7621                         /* ... and then stale other components that are
7622                          * overlapping with primary components */
7623                         lod_foreach_mirror_comp(lod_comp, lo, i) {
7624                                 if (!lu_extent_is_overlapped(
7625                                                         &pri_extent,
7626                                                         &lod_comp->llc_extent))
7627                                         continue;
7628
7629                                 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
7630                                       i, lod_comp_index(lo, lod_comp));
7631
7632                                 lod_comp->llc_flags |= LCME_FL_STALE;
7633                                 lo->ldo_mirrors[i].lme_stale = 1;
7634                         }
7635                 }
7636         }
7637
7638         RETURN(rc);
7639 }
7640
7641 /**
7642  * check an OST's availability
7643  * \param[in] env       execution environment
7644  * \param[in] lo        lod object
7645  * \param[in] dt        dt object
7646  * \param[in] index     mirror index
7647  *
7648  * \retval      negative if failed
7649  * \retval      1 if \a dt is available
7650  * \retval      0 if \a dt is not available
7651  */
7652 static inline int lod_check_ost_avail(const struct lu_env *env,
7653                                       struct lod_object *lo,
7654                                       struct dt_object *dt, int index)
7655 {
7656         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7657         struct lod_tgt_desc *ost;
7658         __u32 idx;
7659         int type = LU_SEQ_RANGE_OST;
7660         int rc;
7661
7662         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
7663         if (rc < 0) {
7664                 CERROR("%s: can't locate "DFID":rc = %d\n",
7665                        lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
7666                        rc);
7667                 return rc;
7668         }
7669
7670         ost = OST_TGT(lod, idx);
7671         if (ost->ltd_statfs.os_state &
7672                 (OS_STATFS_READONLY | OS_STATFS_ENOSPC | OS_STATFS_ENOINO |
7673                  OS_STATFS_NOPRECREATE) ||
7674             ost->ltd_active == 0) {
7675                 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
7676                        PFID(lod_object_fid(lo)), index, idx, rc);
7677                 return 0;
7678         }
7679
7680         return 1;
7681 }
7682
7683 /**
7684  * Pick primary mirror for write
7685  * \param[in] env       execution environment
7686  * \param[in] lo        object
7687  * \param[in] extent    write range
7688  */
7689 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
7690                             struct lu_extent *extent)
7691 {
7692         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
7693         unsigned int seq = 0;
7694         struct lod_layout_component *lod_comp;
7695         int i, j, rc;
7696         int picked = -1, second_pick = -1, third_pick = -1;
7697         ENTRY;
7698
7699         if (CFS_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
7700                 get_random_bytes(&seq, sizeof(seq));
7701                 seq %= lo->ldo_mirror_count;
7702         }
7703
7704         /**
7705          * Pick a mirror as the primary, and check the availability of OSTs.
7706          *
7707          * This algo can be revised later after knowing the topology of
7708          * cluster.
7709          */
7710         lod_qos_statfs_update(env, lod, &lod->lod_ost_descs);
7711
7712         rc = lod_fill_mirrors(lo);
7713         if (rc)
7714                 RETURN(rc);
7715
7716         for (i = 0; i < lo->ldo_mirror_count; i++) {
7717                 bool ost_avail = true;
7718                 int index = (i + seq) % lo->ldo_mirror_count;
7719
7720                 if (lo->ldo_mirrors[index].lme_stale) {
7721                         CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
7722                                PFID(lod_object_fid(lo)), index);
7723                         continue;
7724                 }
7725
7726                 /* 2nd pick is for the primary mirror containing unavail OST */
7727                 if (lo->ldo_mirrors[index].lme_prefer && second_pick < 0)
7728                         second_pick = index;
7729
7730                 /* 3rd pick is for non-primary mirror containing unavail OST */
7731                 if (second_pick < 0 && third_pick < 0)
7732                         third_pick = index;
7733
7734                 /**
7735                  * we found a non-primary 1st pick, we'd like to find a
7736                  * potential pirmary mirror.
7737                  */
7738                 if (picked >= 0 && !lo->ldo_mirrors[index].lme_prefer)
7739                         continue;
7740
7741                 /* check the availability of OSTs */
7742                 lod_foreach_mirror_comp(lod_comp, lo, index) {
7743                         if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
7744                                 continue;
7745
7746                         for (j = 0; j < lod_comp->llc_stripe_count; j++) {
7747                                 struct dt_object *dt = lod_comp->llc_stripe[j];
7748
7749                                 rc = lod_check_ost_avail(env, lo, dt, index);
7750                                 if (rc < 0)
7751                                         RETURN(rc);
7752
7753                                 ost_avail = !!rc;
7754                                 if (!ost_avail)
7755                                         break;
7756                         } /* for all dt object in one component */
7757                         if (!ost_avail)
7758                                 break;
7759                 } /* for all components in a mirror */
7760
7761                 /**
7762                  * the OSTs where allocated objects locates in the components
7763                  * of the mirror are available.
7764                  */
7765                 if (!ost_avail)
7766                         continue;
7767
7768                 /* this mirror has all OSTs available */
7769                 picked = index;
7770
7771                 /**
7772                  * primary with all OSTs are available, this is the perfect
7773                  * 1st pick.
7774                  */
7775                 if (lo->ldo_mirrors[index].lme_prefer)
7776                         break;
7777         } /* for all mirrors */
7778
7779         /* failed to pick a sound mirror, lower our expectation */
7780         if (picked < 0)
7781                 picked = second_pick;
7782         if (picked < 0)
7783                 picked = third_pick;
7784         if (picked < 0)
7785                 RETURN(-ENODATA);
7786
7787         RETURN(picked);
7788 }
7789
7790 static int lod_prepare_resync_mirror(const struct lu_env *env,
7791                                      struct lod_object *lo,
7792                                      __u16 mirror_id)
7793 {
7794         struct lod_thread_info *info = lod_env_info(env);
7795         struct lod_layout_component *lod_comp;
7796         bool neg = !!(MIRROR_ID_NEG & mirror_id);
7797         int i;
7798
7799         mirror_id &= ~MIRROR_ID_NEG;
7800
7801         for (i = 0; i < lo->ldo_mirror_count; i++) {
7802                 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
7803                     (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
7804                         continue;
7805
7806                 lod_foreach_mirror_comp(lod_comp, lo, i) {
7807                         if (lod_comp_inited(lod_comp))
7808                                 continue;
7809
7810                         info->lti_comp_idx[info->lti_count++] =
7811                                 lod_comp_index(lo, lod_comp);
7812                 }
7813         }
7814
7815         return 0;
7816 }
7817
7818 /**
7819  * figure out the components should be instantiated for resync.
7820  */
7821 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
7822                               struct lu_extent *extent)
7823 {
7824         struct lod_thread_info *info = lod_env_info(env);
7825         struct lod_layout_component *lod_comp;
7826         unsigned int need_sync = 0;
7827         int i;
7828
7829         CDEBUG(D_LAYOUT,
7830                DFID": instantiate all stale components in "DEXT"\n",
7831                PFID(lod_object_fid(lo)), PEXT(extent));
7832
7833         /**
7834          * instantiate all components within this extent, even non-stale
7835          * components.
7836          */
7837         for (i = 0; i < lo->ldo_mirror_count; i++) {
7838                 if (!lo->ldo_mirrors[i].lme_stale)
7839                         continue;
7840
7841                 lod_foreach_mirror_comp(lod_comp, lo, i) {
7842                         if (!lu_extent_is_overlapped(extent,
7843                                                 &lod_comp->llc_extent))
7844                                 break;
7845
7846                         need_sync++;
7847
7848                         if (lod_comp_inited(lod_comp))
7849                                 continue;
7850
7851                         CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
7852                                i, lod_comp_index(lo, lod_comp));
7853                         info->lti_comp_idx[info->lti_count++] =
7854                                         lod_comp_index(lo, lod_comp);
7855                 }
7856         }
7857
7858         return need_sync ? 0 : -EALREADY;
7859 }
7860
7861 static int lod_declare_update_rdonly(const struct lu_env *env,
7862                 struct lod_object *lo, struct md_layout_change *mlc,
7863                 struct thandle *th)
7864 {
7865         struct lod_thread_info *info = lod_env_info(env);
7866         struct lu_attr *layout_attr = &info->lti_layout_attr;
7867         struct lod_layout_component *lod_comp;
7868         struct lu_extent extent = { 0 };
7869         int rc;
7870         ENTRY;
7871
7872         LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
7873         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
7874                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
7875         LASSERT(lo->ldo_mirror_count > 0);
7876
7877         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
7878                 struct layout_intent *layout = mlc->mlc_intent;
7879                 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
7880                 int picked;
7881
7882                 extent = layout->li_extent;
7883                 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
7884                        PFID(lod_object_fid(lo)), PEXT(&extent));
7885
7886                 picked = lod_primary_pick(env, lo, &extent);
7887                 if (picked < 0)
7888                         RETURN(picked);
7889
7890                 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
7891                        PFID(lod_object_fid(lo)),
7892                        lo->ldo_mirrors[picked].lme_id);
7893
7894                 /* Update extents of primary before staling */
7895                 rc = lod_declare_update_extents(env, lo, &extent, th, picked,
7896                                                 write);
7897                 if (rc < 0)
7898                         GOTO(out, rc);
7899
7900                 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
7901                         /**
7902                          * trunc transfers [0, size) in the intent extent, we'd
7903                          * stale components overlapping [size, eof).
7904                          */
7905                         extent.e_start = extent.e_end;
7906                         extent.e_end = OBD_OBJECT_EOF;
7907                 }
7908
7909                 /* stale overlapping components from other mirrors */
7910                 rc = lod_stale_components(env, lo, picked, &extent, th);
7911                 if (rc < 0)
7912                         GOTO(out, rc);
7913
7914                 /* restore truncate intent extent */
7915                 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
7916                         extent.e_end = extent.e_start;
7917
7918                 /* instantiate components for the picked mirror, start from 0 */
7919                 extent.e_start = 0;
7920
7921                 lod_foreach_mirror_comp(lod_comp, lo, picked) {
7922                         if (!lu_extent_is_overlapped(&extent,
7923                                                      &lod_comp->llc_extent))
7924                                 break;
7925
7926                         if (!lod_is_instantiation_needed(lod_comp))
7927                                 continue;
7928
7929                         info->lti_comp_idx[info->lti_count++] =
7930                                                 lod_comp_index(lo, lod_comp);
7931                 }
7932
7933                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
7934         } else { /* MD_LAYOUT_RESYNC */
7935                 int i;
7936
7937                 /**
7938                  * could contain multiple non-stale mirrors, so we need to
7939                  * prep uninited all components assuming any non-stale mirror
7940                  * could be picked as the primary mirror.
7941                  */
7942                 if (mlc->mlc_mirror_id == 0) {
7943                         /* normal resync */
7944                         for (i = 0; i < lo->ldo_mirror_count; i++) {
7945                                 if (lo->ldo_mirrors[i].lme_stale)
7946                                         continue;
7947
7948                                 lod_foreach_mirror_comp(lod_comp, lo, i) {
7949                                         if (!lod_comp_inited(lod_comp))
7950                                                 break;
7951
7952                                         if (extent.e_end <
7953                                                 lod_comp->llc_extent.e_end)
7954                                                 extent.e_end =
7955                                                      lod_comp->llc_extent.e_end;
7956                                 }
7957                         }
7958                         rc = lod_prepare_resync(env, lo, &extent);
7959                         if (rc)
7960                                 GOTO(out, rc);
7961                 } else {
7962                         /* mirror write, try to init its all components */
7963                         rc = lod_prepare_resync_mirror(env, lo,
7964                                                        mlc->mlc_mirror_id);
7965                         if (rc)
7966                                 GOTO(out, rc);
7967                 }
7968
7969                 /* change the file state to SYNC_PENDING */
7970                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
7971         }
7972
7973         /* Reset the layout version once it's becoming too large.
7974          * This way it can make sure that the layout version is
7975          * monotonously increased in this writing era. */
7976         lod_obj_inc_layout_gen(lo);
7977
7978         rc = lod_declare_instantiate_components(env, lo, th, 0);
7979         if (rc)
7980                 GOTO(out, rc);
7981
7982         layout_attr->la_valid = LA_LAYOUT_VERSION;
7983         layout_attr->la_layout_version = 0;
7984         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
7985                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
7986         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
7987         if (rc)
7988                 GOTO(out, rc);
7989
7990 out:
7991         if (rc)
7992                 lod_striping_free(env, lo);
7993         RETURN(rc);
7994 }
7995
7996 static int lod_declare_update_write_pending(const struct lu_env *env,
7997                 struct lod_object *lo, struct md_layout_change *mlc,
7998                 struct thandle *th)
7999 {
8000         struct lod_thread_info *info = lod_env_info(env);
8001         struct lu_attr *layout_attr = &info->lti_layout_attr;
8002         struct lod_layout_component *lod_comp;
8003         struct lu_extent extent = { 0 };
8004         int primary = -1;
8005         int i;
8006         int rc;
8007         ENTRY;
8008
8009         LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
8010         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
8011                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
8012
8013         /* look for the first preferred mirror */
8014         for (i = 0; i < lo->ldo_mirror_count; i++) {
8015                 if (lo->ldo_mirrors[i].lme_stale)
8016                         continue;
8017                 if (lo->ldo_mirrors[i].lme_prefer == 0)
8018                         continue;
8019
8020                 primary = i;
8021                 break;
8022         }
8023         if (primary < 0) {
8024                 /* no primary, use any in-sync */
8025                 for (i = 0; i < lo->ldo_mirror_count; i++) {
8026                         if (lo->ldo_mirrors[i].lme_stale)
8027                                 continue;
8028                         primary = i;
8029                         break;
8030                 }
8031                 if (primary < 0) {
8032                         CERROR(DFID ": doesn't have a primary mirror\n",
8033                                PFID(lod_object_fid(lo)));
8034                         GOTO(out, rc = -ENODATA);
8035                 }
8036         }
8037
8038         CDEBUG(D_LAYOUT, DFID": found primary %u\n",
8039                PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
8040
8041         LASSERT(!lo->ldo_mirrors[primary].lme_stale);
8042
8043         /* for LAYOUT_WRITE opc, it has to do the following operations:
8044          * 1. stale overlapping componets from stale mirrors;
8045          * 2. instantiate components of the primary mirror;
8046          * 3. transfter layout version to all objects of the primary;
8047          *
8048          * for LAYOUT_RESYNC opc, it will do:
8049          * 1. instantiate components of all stale mirrors;
8050          * 2. transfer layout version to all objects to close write era. */
8051
8052         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
8053                 struct layout_intent *layout = mlc->mlc_intent;
8054                 int write = layout->li_opc == LAYOUT_INTENT_WRITE;
8055
8056                 LASSERT(mlc->mlc_intent != NULL);
8057
8058                 extent = mlc->mlc_intent->li_extent;
8059
8060                 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
8061                        PFID(lod_object_fid(lo)), PEXT(&extent));
8062
8063                 /* 1. Update extents of primary before staling */
8064                 rc = lod_declare_update_extents(env, lo, &extent, th, primary,
8065                                                 write);
8066                 if (rc < 0)
8067                         GOTO(out, rc);
8068
8069                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
8070                         /**
8071                          * trunc transfers [0, size) in the intent extent, we'd
8072                          * stale components overlapping [size, eof).
8073                          */
8074                         extent.e_start = extent.e_end;
8075                         extent.e_end = OBD_OBJECT_EOF;
8076                 }
8077
8078                 /* 2. stale overlapping components */
8079                 rc = lod_stale_components(env, lo, primary, &extent, th);
8080                 if (rc < 0)
8081                         GOTO(out, rc);
8082
8083                 /* 3. find the components which need instantiating.
8084                  * instantiate [0, mlc->mlc_intent->e_end) */
8085
8086                 /* restore truncate intent extent */
8087                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
8088                         extent.e_end = extent.e_start;
8089                 extent.e_start = 0;
8090
8091                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
8092                         if (!lu_extent_is_overlapped(&extent,
8093                                                      &lod_comp->llc_extent))
8094                                 break;
8095
8096                         if (!lod_is_instantiation_needed(lod_comp))
8097                                 continue;
8098
8099                         CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
8100                                primary, lod_comp_index(lo, lod_comp));
8101                         info->lti_comp_idx[info->lti_count++] =
8102                                                 lod_comp_index(lo, lod_comp);
8103                 }
8104         } else { /* MD_LAYOUT_RESYNC */
8105                 if (mlc->mlc_mirror_id == 0) {
8106                         /* normal resync */
8107                         lod_foreach_mirror_comp(lod_comp, lo, primary) {
8108                                 if (!lod_comp_inited(lod_comp))
8109                                         break;
8110
8111                                 extent.e_end = lod_comp->llc_extent.e_end;
8112                         }
8113
8114                         rc = lod_prepare_resync(env, lo, &extent);
8115                         if (rc)
8116                                 GOTO(out, rc);
8117                 } else {
8118                         /* mirror write, try to init its all components */
8119                         rc = lod_prepare_resync_mirror(env, lo,
8120                                                        mlc->mlc_mirror_id);
8121                         if (rc)
8122                                 GOTO(out, rc);
8123                 }
8124
8125                 /* change the file state to SYNC_PENDING */
8126                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
8127         }
8128
8129         rc = lod_declare_instantiate_components(env, lo, th, 0);
8130         if (rc)
8131                 GOTO(out, rc);
8132
8133         lod_obj_inc_layout_gen(lo);
8134
8135         /* 3. transfer layout version to OST objects.
8136          * transfer new layout version to OST objects so that stale writes
8137          * can be denied. It also ends an era of writing by setting
8138          * LU_LAYOUT_RESYNC. Normal client can never use this bit to
8139          * send write RPC; only resync RPCs could do it. */
8140         layout_attr->la_valid = LA_LAYOUT_VERSION;
8141         layout_attr->la_layout_version = 0;
8142         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
8143                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
8144         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8145         if (rc)
8146                 GOTO(out, rc);
8147 out:
8148         if (rc)
8149                 lod_striping_free(env, lo);
8150         RETURN(rc);
8151 }
8152
8153 static int lod_declare_update_sync_pending(const struct lu_env *env,
8154                 struct lod_object *lo, struct md_layout_change *mlc,
8155                 struct thandle *th)
8156 {
8157         struct lod_thread_info  *info = lod_env_info(env);
8158         struct lu_attr *layout_attr = &info->lti_layout_attr;
8159         unsigned sync_components = 0;
8160         unsigned resync_components = 0;
8161         int i;
8162         int rc;
8163         ENTRY;
8164
8165         LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
8166         LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
8167                 mlc->mlc_opc == MD_LAYOUT_WRITE);
8168
8169         CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
8170                PFID(lod_object_fid(lo)), mlc->mlc_opc);
8171
8172         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
8173                 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
8174                        PFID(lod_object_fid(lo)));
8175
8176                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
8177                 return lod_declare_update_write_pending(env, lo, mlc, th);
8178         }
8179
8180         /* MD_LAYOUT_RESYNC_DONE */
8181
8182         for (i = 0; i < lo->ldo_comp_cnt; i++) {
8183                 struct lod_layout_component *lod_comp;
8184                 int j;
8185
8186                 lod_comp = &lo->ldo_comp_entries[i];
8187
8188                 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
8189                         sync_components++;
8190                         continue;
8191                 }
8192
8193                 for (j = 0; j < mlc->mlc_resync_count; j++) {
8194                         if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
8195                                 continue;
8196
8197                         mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
8198                         lod_comp->llc_flags &= ~LCME_FL_STALE;
8199                         resync_components++;
8200                         break;
8201                 }
8202         }
8203
8204         /* valid check */
8205         for (i = 0; i < mlc->mlc_resync_count; i++) {
8206                 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
8207                         continue;
8208
8209                 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
8210                        "or already synced\n", PFID(lod_object_fid(lo)),
8211                        mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
8212                 GOTO(out, rc = -EINVAL);
8213         }
8214
8215         if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
8216                 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
8217                        PFID(lod_object_fid(lo)));
8218
8219                 /* tend to return an error code here to prevent
8220                  * the MDT from setting SoM attribute */
8221                 GOTO(out, rc = -EINVAL);
8222         }
8223
8224         CDEBUG(D_LAYOUT, DFID": synced %u resynced %u/%zu components\n",
8225                PFID(lod_object_fid(lo)),
8226                sync_components, resync_components, mlc->mlc_resync_count);
8227
8228         lo->ldo_flr_state = LCM_FL_RDONLY;
8229         lod_obj_inc_layout_gen(lo);
8230
8231         layout_attr->la_valid = LA_LAYOUT_VERSION;
8232         layout_attr->la_layout_version = 0;
8233         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
8234         if (rc)
8235                 GOTO(out, rc);
8236
8237         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
8238         rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
8239                                        &info->lti_buf, XATTR_NAME_LOV, 0, th);
8240         EXIT;
8241
8242 out:
8243         if (rc)
8244                 lod_striping_free(env, lo);
8245         RETURN(rc);
8246 }
8247
8248 typedef int (*mlc_handler)(const struct lu_env *env, struct dt_object *dt,
8249                            const struct md_layout_change *mlc,
8250                            struct thandle *th);
8251
8252 /**
8253  * Attach stripes after target's for migrating directory. NB, we
8254  * only need to declare this, the actual work is done inside
8255  * lod_xattr_set_lmv().
8256  *
8257  * \param[in] env       execution environment
8258  * \param[in] dt        target object
8259  * \param[in] mlc       layout change data
8260  * \param[in] th        transaction handle
8261  *
8262  * \retval              0 on success
8263  * \retval              negative if failed
8264  */
8265 static int lod_dir_declare_layout_attach(const struct lu_env *env,
8266                                          struct dt_object *dt,
8267                                          const struct md_layout_change *mlc,
8268                                          struct thandle *th)
8269 {
8270         struct lod_thread_info *info = lod_env_info(env);
8271         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8272         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
8273         struct lod_object *lo = lod_dt_obj(dt);
8274         struct dt_object *next = dt_object_child(dt);
8275         struct dt_object_format *dof = &info->lti_format;
8276         struct lmv_mds_md_v1 *lmv = mlc->mlc_buf.lb_buf;
8277         struct dt_object **stripes;
8278         __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
8279         struct lu_fid *fid = &info->lti_fid;
8280         struct lod_tgt_desc *tgt;
8281         struct dt_object *dto;
8282         struct dt_device *tgt_dt;
8283         int type = LU_SEQ_RANGE_ANY;
8284         struct dt_insert_rec *rec = &info->lti_dt_rec;
8285         char *stripe_name = info->lti_key;
8286         struct lu_name *sname;
8287         struct linkea_data ldata = { NULL };
8288         struct lu_buf linkea_buf;
8289         __u32 idx;
8290         int i;
8291         int rc;
8292
8293         ENTRY;
8294
8295         if (!lmv_is_sane(lmv))
8296                 RETURN(-EINVAL);
8297
8298         if (!dt_try_as_dir(env, dt, false))
8299                 RETURN(-ENOTDIR);
8300
8301         dof->dof_type = DFT_DIR;
8302
8303         OBD_ALLOC_PTR_ARRAY(stripes, (lo->ldo_dir_stripe_count + stripe_count));
8304         if (!stripes)
8305                 RETURN(-ENOMEM);
8306
8307         for (i = 0; i < lo->ldo_dir_stripe_count; i++)
8308                 stripes[i] = lo->ldo_stripe[i];
8309
8310         rec->rec_type = S_IFDIR;
8311
8312         for (i = 0; i < stripe_count; i++) {
8313                 fid_le_to_cpu(fid,
8314                         &lmv->lmv_stripe_fids[i]);
8315                 if (!fid_is_sane(fid))
8316                         continue;
8317
8318                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
8319                 if (rc)
8320                         GOTO(out, rc);
8321
8322                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
8323                         tgt_dt = lod->lod_child;
8324                 } else {
8325                         tgt = LTD_TGT(ltd, idx);
8326                         if (tgt == NULL)
8327                                 GOTO(out, rc = -ESTALE);
8328                         tgt_dt = tgt->ltd_tgt;
8329                 }
8330
8331                 dto = dt_locate_at(env, tgt_dt, fid,
8332                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
8333                                   NULL);
8334                 if (IS_ERR(dto))
8335                         GOTO(out, rc = PTR_ERR(dto));
8336
8337                 stripes[i + lo->ldo_dir_stripe_count] = dto;
8338
8339                 if (!dt_try_as_dir(env, dto, true))
8340                         GOTO(out, rc = -ENOTDIR);
8341
8342                 rc = lod_sub_declare_ref_add(env, dto, th);
8343                 if (rc)
8344                         GOTO(out, rc);
8345
8346                 rec->rec_fid = lu_object_fid(&dto->do_lu);
8347                 rc = lod_sub_declare_insert(env, dto,
8348                                             (const struct dt_rec *)rec,
8349                                             (const struct dt_key *)dot, th);
8350                 if (rc)
8351                         GOTO(out, rc);
8352
8353                 rc = lod_sub_declare_insert(env, dto,
8354                                             (const struct dt_rec *)rec,
8355                                             (const struct dt_key *)dotdot, th);
8356                 if (rc)
8357                         GOTO(out, rc);
8358
8359                 rc = lod_sub_declare_xattr_set(env, dto, &mlc->mlc_buf,
8360                                                 XATTR_NAME_LMV, 0, th);
8361                 if (rc)
8362                         GOTO(out, rc);
8363
8364                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
8365                          PFID(lu_object_fid(&dto->do_lu)),
8366                          i + lo->ldo_dir_stripe_count);
8367
8368                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
8369                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
8370                                       sname, lu_object_fid(&dt->do_lu));
8371                 if (rc)
8372                         GOTO(out, rc);
8373
8374                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
8375                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
8376                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
8377                                                XATTR_NAME_LINK, 0, th);
8378                 if (rc)
8379                         GOTO(out, rc);
8380
8381                 rc = lod_sub_declare_insert(env, next,
8382                                             (const struct dt_rec *)rec,
8383                                             (const struct dt_key *)stripe_name,
8384                                             th);
8385                 if (rc)
8386                         GOTO(out, rc);
8387
8388                 rc = lod_sub_declare_ref_add(env, next, th);
8389                 if (rc)
8390                         GOTO(out, rc);
8391         }
8392
8393         if (lo->ldo_stripe)
8394                 OBD_FREE_PTR_ARRAY(lo->ldo_stripe,
8395                                    lo->ldo_dir_stripes_allocated);
8396         lo->ldo_stripe = stripes;
8397         lo->ldo_is_foreign = 0;
8398         lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
8399         lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
8400         lo->ldo_dir_stripe_count += stripe_count;
8401         lo->ldo_dir_stripes_allocated += stripe_count;
8402
8403         /* plain directory split creates target as a plain directory, while
8404          * after source attached as the first stripe, it becomes a striped
8405          * directory, set correct do_index_ops, otherwise it can't be unlinked.
8406          */
8407         dt->do_index_ops = &lod_striped_index_ops;
8408
8409         RETURN(0);
8410 out:
8411         i = lo->ldo_dir_stripe_count;
8412         while (i < lo->ldo_dir_stripe_count + stripe_count && stripes[i])
8413                 dt_object_put(env, stripes[i++]);
8414
8415         OBD_FREE_PTR_ARRAY(stripes, stripe_count + lo->ldo_dir_stripe_count);
8416         return rc;
8417 }
8418
8419 static int lod_dir_declare_layout_detach(const struct lu_env *env,
8420                                          struct dt_object *dt,
8421                                          const struct md_layout_change *unused,
8422                                          struct thandle *th)
8423 {
8424         struct lod_thread_info *info = lod_env_info(env);
8425         struct lod_object *lo = lod_dt_obj(dt);
8426         struct dt_object *next = dt_object_child(dt);
8427         char *stripe_name = info->lti_key;
8428         struct dt_object *dto;
8429         int i;
8430         int rc = 0;
8431
8432         if (!dt_try_as_dir(env, dt, true))
8433                 return -ENOTDIR;
8434
8435         if (!lo->ldo_dir_stripe_count)
8436                 return lod_sub_declare_delete(env, next,
8437                                         (const struct dt_key *)dotdot, th);
8438
8439         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8440                 dto = lo->ldo_stripe[i];
8441                 if (!dto)
8442                         continue;
8443
8444                 if (!dt_try_as_dir(env, dto, true))
8445                         return -ENOTDIR;
8446
8447                 rc = lod_sub_declare_delete(env, dto,
8448                                         (const struct dt_key *)dotdot, th);
8449                 if (rc)
8450                         return rc;
8451
8452                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8453                          PFID(lu_object_fid(&dto->do_lu)), i);
8454
8455                 rc = lod_sub_declare_delete(env, next,
8456                                         (const struct dt_key *)stripe_name, th);
8457                 if (rc)
8458                         return rc;
8459
8460                 rc = lod_sub_declare_ref_del(env, next, th);
8461                 if (rc)
8462                         return rc;
8463         }
8464
8465         return 0;
8466 }
8467
8468 static int dt_dir_is_empty(const struct lu_env *env,
8469                            struct dt_object *obj)
8470 {
8471         struct dt_it *it;
8472         const struct dt_it_ops *iops;
8473         int rc;
8474
8475         ENTRY;
8476
8477         if (!dt_try_as_dir(env, obj, true))
8478                 RETURN(-ENOTDIR);
8479
8480         iops = &obj->do_index_ops->dio_it;
8481         it = iops->init(env, obj, LUDA_64BITHASH);
8482         if (IS_ERR(it))
8483                 RETURN(PTR_ERR(it));
8484
8485         rc = iops->get(env, it, (const struct dt_key *)"");
8486         if (rc > 0) {
8487                 int i;
8488
8489                 for (rc = 0, i = 0; rc == 0 && i < 3; ++i)
8490                         rc = iops->next(env, it);
8491                 if (!rc)
8492                         rc = -ENOTEMPTY;
8493                 else if (rc == 1)
8494                         rc = 0;
8495         } else if (!rc) {
8496                 /* Huh? Index contains no zero key? */
8497                 rc = -EIO;
8498         }
8499
8500         iops->put(env, it);
8501         iops->fini(env, it);
8502
8503         RETURN(rc);
8504 }
8505
8506 static int lod_dir_declare_layout_shrink(const struct lu_env *env,
8507                                          struct dt_object *dt,
8508                                          const struct md_layout_change *mlc,
8509                                          struct thandle *th)
8510 {
8511         struct lod_thread_info *info = lod_env_info(env);
8512         struct lod_object *lo = lod_dt_obj(dt);
8513         struct dt_object *next = dt_object_child(dt);
8514         struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8515         char *stripe_name = info->lti_key;
8516         struct lu_buf *lmv_buf = &info->lti_buf;
8517         __u32 final_stripe_count;
8518         struct dt_object *dto;
8519         int i;
8520         int rc;
8521
8522         LASSERT(lmu);
8523
8524         if (!dt_try_as_dir(env, dt, true))
8525                 return -ENOTDIR;
8526
8527         /* shouldn't be called on plain directory */
8528         LASSERT(lo->ldo_dir_stripe_count);
8529
8530         lmv_buf->lb_buf = &info->lti_lmv.lmv_md_v1;
8531         lmv_buf->lb_len = sizeof(info->lti_lmv.lmv_md_v1);
8532
8533         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8534         LASSERT(final_stripe_count &&
8535                 final_stripe_count < lo->ldo_dir_stripe_count);
8536
8537         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8538                 dto = lo->ldo_stripe[i];
8539                 if (!dto)
8540                         continue;
8541
8542                 if (i < final_stripe_count) {
8543                         rc = lod_sub_declare_xattr_set(env, dto, lmv_buf,
8544                                                        XATTR_NAME_LMV,
8545                                                        LU_XATTR_REPLACE, th);
8546                         if (rc)
8547                                 return rc;
8548
8549                         continue;
8550                 }
8551
8552                 rc = dt_dir_is_empty(env, dto);
8553                 if (rc < 0)
8554                         return rc;
8555
8556                 rc = lod_sub_declare_ref_del(env, dto, th);
8557                 if (rc)
8558                         return rc;
8559
8560                 rc = lod_sub_declare_destroy(env, dto, th);
8561                 if (rc)
8562                         return rc;
8563
8564                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8565                          PFID(lu_object_fid(&dto->do_lu)), i);
8566
8567                 rc = lod_sub_declare_delete(env, next,
8568                                         (const struct dt_key *)stripe_name, th);
8569                 if (rc)
8570                         return rc;
8571
8572                 rc = lod_sub_declare_ref_del(env, next, th);
8573                 if (rc)
8574                         return rc;
8575         }
8576
8577         rc = lod_sub_declare_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8578                                        LU_XATTR_REPLACE, th);
8579         return rc;
8580 }
8581
8582 /**
8583  * Allocate stripes for split directory.
8584  *
8585  * \param[in] env       execution environment
8586  * \param[in] dt        target object
8587  * \param[in] mlc       layout change data
8588  * \param[in] th        transaction handle
8589  *
8590  * \retval              0 on success
8591  * \retval              negative if failed
8592  */
8593 static int lod_dir_declare_layout_split(const struct lu_env *env,
8594                                         struct dt_object *dt,
8595                                         const struct md_layout_change *mlc,
8596                                         struct thandle *th)
8597 {
8598         struct lod_thread_info *info = lod_env_info(env);
8599         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
8600         struct lod_object *lo = lod_dt_obj(dt);
8601         struct dt_object_format *dof = &info->lti_format;
8602         struct lmv_user_md_v1 *lum = mlc->mlc_spec->u.sp_ea.eadata;
8603         struct dt_object **stripes;
8604         u32 stripe_count;
8605         u32 saved_count;
8606         int i;
8607         int rc;
8608
8609         ENTRY;
8610
8611         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
8612         LASSERT(le32_to_cpu(lum->lum_stripe_offset) == LMV_OFFSET_DEFAULT);
8613
8614         saved_count = lo->ldo_dir_stripes_allocated;
8615         stripe_count = le32_to_cpu(lum->lum_stripe_count);
8616         if (stripe_count <= saved_count)
8617                 RETURN(-EINVAL);
8618
8619         dof->dof_type = DFT_DIR;
8620
8621         OBD_ALLOC(stripes, sizeof(*stripes) * stripe_count);
8622         if (!stripes)
8623                 RETURN(-ENOMEM);
8624
8625         for (i = 0; i < lo->ldo_dir_stripes_allocated; i++)
8626                 stripes[i] = lo->ldo_stripe[i];
8627
8628         lod_qos_statfs_update(env, lod, &lod->lod_mdt_descs);
8629         rc = lod_mdt_alloc_qos(env, lo, stripes, saved_count, stripe_count);
8630         if (rc == -EAGAIN)
8631                 rc = lod_mdt_alloc_rr(env, lo, stripes, saved_count,
8632                                       stripe_count);
8633         if (rc < 0) {
8634                 OBD_FREE(stripes, sizeof(*stripes) * stripe_count);
8635                 RETURN(rc);
8636         }
8637
8638         LASSERT(rc > saved_count);
8639         OBD_FREE(lo->ldo_stripe,
8640                  sizeof(*stripes) * lo->ldo_dir_stripes_allocated);
8641         lo->ldo_stripe = stripes;
8642         lo->ldo_is_foreign = 0;
8643         lo->ldo_dir_striped = 1;
8644         lo->ldo_dir_stripe_count = rc;
8645         lo->ldo_dir_stripes_allocated = stripe_count;
8646         lo->ldo_dir_split_hash = lo->ldo_dir_hash_type;
8647         lo->ldo_dir_hash_type = le32_to_cpu(lum->lum_hash_type);
8648         if (!lmv_is_known_hash_type(lo->ldo_dir_hash_type))
8649                 lo->ldo_dir_hash_type =
8650                         lod->lod_mdt_descs.ltd_lmv_desc.ld_pattern;
8651         lo->ldo_dir_hash_type |= LMV_HASH_FLAG_SPLIT | LMV_HASH_FLAG_MIGRATION;
8652         lo->ldo_dir_split_offset = saved_count;
8653         lo->ldo_dir_layout_version++;
8654         lo->ldo_dir_stripe_loaded = 1;
8655
8656         rc = lod_dir_declare_create_stripes(env, dt, mlc->mlc_attr, dof, th);
8657         if (rc)
8658                 lod_striping_free(env, lo);
8659
8660         RETURN(rc);
8661 }
8662
8663 /*
8664  * detach all stripes from dir master object, NB, stripes are not destroyed, but
8665  * deleted from it's parent namespace, this function is called in two places:
8666  * 1. mdd_migrate_mdt() detach stripes from source, and attach them to
8667  *    target.
8668  * 2. mdd_dir_layout_update() detach stripe before turning 1-stripe directory to
8669  *    a plain directory.
8670  *
8671  * \param[in] env       execution environment
8672  * \param[in] dt        target object
8673  * \param[in] mlc       layout change data
8674  * \param[in] th        transaction handle
8675  *
8676  * \retval              0 on success
8677  * \retval              negative if failed
8678  */
8679 static int lod_dir_layout_detach(const struct lu_env *env,
8680                                  struct dt_object *dt,
8681                                  const struct md_layout_change *mlc,
8682                                  struct thandle *th)
8683 {
8684         struct lod_thread_info *info = lod_env_info(env);
8685         struct lod_object *lo = lod_dt_obj(dt);
8686         struct dt_object *next = dt_object_child(dt);
8687         char *stripe_name = info->lti_key;
8688         struct dt_object *dto;
8689         int i;
8690         int rc = 0;
8691
8692         ENTRY;
8693
8694         if (!lo->ldo_dir_stripe_count) {
8695                 /* plain directory delete .. */
8696                 rc = lod_sub_delete(env, next,
8697                                     (const struct dt_key *)dotdot, th);
8698                 RETURN(rc);
8699         }
8700
8701         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8702                 dto = lo->ldo_stripe[i];
8703                 if (!dto)
8704                         continue;
8705
8706                 rc = lod_sub_delete(env, dto,
8707                                     (const struct dt_key *)dotdot, th);
8708                 if (rc)
8709                         break;
8710
8711                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8712                          PFID(lu_object_fid(&dto->do_lu)), i);
8713
8714                 rc = lod_sub_delete(env, next,
8715                                     (const struct dt_key *)stripe_name, th);
8716                 if (rc)
8717                         break;
8718
8719                 rc = lod_sub_ref_del(env, next, th);
8720                 if (rc)
8721                         break;
8722         }
8723
8724         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8725                 dto = lo->ldo_stripe[i];
8726                 if (dto)
8727                         dt_object_put(env, dto);
8728         }
8729         OBD_FREE_PTR_ARRAY(lo->ldo_stripe, lo->ldo_dir_stripes_allocated);
8730         lo->ldo_stripe = NULL;
8731         lo->ldo_dir_stripes_allocated = 0;
8732         lo->ldo_dir_stripe_count = 0;
8733         dt->do_index_ops = &lod_index_ops;
8734
8735         RETURN(rc);
8736 }
8737
8738 static int lod_dir_layout_shrink(const struct lu_env *env,
8739                                  struct dt_object *dt,
8740                                  const struct md_layout_change *mlc,
8741                                  struct thandle *th)
8742 {
8743         struct lod_thread_info *info = lod_env_info(env);
8744         struct lod_object *lo = lod_dt_obj(dt);
8745         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
8746         struct dt_object *next = dt_object_child(dt);
8747         struct lmv_user_md *lmu = mlc->mlc_buf.lb_buf;
8748         __u32 final_stripe_count;
8749         char *stripe_name = info->lti_key;
8750         struct dt_object *dto;
8751         struct lu_buf *lmv_buf = &info->lti_buf;
8752         struct lmv_mds_md_v1 *lmv = &info->lti_lmv.lmv_md_v1;
8753         u32 mdtidx;
8754         int type = LU_SEQ_RANGE_ANY;
8755         int i;
8756         int rc;
8757
8758         ENTRY;
8759
8760         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
8761
8762         lmv_buf->lb_buf = lmv;
8763         lmv_buf->lb_len = sizeof(*lmv);
8764         lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
8765         lmv->lmv_stripe_count = cpu_to_le32(final_stripe_count);
8766         lmv->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type) &
8767                              cpu_to_le32(LMV_HASH_TYPE_MASK |
8768                                          LMV_HASH_FLAG_FIXED);
8769         lmv->lmv_layout_version =
8770                         cpu_to_le32(lo->ldo_dir_layout_version + 1);
8771         lmv->lmv_migrate_offset = 0;
8772         lmv->lmv_migrate_hash = 0;
8773
8774         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
8775                 dto = lo->ldo_stripe[i];
8776                 if (!dto)
8777                         continue;
8778
8779                 if (i < final_stripe_count) {
8780                         rc = lod_fld_lookup(env, lod,
8781                                             lu_object_fid(&dto->do_lu),
8782                                             &mdtidx, &type);
8783                         if (rc)
8784                                 RETURN(rc);
8785
8786                         lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8787                         rc = lod_sub_xattr_set(env, dto, lmv_buf,
8788                                                XATTR_NAME_LMV,
8789                                                LU_XATTR_REPLACE, th);
8790                         if (rc)
8791                                 RETURN(rc);
8792
8793                         continue;
8794                 }
8795
8796                 dt_write_lock(env, dto, DT_TGT_CHILD);
8797                 rc = lod_sub_ref_del(env, dto, th);
8798                 dt_write_unlock(env, dto);
8799                 if (rc)
8800                         RETURN(rc);
8801
8802                 rc = lod_sub_destroy(env, dto, th);
8803                 if (rc)
8804                         RETURN(rc);
8805
8806                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
8807                          PFID(lu_object_fid(&dto->do_lu)), i);
8808
8809                 rc = lod_sub_delete(env, next,
8810                                     (const struct dt_key *)stripe_name, th);
8811                 if (rc)
8812                         RETURN(rc);
8813
8814                 rc = lod_sub_ref_del(env, next, th);
8815                 if (rc)
8816                         RETURN(rc);
8817         }
8818
8819         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &mdtidx,
8820                             &type);
8821         if (rc)
8822                 RETURN(rc);
8823
8824         lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_V1);
8825         lmv->lmv_master_mdt_index = cpu_to_le32(mdtidx);
8826         rc = lod_sub_xattr_set(env, next, lmv_buf, XATTR_NAME_LMV,
8827                                LU_XATTR_REPLACE, th);
8828         if (rc)
8829                 RETURN(rc);
8830
8831         for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
8832                 dto = lo->ldo_stripe[i];
8833                 if (dto)
8834                         dt_object_put(env, dto);
8835         }
8836         lo->ldo_dir_stripe_count = final_stripe_count;
8837
8838         RETURN(rc);
8839 }
8840
8841 static mlc_handler dir_mlc_declare_ops[MD_LAYOUT_MAX] = {
8842         [MD_LAYOUT_ATTACH] = lod_dir_declare_layout_attach,
8843         [MD_LAYOUT_DETACH] = lod_dir_declare_layout_detach,
8844         [MD_LAYOUT_SHRINK] = lod_dir_declare_layout_shrink,
8845         [MD_LAYOUT_SPLIT]  = lod_dir_declare_layout_split,
8846 };
8847
8848 static mlc_handler dir_mlc_ops[MD_LAYOUT_MAX] = {
8849         [MD_LAYOUT_DETACH] = lod_dir_layout_detach,
8850         [MD_LAYOUT_SHRINK] = lod_dir_layout_shrink,
8851 };
8852
8853 static int lod_declare_layout_change(const struct lu_env *env,
8854                 struct dt_object *dt, struct md_layout_change *mlc,
8855                 struct thandle *th)
8856 {
8857         struct lod_thread_info  *info = lod_env_info(env);
8858         struct lod_object *lo = lod_dt_obj(dt);
8859         int rc;
8860
8861         ENTRY;
8862
8863         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8864                 LASSERT(dir_mlc_declare_ops[mlc->mlc_opc]);
8865                 rc = dir_mlc_declare_ops[mlc->mlc_opc](env, dt, mlc, th);
8866                 RETURN(rc);
8867         }
8868
8869         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
8870             dt_object_remote(dt_object_child(dt)))
8871                 RETURN(-EINVAL);
8872
8873         rc = lod_striping_load(env, lo);
8874         if (rc)
8875                 GOTO(out, rc);
8876
8877         LASSERT(lo->ldo_comp_cnt > 0);
8878
8879         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
8880         if (rc)
8881                 GOTO(out, rc);
8882
8883         switch (lo->ldo_flr_state) {
8884         case LCM_FL_NONE:
8885                 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
8886                                               &mlc->mlc_buf, th);
8887                 break;
8888         case LCM_FL_RDONLY:
8889                 rc = lod_declare_update_rdonly(env, lo, mlc, th);
8890                 break;
8891         case LCM_FL_WRITE_PENDING:
8892                 rc = lod_declare_update_write_pending(env, lo, mlc, th);
8893                 break;
8894         case LCM_FL_SYNC_PENDING:
8895                 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
8896                 break;
8897         default:
8898                 rc = -ENOTSUPP;
8899                 break;
8900         }
8901 out:
8902         RETURN(rc);
8903 }
8904
8905 /**
8906  * Instantiate layout component objects which covers the intent write offset.
8907  */
8908 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
8909                              struct md_layout_change *mlc, struct thandle *th)
8910 {
8911         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
8912         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
8913         struct lod_object *lo = lod_dt_obj(dt);
8914         int rc;
8915
8916         ENTRY;
8917
8918         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
8919                 LASSERT(dir_mlc_ops[mlc->mlc_opc]);
8920                 rc = dir_mlc_ops[mlc->mlc_opc](env, dt, mlc, th);
8921                 RETURN(rc);
8922         }
8923
8924         rc = lod_striped_create(env, dt, attr, NULL, th);
8925         if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
8926                 layout_attr->la_layout_version |= lo->ldo_layout_gen;
8927                 rc = lod_attr_set(env, dt, layout_attr, th);
8928         }
8929
8930         RETURN(rc);
8931 }
8932
8933 const struct dt_object_operations lod_obj_ops = {
8934         .do_read_lock           = lod_read_lock,
8935         .do_write_lock          = lod_write_lock,
8936         .do_read_unlock         = lod_read_unlock,
8937         .do_write_unlock        = lod_write_unlock,
8938         .do_write_locked        = lod_write_locked,
8939         .do_attr_get            = lod_attr_get,
8940         .do_declare_attr_set    = lod_declare_attr_set,
8941         .do_attr_set            = lod_attr_set,
8942         .do_xattr_get           = lod_xattr_get,
8943         .do_declare_xattr_set   = lod_declare_xattr_set,
8944         .do_xattr_set           = lod_xattr_set,
8945         .do_declare_xattr_del   = lod_declare_xattr_del,
8946         .do_xattr_del           = lod_xattr_del,
8947         .do_xattr_list          = lod_xattr_list,
8948         .do_ah_init             = lod_ah_init,
8949         .do_declare_create      = lod_declare_create,
8950         .do_create              = lod_create,
8951         .do_declare_destroy     = lod_declare_destroy,
8952         .do_destroy             = lod_destroy,
8953         .do_index_try           = lod_index_try,
8954         .do_declare_ref_add     = lod_declare_ref_add,
8955         .do_ref_add             = lod_ref_add,
8956         .do_declare_ref_del     = lod_declare_ref_del,
8957         .do_ref_del             = lod_ref_del,
8958         .do_object_sync         = lod_object_sync,
8959         .do_object_lock         = lod_object_lock,
8960         .do_object_unlock       = lod_object_unlock,
8961         .do_invalidate          = lod_invalidate,
8962         .do_declare_layout_change = lod_declare_layout_change,
8963         .do_layout_change       = lod_layout_change,
8964 };
8965
8966 /**
8967  * Implementation of dt_body_operations::dbo_read.
8968  *
8969  * \see dt_body_operations::dbo_read() in the API description for details.
8970  */
8971 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
8972                         struct lu_buf *buf, loff_t *pos)
8973 {
8974         struct dt_object *next = dt_object_child(dt);
8975
8976         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
8977                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
8978         return next->do_body_ops->dbo_read(env, next, buf, pos);
8979 }
8980
8981 /**
8982  * Implementation of dt_body_operations::dbo_declare_write.
8983  *
8984  * \see dt_body_operations::dbo_declare_write() in the API description
8985  * for details.
8986  */
8987 static ssize_t lod_declare_write(const struct lu_env *env,
8988                                  struct dt_object *dt,
8989                                  const struct lu_buf *buf, loff_t pos,
8990                                  struct thandle *th)
8991 {
8992         return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
8993 }
8994
8995 /**
8996  * Implementation of dt_body_operations::dbo_write.
8997  *
8998  * \see dt_body_operations::dbo_write() in the API description for details.
8999  */
9000 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
9001                          const struct lu_buf *buf, loff_t *pos,
9002                          struct thandle *th)
9003 {
9004         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
9005                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
9006         return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
9007 }
9008
9009 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
9010                              __u64 start, __u64 end, struct thandle *th)
9011 {
9012         if (dt_object_remote(dt))
9013                 return -ENOTSUPP;
9014
9015         return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
9016 }
9017
9018 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
9019                      __u64 start, __u64 end, struct thandle *th)
9020 {
9021         if (dt_object_remote(dt))
9022                 return -ENOTSUPP;
9023
9024         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
9025         return lod_sub_punch(env, dt_object_child(dt), start, end, th);
9026 }
9027
9028 /*
9029  * different type of files use the same body_ops because object may be created
9030  * in OUT, where there is no chance to set correct body_ops for each type, so
9031  * body_ops themselves will check file type inside, see lod_read/write/punch for
9032  * details.
9033  */
9034 static const struct dt_body_operations lod_body_ops = {
9035         .dbo_read               = lod_read,
9036         .dbo_declare_write      = lod_declare_write,
9037         .dbo_write              = lod_write,
9038         .dbo_declare_punch      = lod_declare_punch,
9039         .dbo_punch              = lod_punch,
9040 };
9041
9042 /**
9043  * Implementation of lu_object_operations::loo_object_init.
9044  *
9045  * The function determines the type and the index of the target device using
9046  * sequence of the object's FID. Then passes control down to the
9047  * corresponding device:
9048  *  OSD for the local objects, OSP for remote
9049  *
9050  * \see lu_object_operations::loo_object_init() in the API description
9051  * for details.
9052  */
9053 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
9054                            const struct lu_object_conf *conf)
9055 {
9056         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
9057         struct lu_device        *cdev   = NULL;
9058         struct lu_object        *cobj;
9059         struct lod_tgt_descs    *ltd    = NULL;
9060         struct lod_tgt_desc     *tgt;
9061         u32                      idx    = 0;
9062         int                      type   = LU_SEQ_RANGE_ANY;
9063         int                      rc;
9064         ENTRY;
9065
9066         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
9067         if (rc != 0)
9068                 RETURN(rc);
9069
9070         if (type == LU_SEQ_RANGE_MDT &&
9071             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
9072                 cdev = &lod->lod_child->dd_lu_dev;
9073         } else if (type == LU_SEQ_RANGE_MDT) {
9074                 ltd = &lod->lod_mdt_descs;
9075                 lod_getref(ltd);
9076         } else if (type == LU_SEQ_RANGE_OST) {
9077                 ltd = &lod->lod_ost_descs;
9078                 lod_getref(ltd);
9079         } else {
9080                 LBUG();
9081         }
9082
9083         if (ltd != NULL) {
9084                 if (ltd->ltd_tgts_size > idx &&
9085                     test_bit(idx, ltd->ltd_tgt_bitmap)) {
9086                         tgt = LTD_TGT(ltd, idx);
9087
9088                         LASSERT(tgt != NULL);
9089                         LASSERT(tgt->ltd_tgt != NULL);
9090
9091                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
9092                 }
9093                 lod_putref(lod, ltd);
9094         }
9095
9096         if (unlikely(cdev == NULL))
9097                 RETURN(-ENOENT);
9098
9099         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
9100         if (unlikely(cobj == NULL))
9101                 RETURN(-ENOMEM);
9102
9103         lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
9104
9105         lu_object_add(lo, cobj);
9106
9107         RETURN(0);
9108 }
9109
9110 /**
9111  *
9112  * Release resources associated with striping.
9113  *
9114  * If the object is striped (regular or directory), then release
9115  * the stripe objects references and free the ldo_stripe array.
9116  *
9117  * \param[in] env       execution environment
9118  * \param[in] lo        object
9119  */
9120 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
9121 {
9122         struct lod_layout_component *lod_comp;
9123         __u32 obj_attr = lo->ldo_obj.do_lu.lo_header->loh_attr;
9124         int i, j;
9125
9126         if (unlikely(lo->ldo_is_foreign)) {
9127                 if (S_ISREG(obj_attr)) {
9128                         lod_free_foreign_lov(lo);
9129                         lo->ldo_comp_cached = 0;
9130                 } else if (S_ISDIR(obj_attr)) {
9131                         lod_free_foreign_lmv(lo);
9132                         lo->ldo_dir_stripe_loaded = 0;
9133                 }
9134         } else if (lo->ldo_stripe != NULL) {
9135                 LASSERT(lo->ldo_comp_entries == NULL);
9136                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
9137
9138                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
9139                         if (lo->ldo_stripe[i])
9140                                 dt_object_put(env, lo->ldo_stripe[i]);
9141                 }
9142
9143                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
9144                 OBD_FREE(lo->ldo_stripe, j);
9145                 lo->ldo_stripe = NULL;
9146                 lo->ldo_dir_stripes_allocated = 0;
9147                 lo->ldo_dir_stripe_loaded = 0;
9148                 lo->ldo_dir_stripe_count = 0;
9149                 lo->ldo_obj.do_index_ops = NULL;
9150         } else if (lo->ldo_comp_entries != NULL) {
9151                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
9152                         /* free lod_layout_component::llc_stripe array */
9153                         lod_comp = &lo->ldo_comp_entries[i];
9154
9155                         if (lod_comp->llc_stripe == NULL)
9156                                 continue;
9157                         LASSERT(lod_comp->llc_stripes_allocated != 0);
9158                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
9159                                 if (lod_comp->llc_stripe[j] != NULL)
9160                                         lu_object_put(env,
9161                                                &lod_comp->llc_stripe[j]->do_lu);
9162                         }
9163                         OBD_FREE_PTR_ARRAY(lod_comp->llc_stripe,
9164                                            lod_comp->llc_stripes_allocated);
9165                         lod_comp->llc_stripe = NULL;
9166                         OBD_FREE_PTR_ARRAY(lod_comp->llc_ost_indices,
9167                                            lod_comp->llc_stripes_allocated);
9168                         lod_comp->llc_ost_indices = NULL;
9169                         lod_comp->llc_stripes_allocated = 0;
9170                 }
9171                 lod_free_comp_entries(lo);
9172                 lo->ldo_comp_cached = 0;
9173         }
9174 }
9175
9176 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
9177 {
9178         mutex_lock(&lo->ldo_layout_mutex);
9179         lod_striping_free_nolock(env, lo);
9180         mutex_unlock(&lo->ldo_layout_mutex);
9181 }
9182
9183 /**
9184  * Implementation of lu_object_operations::loo_object_free.
9185  *
9186  * \see lu_object_operations::loo_object_free() in the API description
9187  * for details.
9188  */
9189 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
9190 {
9191         struct lod_object *lo = lu2lod_obj(o);
9192
9193         /* release all underlying object pinned */
9194         lod_striping_free(env, lo);
9195         lu_object_fini(o);
9196         /* lo doesn't contain a lu_object_header, so we don't need call_rcu */
9197         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
9198 }
9199
9200 /**
9201  * Implementation of lu_object_operations::loo_object_release.
9202  *
9203  * \see lu_object_operations::loo_object_release() in the API description
9204  * for details.
9205  */
9206 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
9207 {
9208         /* XXX: shouldn't we release everything here in case if object
9209          * creation failed before? */
9210 }
9211
9212 /**
9213  * Implementation of lu_object_operations::loo_object_print.
9214  *
9215  * \see lu_object_operations::loo_object_print() in the API description
9216  * for details.
9217  */
9218 static int lod_object_print(const struct lu_env *env, void *cookie,
9219                             lu_printer_t p, const struct lu_object *l)
9220 {
9221         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
9222
9223         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
9224 }
9225
9226 const struct lu_object_operations lod_lu_obj_ops = {
9227         .loo_object_init        = lod_object_init,
9228         .loo_object_free        = lod_object_free,
9229         .loo_object_release     = lod_object_release,
9230         .loo_object_print       = lod_object_print,
9231 };