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