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