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