Whamcloud - gitweb
f0d467c1e203fd32b4ebca97a4bb9b0225e8bdfb
[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 dt_object        *obj = &lo->ldo_obj;
2044         struct lu_buf   *buf = &info->lti_buf;
2045         struct filter_fid *ff = buf->lb_buf;
2046         int rc;
2047
2048         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2049         if (rc < 0)
2050                 return 0;
2051
2052         fid_le_to_cpu(&ff->ff_parent, &ff->ff_parent);
2053         ff->ff_parent.f_seq = lu_object_fid(&obj->do_lu)->f_seq;
2054         ff->ff_parent.f_oid = lu_object_fid(&obj->do_lu)->f_oid;
2055         fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2056
2057         if (data->locd_declare)
2058                 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2059                                                       XATTR_NAME_FID,
2060                                                       LU_XATTR_REPLACE, th);
2061         else
2062                 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2063                                               LU_XATTR_REPLACE, th);
2064
2065         return rc;
2066 }
2067
2068 /**
2069  * Reset parent FID on OST object
2070  *
2071  * Replace parent FID with @dt object FID, which is only called during migration
2072  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2073  * the FID is changed.
2074  *
2075  * \param[in] env execution environment
2076  * \param[in] dt dt_object whose stripes's parent FID will be reset
2077  * \parem[in] th thandle
2078  * \param[in] declare if it is declare
2079  *
2080  * \retval      0 if reset succeeds
2081  * \retval      negative errno if reset fails
2082  */
2083 static int lod_object_replace_parent_fid(const struct lu_env *env,
2084                                          struct dt_object *dt,
2085                                          struct thandle *th, bool declare)
2086 {
2087         struct lod_object *lo = lod_dt_obj(dt);
2088         struct lod_thread_info  *info = lod_env_info(env);
2089         struct lu_buf *buf = &info->lti_buf;
2090         struct filter_fid *ff;
2091         struct lod_obj_stripe_cb_data data;
2092         int rc;
2093         ENTRY;
2094
2095         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2096
2097         /* set xattr to each stripes, if needed */
2098         rc = lod_load_striping(env, lo);
2099         if (rc != 0)
2100                 RETURN(rc);
2101
2102         if (!lod_obj_is_striped(dt))
2103                 RETURN(0);
2104
2105         if (info->lti_ea_store_size < sizeof(*ff)) {
2106                 rc = lod_ea_store_resize(info, sizeof(*ff));
2107                 if (rc != 0)
2108                         RETURN(rc);
2109         }
2110
2111         buf->lb_buf = info->lti_ea_store;
2112         buf->lb_len = info->lti_ea_store_size;
2113
2114         data.locd_declare = declare;
2115         rc = lod_obj_for_each_stripe(env, lo, th,
2116                         lod_obj_stripe_replace_parent_fid_cb, &data);
2117
2118         RETURN(rc);
2119 }
2120
2121 /**
2122  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2123  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2124  * to be added.
2125   *
2126  * \param[in] env       execution environment
2127  * \param[in] dt        dt_object to add components on
2128  * \param[in] buf       buffer contains components to be added
2129  * \parem[in] th        thandle
2130  *
2131  * \retval      0 on success
2132  * \retval      negative errno on failure
2133  */
2134 static int lod_declare_layout_add(const struct lu_env *env,
2135                                   struct dt_object *dt,
2136                                   const struct lu_buf *buf,
2137                                   struct thandle *th)
2138 {
2139         struct lod_layout_component *comp_array, *lod_comp;
2140         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2141         struct lov_desc         *desc = &d->lod_desc;
2142         struct lod_object       *lo = lod_dt_obj(dt);
2143         struct lov_user_md_v1   *v1;
2144         struct lov_user_md_v3   *v3;
2145         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2146         struct lu_extent        *ext;
2147         __u32   magic;
2148         __u64   prev_end;
2149         int     i, rc, array_cnt;
2150         ENTRY;
2151
2152         LASSERT(lo->ldo_is_composite);
2153
2154         magic = comp_v1->lcm_magic;
2155         /* Replay request, see comment for LOV_MAGIC_DEF */
2156         if (unlikely(le32_to_cpu(magic) == LOV_MAGIC_COMP_V1_DEF)) {
2157                 struct dt_object *next = dt_object_child(dt);
2158
2159                 lod_object_free_striping(env, lo);
2160                 rc = lod_use_defined_striping(env, lo, buf);
2161                 if (rc == 0) {
2162                         lo->ldo_comp_cached = 1;
2163                         rc = lod_sub_object_declare_xattr_set(env, next, buf,
2164                                                               XATTR_NAME_LOV,
2165                                                               0, th);
2166                 }
2167                 RETURN(rc);
2168         }
2169
2170         prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2171         rc = lod_verify_striping(d, buf, false, prev_end);
2172         if (rc != 0)
2173                 RETURN(rc);
2174
2175         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2176                 lustre_swab_lov_comp_md_v1(comp_v1);
2177                 magic = comp_v1->lcm_magic;
2178         }
2179
2180         if (magic != LOV_USER_MAGIC_COMP_V1)
2181                 RETURN(-EINVAL);
2182
2183         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2184         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2185         if (comp_array == NULL)
2186                 RETURN(-ENOMEM);
2187
2188         memcpy(comp_array, lo->ldo_comp_entries,
2189                sizeof(*comp_array) * lo->ldo_comp_cnt);
2190
2191         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2192                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2193                                 comp_v1->lcm_entries[i].lcme_offset);
2194                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2195
2196                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2197                 lod_comp->llc_extent.e_start = ext->e_start;
2198                 lod_comp->llc_extent.e_end = ext->e_end;
2199                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2200
2201                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2202                 if (lod_comp->llc_stripenr <= 0)
2203                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2204                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2205                 if (lod_comp->llc_stripe_size <= 0)
2206                         lod_comp->llc_stripe_size =
2207                                 desc->ld_default_stripe_size;
2208
2209                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2210                         int len;
2211                         v3 = (struct lov_user_md_v3 *) v1;
2212                         if (v3->lmm_pool_name[0] != '\0') {
2213                                 len = strlen(v3->lmm_pool_name);
2214                                 OBD_ALLOC(lod_comp->llc_pool, len + 1);
2215                                 if (lod_comp->llc_pool == NULL)
2216                                         GOTO(error, rc = -ENOMEM);
2217                                 strncpy(lod_comp->llc_pool, v3->lmm_pool_name,
2218                                         len + 1);
2219                         }
2220                 }
2221         }
2222
2223         OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2224         lo->ldo_comp_entries = comp_array;
2225         lo->ldo_comp_cnt = array_cnt;
2226         /* No need to increase layout generation here, it will be increased
2227          * later when generating component ID for the new components */
2228
2229         rc = lod_declare_striped_object(env, dt, NULL, NULL, th);
2230         RETURN(rc);
2231
2232 error:
2233         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2234                 lod_comp = &comp_array[i];
2235                 if (lod_comp->llc_pool != NULL) {
2236                         OBD_FREE(lod_comp->llc_pool,
2237                                  strlen(lod_comp->llc_pool) + 1);
2238                         lod_comp->llc_pool = NULL;
2239                 }
2240         }
2241         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2242         RETURN(rc);
2243 }
2244
2245 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2246 {
2247         int magic, size = 0, i;
2248         struct lod_layout_component *comp_entries;
2249         __u16 comp_cnt;
2250         bool is_composite;
2251
2252         if (is_dir) {
2253                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2254                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2255                 is_composite =
2256                         lo->ldo_def_striping->lds_def_striping_is_composite;
2257         } else {
2258                 comp_cnt = lo->ldo_comp_cnt;
2259                 comp_entries = lo->ldo_comp_entries;
2260                 is_composite = lo->ldo_is_composite;
2261         }
2262
2263
2264         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2265         if (is_composite) {
2266                 size = sizeof(struct lov_comp_md_v1) +
2267                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2268                 LASSERT(size % sizeof(__u64) == 0);
2269         }
2270
2271         for (i = 0; i < comp_cnt; i++) {
2272                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2273
2274                 size += lov_user_md_size(
2275                         is_dir ? 0 : comp_entries[i].llc_stripenr,
2276                         magic);
2277                 LASSERT(size % sizeof(__u64) == 0);
2278         }
2279         return size;
2280 }
2281
2282 /**
2283  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2284  * the '$field' can only be 'flags' now. The xattr value is binary
2285  * lov_comp_md_v1 which contains the component ID(s) and the value of
2286  * the field to be modified.
2287  *
2288  * \param[in] env       execution environment
2289  * \param[in] dt        dt_object to be modified
2290  * \param[in] op        operation string, like "set.flags"
2291  * \param[in] buf       buffer contains components to be set
2292  * \parem[in] th        thandle
2293  *
2294  * \retval      0 on success
2295  * \retval      negative errno on failure
2296  */
2297 static int lod_declare_layout_set(const struct lu_env *env,
2298                                   struct dt_object *dt,
2299                                   char *op, const struct lu_buf *buf,
2300                                   struct thandle *th)
2301 {
2302         struct lod_layout_component     *lod_comp;
2303         struct lod_thread_info  *info = lod_env_info(env);
2304         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2305         struct lod_object       *lo = lod_dt_obj(dt);
2306         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2307         __u32   magic, id;
2308         int     i, j, rc;
2309         bool    changed = false;
2310         ENTRY;
2311
2312         if (strcmp(op, "set.flags") != 0) {
2313                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2314                        lod2obd(d)->obd_name, op);
2315                 RETURN(-ENOTSUPP);
2316         }
2317
2318         magic = comp_v1->lcm_magic;
2319         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2320                 lustre_swab_lov_comp_md_v1(comp_v1);
2321                 magic = comp_v1->lcm_magic;
2322         }
2323
2324         if (magic != LOV_USER_MAGIC_COMP_V1)
2325                 RETURN(-EINVAL);
2326
2327         if (comp_v1->lcm_entry_count == 0) {
2328                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2329                        lod2obd(d)->obd_name);
2330                 RETURN(-EINVAL);
2331         }
2332
2333         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2334                 id = comp_v1->lcm_entries[i].lcme_id;
2335
2336                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2337                         lod_comp = &lo->ldo_comp_entries[j];
2338                         if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2339                                 lod_comp->llc_flags =
2340                                         comp_v1->lcm_entries[i].lcme_flags;
2341                                 changed = true;
2342                         }
2343                 }
2344         }
2345
2346         if (!changed) {
2347                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2348                        lod2obd(d)->obd_name);
2349                 RETURN(-EINVAL);
2350         }
2351
2352         lod_obj_inc_layout_gen(lo);
2353
2354         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2355         rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2356                                               XATTR_NAME_LOV, 0, th);
2357         RETURN(rc);
2358 }
2359
2360 /**
2361  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2362  * and the xattr value is a unique component ID or a special lcme_id.
2363  *
2364  * \param[in] env       execution environment
2365  * \param[in] dt        dt_object to be operated on
2366  * \param[in] buf       buffer contains component ID or lcme_id
2367  * \parem[in] th        thandle
2368  *
2369  * \retval      0 on success
2370  * \retval      negative errno on failure
2371  */
2372 static int lod_declare_layout_del(const struct lu_env *env,
2373                                   struct dt_object *dt,
2374                                   const struct lu_buf *buf,
2375                                   struct thandle *th)
2376 {
2377         struct lod_thread_info  *info = lod_env_info(env);
2378         struct dt_object        *next = dt_object_child(dt);
2379         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2380         struct lod_object       *lo = lod_dt_obj(dt);
2381         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
2382         __u32   id;
2383         int     rc, i, j, left;
2384         ENTRY;
2385
2386         LASSERT(lo->ldo_is_composite);
2387
2388         id = *(__u32 *)buf->lb_buf;
2389         if (id == 0 || id == LCME_ID_NONE) {
2390                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
2391                        lod2obd(d)->obd_name, id);
2392                 RETURN(-EINVAL);
2393         }
2394
2395         left = lo->ldo_comp_cnt;
2396         if (left <= 0)
2397                 RETURN(-EINVAL);
2398
2399         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2400                 struct lod_layout_component *lod_comp;
2401
2402                 lod_comp = &lo->ldo_comp_entries[i];
2403
2404                 if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
2405                         continue;
2406                 else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
2407                          !(id & lod_comp->llc_flags))
2408                         continue;
2409
2410                 if (left != (i + 1)) {
2411                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2412                                "a hole.\n", lod2obd(d)->obd_name);
2413                         RETURN(-EINVAL);
2414                 }
2415                 left--;
2416
2417                 /* Mark the component as deleted */
2418                 lod_comp->llc_id = LCME_ID_INVAL;
2419
2420                 /* Not instantiated component */
2421                 if (lod_comp->llc_stripe == NULL)
2422                         continue;
2423
2424                 LASSERT(lod_comp->llc_stripenr > 0);
2425                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2426                         struct dt_object *obj = lod_comp->llc_stripe[j];
2427
2428                         if (obj == NULL)
2429                                 continue;
2430                         rc = lod_sub_object_declare_destroy(env, obj, th);
2431                         if (rc)
2432                                 RETURN(rc);
2433                 }
2434         }
2435
2436         LASSERTF(left >= 0, "left = %d\n", left);
2437         if (left == lo->ldo_comp_cnt) {
2438                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2439                        lod2obd(d)->obd_name, id);
2440                 RETURN(-EINVAL);
2441         }
2442
2443         memset(attr, 0, sizeof(*attr));
2444         attr->la_valid = LA_SIZE;
2445         rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2446         if (rc)
2447                 RETURN(rc);
2448
2449         if (left > 0) {
2450                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2451                 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2452                                                       XATTR_NAME_LOV, 0, th);
2453         } else {
2454                 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2455                                                       th);
2456         }
2457
2458         RETURN(rc);
2459 }
2460
2461 /**
2462  * Declare layout add/set/del operations issued by special xattr names:
2463  *
2464  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2465  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2466  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2467  *
2468  * \param[in] env       execution environment
2469  * \param[in] dt        object
2470  * \param[in] name      name of xattr
2471  * \param[in] buf       lu_buf contains xattr value
2472  * \param[in] th        transaction handle
2473  *
2474  * \retval              0 on success
2475  * \retval              negative if failed
2476  */
2477 static int lod_declare_modify_layout(const struct lu_env *env,
2478                                      struct dt_object *dt,
2479                                      const char *name,
2480                                      const struct lu_buf *buf,
2481                                      struct thandle *th)
2482 {
2483         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2484         struct lod_object *lo = lod_dt_obj(dt);
2485         struct dt_object *next = dt_object_child(&lo->ldo_obj);
2486         char *op;
2487         int rc, len = strlen(XATTR_LUSTRE_LOV);
2488         ENTRY;
2489
2490         LASSERT(dt_object_exists(dt));
2491
2492         if (strlen(name) <= len || name[len] != '.') {
2493                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2494                        lod2obd(d)->obd_name, name);
2495                 RETURN(-EINVAL);
2496         }
2497         len++;
2498
2499         dt_write_lock(env, next, 0);
2500         rc = lod_load_striping_locked(env, lo);
2501         if (rc)
2502                 GOTO(unlock, rc);
2503
2504         /* the layout to be modified must be a composite layout */
2505         if (!lo->ldo_is_composite) {
2506                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2507                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2508                 GOTO(unlock, rc = -EINVAL);
2509         }
2510
2511         op = (char *)name + len;
2512         if (strcmp(op, "add") == 0) {
2513                 rc = lod_declare_layout_add(env, dt, buf, th);
2514         } else if (strcmp(op, "del") == 0) {
2515                 rc = lod_declare_layout_del(env, dt, buf, th);
2516         } else if (strncmp(op, "set", strlen("set")) == 0) {
2517                 rc = lod_declare_layout_set(env, dt, op, buf, th);
2518         } else  {
2519                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2520                        lod2obd(d)->obd_name, name);
2521                 GOTO(unlock, rc = -ENOTSUPP);
2522         }
2523 unlock:
2524         if (rc)
2525                 lod_object_free_striping(env, lo);
2526         dt_write_unlock(env, next);
2527
2528         RETURN(rc);
2529 }
2530
2531 /**
2532  * Implementation of dt_object_operations::do_declare_xattr_set.
2533  *
2534  * \see dt_object_operations::do_declare_xattr_set() in the API description
2535  * for details.
2536  *
2537  * the extension to the API:
2538  *   - declaring LOVEA requests striping creation
2539  *   - LU_XATTR_REPLACE means layout swap
2540  */
2541 static int lod_declare_xattr_set(const struct lu_env *env,
2542                                  struct dt_object *dt,
2543                                  const struct lu_buf *buf,
2544                                  const char *name, int fl,
2545                                  struct thandle *th)
2546 {
2547         struct dt_object *next = dt_object_child(dt);
2548         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2549         __u32             mode;
2550         int               rc;
2551         ENTRY;
2552
2553         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2554         if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2555             (strcmp(name, XATTR_NAME_LOV) == 0 ||
2556              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2557                 /*
2558                  * this is a request to create object's striping.
2559                  *
2560                  * allow to declare predefined striping on a new (!mode) object
2561                  * which is supposed to be replay of regular file creation
2562                  * (when LOV setting is declared)
2563                  *
2564                  * LU_XATTR_REPLACE is set to indicate a layout swap
2565                  */
2566                 if (dt_object_exists(dt)) {
2567                         rc = dt_attr_get(env, next, attr);
2568                         if (rc)
2569                                 RETURN(rc);
2570                 } else {
2571                         memset(attr, 0, sizeof(*attr));
2572                         attr->la_valid = LA_TYPE | LA_MODE;
2573                         attr->la_mode = S_IFREG;
2574                 }
2575                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2576         } else if (S_ISREG(mode) &&
2577                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2578                    strncmp(name, XATTR_LUSTRE_LOV,
2579                            strlen(XATTR_LUSTRE_LOV)) == 0) {
2580                 /*
2581                  * this is a request to modify object's striping.
2582                  * add/set/del component(s).
2583                  */
2584                 if (!dt_object_exists(dt))
2585                         RETURN(-ENOENT);
2586
2587                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2588         } else if (S_ISDIR(mode)) {
2589                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2590         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2591                 rc = lod_object_replace_parent_fid(env, dt, th, true);
2592         } else {
2593                 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2594                                                       fl, th);
2595         }
2596
2597         RETURN(rc);
2598 }
2599
2600 /**
2601  * Apply xattr changes to the object.
2602  *
2603  * Applies xattr changes to the object and the stripes if the latter exist.
2604  *
2605  * \param[in] env       execution environment
2606  * \param[in] dt        object
2607  * \param[in] buf       buffer pointing to the new value of xattr
2608  * \param[in] name      name of xattr
2609  * \param[in] fl        flags
2610  * \param[in] th        transaction handle
2611  *
2612  * \retval              0 on success
2613  * \retval              negative if failed
2614  */
2615 static int lod_xattr_set_internal(const struct lu_env *env,
2616                                   struct dt_object *dt,
2617                                   const struct lu_buf *buf,
2618                                   const char *name, int fl,
2619                                   struct thandle *th)
2620 {
2621         struct dt_object        *next = dt_object_child(dt);
2622         struct lod_object       *lo = lod_dt_obj(dt);
2623         int                     rc;
2624         int                     i;
2625         ENTRY;
2626
2627         rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2628         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2629                 RETURN(rc);
2630
2631         /* Note: Do not set LinkEA on sub-stripes, otherwise
2632          * it will confuse the fid2path process(see mdt_path_current()).
2633          * The linkEA between master and sub-stripes is set in
2634          * lod_xattr_set_lmv(). */
2635         if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2636                 RETURN(0);
2637
2638         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2639                 LASSERT(lo->ldo_stripe[i]);
2640
2641                 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2642                                               fl, th);
2643                 if (rc != 0)
2644                         break;
2645         }
2646
2647         RETURN(rc);
2648 }
2649
2650 /**
2651  * Delete an extended attribute.
2652  *
2653  * Deletes specified xattr from the object and the stripes if the latter exist.
2654  *
2655  * \param[in] env       execution environment
2656  * \param[in] dt        object
2657  * \param[in] name      name of xattr
2658  * \param[in] th        transaction handle
2659  *
2660  * \retval              0 on success
2661  * \retval              negative if failed
2662  */
2663 static int lod_xattr_del_internal(const struct lu_env *env,
2664                                   struct dt_object *dt,
2665                                   const char *name, struct thandle *th)
2666 {
2667         struct dt_object        *next = dt_object_child(dt);
2668         struct lod_object       *lo = lod_dt_obj(dt);
2669         int                     rc;
2670         int                     i;
2671         ENTRY;
2672
2673         rc = lod_sub_object_xattr_del(env, next, name, th);
2674         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2675                 RETURN(rc);
2676
2677         if (lo->ldo_dir_stripenr == 0)
2678                 RETURN(rc);
2679
2680         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2681                 LASSERT(lo->ldo_stripe[i]);
2682
2683                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2684                                               th);
2685                 if (rc != 0)
2686                         break;
2687         }
2688
2689         RETURN(rc);
2690 }
2691
2692 /**
2693  * Set default striping on a directory.
2694  *
2695  * Sets specified striping on a directory object unless it matches the default
2696  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2697  * EA. This striping will be used when regular file is being created in this
2698  * directory.
2699  *
2700  * \param[in] env       execution environment
2701  * \param[in] dt        the striped object
2702  * \param[in] buf       buffer with the striping
2703  * \param[in] name      name of EA
2704  * \param[in] fl        xattr flag (see OSD API description)
2705  * \param[in] th        transaction handle
2706  *
2707  * \retval              0 on success
2708  * \retval              negative if failed
2709  */
2710 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2711                                     struct dt_object *dt,
2712                                     const struct lu_buf *buf,
2713                                     const char *name, int fl,
2714                                     struct thandle *th)
2715 {
2716         struct lov_user_md_v1   *lum;
2717         struct lov_user_md_v3   *v3 = NULL;
2718         const char              *pool_name = NULL;
2719         int                      rc;
2720         bool                     is_del;
2721         ENTRY;
2722
2723         LASSERT(buf != NULL && buf->lb_buf != NULL);
2724         lum = buf->lb_buf;
2725
2726         switch (lum->lmm_magic) {
2727         case LOV_USER_MAGIC_V3:
2728                 v3 = buf->lb_buf;
2729                 if (v3->lmm_pool_name[0] != '\0')
2730                         pool_name = v3->lmm_pool_name;
2731                 /* fall through */
2732         case LOV_USER_MAGIC_V1:
2733                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2734                  * (i.e. all default values specified) then delete default
2735                  * striping from dir. */
2736                 CDEBUG(D_LAYOUT,
2737                        "set default striping: sz %u # %u offset %d %s %s\n",
2738                        (unsigned)lum->lmm_stripe_size,
2739                        (unsigned)lum->lmm_stripe_count,
2740                        (int)lum->lmm_stripe_offset,
2741                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2742
2743                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2744                                              lum->lmm_stripe_count,
2745                                              lum->lmm_stripe_offset,
2746                                              pool_name);
2747                 break;
2748         case LOV_USER_MAGIC_COMP_V1:
2749                 is_del = false;
2750                 break;
2751         default:
2752                 CERROR("Invalid magic %x\n", lum->lmm_magic);
2753                 RETURN(-EINVAL);
2754         }
2755
2756         if (is_del) {
2757                 rc = lod_xattr_del_internal(env, dt, name, th);
2758                 if (rc == -ENODATA)
2759                         rc = 0;
2760         } else {
2761                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2762         }
2763
2764         RETURN(rc);
2765 }
2766
2767 /**
2768  * Set default striping on a directory object.
2769  *
2770  * Sets specified striping on a directory object unless it matches the default
2771  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2772  * EA. This striping will be used when a new directory is being created in the
2773  * directory.
2774  *
2775  * \param[in] env       execution environment
2776  * \param[in] dt        the striped object
2777  * \param[in] buf       buffer with the striping
2778  * \param[in] name      name of EA
2779  * \param[in] fl        xattr flag (see OSD API description)
2780  * \param[in] th        transaction handle
2781  *
2782  * \retval              0 on success
2783  * \retval              negative if failed
2784  */
2785 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2786                                             struct dt_object *dt,
2787                                             const struct lu_buf *buf,
2788                                             const char *name, int fl,
2789                                             struct thandle *th)
2790 {
2791         struct lmv_user_md_v1   *lum;
2792         int                      rc;
2793         ENTRY;
2794
2795         LASSERT(buf != NULL && buf->lb_buf != NULL);
2796         lum = buf->lb_buf;
2797
2798         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2799               le32_to_cpu(lum->lum_stripe_count),
2800               (int)le32_to_cpu(lum->lum_stripe_offset));
2801
2802         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2803                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2804                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2805                 rc = lod_xattr_del_internal(env, dt, name, th);
2806                 if (rc == -ENODATA)
2807                         rc = 0;
2808         } else {
2809                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2810                 if (rc != 0)
2811                         RETURN(rc);
2812         }
2813
2814         RETURN(rc);
2815 }
2816
2817 /**
2818  * Turn directory into a striped directory.
2819  *
2820  * During replay the client sends the striping created before MDT
2821  * failure, then the layer above LOD sends this defined striping
2822  * using ->do_xattr_set(), so LOD uses this method to replay creation
2823  * of the stripes. Notice the original information for the striping
2824  * (#stripes, FIDs, etc) was transferred in declare path.
2825  *
2826  * \param[in] env       execution environment
2827  * \param[in] dt        the striped object
2828  * \param[in] buf       not used currently
2829  * \param[in] name      not used currently
2830  * \param[in] fl        xattr flag (see OSD API description)
2831  * \param[in] th        transaction handle
2832  *
2833  * \retval              0 on success
2834  * \retval              negative if failed
2835  */
2836 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2837                              const struct lu_buf *buf, const char *name,
2838                              int fl, struct thandle *th)
2839 {
2840         struct lod_object       *lo = lod_dt_obj(dt);
2841         struct lod_thread_info  *info = lod_env_info(env);
2842         struct lu_attr          *attr = &info->lti_attr;
2843         struct dt_object_format *dof = &info->lti_format;
2844         struct lu_buf           lmv_buf;
2845         struct lu_buf           slave_lmv_buf;
2846         struct lmv_mds_md_v1    *lmm;
2847         struct lmv_mds_md_v1    *slave_lmm = NULL;
2848         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2849         int                     i;
2850         int                     rc;
2851         ENTRY;
2852
2853         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2854                 RETURN(-ENOTDIR);
2855
2856         /* The stripes are supposed to be allocated in declare phase,
2857          * if there are no stripes being allocated, it will skip */
2858         if (lo->ldo_dir_stripenr == 0)
2859                 RETURN(0);
2860
2861         rc = dt_attr_get(env, dt_object_child(dt), attr);
2862         if (rc != 0)
2863                 RETURN(rc);
2864
2865         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2866                          LA_MODE | LA_UID | LA_GID | LA_TYPE;
2867         dof->dof_type = DFT_DIR;
2868
2869         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2870         if (rc != 0)
2871                 RETURN(rc);
2872         lmm = lmv_buf.lb_buf;
2873
2874         OBD_ALLOC_PTR(slave_lmm);
2875         if (slave_lmm == NULL)
2876                 RETURN(-ENOMEM);
2877
2878         lod_prep_slave_lmv_md(slave_lmm, lmm);
2879         slave_lmv_buf.lb_buf = slave_lmm;
2880         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2881
2882         rec->rec_type = S_IFDIR;
2883         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2884                 struct dt_object *dto;
2885                 char             *stripe_name = info->lti_key;
2886                 struct lu_name          *sname;
2887                 struct linkea_data       ldata          = { NULL };
2888                 struct lu_buf            linkea_buf;
2889
2890                 dto = lo->ldo_stripe[i];
2891
2892                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2893                 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2894                                            th);
2895                 if (rc != 0) {
2896                         dt_write_unlock(env, dto);
2897                         GOTO(out, rc);
2898                 }
2899
2900                 rc = lod_sub_object_ref_add(env, dto, th);
2901                 dt_write_unlock(env, dto);
2902                 if (rc != 0)
2903                         GOTO(out, rc);
2904
2905                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2906                 rc = lod_sub_object_index_insert(env, dto,
2907                                 (const struct dt_rec *)rec,
2908                                 (const struct dt_key *)dot, th, 0);
2909                 if (rc != 0)
2910                         GOTO(out, rc);
2911
2912                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2913                 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2914                                (const struct dt_key *)dotdot, th, 0);
2915                 if (rc != 0)
2916                         GOTO(out, rc);
2917
2918                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2919                     cfs_fail_val != i) {
2920                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2921                             cfs_fail_val == i)
2922                                 slave_lmm->lmv_master_mdt_index =
2923                                                         cpu_to_le32(i + 1);
2924                         else
2925                                 slave_lmm->lmv_master_mdt_index =
2926                                                         cpu_to_le32(i);
2927
2928                         rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2929                                                       XATTR_NAME_LMV, fl, th);
2930                         if (rc != 0)
2931                                 GOTO(out, rc);
2932                 }
2933
2934                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2935                     cfs_fail_val == i)
2936                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2937                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2938                 else
2939                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2940                                  PFID(lu_object_fid(&dto->do_lu)), i);
2941
2942                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2943                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
2944                                       sname, lu_object_fid(&dt->do_lu));
2945                 if (rc != 0)
2946                         GOTO(out, rc);
2947
2948                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
2949                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
2950                 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
2951                                         XATTR_NAME_LINK, 0, th);
2952                 if (rc != 0)
2953                         GOTO(out, rc);
2954
2955                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2956                 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
2957                                (const struct dt_rec *)rec,
2958                                (const struct dt_key *)stripe_name, th, 0);
2959                 if (rc != 0)
2960                         GOTO(out, rc);
2961
2962                 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
2963                 if (rc != 0)
2964                         GOTO(out, rc);
2965         }
2966
2967         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
2968                 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
2969                                               &lmv_buf, XATTR_NAME_LMV, fl, th);
2970 out:
2971         if (slave_lmm != NULL)
2972                 OBD_FREE_PTR(slave_lmm);
2973
2974         RETURN(rc);
2975 }
2976
2977 /**
2978  * Helper function to declare/execute creation of a striped directory
2979  *
2980  * Called in declare/create object path, prepare striping for a directory
2981  * and prepare defaults data striping for the objects to be created in
2982  * that directory. Notice the function calls "declaration" or "execution"
2983  * methods depending on \a declare param. This is a consequence of the
2984  * current approach while we don't have natural distributed transactions:
2985  * we basically execute non-local updates in the declare phase. So, the
2986  * arguments for the both phases are the same and this is the reason for
2987  * this function to exist.
2988  *
2989  * \param[in] env       execution environment
2990  * \param[in] dt        object
2991  * \param[in] attr      attributes the stripes will be created with
2992  * \param[in] dof       format of stripes (see OSD API description)
2993  * \param[in] th        transaction handle
2994  * \param[in] declare   where to call "declare" or "execute" methods
2995  *
2996  * \retval              0 on success
2997  * \retval              negative if failed
2998  */
2999 static int lod_dir_striping_create_internal(const struct lu_env *env,
3000                                             struct dt_object *dt,
3001                                             struct lu_attr *attr,
3002                                             struct dt_object_format *dof,
3003                                             struct thandle *th,
3004                                             bool declare)
3005 {
3006         struct lod_thread_info *info = lod_env_info(env);
3007         struct lod_object *lo = lod_dt_obj(dt);
3008         const struct lod_default_striping *lds = lo->ldo_def_striping;
3009         int rc;
3010         ENTRY;
3011
3012         LASSERT(ergo(lds != NULL,
3013                      lds->lds_def_striping_set ||
3014                      lds->lds_dir_def_striping_set));
3015
3016         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3017                                  lo->ldo_dir_stripe_offset)) {
3018                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3019                 int stripe_count = lo->ldo_dir_stripenr;
3020
3021                 if (info->lti_ea_store_size < sizeof(*v1)) {
3022                         rc = lod_ea_store_resize(info, sizeof(*v1));
3023                         if (rc != 0)
3024                                 RETURN(rc);
3025                         v1 = info->lti_ea_store;
3026                 }
3027
3028                 memset(v1, 0, sizeof(*v1));
3029                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3030                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3031                 v1->lum_stripe_offset =
3032                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
3033
3034                 info->lti_buf.lb_buf = v1;
3035                 info->lti_buf.lb_len = sizeof(*v1);
3036
3037                 if (declare)
3038                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
3039                                                        &info->lti_buf, dof, th);
3040                 else
3041                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3042                                                XATTR_NAME_LMV, 0, th);
3043                 if (rc != 0)
3044                         RETURN(rc);
3045         }
3046
3047         /* Transfer default LMV striping from the parent */
3048         if (lds != NULL && lds->lds_dir_def_striping_set &&
3049             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3050                                  lds->lds_dir_def_stripe_offset)) {
3051                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3052
3053                 if (info->lti_ea_store_size < sizeof(*v1)) {
3054                         rc = lod_ea_store_resize(info, sizeof(*v1));
3055                         if (rc != 0)
3056                                 RETURN(rc);
3057                         v1 = info->lti_ea_store;
3058                 }
3059
3060                 memset(v1, 0, sizeof(*v1));
3061                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3062                 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3063                 v1->lum_stripe_offset =
3064                                 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3065                 v1->lum_hash_type =
3066                                 cpu_to_le32(lds->lds_dir_def_hash_type);
3067
3068                 info->lti_buf.lb_buf = v1;
3069                 info->lti_buf.lb_len = sizeof(*v1);
3070                 if (declare)
3071                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3072                                                        XATTR_NAME_DEFAULT_LMV,
3073                                                        0, th);
3074                 else
3075                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3076                                                   &info->lti_buf,
3077                                                   XATTR_NAME_DEFAULT_LMV, 0,
3078                                                   th);
3079                 if (rc != 0)
3080                         RETURN(rc);
3081         }
3082
3083         /* Transfer default LOV striping from the parent */
3084         if (lds != NULL && lds->lds_def_striping_set &&
3085             lds->lds_def_comp_cnt != 0) {
3086                 struct lov_mds_md *lmm;
3087                 int lmm_size = lod_comp_md_size(lo, true);
3088
3089                 if (info->lti_ea_store_size < lmm_size) {
3090                         rc = lod_ea_store_resize(info, lmm_size);
3091                         if (rc != 0)
3092                                 RETURN(rc);
3093                 }
3094                 lmm = info->lti_ea_store;
3095
3096                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3097                 if (rc != 0)
3098                         RETURN(rc);
3099
3100                 info->lti_buf.lb_buf = lmm;
3101                 info->lti_buf.lb_len = lmm_size;
3102
3103                 if (declare)
3104                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3105                                                        XATTR_NAME_LOV, 0, th);
3106                 else
3107                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3108                                                       XATTR_NAME_LOV, 0, th);
3109                 if (rc != 0)
3110                         RETURN(rc);
3111         }
3112
3113         RETURN(0);
3114 }
3115
3116 static int lod_declare_dir_striping_create(const struct lu_env *env,
3117                                            struct dt_object *dt,
3118                                            struct lu_attr *attr,
3119                                            struct dt_object_format *dof,
3120                                            struct thandle *th)
3121 {
3122         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3123 }
3124
3125 static int lod_dir_striping_create(const struct lu_env *env,
3126                                    struct dt_object *dt,
3127                                    struct lu_attr *attr,
3128                                    struct dt_object_format *dof,
3129                                    struct thandle *th)
3130 {
3131         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3132 }
3133
3134 /**
3135  * Make LOV EA for striped object.
3136  *
3137  * Generate striping information and store it in the LOV EA of the given
3138  * object. The caller must ensure nobody else is calling the function
3139  * against the object concurrently. The transaction must be started.
3140  * FLDB service must be running as well; it's used to map FID to the target,
3141  * which is stored in LOV EA.
3142  *
3143  * \param[in] env               execution environment for this thread
3144  * \param[in] lo                LOD object
3145  * \param[in] th                transaction handle
3146  *
3147  * \retval                      0 if LOV EA is stored successfully
3148  * \retval                      negative error number on failure
3149  */
3150 static int lod_generate_and_set_lovea(const struct lu_env *env,
3151                                       struct lod_object *lo,
3152                                       struct thandle *th)
3153 {
3154         struct lod_thread_info  *info = lod_env_info(env);
3155         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3156         struct lov_mds_md_v1    *lmm;
3157         int                      rc, lmm_size;
3158         ENTRY;
3159
3160         LASSERT(lo);
3161
3162         if (lo->ldo_comp_cnt == 0) {
3163                 lod_object_free_striping(env, lo);
3164                 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3165                 RETURN(rc);
3166         }
3167
3168         lmm_size = lod_comp_md_size(lo, false);
3169         if (info->lti_ea_store_size < lmm_size) {
3170                 rc = lod_ea_store_resize(info, lmm_size);
3171                 if (rc)
3172                         RETURN(rc);
3173         }
3174         lmm = info->lti_ea_store;
3175
3176         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3177         if (rc)
3178                 RETURN(rc);
3179
3180         info->lti_buf.lb_buf = lmm;
3181         info->lti_buf.lb_len = lmm_size;
3182         rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3183                                       XATTR_NAME_LOV, 0, th);
3184         RETURN(rc);
3185 }
3186
3187 /**
3188  * Delete layout component(s)
3189  *
3190  * \param[in] env       execution environment for this thread
3191  * \param[in] dt        object
3192  * \param[in] th        transaction handle
3193  *
3194  * \retval      0 on success
3195  * \retval      negative error number on failure
3196  */
3197 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3198                           struct thandle *th)
3199 {
3200         struct lod_layout_component     *lod_comp;
3201         struct lod_object       *lo = lod_dt_obj(dt);
3202         struct dt_object        *next = dt_object_child(dt);
3203         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3204         int     rc, i, j, left;
3205
3206         LASSERT(lo->ldo_is_composite);
3207         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3208
3209         left = lo->ldo_comp_cnt;
3210         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3211                 lod_comp = &lo->ldo_comp_entries[i];
3212
3213                 if (lod_comp->llc_id != LCME_ID_INVAL)
3214                         break;
3215                 left--;
3216
3217                 /* Not instantiated component */
3218                 if (lod_comp->llc_stripe == NULL)
3219                         continue;
3220
3221                 LASSERT(lod_comp->llc_stripenr > 0);
3222                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3223                         struct dt_object *obj = lod_comp->llc_stripe[j];
3224
3225                         if (obj == NULL)
3226                                 continue;
3227                         rc = lod_sub_object_destroy(env, obj, th);
3228                         if (rc)
3229                                 GOTO(out, rc);
3230
3231                         lu_object_put(env, &obj->do_lu);
3232                         lod_comp->llc_stripe[j] = NULL;
3233                 }
3234                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3235                                         lod_comp->llc_stripes_allocated);
3236                 lod_comp->llc_stripe = NULL;
3237                 lod_comp->llc_stripes_allocated = 0;
3238                 lod_obj_set_pool(lo, i, NULL);
3239         }
3240
3241         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3242         if (left > 0) {
3243                 struct lod_layout_component     *comp_array;
3244
3245                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3246                 if (comp_array == NULL)
3247                         GOTO(out, rc = -ENOMEM);
3248
3249                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3250                        sizeof(*comp_array) * left);
3251
3252                 OBD_FREE(lo->ldo_comp_entries,
3253                          sizeof(*comp_array) * lo->ldo_comp_cnt);
3254                 lo->ldo_comp_entries = comp_array;
3255                 lo->ldo_comp_cnt = left;
3256                 lod_obj_inc_layout_gen(lo);
3257         } else {
3258                 lod_free_comp_entries(lo);
3259         }
3260
3261         LASSERT(dt_object_exists(dt));
3262         rc = dt_attr_get(env, next, attr);
3263         if (rc)
3264                 GOTO(out, rc);
3265
3266         if (attr->la_size > 0) {
3267                 attr->la_size = 0;
3268                 attr->la_valid = LA_SIZE;
3269                 rc = lod_sub_object_attr_set(env, next, attr, th);
3270                 if (rc)
3271                         GOTO(out, rc);
3272         }
3273
3274         rc = lod_generate_and_set_lovea(env, lo, th);
3275         EXIT;
3276 out:
3277         if (rc)
3278                 lod_object_free_striping(env, lo);
3279         return rc;
3280 }
3281
3282 /**
3283  * Implementation of dt_object_operations::do_xattr_set.
3284  *
3285  * Sets specified extended attribute on the object. Three types of EAs are
3286  * special:
3287  *   LOV EA - stores striping for a regular file or default striping (when set
3288  *            on a directory)
3289  *   LMV EA - stores a marker for the striped directories
3290  *   DMV EA - stores default directory striping
3291  *
3292  * When striping is applied to a non-striped existing object (this is called
3293  * late striping), then LOD notices the caller wants to turn the object into a
3294  * striped one. The stripe objects are created and appropriate EA is set:
3295  * LOV EA storing all the stripes directly or LMV EA storing just a small header
3296  * with striping configuration.
3297  *
3298  * \see dt_object_operations::do_xattr_set() in the API description for details.
3299  */
3300 static int lod_xattr_set(const struct lu_env *env,
3301                          struct dt_object *dt, const struct lu_buf *buf,
3302                          const char *name, int fl, struct thandle *th)
3303 {
3304         struct dt_object        *next = dt_object_child(dt);
3305         int                      rc;
3306         ENTRY;
3307
3308         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3309             strcmp(name, XATTR_NAME_LMV) == 0) {
3310                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3311
3312                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3313                                                 LMV_HASH_FLAG_MIGRATION)
3314                         rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3315                                                       th);
3316                 else
3317                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3318
3319                 RETURN(rc);
3320         }
3321
3322         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3323             strcmp(name, XATTR_NAME_LOV) == 0) {
3324                 /* default LOVEA */
3325                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3326                 RETURN(rc);
3327         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3328                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3329                 /* default LMVEA */
3330                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3331                                                       th);
3332                 RETURN(rc);
3333         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3334                    (!strcmp(name, XATTR_NAME_LOV) ||
3335                     !strncmp(name, XATTR_LUSTRE_LOV,
3336                              strlen(XATTR_LUSTRE_LOV)))) {
3337                 /* in case of lov EA swap, just set it
3338                  * if not, it is a replay so check striping match what we
3339                  * already have during req replay, declare_xattr_set()
3340                  * defines striping, then create() does the work */
3341                 if (fl & LU_XATTR_REPLACE) {
3342                         /* free stripes, then update disk */
3343                         lod_object_free_striping(env, lod_dt_obj(dt));
3344
3345                         rc = lod_sub_object_xattr_set(env, next, buf, name,
3346                                                       fl, th);
3347                 } else if (dt_object_remote(dt)) {
3348                         /* This only happens during migration, see
3349                          * mdd_migrate_create(), in which Master MDT will
3350                          * create a remote target object, and only set
3351                          * (migrating) stripe EA on the remote object,
3352                          * and does not need creating each stripes. */
3353                         rc = lod_sub_object_xattr_set(env, next, buf, name,
3354                                                       fl, th);
3355                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3356                         /* delete component(s) */
3357                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3358                         rc = lod_layout_del(env, dt, th);
3359                 } else {
3360                         /*
3361                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3362                          * it's going to create create file with specified
3363                          * component(s), the striping must have not being
3364                          * cached in this case;
3365                          *
3366                          * Otherwise, it's going to add/change component(s) to
3367                          * an existing file, the striping must have been cached
3368                          * in this case.
3369                          */
3370                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3371                                      !strcmp(name, XATTR_NAME_LOV),
3372                                 !lod_dt_obj(dt)->ldo_comp_cached));
3373
3374                         rc = lod_striping_create(env, dt, NULL, NULL, th);
3375                 }
3376                 RETURN(rc);
3377         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3378                 rc = lod_object_replace_parent_fid(env, dt, th, false);
3379
3380                 RETURN(rc);
3381         }
3382
3383         /* then all other xattr */
3384         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3385
3386         RETURN(rc);
3387 }
3388
3389 /**
3390  * Implementation of dt_object_operations::do_declare_xattr_del.
3391  *
3392  * \see dt_object_operations::do_declare_xattr_del() in the API description
3393  * for details.
3394  */
3395 static int lod_declare_xattr_del(const struct lu_env *env,
3396                                  struct dt_object *dt, const char *name,
3397                                  struct thandle *th)
3398 {
3399         struct lod_object       *lo = lod_dt_obj(dt);
3400         int                     rc;
3401         int                     i;
3402         ENTRY;
3403
3404         rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3405                                               name, th);
3406         if (rc != 0)
3407                 RETURN(rc);
3408
3409         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3410                 RETURN(0);
3411
3412         /* set xattr to each stripes, if needed */
3413         rc = lod_load_striping(env, lo);
3414         if (rc != 0)
3415                 RETURN(rc);
3416
3417         if (lo->ldo_dir_stripenr == 0)
3418                 RETURN(0);
3419
3420         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3421                 LASSERT(lo->ldo_stripe[i]);
3422                 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3423                                                       name, th);
3424                 if (rc != 0)
3425                         break;
3426         }
3427
3428         RETURN(rc);
3429 }
3430
3431 /**
3432  * Implementation of dt_object_operations::do_xattr_del.
3433  *
3434  * If EA storing a regular striping is being deleted, then release
3435  * all the references to the stripe objects in core.
3436  *
3437  * \see dt_object_operations::do_xattr_del() in the API description for details.
3438  */
3439 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3440                          const char *name, struct thandle *th)
3441 {
3442         struct dt_object        *next = dt_object_child(dt);
3443         struct lod_object       *lo = lod_dt_obj(dt);
3444         int                     rc;
3445         int                     i;
3446         ENTRY;
3447
3448         if (!strcmp(name, XATTR_NAME_LOV))
3449                 lod_object_free_striping(env, lod_dt_obj(dt));
3450
3451         rc = lod_sub_object_xattr_del(env, next, name, th);
3452         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3453                 RETURN(rc);
3454
3455         if (lo->ldo_dir_stripenr == 0)
3456                 RETURN(0);
3457
3458         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3459                 LASSERT(lo->ldo_stripe[i]);
3460
3461                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3462                 if (rc != 0)
3463                         break;
3464         }
3465
3466         RETURN(rc);
3467 }
3468
3469 /**
3470  * Implementation of dt_object_operations::do_xattr_list.
3471  *
3472  * \see dt_object_operations::do_xattr_list() in the API description
3473  * for details.
3474  */
3475 static int lod_xattr_list(const struct lu_env *env,
3476                           struct dt_object *dt, const struct lu_buf *buf)
3477 {
3478         return dt_xattr_list(env, dt_object_child(dt), buf);
3479 }
3480
3481 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3482 {
3483         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3484 }
3485
3486
3487 /**
3488  * Get default striping.
3489  *
3490  * \param[in] env               execution environment
3491  * \param[in] lo                object
3492  * \param[out] lds              default striping
3493  *
3494  * \retval              0 on success
3495  * \retval              negative if failed
3496  */
3497 static int lod_get_default_lov_striping(const struct lu_env *env,
3498                                         struct lod_object *lo,
3499                                         struct lod_default_striping *lds)
3500 {
3501         struct lod_thread_info *info = lod_env_info(env);
3502         struct lov_user_md_v1 *v1 = NULL;
3503         struct lov_user_md_v3 *v3 = NULL;
3504         struct lov_comp_md_v1 *comp_v1 = NULL;
3505         __u16   comp_cnt;
3506         bool    composite;
3507         int     rc, i;
3508         ENTRY;
3509
3510         lds->lds_def_striping_set = 0;
3511
3512         rc = lod_get_lov_ea(env, lo);
3513         if (rc < 0)
3514                 RETURN(rc);
3515
3516         if (rc < (typeof(rc))sizeof(struct lov_user_md))
3517                 RETURN(0);
3518
3519         v1 = info->lti_ea_store;
3520         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3521                 lustre_swab_lov_user_md_v1(v1);
3522         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3523                 v3 = (struct lov_user_md_v3 *)v1;
3524                 lustre_swab_lov_user_md_v3(v3);
3525         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3526                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3527                 lustre_swab_lov_comp_md_v1(comp_v1);
3528         }
3529
3530         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3531             v1->lmm_magic != LOV_MAGIC_COMP_V1)
3532                 RETURN(-ENOTSUPP);
3533
3534         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3535                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3536                 comp_cnt = comp_v1->lcm_entry_count;
3537                 if (comp_cnt == 0)
3538                         RETURN(-EINVAL);
3539                 composite = true;
3540         } else {
3541                 comp_cnt = 1;
3542                 composite = false;
3543         }
3544
3545         /* realloc default comp entries if necessary */
3546         rc = lod_def_striping_comp_resize(lds, comp_cnt);
3547         if (rc < 0)
3548                 RETURN(rc);
3549
3550         lds->lds_def_comp_cnt = comp_cnt;
3551         lds->lds_def_striping_is_composite = composite ? 1 : 0;
3552
3553         for (i = 0; i < comp_cnt; i++) {
3554                 struct lod_layout_component *lod_comp;
3555                 struct lu_extent *ext;
3556                 char *pool;
3557
3558                 lod_comp = &lds->lds_def_comp_entries[i];
3559                 /*
3560                  * reset lod_comp values, llc_stripes is always NULL in
3561                  * the default striping template, llc_pool will be reset
3562                  * later below.
3563                  */
3564                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3565
3566                 if (composite) {
3567                         v1 = (struct lov_user_md *)((char *)comp_v1 +
3568                                         comp_v1->lcm_entries[i].lcme_offset);
3569                         ext = &comp_v1->lcm_entries[i].lcme_extent;
3570                         lod_comp->llc_extent = *ext;
3571                 }
3572
3573                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3574                     v1->lmm_pattern != 0) {
3575                         lod_free_def_comp_entries(lds);
3576                         RETURN(-EINVAL);
3577                 }
3578
3579                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3580                        "stripe_offset=%d\n",
3581                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3582                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3583                        (int)v1->lmm_stripe_offset);
3584
3585                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3586                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3587                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3588
3589                 pool = NULL;
3590                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3591                         /* XXX: sanity check here */
3592                         v3 = (struct lov_user_md_v3 *) v1;
3593                         if (v3->lmm_pool_name[0] != '\0')
3594                                 pool = v3->lmm_pool_name;
3595                 }
3596                 lod_set_def_pool(lds, i, pool);
3597         }
3598
3599         lds->lds_def_striping_set = 1;
3600         RETURN(rc);
3601 }
3602
3603 /**
3604  * Get default directory striping.
3605  *
3606  * \param[in] env               execution environment
3607  * \param[in] lo                object
3608  * \param[out] lds              default striping
3609  *
3610  * \retval              0 on success
3611  * \retval              negative if failed
3612  */
3613 static int lod_get_default_lmv_striping(const struct lu_env *env,
3614                                         struct lod_object *lo,
3615                                         struct lod_default_striping *lds)
3616 {
3617         struct lod_thread_info  *info = lod_env_info(env);
3618         struct lmv_user_md_v1   *v1 = NULL;
3619         int                      rc;
3620         ENTRY;
3621
3622         lds->lds_dir_def_striping_set = 0;
3623         rc = lod_get_default_lmv_ea(env, lo);
3624         if (rc < 0)
3625                 RETURN(rc);
3626
3627         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3628                 RETURN(0);
3629
3630         v1 = info->lti_ea_store;
3631
3632         lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3633         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3634         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3635         lds->lds_dir_def_striping_set = 1;
3636
3637         RETURN(0);
3638 }
3639
3640 /**
3641  * Get default striping in the object.
3642  *
3643  * Get object default striping and default directory striping.
3644  *
3645  * \param[in] env               execution environment
3646  * \param[in] lo                object
3647  * \param[out] lds              default striping
3648  *
3649  * \retval              0 on success
3650  * \retval              negative if failed
3651  */
3652 static int lod_get_default_striping(const struct lu_env *env,
3653                                     struct lod_object *lo,
3654                                     struct lod_default_striping *lds)
3655 {
3656         int rc, rc1;
3657
3658         rc = lod_get_default_lov_striping(env, lo, lds);
3659         rc1 = lod_get_default_lmv_striping(env, lo, lds);
3660         if (rc == 0 && rc1 < 0)
3661                 rc = rc1;
3662
3663         return rc;
3664 }
3665
3666 /**
3667  * Apply default striping on object.
3668  *
3669  * If object striping pattern is not set, set to the one in default striping.
3670  * The default striping is from parent or fs.
3671  *
3672  * \param[in] lo                new object
3673  * \param[in] lds               default striping
3674  * \param[in] mode              new object's mode
3675  */
3676 static void lod_striping_from_default(struct lod_object *lo,
3677                                       const struct lod_default_striping *lds,
3678                                       umode_t mode)
3679 {
3680         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3681         struct lov_desc *desc = &d->lod_desc;
3682         int i, rc;
3683
3684         if (lds->lds_def_striping_set && S_ISREG(mode)) {
3685                 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3686                 if (rc != 0)
3687                         return;
3688
3689                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3690
3691                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3692                         struct lod_layout_component *obj_comp =
3693                                                 &lo->ldo_comp_entries[i];
3694                         struct lod_layout_component *def_comp =
3695                                                 &lds->lds_def_comp_entries[i];
3696
3697                         CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3698                                "nr:%u offset:%u %s\n",
3699                                def_comp->llc_stripe_size,
3700                                def_comp->llc_stripenr,
3701                                def_comp->llc_stripe_offset,
3702                                def_comp->llc_pool ?: "");
3703
3704                         *obj_comp = *def_comp;
3705                         if (def_comp->llc_pool != NULL) {
3706                                 /* pointer was copied from def_comp */
3707                                 obj_comp->llc_pool = NULL;
3708                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3709                         }
3710
3711                         /*
3712                          * Don't initialize these fields for plain layout
3713                          * (v1/v3) here, they are inherited in the order of
3714                          * 'parent' -> 'fs default (root)' -> 'global default
3715                          * values for stripe_count & stripe_size'.
3716                          *
3717                          * see lod_ah_init().
3718                          */
3719                         if (!lo->ldo_is_composite)
3720                                 continue;
3721
3722                         if (obj_comp->llc_stripenr <= 0)
3723                                 obj_comp->llc_stripenr =
3724                                         desc->ld_default_stripe_count;
3725                         if (obj_comp->llc_stripe_size <= 0)
3726                                 obj_comp->llc_stripe_size =
3727                                         desc->ld_default_stripe_size;
3728                 }
3729         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3730                 if (lo->ldo_dir_stripenr == 0)
3731                         lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3732                 if (lo->ldo_dir_stripe_offset == -1)
3733                         lo->ldo_dir_stripe_offset =
3734                                 lds->lds_dir_def_stripe_offset;
3735                 if (lo->ldo_dir_hash_type == 0)
3736                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3737
3738                 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3739                        "offset:%u, hash_type:%u\n",
3740                        lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3741                        lo->ldo_dir_hash_type);
3742         }
3743 }
3744
3745 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3746 {
3747         struct lod_layout_component *lod_comp;
3748
3749         if (lo->ldo_comp_cnt == 0)
3750                 return true;
3751
3752         if (lo->ldo_is_composite)
3753                 return false;
3754
3755         lod_comp = &lo->ldo_comp_entries[0];
3756
3757         if (lod_comp->llc_stripenr <= 0 ||
3758             lod_comp->llc_stripe_size <= 0)
3759                 return true;
3760
3761         if (from_root && (lod_comp->llc_pool == NULL ||
3762                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3763                 return true;
3764
3765         return false;
3766 }
3767
3768 /**
3769  * Implementation of dt_object_operations::do_ah_init.
3770  *
3771  * This method is used to make a decision on the striping configuration for the
3772  * object being created. It can be taken from the \a parent object if it exists,
3773  * or filesystem's default. The resulting configuration (number of stripes,
3774  * stripe size/offset, pool name, etc) is stored in the object itself and will
3775  * be used by the methods like ->doo_declare_create().
3776  *
3777  * \see dt_object_operations::do_ah_init() in the API description for details.
3778  */
3779 static void lod_ah_init(const struct lu_env *env,
3780                         struct dt_allocation_hint *ah,
3781                         struct dt_object *parent,
3782                         struct dt_object *child,
3783                         umode_t child_mode)
3784 {
3785         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3786         struct lod_thread_info *info = lod_env_info(env);
3787         struct lod_default_striping *lds = &info->lti_def_striping;
3788         struct dt_object *nextp = NULL;
3789         struct dt_object *nextc;
3790         struct lod_object *lp = NULL;
3791         struct lod_object *lc;
3792         struct lov_desc *desc;
3793         struct lod_layout_component *lod_comp;
3794         int rc;
3795         ENTRY;
3796
3797         LASSERT(child);
3798
3799         if (likely(parent)) {
3800                 nextp = dt_object_child(parent);
3801                 lp = lod_dt_obj(parent);
3802         }
3803
3804         nextc = dt_object_child(child);
3805         lc = lod_dt_obj(child);
3806
3807         LASSERT(!lod_obj_is_striped(child));
3808         /* default layout template may have been set on the regular file
3809          * when this is called from mdd_create_data() */
3810         if (S_ISREG(child_mode))
3811                 lod_free_comp_entries(lc);
3812
3813         if (!dt_object_exists(nextc))
3814                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3815
3816         if (S_ISDIR(child_mode)) {
3817                 /* other default values are 0 */
3818                 lc->ldo_dir_stripe_offset = -1;
3819
3820                 /* get default striping from parent object */
3821                 if (likely(lp != NULL))
3822                         lod_get_default_striping(env, lp, lds);
3823
3824                 /* set child default striping info, default value is NULL */
3825                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3826                         lc->ldo_def_striping = lds;
3827
3828                 /* It should always honour the specified stripes */
3829                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3830                     lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3831                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3832
3833                         lc->ldo_dir_stripenr =
3834                                 le32_to_cpu(lum1->lum_stripe_count);
3835                         lc->ldo_dir_stripe_offset =
3836                                 le32_to_cpu(lum1->lum_stripe_offset);
3837                         lc->ldo_dir_hash_type =
3838                                 le32_to_cpu(lum1->lum_hash_type);
3839                         CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3840                                 "hash_type %u\n",
3841                                 lc->ldo_dir_stripenr,
3842                                 (int)lc->ldo_dir_stripe_offset,
3843                                 lc->ldo_dir_hash_type);
3844                 } else {
3845                         /* transfer defaults LMV to new directory */
3846                         lod_striping_from_default(lc, lds, child_mode);
3847                 }
3848
3849                 /* shrink the stripe_count to the avaible MDT count */
3850                 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3851                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3852                         lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3853
3854                 /* Directory will be striped only if stripe_count > 1, if
3855                  * stripe_count == 1, let's reset stripenr = 0 to avoid
3856                  * create single master stripe and also help to unify the
3857                  * stripe handling of directories and files */
3858                 if (lc->ldo_dir_stripenr == 1)
3859                         lc->ldo_dir_stripenr = 0;
3860
3861                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3862                        lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3863                        lc->ldo_dir_hash_type);
3864
3865                 RETURN_EXIT;
3866         }
3867
3868         /* child object regular file*/
3869
3870         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3871                                         lu_object_fid(&child->do_lu)))
3872                 RETURN_EXIT;
3873
3874         /* If object is going to be striped over OSTs, transfer default
3875          * striping information to the child, so that we can use it
3876          * during declaration and creation.
3877          *
3878          * Try from the parent first.
3879          */
3880         if (likely(lp != NULL)) {
3881                 rc = lod_get_default_lov_striping(env, lp, lds);
3882                 if (rc == 0)
3883                         lod_striping_from_default(lc, lds, child_mode);
3884         }
3885
3886         /* Initialize lod_device::lod_md_root object reference */
3887         if (d->lod_md_root == NULL) {
3888                 struct dt_object *root;
3889                 struct lod_object *lroot;
3890
3891                 lu_root_fid(&info->lti_fid);
3892                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3893                 if (!IS_ERR(root)) {
3894                         lroot = lod_dt_obj(root);
3895
3896                         spin_lock(&d->lod_lock);
3897                         if (d->lod_md_root != NULL)
3898                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
3899                         d->lod_md_root = lroot;
3900                         spin_unlock(&d->lod_lock);
3901                 }
3902         }
3903
3904         /* try inherit layout from the root object (fs default) when:
3905          *  - parent does not have default layout; or
3906          *  - parent has plain(v1/v3) default layout, and some attributes
3907          *    are not specified in the default layout;
3908          */
3909         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3910                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3911                 if (rc)
3912                         goto out;
3913                 if (lc->ldo_comp_cnt == 0) {
3914                         lod_striping_from_default(lc, lds, child_mode);
3915                 } else if (!lds->lds_def_striping_is_composite) {
3916                         struct lod_layout_component *def_comp;
3917
3918                         LASSERT(!lc->ldo_is_composite);
3919                         lod_comp = &lc->ldo_comp_entries[0];
3920                         def_comp = &lds->lds_def_comp_entries[0];
3921
3922                         if (lod_comp->llc_stripenr <= 0)
3923                                 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3924                         if (lod_comp->llc_stripe_size <= 0)
3925                                 lod_comp->llc_stripe_size =
3926                                         def_comp->llc_stripe_size;
3927                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3928                                 lod_comp->llc_stripe_offset =
3929                                         def_comp->llc_stripe_offset;
3930                         if (lod_comp->llc_pool == NULL)
3931                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3932                 }
3933         }
3934 out:
3935         /*
3936          * fs default striping may not be explicitly set, or historically set
3937          * in config log, use them.
3938          */
3939         if (lod_need_inherit_more(lc, false)) {
3940
3941                 if (lc->ldo_comp_cnt == 0) {
3942                         rc = lod_alloc_comp_entries(lc, 1);
3943                         if (rc)
3944                                 /* fail to allocate memory, will create a
3945                                  * non-striped file. */
3946                                 RETURN_EXIT;
3947                         lc->ldo_is_composite = 0;
3948                         lod_comp = &lc->ldo_comp_entries[0];
3949                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
3950                 }
3951                 LASSERT(!lc->ldo_is_composite);
3952                 lod_comp = &lc->ldo_comp_entries[0];
3953                 desc = &d->lod_desc;
3954                 if (lod_comp->llc_stripenr <= 0)
3955                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
3956                 if (lod_comp->llc_stripe_size <= 0)
3957                         lod_comp->llc_stripe_size =
3958                                 desc->ld_default_stripe_size;
3959         }
3960
3961         EXIT;
3962 }
3963
3964 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
3965 /**
3966  * Size initialization on late striping.
3967  *
3968  * Propagate the size of a truncated object to a deferred striping.
3969  * This function handles a special case when truncate was done on a
3970  * non-striped object and now while the striping is being created
3971  * we can't lose that size, so we have to propagate it to the stripes
3972  * being created.
3973  *
3974  * \param[in] env       execution environment
3975  * \param[in] dt        object
3976  * \param[in] th        transaction handle
3977  *
3978  * \retval              0 on success
3979  * \retval              negative if failed
3980  */
3981 static int lod_declare_init_size(const struct lu_env *env,
3982                                  struct dt_object *dt, struct thandle *th)
3983 {
3984         struct dt_object        *next = dt_object_child(dt);
3985         struct lod_object       *lo = lod_dt_obj(dt);
3986         struct dt_object        **objects = NULL;
3987         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3988         uint64_t        size, offs;
3989         int     i, rc, stripe, stripenr = 0, stripe_size = 0;
3990         ENTRY;
3991
3992         if (!lod_obj_is_striped(dt))
3993                 RETURN(0);
3994
3995         rc = dt_attr_get(env, next, attr);
3996         LASSERT(attr->la_valid & LA_SIZE);
3997         if (rc)
3998                 RETURN(rc);
3999
4000         size = attr->la_size;
4001         if (size == 0)
4002                 RETURN(0);
4003
4004         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4005                 struct lod_layout_component *lod_comp;
4006                 struct lu_extent *extent;
4007
4008                 lod_comp = &lo->ldo_comp_entries[i];
4009
4010                 if (lod_comp->llc_stripe == NULL)
4011                         continue;
4012
4013                 extent = &lod_comp->llc_extent;
4014                 if (!lo->ldo_is_composite ||
4015                     (size >= extent->e_start && size < extent->e_end)) {
4016                         objects = lod_comp->llc_stripe;
4017                         stripenr = lod_comp->llc_stripenr;
4018                         stripe_size = lod_comp->llc_stripe_size;
4019                         break;
4020                 }
4021         }
4022
4023         if (stripenr == 0)
4024                 RETURN(0);
4025
4026         LASSERT(objects != NULL && stripe_size != 0);
4027
4028         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4029         ll_do_div64(size, (__u64)stripe_size);
4030         stripe = ll_do_div64(size, (__u64)stripenr);
4031         LASSERT(objects[stripe] != NULL);
4032
4033         size = size * stripe_size;
4034         offs = attr->la_size;
4035         size += ll_do_div64(offs, stripe_size);
4036
4037         attr->la_valid = LA_SIZE;
4038         attr->la_size = size;
4039
4040         rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4041
4042         RETURN(rc);
4043 }
4044
4045 /**
4046  * Declare creation of striped object.
4047  *
4048  * The function declares creation stripes for a regular object. The function
4049  * also declares whether the stripes will be created with non-zero size if
4050  * previously size was set non-zero on the master object. If object \a dt is
4051  * not local, then only fully defined striping can be applied in \a lovea.
4052  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4053  * for the details.
4054  *
4055  * \param[in] env       execution environment
4056  * \param[in] dt        object
4057  * \param[in] attr      attributes the stripes will be created with
4058  * \param[in] lovea     a buffer containing striping description
4059  * \param[in] th        transaction handle
4060  *
4061  * \retval              0 on success
4062  * \retval              negative if failed
4063  */
4064 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4065                                struct lu_attr *attr,
4066                                const struct lu_buf *lovea, struct thandle *th)
4067 {
4068         struct lod_thread_info  *info = lod_env_info(env);
4069         struct dt_object        *next = dt_object_child(dt);
4070         struct lod_object       *lo = lod_dt_obj(dt);
4071         int                      rc;
4072         ENTRY;
4073
4074         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4075                 GOTO(out, rc = -ENOMEM);
4076
4077         if (!dt_object_remote(next)) {
4078                 /* choose OST and generate appropriate objects */
4079                 rc = lod_prepare_create(env, lo, attr, lovea, th);
4080                 if (rc)
4081                         GOTO(out, rc);
4082
4083                 /*
4084                  * declare storage for striping data
4085                  */
4086                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4087         } else {
4088                 /* LOD can not choose OST objects for remote objects, i.e.
4089                  * stripes must be ready before that. Right now, it can only
4090                  * happen during migrate, i.e. migrate process needs to create
4091                  * remote regular file (mdd_migrate_create), then the migrate
4092                  * process will provide stripeEA. */
4093                 LASSERT(lovea != NULL);
4094                 info->lti_buf = *lovea;
4095         }
4096
4097         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4098                                               XATTR_NAME_LOV, 0, th);
4099         if (rc)
4100                 GOTO(out, rc);
4101
4102         /*
4103          * if striping is created with local object's size > 0,
4104          * we have to propagate this size to specific object
4105          * the case is possible only when local object was created previously
4106          */
4107         if (dt_object_exists(next))
4108                 rc = lod_declare_init_size(env, dt, th);
4109
4110 out:
4111         /* failed to create striping or to set initial size, let's reset
4112          * config so that others don't get confused */
4113         if (rc)
4114                 lod_object_free_striping(env, lo);
4115
4116         RETURN(rc);
4117 }
4118
4119 /**
4120  * Implementation of dt_object_operations::do_declare_create.
4121  *
4122  * The method declares creation of a new object. If the object will be striped,
4123  * then helper functions are called to find FIDs for the stripes, declare
4124  * creation of the stripes and declare initialization of the striping
4125  * information to be stored in the master object.
4126  *
4127  * \see dt_object_operations::do_declare_create() in the API description
4128  * for details.
4129  */
4130 static int lod_declare_object_create(const struct lu_env *env,
4131                                      struct dt_object *dt,
4132                                      struct lu_attr *attr,
4133                                      struct dt_allocation_hint *hint,
4134                                      struct dt_object_format *dof,
4135                                      struct thandle *th)
4136 {
4137         struct dt_object   *next = dt_object_child(dt);
4138         struct lod_object  *lo = lod_dt_obj(dt);
4139         int                 rc;
4140         ENTRY;
4141
4142         LASSERT(dof);
4143         LASSERT(attr);
4144         LASSERT(th);
4145
4146         /*
4147          * first of all, we declare creation of local object
4148          */
4149         rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4150         if (rc != 0)
4151                 GOTO(out, rc);
4152
4153         if (dof->dof_type == DFT_SYM)
4154                 dt->do_body_ops = &lod_body_lnk_ops;
4155         else if (dof->dof_type == DFT_REGULAR)
4156                 dt->do_body_ops = &lod_body_ops;
4157
4158         /*
4159          * it's lod_ah_init() that has decided the object will be striped
4160          */
4161         if (dof->dof_type == DFT_REGULAR) {
4162                 /* callers don't want stripes */
4163                 /* XXX: all tricky interactions with ->ah_make_hint() decided
4164                  * to use striping, then ->declare_create() behaving differently
4165                  * should be cleaned */
4166                 if (dof->u.dof_reg.striped != 0)
4167                         rc = lod_declare_striped_object(env, dt, attr,
4168                                                         NULL, th);
4169         } else if (dof->dof_type == DFT_DIR) {
4170                 struct seq_server_site *ss;
4171
4172                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4173
4174                 /* If the parent has default stripeEA, and client
4175                  * did not find it before sending create request,
4176                  * then MDT will return -EREMOTE, and client will
4177                  * retrieve the default stripeEA and re-create the
4178                  * sub directory.
4179                  *
4180                  * Note: if dah_eadata != NULL, it means creating the
4181                  * striped directory with specified stripeEA, then it
4182                  * should ignore the default stripeEA */
4183                 if (hint != NULL && hint->dah_eadata == NULL) {
4184                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4185                                 GOTO(out, rc = -EREMOTE);
4186
4187                         if (lo->ldo_dir_stripe_offset == -1) {
4188                                 /* child and parent should be in the same MDT */
4189                                 if (hint->dah_parent != NULL &&
4190                                     dt_object_remote(hint->dah_parent))
4191                                         GOTO(out, rc = -EREMOTE);
4192                         } else if (lo->ldo_dir_stripe_offset !=
4193                                    ss->ss_node_id) {
4194                                 struct lod_device *lod;
4195                                 struct lod_tgt_descs *ltd;
4196                                 struct lod_tgt_desc *tgt = NULL;
4197                                 bool found_mdt = false;
4198                                 int i;
4199
4200                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4201                                 ltd = &lod->lod_mdt_descs;
4202                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4203                                         tgt = LTD_TGT(ltd, i);
4204                                         if (tgt->ltd_index ==
4205                                                 lo->ldo_dir_stripe_offset) {
4206                                                 found_mdt = true;
4207                                                 break;
4208                                         }
4209                                 }
4210
4211                                 /* If the MDT indicated by stripe_offset can be
4212                                  * found, then tell client to resend the create
4213                                  * request to the correct MDT, otherwise return
4214                                  * error to client */
4215                                 if (found_mdt)
4216                                         GOTO(out, rc = -EREMOTE);
4217                                 else
4218                                         GOTO(out, rc = -EINVAL);
4219                         }
4220                 }
4221
4222                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4223         }
4224 out:
4225         /* failed to create striping or to set initial size, let's reset
4226          * config so that others don't get confused */
4227         if (rc)
4228                 lod_object_free_striping(env, lo);
4229         RETURN(rc);
4230 }
4231
4232 /**
4233  * Creation of a striped regular object.
4234  *
4235  * The function is called to create the stripe objects for a regular
4236  * striped file. This can happen at the initial object creation or
4237  * when the caller asks LOD to do so using ->do_xattr_set() method
4238  * (so called late striping). Notice all the information are already
4239  * prepared in the form of the list of objects (ldo_stripe field).
4240  * This is done during declare phase.
4241  *
4242  * \param[in] env       execution environment
4243  * \param[in] dt        object
4244  * \param[in] attr      attributes the stripes will be created with
4245  * \param[in] dof       format of stripes (see OSD API description)
4246  * \param[in] th        transaction handle
4247  *
4248  * \retval              0 on success
4249  * \retval              negative if failed
4250  */
4251 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4252                         struct lu_attr *attr, struct dt_object_format *dof,
4253                         struct thandle *th)
4254 {
4255         struct lod_layout_component     *lod_comp;
4256         struct lod_object       *lo = lod_dt_obj(dt);
4257         int     rc = 0, i, j;
4258         ENTRY;
4259
4260         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4261
4262         /* create all underlying objects */
4263         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4264                 lod_comp = &lo->ldo_comp_entries[i];
4265
4266                 if (lod_comp->llc_flags & LCME_FL_INIT)
4267                         continue;
4268
4269                 lod_comp->llc_flags |= LCME_FL_INIT;
4270
4271                 if (lod_comp->llc_stripe == NULL)
4272                         continue;
4273
4274                 LASSERT(lod_comp->llc_stripenr > 0);
4275                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4276                         struct dt_object *object = lod_comp->llc_stripe[j];
4277                         LASSERT(object != NULL);
4278                         rc = lod_sub_object_create(env, object, attr, NULL,
4279                                                    dof, th);
4280                         if (rc)
4281                                 break;
4282                 }
4283         }
4284
4285         if (rc == 0)
4286                 rc = lod_generate_and_set_lovea(env, lo, th);
4287
4288         if (rc == 0)
4289                 lo->ldo_comp_cached = 1;
4290         else
4291                 lod_object_free_striping(env, lo);
4292
4293         RETURN(rc);
4294 }
4295
4296 /**
4297  * Implementation of dt_object_operations::do_create.
4298  *
4299  * If any of preceeding methods (like ->do_declare_create(),
4300  * ->do_ah_init(), etc) chose to create a striped object,
4301  * then this method will create the master and the stripes.
4302  *
4303  * \see dt_object_operations::do_create() in the API description for details.
4304  */
4305 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4306                              struct lu_attr *attr,
4307                              struct dt_allocation_hint *hint,
4308                              struct dt_object_format *dof, struct thandle *th)
4309 {
4310         int                 rc;
4311         ENTRY;
4312
4313         /* create local object */
4314         rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4315                                    th);
4316         if (rc != 0)
4317                 RETURN(rc);
4318
4319         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4320             lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4321                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4322                 rc = lod_striping_create(env, dt, attr, dof, th);
4323         }
4324
4325         RETURN(rc);
4326 }
4327
4328 static inline int
4329 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4330                           struct dt_object *dt, struct thandle *th,
4331                           int stripe_idx, struct lod_obj_stripe_cb_data *data)
4332 {
4333         if (data->locd_declare)
4334                 return lod_sub_object_declare_destroy(env, dt, th);
4335         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4336                  stripe_idx == cfs_fail_val)
4337                 return lod_sub_object_destroy(env, dt, th);
4338         else
4339                 return 0;
4340 }
4341
4342 /**
4343  * Implementation of dt_object_operations::do_declare_destroy.
4344  *
4345  * If the object is a striped directory, then the function declares reference
4346  * removal from the master object (this is an index) to the stripes and declares
4347  * destroy of all the stripes. In all the cases, it declares an intention to
4348  * destroy the object itself.
4349  *
4350  * \see dt_object_operations::do_declare_destroy() in the API description
4351  * for details.
4352  */
4353 static int lod_declare_object_destroy(const struct lu_env *env,
4354                                       struct dt_object *dt,
4355                                       struct thandle *th)
4356 {
4357         struct dt_object   *next = dt_object_child(dt);
4358         struct lod_object  *lo = lod_dt_obj(dt);
4359         struct lod_thread_info *info = lod_env_info(env);
4360         char               *stripe_name = info->lti_key;
4361         int                 rc, i;
4362         ENTRY;
4363
4364         /*
4365          * load striping information, notice we don't do this when object
4366          * is being initialized as we don't need this information till
4367          * few specific cases like destroy, chown
4368          */
4369         rc = lod_load_striping(env, lo);
4370         if (rc)
4371                 RETURN(rc);
4372
4373         /* declare destroy for all underlying objects */
4374         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4375                 rc = next->do_ops->do_index_try(env, next,
4376                                                 &dt_directory_features);
4377                 if (rc != 0)
4378                         RETURN(rc);
4379
4380                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4381                         rc = lod_sub_object_declare_ref_del(env, next, th);
4382                         if (rc != 0)
4383                                 RETURN(rc);
4384
4385                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4386                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4387                                 i);
4388                         rc = lod_sub_object_declare_delete(env, next,
4389                                         (const struct dt_key *)stripe_name, th);
4390                         if (rc != 0)
4391                                 RETURN(rc);
4392                 }
4393         }
4394
4395         /*
4396          * we declare destroy for the local object
4397          */
4398         rc = lod_sub_object_declare_destroy(env, next, th);
4399         if (rc)
4400                 RETURN(rc);
4401
4402         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4403             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4404                 RETURN(0);
4405
4406         if (!lod_obj_is_striped(dt))
4407                 RETURN(0);
4408
4409         /* declare destroy all striped objects */
4410         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4411                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4412                         if (lo->ldo_stripe[i] == NULL)
4413                                 continue;
4414
4415                         rc = lod_sub_object_declare_ref_del(env,
4416                                         lo->ldo_stripe[i], th);
4417
4418                         rc = lod_sub_object_declare_destroy(env,
4419                                         lo->ldo_stripe[i], th);
4420                         if (rc != 0)
4421                                 break;
4422                 }
4423         } else {
4424                 struct lod_obj_stripe_cb_data data;
4425
4426                 data.locd_declare = true;
4427                 rc = lod_obj_for_each_stripe(env, lo, th,
4428                                 lod_obj_stripe_destroy_cb, &data);
4429         }
4430
4431         RETURN(rc);
4432 }
4433
4434 /**
4435  * Implementation of dt_object_operations::do_destroy.
4436  *
4437  * If the object is a striped directory, then the function removes references
4438  * from the master object (this is an index) to the stripes and destroys all
4439  * the stripes. In all the cases, the function destroys the object itself.
4440  *
4441  * \see dt_object_operations::do_destroy() in the API description for details.
4442  */
4443 static int lod_object_destroy(const struct lu_env *env,
4444                 struct dt_object *dt, struct thandle *th)
4445 {
4446         struct dt_object  *next = dt_object_child(dt);
4447         struct lod_object *lo = lod_dt_obj(dt);
4448         struct lod_thread_info *info = lod_env_info(env);
4449         char               *stripe_name = info->lti_key;
4450         unsigned int       i;
4451         int                rc;
4452         ENTRY;
4453
4454         /* destroy sub-stripe of master object */
4455         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4456                 rc = next->do_ops->do_index_try(env, next,
4457                                                 &dt_directory_features);
4458                 if (rc != 0)
4459                         RETURN(rc);
4460
4461                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4462                         rc = lod_sub_object_ref_del(env, next, th);
4463                         if (rc != 0)
4464                                 RETURN(rc);
4465
4466                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4467                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4468                                 i);
4469
4470                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4471                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4472                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4473
4474                         rc = lod_sub_object_delete(env, next,
4475                                        (const struct dt_key *)stripe_name, th);
4476                         if (rc != 0)
4477                                 RETURN(rc);
4478                 }
4479         }
4480
4481         rc = lod_sub_object_destroy(env, next, th);
4482         if (rc != 0)
4483                 RETURN(rc);
4484
4485         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4486             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4487                 RETURN(0);
4488
4489         if (!lod_obj_is_striped(dt))
4490                 RETURN(0);
4491
4492         /* destroy all striped objects */
4493         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4494                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4495                         if (lo->ldo_stripe[i] == NULL)
4496                                 continue;
4497                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4498                             i == cfs_fail_val) {
4499                                 dt_write_lock(env, lo->ldo_stripe[i],
4500                                               MOR_TGT_CHILD);
4501                                 rc = lod_sub_object_ref_del(env,
4502                                                 lo->ldo_stripe[i], th);
4503                                 dt_write_unlock(env, lo->ldo_stripe[i]);
4504                                 if (rc != 0)
4505                                         break;
4506
4507                                 rc = lod_sub_object_destroy(env,
4508                                                 lo->ldo_stripe[i], th);
4509                                 if (rc != 0)
4510                                         break;
4511                         }
4512                 }
4513         } else {
4514                 struct lod_obj_stripe_cb_data data;
4515
4516                 data.locd_declare = false;
4517                 rc = lod_obj_for_each_stripe(env, lo, th,
4518                                 lod_obj_stripe_destroy_cb, &data);
4519         }
4520
4521         RETURN(rc);
4522 }
4523
4524 /**
4525  * Implementation of dt_object_operations::do_declare_ref_add.
4526  *
4527  * \see dt_object_operations::do_declare_ref_add() in the API description
4528  * for details.
4529  */
4530 static int lod_declare_ref_add(const struct lu_env *env,
4531                                struct dt_object *dt, struct thandle *th)
4532 {
4533         return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4534 }
4535
4536 /**
4537  * Implementation of dt_object_operations::do_ref_add.
4538  *
4539  * \see dt_object_operations::do_ref_add() in the API description for details.
4540  */
4541 static int lod_ref_add(const struct lu_env *env,
4542                        struct dt_object *dt, struct thandle *th)
4543 {
4544         return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4545 }
4546
4547 /**
4548  * Implementation of dt_object_operations::do_declare_ref_del.
4549  *
4550  * \see dt_object_operations::do_declare_ref_del() in the API description
4551  * for details.
4552  */
4553 static int lod_declare_ref_del(const struct lu_env *env,
4554                                struct dt_object *dt, struct thandle *th)
4555 {
4556         return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4557 }
4558
4559 /**
4560  * Implementation of dt_object_operations::do_ref_del
4561  *
4562  * \see dt_object_operations::do_ref_del() in the API description for details.
4563  */
4564 static int lod_ref_del(const struct lu_env *env,
4565                        struct dt_object *dt, struct thandle *th)
4566 {
4567         return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4568 }
4569
4570 /**
4571  * Implementation of dt_object_operations::do_object_sync.
4572  *
4573  * \see dt_object_operations::do_object_sync() in the API description
4574  * for details.
4575  */
4576 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4577                            __u64 start, __u64 end)
4578 {
4579         return dt_object_sync(env, dt_object_child(dt), start, end);
4580 }
4581
4582 /**
4583  * Release LDLM locks on the stripes of a striped directory.
4584  *
4585  * Iterates over all the locks taken on the stripe objects and
4586  * cancel them.
4587  *
4588  * \param[in] env       execution environment
4589  * \param[in] dt        striped object
4590  * \param[in] einfo     lock description
4591  * \param[in] policy    data describing requested lock
4592  *
4593  * \retval              0 on success
4594  * \retval              negative if failed
4595  */
4596 static int lod_object_unlock_internal(const struct lu_env *env,
4597                                       struct dt_object *dt,
4598                                       struct ldlm_enqueue_info *einfo,
4599                                       union ldlm_policy_data *policy)
4600 {
4601         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4602         int                     rc = 0;
4603         int                     i;
4604         ENTRY;
4605
4606         if (slave_locks == NULL)
4607                 RETURN(0);
4608
4609         for (i = 1; i < slave_locks->count; i++) {
4610                 if (lustre_handle_is_used(&slave_locks->handles[i]))
4611                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4612                                                     einfo->ei_mode);
4613         }
4614
4615         RETURN(rc);
4616 }
4617
4618 /**
4619  * Implementation of dt_object_operations::do_object_unlock.
4620  *
4621  * Used to release LDLM lock(s).
4622  *
4623  * \see dt_object_operations::do_object_unlock() in the API description
4624  * for details.
4625  */
4626 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4627                              struct ldlm_enqueue_info *einfo,
4628                              union ldlm_policy_data *policy)
4629 {
4630         struct lod_object *lo = lod_dt_obj(dt);
4631         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4632         int slave_locks_size;
4633         int i;
4634         ENTRY;
4635
4636         if (slave_locks == NULL)
4637                 RETURN(0);
4638
4639         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4640         LASSERT(lo->ldo_dir_stripenr > 1);
4641         /* Note: for remote lock for single stripe dir, MDT will cancel
4642          * the lock by lockh directly */
4643         LASSERT(!dt_object_remote(dt_object_child(dt)));
4644
4645         /* locks were unlocked in MDT layer */
4646         for (i = 1; i < slave_locks->count; i++) {
4647                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4648                 dt_invalidate(env, lo->ldo_stripe[i]);
4649         }
4650
4651         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4652                            sizeof(slave_locks->handles[0]);
4653         OBD_FREE(slave_locks, slave_locks_size);
4654         einfo->ei_cbdata = NULL;
4655
4656         RETURN(0);
4657 }
4658
4659 /**
4660  * Implementation of dt_object_operations::do_object_lock.
4661  *
4662  * Used to get LDLM lock on the non-striped and striped objects.
4663  *
4664  * \see dt_object_operations::do_object_lock() in the API description
4665  * for details.
4666  */
4667 static int lod_object_lock(const struct lu_env *env,
4668                            struct dt_object *dt,
4669                            struct lustre_handle *lh,
4670                            struct ldlm_enqueue_info *einfo,
4671                            union ldlm_policy_data *policy)
4672 {
4673         struct lod_object       *lo = lod_dt_obj(dt);
4674         int                     rc = 0;
4675         int                     i;
4676         int                     slave_locks_size;
4677         struct lustre_handle_array *slave_locks = NULL;
4678         ENTRY;
4679
4680         /* remote object lock */
4681         if (!einfo->ei_enq_slave) {
4682                 LASSERT(dt_object_remote(dt));
4683                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4684                                       policy);
4685         }
4686
4687         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4688                 GOTO(out, rc = -ENOTDIR);
4689
4690         rc = lod_load_striping(env, lo);
4691         if (rc != 0)
4692                 GOTO(out, rc);
4693
4694         /* No stripes */
4695         if (lo->ldo_dir_stripenr <= 1) {
4696                 /*
4697                  * NB, ei_cbdata stores pointer to slave locks, if no locks
4698                  * taken, make sure it's set to NULL, otherwise MDT will try to
4699                  * unlock them.
4700                  */
4701                 einfo->ei_cbdata = NULL;
4702                 GOTO(out, rc = 0);
4703         }
4704
4705         slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4706                            sizeof(slave_locks->handles[0]);
4707         /* Freed in lod_object_unlock */
4708         OBD_ALLOC(slave_locks, slave_locks_size);
4709         if (slave_locks == NULL)
4710                 GOTO(out, rc = -ENOMEM);
4711         slave_locks->count = lo->ldo_dir_stripenr;
4712
4713         /* striped directory lock */
4714         for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4715                 struct lustre_handle    lockh;
4716                 struct ldlm_res_id      *res_id;
4717
4718                 res_id = &lod_env_info(env)->lti_res_id;
4719                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4720                                        res_id);
4721                 einfo->ei_res_id = res_id;
4722
4723                 LASSERT(lo->ldo_stripe[i] != NULL);
4724                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4725                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4726                                             einfo, policy);
4727                 } else {
4728                         struct ldlm_namespace *ns = einfo->ei_namespace;
4729                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4730                         ldlm_completion_callback completion = einfo->ei_cb_cp;
4731                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
4732
4733                         if (einfo->ei_mode == LCK_PW ||
4734                             einfo->ei_mode == LCK_EX)
4735                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
4736
4737                         /* This only happens if there are mulitple stripes
4738                          * on the master MDT, i.e. except stripe0, there are
4739                          * other stripes on the Master MDT as well, Only
4740                          * happens in the test case right now. */
4741                         LASSERT(ns != NULL);
4742                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4743                                                     policy, einfo->ei_mode,
4744                                                     &dlmflags, blocking,
4745                                                     completion, NULL,
4746                                                     NULL, 0, LVB_T_NONE,
4747                                                     NULL, &lockh);
4748                 }
4749                 if (rc != 0)
4750                         break;
4751                 slave_locks->handles[i] = lockh;
4752         }
4753         einfo->ei_cbdata = slave_locks;
4754
4755         if (rc != 0 && slave_locks != NULL) {
4756                 lod_object_unlock_internal(env, dt, einfo, policy);
4757                 OBD_FREE(slave_locks, slave_locks_size);
4758         }
4759         EXIT;
4760 out:
4761         if (rc != 0)
4762                 einfo->ei_cbdata = NULL;
4763         RETURN(rc);
4764 }
4765
4766 /**
4767  * Implementation of dt_object_operations::do_invalidate.
4768  *
4769  * \see dt_object_operations::do_invalidate() in the API description for details
4770  */
4771 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4772 {
4773         return dt_invalidate(env, dt_object_child(dt));
4774 }
4775
4776 struct dt_object_operations lod_obj_ops = {
4777         .do_read_lock           = lod_object_read_lock,
4778         .do_write_lock          = lod_object_write_lock,
4779         .do_read_unlock         = lod_object_read_unlock,
4780         .do_write_unlock        = lod_object_write_unlock,
4781         .do_write_locked        = lod_object_write_locked,
4782         .do_attr_get            = lod_attr_get,
4783         .do_declare_attr_set    = lod_declare_attr_set,
4784         .do_attr_set            = lod_attr_set,
4785         .do_xattr_get           = lod_xattr_get,
4786         .do_declare_xattr_set   = lod_declare_xattr_set,
4787         .do_xattr_set           = lod_xattr_set,
4788         .do_declare_xattr_del   = lod_declare_xattr_del,
4789         .do_xattr_del           = lod_xattr_del,
4790         .do_xattr_list          = lod_xattr_list,
4791         .do_ah_init             = lod_ah_init,
4792         .do_declare_create      = lod_declare_object_create,
4793         .do_create              = lod_object_create,
4794         .do_declare_destroy     = lod_declare_object_destroy,
4795         .do_destroy             = lod_object_destroy,
4796         .do_index_try           = lod_index_try,
4797         .do_declare_ref_add     = lod_declare_ref_add,
4798         .do_ref_add             = lod_ref_add,
4799         .do_declare_ref_del     = lod_declare_ref_del,
4800         .do_ref_del             = lod_ref_del,
4801         .do_object_sync         = lod_object_sync,
4802         .do_object_lock         = lod_object_lock,
4803         .do_object_unlock       = lod_object_unlock,
4804         .do_invalidate          = lod_invalidate,
4805 };
4806
4807 /**
4808  * Implementation of dt_body_operations::dbo_read.
4809  *
4810  * \see dt_body_operations::dbo_read() in the API description for details.
4811  */
4812 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
4813                         struct lu_buf *buf, loff_t *pos)
4814 {
4815         struct dt_object *next = dt_object_child(dt);
4816         return next->do_body_ops->dbo_read(env, next, buf, pos);
4817 }
4818
4819 /**
4820  * Implementation of dt_body_operations::dbo_declare_write.
4821  *
4822  * \see dt_body_operations::dbo_declare_write() in the API description
4823  * for details.
4824  */
4825 static ssize_t lod_declare_write(const struct lu_env *env,
4826                                  struct dt_object *dt,
4827                                  const struct lu_buf *buf, loff_t pos,
4828                                  struct thandle *th)
4829 {
4830         return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
4831                                             th);
4832 }
4833
4834 /**
4835  * Implementation of dt_body_operations::dbo_write.
4836  *
4837  * \see dt_body_operations::dbo_write() in the API description for details.
4838  */
4839 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
4840                          const struct lu_buf *buf, loff_t *pos,
4841                          struct thandle *th, int iq)
4842 {
4843         return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
4844 }
4845
4846 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
4847                              __u64 start, __u64 end, struct thandle *th)
4848 {
4849         if (dt_object_remote(dt))
4850                 return -ENOTSUPP;
4851
4852         return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
4853                                             end, th);
4854 }
4855
4856 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
4857                      __u64 start, __u64 end, struct thandle *th)
4858 {
4859         if (dt_object_remote(dt))
4860                 return -ENOTSUPP;
4861
4862         return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
4863 }
4864
4865 static const struct dt_body_operations lod_body_lnk_ops = {
4866         .dbo_read               = lod_read,
4867         .dbo_declare_write      = lod_declare_write,
4868         .dbo_write              = lod_write
4869 };
4870
4871 static const struct dt_body_operations lod_body_ops = {
4872         .dbo_read               = lod_read,
4873         .dbo_declare_write      = lod_declare_write,
4874         .dbo_write              = lod_write,
4875         .dbo_declare_punch      = lod_declare_punch,
4876         .dbo_punch              = lod_punch,
4877 };
4878
4879 /**
4880  * Implementation of lu_object_operations::loo_object_init.
4881  *
4882  * The function determines the type and the index of the target device using
4883  * sequence of the object's FID. Then passes control down to the
4884  * corresponding device:
4885  *  OSD for the local objects, OSP for remote
4886  *
4887  * \see lu_object_operations::loo_object_init() in the API description
4888  * for details.
4889  */
4890 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
4891                            const struct lu_object_conf *conf)
4892 {
4893         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
4894         struct lu_device        *cdev   = NULL;
4895         struct lu_object        *cobj;
4896         struct lod_tgt_descs    *ltd    = NULL;
4897         struct lod_tgt_desc     *tgt;
4898         u32                      idx    = 0;
4899         int                      type   = LU_SEQ_RANGE_ANY;
4900         int                      rc;
4901         ENTRY;
4902
4903         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
4904         if (rc != 0) {
4905                 /* Note: Sometimes, it will Return EAGAIN here, see
4906                  * ptrlpc_import_delay_req(), which might confuse
4907                  * lu_object_find_at() and make it wait there incorrectly.
4908                  * so we convert it to EIO here.*/
4909                 if (rc == -EAGAIN)
4910                         rc = -EIO;
4911
4912                 RETURN(rc);
4913         }
4914
4915         if (type == LU_SEQ_RANGE_MDT &&
4916             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
4917                 cdev = &lod->lod_child->dd_lu_dev;
4918         } else if (type == LU_SEQ_RANGE_MDT) {
4919                 ltd = &lod->lod_mdt_descs;
4920                 lod_getref(ltd);
4921         } else if (type == LU_SEQ_RANGE_OST) {
4922                 ltd = &lod->lod_ost_descs;
4923                 lod_getref(ltd);
4924         } else {
4925                 LBUG();
4926         }
4927
4928         if (ltd != NULL) {
4929                 if (ltd->ltd_tgts_size > idx &&
4930                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
4931                         tgt = LTD_TGT(ltd, idx);
4932
4933                         LASSERT(tgt != NULL);
4934                         LASSERT(tgt->ltd_tgt != NULL);
4935
4936                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
4937                 }
4938                 lod_putref(lod, ltd);
4939         }
4940
4941         if (unlikely(cdev == NULL))
4942                 RETURN(-ENOENT);
4943
4944         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
4945         if (unlikely(cobj == NULL))
4946                 RETURN(-ENOMEM);
4947
4948         lu_object_add(lo, cobj);
4949
4950         RETURN(0);
4951 }
4952
4953 /**
4954  *
4955  * Release resources associated with striping.
4956  *
4957  * If the object is striped (regular or directory), then release
4958  * the stripe objects references and free the ldo_stripe array.
4959  *
4960  * \param[in] env       execution environment
4961  * \param[in] lo        object
4962  */
4963 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
4964 {
4965         struct lod_layout_component *lod_comp;
4966         int i, j;
4967
4968         if (lo->ldo_stripe != NULL) {
4969                 LASSERT(lo->ldo_comp_entries == NULL);
4970                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
4971
4972                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4973                         if (lo->ldo_stripe[i])
4974                                 dt_object_put(env, lo->ldo_stripe[i]);
4975                 }
4976
4977                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
4978                 OBD_FREE(lo->ldo_stripe, j);
4979                 lo->ldo_stripe = NULL;
4980                 lo->ldo_dir_stripes_allocated = 0;
4981                 lo->ldo_dir_stripenr = 0;
4982         } else if (lo->ldo_comp_entries != NULL) {
4983                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
4984                         /* free lod_layout_component::llc_stripe array */
4985                         lod_comp = &lo->ldo_comp_entries[i];
4986
4987                         if (lod_comp->llc_stripe == NULL)
4988                                 continue;
4989                         LASSERT(lod_comp->llc_stripes_allocated != 0);
4990                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
4991                                 if (lod_comp->llc_stripe[j] != NULL)
4992                                         lu_object_put(env,
4993                                                &lod_comp->llc_stripe[j]->do_lu);
4994                         }
4995                         OBD_FREE(lod_comp->llc_stripe,
4996                                  sizeof(struct dt_object *) *
4997                                  lod_comp->llc_stripes_allocated);
4998                         lod_comp->llc_stripe = NULL;
4999                         lod_comp->llc_stripes_allocated = 0;
5000                 }
5001                 lod_free_comp_entries(lo);
5002                 lo->ldo_comp_cached = 0;
5003         }
5004 }
5005
5006 /**
5007  * Implementation of lu_object_operations::loo_object_start.
5008  *
5009  * \see lu_object_operations::loo_object_start() in the API description
5010  * for details.
5011  */
5012 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5013 {
5014         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5015                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5016         } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5017                    fid_is_local_file(lu_object_fid(o))) {
5018                 /* Note: some local file (like last rcvd) is created
5019                  * through bottom layer (OSD), so the object initialization
5020                  * comes to lod, it does not set loh_attr yet, so
5021                  * set do_body_ops for local file anyway */
5022                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5023         }
5024         return 0;
5025 }
5026
5027 /**
5028  * Implementation of lu_object_operations::loo_object_free.
5029  *
5030  * \see lu_object_operations::loo_object_free() in the API description
5031  * for details.
5032  */
5033 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5034 {
5035         struct lod_object *lo = lu2lod_obj(o);
5036
5037         /* release all underlying object pinned */
5038         lod_object_free_striping(env, lo);
5039         lu_object_fini(o);
5040         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5041 }
5042
5043 /**
5044  * Implementation of lu_object_operations::loo_object_release.
5045  *
5046  * \see lu_object_operations::loo_object_release() in the API description
5047  * for details.
5048  */
5049 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5050 {
5051         /* XXX: shouldn't we release everything here in case if object
5052          * creation failed before? */
5053 }
5054
5055 /**
5056  * Implementation of lu_object_operations::loo_object_print.
5057  *
5058  * \see lu_object_operations::loo_object_print() in the API description
5059  * for details.
5060  */
5061 static int lod_object_print(const struct lu_env *env, void *cookie,
5062                             lu_printer_t p, const struct lu_object *l)
5063 {
5064         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5065
5066         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5067 }
5068
5069 struct lu_object_operations lod_lu_obj_ops = {
5070         .loo_object_init        = lod_object_init,
5071         .loo_object_start       = lod_object_start,
5072         .loo_object_free        = lod_object_free,
5073         .loo_object_release     = lod_object_release,
5074         .loo_object_print       = lod_object_print,
5075 };