Whamcloud - gitweb
969000a3500e51e19a424388f2cc1f86eb62fa0e
[fs/lustre-release.git] / lustre / lfsck / lfsck_engine.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, 2016, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_engine.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
33 #include <lu_object.h>
34 #include <dt_object.h>
35 #include <lustre_net.h>
36 #include <lustre_fid.h>
37 #include <obd_support.h>
38 #include <lustre_lib.h>
39
40 #include "lfsck_internal.h"
41
42 int lfsck_unpack_ent(struct lu_dirent *ent, __u64 *cookie, __u16 *type)
43 {
44         struct luda_type        *lt;
45         int                      align = sizeof(*lt) - 1;
46         int                      len;
47
48         fid_le_to_cpu(&ent->lde_fid, &ent->lde_fid);
49         *cookie = le64_to_cpu(ent->lde_hash);
50         ent->lde_reclen = le16_to_cpu(ent->lde_reclen);
51         ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
52         ent->lde_attrs = le32_to_cpu(ent->lde_attrs);
53
54         if (unlikely(!(ent->lde_attrs & LUDA_TYPE)))
55                 return -EINVAL;
56
57         len = (ent->lde_namelen + align) & ~align;
58         lt = (struct luda_type *)(ent->lde_name + len);
59         *type = le16_to_cpu(lt->lt_type);
60
61         /* Make sure the name is terminated with '\0'. The data (object type)
62          * after ent::lde_name maybe broken, but we have stored such data in
63          * the output parameter @type as above. */
64         ent->lde_name[ent->lde_namelen] = '\0';
65
66         return 0;
67 }
68
69 static void lfsck_di_oit_put(const struct lu_env *env, struct lfsck_instance *lfsck)
70 {
71         const struct dt_it_ops  *iops;
72         struct dt_it            *di;
73
74         spin_lock(&lfsck->li_lock);
75         iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
76         di = lfsck->li_di_oit;
77         lfsck->li_di_oit = NULL;
78         spin_unlock(&lfsck->li_lock);
79         iops->put(env, di);
80 }
81
82 static void lfsck_di_dir_put(const struct lu_env *env, struct lfsck_instance *lfsck)
83 {
84         const struct dt_it_ops  *iops;
85         struct dt_it            *di;
86
87         spin_lock(&lfsck->li_lock);
88         iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
89         di = lfsck->li_di_dir;
90         lfsck->li_di_dir = NULL;
91         lfsck->li_cookie_dir = 0;
92         spin_unlock(&lfsck->li_lock);
93         iops->put(env, di);
94 }
95
96 /**
97  * Check whether needs to scan the directory or not.
98  *
99  * 1) If we are not doing namespace LFSCK, or the given @obj is not directory,
100  *    then needs not to scan the @obj. Otherwise,
101  * 2) Global /ROOT needs to be scanned, backend root needs not to be scanned.
102  * 3) If the @obj is neither IGIF nor normal FID (including .lustre and its
103  *    sub-directories that have been scanned when the LFSCK engine start),
104  *    then needs not to be scanned.
105  * 4) If it is a remote object, then scanning the object will be done on the
106  *    MDT on which the object really resides.
107  * 5) If the local object has normal FID, then needs to be scanned. Otherwise,
108  * 6) If the object has linkEA, then needs to be scanned. Otherwise,
109  * 7) If none of the previous conditions are true, we need to check the parent
110  *    directories whether this subdirectory is in a tree that should be scanned.
111  *    Set the parent as current @obj, repeat 2)-7).
112  *
113  * \param[in] env       pointer to the thread context
114  * \param[in] lfsck     pointer to the lfsck instance
115  * \param[in] obj       pointer to the object to be checked
116  *
117  * \retval              positive number if the directory needs to be scanned
118  * \retval              0 if the directory needs NOT to be scanned
119  * \retval              negative error number on failure
120  */
121 static int lfsck_needs_scan_dir(const struct lu_env *env,
122                                 struct lfsck_instance *lfsck,
123                                 struct dt_object *obj)
124 {
125         struct lfsck_thread_info *info    = lfsck_env_info(env);
126         struct lu_fid            *fid     = &info->lti_fid;
127         struct lu_seq_range      *range   = &info->lti_range;
128         struct lu_attr           *la      = &info->lti_la;
129         struct seq_server_site   *ss      = lfsck_dev_site(lfsck);
130         __u32                     idx     = lfsck_dev_idx(lfsck);
131         int                       depth   = 0;
132         int                       rc      = 0;
133
134         if (list_empty(&lfsck->li_list_dir) || !S_ISDIR(lfsck_object_type(obj)))
135                 return 0;
136
137         *fid = *lfsck_dto2fid(obj);
138         rc = dt_attr_get(env, obj, la);
139         if (unlikely(rc || (la->la_valid & LA_FLAGS &&
140                             la->la_flags & LUSTRE_ORPHAN_FL))) {
141                 /* Orphan directory is empty, does not need scan. */
142                 CDEBUG(D_INFO,
143                        "%s: skip orphan dir "DFID", %llx/%x: rc = %d\n",
144                        lfsck_lfsck2name(lfsck), PFID(fid),
145                        la->la_valid, la->la_flags, rc);
146
147                 return rc;
148         }
149
150         LASSERT(ss != NULL);
151
152         while (1) {
153                 /* Global /ROOT is visible. */
154                 if (unlikely(lu_fid_eq(fid, &lfsck->li_global_root_fid)))
155                         return 1;
156
157                 /* Backend root is invisible. */
158                 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
159                         return 0;
160
161                 if (!fid_is_norm(fid) && !fid_is_igif(fid))
162                         return 0;
163
164                 fld_range_set_mdt(range);
165                 rc = fld_local_lookup(env, ss->ss_server_fld,
166                                       fid_seq(fid), range);
167                 if (rc != 0 || range->lsr_index != idx)
168                         /* Current FID should NOT be for the input parameter
169                          * @obj, because the lfsck_master_oit_engine() has
170                          * filtered out agent object. So current FID is for
171                          * the ancestor of the original input parameter @obj.
172                          * So the ancestor is a remote directory. The input
173                          * parameter @obj is local directory, and should be
174                          * scanned under such case. */
175                         return 1;
176
177                 /* normal FID on this target (locally) must be for the
178                  * client-side visiable object. */
179                 if (fid_is_norm(fid))
180                         return 1;
181
182                 /* Only true after "obj = NULL" set below */
183                 if (obj == NULL) {
184                         obj = lfsck_object_find_bottom(env, lfsck, fid);
185                         if (IS_ERR(obj))
186                                 return PTR_ERR(obj);
187
188                         depth++;
189                         if (!dt_object_exists(obj))
190                                 GOTO(out, rc = 0);
191                 }
192
193                 dt_read_lock(env, obj, DT_TGT_CHILD);
194                 if (unlikely(lfsck_is_dead_obj(obj))) {
195                         dt_read_unlock(env, obj);
196
197                         GOTO(out, rc = 0);
198                 }
199
200                 rc = dt_xattr_get(env, obj,
201                                   lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK);
202                 dt_read_unlock(env, obj);
203                 if (rc >= 0)
204                         GOTO(out, rc = 1);
205
206                 if (rc < 0 && rc != -ENODATA)
207                         GOTO(out, rc);
208
209                 rc = dt_lookup_dir(env, obj, dotdot, fid);
210                 if (depth > 0)
211                         lfsck_object_put(env, obj);
212
213                 obj = NULL;
214                 if (rc != 0)
215                         return rc;
216
217                 if (!fid_is_sane(fid))
218                         return 0;
219         }
220
221 out:
222         if (depth > 0 && obj != NULL)
223                 lfsck_object_put(env, obj);
224
225         return rc;
226 }
227
228 static int lfsck_load_stripe_lmv(const struct lu_env *env,
229                                  struct lfsck_instance *lfsck,
230                                  struct dt_object *obj)
231 {
232         struct lmv_mds_md_v1    *lmv    = &lfsck_env_info(env)->lti_lmv;
233         struct lfsck_lmv        *llmv;
234         int                      rc;
235         ENTRY;
236
237         LASSERT(lfsck->li_obj_dir == NULL);
238         LASSERT(lfsck->li_lmv == NULL);
239
240         rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv);
241         if (rc == -ENODATA) {
242                 lfsck->li_obj_dir = lfsck_object_get(obj);
243
244                 RETURN(0);
245         }
246
247         if (rc < 0)
248                 RETURN(rc);
249
250         OBD_ALLOC_PTR(llmv);
251         if (llmv == NULL)
252                 RETURN(-ENOMEM);
253
254         if (lmv->lmv_magic == LMV_MAGIC) {
255                 struct lfsck_slave_lmv_rec      *lslr;
256                 __u32                            stripes;
257
258                 llmv->ll_lmv_master = 1;
259                 if (lmv->lmv_stripe_count < 1)
260                         stripes = LFSCK_LMV_DEF_STRIPES;
261                 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
262                         stripes = LFSCK_LMV_MAX_STRIPES;
263                 else
264                         stripes = lmv->lmv_stripe_count;
265
266                 OBD_ALLOC_PTR_ARRAY_LARGE(lslr, stripes);
267                 if (lslr == NULL) {
268                         OBD_FREE_PTR(llmv);
269
270                         RETURN(-ENOMEM);
271                 }
272
273                 llmv->ll_stripes_allocated = stripes;
274                 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
275                 llmv->ll_lslr = lslr;
276         } else {
277                 llmv->ll_lmv_slave = 1;
278         }
279
280         lfsck->li_obj_dir = lfsck_object_get(obj);
281         llmv->ll_lmv = *lmv;
282         atomic_set(&llmv->ll_ref, 1);
283         lfsck->li_lmv = llmv;
284
285         RETURN(0);
286 }
287
288 /* LFSCK wrap functions */
289
290 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
291                        bool new_checked)
292 {
293         struct lfsck_component *com;
294
295         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
296                 com->lc_ops->lfsck_fail(env, com, new_checked);
297         }
298 }
299
300 void lfsck_close_dir(const struct lu_env *env,
301                      struct lfsck_instance *lfsck, int result)
302 {
303         struct lfsck_component *com;
304         ENTRY;
305
306         if (lfsck->li_lmv != NULL) {
307                 lfsck->li_lmv->ll_exit_value = result;
308                 if (lfsck->li_obj_dir != NULL) {
309                         list_for_each_entry(com, &lfsck->li_list_dir,
310                                             lc_link_dir) {
311                                 com->lc_ops->lfsck_close_dir(env, com);
312                         }
313                 }
314
315                 lfsck_lmv_put(env, lfsck->li_lmv);
316                 lfsck->li_lmv = NULL;
317         }
318
319         if (lfsck->li_di_dir != NULL) {
320                 const struct dt_it_ops  *dir_iops;
321                 struct dt_it            *dir_di   = lfsck->li_di_dir;
322
323                 LASSERT(lfsck->li_obj_dir != NULL);
324
325                 dir_iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
326                 lfsck_di_dir_put(env, lfsck);
327                 dir_iops->fini(env, dir_di);
328         }
329
330         if (lfsck->li_obj_dir != NULL) {
331                 struct dt_object        *dir_obj  = lfsck->li_obj_dir;
332
333                 lfsck->li_obj_dir = NULL;
334                 lfsck_object_put(env, dir_obj);
335         }
336
337         EXIT;
338 }
339
340 int lfsck_open_dir(const struct lu_env *env,
341                    struct lfsck_instance *lfsck, __u64 cookie)
342 {
343         struct dt_object        *obj    = lfsck->li_obj_dir;
344         struct dt_it            *di     = lfsck->li_di_dir;
345         struct lfsck_component  *com;
346         const struct dt_it_ops  *iops;
347         int                      rc     = 0;
348         ENTRY;
349
350         LASSERT(obj != NULL);
351         LASSERT(di == NULL);
352
353         if (unlikely(!dt_try_as_dir(env, obj, true)))
354                 GOTO(out, rc = -ENOTDIR);
355
356         list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
357                 rc = com->lc_ops->lfsck_open_dir(env, com);
358                 if (rc != 0)
359                         GOTO(out, rc);
360         }
361
362         iops = &obj->do_index_ops->dio_it;
363         di = iops->init(env, obj, lfsck->li_args_dir);
364         if (IS_ERR(di))
365                 GOTO(out, rc = PTR_ERR(di));
366
367         rc = iops->load(env, di, cookie);
368         if (rc == -ENODATA)
369                 rc = 1;
370         else if (rc == 0 || (rc > 0 && cookie > 0))
371                 rc = iops->next(env, di);
372         else if (rc > 0)
373                 rc = 0;
374
375         if (rc != 0) {
376                 iops->put(env, di);
377                 iops->fini(env, di);
378         } else {
379                 lfsck->li_cookie_dir = iops->store(env, di);
380                 spin_lock(&lfsck->li_lock);
381                 lfsck->li_di_dir = di;
382                 spin_unlock(&lfsck->li_lock);
383         }
384
385         GOTO(out, rc);
386
387 out:
388         if (rc != 0)
389                 lfsck_close_dir(env, lfsck, rc);
390
391         return rc;
392 }
393
394 static int lfsck_checkpoint(const struct lu_env *env,
395                             struct lfsck_instance *lfsck)
396 {
397         struct lfsck_component *com;
398         int                     rc  = 0;
399         int                     rc1 = 0;
400
401         if (likely(ktime_get_seconds() <= lfsck->li_time_next_checkpoint))
402                 return 0;
403
404         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
405         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
406                 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
407                 if (rc != 0)
408                         rc1 = rc;
409         }
410
411         lfsck->li_time_last_checkpoint = ktime_get_seconds();
412         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
413                                          LFSCK_CHECKPOINT_INTERVAL;
414         return rc1 != 0 ? rc1 : rc;
415 }
416
417 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
418                       struct lfsck_start_param *lsp)
419 {
420         struct dt_object       *obj     = NULL;
421         struct lfsck_component *com;
422         struct lfsck_component *next;
423         struct lfsck_position  *pos     = NULL;
424         const struct dt_it_ops *iops    =
425                                 &lfsck->li_obj_oit->do_index_ops->dio_it;
426         int                     rc;
427         ENTRY;
428
429         LASSERT(lfsck->li_obj_dir == NULL);
430         LASSERT(lfsck->li_di_dir == NULL);
431
432         lfsck->li_current_oit_processed = 0;
433         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
434                 com->lc_new_checked = 0;
435                 rc = com->lc_ops->lfsck_prep(env, com, lsp);
436                 if (rc != 0)
437                         GOTO(out, rc);
438
439                 if ((pos == NULL) ||
440                     (!lfsck_pos_is_zero(&com->lc_pos_start) &&
441                      lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
442                         pos = &com->lc_pos_start;
443         }
444
445         /* Init otable-based iterator. */
446         if (pos == NULL) {
447                 rc = iops->load(env, lfsck->li_di_oit, 0);
448                 if (rc > 0 || unlikely(rc == -ENODATA)) {
449                         lfsck->li_oit_over = 1;
450                         rc = 0;
451                 }
452
453                 GOTO(out, rc);
454         }
455
456         rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
457         if (rc > 0 || unlikely(rc == -ENODATA))
458                 lfsck->li_oit_over = 1;
459         else if (rc < 0)
460                 GOTO(out, rc);
461
462         if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
463                 GOTO(out, rc = 0);
464
465         /* Find the directory for namespace-based traverse. */
466         obj = lfsck_object_find_bottom(env, lfsck, &pos->lp_dir_parent);
467         if (IS_ERR(obj))
468                 RETURN(PTR_ERR(obj));
469
470         /* Remote directory will be scanned by the LFSCK instance
471          * on the MDT where the remote object really resides on. */
472         if (!dt_object_exists(obj) || dt_object_remote(obj) ||
473             unlikely(!S_ISDIR(lfsck_object_type(obj))))
474                 GOTO(out, rc = 0);
475
476         rc = lfsck_load_stripe_lmv(env, lfsck, obj);
477         if (rc == 0) {
478                 /* For the master MDT-object of a striped directory,
479                  * reset the iteration from the directory beginning. */
480                 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
481                         pos->lp_dir_cookie = 0;
482
483                 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
484                 if (rc > 0)
485                         /* The end of the directory. */
486                         rc = 0;
487         }
488
489         GOTO(out, rc);
490
491 out:
492         if (obj != NULL)
493                 lfsck_object_put(env, obj);
494
495         if (rc != 0) {
496                 lfsck_close_dir(env, lfsck, rc);
497                 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
498                                          lc_link) {
499                         com->lc_ops->lfsck_post(env, com, rc, true);
500                 }
501
502                 return rc;
503         }
504
505         rc = 0;
506         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
507         lfsck->li_pos_current = lfsck->li_pos_checkpoint;
508         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
509                 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
510                 if (rc != 0)
511                         break;
512         }
513
514         lfsck->li_time_last_checkpoint = ktime_get_seconds();
515         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
516                                          LFSCK_CHECKPOINT_INTERVAL;
517         return rc;
518 }
519
520 static int lfsck_exec_oit(const struct lu_env *env,
521                           struct lfsck_instance *lfsck, struct dt_object *obj)
522 {
523         struct lfsck_component *com;
524         int                     rc;
525         ENTRY;
526
527         LASSERT(lfsck->li_obj_dir == NULL);
528
529         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
530                 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
531                 if (rc != 0)
532                         RETURN(rc);
533         }
534
535         rc = lfsck_needs_scan_dir(env, lfsck, obj);
536         if (rc <= 0)
537                 GOTO(out, rc);
538
539         rc = lfsck_load_stripe_lmv(env, lfsck, obj);
540         if (rc == 0)
541                 rc = lfsck_open_dir(env, lfsck, 0);
542
543         GOTO(out, rc);
544
545 out:
546         if (rc < 0)
547                 lfsck_fail(env, lfsck, false);
548
549         if (rc != 0)
550                 lfsck_close_dir(env, lfsck, rc);
551
552         return rc > 0 ? 0 : rc;
553 }
554
555 static int lfsck_exec_dir(const struct lu_env *env,
556                           struct lfsck_instance *lfsck,
557                           struct lfsck_assistant_object *lso,
558                           struct lu_dirent *ent, __u16 type)
559 {
560         struct lfsck_component *com;
561         int                     rc;
562
563         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
564                 rc = com->lc_ops->lfsck_exec_dir(env, com, lso, ent, type);
565                 if (rc != 0)
566                         return rc;
567         }
568         return 0;
569 }
570
571 static int lfsck_master_dir_engine(const struct lu_env *env,
572                                    struct lfsck_instance *lfsck);
573
574 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
575                       int result)
576 {
577         struct lfsck_component *com;
578         struct lfsck_component *next;
579         int                     rc  = result;
580
581         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
582         lfsck_close_dir(env, lfsck, result);
583
584         while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
585                !list_empty(&lfsck->li_list_lmv)) {
586                 struct lfsck_lmv_unit *llu;
587
588                 spin_lock(&lfsck->li_lock);
589                 llu = list_first_entry(&lfsck->li_list_lmv,
590                                        struct lfsck_lmv_unit, llu_link);
591                 list_del_init(&llu->llu_link);
592                 spin_unlock(&lfsck->li_lock);
593
594                 lfsck->li_lmv = &llu->llu_lmv;
595                 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
596                 rc = lfsck_open_dir(env, lfsck, 0);
597                 if (rc == 0) {
598                         rc = lfsck_master_dir_engine(env, lfsck);
599                         lfsck_close_dir(env, lfsck, result);
600                 }
601         }
602
603         result = rc;
604
605         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
606                 rc = com->lc_ops->lfsck_post(env, com, result, false);
607                 if (rc != 0)
608                         CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
609                                "rc = %d\n", lfsck_lfsck2name(lfsck),
610                                (__u32)com->lc_type, rc);
611         }
612
613         lfsck->li_time_last_checkpoint = ktime_get_seconds();
614         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
615                                          LFSCK_CHECKPOINT_INTERVAL;
616
617         /* Ignore some component post failure to make other can go ahead. */
618         return result;
619 }
620
621 static int lfsck_double_scan(const struct lu_env *env,
622                              struct lfsck_instance *lfsck)
623 {
624         struct lfsck_component *com;
625         struct lfsck_component *next;
626         int                     rc  = 0;
627         int                     rc1 = 0;
628
629         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
630                 rc = com->lc_ops->lfsck_double_scan(env, com);
631                 if (rc != 0)
632                         rc1 = rc;
633         }
634
635         wait_event_idle(lfsck->li_thread.t_ctl_waitq,
636                         atomic_read(&lfsck->li_double_scan_count) == 0);
637
638         if (lfsck->li_status != LS_PAUSED &&
639             lfsck->li_status != LS_CO_PAUSED) {
640                 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
641                                          lc_link) {
642                         spin_lock(&lfsck->li_lock);
643                         list_move_tail(&com->lc_link, &lfsck->li_list_idle);
644                         spin_unlock(&lfsck->li_lock);
645                 }
646         }
647
648         return rc1 != 0 ? rc1 : rc;
649 }
650
651 static void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
652 {
653         struct lfsck_component *com;
654         struct lfsck_component *next;
655
656         list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
657                                  lc_link) {
658                 if (com->lc_ops->lfsck_quit != NULL)
659                         com->lc_ops->lfsck_quit(env, com);
660
661                 spin_lock(&lfsck->li_lock);
662                 list_del_init(&com->lc_link_dir);
663                 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
664                 spin_unlock(&lfsck->li_lock);
665         }
666
667         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
668                                  lc_link) {
669                 if (com->lc_ops->lfsck_quit != NULL)
670                         com->lc_ops->lfsck_quit(env, com);
671
672                 spin_lock(&lfsck->li_lock);
673                 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
674                 spin_unlock(&lfsck->li_lock);
675         }
676 }
677
678 /* LFSCK engines */
679
680 static int lfsck_master_dir_engine(const struct lu_env *env,
681                                    struct lfsck_instance *lfsck)
682 {
683         struct lfsck_thread_info        *info   = lfsck_env_info(env);
684         struct dt_object                *dir    = lfsck->li_obj_dir;
685         const struct dt_it_ops          *iops   = &dir->do_index_ops->dio_it;
686         struct dt_it                    *di     = lfsck->li_di_dir;
687         struct lu_dirent                *ent    =
688                         (struct lu_dirent *)info->lti_key;
689         struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
690         struct ptlrpc_thread            *thread = &lfsck->li_thread;
691         struct lfsck_assistant_object   *lso    = NULL;
692         int                              rc;
693         __u16                            type;
694         ENTRY;
695
696         do {
697                 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY2, cfs_fail_val) &&
698                     unlikely(!thread_is_running(thread))) {
699                         CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
700                                "parent "DFID", cookie %#llx\n",
701                                lfsck_lfsck2name(lfsck),
702                                PFID(lfsck_dto2fid(dir)), lfsck->li_cookie_dir);
703
704                         GOTO(out, rc = 0);
705                 }
706
707                 lfsck->li_new_scanned++;
708                 rc = iops->rec(env, di, (struct dt_rec *)ent,
709                                lfsck->li_args_dir);
710                 if (rc == 0)
711                         rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
712                                               &type);
713
714                 if (rc != 0) {
715                         CDEBUG(D_LFSCK, "%s: scan dir failed at rec(), "
716                                "parent "DFID", cookie %#llx: rc = %d\n",
717                                lfsck_lfsck2name(lfsck),
718                                PFID(lfsck_dto2fid(dir)),
719                                lfsck->li_cookie_dir, rc);
720                         lfsck_fail(env, lfsck, true);
721                         if (bk->lb_param & LPF_FAILOUT)
722                                 GOTO(out, rc);
723                         else
724                                 goto checkpoint;
725                 }
726
727                 if (ent->lde_attrs & LUDA_IGNORE)
728                         goto checkpoint;
729
730                 /* skip dot entry. */
731                 if (ent->lde_namelen == 1 && ent->lde_name[0] == '.')
732                         goto checkpoint;
733
734                 if (lso == NULL) {
735                         lso = lfsck_assistant_object_init(env,
736                                 lfsck_dto2fid(dir), NULL,
737                                 lfsck->li_pos_current.lp_oit_cookie, true);
738                         if (IS_ERR(lso)) {
739                                 if (bk->lb_param & LPF_FAILOUT)
740                                         RETURN(PTR_ERR(lso));
741
742                                 lso = NULL;
743                                 goto checkpoint;
744                         }
745                 }
746
747                 /* The type in the @ent structure may has been overwritten,
748                  * so we need to pass the @type parameter independently. */
749                 rc = lfsck_exec_dir(env, lfsck, lso, ent, type);
750                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
751                         GOTO(out, rc);
752
753 checkpoint:
754                 rc = lfsck_checkpoint(env, lfsck);
755                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
756                         GOTO(out, rc);
757
758                 /* Rate control. */
759                 lfsck_control_speed(lfsck);
760                 if (unlikely(!thread_is_running(thread))) {
761                         CDEBUG(D_LFSCK, "%s: scan dir exit for engine stop, "
762                                "parent "DFID", cookie %#llx\n",
763                                lfsck_lfsck2name(lfsck),
764                                PFID(lfsck_dto2fid(dir)),
765                                lfsck->li_cookie_dir);
766                         GOTO(out, rc = 0);
767                 }
768
769                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL2)) {
770                         spin_lock(&lfsck->li_lock);
771                         thread_set_flags(thread, SVC_STOPPING);
772                         spin_unlock(&lfsck->li_lock);
773                         GOTO(out, rc = -EINVAL);
774                 }
775
776                 rc = iops->next(env, di);
777                 if (rc < 0)
778                         CDEBUG(D_LFSCK, "%s dir engine fail to locate next "
779                                "for the directory "DFID": rc = %d\n",
780                                lfsck_lfsck2name(lfsck),
781                                PFID(&lfsck->li_pos_current.lp_dir_parent), rc);
782         } while (rc == 0);
783
784         if (rc > 0 && !lfsck->li_oit_over)
785                 lfsck_close_dir(env, lfsck, rc);
786
787         GOTO(out, rc);
788
789 out:
790         if (lso != NULL)
791                 lfsck_assistant_object_put(env, lso);
792
793         return rc;
794 }
795
796 /**
797  * Object-table based iteration engine.
798  *
799  * Object-table based iteration is the basic linear engine to scan all the
800  * objects on current device in turn. For each object, it calls all the
801  * registered LFSCK component(s)' API to perform related consistency
802  * verification.
803  *
804  * It flushes related LFSCK trace files to disk via making checkpoint
805  * periodically. Then if the server crashed or the LFSCK is paused, the
806  * LFSCK can resume from the latest checkpoint.
807  *
808  * It also controls the whole LFSCK speed via lfsck_control_speed() to
809  * avoid the server to become overload.
810  *
811  * \param[in] env       pointer to the thread context
812  * \param[in] lfsck     pointer to the lfsck instance
813  *
814  * \retval              positive number if all objects have been scanned
815  * \retval              0 if the iteration is stopped or paused
816  * \retval              negative error number on failure
817  */
818 static int lfsck_master_oit_engine(const struct lu_env *env,
819                                    struct lfsck_instance *lfsck)
820 {
821         struct lfsck_thread_info *info  = lfsck_env_info(env);
822         const struct dt_it_ops   *iops  =
823                                 &lfsck->li_obj_oit->do_index_ops->dio_it;
824         struct dt_it             *di    = lfsck->li_di_oit;
825         struct lu_fid            *fid   = &info->lti_fid;
826         struct lfsck_bookmark    *bk    = &lfsck->li_bookmark_ram;
827         struct ptlrpc_thread     *thread = &lfsck->li_thread;
828         struct seq_server_site   *ss    = lfsck_dev_site(lfsck);
829         __u32                    idx    = lfsck_dev_idx(lfsck);
830         int                      rc;
831         ENTRY;
832
833         if (unlikely(ss == NULL))
834                 RETURN(-EIO);
835
836         do {
837                 struct dt_object *target;
838
839                 if (lfsck->li_di_dir != NULL) {
840                         rc = lfsck_master_dir_engine(env, lfsck);
841                         if (rc <= 0)
842                                 RETURN(rc);
843                 }
844
845                 if (unlikely(lfsck->li_oit_over))
846                         RETURN(1);
847
848                 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY1, cfs_fail_val) &&
849                     unlikely(!thread_is_running(thread))) {
850                         CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
851                                "cookie %llu\n",
852                                lfsck_lfsck2name(lfsck), iops->store(env, di));
853
854                         RETURN(0);
855                 }
856
857                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
858                         RETURN(0);
859
860                 lfsck->li_current_oit_processed = 1;
861
862                 if (!list_empty(&lfsck->li_list_lmv)) {
863                         struct lfsck_lmv_unit *llu;
864
865                         spin_lock(&lfsck->li_lock);
866                         llu = list_first_entry(&lfsck->li_list_lmv,
867                                                struct lfsck_lmv_unit, llu_link);
868                         list_del_init(&llu->llu_link);
869                         spin_unlock(&lfsck->li_lock);
870
871                         lfsck->li_lmv = &llu->llu_lmv;
872                         lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
873                         rc = lfsck_open_dir(env, lfsck, 0);
874                         if (rc == 0)
875                                 rc = lfsck_master_dir_engine(env, lfsck);
876
877                         if (rc <= 0)
878                                 RETURN(rc);
879                 }
880
881                 lfsck->li_new_scanned++;
882                 lfsck->li_pos_current.lp_oit_cookie = iops->store(env, di);
883                 rc = iops->rec(env, di, (struct dt_rec *)fid, 0);
884                 if (rc != 0) {
885                         CDEBUG(D_LFSCK, "%s: OIT scan failed at rec(): "
886                                "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
887                         lfsck_fail(env, lfsck, true);
888                         if (rc < 0 && bk->lb_param & LPF_FAILOUT)
889                                 RETURN(rc);
890                         else
891                                 goto checkpoint;
892                 }
893
894                 if (unlikely(!fid_is_sane(fid))) {
895                         CDEBUG(D_LFSCK, "%s: OIT scan find invalid FID "DFID
896                                ", skip it\n",
897                                lfsck_lfsck2name(lfsck), PFID(fid));
898                         goto checkpoint;
899                 }
900
901                 if (fid_is_idif(fid)) {
902                         __u32 idx1 = fid_idif_ost_idx(fid);
903
904                         LASSERT(!lfsck->li_master);
905
906                         if (idx != idx1) {
907                                 struct ost_id *oi = &info->lti_oi;
908
909                                 if (unlikely(idx1 != 0)) {
910                                         CDEBUG(D_LFSCK, "%s: invalid IDIF "DFID
911                                                ", not match device index %u\n",
912                                                lfsck_lfsck2name(lfsck),
913                                                PFID(fid), idx);
914
915                                         goto checkpoint;
916                                 }
917
918                                 /* rebuild the IDIF with index to
919                                  * avoid double instances for the
920                                  * same object. */
921                                 fid_to_ostid(fid, oi);
922                                 ostid_to_fid(fid, oi, idx);
923                         }
924                 } else if (!fid_is_norm(fid) && !fid_is_igif(fid) &&
925                            !fid_is_last_id(fid) &&
926                            !lu_fid_eq(fid, &lfsck->li_global_root_fid)) {
927
928                         /* If the FID/object is only used locally and invisible
929                          * to external nodes, then LFSCK will not handle it.
930                          *
931                          * dot_lustre sequence has been handled specially. */
932                         goto checkpoint;
933                 } else {
934                         struct lu_seq_range *range = &info->lti_range;
935
936                         if (lfsck->li_master)
937                                 fld_range_set_mdt(range);
938                         else
939                                 fld_range_set_ost(range);
940                         rc = fld_local_lookup(env, ss->ss_server_fld,
941                                               fid_seq(fid), range);
942                         if (rc != 0 || range->lsr_index != idx) {
943                                 /* Remote object will be handled by the LFSCK
944                                  * instance on the MDT where the remote object
945                                  * really resides on. */
946                                 rc = 0;
947                                 goto checkpoint;
948                         }
949                 }
950
951                 target = lfsck_object_find_bottom(env, lfsck, fid);
952                 if (IS_ERR(target)) {
953                         CDEBUG(D_LFSCK, "%s: OIT scan failed at find target "
954                                DFID", cookie %llu: rc = %d\n",
955                                lfsck_lfsck2name(lfsck), PFID(fid),
956                                iops->store(env, di), rc);
957                         lfsck_fail(env, lfsck, true);
958                         if (bk->lb_param & LPF_FAILOUT)
959                                 RETURN(PTR_ERR(target));
960                         else
961                                 goto checkpoint;
962                 }
963
964                 if (dt_object_exists(target))
965                         rc = lfsck_exec_oit(env, lfsck, target);
966
967                 lfsck_object_put(env, target);
968                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
969                         RETURN(rc);
970
971 checkpoint:
972                 rc = lfsck_checkpoint(env, lfsck);
973                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
974                         RETURN(rc);
975
976                 /* Rate control. */
977                 lfsck_control_speed(lfsck);
978
979                 if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_FATAL1)) {
980                         spin_lock(&lfsck->li_lock);
981                         thread_set_flags(thread, SVC_STOPPING);
982                         spin_unlock(&lfsck->li_lock);
983                         RETURN(-EINVAL);
984                 }
985
986                 rc = iops->next(env, di);
987                 if (unlikely(rc > 0))
988                         lfsck->li_oit_over = 1;
989                 else if (likely(rc == 0))
990                         lfsck->li_current_oit_processed = 0;
991                 else
992                         CDEBUG(D_LFSCK, "%s oit engine fail to locate next at "
993                                "%llu: rc = %d\n", lfsck_lfsck2name(lfsck),
994                                iops->store(env, di), rc);
995
996                 if (unlikely(!thread_is_running(thread))) {
997                         CDEBUG(D_LFSCK, "%s: OIT scan exit for engine stop, "
998                                "cookie %llu\n", lfsck_lfsck2name(lfsck),
999                                iops->store(env, di));
1000                         RETURN(0);
1001                 }
1002         } while (rc == 0 || lfsck->li_di_dir != NULL);
1003
1004         RETURN(rc);
1005 }
1006
1007 int lfsck_master_engine(void *args)
1008 {
1009         struct lfsck_thread_args *lta      = args;
1010         struct lu_env            *env      = &lta->lta_env;
1011         struct lfsck_instance    *lfsck    = lta->lta_lfsck;
1012         struct ptlrpc_thread     *thread   = &lfsck->li_thread;
1013         struct dt_object         *oit_obj  = lfsck->li_obj_oit;
1014         const struct dt_it_ops   *oit_iops = &oit_obj->do_index_ops->dio_it;
1015         struct dt_it             *oit_di;
1016         int                       rc;
1017         ENTRY;
1018
1019         /*
1020          * thread is spawned with all signals set to SIG_IGN, re-enable
1021          * SIGINT for lfsck_stop() to awaken and stop the thread.
1022          */
1023         allow_signal(SIGINT);
1024         spin_lock(&lfsck->li_lock);
1025         lfsck->li_task = current;
1026         spin_unlock(&lfsck->li_lock);
1027
1028         /* There will be some objects verification during the LFSCK start,
1029          * such as the subsequent lfsck_verify_lpf(). Trigger low layer OI
1030          * OI scrub before that to handle the potential inconsistence. */
1031         oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit);
1032         if (IS_ERR(oit_di)) {
1033                 rc = PTR_ERR(oit_di);
1034                 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1035                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1036
1037                 GOTO(fini_args, rc);
1038         }
1039
1040         if (lfsck->li_master &&
1041             (!list_empty(&lfsck->li_list_scan) ||
1042              !list_empty(&lfsck->li_list_double_scan))) {
1043                 rc = lfsck_verify_lpf(env, lfsck);
1044                 /* FIXME: once OI files are missing, this will fail, it should
1045                  * return error, but to satisfy sanity-lfsck test 4 & 5, leave
1046                  * it uninitialized here, and any code dereference it need to
1047                  * check.
1048                  */
1049                 if (rc != 0)
1050                         CERROR("%s: master engine fail to verify the "
1051                                ".lustre/lost+found/, go ahead: rc = %d\n",
1052                                lfsck_lfsck2name(lfsck), rc);
1053         }
1054
1055         spin_lock(&lfsck->li_lock);
1056         lfsck->li_di_oit = oit_di;
1057         spin_unlock(&lfsck->li_lock);
1058         rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1059         if (rc != 0)
1060                 GOTO(fini_oit, rc);
1061
1062         CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1063                "oit_cookie = %llu, dir_cookie = %#llx, parent = "DFID
1064                ", pid = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1065                lfsck->li_pos_checkpoint.lp_oit_cookie,
1066                lfsck->li_pos_checkpoint.lp_dir_cookie,
1067                PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1068                current->pid);
1069
1070         spin_lock(&lfsck->li_lock);
1071         if (unlikely(!thread_is_starting(thread))) {
1072                 spin_unlock(&lfsck->li_lock);
1073                 GOTO(fini_oit, rc = 0);
1074         }
1075
1076         thread_set_flags(thread, SVC_RUNNING);
1077         spin_unlock(&lfsck->li_lock);
1078         wake_up(&thread->t_ctl_waitq);
1079
1080         wait_event_idle(thread->t_ctl_waitq,
1081                         lfsck->li_start_unplug ||
1082                         !thread_is_running(thread));
1083         if (!thread_is_running(thread))
1084                 GOTO(fini_oit, rc = 0);
1085
1086         if (!list_empty(&lfsck->li_list_scan) ||
1087             list_empty(&lfsck->li_list_double_scan))
1088                 rc = lfsck_master_oit_engine(env, lfsck);
1089         else
1090                 rc = 1;
1091
1092         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
1093         CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1094                "oit_cookie = %llu, dir_cookie = %#llx, parent = "DFID
1095                ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1096                lfsck->li_pos_checkpoint.lp_oit_cookie,
1097                lfsck->li_pos_checkpoint.lp_dir_cookie,
1098                PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1099                current->pid, rc);
1100
1101         if (!CFS_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1102                 rc = lfsck_post(env, lfsck, rc);
1103         else
1104                 lfsck_close_dir(env, lfsck, rc);
1105
1106 fini_oit:
1107         lfsck_di_oit_put(env, lfsck);
1108         oit_iops->fini(env, oit_di);
1109         if (rc == 1) {
1110                 if (!list_empty(&lfsck->li_list_double_scan))
1111                         rc = lfsck_double_scan(env, lfsck);
1112                 else
1113                         rc = 0;
1114         } else {
1115                 lfsck_quit(env, lfsck);
1116         }
1117
1118         /* XXX: Purge the pinned objects in the future. */
1119
1120 fini_args:
1121         spin_lock(&lfsck->li_lock);
1122         thread_set_flags(thread, SVC_STOPPED);
1123         lfsck->li_task = NULL;
1124         spin_unlock(&lfsck->li_lock);
1125         wake_up(&thread->t_ctl_waitq);
1126         lfsck_thread_args_fini(lta);
1127         return rc;
1128 }
1129
1130 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1131 {
1132         bool empty = false;
1133
1134         spin_lock(&lad->lad_lock);
1135         if (list_empty(&lad->lad_req_list))
1136                 empty = true;
1137         spin_unlock(&lad->lad_lock);
1138
1139         return empty;
1140 }
1141
1142 /**
1143  * Query the LFSCK status from the instatnces on remote servers.
1144  *
1145  * The LFSCK assistant thread queries the LFSCK instances on other
1146  * servers (MDT/OST) about their status, such as whether they have
1147  * finished the phase1/phase2 scanning or not, and so on.
1148  *
1149  * \param[in] env       pointer to the thread context
1150  * \param[in] com       pointer to the lfsck component
1151  *
1152  * \retval              0 for success
1153  * \retval              negative error number on failure
1154  */
1155 static int lfsck_assistant_query_others(const struct lu_env *env,
1156                                         struct lfsck_component *com)
1157 {
1158         struct lfsck_thread_info          *info  = lfsck_env_info(env);
1159         struct lfsck_request              *lr    = &info->lti_lr;
1160         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
1161         struct lfsck_instance             *lfsck = com->lc_lfsck;
1162         struct lfsck_assistant_data       *lad   = com->lc_data;
1163         struct ptlrpc_request_set         *set;
1164         struct lfsck_tgt_descs            *ltds;
1165         struct lfsck_tgt_desc             *ltd;
1166         struct list_head                  *phase_head;
1167         int                                rc    = 0;
1168         int                                rc1   = 0;
1169         ENTRY;
1170
1171         set = ptlrpc_prep_set();
1172         if (set == NULL)
1173                 RETURN(-ENOMEM);
1174
1175         lad->lad_touch_gen++;
1176         memset(lr, 0, sizeof(*lr));
1177         lr->lr_event = LE_QUERY;
1178         lr->lr_active = com->lc_type;
1179
1180         memset(laia, 0, sizeof(*laia));
1181         laia->laia_com = com;
1182         laia->laia_lr = lr;
1183
1184         if (!list_empty(&lad->lad_mdt_phase1_list)) {
1185                 ltds = &lfsck->li_mdt_descs;
1186                 lr->lr_flags = 0;
1187                 phase_head = &lad->lad_mdt_phase1_list;
1188         } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1189                 goto out;
1190         } else {
1191
1192 again:
1193                 ltds = &lfsck->li_ost_descs;
1194                 lr->lr_flags = LEF_TO_OST;
1195                 phase_head = &lad->lad_ost_phase1_list;
1196         }
1197
1198         laia->laia_ltds = ltds;
1199         spin_lock(&ltds->ltd_lock);
1200         while (!list_empty(phase_head)) {
1201                 struct list_head *phase_list;
1202                 __u32            *gen;
1203
1204                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1205                         ltd = list_first_entry(phase_head,
1206                                                struct lfsck_tgt_desc,
1207                                                ltd_layout_phase_list);
1208                         phase_list = &ltd->ltd_layout_phase_list;
1209                         gen = &ltd->ltd_layout_gen;
1210                 } else {
1211                         ltd = list_first_entry(phase_head,
1212                                                struct lfsck_tgt_desc,
1213                                                ltd_namespace_phase_list);
1214                         phase_list = &ltd->ltd_namespace_phase_list;
1215                         gen = &ltd->ltd_namespace_gen;
1216                 }
1217
1218                 if (*gen == lad->lad_touch_gen)
1219                         break;
1220
1221                 *gen = lad->lad_touch_gen;
1222                 list_move_tail(phase_list, phase_head);
1223                 atomic_inc(&ltd->ltd_ref);
1224                 laia->laia_ltd = ltd;
1225                 spin_unlock(&ltds->ltd_lock);
1226                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1227                                          lfsck_async_interpret_common,
1228                                          laia, LFSCK_QUERY);
1229                 if (rc != 0) {
1230                         CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1231                                "%s %x for %s: rc = %d\n",
1232                                lfsck_lfsck2name(lfsck),
1233                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1234                                ltd->ltd_index, lad->lad_name, rc);
1235                         lfsck_tgt_put(ltd);
1236                         rc1 = rc;
1237                 }
1238                 spin_lock(&ltds->ltd_lock);
1239         }
1240         spin_unlock(&ltds->ltd_lock);
1241
1242         rc = ptlrpc_set_wait(env, set);
1243         if (rc < 0) {
1244                 ptlrpc_set_destroy(set);
1245                 RETURN(rc);
1246         }
1247
1248         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1249             list_empty(&lad->lad_mdt_phase1_list))
1250                 goto again;
1251
1252 out:
1253         ptlrpc_set_destroy(set);
1254
1255         RETURN(rc1 != 0 ? rc1 : rc);
1256 }
1257
1258 /**
1259  * Notify the LFSCK event to the instances on remote servers.
1260  *
1261  * The LFSCK assistant thread notifies the LFSCK instances on other
1262  * servers (MDT/OST) about some events, such as start new scanning,
1263  * stop the scanning, this LFSCK instance will exit, and so on.
1264  *
1265  * \param[in] env       pointer to the thread context
1266  * \param[in] com       pointer to the lfsck component
1267  * \param[in] lr        pointer to the LFSCK event request
1268  *
1269  * \retval              0 for success
1270  * \retval              negative error number on failure
1271  */
1272 static int lfsck_assistant_notify_others(const struct lu_env *env,
1273                                          struct lfsck_component *com,
1274                                          struct lfsck_request *lr)
1275 {
1276         struct lfsck_thread_info *info = lfsck_env_info(env);
1277         struct lfsck_async_interpret_args *laia = &info->lti_laia;
1278         struct lfsck_instance *lfsck = com->lc_lfsck;
1279         struct lfsck_assistant_data *lad = com->lc_data;
1280         struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1281         struct ptlrpc_request_set *set;
1282         struct lfsck_tgt_descs *ltds;
1283         struct lfsck_tgt_desc *ltd;
1284         struct lfsck_tgt_desc *next;
1285         int idx;
1286         int rc = 0;
1287         int rc1 = 0;
1288
1289         ENTRY;
1290         set = ptlrpc_prep_set();
1291         if (set == NULL)
1292                 RETURN(-ENOMEM);
1293
1294         lr->lr_index = lfsck_dev_idx(lfsck);
1295         lr->lr_active = com->lc_type;
1296
1297         memset(laia, 0, sizeof(*laia));
1298         laia->laia_com = com;
1299         laia->laia_lr = lr;
1300
1301         switch (lr->lr_event) {
1302         case LE_START:
1303                 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1304                         goto next;
1305
1306                 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN;
1307                 lr->lr_speed = bk->lb_speed_limit;
1308                 lr->lr_version = bk->lb_version;
1309                 lr->lr_param |= bk->lb_param;
1310                 lr->lr_async_windows = bk->lb_async_windows;
1311                 lr->lr_flags = LEF_TO_OST;
1312
1313                 /* Notify OSTs firstly, then handle other MDTs if needed. */
1314                 ltds = &lfsck->li_ost_descs;
1315                 laia->laia_ltds = ltds;
1316                 down_read(&ltds->ltd_rw_sem);
1317                 for_each_set_bit(idx, ltds->ltd_tgts_bitmap,
1318                                  ltds->ltd_tgts_mask_len) {
1319                         ltd = lfsck_tgt_get(ltds, idx);
1320                         LASSERT(ltd != NULL);
1321
1322                         laia->laia_ltd = ltd;
1323                         rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1324                                         lfsck_async_interpret_common,
1325                                         laia, LFSCK_NOTIFY);
1326                         if (rc != 0) {
1327                                 lfsck_lad_set_bitmap(env, com, idx);
1328                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1329                                        "notify OST %x for %s start: rc = %d\n",
1330                                        lfsck_lfsck2name(lfsck), idx,
1331                                        lad->lad_name, rc);
1332                                 lfsck_tgt_put(ltd);
1333                         }
1334                 }
1335                 up_read(&ltds->ltd_rw_sem);
1336
1337                 /* Sync up */
1338                 rc = ptlrpc_set_wait(env, set);
1339                 if (rc < 0) {
1340                         ptlrpc_set_destroy(set);
1341                         RETURN(rc);
1342                 }
1343
1344 next:
1345                 if (!(bk->lb_param & LPF_ALL_TGT))
1346                         break;
1347
1348                 /* link other MDT targets locallly. */
1349                 ltds = &lfsck->li_mdt_descs;
1350                 spin_lock(&ltds->ltd_lock);
1351                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1352                         for_each_set_bit(idx, ltds->ltd_tgts_bitmap,
1353                                          ltds->ltd_tgts_mask_len) {
1354                                 ltd = lfsck_ltd2tgt(ltds, idx);
1355                                 LASSERT(ltd != NULL);
1356
1357                                 if (!list_empty(&ltd->ltd_layout_list))
1358                                         continue;
1359
1360                                 list_add_tail(&ltd->ltd_layout_list,
1361                                               &lad->lad_mdt_list);
1362                                 list_add_tail(&ltd->ltd_layout_phase_list,
1363                                               &lad->lad_mdt_phase1_list);
1364                         }
1365                 } else {
1366                         for_each_set_bit(idx, ltds->ltd_tgts_bitmap,
1367                                          ltds->ltd_tgts_mask_len) {
1368                                 ltd = lfsck_ltd2tgt(ltds, idx);
1369                                 LASSERT(ltd != NULL);
1370
1371                                 if (!list_empty(&ltd->ltd_namespace_list))
1372                                         continue;
1373
1374                                 list_add_tail(&ltd->ltd_namespace_list,
1375                                               &lad->lad_mdt_list);
1376                                 list_add_tail(&ltd->ltd_namespace_phase_list,
1377                                               &lad->lad_mdt_phase1_list);
1378                         }
1379                 }
1380                 spin_unlock(&ltds->ltd_lock);
1381                 break;
1382         case LE_STOP:
1383         case LE_PHASE2_DONE:
1384         case LE_PEER_EXIT: {
1385                 struct list_head *phase_head;
1386
1387                 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1388                 if (bk->lb_param & LPF_ALL_TGT) {
1389                         phase_head = &lad->lad_mdt_list;
1390                         ltds = &lfsck->li_mdt_descs;
1391                         if (lr->lr_event == LE_STOP) {
1392                                 /* unlink other MDT targets locallly. */
1393                                 spin_lock(&ltds->ltd_lock);
1394                                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1395                                         list_for_each_entry_safe(ltd, next,
1396                                                 phase_head, ltd_layout_list) {
1397                                                 list_del_init(
1398                                                 &ltd->ltd_layout_phase_list);
1399                                                 list_del_init(
1400                                                 &ltd->ltd_layout_list);
1401                                         }
1402                                 } else {
1403                                         list_for_each_entry_safe(ltd, next,
1404                                                         phase_head,
1405                                                         ltd_namespace_list) {
1406                                                 list_del_init(
1407                                                 &ltd->ltd_namespace_phase_list);
1408                                                 list_del_init(
1409                                                 &ltd->ltd_namespace_list);
1410                                         }
1411                                 }
1412                                 spin_unlock(&ltds->ltd_lock);
1413
1414                                 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1415                                         break;
1416
1417                                 lr->lr_flags |= LEF_TO_OST;
1418                                 phase_head = &lad->lad_ost_list;
1419                                 ltds = &lfsck->li_ost_descs;
1420                         } else {
1421                                 lr->lr_flags &= ~LEF_TO_OST;
1422                         }
1423                 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1424                         break;
1425                 } else {
1426                         lr->lr_flags |= LEF_TO_OST;
1427                         phase_head = &lad->lad_ost_list;
1428                         ltds = &lfsck->li_ost_descs;
1429                 }
1430
1431 again:
1432                 laia->laia_ltds = ltds;
1433                 spin_lock(&ltds->ltd_lock);
1434                 while (!list_empty(phase_head)) {
1435                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1436                                 ltd = list_first_entry(phase_head,
1437                                                        struct lfsck_tgt_desc,
1438                                                        ltd_layout_list);
1439                                 if (!list_empty(&ltd->ltd_layout_phase_list))
1440                                         list_del_init(
1441                                                 &ltd->ltd_layout_phase_list);
1442                                 list_del_init(&ltd->ltd_layout_list);
1443                         } else {
1444                                 ltd = list_first_entry(phase_head,
1445                                                        struct lfsck_tgt_desc,
1446                                                        ltd_namespace_list);
1447                                 if (!list_empty(&ltd->ltd_namespace_phase_list))
1448                                         list_del_init(
1449                                                 &ltd->ltd_namespace_phase_list);
1450                                 list_del_init(&ltd->ltd_namespace_list);
1451                         }
1452                         atomic_inc(&ltd->ltd_ref);
1453                         laia->laia_ltd = ltd;
1454                         spin_unlock(&ltds->ltd_lock);
1455                         rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1456                                         lfsck_async_interpret_common,
1457                                         laia, LFSCK_NOTIFY);
1458                         if (rc != 0) {
1459                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1460                                        "notify %s %x for %s stop/phase2_done/"
1461                                        "peer_exit: rc = %d\n",
1462                                        lfsck_lfsck2name(lfsck),
1463                                        (lr->lr_flags & LEF_TO_OST) ?
1464                                        "OST" : "MDT", ltd->ltd_index,
1465                                        lad->lad_name, rc);
1466                                 lfsck_tgt_put(ltd);
1467                         }
1468                         spin_lock(&ltds->ltd_lock);
1469                 }
1470                 spin_unlock(&ltds->ltd_lock);
1471
1472                 rc = ptlrpc_set_wait(env, set);
1473                 if (rc < 0) {
1474                         ptlrpc_set_destroy(set);
1475                         RETURN(rc);
1476                 }
1477
1478                 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1479                     !(lr->lr_flags & LEF_TO_OST)) {
1480                         lr->lr_flags |= LEF_TO_OST;
1481                         phase_head = &lad->lad_ost_list;
1482                         ltds = &lfsck->li_ost_descs;
1483                         goto again;
1484                 }
1485                 break;
1486         }
1487         case LE_PHASE1_DONE:
1488                 lad->lad_ops->la_sync_failures(env, com, lr);
1489                 lad->lad_touch_gen++;
1490                 ltds = &lfsck->li_mdt_descs;
1491                 laia->laia_ltds = ltds;
1492                 spin_lock(&ltds->ltd_lock);
1493                 while (!list_empty(&lad->lad_mdt_list)) {
1494                         struct list_head *list;
1495                         __u32            *gen;
1496
1497                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1498                                 ltd = list_first_entry(&lad->lad_mdt_list,
1499                                                        struct lfsck_tgt_desc,
1500                                                        ltd_layout_list);
1501                                 list = &ltd->ltd_layout_list;
1502                                 gen = &ltd->ltd_layout_gen;
1503                         } else {
1504                                 struct lfsck_namespace *ns = com->lc_file_ram;
1505
1506                                 ltd = list_first_entry(&lad->lad_mdt_list,
1507                                                        struct lfsck_tgt_desc,
1508                                                        ltd_namespace_list);
1509                                 list = &ltd->ltd_namespace_list;
1510                                 gen = &ltd->ltd_namespace_gen;
1511                                 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1512                         }
1513
1514                         if (*gen == lad->lad_touch_gen)
1515                                 break;
1516
1517                         *gen = lad->lad_touch_gen;
1518                         list_move_tail(list, &lad->lad_mdt_list);
1519                         if (ltd->ltd_synced_failures)
1520                                 continue;
1521
1522                         atomic_inc(&ltd->ltd_ref);
1523                         laia->laia_ltd = ltd;
1524                         spin_unlock(&ltds->ltd_lock);
1525                         rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1526                                         lfsck_async_interpret_common,
1527                                         laia, LFSCK_NOTIFY);
1528                         if (rc != 0) {
1529                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1530                                        "notify MDT %x for %s phase1 done: "
1531                                        "rc = %d\n", lfsck_lfsck2name(lfsck),
1532                                        ltd->ltd_index, lad->lad_name, rc);
1533                                 lfsck_tgt_put(ltd);
1534                         }
1535                         spin_lock(&ltds->ltd_lock);
1536                 }
1537                 spin_unlock(&ltds->ltd_lock);
1538                 break;
1539         default:
1540                 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1541                        "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1542                 rc = -EINVAL;
1543                 break;
1544         }
1545
1546         rc1 = ptlrpc_set_wait(env, set);
1547         ptlrpc_set_destroy(set);
1548
1549         RETURN(rc != 0 ? rc : rc1);
1550 }
1551
1552 /**
1553  * The LFSCK assistant thread is triggered by the LFSCK main engine.
1554  * They co-work together as an asynchronous pipeline: the LFSCK main
1555  * engine scans the system and pre-fetches the objects, attributes,
1556  * or name entries, etc, and pushes them into the pipeline as input
1557  * requests for the LFSCK assistant thread; on the other end of the
1558  * pipeline, the LFSCK assistant thread performs the real check and
1559  * repair for every request from the main engine.
1560  *
1561  * Generally, the assistant engine may be blocked when check/repair
1562  * something, so the LFSCK main engine will run some faster. On the
1563  * other hand, the LFSCK main engine will drive multiple assistant
1564  * threads in parallel, means for each LFSCK component on the master
1565  * (such as layout LFSCK, namespace LFSCK), there is an independent
1566  * LFSCK assistant thread. So under such 1:N multiple asynchronous
1567  * pipelines mode, the whole LFSCK performance will be much better
1568  * than check/repair everything by the LFSCK main engine itself.
1569  */
1570 int lfsck_assistant_engine(void *args)
1571 {
1572         struct lfsck_thread_args          *lta     = args;
1573         struct lu_env                     *env     = &lta->lta_env;
1574         struct lfsck_component            *com     = lta->lta_com;
1575         struct lfsck_instance             *lfsck   = lta->lta_lfsck;
1576         struct lfsck_bookmark             *bk      = &lfsck->li_bookmark_ram;
1577         struct lfsck_position             *pos     = &com->lc_pos_start;
1578         struct lfsck_thread_info          *info    = lfsck_env_info(env);
1579         struct lfsck_request              *lr      = &info->lti_lr;
1580         struct lfsck_assistant_data       *lad     = com->lc_data;
1581         struct ptlrpc_thread              *mthread = &lfsck->li_thread;
1582         struct ptlrpc_thread              *athread = &lad->lad_thread;
1583         const struct lfsck_assistant_operations *lao     = lad->lad_ops;
1584         struct lfsck_assistant_req        *lar;
1585         int                                rc      = 0;
1586         int                                rc1     = 0;
1587         int                                rc2;
1588         ENTRY;
1589
1590         CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1591                lfsck_lfsck2name(lfsck), lad->lad_name);
1592
1593         memset(lr, 0, sizeof(*lr));
1594         lr->lr_event = LE_START;
1595         if (pos->lp_oit_cookie <= 1)
1596                 lr->lr_param = LPF_RESET;
1597         rc = lfsck_assistant_notify_others(env, com, lr);
1598         if (rc != 0) {
1599                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1600                        "to start %s: rc = %d\n",
1601                        lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1602                 GOTO(fini, rc);
1603         }
1604
1605         /*
1606          * thread is spawned with all signals set to SIG_IGN, re-enable
1607          * SIGINT for lfsck_stop() to awaken and stop the thread.
1608          */
1609         allow_signal(SIGINT);
1610         spin_lock(&lad->lad_lock);
1611         lad->lad_task = current;
1612         thread_set_flags(athread, SVC_RUNNING);
1613         spin_unlock(&lad->lad_lock);
1614         wake_up(&mthread->t_ctl_waitq);
1615
1616         while (1) {
1617                 while (!list_empty(&lad->lad_req_list)) {
1618                         bool wakeup = false;
1619
1620                         if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags) ||
1621                                      !thread_is_running(mthread)))
1622                                 GOTO(cleanup, rc = lad->lad_post_result);
1623
1624                         lar = list_first_entry(&lad->lad_req_list,
1625                                                struct lfsck_assistant_req,
1626                                                lar_list);
1627                         /* Only the lfsck_assistant_engine thread itself can
1628                          * remove the "lar" from the head of the list, LFSCK
1629                          * engine thread only inserts other new "lar" at the
1630                          * end of the list. So it is safe to handle current
1631                          * "lar" without the spin_lock. */
1632                         rc = lao->la_handler_p1(env, com, lar);
1633                         spin_lock(&lad->lad_lock);
1634                         list_del_init(&lar->lar_list);
1635                         lad->lad_prefetched--;
1636                         /* Wake up the main engine thread only when the list
1637                          * is empty or half of the prefetched items have been
1638                          * handled to avoid too frequent thread schedule. */
1639                         if (lad->lad_prefetched <= (bk->lb_async_windows / 2))
1640                                 wakeup = true;
1641                         spin_unlock(&lad->lad_lock);
1642                         if (wakeup)
1643                                 wake_up(&mthread->t_ctl_waitq);
1644
1645                         lao->la_req_fini(env, lar);
1646                         if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1647                                 GOTO(cleanup, rc);
1648                 }
1649
1650                 wait_event_idle(athread->t_ctl_waitq,
1651                                 !lfsck_assistant_req_empty(lad) ||
1652                                 test_bit(LAD_EXIT, &lad->lad_flags) ||
1653                                 test_bit(LAD_TO_POST, &lad->lad_flags) ||
1654                                 test_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags));
1655
1656                 if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags)))
1657                         GOTO(cleanup, rc = lad->lad_post_result);
1658
1659                 if (!list_empty(&lad->lad_req_list))
1660                         continue;
1661
1662                 if (test_bit(LAD_TO_POST, &lad->lad_flags)) {
1663                         CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1664                                lfsck_lfsck2name(lfsck), lad->lad_name);
1665
1666                         if (unlikely(test_bit(LAD_EXIT, &lad->lad_flags)))
1667                                 GOTO(cleanup, rc = lad->lad_post_result);
1668
1669                         clear_bit(LAD_TO_POST, &lad->lad_flags);
1670                         LASSERT(lad->lad_post_result > 0);
1671
1672                         /* Wakeup the master engine to go ahead. */
1673                         wake_up(&mthread->t_ctl_waitq);
1674
1675                         memset(lr, 0, sizeof(*lr));
1676                         lr->lr_event = LE_PHASE1_DONE;
1677                         lr->lr_status = lad->lad_post_result;
1678                         rc = lfsck_assistant_notify_others(env, com, lr);
1679
1680                         CDEBUG(D_LFSCK, "%s: LFSCK assistant notified "
1681                                "others for %s post: rc = %d\n",
1682                                lfsck_lfsck2name(lfsck),
1683                                lad->lad_name, rc);
1684                 }
1685
1686                 if (test_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags)) {
1687                         clear_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
1688                         atomic_inc(&lfsck->li_double_scan_count);
1689                         set_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags);
1690                         wake_up(&mthread->t_ctl_waitq);
1691
1692                         com->lc_new_checked = 0;
1693                         com->lc_new_scanned = 0;
1694                         com->lc_time_last_checkpoint = ktime_get_seconds();
1695                         com->lc_time_next_checkpoint =
1696                                 com->lc_time_last_checkpoint +
1697                                 LFSCK_CHECKPOINT_INTERVAL;
1698
1699                         CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before "
1700                                "the second-stage scaning\n",
1701                                lfsck_lfsck2name(lfsck));
1702
1703                         /* Flush async updates before handling orphan. */
1704                         rc2 = dt_sync(env, lfsck->li_next);
1705
1706                         CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1707                                "scan start, synced: rc = %d\n",
1708                                lfsck_lfsck2name(lfsck), rc2);
1709
1710                         if (CFS_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1711                                 GOTO(cleanup, rc = 0);
1712
1713                         while (test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags)) {
1714                                 int seconds = 30;
1715
1716                                 rc = lfsck_assistant_query_others(env, com);
1717                                 if (lfsck_phase2_next_ready(lad))
1718                                         goto p2_next;
1719
1720                                 if (rc < 0)
1721                                         GOTO(cleanup, rc);
1722
1723                                 /* Pull LFSCK status on related targets once
1724                                  * per 30 seconds if we are not notified. */
1725                                 while (seconds > 0 &&
1726                                        wait_event_idle_timeout(
1727                                                athread->t_ctl_waitq,
1728                                                lfsck_phase2_next_ready(lad) ||
1729                                                test_bit(LAD_EXIT,
1730                                                         &lad->lad_flags) ||
1731                                                !thread_is_running(mthread),
1732                                                cfs_time_seconds(1)) == 0)
1733                                         seconds -= 1;
1734
1735                                 if (unlikely(
1736                                         test_bit(LAD_EXIT, &lad->lad_flags) ||
1737                                         !thread_is_running(mthread)))
1738                                         GOTO(cleanup, rc = 0);
1739
1740                                 if (seconds == 0)
1741                                         continue;
1742
1743 p2_next:
1744                                 rc = lao->la_handler_p2(env, com);
1745                                 if (rc != 0)
1746                                         GOTO(cleanup, rc);
1747
1748                                 if (unlikely(
1749                                         test_bit(LAD_EXIT, &lad->lad_flags) ||
1750                                         !thread_is_running(mthread)))
1751                                         GOTO(cleanup, rc = 0);
1752                         }
1753                 }
1754         }
1755
1756 cleanup:
1757         /* Cleanup the unfinished requests. */
1758         spin_lock(&lad->lad_lock);
1759         if (rc < 0)
1760                 lad->lad_assistant_status = rc;
1761
1762         if (test_bit(LAD_EXIT, &lad->lad_flags) && lad->lad_post_result <= 0)
1763                 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1764
1765         thread_set_flags(athread, SVC_STOPPING);
1766         while (!list_empty(&lad->lad_req_list)) {
1767                 lar = list_first_entry(&lad->lad_req_list,
1768                                        struct lfsck_assistant_req,
1769                                        lar_list);
1770                 list_del_init(&lar->lar_list);
1771                 lad->lad_prefetched--;
1772                 spin_unlock(&lad->lad_lock);
1773                 lao->la_req_fini(env, lar);
1774                 spin_lock(&lad->lad_lock);
1775         }
1776         spin_unlock(&lad->lad_lock);
1777
1778         memset(lr, 0, sizeof(*lr));
1779         if (rc > 0) {
1780                 lr->lr_event = LE_PHASE2_DONE;
1781                 lr->lr_status = rc;
1782         } else if (rc == 0) {
1783                 if (lfsck->li_flags & LPF_ALL_TGT) {
1784                         lr->lr_event = LE_STOP;
1785                         lr->lr_status = LS_STOPPED;
1786                 } else {
1787                         lr->lr_event = LE_PEER_EXIT;
1788                         switch (lfsck->li_status) {
1789                         case LS_PAUSED:
1790                         case LS_CO_PAUSED:
1791                                 lr->lr_status = LS_CO_PAUSED;
1792                                 break;
1793                         case LS_STOPPED:
1794                         case LS_CO_STOPPED:
1795                                 lr->lr_status = LS_CO_STOPPED;
1796                                 break;
1797                         default:
1798                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1799                                        "status: rc = %d\n",
1800                                        lfsck_lfsck2name(lfsck),
1801                                        lfsck->li_status);
1802                                 lr->lr_status = LS_CO_FAILED;
1803                                 break;
1804                         }
1805                 }
1806         } else {
1807                 if (lfsck->li_flags & LPF_ALL_TGT) {
1808                         lr->lr_event = LE_STOP;
1809                         lr->lr_status = LS_FAILED;
1810                 } else {
1811                         lr->lr_event = LE_PEER_EXIT;
1812                         lr->lr_status = LS_CO_FAILED;
1813                 }
1814         }
1815
1816         rc1 = lfsck_assistant_notify_others(env, com, lr);
1817         if (rc1 != 0) {
1818                 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1819                        "others for %s quit: rc = %d\n",
1820                        lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1821                 rc = rc1;
1822         }
1823
1824         CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before exit\n",
1825                lfsck_lfsck2name(lfsck));
1826
1827         /* Flush async updates before exit. */
1828         rc2 = dt_sync(env, lfsck->li_next);
1829
1830         CDEBUG(D_LFSCK, "%s: LFSCK assistant synced before exit: rc = %d\n",
1831                lfsck_lfsck2name(lfsck), rc2);
1832
1833         /* Under force exit case, some requests may be just freed without
1834          * verification, those objects should be re-handled when next run.
1835          * So not update the on-disk trace file under such case. */
1836         if (test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags)) {
1837                 if (!test_bit(LAD_EXIT, &lad->lad_flags))
1838                         rc1 = lao->la_double_scan_result(env, com, rc);
1839
1840                 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1841                        "finished: rc = %d\n",
1842                        lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1843         }
1844
1845 fini:
1846         if (test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags))
1847                 atomic_dec(&lfsck->li_double_scan_count);
1848
1849         spin_lock(&lad->lad_lock);
1850         lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1851         thread_set_flags(athread, SVC_STOPPED);
1852         lad->lad_task = NULL;
1853         spin_unlock(&lad->lad_lock);
1854
1855         CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1856                lfsck_lfsck2name(lfsck), lad->lad_name,
1857                lad->lad_assistant_status);
1858
1859         lfsck_thread_args_fini(lta);
1860         wake_up(&mthread->t_ctl_waitq);
1861
1862         return rc;
1863 }