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);
383 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
384 LDLM_IBITS, policy, mode,
385 &flags, ldlm_blocking_ast,
386 ldlm_completion_ast, NULL, NULL,
387 0, LVB_T_NONE, NULL, lh);
390 if (rc == ELDLM_OK) {
393 memset(lh, 0, sizeof(*lh));
401 * Request the specified ibits lock for the given object.
403 * Before the LFSCK modifying on the namespace visible object,
404 * it needs to acquire related ibits ldlm lock.
406 * \param[in] env pointer to the thread context
407 * \param[in] lfsck pointer to the lfsck instance
408 * \param[in] obj pointer to the dt_object to be locked
409 * \param[out] lh pointer to the lock handle
410 * \param[in] bits the bits for the ldlm lock to be acquired
411 * \param[in] mode the mode for the ldlm lock to be acquired
413 * \retval 0 for success
414 * \retval negative error number on failure
416 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
417 struct dt_object *obj, struct lustre_handle *lh,
418 __u64 bits, enum ldlm_mode mode)
420 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
422 LASSERT(!lustre_handle_is_used(lh));
424 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
425 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
429 * Release the the specified ibits lock.
431 * If the lock has been acquired before, release it
432 * and cleanup the handle. Otherwise, do nothing.
434 * \param[in] lh pointer to the lock handle
435 * \param[in] mode the mode for the ldlm lock to be released
437 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
439 if (lustre_handle_is_used(lh)) {
440 ldlm_lock_decref(lh, mode);
441 memset(lh, 0, sizeof(*lh));
446 * Request compound ibits locks for the given <obj, name> pairs.
448 * Before the LFSCK modifying on the namespace visible object, it needs to
449 * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
450 * the lock purpose. But the simple lfsck_ibits_lock for directory-based
451 * modificationis (such as insert name entry to the directory) may be too
452 * coarse-grained and not efficient.
454 * The lfsck_lock() will request compound ibits locks on the specified
455 * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
456 * lock on the directory object, and the regular ibits lock on the name hash.
458 * \param[in] env pointer to the thread context
459 * \param[in] lfsck pointer to the lfsck instance
460 * \param[in] obj pointer to the dt_object to be locked
461 * \param[in] name used for building the PDO lock resource
462 * \param[out] llh pointer to the lfsck_lock_handle
463 * \param[in] bits the bits for the ldlm lock to be acquired
464 * \param[in] mode the mode for the ldlm lock to be acquired
466 * \retval 0 for success
467 * \retval negative error number on failure
469 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
470 struct dt_object *obj, const char *name,
471 struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
473 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
476 LASSERT(S_ISDIR(lfsck_object_type(obj)));
477 LASSERT(name != NULL);
478 LASSERT(name[0] != 0);
479 LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
480 LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
484 llh->llh_pdo_mode = LCK_EX;
487 llh->llh_pdo_mode = LCK_CW;
490 llh->llh_pdo_mode = LCK_CR;
493 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
494 DFID"\n", lfsck_lfsck2name(lfsck), mode,
495 PFID(lfsck_dto2fid(obj)));
499 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
500 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
501 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
505 llh->llh_reg_mode = mode;
506 resid->name[LUSTRE_RES_ID_HSH_OFF] = ll_full_name_hash(NULL, name,
508 LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
509 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
510 bits, llh->llh_reg_mode);
512 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
518 * Release the the compound ibits locks.
520 * \param[in] llh pointer to the lfsck_lock_handle to be released
522 void lfsck_unlock(struct lfsck_lock_handle *llh)
524 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
525 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
528 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
529 struct lfsck_instance *lfsck,
530 const struct lu_fid *fid)
532 struct seq_server_site *ss = lfsck_dev_site(lfsck);
533 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
536 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
537 /* "ROOT" is always on the MDT0. */
538 if (lu_fid_eq(fid, &lfsck->li_global_root_fid))
541 return lfsck_dev_idx(lfsck);
544 fld_range_set_mdt(range);
545 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
547 rc = range->lsr_index;
552 const char dot[] = ".";
553 const char dotdot[] = "..";
554 static const char dotlustre[] = ".lustre";
555 static const char lostfound[] = "lost+found";
558 * Remove the name entry from the .lustre/lost+found directory.
560 * No need to care about the object referenced by the name entry,
561 * either the name entry is invalid or redundant, or the referenced
562 * object has been processed or will be handled by others.
564 * \param[in] env pointer to the thread context
565 * \param[in] lfsck pointer to the lfsck instance
566 * \param[in] name the name for the name entry to be removed
568 * \retval 0 for success
569 * \retval negative error number on failure
571 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
572 struct lfsck_instance *lfsck,
575 struct dt_object *parent = lfsck->li_lpf_root_obj;
576 struct dt_device *dev = lfsck_obj2dev(parent);
578 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
582 rc = lfsck_lock(env, lfsck, parent, name, llh,
583 MDS_INODELOCK_UPDATE, LCK_PW);
587 th = dt_trans_create(env, dev);
589 GOTO(unlock, rc = PTR_ERR(th));
591 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
595 rc = dt_declare_ref_del(env, parent, th);
599 rc = dt_trans_start_local(env, dev, th);
603 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
607 dt_write_lock(env, parent, 0);
608 rc = dt_ref_del(env, parent, th);
609 dt_write_unlock(env, parent);
614 dt_trans_stop(env, dev, th);
619 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
620 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
625 static int lfsck_create_lpf_local(const struct lu_env *env,
626 struct lfsck_instance *lfsck,
627 struct dt_object *child,
629 struct dt_object_format *dof,
632 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
633 struct dt_object *parent = lfsck->li_lpf_root_obj;
634 struct dt_device *dev = lfsck_obj2dev(child);
635 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
636 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
637 const struct lu_fid *cfid = lfsck_dto2fid(child);
638 struct thandle *th = NULL;
639 struct linkea_data ldata = { NULL };
640 struct lu_buf linkea_buf;
641 const struct lu_name *cname;
643 int len = sizeof(struct lfsck_bookmark);
647 cname = lfsck_name_get_const(env, name, strlen(name));
648 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
649 cname, lfsck_dto2fid(parent));
653 th = dt_trans_create(env, dev);
657 /* 1a. create child */
658 rc = dt_declare_create(env, child, la, NULL, dof, th);
662 if (!dt_try_as_dir(env, child))
663 GOTO(stop, rc = -ENOTDIR);
665 /* 2a. increase child nlink */
666 rc = dt_declare_ref_add(env, child, th);
670 /* 3a. insert dot into child dir */
671 rec->rec_type = S_IFDIR;
673 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
674 (const struct dt_key *)dot, th);
678 /* 4a. insert dotdot into child dir */
679 rec->rec_fid = &LU_LPF_FID;
680 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
681 (const struct dt_key *)dotdot, th);
685 /* 5a. insert linkEA for child */
686 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
687 ldata.ld_leh->leh_len);
688 rc = dt_declare_xattr_set(env, child, &linkea_buf,
689 XATTR_NAME_LINK, 0, th);
693 /* 6a. insert name into parent dir */
694 rec->rec_type = S_IFDIR;
696 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
697 (const struct dt_key *)name, th);
701 /* 7a. increase parent nlink */
702 rc = dt_declare_ref_add(env, parent, th);
706 /* 8a. update bookmark */
707 rc = dt_declare_record_write(env, bk_obj,
708 lfsck_buf_get(env, bk, len), 0, th);
712 rc = dt_trans_start_local(env, dev, th);
716 dt_write_lock(env, child, 0);
717 /* 1b. create child */
718 rc = dt_create(env, child, la, NULL, dof, th);
722 /* 2b. increase child nlink */
723 rc = dt_ref_add(env, child, th);
727 /* 3b. insert dot into child dir */
729 rc = dt_insert(env, child, (const struct dt_rec *)rec,
730 (const struct dt_key *)dot, th, 1);
734 /* 4b. insert dotdot into child dir */
735 rec->rec_fid = &LU_LPF_FID;
736 rc = dt_insert(env, child, (const struct dt_rec *)rec,
737 (const struct dt_key *)dotdot, th, 1);
741 /* 5b. insert linkEA for child. */
742 rc = dt_xattr_set(env, child, &linkea_buf,
743 XATTR_NAME_LINK, 0, th);
744 dt_write_unlock(env, child);
748 /* 6b. insert name into parent dir */
750 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
751 (const struct dt_key *)name, th, 1);
755 dt_write_lock(env, parent, 0);
756 /* 7b. increase parent nlink */
757 rc = dt_ref_add(env, parent, th);
758 dt_write_unlock(env, parent);
762 bk->lb_lpf_fid = *cfid;
763 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
765 /* 8b. update bookmark */
766 rc = dt_record_write(env, bk_obj,
767 lfsck_buf_get(env, bk, len), &pos, th);
772 dt_write_unlock(env, child);
775 dt_trans_stop(env, dev, th);
780 static int lfsck_create_lpf_remote(const struct lu_env *env,
781 struct lfsck_instance *lfsck,
782 struct dt_object *child,
784 struct dt_object_format *dof,
787 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
788 struct dt_object *parent = lfsck->li_lpf_root_obj;
789 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
790 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
791 const struct lu_fid *cfid = lfsck_dto2fid(child);
792 struct thandle *th = NULL;
793 struct linkea_data ldata = { NULL };
794 struct lu_buf linkea_buf;
795 const struct lu_name *cname;
796 struct dt_device *dev;
798 int len = sizeof(struct lfsck_bookmark);
802 cname = lfsck_name_get_const(env, name, strlen(name));
803 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
804 cname, lfsck_dto2fid(parent));
808 /* Create .lustre/lost+found/MDTxxxx. */
810 /* XXX: Currently, cross-MDT create operation needs to create the child
811 * object firstly, then insert name into the parent directory. For
812 * this case, the child object resides on current MDT (local), but
813 * the parent ".lustre/lost+found" may be on remote MDT. It is not
814 * easy to contain all the sub-modifications orderly within single
817 * To avoid more inconsistency, we split the create operation into
820 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
822 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
825 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
826 * repair such inconsistency when LFSCK run next time. */
828 /* Transaction I: locally */
830 dev = lfsck_obj2dev(child);
831 th = dt_trans_create(env, dev);
835 /* 1a. create child */
836 rc = dt_declare_create(env, child, la, NULL, dof, th);
840 if (!dt_try_as_dir(env, child))
841 GOTO(stop, rc = -ENOTDIR);
843 /* 2a. increase child nlink */
844 rc = dt_declare_ref_add(env, child, th);
848 /* 3a. insert dot into child dir */
849 rec->rec_type = S_IFDIR;
851 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
852 (const struct dt_key *)dot, th);
856 /* 4a. insert dotdot into child dir */
857 rec->rec_fid = &LU_LPF_FID;
858 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
859 (const struct dt_key *)dotdot, th);
863 /* 5a. insert linkEA for child */
864 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
865 ldata.ld_leh->leh_len);
866 rc = dt_declare_xattr_set(env, child, &linkea_buf,
867 XATTR_NAME_LINK, 0, th);
871 /* 6a. update bookmark */
872 rc = dt_declare_record_write(env, bk_obj,
873 lfsck_buf_get(env, bk, len), 0, th);
877 rc = dt_trans_start_local(env, dev, th);
881 dt_write_lock(env, child, 0);
882 /* 1b. create child */
883 rc = dt_create(env, child, la, NULL, dof, th);
887 /* 2b. increase child nlink */
888 rc = dt_ref_add(env, child, th);
892 /* 3b. insert dot into child dir */
893 rec->rec_type = S_IFDIR;
895 rc = dt_insert(env, child, (const struct dt_rec *)rec,
896 (const struct dt_key *)dot, th, 1);
900 /* 4b. insert dotdot into child dir */
901 rec->rec_fid = &LU_LPF_FID;
902 rc = dt_insert(env, child, (const struct dt_rec *)rec,
903 (const struct dt_key *)dotdot, th, 1);
907 /* 5b. insert linkEA for child */
908 rc = dt_xattr_set(env, child, &linkea_buf,
909 XATTR_NAME_LINK, 0, th);
913 bk->lb_lpf_fid = *cfid;
914 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
916 /* 6b. update bookmark */
917 rc = dt_record_write(env, bk_obj,
918 lfsck_buf_get(env, bk, len), &pos, th);
920 dt_write_unlock(env, child);
921 dt_trans_stop(env, dev, th);
925 /* Transaction II: remotely */
927 dev = lfsck_obj2dev(parent);
928 th = dt_trans_create(env, dev);
933 /* 5a. insert name into parent dir */
935 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
936 (const struct dt_key *)name, th);
940 /* 6a. increase parent nlink */
941 rc = dt_declare_ref_add(env, parent, th);
945 rc = dt_trans_start_local(env, dev, th);
949 /* 5b. insert name into parent dir */
950 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
951 (const struct dt_key *)name, th, 1);
955 dt_write_lock(env, parent, 0);
956 /* 6b. increase parent nlink */
957 rc = dt_ref_add(env, parent, th);
958 dt_write_unlock(env, parent);
963 dt_write_unlock(env, child);
965 dt_trans_stop(env, dev, th);
967 if (rc != 0 && dev == lfsck_obj2dev(parent))
968 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
969 "for orphans, but failed to insert the name %s "
970 "to the .lustre/lost+found/. Such inconsistency "
971 "will be repaired when LFSCK run next time: rc = %d\n",
972 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
978 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
980 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
981 * orphans and other uncertain inconsistent objects found during the
982 * LFSCK. Such directory will be created by the LFSCK engine on the
983 * local MDT before the LFSCK scanning.
985 * \param[in] env pointer to the thread context
986 * \param[in] lfsck pointer to the lfsck instance
988 * \retval 0 for success
989 * \retval negative error number on failure
991 static int lfsck_create_lpf(const struct lu_env *env,
992 struct lfsck_instance *lfsck)
994 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
995 struct lfsck_thread_info *info = lfsck_env_info(env);
996 struct lu_fid *cfid = &info->lti_fid2;
997 struct lu_attr *la = &info->lti_la;
998 struct dt_object_format *dof = &info->lti_dof;
999 struct dt_object *parent = lfsck->li_lpf_root_obj;
1000 struct dt_object *child = NULL;
1001 struct lfsck_lock_handle *llh = &info->lti_llh;
1003 int node = lfsck_dev_idx(lfsck);
1007 LASSERT(lfsck->li_master);
1008 LASSERT(parent != NULL);
1009 LASSERT(lfsck->li_lpf_obj == NULL);
1011 snprintf(name, 8, "MDT%04x", node);
1012 rc = lfsck_lock(env, lfsck, parent, name, llh,
1013 MDS_INODELOCK_UPDATE, LCK_PW);
1017 if (fid_is_zero(&bk->lb_lpf_fid)) {
1018 /* There is corner case that: in former LFSCK scanning we have
1019 * created the .lustre/lost+found/MDTxxxx but failed to update
1020 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1021 * it from MDT0 firstly. */
1022 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1023 (const struct dt_key *)name);
1024 if (rc != 0 && rc != -ENOENT)
1028 bk->lb_lpf_fid = *cfid;
1029 rc = lfsck_bookmark_store(env, lfsck);
1031 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1036 *cfid = bk->lb_lpf_fid;
1039 child = lfsck_object_find_bottom_new(env, lfsck, cfid);
1041 GOTO(unlock, rc = PTR_ERR(child));
1043 if (dt_object_exists(child) != 0) {
1044 if (unlikely(!dt_try_as_dir(env, child)))
1047 lfsck->li_lpf_obj = child;
1052 memset(la, 0, sizeof(*la));
1053 la->la_atime = la->la_mtime = la->la_ctime = ktime_get_real_seconds();
1054 la->la_mode = S_IFDIR | S_IRWXU;
1055 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1057 memset(dof, 0, sizeof(*dof));
1058 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1061 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1063 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1065 lfsck->li_lpf_obj = child;
1071 if (rc != 0 && child != NULL && !IS_ERR(child))
1072 lfsck_object_put(env, child);
1078 * Scan .lustre/lost+found for bad name entries and remove them.
1080 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1081 * index in the system. Any other formatted name is invalid and should be
1084 * \param[in] env pointer to the thread context
1085 * \param[in] lfsck pointer to the lfsck instance
1087 * \retval 0 for success
1088 * \retval negative error number on failure
1090 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1091 struct lfsck_instance *lfsck)
1093 struct dt_object *parent = lfsck->li_lpf_root_obj;
1094 struct lu_dirent *ent =
1095 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1096 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1101 it = iops->init(env, parent, LUDA_64BITHASH);
1103 RETURN(PTR_ERR(it));
1105 rc = iops->load(env, it, 0);
1107 rc = iops->next(env, it);
1114 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1118 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1119 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1122 /* name length must be strlen("MDTxxxx") */
1123 if (ent->lde_namelen != 7)
1126 if (memcmp(ent->lde_name, "MDT", off) != 0)
1129 while (off < 7 && isxdigit(ent->lde_name[off]))
1135 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1142 rc = iops->next(env, it);
1146 iops->fini(env, it);
1148 RETURN(rc > 0 ? 0 : rc);
1151 static int lfsck_update_lpf_entry(const struct lu_env *env,
1152 struct lfsck_instance *lfsck,
1153 struct dt_object *parent,
1154 struct dt_object *child,
1156 enum lfsck_verify_lpf_types type)
1160 if (type == LVLT_BY_BOOKMARK) {
1161 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1162 lfsck_dto2fid(child), S_IFDIR);
1163 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1164 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1165 rc = lfsck_bookmark_store(env, lfsck);
1167 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1168 " in the bookmark file: rc = %d\n",
1169 lfsck_lfsck2name(lfsck),
1170 PFID(lfsck_dto2fid(child)), rc);
1177 * Check whether the @child back references the @parent.
1180 * 1) The child's FID is stored in the bookmark file. If the child back
1181 * references the parent (LU_LPF_FID object) via its ".." entry, then
1182 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1183 * the child back references another parent2, then:
1184 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1185 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1186 * references the child. So keep them there. As the LFSCK processing,
1187 * the parent3 may be found, then when the LFSCK run next time, the
1188 * inconsistency can be repaired.
1190 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1191 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1192 * via its ".." entry, then update the bookmark file, otherwise, if the child
1193 * back references another parent2, then:
1194 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1195 * from .lustre/lost+found/;
1196 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1197 * sub-directory name entry and update the child;
1198 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1199 * or not, then keep them there.
1201 * \param[in] env pointer to the thread context
1202 * \param[in] lfsck pointer to the lfsck instance
1203 * \param[in] child pointer to the lost+found sub-directory object
1204 * \param[in] name the name for lost+found sub-directory object
1205 * \param[out] fid pointer to the buffer to hold the FID of the object
1206 * (called it as parent2) that is referenced via the
1207 * child's dotdot entry; it also can be the FID that
1208 * is referenced by the name entry under the parent2.
1209 * \param[in] type to indicate where the child's FID is stored in
1211 * \retval positive number for uncertain inconsistency
1212 * \retval 0 for success
1213 * \retval negative error number on failure
1215 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1216 struct lfsck_instance *lfsck,
1217 struct dt_object *child, const char *name,
1219 enum lfsck_verify_lpf_types type)
1221 struct dt_object *parent = lfsck->li_lpf_root_obj;
1222 struct lfsck_thread_info *info = lfsck_env_info(env);
1223 char *name2 = info->lti_key;
1224 struct lu_fid *fid2 = &info->lti_fid3;
1225 struct dt_object *parent2 = NULL;
1226 struct lustre_handle lh = { 0 };
1231 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1232 (const struct dt_key *)dotdot);
1236 if (!fid_is_sane(fid))
1237 GOTO(linkea, rc = -EINVAL);
1239 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1240 const struct lu_name *cname;
1242 if (lfsck->li_lpf_obj == NULL) {
1243 lu_object_get(&child->do_lu);
1244 lfsck->li_lpf_obj = child;
1247 cname = lfsck_name_get_const(env, name, strlen(name));
1248 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1250 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1256 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1257 if (IS_ERR(parent2))
1258 GOTO(linkea, parent2);
1260 if (!dt_object_exists(parent2)) {
1261 lfsck_object_put(env, parent2);
1263 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1266 if (!dt_try_as_dir(env, parent2)) {
1267 lfsck_object_put(env, parent2);
1269 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1273 /* To prevent rename/unlink race */
1274 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1275 MDS_INODELOCK_UPDATE, LCK_PR);
1279 dt_read_lock(env, child, 0);
1280 rc = lfsck_links_get_first(env, child, name2, fid2);
1282 dt_read_unlock(env, child);
1283 lfsck_ibits_unlock(&lh, LCK_PR);
1285 GOTO(out_put, rc = 1);
1288 /* It is almost impossible that the bookmark file (or the name entry)
1289 * and the linkEA hit the same data corruption. Trust the linkEA. */
1290 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1291 dt_read_unlock(env, child);
1292 lfsck_ibits_unlock(&lh, LCK_PR);
1295 if (lfsck->li_lpf_obj == NULL) {
1296 lu_object_get(&child->do_lu);
1297 lfsck->li_lpf_obj = child;
1300 /* Update the child's dotdot entry */
1301 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1302 &LU_LPF_FID, S_IFDIR);
1304 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1310 if (parent2 == NULL || IS_ERR(parent2)) {
1311 dt_read_unlock(env, child);
1312 lfsck_ibits_unlock(&lh, LCK_PR);
1314 GOTO(out_done, rc = 1);
1317 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1318 (const struct dt_key *)name2);
1319 dt_read_unlock(env, child);
1320 lfsck_ibits_unlock(&lh, LCK_PR);
1321 if (rc != 0 && rc != -ENOENT)
1324 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1325 if (type == LVLT_BY_BOOKMARK)
1326 GOTO(out_put, rc = 1);
1328 /* Trust the name entry, update the child's dotdot entry. */
1329 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1330 &LU_LPF_FID, S_IFDIR);
1335 if (type == LVLT_BY_BOOKMARK) {
1336 /* Invalid FID record in the bookmark file, reset it. */
1337 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1338 rc = lfsck_bookmark_store(env, lfsck);
1340 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1341 " in the bookmark file: rc = %d\n",
1342 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1343 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1344 /* The name entry is wrong, remove it. */
1345 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1351 if (parent2 != NULL && !IS_ERR(parent2))
1352 lfsck_object_put(env, parent2);
1359 * Verify the /ROOT/.lustre/lost+found/ directory.
1361 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1362 * the LFSCK does not exactly know how to handle, such as orphans. So before
1363 * the LFSCK scanning the system, the consistency of such directory needs to
1364 * be verified firstly to allow the users to use it during the LFSCK.
1366 * \param[in] env pointer to the thread context
1367 * \param[in] lfsck pointer to the lfsck instance
1369 * \retval positive number for uncertain inconsistency
1370 * \retval 0 for success
1371 * \retval negative error number on failure
1373 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1375 struct lfsck_thread_info *info = lfsck_env_info(env);
1376 struct lu_fid *pfid = &info->lti_fid;
1377 struct lu_fid *cfid = &info->lti_fid2;
1378 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1379 struct dt_object *parent;
1380 /* child1's FID is in the bookmark file. */
1381 struct dt_object *child1 = NULL;
1382 /* child2's FID is in the name entry MDTxxxx. */
1383 struct dt_object *child2 = NULL;
1384 const struct lu_name *cname;
1386 int node = lfsck_dev_idx(lfsck);
1390 LASSERT(lfsck->li_master);
1392 if (lfsck->li_lpf_root_obj != NULL)
1396 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1399 struct lfsck_tgt_desc *ltd;
1401 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1402 if (unlikely(ltd == NULL))
1405 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1411 RETURN(PTR_ERR(parent));
1413 LASSERT(dt_object_exists(parent));
1415 if (unlikely(!dt_try_as_dir(env, parent))) {
1416 lfsck_object_put(env, parent);
1418 GOTO(put, rc = -ENOTDIR);
1421 lfsck->li_lpf_root_obj = parent;
1423 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1425 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1426 "for bad sub-directories: rc = %d\n",
1427 lfsck_lfsck2name(lfsck), rc);
1431 snprintf(name, 8, "MDT%04x", node);
1432 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1433 (const struct dt_key *)name);
1434 if (rc == -ENOENT) {
1442 /* Invalid FID in the name entry, remove the name entry. */
1443 if (!fid_is_norm(cfid)) {
1444 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1451 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1453 GOTO(put, rc = PTR_ERR(child2));
1455 if (unlikely(!dt_object_exists(child2) ||
1456 dt_object_remote(child2)) ||
1457 !S_ISDIR(lfsck_object_type(child2))) {
1458 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1465 if (unlikely(!dt_try_as_dir(env, child2))) {
1466 lfsck_object_put(env, child2);
1472 if (fid_is_zero(&bk->lb_lpf_fid))
1475 if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1476 if (lfsck->li_lpf_obj == NULL) {
1477 lu_object_get(&child2->do_lu);
1478 lfsck->li_lpf_obj = child2;
1481 cname = lfsck_name_get_const(env, name, strlen(name));
1482 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1487 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1488 struct lu_fid tfid = bk->lb_lpf_fid;
1490 /* Invalid FID record in the bookmark file, reset it. */
1491 fid_zero(&bk->lb_lpf_fid);
1492 rc = lfsck_bookmark_store(env, lfsck);
1494 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1495 " in the bookmark file: rc = %d\n",
1496 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1504 child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1505 if (IS_ERR(child1)) {
1510 if (unlikely(!dt_object_exists(child1) ||
1511 dt_object_remote(child1)) ||
1512 !S_ISDIR(lfsck_object_type(child1))) {
1513 /* Invalid FID record in the bookmark file, reset it. */
1514 fid_zero(&bk->lb_lpf_fid);
1515 rc = lfsck_bookmark_store(env, lfsck);
1517 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1518 " in the bookmark file: rc = %d\n",
1519 lfsck_lfsck2name(lfsck),
1520 PFID(lfsck_dto2fid(child1)), rc);
1525 lfsck_object_put(env, child1);
1530 if (unlikely(!dt_try_as_dir(env, child1))) {
1531 lfsck_object_put(env, child1);
1537 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1539 if (lu_fid_eq(pfid, &LU_LPF_FID))
1544 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1545 pfid, LVLT_BY_NAMEENTRY);
1550 if (lfsck->li_lpf_obj != NULL) {
1551 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1552 lfsck_object_put(env, lfsck->li_lpf_obj);
1553 lfsck->li_lpf_obj = NULL;
1556 } else if (rc == 0) {
1557 rc = lfsck_create_lpf(env, lfsck);
1560 if (child2 != NULL && !IS_ERR(child2))
1561 lfsck_object_put(env, child2);
1562 if (child1 != NULL && !IS_ERR(child1))
1563 lfsck_object_put(env, child1);
1568 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1570 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1571 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1576 if (unlikely(ss == NULL))
1579 OBD_ALLOC_PTR(lfsck->li_seq);
1580 if (lfsck->li_seq == NULL)
1583 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1585 GOTO(out, rc = -ENOMEM);
1587 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1588 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1590 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1594 if (fid_is_sane(&bk->lb_last_fid))
1595 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1600 OBD_FREE_PTR(lfsck->li_seq);
1601 lfsck->li_seq = NULL;
1606 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1608 if (lfsck->li_seq != NULL) {
1609 seq_client_fini(lfsck->li_seq);
1610 OBD_FREE_PTR(lfsck->li_seq);
1611 lfsck->li_seq = NULL;
1615 void lfsck_instance_cleanup(const struct lu_env *env,
1616 struct lfsck_instance *lfsck)
1618 struct ptlrpc_thread *thread = &lfsck->li_thread;
1619 struct lfsck_component *com;
1620 struct lfsck_component *next;
1621 struct lfsck_lmv_unit *llu;
1622 struct lfsck_lmv_unit *llu_next;
1623 struct lfsck_lmv *llmv;
1626 LASSERT(list_empty(&lfsck->li_link));
1627 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1629 if (lfsck->li_obj_oit != NULL) {
1630 lfsck_object_put(env, lfsck->li_obj_oit);
1631 lfsck->li_obj_oit = NULL;
1634 LASSERT(lfsck->li_obj_dir == NULL);
1635 LASSERT(lfsck->li_lmv == NULL);
1637 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1638 llmv = &llu->llu_lmv;
1640 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1641 "still in using: %u\n",
1642 atomic_read(&llmv->ll_ref));
1644 lfsck_lmv_put(env, llmv);
1647 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1648 lfsck_component_cleanup(env, com);
1651 LASSERT(list_empty(&lfsck->li_list_dir));
1653 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1655 lfsck_component_cleanup(env, com);
1658 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1659 lfsck_component_cleanup(env, com);
1662 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1663 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1665 if (lfsck->li_lfsck_dir != NULL) {
1666 lfsck_object_put(env, lfsck->li_lfsck_dir);
1667 lfsck->li_lfsck_dir = NULL;
1670 if (lfsck->li_bookmark_obj != NULL) {
1671 lfsck_object_put(env, lfsck->li_bookmark_obj);
1672 lfsck->li_bookmark_obj = NULL;
1675 if (lfsck->li_lpf_obj != NULL) {
1676 lfsck_object_put(env, lfsck->li_lpf_obj);
1677 lfsck->li_lpf_obj = NULL;
1680 if (lfsck->li_lpf_root_obj != NULL) {
1681 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1682 lfsck->li_lpf_root_obj = NULL;
1685 if (lfsck->li_los != NULL) {
1686 local_oid_storage_fini(env, lfsck->li_los);
1687 lfsck->li_los = NULL;
1690 lfsck_fid_fini(lfsck);
1692 OBD_FREE_PTR(lfsck);
1695 static inline struct lfsck_instance *
1696 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1698 struct lfsck_instance *lfsck;
1700 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1701 if (lfsck->li_bottom == key) {
1703 lfsck_instance_get(lfsck);
1705 list_del_init(&lfsck->li_link);
1714 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1717 struct lfsck_instance *lfsck;
1719 spin_lock(&lfsck_instance_lock);
1720 lfsck = __lfsck_instance_find(key, ref, unlink);
1721 spin_unlock(&lfsck_instance_lock);
1726 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1728 struct lfsck_instance *tmp;
1730 spin_lock(&lfsck_instance_lock);
1731 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1732 if (lfsck->li_bottom == tmp->li_bottom) {
1733 spin_unlock(&lfsck_instance_lock);
1738 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1739 spin_unlock(&lfsck_instance_lock);
1743 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1748 bool newline = (bits != 0 ? false : true);
1750 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1752 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1755 if (names[i] != NULL) {
1759 seq_printf(m, "%s%c", names[i],
1760 newline ? '\n' : ',');
1769 void lfsck_time_dump(struct seq_file *m, time64_t time, const char *name)
1772 seq_printf(m, "%s_time: N/A\n", name);
1773 seq_printf(m, "time_since_%s: N/A\n", name);
1775 seq_printf(m, "%s_time: %lld\n", name, time);
1776 seq_printf(m, "time_since_%s: %lld seconds\n",
1777 name, ktime_get_real_seconds() - time);
1781 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1784 if (fid_is_zero(&pos->lp_dir_parent)) {
1785 if (pos->lp_oit_cookie == 0) {
1786 seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1789 seq_printf(m, "%s: %llu, N/A, N/A\n",
1790 prefix, pos->lp_oit_cookie);
1792 seq_printf(m, "%s: %llu, "DFID", %#llx\n",
1793 prefix, pos->lp_oit_cookie,
1794 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1798 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1799 struct lfsck_position *pos, bool init)
1801 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1803 if (unlikely(lfsck->li_di_oit == NULL)) {
1804 memset(pos, 0, sizeof(*pos));
1808 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1809 if (!lfsck->li_current_oit_processed && !init)
1810 pos->lp_oit_cookie--;
1812 LASSERT(pos->lp_oit_cookie > 0);
1814 if (lfsck->li_di_dir != NULL) {
1815 struct dt_object *dto = lfsck->li_obj_dir;
1817 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1820 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1821 fid_zero(&pos->lp_dir_parent);
1822 pos->lp_dir_cookie = 0;
1824 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1827 fid_zero(&pos->lp_dir_parent);
1828 pos->lp_dir_cookie = 0;
1832 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1836 if (limit != LFSCK_SPEED_NO_LIMIT) {
1837 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1838 lfsck->li_sleep_rate = limit /
1839 msecs_to_jiffies(MSEC_PER_SEC);
1840 lfsck->li_sleep_jif = 1;
1842 lfsck->li_sleep_rate = 1;
1843 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1847 lfsck->li_sleep_jif = 0;
1848 lfsck->li_sleep_rate = 0;
1851 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1852 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1859 void lfsck_control_speed(struct lfsck_instance *lfsck)
1861 struct ptlrpc_thread *thread = &lfsck->li_thread;
1862 struct l_wait_info lwi;
1864 if (lfsck->li_sleep_jif > 0 &&
1865 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1866 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1867 LWI_ON_SIGNAL_NOOP, NULL);
1869 l_wait_event(thread->t_ctl_waitq,
1870 !thread_is_running(thread),
1872 lfsck->li_new_scanned = 0;
1876 void lfsck_control_speed_by_self(struct lfsck_component *com)
1878 struct lfsck_instance *lfsck = com->lc_lfsck;
1879 struct ptlrpc_thread *thread = &lfsck->li_thread;
1880 struct l_wait_info lwi;
1882 if (lfsck->li_sleep_jif > 0 &&
1883 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1884 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1885 LWI_ON_SIGNAL_NOOP, NULL);
1887 l_wait_event(thread->t_ctl_waitq,
1888 !thread_is_running(thread),
1890 com->lc_new_scanned = 0;
1894 static struct lfsck_thread_args *
1895 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1896 struct lfsck_component *com,
1897 struct lfsck_start_param *lsp)
1899 struct lfsck_thread_args *lta;
1904 return ERR_PTR(-ENOMEM);
1906 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1912 lta->lta_lfsck = lfsck_instance_get(lfsck);
1914 lta->lta_com = lfsck_component_get(com);
1921 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1923 if (lta->lta_com != NULL)
1924 lfsck_component_put(<a->lta_env, lta->lta_com);
1925 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1926 lu_env_fini(<a->lta_env);
1930 struct lfsck_assistant_data *
1931 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1934 struct lfsck_assistant_data *lad;
1938 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1939 if (lad->lad_bitmap == NULL) {
1944 INIT_LIST_HEAD(&lad->lad_req_list);
1945 spin_lock_init(&lad->lad_lock);
1946 INIT_LIST_HEAD(&lad->lad_ost_list);
1947 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1948 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1949 INIT_LIST_HEAD(&lad->lad_mdt_list);
1950 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1951 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1952 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1954 lad->lad_name = name;
1960 struct lfsck_assistant_object *
1961 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1962 const struct lu_attr *attr, __u64 cookie,
1965 struct lfsck_assistant_object *lso;
1969 return ERR_PTR(-ENOMEM);
1971 lso->lso_fid = *fid;
1973 lso->lso_attr = *attr;
1975 atomic_set(&lso->lso_ref, 1);
1976 lso->lso_oit_cookie = cookie;
1978 lso->lso_is_dir = 1;
1984 lfsck_assistant_object_load(const struct lu_env *env,
1985 struct lfsck_instance *lfsck,
1986 struct lfsck_assistant_object *lso)
1988 struct dt_object *obj;
1990 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1994 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
1996 lfsck_object_put(env, obj);
1998 return ERR_PTR(-ENOENT);
2001 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2002 lfsck_object_put(env, obj);
2004 return ERR_PTR(-ENOTDIR);
2011 * Generic LFSCK asynchronous communication interpretor function.
2012 * The LFSCK RPC reply for both the event notification and status
2013 * querying will be handled here.
2015 * \param[in] env pointer to the thread context
2016 * \param[in] req pointer to the LFSCK request
2017 * \param[in] args pointer to the lfsck_async_interpret_args
2018 * \param[in] rc the result for handling the LFSCK request
2020 * \retval 0 for success
2021 * \retval negative error number on failure
2023 int lfsck_async_interpret_common(const struct lu_env *env,
2024 struct ptlrpc_request *req,
2027 struct lfsck_async_interpret_args *laia = args;
2028 struct lfsck_component *com = laia->laia_com;
2029 struct lfsck_assistant_data *lad = com->lc_data;
2030 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2031 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2032 struct lfsck_request *lr = laia->laia_lr;
2034 LASSERT(com->lc_lfsck->li_master);
2036 switch (lr->lr_event) {
2038 if (unlikely(rc == -EINPROGRESS)) {
2039 ltd->ltd_retry_start = 1;
2044 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2046 lfsck_lfsck2name(com->lc_lfsck),
2047 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2048 ltd->ltd_index, lad->lad_name, rc);
2050 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2051 struct lfsck_layout *lo = com->lc_file_ram;
2053 if (lr->lr_flags & LEF_TO_OST)
2054 lfsck_lad_set_bitmap(env, com,
2057 lo->ll_flags |= LF_INCOMPLETE;
2059 struct lfsck_namespace *ns = com->lc_file_ram;
2061 /* If some MDT does not join the namespace
2062 * LFSCK, then we cannot know whether there
2063 * is some name entry on such MDT that with
2064 * the referenced MDT-object on this MDT or
2065 * not. So the namespace LFSCK on this MDT
2066 * cannot handle orphan MDT-objects properly.
2067 * So we mark the LFSCK as LF_INCOMPLETE and
2068 * skip orphan MDT-objects handling. */
2069 ns->ln_flags |= LF_INCOMPLETE;
2074 spin_lock(<ds->ltd_lock);
2075 if (ltd->ltd_dead) {
2076 spin_unlock(<ds->ltd_lock);
2080 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2081 struct list_head *list;
2082 struct list_head *phase_list;
2084 if (ltd->ltd_layout_done) {
2085 spin_unlock(<ds->ltd_lock);
2089 if (lr->lr_flags & LEF_TO_OST) {
2090 list = &lad->lad_ost_list;
2091 phase_list = &lad->lad_ost_phase1_list;
2093 list = &lad->lad_mdt_list;
2094 phase_list = &lad->lad_mdt_phase1_list;
2097 if (list_empty(<d->ltd_layout_list))
2098 list_add_tail(<d->ltd_layout_list, list);
2099 if (list_empty(<d->ltd_layout_phase_list))
2100 list_add_tail(<d->ltd_layout_phase_list,
2103 if (ltd->ltd_namespace_done) {
2104 spin_unlock(<ds->ltd_lock);
2108 if (list_empty(<d->ltd_namespace_list))
2109 list_add_tail(<d->ltd_namespace_list,
2110 &lad->lad_mdt_list);
2111 if (list_empty(<d->ltd_namespace_phase_list))
2112 list_add_tail(<d->ltd_namespace_phase_list,
2113 &lad->lad_mdt_phase1_list);
2115 spin_unlock(<ds->ltd_lock);
2118 case LE_PHASE1_DONE:
2119 case LE_PHASE2_DONE:
2121 if (rc != 0 && rc != -EALREADY)
2122 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2123 "event = %d, rc = %d\n",
2124 lfsck_lfsck2name(com->lc_lfsck),
2125 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2126 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2129 struct lfsck_reply *reply;
2130 struct list_head *list;
2131 struct list_head *phase_list;
2133 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2134 list = <d->ltd_layout_list;
2135 phase_list = <d->ltd_layout_phase_list;
2137 list = <d->ltd_namespace_list;
2138 phase_list = <d->ltd_namespace_phase_list;
2142 if (lr->lr_flags & LEF_QUERY_ALL) {
2143 lfsck_reset_ltd_status(ltd, com->lc_type);
2147 spin_lock(<ds->ltd_lock);
2148 list_del_init(phase_list);
2149 list_del_init(list);
2150 spin_unlock(<ds->ltd_lock);
2154 reply = req_capsule_server_get(&req->rq_pill,
2156 if (reply == NULL) {
2158 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2159 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2162 if (lr->lr_flags & LEF_QUERY_ALL) {
2163 lfsck_reset_ltd_status(ltd, com->lc_type);
2167 spin_lock(<ds->ltd_lock);
2168 list_del_init(phase_list);
2169 list_del_init(list);
2170 spin_unlock(<ds->ltd_lock);
2174 if (lr->lr_flags & LEF_QUERY_ALL) {
2175 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2176 ltd->ltd_layout_status = reply->lr_status;
2177 ltd->ltd_layout_repaired = reply->lr_repaired;
2179 ltd->ltd_namespace_status = reply->lr_status;
2180 ltd->ltd_namespace_repaired =
2186 switch (reply->lr_status) {
2187 case LS_SCANNING_PHASE1:
2189 case LS_SCANNING_PHASE2:
2190 spin_lock(<ds->ltd_lock);
2191 list_del_init(phase_list);
2192 if (ltd->ltd_dead) {
2193 spin_unlock(<ds->ltd_lock);
2197 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2198 if (ltd->ltd_layout_done) {
2199 spin_unlock(<ds->ltd_lock);
2203 if (lr->lr_flags & LEF_TO_OST)
2204 list_add_tail(phase_list,
2205 &lad->lad_ost_phase2_list);
2207 list_add_tail(phase_list,
2208 &lad->lad_mdt_phase2_list);
2210 if (ltd->ltd_namespace_done) {
2211 spin_unlock(<ds->ltd_lock);
2215 list_add_tail(phase_list,
2216 &lad->lad_mdt_phase2_list);
2218 spin_unlock(<ds->ltd_lock);
2221 spin_lock(<ds->ltd_lock);
2222 list_del_init(phase_list);
2223 list_del_init(list);
2224 spin_unlock(<ds->ltd_lock);
2230 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2231 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2235 if (!laia->laia_shared) {
2237 lfsck_component_put(env, com);
2243 static void lfsck_interpret(const struct lu_env *env,
2244 struct lfsck_instance *lfsck,
2245 struct ptlrpc_request *req, void *args, int result)
2247 struct lfsck_async_interpret_args *laia = args;
2248 struct lfsck_component *com;
2250 LASSERT(laia->laia_com == NULL);
2251 LASSERT(laia->laia_shared);
2253 spin_lock(&lfsck->li_lock);
2254 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2255 laia->laia_com = com;
2256 lfsck_async_interpret_common(env, req, laia, result);
2259 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2260 laia->laia_com = com;
2261 lfsck_async_interpret_common(env, req, laia, result);
2263 spin_unlock(&lfsck->li_lock);
2266 static int lfsck_stop_notify(const struct lu_env *env,
2267 struct lfsck_instance *lfsck,
2268 struct lfsck_tgt_descs *ltds,
2269 struct lfsck_tgt_desc *ltd, __u16 type)
2271 struct lfsck_component *com;
2275 LASSERT(lfsck->li_master);
2277 spin_lock(&lfsck->li_lock);
2278 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2280 com = __lfsck_component_find(lfsck, type,
2281 &lfsck->li_list_double_scan);
2283 lfsck_component_get(com);
2284 spin_unlock(&lfsck->li_lock);
2287 struct lfsck_thread_info *info = lfsck_env_info(env);
2288 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2289 struct lfsck_request *lr = &info->lti_lr;
2290 struct lfsck_assistant_data *lad = com->lc_data;
2291 struct list_head *list;
2292 struct list_head *phase_list;
2293 struct ptlrpc_request_set *set;
2295 set = ptlrpc_prep_set();
2297 lfsck_component_put(env, com);
2302 if (type == LFSCK_TYPE_LAYOUT) {
2303 list = <d->ltd_layout_list;
2304 phase_list = <d->ltd_layout_phase_list;
2306 list = <d->ltd_namespace_list;
2307 phase_list = <d->ltd_namespace_phase_list;
2310 spin_lock(<ds->ltd_lock);
2311 if (list_empty(list)) {
2312 LASSERT(list_empty(phase_list));
2313 spin_unlock(<ds->ltd_lock);
2314 ptlrpc_set_destroy(set);
2319 list_del_init(phase_list);
2320 list_del_init(list);
2321 spin_unlock(<ds->ltd_lock);
2323 memset(lr, 0, sizeof(*lr));
2324 lr->lr_index = lfsck_dev_idx(lfsck);
2325 lr->lr_event = LE_PEER_EXIT;
2326 lr->lr_active = type;
2327 lr->lr_status = LS_CO_PAUSED;
2328 if (ltds == &lfsck->li_ost_descs)
2329 lr->lr_flags = LEF_TO_OST;
2331 memset(laia, 0, sizeof(*laia));
2332 laia->laia_com = com;
2333 laia->laia_ltds = ltds;
2334 atomic_inc(<d->ltd_ref);
2335 laia->laia_ltd = ltd;
2338 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2339 lfsck_async_interpret_common,
2340 laia, LFSCK_NOTIFY);
2342 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2343 "co-stop for %s: rc = %d\n",
2344 lfsck_lfsck2name(lfsck),
2345 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2346 ltd->ltd_index, lad->lad_name, rc);
2349 rc = ptlrpc_set_wait(set);
2352 ptlrpc_set_destroy(set);
2353 lfsck_component_put(env, com);
2359 static int lfsck_async_interpret(const struct lu_env *env,
2360 struct ptlrpc_request *req,
2363 struct lfsck_async_interpret_args *laia = args;
2364 struct lfsck_instance *lfsck;
2366 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2368 lfsck_interpret(env, lfsck, req, laia, rc);
2369 lfsck_tgt_put(laia->laia_ltd);
2370 if (rc != 0 && laia->laia_result != -EALREADY)
2371 laia->laia_result = rc;
2376 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2377 struct lfsck_request *lr,
2378 struct ptlrpc_request_set *set,
2379 ptlrpc_interpterer_t interpreter,
2380 void *args, int request)
2382 struct lfsck_async_interpret_args *laia;
2383 struct ptlrpc_request *req;
2384 struct lfsck_request *tmp;
2385 struct req_format *format;
2390 format = &RQF_LFSCK_NOTIFY;
2393 format = &RQF_LFSCK_QUERY;
2396 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2397 exp->exp_obd->obd_name, request, -EINVAL);
2401 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2405 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2407 ptlrpc_request_free(req);
2412 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2414 ptlrpc_request_set_replen(req);
2416 laia = ptlrpc_req_async_args(req);
2417 *laia = *(struct lfsck_async_interpret_args *)args;
2418 if (laia->laia_com != NULL)
2419 lfsck_component_get(laia->laia_com);
2420 req->rq_interpret_reply = interpreter;
2421 req->rq_allow_intr = 1;
2422 req->rq_no_delay = 1;
2423 ptlrpc_set_add_req(set, req);
2428 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2430 struct lfsck_thread_info *info = lfsck_env_info(env);
2431 struct lfsck_request *lr = &info->lti_lr;
2432 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2433 struct lfsck_instance *lfsck = com->lc_lfsck;
2434 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2435 struct lfsck_tgt_desc *ltd;
2436 struct ptlrpc_request_set *set;
2441 memset(lr, 0, sizeof(*lr));
2442 lr->lr_event = LE_QUERY;
2443 lr->lr_active = com->lc_type;
2444 lr->lr_flags = LEF_QUERY_ALL;
2446 memset(laia, 0, sizeof(*laia));
2447 laia->laia_com = com;
2450 set = ptlrpc_prep_set();
2455 laia->laia_ltds = ltds;
2456 down_read(<ds->ltd_rw_sem);
2457 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2458 ltd = lfsck_tgt_get(ltds, idx);
2459 LASSERT(ltd != NULL);
2461 laia->laia_ltd = ltd;
2462 up_read(<ds->ltd_rw_sem);
2463 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2464 lfsck_async_interpret_common,
2467 struct lfsck_assistant_data *lad = com->lc_data;
2469 CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2470 "rc = %d\n", lfsck_lfsck2name(lfsck),
2471 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2472 ltd->ltd_index, lad->lad_name, rc);
2473 lfsck_reset_ltd_status(ltd, com->lc_type);
2476 down_read(<ds->ltd_rw_sem);
2478 up_read(<ds->ltd_rw_sem);
2480 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2481 ltds = &lfsck->li_ost_descs;
2482 lr->lr_flags |= LEF_TO_OST;
2486 rc = ptlrpc_set_wait(set);
2487 ptlrpc_set_destroy(set);
2492 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2493 struct lfsck_start_param *lsp)
2495 struct lfsck_instance *lfsck = com->lc_lfsck;
2496 struct lfsck_assistant_data *lad = com->lc_data;
2497 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2498 struct ptlrpc_thread *athread = &lad->lad_thread;
2499 struct lfsck_thread_args *lta;
2500 struct task_struct *task;
2504 lad->lad_assistant_status = 0;
2505 lad->lad_post_result = 0;
2506 lad->lad_to_post = 0;
2507 lad->lad_to_double_scan = 0;
2508 lad->lad_in_double_scan = 0;
2510 lad->lad_advance_lock = false;
2511 thread_set_flags(athread, 0);
2513 lta = lfsck_thread_args_init(lfsck, com, lsp);
2515 RETURN(PTR_ERR(lta));
2517 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2520 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2521 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2522 lfsck_thread_args_fini(lta);
2524 struct l_wait_info lwi = { 0 };
2526 l_wait_event(mthread->t_ctl_waitq,
2527 thread_is_running(athread) ||
2528 thread_is_stopped(athread) ||
2529 !thread_is_starting(mthread),
2531 if (unlikely(!thread_is_starting(mthread)))
2532 /* stopped by race */
2534 else if (unlikely(!thread_is_running(athread)))
2535 rc = lad->lad_assistant_status;
2543 int lfsck_checkpoint_generic(const struct lu_env *env,
2544 struct lfsck_component *com)
2546 struct lfsck_assistant_data *lad = com->lc_data;
2547 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2548 struct ptlrpc_thread *athread = &lad->lad_thread;
2549 struct l_wait_info lwi = { 0 };
2551 l_wait_event(mthread->t_ctl_waitq,
2552 list_empty(&lad->lad_req_list) ||
2553 !thread_is_running(mthread) ||
2554 thread_is_stopped(athread),
2557 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2558 return LFSCK_CHECKPOINT_SKIP;
2563 void lfsck_post_generic(const struct lu_env *env,
2564 struct lfsck_component *com, int *result)
2566 struct lfsck_assistant_data *lad = com->lc_data;
2567 struct ptlrpc_thread *athread = &lad->lad_thread;
2568 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2569 struct l_wait_info lwi = { 0 };
2571 lad->lad_post_result = *result;
2574 lad->lad_to_post = 1;
2576 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2577 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2579 wake_up_all(&athread->t_ctl_waitq);
2580 l_wait_event(mthread->t_ctl_waitq,
2581 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2582 thread_is_stopped(athread),
2585 if (lad->lad_assistant_status < 0)
2586 *result = lad->lad_assistant_status;
2588 CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2589 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2592 int lfsck_double_scan_generic(const struct lu_env *env,
2593 struct lfsck_component *com, int status)
2595 struct lfsck_assistant_data *lad = com->lc_data;
2596 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2597 struct ptlrpc_thread *athread = &lad->lad_thread;
2598 struct l_wait_info lwi = { 0 };
2600 if (status != LS_SCANNING_PHASE2)
2603 lad->lad_to_double_scan = 1;
2605 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2607 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2609 wake_up_all(&athread->t_ctl_waitq);
2610 l_wait_event(mthread->t_ctl_waitq,
2611 lad->lad_in_double_scan ||
2612 thread_is_stopped(athread),
2615 CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2616 "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2617 lad->lad_assistant_status);
2619 if (lad->lad_assistant_status < 0)
2620 return lad->lad_assistant_status;
2625 void lfsck_quit_generic(const struct lu_env *env,
2626 struct lfsck_component *com)
2628 struct lfsck_assistant_data *lad = com->lc_data;
2629 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2630 struct ptlrpc_thread *athread = &lad->lad_thread;
2631 struct l_wait_info lwi = { 0 };
2634 wake_up_all(&athread->t_ctl_waitq);
2635 l_wait_event(mthread->t_ctl_waitq,
2636 thread_is_init(athread) ||
2637 thread_is_stopped(athread),
2641 int lfsck_load_one_trace_file(const struct lu_env *env,
2642 struct lfsck_component *com,
2643 struct dt_object *parent,
2644 struct dt_object **child,
2645 const struct dt_index_features *ft,
2646 const char *name, bool reset)
2648 struct lfsck_instance *lfsck = com->lc_lfsck;
2649 struct dt_object *obj;
2653 if (*child != NULL) {
2655 const struct dt_it_ops *iops;
2656 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2662 rc = obj->do_ops->do_index_try(env, obj, ft);
2664 /* unlink by force */
2667 iops = &obj->do_index_ops->dio_it;
2668 it = iops->init(env, obj, 0);
2670 /* unlink by force */
2674 rc = iops->get(env, it, (const struct dt_key *)fid);
2676 rc = iops->next(env, it);
2679 iops->fini(env, it);
2681 /* "rc > 0" means the index file is empty. */
2685 /* The old index is not empty, remove it firstly. */
2686 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2687 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2688 "%s: unlink lfsck sub trace file %s: rc = %d\n",
2689 lfsck_lfsck2name(com->lc_lfsck), name, rc);
2694 lfsck_object_put(env, *child);
2701 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2702 S_IFREG | S_IRUGO | S_IWUSR, ft);
2704 RETURN(PTR_ERR(obj));
2706 rc = obj->do_ops->do_index_try(env, obj, ft);
2708 lfsck_object_put(env, obj);
2715 int lfsck_load_sub_trace_files(const struct lu_env *env,
2716 struct lfsck_component *com,
2717 const struct dt_index_features *ft,
2718 const char *prefix, bool reset)
2720 char *name = lfsck_env_info(env)->lti_key;
2721 struct lfsck_sub_trace_obj *lsto;
2725 for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2726 i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2727 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2728 rc = lfsck_load_one_trace_file(env, com,
2729 com->lc_lfsck->li_lfsck_dir,
2730 &lsto->lsto_obj, ft, name, reset);
2736 /* external interfaces */
2738 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2741 struct lfsck_instance *lfsck;
2745 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2749 lfsck = lfsck_instance_find(key, true, false);
2750 if (likely(lfsck != NULL)) {
2751 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2752 lfsck_instance_put(&env, lfsck);
2761 EXPORT_SYMBOL(lfsck_get_speed);
2763 int lfsck_set_speed(struct dt_device *key, __u32 val)
2766 struct lfsck_instance *lfsck;
2770 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2774 lfsck = lfsck_instance_find(key, true, false);
2775 if (likely(lfsck != NULL)) {
2776 mutex_lock(&lfsck->li_mutex);
2777 if (__lfsck_set_speed(lfsck, val))
2778 rc = lfsck_bookmark_store(&env, lfsck);
2779 mutex_unlock(&lfsck->li_mutex);
2780 lfsck_instance_put(&env, lfsck);
2789 EXPORT_SYMBOL(lfsck_set_speed);
2791 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2794 struct lfsck_instance *lfsck;
2798 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2802 lfsck = lfsck_instance_find(key, true, false);
2803 if (likely(lfsck != NULL)) {
2804 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2805 lfsck_instance_put(&env, lfsck);
2814 EXPORT_SYMBOL(lfsck_get_windows);
2816 int lfsck_set_windows(struct dt_device *key, int val)
2819 struct lfsck_instance *lfsck;
2823 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2827 lfsck = lfsck_instance_find(key, true, false);
2828 if (likely(lfsck != NULL)) {
2829 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2830 CWARN("%s: invalid async windows size that may "
2831 "cause memory issues. The valid range is "
2833 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2835 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2836 mutex_lock(&lfsck->li_mutex);
2837 lfsck->li_bookmark_ram.lb_async_windows = val;
2838 rc = lfsck_bookmark_store(&env, lfsck);
2839 mutex_unlock(&lfsck->li_mutex);
2841 lfsck_instance_put(&env, lfsck);
2850 EXPORT_SYMBOL(lfsck_set_windows);
2852 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2855 struct lfsck_instance *lfsck;
2856 struct lfsck_component *com;
2860 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2864 lfsck = lfsck_instance_find(key, true, false);
2865 if (likely(lfsck != NULL)) {
2866 com = lfsck_component_find(lfsck, type);
2867 if (likely(com != NULL)) {
2868 com->lc_ops->lfsck_dump(&env, com, m);
2869 lfsck_component_put(&env, com);
2874 lfsck_instance_put(&env, lfsck);
2883 EXPORT_SYMBOL(lfsck_dump);
2885 static int lfsck_stop_all(const struct lu_env *env,
2886 struct lfsck_instance *lfsck,
2887 struct lfsck_stop *stop)
2889 struct lfsck_thread_info *info = lfsck_env_info(env);
2890 struct lfsck_request *lr = &info->lti_lr;
2891 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2892 struct ptlrpc_request_set *set;
2893 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2894 struct lfsck_tgt_desc *ltd;
2895 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2901 LASSERT(stop->ls_flags & LPF_BROADCAST);
2903 set = ptlrpc_prep_set();
2904 if (unlikely(set == NULL))
2907 memset(lr, 0, sizeof(*lr));
2908 lr->lr_event = LE_STOP;
2909 lr->lr_index = lfsck_dev_idx(lfsck);
2910 lr->lr_status = stop->ls_status;
2911 lr->lr_version = bk->lb_version;
2912 lr->lr_active = LFSCK_TYPES_ALL;
2913 lr->lr_param = stop->ls_flags;
2915 memset(laia, 0, sizeof(*laia));
2916 laia->laia_ltds = ltds;
2918 laia->laia_shared = 1;
2920 down_read(<ds->ltd_rw_sem);
2921 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2922 ltd = lfsck_tgt_get(ltds, idx);
2923 LASSERT(ltd != NULL);
2925 laia->laia_ltd = ltd;
2926 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2927 lfsck_async_interpret, laia,
2930 lfsck_interpret(env, lfsck, NULL, laia, rc);
2932 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2933 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2937 up_read(<ds->ltd_rw_sem);
2939 rc = ptlrpc_set_wait(set);
2940 ptlrpc_set_destroy(set);
2943 rc = laia->laia_result;
2945 if (rc == -EALREADY)
2949 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2950 lfsck_lfsck2name(lfsck), rc);
2952 RETURN(rc != 0 ? rc : rc1);
2955 static int lfsck_start_all(const struct lu_env *env,
2956 struct lfsck_instance *lfsck,
2957 struct lfsck_start *start)
2959 struct lfsck_thread_info *info = lfsck_env_info(env);
2960 struct lfsck_request *lr = &info->lti_lr;
2961 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2962 struct ptlrpc_request_set *set;
2963 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2964 struct lfsck_tgt_desc *ltd;
2965 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2971 LASSERT(start->ls_flags & LPF_BROADCAST);
2973 memset(lr, 0, sizeof(*lr));
2974 lr->lr_event = LE_START;
2975 lr->lr_index = lfsck_dev_idx(lfsck);
2976 lr->lr_speed = bk->lb_speed_limit;
2977 lr->lr_version = bk->lb_version;
2978 lr->lr_active = start->ls_active;
2979 lr->lr_param = start->ls_flags;
2980 lr->lr_async_windows = bk->lb_async_windows;
2981 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2982 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2985 memset(laia, 0, sizeof(*laia));
2986 laia->laia_ltds = ltds;
2988 laia->laia_shared = 1;
2991 set = ptlrpc_prep_set();
2995 down_read(<ds->ltd_rw_sem);
2996 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2997 ltd = lfsck_tgt_get(ltds, idx);
2998 LASSERT(ltd != NULL);
3000 if (retry && !ltd->ltd_retry_start) {
3005 laia->laia_ltd = ltd;
3006 ltd->ltd_retry_start = 0;
3007 ltd->ltd_layout_done = 0;
3008 ltd->ltd_namespace_done = 0;
3009 ltd->ltd_synced_failures = 0;
3010 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3011 lfsck_async_interpret, laia,
3014 lfsck_interpret(env, lfsck, NULL, laia, rc);
3016 CERROR("%s: cannot notify MDT %x for LFSCK "
3017 "start, failout: rc = %d\n",
3018 lfsck_lfsck2name(lfsck), idx, rc);
3022 up_read(<ds->ltd_rw_sem);
3025 ptlrpc_set_destroy(set);
3030 rc = ptlrpc_set_wait(set);
3031 ptlrpc_set_destroy(set);
3034 rc = laia->laia_result;
3036 if (unlikely(rc == -EINPROGRESS)) {
3038 set_current_state(TASK_INTERRUPTIBLE);
3039 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC));
3040 set_current_state(TASK_RUNNING);
3041 if (!signal_pending(current) &&
3042 thread_is_running(&lfsck->li_thread))
3049 struct lfsck_stop *stop = &info->lti_stop;
3051 CERROR("%s: cannot start LFSCK on some MDTs, "
3052 "stop all: rc = %d\n",
3053 lfsck_lfsck2name(lfsck), rc);
3054 if (rc != -EALREADY) {
3055 stop->ls_status = LS_FAILED;
3056 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3057 lfsck_stop_all(env, lfsck, stop);
3064 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3065 struct lfsck_start_param *lsp)
3067 struct lfsck_start *start = lsp->lsp_start;
3068 struct lfsck_instance *lfsck;
3069 struct lfsck_bookmark *bk;
3070 struct ptlrpc_thread *thread;
3071 struct lfsck_component *com;
3072 struct l_wait_info lwi = { 0 };
3073 struct lfsck_thread_args *lta;
3074 struct task_struct *task;
3075 struct lfsck_tgt_descs *ltds;
3076 struct lfsck_tgt_desc *ltd;
3087 lfsck = lfsck_instance_find(key, true, false);
3088 if (unlikely(lfsck == NULL))
3091 if (unlikely(lfsck->li_stopping))
3092 GOTO(put, rc = -ENXIO);
3094 /* System is not ready, try again later. */
3095 if (unlikely(lfsck->li_namespace == NULL ||
3096 lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3097 GOTO(put, rc = -EINPROGRESS);
3099 /* start == NULL means auto trigger paused LFSCK. */
3101 if (list_empty(&lfsck->li_list_scan) ||
3102 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3104 } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3105 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3106 lfsck_lfsck2name(lfsck));
3108 GOTO(put, rc = -EPERM);
3111 bk = &lfsck->li_bookmark_ram;
3112 thread = &lfsck->li_thread;
3113 mutex_lock(&lfsck->li_mutex);
3114 spin_lock(&lfsck->li_lock);
3115 if (unlikely(thread_is_stopping(thread))) {
3116 /* Someone is stopping the LFSCK. */
3117 spin_unlock(&lfsck->li_lock);
3118 GOTO(out, rc = -EBUSY);
3121 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3123 if (unlikely(start == NULL)) {
3124 spin_unlock(&lfsck->li_lock);
3128 while (start->ls_active != 0) {
3129 if (!(type & start->ls_active)) {
3134 com = __lfsck_component_find(lfsck, type,
3135 &lfsck->li_list_scan);
3137 com = __lfsck_component_find(lfsck, type,
3138 &lfsck->li_list_double_scan);
3144 if (com->lc_ops->lfsck_join != NULL) {
3145 rc = com->lc_ops->lfsck_join( env, com, lsp);
3146 if (rc != 0 && rc != -EALREADY)
3149 start->ls_active &= ~type;
3152 spin_unlock(&lfsck->li_lock);
3155 spin_unlock(&lfsck->li_lock);
3157 lfsck->li_status = 0;
3158 lfsck->li_oit_over = 0;
3159 lfsck->li_start_unplug = 0;
3160 lfsck->li_drop_dryrun = 0;
3161 lfsck->li_new_scanned = 0;
3163 /* For auto trigger. */
3167 start->ls_version = bk->lb_version;
3169 if (start->ls_active != 0) {
3170 struct lfsck_component *next;
3172 if (start->ls_active == LFSCK_TYPES_ALL)
3173 start->ls_active = LFSCK_TYPES_SUPPORTED;
3175 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3176 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3177 GOTO(out, rc = -ENOTSUPP);
3180 list_for_each_entry_safe(com, next,
3181 &lfsck->li_list_scan, lc_link) {
3182 if (!(com->lc_type & start->ls_active)) {
3183 rc = com->lc_ops->lfsck_post(env, com, 0,
3190 while (start->ls_active != 0) {
3191 if (type & start->ls_active) {
3192 com = __lfsck_component_find(lfsck, type,
3193 &lfsck->li_list_idle);
3195 /* The component status will be updated
3196 * when its prep() is called later by
3197 * the LFSCK main engine. */
3198 list_move_tail(&com->lc_link,
3199 &lfsck->li_list_scan);
3200 start->ls_active &= ~type;
3206 if (list_empty(&lfsck->li_list_scan)) {
3207 /* The speed limit will be used to control both the LFSCK and
3208 * low layer scrub (if applied), need to be handled firstly. */
3209 if (start->ls_valid & LSV_SPEED_LIMIT) {
3210 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3211 rc = lfsck_bookmark_store(env, lfsck);
3220 if (start->ls_flags & LPF_RESET)
3221 flags |= DOIF_RESET;
3223 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3227 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3228 start->ls_active |= com->lc_type;
3229 if (flags & DOIF_RESET) {
3230 rc = com->lc_ops->lfsck_reset(env, com, false);
3236 ltds = &lfsck->li_mdt_descs;
3237 down_read(<ds->ltd_rw_sem);
3238 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3239 ltd = lfsck_ltd2tgt(ltds, idx);
3240 LASSERT(ltd != NULL);
3242 ltd->ltd_layout_done = 0;
3243 ltd->ltd_namespace_done = 0;
3244 ltd->ltd_synced_failures = 0;
3245 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3246 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3247 list_del_init(<d->ltd_layout_phase_list);
3248 list_del_init(<d->ltd_layout_list);
3249 list_del_init(<d->ltd_namespace_phase_list);
3250 list_del_init(<d->ltd_namespace_list);
3252 up_read(<ds->ltd_rw_sem);
3254 ltds = &lfsck->li_ost_descs;
3255 down_read(<ds->ltd_rw_sem);
3256 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3257 ltd = lfsck_ltd2tgt(ltds, idx);
3258 LASSERT(ltd != NULL);
3260 ltd->ltd_layout_done = 0;
3261 ltd->ltd_synced_failures = 0;
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);
3266 up_read(<ds->ltd_rw_sem);
3269 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3270 if (bk->lb_param & LPF_DRYRUN)
3271 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3273 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3274 valid |= DOIV_ERROR_HANDLE;
3275 if (start->ls_flags & LPF_FAILOUT)
3276 flags |= DOIF_FAILOUT;
3279 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3280 valid |= DOIV_DRYRUN;
3281 if (start->ls_flags & LPF_DRYRUN)
3282 flags |= DOIF_DRYRUN;
3285 if (!list_empty(&lfsck->li_list_scan))
3286 flags |= DOIF_OUTUSED;
3288 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3289 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3291 GOTO(out, rc = PTR_ERR(lta));
3293 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3294 spin_lock(&lfsck->li_lock);
3295 thread_set_flags(thread, SVC_STARTING);
3296 spin_unlock(&lfsck->li_lock);
3297 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3300 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3301 lfsck_lfsck2name(lfsck), rc);
3302 lfsck_thread_args_fini(lta);
3307 l_wait_event(thread->t_ctl_waitq,
3308 thread_is_running(thread) ||
3309 thread_is_stopped(thread),
3311 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3312 lfsck->li_start_unplug = 1;
3313 wake_up_all(&thread->t_ctl_waitq);
3318 /* release lfsck::li_mutex to avoid deadlock. */
3319 mutex_unlock(&lfsck->li_mutex);
3320 rc = lfsck_start_all(env, lfsck, start);
3322 spin_lock(&lfsck->li_lock);
3323 if (thread_is_stopped(thread)) {
3324 spin_unlock(&lfsck->li_lock);
3326 lfsck->li_status = LS_FAILED;
3327 lfsck->li_flags = 0;
3328 thread_set_flags(thread, SVC_STOPPING);
3329 spin_unlock(&lfsck->li_lock);
3331 lfsck->li_start_unplug = 1;
3332 wake_up_all(&thread->t_ctl_waitq);
3333 l_wait_event(thread->t_ctl_waitq,
3334 thread_is_stopped(thread),
3338 lfsck->li_start_unplug = 1;
3339 wake_up_all(&thread->t_ctl_waitq);
3345 mutex_unlock(&lfsck->li_mutex);
3348 lfsck_instance_put(env, lfsck);
3350 return rc < 0 ? rc : 0;
3352 EXPORT_SYMBOL(lfsck_start);
3354 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3355 struct lfsck_stop *stop)
3357 struct lfsck_instance *lfsck;
3358 struct ptlrpc_thread *thread;
3359 struct l_wait_info lwi = { 0 };
3364 lfsck = lfsck_instance_find(key, true, false);
3365 if (unlikely(lfsck == NULL))
3368 thread = &lfsck->li_thread;
3369 if (stop && stop->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3370 CERROR("%s: only allow to specify '-A' via MDS\n",
3371 lfsck_lfsck2name(lfsck));
3372 GOTO(put, rc = -EPERM);
3375 spin_lock(&lfsck->li_lock);
3376 /* The target is umounted */
3377 if (stop && stop->ls_status == LS_PAUSED)
3378 lfsck->li_stopping = 1;
3380 if (thread_is_init(thread) || thread_is_stopped(thread))
3381 /* no error if LFSCK stopped already, or not started */
3382 GOTO(unlock, rc = 0);
3384 if (thread_is_stopping(thread))
3385 /* Someone is stopping LFSCK. */
3386 GOTO(unlock, rc = -EINPROGRESS);
3389 lfsck->li_status = stop->ls_status;
3390 lfsck->li_flags = stop->ls_flags;
3392 lfsck->li_status = LS_STOPPED;
3393 lfsck->li_flags = 0;
3396 thread_set_flags(thread, SVC_STOPPING);
3398 if (lfsck->li_master) {
3399 struct lfsck_component *com;
3400 struct lfsck_assistant_data *lad;
3402 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3404 spin_lock(&lad->lad_lock);
3405 if (lad->lad_task != NULL)
3406 force_sig(SIGINT, lad->lad_task);
3407 spin_unlock(&lad->lad_lock);
3410 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3412 spin_lock(&lad->lad_lock);
3413 if (lad->lad_task != NULL)
3414 force_sig(SIGINT, lad->lad_task);
3415 spin_unlock(&lad->lad_lock);
3419 wake_up_all(&thread->t_ctl_waitq);
3420 spin_unlock(&lfsck->li_lock);
3421 if (stop && stop->ls_flags & LPF_BROADCAST)
3422 rc1 = lfsck_stop_all(env, lfsck, stop);
3424 /* It was me set the status as 'stopping' just now, if it is not
3425 * 'stopping' now, then either stopped, or re-started by race. */
3426 l_wait_event(thread->t_ctl_waitq,
3427 !thread_is_stopping(thread),
3433 spin_unlock(&lfsck->li_lock);
3435 lfsck_instance_put(env, lfsck);
3437 return rc != 0 ? rc : rc1;
3439 EXPORT_SYMBOL(lfsck_stop);
3441 int lfsck_in_notify_local(const struct lu_env *env, struct dt_device *key,
3442 struct lfsck_req_local *lrl, struct thandle *th)
3444 struct lfsck_instance *lfsck;
3445 struct lfsck_component *com;
3446 int rc = -EOPNOTSUPP;
3449 lfsck = lfsck_instance_find(key, true, false);
3450 if (unlikely(!lfsck))
3453 com = lfsck_component_find(lfsck, lrl->lrl_active);
3454 if (likely(com && com->lc_ops->lfsck_in_notify_local)) {
3455 rc = com->lc_ops->lfsck_in_notify_local(env, com, lrl, th);
3456 lfsck_component_put(env, com);
3459 lfsck_instance_put(env, lfsck);
3463 EXPORT_SYMBOL(lfsck_in_notify_local);
3465 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3466 struct lfsck_request *lr)
3468 int rc = -EOPNOTSUPP;
3471 switch (lr->lr_event) {
3473 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3474 struct lfsck_start_param lsp;
3476 memset(start, 0, sizeof(*start));
3477 start->ls_valid = lr->lr_valid;
3478 start->ls_speed_limit = lr->lr_speed;
3479 start->ls_version = lr->lr_version;
3480 start->ls_active = lr->lr_active;
3481 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3482 start->ls_async_windows = lr->lr_async_windows;
3484 lsp.lsp_start = start;
3485 lsp.lsp_index = lr->lr_index;
3486 lsp.lsp_index_valid = 1;
3487 rc = lfsck_start(env, key, &lsp);
3491 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3493 memset(stop, 0, sizeof(*stop));
3494 stop->ls_status = lr->lr_status;
3495 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3496 rc = lfsck_stop(env, key, stop);
3499 case LE_PHASE1_DONE:
3500 case LE_PHASE2_DONE:
3502 case LE_CONDITIONAL_DESTROY:
3503 case LE_SET_LMV_MASTER:
3504 case LE_SET_LMV_SLAVE:
3505 case LE_PAIRS_VERIFY: {
3506 struct lfsck_instance *lfsck;
3507 struct lfsck_component *com;
3509 lfsck = lfsck_instance_find(key, true, false);
3510 if (unlikely(lfsck == NULL))
3513 com = lfsck_component_find(lfsck, lr->lr_active);
3515 rc = com->lc_ops->lfsck_in_notify(env, com, lr);
3516 lfsck_component_put(env, com);
3519 lfsck_instance_put(env, lfsck);
3528 EXPORT_SYMBOL(lfsck_in_notify);
3530 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3531 struct lfsck_request *req, struct lfsck_reply *rep,
3532 struct lfsck_query *que)
3534 struct lfsck_instance *lfsck;
3535 struct lfsck_component *com;
3541 lfsck = lfsck_instance_find(key, true, false);
3542 if (unlikely(lfsck == NULL))
3546 if (que->lu_types == LFSCK_TYPES_ALL)
3548 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3550 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3551 que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3553 GOTO(out, rc = -ENOTSUPP);
3556 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3557 i++, type = 1 << i) {
3558 if (!(que->lu_types & type))
3562 com = lfsck_component_find(lfsck, type);
3563 if (unlikely(com == NULL))
3564 GOTO(out, rc = -ENOTSUPP);
3566 memset(que->lu_mdts_count[i], 0,
3567 sizeof(__u32) * (LS_MAX + 1));
3568 memset(que->lu_osts_count[i], 0,
3569 sizeof(__u32) * (LS_MAX + 1));
3570 que->lu_repaired[i] = 0;
3571 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3573 lfsck_component_put(env, com);
3578 if (!(que->lu_flags & LPF_WAIT))
3581 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3582 i++, type = 1 << i) {
3583 if (!(que->lu_types & type))
3586 if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3587 que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3588 que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3589 que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3590 struct l_wait_info lwi;
3592 /* If it is required to wait, then sleep
3593 * 3 seconds and try to query again. */
3594 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3598 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3600 if (rc == -ETIMEDOUT)
3605 com = lfsck_component_find(lfsck, req->lr_active);
3606 if (likely(com != NULL)) {
3607 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3609 lfsck_component_put(env, com);
3618 lfsck_instance_put(env, lfsck);
3621 EXPORT_SYMBOL(lfsck_query);
3623 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3624 struct ldlm_namespace *ns)
3626 struct lfsck_instance *lfsck;
3629 lfsck = lfsck_instance_find(key, true, false);
3630 if (likely(lfsck != NULL)) {
3631 lfsck->li_namespace = ns;
3632 lfsck_instance_put(env, lfsck);
3638 EXPORT_SYMBOL(lfsck_register_namespace);
3640 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3641 struct dt_device *next, struct obd_device *obd,
3642 lfsck_out_notify notify, void *notify_data, bool master)
3644 struct lfsck_instance *lfsck;
3645 struct dt_object *root = NULL;
3646 struct dt_object *obj = NULL;
3647 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3651 lfsck = lfsck_instance_find(key, false, false);
3652 if (unlikely(lfsck != NULL))
3655 OBD_ALLOC_PTR(lfsck);
3659 mutex_init(&lfsck->li_mutex);
3660 spin_lock_init(&lfsck->li_lock);
3661 INIT_LIST_HEAD(&lfsck->li_link);
3662 INIT_LIST_HEAD(&lfsck->li_list_scan);
3663 INIT_LIST_HEAD(&lfsck->li_list_dir);
3664 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3665 INIT_LIST_HEAD(&lfsck->li_list_idle);
3666 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3667 atomic_set(&lfsck->li_ref, 1);
3668 atomic_set(&lfsck->li_double_scan_count, 0);
3669 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3670 lfsck->li_out_notify = notify;
3671 lfsck->li_out_notify_data = notify_data;
3672 lfsck->li_next = next;
3673 lfsck->li_bottom = key;
3674 lfsck->li_obd = obd;
3676 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3680 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3684 fid->f_seq = FID_SEQ_LOCAL_NAME;
3687 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3691 rc = dt_root_get(env, key, fid);
3695 root = dt_locate(env, key, fid);
3697 GOTO(out, rc = PTR_ERR(root));
3699 if (unlikely(!dt_try_as_dir(env, root)))
3700 GOTO(out, rc = -ENOTDIR);
3702 lfsck->li_local_root_fid = *fid;
3704 lfsck->li_master = 1;
3705 if (lfsck_dev_idx(lfsck) == 0) {
3706 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3707 const struct lu_name *cname;
3709 rc = dt_lookup(env, root,
3710 (struct dt_rec *)(&lfsck->li_global_root_fid),
3711 (const struct dt_key *)"ROOT");
3715 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3717 GOTO(out, rc = PTR_ERR(obj));
3719 if (unlikely(!dt_try_as_dir(env, obj)))
3720 GOTO(out, rc = -ENOTDIR);
3722 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3723 (const struct dt_key *)dotlustre);
3727 lfsck_object_put(env, obj);
3728 obj = dt_locate(env, key, fid);
3730 GOTO(out, rc = PTR_ERR(obj));
3732 cname = lfsck_name_get_const(env, dotlustre,
3734 rc = lfsck_verify_linkea(env, obj, cname,
3735 &lfsck->li_global_root_fid);
3739 if (unlikely(!dt_try_as_dir(env, obj)))
3740 GOTO(out, rc = -ENOTDIR);
3743 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3744 (const struct dt_key *)lostfound);
3748 lfsck_object_put(env, obj);
3749 obj = dt_locate(env, key, fid);
3751 GOTO(out, rc = PTR_ERR(obj));
3753 cname = lfsck_name_get_const(env, lostfound,
3755 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3759 lfsck_object_put(env, obj);
3764 fid->f_seq = FID_SEQ_LOCAL_FILE;
3765 fid->f_oid = OTABLE_IT_OID;
3767 obj = dt_locate(env, key, fid);
3769 GOTO(out, rc = PTR_ERR(obj));
3771 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3775 lfsck->li_obj_oit = obj;
3776 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3777 S_IFDIR | S_IRUGO | S_IWUSR);
3779 GOTO(out, rc = PTR_ERR(obj));
3781 lu_object_get(&obj->do_lu);
3782 lfsck->li_lfsck_dir = obj;
3783 rc = lfsck_bookmark_setup(env, lfsck);
3788 rc = lfsck_fid_init(lfsck);
3792 rc = lfsck_namespace_setup(env, lfsck);
3797 rc = lfsck_layout_setup(env, lfsck);
3801 /* XXX: more LFSCK components initialization to be added here. */
3803 rc = lfsck_instance_add(lfsck);
3805 rc = lfsck_add_target_from_orphan(env, lfsck);
3807 if (obj != NULL && !IS_ERR(obj))
3808 lfsck_object_put(env, obj);
3809 if (root != NULL && !IS_ERR(root))
3810 lfsck_object_put(env, root);
3812 lfsck_instance_cleanup(env, lfsck);
3815 EXPORT_SYMBOL(lfsck_register);
3817 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3819 struct lfsck_instance *lfsck;
3821 lfsck = lfsck_instance_find(key, false, true);
3823 lfsck_instance_put(env, lfsck);
3825 EXPORT_SYMBOL(lfsck_degister);
3827 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3828 struct dt_device *tgt, struct obd_export *exp,
3829 __u32 index, bool for_ost)
3831 struct lfsck_instance *lfsck;
3832 struct lfsck_tgt_desc *ltd;
3843 INIT_LIST_HEAD(<d->ltd_orphan_list);
3844 INIT_LIST_HEAD(<d->ltd_layout_list);
3845 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3846 INIT_LIST_HEAD(<d->ltd_namespace_list);
3847 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3848 atomic_set(<d->ltd_ref, 1);
3849 ltd->ltd_index = index;
3851 spin_lock(&lfsck_instance_lock);
3852 lfsck = __lfsck_instance_find(key, true, false);
3853 if (lfsck == NULL) {
3855 list_add_tail(<d->ltd_orphan_list,
3856 &lfsck_ost_orphan_list);
3858 list_add_tail(<d->ltd_orphan_list,
3859 &lfsck_mdt_orphan_list);
3860 spin_unlock(&lfsck_instance_lock);
3864 spin_unlock(&lfsck_instance_lock);
3866 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3870 lfsck_instance_put(env, lfsck);
3874 EXPORT_SYMBOL(lfsck_add_target);
3876 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3877 struct dt_device *tgt, __u32 index, bool for_ost)
3879 struct lfsck_instance *lfsck;
3880 struct lfsck_tgt_descs *ltds;
3881 struct lfsck_tgt_desc *ltd;
3882 struct list_head *head;
3885 head = &lfsck_ost_orphan_list;
3887 head = &lfsck_mdt_orphan_list;
3889 spin_lock(&lfsck_instance_lock);
3890 list_for_each_entry(ltd, head, ltd_orphan_list) {
3891 if (ltd->ltd_tgt == tgt) {
3892 list_del_init(<d->ltd_orphan_list);
3893 spin_unlock(&lfsck_instance_lock);
3901 lfsck = __lfsck_instance_find(key, true, false);
3902 spin_unlock(&lfsck_instance_lock);
3903 if (unlikely(lfsck == NULL))
3907 ltds = &lfsck->li_ost_descs;
3909 ltds = &lfsck->li_mdt_descs;
3911 down_write(<ds->ltd_rw_sem);
3912 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3914 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3917 ltd = lfsck_ltd2tgt(ltds, index);
3918 if (unlikely(ltd == NULL))
3921 LASSERT(ltds->ltd_tgtnr > 0);
3924 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3925 lfsck_assign_tgt(ltds, NULL, index);
3930 head = &lfsck->li_ost_descs.ltd_orphan;
3932 head = &lfsck->li_mdt_descs.ltd_orphan;
3934 list_for_each_entry(ltd, head, ltd_orphan_list) {
3935 if (ltd->ltd_tgt == tgt) {
3936 list_del_init(<d->ltd_orphan_list);
3942 up_write(<ds->ltd_rw_sem);
3944 spin_lock(<ds->ltd_lock);
3946 spin_unlock(<ds->ltd_lock);
3947 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3948 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3952 lfsck_instance_put(env, lfsck);
3954 EXPORT_SYMBOL(lfsck_del_target);
3956 static int __init lfsck_init(void)
3960 INIT_LIST_HEAD(&lfsck_instance_list);
3961 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3962 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3963 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3964 rc = lu_context_key_register(&lfsck_thread_key);
3966 tgt_register_lfsck_in_notify_local(lfsck_in_notify_local);
3967 tgt_register_lfsck_in_notify(lfsck_in_notify);
3968 tgt_register_lfsck_query(lfsck_query);
3974 static void __exit lfsck_exit(void)
3976 struct lfsck_tgt_desc *ltd;
3977 struct lfsck_tgt_desc *next;
3979 LASSERT(list_empty(&lfsck_instance_list));
3981 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3983 list_del_init(<d->ltd_orphan_list);
3987 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3989 list_del_init(<d->ltd_orphan_list);
3993 lu_context_key_degister(&lfsck_thread_key);
3996 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3997 MODULE_DESCRIPTION("Lustre File System Checker");
3998 MODULE_VERSION(LUSTRE_VERSION_STRING);
3999 MODULE_LICENSE("GPL");
4001 module_init(lfsck_init);
4002 module_exit(lfsck_exit);