Whamcloud - gitweb
LU-4017 quota: add project id support
[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 will 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                         if (idx == master_index) {
1846                                 /* Allocate the FID locally */
1847                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1848                                                    &fid, NULL);
1849                                 if (rc < 0)
1850                                         GOTO(out_put, rc);
1851                                 tgt_dt = lod->lod_child;
1852                                 break;
1853                         }
1854
1855                         /* Find next available target */
1856                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1857                                 continue;
1858
1859                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1860                                 /* check whether the idx already exists
1861                                  * in current allocated array */
1862                                 for (k = 0; k < i; k++) {
1863                                         if (idx_array[k] == idx) {
1864                                                 already_allocated = true;
1865                                                 break;
1866                                         }
1867                                 }
1868
1869                                 if (already_allocated)
1870                                         continue;
1871                         }
1872
1873                         /* check the status of the OSP */
1874                         tgt = LTD_TGT(ltd, idx);
1875                         if (tgt == NULL)
1876                                 continue;
1877
1878                         tgt_dt = tgt->ltd_tgt;
1879                         rc = dt_statfs(env, tgt_dt, NULL);
1880                         if (rc) {
1881                                 /* this OSP doesn't feel well */
1882                                 rc = 0;
1883                                 continue;
1884                         }
1885
1886                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1887                         if (rc < 0) {
1888                                 rc = 0;
1889                                 continue;
1890                         }
1891
1892                         break;
1893                 }
1894
1895                 /* Can not allocate more stripes */
1896                 if (j == lod->lod_remote_mdt_count) {
1897                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1898                                lod2obd(lod)->obd_name, stripe_count, i - 1);
1899                         break;
1900                 }
1901
1902                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1903                        idx, i, PFID(&fid));
1904                 idx_array[i] = idx;
1905                 /* Set the start index for next stripe allocation */
1906                 if (i < stripe_count - 1)
1907                         idx_array[i + 1] = (idx + 1) %
1908                                            (lod->lod_remote_mdt_count + 1);
1909                 /* tgt_dt and fid must be ready after search avaible OSP
1910                  * in the above loop */
1911                 LASSERT(tgt_dt != NULL);
1912                 LASSERT(fid_is_sane(&fid));
1913                 conf.loc_flags = LOC_F_NEW;
1914                 dto = dt_locate_at(env, tgt_dt, &fid,
1915                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1916                                    &conf);
1917                 if (IS_ERR(dto))
1918                         GOTO(out_put, rc = PTR_ERR(dto));
1919                 stripe[i] = dto;
1920         }
1921
1922         lo->ldo_dir_striped = 1;
1923         lo->ldo_stripe = stripe;
1924         lo->ldo_dir_stripenr = i;
1925         lo->ldo_dir_stripes_allocated = stripe_count;
1926
1927         if (lo->ldo_dir_stripenr == 0)
1928                 GOTO(out_put, rc = -ENOSPC);
1929
1930         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1931         if (rc != 0)
1932                 GOTO(out_put, rc);
1933
1934 out_put:
1935         if (rc < 0) {
1936                 for (i = 0; i < stripe_count; i++)
1937                         if (stripe[i] != NULL)
1938                                 dt_object_put(env, stripe[i]);
1939                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1940                 lo->ldo_dir_stripenr = 0;
1941                 lo->ldo_dir_stripes_allocated = 0;
1942                 lo->ldo_stripe = NULL;
1943         }
1944
1945 out_free:
1946         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1947
1948         RETURN(rc);
1949 }
1950
1951 /**
1952  * Declare create striped md object.
1953  *
1954  * The function declares intention to create a striped directory. This is a
1955  * wrapper for lod_prep_md_striped_create(). The only additional functionality
1956  * is to verify pattern \a lum_buf is good. Check that function for the details.
1957  *
1958  * \param[in] env       execution environment
1959  * \param[in] dt        object
1960  * \param[in] attr      attributes to initialize the objects with
1961  * \param[in] lum_buf   a pattern specifying the number of stripes and
1962  *                      MDT to start from
1963  * \param[in] dof       type of objects to be created
1964  * \param[in] th        transaction handle
1965  *
1966  * \retval              0 on success
1967  * \retval              negative if failed
1968  *
1969  */
1970 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1971                                      struct dt_object *dt,
1972                                      struct lu_attr *attr,
1973                                      const struct lu_buf *lum_buf,
1974                                      struct dt_object_format *dof,
1975                                      struct thandle *th)
1976 {
1977         struct lod_object       *lo = lod_dt_obj(dt);
1978         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1979         struct lmv_user_md_v1   *lum;
1980         int                     rc;
1981         ENTRY;
1982
1983         lum = lum_buf->lb_buf;
1984         LASSERT(lum != NULL);
1985
1986         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1987                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1988                (int)le32_to_cpu(lum->lum_stripe_offset));
1989
1990         if (le32_to_cpu(lum->lum_stripe_count) == 0)
1991                 GOTO(out, rc = 0);
1992
1993         rc = lod_verify_md_striping(lod, lum);
1994         if (rc != 0)
1995                 GOTO(out, rc);
1996
1997         /* prepare dir striped objects */
1998         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1999         if (rc != 0) {
2000                 /* failed to create striping, let's reset
2001                  * config so that others don't get confused */
2002                 lod_object_free_striping(env, lo);
2003                 GOTO(out, rc);
2004         }
2005 out:
2006         RETURN(rc);
2007 }
2008
2009 /**
2010  * Implementation of dt_object_operations::do_declare_xattr_set.
2011  *
2012  * Used with regular (non-striped) objects. Basically it
2013  * initializes the striping information and applies the
2014  * change to all the stripes.
2015  *
2016  * \see dt_object_operations::do_declare_xattr_set() in the API description
2017  * for details.
2018  */
2019 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2020                                      struct dt_object *dt,
2021                                      const struct lu_buf *buf,
2022                                      const char *name, int fl,
2023                                      struct thandle *th)
2024 {
2025         struct dt_object        *next = dt_object_child(dt);
2026         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2027         struct lod_object       *lo = lod_dt_obj(dt);
2028         int                     i;
2029         int                     rc;
2030         ENTRY;
2031
2032         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2033                 struct lmv_user_md_v1 *lum;
2034
2035                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2036                 lum = buf->lb_buf;
2037                 rc = lod_verify_md_striping(d, lum);
2038                 if (rc != 0)
2039                         RETURN(rc);
2040         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2041                 rc = lod_verify_striping(d, buf, false, 0);
2042                 if (rc != 0)
2043                         RETURN(rc);
2044         }
2045
2046         rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2047         if (rc != 0)
2048                 RETURN(rc);
2049
2050         /* Note: Do not set LinkEA on sub-stripes, otherwise
2051          * it will confuse the fid2path process(see mdt_path_current()).
2052          * The linkEA between master and sub-stripes is set in
2053          * lod_xattr_set_lmv(). */
2054         if (strcmp(name, XATTR_NAME_LINK) == 0)
2055                 RETURN(0);
2056
2057         /* set xattr to each stripes, if needed */
2058         rc = lod_load_striping(env, lo);
2059         if (rc != 0)
2060                 RETURN(rc);
2061
2062         if (lo->ldo_dir_stripenr == 0)
2063                 RETURN(0);
2064
2065         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2066                 LASSERT(lo->ldo_stripe[i]);
2067
2068                 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2069                                                 buf, name, fl, th);
2070                 if (rc != 0)
2071                         break;
2072         }
2073
2074         RETURN(rc);
2075 }
2076
2077 static int
2078 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2079                                      struct lod_object *lo,
2080                                      struct dt_object *dt, struct thandle *th,
2081                                      int stripe_idx,
2082                                      struct lod_obj_stripe_cb_data *data)
2083 {
2084         struct lod_thread_info *info = lod_env_info(env);
2085         struct filter_fid *ff = &info->lti_ff;
2086         struct lu_buf *buf = &info->lti_buf;
2087         int rc;
2088
2089         buf->lb_buf = ff;
2090         buf->lb_len = sizeof(*ff);
2091         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2092         if (rc == -ENODATA)
2093                 return 0;
2094
2095         if (rc < 0)
2096                 return rc;
2097
2098         ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2099         ff->ff_parent.f_ver = stripe_idx;
2100         fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2101         if (data->locd_declare)
2102                 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2103                                                       XATTR_NAME_FID,
2104                                                       LU_XATTR_REPLACE, th);
2105         else
2106                 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2107                                               LU_XATTR_REPLACE, th);
2108
2109         return rc;
2110 }
2111
2112 /**
2113  * Reset parent FID on OST object
2114  *
2115  * Replace parent FID with @dt object FID, which is only called during migration
2116  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2117  * the FID is changed.
2118  *
2119  * \param[in] env execution environment
2120  * \param[in] dt dt_object whose stripes's parent FID will be reset
2121  * \parem[in] th thandle
2122  * \param[in] declare if it is declare
2123  *
2124  * \retval      0 if reset succeeds
2125  * \retval      negative errno if reset fails
2126  */
2127 static int lod_object_replace_parent_fid(const struct lu_env *env,
2128                                          struct dt_object *dt,
2129                                          struct thandle *th, bool declare)
2130 {
2131         struct lod_object *lo = lod_dt_obj(dt);
2132         struct lod_thread_info  *info = lod_env_info(env);
2133         struct lu_buf *buf = &info->lti_buf;
2134         struct filter_fid *ff;
2135         struct lod_obj_stripe_cb_data data;
2136         int rc;
2137         ENTRY;
2138
2139         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2140
2141         /* set xattr to each stripes, if needed */
2142         rc = lod_load_striping(env, lo);
2143         if (rc != 0)
2144                 RETURN(rc);
2145
2146         if (!lod_obj_is_striped(dt))
2147                 RETURN(0);
2148
2149         if (info->lti_ea_store_size < sizeof(*ff)) {
2150                 rc = lod_ea_store_resize(info, sizeof(*ff));
2151                 if (rc != 0)
2152                         RETURN(rc);
2153         }
2154
2155         buf->lb_buf = info->lti_ea_store;
2156         buf->lb_len = info->lti_ea_store_size;
2157
2158         data.locd_declare = declare;
2159         rc = lod_obj_for_each_stripe(env, lo, th,
2160                         lod_obj_stripe_replace_parent_fid_cb, &data);
2161
2162         RETURN(rc);
2163 }
2164
2165 inline __u16 lod_comp_entry_stripecnt(struct lod_object *lo,
2166                                       struct lod_layout_component *entry,
2167                                       bool is_dir)
2168 {
2169         if (is_dir)
2170                 return  0;
2171         else if (lod_comp_inited(entry))
2172                 return entry->llc_stripenr;
2173         else
2174                 return lod_get_stripecnt(lu2lod_dev(lod2lu_obj(lo)->lo_dev), lo,
2175                                          entry->llc_stripenr);
2176 }
2177
2178 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2179 {
2180         int magic, size = 0, i;
2181         struct lod_layout_component *comp_entries;
2182         __u16 comp_cnt;
2183         bool is_composite;
2184
2185         if (is_dir) {
2186                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2187                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2188                 is_composite =
2189                         lo->ldo_def_striping->lds_def_striping_is_composite;
2190         } else {
2191                 comp_cnt = lo->ldo_comp_cnt;
2192                 comp_entries = lo->ldo_comp_entries;
2193                 is_composite = lo->ldo_is_composite;
2194         }
2195
2196
2197         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2198         if (is_composite) {
2199                 size = sizeof(struct lov_comp_md_v1) +
2200                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2201                 LASSERT(size % sizeof(__u64) == 0);
2202         }
2203
2204         for (i = 0; i < comp_cnt; i++) {
2205                 __u16 stripenr;
2206
2207                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2208                 stripenr = lod_comp_entry_stripecnt(lo, &comp_entries[i],
2209                                                     is_dir);
2210                 size += lov_user_md_size(stripenr, magic);
2211                 LASSERT(size % sizeof(__u64) == 0);
2212         }
2213         return size;
2214 }
2215
2216 /**
2217  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2218  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2219  * to be added.
2220   *
2221  * \param[in] env       execution environment
2222  * \param[in] dt        dt_object to add components on
2223  * \param[in] buf       buffer contains components to be added
2224  * \parem[in] th        thandle
2225  *
2226  * \retval      0 on success
2227  * \retval      negative errno on failure
2228  */
2229 static int lod_declare_layout_add(const struct lu_env *env,
2230                                   struct dt_object *dt,
2231                                   const struct lu_buf *buf,
2232                                   struct thandle *th)
2233 {
2234         struct lod_thread_info  *info = lod_env_info(env);
2235         struct lod_layout_component *comp_array, *lod_comp;
2236         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2237         struct dt_object *next = dt_object_child(dt);
2238         struct lov_desc         *desc = &d->lod_desc;
2239         struct lod_object       *lo = lod_dt_obj(dt);
2240         struct lov_user_md_v3   *v3;
2241         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2242         __u32   magic;
2243         __u64   prev_end;
2244         int     i, rc, array_cnt;
2245         ENTRY;
2246
2247         LASSERT(lo->ldo_is_composite);
2248
2249         prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2250         rc = lod_verify_striping(d, buf, false, prev_end);
2251         if (rc != 0)
2252                 RETURN(rc);
2253
2254         magic = comp_v1->lcm_magic;
2255         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2256                 lustre_swab_lov_comp_md_v1(comp_v1);
2257                 magic = comp_v1->lcm_magic;
2258         }
2259
2260         if (magic != LOV_USER_MAGIC_COMP_V1)
2261                 RETURN(-EINVAL);
2262
2263         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2264         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2265         if (comp_array == NULL)
2266                 RETURN(-ENOMEM);
2267
2268         memcpy(comp_array, lo->ldo_comp_entries,
2269                sizeof(*comp_array) * lo->ldo_comp_cnt);
2270
2271         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2272                 struct lov_user_md_v1 *v1;
2273                 struct lu_extent *ext;
2274
2275                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2276                                 comp_v1->lcm_entries[i].lcme_offset);
2277                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2278
2279                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2280                 lod_comp->llc_extent.e_start = ext->e_start;
2281                 lod_comp->llc_extent.e_end = ext->e_end;
2282                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2283
2284                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2285                 if (!lod_comp->llc_stripenr ||
2286                     lod_comp->llc_stripenr == (__u16)-1)
2287                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2288                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2289                 if (!lod_comp->llc_stripe_size)
2290                         lod_comp->llc_stripe_size =
2291                                 desc->ld_default_stripe_size;
2292
2293                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2294                         v3 = (struct lov_user_md_v3 *) v1;
2295                         if (v3->lmm_pool_name[0] != '\0') {
2296                                 rc = lod_set_pool(&lod_comp->llc_pool,
2297                                                   v3->lmm_pool_name);
2298                                 if (rc)
2299                                         GOTO(error, rc);
2300                         }
2301                 }
2302         }
2303
2304         OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2305         lo->ldo_comp_entries = comp_array;
2306         lo->ldo_comp_cnt = array_cnt;
2307         /* No need to increase layout generation here, it will be increased
2308          * later when generating component ID for the new components */
2309
2310         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2311         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2312                                               XATTR_NAME_LOV, 0, th);
2313         if (rc)
2314                 GOTO(error, rc);
2315
2316         RETURN(0);
2317
2318 error:
2319         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2320                 lod_comp = &comp_array[i];
2321                 if (lod_comp->llc_pool != NULL) {
2322                         OBD_FREE(lod_comp->llc_pool,
2323                                  strlen(lod_comp->llc_pool) + 1);
2324                         lod_comp->llc_pool = NULL;
2325                 }
2326         }
2327         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2328         RETURN(rc);
2329 }
2330
2331 /**
2332  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2333  * the '$field' can only be 'flags' now. The xattr value is binary
2334  * lov_comp_md_v1 which contains the component ID(s) and the value of
2335  * the field to be modified.
2336  *
2337  * \param[in] env       execution environment
2338  * \param[in] dt        dt_object to be modified
2339  * \param[in] op        operation string, like "set.flags"
2340  * \param[in] buf       buffer contains components to be set
2341  * \parem[in] th        thandle
2342  *
2343  * \retval      0 on success
2344  * \retval      negative errno on failure
2345  */
2346 static int lod_declare_layout_set(const struct lu_env *env,
2347                                   struct dt_object *dt,
2348                                   char *op, const struct lu_buf *buf,
2349                                   struct thandle *th)
2350 {
2351         struct lod_layout_component     *lod_comp;
2352         struct lod_thread_info  *info = lod_env_info(env);
2353         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2354         struct lod_object       *lo = lod_dt_obj(dt);
2355         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2356         __u32   magic, id;
2357         int     i, j, rc;
2358         bool    changed = false;
2359         ENTRY;
2360
2361         if (strcmp(op, "set.flags") != 0) {
2362                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2363                        lod2obd(d)->obd_name, op);
2364                 RETURN(-ENOTSUPP);
2365         }
2366
2367         magic = comp_v1->lcm_magic;
2368         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2369                 lustre_swab_lov_comp_md_v1(comp_v1);
2370                 magic = comp_v1->lcm_magic;
2371         }
2372
2373         if (magic != LOV_USER_MAGIC_COMP_V1)
2374                 RETURN(-EINVAL);
2375
2376         if (comp_v1->lcm_entry_count == 0) {
2377                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2378                        lod2obd(d)->obd_name);
2379                 RETURN(-EINVAL);
2380         }
2381
2382         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2383                 id = comp_v1->lcm_entries[i].lcme_id;
2384
2385                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2386                         lod_comp = &lo->ldo_comp_entries[j];
2387                         if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2388                                 lod_comp->llc_flags =
2389                                         comp_v1->lcm_entries[i].lcme_flags;
2390                                 changed = true;
2391                         }
2392                 }
2393         }
2394
2395         if (!changed) {
2396                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2397                        lod2obd(d)->obd_name);
2398                 RETURN(-EINVAL);
2399         }
2400
2401         lod_obj_inc_layout_gen(lo);
2402
2403         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2404         rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2405                                               XATTR_NAME_LOV, 0, th);
2406         RETURN(rc);
2407 }
2408
2409 /**
2410  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2411  * and the xattr value is a unique component ID or a special lcme_id.
2412  *
2413  * \param[in] env       execution environment
2414  * \param[in] dt        dt_object to be operated on
2415  * \param[in] buf       buffer contains component ID or lcme_id
2416  * \parem[in] th        thandle
2417  *
2418  * \retval      0 on success
2419  * \retval      negative errno on failure
2420  */
2421 static int lod_declare_layout_del(const struct lu_env *env,
2422                                   struct dt_object *dt,
2423                                   const struct lu_buf *buf,
2424                                   struct thandle *th)
2425 {
2426         struct lod_thread_info  *info = lod_env_info(env);
2427         struct dt_object        *next = dt_object_child(dt);
2428         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2429         struct lod_object       *lo = lod_dt_obj(dt);
2430         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
2431         __u32   id;
2432         int     rc, i, j, left;
2433         ENTRY;
2434
2435         LASSERT(lo->ldo_is_composite);
2436
2437         id = *(__u32 *)buf->lb_buf;
2438         if (id == 0 || id == LCME_ID_NONE) {
2439                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
2440                        lod2obd(d)->obd_name, id);
2441                 RETURN(-EINVAL);
2442         }
2443
2444         left = lo->ldo_comp_cnt;
2445         if (left <= 0)
2446                 RETURN(-EINVAL);
2447
2448         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2449                 struct lod_layout_component *lod_comp;
2450
2451                 lod_comp = &lo->ldo_comp_entries[i];
2452
2453                 if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
2454                         continue;
2455                 else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
2456                          !(id & lod_comp->llc_flags))
2457                         continue;
2458
2459                 if (left != (i + 1)) {
2460                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2461                                "a hole.\n", lod2obd(d)->obd_name);
2462                         RETURN(-EINVAL);
2463                 }
2464                 left--;
2465
2466                 /* Mark the component as deleted */
2467                 lod_comp->llc_id = LCME_ID_INVAL;
2468
2469                 /* Not instantiated component */
2470                 if (lod_comp->llc_stripe == NULL)
2471                         continue;
2472
2473                 LASSERT(lod_comp->llc_stripenr > 0);
2474                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2475                         struct dt_object *obj = lod_comp->llc_stripe[j];
2476
2477                         if (obj == NULL)
2478                                 continue;
2479                         rc = lod_sub_object_declare_destroy(env, obj, th);
2480                         if (rc)
2481                                 RETURN(rc);
2482                 }
2483         }
2484
2485         LASSERTF(left >= 0, "left = %d\n", left);
2486         if (left == lo->ldo_comp_cnt) {
2487                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2488                        lod2obd(d)->obd_name, id);
2489                 RETURN(-EINVAL);
2490         }
2491
2492         memset(attr, 0, sizeof(*attr));
2493         attr->la_valid = LA_SIZE;
2494         rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2495         if (rc)
2496                 RETURN(rc);
2497
2498         if (left > 0) {
2499                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2500                 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2501                                                       XATTR_NAME_LOV, 0, th);
2502         } else {
2503                 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2504                                                       th);
2505         }
2506
2507         RETURN(rc);
2508 }
2509
2510 /**
2511  * Declare layout add/set/del operations issued by special xattr names:
2512  *
2513  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2514  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2515  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2516  *
2517  * \param[in] env       execution environment
2518  * \param[in] dt        object
2519  * \param[in] name      name of xattr
2520  * \param[in] buf       lu_buf contains xattr value
2521  * \param[in] th        transaction handle
2522  *
2523  * \retval              0 on success
2524  * \retval              negative if failed
2525  */
2526 static int lod_declare_modify_layout(const struct lu_env *env,
2527                                      struct dt_object *dt,
2528                                      const char *name,
2529                                      const struct lu_buf *buf,
2530                                      struct thandle *th)
2531 {
2532         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2533         struct lod_object *lo = lod_dt_obj(dt);
2534         struct dt_object *next = dt_object_child(&lo->ldo_obj);
2535         char *op;
2536         int rc, len = strlen(XATTR_LUSTRE_LOV);
2537         ENTRY;
2538
2539         LASSERT(dt_object_exists(dt));
2540
2541         if (strlen(name) <= len || name[len] != '.') {
2542                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2543                        lod2obd(d)->obd_name, name);
2544                 RETURN(-EINVAL);
2545         }
2546         len++;
2547
2548         dt_write_lock(env, next, 0);
2549         rc = lod_load_striping_locked(env, lo);
2550         if (rc)
2551                 GOTO(unlock, rc);
2552
2553         /* the layout to be modified must be a composite layout */
2554         if (!lo->ldo_is_composite) {
2555                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2556                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2557                 GOTO(unlock, rc = -EINVAL);
2558         }
2559
2560         op = (char *)name + len;
2561         if (strcmp(op, "add") == 0) {
2562                 rc = lod_declare_layout_add(env, dt, buf, th);
2563         } else if (strcmp(op, "del") == 0) {
2564                 rc = lod_declare_layout_del(env, dt, buf, th);
2565         } else if (strncmp(op, "set", strlen("set")) == 0) {
2566                 rc = lod_declare_layout_set(env, dt, op, buf, th);
2567         } else  {
2568                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2569                        lod2obd(d)->obd_name, name);
2570                 GOTO(unlock, rc = -ENOTSUPP);
2571         }
2572 unlock:
2573         if (rc)
2574                 lod_object_free_striping(env, lo);
2575         dt_write_unlock(env, next);
2576
2577         RETURN(rc);
2578 }
2579
2580 /**
2581  * Implementation of dt_object_operations::do_declare_xattr_set.
2582  *
2583  * \see dt_object_operations::do_declare_xattr_set() in the API description
2584  * for details.
2585  *
2586  * the extension to the API:
2587  *   - declaring LOVEA requests striping creation
2588  *   - LU_XATTR_REPLACE means layout swap
2589  */
2590 static int lod_declare_xattr_set(const struct lu_env *env,
2591                                  struct dt_object *dt,
2592                                  const struct lu_buf *buf,
2593                                  const char *name, int fl,
2594                                  struct thandle *th)
2595 {
2596         struct dt_object *next = dt_object_child(dt);
2597         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2598         __u32             mode;
2599         int               rc;
2600         ENTRY;
2601
2602         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2603         if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2604             (strcmp(name, XATTR_NAME_LOV) == 0 ||
2605              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2606                 /*
2607                  * this is a request to create object's striping.
2608                  *
2609                  * allow to declare predefined striping on a new (!mode) object
2610                  * which is supposed to be replay of regular file creation
2611                  * (when LOV setting is declared)
2612                  *
2613                  * LU_XATTR_REPLACE is set to indicate a layout swap
2614                  */
2615                 if (dt_object_exists(dt)) {
2616                         rc = dt_attr_get(env, next, attr);
2617                         if (rc)
2618                                 RETURN(rc);
2619                 } else {
2620                         memset(attr, 0, sizeof(*attr));
2621                         attr->la_valid = LA_TYPE | LA_MODE;
2622                         attr->la_mode = S_IFREG;
2623                 }
2624                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2625         } else if (S_ISREG(mode) &&
2626                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2627                    strncmp(name, XATTR_LUSTRE_LOV,
2628                            strlen(XATTR_LUSTRE_LOV)) == 0) {
2629                 /*
2630                  * this is a request to modify object's striping.
2631                  * add/set/del component(s).
2632                  */
2633                 if (!dt_object_exists(dt))
2634                         RETURN(-ENOENT);
2635
2636                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2637         } else if (S_ISDIR(mode)) {
2638                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2639         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2640                 rc = lod_object_replace_parent_fid(env, dt, th, true);
2641         } else {
2642                 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2643                                                       fl, th);
2644         }
2645
2646         RETURN(rc);
2647 }
2648
2649 /**
2650  * Apply xattr changes to the object.
2651  *
2652  * Applies xattr changes to the object and the stripes if the latter exist.
2653  *
2654  * \param[in] env       execution environment
2655  * \param[in] dt        object
2656  * \param[in] buf       buffer pointing to the new value of xattr
2657  * \param[in] name      name of xattr
2658  * \param[in] fl        flags
2659  * \param[in] th        transaction handle
2660  *
2661  * \retval              0 on success
2662  * \retval              negative if failed
2663  */
2664 static int lod_xattr_set_internal(const struct lu_env *env,
2665                                   struct dt_object *dt,
2666                                   const struct lu_buf *buf,
2667                                   const char *name, int fl,
2668                                   struct thandle *th)
2669 {
2670         struct dt_object        *next = dt_object_child(dt);
2671         struct lod_object       *lo = lod_dt_obj(dt);
2672         int                     rc;
2673         int                     i;
2674         ENTRY;
2675
2676         rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2677         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2678                 RETURN(rc);
2679
2680         /* Note: Do not set LinkEA on sub-stripes, otherwise
2681          * it will confuse the fid2path process(see mdt_path_current()).
2682          * The linkEA between master and sub-stripes is set in
2683          * lod_xattr_set_lmv(). */
2684         if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2685                 RETURN(0);
2686
2687         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2688                 LASSERT(lo->ldo_stripe[i]);
2689
2690                 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2691                                               fl, th);
2692                 if (rc != 0)
2693                         break;
2694         }
2695
2696         RETURN(rc);
2697 }
2698
2699 /**
2700  * Delete an extended attribute.
2701  *
2702  * Deletes specified xattr from the object and the stripes if the latter exist.
2703  *
2704  * \param[in] env       execution environment
2705  * \param[in] dt        object
2706  * \param[in] name      name of xattr
2707  * \param[in] th        transaction handle
2708  *
2709  * \retval              0 on success
2710  * \retval              negative if failed
2711  */
2712 static int lod_xattr_del_internal(const struct lu_env *env,
2713                                   struct dt_object *dt,
2714                                   const char *name, struct thandle *th)
2715 {
2716         struct dt_object        *next = dt_object_child(dt);
2717         struct lod_object       *lo = lod_dt_obj(dt);
2718         int                     rc;
2719         int                     i;
2720         ENTRY;
2721
2722         rc = lod_sub_object_xattr_del(env, next, name, th);
2723         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2724                 RETURN(rc);
2725
2726         if (lo->ldo_dir_stripenr == 0)
2727                 RETURN(rc);
2728
2729         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2730                 LASSERT(lo->ldo_stripe[i]);
2731
2732                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2733                                               th);
2734                 if (rc != 0)
2735                         break;
2736         }
2737
2738         RETURN(rc);
2739 }
2740
2741 /**
2742  * Set default striping on a directory.
2743  *
2744  * Sets specified striping on a directory object unless it matches the default
2745  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2746  * EA. This striping will be used when regular file is being created in this
2747  * directory.
2748  *
2749  * \param[in] env       execution environment
2750  * \param[in] dt        the striped object
2751  * \param[in] buf       buffer with the striping
2752  * \param[in] name      name of EA
2753  * \param[in] fl        xattr flag (see OSD API description)
2754  * \param[in] th        transaction handle
2755  *
2756  * \retval              0 on success
2757  * \retval              negative if failed
2758  */
2759 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2760                                     struct dt_object *dt,
2761                                     const struct lu_buf *buf,
2762                                     const char *name, int fl,
2763                                     struct thandle *th)
2764 {
2765         struct lov_user_md_v1   *lum;
2766         struct lov_user_md_v3   *v3 = NULL;
2767         const char              *pool_name = NULL;
2768         int                      rc;
2769         bool                     is_del;
2770         ENTRY;
2771
2772         LASSERT(buf != NULL && buf->lb_buf != NULL);
2773         lum = buf->lb_buf;
2774
2775         switch (lum->lmm_magic) {
2776         case LOV_USER_MAGIC_V3:
2777                 v3 = buf->lb_buf;
2778                 if (v3->lmm_pool_name[0] != '\0')
2779                         pool_name = v3->lmm_pool_name;
2780                 /* fall through */
2781         case LOV_USER_MAGIC_V1:
2782                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2783                  * (i.e. all default values specified) then delete default
2784                  * striping from dir. */
2785                 CDEBUG(D_LAYOUT,
2786                        "set default striping: sz %u # %u offset %d %s %s\n",
2787                        (unsigned)lum->lmm_stripe_size,
2788                        (unsigned)lum->lmm_stripe_count,
2789                        (int)lum->lmm_stripe_offset,
2790                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2791
2792                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2793                                              lum->lmm_stripe_count,
2794                                              lum->lmm_stripe_offset,
2795                                              pool_name);
2796                 break;
2797         case LOV_USER_MAGIC_COMP_V1:
2798                 is_del = false;
2799                 break;
2800         default:
2801                 CERROR("Invalid magic %x\n", lum->lmm_magic);
2802                 RETURN(-EINVAL);
2803         }
2804
2805         if (is_del) {
2806                 rc = lod_xattr_del_internal(env, dt, name, th);
2807                 if (rc == -ENODATA)
2808                         rc = 0;
2809         } else {
2810                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2811         }
2812
2813         RETURN(rc);
2814 }
2815
2816 /**
2817  * Set default striping on a directory object.
2818  *
2819  * Sets specified striping on a directory object unless it matches the default
2820  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2821  * EA. This striping will be used when a new directory is being created in the
2822  * directory.
2823  *
2824  * \param[in] env       execution environment
2825  * \param[in] dt        the striped object
2826  * \param[in] buf       buffer with the striping
2827  * \param[in] name      name of EA
2828  * \param[in] fl        xattr flag (see OSD API description)
2829  * \param[in] th        transaction handle
2830  *
2831  * \retval              0 on success
2832  * \retval              negative if failed
2833  */
2834 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2835                                             struct dt_object *dt,
2836                                             const struct lu_buf *buf,
2837                                             const char *name, int fl,
2838                                             struct thandle *th)
2839 {
2840         struct lmv_user_md_v1   *lum;
2841         int                      rc;
2842         ENTRY;
2843
2844         LASSERT(buf != NULL && buf->lb_buf != NULL);
2845         lum = buf->lb_buf;
2846
2847         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2848               le32_to_cpu(lum->lum_stripe_count),
2849               (int)le32_to_cpu(lum->lum_stripe_offset));
2850
2851         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2852                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2853                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2854                 rc = lod_xattr_del_internal(env, dt, name, th);
2855                 if (rc == -ENODATA)
2856                         rc = 0;
2857         } else {
2858                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2859                 if (rc != 0)
2860                         RETURN(rc);
2861         }
2862
2863         RETURN(rc);
2864 }
2865
2866 /**
2867  * Turn directory into a striped directory.
2868  *
2869  * During replay the client sends the striping created before MDT
2870  * failure, then the layer above LOD sends this defined striping
2871  * using ->do_xattr_set(), so LOD uses this method to replay creation
2872  * of the stripes. Notice the original information for the striping
2873  * (#stripes, FIDs, etc) was transferred in declare path.
2874  *
2875  * \param[in] env       execution environment
2876  * \param[in] dt        the striped object
2877  * \param[in] buf       not used currently
2878  * \param[in] name      not used currently
2879  * \param[in] fl        xattr flag (see OSD API description)
2880  * \param[in] th        transaction handle
2881  *
2882  * \retval              0 on success
2883  * \retval              negative if failed
2884  */
2885 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2886                              const struct lu_buf *buf, const char *name,
2887                              int fl, struct thandle *th)
2888 {
2889         struct lod_object       *lo = lod_dt_obj(dt);
2890         struct lod_thread_info  *info = lod_env_info(env);
2891         struct lu_attr          *attr = &info->lti_attr;
2892         struct dt_object_format *dof = &info->lti_format;
2893         struct lu_buf           lmv_buf;
2894         struct lu_buf           slave_lmv_buf;
2895         struct lmv_mds_md_v1    *lmm;
2896         struct lmv_mds_md_v1    *slave_lmm = NULL;
2897         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2898         int                     i;
2899         int                     rc;
2900         ENTRY;
2901
2902         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2903                 RETURN(-ENOTDIR);
2904
2905         /* The stripes are supposed to be allocated in declare phase,
2906          * if there are no stripes being allocated, it will skip */
2907         if (lo->ldo_dir_stripenr == 0)
2908                 RETURN(0);
2909
2910         rc = dt_attr_get(env, dt_object_child(dt), attr);
2911         if (rc != 0)
2912                 RETURN(rc);
2913
2914         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2915                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
2916         dof->dof_type = DFT_DIR;
2917
2918         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2919         if (rc != 0)
2920                 RETURN(rc);
2921         lmm = lmv_buf.lb_buf;
2922
2923         OBD_ALLOC_PTR(slave_lmm);
2924         if (slave_lmm == NULL)
2925                 RETURN(-ENOMEM);
2926
2927         lod_prep_slave_lmv_md(slave_lmm, lmm);
2928         slave_lmv_buf.lb_buf = slave_lmm;
2929         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2930
2931         rec->rec_type = S_IFDIR;
2932         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2933                 struct dt_object *dto;
2934                 char             *stripe_name = info->lti_key;
2935                 struct lu_name          *sname;
2936                 struct linkea_data       ldata          = { NULL };
2937                 struct lu_buf            linkea_buf;
2938
2939                 dto = lo->ldo_stripe[i];
2940
2941                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2942                 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2943                                            th);
2944                 if (rc != 0) {
2945                         dt_write_unlock(env, dto);
2946                         GOTO(out, rc);
2947                 }
2948
2949                 rc = lod_sub_object_ref_add(env, dto, th);
2950                 dt_write_unlock(env, dto);
2951                 if (rc != 0)
2952                         GOTO(out, rc);
2953
2954                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2955                 rc = lod_sub_object_index_insert(env, dto,
2956                                 (const struct dt_rec *)rec,
2957                                 (const struct dt_key *)dot, th, 0);
2958                 if (rc != 0)
2959                         GOTO(out, rc);
2960
2961                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2962                 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2963                                (const struct dt_key *)dotdot, th, 0);
2964                 if (rc != 0)
2965                         GOTO(out, rc);
2966
2967                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2968                     cfs_fail_val != i) {
2969                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2970                             cfs_fail_val == i)
2971                                 slave_lmm->lmv_master_mdt_index =
2972                                                         cpu_to_le32(i + 1);
2973                         else
2974                                 slave_lmm->lmv_master_mdt_index =
2975                                                         cpu_to_le32(i);
2976
2977                         rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2978                                                       XATTR_NAME_LMV, fl, th);
2979                         if (rc != 0)
2980                                 GOTO(out, rc);
2981                 }
2982
2983                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2984                     cfs_fail_val == i)
2985                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2986                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2987                 else
2988                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2989                                  PFID(lu_object_fid(&dto->do_lu)), i);
2990
2991                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2992                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2993                                       sname, lu_object_fid(&dt->do_lu));
2994                 if (rc != 0)
2995                         GOTO(out, rc);
2996
2997                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2998                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2999                 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
3000                                         XATTR_NAME_LINK, 0, th);
3001                 if (rc != 0)
3002                         GOTO(out, rc);
3003
3004                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3005                 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
3006                                (const struct dt_rec *)rec,
3007                                (const struct dt_key *)stripe_name, th, 0);
3008                 if (rc != 0)
3009                         GOTO(out, rc);
3010
3011                 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
3012                 if (rc != 0)
3013                         GOTO(out, rc);
3014         }
3015
3016         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3017                 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
3018                                               &lmv_buf, XATTR_NAME_LMV, fl, th);
3019 out:
3020         if (slave_lmm != NULL)
3021                 OBD_FREE_PTR(slave_lmm);
3022
3023         RETURN(rc);
3024 }
3025
3026 /**
3027  * Helper function to declare/execute creation of a striped directory
3028  *
3029  * Called in declare/create object path, prepare striping for a directory
3030  * and prepare defaults data striping for the objects to be created in
3031  * that directory. Notice the function calls "declaration" or "execution"
3032  * methods depending on \a declare param. This is a consequence of the
3033  * current approach while we don't have natural distributed transactions:
3034  * we basically execute non-local updates in the declare phase. So, the
3035  * arguments for the both phases are the same and this is the reason for
3036  * this function to exist.
3037  *
3038  * \param[in] env       execution environment
3039  * \param[in] dt        object
3040  * \param[in] attr      attributes the stripes will be created with
3041  * \param[in] dof       format of stripes (see OSD API description)
3042  * \param[in] th        transaction handle
3043  * \param[in] declare   where to call "declare" or "execute" methods
3044  *
3045  * \retval              0 on success
3046  * \retval              negative if failed
3047  */
3048 static int lod_dir_striping_create_internal(const struct lu_env *env,
3049                                             struct dt_object *dt,
3050                                             struct lu_attr *attr,
3051                                             struct dt_object_format *dof,
3052                                             struct thandle *th,
3053                                             bool declare)
3054 {
3055         struct lod_thread_info *info = lod_env_info(env);
3056         struct lod_object *lo = lod_dt_obj(dt);
3057         const struct lod_default_striping *lds = lo->ldo_def_striping;
3058         int rc;
3059         ENTRY;
3060
3061         LASSERT(ergo(lds != NULL,
3062                      lds->lds_def_striping_set ||
3063                      lds->lds_dir_def_striping_set));
3064
3065         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3066                                  lo->ldo_dir_stripe_offset)) {
3067                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3068                 int stripe_count = lo->ldo_dir_stripenr;
3069
3070                 if (info->lti_ea_store_size < sizeof(*v1)) {
3071                         rc = lod_ea_store_resize(info, sizeof(*v1));
3072                         if (rc != 0)
3073                                 RETURN(rc);
3074                         v1 = info->lti_ea_store;
3075                 }
3076
3077                 memset(v1, 0, sizeof(*v1));
3078                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3079                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3080                 v1->lum_stripe_offset =
3081                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
3082
3083                 info->lti_buf.lb_buf = v1;
3084                 info->lti_buf.lb_len = sizeof(*v1);
3085
3086                 if (declare)
3087                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
3088                                                        &info->lti_buf, dof, th);
3089                 else
3090                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3091                                                XATTR_NAME_LMV, 0, th);
3092                 if (rc != 0)
3093                         RETURN(rc);
3094         }
3095
3096         /* Transfer default LMV striping from the parent */
3097         if (lds != NULL && lds->lds_dir_def_striping_set &&
3098             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3099                                  lds->lds_dir_def_stripe_offset)) {
3100                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3101
3102                 if (info->lti_ea_store_size < sizeof(*v1)) {
3103                         rc = lod_ea_store_resize(info, sizeof(*v1));
3104                         if (rc != 0)
3105                                 RETURN(rc);
3106                         v1 = info->lti_ea_store;
3107                 }
3108
3109                 memset(v1, 0, sizeof(*v1));
3110                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3111                 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3112                 v1->lum_stripe_offset =
3113                                 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3114                 v1->lum_hash_type =
3115                                 cpu_to_le32(lds->lds_dir_def_hash_type);
3116
3117                 info->lti_buf.lb_buf = v1;
3118                 info->lti_buf.lb_len = sizeof(*v1);
3119                 if (declare)
3120                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3121                                                        XATTR_NAME_DEFAULT_LMV,
3122                                                        0, th);
3123                 else
3124                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3125                                                   &info->lti_buf,
3126                                                   XATTR_NAME_DEFAULT_LMV, 0,
3127                                                   th);
3128                 if (rc != 0)
3129                         RETURN(rc);
3130         }
3131
3132         /* Transfer default LOV striping from the parent */
3133         if (lds != NULL && lds->lds_def_striping_set &&
3134             lds->lds_def_comp_cnt != 0) {
3135                 struct lov_mds_md *lmm;
3136                 int lmm_size = lod_comp_md_size(lo, true);
3137
3138                 if (info->lti_ea_store_size < lmm_size) {
3139                         rc = lod_ea_store_resize(info, lmm_size);
3140                         if (rc != 0)
3141                                 RETURN(rc);
3142                 }
3143                 lmm = info->lti_ea_store;
3144
3145                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3146                 if (rc != 0)
3147                         RETURN(rc);
3148
3149                 info->lti_buf.lb_buf = lmm;
3150                 info->lti_buf.lb_len = lmm_size;
3151
3152                 if (declare)
3153                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3154                                                        XATTR_NAME_LOV, 0, th);
3155                 else
3156                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3157                                                       XATTR_NAME_LOV, 0, th);
3158                 if (rc != 0)
3159                         RETURN(rc);
3160         }
3161
3162         RETURN(0);
3163 }
3164
3165 static int lod_declare_dir_striping_create(const struct lu_env *env,
3166                                            struct dt_object *dt,
3167                                            struct lu_attr *attr,
3168                                            struct dt_object_format *dof,
3169                                            struct thandle *th)
3170 {
3171         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3172 }
3173
3174 static int lod_dir_striping_create(const struct lu_env *env,
3175                                    struct dt_object *dt,
3176                                    struct lu_attr *attr,
3177                                    struct dt_object_format *dof,
3178                                    struct thandle *th)
3179 {
3180         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3181 }
3182
3183 /**
3184  * Make LOV EA for striped object.
3185  *
3186  * Generate striping information and store it in the LOV EA of the given
3187  * object. The caller must ensure nobody else is calling the function
3188  * against the object concurrently. The transaction must be started.
3189  * FLDB service must be running as well; it's used to map FID to the target,
3190  * which is stored in LOV EA.
3191  *
3192  * \param[in] env               execution environment for this thread
3193  * \param[in] lo                LOD object
3194  * \param[in] th                transaction handle
3195  *
3196  * \retval                      0 if LOV EA is stored successfully
3197  * \retval                      negative error number on failure
3198  */
3199 static int lod_generate_and_set_lovea(const struct lu_env *env,
3200                                       struct lod_object *lo,
3201                                       struct thandle *th)
3202 {
3203         struct lod_thread_info  *info = lod_env_info(env);
3204         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3205         struct lov_mds_md_v1    *lmm;
3206         int                      rc, lmm_size;
3207         ENTRY;
3208
3209         LASSERT(lo);
3210
3211         if (lo->ldo_comp_cnt == 0) {
3212                 lod_object_free_striping(env, lo);
3213                 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3214                 RETURN(rc);
3215         }
3216
3217         lmm_size = lod_comp_md_size(lo, false);
3218         if (info->lti_ea_store_size < lmm_size) {
3219                 rc = lod_ea_store_resize(info, lmm_size);
3220                 if (rc)
3221                         RETURN(rc);
3222         }
3223         lmm = info->lti_ea_store;
3224
3225         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3226         if (rc)
3227                 RETURN(rc);
3228
3229         info->lti_buf.lb_buf = lmm;
3230         info->lti_buf.lb_len = lmm_size;
3231         rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3232                                       XATTR_NAME_LOV, 0, th);
3233         RETURN(rc);
3234 }
3235
3236 /**
3237  * Delete layout component(s)
3238  *
3239  * \param[in] env       execution environment for this thread
3240  * \param[in] dt        object
3241  * \param[in] th        transaction handle
3242  *
3243  * \retval      0 on success
3244  * \retval      negative error number on failure
3245  */
3246 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3247                           struct thandle *th)
3248 {
3249         struct lod_layout_component     *lod_comp;
3250         struct lod_object       *lo = lod_dt_obj(dt);
3251         struct dt_object        *next = dt_object_child(dt);
3252         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3253         int     rc, i, j, left;
3254
3255         LASSERT(lo->ldo_is_composite);
3256         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3257
3258         left = lo->ldo_comp_cnt;
3259         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3260                 lod_comp = &lo->ldo_comp_entries[i];
3261
3262                 if (lod_comp->llc_id != LCME_ID_INVAL)
3263                         break;
3264                 left--;
3265
3266                 /* Not instantiated component */
3267                 if (lod_comp->llc_stripe == NULL)
3268                         continue;
3269
3270                 LASSERT(lod_comp->llc_stripenr > 0);
3271                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3272                         struct dt_object *obj = lod_comp->llc_stripe[j];
3273
3274                         if (obj == NULL)
3275                                 continue;
3276                         rc = lod_sub_object_destroy(env, obj, th);
3277                         if (rc)
3278                                 GOTO(out, rc);
3279
3280                         lu_object_put(env, &obj->do_lu);
3281                         lod_comp->llc_stripe[j] = NULL;
3282                 }
3283                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3284                                         lod_comp->llc_stripes_allocated);
3285                 lod_comp->llc_stripe = NULL;
3286                 lod_comp->llc_stripes_allocated = 0;
3287                 lod_obj_set_pool(lo, i, NULL);
3288                 if (lod_comp->llc_ostlist.op_array) {
3289                         OBD_FREE(lod_comp->llc_ostlist.op_array,
3290                                  lod_comp->llc_ostlist.op_size);
3291                         lod_comp->llc_ostlist.op_array = NULL;
3292                         lod_comp->llc_ostlist.op_size = 0;
3293                 }
3294         }
3295
3296         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3297         if (left > 0) {
3298                 struct lod_layout_component     *comp_array;
3299
3300                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3301                 if (comp_array == NULL)
3302                         GOTO(out, rc = -ENOMEM);
3303
3304                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3305                        sizeof(*comp_array) * left);
3306
3307                 OBD_FREE(lo->ldo_comp_entries,
3308                          sizeof(*comp_array) * lo->ldo_comp_cnt);
3309                 lo->ldo_comp_entries = comp_array;
3310                 lo->ldo_comp_cnt = left;
3311                 lod_obj_inc_layout_gen(lo);
3312         } else {
3313                 lod_free_comp_entries(lo);
3314         }
3315
3316         LASSERT(dt_object_exists(dt));
3317         rc = dt_attr_get(env, next, attr);
3318         if (rc)
3319                 GOTO(out, rc);
3320
3321         if (attr->la_size > 0) {
3322                 attr->la_size = 0;
3323                 attr->la_valid = LA_SIZE;
3324                 rc = lod_sub_object_attr_set(env, next, attr, th);
3325                 if (rc)
3326                         GOTO(out, rc);
3327         }
3328
3329         rc = lod_generate_and_set_lovea(env, lo, th);
3330         EXIT;
3331 out:
3332         if (rc)
3333                 lod_object_free_striping(env, lo);
3334         return rc;
3335 }
3336
3337 /**
3338  * Implementation of dt_object_operations::do_xattr_set.
3339  *
3340  * Sets specified extended attribute on the object. Three types of EAs are
3341  * special:
3342  *   LOV EA - stores striping for a regular file or default striping (when set
3343  *            on a directory)
3344  *   LMV EA - stores a marker for the striped directories
3345  *   DMV EA - stores default directory striping
3346  *
3347  * When striping is applied to a non-striped existing object (this is called
3348  * late striping), then LOD notices the caller wants to turn the object into a
3349  * striped one. The stripe objects are created and appropriate EA is set:
3350  * LOV EA storing all the stripes directly or LMV EA storing just a small header
3351  * with striping configuration.
3352  *
3353  * \see dt_object_operations::do_xattr_set() in the API description for details.
3354  */
3355 static int lod_xattr_set(const struct lu_env *env,
3356                          struct dt_object *dt, const struct lu_buf *buf,
3357                          const char *name, int fl, struct thandle *th)
3358 {
3359         struct dt_object        *next = dt_object_child(dt);
3360         int                      rc;
3361         ENTRY;
3362
3363         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3364             strcmp(name, XATTR_NAME_LMV) == 0) {
3365                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3366
3367                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3368                                                 LMV_HASH_FLAG_MIGRATION)
3369                         rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3370                                                       th);
3371                 else
3372                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3373
3374                 RETURN(rc);
3375         }
3376
3377         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3378             strcmp(name, XATTR_NAME_LOV) == 0) {
3379                 /* default LOVEA */
3380                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3381                 RETURN(rc);
3382         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3383                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3384                 /* default LMVEA */
3385                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3386                                                       th);
3387                 RETURN(rc);
3388         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3389                    (!strcmp(name, XATTR_NAME_LOV) ||
3390                     !strncmp(name, XATTR_LUSTRE_LOV,
3391                              strlen(XATTR_LUSTRE_LOV)))) {
3392                 /* in case of lov EA swap, just set it
3393                  * if not, it is a replay so check striping match what we
3394                  * already have during req replay, declare_xattr_set()
3395                  * defines striping, then create() does the work */
3396                 if (fl & LU_XATTR_REPLACE) {
3397                         /* free stripes, then update disk */
3398                         lod_object_free_striping(env, lod_dt_obj(dt));
3399
3400                         rc = lod_sub_object_xattr_set(env, next, buf, name,
3401                                                       fl, th);
3402                 } else if (dt_object_remote(dt)) {
3403                         /* This only happens during migration, see
3404                          * mdd_migrate_create(), in which Master MDT will
3405                          * create a remote target object, and only set
3406                          * (migrating) stripe EA on the remote object,
3407                          * and does not need creating each stripes. */
3408                         rc = lod_sub_object_xattr_set(env, next, buf, name,
3409                                                       fl, th);
3410                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3411                         /* delete component(s) */
3412                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3413                         rc = lod_layout_del(env, dt, th);
3414                 } else {
3415                         /*
3416                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3417                          * it's going to create create file with specified
3418                          * component(s), the striping must have not being
3419                          * cached in this case;
3420                          *
3421                          * Otherwise, it's going to add/change component(s) to
3422                          * an existing file, the striping must have been cached
3423                          * in this case.
3424                          */
3425                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3426                                      !strcmp(name, XATTR_NAME_LOV),
3427                                 !lod_dt_obj(dt)->ldo_comp_cached));
3428
3429                         rc = lod_striping_create(env, dt, NULL, NULL, th);
3430                 }
3431                 RETURN(rc);
3432         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3433                 rc = lod_object_replace_parent_fid(env, dt, th, false);
3434
3435                 RETURN(rc);
3436         }
3437
3438         /* then all other xattr */
3439         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3440
3441         RETURN(rc);
3442 }
3443
3444 /**
3445  * Implementation of dt_object_operations::do_declare_xattr_del.
3446  *
3447  * \see dt_object_operations::do_declare_xattr_del() in the API description
3448  * for details.
3449  */
3450 static int lod_declare_xattr_del(const struct lu_env *env,
3451                                  struct dt_object *dt, const char *name,
3452                                  struct thandle *th)
3453 {
3454         struct lod_object       *lo = lod_dt_obj(dt);
3455         int                     rc;
3456         int                     i;
3457         ENTRY;
3458
3459         rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3460                                               name, th);
3461         if (rc != 0)
3462                 RETURN(rc);
3463
3464         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3465                 RETURN(0);
3466
3467         /* set xattr to each stripes, if needed */
3468         rc = lod_load_striping(env, lo);
3469         if (rc != 0)
3470                 RETURN(rc);
3471
3472         if (lo->ldo_dir_stripenr == 0)
3473                 RETURN(0);
3474
3475         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3476                 LASSERT(lo->ldo_stripe[i]);
3477                 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3478                                                       name, th);
3479                 if (rc != 0)
3480                         break;
3481         }
3482
3483         RETURN(rc);
3484 }
3485
3486 /**
3487  * Implementation of dt_object_operations::do_xattr_del.
3488  *
3489  * If EA storing a regular striping is being deleted, then release
3490  * all the references to the stripe objects in core.
3491  *
3492  * \see dt_object_operations::do_xattr_del() in the API description for details.
3493  */
3494 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3495                          const char *name, struct thandle *th)
3496 {
3497         struct dt_object        *next = dt_object_child(dt);
3498         struct lod_object       *lo = lod_dt_obj(dt);
3499         int                     rc;
3500         int                     i;
3501         ENTRY;
3502
3503         if (!strcmp(name, XATTR_NAME_LOV))
3504                 lod_object_free_striping(env, lod_dt_obj(dt));
3505
3506         rc = lod_sub_object_xattr_del(env, next, name, th);
3507         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3508                 RETURN(rc);
3509
3510         if (lo->ldo_dir_stripenr == 0)
3511                 RETURN(0);
3512
3513         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3514                 LASSERT(lo->ldo_stripe[i]);
3515
3516                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3517                 if (rc != 0)
3518                         break;
3519         }
3520
3521         RETURN(rc);
3522 }
3523
3524 /**
3525  * Implementation of dt_object_operations::do_xattr_list.
3526  *
3527  * \see dt_object_operations::do_xattr_list() in the API description
3528  * for details.
3529  */
3530 static int lod_xattr_list(const struct lu_env *env,
3531                           struct dt_object *dt, const struct lu_buf *buf)
3532 {
3533         return dt_xattr_list(env, dt_object_child(dt), buf);
3534 }
3535
3536 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3537 {
3538         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3539 }
3540
3541
3542 /**
3543  * Get default striping.
3544  *
3545  * \param[in] env               execution environment
3546  * \param[in] lo                object
3547  * \param[out] lds              default striping
3548  *
3549  * \retval              0 on success
3550  * \retval              negative if failed
3551  */
3552 static int lod_get_default_lov_striping(const struct lu_env *env,
3553                                         struct lod_object *lo,
3554                                         struct lod_default_striping *lds)
3555 {
3556         struct lod_thread_info *info = lod_env_info(env);
3557         struct lov_user_md_v1 *v1 = NULL;
3558         struct lov_user_md_v3 *v3 = NULL;
3559         struct lov_comp_md_v1 *comp_v1 = NULL;
3560         __u16   comp_cnt;
3561         bool    composite;
3562         int     rc, i;
3563         ENTRY;
3564
3565         lds->lds_def_striping_set = 0;
3566
3567         rc = lod_get_lov_ea(env, lo);
3568         if (rc < 0)
3569                 RETURN(rc);
3570
3571         if (rc < (typeof(rc))sizeof(struct lov_user_md))
3572                 RETURN(0);
3573
3574         v1 = info->lti_ea_store;
3575         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3576                 lustre_swab_lov_user_md_v1(v1);
3577         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3578                 v3 = (struct lov_user_md_v3 *)v1;
3579                 lustre_swab_lov_user_md_v3(v3);
3580         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3581                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3582                 lustre_swab_lov_comp_md_v1(comp_v1);
3583         }
3584
3585         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3586             v1->lmm_magic != LOV_MAGIC_COMP_V1)
3587                 RETURN(-ENOTSUPP);
3588
3589         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3590                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3591                 comp_cnt = comp_v1->lcm_entry_count;
3592                 if (comp_cnt == 0)
3593                         RETURN(-EINVAL);
3594                 composite = true;
3595         } else {
3596                 comp_cnt = 1;
3597                 composite = false;
3598         }
3599
3600         /* realloc default comp entries if necessary */
3601         rc = lod_def_striping_comp_resize(lds, comp_cnt);
3602         if (rc < 0)
3603                 RETURN(rc);
3604
3605         lds->lds_def_comp_cnt = comp_cnt;
3606         lds->lds_def_striping_is_composite = composite ? 1 : 0;
3607
3608         for (i = 0; i < comp_cnt; i++) {
3609                 struct lod_layout_component *lod_comp;
3610                 struct lu_extent *ext;
3611                 char *pool;
3612
3613                 lod_comp = &lds->lds_def_comp_entries[i];
3614                 /*
3615                  * reset lod_comp values, llc_stripes is always NULL in
3616                  * the default striping template, llc_pool will be reset
3617                  * later below.
3618                  */
3619                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3620
3621                 if (composite) {
3622                         v1 = (struct lov_user_md *)((char *)comp_v1 +
3623                                         comp_v1->lcm_entries[i].lcme_offset);
3624                         ext = &comp_v1->lcm_entries[i].lcme_extent;
3625                         lod_comp->llc_extent = *ext;
3626                 }
3627
3628                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3629                     v1->lmm_pattern != 0) {
3630                         lod_free_def_comp_entries(lds);
3631                         RETURN(-EINVAL);
3632                 }
3633
3634                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3635                        "stripe_offset=%d\n",
3636                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3637                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3638                        (int)v1->lmm_stripe_offset);
3639
3640                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3641                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3642                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3643
3644                 pool = NULL;
3645                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3646                         /* XXX: sanity check here */
3647                         v3 = (struct lov_user_md_v3 *) v1;
3648                         if (v3->lmm_pool_name[0] != '\0')
3649                                 pool = v3->lmm_pool_name;
3650                 }
3651                 lod_set_def_pool(lds, i, pool);
3652         }
3653
3654         lds->lds_def_striping_set = 1;
3655         RETURN(rc);
3656 }
3657
3658 /**
3659  * Get default directory striping.
3660  *
3661  * \param[in] env               execution environment
3662  * \param[in] lo                object
3663  * \param[out] lds              default striping
3664  *
3665  * \retval              0 on success
3666  * \retval              negative if failed
3667  */
3668 static int lod_get_default_lmv_striping(const struct lu_env *env,
3669                                         struct lod_object *lo,
3670                                         struct lod_default_striping *lds)
3671 {
3672         struct lod_thread_info  *info = lod_env_info(env);
3673         struct lmv_user_md_v1   *v1 = NULL;
3674         int                      rc;
3675         ENTRY;
3676
3677         lds->lds_dir_def_striping_set = 0;
3678         rc = lod_get_default_lmv_ea(env, lo);
3679         if (rc < 0)
3680                 RETURN(rc);
3681
3682         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3683                 RETURN(0);
3684
3685         v1 = info->lti_ea_store;
3686
3687         lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3688         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3689         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3690         lds->lds_dir_def_striping_set = 1;
3691
3692         RETURN(0);
3693 }
3694
3695 /**
3696  * Get default striping in the object.
3697  *
3698  * Get object default striping and default directory striping.
3699  *
3700  * \param[in] env               execution environment
3701  * \param[in] lo                object
3702  * \param[out] lds              default striping
3703  *
3704  * \retval              0 on success
3705  * \retval              negative if failed
3706  */
3707 static int lod_get_default_striping(const struct lu_env *env,
3708                                     struct lod_object *lo,
3709                                     struct lod_default_striping *lds)
3710 {
3711         int rc, rc1;
3712
3713         rc = lod_get_default_lov_striping(env, lo, lds);
3714         rc1 = lod_get_default_lmv_striping(env, lo, lds);
3715         if (rc == 0 && rc1 < 0)
3716                 rc = rc1;
3717
3718         return rc;
3719 }
3720
3721 /**
3722  * Apply default striping on object.
3723  *
3724  * If object striping pattern is not set, set to the one in default striping.
3725  * The default striping is from parent or fs.
3726  *
3727  * \param[in] lo                new object
3728  * \param[in] lds               default striping
3729  * \param[in] mode              new object's mode
3730  */
3731 static void lod_striping_from_default(struct lod_object *lo,
3732                                       const struct lod_default_striping *lds,
3733                                       umode_t mode)
3734 {
3735         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3736         struct lov_desc *desc = &d->lod_desc;
3737         int i, rc;
3738
3739         if (lds->lds_def_striping_set && S_ISREG(mode)) {
3740                 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3741                 if (rc != 0)
3742                         return;
3743
3744                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3745
3746                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3747                         struct lod_layout_component *obj_comp =
3748                                                 &lo->ldo_comp_entries[i];
3749                         struct lod_layout_component *def_comp =
3750                                                 &lds->lds_def_comp_entries[i];
3751
3752                         CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3753                                "nr:%u offset:%u %s\n",
3754                                def_comp->llc_stripe_size,
3755                                def_comp->llc_stripenr,
3756                                def_comp->llc_stripe_offset,
3757                                def_comp->llc_pool ?: "");
3758
3759                         *obj_comp = *def_comp;
3760                         if (def_comp->llc_pool != NULL) {
3761                                 /* pointer was copied from def_comp */
3762                                 obj_comp->llc_pool = NULL;
3763                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3764                         }
3765
3766                         /*
3767                          * Don't initialize these fields for plain layout
3768                          * (v1/v3) here, they are inherited in the order of
3769                          * 'parent' -> 'fs default (root)' -> 'global default
3770                          * values for stripe_count & stripe_size'.
3771                          *
3772                          * see lod_ah_init().
3773                          */
3774                         if (!lo->ldo_is_composite)
3775                                 continue;
3776
3777                         if (obj_comp->llc_stripenr <= 0)
3778                                 obj_comp->llc_stripenr =
3779                                         desc->ld_default_stripe_count;
3780                         if (obj_comp->llc_stripe_size <= 0)
3781                                 obj_comp->llc_stripe_size =
3782                                         desc->ld_default_stripe_size;
3783                 }
3784         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3785                 if (lo->ldo_dir_stripenr == 0)
3786                         lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3787                 if (lo->ldo_dir_stripe_offset == -1)
3788                         lo->ldo_dir_stripe_offset =
3789                                 lds->lds_dir_def_stripe_offset;
3790                 if (lo->ldo_dir_hash_type == 0)
3791                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3792
3793                 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3794                        "offset:%u, hash_type:%u\n",
3795                        lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3796                        lo->ldo_dir_hash_type);
3797         }
3798 }
3799
3800 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3801 {
3802         struct lod_layout_component *lod_comp;
3803
3804         if (lo->ldo_comp_cnt == 0)
3805                 return true;
3806
3807         if (lo->ldo_is_composite)
3808                 return false;
3809
3810         lod_comp = &lo->ldo_comp_entries[0];
3811
3812         if (lod_comp->llc_stripenr <= 0 ||
3813             lod_comp->llc_stripe_size <= 0)
3814                 return true;
3815
3816         if (from_root && (lod_comp->llc_pool == NULL ||
3817                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3818                 return true;
3819
3820         return false;
3821 }
3822
3823 /**
3824  * Implementation of dt_object_operations::do_ah_init.
3825  *
3826  * This method is used to make a decision on the striping configuration for the
3827  * object being created. It can be taken from the \a parent object if it exists,
3828  * or filesystem's default. The resulting configuration (number of stripes,
3829  * stripe size/offset, pool name, etc) is stored in the object itself and will
3830  * be used by the methods like ->doo_declare_create().
3831  *
3832  * \see dt_object_operations::do_ah_init() in the API description for details.
3833  */
3834 static void lod_ah_init(const struct lu_env *env,
3835                         struct dt_allocation_hint *ah,
3836                         struct dt_object *parent,
3837                         struct dt_object *child,
3838                         umode_t child_mode)
3839 {
3840         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3841         struct lod_thread_info *info = lod_env_info(env);
3842         struct lod_default_striping *lds = &info->lti_def_striping;
3843         struct dt_object *nextp = NULL;
3844         struct dt_object *nextc;
3845         struct lod_object *lp = NULL;
3846         struct lod_object *lc;
3847         struct lov_desc *desc;
3848         struct lod_layout_component *lod_comp;
3849         int rc;
3850         ENTRY;
3851
3852         LASSERT(child);
3853
3854         if (likely(parent)) {
3855                 nextp = dt_object_child(parent);
3856                 lp = lod_dt_obj(parent);
3857         }
3858
3859         nextc = dt_object_child(child);
3860         lc = lod_dt_obj(child);
3861
3862         LASSERT(!lod_obj_is_striped(child));
3863         /* default layout template may have been set on the regular file
3864          * when this is called from mdd_create_data() */
3865         if (S_ISREG(child_mode))
3866                 lod_free_comp_entries(lc);
3867
3868         if (!dt_object_exists(nextc))
3869                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3870
3871         if (S_ISDIR(child_mode)) {
3872                 /* other default values are 0 */
3873                 lc->ldo_dir_stripe_offset = -1;
3874
3875                 /* get default striping from parent object */
3876                 if (likely(lp != NULL))
3877                         lod_get_default_striping(env, lp, lds);
3878
3879                 /* set child default striping info, default value is NULL */
3880                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3881                         lc->ldo_def_striping = lds;
3882
3883                 /* It should always honour the specified stripes */
3884                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3885                     lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3886                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3887
3888                         lc->ldo_dir_stripenr =
3889                                 le32_to_cpu(lum1->lum_stripe_count);
3890                         lc->ldo_dir_stripe_offset =
3891                                 le32_to_cpu(lum1->lum_stripe_offset);
3892                         lc->ldo_dir_hash_type =
3893                                 le32_to_cpu(lum1->lum_hash_type);
3894                         CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3895                                 "hash_type %u\n",
3896                                 lc->ldo_dir_stripenr,
3897                                 (int)lc->ldo_dir_stripe_offset,
3898                                 lc->ldo_dir_hash_type);
3899                 } else {
3900                         /* transfer defaults LMV to new directory */
3901                         lod_striping_from_default(lc, lds, child_mode);
3902                 }
3903
3904                 /* shrink the stripe_count to the avaible MDT count */
3905                 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3906                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3907                         lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3908
3909                 /* Directory will be striped only if stripe_count > 1, if
3910                  * stripe_count == 1, let's reset stripenr = 0 to avoid
3911                  * create single master stripe and also help to unify the
3912                  * stripe handling of directories and files */
3913                 if (lc->ldo_dir_stripenr == 1)
3914                         lc->ldo_dir_stripenr = 0;
3915
3916                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3917                        lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3918                        lc->ldo_dir_hash_type);
3919
3920                 RETURN_EXIT;
3921         }
3922
3923         /* child object regular file*/
3924
3925         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3926                                         lu_object_fid(&child->do_lu)))
3927                 RETURN_EXIT;
3928
3929         /* If object is going to be striped over OSTs, transfer default
3930          * striping information to the child, so that we can use it
3931          * during declaration and creation.
3932          *
3933          * Try from the parent first.
3934          */
3935         if (likely(lp != NULL)) {
3936                 rc = lod_get_default_lov_striping(env, lp, lds);
3937                 if (rc == 0)
3938                         lod_striping_from_default(lc, lds, child_mode);
3939         }
3940
3941         /* Initialize lod_device::lod_md_root object reference */
3942         if (d->lod_md_root == NULL) {
3943                 struct dt_object *root;
3944                 struct lod_object *lroot;
3945
3946                 lu_root_fid(&info->lti_fid);
3947                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3948                 if (!IS_ERR(root)) {
3949                         lroot = lod_dt_obj(root);
3950
3951                         spin_lock(&d->lod_lock);
3952                         if (d->lod_md_root != NULL)
3953                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
3954                         d->lod_md_root = lroot;
3955                         spin_unlock(&d->lod_lock);
3956                 }
3957         }
3958
3959         /* try inherit layout from the root object (fs default) when:
3960          *  - parent does not have default layout; or
3961          *  - parent has plain(v1/v3) default layout, and some attributes
3962          *    are not specified in the default layout;
3963          */
3964         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3965                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3966                 if (rc)
3967                         goto out;
3968                 if (lc->ldo_comp_cnt == 0) {
3969                         lod_striping_from_default(lc, lds, child_mode);
3970                 } else if (!lds->lds_def_striping_is_composite) {
3971                         struct lod_layout_component *def_comp;
3972
3973                         LASSERT(!lc->ldo_is_composite);
3974                         lod_comp = &lc->ldo_comp_entries[0];
3975                         def_comp = &lds->lds_def_comp_entries[0];
3976
3977                         if (lod_comp->llc_stripenr <= 0)
3978                                 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3979                         if (lod_comp->llc_stripe_size <= 0)
3980                                 lod_comp->llc_stripe_size =
3981                                         def_comp->llc_stripe_size;
3982                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3983                                 lod_comp->llc_stripe_offset =
3984                                         def_comp->llc_stripe_offset;
3985                         if (lod_comp->llc_pool == NULL)
3986                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3987                 }
3988         }
3989 out:
3990         /*
3991          * fs default striping may not be explicitly set, or historically set
3992          * in config log, use them.
3993          */
3994         if (lod_need_inherit_more(lc, false)) {
3995
3996                 if (lc->ldo_comp_cnt == 0) {
3997                         rc = lod_alloc_comp_entries(lc, 1);
3998                         if (rc)
3999                                 /* fail to allocate memory, will create a
4000                                  * non-striped file. */
4001                                 RETURN_EXIT;
4002                         lc->ldo_is_composite = 0;
4003                         lod_comp = &lc->ldo_comp_entries[0];
4004                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4005                 }
4006                 LASSERT(!lc->ldo_is_composite);
4007                 lod_comp = &lc->ldo_comp_entries[0];
4008                 desc = &d->lod_desc;
4009                 if (lod_comp->llc_stripenr <= 0)
4010                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
4011                 if (lod_comp->llc_stripe_size <= 0)
4012                         lod_comp->llc_stripe_size =
4013                                 desc->ld_default_stripe_size;
4014         }
4015
4016         EXIT;
4017 }
4018
4019 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
4020 /**
4021  * Size initialization on late striping.
4022  *
4023  * Propagate the size of a truncated object to a deferred striping.
4024  * This function handles a special case when truncate was done on a
4025  * non-striped object and now while the striping is being created
4026  * we can't lose that size, so we have to propagate it to the stripes
4027  * being created.
4028  *
4029  * \param[in] env       execution environment
4030  * \param[in] dt        object
4031  * \param[in] th        transaction handle
4032  *
4033  * \retval              0 on success
4034  * \retval              negative if failed
4035  */
4036 static int lod_declare_init_size(const struct lu_env *env,
4037                                  struct dt_object *dt, struct thandle *th)
4038 {
4039         struct dt_object        *next = dt_object_child(dt);
4040         struct lod_object       *lo = lod_dt_obj(dt);
4041         struct dt_object        **objects = NULL;
4042         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
4043         uint64_t        size, offs;
4044         int     i, rc, stripe, stripenr = 0, stripe_size = 0;
4045         ENTRY;
4046
4047         if (!lod_obj_is_striped(dt))
4048                 RETURN(0);
4049
4050         rc = dt_attr_get(env, next, attr);
4051         LASSERT(attr->la_valid & LA_SIZE);
4052         if (rc)
4053                 RETURN(rc);
4054
4055         size = attr->la_size;
4056         if (size == 0)
4057                 RETURN(0);
4058
4059         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4060                 struct lod_layout_component *lod_comp;
4061                 struct lu_extent *extent;
4062
4063                 lod_comp = &lo->ldo_comp_entries[i];
4064
4065                 if (lod_comp->llc_stripe == NULL)
4066                         continue;
4067
4068                 extent = &lod_comp->llc_extent;
4069                 if (!lo->ldo_is_composite ||
4070                     (size >= extent->e_start && size < extent->e_end)) {
4071                         objects = lod_comp->llc_stripe;
4072                         stripenr = lod_comp->llc_stripenr;
4073                         stripe_size = lod_comp->llc_stripe_size;
4074                         break;
4075                 }
4076         }
4077
4078         if (stripenr == 0)
4079                 RETURN(0);
4080
4081         LASSERT(objects != NULL && stripe_size != 0);
4082
4083         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4084         ll_do_div64(size, (__u64)stripe_size);
4085         stripe = ll_do_div64(size, (__u64)stripenr);
4086         LASSERT(objects[stripe] != NULL);
4087
4088         size = size * stripe_size;
4089         offs = attr->la_size;
4090         size += ll_do_div64(offs, stripe_size);
4091
4092         attr->la_valid = LA_SIZE;
4093         attr->la_size = size;
4094
4095         rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4096
4097         RETURN(rc);
4098 }
4099
4100 /**
4101  * Declare creation of striped object.
4102  *
4103  * The function declares creation stripes for a regular object. The function
4104  * also declares whether the stripes will be created with non-zero size if
4105  * previously size was set non-zero on the master object. If object \a dt is
4106  * not local, then only fully defined striping can be applied in \a lovea.
4107  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4108  * for the details.
4109  *
4110  * \param[in] env       execution environment
4111  * \param[in] dt        object
4112  * \param[in] attr      attributes the stripes will be created with
4113  * \param[in] lovea     a buffer containing striping description
4114  * \param[in] th        transaction handle
4115  *
4116  * \retval              0 on success
4117  * \retval              negative if failed
4118  */
4119 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4120                                struct lu_attr *attr,
4121                                const struct lu_buf *lovea, struct thandle *th)
4122 {
4123         struct lod_thread_info  *info = lod_env_info(env);
4124         struct dt_object        *next = dt_object_child(dt);
4125         struct lod_object       *lo = lod_dt_obj(dt);
4126         int                      rc;
4127         ENTRY;
4128
4129         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4130                 GOTO(out, rc = -ENOMEM);
4131
4132         if (!dt_object_remote(next)) {
4133                 /* choose OST and generate appropriate objects */
4134                 rc = lod_prepare_create(env, lo, attr, lovea, th);
4135                 if (rc)
4136                         GOTO(out, rc);
4137
4138                 /*
4139                  * declare storage for striping data
4140                  */
4141                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4142         } else {
4143                 /* LOD can not choose OST objects for remote objects, i.e.
4144                  * stripes must be ready before that. Right now, it can only
4145                  * happen during migrate, i.e. migrate process needs to create
4146                  * remote regular file (mdd_migrate_create), then the migrate
4147                  * process will provide stripeEA. */
4148                 LASSERT(lovea != NULL);
4149                 info->lti_buf = *lovea;
4150         }
4151
4152         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4153                                               XATTR_NAME_LOV, 0, th);
4154         if (rc)
4155                 GOTO(out, rc);
4156
4157         /*
4158          * if striping is created with local object's size > 0,
4159          * we have to propagate this size to specific object
4160          * the case is possible only when local object was created previously
4161          */
4162         if (dt_object_exists(next))
4163                 rc = lod_declare_init_size(env, dt, th);
4164
4165 out:
4166         /* failed to create striping or to set initial size, let's reset
4167          * config so that others don't get confused */
4168         if (rc)
4169                 lod_object_free_striping(env, lo);
4170
4171         RETURN(rc);
4172 }
4173
4174 /**
4175  * Implementation of dt_object_operations::do_declare_create.
4176  *
4177  * The method declares creation of a new object. If the object will be striped,
4178  * then helper functions are called to find FIDs for the stripes, declare
4179  * creation of the stripes and declare initialization of the striping
4180  * information to be stored in the master object.
4181  *
4182  * \see dt_object_operations::do_declare_create() in the API description
4183  * for details.
4184  */
4185 static int lod_declare_object_create(const struct lu_env *env,
4186                                      struct dt_object *dt,
4187                                      struct lu_attr *attr,
4188                                      struct dt_allocation_hint *hint,
4189                                      struct dt_object_format *dof,
4190                                      struct thandle *th)
4191 {
4192         struct dt_object   *next = dt_object_child(dt);
4193         struct lod_object  *lo = lod_dt_obj(dt);
4194         int                 rc;
4195         ENTRY;
4196
4197         LASSERT(dof);
4198         LASSERT(attr);
4199         LASSERT(th);
4200
4201         /*
4202          * first of all, we declare creation of local object
4203          */
4204         rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4205         if (rc != 0)
4206                 GOTO(out, rc);
4207
4208         if (dof->dof_type == DFT_SYM)
4209                 dt->do_body_ops = &lod_body_lnk_ops;
4210         else if (dof->dof_type == DFT_REGULAR)
4211                 dt->do_body_ops = &lod_body_ops;
4212
4213         /*
4214          * it's lod_ah_init() that has decided the object will be striped
4215          */
4216         if (dof->dof_type == DFT_REGULAR) {
4217                 /* callers don't want stripes */
4218                 /* XXX: all tricky interactions with ->ah_make_hint() decided
4219                  * to use striping, then ->declare_create() behaving differently
4220                  * should be cleaned */
4221                 if (dof->u.dof_reg.striped != 0)
4222                         rc = lod_declare_striped_object(env, dt, attr,
4223                                                         NULL, th);
4224         } else if (dof->dof_type == DFT_DIR) {
4225                 struct seq_server_site *ss;
4226
4227                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4228
4229                 /* If the parent has default stripeEA, and client
4230                  * did not find it before sending create request,
4231                  * then MDT will return -EREMOTE, and client will
4232                  * retrieve the default stripeEA and re-create the
4233                  * sub directory.
4234                  *
4235                  * Note: if dah_eadata != NULL, it means creating the
4236                  * striped directory with specified stripeEA, then it
4237                  * should ignore the default stripeEA */
4238                 if (hint != NULL && hint->dah_eadata == NULL) {
4239                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4240                                 GOTO(out, rc = -EREMOTE);
4241
4242                         if (lo->ldo_dir_stripe_offset == -1) {
4243                                 /* child and parent should be in the same MDT */
4244                                 if (hint->dah_parent != NULL &&
4245                                     dt_object_remote(hint->dah_parent))
4246                                         GOTO(out, rc = -EREMOTE);
4247                         } else if (lo->ldo_dir_stripe_offset !=
4248                                    ss->ss_node_id) {
4249                                 struct lod_device *lod;
4250                                 struct lod_tgt_descs *ltd;
4251                                 struct lod_tgt_desc *tgt = NULL;
4252                                 bool found_mdt = false;
4253                                 int i;
4254
4255                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4256                                 ltd = &lod->lod_mdt_descs;
4257                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4258                                         tgt = LTD_TGT(ltd, i);
4259                                         if (tgt->ltd_index ==
4260                                                 lo->ldo_dir_stripe_offset) {
4261                                                 found_mdt = true;
4262                                                 break;
4263                                         }
4264                                 }
4265
4266                                 /* If the MDT indicated by stripe_offset can be
4267                                  * found, then tell client to resend the create
4268                                  * request to the correct MDT, otherwise return
4269                                  * error to client */
4270                                 if (found_mdt)
4271                                         GOTO(out, rc = -EREMOTE);
4272                                 else
4273                                         GOTO(out, rc = -EINVAL);
4274                         }
4275                 }
4276
4277                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4278         }
4279 out:
4280         /* failed to create striping or to set initial size, let's reset
4281          * config so that others don't get confused */
4282         if (rc)
4283                 lod_object_free_striping(env, lo);
4284         RETURN(rc);
4285 }
4286
4287 /**
4288  * Creation of a striped regular object.
4289  *
4290  * The function is called to create the stripe objects for a regular
4291  * striped file. This can happen at the initial object creation or
4292  * when the caller asks LOD to do so using ->do_xattr_set() method
4293  * (so called late striping). Notice all the information are already
4294  * prepared in the form of the list of objects (ldo_stripe field).
4295  * This is done during declare phase.
4296  *
4297  * \param[in] env       execution environment
4298  * \param[in] dt        object
4299  * \param[in] attr      attributes the stripes will be created with
4300  * \param[in] dof       format of stripes (see OSD API description)
4301  * \param[in] th        transaction handle
4302  *
4303  * \retval              0 on success
4304  * \retval              negative if failed
4305  */
4306 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4307                         struct lu_attr *attr, struct dt_object_format *dof,
4308                         struct thandle *th)
4309 {
4310         struct lod_layout_component     *lod_comp;
4311         struct lod_object       *lo = lod_dt_obj(dt);
4312         int     rc = 0, i, j;
4313         ENTRY;
4314
4315         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4316
4317         /* create all underlying objects */
4318         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4319                 lod_comp = &lo->ldo_comp_entries[i];
4320
4321                 if (lod_comp_inited(lod_comp))
4322                         continue;
4323
4324                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4325                         lod_comp_set_init(lod_comp);
4326
4327                 if (lod_comp->llc_stripe == NULL)
4328                         continue;
4329
4330                 LASSERT(lod_comp->llc_stripenr);
4331                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4332                         struct dt_object *object = lod_comp->llc_stripe[j];
4333                         LASSERT(object != NULL);
4334                         rc = lod_sub_object_create(env, object, attr, NULL,
4335                                                    dof, th);
4336                         if (rc)
4337                                 break;
4338                 }
4339                 lod_comp_set_init(lod_comp);
4340         }
4341
4342         if (rc == 0)
4343                 rc = lod_generate_and_set_lovea(env, lo, th);
4344
4345         if (rc == 0)
4346                 lo->ldo_comp_cached = 1;
4347         else
4348                 lod_object_free_striping(env, lo);
4349
4350         RETURN(rc);
4351 }
4352
4353 /**
4354  * Implementation of dt_object_operations::do_create.
4355  *
4356  * If any of preceeding methods (like ->do_declare_create(),
4357  * ->do_ah_init(), etc) chose to create a striped object,
4358  * then this method will create the master and the stripes.
4359  *
4360  * \see dt_object_operations::do_create() in the API description for details.
4361  */
4362 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4363                              struct lu_attr *attr,
4364                              struct dt_allocation_hint *hint,
4365                              struct dt_object_format *dof, struct thandle *th)
4366 {
4367         int                 rc;
4368         ENTRY;
4369
4370         /* create local object */
4371         rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4372                                    th);
4373         if (rc != 0)
4374                 RETURN(rc);
4375
4376         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4377             lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4378                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4379                 rc = lod_striping_create(env, dt, attr, dof, th);
4380         }
4381
4382         RETURN(rc);
4383 }
4384
4385 static inline int
4386 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4387                           struct dt_object *dt, struct thandle *th,
4388                           int stripe_idx, struct lod_obj_stripe_cb_data *data)
4389 {
4390         if (data->locd_declare)
4391                 return lod_sub_object_declare_destroy(env, dt, th);
4392         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4393                  stripe_idx == cfs_fail_val)
4394                 return lod_sub_object_destroy(env, dt, th);
4395         else
4396                 return 0;
4397 }
4398
4399 /**
4400  * Implementation of dt_object_operations::do_declare_destroy.
4401  *
4402  * If the object is a striped directory, then the function declares reference
4403  * removal from the master object (this is an index) to the stripes and declares
4404  * destroy of all the stripes. In all the cases, it declares an intention to
4405  * destroy the object itself.
4406  *
4407  * \see dt_object_operations::do_declare_destroy() in the API description
4408  * for details.
4409  */
4410 static int lod_declare_object_destroy(const struct lu_env *env,
4411                                       struct dt_object *dt,
4412                                       struct thandle *th)
4413 {
4414         struct dt_object   *next = dt_object_child(dt);
4415         struct lod_object  *lo = lod_dt_obj(dt);
4416         struct lod_thread_info *info = lod_env_info(env);
4417         char               *stripe_name = info->lti_key;
4418         int                 rc, i;
4419         ENTRY;
4420
4421         /*
4422          * load striping information, notice we don't do this when object
4423          * is being initialized as we don't need this information till
4424          * few specific cases like destroy, chown
4425          */
4426         rc = lod_load_striping(env, lo);
4427         if (rc)
4428                 RETURN(rc);
4429
4430         /* declare destroy for all underlying objects */
4431         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4432                 rc = next->do_ops->do_index_try(env, next,
4433                                                 &dt_directory_features);
4434                 if (rc != 0)
4435                         RETURN(rc);
4436
4437                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4438                         rc = lod_sub_object_declare_ref_del(env, next, th);
4439                         if (rc != 0)
4440                                 RETURN(rc);
4441
4442                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4443                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4444                                 i);
4445                         rc = lod_sub_object_declare_delete(env, next,
4446                                         (const struct dt_key *)stripe_name, th);
4447                         if (rc != 0)
4448                                 RETURN(rc);
4449                 }
4450         }
4451
4452         /*
4453          * we declare destroy for the local object
4454          */
4455         rc = lod_sub_object_declare_destroy(env, next, th);
4456         if (rc)
4457                 RETURN(rc);
4458
4459         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4460             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4461                 RETURN(0);
4462
4463         if (!lod_obj_is_striped(dt))
4464                 RETURN(0);
4465
4466         /* declare destroy all striped objects */
4467         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4468                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4469                         if (lo->ldo_stripe[i] == NULL)
4470                                 continue;
4471
4472                         rc = lod_sub_object_declare_ref_del(env,
4473                                         lo->ldo_stripe[i], th);
4474
4475                         rc = lod_sub_object_declare_destroy(env,
4476                                         lo->ldo_stripe[i], th);
4477                         if (rc != 0)
4478                                 break;
4479                 }
4480         } else {
4481                 struct lod_obj_stripe_cb_data data;
4482
4483                 data.locd_declare = true;
4484                 rc = lod_obj_for_each_stripe(env, lo, th,
4485                                 lod_obj_stripe_destroy_cb, &data);
4486         }
4487
4488         RETURN(rc);
4489 }
4490
4491 /**
4492  * Implementation of dt_object_operations::do_destroy.
4493  *
4494  * If the object is a striped directory, then the function removes references
4495  * from the master object (this is an index) to the stripes and destroys all
4496  * the stripes. In all the cases, the function destroys the object itself.
4497  *
4498  * \see dt_object_operations::do_destroy() in the API description for details.
4499  */
4500 static int lod_object_destroy(const struct lu_env *env,
4501                 struct dt_object *dt, struct thandle *th)
4502 {
4503         struct dt_object  *next = dt_object_child(dt);
4504         struct lod_object *lo = lod_dt_obj(dt);
4505         struct lod_thread_info *info = lod_env_info(env);
4506         char               *stripe_name = info->lti_key;
4507         unsigned int       i;
4508         int                rc;
4509         ENTRY;
4510
4511         /* destroy sub-stripe of master object */
4512         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4513                 rc = next->do_ops->do_index_try(env, next,
4514                                                 &dt_directory_features);
4515                 if (rc != 0)
4516                         RETURN(rc);
4517
4518                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4519                         rc = lod_sub_object_ref_del(env, next, th);
4520                         if (rc != 0)
4521                                 RETURN(rc);
4522
4523                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4524                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4525                                 i);
4526
4527                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4528                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4529                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4530
4531                         rc = lod_sub_object_delete(env, next,
4532                                        (const struct dt_key *)stripe_name, th);
4533                         if (rc != 0)
4534                                 RETURN(rc);
4535                 }
4536         }
4537
4538         rc = lod_sub_object_destroy(env, next, th);
4539         if (rc != 0)
4540                 RETURN(rc);
4541
4542         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4543             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4544                 RETURN(0);
4545
4546         if (!lod_obj_is_striped(dt))
4547                 RETURN(0);
4548
4549         /* destroy all striped objects */
4550         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4551                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4552                         if (lo->ldo_stripe[i] == NULL)
4553                                 continue;
4554                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4555                             i == cfs_fail_val) {
4556                                 dt_write_lock(env, lo->ldo_stripe[i],
4557                                               MOR_TGT_CHILD);
4558                                 rc = lod_sub_object_ref_del(env,
4559                                                 lo->ldo_stripe[i], th);
4560                                 dt_write_unlock(env, lo->ldo_stripe[i]);
4561                                 if (rc != 0)
4562                                         break;
4563
4564                                 rc = lod_sub_object_destroy(env,
4565                                                 lo->ldo_stripe[i], th);
4566                                 if (rc != 0)
4567                                         break;
4568                         }
4569                 }
4570         } else {
4571                 struct lod_obj_stripe_cb_data data;
4572
4573                 data.locd_declare = false;
4574                 rc = lod_obj_for_each_stripe(env, lo, th,
4575                                 lod_obj_stripe_destroy_cb, &data);
4576         }
4577
4578         RETURN(rc);
4579 }
4580
4581 /**
4582  * Implementation of dt_object_operations::do_declare_ref_add.
4583  *
4584  * \see dt_object_operations::do_declare_ref_add() in the API description
4585  * for details.
4586  */
4587 static int lod_declare_ref_add(const struct lu_env *env,
4588                                struct dt_object *dt, struct thandle *th)
4589 {
4590         return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4591 }
4592
4593 /**
4594  * Implementation of dt_object_operations::do_ref_add.
4595  *
4596  * \see dt_object_operations::do_ref_add() in the API description for details.
4597  */
4598 static int lod_ref_add(const struct lu_env *env,
4599                        struct dt_object *dt, struct thandle *th)
4600 {
4601         return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4602 }
4603
4604 /**
4605  * Implementation of dt_object_operations::do_declare_ref_del.
4606  *
4607  * \see dt_object_operations::do_declare_ref_del() in the API description
4608  * for details.
4609  */
4610 static int lod_declare_ref_del(const struct lu_env *env,
4611                                struct dt_object *dt, struct thandle *th)
4612 {
4613         return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4614 }
4615
4616 /**
4617  * Implementation of dt_object_operations::do_ref_del
4618  *
4619  * \see dt_object_operations::do_ref_del() in the API description for details.
4620  */
4621 static int lod_ref_del(const struct lu_env *env,
4622                        struct dt_object *dt, struct thandle *th)
4623 {
4624         return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4625 }
4626
4627 /**
4628  * Implementation of dt_object_operations::do_object_sync.
4629  *
4630  * \see dt_object_operations::do_object_sync() in the API description
4631  * for details.
4632  */
4633 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4634                            __u64 start, __u64 end)
4635 {
4636         return dt_object_sync(env, dt_object_child(dt), start, end);
4637 }
4638
4639 /**
4640  * Release LDLM locks on the stripes of a striped directory.
4641  *
4642  * Iterates over all the locks taken on the stripe objects and
4643  * cancel them.
4644  *
4645  * \param[in] env       execution environment
4646  * \param[in] dt        striped object
4647  * \param[in] einfo     lock description
4648  * \param[in] policy    data describing requested lock
4649  *
4650  * \retval              0 on success
4651  * \retval              negative if failed
4652  */
4653 static int lod_object_unlock_internal(const struct lu_env *env,
4654                                       struct dt_object *dt,
4655                                       struct ldlm_enqueue_info *einfo,
4656                                       union ldlm_policy_data *policy)
4657 {
4658         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4659         int                     rc = 0;
4660         int                     i;
4661         ENTRY;
4662
4663         if (slave_locks == NULL)
4664                 RETURN(0);
4665
4666         for (i = 1; i < slave_locks->count; i++) {
4667                 if (lustre_handle_is_used(&slave_locks->handles[i]))
4668                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4669                                                     einfo->ei_mode);
4670         }
4671
4672         RETURN(rc);
4673 }
4674
4675 /**
4676  * Implementation of dt_object_operations::do_object_unlock.
4677  *
4678  * Used to release LDLM lock(s).
4679  *
4680  * \see dt_object_operations::do_object_unlock() in the API description
4681  * for details.
4682  */
4683 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4684                              struct ldlm_enqueue_info *einfo,
4685                              union ldlm_policy_data *policy)
4686 {
4687         struct lod_object *lo = lod_dt_obj(dt);
4688         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4689         int slave_locks_size;
4690         int i;
4691         ENTRY;
4692
4693         if (slave_locks == NULL)
4694                 RETURN(0);
4695
4696         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4697         LASSERT(lo->ldo_dir_stripenr > 1);
4698         /* Note: for remote lock for single stripe dir, MDT will cancel
4699          * the lock by lockh directly */
4700         LASSERT(!dt_object_remote(dt_object_child(dt)));
4701
4702         /* locks were unlocked in MDT layer */
4703         for (i = 1; i < slave_locks->count; i++) {
4704                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4705                 dt_invalidate(env, lo->ldo_stripe[i]);
4706         }
4707
4708         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4709                            sizeof(slave_locks->handles[0]);
4710         OBD_FREE(slave_locks, slave_locks_size);
4711         einfo->ei_cbdata = NULL;
4712
4713         RETURN(0);
4714 }
4715
4716 /**
4717  * Implementation of dt_object_operations::do_object_lock.
4718  *
4719  * Used to get LDLM lock on the non-striped and striped objects.
4720  *
4721  * \see dt_object_operations::do_object_lock() in the API description
4722  * for details.
4723  */
4724 static int lod_object_lock(const struct lu_env *env,
4725                            struct dt_object *dt,
4726                            struct lustre_handle *lh,
4727                            struct ldlm_enqueue_info *einfo,
4728                            union ldlm_policy_data *policy)
4729 {
4730         struct lod_object       *lo = lod_dt_obj(dt);
4731         int                     rc = 0;
4732         int                     i;
4733         int                     slave_locks_size;
4734         struct lustre_handle_array *slave_locks = NULL;
4735         ENTRY;
4736
4737         /* remote object lock */
4738         if (!einfo->ei_enq_slave) {
4739                 LASSERT(dt_object_remote(dt));
4740                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4741                                       policy);
4742         }
4743
4744         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4745                 GOTO(out, rc = -ENOTDIR);
4746
4747         rc = lod_load_striping(env, lo);
4748         if (rc != 0)
4749                 GOTO(out, rc);
4750
4751         /* No stripes */
4752         if (lo->ldo_dir_stripenr <= 1) {
4753                 /*
4754                  * NB, ei_cbdata stores pointer to slave locks, if no locks
4755                  * taken, make sure it's set to NULL, otherwise MDT will try to
4756                  * unlock them.
4757                  */
4758                 einfo->ei_cbdata = NULL;
4759                 GOTO(out, rc = 0);
4760         }
4761
4762         slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4763                            sizeof(slave_locks->handles[0]);
4764         /* Freed in lod_object_unlock */
4765         OBD_ALLOC(slave_locks, slave_locks_size);
4766         if (slave_locks == NULL)
4767                 GOTO(out, rc = -ENOMEM);
4768         slave_locks->count = lo->ldo_dir_stripenr;
4769
4770         /* striped directory lock */
4771         for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4772                 struct lustre_handle    lockh;
4773                 struct ldlm_res_id      *res_id;
4774
4775                 res_id = &lod_env_info(env)->lti_res_id;
4776                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4777                                        res_id);
4778                 einfo->ei_res_id = res_id;
4779
4780                 LASSERT(lo->ldo_stripe[i] != NULL);
4781                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4782                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4783                                             einfo, policy);
4784                 } else {
4785                         struct ldlm_namespace *ns = einfo->ei_namespace;
4786                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4787                         ldlm_completion_callback completion = einfo->ei_cb_cp;
4788                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
4789
4790                         if (einfo->ei_mode == LCK_PW ||
4791                             einfo->ei_mode == LCK_EX)
4792                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
4793
4794                         /* This only happens if there are mulitple stripes
4795                          * on the master MDT, i.e. except stripe0, there are
4796                          * other stripes on the Master MDT as well, Only
4797                          * happens in the test case right now. */
4798                         LASSERT(ns != NULL);
4799                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4800                                                     policy, einfo->ei_mode,
4801                                                     &dlmflags, blocking,
4802                                                     completion, NULL,
4803                                                     NULL, 0, LVB_T_NONE,
4804                                                     NULL, &lockh);
4805                 }
4806                 if (rc != 0)
4807                         break;
4808                 slave_locks->handles[i] = lockh;
4809         }
4810         einfo->ei_cbdata = slave_locks;
4811
4812         if (rc != 0 && slave_locks != NULL) {
4813                 lod_object_unlock_internal(env, dt, einfo, policy);
4814                 OBD_FREE(slave_locks, slave_locks_size);
4815         }
4816         EXIT;
4817 out:
4818         if (rc != 0)
4819                 einfo->ei_cbdata = NULL;
4820         RETURN(rc);
4821 }
4822
4823 /**
4824  * Implementation of dt_object_operations::do_invalidate.
4825  *
4826  * \see dt_object_operations::do_invalidate() in the API description for details
4827  */
4828 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4829 {
4830         return dt_invalidate(env, dt_object_child(dt));
4831 }
4832
4833 /**
4834  * Resize per-thread ost list to hold OST target index list already used.
4835  *
4836  * \param[in,out] inuse         structure contains ost list array
4837  * \param[in] cnt               total stripe count of all components
4838  * \param[in] max               array's max size if @max > 0
4839  *
4840  * \retval 0            on success
4841  * \retval -ENOMEM      reallocation failed
4842  */
4843 int lod_inuse_resize(struct ost_pool *inuse, __u16 cnt, __u16 max)
4844 {
4845         __u32 *array;
4846         __u32 new = cnt * sizeof(__u32);
4847
4848         inuse->op_count = 0;
4849
4850         if (new <= inuse->op_size)
4851                 return 0;
4852
4853         if (max)
4854                 new = min_t(__u32, new, max);
4855         OBD_ALLOC(array, new);
4856         if (!array)
4857                 return -ENOMEM;
4858
4859         if (inuse->op_array)
4860                 OBD_FREE(inuse->op_array, inuse->op_size);
4861
4862         inuse->op_array = array;
4863         inuse->op_size = new;
4864
4865         return 0;
4866 }
4867
4868 static int lod_declare_layout_change(const struct lu_env *env,
4869                                      struct dt_object *dt,
4870                                      struct layout_intent *layout,
4871                                      const struct lu_buf *buf,
4872                                      struct thandle *th)
4873 {
4874         struct lod_thread_info  *info = lod_env_info(env);
4875         struct lod_object *lo = lod_dt_obj(dt);
4876         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
4877         struct dt_object *next = dt_object_child(dt);
4878         struct lod_obj_stripe_cb_data data;
4879         struct ost_pool *inuse = &info->lti_inuse_osts;
4880         struct lod_layout_component *lod_comp;
4881         struct lov_comp_md_v1 *comp_v1 = NULL;
4882         bool replay = false;
4883         bool need_create = false;
4884         int i, rc;
4885         __u32 stripe_cnt = 0;
4886         ENTRY;
4887
4888         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
4889             dt_object_remote(next))
4890                 RETURN(-EINVAL);
4891
4892         dt_write_lock(env, next, 0);
4893         /*
4894          * In case the client is passing lovea, which only happens during
4895          * the replay of layout intent write RPC for now, we may need to
4896          * parse the lovea and apply new layout configuration.
4897          */
4898         if (buf && buf->lb_len)  {
4899                 struct lov_user_md_v1 *v1 = buf->lb_buf;
4900
4901                 if (v1->lmm_magic != (LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1) &&
4902                     v1->lmm_magic !=
4903                                 __swab32(LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1)) {
4904                         CERROR("%s: the replay buffer of layout extend "
4905                                "(magic %#x) does not contain expected "
4906                                "composite layout.\n",
4907                                lod2obd(d)->obd_name, v1->lmm_magic);
4908                         GOTO(out, rc = -EINVAL);
4909                 }
4910
4911                 lod_object_free_striping(env, lo);
4912                 rc = lod_use_defined_striping(env, lo, buf);
4913                 if (rc)
4914                         GOTO(out, rc);
4915
4916                 rc = lod_get_lov_ea(env, lo);
4917                 if (rc <= 0)
4918                         GOTO(out, rc);
4919                 /* old on-disk EA is stored in info->lti_buf */
4920                 comp_v1 = (struct lov_comp_md_v1 *)&info->lti_buf.lb_buf;
4921                 replay = true;
4922         } else {
4923                 /* non replay path */
4924                 rc = lod_load_striping_locked(env, lo);
4925                 if (rc)
4926                         GOTO(out, rc);
4927
4928                 /* Prepare inuse array for composite file */
4929                 for (i = 0; i < lo->ldo_comp_cnt; i++)
4930                         stripe_cnt += lod_comp_entry_stripecnt(lo,
4931                                                 &lo->ldo_comp_entries[i],
4932                                                 false);
4933                 rc = lod_inuse_resize(inuse, stripe_cnt, d->lod_osd_max_easize);
4934                 if (rc)
4935                         GOTO(out, rc);
4936
4937                 data.locd_inuse = inuse;
4938                 rc = lod_obj_for_each_stripe(env, lo, NULL,
4939                                              lod_obj_stripe_set_inuse_cb,
4940                                              &data);
4941                 if (rc)
4942                         GOTO(out, rc);
4943         }
4944
4945         /* Make sure defined layout covers the requested write range. */
4946         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
4947         if ((lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
4948              lod_comp->llc_extent.e_end < layout->li_end)) {
4949                 CDEBUG(replay ? D_ERROR : D_LAYOUT,
4950                        "%s: the defined layout [0, %#llx) does not covers "
4951                        "the write range [%#llx, %#llx).\n",
4952                        lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
4953                        layout->li_start, layout->li_end);
4954                 GOTO(out, rc = -EINVAL);
4955         }
4956
4957         /*
4958          * Iterate ld->ldo_comp_entries, find the component whose extent under
4959          * the write range and not instantianted.
4960          */
4961         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4962                 lod_comp = &lo->ldo_comp_entries[i];
4963
4964                 if (lod_comp->llc_extent.e_start >= layout->li_end)
4965                         break;
4966
4967                 if (!replay) {
4968                         if (lod_comp_inited(lod_comp))
4969                                 continue;
4970                 } else {
4971                         /**
4972                          * In replay path, lod_comp is the EA passed by
4973                          * client replay buffer,  comp_v1 is the pre-recovery
4974                          * on-disk EA, we'd sift out those components which
4975                          * were init-ed in the on-disk EA.
4976                          */
4977                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
4978                             LCME_FL_INIT)
4979                                 continue;
4980                 }
4981                 /*
4982                  * this component hasn't instantiated in normal path, or during
4983                  * replay it needs replay the instantiation.
4984                  */
4985
4986                 /* A released component is being extended */
4987                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4988                         GOTO(out, rc = -EINVAL);
4989
4990                 need_create = true;
4991                 /*
4992                  * In replay, the component EA is passed by client,
4993                  * Clear LCME_FL_INIT so that lod_striping_create() can create
4994                  * the striping objects.
4995                  */
4996                 if (replay)
4997                         lod_comp_unset_init(lod_comp);
4998
4999                 rc = lod_qos_prep_create(env, lo, NULL, th, i, inuse);
5000                 if (rc)
5001                         break;
5002         }
5003
5004         if (need_create)
5005                 lod_obj_inc_layout_gen(lo);
5006         else
5007                 GOTO(unlock, rc = -EALREADY);
5008
5009         if (!rc) {
5010                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5011                 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
5012                                                       XATTR_NAME_LOV, 0, th);
5013         }
5014 out:
5015         if (rc)
5016                 lod_object_free_striping(env, lo);
5017
5018 unlock:
5019         dt_write_unlock(env, next);
5020
5021         RETURN(rc);
5022 }
5023
5024 /**
5025  * Instantiate layout component objects which covers the intent write offset.
5026  */
5027 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
5028                              struct layout_intent *layout,
5029                              const struct lu_buf *buf, struct thandle *th)
5030 {
5031         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
5032
5033         RETURN(lod_striping_create(env, dt, attr, NULL, th));
5034 }
5035
5036 struct dt_object_operations lod_obj_ops = {
5037         .do_read_lock           = lod_object_read_lock,
5038         .do_write_lock          = lod_object_write_lock,
5039         .do_read_unlock         = lod_object_read_unlock,
5040         .do_write_unlock        = lod_object_write_unlock,
5041         .do_write_locked        = lod_object_write_locked,
5042         .do_attr_get            = lod_attr_get,
5043         .do_declare_attr_set    = lod_declare_attr_set,
5044         .do_attr_set            = lod_attr_set,
5045         .do_xattr_get           = lod_xattr_get,
5046         .do_declare_xattr_set   = lod_declare_xattr_set,
5047         .do_xattr_set           = lod_xattr_set,
5048         .do_declare_xattr_del   = lod_declare_xattr_del,
5049         .do_xattr_del           = lod_xattr_del,
5050         .do_xattr_list          = lod_xattr_list,
5051         .do_ah_init             = lod_ah_init,
5052         .do_declare_create      = lod_declare_object_create,
5053         .do_create              = lod_object_create,
5054         .do_declare_destroy     = lod_declare_object_destroy,
5055         .do_destroy             = lod_object_destroy,
5056         .do_index_try           = lod_index_try,
5057         .do_declare_ref_add     = lod_declare_ref_add,
5058         .do_ref_add             = lod_ref_add,
5059         .do_declare_ref_del     = lod_declare_ref_del,
5060         .do_ref_del             = lod_ref_del,
5061         .do_object_sync         = lod_object_sync,
5062         .do_object_lock         = lod_object_lock,
5063         .do_object_unlock       = lod_object_unlock,
5064         .do_invalidate          = lod_invalidate,
5065         .do_declare_layout_change = lod_declare_layout_change,
5066         .do_layout_change       = lod_layout_change,
5067 };
5068
5069 /**
5070  * Implementation of dt_body_operations::dbo_read.
5071  *
5072  * \see dt_body_operations::dbo_read() in the API description for details.
5073  */
5074 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5075                         struct lu_buf *buf, loff_t *pos)
5076 {
5077         struct dt_object *next = dt_object_child(dt);
5078         return next->do_body_ops->dbo_read(env, next, buf, pos);
5079 }
5080
5081 /**
5082  * Implementation of dt_body_operations::dbo_declare_write.
5083  *
5084  * \see dt_body_operations::dbo_declare_write() in the API description
5085  * for details.
5086  */
5087 static ssize_t lod_declare_write(const struct lu_env *env,
5088                                  struct dt_object *dt,
5089                                  const struct lu_buf *buf, loff_t pos,
5090                                  struct thandle *th)
5091 {
5092         return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
5093                                             th);
5094 }
5095
5096 /**
5097  * Implementation of dt_body_operations::dbo_write.
5098  *
5099  * \see dt_body_operations::dbo_write() in the API description for details.
5100  */
5101 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5102                          const struct lu_buf *buf, loff_t *pos,
5103                          struct thandle *th, int iq)
5104 {
5105         return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
5106 }
5107
5108 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5109                              __u64 start, __u64 end, struct thandle *th)
5110 {
5111         if (dt_object_remote(dt))
5112                 return -ENOTSUPP;
5113
5114         return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
5115                                             end, th);
5116 }
5117
5118 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5119                      __u64 start, __u64 end, struct thandle *th)
5120 {
5121         if (dt_object_remote(dt))
5122                 return -ENOTSUPP;
5123
5124         return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
5125 }
5126
5127 static const struct dt_body_operations lod_body_lnk_ops = {
5128         .dbo_read               = lod_read,
5129         .dbo_declare_write      = lod_declare_write,
5130         .dbo_write              = lod_write
5131 };
5132
5133 static const struct dt_body_operations lod_body_ops = {
5134         .dbo_read               = lod_read,
5135         .dbo_declare_write      = lod_declare_write,
5136         .dbo_write              = lod_write,
5137         .dbo_declare_punch      = lod_declare_punch,
5138         .dbo_punch              = lod_punch,
5139 };
5140
5141 /**
5142  * Implementation of lu_object_operations::loo_object_init.
5143  *
5144  * The function determines the type and the index of the target device using
5145  * sequence of the object's FID. Then passes control down to the
5146  * corresponding device:
5147  *  OSD for the local objects, OSP for remote
5148  *
5149  * \see lu_object_operations::loo_object_init() in the API description
5150  * for details.
5151  */
5152 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
5153                            const struct lu_object_conf *conf)
5154 {
5155         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
5156         struct lu_device        *cdev   = NULL;
5157         struct lu_object        *cobj;
5158         struct lod_tgt_descs    *ltd    = NULL;
5159         struct lod_tgt_desc     *tgt;
5160         u32                      idx    = 0;
5161         int                      type   = LU_SEQ_RANGE_ANY;
5162         int                      rc;
5163         ENTRY;
5164
5165         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
5166         if (rc != 0) {
5167                 /* Note: Sometimes, it will Return EAGAIN here, see
5168                  * ptrlpc_import_delay_req(), which might confuse
5169                  * lu_object_find_at() and make it wait there incorrectly.
5170                  * so we convert it to EIO here.*/
5171                 if (rc == -EAGAIN)
5172                         rc = -EIO;
5173
5174                 RETURN(rc);
5175         }
5176
5177         if (type == LU_SEQ_RANGE_MDT &&
5178             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
5179                 cdev = &lod->lod_child->dd_lu_dev;
5180         } else if (type == LU_SEQ_RANGE_MDT) {
5181                 ltd = &lod->lod_mdt_descs;
5182                 lod_getref(ltd);
5183         } else if (type == LU_SEQ_RANGE_OST) {
5184                 ltd = &lod->lod_ost_descs;
5185                 lod_getref(ltd);
5186         } else {
5187                 LBUG();
5188         }
5189
5190         if (ltd != NULL) {
5191                 if (ltd->ltd_tgts_size > idx &&
5192                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
5193                         tgt = LTD_TGT(ltd, idx);
5194
5195                         LASSERT(tgt != NULL);
5196                         LASSERT(tgt->ltd_tgt != NULL);
5197
5198                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
5199                 }
5200                 lod_putref(lod, ltd);
5201         }
5202
5203         if (unlikely(cdev == NULL))
5204                 RETURN(-ENOENT);
5205
5206         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
5207         if (unlikely(cobj == NULL))
5208                 RETURN(-ENOMEM);
5209
5210         lu_object_add(lo, cobj);
5211
5212         RETURN(0);
5213 }
5214
5215 /**
5216  *
5217  * Release resources associated with striping.
5218  *
5219  * If the object is striped (regular or directory), then release
5220  * the stripe objects references and free the ldo_stripe array.
5221  *
5222  * \param[in] env       execution environment
5223  * \param[in] lo        object
5224  */
5225 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5226 {
5227         struct lod_layout_component *lod_comp;
5228         int i, j;
5229
5230         if (lo->ldo_stripe != NULL) {
5231                 LASSERT(lo->ldo_comp_entries == NULL);
5232                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5233
5234                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
5235                         if (lo->ldo_stripe[i])
5236                                 dt_object_put(env, lo->ldo_stripe[i]);
5237                 }
5238
5239                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5240                 OBD_FREE(lo->ldo_stripe, j);
5241                 lo->ldo_stripe = NULL;
5242                 lo->ldo_dir_stripes_allocated = 0;
5243                 lo->ldo_dir_stripenr = 0;
5244         } else if (lo->ldo_comp_entries != NULL) {
5245                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5246                         /* free lod_layout_component::llc_stripe array */
5247                         lod_comp = &lo->ldo_comp_entries[i];
5248
5249                         if (lod_comp->llc_stripe == NULL)
5250                                 continue;
5251                         LASSERT(lod_comp->llc_stripes_allocated != 0);
5252                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5253                                 if (lod_comp->llc_stripe[j] != NULL)
5254                                         lu_object_put(env,
5255                                                &lod_comp->llc_stripe[j]->do_lu);
5256                         }
5257                         OBD_FREE(lod_comp->llc_stripe,
5258                                  sizeof(struct dt_object *) *
5259                                  lod_comp->llc_stripes_allocated);
5260                         lod_comp->llc_stripe = NULL;
5261                         lod_comp->llc_stripes_allocated = 0;
5262                 }
5263                 lod_free_comp_entries(lo);
5264                 lo->ldo_comp_cached = 0;
5265         }
5266 }
5267
5268 /**
5269  * Implementation of lu_object_operations::loo_object_start.
5270  *
5271  * \see lu_object_operations::loo_object_start() in the API description
5272  * for details.
5273  */
5274 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5275 {
5276         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5277                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5278         } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5279                    fid_is_local_file(lu_object_fid(o))) {
5280                 /* Note: some local file (like last rcvd) is created
5281                  * through bottom layer (OSD), so the object initialization
5282                  * comes to lod, it does not set loh_attr yet, so
5283                  * set do_body_ops for local file anyway */
5284                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5285         }
5286         return 0;
5287 }
5288
5289 /**
5290  * Implementation of lu_object_operations::loo_object_free.
5291  *
5292  * \see lu_object_operations::loo_object_free() in the API description
5293  * for details.
5294  */
5295 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5296 {
5297         struct lod_object *lo = lu2lod_obj(o);
5298
5299         /* release all underlying object pinned */
5300         lod_object_free_striping(env, lo);
5301         lu_object_fini(o);
5302         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5303 }
5304
5305 /**
5306  * Implementation of lu_object_operations::loo_object_release.
5307  *
5308  * \see lu_object_operations::loo_object_release() in the API description
5309  * for details.
5310  */
5311 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5312 {
5313         /* XXX: shouldn't we release everything here in case if object
5314          * creation failed before? */
5315 }
5316
5317 /**
5318  * Implementation of lu_object_operations::loo_object_print.
5319  *
5320  * \see lu_object_operations::loo_object_print() in the API description
5321  * for details.
5322  */
5323 static int lod_object_print(const struct lu_env *env, void *cookie,
5324                             lu_printer_t p, const struct lu_object *l)
5325 {
5326         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5327
5328         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5329 }
5330
5331 struct lu_object_operations lod_lu_obj_ops = {
5332         .loo_object_init        = lod_object_init,
5333         .loo_object_start       = lod_object_start,
5334         .loo_object_free        = lod_object_free,
5335         .loo_object_release     = lod_object_release,
5336         .loo_object_print       = lod_object_print,
5337 };