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