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