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