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