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