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