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