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