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