Whamcloud - gitweb
LU-8886 lfsck: handle -ENODATA for the end of iteration
[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 static int lfsck_parent_fid(const struct lu_env *env, struct dt_object *obj,
97                             struct lu_fid *fid)
98 {
99         if (unlikely(!S_ISDIR(lfsck_object_type(obj)) ||
100                      !dt_try_as_dir(env, obj)))
101                 return -ENOTDIR;
102
103         return dt_lookup(env, obj, (struct dt_rec *)fid,
104                          (const struct dt_key *)"..");
105 }
106
107 /**
108  * Check whether needs to scan the directory or not.
109  *
110  * 1) If we are not doing namespace LFSCK, or the given @obj is not directory,
111  *    then needs not to scan the @obj. Otherwise,
112  * 2) Global /ROOT needs to be scanned, backend root needs not to be scanned.
113  * 3) If the @obj is neither IGIF nor normal FID (including .lustre and its
114  *    sub-directories that have been scanned when the LFSCK engine start),
115  *    then needs not to be scanned.
116  * 4) If it is a remote object, then scanning the object will be done on the
117  *    MDT on which the object really resides.
118  * 5) If the local object has normal FID, then needs to be scanned. Otherwise,
119  * 6) If the object has linkEA, then needs to be scanned. Otherwise,
120  * 7) If none of the previous conditions are true, we need to check the parent
121  *    directories whether this subdirectory is in a tree that should be scanned.
122  *    Set the parent as current @obj, repeat 2)-7).
123  *
124  * \param[in] env       pointer to the thread context
125  * \param[in] lfsck     pointer to the lfsck instance
126  * \param[in] obj       pointer to the object to be checked
127  *
128  * \retval              positive number if the directory needs to be scanned
129  * \retval              0 if the directory needs NOT to be scanned
130  * \retval              negative error number on failure
131  */
132 static int lfsck_needs_scan_dir(const struct lu_env *env,
133                                 struct lfsck_instance *lfsck,
134                                 struct dt_object *obj)
135 {
136         struct lfsck_thread_info *info    = lfsck_env_info(env);
137         struct lu_fid            *fid     = &info->lti_fid;
138         struct lu_seq_range      *range   = &info->lti_range;
139         struct seq_server_site   *ss      = lfsck_dev_site(lfsck);
140         __u32                     idx     = lfsck_dev_idx(lfsck);
141         int                       depth   = 0;
142         int                       rc      = 0;
143
144         if (list_empty(&lfsck->li_list_dir) || !S_ISDIR(lfsck_object_type(obj)))
145                 return 0;
146
147         LASSERT(ss != NULL);
148
149         *fid = *lfsck_dto2fid(obj);
150         while (1) {
151                 /* Global /ROOT is visible. */
152                 if (unlikely(lu_fid_eq(fid, &lfsck->li_global_root_fid)))
153                         return 1;
154
155                 /* Backend root is invisible. */
156                 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
157                         return 0;
158
159                 if (!fid_is_norm(fid) && !fid_is_igif(fid))
160                         return 0;
161
162                 fld_range_set_mdt(range);
163                 rc = fld_local_lookup(env, ss->ss_server_fld,
164                                       fid_seq(fid), range);
165                 if (rc != 0 || range->lsr_index != idx)
166                         /* Current FID should NOT be for the input parameter
167                          * @obj, because the lfsck_master_oit_engine() has
168                          * filtered out agent object. So current FID is for
169                          * the ancestor of the original input parameter @obj.
170                          * So the ancestor is a remote directory. The input
171                          * parameter @obj is local directory, and should be
172                          * scanned under such case. */
173                         return 1;
174
175                 /* normal FID on this target (locally) must be for the
176                  * client-side visiable object. */
177                 if (fid_is_norm(fid))
178                         return 1;
179
180                 if (obj == NULL) {
181                         obj = lfsck_object_find_bottom(env, lfsck, fid);
182                         if (IS_ERR(obj))
183                                 return PTR_ERR(obj);
184
185                         depth++;
186                         if (!dt_object_exists(obj))
187                                 GOTO(out, rc = 0);
188                 }
189
190                 dt_read_lock(env, obj, MOR_TGT_CHILD);
191                 if (unlikely(lfsck_is_dead_obj(obj))) {
192                         dt_read_unlock(env, obj);
193
194                         GOTO(out, rc = 0);
195                 }
196
197                 rc = dt_xattr_get(env, obj,
198                                   lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK);
199                 dt_read_unlock(env, obj);
200                 if (rc >= 0)
201                         GOTO(out, rc = 1);
202
203                 if (rc < 0 && rc != -ENODATA)
204                         GOTO(out, rc);
205
206                 rc = lfsck_parent_fid(env, obj, fid);
207                 if (depth > 0)
208                         lfsck_object_put(env, obj);
209
210                 obj = NULL;
211                 if (rc != 0)
212                         return rc;
213
214                 if (!fid_is_sane(fid))
215                         return 0;
216         }
217
218 out:
219         if (depth > 0 && obj != NULL)
220                 lfsck_object_put(env, obj);
221
222         return rc;
223 }
224
225 static int lfsck_load_stripe_lmv(const struct lu_env *env,
226                                  struct lfsck_instance *lfsck,
227                                  struct dt_object *obj)
228 {
229         struct lmv_mds_md_v1    *lmv    = &lfsck_env_info(env)->lti_lmv;
230         struct lfsck_lmv        *llmv;
231         int                      rc;
232         ENTRY;
233
234         LASSERT(lfsck->li_obj_dir == NULL);
235         LASSERT(lfsck->li_lmv == NULL);
236
237         rc = lfsck_read_stripe_lmv(env, obj, lmv);
238         if (rc == -ENODATA) {
239                 lfsck->li_obj_dir = lfsck_object_get(obj);
240
241                 RETURN(0);
242         }
243
244         if (rc < 0)
245                 RETURN(rc);
246
247         OBD_ALLOC_PTR(llmv);
248         if (llmv == NULL)
249                 RETURN(-ENOMEM);
250
251         if (lmv->lmv_magic == LMV_MAGIC) {
252                 struct lfsck_slave_lmv_rec      *lslr;
253                 __u32                            stripes;
254
255                 llmv->ll_lmv_master = 1;
256                 if (lmv->lmv_stripe_count < 1)
257                         stripes = LFSCK_LMV_DEF_STRIPES;
258                 else if (lmv->lmv_stripe_count > LFSCK_LMV_MAX_STRIPES)
259                         stripes = LFSCK_LMV_MAX_STRIPES;
260                 else
261                         stripes = lmv->lmv_stripe_count;
262
263                 OBD_ALLOC_LARGE(lslr, sizeof(*lslr) * stripes);
264                 if (lslr == NULL) {
265                         OBD_FREE_PTR(llmv);
266
267                         RETURN(-ENOMEM);
268                 }
269
270                 llmv->ll_stripes_allocated = stripes;
271                 llmv->ll_hash_type = LMV_HASH_TYPE_UNKNOWN;
272                 llmv->ll_lslr = lslr;
273         } else {
274                 llmv->ll_lmv_slave = 1;
275         }
276
277         lfsck->li_obj_dir = lfsck_object_get(obj);
278         llmv->ll_lmv = *lmv;
279         atomic_set(&llmv->ll_ref, 1);
280         lfsck->li_lmv = llmv;
281
282         RETURN(0);
283 }
284
285 /* LFSCK wrap functions */
286
287 static void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
288                        bool new_checked)
289 {
290         struct lfsck_component *com;
291
292         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
293                 com->lc_ops->lfsck_fail(env, com, new_checked);
294         }
295 }
296
297 void lfsck_close_dir(const struct lu_env *env,
298                      struct lfsck_instance *lfsck, int result)
299 {
300         struct lfsck_component *com;
301         ENTRY;
302
303         if (lfsck->li_lmv != NULL) {
304                 lfsck->li_lmv->ll_exit_value = result;
305                 if (lfsck->li_obj_dir != NULL) {
306                         list_for_each_entry(com, &lfsck->li_list_dir,
307                                             lc_link_dir) {
308                                 com->lc_ops->lfsck_close_dir(env, com);
309                         }
310                 }
311
312                 lfsck_lmv_put(env, lfsck->li_lmv);
313                 lfsck->li_lmv = NULL;
314         }
315
316         if (lfsck->li_di_dir != NULL) {
317                 const struct dt_it_ops  *dir_iops;
318                 struct dt_it            *dir_di   = lfsck->li_di_dir;
319
320                 LASSERT(lfsck->li_obj_dir != NULL);
321
322                 dir_iops = &lfsck->li_obj_dir->do_index_ops->dio_it;
323                 lfsck_di_dir_put(env, lfsck);
324                 dir_iops->fini(env, dir_di);
325         }
326
327         if (lfsck->li_obj_dir != NULL) {
328                 struct dt_object        *dir_obj  = lfsck->li_obj_dir;
329
330                 lfsck->li_obj_dir = NULL;
331                 lfsck_object_put(env, dir_obj);
332         }
333
334         EXIT;
335 }
336
337 int lfsck_open_dir(const struct lu_env *env,
338                    struct lfsck_instance *lfsck, __u64 cookie)
339 {
340         struct dt_object        *obj    = lfsck->li_obj_dir;
341         struct dt_it            *di     = lfsck->li_di_dir;
342         struct lfsck_component  *com;
343         const struct dt_it_ops  *iops;
344         int                      rc     = 0;
345         ENTRY;
346
347         LASSERT(obj != NULL);
348         LASSERT(di == NULL);
349
350         if (unlikely(!dt_try_as_dir(env, obj)))
351                 GOTO(out, rc = -ENOTDIR);
352
353         list_for_each_entry(com, &lfsck->li_list_dir, lc_link_dir) {
354                 rc = com->lc_ops->lfsck_open_dir(env, com);
355                 if (rc != 0)
356                         GOTO(out, rc);
357         }
358
359         iops = &obj->do_index_ops->dio_it;
360         di = iops->init(env, obj, lfsck->li_args_dir);
361         if (IS_ERR(di))
362                 GOTO(out, rc = PTR_ERR(di));
363
364         rc = iops->load(env, di, cookie);
365         if (rc == -ENODATA)
366                 rc = 1;
367         else if (rc == 0 || (rc > 0 && cookie > 0))
368                 rc = iops->next(env, di);
369         else if (rc > 0)
370                 rc = 0;
371
372         if (rc != 0) {
373                 iops->put(env, di);
374                 iops->fini(env, di);
375         } else {
376                 lfsck->li_cookie_dir = iops->store(env, di);
377                 spin_lock(&lfsck->li_lock);
378                 lfsck->li_di_dir = di;
379                 spin_unlock(&lfsck->li_lock);
380         }
381
382         GOTO(out, rc);
383
384 out:
385         if (rc != 0)
386                 lfsck_close_dir(env, lfsck, rc);
387
388         return rc;
389 }
390
391 static int lfsck_checkpoint(const struct lu_env *env,
392                             struct lfsck_instance *lfsck)
393 {
394         struct lfsck_component *com;
395         int                     rc  = 0;
396         int                     rc1 = 0;
397
398         if (likely(cfs_time_beforeq(cfs_time_current(),
399                                     lfsck->li_time_next_checkpoint)))
400                 return 0;
401
402         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
403         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
404                 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
405                 if (rc != 0)
406                         rc1 = rc;
407         }
408
409         lfsck->li_time_last_checkpoint = cfs_time_current();
410         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
411                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
412         return rc1 != 0 ? rc1 : rc;
413 }
414
415 static int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
416                       struct lfsck_start_param *lsp)
417 {
418         struct dt_object       *obj     = NULL;
419         struct lfsck_component *com;
420         struct lfsck_component *next;
421         struct lfsck_position  *pos     = NULL;
422         const struct dt_it_ops *iops    =
423                                 &lfsck->li_obj_oit->do_index_ops->dio_it;
424         int                     rc;
425         ENTRY;
426
427         LASSERT(lfsck->li_obj_dir == NULL);
428         LASSERT(lfsck->li_di_dir == NULL);
429
430         lfsck->li_current_oit_processed = 0;
431         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
432                 com->lc_new_checked = 0;
433                 rc = com->lc_ops->lfsck_prep(env, com, lsp);
434                 if (rc != 0)
435                         GOTO(out, rc);
436
437                 if ((pos == NULL) ||
438                     (!lfsck_pos_is_zero(&com->lc_pos_start) &&
439                      lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
440                         pos = &com->lc_pos_start;
441         }
442
443         /* Init otable-based iterator. */
444         if (pos == NULL) {
445                 rc = iops->load(env, lfsck->li_di_oit, 0);
446                 if (rc > 0 || unlikely(rc == -ENODATA)) {
447                         lfsck->li_oit_over = 1;
448                         rc = 0;
449                 }
450
451                 GOTO(out, rc);
452         }
453
454         rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
455         if (rc > 0 || unlikely(rc == -ENODATA))
456                 lfsck->li_oit_over = 1;
457         else if (rc < 0)
458                 GOTO(out, rc);
459
460         if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
461                 GOTO(out, rc = 0);
462
463         /* Find the directory for namespace-based traverse. */
464         obj = lfsck_object_find_bottom(env, lfsck, &pos->lp_dir_parent);
465         if (IS_ERR(obj))
466                 RETURN(PTR_ERR(obj));
467
468         /* Remote directory will be scanned by the LFSCK instance
469          * on the MDT where the remote object really resides on. */
470         if (!dt_object_exists(obj) || dt_object_remote(obj) ||
471             unlikely(!S_ISDIR(lfsck_object_type(obj))))
472                 GOTO(out, rc = 0);
473
474         rc = lfsck_load_stripe_lmv(env, lfsck, obj);
475         if (rc == 0) {
476                 /* For the master MDT-object of a striped directory,
477                  * reset the iteration from the directory beginning. */
478                 if (lfsck->li_lmv != NULL && lfsck->li_lmv->ll_lmv_master)
479                         pos->lp_dir_cookie = 0;
480
481                 rc = lfsck_open_dir(env, lfsck, pos->lp_dir_cookie);
482                 if (rc > 0)
483                         /* The end of the directory. */
484                         rc = 0;
485         }
486
487         GOTO(out, rc);
488
489 out:
490         if (obj != NULL)
491                 lfsck_object_put(env, obj);
492
493         if (rc != 0) {
494                 lfsck_close_dir(env, lfsck, rc);
495                 list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
496                                          lc_link) {
497                         com->lc_ops->lfsck_post(env, com, rc, true);
498                 }
499
500                 return rc;
501         }
502
503         rc = 0;
504         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, true);
505         lfsck->li_pos_current = lfsck->li_pos_checkpoint;
506         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
507                 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
508                 if (rc != 0)
509                         break;
510         }
511
512         lfsck->li_time_last_checkpoint = cfs_time_current();
513         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
514                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
515         return rc;
516 }
517
518 static int lfsck_exec_oit(const struct lu_env *env,
519                           struct lfsck_instance *lfsck, struct dt_object *obj)
520 {
521         struct lfsck_component *com;
522         int                     rc;
523         ENTRY;
524
525         LASSERT(lfsck->li_obj_dir == NULL);
526
527         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
528                 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
529                 if (rc != 0)
530                         RETURN(rc);
531         }
532
533         rc = lfsck_needs_scan_dir(env, lfsck, obj);
534         if (rc <= 0)
535                 GOTO(out, rc);
536
537         rc = lfsck_load_stripe_lmv(env, lfsck, obj);
538         if (rc == 0)
539                 rc = lfsck_open_dir(env, lfsck, 0);
540
541         GOTO(out, rc);
542
543 out:
544         if (rc < 0)
545                 lfsck_fail(env, lfsck, false);
546
547         if (rc != 0)
548                 lfsck_close_dir(env, lfsck, rc);
549
550         return rc > 0 ? 0 : rc;
551 }
552
553 static int lfsck_exec_dir(const struct lu_env *env,
554                           struct lfsck_instance *lfsck,
555                           struct lfsck_assistant_object *lso,
556                           struct lu_dirent *ent, __u16 type)
557 {
558         struct lfsck_component *com;
559         int                     rc;
560
561         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
562                 rc = com->lc_ops->lfsck_exec_dir(env, com, lso, ent, type);
563                 if (rc != 0)
564                         return rc;
565         }
566         return 0;
567 }
568
569 static int lfsck_master_dir_engine(const struct lu_env *env,
570                                    struct lfsck_instance *lfsck);
571
572 static int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
573                       int result)
574 {
575         struct lfsck_component *com;
576         struct lfsck_component *next;
577         int                     rc  = result;
578
579         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
580         lfsck_close_dir(env, lfsck, result);
581
582         while (thread_is_running(&lfsck->li_thread) && rc > 0 &&
583                !list_empty(&lfsck->li_list_lmv)) {
584                 struct lfsck_lmv_unit *llu;
585
586                 spin_lock(&lfsck->li_lock);
587                 llu = list_entry(lfsck->li_list_lmv.next,
588                                  struct lfsck_lmv_unit, llu_link);
589                 list_del_init(&llu->llu_link);
590                 spin_unlock(&lfsck->li_lock);
591
592                 lfsck->li_lmv = &llu->llu_lmv;
593                 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
594                 rc = lfsck_open_dir(env, lfsck, 0);
595                 if (rc == 0) {
596                         rc = lfsck_master_dir_engine(env, lfsck);
597                         lfsck_close_dir(env, lfsck, result);
598                 }
599         }
600
601         result = rc;
602
603         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
604                 rc = com->lc_ops->lfsck_post(env, com, result, false);
605                 if (rc != 0)
606                         CDEBUG(D_LFSCK, "%s: lfsck_post at the component %u: "
607                                "rc = %d\n", lfsck_lfsck2name(lfsck),
608                                (__u32)com->lc_type, rc);
609         }
610
611         lfsck->li_time_last_checkpoint = cfs_time_current();
612         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
613                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
614
615         /* Ignore some component post failure to make other can go ahead. */
616         return result;
617 }
618
619 static int lfsck_double_scan(const struct lu_env *env,
620                              struct lfsck_instance *lfsck)
621 {
622         struct lfsck_component *com;
623         struct lfsck_component *next;
624         struct l_wait_info      lwi = { 0 };
625         int                     rc  = 0;
626         int                     rc1 = 0;
627
628         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
629                 rc = com->lc_ops->lfsck_double_scan(env, com);
630                 if (rc != 0)
631                         rc1 = rc;
632         }
633
634         l_wait_event(lfsck->li_thread.t_ctl_waitq,
635                      atomic_read(&lfsck->li_double_scan_count) == 0,
636                      &lwi);
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 (OBD_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 (OBD_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_entry(lfsck->li_list_lmv.next,
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 (OBD_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         struct l_wait_info        lwi      = { 0 };
1017         int                       rc;
1018         ENTRY;
1019
1020         /* There will be some objects verification during the LFSCK start,
1021          * such as the subsequent lfsck_verify_lpf(). Trigger low layer OI
1022          * OI scrub before that to handle the potential inconsistence. */
1023         oit_di = oit_iops->init(env, oit_obj, lfsck->li_args_oit);
1024         if (IS_ERR(oit_di)) {
1025                 rc = PTR_ERR(oit_di);
1026                 CDEBUG(D_LFSCK, "%s: master engine fail to init iteration: "
1027                        "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1028
1029                 GOTO(fini_args, rc);
1030         }
1031
1032         if (lfsck->li_master &&
1033             (!list_empty(&lfsck->li_list_scan) ||
1034              !list_empty(&lfsck->li_list_double_scan))) {
1035                 rc = lfsck_verify_lpf(env, lfsck);
1036                 /* Fail to verify the .lustre/lost+found/MDTxxxx/ may be not
1037                  * fatal, because the .lustre/lost+found/ maybe not accessed
1038                  * by the LFSCK if it does not add orphans or others to such
1039                  * directory. So go ahead until hit failure when really uses
1040                  * the directory. */
1041                 if (rc != 0)
1042                         CDEBUG(D_LFSCK, "%s: master engine fail to verify the "
1043                                ".lustre/lost+found/, go ahead: rc = %d\n",
1044                                lfsck_lfsck2name(lfsck), rc);
1045         }
1046
1047         spin_lock(&lfsck->li_lock);
1048         lfsck->li_di_oit = oit_di;
1049         spin_unlock(&lfsck->li_lock);
1050         rc = lfsck_prep(env, lfsck, lta->lta_lsp);
1051         if (rc != 0)
1052                 GOTO(fini_oit, rc);
1053
1054         CDEBUG(D_LFSCK, "LFSCK entry: oit_flags = %#x, dir_flags = %#x, "
1055                "oit_cookie = %llu, dir_cookie = %#llx, parent = "DFID
1056                ", pid = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1057                lfsck->li_pos_checkpoint.lp_oit_cookie,
1058                lfsck->li_pos_checkpoint.lp_dir_cookie,
1059                PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1060                current_pid());
1061
1062         spin_lock(&lfsck->li_lock);
1063         thread_set_flags(thread, SVC_RUNNING);
1064         spin_unlock(&lfsck->li_lock);
1065         wake_up_all(&thread->t_ctl_waitq);
1066
1067         l_wait_event(thread->t_ctl_waitq,
1068                      lfsck->li_start_unplug ||
1069                      !thread_is_running(thread),
1070                      &lwi);
1071         if (!thread_is_running(thread))
1072                 GOTO(fini_oit, rc = 0);
1073
1074         if (!list_empty(&lfsck->li_list_scan) ||
1075             list_empty(&lfsck->li_list_double_scan))
1076                 rc = lfsck_master_oit_engine(env, lfsck);
1077         else
1078                 rc = 1;
1079
1080         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_checkpoint, false);
1081         CDEBUG(D_LFSCK, "LFSCK exit: oit_flags = %#x, dir_flags = %#x, "
1082                "oit_cookie = %llu, dir_cookie = %#llx, parent = "DFID
1083                ", pid = %d, rc = %d\n", lfsck->li_args_oit, lfsck->li_args_dir,
1084                lfsck->li_pos_checkpoint.lp_oit_cookie,
1085                lfsck->li_pos_checkpoint.lp_dir_cookie,
1086                PFID(&lfsck->li_pos_checkpoint.lp_dir_parent),
1087                current_pid(), rc);
1088
1089         if (!OBD_FAIL_CHECK(OBD_FAIL_LFSCK_CRASH))
1090                 rc = lfsck_post(env, lfsck, rc);
1091         else
1092                 lfsck_close_dir(env, lfsck, rc);
1093
1094 fini_oit:
1095         lfsck_di_oit_put(env, lfsck);
1096         oit_iops->fini(env, oit_di);
1097         if (rc == 1) {
1098                 if (!list_empty(&lfsck->li_list_double_scan))
1099                         rc = lfsck_double_scan(env, lfsck);
1100                 else
1101                         rc = 0;
1102         } else {
1103                 lfsck_quit(env, lfsck);
1104         }
1105
1106         /* XXX: Purge the pinned objects in the future. */
1107
1108 fini_args:
1109         spin_lock(&lfsck->li_lock);
1110         thread_set_flags(thread, SVC_STOPPED);
1111         spin_unlock(&lfsck->li_lock);
1112         wake_up_all(&thread->t_ctl_waitq);
1113         lfsck_thread_args_fini(lta);
1114         return rc;
1115 }
1116
1117 static inline bool lfsck_assistant_req_empty(struct lfsck_assistant_data *lad)
1118 {
1119         bool empty = false;
1120
1121         spin_lock(&lad->lad_lock);
1122         if (list_empty(&lad->lad_req_list))
1123                 empty = true;
1124         spin_unlock(&lad->lad_lock);
1125
1126         return empty;
1127 }
1128
1129 /**
1130  * Query the LFSCK status from the instatnces on remote servers.
1131  *
1132  * The LFSCK assistant thread queries the LFSCK instances on other
1133  * servers (MDT/OST) about their status, such as whether they have
1134  * finished the phase1/phase2 scanning or not, and so on.
1135  *
1136  * \param[in] env       pointer to the thread context
1137  * \param[in] com       pointer to the lfsck component
1138  *
1139  * \retval              0 for success
1140  * \retval              negative error number on failure
1141  */
1142 static int lfsck_assistant_query_others(const struct lu_env *env,
1143                                         struct lfsck_component *com)
1144 {
1145         struct lfsck_thread_info          *info  = lfsck_env_info(env);
1146         struct lfsck_request              *lr    = &info->lti_lr;
1147         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
1148         struct lfsck_instance             *lfsck = com->lc_lfsck;
1149         struct lfsck_assistant_data       *lad   = com->lc_data;
1150         struct ptlrpc_request_set         *set;
1151         struct lfsck_tgt_descs            *ltds;
1152         struct lfsck_tgt_desc             *ltd;
1153         struct list_head                  *phase_head;
1154         int                                rc    = 0;
1155         int                                rc1   = 0;
1156         ENTRY;
1157
1158         set = ptlrpc_prep_set();
1159         if (set == NULL)
1160                 RETURN(-ENOMEM);
1161
1162         lad->lad_touch_gen++;
1163         memset(lr, 0, sizeof(*lr));
1164         lr->lr_event = LE_QUERY;
1165         lr->lr_active = com->lc_type;
1166
1167         memset(laia, 0, sizeof(*laia));
1168         laia->laia_com = com;
1169         laia->laia_lr = lr;
1170
1171         if (!list_empty(&lad->lad_mdt_phase1_list)) {
1172                 ltds = &lfsck->li_mdt_descs;
1173                 lr->lr_flags = 0;
1174                 phase_head = &lad->lad_mdt_phase1_list;
1175         } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1176                 goto out;
1177         } else {
1178
1179 again:
1180                 ltds = &lfsck->li_ost_descs;
1181                 lr->lr_flags = LEF_TO_OST;
1182                 phase_head = &lad->lad_ost_phase1_list;
1183         }
1184
1185         laia->laia_ltds = ltds;
1186         spin_lock(&ltds->ltd_lock);
1187         while (!list_empty(phase_head)) {
1188                 struct list_head *phase_list;
1189                 __u32            *gen;
1190
1191                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1192                         ltd = list_entry(phase_head->next,
1193                                          struct lfsck_tgt_desc,
1194                                          ltd_layout_phase_list);
1195                         phase_list = &ltd->ltd_layout_phase_list;
1196                         gen = &ltd->ltd_layout_gen;
1197                 } else {
1198                         ltd = list_entry(phase_head->next,
1199                                          struct lfsck_tgt_desc,
1200                                          ltd_namespace_phase_list);
1201                         phase_list = &ltd->ltd_namespace_phase_list;
1202                         gen = &ltd->ltd_namespace_gen;
1203                 }
1204
1205                 if (*gen == lad->lad_touch_gen)
1206                         break;
1207
1208                 *gen = lad->lad_touch_gen;
1209                 list_move_tail(phase_list, phase_head);
1210                 atomic_inc(&ltd->ltd_ref);
1211                 laia->laia_ltd = ltd;
1212                 spin_unlock(&ltds->ltd_lock);
1213                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1214                                          lfsck_async_interpret_common,
1215                                          laia, LFSCK_QUERY);
1216                 if (rc != 0) {
1217                         CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to query "
1218                                "%s %x for %s: rc = %d\n",
1219                                lfsck_lfsck2name(lfsck),
1220                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1221                                ltd->ltd_index, lad->lad_name, rc);
1222                         lfsck_tgt_put(ltd);
1223                         rc1 = rc;
1224                 }
1225                 spin_lock(&ltds->ltd_lock);
1226         }
1227         spin_unlock(&ltds->ltd_lock);
1228
1229         rc = ptlrpc_set_wait(set);
1230         if (rc < 0) {
1231                 ptlrpc_set_destroy(set);
1232                 RETURN(rc);
1233         }
1234
1235         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST) &&
1236             list_empty(&lad->lad_mdt_phase1_list))
1237                 goto again;
1238
1239 out:
1240         ptlrpc_set_destroy(set);
1241
1242         RETURN(rc1 != 0 ? rc1 : rc);
1243 }
1244
1245 /**
1246  * Notify the LFSCK event to the instances on remote servers.
1247  *
1248  * The LFSCK assistant thread notifies the LFSCK instances on other
1249  * servers (MDT/OST) about some events, such as start new scanning,
1250  * stop the scanning, this LFSCK instance will exit, and so on.
1251  *
1252  * \param[in] env       pointer to the thread context
1253  * \param[in] com       pointer to the lfsck component
1254  * \param[in] lr        pointer to the LFSCK event request
1255  *
1256  * \retval              0 for success
1257  * \retval              negative error number on failure
1258  */
1259 static int lfsck_assistant_notify_others(const struct lu_env *env,
1260                                          struct lfsck_component *com,
1261                                          struct lfsck_request *lr)
1262 {
1263         struct lfsck_thread_info          *info  = lfsck_env_info(env);
1264         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
1265         struct lfsck_instance             *lfsck = com->lc_lfsck;
1266         struct lfsck_assistant_data       *lad   = com->lc_data;
1267         struct lfsck_bookmark             *bk    = &lfsck->li_bookmark_ram;
1268         struct ptlrpc_request_set         *set;
1269         struct lfsck_tgt_descs            *ltds;
1270         struct lfsck_tgt_desc             *ltd;
1271         struct lfsck_tgt_desc             *next;
1272         __u32                              idx;
1273         int                                rc    = 0;
1274         int                                rc1   = 0;
1275         ENTRY;
1276
1277         set = ptlrpc_prep_set();
1278         if (set == NULL)
1279                 RETURN(-ENOMEM);
1280
1281         lr->lr_index = lfsck_dev_idx(lfsck);
1282         lr->lr_active = com->lc_type;
1283
1284         memset(laia, 0, sizeof(*laia));
1285         laia->laia_com = com;
1286         laia->laia_lr = lr;
1287
1288         switch (lr->lr_event) {
1289         case LE_START:
1290                 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1291                         goto next;
1292
1293                 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN;
1294                 lr->lr_speed = bk->lb_speed_limit;
1295                 lr->lr_version = bk->lb_version;
1296                 lr->lr_param |= bk->lb_param;
1297                 lr->lr_async_windows = bk->lb_async_windows;
1298                 lr->lr_flags = LEF_TO_OST;
1299
1300                 /* Notify OSTs firstly, then handle other MDTs if needed. */
1301                 ltds = &lfsck->li_ost_descs;
1302                 laia->laia_ltds = ltds;
1303                 down_read(&ltds->ltd_rw_sem);
1304                 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1305                         ltd = lfsck_tgt_get(ltds, idx);
1306                         LASSERT(ltd != NULL);
1307
1308                         laia->laia_ltd = ltd;
1309                         rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1310                                         lfsck_async_interpret_common,
1311                                         laia, LFSCK_NOTIFY);
1312                         if (rc != 0) {
1313                                 lfsck_lad_set_bitmap(env, com, idx);
1314                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1315                                        "notify OST %x for %s start: rc = %d\n",
1316                                        lfsck_lfsck2name(lfsck), idx,
1317                                        lad->lad_name, rc);
1318                                 lfsck_tgt_put(ltd);
1319                         }
1320                 }
1321                 up_read(&ltds->ltd_rw_sem);
1322
1323                 /* Sync up */
1324                 rc = ptlrpc_set_wait(set);
1325                 if (rc < 0) {
1326                         ptlrpc_set_destroy(set);
1327                         RETURN(rc);
1328                 }
1329
1330 next:
1331                 if (!(bk->lb_param & LPF_ALL_TGT))
1332                         break;
1333
1334                 /* link other MDT targets locallly. */
1335                 ltds = &lfsck->li_mdt_descs;
1336                 spin_lock(&ltds->ltd_lock);
1337                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1338                         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1339                                 ltd = lfsck_ltd2tgt(ltds, idx);
1340                                 LASSERT(ltd != NULL);
1341
1342                                 if (!list_empty(&ltd->ltd_layout_list))
1343                                         continue;
1344
1345                                 list_add_tail(&ltd->ltd_layout_list,
1346                                               &lad->lad_mdt_list);
1347                                 list_add_tail(&ltd->ltd_layout_phase_list,
1348                                               &lad->lad_mdt_phase1_list);
1349                         }
1350                 } else {
1351                         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1352                                 ltd = lfsck_ltd2tgt(ltds, idx);
1353                                 LASSERT(ltd != NULL);
1354
1355                                 if (!list_empty(&ltd->ltd_namespace_list))
1356                                         continue;
1357
1358                                 list_add_tail(&ltd->ltd_namespace_list,
1359                                               &lad->lad_mdt_list);
1360                                 list_add_tail(&ltd->ltd_namespace_phase_list,
1361                                               &lad->lad_mdt_phase1_list);
1362                         }
1363                 }
1364                 spin_unlock(&ltds->ltd_lock);
1365                 break;
1366         case LE_STOP:
1367         case LE_PHASE2_DONE:
1368         case LE_PEER_EXIT: {
1369                 struct list_head *phase_head;
1370
1371                 /* Handle other MDTs firstly if needed, then notify the OSTs. */
1372                 if (bk->lb_param & LPF_ALL_TGT) {
1373                         phase_head = &lad->lad_mdt_list;
1374                         ltds = &lfsck->li_mdt_descs;
1375                         if (lr->lr_event == LE_STOP) {
1376                                 /* unlink other MDT targets locallly. */
1377                                 spin_lock(&ltds->ltd_lock);
1378                                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1379                                         list_for_each_entry_safe(ltd, next,
1380                                                 phase_head, ltd_layout_list) {
1381                                                 list_del_init(
1382                                                 &ltd->ltd_layout_phase_list);
1383                                                 list_del_init(
1384                                                 &ltd->ltd_layout_list);
1385                                         }
1386                                 } else {
1387                                         list_for_each_entry_safe(ltd, next,
1388                                                         phase_head,
1389                                                         ltd_namespace_list) {
1390                                                 list_del_init(
1391                                                 &ltd->ltd_namespace_phase_list);
1392                                                 list_del_init(
1393                                                 &ltd->ltd_namespace_list);
1394                                         }
1395                                 }
1396                                 spin_unlock(&ltds->ltd_lock);
1397
1398                                 if (com->lc_type != LFSCK_TYPE_LAYOUT)
1399                                         break;
1400
1401                                 lr->lr_flags |= LEF_TO_OST;
1402                                 phase_head = &lad->lad_ost_list;
1403                                 ltds = &lfsck->li_ost_descs;
1404                         } else {
1405                                 lr->lr_flags &= ~LEF_TO_OST;
1406                         }
1407                 } else if (com->lc_type != LFSCK_TYPE_LAYOUT) {
1408                         break;
1409                 } else {
1410                         lr->lr_flags |= LEF_TO_OST;
1411                         phase_head = &lad->lad_ost_list;
1412                         ltds = &lfsck->li_ost_descs;
1413                 }
1414
1415 again:
1416                 laia->laia_ltds = ltds;
1417                 spin_lock(&ltds->ltd_lock);
1418                 while (!list_empty(phase_head)) {
1419                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1420                                 ltd = list_entry(phase_head->next,
1421                                                  struct lfsck_tgt_desc,
1422                                                  ltd_layout_list);
1423                                 if (!list_empty(&ltd->ltd_layout_phase_list))
1424                                         list_del_init(
1425                                                 &ltd->ltd_layout_phase_list);
1426                                 list_del_init(&ltd->ltd_layout_list);
1427                         } else {
1428                                 ltd = list_entry(phase_head->next,
1429                                                  struct lfsck_tgt_desc,
1430                                                  ltd_namespace_list);
1431                                 if (!list_empty(&ltd->ltd_namespace_phase_list))
1432                                         list_del_init(
1433                                                 &ltd->ltd_namespace_phase_list);
1434                                 list_del_init(&ltd->ltd_namespace_list);
1435                         }
1436                         atomic_inc(&ltd->ltd_ref);
1437                         laia->laia_ltd = ltd;
1438                         spin_unlock(&ltds->ltd_lock);
1439                         rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1440                                         lfsck_async_interpret_common,
1441                                         laia, LFSCK_NOTIFY);
1442                         if (rc != 0) {
1443                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1444                                        "notify %s %x for %s stop/phase2_done/"
1445                                        "peer_exit: rc = %d\n",
1446                                        lfsck_lfsck2name(lfsck),
1447                                        (lr->lr_flags & LEF_TO_OST) ?
1448                                        "OST" : "MDT", ltd->ltd_index,
1449                                        lad->lad_name, rc);
1450                                 lfsck_tgt_put(ltd);
1451                         }
1452                         spin_lock(&ltds->ltd_lock);
1453                 }
1454                 spin_unlock(&ltds->ltd_lock);
1455
1456                 rc = ptlrpc_set_wait(set);
1457                 if (rc < 0) {
1458                         ptlrpc_set_destroy(set);
1459                         RETURN(rc);
1460                 }
1461
1462                 if (com->lc_type == LFSCK_TYPE_LAYOUT &&
1463                     !(lr->lr_flags & LEF_TO_OST)) {
1464                         lr->lr_flags |= LEF_TO_OST;
1465                         phase_head = &lad->lad_ost_list;
1466                         ltds = &lfsck->li_ost_descs;
1467                         goto again;
1468                 }
1469                 break;
1470         }
1471         case LE_PHASE1_DONE:
1472                 lad->lad_ops->la_sync_failures(env, com, lr);
1473                 lad->lad_touch_gen++;
1474                 ltds = &lfsck->li_mdt_descs;
1475                 laia->laia_ltds = ltds;
1476                 spin_lock(&ltds->ltd_lock);
1477                 while (!list_empty(&lad->lad_mdt_list)) {
1478                         struct list_head *list;
1479                         __u32            *gen;
1480
1481                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1482                                 ltd = list_entry(lad->lad_mdt_list.next,
1483                                                  struct lfsck_tgt_desc,
1484                                                  ltd_layout_list);
1485                                 list = &ltd->ltd_layout_list;
1486                                 gen = &ltd->ltd_layout_gen;
1487                         } else {
1488                                 struct lfsck_namespace *ns = com->lc_file_ram;
1489
1490                                 ltd = list_entry(lad->lad_mdt_list.next,
1491                                                  struct lfsck_tgt_desc,
1492                                                  ltd_namespace_list);
1493                                 list = &ltd->ltd_namespace_list;
1494                                 gen = &ltd->ltd_namespace_gen;
1495                                 lr->lr_flags2 = ns->ln_flags & ~LF_INCOMPLETE;
1496                         }
1497
1498                         if (*gen == lad->lad_touch_gen)
1499                                 break;
1500
1501                         *gen = lad->lad_touch_gen;
1502                         list_move_tail(list, &lad->lad_mdt_list);
1503                         if (ltd->ltd_synced_failures)
1504                                 continue;
1505
1506                         atomic_inc(&ltd->ltd_ref);
1507                         laia->laia_ltd = ltd;
1508                         spin_unlock(&ltds->ltd_lock);
1509                         rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1510                                         lfsck_async_interpret_common,
1511                                         laia, LFSCK_NOTIFY);
1512                         if (rc != 0) {
1513                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to "
1514                                        "notify MDT %x for %s phase1 done: "
1515                                        "rc = %d\n", lfsck_lfsck2name(lfsck),
1516                                        ltd->ltd_index, lad->lad_name, rc);
1517                                 lfsck_tgt_put(ltd);
1518                         }
1519                         spin_lock(&ltds->ltd_lock);
1520                 }
1521                 spin_unlock(&ltds->ltd_lock);
1522                 break;
1523         default:
1524                 CDEBUG(D_LFSCK, "%s: LFSCK assistant unexpected LFSCK event: "
1525                        "rc = %d\n", lfsck_lfsck2name(lfsck), lr->lr_event);
1526                 rc = -EINVAL;
1527                 break;
1528         }
1529
1530         rc1 = ptlrpc_set_wait(set);
1531         ptlrpc_set_destroy(set);
1532
1533         RETURN(rc != 0 ? rc : rc1);
1534 }
1535
1536 /**
1537  * The LFSCK assistant thread is triggered by the LFSCK main engine.
1538  * They co-work together as an asynchronous pipeline: the LFSCK main
1539  * engine scans the system and pre-fetches the objects, attributes,
1540  * or name entries, etc, and pushes them into the pipeline as input
1541  * requests for the LFSCK assistant thread; on the other end of the
1542  * pipeline, the LFSCK assistant thread performs the real check and
1543  * repair for every request from the main engine.
1544  *
1545  * Generally, the assistant engine may be blocked when check/repair
1546  * something, so the LFSCK main engine will run some faster. On the
1547  * other hand, the LFSCK main engine will drive multiple assistant
1548  * threads in parallel, means for each LFSCK component on the master
1549  * (such as layout LFSCK, namespace LFSCK), there is an independent
1550  * LFSCK assistant thread. So under such 1:N multiple asynchronous
1551  * pipelines mode, the whole LFSCK performance will be much better
1552  * than check/repair everything by the LFSCK main engine itself.
1553  */
1554 int lfsck_assistant_engine(void *args)
1555 {
1556         struct lfsck_thread_args          *lta     = args;
1557         struct lu_env                     *env     = &lta->lta_env;
1558         struct lfsck_component            *com     = lta->lta_com;
1559         struct lfsck_instance             *lfsck   = lta->lta_lfsck;
1560         struct lfsck_bookmark             *bk      = &lfsck->li_bookmark_ram;
1561         struct lfsck_position             *pos     = &com->lc_pos_start;
1562         struct lfsck_thread_info          *info    = lfsck_env_info(env);
1563         struct lfsck_request              *lr      = &info->lti_lr;
1564         struct lfsck_assistant_data       *lad     = com->lc_data;
1565         struct ptlrpc_thread              *mthread = &lfsck->li_thread;
1566         struct ptlrpc_thread              *athread = &lad->lad_thread;
1567         struct lfsck_assistant_operations *lao     = lad->lad_ops;
1568         struct lfsck_assistant_req        *lar;
1569         struct l_wait_info                 lwi     = { 0 };
1570         int                                rc      = 0;
1571         int                                rc1     = 0;
1572         int                                rc2;
1573         ENTRY;
1574
1575         CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread start\n",
1576                lfsck_lfsck2name(lfsck), lad->lad_name);
1577
1578         memset(lr, 0, sizeof(*lr));
1579         lr->lr_event = LE_START;
1580         if (pos->lp_oit_cookie <= 1)
1581                 lr->lr_param = LPF_RESET;
1582         rc = lfsck_assistant_notify_others(env, com, lr);
1583         if (rc != 0) {
1584                 CDEBUG(D_LFSCK, "%s: LFSCK assistant fail to notify others "
1585                        "to start %s: rc = %d\n",
1586                        lfsck_lfsck2name(lfsck), lad->lad_name, rc);
1587                 GOTO(fini, rc);
1588         }
1589
1590         spin_lock(&lad->lad_lock);
1591         lad->lad_task = current;
1592         thread_set_flags(athread, SVC_RUNNING);
1593         spin_unlock(&lad->lad_lock);
1594         wake_up_all(&mthread->t_ctl_waitq);
1595
1596         while (1) {
1597                 while (!list_empty(&lad->lad_req_list)) {
1598                         bool wakeup = false;
1599
1600                         if (unlikely(lad->lad_exit ||
1601                                      !thread_is_running(mthread)))
1602                                 GOTO(cleanup, rc = lad->lad_post_result);
1603
1604                         lar = list_entry(lad->lad_req_list.next,
1605                                          struct lfsck_assistant_req,
1606                                          lar_list);
1607                         /* Only the lfsck_assistant_engine thread itself can
1608                          * remove the "lar" from the head of the list, LFSCK
1609                          * engine thread only inserts other new "lar" at the
1610                          * end of the list. So it is safe to handle current
1611                          * "lar" without the spin_lock. */
1612                         rc = lao->la_handler_p1(env, com, lar);
1613                         spin_lock(&lad->lad_lock);
1614                         list_del_init(&lar->lar_list);
1615                         lad->lad_prefetched--;
1616                         /* Wake up the main engine thread only when the list
1617                          * is empty or half of the prefetched items have been
1618                          * handled to avoid too frequent thread schedule. */
1619                         if (lad->lad_prefetched <= (bk->lb_async_windows / 2))
1620                                 wakeup = true;
1621                         spin_unlock(&lad->lad_lock);
1622                         if (wakeup)
1623                                 wake_up_all(&mthread->t_ctl_waitq);
1624
1625                         lao->la_req_fini(env, lar);
1626                         if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1627                                 GOTO(cleanup, rc);
1628                 }
1629
1630                 l_wait_event(athread->t_ctl_waitq,
1631                              !lfsck_assistant_req_empty(lad) ||
1632                              lad->lad_exit ||
1633                              lad->lad_to_post ||
1634                              lad->lad_to_double_scan,
1635                              &lwi);
1636
1637                 if (unlikely(lad->lad_exit))
1638                         GOTO(cleanup, rc = lad->lad_post_result);
1639
1640                 if (!list_empty(&lad->lad_req_list))
1641                         continue;
1642
1643                 if (lad->lad_to_post) {
1644                         CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread post\n",
1645                                lfsck_lfsck2name(lfsck), lad->lad_name);
1646
1647                         if (unlikely(lad->lad_exit))
1648                                 GOTO(cleanup, rc = lad->lad_post_result);
1649
1650                         lad->lad_to_post = 0;
1651                         LASSERT(lad->lad_post_result > 0);
1652
1653                         /* Wakeup the master engine to go ahead. */
1654                         wake_up_all(&mthread->t_ctl_waitq);
1655
1656                         memset(lr, 0, sizeof(*lr));
1657                         lr->lr_event = LE_PHASE1_DONE;
1658                         lr->lr_status = lad->lad_post_result;
1659                         rc = lfsck_assistant_notify_others(env, com, lr);
1660
1661                         CDEBUG(D_LFSCK, "%s: LFSCK assistant notified "
1662                                "others for %s post: rc = %d\n",
1663                                lfsck_lfsck2name(lfsck),
1664                                lad->lad_name, rc);
1665                 }
1666
1667                 if (lad->lad_to_double_scan) {
1668                         lad->lad_to_double_scan = 0;
1669                         atomic_inc(&lfsck->li_double_scan_count);
1670                         lad->lad_in_double_scan = 1;
1671                         wake_up_all(&mthread->t_ctl_waitq);
1672
1673                         com->lc_new_checked = 0;
1674                         com->lc_new_scanned = 0;
1675                         com->lc_time_last_checkpoint = cfs_time_current();
1676                         com->lc_time_next_checkpoint =
1677                                 com->lc_time_last_checkpoint +
1678                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1679
1680                         CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before "
1681                                "the second-stage scaning\n",
1682                                lfsck_lfsck2name(lfsck));
1683
1684                         /* Flush async updates before handling orphan. */
1685                         rc2 = dt_sync(env, lfsck->li_next);
1686
1687                         CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 "
1688                                "scan start, synced: rc = %d\n",
1689                                lfsck_lfsck2name(lfsck), rc2);
1690
1691                         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1692                                 GOTO(cleanup, rc = 0);
1693
1694                         while (lad->lad_in_double_scan) {
1695                                 rc = lfsck_assistant_query_others(env, com);
1696                                 if (lfsck_phase2_next_ready(lad))
1697                                         goto p2_next;
1698
1699                                 if (rc < 0)
1700                                         GOTO(cleanup, rc);
1701
1702                                 /* Pull LFSCK status on related targets once
1703                                  * per 30 seconds if we are not notified. */
1704                                 lwi = LWI_TIMEOUT_INTERVAL(cfs_time_seconds(30),
1705                                                            cfs_time_seconds(1),
1706                                                            NULL, NULL);
1707                                 rc = l_wait_event(athread->t_ctl_waitq,
1708                                         lfsck_phase2_next_ready(lad) ||
1709                                         lad->lad_exit ||
1710                                         !thread_is_running(mthread),
1711                                         &lwi);
1712
1713                                 if (unlikely(lad->lad_exit ||
1714                                              !thread_is_running(mthread)))
1715                                         GOTO(cleanup, rc = 0);
1716
1717                                 if (rc == -ETIMEDOUT)
1718                                         continue;
1719
1720                                 if (rc < 0)
1721                                         GOTO(cleanup, rc);
1722
1723 p2_next:
1724                                 rc = lao->la_handler_p2(env, com);
1725                                 if (rc != 0)
1726                                         GOTO(cleanup, rc);
1727
1728                                 if (unlikely(lad->lad_exit ||
1729                                              !thread_is_running(mthread)))
1730                                         GOTO(cleanup, rc = 0);
1731                         }
1732                 }
1733         }
1734
1735 cleanup:
1736         /* Cleanup the unfinished requests. */
1737         spin_lock(&lad->lad_lock);
1738         if (rc < 0)
1739                 lad->lad_assistant_status = rc;
1740
1741         if (lad->lad_exit && lad->lad_post_result <= 0)
1742                 lao->la_fill_pos(env, com, &lfsck->li_pos_checkpoint);
1743
1744         thread_set_flags(athread, SVC_STOPPING);
1745         while (!list_empty(&lad->lad_req_list)) {
1746                 lar = list_entry(lad->lad_req_list.next,
1747                                  struct lfsck_assistant_req,
1748                                  lar_list);
1749                 list_del_init(&lar->lar_list);
1750                 lad->lad_prefetched--;
1751                 spin_unlock(&lad->lad_lock);
1752                 lao->la_req_fini(env, lar);
1753                 spin_lock(&lad->lad_lock);
1754         }
1755         spin_unlock(&lad->lad_lock);
1756
1757         LASSERTF(lad->lad_prefetched == 0, "unmatched prefeteched objs %d\n",
1758                  lad->lad_prefetched);
1759
1760         memset(lr, 0, sizeof(*lr));
1761         if (rc > 0) {
1762                 lr->lr_event = LE_PHASE2_DONE;
1763                 lr->lr_status = rc;
1764         } else if (rc == 0) {
1765                 if (lfsck->li_flags & LPF_ALL_TGT) {
1766                         lr->lr_event = LE_STOP;
1767                         lr->lr_status = LS_STOPPED;
1768                 } else {
1769                         lr->lr_event = LE_PEER_EXIT;
1770                         switch (lfsck->li_status) {
1771                         case LS_PAUSED:
1772                         case LS_CO_PAUSED:
1773                                 lr->lr_status = LS_CO_PAUSED;
1774                                 break;
1775                         case LS_STOPPED:
1776                         case LS_CO_STOPPED:
1777                                 lr->lr_status = LS_CO_STOPPED;
1778                                 break;
1779                         default:
1780                                 CDEBUG(D_LFSCK, "%s: LFSCK assistant unknown "
1781                                        "status: rc = %d\n",
1782                                        lfsck_lfsck2name(lfsck),
1783                                        lfsck->li_status);
1784                                 lr->lr_status = LS_CO_FAILED;
1785                                 break;
1786                         }
1787                 }
1788         } else {
1789                 if (lfsck->li_flags & LPF_ALL_TGT) {
1790                         lr->lr_event = LE_STOP;
1791                         lr->lr_status = LS_FAILED;
1792                 } else {
1793                         lr->lr_event = LE_PEER_EXIT;
1794                         lr->lr_status = LS_CO_FAILED;
1795                 }
1796         }
1797
1798         rc1 = lfsck_assistant_notify_others(env, com, lr);
1799         if (rc1 != 0) {
1800                 CDEBUG(D_LFSCK, "%s: LFSCK assistant failed to notify "
1801                        "others for %s quit: rc = %d\n",
1802                        lfsck_lfsck2name(lfsck), lad->lad_name, rc1);
1803                 rc = rc1;
1804         }
1805
1806         CDEBUG(D_LFSCK, "%s: LFSCK assistant sync before exit\n",
1807                lfsck_lfsck2name(lfsck));
1808
1809         /* Flush async updates before exit. */
1810         rc2 = dt_sync(env, lfsck->li_next);
1811
1812         CDEBUG(D_LFSCK, "%s: LFSCK assistant synced before exit: rc = %d\n",
1813                lfsck_lfsck2name(lfsck), rc2);
1814
1815         /* Under force exit case, some requests may be just freed without
1816          * verification, those objects should be re-handled when next run.
1817          * So not update the on-disk trace file under such case. */
1818         if (lad->lad_in_double_scan) {
1819                 if (!lad->lad_exit)
1820                         rc1 = lao->la_double_scan_result(env, com, rc);
1821
1822                 CDEBUG(D_LFSCK, "%s: LFSCK assistant phase2 scan "
1823                        "finished: rc = %d\n",
1824                        lfsck_lfsck2name(lfsck), rc1 != 0 ? rc1 : rc);
1825         }
1826
1827 fini:
1828         if (lad->lad_in_double_scan)
1829                 atomic_dec(&lfsck->li_double_scan_count);
1830
1831         spin_lock(&lad->lad_lock);
1832         lad->lad_assistant_status = (rc1 != 0 ? rc1 : rc);
1833         thread_set_flags(athread, SVC_STOPPED);
1834         wake_up_all(&mthread->t_ctl_waitq);
1835         lad->lad_task = NULL;
1836         spin_unlock(&lad->lad_lock);
1837
1838         CDEBUG(D_LFSCK, "%s: %s LFSCK assistant thread exit: rc = %d\n",
1839                lfsck_lfsck2name(lfsck), lad->lad_name,
1840                lad->lad_assistant_status);
1841
1842         lfsck_thread_args_fini(lta);
1843
1844         return rc;
1845 }