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