Whamcloud - gitweb
LU-4972 lfsck: skip .lustre and children for namespace check
[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) 2012, 2013, 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 thread key */
46 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
47
48 static void lfsck_key_fini(const struct lu_context *ctx,
49                            struct lu_context_key *key, void *data)
50 {
51         struct lfsck_thread_info *info = data;
52
53         lu_buf_free(&info->lti_linkea_buf);
54         lu_buf_free(&info->lti_big_buf);
55         OBD_FREE_PTR(info);
56 }
57
58 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
59 LU_KEY_INIT_GENERIC(lfsck);
60
61 static CFS_LIST_HEAD(lfsck_instance_list);
62 static struct list_head lfsck_ost_orphan_list;
63 static struct list_head lfsck_mdt_orphan_list;
64 static DEFINE_SPINLOCK(lfsck_instance_lock);
65
66 static const char *lfsck_status_names[] = {
67         [LS_INIT]               = "init",
68         [LS_SCANNING_PHASE1]    = "scanning-phase1",
69         [LS_SCANNING_PHASE2]    = "scanning-phase2",
70         [LS_COMPLETED]          = "completed",
71         [LS_FAILED]             = "failed",
72         [LS_STOPPED]            = "stopped",
73         [LS_PAUSED]             = "paused",
74         [LS_CRASHED]            = "crashed",
75         [LS_PARTIAL]            = "partial",
76         [LS_CO_FAILED]          = "co-failed",
77         [LS_CO_STOPPED]         = "co-stopped",
78         [LS_CO_PAUSED]          = "co-paused"
79 };
80
81 const char *lfsck_flags_names[] = {
82         "scanned-once",
83         "inconsistent",
84         "upgrade",
85         "incomplete",
86         "crashed_lastid",
87         NULL
88 };
89
90 const char *lfsck_param_names[] = {
91         NULL,
92         "failout",
93         "dryrun",
94         "all_targets",
95         "broadcast",
96         "orphan",
97         "create_ostobj",
98         NULL
99 };
100
101 const char *lfsck_status2names(enum lfsck_status status)
102 {
103         if (unlikely(status < 0 || status >= LS_MAX))
104                 return "unknown";
105
106         return lfsck_status_names[status];
107 }
108
109 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
110 {
111         spin_lock_init(&ltds->ltd_lock);
112         init_rwsem(&ltds->ltd_rw_sem);
113         INIT_LIST_HEAD(&ltds->ltd_orphan);
114         ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
115         if (ltds->ltd_tgts_bitmap == NULL)
116                 return -ENOMEM;
117
118         return 0;
119 }
120
121 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
122 {
123         struct lfsck_tgt_desc   *ltd;
124         struct lfsck_tgt_desc   *next;
125         int                      idx;
126
127         down_write(&ltds->ltd_rw_sem);
128
129         list_for_each_entry_safe(ltd, next, &ltds->ltd_orphan,
130                                  ltd_orphan_list) {
131                 list_del_init(&ltd->ltd_orphan_list);
132                 lfsck_tgt_put(ltd);
133         }
134
135         if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
136                 up_write(&ltds->ltd_rw_sem);
137
138                 return;
139         }
140
141         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
142                 ltd = LTD_TGT(ltds, idx);
143                 if (likely(ltd != NULL)) {
144                         LASSERT(list_empty(&ltd->ltd_layout_list));
145                         LASSERT(list_empty(&ltd->ltd_layout_phase_list));
146
147                         ltds->ltd_tgtnr--;
148                         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
149                         LTD_TGT(ltds, idx) = NULL;
150                         lfsck_tgt_put(ltd);
151                 }
152         }
153
154         LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
155                  ltds->ltd_tgtnr);
156
157         for (idx = 0; idx < TGT_PTRS; idx++) {
158                 if (ltds->ltd_tgts_idx[idx] != NULL) {
159                         OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
160                         ltds->ltd_tgts_idx[idx] = NULL;
161                 }
162         }
163
164         CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
165         ltds->ltd_tgts_bitmap = NULL;
166         up_write(&ltds->ltd_rw_sem);
167 }
168
169 static int __lfsck_add_target(const struct lu_env *env,
170                               struct lfsck_instance *lfsck,
171                               struct lfsck_tgt_desc *ltd,
172                               bool for_ost, bool locked)
173 {
174         struct lfsck_tgt_descs *ltds;
175         __u32                   index = ltd->ltd_index;
176         int                     rc    = 0;
177         ENTRY;
178
179         if (for_ost)
180                 ltds = &lfsck->li_ost_descs;
181         else
182                 ltds = &lfsck->li_mdt_descs;
183
184         if (!locked)
185                 down_write(&ltds->ltd_rw_sem);
186
187         LASSERT(ltds->ltd_tgts_bitmap != NULL);
188
189         if (index >= ltds->ltd_tgts_bitmap->size) {
190                 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
191                                     (__u32)BITS_PER_LONG);
192                 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
193                 cfs_bitmap_t *new_bitmap;
194
195                 while (newsize < index + 1)
196                         newsize <<= 1;
197
198                 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
199                 if (new_bitmap == NULL)
200                         GOTO(unlock, rc = -ENOMEM);
201
202                 if (ltds->ltd_tgtnr > 0)
203                         cfs_bitmap_copy(new_bitmap, old_bitmap);
204                 ltds->ltd_tgts_bitmap = new_bitmap;
205                 CFS_FREE_BITMAP(old_bitmap);
206         }
207
208         if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
209                 CERROR("%s: the device %s (%u) is registered already\n",
210                        lfsck_lfsck2name(lfsck),
211                        ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
212                 GOTO(unlock, rc = -EEXIST);
213         }
214
215         if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
216                 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
217                 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
218                         GOTO(unlock, rc = -ENOMEM);
219         }
220
221         LTD_TGT(ltds, index) = ltd;
222         cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
223         ltds->ltd_tgtnr++;
224
225         GOTO(unlock, rc = 0);
226
227 unlock:
228         if (!locked)
229                 up_write(&ltds->ltd_rw_sem);
230
231         return rc;
232 }
233
234 static int lfsck_add_target_from_orphan(const struct lu_env *env,
235                                         struct lfsck_instance *lfsck)
236 {
237         struct lfsck_tgt_descs  *ltds    = &lfsck->li_ost_descs;
238         struct lfsck_tgt_desc   *ltd;
239         struct lfsck_tgt_desc   *next;
240         struct list_head        *head    = &lfsck_ost_orphan_list;
241         int                      rc;
242         bool                     for_ost = true;
243
244 again:
245         spin_lock(&lfsck_instance_lock);
246         list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
247                 if (ltd->ltd_key == lfsck->li_bottom) {
248                         list_del_init(&ltd->ltd_orphan_list);
249                         list_add_tail(&ltd->ltd_orphan_list,
250                                       &ltds->ltd_orphan);
251                 }
252         }
253         spin_unlock(&lfsck_instance_lock);
254
255         down_write(&ltds->ltd_rw_sem);
256         while (!list_empty(&ltds->ltd_orphan)) {
257                 ltd = list_entry(ltds->ltd_orphan.next,
258                                  struct lfsck_tgt_desc,
259                                  ltd_orphan_list);
260                 list_del_init(&ltd->ltd_orphan_list);
261                 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
262                 /* Do not hold the semaphore for too long time. */
263                 up_write(&ltds->ltd_rw_sem);
264                 if (rc != 0)
265                         return rc;
266
267                 down_write(&ltds->ltd_rw_sem);
268         }
269         up_write(&ltds->ltd_rw_sem);
270
271         if (for_ost) {
272                 ltds = &lfsck->li_mdt_descs;
273                 head = &lfsck_mdt_orphan_list;
274                 for_ost = false;
275                 goto again;
276         }
277
278         return 0;
279 }
280
281 static inline struct lfsck_component *
282 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type, cfs_list_t *list)
283 {
284         struct lfsck_component *com;
285
286         cfs_list_for_each_entry(com, list, lc_link) {
287                 if (com->lc_type == type)
288                         return com;
289         }
290         return NULL;
291 }
292
293 struct lfsck_component *
294 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
295 {
296         struct lfsck_component *com;
297
298         spin_lock(&lfsck->li_lock);
299         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
300         if (com != NULL)
301                 goto unlock;
302
303         com = __lfsck_component_find(lfsck, type,
304                                      &lfsck->li_list_double_scan);
305         if (com != NULL)
306                 goto unlock;
307
308         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
309
310 unlock:
311         if (com != NULL)
312                 lfsck_component_get(com);
313         spin_unlock(&lfsck->li_lock);
314         return com;
315 }
316
317 void lfsck_component_cleanup(const struct lu_env *env,
318                              struct lfsck_component *com)
319 {
320         if (!cfs_list_empty(&com->lc_link))
321                 cfs_list_del_init(&com->lc_link);
322         if (!cfs_list_empty(&com->lc_link_dir))
323                 cfs_list_del_init(&com->lc_link_dir);
324
325         lfsck_component_put(env, com);
326 }
327
328 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
329                     struct lu_fid *fid, bool locked)
330 {
331         struct lfsck_bookmark   *bk = &lfsck->li_bookmark_ram;
332         int                      rc = 0;
333         ENTRY;
334
335         if (!locked)
336                 mutex_lock(&lfsck->li_mutex);
337
338         rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
339         if (rc >= 0) {
340                 bk->lb_last_fid = *fid;
341                 /* We do not care about whether the subsequent sub-operations
342                  * failed or not. The worst case is that one FID is lost that
343                  * is not a big issue for the LFSCK since it is relative rare
344                  * for LFSCK create. */
345                 rc = lfsck_bookmark_store(env, lfsck);
346         }
347
348         if (!locked)
349                 mutex_unlock(&lfsck->li_mutex);
350
351         RETURN(rc);
352 }
353
354 static const char dot[] = ".";
355 static const char dotdot[] = "..";
356
357 static int lfsck_create_lpf_local(const struct lu_env *env,
358                                   struct lfsck_instance *lfsck,
359                                   struct dt_object *parent,
360                                   struct dt_object *child,
361                                   struct lu_attr *la,
362                                   struct dt_object_format *dof,
363                                   const char *name)
364 {
365         struct dt_device        *dev    = lfsck->li_bottom;
366         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
367         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
368         const struct lu_fid     *cfid   = lu_object_fid(&child->do_lu);
369         struct thandle          *th     = NULL;
370         loff_t                   pos    = 0;
371         int                      len    = sizeof(struct lfsck_bookmark);
372         int                      rc     = 0;
373         ENTRY;
374
375         th = dt_trans_create(env, dev);
376         if (IS_ERR(th))
377                 RETURN(PTR_ERR(th));
378
379         /* 1a. create child */
380         rc = dt_declare_create(env, child, la, NULL, dof, th);
381         if (rc != 0)
382                 GOTO(stop, rc);
383
384         /* 2a. increase child nlink */
385         rc = dt_declare_ref_add(env, child, th);
386         if (rc != 0)
387                 GOTO(stop, rc);
388
389         /* 3a. insert name into parent dir */
390         rc = dt_declare_insert(env, parent, (const struct dt_rec *)cfid,
391                                (const struct dt_key *)name, th);
392         if (rc != 0)
393                 GOTO(stop, rc);
394
395         /* 4a. increase parent nlink */
396         rc = dt_declare_ref_add(env, parent, th);
397         if (rc != 0)
398                 GOTO(stop, rc);
399
400         /* 5a. update bookmark */
401         rc = dt_declare_record_write(env, bk_obj,
402                                      lfsck_buf_get(env, bk, len), 0, th);
403         if (rc != 0)
404                 GOTO(stop, rc);
405
406         rc = dt_trans_start_local(env, dev, th);
407         if (rc != 0)
408                 GOTO(stop, rc);
409
410         dt_write_lock(env, child, 0);
411         /* 1b.1 create child */
412         rc = dt_create(env, child, la, NULL, dof, th);
413         if (rc != 0)
414                 GOTO(unlock, rc);
415
416         if (unlikely(!dt_try_as_dir(env, child)))
417                 GOTO(unlock, rc = -ENOTDIR);
418
419         /* 1b.2 insert dot into child dir */
420         rc = dt_insert(env, child, (const struct dt_rec *)cfid,
421                        (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
422         if (rc != 0)
423                 GOTO(unlock, rc);
424
425         /* 1b.3 insert dotdot into child dir */
426         rc = dt_insert(env, child, (const struct dt_rec *)&LU_LPF_FID,
427                        (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
428         if (rc != 0)
429                 GOTO(unlock, rc);
430
431         /* 2b. increase child nlink */
432         rc = dt_ref_add(env, child, th);
433         dt_write_unlock(env, child);
434         if (rc != 0)
435                 GOTO(stop, rc);
436
437         /* 3b. insert name into parent dir */
438         rc = dt_insert(env, parent, (const struct dt_rec *)cfid,
439                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
440         if (rc != 0)
441                 GOTO(stop, rc);
442
443         dt_write_lock(env, parent, 0);
444         /* 4b. increase parent nlink */
445         rc = dt_ref_add(env, parent, th);
446         dt_write_unlock(env, parent);
447         if (rc != 0)
448                 GOTO(stop, rc);
449
450         bk->lb_lpf_fid = *cfid;
451         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
452
453         /* 5b. update bookmark */
454         rc = dt_record_write(env, bk_obj,
455                              lfsck_buf_get(env, bk, len), &pos, th);
456
457         GOTO(stop, rc);
458
459 unlock:
460         dt_write_unlock(env, child);
461
462 stop:
463         dt_trans_stop(env, dev, th);
464
465         return rc;
466 }
467
468 static int lfsck_create_lpf_remote(const struct lu_env *env,
469                                    struct lfsck_instance *lfsck,
470                                    struct dt_object *parent,
471                                    struct dt_object *child,
472                                    struct lu_attr *la,
473                                    struct dt_object_format *dof,
474                                    const char *name)
475 {
476         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
477         struct dt_object        *bk_obj = lfsck->li_bookmark_obj;
478         const struct lu_fid     *cfid   = lu_object_fid(&child->do_lu);
479         struct thandle          *th     = NULL;
480         struct dt_device        *dev;
481         loff_t                   pos    = 0;
482         int                      len    = sizeof(struct lfsck_bookmark);
483         int                      rc     = 0;
484         ENTRY;
485
486         /* Create .lustre/lost+found/MDTxxxx. */
487
488         /* XXX: Currently, cross-MDT create operation needs to create the child
489          *      object firstly, then insert name into the parent directory. For
490          *      this case, the child object resides on current MDT (local), but
491          *      the parent ".lustre/lost+found" may be on remote MDT. It is not
492          *      easy to contain all the sub-modifications orderly within single
493          *      transaction.
494          *
495          *      To avoid more inconsistency, we split the create operation into
496          *      two transactions:
497          *
498          *      1) create the child locally.
499          *      2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
500          *         remotely and update the lfsck_bookmark::lb_lpf_fid locally.
501          *
502          *      If 1) done but 2) failed, then the worst case is that we lose
503          *      one object locally, which is not a big issue. (can be repaird
504          *      by LFSCK phase III) */
505
506         /* Transaction I: */
507
508         dev = lfsck->li_bottom;
509         th = dt_trans_create(env, dev);
510         if (IS_ERR(th))
511                 RETURN(PTR_ERR(th));
512
513         /* 1a. create child locally. */
514         rc = dt_declare_create(env, child, la, NULL, dof, th);
515         if (rc != 0)
516                 GOTO(stop, rc);
517
518         /* 2a. increase child nlink locally. */
519         rc = dt_declare_ref_add(env, child, th);
520         if (rc != 0)
521                 GOTO(stop, rc);
522
523         rc = dt_trans_start_local(env, dev, th);
524         if (rc != 0)
525                 GOTO(stop, rc);
526
527         dt_write_lock(env, child, 0);
528         /* 1b. create child locally. */
529         rc = dt_create(env, child, la, NULL, dof, th);
530         if (rc != 0)
531                 GOTO(unlock, rc);
532
533         if (unlikely(!dt_try_as_dir(env, child)))
534                 GOTO(unlock, rc = -ENOTDIR);
535
536         /* 2b.1 insert dot into child dir locally. */
537         rc = dt_insert(env, child, (const struct dt_rec *)cfid,
538                        (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
539         if (rc != 0)
540                 GOTO(unlock, rc);
541
542         /* 2b.2 insert dotdot into child dir locally. */
543         rc = dt_insert(env, child, (const struct dt_rec *)&LU_LPF_FID,
544                        (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
545         if (rc != 0)
546                 GOTO(unlock, rc);
547
548         /* 2b.3 increase child nlink locally. */
549         rc = dt_ref_add(env, child, th);
550         dt_write_unlock(env, child);
551         dt_trans_stop(env, dev, th);
552         if (rc != 0)
553                 RETURN(rc);
554
555         /* Transaction II: */
556
557         dev = lfsck->li_next;
558         th = dt_trans_create(env, dev);
559         if (IS_ERR(th))
560                 RETURN(PTR_ERR(th));
561
562         /* 3a. insert name into parent dir remotely. */
563         rc = dt_declare_insert(env, parent, (const struct dt_rec *)cfid,
564                                (const struct dt_key *)name, th);
565         if (rc != 0)
566                 GOTO(stop, rc);
567
568         /* 4a. increase parent nlink remotely. */
569         rc = dt_declare_ref_add(env, parent, th);
570         if (rc != 0)
571                 GOTO(stop, rc);
572
573         /* 5a. decrease child nlink for dotdot locally if former remote
574          *     update failed. */
575         rc = dt_declare_ref_del(env, child, th);
576         if (rc != 0)
577                 GOTO(stop, rc);
578
579         /* 6a. decrease child nlink for dot locally if former remote
580          *     update failed. */
581         rc = dt_declare_ref_del(env, child, th);
582         if (rc != 0)
583                 GOTO(stop, rc);
584
585         /* 7a. destroy child locally if former remote update failed. */
586         rc = dt_declare_destroy(env, child, th);
587         if (rc != 0)
588                 GOTO(stop, rc);
589
590         /* 8a. update bookmark locally. */
591         rc = dt_declare_record_write(env, bk_obj,
592                                      lfsck_buf_get(env, bk, len), 0, th);
593         if (rc != 0)
594                 GOTO(stop, rc);
595
596         rc = dt_trans_start(env, dev, th);
597         if (rc != 0)
598                 GOTO(stop, rc);
599
600         /* 3b. insert name into parent dir remotely. */
601         rc = dt_insert(env, parent, (const struct dt_rec *)cfid,
602                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
603         if (rc == 0) {
604                 dt_write_lock(env, parent, 0);
605                 /* 4b. increase parent nlink remotely. */
606                 rc = dt_ref_add(env, parent, th);
607                 dt_write_unlock(env, parent);
608         }
609         if (rc != 0) {
610                 /* 5b. decrease child nlink for dotdot locally. */
611                 dt_ref_del(env, child, th);
612                 /* 6b. decrease child nlink for dot locally. */
613                 dt_ref_del(env, child, th);
614                 /* 7b. destroy child locally. */
615                 dt_destroy(env, child, th);
616                 GOTO(stop, rc);
617         }
618
619         bk->lb_lpf_fid = *cfid;
620         lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
621
622         /* 8b. update bookmark locally. */
623         rc = dt_record_write(env, bk_obj,
624                              lfsck_buf_get(env, bk, len), &pos, th);
625
626         GOTO(stop, rc);
627
628 unlock:
629         dt_write_unlock(env, child);
630 stop:
631         dt_trans_stop(env, dev, th);
632
633         return rc;
634 }
635
636 /* Do NOT create .lustre/lost+found/MDTxxxx when register the lfsck instance,
637  * because the MDT0 maybe not reaady for sequence allocation yet. We do that
638  * only when it is required, such as orphan OST-objects repairing. */
639 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
640 {
641         struct lfsck_bookmark    *bk    = &lfsck->li_bookmark_ram;
642         struct lfsck_thread_info *info  = lfsck_env_info(env);
643         struct lu_fid            *cfid  = &info->lti_fid2;
644         struct lu_attr           *la    = &info->lti_la;
645         struct dt_object_format  *dof   = &info->lti_dof;
646         struct dt_object         *parent = NULL;
647         struct dt_object         *child = NULL;
648         char                      name[8];
649         int                       node  = lfsck_dev_idx(lfsck->li_bottom);
650         int                       rc    = 0;
651         ENTRY;
652
653         LASSERT(lfsck->li_master);
654
655         sprintf(name, "MDT%04x", node);
656         if (node == 0) {
657                 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
658                                                   &LU_LPF_FID);
659         } else {
660                 struct lfsck_tgt_desc *ltd;
661
662                 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
663                 if (unlikely(ltd == NULL))
664                         RETURN(-ENXIO);
665
666                 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
667                                                   &LU_LPF_FID);
668                 lfsck_tgt_put(ltd);
669         }
670         if (IS_ERR(parent))
671                 RETURN(PTR_ERR(parent));
672
673         if (unlikely(!dt_try_as_dir(env, parent)))
674                 GOTO(out, rc = -ENOTDIR);
675
676         mutex_lock(&lfsck->li_mutex);
677         if (lfsck->li_lpf_obj != NULL)
678                 GOTO(unlock, rc = 0);
679
680         if (fid_is_zero(&bk->lb_lpf_fid)) {
681                 /* There is corner case that: in former LFSCK scanning we have
682                  * created the .lustre/lost+found/MDTxxxx but failed to update
683                  * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
684                  * it from MDT0 firstly. */
685                 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
686                                (const struct dt_key *)name, BYPASS_CAPA);
687                 if (rc != 0 && rc != -ENOENT)
688                         GOTO(unlock, rc);
689
690                 if (rc == 0) {
691                         bk->lb_lpf_fid = *cfid;
692                         rc = lfsck_bookmark_store(env, lfsck);
693                 } else {
694                         rc = lfsck_fid_alloc(env, lfsck, cfid, true);
695                 }
696                 if (rc != 0)
697                         GOTO(unlock, rc);
698         } else {
699                 *cfid = bk->lb_lpf_fid;
700         }
701
702         child = lfsck_object_find_by_dev(env, lfsck->li_bottom, cfid);
703         if (IS_ERR(child))
704                 GOTO(unlock, rc = PTR_ERR(child));
705
706         if (dt_object_exists(child) != 0) {
707                 if (unlikely(!dt_try_as_dir(env, child)))
708                         GOTO(unlock, rc = -ENOTDIR);
709
710                 lfsck->li_lpf_obj = child;
711                 GOTO(unlock, rc = 0);
712         }
713
714         memset(la, 0, sizeof(*la));
715         la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
716         la->la_mode = S_IFDIR | S_IRWXU;
717         la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
718                        LA_UID | LA_GID;
719         memset(dof, 0, sizeof(*dof));
720         dof->dof_type = dt_mode_to_dft(S_IFDIR);
721
722         if (node == 0)
723                 rc = lfsck_create_lpf_local(env, lfsck, parent, child, la,
724                                             dof, name);
725         else
726                 rc = lfsck_create_lpf_remote(env, lfsck, parent, child, la,
727                                              dof, name);
728         if (rc == 0)
729                 lfsck->li_lpf_obj = child;
730
731         GOTO(unlock, rc);
732
733 unlock:
734         mutex_unlock(&lfsck->li_mutex);
735         if (rc != 0 && child != NULL && !IS_ERR(child))
736                 lu_object_put(env, &child->do_lu);
737 out:
738         if (parent != NULL && !IS_ERR(parent))
739                 lu_object_put(env, &parent->do_lu);
740
741         return rc;
742 }
743
744 static int lfsck_fid_init(struct lfsck_instance *lfsck)
745 {
746         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
747         struct seq_server_site  *ss;
748         char                    *prefix;
749         int                      rc     = 0;
750         ENTRY;
751
752         ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
753         if (unlikely(ss == NULL))
754                 RETURN(-ENXIO);
755
756         OBD_ALLOC_PTR(lfsck->li_seq);
757         if (lfsck->li_seq == NULL)
758                 RETURN(-ENOMEM);
759
760         OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
761         if (prefix == NULL)
762                 GOTO(out, rc = -ENOMEM);
763
764         snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
765         rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
766                              ss->ss_server_seq);
767         OBD_FREE(prefix, MAX_OBD_NAME + 7);
768         if (rc != 0)
769                 GOTO(out, rc);
770
771         if (fid_is_sane(&bk->lb_last_fid))
772                 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
773
774         RETURN(0);
775
776 out:
777         OBD_FREE_PTR(lfsck->li_seq);
778         lfsck->li_seq = NULL;
779
780         return rc;
781 }
782
783 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
784 {
785         if (lfsck->li_seq != NULL) {
786                 seq_client_fini(lfsck->li_seq);
787                 OBD_FREE_PTR(lfsck->li_seq);
788                 lfsck->li_seq = NULL;
789         }
790 }
791
792 void lfsck_instance_cleanup(const struct lu_env *env,
793                             struct lfsck_instance *lfsck)
794 {
795         struct ptlrpc_thread    *thread = &lfsck->li_thread;
796         struct lfsck_component  *com;
797         ENTRY;
798
799         LASSERT(list_empty(&lfsck->li_link));
800         LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
801
802         if (lfsck->li_obj_oit != NULL) {
803                 lu_object_put_nocache(env, &lfsck->li_obj_oit->do_lu);
804                 lfsck->li_obj_oit = NULL;
805         }
806
807         LASSERT(lfsck->li_obj_dir == NULL);
808
809         while (!cfs_list_empty(&lfsck->li_list_scan)) {
810                 com = cfs_list_entry(lfsck->li_list_scan.next,
811                                      struct lfsck_component,
812                                      lc_link);
813                 lfsck_component_cleanup(env, com);
814         }
815
816         LASSERT(cfs_list_empty(&lfsck->li_list_dir));
817
818         while (!cfs_list_empty(&lfsck->li_list_double_scan)) {
819                 com = cfs_list_entry(lfsck->li_list_double_scan.next,
820                                      struct lfsck_component,
821                                      lc_link);
822                 lfsck_component_cleanup(env, com);
823         }
824
825         while (!cfs_list_empty(&lfsck->li_list_idle)) {
826                 com = cfs_list_entry(lfsck->li_list_idle.next,
827                                      struct lfsck_component,
828                                      lc_link);
829                 lfsck_component_cleanup(env, com);
830         }
831
832         lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
833         lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
834
835         if (lfsck->li_bookmark_obj != NULL) {
836                 lu_object_put_nocache(env, &lfsck->li_bookmark_obj->do_lu);
837                 lfsck->li_bookmark_obj = NULL;
838         }
839
840         if (lfsck->li_lpf_obj != NULL) {
841                 lu_object_put(env, &lfsck->li_lpf_obj->do_lu);
842                 lfsck->li_lpf_obj = NULL;
843         }
844
845         if (lfsck->li_los != NULL) {
846                 local_oid_storage_fini(env, lfsck->li_los);
847                 lfsck->li_los = NULL;
848         }
849
850         lfsck_fid_fini(lfsck);
851
852         OBD_FREE_PTR(lfsck);
853 }
854
855 static inline struct lfsck_instance *
856 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
857 {
858         struct lfsck_instance *lfsck;
859
860         cfs_list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
861                 if (lfsck->li_bottom == key) {
862                         if (ref)
863                                 lfsck_instance_get(lfsck);
864                         if (unlink)
865                                 list_del_init(&lfsck->li_link);
866
867                         return lfsck;
868                 }
869         }
870
871         return NULL;
872 }
873
874 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
875                                            bool unlink)
876 {
877         struct lfsck_instance *lfsck;
878
879         spin_lock(&lfsck_instance_lock);
880         lfsck = __lfsck_instance_find(key, ref, unlink);
881         spin_unlock(&lfsck_instance_lock);
882
883         return lfsck;
884 }
885
886 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
887 {
888         struct lfsck_instance *tmp;
889
890         spin_lock(&lfsck_instance_lock);
891         cfs_list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
892                 if (lfsck->li_bottom == tmp->li_bottom) {
893                         spin_unlock(&lfsck_instance_lock);
894                         return -EEXIST;
895                 }
896         }
897
898         cfs_list_add_tail(&lfsck->li_link, &lfsck_instance_list);
899         spin_unlock(&lfsck_instance_lock);
900         return 0;
901 }
902
903 int lfsck_bits_dump(char **buf, int *len, int bits, const char *names[],
904                     const char *prefix)
905 {
906         int save = *len;
907         int flag;
908         int rc;
909         int i;
910         bool newline = (bits != 0 ? false : true);
911
912         rc = snprintf(*buf, *len, "%s:%c", prefix, newline ? '\n' : ' ');
913         if (rc <= 0)
914                 return -ENOSPC;
915
916         *buf += rc;
917         *len -= rc;
918         for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
919                 if (flag & bits) {
920                         bits &= ~flag;
921                         if (names[i] != NULL) {
922                                 if (bits == 0)
923                                         newline = true;
924
925                                 rc = snprintf(*buf, *len, "%s%c", names[i],
926                                               newline ? '\n' : ',');
927                                 if (rc <= 0)
928                                         return -ENOSPC;
929
930                                 *buf += rc;
931                                 *len -= rc;
932                         }
933                 }
934         }
935
936         if (!newline) {
937                 rc = snprintf(*buf, *len, "\n");
938                 if (rc <= 0)
939                         return -ENOSPC;
940
941                 *buf += rc;
942                 *len -= rc;
943         }
944
945         return save - *len;
946 }
947
948 int lfsck_time_dump(char **buf, int *len, __u64 time, const char *prefix)
949 {
950         int rc;
951
952         if (time != 0)
953                 rc = snprintf(*buf, *len, "%s: "LPU64" seconds\n", prefix,
954                               cfs_time_current_sec() - time);
955         else
956                 rc = snprintf(*buf, *len, "%s: N/A\n", prefix);
957         if (rc <= 0)
958                 return -ENOSPC;
959
960         *buf += rc;
961         *len -= rc;
962         return rc;
963 }
964
965 int lfsck_pos_dump(char **buf, int *len, struct lfsck_position *pos,
966                    const char *prefix)
967 {
968         int rc;
969
970         if (fid_is_zero(&pos->lp_dir_parent)) {
971                 if (pos->lp_oit_cookie == 0)
972                         rc = snprintf(*buf, *len, "%s: N/A, N/A, N/A\n",
973                                       prefix);
974                 else
975                         rc = snprintf(*buf, *len, "%s: "LPU64", N/A, N/A\n",
976                                       prefix, pos->lp_oit_cookie);
977         } else {
978                 rc = snprintf(*buf, *len, "%s: "LPU64", "DFID", "LPU64"\n",
979                               prefix, pos->lp_oit_cookie,
980                               PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
981         }
982         if (rc <= 0)
983                 return -ENOSPC;
984
985         *buf += rc;
986         *len -= rc;
987         return rc;
988 }
989
990 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
991                     struct lfsck_position *pos, bool init)
992 {
993         const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
994
995         if (unlikely(lfsck->li_di_oit == NULL)) {
996                 memset(pos, 0, sizeof(*pos));
997                 return;
998         }
999
1000         pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1001         if (!lfsck->li_current_oit_processed && !init)
1002                 pos->lp_oit_cookie--;
1003
1004         LASSERT(pos->lp_oit_cookie > 0);
1005
1006         if (lfsck->li_di_dir != NULL) {
1007                 struct dt_object *dto = lfsck->li_obj_dir;
1008
1009                 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1010                                                         lfsck->li_di_dir);
1011
1012                 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1013                         fid_zero(&pos->lp_dir_parent);
1014                         pos->lp_dir_cookie = 0;
1015                 } else {
1016                         pos->lp_dir_parent = *lfsck_dto2fid(dto);
1017                 }
1018         } else {
1019                 fid_zero(&pos->lp_dir_parent);
1020                 pos->lp_dir_cookie = 0;
1021         }
1022 }
1023
1024 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1025 {
1026         bool dirty = false;
1027
1028         if (limit != LFSCK_SPEED_NO_LIMIT) {
1029                 if (limit > HZ) {
1030                         lfsck->li_sleep_rate = limit / HZ;
1031                         lfsck->li_sleep_jif = 1;
1032                 } else {
1033                         lfsck->li_sleep_rate = 1;
1034                         lfsck->li_sleep_jif = HZ / limit;
1035                 }
1036         } else {
1037                 lfsck->li_sleep_jif = 0;
1038                 lfsck->li_sleep_rate = 0;
1039         }
1040
1041         if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1042                 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1043                 dirty = true;
1044         }
1045
1046         return dirty;
1047 }
1048
1049 void lfsck_control_speed(struct lfsck_instance *lfsck)
1050 {
1051         struct ptlrpc_thread *thread = &lfsck->li_thread;
1052         struct l_wait_info    lwi;
1053
1054         if (lfsck->li_sleep_jif > 0 &&
1055             lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1056                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1057                                        LWI_ON_SIGNAL_NOOP, NULL);
1058
1059                 l_wait_event(thread->t_ctl_waitq,
1060                              !thread_is_running(thread),
1061                              &lwi);
1062                 lfsck->li_new_scanned = 0;
1063         }
1064 }
1065
1066 void lfsck_control_speed_by_self(struct lfsck_component *com)
1067 {
1068         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1069         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1070         struct l_wait_info       lwi;
1071
1072         if (lfsck->li_sleep_jif > 0 &&
1073             com->lc_new_scanned >= lfsck->li_sleep_rate) {
1074                 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1075                                        LWI_ON_SIGNAL_NOOP, NULL);
1076
1077                 l_wait_event(thread->t_ctl_waitq,
1078                              !thread_is_running(thread),
1079                              &lwi);
1080                 com->lc_new_scanned = 0;
1081         }
1082 }
1083
1084 static int lfsck_parent_fid(const struct lu_env *env, struct dt_object *obj,
1085                             struct lu_fid *fid)
1086 {
1087         if (unlikely(!S_ISDIR(lfsck_object_type(obj)) ||
1088                      !dt_try_as_dir(env, obj)))
1089                 return -ENOTDIR;
1090
1091         return dt_lookup(env, obj, (struct dt_rec *)fid,
1092                          (const struct dt_key *)"..", BYPASS_CAPA);
1093 }
1094
1095 static int lfsck_needs_scan_dir(const struct lu_env *env,
1096                                 struct lfsck_instance *lfsck,
1097                                 struct dt_object *obj)
1098 {
1099         struct lu_fid *fid   = &lfsck_env_info(env)->lti_fid;
1100         int            depth = 0;
1101         int            rc;
1102
1103         if (!lfsck->li_master || !S_ISDIR(lfsck_object_type(obj)) ||
1104             cfs_list_empty(&lfsck->li_list_dir))
1105                RETURN(0);
1106
1107         while (1) {
1108                 /* XXX: Currently, we do not scan the "/REMOTE_PARENT_DIR",
1109                  *      which is the agent directory to manage the objects
1110                  *      which name entries reside on remote MDTs. Related
1111                  *      consistency verification will be processed in LFSCK
1112                  *      phase III. */
1113                 if (lu_fid_eq(lfsck_dto2fid(obj), &lfsck->li_global_root_fid)) {
1114                         if (depth > 0)
1115                                 lfsck_object_put(env, obj);
1116                         return 1;
1117                 }
1118
1119                 /* No need to check .lustre and its children. */
1120                 if (fid_seq_is_dot_lustre(fid_seq(lfsck_dto2fid(obj)))) {
1121                         if (depth > 0)
1122                                 lfsck_object_put(env, obj);
1123                         return 0;
1124                 }
1125
1126                 dt_read_lock(env, obj, MOR_TGT_CHILD);
1127                 if (unlikely(lfsck_is_dead_obj(obj))) {
1128                         dt_read_unlock(env, obj);
1129                         if (depth > 0)
1130                                 lfsck_object_put(env, obj);
1131                         return 0;
1132                 }
1133
1134                 rc = dt_xattr_get(env, obj,
1135                                   lfsck_buf_get(env, NULL, 0), XATTR_NAME_LINK,
1136                                   BYPASS_CAPA);
1137                 dt_read_unlock(env, obj);
1138                 if (rc >= 0) {
1139                         if (depth > 0)
1140                                 lfsck_object_put(env, obj);
1141                         return 1;
1142                 }
1143
1144                 if (rc < 0 && rc != -ENODATA) {
1145                         if (depth > 0)
1146                                 lfsck_object_put(env, obj);
1147                         return rc;
1148                 }
1149
1150                 rc = lfsck_parent_fid(env, obj, fid);
1151                 if (depth > 0)
1152                         lfsck_object_put(env, obj);
1153                 if (rc != 0)
1154                         return rc;
1155
1156                 if (unlikely(lu_fid_eq(fid, &lfsck->li_local_root_fid)))
1157                         return 0;
1158
1159                 obj = lfsck_object_find(env, lfsck, fid);
1160                 if (obj == NULL)
1161                         return 0;
1162                 else if (IS_ERR(obj))
1163                         return PTR_ERR(obj);
1164
1165                 if (!dt_object_exists(obj)) {
1166                         lfsck_object_put(env, obj);
1167                         return 0;
1168                 }
1169
1170                 if (dt_object_remote(obj)) {
1171                         /* .lustre/lost+found/MDTxxx can be remote directory. */
1172                         if (fid_seq_is_dot_lustre(fid_seq(lfsck_dto2fid(obj))))
1173                                 rc = 0;
1174                         else
1175                                 /* Other remote directory should be client
1176                                  * visible and need to be checked. */
1177                                 rc = 1;
1178                         lfsck_object_put(env, obj);
1179                         return rc;
1180                 }
1181
1182                 depth++;
1183         }
1184         return 0;
1185 }
1186
1187 struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck,
1188                                                  struct lfsck_component *com,
1189                                                  struct lfsck_start_param *lsp)
1190 {
1191         struct lfsck_thread_args *lta;
1192         int                       rc;
1193
1194         OBD_ALLOC_PTR(lta);
1195         if (lta == NULL)
1196                 return ERR_PTR(-ENOMEM);
1197
1198         rc = lu_env_init(&lta->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1199         if (rc != 0) {
1200                 OBD_FREE_PTR(lta);
1201                 return ERR_PTR(rc);
1202         }
1203
1204         lta->lta_lfsck = lfsck_instance_get(lfsck);
1205         if (com != NULL)
1206                 lta->lta_com = lfsck_component_get(com);
1207
1208         lta->lta_lsp = lsp;
1209
1210         return lta;
1211 }
1212
1213 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1214 {
1215         if (lta->lta_com != NULL)
1216                 lfsck_component_put(&lta->lta_env, lta->lta_com);
1217         lfsck_instance_put(&lta->lta_env, lta->lta_lfsck);
1218         lu_env_fini(&lta->lta_env);
1219         OBD_FREE_PTR(lta);
1220 }
1221
1222 /* LFSCK wrap functions */
1223
1224 void lfsck_fail(const struct lu_env *env, struct lfsck_instance *lfsck,
1225                 bool new_checked)
1226 {
1227         struct lfsck_component *com;
1228
1229         cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1230                 com->lc_ops->lfsck_fail(env, com, new_checked);
1231         }
1232 }
1233
1234 int lfsck_checkpoint(const struct lu_env *env, struct lfsck_instance *lfsck)
1235 {
1236         struct lfsck_component *com;
1237         int                     rc  = 0;
1238         int                     rc1 = 0;
1239
1240         if (likely(cfs_time_beforeq(cfs_time_current(),
1241                                     lfsck->li_time_next_checkpoint)))
1242                 return 0;
1243
1244         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_current, false);
1245         cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1246                 rc = com->lc_ops->lfsck_checkpoint(env, com, false);
1247                 if (rc != 0)
1248                         rc1 = rc;
1249         }
1250
1251         lfsck->li_time_last_checkpoint = cfs_time_current();
1252         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
1253                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1254         return rc1 != 0 ? rc1 : rc;
1255 }
1256
1257 int lfsck_prep(const struct lu_env *env, struct lfsck_instance *lfsck,
1258                struct lfsck_start_param *lsp)
1259 {
1260         struct dt_object       *obj     = NULL;
1261         struct lfsck_component *com;
1262         struct lfsck_component *next;
1263         struct lfsck_position  *pos     = NULL;
1264         const struct dt_it_ops *iops    =
1265                                 &lfsck->li_obj_oit->do_index_ops->dio_it;
1266         struct dt_it           *di;
1267         int                     rc;
1268         ENTRY;
1269
1270         LASSERT(lfsck->li_obj_dir == NULL);
1271         LASSERT(lfsck->li_di_dir == NULL);
1272
1273         lfsck->li_current_oit_processed = 0;
1274         cfs_list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1275                 com->lc_new_checked = 0;
1276                 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1277                         com->lc_journal = 0;
1278
1279                 rc = com->lc_ops->lfsck_prep(env, com, lsp);
1280                 if (rc != 0)
1281                         GOTO(out, rc);
1282
1283                 if ((pos == NULL) ||
1284                     (!lfsck_pos_is_zero(&com->lc_pos_start) &&
1285                      lfsck_pos_is_eq(pos, &com->lc_pos_start) > 0))
1286                         pos = &com->lc_pos_start;
1287         }
1288
1289         /* Init otable-based iterator. */
1290         if (pos == NULL) {
1291                 rc = iops->load(env, lfsck->li_di_oit, 0);
1292                 if (rc > 0) {
1293                         lfsck->li_oit_over = 1;
1294                         rc = 0;
1295                 }
1296
1297                 GOTO(out, rc);
1298         }
1299
1300         rc = iops->load(env, lfsck->li_di_oit, pos->lp_oit_cookie);
1301         if (rc < 0)
1302                 GOTO(out, rc);
1303         else if (rc > 0)
1304                 lfsck->li_oit_over = 1;
1305
1306         if (!lfsck->li_master || fid_is_zero(&pos->lp_dir_parent))
1307                 GOTO(out, rc = 0);
1308
1309         /* Find the directory for namespace-based traverse. */
1310         obj = lfsck_object_find(env, lfsck, &pos->lp_dir_parent);
1311         if (obj == NULL)
1312                 GOTO(out, rc = 0);
1313         else if (IS_ERR(obj))
1314                 RETURN(PTR_ERR(obj));
1315
1316         /* XXX: Currently, skip remote object, the consistency for
1317          *      remote object will be processed in LFSCK phase III. */
1318         if (!dt_object_exists(obj) || dt_object_remote(obj) ||
1319             unlikely(!S_ISDIR(lfsck_object_type(obj))))
1320                 GOTO(out, rc = 0);
1321
1322         if (unlikely(!dt_try_as_dir(env, obj)))
1323                 GOTO(out, rc = -ENOTDIR);
1324
1325         /* Init the namespace-based directory traverse. */
1326         iops = &obj->do_index_ops->dio_it;
1327         di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA);
1328         if (IS_ERR(di))
1329                 GOTO(out, rc = PTR_ERR(di));
1330
1331         LASSERT(pos->lp_dir_cookie < MDS_DIR_END_OFF);
1332
1333         rc = iops->load(env, di, pos->lp_dir_cookie);
1334         if ((rc == 0) || (rc > 0 && pos->lp_dir_cookie > 0))
1335                 rc = iops->next(env, di);
1336         else if (rc > 0)
1337                 rc = 0;
1338
1339         if (rc != 0) {
1340                 iops->put(env, di);
1341                 iops->fini(env, di);
1342                 GOTO(out, rc);
1343         }
1344
1345         lfsck->li_obj_dir = lfsck_object_get(obj);
1346         lfsck->li_cookie_dir = iops->store(env, di);
1347         spin_lock(&lfsck->li_lock);
1348         lfsck->li_di_dir = di;
1349         spin_unlock(&lfsck->li_lock);
1350
1351         GOTO(out, rc = 0);
1352
1353 out:
1354         if (obj != NULL)
1355                 lfsck_object_put(env, obj);
1356
1357         if (rc < 0) {
1358                 cfs_list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
1359                                              lc_link)
1360                         com->lc_ops->lfsck_post(env, com, rc, true);
1361
1362                 return rc;
1363         }
1364
1365         rc = 0;
1366         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_current, true);
1367         cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1368                 rc = com->lc_ops->lfsck_checkpoint(env, com, true);
1369                 if (rc != 0)
1370                         break;
1371         }
1372
1373         lfsck->li_time_last_checkpoint = cfs_time_current();
1374         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
1375                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1376         return rc;
1377 }
1378
1379 int lfsck_exec_oit(const struct lu_env *env, struct lfsck_instance *lfsck,
1380                    struct dt_object *obj)
1381 {
1382         struct lfsck_component *com;
1383         const struct dt_it_ops *iops;
1384         struct dt_it           *di;
1385         int                     rc;
1386         ENTRY;
1387
1388         LASSERT(lfsck->li_obj_dir == NULL);
1389
1390         cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1391                 rc = com->lc_ops->lfsck_exec_oit(env, com, obj);
1392                 if (rc != 0)
1393                         RETURN(rc);
1394         }
1395
1396         rc = lfsck_needs_scan_dir(env, lfsck, obj);
1397         if (rc <= 0)
1398                 GOTO(out, rc);
1399
1400         if (unlikely(!dt_try_as_dir(env, obj)))
1401                 GOTO(out, rc = -ENOTDIR);
1402
1403         iops = &obj->do_index_ops->dio_it;
1404         di = iops->init(env, obj, lfsck->li_args_dir, BYPASS_CAPA);
1405         if (IS_ERR(di))
1406                 GOTO(out, rc = PTR_ERR(di));
1407
1408         rc = iops->load(env, di, 0);
1409         if (rc == 0)
1410                 rc = iops->next(env, di);
1411         else if (rc > 0)
1412                 rc = 0;
1413
1414         if (rc != 0) {
1415                 iops->put(env, di);
1416                 iops->fini(env, di);
1417                 GOTO(out, rc);
1418         }
1419
1420         lfsck->li_obj_dir = lfsck_object_get(obj);
1421         lfsck->li_cookie_dir = iops->store(env, di);
1422         spin_lock(&lfsck->li_lock);
1423         lfsck->li_di_dir = di;
1424         spin_unlock(&lfsck->li_lock);
1425
1426         GOTO(out, rc = 0);
1427
1428 out:
1429         if (rc < 0)
1430                 lfsck_fail(env, lfsck, false);
1431         return (rc > 0 ? 0 : rc);
1432 }
1433
1434 int lfsck_exec_dir(const struct lu_env *env, struct lfsck_instance *lfsck,
1435                    struct dt_object *obj, struct lu_dirent *ent)
1436 {
1437         struct lfsck_component *com;
1438         int                     rc;
1439
1440         cfs_list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1441                 rc = com->lc_ops->lfsck_exec_dir(env, com, obj, ent);
1442                 if (rc != 0)
1443                         return rc;
1444         }
1445         return 0;
1446 }
1447
1448 int lfsck_post(const struct lu_env *env, struct lfsck_instance *lfsck,
1449                int result)
1450 {
1451         struct lfsck_component *com;
1452         struct lfsck_component *next;
1453         int                     rc  = 0;
1454         int                     rc1 = 0;
1455
1456         lfsck_pos_fill(env, lfsck, &lfsck->li_pos_current, false);
1457         cfs_list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1458                 rc = com->lc_ops->lfsck_post(env, com, result, false);
1459                 if (rc != 0)
1460                         rc1 = rc;
1461         }
1462
1463         lfsck->li_time_last_checkpoint = cfs_time_current();
1464         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
1465                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1466
1467         /* Ignore some component post failure to make other can go ahead. */
1468         return result;
1469 }
1470
1471 static void lfsck_interpret(const struct lu_env *env,
1472                             struct lfsck_instance *lfsck,
1473                             struct ptlrpc_request *req, void *args, int result)
1474 {
1475         struct lfsck_async_interpret_args *laia = args;
1476         struct lfsck_component            *com;
1477
1478         LASSERT(laia->laia_com == NULL);
1479         LASSERT(laia->laia_shared);
1480
1481         spin_lock(&lfsck->li_lock);
1482         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1483                 if (com->lc_ops->lfsck_interpret != NULL) {
1484                         laia->laia_com = com;
1485                         com->lc_ops->lfsck_interpret(env, req, laia, result);
1486                 }
1487         }
1488
1489         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
1490                 if (com->lc_ops->lfsck_interpret != NULL) {
1491                         laia->laia_com = com;
1492                         com->lc_ops->lfsck_interpret(env, req, laia, result);
1493                 }
1494         }
1495         spin_unlock(&lfsck->li_lock);
1496 }
1497
1498 int lfsck_double_scan(const struct lu_env *env, struct lfsck_instance *lfsck)
1499 {
1500         struct lfsck_component *com;
1501         struct lfsck_component *next;
1502         struct l_wait_info      lwi = { 0 };
1503         int                     rc  = 0;
1504         int                     rc1 = 0;
1505
1506         list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
1507                 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1508                         com->lc_journal = 0;
1509
1510                 rc = com->lc_ops->lfsck_double_scan(env, com);
1511                 if (rc != 0)
1512                         rc1 = rc;
1513         }
1514
1515         l_wait_event(lfsck->li_thread.t_ctl_waitq,
1516                      atomic_read(&lfsck->li_double_scan_count) == 0,
1517                      &lwi);
1518
1519         if (lfsck->li_status != LS_PAUSED &&
1520             lfsck->li_status != LS_CO_PAUSED) {
1521                 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1522                                          lc_link) {
1523                         spin_lock(&lfsck->li_lock);
1524                         list_del_init(&com->lc_link);
1525                         list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1526                         spin_unlock(&lfsck->li_lock);
1527                 }
1528         }
1529
1530         return rc1 != 0 ? rc1 : rc;
1531 }
1532
1533 static int lfsck_stop_notify(const struct lu_env *env,
1534                              struct lfsck_instance *lfsck,
1535                              struct lfsck_tgt_descs *ltds,
1536                              struct lfsck_tgt_desc *ltd, __u16 type)
1537 {
1538         struct ptlrpc_request_set *set;
1539         struct lfsck_component    *com;
1540         int                        rc  = 0;
1541         ENTRY;
1542
1543         spin_lock(&lfsck->li_lock);
1544         com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
1545         if (com == NULL)
1546                 com = __lfsck_component_find(lfsck, type,
1547                                              &lfsck->li_list_double_scan);
1548         if (com != NULL)
1549                 lfsck_component_get(com);
1550         spin_lock(&lfsck->li_lock);
1551
1552         if (com != NULL) {
1553                 if (com->lc_ops->lfsck_stop_notify != NULL) {
1554                         set = ptlrpc_prep_set();
1555                         if (set == NULL) {
1556                                 lfsck_component_put(env, com);
1557
1558                                 RETURN(-ENOMEM);
1559                         }
1560
1561                         rc = com->lc_ops->lfsck_stop_notify(env, com, ltds,
1562                                                             ltd, set);
1563                         if (rc == 0)
1564                                 rc = ptlrpc_set_wait(set);
1565
1566                         ptlrpc_set_destroy(set);
1567                 }
1568
1569                 lfsck_component_put(env, com);
1570         }
1571
1572         RETURN(rc);
1573 }
1574
1575 void lfsck_quit(const struct lu_env *env, struct lfsck_instance *lfsck)
1576 {
1577         struct lfsck_component *com;
1578         struct lfsck_component *next;
1579
1580         list_for_each_entry_safe(com, next, &lfsck->li_list_scan,
1581                                  lc_link) {
1582                 if (com->lc_ops->lfsck_quit != NULL)
1583                         com->lc_ops->lfsck_quit(env, com);
1584
1585                 spin_lock(&lfsck->li_lock);
1586                 list_del_init(&com->lc_link);
1587                 list_del_init(&com->lc_link_dir);
1588                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1589                 spin_unlock(&lfsck->li_lock);
1590         }
1591
1592         list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1593                                  lc_link) {
1594                 if (com->lc_ops->lfsck_quit != NULL)
1595                         com->lc_ops->lfsck_quit(env, com);
1596
1597                 spin_lock(&lfsck->li_lock);
1598                 list_del_init(&com->lc_link);
1599                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1600                 spin_unlock(&lfsck->li_lock);
1601         }
1602 }
1603
1604 static int lfsck_async_interpret(const struct lu_env *env,
1605                                  struct ptlrpc_request *req,
1606                                  void *args, int rc)
1607 {
1608         struct lfsck_async_interpret_args *laia = args;
1609         struct lfsck_instance             *lfsck;
1610
1611         lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
1612                               li_mdt_descs);
1613         lfsck_interpret(env, lfsck, req, laia, rc);
1614         lfsck_tgt_put(laia->laia_ltd);
1615         if (rc != 0 && laia->laia_result != -EALREADY)
1616                 laia->laia_result = rc;
1617
1618         return 0;
1619 }
1620
1621 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
1622                         struct lfsck_request *lr,
1623                         struct ptlrpc_request_set *set,
1624                         ptlrpc_interpterer_t interpreter,
1625                         void *args, int request)
1626 {
1627         struct lfsck_async_interpret_args *laia;
1628         struct ptlrpc_request             *req;
1629         struct lfsck_request              *tmp;
1630         struct req_format                 *format;
1631         int                                rc;
1632
1633         switch (request) {
1634         case LFSCK_NOTIFY:
1635                 format = &RQF_LFSCK_NOTIFY;
1636                 break;
1637         case LFSCK_QUERY:
1638                 format = &RQF_LFSCK_QUERY;
1639                 break;
1640         default:
1641                 CERROR("%s: unknown async request: opc = %d\n",
1642                        exp->exp_obd->obd_name, request);
1643                 return -EINVAL;
1644         }
1645
1646         req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
1647         if (req == NULL)
1648                 return -ENOMEM;
1649
1650         rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
1651         if (rc != 0) {
1652                 ptlrpc_request_free(req);
1653
1654                 return rc;
1655         }
1656
1657         tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1658         *tmp = *lr;
1659         ptlrpc_request_set_replen(req);
1660
1661         laia = ptlrpc_req_async_args(req);
1662         *laia = *(struct lfsck_async_interpret_args *)args;
1663         if (laia->laia_com != NULL)
1664                 lfsck_component_get(laia->laia_com);
1665         req->rq_interpret_reply = interpreter;
1666         ptlrpc_set_add_req(set, req);
1667
1668         return 0;
1669 }
1670
1671 /* external interfaces */
1672
1673 int lfsck_get_speed(struct dt_device *key, void *buf, int len)
1674 {
1675         struct lu_env           env;
1676         struct lfsck_instance  *lfsck;
1677         int                     rc;
1678         ENTRY;
1679
1680         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1681         if (rc != 0)
1682                 RETURN(rc);
1683
1684         lfsck = lfsck_instance_find(key, true, false);
1685         if (likely(lfsck != NULL)) {
1686                 rc = snprintf(buf, len, "%u\n",
1687                               lfsck->li_bookmark_ram.lb_speed_limit);
1688                 lfsck_instance_put(&env, lfsck);
1689         } else {
1690                 rc = -ENXIO;
1691         }
1692
1693         lu_env_fini(&env);
1694
1695         RETURN(rc);
1696 }
1697 EXPORT_SYMBOL(lfsck_get_speed);
1698
1699 int lfsck_set_speed(struct dt_device *key, int val)
1700 {
1701         struct lu_env           env;
1702         struct lfsck_instance  *lfsck;
1703         int                     rc;
1704         ENTRY;
1705
1706         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1707         if (rc != 0)
1708                 RETURN(rc);
1709
1710         lfsck = lfsck_instance_find(key, true, false);
1711         if (likely(lfsck != NULL)) {
1712                 mutex_lock(&lfsck->li_mutex);
1713                 if (__lfsck_set_speed(lfsck, val))
1714                         rc = lfsck_bookmark_store(&env, lfsck);
1715                 mutex_unlock(&lfsck->li_mutex);
1716                 lfsck_instance_put(&env, lfsck);
1717         } else {
1718                 rc = -ENXIO;
1719         }
1720
1721         lu_env_fini(&env);
1722
1723         RETURN(rc);
1724 }
1725 EXPORT_SYMBOL(lfsck_set_speed);
1726
1727 int lfsck_get_windows(struct dt_device *key, void *buf, int len)
1728 {
1729         struct lu_env           env;
1730         struct lfsck_instance  *lfsck;
1731         int                     rc;
1732         ENTRY;
1733
1734         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1735         if (rc != 0)
1736                 RETURN(rc);
1737
1738         lfsck = lfsck_instance_find(key, true, false);
1739         if (likely(lfsck != NULL)) {
1740                 rc = snprintf(buf, len, "%u\n",
1741                               lfsck->li_bookmark_ram.lb_async_windows);
1742                 lfsck_instance_put(&env, lfsck);
1743         } else {
1744                 rc = -ENXIO;
1745         }
1746
1747         lu_env_fini(&env);
1748
1749         RETURN(rc);
1750 }
1751 EXPORT_SYMBOL(lfsck_get_windows);
1752
1753 int lfsck_set_windows(struct dt_device *key, int val)
1754 {
1755         struct lu_env           env;
1756         struct lfsck_instance  *lfsck;
1757         int                     rc;
1758         ENTRY;
1759
1760         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1761         if (rc != 0)
1762                 RETURN(rc);
1763
1764         lfsck = lfsck_instance_find(key, true, false);
1765         if (likely(lfsck != NULL)) {
1766                 if (val > LFSCK_ASYNC_WIN_MAX) {
1767                         CWARN("%s: Too large async window size, which "
1768                               "may cause memory issues. The valid range "
1769                               "is [0 - %u]. If you do not want to restrict "
1770                               "the window size for async requests pipeline, "
1771                               "just set it as 0.\n",
1772                               lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
1773                         rc = -EINVAL;
1774                 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
1775                         mutex_lock(&lfsck->li_mutex);
1776                         lfsck->li_bookmark_ram.lb_async_windows = val;
1777                         rc = lfsck_bookmark_store(&env, lfsck);
1778                         mutex_unlock(&lfsck->li_mutex);
1779                 }
1780                 lfsck_instance_put(&env, lfsck);
1781         } else {
1782                 rc = -ENXIO;
1783         }
1784
1785         lu_env_fini(&env);
1786
1787         RETURN(rc);
1788 }
1789 EXPORT_SYMBOL(lfsck_set_windows);
1790
1791 int lfsck_dump(struct dt_device *key, void *buf, int len, enum lfsck_type type)
1792 {
1793         struct lu_env           env;
1794         struct lfsck_instance  *lfsck;
1795         struct lfsck_component *com;
1796         int                     rc;
1797         ENTRY;
1798
1799         rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1800         if (rc != 0)
1801                 RETURN(rc);
1802
1803         lfsck = lfsck_instance_find(key, true, false);
1804         if (likely(lfsck != NULL)) {
1805                 com = lfsck_component_find(lfsck, type);
1806                 if (likely(com != NULL)) {
1807                         rc = com->lc_ops->lfsck_dump(&env, com, buf, len);
1808                         lfsck_component_put(&env, com);
1809                 } else {
1810                         rc = -ENOTSUPP;
1811                 }
1812
1813                 lfsck_instance_put(&env, lfsck);
1814         } else {
1815                 rc = -ENXIO;
1816         }
1817
1818         lu_env_fini(&env);
1819
1820         RETURN(rc);
1821 }
1822 EXPORT_SYMBOL(lfsck_dump);
1823
1824 static int lfsck_stop_all(const struct lu_env *env,
1825                           struct lfsck_instance *lfsck,
1826                           struct lfsck_stop *stop)
1827 {
1828         struct lfsck_thread_info          *info   = lfsck_env_info(env);
1829         struct lfsck_request              *lr     = &info->lti_lr;
1830         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
1831         struct ptlrpc_request_set         *set;
1832         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
1833         struct lfsck_tgt_desc             *ltd;
1834         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
1835         __u32                              idx;
1836         int                                rc     = 0;
1837         int                                rc1    = 0;
1838         ENTRY;
1839
1840         LASSERT(stop->ls_flags & LPF_BROADCAST);
1841
1842         set = ptlrpc_prep_set();
1843         if (unlikely(set == NULL)) {
1844                 CERROR("%s: cannot allocate memory for stop LFSCK on "
1845                        "all targets\n", lfsck_lfsck2name(lfsck));
1846
1847                 RETURN(-ENOMEM);
1848         }
1849
1850         memset(lr, 0, sizeof(*lr));
1851         lr->lr_event = LE_STOP;
1852         lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1853         lr->lr_status = stop->ls_status;
1854         lr->lr_version = bk->lb_version;
1855         lr->lr_active = LFSCK_TYPES_ALL;
1856         lr->lr_param = stop->ls_flags;
1857
1858         laia->laia_com = NULL;
1859         laia->laia_ltds = ltds;
1860         laia->laia_lr = lr;
1861         laia->laia_result = 0;
1862         laia->laia_shared = 1;
1863
1864         down_read(&ltds->ltd_rw_sem);
1865         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1866                 ltd = lfsck_tgt_get(ltds, idx);
1867                 LASSERT(ltd != NULL);
1868
1869                 laia->laia_ltd = ltd;
1870                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1871                                          lfsck_async_interpret, laia,
1872                                          LFSCK_NOTIFY);
1873                 if (rc != 0) {
1874                         lfsck_interpret(env, lfsck, NULL, laia, rc);
1875                         lfsck_tgt_put(ltd);
1876                         CWARN("%s: cannot notify MDT %x for LFSCK stop: "
1877                               "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
1878                         rc1 = rc;
1879                 }
1880         }
1881         up_read(&ltds->ltd_rw_sem);
1882
1883         rc = ptlrpc_set_wait(set);
1884         ptlrpc_set_destroy(set);
1885
1886         if (rc == 0)
1887                 rc = laia->laia_result;
1888
1889         if (rc == -EALREADY)
1890                 rc = 0;
1891
1892         if (rc != 0)
1893                 CWARN("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
1894                       lfsck_lfsck2name(lfsck), rc);
1895
1896         RETURN(rc != 0 ? rc : rc1);
1897 }
1898
1899 static int lfsck_start_all(const struct lu_env *env,
1900                            struct lfsck_instance *lfsck,
1901                            struct lfsck_start *start)
1902 {
1903         struct lfsck_thread_info          *info   = lfsck_env_info(env);
1904         struct lfsck_request              *lr     = &info->lti_lr;
1905         struct lfsck_async_interpret_args *laia   = &info->lti_laia;
1906         struct ptlrpc_request_set         *set;
1907         struct lfsck_tgt_descs            *ltds   = &lfsck->li_mdt_descs;
1908         struct lfsck_tgt_desc             *ltd;
1909         struct lfsck_bookmark             *bk     = &lfsck->li_bookmark_ram;
1910         __u32                              idx;
1911         int                                rc     = 0;
1912         ENTRY;
1913
1914         LASSERT(start->ls_flags & LPF_BROADCAST);
1915
1916         set = ptlrpc_prep_set();
1917         if (unlikely(set == NULL)) {
1918                 if (bk->lb_param & LPF_FAILOUT) {
1919                         CERROR("%s: cannot allocate memory for start LFSCK on "
1920                                "all targets, failout.\n",
1921                                lfsck_lfsck2name(lfsck));
1922
1923                         RETURN(-ENOMEM);
1924                 } else {
1925                         CWARN("%s: cannot allocate memory for start LFSCK on "
1926                               "all targets, partly scan.\n",
1927                               lfsck_lfsck2name(lfsck));
1928
1929                         RETURN(0);
1930                 }
1931         }
1932
1933         memset(lr, 0, sizeof(*lr));
1934         lr->lr_event = LE_START;
1935         lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1936         lr->lr_speed = bk->lb_speed_limit;
1937         lr->lr_version = bk->lb_version;
1938         lr->lr_active = start->ls_active;
1939         lr->lr_param = start->ls_flags;
1940         lr->lr_async_windows = bk->lb_async_windows;
1941         lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
1942                        LSV_ASYNC_WINDOWS;
1943
1944         laia->laia_com = NULL;
1945         laia->laia_ltds = ltds;
1946         laia->laia_lr = lr;
1947         laia->laia_result = 0;
1948         laia->laia_shared = 1;
1949
1950         down_read(&ltds->ltd_rw_sem);
1951         cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
1952                 ltd = lfsck_tgt_get(ltds, idx);
1953                 LASSERT(ltd != NULL);
1954
1955                 laia->laia_ltd = ltd;
1956                 ltd->ltd_layout_done = 0;
1957                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
1958                                          lfsck_async_interpret, laia,
1959                                          LFSCK_NOTIFY);
1960                 if (rc != 0) {
1961                         lfsck_interpret(env, lfsck, NULL, laia, rc);
1962                         lfsck_tgt_put(ltd);
1963                         if (bk->lb_param & LPF_FAILOUT) {
1964                                 CERROR("%s: cannot notify MDT %x for LFSCK "
1965                                        "start, failout: rc = %d\n",
1966                                        lfsck_lfsck2name(lfsck), idx, rc);
1967                                 break;
1968                         } else {
1969                                 CWARN("%s: cannot notify MDT %x for LFSCK "
1970                                       "start, partly scan: rc = %d\n",
1971                                       lfsck_lfsck2name(lfsck), idx, rc);
1972                                 rc = 0;
1973                         }
1974                 }
1975         }
1976         up_read(&ltds->ltd_rw_sem);
1977
1978         if (rc != 0) {
1979                 ptlrpc_set_destroy(set);
1980
1981                 RETURN(rc);
1982         }
1983
1984         rc = ptlrpc_set_wait(set);
1985         ptlrpc_set_destroy(set);
1986
1987         if (rc == 0)
1988                 rc = laia->laia_result;
1989
1990         if (rc != 0) {
1991                 if (bk->lb_param & LPF_FAILOUT) {
1992                         struct lfsck_stop *stop = &info->lti_stop;
1993
1994                         CERROR("%s: cannot start LFSCK on some MDTs, "
1995                                "stop all: rc = %d\n",
1996                                lfsck_lfsck2name(lfsck), rc);
1997                         if (rc != -EALREADY) {
1998                                 stop->ls_status = LS_FAILED;
1999                                 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2000                                 lfsck_stop_all(env, lfsck, stop);
2001                         }
2002                 } else {
2003                         CWARN("%s: cannot start LFSCK on some MDTs, "
2004                               "partly scan: rc = %d\n",
2005                               lfsck_lfsck2name(lfsck), rc);
2006                         rc = 0;
2007                 }
2008         }
2009
2010         RETURN(rc);
2011 }
2012
2013 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2014                 struct lfsck_start_param *lsp)
2015 {
2016         struct lfsck_start              *start  = lsp->lsp_start;
2017         struct lfsck_instance           *lfsck;
2018         struct lfsck_bookmark           *bk;
2019         struct ptlrpc_thread            *thread;
2020         struct lfsck_component          *com;
2021         struct l_wait_info               lwi    = { 0 };
2022         struct lfsck_thread_args        *lta;
2023         long                             rc     = 0;
2024         __u16                            valid  = 0;
2025         __u16                            flags  = 0;
2026         __u16                            type   = 1;
2027         ENTRY;
2028
2029         lfsck = lfsck_instance_find(key, true, false);
2030         if (unlikely(lfsck == NULL))
2031                 RETURN(-ENXIO);
2032
2033         /* System is not ready, try again later. */
2034         if (unlikely(lfsck->li_namespace == NULL))
2035                 GOTO(put, rc = -EAGAIN);
2036
2037         /* start == NULL means auto trigger paused LFSCK. */
2038         if ((start == NULL) &&
2039             (cfs_list_empty(&lfsck->li_list_scan) ||
2040              OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2041                 GOTO(put, rc = 0);
2042
2043         bk = &lfsck->li_bookmark_ram;
2044         thread = &lfsck->li_thread;
2045         mutex_lock(&lfsck->li_mutex);
2046         spin_lock(&lfsck->li_lock);
2047         if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2048                 rc = -EALREADY;
2049                 while (start->ls_active != 0) {
2050                         if (!(type & start->ls_active)) {
2051                                 type <<= 1;
2052                                 continue;
2053                         }
2054
2055                         com = __lfsck_component_find(lfsck, type,
2056                                                      &lfsck->li_list_scan);
2057                         if (com == NULL)
2058                                 com = __lfsck_component_find(lfsck, type,
2059                                                 &lfsck->li_list_double_scan);
2060                         if (com == NULL) {
2061                                 rc = -EOPNOTSUPP;
2062                                 break;
2063                         }
2064
2065                         if (com->lc_ops->lfsck_join != NULL) {
2066                                 rc = com->lc_ops->lfsck_join( env, com, lsp);
2067                                 if (rc != 0 && rc != -EALREADY)
2068                                         break;
2069                         }
2070                         start->ls_active &= ~type;
2071                         type <<= 1;
2072                 }
2073                 spin_unlock(&lfsck->li_lock);
2074                 GOTO(out, rc);
2075         }
2076         spin_unlock(&lfsck->li_lock);
2077
2078         lfsck->li_status = 0;
2079         lfsck->li_oit_over = 0;
2080         lfsck->li_start_unplug = 0;
2081         lfsck->li_drop_dryrun = 0;
2082         lfsck->li_new_scanned = 0;
2083
2084         /* For auto trigger. */
2085         if (start == NULL)
2086                 goto trigger;
2087
2088         if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2089                 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2090                        lfsck_lfsck2name(lfsck));
2091
2092                 GOTO(out, rc = -EPERM);
2093         }
2094
2095         start->ls_version = bk->lb_version;
2096
2097         if (start->ls_active != 0) {
2098                 struct lfsck_component *next;
2099
2100                 if (start->ls_active == LFSCK_TYPES_ALL)
2101                         start->ls_active = LFSCK_TYPES_SUPPORTED;
2102
2103                 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2104                         start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2105                         GOTO(out, rc = -ENOTSUPP);
2106                 }
2107
2108                 list_for_each_entry_safe(com, next,
2109                                          &lfsck->li_list_scan, lc_link) {
2110                         if (!(com->lc_type & start->ls_active)) {
2111                                 rc = com->lc_ops->lfsck_post(env, com, 0,
2112                                                              false);
2113                                 if (rc != 0)
2114                                         GOTO(out, rc);
2115                         }
2116                 }
2117
2118                 while (start->ls_active != 0) {
2119                         if (type & start->ls_active) {
2120                                 com = __lfsck_component_find(lfsck, type,
2121                                                         &lfsck->li_list_idle);
2122                                 if (com != NULL) {
2123                                         /* The component status will be updated
2124                                          * when its prep() is called later by
2125                                          * the LFSCK main engine. */
2126                                         list_del_init(&com->lc_link);
2127                                         list_add_tail(&com->lc_link,
2128                                                       &lfsck->li_list_scan);
2129                                 }
2130                                 start->ls_active &= ~type;
2131                         }
2132                         type <<= 1;
2133                 }
2134         }
2135
2136         if (list_empty(&lfsck->li_list_scan)) {
2137                 /* The speed limit will be used to control both the LFSCK and
2138                  * low layer scrub (if applied), need to be handled firstly. */
2139                 if (start->ls_valid & LSV_SPEED_LIMIT) {
2140                         if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2141                                 rc = lfsck_bookmark_store(env, lfsck);
2142                                 if (rc != 0)
2143                                         GOTO(out, rc);
2144                         }
2145                 }
2146
2147                 goto trigger;
2148         }
2149
2150         if (start->ls_flags & LPF_RESET)
2151                 flags |= DOIF_RESET;
2152
2153         rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2154         if (rc != 0)
2155                 GOTO(out, rc);
2156
2157         list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2158                 start->ls_active |= com->lc_type;
2159                 if (flags & DOIF_RESET) {
2160                         rc = com->lc_ops->lfsck_reset(env, com, false);
2161                         if (rc != 0)
2162                                 GOTO(out, rc);
2163                 }
2164         }
2165
2166 trigger:
2167         lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY;
2168         if (bk->lb_param & LPF_DRYRUN)
2169                 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2170
2171         if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2172                 valid |= DOIV_ERROR_HANDLE;
2173                 if (start->ls_flags & LPF_FAILOUT)
2174                         flags |= DOIF_FAILOUT;
2175         }
2176
2177         if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2178                 valid |= DOIV_DRYRUN;
2179                 if (start->ls_flags & LPF_DRYRUN)
2180                         flags |= DOIF_DRYRUN;
2181         }
2182
2183         if (!list_empty(&lfsck->li_list_scan))
2184                 flags |= DOIF_OUTUSED;
2185
2186         lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2187         thread_set_flags(thread, 0);
2188         lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2189         if (IS_ERR(lta))
2190                 GOTO(out, rc = PTR_ERR(lta));
2191
2192         __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2193         rc = PTR_ERR(kthread_run(lfsck_master_engine, lta, "lfsck"));
2194         if (IS_ERR_VALUE(rc)) {
2195                 CERROR("%s: cannot start LFSCK thread: rc = %ld\n",
2196                        lfsck_lfsck2name(lfsck), rc);
2197                 lfsck_thread_args_fini(lta);
2198
2199                 GOTO(out, rc);
2200         }
2201
2202         l_wait_event(thread->t_ctl_waitq,
2203                      thread_is_running(thread) ||
2204                      thread_is_stopped(thread),
2205                      &lwi);
2206         if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2207                 lfsck->li_start_unplug = 1;
2208                 wake_up_all(&thread->t_ctl_waitq);
2209
2210                 GOTO(out, rc = 0);
2211         }
2212
2213         /* release lfsck::li_mutex to avoid deadlock. */
2214         mutex_unlock(&lfsck->li_mutex);
2215         rc = lfsck_start_all(env, lfsck, start);
2216         if (rc != 0) {
2217                 spin_lock(&lfsck->li_lock);
2218                 if (thread_is_stopped(thread)) {
2219                         spin_unlock(&lfsck->li_lock);
2220                 } else {
2221                         lfsck->li_status = LS_FAILED;
2222                         lfsck->li_flags = 0;
2223                         thread_set_flags(thread, SVC_STOPPING);
2224                         spin_unlock(&lfsck->li_lock);
2225
2226                         lfsck->li_start_unplug = 1;
2227                         wake_up_all(&thread->t_ctl_waitq);
2228                         l_wait_event(thread->t_ctl_waitq,
2229                                      thread_is_stopped(thread),
2230                                      &lwi);
2231                 }
2232         } else {
2233                 lfsck->li_start_unplug = 1;
2234                 wake_up_all(&thread->t_ctl_waitq);
2235         }
2236
2237         GOTO(put, rc);
2238
2239 out:
2240         mutex_unlock(&lfsck->li_mutex);
2241
2242 put:
2243         lfsck_instance_put(env, lfsck);
2244
2245         return rc < 0 ? rc : 0;
2246 }
2247 EXPORT_SYMBOL(lfsck_start);
2248
2249 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
2250                struct lfsck_stop *stop)
2251 {
2252         struct lfsck_instance   *lfsck;
2253         struct ptlrpc_thread    *thread;
2254         struct l_wait_info       lwi    = { 0 };
2255         int                      rc     = 0;
2256         int                      rc1    = 0;
2257         ENTRY;
2258
2259         lfsck = lfsck_instance_find(key, true, false);
2260         if (unlikely(lfsck == NULL))
2261                 RETURN(-ENXIO);
2262
2263         thread = &lfsck->li_thread;
2264         /* release lfsck::li_mutex to avoid deadlock. */
2265         if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
2266                 if (!lfsck->li_master) {
2267                         CERROR("%s: only allow to specify '-A' via MDS\n",
2268                                lfsck_lfsck2name(lfsck));
2269
2270                         GOTO(out, rc = -EPERM);
2271                 }
2272
2273                 rc1 = lfsck_stop_all(env, lfsck, stop);
2274         }
2275
2276         mutex_lock(&lfsck->li_mutex);
2277         spin_lock(&lfsck->li_lock);
2278         if (thread_is_init(thread) || thread_is_stopped(thread)) {
2279                 spin_unlock(&lfsck->li_lock);
2280                 GOTO(out, rc = -EALREADY);
2281         }
2282
2283         if (stop != NULL) {
2284                 lfsck->li_status = stop->ls_status;
2285                 lfsck->li_flags = stop->ls_flags;
2286         } else {
2287                 lfsck->li_status = LS_STOPPED;
2288                 lfsck->li_flags = 0;
2289         }
2290
2291         thread_set_flags(thread, SVC_STOPPING);
2292         spin_unlock(&lfsck->li_lock);
2293
2294         wake_up_all(&thread->t_ctl_waitq);
2295         l_wait_event(thread->t_ctl_waitq,
2296                      thread_is_stopped(thread),
2297                      &lwi);
2298
2299         GOTO(out, rc = 0);
2300
2301 out:
2302         mutex_unlock(&lfsck->li_mutex);
2303         lfsck_instance_put(env, lfsck);
2304
2305         return rc != 0 ? rc : rc1;
2306 }
2307 EXPORT_SYMBOL(lfsck_stop);
2308
2309 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
2310                     struct lfsck_request *lr)
2311 {
2312         int rc = -EOPNOTSUPP;
2313         ENTRY;
2314
2315         switch (lr->lr_event) {
2316         case LE_START: {
2317                 struct lfsck_start       *start = &lfsck_env_info(env)->lti_start;
2318                 struct lfsck_start_param  lsp;
2319
2320                 memset(start, 0, sizeof(*start));
2321                 start->ls_valid = lr->lr_valid;
2322                 start->ls_speed_limit = lr->lr_speed;
2323                 start->ls_version = lr->lr_version;
2324                 start->ls_active = lr->lr_active;
2325                 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2326                 start->ls_async_windows = lr->lr_async_windows;
2327
2328                 lsp.lsp_start = start;
2329                 lsp.lsp_index = lr->lr_index;
2330                 lsp.lsp_index_valid = 1;
2331                 rc = lfsck_start(env, key, &lsp);
2332                 break;
2333         }
2334         case LE_STOP: {
2335                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2336
2337                 memset(stop, 0, sizeof(*stop));
2338                 stop->ls_status = lr->lr_status;
2339                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2340                 rc = lfsck_stop(env, key, stop);
2341                 break;
2342         }
2343         case LE_PHASE1_DONE:
2344         case LE_PHASE2_DONE:
2345         case LE_FID_ACCESSED:
2346         case LE_PEER_EXIT:
2347         case LE_CONDITIONAL_DESTROY:
2348         case LE_PAIRS_VERIFY: {
2349                 struct lfsck_instance  *lfsck;
2350                 struct lfsck_component *com;
2351
2352                 lfsck = lfsck_instance_find(key, true, false);
2353                 if (unlikely(lfsck == NULL))
2354                         RETURN(-ENXIO);
2355
2356                 com = lfsck_component_find(lfsck, lr->lr_active);
2357                 if (likely(com != NULL)) {
2358                         rc = com->lc_ops->lfsck_in_notify(env, com, lr);
2359                         lfsck_component_put(env, com);
2360                 }
2361
2362                 lfsck_instance_put(env, lfsck);
2363                 break;
2364         }
2365         default:
2366                 break;
2367         }
2368
2369         RETURN(rc);
2370 }
2371 EXPORT_SYMBOL(lfsck_in_notify);
2372
2373 int lfsck_query(const struct lu_env *env, struct dt_device *key,
2374                 struct lfsck_request *lr)
2375 {
2376         struct lfsck_instance  *lfsck;
2377         struct lfsck_component *com;
2378         int                     rc;
2379         ENTRY;
2380
2381         lfsck = lfsck_instance_find(key, true, false);
2382         if (unlikely(lfsck == NULL))
2383                 RETURN(-ENXIO);
2384
2385         com = lfsck_component_find(lfsck, lr->lr_active);
2386         if (likely(com != NULL)) {
2387                 rc = com->lc_ops->lfsck_query(env, com);
2388                 lfsck_component_put(env, com);
2389         } else {
2390                 rc = -ENOTSUPP;
2391         }
2392
2393         lfsck_instance_put(env, lfsck);
2394
2395         RETURN(rc);
2396 }
2397 EXPORT_SYMBOL(lfsck_query);
2398
2399 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
2400                              struct ldlm_namespace *ns)
2401 {
2402         struct lfsck_instance  *lfsck;
2403         int                     rc      = -ENXIO;
2404
2405         lfsck = lfsck_instance_find(key, true, false);
2406         if (likely(lfsck != NULL)) {
2407                 lfsck->li_namespace = ns;
2408                 lfsck_instance_put(env, lfsck);
2409                 rc = 0;
2410         }
2411
2412         return rc;
2413 }
2414 EXPORT_SYMBOL(lfsck_register_namespace);
2415
2416 int lfsck_register(const struct lu_env *env, struct dt_device *key,
2417                    struct dt_device *next, struct obd_device *obd,
2418                    lfsck_out_notify notify, void *notify_data, bool master)
2419 {
2420         struct lfsck_instance   *lfsck;
2421         struct dt_object        *root  = NULL;
2422         struct dt_object        *obj;
2423         struct lu_fid           *fid   = &lfsck_env_info(env)->lti_fid;
2424         int                      rc;
2425         ENTRY;
2426
2427         lfsck = lfsck_instance_find(key, false, false);
2428         if (unlikely(lfsck != NULL))
2429                 RETURN(-EEXIST);
2430
2431         OBD_ALLOC_PTR(lfsck);
2432         if (lfsck == NULL)
2433                 RETURN(-ENOMEM);
2434
2435         mutex_init(&lfsck->li_mutex);
2436         spin_lock_init(&lfsck->li_lock);
2437         CFS_INIT_LIST_HEAD(&lfsck->li_link);
2438         CFS_INIT_LIST_HEAD(&lfsck->li_list_scan);
2439         CFS_INIT_LIST_HEAD(&lfsck->li_list_dir);
2440         CFS_INIT_LIST_HEAD(&lfsck->li_list_double_scan);
2441         CFS_INIT_LIST_HEAD(&lfsck->li_list_idle);
2442         atomic_set(&lfsck->li_ref, 1);
2443         atomic_set(&lfsck->li_double_scan_count, 0);
2444         init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
2445         lfsck->li_out_notify = notify;
2446         lfsck->li_out_notify_data = notify_data;
2447         lfsck->li_next = next;
2448         lfsck->li_bottom = key;
2449         lfsck->li_obd = obd;
2450
2451         rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
2452         if (rc != 0)
2453                 GOTO(out, rc);
2454
2455         rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
2456         if (rc != 0)
2457                 GOTO(out, rc);
2458
2459         fid->f_seq = FID_SEQ_LOCAL_NAME;
2460         fid->f_oid = 1;
2461         fid->f_ver = 0;
2462         rc = local_oid_storage_init(env, lfsck->li_bottom, fid, &lfsck->li_los);
2463         if (rc != 0)
2464                 GOTO(out, rc);
2465
2466         rc = dt_root_get(env, key, fid);
2467         if (rc != 0)
2468                 GOTO(out, rc);
2469
2470         root = dt_locate(env, lfsck->li_bottom, fid);
2471         if (IS_ERR(root))
2472                 GOTO(out, rc = PTR_ERR(root));
2473
2474         if (unlikely(!dt_try_as_dir(env, root)))
2475                 GOTO(out, rc = -ENOTDIR);
2476
2477         lfsck->li_local_root_fid = *fid;
2478         if (master) {
2479                 lfsck->li_master = 1;
2480                 if (lfsck_dev_idx(lfsck->li_bottom) == 0) {
2481                         rc = dt_lookup(env, root,
2482                                 (struct dt_rec *)(&lfsck->li_global_root_fid),
2483                                 (const struct dt_key *)"ROOT", BYPASS_CAPA);
2484                         if (rc != 0)
2485                                 GOTO(out, rc);
2486                 }
2487         }
2488
2489         fid->f_seq = FID_SEQ_LOCAL_FILE;
2490         fid->f_oid = OTABLE_IT_OID;
2491         fid->f_ver = 0;
2492         obj = dt_locate(env, lfsck->li_bottom, fid);
2493         if (IS_ERR(obj))
2494                 GOTO(out, rc = PTR_ERR(obj));
2495
2496         lfsck->li_obj_oit = obj;
2497         rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
2498         if (rc != 0) {
2499                 if (rc == -ENOTSUPP)
2500                         GOTO(add, rc = 0);
2501
2502                 GOTO(out, rc);
2503         }
2504
2505         rc = lfsck_bookmark_setup(env, lfsck);
2506         if (rc != 0)
2507                 GOTO(out, rc);
2508
2509         if (master) {
2510                 rc = lfsck_fid_init(lfsck);
2511                 if (rc < 0)
2512                         GOTO(out, rc);
2513
2514                 rc = lfsck_namespace_setup(env, lfsck);
2515                 if (rc < 0)
2516                         GOTO(out, rc);
2517         }
2518
2519         rc = lfsck_layout_setup(env, lfsck);
2520         if (rc < 0)
2521                 GOTO(out, rc);
2522
2523         /* XXX: more LFSCK components initialization to be added here. */
2524
2525 add:
2526         rc = lfsck_instance_add(lfsck);
2527         if (rc == 0)
2528                 rc = lfsck_add_target_from_orphan(env, lfsck);
2529 out:
2530         if (root != NULL && !IS_ERR(root))
2531                 lu_object_put(env, &root->do_lu);
2532         if (rc != 0)
2533                 lfsck_instance_cleanup(env, lfsck);
2534         return rc;
2535 }
2536 EXPORT_SYMBOL(lfsck_register);
2537
2538 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
2539 {
2540         struct lfsck_instance *lfsck;
2541
2542         lfsck = lfsck_instance_find(key, false, true);
2543         if (lfsck != NULL)
2544                 lfsck_instance_put(env, lfsck);
2545 }
2546 EXPORT_SYMBOL(lfsck_degister);
2547
2548 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
2549                      struct dt_device *tgt, struct obd_export *exp,
2550                      __u32 index, bool for_ost)
2551 {
2552         struct lfsck_instance   *lfsck;
2553         struct lfsck_tgt_desc   *ltd;
2554         int                      rc;
2555         ENTRY;
2556
2557         OBD_ALLOC_PTR(ltd);
2558         if (ltd == NULL)
2559                 RETURN(-ENOMEM);
2560
2561         ltd->ltd_tgt = tgt;
2562         ltd->ltd_key = key;
2563         ltd->ltd_exp = exp;
2564         INIT_LIST_HEAD(&ltd->ltd_orphan_list);
2565         INIT_LIST_HEAD(&ltd->ltd_layout_list);
2566         INIT_LIST_HEAD(&ltd->ltd_layout_phase_list);
2567         atomic_set(&ltd->ltd_ref, 1);
2568         ltd->ltd_index = index;
2569
2570         spin_lock(&lfsck_instance_lock);
2571         lfsck = __lfsck_instance_find(key, true, false);
2572         if (lfsck == NULL) {
2573                 if (for_ost)
2574                         list_add_tail(&ltd->ltd_orphan_list,
2575                                       &lfsck_ost_orphan_list);
2576                 else
2577                         list_add_tail(&ltd->ltd_orphan_list,
2578                                       &lfsck_mdt_orphan_list);
2579                 spin_unlock(&lfsck_instance_lock);
2580
2581                 RETURN(0);
2582         }
2583         spin_unlock(&lfsck_instance_lock);
2584
2585         rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
2586         if (rc != 0)
2587                 lfsck_tgt_put(ltd);
2588
2589         lfsck_instance_put(env, lfsck);
2590
2591         RETURN(rc);
2592 }
2593 EXPORT_SYMBOL(lfsck_add_target);
2594
2595 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
2596                       struct dt_device *tgt, __u32 index, bool for_ost)
2597 {
2598         struct lfsck_instance   *lfsck;
2599         struct lfsck_tgt_descs  *ltds;
2600         struct lfsck_tgt_desc   *ltd    = NULL;
2601         struct list_head        *head;
2602
2603         if (for_ost)
2604                 head = &lfsck_ost_orphan_list;
2605         else
2606                 head = &lfsck_mdt_orphan_list;
2607
2608         spin_lock(&lfsck_instance_lock);
2609         list_for_each_entry(ltd, head, ltd_orphan_list) {
2610                 if (ltd->ltd_tgt == tgt) {
2611                         list_del_init(&ltd->ltd_orphan_list);
2612                         spin_unlock(&lfsck_instance_lock);
2613                         lfsck_tgt_put(ltd);
2614
2615                         return;
2616                 }
2617         }
2618
2619         lfsck = __lfsck_instance_find(key, true, false);
2620         spin_unlock(&lfsck_instance_lock);
2621         if (unlikely(lfsck == NULL))
2622                 return;
2623
2624         if (for_ost)
2625                 ltds = &lfsck->li_ost_descs;
2626         else
2627                 ltds = &lfsck->li_mdt_descs;
2628
2629         down_write(&ltds->ltd_rw_sem);
2630         LASSERT(ltds->ltd_tgts_bitmap != NULL);
2631
2632         if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
2633                 goto unlock;
2634
2635         ltd = LTD_TGT(ltds, index);
2636         if (unlikely(ltd == NULL))
2637                 goto unlock;
2638
2639         LASSERT(ltds->ltd_tgtnr > 0);
2640
2641         ltds->ltd_tgtnr--;
2642         cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
2643         LTD_TGT(ltds, index) = NULL;
2644
2645 unlock:
2646         if (ltd == NULL) {
2647                 if (for_ost)
2648                         head = &lfsck->li_ost_descs.ltd_orphan;
2649                 else
2650                         head = &lfsck->li_ost_descs.ltd_orphan;
2651
2652                 list_for_each_entry(ltd, head, ltd_orphan_list) {
2653                         if (ltd->ltd_tgt == tgt) {
2654                                 list_del_init(&ltd->ltd_orphan_list);
2655                                 break;
2656                         }
2657                 }
2658         }
2659
2660         up_write(&ltds->ltd_rw_sem);
2661         if (ltd != NULL) {
2662                 spin_lock(&ltds->ltd_lock);
2663                 ltd->ltd_dead = 1;
2664                 spin_unlock(&ltds->ltd_lock);
2665                 lfsck_stop_notify(env, lfsck, ltds, ltd, LT_LAYOUT);
2666                 lfsck_tgt_put(ltd);
2667         }
2668
2669         lfsck_instance_put(env, lfsck);
2670 }
2671 EXPORT_SYMBOL(lfsck_del_target);
2672
2673 static int __init lfsck_init(void)
2674 {
2675         int rc;
2676
2677         INIT_LIST_HEAD(&lfsck_ost_orphan_list);
2678         INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
2679         lfsck_key_init_generic(&lfsck_thread_key, NULL);
2680         rc = lu_context_key_register(&lfsck_thread_key);
2681         if (rc == 0) {
2682                 tgt_register_lfsck_in_notify(lfsck_in_notify);
2683                 tgt_register_lfsck_query(lfsck_query);
2684         }
2685
2686         return rc;
2687 }
2688
2689 static void __exit lfsck_exit(void)
2690 {
2691         struct lfsck_tgt_desc *ltd;
2692         struct lfsck_tgt_desc *next;
2693
2694         LASSERT(cfs_list_empty(&lfsck_instance_list));
2695
2696         list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
2697                                  ltd_orphan_list) {
2698                 list_del_init(&ltd->ltd_orphan_list);
2699                 lfsck_tgt_put(ltd);
2700         }
2701
2702         list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
2703                                  ltd_orphan_list) {
2704                 list_del_init(&ltd->ltd_orphan_list);
2705                 lfsck_tgt_put(ltd);
2706         }
2707
2708         lu_context_key_degister(&lfsck_thread_key);
2709 }
2710
2711 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
2712 MODULE_DESCRIPTION("LFSCK");
2713 MODULE_LICENSE("GPL");
2714
2715 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);