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