Whamcloud - gitweb
LU-12616 obclass: fix MDS start/stop race
[fs/lustre-release.git] / lustre / lfsck / lfsck_lib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_lib.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
33 #include <linux/kthread.h>
34 #include <linux/sched.h>
35 #include <linux/list.h>
36 #include <lu_object.h>
37 #include <dt_object.h>
38 #include <md_object.h>
39 #include <lustre_fld.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre_lfsck.h>
43
44 #include "lfsck_internal.h"
45
46 #define LFSCK_CHECKPOINT_SKIP   1
47
48 /* define lfsck thread key */
49 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
50
51 static void lfsck_key_fini(const struct lu_context *ctx,
52                            struct lu_context_key *key, void *data)
53 {
54         struct lfsck_thread_info *info = data;
55
56         lu_buf_free(&info->lti_linkea_buf);
57         lu_buf_free(&info->lti_linkea_buf2);
58         lu_buf_free(&info->lti_big_buf);
59         OBD_FREE_PTR(info);
60 }
61
62 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
63 LU_KEY_INIT_GENERIC(lfsck);
64
65 static struct list_head lfsck_instance_list;
66 static struct list_head lfsck_ost_orphan_list;
67 static struct list_head lfsck_mdt_orphan_list;
68 static DEFINE_SPINLOCK(lfsck_instance_lock);
69
70 const char *lfsck_flags_names[] = {
71         "scanned-once",
72         "inconsistent",
73         "upgrade",
74         "incomplete",
75         "crashed_lastid",
76         NULL
77 };
78
79 const char *lfsck_param_names[] = {
80         NULL,
81         "failout",
82         "dryrun",
83         "all_targets",
84         "broadcast",
85         "orphan",
86         "create_ostobj",
87         "create_mdtobj",
88         NULL,
89         "delay_create_ostobj",
90         NULL
91 };
92
93 enum lfsck_verify_lpf_types {
94         LVLT_BY_BOOKMARK        = 0,
95         LVLT_BY_NAMEENTRY       = 1,
96 };
97
98 static inline void
99 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
100 {
101         if (type == LFSCK_TYPE_LAYOUT) {
102                 ltd->ltd_layout_status = LS_MAX;
103                 ltd->ltd_layout_repaired = 0;
104         } else {
105                 ltd->ltd_namespace_status = LS_MAX;
106                 ltd->ltd_namespace_repaired = 0;
107         }
108 }
109
110 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
111 {
112         spin_lock_init(&ltds->ltd_lock);
113         init_rwsem(&ltds->ltd_rw_sem);
114         INIT_LIST_HEAD(&ltds->ltd_orphan);
115         ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
116         if (ltds->ltd_tgts_bitmap == NULL)
117                 return -ENOMEM;
118
119         return 0;
120 }
121
122 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
123 {
124         struct lfsck_tgt_desc   *ltd;
125         struct lfsck_tgt_desc   *next;
126         int                      idx;
127
128         down_write(&ltds->ltd_rw_sem);
129
130         list_for_each_entry_safe(ltd, next, &ltds->ltd_orphan,
131                                  ltd_orphan_list) {
132                 list_del_init(&ltd->ltd_orphan_list);
133                 lfsck_tgt_put(ltd);
134         }
135
136         if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
137                 up_write(&ltds->ltd_rw_sem);
138
139                 return;
140         }
141
142         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
143                 ltd = lfsck_ltd2tgt(ltds, idx);
144                 if (likely(ltd != NULL)) {
145                         LASSERT(list_empty(&ltd->ltd_layout_list));
146                         LASSERT(list_empty(&ltd->ltd_layout_phase_list));
147                         LASSERT(list_empty(&ltd->ltd_namespace_list));
148                         LASSERT(list_empty(&ltd->ltd_namespace_phase_list));
149
150                         ltds->ltd_tgtnr--;
151                         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
152                         lfsck_assign_tgt(ltds, NULL, idx);
153                         lfsck_tgt_put(ltd);
154                 }
155         }
156
157         LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
158                  ltds->ltd_tgtnr);
159
160         for (idx = 0; idx < TGT_PTRS; idx++) {
161                 if (ltds->ltd_tgts_idx[idx] != NULL) {
162                         OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
163                         ltds->ltd_tgts_idx[idx] = NULL;
164                 }
165         }
166
167         CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
168         ltds->ltd_tgts_bitmap = NULL;
169         up_write(&ltds->ltd_rw_sem);
170 }
171
172 static int __lfsck_add_target(const struct lu_env *env,
173                               struct lfsck_instance *lfsck,
174                               struct lfsck_tgt_desc *ltd,
175                               bool for_ost, bool locked)
176 {
177         struct lfsck_tgt_descs *ltds;
178         __u32                   index = ltd->ltd_index;
179         int                     rc    = 0;
180         ENTRY;
181
182         if (for_ost)
183                 ltds = &lfsck->li_ost_descs;
184         else
185                 ltds = &lfsck->li_mdt_descs;
186
187         if (!locked)
188                 down_write(&ltds->ltd_rw_sem);
189
190         LASSERT(ltds->ltd_tgts_bitmap != NULL);
191
192         if (index >= ltds->ltd_tgts_bitmap->size) {
193                 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
194                                     (__u32)BITS_PER_LONG);
195                 struct cfs_bitmap *old_bitmap = ltds->ltd_tgts_bitmap;
196                 struct cfs_bitmap *new_bitmap;
197
198                 while (newsize < index + 1)
199                         newsize <<= 1;
200
201                 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
202                 if (new_bitmap == NULL)
203                         GOTO(unlock, rc = -ENOMEM);
204
205                 if (ltds->ltd_tgtnr > 0)
206                         cfs_bitmap_copy(new_bitmap, old_bitmap);
207                 ltds->ltd_tgts_bitmap = new_bitmap;
208                 CFS_FREE_BITMAP(old_bitmap);
209         }
210
211         if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
212                 CERROR("%s: the device %s (%u) is registered already\n",
213                        lfsck_lfsck2name(lfsck),
214                        ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
215                 GOTO(unlock, rc = -EEXIST);
216         }
217
218         if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
219                 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
220                 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
221                         GOTO(unlock, rc = -ENOMEM);
222         }
223
224         lfsck_assign_tgt(ltds, ltd, index);
225         cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
226         ltds->ltd_tgtnr++;
227
228         GOTO(unlock, rc = 0);
229
230 unlock:
231         if (!locked)
232                 up_write(&ltds->ltd_rw_sem);
233
234         return rc;
235 }
236
237 static int lfsck_add_target_from_orphan(const struct lu_env *env,
238                                         struct lfsck_instance *lfsck)
239 {
240         struct lfsck_tgt_descs  *ltds    = &lfsck->li_ost_descs;
241         struct lfsck_tgt_desc   *ltd;
242         struct lfsck_tgt_desc   *next;
243         struct list_head        *head    = &lfsck_ost_orphan_list;
244         int                      rc;
245         bool                     for_ost = true;
246
247 again:
248         spin_lock(&lfsck_instance_lock);
249         list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
250                 if (ltd->ltd_key == lfsck->li_bottom)
251                         list_move_tail(&ltd->ltd_orphan_list,
252                                        &ltds->ltd_orphan);
253         }
254         spin_unlock(&lfsck_instance_lock);
255
256         down_write(&ltds->ltd_rw_sem);
257         while (!list_empty(&ltds->ltd_orphan)) {
258                 ltd = list_entry(ltds->ltd_orphan.next,
259                                  struct lfsck_tgt_desc,
260                                  ltd_orphan_list);
261                 list_del_init(&ltd->ltd_orphan_list);
262                 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
263                 /* Do not hold the semaphore for too long time. */
264                 up_write(&ltds->ltd_rw_sem);
265                 if (rc != 0)
266                         return rc;
267
268                 down_write(&ltds->ltd_rw_sem);
269         }
270         up_write(&ltds->ltd_rw_sem);
271
272         if (for_ost) {
273                 ltds = &lfsck->li_mdt_descs;
274                 head = &lfsck_mdt_orphan_list;
275                 for_ost = false;
276                 goto again;
277         }
278
279         return 0;
280 }
281
282 static inline struct lfsck_component *
283 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
284                        struct list_head *list)
285 {
286         struct lfsck_component *com;
287
288         list_for_each_entry(com, list, lc_link) {
289                 if (com->lc_type == type)
290                         return com;
291         }
292         return NULL;
293 }
294
295 struct lfsck_component *
296 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
297 {
298         struct lfsck_component *com;
299
300         spin_lock(&lfsck->li_lock);
301         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
302         if (com != NULL)
303                 goto unlock;
304
305         com = __lfsck_component_find(lfsck, type,
306                                      &lfsck->li_list_double_scan);
307         if (com != NULL)
308                 goto unlock;
309
310         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
311
312 unlock:
313         if (com != NULL)
314                 lfsck_component_get(com);
315         spin_unlock(&lfsck->li_lock);
316         return com;
317 }
318
319 void lfsck_component_cleanup(const struct lu_env *env,
320                              struct lfsck_component *com)
321 {
322         if (!list_empty(&com->lc_link))
323                 list_del_init(&com->lc_link);
324         if (!list_empty(&com->lc_link_dir))
325                 list_del_init(&com->lc_link_dir);
326
327         lfsck_component_put(env, com);
328 }
329
330 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
331                     struct lu_fid *fid, bool locked)
332 {
333         struct lfsck_bookmark   *bk = &lfsck->li_bookmark_ram;
334         int                      rc = 0;
335         ENTRY;
336
337         if (!locked)
338                 mutex_lock(&lfsck->li_mutex);
339
340         rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
341         if (rc >= 0) {
342                 bk->lb_last_fid = *fid;
343                 /* We do not care about whether the subsequent sub-operations
344                  * failed or not. The worst case is that one FID is lost that
345                  * is not a big issue for the LFSCK since it is relative rare
346                  * for LFSCK create. */
347                 rc = lfsck_bookmark_store(env, lfsck);
348         }
349
350         if (!locked)
351                 mutex_unlock(&lfsck->li_mutex);
352
353         RETURN(rc);
354 }
355
356 static int __lfsck_ibits_lock(const struct lu_env *env,
357                               struct lfsck_instance *lfsck,
358                               struct dt_object *obj, struct ldlm_res_id *resid,
359                               struct lustre_handle *lh, __u64 bits,
360                               enum ldlm_mode mode)
361 {
362         struct lfsck_thread_info        *info   = lfsck_env_info(env);
363         union ldlm_policy_data          *policy = &info->lti_policy;
364         __u64                            flags  = LDLM_FL_ATOMIC_CB;
365         int                              rc;
366
367         LASSERT(lfsck->li_namespace != NULL);
368
369         memset(policy, 0, sizeof(*policy));
370         policy->l_inodebits.bits = bits;
371         if (dt_object_remote(obj)) {
372                 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
373
374                 memset(einfo, 0, sizeof(*einfo));
375                 einfo->ei_type = LDLM_IBITS;
376                 einfo->ei_mode = mode;
377                 einfo->ei_cb_bl = ldlm_blocking_ast;
378                 einfo->ei_cb_cp = ldlm_completion_ast;
379                 einfo->ei_res_id = resid;
380
381                 rc = dt_object_lock(env, obj, lh, einfo, policy);
382                 /* for regular checks LFSCK doesn't use LDLM locking,
383                  * so the state isn't coherent. here we just took LDLM
384                  * lock for coherency and it's time to invalidate
385                  * previous state */
386                 if (rc == ELDLM_OK)
387                         dt_invalidate(env, obj);
388         } else {
389                 rc = ldlm_cli_enqueue_local(env, lfsck->li_namespace, resid,
390                                             LDLM_IBITS, policy, mode,
391                                             &flags, ldlm_blocking_ast,
392                                             ldlm_completion_ast, NULL, NULL,
393                                             0, LVB_T_NONE, NULL, lh);
394         }
395
396         if (rc == ELDLM_OK) {
397                 rc = 0;
398         } else {
399                 memset(lh, 0, sizeof(*lh));
400                 rc = -EIO;
401         }
402
403         return rc;
404 }
405
406 /**
407  * Request the specified ibits lock for the given object.
408  *
409  * Before the LFSCK modifying on the namespace visible object,
410  * it needs to acquire related ibits ldlm lock.
411  *
412  * \param[in] env       pointer to the thread context
413  * \param[in] lfsck     pointer to the lfsck instance
414  * \param[in] obj       pointer to the dt_object to be locked
415  * \param[out] lh       pointer to the lock handle
416  * \param[in] bits      the bits for the ldlm lock to be acquired
417  * \param[in] mode      the mode for the ldlm lock to be acquired
418  *
419  * \retval              0 for success
420  * \retval              negative error number on failure
421  */
422 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
423                      struct dt_object *obj, struct lustre_handle *lh,
424                      __u64 bits, enum ldlm_mode mode)
425 {
426         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
427
428         LASSERT(!lustre_handle_is_used(lh));
429
430         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
431         return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
432 }
433
434 /**
435  * Request the remote LOOKUP lock for the given object.
436  *
437  * If \a pobj is remote, the LOOKUP lock of \a obj is on the MDT where
438  * \a pobj is, acquire LOOKUP lock there.
439  *
440  * \param[in] env       pointer to the thread context
441  * \param[in] lfsck     pointer to the lfsck instance
442  * \param[in] pobj      pointer to parent dt_object
443  * \param[in] obj       pointer to the dt_object to be locked
444  * \param[out] lh       pointer to the lock handle
445  * \param[in] mode      the mode for the ldlm lock to be acquired
446  *
447  * \retval              0 for success
448  * \retval              negative error number on failure
449  */
450 int lfsck_remote_lookup_lock(const struct lu_env *env,
451                              struct lfsck_instance *lfsck,
452                              struct dt_object *pobj, struct dt_object *obj,
453                              struct lustre_handle *lh, enum ldlm_mode mode)
454 {
455         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
456
457         LASSERT(!lustre_handle_is_used(lh));
458
459         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
460         return __lfsck_ibits_lock(env, lfsck, pobj, resid, lh,
461                                   MDS_INODELOCK_LOOKUP, mode);
462 }
463
464 /**
465  * Release the the specified ibits lock.
466  *
467  * If the lock has been acquired before, release it
468  * and cleanup the handle. Otherwise, do nothing.
469  *
470  * \param[in] lh        pointer to the lock handle
471  * \param[in] mode      the mode for the ldlm lock to be released
472  */
473 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
474 {
475         if (lustre_handle_is_used(lh)) {
476                 ldlm_lock_decref(lh, mode);
477                 memset(lh, 0, sizeof(*lh));
478         }
479 }
480
481 /**
482  * Request compound ibits locks for the given <obj, name> pairs.
483  *
484  * Before the LFSCK modifying on the namespace visible object, it needs to
485  * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
486  * the lock purpose. But the simple lfsck_ibits_lock for directory-based
487  * modificationis (such as insert name entry to the directory) may be too
488  * coarse-grained and not efficient.
489  *
490  * The lfsck_lock() will request compound ibits locks on the specified
491  * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
492  * lock on the directory object, and the regular ibits lock on the name hash.
493  *
494  * \param[in] env       pointer to the thread context
495  * \param[in] lfsck     pointer to the lfsck instance
496  * \param[in] obj       pointer to the dt_object to be locked
497  * \param[in] name      used for building the PDO lock resource
498  * \param[out] llh      pointer to the lfsck_lock_handle
499  * \param[in] bits      the bits for the ldlm lock to be acquired
500  * \param[in] mode      the mode for the ldlm lock to be acquired
501  *
502  * \retval              0 for success
503  * \retval              negative error number on failure
504  */
505 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
506                struct dt_object *obj, const char *name,
507                struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
508 {
509         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
510         int                 rc;
511
512         LASSERT(S_ISDIR(lfsck_object_type(obj)));
513         LASSERT(name != NULL);
514         LASSERT(name[0] != 0);
515         LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
516         LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
517
518         switch (mode) {
519         case LCK_EX:
520                 llh->llh_pdo_mode = LCK_EX;
521                 break;
522         case LCK_PW:
523                 llh->llh_pdo_mode = LCK_CW;
524                 break;
525         case LCK_PR:
526                 llh->llh_pdo_mode = LCK_CR;
527                 break;
528         default:
529                 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
530                        DFID"\n", lfsck_lfsck2name(lfsck), mode,
531                        PFID(lfsck_dto2fid(obj)));
532                 LBUG();
533         }
534
535         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
536         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
537                                 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
538         if (rc != 0)
539                 return rc;
540
541         llh->llh_reg_mode = mode;
542         resid->name[LUSTRE_RES_ID_HSH_OFF] = ll_full_name_hash(NULL, name,
543                                                                strlen(name));
544         LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
545         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
546                                 bits, llh->llh_reg_mode);
547         if (rc != 0)
548                 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
549
550         return rc;
551 }
552
553 /**
554  * Release the the compound ibits locks.
555  *
556  * \param[in] llh       pointer to the lfsck_lock_handle to be released
557  */
558 void lfsck_unlock(struct lfsck_lock_handle *llh)
559 {
560         lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
561         lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
562 }
563
564 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
565                               struct lfsck_instance *lfsck,
566                               const struct lu_fid *fid)
567 {
568         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
569         struct lu_seq_range     *range  = &lfsck_env_info(env)->lti_range;
570         int                      rc;
571
572         if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
573                 /* "ROOT" is always on the MDT0. */
574                 if (lu_fid_eq(fid, &lfsck->li_global_root_fid))
575                         return 0;
576
577                 return lfsck_dev_idx(lfsck);
578         }
579
580         fld_range_set_mdt(range);
581         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
582         if (rc == 0)
583                 rc = range->lsr_index;
584
585         return rc;
586 }
587
588 const char dot[] = ".";
589 const char dotdot[] = "..";
590 static const char dotlustre[] = ".lustre";
591 static const char lostfound[] = "lost+found";
592
593 /**
594  * Remove the name entry from the .lustre/lost+found directory.
595  *
596  * No need to care about the object referenced by the name entry,
597  * either the name entry is invalid or redundant, or the referenced
598  * object has been processed or will be handled by others.
599  *
600  * \param[in] env       pointer to the thread context
601  * \param[in] lfsck     pointer to the lfsck instance
602  * \param[in] name      the name for the name entry to be removed
603  *
604  * \retval              0 for success
605  * \retval              negative error number on failure
606  */
607 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
608                                        struct lfsck_instance *lfsck,
609                                        const char *name)
610 {
611         struct dt_object        *parent = lfsck->li_lpf_root_obj;
612         struct dt_device        *dev    = lfsck_obj2dev(parent);
613         struct thandle          *th;
614         struct lfsck_lock_handle *llh   = &lfsck_env_info(env)->lti_llh;
615         int                      rc;
616         ENTRY;
617
618         rc = lfsck_lock(env, lfsck, parent, name, llh,
619                         MDS_INODELOCK_UPDATE, LCK_PW);
620         if (rc != 0)
621                 RETURN(rc);
622
623         th = dt_trans_create(env, dev);
624         if (IS_ERR(th))
625                 GOTO(unlock, rc = PTR_ERR(th));
626
627         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
628         if (rc != 0)
629                 GOTO(stop, rc);
630
631         rc = dt_declare_ref_del(env, parent, th);
632         if (rc != 0)
633                 GOTO(stop, rc);
634
635         rc = dt_trans_start_local(env, dev, th);
636         if (rc != 0)
637                 GOTO(stop, rc);
638
639         rc = dt_delete(env, parent, (const struct dt_key *)name, th);
640         if (rc != 0)
641                 GOTO(stop, rc);
642
643         dt_write_lock(env, parent, 0);
644         rc = dt_ref_del(env, parent, th);
645         dt_write_unlock(env, parent);
646
647         GOTO(stop, rc);
648
649 stop:
650         dt_trans_stop(env, dev, th);
651
652 unlock:
653         lfsck_unlock(llh);
654
655         CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
656                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
657
658         return rc;
659 }
660
661 static int lfsck_create_lpf_local(const struct lu_env *env,
662                                   struct lfsck_instance *lfsck,
663                                   struct dt_object *child,
664                                   struct lu_attr *la,
665                                   struct dt_object_format *dof,
666                                   const char *name)
667 {
668         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
669         struct dt_object        *parent = lfsck->li_lpf_root_obj;
670         struct dt_device        *dev    = lfsck_obj2dev(child);
671         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
672         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
673         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
674         struct thandle          *th     = NULL;
675         struct linkea_data       ldata  = { NULL };
676         struct lu_buf            linkea_buf;
677         const struct lu_name    *cname;
678         loff_t                   pos    = 0;
679         int                      len    = sizeof(struct lfsck_bookmark);
680         int                      rc;
681         ENTRY;
682
683         cname = lfsck_name_get_const(env, name, strlen(name));
684         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
685                               cname, lfsck_dto2fid(parent));
686         if (rc != 0)
687                 RETURN(rc);
688
689         th = dt_trans_create(env, dev);
690         if (IS_ERR(th))
691                 RETURN(PTR_ERR(th));
692
693         /* 1a. create child */
694         rc = dt_declare_create(env, child, la, NULL, dof, th);
695         if (rc != 0)
696                 GOTO(stop, rc);
697
698         if (!dt_try_as_dir(env, child))
699                 GOTO(stop, rc = -ENOTDIR);
700
701         /* 2a. increase child nlink */
702         rc = dt_declare_ref_add(env, child, th);
703         if (rc != 0)
704                 GOTO(stop, rc);
705
706         /* 3a. insert dot into child dir */
707         rec->rec_type = S_IFDIR;
708         rec->rec_fid = cfid;
709         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
710                                (const struct dt_key *)dot, th);
711         if (rc != 0)
712                 GOTO(stop, rc);
713
714         /* 4a. insert dotdot into child dir */
715         rec->rec_fid = &LU_LPF_FID;
716         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
717                                (const struct dt_key *)dotdot, th);
718         if (rc != 0)
719                 GOTO(stop, rc);
720
721         /* 5a. insert linkEA for child */
722         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
723                        ldata.ld_leh->leh_len);
724         rc = dt_declare_xattr_set(env, child, &linkea_buf,
725                                   XATTR_NAME_LINK, 0, th);
726         if (rc != 0)
727                 GOTO(stop, rc);
728
729         /* 6a. insert name into parent dir */
730         rec->rec_type = S_IFDIR;
731         rec->rec_fid = cfid;
732         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
733                                (const struct dt_key *)name, th);
734         if (rc != 0)
735                 GOTO(stop, rc);
736
737         /* 7a. increase parent nlink */
738         rc = dt_declare_ref_add(env, parent, th);
739         if (rc != 0)
740                 GOTO(stop, rc);
741
742         /* 8a. update bookmark */
743         rc = dt_declare_record_write(env, bk_obj,
744                                      lfsck_buf_get(env, bk, len), 0, th);
745         if (rc != 0)
746                 GOTO(stop, rc);
747
748         rc = dt_trans_start_local(env, dev, th);
749         if (rc != 0)
750                 GOTO(stop, rc);
751
752         dt_write_lock(env, child, 0);
753         /* 1b. create child */
754         rc = dt_create(env, child, la, NULL, dof, th);
755         if (rc != 0)
756                 GOTO(unlock, rc);
757
758         /* 2b. increase child nlink */
759         rc = dt_ref_add(env, child, th);
760         if (rc != 0)
761                 GOTO(unlock, rc);
762
763         /* 3b. insert dot into child dir */
764         rec->rec_fid = cfid;
765         rc = dt_insert(env, child, (const struct dt_rec *)rec,
766                        (const struct dt_key *)dot, th);
767         if (rc != 0)
768                 GOTO(unlock, rc);
769
770         /* 4b. insert dotdot into child dir */
771         rec->rec_fid = &LU_LPF_FID;
772         rc = dt_insert(env, child, (const struct dt_rec *)rec,
773                        (const struct dt_key *)dotdot, th);
774         if (rc != 0)
775                 GOTO(unlock, rc);
776
777         /* 5b. insert linkEA for child. */
778         rc = dt_xattr_set(env, child, &linkea_buf,
779                           XATTR_NAME_LINK, 0, th);
780         dt_write_unlock(env, child);
781         if (rc != 0)
782                 GOTO(stop, rc);
783
784         /* 6b. insert name into parent dir */
785         rec->rec_fid = cfid;
786         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
787                        (const struct dt_key *)name, th);
788         if (rc != 0)
789                 GOTO(stop, rc);
790
791         dt_write_lock(env, parent, 0);
792         /* 7b. increase parent nlink */
793         rc = dt_ref_add(env, parent, th);
794         dt_write_unlock(env, parent);
795         if (rc != 0)
796                 GOTO(stop, rc);
797
798         bk->lb_lpf_fid = *cfid;
799         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
800
801         /* 8b. update bookmark */
802         rc = dt_record_write(env, bk_obj,
803                              lfsck_buf_get(env, bk, len), &pos, th);
804
805         GOTO(stop, rc);
806
807 unlock:
808         dt_write_unlock(env, child);
809
810 stop:
811         dt_trans_stop(env, dev, th);
812
813         return rc;
814 }
815
816 static int lfsck_create_lpf_remote(const struct lu_env *env,
817                                    struct lfsck_instance *lfsck,
818                                    struct dt_object *child,
819                                    struct lu_attr *la,
820                                    struct dt_object_format *dof,
821                                    const char *name)
822 {
823         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
824         struct dt_object        *parent = lfsck->li_lpf_root_obj;
825         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
826         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
827         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
828         struct thandle          *th     = NULL;
829         struct linkea_data       ldata  = { NULL };
830         struct lu_buf            linkea_buf;
831         const struct lu_name    *cname;
832         struct dt_device        *dev;
833         loff_t                   pos    = 0;
834         int                      len    = sizeof(struct lfsck_bookmark);
835         int                      rc;
836         ENTRY;
837
838         cname = lfsck_name_get_const(env, name, strlen(name));
839         rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
840                               cname, lfsck_dto2fid(parent));
841         if (rc != 0)
842                 RETURN(rc);
843
844         /* Create .lustre/lost+found/MDTxxxx. */
845
846         /* XXX: Currently, cross-MDT create operation needs to create the child
847          *      object firstly, then insert name into the parent directory. For
848          *      this case, the child object resides on current MDT (local), but
849          *      the parent ".lustre/lost+found" may be on remote MDT. It is not
850          *      easy to contain all the sub-modifications orderly within single
851          *      transaction.
852          *
853          *      To avoid more inconsistency, we split the create operation into
854          *      two transactions:
855          *
856          *      1) create the child and update the lfsck_bookmark::lb_lpf_fid
857          *         locally.
858          *      2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
859          *         remotely.
860          *
861          *      If 1) done, but 2) failed, then go ahead, the LFSCK will try to
862          *      repair such inconsistency when LFSCK run next time. */
863
864         /* Transaction I: locally */
865
866         dev = lfsck_obj2dev(child);
867         th = dt_trans_create(env, dev);
868         if (IS_ERR(th))
869                 RETURN(PTR_ERR(th));
870
871         /* 1a. create child */
872         rc = dt_declare_create(env, child, la, NULL, dof, th);
873         if (rc != 0)
874                 GOTO(stop, rc);
875
876         if (!dt_try_as_dir(env, child))
877                 GOTO(stop, rc = -ENOTDIR);
878
879         /* 2a. increase child nlink */
880         rc = dt_declare_ref_add(env, child, th);
881         if (rc != 0)
882                 GOTO(stop, rc);
883
884         /* 3a. insert dot into child dir */
885         rec->rec_type = S_IFDIR;
886         rec->rec_fid = cfid;
887         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
888                                (const struct dt_key *)dot, th);
889         if (rc != 0)
890                 GOTO(stop, rc);
891
892         /* 4a. insert dotdot into child dir */
893         rec->rec_fid = &LU_LPF_FID;
894         rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
895                                (const struct dt_key *)dotdot, th);
896         if (rc != 0)
897                 GOTO(stop, rc);
898
899         /* 5a. insert linkEA for child */
900         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
901                        ldata.ld_leh->leh_len);
902         rc = dt_declare_xattr_set(env, child, &linkea_buf,
903                                   XATTR_NAME_LINK, 0, th);
904         if (rc != 0)
905                 GOTO(stop, rc);
906
907         /* 6a. update bookmark */
908         rc = dt_declare_record_write(env, bk_obj,
909                                      lfsck_buf_get(env, bk, len), 0, th);
910         if (rc != 0)
911                 GOTO(stop, rc);
912
913         rc = dt_trans_start_local(env, dev, th);
914         if (rc != 0)
915                 GOTO(stop, rc);
916
917         dt_write_lock(env, child, 0);
918         /* 1b. create child */
919         rc = dt_create(env, child, la, NULL, dof, th);
920         if (rc != 0)
921                 GOTO(unlock, rc);
922
923         /* 2b. increase child nlink */
924         rc = dt_ref_add(env, child, th);
925         if (rc != 0)
926                 GOTO(unlock, rc);
927
928         /* 3b. insert dot into child dir */
929         rec->rec_type = S_IFDIR;
930         rec->rec_fid = cfid;
931         rc = dt_insert(env, child, (const struct dt_rec *)rec,
932                        (const struct dt_key *)dot, th);
933         if (rc != 0)
934                 GOTO(unlock, rc);
935
936         /* 4b. insert dotdot into child dir */
937         rec->rec_fid = &LU_LPF_FID;
938         rc = dt_insert(env, child, (const struct dt_rec *)rec,
939                        (const struct dt_key *)dotdot, th);
940         if (rc != 0)
941                 GOTO(unlock, rc);
942
943         /* 5b. insert linkEA for child */
944         rc = dt_xattr_set(env, child, &linkea_buf,
945                           XATTR_NAME_LINK, 0, th);
946         if (rc != 0)
947                 GOTO(unlock, rc);
948
949         bk->lb_lpf_fid = *cfid;
950         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
951
952         /* 6b. update bookmark */
953         rc = dt_record_write(env, bk_obj,
954                              lfsck_buf_get(env, bk, len), &pos, th);
955
956         dt_write_unlock(env, child);
957         dt_trans_stop(env, dev, th);
958         if (rc != 0)
959                 RETURN(rc);
960
961         /* Transaction II: remotely */
962
963         dev = lfsck_obj2dev(parent);
964         th = dt_trans_create(env, dev);
965         if (IS_ERR(th))
966                 RETURN(PTR_ERR(th));
967
968         th->th_sync = 1;
969         /* 5a. insert name into parent dir */
970         rec->rec_fid = cfid;
971         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
972                                (const struct dt_key *)name, th);
973         if (rc != 0)
974                 GOTO(stop, rc);
975
976         /* 6a. increase parent nlink */
977         rc = dt_declare_ref_add(env, parent, th);
978         if (rc != 0)
979                 GOTO(stop, rc);
980
981         rc = dt_trans_start_local(env, dev, th);
982         if (rc != 0)
983                 GOTO(stop, rc);
984
985         /* 5b. insert name into parent dir */
986         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
987                        (const struct dt_key *)name, th);
988         if (rc != 0)
989                 GOTO(stop, rc);
990
991         dt_write_lock(env, parent, 0);
992         /* 6b. increase parent nlink */
993         rc = dt_ref_add(env, parent, th);
994         dt_write_unlock(env, parent);
995
996         GOTO(stop, rc);
997
998 unlock:
999         dt_write_unlock(env, child);
1000 stop:
1001         dt_trans_stop(env, dev, th);
1002
1003         if (rc != 0 && dev == lfsck_obj2dev(parent))
1004                 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
1005                        "for orphans, but failed to insert the name %s "
1006                        "to the .lustre/lost+found/. Such inconsistency "
1007                        "will be repaired when LFSCK run next time: rc = %d\n",
1008                        lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
1009
1010         return rc;
1011 }
1012
1013 /**
1014  * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
1015  *
1016  * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
1017  * orphans and other uncertain inconsistent objects found during the
1018  * LFSCK. Such directory will be created by the LFSCK engine on the
1019  * local MDT before the LFSCK scanning.
1020  *
1021  * \param[in] env       pointer to the thread context
1022  * \param[in] lfsck     pointer to the lfsck instance
1023  *
1024  * \retval              0 for success
1025  * \retval              negative error number on failure
1026  */
1027 static int lfsck_create_lpf(const struct lu_env *env,
1028                             struct lfsck_instance *lfsck)
1029 {
1030         struct lfsck_bookmark    *bk    = &lfsck->li_bookmark_ram;
1031         struct lfsck_thread_info *info  = lfsck_env_info(env);
1032         struct lu_fid            *cfid  = &info->lti_fid2;
1033         struct lu_attr           *la    = &info->lti_la;
1034         struct dt_object_format  *dof   = &info->lti_dof;
1035         struct dt_object         *parent = lfsck->li_lpf_root_obj;
1036         struct dt_object         *child = NULL;
1037         struct lfsck_lock_handle *llh   = &info->lti_llh;
1038         char                      name[8];
1039         int                       node  = lfsck_dev_idx(lfsck);
1040         int                       rc    = 0;
1041         ENTRY;
1042
1043         LASSERT(lfsck->li_master);
1044         LASSERT(parent != NULL);
1045         LASSERT(lfsck->li_lpf_obj == NULL);
1046
1047         snprintf(name, 8, "MDT%04x", node);
1048         rc = lfsck_lock(env, lfsck, parent, name, llh,
1049                         MDS_INODELOCK_UPDATE, LCK_PW);
1050         if (rc != 0)
1051                 RETURN(rc);
1052
1053         if (fid_is_zero(&bk->lb_lpf_fid)) {
1054                 /* There is corner case that: in former LFSCK scanning we have
1055                  * created the .lustre/lost+found/MDTxxxx but failed to update
1056                  * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1057                  * it from MDT0 firstly. */
1058                 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1059                                (const struct dt_key *)name);
1060                 if (rc != 0 && rc != -ENOENT)
1061                         GOTO(unlock, rc);
1062
1063                 if (rc == 0) {
1064                         bk->lb_lpf_fid = *cfid;
1065                         rc = lfsck_bookmark_store(env, lfsck);
1066                 } else {
1067                         rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1068                 }
1069                 if (rc != 0)
1070                         GOTO(unlock, rc);
1071         } else {
1072                 *cfid = bk->lb_lpf_fid;
1073         }
1074
1075         child = lfsck_object_find_bottom_new(env, lfsck, cfid);
1076         if (IS_ERR(child))
1077                 GOTO(unlock, rc = PTR_ERR(child));
1078
1079         if (dt_object_exists(child) != 0) {
1080                 if (unlikely(!dt_try_as_dir(env, child)))
1081                         rc = -ENOTDIR;
1082                 else
1083                         lfsck->li_lpf_obj = child;
1084
1085                 GOTO(unlock, rc);
1086         }
1087
1088         memset(la, 0, sizeof(*la));
1089         la->la_atime = la->la_mtime = la->la_ctime = ktime_get_real_seconds();
1090         la->la_mode = S_IFDIR | S_IRWXU;
1091         la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1092                        LA_UID | LA_GID;
1093         memset(dof, 0, sizeof(*dof));
1094         dof->dof_type = dt_mode_to_dft(S_IFDIR);
1095
1096         if (node == 0)
1097                 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1098         else
1099                 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1100         if (rc == 0)
1101                 lfsck->li_lpf_obj = child;
1102
1103         GOTO(unlock, rc);
1104
1105 unlock:
1106         lfsck_unlock(llh);
1107         if (rc != 0 && child != NULL && !IS_ERR(child))
1108                 lfsck_object_put(env, child);
1109
1110         return rc;
1111 }
1112
1113 /**
1114  * Scan .lustre/lost+found for bad name entries and remove them.
1115  *
1116  * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1117  * index in the system. Any other formatted name is invalid and should be
1118  * removed.
1119  *
1120  * \param[in] env       pointer to the thread context
1121  * \param[in] lfsck     pointer to the lfsck instance
1122  *
1123  * \retval              0 for success
1124  * \retval              negative error number on failure
1125  */
1126 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1127                                       struct lfsck_instance *lfsck)
1128 {
1129         struct dt_object        *parent = lfsck->li_lpf_root_obj;
1130         struct lu_dirent        *ent    =
1131                         (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1132         const struct dt_it_ops  *iops   = &parent->do_index_ops->dio_it;
1133         struct dt_it            *it;
1134         int                      rc;
1135         ENTRY;
1136
1137         it = iops->init(env, parent, LUDA_64BITHASH);
1138         if (IS_ERR(it))
1139                 RETURN(PTR_ERR(it));
1140
1141         rc = iops->load(env, it, 0);
1142         if (rc == 0)
1143                 rc = iops->next(env, it);
1144         else if (rc > 0)
1145                 rc = 0;
1146
1147         while (rc == 0) {
1148                 int off = 3;
1149
1150                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1151                 if (rc != 0)
1152                         break;
1153
1154                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1155                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1156                         goto next;
1157
1158                 /* name length must be strlen("MDTxxxx") */
1159                 if (ent->lde_namelen != 7)
1160                         goto remove;
1161
1162                 if (memcmp(ent->lde_name, "MDT", off) != 0)
1163                         goto remove;
1164
1165                 while (off < 7 && isxdigit(ent->lde_name[off]))
1166                         off++;
1167
1168                 if (off != 7) {
1169
1170 remove:
1171                         rc = lfsck_lpf_remove_name_entry(env, lfsck,
1172                                                          ent->lde_name);
1173                         if (rc != 0)
1174                                 break;
1175                 }
1176
1177 next:
1178                 rc = iops->next(env, it);
1179         }
1180
1181         iops->put(env, it);
1182         iops->fini(env, it);
1183
1184         RETURN(rc > 0 ? 0 : rc);
1185 }
1186
1187 static int lfsck_update_lpf_entry(const struct lu_env *env,
1188                                   struct lfsck_instance *lfsck,
1189                                   struct dt_object *parent,
1190                                   struct dt_object *child,
1191                                   const char *name,
1192                                   enum lfsck_verify_lpf_types type)
1193 {
1194         int rc;
1195
1196         if (type == LVLT_BY_BOOKMARK) {
1197                 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1198                                              lfsck_dto2fid(child), S_IFDIR);
1199         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1200                 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1201                 rc = lfsck_bookmark_store(env, lfsck);
1202
1203                 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1204                        " in the bookmark file: rc = %d\n",
1205                        lfsck_lfsck2name(lfsck),
1206                        PFID(lfsck_dto2fid(child)), rc);
1207         }
1208
1209         return rc;
1210 }
1211
1212 /**
1213  * Check whether the @child back references the @parent.
1214  *
1215  * Two cases:
1216  * 1) The child's FID is stored in the bookmark file. If the child back
1217  *    references the parent (LU_LPF_FID object) via its ".." entry, then
1218  *    insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1219  *    the child back references another parent2, then:
1220  * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1221  * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1222  *      references the child. So keep them there. As the LFSCK processing,
1223  *      the parent3 may be found, then when the LFSCK run next time, the
1224  *      inconsistency can be repaired.
1225  *
1226  * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1227  *    entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1228  *    via its ".." entry, then update the bookmark file, otherwise, if the child
1229  *    back references another parent2, then:
1230  * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1231  *      from .lustre/lost+found/;
1232  * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1233  *      sub-directory name entry and update the child;
1234  * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1235  *      or not, then keep them there.
1236  *
1237  * \param[in] env       pointer to the thread context
1238  * \param[in] lfsck     pointer to the lfsck instance
1239  * \param[in] child     pointer to the lost+found sub-directory object
1240  * \param[in] name      the name for lost+found sub-directory object
1241  * \param[out] fid      pointer to the buffer to hold the FID of the object
1242  *                      (called it as parent2) that is referenced via the
1243  *                      child's dotdot entry; it also can be the FID that
1244  *                      is referenced by the name entry under the parent2.
1245  * \param[in] type      to indicate where the child's FID is stored in
1246  *
1247  * \retval              positive number for uncertain inconsistency
1248  * \retval              0 for success
1249  * \retval              negative error number on failure
1250  */
1251 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1252                                   struct lfsck_instance *lfsck,
1253                                   struct dt_object *child, const char *name,
1254                                   struct lu_fid *fid,
1255                                   enum lfsck_verify_lpf_types type)
1256 {
1257         struct dt_object         *parent  = lfsck->li_lpf_root_obj;
1258         struct lfsck_thread_info *info    = lfsck_env_info(env);
1259         char                     *name2   = info->lti_key;
1260         struct lu_fid            *fid2    = &info->lti_fid3;
1261         struct dt_object         *parent2 = NULL;
1262         struct lustre_handle      lh      = { 0 };
1263         int                       rc;
1264         ENTRY;
1265
1266         fid_zero(fid);
1267         rc = dt_lookup(env, child, (struct dt_rec *)fid,
1268                        (const struct dt_key *)dotdot);
1269         if (rc != 0)
1270                 GOTO(linkea, rc);
1271
1272         if (!fid_is_sane(fid))
1273                 GOTO(linkea, rc = -EINVAL);
1274
1275         if (lu_fid_eq(fid, &LU_LPF_FID)) {
1276                 const struct lu_name *cname;
1277
1278                 if (lfsck->li_lpf_obj == NULL) {
1279                         lu_object_get(&child->do_lu);
1280                         lfsck->li_lpf_obj = child;
1281                 }
1282
1283                 cname = lfsck_name_get_const(env, name, strlen(name));
1284                 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1285                 if (rc == 0)
1286                         rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1287                                                     name, type);
1288
1289                 GOTO(out_done, rc);
1290         }
1291
1292         parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1293         if (IS_ERR(parent2))
1294                 GOTO(linkea, parent2);
1295
1296         if (!dt_object_exists(parent2)) {
1297                 lfsck_object_put(env, parent2);
1298
1299                 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1300         }
1301
1302         if (!dt_try_as_dir(env, parent2)) {
1303                 lfsck_object_put(env, parent2);
1304
1305                 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1306         }
1307
1308 linkea:
1309         /* To prevent rename/unlink race */
1310         rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1311                               MDS_INODELOCK_UPDATE, LCK_PR);
1312         if (rc != 0)
1313                 GOTO(out_put, rc);
1314
1315         dt_read_lock(env, child, 0);
1316         rc = lfsck_links_get_first(env, child, name2, fid2);
1317         if (rc != 0) {
1318                 dt_read_unlock(env, child);
1319                 lfsck_ibits_unlock(&lh, LCK_PR);
1320
1321                 GOTO(out_put, rc = 1);
1322         }
1323
1324         /* It is almost impossible that the bookmark file (or the name entry)
1325          * and the linkEA hit the same data corruption. Trust the linkEA. */
1326         if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1327                 dt_read_unlock(env, child);
1328                 lfsck_ibits_unlock(&lh, LCK_PR);
1329
1330                 *fid = *fid2;
1331                 if (lfsck->li_lpf_obj == NULL) {
1332                         lu_object_get(&child->do_lu);
1333                         lfsck->li_lpf_obj = child;
1334                 }
1335
1336                 /* Update the child's dotdot entry */
1337                 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1338                                              &LU_LPF_FID, S_IFDIR);
1339                 if (rc == 0)
1340                         rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1341                                                     name, type);
1342
1343                 GOTO(out_put, rc);
1344         }
1345
1346         if (parent2 == NULL || IS_ERR(parent2)) {
1347                 dt_read_unlock(env, child);
1348                 lfsck_ibits_unlock(&lh, LCK_PR);
1349
1350                 GOTO(out_done, rc = 1);
1351         }
1352
1353         rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1354                        (const struct dt_key *)name2);
1355         dt_read_unlock(env, child);
1356         lfsck_ibits_unlock(&lh, LCK_PR);
1357         if (rc != 0 && rc != -ENOENT)
1358                 GOTO(out_put, rc);
1359
1360         if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1361                 if (type == LVLT_BY_BOOKMARK)
1362                         GOTO(out_put, rc = 1);
1363
1364                 /* Trust the name entry, update the child's dotdot entry. */
1365                 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1366                                              &LU_LPF_FID, S_IFDIR);
1367
1368                 GOTO(out_put, rc);
1369         }
1370
1371         if (type == LVLT_BY_BOOKMARK) {
1372                 /* Invalid FID record in the bookmark file, reset it. */
1373                 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1374                 rc = lfsck_bookmark_store(env, lfsck);
1375
1376                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1377                        " in the bookmark file: rc = %d\n",
1378                        lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1379         } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1380                 /* The name entry is wrong, remove it. */
1381                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1382         }
1383
1384         GOTO(out_put, rc);
1385
1386 out_put:
1387         if (parent2 != NULL && !IS_ERR(parent2))
1388                 lfsck_object_put(env, parent2);
1389
1390 out_done:
1391         return rc;
1392 }
1393
1394 /**
1395  * Verify the /ROOT/.lustre/lost+found/ directory.
1396  *
1397  * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1398  * the LFSCK does not exactly know how to handle, such as orphans. So before
1399  * the LFSCK scanning the system, the consistency of such directory needs to
1400  * be verified firstly to allow the users to use it during the LFSCK.
1401  *
1402  * \param[in] env       pointer to the thread context
1403  * \param[in] lfsck     pointer to the lfsck instance
1404  *
1405  * \retval              positive number for uncertain inconsistency
1406  * \retval              0 for success
1407  * \retval              negative error number on failure
1408  */
1409 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1410 {
1411         struct lfsck_thread_info *info   = lfsck_env_info(env);
1412         struct lu_fid            *pfid   = &info->lti_fid;
1413         struct lu_fid            *cfid   = &info->lti_fid2;
1414         struct lfsck_bookmark    *bk     = &lfsck->li_bookmark_ram;
1415         struct dt_object         *parent;
1416         /* child1's FID is in the bookmark file. */
1417         struct dt_object         *child1 = NULL;
1418         /* child2's FID is in the name entry MDTxxxx. */
1419         struct dt_object         *child2 = NULL;
1420         const struct lu_name     *cname;
1421         char                      name[8];
1422         int                       node   = lfsck_dev_idx(lfsck);
1423         int                       rc     = 0;
1424         ENTRY;
1425
1426         LASSERT(lfsck->li_master);
1427
1428         if (lfsck->li_lpf_root_obj != NULL)
1429                 RETURN(0);
1430
1431         if (node == 0) {
1432                 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1433                                                   &LU_LPF_FID);
1434         } else {
1435                 struct lfsck_tgt_desc *ltd;
1436
1437                 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1438                 if (unlikely(ltd == NULL))
1439                         RETURN(-ENXIO);
1440
1441                 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1442                                                   &LU_LPF_FID);
1443                 lfsck_tgt_put(ltd);
1444         }
1445
1446         if (IS_ERR(parent))
1447                 RETURN(PTR_ERR(parent));
1448
1449         LASSERT(dt_object_exists(parent));
1450
1451         if (unlikely(!dt_try_as_dir(env, parent))) {
1452                 lfsck_object_put(env, parent);
1453
1454                 GOTO(put, rc = -ENOTDIR);
1455         }
1456
1457         lfsck->li_lpf_root_obj = parent;
1458         if (node == 0) {
1459                 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1460                 if (rc != 0)
1461                         CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1462                                "for bad sub-directories: rc = %d\n",
1463                                lfsck_lfsck2name(lfsck), rc);
1464         }
1465
1466         /* child2 */
1467         snprintf(name, 8, "MDT%04x", node);
1468         rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1469                        (const struct dt_key *)name);
1470         if (rc == -ENOENT) {
1471                 rc = 0;
1472                 goto find_child1;
1473         }
1474
1475         if (rc != 0)
1476                 GOTO(put, rc);
1477
1478         /* Invalid FID in the name entry, remove the name entry. */
1479         if (!fid_is_norm(cfid)) {
1480                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1481                 if (rc != 0)
1482                         GOTO(put, rc);
1483
1484                 goto find_child1;
1485         }
1486
1487         child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1488         if (IS_ERR(child2))
1489                 GOTO(put, rc = PTR_ERR(child2));
1490
1491         if (unlikely(!dt_object_exists(child2) ||
1492                      dt_object_remote(child2)) ||
1493                      !S_ISDIR(lfsck_object_type(child2))) {
1494                 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1495                 if (rc != 0)
1496                         GOTO(put, rc);
1497
1498                 goto find_child1;
1499         }
1500
1501         if (unlikely(!dt_try_as_dir(env, child2))) {
1502                 lfsck_object_put(env, child2);
1503                 child2 = NULL;
1504                 rc = -ENOTDIR;
1505         }
1506
1507 find_child1:
1508         if (fid_is_zero(&bk->lb_lpf_fid))
1509                 goto check_child2;
1510
1511         if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1512                 if (lfsck->li_lpf_obj == NULL) {
1513                         lu_object_get(&child2->do_lu);
1514                         lfsck->li_lpf_obj = child2;
1515                 }
1516
1517                 cname = lfsck_name_get_const(env, name, strlen(name));
1518                 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1519
1520                 GOTO(put, rc);
1521         }
1522
1523         if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1524                 struct lu_fid tfid = bk->lb_lpf_fid;
1525
1526                 /* Invalid FID record in the bookmark file, reset it. */
1527                 fid_zero(&bk->lb_lpf_fid);
1528                 rc = lfsck_bookmark_store(env, lfsck);
1529
1530                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1531                        " in the bookmark file: rc = %d\n",
1532                        lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1533
1534                 if (rc != 0)
1535                         GOTO(put, rc);
1536
1537                 goto check_child2;
1538         }
1539
1540         child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1541         if (IS_ERR(child1)) {
1542                 child1 = NULL;
1543                 goto check_child2;
1544         }
1545
1546         if (unlikely(!dt_object_exists(child1) ||
1547                      dt_object_remote(child1)) ||
1548                      !S_ISDIR(lfsck_object_type(child1))) {
1549                 /* Invalid FID record in the bookmark file, reset it. */
1550                 fid_zero(&bk->lb_lpf_fid);
1551                 rc = lfsck_bookmark_store(env, lfsck);
1552
1553                 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1554                        " in the bookmark file: rc = %d\n",
1555                        lfsck_lfsck2name(lfsck),
1556                        PFID(lfsck_dto2fid(child1)), rc);
1557
1558                 if (rc != 0)
1559                         GOTO(put, rc);
1560
1561                 lfsck_object_put(env, child1);
1562                 child1 = NULL;
1563                 goto check_child2;
1564         }
1565
1566         if (unlikely(!dt_try_as_dir(env, child1))) {
1567                 lfsck_object_put(env, child1);
1568                 child1 = NULL;
1569                 rc = -ENOTDIR;
1570                 goto check_child2;
1571         }
1572
1573         rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1574                                     LVLT_BY_BOOKMARK);
1575         if (lu_fid_eq(pfid, &LU_LPF_FID))
1576                 GOTO(put, rc);
1577
1578 check_child2:
1579         if (child2 != NULL)
1580                 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1581                                             pfid, LVLT_BY_NAMEENTRY);
1582
1583         GOTO(put, rc);
1584
1585 put:
1586         if (lfsck->li_lpf_obj != NULL) {
1587                 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1588                         lfsck_object_put(env, lfsck->li_lpf_obj);
1589                         lfsck->li_lpf_obj = NULL;
1590                         rc = -ENOTDIR;
1591                 }
1592         } else if (rc == 0) {
1593                 rc = lfsck_create_lpf(env, lfsck);
1594         }
1595
1596         if (child2 != NULL && !IS_ERR(child2))
1597                 lfsck_object_put(env, child2);
1598         if (child1 != NULL && !IS_ERR(child1))
1599                 lfsck_object_put(env, child1);
1600
1601         return rc;
1602 }
1603
1604 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1605 {
1606         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
1607         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
1608         char                    *prefix;
1609         int                      rc     = 0;
1610         ENTRY;
1611
1612         if (unlikely(ss == NULL))
1613                 RETURN(-ENXIO);
1614
1615         OBD_ALLOC_PTR(lfsck->li_seq);
1616         if (lfsck->li_seq == NULL)
1617                 RETURN(-ENOMEM);
1618
1619         OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1620         if (prefix == NULL)
1621                 GOTO(out, rc = -ENOMEM);
1622
1623         snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1624         rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1625                              ss->ss_server_seq);
1626         OBD_FREE(prefix, MAX_OBD_NAME + 7);
1627         if (rc != 0)
1628                 GOTO(out, rc);
1629
1630         if (fid_is_sane(&bk->lb_last_fid))
1631                 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1632
1633         RETURN(0);
1634
1635 out:
1636         OBD_FREE_PTR(lfsck->li_seq);
1637         lfsck->li_seq = NULL;
1638
1639         return rc;
1640 }
1641
1642 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1643 {
1644         if (lfsck->li_seq != NULL) {
1645                 seq_client_fini(lfsck->li_seq);
1646                 OBD_FREE_PTR(lfsck->li_seq);
1647                 lfsck->li_seq = NULL;
1648         }
1649 }
1650
1651 void lfsck_instance_cleanup(const struct lu_env *env,
1652                             struct lfsck_instance *lfsck)
1653 {
1654         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1655         struct lfsck_component  *com;
1656         struct lfsck_component  *next;
1657         struct lfsck_lmv_unit   *llu;
1658         struct lfsck_lmv_unit   *llu_next;
1659         struct lfsck_lmv        *llmv;
1660         ENTRY;
1661
1662         LASSERT(list_empty(&lfsck->li_link));
1663         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1664
1665         if (lfsck->li_obj_oit != NULL) {
1666                 lfsck_object_put(env, lfsck->li_obj_oit);
1667                 lfsck->li_obj_oit = NULL;
1668         }
1669
1670         LASSERT(lfsck->li_obj_dir == NULL);
1671         LASSERT(lfsck->li_lmv == NULL);
1672
1673         list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1674                 llmv = &llu->llu_lmv;
1675
1676                 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1677                          "still in using: %u\n",
1678                          atomic_read(&llmv->ll_ref));
1679
1680                 lfsck_lmv_put(env, llmv);
1681         }
1682
1683         list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1684                 lfsck_component_cleanup(env, com);
1685         }
1686
1687         LASSERT(list_empty(&lfsck->li_list_dir));
1688
1689         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1690                                  lc_link) {
1691                 lfsck_component_cleanup(env, com);
1692         }
1693
1694         list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1695                 lfsck_component_cleanup(env, com);
1696         }
1697
1698         lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1699         lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1700
1701         if (lfsck->li_lfsck_dir != NULL) {
1702                 lfsck_object_put(env, lfsck->li_lfsck_dir);
1703                 lfsck->li_lfsck_dir = NULL;
1704         }
1705
1706         if (lfsck->li_bookmark_obj != NULL) {
1707                 lfsck_object_put(env, lfsck->li_bookmark_obj);
1708                 lfsck->li_bookmark_obj = NULL;
1709         }
1710
1711         if (lfsck->li_lpf_obj != NULL) {
1712                 lfsck_object_put(env, lfsck->li_lpf_obj);
1713                 lfsck->li_lpf_obj = NULL;
1714         }
1715
1716         if (lfsck->li_lpf_root_obj != NULL) {
1717                 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1718                 lfsck->li_lpf_root_obj = NULL;
1719         }
1720
1721         if (lfsck->li_los != NULL) {
1722                 local_oid_storage_fini(env, lfsck->li_los);
1723                 lfsck->li_los = NULL;
1724         }
1725
1726         lfsck_fid_fini(lfsck);
1727
1728         OBD_FREE_PTR(lfsck);
1729 }
1730
1731 static inline struct lfsck_instance *
1732 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1733 {
1734         struct lfsck_instance *lfsck;
1735
1736         list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1737                 if (lfsck->li_bottom == key) {
1738                         if (ref)
1739                                 lfsck_instance_get(lfsck);
1740                         if (unlink)
1741                                 list_del_init(&lfsck->li_link);
1742
1743                         return lfsck;
1744                 }
1745         }
1746
1747         return NULL;
1748 }
1749
1750 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1751                                            bool unlink)
1752 {
1753         struct lfsck_instance *lfsck;
1754
1755         spin_lock(&lfsck_instance_lock);
1756         lfsck = __lfsck_instance_find(key, ref, unlink);
1757         spin_unlock(&lfsck_instance_lock);
1758
1759         return lfsck;
1760 }
1761
1762 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1763 {
1764         struct lfsck_instance *tmp;
1765
1766         spin_lock(&lfsck_instance_lock);
1767         list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1768                 if (lfsck->li_bottom == tmp->li_bottom) {
1769                         spin_unlock(&lfsck_instance_lock);
1770                         return -EEXIST;
1771                 }
1772         }
1773
1774         list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1775         spin_unlock(&lfsck_instance_lock);
1776         return 0;
1777 }
1778
1779 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1780                      const char *prefix)
1781 {
1782         int flag;
1783         int i;
1784         bool newline = (bits != 0 ? false : true);
1785
1786         seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1787
1788         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1789                 if (flag & bits) {
1790                         bits &= ~flag;
1791                         if (names[i] != NULL) {
1792                                 if (bits == 0)
1793                                         newline = true;
1794
1795                                 seq_printf(m, "%s%c", names[i],
1796                                            newline ? '\n' : ',');
1797                         }
1798                 }
1799         }
1800
1801         if (!newline)
1802                 seq_putc(m, '\n');
1803 }
1804
1805 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name)
1806 {
1807         if (time == 0) {
1808                 seq_printf(m, "%s_time: N/A\n", name);
1809                 seq_printf(m, "time_since_%s: N/A\n", name);
1810         } else {
1811                 seq_printf(m, "%s_time: %lld\n", name, time);
1812                 seq_printf(m, "time_since_%s: %lld seconds\n",
1813                            name, ktime_get_real_seconds() - time);
1814         }
1815 }
1816
1817 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1818                     const char *prefix)
1819 {
1820         if (fid_is_zero(&pos->lp_dir_parent)) {
1821                 if (pos->lp_oit_cookie == 0) {
1822                         seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1823                         return;
1824                 }
1825                 seq_printf(m, "%s: %llu, N/A, N/A\n",
1826                            prefix, pos->lp_oit_cookie);
1827         } else {
1828                 seq_printf(m, "%s: %llu, "DFID", %#llx\n",
1829                            prefix, pos->lp_oit_cookie,
1830                            PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1831         }
1832 }
1833
1834 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1835                     struct lfsck_position *pos, bool init)
1836 {
1837         const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1838
1839         if (unlikely(lfsck->li_di_oit == NULL)) {
1840                 memset(pos, 0, sizeof(*pos));
1841                 return;
1842         }
1843
1844         pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1845         if (!lfsck->li_current_oit_processed && !init)
1846                 pos->lp_oit_cookie--;
1847
1848         if (unlikely(pos->lp_oit_cookie == 0))
1849                 pos->lp_oit_cookie = 1;
1850
1851         if (lfsck->li_di_dir != NULL) {
1852                 struct dt_object *dto = lfsck->li_obj_dir;
1853
1854                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1855                                                         lfsck->li_di_dir);
1856
1857                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1858                         fid_zero(&pos->lp_dir_parent);
1859                         pos->lp_dir_cookie = 0;
1860                 } else {
1861                         pos->lp_dir_parent = *lfsck_dto2fid(dto);
1862                 }
1863         } else {
1864                 fid_zero(&pos->lp_dir_parent);
1865                 pos->lp_dir_cookie = 0;
1866         }
1867 }
1868
1869 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1870 {
1871         bool dirty = false;
1872
1873         if (limit != LFSCK_SPEED_NO_LIMIT) {
1874                 if (limit > cfs_time_seconds(1)) {
1875                         lfsck->li_sleep_rate = limit / cfs_time_seconds(1);
1876                         lfsck->li_sleep_jif = 1;
1877                 } else {
1878                         lfsck->li_sleep_rate = 1;
1879                         lfsck->li_sleep_jif = cfs_time_seconds(1) / limit;
1880                 }
1881         } else {
1882                 lfsck->li_sleep_jif = 0;
1883                 lfsck->li_sleep_rate = 0;
1884         }
1885
1886         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1887                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1888                 dirty = true;
1889         }
1890
1891         return dirty;
1892 }
1893
1894 void lfsck_control_speed(struct lfsck_instance *lfsck)
1895 {
1896         struct ptlrpc_thread *thread = &lfsck->li_thread;
1897         struct l_wait_info    lwi;
1898
1899         if (lfsck->li_sleep_jif > 0 &&
1900             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1901                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1902                                        LWI_ON_SIGNAL_NOOP, NULL);
1903
1904                 l_wait_event(thread->t_ctl_waitq,
1905                              !thread_is_running(thread),
1906                              &lwi);
1907                 lfsck->li_new_scanned = 0;
1908         }
1909 }
1910
1911 void lfsck_control_speed_by_self(struct lfsck_component *com)
1912 {
1913         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1914         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1915         struct l_wait_info       lwi;
1916
1917         if (lfsck->li_sleep_jif > 0 &&
1918             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1919                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1920                                        LWI_ON_SIGNAL_NOOP, NULL);
1921
1922                 l_wait_event(thread->t_ctl_waitq,
1923                              !thread_is_running(thread),
1924                              &lwi);
1925                 com->lc_new_scanned = 0;
1926         }
1927 }
1928
1929 static struct lfsck_thread_args *
1930 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1931                        struct lfsck_component *com,
1932                        struct lfsck_start_param *lsp)
1933 {
1934         struct lfsck_thread_args *lta;
1935         int                       rc;
1936
1937         OBD_ALLOC_PTR(lta);
1938         if (lta == NULL)
1939                 return ERR_PTR(-ENOMEM);
1940
1941         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1942         if (rc != 0) {
1943                 OBD_FREE_PTR(lta);
1944                 return ERR_PTR(rc);
1945         }
1946
1947         lta->lta_lfsck = lfsck_instance_get(lfsck);
1948         if (com != NULL)
1949                 lta->lta_com = lfsck_component_get(com);
1950
1951         lta->lta_lsp = lsp;
1952
1953         return lta;
1954 }
1955
1956 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1957 {
1958         if (lta->lta_com != NULL)
1959                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1960         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1961         lu_env_fini(&lta->lta_env);
1962         OBD_FREE_PTR(lta);
1963 }
1964
1965 struct lfsck_assistant_data *
1966 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1967                           const char *name)
1968 {
1969         struct lfsck_assistant_data *lad;
1970
1971         OBD_ALLOC_PTR(lad);
1972         if (lad != NULL) {
1973                 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1974                 if (lad->lad_bitmap == NULL) {
1975                         OBD_FREE_PTR(lad);
1976                         return NULL;
1977                 }
1978
1979                 INIT_LIST_HEAD(&lad->lad_req_list);
1980                 spin_lock_init(&lad->lad_lock);
1981                 INIT_LIST_HEAD(&lad->lad_ost_list);
1982                 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1983                 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1984                 INIT_LIST_HEAD(&lad->lad_mdt_list);
1985                 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1986                 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1987                 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1988                 lad->lad_ops = lao;
1989                 lad->lad_name = name;
1990         }
1991
1992         return lad;
1993 }
1994
1995 struct lfsck_assistant_object *
1996 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1997                             const struct lu_attr *attr, __u64 cookie,
1998                             bool is_dir)
1999 {
2000         struct lfsck_assistant_object   *lso;
2001
2002         OBD_ALLOC_PTR(lso);
2003         if (lso == NULL)
2004                 return ERR_PTR(-ENOMEM);
2005
2006         lso->lso_fid = *fid;
2007         if (attr != NULL)
2008                 lso->lso_attr = *attr;
2009
2010         atomic_set(&lso->lso_ref, 1);
2011         lso->lso_oit_cookie = cookie;
2012         if (is_dir)
2013                 lso->lso_is_dir = 1;
2014
2015         return lso;
2016 }
2017
2018 struct dt_object *
2019 lfsck_assistant_object_load(const struct lu_env *env,
2020                             struct lfsck_instance *lfsck,
2021                             struct lfsck_assistant_object *lso)
2022 {
2023         struct dt_object *obj;
2024
2025         obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2026         if (IS_ERR(obj))
2027                 return obj;
2028
2029         if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2030                 lso->lso_dead = 1;
2031                 lfsck_object_put(env, obj);
2032
2033                 return ERR_PTR(-ENOENT);
2034         }
2035
2036         if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2037                 lfsck_object_put(env, obj);
2038
2039                 return ERR_PTR(-ENOTDIR);
2040         }
2041
2042         return obj;
2043 }
2044
2045 /**
2046  * Generic LFSCK asynchronous communication interpretor function.
2047  * The LFSCK RPC reply for both the event notification and status
2048  * querying will be handled here.
2049  *
2050  * \param[in] env       pointer to the thread context
2051  * \param[in] req       pointer to the LFSCK request
2052  * \param[in] args      pointer to the lfsck_async_interpret_args
2053  * \param[in] rc        the result for handling the LFSCK request
2054  *
2055  * \retval              0 for success
2056  * \retval              negative error number on failure
2057  */
2058 int lfsck_async_interpret_common(const struct lu_env *env,
2059                                  struct ptlrpc_request *req,
2060                                  void *args, int rc)
2061 {
2062         struct lfsck_async_interpret_args *laia = args;
2063         struct lfsck_component            *com  = laia->laia_com;
2064         struct lfsck_assistant_data       *lad  = com->lc_data;
2065         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
2066         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
2067         struct lfsck_request              *lr   = laia->laia_lr;
2068
2069         LASSERT(com->lc_lfsck->li_master);
2070
2071         switch (lr->lr_event) {
2072         case LE_START:
2073                 if (unlikely(rc == -EINPROGRESS)) {
2074                         ltd->ltd_retry_start = 1;
2075                         break;
2076                 }
2077
2078                 if (rc != 0) {
2079                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2080                                "start: rc = %d\n",
2081                                lfsck_lfsck2name(com->lc_lfsck),
2082                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2083                                ltd->ltd_index, lad->lad_name, rc);
2084
2085                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2086                                 struct lfsck_layout *lo = com->lc_file_ram;
2087
2088                                 if (lr->lr_flags & LEF_TO_OST)
2089                                         lfsck_lad_set_bitmap(env, com,
2090                                                              ltd->ltd_index);
2091                                 else
2092                                         lo->ll_flags |= LF_INCOMPLETE;
2093                         } else {
2094                                 struct lfsck_namespace *ns = com->lc_file_ram;
2095
2096                                 /* If some MDT does not join the namespace
2097                                  * LFSCK, then we cannot know whether there
2098                                  * is some name entry on such MDT that with
2099                                  * the referenced MDT-object on this MDT or
2100                                  * not. So the namespace LFSCK on this MDT
2101                                  * cannot handle orphan MDT-objects properly.
2102                                  * So we mark the LFSCK as LF_INCOMPLETE and
2103                                  * skip orphan MDT-objects handling. */
2104                                 ns->ln_flags |= LF_INCOMPLETE;
2105                         }
2106                         break;
2107                 }
2108
2109                 spin_lock(&ltds->ltd_lock);
2110                 if (ltd->ltd_dead) {
2111                         spin_unlock(&ltds->ltd_lock);
2112                         break;
2113                 }
2114
2115                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2116                         struct list_head *list;
2117                         struct list_head *phase_list;
2118
2119                         if (ltd->ltd_layout_done) {
2120                                 spin_unlock(&ltds->ltd_lock);
2121                                 break;
2122                         }
2123
2124                         if (lr->lr_flags & LEF_TO_OST) {
2125                                 list = &lad->lad_ost_list;
2126                                 phase_list = &lad->lad_ost_phase1_list;
2127                         } else {
2128                                 list = &lad->lad_mdt_list;
2129                                 phase_list = &lad->lad_mdt_phase1_list;
2130                         }
2131
2132                         if (list_empty(&ltd->ltd_layout_list))
2133                                 list_add_tail(&ltd->ltd_layout_list, list);
2134                         if (list_empty(&ltd->ltd_layout_phase_list))
2135                                 list_add_tail(&ltd->ltd_layout_phase_list,
2136                                               phase_list);
2137                 } else {
2138                         if (ltd->ltd_namespace_done) {
2139                                 spin_unlock(&ltds->ltd_lock);
2140                                 break;
2141                         }
2142
2143                         if (list_empty(&ltd->ltd_namespace_list))
2144                                 list_add_tail(&ltd->ltd_namespace_list,
2145                                               &lad->lad_mdt_list);
2146                         if (list_empty(&ltd->ltd_namespace_phase_list))
2147                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2148                                               &lad->lad_mdt_phase1_list);
2149                 }
2150                 spin_unlock(&ltds->ltd_lock);
2151                 break;
2152         case LE_STOP:
2153         case LE_PHASE1_DONE:
2154         case LE_PHASE2_DONE:
2155         case LE_PEER_EXIT:
2156                 if (rc != 0 && rc != -EALREADY)
2157                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2158                               "event = %d, rc = %d\n",
2159                               lfsck_lfsck2name(com->lc_lfsck),
2160                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2161                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2162                 break;
2163         case LE_QUERY: {
2164                 struct lfsck_reply *reply;
2165                 struct list_head *list;
2166                 struct list_head *phase_list;
2167
2168                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2169                         list = &ltd->ltd_layout_list;
2170                         phase_list = &ltd->ltd_layout_phase_list;
2171                 } else {
2172                         list = &ltd->ltd_namespace_list;
2173                         phase_list = &ltd->ltd_namespace_phase_list;
2174                 }
2175
2176                 if (rc != 0) {
2177                         if (lr->lr_flags & LEF_QUERY_ALL) {
2178                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2179                                 break;
2180                         }
2181
2182                         spin_lock(&ltds->ltd_lock);
2183                         list_del_init(phase_list);
2184                         list_del_init(list);
2185                         spin_unlock(&ltds->ltd_lock);
2186                         break;
2187                 }
2188
2189                 reply = req_capsule_server_get(&req->rq_pill,
2190                                                &RMF_LFSCK_REPLY);
2191                 if (reply == NULL) {
2192                         rc = -EPROTO;
2193                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2194                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2195                                lad->lad_name, rc);
2196
2197                         if (lr->lr_flags & LEF_QUERY_ALL) {
2198                                 lfsck_reset_ltd_status(ltd, com->lc_type);
2199                                 break;
2200                         }
2201
2202                         spin_lock(&ltds->ltd_lock);
2203                         list_del_init(phase_list);
2204                         list_del_init(list);
2205                         spin_unlock(&ltds->ltd_lock);
2206                         break;
2207                 }
2208
2209                 if (lr->lr_flags & LEF_QUERY_ALL) {
2210                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2211                                 ltd->ltd_layout_status = reply->lr_status;
2212                                 ltd->ltd_layout_repaired = reply->lr_repaired;
2213                         } else {
2214                                 ltd->ltd_namespace_status = reply->lr_status;
2215                                 ltd->ltd_namespace_repaired =
2216                                                         reply->lr_repaired;
2217                         }
2218                         break;
2219                 }
2220
2221                 switch (reply->lr_status) {
2222                 case LS_SCANNING_PHASE1:
2223                         break;
2224                 case LS_SCANNING_PHASE2:
2225                         spin_lock(&ltds->ltd_lock);
2226                         list_del_init(phase_list);
2227                         if (ltd->ltd_dead) {
2228                                 spin_unlock(&ltds->ltd_lock);
2229                                 break;
2230                         }
2231
2232                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2233                                 if (ltd->ltd_layout_done) {
2234                                         spin_unlock(&ltds->ltd_lock);
2235                                         break;
2236                                 }
2237
2238                                 if (lr->lr_flags & LEF_TO_OST)
2239                                         list_add_tail(phase_list,
2240                                                 &lad->lad_ost_phase2_list);
2241                                 else
2242                                         list_add_tail(phase_list,
2243                                                 &lad->lad_mdt_phase2_list);
2244                         } else {
2245                                 if (ltd->ltd_namespace_done) {
2246                                         spin_unlock(&ltds->ltd_lock);
2247                                         break;
2248                                 }
2249
2250                                 list_add_tail(phase_list,
2251                                               &lad->lad_mdt_phase2_list);
2252                         }
2253                         spin_unlock(&ltds->ltd_lock);
2254                         break;
2255                 default:
2256                         spin_lock(&ltds->ltd_lock);
2257                         list_del_init(phase_list);
2258                         list_del_init(list);
2259                         spin_unlock(&ltds->ltd_lock);
2260                         break;
2261                 }
2262                 break;
2263         }
2264         default:
2265                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2266                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2267                 break;
2268         }
2269
2270         if (!laia->laia_shared) {
2271                 lfsck_tgt_put(ltd);
2272                 lfsck_component_put(env, com);
2273         }
2274
2275         return 0;
2276 }
2277
2278 static void lfsck_interpret(const struct lu_env *env,
2279                             struct lfsck_instance *lfsck,
2280                             struct ptlrpc_request *req, void *args, int result)
2281 {
2282         struct lfsck_async_interpret_args *laia = args;
2283         struct lfsck_component            *com;
2284
2285         LASSERT(laia->laia_com == NULL);
2286         LASSERT(laia->laia_shared);
2287
2288         spin_lock(&lfsck->li_lock);
2289         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2290                 laia->laia_com = com;
2291                 lfsck_async_interpret_common(env, req, laia, result);
2292         }
2293
2294         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2295                 laia->laia_com = com;
2296                 lfsck_async_interpret_common(env, req, laia, result);
2297         }
2298         spin_unlock(&lfsck->li_lock);
2299 }
2300
2301 static int lfsck_stop_notify(const struct lu_env *env,
2302                              struct lfsck_instance *lfsck,
2303                              struct lfsck_tgt_descs *ltds,
2304                              struct lfsck_tgt_desc *ltd, __u16 type)
2305 {
2306         struct lfsck_component *com;
2307         int                     rc = 0;
2308         ENTRY;
2309
2310         LASSERT(lfsck->li_master);
2311
2312         spin_lock(&lfsck->li_lock);
2313         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2314         if (com == NULL)
2315                 com = __lfsck_component_find(lfsck, type,
2316                                              &lfsck->li_list_double_scan);
2317         if (com != NULL)
2318                 lfsck_component_get(com);
2319         spin_unlock(&lfsck->li_lock);
2320
2321         if (com != NULL) {
2322                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2323                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2324                 struct lfsck_request              *lr    = &info->lti_lr;
2325                 struct lfsck_assistant_data       *lad   = com->lc_data;
2326                 struct list_head                  *list;
2327                 struct list_head                  *phase_list;
2328                 struct ptlrpc_request_set         *set;
2329
2330                 set = ptlrpc_prep_set();
2331                 if (set == NULL) {
2332                         lfsck_component_put(env, com);
2333
2334                         RETURN(-ENOMEM);
2335                 }
2336
2337                 if (type == LFSCK_TYPE_LAYOUT) {
2338                         list = &ltd->ltd_layout_list;
2339                         phase_list = &ltd->ltd_layout_phase_list;
2340                 } else {
2341                         list = &ltd->ltd_namespace_list;
2342                         phase_list = &ltd->ltd_namespace_phase_list;
2343                 }
2344
2345                 spin_lock(&ltds->ltd_lock);
2346                 if (list_empty(list)) {
2347                         LASSERT(list_empty(phase_list));
2348                         spin_unlock(&ltds->ltd_lock);
2349                         ptlrpc_set_destroy(set);
2350
2351                         RETURN(0);
2352                 }
2353
2354                 list_del_init(phase_list);
2355                 list_del_init(list);
2356                 spin_unlock(&ltds->ltd_lock);
2357
2358                 memset(lr, 0, sizeof(*lr));
2359                 lr->lr_index = lfsck_dev_idx(lfsck);
2360                 lr->lr_event = LE_PEER_EXIT;
2361                 lr->lr_active = type;
2362                 lr->lr_status = LS_CO_PAUSED;
2363                 if (ltds == &lfsck->li_ost_descs)
2364                         lr->lr_flags = LEF_TO_OST;
2365
2366                 memset(laia, 0, sizeof(*laia));
2367                 laia->laia_com = com;
2368                 laia->laia_ltds = ltds;
2369                 atomic_inc(&ltd->ltd_ref);
2370                 laia->laia_ltd = ltd;
2371                 laia->laia_lr = lr;
2372
2373                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2374                                          lfsck_async_interpret_common,
2375                                          laia, LFSCK_NOTIFY);
2376                 if (rc != 0) {
2377                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2378                                "co-stop for %s: rc = %d\n",
2379                                lfsck_lfsck2name(lfsck),
2380                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2381                                ltd->ltd_index, lad->lad_name, rc);
2382                         lfsck_tgt_put(ltd);
2383                 } else {
2384                         rc = ptlrpc_set_wait(env, set);
2385                 }
2386
2387                 ptlrpc_set_destroy(set);
2388                 lfsck_component_put(env, com);
2389         }
2390
2391         RETURN(rc);
2392 }
2393
2394 static int lfsck_async_interpret(const struct lu_env *env,
2395                                  struct ptlrpc_request *req,
2396                                  void *args, int rc)
2397 {
2398         struct lfsck_async_interpret_args *laia = args;
2399         struct lfsck_instance             *lfsck;
2400
2401         lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2402                               li_mdt_descs);
2403         lfsck_interpret(env, lfsck, req, laia, rc);
2404         lfsck_tgt_put(laia->laia_ltd);
2405         if (rc != 0 && laia->laia_result != -EALREADY)
2406                 laia->laia_result = rc;
2407
2408         return 0;
2409 }
2410
2411 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2412                         struct lfsck_request *lr,
2413                         struct ptlrpc_request_set *set,
2414                         ptlrpc_interpterer_t interpreter,
2415                         void *args, int request)
2416 {
2417         struct lfsck_async_interpret_args *laia;
2418         struct ptlrpc_request             *req;
2419         struct lfsck_request              *tmp;
2420         struct req_format                 *format;
2421         int                                rc;
2422
2423         switch (request) {
2424         case LFSCK_NOTIFY:
2425                 format = &RQF_LFSCK_NOTIFY;
2426                 break;
2427         case LFSCK_QUERY:
2428                 format = &RQF_LFSCK_QUERY;
2429                 break;
2430         default:
2431                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2432                        exp->exp_obd->obd_name, request, -EINVAL);
2433                 return -EINVAL;
2434         }
2435
2436         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2437         if (req == NULL)
2438                 return -ENOMEM;
2439
2440         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2441         if (rc != 0) {
2442                 ptlrpc_request_free(req);
2443
2444                 return rc;
2445         }
2446
2447         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2448         *tmp = *lr;
2449         ptlrpc_request_set_replen(req);
2450
2451         laia = ptlrpc_req_async_args(req);
2452         *laia = *(struct lfsck_async_interpret_args *)args;
2453         if (laia->laia_com != NULL)
2454                 lfsck_component_get(laia->laia_com);
2455         req->rq_interpret_reply = interpreter;
2456         req->rq_allow_intr = 1;
2457         req->rq_no_delay = 1;
2458         ptlrpc_set_add_req(set, req);
2459
2460         return 0;
2461 }
2462
2463 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2464 {
2465         struct lfsck_thread_info          *info  = lfsck_env_info(env);
2466         struct lfsck_request              *lr    = &info->lti_lr;
2467         struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2468         struct lfsck_instance             *lfsck = com->lc_lfsck;
2469         struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
2470         struct lfsck_tgt_desc             *ltd;
2471         struct ptlrpc_request_set         *set;
2472         int                                idx;
2473         int                                rc;
2474         ENTRY;
2475
2476         memset(lr, 0, sizeof(*lr));
2477         lr->lr_event = LE_QUERY;
2478         lr->lr_active = com->lc_type;
2479         lr->lr_flags = LEF_QUERY_ALL;
2480
2481         memset(laia, 0, sizeof(*laia));
2482         laia->laia_com = com;
2483         laia->laia_lr = lr;
2484
2485         set = ptlrpc_prep_set();
2486         if (set == NULL)
2487                 RETURN(-ENOMEM);
2488
2489 again:
2490         laia->laia_ltds = ltds;
2491         down_read(&ltds->ltd_rw_sem);
2492         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2493                 ltd = lfsck_tgt_get(ltds, idx);
2494                 LASSERT(ltd != NULL);
2495
2496                 laia->laia_ltd = ltd;
2497                 up_read(&ltds->ltd_rw_sem);
2498                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2499                                          lfsck_async_interpret_common,
2500                                          laia, LFSCK_QUERY);
2501                 if (rc != 0) {
2502                         struct lfsck_assistant_data *lad = com->lc_data;
2503
2504                         CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2505                                "rc = %d\n", lfsck_lfsck2name(lfsck),
2506                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2507                                ltd->ltd_index, lad->lad_name, rc);
2508                         lfsck_reset_ltd_status(ltd, com->lc_type);
2509                         lfsck_tgt_put(ltd);
2510                 }
2511                 down_read(&ltds->ltd_rw_sem);
2512         }
2513         up_read(&ltds->ltd_rw_sem);
2514
2515         if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2516                 ltds = &lfsck->li_ost_descs;
2517                 lr->lr_flags |= LEF_TO_OST;
2518                 goto again;
2519         }
2520
2521         rc = ptlrpc_set_wait(env, set);
2522         ptlrpc_set_destroy(set);
2523
2524         RETURN(rc);
2525 }
2526
2527 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2528                           struct lfsck_start_param *lsp)
2529 {
2530         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2531         struct lfsck_assistant_data     *lad     = com->lc_data;
2532         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2533         struct ptlrpc_thread            *athread = &lad->lad_thread;
2534         struct lfsck_thread_args        *lta;
2535         struct task_struct              *task;
2536         int                              rc;
2537         ENTRY;
2538
2539         lad->lad_assistant_status = 0;
2540         lad->lad_post_result = 0;
2541         lad->lad_flags = 0;
2542         lad->lad_advance_lock = false;
2543         thread_set_flags(athread, 0);
2544
2545         lta = lfsck_thread_args_init(lfsck, com, lsp);
2546         if (IS_ERR(lta))
2547                 RETURN(PTR_ERR(lta));
2548
2549         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2550         if (IS_ERR(task)) {
2551                 rc = PTR_ERR(task);
2552                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2553                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2554                 lfsck_thread_args_fini(lta);
2555         } else {
2556                 struct l_wait_info lwi = { 0 };
2557
2558                 l_wait_event(mthread->t_ctl_waitq,
2559                              thread_is_running(athread) ||
2560                              thread_is_stopped(athread) ||
2561                              !thread_is_starting(mthread),
2562                              &lwi);
2563                 if (unlikely(!thread_is_starting(mthread)))
2564                         /* stopped by race */
2565                         rc = -ESRCH;
2566                 else if (unlikely(!thread_is_running(athread)))
2567                         rc = lad->lad_assistant_status;
2568                 else
2569                         rc = 0;
2570         }
2571
2572         RETURN(rc);
2573 }
2574
2575 int lfsck_checkpoint_generic(const struct lu_env *env,
2576                              struct lfsck_component *com)
2577 {
2578         struct lfsck_assistant_data     *lad     = com->lc_data;
2579         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2580         struct ptlrpc_thread            *athread = &lad->lad_thread;
2581         struct l_wait_info               lwi     = { 0 };
2582
2583         l_wait_event(mthread->t_ctl_waitq,
2584                      list_empty(&lad->lad_req_list) ||
2585                      !thread_is_running(mthread) ||
2586                      thread_is_stopped(athread),
2587                      &lwi);
2588
2589         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2590                 return LFSCK_CHECKPOINT_SKIP;
2591
2592         return 0;
2593 }
2594
2595 void lfsck_post_generic(const struct lu_env *env,
2596                         struct lfsck_component *com, int *result)
2597 {
2598         struct lfsck_assistant_data     *lad     = com->lc_data;
2599         struct ptlrpc_thread            *athread = &lad->lad_thread;
2600         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2601         struct l_wait_info               lwi     = { 0 };
2602
2603         lad->lad_post_result = *result;
2604         if (*result <= 0)
2605                 set_bit(LAD_EXIT, &lad->lad_flags);
2606         set_bit(LAD_TO_POST, &lad->lad_flags);
2607
2608         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2609                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2610
2611         wake_up_all(&athread->t_ctl_waitq);
2612         l_wait_event(mthread->t_ctl_waitq,
2613                      (*result > 0 && list_empty(&lad->lad_req_list)) ||
2614                      thread_is_stopped(athread),
2615                      &lwi);
2616
2617         if (lad->lad_assistant_status < 0)
2618                 *result = lad->lad_assistant_status;
2619
2620         CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2621                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2622 }
2623
2624 int lfsck_double_scan_generic(const struct lu_env *env,
2625                               struct lfsck_component *com, int status)
2626 {
2627         struct lfsck_assistant_data     *lad     = com->lc_data;
2628         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2629         struct ptlrpc_thread            *athread = &lad->lad_thread;
2630         struct l_wait_info               lwi     = { 0 };
2631
2632         if (status != LS_SCANNING_PHASE2)
2633                 set_bit(LAD_EXIT, &lad->lad_flags);
2634         else
2635                 set_bit(LAD_TO_DOUBLE_SCAN, &lad->lad_flags);
2636
2637         CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2638                "status %d\n",
2639                lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2640
2641         wake_up_all(&athread->t_ctl_waitq);
2642         l_wait_event(mthread->t_ctl_waitq,
2643                      test_bit(LAD_IN_DOUBLE_SCAN, &lad->lad_flags) ||
2644                      thread_is_stopped(athread),
2645                      &lwi);
2646
2647         CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2648                "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2649                lad->lad_assistant_status);
2650
2651         if (lad->lad_assistant_status < 0)
2652                 return lad->lad_assistant_status;
2653
2654         return 0;
2655 }
2656
2657 void lfsck_quit_generic(const struct lu_env *env,
2658                         struct lfsck_component *com)
2659 {
2660         struct lfsck_assistant_data     *lad     = com->lc_data;
2661         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2662         struct ptlrpc_thread            *athread = &lad->lad_thread;
2663         struct l_wait_info               lwi     = { 0 };
2664
2665         set_bit(LAD_EXIT, &lad->lad_flags);
2666         wake_up_all(&athread->t_ctl_waitq);
2667         l_wait_event(mthread->t_ctl_waitq,
2668                      thread_is_init(athread) ||
2669                      thread_is_stopped(athread),
2670                      &lwi);
2671 }
2672
2673 int lfsck_load_one_trace_file(const struct lu_env *env,
2674                               struct lfsck_component *com,
2675                               struct dt_object *parent,
2676                               struct dt_object **child,
2677                               const struct dt_index_features *ft,
2678                               const char *name, bool reset)
2679 {
2680         struct lfsck_instance *lfsck = com->lc_lfsck;
2681         struct dt_object *obj;
2682         int rc;
2683         ENTRY;
2684
2685         if (*child != NULL) {
2686                 struct dt_it *it;
2687                 const struct dt_it_ops *iops;
2688                 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2689
2690                 if (!reset)
2691                         RETURN(0);
2692
2693                 obj = *child;
2694                 rc = obj->do_ops->do_index_try(env, obj, ft);
2695                 if (rc)
2696                         /* unlink by force */
2697                         goto unlink;
2698
2699                 iops = &obj->do_index_ops->dio_it;
2700                 it = iops->init(env, obj, 0);
2701                 if (IS_ERR(it))
2702                         /* unlink by force */
2703                         goto unlink;
2704
2705                 fid_zero(fid);
2706                 rc = iops->get(env, it, (const struct dt_key *)fid);
2707                 if (rc >= 0) {
2708                         rc = iops->next(env, it);
2709                         iops->put(env, it);
2710                 }
2711                 iops->fini(env, it);
2712                 if (rc > 0)
2713                         /* "rc > 0" means the index file is empty. */
2714                         RETURN(0);
2715
2716 unlink:
2717                 /* The old index is not empty, remove it firstly. */
2718                 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2719                 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2720                              "%s: unlink lfsck sub trace file %s: rc = %d\n",
2721                              lfsck_lfsck2name(com->lc_lfsck), name, rc);
2722                 if (rc)
2723                         RETURN(rc);
2724
2725                 if (*child) {
2726                         lfsck_object_put(env, *child);
2727                         *child = NULL;
2728                 }
2729         } else if (reset) {
2730                 goto unlink;
2731         }
2732
2733         obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2734                                          S_IFREG | S_IRUGO | S_IWUSR, ft);
2735         if (IS_ERR(obj))
2736                 RETURN(PTR_ERR(obj));
2737
2738         rc = obj->do_ops->do_index_try(env, obj, ft);
2739         if (rc) {
2740                 lfsck_object_put(env, obj);
2741                 CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
2742                        "sub trace file %s: rc = %d\n",
2743                        lfsck_lfsck2name(com->lc_lfsck), name, rc);
2744         } else {
2745                 *child = obj;
2746         }
2747
2748         RETURN(rc);
2749 }
2750
2751 int lfsck_load_sub_trace_files(const struct lu_env *env,
2752                                struct lfsck_component *com,
2753                                const struct dt_index_features *ft,
2754                                const char *prefix, bool reset)
2755 {
2756         char *name = lfsck_env_info(env)->lti_key;
2757         struct lfsck_sub_trace_obj *lsto;
2758         int rc;
2759         int i;
2760
2761         for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2762              i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2763                 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2764                 rc = lfsck_load_one_trace_file(env, com,
2765                                 com->lc_lfsck->li_lfsck_dir,
2766                                 &lsto->lsto_obj, ft, name, reset);
2767         }
2768
2769         return rc;
2770 }
2771
2772 /* external interfaces */
2773 int lfsck_get_speed(char *buf, struct dt_device *key)
2774 {
2775         struct lu_env           env;
2776         struct lfsck_instance  *lfsck;
2777         int                     rc;
2778         ENTRY;
2779
2780         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2781         if (rc != 0)
2782                 RETURN(rc);
2783
2784         lfsck = lfsck_instance_find(key, true, false);
2785         if (lfsck && buf) {
2786                 rc = sprintf(buf, "%u\n",
2787                              lfsck->li_bookmark_ram.lb_speed_limit);
2788                 lfsck_instance_put(&env, lfsck);
2789         } else {
2790                 rc = -ENXIO;
2791         }
2792
2793         lu_env_fini(&env);
2794
2795         RETURN(rc);
2796 }
2797 EXPORT_SYMBOL(lfsck_get_speed);
2798
2799 int lfsck_set_speed(struct dt_device *key, __u32 val)
2800 {
2801         struct lu_env           env;
2802         struct lfsck_instance  *lfsck;
2803         int                     rc;
2804         ENTRY;
2805
2806         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2807         if (rc != 0)
2808                 RETURN(rc);
2809
2810         lfsck = lfsck_instance_find(key, true, false);
2811         if (likely(lfsck != NULL)) {
2812                 mutex_lock(&lfsck->li_mutex);
2813                 if (__lfsck_set_speed(lfsck, val))
2814                         rc = lfsck_bookmark_store(&env, lfsck);
2815                 mutex_unlock(&lfsck->li_mutex);
2816                 lfsck_instance_put(&env, lfsck);
2817         } else {
2818                 rc = -ENXIO;
2819         }
2820
2821         lu_env_fini(&env);
2822
2823         RETURN(rc);
2824 }
2825 EXPORT_SYMBOL(lfsck_set_speed);
2826
2827 int lfsck_get_windows(char *buf, struct dt_device *key)
2828 {
2829         struct lu_env           env;
2830         struct lfsck_instance  *lfsck;
2831         int                     rc;
2832         ENTRY;
2833
2834         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2835         if (rc != 0)
2836                 RETURN(rc);
2837
2838         lfsck = lfsck_instance_find(key, true, false);
2839         if (likely(lfsck != NULL)) {
2840                 rc = sprintf(buf, "%u\n",
2841                              lfsck->li_bookmark_ram.lb_async_windows);
2842                 lfsck_instance_put(&env, lfsck);
2843         } else {
2844                 rc = -ENXIO;
2845         }
2846
2847         lu_env_fini(&env);
2848
2849         RETURN(rc);
2850 }
2851 EXPORT_SYMBOL(lfsck_get_windows);
2852
2853 int lfsck_set_windows(struct dt_device *key, unsigned int val)
2854 {
2855         struct lu_env           env;
2856         struct lfsck_instance  *lfsck;
2857         int                     rc;
2858         ENTRY;
2859
2860         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2861         if (rc != 0)
2862                 RETURN(rc);
2863
2864         lfsck = lfsck_instance_find(key, true, false);
2865         if (likely(lfsck != NULL)) {
2866                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2867                         CWARN("%s: invalid async windows size that may "
2868                               "cause memory issues. The valid range is "
2869                               "[1 - %u].\n",
2870                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2871                         rc = -EINVAL;
2872                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2873                         mutex_lock(&lfsck->li_mutex);
2874                         lfsck->li_bookmark_ram.lb_async_windows = val;
2875                         rc = lfsck_bookmark_store(&env, lfsck);
2876                         mutex_unlock(&lfsck->li_mutex);
2877                 }
2878                 lfsck_instance_put(&env, lfsck);
2879         } else {
2880                 rc = -ENXIO;
2881         }
2882
2883         lu_env_fini(&env);
2884
2885         RETURN(rc);
2886 }
2887 EXPORT_SYMBOL(lfsck_set_windows);
2888
2889 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2890 {
2891         struct lu_env           env;
2892         struct lfsck_instance  *lfsck;
2893         struct lfsck_component *com;
2894         int                     rc;
2895         ENTRY;
2896
2897         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2898         if (rc != 0)
2899                 RETURN(rc);
2900
2901         lfsck = lfsck_instance_find(key, true, false);
2902         if (likely(lfsck != NULL)) {
2903                 com = lfsck_component_find(lfsck, type);
2904                 if (likely(com != NULL)) {
2905                         com->lc_ops->lfsck_dump(&env, com, m);
2906                         lfsck_component_put(&env, com);
2907                 } else {
2908                         rc = -ENOTSUPP;
2909                 }
2910
2911                 lfsck_instance_put(&env, lfsck);
2912         } else {
2913                 rc = -ENXIO;
2914         }
2915
2916         lu_env_fini(&env);
2917
2918         RETURN(rc);
2919 }
2920 EXPORT_SYMBOL(lfsck_dump);
2921
2922 static int lfsck_stop_all(const struct lu_env *env,
2923                           struct lfsck_instance *lfsck,
2924                           struct lfsck_stop *stop)
2925 {
2926         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2927         struct lfsck_request              *lr     = &info->lti_lr;
2928         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2929         struct ptlrpc_request_set         *set;
2930         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2931         struct lfsck_tgt_desc             *ltd;
2932         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2933         __u32                              idx;
2934         int                                rc     = 0;
2935         int                                rc1    = 0;
2936         ENTRY;
2937
2938         LASSERT(stop->ls_flags & LPF_BROADCAST);
2939
2940         set = ptlrpc_prep_set();
2941         if (unlikely(set == NULL))
2942                 RETURN(-ENOMEM);
2943
2944         memset(lr, 0, sizeof(*lr));
2945         lr->lr_event = LE_STOP;
2946         lr->lr_index = lfsck_dev_idx(lfsck);
2947         lr->lr_status = stop->ls_status;
2948         lr->lr_version = bk->lb_version;
2949         lr->lr_active = LFSCK_TYPES_ALL;
2950         lr->lr_param = stop->ls_flags;
2951
2952         memset(laia, 0, sizeof(*laia));
2953         laia->laia_ltds = ltds;
2954         laia->laia_lr = lr;
2955         laia->laia_shared = 1;
2956
2957         down_read(&ltds->ltd_rw_sem);
2958         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2959                 ltd = lfsck_tgt_get(ltds, idx);
2960                 LASSERT(ltd != NULL);
2961
2962                 laia->laia_ltd = ltd;
2963                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2964                                          lfsck_async_interpret, laia,
2965                                          LFSCK_NOTIFY);
2966                 if (rc != 0) {
2967                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2968                         lfsck_tgt_put(ltd);
2969                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2970                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2971                         rc1 = rc;
2972                 }
2973         }
2974         up_read(&ltds->ltd_rw_sem);
2975
2976         rc = ptlrpc_set_wait(env, set);
2977         ptlrpc_set_destroy(set);
2978
2979         if (rc == 0)
2980                 rc = laia->laia_result;
2981
2982         if (rc == -EALREADY)
2983                 rc = 0;
2984
2985         if (rc != 0)
2986                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2987                        lfsck_lfsck2name(lfsck), rc);
2988
2989         RETURN(rc != 0 ? rc : rc1);
2990 }
2991
2992 static int lfsck_start_all(const struct lu_env *env,
2993                            struct lfsck_instance *lfsck,
2994                            struct lfsck_start *start)
2995 {
2996         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2997         struct lfsck_request              *lr     = &info->lti_lr;
2998         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2999         struct ptlrpc_request_set         *set;
3000         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
3001         struct lfsck_tgt_desc             *ltd;
3002         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
3003         __u32                              idx;
3004         int                                rc     = 0;
3005         bool retry = false;
3006         ENTRY;
3007
3008         LASSERT(start->ls_flags & LPF_BROADCAST);
3009
3010         memset(lr, 0, sizeof(*lr));
3011         lr->lr_event = LE_START;
3012         lr->lr_index = lfsck_dev_idx(lfsck);
3013         lr->lr_speed = bk->lb_speed_limit;
3014         lr->lr_version = bk->lb_version;
3015         lr->lr_active = start->ls_active;
3016         lr->lr_param = start->ls_flags;
3017         lr->lr_async_windows = bk->lb_async_windows;
3018         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
3019                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
3020                        LSV_CREATE_MDTOBJ;
3021
3022         memset(laia, 0, sizeof(*laia));
3023         laia->laia_ltds = ltds;
3024         laia->laia_lr = lr;
3025         laia->laia_shared = 1;
3026
3027 again:
3028         set = ptlrpc_prep_set();
3029         if (unlikely(!set))
3030                 RETURN(-ENOMEM);
3031
3032         down_read(&ltds->ltd_rw_sem);
3033         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3034                 ltd = lfsck_tgt_get(ltds, idx);
3035                 LASSERT(ltd != NULL);
3036
3037                 if (retry && !ltd->ltd_retry_start) {
3038                         lfsck_tgt_put(ltd);
3039                         continue;
3040                 }
3041
3042                 laia->laia_ltd = ltd;
3043                 ltd->ltd_retry_start = 0;
3044                 ltd->ltd_layout_done = 0;
3045                 ltd->ltd_namespace_done = 0;
3046                 ltd->ltd_synced_failures = 0;
3047                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3048                                          lfsck_async_interpret, laia,
3049                                          LFSCK_NOTIFY);
3050                 if (rc != 0) {
3051                         lfsck_interpret(env, lfsck, NULL, laia, rc);
3052                         lfsck_tgt_put(ltd);
3053                         CERROR("%s: cannot notify MDT %x for LFSCK "
3054                                "start, failout: rc = %d\n",
3055                                lfsck_lfsck2name(lfsck), idx, rc);
3056                         break;
3057                 }
3058         }
3059         up_read(&ltds->ltd_rw_sem);
3060
3061         if (rc != 0) {
3062                 ptlrpc_set_destroy(set);
3063
3064                 RETURN(rc);
3065         }
3066
3067         rc = ptlrpc_set_wait(env, set);
3068         ptlrpc_set_destroy(set);
3069
3070         if (rc == 0)
3071                 rc = laia->laia_result;
3072
3073         if (unlikely(rc == -EINPROGRESS)) {
3074                 retry = true;
3075                 set_current_state(TASK_INTERRUPTIBLE);
3076                 schedule_timeout(cfs_time_seconds(1));
3077                 set_current_state(TASK_RUNNING);
3078                 if (!signal_pending(current) &&
3079                     thread_is_running(&lfsck->li_thread))
3080                         goto again;
3081
3082                 rc = -EINTR;
3083         }
3084
3085         if (rc != 0) {
3086                 struct lfsck_stop *stop = &info->lti_stop;
3087
3088                 CERROR("%s: cannot start LFSCK on some MDTs, "
3089                        "stop all: rc = %d\n",
3090                        lfsck_lfsck2name(lfsck), rc);
3091                 if (rc != -EALREADY) {
3092                         stop->ls_status = LS_FAILED;
3093                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3094                         lfsck_stop_all(env, lfsck, stop);
3095                 }
3096         }
3097
3098         RETURN(rc);
3099 }
3100
3101 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3102                 struct lfsck_start_param *lsp)
3103 {
3104         struct lfsck_start              *start  = lsp->lsp_start;
3105         struct lfsck_instance           *lfsck;
3106         struct lfsck_bookmark           *bk;
3107         struct ptlrpc_thread            *thread;
3108         struct lfsck_component          *com;
3109         struct l_wait_info               lwi    = { 0 };
3110         struct lfsck_thread_args        *lta;
3111         struct task_struct              *task;
3112         struct lfsck_tgt_descs          *ltds;
3113         struct lfsck_tgt_desc           *ltd;
3114         __u32                            idx;
3115         int                              rc     = 0;
3116         __u16                            valid  = 0;
3117         __u16                            flags  = 0;
3118         __u16                            type   = 1;
3119         ENTRY;
3120
3121         if (key->dd_rdonly)
3122                 RETURN(-EROFS);
3123
3124         lfsck = lfsck_instance_find(key, true, false);
3125         if (unlikely(lfsck == NULL))
3126                 RETURN(-ENXIO);
3127
3128         if (unlikely(lfsck->li_stopping))
3129                 GOTO(put, rc = -ENXIO);
3130
3131         /* System is not ready, try again later. */
3132         if (unlikely(lfsck->li_namespace == NULL ||
3133                      lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3134                 GOTO(put, rc = -EINPROGRESS);
3135
3136         /* start == NULL means auto trigger paused LFSCK. */
3137         if (!start) {
3138                 if (list_empty(&lfsck->li_list_scan) ||
3139                     OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3140                         GOTO(put, rc = 0);
3141         } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3142                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3143                        lfsck_lfsck2name(lfsck));
3144
3145                 GOTO(put, rc = -EPERM);
3146         }
3147
3148         bk = &lfsck->li_bookmark_ram;
3149         thread = &lfsck->li_thread;
3150         mutex_lock(&lfsck->li_mutex);
3151         spin_lock(&lfsck->li_lock);
3152         if (unlikely(thread_is_stopping(thread))) {
3153                 /* Someone is stopping the LFSCK. */
3154                 spin_unlock(&lfsck->li_lock);
3155                 GOTO(out, rc = -EBUSY);
3156         }
3157
3158         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3159                 rc = -EALREADY;
3160                 if (unlikely(start == NULL)) {
3161                         spin_unlock(&lfsck->li_lock);
3162                         GOTO(out, rc);
3163                 }
3164
3165                 while (start->ls_active != 0) {
3166                         if (!(type & start->ls_active)) {
3167                                 type <<= 1;
3168                                 continue;
3169                         }
3170
3171                         com = __lfsck_component_find(lfsck, type,
3172                                                      &lfsck->li_list_scan);
3173                         if (com == NULL)
3174                                 com = __lfsck_component_find(lfsck, type,
3175                                                 &lfsck->li_list_double_scan);
3176                         if (com == NULL) {
3177                                 rc = -EOPNOTSUPP;
3178                                 break;
3179                         }
3180
3181                         if (com->lc_ops->lfsck_join != NULL) {
3182                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
3183                                 if (rc != 0 && rc != -EALREADY)
3184                                         break;
3185                         }
3186                         start->ls_active &= ~type;
3187                         type <<= 1;
3188                 }
3189                 spin_unlock(&lfsck->li_lock);
3190                 GOTO(out, rc);
3191         }
3192         spin_unlock(&lfsck->li_lock);
3193
3194         lfsck->li_status = 0;
3195         lfsck->li_oit_over = 0;
3196         lfsck->li_start_unplug = 0;
3197         lfsck->li_drop_dryrun = 0;
3198         lfsck->li_new_scanned = 0;
3199
3200         /* For auto trigger. */
3201         if (start == NULL)
3202                 goto trigger;
3203
3204         start->ls_version = bk->lb_version;
3205
3206         if (start->ls_active != 0) {
3207                 struct lfsck_component *next;
3208
3209                 if (start->ls_active == LFSCK_TYPES_ALL)
<