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