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