Whamcloud - gitweb
LU-8922 lod: check master stripes properly
[fs/lustre-release.git] / lustre / lod / lod_object.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * lustre/lod/lod_object.c
30  *
31  * This file contains implementations of methods for the OSD API
32  * for the Logical Object Device (LOD) layer, which provides a virtual
33  * local OSD object interface to the MDD layer, and abstracts the
34  * addressing of local (OSD) and remote (OSP) objects. The API is
35  * described in the file lustre/include/dt_object.h and in
36  * Documentation/osd-api.txt.
37  *
38  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_MDS
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46
47 #include <lustre_fid.h>
48 #include <lustre_linkea.h>
49 #include <lustre_lmv.h>
50 #include <lustre_param.h>
51 #include <lustre_swab.h>
52 #include <lustre_ver.h>
53 #include <lprocfs_status.h>
54 #include <md_object.h>
55
56 #include "lod_internal.h"
57
58 static const char dot[] = ".";
59 static const char dotdot[] = "..";
60
61 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         rc = next->do_ops->do_index_try(env, next, &dt_directory_features);
575         if (rc != 0)
576                 RETURN(rc);
577
578         LASSERT(next->do_index_ops != NULL);
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  * Implementation of dt_object_operations::do_declare_attr_set.
1069  *
1070  * If the object is striped, then apply the changes to all the stripes.
1071  *
1072  * \see dt_object_operations::do_declare_attr_set() in the API description
1073  * for details.
1074  */
1075 static int lod_declare_attr_set(const struct lu_env *env,
1076                                 struct dt_object *dt,
1077                                 const struct lu_attr *attr,
1078                                 struct thandle *th)
1079 {
1080         struct dt_object  *next = dt_object_child(dt);
1081         struct lod_object *lo = lod_dt_obj(dt);
1082         int                rc, i;
1083         ENTRY;
1084
1085         /*
1086          * declare setattr on the local object
1087          */
1088         rc = lod_sub_object_declare_attr_set(env, next, attr, th);
1089         if (rc)
1090                 RETURN(rc);
1091
1092         /* osp_declare_attr_set() ignores all attributes other than
1093          * UID, GID, and size, and osp_attr_set() ignores all but UID
1094          * and GID.  Declaration of size attr setting happens through
1095          * lod_declare_init_size(), and not through this function.
1096          * Therefore we need not load striping unless ownership is
1097          * changing.  This should save memory and (we hope) speed up
1098          * rename(). */
1099         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1100                 if (!(attr->la_valid & (LA_UID | LA_GID)))
1101                         RETURN(rc);
1102
1103                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1104                         RETURN(0);
1105         } else {
1106                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1107                                         LA_ATIME | LA_MTIME | LA_CTIME |
1108                                         LA_FLAGS)))
1109                         RETURN(rc);
1110         }
1111         /*
1112          * load striping information, notice we don't do this when object
1113          * is being initialized as we don't need this information till
1114          * few specific cases like destroy, chown
1115          */
1116         rc = lod_load_striping(env, lo);
1117         if (rc)
1118                 RETURN(rc);
1119
1120         if (lo->ldo_stripenr == 0)
1121                 RETURN(0);
1122
1123         /*
1124          * if object is striped declare changes on the stripes
1125          */
1126         LASSERT(lo->ldo_stripe);
1127         for (i = 0; i < lo->ldo_stripenr; i++) {
1128                 if (lo->ldo_stripe[i] == NULL)
1129                         continue;
1130                 rc = lod_sub_object_declare_attr_set(env,
1131                                         lo->ldo_stripe[i], attr,
1132                                         th);
1133                 if (rc != 0)
1134                         RETURN(rc);
1135         }
1136
1137         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1138             dt_object_exists(next) != 0 &&
1139             dt_object_remote(next) == 0)
1140                 lod_sub_object_declare_xattr_del(env, next,
1141                                                 XATTR_NAME_LOV, th);
1142
1143         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) &&
1144             dt_object_exists(next) &&
1145             dt_object_remote(next) == 0 && S_ISREG(attr->la_mode)) {
1146                 struct lod_thread_info *info = lod_env_info(env);
1147                 struct lu_buf *buf = &info->lti_buf;
1148
1149                 buf->lb_buf = info->lti_ea_store;
1150                 buf->lb_len = info->lti_ea_store_size;
1151                 lod_sub_object_declare_xattr_set(env, next, buf,
1152                                                  XATTR_NAME_LOV,
1153                                                  LU_XATTR_REPLACE, th);
1154         }
1155
1156         RETURN(rc);
1157 }
1158
1159 /**
1160  * Implementation of dt_object_operations::do_attr_set.
1161  *
1162  * If the object is striped, then apply the changes to all or subset of
1163  * the stripes depending on the object type and specific attributes.
1164  *
1165  * \see dt_object_operations::do_attr_set() in the API description for details.
1166  */
1167 static int lod_attr_set(const struct lu_env *env,
1168                         struct dt_object *dt,
1169                         const struct lu_attr *attr,
1170                         struct thandle *th)
1171 {
1172         struct dt_object        *next = dt_object_child(dt);
1173         struct lod_object       *lo = lod_dt_obj(dt);
1174         int                     rc, i;
1175         ENTRY;
1176
1177         /*
1178          * apply changes to the local object
1179          */
1180         rc = lod_sub_object_attr_set(env, next, attr, th);
1181         if (rc)
1182                 RETURN(rc);
1183
1184         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1185                 if (!(attr->la_valid & (LA_UID | LA_GID)))
1186                         RETURN(rc);
1187
1188                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1189                         RETURN(0);
1190         } else {
1191                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE |
1192                                         LA_ATIME | LA_MTIME | LA_CTIME |
1193                                         LA_FLAGS)))
1194                         RETURN(rc);
1195         }
1196
1197         if (lo->ldo_stripenr == 0)
1198                 RETURN(0);
1199
1200         /*
1201          * if object is striped, apply changes to all the stripes
1202          */
1203         LASSERT(lo->ldo_stripe);
1204         for (i = 0; i < lo->ldo_stripenr; i++) {
1205                 if (unlikely(lo->ldo_stripe[i] == NULL))
1206                         continue;
1207
1208                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
1209                     (dt_object_exists(lo->ldo_stripe[i]) == 0))
1210                         continue;
1211
1212                 rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i], attr, th);
1213                 if (rc != 0)
1214                         break;
1215         }
1216
1217         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE) &&
1218             dt_object_exists(next) != 0 &&
1219             dt_object_remote(next) == 0)
1220                 rc = lod_sub_object_xattr_del(env, next, 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                 struct ost_id *oi = &info->lti_ostid;
1228                 struct lu_fid *fid = &info->lti_fid;
1229                 struct lov_mds_md_v1 *lmm;
1230                 struct lov_ost_data_v1 *objs;
1231                 __u32 magic;
1232                 int rc1;
1233
1234                 rc1 = lod_get_lov_ea(env, lo);
1235                 if (rc1  <= 0)
1236                         RETURN(rc);
1237
1238                 buf->lb_buf = info->lti_ea_store;
1239                 buf->lb_len = info->lti_ea_store_size;
1240                 lmm = info->lti_ea_store;
1241                 magic = le32_to_cpu(lmm->lmm_magic);
1242                 if (magic == LOV_MAGIC_V1)
1243                         objs = &(lmm->lmm_objects[0]);
1244                 else
1245                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1246                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1247                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1248                 fid->f_oid--;
1249                 fid_to_ostid(fid, oi);
1250                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1251
1252                 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1253                                               LU_XATTR_REPLACE, th);
1254         }
1255
1256         RETURN(rc);
1257 }
1258
1259 /**
1260  * Implementation of dt_object_operations::do_xattr_get.
1261  *
1262  * If LOV EA is requested from the root object and it's not
1263  * found, then return default striping for the filesystem.
1264  *
1265  * \see dt_object_operations::do_xattr_get() in the API description for details.
1266  */
1267 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1268                          struct lu_buf *buf, const char *name)
1269 {
1270         struct lod_thread_info  *info = lod_env_info(env);
1271         struct lod_device       *dev = lu2lod_dev(dt->do_lu.lo_dev);
1272         int                      rc, is_root;
1273         ENTRY;
1274
1275         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1276         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1277                 struct lmv_mds_md_v1    *lmv1;
1278                 int                      rc1 = 0;
1279
1280                 if (rc > (typeof(rc))sizeof(*lmv1))
1281                         RETURN(rc);
1282
1283                 if (rc < (typeof(rc))sizeof(*lmv1))
1284                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1285
1286                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1287                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1288
1289                         info->lti_buf.lb_buf = info->lti_key;
1290                         info->lti_buf.lb_len = sizeof(*lmv1);
1291                         rc = dt_xattr_get(env, dt_object_child(dt),
1292                                           &info->lti_buf, name);
1293                         if (unlikely(rc != sizeof(*lmv1)))
1294                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1295
1296                         lmv1 = info->lti_buf.lb_buf;
1297                         /* The on-disk LMV EA only contains header, but the
1298                          * returned LMV EA size should contain the space for
1299                          * the FIDs of all shards of the striped directory. */
1300                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1301                                 rc = lmv_mds_md_size(
1302                                         le32_to_cpu(lmv1->lmv_stripe_count),
1303                                         LMV_MAGIC_V1);
1304                 } else {
1305                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1306                                                   buf, false);
1307                 }
1308
1309                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1310         }
1311
1312         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1313                 RETURN(rc);
1314
1315         /*
1316          * lod returns default striping on the real root of the device
1317          * this is like the root stores default striping for the whole
1318          * filesystem. historically we've been using a different approach
1319          * and store it in the config.
1320          */
1321         dt_root_get(env, dev->lod_child, &info->lti_fid);
1322         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1323
1324         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1325                 struct lov_user_md *lum = buf->lb_buf;
1326                 struct lov_desc    *desc = &dev->lod_desc;
1327
1328                 if (buf->lb_buf == NULL) {
1329                         rc = sizeof(*lum);
1330                 } else if (buf->lb_len >= sizeof(*lum)) {
1331                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1332                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1333                         lmm_oi_set_id(&lum->lmm_oi, 0);
1334                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1335                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1336                         lum->lmm_stripe_size = cpu_to_le32(
1337                                                 desc->ld_default_stripe_size);
1338                         lum->lmm_stripe_count = cpu_to_le16(
1339                                                 desc->ld_default_stripe_count);
1340                         lum->lmm_stripe_offset = cpu_to_le16(
1341                                                 desc->ld_default_stripe_offset);
1342                         rc = sizeof(*lum);
1343                 } else {
1344                         rc = -ERANGE;
1345                 }
1346         }
1347
1348         RETURN(rc);
1349 }
1350
1351 /**
1352  * Verify LVM EA.
1353  *
1354  * Checks that the magic of the stripe is sane.
1355  *
1356  * \param[in] lod       lod device
1357  * \param[in] lum       a buffer storing LMV EA to verify
1358  *
1359  * \retval              0 if the EA is sane
1360  * \retval              negative otherwise
1361  */
1362 static int lod_verify_md_striping(struct lod_device *lod,
1363                                   const struct lmv_user_md_v1 *lum)
1364 {
1365         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1366                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1367                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1368                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1369                        (int)le32_to_cpu(lum->lum_stripe_offset),
1370                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1371                 return -EINVAL;
1372         }
1373
1374         return 0;
1375 }
1376
1377 /**
1378  * Initialize LMV EA for a slave.
1379  *
1380  * Initialize slave's LMV EA from the master's LMV EA.
1381  *
1382  * \param[in] master_lmv        a buffer containing master's EA
1383  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1384  *
1385  */
1386 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1387                                   const struct lmv_mds_md_v1 *master_lmv)
1388 {
1389         *slave_lmv = *master_lmv;
1390         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1391 }
1392
1393 /**
1394  * Generate LMV EA.
1395  *
1396  * Generate LMV EA from the object passed as \a dt. The object must have
1397  * the stripes created and initialized.
1398  *
1399  * \param[in] env       execution environment
1400  * \param[in] dt        object
1401  * \param[out] lmv_buf  buffer storing generated LMV EA
1402  *
1403  * \retval              0 on success
1404  * \retval              negative if failed
1405  */
1406 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1407                            struct lu_buf *lmv_buf)
1408 {
1409         struct lod_thread_info  *info = lod_env_info(env);
1410         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1411         struct lod_object       *lo = lod_dt_obj(dt);
1412         struct lmv_mds_md_v1    *lmm1;
1413         int                     stripe_count;
1414         int                     type = LU_SEQ_RANGE_ANY;
1415         int                     rc;
1416         __u32                   mdtidx;
1417         ENTRY;
1418
1419         LASSERT(lo->ldo_dir_striped != 0);
1420         LASSERT(lo->ldo_stripenr > 0);
1421         stripe_count = lo->ldo_stripenr;
1422         /* Only store the LMV EA heahder on the disk. */
1423         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1424                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1425                 if (rc != 0)
1426                         RETURN(rc);
1427         } else {
1428                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1429         }
1430
1431         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1432         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1433         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1434         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1435         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1436                             &mdtidx, &type);
1437         if (rc != 0)
1438                 RETURN(rc);
1439
1440         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1441         lmv_buf->lb_buf = info->lti_ea_store;
1442         lmv_buf->lb_len = sizeof(*lmm1);
1443
1444         RETURN(rc);
1445 }
1446
1447 /**
1448  * Create in-core represenation for a striped directory.
1449  *
1450  * Parse the buffer containing LMV EA and instantiate LU objects
1451  * representing the stripe objects. The pointers to the objects are
1452  * stored in ldo_stripe field of \a lo. This function is used when
1453  * we need to access an already created object (i.e. load from a disk).
1454  *
1455  * \param[in] env       execution environment
1456  * \param[in] lo        lod object
1457  * \param[in] buf       buffer containing LMV EA
1458  *
1459  * \retval              0 on success
1460  * \retval              negative if failed
1461  */
1462 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1463                            const struct lu_buf *buf)
1464 {
1465         struct lod_thread_info  *info = lod_env_info(env);
1466         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1467         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1468         struct dt_object        **stripe;
1469         union lmv_mds_md        *lmm = buf->lb_buf;
1470         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1471         struct lu_fid           *fid = &info->lti_fid;
1472         unsigned int            i;
1473         int                     rc = 0;
1474         ENTRY;
1475
1476         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1477                 RETURN(0);
1478
1479         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1480                 lo->ldo_dir_slave_stripe = 1;
1481                 RETURN(0);
1482         }
1483
1484         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1485                 RETURN(-EINVAL);
1486
1487         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1488                 RETURN(0);
1489
1490         LASSERT(lo->ldo_stripe == NULL);
1491         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1492                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1493         if (stripe == NULL)
1494                 RETURN(-ENOMEM);
1495
1496         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1497                 struct dt_device        *tgt_dt;
1498                 struct dt_object        *dto;
1499                 int                     type = LU_SEQ_RANGE_ANY;
1500                 __u32                   idx;
1501
1502                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1503                 if (!fid_is_sane(fid))
1504                         GOTO(out, rc = -ESTALE);
1505
1506                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1507                 if (rc != 0)
1508                         GOTO(out, rc);
1509
1510                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1511                         tgt_dt = lod->lod_child;
1512                 } else {
1513                         struct lod_tgt_desc     *tgt;
1514
1515                         tgt = LTD_TGT(ltd, idx);
1516                         if (tgt == NULL)
1517                                 GOTO(out, rc = -ESTALE);
1518                         tgt_dt = tgt->ltd_tgt;
1519                 }
1520
1521                 dto = dt_locate_at(env, tgt_dt, fid,
1522                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1523                                   NULL);
1524                 if (IS_ERR(dto))
1525                         GOTO(out, rc = PTR_ERR(dto));
1526
1527                 stripe[i] = dto;
1528         }
1529 out:
1530         lo->ldo_stripe = stripe;
1531         lo->ldo_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1532         lo->ldo_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1533         if (rc != 0)
1534                 lod_object_free_striping(env, lo);
1535
1536         RETURN(rc);
1537 }
1538
1539 /**
1540  * Declare create a striped directory.
1541  *
1542  * Declare creating a striped directory with a given stripe pattern on the
1543  * specified MDTs. A striped directory is represented as a regular directory
1544  * - an index listing all the stripes. The stripes point back to the master
1545  * object with ".." and LinkEA. The master object gets LMV EA which
1546  * identifies it as a striped directory. The function allocates FIDs
1547  * for all stripes.
1548  *
1549  * \param[in] env       execution environment
1550  * \param[in] dt        object
1551  * \param[in] attr      attributes to initialize the objects with
1552  * \param[in] dof       type of objects to be created
1553  * \param[in] th        transaction handle
1554  *
1555  * \retval              0 on success
1556  * \retval              negative if failed
1557  */
1558 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1559                                           struct dt_object *dt,
1560                                           struct lu_attr *attr,
1561                                           struct dt_object_format *dof,
1562                                           struct thandle *th)
1563 {
1564         struct lod_thread_info  *info = lod_env_info(env);
1565         struct lu_buf           lmv_buf;
1566         struct lu_buf           slave_lmv_buf;
1567         struct lmv_mds_md_v1    *lmm;
1568         struct lmv_mds_md_v1    *slave_lmm = NULL;
1569         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1570         struct lod_object       *lo = lod_dt_obj(dt);
1571         int                     rc;
1572         __u32                   i;
1573         ENTRY;
1574
1575         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1576         if (rc != 0)
1577                 GOTO(out, rc);
1578         lmm = lmv_buf.lb_buf;
1579
1580         OBD_ALLOC_PTR(slave_lmm);
1581         if (slave_lmm == NULL)
1582                 GOTO(out, rc = -ENOMEM);
1583
1584         lod_prep_slave_lmv_md(slave_lmm, lmm);
1585         slave_lmv_buf.lb_buf = slave_lmm;
1586         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1587
1588         if (!dt_try_as_dir(env, dt_object_child(dt)))
1589                 GOTO(out, rc = -EINVAL);
1590
1591         rec->rec_type = S_IFDIR;
1592         for (i = 0; i < lo->ldo_stripenr; i++) {
1593                 struct dt_object        *dto = lo->ldo_stripe[i];
1594                 char                    *stripe_name = info->lti_key;
1595                 struct lu_name          *sname;
1596                 struct linkea_data       ldata          = { NULL };
1597                 struct lu_buf           linkea_buf;
1598
1599                 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1600                                                    dof, th);
1601                 if (rc != 0)
1602                         GOTO(out, rc);
1603
1604                 if (!dt_try_as_dir(env, dto))
1605                         GOTO(out, rc = -EINVAL);
1606
1607                 rc = lod_sub_object_declare_ref_add(env, dto, th);
1608                 if (rc != 0)
1609                         GOTO(out, rc);
1610
1611                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1612                 rc = lod_sub_object_declare_insert(env, dto,
1613                                         (const struct dt_rec *)rec,
1614                                         (const struct dt_key *)dot, th);
1615                 if (rc != 0)
1616                         GOTO(out, rc);
1617
1618                 /* master stripe FID will be put to .. */
1619                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1620                 rc = lod_sub_object_declare_insert(env, dto,
1621                                         (const struct dt_rec *)rec,
1622                                         (const struct dt_key *)dotdot,
1623                                         th);
1624                 if (rc != 0)
1625                         GOTO(out, rc);
1626
1627                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1628                     cfs_fail_val != i) {
1629                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1630                             cfs_fail_val == i)
1631                                 slave_lmm->lmv_master_mdt_index =
1632                                                         cpu_to_le32(i + 1);
1633                         else
1634                                 slave_lmm->lmv_master_mdt_index =
1635                                                         cpu_to_le32(i);
1636                         rc = lod_sub_object_declare_xattr_set(env, dto,
1637                                         &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1638                         if (rc != 0)
1639                                 GOTO(out, rc);
1640                 }
1641
1642                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1643                     cfs_fail_val == i)
1644                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1645                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1646                 else
1647                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1648                                 PFID(lu_object_fid(&dto->do_lu)), i);
1649
1650                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1651                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1652                                       sname, lu_object_fid(&dt->do_lu));
1653                 if (rc != 0)
1654                         GOTO(out, rc);
1655
1656                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1657                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1658                 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1659                                           XATTR_NAME_LINK, 0, th);
1660                 if (rc != 0)
1661                         GOTO(out, rc);
1662
1663                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1664                 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1665                                        (const struct dt_rec *)rec,
1666                                        (const struct dt_key *)stripe_name,
1667                                        th);
1668                 if (rc != 0)
1669                         GOTO(out, rc);
1670
1671                 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1672                                                     th);
1673                 if (rc != 0)
1674                         GOTO(out, rc);
1675         }
1676
1677         rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1678                                 &lmv_buf, XATTR_NAME_LMV, 0, th);
1679         if (rc != 0)
1680                 GOTO(out, rc);
1681 out:
1682         if (slave_lmm != NULL)
1683                 OBD_FREE_PTR(slave_lmm);
1684
1685         RETURN(rc);
1686 }
1687
1688 static int lod_prep_md_striped_create(const struct lu_env *env,
1689                                       struct dt_object *dt,
1690                                       struct lu_attr *attr,
1691                                       const struct lmv_user_md_v1 *lum,
1692                                       struct dt_object_format *dof,
1693                                       struct thandle *th)
1694 {
1695         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1696         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1697         struct lod_object       *lo = lod_dt_obj(dt);
1698         struct dt_object        **stripe;
1699         __u32                   stripe_count;
1700         int                     *idx_array;
1701         __u32                   master_index;
1702         int                     rc = 0;
1703         __u32                   i;
1704         __u32                   j;
1705         ENTRY;
1706
1707         /* The lum has been verifed in lod_verify_md_striping */
1708         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1709         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1710
1711         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1712
1713         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1714         if (idx_array == NULL)
1715                 RETURN(-ENOMEM);
1716
1717         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1718         if (stripe == NULL)
1719                 GOTO(out_free, rc = -ENOMEM);
1720
1721         /* Start index must be the master MDT */
1722         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1723         idx_array[0] = master_index;
1724         for (i = 0; i < stripe_count; i++) {
1725                 struct lod_tgt_desc     *tgt = NULL;
1726                 struct dt_object        *dto;
1727                 struct lu_fid           fid = { 0 };
1728                 int                     idx;
1729                 struct lu_object_conf   conf = { 0 };
1730                 struct dt_device        *tgt_dt = NULL;
1731
1732                 /* Try to find next avaible target */
1733                 idx = idx_array[i];
1734                 for (j = 0; j < lod->lod_remote_mdt_count;
1735                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1736                         bool already_allocated = false;
1737                         __u32 k;
1738
1739                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1740                                idx, lod->lod_remote_mdt_count + 1, i);
1741
1742                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1743                                 /* check whether the idx already exists
1744                                  * in current allocated array */
1745                                 for (k = 0; k < i; k++) {
1746                                         if (idx_array[k] == idx) {
1747                                                 already_allocated = true;
1748                                                 break;
1749                                         }
1750                                 }
1751
1752                                 if (already_allocated)
1753                                         continue;
1754                         }
1755
1756                         /* Sigh, this index is not in the bitmap, let's check
1757                          * next available target */
1758                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1759                             idx != master_index)
1760                                 continue;
1761
1762                         if (idx == master_index) {
1763                                 /* Allocate the FID locally */
1764                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1765                                                    &fid, NULL);
1766                                 if (rc < 0)
1767                                         GOTO(out_put, rc);
1768                                 tgt_dt = lod->lod_child;
1769                                 break;
1770                         }
1771
1772                         /* check the status of the OSP */
1773                         tgt = LTD_TGT(ltd, idx);
1774                         if (tgt == NULL)
1775                                 continue;
1776
1777                         tgt_dt = tgt->ltd_tgt;
1778                         rc = dt_statfs(env, tgt_dt, NULL);
1779                         if (rc) {
1780                                 /* this OSP doesn't feel well */
1781                                 rc = 0;
1782                                 continue;
1783                         }
1784
1785                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1786                         if (rc < 0) {
1787                                 rc = 0;
1788                                 continue;
1789                         }
1790
1791                         break;
1792                 }
1793
1794                 /* Can not allocate more stripes */
1795                 if (j == lod->lod_remote_mdt_count) {
1796                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1797                                lod2obd(lod)->obd_name, stripe_count, i);
1798                         break;
1799                 }
1800
1801                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1802                        idx, i, PFID(&fid));
1803                 idx_array[i] = idx;
1804                 /* Set the start index for next stripe allocation */
1805                 if (i < stripe_count - 1)
1806                         idx_array[i + 1] = (idx + 1) %
1807                                            (lod->lod_remote_mdt_count + 1);
1808                 /* tgt_dt and fid must be ready after search avaible OSP
1809                  * in the above loop */
1810                 LASSERT(tgt_dt != NULL);
1811                 LASSERT(fid_is_sane(&fid));
1812                 conf.loc_flags = LOC_F_NEW;
1813                 dto = dt_locate_at(env, tgt_dt, &fid,
1814                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1815                                    &conf);
1816                 if (IS_ERR(dto))
1817                         GOTO(out_put, rc = PTR_ERR(dto));
1818                 stripe[i] = dto;
1819         }
1820
1821         lo->ldo_dir_striped = 1;
1822         lo->ldo_stripe = stripe;
1823         lo->ldo_stripenr = i;
1824         lo->ldo_stripes_allocated = stripe_count;
1825
1826         if (lo->ldo_stripenr == 0)
1827                 GOTO(out_put, rc = -ENOSPC);
1828
1829         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1830         if (rc != 0)
1831                 GOTO(out_put, rc);
1832
1833 out_put:
1834         if (rc < 0) {
1835                 for (i = 0; i < stripe_count; i++)
1836                         if (stripe[i] != NULL)
1837                                 lu_object_put(env, &stripe[i]->do_lu);
1838                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1839                 lo->ldo_stripenr = 0;
1840                 lo->ldo_stripes_allocated = 0;
1841                 lo->ldo_stripe = NULL;
1842         }
1843
1844 out_free:
1845         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1846
1847         RETURN(rc);
1848 }
1849
1850 /**
1851  * Declare create striped md object.
1852  *
1853  * The function declares intention to create a striped directory. This is a
1854  * wrapper for lod_prep_md_striped_create(). The only additional functionality
1855  * is to verify pattern \a lum_buf is good. Check that function for the details.
1856  *
1857  * \param[in] env       execution environment
1858  * \param[in] dt        object
1859  * \param[in] attr      attributes to initialize the objects with
1860  * \param[in] lum_buf   a pattern specifying the number of stripes and
1861  *                      MDT to start from
1862  * \param[in] dof       type of objects to be created
1863  * \param[in] th        transaction handle
1864  *
1865  * \retval              0 on success
1866  * \retval              negative if failed
1867  *
1868  */
1869 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1870                                      struct dt_object *dt,
1871                                      struct lu_attr *attr,
1872                                      const struct lu_buf *lum_buf,
1873                                      struct dt_object_format *dof,
1874                                      struct thandle *th)
1875 {
1876         struct lod_object       *lo = lod_dt_obj(dt);
1877         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1878         struct lmv_user_md_v1   *lum;
1879         int                     rc;
1880         ENTRY;
1881
1882         lum = lum_buf->lb_buf;
1883         LASSERT(lum != NULL);
1884
1885         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1886                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1887                (int)le32_to_cpu(lum->lum_stripe_offset));
1888
1889         if (le32_to_cpu(lum->lum_stripe_count) == 0)
1890                 GOTO(out, rc = 0);
1891
1892         rc = lod_verify_md_striping(lod, lum);
1893         if (rc != 0)
1894                 GOTO(out, rc);
1895
1896         /* prepare dir striped objects */
1897         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1898         if (rc != 0) {
1899                 /* failed to create striping, let's reset
1900                  * config so that others don't get confused */
1901                 lod_object_free_striping(env, lo);
1902                 GOTO(out, rc);
1903         }
1904 out:
1905         RETURN(rc);
1906 }
1907
1908 /**
1909  * Implementation of dt_object_operations::do_declare_xattr_set.
1910  *
1911  * Used with regular (non-striped) objects. Basically it
1912  * initializes the striping information and applies the
1913  * change to all the stripes.
1914  *
1915  * \see dt_object_operations::do_declare_xattr_set() in the API description
1916  * for details.
1917  */
1918 static int lod_dir_declare_xattr_set(const struct lu_env *env,
1919                                      struct dt_object *dt,
1920                                      const struct lu_buf *buf,
1921                                      const char *name, int fl,
1922                                      struct thandle *th)
1923 {
1924         struct dt_object        *next = dt_object_child(dt);
1925         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
1926         struct lod_object       *lo = lod_dt_obj(dt);
1927         int                     i;
1928         int                     rc;
1929         ENTRY;
1930
1931         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1932                 struct lmv_user_md_v1 *lum;
1933
1934                 LASSERT(buf != NULL && buf->lb_buf != NULL);
1935                 lum = buf->lb_buf;
1936                 rc = lod_verify_md_striping(d, lum);
1937                 if (rc != 0)
1938                         RETURN(rc);
1939         }
1940
1941         rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
1942         if (rc != 0)
1943                 RETURN(rc);
1944
1945         /* Note: Do not set LinkEA on sub-stripes, otherwise
1946          * it will confuse the fid2path process(see mdt_path_current()).
1947          * The linkEA between master and sub-stripes is set in
1948          * lod_xattr_set_lmv(). */
1949         if (strcmp(name, XATTR_NAME_LINK) == 0)
1950                 RETURN(0);
1951
1952         /* set xattr to each stripes, if needed */
1953         rc = lod_load_striping(env, lo);
1954         if (rc != 0)
1955                 RETURN(rc);
1956
1957         if (lo->ldo_stripenr == 0)
1958                 RETURN(0);
1959
1960         for (i = 0; i < lo->ldo_stripenr; i++) {
1961                 LASSERT(lo->ldo_stripe[i]);
1962
1963                 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
1964                                                 buf, name, fl, th);
1965                 if (rc != 0)
1966                         break;
1967         }
1968
1969         RETURN(rc);
1970 }
1971
1972 /**
1973  * Reset parent FID on OST object
1974  *
1975  * Replace parent FID with @dt object FID, which is only called during migration
1976  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
1977  * the FID is changed.
1978  *
1979  * \param[in] env execution environment
1980  * \param[in] dt dt_object whose stripes's parent FID will be reset
1981  * \parem[in] th thandle
1982  * \param[in] declare if it is declare
1983  *
1984  * \retval      0 if reset succeeds
1985  * \retval      negative errno if reset fais
1986  */
1987 static int lod_object_replace_parent_fid(const struct lu_env *env,
1988                                          struct dt_object *dt,
1989                                          struct thandle *th, bool declare)
1990 {
1991         struct lod_object *lo = lod_dt_obj(dt);
1992         struct lod_thread_info  *info = lod_env_info(env);
1993         struct lu_buf *buf = &info->lti_buf;
1994         struct filter_fid *ff;
1995         int i, rc;
1996         ENTRY;
1997
1998         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
1999
2000         /* set xattr to each stripes, if needed */
2001         rc = lod_load_striping(env, lo);
2002         if (rc != 0)
2003                 RETURN(rc);
2004
2005         if (lo->ldo_stripenr == 0)
2006                 RETURN(0);
2007
2008         if (info->lti_ea_store_size < sizeof(*ff)) {
2009                 rc = lod_ea_store_resize(info, sizeof(*ff));
2010                 if (rc != 0)
2011                         RETURN(rc);
2012         }
2013
2014         buf->lb_buf = info->lti_ea_store;
2015         buf->lb_len = info->lti_ea_store_size;
2016
2017         for (i = 0; i < lo->ldo_stripenr; i++) {
2018                 if (lo->ldo_stripe[i] == NULL)
2019                         continue;
2020
2021                 rc = dt_xattr_get(env, lo->ldo_stripe[i], buf,
2022                                   XATTR_NAME_FID);
2023                 if (rc < 0) {
2024                         rc = 0;
2025                         continue;
2026                 }
2027
2028                 ff = buf->lb_buf;
2029                 fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
2030                 ff->ff_parent.f_seq = lu_object_fid(&dt->do_lu)->f_seq;
2031                 ff->ff_parent.f_oid = lu_object_fid(&dt->do_lu)->f_oid;
2032                 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2033
2034                 if (declare) {
2035                         rc = lod_sub_object_declare_xattr_set(env,
2036                                                 lo->ldo_stripe[i], buf,
2037                                                 XATTR_NAME_FID,
2038                                                 LU_XATTR_REPLACE, th);
2039                 } else {
2040                         rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
2041                                                       buf, XATTR_NAME_FID,
2042                                                       LU_XATTR_REPLACE, th);
2043                 }
2044                 if (rc < 0)
2045                         break;
2046         }
2047
2048         RETURN(rc);
2049 }
2050
2051 /**
2052  * Implementation of dt_object_operations::do_declare_xattr_set.
2053  *
2054  * \see dt_object_operations::do_declare_xattr_set() in the API description
2055  * for details.
2056  *
2057  * the extension to the API:
2058  *   - declaring LOVEA requests striping creation
2059  *   - LU_XATTR_REPLACE means layout swap
2060  */
2061 static int lod_declare_xattr_set(const struct lu_env *env,
2062                                  struct dt_object *dt,
2063                                  const struct lu_buf *buf,
2064                                  const char *name, int fl,
2065                                  struct thandle *th)
2066 {
2067         struct dt_object *next = dt_object_child(dt);
2068         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2069         __u32             mode;
2070         int               rc;
2071         ENTRY;
2072
2073         /*
2074          * allow to declare predefined striping on a new (!mode) object
2075          * which is supposed to be replay of regular file creation
2076          * (when LOV setting is declared)
2077          * LU_XATTR_REPLACE is set to indicate a layout swap
2078          */
2079         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2080         if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2081              !(fl & LU_XATTR_REPLACE)) {
2082                 /*
2083                  * this is a request to manipulate object's striping
2084                  */
2085                 if (dt_object_exists(dt)) {
2086                         rc = dt_attr_get(env, next, attr);
2087                         if (rc)
2088                                 RETURN(rc);
2089                 } else {
2090                         memset(attr, 0, sizeof(*attr));
2091                         attr->la_valid = LA_TYPE | LA_MODE;
2092                         attr->la_mode = S_IFREG;
2093                 }
2094                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2095         } else if (S_ISDIR(mode)) {
2096                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2097         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2098                 rc = lod_object_replace_parent_fid(env, dt, th, true);
2099         } else {
2100                 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2101                                                       fl, th);
2102         }
2103
2104         RETURN(rc);
2105 }
2106
2107 /**
2108  * Apply xattr changes to the object.
2109  *
2110  * Applies xattr changes to the object and the stripes if the latter exist.
2111  *
2112  * \param[in] env       execution environment
2113  * \param[in] dt        object
2114  * \param[in] buf       buffer pointing to the new value of xattr
2115  * \param[in] name      name of xattr
2116  * \param[in] fl        flags
2117  * \param[in] th        transaction handle
2118  *
2119  * \retval              0 on success
2120  * \retval              negative if failed
2121  */
2122 static int lod_xattr_set_internal(const struct lu_env *env,
2123                                   struct dt_object *dt,
2124                                   const struct lu_buf *buf,
2125                                   const char *name, int fl,
2126                                   struct thandle *th)
2127 {
2128         struct dt_object        *next = dt_object_child(dt);
2129         struct lod_object       *lo = lod_dt_obj(dt);
2130         int                     rc;
2131         int                     i;
2132         ENTRY;
2133
2134         rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2135         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2136                 RETURN(rc);
2137
2138         /* Note: Do not set LinkEA on sub-stripes, otherwise
2139          * it will confuse the fid2path process(see mdt_path_current()).
2140          * The linkEA between master and sub-stripes is set in
2141          * lod_xattr_set_lmv(). */
2142         if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2143                 RETURN(0);
2144
2145         for (i = 0; i < lo->ldo_stripenr; i++) {
2146                 LASSERT(lo->ldo_stripe[i]);
2147
2148                 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2149                                               fl, th);
2150                 if (rc != 0)
2151                         break;
2152         }
2153
2154         RETURN(rc);
2155 }
2156
2157 /**
2158  * Delete an extended attribute.
2159  *
2160  * Deletes specified xattr from the object and the stripes if the latter exist.
2161  *
2162  * \param[in] env       execution environment
2163  * \param[in] dt        object
2164  * \param[in] name      name of xattr
2165  * \param[in] th        transaction handle
2166  *
2167  * \retval              0 on success
2168  * \retval              negative if failed
2169  */
2170 static int lod_xattr_del_internal(const struct lu_env *env,
2171                                   struct dt_object *dt,
2172                                   const char *name, struct thandle *th)
2173 {
2174         struct dt_object        *next = dt_object_child(dt);
2175         struct lod_object       *lo = lod_dt_obj(dt);
2176         int                     rc;
2177         int                     i;
2178         ENTRY;
2179
2180         rc = lod_sub_object_xattr_del(env, next, name, th);
2181         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2182                 RETURN(rc);
2183
2184         if (lo->ldo_stripenr == 0)
2185                 RETURN(rc);
2186
2187         for (i = 0; i < lo->ldo_stripenr; i++) {
2188                 LASSERT(lo->ldo_stripe[i]);
2189
2190                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2191                                               th);
2192                 if (rc != 0)
2193                         break;
2194         }
2195
2196         RETURN(rc);
2197 }
2198
2199 /**
2200  * Set default striping on a directory.
2201  *
2202  * Sets specified striping on a directory object unless it matches the default
2203  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2204  * EA. This striping will be used when regular file is being created in this
2205  * directory.
2206  *
2207  * \param[in] env       execution environment
2208  * \param[in] dt        the striped object
2209  * \param[in] buf       buffer with the striping
2210  * \param[in] name      name of EA
2211  * \param[in] fl        xattr flag (see OSD API description)
2212  * \param[in] th        transaction handle
2213  *
2214  * \retval              0 on success
2215  * \retval              negative if failed
2216  */
2217 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2218                                     struct dt_object *dt,
2219                                     const struct lu_buf *buf,
2220                                     const char *name, int fl,
2221                                     struct thandle *th)
2222 {
2223         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2224         struct lov_user_md_v1   *lum;
2225         struct lov_user_md_v3   *v3 = NULL;
2226         const char              *pool_name = NULL;
2227         int                      rc;
2228         ENTRY;
2229
2230         LASSERT(buf != NULL && buf->lb_buf != NULL);
2231         lum = buf->lb_buf;
2232
2233         rc = lod_verify_striping(d, buf, false);
2234         if (rc)
2235                 RETURN(rc);
2236
2237         if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2238                 v3 = buf->lb_buf;
2239                 if (v3->lmm_pool_name[0] != '\0')
2240                         pool_name = v3->lmm_pool_name;
2241         }
2242
2243         /* if { size, offset, count } = { 0, -1, 0 } and no pool
2244          * (i.e. all default values specified) then delete default
2245          * striping from dir. */
2246         CDEBUG(D_OTHER,
2247                 "set default striping: sz %u # %u offset %d %s %s\n",
2248                 (unsigned)lum->lmm_stripe_size,
2249                 (unsigned)lum->lmm_stripe_count,
2250                 (int)lum->lmm_stripe_offset,
2251                 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2252
2253         if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2254                                 lum->lmm_stripe_offset, pool_name)) {
2255                 rc = lod_xattr_del_internal(env, dt, name, th);
2256                 if (rc == -ENODATA)
2257                         rc = 0;
2258         } else {
2259                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2260         }
2261
2262         RETURN(rc);
2263 }
2264
2265 /**
2266  * Set default striping on a directory object.
2267  *
2268  * Sets specified striping on a directory object unless it matches the default
2269  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2270  * EA. This striping will be used when a new directory is being created in the
2271  * directory.
2272  *
2273  * \param[in] env       execution environment
2274  * \param[in] dt        the striped object
2275  * \param[in] buf       buffer with the striping
2276  * \param[in] name      name of EA
2277  * \param[in] fl        xattr flag (see OSD API description)
2278  * \param[in] th        transaction handle
2279  *
2280  * \retval              0 on success
2281  * \retval              negative if failed
2282  */
2283 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2284                                             struct dt_object *dt,
2285                                             const struct lu_buf *buf,
2286                                             const char *name, int fl,
2287                                             struct thandle *th)
2288 {
2289         struct lmv_user_md_v1   *lum;
2290         int                      rc;
2291         ENTRY;
2292
2293         LASSERT(buf != NULL && buf->lb_buf != NULL);
2294         lum = buf->lb_buf;
2295
2296         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2297               le32_to_cpu(lum->lum_stripe_count),
2298               (int)le32_to_cpu(lum->lum_stripe_offset));
2299
2300         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2301                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2302                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2303                 rc = lod_xattr_del_internal(env, dt, name, th);
2304                 if (rc == -ENODATA)
2305                         rc = 0;
2306         } else {
2307                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2308                 if (rc != 0)
2309                         RETURN(rc);
2310         }
2311
2312         RETURN(rc);
2313 }
2314
2315 /**
2316  * Turn directory into a striped directory.
2317  *
2318  * During replay the client sends the striping created before MDT
2319  * failure, then the layer above LOD sends this defined striping
2320  * using ->do_xattr_set(), so LOD uses this method to replay creation
2321  * of the stripes. Notice the original information for the striping
2322  * (#stripes, FIDs, etc) was transferred in declare path.
2323  *
2324  * \param[in] env       execution environment
2325  * \param[in] dt        the striped object
2326  * \param[in] buf       not used currently
2327  * \param[in] name      not used currently
2328  * \param[in] fl        xattr flag (see OSD API description)
2329  * \param[in] th        transaction handle
2330  *
2331  * \retval              0 on success
2332  * \retval              negative if failed
2333  */
2334 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2335                              const struct lu_buf *buf, const char *name,
2336                              int fl, struct thandle *th)
2337 {
2338         struct lod_object       *lo = lod_dt_obj(dt);
2339         struct lod_thread_info  *info = lod_env_info(env);
2340         struct lu_attr          *attr = &info->lti_attr;
2341         struct dt_object_format *dof = &info->lti_format;
2342         struct lu_buf           lmv_buf;
2343         struct lu_buf           slave_lmv_buf;
2344         struct lmv_mds_md_v1    *lmm;
2345         struct lmv_mds_md_v1    *slave_lmm = NULL;
2346         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2347         int                     i;
2348         int                     rc;
2349         ENTRY;
2350
2351         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2352                 RETURN(-ENOTDIR);
2353
2354         /* The stripes are supposed to be allocated in declare phase,
2355          * if there are no stripes being allocated, it will skip */
2356         if (lo->ldo_stripenr == 0)
2357                 RETURN(0);
2358
2359         rc = dt_attr_get(env, dt_object_child(dt), attr);
2360         if (rc != 0)
2361                 RETURN(rc);
2362
2363         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2364                          LA_MODE | LA_UID | LA_GID | LA_TYPE;
2365         dof->dof_type = DFT_DIR;
2366
2367         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2368         if (rc != 0)
2369                 RETURN(rc);
2370         lmm = lmv_buf.lb_buf;
2371
2372         OBD_ALLOC_PTR(slave_lmm);
2373         if (slave_lmm == NULL)
2374                 RETURN(-ENOMEM);
2375
2376         lod_prep_slave_lmv_md(slave_lmm, lmm);
2377         slave_lmv_buf.lb_buf = slave_lmm;
2378         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2379
2380         rec->rec_type = S_IFDIR;
2381         for (i = 0; i < lo->ldo_stripenr; i++) {
2382                 struct dt_object *dto;
2383                 char             *stripe_name = info->lti_key;
2384                 struct lu_name          *sname;
2385                 struct linkea_data       ldata          = { NULL };
2386                 struct lu_buf            linkea_buf;
2387
2388                 dto = lo->ldo_stripe[i];
2389
2390                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2391                 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2392                                            th);
2393                 if (rc != 0) {
2394                         dt_write_unlock(env, dto);
2395                         GOTO(out, rc);
2396                 }
2397
2398                 rc = lod_sub_object_ref_add(env, dto, th);
2399                 dt_write_unlock(env, dto);
2400                 if (rc != 0)
2401                         GOTO(out, rc);
2402
2403                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2404                 rc = lod_sub_object_index_insert(env, dto,
2405                                 (const struct dt_rec *)rec,
2406                                 (const struct dt_key *)dot, th, 0);
2407                 if (rc != 0)
2408                         GOTO(out, rc);
2409
2410                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2411                 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2412                                (const struct dt_key *)dotdot, th, 0);
2413                 if (rc != 0)
2414                         GOTO(out, rc);
2415
2416                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2417                     cfs_fail_val != i) {
2418                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2419                             cfs_fail_val == i)
2420                                 slave_lmm->lmv_master_mdt_index =
2421                                                         cpu_to_le32(i + 1);
2422                         else
2423                                 slave_lmm->lmv_master_mdt_index =
2424                                                         cpu_to_le32(i);
2425
2426                         rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2427                                                       XATTR_NAME_LMV, fl, th);
2428                         if (rc != 0)
2429                                 GOTO(out, rc);
2430                 }
2431
2432                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2433                     cfs_fail_val == i)
2434                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2435                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2436                 else
2437                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2438                                  PFID(lu_object_fid(&dto->do_lu)), i);
2439
2440                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2441                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2442                                       sname, lu_object_fid(&dt->do_lu));
2443                 if (rc != 0)
2444                         GOTO(out, rc);
2445
2446                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2447                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2448                 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2449                                         XATTR_NAME_LINK, 0, th);
2450                 if (rc != 0)
2451                         GOTO(out, rc);
2452
2453                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2454                 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2455                                (const struct dt_rec *)rec,
2456                                (const struct dt_key *)stripe_name, th, 0);
2457                 if (rc != 0)
2458                         GOTO(out, rc);
2459
2460                 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2461                 if (rc != 0)
2462                         GOTO(out, rc);
2463         }
2464
2465         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2466                 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2467                                               &lmv_buf, XATTR_NAME_LMV, fl, th);
2468 out:
2469         if (slave_lmm != NULL)
2470                 OBD_FREE_PTR(slave_lmm);
2471
2472         RETURN(rc);
2473 }
2474
2475 /**
2476  * Helper function to declare/execute creation of a striped directory
2477  *
2478  * Called in declare/create object path, prepare striping for a directory
2479  * and prepare defaults data striping for the objects to be created in
2480  * that directory. Notice the function calls "declaration" or "execution"
2481  * methods depending on \a declare param. This is a consequence of the
2482  * current approach while we don't have natural distributed transactions:
2483  * we basically execute non-local updates in the declare phase. So, the
2484  * arguments for the both phases are the same and this is the reason for
2485  * this function to exist.
2486  *
2487  * \param[in] env       execution environment
2488  * \param[in] dt        object
2489  * \param[in] attr      attributes the stripes will be created with
2490  * \param[in] dof       format of stripes (see OSD API description)
2491  * \param[in] th        transaction handle
2492  * \param[in] declare   where to call "declare" or "execute" methods
2493  *
2494  * \retval              0 on success
2495  * \retval              negative if failed
2496  */
2497 static int lod_dir_striping_create_internal(const struct lu_env *env,
2498                                             struct dt_object *dt,
2499                                             struct lu_attr *attr,
2500                                             struct dt_object_format *dof,
2501                                             struct thandle *th,
2502                                             bool declare)
2503 {
2504         struct lod_thread_info *info = lod_env_info(env);
2505         struct lod_object *lo = lod_dt_obj(dt);
2506         const struct lod_default_striping *lds = lo->ldo_def_striping;
2507         const char *poolname = NULL;
2508         int rc;
2509         ENTRY;
2510
2511         LASSERT(ergo(lds != NULL,
2512                      lds->lds_def_striping_set ||
2513                      lds->lds_dir_def_striping_set));
2514
2515         if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2516                                  lo->ldo_dir_stripe_offset)) {
2517                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2518                 int stripe_count = lo->ldo_stripenr;
2519
2520                 if (info->lti_ea_store_size < sizeof(*v1)) {
2521                         rc = lod_ea_store_resize(info, sizeof(*v1));
2522                         if (rc != 0)
2523                                 RETURN(rc);
2524                         v1 = info->lti_ea_store;
2525                 }
2526
2527                 memset(v1, 0, sizeof(*v1));
2528                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2529                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2530                 v1->lum_stripe_offset =
2531                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
2532
2533                 info->lti_buf.lb_buf = v1;
2534                 info->lti_buf.lb_len = sizeof(*v1);
2535
2536                 if (declare)
2537                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
2538                                                        &info->lti_buf, dof, th);
2539                 else
2540                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2541                                                XATTR_NAME_LMV, 0, th);
2542                 if (rc != 0)
2543                         RETURN(rc);
2544         }
2545
2546         /* Transfer default LMV striping from the parent */
2547         if (lds != NULL && lds->lds_dir_def_striping_set &&
2548             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
2549                                  lds->lds_dir_def_stripe_offset)) {
2550                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2551
2552                 if (info->lti_ea_store_size < sizeof(*v1)) {
2553                         rc = lod_ea_store_resize(info, sizeof(*v1));
2554                         if (rc != 0)
2555                                 RETURN(rc);
2556                         v1 = info->lti_ea_store;
2557                 }
2558
2559                 memset(v1, 0, sizeof(*v1));
2560                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2561                 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
2562                 v1->lum_stripe_offset =
2563                                 cpu_to_le32(lds->lds_dir_def_stripe_offset);
2564                 v1->lum_hash_type =
2565                                 cpu_to_le32(lds->lds_dir_def_hash_type);
2566
2567                 info->lti_buf.lb_buf = v1;
2568                 info->lti_buf.lb_len = sizeof(*v1);
2569                 if (declare)
2570                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2571                                                        XATTR_NAME_DEFAULT_LMV,
2572                                                        0, th);
2573                 else
2574                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2575                                                   &info->lti_buf,
2576                                                   XATTR_NAME_DEFAULT_LMV, 0,
2577                                                   th);
2578                 if (rc != 0)
2579                         RETURN(rc);
2580         }
2581
2582         if (lds != NULL && lds->lds_def_pool[0] != '\0')
2583                 poolname = lds->lds_def_pool;
2584
2585         /* Transfer default LOV striping from the parent */
2586         if (lds != NULL && lds->lds_def_striping_set &&
2587             !LOVEA_DELETE_VALUES(lds->lds_def_stripe_size,
2588                                  lds->lds_def_stripenr,
2589                                  lds->lds_def_stripe_offset,
2590                                  poolname)) {
2591                 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2592
2593                 if (info->lti_ea_store_size < sizeof(*v3)) {
2594                         rc = lod_ea_store_resize(info, sizeof(*v3));
2595                         if (rc != 0)
2596                                 RETURN(rc);
2597                         v3 = info->lti_ea_store;
2598                 }
2599
2600                 memset(v3, 0, sizeof(*v3));
2601                 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2602                 v3->lmm_stripe_count = cpu_to_le16(lds->lds_def_stripenr);
2603                 v3->lmm_stripe_offset = cpu_to_le16(lds->lds_def_stripe_offset);
2604                 v3->lmm_stripe_size = cpu_to_le32(lds->lds_def_stripe_size);
2605                 if (poolname != NULL)
2606                         strlcpy(v3->lmm_pool_name, poolname,
2607                                 sizeof(v3->lmm_pool_name));
2608
2609                 info->lti_buf.lb_buf = v3;
2610                 info->lti_buf.lb_len = sizeof(*v3);
2611
2612                 if (declare)
2613                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2614                                                        XATTR_NAME_LOV, 0, th);
2615                 else
2616                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2617                                                       XATTR_NAME_LOV, 0, th);
2618                 if (rc != 0)
2619                         RETURN(rc);
2620         }
2621
2622         RETURN(0);
2623 }
2624
2625 static int lod_declare_dir_striping_create(const struct lu_env *env,
2626                                            struct dt_object *dt,
2627                                            struct lu_attr *attr,
2628                                            struct dt_object_format *dof,
2629                                            struct thandle *th)
2630 {
2631         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2632 }
2633
2634 static int lod_dir_striping_create(const struct lu_env *env,
2635                                    struct dt_object *dt,
2636                                    struct lu_attr *attr,
2637                                    struct dt_object_format *dof,
2638                                    struct thandle *th)
2639 {
2640         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2641 }
2642
2643 /**
2644  * Implementation of dt_object_operations::do_xattr_set.
2645  *
2646  * Sets specified extended attribute on the object. Three types of EAs are
2647  * special:
2648  *   LOV EA - stores striping for a regular file or default striping (when set
2649  *            on a directory)
2650  *   LMV EA - stores a marker for the striped directories
2651  *   DMV EA - stores default directory striping
2652  *
2653  * When striping is applied to a non-striped existing object (this is called
2654  * late striping), then LOD notices the caller wants to turn the object into a
2655  * striped one. The stripe objects are created and appropriate EA is set:
2656  * LOV EA storing all the stripes directly or LMV EA storing just a small header
2657  * with striping configuration.
2658  *
2659  * \see dt_object_operations::do_xattr_set() in the API description for details.
2660  */
2661 static int lod_xattr_set(const struct lu_env *env,
2662                          struct dt_object *dt, const struct lu_buf *buf,
2663                          const char *name, int fl, struct thandle *th)
2664 {
2665         struct dt_object        *next = dt_object_child(dt);
2666         int                      rc;
2667         ENTRY;
2668
2669         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2670             strcmp(name, XATTR_NAME_LMV) == 0) {
2671                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2672
2673                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2674                                                 LMV_HASH_FLAG_MIGRATION)
2675                         rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2676                                                       th);
2677                 else
2678                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2679
2680                 RETURN(rc);
2681         }
2682
2683         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2684             strcmp(name, XATTR_NAME_LOV) == 0) {
2685                 /* default LOVEA */
2686                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2687                 RETURN(rc);
2688         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2689                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2690                 /* default LMVEA */
2691                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2692                                                       th);
2693                 RETURN(rc);
2694         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2695                    !strcmp(name, XATTR_NAME_LOV)) {
2696                 /* in case of lov EA swap, just set it
2697                  * if not, it is a replay so check striping match what we
2698                  * already have during req replay, declare_xattr_set()
2699                  * defines striping, then create() does the work */
2700                 if (fl & LU_XATTR_REPLACE) {
2701                         /* free stripes, then update disk */
2702                         lod_object_free_striping(env, lod_dt_obj(dt));
2703
2704                         rc = lod_sub_object_xattr_set(env, next, buf, name,
2705                                                       fl, th);
2706                 } else if (dt_object_remote(dt)) {
2707                         /* This only happens during migration, see
2708                          * mdd_migrate_create(), in which Master MDT will
2709                          * create a remote target object, and only set
2710                          * (migrating) stripe EA on the remote object,
2711                          * and does not need creating each stripes. */
2712                         rc = lod_sub_object_xattr_set(env, next, buf, name,
2713                                                       fl, th);
2714                 } else {
2715                         rc = lod_striping_create(env, dt, NULL, NULL, th);
2716                 }
2717                 RETURN(rc);
2718         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2719                 rc = lod_object_replace_parent_fid(env, dt, th, false);
2720
2721                 RETURN(rc);
2722         }
2723
2724         /* then all other xattr */
2725         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2726
2727         RETURN(rc);
2728 }
2729
2730 /**
2731  * Implementation of dt_object_operations::do_declare_xattr_del.
2732  *
2733  * \see dt_object_operations::do_declare_xattr_del() in the API description
2734  * for details.
2735  */
2736 static int lod_declare_xattr_del(const struct lu_env *env,
2737                                  struct dt_object *dt, const char *name,
2738                                  struct thandle *th)
2739 {
2740         struct lod_object       *lo = lod_dt_obj(dt);
2741         int                     rc;
2742         int                     i;
2743         ENTRY;
2744
2745         rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2746                                               name, th);
2747         if (rc != 0)
2748                 RETURN(rc);
2749
2750         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2751                 RETURN(0);
2752
2753         /* set xattr to each stripes, if needed */
2754         rc = lod_load_striping(env, lo);
2755         if (rc != 0)
2756                 RETURN(rc);
2757
2758         if (lo->ldo_stripenr == 0)
2759                 RETURN(0);
2760
2761         for (i = 0; i < lo->ldo_stripenr; i++) {
2762                 LASSERT(lo->ldo_stripe[i]);
2763                 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2764                                                       name, th);
2765                 if (rc != 0)
2766                         break;
2767         }
2768
2769         RETURN(rc);
2770 }
2771
2772 /**
2773  * Implementation of dt_object_operations::do_xattr_del.
2774  *
2775  * If EA storing a regular striping is being deleted, then release
2776  * all the references to the stripe objects in core.
2777  *
2778  * \see dt_object_operations::do_xattr_del() in the API description for details.
2779  */
2780 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2781                          const char *name, struct thandle *th)
2782 {
2783         struct dt_object        *next = dt_object_child(dt);
2784         struct lod_object       *lo = lod_dt_obj(dt);
2785         int                     rc;
2786         int                     i;
2787         ENTRY;
2788
2789         if (!strcmp(name, XATTR_NAME_LOV))
2790                 lod_object_free_striping(env, lod_dt_obj(dt));
2791
2792         rc = lod_sub_object_xattr_del(env, next, name, th);
2793         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2794                 RETURN(rc);
2795
2796         if (lo->ldo_stripenr == 0)
2797                 RETURN(0);
2798
2799         for (i = 0; i < lo->ldo_stripenr; i++) {
2800                 LASSERT(lo->ldo_stripe[i]);
2801
2802                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
2803                 if (rc != 0)
2804                         break;
2805         }
2806
2807         RETURN(rc);
2808 }
2809
2810 /**
2811  * Implementation of dt_object_operations::do_xattr_list.
2812  *
2813  * \see dt_object_operations::do_xattr_list() in the API description
2814  * for details.
2815  */
2816 static int lod_xattr_list(const struct lu_env *env,
2817                           struct dt_object *dt, const struct lu_buf *buf)
2818 {
2819         return dt_xattr_list(env, dt_object_child(dt), buf);
2820 }
2821
2822 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2823 {
2824         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2825 }
2826
2827
2828 /**
2829  * Get default striping.
2830  *
2831  * \param[in] env               execution environment
2832  * \param[in] lo                object
2833  * \param[out] lds              default striping
2834  *
2835  * \retval              0 on success
2836  * \retval              negative if failed
2837  */
2838 static int lod_get_default_lov_striping(const struct lu_env *env,
2839                                         struct lod_object *lo,
2840                                         struct lod_default_striping *lds)
2841 {
2842         struct lod_thread_info *info = lod_env_info(env);
2843         struct lov_user_md_v1 *v1 = NULL;
2844         struct lov_user_md_v3 *v3 = NULL;
2845         int rc;
2846
2847         rc = lod_get_lov_ea(env, lo);
2848         if (rc < 0)
2849                 return rc;
2850
2851         if (rc < (typeof(rc))sizeof(struct lov_user_md))
2852                 return 0;
2853
2854         v1 = info->lti_ea_store;
2855         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2856                 lustre_swab_lov_user_md_v1(v1);
2857         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2858                 v3 = (struct lov_user_md_v3 *)v1;
2859                 lustre_swab_lov_user_md_v3(v3);
2860         }
2861
2862         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2863                 return 0;
2864
2865         if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2866                 return 0;
2867
2868         lds->lds_def_stripenr = v1->lmm_stripe_count;
2869         lds->lds_def_stripe_size = v1->lmm_stripe_size;
2870         lds->lds_def_stripe_offset = v1->lmm_stripe_offset;
2871         lds->lds_def_striping_set = 1;
2872         if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2873                 v3 = (struct lov_user_md_v3 *)v1;
2874                 if (v3->lmm_pool_name[0] != '\0')
2875                         strlcpy(lds->lds_def_pool, v3->lmm_pool_name,
2876                                 sizeof(lds->lds_def_pool));
2877         }
2878
2879         return 0;
2880 }
2881
2882 /**
2883  * Get default directory striping.
2884  *
2885  * \param[in] env               execution environment
2886  * \param[in] lo                object
2887  * \param[out] lds              default striping
2888  *
2889  * \retval              0 on success
2890  * \retval              negative if failed
2891  */
2892 static int lod_get_default_lmv_striping(const struct lu_env *env,
2893                                         struct lod_object *lo,
2894                                         struct lod_default_striping *lds)
2895 {
2896         struct lod_thread_info  *info = lod_env_info(env);
2897         struct lmv_user_md_v1   *v1 = NULL;
2898         int                      rc;
2899
2900         rc = lod_get_default_lmv_ea(env, lo);
2901         if (rc < 0)
2902                 return rc;
2903
2904         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
2905                 return 0;
2906
2907         v1 = info->lti_ea_store;
2908
2909         lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
2910         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
2911         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
2912         lds->lds_dir_def_striping_set = 1;
2913
2914         return 0;
2915 }
2916
2917 /**
2918  * Get default striping in the object.
2919  *
2920  * Get object default striping and default directory striping.
2921  *
2922  * \param[in] env               execution environment
2923  * \param[in] lo                object
2924  * \param[out] lds              default striping
2925  *
2926  * \retval              0 on success
2927  * \retval              negative if failed
2928  */
2929 static int lod_get_default_striping(const struct lu_env *env,
2930                                     struct lod_object *lo,
2931                                     struct lod_default_striping *lds)
2932 {
2933         int rc;
2934
2935         rc = lod_get_default_lov_striping(env, lo, lds);
2936         if (rc == 0)
2937                 rc = lod_get_default_lmv_striping(env, lo, lds);
2938         return rc;
2939 }
2940
2941 /**
2942  * Apply default striping on object.
2943  *
2944  * If object striping pattern is not set, set to the one in default striping.
2945  * The default striping is from parent or fs.
2946  *
2947  * \param[in] lo                new object
2948  * \param[in] lds               default striping
2949  * \param[in] mode              new object's mode
2950  */
2951 static void lod_striping_from_default(struct lod_object *lo,
2952                                       const struct lod_default_striping *lds,
2953                                       umode_t mode)
2954 {
2955         if (lds->lds_def_striping_set && S_ISREG(mode)) {
2956                 if (lo->ldo_stripenr == 0)
2957                         lo->ldo_stripenr = lds->lds_def_stripenr;
2958                 if (lo->ldo_stripe_size == 0)
2959                         lo->ldo_stripe_size = lds->lds_def_stripe_size;
2960                 if (lo->ldo_stripe_offset == LOV_OFFSET_DEFAULT)
2961                         lo->ldo_stripe_offset = lds->lds_def_stripe_offset;
2962                 if (lo->ldo_pool == NULL && lds->lds_def_pool[0] != '\0')
2963                         lod_object_set_pool(lo, lds->lds_def_pool);
2964
2965                 CDEBUG(D_INFO, "striping from default: count %hu, size %u, "
2966                         "offset %d, pool %s\n",
2967                         lo->ldo_stripenr, lo->ldo_stripe_size,
2968                         (int)lo->ldo_stripe_offset, lo->ldo_pool ?: "");
2969         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
2970                 if (lo->ldo_stripenr == 0)
2971                         lo->ldo_stripenr = lds->lds_dir_def_stripenr;
2972                 if (lo->ldo_dir_stripe_offset == -1)
2973                         lo->ldo_dir_stripe_offset =
2974                                 lds->lds_dir_def_stripe_offset;
2975                 if (lo->ldo_dir_hash_type == 0)
2976                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
2977
2978                 CDEBUG(D_INFO, "striping from default: count %hu, offset %d, "
2979                         "hash_type %u\n",
2980                         lo->ldo_stripenr, (int)lo->ldo_dir_stripe_offset,
2981                         lo->ldo_dir_hash_type);
2982         }
2983 }
2984
2985 /**
2986  * Implementation of dt_object_operations::do_ah_init.
2987  *
2988  * This method is used to make a decision on the striping configuration for the
2989  * object being created. It can be taken from the \a parent object if it exists,
2990  * or filesystem's default. The resulting configuration (number of stripes,
2991  * stripe size/offset, pool name, etc) is stored in the object itself and will
2992  * be used by the methods like ->doo_declare_create().
2993  *
2994  * \see dt_object_operations::do_ah_init() in the API description for details.
2995  */
2996 static void lod_ah_init(const struct lu_env *env,
2997                         struct dt_allocation_hint *ah,
2998                         struct dt_object *parent,
2999                         struct dt_object *child,
3000                         umode_t child_mode)
3001 {
3002         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3003         struct lod_thread_info *info = lod_env_info(env);
3004         struct lod_default_striping *lds = &info->lti_def_striping;
3005         struct dt_object *nextp = NULL;
3006         struct dt_object *nextc;
3007         struct lod_object *lp = NULL;
3008         struct lod_object *lc;
3009         struct lov_desc *desc;
3010         ENTRY;
3011
3012         LASSERT(child);
3013
3014         if (likely(parent)) {
3015                 nextp = dt_object_child(parent);
3016                 lp = lod_dt_obj(parent);
3017         }
3018
3019         nextc = dt_object_child(child);
3020         lc = lod_dt_obj(child);
3021
3022         LASSERT(lc->ldo_stripenr == 0);
3023         LASSERT(lc->ldo_stripe == NULL);
3024
3025         if (!dt_object_exists(nextc))
3026                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3027
3028         if (S_ISDIR(child_mode)) {
3029                 /* other default values are 0 */
3030                 lc->ldo_dir_stripe_offset = -1;
3031
3032                 memset(lds, 0, sizeof(*lds));
3033                 lod_get_default_striping(env, lp, lds);
3034
3035                 /* inherit parent default striping */
3036                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3037                         lc->ldo_def_striping = lds;
3038
3039                 /* It should always honour the specified stripes */
3040                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3041                     lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3042                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3043
3044                         lc->ldo_stripenr = le32_to_cpu(lum1->lum_stripe_count);
3045                         lc->ldo_dir_stripe_offset =
3046                                         le32_to_cpu(lum1->lum_stripe_offset);
3047                         lc->ldo_dir_hash_type =
3048                                         le32_to_cpu(lum1->lum_hash_type);
3049                         CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3050                                 "hash_type %u\n",
3051                                 lc->ldo_stripenr,
3052                                 (int)lc->ldo_dir_stripe_offset,
3053                                 lc->ldo_dir_hash_type);
3054                 } else {
3055                         lod_striping_from_default(lc, lds, child_mode);
3056                 }
3057
3058                 /* shrink the stripe_count to the avaible MDT count */
3059                 if (lc->ldo_stripenr > d->lod_remote_mdt_count + 1 &&
3060                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3061                         lc->ldo_stripenr = d->lod_remote_mdt_count + 1;
3062
3063                 /* Directory will be striped only if stripe_count > 1, if
3064                  * stripe_count == 1, let's reset stripenr = 0 to avoid
3065                  * create single master stripe and also help to unify the
3066                  * stripe handling of directories and files */
3067                 if (lc->ldo_stripenr == 1)
3068                         lc->ldo_stripenr = 0;
3069
3070                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3071                        lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset,
3072                        lc->ldo_dir_hash_type);
3073
3074                 RETURN_EXIT;
3075         }
3076
3077         /*
3078          * if object is going to be striped over OSTs, transfer default
3079          * striping information to the child, so that we can use it
3080          * during declaration and creation
3081          */
3082         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3083                                         lu_object_fid(&child->do_lu)))
3084                 RETURN_EXIT;
3085
3086         /* other default values are 0 */
3087         lc->ldo_stripe_offset = LOV_OFFSET_DEFAULT;
3088
3089         /* striping from parent default */
3090         if (likely(parent)) {
3091                 memset(lds, 0, sizeof(*lds));
3092                 lod_get_default_lov_striping(env, lp, lds);
3093                 lod_striping_from_default(lc, lds, child_mode);
3094         }
3095
3096         if (d->lod_md_root == NULL) {
3097                 struct dt_object *root;
3098                 struct lod_object *lroot;
3099
3100                 lu_root_fid(&info->lti_fid);
3101                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3102                 if (!IS_ERR(root)) {
3103                         lroot = lod_dt_obj(root);
3104
3105                         spin_lock(&d->lod_lock);
3106                         if (d->lod_md_root != NULL)
3107                                 lu_object_put(env,
3108                                               &d->lod_md_root->ldo_obj.do_lu);
3109                         d->lod_md_root = lroot;
3110                         spin_unlock(&d->lod_lock);
3111                 }
3112         }
3113
3114         /* if parent doesn't provide all defaults, striping from fs default */
3115         if (d->lod_md_root != NULL &&
3116             (lc->ldo_stripenr == 0 ||
3117              lc->ldo_stripe_size == 0 ||
3118              lc->ldo_stripe_offset == LOV_OFFSET_DEFAULT ||
3119              lc->ldo_pool == NULL)) {
3120                 memset(lds, 0, sizeof(*lds));
3121                 lod_get_default_lov_striping(env, d->lod_md_root, lds);
3122                 lod_striping_from_default(lc, lds, child_mode);
3123         }
3124
3125         /*
3126          * fs default striping may not be explicitly set, or historically set
3127          * in config log, check striping sanity here and fix to sane values.
3128          */
3129         desc = &d->lod_desc;
3130         if (lc->ldo_stripenr == 0)
3131                 lc->ldo_stripenr = desc->ld_default_stripe_count;
3132         if (lc->ldo_stripe_size == 0)
3133                 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3134
3135         CDEBUG(D_INFO, "final striping [%hu %u %d %s]\n",
3136                lc->ldo_stripenr, lc->ldo_stripe_size,
3137                (int)lc->ldo_stripe_offset, lc->ldo_pool ?: "");
3138         EXIT;
3139 }
3140
3141 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
3142 /**
3143  * Size initialization on late striping.
3144  *
3145  * Propagate the size of a truncated object to a deferred striping.
3146  * This function handles a special case when truncate was done on a
3147  * non-striped object and now while the striping is being created
3148  * we can't lose that size, so we have to propagate it to the stripes
3149  * being created.
3150  *
3151  * \param[in] env       execution environment
3152  * \param[in] dt        object
3153  * \param[in] th        transaction handle
3154  *
3155  * \retval              0 on success
3156  * \retval              negative if failed
3157  */
3158 static int lod_declare_init_size(const struct lu_env *env,
3159                                  struct dt_object *dt, struct thandle *th)
3160 {
3161         struct dt_object   *next = dt_object_child(dt);
3162         struct lod_object  *lo = lod_dt_obj(dt);
3163         struct lu_attr     *attr = &lod_env_info(env)->lti_attr;
3164         uint64_t            size, offs;
3165         int                 rc, stripe;
3166         ENTRY;
3167
3168         /* XXX: we support the simplest (RAID0) striping so far */
3169         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3170         LASSERT(lo->ldo_stripe_size > 0);
3171
3172         if (lo->ldo_stripenr == 0)
3173                 RETURN(0);
3174
3175         rc = dt_attr_get(env, next, attr);
3176         LASSERT(attr->la_valid & LA_SIZE);
3177         if (rc)
3178                 RETURN(rc);
3179
3180         size = attr->la_size;
3181         if (size == 0)
3182                 RETURN(0);
3183
3184         /* ll_do_div64(a, b) returns a % b, and a = a / b */
3185         ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3186         stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3187
3188         size = size * lo->ldo_stripe_size;
3189         offs = attr->la_size;
3190         size += ll_do_div64(offs, lo->ldo_stripe_size);
3191
3192         attr->la_valid = LA_SIZE;
3193         attr->la_size = size;
3194
3195         rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3196                                              th);
3197
3198         RETURN(rc);
3199 }
3200
3201 /**
3202  * Declare creation of striped object.
3203  *
3204  * The function declares creation stripes for a regular object. The function
3205  * also declares whether the stripes will be created with non-zero size if
3206  * previously size was set non-zero on the master object. If object \a dt is
3207  * not local, then only fully defined striping can be applied in \a lovea.
3208  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3209  * for the details.
3210  *
3211  * \param[in] env       execution environment
3212  * \param[in] dt        object
3213  * \param[in] attr      attributes the stripes will be created with
3214  * \param[in] lovea     a buffer containing striping description
3215  * \param[in] th        transaction handle
3216  *
3217  * \retval              0 on success
3218  * \retval              negative if failed
3219  */
3220 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3221                                struct lu_attr *attr,
3222                                const struct lu_buf *lovea, struct thandle *th)
3223 {
3224         struct lod_thread_info  *info = lod_env_info(env);
3225         struct dt_object        *next = dt_object_child(dt);
3226         struct lod_object       *lo = lod_dt_obj(dt);
3227         int                      rc;
3228         ENTRY;
3229
3230         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
3231                 GOTO(out, rc = -ENOMEM);
3232
3233         if (!dt_object_remote(next)) {
3234                 /* choose OST and generate appropriate objects */
3235                 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3236                 if (rc)
3237                         GOTO(out, rc);
3238
3239                 /*
3240                  * declare storage for striping data
3241                  */
3242                 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3243                                 lo->ldo_pool ?  LOV_MAGIC_V3 : LOV_MAGIC_V1);
3244         } else {
3245                 /* LOD can not choose OST objects for remote objects, i.e.
3246                  * stripes must be ready before that. Right now, it can only
3247                  * happen during migrate, i.e. migrate process needs to create
3248                  * remote regular file (mdd_migrate_create), then the migrate
3249                  * process will provide stripeEA. */
3250                 LASSERT(lovea != NULL);
3251                 info->lti_buf = *lovea;
3252         }
3253
3254         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3255                                               XATTR_NAME_LOV, 0, th);
3256         if (rc)
3257                 GOTO(out, rc);
3258
3259         /*
3260          * if striping is created with local object's size > 0,
3261          * we have to propagate this size to specific object
3262          * the case is possible only when local object was created previously
3263          */
3264         if (dt_object_exists(next))
3265                 rc = lod_declare_init_size(env, dt, th);
3266
3267 out:
3268         /* failed to create striping or to set initial size, let's reset
3269          * config so that others don't get confused */
3270         if (rc)
3271                 lod_object_free_striping(env, lo);
3272
3273         RETURN(rc);
3274 }
3275
3276 /**
3277  * Implementation of dt_object_operations::do_declare_create.
3278  *
3279  * The method declares creation of a new object. If the object will be striped,
3280  * then helper functions are called to find FIDs for the stripes, declare
3281  * creation of the stripes and declare initialization of the striping
3282  * information to be stored in the master object.
3283  *
3284  * \see dt_object_operations::do_declare_create() in the API description
3285  * for details.
3286  */
3287 static int lod_declare_object_create(const struct lu_env *env,
3288                                      struct dt_object *dt,
3289                                      struct lu_attr *attr,
3290                                      struct dt_allocation_hint *hint,
3291                                      struct dt_object_format *dof,
3292                                      struct thandle *th)
3293 {
3294         struct dt_object   *next = dt_object_child(dt);
3295         struct lod_object  *lo = lod_dt_obj(dt);
3296         int                 rc;
3297         ENTRY;
3298
3299         LASSERT(dof);
3300         LASSERT(attr);
3301         LASSERT(th);
3302
3303         /*
3304          * first of all, we declare creation of local object
3305          */
3306         rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3307         if (rc != 0)
3308                 GOTO(out, rc);
3309
3310         if (dof->dof_type == DFT_SYM)
3311                 dt->do_body_ops = &lod_body_lnk_ops;
3312         else if (dof->dof_type == DFT_REGULAR)
3313                 dt->do_body_ops = &lod_body_ops;
3314
3315         /*
3316          * it's lod_ah_init() that has decided the object will be striped
3317          */
3318         if (dof->dof_type == DFT_REGULAR) {
3319                 /* callers don't want stripes */
3320                 /* XXX: all tricky interactions with ->ah_make_hint() decided
3321                  * to use striping, then ->declare_create() behaving differently
3322                  * should be cleaned */
3323                 if (dof->u.dof_reg.striped == 0)
3324                         lo->ldo_stripenr = 0;
3325                 if (lo->ldo_stripenr > 0)
3326                         rc = lod_declare_striped_object(env, dt, attr,
3327                                                         NULL, th);
3328         } else if (dof->dof_type == DFT_DIR) {
3329                 struct seq_server_site *ss;
3330
3331                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3332
3333                 /* If the parent has default stripeEA, and client
3334                  * did not find it before sending create request,
3335                  * then MDT will return -EREMOTE, and client will
3336                  * retrieve the default stripeEA and re-create the
3337                  * sub directory.
3338                  *
3339                  * Note: if dah_eadata != NULL, it means creating the
3340                  * striped directory with specified stripeEA, then it
3341                  * should ignore the default stripeEA */
3342                 if (hint != NULL && hint->dah_eadata == NULL) {
3343                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
3344                                 GOTO(out, rc = -EREMOTE);
3345
3346                         if (lo->ldo_dir_stripe_offset == -1) {
3347                                 /* child and parent should be in the same MDT */
3348                                 if (hint->dah_parent != NULL &&
3349                                     dt_object_remote(hint->dah_parent))
3350                                         GOTO(out, rc = -EREMOTE);
3351                         } else if (lo->ldo_dir_stripe_offset !=
3352                                    ss->ss_node_id) {
3353                                 struct lod_device *lod;
3354                                 struct lod_tgt_descs *ltd;
3355                                 struct lod_tgt_desc *tgt = NULL;
3356                                 bool found_mdt = false;
3357                                 int i;
3358
3359                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3360                                 ltd = &lod->lod_mdt_descs;
3361                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
3362                                         tgt = LTD_TGT(ltd, i);
3363                                         if (tgt->ltd_index ==
3364                                                 lo->ldo_dir_stripe_offset) {
3365                                                 found_mdt = true;
3366                                                 break;
3367                                         }
3368                                 }
3369
3370                                 /* If the MDT indicated by stripe_offset can be
3371                                  * found, then tell client to resend the create
3372                                  * request to the correct MDT, otherwise return
3373                                  * error to client */
3374                                 if (found_mdt)
3375                                         GOTO(out, rc = -EREMOTE);
3376                                 else
3377                                         GOTO(out, rc = -EINVAL);
3378                         }
3379                 }
3380
3381                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
3382         }
3383 out:
3384         RETURN(rc);
3385 }
3386
3387 /**
3388  * Creation of a striped regular object.
3389  *
3390  * The function is called to create the stripe objects for a regular
3391  * striped file. This can happen at the initial object creation or
3392  * when the caller asks LOD to do so using ->do_xattr_set() method
3393  * (so called late striping). Notice all the information are already
3394  * prepared in the form of the list of objects (ldo_stripe field).
3395  * This is done during declare phase.
3396  *
3397  * \param[in] env       execution environment
3398  * \param[in] dt        object
3399  * \param[in] attr      attributes the stripes will be created with
3400  * \param[in] dof       format of stripes (see OSD API description)
3401  * \param[in] th        transaction handle
3402  *
3403  * \retval              0 on success
3404  * \retval              negative if failed
3405  */
3406 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3407                         struct lu_attr *attr, struct dt_object_format *dof,
3408                         struct thandle *th)
3409 {
3410         struct lod_object *lo = lod_dt_obj(dt);
3411         int                rc = 0, i;
3412         ENTRY;
3413
3414         /* create all underlying objects */
3415         for (i = 0; i < lo->ldo_stripenr; i++) {
3416                 LASSERT(lo->ldo_stripe[i]);
3417                 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3418                                            dof, th);
3419                 if (rc)
3420                         break;
3421         }
3422
3423         if (rc == 0)
3424                 rc = lod_generate_and_set_lovea(env, lo, th);
3425
3426         RETURN(rc);
3427 }
3428
3429 /**
3430  * Implementation of dt_object_operations::do_create.
3431  *
3432  * If any of preceeding methods (like ->do_declare_create(),
3433  * ->do_ah_init(), etc) chose to create a striped object,
3434  * then this method will create the master and the stripes.
3435  *
3436  * \see dt_object_operations::do_create() in the API description for details.
3437  */
3438 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3439                              struct lu_attr *attr,
3440                              struct dt_allocation_hint *hint,
3441                              struct dt_object_format *dof, struct thandle *th)
3442 {
3443         struct lod_object  *lo = lod_dt_obj(dt);
3444         int                 rc;
3445         ENTRY;
3446
3447         /* create local object */
3448         rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3449                                    th);
3450         if (rc != 0)
3451                 RETURN(rc);
3452
3453         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3454             lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3455                 rc = lod_striping_create(env, dt, attr, dof, th);
3456
3457         RETURN(rc);
3458 }
3459
3460 /**
3461  * Implementation of dt_object_operations::do_declare_destroy.
3462  *
3463  * If the object is a striped directory, then the function declares reference
3464  * removal from the master object (this is an index) to the stripes and declares
3465  * destroy of all the stripes. In all the cases, it declares an intention to
3466  * destroy the object itself.
3467  *
3468  * \see dt_object_operations::do_declare_destroy() in the API description
3469  * for details.
3470  */
3471 static int lod_declare_object_destroy(const struct lu_env *env,
3472                                       struct dt_object *dt,
3473                                       struct thandle *th)
3474 {
3475         struct dt_object   *next = dt_object_child(dt);
3476         struct lod_object  *lo = lod_dt_obj(dt);
3477         struct lod_thread_info *info = lod_env_info(env);
3478         char               *stripe_name = info->lti_key;
3479         int                 rc, i;
3480         ENTRY;
3481
3482         /*
3483          * load striping information, notice we don't do this when object
3484          * is being initialized as we don't need this information till
3485          * few specific cases like destroy, chown
3486          */
3487         rc = lod_load_striping(env, lo);
3488         if (rc)
3489                 RETURN(rc);
3490
3491         /* declare destroy for all underlying objects */
3492         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3493                 rc = next->do_ops->do_index_try(env, next,
3494                                                 &dt_directory_features);
3495                 if (rc != 0)
3496                         RETURN(rc);
3497
3498                 for (i = 0; i < lo->ldo_stripenr; i++) {
3499                         rc = lod_sub_object_declare_ref_del(env, next, th);
3500                         if (rc != 0)
3501                                 RETURN(rc);
3502
3503                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3504                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3505                                 i);
3506                         rc = lod_sub_object_declare_delete(env, next,
3507                                         (const struct dt_key *)stripe_name, th);
3508                         if (rc != 0)
3509                                 RETURN(rc);
3510                 }
3511         }
3512
3513         /*
3514          * we declare destroy for the local object
3515          */
3516         rc = lod_sub_object_declare_destroy(env, next, th);
3517         if (rc)
3518                 RETURN(rc);
3519
3520         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
3521             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3522                 RETURN(0);
3523
3524         /* declare destroy all striped objects */
3525         for (i = 0; i < lo->ldo_stripenr; i++) {
3526                 if (lo->ldo_stripe[i] == NULL)
3527                         continue;
3528
3529                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3530                         rc = lod_sub_object_declare_ref_del(env,
3531                                         lo->ldo_stripe[i], th);
3532
3533                 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3534                                         th);
3535                 if (rc != 0)
3536                         break;
3537         }
3538
3539         RETURN(rc);
3540 }
3541
3542 /**
3543  * Implementation of dt_object_operations::do_destroy.
3544  *
3545  * If the object is a striped directory, then the function removes references
3546  * from the master object (this is an index) to the stripes and destroys all
3547  * the stripes. In all the cases, the function destroys the object itself.
3548  *
3549  * \see dt_object_operations::do_destroy() in the API description for details.
3550  */
3551 static int lod_object_destroy(const struct lu_env *env,
3552                 struct dt_object *dt, struct thandle *th)
3553 {
3554         struct dt_object  *next = dt_object_child(dt);
3555         struct lod_object *lo = lod_dt_obj(dt);
3556         struct lod_thread_info *info = lod_env_info(env);
3557         char               *stripe_name = info->lti_key;
3558         unsigned int       i;
3559         int                rc;
3560         ENTRY;
3561
3562         /* destroy sub-stripe of master object */
3563         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3564                 rc = next->do_ops->do_index_try(env, next,
3565                                                 &dt_directory_features);
3566                 if (rc != 0)
3567                         RETURN(rc);
3568
3569                 for (i = 0; i < lo->ldo_stripenr; i++) {
3570                         rc = lod_sub_object_ref_del(env, next, th);
3571                         if (rc != 0)
3572                                 RETURN(rc);
3573
3574                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3575                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3576                                 i);
3577
3578                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3579                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3580                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3581
3582                         rc = lod_sub_object_delete(env, next,
3583                                        (const struct dt_key *)stripe_name, th);
3584                         if (rc != 0)
3585                                 RETURN(rc);
3586                 }
3587         }
3588
3589         rc = lod_sub_object_destroy(env, next, th);
3590         if (rc != 0)
3591                 RETURN(rc);
3592
3593         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
3594             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
3595                 RETURN(0);
3596
3597         /* destroy all striped objects */
3598         for (i = 0; i < lo->ldo_stripenr; i++) {
3599                 if (likely(lo->ldo_stripe[i] != NULL) &&
3600                     (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3601                      i == cfs_fail_val)) {
3602                         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3603                                 dt_write_lock(env, lo->ldo_stripe[i],
3604                                               MOR_TGT_CHILD);
3605                                 rc = lod_sub_object_ref_del(env,
3606                                                 lo->ldo_stripe[i], th);
3607                                 dt_write_unlock(env, lo->ldo_stripe[i]);
3608                                 if (rc != 0)
3609                                         break;
3610                         }
3611
3612                         rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3613                         if (rc != 0)
3614                                 break;
3615                 }
3616         }
3617
3618         RETURN(rc);
3619 }
3620
3621 /**
3622  * Implementation of dt_object_operations::do_declare_ref_add.
3623  *
3624  * \see dt_object_operations::do_declare_ref_add() in the API description
3625  * for details.
3626  */
3627 static int lod_declare_ref_add(const struct lu_env *env,
3628                                struct dt_object *dt, struct thandle *th)
3629 {
3630         return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3631 }
3632
3633 /**
3634  * Implementation of dt_object_operations::do_ref_add.
3635  *
3636  * \see dt_object_operations::do_ref_add() in the API description for details.
3637  */
3638 static int lod_ref_add(const struct lu_env *env,
3639                        struct dt_object *dt, struct thandle *th)
3640 {
3641         return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3642 }
3643
3644 /**
3645  * Implementation of dt_object_operations::do_declare_ref_del.
3646  *
3647  * \see dt_object_operations::do_declare_ref_del() in the API description
3648  * for details.
3649  */
3650 static int lod_declare_ref_del(const struct lu_env *env,
3651                                struct dt_object *dt, struct thandle *th)
3652 {
3653         return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3654 }
3655
3656 /**
3657  * Implementation of dt_object_operations::do_ref_del
3658  *
3659  * \see dt_object_operations::do_ref_del() in the API description for details.
3660  */
3661 static int lod_ref_del(const struct lu_env *env,
3662                        struct dt_object *dt, struct thandle *th)
3663 {
3664         return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3665 }
3666
3667 /**
3668  * Implementation of dt_object_operations::do_object_sync.
3669  *
3670  * \see dt_object_operations::do_object_sync() in the API description
3671  * for details.
3672  */
3673 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3674                            __u64 start, __u64 end)
3675 {
3676         return dt_object_sync(env, dt_object_child(dt), start, end);
3677 }
3678
3679 /**
3680  * Release LDLM locks on the stripes of a striped directory.
3681  *
3682  * Iterates over all the locks taken on the stripe objects and
3683  * cancel them.
3684  *
3685  * \param[in] env       execution environment
3686  * \param[in] dt        striped object
3687  * \param[in] einfo     lock description
3688  * \param[in] policy    data describing requested lock
3689  *
3690  * \retval              0 on success
3691  * \retval              negative if failed
3692  */
3693 static int lod_object_unlock_internal(const struct lu_env *env,
3694                                       struct dt_object *dt,
3695                                       struct ldlm_enqueue_info *einfo,
3696                                       union ldlm_policy_data *policy)
3697 {
3698         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
3699         int                     rc = 0;
3700         int                     i;
3701         ENTRY;
3702
3703         if (slave_locks == NULL)
3704                 RETURN(0);
3705
3706         for (i = 1; i < slave_locks->count; i++) {
3707                 if (lustre_handle_is_used(&slave_locks->handles[i]))
3708                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
3709                                                     einfo->ei_mode);
3710         }
3711
3712         RETURN(rc);
3713 }
3714
3715 /**
3716  * Implementation of dt_object_operations::do_object_unlock.
3717  *
3718  * Used to release LDLM lock(s).
3719  *
3720  * \see dt_object_operations::do_object_unlock() in the API description
3721  * for details.
3722  */
3723 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3724                              struct ldlm_enqueue_info *einfo,
3725                              union ldlm_policy_data *policy)
3726 {
3727         struct lod_object *lo = lod_dt_obj(dt);
3728         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
3729         int slave_locks_size;
3730         int i;
3731         ENTRY;
3732
3733         if (slave_locks == NULL)
3734                 RETURN(0);
3735
3736         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
3737         LASSERT(lo->ldo_stripenr > 1);
3738         /* Note: for remote lock for single stripe dir, MDT will cancel
3739          * the lock by lockh directly */
3740         LASSERT(!dt_object_remote(dt_object_child(dt)));
3741
3742         /* locks were unlocked in MDT layer */
3743         for (i = 1; i < slave_locks->count; i++) {
3744                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
3745                 dt_invalidate(env, lo->ldo_stripe[i]);
3746         }
3747
3748         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
3749                            sizeof(slave_locks->handles[0]);
3750         OBD_FREE(slave_locks, slave_locks_size);
3751         einfo->ei_cbdata = NULL;
3752
3753         RETURN(0);
3754 }
3755
3756 /**
3757  * Implementation of dt_object_operations::do_object_lock.
3758  *
3759  * Used to get LDLM lock on the non-striped and striped objects.
3760  *
3761  * \see dt_object_operations::do_object_lock() in the API description
3762  * for details.
3763  */
3764 static int lod_object_lock(const struct lu_env *env,
3765                            struct dt_object *dt,
3766                            struct lustre_handle *lh,
3767                            struct ldlm_enqueue_info *einfo,
3768                            union ldlm_policy_data *policy)
3769 {
3770         struct lod_object       *lo = lod_dt_obj(dt);
3771         int                     rc = 0;
3772         int                     i;
3773         int                     slave_locks_size;
3774         struct lustre_handle_array *slave_locks = NULL;
3775         ENTRY;
3776
3777         /* remote object lock */
3778         if (!einfo->ei_enq_slave) {
3779                 LASSERT(dt_object_remote(dt));
3780                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3781                                       policy);
3782         }
3783
3784         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3785                 GOTO(out, rc = -ENOTDIR);
3786
3787         rc = lod_load_striping(env, lo);
3788         if (rc != 0)
3789                 GOTO(out, rc);
3790
3791         /* No stripes */
3792         if (lo->ldo_stripenr <= 1) {
3793                 /*
3794                  * NB, ei_cbdata stores pointer to slave locks, if no locks
3795                  * taken, make sure it's set to NULL, otherwise MDT will try to
3796                  * unlock them.
3797                  */
3798                 einfo->ei_cbdata = NULL;
3799                 GOTO(out, rc = 0);
3800         }
3801
3802         slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3803                            sizeof(slave_locks->handles[0]);
3804         /* Freed in lod_object_unlock */
3805         OBD_ALLOC(slave_locks, slave_locks_size);
3806         if (slave_locks == NULL)
3807                 GOTO(out, rc = -ENOMEM);
3808         slave_locks->count = lo->ldo_stripenr;
3809
3810         /* striped directory lock */
3811         for (i = 1; i < lo->ldo_stripenr; i++) {
3812                 struct lustre_handle    lockh;
3813                 struct ldlm_res_id      *res_id;
3814
3815                 res_id = &lod_env_info(env)->lti_res_id;
3816                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3817                                        res_id);
3818                 einfo->ei_res_id = res_id;
3819
3820                 LASSERT(lo->ldo_stripe[i] != NULL);
3821                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
3822                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
3823                                             einfo, policy);
3824                 } else {
3825                         struct ldlm_namespace *ns = einfo->ei_namespace;
3826                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
3827                         ldlm_completion_callback completion = einfo->ei_cb_cp;
3828                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
3829
3830                         if (einfo->ei_mode == LCK_PW ||
3831                             einfo->ei_mode == LCK_EX)
3832                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
3833
3834                         /* This only happens if there are mulitple stripes
3835                          * on the master MDT, i.e. except stripe0, there are
3836                          * other stripes on the Master MDT as well, Only
3837                          * happens in the test case right now. */
3838                         LASSERT(ns != NULL);
3839                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
3840                                                     policy, einfo->ei_mode,
3841                                                     &dlmflags, blocking,
3842                                                     completion, NULL,
3843                                                     NULL, 0, LVB_T_NONE,
3844                                                     NULL, &lockh);
3845                 }
3846                 if (rc != 0)
3847                         break;
3848                 slave_locks->handles[i] = lockh;
3849         }
3850         einfo->ei_cbdata = slave_locks;
3851
3852         if (rc != 0 && slave_locks != NULL) {
3853                 lod_object_unlock_internal(env, dt, einfo, policy);
3854                 OBD_FREE(slave_locks, slave_locks_size);
3855         }
3856         EXIT;
3857 out:
3858         if (rc != 0)
3859                 einfo->ei_cbdata = NULL;
3860         RETURN(rc);
3861 }
3862
3863 /**
3864  * Implementation of dt_object_operations::do_invalidate.
3865  *
3866  * \see dt_object_operations::do_invalidate() in the API description for details
3867  */
3868 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
3869 {
3870         return dt_invalidate(env, dt_object_child(dt));
3871 }
3872
3873 struct dt_object_operations lod_obj_ops = {
3874         .do_read_lock           = lod_object_read_lock,
3875         .do_write_lock          = lod_object_write_lock,
3876         .do_read_unlock         = lod_object_read_unlock,
3877         .do_write_unlock        = lod_object_write_unlock,
3878         .do_write_locked        = lod_object_write_locked,
3879         .do_attr_get            = lod_attr_get,
3880         .do_declare_attr_set    = lod_declare_attr_set,
3881         .do_attr_set            = lod_attr_set,
3882         .do_xattr_get           = lod_xattr_get,
3883         .do_declare_xattr_set   = lod_declare_xattr_set,
3884         .do_xattr_set           = lod_xattr_set,
3885         .do_declare_xattr_del   = lod_declare_xattr_del,
3886         .do_xattr_del           = lod_xattr_del,
3887         .do_xattr_list          = lod_xattr_list,
3888         .do_ah_init             = lod_ah_init,
3889         .do_declare_create      = lod_declare_object_create,
3890         .do_create              = lod_object_create,
3891         .do_declare_destroy     = lod_declare_object_destroy,
3892         .do_destroy             = lod_object_destroy,
3893         .do_index_try           = lod_index_try,
3894         .do_declare_ref_add     = lod_declare_ref_add,
3895         .do_ref_add             = lod_ref_add,
3896         .do_declare_ref_del     = lod_declare_ref_del,
3897         .do_ref_del             = lod_ref_del,
3898         .do_object_sync         = lod_object_sync,
3899         .do_object_lock         = lod_object_lock,
3900         .do_object_unlock       = lod_object_unlock,
3901         .do_invalidate          = lod_invalidate,
3902 };
3903
3904 /**
3905  * Implementation of dt_body_operations::dbo_read.
3906  *
3907  * \see dt_body_operations::dbo_read() in the API description for details.
3908  */
3909 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
3910                         struct lu_buf *buf, loff_t *pos)
3911 {
3912         struct dt_object *next = dt_object_child(dt);
3913         return next->do_body_ops->dbo_read(env, next, buf, pos);
3914 }
3915
3916 /**
3917  * Implementation of dt_body_operations::dbo_declare_write.
3918  *
3919  * \see dt_body_operations::dbo_declare_write() in the API description
3920  * for details.
3921  */
3922 static ssize_t lod_declare_write(const struct lu_env *env,
3923                                  struct dt_object *dt,
3924                                  const struct lu_buf *buf, loff_t pos,
3925                                  struct thandle *th)
3926 {
3927         return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
3928                                             th);
3929 }
3930
3931 /**
3932  * Implementation of dt_body_operations::dbo_write.
3933  *
3934  * \see dt_body_operations::dbo_write() in the API description for details.
3935  */
3936 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
3937                          const struct lu_buf *buf, loff_t *pos,
3938                          struct thandle *th, int iq)
3939 {
3940         return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
3941 }
3942
3943 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
3944                              __u64 start, __u64 end, struct thandle *th)
3945 {
3946         if (dt_object_remote(dt))
3947                 return -ENOTSUPP;
3948
3949         return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
3950                                             end, th);
3951 }
3952
3953 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
3954                      __u64 start, __u64 end, struct thandle *th)
3955 {
3956         if (dt_object_remote(dt))
3957                 return -ENOTSUPP;
3958
3959         return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
3960 }
3961
3962 static const struct dt_body_operations lod_body_lnk_ops = {
3963         .dbo_read               = lod_read,
3964         .dbo_declare_write      = lod_declare_write,
3965         .dbo_write              = lod_write
3966 };
3967
3968 static const struct dt_body_operations lod_body_ops = {
3969         .dbo_read               = lod_read,
3970         .dbo_declare_write      = lod_declare_write,
3971         .dbo_write              = lod_write,
3972         .dbo_declare_punch      = lod_declare_punch,
3973         .dbo_punch              = lod_punch,
3974 };
3975
3976 /**
3977  * Implementation of lu_object_operations::loo_object_init.
3978  *
3979  * The function determines the type and the index of the target device using
3980  * sequence of the object's FID. Then passes control down to the
3981  * corresponding device:
3982  *  OSD for the local objects, OSP for remote
3983  *
3984  * \see lu_object_operations::loo_object_init() in the API description
3985  * for details.
3986  */
3987 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
3988                            const struct lu_object_conf *conf)
3989 {
3990         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
3991         struct lu_device        *cdev   = NULL;
3992         struct lu_object        *cobj;
3993         struct lod_tgt_descs    *ltd    = NULL;
3994         struct lod_tgt_desc     *tgt;
3995         u32                      idx    = 0;
3996         int                      type   = LU_SEQ_RANGE_ANY;
3997         int                      rc;
3998         ENTRY;
3999
4000         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4001         if (rc != 0) {
4002                 /* Note: Sometimes, it will Return EAGAIN here, see
4003                  * ptrlpc_import_delay_req(), which might confuse
4004                  * lu_object_find_at() and make it wait there incorrectly.
4005                  * so we convert it to EIO here.*/
4006                 if (rc == -EAGAIN)
4007                         rc = -EIO;
4008
4009                 RETURN(rc);
4010         }
4011
4012         if (type == LU_SEQ_RANGE_MDT &&
4013             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4014                 cdev = &lod->lod_child->dd_lu_dev;
4015         } else if (type == LU_SEQ_RANGE_MDT) {
4016                 ltd = &lod->lod_mdt_descs;
4017                 lod_getref(ltd);
4018         } else if (type == LU_SEQ_RANGE_OST) {
4019                 ltd = &lod->lod_ost_descs;
4020                 lod_getref(ltd);
4021         } else {
4022                 LBUG();
4023         }
4024
4025         if (ltd != NULL) {
4026                 if (ltd->ltd_tgts_size > idx &&
4027                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4028                         tgt = LTD_TGT(ltd, idx);
4029
4030                         LASSERT(tgt != NULL);
4031                         LASSERT(tgt->ltd_tgt != NULL);
4032
4033                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
4034                 }
4035                 lod_putref(lod, ltd);
4036         }
4037
4038         if (unlikely(cdev == NULL))
4039                 RETURN(-ENOENT);
4040
4041         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4042         if (unlikely(cobj == NULL))
4043                 RETURN(-ENOMEM);
4044
4045         lu_object_add(lo, cobj);
4046
4047         RETURN(0);
4048 }
4049
4050 /**
4051  *
4052  * Release resources associated with striping.
4053  *
4054  * If the object is striped (regular or directory), then release
4055  * the stripe objects references and free the ldo_stripe array.
4056  *
4057  * \param[in] env       execution environment
4058  * \param[in] lo        object
4059  */
4060 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4061 {
4062         int len;
4063
4064         if (lo->ldo_stripe != NULL) {
4065                 int i;
4066
4067                 LASSERT(lo->ldo_stripes_allocated > 0);
4068
4069                 for (i = 0; i < lo->ldo_stripenr; i++) {
4070                         if (lo->ldo_stripe[i])
4071                                 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4072                 }
4073
4074                 len = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4075                 OBD_FREE(lo->ldo_stripe, len);
4076                 lo->ldo_stripe = NULL;
4077                 lo->ldo_stripes_allocated = 0;
4078         }
4079         lo->ldo_stripenr = 0;
4080 }
4081
4082 /**
4083  * Implementation of lu_object_operations::loo_object_start.
4084  *
4085  * \see lu_object_operations::loo_object_start() in the API description
4086  * for details.
4087  */
4088 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4089 {
4090         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
4091                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4092         } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
4093                    fid_is_local_file(lu_object_fid(o))) {
4094                 /* Note: some local file (like last rcvd) is created
4095                  * through bottom layer (OSD), so the object initialization
4096                  * comes to lod, it does not set loh_attr yet, so
4097                  * set do_body_ops for local file anyway */
4098                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
4099         }
4100         return 0;
4101 }
4102
4103 /**
4104  * Implementation of lu_object_operations::loo_object_free.
4105  *
4106  * \see lu_object_operations::loo_object_free() in the API description
4107  * for details.
4108  */
4109 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4110 {
4111         struct lod_object *lo = lu2lod_obj(o);
4112
4113         lod_object_set_pool(lo, NULL);
4114         /* release all underlying object pinned */
4115         lod_object_free_striping(env, lo);
4116         lu_object_fini(o);
4117         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
4118 }
4119
4120 /**
4121  * Implementation of lu_object_operations::loo_object_release.
4122  *
4123  * \see lu_object_operations::loo_object_release() in the API description
4124  * for details.
4125  */
4126 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4127 {
4128         /* XXX: shouldn't we release everything here in case if object
4129          * creation failed before? */
4130 }
4131
4132 /**
4133  * Implementation of lu_object_operations::loo_object_print.
4134  *
4135  * \see lu_object_operations::loo_object_print() in the API description
4136  * for details.
4137  */
4138 static int lod_object_print(const struct lu_env *env, void *cookie,
4139                             lu_printer_t p, const struct lu_object *l)
4140 {
4141         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4142
4143         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4144 }
4145
4146 struct lu_object_operations lod_lu_obj_ops = {
4147         .loo_object_init        = lod_object_init,
4148         .loo_object_start       = lod_object_start,
4149         .loo_object_free        = lod_object_free,
4150         .loo_object_release     = lod_object_release,
4151         .loo_object_print       = lod_object_print,
4152 };