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