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