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