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