Whamcloud - gitweb
LU-6475 mdt: race between open and migrate
[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, 2014, 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  * lustre/doc/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 <lustre_ver.h>
46 #include <obd_support.h>
47 #include <lprocfs_status.h>
48
49 #include <lustre_fid.h>
50 #include <lustre_param.h>
51 #include <lustre_fid.h>
52 #include <lustre_lmv.h>
53 #include <md_object.h>
54 #include <lustre_linkea.h>
55
56 #include "lod_internal.h"
57
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
60
61 static const struct dt_body_operations lod_body_lnk_ops;
62 static const struct dt_body_operations lod_body_ops;
63
64 /**
65  * Implementation of dt_index_operations::dio_lookup
66  *
67  * Used with regular (non-striped) objects.
68  *
69  * \see dt_index_operations::dio_lookup() in the API description for details.
70  */
71 static int lod_index_lookup(const struct lu_env *env, struct dt_object *dt,
72                             struct dt_rec *rec, const struct dt_key *key)
73 {
74         struct dt_object *next = dt_object_child(dt);
75         return next->do_index_ops->dio_lookup(env, next, rec, key);
76 }
77
78 /**
79  * Implementation of dt_index_operations::dio_declare_insert.
80  *
81  * Used with regular (non-striped) objects.
82  *
83  * \see dt_index_operations::dio_declare_insert() in the API description
84  * for details.
85  */
86 static int lod_declare_index_insert(const struct lu_env *env,
87                                     struct dt_object *dt,
88                                     const struct dt_rec *rec,
89                                     const struct dt_key *key,
90                                     struct thandle *th)
91 {
92         return lod_sub_object_declare_insert(env, dt_object_child(dt),
93                                              rec, key, th);
94 }
95
96 /**
97  * Implementation of dt_index_operations::dio_insert.
98  *
99  * Used with regular (non-striped) objects
100  *
101  * \see dt_index_operations::dio_insert() in the API description for details.
102  */
103 static int lod_index_insert(const struct lu_env *env,
104                             struct dt_object *dt,
105                             const struct dt_rec *rec,
106                             const struct dt_key *key,
107                             struct thandle *th,
108                             int ign)
109 {
110         return lod_sub_object_index_insert(env, dt_object_child(dt), rec, key,
111                                            th, ign);
112 }
113
114 /**
115  * Implementation of dt_index_operations::dio_declare_delete.
116  *
117  * Used with regular (non-striped) objects.
118  *
119  * \see dt_index_operations::dio_declare_delete() in the API description
120  * for details.
121  */
122 static int lod_declare_index_delete(const struct lu_env *env,
123                                     struct dt_object *dt,
124                                     const struct dt_key *key,
125                                     struct thandle *th)
126 {
127         return lod_sub_object_declare_delete(env, dt_object_child(dt), key,
128                                              th);
129 }
130
131 /**
132  * Implementation of dt_index_operations::dio_delete.
133  *
134  * Used with regular (non-striped) objects.
135  *
136  * \see dt_index_operations::dio_delete() in the API description for details.
137  */
138 static int lod_index_delete(const struct lu_env *env,
139                             struct dt_object *dt,
140                             const struct dt_key *key,
141                             struct thandle *th)
142 {
143         return lod_sub_object_delete(env, dt_object_child(dt), key, th);
144 }
145
146 /**
147  * Implementation of dt_it_ops::init.
148  *
149  * Used with regular (non-striped) objects.
150  *
151  * \see dt_it_ops::init() in the API description for details.
152  */
153 static struct dt_it *lod_it_init(const struct lu_env *env,
154                                  struct dt_object *dt, __u32 attr)
155 {
156         struct dt_object        *next = dt_object_child(dt);
157         struct lod_it           *it = &lod_env_info(env)->lti_it;
158         struct dt_it            *it_next;
159
160         it_next = next->do_index_ops->dio_it.init(env, next, attr);
161         if (IS_ERR(it_next))
162                 return it_next;
163
164         /* currently we do not use more than one iterator per thread
165          * so we store it in thread info. if at some point we need
166          * more active iterators in a single thread, we can allocate
167          * additional ones */
168         LASSERT(it->lit_obj == NULL);
169
170         it->lit_it = it_next;
171         it->lit_obj = next;
172
173         return (struct dt_it *)it;
174 }
175
176 #define LOD_CHECK_IT(env, it)                                   \
177 do {                                                            \
178         LASSERT((it)->lit_obj != NULL);                         \
179         LASSERT((it)->lit_it != NULL);                          \
180 } while (0)
181
182 /**
183  * Implementation of dt_index_operations::dio_it.fini.
184  *
185  * Used with regular (non-striped) objects.
186  *
187  * \see dt_index_operations::dio_it.fini() in the API description for details.
188  */
189 static void lod_it_fini(const struct lu_env *env, struct dt_it *di)
190 {
191         struct lod_it *it = (struct lod_it *)di;
192
193         LOD_CHECK_IT(env, it);
194         it->lit_obj->do_index_ops->dio_it.fini(env, it->lit_it);
195
196         /* the iterator not in use any more */
197         it->lit_obj = NULL;
198         it->lit_it = NULL;
199 }
200
201 /**
202  * Implementation of dt_it_ops::get.
203  *
204  * Used with regular (non-striped) objects.
205  *
206  * \see dt_it_ops::get() in the API description for details.
207  */
208 static int lod_it_get(const struct lu_env *env, struct dt_it *di,
209                       const struct dt_key *key)
210 {
211         const struct lod_it *it = (const struct lod_it *)di;
212
213         LOD_CHECK_IT(env, it);
214         return it->lit_obj->do_index_ops->dio_it.get(env, it->lit_it, key);
215 }
216
217 /**
218  * Implementation of dt_it_ops::put.
219  *
220  * Used with regular (non-striped) objects.
221  *
222  * \see dt_it_ops::put() in the API description for details.
223  */
224 static void lod_it_put(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.put(env, it->lit_it);
230 }
231
232 /**
233  * Implementation of dt_it_ops::next.
234  *
235  * Used with regular (non-striped) objects
236  *
237  * \see dt_it_ops::next() in the API description for details.
238  */
239 static int lod_it_next(const struct lu_env *env, struct dt_it *di)
240 {
241         struct lod_it *it = (struct lod_it *)di;
242
243         LOD_CHECK_IT(env, it);
244         return it->lit_obj->do_index_ops->dio_it.next(env, it->lit_it);
245 }
246
247 /**
248  * Implementation of dt_it_ops::key.
249  *
250  * Used with regular (non-striped) objects.
251  *
252  * \see dt_it_ops::key() in the API description for details.
253  */
254 static struct dt_key *lod_it_key(const struct lu_env *env,
255                                  const struct dt_it *di)
256 {
257         const struct lod_it *it = (const struct lod_it *)di;
258
259         LOD_CHECK_IT(env, it);
260         return it->lit_obj->do_index_ops->dio_it.key(env, it->lit_it);
261 }
262
263 /**
264  * Implementation of dt_it_ops::key_size.
265  *
266  * Used with regular (non-striped) objects.
267  *
268  * \see dt_it_ops::key_size() in the API description for details.
269  */
270 static int lod_it_key_size(const struct lu_env *env, const struct dt_it *di)
271 {
272         struct lod_it *it = (struct lod_it *)di;
273
274         LOD_CHECK_IT(env, it);
275         return it->lit_obj->do_index_ops->dio_it.key_size(env, it->lit_it);
276 }
277
278 /**
279  * Implementation of dt_it_ops::rec.
280  *
281  * Used with regular (non-striped) objects.
282  *
283  * \see dt_it_ops::rec() in the API description for details.
284  */
285 static int lod_it_rec(const struct lu_env *env, const struct dt_it *di,
286                       struct dt_rec *rec, __u32 attr)
287 {
288         const struct lod_it *it = (const struct lod_it *)di;
289
290         LOD_CHECK_IT(env, it);
291         return it->lit_obj->do_index_ops->dio_it.rec(env, it->lit_it, rec,
292                                                      attr);
293 }
294
295 /**
296  * Implementation of dt_it_ops::rec_size.
297  *
298  * Used with regular (non-striped) objects.
299  *
300  * \see dt_it_ops::rec_size() in the API description for details.
301  */
302 static int lod_it_rec_size(const struct lu_env *env, const struct dt_it *di,
303                            __u32 attr)
304 {
305         const struct lod_it *it = (const struct lod_it *)di;
306
307         LOD_CHECK_IT(env, it);
308         return it->lit_obj->do_index_ops->dio_it.rec_size(env, it->lit_it,
309                                                           attr);
310 }
311
312 /**
313  * Implementation of dt_it_ops::store.
314  *
315  * Used with regular (non-striped) objects.
316  *
317  * \see dt_it_ops::store() in the API description for details.
318  */
319 static __u64 lod_it_store(const struct lu_env *env, const struct dt_it *di)
320 {
321         const struct lod_it *it = (const struct lod_it *)di;
322
323         LOD_CHECK_IT(env, it);
324         return it->lit_obj->do_index_ops->dio_it.store(env, it->lit_it);
325 }
326
327 /**
328  * Implementation of dt_it_ops::load.
329  *
330  * Used with regular (non-striped) objects.
331  *
332  * \see dt_it_ops::load() in the API description for details.
333  */
334 static int lod_it_load(const struct lu_env *env, const struct dt_it *di,
335                        __u64 hash)
336 {
337         const struct lod_it *it = (const struct lod_it *)di;
338
339         LOD_CHECK_IT(env, it);
340         return it->lit_obj->do_index_ops->dio_it.load(env, it->lit_it, hash);
341 }
342
343 /**
344  * Implementation of dt_it_ops::key_rec.
345  *
346  * Used with regular (non-striped) objects.
347  *
348  * \see dt_it_ops::rec() in the API description for details.
349  */
350 static int lod_it_key_rec(const struct lu_env *env, const struct dt_it *di,
351                           void *key_rec)
352 {
353         const struct lod_it *it = (const struct lod_it *)di;
354
355         LOD_CHECK_IT(env, it);
356         return it->lit_obj->do_index_ops->dio_it.key_rec(env, it->lit_it,
357                                                          key_rec);
358 }
359
360 static struct dt_index_operations lod_index_ops = {
361         .dio_lookup             = lod_index_lookup,
362         .dio_declare_insert     = lod_declare_index_insert,
363         .dio_insert             = lod_index_insert,
364         .dio_declare_delete     = lod_declare_index_delete,
365         .dio_delete             = lod_index_delete,
366         .dio_it = {
367                 .init           = lod_it_init,
368                 .fini           = lod_it_fini,
369                 .get            = lod_it_get,
370                 .put            = lod_it_put,
371                 .next           = lod_it_next,
372                 .key            = lod_it_key,
373                 .key_size       = lod_it_key_size,
374                 .rec            = lod_it_rec,
375                 .rec_size       = lod_it_rec_size,
376                 .store          = lod_it_store,
377                 .load           = lod_it_load,
378                 .key_rec        = lod_it_key_rec,
379         }
380 };
381
382 /**
383  * Implementation of dt_it_ops::init.
384  *
385  * Used with striped objects. Internally just initializes the iterator
386  * on the first stripe.
387  *
388  * \see dt_it_ops::init() in the API description for details.
389  */
390 static struct dt_it *lod_striped_it_init(const struct lu_env *env,
391                                          struct dt_object *dt, __u32 attr)
392 {
393         struct lod_object       *lo = lod_dt_obj(dt);
394         struct dt_object        *next;
395         struct lod_it           *it = &lod_env_info(env)->lti_it;
396         struct dt_it            *it_next;
397         ENTRY;
398
399         LASSERT(lo->ldo_stripenr > 0);
400         next = lo->ldo_stripe[0];
401         LASSERT(next != NULL);
402         LASSERT(next->do_index_ops != NULL);
403
404         it_next = next->do_index_ops->dio_it.init(env, next, attr);
405         if (IS_ERR(it_next))
406                 return it_next;
407
408         /* currently we do not use more than one iterator per thread
409          * so we store it in thread info. if at some point we need
410          * more active iterators in a single thread, we can allocate
411          * additional ones */
412         LASSERT(it->lit_obj == NULL);
413
414         it->lit_stripe_index = 0;
415         it->lit_attr = attr;
416         it->lit_it = it_next;
417         it->lit_obj = dt;
418
419         return (struct dt_it *)it;
420 }
421
422 #define LOD_CHECK_STRIPED_IT(env, it, lo)                       \
423 do {                                                            \
424         LASSERT((it)->lit_obj != NULL);                         \
425         LASSERT((it)->lit_it != NULL);                          \
426         LASSERT((lo)->ldo_stripenr > 0);                        \
427         LASSERT((it)->lit_stripe_index < (lo)->ldo_stripenr);   \
428 } while (0)
429
430 /**
431  * Implementation of dt_it_ops::fini.
432  *
433  * Used with striped objects.
434  *
435  * \see dt_it_ops::fini() in the API description for details.
436  */
437 static void lod_striped_it_fini(const struct lu_env *env, struct dt_it *di)
438 {
439         struct lod_it           *it = (struct lod_it *)di;
440         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
441         struct dt_object        *next;
442
443         /* If lit_it == NULL, then it means the sub_it has been finished,
444          * which only happens in failure cases, see lod_striped_it_next() */
445         if (it->lit_it != NULL) {
446                 LOD_CHECK_STRIPED_IT(env, it, lo);
447
448                 next = lo->ldo_stripe[it->lit_stripe_index];
449                 LASSERT(next != NULL);
450                 LASSERT(next->do_index_ops != NULL);
451
452                 next->do_index_ops->dio_it.fini(env, it->lit_it);
453         }
454
455         /* the iterator not in use any more */
456         it->lit_obj = NULL;
457         it->lit_it = NULL;
458         it->lit_stripe_index = 0;
459 }
460
461 /**
462  * Implementation of dt_it_ops::get.
463  *
464  * Right now it's not used widely, only to reset the iterator to the
465  * initial position. It should be possible to implement a full version
466  * which chooses a correct stripe to be able to position with any key.
467  *
468  * \see dt_it_ops::get() in the API description for details.
469  */
470 static int lod_striped_it_get(const struct lu_env *env, struct dt_it *di,
471                               const struct dt_key *key)
472 {
473         const struct lod_it     *it = (const struct lod_it *)di;
474         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
475         struct dt_object        *next;
476         ENTRY;
477
478         LOD_CHECK_STRIPED_IT(env, it, lo);
479
480         next = lo->ldo_stripe[it->lit_stripe_index];
481         LASSERT(next != NULL);
482         LASSERT(next->do_index_ops != NULL);
483
484         return next->do_index_ops->dio_it.get(env, it->lit_it, key);
485 }
486
487 /**
488  * Implementation of dt_it_ops::put.
489  *
490  * Used with striped objects.
491  *
492  * \see dt_it_ops::put() in the API description for details.
493  */
494 static void lod_striped_it_put(const struct lu_env *env, struct dt_it *di)
495 {
496         struct lod_it           *it = (struct lod_it *)di;
497         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
498         struct dt_object        *next;
499
500         LOD_CHECK_STRIPED_IT(env, it, lo);
501
502         next = lo->ldo_stripe[it->lit_stripe_index];
503         LASSERT(next != NULL);
504         LASSERT(next->do_index_ops != NULL);
505
506         return next->do_index_ops->dio_it.put(env, it->lit_it);
507 }
508
509 /**
510  * Implementation of dt_it_ops::next.
511  *
512  * Used with striped objects. When the end of the current stripe is
513  * reached, the method takes the next stripe's iterator.
514  *
515  * \see dt_it_ops::next() in the API description for details.
516  */
517 static int lod_striped_it_next(const struct lu_env *env, struct dt_it *di)
518 {
519         struct lod_it           *it = (struct lod_it *)di;
520         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
521         struct dt_object        *next;
522         struct dt_it            *it_next;
523         int                     rc;
524         ENTRY;
525
526         LOD_CHECK_STRIPED_IT(env, it, lo);
527
528         next = lo->ldo_stripe[it->lit_stripe_index];
529         LASSERT(next != NULL);
530         LASSERT(next->do_index_ops != NULL);
531 again:
532         rc = next->do_index_ops->dio_it.next(env, it->lit_it);
533         if (rc < 0)
534                 RETURN(rc);
535
536         if (rc == 0 && it->lit_stripe_index == 0)
537                 RETURN(rc);
538
539         if (rc == 0 && it->lit_stripe_index > 0) {
540                 struct lu_dirent *ent;
541
542                 ent = (struct lu_dirent *)lod_env_info(env)->lti_key;
543
544                 rc = next->do_index_ops->dio_it.rec(env, it->lit_it,
545                                                     (struct dt_rec *)ent,
546                                                     it->lit_attr);
547                 if (rc != 0)
548                         RETURN(rc);
549
550                 /* skip . and .. for slave stripe */
551                 if ((strncmp(ent->lde_name, ".",
552                              le16_to_cpu(ent->lde_namelen)) == 0 &&
553                      le16_to_cpu(ent->lde_namelen) == 1) ||
554                     (strncmp(ent->lde_name, "..",
555                              le16_to_cpu(ent->lde_namelen)) == 0 &&
556                      le16_to_cpu(ent->lde_namelen) == 2))
557                         goto again;
558
559                 RETURN(rc);
560         }
561
562         /* go to next stripe */
563         if (it->lit_stripe_index + 1 >= lo->ldo_stripenr)
564                 RETURN(1);
565
566         it->lit_stripe_index++;
567
568         next->do_index_ops->dio_it.put(env, it->lit_it);
569         next->do_index_ops->dio_it.fini(env, it->lit_it);
570         it->lit_it = NULL;
571
572         next = lo->ldo_stripe[it->lit_stripe_index];
573         LASSERT(next != NULL);
574         LASSERT(next->do_index_ops != NULL);
575
576         rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
577         if (rc != 0)
578                 RETURN(rc);
579
580         it_next = next->do_index_ops->dio_it.init(env, next, it->lit_attr);
581         if (!IS_ERR(it_next)) {
582                 it->lit_it = it_next;
583                 goto again;
584         } else {
585                 rc = PTR_ERR(it_next);
586         }
587
588         RETURN(rc);
589 }
590
591 /**
592  * Implementation of dt_it_ops::key.
593  *
594  * Used with striped objects.
595  *
596  * \see dt_it_ops::key() in the API description for details.
597  */
598 static struct dt_key *lod_striped_it_key(const struct lu_env *env,
599                                          const struct dt_it *di)
600 {
601         const struct lod_it     *it = (const struct lod_it *)di;
602         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
603         struct dt_object        *next;
604
605         LOD_CHECK_STRIPED_IT(env, it, lo);
606
607         next = lo->ldo_stripe[it->lit_stripe_index];
608         LASSERT(next != NULL);
609         LASSERT(next->do_index_ops != NULL);
610
611         return next->do_index_ops->dio_it.key(env, it->lit_it);
612 }
613
614 /**
615  * Implementation of dt_it_ops::key_size.
616  *
617  * Used with striped objects.
618  *
619  * \see dt_it_ops::size() in the API description for details.
620  */
621 static int lod_striped_it_key_size(const struct lu_env *env,
622                                    const struct dt_it *di)
623 {
624         struct lod_it           *it = (struct lod_it *)di;
625         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
626         struct dt_object        *next;
627
628         LOD_CHECK_STRIPED_IT(env, it, lo);
629
630         next = lo->ldo_stripe[it->lit_stripe_index];
631         LASSERT(next != NULL);
632         LASSERT(next->do_index_ops != NULL);
633
634         return next->do_index_ops->dio_it.key_size(env, it->lit_it);
635 }
636
637 /**
638  * Implementation of dt_it_ops::rec.
639  *
640  * Used with striped objects.
641  *
642  * \see dt_it_ops::rec() in the API description for details.
643  */
644 static int lod_striped_it_rec(const struct lu_env *env, const struct dt_it *di,
645                               struct dt_rec *rec, __u32 attr)
646 {
647         const struct lod_it     *it = (const struct lod_it *)di;
648         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
649         struct dt_object        *next;
650
651         LOD_CHECK_STRIPED_IT(env, it, lo);
652
653         next = lo->ldo_stripe[it->lit_stripe_index];
654         LASSERT(next != NULL);
655         LASSERT(next->do_index_ops != NULL);
656
657         return next->do_index_ops->dio_it.rec(env, it->lit_it, rec, attr);
658 }
659
660 /**
661  * Implementation of dt_it_ops::rec_size.
662  *
663  * Used with striped objects.
664  *
665  * \see dt_it_ops::rec_size() in the API description for details.
666  */
667 static int lod_striped_it_rec_size(const struct lu_env *env,
668                                    const struct dt_it *di, __u32 attr)
669 {
670         struct lod_it           *it = (struct lod_it *)di;
671         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
672         struct dt_object        *next;
673
674         LOD_CHECK_STRIPED_IT(env, it, lo);
675
676         next = lo->ldo_stripe[it->lit_stripe_index];
677         LASSERT(next != NULL);
678         LASSERT(next->do_index_ops != NULL);
679
680         return next->do_index_ops->dio_it.rec_size(env, it->lit_it, attr);
681 }
682
683 /**
684  * Implementation of dt_it_ops::store.
685  *
686  * Used with striped objects.
687  *
688  * \see dt_it_ops::store() in the API description for details.
689  */
690 static __u64 lod_striped_it_store(const struct lu_env *env,
691                                   const struct dt_it *di)
692 {
693         const struct lod_it     *it = (const struct lod_it *)di;
694         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
695         struct dt_object        *next;
696
697         LOD_CHECK_STRIPED_IT(env, it, lo);
698
699         next = lo->ldo_stripe[it->lit_stripe_index];
700         LASSERT(next != NULL);
701         LASSERT(next->do_index_ops != NULL);
702
703         return next->do_index_ops->dio_it.store(env, it->lit_it);
704 }
705
706 /**
707  * Implementation of dt_it_ops::load.
708  *
709  * Used with striped objects.
710  *
711  * \see dt_it_ops::load() in the API description for details.
712  */
713 static int lod_striped_it_load(const struct lu_env *env,
714                                const struct dt_it *di, __u64 hash)
715 {
716         const struct lod_it     *it = (const struct lod_it *)di;
717         struct lod_object       *lo = lod_dt_obj(it->lit_obj);
718         struct dt_object        *next;
719
720         LOD_CHECK_STRIPED_IT(env, it, lo);
721
722         next = lo->ldo_stripe[it->lit_stripe_index];
723         LASSERT(next != NULL);
724         LASSERT(next->do_index_ops != NULL);
725
726         return next->do_index_ops->dio_it.load(env, it->lit_it, hash);
727 }
728
729 static struct dt_index_operations lod_striped_index_ops = {
730         .dio_lookup             = lod_index_lookup,
731         .dio_declare_insert     = lod_declare_index_insert,
732         .dio_insert             = lod_index_insert,
733         .dio_declare_delete     = lod_declare_index_delete,
734         .dio_delete             = lod_index_delete,
735         .dio_it = {
736                 .init           = lod_striped_it_init,
737                 .fini           = lod_striped_it_fini,
738                 .get            = lod_striped_it_get,
739                 .put            = lod_striped_it_put,
740                 .next           = lod_striped_it_next,
741                 .key            = lod_striped_it_key,
742                 .key_size       = lod_striped_it_key_size,
743                 .rec            = lod_striped_it_rec,
744                 .rec_size       = lod_striped_it_rec_size,
745                 .store          = lod_striped_it_store,
746                 .load           = lod_striped_it_load,
747         }
748 };
749
750 /**
751  * Append the FID for each shard of the striped directory after the
752  * given LMV EA header.
753  *
754  * To simplify striped directory and the consistency verification,
755  * we only store the LMV EA header on disk, for both master object
756  * and slave objects. When someone wants to know the whole LMV EA,
757  * such as client readdir(), we can build the entrie LMV EA on the
758  * MDT side (in RAM) via iterating the sub-directory entries that
759  * are contained in the master object of the stripe directory.
760  *
761  * For the master object of the striped directroy, the valid name
762  * for each shard is composed of the ${shard_FID}:${shard_idx}.
763  *
764  * There may be holes in the LMV EA if some shards' name entries
765  * are corrupted or lost.
766  *
767  * \param[in] env       pointer to the thread context
768  * \param[in] lo        pointer to the master object of the striped directory
769  * \param[in] buf       pointer to the lu_buf which will hold the LMV EA
770  * \param[in] resize    whether re-allocate the buffer if it is not big enough
771  *
772  * \retval              positive size of the LMV EA
773  * \retval              0 for nothing to be loaded
774  * \retval              negative error number on failure
775  */
776 int lod_load_lmv_shards(const struct lu_env *env, struct lod_object *lo,
777                         struct lu_buf *buf, bool resize)
778 {
779         struct lu_dirent        *ent    =
780                         (struct lu_dirent *)lod_env_info(env)->lti_key;
781         struct lod_device       *lod    = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
782         struct dt_object        *obj    = dt_object_child(&lo->ldo_obj);
783         struct lmv_mds_md_v1    *lmv1   = buf->lb_buf;
784         struct dt_it            *it;
785         const struct dt_it_ops  *iops;
786         __u32                    stripes;
787         __u32                    magic  = le32_to_cpu(lmv1->lmv_magic);
788         size_t                   lmv1_size;
789         int                      rc;
790         ENTRY;
791
792         /* If it is not a striped directory, then load nothing. */
793         if (magic != LMV_MAGIC_V1)
794                 RETURN(0);
795
796         /* If it is in migration (or failure), then load nothing. */
797         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
798                 RETURN(0);
799
800         stripes = le32_to_cpu(lmv1->lmv_stripe_count);
801         if (stripes < 1)
802                 RETURN(0);
803
804         rc = lmv_mds_md_size(stripes, magic);
805         if (rc < 0)
806                 RETURN(rc);
807         lmv1_size = rc;
808         if (buf->lb_len < lmv1_size) {
809                 struct lu_buf tbuf;
810
811                 if (!resize)
812                         RETURN(-ERANGE);
813
814                 tbuf = *buf;
815                 buf->lb_buf = NULL;
816                 buf->lb_len = 0;
817                 lu_buf_alloc(buf, lmv1_size);
818                 lmv1 = buf->lb_buf;
819                 if (lmv1 == NULL)
820                         RETURN(-ENOMEM);
821
822                 memcpy(buf->lb_buf, tbuf.lb_buf, tbuf.lb_len);
823         }
824
825         if (unlikely(!dt_try_as_dir(env, obj)))
826                 RETURN(-ENOTDIR);
827
828         memset(&lmv1->lmv_stripe_fids[0], 0, stripes * sizeof(struct lu_fid));
829         iops = &obj->do_index_ops->dio_it;
830         it = iops->init(env, obj, LUDA_64BITHASH);
831         if (IS_ERR(it))
832                 RETURN(PTR_ERR(it));
833
834         rc = iops->load(env, it, 0);
835         if (rc == 0)
836                 rc = iops->next(env, it);
837         else if (rc > 0)
838                 rc = 0;
839
840         while (rc == 0) {
841                 char             name[FID_LEN + 2] = "";
842                 struct lu_fid    fid;
843                 __u32            index;
844                 int              len;
845
846                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
847                 if (rc != 0)
848                         break;
849
850                 rc = -EIO;
851
852                 fid_le_to_cpu(&fid, &ent->lde_fid);
853                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
854                 if (ent->lde_name[0] == '.') {
855                         if (ent->lde_namelen == 1)
856                                 goto next;
857
858                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
859                                 goto next;
860                 }
861
862                 len = snprintf(name, sizeof(name),
863                                DFID":", PFID(&ent->lde_fid));
864                 /* The ent->lde_name is composed of ${FID}:${index} */
865                 if (ent->lde_namelen < len + 1 ||
866                     memcmp(ent->lde_name, name, len) != 0) {
867                         CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
868                                "%s: invalid shard name %.*s with the FID "DFID
869                                " for the striped directory "DFID", %s\n",
870                                lod2obd(lod)->obd_name, ent->lde_namelen,
871                                ent->lde_name, PFID(&fid),
872                                PFID(lu_object_fid(&obj->do_lu)),
873                                lod->lod_lmv_failout ? "failout" : "skip");
874
875                         if (lod->lod_lmv_failout)
876                                 break;
877
878                         goto next;
879                 }
880
881                 index = 0;
882                 do {
883                         if (ent->lde_name[len] < '0' ||
884                             ent->lde_name[len] > '9') {
885                                 CDEBUG(lod->lod_lmv_failout ? D_ERROR : D_INFO,
886                                        "%s: invalid shard name %.*s with the "
887                                        "FID "DFID" for the striped directory "
888                                        DFID", %s\n",
889                                        lod2obd(lod)->obd_name, ent->lde_namelen,
890                                        ent->lde_name, PFID(&fid),
891                                        PFID(lu_object_fid(&obj->do_lu)),
892                                        lod->lod_lmv_failout ?
893                                        "failout" : "skip");
894
895                                 if (lod->lod_lmv_failout)
896                                         break;
897
898                                 goto next;
899                         }
900
901                         index = index * 10 + ent->lde_name[len++] - '0';
902                 } while (len < ent->lde_namelen);
903
904                 if (len == ent->lde_namelen) {
905                         /* Out of LMV EA range. */
906                         if (index >= stripes) {
907                                 CERROR("%s: the shard %.*s for the striped "
908                                        "directory "DFID" is out of the known "
909                                        "LMV EA range [0 - %u], failout\n",
910                                        lod2obd(lod)->obd_name, ent->lde_namelen,
911                                        ent->lde_name,
912                                        PFID(lu_object_fid(&obj->do_lu)),
913                                        stripes - 1);
914
915                                 break;
916                         }
917
918                         /* The slot has been occupied. */
919                         if (!fid_is_zero(&lmv1->lmv_stripe_fids[index])) {
920                                 struct lu_fid fid0;
921
922                                 fid_le_to_cpu(&fid0,
923                                         &lmv1->lmv_stripe_fids[index]);
924                                 CERROR("%s: both the shard "DFID" and "DFID
925                                        " for the striped directory "DFID
926                                        " claim the same LMV EA slot at the "
927                                        "index %d, failout\n",
928                                        lod2obd(lod)->obd_name,
929                                        PFID(&fid0), PFID(&fid),
930                                        PFID(lu_object_fid(&obj->do_lu)), index);
931
932                                 break;
933                         }
934
935                         /* stored as LE mode */
936                         lmv1->lmv_stripe_fids[index] = ent->lde_fid;
937
938 next:
939                         rc = iops->next(env, it);
940                 }
941         }
942
943         iops->put(env, it);
944         iops->fini(env, it);
945
946         RETURN(rc > 0 ? lmv_mds_md_size(stripes, magic) : rc);
947 }
948
949 /**
950  * Implementation of dt_object_operations::do_index_try.
951  *
952  * \see dt_object_operations::do_index_try() in the API description for details.
953  */
954 static int lod_index_try(const struct lu_env *env, struct dt_object *dt,
955                          const struct dt_index_features *feat)
956 {
957         struct lod_object       *lo = lod_dt_obj(dt);
958         struct dt_object        *next = dt_object_child(dt);
959         int                     rc;
960         ENTRY;
961
962         LASSERT(next->do_ops);
963         LASSERT(next->do_ops->do_index_try);
964
965         rc = lod_load_striping_locked(env, lo);
966         if (rc != 0)
967                 RETURN(rc);
968
969         rc = next->do_ops->do_index_try(env, next, feat);
970         if (rc != 0)
971                 RETURN(rc);
972
973         if (lo->ldo_stripenr > 0) {
974                 int i;
975
976                 for (i = 0; i < lo->ldo_stripenr; i++) {
977                         if (dt_object_exists(lo->ldo_stripe[i]) == 0)
978                                 continue;
979                         rc = lo->ldo_stripe[i]->do_ops->do_index_try(env,
980                                                 lo->ldo_stripe[i], feat);
981                         if (rc != 0)
982                                 RETURN(rc);
983                 }
984                 dt->do_index_ops = &lod_striped_index_ops;
985         } else {
986                 dt->do_index_ops = &lod_index_ops;
987         }
988
989         RETURN(rc);
990 }
991
992 /**
993  * Implementation of dt_object_operations::do_read_lock.
994  *
995  * \see dt_object_operations::do_read_lock() in the API description for details.
996  */
997 static void lod_object_read_lock(const struct lu_env *env,
998                                  struct dt_object *dt, unsigned role)
999 {
1000         dt_read_lock(env, dt_object_child(dt), role);
1001 }
1002
1003 /**
1004  * Implementation of dt_object_operations::do_write_lock.
1005  *
1006  * \see dt_object_operations::do_write_lock() in the API description for
1007  * details.
1008  */
1009 static void lod_object_write_lock(const struct lu_env *env,
1010                                   struct dt_object *dt, unsigned role)
1011 {
1012         dt_write_lock(env, dt_object_child(dt), role);
1013 }
1014
1015 /**
1016  * Implementation of dt_object_operations::do_read_unlock.
1017  *
1018  * \see dt_object_operations::do_read_unlock() in the API description for
1019  * details.
1020  */
1021 static void lod_object_read_unlock(const struct lu_env *env,
1022                                    struct dt_object *dt)
1023 {
1024         dt_read_unlock(env, dt_object_child(dt));
1025 }
1026
1027 /**
1028  * Implementation of dt_object_operations::do_write_unlock.
1029  *
1030  * \see dt_object_operations::do_write_unlock() in the API description for
1031  * details.
1032  */
1033 static void lod_object_write_unlock(const struct lu_env *env,
1034                                     struct dt_object *dt)
1035 {
1036         dt_write_unlock(env, dt_object_child(dt));
1037 }
1038
1039 /**
1040  * Implementation of dt_object_operations::do_write_locked.
1041  *
1042  * \see dt_object_operations::do_write_locked() in the API description for
1043  * details.
1044  */
1045 static int lod_object_write_locked(const struct lu_env *env,
1046                                    struct dt_object *dt)
1047 {
1048         return dt_write_locked(env, dt_object_child(dt));
1049 }
1050
1051 /**
1052  * Implementation of dt_object_operations::do_attr_get.
1053  *
1054  * \see dt_object_operations::do_attr_get() in the API description for details.
1055  */
1056 static int lod_attr_get(const struct lu_env *env,
1057                         struct dt_object *dt,
1058                         struct lu_attr *attr)
1059 {
1060         /* Note: for striped directory, client will merge attributes
1061          * from all of the sub-stripes see lmv_merge_attr(), and there
1062          * no MDD logic depend on directory nlink/size/time, so we can
1063          * always use master inode nlink and size for now. */
1064         return dt_attr_get(env, dt_object_child(dt), attr);
1065 }
1066
1067 /**
1068  * Mark all of the striped directory sub-stripes dead.
1069  *
1070  * When a striped object is a subject to removal, we have
1071  * to mark all the stripes to prevent further access to
1072  * them (e.g. create a new file in those). So we mark
1073  * all the stripes with LMV_HASH_FLAG_DEAD. The function
1074  * can be used to declare the changes and to apply them.
1075  * If the object isn't striped, then just return success.
1076  *
1077  * \param[in] env       execution environment
1078  * \param[in] dt        the striped object
1079  * \param[in] handle    transaction handle
1080  * \param[in] declare   whether to declare the change or apply
1081  *
1082  * \retval              0 on success
1083  * \retval              negative if failed
1084  **/
1085 static int lod_mark_dead_object(const struct lu_env *env,
1086                                 struct dt_object *dt,
1087                                 struct thandle *th,
1088                                 bool declare)
1089 {
1090         struct lod_object       *lo = lod_dt_obj(dt);
1091         struct lmv_mds_md_v1    *lmv;
1092         __u32                   dead_hash_type;
1093         int                     rc;
1094         int                     i;
1095
1096         ENTRY;
1097
1098         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
1099                 RETURN(0);
1100
1101         rc = lod_load_striping_locked(env, lo);
1102         if (rc != 0)
1103                 RETURN(rc);
1104
1105         if (lo->ldo_stripenr == 0)
1106                 RETURN(0);
1107
1108         rc = lod_get_lmv_ea(env, lo);
1109         if (rc <= 0)
1110                 RETURN(rc);
1111
1112         lmv = lod_env_info(env)->lti_ea_store;
1113         lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1114         dead_hash_type = le32_to_cpu(lmv->lmv_hash_type) | LMV_HASH_FLAG_DEAD;
1115         lmv->lmv_hash_type = cpu_to_le32(dead_hash_type);
1116         for (i = 0; i < lo->ldo_stripenr; i++) {
1117                 struct lu_buf buf;
1118
1119                 lmv->lmv_master_mdt_index = i;
1120                 buf.lb_buf = lmv;
1121                 buf.lb_len = sizeof(*lmv);
1122                 if (declare) {
1123                         rc = lod_sub_object_declare_xattr_set(env,
1124                                                 lo->ldo_stripe[i], &buf,
1125                                                 XATTR_NAME_LMV,
1126                                                 LU_XATTR_REPLACE, th);
1127                 } else {
1128                         rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
1129                                                       &buf, XATTR_NAME_LMV,
1130                                                       LU_XATTR_REPLACE, th);
1131                 }
1132                 if (rc != 0)
1133                         break;
1134         }
1135
1136         RETURN(rc);
1137 }
1138
1139 /**
1140  * Implementation of dt_object_operations::do_declare_attr_set.
1141  *
1142  * If the object is striped, then apply the changes to all the stripes.
1143  *
1144  * \see dt_object_operations::do_declare_attr_set() in the API description
1145  * for details.
1146  */
1147 static int lod_declare_attr_set(const struct lu_env *env,
1148                                 struct dt_object *dt,
1149                                 const struct lu_attr *attr,
1150                                 struct thandle *th)
1151 {
1152         struct dt_object  *next = dt_object_child(dt);
1153         struct lod_object *lo = lod_dt_obj(dt);
1154         int                rc, i;
1155         ENTRY;
1156
1157         /* Set dead object on all other stripes */
1158         if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1159             attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1160                 rc = lod_mark_dead_object(env, dt, th, true);
1161                 RETURN(rc);
1162         }
1163
1164         /*
1165          * declare setattr on the local object
1166          */
1167         rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1168         if (rc)
1169                 RETURN(rc);
1170
1171         /* osp_declare_attr_set() ignores all attributes other than
1172          * UID, GID, and size, and osp_attr_set() ignores all but UID
1173          * and GID.  Declaration of size attr setting happens through
1174          * lod_declare_init_size(), and not through this function.
1175          * Therefore we need not load striping unless ownership is
1176          * changing.  This should save memory and (we hope) speed up
1177          * rename(). */
1178         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1179                 if (!(attr->la_valid & (LA_UID | LA_GID)))
1180                         RETURN(rc);
1181
1182                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1183                         RETURN(0);
1184         } else {
1185                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1186                                         LA_ATIME | LA_MTIME | LA_CTIME |
1187                                         LA_FLAGS)))
1188                         RETURN(rc);
1189         }
1190         /*
1191          * load striping information, notice we don't do this when object
1192          * is being initialized as we don't need this information till
1193          * few specific cases like destroy, chown
1194          */
1195         rc = lod_load_striping(env, lo);
1196         if (rc)
1197                 RETURN(rc);
1198
1199         if (lo->ldo_stripenr == 0)
1200                 RETURN(0);
1201
1202         /*
1203          * if object is striped declare changes on the stripes
1204          */
1205         LASSERT(lo->ldo_stripe);
1206         for (i = 0; i < lo->ldo_stripenr; i++) {
1207                 if (lo->ldo_stripe[i] == NULL)
1208                         continue;
1209                 rc = lod_sub_object_declare_attr_set(env,
1210                                         lo->ldo_stripe[i], attr,
1211                                         th);
1212                 if (rc != 0)
1213                         RETURN(rc);
1214         }
1215
1216         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1217             dt_object_exists(next) != 0 &&
1218             dt_object_remote(next) == 0)
1219                 lod_sub_object_declare_xattr_del(env, next,
1220                                                 XATTR_NAME_LOV, th);
1221
1222         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1223             dt_object_exists(next) &&
1224             dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1225                 struct lod_thread_info *info = lod_env_info(env);
1226                 struct lu_buf *buf = &info->lti_buf;
1227
1228                 buf->lb_buf = info->lti_ea_store;
1229                 buf->lb_len = info->lti_ea_store_size;
1230                 lod_sub_object_declare_xattr_set(env, next, buf,
1231                                                  XATTR_NAME_LOV,
1232                                                  LU_XATTR_REPLACE, th);
1233         }
1234
1235         RETURN(rc);
1236 }
1237
1238 /**
1239  * Implementation of dt_object_operations::do_attr_set.
1240  *
1241  * If the object is striped, then apply the changes to all or subset of
1242  * the stripes depending on the object type and specific attributes.
1243  *
1244  * \see dt_object_operations::do_attr_set() in the API description for details.
1245  */
1246 static int lod_attr_set(const struct lu_env *env,
1247                         struct dt_object *dt,
1248                         const struct lu_attr *attr,
1249                         struct thandle *th)
1250 {
1251         struct dt_object        *next = dt_object_child(dt);
1252         struct lod_object       *lo = lod_dt_obj(dt);
1253         int                     rc, i;
1254         ENTRY;
1255
1256         /* Set dead object on all other stripes */
1257         if (attr->la_valid & LA_FLAGS && !(attr->la_valid & ~LA_FLAGS) &&
1258             attr->la_flags & LUSTRE_SLAVE_DEAD_FL) {
1259                 rc = lod_mark_dead_object(env, dt, th, false);
1260                 RETURN(rc);
1261         }
1262
1263         /*
1264          * apply changes to the local object
1265          */
1266         rc = lod_sub_object_attr_set(env, next, attr, th);
1267         if (rc)
1268                 RETURN(rc);
1269
1270         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1271                 if (!(attr->la_valid & (LA_UID | LA_GID)))
1272                         RETURN(rc);
1273
1274                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1275                         RETURN(0);
1276         } else {
1277                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1278                                         LA_ATIME | LA_MTIME | LA_CTIME |
1279                                         LA_FLAGS)))
1280                         RETURN(rc);
1281         }
1282
1283         if (lo->ldo_stripenr == 0)
1284                 RETURN(0);
1285
1286         /*
1287          * if object is striped, apply changes to all the stripes
1288          */
1289         LASSERT(lo->ldo_stripe);
1290         for (i = 0; i < lo->ldo_stripenr; i++) {
1291                 if (unlikely(lo->ldo_stripe[i] == NULL))
1292                         continue;
1293
1294                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1295                     (dt_object_exists(lo->ldo_stripe[i]) == 0))
1296                         continue;
1297
1298                 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i], attr, th);
1299                 if (rc != 0)
1300                         break;
1301         }
1302
1303         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1304             dt_object_exists(next) != 0 &&
1305             dt_object_remote(next) == 0)
1306                 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1307
1308         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1309             dt_object_exists(next) &&
1310             dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1311                 struct lod_thread_info *info = lod_env_info(env);
1312                 struct lu_buf *buf = &info->lti_buf;
1313                 struct ost_id *oi = &info->lti_ostid;
1314                 struct lu_fid *fid = &info->lti_fid;
1315                 struct lov_mds_md_v1 *lmm;
1316                 struct lov_ost_data_v1 *objs;
1317                 __u32 magic;
1318                 int rc1;
1319
1320                 rc1 = lod_get_lov_ea(env, lo);
1321                 if (rc1  <= 0)
1322                         RETURN(rc);
1323
1324                 buf->lb_buf = info->lti_ea_store;
1325                 buf->lb_len = info->lti_ea_store_size;
1326                 lmm = info->lti_ea_store;
1327                 magic = le32_to_cpu(lmm->lmm_magic);
1328                 if (magic == LOV_MAGIC_V1)
1329                         objs = &(lmm->lmm_objects[0]);
1330                 else
1331                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1332                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1333                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1334                 fid->f_oid--;
1335                 fid_to_ostid(fid, oi);
1336                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1337
1338                 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1339                                               LU_XATTR_REPLACE, th);
1340         }
1341
1342         RETURN(rc);
1343 }
1344
1345 /**
1346  * Implementation of dt_object_operations::do_xattr_get.
1347  *
1348  * If LOV EA is requested from the root object and it's not
1349  * found, then return default striping for the filesystem.
1350  *
1351  * \see dt_object_operations::do_xattr_get() in the API description for details.
1352  */
1353 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1354                          struct lu_buf *buf, const char *name)
1355 {
1356         struct lod_thread_info  *info = lod_env_info(env);
1357         struct lod_device       *dev = lu2lod_dev(dt->do_lu.lo_dev);
1358         int                      rc, is_root;
1359         ENTRY;
1360
1361         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1362         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1363                 struct lmv_mds_md_v1    *lmv1;
1364                 int                      rc1 = 0;
1365
1366                 if (rc > (typeof(rc))sizeof(*lmv1))
1367                         RETURN(rc);
1368
1369                 if (rc < (typeof(rc))sizeof(*lmv1))
1370                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1371
1372                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1373                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1374
1375                         info->lti_buf.lb_buf = info->lti_key;
1376                         info->lti_buf.lb_len = sizeof(*lmv1);
1377                         rc = dt_xattr_get(env, dt_object_child(dt),
1378                                           &info->lti_buf, name);
1379                         if (unlikely(rc != sizeof(*lmv1)))
1380                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1381
1382                         lmv1 = info->lti_buf.lb_buf;
1383                         /* The on-disk LMV EA only contains header, but the
1384                          * returned LMV EA size should contain the space for
1385                          * the FIDs of all shards of the striped directory. */
1386                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1387                                 rc = lmv_mds_md_size(
1388                                         le32_to_cpu(lmv1->lmv_stripe_count),
1389                                         LMV_MAGIC_V1);
1390                 } else {
1391                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1392                                                   buf, false);
1393                 }
1394
1395                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1396         }
1397
1398         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1399                 RETURN(rc);
1400
1401         /*
1402          * lod returns default striping on the real root of the device
1403          * this is like the root stores default striping for the whole
1404          * filesystem. historically we've been using a different approach
1405          * and store it in the config.
1406          */
1407         dt_root_get(env, dev->lod_child, &info->lti_fid);
1408         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1409
1410         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1411                 struct lov_user_md *lum = buf->lb_buf;
1412                 struct lov_desc    *desc = &dev->lod_desc;
1413
1414                 if (buf->lb_buf == NULL) {
1415                         rc = sizeof(*lum);
1416                 } else if (buf->lb_len >= sizeof(*lum)) {
1417                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1418                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1419                         lmm_oi_set_id(&lum->lmm_oi, 0);
1420                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1421                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1422                         lum->lmm_stripe_size = cpu_to_le32(
1423                                                 desc->ld_default_stripe_size);
1424                         lum->lmm_stripe_count = cpu_to_le16(
1425                                                 desc->ld_default_stripe_count);
1426                         lum->lmm_stripe_offset = cpu_to_le16(
1427                                                 desc->ld_default_stripe_offset);
1428                         rc = sizeof(*lum);
1429                 } else {
1430                         rc = -ERANGE;
1431                 }
1432         }
1433
1434         RETURN(rc);
1435 }
1436
1437 /**
1438  * Verify LVM EA.
1439  *
1440  * Checks that the magic of the stripe is sane.
1441  *
1442  * \param[in] lod       lod device
1443  * \param[in] lum       a buffer storing LMV EA to verify
1444  *
1445  * \retval              0 if the EA is sane
1446  * \retval              negative otherwise
1447  */
1448 static int lod_verify_md_striping(struct lod_device *lod,
1449                                   const struct lmv_user_md_v1 *lum)
1450 {
1451         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1452                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1453                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1454                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1455                        (int)le32_to_cpu(lum->lum_stripe_offset),
1456                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1457                 return -EINVAL;
1458         }
1459
1460         return 0;
1461 }
1462
1463 /**
1464  * Initialize LMV EA for a slave.
1465  *
1466  * Initialize slave's LMV EA from the master's LMV EA.
1467  *
1468  * \param[in] master_lmv        a buffer containing master's EA
1469  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1470  *
1471  */
1472 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1473                                   const struct lmv_mds_md_v1 *master_lmv)
1474 {
1475         *slave_lmv = *master_lmv;
1476         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1477 }
1478
1479 /**
1480  * Generate LMV EA.
1481  *
1482  * Generate LMV EA from the object passed as \a dt. The object must have
1483  * the stripes created and initialized.
1484  *
1485  * \param[in] env       execution environment
1486  * \param[in] dt        object
1487  * \param[out] lmv_buf  buffer storing generated LMV EA
1488  *
1489  * \retval              0 on success
1490  * \retval              negative if failed
1491  */
1492 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1493                            struct lu_buf *lmv_buf)
1494 {
1495         struct lod_thread_info  *info = lod_env_info(env);
1496         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1497         struct lod_object       *lo = lod_dt_obj(dt);
1498         struct lmv_mds_md_v1    *lmm1;
1499         int                     stripe_count;
1500         int                     type = LU_SEQ_RANGE_ANY;
1501         int                     rc;
1502         __u32                   mdtidx;
1503         ENTRY;
1504
1505         LASSERT(lo->ldo_dir_striped != 0);
1506         LASSERT(lo->ldo_stripenr > 0);
1507         stripe_count = lo->ldo_stripenr;
1508         /* Only store the LMV EA heahder on the disk. */
1509         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1510                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1511                 if (rc != 0)
1512                         RETURN(rc);
1513         } else {
1514                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1515         }
1516
1517         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1518         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1519         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1520         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1521         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1522                             &mdtidx, &type);
1523         if (rc != 0)
1524                 RETURN(rc);
1525
1526         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1527         lmv_buf->lb_buf = info->lti_ea_store;
1528         lmv_buf->lb_len = sizeof(*lmm1);
1529
1530         RETURN(rc);
1531 }
1532
1533 /**
1534  * Create in-core represenation for a striped directory.
1535  *
1536  * Parse the buffer containing LMV EA and instantiate LU objects
1537  * representing the stripe objects. The pointers to the objects are
1538  * stored in ldo_stripe field of \a lo. This function is used when
1539  * we need to access an already created object (i.e. load from a disk).
1540  *
1541  * \param[in] env       execution environment
1542  * \param[in] lo        lod object
1543  * \param[in] buf       buffer containing LMV EA
1544  *
1545  * \retval              0 on success
1546  * \retval              negative if failed
1547  */
1548 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1549                            const struct lu_buf *buf)
1550 {
1551         struct lod_thread_info  *info = lod_env_info(env);
1552         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1553         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1554         struct dt_object        **stripe;
1555         union lmv_mds_md        *lmm = buf->lb_buf;
1556         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1557         struct lu_fid           *fid = &info->lti_fid;
1558         unsigned int            i;
1559         int                     rc = 0;
1560         ENTRY;
1561
1562         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1563                 RETURN(0);
1564
1565         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1566                 lo->ldo_dir_slave_stripe = 1;
1567                 RETURN(0);
1568         }
1569
1570         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1571                 RETURN(-EINVAL);
1572
1573         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1574                 RETURN(0);
1575
1576         LASSERT(lo->ldo_stripe == NULL);
1577         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1578                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1579         if (stripe == NULL)
1580                 RETURN(-ENOMEM);
1581
1582         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1583                 struct dt_device        *tgt_dt;
1584                 struct dt_object        *dto;
1585                 int                     type = LU_SEQ_RANGE_ANY;
1586                 __u32                   idx;
1587
1588                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1589                 if (!fid_is_sane(fid))
1590                         GOTO(out, rc = -ESTALE);
1591
1592                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1593                 if (rc != 0)
1594                         GOTO(out, rc);
1595
1596                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1597                         tgt_dt = lod->lod_child;
1598                 } else {
1599                         struct lod_tgt_desc     *tgt;
1600
1601                         tgt = LTD_TGT(ltd, idx);
1602                         if (tgt == NULL)
1603                                 GOTO(out, rc = -ESTALE);
1604                         tgt_dt = tgt->ltd_tgt;
1605                 }
1606
1607                 dto = dt_locate_at(env, tgt_dt, fid,
1608                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1609                                   NULL);
1610                 if (IS_ERR(dto))
1611                         GOTO(out, rc = PTR_ERR(dto));
1612
1613                 stripe[i] = dto;
1614         }
1615 out:
1616         lo->ldo_stripe = stripe;
1617         lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1618         lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1619         if (rc != 0)
1620                 lod_object_free_striping(env, lo);
1621
1622         RETURN(rc);
1623 }
1624
1625 /**
1626  * Declare create a striped directory.
1627  *
1628  * Declare creating a striped directory with a given stripe pattern on the
1629  * specified MDTs. A striped directory is represented as a regular directory
1630  * - an index listing all the stripes. The stripes point back to the master
1631  * object with ".." and LinkEA. The master object gets LMV EA which
1632  * identifies it as a striped directory. The function allocates FIDs
1633  * for all stripes.
1634  *
1635  * \param[in] env       execution environment
1636  * \param[in] dt        object
1637  * \param[in] attr      attributes to initialize the objects with
1638  * \param[in] dof       type of objects to be created
1639  * \param[in] th        transaction handle
1640  *
1641  * \retval              0 on success
1642  * \retval              negative if failed
1643  */
1644 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1645                                           struct dt_object *dt,
1646                                           struct lu_attr *attr,
1647                                           struct dt_object_format *dof,
1648                                           struct thandle *th)
1649 {
1650         struct lod_thread_info  *info = lod_env_info(env);
1651         struct lu_buf           lmv_buf;
1652         struct lu_buf           slave_lmv_buf;
1653         struct lmv_mds_md_v1    *lmm;
1654         struct lmv_mds_md_v1    *slave_lmm = NULL;
1655         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1656         struct lod_object       *lo = lod_dt_obj(dt);
1657         int                     rc;
1658         __u32                   i;
1659         ENTRY;
1660
1661         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1662         if (rc != 0)
1663                 GOTO(out, rc);
1664         lmm = lmv_buf.lb_buf;
1665
1666         OBD_ALLOC_PTR(slave_lmm);
1667         if (slave_lmm == NULL)
1668                 GOTO(out, rc = -ENOMEM);
1669
1670         lod_prep_slave_lmv_md(slave_lmm, lmm);
1671         slave_lmv_buf.lb_buf = slave_lmm;
1672         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1673
1674         if (!dt_try_as_dir(env, dt_object_child(dt)))
1675                 GOTO(out, rc = -EINVAL);
1676
1677         rec->rec_type = S_IFDIR;
1678         for (i = 0; i < lo->ldo_stripenr; i++) {
1679                 struct dt_object        *dto = lo->ldo_stripe[i];
1680                 char                    *stripe_name = info->lti_key;
1681                 struct lu_name          *sname;
1682                 struct linkea_data       ldata          = { NULL };
1683                 struct lu_buf           linkea_buf;
1684
1685                 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1686                                                    dof, th);
1687                 if (rc != 0)
1688                         GOTO(out, rc);
1689
1690                 if (!dt_try_as_dir(env, dto))
1691                         GOTO(out, rc = -EINVAL);
1692
1693                 rc = lod_sub_object_declare_ref_add(env, dto, th);
1694                 if (rc != 0)
1695                         GOTO(out, rc);
1696
1697                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1698                 rc = lod_sub_object_declare_insert(env, dto,
1699                                         (const struct dt_rec *)rec,
1700                                         (const struct dt_key *)dot, th);
1701                 if (rc != 0)
1702                         GOTO(out, rc);
1703
1704                 /* master stripe FID will be put to .. */
1705                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1706                 rc = lod_sub_object_declare_insert(env, dto,
1707                                         (const struct dt_rec *)rec,
1708                                         (const struct dt_key *)dotdot,
1709                                         th);
1710                 if (rc != 0)
1711                         GOTO(out, rc);
1712
1713                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1714                     cfs_fail_val != i) {
1715                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1716                             cfs_fail_val == i)
1717                                 slave_lmm->lmv_master_mdt_index =
1718                                                         cpu_to_le32(i + 1);
1719                         else
1720                                 slave_lmm->lmv_master_mdt_index =
1721                                                         cpu_to_le32(i);
1722                         rc = lod_sub_object_declare_xattr_set(env, dto,
1723                                         &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1724                         if (rc != 0)
1725                                 GOTO(out, rc);
1726                 }
1727
1728                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1729                     cfs_fail_val == i)
1730                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1731                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1732                 else
1733                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1734                                 PFID(lu_object_fid(&dto->do_lu)), i);
1735
1736                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1737                 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
1738                 if (rc != 0)
1739                         GOTO(out, rc);
1740
1741                 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1742                 if (rc != 0)
1743                         GOTO(out, rc);
1744
1745                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1746                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1747                 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1748                                           XATTR_NAME_LINK, 0, th);
1749                 if (rc != 0)
1750                         GOTO(out, rc);
1751
1752                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1753                 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1754                                        (const struct dt_rec *)rec,
1755                                        (const struct dt_key *)stripe_name,
1756                                        th);
1757                 if (rc != 0)
1758                         GOTO(out, rc);
1759
1760                 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1761                                                     th);
1762                 if (rc != 0)
1763                         GOTO(out, rc);
1764         }
1765
1766         rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1767                                 &lmv_buf, XATTR_NAME_LMV, 0, th);
1768         if (rc != 0)
1769                 GOTO(out, rc);
1770 out:
1771         if (slave_lmm != NULL)
1772                 OBD_FREE_PTR(slave_lmm);
1773
1774         RETURN(rc);
1775 }
1776
1777 static int lod_prep_md_striped_create(const struct lu_env *env,
1778                                       struct dt_object *dt,
1779                                       struct lu_attr *attr,
1780                                       const struct lmv_user_md_v1 *lum,
1781                                       struct dt_object_format *dof,
1782                                       struct thandle *th)
1783 {
1784         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1785         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1786         struct lod_object       *lo = lod_dt_obj(dt);
1787         struct dt_object        **stripe;
1788         __u32                   stripe_count;
1789         int                     *idx_array;
1790         __u32                   master_index;
1791         int                     rc = 0;
1792         __u32                   i;
1793         __u32                   j;
1794         ENTRY;
1795
1796         /* The lum has been verifed in lod_verify_md_striping */
1797         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1798         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1799
1800         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1801
1802         /* shrink the stripe_count to the avaible MDT count */
1803         if (stripe_count > lod->lod_remote_mdt_count + 1 &&
1804             !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
1805                 stripe_count = lod->lod_remote_mdt_count + 1;
1806
1807         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1808         if (stripe == NULL)
1809                 RETURN(-ENOMEM);
1810
1811         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1812         if (idx_array == NULL)
1813                 GOTO(out_free, rc = -ENOMEM);
1814
1815         /* Start index will be the master MDT */
1816         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1817         idx_array[0] = master_index;
1818         for (i = 0; i < stripe_count; i++) {
1819                 struct lod_tgt_desc     *tgt = NULL;
1820                 struct dt_object        *dto;
1821                 struct lu_fid           fid = { 0 };
1822                 int                     idx;
1823                 struct lu_object_conf   conf = { 0 };
1824                 struct dt_device        *tgt_dt = NULL;
1825
1826                 /* Try to find next avaible target */
1827                 idx = idx_array[i];
1828                 for (j = 0; j < lod->lod_remote_mdt_count;
1829                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1830                         bool already_allocated = false;
1831                         __u32 k;
1832
1833                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1834                                idx, lod->lod_remote_mdt_count + 1, i);
1835                         if (idx == master_index) {
1836                                 /* Allocate the FID locally */
1837                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1838                                                    &fid, NULL);
1839                                 if (rc < 0)
1840                                         GOTO(out_put, rc);
1841                                 tgt_dt = lod->lod_child;
1842                                 break;
1843                         }
1844
1845                         /* Find next available target */
1846                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1847                                 continue;
1848
1849                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1850                                 /* check whether the idx already exists
1851                                  * in current allocated array */
1852                                 for (k = 0; k < i; k++) {
1853                                         if (idx_array[k] == idx) {
1854                                                 already_allocated = true;
1855                                                 break;
1856                                         }
1857                                 }
1858
1859                                 if (already_allocated)
1860                                         continue;
1861                         }
1862
1863                         /* check the status of the OSP */
1864                         tgt = LTD_TGT(ltd, idx);
1865                         if (tgt == NULL)
1866                                 continue;
1867
1868                         tgt_dt = tgt->ltd_tgt;
1869                         rc = dt_statfs(env, tgt_dt, NULL);
1870                         if (rc) {
1871                                 /* this OSP doesn't feel well */
1872                                 rc = 0;
1873                                 continue;
1874                         }
1875
1876                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1877                         if (rc < 0) {
1878                                 rc = 0;
1879                                 continue;
1880                         }
1881
1882                         break;
1883                 }
1884
1885                 /* Can not allocate more stripes */
1886                 if (j == lod->lod_remote_mdt_count) {
1887                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1888                                lod2obd(lod)->obd_name, stripe_count, i - 1);
1889                         break;
1890                 }
1891
1892                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1893                        idx, i, PFID(&fid));
1894                 idx_array[i] = idx;
1895                 /* Set the start index for next stripe allocation */
1896                 if (i < stripe_count - 1)
1897                         idx_array[i + 1] = (idx + 1) %
1898                                            (lod->lod_remote_mdt_count + 1);
1899                 /* tgt_dt and fid must be ready after search avaible OSP
1900                  * in the above loop */
1901                 LASSERT(tgt_dt != NULL);
1902                 LASSERT(fid_is_sane(&fid));
1903                 conf.loc_flags = LOC_F_NEW;
1904                 dto = dt_locate_at(env, tgt_dt, &fid,
1905                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1906                                    &conf);
1907                 if (IS_ERR(dto))
1908                         GOTO(out_put, rc = PTR_ERR(dto));
1909                 stripe[i] = dto;
1910         }
1911
1912         lo->ldo_dir_striped = 1;
1913         lo->ldo_stripe = stripe;
1914         lo->ldo_stripenr = i;
1915         lo->ldo_stripes_allocated = stripe_count;
1916
1917         if (lo->ldo_stripenr == 0)
1918                 GOTO(out_put, rc = -ENOSPC);
1919
1920         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1921         if (rc != 0)
1922                 GOTO(out_put, rc);
1923
1924 out_put:
1925         if (rc < 0) {
1926                 for (i = 0; i < stripe_count; i++)
1927                         if (stripe[i] != NULL)
1928                                 lu_object_put(env, &stripe[i]->do_lu);
1929                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1930                 lo->ldo_stripenr = 0;
1931                 lo->ldo_stripes_allocated = 0;
1932                 lo->ldo_stripe = NULL;
1933         }
1934
1935 out_free:
1936         if (idx_array != NULL)
1937                 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1938
1939         RETURN(rc);
1940 }
1941
1942 /**
1943  * Declare create striped md object.
1944  *
1945  * The function declares intention to create a striped directory. This is a
1946  * wrapper for lod_prep_md_striped_create(). The only additional functionality
1947  * is to verify pattern \a lum_buf is good. Check that function for the details.
1948  *
1949  * \param[in] env       execution environment
1950  * \param[in] dt        object
1951  * \param[in] attr      attributes to initialize the objects with
1952  * \param[in] lum_buf   a pattern specifying the number of stripes and
1953  *                      MDT to start from
1954  * \param[in] dof       type of objects to be created
1955  * \param[in] th        transaction handle
1956  *
1957  * \retval              0 on success
1958  * \retval              negative if failed
1959  *
1960  */
1961 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1962                                      struct dt_object *dt,
1963                                      struct lu_attr *attr,
1964                                      const struct lu_buf *lum_buf,
1965                                      struct dt_object_format *dof,
1966                                      struct thandle *th)
1967 {
1968         struct lod_object       *lo = lod_dt_obj(dt);
1969         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1970         struct lmv_user_md_v1   *lum;
1971         int                     rc;
1972         ENTRY;
1973
1974         lum = lum_buf->lb_buf;
1975         LASSERT(lum != NULL);
1976
1977         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1978                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1979                (int)le32_to_cpu(lum->lum_stripe_offset));
1980
1981         if (le32_to_cpu(lum->lum_stripe_count) == 0)
1982                 GOTO(out, rc = 0);
1983
1984         rc = lod_verify_md_striping(lod, lum);
1985         if (rc != 0)
1986                 GOTO(out, rc);
1987
1988         /* prepare dir striped objects */
1989         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1990         if (rc != 0) {
1991                 /* failed to create striping, let's reset
1992                  * config so that others don't get confused */
1993                 lod_object_free_striping(env, lo);
1994                 GOTO(out, rc);
1995         }
1996 out:
1997         RETURN(rc);
1998 }
1999
2000
2001 /**
2002  * Implementation of dt_object_operations::do_declare_xattr_set.
2003  *
2004  * Used with regular (non-striped) objects. Basically it
2005  * initializes the striping information and applies the
2006  * change to all the stripes.
2007  *
2008  * \see dt_object_operations::do_declare_xattr_set() in the API description
2009  * for details.
2010  */
2011 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2012                                      struct dt_object *dt,
2013                                      const struct lu_buf *buf,
2014                                      const char *name, int fl,
2015                                      struct thandle *th)
2016 {
2017         struct dt_object        *next = dt_object_child(dt);
2018         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2019         struct lod_object       *lo = lod_dt_obj(dt);
2020         int                     i;
2021         int                     rc;
2022         ENTRY;
2023
2024         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2025                 struct lmv_user_md_v1 *lum;
2026
2027                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2028                 lum = buf->lb_buf;
2029                 rc = lod_verify_md_striping(d, lum);
2030                 if (rc != 0)
2031                         RETURN(rc);
2032         }
2033
2034         rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2035         if (rc != 0)
2036                 RETURN(rc);
2037
2038         /* Note: Do not set LinkEA on sub-stripes, otherwise
2039          * it will confuse the fid2path process(see mdt_path_current()).
2040          * The linkEA between master and sub-stripes is set in
2041          * lod_xattr_set_lmv(). */
2042         if (strcmp(name, XATTR_NAME_LINK) == 0)
2043                 RETURN(0);
2044
2045         /* set xattr to each stripes, if needed */
2046         rc = lod_load_striping(env, lo);
2047         if (rc != 0)
2048                 RETURN(rc);
2049
2050         if (lo->ldo_stripenr == 0)
2051                 RETURN(0);
2052
2053         for (i = 0; i < lo->ldo_stripenr; i++) {
2054                 LASSERT(lo->ldo_stripe[i]);
2055
2056                 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2057                                                 buf, name, fl, th);
2058                 if (rc != 0)
2059                         break;
2060         }
2061
2062         RETURN(rc);
2063 }
2064
2065 /**
2066  * Reset parent FID on OST object
2067  *
2068  * Replace parent FID with @dt object FID, which is only called during migration
2069  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2070  * the FID is changed.
2071  *
2072  * \param[in] env execution environment
2073  * \param[in] dt dt_object whose stripes's parent FID will be reset
2074  * \parem[in] th thandle
2075  * \param[in] declare if it is declare
2076  *
2077  * \retval      0 if reset succeeds
2078  * \retval      negative errno if reset fais
2079  */
2080 static int lod_object_replace_parent_fid(const struct lu_env *env,
2081                                          struct dt_object *dt,
2082                                          struct thandle *th, bool declare)
2083 {
2084         struct lod_object *lo = lod_dt_obj(dt);
2085         struct lod_thread_info  *info = lod_env_info(env);
2086         struct lu_buf *buf = &info->lti_buf;
2087         struct filter_fid *ff;
2088         int i, rc;
2089         ENTRY;
2090
2091         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2092
2093         /* set xattr to each stripes, if needed */
2094         rc = lod_load_striping(env, lo);
2095         if (rc != 0)
2096                 RETURN(rc);
2097
2098         if (lo->ldo_stripenr == 0)
2099                 RETURN(0);
2100
2101         if (info->lti_ea_store_size < sizeof(*ff)) {
2102                 rc = lod_ea_store_resize(info, sizeof(*ff));
2103                 if (rc != 0)
2104                         RETURN(rc);
2105         }
2106
2107         buf->lb_buf = info->lti_ea_store;
2108         buf->lb_len = info->lti_ea_store_size;
2109
2110         for (i = 0; i < lo->ldo_stripenr; i++) {
2111                 if (lo->ldo_stripe[i] == NULL)
2112                         continue;
2113
2114                 rc = dt_xattr_get(env, lo->ldo_stripe[i], buf,
2115                                   XATTR_NAME_FID);
2116                 if (rc < 0) {
2117                         rc = 0;
2118                         continue;
2119                 }
2120
2121                 ff = buf->lb_buf;
2122                 fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
2123                 ff->ff_parent.f_seq = lu_object_fid(&dt->do_lu)->f_seq;
2124                 ff->ff_parent.f_oid = lu_object_fid(&dt->do_lu)->f_oid;
2125                 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2126
2127                 if (declare) {
2128                         rc = lod_sub_object_declare_xattr_set(env,
2129                                                 lo->ldo_stripe[i], buf,
2130                                                 XATTR_NAME_FID,
2131                                                 LU_XATTR_REPLACE, th);
2132                 } else {
2133                         rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
2134                                                       buf, XATTR_NAME_FID,
2135                                                       LU_XATTR_REPLACE, th);
2136                 }
2137                 if (rc < 0)
2138                         break;
2139         }
2140
2141         RETURN(rc);
2142 }
2143
2144 /**
2145  * Implementation of dt_object_operations::do_declare_xattr_set.
2146  *
2147  * \see dt_object_operations::do_declare_xattr_set() in the API description
2148  * for details.
2149  *
2150  * the extension to the API:
2151  *   - declaring LOVEA requests striping creation
2152  *   - LU_XATTR_REPLACE means layout swap
2153  */
2154 static int lod_declare_xattr_set(const struct lu_env *env,
2155                                  struct dt_object *dt,
2156                                  const struct lu_buf *buf,
2157                                  const char *name, int fl,
2158                                  struct thandle *th)
2159 {
2160         struct dt_object *next = dt_object_child(dt);
2161         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2162         __u32             mode;
2163         int               rc;
2164         ENTRY;
2165
2166         /*
2167          * allow to declare predefined striping on a new (!mode) object
2168          * which is supposed to be replay of regular file creation
2169          * (when LOV setting is declared)
2170          * LU_XATTR_REPLACE is set to indicate a layout swap
2171          */
2172         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2173         if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2174              !(fl & LU_XATTR_REPLACE)) {
2175                 /*
2176                  * this is a request to manipulate object's striping
2177                  */
2178                 if (dt_object_exists(dt)) {
2179                         rc = dt_attr_get(env, next, attr);
2180                         if (rc)
2181                                 RETURN(rc);
2182                 } else {
2183                         memset(attr, 0, sizeof(*attr));
2184                         attr->la_valid = LA_TYPE | LA_MODE;
2185                         attr->la_mode = S_IFREG;
2186                 }
2187                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2188         } else if (S_ISDIR(mode)) {
2189                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2190         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2191                 rc = lod_object_replace_parent_fid(env, dt, th, true);
2192         } else {
2193                 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2194                                                       fl, th);
2195         }
2196
2197         RETURN(rc);
2198 }
2199
2200 /**
2201  * Resets cached default striping in the object.
2202  *
2203  * \param[in] lo        object
2204  */
2205 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2206 {
2207         lo->ldo_def_striping_set = 0;
2208         lo->ldo_def_striping_cached = 0;
2209         lod_object_set_pool(lo, NULL);
2210         lo->ldo_def_stripe_size = 0;
2211         lo->ldo_def_stripenr = 0;
2212         if (lo->ldo_dir_stripe != NULL)
2213                 lo->ldo_dir_def_striping_cached = 0;
2214 }
2215
2216 /**
2217  * Apply xattr changes to the object.
2218  *
2219  * Applies xattr changes to the object and the stripes if the latter exist.
2220  *
2221  * \param[in] env       execution environment
2222  * \param[in] dt        object
2223  * \param[in] buf       buffer pointing to the new value of xattr
2224  * \param[in] name      name of xattr
2225  * \param[in] fl        flags
2226  * \param[in] th        transaction handle
2227  *
2228  * \retval              0 on success
2229  * \retval              negative if failed
2230  */
2231 static int lod_xattr_set_internal(const struct lu_env *env,
2232                                   struct dt_object *dt,
2233                                   const struct lu_buf *buf,
2234                                   const char *name, int fl,
2235                                   struct thandle *th)
2236 {
2237         struct dt_object        *next = dt_object_child(dt);
2238         struct lod_object       *lo = lod_dt_obj(dt);
2239         int                     rc;
2240         int                     i;
2241         ENTRY;
2242
2243         rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2244         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2245                 RETURN(rc);
2246
2247         /* Note: Do not set LinkEA on sub-stripes, otherwise
2248          * it will confuse the fid2path process(see mdt_path_current()).
2249          * The linkEA between master and sub-stripes is set in
2250          * lod_xattr_set_lmv(). */
2251         if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2252                 RETURN(0);
2253
2254         for (i = 0; i < lo->ldo_stripenr; i++) {
2255                 LASSERT(lo->ldo_stripe[i]);
2256
2257                 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2258                                               fl, th);
2259                 if (rc != 0)
2260                         break;
2261         }
2262
2263         RETURN(rc);
2264 }
2265
2266 /**
2267  * Delete an extended attribute.
2268  *
2269  * Deletes specified xattr from the object and the stripes if the latter exist.
2270  *
2271  * \param[in] env       execution environment
2272  * \param[in] dt        object
2273  * \param[in] name      name of xattr
2274  * \param[in] th        transaction handle
2275  *
2276  * \retval              0 on success
2277  * \retval              negative if failed
2278  */
2279 static int lod_xattr_del_internal(const struct lu_env *env,
2280                                   struct dt_object *dt,
2281                                   const char *name, struct thandle *th)
2282 {
2283         struct dt_object        *next = dt_object_child(dt);
2284         struct lod_object       *lo = lod_dt_obj(dt);
2285         int                     rc;
2286         int                     i;
2287         ENTRY;
2288
2289         rc = lod_sub_object_xattr_del(env, next, name, th);
2290         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2291                 RETURN(rc);
2292
2293         if (lo->ldo_stripenr == 0)
2294                 RETURN(rc);
2295
2296         for (i = 0; i < lo->ldo_stripenr; i++) {
2297                 LASSERT(lo->ldo_stripe[i]);
2298
2299                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2300                                               th);
2301                 if (rc != 0)
2302                         break;
2303         }
2304
2305         RETURN(rc);
2306 }
2307
2308 /**
2309  * Set default striping on a directory.
2310  *
2311  * Sets specified striping on a directory object unless it matches the default
2312  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2313  * EA. This striping will be used when regular file is being created in this
2314  * directory.
2315  *
2316  * \param[in] env       execution environment
2317  * \param[in] dt        the striped object
2318  * \param[in] buf       buffer with the striping
2319  * \param[in] name      name of EA
2320  * \param[in] fl        xattr flag (see OSD API description)
2321  * \param[in] th        transaction handle
2322  *
2323  * \retval              0 on success
2324  * \retval              negative if failed
2325  */
2326 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2327                                     struct dt_object *dt,
2328                                     const struct lu_buf *buf,
2329                                     const char *name, int fl,
2330                                     struct thandle *th)
2331 {
2332         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2333         struct lod_object       *l = lod_dt_obj(dt);
2334         struct lov_user_md_v1   *lum;
2335         struct lov_user_md_v3   *v3 = NULL;
2336         const char              *pool_name = NULL;
2337         int                      rc;
2338         ENTRY;
2339
2340         /* If it is striped dir, we should clear the stripe cache for
2341          * slave stripe as well, but there are no effective way to
2342          * notify the LOD on the slave MDT, so we do not cache stripe
2343          * information for slave stripe for now. XXX*/
2344         lod_lov_stripe_cache_clear(l);
2345         LASSERT(buf != NULL && buf->lb_buf != NULL);
2346         lum = buf->lb_buf;
2347
2348         rc = lod_verify_striping(d, buf, false);
2349         if (rc)
2350                 RETURN(rc);
2351
2352         if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2353                 v3 = buf->lb_buf;
2354                 if (v3->lmm_pool_name[0] != '\0')
2355                         pool_name = v3->lmm_pool_name;
2356         }
2357
2358         /* if { size, offset, count } = { 0, -1, 0 } and no pool
2359          * (i.e. all default values specified) then delete default
2360          * striping from dir. */
2361         CDEBUG(D_OTHER,
2362                 "set default striping: sz %u # %u offset %d %s %s\n",
2363                 (unsigned)lum->lmm_stripe_size,
2364                 (unsigned)lum->lmm_stripe_count,
2365                 (int)lum->lmm_stripe_offset,
2366                 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2367
2368         if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2369                                 lum->lmm_stripe_offset, pool_name)) {
2370                 rc = lod_xattr_del_internal(env, dt, name, th);
2371                 if (rc == -ENODATA)
2372                         rc = 0;
2373         } else {
2374                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2375         }
2376
2377         RETURN(rc);
2378 }
2379
2380 /**
2381  * Set default striping on a directory object.
2382  *
2383  * Sets specified striping on a directory object unless it matches the default
2384  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2385  * EA. This striping will be used when a new directory is being created in the
2386  * directory.
2387  *
2388  * \param[in] env       execution environment
2389  * \param[in] dt        the striped object
2390  * \param[in] buf       buffer with the striping
2391  * \param[in] name      name of EA
2392  * \param[in] fl        xattr flag (see OSD API description)
2393  * \param[in] th        transaction handle
2394  *
2395  * \retval              0 on success
2396  * \retval              negative if failed
2397  */
2398 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2399                                             struct dt_object *dt,
2400                                             const struct lu_buf *buf,
2401                                             const char *name, int fl,
2402                                             struct thandle *th)
2403 {
2404         struct lod_object       *l = lod_dt_obj(dt);
2405         struct lmv_user_md_v1   *lum;
2406         int                      rc;
2407         ENTRY;
2408
2409         LASSERT(buf != NULL && buf->lb_buf != NULL);
2410         lum = buf->lb_buf;
2411
2412         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2413               le32_to_cpu(lum->lum_stripe_count),
2414               (int)le32_to_cpu(lum->lum_stripe_offset));
2415
2416         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2417                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2418                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2419                 rc = lod_xattr_del_internal(env, dt, name, th);
2420                 if (rc == -ENODATA)
2421                         rc = 0;
2422         } else {
2423                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2424                 if (rc != 0)
2425                         RETURN(rc);
2426         }
2427
2428         /* Update default stripe cache */
2429         if (l->ldo_dir_stripe == NULL) {
2430                 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2431                 if (l->ldo_dir_stripe == NULL)
2432                         RETURN(-ENOMEM);
2433         }
2434
2435         l->ldo_dir_def_striping_cached = 0;
2436         RETURN(rc);
2437 }
2438
2439 /**
2440  * Turn directory into a striped directory.
2441  *
2442  * During replay the client sends the striping created before MDT
2443  * failure, then the layer above LOD sends this defined striping
2444  * using ->do_xattr_set(), so LOD uses this method to replay creation
2445  * of the stripes. Notice the original information for the striping
2446  * (#stripes, FIDs, etc) was transferred in declare path.
2447  *
2448  * \param[in] env       execution environment
2449  * \param[in] dt        the striped object
2450  * \param[in] buf       not used currently
2451  * \param[in] name      not used currently
2452  * \param[in] fl        xattr flag (see OSD API description)
2453  * \param[in] th        transaction handle
2454  *
2455  * \retval              0 on success
2456  * \retval              negative if failed
2457  */
2458 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2459                              const struct lu_buf *buf, const char *name,
2460                              int fl, struct thandle *th)
2461 {
2462         struct lod_object       *lo = lod_dt_obj(dt);
2463         struct lod_thread_info  *info = lod_env_info(env);
2464         struct lu_attr          *attr = &info->lti_attr;
2465         struct dt_object_format *dof = &info->lti_format;
2466         struct lu_buf           lmv_buf;
2467         struct lu_buf           slave_lmv_buf;
2468         struct lmv_mds_md_v1    *lmm;
2469         struct lmv_mds_md_v1    *slave_lmm = NULL;
2470         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2471         int                     i;
2472         int                     rc;
2473         ENTRY;
2474
2475         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2476                 RETURN(-ENOTDIR);
2477
2478         /* The stripes are supposed to be allocated in declare phase,
2479          * if there are no stripes being allocated, it will skip */
2480         if (lo->ldo_stripenr == 0)
2481                 RETURN(0);
2482
2483         rc = dt_attr_get(env, dt_object_child(dt), attr);
2484         if (rc != 0)
2485                 RETURN(rc);
2486
2487         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2488                          LA_MODE | LA_UID | LA_GID | LA_TYPE;
2489         dof->dof_type = DFT_DIR;
2490
2491         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2492         if (rc != 0)
2493                 RETURN(rc);
2494         lmm = lmv_buf.lb_buf;
2495
2496         OBD_ALLOC_PTR(slave_lmm);
2497         if (slave_lmm == NULL)
2498                 RETURN(-ENOMEM);
2499
2500         lod_prep_slave_lmv_md(slave_lmm, lmm);
2501         slave_lmv_buf.lb_buf = slave_lmm;
2502         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2503
2504         rec->rec_type = S_IFDIR;
2505         for (i = 0; i < lo->ldo_stripenr; i++) {
2506                 struct dt_object *dto;
2507                 char             *stripe_name = info->lti_key;
2508                 struct lu_name          *sname;
2509                 struct linkea_data       ldata          = { NULL };
2510                 struct lu_buf            linkea_buf;
2511
2512                 dto = lo->ldo_stripe[i];
2513
2514                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2515                 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2516                                            th);
2517                 if (rc != 0) {
2518                         dt_write_unlock(env, dto);
2519                         GOTO(out, rc);
2520                 }
2521
2522                 rc = lod_sub_object_ref_add(env, dto, th);
2523                 dt_write_unlock(env, dto);
2524                 if (rc != 0)
2525                         GOTO(out, rc);
2526
2527                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2528                 rc = lod_sub_object_index_insert(env, dto,
2529                                 (const struct dt_rec *)rec,
2530                                 (const struct dt_key *)dot, th, 0);
2531                 if (rc != 0)
2532                         GOTO(out, rc);
2533
2534                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2535                 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2536                                (const struct dt_key *)dotdot, th, 0);
2537                 if (rc != 0)
2538                         GOTO(out, rc);
2539
2540                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2541                     cfs_fail_val != i) {
2542                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2543                             cfs_fail_val == i)
2544                                 slave_lmm->lmv_master_mdt_index =
2545                                                         cpu_to_le32(i + 1);
2546                         else
2547                                 slave_lmm->lmv_master_mdt_index =
2548                                                         cpu_to_le32(i);
2549
2550                         rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2551                                                       XATTR_NAME_LMV, fl, th);
2552                         if (rc != 0)
2553                                 GOTO(out, rc);
2554                 }
2555
2556                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2557                     cfs_fail_val == i)
2558                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2559                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2560                 else
2561                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2562                                  PFID(lu_object_fid(&dto->do_lu)), i);
2563
2564                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2565                 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2566                 if (rc != 0)
2567                         GOTO(out, rc);
2568
2569                 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2570                 if (rc != 0)
2571                         GOTO(out, rc);
2572
2573                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2574                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2575                 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2576                                         XATTR_NAME_LINK, 0, th);
2577                 if (rc != 0)
2578                         GOTO(out, rc);
2579
2580                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2581                 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2582                                (const struct dt_rec *)rec,
2583                                (const struct dt_key *)stripe_name, th, 0);
2584                 if (rc != 0)
2585                         GOTO(out, rc);
2586
2587                 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2588                 if (rc != 0)
2589                         GOTO(out, rc);
2590         }
2591
2592         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2593                 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2594                                               &lmv_buf, XATTR_NAME_LMV, fl, th);
2595 out:
2596         if (slave_lmm != NULL)
2597                 OBD_FREE_PTR(slave_lmm);
2598
2599         RETURN(rc);
2600 }
2601
2602 /**
2603  * Helper function to declare/execute creation of a striped directory
2604  *
2605  * Called in declare/create object path, prepare striping for a directory
2606  * and prepare defaults data striping for the objects to be created in
2607  * that directory. Notice the function calls "declaration" or "execution"
2608  * methods depending on \a declare param. This is a consequence of the
2609  * current approach while we don't have natural distributed transactions:
2610  * we basically execute non-local updates in the declare phase. So, the
2611  * arguments for the both phases are the same and this is the reason for
2612  * this function to exist.
2613  *
2614  * \param[in] env       execution environment
2615  * \param[in] dt        object
2616  * \param[in] attr      attributes the stripes will be created with
2617  * \param[in] dof       format of stripes (see OSD API description)
2618  * \param[in] th        transaction handle
2619  * \param[in] declare   where to call "declare" or "execute" methods
2620  *
2621  * \retval              0 on success
2622  * \retval              negative if failed
2623  */
2624 static int lod_dir_striping_create_internal(const struct lu_env *env,
2625                                             struct dt_object *dt,
2626                                             struct lu_attr *attr,
2627                                             struct dt_object_format *dof,
2628                                             struct thandle *th,
2629                                             bool declare)
2630 {
2631         struct lod_thread_info  *info = lod_env_info(env);
2632         struct lod_object       *lo = lod_dt_obj(dt);
2633         int                     rc;
2634         ENTRY;
2635
2636         if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2637                                  lo->ldo_dir_stripe_offset)) {
2638                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2639                 int stripe_count = lo->ldo_stripenr;
2640
2641                 if (info->lti_ea_store_size < sizeof(*v1)) {
2642                         rc = lod_ea_store_resize(info, sizeof(*v1));
2643                         if (rc != 0)
2644                                 RETURN(rc);
2645                         v1 = info->lti_ea_store;
2646                 }
2647
2648                 memset(v1, 0, sizeof(*v1));
2649                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2650                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2651                 v1->lum_stripe_offset =
2652                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
2653
2654                 info->lti_buf.lb_buf = v1;
2655                 info->lti_buf.lb_len = sizeof(*v1);
2656
2657                 if (declare)
2658                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
2659                                                        &info->lti_buf, dof, th);
2660                 else
2661                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2662                                                XATTR_NAME_LMV, 0, th);
2663                 if (rc != 0)
2664                         RETURN(rc);
2665         }
2666
2667         /* Transfer default LMV striping from the parent */
2668         if (lo->ldo_dir_def_striping_set &&
2669             !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2670                                  lo->ldo_dir_def_stripe_offset)) {
2671                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2672                 int def_stripe_count = lo->ldo_dir_def_stripenr;
2673
2674                 if (info->lti_ea_store_size < sizeof(*v1)) {
2675                         rc = lod_ea_store_resize(info, sizeof(*v1));
2676                         if (rc != 0)
2677                                 RETURN(rc);
2678                         v1 = info->lti_ea_store;
2679                 }
2680
2681                 memset(v1, 0, sizeof(*v1));
2682                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2683                 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2684                 v1->lum_stripe_offset =
2685                                 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2686                 v1->lum_hash_type =
2687                                 cpu_to_le32(lo->ldo_dir_def_hash_type);
2688
2689                 info->lti_buf.lb_buf = v1;
2690                 info->lti_buf.lb_len = sizeof(*v1);
2691                 if (declare)
2692                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2693                                                        XATTR_NAME_DEFAULT_LMV,
2694                                                        0, th);
2695                 else
2696                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2697                                                   &info->lti_buf,
2698                                                   XATTR_NAME_DEFAULT_LMV, 0,
2699                                                   th);
2700                 if (rc != 0)
2701                         RETURN(rc);
2702         }
2703
2704         /* Transfer default LOV striping from the parent */
2705         if (lo->ldo_def_striping_set &&
2706             !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2707                                  lo->ldo_def_stripenr,
2708                                  lo->ldo_def_stripe_offset,
2709                                  lo->ldo_pool)) {
2710                 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2711
2712                 if (info->lti_ea_store_size < sizeof(*v3)) {
2713                         rc = lod_ea_store_resize(info, sizeof(*v3));
2714                         if (rc != 0)
2715                                 RETURN(rc);
2716                         v3 = info->lti_ea_store;
2717                 }
2718
2719                 memset(v3, 0, sizeof(*v3));
2720                 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2721                 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2722                 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2723                 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2724                 if (lo->ldo_pool != NULL)
2725                         strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2726                                 sizeof(v3->lmm_pool_name));
2727
2728                 info->lti_buf.lb_buf = v3;
2729                 info->lti_buf.lb_len = sizeof(*v3);
2730
2731                 if (declare)
2732                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2733                                                        XATTR_NAME_LOV, 0, th);
2734                 else
2735                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2736                                                       XATTR_NAME_LOV, 0, th);
2737                 if (rc != 0)
2738                         RETURN(rc);
2739         }
2740
2741         RETURN(0);
2742 }
2743
2744 static int lod_declare_dir_striping_create(const struct lu_env *env,
2745                                            struct dt_object *dt,
2746                                            struct lu_attr *attr,
2747                                            struct dt_object_format *dof,
2748                                            struct thandle *th)
2749 {
2750         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2751 }
2752
2753 static int lod_dir_striping_create(const struct lu_env *env,
2754                                    struct dt_object *dt,
2755                                    struct lu_attr *attr,
2756                                    struct dt_object_format *dof,
2757                                    struct thandle *th)
2758 {
2759         struct lod_object *lo = lod_dt_obj(dt);
2760         int rc;
2761
2762         rc = lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2763         if (rc == 0)
2764                 lo->ldo_striping_cached = 1;
2765
2766         return rc;
2767 }
2768
2769 /**
2770  * Implementation of dt_object_operations::do_xattr_set.
2771  *
2772  * Sets specified extended attribute on the object. Three types of EAs are
2773  * special:
2774  *   LOV EA - stores striping for a regular file or default striping (when set
2775  *            on a directory)
2776  *   LMV EA - stores a marker for the striped directories
2777  *   DMV EA - stores default directory striping
2778  *
2779  * When striping is applied to a non-striped existing object (this is called
2780  * late striping), then LOD notices the caller wants to turn the object into a
2781  * striped one. The stripe objects are created and appropriate EA is set:
2782  * LOV EA storing all the stripes directly or LMV EA storing just a small header
2783  * with striping configuration.
2784  *
2785  * \see dt_object_operations::do_xattr_set() in the API description for details.
2786  */
2787 static int lod_xattr_set(const struct lu_env *env,
2788                          struct dt_object *dt, const struct lu_buf *buf,
2789                          const char *name, int fl, struct thandle *th)
2790 {
2791         struct dt_object        *next = dt_object_child(dt);
2792         int                      rc;
2793         ENTRY;
2794
2795         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2796             strcmp(name, XATTR_NAME_LMV) == 0) {
2797                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2798
2799                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2800                                                 LMV_HASH_FLAG_MIGRATION)
2801                         rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2802                                                       th);
2803                 else
2804                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2805
2806                 RETURN(rc);
2807         }
2808
2809         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2810             strcmp(name, XATTR_NAME_LOV) == 0) {
2811                 /* default LOVEA */
2812                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2813                 RETURN(rc);
2814         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2815                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2816                 /* default LMVEA */
2817                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2818                                                       th);
2819                 RETURN(rc);
2820         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2821                    !strcmp(name, XATTR_NAME_LOV)) {
2822                 /* in case of lov EA swap, just set it
2823                  * if not, it is a replay so check striping match what we
2824                  * already have during req replay, declare_xattr_set()
2825                  * defines striping, then create() does the work */
2826                 if (fl & LU_XATTR_REPLACE) {
2827                         /* free stripes, then update disk */
2828                         lod_object_free_striping(env, lod_dt_obj(dt));
2829
2830                         rc = lod_sub_object_xattr_set(env, next, buf, name,
2831                                                       fl, th);
2832                 } else if (dt_object_remote(dt)) {
2833                         /* This only happens during migration, see
2834                          * mdd_migrate_create(), in which Master MDT will
2835                          * create a remote target object, and only set
2836                          * (migrating) stripe EA on the remote object,
2837                          * and does not need creating each stripes. */
2838                         rc = lod_sub_object_xattr_set(env, next, buf, name,
2839                                                       fl, th);
2840                 } else {
2841                         rc = lod_striping_create(env, dt, NULL, NULL, th);
2842                 }
2843                 RETURN(rc);
2844         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2845                 rc = lod_object_replace_parent_fid(env, dt, th, false);
2846
2847                 RETURN(rc);
2848         }
2849
2850         /* then all other xattr */
2851         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2852
2853         RETURN(rc);
2854 }
2855
2856 /**
2857  * Implementation of dt_object_operations::do_declare_xattr_del.
2858  *
2859  * \see dt_object_operations::do_declare_xattr_del() in the API description
2860  * for details.
2861  */
2862 static int lod_declare_xattr_del(const struct lu_env *env,
2863                                  struct dt_object *dt, const char *name,
2864                                  struct thandle *th)
2865 {
2866         struct lod_object       *lo = lod_dt_obj(dt);
2867         int                     rc;
2868         int                     i;
2869         ENTRY;
2870
2871         rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2872                                               name, th);
2873         if (rc != 0)
2874                 RETURN(rc);
2875
2876         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2877                 RETURN(0);
2878
2879         /* set xattr to each stripes, if needed */
2880         rc = lod_load_striping(env, lo);
2881         if (rc != 0)
2882                 RETURN(rc);
2883
2884         if (lo->ldo_stripenr == 0)
2885                 RETURN(0);
2886
2887         for (i = 0; i < lo->ldo_stripenr; i++) {
2888                 LASSERT(lo->ldo_stripe[i]);
2889                 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2890                                                       name, th);
2891                 if (rc != 0)
2892                         break;
2893         }
2894
2895         RETURN(rc);
2896 }
2897
2898 /**
2899  * Implementation of dt_object_operations::do_xattr_del.
2900  *
2901  * If EA storing a regular striping is being deleted, then release
2902  * all the references to the stripe objects in core.
2903  *
2904  * \see dt_object_operations::do_xattr_del() in the API description for details.
2905  */
2906 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2907                          const char *name, struct thandle *th)
2908 {
2909         struct dt_object        *next = dt_object_child(dt);
2910         struct lod_object       *lo = lod_dt_obj(dt);
2911         int                     rc;
2912         int                     i;
2913         ENTRY;
2914
2915         if (!strcmp(name, XATTR_NAME_LOV))
2916                 lod_object_free_striping(env, lod_dt_obj(dt));
2917
2918         rc = lod_sub_object_xattr_del(env, next, name, th);
2919         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2920                 RETURN(rc);
2921
2922         if (lo->ldo_stripenr == 0)
2923                 RETURN(0);
2924
2925         for (i = 0; i < lo->ldo_stripenr; i++) {
2926                 LASSERT(lo->ldo_stripe[i]);
2927
2928                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
2929                 if (rc != 0)
2930                         break;
2931         }
2932
2933         RETURN(rc);
2934 }
2935
2936 /**
2937  * Implementation of dt_object_operations::do_xattr_list.
2938  *
2939  * \see dt_object_operations::do_xattr_list() in the API description
2940  * for details.
2941  */
2942 static int lod_xattr_list(const struct lu_env *env,
2943                           struct dt_object *dt, const struct lu_buf *buf)
2944 {
2945         return dt_xattr_list(env, dt_object_child(dt), buf);
2946 }
2947
2948 /**
2949  * Initialize a pool the object belongs to.
2950  *
2951  * When a striped object is being created, striping configuration
2952  * may demand the stripes are allocated on a limited set of the
2953  * targets. These limited sets are known as "pools". So we copy
2954  * a pool name into the object and later actual creation methods
2955  * (like lod_object_create()) will use this information to allocate
2956  * the stripes properly.
2957  *
2958  * \param[in] o         object
2959  * \param[in] pool      pool name
2960  */
2961 int lod_object_set_pool(struct lod_object *o, char *pool)
2962 {
2963         int len;
2964
2965         if (o->ldo_pool) {
2966                 len = strlen(o->ldo_pool);
2967                 OBD_FREE(o->ldo_pool, len + 1);
2968                 o->ldo_pool = NULL;
2969         }
2970         if (pool) {
2971                 len = strlen(pool);
2972                 OBD_ALLOC(o->ldo_pool, len + 1);
2973                 if (o->ldo_pool == NULL)
2974                         return -ENOMEM;
2975                 strcpy(o->ldo_pool, pool);
2976         }
2977         return 0;
2978 }
2979
2980 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2981 {
2982         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2983 }
2984
2985
2986 /**
2987  * Cache default regular striping in the object.
2988  *
2989  * To improve performance of striped regular object creation we cache
2990  * default LOV striping (if it exists) in the parent directory object.
2991  *
2992  * \param[in] env               execution environment
2993  * \param[in] lp                object
2994  *
2995  * \retval              0 on success
2996  * \retval              negative if failed
2997  */
2998 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2999                                          struct lod_object *lp)
3000 {
3001         struct lod_thread_info  *info = lod_env_info(env);
3002         struct lov_user_md_v1   *v1 = NULL;
3003         struct lov_user_md_v3   *v3 = NULL;
3004         int                      rc;
3005         ENTRY;
3006
3007         /* called from MDD without parent being write locked,
3008          * lock it here */
3009         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
3010         rc = lod_get_lov_ea(env, lp);
3011         if (rc < 0)
3012                 GOTO(unlock, rc);
3013
3014         if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
3015                 /* don't lookup for non-existing or invalid striping */
3016                 lp->ldo_def_striping_set = 0;
3017                 lp->ldo_def_striping_cached = 1;
3018                 lp->ldo_def_stripe_size = 0;
3019                 lp->ldo_def_stripenr = 0;
3020                 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
3021                 GOTO(unlock, rc = 0);
3022         }
3023
3024         rc = 0;
3025         v1 = info->lti_ea_store;
3026         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3027                 lustre_swab_lov_user_md_v1(v1);
3028         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3029                 v3 = (struct lov_user_md_v3 *)v1;
3030                 lustre_swab_lov_user_md_v3(v3);
3031         }
3032
3033         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
3034                 GOTO(unlock, rc = 0);
3035
3036         if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
3037                 GOTO(unlock, rc = 0);
3038
3039         CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
3040                PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
3041                (int)v1->lmm_stripe_count,
3042                (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
3043
3044         lp->ldo_def_stripenr = v1->lmm_stripe_count;
3045         lp->ldo_def_stripe_size = v1->lmm_stripe_size;
3046         lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
3047         lp->ldo_def_striping_cached = 1;
3048         lp->ldo_def_striping_set = 1;
3049         if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3050                 /* XXX: sanity check here */
3051                 v3 = (struct lov_user_md_v3 *) v1;
3052                 if (v3->lmm_pool_name[0])
3053                         lod_object_set_pool(lp, v3->lmm_pool_name);
3054         }
3055         EXIT;
3056 unlock:
3057         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3058         return rc;
3059 }
3060
3061
3062 /**
3063  * Cache default directory striping in the object.
3064  *
3065  * To improve performance of striped directory creation we cache default
3066  * directory striping (if it exists) in the parent directory object.
3067  *
3068  * \param[in] env               execution environment
3069  * \param[in] lp                object
3070  *
3071  * \retval              0 on success
3072  * \retval              negative if failed
3073  */
3074 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
3075                                          struct lod_object *lp)
3076 {
3077         struct lod_thread_info  *info = lod_env_info(env);
3078         struct lmv_user_md_v1   *v1 = NULL;
3079         int                      rc;
3080         ENTRY;
3081
3082         /* called from MDD without parent being write locked,
3083          * lock it here */
3084         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
3085         rc = lod_get_default_lmv_ea(env, lp);
3086         if (rc < 0)
3087                 GOTO(unlock, rc);
3088
3089         if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
3090                 /* don't lookup for non-existing or invalid striping */
3091                 lp->ldo_dir_def_striping_set = 0;
3092                 lp->ldo_dir_def_striping_cached = 1;
3093                 lp->ldo_dir_def_stripenr = 0;
3094                 lp->ldo_dir_def_stripe_offset =
3095                                         (typeof(v1->lum_stripe_offset))(-1);
3096                 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3097                 GOTO(unlock, rc = 0);
3098         }
3099
3100         rc = 0;
3101         v1 = info->lti_ea_store;
3102
3103         lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3104         lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3105         lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3106         lp->ldo_dir_def_striping_set = 1;
3107         lp->ldo_dir_def_striping_cached = 1;
3108
3109         EXIT;
3110 unlock:
3111         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3112         return rc;
3113 }
3114
3115 /**
3116  * Cache default striping in the object.
3117  *
3118  * To improve performance of striped object creation we cache default striping
3119  * (if it exists) in the parent directory object. We always cache default
3120  * striping for the regular files (stored in LOV EA) and we cache default
3121  * striping for the directories if requested by \a child_mode (when a new
3122  * directory is being created).
3123  *
3124  * \param[in] env               execution environment
3125  * \param[in] lp                object
3126  * \param[in] child_mode        new object's mode
3127  *
3128  * \retval              0 on success
3129  * \retval              negative if failed
3130  */
3131 static int lod_cache_parent_striping(const struct lu_env *env,
3132                                      struct lod_object *lp,
3133                                      umode_t child_mode)
3134 {
3135         int rc = 0;
3136         ENTRY;
3137
3138         if (!lp->ldo_def_striping_cached) {
3139                 /* we haven't tried to get default striping for
3140                  * the directory yet, let's cache it in the object */
3141                 rc = lod_cache_parent_lov_striping(env, lp);
3142                 if (rc != 0)
3143                         RETURN(rc);
3144         }
3145
3146         /* If the parent is on the remote MDT, we should always
3147          * try to refresh the default stripeEA cache, because we
3148          * do not cache default striping information for remote
3149          * object. */
3150         if (S_ISDIR(child_mode) && (!lp->ldo_dir_def_striping_cached ||
3151                                     dt_object_remote(&lp->ldo_obj)))
3152                 rc = lod_cache_parent_lmv_striping(env, lp);
3153
3154         RETURN(rc);
3155 }
3156
3157 /**
3158  * Implementation of dt_object_operations::do_ah_init.
3159  *
3160  * This method is used to make a decision on the striping configuration for the
3161  * object being created. It can be taken from the \a parent object if it exists,
3162  * or filesystem's default. The resulting configuration (number of stripes,
3163  * stripe size/offset, pool name, etc) is stored in the object itself and will
3164  * be used by the methods like ->doo_declare_create().
3165  *
3166  * \see dt_object_operations::do_ah_init() in the API description for details.
3167  */
3168 static void lod_ah_init(const struct lu_env *env,
3169                         struct dt_allocation_hint *ah,
3170                         struct dt_object *parent,
3171                         struct dt_object *child,
3172                         umode_t child_mode)
3173 {
3174         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3175         struct dt_object  *nextp = NULL;
3176         struct dt_object  *nextc;
3177         struct lod_object *lp = NULL;
3178         struct lod_object *lc;
3179         struct lov_desc   *desc;
3180         int               rc;
3181         ENTRY;
3182
3183         LASSERT(child);
3184
3185         if (likely(parent)) {
3186                 nextp = dt_object_child(parent);
3187                 lp = lod_dt_obj(parent);
3188                 rc = lod_load_striping(env, lp);
3189                 if (rc != 0)
3190                         return;
3191         }
3192
3193         nextc = dt_object_child(child);
3194         lc = lod_dt_obj(child);
3195
3196         LASSERT(lc->ldo_stripenr == 0);
3197         LASSERT(lc->ldo_stripe == NULL);
3198
3199         if (!dt_object_exists(nextc))
3200                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3201
3202         if (S_ISDIR(child_mode)) {
3203                 if (lc->ldo_dir_stripe == NULL) {
3204                         OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3205                         if (lc->ldo_dir_stripe == NULL)
3206                                 return;
3207                 }
3208
3209                 LASSERT(lp != NULL);
3210                 if (lp->ldo_dir_stripe == NULL) {
3211                         OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3212                         if (lp->ldo_dir_stripe == NULL)
3213                                 return;
3214                 }
3215
3216                 rc = lod_cache_parent_striping(env, lp, child_mode);
3217                 if (rc != 0)
3218                         return;
3219
3220                 /* transfer defaults to new directory */
3221                 if (lp->ldo_def_striping_set) {
3222                         if (lp->ldo_pool)
3223                                 lod_object_set_pool(lc, lp->ldo_pool);
3224                         lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3225                         lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3226                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3227                         lc->ldo_def_striping_set = 1;
3228                         lc->ldo_def_striping_cached = 1;
3229                         CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3230                                (int)lc->ldo_def_stripe_size,
3231                                (int)lc->ldo_def_stripe_offset,
3232                                (int)lc->ldo_def_stripenr);
3233                 }
3234
3235                 /* transfer dir defaults to new directory */
3236                 if (lp->ldo_dir_def_striping_set) {
3237                         lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3238                         lc->ldo_dir_def_stripe_offset =
3239                                                   lp->ldo_dir_def_stripe_offset;
3240                         lc->ldo_dir_def_hash_type =
3241                                                   lp->ldo_dir_def_hash_type;
3242                         lc->ldo_dir_def_striping_set = 1;
3243                         lc->ldo_dir_def_striping_cached = 1;
3244                         CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3245                                (int)lc->ldo_dir_def_stripenr,
3246                                (int)lc->ldo_dir_def_stripe_offset,
3247                                lc->ldo_dir_def_hash_type);
3248                 }
3249
3250                 /* It should always honour the specified stripes */
3251                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3252                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3253
3254                         rc = lod_verify_md_striping(d, lum1);
3255                         if (rc == 0 &&
3256                                 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3257                                 /* Directory will be striped only if
3258                                  * stripe_count > 1 */
3259                                 lc->ldo_stripenr =
3260                                         le32_to_cpu(lum1->lum_stripe_count);
3261                                 lc->ldo_dir_stripe_offset =
3262                                         le32_to_cpu(lum1->lum_stripe_offset);
3263                                 lc->ldo_dir_hash_type =
3264                                         le32_to_cpu(lum1->lum_hash_type);
3265                                 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3266                                        lc->ldo_stripenr,
3267                                        (int)lc->ldo_dir_stripe_offset);
3268                         }
3269                 /* then check whether there is default stripes from parent */
3270                 } else if (lp->ldo_dir_def_striping_set) {
3271                         /* If there are default dir stripe from parent */
3272                         lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3273                         lc->ldo_dir_stripe_offset =
3274                                         lp->ldo_dir_def_stripe_offset;
3275                         lc->ldo_dir_hash_type =
3276                                         lp->ldo_dir_def_hash_type;
3277                         CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3278                                lc->ldo_stripenr,
3279                                (int)lc->ldo_dir_stripe_offset);
3280                 } else {
3281                         /* set default stripe for this directory */
3282                         lc->ldo_stripenr = 0;
3283                         lc->ldo_dir_stripe_offset = -1;
3284                 }
3285
3286                 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3287                        lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3288
3289                 goto out;
3290         }
3291
3292         /*
3293          * if object is going to be striped over OSTs, transfer default
3294          * striping information to the child, so that we can use it
3295          * during declaration and creation
3296          */
3297         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3298                                         lu_object_fid(&child->do_lu)))
3299                 goto out;
3300         /*
3301          * try from the parent
3302          */
3303         if (likely(parent)) {
3304                 lod_cache_parent_striping(env, lp, child_mode);
3305
3306                 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3307
3308                 if (lp->ldo_def_striping_set) {
3309                         if (lp->ldo_pool)
3310                                 lod_object_set_pool(lc, lp->ldo_pool);
3311                         lc->ldo_stripenr = lp->ldo_def_stripenr;
3312                         lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3313                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3314                         CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3315                                lc->ldo_stripenr, lc->ldo_stripe_size,
3316                                lp->ldo_pool ? lp->ldo_pool : "");
3317                 }
3318         }
3319
3320         /*
3321          * if the parent doesn't provide with specific pattern, grab fs-wide one
3322          */
3323         desc = &d->lod_desc;
3324         if (lc->ldo_stripenr == 0)
3325                 lc->ldo_stripenr = desc->ld_default_stripe_count;
3326         if (lc->ldo_stripe_size == 0)
3327                 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3328         CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3329                lc->ldo_stripenr, lc->ldo_stripe_size,
3330                lc->ldo_pool ? lc->ldo_pool : "");
3331
3332 out:
3333         /* we do not cache stripe information for slave stripe, see
3334          * lod_xattr_set_lov_on_dir */
3335         if (lp != NULL && lp->ldo_dir_slave_stripe)
3336                 lod_lov_stripe_cache_clear(lp);
3337
3338         EXIT;
3339 }
3340
3341 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
3342 /**
3343  * Size initialization on late striping.
3344  *
3345  * Propagate the size of a truncated object to a deferred striping.
3346  * This function handles a special case when truncate was done on a
3347  * non-striped object and now while the striping is being created
3348  * we can't lose that size, so we have to propagate it to the stripes
3349  * being created.
3350  *
3351  * \param[in] env       execution environment
3352  * \param[in] dt        object
3353  * \param[in] th        transaction handle
3354  *
3355  * \retval              0 on success
3356  * \retval              negative if failed
3357  */
3358 static int lod_declare_init_size(const struct lu_env *env,
3359                                  struct dt_object *dt, struct thandle *th)
3360 {
3361         struct dt_object   *next = dt_object_child(dt);
3362         struct lod_object  *lo = lod_dt_obj(dt);
3363         struct lu_attr     *attr = &lod_env_info(env)->lti_attr;
3364         uint64_t            size, offs;
3365         int                 rc, stripe;
3366         ENTRY;
3367
3368         /* XXX: we support the simplest (RAID0) striping so far */
3369         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3370         LASSERT(lo->ldo_stripe_size > 0);
3371
3372         rc = dt_attr_get(env, next, attr);
3373         LASSERT(attr->la_valid & LA_SIZE);
3374         if (rc)
3375                 RETURN(rc);
3376
3377         size = attr->la_size;
3378         if (size == 0)
3379                 RETURN(0);
3380
3381         /* ll_do_div64(a, b) returns a % b, and a = a / b */
3382         ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3383         stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3384
3385         size = size * lo->ldo_stripe_size;
3386         offs = attr->la_size;
3387         size += ll_do_div64(offs, lo->ldo_stripe_size);
3388
3389         attr->la_valid = LA_SIZE;
3390         attr->la_size = size;
3391
3392         rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3393                                              th);
3394
3395         RETURN(rc);
3396 }
3397
3398 /**
3399  * Declare creation of striped object.
3400  *
3401  * The function declares creation stripes for a regular object. The function
3402  * also declares whether the stripes will be created with non-zero size if
3403  * previously size was set non-zero on the master object. If object \a dt is
3404  * not local, then only fully defined striping can be applied in \a lovea.
3405  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3406  * for the details.
3407  *
3408  * \param[in] env       execution environment
3409  * \param[in] dt        object
3410  * \param[in] attr      attributes the stripes will be created with
3411  * \param[in] lovea     a buffer containing striping description
3412  * \param[in] th        transaction handle
3413  *
3414  * \retval              0 on success
3415  * \retval              negative if failed
3416  */
3417 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3418                                struct lu_attr *attr,
3419                                const struct lu_buf *lovea, struct thandle *th)
3420 {
3421         struct lod_thread_info  *info = lod_env_info(env);
3422         struct dt_object        *next = dt_object_child(dt);
3423         struct lod_object       *lo = lod_dt_obj(dt);
3424         int                      rc;
3425         ENTRY;
3426
3427         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3428                 /* failed to create striping, let's reset
3429                  * config so that others don't get confused */
3430                 lod_object_free_striping(env, lo);
3431                 GOTO(out, rc = -ENOMEM);
3432         }
3433
3434         if (!dt_object_remote(next)) {
3435                 /* choose OST and generate appropriate objects */
3436                 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3437                 if (rc) {
3438                         /* failed to create striping, let's reset
3439                          * config so that others don't get confused */
3440                         lod_object_free_striping(env, lo);
3441                         GOTO(out, rc);
3442                 }
3443
3444                 /*
3445                  * declare storage for striping data
3446                  */
3447                 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3448                                 lo->ldo_pool ?  LOV_MAGIC_V3 : LOV_MAGIC_V1);
3449         } else {
3450                 /* LOD can not choose OST objects for remote objects, i.e.
3451                  * stripes must be ready before that. Right now, it can only
3452                  * happen during migrate, i.e. migrate process needs to create
3453                  * remote regular file (mdd_migrate_create), then the migrate
3454                  * process will provide stripeEA. */
3455                 LASSERT(lovea != NULL);
3456                 info->lti_buf = *lovea;
3457         }
3458
3459         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3460                                               XATTR_NAME_LOV, 0, th);
3461         if (rc)
3462                 GOTO(out, rc);
3463
3464         /*
3465          * if striping is created with local object's size > 0,
3466          * we have to propagate this size to specific object
3467          * the case is possible only when local object was created previously
3468          */
3469         if (dt_object_exists(next))
3470                 rc = lod_declare_init_size(env, dt, th);
3471
3472 out:
3473         RETURN(rc);
3474 }
3475
3476 /**
3477  * Implementation of dt_object_operations::do_declare_create.
3478  *
3479  * The method declares creation of a new object. If the object will be striped,
3480  * then helper functions are called to find FIDs for the stripes, declare
3481  * creation of the stripes and declare initialization of the striping
3482  * information to be stored in the master object.
3483  *
3484  * \see dt_object_operations::do_declare_create() in the API description
3485  * for details.
3486  */
3487 static int lod_declare_object_create(const struct lu_env *env,
3488                                      struct dt_object *dt,
3489                                      struct lu_attr *attr,
3490                                      struct dt_allocation_hint *hint,
3491                                      struct dt_object_format *dof,
3492                                      struct thandle *th)
3493 {
3494         struct dt_object   *next = dt_object_child(dt);
3495         struct lod_object  *lo = lod_dt_obj(dt);
3496         int                 rc;
3497         ENTRY;
3498
3499         LASSERT(dof);
3500         LASSERT(attr);
3501         LASSERT(th);
3502
3503         /*
3504          * first of all, we declare creation of local object
3505          */
3506         rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3507         if (rc != 0)
3508                 GOTO(out, rc);
3509
3510         if (dof->dof_type == DFT_SYM)
3511                 dt->do_body_ops = &lod_body_lnk_ops;
3512         else if (dof->dof_type == DFT_REGULAR)
3513                 dt->do_body_ops = &lod_body_ops;
3514
3515         /*
3516          * it's lod_ah_init() that has decided the object will be striped
3517          */
3518         if (dof->dof_type == DFT_REGULAR) {
3519                 /* callers don't want stripes */
3520                 /* XXX: all tricky interactions with ->ah_make_hint() decided
3521                  * to use striping, then ->declare_create() behaving differently
3522                  * should be cleaned */
3523                 if (dof->u.dof_reg.striped == 0)
3524                         lo->ldo_stripenr = 0;
3525                 if (lo->ldo_stripenr > 0)
3526                         rc = lod_declare_striped_object(env, dt, attr,
3527                                                         NULL, th);
3528         } else if (dof->dof_type == DFT_DIR) {
3529                 struct seq_server_site *ss;
3530
3531                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3532
3533                 /* If the parent has default stripeEA, and client
3534                  * did not find it before sending create request,
3535                  * then MDT will return -EREMOTE, and client will
3536                  * retrieve the default stripeEA and re-create the
3537                  * sub directory.
3538                  *
3539                  * Note: if dah_eadata != NULL, it means creating the
3540                  * striped directory with specified stripeEA, then it
3541                  * should ignore the default stripeEA */
3542                 if (hint != NULL && hint->dah_eadata == NULL) {
3543                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
3544                                 GOTO(out, rc = -EREMOTE);
3545
3546                         if (lo->ldo_dir_stripe_offset == -1) {
3547                                 /* child and parent should be in the same MDT */
3548                                 if (hint->dah_parent != NULL &&
3549                                     dt_object_remote(hint->dah_parent))
3550                                         GOTO(out, rc = -EREMOTE);
3551                         } else if (lo->ldo_dir_stripe_offset !=
3552                                    ss->ss_node_id) {
3553                                 GOTO(out, rc = -EREMOTE);
3554                         }
3555                 }
3556
3557                 /* Orphan object (like migrating object) does not have
3558                  * lod_dir_stripe, see lod_ah_init */
3559                 if (lo->ldo_dir_stripe != NULL)
3560                         rc = lod_declare_dir_striping_create(env, dt, attr,
3561                                                              dof, th);
3562         }
3563 out:
3564         RETURN(rc);
3565 }
3566
3567 /**
3568  * Creation of a striped regular object.
3569  *
3570  * The function is called to create the stripe objects for a regular
3571  * striped file. This can happen at the initial object creation or
3572  * when the caller asks LOD to do so using ->do_xattr_set() method
3573  * (so called late striping). Notice all the information are already
3574  * prepared in the form of the list of objects (ldo_stripe field).
3575  * This is done during declare phase.
3576  *
3577  * \param[in] env       execution environment
3578  * \param[in] dt        object
3579  * \param[in] attr      attributes the stripes will be created with
3580  * \param[in] dof       format of stripes (see OSD API description)
3581  * \param[in] th        transaction handle
3582  *
3583  * \retval              0 on success
3584  * \retval              negative if failed
3585  */
3586 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3587                         struct lu_attr *attr, struct dt_object_format *dof,
3588                         struct thandle *th)
3589 {
3590         struct lod_object *lo = lod_dt_obj(dt);
3591         int                rc = 0, i;
3592         ENTRY;
3593
3594         LASSERT(lo->ldo_striping_cached == 0);
3595
3596         /* create all underlying objects */
3597         for (i = 0; i < lo->ldo_stripenr; i++) {
3598                 LASSERT(lo->ldo_stripe[i]);
3599                 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3600                                            dof, th);
3601                 if (rc)
3602                         break;
3603         }
3604
3605         if (rc == 0) {
3606                 rc = lod_generate_and_set_lovea(env, lo, th);
3607                 if (rc == 0)
3608                         lo->ldo_striping_cached = 1;
3609         }
3610
3611         RETURN(rc);
3612 }
3613
3614 /**
3615  * Implementation of dt_object_operations::do_create.
3616  *
3617  * If any of preceeding methods (like ->do_declare_create(),
3618  * ->do_ah_init(), etc) chose to create a striped object,
3619  * then this method will create the master and the stripes.
3620  *
3621  * \see dt_object_operations::do_create() in the API description for details.
3622  */
3623 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3624                              struct lu_attr *attr,
3625                              struct dt_allocation_hint *hint,
3626                              struct dt_object_format *dof, struct thandle *th)
3627 {
3628         struct lod_object  *lo = lod_dt_obj(dt);
3629         int                 rc;
3630         ENTRY;
3631
3632         /* create local object */
3633         rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3634                                    th);
3635         if (rc != 0)
3636                 RETURN(rc);
3637
3638         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3639             lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3640                 rc = lod_striping_create(env, dt, attr, dof, th);
3641
3642         RETURN(rc);
3643 }
3644
3645 /**
3646  * Implementation of dt_object_operations::do_declare_destroy.
3647  *
3648  * If the object is a striped directory, then the function declares reference
3649  * removal from the master object (this is an index) to the stripes and declares
3650  * destroy of all the stripes. In all the cases, it declares an intention to
3651  * destroy the object itself.
3652  *
3653  * \see dt_object_operations::do_declare_destroy() in the API description
3654  * for details.
3655  */
3656 static int lod_declare_object_destroy(const struct lu_env *env,
3657                                       struct dt_object *dt,
3658                                       struct thandle *th)
3659 {
3660         struct dt_object   *next = dt_object_child(dt);
3661         struct lod_object  *lo = lod_dt_obj(dt);
3662         struct lod_thread_info *info = lod_env_info(env);
3663         char               *stripe_name = info->lti_key;
3664         int                 rc, i;
3665         ENTRY;
3666
3667         /*
3668          * load striping information, notice we don't do this when object
3669          * is being initialized as we don't need this information till
3670          * few specific cases like destroy, chown
3671          */
3672         rc = lod_load_striping(env, lo);
3673         if (rc)
3674                 RETURN(rc);
3675
3676         /* declare destroy for all underlying objects */
3677         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3678                 rc = next->do_ops->do_index_try(env, next,
3679                                                 &dt_directory_features);
3680                 if (rc != 0)
3681                         RETURN(rc);
3682
3683                 for (i = 0; i < lo->ldo_stripenr; i++) {
3684                         rc = lod_sub_object_declare_ref_del(env, next, th);
3685                         if (rc != 0)
3686                                 RETURN(rc);
3687
3688                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3689                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3690                                 i);
3691                         rc = lod_sub_object_declare_delete(env, next,
3692                                         (const struct dt_key *)stripe_name, th);
3693                         if (rc != 0)
3694                                 RETURN(rc);
3695                 }
3696         }
3697
3698         /*
3699          * we declare destroy for the local object
3700          */
3701         rc = lod_sub_object_declare_destroy(env, next, th);
3702         if (rc)
3703                 RETURN(rc);
3704
3705         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3706                 RETURN(0);
3707
3708         /* declare destroy all striped objects */
3709         for (i = 0; i < lo->ldo_stripenr; i++) {
3710                 if (lo->ldo_stripe[i] == NULL)
3711                         continue;
3712
3713                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3714                         rc = lod_sub_object_declare_ref_del(env,
3715                                         lo->ldo_stripe[i], th);
3716
3717                 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3718                                         th);
3719                 if (rc != 0)
3720                         break;
3721         }
3722
3723         RETURN(rc);
3724 }
3725
3726 /**
3727  * Implementation of dt_object_operations::do_destroy.
3728  *
3729  * If the object is a striped directory, then the function removes references
3730  * from the master object (this is an index) to the stripes and destroys all
3731  * the stripes. In all the cases, the function destroys the object itself.
3732  *
3733  * \see dt_object_operations::do_destroy() in the API description for details.
3734  */
3735 static int lod_object_destroy(const struct lu_env *env,
3736                 struct dt_object *dt, struct thandle *th)
3737 {
3738         struct dt_object  *next = dt_object_child(dt);
3739         struct lod_object *lo = lod_dt_obj(dt);
3740         struct lod_thread_info *info = lod_env_info(env);
3741         char               *stripe_name = info->lti_key;
3742         unsigned int       i;
3743         int                rc;
3744         ENTRY;
3745
3746         /* destroy sub-stripe of master object */
3747         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3748                 rc = next->do_ops->do_index_try(env, next,
3749                                                 &dt_directory_features);
3750                 if (rc != 0)
3751                         RETURN(rc);
3752
3753                 for (i = 0; i < lo->ldo_stripenr; i++) {
3754                         rc = lod_sub_object_ref_del(env, next, th);
3755                         if (rc != 0)
3756                                 RETURN(rc);
3757
3758                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3759                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3760                                 i);
3761
3762                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3763                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3764                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3765
3766                         rc = lod_sub_object_delete(env, next,
3767                                        (const struct dt_key *)stripe_name, th);
3768                         if (rc != 0)
3769                                 RETURN(rc);
3770                 }
3771         }
3772
3773         rc = lod_sub_object_destroy(env, next, th);
3774         if (rc != 0)
3775                 RETURN(rc);
3776
3777         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3778                 RETURN(0);
3779
3780         /* destroy all striped objects */
3781         for (i = 0; i < lo->ldo_stripenr; i++) {
3782                 if (likely(lo->ldo_stripe[i] != NULL) &&
3783                     (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3784                      i == cfs_fail_val)) {
3785                         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3786                                 dt_write_lock(env, lo->ldo_stripe[i],
3787                                               MOR_TGT_CHILD);
3788                                 rc = lod_sub_object_ref_del(env,
3789                                                 lo->ldo_stripe[i], th);
3790                                 dt_write_unlock(env, lo->ldo_stripe[i]);
3791                                 if (rc != 0)
3792                                         break;
3793                         }
3794
3795                         rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3796                         if (rc != 0)
3797                                 break;
3798                 }
3799         }
3800
3801         RETURN(rc);
3802 }
3803
3804 /**
3805  * Implementation of dt_object_operations::do_declare_ref_add.
3806  *
3807  * \see dt_object_operations::do_declare_ref_add() in the API description
3808  * for details.
3809  */
3810 static int lod_declare_ref_add(const struct lu_env *env,
3811                                struct dt_object *dt, struct thandle *th)
3812 {
3813         return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3814 }
3815
3816 /**
3817  * Implementation of dt_object_operations::do_ref_add.
3818  *
3819  * \see dt_object_operations::do_ref_add() in the API description for details.
3820  */
3821 static int lod_ref_add(const struct lu_env *env,
3822                        struct dt_object *dt, struct thandle *th)
3823 {
3824         return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3825 }
3826
3827 /**
3828  * Implementation of dt_object_operations::do_declare_ref_del.
3829  *
3830  * \see dt_object_operations::do_declare_ref_del() in the API description
3831  * for details.
3832  */
3833 static int lod_declare_ref_del(const struct lu_env *env,
3834                                struct dt_object *dt, struct thandle *th)
3835 {
3836         return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3837 }
3838
3839 /**
3840  * Implementation of dt_object_operations::do_ref_del
3841  *
3842  * \see dt_object_operations::do_ref_del() in the API description for details.
3843  */
3844 static int lod_ref_del(const struct lu_env *env,
3845                        struct dt_object *dt, struct thandle *th)
3846 {
3847         return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3848 }
3849
3850 /**
3851  * Implementation of dt_object_operations::do_object_sync.
3852  *
3853  * \see dt_object_operations::do_object_sync() in the API description
3854  * for details.
3855  */
3856 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3857                            __u64 start, __u64 end)
3858 {
3859         return dt_object_sync(env, dt_object_child(dt), start, end);
3860 }
3861
3862 struct lod_slave_locks  {
3863         int                     lsl_lock_count;
3864         struct lustre_handle    lsl_handle[0];
3865 };
3866
3867 /**
3868  * Release LDLM locks on the stripes of a striped directory.
3869  *
3870  * Iterates over all the locks taken on the stripe objects and
3871  * release them using ->do_object_unlock() method.
3872  *
3873  * \param[in] env       execution environment
3874  * \param[in] dt        striped object
3875  * \param[in] einfo     lock description
3876  * \param[in] policy    data describing requested lock
3877  *
3878  * \retval              0 on success
3879  * \retval              negative if failed
3880  */
3881 static int lod_object_unlock_internal(const struct lu_env *env,
3882                                       struct dt_object *dt,
3883                                       struct ldlm_enqueue_info *einfo,
3884                                       ldlm_policy_data_t *policy)
3885 {
3886         struct lod_slave_locks  *slave_locks = einfo->ei_cbdata;
3887         int                     rc = 0;
3888         int                     i;
3889         ENTRY;
3890
3891         if (slave_locks == NULL)
3892                 RETURN(0);
3893
3894         for (i = 1; i < slave_locks->lsl_lock_count; i++) {
3895                 if (lustre_handle_is_used(&slave_locks->lsl_handle[i]))
3896                         ldlm_lock_decref(&slave_locks->lsl_handle[i],
3897                                          einfo->ei_mode);
3898         }
3899
3900         RETURN(rc);
3901 }
3902
3903 /**
3904  * Implementation of dt_object_operations::do_object_unlock.
3905  *
3906  * Used to release LDLM lock(s).
3907  *
3908  * \see dt_object_operations::do_object_unlock() in the API description
3909  * for details.
3910  */
3911 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3912                              struct ldlm_enqueue_info *einfo,
3913                              union ldlm_policy_data *policy)
3914 {
3915         struct lod_object       *lo = lod_dt_obj(dt);
3916         struct lod_slave_locks  *slave_locks = einfo->ei_cbdata;
3917         int                     slave_locks_size;
3918         int                     rc;
3919         ENTRY;
3920
3921         if (slave_locks == NULL)
3922                 RETURN(0);
3923
3924         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3925                 RETURN(-ENOTDIR);
3926
3927         /* Note: for remote lock for single stripe dir, MDT will cancel
3928          * the lock by lockh directly */
3929         if (lo->ldo_stripenr <= 1 && dt_object_remote(dt_object_child(dt)))
3930                 RETURN(0);
3931
3932         /* Only cancel slave lock for striped dir */
3933         rc = lod_object_unlock_internal(env, dt, einfo, policy);
3934
3935         slave_locks_size = sizeof(*slave_locks) + slave_locks->lsl_lock_count *
3936                            sizeof(slave_locks->lsl_handle[0]);
3937         OBD_FREE(slave_locks, slave_locks_size);
3938         einfo->ei_cbdata = NULL;
3939
3940         RETURN(rc);
3941 }
3942
3943 /**
3944  * Implementation of dt_object_operations::do_object_lock.
3945  *
3946  * Used to get LDLM lock on the non-striped and striped objects.
3947  *
3948  * \see dt_object_operations::do_object_lock() in the API description
3949  * for details.
3950  */
3951 static int lod_object_lock(const struct lu_env *env,
3952                            struct dt_object *dt,
3953                            struct lustre_handle *lh,
3954                            struct ldlm_enqueue_info *einfo,
3955                            union ldlm_policy_data *policy)
3956 {
3957         struct lod_object       *lo = lod_dt_obj(dt);
3958         int                     rc = 0;
3959         int                     i;
3960         int                     slave_locks_size;
3961         struct lod_slave_locks  *slave_locks = NULL;
3962         ENTRY;
3963
3964         /* remote object lock */
3965         if (!einfo->ei_enq_slave) {
3966                 LASSERT(dt_object_remote(dt));
3967                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3968                                       policy);
3969         }
3970
3971         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3972                 RETURN(-ENOTDIR);
3973
3974         rc = lod_load_striping(env, lo);
3975         if (rc != 0)
3976                 RETURN(rc);
3977
3978         /* No stripes */
3979         if (lo->ldo_stripenr <= 1)
3980                 RETURN(0);
3981
3982         slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3983                            sizeof(slave_locks->lsl_handle[0]);
3984         /* Freed in lod_object_unlock */
3985         OBD_ALLOC(slave_locks, slave_locks_size);
3986         if (slave_locks == NULL)
3987                 RETURN(-ENOMEM);
3988         slave_locks->lsl_lock_count = lo->ldo_stripenr;
3989
3990         /* striped directory lock */
3991         for (i = 1; i < lo->ldo_stripenr; i++) {
3992                 struct lustre_handle    lockh;
3993                 struct ldlm_res_id      *res_id;
3994
3995                 res_id = &lod_env_info(env)->lti_res_id;
3996                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3997                                        res_id);
3998                 einfo->ei_res_id = res_id;
3999
4000                 LASSERT(lo->ldo_stripe[i] != NULL);
4001                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4002                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4003                                             einfo, policy);
4004                 } else {
4005                         struct ldlm_namespace *ns = einfo->ei_namespace;
4006                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4007                         ldlm_completion_callback completion = einfo->ei_cb_cp;
4008                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
4009
4010                         /* This only happens if there are mulitple stripes
4011                          * on the master MDT, i.e. except stripe0, there are
4012                          * other stripes on the Master MDT as well, Only
4013                          * happens in the test case right now. */
4014                         LASSERT(ns != NULL);
4015                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4016                                                     policy, einfo->ei_mode,
4017                                                     &dlmflags, blocking,
4018                                                     completion, NULL,
4019                                                     NULL, 0, LVB_T_NONE,
4020                                                     NULL, &lockh);
4021                 }
4022                 if (rc != 0)
4023                         GOTO(out, rc);
4024                 slave_locks->lsl_handle[i] = lockh;
4025         }
4026
4027         einfo->ei_cbdata = slave_locks;
4028
4029 out:
4030         if (rc != 0 && slave_locks != NULL) {
4031                 einfo->ei_cbdata = slave_locks;
4032                 lod_object_unlock_internal(env, dt, einfo, policy);
4033                 OBD_FREE(slave_locks, slave_locks_size);
4034                 einfo->ei_cbdata = NULL;
4035         }
4036
4037         RETURN(rc);
4038 }
4039
4040 struct dt_object_operations lod_obj_ops = {
4041         .do_read_lock           = lod_object_read_lock,
4042         .do_write_lock          = lod_object_write_lock,
4043         .do_read_unlock         = lod_object_read_unlock,
4044         .do_write_unlock        = lod_object_write_unlock,
4045         .do_write_locked        = lod_object_write_locked,
4046         .do_attr_get            = lod_attr_get,
4047         .do_declare_attr_set    = lod_declare_attr_set,
4048         .do_attr_set            = lod_attr_set,
4049         .do_xattr_get           = lod_xattr_get,
4050         .do_declare_xattr_set   = lod_declare_xattr_set,
4051         .do_xattr_set           = lod_xattr_set,
4052         .do_declare_xattr_del   = lod_declare_xattr_del,
4053         .do_xattr_del           = lod_xattr_del,
4054         .do_xattr_list          = lod_xattr_list,
4055         .do_ah_init             = lod_ah_init,
4056         .do_declare_create      = lod_declare_object_create,
4057         .do_create              = lod_object_create,
4058         .do_declare_destroy     = lod_declare_object_destroy,
4059         .do_destroy             = lod_object_destroy,
4060         .do_index_try           = lod_index_try,
4061         .do_declare_ref_add     = lod_declare_ref_add,
4062         .do_ref_add             = lod_ref_add,
4063         .do_declare_ref_del     = lod_declare_ref_del,
4064         .do_ref_del             = lod_ref_del,
4065         .do_object_sync         = lod_object_sync,
4066         .do_object_lock         = lod_object_lock,
4067         .do_object_unlock       = lod_object_unlock,
4068 };
4069
4070 /**
4071  * Implementation of dt_body_operations::dbo_read.
4072  *
4073  * \see dt_body_operations::dbo_read() in the API description for details.
4074  */
4075 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
4076                         struct lu_buf *buf, loff_t *pos)
4077 {
4078         struct dt_object *next = dt_object_child(dt);
4079         return next->do_body_ops->dbo_read(env, next, buf, pos);
4080 }
4081
4082 /**
4083  * Implementation of dt_body_operations::dbo_declare_write.
4084  *
4085  * \see dt_body_operations::dbo_declare_write() in the API description
4086  * for details.
4087  */
4088 static ssize_t lod_declare_write(const struct lu_env *env,
4089                                  struct dt_object *dt,
4090                                  const struct lu_buf *buf, loff_t pos,
4091                                  struct thandle *th)
4092 {
4093         return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4094                                             th);
4095 }
4096
4097 /**
4098  * Implementation of dt_body_operations::dbo_write.
4099  *
4100  * \see dt_body_operations::dbo_write() in the API description for details.
4101  */
4102 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4103                          const struct lu_buf *buf, loff_t *pos,
4104                          struct thandle *th, int iq)
4105 {
4106         return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4107 }
4108
4109 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4110                              __u64 start, __u64 end, struct thandle *th)
4111 {
4112         if (dt_object_remote(dt))
4113                 return -ENOTSUPP;
4114
4115         return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4116                                             end, th);
4117 }
4118
4119 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4120                      __u64 start, __u64 end, struct thandle *th)
4121 {
4122         if (dt_object_remote(dt))
4123                 return -ENOTSUPP;
4124
4125         return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4126 }
4127
4128 static const struct dt_body_operations lod_body_lnk_ops = {
4129         .dbo_read               = lod_read,
4130         .dbo_declare_write      = lod_declare_write,
4131         .dbo_write              = lod_write
4132 };
4133
4134 static const struct dt_body_operations lod_body_ops = {
4135         .dbo_read               = lod_read,
4136         .dbo_declare_write      = lod_declare_write,
4137         .dbo_write              = lod_write,
4138         .dbo_declare_punch      = lod_declare_punch,
4139         .dbo_punch              = lod_punch,
4140 };
4141
4142 /**
4143  * Implementation of lu_object_operations::loo_object_init.
4144  *
4145  * The function determines the type and the index of the target device using
4146  * sequence of the object's FID. Then passes control down to the
4147  * corresponding device:
4148  *  OSD for the local objects, OSP for remote
4149  *
4150  * \see lu_object_operations::loo_object_init() in the API description
4151  * for details.
4152  */
4153 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4154                            const struct lu_object_conf *conf)
4155 {
4156         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
4157         struct lu_device        *cdev   = NULL;
4158         struct lu_object        *cobj;
4159         struct lod_tgt_descs    *ltd    = NULL;
4160         struct lod_tgt_desc     *tgt;
4161         u32                      idx    = 0;
4162         int                      type   = LU_SEQ_RANGE_ANY;
4163         int                      rc;
4164         ENTRY;
4165
4166         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4167         if (rc != 0) {
4168                 /* Note: Sometimes, it will Return EAGAIN here, see
4169                  * ptrlpc_import_delay_req(), which might confuse
4170                  * lu_object_find_at() and make it wait there incorrectly.
4171                  * so we convert it to EIO here.*/
4172                 if (rc == -EAGAIN)
4173                         rc = -EIO;
4174
4175                 RETURN(rc);
4176         }
4177
4178         if (type == LU_SEQ_RANGE_MDT &&
4179             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4180                 cdev = &lod->lod_child->dd_lu_dev;
4181         } else if (type == LU_SEQ_RANGE_MDT) {
4182                 ltd = &lod->lod_mdt_descs;
4183                 lod_getref(ltd);
4184         } else if (type == LU_SEQ_RANGE_OST) {
4185                 ltd = &lod->lod_ost_descs;
4186                 lod_getref(ltd);
4187         } else {
4188                 LBUG();
4189         }
4190
4191         if (ltd != NULL) {
4192                 if (ltd->ltd_tgts_size > idx &&
4193                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4194                         tgt = LTD_TGT(ltd, idx);
4195
4196                         LASSERT(tgt != NULL);
4197                         LASSERT(tgt->ltd_tgt != NULL);
4198
4199                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
4200                 }
4201                 lod_putref(lod, ltd);
4202         }
4203
4204         if (unlikely(cdev == NULL))
4205                 RETURN(-ENOENT);
4206
4207         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4208         if (unlikely(cobj == NULL))
4209                 RETURN(-ENOMEM);
4210
4211         lu_object_add(lo, cobj);
4212
4213         RETURN(0);
4214 }
4215
4216 /**
4217  *
4218  * Release resources associated with striping.
4219  *
4220  * If the object is striped (regular or directory), then release
4221  * the stripe objects references and free the ldo_stripe array.
4222  *
4223  * \param[in] env       execution environment
4224  * \param[in] lo        object
4225  */
4226 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4227 {
4228         int i;
4229
4230         if (lo->ldo_dir_stripe != NULL) {
4231                 OBD_FREE_PTR(lo->ldo_dir_stripe);
4232                 lo->ldo_dir_stripe = NULL;
4233         }
4234
4235         if (lo->ldo_stripe) {
4236                 LASSERT(lo->ldo_stripes_allocated > 0);
4237
4238                 for (i = 0; i < lo->ldo_stripenr; i++) {
4239                         if (lo->ldo_stripe[i])
4240                                 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4241                 }
4242
4243                 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4244                 OBD_FREE(lo->ldo_stripe, i);
4245                 lo->ldo_stripe = NULL;
4246                 lo->ldo_stripes_allocated = 0;
4247         }
4248         lo->ldo_striping_cached = 0;
4249         lo->ldo_stripenr = 0;
4250         lo->ldo_pattern = 0;
4251 }
4252
4253 /**
4254  * Implementation of lu_object_operations::loo_object_start.
4255  *
4256  * \see lu_object_operations::loo_object_start() in the API description
4257  * for details.
4258  */
4259 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4260 {
4261         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
4262                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4263         } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
4264                    fid_is_local_file(lu_object_fid(o))) {
4265                 /* Note: some local file (like last rcvd) is created
4266                  * through bottom layer (OSD), so the object initialization
4267                  * comes to lod, it does not set loh_attr yet, so
4268                  * set do_body_ops for local file anyway */
4269                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
4270         }
4271         return 0;
4272 }
4273
4274 /**
4275  * Implementation of lu_object_operations::loo_object_free.
4276  *
4277  * \see lu_object_operations::loo_object_free() in the API description
4278  * for details.
4279  */
4280 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4281 {
4282         struct lod_object *mo = lu2lod_obj(o);
4283
4284         /*
4285          * release all underlying object pinned
4286          */
4287
4288         lod_object_free_striping(env, mo);
4289
4290         lod_object_set_pool(mo, NULL);
4291
4292         lu_object_fini(o);
4293         OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4294 }
4295
4296 /**
4297  * Implementation of lu_object_operations::loo_object_release.
4298  *
4299  * \see lu_object_operations::loo_object_release() in the API description
4300  * for details.
4301  */
4302 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4303 {
4304         /* XXX: shouldn't we release everything here in case if object
4305          * creation failed before? */
4306 }
4307
4308 /**
4309  * Implementation of lu_object_operations::loo_object_print.
4310  *
4311  * \see lu_object_operations::loo_object_print() in the API description
4312  * for details.
4313  */
4314 static int lod_object_print(const struct lu_env *env, void *cookie,
4315                             lu_printer_t p, const struct lu_object *l)
4316 {
4317         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4318
4319         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4320 }
4321
4322 struct lu_object_operations lod_lu_obj_ops = {
4323         .loo_object_init        = lod_object_init,
4324         .loo_object_start       = lod_object_start,
4325         .loo_object_free        = lod_object_free,
4326         .loo_object_release     = lod_object_release,
4327         .loo_object_print       = lod_object_print,
4328 };