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