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