4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2013, 2017, Intel Corporation.
26 * lustre/lfsck/lfsck_lib.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
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>
44 #include "lfsck_internal.h"
46 #define LFSCK_CHECKPOINT_SKIP 1
48 /* define lfsck thread key */
49 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
51 static void lfsck_key_fini(const struct lu_context *ctx,
52 struct lu_context_key *key, void *data)
54 struct lfsck_thread_info *info = data;
56 lu_buf_free(&info->lti_linkea_buf);
57 lu_buf_free(&info->lti_linkea_buf2);
58 lu_buf_free(&info->lti_big_buf);
62 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
63 LU_KEY_INIT_GENERIC(lfsck);
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);
70 const char *lfsck_flags_names[] = {
79 const char *lfsck_param_names[] = {
89 "delay_create_ostobj",
93 enum lfsck_verify_lpf_types {
95 LVLT_BY_NAMEENTRY = 1,
99 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
101 if (type == LFSCK_TYPE_LAYOUT) {
102 ltd->ltd_layout_status = LS_MAX;
103 ltd->ltd_layout_repaired = 0;
105 ltd->ltd_namespace_status = LS_MAX;
106 ltd->ltd_namespace_repaired = 0;
110 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
112 spin_lock_init(<ds->ltd_lock);
113 init_rwsem(<ds->ltd_rw_sem);
114 INIT_LIST_HEAD(<ds->ltd_orphan);
115 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
116 if (ltds->ltd_tgts_bitmap == NULL)
122 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
124 struct lfsck_tgt_desc *ltd;
125 struct lfsck_tgt_desc *next;
128 down_write(<ds->ltd_rw_sem);
130 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
132 list_del_init(<d->ltd_orphan_list);
136 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
137 up_write(<ds->ltd_rw_sem);
142 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
143 ltd = lfsck_ltd2tgt(ltds, idx);
144 if (likely(ltd != NULL)) {
145 LASSERT(list_empty(<d->ltd_layout_list));
146 LASSERT(list_empty(<d->ltd_layout_phase_list));
147 LASSERT(list_empty(<d->ltd_namespace_list));
148 LASSERT(list_empty(<d->ltd_namespace_phase_list));
151 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
152 lfsck_assign_tgt(ltds, NULL, idx);
157 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
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;
167 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
168 ltds->ltd_tgts_bitmap = NULL;
169 up_write(<ds->ltd_rw_sem);
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)
177 struct lfsck_tgt_descs *ltds;
178 __u32 index = ltd->ltd_index;
183 ltds = &lfsck->li_ost_descs;
185 ltds = &lfsck->li_mdt_descs;
188 down_write(<ds->ltd_rw_sem);
190 LASSERT(ltds->ltd_tgts_bitmap != NULL);
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;
198 while (newsize < index + 1)
201 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
202 if (new_bitmap == NULL)
203 GOTO(unlock, rc = -ENOMEM);
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);
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);
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);
224 lfsck_assign_tgt(ltds, ltd, index);
225 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
228 GOTO(unlock, rc = 0);
232 up_write(<ds->ltd_rw_sem);
237 static int lfsck_add_target_from_orphan(const struct lu_env *env,
238 struct lfsck_instance *lfsck)
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;
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(<d->ltd_orphan_list,
254 spin_unlock(&lfsck_instance_lock);
256 down_write(<ds->ltd_rw_sem);
257 while (!list_empty(<ds->ltd_orphan)) {
258 ltd = list_entry(ltds->ltd_orphan.next,
259 struct lfsck_tgt_desc,
261 list_del_init(<d->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(<ds->ltd_rw_sem);
268 down_write(<ds->ltd_rw_sem);
270 up_write(<ds->ltd_rw_sem);
273 ltds = &lfsck->li_mdt_descs;
274 head = &lfsck_mdt_orphan_list;
282 static inline struct lfsck_component *
283 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
284 struct list_head *list)
286 struct lfsck_component *com;
288 list_for_each_entry(com, list, lc_link) {
289 if (com->lc_type == type)
295 struct lfsck_component *
296 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
298 struct lfsck_component *com;
300 spin_lock(&lfsck->li_lock);
301 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
305 com = __lfsck_component_find(lfsck, type,
306 &lfsck->li_list_double_scan);
310 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
314 lfsck_component_get(com);
315 spin_unlock(&lfsck->li_lock);
319 void lfsck_component_cleanup(const struct lu_env *env,
320 struct lfsck_component *com)
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);
327 lfsck_component_put(env, com);
330 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
331 struct lu_fid *fid, bool locked)
333 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
338 mutex_lock(&lfsck->li_mutex);
340 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
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);
351 mutex_unlock(&lfsck->li_mutex);
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,
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;
367 LASSERT(lfsck->li_namespace != NULL);
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;
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;
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
387 dt_invalidate(env, obj);
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);
396 if (rc == ELDLM_OK) {
399 memset(lh, 0, sizeof(*lh));
407 * Request the specified ibits lock for the given object.
409 * Before the LFSCK modifying on the namespace visible object,
410 * it needs to acquire related ibits ldlm lock.
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
419 * \retval 0 for success
420 * \retval negative error number on failure
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)
426 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
428 LASSERT(!lustre_handle_is_used(lh));
430 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
431 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
435 * Release the the specified ibits lock.
437 * If the lock has been acquired before, release it
438 * and cleanup the handle. Otherwise, do nothing.
440 * \param[in] lh pointer to the lock handle
441 * \param[in] mode the mode for the ldlm lock to be released
443 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
445 if (lustre_handle_is_used(lh)) {
446 ldlm_lock_decref(lh, mode);
447 memset(lh, 0, sizeof(*lh));
452 * Request compound ibits locks for the given <obj, name> pairs.
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.
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.
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
472 * \retval 0 for success
473 * \retval negative error number on failure
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)
479 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
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));
490 llh->llh_pdo_mode = LCK_EX;
493 llh->llh_pdo_mode = LCK_CW;
496 llh->llh_pdo_mode = LCK_CR;
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)));
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);
511 llh->llh_reg_mode = mode;
512 resid->name[LUSTRE_RES_ID_HSH_OFF] = ll_full_name_hash(NULL, 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);
518 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
524 * Release the the compound ibits locks.
526 * \param[in] llh pointer to the lfsck_lock_handle to be released
528 void lfsck_unlock(struct lfsck_lock_handle *llh)
530 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
531 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
534 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
535 struct lfsck_instance *lfsck,
536 const struct lu_fid *fid)
538 struct seq_server_site *ss = lfsck_dev_site(lfsck);
539 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
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))
547 return lfsck_dev_idx(lfsck);
550 fld_range_set_mdt(range);
551 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
553 rc = range->lsr_index;
558 const char dot[] = ".";
559 const char dotdot[] = "..";
560 static const char dotlustre[] = ".lustre";
561 static const char lostfound[] = "lost+found";
564 * Remove the name entry from the .lustre/lost+found directory.
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.
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
574 * \retval 0 for success
575 * \retval negative error number on failure
577 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
578 struct lfsck_instance *lfsck,
581 struct dt_object *parent = lfsck->li_lpf_root_obj;
582 struct dt_device *dev = lfsck_obj2dev(parent);
584 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
588 rc = lfsck_lock(env, lfsck, parent, name, llh,
589 MDS_INODELOCK_UPDATE, LCK_PW);
593 th = dt_trans_create(env, dev);
595 GOTO(unlock, rc = PTR_ERR(th));
597 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
601 rc = dt_declare_ref_del(env, parent, th);
605 rc = dt_trans_start_local(env, dev, th);
609 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
613 dt_write_lock(env, parent, 0);
614 rc = dt_ref_del(env, parent, th);
615 dt_write_unlock(env, parent);
620 dt_trans_stop(env, dev, th);
625 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
626 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
631 static int lfsck_create_lpf_local(const struct lu_env *env,
632 struct lfsck_instance *lfsck,
633 struct dt_object *child,
635 struct dt_object_format *dof,
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;
649 int len = sizeof(struct lfsck_bookmark);
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));
659 th = dt_trans_create(env, dev);
663 /* 1a. create child */
664 rc = dt_declare_create(env, child, la, NULL, dof, th);
668 if (!dt_try_as_dir(env, child))
669 GOTO(stop, rc = -ENOTDIR);
671 /* 2a. increase child nlink */
672 rc = dt_declare_ref_add(env, child, th);
676 /* 3a. insert dot into child dir */
677 rec->rec_type = S_IFDIR;
679 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
680 (const struct dt_key *)dot, th);
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);
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);
699 /* 6a. insert name into parent dir */
700 rec->rec_type = S_IFDIR;
702 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
703 (const struct dt_key *)name, th);
707 /* 7a. increase parent nlink */
708 rc = dt_declare_ref_add(env, parent, th);
712 /* 8a. update bookmark */
713 rc = dt_declare_record_write(env, bk_obj,
714 lfsck_buf_get(env, bk, len), 0, th);
718 rc = dt_trans_start_local(env, dev, th);
722 dt_write_lock(env, child, 0);
723 /* 1b. create child */
724 rc = dt_create(env, child, la, NULL, dof, th);
728 /* 2b. increase child nlink */
729 rc = dt_ref_add(env, child, th);
733 /* 3b. insert dot into child dir */
735 rc = dt_insert(env, child, (const struct dt_rec *)rec,
736 (const struct dt_key *)dot, th);
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);
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);
754 /* 6b. insert name into parent dir */
756 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
757 (const struct dt_key *)name, th);
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);
768 bk->lb_lpf_fid = *cfid;
769 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
771 /* 8b. update bookmark */
772 rc = dt_record_write(env, bk_obj,
773 lfsck_buf_get(env, bk, len), &pos, th);
778 dt_write_unlock(env, child);
781 dt_trans_stop(env, dev, th);
786 static int lfsck_create_lpf_remote(const struct lu_env *env,
787 struct lfsck_instance *lfsck,
788 struct dt_object *child,
790 struct dt_object_format *dof,
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;
804 int len = sizeof(struct lfsck_bookmark);
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));
814 /* Create .lustre/lost+found/MDTxxxx. */
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
823 * To avoid more inconsistency, we split the create operation into
826 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
828 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
831 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
832 * repair such inconsistency when LFSCK run next time. */
834 /* Transaction I: locally */
836 dev = lfsck_obj2dev(child);
837 th = dt_trans_create(env, dev);
841 /* 1a. create child */
842 rc = dt_declare_create(env, child, la, NULL, dof, th);
846 if (!dt_try_as_dir(env, child))
847 GOTO(stop, rc = -ENOTDIR);
849 /* 2a. increase child nlink */
850 rc = dt_declare_ref_add(env, child, th);
854 /* 3a. insert dot into child dir */
855 rec->rec_type = S_IFDIR;
857 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
858 (const struct dt_key *)dot, th);
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);
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);
877 /* 6a. update bookmark */
878 rc = dt_declare_record_write(env, bk_obj,
879 lfsck_buf_get(env, bk, len), 0, th);
883 rc = dt_trans_start_local(env, dev, th);
887 dt_write_lock(env, child, 0);
888 /* 1b. create child */
889 rc = dt_create(env, child, la, NULL, dof, th);
893 /* 2b. increase child nlink */
894 rc = dt_ref_add(env, child, th);
898 /* 3b. insert dot into child dir */
899 rec->rec_type = S_IFDIR;
901 rc = dt_insert(env, child, (const struct dt_rec *)rec,
902 (const struct dt_key *)dot, th);
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);
913 /* 5b. insert linkEA for child */
914 rc = dt_xattr_set(env, child, &linkea_buf,
915 XATTR_NAME_LINK, 0, th);
919 bk->lb_lpf_fid = *cfid;
920 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
922 /* 6b. update bookmark */
923 rc = dt_record_write(env, bk_obj,
924 lfsck_buf_get(env, bk, len), &pos, th);
926 dt_write_unlock(env, child);
927 dt_trans_stop(env, dev, th);
931 /* Transaction II: remotely */
933 dev = lfsck_obj2dev(parent);
934 th = dt_trans_create(env, dev);
939 /* 5a. insert name into parent dir */
941 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
942 (const struct dt_key *)name, th);
946 /* 6a. increase parent nlink */
947 rc = dt_declare_ref_add(env, parent, th);
951 rc = dt_trans_start_local(env, dev, th);
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);
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);
969 dt_write_unlock(env, child);
971 dt_trans_stop(env, dev, th);
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);
984 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
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.
991 * \param[in] env pointer to the thread context
992 * \param[in] lfsck pointer to the lfsck instance
994 * \retval 0 for success
995 * \retval negative error number on failure
997 static int lfsck_create_lpf(const struct lu_env *env,
998 struct lfsck_instance *lfsck)
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;
1009 int node = lfsck_dev_idx(lfsck);
1013 LASSERT(lfsck->li_master);
1014 LASSERT(parent != NULL);
1015 LASSERT(lfsck->li_lpf_obj == NULL);
1017 snprintf(name, 8, "MDT%04x", node);
1018 rc = lfsck_lock(env, lfsck, parent, name, llh,
1019 MDS_INODELOCK_UPDATE, LCK_PW);
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)
1034 bk->lb_lpf_fid = *cfid;
1035 rc = lfsck_bookmark_store(env, lfsck);
1037 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1042 *cfid = bk->lb_lpf_fid;
1045 child = lfsck_object_find_bottom_new(env, lfsck, cfid);
1047 GOTO(unlock, rc = PTR_ERR(child));
1049 if (dt_object_exists(child) != 0) {
1050 if (unlikely(!dt_try_as_dir(env, child)))
1053 lfsck->li_lpf_obj = child;
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 |
1063 memset(dof, 0, sizeof(*dof));
1064 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1067 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1069 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1071 lfsck->li_lpf_obj = child;
1077 if (rc != 0 && child != NULL && !IS_ERR(child))
1078 lfsck_object_put(env, child);
1084 * Scan .lustre/lost+found for bad name entries and remove them.
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
1090 * \param[in] env pointer to the thread context
1091 * \param[in] lfsck pointer to the lfsck instance
1093 * \retval 0 for success
1094 * \retval negative error number on failure
1096 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1097 struct lfsck_instance *lfsck)
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;
1107 it = iops->init(env, parent, LUDA_64BITHASH);
1109 RETURN(PTR_ERR(it));
1111 rc = iops->load(env, it, 0);
1113 rc = iops->next(env, it);
1120 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1124 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1125 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1128 /* name length must be strlen("MDTxxxx") */
1129 if (ent->lde_namelen != 7)
1132 if (memcmp(ent->lde_name, "MDT", off) != 0)
1135 while (off < 7 && isxdigit(ent->lde_name[off]))
1141 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1148 rc = iops->next(env, it);
1152 iops->fini(env, it);
1154 RETURN(rc > 0 ? 0 : rc);
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,
1162 enum lfsck_verify_lpf_types type)
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);
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);
1183 * Check whether the @child back references the @parent.
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.
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.
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
1217 * \retval positive number for uncertain inconsistency
1218 * \retval 0 for success
1219 * \retval negative error number on failure
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,
1225 enum lfsck_verify_lpf_types type)
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 };
1237 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1238 (const struct dt_key *)dotdot);
1242 if (!fid_is_sane(fid))
1243 GOTO(linkea, rc = -EINVAL);
1245 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1246 const struct lu_name *cname;
1248 if (lfsck->li_lpf_obj == NULL) {
1249 lu_object_get(&child->do_lu);
1250 lfsck->li_lpf_obj = child;
1253 cname = lfsck_name_get_const(env, name, strlen(name));
1254 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1256 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1262 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1263 if (IS_ERR(parent2))
1264 GOTO(linkea, parent2);
1266 if (!dt_object_exists(parent2)) {
1267 lfsck_object_put(env, parent2);
1269 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1272 if (!dt_try_as_dir(env, parent2)) {
1273 lfsck_object_put(env, parent2);
1275 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1279 /* To prevent rename/unlink race */
1280 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1281 MDS_INODELOCK_UPDATE, LCK_PR);
1285 dt_read_lock(env, child, 0);
1286 rc = lfsck_links_get_first(env, child, name2, fid2);
1288 dt_read_unlock(env, child);
1289 lfsck_ibits_unlock(&lh, LCK_PR);
1291 GOTO(out_put, rc = 1);
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);
1301 if (lfsck->li_lpf_obj == NULL) {
1302 lu_object_get(&child->do_lu);
1303 lfsck->li_lpf_obj = child;
1306 /* Update the child's dotdot entry */
1307 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1308 &LU_LPF_FID, S_IFDIR);
1310 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1316 if (parent2 == NULL || IS_ERR(parent2)) {
1317 dt_read_unlock(env, child);
1318 lfsck_ibits_unlock(&lh, LCK_PR);
1320 GOTO(out_done, rc = 1);
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)
1330 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1331 if (type == LVLT_BY_BOOKMARK)
1332 GOTO(out_put, rc = 1);
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);
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);
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);
1357 if (parent2 != NULL && !IS_ERR(parent2))
1358 lfsck_object_put(env, parent2);
1365 * Verify the /ROOT/.lustre/lost+found/ directory.
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.
1372 * \param[in] env pointer to the thread context
1373 * \param[in] lfsck pointer to the lfsck instance
1375 * \retval positive number for uncertain inconsistency
1376 * \retval 0 for success
1377 * \retval negative error number on failure
1379 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
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;
1392 int node = lfsck_dev_idx(lfsck);
1396 LASSERT(lfsck->li_master);
1398 if (lfsck->li_lpf_root_obj != NULL)
1402 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1405 struct lfsck_tgt_desc *ltd;
1407 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1408 if (unlikely(ltd == NULL))
1411 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1417 RETURN(PTR_ERR(parent));
1419 LASSERT(dt_object_exists(parent));
1421 if (unlikely(!dt_try_as_dir(env, parent))) {
1422 lfsck_object_put(env, parent);
1424 GOTO(put, rc = -ENOTDIR);
1427 lfsck->li_lpf_root_obj = parent;
1429 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1431 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1432 "for bad sub-directories: rc = %d\n",
1433 lfsck_lfsck2name(lfsck), rc);
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) {
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);
1457 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1459 GOTO(put, rc = PTR_ERR(child2));
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);
1471 if (unlikely(!dt_try_as_dir(env, child2))) {
1472 lfsck_object_put(env, child2);
1478 if (fid_is_zero(&bk->lb_lpf_fid))
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;
1487 cname = lfsck_name_get_const(env, name, strlen(name));
1488 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1493 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1494 struct lu_fid tfid = bk->lb_lpf_fid;
1496 /* Invalid FID record in the bookmark file, reset it. */
1497 fid_zero(&bk->lb_lpf_fid);
1498 rc = lfsck_bookmark_store(env, lfsck);
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);
1510 child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1511 if (IS_ERR(child1)) {
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);
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);
1531 lfsck_object_put(env, child1);
1536 if (unlikely(!dt_try_as_dir(env, child1))) {
1537 lfsck_object_put(env, child1);
1543 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1545 if (lu_fid_eq(pfid, &LU_LPF_FID))
1550 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1551 pfid, LVLT_BY_NAMEENTRY);
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;
1562 } else if (rc == 0) {
1563 rc = lfsck_create_lpf(env, lfsck);
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);
1574 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1576 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1577 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1582 if (unlikely(ss == NULL))
1585 OBD_ALLOC_PTR(lfsck->li_seq);
1586 if (lfsck->li_seq == NULL)
1589 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1591 GOTO(out, rc = -ENOMEM);
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,
1596 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1600 if (fid_is_sane(&bk->lb_last_fid))
1601 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1606 OBD_FREE_PTR(lfsck->li_seq);
1607 lfsck->li_seq = NULL;
1612 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
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;
1621 void lfsck_instance_cleanup(const struct lu_env *env,
1622 struct lfsck_instance *lfsck)
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;
1632 LASSERT(list_empty(&lfsck->li_link));
1633 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1635 if (lfsck->li_obj_oit != NULL) {
1636 lfsck_object_put(env, lfsck->li_obj_oit);
1637 lfsck->li_obj_oit = NULL;
1640 LASSERT(lfsck->li_obj_dir == NULL);
1641 LASSERT(lfsck->li_lmv == NULL);
1643 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1644 llmv = &llu->llu_lmv;
1646 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1647 "still in using: %u\n",
1648 atomic_read(&llmv->ll_ref));
1650 lfsck_lmv_put(env, llmv);
1653 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1654 lfsck_component_cleanup(env, com);
1657 LASSERT(list_empty(&lfsck->li_list_dir));
1659 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1661 lfsck_component_cleanup(env, com);
1664 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1665 lfsck_component_cleanup(env, com);
1668 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1669 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1671 if (lfsck->li_lfsck_dir != NULL) {
1672 lfsck_object_put(env, lfsck->li_lfsck_dir);
1673 lfsck->li_lfsck_dir = NULL;
1676 if (lfsck->li_bookmark_obj != NULL) {
1677 lfsck_object_put(env, lfsck->li_bookmark_obj);
1678 lfsck->li_bookmark_obj = NULL;
1681 if (lfsck->li_lpf_obj != NULL) {
1682 lfsck_object_put(env, lfsck->li_lpf_obj);
1683 lfsck->li_lpf_obj = NULL;
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;
1691 if (lfsck->li_los != NULL) {
1692 local_oid_storage_fini(env, lfsck->li_los);
1693 lfsck->li_los = NULL;
1696 lfsck_fid_fini(lfsck);
1698 OBD_FREE_PTR(lfsck);
1701 static inline struct lfsck_instance *
1702 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1704 struct lfsck_instance *lfsck;
1706 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1707 if (lfsck->li_bottom == key) {
1709 lfsck_instance_get(lfsck);
1711 list_del_init(&lfsck->li_link);
1720 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1723 struct lfsck_instance *lfsck;
1725 spin_lock(&lfsck_instance_lock);
1726 lfsck = __lfsck_instance_find(key, ref, unlink);
1727 spin_unlock(&lfsck_instance_lock);
1732 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1734 struct lfsck_instance *tmp;
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);
1744 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1745 spin_unlock(&lfsck_instance_lock);
1749 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1754 bool newline = (bits != 0 ? false : true);
1756 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1758 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1761 if (names[i] != NULL) {
1765 seq_printf(m, "%s%c", names[i],
1766 newline ? '\n' : ',');
1775 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name)
1778 seq_printf(m, "%s_time: N/A\n", name);
1779 seq_printf(m, "time_since_%s: N/A\n", name);
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);
1787 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
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);
1795 seq_printf(m, "%s: %llu, N/A, N/A\n",
1796 prefix, pos->lp_oit_cookie);
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);
1804 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1805 struct lfsck_position *pos, bool init)
1807 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1809 if (unlikely(lfsck->li_di_oit == NULL)) {
1810 memset(pos, 0, sizeof(*pos));
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--;
1818 if (unlikely(pos->lp_oit_cookie == 0))
1819 pos->lp_oit_cookie = 1;
1821 if (lfsck->li_di_dir != NULL) {
1822 struct dt_object *dto = lfsck->li_obj_dir;
1824 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1827 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1828 fid_zero(&pos->lp_dir_parent);
1829 pos->lp_dir_cookie = 0;
1831 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1834 fid_zero(&pos->lp_dir_parent);
1835 pos->lp_dir_cookie = 0;
1839 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
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;
1849 lfsck->li_sleep_rate = 1;
1850 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1854 lfsck->li_sleep_jif = 0;
1855 lfsck->li_sleep_rate = 0;
1858 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1859 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1866 void lfsck_control_speed(struct lfsck_instance *lfsck)
1868 struct ptlrpc_thread *thread = &lfsck->li_thread;
1869 struct l_wait_info lwi;
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);
1876 l_wait_event(thread->t_ctl_waitq,
1877 !thread_is_running(thread),
1879 lfsck->li_new_scanned = 0;
1883 void lfsck_control_speed_by_self(struct lfsck_component *com)
1885 struct lfsck_instance *lfsck = com->lc_lfsck;
1886 struct ptlrpc_thread *thread = &lfsck->li_thread;
1887 struct l_wait_info lwi;
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);
1894 l_wait_event(thread->t_ctl_waitq,
1895 !thread_is_running(thread),
1897 com->lc_new_scanned = 0;
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)
1906 struct lfsck_thread_args *lta;
1911 return ERR_PTR(-ENOMEM);
1913 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1919 lta->lta_lfsck = lfsck_instance_get(lfsck);
1921 lta->lta_com = lfsck_component_get(com);
1928 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1930 if (lta->lta_com != NULL)
1931 lfsck_component_put(<a->lta_env, lta->lta_com);
1932 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1933 lu_env_fini(<a->lta_env);
1937 struct lfsck_assistant_data *
1938 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1941 struct lfsck_assistant_data *lad;
1945 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1946 if (lad->lad_bitmap == NULL) {
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);
1961 lad->lad_name = name;
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,
1972 struct lfsck_assistant_object *lso;
1976 return ERR_PTR(-ENOMEM);
1978 lso->lso_fid = *fid;
1980 lso->lso_attr = *attr;
1982 atomic_set(&lso->lso_ref, 1);
1983 lso->lso_oit_cookie = cookie;
1985 lso->lso_is_dir = 1;
1991 lfsck_assistant_object_load(const struct lu_env *env,
1992 struct lfsck_instance *lfsck,
1993 struct lfsck_assistant_object *lso)
1995 struct dt_object *obj;
1997 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2001 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2003 lfsck_object_put(env, obj);
2005 return ERR_PTR(-ENOENT);
2008 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2009 lfsck_object_put(env, obj);
2011 return ERR_PTR(-ENOTDIR);
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.
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
2027 * \retval 0 for success
2028 * \retval negative error number on failure
2030 int lfsck_async_interpret_common(const struct lu_env *env,
2031 struct ptlrpc_request *req,
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;
2041 LASSERT(com->lc_lfsck->li_master);
2043 switch (lr->lr_event) {
2045 if (unlikely(rc == -EINPROGRESS)) {
2046 ltd->ltd_retry_start = 1;
2051 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2053 lfsck_lfsck2name(com->lc_lfsck),
2054 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2055 ltd->ltd_index, lad->lad_name, rc);
2057 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2058 struct lfsck_layout *lo = com->lc_file_ram;
2060 if (lr->lr_flags & LEF_TO_OST)
2061 lfsck_lad_set_bitmap(env, com,
2064 lo->ll_flags |= LF_INCOMPLETE;
2066 struct lfsck_namespace *ns = com->lc_file_ram;
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;
2081 spin_lock(<ds->ltd_lock);
2082 if (ltd->ltd_dead) {
2083 spin_unlock(<ds->ltd_lock);
2087 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2088 struct list_head *list;
2089 struct list_head *phase_list;
2091 if (ltd->ltd_layout_done) {
2092 spin_unlock(<ds->ltd_lock);
2096 if (lr->lr_flags & LEF_TO_OST) {
2097 list = &lad->lad_ost_list;
2098 phase_list = &lad->lad_ost_phase1_list;
2100 list = &lad->lad_mdt_list;
2101 phase_list = &lad->lad_mdt_phase1_list;
2104 if (list_empty(<d->ltd_layout_list))
2105 list_add_tail(<d->ltd_layout_list, list);
2106 if (list_empty(<d->ltd_layout_phase_list))
2107 list_add_tail(<d->ltd_layout_phase_list,
2110 if (ltd->ltd_namespace_done) {
2111 spin_unlock(<ds->ltd_lock);
2115 if (list_empty(<d->ltd_namespace_list))
2116 list_add_tail(<d->ltd_namespace_list,
2117 &lad->lad_mdt_list);
2118 if (list_empty(<d->ltd_namespace_phase_list))
2119 list_add_tail(<d->ltd_namespace_phase_list,
2120 &lad->lad_mdt_phase1_list);
2122 spin_unlock(<ds->ltd_lock);
2125 case LE_PHASE1_DONE:
2126 case LE_PHASE2_DONE:
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);
2136 struct lfsck_reply *reply;
2137 struct list_head *list;
2138 struct list_head *phase_list;
2140 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2141 list = <d->ltd_layout_list;
2142 phase_list = <d->ltd_layout_phase_list;
2144 list = <d->ltd_namespace_list;
2145 phase_list = <d->ltd_namespace_phase_list;
2149 if (lr->lr_flags & LEF_QUERY_ALL) {
2150 lfsck_reset_ltd_status(ltd, com->lc_type);
2154 spin_lock(<ds->ltd_lock);
2155 list_del_init(phase_list);
2156 list_del_init(list);
2157 spin_unlock(<ds->ltd_lock);
2161 reply = req_capsule_server_get(&req->rq_pill,
2163 if (reply == NULL) {
2165 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2166 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2169 if (lr->lr_flags & LEF_QUERY_ALL) {
2170 lfsck_reset_ltd_status(ltd, com->lc_type);
2174 spin_lock(<ds->ltd_lock);
2175 list_del_init(phase_list);
2176 list_del_init(list);
2177 spin_unlock(<ds->ltd_lock);
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;
2186 ltd->ltd_namespace_status = reply->lr_status;
2187 ltd->ltd_namespace_repaired =
2193 switch (reply->lr_status) {
2194 case LS_SCANNING_PHASE1:
2196 case LS_SCANNING_PHASE2:
2197 spin_lock(<ds->ltd_lock);
2198 list_del_init(phase_list);
2199 if (ltd->ltd_dead) {
2200 spin_unlock(<ds->ltd_lock);
2204 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2205 if (ltd->ltd_layout_done) {
2206 spin_unlock(<ds->ltd_lock);
2210 if (lr->lr_flags & LEF_TO_OST)
2211 list_add_tail(phase_list,
2212 &lad->lad_ost_phase2_list);
2214 list_add_tail(phase_list,
2215 &lad->lad_mdt_phase2_list);
2217 if (ltd->ltd_namespace_done) {
2218 spin_unlock(<ds->ltd_lock);
2222 list_add_tail(phase_list,
2223 &lad->lad_mdt_phase2_list);
2225 spin_unlock(<ds->ltd_lock);
2228 spin_lock(<ds->ltd_lock);
2229 list_del_init(phase_list);
2230 list_del_init(list);
2231 spin_unlock(<ds->ltd_lock);
2237 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2238 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2242 if (!laia->laia_shared) {
2244 lfsck_component_put(env, com);
2250 static void lfsck_interpret(const struct lu_env *env,
2251 struct lfsck_instance *lfsck,
2252 struct ptlrpc_request *req, void *args, int result)
2254 struct lfsck_async_interpret_args *laia = args;
2255 struct lfsck_component *com;
2257 LASSERT(laia->laia_com == NULL);
2258 LASSERT(laia->laia_shared);
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);
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);
2270 spin_unlock(&lfsck->li_lock);
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)
2278 struct lfsck_component *com;
2282 LASSERT(lfsck->li_master);
2284 spin_lock(&lfsck->li_lock);
2285 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2287 com = __lfsck_component_find(lfsck, type,
2288 &lfsck->li_list_double_scan);
2290 lfsck_component_get(com);
2291 spin_unlock(&lfsck->li_lock);
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;
2302 set = ptlrpc_prep_set();
2304 lfsck_component_put(env, com);
2309 if (type == LFSCK_TYPE_LAYOUT) {
2310 list = <d->ltd_layout_list;
2311 phase_list = <d->ltd_layout_phase_list;
2313 list = <d->ltd_namespace_list;
2314 phase_list = <d->ltd_namespace_phase_list;
2317 spin_lock(<ds->ltd_lock);
2318 if (list_empty(list)) {
2319 LASSERT(list_empty(phase_list));
2320 spin_unlock(<ds->ltd_lock);
2321 ptlrpc_set_destroy(set);
2326 list_del_init(phase_list);
2327 list_del_init(list);
2328 spin_unlock(<ds->ltd_lock);
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;
2338 memset(laia, 0, sizeof(*laia));
2339 laia->laia_com = com;
2340 laia->laia_ltds = ltds;
2341 atomic_inc(<d->ltd_ref);
2342 laia->laia_ltd = ltd;
2345 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2346 lfsck_async_interpret_common,
2347 laia, LFSCK_NOTIFY);
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);
2356 rc = ptlrpc_set_wait(env, set);
2359 ptlrpc_set_destroy(set);
2360 lfsck_component_put(env, com);
2366 static int lfsck_async_interpret(const struct lu_env *env,
2367 struct ptlrpc_request *req,
2370 struct lfsck_async_interpret_args *laia = args;
2371 struct lfsck_instance *lfsck;
2373 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
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;
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)
2389 struct lfsck_async_interpret_args *laia;
2390 struct ptlrpc_request *req;
2391 struct lfsck_request *tmp;
2392 struct req_format *format;
2397 format = &RQF_LFSCK_NOTIFY;
2400 format = &RQF_LFSCK_QUERY;
2403 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2404 exp->exp_obd->obd_name, request, -EINVAL);
2408 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2412 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2414 ptlrpc_request_free(req);
2419 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2421 ptlrpc_request_set_replen(req);
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);
2435 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
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;
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;
2453 memset(laia, 0, sizeof(*laia));
2454 laia->laia_com = com;
2457 set = ptlrpc_prep_set();
2462 laia->laia_ltds = ltds;
2463 down_read(<ds->ltd_rw_sem);
2464 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2465 ltd = lfsck_tgt_get(ltds, idx);
2466 LASSERT(ltd != NULL);
2468 laia->laia_ltd = ltd;
2469 up_read(<ds->ltd_rw_sem);
2470 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2471 lfsck_async_interpret_common,
2474 struct lfsck_assistant_data *lad = com->lc_data;
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);
2483 down_read(<ds->ltd_rw_sem);
2485 up_read(<ds->ltd_rw_sem);
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;
2493 rc = ptlrpc_set_wait(env, set);
2494 ptlrpc_set_destroy(set);
2499 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2500 struct lfsck_start_param *lsp)
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;
2511 lad->lad_assistant_status = 0;
2512 lad->lad_post_result = 0;
2513 lad->lad_to_post = 0;
2514 lad->lad_to_double_scan = 0;
2515 lad->lad_in_double_scan = 0;
2517 lad->lad_advance_lock = false;
2518 thread_set_flags(athread, 0);
2520 lta = lfsck_thread_args_init(lfsck, com, lsp);
2522 RETURN(PTR_ERR(lta));
2524 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2527 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2528 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2529 lfsck_thread_args_fini(lta);
2531 struct l_wait_info lwi = { 0 };
2533 l_wait_event(mthread->t_ctl_waitq,
2534 thread_is_running(athread) ||
2535 thread_is_stopped(athread) ||
2536 !thread_is_starting(mthread),
2538 if (unlikely(!thread_is_starting(mthread)))
2539 /* stopped by race */
2541 else if (unlikely(!thread_is_running(athread)))
2542 rc = lad->lad_assistant_status;
2550 int lfsck_checkpoint_generic(const struct lu_env *env,
2551 struct lfsck_component *com)
2553 struct lfsck_assistant_data *lad = com->lc_data;
2554 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2555 struct ptlrpc_thread *athread = &lad->lad_thread;
2556 struct l_wait_info lwi = { 0 };
2558 l_wait_event(mthread->t_ctl_waitq,
2559 list_empty(&lad->lad_req_list) ||
2560 !thread_is_running(mthread) ||
2561 thread_is_stopped(athread),
2564 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2565 return LFSCK_CHECKPOINT_SKIP;
2570 void lfsck_post_generic(const struct lu_env *env,
2571 struct lfsck_component *com, int *result)
2573 struct lfsck_assistant_data *lad = com->lc_data;
2574 struct ptlrpc_thread *athread = &lad->lad_thread;
2575 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2576 struct l_wait_info lwi = { 0 };
2578 lad->lad_post_result = *result;
2581 lad->lad_to_post = 1;
2583 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2584 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2586 wake_up_all(&athread->t_ctl_waitq);
2587 l_wait_event(mthread->t_ctl_waitq,
2588 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2589 thread_is_stopped(athread),
2592 if (lad->lad_assistant_status < 0)
2593 *result = lad->lad_assistant_status;
2595 CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2596 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2599 int lfsck_double_scan_generic(const struct lu_env *env,
2600 struct lfsck_component *com, int status)
2602 struct lfsck_assistant_data *lad = com->lc_data;
2603 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2604 struct ptlrpc_thread *athread = &lad->lad_thread;
2605 struct l_wait_info lwi = { 0 };
2607 if (status != LS_SCANNING_PHASE2)
2610 lad->lad_to_double_scan = 1;
2612 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2614 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2616 wake_up_all(&athread->t_ctl_waitq);
2617 l_wait_event(mthread->t_ctl_waitq,
2618 lad->lad_in_double_scan ||
2619 thread_is_stopped(athread),
2622 CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2623 "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2624 lad->lad_assistant_status);
2626 if (lad->lad_assistant_status < 0)
2627 return lad->lad_assistant_status;
2632 void lfsck_quit_generic(const struct lu_env *env,
2633 struct lfsck_component *com)
2635 struct lfsck_assistant_data *lad = com->lc_data;
2636 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2637 struct ptlrpc_thread *athread = &lad->lad_thread;
2638 struct l_wait_info lwi = { 0 };
2641 wake_up_all(&athread->t_ctl_waitq);
2642 l_wait_event(mthread->t_ctl_waitq,
2643 thread_is_init(athread) ||
2644 thread_is_stopped(athread),
2648 int lfsck_load_one_trace_file(const struct lu_env *env,
2649 struct lfsck_component *com,
2650 struct dt_object *parent,
2651 struct dt_object **child,
2652 const struct dt_index_features *ft,
2653 const char *name, bool reset)
2655 struct lfsck_instance *lfsck = com->lc_lfsck;
2656 struct dt_object *obj;
2660 if (*child != NULL) {
2662 const struct dt_it_ops *iops;
2663 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2669 rc = obj->do_ops->do_index_try(env, obj, ft);
2671 /* unlink by force */
2674 iops = &obj->do_index_ops->dio_it;
2675 it = iops->init(env, obj, 0);
2677 /* unlink by force */
2681 rc = iops->get(env, it, (const struct dt_key *)fid);
2683 rc = iops->next(env, it);
2686 iops->fini(env, it);
2688 /* "rc > 0" means the index file is empty. */
2692 /* The old index is not empty, remove it firstly. */
2693 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2694 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2695 "%s: unlink lfsck sub trace file %s: rc = %d\n",
2696 lfsck_lfsck2name(com->lc_lfsck), name, rc);
2701 lfsck_object_put(env, *child);
2708 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2709 S_IFREG | S_IRUGO | S_IWUSR, ft);
2711 RETURN(PTR_ERR(obj));
2713 rc = obj->do_ops->do_index_try(env, obj, ft);
2715 lfsck_object_put(env, obj);
2716 CDEBUG(D_LFSCK, "%s: LFSCK fail to load "
2717 "sub trace file %s: rc = %d\n",
2718 lfsck_lfsck2name(com->lc_lfsck), name, rc);
2726 int lfsck_load_sub_trace_files(const struct lu_env *env,
2727 struct lfsck_component *com,
2728 const struct dt_index_features *ft,
2729 const char *prefix, bool reset)
2731 char *name = lfsck_env_info(env)->lti_key;
2732 struct lfsck_sub_trace_obj *lsto;
2736 for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2737 i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2738 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2739 rc = lfsck_load_one_trace_file(env, com,
2740 com->lc_lfsck->li_lfsck_dir,
2741 &lsto->lsto_obj, ft, name, reset);
2747 /* external interfaces */
2748 int lfsck_get_speed(struct seq_file *m, char *buf, struct dt_device *key)
2751 struct lfsck_instance *lfsck;
2755 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2759 lfsck = lfsck_instance_find(key, true, false);
2760 if (likely(lfsck != NULL)) {
2762 seq_printf(m, "%u\n",
2763 lfsck->li_bookmark_ram.lb_speed_limit);
2765 rc = sprintf(buf, "%u\n",
2766 lfsck->li_bookmark_ram.lb_speed_limit);
2768 lfsck_instance_put(&env, lfsck);
2777 EXPORT_SYMBOL(lfsck_get_speed);
2779 int lfsck_set_speed(struct dt_device *key, __u32 val)
2782 struct lfsck_instance *lfsck;
2786 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2790 lfsck = lfsck_instance_find(key, true, false);
2791 if (likely(lfsck != NULL)) {
2792 mutex_lock(&lfsck->li_mutex);
2793 if (__lfsck_set_speed(lfsck, val))
2794 rc = lfsck_bookmark_store(&env, lfsck);
2795 mutex_unlock(&lfsck->li_mutex);
2796 lfsck_instance_put(&env, lfsck);
2805 EXPORT_SYMBOL(lfsck_set_speed);
2807 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2810 struct lfsck_instance *lfsck;
2814 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2818 lfsck = lfsck_instance_find(key, true, false);
2819 if (likely(lfsck != NULL)) {
2820 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2821 lfsck_instance_put(&env, lfsck);
2830 EXPORT_SYMBOL(lfsck_get_windows);
2832 int lfsck_set_windows(struct dt_device *key, unsigned int val)
2835 struct lfsck_instance *lfsck;
2839 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2843 lfsck = lfsck_instance_find(key, true, false);
2844 if (likely(lfsck != NULL)) {
2845 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2846 CWARN("%s: invalid async windows size that may "
2847 "cause memory issues. The valid range is "
2849 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2851 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2852 mutex_lock(&lfsck->li_mutex);
2853 lfsck->li_bookmark_ram.lb_async_windows = val;
2854 rc = lfsck_bookmark_store(&env, lfsck);
2855 mutex_unlock(&lfsck->li_mutex);
2857 lfsck_instance_put(&env, lfsck);
2866 EXPORT_SYMBOL(lfsck_set_windows);
2868 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2871 struct lfsck_instance *lfsck;
2872 struct lfsck_component *com;
2876 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2880 lfsck = lfsck_instance_find(key, true, false);
2881 if (likely(lfsck != NULL)) {
2882 com = lfsck_component_find(lfsck, type);
2883 if (likely(com != NULL)) {
2884 com->lc_ops->lfsck_dump(&env, com, m);
2885 lfsck_component_put(&env, com);
2890 lfsck_instance_put(&env, lfsck);
2899 EXPORT_SYMBOL(lfsck_dump);
2901 static int lfsck_stop_all(const struct lu_env *env,
2902 struct lfsck_instance *lfsck,
2903 struct lfsck_stop *stop)
2905 struct lfsck_thread_info *info = lfsck_env_info(env);
2906 struct lfsck_request *lr = &info->lti_lr;
2907 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2908 struct ptlrpc_request_set *set;
2909 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2910 struct lfsck_tgt_desc *ltd;
2911 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2917 LASSERT(stop->ls_flags & LPF_BROADCAST);
2919 set = ptlrpc_prep_set();
2920 if (unlikely(set == NULL))
2923 memset(lr, 0, sizeof(*lr));
2924 lr->lr_event = LE_STOP;
2925 lr->lr_index = lfsck_dev_idx(lfsck);
2926 lr->lr_status = stop->ls_status;
2927 lr->lr_version = bk->lb_version;
2928 lr->lr_active = LFSCK_TYPES_ALL;
2929 lr->lr_param = stop->ls_flags;
2931 memset(laia, 0, sizeof(*laia));
2932 laia->laia_ltds = ltds;
2934 laia->laia_shared = 1;
2936 down_read(<ds->ltd_rw_sem);
2937 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2938 ltd = lfsck_tgt_get(ltds, idx);
2939 LASSERT(ltd != NULL);
2941 laia->laia_ltd = ltd;
2942 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2943 lfsck_async_interpret, laia,
2946 lfsck_interpret(env, lfsck, NULL, laia, rc);
2948 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2949 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2953 up_read(<ds->ltd_rw_sem);
2955 rc = ptlrpc_set_wait(env, set);
2956 ptlrpc_set_destroy(set);
2959 rc = laia->laia_result;
2961 if (rc == -EALREADY)
2965 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2966 lfsck_lfsck2name(lfsck), rc);
2968 RETURN(rc != 0 ? rc : rc1);
2971 static int lfsck_start_all(const struct lu_env *env,
2972 struct lfsck_instance *lfsck,
2973 struct lfsck_start *start)
2975 struct lfsck_thread_info *info = lfsck_env_info(env);
2976 struct lfsck_request *lr = &info->lti_lr;
2977 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2978 struct ptlrpc_request_set *set;
2979 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2980 struct lfsck_tgt_desc *ltd;
2981 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2987 LASSERT(start->ls_flags & LPF_BROADCAST);
2989 memset(lr, 0, sizeof(*lr));
2990 lr->lr_event = LE_START;
2991 lr->lr_index = lfsck_dev_idx(lfsck);
2992 lr->lr_speed = bk->lb_speed_limit;
2993 lr->lr_version = bk->lb_version;
2994 lr->lr_active = start->ls_active;
2995 lr->lr_param = start->ls_flags;
2996 lr->lr_async_windows = bk->lb_async_windows;
2997 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2998 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
3001 memset(laia, 0, sizeof(*laia));
3002 laia->laia_ltds = ltds;
3004 laia->laia_shared = 1;
3007 set = ptlrpc_prep_set();
3011 down_read(<ds->ltd_rw_sem);
3012 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3013 ltd = lfsck_tgt_get(ltds, idx);
3014 LASSERT(ltd != NULL);
3016 if (retry && !ltd->ltd_retry_start) {
3021 laia->laia_ltd = ltd;
3022 ltd->ltd_retry_start = 0;
3023 ltd->ltd_layout_done = 0;
3024 ltd->ltd_namespace_done = 0;
3025 ltd->ltd_synced_failures = 0;
3026 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3027 lfsck_async_interpret, laia,
3030 lfsck_interpret(env, lfsck, NULL, laia, rc);
3032 CERROR("%s: cannot notify MDT %x for LFSCK "
3033 "start, failout: rc = %d\n",
3034 lfsck_lfsck2name(lfsck), idx, rc);
3038 up_read(<ds->ltd_rw_sem);
3041 ptlrpc_set_destroy(set);
3046 rc = ptlrpc_set_wait(env, set);
3047 ptlrpc_set_destroy(set);
3050 rc = laia->laia_result;
3052 if (unlikely(rc == -EINPROGRESS)) {
3054 set_current_state(TASK_INTERRUPTIBLE);
3055 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC));
3056 set_current_state(TASK_RUNNING);
3057 if (!signal_pending(current) &&
3058 thread_is_running(&lfsck->li_thread))
3065 struct lfsck_stop *stop = &info->lti_stop;
3067 CERROR("%s: cannot start LFSCK on some MDTs, "
3068 "stop all: rc = %d\n",
3069 lfsck_lfsck2name(lfsck), rc);
3070 if (rc != -EALREADY) {
3071 stop->ls_status = LS_FAILED;
3072 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3073 lfsck_stop_all(env, lfsck, stop);
3080 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3081 struct lfsck_start_param *lsp)
3083 struct lfsck_start *start = lsp->lsp_start;
3084 struct lfsck_instance *lfsck;
3085 struct lfsck_bookmark *bk;
3086 struct ptlrpc_thread *thread;
3087 struct lfsck_component *com;
3088 struct l_wait_info lwi = { 0 };
3089 struct lfsck_thread_args *lta;
3090 struct task_struct *task;
3091 struct lfsck_tgt_descs *ltds;
3092 struct lfsck_tgt_desc *ltd;
3103 lfsck = lfsck_instance_find(key, true, false);
3104 if (unlikely(lfsck == NULL))
3107 if (unlikely(lfsck->li_stopping))
3108 GOTO(put, rc = -ENXIO);
3110 /* System is not ready, try again later. */
3111 if (unlikely(lfsck->li_namespace == NULL ||
3112 lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3113 GOTO(put, rc = -EINPROGRESS);
3115 /* start == NULL means auto trigger paused LFSCK. */
3117 if (list_empty(&lfsck->li_list_scan) ||
3118 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3120 } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3121 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3122 lfsck_lfsck2name(lfsck));
3124 GOTO(put, rc = -EPERM);
3127 bk = &lfsck->li_bookmark_ram;
3128 thread = &lfsck->li_thread;
3129 mutex_lock(&lfsck->li_mutex);
3130 spin_lock(&lfsck->li_lock);
3131 if (unlikely(thread_is_stopping(thread))) {
3132 /* Someone is stopping the LFSCK. */
3133 spin_unlock(&lfsck->li_lock);
3134 GOTO(out, rc = -EBUSY);
3137 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3139 if (unlikely(start == NULL)) {
3140 spin_unlock(&lfsck->li_lock);
3144 while (start->ls_active != 0) {
3145 if (!(type & start->ls_active)) {
3150 com = __lfsck_component_find(lfsck, type,
3151 &lfsck->li_list_scan);
3153 com = __lfsck_component_find(lfsck, type,
3154 &lfsck->li_list_double_scan);
3160 if (com->lc_ops->lfsck_join != NULL) {
3161 rc = com->lc_ops->lfsck_join( env, com, lsp);
3162 if (rc != 0 && rc != -EALREADY)
3165 start->ls_active &= ~type;
3168 spin_unlock(&lfsck->li_lock);
3171 spin_unlock(&lfsck->li_lock);
3173 lfsck->li_status = 0;
3174 lfsck->li_oit_over = 0;
3175 lfsck->li_start_unplug = 0;
3176 lfsck->li_drop_dryrun = 0;
3177 lfsck->li_new_scanned = 0;
3179 /* For auto trigger. */
3183 start->ls_version = bk->lb_version;
3185 if (start->ls_active != 0) {
3186 struct lfsck_component *next;
3188 if (start->ls_active == LFSCK_TYPES_ALL)
3189 start->ls_active = LFSCK_TYPES_SUPPORTED;
3191 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3192 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3193 GOTO(out, rc = -ENOTSUPP);
3196 list_for_each_entry_safe(com, next,
3197 &lfsck->li_list_scan, lc_link) {
3198 if (!(com->lc_type & start->ls_active)) {
3199 rc = com->lc_ops->lfsck_post(env, com, 0,
3206 while (start->ls_active != 0) {
3207 if (type & start->ls_active) {
3208 com = __lfsck_component_find(lfsck, type,
3209 &lfsck->li_list_idle);
3211 /* The component status will be updated
3212 * when its prep() is called later by
3213 * the LFSCK main engine. */
3214 list_move_tail(&com->lc_link,
3215 &lfsck->li_list_scan);
3216 start->ls_active &= ~type;
3222 if (list_empty(&lfsck->li_list_scan)) {
3223 /* The speed limit will be used to control both the LFSCK and
3224 * low layer scrub (if applied), need to be handled firstly. */
3225 if (start->ls_valid & LSV_SPEED_LIMIT) {
3226 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3227 rc = lfsck_bookmark_store(env, lfsck);
3236 if (start->ls_flags & LPF_RESET)
3237 flags |= DOIF_RESET;
3239 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3243 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3244 start->ls_active |= com->lc_type;
3245 if (flags & DOIF_RESET) {
3246 rc = com->lc_ops->lfsck_reset(env, com, false);
3252 ltds = &lfsck->li_mdt_descs;
3253 down_read(<ds->ltd_rw_sem);
3254 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3255 ltd = lfsck_ltd2tgt(ltds, idx);
3256 LASSERT(ltd != NULL);
3258 ltd->ltd_layout_done = 0;
3259 ltd->ltd_namespace_done = 0;
3260 ltd->ltd_synced_failures = 0;
3261 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3262 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3263 list_del_init(<d->ltd_layout_phase_list);
3264 list_del_init(<d->ltd_layout_list);
3265 list_del_init(<d->ltd_namespace_phase_list);
3266 list_del_init(<d->ltd_namespace_list);
3268 up_read(<ds->ltd_rw_sem);
3270 ltds = &lfsck->li_ost_descs;
3271 down_read(<ds->ltd_rw_sem);
3272 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3273 ltd = lfsck_ltd2tgt(ltds, idx);
3274 LASSERT(ltd != NULL);
3276 ltd->ltd_layout_done = 0;
3277 ltd->ltd_synced_failures = 0;
3278 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3279 list_del_init(<d->ltd_layout_phase_list);
3280 list_del_init(<d->ltd_layout_list);
3282 up_read(<ds->ltd_rw_sem);
3285 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3286 if (bk->lb_param & LPF_DRYRUN)
3287 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3289 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3290 valid |= DOIV_ERROR_HANDLE;
3291 if (start->ls_flags & LPF_FAILOUT)
3292 flags |= DOIF_FAILOUT;
3295 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3296 valid |= DOIV_DRYRUN;
3297 if (start->ls_flags & LPF_DRYRUN)
3298 flags |= DOIF_DRYRUN;
3301 if (!list_empty(&lfsck->li_list_scan))
3302 flags |= DOIF_OUTUSED;
3304 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3305 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3307 GOTO(out, rc = PTR_ERR(lta));
3309 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3310 spin_lock(&lfsck->li_lock);
3311 thread_set_flags(thread, SVC_STARTING);
3312 spin_unlock(&lfsck->li_lock);
3313 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3316 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3317 lfsck_lfsck2name(lfsck), rc);
3318 lfsck_thread_args_fini(lta);
3323 l_wait_event(thread->t_ctl_waitq,
3324 thread_is_running(thread) ||
3325 thread_is_stopped(thread),
3327 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3328 lfsck->li_start_unplug = 1;
3329 wake_up_all(&thread->t_ctl_waitq);
3334 /* release lfsck::li_mutex to avoid deadlock. */
3335 mutex_unlock(&lfsck->li_mutex);
3336 rc = lfsck_start_all(env, lfsck, start);
3338 spin_lock(&lfsck->li_lock);
3339 if (thread_is_stopped(thread)) {
3340 spin_unlock(&lfsck->li_lock);
3342 lfsck->li_status = LS_FAILED;
3343 lfsck->li_flags = 0;
3344 thread_set_flags(thread, SVC_STOPPING);
3345 spin_unlock(&lfsck->li_lock);
3347 lfsck->li_start_unplug = 1;
3348 wake_up_all(&thread->t_ctl_waitq);
3349 l_wait_event(thread->t_ctl_waitq,
3350 thread_is_stopped(thread),
3354 lfsck->li_start_unplug = 1;
3355 wake_up_all(&thread->t_ctl_waitq);
3361 mutex_unlock(&lfsck->li_mutex);
3364 lfsck_instance_put(env, lfsck);
3366 return rc < 0 ? rc : 0;
3368 EXPORT_SYMBOL(lfsck_start);
3370 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3371 struct lfsck_stop *stop)
3373 struct lfsck_instance *lfsck;
3374 struct ptlrpc_thread *thread;
3375 struct l_wait_info lwi = { 0 };
3380 lfsck = lfsck_instance_find(key, true, false);
3381 if (unlikely(lfsck == NULL))
3384 thread = &lfsck->li_thread;
3385 if (stop && stop->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3386 CERROR("%s: only allow to specify '-A' via MDS\n",
3387 lfsck_lfsck2name(lfsck));
3388 GOTO(put, rc = -EPERM);
3391 spin_lock(&lfsck->li_lock);
3392 /* The target is umounted */
3393 if (stop && stop->ls_status == LS_PAUSED)
3394 lfsck->li_stopping = 1;
3396 if (thread_is_init(thread) || thread_is_stopped(thread))
3397 /* no error if LFSCK stopped already, or not started */
3398 GOTO(unlock, rc = 0);
3400 if (thread_is_stopping(thread))
3401 /* Someone is stopping LFSCK. */
3402 GOTO(unlock, rc = -EINPROGRESS);
3405 lfsck->li_status = stop->ls_status;
3406 lfsck->li_flags = stop->ls_flags;
3408 lfsck->li_status = LS_STOPPED;
3409 lfsck->li_flags = 0;
3412 thread_set_flags(thread, SVC_STOPPING);
3414 LASSERT(lfsck->li_task != NULL);
3415 force_sig(SIGINT, lfsck->li_task);
3417 if (lfsck->li_master) {
3418 struct lfsck_component *com;
3419 struct lfsck_assistant_data *lad;
3421 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3423 spin_lock(&lad->lad_lock);
3424 if (lad->lad_task != NULL)
3425 force_sig(SIGINT, lad->lad_task);
3426 spin_unlock(&lad->lad_lock);
3429 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3431 spin_lock(&lad->lad_lock);
3432 if (lad->lad_task != NULL)
3433 force_sig(SIGINT, lad->lad_task);
3434 spin_unlock(&lad->lad_lock);
3438 wake_up_all(&thread->t_ctl_waitq);
3439 spin_unlock(&lfsck->li_lock);
3440 if (stop && stop->ls_flags & LPF_BROADCAST)
3441 rc1 = lfsck_stop_all(env, lfsck, stop);
3443 /* It was me set the status as 'stopping' just now, if it is not
3444 * 'stopping' now, then either stopped, or re-started by race. */
3445 l_wait_event(thread->t_ctl_waitq,
3446 !thread_is_stopping(thread),
3452 spin_unlock(&lfsck->li_lock);
3454 lfsck_instance_put(env, lfsck);
3456 return rc != 0 ? rc : rc1;
3458 EXPORT_SYMBOL(lfsck_stop);
3460 int lfsck_in_notify_local(const struct lu_env *env, struct dt_device *key,
3461 struct lfsck_req_local *lrl, struct thandle *th)
3463 struct lfsck_instance *lfsck;
3464 struct lfsck_component *com;
3465 int rc = -EOPNOTSUPP;
3468 lfsck = lfsck_instance_find(key, true, false);
3469 if (unlikely(!lfsck))
3472 com = lfsck_component_find(lfsck, lrl->lrl_active);
3473 if (likely(com && com->lc_ops->lfsck_in_notify_local)) {
3474 rc = com->lc_ops->lfsck_in_notify_local(env, com, lrl, th);
3475 lfsck_component_put(env, com);
3478 lfsck_instance_put(env, lfsck);
3482 EXPORT_SYMBOL(lfsck_in_notify_local);
3484 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3485 struct lfsck_request *lr)
3487 int rc = -EOPNOTSUPP;
3490 switch (lr->lr_event) {
3492 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3493 struct lfsck_start_param lsp;
3495 memset(start, 0, sizeof(*start));
3496 start->ls_valid = lr->lr_valid;
3497 start->ls_speed_limit = lr->lr_speed;
3498 start->ls_version = lr->lr_version;
3499 start->ls_active = lr->lr_active;
3500 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3501 start->ls_async_windows = lr->lr_async_windows;
3503 lsp.lsp_start = start;
3504 lsp.lsp_index = lr->lr_index;
3505 lsp.lsp_index_valid = 1;
3506 rc = lfsck_start(env, key, &lsp);
3510 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3512 memset(stop, 0, sizeof(*stop));
3513 stop->ls_status = lr->lr_status;
3514 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3515 rc = lfsck_stop(env, key, stop);
3518 case LE_PHASE1_DONE:
3519 case LE_PHASE2_DONE:
3521 case LE_CONDITIONAL_DESTROY:
3522 case LE_SET_LMV_MASTER:
3523 case LE_SET_LMV_SLAVE:
3524 case LE_PAIRS_VERIFY: {
3525 struct lfsck_instance *lfsck;
3526 struct lfsck_component *com;
3528 lfsck = lfsck_instance_find(key, true, false);
3529 if (unlikely(lfsck == NULL))
3532 com = lfsck_component_find(lfsck, lr->lr_active);
3534 rc = com->lc_ops->lfsck_in_notify(env, com, lr);
3535 lfsck_component_put(env, com);
3538 lfsck_instance_put(env, lfsck);
3547 EXPORT_SYMBOL(lfsck_in_notify);
3549 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3550 struct lfsck_request *req, struct lfsck_reply *rep,
3551 struct lfsck_query *que)
3553 struct lfsck_instance *lfsck;
3554 struct lfsck_component *com;
3560 lfsck = lfsck_instance_find(key, true, false);
3561 if (unlikely(lfsck == NULL))
3565 if (que->lu_types == LFSCK_TYPES_ALL)
3567 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3569 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3570 que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3572 GOTO(out, rc = -ENOTSUPP);
3575 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3576 i++, type = 1 << i) {
3577 if (!(que->lu_types & type))
3581 com = lfsck_component_find(lfsck, type);
3582 if (unlikely(com == NULL))
3583 GOTO(out, rc = -ENOTSUPP);
3585 memset(que->lu_mdts_count[i], 0,
3586 sizeof(__u32) * (LS_MAX + 1));
3587 memset(que->lu_osts_count[i], 0,
3588 sizeof(__u32) * (LS_MAX + 1));
3589 que->lu_repaired[i] = 0;
3590 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3592 lfsck_component_put(env, com);
3597 if (!(que->lu_flags & LPF_WAIT))
3600 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3601 i++, type = 1 << i) {
3602 if (!(que->lu_types & type))
3605 if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3606 que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3607 que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3608 que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3609 struct l_wait_info lwi;
3611 /* If it is required to wait, then sleep
3612 * 3 seconds and try to query again. */
3613 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3617 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3619 if (rc == -ETIMEDOUT)
3624 com = lfsck_component_find(lfsck, req->lr_active);
3625 if (likely(com != NULL)) {
3626 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3628 lfsck_component_put(env, com);
3637 lfsck_instance_put(env, lfsck);
3640 EXPORT_SYMBOL(lfsck_query);
3642 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3643 struct ldlm_namespace *ns)
3645 struct lfsck_instance *lfsck;
3648 lfsck = lfsck_instance_find(key, true, false);
3649 if (likely(lfsck != NULL)) {
3650 lfsck->li_namespace = ns;
3651 lfsck_instance_put(env, lfsck);
3657 EXPORT_SYMBOL(lfsck_register_namespace);
3659 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3660 struct dt_device *next, struct obd_device *obd,
3661 lfsck_out_notify notify, void *notify_data, bool master)
3663 struct lfsck_instance *lfsck;
3664 struct dt_object *root = NULL;
3665 struct dt_object *obj = NULL;
3666 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3670 lfsck = lfsck_instance_find(key, false, false);
3671 if (unlikely(lfsck != NULL))
3674 OBD_ALLOC_PTR(lfsck);
3678 mutex_init(&lfsck->li_mutex);
3679 spin_lock_init(&lfsck->li_lock);
3680 INIT_LIST_HEAD(&lfsck->li_link);
3681 INIT_LIST_HEAD(&lfsck->li_list_scan);
3682 INIT_LIST_HEAD(&lfsck->li_list_dir);
3683 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3684 INIT_LIST_HEAD(&lfsck->li_list_idle);
3685 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3686 atomic_set(&lfsck->li_ref, 1);
3687 atomic_set(&lfsck->li_double_scan_count, 0);
3688 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3689 lfsck->li_out_notify = notify;
3690 lfsck->li_out_notify_data = notify_data;
3691 lfsck->li_next = next;
3692 lfsck->li_bottom = key;
3693 lfsck->li_obd = obd;