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