Whamcloud - gitweb
LU-13124 scrub: check for multiple linked file
[fs/lustre-release.git] / lustre / lfsck / lfsck_layout.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 (c) 2014, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_layout.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34 #define DEBUG_SUBSYSTEM S_LFSCK
35
36 #include <linux/bitops.h>
37 #include <linux/rbtree.h>
38
39 #include <lu_object.h>
40 #include <dt_object.h>
41 #include <lustre_fid.h>
42 #include <lustre_lib.h>
43 #include <lustre_net.h>
44 #include <md_object.h>
45 #include <obd_class.h>
46
47 #include "lfsck_internal.h"
48
49 #define LFSCK_LAYOUT_MAGIC_V1           0xB173AE14
50 #define LFSCK_LAYOUT_MAGIC_V2           0xB1734D76
51 #define LFSCK_LAYOUT_MAGIC_V3           0xB17371B9
52 #define LFSCK_LAYOUT_MAGIC_V4           0xB1732FED
53
54 #define LFSCK_LAYOUT_MAGIC              LFSCK_LAYOUT_MAGIC_V4
55
56 struct lfsck_layout_seq {
57         struct list_head         lls_list;
58         __u64                    lls_seq;
59         __u64                    lls_lastid;
60         __u64                    lls_lastid_known;
61         struct dt_object        *lls_lastid_obj;
62         unsigned int             lls_dirty:1;
63 };
64
65 struct lfsck_layout_slave_target {
66         /* link into lfsck_layout_slave_data::llsd_master_list. */
67         struct list_head        llst_list;
68         /* The position for next record in the rbtree for iteration. */
69         struct lu_fid           llst_fid;
70         /* Dummy hash for iteration against the rbtree. */
71         __u64                   llst_hash;
72         __u64                   llst_gen;
73         atomic_t                llst_ref;
74         __u32                   llst_index;
75         /* How many times we have failed to get the master status. */
76         int                     llst_failures;
77 };
78
79 struct lfsck_layout_slave_data {
80         /* list for lfsck_layout_seq */
81         struct list_head         llsd_seq_list;
82
83         /* list for the masters involve layout verification. */
84         struct list_head         llsd_master_list;
85         spinlock_t               llsd_lock;
86         __u64                    llsd_touch_gen;
87         struct dt_object        *llsd_rb_obj;
88         struct rb_root           llsd_rb_root;
89         struct rw_semaphore      llsd_rb_rwsem;
90         unsigned int             llsd_rbtree_valid:1;
91 };
92
93 struct lfsck_layout_slave_async_args {
94         struct obd_export                *llsaa_exp;
95         struct lfsck_component           *llsaa_com;
96         struct lfsck_layout_slave_target *llsaa_llst;
97 };
98
99 static inline bool lfsck_comp_extent_aligned(__u64 border, __u32 size)
100 {
101         return (border & (size - 1)) == 0;
102 }
103
104 static inline void
105 lfsck_layout_llst_put(struct lfsck_layout_slave_target *llst)
106 {
107         if (atomic_dec_and_test(&llst->llst_ref)) {
108                 LASSERT(list_empty(&llst->llst_list));
109
110                 OBD_FREE_PTR(llst);
111         }
112 }
113
114 static inline int
115 lfsck_layout_llst_add(struct lfsck_layout_slave_data *llsd, __u32 index)
116 {
117         struct lfsck_layout_slave_target *llst;
118         struct lfsck_layout_slave_target *tmp;
119         int                               rc   = 0;
120
121         OBD_ALLOC_PTR(llst);
122         if (llst == NULL)
123                 return -ENOMEM;
124
125         INIT_LIST_HEAD(&llst->llst_list);
126         llst->llst_gen = 0;
127         llst->llst_index = index;
128         atomic_set(&llst->llst_ref, 1);
129
130         spin_lock(&llsd->llsd_lock);
131         list_for_each_entry(tmp, &llsd->llsd_master_list, llst_list) {
132                 if (tmp->llst_index == index) {
133                         rc = -EALREADY;
134                         break;
135                 }
136         }
137         if (rc == 0)
138                 list_add_tail(&llst->llst_list, &llsd->llsd_master_list);
139         spin_unlock(&llsd->llsd_lock);
140
141         if (rc != 0)
142                 OBD_FREE_PTR(llst);
143
144         return rc;
145 }
146
147 static inline void
148 lfsck_layout_llst_del(struct lfsck_layout_slave_data *llsd,
149                       struct lfsck_layout_slave_target *llst)
150 {
151         bool del = false;
152
153         spin_lock(&llsd->llsd_lock);
154         if (!list_empty(&llst->llst_list)) {
155                 list_del_init(&llst->llst_list);
156                 del = true;
157         }
158         spin_unlock(&llsd->llsd_lock);
159
160         if (del)
161                 lfsck_layout_llst_put(llst);
162 }
163
164 static inline struct lfsck_layout_slave_target *
165 lfsck_layout_llst_find_and_del(struct lfsck_layout_slave_data *llsd,
166                                __u32 index, bool unlink)
167 {
168         struct lfsck_layout_slave_target *llst;
169
170         spin_lock(&llsd->llsd_lock);
171         list_for_each_entry(llst, &llsd->llsd_master_list, llst_list) {
172                 if (llst->llst_index == index) {
173                         if (unlink)
174                                 list_del_init(&llst->llst_list);
175                         else
176                                 atomic_inc(&llst->llst_ref);
177                         spin_unlock(&llsd->llsd_lock);
178
179                         return llst;
180                 }
181         }
182         spin_unlock(&llsd->llsd_lock);
183
184         return NULL;
185 }
186
187 static struct lfsck_layout_req *
188 lfsck_layout_assistant_req_init(struct lfsck_assistant_object *lso,
189                                 struct dt_object *child, __u32 comp_id,
190                                 __u32 ost_idx, __u32 lov_idx)
191 {
192         struct lfsck_layout_req *llr;
193
194         OBD_ALLOC_PTR(llr);
195         if (llr == NULL)
196                 return ERR_PTR(-ENOMEM);
197
198         INIT_LIST_HEAD(&llr->llr_lar.lar_list);
199         llr->llr_lar.lar_parent = lfsck_assistant_object_get(lso);
200         llr->llr_child = child;
201         llr->llr_comp_id = comp_id;
202         llr->llr_ost_idx = ost_idx;
203         llr->llr_lov_idx = lov_idx;
204
205         return llr;
206 }
207
208 static void lfsck_layout_assistant_req_fini(const struct lu_env *env,
209                                             struct lfsck_assistant_req *lar)
210 {
211         struct lfsck_layout_req *llr =
212                 container_of(lar, struct lfsck_layout_req, llr_lar);
213
214         lfsck_object_put(env, llr->llr_child);
215         lfsck_assistant_object_put(env, lar->lar_parent);
216         OBD_FREE_PTR(llr);
217 }
218
219 static int
220 lfsck_layout_assistant_sync_failures_interpret(const struct lu_env *env,
221                                                struct ptlrpc_request *req,
222                                                void *args, int rc)
223 {
224         if (rc == 0) {
225                 struct lfsck_async_interpret_args *laia = args;
226                 struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
227
228                 ltd->ltd_synced_failures = 1;
229                 atomic_dec(laia->laia_count);
230         }
231
232         return 0;
233 }
234
235 /**
236  * Notify remote LFSCK instances about former failures.
237  *
238  * The local LFSCK instance has recorded which OSTs have ever failed to respond
239  * some LFSCK verification requests (maybe because of network issues or the OST
240  * itself trouble). During the respond gap, the OST may missed some OST-objects
241  * verification, then the OST cannot know whether related OST-objects have been
242  * referenced by related MDT-objects or not, then in the second-stage scanning,
243  * these OST-objects will be regarded as orphan, if the OST-object contains bad
244  * parent FID for back reference, then it will misguide the LFSCK to make wrong
245  * fixing for the fake orphan.
246  *
247  * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
248  * it will scan the bitmap for the ever failed OSTs, and notify them that they
249  * have ever missed some OST-object verification and should skip the handling
250  * for orphan OST-objects on all MDTs that are in the layout LFSCK.
251  *
252  * \param[in] env       pointer to the thread context
253  * \param[in] com       pointer to the lfsck component
254  * \param[in] lr        pointer to the lfsck request
255  */
256 static void lfsck_layout_assistant_sync_failures(const struct lu_env *env,
257                                                  struct lfsck_component *com,
258                                                  struct lfsck_request *lr)
259 {
260         struct lfsck_async_interpret_args *laia  =
261                                 &lfsck_env_info(env)->lti_laia2;
262         struct lfsck_assistant_data       *lad   = com->lc_data;
263         struct lfsck_layout               *lo    = com->lc_file_ram;
264         struct lfsck_instance             *lfsck = com->lc_lfsck;
265         struct lfsck_tgt_descs            *ltds  = &lfsck->li_ost_descs;
266         struct lfsck_tgt_desc             *ltd;
267         struct ptlrpc_request_set         *set;
268         atomic_t                           count;
269         __u32                              idx;
270         int                                rc    = 0;
271         ENTRY;
272
273         if (!test_bit(LAD_INCOMPLETE, &lad->lad_flags))
274                 RETURN_EXIT;
275
276         /* If the MDT has ever failed to verfiy some OST-objects,
277          * then sync failures with them firstly. */
278         lr->lr_flags2 = lo->ll_flags | LF_INCOMPLETE;
279
280         atomic_set(&count, 0);
281         memset(laia, 0, sizeof(*laia));
282         laia->laia_count = &count;
283         set = ptlrpc_prep_set();
284         if (set == NULL)
285                 GOTO(out, rc = -ENOMEM);
286
287         down_read(&ltds->ltd_rw_sem);
288         cfs_foreach_bit(lad->lad_bitmap, idx) {
289                 ltd = lfsck_ltd2tgt(ltds, idx);
290                 if (unlikely(!ltd))
291                         continue;
292
293                 laia->laia_ltd = ltd;
294                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
295                                 lfsck_layout_assistant_sync_failures_interpret,
296                                 laia, LFSCK_NOTIFY);
297                 if (rc != 0) {
298                         CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
299                                "notify target %x for %s phase1 done: "
300                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
301                                ltd->ltd_index, lad->lad_name, rc);
302
303                         break;
304                 }
305
306                 atomic_inc(&count);
307         }
308         up_read(&ltds->ltd_rw_sem);
309
310         if (rc == 0 && atomic_read(&count) > 0)
311                 rc = ptlrpc_set_wait(env, set);
312
313         ptlrpc_set_destroy(set);
314
315         if (rc == 0 && atomic_read(&count) > 0)
316                 rc = -EINVAL;
317
318         GOTO(out, rc);
319
320 out:
321         if (rc != 0)
322                 /* If failed to sync failures with the OSTs, then have to
323                  * mark the whole LFSCK as LF_INCOMPLETE to skip the whole
324                  * subsequent orphan OST-object handling. */
325                 lo->ll_flags |= LF_INCOMPLETE;
326
327         lr->lr_flags2 = lo->ll_flags;
328 }
329
330 static int lfsck_layout_verify_header_v1v3(struct dt_object *obj,
331                                            struct lov_mds_md_v1 *lmm,
332                                            __u64 start, __u64 end,
333                                            __u32 comp_id,
334                                            bool ext, bool *dom)
335 {
336         __u32 magic;
337         __u32 pattern;
338         __u32 size;
339
340         magic = le32_to_cpu(lmm->lmm_magic);
341         /* If magic crashed, keep it there. Sometime later, during OST-object
342          * orphan handling, if some OST-object(s) back-point to it, it can be
343          * verified and repaired. */
344         if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3) {
345                 int rc;
346
347                 if ((magic & LOV_MAGIC_MASK) == LOV_MAGIC_MAGIC)
348                         rc = -EOPNOTSUPP;
349                 else
350                         rc = -EINVAL;
351
352                 CDEBUG(D_LFSCK, "%s LOV EA magic 0x%X for the file "DFID"\n",
353                        rc == -EINVAL ? "Unknown" : "Unsupported",
354                        magic, PFID(lfsck_dto2fid(obj)));
355
356                 return rc;
357         }
358
359         pattern = le32_to_cpu(lmm->lmm_pattern);
360         *dom = !!(lov_pattern(pattern) == LOV_PATTERN_MDT);
361
362         /* XXX: DoM file verification will be supportted via LU-11081. */
363         if (lov_pattern(pattern) == LOV_PATTERN_MDT) {
364 #if 0
365                 if (start != 0) {
366                         CDEBUG(D_LFSCK, "The DoM entry for "DFID" is not "
367                                "the first component in the mirror %x/%llu\n",
368                                PFID(lfsck_dto2fid(obj)), comp_id, start);
369
370                         return -EINVAL;
371                 }
372 #endif
373         } else if (!lov_pattern_supported_normal_comp(lov_pattern(pattern))) {
374                 CDEBUG(D_LFSCK, "Unsupported LOV EA pattern %u for the file "
375                        DFID" in the component %x\n",
376                        pattern, PFID(lfsck_dto2fid(obj)), comp_id);
377
378                 return -EOPNOTSUPP;
379         }
380
381         size = le32_to_cpu(lmm->lmm_stripe_size);
382         if (!ext && end != LUSTRE_EOF && start != end &&
383             !lfsck_comp_extent_aligned(end, size)){
384                 CDEBUG(D_LFSCK, "not aligned border in PFL extent range "
385                        "[%llu - %llu) stripesize %u for the file "DFID
386                        " at idx %d\n", start, end, size,
387                        PFID(lfsck_dto2fid(obj)), comp_id);
388
389                 return -EINVAL;
390         }
391
392         return 0;
393 }
394
395 static int lfsck_layout_verify_header_foreign(struct dt_object *obj,
396                                               struct lov_foreign_md *lfm,
397                                               size_t len)
398 {
399         /* magic has been verified already */
400         __u32 value_len = le32_to_cpu(lfm->lfm_length);
401         /* type and flags are not checked for instance */
402
403         CDEBUG(D_INFO, "foreign LOV EA, magic %x, len %u, type %x, flags %x, for file "DFID"\n",
404                le32_to_cpu(lfm->lfm_magic), value_len,
405                le32_to_cpu(lfm->lfm_type), le32_to_cpu(lfm->lfm_flags),
406                PFID(lfsck_dto2fid(obj)));
407
408         if (len != value_len + offsetof(typeof(*lfm), lfm_value))
409                 CDEBUG(D_LFSCK, "foreign LOV EA internal size %u does not match EA full size %zu for file "DFID"\n",
410                        value_len, len, PFID(lfsck_dto2fid(obj)));
411
412         /* nothing to repair */
413         return -ENODATA;
414 }
415
416 static int lfsck_layout_verify_header(struct dt_object *obj,
417                                       struct lov_mds_md_v1 *lmm, size_t len)
418 {
419         bool p_dom = false;
420         int rc = 0;
421
422         if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_COMP_V1 ||
423             le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_SEL) {
424                 struct lov_comp_md_v1 *lcm = (struct lov_comp_md_v1 *)lmm;
425                 bool p_zero = false;
426                 int i;
427                 __u16 count = le16_to_cpu(lcm->lcm_entry_count);
428
429                 if (unlikely(count == 0)) {
430                         CDEBUG(D_LFSCK, "the PFL file "DFID" contains invalid "
431                                "components count 0\n",
432                                PFID(lfsck_dto2fid(obj)));
433
434                         return -EINVAL;
435                 }
436
437                 for (i = 0; i < count && !rc; i++) {
438                         struct lov_comp_md_entry_v1 *lcme =
439                                                 &lcm->lcm_entries[i];
440                         __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
441                         __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
442                         __u32 comp_id = le32_to_cpu(lcme->lcme_id);
443                         bool ext, inited, zero;
444                         __u32 flags;
445
446                         if (unlikely(comp_id == LCME_ID_INVAL ||
447                                      comp_id > LCME_ID_MAX)) {
448                                 CDEBUG(D_LFSCK, "found invalid PFL ID %u "
449                                        "for the file "DFID" at idx %d\n",
450                                        comp_id, PFID(lfsck_dto2fid(obj)), i);
451
452                                 return -EINVAL;
453                         }
454
455                         flags = le32_to_cpu(lcme->lcme_flags);
456                         ext = flags & LCME_FL_EXTENSION;
457                         inited = flags & LCME_FL_INIT;
458                         zero = !!(start == end);
459
460                         if ((i == 0) && zero) {
461                                 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu "
462                                        "- %llu) for "DFID"\n", i, start, end,
463                                        PFID(lfsck_dto2fid(obj)));
464                                 return -EINVAL;
465                         }
466
467                         if ((zero && (inited || (i + 1 == count))) ||
468                             (start > end)) {
469                                 CDEBUG(D_LFSCK, "invalid PFL comp %d/%d: "
470                                        "[%llu, %llu) for "DFID", %sinited\n",
471                                        i, count, start, end,
472                                        PFID(lfsck_dto2fid(obj)),
473                                        inited ? "" : "NOT ");
474                                 return -EINVAL;
475                         }
476
477                         if (!ext && p_zero) {
478                                 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu, "
479                                        "%llu) for "DFID": NOT extension "
480                                        "after 0-length component\n", i,
481                                        start, end, PFID(lfsck_dto2fid(obj)));
482                                 return -EINVAL;
483                         }
484
485                         if (ext && (inited || p_dom || zero)) {
486                                 CDEBUG(D_LFSCK, "invalid PFL comp %d: [%llu, "
487                                        "%llu) for "DFID": %s\n", i,
488                                        start, end, PFID(lfsck_dto2fid(obj)),
489                                        inited ? "inited extension" :
490                                        p_dom ? "extension follows DOM" :
491                                        zero ? "zero length extension" : "");
492                                 return -EINVAL;
493                         }
494
495                         rc = lfsck_layout_verify_header_v1v3(obj,
496                                         (struct lov_mds_md_v1 *)((char *)lmm +
497                                         le32_to_cpu(lcme->lcme_offset)), start,
498                                         end, comp_id, ext, &p_dom);
499
500                         p_zero = zero;
501                 }
502         } else if (le32_to_cpu(lmm->lmm_magic) == LOV_MAGIC_FOREIGN) {
503                 rc = lfsck_layout_verify_header_foreign(obj,
504                                                 (struct lov_foreign_md *)lmm,
505                                                 len);
506         } else {
507                 rc = lfsck_layout_verify_header_v1v3(obj, lmm, 0, LUSTRE_EOF,
508                                                      0, false, &p_dom);
509         }
510
511         return rc;
512 }
513
514 static int lfsck_layout_get_lovea(const struct lu_env *env,
515                                   struct dt_object *obj, struct lu_buf *buf)
516 {
517         int rc;
518         int rc1;
519
520 again:
521         rc = dt_xattr_get(env, obj, buf, XATTR_NAME_LOV);
522         if (rc == -ERANGE) {
523                 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV);
524                 if (rc <= 0)
525                         return !rc ? -ENODATA : rc;
526
527                 lu_buf_realloc(buf, rc);
528                 if (buf->lb_buf == NULL)
529                         return -ENOMEM;
530
531                 goto again;
532         }
533
534         if (rc <= 0)
535                 return !rc ? -ENODATA : rc;
536
537         if (unlikely(buf->lb_buf == NULL)) {
538                 lu_buf_alloc(buf, rc);
539                 if (buf->lb_buf == NULL)
540                         return -ENOMEM;
541
542                 goto again;
543         }
544
545         rc1 = lfsck_layout_verify_header(obj, buf->lb_buf, rc);
546
547         return rc1 ? rc1 : rc;
548 }
549
550 #define LFSCK_RBTREE_BITMAP_SIZE        PAGE_SIZE
551 #define LFSCK_RBTREE_BITMAP_WIDTH       (LFSCK_RBTREE_BITMAP_SIZE << 3)
552 #define LFSCK_RBTREE_BITMAP_MASK        (LFSCK_RBTREE_BITMAP_WIDTH - 1)
553
554 struct lfsck_rbtree_node {
555         struct rb_node   lrn_node;
556         __u64            lrn_seq;
557         __u32            lrn_first_oid;
558         atomic_t         lrn_known_count;
559         atomic_t         lrn_accessed_count;
560         void            *lrn_known_bitmap;
561         void            *lrn_accessed_bitmap;
562 };
563
564 static inline int lfsck_rbtree_cmp(struct lfsck_rbtree_node *lrn,
565                                    __u64 seq, __u32 oid)
566 {
567         if (seq < lrn->lrn_seq)
568                 return -1;
569
570         if (seq > lrn->lrn_seq)
571                 return 1;
572
573         if (oid < lrn->lrn_first_oid)
574                 return -1;
575
576         if (oid - lrn->lrn_first_oid >= LFSCK_RBTREE_BITMAP_WIDTH)
577                 return 1;
578
579         return 0;
580 }
581
582 /* The caller should hold llsd->llsd_rb_lock. */
583 static struct lfsck_rbtree_node *
584 lfsck_rbtree_search(struct lfsck_layout_slave_data *llsd,
585                     const struct lu_fid *fid, bool *exact)
586 {
587         struct rb_node           *node  = llsd->llsd_rb_root.rb_node;
588         struct rb_node           *prev  = NULL;
589         struct lfsck_rbtree_node *lrn   = NULL;
590         int                       rc    = 0;
591
592         if (exact != NULL)
593                 *exact = true;
594
595         while (node != NULL) {
596                 prev = node;
597                 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
598                 rc = lfsck_rbtree_cmp(lrn, fid_seq(fid), fid_oid(fid));
599                 if (rc < 0)
600                         node = node->rb_left;
601                 else if (rc > 0)
602                         node = node->rb_right;
603                 else
604                         return lrn;
605         }
606
607         if (exact == NULL)
608                 return NULL;
609
610         /* If there is no exactly matched one, then to the next valid one. */
611         *exact = false;
612
613         /* The rbtree is empty. */
614         if (rc == 0)
615                 return NULL;
616
617         if (rc < 0)
618                 return lrn;
619
620         node = rb_next(prev);
621
622         /* The end of the rbtree. */
623         if (node == NULL)
624                 return NULL;
625
626         lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
627
628         return lrn;
629 }
630
631 static struct lfsck_rbtree_node *lfsck_rbtree_new(const struct lu_env *env,
632                                                   const struct lu_fid *fid)
633 {
634         struct lfsck_rbtree_node *lrn;
635
636         OBD_ALLOC_PTR(lrn);
637         if (lrn == NULL)
638                 return ERR_PTR(-ENOMEM);
639
640         OBD_ALLOC(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
641         if (lrn->lrn_known_bitmap == NULL) {
642                 OBD_FREE_PTR(lrn);
643
644                 return ERR_PTR(-ENOMEM);
645         }
646
647         OBD_ALLOC(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
648         if (lrn->lrn_accessed_bitmap == NULL) {
649                 OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
650                 OBD_FREE_PTR(lrn);
651
652                 return ERR_PTR(-ENOMEM);
653         }
654
655         RB_CLEAR_NODE(&lrn->lrn_node);
656         lrn->lrn_seq = fid_seq(fid);
657         lrn->lrn_first_oid = fid_oid(fid) & ~LFSCK_RBTREE_BITMAP_MASK;
658         atomic_set(&lrn->lrn_known_count, 0);
659         atomic_set(&lrn->lrn_accessed_count, 0);
660
661         return lrn;
662 }
663
664 static void lfsck_rbtree_free(struct lfsck_rbtree_node *lrn)
665 {
666         OBD_FREE(lrn->lrn_accessed_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
667         OBD_FREE(lrn->lrn_known_bitmap, LFSCK_RBTREE_BITMAP_SIZE);
668         OBD_FREE_PTR(lrn);
669 }
670
671 /* The caller should hold lock. */
672 static struct lfsck_rbtree_node *
673 lfsck_rbtree_insert(struct lfsck_layout_slave_data *llsd,
674                     struct lfsck_rbtree_node *lrn)
675 {
676         struct rb_node           **pos    = &llsd->llsd_rb_root.rb_node;
677         struct rb_node            *parent = NULL;
678         struct lfsck_rbtree_node  *tmp;
679         int                        rc;
680
681         while (*pos != NULL) {
682                 parent = *pos;
683                 tmp = rb_entry(parent, struct lfsck_rbtree_node, lrn_node);
684                 rc = lfsck_rbtree_cmp(tmp, lrn->lrn_seq, lrn->lrn_first_oid);
685                 if (rc < 0)
686                         pos = &(*pos)->rb_left;
687                 else if (rc > 0)
688                         pos = &(*pos)->rb_right;
689                 else
690                         return tmp;
691         }
692
693         rb_link_node(&lrn->lrn_node, parent, pos);
694         rb_insert_color(&lrn->lrn_node, &llsd->llsd_rb_root);
695
696         return lrn;
697 }
698
699 static const struct dt_index_operations lfsck_orphan_index_ops;
700
701 static int lfsck_rbtree_setup(const struct lu_env *env,
702                               struct lfsck_component *com)
703 {
704         struct lu_fid                   *fid    = &lfsck_env_info(env)->lti_fid;
705         struct lfsck_instance           *lfsck  = com->lc_lfsck;
706         struct dt_device                *dev    = lfsck->li_bottom;
707         struct lfsck_layout_slave_data  *llsd   = com->lc_data;
708         struct dt_object                *obj;
709
710         fid->f_seq = FID_SEQ_LAYOUT_RBTREE;
711         fid->f_oid = lfsck_dev_idx(lfsck);
712         fid->f_ver = 0;
713         obj = dt_locate(env, dev, fid);
714         if (IS_ERR(obj))
715                 RETURN(PTR_ERR(obj));
716
717         /* Generate an in-RAM object to stand for the layout rbtree.
718          * Scanning the layout rbtree will be via the iteration over
719          * the object. In the future, the rbtree may be written onto
720          * disk with the object.
721          *
722          * Mark the object to be as exist. */
723         obj->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
724         obj->do_index_ops = &lfsck_orphan_index_ops;
725         llsd->llsd_rb_obj = obj;
726         llsd->llsd_rbtree_valid = 1;
727         dev->dd_record_fid_accessed = 1;
728
729         CDEBUG(D_LFSCK, "%s: layout LFSCK init OST-objects accessing bitmap\n",
730                lfsck_lfsck2name(lfsck));
731
732         return 0;
733 }
734
735 static void lfsck_rbtree_cleanup(const struct lu_env *env,
736                                  struct lfsck_component *com)
737 {
738         struct lfsck_instance           *lfsck = com->lc_lfsck;
739         struct lfsck_layout_slave_data  *llsd  = com->lc_data;
740         struct rb_node                  *node  = rb_first(&llsd->llsd_rb_root);
741         struct rb_node                  *next;
742         struct lfsck_rbtree_node        *lrn;
743
744         lfsck->li_bottom->dd_record_fid_accessed = 0;
745         /* Invalid the rbtree, then no others will use it. */
746         down_write(&llsd->llsd_rb_rwsem);
747         llsd->llsd_rbtree_valid = 0;
748         up_write(&llsd->llsd_rb_rwsem);
749
750         while (node != NULL) {
751                 next = rb_next(node);
752                 lrn = rb_entry(node, struct lfsck_rbtree_node, lrn_node);
753                 rb_erase(node, &llsd->llsd_rb_root);
754                 lfsck_rbtree_free(lrn);
755                 node = next;
756         }
757
758         if (llsd->llsd_rb_obj != NULL) {
759                 lfsck_object_put(env, llsd->llsd_rb_obj);
760                 llsd->llsd_rb_obj = NULL;
761         }
762
763         CDEBUG(D_LFSCK, "%s: layout LFSCK fini OST-objects accessing bitmap\n",
764                lfsck_lfsck2name(lfsck));
765 }
766
767 static void lfsck_rbtree_update_bitmap(const struct lu_env *env,
768                                        struct lfsck_component *com,
769                                        const struct lu_fid *fid,
770                                        bool accessed)
771 {
772         struct lfsck_layout_slave_data  *llsd   = com->lc_data;
773         struct lfsck_rbtree_node        *lrn;
774         bool                             insert = false;
775         int                              idx;
776         int                              rc     = 0;
777         ENTRY;
778
779         if (unlikely(!fid_is_sane(fid) || fid_is_last_id(fid)))
780                 RETURN_EXIT;
781
782         if (!fid_is_idif(fid) && !fid_is_norm(fid))
783                 RETURN_EXIT;
784
785         down_read(&llsd->llsd_rb_rwsem);
786         if (!llsd->llsd_rbtree_valid)
787                 GOTO(unlock, rc = 0);
788
789         lrn = lfsck_rbtree_search(llsd, fid, NULL);
790         if (lrn == NULL) {
791                 struct lfsck_rbtree_node *tmp;
792
793                 LASSERT(!insert);
794
795                 up_read(&llsd->llsd_rb_rwsem);
796                 tmp = lfsck_rbtree_new(env, fid);
797                 if (IS_ERR(tmp))
798                         GOTO(out, rc = PTR_ERR(tmp));
799
800                 insert = true;
801                 down_write(&llsd->llsd_rb_rwsem);
802                 if (!llsd->llsd_rbtree_valid) {
803                         lfsck_rbtree_free(tmp);
804                         GOTO(unlock, rc = 0);
805                 }
806
807                 lrn = lfsck_rbtree_insert(llsd, tmp);
808                 if (lrn != tmp)
809                         lfsck_rbtree_free(tmp);
810         }
811
812         idx = fid_oid(fid) & LFSCK_RBTREE_BITMAP_MASK;
813         /* Any accessed object must be a known object. */
814         if (!test_and_set_bit(idx, lrn->lrn_known_bitmap))
815                 atomic_inc(&lrn->lrn_known_count);
816         if (accessed && !test_and_set_bit(idx, lrn->lrn_accessed_bitmap))
817                 atomic_inc(&lrn->lrn_accessed_count);
818
819         GOTO(unlock, rc = 0);
820
821 unlock:
822         if (insert)
823                 up_write(&llsd->llsd_rb_rwsem);
824         else
825                 up_read(&llsd->llsd_rb_rwsem);
826 out:
827         if (rc != 0 && accessed) {
828                 struct lfsck_layout *lo = com->lc_file_ram;
829
830                 CDEBUG(D_LFSCK, "%s: fail to update OST-objects accessing "
831                        "bitmap, and will cause incorrect LFSCK OST-object "
832                        "handling, so disable it to cancel orphan handling "
833                        "for related device. rc = %d\n",
834                        lfsck_lfsck2name(com->lc_lfsck), rc);
835
836                 lo->ll_flags |= LF_INCOMPLETE;
837                 lfsck_rbtree_cleanup(env, com);
838         }
839 }
840
841 static inline void lldk_le_to_cpu(struct lfsck_layout_dangling_key *des,
842                                   const struct lfsck_layout_dangling_key *src)
843 {
844         fid_le_to_cpu(&des->lldk_fid, &src->lldk_fid);
845         des->lldk_comp_id = le32_to_cpu(src->lldk_comp_id);
846         des->lldk_ea_off = le32_to_cpu(src->lldk_ea_off);
847 }
848
849 static inline void lldk_cpu_to_le(struct lfsck_layout_dangling_key *des,
850                                   const struct lfsck_layout_dangling_key *src)
851 {
852         fid_cpu_to_le(&des->lldk_fid, &src->lldk_fid);
853         des->lldk_comp_id = cpu_to_le32(src->lldk_comp_id);
854         des->lldk_ea_off = cpu_to_le32(src->lldk_ea_off);
855 }
856
857 static inline void lldk_be_to_cpu(struct lfsck_layout_dangling_key *des,
858                                   const struct lfsck_layout_dangling_key *src)
859 {
860         fid_be_to_cpu(&des->lldk_fid, &src->lldk_fid);
861         des->lldk_comp_id = be32_to_cpu(src->lldk_comp_id);
862         des->lldk_ea_off = be32_to_cpu(src->lldk_ea_off);
863 }
864
865 static inline void lldk_cpu_to_be(struct lfsck_layout_dangling_key *des,
866                                   const struct lfsck_layout_dangling_key *src)
867 {
868         fid_cpu_to_be(&des->lldk_fid, &src->lldk_fid);
869         des->lldk_comp_id = cpu_to_be32(src->lldk_comp_id);
870         des->lldk_ea_off = cpu_to_be32(src->lldk_ea_off);
871 }
872
873 static void lfsck_layout_le_to_cpu(struct lfsck_layout *des,
874                                    const struct lfsck_layout *src)
875 {
876         int i;
877
878         des->ll_magic = le32_to_cpu(src->ll_magic);
879         des->ll_status = le32_to_cpu(src->ll_status);
880         des->ll_flags = le32_to_cpu(src->ll_flags);
881         des->ll_success_count = le32_to_cpu(src->ll_success_count);
882         des->ll_run_time_phase1 = le64_to_cpu(src->ll_run_time_phase1);
883         des->ll_run_time_phase2 = le64_to_cpu(src->ll_run_time_phase2);
884         des->ll_time_last_complete = le64_to_cpu(src->ll_time_last_complete);
885         des->ll_time_latest_start = le64_to_cpu(src->ll_time_latest_start);
886         des->ll_time_last_checkpoint =
887                                 le64_to_cpu(src->ll_time_last_checkpoint);
888         des->ll_pos_latest_start = le64_to_cpu(src->ll_pos_latest_start);
889         des->ll_pos_last_checkpoint = le64_to_cpu(src->ll_pos_last_checkpoint);
890         des->ll_pos_first_inconsistent =
891                         le64_to_cpu(src->ll_pos_first_inconsistent);
892         des->ll_objs_checked_phase1 = le64_to_cpu(src->ll_objs_checked_phase1);
893         des->ll_objs_failed_phase1 = le64_to_cpu(src->ll_objs_failed_phase1);
894         des->ll_objs_checked_phase2 = le64_to_cpu(src->ll_objs_checked_phase2);
895         des->ll_objs_failed_phase2 = le64_to_cpu(src->ll_objs_failed_phase2);
896         for (i = 0; i < LLIT_MAX; i++)
897                 des->ll_objs_repaired[i] =
898                                 le64_to_cpu(src->ll_objs_repaired[i]);
899         des->ll_objs_skipped = le64_to_cpu(src->ll_objs_skipped);
900         des->ll_bitmap_size = le32_to_cpu(src->ll_bitmap_size);
901         lldk_le_to_cpu(&des->ll_lldk_latest_scanned_phase2,
902                        &src->ll_lldk_latest_scanned_phase2);
903 }
904
905 static void lfsck_layout_cpu_to_le(struct lfsck_layout *des,
906                                    const struct lfsck_layout *src)
907 {
908         int i;
909
910         des->ll_magic = cpu_to_le32(src->ll_magic);
911         des->ll_status = cpu_to_le32(src->ll_status);
912         des->ll_flags = cpu_to_le32(src->ll_flags);
913         des->ll_success_count = cpu_to_le32(src->ll_success_count);
914         des->ll_run_time_phase1 = cpu_to_le64(src->ll_run_time_phase1);
915         des->ll_run_time_phase2 = cpu_to_le64(src->ll_run_time_phase2);
916         des->ll_time_last_complete = cpu_to_le64(src->ll_time_last_complete);
917         des->ll_time_latest_start = cpu_to_le64(src->ll_time_latest_start);
918         des->ll_time_last_checkpoint =
919                                 cpu_to_le64(src->ll_time_last_checkpoint);
920         des->ll_pos_latest_start = cpu_to_le64(src->ll_pos_latest_start);
921         des->ll_pos_last_checkpoint = cpu_to_le64(src->ll_pos_last_checkpoint);
922         des->ll_pos_first_inconsistent =
923                         cpu_to_le64(src->ll_pos_first_inconsistent);
924         des->ll_objs_checked_phase1 = cpu_to_le64(src->ll_objs_checked_phase1);
925         des->ll_objs_failed_phase1 = cpu_to_le64(src->ll_objs_failed_phase1);
926         des->ll_objs_checked_phase2 = cpu_to_le64(src->ll_objs_checked_phase2);
927         des->ll_objs_failed_phase2 = cpu_to_le64(src->ll_objs_failed_phase2);
928         for (i = 0; i < LLIT_MAX; i++)
929                 des->ll_objs_repaired[i] =
930                                 cpu_to_le64(src->ll_objs_repaired[i]);
931         des->ll_objs_skipped = cpu_to_le64(src->ll_objs_skipped);
932         des->ll_bitmap_size = cpu_to_le32(src->ll_bitmap_size);
933         lldk_cpu_to_le(&des->ll_lldk_latest_scanned_phase2,
934                        &src->ll_lldk_latest_scanned_phase2);
935 }
936
937 /**
938  * Load the OST bitmap from the lfsck_layout trace file.
939  *
940  * \param[in] env       pointer to the thread context
941  * \param[in] com       pointer to the lfsck component
942  *
943  * \retval              0 for success
944  * \retval              negative error number on failure or data corruption
945  */
946 static int lfsck_layout_load_bitmap(const struct lu_env *env,
947                                     struct lfsck_component *com)
948 {
949         struct dt_object                *obj    = com->lc_obj;
950         struct lfsck_assistant_data     *lad    = com->lc_data;
951         struct lfsck_layout             *lo     = com->lc_file_ram;
952         struct cfs_bitmap                       *bitmap = lad->lad_bitmap;
953         loff_t                           pos    = com->lc_file_size;
954         ssize_t                          size;
955         __u32                            nbits;
956         int                              rc;
957         ENTRY;
958
959         if (com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size >
960             lo->ll_bitmap_size)
961                 nbits = com->lc_lfsck->li_ost_descs.ltd_tgts_bitmap->size;
962         else
963                 nbits = lo->ll_bitmap_size;
964
965         if (unlikely(nbits < BITS_PER_LONG))
966                 nbits = BITS_PER_LONG;
967
968         if (nbits > bitmap->size) {
969                 __u32 new_bits = bitmap->size;
970                 struct cfs_bitmap *new_bitmap;
971
972                 while (new_bits < nbits)
973                         new_bits <<= 1;
974
975                 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
976                 if (new_bitmap == NULL)
977                         RETURN(-ENOMEM);
978
979                 lad->lad_bitmap = new_bitmap;
980                 CFS_FREE_BITMAP(bitmap);
981                 bitmap = new_bitmap;
982         }
983
984         if (lo->ll_bitmap_size == 0) {
985                 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
986                 CFS_RESET_BITMAP(bitmap);
987
988                 RETURN(0);
989         }
990
991         size = (lo->ll_bitmap_size + 7) >> 3;
992         rc = dt_read(env, obj, lfsck_buf_get(env, bitmap->data, size), &pos);
993         if (rc != size)
994                 RETURN(rc >= 0 ? -EINVAL : rc);
995
996         if (cfs_bitmap_check_empty(bitmap))
997                 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
998         else
999                 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
1000
1001         RETURN(0);
1002 }
1003
1004 /**
1005  * Load the layout LFSCK trace file from disk.
1006  *
1007  * The layout LFSCK trace file records the layout LFSCK status information
1008  * and other statistics, such as how many objects have been scanned, and how
1009  * many objects have been repaired, and etc. It also contains the bitmap for
1010  * failed OSTs during the layout LFSCK. All these information will be loaded
1011  * from disk to RAM when the layout LFSCK component setup.
1012  *
1013  * \param[in] env       pointer to the thread context
1014  * \param[in] com       pointer to the lfsck component
1015  *
1016  * \retval              positive number for file data corruption, the caller
1017  *                      should reset the layout LFSCK trace file
1018  * \retval              0 for success
1019  * \retval              negative error number on failure
1020  */
1021 static int lfsck_layout_load(const struct lu_env *env,
1022                              struct lfsck_component *com)
1023 {
1024         struct lfsck_layout             *lo     = com->lc_file_ram;
1025         ssize_t                          size   = com->lc_file_size;
1026         loff_t                           pos    = 0;
1027         int                              rc;
1028
1029         rc = dt_read(env, com->lc_obj,
1030                      lfsck_buf_get(env, com->lc_file_disk, size), &pos);
1031         if (rc == 0) {
1032                 return -ENOENT;
1033         } else if (rc < 0) {
1034                 CDEBUG(D_LFSCK, "%s: failed to load lfsck_layout: rc = %d\n",
1035                        lfsck_lfsck2name(com->lc_lfsck), rc);
1036                 return rc;
1037         } else if (rc != size) {
1038                 CDEBUG(D_LFSCK, "%s: lfsck_layout size %u != %u; reset it\n",
1039                        lfsck_lfsck2name(com->lc_lfsck), rc, (unsigned int)size);
1040                 return 1;
1041         }
1042
1043         lfsck_layout_le_to_cpu(lo, com->lc_file_disk);
1044         if (lo->ll_magic != LFSCK_LAYOUT_MAGIC) {
1045                 CDEBUG(D_LFSCK, "%s: invalid lfsck_layout magic %#x != %#x, "
1046                        "to be reset\n", lfsck_lfsck2name(com->lc_lfsck),
1047                        lo->ll_magic, LFSCK_LAYOUT_MAGIC);
1048                 return 1;
1049         }
1050
1051         return 0;
1052 }
1053
1054 /**
1055  * Store the layout LFSCK trace file on disk.
1056  *
1057  * The layout LFSCK trace file records the layout LFSCK status information
1058  * and other statistics, such as how many objects have been scanned, and how
1059  * many objects have been repaired, and etc. It also contains the bitmap for
1060  * failed OSTs during the layout LFSCK. All these information will be synced
1061  * from RAM to disk periodically.
1062  *
1063  * \param[in] env       pointer to the thread context
1064  * \param[in] com       pointer to the lfsck component
1065  *
1066  * \retval              0 for success
1067  * \retval              negative error number on failure
1068  */
1069 static int lfsck_layout_store(const struct lu_env *env,
1070                               struct lfsck_component *com)
1071 {
1072         struct dt_object        *obj    = com->lc_obj;
1073         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1074         struct lfsck_layout     *lo_ram = com->lc_file_ram;
1075         struct lfsck_layout     *lo     = com->lc_file_disk;
1076         struct thandle          *th;
1077         struct dt_device        *dev    = lfsck_obj2dev(obj);
1078         struct cfs_bitmap       *bitmap = NULL;
1079         loff_t                   pos;
1080         ssize_t                  size   = com->lc_file_size;
1081         __u32                    nbits  = 0;
1082         int                      rc;
1083         ENTRY;
1084
1085         if (lfsck->li_master) {
1086                 struct lfsck_assistant_data *lad = com->lc_data;
1087
1088                 bitmap = lad->lad_bitmap;
1089                 nbits = bitmap->size;
1090
1091                 LASSERT(nbits > 0);
1092                 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
1093         }
1094
1095         lo_ram->ll_bitmap_size = nbits;
1096         lfsck_layout_cpu_to_le(lo, lo_ram);
1097         th = dt_trans_create(env, dev);
1098         if (IS_ERR(th))
1099                 GOTO(log, rc = PTR_ERR(th));
1100
1101         rc = dt_declare_record_write(env, obj, lfsck_buf_get(env, lo, size),
1102                                      (loff_t)0, th);
1103         if (rc != 0)
1104                 GOTO(out, rc);
1105
1106         if (bitmap != NULL) {
1107                 rc = dt_declare_record_write(env, obj,
1108                                 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1109                                 (loff_t)size, th);
1110                 if (rc != 0)
1111                         GOTO(out, rc);
1112         }
1113
1114         rc = dt_trans_start_local(env, dev, th);
1115         if (rc != 0)
1116                 GOTO(out, rc);
1117
1118         pos = 0;
1119         rc = dt_record_write(env, obj, lfsck_buf_get(env, lo, size), &pos, th);
1120         if (rc != 0)
1121                 GOTO(out, rc);
1122
1123         if (bitmap != NULL) {
1124                 pos = size;
1125                 rc = dt_record_write(env, obj,
1126                                 lfsck_buf_get(env, bitmap->data, nbits >> 3),
1127                                 &pos, th);
1128         }
1129
1130         GOTO(out, rc);
1131
1132 out:
1133         dt_trans_stop(env, dev, th);
1134
1135 log:
1136         if (rc != 0)
1137                 CDEBUG(D_LFSCK, "%s: fail to store lfsck_layout: rc = %d\n",
1138                        lfsck_lfsck2name(lfsck), rc);
1139
1140         return rc;
1141 }
1142
1143 static int lfsck_layout_init(const struct lu_env *env,
1144                              struct lfsck_component *com)
1145 {
1146         struct lfsck_layout *lo = com->lc_file_ram;
1147         int rc;
1148
1149         memset(lo, 0, com->lc_file_size);
1150         lo->ll_magic = LFSCK_LAYOUT_MAGIC;
1151         lo->ll_status = LS_INIT;
1152         down_write(&com->lc_sem);
1153         rc = lfsck_layout_store(env, com);
1154         if (rc == 0 && com->lc_lfsck->li_master)
1155                 rc = lfsck_load_sub_trace_files(env, com,
1156                         &dt_lfsck_layout_dangling_features, LFSCK_LAYOUT, true);
1157         up_write(&com->lc_sem);
1158
1159         return rc;
1160 }
1161
1162 static int fid_is_for_ostobj(const struct lu_env *env,
1163                              struct lfsck_instance *lfsck,
1164                              struct dt_object *obj, const struct lu_fid *fid)
1165 {
1166         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
1167         struct lu_seq_range     *range  = &lfsck_env_info(env)->lti_range;
1168         struct lustre_ost_attrs *loa;
1169         int                      rc;
1170
1171         fld_range_set_any(range);
1172         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1173         if (rc == 0) {
1174                 if (fld_range_is_ost(range))
1175                         return 1;
1176
1177                 return 0;
1178         }
1179
1180         loa = &lfsck_env_info(env)->lti_loa;
1181         rc = dt_xattr_get(env, obj, lfsck_buf_get(env, loa, sizeof(*loa)),
1182                           XATTR_NAME_LMA);
1183         if (rc >= (int)sizeof(struct lustre_mdt_attrs)) {
1184                 lustre_lma_swab(&loa->loa_lma);
1185
1186                 return loa->loa_lma.lma_compat & LMAC_FID_ON_OST ? 1 : 0;
1187         }
1188
1189         rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_FID);
1190
1191         return rc > 0;
1192 }
1193
1194 static struct lfsck_layout_seq *
1195 lfsck_layout_seq_lookup(struct lfsck_layout_slave_data *llsd, __u64 seq)
1196 {
1197         struct lfsck_layout_seq *lls;
1198
1199         list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1200                 if (lls->lls_seq == seq)
1201                         return lls;
1202
1203                 if (lls->lls_seq > seq)
1204                         return NULL;
1205         }
1206
1207         return NULL;
1208 }
1209
1210 static void
1211 lfsck_layout_seq_insert(struct lfsck_layout_slave_data *llsd,
1212                         struct lfsck_layout_seq *lls)
1213 {
1214         struct lfsck_layout_seq *tmp;
1215         struct list_head        *pos = &llsd->llsd_seq_list;
1216
1217         list_for_each_entry(tmp, &llsd->llsd_seq_list, lls_list) {
1218                 if (lls->lls_seq < tmp->lls_seq) {
1219                         pos = &tmp->lls_list;
1220                         break;
1221                 }
1222         }
1223         list_add_tail(&lls->lls_list, pos);
1224 }
1225
1226 static int
1227 lfsck_layout_lastid_create(const struct lu_env *env,
1228                            struct lfsck_instance *lfsck,
1229                            struct dt_object *obj)
1230 {
1231         struct lfsck_thread_info *info   = lfsck_env_info(env);
1232         struct lu_attr           *la     = &info->lti_la;
1233         struct dt_object_format  *dof    = &info->lti_dof;
1234         struct lfsck_bookmark    *bk     = &lfsck->li_bookmark_ram;
1235         struct dt_device         *dt     = lfsck_obj2dev(obj);
1236         struct thandle           *th;
1237         __u64                     lastid = 0;
1238         loff_t                    pos    = 0;
1239         int                       rc;
1240         ENTRY;
1241
1242         if (bk->lb_param & LPF_DRYRUN)
1243                 return 0;
1244
1245         memset(la, 0, sizeof(*la));
1246         la->la_mode = S_IFREG |  S_IRUGO | S_IWUSR;
1247         la->la_valid = LA_MODE | LA_UID | LA_GID;
1248         memset(dof, 0, sizeof(*dof));
1249         dof->dof_type = dt_mode_to_dft(S_IFREG);
1250
1251         th = lfsck_trans_create(env, dt, lfsck);
1252         if (IS_ERR(th))
1253                 GOTO(log, rc = PTR_ERR(th));
1254
1255         rc = dt_declare_create(env, obj, la, NULL, dof, th);
1256         if (rc != 0)
1257                 GOTO(stop, rc);
1258
1259         rc = dt_declare_record_write(env, obj,
1260                                      lfsck_buf_get(env, &lastid,
1261                                                    sizeof(lastid)),
1262                                      pos, th);
1263         if (rc != 0)
1264                 GOTO(stop, rc);
1265
1266         rc = dt_trans_start_local(env, dt, th);
1267         if (rc != 0)
1268                 GOTO(stop, rc);
1269
1270         dt_write_lock(env, obj, 0);
1271         if (likely(dt_object_exists(obj) == 0)) {
1272                 rc = dt_create(env, obj, la, NULL, dof, th);
1273                 if (rc == 0)
1274                         rc = dt_record_write(env, obj,
1275                                 lfsck_buf_get(env, &lastid, sizeof(lastid)),
1276                                 &pos, th);
1277         }
1278         dt_write_unlock(env, obj);
1279
1280         GOTO(stop, rc);
1281
1282 stop:
1283         dt_trans_stop(env, dt, th);
1284
1285 log:
1286         CDEBUG(D_LFSCK, "%s: layout LFSCK will create LAST_ID for <seq> "
1287                "%#llx: rc = %d\n",
1288                lfsck_lfsck2name(lfsck), fid_seq(lfsck_dto2fid(obj)), rc);
1289
1290         return rc;
1291 }
1292
1293 static int
1294 lfsck_layout_lastid_reload(const struct lu_env *env,
1295                            struct lfsck_component *com,
1296                            struct lfsck_layout_seq *lls)
1297 {
1298         __u64   lastid;
1299         loff_t  pos     = 0;
1300         int     rc;
1301
1302         dt_read_lock(env, lls->lls_lastid_obj, 0);
1303         rc = dt_record_read(env, lls->lls_lastid_obj,
1304                             lfsck_buf_get(env, &lastid, sizeof(lastid)), &pos);
1305         dt_read_unlock(env, lls->lls_lastid_obj);
1306         if (unlikely(rc != 0))
1307                 return rc;
1308
1309         lastid = le64_to_cpu(lastid);
1310         if (lastid < lls->lls_lastid_known) {
1311                 struct lfsck_instance   *lfsck  = com->lc_lfsck;
1312                 struct lfsck_layout     *lo     = com->lc_file_ram;
1313
1314                 lls->lls_lastid = lls->lls_lastid_known;
1315                 lls->lls_dirty = 1;
1316                 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1317                         LASSERT(lfsck->li_out_notify != NULL);
1318
1319                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1320                                              LE_LASTID_REBUILDING);
1321                         lo->ll_flags |= LF_CRASHED_LASTID;
1322
1323                         CDEBUG(D_LFSCK, "%s: layout LFSCK finds crashed "
1324                                "LAST_ID file (1) for the sequence %#llx"
1325                                ", old value %llu, known value %llu\n",
1326                                lfsck_lfsck2name(lfsck), lls->lls_seq,
1327                                lastid, lls->lls_lastid);
1328                 }
1329         } else if (lastid >= lls->lls_lastid) {
1330                 lls->lls_lastid = lastid;
1331                 lls->lls_dirty = 0;
1332         }
1333
1334         return 0;
1335 }
1336
1337 static int
1338 lfsck_layout_lastid_store(const struct lu_env *env,
1339                           struct lfsck_component *com)
1340 {
1341         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1342         struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
1343         struct dt_device                *dt     = lfsck->li_bottom;
1344         struct lfsck_layout_slave_data  *llsd   = com->lc_data;
1345         struct lfsck_layout_seq         *lls;
1346         struct thandle                  *th;
1347         __u64                            lastid;
1348         int                              rc     = 0;
1349         int                              rc1    = 0;
1350
1351         list_for_each_entry(lls, &llsd->llsd_seq_list, lls_list) {
1352                 loff_t pos = 0;
1353
1354                 if (!lls->lls_dirty)
1355                         continue;
1356
1357                 CDEBUG(D_LFSCK, "%s: layout LFSCK will sync the LAST_ID for "
1358                        "<seq> %#llx as <oid> %llu\n",
1359                        lfsck_lfsck2name(lfsck), lls->lls_seq, lls->lls_lastid);
1360
1361                 if (bk->lb_param & LPF_DRYRUN) {
1362                         lls->lls_dirty = 0;
1363                         continue;
1364                 }
1365
1366                 th = lfsck_trans_create(env, dt, lfsck);
1367                 if (IS_ERR(th)) {
1368                         rc1 = PTR_ERR(th);
1369                         CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1370                                "the LAST_ID for <seq> %#llx(1): rc = %d\n",
1371                                lfsck_lfsck2name(com->lc_lfsck),
1372                                lls->lls_seq, rc1);
1373                         continue;
1374                 }
1375
1376                 lastid = cpu_to_le64(lls->lls_lastid);
1377                 rc = dt_declare_record_write(env, lls->lls_lastid_obj,
1378                                              lfsck_buf_get(env, &lastid,
1379                                                            sizeof(lastid)),
1380                                              pos, th);
1381                 if (rc != 0)
1382                         goto stop;
1383
1384                 rc = dt_trans_start_local(env, dt, th);
1385                 if (rc != 0)
1386                         goto stop;
1387
1388                 dt_write_lock(env, lls->lls_lastid_obj, 0);
1389                 rc = dt_record_write(env, lls->lls_lastid_obj,
1390                                      lfsck_buf_get(env, &lastid,
1391                                      sizeof(lastid)), &pos, th);
1392                 dt_write_unlock(env, lls->lls_lastid_obj);
1393                 if (rc == 0)
1394                         lls->lls_dirty = 0;
1395
1396 stop:
1397                 dt_trans_stop(env, dt, th);
1398                 if (rc != 0) {
1399                         rc1 = rc;
1400                         CDEBUG(D_LFSCK, "%s: layout LFSCK failed to store "
1401                                "the LAST_ID for <seq> %#llx(2): rc = %d\n",
1402                                lfsck_lfsck2name(com->lc_lfsck),
1403                                lls->lls_seq, rc1);
1404                 }
1405         }
1406
1407         return rc1;
1408 }
1409
1410 static int
1411 lfsck_layout_lastid_load(const struct lu_env *env,
1412                          struct lfsck_component *com,
1413                          struct lfsck_layout_seq *lls)
1414 {
1415         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1416         struct lfsck_layout     *lo     = com->lc_file_ram;
1417         struct lu_fid           *fid    = &lfsck_env_info(env)->lti_fid;
1418         struct dt_object        *obj;
1419         loff_t                   pos    = 0;
1420         int                      rc;
1421         ENTRY;
1422
1423         lu_last_id_fid(fid, lls->lls_seq, lfsck_dev_idx(lfsck));
1424         obj = dt_locate(env, lfsck->li_bottom, fid);
1425         if (IS_ERR(obj))
1426                 RETURN(PTR_ERR(obj));
1427
1428         /* LAST_ID crashed, to be rebuilt */
1429         if (dt_object_exists(obj) == 0) {
1430                 if (!(lo->ll_flags & LF_CRASHED_LASTID)) {
1431                         LASSERT(lfsck->li_out_notify != NULL);
1432
1433                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1434                                              LE_LASTID_REBUILDING);
1435                         lo->ll_flags |= LF_CRASHED_LASTID;
1436
1437                         CDEBUG(D_LFSCK, "%s: layout LFSCK cannot find the "
1438                                "LAST_ID file for sequence %#llx\n",
1439                                lfsck_lfsck2name(lfsck), lls->lls_seq);
1440
1441                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY4) &&
1442                             cfs_fail_val > 0) {
1443                                 struct ptlrpc_thread *thread =
1444                                         &lfsck->li_thread;
1445
1446                                 up_write(&com->lc_sem);
1447                                 wait_event_idle_timeout(
1448                                         thread->t_ctl_waitq,
1449                                         !thread_is_running(thread),
1450                                         cfs_time_seconds(cfs_fail_val));
1451                                 down_write(&com->lc_sem);
1452                         }
1453                 }
1454
1455                 rc = lfsck_layout_lastid_create(env, lfsck, obj);
1456         } else {
1457                 dt_read_lock(env, obj, 0);
1458                 rc = dt_read(env, obj,
1459                         lfsck_buf_get(env, &lls->lls_lastid, sizeof(__u64)),
1460                         &pos);
1461                 dt_read_unlock(env, obj);
1462                 if (rc != 0 && rc != sizeof(__u64))
1463                         GOTO(out, rc = (rc > 0 ? -EFAULT : rc));
1464
1465                 if (rc == 0 && !(lo->ll_flags & LF_CRASHED_LASTID)) {
1466                         LASSERT(lfsck->li_out_notify != NULL);
1467
1468                         lfsck->li_out_notify(env, lfsck->li_out_notify_data,
1469                                              LE_LASTID_REBUILDING);
1470                         lo->ll_flags |= LF_CRASHED_LASTID;
1471
1472                         CDEBUG(D_LFSCK, "%s: layout LFSCK finds invalid "
1473                                "LAST_ID file for the sequence %#llx"
1474                                ": rc = %d\n",
1475                                lfsck_lfsck2name(lfsck), lls->lls_seq, rc);
1476                 }
1477
1478                 lls->lls_lastid = le64_to_cpu(lls->lls_lastid);
1479                 rc = 0;
1480         }
1481
1482         GOTO(out, rc);
1483
1484 out:
1485         if (rc != 0)
1486                 lfsck_object_put(env, obj);
1487         else
1488                 lls->lls_lastid_obj = obj;
1489
1490         return rc;
1491 }
1492
1493 static void lfsck_layout_record_failure(const struct lu_env *env,
1494                                         struct lfsck_instance *lfsck,
1495                                         struct lfsck_layout *lo)
1496 {
1497         __u64 cookie;
1498
1499         lo->ll_objs_failed_phase1++;
1500         cookie = lfsck->li_obj_oit->do_index_ops->dio_it.store(env,
1501                                                         lfsck->li_di_oit);
1502         if (lo->ll_pos_first_inconsistent == 0 ||
1503             lo->ll_pos_first_inconsistent < cookie) {
1504                 lo->ll_pos_first_inconsistent = cookie;
1505
1506                 CDEBUG(D_LFSCK, "%s: layout LFSCK hit first non-repaired "
1507                        "inconsistency at the pos [%llu]\n",
1508                        lfsck_lfsck2name(lfsck),
1509                        lo->ll_pos_first_inconsistent);
1510         }
1511 }
1512
1513 static int lfsck_layout_double_scan_result(const struct lu_env *env,
1514                                            struct lfsck_component *com,
1515                                            int rc)
1516 {
1517         struct lfsck_instance   *lfsck = com->lc_lfsck;
1518         struct lfsck_layout     *lo    = com->lc_file_ram;
1519
1520         CDEBUG(D_LFSCK, "%s: layout LFSCK double scan: rc = %d\n",
1521                lfsck_lfsck2name(lfsck), rc);
1522
1523         down_write(&com->lc_sem);
1524         lo->ll_run_time_phase2 += ktime_get_seconds() -
1525                                   com->lc_time_last_checkpoint;
1526         lo->ll_time_last_checkpoint = ktime_get_real_seconds();
1527         lo->ll_objs_checked_phase2 += com->lc_new_checked;
1528
1529         if (rc > 0) {
1530                 if (lo->ll_flags & LF_INCOMPLETE) {
1531                         lo->ll_status = LS_PARTIAL;
1532                 } else {
1533                         if (lfsck->li_master) {
1534                                 struct lfsck_assistant_data *lad = com->lc_data;
1535
1536                                 if (test_bit(LAD_INCOMPLETE, &lad->lad_flags))
1537                                         lo->ll_status = LS_PARTIAL;
1538                                 else
1539                                         lo->ll_status = LS_COMPLETED;
1540                         } else {
1541                                 lo->ll_status = LS_COMPLETED;
1542                         }
1543                 }
1544                 lo->ll_flags &= ~LF_SCANNED_ONCE;
1545                 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
1546                         lo->ll_flags &= ~LF_INCONSISTENT;
1547                 lo->ll_time_last_complete = lo->ll_time_last_checkpoint;
1548                 lo->ll_success_count++;
1549         } else if (rc == 0) {
1550                 if (lfsck->li_status != 0)
1551                         lo->ll_status = lfsck->li_status;
1552                 else
1553                         lo->ll_status = LS_STOPPED;
1554         } else {
1555                 lo->ll_status = LS_FAILED;
1556         }
1557
1558         rc = lfsck_layout_store(env, com);
1559         up_write(&com->lc_sem);
1560
1561         CDEBUG(D_LFSCK, "%s: layout LFSCK double scan result %u: rc = %d\n",
1562                lfsck_lfsck2name(lfsck), lo->ll_status, rc);
1563
1564         return rc;
1565 }
1566
1567 static int lfsck_layout_trans_stop(const struct lu_env *env,
1568                                    struct dt_device *dev,
1569                                    struct thandle *handle, int result)
1570 {
1571         int rc;
1572
1573         /* XXX: If there is something worng or it needs to repair nothing,
1574          *      then notify the lower to stop the modification. Currently,
1575          *      we use th_result for such purpose, that may be replaced by
1576          *      some rollback mechanism in the future. */
1577         handle->th_result = result;
1578         rc = dt_trans_stop(env, dev, handle);
1579         if (result != 0)
1580                 return result > 0 ? 0 : result;
1581
1582         return rc == 0 ? 1 : rc;
1583 }
1584
1585 static int lfsck_layout_ins_dangling_rec(const struct lu_env *env,
1586                                          struct lfsck_component *com,
1587                                          const struct lu_fid *pfid,
1588                                          const struct lu_fid *cfid,
1589                                          __u32 comp_id, __u32 ea_off,
1590                                          __u32 ost_idx)
1591 {
1592         struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1593         struct lu_fid *rec = &lfsck_env_info(env)->lti_fid3;
1594         struct dt_device *dev;
1595         struct dt_object *obj;
1596         struct thandle *th = NULL;
1597         int idx;
1598         int rc = 0;
1599         ENTRY;
1600
1601         idx = lfsck_sub_trace_file_fid2idx(pfid);
1602         obj = com->lc_sub_trace_objs[idx].lsto_obj;
1603         dev = lfsck_obj2dev(obj);
1604
1605         fid_cpu_to_be(&key->lldk_fid, pfid);
1606         key->lldk_comp_id = cpu_to_be32(comp_id);
1607         key->lldk_ea_off = cpu_to_be32(ea_off);
1608
1609         fid_cpu_to_be(rec, cfid);
1610         rec->f_ver = cpu_to_be32(ost_idx);
1611
1612         mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1613
1614         th = lfsck_trans_create(env, dev, com->lc_lfsck);
1615         if (IS_ERR(th))
1616                 GOTO(unlock, rc = PTR_ERR(th));
1617
1618         rc = dt_declare_insert(env, obj,
1619                                (const struct dt_rec *)rec,
1620                                (const struct dt_key *)key, th);
1621         if (rc)
1622                 GOTO(unlock, rc);
1623
1624         rc = dt_trans_start_local(env, dev, th);
1625         if (rc)
1626                 GOTO(unlock, rc);
1627
1628         rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1629                        (const struct dt_key *)key, th);
1630
1631         GOTO(unlock, rc);
1632
1633 unlock:
1634         if (th && !IS_ERR(th))
1635                 dt_trans_stop(env, dev, th);
1636
1637         mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1638
1639         CDEBUG(D_LFSCK, "%s: insert the paris "DFID" => "DFID", comp_id = %u, "
1640                "ea_off = %u, ost_idx = %u, into the trace file for further "
1641                "dangling check: rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1642                PFID(pfid), PFID(cfid), comp_id, ea_off, ost_idx, rc);
1643
1644         return rc;
1645 }
1646
1647 static int lfsck_layout_del_dangling_rec(const struct lu_env *env,
1648                                          struct lfsck_component *com,
1649                                          const struct lu_fid *fid,
1650                                          __u32 comp_id, __u32 ea_off)
1651 {
1652         struct lfsck_layout_dangling_key *key = &lfsck_env_info(env)->lti_lldk;
1653         struct dt_device *dev;
1654         struct dt_object *obj;
1655         struct thandle *th = NULL;
1656         int idx;
1657         int rc = 0;
1658         ENTRY;
1659
1660         idx = lfsck_sub_trace_file_fid2idx(fid);
1661         obj = com->lc_sub_trace_objs[idx].lsto_obj;
1662         dev = lfsck_obj2dev(obj);
1663
1664         fid_cpu_to_be(&key->lldk_fid, fid);
1665         key->lldk_comp_id = cpu_to_be32(comp_id);
1666         key->lldk_ea_off = cpu_to_be32(ea_off);
1667
1668         mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1669
1670         th = lfsck_trans_create(env, dev, com->lc_lfsck);
1671         if (IS_ERR(th))
1672                 GOTO(unlock, rc = PTR_ERR(th));
1673
1674         rc = dt_declare_delete(env, obj, (const struct dt_key *)key, th);
1675         if (rc)
1676                 GOTO(unlock, rc);
1677
1678         rc = dt_trans_start_local(env, dev, th);
1679         if (rc)
1680                 GOTO(unlock, rc);
1681
1682         rc = dt_delete(env, obj, (const struct dt_key *)key, th);
1683
1684         GOTO(unlock, rc);
1685
1686 unlock:
1687         if (th && !IS_ERR(th))
1688                 dt_trans_stop(env, dev, th);
1689
1690         mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
1691
1692         CDEBUG(D_LFSCK, "%s: delete the dangling record for "DFID
1693                ", comp_id = %u, ea_off = %u from the trace file: rc = %d\n",
1694                lfsck_lfsck2name(com->lc_lfsck), PFID(fid), comp_id, ea_off, rc);
1695
1696         return rc;
1697 }
1698
1699 /**
1700  * Get the system default stripe size.
1701  *
1702  * \param[in] env       pointer to the thread context
1703  * \param[in] lfsck     pointer to the lfsck instance
1704  * \param[out] size     pointer to the default stripe size
1705  *
1706  * \retval              0 for success
1707  * \retval              negative error number on failure
1708  */
1709 static int lfsck_layout_get_def_stripesize(const struct lu_env *env,
1710                                            struct lfsck_instance *lfsck,
1711                                            __u32 *size)
1712 {
1713         struct lov_user_md      *lum = &lfsck_env_info(env)->lti_lum;
1714         struct dt_object        *root;
1715         int                      rc;
1716
1717         root = dt_locate(env, lfsck->li_next, &lfsck->li_local_root_fid);
1718         if (IS_ERR(root))
1719                 return PTR_ERR(root);
1720
1721         /* Get the default stripe size via xattr_get on the backend root. */
1722         rc = dt_xattr_get(env, root, lfsck_buf_get(env, lum, sizeof(*lum)),
1723                           XATTR_NAME_LOV);
1724         if (rc > 0) {
1725                 /* The lum->lmm_stripe_size is LE mode. The *size also
1726                  * should be LE mode. So it is unnecessary to convert. */
1727                 *size = lum->lmm_stripe_size;
1728                 rc = 0;
1729         } else if (unlikely(rc == 0)) {
1730                 rc = -EINVAL;
1731         }
1732
1733         lfsck_object_put(env, root);
1734
1735         return rc;
1736 }
1737
1738 /**
1739  * \retval       +1: repaired
1740  * \retval        0: did nothing
1741  * \retval      -ve: on error
1742  */
1743 static int lfsck_layout_refill_lovea(const struct lu_env *env,
1744                                      struct lfsck_instance *lfsck,
1745                                      struct thandle *handle,
1746                                      struct dt_object *parent,
1747                                      const struct lu_fid *cfid,
1748                                      struct lu_buf *buf,
1749                                      struct lov_mds_md_v1 *lmm,
1750                                      struct lov_ost_data_v1 *slot,
1751                                      int fl, __u32 ost_idx, int size)
1752 {
1753         struct ost_id           *oi     = &lfsck_env_info(env)->lti_oi;
1754         struct lu_buf            ea_buf;
1755         int                      rc;
1756         __u32                    magic;
1757         __u32                    pattern;
1758         __u16                    count;
1759         ENTRY;
1760
1761         magic = le32_to_cpu(lmm->lmm_magic);
1762         pattern = le32_to_cpu(lmm->lmm_pattern);
1763         count = le16_to_cpu(lmm->lmm_stripe_count);
1764
1765         fid_to_ostid(cfid, oi);
1766         ostid_cpu_to_le(oi, &slot->l_ost_oi);
1767         slot->l_ost_gen = cpu_to_le32(0);
1768         slot->l_ost_idx = cpu_to_le32(ost_idx);
1769
1770         if (pattern & LOV_PATTERN_F_HOLE) {
1771                 struct lov_ost_data_v1 *objs;
1772                 int                     i;
1773
1774                 if (magic == LOV_MAGIC_V1)
1775                         objs = &lmm->lmm_objects[0];
1776                 else
1777                         objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
1778                 for (i = 0; i < count; i++, objs++) {
1779                         if (lovea_slot_is_dummy(objs))
1780                                 break;
1781                 }
1782
1783                 /* If the @slot is the last dummy slot to be refilled,
1784                  * then drop LOV_PATTERN_F_HOLE from lmm::lmm_pattern. */
1785                 if (i == count) {
1786                         lmm->lmm_pattern =
1787                                 cpu_to_le32(pattern & ~LOV_PATTERN_F_HOLE);
1788
1789                         CDEBUG(D_LFSCK, "%s: remove layout HOLE for "DFID
1790                                ": parent "DFID"\n", lfsck_lfsck2name(lfsck),
1791                                PFID(cfid), PFID(lfsck_dto2fid(parent)));
1792                 }
1793         }
1794
1795         lfsck_buf_init(&ea_buf, buf->lb_buf, size);
1796         rc = dt_xattr_set(env, parent, &ea_buf, XATTR_NAME_LOV, fl, handle);
1797         if (rc == 0)
1798                 rc = 1;
1799
1800         RETURN(rc);
1801 }
1802
1803 static struct lov_ost_data_v1 *
1804 __lfsck_layout_new_v1_lovea(struct lov_mds_md_v1 *lmm,
1805                             const struct lu_fid *pfid,
1806                             __u32 stripe_size, __u32 ea_off,
1807                             __u32 pattern, __u16 count)
1808 {
1809         lmm->lmm_magic = cpu_to_le32(LOV_MAGIC_V1);
1810         lmm->lmm_pattern = cpu_to_le32(pattern);
1811         fid_to_lmm_oi(pfid, &lmm->lmm_oi);
1812         lmm_oi_cpu_to_le(&lmm->lmm_oi, &lmm->lmm_oi);
1813         lmm->lmm_stripe_size = cpu_to_le32(stripe_size);
1814         lmm->lmm_stripe_count = cpu_to_le16(count);
1815         lmm->lmm_layout_gen = cpu_to_le16(1);
1816         memset(&lmm->lmm_objects[0], 0,
1817                sizeof(struct lov_ost_data_v1) * count);
1818
1819         return &lmm->lmm_objects[ea_off];
1820 }
1821
1822 static int lfsck_layout_new_v1_lovea(const struct lu_env *env,
1823                                      struct lfsck_instance *lfsck,
1824                                      struct ost_layout *ol,
1825                                      struct dt_object *parent,
1826                                      struct lu_buf *buf, __u32 ea_off,
1827                                      struct lov_mds_md_v1 **lmm,
1828                                      struct lov_ost_data_v1 **objs)
1829 {
1830         int size;
1831         __u32 stripe_size = ol->ol_stripe_size;
1832         __u32 pattern = LOV_PATTERN_RAID0;
1833         __u16 count;
1834
1835         if (ol->ol_stripe_count != 0)
1836                 count = ol->ol_stripe_count;
1837         else
1838                 count = ea_off + 1;
1839
1840         size = lov_mds_md_size(count, LOV_MAGIC_V1);
1841         LASSERTF(buf->lb_len >= size,
1842                  "buffer len %d is less than real size %d\n",
1843                  (int)buf->lb_len, size);
1844
1845         if (stripe_size == 0) {
1846                 int rc;
1847
1848                 rc = lfsck_layout_get_def_stripesize(env, lfsck, &stripe_size);
1849                 if (rc)
1850                         return rc;
1851         }
1852
1853         *lmm = buf->lb_buf;
1854         if (ol->ol_stripe_count > 1 ||
1855             (ol->ol_stripe_count == 0 && ea_off != 0)) {
1856                 pattern |= LOV_PATTERN_F_HOLE;
1857                 memset(&(*lmm)->lmm_objects[0], 0,
1858                        count * sizeof(struct lov_ost_data_v1));
1859         }
1860
1861         *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1862                                 stripe_size, ea_off, pattern, count);
1863
1864         return size;
1865 }
1866
1867 static int lfsck_layout_new_comp_lovea(const struct lu_env *env,
1868                                        struct lu_orphan_rec_v3 *rec,
1869                                        struct dt_object *parent,
1870                                        struct lu_buf *buf, __u32 ea_off,
1871                                        struct lov_mds_md_v1 **lmm,
1872                                        struct lov_ost_data_v1 **objs)
1873 {
1874         struct ost_layout *ol = &rec->lor_layout;
1875         struct lov_comp_md_v1 *lcm;
1876         struct lov_comp_md_entry_v1 *lcme;
1877         __u32 pattern = LOV_PATTERN_RAID0;
1878         __u32 offset = sizeof(*lcm) + sizeof(*lcme);
1879         int lcme_size = lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1880         int size = offset + lcme_size;
1881
1882         LASSERTF(buf->lb_len >= size,
1883                  "buffer len %d is less than real size %d\n",
1884                  (int)buf->lb_len, size);
1885
1886         lcm = buf->lb_buf;
1887         lcm->lcm_magic = cpu_to_le32(LOV_MAGIC_COMP_V1);
1888         lcm->lcm_size = cpu_to_le32(size);
1889         if (rec->lor_range) {
1890                 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1891                                                   rec->lor_range);
1892                 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1893         } else if (rec->lor_layout_version) {
1894                 lcm->lcm_layout_gen = cpu_to_le32(rec->lor_layout_version +
1895                                                   rec->lor_range);
1896                 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1897         } else {
1898                 lcm->lcm_layout_gen = cpu_to_le32(1);
1899                 lcm->lcm_flags = cpu_to_le16(LCM_FL_NONE);
1900         }
1901         lcm->lcm_entry_count = cpu_to_le16(1);
1902         /* Currently, we do not know how many mirrors will be, set it as zero
1903          * at the beginning. It will be updated when more mirrors are found. */
1904         lcm->lcm_mirror_count = 0;
1905
1906         lcme = &lcm->lcm_entries[0];
1907         lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
1908         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
1909         lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
1910         lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
1911         lcme->lcme_offset = cpu_to_le32(offset);
1912         lcme->lcme_size = cpu_to_le32(lcme_size);
1913         lcme->lcme_layout_gen = lcm->lcm_layout_gen;
1914         if (ol->ol_stripe_count > 1)
1915                 pattern |= LOV_PATTERN_F_HOLE;
1916
1917         *lmm = buf->lb_buf + offset;
1918         *objs = __lfsck_layout_new_v1_lovea(*lmm, lfsck_dto2fid(parent),
1919                                             ol->ol_stripe_size, ea_off,
1920                                             pattern, ol->ol_stripe_count);
1921
1922         return size;
1923 }
1924
1925 static void lfsck_layout_update_lcm(struct lov_comp_md_v1 *lcm,
1926                                     struct lov_comp_md_entry_v1 *lcme,
1927                                     __u32 version, __u32 range)
1928 {
1929         struct lov_comp_md_entry_v1 *tmp;
1930         __u64 start = le64_to_cpu(lcme->lcme_extent.e_start);
1931         __u64 end = le64_to_cpu(lcme->lcme_extent.e_end);
1932         __u32 gen = version + range;
1933         __u32 tmp_gen;
1934         int i;
1935         __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1936         __u16 flags = le16_to_cpu(lcm->lcm_flags);
1937
1938         if (!gen)
1939                 gen = 1;
1940         lcme->lcme_layout_gen = cpu_to_le32(gen);
1941         if (le32_to_cpu(lcm->lcm_layout_gen) < gen)
1942                 lcm->lcm_layout_gen = cpu_to_le32(gen);
1943
1944         if (range)
1945                 lcm->lcm_flags = cpu_to_le16(LCM_FL_WRITE_PENDING);
1946         else if (flags == LCM_FL_NONE && le16_to_cpu(lcm->lcm_mirror_count) > 0)
1947                 lcm->lcm_flags = cpu_to_le16(LCM_FL_RDONLY);
1948
1949         for (i = 0; i < count; i++) {
1950                 tmp = &lcm->lcm_entries[i];
1951                 if (le64_to_cpu(tmp->lcme_extent.e_end) <= start)
1952                         continue;
1953
1954                 if (le64_to_cpu(tmp->lcme_extent.e_start) >= end)
1955                         continue;
1956
1957                 if (le32_to_cpu(tmp->lcme_flags) & LCME_FL_STALE)
1958                         continue;
1959
1960                 tmp_gen = le32_to_cpu(tmp->lcme_layout_gen);
1961                 /* "lcme_layout_gen == 0" but without LCME_FL_STALE flag,
1962                  * then it should be the latest version of all mirrors. */
1963                 if (tmp_gen == 0 || tmp_gen > gen) {
1964                         lcme->lcme_flags = cpu_to_le32(
1965                                 le32_to_cpu(lcme->lcme_flags) | LCME_FL_STALE);
1966                         break;
1967                 }
1968
1969                 if (tmp_gen < gen)
1970                         tmp->lcme_flags = cpu_to_le32(
1971                                 le32_to_cpu(tmp->lcme_flags) | LCME_FL_STALE);
1972         }
1973 }
1974
1975 static int lfsck_layout_add_comp(const struct lu_env *env,
1976                                  struct lfsck_instance *lfsck,
1977                                  struct thandle *handle,
1978                                  struct lu_orphan_rec_v3 *rec,
1979                                  struct dt_object *parent,
1980                                  const struct lu_fid *cfid,
1981                                  struct lu_buf *buf, __u32 ost_idx,
1982                                  __u32 ea_off, int pos, bool new_mirror)
1983 {
1984         struct ost_layout *ol = &rec->lor_layout;
1985         struct lov_comp_md_v1 *lcm = buf->lb_buf;
1986         struct lov_comp_md_entry_v1 *lcme;
1987         struct lov_mds_md_v1 *lmm;
1988         struct lov_ost_data_v1 *objs;
1989         int added = sizeof(*lcme) +
1990                     lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
1991         int size = le32_to_cpu(lcm->lcm_size) + added;
1992         int rc;
1993         int i;
1994         __u32 offset;
1995         __u32 pattern = LOV_PATTERN_RAID0;
1996         __u16 count = le16_to_cpu(lcm->lcm_entry_count);
1997         ENTRY;
1998
1999         lu_buf_check_and_grow(buf, size);
2000         /* set the lcm again because lu_buf_check_and_grow() may
2001          * have reallocated the buf. */
2002         lcm = buf->lb_buf;
2003         lcm->lcm_size = cpu_to_le32(size);
2004         lcm->lcm_entry_count = cpu_to_le16(count + 1);
2005         if (new_mirror)
2006                 le16_add_cpu(&lcm->lcm_mirror_count, 1);
2007
2008         /* 1. Move the component bodies from [pos, count-1] to [pos+1, count]
2009          *    with distance of 'added'. */
2010         if (pos < count) {
2011                 size = 0;
2012                 for (i = pos; i < count; i++) {
2013                         lcme = &lcm->lcm_entries[i];
2014                         size += le32_to_cpu(lcme->lcme_size);
2015                 }
2016
2017                 offset = le32_to_cpu(lcm->lcm_entries[pos].lcme_offset);
2018                 memmove(buf->lb_buf + offset + added,
2019                         buf->lb_buf + offset, size);
2020         }
2021
2022         size = 0;
2023         /* 2. Move the component header [0, pos-1] to [0, pos-1] with distance
2024          *    of 'sizeof(struct lov_comp_md_entry_v1)' */
2025         if (pos > 0) {
2026                 for (i = 0; i < pos; i++) {
2027                         lcme = &lcm->lcm_entries[i];
2028                         size += le32_to_cpu(lcme->lcme_size);
2029                 }
2030
2031                 offset = le32_to_cpu(lcm->lcm_entries[0].lcme_offset);
2032                 memmove(buf->lb_buf + offset + sizeof(*lcme),
2033                         buf->lb_buf + offset, size);
2034         }
2035
2036         /* 3. Recalculate the enter offset for the component [pos, count-1] */
2037         for (i = count - 1; i >= pos; i--) {
2038                 lcm->lcm_entries[i + 1] = lcm->lcm_entries[i];
2039                 lcm->lcm_entries[i + 1].lcme_offset =
2040                         cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i + 1].
2041                                                 lcme_offset) + added);
2042         }
2043
2044         /* 4. Recalculate the enter offset for the component [0, pos) */
2045         for (i = 0; i < pos; i++) {
2046                 lcm->lcm_entries[i].lcme_offset =
2047                         cpu_to_le32(le32_to_cpu(lcm->lcm_entries[i].
2048                                                 lcme_offset) + sizeof(*lcme));
2049         }
2050
2051         offset = sizeof(*lcm) + sizeof(*lcme) * (count + 1) + size;
2052         /* 4. Insert the new component header (entry) at the slot 'pos'. */
2053         lcme = &lcm->lcm_entries[pos];
2054         lcme->lcme_id = cpu_to_le32(ol->ol_comp_id);
2055         lcme->lcme_flags = cpu_to_le32(LCME_FL_INIT);
2056         lcme->lcme_extent.e_start = cpu_to_le64(ol->ol_comp_start);
2057         lcme->lcme_extent.e_end = cpu_to_le64(ol->ol_comp_end);
2058         lcme->lcme_offset = cpu_to_le32(offset);
2059         lcme->lcme_size = cpu_to_le32(lov_mds_md_size(ol->ol_stripe_count,
2060                                                       LOV_MAGIC_V1));
2061
2062         if (ol->ol_stripe_count > 1)
2063                 pattern |= LOV_PATTERN_F_HOLE;
2064
2065         lmm = buf->lb_buf + offset;
2066         /* 5. Insert teh new component body at the 'offset'. */
2067         objs = __lfsck_layout_new_v1_lovea(lmm, lfsck_dto2fid(parent),
2068                                            ol->ol_stripe_size, ea_off,
2069                                            pattern, ol->ol_stripe_count);
2070
2071         /* 6. Update mirror related flags and version. */
2072         lfsck_layout_update_lcm(lcm, lcme, rec->lor_layout_version,
2073                                 rec->lor_range);
2074
2075         rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2076                                        lmm, objs, LU_XATTR_REPLACE, ost_idx,
2077                                        le32_to_cpu(lcm->lcm_size));
2078
2079         CDEBUG(D_LFSCK, "%s: layout LFSCK assistant add new COMP for "
2080                DFID": parent "DFID", OST-index %u, stripe-index %u, "
2081                "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2082                "comp_end %llu, layout version %u, range %u, "
2083                "%s LOV EA hole: rc = %d\n",
2084                lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2085                ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2086                ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2087                rec->lor_layout_version, rec->lor_range,
2088                le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2089                "with" : "without", rc);
2090
2091         RETURN(rc);
2092 }
2093
2094 static int lfsck_layout_extend_v1v3_lovea(const struct lu_env *env,
2095                                           struct lfsck_instance *lfsck,
2096                                           struct thandle *handle,
2097                                           struct ost_layout *ol,
2098                                           struct dt_object *parent,
2099                                           const struct lu_fid *cfid,
2100                                           struct lu_buf *buf, __u32 ost_idx,
2101                                           __u32 ea_off)
2102 {
2103         struct lov_mds_md_v1 *lmm = buf->lb_buf;
2104         struct lov_ost_data_v1 *objs;
2105         __u16 count = le16_to_cpu(lmm->lmm_stripe_count);
2106         __u32 magic = le32_to_cpu(lmm->lmm_magic);
2107         int size;
2108         int gap;
2109         int rc;
2110         ENTRY;
2111
2112         /* The original LOVEA maybe re-generated via old filter_fid, at
2113          * that time, we do not know the stripe count and stripe size. */
2114         if (ol->ol_stripe_count > count)
2115                 count = ol->ol_stripe_count;
2116         if (ol->ol_stripe_size != 0 &&
2117             ol->ol_stripe_size != le32_to_cpu(lmm->lmm_stripe_size))
2118                 lmm->lmm_stripe_size = cpu_to_le32(ol->ol_stripe_size);
2119
2120         if (magic == LOV_MAGIC_V1)
2121                 objs = &lmm->lmm_objects[count];
2122         else
2123                 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[count];
2124
2125         gap = ea_off - count;
2126         if (gap >= 0)
2127                 count = ea_off + 1;
2128
2129         size = lov_mds_md_size(count, magic);
2130         LASSERTF(buf->lb_len >= size,
2131                  "buffer len %d is less than real size %d\n",
2132                  (int)buf->lb_len, size);
2133
2134         if (gap > 0) {
2135                 memset(objs, 0, gap * sizeof(*objs));
2136                 lmm->lmm_pattern |= cpu_to_le32(LOV_PATTERN_F_HOLE);
2137         }
2138
2139         lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2140         lmm->lmm_stripe_count = cpu_to_le16(count);
2141         objs += gap;
2142
2143         rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid, buf,
2144                                 lmm, objs, LU_XATTR_REPLACE, ost_idx, size);
2145
2146         CDEBUG(D_LFSCK, "%s: layout LFSCK assistant extend layout EA for "
2147                DFID": parent "DFID", OST-index %u, stripe-index %u, "
2148                "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2149                "comp_end %llu, %s LOV EA hole: rc = %d\n",
2150                lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2151                ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2152                ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2153                le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2154                "with" : "without", rc);
2155
2156         RETURN(rc);
2157 }
2158
2159 /**
2160  * \retval       +1: repaired
2161  * \retval        0: did nothing
2162  * \retval      -ve: on error
2163  */
2164 static int lfsck_layout_update_lovea(const struct lu_env *env,
2165                                      struct lfsck_instance *lfsck,
2166                                      struct thandle *handle,
2167                                      struct lu_orphan_rec_v3 *rec,
2168                                      struct dt_object *parent,
2169                                      const struct lu_fid *cfid,
2170                                      struct lu_buf *buf, int fl,
2171                                      __u32 ost_idx, __u32 ea_off)
2172 {
2173         struct ost_layout *ol = &rec->lor_layout;
2174         struct lov_mds_md_v1 *lmm = NULL;
2175         struct lov_ost_data_v1 *objs = NULL;
2176         int rc = 0;
2177         ENTRY;
2178
2179         if (ol->ol_comp_id != 0)
2180                 rc = lfsck_layout_new_comp_lovea(env, rec, parent, buf, ea_off,
2181                                                  &lmm, &objs);
2182         else
2183                 rc = lfsck_layout_new_v1_lovea(env, lfsck, &rec->lor_layout,
2184                                                parent, buf, ea_off, &lmm,
2185                                                &objs);
2186         if (rc > 0)
2187                 rc = lfsck_layout_refill_lovea(env, lfsck, handle, parent, cfid,
2188                                                buf, lmm, objs, fl, ost_idx, rc);
2189
2190         CDEBUG(D_LFSCK, "%s: layout LFSCK assistant created layout EA for "
2191                DFID": parent "DFID", OST-index %u, stripe-index %u, "
2192                "stripe_size %u, stripe_count %u, comp_id %u, comp_start %llu, "
2193                "comp_end %llu, layout version %u, range %u, fl %d, "
2194                "%s LOV EA hole: rc = %d\n",
2195                lfsck_lfsck2name(lfsck), PFID(cfid), PFID(lfsck_dto2fid(parent)),
2196                ost_idx, ea_off, ol->ol_stripe_size, ol->ol_stripe_count,
2197                ol->ol_comp_id, ol->ol_comp_start, ol->ol_comp_end,
2198                rec->lor_layout_version, rec->lor_range, fl,
2199                le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_HOLE ?
2200                "with" : "without", rc);
2201
2202         RETURN(rc);
2203 }
2204
2205 static int __lfsck_layout_update_pfid(const struct lu_env *env,
2206                                       struct lfsck_component *com,
2207                                       struct dt_object *child,
2208                                       const struct lu_fid *pfid,
2209                                       const struct ost_layout *ol, __u32 offset,
2210                                       __u32 version, __u32 range)
2211 {
2212         struct dt_device        *dev    = lfsck_obj2dev(child);
2213         struct filter_fid       *ff     = &lfsck_env_info(env)->lti_ff;
2214         struct thandle          *handle;
2215         struct lu_buf            buf    = { NULL };
2216         int                      rc;
2217
2218         ff->ff_parent.f_seq = cpu_to_le64(pfid->f_seq);
2219         ff->ff_parent.f_oid = cpu_to_le32(pfid->f_oid);
2220         /* Currently, the filter_fid::ff_parent::f_ver is not the real parent
2221          * MDT-object's FID::f_ver, instead it is the OST-object index in its
2222          * parent MDT-object's layout EA. */
2223         ff->ff_parent.f_stripe_idx = cpu_to_le32(offset);
2224         ost_layout_cpu_to_le(&ff->ff_layout, ol);
2225         ff->ff_layout_version = cpu_to_le32(version);
2226         ff->ff_range = cpu_to_le32(range);
2227         lfsck_buf_init(&buf, ff, sizeof(*ff));
2228
2229         handle = lfsck_trans_create(env, dev, com->lc_lfsck);
2230         if (IS_ERR(handle))
2231                 RETURN(PTR_ERR(handle));
2232
2233         rc = dt_declare_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2234         if (rc != 0)
2235                 GOTO(stop, rc);
2236
2237         rc = dt_trans_start_local(env, dev, handle);
2238         if (rc != 0)
2239                 GOTO(stop, rc);
2240
2241         rc = dt_xattr_set(env, child, &buf, XATTR_NAME_FID, 0, handle);
2242
2243         GOTO(stop, rc);
2244
2245 stop:
2246         dt_trans_stop(env, dev, handle);
2247
2248         return rc;
2249 }
2250
2251 /**
2252  * \retval       +1: repaired
2253  * \retval        0: did nothing
2254  * \retval      -ve: on error
2255  */
2256 static int lfsck_layout_update_pfid(const struct lu_env *env,
2257                                     struct lfsck_component *com,
2258                                     struct dt_object *parent,
2259                                     struct lu_fid *cfid,
2260                                     struct dt_device *cdev,
2261                                     struct lu_orphan_rec_v3 *rec, __u32 ea_off)
2262 {
2263         struct dt_object        *child;
2264         int                      rc     = 0;
2265         ENTRY;
2266
2267         child = lfsck_object_find_by_dev(env, cdev, cfid);
2268         if (IS_ERR(child))
2269                 RETURN(PTR_ERR(child));
2270
2271         rc = __lfsck_layout_update_pfid(env, com, child,
2272                                         lu_object_fid(&parent->do_lu),
2273                                         &rec->lor_layout, ea_off,
2274                                         rec->lor_layout_version,
2275                                         rec->lor_range);
2276         lfsck_object_put(env, child);
2277
2278         RETURN(rc == 0 ? 1 : rc);
2279 }
2280
2281 static int lfsck_lovea_size(struct ost_layout *ol, __u32 ea_off)
2282 {
2283         if (ol->ol_comp_id != 0)
2284                 return sizeof(struct lov_comp_md_v1) +
2285                        sizeof(struct lov_comp_md_entry_v1) +
2286                        lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2287
2288         if (ol->ol_stripe_count != 0)
2289                 return lov_mds_md_size(ol->ol_stripe_count, LOV_MAGIC_V1);
2290
2291         return lov_mds_md_size(ea_off + 1, LOV_MAGIC_V1);
2292 }
2293
2294 /**
2295  * This function will create the MDT-object with the given (partial) LOV EA.
2296  *
2297  * Under some data corruption cases, the MDT-object of the file may be lost,
2298  * but its OST-objects, or some of them are there. The layout LFSCK needs to
2299  * re-create the MDT-object with the orphan OST-object(s) information.
2300  *
2301  * On the other hand, the LFSCK may has created some OST-object for repairing
2302  * dangling LOV EA reference, but as the LFSCK processing, it may find that
2303  * the old OST-object is there and should replace the former new created OST
2304  * object. Unfortunately, some others have modified such newly created object.
2305  * To keep the data (both new and old), the LFSCK will create MDT-object with
2306  * new FID to reference the original OST-object.
2307  *
2308  * \param[in] env       pointer to the thread context
2309  * \param[in] com       pointer to the lfsck component
2310  * \param[in] ltd       pointer to target device descriptor
2311  * \param[in] rec       pointer to the record for the orphan OST-object
2312  * \param[in] cfid      pointer to FID for the orphan OST-object
2313  * \param[in] infix     additional information, such as the FID for original
2314  *                      MDT-object and the stripe offset in the LOV EA
2315  * \param[in] type      the type for describing why the orphan MDT-object is
2316  *                      created. The rules are as following:
2317  *
2318  *  type "C":           Multiple OST-objects claim the same MDT-object and the
2319  *                      same slot in the layout EA. Then the LFSCK will create
2320  *                      new MDT-object(s) to hold the conflict OST-object(s).
2321  *
2322  *  type "N":           The orphan OST-object does not know which one was the
2323  *                      real parent MDT-object, so the LFSCK uses new FID for
2324  *                      its parent MDT-object.
2325  *
2326  *  type "R":           The orphan OST-object knows its parent MDT-object FID,
2327  *                      but does not know the position (the file name) in the
2328  *                      layout.
2329  *
2330  *  type "D":           The MDT-object is a directory, it may knows its parent
2331  *                      but because there is no valid linkEA, the LFSCK cannot
2332  *                      know where to put it back to the namespace.
2333  *  type "O":           The MDT-object has no linkEA, and there is no name
2334  *                      entry that references the MDT-object.
2335  *
2336  *  type "P":           The orphan object to be created was a parent directory
2337  *                      of some MDT-object which linkEA shows that the @orphan
2338  *                      object is missing.
2339  *
2340  * The orphan name will be like:
2341  * ${FID}-${infix}-${type}-${conflict_version}
2342  *
2343  * \param[in] ea_off    the stripe offset in the LOV EA
2344  *
2345  * \retval              positive on repaired something
2346  * \retval              0 if needs to repair nothing
2347  * \retval              negative error number on failure
2348  */
2349 static int lfsck_layout_recreate_parent(const struct lu_env *env,
2350                                         struct lfsck_component *com,
2351                                         struct lfsck_tgt_desc *ltd,
2352                                         struct lu_orphan_rec_v3 *rec,
2353                                         struct lu_fid *cfid,
2354                                         const char *infix,
2355                                         const char *type,
2356                                         __u32 ea_off)
2357 {
2358         struct lfsck_thread_info        *info   = lfsck_env_info(env);
2359         struct dt_insert_rec            *dtrec  = &info->lti_dt_rec;
2360         char                            *name   = info->lti_key;
2361         struct lu_attr                  *la     = &info->lti_la2;
2362         struct dt_object_format         *dof    = &info->lti_dof;
2363         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2364         struct lu_fid                   *pfid   = &rec->lor_rec.lor_fid;
2365         struct lu_fid                   *tfid   = &info->lti_fid3;
2366         struct dt_device                *dev    = lfsck->li_bottom;
2367         struct dt_object                *lpf    = lfsck->li_lpf_obj;
2368         struct dt_object                *pobj   = NULL;
2369         struct dt_object                *cobj   = NULL;
2370         struct thandle                  *th     = NULL;
2371         struct lu_buf                   *ea_buf = &info->lti_big_buf;
2372         struct lu_buf                    lov_buf;
2373         struct lfsck_lock_handle        *llh    = &info->lti_llh;
2374         struct linkea_data               ldata  = { NULL };
2375         struct lu_buf                    linkea_buf;
2376         const struct lu_name            *pname;
2377         int                              size   = 0;
2378         int                              idx    = 0;
2379         int                              rc     = 0;
2380         ENTRY;
2381
2382         if (lfsck_is_dryrun(lfsck))
2383                 GOTO(log, rc = 0);
2384
2385         if (unlikely(lpf == NULL))
2386                 GOTO(log, rc = -ENXIO);
2387
2388         /* We use two separated transactions to repair the inconsistency.
2389          *
2390          * 1) create the MDT-object locally.
2391          * 2) update the OST-object's PFID EA if necessary.
2392          *
2393          * If 1) succeed, but 2) failed, then the OST-object's PFID EA will be
2394          * updated when the layout LFSCK run next time.
2395          *
2396          * If 1) failed, but 2) succeed, then such MDT-object will be re-created
2397          * when the layout LFSCK run next time. */
2398
2399         if (fid_is_zero(pfid)) {
2400                 rc = lfsck_fid_alloc(env, lfsck, pfid, false);
2401                 if (rc != 0)
2402                         GOTO(log, rc);
2403
2404                 cobj = lfsck_object_find_by_dev(env, ltd->ltd_tgt, cfid);
2405                 if (IS_ERR(cobj))
2406                         GOTO(log, rc = PTR_ERR(cobj));
2407         }
2408
2409         pobj = lfsck_object_find_by_dev(env, dev, pfid);
2410         if (IS_ERR(pobj))
2411                 GOTO(log, rc = PTR_ERR(pobj));
2412
2413         LASSERT(infix != NULL);
2414         LASSERT(type != NULL);
2415
2416         memset(la, 0, sizeof(*la));
2417         la->la_uid = rec->lor_rec.lor_uid;
2418         la->la_gid = rec->lor_rec.lor_gid;
2419         la->la_mode = S_IFREG | S_IRUSR;
2420         la->la_valid = LA_MODE | LA_UID | LA_GID;
2421
2422         memset(dof, 0, sizeof(*dof));
2423         dof->dof_type = dt_mode_to_dft(S_IFREG);
2424         /* Because the dof->dof_reg.striped = 0, the LOD will not create
2425          * the stripe(s). The LFSCK will specify the LOV EA via
2426          * lfsck_layout_update_lovea(). */
2427
2428         size = lfsck_lovea_size(&rec->lor_layout, ea_off);
2429         if (ea_buf->lb_len < size) {
2430                 lu_buf_realloc(ea_buf, size);
2431                 if (ea_buf->lb_buf == NULL)
2432                         GOTO(log, rc = -ENOMEM);
2433         }
2434
2435 again:
2436         do {
2437                 snprintf(name, NAME_MAX, DFID"%s-%s-%d", PFID(pfid), infix,
2438                          type, idx++);
2439                 rc = dt_lookup_dir(env, lfsck->li_lpf_obj, name, tfid);
2440                 if (rc != 0 && rc != -ENOENT)
2441                         GOTO(log, rc);
2442         } while (rc == 0);
2443
2444         rc = lfsck_lock(env, lfsck, lfsck->li_lpf_obj, name, llh,
2445                         MDS_INODELOCK_UPDATE, LCK_PW);
2446         if (rc != 0)
2447                 GOTO(log, rc);
2448
2449         /* Re-check whether the name conflict with othrs after taken
2450          * the ldlm lock. */
2451         rc = dt_lookup_dir(env, lfsck->li_lpf_obj, name, tfid);
2452         if (unlikely(rc == 0)) {
2453                 lfsck_unlock(llh);
2454                 goto again;
2455         }
2456
2457         if (rc != -ENOENT)
2458                 GOTO(unlock, rc);
2459
2460         pname = lfsck_name_get_const(env, name, strlen(name));
2461         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
2462                               pname, lfsck_dto2fid(lfsck->li_lpf_obj));
2463         if (rc != 0)
2464                 GOTO(unlock, rc);
2465
2466         /* The 1st transaction. */
2467         th = lfsck_trans_create(env, dev, lfsck);
2468         if (IS_ERR(th))
2469                 GOTO(unlock, rc = PTR_ERR(th));
2470
2471         rc = dt_declare_create(env, pobj, la, NULL, dof, th);
2472         if (rc != 0)
2473                 GOTO(stop, rc);
2474
2475         lfsck_buf_init(&lov_buf, ea_buf->lb_buf, size);
2476         rc = dt_declare_xattr_set(env, pobj, &lov_buf, XATTR_NAME_LOV,
2477                                   LU_XATTR_CREATE, th);
2478         if (rc != 0)
2479                 GOTO(stop, rc);
2480
2481         dtrec->rec_fid = pfid;
2482         dtrec->rec_type = S_IFREG;
2483         rc = dt_declare_insert(env, lpf,
2484                                (const struct dt_rec *)dtrec,
2485                                (const struct dt_key *)name, th);
2486         if (rc != 0)
2487                 GOTO(stop, rc);
2488
2489         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2490                        ldata.ld_leh->leh_len);
2491         rc = dt_declare_xattr_set(env, pobj, &linkea_buf,
2492                                   XATTR_NAME_LINK, 0, th);
2493         if (rc != 0)
2494                 GOTO(stop, rc);
2495
2496         rc = dt_trans_start_local(env, dev, th);
2497         if (rc != 0)
2498                 GOTO(stop, rc);
2499
2500         dt_write_lock(env, pobj, 0);
2501         rc = dt_create(env, pobj, la, NULL, dof, th);
2502         if (rc == 0)
2503                 rc = lfsck_layout_update_lovea(env, lfsck, th, rec, pobj, cfid,
2504                         &lov_buf, LU_XATTR_CREATE, ltd->ltd_index, ea_off);
2505         dt_write_unlock(env, pobj);
2506         if (rc < 0)
2507                 GOTO(stop, rc);
2508
2509         rc = dt_insert(env, lpf, (const struct dt_rec *)dtrec,
2510                        (const struct dt_key *)name, th);
2511         if (rc != 0)
2512                 GOTO(stop, rc);
2513
2514         rc = dt_xattr_set(env, pobj, &linkea_buf, XATTR_NAME_LINK, 0, th);
2515         if (rc == 0 && cobj != NULL) {
2516                 dt_trans_stop(env, dev, th);
2517                 th = NULL;
2518
2519                 /* The 2nd transaction. */
2520                 rc = __lfsck_layout_update_pfid(env, com, cobj, pfid,
2521                                                 &rec->lor_layout, ea_off,
2522                                                 rec->lor_layout_version,
2523                                                 rec->lor_range);
2524         }
2525
2526         GOTO(stop, rc);
2527
2528 stop:
2529         if (th != NULL)
2530                 dt_trans_stop(env, dev, th);
2531
2532 unlock:
2533         lfsck_unlock(llh);
2534
2535 log:
2536         if (cobj != NULL && !IS_ERR(cobj))
2537                 lfsck_object_put(env, cobj);
2538         if (pobj != NULL && !IS_ERR(pobj))
2539                 lfsck_object_put(env, pobj);
2540
2541         if (rc < 0)
2542                 CDEBUG(D_LFSCK, "%s layout LFSCK assistant failed to "
2543                        "recreate the lost MDT-object: parent "DFID
2544                        ", child "DFID", OST-index %u, stripe-index %u, "
2545                        "infix %s, type %s: rc = %d\n",
2546                        lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2547                        ltd->ltd_index, ea_off, infix, type, rc);
2548
2549         return rc >= 0 ? 1 : rc;
2550 }
2551
2552 static int lfsck_layout_master_conditional_destroy(const struct lu_env *env,
2553                                                    struct lfsck_component *com,
2554                                                    const struct lu_fid *fid,
2555                                                    __u32 index)
2556 {
2557         struct lfsck_thread_info *info  = lfsck_env_info(env);
2558         struct lfsck_request     *lr    = &info->lti_lr;
2559         struct lfsck_instance    *lfsck = com->lc_lfsck;
2560         struct lfsck_tgt_desc    *ltd;
2561         struct ptlrpc_request    *req;
2562         struct lfsck_request     *tmp;
2563         struct obd_export        *exp;
2564         int                       rc    = 0;
2565         ENTRY;
2566
2567         ltd = lfsck_tgt_get(&lfsck->li_ost_descs, index);
2568         if (unlikely(ltd == NULL))
2569                 RETURN(-ENXIO);
2570
2571         exp = ltd->ltd_exp;
2572         if (!(exp_connect_flags(exp) & OBD_CONNECT_LFSCK))
2573                 GOTO(put, rc = -EOPNOTSUPP);
2574
2575         req = ptlrpc_request_alloc(class_exp2cliimp(exp), &RQF_LFSCK_NOTIFY);
2576         if (req == NULL)
2577                 GOTO(put, rc = -ENOMEM);
2578
2579         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
2580         if (rc != 0) {
2581                 ptlrpc_request_free(req);
2582
2583                 GOTO(put, rc);
2584         }
2585
2586         memset(lr, 0, sizeof(*lr));
2587         lr->lr_event = LE_CONDITIONAL_DESTROY;
2588         lr->lr_active = LFSCK_TYPE_LAYOUT;
2589         lr->lr_fid = *fid;
2590
2591         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2592         *tmp = *lr;
2593         ptlrpc_request_set_replen(req);
2594
2595         rc = ptlrpc_queue_wait(req);
2596         ptlrpc_req_finished(req);
2597
2598         GOTO(put, rc);
2599
2600 put:
2601         lfsck_tgt_put(ltd);
2602
2603         return rc;
2604 }
2605
2606 static int lfsck_layout_slave_conditional_destroy(const struct lu_env *env,
2607                                                   struct lfsck_component *com,
2608                                                   struct lfsck_request *lr)
2609 {
2610         struct lfsck_thread_info        *info   = lfsck_env_info(env);
2611         struct lu_attr                  *la     = &info->lti_la;
2612         union ldlm_policy_data          *policy = &info->lti_policy;
2613         struct ldlm_res_id              *resid  = &info->lti_resid;
2614         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2615         struct dt_device                *dev    = lfsck->li_bottom;
2616         struct lu_fid                   *fid    = &lr->lr_fid;
2617         struct dt_object                *obj;
2618         struct thandle                  *th     = NULL;
2619         struct lustre_handle             lh     = { 0 };
2620         __u64                            flags  = 0;
2621         int                              rc     = 0;
2622         ENTRY;
2623
2624         obj = lfsck_object_find_by_dev(env, dev, fid);
2625         if (IS_ERR(obj))
2626                 RETURN(PTR_ERR(obj));
2627
2628         dt_read_lock(env, obj, 0);
2629         if (dt_object_exists(obj) == 0 ||
2630             lfsck_is_dead_obj(obj)) {
2631                 dt_read_unlock(env, obj);
2632
2633                 GOTO(put, rc = -ENOENT);
2634         }
2635
2636         /* Get obj's attr without lock firstly. */
2637         rc = dt_attr_get(env, obj, la);
2638         dt_read_unlock(env, obj);
2639         if (rc != 0)
2640                 GOTO(put, rc);
2641
2642         if (likely(la->la_ctime != 0 || la->la_mode & S_ISUID))
2643                 GOTO(put, rc = -ETXTBSY);
2644
2645         /* Acquire extent lock on [0, EOF] to sync with all possible written. */
2646         LASSERT(lfsck->li_namespace != NULL);
2647
2648         memset(policy, 0, sizeof(*policy));
2649         policy->l_extent.end = OBD_OBJECT_EOF;
2650         ost_fid_build_resid(fid, resid);
2651         rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
2652                                     LDLM_EXTENT, policy, LCK_EX, &flags,
2653                                     ldlm_blocking_ast, ldlm_completion_ast,
2654                                     NULL, NULL, 0, LVB_T_NONE, NULL, &lh);
2655         if (rc != ELDLM_OK)
2656                 GOTO(put, rc = -EIO);
2657
2658         dt_write_lock(env, obj, 0);
2659         /* Get obj's attr within lock again. */
2660         rc = dt_attr_get(env, obj, la);
2661         if (rc != 0)
2662                 GOTO(unlock, rc);
2663
2664         if (la->la_ctime != 0)
2665                 GOTO(unlock, rc = -ETXTBSY);
2666
2667         th = lfsck_trans_create(env, dev, lfsck);
2668         if (IS_ERR(th))
2669                 GOTO(unlock, rc = PTR_ERR(th));
2670
2671         rc = dt_declare_ref_del(env, obj, th);
2672         if (rc != 0)
2673                 GOTO(stop, rc);
2674
2675         rc = dt_declare_destroy(env, obj, th);
2676         if (rc != 0)
2677                 GOTO(stop, rc);
2678
2679         rc = dt_trans_start_local(env, dev, th);
2680         if (rc != 0)
2681                 GOTO(stop, rc);
2682
2683         rc = dt_ref_del(env, obj, th);
2684         if (rc != 0)
2685                 GOTO(stop, rc);
2686
2687         rc = dt_destroy(env, obj, th);
2688         if (rc == 0)
2689                 CDEBUG(D_LFSCK, "%s: layout LFSCK destroyed the empty "
2690                        "OST-object "DFID" that was created for reparing "
2691                        "dangling referenced case. But the original missing "
2692                        "OST-object is found now.\n",
2693                        lfsck_lfsck2name(lfsck), PFID(fid));
2694
2695         GOTO(stop, rc);
2696
2697 stop:
2698         dt_trans_stop(env, dev, th);
2699
2700 unlock:
2701         dt_write_unlock(env, obj);
2702         ldlm_lock_decref(&lh, LCK_EX);
2703
2704 put:
2705         lfsck_object_put(env, obj);
2706
2707         return rc;
2708 }
2709
2710 /**
2711  * Some OST-object has occupied the specified layout EA slot.
2712  * Such OST-object may be generated by the LFSCK when repair
2713  * dangling referenced MDT-object, which can be indicated by
2714  * attr::la_ctime == 0 but without S_ISUID in la_mode. If it
2715  * is true and such OST-object has not been modified yet, we
2716  * will replace it with the orphan OST-object; otherwise the
2717  * LFSCK will create new MDT-object to reference the orphan.
2718  *
2719  * \retval       +1: repaired
2720  * \retval        0: did nothing
2721  * \retval      -ve: on error
2722  */
2723 static int lfsck_layout_conflict_create(const struct lu_env *env,
2724                                         struct lfsck_component *com,
2725                                         struct lfsck_tgt_desc *ltd,
2726                                         struct lu_orphan_rec_v3 *rec,
2727                                         struct dt_object *parent,
2728                                         struct lu_fid *cfid,
2729                                         struct lu_buf *ea_buf,
2730                                         struct lov_mds_md_v1 *lmm,
2731                                         struct lov_ost_data_v1 *slot,
2732                                         __u32 ea_off, int lovea_size)
2733 {
2734         struct lfsck_thread_info *info          = lfsck_env_info(env);
2735         struct lu_fid            *cfid2         = &info->lti_fid2;
2736         struct ost_id            *oi            = &info->lti_oi;
2737         struct dt_device         *dev           = lfsck_obj2dev(parent);
2738         struct thandle           *th            = NULL;
2739         struct lustre_handle      lh            = { 0 };
2740         __u32                     ost_idx2      = le32_to_cpu(slot->l_ost_idx);
2741         int                       rc            = 0;
2742         ENTRY;
2743
2744         while (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val)) {
2745                 if (unlikely(!thread_is_running(&com->lc_lfsck->li_thread)))
2746                         RETURN(0);
2747         }
2748
2749         ostid_le_to_cpu(&slot->l_ost_oi, oi);
2750         rc = ostid_to_fid(cfid2, oi, ost_idx2);
2751         if (rc != 0)
2752                 GOTO(out, rc);
2753
2754         rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
2755                               MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2756                               LCK_EX);
2757         if (rc != 0)
2758                 GOTO(out, rc);
2759
2760         rc = lfsck_layout_master_conditional_destroy(env, com, cfid2, ost_idx2);
2761
2762         /* If the conflict OST-obejct is not created for fixing dangling
2763          * referenced MDT-object in former LFSCK check/repair, or it has
2764          * been modified by others, then we cannot destroy it. Re-create
2765          * a new MDT-object for the orphan OST-object. */
2766         if (rc == -ETXTBSY) {
2767                 /* No need the layout lock on the original parent. */
2768                 lfsck_ibits_unlock(&lh, LCK_EX);
2769
2770                 fid_zero(&rec->lor_rec.lor_fid);
2771                 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2772                          "-"DFID"-%x", PFID(lu_object_fid(&parent->do_lu)),
2773                          ea_off);
2774                 rc = lfsck_layout_recreate_parent(env, com, ltd, rec, cfid,
2775                                                 info->lti_tmpbuf, "C", ea_off);
2776
2777                 RETURN(rc);
2778         }
2779
2780         if (rc != 0 && rc != -ENOENT)
2781                 GOTO(unlock, rc);
2782
2783         if (lfsck_is_dryrun(com->lc_lfsck))
2784                 GOTO(unlock, rc = 0);
2785
2786         th = lfsck_trans_create(env, dev, com->lc_lfsck);
2787         if (IS_ERR(th))
2788                 GOTO(unlock, rc = PTR_ERR(th));
2789
2790         rc = dt_declare_xattr_set(env, parent, ea_buf, XATTR_NAME_LOV,
2791                                   LU_XATTR_REPLACE, th);
2792         if (rc != 0)
2793                 GOTO(stop, rc);
2794
2795         rc = dt_trans_start_local(env, dev, th);
2796         if (rc != 0)
2797                 GOTO(stop, rc);
2798
2799         dt_write_lock(env, parent, 0);
2800         lmm->lmm_layout_gen = cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
2801         rc = lfsck_layout_refill_lovea(env, com->lc_lfsck, th, parent, cfid,
2802                                        ea_buf, lmm, slot, LU_XATTR_REPLACE,
2803                                        ltd->ltd_index, lovea_size);
2804         dt_write_unlock(env, parent);
2805
2806         GOTO(stop, rc);
2807
2808 stop:
2809         dt_trans_stop(env, dev, th);
2810
2811 unlock:
2812         lfsck_ibits_unlock(&lh, LCK_EX);
2813
2814 out:
2815         CDEBUG(D_LFSCK, "%s: layout LFSCK assistant replaced the conflict "
2816                "OST-object "DFID" on the OST %x with the orphan "DFID" on "
2817                "the OST %x: parent "DFID", stripe-index %u: rc = %d\n",
2818                lfsck_lfsck2name(com->lc_lfsck), PFID(cfid2), ost_idx2,
2819                PFID(cfid), ltd->ltd_index, PFID(lfsck_dto2fid(parent)),
2820                ea_off, rc);
2821
2822         return rc >= 0 ? 1 : rc;
2823 }
2824
2825 /**
2826  * \retval       +1: repaired
2827  * \retval        0: did nothing
2828  * \retval      -ve: on error
2829  */
2830 static int lfsck_layout_recreate_lovea(const struct lu_env *env,
2831                                        struct lfsck_component *com,
2832                                        struct lfsck_tgt_desc *ltd,
2833                                        struct lu_orphan_rec_v3 *rec,
2834                                        struct dt_object *parent,
2835                                        struct lu_fid *cfid,
2836                                        __u32 ost_idx, __u32 ea_off)
2837 {
2838         struct lfsck_thread_info *info          = lfsck_env_info(env);
2839         struct lu_buf            *buf           = &info->lti_big_buf;
2840         struct lu_fid            *fid           = &info->lti_fid2;
2841         struct ost_id            *oi            = &info->lti_oi;
2842         struct lfsck_instance    *lfsck         = com->lc_lfsck;
2843         struct dt_device         *dt            = lfsck_obj2dev(parent);
2844         struct lfsck_bookmark    *bk            = &lfsck->li_bookmark_ram;
2845         struct ost_layout        *ol            = &rec->lor_layout;
2846         struct lov_comp_md_v1    *lcm           = NULL;
2847         struct lov_comp_md_entry_v1 *lcme       = NULL;
2848         struct thandle           *handle        = NULL;
2849         size_t                    lovea_size;
2850         struct lov_mds_md_v1     *lmm;
2851         struct lov_ost_data_v1   *objs;
2852         struct lustre_handle      lh            = { 0 };
2853         __u32                     magic;
2854         __u32 flags = 0;
2855         int                       fl            = 0;
2856         int                       rc            = 0;
2857         int                       rc1;
2858         int                       i;
2859         int pos = 0;
2860         __u16 count;
2861         bool locked = false;
2862         bool new_mirror = true;
2863         ENTRY;
2864
2865         if (lfsck_is_dryrun(lfsck))
2866                 RETURN(0);
2867
2868         rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2869                               MDS_INODELOCK_LAYOUT | MDS_INODELOCK_XATTR,
2870                               LCK_EX);
2871         if (rc != 0) {
2872                 CDEBUG(D_LFSCK, "%s: layout LFSCK assistant failed to recreate "
2873                        "LOV EA for "DFID": parent "DFID", OST-index %u, "
2874                        "stripe-index %u, comp_id %u, comp_start %llu, "
2875                        "comp_end %llu, layout version %u, range %u: rc = %d\n",
2876                        lfsck_lfsck2name(lfsck), PFID(cfid),
2877                        PFID(lfsck_dto2fid(parent)), ost_idx, ea_off,
2878                        ol->ol_comp_id, ol->ol_comp_start,
2879                        ol->ol_comp_end, rec->lor_layout_version,
2880                        rec->lor_range, rc);
2881
2882                 RETURN(rc);
2883         }
2884
2885 again:
2886         if (locked) {
2887                 dt_write_unlock(env, parent);
2888                 locked = false;
2889         }
2890
2891         if (handle != NULL) {
2892                 dt_trans_stop(env, dt, handle);
2893                 handle = NULL;
2894         }
2895
2896         if (rc < 0)
2897                 GOTO(unlock_layout, rc);
2898
2899         lovea_size = rc;
2900         if (buf->lb_len < lovea_size) {
2901                 lu_buf_realloc(buf, lovea_size);
2902                 if (buf->lb_buf == NULL)
2903                         GOTO(unlock_layout, rc = -ENOMEM);
2904         }
2905
2906         if (!(bk->lb_param & LPF_DRYRUN)) {
2907                 handle = lfsck_trans_create(env, dt, lfsck);
2908                 if (IS_ERR(handle))
2909                         GOTO(unlock_layout, rc = PTR_ERR(handle));
2910
2911                 rc = dt_declare_xattr_set(env, parent, buf, XATTR_NAME_LOV,
2912                                           fl, handle);
2913                 if (rc != 0)
2914                         GOTO(stop, rc);
2915
2916                 rc = dt_trans_start_local(env, dt, handle);
2917                 if (rc != 0)
2918                         GOTO(stop, rc);
2919         }
2920
2921         dt_write_lock(env, parent, 0);
2922         locked = true;
2923         rc = dt_xattr_get(env, parent, buf, XATTR_NAME_LOV);
2924         if (rc == -ERANGE) {
2925                 rc = dt_xattr_get(env, parent, &LU_BUF_NULL, XATTR_NAME_LOV);
2926                 LASSERT(rc != 0);
2927                 goto again;
2928         } else if (rc == -ENODATA || rc == 0) {
2929                 lovea_size = lfsck_lovea_size(ol, ea_off);
2930                 /* If the declared is not big enough, re-try. */
2931                 if (buf->lb_len < lovea_size) {
2932                         rc = lovea_size;
2933                         goto again;
2934                 }
2935                 fl = LU_XATTR_CREATE;
2936         } else if (rc < 0) {
2937                 GOTO(unlock_parent, rc);
2938         } else if (unlikely(buf->lb_len == 0)) {
2939                 goto again;
2940         } else {
2941                 fl = LU_XATTR_REPLACE;
2942                 lovea_size = rc;
2943         }
2944
2945         if (fl == LU_XATTR_CREATE) {
2946                 if (bk->lb_param & LPF_DRYRUN)
2947                         GOTO(unlock_parent, rc = 1);
2948
2949                 LASSERT(buf->lb_len >= lovea_size);
2950
2951                 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2952                                                cfid, buf, fl, ost_idx, ea_off);
2953
2954                 GOTO(unlock_parent, rc);
2955         }
2956
2957         lmm = buf->lb_buf;
2958         rc1 = lfsck_layout_verify_header(parent, lmm, lovea_size);
2959
2960         /* If the LOV EA crashed, the rebuild it. */
2961         if (rc1 == -EINVAL) {
2962                 if (bk->lb_param & LPF_DRYRUN)
2963                         GOTO(unlock_parent, rc = 1);
2964
2965                 LASSERT(buf->lb_len >= lovea_size);
2966
2967                 rc = lfsck_layout_update_lovea(env, lfsck, handle, rec, parent,
2968                                                cfid, buf, fl, ost_idx, ea_off);
2969
2970                 GOTO(unlock_parent, rc);
2971         }
2972
2973         /* For other unknown magic/pattern, keep the current LOV EA. */
2974         if (rc1 == -EOPNOTSUPP)
2975                 GOTO(unlock_parent, rc1 = 0);
2976
2977         if (rc1)
2978                 GOTO(unlock_parent, rc = rc1);
2979
2980         magic = le32_to_cpu(lmm->lmm_magic);
2981         if (magic == LOV_MAGIC_COMP_V1 || magic == LOV_MAGIC_SEL) {
2982                 __u64 start;
2983                 __u64 end;
2984                 __u16 mirror_id0 = mirror_id_of(ol->ol_comp_id);
2985                 __u16 mirror_id1;
2986
2987                 if (bk->lb_param & LPF_DRYRUN)
2988                         GOTO(unlock_parent, rc = 1);
2989
2990                 lcm = buf->lb_buf;
2991                 count = le16_to_cpu(lcm->lcm_entry_count);
2992                 for (i = 0; i < count; pos = ++i) {
2993                         lcme = &lcm->lcm_entries[i];
2994                         start = le64_to_cpu(lcme->lcme_extent.e_start);
2995                         end = le64_to_cpu(lcme->lcme_extent.e_end);
2996                         mirror_id1 = mirror_id_of(le32_to_cpu(lcme->lcme_id));
2997
2998                         if (mirror_id0 > mirror_id1)
2999                                 continue;
3000
3001                         if (mirror_id0 < mirror_id1)
3002                                 break;
3003
3004                         new_mirror = false;
3005                         if (end <= ol->ol_comp_start)
3006                                 continue;
3007
3008                         if (start >= ol->ol_comp_end)
3009                                 break;
3010
3011                         lmm = buf->lb_buf + le32_to_cpu(lcme->lcme_offset);
3012                         magic = le32_to_cpu(lmm->lmm_magic);
3013                         flags = le32_to_cpu(lcme->lcme_flags);
3014                         goto further;
3015                 }
3016
3017                 rc = lfsck_layout_add_comp(env, lfsck, handle, rec, parent,
3018                                 cfid, buf, ost_idx, ea_off, pos, new_mirror);
3019
3020                 GOTO(unlock_parent, rc);
3021         }
3022
3023 further:
3024         count = le16_to_cpu(lmm->lmm_stripe_count);
3025         if (count == 0)
3026                 GOTO(unlock_parent, rc = -EINVAL);
3027         LASSERT(count > 0);
3028
3029         /* Exceed the current end of MDT-object layout EA. Then extend it. */
3030         if (count <= ea_off) {
3031                 if (bk->lb_param & LPF_DRYRUN)
3032                         GOTO(unlock_parent, rc = 1);
3033
3034                 lovea_size = lov_mds_md_size(ea_off + 1, magic);
3035                 /* If the declared is not big enough, re-try. */
3036                 if (buf->lb_len < lovea_size) {
3037                         rc = lovea_size;
3038                         goto again;
3039                 }
3040
3041                 if (lcm) {
3042                         LASSERT(lcme);
3043
3044                         lcme->lcme_flags = cpu_to_le32(flags | LCME_FL_INIT);
3045                         lfsck_layout_update_lcm(lcm, lcme,
3046                                                 rec->lor_layout_version,
3047                                                 rec->lor_range);
3048                 }
3049
3050                 rc = lfsck_layout_extend_v1v3_lovea(env, lfsck, handle, ol,
3051                                         parent, cfid, buf, ost_idx, ea_off);
3052
3053                 GOTO(unlock_parent, rc);
3054         }
3055
3056         LASSERTF(rc > 0, "invalid rc = %d\n", rc);
3057
3058         if (magic == LOV_MAGIC_V1) {
3059                 objs = &lmm->lmm_objects[0];
3060         } else {
3061                 LASSERT(magic == LOV_MAGIC_V3);
3062                 objs = &((struct lov_mds_md_v3 *)lmm)->lmm_objects[0];
3063         }
3064
3065         for (i = 0; i < count; i++, objs++) {
3066                 /* The MDT-object was created via lfsck_layout_recover_create()
3067                  * by others before, and we fill the dummy layout EA. */
3068                 if ((lcme && !(flags & LCME_FL_INIT)) ||
3069                      lovea_slot_is_dummy(objs)) {
3070                         if (i != ea_off)
3071                                 continue;
3072
3073                         if (bk->lb_param & LPF_DRYRUN)
3074                                 GOTO(unlock_parent, rc = 1);
3075
3076                         lmm->lmm_layout_gen =
3077                             cpu_to_le16(le16_to_cpu(lmm->lmm_layout_gen) + 1);
3078                         if (lcme) {
3079                                 LASSERT(lcm);
3080
3081                                 if (le32_to_cpu(lmm->lmm_stripe_size) !=
3082                                         ol->ol_stripe_size ||
3083                                     le16_to_cpu(lmm->lmm_stripe_count) !=
3084                                         ol->ol_stripe_count ||
3085                                     le64_to_cpu(lcme->lcme_extent.e_start) !=
3086                                         ol->ol_comp_start ||
3087                                     le64_to_cpu(lcme->lcme_extent.e_end) !=
3088                                         ol->ol_comp_end) {
3089                                         CDEBUG(D_LFSCK, "%s: found invalid "
3090                                         "component for "DFID ": parent "DFID
3091                                         ", stripe-index %u, stripe_size %u, "
3092                                         "stripe_count %u, comp_id %u, "
3093                                         "comp_start %llu, comp_end %llu, "
3094                                         "cur_stripe_size %u, "
3095                                         "cur_stripe_count %u, "
3096                                         "cur_comp_start %llu, "
3097                                         "cur_comp_end %llu\n",
3098