Whamcloud - gitweb
4951396d217066dfd30c7ed284d866e14c90253b
[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 <libcfs/list.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fld.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre_lfsck.h>
41 #include <lustre/lustre_lfsck_user.h>
42
43 #include "lfsck_internal.h"
44
45 #define LFSCK_CHECKPOINT_SKIP   1
46
47 /* define lfsck thread key */
48 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
49
50 static void lfsck_key_fini(const struct lu_context *ctx,
51                            struct lu_context_key *key, void *data)
52 {
53         struct lfsck_thread_info *info = data;
54
55         lu_buf_free(&info->lti_linkea_buf);
56         lu_buf_free(&info->lti_linkea_buf2);
57         lu_buf_free(&info->lti_big_buf);
58         OBD_FREE_PTR(info);
59 }
60
61 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
62 LU_KEY_INIT_GENERIC(lfsck);
63
64 static struct list_head lfsck_instance_list;
65 static struct list_head lfsck_ost_orphan_list;
66 static struct list_head lfsck_mdt_orphan_list;
67 static DEFINE_SPINLOCK(lfsck_instance_lock);
68
69 static const char *lfsck_status_names[] = {
70         [LS_INIT]               = "init",
71         [LS_SCANNING_PHASE1]    = "scanning-phase1",
72         [LS_SCANNING_PHASE2]    = "scanning-phase2",
73         [LS_COMPLETED]          = "completed",
74         [LS_FAILED]             = "failed",
75         [LS_STOPPED]            = "stopped",
76         [LS_PAUSED]             = "paused",
77         [LS_CRASHED]            = "crashed",
78         [LS_PARTIAL]            = "partial",
79         [LS_CO_FAILED]          = "co-failed",
80         [LS_CO_STOPPED]         = "co-stopped",
81         [LS_CO_PAUSED]          = "co-paused"
82 };
83
84 const char *lfsck_flags_names[] = {
85         "scanned-once",
86         "inconsistent",
87         "upgrade",
88         "incomplete",
89         "crashed_lastid",
90         NULL
91 };
92
93 const char *lfsck_param_names[] = {
94         NULL,
95         "failout",
96         "dryrun",
97         "all_targets",
98         "broadcast",
99         "orphan",
100         "create_ostobj",
101         "create_mdtobj",
102         NULL
103 };
104
105 enum lfsck_verify_lpf_types {
106         LVLT_BY_BOOKMARK        = 0,
107         LVLT_BY_NAMEENTRY       = 1,
108 };
109
110 const char *lfsck_status2names(enum lfsck_status status)
111 {
112         if (unlikely(status < 0 || status >= LS_MAX))
113                 return "unknown";
114
115         return lfsck_status_names[status];
116 }
117
118 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
119 {
120         spin_lock_init(&ltds->ltd_lock);
121         init_rwsem(&ltds->ltd_rw_sem);
122         INIT_LIST_HEAD(&ltds->ltd_orphan);
123         ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
124         if (ltds->ltd_tgts_bitmap == NULL)
125                 return -ENOMEM;
126
127         return 0;
128 }
129
130 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
131 {
132         struct lfsck_tgt_desc   *ltd;
133         struct lfsck_tgt_desc   *next;
134         int                      idx;
135
136         down_write(&ltds->ltd_rw_sem);
137
138         list_for_each_entry_safe(ltd, next, &ltds->ltd_orphan,
139                                  ltd_orphan_list) {
140                 list_del_init(&ltd->ltd_orphan_list);
141                 lfsck_tgt_put(ltd);
142         }
143
144         if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
145                 up_write(&ltds->ltd_rw_sem);
146
147                 return;
148         }
149
150         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
151                 ltd = LTD_TGT(ltds, idx);
152                 if (likely(ltd != NULL)) {
153                         LASSERT(list_empty(&ltd->ltd_layout_list));
154                         LASSERT(list_empty(&ltd->ltd_layout_phase_list));
155                         LASSERT(list_empty(&ltd->ltd_namespace_list));
156                         LASSERT(list_empty(&ltd->ltd_namespace_phase_list));
157
158                         ltds->ltd_tgtnr--;
159                         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
160                         LTD_TGT(ltds, idx) = NULL;
161                         lfsck_tgt_put(ltd);
162                 }
163         }
164
165         LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
166                  ltds->ltd_tgtnr);
167
168         for (idx = 0; idx < TGT_PTRS; idx++) {
169                 if (ltds->ltd_tgts_idx[idx] != NULL) {
170                         OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
171                         ltds->ltd_tgts_idx[idx] = NULL;
172                 }
173         }
174
175         CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
176         ltds->ltd_tgts_bitmap = NULL;
177         up_write(&ltds->ltd_rw_sem);
178 }
179
180 static int __lfsck_add_target(const struct lu_env *env,
181                               struct lfsck_instance *lfsck,
182                               struct lfsck_tgt_desc *ltd,
183                               bool for_ost, bool locked)
184 {
185         struct lfsck_tgt_descs *ltds;
186         __u32                   index = ltd->ltd_index;
187         int                     rc    = 0;
188         ENTRY;
189
190         if (for_ost)
191                 ltds = &lfsck->li_ost_descs;
192         else
193                 ltds = &lfsck->li_mdt_descs;
194
195         if (!locked)
196                 down_write(&ltds->ltd_rw_sem);
197
198         LASSERT(ltds->ltd_tgts_bitmap != NULL);
199
200         if (index >= ltds->ltd_tgts_bitmap->size) {
201                 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
202                                     (__u32)BITS_PER_LONG);
203                 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
204                 cfs_bitmap_t *new_bitmap;
205
206                 while (newsize < index + 1)
207                         newsize <<= 1;
208
209                 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
210                 if (new_bitmap == NULL)
211                         GOTO(unlock, rc = -ENOMEM);
212
213                 if (ltds->ltd_tgtnr > 0)
214                         cfs_bitmap_copy(new_bitmap, old_bitmap);
215                 ltds->ltd_tgts_bitmap = new_bitmap;
216                 CFS_FREE_BITMAP(old_bitmap);
217         }
218
219         if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
220                 CERROR("%s: the device %s (%u) is registered already\n",
221                        lfsck_lfsck2name(lfsck),
222                        ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
223                 GOTO(unlock, rc = -EEXIST);
224         }
225
226         if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
227                 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
228                 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
229                         GOTO(unlock, rc = -ENOMEM);
230         }
231
232         LTD_TGT(ltds, index) = ltd;
233         cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
234         ltds->ltd_tgtnr++;
235
236         GOTO(unlock, rc = 0);
237
238 unlock:
239         if (!locked)
240                 up_write(&ltds->ltd_rw_sem);
241
242         return rc;
243 }
244
245 static int lfsck_add_target_from_orphan(const struct lu_env *env,
246                                         struct lfsck_instance *lfsck)
247 {
248         struct lfsck_tgt_descs  *ltds    = &lfsck->li_ost_descs;
249         struct lfsck_tgt_desc   *ltd;
250         struct lfsck_tgt_desc   *next;
251         struct list_head        *head    = &lfsck_ost_orphan_list;
252         int                      rc;
253         bool                     for_ost = true;
254
255 again:
256         spin_lock(&lfsck_instance_lock);
257         list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
258                 if (ltd->ltd_key == lfsck->li_bottom)
259                         list_move_tail(&ltd->ltd_orphan_list,
260                                        &ltds->ltd_orphan);
261         }
262         spin_unlock(&lfsck_instance_lock);
263
264         down_write(&ltds->ltd_rw_sem);
265         while (!list_empty(&ltds->ltd_orphan)) {
266                 ltd = list_entry(ltds->ltd_orphan.next,
267                                  struct lfsck_tgt_desc,
268                                  ltd_orphan_list);
269                 list_del_init(&ltd->ltd_orphan_list);
270                 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
271                 /* Do not hold the semaphore for too long time. */
272                 up_write(&ltds->ltd_rw_sem);
273                 if (rc != 0)
274                         return rc;
275
276                 down_write(&ltds->ltd_rw_sem);
277         }
278         up_write(&ltds->ltd_rw_sem);
279
280         if (for_ost) {
281                 ltds = &lfsck->li_mdt_descs;
282                 head = &lfsck_mdt_orphan_list;
283                 for_ost = false;
284                 goto again;
285         }
286
287         return 0;
288 }
289
290 static inline struct lfsck_component *
291 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
292                        struct list_head *list)
293 {
294         struct lfsck_component *com;
295
296         list_for_each_entry(com, list, lc_link) {
297                 if (com->lc_type == type)
298                         return com;
299         }
300         return NULL;
301 }
302
303 struct lfsck_component *
304 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
305 {
306         struct lfsck_component *com;
307
308         spin_lock(&lfsck->li_lock);
309         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
310         if (com != NULL)
311                 goto unlock;
312
313         com = __lfsck_component_find(lfsck, type,
314                                      &lfsck->li_list_double_scan);
315         if (com != NULL)
316                 goto unlock;
317
318         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
319
320 unlock:
321         if (com != NULL)
322                 lfsck_component_get(com);
323         spin_unlock(&lfsck->li_lock);
324         return com;
325 }
326
327 void lfsck_component_cleanup(const struct lu_env *env,
328                              struct lfsck_component *com)
329 {
330         if (!list_empty(&com->lc_link))
331                 list_del_init(&com->lc_link);
332         if (!list_empty(&com->lc_link_dir))
333                 list_del_init(&com->lc_link_dir);
334
335         lfsck_component_put(env, com);
336 }
337
338 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
339                     struct lu_fid *fid, bool locked)
340 {
341         struct lfsck_bookmark   *bk = &lfsck->li_bookmark_ram;
342         int                      rc = 0;
343         ENTRY;
344
345         if (!locked)
346                 mutex_lock(&lfsck->li_mutex);
347
348         rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
349         if (rc >= 0) {
350                 bk->lb_last_fid = *fid;
351                 /* We do not care about whether the subsequent sub-operations
352                  * failed or not. The worst case is that one FID is lost that
353                  * is not a big issue for the LFSCK since it is relative rare
354                  * for LFSCK create. */
355                 rc = lfsck_bookmark_store(env, lfsck);
356         }
357
358         if (!locked)
359                 mutex_unlock(&lfsck->li_mutex);
360
361         RETURN(rc);
362 }
363
364 static int __lfsck_ibits_lock(const struct lu_env *env,
365                               struct lfsck_instance *lfsck,
366                               struct dt_object *obj,
367                               struct ldlm_res_id *resid,
368                               struct lustre_handle *lh,
369                               __u64 bits, ldlm_mode_t mode)
370 {
371         struct lfsck_thread_info        *info   = lfsck_env_info(env);
372         ldlm_policy_data_t              *policy = &info->lti_policy;
373         __u64                            flags  = LDLM_FL_ATOMIC_CB;
374         int                              rc;
375
376         LASSERT(lfsck->li_namespace != NULL);
377
378         memset(policy, 0, sizeof(*policy));
379         policy->l_inodebits.bits = bits;
380         if (dt_object_remote(obj)) {
381                 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
382
383                 memset(einfo, 0, sizeof(*einfo));
384                 einfo->ei_type = LDLM_IBITS;
385                 einfo->ei_mode = mode;
386                 einfo->ei_cb_bl = ldlm_blocking_ast;
387                 einfo->ei_cb_cp = ldlm_completion_ast;
388                 einfo->ei_res_id = resid;
389
390                 rc = dt_object_lock(env, obj, lh, einfo, policy);
391         } else {
392                 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
393                                             LDLM_IBITS, policy, mode,
394                                             &flags, ldlm_blocking_ast,
395                                             ldlm_completion_ast, NULL, NULL,
396                                             0, LVB_T_NONE, NULL, lh);
397         }
398
399         if (rc == ELDLM_OK) {
400                 rc = 0;
401         } else {
402                 memset(lh, 0, sizeof(*lh));
403                 rc = -EIO;
404         }
405
406         return rc;
407 }
408
409 /**
410  * Request the specified ibits lock for the given object.
411  *
412  * Before the LFSCK modifying on the namespace visible object,
413  * it needs to acquire related ibits ldlm lock.
414  *
415  * \param[in] env       pointer to the thread context
416  * \param[in] lfsck     pointer to the lfsck instance
417  * \param[in] obj       pointer to the dt_object to be locked
418  * \param[out] lh       pointer to the lock handle
419  * \param[in] bits      the bits for the ldlm lock to be acquired
420  * \param[in] mode      the mode for the ldlm lock to be acquired
421  *
422  * \retval              0 for success
423  * \retval              negative error number on failure
424  */
425 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
426                      struct dt_object *obj, struct lustre_handle *lh,
427                      __u64 bits, ldlm_mode_t mode)
428 {
429         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
430
431         LASSERT(!lustre_handle_is_used(lh));
432
433         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
434         return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
435 }
436
437 /**
438  * Release the the specified ibits lock.
439  *
440  * If the lock has been acquired before, release it
441  * and cleanup the handle. Otherwise, do nothing.
442  *
443  * \param[in] lh        pointer to the lock handle
444  * \param[in] mode      the mode for the ldlm lock to be released
445  */
446 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
447 {
448         if (lustre_handle_is_used(lh)) {
449                 ldlm_lock_decref(lh, mode);
450                 memset(lh, 0, sizeof(*lh));
451         }
452 }
453
454 /**
455  * Request compound ibits locks for the given <obj, name> pairs.
456  *
457  * Before the LFSCK modifying on the namespace visible object, it needs to
458  * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
459  * the lock purpose. But the simple lfsck_ibits_lock for directory-based
460  * modificationis (such as insert name entry to the directory) may be too
461  * coarse-grained and not efficient.
462  *
463  * The lfsck_lock() will request compound ibits locks on the specified
464  * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
465  * lock on the directory object, and the regular ibits lock on the name hash.
466  *
467  * \param[in] env       pointer to the thread context
468  * \param[in] lfsck     pointer to the lfsck instance
469  * \param[in] obj       pointer to the dt_object to be locked
470  * \param[in] name      used for building the PDO lock resource
471  * \param[out] llh      pointer to the lfsck_lock_handle
472  * \param[in] bits      the bits for the ldlm lock to be acquired
473  * \param[in] mode      the mode for the ldlm lock to be acquired
474  *
475  * \retval              0 for success
476  * \retval              negative error number on failure
477  */
478 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
479                    struct dt_object *obj, const char *name,
480                    struct lfsck_lock_handle *llh, __u64 bits, ldlm_mode_t mode)
481 {
482         struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
483         int                 rc;
484
485         LASSERT(S_ISDIR(lfsck_object_type(obj)));
486         LASSERT(name != NULL);
487         LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
488         LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
489
490         switch (mode) {
491         case LCK_EX:
492                 llh->llh_pdo_mode = LCK_EX;
493                 break;
494         case LCK_PW:
495                 llh->llh_pdo_mode = LCK_CW;
496                 break;
497         case LCK_PR:
498                 llh->llh_pdo_mode = LCK_CR;
499                 break;
500         default:
501                 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
502                        DFID"\n", lfsck_lfsck2name(lfsck), mode,
503                        PFID(lfsck_dto2fid(obj)));
504                 LBUG();
505         }
506
507         fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
508         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
509                                 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
510         if (rc != 0)
511                 return rc;
512
513         llh->llh_reg_mode = mode;
514         resid->name[LUSTRE_RES_ID_HSH_OFF] = full_name_hash(name, strlen(name));
515         rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
516                                 bits, llh->llh_reg_mode);
517         if (rc != 0)
518                 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
519
520         return rc;
521 }
522
523 /**
524  * Release the the compound ibits locks.
525  *
526  * \param[in] llh       pointer to the lfsck_lock_handle to be released
527  */
528 void lfsck_unlock(struct lfsck_lock_handle *llh)
529 {
530         lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
531         lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
532 }
533
534 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
535                               struct lfsck_instance *lfsck,
536                               const struct lu_fid *fid)
537 {
538         struct seq_server_site  *ss     = lfsck_dev_site(lfsck);
539         struct lu_seq_range     *range  = &lfsck_env_info(env)->lti_range;
540         int                      rc;
541
542         fld_range_set_mdt(range);
543         rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
544         if (rc == 0)
545                 rc = range->lsr_index;
546
547         return rc;
548 }
549
550 const char dot[] = ".";
551 const char dotdot[] = "..";
552 static const char dotlustre[] = ".lustre";
553 static const char lostfound[] = "lost+found";
554
555 /**
556  * Remove the name entry from the .lustre/lost+found directory.
557  *
558  * No need to care about the object referenced by the name entry,
559  * either the name entry is invalid or redundant, or the referenced
560  * object has been processed or will be handled by others.
561  *
562  * \param[in] env       pointer to the thread context
563  * \param[in] lfsck     pointer to the lfsck instance
564  * \param[in] name      the name for the name entry to be removed
565  *
566  * \retval              0 for success
567  * \retval              negative error number on failure
568  */
569 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
570                                        struct lfsck_instance *lfsck,
571                                        const char *name)
572 {
573         struct dt_object        *parent = lfsck->li_lpf_root_obj;
574         struct dt_device        *dev    = lfsck_obj2dev(parent);
575         struct thandle          *th;
576         struct lfsck_lock_handle *llh   = &lfsck_env_info(env)->lti_llh;
577         int                      rc;
578         ENTRY;
579
580         rc = lfsck_lock(env, lfsck, parent, name, llh,
581                         MDS_INODELOCK_UPDATE, LCK_PW);
582         if (rc != 0)
583                 RETURN(rc);
584
585         th = dt_trans_create(env, dev);
586         if (IS_ERR(th))
587                 GOTO(unlock, rc = PTR_ERR(th));
588
589         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
590         if (rc != 0)
591                 GOTO(stop, rc);
592
593         rc = dt_declare_ref_del(env, parent, th);
594         if (rc != 0)
595                 GOTO(stop, rc);
596
597         rc = dt_trans_start_local(env, dev, th);
598         if (rc != 0)
599                 GOTO(stop, rc);
600
601         rc = dt_delete(env, parent, (const struct dt_key *)name, th,
602                        BYPASS_CAPA);
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, BYPASS_CAPA, 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, BYPASS_CAPA, 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, BYPASS_CAPA);
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, BYPASS_CAPA, 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, BYPASS_CAPA, 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, BYPASS_CAPA, 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, BYPASS_CAPA);
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, BYPASS_CAPA, 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, BYPASS_CAPA);
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, BYPASS_CAPA);
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, BYPASS_CAPA);
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, BYPASS_CAPA);
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, BYPASS_CAPA);
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 /**
1948  * Generic LFSCK asynchronous communication interpretor function.
1949  * The LFSCK RPC reply for both the event notification and status
1950  * querying will be handled here.
1951  *
1952  * \param[in] env       pointer to the thread context
1953  * \param[in] req       pointer to the LFSCK request
1954  * \param[in] args      pointer to the lfsck_async_interpret_args
1955  * \param[in] rc        the result for handling the LFSCK request
1956  *
1957  * \retval              0 for success
1958  * \retval              negative error number on failure
1959  */
1960 int lfsck_async_interpret_common(const struct lu_env *env,
1961                                  struct ptlrpc_request *req,
1962                                  void *args, int rc)
1963 {
1964         struct lfsck_async_interpret_args *laia = args;
1965         struct lfsck_component            *com  = laia->laia_com;
1966         struct lfsck_assistant_data       *lad  = com->lc_data;
1967         struct lfsck_tgt_descs            *ltds = laia->laia_ltds;
1968         struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
1969         struct lfsck_request              *lr   = laia->laia_lr;
1970
1971         LASSERT(com->lc_lfsck->li_master);
1972
1973         switch (lr->lr_event) {
1974         case LE_START:
1975                 if (rc != 0) {
1976                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1977                                "start: rc = %d\n",
1978                                lfsck_lfsck2name(com->lc_lfsck),
1979                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1980                                ltd->ltd_index, lad->lad_name, rc);
1981
1982                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1983                                 struct lfsck_layout *lo = com->lc_file_ram;
1984
1985                                 if (lr->lr_flags & LEF_TO_OST)
1986                                         lfsck_lad_set_bitmap(env, com,
1987                                                              ltd->ltd_index);
1988                                 else
1989                                         lo->ll_flags |= LF_INCOMPLETE;
1990                         } else {
1991                                 struct lfsck_namespace *ns = com->lc_file_ram;
1992
1993                                 /* If some MDT does not join the namespace
1994                                  * LFSCK, then we cannot know whether there
1995                                  * is some name entry on such MDT that with
1996                                  * the referenced MDT-object on this MDT or
1997                                  * not. So the namespace LFSCK on this MDT
1998                                  * cannot handle orphan MDT-objects properly.
1999                                  * So we mark the LFSCK as LF_INCOMPLETE and
2000                                  * skip orphan MDT-objects handling. */
2001                                 ns->ln_flags |= LF_INCOMPLETE;
2002                         }
2003                         break;
2004                 }
2005
2006                 spin_lock(&ltds->ltd_lock);
2007                 if (ltd->ltd_dead) {
2008                         spin_unlock(&ltds->ltd_lock);
2009                         break;
2010                 }
2011
2012                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2013                         struct list_head *list;
2014                         struct list_head *phase_list;
2015
2016                         if (ltd->ltd_layout_done) {
2017                                 spin_unlock(&ltds->ltd_lock);
2018                                 break;
2019                         }
2020
2021                         if (lr->lr_flags & LEF_TO_OST) {
2022                                 list = &lad->lad_ost_list;
2023                                 phase_list = &lad->lad_ost_phase1_list;
2024                         } else {
2025                                 list = &lad->lad_mdt_list;
2026                                 phase_list = &lad->lad_mdt_phase1_list;
2027                         }
2028
2029                         if (list_empty(&ltd->ltd_layout_list))
2030                                 list_add_tail(&ltd->ltd_layout_list, list);
2031                         if (list_empty(&ltd->ltd_layout_phase_list))
2032                                 list_add_tail(&ltd->ltd_layout_phase_list,
2033                                               phase_list);
2034                 } else {
2035                         if (ltd->ltd_namespace_done) {
2036                                 spin_unlock(&ltds->ltd_lock);
2037                                 break;
2038                         }
2039
2040                         if (list_empty(&ltd->ltd_namespace_list))
2041                                 list_add_tail(&ltd->ltd_namespace_list,
2042                                               &lad->lad_mdt_list);
2043                         if (list_empty(&ltd->ltd_namespace_phase_list))
2044                                 list_add_tail(&ltd->ltd_namespace_phase_list,
2045                                               &lad->lad_mdt_phase1_list);
2046                 }
2047                 spin_unlock(&ltds->ltd_lock);
2048                 break;
2049         case LE_STOP:
2050         case LE_PHASE1_DONE:
2051         case LE_PHASE2_DONE:
2052         case LE_PEER_EXIT:
2053                 if (rc != 0 && rc != -EALREADY)
2054                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2055                               "event = %d, rc = %d\n",
2056                               lfsck_lfsck2name(com->lc_lfsck),
2057                               (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2058                               ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2059                 break;
2060         case LE_QUERY: {
2061                 struct lfsck_reply *reply;
2062                 struct list_head *list;
2063                 struct list_head *phase_list;
2064
2065                 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2066                         list = &ltd->ltd_layout_list;
2067                         phase_list = &ltd->ltd_layout_phase_list;
2068                 } else {
2069                         list = &ltd->ltd_namespace_list;
2070                         phase_list = &ltd->ltd_namespace_phase_list;
2071                 }
2072
2073                 if (rc != 0) {
2074                         spin_lock(&ltds->ltd_lock);
2075                         list_del_init(phase_list);
2076                         list_del_init(list);
2077                         spin_unlock(&ltds->ltd_lock);
2078                         break;
2079                 }
2080
2081                 reply = req_capsule_server_get(&req->rq_pill,
2082                                                &RMF_LFSCK_REPLY);
2083                 if (reply == NULL) {
2084                         rc = -EPROTO;
2085                         CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2086                                "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2087                                lad->lad_name, rc);
2088                         spin_lock(&ltds->ltd_lock);
2089                         list_del_init(phase_list);
2090                         list_del_init(list);
2091                         spin_unlock(&ltds->ltd_lock);
2092                         break;
2093                 }
2094
2095                 switch (reply->lr_status) {
2096                 case LS_SCANNING_PHASE1:
2097                         break;
2098                 case LS_SCANNING_PHASE2:
2099                         spin_lock(&ltds->ltd_lock);
2100                         list_del_init(phase_list);
2101                         if (ltd->ltd_dead) {
2102                                 spin_unlock(&ltds->ltd_lock);
2103                                 break;
2104                         }
2105
2106                         if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2107                                 if (ltd->ltd_layout_done) {
2108                                         spin_unlock(&ltds->ltd_lock);
2109                                         break;
2110                                 }
2111
2112                                 if (lr->lr_flags & LEF_TO_OST)
2113                                         list_add_tail(phase_list,
2114                                                 &lad->lad_ost_phase2_list);
2115                                 else
2116                                         list_add_tail(phase_list,
2117                                                 &lad->lad_mdt_phase2_list);
2118                         } else {
2119                                 if (ltd->ltd_namespace_done) {
2120                                         spin_unlock(&ltds->ltd_lock);
2121                                         break;
2122                                 }
2123
2124                                 list_add_tail(phase_list,
2125                                               &lad->lad_mdt_phase2_list);
2126                         }
2127                         spin_unlock(&ltds->ltd_lock);
2128                         break;
2129                 default:
2130                         spin_lock(&ltds->ltd_lock);
2131                         list_del_init(phase_list);
2132                         list_del_init(list);
2133                         spin_unlock(&ltds->ltd_lock);
2134                         break;
2135                 }
2136                 break;
2137         }
2138         default:
2139                 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2140                        lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2141                 break;
2142         }
2143
2144         if (!laia->laia_shared) {
2145                 lfsck_tgt_put(ltd);
2146                 lfsck_component_put(env, com);
2147         }
2148
2149         return 0;
2150 }
2151
2152 static void lfsck_interpret(const struct lu_env *env,
2153                             struct lfsck_instance *lfsck,
2154                             struct ptlrpc_request *req, void *args, int result)
2155 {
2156         struct lfsck_async_interpret_args *laia = args;
2157         struct lfsck_component            *com;
2158
2159         LASSERT(laia->laia_com == NULL);
2160         LASSERT(laia->laia_shared);
2161
2162         spin_lock(&lfsck->li_lock);
2163         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2164                 laia->laia_com = com;
2165                 lfsck_async_interpret_common(env, req, laia, result);
2166         }
2167
2168         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2169                 laia->laia_com = com;
2170                 lfsck_async_interpret_common(env, req, laia, result);
2171         }
2172         spin_unlock(&lfsck->li_lock);
2173 }
2174
2175 static int lfsck_stop_notify(const struct lu_env *env,
2176                              struct lfsck_instance *lfsck,
2177                              struct lfsck_tgt_descs *ltds,
2178                              struct lfsck_tgt_desc *ltd, __u16 type)
2179 {
2180         struct lfsck_component *com;
2181         int                     rc = 0;
2182         ENTRY;
2183
2184         LASSERT(lfsck->li_master);
2185
2186         spin_lock(&lfsck->li_lock);
2187         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2188         if (com == NULL)
2189                 com = __lfsck_component_find(lfsck, type,
2190                                              &lfsck->li_list_double_scan);
2191         if (com != NULL)
2192                 lfsck_component_get(com);
2193         spin_unlock(&lfsck->li_lock);
2194
2195         if (com != NULL) {
2196                 struct lfsck_thread_info          *info  = lfsck_env_info(env);
2197                 struct lfsck_async_interpret_args *laia  = &info->lti_laia;
2198                 struct lfsck_request              *lr    = &info->lti_lr;
2199                 struct lfsck_assistant_data       *lad   = com->lc_data;
2200                 struct list_head                  *list;
2201                 struct list_head                  *phase_list;
2202                 struct ptlrpc_request_set         *set;
2203
2204                 set = ptlrpc_prep_set();
2205                 if (set == NULL) {
2206                         lfsck_component_put(env, com);
2207
2208                         RETURN(-ENOMEM);
2209                 }
2210
2211                 if (type == LFSCK_TYPE_LAYOUT) {
2212                         list = &ltd->ltd_layout_list;
2213                         phase_list = &ltd->ltd_layout_phase_list;
2214                 } else {
2215                         list = &ltd->ltd_namespace_list;
2216                         phase_list = &ltd->ltd_namespace_phase_list;
2217                 }
2218
2219                 spin_lock(&ltds->ltd_lock);
2220                 if (list_empty(list)) {
2221                         LASSERT(list_empty(phase_list));
2222                         spin_unlock(&ltds->ltd_lock);
2223                         ptlrpc_set_destroy(set);
2224
2225                         RETURN(0);
2226                 }
2227
2228                 list_del_init(phase_list);
2229                 list_del_init(list);
2230                 spin_unlock(&ltds->ltd_lock);
2231
2232                 memset(lr, 0, sizeof(*lr));
2233                 lr->lr_index = lfsck_dev_idx(lfsck);
2234                 lr->lr_event = LE_PEER_EXIT;
2235                 lr->lr_active = type;
2236                 lr->lr_status = LS_CO_PAUSED;
2237                 if (ltds == &lfsck->li_ost_descs)
2238                         lr->lr_flags = LEF_TO_OST;
2239
2240                 laia->laia_com = com;
2241                 laia->laia_ltds = ltds;
2242                 atomic_inc(&ltd->ltd_ref);
2243                 laia->laia_ltd = ltd;
2244                 laia->laia_lr = lr;
2245                 laia->laia_shared = 0;
2246
2247                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2248                                          lfsck_async_interpret_common,
2249                                          laia, LFSCK_NOTIFY);
2250                 if (rc != 0) {
2251                         CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2252                                "co-stop for %s: rc = %d\n",
2253                                lfsck_lfsck2name(lfsck),
2254                                (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2255                                ltd->ltd_index, lad->lad_name, rc);
2256                         lfsck_tgt_put(ltd);
2257                 } else {
2258                         rc = ptlrpc_set_wait(set);
2259                 }
2260
2261                 ptlrpc_set_destroy(set);
2262                 lfsck_component_put(env, com);
2263         }
2264
2265         RETURN(rc);
2266 }
2267
2268 static int lfsck_async_interpret(const struct lu_env *env,
2269                                  struct ptlrpc_request *req,
2270                                  void *args, int rc)
2271 {
2272         struct lfsck_async_interpret_args *laia = args;
2273         struct lfsck_instance             *lfsck;
2274
2275         lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2276                               li_mdt_descs);
2277         lfsck_interpret(env, lfsck, req, laia, rc);
2278         lfsck_tgt_put(laia->laia_ltd);
2279         if (rc != 0 && laia->laia_result != -EALREADY)
2280                 laia->laia_result = rc;
2281
2282         return 0;
2283 }
2284
2285 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2286                         struct lfsck_request *lr,
2287                         struct ptlrpc_request_set *set,
2288                         ptlrpc_interpterer_t interpreter,
2289                         void *args, int request)
2290 {
2291         struct lfsck_async_interpret_args *laia;
2292         struct ptlrpc_request             *req;
2293         struct lfsck_request              *tmp;
2294         struct req_format                 *format;
2295         int                                rc;
2296
2297         switch (request) {
2298         case LFSCK_NOTIFY:
2299                 format = &RQF_LFSCK_NOTIFY;
2300                 break;
2301         case LFSCK_QUERY:
2302                 format = &RQF_LFSCK_QUERY;
2303                 break;
2304         default:
2305                 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2306                        exp->exp_obd->obd_name, request, -EINVAL);
2307                 return -EINVAL;
2308         }
2309
2310         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2311         if (req == NULL)
2312                 return -ENOMEM;
2313
2314         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2315         if (rc != 0) {
2316                 ptlrpc_request_free(req);
2317
2318                 return rc;
2319         }
2320
2321         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2322         *tmp = *lr;
2323         ptlrpc_request_set_replen(req);
2324
2325         laia = ptlrpc_req_async_args(req);
2326         *laia = *(struct lfsck_async_interpret_args *)args;
2327         if (laia->laia_com != NULL)
2328                 lfsck_component_get(laia->laia_com);
2329         req->rq_interpret_reply = interpreter;
2330         ptlrpc_set_add_req(set, req);
2331
2332         return 0;
2333 }
2334
2335 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2336                           struct lfsck_start_param *lsp)
2337 {
2338         struct lfsck_instance           *lfsck   = com->lc_lfsck;
2339         struct lfsck_assistant_data     *lad     = com->lc_data;
2340         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
2341         struct ptlrpc_thread            *athread = &lad->lad_thread;
2342         struct lfsck_thread_args        *lta;
2343         struct task_struct              *task;
2344         int                              rc;
2345         ENTRY;
2346
2347         lad->lad_assistant_status = 0;
2348         lad->lad_post_result = 0;
2349         lad->lad_to_post = 0;
2350         lad->lad_to_double_scan = 0;
2351         lad->lad_in_double_scan = 0;
2352         lad->lad_exit = 0;
2353         thread_set_flags(athread, 0);
2354
2355         lta = lfsck_thread_args_init(lfsck, com, lsp);
2356         if (IS_ERR(lta))
2357                 RETURN(PTR_ERR(lta));
2358
2359         task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2360         if (IS_ERR(task)) {
2361                 rc = PTR_ERR(task);
2362                 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2363                        "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2364                 lfsck_thread_args_fini(lta);
2365         } else {
2366                 struct l_wait_info lwi = { 0 };
2367
2368                 l_wait_event(mthread->t_ctl_waitq,
2369                              thread_is_running(athread) ||
2370                              thread_is_stopped(athread),
2371                              &lwi);
2372                 if (unlikely(!thread_is_running(athread)))
2373                         rc = lad->lad_assistant_status;
2374                 else
2375                         rc = 0;
2376         }
2377
2378         RETURN(rc);
2379 }
2380
2381 int lfsck_checkpoint_generic(const struct lu_env *env,
2382                              struct lfsck_component *com)
2383 {
2384         struct lfsck_assistant_data     *lad     = com->lc_data;
2385         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2386         struct ptlrpc_thread            *athread = &lad->lad_thread;
2387         struct l_wait_info               lwi     = { 0 };
2388
2389         l_wait_event(mthread->t_ctl_waitq,
2390                      list_empty(&lad->lad_req_list) ||
2391                      !thread_is_running(mthread) ||
2392                      thread_is_stopped(athread),
2393                      &lwi);
2394
2395         if (!thread_is_running(mthread) || thread_is_stopped(athread))
2396                 return LFSCK_CHECKPOINT_SKIP;
2397
2398         return 0;
2399 }
2400
2401 void lfsck_post_generic(const struct lu_env *env,
2402                         struct lfsck_component *com, int *result)
2403 {
2404         struct lfsck_assistant_data     *lad     = com->lc_data;
2405         struct ptlrpc_thread            *athread = &lad->lad_thread;
2406         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2407         struct l_wait_info               lwi     = { 0 };
2408
2409         lad->lad_post_result = *result;
2410         if (*result <= 0)
2411                 lad->lad_exit = 1;
2412         lad->lad_to_post = 1;
2413
2414         wake_up_all(&athread->t_ctl_waitq);
2415         l_wait_event(mthread->t_ctl_waitq,
2416                      (*result > 0 && list_empty(&lad->lad_req_list)) ||
2417                      thread_is_stopped(athread),
2418                      &lwi);
2419
2420         if (lad->lad_assistant_status < 0)
2421                 *result = lad->lad_assistant_status;
2422 }
2423
2424 int lfsck_double_scan_generic(const struct lu_env *env,
2425                               struct lfsck_component *com, int status)
2426 {
2427         struct lfsck_assistant_data     *lad     = com->lc_data;
2428         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2429         struct ptlrpc_thread            *athread = &lad->lad_thread;
2430         struct l_wait_info               lwi     = { 0 };
2431
2432         if (status != LS_SCANNING_PHASE2)
2433                 lad->lad_exit = 1;
2434         else
2435                 lad->lad_to_double_scan = 1;
2436
2437         wake_up_all(&athread->t_ctl_waitq);
2438         l_wait_event(mthread->t_ctl_waitq,
2439                      lad->lad_in_double_scan ||
2440                      thread_is_stopped(athread),
2441                      &lwi);
2442
2443         if (lad->lad_assistant_status < 0)
2444                 return lad->lad_assistant_status;
2445
2446         return 0;
2447 }
2448
2449 void lfsck_quit_generic(const struct lu_env *env,
2450                         struct lfsck_component *com)
2451 {
2452         struct lfsck_assistant_data     *lad     = com->lc_data;
2453         struct ptlrpc_thread            *mthread = &com->lc_lfsck->li_thread;
2454         struct ptlrpc_thread            *athread = &lad->lad_thread;
2455         struct l_wait_info               lwi     = { 0 };
2456
2457         lad->lad_exit = 1;
2458         wake_up_all(&athread->t_ctl_waitq);
2459         l_wait_event(mthread->t_ctl_waitq,
2460                      thread_is_init(athread) ||
2461                      thread_is_stopped(athread),
2462                      &lwi);
2463 }
2464
2465 /* external interfaces */
2466
2467 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2468 {
2469         struct lu_env           env;
2470         struct lfsck_instance  *lfsck;
2471         int                     rc;
2472         ENTRY;
2473
2474         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2475         if (rc != 0)
2476                 RETURN(rc);
2477
2478         lfsck = lfsck_instance_find(key, true, false);
2479         if (likely(lfsck != NULL)) {
2480                 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2481                 lfsck_instance_put(&env, lfsck);
2482         } else {
2483                 rc = -ENXIO;
2484         }
2485
2486         lu_env_fini(&env);
2487
2488         RETURN(rc);
2489 }
2490 EXPORT_SYMBOL(lfsck_get_speed);
2491
2492 int lfsck_set_speed(struct dt_device *key, int val)
2493 {
2494         struct lu_env           env;
2495         struct lfsck_instance  *lfsck;
2496         int                     rc;
2497         ENTRY;
2498
2499         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2500         if (rc != 0)
2501                 RETURN(rc);
2502
2503         lfsck = lfsck_instance_find(key, true, false);
2504         if (likely(lfsck != NULL)) {
2505                 mutex_lock(&lfsck->li_mutex);
2506                 if (__lfsck_set_speed(lfsck, val))
2507                         rc = lfsck_bookmark_store(&env, lfsck);
2508                 mutex_unlock(&lfsck->li_mutex);
2509                 lfsck_instance_put(&env, lfsck);
2510         } else {
2511                 rc = -ENXIO;
2512         }
2513
2514         lu_env_fini(&env);
2515
2516         RETURN(rc);
2517 }
2518 EXPORT_SYMBOL(lfsck_set_speed);
2519
2520 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2521 {
2522         struct lu_env           env;
2523         struct lfsck_instance  *lfsck;
2524         int                     rc;
2525         ENTRY;
2526
2527         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2528         if (rc != 0)
2529                 RETURN(rc);
2530
2531         lfsck = lfsck_instance_find(key, true, false);
2532         if (likely(lfsck != NULL)) {
2533                 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2534                 lfsck_instance_put(&env, lfsck);
2535         } else {
2536                 rc = -ENXIO;
2537         }
2538
2539         lu_env_fini(&env);
2540
2541         RETURN(rc);
2542 }
2543 EXPORT_SYMBOL(lfsck_get_windows);
2544
2545 int lfsck_set_windows(struct dt_device *key, int val)
2546 {
2547         struct lu_env           env;
2548         struct lfsck_instance  *lfsck;
2549         int                     rc;
2550         ENTRY;
2551
2552         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2553         if (rc != 0)
2554                 RETURN(rc);
2555
2556         lfsck = lfsck_instance_find(key, true, false);
2557         if (likely(lfsck != NULL)) {
2558                 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2559                         CWARN("%s: invalid async windows size that may "
2560                               "cause memory issues. The valid range is "
2561                               "[1 - %u].\n",
2562                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2563                         rc = -EINVAL;
2564                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2565                         mutex_lock(&lfsck->li_mutex);
2566                         lfsck->li_bookmark_ram.lb_async_windows = val;
2567                         rc = lfsck_bookmark_store(&env, lfsck);
2568                         mutex_unlock(&lfsck->li_mutex);
2569                 }
2570                 lfsck_instance_put(&env, lfsck);
2571         } else {
2572                 rc = -ENXIO;
2573         }
2574
2575         lu_env_fini(&env);
2576
2577         RETURN(rc);
2578 }
2579 EXPORT_SYMBOL(lfsck_set_windows);
2580
2581 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2582 {
2583         struct lu_env           env;
2584         struct lfsck_instance  *lfsck;
2585         struct lfsck_component *com;
2586         int                     rc;
2587         ENTRY;
2588
2589         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2590         if (rc != 0)
2591                 RETURN(rc);
2592
2593         lfsck = lfsck_instance_find(key, true, false);
2594         if (likely(lfsck != NULL)) {
2595                 com = lfsck_component_find(lfsck, type);
2596                 if (likely(com != NULL)) {
2597                         rc = com->lc_ops->lfsck_dump(&env, com, m);
2598                         lfsck_component_put(&env, com);
2599                 } else {
2600                         rc = -ENOTSUPP;
2601                 }
2602
2603                 lfsck_instance_put(&env, lfsck);
2604         } else {
2605                 rc = -ENXIO;
2606         }
2607
2608         lu_env_fini(&env);
2609
2610         RETURN(rc);
2611 }
2612 EXPORT_SYMBOL(lfsck_dump);
2613
2614 static int lfsck_stop_all(const struct lu_env *env,
2615                           struct lfsck_instance *lfsck,
2616                           struct lfsck_stop *stop)
2617 {
2618         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2619         struct lfsck_request              *lr     = &info->lti_lr;
2620         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2621         struct ptlrpc_request_set         *set;
2622         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2623         struct lfsck_tgt_desc             *ltd;
2624         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2625         __u32                              idx;
2626         int                                rc     = 0;
2627         int                                rc1    = 0;
2628         ENTRY;
2629
2630         LASSERT(stop->ls_flags & LPF_BROADCAST);
2631
2632         set = ptlrpc_prep_set();
2633         if (unlikely(set == NULL))
2634                 RETURN(-ENOMEM);
2635
2636         memset(lr, 0, sizeof(*lr));
2637         lr->lr_event = LE_STOP;
2638         lr->lr_index = lfsck_dev_idx(lfsck);
2639         lr->lr_status = stop->ls_status;
2640         lr->lr_version = bk->lb_version;
2641         lr->lr_active = LFSCK_TYPES_ALL;
2642         lr->lr_param = stop->ls_flags;
2643
2644         laia->laia_com = NULL;
2645         laia->laia_ltds = ltds;
2646         laia->laia_lr = lr;
2647         laia->laia_result = 0;
2648         laia->laia_shared = 1;
2649
2650         down_read(&ltds->ltd_rw_sem);
2651         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2652                 ltd = lfsck_tgt_get(ltds, idx);
2653                 LASSERT(ltd != NULL);
2654
2655                 laia->laia_ltd = ltd;
2656                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2657                                          lfsck_async_interpret, laia,
2658                                          LFSCK_NOTIFY);
2659                 if (rc != 0) {
2660                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2661                         lfsck_tgt_put(ltd);
2662                         CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2663                                "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2664                         rc1 = rc;
2665                 }
2666         }
2667         up_read(&ltds->ltd_rw_sem);
2668
2669         rc = ptlrpc_set_wait(set);
2670         ptlrpc_set_destroy(set);
2671
2672         if (rc == 0)
2673                 rc = laia->laia_result;
2674
2675         if (rc == -EALREADY)
2676                 rc = 0;
2677
2678         if (rc != 0)
2679                 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2680                        lfsck_lfsck2name(lfsck), rc);
2681
2682         RETURN(rc != 0 ? rc : rc1);
2683 }
2684
2685 static int lfsck_start_all(const struct lu_env *env,
2686                            struct lfsck_instance *lfsck,
2687                            struct lfsck_start *start)
2688 {
2689         struct lfsck_thread_info          *info   = lfsck_env_info(env);
2690         struct lfsck_request              *lr     = &info->lti_lr;
2691         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
2692         struct ptlrpc_request_set         *set;
2693         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
2694         struct lfsck_tgt_desc             *ltd;
2695         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
2696         __u32                              idx;
2697         int                                rc     = 0;
2698         ENTRY;
2699
2700         LASSERT(start->ls_flags & LPF_BROADCAST);
2701
2702         set = ptlrpc_prep_set();
2703         if (unlikely(set == NULL))
2704                 RETURN(-ENOMEM);
2705
2706         memset(lr, 0, sizeof(*lr));
2707         lr->lr_event = LE_START;
2708         lr->lr_index = lfsck_dev_idx(lfsck);
2709         lr->lr_speed = bk->lb_speed_limit;
2710         lr->lr_version = bk->lb_version;
2711         lr->lr_active = start->ls_active;
2712         lr->lr_param = start->ls_flags;
2713         lr->lr_async_windows = bk->lb_async_windows;
2714         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2715                        LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2716                        LSV_CREATE_MDTOBJ;
2717
2718         laia->laia_com = NULL;
2719         laia->laia_ltds = ltds;
2720         laia->laia_lr = lr;
2721         laia->laia_result = 0;
2722         laia->laia_shared = 1;
2723
2724         down_read(&ltds->ltd_rw_sem);
2725         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2726                 ltd = lfsck_tgt_get(ltds, idx);
2727                 LASSERT(ltd != NULL);
2728
2729                 laia->laia_ltd = ltd;
2730                 ltd->ltd_layout_done = 0;
2731                 ltd->ltd_namespace_done = 0;
2732                 ltd->ltd_synced_failures = 0;
2733                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2734                                          lfsck_async_interpret, laia,
2735                                          LFSCK_NOTIFY);
2736                 if (rc != 0) {
2737                         lfsck_interpret(env, lfsck, NULL, laia, rc);
2738                         lfsck_tgt_put(ltd);
2739                         CERROR("%s: cannot notify MDT %x for LFSCK "
2740                                "start, failout: rc = %d\n",
2741                                lfsck_lfsck2name(lfsck), idx, rc);
2742                         break;
2743                 }
2744         }
2745         up_read(&ltds->ltd_rw_sem);
2746
2747         if (rc != 0) {
2748                 ptlrpc_set_destroy(set);
2749
2750                 RETURN(rc);
2751         }
2752
2753         rc = ptlrpc_set_wait(set);
2754         ptlrpc_set_destroy(set);
2755
2756         if (rc == 0)
2757                 rc = laia->laia_result;
2758
2759         if (rc != 0) {
2760                 struct lfsck_stop *stop = &info->lti_stop;
2761
2762                 CERROR("%s: cannot start LFSCK on some MDTs, "
2763                        "stop all: rc = %d\n",
2764                        lfsck_lfsck2name(lfsck), rc);
2765                 if (rc != -EALREADY) {
2766                         stop->ls_status = LS_FAILED;
2767                         stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2768                         lfsck_stop_all(env, lfsck, stop);
2769                 }
2770         }
2771
2772         RETURN(rc);
2773 }
2774
2775 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2776                 struct lfsck_start_param *lsp)
2777 {
2778         struct lfsck_start              *start  = lsp->lsp_start;
2779         struct lfsck_instance           *lfsck;
2780         struct lfsck_bookmark           *bk;
2781         struct ptlrpc_thread            *thread;
2782         struct lfsck_component          *com;
2783         struct l_wait_info               lwi    = { 0 };
2784         struct lfsck_thread_args        *lta;
2785         struct task_struct              *task;
2786         int                              rc     = 0;
2787         __u16                            valid  = 0;
2788         __u16                            flags  = 0;
2789         __u16                            type   = 1;
2790         ENTRY;
2791
2792         lfsck = lfsck_instance_find(key, true, false);
2793         if (unlikely(lfsck == NULL))
2794                 RETURN(-ENXIO);
2795
2796         /* System is not ready, try again later. */
2797         if (unlikely(lfsck->li_namespace == NULL))
2798                 GOTO(put, rc = -EAGAIN);
2799
2800         /* start == NULL means auto trigger paused LFSCK. */
2801         if ((start == NULL) &&
2802             (list_empty(&lfsck->li_list_scan) ||
2803              OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2804                 GOTO(put, rc = 0);
2805
2806         bk = &lfsck->li_bookmark_ram;
2807         thread = &lfsck->li_thread;
2808         mutex_lock(&lfsck->li_mutex);
2809         spin_lock(&lfsck->li_lock);
2810         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2811                 rc = -EALREADY;
2812                 if (unlikely(start == NULL)) {
2813                         spin_unlock(&lfsck->li_lock);
2814                         GOTO(out, rc);
2815                 }
2816
2817                 while (start->ls_active != 0) {
2818                         if (!(type & start->ls_active)) {
2819                                 type <<= 1;
2820                                 continue;
2821                         }
2822
2823                         com = __lfsck_component_find(lfsck, type,
2824                                                      &lfsck->li_list_scan);
2825                         if (com == NULL)
2826                                 com = __lfsck_component_find(lfsck, type,
2827                                                 &lfsck->li_list_double_scan);
2828                         if (com == NULL) {
2829                                 rc = -EOPNOTSUPP;
2830                                 break;
2831                         }
2832
2833                         if (com->lc_ops->lfsck_join != NULL) {
2834                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
2835                                 if (rc != 0 && rc != -EALREADY)
2836                                         break;
2837                         }
2838                         start->ls_active &= ~type;
2839                         type <<= 1;
2840                 }
2841                 spin_unlock(&lfsck->li_lock);
2842                 GOTO(out, rc);
2843         }
2844         spin_unlock(&lfsck->li_lock);
2845
2846         lfsck->li_status = 0;
2847         lfsck->li_oit_over = 0;
2848         lfsck->li_start_unplug = 0;
2849         lfsck->li_drop_dryrun = 0;
2850         lfsck->li_new_scanned = 0;
2851
2852         /* For auto trigger. */
2853         if (start == NULL)
2854                 goto trigger;
2855
2856         if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2857                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2858                        lfsck_lfsck2name(lfsck));
2859
2860                 GOTO(out, rc = -EPERM);
2861         }
2862
2863         start->ls_version = bk->lb_version;
2864
2865         if (start->ls_active != 0) {
2866                 struct lfsck_component *next;
2867
2868                 if (start->ls_active == LFSCK_TYPES_ALL)
2869                         start->ls_active = LFSCK_TYPES_SUPPORTED;
2870
2871                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2872                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2873                         GOTO(out, rc = -ENOTSUPP);
2874                 }
2875
2876                 list_for_each_entry_safe(com, next,
2877                                          &lfsck->li_list_scan, lc_link) {
2878                         if (!(com->lc_type & start->ls_active)) {
2879                                 rc = com->lc_ops->lfsck_post(env, com, 0,
2880                                                              false);
2881                                 if (rc != 0)
2882                                         GOTO(out, rc);
2883                         }
2884                 }
2885
2886                 while (start->ls_active != 0) {
2887                         if (type & start->ls_active) {
2888                                 com = __lfsck_component_find(lfsck, type,
2889                                                         &lfsck->li_list_idle);
2890                                 if (com != NULL)
2891                                         /* The component status will be updated
2892                                          * when its prep() is called later by
2893                                          * the LFSCK main engine. */
2894                                         list_move_tail(&com->lc_link,
2895                                                        &lfsck->li_list_scan);
2896                                 start->ls_active &= ~type;
2897                         }
2898                         type <<= 1;
2899                 }
2900         }
2901
2902         if (list_empty(&lfsck->li_list_scan)) {
2903                 /* The speed limit will be used to control both the LFSCK and
2904                  * low layer scrub (if applied), need to be handled firstly. */
2905                 if (start->ls_valid & LSV_SPEED_LIMIT) {
2906                         if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2907                                 rc = lfsck_bookmark_store(env, lfsck);
2908                                 if (rc != 0)
2909                                         GOTO(out, rc);
2910                         }
2911                 }
2912
2913                 goto trigger;
2914         }
2915
2916         if (start->ls_flags & LPF_RESET)
2917                 flags |= DOIF_RESET;
2918
2919         rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2920         if (rc != 0)
2921                 GOTO(out, rc);
2922
2923         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2924                 start->ls_active |= com->lc_type;
2925                 if (flags & DOIF_RESET) {
2926                         rc = com->lc_ops->lfsck_reset(env, com, false);
2927                         if (rc != 0)
2928                                 GOTO(out, rc);
2929                 }
2930         }
2931
2932 trigger:
2933         lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2934         if (bk->lb_param & LPF_DRYRUN)
2935                 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2936
2937         if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2938                 valid |= DOIV_ERROR_HANDLE;
2939                 if (start->ls_flags & LPF_FAILOUT)
2940                         flags |= DOIF_FAILOUT;
2941         }
2942
2943         if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2944                 valid |= DOIV_DRYRUN;
2945                 if (start->ls_flags & LPF_DRYRUN)
2946                         flags |= DOIF_DRYRUN;
2947         }
2948
2949         if (!list_empty(&lfsck->li_list_scan))
2950                 flags |= DOIF_OUTUSED;
2951
2952         lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2953         thread_set_flags(thread, 0);
2954         lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2955         if (IS_ERR(lta))
2956                 GOTO(out, rc = PTR_ERR(lta));
2957
2958         __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2959         task = kthread_run(lfsck_master_engine, lta, "lfsck");
2960         if (IS_ERR(task)) {
2961                 rc = PTR_ERR(task);
2962                 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2963                        lfsck_lfsck2name(lfsck), rc);
2964                 lfsck_thread_args_fini(lta);
2965
2966                 GOTO(out, rc);
2967         }
2968
2969         l_wait_event(thread->t_ctl_waitq,
2970                      thread_is_running(thread) ||
2971                      thread_is_stopped(thread),
2972                      &lwi);
2973         if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2974                 lfsck->li_start_unplug = 1;
2975                 wake_up_all(&thread->t_ctl_waitq);
2976
2977                 GOTO(out, rc = 0);
2978         }
2979
2980         /* release lfsck::li_mutex to avoid deadlock. */
2981         mutex_unlock(&lfsck->li_mutex);
2982         rc = lfsck_start_all(env, lfsck, start);
2983         if (rc != 0) {
2984                 spin_lock(&lfsck->li_lock);
2985                 if (thread_is_stopped(thread)) {
2986                         spin_unlock(&lfsck->li_lock);
2987                 } else {
2988                         lfsck->li_status = LS_FAILED;
2989                         lfsck->li_flags = 0;
2990                         thread_set_flags(thread, SVC_STOPPING);
2991                         spin_unlock(&lfsck->li_lock);
2992
2993                         lfsck->li_start_unplug = 1;
2994                         wake_up_all(&thread->t_ctl_waitq);
2995                         l_wait_event(thread->t_ctl_waitq,
2996                                      thread_is_stopped(thread),
2997                                      &lwi);
2998                 }
2999         } else {
3000                 lfsck->li_start_unplug = 1;
3001                 wake_up_all(&thread->t_ctl_waitq);
3002         }
3003
3004         GOTO(put, rc);
3005
3006 out:
3007         mutex_unlock(&lfsck->li_mutex);
3008
3009 put:
3010         lfsck_instance_put(env, lfsck);
3011
3012         return rc < 0 ? rc : 0;
3013 }
3014 EXPORT_SYMBOL(lfsck_start);
3015
3016 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3017                struct lfsck_stop *stop)
3018 {
3019         struct lfsck_instance   *lfsck;
3020         struct ptlrpc_thread    *thread;
3021         struct l_wait_info       lwi    = { 0 };
3022         int                      rc     = 0;
3023         int                      rc1    = 0;
3024         ENTRY;
3025
3026         lfsck = lfsck_instance_find(key, true, false);
3027         if (unlikely(lfsck == NULL))
3028                 RETURN(-ENXIO);
3029
3030         thread = &lfsck->li_thread;
3031         /* release lfsck::li_mutex to avoid deadlock. */
3032         if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3033                 if (!lfsck->li_master) {
3034                         CERROR("%s: only allow to specify '-A' via MDS\n",
3035                                lfsck_lfsck2name(lfsck));
3036
3037                         GOTO(out, rc = -EPERM);
3038                 }
3039
3040                 rc1 = lfsck_stop_all(env, lfsck, stop);
3041         }
3042
3043         mutex_lock(&lfsck->li_mutex);
3044         spin_lock(&lfsck->li_lock);
3045         /* no error if LFSCK is already stopped, or was never started */
3046         if (thread_is_init(thread) || thread_is_stopped(thread)) {
3047                 spin_unlock(&lfsck->li_lock);
3048                 GOTO(out, rc = 0);
3049         }
3050
3051         if (stop != NULL) {
3052                 lfsck->li_status = stop->ls_status;
3053                 lfsck->li_flags = stop->ls_flags;
3054         } else {
3055                 lfsck->li_status = LS_STOPPED;
3056                 lfsck->li_flags = 0;
3057         }
3058
3059         thread_set_flags(thread, SVC_STOPPING);
3060         spin_unlock(&lfsck->li_lock);
3061
3062         wake_up_all(&thread->t_ctl_waitq);
3063         l_wait_event(thread->t_ctl_waitq,
3064                      thread_is_stopped(thread),
3065                      &lwi);
3066
3067         GOTO(out, rc = 0);
3068
3069 out:
3070         mutex_unlock(&lfsck->li_mutex);
3071         lfsck_instance_put(env, lfsck);
3072
3073         return rc != 0 ? rc : rc1;
3074 }
3075 EXPORT_SYMBOL(lfsck_stop);
3076
3077 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3078                     struct lfsck_request *lr, struct thandle *th)
3079 {
3080         int rc = -EOPNOTSUPP;
3081         ENTRY;
3082
3083         switch (lr->lr_event) {
3084         case LE_START: {
3085                 struct lfsck_start       *start = &lfsck_env_info(env)->lti_start;
3086                 struct lfsck_start_param  lsp;
3087
3088                 memset(start, 0, sizeof(*start));
3089                 start->ls_valid = lr->lr_valid;
3090                 start->ls_speed_limit = lr->lr_speed;
3091                 start->ls_version = lr->lr_version;
3092                 start->ls_active = lr->lr_active;
3093                 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3094                 start->ls_async_windows = lr->lr_async_windows;
3095
3096                 lsp.lsp_start = start;
3097                 lsp.lsp_index = lr->lr_index;
3098                 lsp.lsp_index_valid = 1;
3099                 rc = lfsck_start(env, key, &lsp);
3100                 break;
3101         }
3102         case LE_STOP: {
3103                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3104
3105                 memset(stop, 0, sizeof(*stop));
3106                 stop->ls_status = lr->lr_status;
3107                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3108                 rc = lfsck_stop(env, key, stop);
3109                 break;
3110         }
3111         case LE_PHASE1_DONE:
3112         case LE_PHASE2_DONE:
3113         case LE_FID_ACCESSED:
3114         case LE_PEER_EXIT:
3115         case LE_CONDITIONAL_DESTROY:
3116         case LE_SKIP_NLINK_DECLARE:
3117         case LE_SKIP_NLINK:
3118         case LE_SET_LMV_MASTER:
3119         case LE_SET_LMV_SLAVE:
3120         case LE_PAIRS_VERIFY: {
3121                 struct lfsck_instance  *lfsck;
3122                 struct lfsck_component *com;
3123
3124                 lfsck = lfsck_instance_find(key, true, false);
3125                 if (unlikely(lfsck == NULL))
3126                         RETURN(-ENXIO);
3127
3128                 com = lfsck_component_find(lfsck, lr->lr_active);
3129                 if (likely(com != NULL)) {
3130                         rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3131                         lfsck_component_put(env, com);
3132                 }
3133
3134                 lfsck_instance_put(env, lfsck);
3135                 break;
3136         }
3137         default:
3138                 break;
3139         }
3140
3141         RETURN(rc);
3142 }
3143 EXPORT_SYMBOL(lfsck_in_notify);
3144
3145 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3146                 struct lfsck_request *lr)
3147 {
3148         struct lfsck_instance  *lfsck;
3149         struct lfsck_component *com;
3150         int                     rc;
3151         ENTRY;
3152
3153         lfsck = lfsck_instance_find(key, true, false);
3154         if (unlikely(lfsck == NULL))
3155                 RETURN(-ENXIO);
3156
3157         com = lfsck_component_find(lfsck, lr->lr_active);
3158         if (likely(com != NULL)) {
3159                 rc = com->lc_ops->lfsck_query(env, com);
3160                 lfsck_component_put(env, com);
3161         } else {
3162                 rc = -ENOTSUPP;
3163         }
3164
3165         lfsck_instance_put(env, lfsck);
3166
3167         RETURN(rc);
3168 }
3169
3170 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3171                              struct ldlm_namespace *ns)
3172 {
3173         struct lfsck_instance  *lfsck;
3174         int                     rc      = -ENXIO;
3175
3176         lfsck = lfsck_instance_find(key, true, false);
3177         if (likely(lfsck != NULL)) {
3178                 lfsck->li_namespace = ns;
3179                 lfsck_instance_put(env, lfsck);
3180                 rc = 0;
3181         }
3182
3183         return rc;
3184 }
3185 EXPORT_SYMBOL(lfsck_register_namespace);
3186
3187 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3188                    struct dt_device *next, struct obd_device *obd,
3189                    lfsck_out_notify notify, void *notify_data, bool master)
3190 {
3191         struct lfsck_instance   *lfsck;
3192         struct dt_object        *root  = NULL;
3193         struct dt_object        *obj   = NULL;
3194         struct lu_fid           *fid   = &lfsck_env_info(env)->lti_fid;
3195         int                      rc;
3196         ENTRY;
3197
3198         lfsck = lfsck_instance_find(key, false, false);
3199         if (unlikely(lfsck != NULL))
3200                 RETURN(-EEXIST);
3201
3202         OBD_ALLOC_PTR(lfsck);
3203         if (lfsck == NULL)
3204                 RETURN(-ENOMEM);
3205
3206         mutex_init(&lfsck->li_mutex);
3207         spin_lock_init(&lfsck->li_lock);
3208         INIT_LIST_HEAD(&lfsck->li_link);
3209         INIT_LIST_HEAD(&lfsck->li_list_scan);
3210         INIT_LIST_HEAD(&lfsck->li_list_dir);
3211         INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3212         INIT_LIST_HEAD(&lfsck->li_list_idle);
3213         INIT_LIST_HEAD(&lfsck->li_list_lmv);
3214         atomic_set(&lfsck->li_ref, 1);
3215         atomic_set(&lfsck->li_double_scan_count, 0);
3216         init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3217         lfsck->li_out_notify = notify;
3218         lfsck->li_out_notify_data = notify_data;
3219         lfsck->li_next = next;
3220         lfsck->li_bottom = key;
3221         lfsck->li_obd = obd;
3222
3223         rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3224         if (rc != 0)
3225                 GOTO(out, rc);
3226
3227         rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3228         if (rc != 0)
3229                 GOTO(out, rc);
3230
3231         fid->f_seq = FID_SEQ_LOCAL_NAME;
3232         fid->f_oid = 1;
3233         fid->f_ver = 0;
3234         rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3235         if (rc != 0)
3236                 GOTO(out, rc);
3237
3238         rc = dt_root_get(env, key, fid);
3239         if (rc != 0)
3240                 GOTO(out, rc);
3241
3242         root = dt_locate(env, key, fid);
3243         if (IS_ERR(root))
3244                 GOTO(out, rc = PTR_ERR(root));
3245
3246         if (unlikely(!dt_try_as_dir(env, root)))
3247                 GOTO(out, rc = -ENOTDIR);
3248
3249         lfsck->li_local_root_fid = *fid;
3250         if (master) {
3251                 lfsck->li_master = 1;
3252                 if (lfsck_dev_idx(lfsck) == 0) {
3253                         struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3254                         const struct lu_name *cname;
3255
3256                         rc = dt_lookup(env, root,
3257                                 (struct dt_rec *)(&lfsck->li_global_root_fid),
3258                                 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3259                         if (rc != 0)
3260                                 GOTO(out, rc);
3261
3262                         obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3263                         if (IS_ERR(obj))
3264                                 GOTO(out, rc = PTR_ERR(obj));
3265
3266                         if (unlikely(!dt_try_as_dir(env, obj)))
3267                                 GOTO(out, rc = -ENOTDIR);
3268
3269                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3270                                 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3271                         if (rc != 0)
3272                                 GOTO(out, rc);
3273
3274                         lfsck_object_put(env, obj);
3275                         obj = dt_locate(env, key, fid);
3276                         if (IS_ERR(obj))
3277                                 GOTO(out, rc = PTR_ERR(obj));
3278
3279                         cname = lfsck_name_get_const(env, dotlustre,
3280                                                      strlen(dotlustre));
3281                         rc = lfsck_verify_linkea(env, obj, cname,
3282                                                  &lfsck->li_global_root_fid);
3283                         if (rc != 0)
3284                                 GOTO(out, rc);
3285
3286                         if (unlikely(!dt_try_as_dir(env, obj)))
3287                                 GOTO(out, rc = -ENOTDIR);
3288
3289                         *pfid = *fid;
3290                         rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3291                                        (const struct dt_key *)lostfound,
3292                                        BYPASS_CAPA);
3293                         if (rc != 0)
3294                                 GOTO(out, rc);
3295
3296                         lfsck_object_put(env, obj);
3297                         obj = dt_locate(env, key, fid);
3298                         if (IS_ERR(obj))
3299                                 GOTO(out, rc = PTR_ERR(obj));
3300
3301                         cname = lfsck_name_get_const(env, lostfound,
3302                                                      strlen(lostfound));
3303                         rc = lfsck_verify_linkea(env, obj, cname, pfid);
3304                         if (rc != 0)
3305                                 GOTO(out, rc);
3306
3307                         lfsck_object_put(env, obj);
3308                         obj = NULL;
3309                 }
3310         }
3311
3312         fid->f_seq = FID_SEQ_LOCAL_FILE;
3313         fid->f_oid = OTABLE_IT_OID;
3314         fid->f_ver = 0;
3315         obj = dt_locate(env, key, fid);
3316         if (IS_ERR(obj))
3317                 GOTO(out, rc = PTR_ERR(obj));
3318
3319         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3320         if (rc != 0)
3321                 GOTO(out, rc);
3322
3323         lfsck->li_obj_oit = obj;
3324         obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3325                                         S_IFDIR | S_IRUGO | S_IWUSR);
3326         if (IS_ERR(obj))
3327                 GOTO(out, rc = PTR_ERR(obj));
3328
3329         lu_object_get(&obj->do_lu);
3330         lfsck->li_lfsck_dir = obj;
3331         rc = lfsck_bookmark_setup(env, lfsck);
3332         if (rc != 0)
3333                 GOTO(out, rc);
3334
3335         if (master) {
3336                 rc = lfsck_fid_init(lfsck);
3337                 if (rc < 0)
3338                         GOTO(out, rc);
3339
3340                 rc = lfsck_namespace_setup(env, lfsck);
3341                 if (rc < 0)
3342                         GOTO(out, rc);
3343         }
3344
3345         rc = lfsck_layout_setup(env, lfsck);
3346         if (rc < 0)
3347                 GOTO(out, rc);
3348
3349         /* XXX: more LFSCK components initialization to be added here. */
3350
3351         rc = lfsck_instance_add(lfsck);
3352         if (rc == 0)
3353                 rc = lfsck_add_target_from_orphan(env, lfsck);
3354 out:
3355         if (obj != NULL && !IS_ERR(obj))
3356                 lfsck_object_put(env, obj);
3357         if (root != NULL && !IS_ERR(root))
3358                 lfsck_object_put(env, root);
3359         if (rc != 0)
3360                 lfsck_instance_cleanup(env, lfsck);
3361         return rc;
3362 }
3363 EXPORT_SYMBOL(lfsck_register);
3364
3365 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3366 {
3367         struct lfsck_instance *lfsck;
3368
3369         lfsck = lfsck_instance_find(key, false, true);
3370         if (lfsck != NULL)
3371                 lfsck_instance_put(env, lfsck);
3372 }
3373 EXPORT_SYMBOL(lfsck_degister);
3374
3375 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3376                      struct dt_device *tgt, struct obd_export *exp,
3377                      __u32 index, bool for_ost)
3378 {
3379         struct lfsck_instance   *lfsck;
3380         struct lfsck_tgt_desc   *ltd;
3381         int                      rc;
3382         ENTRY;
3383
3384         OBD_ALLOC_PTR(ltd);
3385         if (ltd == NULL)
3386                 RETURN(-ENOMEM);
3387
3388         ltd->ltd_tgt = tgt;
3389         ltd->ltd_key = key;
3390         ltd->ltd_exp = exp;
3391         INIT_LIST_HEAD(&ltd->ltd_orphan_list);
3392         INIT_LIST_HEAD(&ltd->ltd_layout_list);
3393         INIT_LIST_HEAD(&ltd->ltd_layout_phase_list);
3394         INIT_LIST_HEAD(&ltd->ltd_namespace_list);
3395         INIT_LIST_HEAD(&ltd->ltd_namespace_phase_list);
3396         atomic_set(&ltd->ltd_ref, 1);
3397         ltd->ltd_index = index;
3398
3399         spin_lock(&lfsck_instance_lock);
3400         lfsck = __lfsck_instance_find(key, true, false);
3401         if (lfsck == NULL) {
3402                 if (for_ost)
3403                         list_add_tail(&ltd->ltd_orphan_list,
3404                                       &lfsck_ost_orphan_list);
3405                 else
3406                         list_add_tail(&ltd->ltd_orphan_list,
3407                                       &lfsck_mdt_orphan_list);
3408                 spin_unlock(&lfsck_instance_lock);
3409
3410                 RETURN(0);
3411         }
3412         spin_unlock(&lfsck_instance_lock);
3413
3414         rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3415         if (rc != 0)
3416                 lfsck_tgt_put(ltd);
3417
3418         lfsck_instance_put(env, lfsck);
3419
3420         RETURN(rc);
3421 }
3422 EXPORT_SYMBOL(lfsck_add_target);
3423
3424 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3425                       struct dt_device *tgt, __u32 index, bool for_ost)
3426 {
3427         struct lfsck_instance   *lfsck;
3428         struct lfsck_tgt_descs  *ltds;
3429         struct lfsck_tgt_desc   *ltd;
3430         struct list_head        *head;
3431
3432         if (for_ost)
3433                 head = &lfsck_ost_orphan_list;
3434         else
3435                 head = &lfsck_mdt_orphan_list;
3436
3437         spin_lock(&lfsck_instance_lock);
3438         list_for_each_entry(ltd, head, ltd_orphan_list) {
3439                 if (ltd->ltd_tgt == tgt) {
3440                         list_del_init(&ltd->ltd_orphan_list);
3441                         spin_unlock(&lfsck_instance_lock);
3442                         lfsck_tgt_put(ltd);
3443
3444                         return;
3445                 }
3446         }
3447
3448         ltd = NULL;
3449         lfsck = __lfsck_instance_find(key, true, false);
3450         spin_unlock(&lfsck_instance_lock);
3451         if (unlikely(lfsck == NULL))
3452                 return;
3453
3454         if (for_ost)
3455                 ltds = &lfsck->li_ost_descs;
3456         else
3457                 ltds = &lfsck->li_mdt_descs;
3458
3459         down_write(&ltds->ltd_rw_sem);
3460         LASSERT(ltds->ltd_tgts_bitmap != NULL);
3461
3462         if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3463                 goto unlock;
3464
3465         ltd = LTD_TGT(ltds, index);
3466         if (unlikely(ltd == NULL))
3467                 goto unlock;
3468
3469         LASSERT(ltds->ltd_tgtnr > 0);
3470
3471         ltds->ltd_tgtnr--;
3472         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3473         LTD_TGT(ltds, index) = NULL;
3474
3475 unlock:
3476         if (ltd == NULL) {
3477                 if (for_ost)
3478                         head = &lfsck->li_ost_descs.ltd_orphan;
3479                 else
3480                         head = &lfsck->li_mdt_descs.ltd_orphan;
3481
3482                 list_for_each_entry(ltd, head, ltd_orphan_list) {
3483                         if (ltd->ltd_tgt == tgt) {
3484                                 list_del_init(&ltd->ltd_orphan_list);
3485                                 break;
3486                         }
3487                 }
3488         }
3489
3490         up_write(&ltds->ltd_rw_sem);
3491         if (ltd != NULL) {
3492                 spin_lock(&ltds->ltd_lock);
3493                 ltd->ltd_dead = 1;
3494                 spin_unlock(&ltds->ltd_lock);
3495                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3496                 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3497                 lfsck_tgt_put(ltd);
3498         }
3499
3500         lfsck_instance_put(env, lfsck);
3501 }
3502 EXPORT_SYMBOL(lfsck_del_target);
3503
3504 static int __init lfsck_init(void)
3505 {
3506         int rc;
3507
3508         INIT_LIST_HEAD(&lfsck_instance_list);
3509         INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3510         INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3511         lfsck_key_init_generic(&lfsck_thread_key, NULL);
3512         rc = lu_context_key_register(&lfsck_thread_key);
3513         if (rc == 0) {
3514                 tgt_register_lfsck_in_notify(lfsck_in_notify);
3515                 tgt_register_lfsck_query(lfsck_query);
3516         }
3517
3518         return rc;
3519 }
3520
3521 static void __exit lfsck_exit(void)
3522 {
3523         struct lfsck_tgt_desc *ltd;
3524         struct lfsck_tgt_desc *next;
3525
3526         LASSERT(list_empty(&lfsck_instance_list));
3527
3528         list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3529                                  ltd_orphan_list) {
3530                 list_del_init(&ltd->ltd_orphan_list);
3531                 lfsck_tgt_put(ltd);
3532         }
3533
3534         list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3535                                  ltd_orphan_list) {
3536                 list_del_init(&ltd->ltd_orphan_list);
3537                 lfsck_tgt_put(ltd);
3538         }
3539
3540         lu_context_key_degister(&lfsck_thread_key);
3541 }
3542
3543 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3544 MODULE_DESCRIPTION("LFSCK");
3545 MODULE_LICENSE("GPL");
3546
3547 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);