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