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