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