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