Whamcloud - gitweb
LU-7124 o2iblnd: limit cap.max_send_wr for MLX5
[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, 2015, 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_data_new(&ldata, &info->lti_linkea_buf);
1652                 if (rc != 0)
1653                         GOTO(out, rc);
1654
1655                 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
1656                 if (rc != 0)
1657                         GOTO(out, rc);
1658
1659                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1660                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1661                 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1662                                           XATTR_NAME_LINK, 0, th);
1663                 if (rc != 0)
1664                         GOTO(out, rc);
1665
1666                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1667                 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1668                                        (const struct dt_rec *)rec,
1669                                        (const struct dt_key *)stripe_name,
1670                                        th);
1671                 if (rc != 0)
1672                         GOTO(out, rc);
1673
1674                 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1675                                                     th);
1676                 if (rc != 0)
1677                         GOTO(out, rc);
1678         }
1679
1680         rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1681                                 &lmv_buf, XATTR_NAME_LMV, 0, th);
1682         if (rc != 0)
1683                 GOTO(out, rc);
1684 out:
1685         if (slave_lmm != NULL)
1686                 OBD_FREE_PTR(slave_lmm);
1687
1688         RETURN(rc);
1689 }
1690
1691 static int lod_prep_md_striped_create(const struct lu_env *env,
1692                                       struct dt_object *dt,
1693                                       struct lu_attr *attr,
1694                                       const struct lmv_user_md_v1 *lum,
1695                                       struct dt_object_format *dof,
1696                                       struct thandle *th)
1697 {
1698         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1699         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1700         struct lod_object       *lo = lod_dt_obj(dt);
1701         struct dt_object        **stripe;
1702         __u32                   stripe_count;
1703         int                     *idx_array;
1704         __u32                   master_index;
1705         int                     rc = 0;
1706         __u32                   i;
1707         __u32                   j;
1708         ENTRY;
1709
1710         /* The lum has been verifed in lod_verify_md_striping */
1711         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1712         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1713
1714         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1715
1716         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1717         if (stripe == NULL)
1718                 RETURN(-ENOMEM);
1719
1720         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1721         if (idx_array == NULL)
1722                 GOTO(out_free, rc = -ENOMEM);
1723
1724         /* Start index will be the master MDT */
1725         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1726         idx_array[0] = master_index;
1727         for (i = 0; i < stripe_count; i++) {
1728                 struct lod_tgt_desc     *tgt = NULL;
1729                 struct dt_object        *dto;
1730                 struct lu_fid           fid = { 0 };
1731                 int                     idx;
1732                 struct lu_object_conf   conf = { 0 };
1733                 struct dt_device        *tgt_dt = NULL;
1734
1735                 /* Try to find next avaible target */
1736                 idx = idx_array[i];
1737                 for (j = 0; j < lod->lod_remote_mdt_count;
1738                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1739                         bool already_allocated = false;
1740                         __u32 k;
1741
1742                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1743                                idx, lod->lod_remote_mdt_count + 1, i);
1744                         if (idx == master_index) {
1745                                 /* Allocate the FID locally */
1746                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1747                                                    &fid, NULL);
1748                                 if (rc < 0)
1749                                         GOTO(out_put, rc);
1750                                 tgt_dt = lod->lod_child;
1751                                 break;
1752                         }
1753
1754                         /* Find next available target */
1755                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx))
1756                                 continue;
1757
1758                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1759                                 /* check whether the idx already exists
1760                                  * in current allocated array */
1761                                 for (k = 0; k < i; k++) {
1762                                         if (idx_array[k] == idx) {
1763                                                 already_allocated = true;
1764                                                 break;
1765                                         }
1766                                 }
1767
1768                                 if (already_allocated)
1769                                         continue;
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 - 1);
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         if (idx_array != NULL)
1846                 OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1847
1848         RETURN(rc);
1849 }
1850
1851 /**
1852  * Declare create striped md object.
1853  *
1854  * The function declares intention to create a striped directory. This is a
1855  * wrapper for lod_prep_md_striped_create(). The only additional functionality
1856  * is to verify pattern \a lum_buf is good. Check that function for the details.
1857  *
1858  * \param[in] env       execution environment
1859  * \param[in] dt        object
1860  * \param[in] attr      attributes to initialize the objects with
1861  * \param[in] lum_buf   a pattern specifying the number of stripes and
1862  *                      MDT to start from
1863  * \param[in] dof       type of objects to be created
1864  * \param[in] th        transaction handle
1865  *
1866  * \retval              0 on success
1867  * \retval              negative if failed
1868  *
1869  */
1870 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1871                                      struct dt_object *dt,
1872                                      struct lu_attr *attr,
1873                                      const struct lu_buf *lum_buf,
1874                                      struct dt_object_format *dof,
1875                                      struct thandle *th)
1876 {
1877         struct lod_object       *lo = lod_dt_obj(dt);
1878         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1879         struct lmv_user_md_v1   *lum;
1880         int                     rc;
1881         ENTRY;
1882
1883         lum = lum_buf->lb_buf;
1884         LASSERT(lum != NULL);
1885
1886         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1887                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1888                (int)le32_to_cpu(lum->lum_stripe_offset));
1889
1890         if (le32_to_cpu(lum->lum_stripe_count) == 0)
1891                 GOTO(out, rc = 0);
1892
1893         rc = lod_verify_md_striping(lod, lum);
1894         if (rc != 0)
1895                 GOTO(out, rc);
1896
1897         /* prepare dir striped objects */
1898         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
1899         if (rc != 0) {
1900                 /* failed to create striping, let's reset
1901                  * config so that others don't get confused */
1902                 lod_object_free_striping(env, lo);
1903                 GOTO(out, rc);
1904         }
1905 out:
1906         RETURN(rc);
1907 }
1908
1909 /**
1910  * Implementation of dt_object_operations::do_declare_xattr_set.
1911  *
1912  * Used with regular (non-striped) objects. Basically it
1913  * initializes the striping information and applies the
1914  * change to all the stripes.
1915  *
1916  * \see dt_object_operations::do_declare_xattr_set() in the API description
1917  * for details.
1918  */
1919 static int lod_dir_declare_xattr_set(const struct lu_env *env,
1920                                      struct dt_object *dt,
1921                                      const struct lu_buf *buf,
1922                                      const char *name, int fl,
1923                                      struct thandle *th)
1924 {
1925         struct dt_object        *next = dt_object_child(dt);
1926         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
1927         struct lod_object       *lo = lod_dt_obj(dt);
1928         int                     i;
1929         int                     rc;
1930         ENTRY;
1931
1932         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
1933                 struct lmv_user_md_v1 *lum;
1934
1935                 LASSERT(buf != NULL && buf->lb_buf != NULL);
1936                 lum = buf->lb_buf;
1937                 rc = lod_verify_md_striping(d, lum);
1938                 if (rc != 0)
1939                         RETURN(rc);
1940         }
1941
1942         rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
1943         if (rc != 0)
1944                 RETURN(rc);
1945
1946         /* Note: Do not set LinkEA on sub-stripes, otherwise
1947          * it will confuse the fid2path process(see mdt_path_current()).
1948          * The linkEA between master and sub-stripes is set in
1949          * lod_xattr_set_lmv(). */
1950         if (strcmp(name, XATTR_NAME_LINK) == 0)
1951                 RETURN(0);
1952
1953         /* set xattr to each stripes, if needed */
1954         rc = lod_load_striping(env, lo);
1955         if (rc != 0)
1956                 RETURN(rc);
1957
1958         if (lo->ldo_stripenr == 0)
1959                 RETURN(0);
1960
1961         for (i = 0; i < lo->ldo_stripenr; i++) {
1962                 LASSERT(lo->ldo_stripe[i]);
1963
1964                 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
1965                                                 buf, name, fl, th);
1966                 if (rc != 0)
1967                         break;
1968         }
1969
1970         RETURN(rc);
1971 }
1972
1973 /**
1974  * Reset parent FID on OST object
1975  *
1976  * Replace parent FID with @dt object FID, which is only called during migration
1977  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
1978  * the FID is changed.
1979  *
1980  * \param[in] env execution environment
1981  * \param[in] dt dt_object whose stripes's parent FID will be reset
1982  * \parem[in] th thandle
1983  * \param[in] declare if it is declare
1984  *
1985  * \retval      0 if reset succeeds
1986  * \retval      negative errno if reset fais
1987  */
1988 static int lod_object_replace_parent_fid(const struct lu_env *env,
1989                                          struct dt_object *dt,
1990                                          struct thandle *th, bool declare)
1991 {
1992         struct lod_object *lo = lod_dt_obj(dt);
1993         struct lod_thread_info  *info = lod_env_info(env);
1994         struct lu_buf *buf = &info->lti_buf;
1995         struct filter_fid *ff;
1996         int i, rc;
1997         ENTRY;
1998
1999         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2000
2001         /* set xattr to each stripes, if needed */
2002         rc = lod_load_striping(env, lo);
2003         if (rc != 0)
2004                 RETURN(rc);
2005
2006         if (lo->ldo_stripenr == 0)
2007                 RETURN(0);
2008
2009         if (info->lti_ea_store_size < sizeof(*ff)) {
2010                 rc = lod_ea_store_resize(info, sizeof(*ff));
2011                 if (rc != 0)
2012                         RETURN(rc);
2013         }
2014
2015         buf->lb_buf = info->lti_ea_store;
2016         buf->lb_len = info->lti_ea_store_size;
2017
2018         for (i = 0; i < lo->ldo_stripenr; i++) {
2019                 if (lo->ldo_stripe[i] == NULL)
2020                         continue;
2021
2022                 rc = dt_xattr_get(env, lo->ldo_stripe[i], buf,
2023                                   XATTR_NAME_FID);
2024                 if (rc < 0) {
2025                         rc = 0;
2026                         continue;
2027                 }
2028
2029                 ff = buf->lb_buf;
2030                 fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
2031                 ff->ff_parent.f_seq = lu_object_fid(&dt->do_lu)->f_seq;
2032                 ff->ff_parent.f_oid = lu_object_fid(&dt->do_lu)->f_oid;
2033                 fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2034
2035                 if (declare) {
2036                         rc = lod_sub_object_declare_xattr_set(env,
2037                                                 lo->ldo_stripe[i], buf,
2038                                                 XATTR_NAME_FID,
2039                                                 LU_XATTR_REPLACE, th);
2040                 } else {
2041                         rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i],
2042                                                       buf, XATTR_NAME_FID,
2043                                                       LU_XATTR_REPLACE, th);
2044                 }
2045                 if (rc < 0)
2046                         break;
2047         }
2048
2049         RETURN(rc);
2050 }
2051
2052 /**
2053  * Implementation of dt_object_operations::do_declare_xattr_set.
2054  *
2055  * \see dt_object_operations::do_declare_xattr_set() in the API description
2056  * for details.
2057  *
2058  * the extension to the API:
2059  *   - declaring LOVEA requests striping creation
2060  *   - LU_XATTR_REPLACE means layout swap
2061  */
2062 static int lod_declare_xattr_set(const struct lu_env *env,
2063                                  struct dt_object *dt,
2064                                  const struct lu_buf *buf,
2065                                  const char *name, int fl,
2066                                  struct thandle *th)
2067 {
2068         struct dt_object *next = dt_object_child(dt);
2069         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2070         __u32             mode;
2071         int               rc;
2072         ENTRY;
2073
2074         /*
2075          * allow to declare predefined striping on a new (!mode) object
2076          * which is supposed to be replay of regular file creation
2077          * (when LOV setting is declared)
2078          * LU_XATTR_REPLACE is set to indicate a layout swap
2079          */
2080         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2081         if ((S_ISREG(mode) || mode == 0) && strcmp(name, XATTR_NAME_LOV) == 0 &&
2082              !(fl & LU_XATTR_REPLACE)) {
2083                 /*
2084                  * this is a request to manipulate object's striping
2085                  */
2086                 if (dt_object_exists(dt)) {
2087                         rc = dt_attr_get(env, next, attr);
2088                         if (rc)
2089                                 RETURN(rc);
2090                 } else {
2091                         memset(attr, 0, sizeof(*attr));
2092                         attr->la_valid = LA_TYPE | LA_MODE;
2093                         attr->la_mode = S_IFREG;
2094                 }
2095                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2096         } else if (S_ISDIR(mode)) {
2097                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2098         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2099                 rc = lod_object_replace_parent_fid(env, dt, th, true);
2100         } else {
2101                 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2102                                                       fl, th);
2103         }
2104
2105         RETURN(rc);
2106 }
2107
2108 /**
2109  * Resets cached default striping in the object.
2110  *
2111  * \param[in] lo        object
2112  */
2113 static void lod_lov_stripe_cache_clear(struct lod_object *lo)
2114 {
2115         lo->ldo_def_striping_set = 0;
2116         lo->ldo_def_striping_cached = 0;
2117         lod_object_set_pool(lo, NULL);
2118         lo->ldo_def_stripe_size = 0;
2119         lo->ldo_def_stripenr = 0;
2120         if (lo->ldo_dir_stripe != NULL)
2121                 lo->ldo_dir_def_striping_cached = 0;
2122 }
2123
2124 /**
2125  * Apply xattr changes to the object.
2126  *
2127  * Applies xattr changes to the object and the stripes if the latter exist.
2128  *
2129  * \param[in] env       execution environment
2130  * \param[in] dt        object
2131  * \param[in] buf       buffer pointing to the new value of xattr
2132  * \param[in] name      name of xattr
2133  * \param[in] fl        flags
2134  * \param[in] th        transaction handle
2135  *
2136  * \retval              0 on success
2137  * \retval              negative if failed
2138  */
2139 static int lod_xattr_set_internal(const struct lu_env *env,
2140                                   struct dt_object *dt,
2141                                   const struct lu_buf *buf,
2142                                   const char *name, int fl,
2143                                   struct thandle *th)
2144 {
2145         struct dt_object        *next = dt_object_child(dt);
2146         struct lod_object       *lo = lod_dt_obj(dt);
2147         int                     rc;
2148         int                     i;
2149         ENTRY;
2150
2151         rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2152         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2153                 RETURN(rc);
2154
2155         /* Note: Do not set LinkEA on sub-stripes, otherwise
2156          * it will confuse the fid2path process(see mdt_path_current()).
2157          * The linkEA between master and sub-stripes is set in
2158          * lod_xattr_set_lmv(). */
2159         if (lo->ldo_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2160                 RETURN(0);
2161
2162         for (i = 0; i < lo->ldo_stripenr; i++) {
2163                 LASSERT(lo->ldo_stripe[i]);
2164
2165                 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2166                                               fl, th);
2167                 if (rc != 0)
2168                         break;
2169         }
2170
2171         RETURN(rc);
2172 }
2173
2174 /**
2175  * Delete an extended attribute.
2176  *
2177  * Deletes specified xattr from the object and the stripes if the latter exist.
2178  *
2179  * \param[in] env       execution environment
2180  * \param[in] dt        object
2181  * \param[in] name      name of xattr
2182  * \param[in] th        transaction handle
2183  *
2184  * \retval              0 on success
2185  * \retval              negative if failed
2186  */
2187 static int lod_xattr_del_internal(const struct lu_env *env,
2188                                   struct dt_object *dt,
2189                                   const char *name, struct thandle *th)
2190 {
2191         struct dt_object        *next = dt_object_child(dt);
2192         struct lod_object       *lo = lod_dt_obj(dt);
2193         int                     rc;
2194         int                     i;
2195         ENTRY;
2196
2197         rc = lod_sub_object_xattr_del(env, next, name, th);
2198         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2199                 RETURN(rc);
2200
2201         if (lo->ldo_stripenr == 0)
2202                 RETURN(rc);
2203
2204         for (i = 0; i < lo->ldo_stripenr; i++) {
2205                 LASSERT(lo->ldo_stripe[i]);
2206
2207                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2208                                               th);
2209                 if (rc != 0)
2210                         break;
2211         }
2212
2213         RETURN(rc);
2214 }
2215
2216 /**
2217  * Set default striping on a directory.
2218  *
2219  * Sets specified striping on a directory object unless it matches the default
2220  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2221  * EA. This striping will be used when regular file is being created in this
2222  * directory.
2223  *
2224  * \param[in] env       execution environment
2225  * \param[in] dt        the striped object
2226  * \param[in] buf       buffer with the striping
2227  * \param[in] name      name of EA
2228  * \param[in] fl        xattr flag (see OSD API description)
2229  * \param[in] th        transaction handle
2230  *
2231  * \retval              0 on success
2232  * \retval              negative if failed
2233  */
2234 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2235                                     struct dt_object *dt,
2236                                     const struct lu_buf *buf,
2237                                     const char *name, int fl,
2238                                     struct thandle *th)
2239 {
2240         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2241         struct lod_object       *l = lod_dt_obj(dt);
2242         struct lov_user_md_v1   *lum;
2243         struct lov_user_md_v3   *v3 = NULL;
2244         const char              *pool_name = NULL;
2245         int                      rc;
2246         ENTRY;
2247
2248         /* If it is striped dir, we should clear the stripe cache for
2249          * slave stripe as well, but there are no effective way to
2250          * notify the LOD on the slave MDT, so we do not cache stripe
2251          * information for slave stripe for now. XXX*/
2252         lod_lov_stripe_cache_clear(l);
2253         LASSERT(buf != NULL && buf->lb_buf != NULL);
2254         lum = buf->lb_buf;
2255
2256         rc = lod_verify_striping(d, buf, false);
2257         if (rc)
2258                 RETURN(rc);
2259
2260         if (lum->lmm_magic == LOV_USER_MAGIC_V3) {
2261                 v3 = buf->lb_buf;
2262                 if (v3->lmm_pool_name[0] != '\0')
2263                         pool_name = v3->lmm_pool_name;
2264         }
2265
2266         /* if { size, offset, count } = { 0, -1, 0 } and no pool
2267          * (i.e. all default values specified) then delete default
2268          * striping from dir. */
2269         CDEBUG(D_OTHER,
2270                 "set default striping: sz %u # %u offset %d %s %s\n",
2271                 (unsigned)lum->lmm_stripe_size,
2272                 (unsigned)lum->lmm_stripe_count,
2273                 (int)lum->lmm_stripe_offset,
2274                 v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2275
2276         if (LOVEA_DELETE_VALUES(lum->lmm_stripe_size, lum->lmm_stripe_count,
2277                                 lum->lmm_stripe_offset, pool_name)) {
2278                 rc = lod_xattr_del_internal(env, dt, name, th);
2279                 if (rc == -ENODATA)
2280                         rc = 0;
2281         } else {
2282                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2283         }
2284
2285         RETURN(rc);
2286 }
2287
2288 /**
2289  * Set default striping on a directory object.
2290  *
2291  * Sets specified striping on a directory object unless it matches the default
2292  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2293  * EA. This striping will be used when a new directory is being created in the
2294  * directory.
2295  *
2296  * \param[in] env       execution environment
2297  * \param[in] dt        the striped object
2298  * \param[in] buf       buffer with the striping
2299  * \param[in] name      name of EA
2300  * \param[in] fl        xattr flag (see OSD API description)
2301  * \param[in] th        transaction handle
2302  *
2303  * \retval              0 on success
2304  * \retval              negative if failed
2305  */
2306 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2307                                             struct dt_object *dt,
2308                                             const struct lu_buf *buf,
2309                                             const char *name, int fl,
2310                                             struct thandle *th)
2311 {
2312         struct lod_object       *l = lod_dt_obj(dt);
2313         struct lmv_user_md_v1   *lum;
2314         int                      rc;
2315         ENTRY;
2316
2317         LASSERT(buf != NULL && buf->lb_buf != NULL);
2318         lum = buf->lb_buf;
2319
2320         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2321               le32_to_cpu(lum->lum_stripe_count),
2322               (int)le32_to_cpu(lum->lum_stripe_offset));
2323
2324         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2325                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2326                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2327                 rc = lod_xattr_del_internal(env, dt, name, th);
2328                 if (rc == -ENODATA)
2329                         rc = 0;
2330         } else {
2331                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2332                 if (rc != 0)
2333                         RETURN(rc);
2334         }
2335
2336         /* Update default stripe cache */
2337         if (l->ldo_dir_stripe == NULL) {
2338                 OBD_ALLOC_PTR(l->ldo_dir_stripe);
2339                 if (l->ldo_dir_stripe == NULL)
2340                         RETURN(-ENOMEM);
2341         }
2342
2343         l->ldo_dir_def_striping_cached = 0;
2344         RETURN(rc);
2345 }
2346
2347 /**
2348  * Turn directory into a striped directory.
2349  *
2350  * During replay the client sends the striping created before MDT
2351  * failure, then the layer above LOD sends this defined striping
2352  * using ->do_xattr_set(), so LOD uses this method to replay creation
2353  * of the stripes. Notice the original information for the striping
2354  * (#stripes, FIDs, etc) was transferred in declare path.
2355  *
2356  * \param[in] env       execution environment
2357  * \param[in] dt        the striped object
2358  * \param[in] buf       not used currently
2359  * \param[in] name      not used currently
2360  * \param[in] fl        xattr flag (see OSD API description)
2361  * \param[in] th        transaction handle
2362  *
2363  * \retval              0 on success
2364  * \retval              negative if failed
2365  */
2366 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2367                              const struct lu_buf *buf, const char *name,
2368                              int fl, struct thandle *th)
2369 {
2370         struct lod_object       *lo = lod_dt_obj(dt);
2371         struct lod_thread_info  *info = lod_env_info(env);
2372         struct lu_attr          *attr = &info->lti_attr;
2373         struct dt_object_format *dof = &info->lti_format;
2374         struct lu_buf           lmv_buf;
2375         struct lu_buf           slave_lmv_buf;
2376         struct lmv_mds_md_v1    *lmm;
2377         struct lmv_mds_md_v1    *slave_lmm = NULL;
2378         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2379         int                     i;
2380         int                     rc;
2381         ENTRY;
2382
2383         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2384                 RETURN(-ENOTDIR);
2385
2386         /* The stripes are supposed to be allocated in declare phase,
2387          * if there are no stripes being allocated, it will skip */
2388         if (lo->ldo_stripenr == 0)
2389                 RETURN(0);
2390
2391         rc = dt_attr_get(env, dt_object_child(dt), attr);
2392         if (rc != 0)
2393                 RETURN(rc);
2394
2395         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2396                          LA_MODE | LA_UID | LA_GID | LA_TYPE;
2397         dof->dof_type = DFT_DIR;
2398
2399         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2400         if (rc != 0)
2401                 RETURN(rc);
2402         lmm = lmv_buf.lb_buf;
2403
2404         OBD_ALLOC_PTR(slave_lmm);
2405         if (slave_lmm == NULL)
2406                 RETURN(-ENOMEM);
2407
2408         lod_prep_slave_lmv_md(slave_lmm, lmm);
2409         slave_lmv_buf.lb_buf = slave_lmm;
2410         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2411
2412         rec->rec_type = S_IFDIR;
2413         for (i = 0; i < lo->ldo_stripenr; i++) {
2414                 struct dt_object *dto;
2415                 char             *stripe_name = info->lti_key;
2416                 struct lu_name          *sname;
2417                 struct linkea_data       ldata          = { NULL };
2418                 struct lu_buf            linkea_buf;
2419
2420                 dto = lo->ldo_stripe[i];
2421
2422                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2423                 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2424                                            th);
2425                 if (rc != 0) {
2426                         dt_write_unlock(env, dto);
2427                         GOTO(out, rc);
2428                 }
2429
2430                 rc = lod_sub_object_ref_add(env, dto, th);
2431                 dt_write_unlock(env, dto);
2432                 if (rc != 0)
2433                         GOTO(out, rc);
2434
2435                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2436                 rc = lod_sub_object_index_insert(env, dto,
2437                                 (const struct dt_rec *)rec,
2438                                 (const struct dt_key *)dot, th, 0);
2439                 if (rc != 0)
2440                         GOTO(out, rc);
2441
2442                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2443                 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2444                                (const struct dt_key *)dotdot, th, 0);
2445                 if (rc != 0)
2446                         GOTO(out, rc);
2447
2448                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2449                     cfs_fail_val != i) {
2450                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2451                             cfs_fail_val == i)
2452                                 slave_lmm->lmv_master_mdt_index =
2453                                                         cpu_to_le32(i + 1);
2454                         else
2455                                 slave_lmm->lmv_master_mdt_index =
2456                                                         cpu_to_le32(i);
2457
2458                         rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2459                                                       XATTR_NAME_LMV, fl, th);
2460                         if (rc != 0)
2461                                 GOTO(out, rc);
2462                 }
2463
2464                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2465                     cfs_fail_val == i)
2466                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2467                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2468                 else
2469                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2470                                  PFID(lu_object_fid(&dto->do_lu)), i);
2471
2472                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2473                 rc = linkea_data_new(&ldata, &info->lti_linkea_buf);
2474                 if (rc != 0)
2475                         GOTO(out, rc);
2476
2477                 rc = linkea_add_buf(&ldata, sname, lu_object_fid(&dt->do_lu));
2478                 if (rc != 0)
2479                         GOTO(out, rc);
2480
2481                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2482                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2483                 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2484                                         XATTR_NAME_LINK, 0, th);
2485                 if (rc != 0)
2486                         GOTO(out, rc);
2487
2488                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2489                 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2490                                (const struct dt_rec *)rec,
2491                                (const struct dt_key *)stripe_name, th, 0);
2492                 if (rc != 0)
2493                         GOTO(out, rc);
2494
2495                 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2496                 if (rc != 0)
2497                         GOTO(out, rc);
2498         }
2499
2500         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2501                 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2502                                               &lmv_buf, XATTR_NAME_LMV, fl, th);
2503 out:
2504         if (slave_lmm != NULL)
2505                 OBD_FREE_PTR(slave_lmm);
2506
2507         RETURN(rc);
2508 }
2509
2510 /**
2511  * Helper function to declare/execute creation of a striped directory
2512  *
2513  * Called in declare/create object path, prepare striping for a directory
2514  * and prepare defaults data striping for the objects to be created in
2515  * that directory. Notice the function calls "declaration" or "execution"
2516  * methods depending on \a declare param. This is a consequence of the
2517  * current approach while we don't have natural distributed transactions:
2518  * we basically execute non-local updates in the declare phase. So, the
2519  * arguments for the both phases are the same and this is the reason for
2520  * this function to exist.
2521  *
2522  * \param[in] env       execution environment
2523  * \param[in] dt        object
2524  * \param[in] attr      attributes the stripes will be created with
2525  * \param[in] dof       format of stripes (see OSD API description)
2526  * \param[in] th        transaction handle
2527  * \param[in] declare   where to call "declare" or "execute" methods
2528  *
2529  * \retval              0 on success
2530  * \retval              negative if failed
2531  */
2532 static int lod_dir_striping_create_internal(const struct lu_env *env,
2533                                             struct dt_object *dt,
2534                                             struct lu_attr *attr,
2535                                             struct dt_object_format *dof,
2536                                             struct thandle *th,
2537                                             bool declare)
2538 {
2539         struct lod_thread_info  *info = lod_env_info(env);
2540         struct lod_object       *lo = lod_dt_obj(dt);
2541         int                     rc;
2542         ENTRY;
2543
2544         if (!LMVEA_DELETE_VALUES(lo->ldo_stripenr,
2545                                  lo->ldo_dir_stripe_offset)) {
2546                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2547                 int stripe_count = lo->ldo_stripenr;
2548
2549                 if (info->lti_ea_store_size < sizeof(*v1)) {
2550                         rc = lod_ea_store_resize(info, sizeof(*v1));
2551                         if (rc != 0)
2552                                 RETURN(rc);
2553                         v1 = info->lti_ea_store;
2554                 }
2555
2556                 memset(v1, 0, sizeof(*v1));
2557                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2558                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
2559                 v1->lum_stripe_offset =
2560                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
2561
2562                 info->lti_buf.lb_buf = v1;
2563                 info->lti_buf.lb_len = sizeof(*v1);
2564
2565                 if (declare)
2566                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
2567                                                        &info->lti_buf, dof, th);
2568                 else
2569                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
2570                                                XATTR_NAME_LMV, 0, th);
2571                 if (rc != 0)
2572                         RETURN(rc);
2573         }
2574
2575         /* Transfer default LMV striping from the parent */
2576         if (lo->ldo_dir_def_striping_set &&
2577             !LMVEA_DELETE_VALUES(lo->ldo_dir_def_stripenr,
2578                                  lo->ldo_dir_def_stripe_offset)) {
2579                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
2580                 int def_stripe_count = lo->ldo_dir_def_stripenr;
2581
2582                 if (info->lti_ea_store_size < sizeof(*v1)) {
2583                         rc = lod_ea_store_resize(info, sizeof(*v1));
2584                         if (rc != 0)
2585                                 RETURN(rc);
2586                         v1 = info->lti_ea_store;
2587                 }
2588
2589                 memset(v1, 0, sizeof(*v1));
2590                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
2591                 v1->lum_stripe_count = cpu_to_le32(def_stripe_count);
2592                 v1->lum_stripe_offset =
2593                                 cpu_to_le32(lo->ldo_dir_def_stripe_offset);
2594                 v1->lum_hash_type =
2595                                 cpu_to_le32(lo->ldo_dir_def_hash_type);
2596
2597                 info->lti_buf.lb_buf = v1;
2598                 info->lti_buf.lb_len = sizeof(*v1);
2599                 if (declare)
2600                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2601                                                        XATTR_NAME_DEFAULT_LMV,
2602                                                        0, th);
2603                 else
2604                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
2605                                                   &info->lti_buf,
2606                                                   XATTR_NAME_DEFAULT_LMV, 0,
2607                                                   th);
2608                 if (rc != 0)
2609                         RETURN(rc);
2610         }
2611
2612         /* Transfer default LOV striping from the parent */
2613         if (lo->ldo_def_striping_set &&
2614             !LOVEA_DELETE_VALUES(lo->ldo_def_stripe_size,
2615                                  lo->ldo_def_stripenr,
2616                                  lo->ldo_def_stripe_offset,
2617                                  lo->ldo_pool)) {
2618                 struct lov_user_md_v3 *v3 = info->lti_ea_store;
2619
2620                 if (info->lti_ea_store_size < sizeof(*v3)) {
2621                         rc = lod_ea_store_resize(info, sizeof(*v3));
2622                         if (rc != 0)
2623                                 RETURN(rc);
2624                         v3 = info->lti_ea_store;
2625                 }
2626
2627                 memset(v3, 0, sizeof(*v3));
2628                 v3->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V3);
2629                 v3->lmm_stripe_count = cpu_to_le16(lo->ldo_def_stripenr);
2630                 v3->lmm_stripe_offset = cpu_to_le16(lo->ldo_def_stripe_offset);
2631                 v3->lmm_stripe_size = cpu_to_le32(lo->ldo_def_stripe_size);
2632                 if (lo->ldo_pool != NULL)
2633                         strlcpy(v3->lmm_pool_name, lo->ldo_pool,
2634                                 sizeof(v3->lmm_pool_name));
2635
2636                 info->lti_buf.lb_buf = v3;
2637                 info->lti_buf.lb_len = sizeof(*v3);
2638
2639                 if (declare)
2640                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
2641                                                        XATTR_NAME_LOV, 0, th);
2642                 else
2643                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
2644                                                       XATTR_NAME_LOV, 0, th);
2645                 if (rc != 0)
2646                         RETURN(rc);
2647         }
2648
2649         RETURN(0);
2650 }
2651
2652 static int lod_declare_dir_striping_create(const struct lu_env *env,
2653                                            struct dt_object *dt,
2654                                            struct lu_attr *attr,
2655                                            struct dt_object_format *dof,
2656                                            struct thandle *th)
2657 {
2658         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
2659 }
2660
2661 static int lod_dir_striping_create(const struct lu_env *env,
2662                                    struct dt_object *dt,
2663                                    struct lu_attr *attr,
2664                                    struct dt_object_format *dof,
2665                                    struct thandle *th)
2666 {
2667         struct lod_object *lo = lod_dt_obj(dt);
2668         int rc;
2669
2670         rc = lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
2671         if (rc == 0)
2672                 lo->ldo_striping_cached = 1;
2673
2674         return rc;
2675 }
2676
2677 /**
2678  * Implementation of dt_object_operations::do_xattr_set.
2679  *
2680  * Sets specified extended attribute on the object. Three types of EAs are
2681  * special:
2682  *   LOV EA - stores striping for a regular file or default striping (when set
2683  *            on a directory)
2684  *   LMV EA - stores a marker for the striped directories
2685  *   DMV EA - stores default directory striping
2686  *
2687  * When striping is applied to a non-striped existing object (this is called
2688  * late striping), then LOD notices the caller wants to turn the object into a
2689  * striped one. The stripe objects are created and appropriate EA is set:
2690  * LOV EA storing all the stripes directly or LMV EA storing just a small header
2691  * with striping configuration.
2692  *
2693  * \see dt_object_operations::do_xattr_set() in the API description for details.
2694  */
2695 static int lod_xattr_set(const struct lu_env *env,
2696                          struct dt_object *dt, const struct lu_buf *buf,
2697                          const char *name, int fl, struct thandle *th)
2698 {
2699         struct dt_object        *next = dt_object_child(dt);
2700         int                      rc;
2701         ENTRY;
2702
2703         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2704             strcmp(name, XATTR_NAME_LMV) == 0) {
2705                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
2706
2707                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
2708                                                 LMV_HASH_FLAG_MIGRATION)
2709                         rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
2710                                                       th);
2711                 else
2712                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
2713
2714                 RETURN(rc);
2715         }
2716
2717         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2718             strcmp(name, XATTR_NAME_LOV) == 0) {
2719                 /* default LOVEA */
2720                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
2721                 RETURN(rc);
2722         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
2723                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2724                 /* default LMVEA */
2725                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
2726                                                       th);
2727                 RETURN(rc);
2728         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
2729                    !strcmp(name, XATTR_NAME_LOV)) {
2730                 /* in case of lov EA swap, just set it
2731                  * if not, it is a replay so check striping match what we
2732                  * already have during req replay, declare_xattr_set()
2733                  * defines striping, then create() does the work */
2734                 if (fl & LU_XATTR_REPLACE) {
2735                         /* free stripes, then update disk */
2736                         lod_object_free_striping(env, lod_dt_obj(dt));
2737
2738                         rc = lod_sub_object_xattr_set(env, next, buf, name,
2739                                                       fl, th);
2740                 } else if (dt_object_remote(dt)) {
2741                         /* This only happens during migration, see
2742                          * mdd_migrate_create(), in which Master MDT will
2743                          * create a remote target object, and only set
2744                          * (migrating) stripe EA on the remote object,
2745                          * and does not need creating each stripes. */
2746                         rc = lod_sub_object_xattr_set(env, next, buf, name,
2747                                                       fl, th);
2748                 } else {
2749                         rc = lod_striping_create(env, dt, NULL, NULL, th);
2750                 }
2751                 RETURN(rc);
2752         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2753                 rc = lod_object_replace_parent_fid(env, dt, th, false);
2754
2755                 RETURN(rc);
2756         }
2757
2758         /* then all other xattr */
2759         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2760
2761         RETURN(rc);
2762 }
2763
2764 /**
2765  * Implementation of dt_object_operations::do_declare_xattr_del.
2766  *
2767  * \see dt_object_operations::do_declare_xattr_del() in the API description
2768  * for details.
2769  */
2770 static int lod_declare_xattr_del(const struct lu_env *env,
2771                                  struct dt_object *dt, const char *name,
2772                                  struct thandle *th)
2773 {
2774         struct lod_object       *lo = lod_dt_obj(dt);
2775         int                     rc;
2776         int                     i;
2777         ENTRY;
2778
2779         rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
2780                                               name, th);
2781         if (rc != 0)
2782                 RETURN(rc);
2783
2784         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2785                 RETURN(0);
2786
2787         /* set xattr to each stripes, if needed */
2788         rc = lod_load_striping(env, lo);
2789         if (rc != 0)
2790                 RETURN(rc);
2791
2792         if (lo->ldo_stripenr == 0)
2793                 RETURN(0);
2794
2795         for (i = 0; i < lo->ldo_stripenr; i++) {
2796                 LASSERT(lo->ldo_stripe[i]);
2797                 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
2798                                                       name, th);
2799                 if (rc != 0)
2800                         break;
2801         }
2802
2803         RETURN(rc);
2804 }
2805
2806 /**
2807  * Implementation of dt_object_operations::do_xattr_del.
2808  *
2809  * If EA storing a regular striping is being deleted, then release
2810  * all the references to the stripe objects in core.
2811  *
2812  * \see dt_object_operations::do_xattr_del() in the API description for details.
2813  */
2814 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
2815                          const char *name, struct thandle *th)
2816 {
2817         struct dt_object        *next = dt_object_child(dt);
2818         struct lod_object       *lo = lod_dt_obj(dt);
2819         int                     rc;
2820         int                     i;
2821         ENTRY;
2822
2823         if (!strcmp(name, XATTR_NAME_LOV))
2824                 lod_object_free_striping(env, lod_dt_obj(dt));
2825
2826         rc = lod_sub_object_xattr_del(env, next, name, th);
2827         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2828                 RETURN(rc);
2829
2830         if (lo->ldo_stripenr == 0)
2831                 RETURN(0);
2832
2833         for (i = 0; i < lo->ldo_stripenr; i++) {
2834                 LASSERT(lo->ldo_stripe[i]);
2835
2836                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
2837                 if (rc != 0)
2838                         break;
2839         }
2840
2841         RETURN(rc);
2842 }
2843
2844 /**
2845  * Implementation of dt_object_operations::do_xattr_list.
2846  *
2847  * \see dt_object_operations::do_xattr_list() in the API description
2848  * for details.
2849  */
2850 static int lod_xattr_list(const struct lu_env *env,
2851                           struct dt_object *dt, const struct lu_buf *buf)
2852 {
2853         return dt_xattr_list(env, dt_object_child(dt), buf);
2854 }
2855
2856 /**
2857  * Initialize a pool the object belongs to.
2858  *
2859  * When a striped object is being created, striping configuration
2860  * may demand the stripes are allocated on a limited set of the
2861  * targets. These limited sets are known as "pools". So we copy
2862  * a pool name into the object and later actual creation methods
2863  * (like lod_object_create()) will use this information to allocate
2864  * the stripes properly.
2865  *
2866  * \param[in] o         object
2867  * \param[in] pool      pool name
2868  */
2869 int lod_object_set_pool(struct lod_object *o, char *pool)
2870 {
2871         int len;
2872
2873         if (o->ldo_pool) {
2874                 len = strlen(o->ldo_pool);
2875                 OBD_FREE(o->ldo_pool, len + 1);
2876                 o->ldo_pool = NULL;
2877         }
2878         if (pool) {
2879                 len = strlen(pool);
2880                 OBD_ALLOC(o->ldo_pool, len + 1);
2881                 if (o->ldo_pool == NULL)
2882                         return -ENOMEM;
2883                 strcpy(o->ldo_pool, pool);
2884         }
2885         return 0;
2886 }
2887
2888 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
2889 {
2890         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
2891 }
2892
2893
2894 /**
2895  * Cache default regular striping in the object.
2896  *
2897  * To improve performance of striped regular object creation we cache
2898  * default LOV striping (if it exists) in the parent directory object.
2899  *
2900  * \param[in] env               execution environment
2901  * \param[in] lp                object
2902  *
2903  * \retval              0 on success
2904  * \retval              negative if failed
2905  */
2906 static int lod_cache_parent_lov_striping(const struct lu_env *env,
2907                                          struct lod_object *lp)
2908 {
2909         struct lod_thread_info  *info = lod_env_info(env);
2910         struct lov_user_md_v1   *v1 = NULL;
2911         struct lov_user_md_v3   *v3 = NULL;
2912         int                      rc;
2913         ENTRY;
2914
2915         /* called from MDD without parent being write locked,
2916          * lock it here */
2917         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2918         rc = lod_get_lov_ea(env, lp);
2919         if (rc < 0)
2920                 GOTO(unlock, rc);
2921
2922         if (rc < (typeof(rc))sizeof(struct lov_user_md)) {
2923                 /* don't lookup for non-existing or invalid striping */
2924                 lp->ldo_def_striping_set = 0;
2925                 lp->ldo_def_striping_cached = 1;
2926                 lp->ldo_def_stripe_size = 0;
2927                 lp->ldo_def_stripenr = 0;
2928                 lp->ldo_def_stripe_offset = (typeof(v1->lmm_stripe_offset))(-1);
2929                 GOTO(unlock, rc = 0);
2930         }
2931
2932         rc = 0;
2933         v1 = info->lti_ea_store;
2934         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
2935                 lustre_swab_lov_user_md_v1(v1);
2936         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
2937                 v3 = (struct lov_user_md_v3 *)v1;
2938                 lustre_swab_lov_user_md_v3(v3);
2939         }
2940
2941         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1)
2942                 GOTO(unlock, rc = 0);
2943
2944         if (v1->lmm_pattern != LOV_PATTERN_RAID0 && v1->lmm_pattern != 0)
2945                 GOTO(unlock, rc = 0);
2946
2947         CDEBUG(D_INFO, DFID" stripe_count=%d stripe_size=%d stripe_offset=%d\n",
2948                PFID(lu_object_fid(&lp->ldo_obj.do_lu)),
2949                (int)v1->lmm_stripe_count,
2950                (int)v1->lmm_stripe_size, (int)v1->lmm_stripe_offset);
2951
2952         lp->ldo_def_stripenr = v1->lmm_stripe_count;
2953         lp->ldo_def_stripe_size = v1->lmm_stripe_size;
2954         lp->ldo_def_stripe_offset = v1->lmm_stripe_offset;
2955         lp->ldo_def_striping_cached = 1;
2956         lp->ldo_def_striping_set = 1;
2957         if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2958                 /* XXX: sanity check here */
2959                 v3 = (struct lov_user_md_v3 *) v1;
2960                 if (v3->lmm_pool_name[0])
2961                         lod_object_set_pool(lp, v3->lmm_pool_name);
2962         }
2963         EXIT;
2964 unlock:
2965         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
2966         return rc;
2967 }
2968
2969
2970 /**
2971  * Cache default directory striping in the object.
2972  *
2973  * To improve performance of striped directory creation we cache default
2974  * directory striping (if it exists) in the parent directory object.
2975  *
2976  * \param[in] env               execution environment
2977  * \param[in] lp                object
2978  *
2979  * \retval              0 on success
2980  * \retval              negative if failed
2981  */
2982 static int lod_cache_parent_lmv_striping(const struct lu_env *env,
2983                                          struct lod_object *lp)
2984 {
2985         struct lod_thread_info  *info = lod_env_info(env);
2986         struct lmv_user_md_v1   *v1 = NULL;
2987         int                      rc;
2988         ENTRY;
2989
2990         /* called from MDD without parent being write locked,
2991          * lock it here */
2992         dt_write_lock(env, dt_object_child(&lp->ldo_obj), 0);
2993         rc = lod_get_default_lmv_ea(env, lp);
2994         if (rc < 0)
2995                 GOTO(unlock, rc);
2996
2997         if (rc < (typeof(rc))sizeof(struct lmv_user_md)) {
2998                 /* don't lookup for non-existing or invalid striping */
2999                 lp->ldo_dir_def_striping_set = 0;
3000                 lp->ldo_dir_def_striping_cached = 1;
3001                 lp->ldo_dir_def_stripenr = 0;
3002                 lp->ldo_dir_def_stripe_offset =
3003                                         (typeof(v1->lum_stripe_offset))(-1);
3004                 lp->ldo_dir_def_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3005                 GOTO(unlock, rc = 0);
3006         }
3007
3008         rc = 0;
3009         v1 = info->lti_ea_store;
3010
3011         lp->ldo_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3012         lp->ldo_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3013         lp->ldo_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3014         lp->ldo_dir_def_striping_set = 1;
3015         lp->ldo_dir_def_striping_cached = 1;
3016
3017         EXIT;
3018 unlock:
3019         dt_write_unlock(env, dt_object_child(&lp->ldo_obj));
3020         return rc;
3021 }
3022
3023 /**
3024  * Cache default striping in the object.
3025  *
3026  * To improve performance of striped object creation we cache default striping
3027  * (if it exists) in the parent directory object. We always cache default
3028  * striping for the regular files (stored in LOV EA) and we cache default
3029  * striping for the directories if requested by \a child_mode (when a new
3030  * directory is being created).
3031  *
3032  * \param[in] env               execution environment
3033  * \param[in] lp                object
3034  * \param[in] child_mode        new object's mode
3035  *
3036  * \retval              0 on success
3037  * \retval              negative if failed
3038  */
3039 static int lod_cache_parent_striping(const struct lu_env *env,
3040                                      struct lod_object *lp,
3041                                      umode_t child_mode)
3042 {
3043         int rc = 0;
3044         ENTRY;
3045
3046         if (!lp->ldo_def_striping_cached) {
3047                 /* we haven't tried to get default striping for
3048                  * the directory yet, let's cache it in the object */
3049                 rc = lod_cache_parent_lov_striping(env, lp);
3050                 if (rc != 0)
3051                         RETURN(rc);
3052         }
3053
3054         /* If the parent is on the remote MDT, we should always
3055          * try to refresh the default stripeEA cache, because we
3056          * do not cache default striping information for remote
3057          * object. */
3058         if (S_ISDIR(child_mode) && (!lp->ldo_dir_def_striping_cached ||
3059                                     dt_object_remote(&lp->ldo_obj)))
3060                 rc = lod_cache_parent_lmv_striping(env, lp);
3061
3062         RETURN(rc);
3063 }
3064
3065 /**
3066  * Implementation of dt_object_operations::do_ah_init.
3067  *
3068  * This method is used to make a decision on the striping configuration for the
3069  * object being created. It can be taken from the \a parent object if it exists,
3070  * or filesystem's default. The resulting configuration (number of stripes,
3071  * stripe size/offset, pool name, etc) is stored in the object itself and will
3072  * be used by the methods like ->doo_declare_create().
3073  *
3074  * \see dt_object_operations::do_ah_init() in the API description for details.
3075  */
3076 static void lod_ah_init(const struct lu_env *env,
3077                         struct dt_allocation_hint *ah,
3078                         struct dt_object *parent,
3079                         struct dt_object *child,
3080                         umode_t child_mode)
3081 {
3082         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3083         struct dt_object  *nextp = NULL;
3084         struct dt_object  *nextc;
3085         struct lod_object *lp = NULL;
3086         struct lod_object *lc;
3087         struct lov_desc   *desc;
3088         int               rc;
3089         ENTRY;
3090
3091         LASSERT(child);
3092
3093         if (likely(parent)) {
3094                 nextp = dt_object_child(parent);
3095                 lp = lod_dt_obj(parent);
3096                 rc = lod_load_striping(env, lp);
3097                 if (rc != 0)
3098                         return;
3099         }
3100
3101         nextc = dt_object_child(child);
3102         lc = lod_dt_obj(child);
3103
3104         LASSERT(lc->ldo_stripenr == 0);
3105         LASSERT(lc->ldo_stripe == NULL);
3106
3107         if (!dt_object_exists(nextc))
3108                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3109
3110         if (S_ISDIR(child_mode)) {
3111                 if (lc->ldo_dir_stripe == NULL) {
3112                         OBD_ALLOC_PTR(lc->ldo_dir_stripe);
3113                         if (lc->ldo_dir_stripe == NULL)
3114                                 return;
3115                 }
3116
3117                 LASSERT(lp != NULL);
3118                 if (lp->ldo_dir_stripe == NULL) {
3119                         OBD_ALLOC_PTR(lp->ldo_dir_stripe);
3120                         if (lp->ldo_dir_stripe == NULL)
3121                                 return;
3122                 }
3123
3124                 rc = lod_cache_parent_striping(env, lp, child_mode);
3125                 if (rc != 0)
3126                         return;
3127
3128                 /* transfer defaults to new directory */
3129                 if (lp->ldo_def_striping_set) {
3130                         if (lp->ldo_pool)
3131                                 lod_object_set_pool(lc, lp->ldo_pool);
3132                         lc->ldo_def_stripenr = lp->ldo_def_stripenr;
3133                         lc->ldo_def_stripe_size = lp->ldo_def_stripe_size;
3134                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3135                         lc->ldo_def_striping_set = 1;
3136                         lc->ldo_def_striping_cached = 1;
3137                         CDEBUG(D_OTHER, "inherite EA sz:%d off:%d nr:%d\n",
3138                                (int)lc->ldo_def_stripe_size,
3139                                (int)lc->ldo_def_stripe_offset,
3140                                (int)lc->ldo_def_stripenr);
3141                 }
3142
3143                 /* transfer dir defaults to new directory */
3144                 if (lp->ldo_dir_def_striping_set) {
3145                         lc->ldo_dir_def_stripenr = lp->ldo_dir_def_stripenr;
3146                         lc->ldo_dir_def_stripe_offset =
3147                                                   lp->ldo_dir_def_stripe_offset;
3148                         lc->ldo_dir_def_hash_type =
3149                                                   lp->ldo_dir_def_hash_type;
3150                         lc->ldo_dir_def_striping_set = 1;
3151                         lc->ldo_dir_def_striping_cached = 1;
3152                         CDEBUG(D_INFO, "inherit default EA nr:%d off:%d t%u\n",
3153                                (int)lc->ldo_dir_def_stripenr,
3154                                (int)lc->ldo_dir_def_stripe_offset,
3155                                lc->ldo_dir_def_hash_type);
3156                 }
3157
3158                 /* It should always honour the specified stripes */
3159                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0) {
3160                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3161
3162                         rc = lod_verify_md_striping(d, lum1);
3163                         if (rc == 0 &&
3164                                 le32_to_cpu(lum1->lum_stripe_count) > 1) {
3165                                 lc->ldo_stripenr =
3166                                         le32_to_cpu(lum1->lum_stripe_count);
3167                                 lc->ldo_dir_stripe_offset =
3168                                         le32_to_cpu(lum1->lum_stripe_offset);
3169                                 lc->ldo_dir_hash_type =
3170                                         le32_to_cpu(lum1->lum_hash_type);
3171                                 CDEBUG(D_INFO, "set stripe EA nr:%hu off:%d\n",
3172                                        lc->ldo_stripenr,
3173                                        (int)lc->ldo_dir_stripe_offset);
3174                         }
3175                 /* then check whether there is default stripes from parent */
3176                 } else if (lp->ldo_dir_def_striping_set) {
3177                         /* If there are default dir stripe from parent */
3178                         lc->ldo_stripenr = lp->ldo_dir_def_stripenr;
3179                         lc->ldo_dir_stripe_offset =
3180                                         lp->ldo_dir_def_stripe_offset;
3181                         lc->ldo_dir_hash_type =
3182                                         lp->ldo_dir_def_hash_type;
3183                         CDEBUG(D_INFO, "inherit EA nr:%hu off:%d\n",
3184                                lc->ldo_stripenr,
3185                                (int)lc->ldo_dir_stripe_offset);
3186                 } else {
3187                         /* set default stripe for this directory */
3188                         lc->ldo_stripenr = 0;
3189                         lc->ldo_dir_stripe_offset = -1;
3190                 }
3191
3192                 /* shrink the stripe_count to the avaible MDT count */
3193                 if (lc->ldo_stripenr > d->lod_remote_mdt_count + 1 &&
3194                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3195                         lc->ldo_stripenr = d->lod_remote_mdt_count + 1;
3196
3197                 /* Directory will be striped only if stripe_count > 1, if
3198                  * stripe_count == 1, let's reset stripenr = 0 to avoid
3199                  * create single master stripe and also help to unify the
3200                  * stripe handling of directories and files */
3201                 if (lc->ldo_stripenr == 1)
3202                         lc->ldo_stripenr = 0;
3203
3204                 CDEBUG(D_INFO, "final striping count:%hu, offset:%d\n",
3205                        lc->ldo_stripenr, (int)lc->ldo_dir_stripe_offset);
3206
3207                 goto out;
3208         }
3209
3210         /*
3211          * if object is going to be striped over OSTs, transfer default
3212          * striping information to the child, so that we can use it
3213          * during declaration and creation
3214          */
3215         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3216                                         lu_object_fid(&child->do_lu)))
3217                 goto out;
3218         /*
3219          * try from the parent
3220          */
3221         if (likely(parent)) {
3222                 lod_cache_parent_striping(env, lp, child_mode);
3223
3224                 lc->ldo_def_stripe_offset = LOV_OFFSET_DEFAULT;
3225
3226                 if (lp->ldo_def_striping_set) {
3227                         if (lp->ldo_pool)
3228                                 lod_object_set_pool(lc, lp->ldo_pool);
3229                         lc->ldo_stripenr = lp->ldo_def_stripenr;
3230                         lc->ldo_stripe_size = lp->ldo_def_stripe_size;
3231                         lc->ldo_def_stripe_offset = lp->ldo_def_stripe_offset;
3232                         CDEBUG(D_OTHER, "striping from parent: #%d, sz %d %s\n",
3233                                lc->ldo_stripenr, lc->ldo_stripe_size,
3234                                lp->ldo_pool ? lp->ldo_pool : "");
3235                 }
3236         }
3237
3238         /*
3239          * if the parent doesn't provide with specific pattern, grab fs-wide one
3240          */
3241         desc = &d->lod_desc;
3242         if (lc->ldo_stripenr == 0)
3243                 lc->ldo_stripenr = desc->ld_default_stripe_count;
3244         if (lc->ldo_stripe_size == 0)
3245                 lc->ldo_stripe_size = desc->ld_default_stripe_size;
3246         CDEBUG(D_OTHER, "final striping: # %d stripes, sz %d from %s\n",
3247                lc->ldo_stripenr, lc->ldo_stripe_size,
3248                lc->ldo_pool ? lc->ldo_pool : "");
3249
3250 out:
3251         /* we do not cache stripe information for slave stripe, see
3252          * lod_xattr_set_lov_on_dir */
3253         if (lp != NULL && lp->ldo_dir_slave_stripe)
3254                 lod_lov_stripe_cache_clear(lp);
3255
3256         EXIT;
3257 }
3258
3259 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
3260 /**
3261  * Size initialization on late striping.
3262  *
3263  * Propagate the size of a truncated object to a deferred striping.
3264  * This function handles a special case when truncate was done on a
3265  * non-striped object and now while the striping is being created
3266  * we can't lose that size, so we have to propagate it to the stripes
3267  * being created.
3268  *
3269  * \param[in] env       execution environment
3270  * \param[in] dt        object
3271  * \param[in] th        transaction handle
3272  *
3273  * \retval              0 on success
3274  * \retval              negative if failed
3275  */
3276 static int lod_declare_init_size(const struct lu_env *env,
3277                                  struct dt_object *dt, struct thandle *th)
3278 {
3279         struct dt_object   *next = dt_object_child(dt);
3280         struct lod_object  *lo = lod_dt_obj(dt);
3281         struct lu_attr     *attr = &lod_env_info(env)->lti_attr;
3282         uint64_t            size, offs;
3283         int                 rc, stripe;
3284         ENTRY;
3285
3286         /* XXX: we support the simplest (RAID0) striping so far */
3287         LASSERT(lo->ldo_stripe || lo->ldo_stripenr == 0);
3288         LASSERT(lo->ldo_stripe_size > 0);
3289
3290         if (lo->ldo_stripenr == 0)
3291                 RETURN(0);
3292
3293         rc = dt_attr_get(env, next, attr);
3294         LASSERT(attr->la_valid & LA_SIZE);
3295         if (rc)
3296                 RETURN(rc);
3297
3298         size = attr->la_size;
3299         if (size == 0)
3300                 RETURN(0);
3301
3302         /* ll_do_div64(a, b) returns a % b, and a = a / b */
3303         ll_do_div64(size, (__u64) lo->ldo_stripe_size);
3304         stripe = ll_do_div64(size, (__u64) lo->ldo_stripenr);
3305
3306         size = size * lo->ldo_stripe_size;
3307         offs = attr->la_size;
3308         size += ll_do_div64(offs, lo->ldo_stripe_size);
3309
3310         attr->la_valid = LA_SIZE;
3311         attr->la_size = size;
3312
3313         rc = lod_sub_object_declare_attr_set(env, lo->ldo_stripe[stripe], attr,
3314                                              th);
3315
3316         RETURN(rc);
3317 }
3318
3319 /**
3320  * Declare creation of striped object.
3321  *
3322  * The function declares creation stripes for a regular object. The function
3323  * also declares whether the stripes will be created with non-zero size if
3324  * previously size was set non-zero on the master object. If object \a dt is
3325  * not local, then only fully defined striping can be applied in \a lovea.
3326  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
3327  * for the details.
3328  *
3329  * \param[in] env       execution environment
3330  * \param[in] dt        object
3331  * \param[in] attr      attributes the stripes will be created with
3332  * \param[in] lovea     a buffer containing striping description
3333  * \param[in] th        transaction handle
3334  *
3335  * \retval              0 on success
3336  * \retval              negative if failed
3337  */
3338 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
3339                                struct lu_attr *attr,
3340                                const struct lu_buf *lovea, struct thandle *th)
3341 {
3342         struct lod_thread_info  *info = lod_env_info(env);
3343         struct dt_object        *next = dt_object_child(dt);
3344         struct lod_object       *lo = lod_dt_obj(dt);
3345         int                      rc;
3346         ENTRY;
3347
3348         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO)) {
3349                 /* failed to create striping, let's reset
3350                  * config so that others don't get confused */
3351                 lod_object_free_striping(env, lo);
3352                 GOTO(out, rc = -ENOMEM);
3353         }
3354
3355         if (!dt_object_remote(next)) {
3356                 /* choose OST and generate appropriate objects */
3357                 rc = lod_qos_prep_create(env, lo, attr, lovea, th);
3358                 if (rc) {
3359                         /* failed to create striping, let's reset
3360                          * config so that others don't get confused */
3361                         lod_object_free_striping(env, lo);
3362                         GOTO(out, rc);
3363                 }
3364
3365                 /*
3366                  * declare storage for striping data
3367                  */
3368                 info->lti_buf.lb_len = lov_mds_md_size(lo->ldo_stripenr,
3369                                 lo->ldo_pool ?  LOV_MAGIC_V3 : LOV_MAGIC_V1);
3370         } else {
3371                 /* LOD can not choose OST objects for remote objects, i.e.
3372                  * stripes must be ready before that. Right now, it can only
3373                  * happen during migrate, i.e. migrate process needs to create
3374                  * remote regular file (mdd_migrate_create), then the migrate
3375                  * process will provide stripeEA. */
3376                 LASSERT(lovea != NULL);
3377                 info->lti_buf = *lovea;
3378         }
3379
3380         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
3381                                               XATTR_NAME_LOV, 0, th);
3382         if (rc)
3383                 GOTO(out, rc);
3384
3385         /*
3386          * if striping is created with local object's size > 0,
3387          * we have to propagate this size to specific object
3388          * the case is possible only when local object was created previously
3389          */
3390         if (dt_object_exists(next))
3391                 rc = lod_declare_init_size(env, dt, th);
3392
3393 out:
3394         RETURN(rc);
3395 }
3396
3397 /**
3398  * Implementation of dt_object_operations::do_declare_create.
3399  *
3400  * The method declares creation of a new object. If the object will be striped,
3401  * then helper functions are called to find FIDs for the stripes, declare
3402  * creation of the stripes and declare initialization of the striping
3403  * information to be stored in the master object.
3404  *
3405  * \see dt_object_operations::do_declare_create() in the API description
3406  * for details.
3407  */
3408 static int lod_declare_object_create(const struct lu_env *env,
3409                                      struct dt_object *dt,
3410                                      struct lu_attr *attr,
3411                                      struct dt_allocation_hint *hint,
3412                                      struct dt_object_format *dof,
3413                                      struct thandle *th)
3414 {
3415         struct dt_object   *next = dt_object_child(dt);
3416         struct lod_object  *lo = lod_dt_obj(dt);
3417         int                 rc;
3418         ENTRY;
3419
3420         LASSERT(dof);
3421         LASSERT(attr);
3422         LASSERT(th);
3423
3424         /*
3425          * first of all, we declare creation of local object
3426          */
3427         rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
3428         if (rc != 0)
3429                 GOTO(out, rc);
3430
3431         if (dof->dof_type == DFT_SYM)
3432                 dt->do_body_ops = &lod_body_lnk_ops;
3433         else if (dof->dof_type == DFT_REGULAR)
3434                 dt->do_body_ops = &lod_body_ops;
3435
3436         /*
3437          * it's lod_ah_init() that has decided the object will be striped
3438          */
3439         if (dof->dof_type == DFT_REGULAR) {
3440                 /* callers don't want stripes */
3441                 /* XXX: all tricky interactions with ->ah_make_hint() decided
3442                  * to use striping, then ->declare_create() behaving differently
3443                  * should be cleaned */
3444                 if (dof->u.dof_reg.striped == 0)
3445                         lo->ldo_stripenr = 0;
3446                 if (lo->ldo_stripenr > 0)
3447                         rc = lod_declare_striped_object(env, dt, attr,
3448                                                         NULL, th);
3449         } else if (dof->dof_type == DFT_DIR) {
3450                 struct seq_server_site *ss;
3451
3452                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
3453
3454                 /* If the parent has default stripeEA, and client
3455                  * did not find it before sending create request,
3456                  * then MDT will return -EREMOTE, and client will
3457                  * retrieve the default stripeEA and re-create the
3458                  * sub directory.
3459                  *
3460                  * Note: if dah_eadata != NULL, it means creating the
3461                  * striped directory with specified stripeEA, then it
3462                  * should ignore the default stripeEA */
3463                 if (hint != NULL && hint->dah_eadata == NULL) {
3464                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
3465                                 GOTO(out, rc = -EREMOTE);
3466
3467                         if (lo->ldo_dir_stripe_offset == -1) {
3468                                 /* child and parent should be in the same MDT */
3469                                 if (hint->dah_parent != NULL &&
3470                                     dt_object_remote(hint->dah_parent))
3471                                         GOTO(out, rc = -EREMOTE);
3472                         } else if (lo->ldo_dir_stripe_offset !=
3473                                    ss->ss_node_id) {
3474                                 struct lod_device *lod;
3475                                 struct lod_tgt_descs *ltd;
3476                                 struct lod_tgt_desc *tgt = NULL;
3477                                 bool found_mdt = false;
3478                                 int i;
3479
3480                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3481                                 ltd = &lod->lod_mdt_descs;
3482                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
3483                                         tgt = LTD_TGT(ltd, i);
3484                                         if (tgt->ltd_index ==
3485                                                 lo->ldo_dir_stripe_offset) {
3486                                                 found_mdt = true;
3487                                                 break;
3488                                         }
3489                                 }
3490
3491                                 /* If the MDT indicated by stripe_offset can be
3492                                  * found, then tell client to resend the create
3493                                  * request to the correct MDT, otherwise return
3494                                  * error to client */
3495                                 if (found_mdt)
3496                                         GOTO(out, rc = -EREMOTE);
3497                                 else
3498                                         GOTO(out, rc = -EINVAL);
3499                         }
3500                 }
3501
3502                 /* Orphan object (like migrating object) does not have
3503                  * lod_dir_stripe, see lod_ah_init */
3504                 if (lo->ldo_dir_stripe != NULL)
3505                         rc = lod_declare_dir_striping_create(env, dt, attr,
3506                                                              dof, th);
3507         }
3508 out:
3509         RETURN(rc);
3510 }
3511
3512 /**
3513  * Creation of a striped regular object.
3514  *
3515  * The function is called to create the stripe objects for a regular
3516  * striped file. This can happen at the initial object creation or
3517  * when the caller asks LOD to do so using ->do_xattr_set() method
3518  * (so called late striping). Notice all the information are already
3519  * prepared in the form of the list of objects (ldo_stripe field).
3520  * This is done during declare phase.
3521  *
3522  * \param[in] env       execution environment
3523  * \param[in] dt        object
3524  * \param[in] attr      attributes the stripes will be created with
3525  * \param[in] dof       format of stripes (see OSD API description)
3526  * \param[in] th        transaction handle
3527  *
3528  * \retval              0 on success
3529  * \retval              negative if failed
3530  */
3531 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
3532                         struct lu_attr *attr, struct dt_object_format *dof,
3533                         struct thandle *th)
3534 {
3535         struct lod_object *lo = lod_dt_obj(dt);
3536         int                rc = 0, i;
3537         ENTRY;
3538
3539         LASSERT(lo->ldo_striping_cached == 0);
3540
3541         /* create all underlying objects */
3542         for (i = 0; i < lo->ldo_stripenr; i++) {
3543                 LASSERT(lo->ldo_stripe[i]);
3544                 rc = lod_sub_object_create(env, lo->ldo_stripe[i], attr, NULL,
3545                                            dof, th);
3546                 if (rc)
3547                         break;
3548         }
3549
3550         if (rc == 0) {
3551                 rc = lod_generate_and_set_lovea(env, lo, th);
3552                 if (rc == 0)
3553                         lo->ldo_striping_cached = 1;
3554         }
3555
3556         RETURN(rc);
3557 }
3558
3559 /**
3560  * Implementation of dt_object_operations::do_create.
3561  *
3562  * If any of preceeding methods (like ->do_declare_create(),
3563  * ->do_ah_init(), etc) chose to create a striped object,
3564  * then this method will create the master and the stripes.
3565  *
3566  * \see dt_object_operations::do_create() in the API description for details.
3567  */
3568 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
3569                              struct lu_attr *attr,
3570                              struct dt_allocation_hint *hint,
3571                              struct dt_object_format *dof, struct thandle *th)
3572 {
3573         struct lod_object  *lo = lod_dt_obj(dt);
3574         int                 rc;
3575         ENTRY;
3576
3577         /* create local object */
3578         rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
3579                                    th);
3580         if (rc != 0)
3581                 RETURN(rc);
3582
3583         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3584             lo->ldo_stripe && dof->u.dof_reg.striped != 0)
3585                 rc = lod_striping_create(env, dt, attr, dof, th);
3586
3587         RETURN(rc);
3588 }
3589
3590 /**
3591  * Implementation of dt_object_operations::do_declare_destroy.
3592  *
3593  * If the object is a striped directory, then the function declares reference
3594  * removal from the master object (this is an index) to the stripes and declares
3595  * destroy of all the stripes. In all the cases, it declares an intention to
3596  * destroy the object itself.
3597  *
3598  * \see dt_object_operations::do_declare_destroy() in the API description
3599  * for details.
3600  */
3601 static int lod_declare_object_destroy(const struct lu_env *env,
3602                                       struct dt_object *dt,
3603                                       struct thandle *th)
3604 {
3605         struct dt_object   *next = dt_object_child(dt);
3606         struct lod_object  *lo = lod_dt_obj(dt);
3607         struct lod_thread_info *info = lod_env_info(env);
3608         char               *stripe_name = info->lti_key;
3609         int                 rc, i;
3610         ENTRY;
3611
3612         /*
3613          * load striping information, notice we don't do this when object
3614          * is being initialized as we don't need this information till
3615          * few specific cases like destroy, chown
3616          */
3617         rc = lod_load_striping(env, lo);
3618         if (rc)
3619                 RETURN(rc);
3620
3621         /* declare destroy for all underlying objects */
3622         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3623                 rc = next->do_ops->do_index_try(env, next,
3624                                                 &dt_directory_features);
3625                 if (rc != 0)
3626                         RETURN(rc);
3627
3628                 for (i = 0; i < lo->ldo_stripenr; i++) {
3629                         rc = lod_sub_object_declare_ref_del(env, next, th);
3630                         if (rc != 0)
3631                                 RETURN(rc);
3632
3633                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3634                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3635                                 i);
3636                         rc = lod_sub_object_declare_delete(env, next,
3637                                         (const struct dt_key *)stripe_name, th);
3638                         if (rc != 0)
3639                                 RETURN(rc);
3640                 }
3641         }
3642
3643         /*
3644          * we declare destroy for the local object
3645          */
3646         rc = lod_sub_object_declare_destroy(env, next, th);
3647         if (rc)
3648                 RETURN(rc);
3649
3650         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3651                 RETURN(0);
3652
3653         /* declare destroy all striped objects */
3654         for (i = 0; i < lo->ldo_stripenr; i++) {
3655                 if (lo->ldo_stripe[i] == NULL)
3656                         continue;
3657
3658                 if (S_ISDIR(dt->do_lu.lo_header->loh_attr))
3659                         rc = lod_sub_object_declare_ref_del(env,
3660                                         lo->ldo_stripe[i], th);
3661
3662                 rc = lod_sub_object_declare_destroy(env, lo->ldo_stripe[i],
3663                                         th);
3664                 if (rc != 0)
3665                         break;
3666         }
3667
3668         RETURN(rc);
3669 }
3670
3671 /**
3672  * Implementation of dt_object_operations::do_destroy.
3673  *
3674  * If the object is a striped directory, then the function removes references
3675  * from the master object (this is an index) to the stripes and destroys all
3676  * the stripes. In all the cases, the function destroys the object itself.
3677  *
3678  * \see dt_object_operations::do_destroy() in the API description for details.
3679  */
3680 static int lod_object_destroy(const struct lu_env *env,
3681                 struct dt_object *dt, struct thandle *th)
3682 {
3683         struct dt_object  *next = dt_object_child(dt);
3684         struct lod_object *lo = lod_dt_obj(dt);
3685         struct lod_thread_info *info = lod_env_info(env);
3686         char               *stripe_name = info->lti_key;
3687         unsigned int       i;
3688         int                rc;
3689         ENTRY;
3690
3691         /* destroy sub-stripe of master object */
3692         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3693                 rc = next->do_ops->do_index_try(env, next,
3694                                                 &dt_directory_features);
3695                 if (rc != 0)
3696                         RETURN(rc);
3697
3698                 for (i = 0; i < lo->ldo_stripenr; i++) {
3699                         rc = lod_sub_object_ref_del(env, next, th);
3700                         if (rc != 0)
3701                                 RETURN(rc);
3702
3703                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
3704                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
3705                                 i);
3706
3707                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
3708                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
3709                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
3710
3711                         rc = lod_sub_object_delete(env, next,
3712                                        (const struct dt_key *)stripe_name, th);
3713                         if (rc != 0)
3714                                 RETURN(rc);
3715                 }
3716         }
3717
3718         rc = lod_sub_object_destroy(env, next, th);
3719         if (rc != 0)
3720                 RETURN(rc);
3721
3722         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ))
3723                 RETURN(0);
3724
3725         /* destroy all striped objects */
3726         for (i = 0; i < lo->ldo_stripenr; i++) {
3727                 if (likely(lo->ldo_stripe[i] != NULL) &&
3728                     (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
3729                      i == cfs_fail_val)) {
3730                         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
3731                                 dt_write_lock(env, lo->ldo_stripe[i],
3732                                               MOR_TGT_CHILD);
3733                                 rc = lod_sub_object_ref_del(env,
3734                                                 lo->ldo_stripe[i], th);
3735                                 dt_write_unlock(env, lo->ldo_stripe[i]);
3736                                 if (rc != 0)
3737                                         break;
3738                         }
3739
3740                         rc = lod_sub_object_destroy(env, lo->ldo_stripe[i], th);
3741                         if (rc != 0)
3742                                 break;
3743                 }
3744         }
3745
3746         RETURN(rc);
3747 }
3748
3749 /**
3750  * Implementation of dt_object_operations::do_declare_ref_add.
3751  *
3752  * \see dt_object_operations::do_declare_ref_add() in the API description
3753  * for details.
3754  */
3755 static int lod_declare_ref_add(const struct lu_env *env,
3756                                struct dt_object *dt, struct thandle *th)
3757 {
3758         return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
3759 }
3760
3761 /**
3762  * Implementation of dt_object_operations::do_ref_add.
3763  *
3764  * \see dt_object_operations::do_ref_add() in the API description for details.
3765  */
3766 static int lod_ref_add(const struct lu_env *env,
3767                        struct dt_object *dt, struct thandle *th)
3768 {
3769         return lod_sub_object_ref_add(env, dt_object_child(dt), th);
3770 }
3771
3772 /**
3773  * Implementation of dt_object_operations::do_declare_ref_del.
3774  *
3775  * \see dt_object_operations::do_declare_ref_del() in the API description
3776  * for details.
3777  */
3778 static int lod_declare_ref_del(const struct lu_env *env,
3779                                struct dt_object *dt, struct thandle *th)
3780 {
3781         return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
3782 }
3783
3784 /**
3785  * Implementation of dt_object_operations::do_ref_del
3786  *
3787  * \see dt_object_operations::do_ref_del() in the API description for details.
3788  */
3789 static int lod_ref_del(const struct lu_env *env,
3790                        struct dt_object *dt, struct thandle *th)
3791 {
3792         return lod_sub_object_ref_del(env, dt_object_child(dt), th);
3793 }
3794
3795 /**
3796  * Implementation of dt_object_operations::do_object_sync.
3797  *
3798  * \see dt_object_operations::do_object_sync() in the API description
3799  * for details.
3800  */
3801 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
3802                            __u64 start, __u64 end)
3803 {
3804         return dt_object_sync(env, dt_object_child(dt), start, end);
3805 }
3806
3807 /**
3808  * Release LDLM locks on the stripes of a striped directory.
3809  *
3810  * Iterates over all the locks taken on the stripe objects and
3811  * release them using ->do_object_unlock() method.
3812  *
3813  * \param[in] env       execution environment
3814  * \param[in] dt        striped object
3815  * \param[in] einfo     lock description
3816  * \param[in] policy    data describing requested lock
3817  *
3818  * \retval              0 on success
3819  * \retval              negative if failed
3820  */
3821 static int lod_object_unlock_internal(const struct lu_env *env,
3822                                       struct dt_object *dt,
3823                                       struct ldlm_enqueue_info *einfo,
3824                                       union ldlm_policy_data *policy)
3825 {
3826         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
3827         int                     rc = 0;
3828         int                     i;
3829         ENTRY;
3830
3831         if (slave_locks == NULL)
3832                 RETURN(0);
3833
3834         for (i = 1; i < slave_locks->count; i++) {
3835                 if (lustre_handle_is_used(&slave_locks->handles[i]))
3836                         ldlm_lock_decref(&slave_locks->handles[i],
3837                                          einfo->ei_mode);
3838         }
3839
3840         RETURN(rc);
3841 }
3842
3843 /**
3844  * Implementation of dt_object_operations::do_object_unlock.
3845  *
3846  * Used to release LDLM lock(s).
3847  *
3848  * \see dt_object_operations::do_object_unlock() in the API description
3849  * for details.
3850  */
3851 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
3852                              struct ldlm_enqueue_info *einfo,
3853                              union ldlm_policy_data *policy)
3854 {
3855         struct lod_object *lo = lod_dt_obj(dt);
3856         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
3857         int slave_locks_size;
3858         int i;
3859         ENTRY;
3860
3861         if (slave_locks == NULL)
3862                 RETURN(0);
3863
3864         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
3865         LASSERT(lo->ldo_stripenr > 1);
3866         /* Note: for remote lock for single stripe dir, MDT will cancel
3867          * the lock by lockh directly */
3868         LASSERT(!dt_object_remote(dt_object_child(dt)));
3869
3870         /* locks were unlocked in MDT layer */
3871         for (i = 1; i < slave_locks->count; i++)
3872                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
3873
3874         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
3875                            sizeof(slave_locks->handles[0]);
3876         OBD_FREE(slave_locks, slave_locks_size);
3877         einfo->ei_cbdata = NULL;
3878
3879         RETURN(0);
3880 }
3881
3882 /**
3883  * Implementation of dt_object_operations::do_object_lock.
3884  *
3885  * Used to get LDLM lock on the non-striped and striped objects.
3886  *
3887  * \see dt_object_operations::do_object_lock() in the API description
3888  * for details.
3889  */
3890 static int lod_object_lock(const struct lu_env *env,
3891                            struct dt_object *dt,
3892                            struct lustre_handle *lh,
3893                            struct ldlm_enqueue_info *einfo,
3894                            union ldlm_policy_data *policy)
3895 {
3896         struct lod_object       *lo = lod_dt_obj(dt);
3897         int                     rc = 0;
3898         int                     i;
3899         int                     slave_locks_size;
3900         struct lustre_handle_array *slave_locks = NULL;
3901         ENTRY;
3902
3903         /* remote object lock */
3904         if (!einfo->ei_enq_slave) {
3905                 LASSERT(dt_object_remote(dt));
3906                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
3907                                       policy);
3908         }
3909
3910         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3911                 RETURN(-ENOTDIR);
3912
3913         rc = lod_load_striping(env, lo);
3914         if (rc != 0)
3915                 RETURN(rc);
3916
3917         /* No stripes */
3918         if (lo->ldo_stripenr <= 1)
3919                 RETURN(0);
3920
3921         slave_locks_size = sizeof(*slave_locks) + lo->ldo_stripenr *
3922                            sizeof(slave_locks->handles[0]);
3923         /* Freed in lod_object_unlock */
3924         OBD_ALLOC(slave_locks, slave_locks_size);
3925         if (slave_locks == NULL)
3926                 RETURN(-ENOMEM);
3927         slave_locks->count = lo->ldo_stripenr;
3928
3929         /* striped directory lock */
3930         for (i = 1; i < lo->ldo_stripenr; i++) {
3931                 struct lustre_handle    lockh;
3932                 struct ldlm_res_id      *res_id;
3933
3934                 res_id = &lod_env_info(env)->lti_res_id;
3935                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
3936                                        res_id);
3937                 einfo->ei_res_id = res_id;
3938
3939                 LASSERT(lo->ldo_stripe[i] != NULL);
3940                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
3941                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
3942                                             einfo, policy);
3943                 } else {
3944                         struct ldlm_namespace *ns = einfo->ei_namespace;
3945                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
3946                         ldlm_completion_callback completion = einfo->ei_cb_cp;
3947                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
3948
3949                         if (einfo->ei_mode == LCK_PW ||
3950                             einfo->ei_mode == LCK_EX)
3951                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
3952
3953                         /* This only happens if there are mulitple stripes
3954                          * on the master MDT, i.e. except stripe0, there are
3955                          * other stripes on the Master MDT as well, Only
3956                          * happens in the test case right now. */
3957                         LASSERT(ns != NULL);
3958                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
3959                                                     policy, einfo->ei_mode,
3960                                                     &dlmflags, blocking,
3961                                                     completion, NULL,
3962                                                     NULL, 0, LVB_T_NONE,
3963                                                     NULL, &lockh);
3964                 }
3965                 if (rc != 0)
3966                         GOTO(out, rc);
3967                 slave_locks->handles[i] = lockh;
3968         }
3969
3970         einfo->ei_cbdata = slave_locks;
3971
3972 out:
3973         if (rc != 0 && slave_locks != NULL) {
3974                 einfo->ei_cbdata = slave_locks;
3975                 lod_object_unlock_internal(env, dt, einfo, policy);
3976                 OBD_FREE(slave_locks, slave_locks_size);
3977                 einfo->ei_cbdata = NULL;
3978         }
3979
3980         RETURN(rc);
3981 }
3982
3983 struct dt_object_operations lod_obj_ops = {
3984         .do_read_lock           = lod_object_read_lock,
3985         .do_write_lock          = lod_object_write_lock,
3986         .do_read_unlock         = lod_object_read_unlock,
3987         .do_write_unlock        = lod_object_write_unlock,
3988         .do_write_locked        = lod_object_write_locked,
3989         .do_attr_get            = lod_attr_get,
3990         .do_declare_attr_set    = lod_declare_attr_set,
3991         .do_attr_set            = lod_attr_set,
3992         .do_xattr_get           = lod_xattr_get,
3993         .do_declare_xattr_set   = lod_declare_xattr_set,
3994         .do_xattr_set           = lod_xattr_set,
3995         .do_declare_xattr_del   = lod_declare_xattr_del,
3996         .do_xattr_del           = lod_xattr_del,
3997         .do_xattr_list          = lod_xattr_list,
3998         .do_ah_init             = lod_ah_init,
3999         .do_declare_create      = lod_declare_object_create,
4000         .do_create              = lod_object_create,
4001         .do_declare_destroy     = lod_declare_object_destroy,
4002         .do_destroy             = lod_object_destroy,
4003         .do_index_try           = lod_index_try,
4004         .do_declare_ref_add     = lod_declare_ref_add,
4005         .do_ref_add             = lod_ref_add,
4006         .do_declare_ref_del     = lod_declare_ref_del,
4007         .do_ref_del             = lod_ref_del,
4008         .do_object_sync         = lod_object_sync,
4009         .do_object_lock         = lod_object_lock,
4010         .do_object_unlock       = lod_object_unlock,
4011 };
4012
4013 /**
4014  * Implementation of dt_body_operations::dbo_read.
4015  *
4016  * \see dt_body_operations::dbo_read() in the API description for details.
4017  */
4018 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
4019                         struct lu_buf *buf, loff_t *pos)
4020 {
4021         struct dt_object *next = dt_object_child(dt);
4022         return next->do_body_ops->dbo_read(env, next, buf, pos);
4023 }
4024
4025 /**
4026  * Implementation of dt_body_operations::dbo_declare_write.
4027  *
4028  * \see dt_body_operations::dbo_declare_write() in the API description
4029  * for details.
4030  */
4031 static ssize_t lod_declare_write(const struct lu_env *env,
4032                                  struct dt_object *dt,
4033                                  const struct lu_buf *buf, loff_t pos,
4034                                  struct thandle *th)
4035 {
4036         return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4037                                             th);
4038 }
4039
4040 /**
4041  * Implementation of dt_body_operations::dbo_write.
4042  *
4043  * \see dt_body_operations::dbo_write() in the API description for details.
4044  */
4045 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4046                          const struct lu_buf *buf, loff_t *pos,
4047                          struct thandle *th, int iq)
4048 {
4049         return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4050 }
4051
4052 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4053                              __u64 start, __u64 end, struct thandle *th)
4054 {
4055         if (dt_object_remote(dt))
4056                 return -ENOTSUPP;
4057
4058         return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4059                                             end, th);
4060 }
4061
4062 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4063                      __u64 start, __u64 end, struct thandle *th)
4064 {
4065         if (dt_object_remote(dt))
4066                 return -ENOTSUPP;
4067
4068         return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4069 }
4070
4071 static const struct dt_body_operations lod_body_lnk_ops = {
4072         .dbo_read               = lod_read,
4073         .dbo_declare_write      = lod_declare_write,
4074         .dbo_write              = lod_write
4075 };
4076
4077 static const struct dt_body_operations lod_body_ops = {
4078         .dbo_read               = lod_read,
4079         .dbo_declare_write      = lod_declare_write,
4080         .dbo_write              = lod_write,
4081         .dbo_declare_punch      = lod_declare_punch,
4082         .dbo_punch              = lod_punch,
4083 };
4084
4085 /**
4086  * Implementation of lu_object_operations::loo_object_init.
4087  *
4088  * The function determines the type and the index of the target device using
4089  * sequence of the object's FID. Then passes control down to the
4090  * corresponding device:
4091  *  OSD for the local objects, OSP for remote
4092  *
4093  * \see lu_object_operations::loo_object_init() in the API description
4094  * for details.
4095  */
4096 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4097                            const struct lu_object_conf *conf)
4098 {
4099         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
4100         struct lu_device        *cdev   = NULL;
4101         struct lu_object        *cobj;
4102         struct lod_tgt_descs    *ltd    = NULL;
4103         struct lod_tgt_desc     *tgt;
4104         u32                      idx    = 0;
4105         int                      type   = LU_SEQ_RANGE_ANY;
4106         int                      rc;
4107         ENTRY;
4108
4109         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4110         if (rc != 0) {
4111                 /* Note: Sometimes, it will Return EAGAIN here, see
4112                  * ptrlpc_import_delay_req(), which might confuse
4113                  * lu_object_find_at() and make it wait there incorrectly.
4114                  * so we convert it to EIO here.*/
4115                 if (rc == -EAGAIN)
4116                         rc = -EIO;
4117
4118                 RETURN(rc);
4119         }
4120
4121         if (type == LU_SEQ_RANGE_MDT &&
4122             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4123                 cdev = &lod->lod_child->dd_lu_dev;
4124         } else if (type == LU_SEQ_RANGE_MDT) {
4125                 ltd = &lod->lod_mdt_descs;
4126                 lod_getref(ltd);
4127         } else if (type == LU_SEQ_RANGE_OST) {
4128                 ltd = &lod->lod_ost_descs;
4129                 lod_getref(ltd);
4130         } else {
4131                 LBUG();
4132         }
4133
4134         if (ltd != NULL) {
4135                 if (ltd->ltd_tgts_size > idx &&
4136                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4137                         tgt = LTD_TGT(ltd, idx);
4138
4139                         LASSERT(tgt != NULL);
4140                         LASSERT(tgt->ltd_tgt != NULL);
4141
4142                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
4143                 }
4144                 lod_putref(lod, ltd);
4145         }
4146
4147         if (unlikely(cdev == NULL))
4148                 RETURN(-ENOENT);
4149
4150         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4151         if (unlikely(cobj == NULL))
4152                 RETURN(-ENOMEM);
4153
4154         lu_object_add(lo, cobj);
4155
4156         RETURN(0);
4157 }
4158
4159 /**
4160  *
4161  * Release resources associated with striping.
4162  *
4163  * If the object is striped (regular or directory), then release
4164  * the stripe objects references and free the ldo_stripe array.
4165  *
4166  * \param[in] env       execution environment
4167  * \param[in] lo        object
4168  */
4169 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4170 {
4171         int i;
4172
4173         if (lo->ldo_dir_stripe != NULL) {
4174                 OBD_FREE_PTR(lo->ldo_dir_stripe);
4175                 lo->ldo_dir_stripe = NULL;
4176         }
4177
4178         if (lo->ldo_stripe) {
4179                 LASSERT(lo->ldo_stripes_allocated > 0);
4180
4181                 for (i = 0; i < lo->ldo_stripenr; i++) {
4182                         if (lo->ldo_stripe[i])
4183                                 lu_object_put(env, &lo->ldo_stripe[i]->do_lu);
4184                 }
4185
4186                 i = sizeof(struct dt_object *) * lo->ldo_stripes_allocated;
4187                 OBD_FREE(lo->ldo_stripe, i);
4188                 lo->ldo_stripe = NULL;
4189                 lo->ldo_stripes_allocated = 0;
4190         }
4191         lo->ldo_striping_cached = 0;
4192         lo->ldo_stripenr = 0;
4193         lo->ldo_pattern = 0;
4194 }
4195
4196 /**
4197  * Implementation of lu_object_operations::loo_object_start.
4198  *
4199  * \see lu_object_operations::loo_object_start() in the API description
4200  * for details.
4201  */
4202 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
4203 {
4204         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
4205                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
4206         } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
4207                    fid_is_local_file(lu_object_fid(o))) {
4208                 /* Note: some local file (like last rcvd) is created
4209                  * through bottom layer (OSD), so the object initialization
4210                  * comes to lod, it does not set loh_attr yet, so
4211                  * set do_body_ops for local file anyway */
4212                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
4213         }
4214         return 0;
4215 }
4216
4217 /**
4218  * Implementation of lu_object_operations::loo_object_free.
4219  *
4220  * \see lu_object_operations::loo_object_free() in the API description
4221  * for details.
4222  */
4223 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
4224 {
4225         struct lod_object *mo = lu2lod_obj(o);
4226
4227         /*
4228          * release all underlying object pinned
4229          */
4230
4231         lod_object_free_striping(env, mo);
4232
4233         lod_object_set_pool(mo, NULL);
4234
4235         lu_object_fini(o);
4236         OBD_SLAB_FREE_PTR(mo, lod_object_kmem);
4237 }
4238
4239 /**
4240  * Implementation of lu_object_operations::loo_object_release.
4241  *
4242  * \see lu_object_operations::loo_object_release() in the API description
4243  * for details.
4244  */
4245 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
4246 {
4247         /* XXX: shouldn't we release everything here in case if object
4248          * creation failed before? */
4249 }
4250
4251 /**
4252  * Implementation of lu_object_operations::loo_object_print.
4253  *
4254  * \see lu_object_operations::loo_object_print() in the API description
4255  * for details.
4256  */
4257 static int lod_object_print(const struct lu_env *env, void *cookie,
4258                             lu_printer_t p, const struct lu_object *l)
4259 {
4260         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
4261
4262         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
4263 }
4264
4265 struct lu_object_operations lod_lu_obj_ops = {
4266         .loo_object_init        = lod_object_init,
4267         .loo_object_start       = lod_object_start,
4268         .loo_object_free        = lod_object_free,
4269         .loo_object_release     = lod_object_release,
4270         .loo_object_print       = lod_object_print,
4271 };