Whamcloud - gitweb
LU-4017 quota: cleanup to improve quota codes
[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, PROJID, and size, and osp_attr_set() ignores all
1135          * but UID, GID and PROJID. Declaration of size attr setting
1136          * happens through lod_declare_init_size(), and not through
1137          * this function. Therefore we need not load striping unless
1138          * ownership is changing.  This should save memory and (we hope)
1139          * speed up rename().
1140          */
1141         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1142                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1143                         RETURN(rc);
1144
1145                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1146                         RETURN(0);
1147         } else {
1148                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID | LA_MODE |
1149                                         LA_ATIME | LA_MTIME | LA_CTIME |
1150                                         LA_FLAGS)))
1151                         RETURN(rc);
1152         }
1153         /*
1154          * load striping information, notice we don't do this when object
1155          * is being initialized as we don't need this information till
1156          * few specific cases like destroy, chown
1157          */
1158         rc = lod_load_striping(env, lo);
1159         if (rc)
1160                 RETURN(rc);
1161
1162         if (!lod_obj_is_striped(dt))
1163                 RETURN(0);
1164
1165         /*
1166          * if object is striped declare changes on the stripes
1167          */
1168         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1169                 LASSERT(lo->ldo_stripe);
1170                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1171                         if (lo->ldo_stripe[i] == NULL)
1172                                 continue;
1173                         rc = lod_sub_object_declare_attr_set(env,
1174                                         lo->ldo_stripe[i], attr,
1175                                         th);
1176                         if (rc != 0)
1177                                 RETURN(rc);
1178                 }
1179         } else {
1180                 struct lod_obj_stripe_cb_data data;
1181
1182                 data.locd_attr = attr;
1183                 data.locd_declare = true;
1184                 rc = lod_obj_for_each_stripe(env, lo, th,
1185                                 lod_obj_stripe_attr_set_cb, &data);
1186         }
1187
1188         if (rc)
1189                 RETURN(rc);
1190
1191         if (!dt_object_exists(next) || dt_object_remote(next) ||
1192             !S_ISREG(attr->la_mode))
1193                 RETURN(0);
1194
1195         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1196                 rc = lod_sub_object_declare_xattr_del(env, next,
1197                                                       XATTR_NAME_LOV, th);
1198                 RETURN(rc);
1199         }
1200
1201         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE) ||
1202             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1203                 struct lod_thread_info *info = lod_env_info(env);
1204                 struct lu_buf *buf = &info->lti_buf;
1205
1206                 buf->lb_buf = info->lti_ea_store;
1207                 buf->lb_len = info->lti_ea_store_size;
1208                 rc = lod_sub_object_declare_xattr_set(env, next, buf,
1209                                                       XATTR_NAME_LOV,
1210                                                       LU_XATTR_REPLACE, th);
1211         }
1212
1213         RETURN(rc);
1214 }
1215
1216 /**
1217  * Implementation of dt_object_operations::do_attr_set.
1218  *
1219  * If the object is striped, then apply the changes to all or subset of
1220  * the stripes depending on the object type and specific attributes.
1221  *
1222  * \see dt_object_operations::do_attr_set() in the API description for details.
1223  */
1224 static int lod_attr_set(const struct lu_env *env,
1225                         struct dt_object *dt,
1226                         const struct lu_attr *attr,
1227                         struct thandle *th)
1228 {
1229         struct dt_object        *next = dt_object_child(dt);
1230         struct lod_object       *lo = lod_dt_obj(dt);
1231         int                     rc, i;
1232         ENTRY;
1233
1234         /*
1235          * apply changes to the local object
1236          */
1237         rc = lod_sub_object_attr_set(env, next, attr, th);
1238         if (rc)
1239                 RETURN(rc);
1240
1241         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1242                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_PROJID)))
1243                         RETURN(rc);
1244
1245                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_OWNER))
1246                         RETURN(0);
1247         } else {
1248                 if (!(attr->la_valid & (LA_UID | LA_GID | LA_MODE | LA_PROJID |
1249                                         LA_ATIME | LA_MTIME | LA_CTIME |
1250                                         LA_FLAGS)))
1251                         RETURN(rc);
1252         }
1253
1254         if (!lod_obj_is_striped(dt))
1255                 RETURN(0);
1256
1257         /*
1258          * if object is striped, apply changes to all the stripes
1259          */
1260         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
1261                 LASSERT(lo->ldo_stripe);
1262                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1263                         if (unlikely(lo->ldo_stripe[i] == NULL))
1264                                 continue;
1265
1266                         if ((dt_object_exists(lo->ldo_stripe[i]) == 0))
1267                                 continue;
1268
1269                         rc = lod_sub_object_attr_set(env, lo->ldo_stripe[i],
1270                                                      attr, th);
1271                         if (rc != 0)
1272                                 break;
1273                 }
1274         } else {
1275                 struct lod_obj_stripe_cb_data data;
1276
1277                 data.locd_attr = attr;
1278                 data.locd_declare = false;
1279                 rc = lod_obj_for_each_stripe(env, lo, th,
1280                                 lod_obj_stripe_attr_set_cb, &data);
1281         }
1282
1283         if (rc)
1284                 RETURN(rc);
1285
1286         if (!dt_object_exists(next) || dt_object_remote(next) ||
1287             !S_ISREG(attr->la_mode))
1288                 RETURN(0);
1289
1290         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_STRIPE)) {
1291                 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
1292                 RETURN(rc);
1293         }
1294
1295         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CHANGE_STRIPE)) {
1296                 struct lod_thread_info *info = lod_env_info(env);
1297                 struct lu_buf *buf = &info->lti_buf;
1298                 struct ost_id *oi = &info->lti_ostid;
1299                 struct lu_fid *fid = &info->lti_fid;
1300                 struct lov_mds_md_v1 *lmm;
1301                 struct lov_ost_data_v1 *objs;
1302                 __u32 magic;
1303
1304                 rc = lod_get_lov_ea(env, lo);
1305                 if (rc <= 0)
1306                         RETURN(rc);
1307
1308                 buf->lb_buf = info->lti_ea_store;
1309                 buf->lb_len = info->lti_ea_store_size;
1310                 lmm = info->lti_ea_store;
1311                 magic = le32_to_cpu(lmm->lmm_magic);
1312                 if (magic == LOV_MAGIC_COMP_V1) {
1313                         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1314                         struct lov_comp_md_entry_v1 *lcme =
1315                                                 &lcm->lcm_entries[0];
1316
1317                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
1318                         magic = le32_to_cpu(lmm->lmm_magic);
1319                 }
1320
1321                 if (magic == LOV_MAGIC_V1)
1322                         objs = &(lmm->lmm_objects[0]);
1323                 else
1324                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1325                 ostid_le_to_cpu(&objs->l_ost_oi, oi);
1326                 ostid_to_fid(fid, oi, le32_to_cpu(objs->l_ost_idx));
1327                 fid->f_oid--;
1328                 fid_to_ostid(fid, oi);
1329                 ostid_cpu_to_le(oi, &objs->l_ost_oi);
1330
1331                 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1332                                               LU_XATTR_REPLACE, th);
1333         } else if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_PFL_RANGE)) {
1334                 struct lod_thread_info *info = lod_env_info(env);
1335                 struct lu_buf *buf = &info->lti_buf;
1336                 struct lov_comp_md_v1 *lcm;
1337                 struct lov_comp_md_entry_v1 *lcme;
1338
1339                 rc = lod_get_lov_ea(env, lo);
1340                 if (rc <= 0)
1341                         RETURN(rc);
1342
1343                 buf->lb_buf = info->lti_ea_store;
1344                 buf->lb_len = info->lti_ea_store_size;
1345                 lcm = buf->lb_buf;
1346                 if (le32_to_cpu(lcm->lcm_magic) != LOV_MAGIC_COMP_V1)
1347                         RETURN(-EINVAL);
1348
1349                 le32_add_cpu(&lcm->lcm_layout_gen, 1);
1350                 lcme = &lcm->lcm_entries[0];
1351                 le64_add_cpu(&lcme->lcme_extent.e_start, 1);
1352                 le64_add_cpu(&lcme->lcme_extent.e_end, -1);
1353
1354                 rc = lod_sub_object_xattr_set(env, next, buf, XATTR_NAME_LOV,
1355                                               LU_XATTR_REPLACE, th);
1356         }
1357
1358         RETURN(rc);
1359 }
1360
1361 /**
1362  * Implementation of dt_object_operations::do_xattr_get.
1363  *
1364  * If LOV EA is requested from the root object and it's not
1365  * found, then return default striping for the filesystem.
1366  *
1367  * \see dt_object_operations::do_xattr_get() in the API description for details.
1368  */
1369 static int lod_xattr_get(const struct lu_env *env, struct dt_object *dt,
1370                          struct lu_buf *buf, const char *name)
1371 {
1372         struct lod_thread_info *info = lod_env_info(env);
1373         struct lod_device *dev = lu2lod_dev(dt->do_lu.lo_dev);
1374         int is_root;
1375         int rc;
1376         ENTRY;
1377
1378         rc = dt_xattr_get(env, dt_object_child(dt), buf, name);
1379         if (strcmp(name, XATTR_NAME_LMV) == 0) {
1380                 struct lmv_mds_md_v1    *lmv1;
1381                 int                      rc1 = 0;
1382
1383                 if (rc > (typeof(rc))sizeof(*lmv1))
1384                         RETURN(rc);
1385
1386                 if (rc < (typeof(rc))sizeof(*lmv1))
1387                         RETURN(rc = rc > 0 ? -EINVAL : rc);
1388
1389                 if (buf->lb_buf == NULL || buf->lb_len == 0) {
1390                         CLASSERT(sizeof(*lmv1) <= sizeof(info->lti_key));
1391
1392                         info->lti_buf.lb_buf = info->lti_key;
1393                         info->lti_buf.lb_len = sizeof(*lmv1);
1394                         rc = dt_xattr_get(env, dt_object_child(dt),
1395                                           &info->lti_buf, name);
1396                         if (unlikely(rc != sizeof(*lmv1)))
1397                                 RETURN(rc = rc > 0 ? -EINVAL : rc);
1398
1399                         lmv1 = info->lti_buf.lb_buf;
1400                         /* The on-disk LMV EA only contains header, but the
1401                          * returned LMV EA size should contain the space for
1402                          * the FIDs of all shards of the striped directory. */
1403                         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_V1)
1404                                 rc = lmv_mds_md_size(
1405                                         le32_to_cpu(lmv1->lmv_stripe_count),
1406                                         LMV_MAGIC_V1);
1407                 } else {
1408                         rc1 = lod_load_lmv_shards(env, lod_dt_obj(dt),
1409                                                   buf, false);
1410                 }
1411
1412                 RETURN(rc = rc1 != 0 ? rc1 : rc);
1413         }
1414
1415         if (rc != -ENODATA || !S_ISDIR(dt->do_lu.lo_header->loh_attr & S_IFMT))
1416                 RETURN(rc);
1417
1418         /*
1419          * XXX: Only used by lfsck
1420          *
1421          * lod returns default striping on the real root of the device
1422          * this is like the root stores default striping for the whole
1423          * filesystem. historically we've been using a different approach
1424          * and store it in the config.
1425          */
1426         dt_root_get(env, dev->lod_child, &info->lti_fid);
1427         is_root = lu_fid_eq(&info->lti_fid, lu_object_fid(&dt->do_lu));
1428
1429         if (is_root && strcmp(XATTR_NAME_LOV, name) == 0) {
1430                 struct lov_user_md *lum = buf->lb_buf;
1431                 struct lov_desc    *desc = &dev->lod_desc;
1432
1433                 if (buf->lb_buf == NULL) {
1434                         rc = sizeof(*lum);
1435                 } else if (buf->lb_len >= sizeof(*lum)) {
1436                         lum->lmm_magic = cpu_to_le32(LOV_USER_MAGIC_V1);
1437                         lmm_oi_set_seq(&lum->lmm_oi, FID_SEQ_LOV_DEFAULT);
1438                         lmm_oi_set_id(&lum->lmm_oi, 0);
1439                         lmm_oi_cpu_to_le(&lum->lmm_oi, &lum->lmm_oi);
1440                         lum->lmm_pattern = cpu_to_le32(desc->ld_pattern);
1441                         lum->lmm_stripe_size = cpu_to_le32(
1442                                                 desc->ld_default_stripe_size);
1443                         lum->lmm_stripe_count = cpu_to_le16(
1444                                                 desc->ld_default_stripe_count);
1445                         lum->lmm_stripe_offset = cpu_to_le16(
1446                                                 desc->ld_default_stripe_offset);
1447                         rc = sizeof(*lum);
1448                 } else {
1449                         rc = -ERANGE;
1450                 }
1451         }
1452
1453         RETURN(rc);
1454 }
1455
1456 /**
1457  * Verify LVM EA.
1458  *
1459  * Checks that the magic of the stripe is sane.
1460  *
1461  * \param[in] lod       lod device
1462  * \param[in] lum       a buffer storing LMV EA to verify
1463  *
1464  * \retval              0 if the EA is sane
1465  * \retval              negative otherwise
1466  */
1467 static int lod_verify_md_striping(struct lod_device *lod,
1468                                   const struct lmv_user_md_v1 *lum)
1469 {
1470         if (unlikely(le32_to_cpu(lum->lum_magic) != LMV_USER_MAGIC)) {
1471                 CERROR("%s: invalid lmv_user_md: magic = %x, "
1472                        "stripe_offset = %d, stripe_count = %u: rc = %d\n",
1473                        lod2obd(lod)->obd_name, le32_to_cpu(lum->lum_magic),
1474                        (int)le32_to_cpu(lum->lum_stripe_offset),
1475                        le32_to_cpu(lum->lum_stripe_count), -EINVAL);
1476                 return -EINVAL;
1477         }
1478
1479         return 0;
1480 }
1481
1482 /**
1483  * Initialize LMV EA for a slave.
1484  *
1485  * Initialize slave's LMV EA from the master's LMV EA.
1486  *
1487  * \param[in] master_lmv        a buffer containing master's EA
1488  * \param[out] slave_lmv        a buffer where slave's EA will be stored
1489  *
1490  */
1491 static void lod_prep_slave_lmv_md(struct lmv_mds_md_v1 *slave_lmv,
1492                                   const struct lmv_mds_md_v1 *master_lmv)
1493 {
1494         *slave_lmv = *master_lmv;
1495         slave_lmv->lmv_magic = cpu_to_le32(LMV_MAGIC_STRIPE);
1496 }
1497
1498 /**
1499  * Generate LMV EA.
1500  *
1501  * Generate LMV EA from the object passed as \a dt. The object must have
1502  * the stripes created and initialized.
1503  *
1504  * \param[in] env       execution environment
1505  * \param[in] dt        object
1506  * \param[out] lmv_buf  buffer storing generated LMV EA
1507  *
1508  * \retval              0 on success
1509  * \retval              negative if failed
1510  */
1511 static int lod_prep_lmv_md(const struct lu_env *env, struct dt_object *dt,
1512                            struct lu_buf *lmv_buf)
1513 {
1514         struct lod_thread_info  *info = lod_env_info(env);
1515         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1516         struct lod_object       *lo = lod_dt_obj(dt);
1517         struct lmv_mds_md_v1    *lmm1;
1518         int                     stripe_count;
1519         int                     type = LU_SEQ_RANGE_ANY;
1520         int                     rc;
1521         __u32                   mdtidx;
1522         ENTRY;
1523
1524         LASSERT(lo->ldo_dir_striped != 0);
1525         LASSERT(lo->ldo_dir_stripenr > 0);
1526         stripe_count = lo->ldo_dir_stripenr;
1527         /* Only store the LMV EA heahder on the disk. */
1528         if (info->lti_ea_store_size < sizeof(*lmm1)) {
1529                 rc = lod_ea_store_resize(info, sizeof(*lmm1));
1530                 if (rc != 0)
1531                         RETURN(rc);
1532         } else {
1533                 memset(info->lti_ea_store, 0, sizeof(*lmm1));
1534         }
1535
1536         lmm1 = (struct lmv_mds_md_v1 *)info->lti_ea_store;
1537         lmm1->lmv_magic = cpu_to_le32(LMV_MAGIC);
1538         lmm1->lmv_stripe_count = cpu_to_le32(stripe_count);
1539         lmm1->lmv_hash_type = cpu_to_le32(lo->ldo_dir_hash_type);
1540         rc = lod_fld_lookup(env, lod, lu_object_fid(&dt->do_lu),
1541                             &mdtidx, &type);
1542         if (rc != 0)
1543                 RETURN(rc);
1544
1545         lmm1->lmv_master_mdt_index = cpu_to_le32(mdtidx);
1546         lmv_buf->lb_buf = info->lti_ea_store;
1547         lmv_buf->lb_len = sizeof(*lmm1);
1548
1549         RETURN(rc);
1550 }
1551
1552 /**
1553  * Create in-core represenation for a striped directory.
1554  *
1555  * Parse the buffer containing LMV EA and instantiate LU objects
1556  * representing the stripe objects. The pointers to the objects are
1557  * stored in ldo_stripe field of \a lo. This function is used when
1558  * we need to access an already created object (i.e. load from a disk).
1559  *
1560  * \param[in] env       execution environment
1561  * \param[in] lo        lod object
1562  * \param[in] buf       buffer containing LMV EA
1563  *
1564  * \retval              0 on success
1565  * \retval              negative if failed
1566  */
1567 int lod_parse_dir_striping(const struct lu_env *env, struct lod_object *lo,
1568                            const struct lu_buf *buf)
1569 {
1570         struct lod_thread_info  *info = lod_env_info(env);
1571         struct lod_device       *lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
1572         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1573         struct dt_object        **stripe;
1574         union lmv_mds_md        *lmm = buf->lb_buf;
1575         struct lmv_mds_md_v1    *lmv1 = &lmm->lmv_md_v1;
1576         struct lu_fid           *fid = &info->lti_fid;
1577         unsigned int            i;
1578         int                     rc = 0;
1579         ENTRY;
1580
1581         if (le32_to_cpu(lmv1->lmv_hash_type) & LMV_HASH_FLAG_MIGRATION)
1582                 RETURN(0);
1583
1584         if (le32_to_cpu(lmv1->lmv_magic) == LMV_MAGIC_STRIPE) {
1585                 lo->ldo_dir_slave_stripe = 1;
1586                 RETURN(0);
1587         }
1588
1589         if (le32_to_cpu(lmv1->lmv_magic) != LMV_MAGIC_V1)
1590                 RETURN(-EINVAL);
1591
1592         if (le32_to_cpu(lmv1->lmv_stripe_count) < 1)
1593                 RETURN(0);
1594
1595         LASSERT(lo->ldo_stripe == NULL);
1596         OBD_ALLOC(stripe, sizeof(stripe[0]) *
1597                   (le32_to_cpu(lmv1->lmv_stripe_count)));
1598         if (stripe == NULL)
1599                 RETURN(-ENOMEM);
1600
1601         for (i = 0; i < le32_to_cpu(lmv1->lmv_stripe_count); i++) {
1602                 struct dt_device        *tgt_dt;
1603                 struct dt_object        *dto;
1604                 int                     type = LU_SEQ_RANGE_ANY;
1605                 __u32                   idx;
1606
1607                 fid_le_to_cpu(fid, &lmv1->lmv_stripe_fids[i]);
1608                 if (!fid_is_sane(fid))
1609                         GOTO(out, rc = -ESTALE);
1610
1611                 rc = lod_fld_lookup(env, lod, fid, &idx, &type);
1612                 if (rc != 0)
1613                         GOTO(out, rc);
1614
1615                 if (idx == lod2lu_dev(lod)->ld_site->ld_seq_site->ss_node_id) {
1616                         tgt_dt = lod->lod_child;
1617                 } else {
1618                         struct lod_tgt_desc     *tgt;
1619
1620                         tgt = LTD_TGT(ltd, idx);
1621                         if (tgt == NULL)
1622                                 GOTO(out, rc = -ESTALE);
1623                         tgt_dt = tgt->ltd_tgt;
1624                 }
1625
1626                 dto = dt_locate_at(env, tgt_dt, fid,
1627                                   lo->ldo_obj.do_lu.lo_dev->ld_site->ls_top_dev,
1628                                   NULL);
1629                 if (IS_ERR(dto))
1630                         GOTO(out, rc = PTR_ERR(dto));
1631
1632                 stripe[i] = dto;
1633         }
1634 out:
1635         lo->ldo_stripe = stripe;
1636         lo->ldo_dir_stripenr = le32_to_cpu(lmv1->lmv_stripe_count);
1637         lo->ldo_dir_stripes_allocated = le32_to_cpu(lmv1->lmv_stripe_count);
1638         if (rc != 0)
1639                 lod_object_free_striping(env, lo);
1640
1641         RETURN(rc);
1642 }
1643
1644 /**
1645  * Declare create a striped directory.
1646  *
1647  * Declare creating a striped directory with a given stripe pattern on the
1648  * specified MDTs. A striped directory is represented as a regular directory
1649  * - an index listing all the stripes. The stripes point back to the master
1650  * object with ".." and LinkEA. The master object gets LMV EA which
1651  * identifies it as a striped directory. The function allocates FIDs
1652  * for all stripes.
1653  *
1654  * \param[in] env       execution environment
1655  * \param[in] dt        object
1656  * \param[in] attr      attributes to initialize the objects with
1657  * \param[in] dof       type of objects to be created
1658  * \param[in] th        transaction handle
1659  *
1660  * \retval              0 on success
1661  * \retval              negative if failed
1662  */
1663 static int lod_dir_declare_create_stripes(const struct lu_env *env,
1664                                           struct dt_object *dt,
1665                                           struct lu_attr *attr,
1666                                           struct dt_object_format *dof,
1667                                           struct thandle *th)
1668 {
1669         struct lod_thread_info  *info = lod_env_info(env);
1670         struct lu_buf           lmv_buf;
1671         struct lu_buf           slave_lmv_buf;
1672         struct lmv_mds_md_v1    *lmm;
1673         struct lmv_mds_md_v1    *slave_lmm = NULL;
1674         struct dt_insert_rec    *rec = &info->lti_dt_rec;
1675         struct lod_object       *lo = lod_dt_obj(dt);
1676         int                     rc;
1677         __u32                   i;
1678         ENTRY;
1679
1680         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
1681         if (rc != 0)
1682                 GOTO(out, rc);
1683         lmm = lmv_buf.lb_buf;
1684
1685         OBD_ALLOC_PTR(slave_lmm);
1686         if (slave_lmm == NULL)
1687                 GOTO(out, rc = -ENOMEM);
1688
1689         lod_prep_slave_lmv_md(slave_lmm, lmm);
1690         slave_lmv_buf.lb_buf = slave_lmm;
1691         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
1692
1693         if (!dt_try_as_dir(env, dt_object_child(dt)))
1694                 GOTO(out, rc = -EINVAL);
1695
1696         rec->rec_type = S_IFDIR;
1697         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
1698                 struct dt_object        *dto = lo->ldo_stripe[i];
1699                 char                    *stripe_name = info->lti_key;
1700                 struct lu_name          *sname;
1701                 struct linkea_data       ldata          = { NULL };
1702                 struct lu_buf           linkea_buf;
1703
1704                 rc = lod_sub_object_declare_create(env, dto, attr, NULL,
1705                                                    dof, th);
1706                 if (rc != 0)
1707                         GOTO(out, rc);
1708
1709                 if (!dt_try_as_dir(env, dto))
1710                         GOTO(out, rc = -EINVAL);
1711
1712                 rc = lod_sub_object_declare_ref_add(env, dto, th);
1713                 if (rc != 0)
1714                         GOTO(out, rc);
1715
1716                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1717                 rc = lod_sub_object_declare_insert(env, dto,
1718                                         (const struct dt_rec *)rec,
1719                                         (const struct dt_key *)dot, th);
1720                 if (rc != 0)
1721                         GOTO(out, rc);
1722
1723                 /* master stripe FID will be put to .. */
1724                 rec->rec_fid = lu_object_fid(&dt->do_lu);
1725                 rc = lod_sub_object_declare_insert(env, dto,
1726                                         (const struct dt_rec *)rec,
1727                                         (const struct dt_key *)dotdot,
1728                                         th);
1729                 if (rc != 0)
1730                         GOTO(out, rc);
1731
1732                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
1733                     cfs_fail_val != i) {
1734                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
1735                             cfs_fail_val == i)
1736                                 slave_lmm->lmv_master_mdt_index =
1737                                                         cpu_to_le32(i + 1);
1738                         else
1739                                 slave_lmm->lmv_master_mdt_index =
1740                                                         cpu_to_le32(i);
1741                         rc = lod_sub_object_declare_xattr_set(env, dto,
1742                                         &slave_lmv_buf, XATTR_NAME_LMV, 0, th);
1743                         if (rc != 0)
1744                                 GOTO(out, rc);
1745                 }
1746
1747                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
1748                     cfs_fail_val == i)
1749                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1750                                 PFID(lu_object_fid(&dto->do_lu)), i + 1);
1751                 else
1752                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%u",
1753                                 PFID(lu_object_fid(&dto->do_lu)), i);
1754
1755                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
1756                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
1757                                       sname, lu_object_fid(&dt->do_lu));
1758                 if (rc != 0)
1759                         GOTO(out, rc);
1760
1761                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
1762                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
1763                 rc = lod_sub_object_declare_xattr_set(env, dto, &linkea_buf,
1764                                           XATTR_NAME_LINK, 0, th);
1765                 if (rc != 0)
1766                         GOTO(out, rc);
1767
1768                 rec->rec_fid = lu_object_fid(&dto->do_lu);
1769                 rc = lod_sub_object_declare_insert(env, dt_object_child(dt),
1770                                        (const struct dt_rec *)rec,
1771                                        (const struct dt_key *)stripe_name,
1772                                        th);
1773                 if (rc != 0)
1774                         GOTO(out, rc);
1775
1776                 rc = lod_sub_object_declare_ref_add(env, dt_object_child(dt),
1777                                                     th);
1778                 if (rc != 0)
1779                         GOTO(out, rc);
1780         }
1781
1782         rc = lod_sub_object_declare_xattr_set(env, dt_object_child(dt),
1783                                 &lmv_buf, XATTR_NAME_LMV, 0, th);
1784         if (rc != 0)
1785                 GOTO(out, rc);
1786 out:
1787         if (slave_lmm != NULL)
1788                 OBD_FREE_PTR(slave_lmm);
1789
1790         RETURN(rc);
1791 }
1792
1793 static int lod_prep_md_striped_create(const struct lu_env *env,
1794                                       struct dt_object *dt,
1795                                       struct lu_attr *attr,
1796                                       const struct lmv_user_md_v1 *lum,
1797                                       struct dt_object_format *dof,
1798                                       struct thandle *th)
1799 {
1800         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1801         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1802         struct lod_object       *lo = lod_dt_obj(dt);
1803         struct dt_object        **stripe;
1804         __u32                   stripe_count;
1805         int                     *idx_array;
1806         __u32                   master_index;
1807         int                     rc = 0;
1808         __u32                   i;
1809         __u32                   j;
1810         ENTRY;
1811
1812         /* The lum has been verifed in lod_verify_md_striping */
1813         LASSERT(le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC);
1814         LASSERT(le32_to_cpu(lum->lum_stripe_count) > 0);
1815
1816         stripe_count = le32_to_cpu(lum->lum_stripe_count);
1817
1818         OBD_ALLOC(idx_array, sizeof(idx_array[0]) * stripe_count);
1819         if (idx_array == NULL)
1820                 RETURN(-ENOMEM);
1821
1822         OBD_ALLOC(stripe, sizeof(stripe[0]) * stripe_count);
1823         if (stripe == NULL)
1824                 GOTO(out_free, rc = -ENOMEM);
1825
1826         /* Start index must be the master MDT */
1827         master_index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1828         idx_array[0] = master_index;
1829         for (i = 0; i < stripe_count; i++) {
1830                 struct lod_tgt_desc     *tgt = NULL;
1831                 struct dt_object        *dto;
1832                 struct lu_fid           fid = { 0 };
1833                 int                     idx;
1834                 struct lu_object_conf   conf = { 0 };
1835                 struct dt_device        *tgt_dt = NULL;
1836
1837                 /* Try to find next avaible target */
1838                 idx = idx_array[i];
1839                 for (j = 0; j < lod->lod_remote_mdt_count;
1840                      j++, idx = (idx + 1) % (lod->lod_remote_mdt_count + 1)) {
1841                         bool already_allocated = false;
1842                         __u32 k;
1843
1844                         CDEBUG(D_INFO, "try idx %d, mdt cnt %u, allocated %u\n",
1845                                idx, lod->lod_remote_mdt_count + 1, i);
1846
1847                         if (likely(!OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))) {
1848                                 /* check whether the idx already exists
1849                                  * in current allocated array */
1850                                 for (k = 0; k < i; k++) {
1851                                         if (idx_array[k] == idx) {
1852                                                 already_allocated = true;
1853                                                 break;
1854                                         }
1855                                 }
1856
1857                                 if (already_allocated)
1858                                         continue;
1859                         }
1860
1861                         /* Sigh, this index is not in the bitmap, let's check
1862                          * next available target */
1863                         if (!cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx) &&
1864                             idx != master_index)
1865                                 continue;
1866
1867                         if (idx == master_index) {
1868                                 /* Allocate the FID locally */
1869                                 rc = obd_fid_alloc(env, lod->lod_child_exp,
1870                                                    &fid, NULL);
1871                                 if (rc < 0)
1872                                         GOTO(out_put, rc);
1873                                 tgt_dt = lod->lod_child;
1874                                 break;
1875                         }
1876
1877                         /* check the status of the OSP */
1878                         tgt = LTD_TGT(ltd, idx);
1879                         if (tgt == NULL)
1880                                 continue;
1881
1882                         tgt_dt = tgt->ltd_tgt;
1883                         rc = dt_statfs(env, tgt_dt, NULL);
1884                         if (rc) {
1885                                 /* this OSP doesn't feel well */
1886                                 rc = 0;
1887                                 continue;
1888                         }
1889
1890                         rc = obd_fid_alloc(env, tgt->ltd_exp, &fid, NULL);
1891                         if (rc < 0) {
1892                                 rc = 0;
1893                                 continue;
1894                         }
1895
1896                         break;
1897                 }
1898
1899                 /* Can not allocate more stripes */
1900                 if (j == lod->lod_remote_mdt_count) {
1901                         CDEBUG(D_INFO, "%s: require stripes %u only get %d\n",
1902                                lod2obd(lod)->obd_name, stripe_count, i);
1903                         break;
1904                 }
1905
1906                 CDEBUG(D_INFO, "Get idx %d, for stripe %d "DFID"\n",
1907                        idx, i, PFID(&fid));
1908                 idx_array[i] = idx;
1909                 /* Set the start index for next stripe allocation */
1910                 if (i < stripe_count - 1)
1911                         idx_array[i + 1] = (idx + 1) %
1912                                            (lod->lod_remote_mdt_count + 1);
1913                 /* tgt_dt and fid must be ready after search avaible OSP
1914                  * in the above loop */
1915                 LASSERT(tgt_dt != NULL);
1916                 LASSERT(fid_is_sane(&fid));
1917                 conf.loc_flags = LOC_F_NEW;
1918                 dto = dt_locate_at(env, tgt_dt, &fid,
1919                                    dt->do_lu.lo_dev->ld_site->ls_top_dev,
1920                                    &conf);
1921                 if (IS_ERR(dto))
1922                         GOTO(out_put, rc = PTR_ERR(dto));
1923                 stripe[i] = dto;
1924         }
1925
1926         lo->ldo_dir_striped = 1;
1927         lo->ldo_stripe = stripe;
1928         lo->ldo_dir_stripenr = i;
1929         lo->ldo_dir_stripes_allocated = stripe_count;
1930
1931         if (lo->ldo_dir_stripenr == 0)
1932                 GOTO(out_put, rc = -ENOSPC);
1933
1934         rc = lod_dir_declare_create_stripes(env, dt, attr, dof, th);
1935         if (rc != 0)
1936                 GOTO(out_put, rc);
1937
1938 out_put:
1939         if (rc < 0) {
1940                 for (i = 0; i < stripe_count; i++)
1941                         if (stripe[i] != NULL)
1942                                 dt_object_put(env, stripe[i]);
1943                 OBD_FREE(stripe, sizeof(stripe[0]) * stripe_count);
1944                 lo->ldo_dir_stripenr = 0;
1945                 lo->ldo_dir_stripes_allocated = 0;
1946                 lo->ldo_stripe = NULL;
1947         }
1948
1949 out_free:
1950         OBD_FREE(idx_array, sizeof(idx_array[0]) * stripe_count);
1951
1952         RETURN(rc);
1953 }
1954
1955 /**
1956  * Declare create striped md object.
1957  *
1958  * The function declares intention to create a striped directory. This is a
1959  * wrapper for lod_prep_md_striped_create(). The only additional functionality
1960  * is to verify pattern \a lum_buf is good. Check that function for the details.
1961  *
1962  * \param[in] env       execution environment
1963  * \param[in] dt        object
1964  * \param[in] attr      attributes to initialize the objects with
1965  * \param[in] lum_buf   a pattern specifying the number of stripes and
1966  *                      MDT to start from
1967  * \param[in] dof       type of objects to be created
1968  * \param[in] th        transaction handle
1969  *
1970  * \retval              0 on success
1971  * \retval              negative if failed
1972  *
1973  */
1974 static int lod_declare_xattr_set_lmv(const struct lu_env *env,
1975                                      struct dt_object *dt,
1976                                      struct lu_attr *attr,
1977                                      const struct lu_buf *lum_buf,
1978                                      struct dt_object_format *dof,
1979                                      struct thandle *th)
1980 {
1981         struct lod_object       *lo = lod_dt_obj(dt);
1982         struct lod_device       *lod = lu2lod_dev(dt->do_lu.lo_dev);
1983         struct lmv_user_md_v1   *lum;
1984         int                     rc;
1985         ENTRY;
1986
1987         lum = lum_buf->lb_buf;
1988         LASSERT(lum != NULL);
1989
1990         CDEBUG(D_INFO, "lum magic = %x count = %u offset = %d\n",
1991                le32_to_cpu(lum->lum_magic), le32_to_cpu(lum->lum_stripe_count),
1992                (int)le32_to_cpu(lum->lum_stripe_offset));
1993
1994         if (le32_to_cpu(lum->lum_stripe_count) == 0)
1995                 GOTO(out, rc = 0);
1996
1997         rc = lod_verify_md_striping(lod, lum);
1998         if (rc != 0)
1999                 GOTO(out, rc);
2000
2001         /* prepare dir striped objects */
2002         rc = lod_prep_md_striped_create(env, dt, attr, lum, dof, th);
2003         if (rc != 0) {
2004                 /* failed to create striping, let's reset
2005                  * config so that others don't get confused */
2006                 lod_object_free_striping(env, lo);
2007                 GOTO(out, rc);
2008         }
2009 out:
2010         RETURN(rc);
2011 }
2012
2013 /**
2014  * Implementation of dt_object_operations::do_declare_xattr_set.
2015  *
2016  * Used with regular (non-striped) objects. Basically it
2017  * initializes the striping information and applies the
2018  * change to all the stripes.
2019  *
2020  * \see dt_object_operations::do_declare_xattr_set() in the API description
2021  * for details.
2022  */
2023 static int lod_dir_declare_xattr_set(const struct lu_env *env,
2024                                      struct dt_object *dt,
2025                                      const struct lu_buf *buf,
2026                                      const char *name, int fl,
2027                                      struct thandle *th)
2028 {
2029         struct dt_object        *next = dt_object_child(dt);
2030         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2031         struct lod_object       *lo = lod_dt_obj(dt);
2032         int                     i;
2033         int                     rc;
2034         ENTRY;
2035
2036         if (strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
2037                 struct lmv_user_md_v1 *lum;
2038
2039                 LASSERT(buf != NULL && buf->lb_buf != NULL);
2040                 lum = buf->lb_buf;
2041                 rc = lod_verify_md_striping(d, lum);
2042                 if (rc != 0)
2043                         RETURN(rc);
2044         } else if (strcmp(name, XATTR_NAME_LOV) == 0) {
2045                 rc = lod_verify_striping(d, buf, false, 0);
2046                 if (rc != 0)
2047                         RETURN(rc);
2048         }
2049
2050         rc = lod_sub_object_declare_xattr_set(env, next, buf, name, fl, th);
2051         if (rc != 0)
2052                 RETURN(rc);
2053
2054         /* Note: Do not set LinkEA on sub-stripes, otherwise
2055          * it will confuse the fid2path process(see mdt_path_current()).
2056          * The linkEA between master and sub-stripes is set in
2057          * lod_xattr_set_lmv(). */
2058         if (strcmp(name, XATTR_NAME_LINK) == 0)
2059                 RETURN(0);
2060
2061         /* set xattr to each stripes, if needed */
2062         rc = lod_load_striping(env, lo);
2063         if (rc != 0)
2064                 RETURN(rc);
2065
2066         if (lo->ldo_dir_stripenr == 0)
2067                 RETURN(0);
2068
2069         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2070                 LASSERT(lo->ldo_stripe[i]);
2071
2072                 rc = lod_sub_object_declare_xattr_set(env, lo->ldo_stripe[i],
2073                                                 buf, name, fl, th);
2074                 if (rc != 0)
2075                         break;
2076         }
2077
2078         RETURN(rc);
2079 }
2080
2081 static int
2082 lod_obj_stripe_replace_parent_fid_cb(const struct lu_env *env,
2083                                      struct lod_object *lo,
2084                                      struct dt_object *dt, struct thandle *th,
2085                                      int stripe_idx,
2086                                      struct lod_obj_stripe_cb_data *data)
2087 {
2088         struct lod_thread_info *info = lod_env_info(env);
2089         struct filter_fid *ff = &info->lti_ff;
2090         struct lu_buf *buf = &info->lti_buf;
2091         int rc;
2092
2093         buf->lb_buf = ff;
2094         buf->lb_len = sizeof(*ff);
2095         rc = dt_xattr_get(env, dt, buf, XATTR_NAME_FID);
2096         if (rc == -ENODATA)
2097                 return 0;
2098
2099         if (rc < 0)
2100                 return rc;
2101
2102         ff->ff_parent = *lu_object_fid(&lo->ldo_obj.do_lu);
2103         ff->ff_parent.f_ver = stripe_idx;
2104         fid_cpu_to_le(&ff->ff_parent, &ff->ff_parent);
2105         if (data->locd_declare)
2106                 rc = lod_sub_object_declare_xattr_set(env, dt, buf,
2107                                                       XATTR_NAME_FID,
2108                                                       LU_XATTR_REPLACE, th);
2109         else
2110                 rc = lod_sub_object_xattr_set(env, dt, buf, XATTR_NAME_FID,
2111                                               LU_XATTR_REPLACE, th);
2112
2113         return rc;
2114 }
2115
2116 /**
2117  * Reset parent FID on OST object
2118  *
2119  * Replace parent FID with @dt object FID, which is only called during migration
2120  * to reset the parent FID after the MDT object is migrated to the new MDT, i.e.
2121  * the FID is changed.
2122  *
2123  * \param[in] env execution environment
2124  * \param[in] dt dt_object whose stripes's parent FID will be reset
2125  * \parem[in] th thandle
2126  * \param[in] declare if it is declare
2127  *
2128  * \retval      0 if reset succeeds
2129  * \retval      negative errno if reset fails
2130  */
2131 static int lod_object_replace_parent_fid(const struct lu_env *env,
2132                                          struct dt_object *dt,
2133                                          struct thandle *th, bool declare)
2134 {
2135         struct lod_object *lo = lod_dt_obj(dt);
2136         struct lod_thread_info  *info = lod_env_info(env);
2137         struct lu_buf *buf = &info->lti_buf;
2138         struct filter_fid *ff;
2139         struct lod_obj_stripe_cb_data data;
2140         int rc;
2141         ENTRY;
2142
2143         LASSERT(S_ISREG(dt->do_lu.lo_header->loh_attr));
2144
2145         /* set xattr to each stripes, if needed */
2146         rc = lod_load_striping(env, lo);
2147         if (rc != 0)
2148                 RETURN(rc);
2149
2150         if (!lod_obj_is_striped(dt))
2151                 RETURN(0);
2152
2153         if (info->lti_ea_store_size < sizeof(*ff)) {
2154                 rc = lod_ea_store_resize(info, sizeof(*ff));
2155                 if (rc != 0)
2156                         RETURN(rc);
2157         }
2158
2159         buf->lb_buf = info->lti_ea_store;
2160         buf->lb_len = info->lti_ea_store_size;
2161
2162         data.locd_declare = declare;
2163         rc = lod_obj_for_each_stripe(env, lo, th,
2164                         lod_obj_stripe_replace_parent_fid_cb, &data);
2165
2166         RETURN(rc);
2167 }
2168
2169 inline __u16 lod_comp_entry_stripecnt(struct lod_object *lo,
2170                                       struct lod_layout_component *entry,
2171                                       bool is_dir)
2172 {
2173         struct lod_device *lod = lu2lod_dev(lod2lu_obj(lo)->lo_dev);
2174
2175         if (is_dir)
2176                 return  0;
2177         else if (lod_comp_inited(entry))
2178                 return entry->llc_stripenr;
2179         else if ((__u16)-1 == entry->llc_stripenr)
2180                 return lod->lod_desc.ld_tgt_count;
2181         else
2182                 return lod_get_stripecnt(lod, lo, entry->llc_stripenr);
2183 }
2184
2185 static int lod_comp_md_size(struct lod_object *lo, bool is_dir)
2186 {
2187         int magic, size = 0, i;
2188         struct lod_layout_component *comp_entries;
2189         __u16 comp_cnt;
2190         bool is_composite;
2191
2192         if (is_dir) {
2193                 comp_cnt = lo->ldo_def_striping->lds_def_comp_cnt;
2194                 comp_entries = lo->ldo_def_striping->lds_def_comp_entries;
2195                 is_composite =
2196                         lo->ldo_def_striping->lds_def_striping_is_composite;
2197         } else {
2198                 comp_cnt = lo->ldo_comp_cnt;
2199                 comp_entries = lo->ldo_comp_entries;
2200                 is_composite = lo->ldo_is_composite;
2201         }
2202
2203
2204         LASSERT(comp_cnt != 0 && comp_entries != NULL);
2205         if (is_composite) {
2206                 size = sizeof(struct lov_comp_md_v1) +
2207                        sizeof(struct lov_comp_md_entry_v1) * comp_cnt;
2208                 LASSERT(size % sizeof(__u64) == 0);
2209         }
2210
2211         for (i = 0; i < comp_cnt; i++) {
2212                 __u16 stripenr;
2213
2214                 magic = comp_entries[i].llc_pool ? LOV_MAGIC_V3 : LOV_MAGIC_V1;
2215                 stripenr = lod_comp_entry_stripecnt(lo, &comp_entries[i],
2216                                                     is_dir);
2217                 size += lov_user_md_size(stripenr, magic);
2218                 LASSERT(size % sizeof(__u64) == 0);
2219         }
2220         return size;
2221 }
2222
2223 /**
2224  * Declare component add. The xattr name is XATTR_LUSTRE_LOV.add, and
2225  * the xattr value is binary lov_comp_md_v1 which contains component(s)
2226  * to be added.
2227   *
2228  * \param[in] env       execution environment
2229  * \param[in] dt        dt_object to add components on
2230  * \param[in] buf       buffer contains components to be added
2231  * \parem[in] th        thandle
2232  *
2233  * \retval      0 on success
2234  * \retval      negative errno on failure
2235  */
2236 static int lod_declare_layout_add(const struct lu_env *env,
2237                                   struct dt_object *dt,
2238                                   const struct lu_buf *buf,
2239                                   struct thandle *th)
2240 {
2241         struct lod_thread_info  *info = lod_env_info(env);
2242         struct lod_layout_component *comp_array, *lod_comp;
2243         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2244         struct dt_object *next = dt_object_child(dt);
2245         struct lov_desc         *desc = &d->lod_desc;
2246         struct lod_object       *lo = lod_dt_obj(dt);
2247         struct lov_user_md_v3   *v3;
2248         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2249         __u32   magic;
2250         __u64   prev_end;
2251         int     i, rc, array_cnt;
2252         ENTRY;
2253
2254         LASSERT(lo->ldo_is_composite);
2255
2256         prev_end = lo->ldo_comp_entries[lo->ldo_comp_cnt - 1].llc_extent.e_end;
2257         rc = lod_verify_striping(d, buf, false, prev_end);
2258         if (rc != 0)
2259                 RETURN(rc);
2260
2261         magic = comp_v1->lcm_magic;
2262         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2263                 lustre_swab_lov_comp_md_v1(comp_v1);
2264                 magic = comp_v1->lcm_magic;
2265         }
2266
2267         if (magic != LOV_USER_MAGIC_COMP_V1)
2268                 RETURN(-EINVAL);
2269
2270         array_cnt = lo->ldo_comp_cnt + comp_v1->lcm_entry_count;
2271         OBD_ALLOC(comp_array, sizeof(*comp_array) * array_cnt);
2272         if (comp_array == NULL)
2273                 RETURN(-ENOMEM);
2274
2275         memcpy(comp_array, lo->ldo_comp_entries,
2276                sizeof(*comp_array) * lo->ldo_comp_cnt);
2277
2278         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2279                 struct lov_user_md_v1 *v1;
2280                 struct lu_extent *ext;
2281
2282                 v1 = (struct lov_user_md *)((char *)comp_v1 +
2283                                 comp_v1->lcm_entries[i].lcme_offset);
2284                 ext = &comp_v1->lcm_entries[i].lcme_extent;
2285
2286                 lod_comp = &comp_array[lo->ldo_comp_cnt + i];
2287                 lod_comp->llc_extent.e_start = ext->e_start;
2288                 lod_comp->llc_extent.e_end = ext->e_end;
2289                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
2290
2291                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
2292                 if (!lod_comp->llc_stripenr ||
2293                     lod_comp->llc_stripenr == (__u16)-1)
2294                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
2295                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
2296                 if (!lod_comp->llc_stripe_size)
2297                         lod_comp->llc_stripe_size =
2298                                 desc->ld_default_stripe_size;
2299
2300                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
2301                         v3 = (struct lov_user_md_v3 *) v1;
2302                         if (v3->lmm_pool_name[0] != '\0') {
2303                                 rc = lod_set_pool(&lod_comp->llc_pool,
2304                                                   v3->lmm_pool_name);
2305                                 if (rc)
2306                                         GOTO(error, rc);
2307                         }
2308                 }
2309         }
2310
2311         OBD_FREE(lo->ldo_comp_entries, sizeof(*lod_comp) * lo->ldo_comp_cnt);
2312         lo->ldo_comp_entries = comp_array;
2313         lo->ldo_comp_cnt = array_cnt;
2314         /* No need to increase layout generation here, it will be increased
2315          * later when generating component ID for the new components */
2316
2317         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2318         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2319                                               XATTR_NAME_LOV, 0, th);
2320         if (rc)
2321                 GOTO(error, rc);
2322
2323         RETURN(0);
2324
2325 error:
2326         for (i = lo->ldo_comp_cnt; i < array_cnt; i++) {
2327                 lod_comp = &comp_array[i];
2328                 if (lod_comp->llc_pool != NULL) {
2329                         OBD_FREE(lod_comp->llc_pool,
2330                                  strlen(lod_comp->llc_pool) + 1);
2331                         lod_comp->llc_pool = NULL;
2332                 }
2333         }
2334         OBD_FREE(comp_array, sizeof(*comp_array) * array_cnt);
2335         RETURN(rc);
2336 }
2337
2338 /**
2339  * Declare component set. The xattr is name XATTR_LUSTRE_LOV.set.$field,
2340  * the '$field' can only be 'flags' now. The xattr value is binary
2341  * lov_comp_md_v1 which contains the component ID(s) and the value of
2342  * the field to be modified.
2343  *
2344  * \param[in] env       execution environment
2345  * \param[in] dt        dt_object to be modified
2346  * \param[in] op        operation string, like "set.flags"
2347  * \param[in] buf       buffer contains components to be set
2348  * \parem[in] th        thandle
2349  *
2350  * \retval      0 on success
2351  * \retval      negative errno on failure
2352  */
2353 static int lod_declare_layout_set(const struct lu_env *env,
2354                                   struct dt_object *dt,
2355                                   char *op, const struct lu_buf *buf,
2356                                   struct thandle *th)
2357 {
2358         struct lod_layout_component     *lod_comp;
2359         struct lod_thread_info  *info = lod_env_info(env);
2360         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2361         struct lod_object       *lo = lod_dt_obj(dt);
2362         struct lov_comp_md_v1   *comp_v1 = buf->lb_buf;
2363         __u32   magic, id;
2364         int     i, j, rc;
2365         bool    changed = false;
2366         ENTRY;
2367
2368         if (strcmp(op, "set.flags") != 0) {
2369                 CDEBUG(D_LAYOUT, "%s: operation (%s) not supported.\n",
2370                        lod2obd(d)->obd_name, op);
2371                 RETURN(-ENOTSUPP);
2372         }
2373
2374         magic = comp_v1->lcm_magic;
2375         if (magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
2376                 lustre_swab_lov_comp_md_v1(comp_v1);
2377                 magic = comp_v1->lcm_magic;
2378         }
2379
2380         if (magic != LOV_USER_MAGIC_COMP_V1)
2381                 RETURN(-EINVAL);
2382
2383         if (comp_v1->lcm_entry_count == 0) {
2384                 CDEBUG(D_LAYOUT, "%s: entry count is zero.\n",
2385                        lod2obd(d)->obd_name);
2386                 RETURN(-EINVAL);
2387         }
2388
2389         for (i = 0; i < comp_v1->lcm_entry_count; i++) {
2390                 id = comp_v1->lcm_entries[i].lcme_id;
2391
2392                 for (j = 0; j < lo->ldo_comp_cnt; j++) {
2393                         lod_comp = &lo->ldo_comp_entries[j];
2394                         if (id == lod_comp->llc_id || id == LCME_ID_ALL) {
2395                                 lod_comp->llc_flags =
2396                                         comp_v1->lcm_entries[i].lcme_flags;
2397                                 changed = true;
2398                         }
2399                 }
2400         }
2401
2402         if (!changed) {
2403                 CDEBUG(D_LAYOUT, "%s: requested component(s) not found.\n",
2404                        lod2obd(d)->obd_name);
2405                 RETURN(-EINVAL);
2406         }
2407
2408         lod_obj_inc_layout_gen(lo);
2409
2410         info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2411         rc = lod_sub_object_declare_xattr_set(env, dt, &info->lti_buf,
2412                                               XATTR_NAME_LOV, 0, th);
2413         RETURN(rc);
2414 }
2415
2416 /**
2417  * Declare component deletion. The xattr name is XATTR_LUSTRE_LOV.del,
2418  * and the xattr value is a unique component ID or a special lcme_id.
2419  *
2420  * \param[in] env       execution environment
2421  * \param[in] dt        dt_object to be operated on
2422  * \param[in] buf       buffer contains component ID or lcme_id
2423  * \parem[in] th        thandle
2424  *
2425  * \retval      0 on success
2426  * \retval      negative errno on failure
2427  */
2428 static int lod_declare_layout_del(const struct lu_env *env,
2429                                   struct dt_object *dt,
2430                                   const struct lu_buf *buf,
2431                                   struct thandle *th)
2432 {
2433         struct lod_thread_info  *info = lod_env_info(env);
2434         struct dt_object        *next = dt_object_child(dt);
2435         struct lod_device       *d = lu2lod_dev(dt->do_lu.lo_dev);
2436         struct lod_object       *lo = lod_dt_obj(dt);
2437         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
2438         __u32   id;
2439         int     rc, i, j, left;
2440         ENTRY;
2441
2442         LASSERT(lo->ldo_is_composite);
2443
2444         id = *(__u32 *)buf->lb_buf;
2445         if (id == 0 || id == LCME_ID_NONE) {
2446                 CDEBUG(D_LAYOUT, "%s: invalid component id %#x\n",
2447                        lod2obd(d)->obd_name, id);
2448                 RETURN(-EINVAL);
2449         }
2450
2451         left = lo->ldo_comp_cnt;
2452         if (left <= 0)
2453                 RETURN(-EINVAL);
2454
2455         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
2456                 struct lod_layout_component *lod_comp;
2457
2458                 lod_comp = &lo->ldo_comp_entries[i];
2459
2460                 if (id <= LCME_ID_MAX && id != lod_comp->llc_id)
2461                         continue;
2462                 else if (id > LCME_ID_MAX && id < LCME_ID_ALL &&
2463                          !(id & lod_comp->llc_flags))
2464                         continue;
2465
2466                 if (left != (i + 1)) {
2467                         CDEBUG(D_LAYOUT, "%s: this deletion will create "
2468                                "a hole.\n", lod2obd(d)->obd_name);
2469                         RETURN(-EINVAL);
2470                 }
2471                 left--;
2472
2473                 /* Mark the component as deleted */
2474                 lod_comp->llc_id = LCME_ID_INVAL;
2475
2476                 /* Not instantiated component */
2477                 if (lod_comp->llc_stripe == NULL)
2478                         continue;
2479
2480                 LASSERT(lod_comp->llc_stripenr > 0);
2481                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
2482                         struct dt_object *obj = lod_comp->llc_stripe[j];
2483
2484                         if (obj == NULL)
2485                                 continue;
2486                         rc = lod_sub_object_declare_destroy(env, obj, th);
2487                         if (rc)
2488                                 RETURN(rc);
2489                 }
2490         }
2491
2492         LASSERTF(left >= 0, "left = %d\n", left);
2493         if (left == lo->ldo_comp_cnt) {
2494                 CDEBUG(D_LAYOUT, "%s: requested component id:%#x not found\n",
2495                        lod2obd(d)->obd_name, id);
2496                 RETURN(-EINVAL);
2497         }
2498
2499         memset(attr, 0, sizeof(*attr));
2500         attr->la_valid = LA_SIZE;
2501         rc = lod_sub_object_declare_attr_set(env, next, attr, th);
2502         if (rc)
2503                 RETURN(rc);
2504
2505         if (left > 0) {
2506                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
2507                 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
2508                                                       XATTR_NAME_LOV, 0, th);
2509         } else {
2510                 rc = lod_sub_object_declare_xattr_del(env, next, XATTR_NAME_LOV,
2511                                                       th);
2512         }
2513
2514         RETURN(rc);
2515 }
2516
2517 /**
2518  * Declare layout add/set/del operations issued by special xattr names:
2519  *
2520  * XATTR_LUSTRE_LOV.add         add component(s) to existing file
2521  * XATTR_LUSTRE_LOV.del         delete component(s) from existing file
2522  * XATTR_LUSTRE_LOV.set.$field  set specified field of certain component(s)
2523  *
2524  * \param[in] env       execution environment
2525  * \param[in] dt        object
2526  * \param[in] name      name of xattr
2527  * \param[in] buf       lu_buf contains xattr value
2528  * \param[in] th        transaction handle
2529  *
2530  * \retval              0 on success
2531  * \retval              negative if failed
2532  */
2533 static int lod_declare_modify_layout(const struct lu_env *env,
2534                                      struct dt_object *dt,
2535                                      const char *name,
2536                                      const struct lu_buf *buf,
2537                                      struct thandle *th)
2538 {
2539         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
2540         struct lod_object *lo = lod_dt_obj(dt);
2541         struct dt_object *next = dt_object_child(&lo->ldo_obj);
2542         char *op;
2543         int rc, len = strlen(XATTR_LUSTRE_LOV);
2544         ENTRY;
2545
2546         LASSERT(dt_object_exists(dt));
2547
2548         if (strlen(name) <= len || name[len] != '.') {
2549                 CDEBUG(D_LAYOUT, "%s: invalid xattr name: %s\n",
2550                        lod2obd(d)->obd_name, name);
2551                 RETURN(-EINVAL);
2552         }
2553         len++;
2554
2555         dt_write_lock(env, next, 0);
2556         rc = lod_load_striping_locked(env, lo);
2557         if (rc)
2558                 GOTO(unlock, rc);
2559
2560         /* the layout to be modified must be a composite layout */
2561         if (!lo->ldo_is_composite) {
2562                 CDEBUG(D_LAYOUT, "%s: object "DFID" isn't a composite file.\n",
2563                        lod2obd(d)->obd_name, PFID(lu_object_fid(&dt->do_lu)));
2564                 GOTO(unlock, rc = -EINVAL);
2565         }
2566
2567         op = (char *)name + len;
2568         if (strcmp(op, "add") == 0) {
2569                 rc = lod_declare_layout_add(env, dt, buf, th);
2570         } else if (strcmp(op, "del") == 0) {
2571                 rc = lod_declare_layout_del(env, dt, buf, th);
2572         } else if (strncmp(op, "set", strlen("set")) == 0) {
2573                 rc = lod_declare_layout_set(env, dt, op, buf, th);
2574         } else  {
2575                 CDEBUG(D_LAYOUT, "%s: unsupported xattr name:%s\n",
2576                        lod2obd(d)->obd_name, name);
2577                 GOTO(unlock, rc = -ENOTSUPP);
2578         }
2579 unlock:
2580         if (rc)
2581                 lod_object_free_striping(env, lo);
2582         dt_write_unlock(env, next);
2583
2584         RETURN(rc);
2585 }
2586
2587 /**
2588  * Implementation of dt_object_operations::do_declare_xattr_set.
2589  *
2590  * \see dt_object_operations::do_declare_xattr_set() in the API description
2591  * for details.
2592  *
2593  * the extension to the API:
2594  *   - declaring LOVEA requests striping creation
2595  *   - LU_XATTR_REPLACE means layout swap
2596  */
2597 static int lod_declare_xattr_set(const struct lu_env *env,
2598                                  struct dt_object *dt,
2599                                  const struct lu_buf *buf,
2600                                  const char *name, int fl,
2601                                  struct thandle *th)
2602 {
2603         struct dt_object *next = dt_object_child(dt);
2604         struct lu_attr   *attr = &lod_env_info(env)->lti_attr;
2605         __u32             mode;
2606         int               rc;
2607         ENTRY;
2608
2609         mode = dt->do_lu.lo_header->loh_attr & S_IFMT;
2610         if ((S_ISREG(mode) || mode == 0) && !(fl & LU_XATTR_REPLACE) &&
2611             (strcmp(name, XATTR_NAME_LOV) == 0 ||
2612              strcmp(name, XATTR_LUSTRE_LOV) == 0)) {
2613                 /*
2614                  * this is a request to create object's striping.
2615                  *
2616                  * allow to declare predefined striping on a new (!mode) object
2617                  * which is supposed to be replay of regular file creation
2618                  * (when LOV setting is declared)
2619                  *
2620                  * LU_XATTR_REPLACE is set to indicate a layout swap
2621                  */
2622                 if (dt_object_exists(dt)) {
2623                         rc = dt_attr_get(env, next, attr);
2624                         if (rc)
2625                                 RETURN(rc);
2626                 } else {
2627                         memset(attr, 0, sizeof(*attr));
2628                         attr->la_valid = LA_TYPE | LA_MODE;
2629                         attr->la_mode = S_IFREG;
2630                 }
2631                 rc = lod_declare_striped_object(env, dt, attr, buf, th);
2632         } else if (S_ISREG(mode) &&
2633                    strlen(name) > strlen(XATTR_LUSTRE_LOV) + 1 &&
2634                    strncmp(name, XATTR_LUSTRE_LOV,
2635                            strlen(XATTR_LUSTRE_LOV)) == 0) {
2636                 /*
2637                  * this is a request to modify object's striping.
2638                  * add/set/del component(s).
2639                  */
2640                 if (!dt_object_exists(dt))
2641                         RETURN(-ENOENT);
2642
2643                 rc = lod_declare_modify_layout(env, dt, name, buf, th);
2644         } else if (S_ISDIR(mode)) {
2645                 rc = lod_dir_declare_xattr_set(env, dt, buf, name, fl, th);
2646         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
2647                 rc = lod_object_replace_parent_fid(env, dt, th, true);
2648         } else {
2649                 rc = lod_sub_object_declare_xattr_set(env, next, buf, name,
2650                                                       fl, th);
2651         }
2652
2653         RETURN(rc);
2654 }
2655
2656 /**
2657  * Apply xattr changes to the object.
2658  *
2659  * Applies xattr changes to the object and the stripes if the latter exist.
2660  *
2661  * \param[in] env       execution environment
2662  * \param[in] dt        object
2663  * \param[in] buf       buffer pointing to the new value of xattr
2664  * \param[in] name      name of xattr
2665  * \param[in] fl        flags
2666  * \param[in] th        transaction handle
2667  *
2668  * \retval              0 on success
2669  * \retval              negative if failed
2670  */
2671 static int lod_xattr_set_internal(const struct lu_env *env,
2672                                   struct dt_object *dt,
2673                                   const struct lu_buf *buf,
2674                                   const char *name, int fl,
2675                                   struct thandle *th)
2676 {
2677         struct dt_object        *next = dt_object_child(dt);
2678         struct lod_object       *lo = lod_dt_obj(dt);
2679         int                     rc;
2680         int                     i;
2681         ENTRY;
2682
2683         rc = lod_sub_object_xattr_set(env, next, buf, name, fl, th);
2684         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2685                 RETURN(rc);
2686
2687         /* Note: Do not set LinkEA on sub-stripes, otherwise
2688          * it will confuse the fid2path process(see mdt_path_current()).
2689          * The linkEA between master and sub-stripes is set in
2690          * lod_xattr_set_lmv(). */
2691         if (lo->ldo_dir_stripenr == 0 || strcmp(name, XATTR_NAME_LINK) == 0)
2692                 RETURN(0);
2693
2694         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2695                 LASSERT(lo->ldo_stripe[i]);
2696
2697                 rc = lod_sub_object_xattr_set(env, lo->ldo_stripe[i], buf, name,
2698                                               fl, th);
2699                 if (rc != 0)
2700                         break;
2701         }
2702
2703         RETURN(rc);
2704 }
2705
2706 /**
2707  * Delete an extended attribute.
2708  *
2709  * Deletes specified xattr from the object and the stripes if the latter exist.
2710  *
2711  * \param[in] env       execution environment
2712  * \param[in] dt        object
2713  * \param[in] name      name of xattr
2714  * \param[in] th        transaction handle
2715  *
2716  * \retval              0 on success
2717  * \retval              negative if failed
2718  */
2719 static int lod_xattr_del_internal(const struct lu_env *env,
2720                                   struct dt_object *dt,
2721                                   const char *name, struct thandle *th)
2722 {
2723         struct dt_object        *next = dt_object_child(dt);
2724         struct lod_object       *lo = lod_dt_obj(dt);
2725         int                     rc;
2726         int                     i;
2727         ENTRY;
2728
2729         rc = lod_sub_object_xattr_del(env, next, name, th);
2730         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
2731                 RETURN(rc);
2732
2733         if (lo->ldo_dir_stripenr == 0)
2734                 RETURN(rc);
2735
2736         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2737                 LASSERT(lo->ldo_stripe[i]);
2738
2739                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name,
2740                                               th);
2741                 if (rc != 0)
2742                         break;
2743         }
2744
2745         RETURN(rc);
2746 }
2747
2748 /**
2749  * Set default striping on a directory.
2750  *
2751  * Sets specified striping on a directory object unless it matches the default
2752  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2753  * EA. This striping will be used when regular file is being created in this
2754  * directory.
2755  *
2756  * \param[in] env       execution environment
2757  * \param[in] dt        the striped object
2758  * \param[in] buf       buffer with the striping
2759  * \param[in] name      name of EA
2760  * \param[in] fl        xattr flag (see OSD API description)
2761  * \param[in] th        transaction handle
2762  *
2763  * \retval              0 on success
2764  * \retval              negative if failed
2765  */
2766 static int lod_xattr_set_lov_on_dir(const struct lu_env *env,
2767                                     struct dt_object *dt,
2768                                     const struct lu_buf *buf,
2769                                     const char *name, int fl,
2770                                     struct thandle *th)
2771 {
2772         struct lov_user_md_v1   *lum;
2773         struct lov_user_md_v3   *v3 = NULL;
2774         const char              *pool_name = NULL;
2775         int                      rc;
2776         bool                     is_del;
2777         ENTRY;
2778
2779         LASSERT(buf != NULL && buf->lb_buf != NULL);
2780         lum = buf->lb_buf;
2781
2782         switch (lum->lmm_magic) {
2783         case LOV_USER_MAGIC_V3:
2784                 v3 = buf->lb_buf;
2785                 if (v3->lmm_pool_name[0] != '\0')
2786                         pool_name = v3->lmm_pool_name;
2787                 /* fall through */
2788         case LOV_USER_MAGIC_V1:
2789                 /* if { size, offset, count } = { 0, -1, 0 } and no pool
2790                  * (i.e. all default values specified) then delete default
2791                  * striping from dir. */
2792                 CDEBUG(D_LAYOUT,
2793                        "set default striping: sz %u # %u offset %d %s %s\n",
2794                        (unsigned)lum->lmm_stripe_size,
2795                        (unsigned)lum->lmm_stripe_count,
2796                        (int)lum->lmm_stripe_offset,
2797                        v3 ? "from" : "", v3 ? v3->lmm_pool_name : "");
2798
2799                 is_del = LOVEA_DELETE_VALUES(lum->lmm_stripe_size,
2800                                              lum->lmm_stripe_count,
2801                                              lum->lmm_stripe_offset,
2802                                              pool_name);
2803                 break;
2804         case LOV_USER_MAGIC_COMP_V1:
2805                 is_del = false;
2806                 break;
2807         default:
2808                 CERROR("Invalid magic %x\n", lum->lmm_magic);
2809                 RETURN(-EINVAL);
2810         }
2811
2812         if (is_del) {
2813                 rc = lod_xattr_del_internal(env, dt, name, th);
2814                 if (rc == -ENODATA)
2815                         rc = 0;
2816         } else {
2817                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2818         }
2819
2820         RETURN(rc);
2821 }
2822
2823 /**
2824  * Set default striping on a directory object.
2825  *
2826  * Sets specified striping on a directory object unless it matches the default
2827  * striping (LOVEA_DELETE_VALUES() macro). In the latter case remove existing
2828  * EA. This striping will be used when a new directory is being created in the
2829  * directory.
2830  *
2831  * \param[in] env       execution environment
2832  * \param[in] dt        the striped object
2833  * \param[in] buf       buffer with the striping
2834  * \param[in] name      name of EA
2835  * \param[in] fl        xattr flag (see OSD API description)
2836  * \param[in] th        transaction handle
2837  *
2838  * \retval              0 on success
2839  * \retval              negative if failed
2840  */
2841 static int lod_xattr_set_default_lmv_on_dir(const struct lu_env *env,
2842                                             struct dt_object *dt,
2843                                             const struct lu_buf *buf,
2844                                             const char *name, int fl,
2845                                             struct thandle *th)
2846 {
2847         struct lmv_user_md_v1   *lum;
2848         int                      rc;
2849         ENTRY;
2850
2851         LASSERT(buf != NULL && buf->lb_buf != NULL);
2852         lum = buf->lb_buf;
2853
2854         CDEBUG(D_OTHER, "set default stripe_count # %u stripe_offset %d\n",
2855               le32_to_cpu(lum->lum_stripe_count),
2856               (int)le32_to_cpu(lum->lum_stripe_offset));
2857
2858         if (LMVEA_DELETE_VALUES((le32_to_cpu(lum->lum_stripe_count)),
2859                                  le32_to_cpu(lum->lum_stripe_offset)) &&
2860                                 le32_to_cpu(lum->lum_magic) == LMV_USER_MAGIC) {
2861                 rc = lod_xattr_del_internal(env, dt, name, th);
2862                 if (rc == -ENODATA)
2863                         rc = 0;
2864         } else {
2865                 rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
2866                 if (rc != 0)
2867                         RETURN(rc);
2868         }
2869
2870         RETURN(rc);
2871 }
2872
2873 /**
2874  * Turn directory into a striped directory.
2875  *
2876  * During replay the client sends the striping created before MDT
2877  * failure, then the layer above LOD sends this defined striping
2878  * using ->do_xattr_set(), so LOD uses this method to replay creation
2879  * of the stripes. Notice the original information for the striping
2880  * (#stripes, FIDs, etc) was transferred in declare path.
2881  *
2882  * \param[in] env       execution environment
2883  * \param[in] dt        the striped object
2884  * \param[in] buf       not used currently
2885  * \param[in] name      not used currently
2886  * \param[in] fl        xattr flag (see OSD API description)
2887  * \param[in] th        transaction handle
2888  *
2889  * \retval              0 on success
2890  * \retval              negative if failed
2891  */
2892 static int lod_xattr_set_lmv(const struct lu_env *env, struct dt_object *dt,
2893                              const struct lu_buf *buf, const char *name,
2894                              int fl, struct thandle *th)
2895 {
2896         struct lod_object       *lo = lod_dt_obj(dt);
2897         struct lod_thread_info  *info = lod_env_info(env);
2898         struct lu_attr          *attr = &info->lti_attr;
2899         struct dt_object_format *dof = &info->lti_format;
2900         struct lu_buf           lmv_buf;
2901         struct lu_buf           slave_lmv_buf;
2902         struct lmv_mds_md_v1    *lmm;
2903         struct lmv_mds_md_v1    *slave_lmm = NULL;
2904         struct dt_insert_rec    *rec = &info->lti_dt_rec;
2905         int                     i;
2906         int                     rc;
2907         ENTRY;
2908
2909         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
2910                 RETURN(-ENOTDIR);
2911
2912         /* The stripes are supposed to be allocated in declare phase,
2913          * if there are no stripes being allocated, it will skip */
2914         if (lo->ldo_dir_stripenr == 0)
2915                 RETURN(0);
2916
2917         rc = dt_attr_get(env, dt_object_child(dt), attr);
2918         if (rc != 0)
2919                 RETURN(rc);
2920
2921         attr->la_valid = LA_ATIME | LA_MTIME | LA_CTIME |
2922                          LA_MODE | LA_UID | LA_GID | LA_TYPE | LA_PROJID;
2923         dof->dof_type = DFT_DIR;
2924
2925         rc = lod_prep_lmv_md(env, dt, &lmv_buf);
2926         if (rc != 0)
2927                 RETURN(rc);
2928         lmm = lmv_buf.lb_buf;
2929
2930         OBD_ALLOC_PTR(slave_lmm);
2931         if (slave_lmm == NULL)
2932                 RETURN(-ENOMEM);
2933
2934         lod_prep_slave_lmv_md(slave_lmm, lmm);
2935         slave_lmv_buf.lb_buf = slave_lmm;
2936         slave_lmv_buf.lb_len = sizeof(*slave_lmm);
2937
2938         rec->rec_type = S_IFDIR;
2939         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
2940                 struct dt_object *dto;
2941                 char             *stripe_name = info->lti_key;
2942                 struct lu_name          *sname;
2943                 struct linkea_data       ldata          = { NULL };
2944                 struct lu_buf            linkea_buf;
2945
2946                 dto = lo->ldo_stripe[i];
2947
2948                 dt_write_lock(env, dto, MOR_TGT_CHILD);
2949                 rc = lod_sub_object_create(env, dto, attr, NULL, dof,
2950                                            th);
2951                 if (rc != 0) {
2952                         dt_write_unlock(env, dto);
2953                         GOTO(out, rc);
2954                 }
2955
2956                 rc = lod_sub_object_ref_add(env, dto, th);
2957                 dt_write_unlock(env, dto);
2958                 if (rc != 0)
2959                         GOTO(out, rc);
2960
2961                 rec->rec_fid = lu_object_fid(&dto->do_lu);
2962                 rc = lod_sub_object_index_insert(env, dto,
2963                                 (const struct dt_rec *)rec,
2964                                 (const struct dt_key *)dot, th, 0);
2965                 if (rc != 0)
2966                         GOTO(out, rc);
2967
2968                 rec->rec_fid = lu_object_fid(&dt->do_lu);
2969                 rc = lod_sub_object_index_insert(env, dto, (struct dt_rec *)rec,
2970                                (const struct dt_key *)dotdot, th, 0);
2971                 if (rc != 0)
2972                         GOTO(out, rc);
2973
2974                 if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SLAVE_LMV) ||
2975                     cfs_fail_val != i) {
2976                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_LMV) &&
2977                             cfs_fail_val == i)
2978                                 slave_lmm->lmv_master_mdt_index =
2979                                                         cpu_to_le32(i + 1);
2980                         else
2981                                 slave_lmm->lmv_master_mdt_index =
2982                                                         cpu_to_le32(i);
2983
2984                         rc = lod_sub_object_xattr_set(env, dto, &slave_lmv_buf,
2985                                                       XATTR_NAME_LMV, fl, th);
2986                         if (rc != 0)
2987                                 GOTO(out, rc);
2988                 }
2989
2990                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_BAD_SLAVE_NAME) &&
2991                     cfs_fail_val == i)
2992                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2993                                  PFID(lu_object_fid(&dto->do_lu)), i + 1);
2994                 else
2995                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
2996                                  PFID(lu_object_fid(&dto->do_lu)), i);
2997
2998                 sname = lod_name_get(env, stripe_name, strlen(stripe_name));
2999                 rc = linkea_links_new(&ldata, &info->lti_linkea_buf,
3000                                       sname, lu_object_fid(&dt->do_lu));
3001                 if (rc != 0)
3002                         GOTO(out, rc);
3003
3004                 linkea_buf.lb_buf = ldata.ld_buf->lb_buf;
3005                 linkea_buf.lb_len = ldata.ld_leh->leh_len;
3006                 rc = lod_sub_object_xattr_set(env, dto, &linkea_buf,
3007                                         XATTR_NAME_LINK, 0, th);
3008                 if (rc != 0)
3009                         GOTO(out, rc);
3010
3011                 rec->rec_fid = lu_object_fid(&dto->do_lu);
3012                 rc = lod_sub_object_index_insert(env, dt_object_child(dt),
3013                                (const struct dt_rec *)rec,
3014                                (const struct dt_key *)stripe_name, th, 0);
3015                 if (rc != 0)
3016                         GOTO(out, rc);
3017
3018                 rc = lod_sub_object_ref_add(env, dt_object_child(dt), th);
3019                 if (rc != 0)
3020                         GOTO(out, rc);
3021         }
3022
3023         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MASTER_LMV))
3024                 rc = lod_sub_object_xattr_set(env, dt_object_child(dt),
3025                                               &lmv_buf, XATTR_NAME_LMV, fl, th);
3026 out:
3027         if (slave_lmm != NULL)
3028                 OBD_FREE_PTR(slave_lmm);
3029
3030         RETURN(rc);
3031 }
3032
3033 /**
3034  * Helper function to declare/execute creation of a striped directory
3035  *
3036  * Called in declare/create object path, prepare striping for a directory
3037  * and prepare defaults data striping for the objects to be created in
3038  * that directory. Notice the function calls "declaration" or "execution"
3039  * methods depending on \a declare param. This is a consequence of the
3040  * current approach while we don't have natural distributed transactions:
3041  * we basically execute non-local updates in the declare phase. So, the
3042  * arguments for the both phases are the same and this is the reason for
3043  * this function to exist.
3044  *
3045  * \param[in] env       execution environment
3046  * \param[in] dt        object
3047  * \param[in] attr      attributes the stripes will be created with
3048  * \param[in] dof       format of stripes (see OSD API description)
3049  * \param[in] th        transaction handle
3050  * \param[in] declare   where to call "declare" or "execute" methods
3051  *
3052  * \retval              0 on success
3053  * \retval              negative if failed
3054  */
3055 static int lod_dir_striping_create_internal(const struct lu_env *env,
3056                                             struct dt_object *dt,
3057                                             struct lu_attr *attr,
3058                                             struct dt_object_format *dof,
3059                                             struct thandle *th,
3060                                             bool declare)
3061 {
3062         struct lod_thread_info *info = lod_env_info(env);
3063         struct lod_object *lo = lod_dt_obj(dt);
3064         const struct lod_default_striping *lds = lo->ldo_def_striping;
3065         int rc;
3066         ENTRY;
3067
3068         LASSERT(ergo(lds != NULL,
3069                      lds->lds_def_striping_set ||
3070                      lds->lds_dir_def_striping_set));
3071
3072         if (!LMVEA_DELETE_VALUES(lo->ldo_dir_stripenr,
3073                                  lo->ldo_dir_stripe_offset)) {
3074                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3075                 int stripe_count = lo->ldo_dir_stripenr;
3076
3077                 if (info->lti_ea_store_size < sizeof(*v1)) {
3078                         rc = lod_ea_store_resize(info, sizeof(*v1));
3079                         if (rc != 0)
3080                                 RETURN(rc);
3081                         v1 = info->lti_ea_store;
3082                 }
3083
3084                 memset(v1, 0, sizeof(*v1));
3085                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3086                 v1->lum_stripe_count = cpu_to_le32(stripe_count);
3087                 v1->lum_stripe_offset =
3088                                 cpu_to_le32(lo->ldo_dir_stripe_offset);
3089
3090                 info->lti_buf.lb_buf = v1;
3091                 info->lti_buf.lb_len = sizeof(*v1);
3092
3093                 if (declare)
3094                         rc = lod_declare_xattr_set_lmv(env, dt, attr,
3095                                                        &info->lti_buf, dof, th);
3096                 else
3097                         rc = lod_xattr_set_lmv(env, dt, &info->lti_buf,
3098                                                XATTR_NAME_LMV, 0, th);
3099                 if (rc != 0)
3100                         RETURN(rc);
3101         }
3102
3103         /* Transfer default LMV striping from the parent */
3104         if (lds != NULL && lds->lds_dir_def_striping_set &&
3105             !LMVEA_DELETE_VALUES(lds->lds_dir_def_stripenr,
3106                                  lds->lds_dir_def_stripe_offset)) {
3107                 struct lmv_user_md_v1 *v1 = info->lti_ea_store;
3108
3109                 if (info->lti_ea_store_size < sizeof(*v1)) {
3110                         rc = lod_ea_store_resize(info, sizeof(*v1));
3111                         if (rc != 0)
3112                                 RETURN(rc);
3113                         v1 = info->lti_ea_store;
3114                 }
3115
3116                 memset(v1, 0, sizeof(*v1));
3117                 v1->lum_magic = cpu_to_le32(LMV_USER_MAGIC);
3118                 v1->lum_stripe_count = cpu_to_le32(lds->lds_dir_def_stripenr);
3119                 v1->lum_stripe_offset =
3120                                 cpu_to_le32(lds->lds_dir_def_stripe_offset);
3121                 v1->lum_hash_type =
3122                                 cpu_to_le32(lds->lds_dir_def_hash_type);
3123
3124                 info->lti_buf.lb_buf = v1;
3125                 info->lti_buf.lb_len = sizeof(*v1);
3126                 if (declare)
3127                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3128                                                        XATTR_NAME_DEFAULT_LMV,
3129                                                        0, th);
3130                 else
3131                         rc = lod_xattr_set_default_lmv_on_dir(env, dt,
3132                                                   &info->lti_buf,
3133                                                   XATTR_NAME_DEFAULT_LMV, 0,
3134                                                   th);
3135                 if (rc != 0)
3136                         RETURN(rc);
3137         }
3138
3139         /* Transfer default LOV striping from the parent */
3140         if (lds != NULL && lds->lds_def_striping_set &&
3141             lds->lds_def_comp_cnt != 0) {
3142                 struct lov_mds_md *lmm;
3143                 int lmm_size = lod_comp_md_size(lo, true);
3144
3145                 if (info->lti_ea_store_size < lmm_size) {
3146                         rc = lod_ea_store_resize(info, lmm_size);
3147                         if (rc != 0)
3148                                 RETURN(rc);
3149                 }
3150                 lmm = info->lti_ea_store;
3151
3152                 rc = lod_generate_lovea(env, lo, lmm, &lmm_size, true);
3153                 if (rc != 0)
3154                         RETURN(rc);
3155
3156                 info->lti_buf.lb_buf = lmm;
3157                 info->lti_buf.lb_len = lmm_size;
3158
3159                 if (declare)
3160                         rc = lod_dir_declare_xattr_set(env, dt, &info->lti_buf,
3161                                                        XATTR_NAME_LOV, 0, th);
3162                 else
3163                         rc = lod_xattr_set_lov_on_dir(env, dt, &info->lti_buf,
3164                                                       XATTR_NAME_LOV, 0, th);
3165                 if (rc != 0)
3166                         RETURN(rc);
3167         }
3168
3169         RETURN(0);
3170 }
3171
3172 static int lod_declare_dir_striping_create(const struct lu_env *env,
3173                                            struct dt_object *dt,
3174                                            struct lu_attr *attr,
3175                                            struct dt_object_format *dof,
3176                                            struct thandle *th)
3177 {
3178         return lod_dir_striping_create_internal(env, dt, attr, dof, th, true);
3179 }
3180
3181 static int lod_dir_striping_create(const struct lu_env *env,
3182                                    struct dt_object *dt,
3183                                    struct lu_attr *attr,
3184                                    struct dt_object_format *dof,
3185                                    struct thandle *th)
3186 {
3187         return lod_dir_striping_create_internal(env, dt, attr, dof, th, false);
3188 }
3189
3190 /**
3191  * Make LOV EA for striped object.
3192  *
3193  * Generate striping information and store it in the LOV EA of the given
3194  * object. The caller must ensure nobody else is calling the function
3195  * against the object concurrently. The transaction must be started.
3196  * FLDB service must be running as well; it's used to map FID to the target,
3197  * which is stored in LOV EA.
3198  *
3199  * \param[in] env               execution environment for this thread
3200  * \param[in] lo                LOD object
3201  * \param[in] th                transaction handle
3202  *
3203  * \retval                      0 if LOV EA is stored successfully
3204  * \retval                      negative error number on failure
3205  */
3206 static int lod_generate_and_set_lovea(const struct lu_env *env,
3207                                       struct lod_object *lo,
3208                                       struct thandle *th)
3209 {
3210         struct lod_thread_info  *info = lod_env_info(env);
3211         struct dt_object        *next = dt_object_child(&lo->ldo_obj);
3212         struct lov_mds_md_v1    *lmm;
3213         int                      rc, lmm_size;
3214         ENTRY;
3215
3216         LASSERT(lo);
3217
3218         if (lo->ldo_comp_cnt == 0) {
3219                 lod_object_free_striping(env, lo);
3220                 rc = lod_sub_object_xattr_del(env, next, XATTR_NAME_LOV, th);
3221                 RETURN(rc);
3222         }
3223
3224         lmm_size = lod_comp_md_size(lo, false);
3225         if (info->lti_ea_store_size < lmm_size) {
3226                 rc = lod_ea_store_resize(info, lmm_size);
3227                 if (rc)
3228                         RETURN(rc);
3229         }
3230         lmm = info->lti_ea_store;
3231
3232         rc = lod_generate_lovea(env, lo, lmm, &lmm_size, false);
3233         if (rc)
3234                 RETURN(rc);
3235
3236         info->lti_buf.lb_buf = lmm;
3237         info->lti_buf.lb_len = lmm_size;
3238         rc = lod_sub_object_xattr_set(env, next, &info->lti_buf,
3239                                       XATTR_NAME_LOV, 0, th);
3240         RETURN(rc);
3241 }
3242
3243 /**
3244  * Delete layout component(s)
3245  *
3246  * \param[in] env       execution environment for this thread
3247  * \param[in] dt        object
3248  * \param[in] th        transaction handle
3249  *
3250  * \retval      0 on success
3251  * \retval      negative error number on failure
3252  */
3253 static int lod_layout_del(const struct lu_env *env, struct dt_object *dt,
3254                           struct thandle *th)
3255 {
3256         struct lod_layout_component     *lod_comp;
3257         struct lod_object       *lo = lod_dt_obj(dt);
3258         struct dt_object        *next = dt_object_child(dt);
3259         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
3260         int     rc, i, j, left;
3261
3262         LASSERT(lo->ldo_is_composite);
3263         LASSERT(lo->ldo_comp_cnt > 0 && lo->ldo_comp_entries != NULL);
3264
3265         left = lo->ldo_comp_cnt;
3266         for (i = (lo->ldo_comp_cnt - 1); i >= 0; i--) {
3267                 lod_comp = &lo->ldo_comp_entries[i];
3268
3269                 if (lod_comp->llc_id != LCME_ID_INVAL)
3270                         break;
3271                 left--;
3272
3273                 /* Not instantiated component */
3274                 if (lod_comp->llc_stripe == NULL)
3275                         continue;
3276
3277                 LASSERT(lod_comp->llc_stripenr > 0);
3278                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
3279                         struct dt_object *obj = lod_comp->llc_stripe[j];
3280
3281                         if (obj == NULL)
3282                                 continue;
3283                         rc = lod_sub_object_destroy(env, obj, th);
3284                         if (rc)
3285                                 GOTO(out, rc);
3286
3287                         lu_object_put(env, &obj->do_lu);
3288                         lod_comp->llc_stripe[j] = NULL;
3289                 }
3290                 OBD_FREE(lod_comp->llc_stripe, sizeof(struct dt_object *) *
3291                                         lod_comp->llc_stripes_allocated);
3292                 lod_comp->llc_stripe = NULL;
3293                 lod_comp->llc_stripes_allocated = 0;
3294                 lod_obj_set_pool(lo, i, NULL);
3295                 if (lod_comp->llc_ostlist.op_array) {
3296                         OBD_FREE(lod_comp->llc_ostlist.op_array,
3297                                  lod_comp->llc_ostlist.op_size);
3298                         lod_comp->llc_ostlist.op_array = NULL;
3299                         lod_comp->llc_ostlist.op_size = 0;
3300                 }
3301         }
3302
3303         LASSERTF(left >= 0 && left < lo->ldo_comp_cnt, "left = %d\n", left);
3304         if (left > 0) {
3305                 struct lod_layout_component     *comp_array;
3306
3307                 OBD_ALLOC(comp_array, sizeof(*comp_array) * left);
3308                 if (comp_array == NULL)
3309                         GOTO(out, rc = -ENOMEM);
3310
3311                 memcpy(&comp_array[0], &lo->ldo_comp_entries[0],
3312                        sizeof(*comp_array) * left);
3313
3314                 OBD_FREE(lo->ldo_comp_entries,
3315                          sizeof(*comp_array) * lo->ldo_comp_cnt);
3316                 lo->ldo_comp_entries = comp_array;
3317                 lo->ldo_comp_cnt = left;
3318                 lod_obj_inc_layout_gen(lo);
3319         } else {
3320                 lod_free_comp_entries(lo);
3321         }
3322
3323         LASSERT(dt_object_exists(dt));
3324         rc = dt_attr_get(env, next, attr);
3325         if (rc)
3326                 GOTO(out, rc);
3327
3328         if (attr->la_size > 0) {
3329                 attr->la_size = 0;
3330                 attr->la_valid = LA_SIZE;
3331                 rc = lod_sub_object_attr_set(env, next, attr, th);
3332                 if (rc)
3333                         GOTO(out, rc);
3334         }
3335
3336         rc = lod_generate_and_set_lovea(env, lo, th);
3337         EXIT;
3338 out:
3339         if (rc)
3340                 lod_object_free_striping(env, lo);
3341         return rc;
3342 }
3343
3344 /**
3345  * Implementation of dt_object_operations::do_xattr_set.
3346  *
3347  * Sets specified extended attribute on the object. Three types of EAs are
3348  * special:
3349  *   LOV EA - stores striping for a regular file or default striping (when set
3350  *            on a directory)
3351  *   LMV EA - stores a marker for the striped directories
3352  *   DMV EA - stores default directory striping
3353  *
3354  * When striping is applied to a non-striped existing object (this is called
3355  * late striping), then LOD notices the caller wants to turn the object into a
3356  * striped one. The stripe objects are created and appropriate EA is set:
3357  * LOV EA storing all the stripes directly or LMV EA storing just a small header
3358  * with striping configuration.
3359  *
3360  * \see dt_object_operations::do_xattr_set() in the API description for details.
3361  */
3362 static int lod_xattr_set(const struct lu_env *env,
3363                          struct dt_object *dt, const struct lu_buf *buf,
3364                          const char *name, int fl, struct thandle *th)
3365 {
3366         struct dt_object        *next = dt_object_child(dt);
3367         int                      rc;
3368         ENTRY;
3369
3370         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3371             strcmp(name, XATTR_NAME_LMV) == 0) {
3372                 struct lmv_mds_md_v1 *lmm = buf->lb_buf;
3373
3374                 if (lmm != NULL && le32_to_cpu(lmm->lmv_hash_type) &
3375                                                 LMV_HASH_FLAG_MIGRATION)
3376                         rc = lod_sub_object_xattr_set(env, next, buf, name, fl,
3377                                                       th);
3378                 else
3379                         rc = lod_dir_striping_create(env, dt, NULL, NULL, th);
3380
3381                 RETURN(rc);
3382         }
3383
3384         if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3385             strcmp(name, XATTR_NAME_LOV) == 0) {
3386                 /* default LOVEA */
3387                 rc = lod_xattr_set_lov_on_dir(env, dt, buf, name, fl, th);
3388                 RETURN(rc);
3389         } else if (S_ISDIR(dt->do_lu.lo_header->loh_attr) &&
3390                    strcmp(name, XATTR_NAME_DEFAULT_LMV) == 0) {
3391                 /* default LMVEA */
3392                 rc = lod_xattr_set_default_lmv_on_dir(env, dt, buf, name, fl,
3393                                                       th);
3394                 RETURN(rc);
3395         } else if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
3396                    (!strcmp(name, XATTR_NAME_LOV) ||
3397                     !strncmp(name, XATTR_LUSTRE_LOV,
3398                              strlen(XATTR_LUSTRE_LOV)))) {
3399                 /* in case of lov EA swap, just set it
3400                  * if not, it is a replay so check striping match what we
3401                  * already have during req replay, declare_xattr_set()
3402                  * defines striping, then create() does the work */
3403                 if (fl & LU_XATTR_REPLACE) {
3404                         /* free stripes, then update disk */
3405                         lod_object_free_striping(env, lod_dt_obj(dt));
3406
3407                         rc = lod_sub_object_xattr_set(env, next, buf, name,
3408                                                       fl, th);
3409                 } else if (dt_object_remote(dt)) {
3410                         /* This only happens during migration, see
3411                          * mdd_migrate_create(), in which Master MDT will
3412                          * create a remote target object, and only set
3413                          * (migrating) stripe EA on the remote object,
3414                          * and does not need creating each stripes. */
3415                         rc = lod_sub_object_xattr_set(env, next, buf, name,
3416                                                       fl, th);
3417                 } else if (strcmp(name, XATTR_LUSTRE_LOV".del") == 0) {
3418                         /* delete component(s) */
3419                         LASSERT(lod_dt_obj(dt)->ldo_comp_cached);
3420                         rc = lod_layout_del(env, dt, th);
3421                 } else {
3422                         /*
3423                          * When 'name' is XATTR_LUSTRE_LOV or XATTR_NAME_LOV,
3424                          * it's going to create create file with specified
3425                          * component(s), the striping must have not being
3426                          * cached in this case;
3427                          *
3428                          * Otherwise, it's going to add/change component(s) to
3429                          * an existing file, the striping must have been cached
3430                          * in this case.
3431                          */
3432                         LASSERT(equi(!strcmp(name, XATTR_LUSTRE_LOV) ||
3433                                      !strcmp(name, XATTR_NAME_LOV),
3434                                 !lod_dt_obj(dt)->ldo_comp_cached));
3435
3436                         rc = lod_striping_create(env, dt, NULL, NULL, th);
3437                 }
3438                 RETURN(rc);
3439         } else if (strcmp(name, XATTR_NAME_FID) == 0) {
3440                 rc = lod_object_replace_parent_fid(env, dt, th, false);
3441
3442                 RETURN(rc);
3443         }
3444
3445         /* then all other xattr */
3446         rc = lod_xattr_set_internal(env, dt, buf, name, fl, th);
3447
3448         RETURN(rc);
3449 }
3450
3451 /**
3452  * Implementation of dt_object_operations::do_declare_xattr_del.
3453  *
3454  * \see dt_object_operations::do_declare_xattr_del() in the API description
3455  * for details.
3456  */
3457 static int lod_declare_xattr_del(const struct lu_env *env,
3458                                  struct dt_object *dt, const char *name,
3459                                  struct thandle *th)
3460 {
3461         struct lod_object       *lo = lod_dt_obj(dt);
3462         int                     rc;
3463         int                     i;
3464         ENTRY;
3465
3466         rc = lod_sub_object_declare_xattr_del(env, dt_object_child(dt),
3467                                               name, th);
3468         if (rc != 0)
3469                 RETURN(rc);
3470
3471         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
3472                 RETURN(0);
3473
3474         /* set xattr to each stripes, if needed */
3475         rc = lod_load_striping(env, lo);
3476         if (rc != 0)
3477                 RETURN(rc);
3478
3479         if (lo->ldo_dir_stripenr == 0)
3480                 RETURN(0);
3481
3482         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3483                 LASSERT(lo->ldo_stripe[i]);
3484                 rc = lod_sub_object_declare_xattr_del(env, lo->ldo_stripe[i],
3485                                                       name, th);
3486                 if (rc != 0)
3487                         break;
3488         }
3489
3490         RETURN(rc);
3491 }
3492
3493 /**
3494  * Implementation of dt_object_operations::do_xattr_del.
3495  *
3496  * If EA storing a regular striping is being deleted, then release
3497  * all the references to the stripe objects in core.
3498  *
3499  * \see dt_object_operations::do_xattr_del() in the API description for details.
3500  */
3501 static int lod_xattr_del(const struct lu_env *env, struct dt_object *dt,
3502                          const char *name, struct thandle *th)
3503 {
3504         struct dt_object        *next = dt_object_child(dt);
3505         struct lod_object       *lo = lod_dt_obj(dt);
3506         int                     rc;
3507         int                     i;
3508         ENTRY;
3509
3510         if (!strcmp(name, XATTR_NAME_LOV))
3511                 lod_object_free_striping(env, lod_dt_obj(dt));
3512
3513         rc = lod_sub_object_xattr_del(env, next, name, th);
3514         if (rc != 0 || !S_ISDIR(dt->do_lu.lo_header->loh_attr))
3515                 RETURN(rc);
3516
3517         if (lo->ldo_dir_stripenr == 0)
3518                 RETURN(0);
3519
3520         for (i = 0; i < lo->ldo_dir_stripenr; i++) {
3521                 LASSERT(lo->ldo_stripe[i]);
3522
3523                 rc = lod_sub_object_xattr_del(env, lo->ldo_stripe[i], name, th);
3524                 if (rc != 0)
3525                         break;
3526         }
3527
3528         RETURN(rc);
3529 }
3530
3531 /**
3532  * Implementation of dt_object_operations::do_xattr_list.
3533  *
3534  * \see dt_object_operations::do_xattr_list() in the API description
3535  * for details.
3536  */
3537 static int lod_xattr_list(const struct lu_env *env,
3538                           struct dt_object *dt, const struct lu_buf *buf)
3539 {
3540         return dt_xattr_list(env, dt_object_child(dt), buf);
3541 }
3542
3543 static inline int lod_object_will_be_striped(int is_reg, const struct lu_fid *fid)
3544 {
3545         return (is_reg && fid_seq(fid) != FID_SEQ_LOCAL_FILE);
3546 }
3547
3548
3549 /**
3550  * Get default striping.
3551  *
3552  * \param[in] env               execution environment
3553  * \param[in] lo                object
3554  * \param[out] lds              default striping
3555  *
3556  * \retval              0 on success
3557  * \retval              negative if failed
3558  */
3559 static int lod_get_default_lov_striping(const struct lu_env *env,
3560                                         struct lod_object *lo,
3561                                         struct lod_default_striping *lds)
3562 {
3563         struct lod_thread_info *info = lod_env_info(env);
3564         struct lov_user_md_v1 *v1 = NULL;
3565         struct lov_user_md_v3 *v3 = NULL;
3566         struct lov_comp_md_v1 *comp_v1 = NULL;
3567         __u16   comp_cnt;
3568         bool    composite;
3569         int     rc, i;
3570         ENTRY;
3571
3572         lds->lds_def_striping_set = 0;
3573
3574         rc = lod_get_lov_ea(env, lo);
3575         if (rc < 0)
3576                 RETURN(rc);
3577
3578         if (rc < (typeof(rc))sizeof(struct lov_user_md))
3579                 RETURN(0);
3580
3581         v1 = info->lti_ea_store;
3582         if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V1)) {
3583                 lustre_swab_lov_user_md_v1(v1);
3584         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_V3)) {
3585                 v3 = (struct lov_user_md_v3 *)v1;
3586                 lustre_swab_lov_user_md_v3(v3);
3587         } else if (v1->lmm_magic == __swab32(LOV_USER_MAGIC_COMP_V1)) {
3588                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3589                 lustre_swab_lov_comp_md_v1(comp_v1);
3590         }
3591
3592         if (v1->lmm_magic != LOV_MAGIC_V3 && v1->lmm_magic != LOV_MAGIC_V1 &&
3593             v1->lmm_magic != LOV_MAGIC_COMP_V1)
3594                 RETURN(-ENOTSUPP);
3595
3596         if (v1->lmm_magic == LOV_MAGIC_COMP_V1) {
3597                 comp_v1 = (struct lov_comp_md_v1 *)v1;
3598                 comp_cnt = comp_v1->lcm_entry_count;
3599                 if (comp_cnt == 0)
3600                         RETURN(-EINVAL);
3601                 composite = true;
3602         } else {
3603                 comp_cnt = 1;
3604                 composite = false;
3605         }
3606
3607         /* realloc default comp entries if necessary */
3608         rc = lod_def_striping_comp_resize(lds, comp_cnt);
3609         if (rc < 0)
3610                 RETURN(rc);
3611
3612         lds->lds_def_comp_cnt = comp_cnt;
3613         lds->lds_def_striping_is_composite = composite ? 1 : 0;
3614
3615         for (i = 0; i < comp_cnt; i++) {
3616                 struct lod_layout_component *lod_comp;
3617                 struct lu_extent *ext;
3618                 char *pool;
3619
3620                 lod_comp = &lds->lds_def_comp_entries[i];
3621                 /*
3622                  * reset lod_comp values, llc_stripes is always NULL in
3623                  * the default striping template, llc_pool will be reset
3624                  * later below.
3625                  */
3626                 memset(lod_comp, 0, offsetof(typeof(*lod_comp), llc_pool));
3627
3628                 if (composite) {
3629                         v1 = (struct lov_user_md *)((char *)comp_v1 +
3630                                         comp_v1->lcm_entries[i].lcme_offset);
3631                         ext = &comp_v1->lcm_entries[i].lcme_extent;
3632                         lod_comp->llc_extent = *ext;
3633                 }
3634
3635                 if (v1->lmm_pattern != LOV_PATTERN_RAID0 &&
3636                     v1->lmm_pattern != 0) {
3637                         lod_free_def_comp_entries(lds);
3638                         RETURN(-EINVAL);
3639                 }
3640
3641                 CDEBUG(D_LAYOUT, DFID" stripe_count=%d stripe_size=%d "
3642                        "stripe_offset=%d\n",
3643                        PFID(lu_object_fid(&lo->ldo_obj.do_lu)),
3644                        (int)v1->lmm_stripe_count, (int)v1->lmm_stripe_size,
3645                        (int)v1->lmm_stripe_offset);
3646
3647                 lod_comp->llc_stripenr = v1->lmm_stripe_count;
3648                 lod_comp->llc_stripe_size = v1->lmm_stripe_size;
3649                 lod_comp->llc_stripe_offset = v1->lmm_stripe_offset;
3650
3651                 pool = NULL;
3652                 if (v1->lmm_magic == LOV_USER_MAGIC_V3) {
3653                         /* XXX: sanity check here */
3654                         v3 = (struct lov_user_md_v3 *) v1;
3655                         if (v3->lmm_pool_name[0] != '\0')
3656                                 pool = v3->lmm_pool_name;
3657                 }
3658                 lod_set_def_pool(lds, i, pool);
3659         }
3660
3661         lds->lds_def_striping_set = 1;
3662         RETURN(rc);
3663 }
3664
3665 /**
3666  * Get default directory striping.
3667  *
3668  * \param[in] env               execution environment
3669  * \param[in] lo                object
3670  * \param[out] lds              default striping
3671  *
3672  * \retval              0 on success
3673  * \retval              negative if failed
3674  */
3675 static int lod_get_default_lmv_striping(const struct lu_env *env,
3676                                         struct lod_object *lo,
3677                                         struct lod_default_striping *lds)
3678 {
3679         struct lod_thread_info  *info = lod_env_info(env);
3680         struct lmv_user_md_v1   *v1 = NULL;
3681         int                      rc;
3682         ENTRY;
3683
3684         lds->lds_dir_def_striping_set = 0;
3685         rc = lod_get_default_lmv_ea(env, lo);
3686         if (rc < 0)
3687                 RETURN(rc);
3688
3689         if (rc < (typeof(rc))sizeof(struct lmv_user_md))
3690                 RETURN(0);
3691
3692         v1 = info->lti_ea_store;
3693
3694         lds->lds_dir_def_stripenr = le32_to_cpu(v1->lum_stripe_count);
3695         lds->lds_dir_def_stripe_offset = le32_to_cpu(v1->lum_stripe_offset);
3696         lds->lds_dir_def_hash_type = le32_to_cpu(v1->lum_hash_type);
3697         lds->lds_dir_def_striping_set = 1;
3698
3699         RETURN(0);
3700 }
3701
3702 /**
3703  * Get default striping in the object.
3704  *
3705  * Get object default striping and default directory striping.
3706  *
3707  * \param[in] env               execution environment
3708  * \param[in] lo                object
3709  * \param[out] lds              default striping
3710  *
3711  * \retval              0 on success
3712  * \retval              negative if failed
3713  */
3714 static int lod_get_default_striping(const struct lu_env *env,
3715                                     struct lod_object *lo,
3716                                     struct lod_default_striping *lds)
3717 {
3718         int rc, rc1;
3719
3720         rc = lod_get_default_lov_striping(env, lo, lds);
3721         rc1 = lod_get_default_lmv_striping(env, lo, lds);
3722         if (rc == 0 && rc1 < 0)
3723                 rc = rc1;
3724
3725         return rc;
3726 }
3727
3728 /**
3729  * Apply default striping on object.
3730  *
3731  * If object striping pattern is not set, set to the one in default striping.
3732  * The default striping is from parent or fs.
3733  *
3734  * \param[in] lo                new object
3735  * \param[in] lds               default striping
3736  * \param[in] mode              new object's mode
3737  */
3738 static void lod_striping_from_default(struct lod_object *lo,
3739                                       const struct lod_default_striping *lds,
3740                                       umode_t mode)
3741 {
3742         struct lod_device *d = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
3743         struct lov_desc *desc = &d->lod_desc;
3744         int i, rc;
3745
3746         if (lds->lds_def_striping_set && S_ISREG(mode)) {
3747                 rc = lod_alloc_comp_entries(lo, lds->lds_def_comp_cnt);
3748                 if (rc != 0)
3749                         return;
3750
3751                 lo->ldo_is_composite = lds->lds_def_striping_is_composite;
3752
3753                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
3754                         struct lod_layout_component *obj_comp =
3755                                                 &lo->ldo_comp_entries[i];
3756                         struct lod_layout_component *def_comp =
3757                                                 &lds->lds_def_comp_entries[i];
3758
3759                         CDEBUG(D_LAYOUT, "Inherite from default: size:%hu "
3760                                "nr:%u offset:%u %s\n",
3761                                def_comp->llc_stripe_size,
3762                                def_comp->llc_stripenr,
3763                                def_comp->llc_stripe_offset,
3764                                def_comp->llc_pool ?: "");
3765
3766                         *obj_comp = *def_comp;
3767                         if (def_comp->llc_pool != NULL) {
3768                                 /* pointer was copied from def_comp */
3769                                 obj_comp->llc_pool = NULL;
3770                                 lod_obj_set_pool(lo, i, def_comp->llc_pool);
3771                         }
3772
3773                         /*
3774                          * Don't initialize these fields for plain layout
3775                          * (v1/v3) here, they are inherited in the order of
3776                          * 'parent' -> 'fs default (root)' -> 'global default
3777                          * values for stripe_count & stripe_size'.
3778                          *
3779                          * see lod_ah_init().
3780                          */
3781                         if (!lo->ldo_is_composite)
3782                                 continue;
3783
3784                         if (obj_comp->llc_stripenr <= 0)
3785                                 obj_comp->llc_stripenr =
3786                                         desc->ld_default_stripe_count;
3787                         if (obj_comp->llc_stripe_size <= 0)
3788                                 obj_comp->llc_stripe_size =
3789                                         desc->ld_default_stripe_size;
3790                 }
3791         } else if (lds->lds_dir_def_striping_set && S_ISDIR(mode)) {
3792                 if (lo->ldo_dir_stripenr == 0)
3793                         lo->ldo_dir_stripenr = lds->lds_dir_def_stripenr;
3794                 if (lo->ldo_dir_stripe_offset == -1)
3795                         lo->ldo_dir_stripe_offset =
3796                                 lds->lds_dir_def_stripe_offset;
3797                 if (lo->ldo_dir_hash_type == 0)
3798                         lo->ldo_dir_hash_type = lds->lds_dir_def_hash_type;
3799
3800                 CDEBUG(D_LAYOUT, "striping from default dir: nr:%hu, "
3801                        "offset:%u, hash_type:%u\n",
3802                        lo->ldo_dir_stripenr, lo->ldo_dir_stripe_offset,
3803                        lo->ldo_dir_hash_type);
3804         }
3805 }
3806
3807 static inline bool lod_need_inherit_more(struct lod_object *lo, bool from_root)
3808 {
3809         struct lod_layout_component *lod_comp;
3810
3811         if (lo->ldo_comp_cnt == 0)
3812                 return true;
3813
3814         if (lo->ldo_is_composite)
3815                 return false;
3816
3817         lod_comp = &lo->ldo_comp_entries[0];
3818
3819         if (lod_comp->llc_stripenr <= 0 ||
3820             lod_comp->llc_stripe_size <= 0)
3821                 return true;
3822
3823         if (from_root && (lod_comp->llc_pool == NULL ||
3824                           lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT))
3825                 return true;
3826
3827         return false;
3828 }
3829
3830 /**
3831  * Implementation of dt_object_operations::do_ah_init.
3832  *
3833  * This method is used to make a decision on the striping configuration for the
3834  * object being created. It can be taken from the \a parent object if it exists,
3835  * or filesystem's default. The resulting configuration (number of stripes,
3836  * stripe size/offset, pool name, etc) is stored in the object itself and will
3837  * be used by the methods like ->doo_declare_create().
3838  *
3839  * \see dt_object_operations::do_ah_init() in the API description for details.
3840  */
3841 static void lod_ah_init(const struct lu_env *env,
3842                         struct dt_allocation_hint *ah,
3843                         struct dt_object *parent,
3844                         struct dt_object *child,
3845                         umode_t child_mode)
3846 {
3847         struct lod_device *d = lu2lod_dev(child->do_lu.lo_dev);
3848         struct lod_thread_info *info = lod_env_info(env);
3849         struct lod_default_striping *lds = &info->lti_def_striping;
3850         struct dt_object *nextp = NULL;
3851         struct dt_object *nextc;
3852         struct lod_object *lp = NULL;
3853         struct lod_object *lc;
3854         struct lov_desc *desc;
3855         struct lod_layout_component *lod_comp;
3856         int rc;
3857         ENTRY;
3858
3859         LASSERT(child);
3860
3861         if (likely(parent)) {
3862                 nextp = dt_object_child(parent);
3863                 lp = lod_dt_obj(parent);
3864         }
3865
3866         nextc = dt_object_child(child);
3867         lc = lod_dt_obj(child);
3868
3869         LASSERT(!lod_obj_is_striped(child));
3870         /* default layout template may have been set on the regular file
3871          * when this is called from mdd_create_data() */
3872         if (S_ISREG(child_mode))
3873                 lod_free_comp_entries(lc);
3874
3875         if (!dt_object_exists(nextc))
3876                 nextc->do_ops->do_ah_init(env, ah, nextp, nextc, child_mode);
3877
3878         if (S_ISDIR(child_mode)) {
3879                 /* other default values are 0 */
3880                 lc->ldo_dir_stripe_offset = -1;
3881
3882                 /* get default striping from parent object */
3883                 if (likely(lp != NULL))
3884                         lod_get_default_striping(env, lp, lds);
3885
3886                 /* set child default striping info, default value is NULL */
3887                 if (lds->lds_def_striping_set || lds->lds_dir_def_striping_set)
3888                         lc->ldo_def_striping = lds;
3889
3890                 /* It should always honour the specified stripes */
3891                 if (ah->dah_eadata != NULL && ah->dah_eadata_len != 0 &&
3892                     lod_verify_md_striping(d, ah->dah_eadata) == 0) {
3893                         const struct lmv_user_md_v1 *lum1 = ah->dah_eadata;
3894
3895                         lc->ldo_dir_stripenr =
3896                                 le32_to_cpu(lum1->lum_stripe_count);
3897                         lc->ldo_dir_stripe_offset =
3898                                 le32_to_cpu(lum1->lum_stripe_offset);
3899                         lc->ldo_dir_hash_type =
3900                                 le32_to_cpu(lum1->lum_hash_type);
3901                         CDEBUG(D_INFO, "set dir stripe: count %hu, offset %d, "
3902                                 "hash_type %u\n",
3903                                 lc->ldo_dir_stripenr,
3904                                 (int)lc->ldo_dir_stripe_offset,
3905                                 lc->ldo_dir_hash_type);
3906                 } else {
3907                         /* transfer defaults LMV to new directory */
3908                         lod_striping_from_default(lc, lds, child_mode);
3909                 }
3910
3911                 /* shrink the stripe_count to the avaible MDT count */
3912                 if (lc->ldo_dir_stripenr > d->lod_remote_mdt_count + 1 &&
3913                     !OBD_FAIL_CHECK(OBD_FAIL_LARGE_STRIPE))
3914                         lc->ldo_dir_stripenr = d->lod_remote_mdt_count + 1;
3915
3916                 /* Directory will be striped only if stripe_count > 1, if
3917                  * stripe_count == 1, let's reset stripenr = 0 to avoid
3918                  * create single master stripe and also help to unify the
3919                  * stripe handling of directories and files */
3920                 if (lc->ldo_dir_stripenr == 1)
3921                         lc->ldo_dir_stripenr = 0;
3922
3923                 CDEBUG(D_INFO, "final dir stripe [%hu %d %u]\n",
3924                        lc->ldo_dir_stripenr, (int)lc->ldo_dir_stripe_offset,
3925                        lc->ldo_dir_hash_type);
3926
3927                 RETURN_EXIT;
3928         }
3929
3930         /* child object regular file*/
3931
3932         if (!lod_object_will_be_striped(S_ISREG(child_mode),
3933                                         lu_object_fid(&child->do_lu)))
3934                 RETURN_EXIT;
3935
3936         /* If object is going to be striped over OSTs, transfer default
3937          * striping information to the child, so that we can use it
3938          * during declaration and creation.
3939          *
3940          * Try from the parent first.
3941          */
3942         if (likely(lp != NULL)) {
3943                 rc = lod_get_default_lov_striping(env, lp, lds);
3944                 if (rc == 0)
3945                         lod_striping_from_default(lc, lds, child_mode);
3946         }
3947
3948         /* Initialize lod_device::lod_md_root object reference */
3949         if (d->lod_md_root == NULL) {
3950                 struct dt_object *root;
3951                 struct lod_object *lroot;
3952
3953                 lu_root_fid(&info->lti_fid);
3954                 root = dt_locate(env, &d->lod_dt_dev, &info->lti_fid);
3955                 if (!IS_ERR(root)) {
3956                         lroot = lod_dt_obj(root);
3957
3958                         spin_lock(&d->lod_lock);
3959                         if (d->lod_md_root != NULL)
3960                                 dt_object_put(env, &d->lod_md_root->ldo_obj);
3961                         d->lod_md_root = lroot;
3962                         spin_unlock(&d->lod_lock);
3963                 }
3964         }
3965
3966         /* try inherit layout from the root object (fs default) when:
3967          *  - parent does not have default layout; or
3968          *  - parent has plain(v1/v3) default layout, and some attributes
3969          *    are not specified in the default layout;
3970          */
3971         if (d->lod_md_root != NULL && lod_need_inherit_more(lc, true)) {
3972                 rc = lod_get_default_lov_striping(env, d->lod_md_root, lds);
3973                 if (rc)
3974                         goto out;
3975                 if (lc->ldo_comp_cnt == 0) {
3976                         lod_striping_from_default(lc, lds, child_mode);
3977                 } else if (!lds->lds_def_striping_is_composite) {
3978                         struct lod_layout_component *def_comp;
3979
3980                         LASSERT(!lc->ldo_is_composite);
3981                         lod_comp = &lc->ldo_comp_entries[0];
3982                         def_comp = &lds->lds_def_comp_entries[0];
3983
3984                         if (lod_comp->llc_stripenr <= 0)
3985                                 lod_comp->llc_stripenr = def_comp->llc_stripenr;
3986                         if (lod_comp->llc_stripe_size <= 0)
3987                                 lod_comp->llc_stripe_size =
3988                                         def_comp->llc_stripe_size;
3989                         if (lod_comp->llc_stripe_offset == LOV_OFFSET_DEFAULT)
3990                                 lod_comp->llc_stripe_offset =
3991                                         def_comp->llc_stripe_offset;
3992                         if (lod_comp->llc_pool == NULL)
3993                                 lod_obj_set_pool(lc, 0, def_comp->llc_pool);
3994                 }
3995         }
3996 out:
3997         /*
3998          * fs default striping may not be explicitly set, or historically set
3999          * in config log, use them.
4000          */
4001         if (lod_need_inherit_more(lc, false)) {
4002
4003                 if (lc->ldo_comp_cnt == 0) {
4004                         rc = lod_alloc_comp_entries(lc, 1);
4005                         if (rc)
4006                                 /* fail to allocate memory, will create a
4007                                  * non-striped file. */
4008                                 RETURN_EXIT;
4009                         lc->ldo_is_composite = 0;
4010                         lod_comp = &lc->ldo_comp_entries[0];
4011                         lod_comp->llc_stripe_offset = LOV_OFFSET_DEFAULT;
4012                 }
4013                 LASSERT(!lc->ldo_is_composite);
4014                 lod_comp = &lc->ldo_comp_entries[0];
4015                 desc = &d->lod_desc;
4016                 if (lod_comp->llc_stripenr <= 0)
4017                         lod_comp->llc_stripenr = desc->ld_default_stripe_count;
4018                 if (lod_comp->llc_stripe_size <= 0)
4019                         lod_comp->llc_stripe_size =
4020                                 desc->ld_default_stripe_size;
4021         }
4022
4023         EXIT;
4024 }
4025
4026 #define ll_do_div64(aaa,bbb)    do_div((aaa), (bbb))
4027 /**
4028  * Size initialization on late striping.
4029  *
4030  * Propagate the size of a truncated object to a deferred striping.
4031  * This function handles a special case when truncate was done on a
4032  * non-striped object and now while the striping is being created
4033  * we can't lose that size, so we have to propagate it to the stripes
4034  * being created.
4035  *
4036  * \param[in] env       execution environment
4037  * \param[in] dt        object
4038  * \param[in] th        transaction handle
4039  *
4040  * \retval              0 on success
4041  * \retval              negative if failed
4042  */
4043 static int lod_declare_init_size(const struct lu_env *env,
4044                                  struct dt_object *dt, struct thandle *th)
4045 {
4046         struct dt_object        *next = dt_object_child(dt);
4047         struct lod_object       *lo = lod_dt_obj(dt);
4048         struct dt_object        **objects = NULL;
4049         struct lu_attr  *attr = &lod_env_info(env)->lti_attr;
4050         uint64_t        size, offs;
4051         int     i, rc, stripe, stripenr = 0, stripe_size = 0;
4052         ENTRY;
4053
4054         if (!lod_obj_is_striped(dt))
4055                 RETURN(0);
4056
4057         rc = dt_attr_get(env, next, attr);
4058         LASSERT(attr->la_valid & LA_SIZE);
4059         if (rc)
4060                 RETURN(rc);
4061
4062         size = attr->la_size;
4063         if (size == 0)
4064                 RETURN(0);
4065
4066         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4067                 struct lod_layout_component *lod_comp;
4068                 struct lu_extent *extent;
4069
4070                 lod_comp = &lo->ldo_comp_entries[i];
4071
4072                 if (lod_comp->llc_stripe == NULL)
4073                         continue;
4074
4075                 extent = &lod_comp->llc_extent;
4076                 CDEBUG(D_INFO, "%lld [%lld, %lld)\n",
4077                        size, extent->e_start, extent->e_end);
4078                 if (!lo->ldo_is_composite ||
4079                     (size >= extent->e_start && size < extent->e_end)) {
4080                         objects = lod_comp->llc_stripe;
4081                         stripenr = lod_comp->llc_stripenr;
4082                         stripe_size = lod_comp->llc_stripe_size;
4083                         break;
4084                 }
4085         }
4086
4087         if (stripenr == 0)
4088                 RETURN(0);
4089
4090         LASSERT(objects != NULL && stripe_size != 0);
4091
4092         /* ll_do_div64(a, b) returns a % b, and a = a / b */
4093         ll_do_div64(size, (__u64)stripe_size);
4094         stripe = ll_do_div64(size, (__u64)stripenr);
4095         LASSERT(objects[stripe] != NULL);
4096
4097         size = size * stripe_size;
4098         offs = attr->la_size;
4099         size += ll_do_div64(offs, stripe_size);
4100
4101         attr->la_valid = LA_SIZE;
4102         attr->la_size = size;
4103
4104         rc = lod_sub_object_declare_attr_set(env, objects[stripe], attr, th);
4105
4106         RETURN(rc);
4107 }
4108
4109 /**
4110  * Declare creation of striped object.
4111  *
4112  * The function declares creation stripes for a regular object. The function
4113  * also declares whether the stripes will be created with non-zero size if
4114  * previously size was set non-zero on the master object. If object \a dt is
4115  * not local, then only fully defined striping can be applied in \a lovea.
4116  * Otherwise \a lovea can be in the form of pattern, see lod_qos_parse_config()
4117  * for the details.
4118  *
4119  * \param[in] env       execution environment
4120  * \param[in] dt        object
4121  * \param[in] attr      attributes the stripes will be created with
4122  * \param[in] lovea     a buffer containing striping description
4123  * \param[in] th        transaction handle
4124  *
4125  * \retval              0 on success
4126  * \retval              negative if failed
4127  */
4128 int lod_declare_striped_object(const struct lu_env *env, struct dt_object *dt,
4129                                struct lu_attr *attr,
4130                                const struct lu_buf *lovea, struct thandle *th)
4131 {
4132         struct lod_thread_info  *info = lod_env_info(env);
4133         struct dt_object        *next = dt_object_child(dt);
4134         struct lod_object       *lo = lod_dt_obj(dt);
4135         int                      rc;
4136         ENTRY;
4137
4138         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_ALLOC_OBDO))
4139                 GOTO(out, rc = -ENOMEM);
4140
4141         if (!dt_object_remote(next)) {
4142                 /* choose OST and generate appropriate objects */
4143                 rc = lod_prepare_create(env, lo, attr, lovea, th);
4144                 if (rc)
4145                         GOTO(out, rc);
4146
4147                 /*
4148                  * declare storage for striping data
4149                  */
4150                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4151         } else {
4152                 /* LOD can not choose OST objects for remote objects, i.e.
4153                  * stripes must be ready before that. Right now, it can only
4154                  * happen during migrate, i.e. migrate process needs to create
4155                  * remote regular file (mdd_migrate_create), then the migrate
4156                  * process will provide stripeEA. */
4157                 LASSERT(lovea != NULL);
4158                 info->lti_buf = *lovea;
4159         }
4160
4161         rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4162                                               XATTR_NAME_LOV, 0, th);
4163         if (rc)
4164                 GOTO(out, rc);
4165
4166         /*
4167          * if striping is created with local object's size > 0,
4168          * we have to propagate this size to specific object
4169          * the case is possible only when local object was created previously
4170          */
4171         if (dt_object_exists(next))
4172                 rc = lod_declare_init_size(env, dt, th);
4173
4174 out:
4175         /* failed to create striping or to set initial size, let's reset
4176          * config so that others don't get confused */
4177         if (rc)
4178                 lod_object_free_striping(env, lo);
4179
4180         RETURN(rc);
4181 }
4182
4183 /**
4184  * Implementation of dt_object_operations::do_declare_create.
4185  *
4186  * The method declares creation of a new object. If the object will be striped,
4187  * then helper functions are called to find FIDs for the stripes, declare
4188  * creation of the stripes and declare initialization of the striping
4189  * information to be stored in the master object.
4190  *
4191  * \see dt_object_operations::do_declare_create() in the API description
4192  * for details.
4193  */
4194 static int lod_declare_object_create(const struct lu_env *env,
4195                                      struct dt_object *dt,
4196                                      struct lu_attr *attr,
4197                                      struct dt_allocation_hint *hint,
4198                                      struct dt_object_format *dof,
4199                                      struct thandle *th)
4200 {
4201         struct dt_object   *next = dt_object_child(dt);
4202         struct lod_object  *lo = lod_dt_obj(dt);
4203         int                 rc;
4204         ENTRY;
4205
4206         LASSERT(dof);
4207         LASSERT(attr);
4208         LASSERT(th);
4209
4210         /*
4211          * first of all, we declare creation of local object
4212          */
4213         rc = lod_sub_object_declare_create(env, next, attr, hint, dof, th);
4214         if (rc != 0)
4215                 GOTO(out, rc);
4216
4217         if (dof->dof_type == DFT_SYM)
4218                 dt->do_body_ops = &lod_body_lnk_ops;
4219         else if (dof->dof_type == DFT_REGULAR)
4220                 dt->do_body_ops = &lod_body_ops;
4221
4222         /*
4223          * it's lod_ah_init() that has decided the object will be striped
4224          */
4225         if (dof->dof_type == DFT_REGULAR) {
4226                 /* callers don't want stripes */
4227                 /* XXX: all tricky interactions with ->ah_make_hint() decided
4228                  * to use striping, then ->declare_create() behaving differently
4229                  * should be cleaned */
4230                 if (dof->u.dof_reg.striped != 0)
4231                         rc = lod_declare_striped_object(env, dt, attr,
4232                                                         NULL, th);
4233         } else if (dof->dof_type == DFT_DIR) {
4234                 struct seq_server_site *ss;
4235
4236                 ss = lu_site2seq(dt->do_lu.lo_dev->ld_site);
4237
4238                 /* If the parent has default stripeEA, and client
4239                  * did not find it before sending create request,
4240                  * then MDT will return -EREMOTE, and client will
4241                  * retrieve the default stripeEA and re-create the
4242                  * sub directory.
4243                  *
4244                  * Note: if dah_eadata != NULL, it means creating the
4245                  * striped directory with specified stripeEA, then it
4246                  * should ignore the default stripeEA */
4247                 if (hint != NULL && hint->dah_eadata == NULL) {
4248                         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_STALE_DIR_LAYOUT))
4249                                 GOTO(out, rc = -EREMOTE);
4250
4251                         if (lo->ldo_dir_stripe_offset == -1) {
4252                                 /* child and parent should be in the same MDT */
4253                                 if (hint->dah_parent != NULL &&
4254                                     dt_object_remote(hint->dah_parent))
4255                                         GOTO(out, rc = -EREMOTE);
4256                         } else if (lo->ldo_dir_stripe_offset !=
4257                                    ss->ss_node_id) {
4258                                 struct lod_device *lod;
4259                                 struct lod_tgt_descs *ltd;
4260                                 struct lod_tgt_desc *tgt = NULL;
4261                                 bool found_mdt = false;
4262                                 int i;
4263
4264                                 lod = lu2lod_dev(lo->ldo_obj.do_lu.lo_dev);
4265                                 ltd = &lod->lod_mdt_descs;
4266                                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
4267                                         tgt = LTD_TGT(ltd, i);
4268                                         if (tgt->ltd_index ==
4269                                                 lo->ldo_dir_stripe_offset) {
4270                                                 found_mdt = true;
4271                                                 break;
4272                                         }
4273                                 }
4274
4275                                 /* If the MDT indicated by stripe_offset can be
4276                                  * found, then tell client to resend the create
4277                                  * request to the correct MDT, otherwise return
4278                                  * error to client */
4279                                 if (found_mdt)
4280                                         GOTO(out, rc = -EREMOTE);
4281                                 else
4282                                         GOTO(out, rc = -EINVAL);
4283                         }
4284                 }
4285
4286                 rc = lod_declare_dir_striping_create(env, dt, attr, dof, th);
4287         }
4288 out:
4289         /* failed to create striping or to set initial size, let's reset
4290          * config so that others don't get confused */
4291         if (rc)
4292                 lod_object_free_striping(env, lo);
4293         RETURN(rc);
4294 }
4295
4296 /**
4297  * Creation of a striped regular object.
4298  *
4299  * The function is called to create the stripe objects for a regular
4300  * striped file. This can happen at the initial object creation or
4301  * when the caller asks LOD to do so using ->do_xattr_set() method
4302  * (so called late striping). Notice all the information are already
4303  * prepared in the form of the list of objects (ldo_stripe field).
4304  * This is done during declare phase.
4305  *
4306  * \param[in] env       execution environment
4307  * \param[in] dt        object
4308  * \param[in] attr      attributes the stripes will be created with
4309  * \param[in] dof       format of stripes (see OSD API description)
4310  * \param[in] th        transaction handle
4311  *
4312  * \retval              0 on success
4313  * \retval              negative if failed
4314  */
4315 int lod_striping_create(const struct lu_env *env, struct dt_object *dt,
4316                         struct lu_attr *attr, struct dt_object_format *dof,
4317                         struct thandle *th)
4318 {
4319         struct lod_layout_component     *lod_comp;
4320         struct lod_object       *lo = lod_dt_obj(dt);
4321         int     rc = 0, i, j;
4322         ENTRY;
4323
4324         LASSERT(lo->ldo_comp_cnt != 0 && lo->ldo_comp_entries != NULL);
4325
4326         /* create all underlying objects */
4327         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4328                 lod_comp = &lo->ldo_comp_entries[i];
4329
4330                 if (lod_comp_inited(lod_comp))
4331                         continue;
4332
4333                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4334                         lod_comp_set_init(lod_comp);
4335
4336                 if (lod_comp->llc_stripe == NULL)
4337                         continue;
4338
4339                 LASSERT(lod_comp->llc_stripenr);
4340                 for (j = 0; j < lod_comp->llc_stripenr; j++) {
4341                         struct dt_object *object = lod_comp->llc_stripe[j];
4342                         LASSERT(object != NULL);
4343                         rc = lod_sub_object_create(env, object, attr, NULL,
4344                                                    dof, th);
4345                         if (rc)
4346                                 break;
4347                 }
4348                 lod_comp_set_init(lod_comp);
4349         }
4350
4351         if (rc == 0)
4352                 rc = lod_generate_and_set_lovea(env, lo, th);
4353
4354         if (rc == 0)
4355                 lo->ldo_comp_cached = 1;
4356         else
4357                 lod_object_free_striping(env, lo);
4358
4359         RETURN(rc);
4360 }
4361
4362 /**
4363  * Implementation of dt_object_operations::do_create.
4364  *
4365  * If any of preceeding methods (like ->do_declare_create(),
4366  * ->do_ah_init(), etc) chose to create a striped object,
4367  * then this method will create the master and the stripes.
4368  *
4369  * \see dt_object_operations::do_create() in the API description for details.
4370  */
4371 static int lod_object_create(const struct lu_env *env, struct dt_object *dt,
4372                              struct lu_attr *attr,
4373                              struct dt_allocation_hint *hint,
4374                              struct dt_object_format *dof, struct thandle *th)
4375 {
4376         int                 rc;
4377         ENTRY;
4378
4379         /* create local object */
4380         rc = lod_sub_object_create(env, dt_object_child(dt), attr, hint, dof,
4381                                    th);
4382         if (rc != 0)
4383                 RETURN(rc);
4384
4385         if (S_ISREG(dt->do_lu.lo_header->loh_attr) &&
4386             lod_obj_is_striped(dt) && dof->u.dof_reg.striped != 0) {
4387                 LASSERT(lod_dt_obj(dt)->ldo_comp_cached == 0);
4388                 rc = lod_striping_create(env, dt, attr, dof, th);
4389         }
4390
4391         RETURN(rc);
4392 }
4393
4394 static inline int
4395 lod_obj_stripe_destroy_cb(const struct lu_env *env, struct lod_object *lo,
4396                           struct dt_object *dt, struct thandle *th,
4397                           int stripe_idx, struct lod_obj_stripe_cb_data *data)
4398 {
4399         if (data->locd_declare)
4400                 return lod_sub_object_declare_destroy(env, dt, th);
4401         else if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4402                  stripe_idx == cfs_fail_val)
4403                 return lod_sub_object_destroy(env, dt, th);
4404         else
4405                 return 0;
4406 }
4407
4408 /**
4409  * Implementation of dt_object_operations::do_declare_destroy.
4410  *
4411  * If the object is a striped directory, then the function declares reference
4412  * removal from the master object (this is an index) to the stripes and declares
4413  * destroy of all the stripes. In all the cases, it declares an intention to
4414  * destroy the object itself.
4415  *
4416  * \see dt_object_operations::do_declare_destroy() in the API description
4417  * for details.
4418  */
4419 static int lod_declare_object_destroy(const struct lu_env *env,
4420                                       struct dt_object *dt,
4421                                       struct thandle *th)
4422 {
4423         struct dt_object   *next = dt_object_child(dt);
4424         struct lod_object  *lo = lod_dt_obj(dt);
4425         struct lod_thread_info *info = lod_env_info(env);
4426         char               *stripe_name = info->lti_key;
4427         int                 rc, i;
4428         ENTRY;
4429
4430         /*
4431          * load striping information, notice we don't do this when object
4432          * is being initialized as we don't need this information till
4433          * few specific cases like destroy, chown
4434          */
4435         rc = lod_load_striping(env, lo);
4436         if (rc)
4437                 RETURN(rc);
4438
4439         /* declare destroy for all underlying objects */
4440         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4441                 rc = next->do_ops->do_index_try(env, next,
4442                                                 &dt_directory_features);
4443                 if (rc != 0)
4444                         RETURN(rc);
4445
4446                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4447                         rc = lod_sub_object_declare_ref_del(env, next, th);
4448                         if (rc != 0)
4449                                 RETURN(rc);
4450
4451                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4452                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4453                                 i);
4454                         rc = lod_sub_object_declare_delete(env, next,
4455                                         (const struct dt_key *)stripe_name, th);
4456                         if (rc != 0)
4457                                 RETURN(rc);
4458                 }
4459         }
4460
4461         /*
4462          * we declare destroy for the local object
4463          */
4464         rc = lod_sub_object_declare_destroy(env, next, th);
4465         if (rc)
4466                 RETURN(rc);
4467
4468         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4469             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4470                 RETURN(0);
4471
4472         if (!lod_obj_is_striped(dt))
4473                 RETURN(0);
4474
4475         /* declare destroy all striped objects */
4476         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4477                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4478                         if (lo->ldo_stripe[i] == NULL)
4479                                 continue;
4480
4481                         rc = lod_sub_object_declare_ref_del(env,
4482                                         lo->ldo_stripe[i], th);
4483
4484                         rc = lod_sub_object_declare_destroy(env,
4485                                         lo->ldo_stripe[i], th);
4486                         if (rc != 0)
4487                                 break;
4488                 }
4489         } else {
4490                 struct lod_obj_stripe_cb_data data;
4491
4492                 data.locd_declare = true;
4493                 rc = lod_obj_for_each_stripe(env, lo, th,
4494                                 lod_obj_stripe_destroy_cb, &data);
4495         }
4496
4497         RETURN(rc);
4498 }
4499
4500 /**
4501  * Implementation of dt_object_operations::do_destroy.
4502  *
4503  * If the object is a striped directory, then the function removes references
4504  * from the master object (this is an index) to the stripes and destroys all
4505  * the stripes. In all the cases, the function destroys the object itself.
4506  *
4507  * \see dt_object_operations::do_destroy() in the API description for details.
4508  */
4509 static int lod_object_destroy(const struct lu_env *env,
4510                 struct dt_object *dt, struct thandle *th)
4511 {
4512         struct dt_object  *next = dt_object_child(dt);
4513         struct lod_object *lo = lod_dt_obj(dt);
4514         struct lod_thread_info *info = lod_env_info(env);
4515         char               *stripe_name = info->lti_key;
4516         unsigned int       i;
4517         int                rc;
4518         ENTRY;
4519
4520         /* destroy sub-stripe of master object */
4521         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4522                 rc = next->do_ops->do_index_try(env, next,
4523                                                 &dt_directory_features);
4524                 if (rc != 0)
4525                         RETURN(rc);
4526
4527                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4528                         rc = lod_sub_object_ref_del(env, next, th);
4529                         if (rc != 0)
4530                                 RETURN(rc);
4531
4532                         snprintf(stripe_name, sizeof(info->lti_key), DFID":%d",
4533                                 PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)),
4534                                 i);
4535
4536                         CDEBUG(D_INFO, DFID" delete stripe %s "DFID"\n",
4537                                PFID(lu_object_fid(&dt->do_lu)), stripe_name,
4538                                PFID(lu_object_fid(&lo->ldo_stripe[i]->do_lu)));
4539
4540                         rc = lod_sub_object_delete(env, next,
4541                                        (const struct dt_key *)stripe_name, th);
4542                         if (rc != 0)
4543                                 RETURN(rc);
4544                 }
4545         }
4546
4547         rc = lod_sub_object_destroy(env, next, th);
4548         if (rc != 0)
4549                 RETURN(rc);
4550
4551         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ) ||
4552             OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_MDTOBJ2))
4553                 RETURN(0);
4554
4555         if (!lod_obj_is_striped(dt))
4556                 RETURN(0);
4557
4558         /* destroy all striped objects */
4559         if (S_ISDIR(dt->do_lu.lo_header->loh_attr)) {
4560                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
4561                         if (lo->ldo_stripe[i] == NULL)
4562                                 continue;
4563                         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_LOST_SPEOBJ) ||
4564                             i == cfs_fail_val) {
4565                                 dt_write_lock(env, lo->ldo_stripe[i],
4566                                               MOR_TGT_CHILD);
4567                                 rc = lod_sub_object_ref_del(env,
4568                                                 lo->ldo_stripe[i], th);
4569                                 dt_write_unlock(env, lo->ldo_stripe[i]);
4570                                 if (rc != 0)
4571                                         break;
4572
4573                                 rc = lod_sub_object_destroy(env,
4574                                                 lo->ldo_stripe[i], th);
4575                                 if (rc != 0)
4576                                         break;
4577                         }
4578                 }
4579         } else {
4580                 struct lod_obj_stripe_cb_data data;
4581
4582                 data.locd_declare = false;
4583                 rc = lod_obj_for_each_stripe(env, lo, th,
4584                                 lod_obj_stripe_destroy_cb, &data);
4585         }
4586
4587         RETURN(rc);
4588 }
4589
4590 /**
4591  * Implementation of dt_object_operations::do_declare_ref_add.
4592  *
4593  * \see dt_object_operations::do_declare_ref_add() in the API description
4594  * for details.
4595  */
4596 static int lod_declare_ref_add(const struct lu_env *env,
4597                                struct dt_object *dt, struct thandle *th)
4598 {
4599         return lod_sub_object_declare_ref_add(env, dt_object_child(dt), th);
4600 }
4601
4602 /**
4603  * Implementation of dt_object_operations::do_ref_add.
4604  *
4605  * \see dt_object_operations::do_ref_add() in the API description for details.
4606  */
4607 static int lod_ref_add(const struct lu_env *env,
4608                        struct dt_object *dt, struct thandle *th)
4609 {
4610         return lod_sub_object_ref_add(env, dt_object_child(dt), th);
4611 }
4612
4613 /**
4614  * Implementation of dt_object_operations::do_declare_ref_del.
4615  *
4616  * \see dt_object_operations::do_declare_ref_del() in the API description
4617  * for details.
4618  */
4619 static int lod_declare_ref_del(const struct lu_env *env,
4620                                struct dt_object *dt, struct thandle *th)
4621 {
4622         return lod_sub_object_declare_ref_del(env, dt_object_child(dt), th);
4623 }
4624
4625 /**
4626  * Implementation of dt_object_operations::do_ref_del
4627  *
4628  * \see dt_object_operations::do_ref_del() in the API description for details.
4629  */
4630 static int lod_ref_del(const struct lu_env *env,
4631                        struct dt_object *dt, struct thandle *th)
4632 {
4633         return lod_sub_object_ref_del(env, dt_object_child(dt), th);
4634 }
4635
4636 /**
4637  * Implementation of dt_object_operations::do_object_sync.
4638  *
4639  * \see dt_object_operations::do_object_sync() in the API description
4640  * for details.
4641  */
4642 static int lod_object_sync(const struct lu_env *env, struct dt_object *dt,
4643                            __u64 start, __u64 end)
4644 {
4645         return dt_object_sync(env, dt_object_child(dt), start, end);
4646 }
4647
4648 /**
4649  * Release LDLM locks on the stripes of a striped directory.
4650  *
4651  * Iterates over all the locks taken on the stripe objects and
4652  * cancel them.
4653  *
4654  * \param[in] env       execution environment
4655  * \param[in] dt        striped object
4656  * \param[in] einfo     lock description
4657  * \param[in] policy    data describing requested lock
4658  *
4659  * \retval              0 on success
4660  * \retval              negative if failed
4661  */
4662 static int lod_object_unlock_internal(const struct lu_env *env,
4663                                       struct dt_object *dt,
4664                                       struct ldlm_enqueue_info *einfo,
4665                                       union ldlm_policy_data *policy)
4666 {
4667         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4668         int                     rc = 0;
4669         int                     i;
4670         ENTRY;
4671
4672         if (slave_locks == NULL)
4673                 RETURN(0);
4674
4675         for (i = 1; i < slave_locks->count; i++) {
4676                 if (lustre_handle_is_used(&slave_locks->handles[i]))
4677                         ldlm_lock_decref_and_cancel(&slave_locks->handles[i],
4678                                                     einfo->ei_mode);
4679         }
4680
4681         RETURN(rc);
4682 }
4683
4684 /**
4685  * Implementation of dt_object_operations::do_object_unlock.
4686  *
4687  * Used to release LDLM lock(s).
4688  *
4689  * \see dt_object_operations::do_object_unlock() in the API description
4690  * for details.
4691  */
4692 static int lod_object_unlock(const struct lu_env *env, struct dt_object *dt,
4693                              struct ldlm_enqueue_info *einfo,
4694                              union ldlm_policy_data *policy)
4695 {
4696         struct lod_object *lo = lod_dt_obj(dt);
4697         struct lustre_handle_array *slave_locks = einfo->ei_cbdata;
4698         int slave_locks_size;
4699         int i;
4700         ENTRY;
4701
4702         if (slave_locks == NULL)
4703                 RETURN(0);
4704
4705         LASSERT(S_ISDIR(dt->do_lu.lo_header->loh_attr));
4706         LASSERT(lo->ldo_dir_stripenr > 1);
4707         /* Note: for remote lock for single stripe dir, MDT will cancel
4708          * the lock by lockh directly */
4709         LASSERT(!dt_object_remote(dt_object_child(dt)));
4710
4711         /* locks were unlocked in MDT layer */
4712         for (i = 1; i < slave_locks->count; i++) {
4713                 LASSERT(!lustre_handle_is_used(&slave_locks->handles[i]));
4714                 dt_invalidate(env, lo->ldo_stripe[i]);
4715         }
4716
4717         slave_locks_size = sizeof(*slave_locks) + slave_locks->count *
4718                            sizeof(slave_locks->handles[0]);
4719         OBD_FREE(slave_locks, slave_locks_size);
4720         einfo->ei_cbdata = NULL;
4721
4722         RETURN(0);
4723 }
4724
4725 /**
4726  * Implementation of dt_object_operations::do_object_lock.
4727  *
4728  * Used to get LDLM lock on the non-striped and striped objects.
4729  *
4730  * \see dt_object_operations::do_object_lock() in the API description
4731  * for details.
4732  */
4733 static int lod_object_lock(const struct lu_env *env,
4734                            struct dt_object *dt,
4735                            struct lustre_handle *lh,
4736                            struct ldlm_enqueue_info *einfo,
4737                            union ldlm_policy_data *policy)
4738 {
4739         struct lod_object       *lo = lod_dt_obj(dt);
4740         int                     rc = 0;
4741         int                     i;
4742         int                     slave_locks_size;
4743         struct lustre_handle_array *slave_locks = NULL;
4744         ENTRY;
4745
4746         /* remote object lock */
4747         if (!einfo->ei_enq_slave) {
4748                 LASSERT(dt_object_remote(dt));
4749                 return dt_object_lock(env, dt_object_child(dt), lh, einfo,
4750                                       policy);
4751         }
4752
4753         if (!S_ISDIR(dt->do_lu.lo_header->loh_attr))
4754                 GOTO(out, rc = -ENOTDIR);
4755
4756         rc = lod_load_striping(env, lo);
4757         if (rc != 0)
4758                 GOTO(out, rc);
4759
4760         /* No stripes */
4761         if (lo->ldo_dir_stripenr <= 1) {
4762                 /*
4763                  * NB, ei_cbdata stores pointer to slave locks, if no locks
4764                  * taken, make sure it's set to NULL, otherwise MDT will try to
4765                  * unlock them.
4766                  */
4767                 einfo->ei_cbdata = NULL;
4768                 GOTO(out, rc = 0);
4769         }
4770
4771         slave_locks_size = sizeof(*slave_locks) + lo->ldo_dir_stripenr *
4772                            sizeof(slave_locks->handles[0]);
4773         /* Freed in lod_object_unlock */
4774         OBD_ALLOC(slave_locks, slave_locks_size);
4775         if (slave_locks == NULL)
4776                 GOTO(out, rc = -ENOMEM);
4777         slave_locks->count = lo->ldo_dir_stripenr;
4778
4779         /* striped directory lock */
4780         for (i = 1; i < lo->ldo_dir_stripenr; i++) {
4781                 struct lustre_handle    lockh;
4782                 struct ldlm_res_id      *res_id;
4783
4784                 res_id = &lod_env_info(env)->lti_res_id;
4785                 fid_build_reg_res_name(lu_object_fid(&lo->ldo_stripe[i]->do_lu),
4786                                        res_id);
4787                 einfo->ei_res_id = res_id;
4788
4789                 LASSERT(lo->ldo_stripe[i] != NULL);
4790                 if (likely(dt_object_remote(lo->ldo_stripe[i]))) {
4791                         rc = dt_object_lock(env, lo->ldo_stripe[i], &lockh,
4792                                             einfo, policy);
4793                 } else {
4794                         struct ldlm_namespace *ns = einfo->ei_namespace;
4795                         ldlm_blocking_callback blocking = einfo->ei_cb_local_bl;
4796                         ldlm_completion_callback completion = einfo->ei_cb_cp;
4797                         __u64   dlmflags = LDLM_FL_ATOMIC_CB;
4798
4799                         if (einfo->ei_mode == LCK_PW ||
4800                             einfo->ei_mode == LCK_EX)
4801                                 dlmflags |= LDLM_FL_COS_INCOMPAT;
4802
4803                         /* This only happens if there are mulitple stripes
4804                          * on the master MDT, i.e. except stripe0, there are
4805                          * other stripes on the Master MDT as well, Only
4806                          * happens in the test case right now. */
4807                         LASSERT(ns != NULL);
4808                         rc = ldlm_cli_enqueue_local(ns, res_id, LDLM_IBITS,
4809                                                     policy, einfo->ei_mode,
4810                                                     &dlmflags, blocking,
4811                                                     completion, NULL,
4812                                                     NULL, 0, LVB_T_NONE,
4813                                                     NULL, &lockh);
4814                 }
4815                 if (rc != 0)
4816                         break;
4817                 slave_locks->handles[i] = lockh;
4818         }
4819         einfo->ei_cbdata = slave_locks;
4820
4821         if (rc != 0 && slave_locks != NULL) {
4822                 lod_object_unlock_internal(env, dt, einfo, policy);
4823                 OBD_FREE(slave_locks, slave_locks_size);
4824         }
4825         EXIT;
4826 out:
4827         if (rc != 0)
4828                 einfo->ei_cbdata = NULL;
4829         RETURN(rc);
4830 }
4831
4832 /**
4833  * Implementation of dt_object_operations::do_invalidate.
4834  *
4835  * \see dt_object_operations::do_invalidate() in the API description for details
4836  */
4837 static int lod_invalidate(const struct lu_env *env, struct dt_object *dt)
4838 {
4839         return dt_invalidate(env, dt_object_child(dt));
4840 }
4841
4842 static int lod_declare_layout_change(const struct lu_env *env,
4843                                      struct dt_object *dt,
4844                                      struct layout_intent *layout,
4845                                      const struct lu_buf *buf,
4846                                      struct thandle *th)
4847 {
4848         struct lod_thread_info  *info = lod_env_info(env);
4849         struct lod_object *lo = lod_dt_obj(dt);
4850         struct lod_device *d = lu2lod_dev(dt->do_lu.lo_dev);
4851         struct dt_object *next = dt_object_child(dt);
4852         struct ost_pool *inuse = &info->lti_inuse_osts;
4853         struct lod_layout_component *lod_comp;
4854         struct lov_comp_md_v1 *comp_v1 = NULL;
4855         bool replay = false;
4856         bool need_create = false;
4857         int i, rc;
4858         ENTRY;
4859
4860         if (!S_ISREG(dt->do_lu.lo_header->loh_attr) || !dt_object_exists(dt) ||
4861             dt_object_remote(next))
4862                 RETURN(-EINVAL);
4863
4864         dt_write_lock(env, next, 0);
4865         /*
4866          * In case the client is passing lovea, which only happens during
4867          * the replay of layout intent write RPC for now, we may need to
4868          * parse the lovea and apply new layout configuration.
4869          */
4870         if (buf && buf->lb_len)  {
4871                 struct lov_user_md_v1 *v1 = buf->lb_buf;
4872
4873                 if (v1->lmm_magic != (LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1) &&
4874                     v1->lmm_magic !=
4875                                 __swab32(LOV_MAGIC_DEF | LOV_MAGIC_COMP_V1)) {
4876                         CERROR("%s: the replay buffer of layout extend "
4877                                "(magic %#x) does not contain expected "
4878                                "composite layout.\n",
4879                                lod2obd(d)->obd_name, v1->lmm_magic);
4880                         GOTO(out, rc = -EINVAL);
4881                 }
4882
4883                 lod_object_free_striping(env, lo);
4884                 rc = lod_use_defined_striping(env, lo, buf);
4885                 if (rc)
4886                         GOTO(out, rc);
4887
4888                 rc = lod_get_lov_ea(env, lo);
4889                 if (rc <= 0)
4890                         GOTO(out, rc);
4891                 /* old on-disk EA is stored in info->lti_buf */
4892                 comp_v1 = (struct lov_comp_md_v1 *)&info->lti_buf.lb_buf;
4893                 replay = true;
4894         } else {
4895                 /* non replay path */
4896                 rc = lod_load_striping_locked(env, lo);
4897                 if (rc)
4898                         GOTO(out, rc);
4899
4900                 /* Prepare inuse array for composite file */
4901                 rc = lod_prepare_inuse(env, lo);
4902                 if (rc)
4903                         GOTO(out, rc);
4904         }
4905
4906         /* Make sure defined layout covers the requested write range. */
4907         lod_comp = &lo->ldo_comp_entries[lo->ldo_comp_cnt - 1];
4908         if (lo->ldo_comp_cnt > 1 &&
4909             lod_comp->llc_extent.e_end != OBD_OBJECT_EOF &&
4910             lod_comp->llc_extent.e_end < layout->li_end) {
4911                 CDEBUG(replay ? D_ERROR : D_LAYOUT,
4912                        "%s: the defined layout [0, %#llx) does not covers "
4913                        "the write range [%#llx, %#llx).\n",
4914                        lod2obd(d)->obd_name, lod_comp->llc_extent.e_end,
4915                        layout->li_start, layout->li_end);
4916                 GOTO(out, rc = -EINVAL);
4917         }
4918
4919         /*
4920          * Iterate ld->ldo_comp_entries, find the component whose extent under
4921          * the write range and not instantianted.
4922          */
4923         for (i = 0; i < lo->ldo_comp_cnt; i++) {
4924                 lod_comp = &lo->ldo_comp_entries[i];
4925
4926                 if (lod_comp->llc_extent.e_start >= layout->li_end)
4927                         break;
4928
4929                 if (!replay) {
4930                         if (lod_comp_inited(lod_comp))
4931                                 continue;
4932                 } else {
4933                         /**
4934                          * In replay path, lod_comp is the EA passed by
4935                          * client replay buffer,  comp_v1 is the pre-recovery
4936                          * on-disk EA, we'd sift out those components which
4937                          * were init-ed in the on-disk EA.
4938                          */
4939                         if (le32_to_cpu(comp_v1->lcm_entries[i].lcme_flags) &
4940                             LCME_FL_INIT)
4941                                 continue;
4942                 }
4943                 /*
4944                  * this component hasn't instantiated in normal path, or during
4945                  * replay it needs replay the instantiation.
4946                  */
4947
4948                 /* A released component is being extended */
4949                 if (lod_comp->llc_pattern & LOV_PATTERN_F_RELEASED)
4950                         GOTO(out, rc = -EINVAL);
4951
4952                 need_create = true;
4953
4954                 rc = lod_qos_prep_create(env, lo, NULL, th, i, inuse);
4955                 if (rc)
4956                         break;
4957         }
4958
4959         if (need_create)
4960                 lod_obj_inc_layout_gen(lo);
4961         else
4962                 GOTO(unlock, rc = -EALREADY);
4963
4964         if (!rc) {
4965                 info->lti_buf.lb_len = lod_comp_md_size(lo, false);
4966                 rc = lod_sub_object_declare_xattr_set(env, next, &info->lti_buf,
4967                                                       XATTR_NAME_LOV, 0, th);
4968         }
4969 out:
4970         if (rc)
4971                 lod_object_free_striping(env, lo);
4972
4973 unlock:
4974         dt_write_unlock(env, next);
4975
4976         RETURN(rc);
4977 }
4978
4979 /**
4980  * Instantiate layout component objects which covers the intent write offset.
4981  */
4982 static int lod_layout_change(const struct lu_env *env, struct dt_object *dt,
4983                              struct layout_intent *layout,
4984                              const struct lu_buf *buf, struct thandle *th)
4985 {
4986         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
4987
4988         RETURN(lod_striping_create(env, dt, attr, NULL, th));
4989 }
4990
4991 struct dt_object_operations lod_obj_ops = {
4992         .do_read_lock           = lod_object_read_lock,
4993         .do_write_lock          = lod_object_write_lock,
4994         .do_read_unlock         = lod_object_read_unlock,
4995         .do_write_unlock        = lod_object_write_unlock,
4996         .do_write_locked        = lod_object_write_locked,
4997         .do_attr_get            = lod_attr_get,
4998         .do_declare_attr_set    = lod_declare_attr_set,
4999         .do_attr_set            = lod_attr_set,
5000         .do_xattr_get           = lod_xattr_get,
5001         .do_declare_xattr_set   = lod_declare_xattr_set,
5002         .do_xattr_set           = lod_xattr_set,
5003         .do_declare_xattr_del   = lod_declare_xattr_del,
5004         .do_xattr_del           = lod_xattr_del,
5005         .do_xattr_list          = lod_xattr_list,
5006         .do_ah_init             = lod_ah_init,
5007         .do_declare_create      = lod_declare_object_create,
5008         .do_create              = lod_object_create,
5009         .do_declare_destroy     = lod_declare_object_destroy,
5010         .do_destroy             = lod_object_destroy,
5011         .do_index_try           = lod_index_try,
5012         .do_declare_ref_add     = lod_declare_ref_add,
5013         .do_ref_add             = lod_ref_add,
5014         .do_declare_ref_del     = lod_declare_ref_del,
5015         .do_ref_del             = lod_ref_del,
5016         .do_object_sync         = lod_object_sync,
5017         .do_object_lock         = lod_object_lock,
5018         .do_object_unlock       = lod_object_unlock,
5019         .do_invalidate          = lod_invalidate,
5020         .do_declare_layout_change = lod_declare_layout_change,
5021         .do_layout_change       = lod_layout_change,
5022 };
5023
5024 /**
5025  * Implementation of dt_body_operations::dbo_read.
5026  *
5027  * \see dt_body_operations::dbo_read() in the API description for details.
5028  */
5029 static ssize_t lod_read(const struct lu_env *env, struct dt_object *dt,
5030                         struct lu_buf *buf, loff_t *pos)
5031 {
5032         struct dt_object *next = dt_object_child(dt);
5033         return next->do_body_ops->dbo_read(env, next, buf, pos);
5034 }
5035
5036 /**
5037  * Implementation of dt_body_operations::dbo_declare_write.
5038  *
5039  * \see dt_body_operations::dbo_declare_write() in the API description
5040  * for details.
5041  */
5042 static ssize_t lod_declare_write(const struct lu_env *env,
5043                                  struct dt_object *dt,
5044                                  const struct lu_buf *buf, loff_t pos,
5045                                  struct thandle *th)
5046 {
5047         return lod_sub_object_declare_write(env, dt_object_child(dt), buf, pos,
5048                                             th);
5049 }
5050
5051 /**
5052  * Implementation of dt_body_operations::dbo_write.
5053  *
5054  * \see dt_body_operations::dbo_write() in the API description for details.
5055  */
5056 static ssize_t lod_write(const struct lu_env *env, struct dt_object *dt,
5057                          const struct lu_buf *buf, loff_t *pos,
5058                          struct thandle *th, int iq)
5059 {
5060         return lod_sub_object_write(env, dt_object_child(dt), buf, pos, th, iq);
5061 }
5062
5063 static int lod_declare_punch(const struct lu_env *env, struct dt_object *dt,
5064                              __u64 start, __u64 end, struct thandle *th)
5065 {
5066         if (dt_object_remote(dt))
5067                 return -ENOTSUPP;
5068
5069         return lod_sub_object_declare_punch(env, dt_object_child(dt), start,
5070                                             end, th);
5071 }
5072
5073 static int lod_punch(const struct lu_env *env, struct dt_object *dt,
5074                      __u64 start, __u64 end, struct thandle *th)
5075 {
5076         if (dt_object_remote(dt))
5077                 return -ENOTSUPP;
5078
5079         return lod_sub_object_punch(env, dt_object_child(dt), start, end, th);
5080 }
5081
5082 static const struct dt_body_operations lod_body_lnk_ops = {
5083         .dbo_read               = lod_read,
5084         .dbo_declare_write      = lod_declare_write,
5085         .dbo_write              = lod_write
5086 };
5087
5088 static const struct dt_body_operations lod_body_ops = {
5089         .dbo_read               = lod_read,
5090         .dbo_declare_write      = lod_declare_write,
5091         .dbo_write              = lod_write,
5092         .dbo_declare_punch      = lod_declare_punch,
5093         .dbo_punch              = lod_punch,
5094 };
5095
5096 /**
5097  * Implementation of lu_object_operations::loo_object_init.
5098  *
5099  * The function determines the type and the index of the target device using
5100  * sequence of the object's FID. Then passes control down to the
5101  * corresponding device:
5102  *  OSD for the local objects, OSP for remote
5103  *
5104  * \see lu_object_operations::loo_object_init() in the API description
5105  * for details.
5106  */
5107 static int lod_object_init(const struct lu_env *env, struct lu_object *lo,
5108                            const struct lu_object_conf *conf)
5109 {
5110         struct lod_device       *lod    = lu2lod_dev(lo->lo_dev);
5111         struct lu_device        *cdev   = NULL;
5112         struct lu_object        *cobj;
5113         struct lod_tgt_descs    *ltd    = NULL;
5114         struct lod_tgt_desc     *tgt;
5115         u32                      idx    = 0;
5116         int                      type   = LU_SEQ_RANGE_ANY;
5117         int                      rc;
5118         ENTRY;
5119
5120         rc = lod_fld_lookup(env, lod, lu_object_fid(lo), &idx, &type);
5121         if (rc != 0) {
5122                 /* Note: Sometimes, it will Return EAGAIN here, see
5123                  * ptrlpc_import_delay_req(), which might confuse
5124                  * lu_object_find_at() and make it wait there incorrectly.
5125                  * so we convert it to EIO here.*/
5126                 if (rc == -EAGAIN)
5127                         rc = -EIO;
5128
5129                 RETURN(rc);
5130         }
5131
5132         if (type == LU_SEQ_RANGE_MDT &&
5133             idx == lu_site2seq(lo->lo_dev->ld_site)->ss_node_id) {
5134                 cdev = &lod->lod_child->dd_lu_dev;
5135         } else if (type == LU_SEQ_RANGE_MDT) {
5136                 ltd = &lod->lod_mdt_descs;
5137                 lod_getref(ltd);
5138         } else if (type == LU_SEQ_RANGE_OST) {
5139                 ltd = &lod->lod_ost_descs;
5140                 lod_getref(ltd);
5141         } else {
5142                 LBUG();
5143         }
5144
5145         if (ltd != NULL) {
5146                 if (ltd->ltd_tgts_size > idx &&
5147                     cfs_bitmap_check(ltd->ltd_tgt_bitmap, idx)) {
5148                         tgt = LTD_TGT(ltd, idx);
5149
5150                         LASSERT(tgt != NULL);
5151                         LASSERT(tgt->ltd_tgt != NULL);
5152
5153                         cdev = &(tgt->ltd_tgt->dd_lu_dev);
5154                 }
5155                 lod_putref(lod, ltd);
5156         }
5157
5158         if (unlikely(cdev == NULL))
5159                 RETURN(-ENOENT);
5160
5161         cobj = cdev->ld_ops->ldo_object_alloc(env, lo->lo_header, cdev);
5162         if (unlikely(cobj == NULL))
5163                 RETURN(-ENOMEM);
5164
5165         lu_object_add(lo, cobj);
5166
5167         RETURN(0);
5168 }
5169
5170 /**
5171  *
5172  * Release resources associated with striping.
5173  *
5174  * If the object is striped (regular or directory), then release
5175  * the stripe objects references and free the ldo_stripe array.
5176  *
5177  * \param[in] env       execution environment
5178  * \param[in] lo        object
5179  */
5180 void lod_object_free_striping(const struct lu_env *env, struct lod_object *lo)
5181 {
5182         struct lod_layout_component *lod_comp;
5183         int i, j;
5184
5185         if (lo->ldo_stripe != NULL) {
5186                 LASSERT(lo->ldo_comp_entries == NULL);
5187                 LASSERT(lo->ldo_dir_stripes_allocated > 0);
5188
5189                 for (i = 0; i < lo->ldo_dir_stripenr; i++) {
5190                         if (lo->ldo_stripe[i])
5191                                 dt_object_put(env, lo->ldo_stripe[i]);
5192                 }
5193
5194                 j = sizeof(struct dt_object *) * lo->ldo_dir_stripes_allocated;
5195                 OBD_FREE(lo->ldo_stripe, j);
5196                 lo->ldo_stripe = NULL;
5197                 lo->ldo_dir_stripes_allocated = 0;
5198                 lo->ldo_dir_stripenr = 0;
5199         } else if (lo->ldo_comp_entries != NULL) {
5200                 for (i = 0; i < lo->ldo_comp_cnt; i++) {
5201                         /* free lod_layout_component::llc_stripe array */
5202                         lod_comp = &lo->ldo_comp_entries[i];
5203
5204                         if (lod_comp->llc_stripe == NULL)
5205                                 continue;
5206                         LASSERT(lod_comp->llc_stripes_allocated != 0);
5207                         for (j = 0; j < lod_comp->llc_stripes_allocated; j++) {
5208                                 if (lod_comp->llc_stripe[j] != NULL)
5209                                         lu_object_put(env,
5210                                                &lod_comp->llc_stripe[j]->do_lu);
5211                         }
5212                         OBD_FREE(lod_comp->llc_stripe,
5213                                  sizeof(struct dt_object *) *
5214                                  lod_comp->llc_stripes_allocated);
5215                         lod_comp->llc_stripe = NULL;
5216                         lod_comp->llc_stripes_allocated = 0;
5217                 }
5218                 lod_free_comp_entries(lo);
5219                 lo->ldo_comp_cached = 0;
5220         }
5221 }
5222
5223 /**
5224  * Implementation of lu_object_operations::loo_object_start.
5225  *
5226  * \see lu_object_operations::loo_object_start() in the API description
5227  * for details.
5228  */
5229 static int lod_object_start(const struct lu_env *env, struct lu_object *o)
5230 {
5231         if (S_ISLNK(o->lo_header->loh_attr & S_IFMT)) {
5232                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_lnk_ops;
5233         } else if (S_ISREG(o->lo_header->loh_attr & S_IFMT) ||
5234                    fid_is_local_file(lu_object_fid(o))) {
5235                 /* Note: some local file (like last rcvd) is created
5236                  * through bottom layer (OSD), so the object initialization
5237                  * comes to lod, it does not set loh_attr yet, so
5238                  * set do_body_ops for local file anyway */
5239                 lu2lod_obj(o)->ldo_obj.do_body_ops = &lod_body_ops;
5240         }
5241         return 0;
5242 }
5243
5244 /**
5245  * Implementation of lu_object_operations::loo_object_free.
5246  *
5247  * \see lu_object_operations::loo_object_free() in the API description
5248  * for details.
5249  */
5250 static void lod_object_free(const struct lu_env *env, struct lu_object *o)
5251 {
5252         struct lod_object *lo = lu2lod_obj(o);
5253
5254         /* release all underlying object pinned */
5255         lod_object_free_striping(env, lo);
5256         lu_object_fini(o);
5257         OBD_SLAB_FREE_PTR(lo, lod_object_kmem);
5258 }
5259
5260 /**
5261  * Implementation of lu_object_operations::loo_object_release.
5262  *
5263  * \see lu_object_operations::loo_object_release() in the API description
5264  * for details.
5265  */
5266 static void lod_object_release(const struct lu_env *env, struct lu_object *o)
5267 {
5268         /* XXX: shouldn't we release everything here in case if object
5269          * creation failed before? */
5270 }
5271
5272 /**
5273  * Implementation of lu_object_operations::loo_object_print.
5274  *
5275  * \see lu_object_operations::loo_object_print() in the API description
5276  * for details.
5277  */
5278 static int lod_object_print(const struct lu_env *env, void *cookie,
5279                             lu_printer_t p, const struct lu_object *l)
5280 {
5281         struct lod_object *o = lu2lod_obj((struct lu_object *) l);
5282
5283         return (*p)(env, cookie, LUSTRE_LOD_NAME"-object@%p", o);
5284 }
5285
5286 struct lu_object_operations lod_lu_obj_ops = {
5287         .loo_object_init        = lod_object_init,
5288         .loo_object_start       = lod_object_start,
5289         .loo_object_free        = lod_object_free,
5290         .loo_object_release     = lod_object_release,
5291         .loo_object_print       = lod_object_print,
5292 };