Whamcloud - gitweb
9bd60afae95724a394b892b239e6b33a7b22727f
[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)
102 {
103         return lod_sub_insert(env, dt_object_child(dt), rec, key, th);
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 (magic != LMV_MAGIC_V1)
780                 RETURN(0);
781
782         stripes = le32_to_cpu(lmv1->lmv_stripe_count);
783         if (stripes < 1)
784                 RETURN(0);
785
786         rc = lmv_mds_md_size(stripes, magic);
787         if (rc < 0)
788                 RETURN(rc);
789         lmv1_size = rc;
790         if (buf->lb_len < lmv1_size) {
791                 struct lu_buf tbuf;
792
793                 if (!resize)
794                         RETURN(-ERANGE);
795
796                 tbuf = *buf;
797                 buf->lb_buf = NULL;
798                 buf->lb_len = 0;
799                 lu_buf_alloc(buf, lmv1_size);
800                 lmv1 = buf->lb_buf;
801                 if (lmv1 == NULL)
802                         RETURN(-ENOMEM);
803
804                 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
805         }
806
807         if (unlikely(!dt_try_as_dir(env, obj)))
808                 RETURN(-ENOTDIR);
809
810         memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
811         iops = &obj->do_index_ops->dio_it;
812         it = iops->init(env, obj, LUDA_64BITHASH);
813         if (IS_ERR(it))
814                 RETURN(PTR_ERR(it));
815
816         rc = iops->load(env, it, 0);
817         if (rc == 0)
818                 rc = iops->next(env, it);
819         else if (rc > 0)
820                 rc = 0;
821
822         while (rc == 0) {
823                 char             name[FID_LEN + 2] = "";
824                 struct lu_fid    fid;
825                 __u32            index;
826                 int              len;
827
828                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
829                 if (rc != 0)
830                         break;
831
832                 rc = -EIO;
833
834                 fid_le_to_cpu(&fid, &ent->lde_fid);
835                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
836                 if (ent->lde_name[0] == '.') {
837                         if (ent->lde_namelen == 1)
838                                 goto next;
839
840                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
841                                 goto next;
842                 }
843
844                 len = snprintf(name, sizeof(name),
845                                DFID":", PFID(&ent->lde_fid));
846                 /* The ent->lde_name is composed of ${FID}:${index} */
847                 if (ent->lde_namelen < len + 1 ||
848                     memcmp(ent->lde_name, name, len) != 0) {
849                         CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
850                                "%s: invalid shard name %.*s with the FID "DFID
851                                " for the striped directory "DFID", %s\n",
852                                lod2obd(lod)->obd_name, ent->lde_namelen,
853                                ent->lde_name, PFID(&fid),
854                                PFID(lu_object_fid(&obj->do_lu)),
855                                lod->lod_lmv_failout ? "failout" : "skip");
856
857                         if (lod->lod_lmv_failout)
858                                 break;
859
860                         goto next;
861                 }
862
863                 index = 0;
864                 do {
865                         if (ent->lde_name[len] < '0' ||
866                             ent->lde_name[len] > '9') {
867                                 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
868                                        "%s: invalid shard name %.*s with the "
869                                        "FID "DFID" for the striped directory "
870                                        DFID", %s\n",
871                                        lod2obd(lod)->obd_name, ent->lde_namelen,
872                                        ent->lde_name, PFID(&fid),
873                                        PFID(lu_object_fid(&obj->do_lu)),
874                                        lod->lod_lmv_failout ?
875                                        "failout" : "skip");
876
877                                 if (lod->lod_lmv_failout)
878                                         break;
879
880                                 goto next;
881                         }
882
883                         index = index * 10 + ent->lde_name[len++] - '0';
884                 } while (len < ent->lde_namelen);
885
886                 if (len == ent->lde_namelen) {
887                         /* Out of LMV EA range. */
888                         if (index >= stripes) {
889                                 CERROR("%s: the shard %.*s for the striped "
890                                        "directory "DFID" is out of the known "
891                                        "LMV EA range [0 - %u], failout\n",
892                                        lod2obd(lod)->obd_name, ent->lde_namelen,
893                                        ent->lde_name,
894                                        PFID(lu_object_fid(&obj->do_lu)),
895                                        stripes - 1);
896
897                                 break;
898                         }
899
900                         /* The slot has been occupied. */
901                         if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
902                                 struct lu_fid fid0;
903
904                                 fid_le_to_cpu(&fid0,
905                                         &lmv1->lmv_stripe_fids[index]);
906                                 CERROR("%s: both the shard "DFID" and "DFID
907                                        " for the striped directory "DFID
908                                        " claim the same LMV EA slot at the "
909                                        "index %d, failout\n",
910                                        lod2obd(lod)->obd_name,
911                                        PFID(&fid0), PFID(&fid),
912                                        PFID(lu_object_fid(&obj->do_lu)), index);
913
914                                 break;
915                         }
916
917                         /* stored as LE mode */
918                         lmv1->lmv_stripe_fids[index] = ent->lde_fid;
919
920 next:
921                         rc = iops->next(env, it);
922                 }
923         }
924
925         iops->put(env, it);
926         iops->fini(env, it);
927
928         RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
929 }
930
931 /**
932  * Implementation of dt_object_operations::do_index_try.
933  *
934  * \see dt_object_operations::do_index_try() in the API description for details.
935  */
936 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
937                          const struct dt_index_features *feat)
938 {
939         struct lod_object       *lo = lod_dt_obj(dt);
940         struct dt_object        *next = dt_object_child(dt);
941         int                     rc;
942         ENTRY;
943
944         LASSERT(next->do_ops);
945         LASSERT(next->do_ops->do_index_try);
946
947         rc = lod_striping_load(env, lo);
948         if (rc != 0)
949                 RETURN(rc);
950
951         rc = next->do_ops->do_index_try(env, next, feat);
952         if (rc != 0)
953                 RETURN(rc);
954
955         if (lo->ldo_dir_stripe_count > 0) {
956                 int i;
957
958                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
959                         if (dt_object_exists(lo->ldo_stripe[i]) == 0)
960                                 continue;
961                         rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
962                                                 lo->ldo_stripe[i], feat);
963                         if (rc != 0)
964                                 RETURN(rc);
965                 }
966                 dt->do_index_ops = &lod_striped_index_ops;
967         } else {
968                 dt->do_index_ops = &lod_index_ops;
969         }
970
971         RETURN(rc);
972 }
973
974 /**
975  * Implementation of dt_object_operations::do_read_lock.
976  *
977  * \see dt_object_operations::do_read_lock() in the API description for details.
978  */
979 static void lod_read_lock(const struct lu_env *env, struct dt_object *dt,
980                           unsigned role)
981 {
982         dt_read_lock(env, dt_object_child(dt), role);
983 }
984
985 /**
986  * Implementation of dt_object_operations::do_write_lock.
987  *
988  * \see dt_object_operations::do_write_lock() in the API description for
989  * details.
990  */
991 static void lod_write_lock(const struct lu_env *env, struct dt_object *dt,
992                            unsigned role)
993 {
994         dt_write_lock(env, dt_object_child(dt), role);
995 }
996
997 /**
998  * Implementation of dt_object_operations::do_read_unlock.
999  *
1000  * \see dt_object_operations::do_read_unlock() in the API description for
1001  * details.
1002  */
1003 static void lod_read_unlock(const struct lu_env *env, struct dt_object *dt)
1004 {
1005         dt_read_unlock(env, dt_object_child(dt));
1006 }
1007
1008 /**
1009  * Implementation of dt_object_operations::do_write_unlock.
1010  *
1011  * \see dt_object_operations::do_write_unlock() in the API description for
1012  * details.
1013  */
1014 static void lod_write_unlock(const struct lu_env *env, struct dt_object *dt)
1015 {
1016         dt_write_unlock(env, dt_object_child(dt));
1017 }
1018
1019 /**
1020  * Implementation of dt_object_operations::do_write_locked.
1021  *
1022  * \see dt_object_operations::do_write_locked() in the API description for
1023  * details.
1024  */
1025 static int lod_write_locked(const struct lu_env *env, struct dt_object *dt)
1026 {
1027         return dt_write_locked(env, dt_object_child(dt));
1028 }
1029
1030 /**
1031  * Implementation of dt_object_operations::do_attr_get.
1032  *
1033  * \see dt_object_operations::do_attr_get() in the API description for details.
1034  */
1035 static int lod_attr_get(const struct lu_env *env,
1036                         struct dt_object *dt,
1037                         struct lu_attr *attr)
1038 {
1039         /* Note: for striped directory, client will merge attributes
1040          * from all of the sub-stripes see lmv_merge_attr(), and there
1041          * no MDD logic depend on directory nlink/size/time, so we can
1042          * always use master inode nlink and size for now. */
1043         return dt_attr_get(env, dt_object_child(dt), attr);
1044 }
1045
1046 static inline void lod_adjust_stripe_info(struct lod_layout_component *comp,
1047                                           struct lov_desc *desc)
1048 {
1049         if (comp->llc_pattern != LOV_PATTERN_MDT) {
1050                 if (!comp->llc_stripe_count)
1051                         comp->llc_stripe_count =
1052                                 desc->ld_default_stripe_count;
1053         }
1054         if (comp->llc_stripe_size <= 0)
1055                 comp->llc_stripe_size = desc->ld_default_stripe_size;
1056 }
1057
1058 int lod_obj_for_each_stripe(const struct lu_env *env, struct lod_object *lo,
1059                             struct thandle *th,
1060                             struct lod_obj_stripe_cb_data *data)
1061 {
1062         struct lod_layout_component *lod_comp;
1063         int i, j, rc;
1064         ENTRY;
1065
1066         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
1067         for (i = 0; i < lo->ldo_comp_cnt; i++) {
1068                 lod_comp = &lo->ldo_comp_entries[i];
1069
1070                 if (lod_comp->llc_stripe == NULL)
1071                         continue;
1072
1073                 /* has stripe but not inited yet, this component has been
1074                  * declared to be created, but hasn't created yet.
1075                  */
1076                 if (!lod_comp_inited(lod_comp))
1077                         continue;
1078
1079                 if (data->locd_comp_skip_cb &&
1080                     data->locd_comp_skip_cb(env, lo, i, data))
1081                         continue;
1082
1083                 if (data->locd_comp_cb) {
1084                         rc = data->locd_comp_cb(env, lo, i, data);
1085                         if (rc)
1086                                 RETURN(rc);
1087                 }
1088
1089                 /* could used just to do sth about component, not each
1090                  * stripes
1091                  */
1092                 if (!data->locd_stripe_cb)
1093                         continue;
1094
1095                 LASSERT(lod_comp->llc_stripe_count > 0);
1096                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
1097                         struct dt_object *dt = lod_comp->llc_stripe[j];
1098
1099                         if (dt == NULL)
1100                                 continue;
1101                         rc = data->locd_stripe_cb(env, lo, dt, th, i, j, data);
1102                         if (rc != 0)
1103                                 RETURN(rc);
1104                 }
1105         }
1106         RETURN(0);
1107 }
1108
1109 static bool lod_obj_attr_set_comp_skip_cb(const struct lu_env *env,
1110                 struct lod_object *lo, int comp_idx,
1111                 struct lod_obj_stripe_cb_data *data)
1112 {
1113         struct lod_layout_component *lod_comp = &lo->ldo_comp_entries[comp_idx];
1114         bool skipped = false;
1115
1116         if (!(data->locd_attr->la_valid & LA_LAYOUT_VERSION))
1117                 return skipped;
1118
1119         switch (lo->ldo_flr_state) {
1120         case LCM_FL_WRITE_PENDING: {
1121                 int i;
1122
1123                 /* skip stale components */
1124                 if (lod_comp->llc_flags & LCME_FL_STALE) {
1125                         skipped = true;
1126                         break;
1127                 }
1128
1129                 /* skip valid and overlapping components, therefore any
1130                  * attempts to write overlapped components will never succeed
1131                  * because client will get EINPROGRESS. */
1132                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
1133                         if (i == comp_idx)
1134                                 continue;
1135
1136                         if (lo->ldo_comp_entries[i].llc_flags & LCME_FL_STALE)
1137                                 continue;
1138
1139                         if (lu_extent_is_overlapped(&lod_comp->llc_extent,
1140                                         &lo->ldo_comp_entries[i].llc_extent)) {
1141                                 skipped = true;
1142                                 break;
1143                         }
1144                 }
1145                 break;
1146         }
1147         default:
1148                 LASSERTF(0, "impossible: %d\n", lo->ldo_flr_state);
1149         case LCM_FL_SYNC_PENDING:
1150                 break;
1151         }
1152
1153         CDEBUG(D_LAYOUT, DFID": %s to set component %x to version: %u\n",
1154                PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
1155                skipped ? "skipped" : "chose", lod_comp->llc_id,
1156                data->locd_attr->la_layout_version);
1157
1158         return skipped;
1159 }
1160
1161 static inline int
1162 lod_obj_stripe_attr_set_cb(const struct lu_env *env, struct lod_object *lo,
1163                            struct dt_object *dt, struct thandle *th,
1164                            int comp_idx, int stripe_idx,
1165                            struct lod_obj_stripe_cb_data *data)
1166 {
1167         if (data->locd_declare)
1168                 return lod_sub_declare_attr_set(env, dt, data->locd_attr, th);
1169
1170         if (data->locd_attr->la_valid & LA_LAYOUT_VERSION) {
1171                 CDEBUG(D_LAYOUT, DFID": set layout version: %u, comp_idx: %d\n",
1172                        PFID(lu_object_fid(&dt->do_lu)),
1173                        data->locd_attr->la_layout_version, comp_idx);
1174         }
1175
1176         return lod_sub_attr_set(env, dt, data->locd_attr, th);
1177 }
1178
1179 /**
1180  * Implementation of dt_object_operations::do_declare_attr_set.
1181  *
1182  * If the object is striped, then apply the changes to all the stripes.
1183  *
1184  * \see dt_object_operations::do_declare_attr_set() in the API description
1185  * for details.
1186  */
1187 static int lod_declare_attr_set(const struct lu_env *env,
1188                                 struct dt_object *dt,
1189                                 const struct lu_attr *attr,
1190                                 struct thandle *th)
1191 {
1192         struct dt_object  *next = dt_object_child(dt);
1193         struct lod_object *lo = lod_dt_obj(dt);
1194         int                rc, i;
1195         ENTRY;
1196
1197         /*
1198          * declare setattr on the local object
1199          */
1200         rc = lod_sub_declare_attr_set(env, next, attr, th);
1201         if (rc)
1202                 RETURN(rc);
1203
1204         /* osp_declare_attr_set() ignores all attributes other than
1205          * UID, GID, PROJID, and size, and osp_attr_set() ignores all
1206          * but UID, GID and PROJID. Declaration of size attr setting
1207          * happens through lod_declare_init_size(), and not through
1208          * this function. Therefore we need not load striping unless
1209          * ownership is changing.  This should save memory and (we hope)
1210          * speed up rename().
1211          */
1212         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1213                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1214                         RETURN(rc);
1215
1216                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1217                         RETURN(0);
1218         } else {
1219                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1220                                         LA_ATIME | LA_MTIME | LA_CTIME |
1221                                         LA_FLAGS)))
1222                         RETURN(rc);
1223         }
1224         /*
1225          * load striping information, notice we don't do this when object
1226          * is being initialized as we don't need this information till
1227          * few specific cases like destroy, chown
1228          */
1229         rc = lod_striping_load(env, lo);
1230         if (rc)
1231                 RETURN(rc);
1232
1233         if (!lod_obj_is_striped(dt))
1234                 RETURN(0);
1235
1236         /*
1237          * if object is striped declare changes on the stripes
1238          */
1239         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1240                 LASSERT(lo->ldo_stripe);
1241                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1242                         if (lo->ldo_stripe[i] == NULL)
1243                                 continue;
1244                         rc = lod_sub_declare_attr_set(env, lo->ldo_stripe[i],
1245                                                       attr, th);
1246                         if (rc != 0)
1247                                 RETURN(rc);
1248                 }
1249         } else {
1250                 struct lod_obj_stripe_cb_data data = { { 0 } };
1251
1252                 data.locd_attr = attr;
1253                 data.locd_declare = true;
1254                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1255                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1256         }
1257
1258         if (rc)
1259                 RETURN(rc);
1260
1261         if (!dt_object_exists(next) || dt_object_remote(next) ||
1262             !S_ISREG(attr->la_mode))
1263                 RETURN(0);
1264
1265         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1266                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
1267                 RETURN(rc);
1268         }
1269
1270         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1271             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1272                 struct lod_thread_info *info = lod_env_info(env);
1273                 struct lu_buf *buf = &info->lti_buf;
1274
1275                 buf->lb_buf = info->lti_ea_store;
1276                 buf->lb_len = info->lti_ea_store_size;
1277                 rc = lod_sub_declare_xattr_set(env, next, buf, XATTR_NAME_LOV,
1278                                                LU_XATTR_REPLACE, th);
1279         }
1280
1281         RETURN(rc);
1282 }
1283
1284 /**
1285  * Implementation of dt_object_operations::do_attr_set.
1286  *
1287  * If the object is striped, then apply the changes to all or subset of
1288  * the stripes depending on the object type and specific attributes.
1289  *
1290  * \see dt_object_operations::do_attr_set() in the API description for details.
1291  */
1292 static int lod_attr_set(const struct lu_env *env,
1293                         struct dt_object *dt,
1294                         const struct lu_attr *attr,
1295                         struct thandle *th)
1296 {
1297         struct dt_object        *next = dt_object_child(dt);
1298         struct lod_object       *lo = lod_dt_obj(dt);
1299         int                     rc, i;
1300         ENTRY;
1301
1302         /*
1303          * apply changes to the local object
1304          */
1305         rc = lod_sub_attr_set(env, next, attr, th);
1306         if (rc)
1307                 RETURN(rc);
1308
1309         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1310                 if (!(attr->la_valid & LA_REMOTE_ATTR_SET))
1311                         RETURN(rc);
1312
1313                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1314                         RETURN(0);
1315         } else {
1316                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1317                                         LA_ATIME | LA_MTIME | LA_CTIME |
1318                                         LA_FLAGS)))
1319                         RETURN(rc);
1320         }
1321
1322         /* FIXME: a tricky case in the code path of mdd_layout_change():
1323          * the in-memory striping information has been freed in lod_xattr_set()
1324          * due to layout change. It has to load stripe here again. It only
1325          * changes flags of layout so declare_attr_set() is still accurate */
1326         rc = lod_striping_load(env, lo);
1327         if (rc)
1328                 RETURN(rc);
1329
1330         if (!lod_obj_is_striped(dt))
1331                 RETURN(0);
1332
1333         /*
1334          * if object is striped, apply changes to all the stripes
1335          */
1336         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1337                 LASSERT(lo->ldo_stripe);
1338                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1339                         if (unlikely(lo->ldo_stripe[i] == NULL))
1340                                 continue;
1341
1342                         if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1343                                 continue;
1344
1345                         rc = lod_sub_attr_set(env, lo->ldo_stripe[i], attr, th);
1346                         if (rc != 0)
1347                                 break;
1348                 }
1349         } else {
1350                 struct lod_obj_stripe_cb_data data = { { 0 } };
1351
1352                 data.locd_attr = attr;
1353                 data.locd_declare = false;
1354                 data.locd_comp_skip_cb = lod_obj_attr_set_comp_skip_cb;
1355                 data.locd_stripe_cb = lod_obj_stripe_attr_set_cb;
1356                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
1357         }
1358
1359         if (rc)
1360                 RETURN(rc);
1361
1362         if (!dt_object_exists(next) || dt_object_remote(next) ||
1363             !S_ISREG(attr->la_mode))
1364                 RETURN(0);
1365
1366         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1367                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
1368                 RETURN(rc);
1369         }
1370
1371         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1372                 struct lod_thread_info *info = lod_env_info(env);
1373                 struct lu_buf *buf = &info->lti_buf;
1374                 struct ost_id *oi = &info->lti_ostid;
1375                 struct lu_fid *fid = &info->lti_fid;
1376                 struct lov_mds_md_v1 *lmm;
1377                 struct lov_ost_data_v1 *objs;
1378                 __u32 magic;
1379
1380                 rc = lod_get_lov_ea(env, lo);
1381                 if (rc <= 0)
1382                         RETURN(rc);
1383
1384                 buf->lb_buf = info->lti_ea_store;
1385                 buf->lb_len = info->lti_ea_store_size;
1386                 lmm = info->lti_ea_store;
1387                 magic = le32_to_cpu(lmm->lmm_magic);
1388                 if (magic == LOV_MAGIC_COMP_V1) {
1389                         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1390                         struct lov_comp_md_entry_v1 *lcme =
1391                                                 &lcm->lcm_entries[0];
1392
1393                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1394                         magic = le32_to_cpu(lmm->lmm_magic);
1395                 }
1396
1397                 if (magic == LOV_MAGIC_V1)
1398                         objs = &(lmm->lmm_objects[0]);
1399                 else
1400                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1401                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1402                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1403                 fid->f_oid--;
1404                 fid_to_ostid(fid, oi);
1405                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1406
1407                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1408                                        LU_XATTR_REPLACE, th);
1409         } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1410                 struct lod_thread_info *info = lod_env_info(env);
1411                 struct lu_buf *buf = &info->lti_buf;
1412                 struct lov_comp_md_v1 *lcm;
1413                 struct lov_comp_md_entry_v1 *lcme;
1414
1415                 rc = lod_get_lov_ea(env, lo);
1416                 if (rc <= 0)
1417                         RETURN(rc);
1418
1419                 buf->lb_buf = info->lti_ea_store;
1420                 buf->lb_len = info->lti_ea_store_size;
1421                 lcm = buf->lb_buf;
1422                 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1423                         RETURN(-EINVAL);
1424
1425                 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1426                 lcme = &lcm->lcm_entries[0];
1427                 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1428                 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1429
1430                 rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LOV,
1431                                        LU_XATTR_REPLACE, th);
1432         }
1433
1434         RETURN(rc);
1435 }
1436
1437 /**
1438  * Implementation of dt_object_operations::do_xattr_get.
1439  *
1440  * If LOV EA is requested from the root object and it's not
1441  * found, then return default striping for the filesystem.
1442  *
1443  * \see dt_object_operations::do_xattr_get() in the API description for details.
1444  */
1445 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1446                          struct lu_buf *buf, const char *name)
1447 {
1448         struct lod_thread_info *info = lod_env_info(env);
1449         struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1450         int is_root;
1451         int rc;
1452         ENTRY;
1453
1454         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1455         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1456                 struct lmv_mds_md_v1    *lmv1;
1457                 int                      rc1 = 0;
1458
1459                 if (rc > (typeof(rc))sizeof(*lmv1))
1460                         RETURN(rc);
1461
1462                 if (rc < (typeof(rc))sizeof(*lmv1))
1463                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1464
1465                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1466                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1467
1468                         info->lti_buf.lb_buf = info->lti_key;
1469                         info->lti_buf.lb_len = sizeof(*lmv1);
1470                         rc = dt_xattr_get(env, dt_object_child(dt),
1471                                           &info->lti_buf, name);
1472                         if (unlikely(rc != sizeof(*lmv1)))
1473                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1474
1475                         lmv1 = info->lti_buf.lb_buf;
1476                         /* The on-disk LMV EA only contains header, but the
1477                          * returned LMV EA size should contain the space for
1478                          * the FIDs of all shards of the striped directory. */
1479                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1480                                 rc = lmv_mds_md_size(
1481                                         le32_to_cpu(lmv1->lmv_stripe_count),
1482                                         LMV_MAGIC_V1);
1483                 } else {
1484                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1485                                                   buf, false);
1486                 }
1487
1488                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1489         }
1490
1491         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1492                 RETURN(rc);
1493
1494         /*
1495          * XXX: Only used by lfsck
1496          *
1497          * lod returns default striping on the real root of the device
1498          * this is like the root stores default striping for the whole
1499          * filesystem. historically we've been using a different approach
1500          * and store it in the config.
1501          */
1502         dt_root_get(env, dev->lod_child, &info->lti_fid);
1503         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1504
1505         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1506                 struct lov_user_md *lum = buf->lb_buf;
1507                 struct lov_desc    *desc = &dev->lod_desc;
1508
1509                 if (buf->lb_buf == NULL) {
1510                         rc = sizeof(*lum);
1511                 } else if (buf->lb_len >= sizeof(*lum)) {
1512                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1513                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1514                         lmm_oi_set_id(&lum->lmm_oi, 0);
1515                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1516                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1517                         lum->lmm_stripe_size = cpu_to_le32(
1518                                                 desc->ld_default_stripe_size);
1519                         lum->lmm_stripe_count = cpu_to_le16(
1520                                                 desc->ld_default_stripe_count);
1521                         lum->lmm_stripe_offset = cpu_to_le16(
1522                                                 desc->ld_default_stripe_offset);
1523                         rc = sizeof(*lum);
1524                 } else {
1525                         rc = -ERANGE;
1526                 }
1527         }
1528
1529         RETURN(rc);
1530 }
1531
1532 /**
1533  * Verify LVM EA.
1534  *
1535  * Checks that the magic of the stripe is sane.
1536  *
1537  * \param[in] lod       lod device
1538  * \param[in] lum       a buffer storing LMV EA to verify
1539  *
1540  * \retval              0 if the EA is sane
1541  * \retval              negative otherwise
1542  */
1543 static int lod_verify_md_striping(struct lod_device *lod,
1544                                   const struct lmv_user_md_v1 *lum)
1545 {
1546         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1547                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1548                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1549                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1550                        (int)le32_to_cpu(lum->lum_stripe_offset),
1551                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1552                 return -EINVAL;
1553         }
1554
1555         return 0;
1556 }
1557
1558 /**
1559  * Initialize LMV EA for a slave.
1560  *
1561  * Initialize slave's LMV EA from the master's LMV EA.
1562  *
1563  * \param[in] master_lmv        a buffer containing master's EA
1564  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1565  *
1566  */
1567 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1568                                   const struct lmv_mds_md_v1 *master_lmv)
1569 {
1570         *slave_lmv = *master_lmv;
1571         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1572 }
1573
1574 /**
1575  * Generate LMV EA.
1576  *
1577  * Generate LMV EA from the object passed as \a dt. The object must have
1578  * the stripes created and initialized.
1579  *
1580  * \param[in] env       execution environment
1581  * \param[in] dt        object
1582  * \param[out] lmv_buf  buffer storing generated LMV EA
1583  *
1584  * \retval              0 on success
1585  * \retval              negative if failed
1586  */
1587 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1588                            struct lu_buf *lmv_buf)
1589 {
1590         struct lod_thread_info  *info = lod_env_info(env);
1591         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1592         struct lod_object       *lo = lod_dt_obj(dt);
1593         struct lmv_mds_md_v1    *lmm1;
1594         int                     stripe_count;
1595         int                     type = LU_SEQ_RANGE_ANY;
1596         int                     rc;
1597         __u32                   mdtidx;
1598         ENTRY;
1599
1600         LASSERT(lo->ldo_dir_striped != 0);
1601         LASSERT(lo->ldo_dir_stripe_count > 0);
1602         stripe_count = lo->ldo_dir_stripe_count;
1603         /* Only store the LMV EA heahder on the disk. */
1604         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1605                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1606                 if (rc != 0)
1607                         RETURN(rc);
1608         } else {
1609                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1610         }
1611
1612         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1613         memset(lmm1, 0, sizeof(*lmm1));
1614         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1615         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1616         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1617         if (lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) {
1618                 lmm1->lmv_migrate_hash = cpu_to_le32(lo->ldo_dir_migrate_hash);
1619                 lmm1->lmv_migrate_offset =
1620                         cpu_to_le32(lo->ldo_dir_migrate_offset);
1621         }
1622         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1623                             &mdtidx, &type);
1624         if (rc != 0)
1625                 RETURN(rc);
1626
1627         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1628         lmv_buf->lb_buf = info->lti_ea_store;
1629         lmv_buf->lb_len = sizeof(*lmm1);
1630
1631         RETURN(rc);
1632 }
1633
1634 /**
1635  * Create in-core represenation for a striped directory.
1636  *
1637  * Parse the buffer containing LMV EA and instantiate LU objects
1638  * representing the stripe objects. The pointers to the objects are
1639  * stored in ldo_stripe field of \a lo. This function is used when
1640  * we need to access an already created object (i.e. load from a disk).
1641  *
1642  * \param[in] env       execution environment
1643  * \param[in] lo        lod object
1644  * \param[in] buf       buffer containing LMV EA
1645  *
1646  * \retval              0 on success
1647  * \retval              negative if failed
1648  */
1649 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1650                            const struct lu_buf *buf)
1651 {
1652         struct lod_thread_info  *info = lod_env_info(env);
1653         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1654         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1655         struct dt_object        **stripe;
1656         union lmv_mds_md        *lmm = buf->lb_buf;
1657         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1658         struct lu_fid           *fid = &info->lti_fid;
1659         unsigned int            i;
1660         int                     rc = 0;
1661         ENTRY;
1662
1663         LASSERT(mutex_is_locked(&lo->ldo_layout_mutex));
1664
1665         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1666                 lo->ldo_dir_slave_stripe = 1;
1667                 RETURN(0);
1668         }
1669
1670         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1671                 RETURN(-EINVAL);
1672
1673         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1674                 RETURN(0);
1675
1676         LASSERT(lo->ldo_stripe == NULL);
1677         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1678                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1679         if (stripe == NULL)
1680                 RETURN(-ENOMEM);
1681
1682         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1683                 struct dt_device        *tgt_dt;
1684                 struct dt_object        *dto;
1685                 int                     type = LU_SEQ_RANGE_ANY;
1686                 __u32                   idx;
1687
1688                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1689                 if (!fid_is_sane(fid))
1690                         GOTO(out, rc = -ESTALE);
1691
1692                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1693                 if (rc != 0)
1694                         GOTO(out, rc);
1695
1696                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1697                         tgt_dt = lod->lod_child;
1698                 } else {
1699                         struct lod_tgt_desc     *tgt;
1700
1701                         tgt = LTD_TGT(ltd, idx);
1702                         if (tgt == NULL)
1703                                 GOTO(out, rc = -ESTALE);
1704                         tgt_dt = tgt->ltd_tgt;
1705                 }
1706
1707                 dto = dt_locate_at(env, tgt_dt, fid,
1708                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1709                                   NULL);
1710                 if (IS_ERR(dto))
1711                         GOTO(out, rc = PTR_ERR(dto));
1712
1713                 stripe[i] = dto;
1714         }
1715 out:
1716         lo->ldo_stripe = stripe;
1717         lo->ldo_dir_stripe_count = le32_to_cpu(lmv1->lmv_stripe_count);
1718         lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1719         if (rc != 0)
1720                 lod_striping_free_nolock(env, lo);
1721
1722         RETURN(rc);
1723 }
1724
1725 /**
1726  * Declare create a striped directory.
1727  *
1728  * Declare creating a striped directory with a given stripe pattern on the
1729  * specified MDTs. A striped directory is represented as a regular directory
1730  * - an index listing all the stripes. The stripes point back to the master
1731  * object with ".." and LinkEA. The master object gets LMV EA which
1732  * identifies it as a striped directory. The function allocates FIDs
1733  * for all stripes.
1734  *
1735  * \param[in] env       execution environment
1736  * \param[in] dt        object
1737  * \param[in] attr      attributes to initialize the objects with
1738  * \param[in] dof       type of objects to be created
1739  * \param[in] th        transaction handle
1740  *
1741  * \retval              0 on success
1742  * \retval              negative if failed
1743  */
1744 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1745                                           struct dt_object *dt,
1746                                           struct lu_attr *attr,
1747                                           struct dt_object_format *dof,
1748                                           struct thandle *th)
1749 {
1750         struct lod_thread_info  *info = lod_env_info(env);
1751         struct lu_buf           lmv_buf;
1752         struct lu_buf           slave_lmv_buf;
1753         struct lmv_mds_md_v1    *lmm;
1754         struct lmv_mds_md_v1    *slave_lmm = NULL;
1755         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1756         struct lod_object       *lo = lod_dt_obj(dt);
1757         int                     rc;
1758         __u32                   i;
1759         ENTRY;
1760
1761         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1762         if (rc != 0)
1763                 GOTO(out, rc);
1764         lmm = lmv_buf.lb_buf;
1765
1766         OBD_ALLOC_PTR(slave_lmm);
1767         if (slave_lmm == NULL)
1768                 GOTO(out, rc = -ENOMEM);
1769
1770         lod_prep_slave_lmv_md(slave_lmm, lmm);
1771         slave_lmv_buf.lb_buf = slave_lmm;
1772         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1773
1774         if (!dt_try_as_dir(env, dt_object_child(dt)))
1775                 GOTO(out, rc = -EINVAL);
1776
1777         rec->rec_type = S_IFDIR;
1778         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
1779                 struct dt_object        *dto = lo->ldo_stripe[i];
1780                 char                    *stripe_name = info->lti_key;
1781                 struct lu_name          *sname;
1782                 struct linkea_data       ldata          = { NULL };
1783                 struct lu_buf           linkea_buf;
1784
1785                 rc = lod_sub_declare_create(env, dto, attr, NULL, dof, th);
1786                 if (rc != 0)
1787                         GOTO(out, rc);
1788
1789                 if (!dt_try_as_dir(env, dto))
1790                         GOTO(out, rc = -EINVAL);
1791
1792                 rc = lod_sub_declare_ref_add(env, dto, th);
1793                 if (rc != 0)
1794                         GOTO(out, rc);
1795
1796                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1797                 rc = lod_sub_declare_insert(env, dto,
1798                                             (const struct dt_rec *)rec,
1799                                             (const struct dt_key *)dot, th);
1800                 if (rc != 0)
1801                         GOTO(out, rc);
1802
1803                 /* master stripe FID will be put to .. */
1804                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1805                 rc = lod_sub_declare_insert(env, dto,
1806                                             (const struct dt_rec *)rec,
1807                                             (const struct dt_key *)dotdot, th);
1808                 if (rc != 0)
1809                         GOTO(out, rc);
1810
1811                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1812                     cfs_fail_val != i) {
1813                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1814                             cfs_fail_val == i)
1815                                 slave_lmm->lmv_master_mdt_index =
1816                                                         cpu_to_le32(i + 1);
1817                         else
1818                                 slave_lmm->lmv_master_mdt_index =
1819                                                         cpu_to_le32(i);
1820                         rc = lod_sub_declare_xattr_set(env, dto, &slave_lmv_buf,
1821                                                        XATTR_NAME_LMV, 0, th);
1822                         if (rc != 0)
1823                                 GOTO(out, rc);
1824                 }
1825
1826                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1827                     cfs_fail_val == i)
1828                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1829                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1830                 else
1831                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1832                                 PFID(lu_object_fid(&dto->do_lu)), i);
1833
1834                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1835                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1836                                       sname, lu_object_fid(&dt->do_lu));
1837                 if (rc != 0)
1838                         GOTO(out, rc);
1839
1840                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1841                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1842                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
1843                                                XATTR_NAME_LINK, 0, th);
1844                 if (rc != 0)
1845                         GOTO(out, rc);
1846
1847                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1848                 rc = lod_sub_declare_insert(env, dt_object_child(dt),
1849                                             (const struct dt_rec *)rec,
1850                                             (const struct dt_key *)stripe_name,
1851                                             th);
1852                 if (rc != 0)
1853                         GOTO(out, rc);
1854
1855                 rc = lod_sub_declare_ref_add(env, dt_object_child(dt), th);
1856                 if (rc != 0)
1857                         GOTO(out, rc);
1858         }
1859
1860         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt),
1861                                        &lmv_buf, XATTR_NAME_LMV, 0, th);
1862         if (rc != 0)
1863                 GOTO(out, rc);
1864 out:
1865         if (slave_lmm != NULL)
1866                 OBD_FREE_PTR(slave_lmm);
1867
1868         RETURN(rc);
1869 }
1870
1871 static int lod_prep_md_striped_create(const struct lu_env *env,
1872                                       struct dt_object *dt,
1873                                       struct lu_attr *attr,
1874                                       const struct lmv_user_md_v1 *lum,
1875                                       struct dt_object_format *dof,
1876                                       struct thandle *th)
1877 {
1878         struct lod_thread_info  *info = lod_env_info(env);
1879         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1880         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1881         struct lod_object       *lo = lod_dt_obj(dt);
1882         struct dt_object        **stripe;
1883         __u32                   stripe_count;
1884         int                     *idx_array;
1885         __u32                   master_index;
1886         int                     rc = 0;
1887         __u32                   i;
1888         __u32                   j;
1889         bool                    is_specific = false;
1890         ENTRY;
1891
1892         /* The lum has been verifed in lod_verify_md_striping */
1893         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC ||
1894                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC);
1895
1896         stripe_count = lo->ldo_dir_stripe_count;
1897
1898         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1899         if (idx_array == NULL)
1900                 RETURN(-ENOMEM);
1901
1902         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1903         if (stripe == NULL)
1904                 GOTO(out_free, rc = -ENOMEM);
1905
1906         /* Start index must be the master MDT */
1907         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1908         idx_array[0] = master_index;
1909         if (le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC_SPECIFIC) {
1910                 is_specific = true;
1911                 for (i = 1; i < stripe_count; i++)
1912                         idx_array[i] = le32_to_cpu(lum->lum_objects[i].lum_mds);
1913         }
1914
1915         for (i = 0; i < stripe_count; i++) {
1916                 struct lod_tgt_desc     *tgt = NULL;
1917                 struct dt_object        *dto;
1918                 struct lu_fid           fid = { 0 };
1919                 int                     idx;
1920                 struct lu_object_conf   conf = { 0 };
1921                 struct dt_device        *tgt_dt = NULL;
1922
1923                 /* Try to find next avaible target */
1924                 idx = idx_array[i];
1925                 for (j = 0; j < lod->lod_remote_mdt_count;
1926                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1927                         bool already_allocated = false;
1928                         __u32 k;
1929
1930                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1931                                idx, lod->lod_remote_mdt_count + 1, i);
1932
1933                         if (likely(!is_specific &&
1934                                    !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1935                                 /* check whether the idx already exists
1936                                  * in current allocated array */
1937                                 for (k = 0; k < i; k++) {
1938                                         if (idx_array[k] == idx) {
1939                                                 already_allocated = true;
1940                                                 break;
1941                                         }
1942                                 }
1943
1944                                 if (already_allocated)
1945                                         continue;
1946                         }
1947
1948                         /* Sigh, this index is not in the bitmap, let's check
1949                          * next available target */
1950                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1951                             idx != master_index)
1952                                 continue;
1953
1954                         if (idx == master_index) {
1955                                 /* Allocate the FID locally */
1956                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1957                                                    &fid, NULL);
1958                                 if (rc < 0)
1959                                         GOTO(out_put, rc);
1960                                 tgt_dt = lod->lod_child;
1961                                 break;
1962                         }
1963
1964                         /* check the status of the OSP */
1965                         tgt = LTD_TGT(ltd, idx);
1966                         if (tgt == NULL)
1967                                 continue;
1968
1969                         tgt_dt = tgt->ltd_tgt;
1970                         rc = dt_statfs(env, tgt_dt, &info->lti_osfs);
1971                         if (rc) {
1972                                 /* this OSP doesn't feel well */
1973                                 rc = 0;
1974                                 continue;
1975                         }
1976
1977                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1978                         if (rc < 0) {
1979                                 rc = 0;
1980                                 continue;
1981                         }
1982
1983                         break;
1984                 }
1985
1986                 /* Can not allocate more stripes */
1987                 if (j == lod->lod_remote_mdt_count) {
1988                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1989                                lod2obd(lod)->obd_name, stripe_count, i);
1990                         break;
1991                 }
1992
1993                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1994                        idx, i, PFID(&fid));
1995                 idx_array[i] = idx;
1996                 /* Set the start index for next stripe allocation */
1997                 if (!is_specific && i < stripe_count - 1) {
1998                         /*
1999                          * for large dir test, put all other slaves on one
2000                          * remote MDT, otherwise we may save too many local
2001                          * slave locks which will exceed RS_MAX_LOCKS.
2002                          */
2003                         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)))
2004                                 idx = master_index;
2005                         idx_array[i + 1] = (idx + 1) %
2006                                            (lod->lod_remote_mdt_count + 1);
2007                 }
2008                 /* tgt_dt and fid must be ready after search avaible OSP
2009                  * in the above loop */
2010                 LASSERT(tgt_dt != NULL);
2011                 LASSERT(fid_is_sane(&fid));
2012                 conf.loc_flags = LOC_F_NEW;
2013                 dto = dt_locate_at(env, tgt_dt, &fid,
2014                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
2015                                    &conf);
2016                 if (IS_ERR(dto))
2017                         GOTO(out_put, rc = PTR_ERR(dto));
2018                 stripe[i] = dto;
2019         }
2020
2021         lo->ldo_dir_striped = 1;
2022         lo->ldo_stripe = stripe;
2023         lo->ldo_dir_stripe_count = i;
2024         lo->ldo_dir_stripes_allocated = stripe_count;
2025         smp_mb();
2026         lo->ldo_dir_stripe_loaded = 1;
2027
2028         if (lo->ldo_dir_stripe_count == 0)
2029                 GOTO(out_put, rc = -ENOSPC);
2030
2031         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
2032         if (rc != 0)
2033                 GOTO(out_put, rc);
2034
2035 out_put:
2036         if (rc < 0) {
2037                 for (i = 0; i < stripe_count; i++)
2038                         if (stripe[i] != NULL)
2039                                 dt_object_put(env, stripe[i]);
2040                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
2041                 lo->ldo_dir_stripe_count = 0;
2042                 lo->ldo_dir_stripes_allocated = 0;
2043                 lo->ldo_stripe = NULL;
2044         }
2045
2046 out_free:
2047         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
2048
2049         RETURN(rc);
2050 }
2051
2052 /**
2053  * Declare create striped md object.
2054  *
2055  * The function declares intention to create a striped directory. This is a
2056  * wrapper for lod_prep_md_striped_create(). The only additional functionality
2057  * is to verify pattern \a lum_buf is good. Check that function for the details.
2058  *
2059  * \param[in] env       execution environment
2060  * \param[in] dt        object
2061  * \param[in] attr      attributes to initialize the objects with
2062  * \param[in] lum_buf   a pattern specifying the number of stripes and
2063  *                      MDT to start from
2064  * \param[in] dof       type of objects to be created
2065  * \param[in] th        transaction handle
2066  *
2067  * \retval              0 on success
2068  * \retval              negative if failed
2069  *
2070  */
2071 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
2072                                      struct dt_object *dt,
2073                                      struct lu_attr *attr,
2074                                      const struct lu_buf *lum_buf,
2075                                      struct dt_object_format *dof,
2076                                      struct thandle *th)
2077 {
2078         struct lod_object       *lo = lod_dt_obj(dt);
2079         struct lmv_user_md_v1   *lum = lum_buf->lb_buf;
2080         int                     rc;
2081         ENTRY;
2082
2083         LASSERT(lum != NULL);
2084
2085         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
2086                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
2087                (int)le32_to_cpu(lum->lum_stripe_offset));
2088
2089         if (lo->ldo_dir_stripe_count == 0)
2090                 GOTO(out, rc = 0);
2091
2092         /* prepare dir striped objects */
2093         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2094         if (rc != 0) {
2095                 /* failed to create striping, let's reset
2096                  * config so that others don't get confused */
2097                 lod_striping_free(env, lo);
2098                 GOTO(out, rc);
2099         }
2100 out:
2101         RETURN(rc);
2102 }
2103
2104 /**
2105  * Append source stripes after target stripes for migrating directory. NB, we
2106  * only need to declare this, the append is done inside lod_xattr_set_lmv().
2107  *
2108  * \param[in] env       execution environment
2109  * \param[in] dt        target object
2110  * \param[in] buf       LMV buf which contains source stripe fids
2111  * \param[in] th        transaction handle
2112  *
2113  * \retval              0 on success
2114  * \retval              negative if failed
2115  */
2116 static int lod_dir_declare_layout_add(const struct lu_env *env,
2117                                       struct dt_object *dt,
2118                                       const struct lu_buf *buf,
2119                                       struct thandle *th)
2120 {
2121         struct lod_thread_info *info = lod_env_info(env);
2122         struct lod_device *lod = lu2lod_dev(dt->do_lu.lo_dev);
2123         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
2124         struct lod_object *lo = lod_dt_obj(dt);
2125         struct dt_object *next = dt_object_child(dt);
2126         struct dt_object_format *dof = &info->lti_format;
2127         struct lmv_mds_md_v1 *lmv = buf->lb_buf;
2128         struct dt_object **stripe;
2129         __u32 stripe_count = le32_to_cpu(lmv->lmv_stripe_count);
2130         struct lu_fid *fid = &info->lti_fid;
2131         struct lod_tgt_desc *tgt;
2132         struct dt_object *dto;
2133         struct dt_device *tgt_dt;
2134         int type = LU_SEQ_RANGE_ANY;
2135         struct dt_insert_rec *rec = &info->lti_dt_rec;
2136         char *stripe_name = info->lti_key;
2137         struct lu_name *sname;
2138         struct linkea_data ldata = { NULL };
2139         struct lu_buf linkea_buf;
2140         __u32 idx;
2141         int i;
2142         int rc;
2143
2144         ENTRY;
2145
2146         if (le32_to_cpu(lmv->lmv_magic) != LMV_MAGIC_V1)
2147                 RETURN(-EINVAL);
2148
2149         if (stripe_count == 0)
2150                 RETURN(-EINVAL);
2151
2152         dof->dof_type = DFT_DIR;
2153
2154         OBD_ALLOC(stripe,
2155                   sizeof(*stripe) * (lo->ldo_dir_stripe_count + stripe_count));
2156         if (stripe == NULL)
2157                 RETURN(-ENOMEM);
2158
2159         for (i = 0; i < lo->ldo_dir_stripe_count; i++)
2160                 stripe[i] = lo->ldo_stripe[i];
2161
2162         for (i = 0; i < stripe_count; i++) {
2163                 fid_le_to_cpu(fid,
2164                         &lmv->lmv_stripe_fids[i]);
2165                 if (!fid_is_sane(fid))
2166                         GOTO(out, rc = -ESTALE);
2167
2168                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
2169                 if (rc)
2170                         GOTO(out, rc);
2171
2172                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
2173                         tgt_dt = lod->lod_child;
2174                 } else {
2175                         tgt = LTD_TGT(ltd, idx);
2176                         if (tgt == NULL)
2177                                 GOTO(out, rc = -ESTALE);
2178                         tgt_dt = tgt->ltd_tgt;
2179                 }
2180
2181                 dto = dt_locate_at(env, tgt_dt, fid,
2182                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
2183                                   NULL);
2184                 if (IS_ERR(dto))
2185                         GOTO(out, rc = PTR_ERR(dto));
2186
2187                 stripe[i + lo->ldo_dir_stripe_count] = dto;
2188
2189                 if (!dt_try_as_dir(env, dto))
2190                         GOTO(out, rc = -ENOTDIR);
2191
2192                 rc = lod_sub_declare_ref_add(env, dto, th);
2193                 if (rc)
2194                         GOTO(out, rc);
2195
2196                 rc = lod_sub_declare_insert(env, dto,
2197                                             (const struct dt_rec *)rec,
2198                                             (const struct dt_key *)dot, th);
2199                 if (rc)
2200                         GOTO(out, rc);
2201
2202                 rc = lod_sub_declare_insert(env, dto,
2203                                             (const struct dt_rec *)rec,
2204                                             (const struct dt_key *)dotdot, th);
2205                 if (rc)
2206                         GOTO(out, rc);
2207
2208                 rc = lod_sub_declare_xattr_set(env, dto, buf,
2209                                                 XATTR_NAME_LMV, 0, th);
2210                 if (rc)
2211                         GOTO(out, rc);
2212
2213                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
2214                          PFID(lu_object_fid(&dto->do_lu)),
2215                          i + lo->ldo_dir_stripe_count);
2216
2217                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2218                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2219                                       sname, lu_object_fid(&dt->do_lu));
2220                 if (rc)
2221                         GOTO(out, rc);
2222
2223                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2224                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2225                 rc = lod_sub_declare_xattr_set(env, dto, &linkea_buf,
2226                                                XATTR_NAME_LINK, 0, th);
2227                 if (rc)
2228                         GOTO(out, rc);
2229
2230                 rc = lod_sub_declare_insert(env, next,
2231                                             (const struct dt_rec *)rec,
2232                                             (const struct dt_key *)stripe_name,
2233                                             th);
2234                 if (rc)
2235                         GOTO(out, rc);
2236
2237                 rc = lod_sub_declare_ref_add(env, next, th);
2238                 if (rc)
2239                         GOTO(out, rc);
2240         }
2241
2242         if (lo->ldo_stripe)
2243                 OBD_FREE(lo->ldo_stripe,
2244                          sizeof(*stripe) * lo->ldo_dir_stripes_allocated);
2245         lo->ldo_stripe = stripe;
2246         lo->ldo_dir_migrate_offset = lo->ldo_dir_stripe_count;
2247         lo->ldo_dir_migrate_hash = le32_to_cpu(lmv->lmv_hash_type);
2248         lo->ldo_dir_stripe_count += stripe_count;
2249         lo->ldo_dir_stripes_allocated += stripe_count;
2250         lo->ldo_dir_hash_type |= LMV_HASH_FLAG_MIGRATION;
2251
2252         RETURN(0);
2253 out:
2254         i = lo->ldo_dir_stripe_count;
2255         while (i < lo->ldo_dir_stripe_count + stripe_count && stripe[i])
2256                 dt_object_put(env, stripe[i++]);
2257
2258         OBD_FREE(stripe,
2259                  sizeof(*stripe) * (stripe_count + lo->ldo_dir_stripe_count));
2260         RETURN(rc);
2261 }
2262
2263 static int lod_dir_declare_layout_delete(const struct lu_env *env,
2264                                          struct dt_object *dt,
2265                                          const struct lu_buf *buf,
2266                                          struct thandle *th)
2267 {
2268         struct lod_thread_info *info = lod_env_info(env);
2269         struct lod_object *lo = lod_dt_obj(dt);
2270         struct dt_object *next = dt_object_child(dt);
2271         struct lmv_user_md *lmu = buf->lb_buf;
2272         __u32 final_stripe_count;
2273         char *stripe_name = info->lti_key;
2274         struct dt_object *dto;
2275         int i;
2276         int rc = 0;
2277
2278         if (!lmu)
2279                 return -EINVAL;
2280
2281         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2282         if (final_stripe_count >= lo->ldo_dir_stripe_count)
2283                 return -EINVAL;
2284
2285         for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2286                 dto = lo->ldo_stripe[i];
2287                 LASSERT(dto);
2288
2289                 if (!dt_try_as_dir(env, dto))
2290                         return -ENOTDIR;
2291
2292                 rc = lod_sub_declare_delete(env, dto,
2293                                             (const struct dt_key *)dot, th);
2294                 if (rc)
2295                         return rc;
2296
2297                 rc = lod_sub_declare_ref_del(env, dto, th);
2298                 if (rc)
2299                         return rc;
2300
2301                 rc = lod_sub_declare_delete(env, dto,
2302                                         (const struct dt_key *)dotdot, th);
2303                 if (rc)
2304                         return rc;
2305
2306                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2307                          PFID(lu_object_fid(&dto->do_lu)), i);
2308
2309                 rc = lod_sub_declare_delete(env, next,
2310                                         (const struct dt_key *)stripe_name, th);
2311                 if (rc)
2312                         return rc;
2313
2314                 rc = lod_sub_declare_ref_del(env, next, th);
2315                 if (rc)
2316                         return rc;
2317         }
2318
2319         return 0;
2320 }
2321
2322 /*
2323  * delete stripes from dir master object, the lum_stripe_count in argument is
2324  * the final stripe count, the stripes after that will be deleted, NB, they
2325  * are not destroyed, but deleted from it's parent namespace, this function
2326  * will be called in two places:
2327  * 1. mdd_migrate_create() delete stripes from source, and append them to
2328  *    target.
2329  * 2. mdd_dir_layout_shrink() delete stripes from source, and destroy them.
2330  */
2331 static int lod_dir_layout_delete(const struct lu_env *env,
2332                                  struct dt_object *dt,
2333                                  const struct lu_buf *buf,
2334                                  struct thandle *th)
2335 {
2336         struct lod_thread_info *info = lod_env_info(env);
2337         struct lod_object *lo = lod_dt_obj(dt);
2338         struct dt_object *next = dt_object_child(dt);
2339         struct lmv_user_md *lmu = buf->lb_buf;
2340         __u32 final_stripe_count;
2341         char *stripe_name = info->lti_key;
2342         struct dt_object *dto;
2343         int i;
2344         int rc = 0;
2345
2346         ENTRY;
2347
2348         if (!lmu)
2349                 RETURN(-EINVAL);
2350
2351         final_stripe_count = le32_to_cpu(lmu->lum_stripe_count);
2352         if (final_stripe_count >= lo->ldo_dir_stripe_count)
2353                 RETURN(-EINVAL);
2354
2355         for (i = final_stripe_count; i < lo->ldo_dir_stripe_count; i++) {
2356                 dto = lo->ldo_stripe[i];
2357                 LASSERT(dto);
2358
2359                 rc = lod_sub_delete(env, dto,
2360                                     (const struct dt_key *)dotdot, th);
2361                 if (rc)
2362                         break;
2363
2364                 snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2365                          PFID(lu_object_fid(&dto->do_lu)), i);
2366
2367                 rc = lod_sub_delete(env, next,
2368                                     (const struct dt_key *)stripe_name, th);
2369                 if (rc)
2370                         break;
2371
2372                 rc = lod_sub_ref_del(env, next, th);
2373                 if (rc)
2374                         break;
2375         }
2376
2377         lod_striping_free(env, lod_dt_obj(dt));
2378
2379         RETURN(rc);
2380 }
2381
2382 /**
2383  * Implementation of dt_object_operations::do_declare_xattr_set.
2384  *
2385  * Used with regular (non-striped) objects. Basically it
2386  * initializes the striping information and applies the
2387  * change to all the stripes.
2388  *
2389  * \see dt_object_operations::do_declare_xattr_set() in the API description
2390  * for details.
2391  */
2392 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2393                                      struct dt_object *dt,
2394                                      const struct lu_buf *buf,
2395                                      const char *name, int fl,
2396                                      struct thandle *th)
2397 {
2398         struct dt_object        *next = dt_object_child(dt);
2399         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2400         struct lod_object       *lo = lod_dt_obj(dt);
2401         int                     i;
2402         int                     rc;
2403         ENTRY;
2404
2405         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2406                 struct lmv_user_md_v1 *lum;
2407
2408                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2409                 lum = buf->lb_buf;
2410                 rc = lod_verify_md_striping(d, lum);
2411                 if (rc != 0)
2412                         RETURN(rc);
2413         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2414                 rc = lod_verify_striping(d, lo, buf, false);
2415                 if (rc != 0)
2416                         RETURN(rc);
2417         }
2418
2419         rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
2420         if (rc != 0)
2421                 RETURN(rc);
2422
2423         /* Note: Do not set LinkEA on sub-stripes, otherwise
2424          * it will confuse the fid2path process(see mdt_path_current()).
2425          * The linkEA between master and sub-stripes is set in
2426          * lod_xattr_set_lmv(). */
2427         if (strcmp(name, XATTR_NAME_LINK) == 0)
2428                 RETURN(0);
2429
2430         /* set xattr to each stripes, if needed */
2431         rc = lod_striping_load(env, lo);
2432         if (rc != 0)
2433                 RETURN(rc);
2434
2435         if (lo->ldo_dir_stripe_count == 0)
2436                 RETURN(0);
2437
2438         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
2439                 LASSERT(lo->ldo_stripe[i]);
2440
2441                 rc = lod_sub_declare_xattr_set(env, lo->ldo_stripe[i],
2442                                                buf, name, fl, th);
2443                 if (rc != 0)
2444                         break;
2445         }
2446
2447         RETURN(rc);
2448 }
2449
2450 static int
2451 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2452                                      struct lod_object *lo,
2453                                      struct dt_object *dt, struct thandle *th,
2454                                      int comp_idx, int stripe_idx,
2455                                      struct lod_obj_stripe_cb_data *data)
2456 {
2457         struct lod_thread_info *info = lod_env_info(env);
2458         struct lod_layout_component *comp = &lo->ldo_comp_entries[comp_idx];
2459         struct filter_fid *ff = &info->lti_ff;
2460         struct lu_buf *buf = &info->lti_buf;
2461         int rc;
2462
2463         buf->lb_buf = ff;
2464         buf->lb_len = sizeof(*ff);
2465         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2466         if (rc < 0) {
2467                 if (rc == -ENODATA)
2468                         return 0;
2469                 return rc;
2470         }
2471
2472         /*
2473          * locd_buf is set if it's called by dir migration, which doesn't check
2474          * pfid and comp id.
2475          */
2476         if (data->locd_buf) {
2477                 memset(ff, 0, sizeof(*ff));
2478                 ff->ff_parent = *(struct lu_fid *)data->locd_buf->lb_buf;
2479         } else {
2480                 filter_fid_le_to_cpu(ff, ff, sizeof(*ff));
2481
2482                 if (lu_fid_eq(lod_object_fid(lo), &ff->ff_parent) &&
2483                     ff->ff_layout.ol_comp_id == comp->llc_id)
2484                         return 0;
2485
2486                 memset(ff, 0, sizeof(*ff));
2487                 ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2488         }
2489
2490         /* rewrite filter_fid */
2491         ff->ff_parent.f_ver = stripe_idx;
2492         ff->ff_layout.ol_stripe_size = comp->llc_stripe_size;
2493         ff->ff_layout.ol_stripe_count = comp->llc_stripe_count;
2494         ff->ff_layout.ol_comp_id = comp->llc_id;
2495         ff->ff_layout.ol_comp_start = comp->llc_extent.e_start;
2496         ff->ff_layout.ol_comp_end = comp->llc_extent.e_end;
2497         filter_fid_cpu_to_le(ff, ff, sizeof(*ff));
2498
2499         if (data->locd_declare)
2500                 rc = lod_sub_declare_xattr_set(env, dt, buf, XATTR_NAME_FID,
2501                                                LU_XATTR_REPLACE, th);
2502         else
2503                 rc = lod_sub_xattr_set(env, dt, buf, XATTR_NAME_FID,
2504                                        LU_XATTR_REPLACE, th);
2505
2506         return rc;
2507 }
2508
2509 /**
2510  * Reset parent FID on OST object
2511  *
2512  * Replace parent FID with @dt object FID, which is only called during migration
2513  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2514  * the FID is changed.
2515  *
2516  * \param[in] env execution environment
2517  * \param[in] dt dt_object whose stripes's parent FID will be reset
2518  * \parem[in] th thandle
2519  * \param[in] declare if it is declare
2520  *
2521  * \retval      0 if reset succeeds
2522  * \retval      negative errno if reset fails
2523  */
2524 static int lod_replace_parent_fid(const struct lu_env *env,
2525                                   struct dt_object *dt,
2526                                   const struct lu_buf *buf,
2527                                   struct thandle *th, bool declare)
2528 {
2529         struct lod_object *lo = lod_dt_obj(dt);
2530         struct lod_thread_info  *info = lod_env_info(env);
2531         struct filter_fid *ff;
2532         struct lod_obj_stripe_cb_data data = { { 0 } };
2533         int rc;
2534         ENTRY;
2535
2536         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2537
2538         /* set xattr to each stripes, if needed */
2539         rc = lod_striping_load(env, lo);
2540         if (rc != 0)
2541                 RETURN(rc);
2542
2543         if (!lod_obj_is_striped(dt))
2544                 RETURN(0);
2545
2546         if (info->lti_ea_store_size < sizeof(*ff)) {
2547                 rc = lod_ea_store_resize(info, sizeof(*ff));
2548                 if (rc != 0)
2549                         RETURN(rc);
2550         }
2551
2552         data.locd_declare = declare;
2553         data.locd_stripe_cb = lod_obj_stripe_replace_parent_fid_cb;
2554         data.locd_buf = buf;
2555         rc = lod_obj_for_each_stripe(env, lo, th, &data);
2556
2557         RETURN(rc);
2558 }
2559
2560 inline __u16 lod_comp_entry_stripe_count(struct lod_object *lo,
2561                                          struct lod_layout_component *entry,
2562                                          bool is_dir)
2563 {
2564         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2565
2566         if (is_dir)
2567                 return  0;
2568         else if (lod_comp_inited(entry))
2569                 return entry->llc_stripe_count;
2570         else if ((__u16)-1 == entry->llc_stripe_count)
2571                 return lod->lod_desc.ld_tgt_count;
2572         else
2573                 return lod_get_stripe_count(lod, lo, entry->llc_stripe_count);
2574 }
2575
2576 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2577 {
2578         int magic, size = 0, i;
2579         struct lod_layout_component *comp_entries;
2580         __u16 comp_cnt;
2581         bool is_composite, is_foreign = false;
2582
2583         if (is_dir) {
2584                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2585                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2586                 is_composite =
2587                         lo->ldo_def_striping->lds_def_striping_is_composite;
2588         } else {
2589                 comp_cnt = lo->ldo_comp_cnt;
2590                 comp_entries = lo->ldo_comp_entries;
2591                 is_composite = lo->ldo_is_composite;
2592                 is_foreign = lo->ldo_is_foreign;
2593         }
2594
2595         if (is_foreign)
2596                 return lo->ldo_foreign_lov_size;
2597
2598         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2599         if (is_composite) {
2600                 size = sizeof(struct lov_comp_md_v1) +
2601                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2602                 LASSERT(size % sizeof(__u64) == 0);
2603         }
2604
2605         for (i = 0; i < comp_cnt; i++) {
2606                 __u16 stripe_count;
2607
2608                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2609                 stripe_count = lod_comp_entry_stripe_count(lo, &comp_entries[i],
2610                                                            is_dir);
2611                 if (!is_dir && is_composite)
2612                         lod_comp_shrink_stripe_count(&comp_entries[i],
2613                                                      &stripe_count);
2614
2615                 size += lov_user_md_size(stripe_count, magic);
2616                 LASSERT(size % sizeof(__u64) == 0);
2617         }
2618         return size;
2619 }
2620
2621 /**
2622  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2623  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2624  * to be added.
2625   *
2626  * \param[in] env       execution environment
2627  * \param[in] dt        dt_object to add components on
2628  * \param[in] buf       buffer contains components to be added
2629  * \parem[in] th        thandle
2630  *
2631  * \retval      0 on success
2632  * \retval      negative errno on failure
2633  */
2634 static int lod_declare_layout_add(const struct lu_env *env,
2635                                   struct dt_object *dt,
2636                                   const struct lu_buf *buf,
2637                                   struct thandle *th)
2638 {
2639         struct lod_thread_info  *info = lod_env_info(env);
2640         struct lod_layout_component *comp_array, *lod_comp, *old_array;
2641         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2642         struct dt_object *next = dt_object_child(dt);
2643         struct lov_desc         *desc = &d->lod_desc;
2644         struct lod_object       *lo = lod_dt_obj(dt);
2645         struct lov_user_md_v3   *v3;
2646         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2647         __u32   magic;
2648         int     i, rc, array_cnt, old_array_cnt;
2649         ENTRY;
2650
2651         LASSERT(lo->ldo_is_composite);
2652
2653         if (lo->ldo_flr_state != LCM_FL_NONE)
2654                 RETURN(-EBUSY);
2655
2656         rc = lod_verify_striping(d, lo, buf, false);
2657         if (rc != 0)
2658                 RETURN(rc);
2659
2660         magic = comp_v1->lcm_magic;
2661         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2662                 lustre_swab_lov_comp_md_v1(comp_v1);
2663                 magic = comp_v1->lcm_magic;
2664         }
2665
2666         if (magic != LOV_USER_MAGIC_COMP_V1)
2667                 RETURN(-EINVAL);
2668
2669         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2670         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2671         if (comp_array == NULL)
2672                 RETURN(-ENOMEM);
2673
2674         memcpy(comp_array, lo->ldo_comp_entries,
2675                sizeof(*comp_array) * lo->ldo_comp_cnt);
2676
2677         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2678                 struct lov_user_md_v1 *v1;
2679                 struct lu_extent *ext;
2680
2681                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2682                                 comp_v1->lcm_entries[i].lcme_offset);
2683                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2684
2685                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2686                 lod_comp->llc_extent.e_start = ext->e_start;
2687                 lod_comp->llc_extent.e_end = ext->e_end;
2688                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2689                 lod_comp->llc_flags = comp_v1->lcm_entries[i].lcme_flags;
2690
2691                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
2692                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2693                 lod_adjust_stripe_info(lod_comp, desc);
2694
2695                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2696                         v3 = (struct lov_user_md_v3 *) v1;
2697                         if (v3->lmm_pool_name[0] != '\0') {
2698                                 rc = lod_set_pool(&lod_comp->llc_pool,
2699                                                   v3->lmm_pool_name);
2700                                 if (rc)
2701                                         GOTO(error, rc);
2702                         }
2703                 }
2704         }
2705
2706         old_array = lo->ldo_comp_entries;
2707         old_array_cnt = lo->ldo_comp_cnt;
2708
2709         lo->ldo_comp_entries = comp_array;
2710         lo->ldo_comp_cnt = array_cnt;
2711
2712         /* No need to increase layout generation here, it will be increased
2713          * later when generating component ID for the new components */
2714
2715         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2716         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2717                                               XATTR_NAME_LOV, 0, th);
2718         if (rc) {
2719                 lo->ldo_comp_entries = old_array;
2720                 lo->ldo_comp_cnt = old_array_cnt;
2721                 GOTO(error, rc);
2722         }
2723
2724         OBD_FREE(old_array, sizeof(*lod_comp) * old_array_cnt);
2725
2726         LASSERT(lo->ldo_mirror_count == 1);
2727         lo->ldo_mirrors[0].lme_end = array_cnt - 1;
2728
2729         RETURN(0);
2730
2731 error:
2732         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2733                 lod_comp = &comp_array[i];
2734                 if (lod_comp->llc_pool != NULL) {
2735                         OBD_FREE(lod_comp->llc_pool,
2736                                  strlen(lod_comp->llc_pool) + 1);
2737                         lod_comp->llc_pool = NULL;
2738                 }
2739         }
2740         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2741         RETURN(rc);
2742 }
2743
2744 /**
2745  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2746  * the '$field' can only be 'flags' now. The xattr value is binary
2747  * lov_comp_md_v1 which contains the component ID(s) and the value of
2748  * the field to be modified.
2749  *
2750  * \param[in] env       execution environment
2751  * \param[in] dt        dt_object to be modified
2752  * \param[in] op        operation string, like "set.flags"
2753  * \param[in] buf       buffer contains components to be set
2754  * \parem[in] th        thandle
2755  *
2756  * \retval      0 on success
2757  * \retval      negative errno on failure
2758  */
2759 static int lod_declare_layout_set(const struct lu_env *env,
2760                                   struct dt_object *dt,
2761                                   char *op, const struct lu_buf *buf,
2762                                   struct thandle *th)
2763 {
2764         struct lod_layout_component     *lod_comp;
2765         struct lod_thread_info  *info = lod_env_info(env);
2766         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2767         struct lod_object       *lo = lod_dt_obj(dt);
2768         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2769         __u32   magic;
2770         int     i, j, rc;
2771         bool    changed = false;
2772         ENTRY;
2773
2774         if (strcmp(op, "set.flags") != 0) {
2775                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2776                        lod2obd(d)->obd_name, op);
2777                 RETURN(-ENOTSUPP);
2778         }
2779
2780         magic = comp_v1->lcm_magic;
2781         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2782                 lustre_swab_lov_comp_md_v1(comp_v1);
2783                 magic = comp_v1->lcm_magic;
2784         }
2785
2786         if (magic != LOV_USER_MAGIC_COMP_V1)
2787                 RETURN(-EINVAL);
2788
2789         if (comp_v1->lcm_entry_count == 0) {
2790                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2791                        lod2obd(d)->obd_name);
2792                 RETURN(-EINVAL);
2793         }
2794
2795         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2796                 __u32 id = comp_v1->lcm_entries[i].lcme_id;
2797                 __u32 flags = comp_v1->lcm_entries[i].lcme_flags;
2798                 __u32 mirror_flag = flags & LCME_MIRROR_FLAGS;
2799                 bool neg = flags & LCME_FL_NEG;
2800
2801                 if (flags & LCME_FL_INIT) {
2802                         if (changed)
2803                                 lod_striping_free(env, lo);
2804                         RETURN(-EINVAL);
2805                 }
2806
2807                 flags &= ~(LCME_MIRROR_FLAGS | LCME_FL_NEG);
2808                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2809                         lod_comp = &lo->ldo_comp_entries[j];
2810
2811                         /* lfs only put one flag in each entry */
2812                         if ((flags && id != lod_comp->llc_id) ||
2813                             (mirror_flag && mirror_id_of(id) !=
2814                                             mirror_id_of(lod_comp->llc_id)))
2815                                 continue;
2816
2817                         if (neg) {
2818                                 if (flags)
2819                                         lod_comp->llc_flags &= ~flags;
2820                                 if (mirror_flag)
2821                                         lod_comp->llc_flags &= ~mirror_flag;
2822                         } else {
2823                                 if (flags)
2824                                         lod_comp->llc_flags |= flags;
2825                                 if (mirror_flag) {
2826                                         lod_comp->llc_flags |= mirror_flag;
2827                                         if (mirror_flag & LCME_FL_NOSYNC)
2828                                                 lod_comp->llc_timestamp =
2829                                                        ktime_get_real_seconds();
2830                                 }
2831                         }
2832                         changed = true;
2833                 }
2834         }
2835
2836         if (!changed) {
2837                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2838                        lod2obd(d)->obd_name);
2839                 RETURN(-EINVAL);
2840         }
2841
2842         lod_obj_inc_layout_gen(lo);
2843
2844         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2845         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), &info->lti_buf,
2846                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
2847         RETURN(rc);
2848 }
2849
2850 /**
2851  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2852  * and the xattr value is a unique component ID or a special lcme_id.
2853  *
2854  * \param[in] env       execution environment
2855  * \param[in] dt        dt_object to be operated on
2856  * \param[in] buf       buffer contains component ID or lcme_id
2857  * \parem[in] th        thandle
2858  *
2859  * \retval      0 on success
2860  * \retval      negative errno on failure
2861  */
2862 static int lod_declare_layout_del(const struct lu_env *env,
2863                                   struct dt_object *dt,
2864                                   const struct lu_buf *buf,
2865                                   struct thandle *th)
2866 {
2867         struct lod_thread_info  *info = lod_env_info(env);
2868         struct dt_object *next = dt_object_child(dt);
2869         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2870         struct lod_object *lo = lod_dt_obj(dt);
2871         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
2872         struct lov_comp_md_v1 *comp_v1 = buf->lb_buf;
2873         __u32 magic, id, flags, neg_flags = 0;
2874         int rc, i, j, left;
2875         ENTRY;
2876
2877         LASSERT(lo->ldo_is_composite);
2878
2879         if (lo->ldo_flr_state != LCM_FL_NONE)
2880                 RETURN(-EBUSY);
2881
2882         magic = comp_v1->lcm_magic;
2883         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2884                 lustre_swab_lov_comp_md_v1(comp_v1);
2885                 magic = comp_v1->lcm_magic;
2886         }
2887
2888         if (magic != LOV_USER_MAGIC_COMP_V1)
2889                 RETURN(-EINVAL);
2890
2891         id = comp_v1->lcm_entries[0].lcme_id;
2892         flags = comp_v1->lcm_entries[0].lcme_flags;
2893
2894         if (id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2895                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x, flags %#x\n",
2896                        lod2obd(d)->obd_name, id, flags);
2897                 RETURN(-EINVAL);
2898         }
2899
2900         if (id != LCME_ID_INVAL && flags != 0) {
2901                 CDEBUG(D_LAYOUT, "%s: specified both id and flags.\n",
2902                        lod2obd(d)->obd_name);
2903                 RETURN(-EINVAL);
2904         }
2905
2906         if (id == LCME_ID_INVAL && !flags) {
2907                 CDEBUG(D_LAYOUT, "%s: no id or flags specified.\n",
2908                        lod2obd(d)->obd_name);
2909                 RETURN(-EINVAL);
2910         }
2911
2912         if (flags & LCME_FL_NEG) {
2913                 neg_flags = flags & ~LCME_FL_NEG;
2914                 flags = 0;
2915         }
2916
2917         left = lo->ldo_comp_cnt;
2918         if (left <= 0)
2919                 RETURN(-EINVAL);
2920
2921         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2922                 struct lod_layout_component *lod_comp;
2923
2924                 lod_comp = &lo->ldo_comp_entries[i];
2925
2926                 if (id != LCME_ID_INVAL && id != lod_comp->llc_id)
2927                         continue;
2928                 else if (flags && !(flags & lod_comp->llc_flags))
2929                         continue;
2930                 else if (neg_flags && (neg_flags & lod_comp->llc_flags))
2931                         continue;
2932
2933                 if (left != (i + 1)) {
2934                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2935                                "a hole.\n", lod2obd(d)->obd_name);
2936                         RETURN(-EINVAL);
2937                 }
2938                 left--;
2939
2940                 /* Mark the component as deleted */
2941                 lod_comp->llc_id = LCME_ID_INVAL;
2942
2943                 /* Not instantiated component */
2944                 if (lod_comp->llc_stripe == NULL)
2945                         continue;
2946
2947                 LASSERT(lod_comp->llc_stripe_count > 0);
2948                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
2949                         struct dt_object *obj = lod_comp->llc_stripe[j];
2950
2951                         if (obj == NULL)
2952                                 continue;
2953                         rc = lod_sub_declare_destroy(env, obj, th);
2954                         if (rc)
2955                                 RETURN(rc);
2956                 }
2957         }
2958
2959         LASSERTF(left >= 0, "left = %d\n", left);
2960         if (left == lo->ldo_comp_cnt) {
2961                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2962                        lod2obd(d)->obd_name, id);
2963                 RETURN(-EINVAL);
2964         }
2965
2966         memset(attr, 0, sizeof(*attr));
2967         attr->la_valid = LA_SIZE;
2968         rc = lod_sub_declare_attr_set(env, next, attr, th);
2969         if (rc)
2970                 RETURN(rc);
2971
2972         if (left > 0) {
2973                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2974                 rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
2975                                                XATTR_NAME_LOV, 0, th);
2976         } else {
2977                 rc = lod_sub_declare_xattr_del(env, next, XATTR_NAME_LOV, th);
2978         }
2979
2980         RETURN(rc);
2981 }
2982
2983 /**
2984  * Declare layout add/set/del operations issued by special xattr names:
2985  *
2986  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2987  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2988  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2989  *
2990  * \param[in] env       execution environment
2991  * \param[in] dt        object
2992  * \param[in] name      name of xattr
2993  * \param[in] buf       lu_buf contains xattr value
2994  * \param[in] th        transaction handle
2995  *
2996  * \retval              0 on success
2997  * \retval              negative if failed
2998  */
2999 static int lod_declare_modify_layout(const struct lu_env *env,
3000                                      struct dt_object *dt,
3001                                      const char *name,
3002                                      const struct lu_buf *buf,
3003                                      struct thandle *th)
3004 {
3005         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
3006         struct lod_object *lo = lod_dt_obj(dt);
3007         char *op;
3008         int rc, len = strlen(XATTR_LUSTRE_LOV);
3009         ENTRY;
3010
3011         LASSERT(dt_object_exists(dt));
3012
3013         if (strlen(name) <= len || name[len] != '.') {
3014                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
3015                        lod2obd(d)->obd_name, name);
3016                 RETURN(-EINVAL);
3017         }
3018         len++;
3019
3020         rc = lod_striping_load(env, lo);
3021         if (rc)
3022                 GOTO(unlock, rc);
3023
3024         /* the layout to be modified must be a composite layout */
3025         if (!lo->ldo_is_composite) {
3026                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
3027                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
3028                 GOTO(unlock, rc = -EINVAL);
3029         }
3030
3031         op = (char *)name + len;
3032         if (strcmp(op, "add") == 0) {
3033                 rc = lod_declare_layout_add(env, dt, buf, th);
3034         } else if (strcmp(op, "del") == 0) {
3035                 rc = lod_declare_layout_del(env, dt, buf, th);
3036         } else if (strncmp(op, "set", strlen("set")) == 0) {
3037                 rc = lod_declare_layout_set(env, dt, op, buf, th);
3038         } else  {
3039                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
3040                        lod2obd(d)->obd_name, name);
3041                 GOTO(unlock, rc = -ENOTSUPP);
3042         }
3043 unlock:
3044         if (rc)
3045                 lod_striping_free(env, lo);
3046
3047         RETURN(rc);
3048 }
3049
3050 /**
3051  * Convert a plain file lov_mds_md to a composite layout.
3052  *
3053  * \param[in,out] info  the thread info::lti_ea_store buffer contains little
3054  *                      endian plain file layout
3055  *
3056  * \retval              0 on success, <0 on failure
3057  */
3058 static int lod_layout_convert(struct lod_thread_info *info)
3059 {
3060         struct lov_mds_md *lmm = info->lti_ea_store;
3061         struct lov_mds_md *lmm_save;
3062         struct lov_comp_md_v1 *lcm;
3063         struct lov_comp_md_entry_v1 *lcme;
3064         size_t size;
3065         __u32 blob_size;
3066         int rc = 0;
3067         ENTRY;
3068
3069         /* realloc buffer to a composite layout which contains one component */
3070         blob_size = lov_mds_md_size(le16_to_cpu(lmm->lmm_stripe_count),
3071                                     le32_to_cpu(lmm->lmm_magic));
3072         size = sizeof(*lcm) + sizeof(*lcme) + blob_size;
3073
3074         OBD_ALLOC_LARGE(lmm_save, blob_size);
3075         if (!lmm_save)
3076                 GOTO(out, rc = -ENOMEM);
3077
3078         memcpy(lmm_save, lmm, blob_size);
3079
3080         if (info->lti_ea_store_size < size) {
3081                 rc = lod_ea_store_resize(info, size);
3082                 if (rc)
3083                         GOTO(out, rc);
3084         }
3085
3086         lcm = info->lti_ea_store;
3087         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
3088         lcm->lcm_size = cpu_to_le32(size);
3089         lcm->lcm_layout_gen = cpu_to_le32(le16_to_cpu(
3090                                                 lmm_save->lmm_layout_gen));
3091         lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
3092         lcm->lcm_entry_count = cpu_to_le16(1);
3093         lcm->lcm_mirror_count = 0;
3094
3095         lcme = &lcm->lcm_entries[0];
3096         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
3097         lcme->lcme_extent.e_start = 0;
3098         lcme->lcme_extent.e_end = cpu_to_le64(OBD_OBJECT_EOF);
3099         lcme->lcme_offset = cpu_to_le32(sizeof(*lcm) + sizeof(*lcme));
3100         lcme->lcme_size = cpu_to_le32(blob_size);
3101
3102         memcpy((char *)lcm + lcme->lcme_offset, (char *)lmm_save, blob_size);
3103
3104         EXIT;
3105 out:
3106         if (lmm_save)
3107                 OBD_FREE_LARGE(lmm_save, blob_size);
3108         return rc;
3109 }
3110
3111 /**
3112  * Merge layouts to form a mirrored file.
3113  */
3114 static int lod_declare_layout_merge(const struct lu_env *env,
3115                 struct dt_object *dt, const struct lu_buf *mbuf,
3116                 struct thandle *th)
3117 {
3118         struct lod_thread_info  *info = lod_env_info(env);
3119         struct lu_buf           *buf = &info->lti_buf;
3120         struct lod_object       *lo = lod_dt_obj(dt);
3121         struct lov_comp_md_v1   *lcm;
3122         struct lov_comp_md_v1   *cur_lcm;
3123         struct lov_comp_md_v1   *merge_lcm;
3124         struct lov_comp_md_entry_v1     *lcme;
3125         size_t size = 0;
3126         size_t offset;
3127         __u16 cur_entry_count;
3128         __u16 merge_entry_count;
3129         __u32 id = 0;
3130         __u16 mirror_id = 0;
3131         __u32 mirror_count;
3132         int     rc, i;
3133         ENTRY;
3134
3135         merge_lcm = mbuf->lb_buf;
3136         if (mbuf->lb_len < sizeof(*merge_lcm))
3137                 RETURN(-EINVAL);
3138
3139         /* must be an existing layout from disk */
3140         if (le32_to_cpu(merge_lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
3141                 RETURN(-EINVAL);
3142
3143         merge_entry_count = le16_to_cpu(merge_lcm->lcm_entry_count);
3144
3145         /* do not allow to merge two mirrored files */
3146         if (le16_to_cpu(merge_lcm->lcm_mirror_count))
3147                 RETURN(-EBUSY);
3148
3149         /* verify the target buffer */
3150         rc = lod_get_lov_ea(env, lo);
3151         if (rc <= 0)
3152                 RETURN(rc ? : -ENODATA);
3153
3154         cur_lcm = info->lti_ea_store;
3155         switch (le32_to_cpu(cur_lcm->lcm_magic)) {
3156         case LOV_MAGIC_V1:
3157         case LOV_MAGIC_V3:
3158                 rc = lod_layout_convert(info);
3159                 break;
3160         case LOV_MAGIC_COMP_V1:
3161                 rc = 0;
3162                 break;
3163         default:
3164                 rc = -EINVAL;
3165         }
3166         if (rc)
3167                 RETURN(rc);
3168
3169         /* info->lti_ea_store could be reallocated in lod_layout_convert() */
3170         cur_lcm = info->lti_ea_store;
3171         cur_entry_count = le16_to_cpu(cur_lcm->lcm_entry_count);
3172
3173         /* 'lcm_mirror_count + 1' is the current # of mirrors the file has */
3174         mirror_count = le16_to_cpu(cur_lcm->lcm_mirror_count) + 1;
3175         if (mirror_count + 1 > LUSTRE_MIRROR_COUNT_MAX)
3176                 RETURN(-ERANGE);
3177
3178         /* size of new layout */
3179         size = le32_to_cpu(cur_lcm->lcm_size) +
3180                le32_to_cpu(merge_lcm->lcm_size) - sizeof(*cur_lcm);
3181
3182         memset(buf, 0, sizeof(*buf));
3183         lu_buf_alloc(buf, size);
3184         if (buf->lb_buf == NULL)
3185                 RETURN(-ENOMEM);
3186
3187         lcm = buf->lb_buf;
3188         memcpy(lcm, cur_lcm, sizeof(*lcm) + cur_entry_count * sizeof(*lcme));
3189
3190         offset = sizeof(*lcm) +
3191                  sizeof(*lcme) * (cur_entry_count + merge_entry_count);
3192         for (i = 0; i < cur_entry_count; i++) {
3193                 struct lov_comp_md_entry_v1 *cur_lcme;
3194
3195                 lcme = &lcm->lcm_entries[i];
3196                 cur_lcme = &cur_lcm->lcm_entries[i];
3197
3198                 lcme->lcme_offset = cpu_to_le32(offset);
3199                 memcpy((char *)lcm + offset,
3200                        (char *)cur_lcm + le32_to_cpu(cur_lcme->lcme_offset),
3201                        le32_to_cpu(lcme->lcme_size));
3202
3203                 offset += le32_to_cpu(lcme->lcme_size);
3204
3205                 if (mirror_count == 1 &&
3206                     mirror_id_of(le32_to_cpu(lcme->lcme_id)) == 0) {
3207                         /* Add mirror from a non-flr file, create new mirror ID.
3208                          * Otherwise, keep existing mirror's component ID, used
3209                          * for mirror extension.
3210                          */
3211                         id = pflr_id(1, i + 1);
3212                         lcme->lcme_id = cpu_to_le32(id);
3213                 }
3214
3215                 id = MAX(le32_to_cpu(lcme->lcme_id), id);
3216         }
3217
3218         mirror_id = mirror_id_of(id) + 1;
3219         for (i = 0; i < merge_entry_count; i++) {
3220                 struct lov_comp_md_entry_v1 *merge_lcme;
3221
3222                 merge_lcme = &merge_lcm->lcm_entries[i];
3223                 lcme = &lcm->lcm_entries[cur_entry_count + i];
3224
3225                 *lcme = *merge_lcme;
3226                 lcme->lcme_offset = cpu_to_le32(offset);
3227
3228                 id = pflr_id(mirror_id, i + 1);
3229                 lcme->lcme_id = cpu_to_le32(id);
3230
3231                 memcpy((char *)lcm + offset,
3232                        (char *)merge_lcm + le32_to_cpu(merge_lcme->lcme_offset),
3233                        le32_to_cpu(lcme->lcme_size));
3234
3235                 offset += le32_to_cpu(lcme->lcme_size);
3236         }
3237
3238         /* fixup layout information */
3239         lod_obj_inc_layout_gen(lo);
3240         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3241         lcm->lcm_size = cpu_to_le32(size);
3242         lcm->lcm_entry_count = cpu_to_le16(cur_entry_count + merge_entry_count);
3243         lcm->lcm_mirror_count = cpu_to_le16(mirror_count);
3244         if ((le16_to_cpu(lcm->lcm_flags) & LCM_FL_FLR_MASK) == LCM_FL_NONE)
3245                 lcm->lcm_flags = cpu_to_le32(LCM_FL_RDONLY);
3246
3247         rc = lod_striping_reload(env, lo, buf);
3248         if (rc)
3249                 GOTO(out, rc);
3250
3251         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), buf,
3252                                         XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3253
3254 out:
3255         lu_buf_free(buf);
3256         RETURN(rc);
3257 }
3258
3259 /**
3260  * Split layouts, just set the LOVEA with the layout from mbuf.
3261  */
3262 static int lod_declare_layout_split(const struct lu_env *env,
3263                 struct dt_object *dt, const struct lu_buf *mbuf,
3264                 struct thandle *th)
3265 {
3266         struct lod_object *lo = lod_dt_obj(dt);
3267         struct lov_comp_md_v1 *lcm = mbuf->lb_buf;
3268         int rc;
3269         ENTRY;
3270
3271         lod_obj_inc_layout_gen(lo);
3272         lcm->lcm_layout_gen = cpu_to_le32(lo->ldo_layout_gen);
3273
3274         rc = lod_striping_reload(env, lo, mbuf);
3275         if (rc)
3276                 RETURN(rc);
3277
3278         rc = lod_sub_declare_xattr_set(env, dt_object_child(dt), mbuf,
3279                                        XATTR_NAME_LOV, LU_XATTR_REPLACE, th);
3280         RETURN(rc);
3281 }
3282
3283 /**
3284  * Implementation of dt_object_operations::do_declare_xattr_set.
3285  *
3286  * \see dt_object_operations::do_declare_xattr_set() in the API description
3287  * for details.
3288  *
3289  * the extension to the API:
3290  *   - declaring LOVEA requests striping creation
3291  *   - LU_XATTR_REPLACE means layout swap
3292  */
3293 static int lod_declare_xattr_set(const struct lu_env *env,
3294                                  struct dt_object *dt,
3295                                  const struct lu_buf *buf,
3296                                  const char *name, int fl,
3297                                  struct thandle *th)
3298 {
3299         struct dt_object *next = dt_object_child(dt);
3300         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
3301         __u32             mode;
3302         int               rc;
3303         ENTRY;
3304
3305         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
3306         if ((S_ISREG(mode) || mode == 0) &&
3307             !(fl & (LU_XATTR_REPLACE | LU_XATTR_MERGE | LU_XATTR_SPLIT)) &&
3308             (strcmp(name, XATTR_NAME_LOV) == 0 ||
3309              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
3310                 /*
3311                  * this is a request to create object's striping.
3312                  *
3313                  * allow to declare predefined striping on a new (!mode) object
3314                  * which is supposed to be replay of regular file creation
3315                  * (when LOV setting is declared)
3316                  *
3317                  * LU_XATTR_REPLACE is set to indicate a layout swap
3318                  */
3319                 if (dt_object_exists(dt)) {
3320                         rc = dt_attr_get(env, next, attr);
3321                         if (rc)
3322                                 RETURN(rc);
3323                 } else {
3324                         memset(attr, 0, sizeof(*attr));
3325                         attr->la_valid = LA_TYPE | LA_MODE;
3326                         attr->la_mode = S_IFREG;
3327                 }
3328                 rc = lod_declare_striped_create(env, dt, attr, buf, th);
3329         } else if (fl & LU_XATTR_MERGE) {
3330                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3331                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3332                 rc = lod_declare_layout_merge(env, dt, buf, th);
3333         } else if (fl & LU_XATTR_SPLIT) {
3334                 LASSERT(strcmp(name, XATTR_NAME_LOV) == 0 ||
3335                         strcmp(name, XATTR_LUSTRE_LOV) == 0);
3336                 rc = lod_declare_layout_split(env, dt, buf, th);
3337         } else if (S_ISREG(mode) &&
3338                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
3339                    strncmp(name, XATTR_LUSTRE_LOV,
3340                            strlen(XATTR_LUSTRE_LOV)) == 0) {
3341                 /*
3342                  * this is a request to modify object's striping.
3343                  * add/set/del component(s).
3344                  */
3345                 if (!dt_object_exists(dt))
3346                         RETURN(-ENOENT);
3347
3348                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
3349         } else if (strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
3350                    strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
3351                 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
3352
3353                 rc = -ENOTSUPP;
3354                 if (strcmp(op, "add") == 0)
3355                         rc = lod_dir_declare_layout_add(env, dt, buf, th);
3356                 else if (strcmp(op, "del") == 0)
3357                         rc = lod_dir_declare_layout_delete(env, dt, buf, th);
3358                 else if (strcmp(op, "set") == 0)
3359                         rc = lod_sub_declare_xattr_set(env, next, buf,
3360                                                        XATTR_NAME_LMV, fl, th);
3361
3362                 RETURN(rc);
3363         } else if (S_ISDIR(mode)) {
3364                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
3365         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3366                 rc = lod_replace_parent_fid(env, dt, buf, th, true);
3367         } else {
3368                 rc = lod_sub_declare_xattr_set(env, next, buf, name, fl, th);
3369         }
3370
3371         RETURN(rc);
3372 }
3373
3374 /**
3375  * Apply xattr changes to the object.
3376  *
3377  * Applies xattr changes to the object and the stripes if the latter exist.
3378  *
3379  * \param[in] env       execution environment
3380  * \param[in] dt        object
3381  * \param[in] buf       buffer pointing to the new value of xattr
3382  * \param[in] name      name of xattr
3383  * \param[in] fl        flags
3384  * \param[in] th        transaction handle
3385  *
3386  * \retval              0 on success
3387  * \retval              negative if failed
3388  */
3389 static int lod_xattr_set_internal(const struct lu_env *env,
3390                                   struct dt_object *dt,
3391                                   const struct lu_buf *buf,
3392                                   const char *name, int fl,
3393                                   struct thandle *th)
3394 {
3395         struct dt_object        *next = dt_object_child(dt);
3396         struct lod_object       *lo = lod_dt_obj(dt);
3397         int                     rc;
3398         int                     i;
3399         ENTRY;
3400
3401         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
3402         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3403                 RETURN(rc);
3404
3405         /* Note: Do not set LinkEA on sub-stripes, otherwise
3406          * it will confuse the fid2path process(see mdt_path_current()).
3407          * The linkEA between master and sub-stripes is set in
3408          * lod_xattr_set_lmv(). */
3409         if (lo->ldo_dir_stripe_count == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
3410                 RETURN(0);
3411
3412         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3413                 LASSERT(lo->ldo_stripe[i]);
3414
3415                 rc = lod_sub_xattr_set(env, lo->ldo_stripe[i], buf, name,
3416                                        fl, th);
3417                 if (rc != 0)
3418                         break;
3419         }
3420
3421         RETURN(rc);
3422 }
3423
3424 /**
3425  * Delete an extended attribute.
3426  *
3427  * Deletes specified xattr from the object and the stripes if the latter exist.
3428  *
3429  * \param[in] env       execution environment
3430  * \param[in] dt        object
3431  * \param[in] name      name of xattr
3432  * \param[in] th        transaction handle
3433  *
3434  * \retval              0 on success
3435  * \retval              negative if failed
3436  */
3437 static int lod_xattr_del_internal(const struct lu_env *env,
3438                                   struct dt_object *dt,
3439                                   const char *name, struct thandle *th)
3440 {
3441         struct dt_object        *next = dt_object_child(dt);
3442         struct lod_object       *lo = lod_dt_obj(dt);
3443         int                     rc;
3444         int                     i;
3445         ENTRY;
3446
3447         rc = lod_sub_xattr_del(env, next, name, th);
3448         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3449                 RETURN(rc);
3450
3451         if (lo->ldo_dir_stripe_count == 0)
3452                 RETURN(rc);
3453
3454         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3455                 LASSERT(lo->ldo_stripe[i]);
3456
3457                 rc = lod_sub_xattr_del(env, lo->ldo_stripe[i], name, th);
3458                 if (rc != 0)
3459                         break;
3460         }
3461
3462         RETURN(rc);
3463 }
3464
3465 /**
3466  * Set default striping on a directory.
3467  *
3468  * Sets specified striping on a directory object unless it matches the default
3469  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3470  * EA. This striping will be used when regular file is being created in this
3471  * directory.
3472  *
3473  * \param[in] env       execution environment
3474  * \param[in] dt        the striped object
3475  * \param[in] buf       buffer with the striping
3476  * \param[in] name      name of EA
3477  * \param[in] fl        xattr flag (see OSD API description)
3478  * \param[in] th        transaction handle
3479  *
3480  * \retval              0 on success
3481  * \retval              negative if failed
3482  */
3483 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
3484                                     struct dt_object *dt,
3485                                     const struct lu_buf *buf,
3486                                     const char *name, int fl,
3487                                     struct thandle *th)
3488 {
3489         struct lov_user_md_v1   *lum;
3490         struct lov_user_md_v3   *v3 = NULL;
3491         const char              *pool_name = NULL;
3492         int                      rc;
3493         bool                     is_del;
3494         ENTRY;
3495
3496         LASSERT(buf != NULL && buf->lb_buf != NULL);
3497         lum = buf->lb_buf;
3498
3499         switch (lum->lmm_magic) {
3500         case LOV_USER_MAGIC_SPECIFIC:
3501         case LOV_USER_MAGIC_V3:
3502                 v3 = buf->lb_buf;
3503                 if (v3->lmm_pool_name[0] != '\0')
3504                         pool_name = v3->lmm_pool_name;
3505                 /* fall through */
3506         case LOV_USER_MAGIC_V1:
3507                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
3508                  * (i.e. all default values specified) then delete default
3509                  * striping from dir. */
3510                 CDEBUG(D_LAYOUT,
3511                        "set default striping: sz %u # %u offset %d %s %s\n",
3512                        (unsigned)lum->lmm_stripe_size,
3513                        (unsigned)lum->lmm_stripe_count,
3514                        (int)lum->lmm_stripe_offset,
3515                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
3516
3517                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
3518                                              lum->lmm_stripe_count,
3519                                              lum->lmm_stripe_offset,
3520                                              pool_name);
3521                 break;
3522         case LOV_USER_MAGIC_COMP_V1:
3523                 is_del = false;
3524                 break;
3525         default:
3526                 CERROR("Invalid magic %x\n", lum->lmm_magic);
3527                 RETURN(-EINVAL);
3528         }
3529
3530         if (is_del) {
3531                 rc = lod_xattr_del_internal(env, dt, name, th);
3532                 if (rc == -ENODATA)
3533                         rc = 0;
3534         } else {
3535                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3536         }
3537
3538         RETURN(rc);
3539 }
3540
3541 /**
3542  * Set default striping on a directory object.
3543  *
3544  * Sets specified striping on a directory object unless it matches the default
3545  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
3546  * EA. This striping will be used when a new directory is being created in the
3547  * directory.
3548  *
3549  * \param[in] env       execution environment
3550  * \param[in] dt        the striped object
3551  * \param[in] buf       buffer with the striping
3552  * \param[in] name      name of EA
3553  * \param[in] fl        xattr flag (see OSD API description)
3554  * \param[in] th        transaction handle
3555  *
3556  * \retval              0 on success
3557  * \retval              negative if failed
3558  */
3559 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
3560                                             struct dt_object *dt,
3561                                             const struct lu_buf *buf,
3562                                             const char *name, int fl,
3563                                             struct thandle *th)
3564 {
3565         struct lmv_user_md_v1   *lum;
3566         int                      rc;
3567         ENTRY;
3568
3569         LASSERT(buf != NULL && buf->lb_buf != NULL);
3570         lum = buf->lb_buf;
3571
3572         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
3573               le32_to_cpu(lum->lum_stripe_count),
3574               (int)le32_to_cpu(lum->lum_stripe_offset));
3575
3576         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
3577                                  le32_to_cpu(lum->lum_stripe_offset)) &&
3578                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
3579                 rc = lod_xattr_del_internal(env, dt, name, th);
3580                 if (rc == -ENODATA)
3581                         rc = 0;
3582         } else {
3583                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3584                 if (rc != 0)
3585                         RETURN(rc);
3586         }
3587
3588         RETURN(rc);
3589 }
3590
3591 /**
3592  * Turn directory into a striped directory.
3593  *
3594  * During replay the client sends the striping created before MDT
3595  * failure, then the layer above LOD sends this defined striping
3596  * using ->do_xattr_set(), so LOD uses this method to replay creation
3597  * of the stripes. Notice the original information for the striping
3598  * (#stripes, FIDs, etc) was transferred in declare path.
3599  *
3600  * \param[in] env       execution environment
3601  * \param[in] dt        the striped object
3602  * \param[in] buf       not used currently
3603  * \param[in] name      not used currently
3604  * \param[in] fl        xattr flag (see OSD API description)
3605  * \param[in] th        transaction handle
3606  *
3607  * \retval              0 on success
3608  * \retval              negative if failed
3609  */
3610 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
3611                              const struct lu_buf *buf, const char *name,
3612                              int fl, struct thandle *th)
3613 {
3614         struct lod_object       *lo = lod_dt_obj(dt);
3615         struct lod_thread_info  *info = lod_env_info(env);
3616         struct lu_attr          *attr = &info->lti_attr;
3617         struct dt_object_format *dof = &info->lti_format;
3618         struct lu_buf           lmv_buf;
3619         struct lu_buf           slave_lmv_buf;
3620         struct lmv_mds_md_v1    *lmm;
3621         struct lmv_mds_md_v1    *slave_lmm = NULL;
3622         struct dt_insert_rec    *rec = &info->lti_dt_rec;
3623         int                     i;
3624         int                     rc;
3625         ENTRY;
3626
3627         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3628                 RETURN(-ENOTDIR);
3629
3630         /* The stripes are supposed to be allocated in declare phase,
3631          * if there are no stripes being allocated, it will skip */
3632         if (lo->ldo_dir_stripe_count == 0)
3633                 RETURN(0);
3634
3635         rc = dt_attr_get(env, dt_object_child(dt), attr);
3636         if (rc != 0)
3637                 RETURN(rc);
3638
3639         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
3640                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
3641         dof->dof_type = DFT_DIR;
3642
3643         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
3644         if (rc != 0)
3645                 RETURN(rc);
3646         lmm = lmv_buf.lb_buf;
3647
3648         OBD_ALLOC_PTR(slave_lmm);
3649         if (slave_lmm == NULL)
3650                 RETURN(-ENOMEM);
3651
3652         lod_prep_slave_lmv_md(slave_lmm, lmm);
3653         slave_lmv_buf.lb_buf = slave_lmm;
3654         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
3655
3656         rec->rec_type = S_IFDIR;
3657         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
3658                 struct dt_object *dto = lo->ldo_stripe[i];
3659                 char *stripe_name = info->lti_key;
3660                 struct lu_name *sname;
3661                 struct linkea_data ldata = { NULL };
3662                 struct lu_buf linkea_buf;
3663
3664                 /* if it's source stripe of migrating directory, don't create */
3665                 if (!((lo->ldo_dir_hash_type & LMV_HASH_FLAG_MIGRATION) &&
3666                       i >= lo->ldo_dir_migrate_offset)) {
3667                         dt_write_lock(env, dto, MOR_TGT_CHILD);
3668                         rc = lod_sub_create(env, dto, attr, NULL, dof, th);
3669                         if (rc != 0) {
3670                                 dt_write_unlock(env, dto);
3671                                 GOTO(out, rc);
3672                         }
3673
3674                         rc = lod_sub_ref_add(env, dto, th);
3675                         dt_write_unlock(env, dto);
3676                         if (rc != 0)
3677                                 GOTO(out, rc);
3678
3679                         rec->rec_fid = lu_object_fid(&dto->do_lu);
3680                         rc = lod_sub_insert(env, dto,
3681                                             (const struct dt_rec *)rec,
3682                                             (const struct dt_key *)dot, th);
3683                         if (rc != 0)
3684                                 GOTO(out, rc);
3685                 }
3686
3687                 rec->rec_fid = lu_object_fid(&dt->do_lu);
3688                 rc = lod_sub_insert(env, dto, (struct dt_rec *)rec,
3689                                     (const struct dt_key *)dotdot, th);
3690                 if (rc != 0)
3691                         GOTO(out, rc);
3692
3693                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
3694                     cfs_fail_val != i) {
3695                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
3696                             cfs_fail_val == i)
3697                                 slave_lmm->lmv_master_mdt_index =
3698                                                         cpu_to_le32(i + 1);
3699                         else
3700                                 slave_lmm->lmv_master_mdt_index =
3701                                                         cpu_to_le32(i);
3702
3703                         rc = lod_sub_xattr_set(env, dto, &slave_lmv_buf,
3704                                                XATTR_NAME_LMV, fl, th);
3705                         if (rc != 0)
3706                                 GOTO(out, rc);
3707                 }
3708
3709                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
3710                     cfs_fail_val == i)
3711                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3712                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
3713                 else
3714                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3715                                  PFID(lu_object_fid(&dto->do_lu)), i);
3716
3717                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
3718                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3719                                       sname, lu_object_fid(&dt->do_lu));
3720                 if (rc != 0)
3721                         GOTO(out, rc);
3722
3723                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3724                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3725                 rc = lod_sub_xattr_set(env, dto, &linkea_buf,
3726                                        XATTR_NAME_LINK, 0, th);
3727                 if (rc != 0)
3728                         GOTO(out, rc);
3729
3730                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3731                 rc = lod_sub_insert(env, dt_object_child(dt),
3732                                     (const struct dt_rec *)rec,
3733                                     (const struct dt_key *)stripe_name, th);
3734                 if (rc != 0)
3735                         GOTO(out, rc);
3736
3737                 rc = lod_sub_ref_add(env, dt_object_child(dt), th);
3738                 if (rc != 0)
3739                         GOTO(out, rc);
3740         }
3741
3742         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3743                 rc = lod_sub_xattr_set(env, dt_object_child(dt),
3744                                        &lmv_buf, XATTR_NAME_LMV, fl, th);
3745 out:
3746         if (slave_lmm != NULL)
3747                 OBD_FREE_PTR(slave_lmm);
3748
3749         RETURN(rc);
3750 }
3751
3752 /**
3753  * Helper function to declare/execute creation of a striped directory
3754  *
3755  * Called in declare/create object path, prepare striping for a directory
3756  * and prepare defaults data striping for the objects to be created in
3757  * that directory. Notice the function calls "declaration" or "execution"
3758  * methods depending on \a declare param. This is a consequence of the
3759  * current approach while we don't have natural distributed transactions:
3760  * we basically execute non-local updates in the declare phase. So, the
3761  * arguments for the both phases are the same and this is the reason for
3762  * this function to exist.
3763  *
3764  * \param[in] env       execution environment
3765  * \param[in] dt        object
3766  * \param[in] attr      attributes the stripes will be created with
3767  * \param[in] lmu       lmv_user_md if MDT indices are specified
3768  * \param[in] dof       format of stripes (see OSD API description)
3769  * \param[in] th        transaction handle
3770  * \param[in] declare   where to call "declare" or "execute" methods
3771  *
3772  * \retval              0 on success
3773  * \retval              negative if failed
3774  */
3775 static int lod_dir_striping_create_internal(const struct lu_env *env,
3776                                             struct dt_object *dt,
3777                                             struct lu_attr *attr,
3778                                             const struct lu_buf *lmu,
3779                                             struct dt_object_format *dof,
3780                                             struct thandle *th,
3781                                             bool declare)
3782 {
3783         struct lod_thread_info *info = lod_env_info(env);
3784         struct lod_object *lo = lod_dt_obj(dt);
3785         const struct lod_default_striping *lds = lo->ldo_def_striping;
3786         int rc;
3787         ENTRY;
3788
3789         LASSERT(ergo(lds != NULL,
3790                      lds->lds_def_striping_set ||
3791                      lds->lds_dir_def_striping_set));
3792
3793         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripe_count,
3794                                  lo->ldo_dir_stripe_offset)) {
3795                 if (!lmu) {
3796                         struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3797                         int stripe_count = lo->ldo_dir_stripe_count;
3798
3799                         if (info->lti_ea_store_size < sizeof(*v1)) {
3800                                 rc = lod_ea_store_resize(info, sizeof(*v1));
3801                                 if (rc != 0)
3802                                         RETURN(rc);
3803                                 v1 = info->lti_ea_store;
3804                         }
3805
3806                         memset(v1, 0, sizeof(*v1));
3807                         v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3808                         v1->lum_stripe_count = cpu_to_le32(stripe_count);
3809                         v1->lum_stripe_offset =
3810                                         cpu_to_le32(lo->ldo_dir_stripe_offset);
3811
3812                         info->lti_buf.lb_buf = v1;
3813                         info->lti_buf.lb_len = sizeof(*v1);
3814                         lmu = &info->lti_buf;
3815                 }
3816
3817                 if (declare)
3818                         rc = lod_declare_xattr_set_lmv(env, dt, attr, lmu, dof,
3819                                                        th);
3820                 else
3821                         rc = lod_xattr_set_lmv(env, dt, lmu, XATTR_NAME_LMV, 0,
3822                                                th);
3823                 if (rc != 0)
3824                         RETURN(rc);
3825         }
3826
3827         /* Transfer default LMV striping from the parent */
3828         if (lds != NULL && lds->lds_dir_def_striping_set &&
3829             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripe_count,
3830                                  lds->lds_dir_def_stripe_offset)) {
3831                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3832
3833                 if (info->lti_ea_store_size < sizeof(*v1)) {
3834                         rc = lod_ea_store_resize(info, sizeof(*v1));
3835                         if (rc != 0)
3836                                 RETURN(rc);
3837                         v1 = info->lti_ea_store;
3838                 }
3839
3840                 memset(v1, 0, sizeof(*v1));
3841                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3842                 v1->lum_stripe_count =
3843                         cpu_to_le32(lds->lds_dir_def_stripe_count);
3844                 v1->lum_stripe_offset =
3845                         cpu_to_le32(lds->lds_dir_def_stripe_offset);
3846                 v1->lum_hash_type =
3847                         cpu_to_le32(lds->lds_dir_def_hash_type);
3848
3849                 info->lti_buf.lb_buf = v1;
3850                 info->lti_buf.lb_len = sizeof(*v1);
3851                 if (declare)
3852                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3853                                                        XATTR_NAME_DEFAULT_LMV,
3854                                                        0, th);
3855                 else
3856                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3857                                                   &info->lti_buf,
3858                                                   XATTR_NAME_DEFAULT_LMV, 0,
3859                                                   th);
3860                 if (rc != 0)
3861                         RETURN(rc);
3862         }
3863
3864         /* Transfer default LOV striping from the parent */
3865         if (lds != NULL && lds->lds_def_striping_set &&
3866             lds->lds_def_comp_cnt != 0) {
3867                 struct lov_mds_md *lmm;
3868                 int lmm_size = lod_comp_md_size(lo, true);
3869
3870                 if (info->lti_ea_store_size < lmm_size) {
3871                         rc = lod_ea_store_resize(info, lmm_size);
3872                         if (rc != 0)
3873                                 RETURN(rc);
3874                 }
3875                 lmm = info->lti_ea_store;
3876
3877                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3878                 if (rc != 0)
3879                         RETURN(rc);
3880
3881                 info->lti_buf.lb_buf = lmm;
3882                 info->lti_buf.lb_len = lmm_size;
3883
3884                 if (declare)
3885                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3886                                                        XATTR_NAME_LOV, 0, th);
3887                 else
3888                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3889                                                       XATTR_NAME_LOV, 0, th);
3890                 if (rc != 0)
3891                         RETURN(rc);
3892         }
3893
3894         RETURN(0);
3895 }
3896
3897 static int lod_declare_dir_striping_create(const struct lu_env *env,
3898                                            struct dt_object *dt,
3899                                            struct lu_attr *attr,
3900                                            struct lu_buf *lmu,
3901                                            struct dt_object_format *dof,
3902                                            struct thandle *th)
3903 {
3904         return lod_dir_striping_create_internal(env, dt, attr, lmu, dof, th,
3905                                                 true);
3906 }
3907
3908 static int lod_dir_striping_create(const struct lu_env *env,
3909                                    struct dt_object *dt,
3910                                    struct lu_attr *attr,
3911                                    struct dt_object_format *dof,
3912                                    struct thandle *th)
3913 {
3914         return lod_dir_striping_create_internal(env, dt, attr, NULL, dof, th,
3915                                                 false);
3916 }
3917
3918 /**
3919  * Make LOV EA for striped object.
3920  *
3921  * Generate striping information and store it in the LOV EA of the given
3922  * object. The caller must ensure nobody else is calling the function
3923  * against the object concurrently. The transaction must be started.
3924  * FLDB service must be running as well; it's used to map FID to the target,
3925  * which is stored in LOV EA.
3926  *
3927  * \param[in] env               execution environment for this thread
3928  * \param[in] lo                LOD object
3929  * \param[in] th                transaction handle
3930  *
3931  * \retval                      0 if LOV EA is stored successfully
3932  * \retval                      negative error number on failure
3933  */
3934 static int lod_generate_and_set_lovea(const struct lu_env *env,
3935                                       struct lod_object *lo,
3936                                       struct thandle *th)
3937 {
3938         struct lod_thread_info  *info = lod_env_info(env);
3939         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3940         struct lov_mds_md_v1    *lmm;
3941         int                      rc, lmm_size;
3942         ENTRY;
3943
3944         LASSERT(lo);
3945
3946         if (lo->ldo_comp_cnt == 0 && !lo->ldo_is_foreign) {
3947                 lod_striping_free(env, lo);
3948                 rc = lod_sub_xattr_del(env, next, XATTR_NAME_LOV, th);
3949                 RETURN(rc);
3950         }
3951
3952         lmm_size = lod_comp_md_size(lo, false);
3953         if (info->lti_ea_store_size < lmm_size) {
3954                 rc = lod_ea_store_resize(info, lmm_size);
3955                 if (rc)
3956                         RETURN(rc);
3957         }
3958         lmm = info->lti_ea_store;
3959
3960         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3961         if (rc)
3962                 RETURN(rc);
3963
3964         info->lti_buf.lb_buf = lmm;
3965         info->lti_buf.lb_len = lmm_size;
3966         rc = lod_sub_xattr_set(env, next, &info->lti_buf,
3967                                XATTR_NAME_LOV, 0, th);
3968         RETURN(rc);
3969 }
3970
3971 /**
3972  * Delete layout component(s)
3973  *
3974  * \param[in] env       execution environment for this thread
3975  * \param[in] dt        object
3976  * \param[in] th        transaction handle
3977  *
3978  * \retval      0 on success
3979  * \retval      negative error number on failure
3980  */
3981 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3982                           struct thandle *th)
3983 {
3984         struct lod_layout_component     *lod_comp;
3985         struct lod_object       *lo = lod_dt_obj(dt);
3986         struct dt_object        *next = dt_object_child(dt);
3987         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3988         int     rc, i, j, left;
3989
3990         LASSERT(lo->ldo_is_composite);
3991         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3992
3993         left = lo->ldo_comp_cnt;
3994         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3995                 lod_comp = &lo->ldo_comp_entries[i];
3996
3997                 if (lod_comp->llc_id != LCME_ID_INVAL)
3998                         break;
3999                 left--;
4000
4001                 /* Not instantiated component */
4002                 if (lod_comp->llc_stripe == NULL)
4003                         continue;
4004
4005                 LASSERT(lod_comp->llc_stripe_count > 0);
4006                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
4007                         struct dt_object *obj = lod_comp->llc_stripe[j];
4008
4009                         if (obj == NULL)
4010                                 continue;
4011                         rc = lod_sub_destroy(env, obj, th);
4012                         if (rc)
4013                                 GOTO(out, rc);
4014
4015                         lu_object_put(env, &obj->do_lu);
4016                         lod_comp->llc_stripe[j] = NULL;
4017                 }
4018                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
4019                                         lod_comp->llc_stripes_allocated);
4020                 lod_comp->llc_stripe = NULL;
4021                 OBD_FREE(lod_comp->llc_ost_indices,
4022                          sizeof(__u32) * lod_comp->llc_stripes_allocated);
4023                 lod_comp->llc_ost_indices = NULL;
4024                 lod_comp->llc_stripes_allocated = 0;
4025                 lod_obj_set_pool(lo, i, NULL);
4026                 if (lod_comp->llc_ostlist.op_array) {
4027                         OBD_FREE(lod_comp->llc_ostlist.op_array,
4028                                  lod_comp->llc_ostlist.op_size);
4029                         lod_comp->llc_ostlist.op_array = NULL;
4030                         lod_comp->llc_ostlist.op_size = 0;
4031                 }
4032         }
4033
4034         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
4035         if (left > 0) {
4036                 struct lod_layout_component     *comp_array;
4037
4038                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
4039                 if (comp_array == NULL)
4040                         GOTO(out, rc = -ENOMEM);
4041
4042                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
4043                        sizeof(*comp_array) * left);
4044
4045                 OBD_FREE(lo->ldo_comp_entries,
4046                          sizeof(*comp_array) * lo->ldo_comp_cnt);
4047                 lo->ldo_comp_entries = comp_array;
4048                 lo->ldo_comp_cnt = left;
4049
4050                 LASSERT(lo->ldo_mirror_count == 1);
4051                 lo->ldo_mirrors[0].lme_end = left - 1;
4052                 lod_obj_inc_layout_gen(lo);
4053         } else {
4054                 lod_free_comp_entries(lo);
4055         }
4056
4057         LASSERT(dt_object_exists(dt));
4058         rc = dt_attr_get(env, next, attr);
4059         if (rc)
4060                 GOTO(out, rc);
4061
4062         if (attr->la_size > 0) {
4063                 attr->la_size = 0;
4064                 attr->la_valid = LA_SIZE;
4065                 rc = lod_sub_attr_set(env, next, attr, th);
4066                 if (rc)
4067                         GOTO(out, rc);
4068         }
4069
4070         rc = lod_generate_and_set_lovea(env, lo, th);
4071         EXIT;
4072 out:
4073         if (rc)
4074                 lod_striping_free(env, lo);
4075         return rc;
4076 }
4077
4078
4079 static int lod_get_default_lov_striping(const struct lu_env *env,
4080                                         struct lod_object *lo,
4081                                         struct lod_default_striping *lds);
4082 /**
4083  * Implementation of dt_object_operations::do_xattr_set.
4084  *
4085  * Sets specified extended attribute on the object. Three types of EAs are
4086  * special:
4087  *   LOV EA - stores striping for a regular file or default striping (when set
4088  *            on a directory)
4089  *   LMV EA - stores a marker for the striped directories
4090  *   DMV EA - stores default directory striping
4091  *
4092  * When striping is applied to a non-striped existing object (this is called
4093  * late striping), then LOD notices the caller wants to turn the object into a
4094  * striped one. The stripe objects are created and appropriate EA is set:
4095  * LOV EA storing all the stripes directly or LMV EA storing just a small header
4096  * with striping configuration.
4097  *
4098  * \see dt_object_operations::do_xattr_set() in the API description for details.
4099  */
4100 static int lod_xattr_set(const struct lu_env *env,
4101                          struct dt_object *dt, const struct lu_buf *buf,
4102                          const char *name, int fl, struct thandle *th)
4103 {
4104         struct dt_object        *next = dt_object_child(dt);
4105         int                      rc;
4106         ENTRY;
4107
4108         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4109             strcmp(name, XATTR_NAME_LMV) == 0) {
4110                 rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
4111                 RETURN(rc);
4112         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4113                    strncmp(name, XATTR_NAME_LMV, strlen(XATTR_NAME_LMV)) == 0 &&
4114                    strlen(name) > strlen(XATTR_NAME_LMV) + 1) {
4115                 const char *op = name + strlen(XATTR_NAME_LMV) + 1;
4116
4117                 rc = -ENOTSUPP;
4118                 /*
4119                  * XATTR_NAME_LMV".add" is never called, but only declared,
4120                  * because lod_xattr_set_lmv() will do the addition.
4121                  */
4122                 if (strcmp(op, "del") == 0)
4123                         rc = lod_dir_layout_delete(env, dt, buf, th);
4124                 else if (strcmp(op, "set") == 0)
4125                         rc = lod_sub_xattr_set(env, next, buf, XATTR_NAME_LMV,
4126                                                fl, th);
4127
4128                 RETURN(rc);
4129         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4130             strcmp(name, XATTR_NAME_LOV) == 0) {
4131                 struct lod_default_striping *lds = lod_lds_buf_get(env);
4132                 struct lov_user_md_v1 *v1 = buf->lb_buf;
4133                 char pool[LOV_MAXPOOLNAME + 1];
4134                 bool is_del;
4135
4136                 /* get existing striping config */
4137                 rc = lod_get_default_lov_striping(env, lod_dt_obj(dt), lds);
4138                 if (rc)
4139                         RETURN(rc);
4140
4141                 memset(pool, 0, sizeof(pool));
4142                 if (lds->lds_def_striping_set == 1)
4143                         lod_layout_get_pool(lds->lds_def_comp_entries,
4144                                             lds->lds_def_comp_cnt, pool,
4145                                             sizeof(pool));
4146
4147                 is_del = LOVEA_DELETE_VALUES(v1->lmm_stripe_size,
4148                                              v1->lmm_stripe_count,
4149                                              v1->lmm_stripe_offset,
4150                                              NULL);
4151
4152                 /* Retain the pool name if it is not given */
4153                 if (v1->lmm_magic == LOV_USER_MAGIC_V1 && pool[0] != '\0' &&
4154                         !is_del) {
4155                         struct lod_thread_info *info = lod_env_info(env);
4156                         struct lov_user_md_v3 *v3  = info->lti_ea_store;
4157
4158                         memset(v3, 0, sizeof(*v3));
4159                         v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
4160                         v3->lmm_pattern = cpu_to_le32(v1->lmm_pattern);
4161                         v3->lmm_stripe_count =
4162                                         cpu_to_le32(v1->lmm_stripe_count);
4163                         v3->lmm_stripe_offset =
4164                                         cpu_to_le32(v1->lmm_stripe_offset);
4165                         v3->lmm_stripe_size = cpu_to_le32(v1->lmm_stripe_size);
4166
4167                         strlcpy(v3->lmm_pool_name, pool,
4168                                 sizeof(v3->lmm_pool_name));
4169
4170                         info->lti_buf.lb_buf = v3;
4171                         info->lti_buf.lb_len = sizeof(*v3);
4172                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
4173                                                       name, fl, th);
4174                 } else {
4175                         rc = lod_xattr_set_lov_on_dir(env, dt, buf, name,
4176                                                       fl, th);
4177                 }
4178
4179                 if (lds->lds_def_striping_set == 1 &&
4180                     lds->lds_def_comp_entries != NULL)
4181                         lod_free_def_comp_entries(lds);
4182
4183                 RETURN(rc);
4184         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
4185                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
4186                 /* default LMVEA */
4187                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
4188                                                       th);
4189                 RETURN(rc);
4190         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4191                    (!strcmp(name, XATTR_NAME_LOV) ||
4192                     !strncmp(name, XATTR_LUSTRE_LOV,
4193                              strlen(XATTR_LUSTRE_LOV)))) {
4194                 /* in case of lov EA swap, just set it
4195                  * if not, it is a replay so check striping match what we
4196                  * already have during req replay, declare_xattr_set()
4197                  * defines striping, then create() does the work */
4198                 if (fl & LU_XATTR_REPLACE) {
4199                         /* free stripes, then update disk */
4200                         lod_striping_free(env, lod_dt_obj(dt));
4201
4202                         rc = lod_sub_xattr_set(env, next, buf, name, fl, th);
4203                 } else if (dt_object_remote(dt)) {
4204                         /* This only happens during migration, see
4205                          * mdd_migrate_create(), in which Master MDT will
4206                          * create a remote target object, and only set
4207                          * (migrating) stripe EA on the remote object,
4208                          * and does not need creating each stripes. */
4209                         rc = lod_sub_xattr_set(env, next, buf, name,
4210                                                       fl, th);
4211                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
4212                         /* delete component(s) */
4213                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
4214                         rc = lod_layout_del(env, dt, th);
4215                 } else {
4216                         /*
4217                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
4218                          * it's going to create create file with specified
4219                          * component(s), the striping must have not being
4220                          * cached in this case;
4221                          *
4222                          * Otherwise, it's going to add/change component(s) to
4223                          * an existing file, the striping must have been cached
4224                          * in this case.
4225                          */
4226                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
4227                                      !strcmp(name, XATTR_NAME_LOV),
4228                                 !lod_dt_obj(dt)->ldo_comp_cached));
4229
4230                         rc = lod_striped_create(env, dt, NULL, NULL, th);
4231                 }
4232                 RETURN(rc);
4233         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
4234                 rc = lod_replace_parent_fid(env, dt, buf, th, false);
4235
4236                 RETURN(rc);
4237         }
4238
4239         /* then all other xattr */
4240         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
4241
4242         RETURN(rc);
4243 }
4244
4245 /**
4246  * Implementation of dt_object_operations::do_declare_xattr_del.
4247  *
4248  * \see dt_object_operations::do_declare_xattr_del() in the API description
4249  * for details.
4250  */
4251 static int lod_declare_xattr_del(const struct lu_env *env,
4252                                  struct dt_object *dt, const char *name,
4253                                  struct thandle *th)
4254 {
4255         struct lod_object *lo = lod_dt_obj(dt);
4256         struct dt_object *next = dt_object_child(dt);
4257         int i;
4258         int rc;
4259         ENTRY;
4260
4261         rc = lod_sub_declare_xattr_del(env, next, name, th);
4262         if (rc != 0)
4263                 RETURN(rc);
4264
4265         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4266                 RETURN(0);
4267
4268         /* set xattr to each stripes, if needed */
4269         rc = lod_striping_load(env, lo);
4270         if (rc != 0)
4271                 RETURN(rc);
4272
4273         if (lo->ldo_dir_stripe_count == 0)
4274                 RETURN(0);
4275
4276         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4277                 struct dt_object *dto = lo->ldo_stripe[i];
4278
4279                 LASSERT(dto);
4280                 rc = lod_sub_declare_xattr_del(env, dto, name, th);
4281                 if (rc != 0)
4282                         break;
4283         }
4284
4285         RETURN(rc);
4286 }
4287
4288 /**
4289  * Implementation of dt_object_operations::do_xattr_del.
4290  *
4291  * If EA storing a regular striping is being deleted, then release
4292  * all the references to the stripe objects in core.
4293  *
4294  * \see dt_object_operations::do_xattr_del() in the API description for details.
4295  */
4296 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
4297                          const char *name, struct thandle *th)
4298 {
4299         struct dt_object        *next = dt_object_child(dt);
4300         struct lod_object       *lo = lod_dt_obj(dt);
4301         int                     rc;
4302         int                     i;
4303         ENTRY;
4304
4305         if (!strcmp(name, XATTR_NAME_LOV) || !strcmp(name, XATTR_NAME_LMV))
4306                 lod_striping_free(env, lod_dt_obj(dt));
4307
4308         rc = lod_sub_xattr_del(env, next, name, th);
4309         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
4310                 RETURN(rc);
4311
4312         if (lo->ldo_dir_stripe_count == 0)
4313                 RETURN(0);
4314
4315         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
4316                 struct dt_object *dto = lo->ldo_stripe[i];
4317
4318                 LASSERT(dto);
4319
4320                 rc = lod_sub_xattr_del(env, dto, name, th);
4321                 if (rc != 0)
4322                         break;
4323         }
4324
4325         RETURN(rc);
4326 }
4327
4328 /**
4329  * Implementation of dt_object_operations::do_xattr_list.
4330  *
4331  * \see dt_object_operations::do_xattr_list() in the API description
4332  * for details.
4333  */
4334 static int lod_xattr_list(const struct lu_env *env,
4335                           struct dt_object *dt, const struct lu_buf *buf)
4336 {
4337         return dt_xattr_list(env, dt_object_child(dt), buf);
4338 }
4339
4340 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
4341 {
4342         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
4343 }
4344
4345 /**
4346  * Copy OST list from layout provided by user.
4347  *
4348  * \param[in] lod_comp          layout_component to be filled
4349  * \param[in] v3                LOV EA V3 user data
4350  *
4351  * \retval              0 on success
4352  * \retval              negative if failed
4353  */
4354 int lod_comp_copy_ost_lists(struct lod_layout_component *lod_comp,
4355                             struct lov_user_md_v3 *v3)
4356 {
4357         int j;
4358
4359         ENTRY;
4360
4361         if (v3->lmm_stripe_offset == LOV_OFFSET_DEFAULT)
4362                 v3->lmm_stripe_offset = v3->lmm_objects[0].l_ost_idx;
4363
4364         if (lod_comp->llc_ostlist.op_array) {
4365                 if (lod_comp->llc_ostlist.op_size >=
4366                     v3->lmm_stripe_count * sizeof(__u32))  {
4367                         lod_comp->llc_ostlist.op_count =
4368                                         v3->lmm_stripe_count;
4369                         goto skip;
4370                 }
4371                 OBD_FREE(lod_comp->llc_ostlist.op_array,
4372                          lod_comp->llc_ostlist.op_size);
4373         }
4374
4375         /* copy ost list from lmm */
4376         lod_comp->llc_ostlist.op_count = v3->lmm_stripe_count;
4377         lod_comp->llc_ostlist.op_size = v3->lmm_stripe_count * sizeof(__u32);
4378         OBD_ALLOC(lod_comp->llc_ostlist.op_array,
4379                   lod_comp->llc_ostlist.op_size);
4380         if (!lod_comp->llc_ostlist.op_array)
4381                 RETURN(-ENOMEM);
4382 skip:
4383         for (j = 0; j < v3->lmm_stripe_count; j++) {
4384                 lod_comp->llc_ostlist.op_array[j] =
4385                         v3->lmm_objects[j].l_ost_idx;
4386         }
4387
4388         RETURN(0);
4389 }
4390
4391
4392 /**
4393  * Get default striping.
4394  *
4395  * \param[in] env               execution environment
4396  * \param[in] lo                object
4397  * \param[out] lds              default striping
4398  *
4399  * \retval              0 on success
4400  * \retval              negative if failed
4401  */
4402 static int lod_get_default_lov_striping(const struct lu_env *env,
4403                                         struct lod_object *lo,
4404                                         struct lod_default_striping *lds)
4405 {
4406         struct lod_thread_info *info = lod_env_info(env);
4407         struct lov_user_md_v1 *v1 = NULL;
4408         struct lov_user_md_v3 *v3 = NULL;
4409         struct lov_comp_md_v1 *comp_v1 = NULL;
4410         __u16   comp_cnt;
4411         __u16   mirror_cnt;
4412         bool    composite;
4413         int     rc, i, j;
4414         ENTRY;
4415
4416         lds->lds_def_striping_set = 0;
4417
4418         rc = lod_get_lov_ea(env, lo);
4419         if (rc < 0)
4420                 RETURN(rc);
4421
4422         if (rc < (typeof(rc))sizeof(struct lov_user_md))
4423                 RETURN(0);
4424
4425         v1 = info->lti_ea_store;
4426         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
4427                 lustre_swab_lov_user_md_v1(v1);
4428         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
4429                 v3 = (struct lov_user_md_v3 *)v1;
4430                 lustre_swab_lov_user_md_v3(v3);
4431         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_SPECIFIC)) {
4432                 v3 = (struct lov_user_md_v3 *)v1;
4433                 lustre_swab_lov_user_md_v3(v3);
4434                 lustre_swab_lov_user_md_objects(v3->lmm_objects,
4435                                                 v3->lmm_stripe_count);
4436         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
4437                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4438                 lustre_swab_lov_comp_md_v1(comp_v1);
4439         }
4440
4441         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
4442             v1->lmm_magic != LOV_MAGIC_COMP_V1 &&
4443             v1->lmm_magic != LOV_USER_MAGIC_SPECIFIC)
4444                 RETURN(-ENOTSUPP);
4445
4446         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
4447                 comp_v1 = (struct lov_comp_md_v1 *)v1;
4448                 comp_cnt = comp_v1->lcm_entry_count;
4449                 if (comp_cnt == 0)
4450                         RETURN(-EINVAL);
4451                 mirror_cnt = comp_v1->lcm_mirror_count + 1;
4452                 composite = true;
4453         } else {
4454                 comp_cnt = 1;
4455                 mirror_cnt = 0;
4456                 composite = false;
4457         }
4458
4459         /* realloc default comp entries if necessary */
4460         rc = lod_def_striping_comp_resize(lds, comp_cnt);
4461         if (rc < 0)
4462                 RETURN(rc);
4463
4464         lds->lds_def_comp_cnt = comp_cnt;
4465         lds->lds_def_striping_is_composite = composite;
4466         lds->lds_def_mirror_cnt = mirror_cnt;
4467
4468         for (i = 0; i < comp_cnt; i++) {
4469                 struct lod_layout_component *lod_comp;
4470                 char *pool;
4471
4472                 lod_comp = &lds->lds_def_comp_entries[i];
4473                 /*
4474                  * reset lod_comp values, llc_stripes is always NULL in
4475                  * the default striping template, llc_pool will be reset
4476                  * later below.
4477                  */
4478                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
4479
4480                 if (composite) {
4481                         v1 = (struct lov_user_md *)((char *)comp_v1 +
4482                                         comp_v1->lcm_entries[i].lcme_offset);
4483                         lod_comp->llc_extent =
4484                                         comp_v1->lcm_entries[i].lcme_extent;
4485                         /* We only inherit certain flags from the layout */
4486                         lod_comp->llc_flags =
4487                                         comp_v1->lcm_entries[i].lcme_flags &
4488                                         LCME_TEMPLATE_FLAGS;
4489                 }
4490
4491                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
4492                     v1->lmm_pattern != LOV_PATTERN_MDT &&
4493                     v1->lmm_pattern != 0) {
4494                         lod_free_def_comp_entries(lds);
4495                         RETURN(-EINVAL);
4496                 }
4497
4498                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
4499                        "stripe_offset=%d\n",
4500                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
4501                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
4502                        (int)v1->lmm_stripe_offset);
4503
4504                 lod_comp->llc_stripe_count = v1->lmm_stripe_count;
4505                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
4506                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
4507                 lod_comp->llc_pattern = v1->lmm_pattern;
4508
4509                 pool = NULL;
4510                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
4511                         /* XXX: sanity check here */
4512                         v3 = (struct lov_user_md_v3 *) v1;
4513                         if (v3->lmm_pool_name[0] != '\0')
4514                                 pool = v3->lmm_pool_name;
4515                 }
4516                 lod_set_def_pool(lds, i, pool);
4517                 if (v1->lmm_magic == LOV_USER_MAGIC_SPECIFIC) {
4518                         v3 = (struct lov_user_md_v3 *)v1;
4519                         rc = lod_comp_copy_ost_lists(lod_comp, v3);
4520                         if (rc)
4521                                 RETURN(rc);
4522                 } else if (lod_comp->llc_ostlist.op_array &&
4523                            lod_comp->llc_ostlist.op_count) {
4524                         for (j = 0; j < lod_comp->llc_ostlist.op_count; j++)
4525                                 lod_comp->llc_ostlist.op_array[j] = -1;
4526                         lod_comp->llc_ostlist.op_count = 0;
4527                 }
4528         }
4529
4530         lds->lds_def_striping_set = 1;
4531         RETURN(rc);
4532 }
4533
4534 /**
4535  * Get default directory striping.
4536  *
4537  * \param[in] env               execution environment
4538  * \param[in] lo                object
4539  * \param[out] lds              default striping
4540  *
4541  * \retval              0 on success
4542  * \retval              negative if failed
4543  */
4544 static int lod_get_default_lmv_striping(const struct lu_env *env,
4545                                         struct lod_object *lo,
4546                                         struct lod_default_striping *lds)
4547 {
4548         struct lod_thread_info  *info = lod_env_info(env);
4549         struct lmv_user_md_v1   *v1 = NULL;
4550         int                      rc;
4551         ENTRY;
4552
4553         lds->lds_dir_def_striping_set = 0;
4554         rc = lod_get_default_lmv_ea(env, lo);
4555         if (rc < 0)
4556                 RETURN(rc);
4557
4558         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
4559                 RETURN(0);
4560
4561         v1 = info->lti_ea_store;
4562
4563         lds->lds_dir_def_stripe_count = le32_to_cpu(v1->lum_stripe_count);
4564         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
4565         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
4566         lds->lds_dir_def_striping_set = 1;
4567
4568         RETURN(0);
4569 }
4570
4571 /**
4572  * Get default striping in the object.
4573  *
4574  * Get object default striping and default directory striping.
4575  *
4576  * \param[in] env               execution environment
4577  * \param[in] lo                object
4578  * \param[out] lds              default striping
4579  *
4580  * \retval              0 on success
4581  * \retval              negative if failed
4582  */
4583 static int lod_get_default_striping(const struct lu_env *env,
4584                                     struct lod_object *lo,
4585                                     struct lod_default_striping *lds)
4586 {
4587         int rc, rc1;
4588
4589         rc = lod_get_default_lov_striping(env, lo, lds);
4590         rc1 = lod_get_default_lmv_striping(env, lo, lds);
4591         if (rc == 0 && rc1 < 0)
4592                 rc = rc1;
4593
4594         return rc;
4595 }
4596
4597 /**
4598  * Apply default striping on object.
4599  *
4600  * If object striping pattern is not set, set to the one in default striping.
4601  * The default striping is from parent or fs.
4602  *
4603  * \param[in] lo                new object
4604  * \param[in] lds               default striping
4605  * \param[in] mode              new object's mode
4606  */
4607 static void lod_striping_from_default(struct lod_object *lo,
4608                                       const struct lod_default_striping *lds,
4609                                       umode_t mode)
4610 {
4611         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4612         struct lov_desc *desc = &d->lod_desc;
4613         int i, rc;
4614
4615         if (lds->lds_def_striping_set && S_ISREG(mode)) {
4616                 rc = lod_alloc_comp_entries(lo, lds->lds_def_mirror_cnt,
4617                                             lds->lds_def_comp_cnt);
4618                 if (rc != 0)
4619                         return;
4620
4621                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
4622                 if (lds->lds_def_mirror_cnt > 1)
4623                         lo->ldo_flr_state = LCM_FL_RDONLY;
4624
4625                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4626                         struct lod_layout_component *obj_comp =
4627                                                 &lo->ldo_comp_entries[i];
4628                         struct lod_layout_component *def_comp =
4629                                                 &lds->lds_def_comp_entries[i];
4630
4631                         CDEBUG(D_LAYOUT, "Inherit from default: flags=%#x "
4632                                "size=%hu nr=%u offset=%u pattern=%#x pool=%s\n",
4633                                def_comp->llc_flags,
4634                                def_comp->llc_stripe_size,
4635                                def_comp->llc_stripe_count,
4636                                def_comp->llc_stripe_offset,
4637                                def_comp->llc_pattern,
4638                                def_comp->llc_pool ?: "");
4639
4640                         *obj_comp = *def_comp;
4641                         if (def_comp->llc_pool != NULL) {
4642                                 /* pointer was copied from def_comp */
4643                                 obj_comp->llc_pool = NULL;
4644                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
4645                         }
4646
4647                         /* copy ost list */
4648                         if (def_comp->llc_ostlist.op_array &&
4649                             def_comp->llc_ostlist.op_count) {
4650                                 OBD_ALLOC(obj_comp->llc_ostlist.op_array,
4651                                           obj_comp->llc_ostlist.op_size);
4652                                 if (!obj_comp->llc_ostlist.op_array)
4653                                         return;
4654                                 memcpy(obj_comp->llc_ostlist.op_array,
4655                                        def_comp->llc_ostlist.op_array,
4656                                        obj_comp->llc_ostlist.op_size);
4657                         } else if (def_comp->llc_ostlist.op_array) {
4658                                 obj_comp->llc_ostlist.op_array = NULL;
4659                         }
4660
4661                         /*
4662                          * Don't initialize these fields for plain layout
4663                          * (v1/v3) here, they are inherited in the order of
4664                          * 'parent' -> 'fs default (root)' -> 'global default
4665                          * values for stripe_count & stripe_size'.
4666                          *
4667                          * see lod_ah_init().
4668                          */
4669                         if (!lo->ldo_is_composite)
4670                                 continue;
4671
4672                         lod_adjust_stripe_info(obj_comp, desc);
4673                 }
4674         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
4675                 if (lo->ldo_dir_stripe_count == 0)
4676                         lo->ldo_dir_stripe_count =
4677                                 lds->lds_dir_def_stripe_count;
4678                 if (lo->ldo_dir_stripe_offset == -1)
4679                         lo->ldo_dir_stripe_offset =
4680                                 lds->lds_dir_def_stripe_offset;
4681                 if (lo->ldo_dir_hash_type == 0)
4682                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
4683
4684                 CDEBUG(D_LAYOUT, "striping from default dir: count:%hu, "
4685                        "offset:%u, hash_type:%u\n",
4686                        lo->ldo_dir_stripe_count, lo->ldo_dir_stripe_offset,
4687                        lo->ldo_dir_hash_type);
4688         }
4689 }
4690
4691 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
4692 {
4693         struct lod_layout_component *lod_comp;
4694
4695         if (lo->ldo_comp_cnt == 0)
4696                 return true;
4697
4698         if (lo->ldo_is_composite)
4699                 return false;
4700
4701         lod_comp = &lo->ldo_comp_entries[0];
4702
4703         if (lod_comp->llc_stripe_count <= 0 ||
4704             lod_comp->llc_stripe_size <= 0)
4705                 return true;
4706
4707         if (from_root && (lod_comp->llc_pool == NULL ||
4708                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
4709                 return true;
4710
4711         return false;
4712 }
4713
4714 /**
4715  * Implementation of dt_object_operations::do_ah_init.
4716  *
4717  * This method is used to make a decision on the striping configuration for the
4718  * object being created. It can be taken from the \a parent object if it exists,
4719  * or filesystem's default. The resulting configuration (number of stripes,
4720  * stripe size/offset, pool name, etc) is stored in the object itself and will
4721  * be used by the methods like ->doo_declare_create().
4722  *
4723  * \see dt_object_operations::do_ah_init() in the API description for details.
4724  */
4725 static void lod_ah_init(const struct lu_env *env,
4726                         struct dt_allocation_hint *ah,
4727                         struct dt_object *parent,
4728                         struct dt_object *child,
4729                         umode_t child_mode)
4730 {
4731         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
4732         struct lod_thread_info *info = lod_env_info(env);
4733         struct lod_default_striping *lds = lod_lds_buf_get(env);
4734         struct dt_object *nextp = NULL;
4735         struct dt_object *nextc;
4736         struct lod_object *lp = NULL;
4737         struct lod_object *lc;
4738         struct lov_desc *desc;
4739         struct lod_layout_component *lod_comp;
4740         int rc;
4741         ENTRY;
4742
4743         LASSERT(child);
4744
4745         if (likely(parent)) {
4746                 nextp = dt_object_child(parent);
4747                 lp = lod_dt_obj(parent);
4748         }
4749
4750         nextc = dt_object_child(child);
4751         lc = lod_dt_obj(child);
4752
4753         LASSERT(!lod_obj_is_striped(child));
4754         /* default layout template may have been set on the regular file
4755          * when this is called from mdd_create_data() */
4756         if (S_ISREG(child_mode))
4757                 lod_free_comp_entries(lc);
4758
4759         if (!dt_object_exists(nextc))
4760                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
4761
4762         if (S_ISDIR(child_mode)) {
4763                 const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
4764
4765                 /* other default values are 0 */
4766                 lc->ldo_dir_stripe_offset = -1;
4767
4768                 /*
4769                  * If parent object is not root directory,
4770                  * then get default striping from parent object.
4771                  */
4772                 if (likely(lp != NULL) && !fid_is_root(lod_object_fid(lp)))
4773                         lod_get_default_striping(env, lp, lds);
4774
4775                 /* set child default striping info, default value is NULL */
4776                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
4777                         lc->ldo_def_striping = lds;
4778
4779                 /* It should always honour the specified stripes */
4780                 /* Note: old client (< 2.7)might also do lfs mkdir, whose EA
4781                  * will have old magic. In this case, we should ignore the
4782                  * stripe count and try to create dir by default stripe.
4783                  */
4784                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
4785                     (le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC ||
4786                      le32_to_cpu(lum1->lum_magic) == LMV_USER_MAGIC_SPECIFIC)) {
4787                         lc->ldo_dir_stripe_count =
4788                                 le32_to_cpu(lum1->lum_stripe_count);
4789                         lc->ldo_dir_stripe_offset =
4790                                 le32_to_cpu(lum1->lum_stripe_offset);
4791                         lc->ldo_dir_hash_type =
4792                                 le32_to_cpu(lum1->lum_hash_type);
4793                         CDEBUG(D_INFO,
4794                                "set dirstripe: count %hu, offset %d, hash %u\n",
4795                                 lc->ldo_dir_stripe_count,
4796                                 (int)lc->ldo_dir_stripe_offset,
4797                                 lc->ldo_dir_hash_type);
4798                 } else {
4799                         /* transfer defaults LMV to new directory */
4800                         lod_striping_from_default(lc, lds, child_mode);
4801
4802                         /* set count 0 to create normal directory */
4803                         if (lc->ldo_dir_stripe_count == 1)
4804                                 lc->ldo_dir_stripe_count = 0;
4805                 }
4806
4807                 /* shrink the stripe_count to the avaible MDT count */
4808                 if (lc->ldo_dir_stripe_count > d->lod_remote_mdt_count + 1 &&
4809                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE)) {
4810                         lc->ldo_dir_stripe_count = d->lod_remote_mdt_count + 1;
4811                         if (lc->ldo_dir_stripe_count == 1)
4812                                 lc->ldo_dir_stripe_count = 0;
4813                 }
4814
4815                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
4816                        lc->ldo_dir_stripe_count,
4817                        (int)lc->ldo_dir_stripe_offset, lc->ldo_dir_hash_type);
4818
4819                 RETURN_EXIT;
4820         }
4821
4822         /* child object regular file*/
4823
4824         if (!lod_object_will_be_striped(S_ISREG(child_mode),
4825                                         lu_object_fid(&child->do_lu)))
4826                 RETURN_EXIT;
4827
4828         /* If object is going to be striped over OSTs, transfer default
4829          * striping information to the child, so that we can use it
4830          * during declaration and creation.
4831          *
4832          * Try from the parent first.
4833          */
4834         if (likely(lp != NULL)) {
4835                 rc = lod_get_default_lov_striping(env, lp, lds);
4836                 if (rc == 0)
4837                         lod_striping_from_default(lc, lds, child_mode);
4838         }
4839
4840         /* Initialize lod_device::lod_md_root object reference */
4841         if (d->lod_md_root == NULL) {
4842                 struct dt_object *root;
4843                 struct lod_object *lroot;
4844
4845                 lu_root_fid(&info->lti_fid);
4846                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
4847                 if (!IS_ERR(root)) {
4848                         lroot = lod_dt_obj(root);
4849
4850                         spin_lock(&d->lod_lock);
4851                         if (d->lod_md_root != NULL)
4852                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
4853                         d->lod_md_root = lroot;
4854                         spin_unlock(&d->lod_lock);
4855                 }
4856         }
4857
4858         /* try inherit layout from the root object (fs default) when:
4859          *  - parent does not have default layout; or
4860          *  - parent has plain(v1/v3) default layout, and some attributes
4861          *    are not specified in the default layout;
4862          */
4863         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
4864                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
4865                 if (rc)
4866                         goto out;
4867                 if (lc->ldo_comp_cnt == 0) {
4868                         lod_striping_from_default(lc, lds, child_mode);
4869                 } else if (!lds->lds_def_striping_is_composite) {
4870                         struct lod_layout_component *def_comp;
4871
4872                         LASSERT(!lc->ldo_is_composite);
4873                         lod_comp = &lc->ldo_comp_entries[0];
4874                         def_comp = &lds->lds_def_comp_entries[0];
4875
4876                         if (lod_comp->llc_stripe_count <= 0)
4877                                 lod_comp->llc_stripe_count =
4878                                         def_comp->llc_stripe_count;
4879                         if (lod_comp->llc_stripe_size <= 0)
4880                                 lod_comp->llc_stripe_size =
4881                                         def_comp->llc_stripe_size;
4882                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT &&
4883                             (!lod_comp->llc_pool || !lod_comp->llc_pool[0]))
4884                                 lod_comp->llc_stripe_offset =
4885                                         def_comp->llc_stripe_offset;
4886                         if (lod_comp->llc_pool == NULL)
4887                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
4888                 }
4889         }
4890 out:
4891         /*
4892          * fs default striping may not be explicitly set, or historically set
4893          * in config log, use them.
4894          */
4895         if (lod_need_inherit_more(lc, false)) {
4896                 if (lc->ldo_comp_cnt == 0) {
4897                         rc = lod_alloc_comp_entries(lc, 0, 1);
4898                         if (rc)
4899                                 /* fail to allocate memory, will create a
4900                                  * non-striped file. */
4901                                 RETURN_EXIT;
4902                         lc->ldo_is_composite = 0;
4903                         lod_comp = &lc->ldo_comp_entries[0];
4904                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4905                 }
4906                 LASSERT(!lc->ldo_is_composite);
4907                 lod_comp = &lc->ldo_comp_entries[0];
4908                 desc = &d->lod_desc;
4909                 lod_adjust_stripe_info(lod_comp, desc);
4910         }
4911
4912         EXIT;
4913 }
4914
4915 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
4916 /**
4917  * Size initialization on late striping.
4918  *
4919  * Propagate the size of a truncated object to a deferred striping.
4920  * This function handles a special case when truncate was done on a
4921  * non-striped object and now while the striping is being created
4922  * we can't lose that size, so we have to propagate it to the stripes
4923  * being created.
4924  *
4925  * \param[in] env       execution environment
4926  * \param[in] dt        object
4927  * \param[in] th        transaction handle
4928  *
4929  * \retval              0 on success
4930  * \retval              negative if failed
4931  */
4932 static int lod_declare_init_size(const struct lu_env *env,
4933                                  struct dt_object *dt, struct thandle *th)
4934 {
4935         struct dt_object        *next = dt_object_child(dt);
4936         struct lod_object       *lo = lod_dt_obj(dt);
4937         struct dt_object        **objects = NULL;
4938         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
4939         uint64_t        size, offs;
4940         int     i, rc, stripe, stripe_count = 0, stripe_size = 0;
4941         struct lu_extent size_ext;
4942         ENTRY;
4943
4944         if (!lod_obj_is_striped(dt))
4945                 RETURN(0);
4946
4947         rc = dt_attr_get(env, next, attr);
4948         LASSERT(attr->la_valid & LA_SIZE);
4949         if (rc)
4950                 RETURN(rc);
4951
4952         size = attr->la_size;
4953         if (size == 0)
4954                 RETURN(0);
4955
4956         size_ext = (typeof(size_ext)){ .e_start = size - 1, .e_end = size };
4957         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4958                 struct lod_layout_component *lod_comp;
4959                 struct lu_extent *extent;
4960
4961                 lod_comp = &lo->ldo_comp_entries[i];
4962
4963                 if (lod_comp->llc_stripe == NULL)
4964                         continue;
4965
4966                 extent = &lod_comp->llc_extent;
4967                 CDEBUG(D_INFO, "%lld "DEXT"\n", size, PEXT(extent));
4968                 if (!lo->ldo_is_composite ||
4969                     lu_extent_is_overlapped(extent, &size_ext)) {
4970                         objects = lod_comp->llc_stripe;
4971                         stripe_count = lod_comp->llc_stripe_count;
4972                         stripe_size = lod_comp->llc_stripe_size;
4973
4974                         /* next mirror */
4975                         if (stripe_count == 0)
4976                                 continue;
4977
4978                         LASSERT(objects != NULL && stripe_size != 0);
4979                         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4980                         ll_do_div64(size, (__u64)stripe_size);
4981                         stripe = ll_do_div64(size, (__u64)stripe_count);
4982                         LASSERT(objects[stripe] != NULL);
4983
4984                         size = size * stripe_size;
4985                         offs = attr->la_size;
4986                         size += ll_do_div64(offs, stripe_size);
4987
4988                         attr->la_valid = LA_SIZE;
4989                         attr->la_size = size;
4990
4991                         rc = lod_sub_declare_attr_set(env, objects[stripe],
4992                                                       attr, th);
4993                 }
4994         }
4995
4996         RETURN(rc);
4997 }
4998
4999 /**
5000  * Declare creation of striped object.
5001  *
5002  * The function declares creation stripes for a regular object. The function
5003  * also declares whether the stripes will be created with non-zero size if
5004  * previously size was set non-zero on the master object. If object \a dt is
5005  * not local, then only fully defined striping can be applied in \a lovea.
5006  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
5007  * for the details.
5008  *
5009  * \param[in] env       execution environment
5010  * \param[in] dt        object
5011  * \param[in] attr      attributes the stripes will be created with
5012  * \param[in] lovea     a buffer containing striping description
5013  * \param[in] th        transaction handle
5014  *
5015  * \retval              0 on success
5016  * \retval              negative if failed
5017  */
5018 int lod_declare_striped_create(const struct lu_env *env, struct dt_object *dt,
5019                                struct lu_attr *attr,
5020                                const struct lu_buf *lovea, struct thandle *th)
5021 {
5022         struct lod_thread_info  *info = lod_env_info(env);
5023         struct dt_object        *next = dt_object_child(dt);
5024         struct lod_object       *lo = lod_dt_obj(dt);
5025         int                      rc;
5026         ENTRY;
5027
5028         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
5029                 GOTO(out, rc = -ENOMEM);
5030
5031         if (!dt_object_remote(next)) {
5032                 /* choose OST and generate appropriate objects */
5033                 rc = lod_prepare_create(env, lo, attr, lovea, th);
5034                 if (rc)
5035                         GOTO(out, rc);
5036
5037                 /*
5038                  * declare storage for striping data
5039                  */
5040                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5041         } else {
5042                 /* LOD can not choose OST objects for remote objects, i.e.
5043                  * stripes must be ready before that. Right now, it can only
5044                  * happen during migrate, i.e. migrate process needs to create
5045                  * remote regular file (mdd_migrate_create), then the migrate
5046                  * process will provide stripeEA. */
5047                 LASSERT(lovea != NULL);
5048                 info->lti_buf = *lovea;
5049         }
5050
5051         rc = lod_sub_declare_xattr_set(env, next, &info->lti_buf,
5052                                        XATTR_NAME_LOV, 0, th);
5053         if (rc)
5054                 GOTO(out, rc);
5055
5056         /*
5057          * if striping is created with local object's size > 0,
5058          * we have to propagate this size to specific object
5059          * the case is possible only when local object was created previously
5060          */
5061         if (dt_object_exists(next))
5062                 rc = lod_declare_init_size(env, dt, th);
5063
5064 out:
5065         /* failed to create striping or to set initial size, let's reset
5066          * config so that others don't get confused */
5067         if (rc)
5068                 lod_striping_free(env, lo);
5069
5070         RETURN(rc);
5071 }
5072
5073 /**
5074  * Implementation of dt_object_operations::do_declare_create.
5075  *
5076  * The method declares creation of a new object. If the object will be striped,
5077  * then helper functions are called to find FIDs for the stripes, declare
5078  * creation of the stripes and declare initialization of the striping
5079  * information to be stored in the master object.
5080  *
5081  * \see dt_object_operations::do_declare_create() in the API description
5082  * for details.
5083  */
5084 static int lod_declare_create(const struct lu_env *env, struct dt_object *dt,
5085                               struct lu_attr *attr,
5086                               struct dt_allocation_hint *hint,
5087                               struct dt_object_format *dof, struct thandle *th)
5088 {
5089         struct dt_object   *next = dt_object_child(dt);
5090         struct lod_object  *lo = lod_dt_obj(dt);
5091         int                 rc;
5092         ENTRY;
5093
5094         LASSERT(dof);
5095         LASSERT(attr);
5096         LASSERT(th);
5097
5098         /*
5099          * first of all, we declare creation of local object
5100          */
5101         rc = lod_sub_declare_create(env, next, attr, hint, dof, th);
5102         if (rc != 0)
5103                 GOTO(out, rc);
5104
5105         /*
5106          * it's lod_ah_init() that has decided the object will be striped
5107          */
5108         if (dof->dof_type == DFT_REGULAR) {
5109                 /* callers don't want stripes */
5110                 /* XXX: all tricky interactions with ->ah_make_hint() decided
5111                  * to use striping, then ->declare_create() behaving differently
5112                  * should be cleaned */
5113                 if (dof->u.dof_reg.striped != 0)
5114                         rc = lod_declare_striped_create(env, dt, attr,
5115                                                         NULL, th);
5116         } else if (dof->dof_type == DFT_DIR) {
5117                 struct seq_server_site *ss;
5118                 struct lu_buf buf = { NULL };
5119                 struct lu_buf *lmu = NULL;
5120
5121                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
5122
5123                 /* If the parent has default stripeEA, and client
5124                  * did not find it before sending create request,
5125                  * then MDT will return -EREMOTE, and client will
5126                  * retrieve the default stripeEA and re-create the
5127                  * sub directory.
5128                  *
5129                  * Note: if dah_eadata != NULL, it means creating the
5130                  * striped directory with specified stripeEA, then it
5131                  * should ignore the default stripeEA */
5132                 if (hint != NULL && hint->dah_eadata == NULL) {
5133                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
5134                                 GOTO(out, rc = -EREMOTE);
5135
5136                         if (lo->ldo_dir_stripe_offset == -1) {
5137                                 /* child and parent should be in the same MDT */
5138                                 if (hint->dah_parent != NULL &&
5139                                     dt_object_remote(hint->dah_parent))
5140                                         GOTO(out, rc = -EREMOTE);
5141                         } else if (lo->ldo_dir_stripe_offset !=
5142                                    ss->ss_node_id) {
5143                                 struct lod_device *lod;
5144                                 struct lod_tgt_descs *ltd;
5145                                 struct lod_tgt_desc *tgt = NULL;
5146                                 bool found_mdt = false;
5147                                 int i;
5148
5149                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5150                                 ltd = &lod->lod_mdt_descs;
5151                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
5152                                         tgt = LTD_TGT(ltd, i);
5153                                         if (tgt->ltd_index ==
5154                                                 lo->ldo_dir_stripe_offset) {
5155                                                 found_mdt = true;
5156                                                 break;
5157                                         }
5158                                 }
5159
5160                                 /* If the MDT indicated by stripe_offset can be
5161                                  * found, then tell client to resend the create
5162                                  * request to the correct MDT, otherwise return
5163                                  * error to client */
5164                                 if (found_mdt)
5165                                         GOTO(out, rc = -EREMOTE);
5166                                 else
5167                                         GOTO(out, rc = -EINVAL);
5168                         }
5169                 } else if (hint && hint->dah_eadata) {
5170                         lmu = &buf;
5171                         lmu->lb_buf = (void *)hint->dah_eadata;
5172                         lmu->lb_len = hint->dah_eadata_len;
5173                 }
5174
5175                 rc = lod_declare_dir_striping_create(env, dt, attr, lmu, dof,
5176                                                      th);
5177         }
5178 out:
5179         /* failed to create striping or to set initial size, let's reset
5180          * config so that others don't get confused */
5181         if (rc)
5182                 lod_striping_free(env, lo);
5183         RETURN(rc);
5184 }
5185
5186 /**
5187  * Generate component ID for new created component.
5188  *
5189  * \param[in] lo                LOD object
5190  * \param[in] comp_idx          index of ldo_comp_entries
5191  *
5192  * \retval                      component ID on success
5193  * \retval                      LCME_ID_INVAL on failure
5194  */
5195 static __u32 lod_gen_component_id(struct lod_object *lo,
5196                                   int mirror_id, int comp_idx)
5197 {
5198         struct lod_layout_component *lod_comp;
5199         __u32   id, start, end;
5200         int     i;
5201
5202         LASSERT(lo->ldo_comp_entries[comp_idx].llc_id == LCME_ID_INVAL);
5203
5204         lod_obj_inc_layout_gen(lo);
5205         id = lo->ldo_layout_gen;
5206         if (likely(id <= SEQ_ID_MAX))
5207                 RETURN(pflr_id(mirror_id, id & SEQ_ID_MASK));
5208
5209         /* Layout generation wraps, need to check collisions. */
5210         start = id & SEQ_ID_MASK;
5211         end = SEQ_ID_MAX;
5212 again:
5213         for (id = start; id <= end; id++) {
5214                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5215                         lod_comp = &lo->ldo_comp_entries[i];
5216                         if (pflr_id(mirror_id, id) == lod_comp->llc_id)
5217                                 break;
5218                 }
5219                 /* Found the ununsed ID */
5220                 if (i == lo->ldo_comp_cnt)
5221                         RETURN(pflr_id(mirror_id, id));
5222         }
5223         if (end == LCME_ID_MAX) {
5224                 start = 1;
5225                 end = min(lo->ldo_layout_gen & LCME_ID_MASK,
5226                           (__u32)(LCME_ID_MAX - 1));
5227                 goto again;
5228         }
5229
5230         RETURN(LCME_ID_INVAL);
5231 }
5232
5233 /**
5234  * Creation of a striped regular object.
5235  *
5236  * The function is called to create the stripe objects for a regular
5237  * striped file. This can happen at the initial object creation or
5238  * when the caller asks LOD to do so using ->do_xattr_set() method
5239  * (so called late striping). Notice all the information are already
5240  * prepared in the form of the list of objects (ldo_stripe field).
5241  * This is done during declare phase.
5242  *
5243  * \param[in] env       execution environment
5244  * \param[in] dt        object
5245  * \param[in] attr      attributes the stripes will be created with
5246  * \param[in] dof       format of stripes (see OSD API description)
5247  * \param[in] th        transaction handle
5248  *
5249  * \retval              0 on success
5250  * \retval              negative if failed
5251  */
5252 int lod_striped_create(const struct lu_env *env, struct dt_object *dt,
5253                        struct lu_attr *attr, struct dt_object_format *dof,
5254                        struct thandle *th)
5255 {
5256         struct lod_layout_component     *lod_comp;
5257         struct lod_object       *lo = lod_dt_obj(dt);
5258         __u16   mirror_id;
5259         int     rc = 0, i, j;
5260         ENTRY;
5261
5262         LASSERT((lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL) ||
5263                 lo->ldo_is_foreign);
5264
5265         mirror_id = 0; /* non-flr file's mirror_id is 0 */
5266         if (lo->ldo_mirror_count > 1) {
5267                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5268                         lod_comp = &lo->ldo_comp_entries[i];
5269                         if (lod_comp->llc_id != LCME_ID_INVAL &&
5270                             mirror_id_of(lod_comp->llc_id) > mirror_id)
5271                                 mirror_id = mirror_id_of(lod_comp->llc_id);
5272                 }
5273         }
5274
5275         /* create all underlying objects */
5276         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5277                 lod_comp = &lo->ldo_comp_entries[i];
5278
5279                 if (lod_comp->llc_id == LCME_ID_INVAL) {
5280                         /* only the component of FLR layout with more than 1
5281                          * mirror has mirror ID in its component ID.
5282                          */
5283                         if (lod_comp->llc_extent.e_start == 0 &&
5284                             lo->ldo_mirror_count > 1)
5285                                 ++mirror_id;
5286
5287                         lod_comp->llc_id = lod_gen_component_id(lo,
5288                                                                 mirror_id, i);
5289                         if (lod_comp->llc_id == LCME_ID_INVAL)
5290                                 GOTO(out, rc = -ERANGE);
5291                 }
5292
5293                 if (lod_comp_inited(lod_comp))
5294                         continue;
5295
5296                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5297                         lod_comp_set_init(lod_comp);
5298
5299                 if (lov_pattern(lod_comp->llc_pattern) == LOV_PATTERN_MDT)
5300                         lod_comp_set_init(lod_comp);
5301
5302                 if (lod_comp->llc_stripe == NULL)
5303                         continue;
5304
5305                 LASSERT(lod_comp->llc_stripe_count);
5306                 for (j = 0; j < lod_comp->llc_stripe_count; j++) {
5307                         struct dt_object *object = lod_comp->llc_stripe[j];
5308                         LASSERT(object != NULL);
5309                         rc = lod_sub_create(env, object, attr, NULL, dof, th);
5310                         if (rc)
5311                                 GOTO(out, rc);
5312                 }
5313                 lod_comp_set_init(lod_comp);
5314         }
5315
5316         rc = lod_fill_mirrors(lo);
5317         if (rc)
5318                 GOTO(out, rc);
5319
5320         rc = lod_generate_and_set_lovea(env, lo, th);
5321         if (rc)
5322                 GOTO(out, rc);
5323
5324         lo->ldo_comp_cached = 1;
5325         RETURN(0);
5326
5327 out:
5328         lod_striping_free(env, lo);
5329         RETURN(rc);
5330 }
5331
5332 static inline bool lod_obj_is_dom(struct dt_object *dt)
5333 {
5334         struct lod_object *lo = lod_dt_obj(dt);
5335
5336         if (!dt_object_exists(dt_object_child(dt)))
5337                 return false;
5338
5339         if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
5340                 return false;
5341
5342         if (!lo->ldo_comp_cnt)
5343                 return false;
5344
5345         return (lov_pattern(lo->ldo_comp_entries[0].llc_pattern) ==
5346                 LOV_PATTERN_MDT);
5347 }
5348
5349 /**
5350  * Implementation of dt_object_operations::do_create.
5351  *
5352  * If any of preceeding methods (like ->do_declare_create(),
5353  * ->do_ah_init(), etc) chose to create a striped object,
5354  * then this method will create the master and the stripes.
5355  *
5356  * \see dt_object_operations::do_create() in the API description for details.
5357  */
5358 static int lod_create(const struct lu_env *env, struct dt_object *dt,
5359                       struct lu_attr *attr, struct dt_allocation_hint *hint,
5360                       struct dt_object_format *dof, struct thandle *th)
5361 {
5362         int                 rc;
5363         ENTRY;
5364
5365         /* create local object */
5366         rc = lod_sub_create(env, dt_object_child(dt), attr, hint, dof, th);
5367         if (rc != 0)
5368                 RETURN(rc);
5369
5370         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
5371             (lod_obj_is_striped(dt) || lod_obj_is_dom(dt)) &&
5372             dof->u.dof_reg.striped != 0) {
5373                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
5374                 rc = lod_striped_create(env, dt, attr, dof, th);
5375         }
5376
5377         RETURN(rc);
5378 }
5379
5380 static inline int
5381 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
5382                           struct dt_object *dt, struct thandle *th,
5383                           int comp_idx, int stripe_idx,
5384                           struct lod_obj_stripe_cb_data *data)
5385 {
5386         if (data->locd_declare)
5387                 return lod_sub_declare_destroy(env, dt, th);
5388         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5389                  stripe_idx == cfs_fail_val)
5390                 return lod_sub_destroy(env, dt, th);
5391         else
5392                 return 0;
5393 }
5394
5395 /**
5396  * Implementation of dt_object_operations::do_declare_destroy.
5397  *
5398  * If the object is a striped directory, then the function declares reference
5399  * removal from the master object (this is an index) to the stripes and declares
5400  * destroy of all the stripes. In all the cases, it declares an intention to
5401  * destroy the object itself.
5402  *
5403  * \see dt_object_operations::do_declare_destroy() in the API description
5404  * for details.
5405  */
5406 static int lod_declare_destroy(const struct lu_env *env, struct dt_object *dt,
5407                                struct thandle *th)
5408 {
5409         struct dt_object   *next = dt_object_child(dt);
5410         struct lod_object  *lo = lod_dt_obj(dt);
5411         struct lod_thread_info *info = lod_env_info(env);
5412         char               *stripe_name = info->lti_key;
5413         int                 rc, i;
5414         ENTRY;
5415
5416         /*
5417          * load striping information, notice we don't do this when object
5418          * is being initialized as we don't need this information till
5419          * few specific cases like destroy, chown
5420          */
5421         rc = lod_striping_load(env, lo);
5422         if (rc)
5423                 RETURN(rc);
5424
5425         /* declare destroy for all underlying objects */
5426         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5427                 rc = next->do_ops->do_index_try(env, next,
5428                                                 &dt_directory_features);
5429                 if (rc != 0)
5430                         RETURN(rc);
5431
5432                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5433                         rc = lod_sub_declare_ref_del(env, next, th);
5434                         if (rc != 0)
5435                                 RETURN(rc);
5436
5437                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5438                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5439                                 i);
5440                         rc = lod_sub_declare_delete(env, next,
5441                                         (const struct dt_key *)stripe_name, th);
5442                         if (rc != 0)
5443                                 RETURN(rc);
5444                 }
5445         }
5446
5447         /*
5448          * we declare destroy for the local object
5449          */
5450         rc = lod_sub_declare_destroy(env, next, th);
5451         if (rc)
5452                 RETURN(rc);
5453
5454         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5455             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5456                 RETURN(0);
5457
5458         if (!lod_obj_is_striped(dt))
5459                 RETURN(0);
5460
5461         /* declare destroy all striped objects */
5462         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5463                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5464                         if (lo->ldo_stripe[i] == NULL)
5465                                 continue;
5466
5467                         rc = lod_sub_declare_ref_del(env, lo->ldo_stripe[i],
5468                                                      th);
5469
5470                         rc = lod_sub_declare_destroy(env, lo->ldo_stripe[i],
5471                                                      th);
5472                         if (rc != 0)
5473                                 break;
5474                 }
5475         } else {
5476                 struct lod_obj_stripe_cb_data data = { { 0 } };
5477
5478                 data.locd_declare = true;
5479                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5480                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5481         }
5482
5483         RETURN(rc);
5484 }
5485
5486 /**
5487  * Implementation of dt_object_operations::do_destroy.
5488  *
5489  * If the object is a striped directory, then the function removes references
5490  * from the master object (this is an index) to the stripes and destroys all
5491  * the stripes. In all the cases, the function destroys the object itself.
5492  *
5493  * \see dt_object_operations::do_destroy() in the API description for details.
5494  */
5495 static int lod_destroy(const struct lu_env *env, struct dt_object *dt,
5496                        struct thandle *th)
5497 {
5498         struct dt_object  *next = dt_object_child(dt);
5499         struct lod_object *lo = lod_dt_obj(dt);
5500         struct lod_thread_info *info = lod_env_info(env);
5501         char               *stripe_name = info->lti_key;
5502         unsigned int       i;
5503         int                rc;
5504         ENTRY;
5505
5506         /* destroy sub-stripe of master object */
5507         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5508                 rc = next->do_ops->do_index_try(env, next,
5509                                                 &dt_directory_features);
5510                 if (rc != 0)
5511                         RETURN(rc);
5512
5513                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5514                         rc = lod_sub_ref_del(env, next, th);
5515                         if (rc != 0)
5516                                 RETURN(rc);
5517
5518                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
5519                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
5520                                 i);
5521
5522                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
5523                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
5524                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
5525
5526                         rc = lod_sub_delete(env, next,
5527                                        (const struct dt_key *)stripe_name, th);
5528                         if (rc != 0)
5529                                 RETURN(rc);
5530                 }
5531         }
5532
5533         rc = lod_sub_destroy(env, next, th);
5534         if (rc != 0)
5535                 RETURN(rc);
5536
5537         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
5538             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
5539                 RETURN(0);
5540
5541         if (!lod_obj_is_striped(dt))
5542                 RETURN(0);
5543
5544         /* destroy all striped objects */
5545         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
5546                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5547                         if (lo->ldo_stripe[i] == NULL)
5548                                 continue;
5549                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
5550                             i == cfs_fail_val) {
5551                                 dt_write_lock(env, lo->ldo_stripe[i],
5552                                               MOR_TGT_CHILD);
5553                                 rc = lod_sub_ref_del(env, lo->ldo_stripe[i],
5554                                                      th);
5555                                 dt_write_unlock(env, lo->ldo_stripe[i]);
5556                                 if (rc != 0)
5557                                         break;
5558
5559                                 rc = lod_sub_destroy(env, lo->ldo_stripe[i],
5560                                                      th);
5561                                 if (rc != 0)
5562                                         break;
5563                         }
5564                 }
5565         } else {
5566                 struct lod_obj_stripe_cb_data data = { { 0 } };
5567
5568                 data.locd_declare = false;
5569                 data.locd_stripe_cb = lod_obj_stripe_destroy_cb;
5570                 rc = lod_obj_for_each_stripe(env, lo, th, &data);
5571         }
5572
5573         RETURN(rc);
5574 }
5575
5576 /**
5577  * Implementation of dt_object_operations::do_declare_ref_add.
5578  *
5579  * \see dt_object_operations::do_declare_ref_add() in the API description
5580  * for details.
5581  */
5582 static int lod_declare_ref_add(const struct lu_env *env,
5583                                struct dt_object *dt, struct thandle *th)
5584 {
5585         return lod_sub_declare_ref_add(env, dt_object_child(dt), th);
5586 }
5587
5588 /**
5589  * Implementation of dt_object_operations::do_ref_add.
5590  *
5591  * \see dt_object_operations::do_ref_add() in the API description for details.
5592  */
5593 static int lod_ref_add(const struct lu_env *env,
5594                        struct dt_object *dt, struct thandle *th)
5595 {
5596         return lod_sub_ref_add(env, dt_object_child(dt), th);
5597 }
5598
5599 /**
5600  * Implementation of dt_object_operations::do_declare_ref_del.
5601  *
5602  * \see dt_object_operations::do_declare_ref_del() in the API description
5603  * for details.
5604  */
5605 static int lod_declare_ref_del(const struct lu_env *env,
5606                                struct dt_object *dt, struct thandle *th)
5607 {
5608         return lod_sub_declare_ref_del(env, dt_object_child(dt), th);
5609 }
5610
5611 /**
5612  * Implementation of dt_object_operations::do_ref_del
5613  *
5614  * \see dt_object_operations::do_ref_del() in the API description for details.
5615  */
5616 static int lod_ref_del(const struct lu_env *env,
5617                        struct dt_object *dt, struct thandle *th)
5618 {
5619         return lod_sub_ref_del(env, dt_object_child(dt), th);
5620 }
5621
5622 /**
5623  * Implementation of dt_object_operations::do_object_sync.
5624  *
5625  * \see dt_object_operations::do_object_sync() in the API description
5626  * for details.
5627  */
5628 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
5629                            __u64 start, __u64 end)
5630 {
5631         return dt_object_sync(env, dt_object_child(dt), start, end);
5632 }
5633
5634 /**
5635  * Implementation of dt_object_operations::do_object_unlock.
5636  *
5637  * Used to release LDLM lock(s).
5638  *
5639  * \see dt_object_operations::do_object_unlock() in the API description
5640  * for details.
5641  */
5642 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
5643                              struct ldlm_enqueue_info *einfo,
5644                              union ldlm_policy_data *policy)
5645 {
5646         struct lod_object *lo = lod_dt_obj(dt);
5647         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
5648         int slave_locks_size;
5649         int i;
5650         ENTRY;
5651
5652         if (slave_locks == NULL)
5653                 RETURN(0);
5654
5655         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
5656         /* Note: for remote lock for single stripe dir, MDT will cancel
5657          * the lock by lockh directly */
5658         LASSERT(!dt_object_remote(dt_object_child(dt)));
5659
5660         /* locks were unlocked in MDT layer */
5661         for (i = 0; i < slave_locks->ha_count; i++)
5662                 LASSERT(!lustre_handle_is_used(&slave_locks->ha_handles[i]));
5663
5664         /*
5665          * NB, ha_count may not equal to ldo_dir_stripe_count, because dir
5666          * layout may change, e.g., shrink dir layout after migration.
5667          */
5668         for (i = 0; i < lo->ldo_dir_stripe_count; i++)
5669                 dt_invalidate(env, lo->ldo_stripe[i]);
5670
5671         slave_locks_size = offsetof(typeof(*slave_locks),
5672                                     ha_handles[slave_locks->ha_count]);
5673         OBD_FREE(slave_locks, slave_locks_size);
5674         einfo->ei_cbdata = NULL;
5675
5676         RETURN(0);
5677 }
5678
5679 /**
5680  * Implementation of dt_object_operations::do_object_lock.
5681  *
5682  * Used to get LDLM lock on the non-striped and striped objects.
5683  *
5684  * \see dt_object_operations::do_object_lock() in the API description
5685  * for details.
5686  */
5687 static int lod_object_lock(const struct lu_env *env,
5688                            struct dt_object *dt,
5689                            struct lustre_handle *lh,
5690                            struct ldlm_enqueue_info *einfo,
5691                            union ldlm_policy_data *policy)
5692 {
5693         struct lod_object *lo = lod_dt_obj(dt);
5694         int slave_locks_size;
5695         struct lustre_handle_array *slave_locks = NULL;
5696         int i;
5697         int rc;
5698         ENTRY;
5699
5700         /* remote object lock */
5701         if (!einfo->ei_enq_slave) {
5702                 LASSERT(dt_object_remote(dt));
5703                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
5704                                       policy);
5705         }
5706
5707         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
5708                 RETURN(-ENOTDIR);
5709
5710         rc = lod_striping_load(env, lo);
5711         if (rc != 0)
5712                 RETURN(rc);
5713
5714         /* No stripes */
5715         if (lo->ldo_dir_stripe_count <= 1)
5716                 RETURN(0);
5717
5718         slave_locks_size = offsetof(typeof(*slave_locks),
5719                                     ha_handles[lo->ldo_dir_stripe_count]);
5720         /* Freed in lod_object_unlock */
5721         OBD_ALLOC(slave_locks, slave_locks_size);
5722         if (!slave_locks)
5723                 RETURN(-ENOMEM);
5724         slave_locks->ha_count = lo->ldo_dir_stripe_count;
5725
5726         /* striped directory lock */
5727         for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
5728                 struct lustre_handle lockh;
5729                 struct ldlm_res_id *res_id;
5730
5731                 res_id = &lod_env_info(env)->lti_res_id;
5732                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
5733                                        res_id);
5734                 einfo->ei_res_id = res_id;
5735
5736                 LASSERT(lo->ldo_stripe[i] != NULL);
5737                 if (dt_object_remote(lo->ldo_stripe[i])) {
5738                         set_bit(i, (void *)slave_locks->ha_map);
5739                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
5740                                             einfo, policy);
5741                 } else {
5742                         struct ldlm_namespace *ns = einfo->ei_namespace;
5743                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
5744                         ldlm_completion_callback completion = einfo->ei_cb_cp;
5745                         __u64 dlmflags = LDLM_FL_ATOMIC_CB;
5746
5747                         if (einfo->ei_mode == LCK_PW ||
5748                             einfo->ei_mode == LCK_EX)
5749                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
5750
5751                         LASSERT(ns != NULL);
5752                         rc = ldlm_cli_enqueue_local(env, ns, res_id, LDLM_IBITS,
5753                                                     policy, einfo->ei_mode,
5754                                                     &dlmflags, blocking,
5755                                                     completion, NULL,
5756                                                     NULL, 0, LVB_T_NONE,
5757                                                     NULL, &lockh);
5758                 }
5759                 if (rc) {
5760                         while (i--)
5761                                 ldlm_lock_decref_and_cancel(
5762                                                 &slave_locks->ha_handles[i],
5763                                                 einfo->ei_mode);
5764                         OBD_FREE(slave_locks, slave_locks_size);
5765                         RETURN(rc);
5766                 }
5767                 slave_locks->ha_handles[i] = lockh;
5768         }
5769         einfo->ei_cbdata = slave_locks;
5770
5771         RETURN(0);
5772 }
5773
5774 /**
5775  * Implementation of dt_object_operations::do_invalidate.
5776  *
5777  * \see dt_object_operations::do_invalidate() in the API description for details
5778  */
5779 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
5780 {
5781         return dt_invalidate(env, dt_object_child(dt));
5782 }
5783
5784 static int lod_layout_data_init(struct lod_thread_info *info, __u32 comp_cnt)
5785 {
5786         ENTRY;
5787
5788         /* clear memory region that will be used for layout change */
5789         memset(&info->lti_layout_attr, 0, sizeof(struct lu_attr));
5790         info->lti_count = 0;
5791
5792         if (info->lti_comp_size >= comp_cnt)
5793                 RETURN(0);
5794
5795         if (info->lti_comp_size > 0) {
5796                 OBD_FREE(info->lti_comp_idx,
5797                          info->lti_comp_size * sizeof(__u32));
5798                 info->lti_comp_size = 0;
5799         }
5800
5801         OBD_ALLOC(info->lti_comp_idx, comp_cnt * sizeof(__u32));
5802         if (!info->lti_comp_idx)
5803                 RETURN(-ENOMEM);
5804
5805         info->lti_comp_size = comp_cnt;
5806         RETURN(0);
5807 }
5808
5809 static int lod_declare_instantiate_components(const struct lu_env *env,
5810                 struct lod_object *lo, struct thandle *th)
5811 {
5812         struct lod_thread_info *info = lod_env_info(env);
5813         int i;
5814         int rc = 0;
5815         ENTRY;
5816
5817         LASSERT(info->lti_count < lo->ldo_comp_cnt);
5818
5819         for (i = 0; i < info->lti_count; i++) {
5820                 rc = lod_qos_prep_create(env, lo, NULL, th,
5821                                          info->lti_comp_idx[i]);
5822                 if (rc)
5823                         break;
5824         }
5825
5826         if (!rc) {
5827                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
5828                 rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
5829                                 &info->lti_buf, XATTR_NAME_LOV, 0, th);
5830         }
5831
5832         RETURN(rc);
5833 }
5834
5835 static int lod_declare_update_plain(const struct lu_env *env,
5836                 struct lod_object *lo, struct layout_intent *layout,
5837                 const struct lu_buf *buf, struct thandle *th)
5838 {
5839         struct lod_thread_info *info = lod_env_info(env);
5840         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
5841         struct lod_layout_component *lod_comp;
5842         struct lov_comp_md_v1 *comp_v1 = NULL;
5843         bool replay = false;
5844         int i, rc;
5845         ENTRY;
5846
5847         LASSERT(lo->ldo_flr_state == LCM_FL_NONE);
5848
5849         /*
5850          * In case the client is passing lovea, which only happens during
5851          * the replay of layout intent write RPC for now, we may need to
5852          * parse the lovea and apply new layout configuration.
5853          */
5854         if (buf && buf->lb_len)  {
5855                 struct lov_user_md_v1 *v1 = buf->lb_buf;
5856
5857                 if (v1->lmm_magic != (LOV_MAGIC_DEFINED | LOV_MAGIC_COMP_V1) &&
5858                     v1->lmm_magic != __swab32(LOV_MAGIC_DEFINED |
5859                                               LOV_MAGIC_COMP_V1)) {
5860                         CERROR("%s: the replay buffer of layout extend "
5861                                "(magic %#x) does not contain expected "
5862                                "composite layout.\n",
5863                                lod2obd(d)->obd_name, v1->lmm_magic);
5864                         GOTO(out, rc = -EINVAL);
5865                 }
5866
5867                 rc = lod_use_defined_striping(env, lo, buf);
5868                 if (rc)
5869                         GOTO(out, rc);
5870                 lo->ldo_comp_cached = 1;
5871
5872                 rc = lod_get_lov_ea(env, lo);
5873                 if (rc <= 0)
5874                         GOTO(out, rc);
5875                 /* old on-disk EA is stored in info->lti_buf */
5876                 comp_v1 = (struct lov_comp_md_v1 *)info->lti_buf.lb_buf;
5877                 replay = true;
5878         } else {
5879                 /* non replay path */
5880                 rc = lod_striping_load(env, lo);
5881                 if (rc)
5882                         GOTO(out, rc);
5883         }
5884
5885         /* Make sure defined layout covers the requested write range. */
5886         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
5887         if (lo->ldo_comp_cnt > 1 &&
5888             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
5889             lod_comp->llc_extent.e_end < layout->li_extent.e_end) {
5890                 CDEBUG(replay ? D_ERROR : D_LAYOUT,
5891                        "%s: the defined layout [0, %#llx) does not covers "
5892                        "the write range "DEXT"\n",
5893                        lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
5894                        PEXT(&layout->li_extent));
5895                 GOTO(out, rc = -EINVAL);
5896         }
5897
5898         CDEBUG(D_LAYOUT, "%s: "DFID": instantiate components "DEXT"\n",
5899                lod2obd(d)->obd_name, PFID(lod_object_fid(lo)),
5900                PEXT(&layout->li_extent));
5901
5902         /*
5903          * Iterate ld->ldo_comp_entries, find the component whose extent under
5904          * the write range and not instantianted.
5905          */
5906         for (i = 0; i < lo->ldo_comp_cnt; i++) {
5907                 lod_comp = &lo->ldo_comp_entries[i];
5908
5909                 if (lod_comp->llc_extent.e_start >= layout->li_extent.e_end)
5910                         break;
5911
5912                 if (!replay) {
5913                         if (lod_comp_inited(lod_comp))
5914                                 continue;
5915                 } else {
5916                         /**
5917                          * In replay path, lod_comp is the EA passed by
5918                          * client replay buffer,  comp_v1 is the pre-recovery
5919                          * on-disk EA, we'd sift out those components which
5920                          * were init-ed in the on-disk EA.
5921                          */
5922                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
5923                             LCME_FL_INIT)
5924                                 continue;
5925                 }
5926                 /*
5927                  * this component hasn't instantiated in normal path, or during
5928                  * replay it needs replay the instantiation.
5929                  */
5930
5931                 /* A released component is being extended */
5932                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
5933                         GOTO(out, rc = -EINVAL);
5934
5935                 LASSERT(info->lti_comp_idx != NULL);
5936                 info->lti_comp_idx[info->lti_count++] = i;
5937         }
5938
5939         if (info->lti_count == 0)
5940                 RETURN(-EALREADY);
5941
5942         lod_obj_inc_layout_gen(lo);
5943         rc = lod_declare_instantiate_components(env, lo, th);
5944 out:
5945         if (rc)
5946                 lod_striping_free(env, lo);
5947         RETURN(rc);
5948 }
5949
5950 static inline int lod_comp_index(struct lod_object *lo,
5951                                  struct lod_layout_component *lod_comp)
5952 {
5953         LASSERT(lod_comp >= lo->ldo_comp_entries &&
5954                 lod_comp <= &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1]);
5955
5956         return lod_comp - lo->ldo_comp_entries;
5957 }
5958
5959 /**
5960  * Stale other mirrors by writing extent.
5961  */
5962 static void lod_stale_components(struct lod_object *lo, int primary,
5963                                  struct lu_extent *extent)
5964 {
5965         struct lod_layout_component *pri_comp, *lod_comp;
5966         int i;
5967
5968         /* The writing extent decides which components in the primary
5969          * are affected... */
5970         CDEBUG(D_LAYOUT, "primary mirror %d, "DEXT"\n", primary, PEXT(extent));
5971         lod_foreach_mirror_comp(pri_comp, lo, primary) {
5972                 if (!lu_extent_is_overlapped(extent, &pri_comp->llc_extent))
5973                         continue;
5974
5975                 CDEBUG(D_LAYOUT, "primary comp %u "DEXT"\n",
5976                        lod_comp_index(lo, pri_comp),
5977                        PEXT(&pri_comp->llc_extent));
5978
5979                 for (i = 0; i < lo->ldo_mirror_count; i++) {
5980                         if (i == primary)
5981                                 continue;
5982
5983                         /* ... and then stale other components that are
5984                          * overlapping with primary components */
5985                         lod_foreach_mirror_comp(lod_comp, lo, i) {
5986                                 if (!lu_extent_is_overlapped(
5987                                                         &pri_comp->llc_extent,
5988                                                         &lod_comp->llc_extent))
5989                                         continue;
5990
5991                                 CDEBUG(D_LAYOUT, "stale: %u / %u\n",
5992                                       i, lod_comp_index(lo, lod_comp));
5993
5994                                 lod_comp->llc_flags |= LCME_FL_STALE;
5995                                 lo->ldo_mirrors[i].lme_stale = 1;
5996                         }
5997                 }
5998         }
5999 }
6000
6001 /**
6002  * check an OST's availability
6003  * \param[in] env       execution environment
6004  * \param[in] lo        lod object
6005  * \param[in] dt        dt object
6006  * \param[in] index     mirror index
6007  *
6008  * \retval      negative if failed
6009  * \retval      1 if \a dt is available
6010  * \retval      0 if \a dt is not available
6011  */
6012 static inline int lod_check_ost_avail(const struct lu_env *env,
6013                                       struct lod_object *lo,
6014                                       struct dt_object *dt, int index)
6015 {
6016         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6017         struct lod_tgt_desc *ost;
6018         __u32 idx;
6019         int type = LU_SEQ_RANGE_OST;
6020         int rc;
6021
6022         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu), &idx, &type);
6023         if (rc < 0) {
6024                 CERROR("%s: can't locate "DFID":rc = %d\n",
6025                        lod2obd(lod)->obd_name, PFID(lu_object_fid(&dt->do_lu)),
6026                        rc);
6027                 return rc;
6028         }
6029
6030         ost = OST_TGT(lod, idx);
6031         if (ost->ltd_statfs.os_state &
6032                 (OS_STATE_READONLY | OS_STATE_ENOSPC | OS_STATE_ENOINO |
6033                  OS_STATE_NOPRECREATE) ||
6034             ost->ltd_active == 0) {
6035                 CDEBUG(D_LAYOUT, DFID ": mirror %d OST%d unavail, rc = %d\n",
6036                        PFID(lod_object_fid(lo)), index, idx, rc);
6037                 return 0;
6038         }
6039
6040         return 1;
6041 }
6042
6043 /**
6044  * Pick primary mirror for write
6045  * \param[in] env       execution environment
6046  * \param[in] lo        object
6047  * \param[in] extent    write range
6048  */
6049 static int lod_primary_pick(const struct lu_env *env, struct lod_object *lo,
6050                             struct lu_extent *extent)
6051 {
6052         struct lod_device *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
6053         unsigned int seq = 0;
6054         struct lod_layout_component *lod_comp;
6055         int i, j, rc;
6056         int picked = -1, second_pick = -1, third_pick = -1;
6057         ENTRY;
6058
6059         if (OBD_FAIL_CHECK(OBD_FAIL_FLR_RANDOM_PICK_MIRROR)) {
6060                 get_random_bytes(&seq, sizeof(seq));
6061                 seq %= lo->ldo_mirror_count;
6062         }
6063
6064         /**
6065          * Pick a mirror as the primary, and check the availability of OSTs.
6066          *
6067          * This algo can be revised later after knowing the topology of
6068          * cluster.
6069          */
6070         lod_qos_statfs_update(env, lod);
6071         for (i = 0; i < lo->ldo_mirror_count; i++) {
6072                 bool ost_avail = true;
6073                 int index = (i + seq) % lo->ldo_mirror_count;
6074
6075                 if (lo->ldo_mirrors[index].lme_stale) {
6076                         CDEBUG(D_LAYOUT, DFID": mirror %d stale\n",
6077                                PFID(lod_object_fid(lo)), index);
6078                         continue;
6079                 }
6080
6081                 /* 2nd pick is for the primary mirror containing unavail OST */
6082                 if (lo->ldo_mirrors[index].lme_primary && second_pick < 0)
6083                         second_pick = index;
6084
6085                 /* 3rd pick is for non-primary mirror containing unavail OST */
6086                 if (second_pick < 0 && third_pick < 0)
6087                         third_pick = index;
6088
6089                 /**
6090                  * we found a non-primary 1st pick, we'd like to find a
6091                  * potential pirmary mirror.
6092                  */
6093                 if (picked >= 0 && !lo->ldo_mirrors[index].lme_primary)
6094                         continue;
6095
6096                 /* check the availability of OSTs */
6097                 lod_foreach_mirror_comp(lod_comp, lo, index) {
6098                         if (!lod_comp_inited(lod_comp) || !lod_comp->llc_stripe)
6099                                 continue;
6100
6101                         for (j = 0; j < lod_comp->llc_stripe_count; j++) {
6102                                 struct dt_object *dt = lod_comp->llc_stripe[j];
6103
6104                                 rc = lod_check_ost_avail(env, lo, dt, index);
6105                                 if (rc < 0)
6106                                         RETURN(rc);
6107
6108                                 ost_avail = !!rc;
6109                                 if (!ost_avail)
6110                                         break;
6111                         } /* for all dt object in one component */
6112                         if (!ost_avail)
6113                                 break;
6114                 } /* for all components in a mirror */
6115
6116                 /**
6117                  * the OSTs where allocated objects locates in the components
6118                  * of the mirror are available.
6119                  */
6120                 if (!ost_avail)
6121                         continue;
6122
6123                 /* this mirror has all OSTs available */
6124                 picked = index;
6125
6126                 /**
6127                  * primary with all OSTs are available, this is the perfect
6128                  * 1st pick.
6129                  */
6130                 if (lo->ldo_mirrors[index].lme_primary)
6131                         break;
6132         } /* for all mirrors */
6133
6134         /* failed to pick a sound mirror, lower our expectation */
6135         if (picked < 0)
6136                 picked = second_pick;
6137         if (picked < 0)
6138                 picked = third_pick;
6139         if (picked < 0)
6140                 RETURN(-ENODATA);
6141
6142         RETURN(picked);
6143 }
6144
6145 static int lod_prepare_resync_mirror(const struct lu_env *env,
6146                                      struct lod_object *lo,
6147                                      __u16 mirror_id)
6148 {
6149         struct lod_thread_info *info = lod_env_info(env);
6150         struct lod_layout_component *lod_comp;
6151         bool neg = !!(MIRROR_ID_NEG & mirror_id);
6152         int i;
6153
6154         mirror_id &= ~MIRROR_ID_NEG;
6155
6156         for (i = 0; i < lo->ldo_mirror_count; i++) {
6157                 if ((!neg && lo->ldo_mirrors[i].lme_id != mirror_id) ||
6158                     (neg && lo->ldo_mirrors[i].lme_id == mirror_id))
6159                         continue;
6160
6161                 lod_foreach_mirror_comp(lod_comp, lo, i) {
6162                         if (lod_comp_inited(lod_comp))
6163                                 continue;
6164
6165                         info->lti_comp_idx[info->lti_count++] =
6166                                 lod_comp_index(lo, lod_comp);
6167                 }
6168         }
6169
6170         return 0;
6171 }
6172
6173 /**
6174  * figure out the components should be instantiated for resync.
6175  */
6176 static int lod_prepare_resync(const struct lu_env *env, struct lod_object *lo,
6177                               struct lu_extent *extent)
6178 {
6179         struct lod_thread_info *info = lod_env_info(env);
6180         struct lod_layout_component *lod_comp;
6181         unsigned int need_sync = 0;
6182         int i;
6183
6184         CDEBUG(D_LAYOUT,
6185                DFID": instantiate all stale components in "DEXT"\n",
6186                PFID(lod_object_fid(lo)), PEXT(extent));
6187
6188         /**
6189          * instantiate all components within this extent, even non-stale
6190          * components.
6191          */
6192         for (i = 0; i < lo->ldo_mirror_count; i++) {
6193                 if (!lo->ldo_mirrors[i].lme_stale)
6194                         continue;
6195
6196                 lod_foreach_mirror_comp(lod_comp, lo, i) {
6197                         if (!lu_extent_is_overlapped(extent,
6198                                                 &lod_comp->llc_extent))
6199                                 break;
6200
6201                         need_sync++;
6202
6203                         if (lod_comp_inited(lod_comp))
6204                                 continue;
6205
6206                         CDEBUG(D_LAYOUT, "resync instantiate %d / %d\n",
6207                                i, lod_comp_index(lo, lod_comp));
6208                         info->lti_comp_idx[info->lti_count++] =
6209                                         lod_comp_index(lo, lod_comp);
6210                 }
6211         }
6212
6213         return need_sync ? 0 : -EALREADY;
6214 }
6215
6216 static int lod_declare_update_rdonly(const struct lu_env *env,
6217                 struct lod_object *lo, struct md_layout_change *mlc,
6218                 struct thandle *th)
6219 {
6220         struct lod_thread_info *info = lod_env_info(env);
6221         struct lu_attr *layout_attr = &info->lti_layout_attr;
6222         struct lod_layout_component *lod_comp;
6223         struct lu_extent extent = { 0 };
6224         int rc;
6225         ENTRY;
6226
6227         LASSERT(lo->ldo_flr_state == LCM_FL_RDONLY);
6228         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
6229                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
6230         LASSERT(lo->ldo_mirror_count > 0);
6231
6232         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6233                 struct layout_intent *layout = mlc->mlc_intent;
6234                 int picked;
6235
6236                 extent = layout->li_extent;
6237                 CDEBUG(D_LAYOUT, DFID": trying to write :"DEXT"\n",
6238                        PFID(lod_object_fid(lo)), PEXT(&extent));
6239
6240                 picked = lod_primary_pick(env, lo, &extent);
6241                 if (picked < 0)
6242                         RETURN(picked);
6243
6244                 CDEBUG(D_LAYOUT, DFID": picked mirror id %u as primary\n",
6245                        PFID(lod_object_fid(lo)),
6246                        lo->ldo_mirrors[picked].lme_id);
6247
6248                 if (layout->li_opc == LAYOUT_INTENT_TRUNC) {
6249                         /**
6250                          * trunc transfers [0, size) in the intent extent, we'd
6251                          * stale components overlapping [size, eof).
6252                          */
6253                         extent.e_start = extent.e_end;
6254                         extent.e_end = OBD_OBJECT_EOF;
6255                 }
6256
6257                 /* stale overlapping components from other mirrors */
6258                 lod_stale_components(lo, picked, &extent);
6259
6260                 /* restore truncate intent extent */
6261                 if (layout->li_opc == LAYOUT_INTENT_TRUNC)
6262                         extent.e_end = extent.e_start;
6263
6264                 /* instantiate components for the picked mirror, start from 0 */
6265                 extent.e_start = 0;
6266
6267                 lod_foreach_mirror_comp(lod_comp, lo, picked) {
6268                         if (!lu_extent_is_overlapped(&extent,
6269                                                      &lod_comp->llc_extent))
6270                                 break;
6271
6272                         if (lod_comp_inited(lod_comp))
6273                                 continue;
6274
6275                         info->lti_comp_idx[info->lti_count++] =
6276                                                 lod_comp_index(lo, lod_comp);
6277                 }
6278
6279                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6280         } else { /* MD_LAYOUT_RESYNC */
6281                 int i;
6282
6283                 /**
6284                  * could contain multiple non-stale mirrors, so we need to
6285                  * prep uninited all components assuming any non-stale mirror
6286                  * could be picked as the primary mirror.
6287                  */
6288                 if (mlc->mlc_mirror_id == 0) {
6289                         /* normal resync */
6290                         for (i = 0; i < lo->ldo_mirror_count; i++) {
6291                                 if (lo->ldo_mirrors[i].lme_stale)
6292                                         continue;
6293
6294                                 lod_foreach_mirror_comp(lod_comp, lo, i) {
6295                                         if (!lod_comp_inited(lod_comp))
6296                                                 break;
6297
6298                                         if (extent.e_end <
6299                                                 lod_comp->llc_extent.e_end)
6300                                                 extent.e_end =
6301                                                      lod_comp->llc_extent.e_end;
6302                                 }
6303                         }
6304                         rc = lod_prepare_resync(env, lo, &extent);
6305                         if (rc)
6306                                 GOTO(out, rc);
6307                 } else {
6308                         /* mirror write, try to init its all components */
6309                         rc = lod_prepare_resync_mirror(env, lo,
6310                                                        mlc->mlc_mirror_id);
6311                         if (rc)
6312                                 GOTO(out, rc);
6313                 }
6314
6315                 /* change the file state to SYNC_PENDING */
6316                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6317         }
6318
6319         /* Reset the layout version once it's becoming too large.
6320          * This way it can make sure that the layout version is
6321          * monotonously increased in this writing era. */
6322         lod_obj_inc_layout_gen(lo);
6323         if (lo->ldo_layout_gen > (LCME_ID_MAX >> 1)) {
6324                 __u32 layout_version;
6325
6326                 cfs_get_random_bytes(&layout_version, sizeof(layout_version));
6327                 lo->ldo_layout_gen = layout_version & 0xffff;
6328         }
6329
6330         rc = lod_declare_instantiate_components(env, lo, th);
6331         if (rc)
6332                 GOTO(out, rc);
6333
6334         layout_attr->la_valid = LA_LAYOUT_VERSION;
6335         layout_attr->la_layout_version = 0; /* set current version */
6336         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6337                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6338         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6339         if (rc)
6340                 GOTO(out, rc);
6341
6342 out:
6343         if (rc)
6344                 lod_striping_free(env, lo);
6345         RETURN(rc);
6346 }
6347
6348 static int lod_declare_update_write_pending(const struct lu_env *env,
6349                 struct lod_object *lo, struct md_layout_change *mlc,
6350                 struct thandle *th)
6351 {
6352         struct lod_thread_info *info = lod_env_info(env);
6353         struct lu_attr *layout_attr = &info->lti_layout_attr;
6354         struct lod_layout_component *lod_comp;
6355         struct lu_extent extent = { 0 };
6356         int primary = -1;
6357         int i;
6358         int rc;
6359         ENTRY;
6360
6361         LASSERT(lo->ldo_flr_state == LCM_FL_WRITE_PENDING);
6362         LASSERT(mlc->mlc_opc == MD_LAYOUT_WRITE ||
6363                 mlc->mlc_opc == MD_LAYOUT_RESYNC);
6364
6365         /* look for the primary mirror */
6366         for (i = 0; i < lo->ldo_mirror_count; i++) {
6367                 if (lo->ldo_mirrors[i].lme_stale)
6368                         continue;
6369
6370                 LASSERTF(primary < 0, DFID " has multiple primary: %u / %u",
6371                          PFID(lod_object_fid(lo)),
6372                          lo->ldo_mirrors[i].lme_id,
6373                          lo->ldo_mirrors[primary].lme_id);
6374
6375                 primary = i;
6376         }
6377         if (primary < 0) {
6378                 CERROR(DFID ": doesn't have a primary mirror\n",
6379                        PFID(lod_object_fid(lo)));
6380                 GOTO(out, rc = -ENODATA);
6381         }
6382
6383         CDEBUG(D_LAYOUT, DFID": found primary %u\n",
6384                PFID(lod_object_fid(lo)), lo->ldo_mirrors[primary].lme_id);
6385
6386         LASSERT(!lo->ldo_mirrors[primary].lme_stale);
6387
6388         /* for LAYOUT_WRITE opc, it has to do the following operations:
6389          * 1. stale overlapping componets from stale mirrors;
6390          * 2. instantiate components of the primary mirror;
6391          * 3. transfter layout version to all objects of the primary;
6392          *
6393          * for LAYOUT_RESYNC opc, it will do:
6394          * 1. instantiate components of all stale mirrors;
6395          * 2. transfer layout version to all objects to close write era. */
6396
6397         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6398                 LASSERT(mlc->mlc_intent != NULL);
6399
6400                 extent = mlc->mlc_intent->li_extent;
6401
6402                 CDEBUG(D_LAYOUT, DFID": intent to write: "DEXT"\n",
6403                        PFID(lod_object_fid(lo)), PEXT(&extent));
6404
6405                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC) {
6406                         /**
6407                          * trunc transfers [0, size) in the intent extent, we'd
6408                          * stale components overlapping [size, eof).
6409                          */
6410                         extent.e_start = extent.e_end;
6411                         extent.e_end = OBD_OBJECT_EOF;
6412                 }
6413                 /* 1. stale overlapping components */
6414                 lod_stale_components(lo, primary, &extent);
6415
6416                 /* 2. find out the components need instantiating.
6417                  * instantiate [0, mlc->mlc_intent->e_end) */
6418
6419                 /* restore truncate intent extent */
6420                 if (mlc->mlc_intent->li_opc == LAYOUT_INTENT_TRUNC)
6421                         extent.e_end = extent.e_start;
6422                 extent.e_start = 0;
6423
6424                 lod_foreach_mirror_comp(lod_comp, lo, primary) {
6425                         if (!lu_extent_is_overlapped(&extent,
6426                                                      &lod_comp->llc_extent))
6427                                 break;
6428
6429                         if (lod_comp_inited(lod_comp))
6430                                 continue;
6431
6432                         CDEBUG(D_LAYOUT, "write instantiate %d / %d\n",
6433                                primary, lod_comp_index(lo, lod_comp));
6434                         info->lti_comp_idx[info->lti_count++] =
6435                                                 lod_comp_index(lo, lod_comp);
6436                 }
6437         } else { /* MD_LAYOUT_RESYNC */
6438                 if (mlc->mlc_mirror_id == 0) {
6439                         /* normal resync */
6440                         lod_foreach_mirror_comp(lod_comp, lo, primary) {
6441                                 if (!lod_comp_inited(lod_comp))
6442                                         break;
6443
6444                                 extent.e_end = lod_comp->llc_extent.e_end;
6445                         }
6446
6447                         rc = lod_prepare_resync(env, lo, &extent);
6448                         if (rc)
6449                                 GOTO(out, rc);
6450                 } else {
6451                         /* mirror write, try to init its all components */
6452                         rc = lod_prepare_resync_mirror(env, lo,
6453                                                        mlc->mlc_mirror_id);
6454                         if (rc)
6455                                 GOTO(out, rc);
6456                 }
6457
6458                 /* change the file state to SYNC_PENDING */
6459                 lo->ldo_flr_state = LCM_FL_SYNC_PENDING;
6460         }
6461
6462         rc = lod_declare_instantiate_components(env, lo, th);
6463         if (rc)
6464                 GOTO(out, rc);
6465
6466         /* 3. transfer layout version to OST objects.
6467          * transfer new layout version to OST objects so that stale writes
6468          * can be denied. It also ends an era of writing by setting
6469          * LU_LAYOUT_RESYNC. Normal client can never use this bit to
6470          * send write RPC; only resync RPCs could do it. */
6471         layout_attr->la_valid = LA_LAYOUT_VERSION;
6472         layout_attr->la_layout_version = 0; /* set current version */
6473         if (mlc->mlc_opc == MD_LAYOUT_RESYNC)
6474                 layout_attr->la_layout_version = LU_LAYOUT_RESYNC;
6475         rc = lod_declare_attr_set(env, &lo->ldo_obj, layout_attr, th);
6476         if (rc)
6477                 GOTO(out, rc);
6478
6479         lod_obj_inc_layout_gen(lo);
6480 out:
6481         if (rc)
6482                 lod_striping_free(env, lo);
6483         RETURN(rc);
6484 }
6485
6486 static int lod_declare_update_sync_pending(const struct lu_env *env,
6487                 struct lod_object *lo, struct md_layout_change *mlc,
6488                 struct thandle *th)
6489 {
6490         struct lod_thread_info  *info = lod_env_info(env);
6491         unsigned sync_components = 0;
6492         unsigned resync_components = 0;
6493         int i;
6494         int rc;
6495         ENTRY;
6496
6497         LASSERT(lo->ldo_flr_state == LCM_FL_SYNC_PENDING);
6498         LASSERT(mlc->mlc_opc == MD_LAYOUT_RESYNC_DONE ||
6499                 mlc->mlc_opc == MD_LAYOUT_WRITE);
6500
6501         CDEBUG(D_LAYOUT, DFID ": received op %d in sync pending\n",
6502                PFID(lod_object_fid(lo)), mlc->mlc_opc);
6503
6504         if (mlc->mlc_opc == MD_LAYOUT_WRITE) {
6505                 CDEBUG(D_LAYOUT, DFID": cocurrent write to sync pending\n",
6506                        PFID(lod_object_fid(lo)));
6507
6508                 lo->ldo_flr_state = LCM_FL_WRITE_PENDING;
6509                 return lod_declare_update_write_pending(env, lo, mlc, th);
6510         }
6511
6512         /* MD_LAYOUT_RESYNC_DONE */
6513
6514         for (i = 0; i < lo->ldo_comp_cnt; i++) {
6515                 struct lod_layout_component *lod_comp;
6516                 int j;
6517
6518                 lod_comp = &lo->ldo_comp_entries[i];
6519
6520                 if (!(lod_comp->llc_flags & LCME_FL_STALE)) {
6521                         sync_components++;
6522                         continue;
6523                 }
6524
6525                 for (j = 0; j < mlc->mlc_resync_count; j++) {
6526                         if (lod_comp->llc_id != mlc->mlc_resync_ids[j])
6527                                 continue;
6528
6529                         mlc->mlc_resync_ids[j] = LCME_ID_INVAL;
6530                         lod_comp->llc_flags &= ~LCME_FL_STALE;
6531                         resync_components++;
6532                         break;
6533                 }
6534         }
6535
6536         /* valid check */
6537         for (i = 0; i < mlc->mlc_resync_count; i++) {
6538                 if (mlc->mlc_resync_ids[i] == LCME_ID_INVAL)
6539                         continue;
6540
6541                 CDEBUG(D_LAYOUT, DFID": lcme id %u (%d / %zd) not exist "
6542                        "or already synced\n", PFID(lod_object_fid(lo)),
6543                        mlc->mlc_resync_ids[i], i, mlc->mlc_resync_count);
6544                 GOTO(out, rc = -EINVAL);
6545         }
6546
6547         if (!sync_components || (mlc->mlc_resync_count && !resync_components)) {
6548                 CDEBUG(D_LAYOUT, DFID": no mirror in sync\n",
6549                        PFID(lod_object_fid(lo)));
6550
6551                 /* tend to return an error code here to prevent
6552                  * the MDT from setting SoM attribute */
6553                 GOTO(out, rc = -EINVAL);
6554         }
6555
6556         CDEBUG(D_LAYOUT, DFID": resynced %u/%zu components\n",
6557                PFID(lod_object_fid(lo)),
6558                resync_components, mlc->mlc_resync_count);
6559
6560         lo->ldo_flr_state = LCM_FL_RDONLY;
6561         lod_obj_inc_layout_gen(lo);
6562
6563         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
6564         rc = lod_sub_declare_xattr_set(env, lod_object_child(lo),
6565                                        &info->lti_buf, XATTR_NAME_LOV, 0, th);
6566         EXIT;
6567
6568 out:
6569         if (rc)
6570                 lod_striping_free(env, lo);
6571         RETURN(rc);
6572 }
6573
6574 static int lod_declare_layout_change(const struct lu_env *env,
6575                 struct dt_object *dt, struct md_layout_change *mlc,
6576                 struct thandle *th)
6577 {
6578         struct lod_thread_info  *info = lod_env_info(env);
6579         struct lod_object *lo = lod_dt_obj(dt);
6580         int rc;
6581         ENTRY;
6582
6583         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
6584             dt_object_remote(dt_object_child(dt)))
6585                 RETURN(-EINVAL);
6586
6587         rc = lod_striping_load(env, lo);
6588         if (rc)
6589                 GOTO(out, rc);
6590
6591         LASSERT(lo->ldo_comp_cnt > 0);
6592
6593         rc = lod_layout_data_init(info, lo->ldo_comp_cnt);
6594         if (rc)
6595                 GOTO(out, rc);
6596
6597         switch (lo->ldo_flr_state) {
6598         case LCM_FL_NONE:
6599                 rc = lod_declare_update_plain(env, lo, mlc->mlc_intent,
6600                                               &mlc->mlc_buf, th);
6601                 break;
6602         case LCM_FL_RDONLY:
6603                 rc = lod_declare_update_rdonly(env, lo, mlc, th);
6604                 break;
6605         case LCM_FL_WRITE_PENDING:
6606                 rc = lod_declare_update_write_pending(env, lo, mlc, th);
6607                 break;
6608         case LCM_FL_SYNC_PENDING:
6609                 rc = lod_declare_update_sync_pending(env, lo, mlc, th);
6610                 break;
6611         default:
6612                 rc = -ENOTSUPP;
6613                 break;
6614         }
6615 out:
6616         RETURN(rc);
6617 }
6618
6619 /**
6620  * Instantiate layout component objects which covers the intent write offset.
6621  */
6622 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
6623                              struct md_layout_change *mlc, struct thandle *th)
6624 {
6625         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
6626         struct lu_attr *layout_attr = &lod_env_info(env)->lti_layout_attr;
6627         struct lod_object *lo = lod_dt_obj(dt);
6628         int rc;
6629
6630         rc = lod_striped_create(env, dt, attr, NULL, th);
6631         if (!rc && layout_attr->la_valid & LA_LAYOUT_VERSION) {
6632                 layout_attr->la_layout_version |= lo->ldo_layout_gen;
6633                 rc = lod_attr_set(env, dt, layout_attr, th);
6634         }
6635
6636         return rc;
6637 }
6638
6639 struct dt_object_operations lod_obj_ops = {
6640         .do_read_lock           = lod_read_lock,
6641         .do_write_lock          = lod_write_lock,
6642         .do_read_unlock         = lod_read_unlock,
6643         .do_write_unlock        = lod_write_unlock,
6644         .do_write_locked        = lod_write_locked,
6645         .do_attr_get            = lod_attr_get,
6646         .do_declare_attr_set    = lod_declare_attr_set,
6647         .do_attr_set            = lod_attr_set,
6648         .do_xattr_get           = lod_xattr_get,
6649         .do_declare_xattr_set   = lod_declare_xattr_set,
6650         .do_xattr_set           = lod_xattr_set,
6651         .do_declare_xattr_del   = lod_declare_xattr_del,
6652         .do_xattr_del           = lod_xattr_del,
6653         .do_xattr_list          = lod_xattr_list,
6654         .do_ah_init             = lod_ah_init,
6655         .do_declare_create      = lod_declare_create,
6656         .do_create              = lod_create,
6657         .do_declare_destroy     = lod_declare_destroy,
6658         .do_destroy             = lod_destroy,
6659         .do_index_try           = lod_index_try,
6660         .do_declare_ref_add     = lod_declare_ref_add,
6661         .do_ref_add             = lod_ref_add,
6662         .do_declare_ref_del     = lod_declare_ref_del,
6663         .do_ref_del             = lod_ref_del,
6664         .do_object_sync         = lod_object_sync,
6665         .do_object_lock         = lod_object_lock,
6666         .do_object_unlock       = lod_object_unlock,
6667         .do_invalidate          = lod_invalidate,
6668         .do_declare_layout_change = lod_declare_layout_change,
6669         .do_layout_change       = lod_layout_change,
6670 };
6671
6672 /**
6673  * Implementation of dt_body_operations::dbo_read.
6674  *
6675  * \see dt_body_operations::dbo_read() in the API description for details.
6676  */
6677 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
6678                         struct lu_buf *buf, loff_t *pos)
6679 {
6680         struct dt_object *next = dt_object_child(dt);
6681
6682         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6683                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6684         return next->do_body_ops->dbo_read(env, next, buf, pos);
6685 }
6686
6687 /**
6688  * Implementation of dt_body_operations::dbo_declare_write.
6689  *
6690  * \see dt_body_operations::dbo_declare_write() in the API description
6691  * for details.
6692  */
6693 static ssize_t lod_declare_write(const struct lu_env *env,
6694                                  struct dt_object *dt,
6695                                  const struct lu_buf *buf, loff_t pos,
6696                                  struct thandle *th)
6697 {
6698         return lod_sub_declare_write(env, dt_object_child(dt), buf, pos, th);
6699 }
6700
6701 /**
6702  * Implementation of dt_body_operations::dbo_write.
6703  *
6704  * \see dt_body_operations::dbo_write() in the API description for details.
6705  */
6706 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
6707                          const struct lu_buf *buf, loff_t *pos,
6708                          struct thandle *th)
6709 {
6710         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr) ||
6711                 S_ISLNK(dt->do_lu.lo_header->loh_attr));
6712         return lod_sub_write(env, dt_object_child(dt), buf, pos, th);
6713 }
6714
6715 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
6716                              __u64 start, __u64 end, struct thandle *th)
6717 {
6718         if (dt_object_remote(dt))
6719                 return -ENOTSUPP;
6720
6721         return lod_sub_declare_punch(env, dt_object_child(dt), start, end, th);
6722 }
6723
6724 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
6725                      __u64 start, __u64 end, struct thandle *th)
6726 {
6727         if (dt_object_remote(dt))
6728                 return -ENOTSUPP;
6729
6730         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
6731         return lod_sub_punch(env, dt_object_child(dt), start, end, th);
6732 }
6733
6734 /*
6735  * different type of files use the same body_ops because object may be created
6736  * in OUT, where there is no chance to set correct body_ops for each type, so
6737  * body_ops themselves will check file type inside, see lod_read/write/punch for
6738  * details.
6739  */
6740 const struct dt_body_operations lod_body_ops = {
6741         .dbo_read               = lod_read,
6742         .dbo_declare_write      = lod_declare_write,
6743         .dbo_write              = lod_write,
6744         .dbo_declare_punch      = lod_declare_punch,
6745         .dbo_punch              = lod_punch,
6746 };
6747
6748 /**
6749  * Implementation of lu_object_operations::loo_object_init.
6750  *
6751  * The function determines the type and the index of the target device using
6752  * sequence of the object's FID. Then passes control down to the
6753  * corresponding device:
6754  *  OSD for the local objects, OSP for remote
6755  *
6756  * \see lu_object_operations::loo_object_init() in the API description
6757  * for details.
6758  */
6759 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
6760                            const struct lu_object_conf *conf)
6761 {
6762         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
6763         struct lu_device        *cdev   = NULL;
6764         struct lu_object        *cobj;
6765         struct lod_tgt_descs    *ltd    = NULL;
6766         struct lod_tgt_desc     *tgt;
6767         u32                      idx    = 0;
6768         int                      type   = LU_SEQ_RANGE_ANY;
6769         int                      rc;
6770         ENTRY;
6771
6772         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
6773         if (rc != 0) {
6774                 /* Note: Sometimes, it will Return EAGAIN here, see
6775                  * ptrlpc_import_delay_req(), which might confuse
6776                  * lu_object_find_at() and make it wait there incorrectly.
6777                  * so we convert it to EIO here.*/
6778                 if (rc == -EAGAIN)
6779                         rc = -EIO;
6780
6781                 RETURN(rc);
6782         }
6783
6784         if (type == LU_SEQ_RANGE_MDT &&
6785             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
6786                 cdev = &lod->lod_child->dd_lu_dev;
6787         } else if (type == LU_SEQ_RANGE_MDT) {
6788                 ltd = &lod->lod_mdt_descs;
6789                 lod_getref(ltd);
6790         } else if (type == LU_SEQ_RANGE_OST) {
6791                 ltd = &lod->lod_ost_descs;
6792                 lod_getref(ltd);
6793         } else {
6794                 LBUG();
6795         }
6796
6797         if (ltd != NULL) {
6798                 if (ltd->ltd_tgts_size > idx &&
6799                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
6800                         tgt = LTD_TGT(ltd, idx);
6801
6802                         LASSERT(tgt != NULL);
6803                         LASSERT(tgt->ltd_tgt != NULL);
6804
6805                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
6806                 }
6807                 lod_putref(lod, ltd);
6808         }
6809
6810         if (unlikely(cdev == NULL))
6811                 RETURN(-ENOENT);
6812
6813         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
6814         if (unlikely(cobj == NULL))
6815                 RETURN(-ENOMEM);
6816
6817         lu2lod_obj(lo)->ldo_obj.do_body_ops = &lod_body_ops;
6818
6819         lu_object_add(lo, cobj);
6820
6821         RETURN(0);
6822 }
6823
6824 /**
6825  *
6826  * Alloc cached foreign LOV
6827  *
6828  * \param[in] lo        object
6829  * \param[in] size      size of foreign LOV
6830  *
6831  * \retval              0 on success
6832  * \retval              negative if failed
6833  */
6834 int lod_alloc_foreign_lov(struct lod_object *lo, size_t size)
6835 {
6836         OBD_ALLOC_LARGE(lo->ldo_foreign_lov, size);
6837         if (lo->ldo_foreign_lov == NULL)
6838                 return -ENOMEM;
6839         lo->ldo_foreign_lov_size = size;
6840         lo->ldo_is_foreign = 1;
6841         return 0;
6842 }
6843
6844 /**
6845  *
6846  * Free cached foreign LOV
6847  *
6848  * \param[in] lo        object
6849  */
6850 void lod_free_foreign_lov(struct lod_object *lo)
6851 {
6852         if (lo->ldo_foreign_lov != NULL)
6853                 OBD_FREE_LARGE(lo->ldo_foreign_lov, lo->ldo_foreign_lov_size);
6854         lo->ldo_foreign_lov = NULL;
6855         lo->ldo_foreign_lov_size = 0;
6856         lo->ldo_is_foreign = 0;
6857 }
6858
6859 /**
6860  *
6861  * Release resources associated with striping.
6862  *
6863  * If the object is striped (regular or directory), then release
6864  * the stripe objects references and free the ldo_stripe array.
6865  *
6866  * \param[in] env       execution environment
6867  * \param[in] lo        object
6868  */
6869 void lod_striping_free_nolock(const struct lu_env *env, struct lod_object *lo)
6870 {
6871         struct lod_layout_component *lod_comp;
6872         int i, j;
6873
6874         if (unlikely(lo->ldo_is_foreign)) {
6875                 lod_free_foreign_lov(lo);
6876                 lo->ldo_comp_cached = 0;
6877         } else if (lo->ldo_stripe != NULL) {
6878                 LASSERT(lo->ldo_comp_entries == NULL);
6879                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
6880
6881                 for (i = 0; i < lo->ldo_dir_stripe_count; i++) {
6882                         if (lo->ldo_stripe[i])
6883                                 dt_object_put(env, lo->ldo_stripe[i]);
6884                 }
6885
6886                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
6887                 OBD_FREE(lo->ldo_stripe, j);
6888                 lo->ldo_stripe = NULL;
6889                 lo->ldo_dir_stripes_allocated = 0;
6890                 lo->ldo_dir_stripe_loaded = 0;
6891                 lo->ldo_dir_stripe_count = 0;
6892         } else if (lo->ldo_comp_entries != NULL) {
6893                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
6894                         /* free lod_layout_component::llc_stripe array */
6895                         lod_comp = &lo->ldo_comp_entries[i];
6896
6897                         if (lod_comp->llc_stripe == NULL)
6898                                 continue;
6899                         LASSERT(lod_comp->llc_stripes_allocated != 0);
6900                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
6901                                 if (lod_comp->llc_stripe[j] != NULL)
6902                                         lu_object_put(env,
6903                                                &lod_comp->llc_stripe[j]->do_lu);
6904                         }
6905                         OBD_FREE(lod_comp->llc_stripe,
6906                                  sizeof(struct dt_object *) *
6907                                  lod_comp->llc_stripes_allocated);
6908                         lod_comp->llc_stripe = NULL;
6909                         OBD_FREE(lod_comp->llc_ost_indices,
6910                                  sizeof(__u32) *
6911                                  lod_comp->llc_stripes_allocated);
6912                         lod_comp->llc_ost_indices = NULL;
6913                         lod_comp->llc_stripes_allocated = 0;
6914                 }
6915                 lod_free_comp_entries(lo);
6916                 lo->ldo_comp_cached = 0;
6917         }
6918 }
6919
6920 void lod_striping_free(const struct lu_env *env, struct lod_object *lo)
6921 {
6922         mutex_lock(&lo->ldo_layout_mutex);
6923         lod_striping_free_nolock(env, lo);
6924         mutex_unlock(&lo->ldo_layout_mutex);
6925 }
6926
6927 /**
6928  * Implementation of lu_object_operations::loo_object_free.
6929  *
6930  * \see lu_object_operations::loo_object_free() in the API description
6931  * for details.
6932  */
6933 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
6934 {
6935         struct lod_object *lo = lu2lod_obj(o);
6936
6937         /* release all underlying object pinned */
6938         lod_striping_free(env, lo);
6939         lu_object_fini(o);
6940         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
6941 }
6942
6943 /**
6944  * Implementation of lu_object_operations::loo_object_release.
6945  *
6946  * \see lu_object_operations::loo_object_release() in the API description
6947  * for details.
6948  */
6949 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
6950 {
6951         /* XXX: shouldn't we release everything here in case if object
6952          * creation failed before? */
6953 }
6954
6955 /**
6956  * Implementation of lu_object_operations::loo_object_print.
6957  *
6958  * \see lu_object_operations::loo_object_print() in the API description
6959  * for details.
6960  */
6961 static int lod_object_print(const struct lu_env *env, void *cookie,
6962                             lu_printer_t p, const struct lu_object *l)
6963 {
6964         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
6965
6966         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
6967 }
6968
6969 struct lu_object_operations lod_lu_obj_ops = {
6970         .loo_object_init        = lod_object_init,
6971         .loo_object_free        = lod_object_free,
6972         .loo_object_release     = lod_object_release,
6973         .loo_object_print       = lod_object_print,
6974 };