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