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