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