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