Whamcloud - gitweb
LU-10248 mdd: set PFID for swap and merge layout
[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, 2016, 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 <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46
47 #include <lustre_fid.h>
48 #include <lustre_linkea.h>
49 #include <lustre_lmv.h>
50 #include <uapi/linux/lustre/lustre_param.h>
51 #include <lustre_swab.h>
52 #include <uapi/linux/lustre/lustre_ver.h>
53 #include <lprocfs_status.h>
54 #include <md_object.h>
55
56 #include "lod_internal.h"
57
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
60
61 /**
62  * Implementation of dt_index_operations::dio_lookup
63  *
64  * Used with regular (non-striped) objects.
65  *
66  * \see dt_index_operations::dio_lookup() in the API description for details.
67  */
68 static int lod_lookup(const struct lu_env *env, struct dt_object *dt,
69                       struct dt_rec *rec, const struct dt_key *key)
70 {
71         struct dt_object *next = dt_object_child(dt);
72         return next->do_index_ops->dio_lookup(env, next, rec, key);
73 }
74
75 /**
76  * Implementation of dt_index_operations::dio_declare_insert.
77  *
78  * Used with regular (non-striped) objects.
79  *
80  * \see dt_index_operations::dio_declare_insert() in the API description
81  * for details.
82  */
83 static int lod_declare_insert(const struct lu_env *env, struct dt_object *dt,
84                               const struct dt_rec *rec,
85                               const struct dt_key *key, struct thandle *th)
86 {
87         return lod_sub_declare_insert(env, dt_object_child(dt), rec, key, th);
88 }
89
90 /**
91  * Implementation of dt_index_operations::dio_insert.
92  *
93  * Used with regular (non-striped) objects
94  *
95  * \see dt_index_operations::dio_insert() in the API description for details.
96  */
97 static int lod_insert(const struct lu_env *env, struct dt_object *dt,
98                       const struct dt_rec *rec, const struct dt_key *key,
99                       struct thandle *th, int ign)
100 {
101         return lod_sub_insert(env, dt_object_child(dt), rec, key, th, ign);
102 }
103
104 /**
105  * Implementation of dt_index_operations::dio_declare_delete.
106  *
107  * Used with regular (non-striped) objects.
108  *
109  * \see dt_index_operations::dio_declare_delete() in the API description
110  * for details.
111  */
112 static int lod_declare_delete(const struct lu_env *env, struct dt_object *dt,
113                               const struct dt_key *key, struct thandle *th)
114 {
115         return lod_sub_declare_delete(env, dt_object_child(dt), key, th);
116 }
117
118 /**
119  * Implementation of dt_index_operations::dio_delete.
120  *
121  * Used with regular (non-striped) objects.
122  *
123  * \see dt_index_operations::dio_delete() in the API description for details.
124  */
125 static int lod_delete(const struct lu_env *env, struct dt_object *dt,
126                       const struct dt_key *key, struct thandle *th)
127 {
128         return lod_sub_delete(env, dt_object_child(dt), key, th);
129 }
130
131 /**
132  * Implementation of dt_it_ops::init.
133  *
134  * Used with regular (non-striped) objects.
135  *
136  * \see dt_it_ops::init() in the API description for details.
137  */
138 static struct dt_it *lod_it_init(const struct lu_env *env,
139                                  struct dt_object *dt, __u32 attr)
140 {
141         struct dt_object        *next = dt_object_child(dt);
142         struct lod_it           *it = &lod_env_info(env)->lti_it;
143         struct dt_it            *it_next;
144
145         it_next = next->do_index_ops->dio_it.init(env, next, attr);
146         if (IS_ERR(it_next))
147                 return it_next;
148
149         /* currently we do not use more than one iterator per thread
150          * so we store it in thread info. if at some point we need
151          * more active iterators in a single thread, we can allocate
152          * additional ones */
153         LASSERT(it->lit_obj == NULL);
154
155         it->lit_it = it_next;
156         it->lit_obj = next;
157
158         return (struct dt_it *)it;
159 }
160
161 #define LOD_CHECK_IT(env, it)                                   \
162 do {                                                            \
163         LASSERT((it)->lit_obj != NULL);                         \
164         LASSERT((it)->lit_it != NULL);                          \
165 } while (0)
166
167 /**
168  * Implementation of dt_index_operations::dio_it.fini.
169  *
170  * Used with regular (non-striped) objects.
171  *
172  * \see dt_index_operations::dio_it.fini() in the API description for details.
173  */
174 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
175 {
176         struct lod_it *it = (struct lod_it *)di;
177
178         LOD_CHECK_IT(env, it);
179         it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
180
181         /* the iterator not in use any more */
182         it->lit_obj = NULL;
183         it->lit_it = NULL;
184 }
185
186 /**
187  * Implementation of dt_it_ops::get.
188  *
189  * Used with regular (non-striped) objects.
190  *
191  * \see dt_it_ops::get() in the API description for details.
192  */
193 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
194                       const struct dt_key *key)
195 {
196         const struct lod_it *it = (const struct lod_it *)di;
197
198         LOD_CHECK_IT(env, it);
199         return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
200 }
201
202 /**
203  * Implementation of dt_it_ops::put.
204  *
205  * Used with regular (non-striped) objects.
206  *
207  * \see dt_it_ops::put() in the API description for details.
208  */
209 static void lod_it_put(const struct lu_env *env, struct dt_it *di)
210 {
211         struct lod_it *it = (struct lod_it *)di;
212
213         LOD_CHECK_IT(env, it);
214         return it->lit_obj->do_index_ops->dio_it.put(env, it->lit_it);
215 }
216
217 /**
218  * Implementation of dt_it_ops::next.
219  *
220  * Used with regular (non-striped) objects
221  *
222  * \see dt_it_ops::next() in the API description for details.
223  */
224 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
225 {
226         struct lod_it *it = (struct lod_it *)di;
227
228         LOD_CHECK_IT(env, it);
229         return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
230 }
231
232 /**
233  * Implementation of dt_it_ops::key.
234  *
235  * Used with regular (non-striped) objects.
236  *
237  * \see dt_it_ops::key() in the API description for details.
238  */
239 static struct dt_key *lod_it_key(const struct lu_env *env,
240                                  const struct dt_it *di)
241 {
242         const struct lod_it *it = (const struct lod_it *)di;
243
244         LOD_CHECK_IT(env, it);
245         return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
246 }
247
248 /**
249  * Implementation of dt_it_ops::key_size.
250  *
251  * Used with regular (non-striped) objects.
252  *
253  * \see dt_it_ops::key_size() in the API description for details.
254  */
255 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
256 {
257         struct lod_it *it = (struct lod_it *)di;
258
259         LOD_CHECK_IT(env, it);
260         return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
261 }
262
263 /**
264  * Implementation of dt_it_ops::rec.
265  *
266  * Used with regular (non-striped) objects.
267  *
268  * \see dt_it_ops::rec() in the API description for details.
269  */
270 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
271                       struct dt_rec *rec, __u32 attr)
272 {
273         const struct lod_it *it = (const struct lod_it *)di;
274
275         LOD_CHECK_IT(env, it);
276         return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
277                                                      attr);
278 }
279
280 /**
281  * Implementation of dt_it_ops::rec_size.
282  *
283  * Used with regular (non-striped) objects.
284  *
285  * \see dt_it_ops::rec_size() in the API description for details.
286  */
287 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
288                            __u32 attr)
289 {
290         const struct lod_it *it = (const struct lod_it *)di;
291
292         LOD_CHECK_IT(env, it);
293         return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
294                                                           attr);
295 }
296
297 /**
298  * Implementation of dt_it_ops::store.
299  *
300  * Used with regular (non-striped) objects.
301  *
302  * \see dt_it_ops::store() in the API description for details.
303  */
304 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
305 {
306         const struct lod_it *it = (const struct lod_it *)di;
307
308         LOD_CHECK_IT(env, it);
309         return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
310 }
311
312 /**
313  * Implementation of dt_it_ops::load.
314  *
315  * Used with regular (non-striped) objects.
316  *
317  * \see dt_it_ops::load() in the API description for details.
318  */
319 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
320                        __u64 hash)
321 {
322         const struct lod_it *it = (const struct lod_it *)di;
323
324         LOD_CHECK_IT(env, it);
325         return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
326 }
327
328 /**
329  * Implementation of dt_it_ops::key_rec.
330  *
331  * Used with regular (non-striped) objects.
332  *
333  * \see dt_it_ops::rec() in the API description for details.
334  */
335 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
336                           void *key_rec)
337 {
338         const struct lod_it *it = (const struct lod_it *)di;
339
340         LOD_CHECK_IT(env, it);
341         return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
342                                                          key_rec);
343 }
344
345 static struct dt_index_operations lod_index_ops = {
346         .dio_lookup             = lod_lookup,
347         .dio_declare_insert     = lod_declare_insert,
348         .dio_insert             = lod_insert,
349         .dio_declare_delete     = lod_declare_delete,
350         .dio_delete             = lod_delete,
351         .dio_it = {
352                 .init           = lod_it_init,
353                 .fini           = lod_it_fini,
354                 .get            = lod_it_get,
355                 .put            = lod_it_put,
356                 .next           = lod_it_next,
357                 .key            = lod_it_key,
358                 .key_size       = lod_it_key_size,
359                 .rec            = lod_it_rec,
360                 .rec_size       = lod_it_rec_size,
361                 .store          = lod_it_store,
362                 .load           = lod_it_load,
363                 .key_rec        = lod_it_key_rec,
364         }
365 };
366
367 /**
368  * Implementation of dt_it_ops::init.
369  *
370  * Used with striped objects. Internally just initializes the iterator
371  * on the first stripe.
372  *
373  * \see dt_it_ops::init() in the API description for details.
374  */
375 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
376                                          struct dt_object *dt, __u32 attr)
377 {
378         struct lod_object       *lo = lod_dt_obj(dt);
379         struct dt_object        *next;
380         struct lod_it           *it = &lod_env_info(env)->lti_it;
381         struct dt_it            *it_next;
382         ENTRY;
383
384         LASSERT(lo->ldo_dir_stripe_count > 0);
385         next = lo->ldo_stripe[0];
386         LASSERT(next != NULL);
387         LASSERT(next->do_index_ops != NULL);
388
389         it_next = next->do_index_ops->dio_it.init(env, next, attr);
390         if (IS_ERR(it_next))
391                 return it_next;
392
393         /* currently we do not use more than one iterator per thread
394          * so we store it in thread info. if at some point we need
395          * more active iterators in a single thread, we can allocate
396          * additional ones */
397         LASSERT(it->lit_obj == NULL);
398
399         it->lit_stripe_index = 0;
400         it->lit_attr = attr;
401         it->lit_it = it_next;
402         it->lit_obj = dt;
403
404         return (struct dt_it *)it;
405 }
406
407 #define LOD_CHECK_STRIPED_IT(env, it, lo)                               \
408 do {                                                                    \
409         LASSERT((it)->lit_obj != NULL);                                 \
410         LASSERT((it)->lit_it != NULL);                                  \
411         LASSERT((lo)->ldo_dir_stripe_count > 0);                        \
412         LASSERT((it)->lit_stripe_index < (lo)->ldo_dir_stripe_count);   \
413 } while (0)
414
415 /**
416  * Implementation of dt_it_ops::fini.
417  *
418  * Used with striped objects.
419  *
420  * \see dt_it_ops::fini() in the API description for details.
421  */
422 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
423 {
424         struct lod_it           *it = (struct lod_it *)di;
425         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
426         struct dt_object        *next;
427
428         /* If lit_it == NULL, then it means the sub_it has been finished,
429          * which only happens in failure cases, see lod_striped_it_next() */
430         if (it->lit_it != NULL) {
431                 LOD_CHECK_STRIPED_IT(env, it, lo);
432
433                 next = lo->ldo_stripe[it->lit_stripe_index];
434                 LASSERT(next != NULL);
435                 LASSERT(next->do_index_ops != NULL);
436
437                 next->do_index_ops->dio_it.fini(env, it->lit_it);
438         }
439
440         /* the iterator not in use any more */
441         it->lit_obj = NULL;
442         it->lit_it = NULL;
443         it->lit_stripe_index = 0;
444 }
445
446 /**
447  * Implementation of dt_it_ops::get.
448  *
449  * Right now it's not used widely, only to reset the iterator to the
450  * initial position. It should be possible to implement a full version
451  * which chooses a correct stripe to be able to position with any key.
452  *
453  * \see dt_it_ops::get() in the API description for details.
454  */
455 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
456                               const struct dt_key *key)
457 {
458         const struct lod_it     *it = (const struct lod_it *)di;
459         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
460         struct dt_object        *next;
461         ENTRY;
462
463         LOD_CHECK_STRIPED_IT(env, it, lo);
464
465         next = lo->ldo_stripe[it->lit_stripe_index];
466         LASSERT(next != NULL);
467         LASSERT(next->do_index_ops != NULL);
468
469         return next->do_index_ops->dio_it.get(env, it->lit_it, key);
470 }
471
472 /**
473  * Implementation of dt_it_ops::put.
474  *
475  * Used with striped objects.
476  *
477  * \see dt_it_ops::put() in the API description for details.
478  */
479 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
480 {
481         struct lod_it           *it = (struct lod_it *)di;
482         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
483         struct dt_object        *next;
484
485         LOD_CHECK_STRIPED_IT(env, it, lo);
486
487         next = lo->ldo_stripe[it->lit_stripe_index];
488         LASSERT(next != NULL);
489         LASSERT(next->do_index_ops != NULL);
490
491         return next->do_index_ops->dio_it.put(env, it->lit_it);
492 }
493
494 /**
495  * Implementation of dt_it_ops::next.
496  *
497  * Used with striped objects. When the end of the current stripe is
498  * reached, the method takes the next stripe's iterator.
499  *
500  * \see dt_it_ops::next() in the API description for details.
501  */
502 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
503 {
504         struct lod_it           *it = (struct lod_it *)di;
505         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
506         struct dt_object        *next;
507         struct dt_it            *it_next;
508         int                     rc;
509         ENTRY;
510
511         LOD_CHECK_STRIPED_IT(env, it, lo);
512
513         next = lo->ldo_stripe[it->lit_stripe_index];
514         LASSERT(next != NULL);
515         LASSERT(next->do_index_ops != NULL);
516 again:
517         rc = next->do_index_ops->dio_it.next(env, it->lit_it);
518         if (rc < 0)
519                 RETURN(rc);
520
521         if (rc == 0 && it->lit_stripe_index == 0)
522                 RETURN(rc);
523
524         if (rc == 0 && it->lit_stripe_index > 0) {
525                 struct lu_dirent *ent;
526
527                 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
528
529                 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
530                                                     (struct dt_rec *)ent,
531                                                     it->lit_attr);
532                 if (rc != 0)
533                         RETURN(rc);
534
535                 /* skip . and .. for slave stripe */
536                 if ((strncmp(ent->lde_name, ".",
537                              le16_to_cpu(ent->lde_namelen)) == 0 &&
538                      le16_to_cpu(ent->lde_namelen) == 1) ||
539                     (strncmp(ent->lde_name, "..",
540                              le16_to_cpu(ent->lde_namelen)) == 0 &&
541                      le16_to_cpu(ent->lde_namelen) == 2))
542                         goto again;
543
544                 RETURN(rc);
545         }
546
547         /* go to next stripe */
548         if (it->lit_stripe_index + 1 >= lo->ldo_dir_stripe_count)
549                 RETURN(1);
550
551         it->lit_stripe_index++;
552
553         next->do_index_ops->dio_it.put(env, it->lit_it);
554         next->do_index_ops->dio_it.fini(env, it->lit_it);
555         it->lit_it = NULL;
556
557         next = lo->ldo_stripe[it->lit_stripe_index];
558         LASSERT(next != NULL);
559         rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
560         if (rc != 0)
561                 RETURN(rc);
562
563         LASSERT(next->do_index_ops != NULL);
564
565         it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
566         if (!IS_ERR(it_next)) {
567                 it->lit_it = it_next;
568                 goto again;
569         } else {
570                 rc = PTR_ERR(it_next);
571         }
572
573         RETURN(rc);
574 }
575
576 /**
577  * Implementation of dt_it_ops::key.
578  *
579  * Used with striped objects.
580  *
581  * \see dt_it_ops::key() in the API description for details.
582  */
583 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
584                                          const struct dt_it *di)
585 {
586         const struct lod_it     *it = (const struct lod_it *)di;
587         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
588         struct dt_object        *next;
589
590         LOD_CHECK_STRIPED_IT(env, it, lo);
591
592         next = lo->ldo_stripe[it->lit_stripe_index];
593         LASSERT(next != NULL);
594         LASSERT(next->do_index_ops != NULL);
595
596         return next->do_index_ops->dio_it.key(env, it->lit_it);
597 }
598
599 /**
600  * Implementation of dt_it_ops::key_size.
601  *
602  * Used with striped objects.
603  *
604  * \see dt_it_ops::size() in the API description for details.
605  */
606 static int lod_striped_it_key_size(const struct lu_env *env,
607                                    const struct dt_it *di)
608 {
609         struct lod_it           *it = (struct lod_it *)di;
610         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
611         struct dt_object        *next;
612
613         LOD_CHECK_STRIPED_IT(env, it, lo);
614
615         next = lo->ldo_stripe[it->lit_stripe_index];
616         LASSERT(next != NULL);
617         LASSERT(next->do_index_ops != NULL);
618
619         return next->do_index_ops->dio_it.key_size(env, it->lit_it);
620 }
621
622 /**
623  * Implementation of dt_it_ops::rec.
624  *
625  * Used with striped objects.
626  *
627  * \see dt_it_ops::rec() in the API description for details.
628  */
629 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
630                               struct dt_rec *rec, __u32 attr)
631 {
632         const struct lod_it     *it = (const struct lod_it *)di;
633         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
634         struct dt_object        *next;
635
636         LOD_CHECK_STRIPED_IT(env, it, lo);
637
638         next = lo->ldo_stripe[it->lit_stripe_index];
639         LASSERT(next != NULL);
640         LASSERT(next->do_index_ops != NULL);
641
642         return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
643 }
644
645 /**
646  * Implementation of dt_it_ops::rec_size.
647  *
648  * Used with striped objects.
649  *
650  * \see dt_it_ops::rec_size() in the API description for details.
651  */
652 static int lod_striped_it_rec_size(const struct lu_env *env,
653                                    const struct dt_it *di, __u32 attr)
654 {
655         struct lod_it           *it = (struct lod_it *)di;
656         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
657         struct dt_object        *next;
658
659         LOD_CHECK_STRIPED_IT(env, it, lo);
660
661         next = lo->ldo_stripe[it->lit_stripe_index];
662         LASSERT(next != NULL);
663         LASSERT(next->do_index_ops != NULL);
664
665         return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
666 }
667
668 /**
669  * Implementation of dt_it_ops::store.
670  *
671  * Used with striped objects.
672  *
673  * \see dt_it_ops::store() in the API description for details.
674  */
675 static __u64 lod_striped_it_store(const struct lu_env *env,
676                                   const struct dt_it *di)
677 {
678         const struct lod_it     *it = (const struct lod_it *)di;
679         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
680         struct dt_object        *next;
681
682         LOD_CHECK_STRIPED_IT(env, it, lo);
683
684         next = lo->ldo_stripe[it->lit_stripe_index];
685         LASSERT(next != NULL);
686         LASSERT(next->do_index_ops != NULL);
687
688         return next->do_index_ops->dio_it.store(env, it->lit_it);
689 }
690
691 /**
692  * Implementation of dt_it_ops::load.
693  *
694  * Used with striped objects.
695  *
696  * \see dt_it_ops::load() in the API description for details.
697  */
698 static int lod_striped_it_load(const struct lu_env *env,
699                                const struct dt_it *di, __u64 hash)
700 {
701         const struct lod_it     *it = (const struct lod_it *)di;
702         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
703         struct dt_object        *next;
704
705         LOD_CHECK_STRIPED_IT(env, it, lo);
706
707         next = lo->ldo_stripe[it->lit_stripe_index];
708         LASSERT(next != NULL);
709         LASSERT(next->do_index_ops != NULL);
710
711         return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
712 }
713
714 static struct dt_index_operations lod_striped_index_ops = {
715         .dio_lookup             = lod_lookup,
716         .dio_declare_insert     = lod_declare_insert,
717         .dio_insert             = lod_insert,
718         .dio_declare_delete     = lod_declare_delete,
719         .dio_delete             = lod_delete,
720         .dio_it = {
721                 .init           = lod_striped_it_init,
722                 .fini           = lod_striped_it_fini,
723                 .get            = lod_striped_it_get,
724                 .put            = lod_striped_it_put,
725                 .next           = lod_striped_it_next,
726                 .key            = lod_striped_it_key,
727                 .key_size       = lod_striped_it_key_size,
728                 .rec            = lod_striped_it_rec,
729                 .rec_size       = lod_striped_it_rec_size,
730                 .store          = lod_striped_it_store,
731                 .load           = lod_striped_it_load,
732         }
733 };
734
735 /**
736  * Append the FID for each shard of the striped directory after the
737  * given LMV EA header.
738  *
739  * To simplify striped directory and the consistency verification,
740  * we only store the LMV EA header on disk, for both master object
741  * and slave objects. When someone wants to know the whole LMV EA,
742  * such as client readdir(), we can build the entrie LMV EA on the
743  * MDT side (in RAM) via iterating the sub-directory entries that
744  * are contained in the master object of the stripe directory.
745  *
746  * For the master object of the striped directroy, the valid name
747  * for each shard is composed of the ${shard_FID}:${shard_idx}.
748  *
749  * There may be holes in the LMV EA if some shards' name entries
750  * are corrupted or lost.
751  *
752  * \param[in] env       pointer to the thread context
753  * \param[in] lo        pointer to the master object of the striped directory
754  * \param[in] buf       pointer to the lu_buf which will hold the LMV EA
755  * \param[in] resize    whether re-allocate the buffer if it is not big enough
756  *
757  * \retval              positive size of the LMV EA
758  * \retval              0 for nothing to be loaded
759  * \retval              negative error number on failure
760  */
761 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
762                         struct lu_buf *buf, bool resize)
763 {
764         struct lu_dirent        *ent    =
765                         (struct lu_dirent *)lod_env_info(env)->lti_key;
766         struct lod_device       *lod    = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
767         struct dt_object        *obj    = dt_object_child(&lo->ldo_obj);
768         struct lmv_mds_md_v1    *lmv1   = buf->lb_buf;
769         struct dt_it            *it;
770         const struct dt_it_ops  *iops;
771         __u32                    stripes;
772         __u32                    magic  = le32_to_cpu(lmv1->lmv_magic);
773         size_t                   lmv1_size;
774         int                      rc;
775         ENTRY;
776
777         /* If it is not a striped directory, then load nothing. */
778         if (magic != LMV_MAGIC_V1)
779                 RETURN(0);
780
781         /* If it is in migration (or failure), then load nothing. */
782         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
783                 RETURN(0);
784
785         stripes = le32_to_cpu(lmv1->lmv_stripe_count);
786         if (stripes < 1)
787                 RETURN(0);
788
789         rc = lmv_mds_md_size(stripes, magic);
790         if (rc < 0)
791                 RETURN(rc);
792         lmv1_size = rc;
793         if (buf->lb_len < lmv1_size) {
794                 struct lu_buf tbuf;
795
796                 if (!resize)
797                         RETURN(-ERANGE);
798
799                 tbuf = *buf;
800                 buf->lb_buf = NULL;
801                 buf->lb_len = 0;
802                 lu_buf_alloc(buf, lmv1_size);
803                 lmv1 = buf->lb_buf;
804                 if (lmv1 == NULL)
805                         RETURN(-ENOMEM);
806
807                 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
808         }
809
810         if (unlikely(!dt_try_as_dir(env, obj)))
811                 RETURN(-ENOTDIR);
812
813         memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
814         iops = &obj->do_index_ops->dio_it;
815         it = iops->init(env, obj, LUDA_64BITHASH);
816         if (IS_ERR(it))
817                 RETURN(PTR_ERR(it));
818
819         rc = iops->load(env, it, 0);
820         if (rc == 0)
821                 rc = iops->next(env, it);
822         else if (rc > 0)
823                 rc = 0;
824
825         while (rc == 0) {
826                 char             name[FID_LEN + 2] = "";
827                 struct lu_fid    fid;
828                 __u32            index;
829                 int              len;
830
831                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
832                 if (rc != 0)
833                         break;
834
835                 rc = -EIO;
836
837                 fid_le_to_cpu(&fid, &ent->lde_fid);
838                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
839                 if (ent->lde_name[0] == '.') {
840                         if (ent->lde_namelen == 1)
841                                 goto next;
842
843                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
844                                 goto next;
845                 }
846
847                 len = snprintf(name, sizeof(name),
848                                DFID":", PFID(&ent->lde_fid));
849                 /* The ent->lde_name is composed of ${FID}:${index} */
850                 if (ent->lde_namelen < len + 1 ||
851                     memcmp(ent->lde_name, name, len) != 0) {
852                         CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
853                                "%s: invalid shard name %.*s with the FID "DFID
854                                " for the striped directory "DFID", %s\n",
855                                lod2obd(lod)->obd_name, ent->lde_namelen,
856                                ent->lde_name, PFID(&fid),
857                                PFID(lu_object_fid(&obj->do_lu)),
858                                lod->lod_lmv_failout ? "failout" : "skip");
859
860                         if (lod->lod_lmv_failout)
861                                 break;
862
863                         goto next;
864                 }
865
866                 index = 0;
867                 do {
868                         if (ent->lde_name[len] < '0' ||
869                             ent->lde_name[len] > '9') {
870                                 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
871                                        "%s: invalid shard name %.*s with the "
872                                        "FID "DFID" for the striped directory "
873                                        DFID", %s\n",
874                                        lod2obd(lod)->obd_name, ent->lde_namelen,
875                                        ent->lde_name, PFID(&fid),
876                                        PFID(lu_object_fid(&obj->do_lu)),
877                                        lod->lod_lmv_failout ?
878                                        "failout" : "skip");
879
880                                 if (lod->lod_lmv_failout)
881                                         break;
882
883                                 goto next;
884                         }
885
886                         index = index * 10 + ent->lde_name[len++] - '0';
887                 } while (len < ent->lde_namelen);
888
889                 if (len == ent->lde_namelen) {
890                         /* Out of LMV EA range. */
891                         if (index >= stripes) {
892                                 CERROR("%s: the shard %.*s for the striped "
893                                        "directory "DFID" is out of the known "
894                                        "LMV EA range [0 - %u], failout\n",
895                                        lod2obd(lod)->obd_name, ent->lde_namelen,
896                                        ent->lde_name,
897                                        PFID(lu_object_fid(&obj->do_lu)),
898                                        stripes - 1);
899
900                                 break;
901                         }
902
903                         /* The slot has been occupied. */
904                         if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
905                                 struct lu_fid fid0;
906
907                                 fid_le_to_cpu(&fid0,
908                                         &lmv1->lmv_stripe_fids[index]);
909                                 CERROR("%s: both the shard "DFID" and "DFID
910                                        " for the striped directory "DFID
911                                        " claim the same LMV EA slot at the "
912                                        "index %d, failout\n",
913                                        lod2obd(lod)->obd_name,
914                                        PFID(&fid0), PFID(&fid),
915                                        PFID(lu_object_fid(&obj->do_lu)), index);
916
917                                 break;
918                         }
919
920                         /* stored as LE mode */
921                         lmv1->lmv_stripe_fids[index] = ent->lde_fid;
922
923 next:
924                         rc = iops->next(env, it);
925                 }
926         }
927
928         iops->put(env, it);
929         iops->fini(env, it);
930
931         RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
932 }
933
934 /**
935  * Implementation of dt_object_operations::do_index_try.
936  *
937  * \see dt_object_operations::do_index_try() in the API description for details.
938  */
939 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
940                          const struct dt_index_features *feat)
941 {
942         struct lod_object       *lo = lod_dt_obj(dt);
943         struct dt_object        *next = dt_object_child(dt);
944         int                     rc;
945         ENTRY;
946
947         LASSERT(next->do_ops);
948         LASSERT(next->do_ops->do_index_try);
949
950         rc = lod_load_striping_locked(env, lo);
951         if (rc != 0)
952                 RETURN(rc);
953
954         rc = next->do_ops->do_index_try(env, next, feat);
955         if (rc != 0)
956                 RETURN(rc);
957
958         if (lo->ldo_dir_stripe_count > 0) {
959                 int i;
960
961                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
962                         if (dt_object_exists(lo->ldo_stripe[i]) == 0)
963                                 continue;
964                         rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
965                                                 lo->ldo_stripe[i], feat);
966                         if (rc != 0)
967                                 RETURN(rc);
968                 }
969                 dt->do_index_ops = &lod_striped_index_ops;
970         } else {
971                 dt->do_index_ops = &lod_index_ops;
972         }
973
974         RETURN(rc);
975 }
976
977 /**
978  * Implementation of dt_object_operations::do_read_lock.
979  *
980  * \see dt_object_operations::do_read_lock() in the API description for details.
981  */
982 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
983                           unsigned role)
984 {
985         dt_read_lock(env, dt_object_child(dt), role);
986 }
987
988 /**
989  * Implementation of dt_object_operations::do_write_lock.
990  *
991  * \see dt_object_operations::do_write_lock() in the API description for
992  * details.
993  */
994 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
995                            unsigned role)
996 {
997         dt_write_lock(env, dt_object_child(dt), role);
998 }
999
1000 /**
1001  * Implementation of dt_object_operations::do_read_unlock.
1002  *
1003  * \see dt_object_operations::do_read_unlock() in the API description for
1004  * details.
1005  */
1006 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1007 {
1008         dt_read_unlock(env, dt_object_child(dt));
1009 }
1010
1011 /**
1012  * Implementation of dt_object_operations::do_write_unlock.
1013  *
1014  * \see dt_object_operations::do_write_unlock() in the API description for
1015  * details.
1016  */
1017 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1018 {
1019         dt_write_unlock(env, dt_object_child(dt));
1020 }
1021
1022 /**
1023  * Implementation of dt_object_operations::do_write_locked.
1024  *
1025  * \see dt_object_operations::do_write_locked() in the API description for
1026  * details.
1027  */
1028 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1029 {
1030         return dt_write_locked(env, dt_object_child(dt));
1031 }
1032
1033 /**
1034  * Implementation of dt_object_operations::do_attr_get.
1035  *
1036  * \see dt_object_operations::do_attr_get() in the API description for details.
1037  */
1038 static int lod_attr_get(const struct lu_env *env,
1039                         struct dt_object *dt,
1040                         struct lu_attr *attr)
1041 {
1042         /* Note: for striped directory, client will merge attributes
1043          * from all of the sub-stripes see lmv_merge_attr(), and there
1044          * no MDD logic depend on directory nlink/size/time, so we can
1045          * always use master inode nlink and size for now. */
1046         return dt_attr_get(env, dt_object_child(dt), attr);
1047 }
1048
1049 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1050                             struct thandle *th,
1051                             struct lod_obj_stripe_cb_data *data)
1052 {
1053         struct lod_layout_component *lod_comp;
1054         int i, j, rc;
1055         ENTRY;
1056
1057         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1058         for (i = 0; i < lo->ldo_comp_cnt; i++) {
1059                 lod_comp = &lo->ldo_comp_entries[i];
1060
1061                 if (lod_comp->llc_stripe == NULL)
1062                         continue;
1063
1064                 /* has stripe but not inited yet, this component has been
1065                  * declared to be created, but hasn't created yet.
1066                  */
1067                 if (!lod_comp_inited(lod_comp))
1068                         continue;
1069
1070                 if (data->locd_comp_skip_cb &&
1071                     data->locd_comp_skip_cb(env, lo, i, data))
1072                         continue;
1073
1074                 LASSERT(lod_comp->llc_stripe_count > 0);
1075                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1076                         struct dt_object *dt = lod_comp->llc_stripe[j];
1077
1078                         if (dt == NULL)
1079                                 continue;
1080                         rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1081                         if (rc != 0)
1082                                 RETURN(rc);
1083                 }
1084         }
1085         RETURN(0);
1086 }
1087
1088 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1089                 struct lod_object *lo, int comp_idx,
1090                 struct lod_obj_stripe_cb_data *data)
1091 {
1092         struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1093         bool skipped = false;
1094
1095         if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1096                 return skipped;
1097
1098         switch (lo->ldo_flr_state) {
1099         case LCM_FL_WRITE_PENDING: {
1100                 int i;
1101
1102                 /* skip stale components */
1103                 if (lod_comp->llc_flags & LCME_FL_STALE) {
1104                         skipped = true;
1105                         break;
1106                 }
1107
1108                 /* skip valid and overlapping components, therefore any
1109                  * attempts to write overlapped components will never succeed
1110                  * because client will get EINPROGRESS. */
1111                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1112                         if (i == comp_idx)
1113                                 continue;
1114
1115                         if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1116                                 continue;
1117
1118                         if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1119                                         &lo->ldo_comp_entries[i].llc_extent)) {
1120                                 skipped = true;
1121                                 break;
1122                         }
1123                 }
1124                 break;
1125         }
1126         default:
1127                 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1128         case LCM_FL_SYNC_PENDING:
1129                 break;
1130         }
1131
1132         CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1133                PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1134                skipped ? "skipped" : "chose", lod_comp->llc_id,
1135                data->locd_attr->la_layout_version);
1136
1137         return skipped;
1138 }
1139
1140 static inline int
1141 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1142                            struct dt_object *dt, struct thandle *th,
1143                            int comp_idx, int stripe_idx,
1144                            struct lod_obj_stripe_cb_data *data)
1145 {
1146         if (data->locd_declare)
1147                 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1148
1149         if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1150                 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1151                        PFID(lu_object_fid(&dt->do_lu)),
1152                        data->locd_attr->la_layout_version, comp_idx);
1153         }
1154
1155         return lod_sub_attr_set(env, dt, data->locd_attr, th);
1156 }
1157
1158 /**
1159  * Implementation of dt_object_operations::do_declare_attr_set.
1160  *
1161  * If the object is striped, then apply the changes to all the stripes.
1162  *
1163  * \see dt_object_operations::do_declare_attr_set() in the API description
1164  * for details.
1165  */
1166 static int lod_declare_attr_set(const struct lu_env *env,
1167                                 struct dt_object *dt,
1168                                 const struct lu_attr *attr,
1169                                 struct thandle *th)
1170 {
1171         struct dt_object  *next = dt_object_child(dt);
1172         struct lod_object *lo = lod_dt_obj(dt);
1173         int                rc, i;
1174         ENTRY;
1175
1176         /*
1177          * declare setattr on the local object
1178          */
1179         rc = lod_sub_declare_attr_set(env, next, attr, th);
1180         if (rc)
1181                 RETURN(rc);
1182
1183         /* osp_declare_attr_set() ignores all attributes other than
1184          * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1185          * but UID, GID and PROJID. Declaration of size attr setting
1186          * happens through lod_declare_init_size(), and not through
1187          * this function. Therefore we need not load striping unless
1188          * ownership is changing.  This should save memory and (we hope)
1189          * speed up rename().
1190          */
1191         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1192                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1193                         RETURN(rc);
1194
1195                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1196                         RETURN(0);
1197         } else {
1198                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1199                                         LA_ATIME | LA_MTIME | LA_CTIME |
1200                                         LA_FLAGS)))
1201                         RETURN(rc);
1202         }
1203         /*
1204          * load striping information, notice we don't do this when object
1205          * is being initialized as we don't need this information till
1206          * few specific cases like destroy, chown
1207          */
1208         rc = lod_load_striping(env, lo);
1209         if (rc)
1210                 RETURN(rc);
1211
1212         if (!lod_obj_is_striped(dt))
1213                 RETURN(0);
1214
1215         /*
1216          * if object is striped declare changes on the stripes
1217          */
1218         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1219                 LASSERT(lo->ldo_stripe);
1220                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1221                         if (lo->ldo_stripe[i] == NULL)
1222                                 continue;
1223                         rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1224                                                       attr, th);
1225                         if (rc != 0)
1226                                 RETURN(rc);
1227                 }
1228         } else {
1229                 struct lod_obj_stripe_cb_data data = { { 0 } };
1230
1231                 data.locd_attr = attr;
1232                 data.locd_declare = true;
1233                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1234                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1235         }
1236
1237         if (rc)
1238                 RETURN(rc);
1239
1240         if (!dt_object_exists(next) || dt_object_remote(next) ||
1241             !S_ISREG(attr->la_mode))
1242                 RETURN(0);
1243
1244         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1245                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1246                 RETURN(rc);
1247         }
1248
1249         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1250             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1251                 struct lod_thread_info *info = lod_env_info(env);
1252                 struct lu_buf *buf = &info->lti_buf;
1253
1254                 buf->lb_buf = info->lti_ea_store;
1255                 buf->lb_len = info->lti_ea_store_size;
1256                 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1257                                                LU_XATTR_REPLACE, th);
1258         }
1259
1260         RETURN(rc);
1261 }
1262
1263 /**
1264  * Implementation of dt_object_operations::do_attr_set.
1265  *
1266  * If the object is striped, then apply the changes to all or subset of
1267  * the stripes depending on the object type and specific attributes.
1268  *
1269  * \see dt_object_operations::do_attr_set() in the API description for details.
1270  */
1271 static int lod_attr_set(const struct lu_env *env,
1272                         struct dt_object *dt,
1273                         const struct lu_attr *attr,
1274                         struct thandle *th)
1275 {
1276         struct dt_object        *next = dt_object_child(dt);
1277         struct lod_object       *lo = lod_dt_obj(dt);
1278         int                     rc, i;
1279         ENTRY;
1280
1281         /*
1282          * apply changes to the local object
1283          */
1284         rc = lod_sub_attr_set(env, next, attr, th);
1285         if (rc)
1286                 RETURN(rc);
1287
1288         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1289                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1290                         RETURN(rc);
1291
1292                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1293                         RETURN(0);
1294         } else {
1295                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1296                                         LA_ATIME | LA_MTIME | LA_CTIME |
1297                                         LA_FLAGS)))
1298                         RETURN(rc);
1299         }
1300
1301         /* FIXME: a tricky case in the code path of mdd_layout_change():
1302          * the in-memory striping information has been freed in lod_xattr_set()
1303          * due to layout change. It has to load stripe here again. It only
1304          * changes flags of layout so declare_attr_set() is still accurate */
1305         rc = lod_load_striping_locked(env, lo);
1306         if (rc)
1307                 RETURN(rc);
1308
1309         if (!lod_obj_is_striped(dt))
1310                 RETURN(0);
1311
1312         /*
1313          * if object is striped, apply changes to all the stripes
1314          */
1315         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1316                 LASSERT(lo->ldo_stripe);
1317                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1318                         if (unlikely(lo->ldo_stripe[i] == NULL))
1319                                 continue;
1320
1321                         if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1322                                 continue;
1323
1324                         rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1325                         if (rc != 0)
1326                                 break;
1327                 }
1328         } else {
1329                 struct lod_obj_stripe_cb_data data = { { 0 } };
1330
1331                 data.locd_attr = attr;
1332                 data.locd_declare = false;
1333                 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1334                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1335                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1336         }
1337
1338         if (rc)
1339                 RETURN(rc);
1340
1341         if (!dt_object_exists(next) || dt_object_remote(next) ||
1342             !S_ISREG(attr->la_mode))
1343                 RETURN(0);
1344
1345         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1346                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1347                 RETURN(rc);
1348         }
1349
1350         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1351                 struct lod_thread_info *info = lod_env_info(env);
1352                 struct lu_buf *buf = &info->lti_buf;
1353                 struct ost_id *oi = &info->lti_ostid;
1354                 struct lu_fid *fid = &info->lti_fid;
1355                 struct lov_mds_md_v1 *lmm;
1356                 struct lov_ost_data_v1 *objs;
1357                 __u32 magic;
1358
1359                 rc = lod_get_lov_ea(env, lo);
1360                 if (rc <= 0)
1361                         RETURN(rc);
1362
1363                 buf->lb_buf = info->lti_ea_store;
1364                 buf->lb_len = info->lti_ea_store_size;
1365                 lmm = info->lti_ea_store;
1366                 magic = le32_to_cpu(lmm->lmm_magic);
1367                 if (magic == LOV_MAGIC_COMP_V1) {
1368                         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1369                         struct lov_comp_md_entry_v1 *lcme =
1370                                                 &lcm->lcm_entries[0];
1371
1372                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1373                         magic = le32_to_cpu(lmm->lmm_magic);
1374                 }
1375
1376                 if (magic == LOV_MAGIC_V1)
1377                         objs = &(lmm->lmm_objects[0]);
1378                 else
1379                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1380                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1381                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1382                 fid->f_oid--;
1383                 fid_to_ostid(fid, oi);
1384                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1385
1386                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1387                                        LU_XATTR_REPLACE, th);
1388         } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1389                 struct lod_thread_info *info = lod_env_info(env);
1390                 struct lu_buf *buf = &info->lti_buf;
1391                 struct lov_comp_md_v1 *lcm;
1392                 struct lov_comp_md_entry_v1 *lcme;
1393
1394                 rc = lod_get_lov_ea(env, lo);
1395                 if (rc <= 0)
1396                         RETURN(rc);
1397
1398                 buf->lb_buf = info->lti_ea_store;
1399                 buf->lb_len = info->lti_ea_store_size;
1400                 lcm = buf->lb_buf;
1401                 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1402                         RETURN(-EINVAL);
1403
1404                 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1405                 lcme = &lcm->lcm_entries[0];
1406                 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1407                 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1408
1409                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1410                                        LU_XATTR_REPLACE, th);
1411         }
1412
1413         RETURN(rc);
1414 }
1415
1416 /**
1417  * Implementation of dt_object_operations::do_xattr_get.
1418  *
1419  * If LOV EA is requested from the root object and it's not
1420  * found, then return default striping for the filesystem.
1421  *
1422  * \see dt_object_operations::do_xattr_get() in the API description for details.
1423  */
1424 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1425                          struct lu_buf *buf, const char *name)
1426 {
1427         struct lod_thread_info *info = lod_env_info(env);
1428         struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1429         int is_root;
1430         int rc;
1431         ENTRY;
1432
1433         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1434         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1435                 struct lmv_mds_md_v1    *lmv1;
1436                 int                      rc1 = 0;
1437
1438                 if (rc > (typeof(rc))sizeof(*lmv1))
1439                         RETURN(rc);
1440
1441                 if (rc < (typeof(rc))sizeof(*lmv1))
1442                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1443
1444                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1445                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1446
1447                         info->lti_buf.lb_buf = info->lti_key;
1448                         info->lti_buf.lb_len = sizeof(*lmv1);
1449                         rc = dt_xattr_get(env, dt_object_child(dt),
1450                                           &info->lti_buf, name);
1451                         if (unlikely(rc != sizeof(*lmv1)))
1452                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1453
1454                         lmv1 = info->lti_buf.lb_buf;
1455                         /* The on-disk LMV EA only contains header, but the
1456                          * returned LMV EA size should contain the space for
1457                          * the FIDs of all shards of the striped directory. */
1458                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1459                                 rc = lmv_mds_md_size(
1460                                         le32_to_cpu(lmv1->lmv_stripe_count),
1461                                         LMV_MAGIC_V1);
1462                 } else {
1463                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1464                                                   buf, false);
1465                 }
1466
1467                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1468         }
1469
1470         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1471                 RETURN(rc);
1472
1473         /*
1474          * XXX: Only used by lfsck
1475          *
1476          * lod returns default striping on the real root of the device
1477          * this is like the root stores default striping for the whole
1478          * filesystem. historically we've been using a different approach
1479          * and store it in the config.
1480          */
1481         dt_root_get(env, dev->lod_child, &info->lti_fid);
1482         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1483
1484         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1485                 struct lov_user_md *lum = buf->lb_buf;
1486                 struct lov_desc    *desc = &dev->lod_desc;
1487
1488                 if (buf->lb_buf == NULL) {
1489                         rc = sizeof(*lum);
1490                 } else if (buf->lb_len >= sizeof(*lum)) {
1491                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1492                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1493                         lmm_oi_set_id(&lum->lmm_oi, 0);
1494                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1495                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1496                         lum->lmm_stripe_size = cpu_to_le32(
1497                                                 desc->ld_default_stripe_size);
1498                         lum->lmm_stripe_count = cpu_to_le16(
1499                                                 desc->ld_default_stripe_count);
1500                         lum->lmm_stripe_offset = cpu_to_le16(
1501                                                 desc->ld_default_stripe_offset);
1502                         rc = sizeof(*lum);
1503                 } else {
1504                         rc = -ERANGE;
1505                 }
1506         }
1507
1508         RETURN(rc);
1509 }
1510
1511 /**
1512  * Verify LVM EA.
1513  *
1514  * Checks that the magic of the stripe is sane.
1515  *
1516  * \param[in] lod       lod device
1517  * \param[in] lum       a buffer storing LMV EA to verify
1518  *
1519  * \retval              0 if the EA is sane
1520  * \retval              negative otherwise
1521  */
1522 static int lod_verify_md_striping(struct lod_device *lod,
1523                                   const struct lmv_user_md_v1 *lum)
1524 {
1525         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1526                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1527                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1528                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1529                        (int)le32_to_cpu(lum->lum_stripe_offset),
1530                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1531                 return -EINVAL;
1532         }
1533
1534         return 0;
1535 }
1536
1537 /**
1538  * Initialize LMV EA for a slave.
1539  *
1540  * Initialize slave's LMV EA from the master's LMV EA.
1541  *
1542  * \param[in] master_lmv        a buffer containing master's EA
1543  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1544  *
1545  */
1546 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1547                                   const struct lmv_mds_md_v1 *master_lmv)
1548 {
1549         *slave_lmv = *master_lmv;
1550         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1551 }
1552
1553 /**
1554  * Generate LMV EA.
1555  *
1556  * Generate LMV EA from the object passed as \a dt. The object must have
1557  * the stripes created and initialized.
1558  *
1559  * \param[in] env       execution environment
1560  * \param[in] dt        object
1561  * \param[out] lmv_buf  buffer storing generated LMV EA
1562  *
1563  * \retval              0 on success
1564  * \retval              negative if failed
1565  */
1566 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1567                            struct lu_buf *lmv_buf)
1568 {
1569         struct lod_thread_info  *info = lod_env_info(env);
1570         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1571         struct lod_object       *lo = lod_dt_obj(dt);
1572         struct lmv_mds_md_v1    *lmm1;
1573         int                     stripe_count;
1574         int                     type = LU_SEQ_RANGE_ANY;
1575         int                     rc;
1576         __u32                   mdtidx;
1577         ENTRY;
1578
1579         LASSERT(lo->ldo_dir_striped != 0);
1580         LASSERT(lo->ldo_dir_stripe_count > 0);
1581         stripe_count = lo->ldo_dir_stripe_count;
1582         /* Only store the LMV EA heahder on the disk. */
1583         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1584                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1585                 if (rc != 0)
1586                         RETURN(rc);
1587         } else {
1588                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1589         }
1590
1591         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1592         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1593         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1594         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1595         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1596                             &mdtidx, &type);
1597         if (rc != 0)
1598                 RETURN(rc);
1599
1600         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1601         lmv_buf->lb_buf = info->lti_ea_store;
1602         lmv_buf->lb_len = sizeof(*lmm1);
1603
1604         RETURN(rc);
1605 }
1606
1607 /**
1608  * Create in-core represenation for a striped directory.
1609  *
1610  * Parse the buffer containing LMV EA and instantiate LU objects
1611  * representing the stripe objects. The pointers to the objects are
1612  * stored in ldo_stripe field of \a lo. This function is used when
1613  * we need to access an already created object (i.e. load from a disk).
1614  *
1615  * \param[in] env       execution environment
1616  * \param[in] lo        lod object
1617  * \param[in] buf       buffer containing LMV EA
1618  *
1619  * \retval              0 on success
1620  * \retval              negative if failed
1621  */
1622 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1623                            const struct lu_buf *buf)
1624 {
1625         struct lod_thread_info  *info = lod_env_info(env);
1626         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1627         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1628         struct dt_object        **stripe;
1629         union lmv_mds_md        *lmm = buf->lb_buf;
1630         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1631         struct lu_fid           *fid = &info->lti_fid;
1632         unsigned int            i;
1633         int                     rc = 0;
1634         ENTRY;
1635
1636         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1637                 RETURN(0);
1638
1639         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1640                 lo->ldo_dir_slave_stripe = 1;
1641                 RETURN(0);
1642         }
1643
1644         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1645                 RETURN(-EINVAL);
1646
1647         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1648                 RETURN(0);
1649
1650         LASSERT(lo->ldo_stripe == NULL);
1651         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1652                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1653         if (stripe == NULL)
1654                 RETURN(-ENOMEM);
1655
1656         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1657                 struct dt_device        *tgt_dt;
1658                 struct dt_object        *dto;
1659                 int                     type = LU_SEQ_RANGE_ANY;
1660                 __u32                   idx;
1661
1662                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1663                 if (!fid_is_sane(fid))
1664                         GOTO(out, rc = -ESTALE);
1665
1666                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1667                 if (rc != 0)
1668                         GOTO(out, rc);
1669
1670                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1671                         tgt_dt = lod->lod_child;
1672                 } else {
1673                         struct lod_tgt_desc     *tgt;
1674
1675                         tgt = LTD_TGT(ltd, idx);
1676                         if (tgt == NULL)
1677                                 GOTO(out, rc = -ESTALE);
1678                         tgt_dt = tgt->ltd_tgt;
1679                 }
1680
1681                 dto = dt_locate_at(env, tgt_dt, fid,
1682                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1683                                   NULL);
1684                 if (IS_ERR(dto))
1685                         GOTO(out, rc = PTR_ERR(dto));
1686
1687                 stripe[i] = dto;
1688         }
1689 out:
1690         lo->ldo_stripe = stripe;
1691         lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1692         lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1693         if (rc != 0)
1694                 lod_object_free_striping(env, lo);
1695
1696         RETURN(rc);
1697 }
1698
1699 /**
1700  * Declare create a striped directory.
1701  *
1702  * Declare creating a striped directory with a given stripe pattern on the
1703  * specified MDTs. A striped directory is represented as a regular directory
1704  * - an index listing all the stripes. The stripes point back to the master
1705  * object with ".." and LinkEA. The master object gets LMV EA which
1706  * identifies it as a striped directory. The function allocates FIDs
1707  * for all stripes.
1708  *
1709  * \param[in] env       execution environment
1710  * \param[in] dt        object
1711  * \param[in] attr      attributes to initialize the objects with
1712  * \param[in] dof       type of objects to be created
1713  * \param[in] th        transaction handle
1714  *
1715  * \retval              0 on success
1716  * \retval              negative if failed
1717  */
1718 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1719                                           struct dt_object *dt,
1720                                           struct lu_attr *attr,
1721                                           struct dt_object_format *dof,
1722                                           struct thandle *th)
1723 {
1724         struct lod_thread_info  *info = lod_env_info(env);
1725         struct lu_buf           lmv_buf;
1726         struct lu_buf           slave_lmv_buf;
1727         struct lmv_mds_md_v1    *lmm;
1728         struct lmv_mds_md_v1    *slave_lmm = NULL;
1729         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1730         struct lod_object       *lo = lod_dt_obj(dt);
1731         int                     rc;
1732         __u32                   i;
1733         ENTRY;
1734
1735         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1736         if (rc != 0)
1737                 GOTO(out, rc);
1738         lmm = lmv_buf.lb_buf;
1739
1740         OBD_ALLOC_PTR(slave_lmm);
1741         if (slave_lmm == NULL)
1742                 GOTO(out, rc = -ENOMEM);
1743
1744         lod_prep_slave_lmv_md(slave_lmm, lmm);
1745         slave_lmv_buf.lb_buf = slave_lmm;
1746         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1747
1748         if (!dt_try_as_dir(env, dt_object_child(dt)))
1749                 GOTO(out, rc = -EINVAL);
1750
1751         rec->rec_type = S_IFDIR;
1752         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1753                 struct dt_object        *dto = lo->ldo_stripe[i];
1754                 char                    *stripe_name = info->lti_key;
1755                 struct lu_name          *sname;
1756                 struct linkea_data       ldata          = { NULL };
1757                 struct lu_buf           linkea_buf;
1758
1759                 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1760                 if (rc != 0)
1761                         GOTO(out, rc);
1762
1763                 if (!dt_try_as_dir(env, dto))
1764                         GOTO(out, rc = -EINVAL);
1765
1766                 rc = lod_sub_declare_ref_add(env, dto, th);
1767                 if (rc != 0)
1768                         GOTO(out, rc);
1769
1770                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1771                 rc = lod_sub_declare_insert(env, dto,
1772                                             (const struct dt_rec *)rec,
1773                                             (const struct dt_key *)dot, th);
1774                 if (rc != 0)
1775                         GOTO(out, rc);
1776
1777                 /* master stripe FID will be put to .. */
1778                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1779                 rc = lod_sub_declare_insert(env, dto,
1780                                             (const struct dt_rec *)rec,
1781                                             (const struct dt_key *)dotdot, th);
1782                 if (rc != 0)
1783                         GOTO(out, rc);
1784
1785                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1786                     cfs_fail_val != i) {
1787                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1788                             cfs_fail_val == i)
1789                                 slave_lmm->lmv_master_mdt_index =
1790                                                         cpu_to_le32(i + 1);
1791                         else
1792                                 slave_lmm->lmv_master_mdt_index =
1793                                                         cpu_to_le32(i);
1794                         rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1795                                                        XATTR_NAME_LMV, 0, th);
1796                         if (rc != 0)
1797                                 GOTO(out, rc);
1798                 }
1799
1800                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1801                     cfs_fail_val == i)
1802                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1803                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1804                 else
1805                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1806                                 PFID(lu_object_fid(&dto->do_lu)), i);
1807
1808                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1809                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1810                                       sname, lu_object_fid(&dt->do_lu));
1811                 if (rc != 0)
1812                         GOTO(out, rc);
1813
1814                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1815                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1816                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1817                                                XATTR_NAME_LINK, 0, th);
1818                 if (rc != 0)
1819                         GOTO(out, rc);
1820
1821                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1822                 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1823                                             (const struct dt_rec *)rec,
1824                                             (const struct dt_key *)stripe_name,
1825                                             th);
1826                 if (rc != 0)
1827                         GOTO(out, rc);
1828
1829                 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1830                 if (rc != 0)
1831                         GOTO(out, rc);
1832         }
1833
1834         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1835                                        &lmv_buf, XATTR_NAME_LMV, 0, th);
1836         if (rc != 0)
1837                 GOTO(out, rc);
1838 out:
1839         if (slave_lmm != NULL)
1840                 OBD_FREE_PTR(slave_lmm);
1841
1842         RETURN(rc);
1843 }
1844
1845 static int lod_prep_md_striped_create(const struct lu_env *env,
1846                                       struct dt_object *dt,
1847                                       struct lu_attr *attr,
1848                                       const struct lmv_user_md_v1 *lum,
1849                                       struct dt_object_format *dof,
1850                                       struct thandle *th)
1851 {
1852         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1853         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1854         struct lod_object       *lo = lod_dt_obj(dt);
1855         struct dt_object        **stripe;
1856         __u32                   stripe_count;
1857         int                     *idx_array;
1858         __u32                   master_index;
1859         int                     rc = 0;
1860         __u32                   i;
1861         __u32                   j;
1862         ENTRY;
1863
1864         /* The lum has been verifed in lod_verify_md_striping */
1865         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1866         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1867
1868         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1869
1870         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1871         if (idx_array == NULL)
1872                 RETURN(-ENOMEM);
1873
1874         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1875         if (stripe == NULL)
1876                 GOTO(out_free, rc = -ENOMEM);
1877
1878         /* Start index must be the master MDT */
1879         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1880         idx_array[0] = master_index;
1881         for (i = 0; i < stripe_count; i++) {
1882                 struct lod_tgt_desc     *tgt = NULL;
1883                 struct dt_object        *dto;
1884                 struct lu_fid           fid = { 0 };
1885                 int                     idx;
1886                 struct lu_object_conf   conf = { 0 };
1887                 struct dt_device        *tgt_dt = NULL;
1888
1889                 /* Try to find next avaible target */
1890                 idx = idx_array[i];
1891                 for (j = 0; j < lod->lod_remote_mdt_count;
1892                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1893                         bool already_allocated = false;
1894                         __u32 k;
1895
1896                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1897                                idx, lod->lod_remote_mdt_count + 1, i);
1898
1899                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1900                                 /* check whether the idx already exists
1901                                  * in current allocated array */
1902                                 for (k = 0; k < i; k++) {
1903                                         if (idx_array[k] == idx) {
1904                                                 already_allocated = true;
1905                                                 break;
1906                                         }
1907                                 }
1908
1909                                 if (already_allocated)
1910                                         continue;
1911                         }
1912
1913                         /* Sigh, this index is not in the bitmap, let's check
1914                          * next available target */
1915                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1916                             idx != master_index)
1917                                 continue;
1918
1919                         if (idx == master_index) {
1920                                 /* Allocate the FID locally */
1921                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1922                                                    &fid, NULL);
1923                                 if (rc < 0)
1924                                         GOTO(out_put, rc);
1925                                 tgt_dt = lod->lod_child;
1926                                 break;
1927                         }
1928
1929                         /* check the status of the OSP */
1930                         tgt = LTD_TGT(ltd, idx);
1931                         if (tgt == NULL)
1932                                 continue;
1933
1934                         tgt_dt = tgt->ltd_tgt;
1935                         rc = dt_statfs(env, tgt_dt, NULL);
1936                         if (rc) {
1937                                 /* this OSP doesn't feel well */
1938                                 rc = 0;
1939                                 continue;
1940                         }
1941
1942                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1943                         if (rc < 0) {
1944                                 rc = 0;
1945                                 continue;
1946                         }
1947
1948                         break;
1949                 }
1950
1951                 /* Can not allocate more stripes */
1952                 if (j == lod->lod_remote_mdt_count) {
1953                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1954                                lod2obd(lod)->obd_name, stripe_count, i);
1955                         break;
1956                 }
1957
1958                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1959                        idx, i, PFID(&fid));
1960                 idx_array[i] = idx;
1961                 /* Set the start index for next stripe allocation */
1962                 if (i < stripe_count - 1)
1963                         idx_array[i + 1] = (idx + 1) %
1964                                            (lod->lod_remote_mdt_count + 1);
1965                 /* tgt_dt and fid must be ready after search avaible OSP
1966                  * in the above loop */
1967                 LASSERT(tgt_dt != NULL);
1968                 LASSERT(fid_is_sane(&fid));
1969                 conf.loc_flags = LOC_F_NEW;
1970                 dto = dt_locate_at(env, tgt_dt, &fid,
1971                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1972                                    &conf);
1973                 if (IS_ERR(dto))
1974                         GOTO(out_put, rc = PTR_ERR(dto));
1975                 stripe[i] = dto;
1976         }
1977
1978         lo->ldo_dir_stripe_loaded = 1;
1979         lo->ldo_dir_striped = 1;
1980         lo->ldo_stripe = stripe;
1981         lo->ldo_dir_stripe_count = i;
1982         lo->ldo_dir_stripes_allocated = stripe_count;
1983
1984         if (lo->ldo_dir_stripe_count == 0)
1985                 GOTO(out_put, rc = -ENOSPC);
1986
1987         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1988         if (rc != 0)
1989                 GOTO(out_put, rc);
1990
1991 out_put:
1992         if (rc < 0) {
1993                 for (i = 0; i < stripe_count; i++)
1994                         if (stripe[i] != NULL)
1995                                 dt_object_put(env, stripe[i]);
1996                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1997                 lo->ldo_dir_stripe_count = 0;
1998                 lo->ldo_dir_stripes_allocated = 0;
1999                 lo->ldo_stripe = NULL;
2000         }
2001
2002 out_free:
2003         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2004
2005         RETURN(rc);
2006 }
2007
2008 /**
2009  * Declare create striped md object.
2010  *
2011  * The function declares intention to create a striped directory. This is a
2012  * wrapper for lod_prep_md_striped_create(). The only additional functionality
2013  * is to verify pattern \a lum_buf is good. Check that function for the details.
2014  *
2015  * \param[in] env       execution environment
2016  * \param[in] dt        object
2017  * \param[in] attr      attributes to initialize the objects with
2018  * \param[in] lum_buf   a pattern specifying the number of stripes and
2019  *                      MDT to start from
2020  * \param[in] dof       type of objects to be created
2021  * \param[in] th        transaction handle
2022  *
2023  * \retval              0 on success
2024  * \retval              negative if failed
2025  *
2026  */
2027 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2028                                      struct dt_object *dt,
2029                                      struct lu_attr *attr,
2030                                      const struct lu_buf *lum_buf,
2031                                      struct dt_object_format *dof,
2032                                      struct thandle *th)
2033 {
2034         struct lod_object       *lo = lod_dt_obj(dt);
2035         struct lmv_user_md_v1   *lum = lum_buf->lb_buf;
2036         int                     rc;
2037         ENTRY;
2038
2039         LASSERT(lum != NULL);
2040
2041         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2042                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2043                (int)le32_to_cpu(lum->lum_stripe_offset));
2044
2045         if (le32_to_cpu(lum->lum_stripe_count) == 0)
2046                 GOTO(out, rc = 0);
2047
2048         /* prepare dir striped objects */
2049         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2050         if (rc != 0) {
2051                 /* failed to create striping, let's reset
2052                  * config so that others don't get confused */
2053                 lod_object_free_striping(env, lo);
2054                 GOTO(out, rc);
2055         }
2056 out:
2057         RETURN(rc);
2058 }
2059
2060 /**
2061  * Implementation of dt_object_operations::do_declare_xattr_set.
2062  *
2063  * Used with regular (non-striped) objects. Basically it
2064  * initializes the striping information and applies the
2065  * change to all the stripes.
2066  *
2067  * \see dt_object_operations::do_declare_xattr_set() in the API description
2068  * for details.
2069  */
2070 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2071                                      struct dt_object *dt,
2072                                      const struct lu_buf *buf,
2073                                      const char *name, int fl,
2074                                      struct thandle *th)
2075 {
2076         struct dt_object        *next = dt_object_child(dt);
2077         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2078         struct lod_object       *lo = lod_dt_obj(dt);
2079         int                     i;
2080         int                     rc;
2081         ENTRY;
2082
2083         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2084                 struct lmv_user_md_v1 *lum;
2085
2086                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2087                 lum = buf->lb_buf;
2088                 rc = lod_verify_md_striping(d, lum);
2089                 if (rc != 0)
2090                         RETURN(rc);
2091         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2092                 rc = lod_verify_striping(d, lo, buf, false);
2093                 if (rc != 0)
2094                         RETURN(rc);
2095         }
2096
2097         rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2098         if (rc != 0)
2099                 RETURN(rc);
2100
2101         /* Note: Do not set LinkEA on sub-stripes, otherwise
2102          * it will confuse the fid2path process(see mdt_path_current()).
2103          * The linkEA between master and sub-stripes is set in
2104          * lod_xattr_set_lmv(). */
2105         if (strcmp(name, XATTR_NAME_LINK) == 0)
2106                 RETURN(0);
2107
2108         /* set xattr to each stripes, if needed */
2109         rc = lod_load_striping(env, lo);
2110         if (rc != 0)
2111                 RETURN(rc);
2112
2113         if (lo->ldo_dir_stripe_count == 0)
2114                 RETURN(0);
2115
2116         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2117                 LASSERT(lo->ldo_stripe[i]);
2118
2119                 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2120                                                buf, name, fl, th);
2121                 if (rc != 0)
2122                         break;
2123         }
2124
2125         RETURN(rc);
2126 }
2127
2128 static int
2129 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2130                                      struct lod_object *lo,
2131                                      struct dt_object *dt, struct thandle *th,
2132                                      int comp_idx, int stripe_idx,
2133                                      struct lod_obj_stripe_cb_data *data)
2134 {
2135         struct lod_thread_info *info = lod_env_info(env);
2136         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2137         struct filter_fid *ff = &info->lti_ff;
2138         struct lu_buf *buf = &info->lti_buf;
2139         int rc;
2140
2141         buf->lb_buf = ff;
2142         buf->lb_len = sizeof(*ff);
2143         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2144         if (rc < 0) {
2145                 if (rc == -ENODATA)
2146                         return 0;
2147                 return rc;
2148         }
2149
2150         filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2151         if (lu_fid_eq(lu_object_fid(&lo->ldo_obj.do_lu), &ff->ff_parent) &&
2152             ff->ff_layout.ol_comp_id == comp->llc_id)
2153                 return 0;
2154
2155         /* rewrite filter_fid */
2156         memset(ff, 0, sizeof(*ff));
2157         ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2158         ff->ff_parent.f_ver = stripe_idx;
2159         ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2160         ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2161         ff->ff_layout.ol_comp_id = comp->llc_id;
2162         ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2163         ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2164         filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2165
2166         if (data->locd_declare)
2167                 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2168                                                LU_XATTR_REPLACE, th);
2169         else
2170                 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2171                                        LU_XATTR_REPLACE, th);
2172
2173         return rc;
2174 }
2175
2176 /**
2177  * Reset parent FID on OST object
2178  *
2179  * Replace parent FID with @dt object FID, which is only called during migration
2180  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2181  * the FID is changed.
2182  *
2183  * \param[in] env execution environment
2184  * \param[in] dt dt_object whose stripes's parent FID will be reset
2185  * \parem[in] th thandle
2186  * \param[in] declare if it is declare
2187  *
2188  * \retval      0 if reset succeeds
2189  * \retval      negative errno if reset fails
2190  */
2191 static int lod_replace_parent_fid(const struct lu_env *env,
2192                                   struct dt_object *dt,
2193                                   struct thandle *th, bool declare)
2194 {
2195         struct lod_object *lo = lod_dt_obj(dt);
2196         struct lod_thread_info  *info = lod_env_info(env);
2197         struct lu_buf *buf = &info->lti_buf;
2198         struct filter_fid *ff;
2199         struct lod_obj_stripe_cb_data data = { { 0 } };
2200         int rc;
2201         ENTRY;
2202
2203         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2204
2205         /* set xattr to each stripes, if needed */
2206         rc = lod_load_striping(env, lo);
2207         if (rc != 0)
2208                 RETURN(rc);
2209
2210         if (!lod_obj_is_striped(dt))
2211                 RETURN(0);
2212
2213         if (info->lti_ea_store_size < sizeof(*ff)) {
2214                 rc = lod_ea_store_resize(info, sizeof(*ff));
2215                 if (rc != 0)
2216                         RETURN(rc);
2217         }
2218
2219         buf->lb_buf = info->lti_ea_store;
2220         buf->lb_len = info->lti_ea_store_size;
2221
2222         data.locd_declare = declare;
2223         data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2224         rc = lod_obj_for_each_stripe(env, lo, th, &data);
2225
2226         RETURN(rc);
2227 }
2228
2229 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2230                                          struct lod_layout_component *entry,
2231                                          bool is_dir)
2232 {
2233         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2234
2235         if (is_dir)
2236                 return  0;
2237         else if (lod_comp_inited(entry))
2238                 return entry->llc_stripe_count;
2239         else if ((__u16)-1 == entry->llc_stripe_count)
2240                 return lod->lod_desc.ld_tgt_count;
2241         else
2242                 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2243 }
2244
2245 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2246 {
2247         int magic, size = 0, i;
2248         struct lod_layout_component *comp_entries;
2249         __u16 comp_cnt;
2250         bool is_composite;
2251
2252         if (is_dir) {
2253                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2254                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2255                 is_composite =
2256                         lo->ldo_def_striping->lds_def_striping_is_composite;
2257         } else {
2258                 comp_cnt = lo->ldo_comp_cnt;
2259                 comp_entries = lo->ldo_comp_entries;
2260                 is_composite = lo->ldo_is_composite;
2261         }
2262
2263
2264         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2265         if (is_composite) {
2266                 size = sizeof(struct lov_comp_md_v1) +
2267                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2268                 LASSERT(size % sizeof(__u64) == 0);
2269         }
2270
2271         for (i = 0; i < comp_cnt; i++) {
2272                 __u16 stripe_count;
2273
2274                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2275                 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2276                                                            is_dir);
2277                 if (!is_dir && is_composite)
2278                         lod_comp_shrink_stripe_count(&comp_entries[i],
2279                                                      &stripe_count);
2280
2281                 size += lov_user_md_size(stripe_count, magic);
2282                 LASSERT(size % sizeof(__u64) == 0);
2283         }
2284         return size;
2285 }
2286
2287 /**
2288  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2289  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2290  * to be added.
2291   *
2292  * \param[in] env       execution environment
2293  * \param[in] dt        dt_object to add components on
2294  * \param[in] buf       buffer contains components to be added
2295  * \parem[in] th        thandle
2296  *
2297  * \retval      0 on success
2298  * \retval      negative errno on failure
2299  */
2300 static int lod_declare_layout_add(const struct lu_env *env,
2301                                   struct dt_object *dt,
2302                                   const struct lu_buf *buf,
2303                                   struct thandle *th)
2304 {
2305         struct lod_thread_info  *info = lod_env_info(env);
2306         struct lod_layout_component *comp_array, *lod_comp, *old_array;
2307         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2308         struct dt_object *next = dt_object_child(dt);
2309         struct lov_desc         *desc = &d->lod_desc;
2310         struct lod_object       *lo = lod_dt_obj(dt);
2311         struct lov_user_md_v3   *v3;
2312         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2313         __u32   magic;
2314         int     i, rc, array_cnt, old_array_cnt;
2315         ENTRY;
2316
2317         LASSERT(lo->ldo_is_composite);
2318
2319         if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2320                 RETURN(-EBUSY);
2321
2322         rc = lod_verify_striping(d, lo, buf, false);
2323         if (rc != 0)
2324                 RETURN(rc);
2325
2326         magic = comp_v1->lcm_magic;
2327         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2328                 lustre_swab_lov_comp_md_v1(comp_v1);
2329                 magic = comp_v1->lcm_magic;
2330         }
2331
2332         if (magic != LOV_USER_MAGIC_COMP_V1)
2333                 RETURN(-EINVAL);
2334
2335         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2336         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2337         if (comp_array == NULL)
2338                 RETURN(-ENOMEM);
2339
2340         memcpy(comp_array, lo->ldo_comp_entries,
2341                sizeof(*comp_array) * lo->ldo_comp_cnt);
2342
2343         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2344                 struct lov_user_md_v1 *v1;
2345                 struct lu_extent *ext;
2346
2347                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2348                                 comp_v1->lcm_entries[i].lcme_offset);
2349                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2350
2351                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2352                 lod_comp->llc_extent.e_start = ext->e_start;
2353                 lod_comp->llc_extent.e_end = ext->e_end;
2354                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2355                 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2356
2357                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2358                 if (!lod_comp->llc_stripe_count ||
2359                     lod_comp->llc_stripe_count == (__u16)-1)
2360                         lod_comp->llc_stripe_count =
2361                                 desc->ld_default_stripe_count;
2362                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2363                 if (!lod_comp->llc_stripe_size)
2364                         lod_comp->llc_stripe_size =
2365                                 desc->ld_default_stripe_size;
2366
2367                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2368                         v3 = (struct lov_user_md_v3 *) v1;
2369                         if (v3->lmm_pool_name[0] != '\0') {
2370                                 rc = lod_set_pool(&lod_comp->llc_pool,
2371                                                   v3->lmm_pool_name);
2372                                 if (rc)
2373                                         GOTO(error, rc);
2374                         }
2375                 }
2376         }
2377
2378         old_array = lo->ldo_comp_entries;
2379         old_array_cnt = lo->ldo_comp_cnt;
2380
2381         lo->ldo_comp_entries = comp_array;
2382         lo->ldo_comp_cnt = array_cnt;
2383
2384         /* No need to increase layout generation here, it will be increased
2385          * later when generating component ID for the new components */
2386
2387         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2388         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2389                                               XATTR_NAME_LOV, 0, th);
2390         if (rc) {
2391                 lo->ldo_comp_entries = old_array;
2392                 lo->ldo_comp_cnt = old_array_cnt;
2393                 GOTO(error, rc);
2394         }
2395
2396         OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2397
2398         LASSERT(lo->ldo_mirror_count == 1);
2399         lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2400
2401         RETURN(0);
2402
2403 error:
2404         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2405                 lod_comp = &comp_array[i];
2406                 if (lod_comp->llc_pool != NULL) {
2407                         OBD_FREE(lod_comp->llc_pool,
2408                                  strlen(lod_comp->llc_pool) + 1);
2409                         lod_comp->llc_pool = NULL;
2410                 }
2411         }
2412         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2413         RETURN(rc);
2414 }
2415
2416 /**
2417  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2418  * the '$field' can only be 'flags' now. The xattr value is binary
2419  * lov_comp_md_v1 which contains the component ID(s) and the value of
2420  * the field to be modified.
2421  *
2422  * \param[in] env       execution environment
2423  * \param[in] dt        dt_object to be modified
2424  * \param[in] op        operation string, like "set.flags"
2425  * \param[in] buf       buffer contains components to be set
2426  * \parem[in] th        thandle
2427  *
2428  * \retval      0 on success
2429  * \retval      negative errno on failure
2430  */
2431 static int lod_declare_layout_set(const struct lu_env *env,
2432                                   struct dt_object *dt,
2433                                   char *op, const struct lu_buf *buf,
2434                                   struct thandle *th)
2435 {
2436         struct lod_layout_component     *lod_comp;
2437         struct lod_thread_info  *info = lod_env_info(env);
2438         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2439         struct lod_object       *lo = lod_dt_obj(dt);
2440         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2441         __u32   magic, id;
2442         int     i, j, rc;
2443         bool    changed = false;
2444         ENTRY;
2445
2446         if (strcmp(op, "set.flags") != 0) {
2447                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2448                        lod2obd(d)->obd_name, op);
2449                 RETURN(-ENOTSUPP);
2450         }
2451
2452         magic = comp_v1->lcm_magic;
2453         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2454                 lustre_swab_lov_comp_md_v1(comp_v1);
2455                 magic = comp_v1->lcm_magic;
2456         }
2457
2458         if (magic != LOV_USER_MAGIC_COMP_V1)
2459                 RETURN(-EINVAL);
2460
2461         if (comp_v1->lcm_entry_count == 0) {
2462                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2463                        lod2obd(d)->obd_name);
2464                 RETURN(-EINVAL);
2465         }
2466
2467         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2468                 id = comp_v1->lcm_entries[i].lcme_id;
2469
2470                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2471                         lod_comp = &lo->ldo_comp_entries[j];
2472                         if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2473                                 lod_comp->llc_flags =
2474                                         comp_v1->lcm_entries[i].lcme_flags;
2475                                 changed = true;
2476                         }
2477                 }
2478         }
2479
2480         if (!changed) {
2481                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2482                        lod2obd(d)->obd_name);
2483                 RETURN(-EINVAL);
2484         }
2485
2486         lod_obj_inc_layout_gen(lo);
2487
2488         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2489         rc = lod_sub_declare_xattr_set(env, dt, &info->lti_buf,
2490                                        XATTR_NAME_LOV, 0, th);
2491         RETURN(rc);
2492 }
2493
2494 /**
2495  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2496  * and the xattr value is a unique component ID or a special lcme_id.
2497  *
2498  * \param[in] env       execution environment
2499  * \param[in] dt        dt_object to be operated on
2500  * \param[in] buf       buffer contains component ID or lcme_id
2501  * \parem[in] th        thandle
2502  *
2503  * \retval      0 on success
2504  * \retval      negative errno on failure
2505  */
2506 static int lod_declare_layout_del(const struct lu_env *env,
2507                                   struct dt_object *dt,
2508                                   const struct lu_buf *buf,
2509                                   struct thandle *th)
2510 {
2511         struct lod_thread_info  *info = lod_env_info(env);
2512         struct dt_object *next = dt_object_child(dt);
2513         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2514         struct lod_object *lo = lod_dt_obj(dt);
2515         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2516         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2517         __u32 magic, id, flags, neg_flags = 0;
2518         int rc, i, j, left;
2519         ENTRY;
2520
2521         LASSERT(lo->ldo_is_composite);
2522
2523         if (lo->ldo_flr_state != LCM_FL_NOT_FLR)
2524                 RETURN(-EBUSY);
2525
2526         magic = comp_v1->lcm_magic;
2527         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2528                 lustre_swab_lov_comp_md_v1(comp_v1);
2529                 magic = comp_v1->lcm_magic;
2530         }
2531
2532         if (magic != LOV_USER_MAGIC_COMP_V1)
2533                 RETURN(-EINVAL);
2534
2535         id = comp_v1->lcm_entries[0].lcme_id;
2536         flags = comp_v1->lcm_entries[0].lcme_flags;
2537
2538         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2539                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2540                        lod2obd(d)->obd_name, id, flags);
2541                 RETURN(-EINVAL);
2542         }
2543
2544         if (id != LCME_ID_INVAL && flags != 0) {
2545                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2546                        lod2obd(d)->obd_name);
2547                 RETURN(-EINVAL);
2548         }
2549
2550         if (flags & LCME_FL_NEG) {
2551                 neg_flags = flags & ~LCME_FL_NEG;
2552                 flags = 0;
2553         }
2554
2555         left = lo->ldo_comp_cnt;
2556         if (left <= 0)
2557                 RETURN(-EINVAL);
2558
2559         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2560                 struct lod_layout_component *lod_comp;
2561
2562                 lod_comp = &lo->ldo_comp_entries[i];
2563
2564                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2565                         continue;
2566                 else if (flags && !(flags & lod_comp->llc_flags))
2567                         continue;
2568                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2569                         continue;
2570
2571                 if (left != (i + 1)) {
2572                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2573                                "a hole.\n", lod2obd(d)->obd_name);
2574                         RETURN(-EINVAL);
2575                 }
2576                 left--;
2577
2578                 /* Mark the component as deleted */
2579                 lod_comp->llc_id = LCME_ID_INVAL;
2580
2581                 /* Not instantiated component */
2582                 if (lod_comp->llc_stripe == NULL)
2583                         continue;
2584
2585                 LASSERT(lod_comp->llc_stripe_count > 0);
2586                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2587                         struct dt_object *obj = lod_comp->llc_stripe[j];
2588
2589                         if (obj == NULL)
2590                                 continue;
2591                         rc = lod_sub_declare_destroy(env, obj, th);
2592                         if (rc)
2593                                 RETURN(rc);
2594                 }
2595         }
2596
2597         LASSERTF(left >= 0, "left = %d\n", left);
2598         if (left == lo->ldo_comp_cnt) {
2599                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2600                        lod2obd(d)->obd_name, id);
2601                 RETURN(-EINVAL);
2602         }
2603
2604         memset(attr, 0, sizeof(*attr));
2605         attr->la_valid = LA_SIZE;
2606         rc = lod_sub_declare_attr_set(env, next, attr, th);
2607         if (rc)
2608                 RETURN(rc);
2609
2610         if (left > 0) {
2611                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2612                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2613                                                XATTR_NAME_LOV, 0, th);
2614         } else {
2615                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2616         }
2617
2618         RETURN(rc);
2619 }
2620
2621 /**
2622  * Declare layout add/set/del operations issued by special xattr names:
2623  *
2624  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2625  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2626  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2627  *
2628  * \param[in] env       execution environment
2629  * \param[in] dt        object
2630  * \param[in] name      name of xattr
2631  * \param[in] buf       lu_buf contains xattr value
2632  * \param[in] th        transaction handle
2633  *
2634  * \retval              0 on success
2635  * \retval              negative if failed
2636  */
2637 static int lod_declare_modify_layout(const struct lu_env *env,
2638                                      struct dt_object *dt,
2639                                      const char *name,
2640                                      const struct lu_buf *buf,
2641                                      struct thandle *th)
2642 {
2643         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2644         struct lod_object *lo = lod_dt_obj(dt);
2645         struct dt_object *next = dt_object_child(&lo->ldo_obj);
2646         char *op;
2647         int rc, len = strlen(XATTR_LUSTRE_LOV);
2648         ENTRY;
2649
2650         LASSERT(dt_object_exists(dt));
2651
2652         if (strlen(name) <= len || name[len] != '.') {
2653                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2654                        lod2obd(d)->obd_name, name);
2655                 RETURN(-EINVAL);
2656         }
2657         len++;
2658
2659         dt_write_lock(env, next, 0);
2660         rc = lod_load_striping_locked(env, lo);
2661         if (rc)
2662                 GOTO(unlock, rc);
2663
2664         /* the layout to be modified must be a composite layout */
2665         if (!lo->ldo_is_composite) {
2666                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2667                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2668                 GOTO(unlock, rc = -EINVAL);
2669         }
2670
2671         op = (char *)name + len;
2672         if (strcmp(op, "add") == 0) {
2673                 rc = lod_declare_layout_add(env, dt, buf, th);
2674         } else if (strcmp(op, "del") == 0) {
2675                 rc = lod_declare_layout_del(env, dt, buf, th);
2676         } else if (strncmp(op, "set", strlen("set")) == 0) {
2677                 rc = lod_declare_layout_set(env, dt, op, buf, th);
2678         } else  {
2679                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2680                        lod2obd(d)->obd_name, name);
2681                 GOTO(unlock, rc = -ENOTSUPP);
2682         }
2683 unlock:
2684         if (rc)
2685                 lod_object_free_striping(env, lo);
2686         dt_write_unlock(env, next);
2687
2688         RETURN(rc);
2689 }
2690
2691 /**
2692  * Convert a plain file lov_mds_md to a composite layout.
2693  *
2694  * \param[in,out] info  the thread info::lti_ea_store buffer contains little
2695  *                      endian plain file layout
2696  *
2697  * \retval              0 on success, <0 on failure
2698  */
2699 static int lod_layout_convert(struct lod_thread_info *info)
2700 {
2701         struct lov_mds_md *lmm = info->lti_ea_store;
2702         struct lov_mds_md *lmm_save;
2703         struct lov_comp_md_v1 *lcm;
2704         struct lov_comp_md_entry_v1 *lcme;
2705         size_t size;
2706         __u32 blob_size;
2707         int rc = 0;
2708         ENTRY;
2709
2710         /* realloc buffer to a composite layout which contains one component */
2711         blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
2712                                     le32_to_cpu(lmm->lmm_magic));
2713         size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
2714
2715         OBD_ALLOC_LARGE(lmm_save, blob_size);
2716         if (!lmm_save)
2717                 GOTO(out, rc = -ENOMEM);
2718
2719         memcpy(lmm_save, lmm, blob_size);
2720
2721         if (info->lti_ea_store_size < size) {
2722                 rc = lod_ea_store_resize(info, size);
2723                 if (rc)
2724                         GOTO(out, rc);
2725         }
2726
2727         lcm = info->lti_ea_store;
2728         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
2729         lcm->lcm_size = cpu_to_le32(size);
2730         lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
2731                                                 lmm_save->lmm_layout_gen));
2732         lcm->lcm_flags = cpu_to_le16(LCM_FL_NOT_FLR);
2733         lcm->lcm_entry_count = cpu_to_le16(1);
2734         lcm->lcm_mirror_count = 0;
2735
2736         lcme = &lcm->lcm_entries[0];
2737         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2738         lcme->lcme_extent.e_start = 0;
2739         lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
2740         lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
2741         lcme->lcme_size = cpu_to_le32(blob_size);
2742
2743         memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
2744
2745         EXIT;
2746 out:
2747         if (lmm_save)
2748                 OBD_FREE_LARGE(lmm_save, blob_size);
2749         return rc;
2750 }
2751
2752 /**
2753  * Merge layouts to form a mirrored file.
2754  */
2755 static int lod_declare_layout_merge(const struct lu_env *env,
2756                 struct dt_object *dt, const struct lu_buf *mbuf,
2757                 struct thandle *th)
2758 {
2759         struct lod_thread_info  *info = lod_env_info(env);
2760         struct lu_buf           *buf = &info->lti_buf;
2761         struct lod_object       *lo = lod_dt_obj(dt);
2762         struct lov_comp_md_v1   *lcm;
2763         struct lov_comp_md_v1   *cur_lcm;
2764         struct lov_comp_md_v1   *merge_lcm;
2765         struct lov_comp_md_entry_v1     *lcme;
2766         size_t size = 0;
2767         size_t offset;
2768         __u16 cur_entry_count;
2769         __u16 merge_entry_count;
2770         __u32 id = 0;
2771         __u16 mirror_id = 0;
2772         __u32 mirror_count;
2773         int     rc, i;
2774         ENTRY;
2775
2776         merge_lcm = mbuf->lb_buf;
2777         if (mbuf->lb_len < sizeof(*merge_lcm))
2778                 RETURN(-EINVAL);
2779
2780         /* must be an existing layout from disk */
2781         if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
2782                 RETURN(-EINVAL);
2783
2784         merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
2785
2786         /* do not allow to merge two mirrored files */
2787         if (le16_to_cpu(merge_lcm->lcm_mirror_count))
2788                 RETURN(-EBUSY);
2789
2790         /* verify the target buffer */
2791         rc = lod_get_lov_ea(env, lo);
2792         if (rc <= 0)
2793                 RETURN(rc ? : -ENODATA);
2794
2795         cur_lcm = info->lti_ea_store;
2796         switch (le32_to_cpu(cur_lcm->lcm_magic)) {
2797         case LOV_MAGIC_V1:
2798         case LOV_MAGIC_V3:
2799                 rc = lod_layout_convert(info);
2800                 break;
2801         case LOV_MAGIC_COMP_V1:
2802                 rc = 0;
2803                 break;
2804         default:
2805                 rc = -EINVAL;
2806         }
2807         if (rc)
2808                 RETURN(rc);
2809
2810         /* info->lti_ea_store could be reallocated in lod_layout_convert() */
2811         cur_lcm = info->lti_ea_store;
2812         cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
2813
2814         /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
2815         mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
2816         if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
2817                 RETURN(-ERANGE);
2818
2819         /* size of new layout */
2820         size = le32_to_cpu(cur_lcm->lcm_size) +
2821                le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
2822
2823         memset(buf, 0, sizeof(*buf));
2824         lu_buf_alloc(buf, size);
2825         if (buf->lb_buf == NULL)
2826                 RETURN(-ENOMEM);
2827
2828         lcm = buf->lb_buf;
2829         memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
2830
2831         offset = sizeof(*lcm) +
2832                  sizeof(*lcme) * (cur_entry_count + merge_entry_count);
2833         for (i = 0; i < cur_entry_count; i++) {
2834                 struct lov_comp_md_entry_v1 *cur_lcme;
2835
2836                 lcme = &lcm->lcm_entries[i];
2837                 cur_lcme = &cur_lcm->lcm_entries[i];
2838
2839                 lcme->lcme_offset = cpu_to_le32(offset);
2840                 memcpy((char *)lcm + offset,
2841                        (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
2842                        le32_to_cpu(lcme->lcme_size));
2843
2844                 offset += le32_to_cpu(lcme->lcme_size);
2845
2846                 if (mirror_count == 1) {
2847                         /* new mirrored file, create new mirror ID */
2848                         id = pflr_id(1, i + 1);
2849                         lcme->lcme_id = cpu_to_le32(id);
2850                 }
2851
2852                 id = MAX(le32_to_cpu(lcme->lcme_id), id);
2853         }
2854
2855         mirror_id = mirror_id_of(id) + 1;
2856         for (i = 0; i < merge_entry_count; i++) {
2857                 struct lov_comp_md_entry_v1 *merge_lcme;
2858
2859                 merge_lcme = &merge_lcm->lcm_entries[i];
2860                 lcme = &lcm->lcm_entries[cur_entry_count + i];
2861
2862                 *lcme = *merge_lcme;
2863                 lcme->lcme_offset = cpu_to_le32(offset);
2864
2865                 id = pflr_id(mirror_id, i + 1);
2866                 lcme->lcme_id = cpu_to_le32(id);
2867
2868                 memcpy((char *)lcm + offset,
2869                        (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
2870                        le32_to_cpu(lcme->lcme_size));
2871
2872                 offset += le32_to_cpu(lcme->lcme_size);
2873         }
2874
2875         /* fixup layout information */
2876         lod_obj_inc_layout_gen(lo);
2877         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
2878         lcm->lcm_size = cpu_to_le32(size);
2879         lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
2880         lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
2881         if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NOT_FLR)
2882                 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
2883
2884         LASSERT(dt_write_locked(env, dt_object_child(dt)));
2885         lod_object_free_striping(env, lo);
2886         rc = lod_parse_striping(env, lo, buf);
2887         if (rc)
2888                 GOTO(out, rc);
2889
2890         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
2891                                         XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2892
2893 out:
2894         lu_buf_free(buf);
2895         RETURN(rc);
2896 }
2897
2898 /**
2899  * Implementation of dt_object_operations::do_declare_xattr_set.
2900  *
2901  * \see dt_object_operations::do_declare_xattr_set() in the API description
2902  * for details.
2903  *
2904  * the extension to the API:
2905  *   - declaring LOVEA requests striping creation
2906  *   - LU_XATTR_REPLACE means layout swap
2907  */
2908 static int lod_declare_xattr_set(const struct lu_env *env,
2909                                  struct dt_object *dt,
2910                                  const struct lu_buf *buf,
2911                                  const char *name, int fl,
2912                                  struct thandle *th)
2913 {
2914         struct dt_object *next = dt_object_child(dt);
2915         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2916         __u32             mode;
2917         int               rc;
2918         ENTRY;
2919
2920         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2921         if ((S_ISREG(mode) || mode == 0) &&
2922             !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE)) &&
2923             (strcmp(name, XATTR_NAME_LOV) == 0 ||
2924              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2925                 /*
2926                  * this is a request to create object's striping.
2927                  *
2928                  * allow to declare predefined striping on a new (!mode) object
2929                  * which is supposed to be replay of regular file creation
2930                  * (when LOV setting is declared)
2931                  *
2932                  * LU_XATTR_REPLACE is set to indicate a layout swap
2933                  */
2934                 if (dt_object_exists(dt)) {
2935                         rc = dt_attr_get(env, next, attr);
2936                         if (rc)
2937                                 RETURN(rc);
2938                 } else {
2939                         memset(attr, 0, sizeof(*attr));
2940                         attr->la_valid = LA_TYPE | LA_MODE;
2941                         attr->la_mode = S_IFREG;
2942                 }
2943                 rc = lod_declare_striped_create(env, dt, attr, buf, th);
2944         } else if (fl & LU_XATTR_MERGE) {
2945                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
2946                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
2947                 rc = lod_declare_layout_merge(env, dt, buf, th);
2948         } else if (S_ISREG(mode) &&
2949                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2950                    strncmp(name, XATTR_LUSTRE_LOV,
2951                            strlen(XATTR_LUSTRE_LOV)) == 0) {
2952                 /*
2953                  * this is a request to modify object's striping.
2954                  * add/set/del component(s).
2955                  */
2956                 if (!dt_object_exists(dt))
2957                         RETURN(-ENOENT);
2958
2959                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2960         } else if (S_ISDIR(mode)) {
2961                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2962         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2963                 rc = lod_replace_parent_fid(env, dt, th, true);
2964         } else {
2965                 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2966         }
2967
2968         RETURN(rc);
2969 }
2970
2971 /**
2972  * Apply xattr changes to the object.
2973  *
2974  * Applies xattr changes to the object and the stripes if the latter exist.
2975  *
2976  * \param[in] env       execution environment
2977  * \param[in] dt        object
2978  * \param[in] buf       buffer pointing to the new value of xattr
2979  * \param[in] name      name of xattr
2980  * \param[in] fl        flags
2981  * \param[in] th        transaction handle
2982  *
2983  * \retval              0 on success
2984  * \retval              negative if failed
2985  */
2986 static int lod_xattr_set_internal(const struct lu_env *env,
2987                                   struct dt_object *dt,
2988                                   const struct lu_buf *buf,
2989                                   const char *name, int fl,
2990                                   struct thandle *th)
2991 {
2992         struct dt_object        *next = dt_object_child(dt);
2993         struct lod_object       *lo = lod_dt_obj(dt);
2994         int                     rc;
2995         int                     i;
2996         ENTRY;
2997
2998         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
2999         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3000                 RETURN(rc);
3001
3002         /* Note: Do not set LinkEA on sub-stripes, otherwise
3003          * it will confuse the fid2path process(see mdt_path_current()).
3004          * The linkEA between master and sub-stripes is set in
3005          * lod_xattr_set_lmv(). */
3006         if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3007                 RETURN(0);
3008
3009         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3010                 LASSERT(lo->ldo_stripe[i]);
3011
3012                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3013                                        fl, th);
3014                 if (rc != 0)
3015                         break;
3016         }
3017
3018         RETURN(rc);
3019 }
3020
3021 /**
3022  * Delete an extended attribute.
3023  *
3024  * Deletes specified xattr from the object and the stripes if the latter exist.
3025  *
3026  * \param[in] env       execution environment
3027  * \param[in] dt        object
3028  * \param[in] name      name of xattr
3029  * \param[in] th        transaction handle
3030  *
3031  * \retval              0 on success
3032  * \retval              negative if failed
3033  */
3034 static int lod_xattr_del_internal(const struct lu_env *env,
3035                                   struct dt_object *dt,
3036                                   const char *name, struct thandle *th)
3037 {
3038         struct dt_object        *next = dt_object_child(dt);
3039         struct lod_object       *lo = lod_dt_obj(dt);
3040         int                     rc;
3041         int                     i;
3042         ENTRY;
3043
3044         rc = lod_sub_xattr_del(env, next, name, th);
3045         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3046                 RETURN(rc);
3047
3048         if (lo->ldo_dir_stripe_count == 0)
3049                 RETURN(rc);
3050
3051         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3052                 LASSERT(lo->ldo_stripe[i]);
3053
3054                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3055                 if (rc != 0)
3056                         break;
3057         }
3058
3059         RETURN(rc);
3060 }
3061
3062 /**
3063  * Set default striping on a directory.
3064  *
3065  * Sets specified striping on a directory object unless it matches the default
3066  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3067  * EA. This striping will be used when regular file is being created in this
3068  * directory.
3069  *
3070  * \param[in] env       execution environment
3071  * \param[in] dt        the striped object
3072  * \param[in] buf       buffer with the striping
3073  * \param[in] name      name of EA
3074  * \param[in] fl        xattr flag (see OSD API description)
3075  * \param[in] th        transaction handle
3076  *
3077  * \retval              0 on success
3078  * \retval              negative if failed
3079  */
3080 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3081                                     struct dt_object *dt,
3082                                     const struct lu_buf *buf,
3083                                     const char *name, int fl,
3084                                     struct thandle *th)
3085 {
3086         struct lov_user_md_v1   *lum;
3087         struct lov_user_md_v3   *v3 = NULL;
3088         const char              *pool_name = NULL;
3089         int                      rc;
3090         bool                     is_del;
3091         ENTRY;
3092
3093         LASSERT(buf != NULL && buf->lb_buf != NULL);
3094         lum = buf->lb_buf;
3095
3096         switch (lum->lmm_magic) {
3097         case LOV_USER_MAGIC_V3:
3098                 v3 = buf->lb_buf;
3099                 if (v3->lmm_pool_name[0] != '\0')
3100                         pool_name = v3->lmm_pool_name;
3101                 /* fall through */
3102         case LOV_USER_MAGIC_V1:
3103                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3104                  * (i.e. all default values specified) then delete default
3105                  * striping from dir. */
3106                 CDEBUG(D_LAYOUT,
3107                        "set default striping: sz %u # %u offset %d %s %s\n",
3108                        (unsigned)lum->lmm_stripe_size,
3109                        (unsigned)lum->lmm_stripe_count,
3110                        (int)lum->lmm_stripe_offset,
3111                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3112
3113                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3114                                              lum->lmm_stripe_count,
3115                                              lum->lmm_stripe_offset,
3116                                              pool_name);
3117                 break;
3118         case LOV_USER_MAGIC_COMP_V1:
3119                 is_del = false;
3120                 break;
3121         default:
3122                 CERROR("Invalid magic %x\n", lum->lmm_magic);
3123                 RETURN(-EINVAL);
3124         }
3125
3126         if (is_del) {
3127                 rc = lod_xattr_del_internal(env, dt, name, th);
3128                 if (rc == -ENODATA)
3129                         rc = 0;
3130         } else {
3131                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3132         }
3133
3134         RETURN(rc);
3135 }
3136
3137 /**
3138  * Set default striping on a directory object.
3139  *
3140  * Sets specified striping on a directory object unless it matches the default
3141  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3142  * EA. This striping will be used when a new directory is being created in the
3143  * directory.
3144  *
3145  * \param[in] env       execution environment
3146  * \param[in] dt        the striped object
3147  * \param[in] buf       buffer with the striping
3148  * \param[in] name      name of EA
3149  * \param[in] fl        xattr flag (see OSD API description)
3150  * \param[in] th        transaction handle
3151  *
3152  * \retval              0 on success
3153  * \retval              negative if failed
3154  */
3155 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3156                                             struct dt_object *dt,
3157                                             const struct lu_buf *buf,
3158                                             const char *name, int fl,
3159                                             struct thandle *th)
3160 {
3161         struct lmv_user_md_v1   *lum;
3162         int                      rc;
3163         ENTRY;
3164
3165         LASSERT(buf != NULL && buf->lb_buf != NULL);
3166         lum = buf->lb_buf;
3167
3168         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3169               le32_to_cpu(lum->lum_stripe_count),
3170               (int)le32_to_cpu(lum->lum_stripe_offset));
3171
3172         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3173                                  le32_to_cpu(lum->lum_stripe_offset)) &&
3174                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3175                 rc = lod_xattr_del_internal(env, dt, name, th);
3176                 if (rc == -ENODATA)
3177                         rc = 0;
3178         } else {
3179                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3180                 if (rc != 0)
3181                         RETURN(rc);
3182         }
3183
3184         RETURN(rc);
3185 }
3186
3187 /**
3188  * Turn directory into a striped directory.
3189  *
3190  * During replay the client sends the striping created before MDT
3191  * failure, then the layer above LOD sends this defined striping
3192  * using ->do_xattr_set(), so LOD uses this method to replay creation
3193  * of the stripes. Notice the original information for the striping
3194  * (#stripes, FIDs, etc) was transferred in declare path.
3195  *
3196  * \param[in] env       execution environment
3197  * \param[in] dt        the striped object
3198  * \param[in] buf       not used currently
3199  * \param[in] name      not used currently
3200  * \param[in] fl        xattr flag (see OSD API description)
3201  * \param[in] th        transaction handle
3202  *
3203  * \retval              0 on success
3204  * \retval              negative if failed
3205  */
3206 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3207                              const struct lu_buf *buf, const char *name,
3208                              int fl, struct thandle *th)
3209 {
3210         struct lod_object       *lo = lod_dt_obj(dt);
3211         struct lod_thread_info  *info = lod_env_info(env);
3212         struct lu_attr          *attr = &info->lti_attr;
3213         struct dt_object_format *dof = &info->lti_format;
3214         struct lu_buf           lmv_buf;
3215         struct lu_buf           slave_lmv_buf;
3216         struct lmv_mds_md_v1    *lmm;
3217         struct lmv_mds_md_v1    *slave_lmm = NULL;
3218         struct dt_insert_rec    *rec = &info->lti_dt_rec;
3219         int                     i;
3220         int                     rc;
3221         ENTRY;
3222
3223         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3224                 RETURN(-ENOTDIR);
3225
3226         /* The stripes are supposed to be allocated in declare phase,
3227          * if there are no stripes being allocated, it will skip */
3228         if (lo->ldo_dir_stripe_count == 0)
3229                 RETURN(0);
3230
3231         rc = dt_attr_get(env, dt_object_child(dt), attr);
3232         if (rc != 0)
3233                 RETURN(rc);
3234
3235         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3236                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3237         dof->dof_type = DFT_DIR;
3238
3239         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3240         if (rc != 0)
3241                 RETURN(rc);
3242         lmm = lmv_buf.lb_buf;
3243
3244         OBD_ALLOC_PTR(slave_lmm);
3245         if (slave_lmm == NULL)
3246                 RETURN(-ENOMEM);
3247
3248         lod_prep_slave_lmv_md(slave_lmm, lmm);
3249         slave_lmv_buf.lb_buf = slave_lmm;
3250         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3251
3252         rec->rec_type = S_IFDIR;
3253         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3254                 struct dt_object *dto;
3255                 char             *stripe_name = info->lti_key;
3256                 struct lu_name          *sname;
3257                 struct linkea_data       ldata          = { NULL };
3258                 struct lu_buf            linkea_buf;
3259
3260                 dto = lo->ldo_stripe[i];
3261
3262                 dt_write_lock(env, dto, MOR_TGT_CHILD);
3263                 rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3264                 if (rc != 0) {
3265                         dt_write_unlock(env, dto);
3266                         GOTO(out, rc);
3267                 }
3268
3269                 rc = lod_sub_ref_add(env, dto, th);
3270                 dt_write_unlock(env, dto);
3271                 if (rc != 0)
3272                         GOTO(out, rc);
3273
3274                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3275                 rc = lod_sub_insert(env, dto, (const struct dt_rec *)rec,
3276                                     (const struct dt_key *)dot, th, 0);
3277                 if (rc != 0)
3278                         GOTO(out, rc);
3279
3280                 rec->rec_fid = lu_object_fid(&dt->do_lu);
3281                 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3282                                     (const struct dt_key *)dotdot, th, 0);
3283                 if (rc != 0)
3284                         GOTO(out, rc);
3285
3286                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3287                     cfs_fail_val != i) {
3288                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3289                             cfs_fail_val == i)
3290                                 slave_lmm->lmv_master_mdt_index =
3291                                                         cpu_to_le32(i + 1);
3292                         else
3293                                 slave_lmm->lmv_master_mdt_index =
3294                                                         cpu_to_le32(i);
3295
3296                         rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3297                                                XATTR_NAME_LMV, fl, th);
3298                         if (rc != 0)
3299                                 GOTO(out, rc);
3300                 }
3301
3302                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3303                     cfs_fail_val == i)
3304                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3305                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
3306                 else
3307                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3308                                  PFID(lu_object_fid(&dto->do_lu)), i);
3309
3310                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3311                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3312                                       sname, lu_object_fid(&dt->do_lu));
3313                 if (rc != 0)
3314                         GOTO(out, rc);
3315
3316                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3317                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3318                 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3319                                        XATTR_NAME_LINK, 0, th);
3320                 if (rc != 0)
3321                         GOTO(out, rc);
3322
3323                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3324                 rc = lod_sub_insert(env, dt_object_child(dt),
3325                                     (const struct dt_rec *)rec,
3326                                     (const struct dt_key *)stripe_name, th, 0);
3327                 if (rc != 0)
3328                         GOTO(out, rc);
3329
3330                 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3331                 if (rc != 0)
3332                         GOTO(out, rc);
3333         }
3334
3335         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3336                 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3337                                        &lmv_buf, XATTR_NAME_LMV, fl, th);
3338 out:
3339         if (slave_lmm != NULL)
3340                 OBD_FREE_PTR(slave_lmm);
3341
3342         RETURN(rc);
3343 }
3344
3345 /**
3346  * Helper function to declare/execute creation of a striped directory
3347  *
3348  * Called in declare/create object path, prepare striping for a directory
3349  * and prepare defaults data striping for the objects to be created in
3350  * that directory. Notice the function calls "declaration" or "execution"
3351  * methods depending on \a declare param. This is a consequence of the
3352  * current approach while we don't have natural distributed transactions:
3353  * we basically execute non-local updates in the declare phase. So, the
3354  * arguments for the both phases are the same and this is the reason for
3355  * this function to exist.
3356  *
3357  * \param[in] env       execution environment
3358  * \param[in] dt        object
3359  * \param[in] attr      attributes the stripes will be created with
3360  * \param[in] dof       format of stripes (see OSD API description)
3361  * \param[in] th        transaction handle
3362  * \param[in] declare   where to call "declare" or "execute" methods
3363  *
3364  * \retval              0 on success
3365  * \retval              negative if failed
3366  */
3367 static int lod_dir_striping_create_internal(const struct lu_env *env,
3368                                             struct dt_object *dt,
3369                                             struct lu_attr *attr,
3370                                             struct dt_object_format *dof,
3371                                             struct thandle *th,
3372                                             bool declare)
3373 {
3374         struct lod_thread_info *info = lod_env_info(env);
3375         struct lod_object *lo = lod_dt_obj(dt);
3376         const struct lod_default_striping *lds = lo->ldo_def_striping;
3377         int rc;
3378         ENTRY;
3379
3380         LASSERT(ergo(lds != NULL,
3381                      lds->lds_def_striping_set ||
3382                      lds->lds_dir_def_striping_set));
3383
3384         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3385                                  lo->ldo_dir_stripe_offset)) {
3386                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3387                 int stripe_count = lo->ldo_dir_stripe_count;
3388
3389                 if (info->lti_ea_store_size < sizeof(*v1)) {
3390                         rc = lod_ea_store_resize(info, sizeof(*v1));
3391                         if (rc != 0)
3392                                 RETURN(rc);
3393                         v1 = info->lti_ea_store;
3394                 }
3395
3396                 memset(v1, 0, sizeof(*v1));
3397                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3398                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3399                 v1->lum_stripe_offset =
3400                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
3401
3402                 info->lti_buf.lb_buf = v1;
3403                 info->lti_buf.lb_len = sizeof(*v1);
3404
3405                 if (declare)
3406                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
3407                                                        &info->lti_buf, dof, th);
3408                 else
3409                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3410                                                XATTR_NAME_LMV, 0, th);
3411                 if (rc != 0)
3412                         RETURN(rc);
3413         }
3414
3415         /* Transfer default LMV striping from the parent */
3416         if (lds != NULL && lds->lds_dir_def_striping_set &&
3417             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3418                                  lds->lds_dir_def_stripe_offset)) {
3419                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3420
3421                 if (info->lti_ea_store_size < sizeof(*v1)) {
3422                         rc = lod_ea_store_resize(info, sizeof(*v1));
3423                         if (rc != 0)
3424                                 RETURN(rc);
3425                         v1 = info->lti_ea_store;
3426                 }
3427
3428                 memset(v1, 0, sizeof(*v1));
3429                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3430                 v1->lum_stripe_count =
3431                         cpu_to_le32(lds->lds_dir_def_stripe_count);
3432                 v1->lum_stripe_offset =
3433                         cpu_to_le32(lds->lds_dir_def_stripe_offset);
3434                 v1->lum_hash_type =
3435                         cpu_to_le32(lds->lds_dir_def_hash_type);
3436
3437                 info->lti_buf.lb_buf = v1;
3438                 info->lti_buf.lb_len = sizeof(*v1);
3439                 if (declare)
3440                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3441                                                        XATTR_NAME_DEFAULT_LMV,
3442                                                        0, th);
3443                 else
3444                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3445                                                   &info->lti_buf,
3446                                                   XATTR_NAME_DEFAULT_LMV, 0,
3447                                                   th);
3448                 if (rc != 0)
3449                         RETURN(rc);
3450         }
3451
3452         /* Transfer default LOV striping from the parent */
3453         if (lds != NULL && lds->lds_def_striping_set &&
3454             lds->lds_def_comp_cnt != 0) {
3455                 struct lov_mds_md *lmm;
3456                 int lmm_size = lod_comp_md_size(lo, true);
3457
3458                 if (info->lti_ea_store_size < lmm_size) {
3459                         rc = lod_ea_store_resize(info, lmm_size);
3460                         if (rc != 0)
3461                                 RETURN(rc);
3462                 }
3463                 lmm = info->lti_ea_store;
3464
3465                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3466                 if (rc != 0)
3467                         RETURN(rc);
3468
3469                 info->lti_buf.lb_buf = lmm;
3470                 info->lti_buf.lb_len = lmm_size;
3471
3472                 if (declare)
3473                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3474                                                        XATTR_NAME_LOV, 0, th);
3475                 else
3476                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3477                                                       XATTR_NAME_LOV, 0, th);
3478                 if (rc != 0)
3479                         RETURN(rc);
3480         }
3481
3482         RETURN(0);
3483 }
3484
3485 static int lod_declare_dir_striping_create(const struct lu_env *env,
3486                                            struct dt_object *dt,
3487                                            struct lu_attr *attr,
3488                                            struct dt_object_format *dof,
3489                                            struct thandle *th)
3490 {
3491         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3492 }
3493
3494 static int lod_dir_striping_create(const struct lu_env *env,
3495                                    struct dt_object *dt,
3496                                    struct lu_attr *attr,
3497                                    struct dt_object_format *dof,
3498                                    struct thandle *th)
3499 {
3500         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3501 }
3502
3503 /**
3504  * Make LOV EA for striped object.
3505  *
3506  * Generate striping information and store it in the LOV EA of the given
3507  * object. The caller must ensure nobody else is calling the function
3508  * against the object concurrently. The transaction must be started.
3509  * FLDB service must be running as well; it's used to map FID to the target,
3510  * which is stored in LOV EA.
3511  *
3512  * \param[in] env               execution environment for this thread
3513  * \param[in] lo                LOD object
3514  * \param[in] th                transaction handle
3515  *
3516  * \retval                      0 if LOV EA is stored successfully
3517  * \retval                      negative error number on failure
3518  */
3519 static int lod_generate_and_set_lovea(const struct lu_env *env,
3520                                       struct lod_object *lo,
3521                                       struct thandle *th)
3522 {
3523         struct lod_thread_info  *info = lod_env_info(env);
3524         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3525         struct lov_mds_md_v1    *lmm;
3526         int                      rc, lmm_size;
3527         ENTRY;
3528
3529         LASSERT(lo);
3530
3531         if (lo->ldo_comp_cnt == 0) {
3532                 lod_object_free_striping(env, lo);
3533                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3534                 RETURN(rc);
3535         }
3536
3537         lmm_size = lod_comp_md_size(lo, false);
3538         if (info->lti_ea_store_size < lmm_size) {
3539                 rc = lod_ea_store_resize(info, lmm_size);
3540                 if (rc)
3541                         RETURN(rc);
3542         }
3543         lmm = info->lti_ea_store;
3544
3545         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3546         if (rc)
3547                 RETURN(rc);
3548
3549         info->lti_buf.lb_buf = lmm;
3550         info->lti_buf.lb_len = lmm_size;
3551         rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3552                                XATTR_NAME_LOV, 0, th);
3553         RETURN(rc);
3554 }
3555
3556 /**
3557  * Delete layout component(s)
3558  *
3559  * \param[in] env       execution environment for this thread
3560  * \param[in] dt        object
3561  * \param[in] th        transaction handle
3562  *
3563  * \retval      0 on success
3564  * \retval      negative error number on failure
3565  */
3566 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3567                           struct thandle *th)
3568 {
3569         struct lod_layout_component     *lod_comp;
3570         struct lod_object       *lo = lod_dt_obj(dt);
3571         struct dt_object        *next = dt_object_child(dt);
3572         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3573         int     rc, i, j, left;
3574
3575         LASSERT(lo->ldo_is_composite);
3576         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3577
3578         left = lo->ldo_comp_cnt;
3579         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3580                 lod_comp = &lo->ldo_comp_entries[i];
3581
3582                 if (lod_comp->llc_id != LCME_ID_INVAL)
3583                         break;
3584                 left--;
3585
3586                 /* Not instantiated component */
3587                 if (lod_comp->llc_stripe == NULL)
3588                         continue;
3589
3590                 LASSERT(lod_comp->llc_stripe_count > 0);
3591                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
3592                         struct dt_object *obj = lod_comp->llc_stripe[j];
3593
3594                         if (obj == NULL)
3595                                 continue;
3596                         rc = lod_sub_destroy(env, obj, th);
3597                         if (rc)
3598                                 GOTO(out, rc);
3599
3600                         lu_object_put(env, &obj->do_lu);
3601                         lod_comp->llc_stripe[j] = NULL;
3602                 }
3603                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3604                                         lod_comp->llc_stripes_allocated);
3605                 lod_comp->llc_stripe = NULL;
3606                 lod_comp->llc_stripes_allocated = 0;
3607                 lod_obj_set_pool(lo, i, NULL);
3608                 if (lod_comp->llc_ostlist.op_array) {
3609                         OBD_FREE(lod_comp->llc_ostlist.op_array,
3610                                  lod_comp->llc_ostlist.op_size);
3611                         lod_comp->llc_ostlist.op_array = NULL;
3612                         lod_comp->llc_ostlist.op_size = 0;
3613                 }
3614         }
3615
3616         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3617         if (left > 0) {
3618                 struct lod_layout_component     *comp_array;
3619
3620                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3621                 if (comp_array == NULL)
3622                         GOTO(out, rc = -ENOMEM);
3623
3624                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3625                        sizeof(*comp_array) * left);
3626
3627                 OBD_FREE(lo->ldo_comp_entries,
3628                          sizeof(*comp_array) * lo->ldo_comp_cnt);
3629                 lo->ldo_comp_entries = comp_array;
3630                 lo->ldo_comp_cnt = left;
3631
3632                 LASSERT(lo->ldo_mirror_count == 1);
3633                 lo->ldo_mirrors[0].lme_end = left - 1;
3634                 lod_obj_inc_layout_gen(lo);
3635         } else {
3636                 lod_free_comp_entries(lo);
3637         }
3638
3639         LASSERT(dt_object_exists(dt));
3640         rc = dt_attr_get(env, next, attr);
3641         if (rc)
3642                 GOTO(out, rc);
3643
3644         if (attr->la_size > 0) {
3645                 attr->la_size = 0;
3646                 attr->la_valid = LA_SIZE;
3647                 rc = lod_sub_attr_set(env, next, attr, th);
3648                 if (rc)
3649                         GOTO(out, rc);
3650         }
3651
3652         rc = lod_generate_and_set_lovea(env, lo, th);
3653         EXIT;
3654 out:
3655         if (rc)
3656                 lod_object_free_striping(env, lo);
3657         return rc;
3658 }
3659
3660 /**
3661  * Implementation of dt_object_operations::do_xattr_set.
3662  *
3663  * Sets specified extended attribute on the object. Three types of EAs are
3664  * special:
3665  *   LOV EA - stores striping for a regular file or default striping (when set
3666  *            on a directory)
3667  *   LMV EA - stores a marker for the striped directories
3668  *   DMV EA - stores default directory striping
3669  *
3670  * When striping is applied to a non-striped existing object (this is called
3671  * late striping), then LOD notices the caller wants to turn the object into a
3672  * striped one. The stripe objects are created and appropriate EA is set:
3673  * LOV EA storing all the stripes directly or LMV EA storing just a small header
3674  * with striping configuration.
3675  *
3676  * \see dt_object_operations::do_xattr_set() in the API description for details.
3677  */
3678 static int lod_xattr_set(const struct lu_env *env,
3679                          struct dt_object *dt, const struct lu_buf *buf,
3680                          const char *name, int fl, struct thandle *th)
3681 {
3682         struct dt_object        *next = dt_object_child(dt);
3683         int                      rc;
3684         ENTRY;
3685
3686         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3687             strcmp(name, XATTR_NAME_LMV) == 0) {
3688                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3689
3690                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3691                                                 LMV_HASH_FLAG_MIGRATION)
3692                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3693                 else
3694                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3695
3696                 RETURN(rc);
3697         }
3698
3699         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3700             strcmp(name, XATTR_NAME_LOV) == 0) {
3701                 /* default LOVEA */
3702                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3703                 RETURN(rc);
3704         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3705                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3706                 /* default LMVEA */
3707                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3708                                                       th);
3709                 RETURN(rc);
3710         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3711                    (!strcmp(name, XATTR_NAME_LOV) ||
3712                     !strncmp(name, XATTR_LUSTRE_LOV,
3713                              strlen(XATTR_LUSTRE_LOV)))) {
3714                 /* in case of lov EA swap, just set it
3715                  * if not, it is a replay so check striping match what we
3716                  * already have during req replay, declare_xattr_set()
3717                  * defines striping, then create() does the work */
3718                 if (fl & LU_XATTR_REPLACE) {
3719                         /* free stripes, then update disk */
3720                         lod_object_free_striping(env, lod_dt_obj(dt));
3721
3722                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3723                 } else if (dt_object_remote(dt)) {
3724                         /* This only happens during migration, see
3725                          * mdd_migrate_create(), in which Master MDT will
3726                          * create a remote target object, and only set
3727                          * (migrating) stripe EA on the remote object,
3728                          * and does not need creating each stripes. */
3729                         rc = lod_sub_xattr_set(env, next, buf, name,
3730                                                       fl, th);
3731                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3732                         /* delete component(s) */
3733                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3734                         rc = lod_layout_del(env, dt, th);
3735                 } else {
3736                         /*
3737                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3738                          * it's going to create create file with specified
3739                          * component(s), the striping must have not being
3740                          * cached in this case;
3741                          *
3742                          * Otherwise, it's going to add/change component(s) to
3743                          * an existing file, the striping must have been cached
3744                          * in this case.
3745                          */
3746                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3747                                      !strcmp(name, XATTR_NAME_LOV),
3748                                 !lod_dt_obj(dt)->ldo_comp_cached));
3749
3750                         rc = lod_striped_create(env, dt, NULL, NULL, th);
3751                 }
3752                 RETURN(rc);
3753         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3754                 rc = lod_replace_parent_fid(env, dt, th, false);
3755
3756                 RETURN(rc);
3757         }
3758
3759         /* then all other xattr */
3760         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3761
3762         RETURN(rc);
3763 }
3764
3765 /**
3766  * Implementation of dt_object_operations::do_declare_xattr_del.
3767  *
3768  * \see dt_object_operations::do_declare_xattr_del() in the API description
3769  * for details.
3770  */
3771 static int lod_declare_xattr_del(const struct lu_env *env,
3772                                  struct dt_object *dt, const char *name,
3773                                  struct thandle *th)
3774 {
3775         struct lod_object       *lo = lod_dt_obj(dt);
3776         int                     rc;
3777         int                     i;
3778         ENTRY;
3779
3780         rc = lod_sub_declare_xattr_del(env, dt_object_child(dt), name, th);
3781         if (rc != 0)
3782                 RETURN(rc);
3783
3784         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3785                 RETURN(0);
3786
3787         /* set xattr to each stripes, if needed */
3788         rc = lod_load_striping(env, lo);
3789         if (rc != 0)
3790                 RETURN(rc);
3791
3792         if (lo->ldo_dir_stripe_count == 0)
3793                 RETURN(0);
3794
3795         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3796                 LASSERT(lo->ldo_stripe[i]);
3797                 rc = lod_sub_declare_xattr_del(env, lo->ldo_stripe[i],
3798                                                name, th);
3799                 if (rc != 0)
3800                         break;
3801         }
3802
3803         RETURN(rc);
3804 }
3805
3806 /**
3807  * Implementation of dt_object_operations::do_xattr_del.
3808  *
3809  * If EA storing a regular striping is being deleted, then release
3810  * all the references to the stripe objects in core.
3811  *
3812  * \see dt_object_operations::do_xattr_del() in the API description for details.
3813  */
3814 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3815                          const char *name, struct thandle *th)
3816 {
3817         struct dt_object        *next = dt_object_child(dt);
3818         struct lod_object       *lo = lod_dt_obj(dt);
3819         int                     rc;
3820         int                     i;
3821         ENTRY;
3822
3823         if (!strcmp(name, XATTR_NAME_LOV))
3824                 lod_object_free_striping(env, lod_dt_obj(dt));
3825
3826         rc = lod_sub_xattr_del(env, next, name, th);
3827         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3828                 RETURN(rc);
3829
3830         if (lo->ldo_dir_stripe_count == 0)
3831                 RETURN(0);
3832
3833         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3834                 LASSERT(lo->ldo_stripe[i]);
3835
3836                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3837                 if (rc != 0)
3838                         break;
3839         }
3840
3841         RETURN(rc);
3842 }
3843
3844 /**
3845  * Implementation of dt_object_operations::do_xattr_list.
3846  *
3847  * \see dt_object_operations::do_xattr_list() in the API description
3848  * for details.
3849  */
3850 static int lod_xattr_list(const struct lu_env *env,
3851                           struct dt_object *dt, const struct lu_buf *buf)
3852 {
3853         return dt_xattr_list(env, dt_object_child(dt), buf);
3854 }
3855
3856 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3857 {
3858         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3859 }
3860
3861
3862 /**
3863  * Get default striping.
3864  *
3865  * \param[in] env               execution environment
3866  * \param[in] lo                object
3867  * \param[out] lds              default striping
3868  *
3869  * \retval              0 on success
3870  * \retval              negative if failed
3871  */
3872 static int lod_get_default_lov_striping(const struct lu_env *env,
3873                                         struct lod_object *lo,
3874                                         struct lod_default_striping *lds)
3875 {
3876         struct lod_thread_info *info = lod_env_info(env);
3877         struct lov_user_md_v1 *v1 = NULL;
3878         struct lov_user_md_v3 *v3 = NULL;
3879         struct lov_comp_md_v1 *comp_v1 = NULL;
3880         __u16   comp_cnt;
3881         __u16   mirror_cnt;
3882         bool    composite;
3883         int     rc, i;
3884         ENTRY;
3885
3886         lds->lds_def_striping_set = 0;
3887
3888         rc = lod_get_lov_ea(env, lo);
3889         if (rc < 0)
3890                 RETURN(rc);
3891
3892         if (rc < (typeof(rc))sizeof(struct lov_user_md))
3893                 RETURN(0);
3894
3895         v1 = info->lti_ea_store;
3896         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3897                 lustre_swab_lov_user_md_v1(v1);
3898         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3899                 v3 = (struct lov_user_md_v3 *)v1;
3900                 lustre_swab_lov_user_md_v3(v3);
3901         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3902                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3903                 lustre_swab_lov_comp_md_v1(comp_v1);
3904         }
3905
3906         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3907             v1->lmm_magic != LOV_MAGIC_COMP_V1)
3908                 RETURN(-ENOTSUPP);
3909
3910         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3911                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3912                 comp_cnt = comp_v1->lcm_entry_count;
3913                 if (comp_cnt == 0)
3914                         RETURN(-EINVAL);
3915                 mirror_cnt = comp_v1->lcm_mirror_count + 1;
3916                 composite = true;
3917         } else {
3918                 comp_cnt = 1;
3919                 mirror_cnt = 0;
3920                 composite = false;
3921         }
3922
3923         /* realloc default comp entries if necessary */
3924         rc = lod_def_striping_comp_resize(lds, comp_cnt);
3925         if (rc < 0)
3926                 RETURN(rc);
3927
3928         lds->lds_def_comp_cnt = comp_cnt;
3929         lds->lds_def_striping_is_composite = composite;
3930         lds->lds_def_mirror_cnt = mirror_cnt;
3931
3932         for (i = 0; i < comp_cnt; i++) {
3933                 struct lod_layout_component *lod_comp;
3934                 struct lu_extent *ext;
3935                 char *pool;
3936
3937                 lod_comp = &lds->lds_def_comp_entries[i];
3938                 /*
3939                  * reset lod_comp values, llc_stripes is always NULL in
3940                  * the default striping template, llc_pool will be reset
3941                  * later below.
3942                  */
3943                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3944
3945                 if (composite) {
3946                         v1 = (struct lov_user_md *)((char *)comp_v1 +
3947                                         comp_v1->lcm_entries[i].lcme_offset);
3948                         ext = &comp_v1->lcm_entries[i].lcme_extent;
3949                         lod_comp->llc_extent = *ext;
3950                 }
3951
3952                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3953                     v1->lmm_pattern != LOV_PATTERN_MDT &&
3954                     v1->lmm_pattern != 0) {
3955                         lod_free_def_comp_entries(lds);
3956                         RETURN(-EINVAL);
3957                 }
3958
3959                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3960                        "stripe_offset=%d\n",
3961                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3962                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3963                        (int)v1->lmm_stripe_offset);
3964
3965                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
3966                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3967                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3968                 lod_comp->llc_pattern = v1->lmm_pattern;
3969
3970                 pool = NULL;
3971                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3972                         /* XXX: sanity check here */
3973                         v3 = (struct lov_user_md_v3 *) v1;
3974                         if (v3->lmm_pool_name[0] != '\0')
3975                                 pool = v3->lmm_pool_name;
3976                 }
3977                 lod_set_def_pool(lds, i, pool);
3978         }
3979
3980         lds->lds_def_striping_set = 1;
3981         RETURN(rc);
3982 }
3983
3984 /**
3985  * Get default directory striping.
3986  *
3987  * \param[in] env               execution environment
3988  * \param[in] lo                object
3989  * \param[out] lds              default striping
3990  *
3991  * \retval              0 on success
3992  * \retval              negative if failed
3993  */
3994 static int lod_get_default_lmv_striping(const struct lu_env *env,
3995                                         struct lod_object *lo,
3996                                         struct lod_default_striping *lds)
3997 {
3998         struct lod_thread_info  *info = lod_env_info(env);
3999         struct lmv_user_md_v1   *v1 = NULL;
4000         int                      rc;
4001         ENTRY;
4002
4003         lds->lds_dir_def_striping_set = 0;
4004         rc = lod_get_default_lmv_ea(env, lo);
4005         if (rc < 0)
4006                 RETURN(rc);
4007
4008         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4009                 RETURN(0);
4010
4011         v1 = info->lti_ea_store;
4012
4013         lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4014         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4015         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4016         lds->lds_dir_def_striping_set = 1;
4017
4018         RETURN(0);
4019 }
4020
4021 /**
4022  * Get default striping in the object.
4023  *
4024  * Get object default striping and default directory striping.
4025  *
4026  * \param[in] env               execution environment
4027  * \param[in] lo                object
4028  * \param[out] lds              default striping
4029  *
4030  * \retval              0 on success
4031  * \retval              negative if failed
4032  */
4033 static int lod_get_default_striping(const struct lu_env *env,
4034                                     struct lod_object *lo,
4035                                     struct lod_default_striping *lds)
4036 {
4037         int rc, rc1;
4038
4039         rc = lod_get_default_lov_striping(env, lo, lds);
4040         rc1 = lod_get_default_lmv_striping(env, lo, lds);
4041         if (rc == 0 && rc1 < 0)
4042                 rc = rc1;
4043
4044         return rc;
4045 }
4046
4047 /**
4048  * Apply default striping on object.
4049  *
4050  * If object striping pattern is not set, set to the one in default striping.
4051  * The default striping is from parent or fs.
4052  *
4053  * \param[in] lo                new object
4054  * \param[in] lds               default striping
4055  * \param[in] mode              new object's mode
4056  */
4057 static void lod_striping_from_default(struct lod_object *lo,
4058                                       const struct lod_default_striping *lds,
4059                                       umode_t mode)
4060 {
4061         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4062         struct lov_desc *desc = &d->lod_desc;
4063         int i, rc;
4064
4065         if (lds->lds_def_striping_set && S_ISREG(mode)) {
4066                 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4067                                             lds->lds_def_comp_cnt);
4068                 if (rc != 0)
4069                         return;
4070
4071                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4072                 if (lds->lds_def_mirror_cnt > 1)
4073                         lo->ldo_flr_state = LCM_FL_RDONLY;
4074
4075                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4076                         struct lod_layout_component *obj_comp =
4077                                                 &lo->ldo_comp_entries[i];
4078                         struct lod_layout_component *def_comp =
4079                                                 &lds->lds_def_comp_entries[i];
4080
4081                         CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
4082                                "nr:%u offset:%u pattern %#x %s\n",
4083                                def_comp->llc_stripe_size,
4084                                def_comp->llc_stripe_count,
4085                                def_comp->llc_stripe_offset,
4086                                def_comp->llc_pattern,
4087                                def_comp->llc_pool ?: "");
4088
4089                         *obj_comp = *def_comp;
4090                         if (def_comp->llc_pool != NULL) {
4091                                 /* pointer was copied from def_comp */
4092                                 obj_comp->llc_pool = NULL;
4093                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4094                         }
4095
4096                         /*
4097                          * Don't initialize these fields for plain layout
4098                          * (v1/v3) here, they are inherited in the order of
4099                          * 'parent' -> 'fs default (root)' -> 'global default
4100                          * values for stripe_count & stripe_size'.
4101                          *
4102                          * see lod_ah_init().
4103                          */
4104                         if (!lo->ldo_is_composite)
4105                                 continue;
4106
4107                         if (obj_comp->llc_stripe_count <= 0 &&
4108                             obj_comp->llc_pattern != LOV_PATTERN_MDT)
4109                                 obj_comp->llc_stripe_count =
4110                                         desc->ld_default_stripe_count;
4111                         if (obj_comp->llc_stripe_size <= 0)
4112                                 obj_comp->llc_stripe_size =
4113                                         desc->ld_default_stripe_size;
4114                 }
4115         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4116                 if (lo->ldo_dir_stripe_count == 0)
4117                         lo->ldo_dir_stripe_count =
4118                                 lds->lds_dir_def_stripe_count;
4119                 if (lo->ldo_dir_stripe_offset == -1)
4120                         lo->ldo_dir_stripe_offset =
4121                                 lds->lds_dir_def_stripe_offset;
4122                 if (lo->ldo_dir_hash_type == 0)
4123                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4124
4125                 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4126                        "offset:%u, hash_type:%u\n",
4127                        lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4128                        lo->ldo_dir_hash_type);
4129         }
4130 }
4131
4132 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4133 {
4134         struct lod_layout_component *lod_comp;
4135
4136         if (lo->ldo_comp_cnt == 0)
4137                 return true;
4138
4139         if (lo->ldo_is_composite)
4140                 return false;
4141
4142         lod_comp = &lo->ldo_comp_entries[0];
4143
4144         if (lod_comp->llc_stripe_count <= 0 ||
4145             lod_comp->llc_stripe_size <= 0)
4146                 return true;
4147
4148         if (from_root && (lod_comp->llc_pool == NULL ||
4149                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4150                 return true;
4151
4152         return false;
4153 }
4154
4155 /**
4156  * Implementation of dt_object_operations::do_ah_init.
4157  *
4158  * This method is used to make a decision on the striping configuration for the
4159  * object being created. It can be taken from the \a parent object if it exists,
4160  * or filesystem's default. The resulting configuration (number of stripes,
4161  * stripe size/offset, pool name, etc) is stored in the object itself and will
4162  * be used by the methods like ->doo_declare_create().
4163  *
4164  * \see dt_object_operations::do_ah_init() in the API description for details.
4165  */
4166 static void lod_ah_init(const struct lu_env *env,
4167                         struct dt_allocation_hint *ah,
4168                         struct dt_object *parent,
4169                         struct dt_object *child,
4170                         umode_t child_mode)
4171 {
4172         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4173         struct lod_thread_info *info = lod_env_info(env);
4174         struct lod_default_striping *lds = &info->lti_def_striping;
4175         struct dt_object *nextp = NULL;
4176         struct dt_object *nextc;
4177         struct lod_object *lp = NULL;
4178         struct lod_object *lc;
4179         struct lov_desc *desc;
4180         struct lod_layout_component *lod_comp;
4181         int rc;
4182         ENTRY;
4183
4184         LASSERT(child);
4185
4186         if (likely(parent)) {
4187                 nextp = dt_object_child(parent);
4188                 lp = lod_dt_obj(parent);
4189         }
4190
4191         nextc = dt_object_child(child);
4192         lc = lod_dt_obj(child);
4193
4194         LASSERT(!lod_obj_is_striped(child));
4195         /* default layout template may have been set on the regular file
4196          * when this is called from mdd_create_data() */
4197         if (S_ISREG(child_mode))
4198                 lod_free_comp_entries(lc);
4199
4200         if (!dt_object_exists(nextc))
4201                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4202
4203         if (S_ISDIR(child_mode)) {
4204                 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4205
4206                 /* other default values are 0 */
4207                 lc->ldo_dir_stripe_offset = -1;
4208
4209                 /* get default striping from parent object */
4210                 if (likely(lp != NULL))
4211                         lod_get_default_striping(env, lp, lds);
4212
4213                 /* set child default striping info, default value is NULL */
4214                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4215                         lc->ldo_def_striping = lds;
4216
4217                 /* It should always honour the specified stripes */
4218                 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4219                  * will have old magic. In this case, we should ignore the
4220                  * stripe count and try to create dir by default stripe.
4221                  */
4222                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4223                     le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC) {
4224                         lc->ldo_dir_stripe_count =
4225                                 le32_to_cpu(lum1->lum_stripe_count);
4226                         lc->ldo_dir_stripe_offset =
4227                                 le32_to_cpu(lum1->lum_stripe_offset);
4228                         lc->ldo_dir_hash_type =
4229                                 le32_to_cpu(lum1->lum_hash_type);
4230                         CDEBUG(D_INFO,
4231                                "set dirstripe: count %hu, offset %d, hash %u\n",
4232                                 lc->ldo_dir_stripe_count,
4233                                 (int)lc->ldo_dir_stripe_offset,
4234                                 lc->ldo_dir_hash_type);
4235                 } else {
4236                         /* transfer defaults LMV to new directory */
4237                         lod_striping_from_default(lc, lds, child_mode);
4238                 }
4239
4240                 /* shrink the stripe_count to the avaible MDT count */
4241                 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4242                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
4243                         lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4244
4245                 /* Directory will be striped only if stripe_count > 1, if
4246                  * stripe_count == 1, let's reset stripe_count = 0 to avoid
4247                  * create single master stripe and also help to unify the
4248                  * stripe handling of directories and files */
4249                 if (lc->ldo_dir_stripe_count == 1)
4250                         lc->ldo_dir_stripe_count = 0;
4251
4252                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4253                        lc->ldo_dir_stripe_count,
4254                        (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4255
4256                 RETURN_EXIT;
4257         }
4258
4259         /* child object regular file*/
4260
4261         if (!lod_object_will_be_striped(S_ISREG(child_mode),
4262                                         lu_object_fid(&child->do_lu)))
4263                 RETURN_EXIT;
4264
4265         /* If object is going to be striped over OSTs, transfer default
4266          * striping information to the child, so that we can use it
4267          * during declaration and creation.
4268          *
4269          * Try from the parent first.
4270          */
4271         if (likely(lp != NULL)) {
4272                 rc = lod_get_default_lov_striping(env, lp, lds);
4273                 if (rc == 0)
4274                         lod_striping_from_default(lc, lds, child_mode);
4275         }
4276
4277         /* Initialize lod_device::lod_md_root object reference */
4278         if (d->lod_md_root == NULL) {
4279                 struct dt_object *root;
4280                 struct lod_object *lroot;
4281
4282                 lu_root_fid(&info->lti_fid);
4283                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4284                 if (!IS_ERR(root)) {
4285                         lroot = lod_dt_obj(root);
4286
4287                         spin_lock(&d->lod_lock);
4288                         if (d->lod_md_root != NULL)
4289                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
4290                         d->lod_md_root = lroot;
4291                         spin_unlock(&d->lod_lock);
4292                 }
4293         }
4294
4295         /* try inherit layout from the root object (fs default) when:
4296          *  - parent does not have default layout; or
4297          *  - parent has plain(v1/v3) default layout, and some attributes
4298          *    are not specified in the default layout;
4299          */
4300         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4301                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4302                 if (rc)
4303                         goto out;
4304                 if (lc->ldo_comp_cnt == 0) {
4305                         lod_striping_from_default(lc, lds, child_mode);
4306                 } else if (!lds->lds_def_striping_is_composite) {
4307                         struct lod_layout_component *def_comp;
4308
4309                         LASSERT(!lc->ldo_is_composite);
4310                         lod_comp = &lc->ldo_comp_entries[0];
4311                         def_comp = &lds->lds_def_comp_entries[0];
4312
4313                         if (lod_comp->llc_stripe_count <= 0)
4314                                 lod_comp->llc_stripe_count =
4315                                         def_comp->llc_stripe_count;
4316                         if (lod_comp->llc_stripe_size <= 0)
4317                                 lod_comp->llc_stripe_size =
4318                                         def_comp->llc_stripe_size;
4319                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
4320                                 lod_comp->llc_stripe_offset =
4321                                         def_comp->llc_stripe_offset;
4322                         if (lod_comp->llc_pool == NULL)
4323                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4324                 }
4325         }
4326 out:
4327         /*
4328          * fs default striping may not be explicitly set, or historically set
4329          * in config log, use them.
4330          */
4331         if (lod_need_inherit_more(lc, false)) {
4332                 if (lc->ldo_comp_cnt == 0) {
4333                         rc = lod_alloc_comp_entries(lc, 0, 1);
4334                         if (rc)
4335                                 /* fail to allocate memory, will create a
4336                                  * non-striped file. */
4337                                 RETURN_EXIT;
4338                         lc->ldo_is_composite = 0;
4339                         lod_comp = &lc->ldo_comp_entries[0];
4340                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4341                 }
4342                 LASSERT(!lc->ldo_is_composite);
4343                 lod_comp = &lc->ldo_comp_entries[0];
4344                 desc = &d->lod_desc;
4345                 if (lod_comp->llc_stripe_count <= 0)
4346                         lod_comp->llc_stripe_count =
4347                                 desc->ld_default_stripe_count;
4348                 if (lod_comp->llc_stripe_size <= 0)
4349                         lod_comp->llc_stripe_size =
4350                                 desc->ld_default_stripe_size;
4351         }
4352
4353         EXIT;
4354 }
4355
4356 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
4357 /**
4358  * Size initialization on late striping.
4359  *
4360  * Propagate the size of a truncated object to a deferred striping.
4361  * This function handles a special case when truncate was done on a
4362  * non-striped object and now while the striping is being created
4363  * we can't lose that size, so we have to propagate it to the stripes
4364  * being created.
4365  *
4366  * \param[in] env       execution environment
4367  * \param[in] dt        object
4368  * \param[in] th        transaction handle
4369  *
4370  * \retval              0 on success
4371  * \retval              negative if failed
4372  */
4373 static int lod_declare_init_size(const struct lu_env *env,
4374                                  struct dt_object *dt, struct thandle *th)
4375 {
4376         struct dt_object        *next = dt_object_child(dt);
4377         struct lod_object       *lo = lod_dt_obj(dt);
4378         struct dt_object        **objects = NULL;
4379         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
4380         uint64_t        size, offs;
4381         int     i, rc, stripe, stripe_count = 0, stripe_size = 0;
4382         struct lu_extent size_ext;
4383         ENTRY;
4384
4385         if (!lod_obj_is_striped(dt))
4386                 RETURN(0);
4387
4388         rc = dt_attr_get(env, next, attr);
4389         LASSERT(attr->la_valid & LA_SIZE);
4390         if (rc)
4391                 RETURN(rc);
4392
4393         size = attr->la_size;
4394         if (size == 0)
4395                 RETURN(0);
4396
4397         size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4398         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4399                 struct lod_layout_component *lod_comp;
4400                 struct lu_extent *extent;
4401
4402                 lod_comp = &lo->ldo_comp_entries[i];
4403
4404                 if (lod_comp->llc_stripe == NULL)
4405                         continue;
4406
4407                 extent = &lod_comp->llc_extent;
4408                 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4409                 if (!lo->ldo_is_composite ||
4410                     lu_extent_is_overlapped(extent, &size_ext)) {
4411                         objects = lod_comp->llc_stripe;
4412                         stripe_count = lod_comp->llc_stripe_count;
4413                         stripe_size = lod_comp->llc_stripe_size;
4414
4415                         /* next mirror */
4416                         if (stripe_count == 0)
4417                                 continue;
4418
4419                         LASSERT(objects != NULL && stripe_size != 0);
4420                         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4421                         ll_do_div64(size, (__u64)stripe_size);
4422                         stripe = ll_do_div64(size, (__u64)stripe_count);
4423                         LASSERT(objects[stripe] != NULL);
4424
4425                         size = size * stripe_size;
4426                         offs = attr->la_size;
4427                         size += ll_do_div64(offs, stripe_size);
4428
4429                         attr->la_valid = LA_SIZE;
4430                         attr->la_size = size;
4431
4432                         rc = lod_sub_declare_attr_set(env, objects[stripe],
4433                                                       attr, th);
4434                 }
4435         }
4436
4437         RETURN(rc);
4438 }
4439
4440 /**
4441  * Declare creation of striped object.
4442  *
4443  * The function declares creation stripes for a regular object. The function
4444  * also declares whether the stripes will be created with non-zero size if
4445  * previously size was set non-zero on the master object. If object \a dt is
4446  * not local, then only fully defined striping can be applied in \a lovea.
4447  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4448  * for the details.
4449  *
4450  * \param[in] env       execution environment
4451  * \param[in] dt        object
4452  * \param[in] attr      attributes the stripes will be created with
4453  * \param[in] lovea     a buffer containing striping description
4454  * \param[in] th        transaction handle
4455  *
4456  * \retval              0 on success
4457  * \retval              negative if failed
4458  */
4459 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
4460                                struct lu_attr *attr,
4461                                const struct lu_buf *lovea, struct thandle *th)
4462 {
4463         struct lod_thread_info  *info = lod_env_info(env);
4464         struct dt_object        *next = dt_object_child(dt);
4465         struct lod_object       *lo = lod_dt_obj(dt);
4466         int                      rc;
4467         ENTRY;
4468
4469         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4470                 GOTO(out, rc = -ENOMEM);
4471
4472         if (!dt_object_remote(next)) {
4473                 /* choose OST and generate appropriate objects */
4474                 rc = lod_prepare_create(env, lo, attr, lovea, th);
4475                 if (rc)
4476                         GOTO(out, rc);
4477
4478                 /*
4479                  * declare storage for striping data
4480                  */
4481                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4482         } else {
4483                 /* LOD can not choose OST objects for remote objects, i.e.
4484                  * stripes must be ready before that. Right now, it can only
4485                  * happen during migrate, i.e. migrate process needs to create
4486                  * remote regular file (mdd_migrate_create), then the migrate
4487                  * process will provide stripeEA. */
4488                 LASSERT(lovea != NULL);
4489                 info->lti_buf = *lovea;
4490         }
4491
4492         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
4493                                        XATTR_NAME_LOV, 0, th);
4494         if (rc)
4495                 GOTO(out, rc);
4496
4497         /*
4498          * if striping is created with local object's size > 0,
4499          * we have to propagate this size to specific object
4500          * the case is possible only when local object was created previously
4501          */
4502         if (dt_object_exists(next))
4503                 rc = lod_declare_init_size(env, dt, th);
4504
4505 out:
4506         /* failed to create striping or to set initial size, let's reset
4507          * config so that others don't get confused */
4508         if (rc)
4509                 lod_object_free_striping(env, lo);
4510
4511         RETURN(rc);
4512 }
4513
4514 /**
4515  * Implementation of dt_object_operations::do_declare_create.
4516  *
4517  * The method declares creation of a new object. If the object will be striped,
4518  * then helper functions are called to find FIDs for the stripes, declare
4519  * creation of the stripes and declare initialization of the striping
4520  * information to be stored in the master object.
4521  *
4522  * \see dt_object_operations::do_declare_create() in the API description
4523  * for details.
4524  */
4525 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
4526                               struct lu_attr *attr,
4527                               struct dt_allocation_hint *hint,
4528                               struct dt_object_format *dof, struct thandle *th)
4529 {
4530         struct dt_object   *next = dt_object_child(dt);
4531         struct lod_object  *lo = lod_dt_obj(dt);
4532         int                 rc;
4533         ENTRY;
4534
4535         LASSERT(dof);
4536         LASSERT(attr);
4537         LASSERT(th);
4538
4539         /*
4540          * first of all, we declare creation of local object
4541          */
4542         rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
4543         if (rc != 0)
4544                 GOTO(out, rc);
4545
4546         /*
4547          * it's lod_ah_init() that has decided the object will be striped
4548          */
4549         if (dof->dof_type == DFT_REGULAR) {
4550                 /* callers don't want stripes */
4551                 /* XXX: all tricky interactions with ->ah_make_hint() decided
4552                  * to use striping, then ->declare_create() behaving differently
4553                  * should be cleaned */
4554                 if (dof->u.dof_reg.striped != 0)
4555                         rc = lod_declare_striped_create(env, dt, attr,
4556                                                         NULL, th);
4557         } else if (dof->dof_type == DFT_DIR) {
4558                 struct seq_server_site *ss;
4559
4560                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4561
4562                 /* If the parent has default stripeEA, and client
4563                  * did not find it before sending create request,
4564                  * then MDT will return -EREMOTE, and client will
4565                  * retrieve the default stripeEA and re-create the
4566                  * sub directory.
4567                  *
4568                  * Note: if dah_eadata != NULL, it means creating the
4569                  * striped directory with specified stripeEA, then it
4570                  * should ignore the default stripeEA */
4571                 if (hint != NULL && hint->dah_eadata == NULL) {
4572                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4573                                 GOTO(out, rc = -EREMOTE);
4574
4575                         if (lo->ldo_dir_stripe_offset == -1) {
4576                                 /* child and parent should be in the same MDT */
4577                                 if (hint->dah_parent != NULL &&
4578                                     dt_object_remote(hint->dah_parent))
4579                                         GOTO(out, rc = -EREMOTE);
4580                         } else if (lo->ldo_dir_stripe_offset !=
4581                                    ss->ss_node_id) {
4582                                 struct lod_device *lod;
4583                                 struct lod_tgt_descs *ltd;
4584                                 struct lod_tgt_desc *tgt = NULL;
4585                                 bool found_mdt = false;
4586                                 int i;
4587
4588                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4589                                 ltd = &lod->lod_mdt_descs;
4590                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4591                                         tgt = LTD_TGT(ltd, i);
4592                                         if (tgt->ltd_index ==
4593                                                 lo->ldo_dir_stripe_offset) {
4594                                                 found_mdt = true;
4595                                                 break;
4596                                         }
4597                                 }
4598
4599                                 /* If the MDT indicated by stripe_offset can be
4600                                  * found, then tell client to resend the create
4601                                  * request to the correct MDT, otherwise return
4602                                  * error to client */
4603                                 if (found_mdt)
4604                                         GOTO(out, rc = -EREMOTE);
4605                                 else
4606                                         GOTO(out, rc = -EINVAL);
4607                         }
4608                 }
4609
4610                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4611         }
4612 out:
4613         /* failed to create striping or to set initial size, let's reset
4614          * config so that others don't get confused */
4615         if (rc)
4616                 lod_object_free_striping(env, lo);
4617         RETURN(rc);
4618 }
4619
4620 /**
4621  * Generate component ID for new created component.
4622  *
4623  * \param[in] lo                LOD object
4624  * \param[in] comp_idx          index of ldo_comp_entries
4625  *
4626  * \retval                      component ID on success
4627  * \retval                      LCME_ID_INVAL on failure
4628  */
4629 static __u32 lod_gen_component_id(struct lod_object *lo,
4630                                   int mirror_id, int comp_idx)
4631 {
4632         struct lod_layout_component *lod_comp;
4633         __u32   id, start, end;
4634         int     i;
4635
4636         LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
4637
4638         lod_obj_inc_layout_gen(lo);
4639         id = lo->ldo_layout_gen;
4640         if (likely(id <= SEQ_ID_MAX))
4641                 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
4642
4643         /* Layout generation wraps, need to check collisions. */
4644         start = id & SEQ_ID_MASK;
4645         end = SEQ_ID_MAX;
4646 again:
4647         for (id = start; id <= end; id++) {
4648                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4649                         lod_comp = &lo->ldo_comp_entries[i];
4650                         if (pflr_id(mirror_id, id) == lod_comp->llc_id)
4651                                 break;
4652                 }
4653                 /* Found the ununsed ID */
4654                 if (i == lo->ldo_comp_cnt)
4655                         RETURN(pflr_id(mirror_id, id));
4656         }
4657         if (end == LCME_ID_MAX) {
4658                 start = 1;
4659                 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
4660                           (__u32)(LCME_ID_MAX - 1));
4661                 goto again;
4662         }
4663
4664         RETURN(LCME_ID_INVAL);
4665 }
4666
4667 /**
4668  * Creation of a striped regular object.
4669  *
4670  * The function is called to create the stripe objects for a regular
4671  * striped file. This can happen at the initial object creation or
4672  * when the caller asks LOD to do so using ->do_xattr_set() method
4673  * (so called late striping). Notice all the information are already
4674  * prepared in the form of the list of objects (ldo_stripe field).
4675  * This is done during declare phase.
4676  *
4677  * \param[in] env       execution environment
4678  * \param[in] dt        object
4679  * \param[in] attr      attributes the stripes will be created with
4680  * \param[in] dof       format of stripes (see OSD API description)
4681  * \param[in] th        transaction handle
4682  *
4683  * \retval              0 on success
4684  * \retval              negative if failed
4685  */
4686 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
4687                        struct lu_attr *attr, struct dt_object_format *dof,
4688                        struct thandle *th)
4689 {
4690         struct lod_layout_component     *lod_comp;
4691         struct lod_object       *lo = lod_dt_obj(dt);
4692         __u16   mirror_id;
4693         int     rc = 0, i, j;
4694         ENTRY;
4695
4696         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4697
4698         mirror_id = lo->ldo_mirror_count > 1 ? 1 : 0;
4699
4700         /* create all underlying objects */
4701         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4702                 lod_comp = &lo->ldo_comp_entries[i];
4703
4704                 if (lod_comp->llc_extent.e_start == 0 && i > 0) /* new mirror */
4705                         ++mirror_id;
4706
4707                 if (lod_comp->llc_id == LCME_ID_INVAL) {
4708                         lod_comp->llc_id = lod_gen_component_id(lo,
4709                                                                 mirror_id, i);
4710                         if (lod_comp->llc_id == LCME_ID_INVAL)
4711                                 GOTO(out, rc = -ERANGE);
4712                 }
4713
4714                 if (lod_comp_inited(lod_comp))
4715                         continue;
4716
4717                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4718                         lod_comp_set_init(lod_comp);
4719
4720                 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
4721                         lod_comp_set_init(lod_comp);
4722
4723                 if (lod_comp->llc_stripe == NULL)
4724                         continue;
4725
4726                 LASSERT(lod_comp->llc_stripe_count);
4727                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4728                         struct dt_object *object = lod_comp->llc_stripe[j];
4729                         LASSERT(object != NULL);
4730                         rc = lod_sub_create(env, object, attr, NULL, dof, th);
4731                         if (rc)
4732                                 GOTO(out, rc);
4733                 }
4734                 lod_comp_set_init(lod_comp);
4735         }
4736
4737         rc = lod_fill_mirrors(lo);
4738         if (rc)
4739                 GOTO(out, rc);
4740
4741         rc = lod_generate_and_set_lovea(env, lo, th);
4742         if (rc)
4743                 GOTO(out, rc);
4744
4745         lo->ldo_comp_cached = 1;
4746         RETURN(0);
4747
4748 out:
4749         lod_object_free_striping(env, lo);
4750         RETURN(rc);
4751 }
4752
4753 /**
4754  * Implementation of dt_object_operations::do_create.
4755  *
4756  * If any of preceeding methods (like ->do_declare_create(),
4757  * ->do_ah_init(), etc) chose to create a striped object,
4758  * then this method will create the master and the stripes.
4759  *
4760  * \see dt_object_operations::do_create() in the API description for details.
4761  */
4762 static int lod_create(const struct lu_env *env, struct dt_object *dt,
4763                       struct lu_attr *attr, struct dt_allocation_hint *hint,
4764                       struct dt_object_format *dof, struct thandle *th)
4765 {
4766         int                 rc;
4767         ENTRY;
4768
4769         /* create local object */
4770         rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
4771         if (rc != 0)
4772                 RETURN(rc);
4773
4774         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4775             lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4776                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4777                 rc = lod_striped_create(env, dt, attr, dof, th);
4778         }
4779
4780         RETURN(rc);
4781 }
4782
4783 static inline int
4784 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4785                           struct dt_object *dt, struct thandle *th,
4786                           int comp_idx, int stripe_idx,
4787                           struct lod_obj_stripe_cb_data *data)
4788 {
4789         if (data->locd_declare)
4790                 return lod_sub_declare_destroy(env, dt, th);
4791         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4792                  stripe_idx == cfs_fail_val)
4793                 return lod_sub_destroy(env, dt, th);
4794         else
4795                 return 0;
4796 }
4797
4798 /**
4799  * Implementation of dt_object_operations::do_declare_destroy.
4800  *
4801  * If the object is a striped directory, then the function declares reference
4802  * removal from the master object (this is an index) to the stripes and declares
4803  * destroy of all the stripes. In all the cases, it declares an intention to
4804  * destroy the object itself.
4805  *
4806  * \see dt_object_operations::do_declare_destroy() in the API description
4807  * for details.
4808  */
4809 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
4810                                struct thandle *th)
4811 {
4812         struct dt_object   *next = dt_object_child(dt);
4813         struct lod_object  *lo = lod_dt_obj(dt);
4814         struct lod_thread_info *info = lod_env_info(env);
4815         char               *stripe_name = info->lti_key;
4816         int                 rc, i;
4817         ENTRY;
4818
4819         /*
4820          * load striping information, notice we don't do this when object
4821          * is being initialized as we don't need this information till
4822          * few specific cases like destroy, chown
4823          */
4824         rc = lod_load_striping(env, lo);
4825         if (rc)
4826                 RETURN(rc);
4827
4828         /* declare destroy for all underlying objects */
4829         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4830                 rc = next->do_ops->do_index_try(env, next,
4831                                                 &dt_directory_features);
4832                 if (rc != 0)
4833                         RETURN(rc);
4834
4835                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4836                         rc = lod_sub_declare_ref_del(env, next, th);
4837                         if (rc != 0)
4838                                 RETURN(rc);
4839
4840                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4841                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4842                                 i);
4843                         rc = lod_sub_declare_delete(env, next,
4844                                         (const struct dt_key *)stripe_name, th);
4845                         if (rc != 0)
4846                                 RETURN(rc);
4847                 }
4848         }
4849
4850         /*
4851          * we declare destroy for the local object
4852          */
4853         rc = lod_sub_declare_destroy(env, next, th);
4854         if (rc)
4855                 RETURN(rc);
4856
4857         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4858             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4859                 RETURN(0);
4860
4861         if (!lod_obj_is_striped(dt))
4862                 RETURN(0);
4863
4864         /* declare destroy all striped objects */
4865         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4866                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4867                         if (lo->ldo_stripe[i] == NULL)
4868                                 continue;
4869
4870                         rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
4871                                                      th);
4872
4873                         rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
4874                                                      th);
4875                         if (rc != 0)
4876                                 break;
4877                 }
4878         } else {
4879                 struct lod_obj_stripe_cb_data data = { { 0 } };
4880
4881                 data.locd_declare = true;
4882                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
4883                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
4884         }
4885
4886         RETURN(rc);
4887 }
4888
4889 /**
4890  * Implementation of dt_object_operations::do_destroy.
4891  *
4892  * If the object is a striped directory, then the function removes references
4893  * from the master object (this is an index) to the stripes and destroys all
4894  * the stripes. In all the cases, the function destroys the object itself.
4895  *
4896  * \see dt_object_operations::do_destroy() in the API description for details.
4897  */
4898 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
4899                        struct thandle *th)
4900 {
4901         struct dt_object  *next = dt_object_child(dt);
4902         struct lod_object *lo = lod_dt_obj(dt);
4903         struct lod_thread_info *info = lod_env_info(env);
4904         char               *stripe_name = info->lti_key;
4905         unsigned int       i;
4906         int                rc;
4907         ENTRY;
4908
4909         /* destroy sub-stripe of master object */
4910         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4911                 rc = next->do_ops->do_index_try(env, next,
4912                                                 &dt_directory_features);
4913                 if (rc != 0)
4914                         RETURN(rc);
4915
4916                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4917                         rc = lod_sub_ref_del(env, next, th);
4918                         if (rc != 0)
4919                                 RETURN(rc);
4920
4921                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4922                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4923                                 i);
4924
4925                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4926                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4927                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4928
4929                         rc = lod_sub_delete(env, next,
4930                                        (const struct dt_key *)stripe_name, th);
4931                         if (rc != 0)
4932                                 RETURN(rc);
4933                 }
4934         }
4935
4936         rc = lod_sub_destroy(env, next, th);
4937         if (rc != 0)
4938                 RETURN(rc);
4939
4940         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4941             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4942                 RETURN(0);
4943
4944         if (!lod_obj_is_striped(dt))
4945                 RETURN(0);
4946
4947         /* destroy all striped objects */
4948         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4949                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4950                         if (lo->ldo_stripe[i] == NULL)
4951                                 continue;
4952                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4953                             i == cfs_fail_val) {
4954                                 dt_write_lock(env, lo->ldo_stripe[i],
4955                                               MOR_TGT_CHILD);
4956                                 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
4957                                                      th);
4958                                 dt_write_unlock(env, lo->ldo_stripe[i]);
4959                                 if (rc != 0)
4960                                         break;
4961
4962                                 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
4963                                                      th);
4964                                 if (rc != 0)
4965                                         break;
4966                         }
4967                 }
4968         } else {
4969                 struct lod_obj_stripe_cb_data data = { { 0 } };
4970
4971                 data.locd_declare = false;
4972                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
4973                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
4974         }
4975
4976         RETURN(rc);
4977 }
4978
4979 /**
4980  * Implementation of dt_object_operations::do_declare_ref_add.
4981  *
4982  * \see dt_object_operations::do_declare_ref_add() in the API description
4983  * for details.
4984  */
4985 static int lod_declare_ref_add(const struct lu_env *env,
4986                                struct dt_object *dt, struct thandle *th)
4987 {
4988         return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
4989 }
4990
4991 /**
4992  * Implementation of dt_object_operations::do_ref_add.
4993  *
4994  * \see dt_object_operations::do_ref_add() in the API description for details.
4995  */
4996 static int lod_ref_add(const struct lu_env *env,
4997                        struct dt_object *dt, struct thandle *th)
4998 {
4999         return lod_sub_ref_add(env, dt_object_child(dt), th);
5000 }
5001
5002 /**
5003  * Implementation of dt_object_operations::do_declare_ref_del.
5004  *
5005  * \see dt_object_operations::do_declare_ref_del() in the API description
5006  * for details.
5007  */
5008 static int lod_declare_ref_del(const struct lu_env *env,
5009                                struct dt_object *dt, struct thandle *th)
5010 {
5011         return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5012 }
5013
5014 /**
5015  * Implementation of dt_object_operations::do_ref_del
5016  *
5017  * \see dt_object_operations::do_ref_del() in the API description for details.
5018  */
5019 static int lod_ref_del(const struct lu_env *env,
5020                        struct dt_object *dt, struct thandle *th)
5021 {
5022         return lod_sub_ref_del(env, dt_object_child(dt), th);
5023 }
5024
5025 /**
5026  * Implementation of dt_object_operations::do_object_sync.
5027  *
5028  * \see dt_object_operations::do_object_sync() in the API description
5029  * for details.
5030  */
5031 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5032                            __u64 start, __u64 end)
5033 {
5034         return dt_object_sync(env, dt_object_child(dt), start, end);
5035 }
5036
5037 /**
5038  * Release LDLM locks on the stripes of a striped directory.
5039  *
5040  * Iterates over all the locks taken on the stripe objects and
5041  * cancel them.
5042  *
5043  * \param[in] env       execution environment
5044  * \param[in] dt        striped object
5045  * \param[in] einfo     lock description
5046  * \param[in] policy    data describing requested lock
5047  *
5048  * \retval              0 on success
5049  * \retval              negative if failed
5050  */
5051 static int lod_object_unlock_internal(const struct lu_env *env,
5052                                       struct dt_object *dt,
5053                                       struct ldlm_enqueue_info *einfo,
5054                                       union ldlm_policy_data *policy)
5055 {
5056         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5057         int                     rc = 0;
5058         int                     i;
5059         ENTRY;
5060
5061         if (slave_locks == NULL)
5062                 RETURN(0);
5063
5064         for (i = 1; i < slave_locks->count; i++) {
5065                 if (lustre_handle_is_used(&slave_locks->handles[i]))
5066                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
5067                                                     einfo->ei_mode);
5068         }
5069
5070         RETURN(rc);
5071 }
5072
5073 /**
5074  * Implementation of dt_object_operations::do_object_unlock.
5075  *
5076  * Used to release LDLM lock(s).
5077  *
5078  * \see dt_object_operations::do_object_unlock() in the API description
5079  * for details.
5080  */
5081 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5082                              struct ldlm_enqueue_info *einfo,
5083                              union ldlm_policy_data *policy)
5084 {
5085         struct lod_object *lo = lod_dt_obj(dt);
5086         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5087         int slave_locks_size;
5088         int i;
5089         ENTRY;
5090
5091         if (slave_locks == NULL)
5092                 RETURN(0);
5093
5094         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5095         LASSERT(lo->ldo_dir_stripe_count > 1);
5096         /* Note: for remote lock for single stripe dir, MDT will cancel
5097          * the lock by lockh directly */
5098         LASSERT(!dt_object_remote(dt_object_child(dt)));
5099
5100         /* locks were unlocked in MDT layer */
5101         for (i = 1; i < slave_locks->count; i++) {
5102                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
5103                 dt_invalidate(env, lo->ldo_stripe[i]);
5104         }
5105
5106         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
5107                            sizeof(slave_locks->handles[0]);
5108         OBD_FREE(slave_locks, slave_locks_size);
5109         einfo->ei_cbdata = NULL;
5110
5111         RETURN(0);
5112 }
5113
5114 /**
5115  * Implementation of dt_object_operations::do_object_lock.
5116  *
5117  * Used to get LDLM lock on the non-striped and striped objects.
5118  *
5119  * \see dt_object_operations::do_object_lock() in the API description
5120  * for details.
5121  */
5122 static int lod_object_lock(const struct lu_env *env,
5123                            struct dt_object *dt,
5124                            struct lustre_handle *lh,
5125                            struct ldlm_enqueue_info *einfo,
5126                            union ldlm_policy_data *policy)
5127 {
5128         struct lod_object       *lo = lod_dt_obj(dt);
5129         int                     rc = 0;
5130         int                     i;
5131         int                     slave_locks_size;
5132         struct lustre_handle_array *slave_locks = NULL;
5133         ENTRY;
5134
5135         /* remote object lock */
5136         if (!einfo->ei_enq_slave) {
5137                 LASSERT(dt_object_remote(dt));
5138                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5139                                       policy);
5140         }
5141
5142         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5143                 GOTO(out, rc = -ENOTDIR);
5144
5145         rc = lod_load_striping(env, lo);
5146         if (rc != 0)
5147                 GOTO(out, rc);
5148
5149         /* No stripes */
5150         if (lo->ldo_dir_stripe_count <= 1) {
5151                 /*
5152                  * NB, ei_cbdata stores pointer to slave locks, if no locks
5153                  * taken, make sure it's set to NULL, otherwise MDT will try to
5154                  * unlock them.
5155                  */
5156                 einfo->ei_cbdata = NULL;
5157                 GOTO(out, rc = 0);
5158         }
5159
5160         slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripe_count *
5161                            sizeof(slave_locks->handles[0]);
5162         /* Freed in lod_object_unlock */
5163         OBD_ALLOC(slave_locks, slave_locks_size);
5164         if (slave_locks == NULL)
5165                 GOTO(out, rc = -ENOMEM);
5166         slave_locks->count = lo->ldo_dir_stripe_count;
5167
5168         /* striped directory lock */
5169         for (i = 1; i < lo->ldo_dir_stripe_count; i++) {
5170                 struct lustre_handle    lockh;
5171                 struct ldlm_res_id      *res_id;
5172
5173                 res_id = &lod_env_info(env)->lti_res_id;
5174                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5175                                        res_id);
5176                 einfo->ei_res_id = res_id;
5177
5178                 LASSERT(lo->ldo_stripe[i] != NULL);
5179                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
5180                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5181                                             einfo, policy);
5182                 } else {
5183                         struct ldlm_namespace *ns = einfo->ei_namespace;
5184                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5185                         ldlm_completion_callback completion = einfo->ei_cb_cp;
5186                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
5187
5188                         if (einfo->ei_mode == LCK_PW ||
5189                             einfo->ei_mode == LCK_EX)
5190                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
5191
5192                         /* This only happens if there are mulitple stripes
5193                          * on the master MDT, i.e. except stripe0, there are
5194                          * other stripes on the Master MDT as well, Only
5195                          * happens in the test case right now. */
5196                         LASSERT(ns != NULL);
5197                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
5198                                                     policy, einfo->ei_mode,
5199                                                     &dlmflags, blocking,
5200                                                     completion, NULL,
5201                                                     NULL, 0, LVB_T_NONE,
5202                                                     NULL, &lockh);
5203                 }
5204                 if (rc != 0)
5205                         break;
5206                 slave_locks->handles[i] = lockh;
5207         }
5208         einfo->ei_cbdata = slave_locks;
5209
5210         if (rc != 0 && slave_locks != NULL) {
5211                 lod_object_unlock_internal(env, dt, einfo, policy);
5212                 OBD_FREE(slave_locks, slave_locks_size);
5213         }
5214         EXIT;
5215 out:
5216         if (rc != 0)
5217                 einfo->ei_cbdata = NULL;
5218         RETURN(rc);
5219 }
5220
5221 /**
5222  * Implementation of dt_object_operations::do_invalidate.
5223  *
5224  * \see dt_object_operations::do_invalidate() in the API description for details
5225  */
5226 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5227 {
5228         return dt_invalidate(env, dt_object_child(dt));
5229 }
5230
5231 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5232 {
5233         ENTRY;
5234
5235         /* clear memory region that will be used for layout change */
5236         memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5237         info->lti_count = 0;
5238
5239         if (info->lti_comp_size >= comp_cnt)
5240                 RETURN(0);
5241
5242         if (info->lti_comp_size > 0) {
5243                 OBD_FREE(info->lti_comp_idx,
5244                          info->lti_comp_size * sizeof(__u32));
5245                 info->lti_comp_size = 0;
5246         }
5247
5248         OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5249         if (!info->lti_comp_idx)
5250                 RETURN(-ENOMEM);
5251
5252         info->lti_comp_size = comp_cnt;
5253         RETURN(0);
5254 }
5255
5256 static int lod_declare_instantiate_components(const struct lu_env *env,
5257                 struct lod_object *lo, struct thandle *th)
5258 {
5259         struct lod_thread_info *info = lod_env_info(env);
5260         struct ost_pool *inuse = &info->lti_inuse_osts;
5261         int i;
5262         int rc = 0;
5263         ENTRY;
5264
5265         LASSERT(info->lti_count < lo->ldo_comp_cnt);
5266         if (info->lti_count > 0) {
5267                 /* Prepare inuse array for composite file */
5268                 rc = lod_prepare_inuse(env, lo);
5269                 if (rc)
5270                         RETURN(rc);
5271         }
5272
5273         for (i = 0; i < info->lti_count; i++) {
5274                 rc = lod_qos_prep_create(env, lo, NULL, th,
5275                                          info->lti_comp_idx[i], inuse);
5276                 if (rc)
5277                         break;
5278         }
5279
5280         if (!rc) {
5281                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5282                 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5283                                 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5284         }
5285
5286         RETURN(rc);
5287 }
5288
5289 static int lod_declare_update_plain(const struct lu_env *env,
5290                 struct lod_object *lo, struct layout_intent *layout,
5291                 const struct lu_buf *buf, struct thandle *th)
5292 {
5293         struct lod_thread_info *info = lod_env_info(env);
5294         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5295         struct lod_layout_component *lod_comp;
5296         struct lov_comp_md_v1 *comp_v1 = NULL;
5297         bool replay = false;
5298         int i, rc;
5299         ENTRY;
5300
5301         LASSERT(lo->ldo_flr_state == LCM_FL_NOT_FLR);
5302
5303         /*
5304          * In case the client is passing lovea, which only happens during
5305          * the replay of layout intent write RPC for now, we may need to
5306          * parse the lovea and apply new layout configuration.
5307          */
5308         if (buf && buf->lb_len)  {
5309                 struct lov_user_md_v1 *v1 = buf->lb_buf;
5310
5311                 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5312                     v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5313                                               LOV_MAGIC_COMP_V1)) {
5314                         CERROR("%s: the replay buffer of layout extend "
5315                                "(magic %#x) does not contain expected "
5316                                "composite layout.\n",
5317                                lod2obd(d)->obd_name, v1->lmm_magic);
5318                         GOTO(out, rc = -EINVAL);
5319                 }
5320
5321                 lod_object_free_striping(env, lo);
5322                 rc = lod_use_defined_striping(env, lo, buf);
5323                 if (rc)
5324                         GOTO(out, rc);
5325
5326                 rc = lod_get_lov_ea(env, lo);
5327                 if (rc <= 0)
5328                         GOTO(out, rc);
5329                 /* old on-disk EA is stored in info->lti_buf */
5330                 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5331                 replay = true;
5332         } else {
5333                 /* non replay path */
5334                 rc = lod_load_striping_locked(env, lo);
5335                 if (rc)
5336                         GOTO(out, rc);
5337         }
5338
5339         if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5340                 /**
5341                  * trunc transfers [size, eof) in the intent extent, while
5342                  * we'd instantiated components covers [0, size).
5343                  */
5344                 layout->li_extent.e_end = layout->li_extent.e_start;
5345                 layout->li_extent.e_start = 0;
5346         }
5347
5348         /* Make sure defined layout covers the requested write range. */
5349         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5350         if (lo->ldo_comp_cnt > 1 &&
5351             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5352             lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5353                 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5354                        "%s: the defined layout [0, %#llx) does not covers "
5355                        "the write range "DEXT"\n",
5356                        lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5357                        PEXT(&layout->li_extent));
5358                 GOTO(out, rc = -EINVAL);
5359         }
5360
5361         CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5362                lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5363                PEXT(&layout->li_extent));
5364
5365         /*
5366          * Iterate ld->ldo_comp_entries, find the component whose extent under
5367          * the write range and not instantianted.
5368          */
5369         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5370                 lod_comp = &lo->ldo_comp_entries[i];
5371
5372                 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5373                         break;
5374
5375                 if (!replay) {
5376                         if (lod_comp_inited(lod_comp))
5377                                 continue;
5378                 } else {
5379                         /**
5380                          * In replay path, lod_comp is the EA passed by
5381                          * client replay buffer,  comp_v1 is the pre-recovery
5382                          * on-disk EA, we'd sift out those components which
5383                          * were init-ed in the on-disk EA.
5384                          */
5385                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5386                             LCME_FL_INIT)
5387                                 continue;
5388                 }
5389                 /*
5390                  * this component hasn't instantiated in normal path, or during
5391                  * replay it needs replay the instantiation.
5392                  */
5393
5394                 /* A released component is being extended */
5395                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5396                         GOTO(out, rc = -EINVAL);
5397
5398                 LASSERT(info->lti_comp_idx != NULL);
5399                 info->lti_comp_idx[info->lti_count++] = i;
5400         }
5401
5402         if (info->lti_count == 0)
5403                 RETURN(-EALREADY);
5404
5405         lod_obj_inc_layout_gen(lo);
5406         rc = lod_declare_instantiate_components(env, lo, th);
5407 out:
5408         if (rc)
5409                 lod_object_free_striping(env, lo);
5410         RETURN(rc);
5411 }
5412
5413 #define lod_foreach_mirror_comp(comp, lo, mirror_idx)                      \
5414 for (comp = &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_start];  \
5415      comp <= &lo->ldo_comp_entries[lo->ldo_mirrors[mirror_idx].lme_end];   \
5416      comp++)
5417
5418 static inline int lod_comp_index(struct lod_object *lo,
5419                                  struct lod_layout_component *lod_comp)
5420 {
5421         LASSERT(lod_comp >= lo->ldo_comp_entries &&
5422                 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5423
5424         return lod_comp - lo->ldo_comp_entries;
5425 }
5426
5427 /**
5428  * Stale other mirrors by writing extent.
5429  */
5430 static void lod_stale_components(struct lod_object *lo, int primary,
5431                                  struct lu_extent *extent)
5432 {
5433         struct lod_layout_component *pri_comp, *lod_comp;
5434         int i;
5435
5436         /* The writing extent decides which components in the primary
5437          * are affected... */
5438         CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5439         lod_foreach_mirror_comp(pri_comp, lo, primary) {
5440                 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5441                         continue;
5442
5443                 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5444                        lod_comp_index(lo, pri_comp),
5445                        PEXT(&pri_comp->llc_extent));
5446
5447                 for (i = 0; i < lo->ldo_mirror_count; i++) {
5448                         if (i == primary)
5449                                 continue;
5450
5451                         /* ... and then stale other components that are
5452                          * overlapping with primary components */
5453                         lod_foreach_mirror_comp(lod_comp, lo, i) {
5454                                 if (!lu_extent_is_overlapped(
5455                                                         &pri_comp->llc_extent,
5456                                                         &lod_comp->llc_extent))
5457                                         continue;
5458
5459                                 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5460                                       i, lod_comp_index(lo, lod_comp));
5461
5462                                 lod_comp->llc_flags |= LCME_FL_STALE;
5463                                 lo->ldo_mirrors[i].lme_stale = 1;
5464                         }
5465                 }
5466         }
5467 }
5468
5469 static int lod_declare_update_rdonly(const struct lu_env *env,
5470                 struct lod_object *lo, struct md_layout_change *mlc,
5471                 struct thandle *th)
5472 {
5473         struct lod_thread_info *info = lod_env_info(env);
5474         struct lu_attr *layout_attr = &info->lti_layout_attr;
5475         struct lod_layout_component *lod_comp;
5476         struct layout_intent *layout = mlc->mlc_intent;
5477         struct lu_extent extent = layout->li_extent;
5478         unsigned int seq = 0;
5479         int picked;
5480         int i;
5481         int rc;
5482         ENTRY;
5483
5484         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE);
5485         LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
5486         LASSERT(lo->ldo_mirror_count > 0);
5487
5488         CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
5489                PFID(lod_object_fid(lo)), PEXT(&extent));
5490
5491         if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
5492                 get_random_bytes(&seq, sizeof(seq));
5493                 seq %= lo->ldo_mirror_count;
5494         }
5495
5496         /**
5497          * Pick a mirror as the primary.
5498          * Now it only picks the first mirror, this algo can be
5499          * revised later after knowing the topology of cluster or
5500          * the availability of OSTs.
5501          */
5502         for (picked = -1, i = 0; i < lo->ldo_mirror_count; i++) {
5503                 int index = (i + seq) % lo->ldo_mirror_count;
5504
5505                 if (!lo->ldo_mirrors[index].lme_stale) {
5506                         picked = index;
5507                         break;
5508                 }
5509         }
5510         if (picked < 0) /* failed to pick a primary */
5511                 RETURN(-ENODATA);
5512
5513         CDEBUG(D_LAYOUT, DFID": picked mirror %u as primary\n",
5514                PFID(lod_object_fid(lo)), lo->ldo_mirrors[picked].lme_id);
5515
5516         /* stale overlapping components from other mirrors */
5517         lod_stale_components(lo, picked, &extent);
5518
5519         /* instantiate components for the picked mirror, start from 0 */
5520         if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
5521                 /**
5522                  * trunc transfers [size, eof) in the intent extent, we'd
5523                  * stale components overlapping [size, eof), while we'd
5524                  * instantiated components covers [0, size).
5525                  */
5526                 extent.e_end = extent.e_start;
5527         }
5528         extent.e_start = 0;
5529
5530         lod_foreach_mirror_comp(lod_comp, lo, picked) {
5531                 if (!lu_extent_is_overlapped(&extent,
5532                                              &lod_comp->llc_extent))
5533                         break;
5534
5535                 if (lod_comp_inited(lod_comp))
5536                         continue;
5537
5538                 CDEBUG(D_LAYOUT, "instantiate: %u / %u\n",
5539                        i, lod_comp_index(lo, lod_comp));
5540
5541                 info->lti_comp_idx[info->lti_count++] =
5542                                                 lod_comp_index(lo, lod_comp);
5543         }
5544
5545         lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5546
5547         /* Reset the layout version once it's becoming too large.
5548          * This way it can make sure that the layout version is
5549          * monotonously increased in this writing era. */
5550         lod_obj_inc_layout_gen(lo);
5551         if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
5552                 __u32 layout_version;
5553
5554                 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
5555                 lo->ldo_layout_gen = layout_version & 0xffff;
5556         }
5557
5558         rc = lod_declare_instantiate_components(env, lo, th);
5559         if (rc)
5560                 GOTO(out, rc);
5561
5562         layout_attr->la_valid = LA_LAYOUT_VERSION;
5563         layout_attr->la_layout_version = 0; /* set current version */
5564         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5565         if (rc)
5566                 GOTO(out, rc);
5567
5568 out:
5569         if (rc)
5570                 lod_object_free_striping(env, lo);
5571         RETURN(rc);
5572 }
5573
5574 static int lod_declare_update_write_pending(const struct lu_env *env,
5575                 struct lod_object *lo, struct md_layout_change *mlc,
5576                 struct thandle *th)
5577 {
5578         struct lod_thread_info *info = lod_env_info(env);
5579         struct lu_attr *layout_attr = &info->lti_layout_attr;
5580         struct lod_layout_component *lod_comp;
5581         struct lu_extent extent = { 0 };
5582         int primary = -1;
5583         int i;
5584         int rc;
5585         ENTRY;
5586
5587         LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
5588         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
5589                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
5590
5591         /* look for the primary mirror */
5592         for (i = 0; i < lo->ldo_mirror_count; i++) {
5593                 if (lo->ldo_mirrors[i].lme_stale)
5594                         continue;
5595
5596                 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
5597                          PFID(lod_object_fid(lo)),
5598                          lo->ldo_mirrors[i].lme_id,
5599                          lo->ldo_mirrors[primary].lme_id);
5600
5601                 primary = i;
5602         }
5603         if (primary < 0) {
5604                 CERROR(DFID ": doesn't have a primary mirror\n",
5605                        PFID(lod_object_fid(lo)));
5606                 GOTO(out, rc = -ENODATA);
5607         }
5608
5609         CDEBUG(D_LAYOUT, DFID": found primary %u\n",
5610                PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
5611
5612         LASSERT(!lo->ldo_mirrors[primary].lme_stale);
5613
5614         /* for LAYOUT_WRITE opc, it has to do the following operations:
5615          * 1. stale overlapping componets from stale mirrors;
5616          * 2. instantiate components of the primary mirror;
5617          * 3. transfter layout version to all objects of the primary;
5618          *
5619          * for LAYOUT_RESYNC opc, it will do:
5620          * 1. instantiate components of all stale mirrors;
5621          * 2. transfer layout version to all objects to close write era. */
5622
5623         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5624                 LASSERT(mlc->mlc_intent != NULL);
5625
5626                 extent = mlc->mlc_intent->li_extent;
5627
5628                 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
5629                        PFID(lod_object_fid(lo)), PEXT(&extent));
5630
5631                 /* 1. stale overlapping components */
5632                 lod_stale_components(lo, primary, &extent);
5633
5634                 /* 2. find out the components need instantiating.
5635                  * instantiate [0, mlc->mlc_intent->e_end) */
5636                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
5637                         /**
5638                          * trunc transfers [size, eof) in the intent extent,
5639                          * we'd stale components overlapping [size, eof),
5640                          * while we'd instantiated components covers [0, size).
5641                          */
5642                         extent.e_end = extent.e_start;
5643                 }
5644                 extent.e_start = 0;
5645
5646                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5647                         if (!lu_extent_is_overlapped(&extent,
5648                                                      &lod_comp->llc_extent))
5649                                 break;
5650
5651                         if (lod_comp_inited(lod_comp))
5652                                 continue;
5653
5654                         CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
5655                                primary, lod_comp_index(lo, lod_comp));
5656                         info->lti_comp_idx[info->lti_count++] =
5657                                                 lod_comp_index(lo, lod_comp);
5658                 }
5659         } else { /* MD_LAYOUT_RESYNC */
5660                 /* figure out the components that have been instantiated in
5661                  * in primary to decide what components should be instantiated
5662                  * in stale mirrors */
5663                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
5664                         if (!lod_comp_inited(lod_comp))
5665                                 break;
5666
5667                         extent.e_end = lod_comp->llc_extent.e_end;
5668                 }
5669
5670                 CDEBUG(D_LAYOUT,
5671                        DFID": instantiate all stale components in "DEXT"\n",
5672                        PFID(lod_object_fid(lo)), PEXT(&extent));
5673
5674                 /* 1. instantiate all components within this extent, even
5675                  * non-stale components so that it won't need to instantiate
5676                  * those components for mirror truncate later. */
5677                 for (i = 0; i < lo->ldo_mirror_count; i++) {
5678                         if (primary == i)
5679                                 continue;
5680
5681                         LASSERTF(lo->ldo_mirrors[i].lme_stale,
5682                                  "both %d and %d are primary\n", i, primary);
5683
5684                         lod_foreach_mirror_comp(lod_comp, lo, i) {
5685                                 if (!lu_extent_is_overlapped(&extent,
5686                                                         &lod_comp->llc_extent))
5687                                         break;
5688
5689                                 if (lod_comp_inited(lod_comp))
5690                                         continue;
5691
5692                                 CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
5693                                        i, lod_comp_index(lo, lod_comp));
5694
5695                                 info->lti_comp_idx[info->lti_count++] =
5696                                                 lod_comp_index(lo, lod_comp);
5697                         }
5698                 }
5699
5700                 /* change the file state to SYNC_PENDING */
5701                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
5702         }
5703
5704         rc = lod_declare_instantiate_components(env, lo, th);
5705         if (rc)
5706                 GOTO(out, rc);
5707
5708         /* 3. transfer layout version to OST objects.
5709          * transfer new layout version to OST objects so that stale writes
5710          * can be denied. It also ends an era of writing by setting
5711          * LU_LAYOUT_RESYNC. Normal client can never use this bit to
5712          * send write RPC; only resync RPCs could do it. */
5713         layout_attr->la_valid = LA_LAYOUT_VERSION;
5714         layout_attr->la_layout_version = 0; /* set current version */
5715         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
5716                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
5717         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
5718         if (rc)
5719                 GOTO(out, rc);
5720
5721         lod_obj_inc_layout_gen(lo);
5722 out:
5723         if (rc)
5724                 lod_object_free_striping(env, lo);
5725         RETURN(rc);
5726 }
5727
5728 static int lod_declare_update_sync_pending(const struct lu_env *env,
5729                 struct lod_object *lo, struct md_layout_change *mlc,
5730                 struct thandle *th)
5731 {
5732         struct lod_thread_info  *info = lod_env_info(env);
5733         unsigned sync_components = 0;
5734         unsigned resync_components = 0;
5735         int i;
5736         int rc;
5737         ENTRY;
5738
5739         LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
5740         LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
5741                 mlc->mlc_opc == MD_LAYOUT_WRITE);
5742
5743         CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
5744                PFID(lod_object_fid(lo)), mlc->mlc_opc);
5745
5746         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
5747                 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
5748                        PFID(lod_object_fid(lo)));
5749
5750                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
5751                 return lod_declare_update_write_pending(env, lo, mlc, th);
5752         }
5753
5754         /* MD_LAYOUT_RESYNC_DONE */
5755
5756         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5757                 struct lod_layout_component *lod_comp;
5758                 int j;
5759
5760                 lod_comp = &lo->ldo_comp_entries[i];
5761
5762                 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
5763                         sync_components++;
5764                         continue;
5765                 }
5766
5767                 for (j = 0; j < mlc->mlc_resync_count; j++) {
5768                         if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
5769                                 continue;
5770
5771                         mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
5772                         lod_comp->llc_flags &= ~LCME_FL_STALE;
5773                         resync_components++;
5774                         break;
5775                 }
5776         }
5777
5778         /* valid check */
5779         for (i = 0; i < mlc->mlc_resync_count; i++) {
5780                 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
5781                         continue;
5782
5783                 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
5784                        "or already synced\n", PFID(lod_object_fid(lo)),
5785                        mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
5786                 GOTO(out, rc = -EINVAL);
5787         }
5788
5789         if (!sync_components || !resync_components) {
5790                 CDEBUG(D_LAYOUT, DFID": no mirror in sync or resync\n",
5791                        PFID(lod_object_fid(lo)));
5792
5793                 /* tend to return an error code here to prevent
5794                  * the MDT from setting SoM attribute */
5795                 GOTO(out, rc = -EINVAL);
5796         }
5797
5798         CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
5799                PFID(lod_object_fid(lo)),
5800                resync_components, mlc->mlc_resync_count);
5801
5802         lo->ldo_flr_state = LCM_FL_RDONLY;
5803         lod_obj_inc_layout_gen(lo);
5804
5805         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5806         rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5807                                        &info->lti_buf, XATTR_NAME_LOV, 0, th);
5808         EXIT;
5809
5810 out:
5811         if (rc)
5812                 lod_object_free_striping(env, lo);
5813         RETURN(rc);
5814 }
5815
5816 static int lod_declare_layout_change(const struct lu_env *env,
5817                 struct dt_object *dt, struct md_layout_change *mlc,
5818                 struct thandle *th)
5819 {
5820         struct lod_thread_info  *info = lod_env_info(env);
5821         struct lod_object *lo = lod_dt_obj(dt);
5822         int rc;
5823         ENTRY;
5824
5825         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
5826             dt_object_remote(dt_object_child(dt)))
5827                 RETURN(-EINVAL);
5828
5829         lod_write_lock(env, dt, 0);
5830         rc = lod_load_striping_locked(env, lo);
5831         if (rc)
5832                 GOTO(out, rc);
5833
5834         LASSERT(lo->ldo_comp_cnt > 0);
5835
5836         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
5837         if (rc)
5838                 GOTO(out, rc);
5839
5840         switch (lo->ldo_flr_state) {
5841         case LCM_FL_NOT_FLR:
5842                 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
5843                                               &mlc->mlc_buf, th);
5844                 break;
5845         case LCM_FL_RDONLY:
5846                 rc = lod_declare_update_rdonly(env, lo, mlc, th);
5847                 break;
5848         case LCM_FL_WRITE_PENDING:
5849                 rc = lod_declare_update_write_pending(env, lo, mlc, th);
5850                 break;
5851         case LCM_FL_SYNC_PENDING:
5852                 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
5853                 break;
5854         default:
5855                 rc = -ENOTSUPP;
5856                 break;
5857         }
5858 out:
5859         dt_write_unlock(env, dt);
5860         RETURN(rc);
5861 }
5862
5863 /**
5864  * Instantiate layout component objects which covers the intent write offset.
5865  */
5866 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
5867                              struct md_layout_change *mlc, struct thandle *th)
5868 {
5869         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5870         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
5871         struct lod_object *lo = lod_dt_obj(dt);
5872         int rc;
5873
5874         rc = lod_striped_create(env, dt, attr, NULL, th);
5875         if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
5876                 layout_attr->la_layout_version |= lo->ldo_layout_gen;
5877                 rc = lod_attr_set(env, dt, layout_attr, th);
5878         }
5879
5880         return rc;
5881 }
5882
5883 struct dt_object_operations lod_obj_ops = {
5884         .do_read_lock           = lod_read_lock,
5885         .do_write_lock          = lod_write_lock,
5886         .do_read_unlock         = lod_read_unlock,
5887         .do_write_unlock        = lod_write_unlock,
5888         .do_write_locked        = lod_write_locked,
5889         .do_attr_get            = lod_attr_get,
5890         .do_declare_attr_set    = lod_declare_attr_set,
5891         .do_attr_set            = lod_attr_set,
5892         .do_xattr_get           = lod_xattr_get,
5893         .do_declare_xattr_set   = lod_declare_xattr_set,
5894         .do_xattr_set           = lod_xattr_set,
5895         .do_declare_xattr_del   = lod_declare_xattr_del,
5896         .do_xattr_del           = lod_xattr_del,
5897         .do_xattr_list          = lod_xattr_list,
5898         .do_ah_init             = lod_ah_init,
5899         .do_declare_create      = lod_declare_create,
5900         .do_create              = lod_create,
5901         .do_declare_destroy     = lod_declare_destroy,
5902         .do_destroy             = lod_destroy,
5903         .do_index_try           = lod_index_try,
5904         .do_declare_ref_add     = lod_declare_ref_add,
5905         .do_ref_add             = lod_ref_add,
5906         .do_declare_ref_del     = lod_declare_ref_del,
5907         .do_ref_del             = lod_ref_del,
5908         .do_object_sync         = lod_object_sync,
5909         .do_object_lock         = lod_object_lock,
5910         .do_object_unlock       = lod_object_unlock,
5911         .do_invalidate          = lod_invalidate,
5912         .do_declare_layout_change = lod_declare_layout_change,
5913         .do_layout_change       = lod_layout_change,
5914 };
5915
5916 /**
5917  * Implementation of dt_body_operations::dbo_read.
5918  *
5919  * \see dt_body_operations::dbo_read() in the API description for details.
5920  */
5921 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5922                         struct lu_buf *buf, loff_t *pos)
5923 {
5924         struct dt_object *next = dt_object_child(dt);
5925
5926         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5927                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5928         return next->do_body_ops->dbo_read(env, next, buf, pos);
5929 }
5930
5931 /**
5932  * Implementation of dt_body_operations::dbo_declare_write.
5933  *
5934  * \see dt_body_operations::dbo_declare_write() in the API description
5935  * for details.
5936  */
5937 static ssize_t lod_declare_write(const struct lu_env *env,
5938                                  struct dt_object *dt,
5939                                  const struct lu_buf *buf, loff_t pos,
5940                                  struct thandle *th)
5941 {
5942         return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
5943 }
5944
5945 /**
5946  * Implementation of dt_body_operations::dbo_write.
5947  *
5948  * \see dt_body_operations::dbo_write() in the API description for details.
5949  */
5950 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5951                          const struct lu_buf *buf, loff_t *pos,
5952                          struct thandle *th, int iq)
5953 {
5954         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
5955                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
5956         return lod_sub_write(env, dt_object_child(dt), buf, pos, th, iq);
5957 }
5958
5959 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5960                              __u64 start, __u64 end, struct thandle *th)
5961 {
5962         if (dt_object_remote(dt))
5963                 return -ENOTSUPP;
5964
5965         return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
5966 }
5967
5968 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5969                      __u64 start, __u64 end, struct thandle *th)
5970 {
5971         if (dt_object_remote(dt))
5972                 return -ENOTSUPP;
5973
5974         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
5975         return lod_sub_punch(env, dt_object_child(dt), start, end, th);
5976 }
5977
5978 /*
5979  * different type of files use the same body_ops because object may be created
5980  * in OUT, where there is no chance to set correct body_ops for each type, so
5981  * body_ops themselves will check file type inside, see lod_read/write/punch for
5982  * details.
5983  */
5984 const struct dt_body_operations lod_body_ops = {
5985         .dbo_read               = lod_read,
5986         .dbo_declare_write      = lod_declare_write,
5987         .dbo_write              = lod_write,
5988         .dbo_declare_punch      = lod_declare_punch,
5989         .dbo_punch              = lod_punch,
5990 };
5991
5992 /**
5993  * Implementation of lu_object_operations::loo_object_init.
5994  *
5995  * The function determines the type and the index of the target device using
5996  * sequence of the object's FID. Then passes control down to the
5997  * corresponding device:
5998  *  OSD for the local objects, OSP for remote
5999  *
6000  * \see lu_object_operations::loo_object_init() in the API description
6001  * for details.
6002  */
6003 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6004                            const struct lu_object_conf *conf)
6005 {
6006         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
6007         struct lu_device        *cdev   = NULL;
6008         struct lu_object        *cobj;
6009         struct lod_tgt_descs    *ltd    = NULL;
6010         struct lod_tgt_desc     *tgt;
6011         u32                      idx    = 0;
6012         int                      type   = LU_SEQ_RANGE_ANY;
6013         int                      rc;
6014         ENTRY;
6015
6016         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6017         if (rc != 0) {
6018                 /* Note: Sometimes, it will Return EAGAIN here, see
6019                  * ptrlpc_import_delay_req(), which might confuse
6020                  * lu_object_find_at() and make it wait there incorrectly.
6021                  * so we convert it to EIO here.*/
6022                 if (rc == -EAGAIN)
6023                         rc = -EIO;
6024
6025                 RETURN(rc);
6026         }
6027
6028         if (type == LU_SEQ_RANGE_MDT &&
6029             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6030                 cdev = &lod->lod_child->dd_lu_dev;
6031         } else if (type == LU_SEQ_RANGE_MDT) {
6032                 ltd = &lod->lod_mdt_descs;
6033                 lod_getref(ltd);
6034         } else if (type == LU_SEQ_RANGE_OST) {
6035                 ltd = &lod->lod_ost_descs;
6036                 lod_getref(ltd);
6037         } else {
6038                 LBUG();
6039         }
6040
6041         if (ltd != NULL) {
6042                 if (ltd->ltd_tgts_size > idx &&
6043                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6044                         tgt = LTD_TGT(ltd, idx);
6045
6046                         LASSERT(tgt != NULL);
6047                         LASSERT(tgt->ltd_tgt != NULL);
6048
6049                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
6050                 }
6051                 lod_putref(lod, ltd);
6052         }
6053
6054         if (unlikely(cdev == NULL))
6055                 RETURN(-ENOENT);
6056
6057         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6058         if (unlikely(cobj == NULL))
6059                 RETURN(-ENOMEM);
6060
6061         lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6062
6063         lu_object_add(lo, cobj);
6064
6065         RETURN(0);
6066 }
6067
6068 /**
6069  *
6070  * Release resources associated with striping.
6071  *
6072  * If the object is striped (regular or directory), then release
6073  * the stripe objects references and free the ldo_stripe array.
6074  *
6075  * \param[in] env       execution environment
6076  * \param[in] lo        object
6077  */
6078 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
6079 {
6080         struct lod_layout_component *lod_comp;
6081         int i, j;
6082
6083         if (lo->ldo_stripe != NULL) {
6084                 LASSERT(lo->ldo_comp_entries == NULL);
6085                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6086
6087                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6088                         if (lo->ldo_stripe[i])
6089                                 dt_object_put(env, lo->ldo_stripe[i]);
6090                 }
6091
6092                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6093                 OBD_FREE(lo->ldo_stripe, j);
6094                 lo->ldo_stripe = NULL;
6095                 lo->ldo_dir_stripes_allocated = 0;
6096                 lo->ldo_dir_stripe_loaded = 0;
6097                 lo->ldo_dir_stripe_count = 0;
6098         } else if (lo->ldo_comp_entries != NULL) {
6099                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6100                         /* free lod_layout_component::llc_stripe array */
6101                         lod_comp = &lo->ldo_comp_entries[i];
6102
6103                         if (lod_comp->llc_stripe == NULL)
6104                                 continue;
6105                         LASSERT(lod_comp->llc_stripes_allocated != 0);
6106                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6107                                 if (lod_comp->llc_stripe[j] != NULL)
6108                                         lu_object_put(env,
6109                                                &lod_comp->llc_stripe[j]->do_lu);
6110                         }
6111                         OBD_FREE(lod_comp->llc_stripe,
6112                                  sizeof(struct dt_object *) *
6113                                  lod_comp->llc_stripes_allocated);
6114                         lod_comp->llc_stripe = NULL;
6115                         lod_comp->llc_stripes_allocated = 0;
6116                 }
6117                 lod_free_comp_entries(lo);
6118                 lo->ldo_comp_cached = 0;
6119         }
6120 }
6121
6122 /**
6123  * Implementation of lu_object_operations::loo_object_free.
6124  *
6125  * \see lu_object_operations::loo_object_free() in the API description
6126  * for details.
6127  */
6128 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6129 {
6130         struct lod_object *lo = lu2lod_obj(o);
6131
6132         /* release all underlying object pinned */
6133         lod_object_free_striping(env, lo);
6134         lu_object_fini(o);
6135         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6136 }
6137
6138 /**
6139  * Implementation of lu_object_operations::loo_object_release.
6140  *
6141  * \see lu_object_operations::loo_object_release() in the API description
6142  * for details.
6143  */
6144 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6145 {
6146         /* XXX: shouldn't we release everything here in case if object
6147          * creation failed before? */
6148 }
6149
6150 /**
6151  * Implementation of lu_object_operations::loo_object_print.
6152  *
6153  * \see lu_object_operations::loo_object_print() in the API description
6154  * for details.
6155  */
6156 static int lod_object_print(const struct lu_env *env, void *cookie,
6157                             lu_printer_t p, const struct lu_object *l)
6158 {
6159         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6160
6161         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6162 }
6163
6164 struct lu_object_operations lod_lu_obj_ops = {
6165         .loo_object_init        = lod_object_init,
6166         .loo_object_free        = lod_object_free,
6167         .loo_object_release     = lod_object_release,
6168         .loo_object_print       = lod_object_print,
6169 };