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, 2016, 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>
43 #include <lustre/lustre_lfsck_user.h>
45 #include "lfsck_internal.h"
47 #define LFSCK_CHECKPOINT_SKIP 1
49 /* define lfsck thread key */
50 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
52 static void lfsck_key_fini(const struct lu_context *ctx,
53 struct lu_context_key *key, void *data)
55 struct lfsck_thread_info *info = data;
57 lu_buf_free(&info->lti_linkea_buf);
58 lu_buf_free(&info->lti_linkea_buf2);
59 lu_buf_free(&info->lti_big_buf);
63 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
64 LU_KEY_INIT_GENERIC(lfsck);
66 static struct list_head lfsck_instance_list;
67 static struct list_head lfsck_ost_orphan_list;
68 static struct list_head lfsck_mdt_orphan_list;
69 static DEFINE_SPINLOCK(lfsck_instance_lock);
71 const char *lfsck_flags_names[] = {
80 const char *lfsck_param_names[] = {
90 "delay_create_ostobj",
94 enum lfsck_verify_lpf_types {
96 LVLT_BY_NAMEENTRY = 1,
100 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
102 if (type == LFSCK_TYPE_LAYOUT) {
103 ltd->ltd_layout_status = LS_MAX;
104 ltd->ltd_layout_repaired = 0;
106 ltd->ltd_namespace_status = LS_MAX;
107 ltd->ltd_namespace_repaired = 0;
111 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
113 spin_lock_init(<ds->ltd_lock);
114 init_rwsem(<ds->ltd_rw_sem);
115 INIT_LIST_HEAD(<ds->ltd_orphan);
116 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
117 if (ltds->ltd_tgts_bitmap == NULL)
123 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
125 struct lfsck_tgt_desc *ltd;
126 struct lfsck_tgt_desc *next;
129 down_write(<ds->ltd_rw_sem);
131 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
133 list_del_init(<d->ltd_orphan_list);
137 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
138 up_write(<ds->ltd_rw_sem);
143 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
144 ltd = lfsck_ltd2tgt(ltds, idx);
145 if (likely(ltd != NULL)) {
146 LASSERT(list_empty(<d->ltd_layout_list));
147 LASSERT(list_empty(<d->ltd_layout_phase_list));
148 LASSERT(list_empty(<d->ltd_namespace_list));
149 LASSERT(list_empty(<d->ltd_namespace_phase_list));
152 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
153 lfsck_assign_tgt(ltds, NULL, idx);
158 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
161 for (idx = 0; idx < TGT_PTRS; idx++) {
162 if (ltds->ltd_tgts_idx[idx] != NULL) {
163 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
164 ltds->ltd_tgts_idx[idx] = NULL;
168 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
169 ltds->ltd_tgts_bitmap = NULL;
170 up_write(<ds->ltd_rw_sem);
173 static int __lfsck_add_target(const struct lu_env *env,
174 struct lfsck_instance *lfsck,
175 struct lfsck_tgt_desc *ltd,
176 bool for_ost, bool locked)
178 struct lfsck_tgt_descs *ltds;
179 __u32 index = ltd->ltd_index;
184 ltds = &lfsck->li_ost_descs;
186 ltds = &lfsck->li_mdt_descs;
189 down_write(<ds->ltd_rw_sem);
191 LASSERT(ltds->ltd_tgts_bitmap != NULL);
193 if (index >= ltds->ltd_tgts_bitmap->size) {
194 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
195 (__u32)BITS_PER_LONG);
196 struct cfs_bitmap *old_bitmap = ltds->ltd_tgts_bitmap;
197 struct cfs_bitmap *new_bitmap;
199 while (newsize < index + 1)
202 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
203 if (new_bitmap == NULL)
204 GOTO(unlock, rc = -ENOMEM);
206 if (ltds->ltd_tgtnr > 0)
207 cfs_bitmap_copy(new_bitmap, old_bitmap);
208 ltds->ltd_tgts_bitmap = new_bitmap;
209 CFS_FREE_BITMAP(old_bitmap);
212 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
213 CERROR("%s: the device %s (%u) is registered already\n",
214 lfsck_lfsck2name(lfsck),
215 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
216 GOTO(unlock, rc = -EEXIST);
219 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
220 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
221 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
222 GOTO(unlock, rc = -ENOMEM);
225 lfsck_assign_tgt(ltds, ltd, index);
226 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
229 GOTO(unlock, rc = 0);
233 up_write(<ds->ltd_rw_sem);
238 static int lfsck_add_target_from_orphan(const struct lu_env *env,
239 struct lfsck_instance *lfsck)
241 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
242 struct lfsck_tgt_desc *ltd;
243 struct lfsck_tgt_desc *next;
244 struct list_head *head = &lfsck_ost_orphan_list;
249 spin_lock(&lfsck_instance_lock);
250 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
251 if (ltd->ltd_key == lfsck->li_bottom)
252 list_move_tail(<d->ltd_orphan_list,
255 spin_unlock(&lfsck_instance_lock);
257 down_write(<ds->ltd_rw_sem);
258 while (!list_empty(<ds->ltd_orphan)) {
259 ltd = list_entry(ltds->ltd_orphan.next,
260 struct lfsck_tgt_desc,
262 list_del_init(<d->ltd_orphan_list);
263 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
264 /* Do not hold the semaphore for too long time. */
265 up_write(<ds->ltd_rw_sem);
269 down_write(<ds->ltd_rw_sem);
271 up_write(<ds->ltd_rw_sem);
274 ltds = &lfsck->li_mdt_descs;
275 head = &lfsck_mdt_orphan_list;
283 static inline struct lfsck_component *
284 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
285 struct list_head *list)
287 struct lfsck_component *com;
289 list_for_each_entry(com, list, lc_link) {
290 if (com->lc_type == type)
296 struct lfsck_component *
297 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
299 struct lfsck_component *com;
301 spin_lock(&lfsck->li_lock);
302 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
306 com = __lfsck_component_find(lfsck, type,
307 &lfsck->li_list_double_scan);
311 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
315 lfsck_component_get(com);
316 spin_unlock(&lfsck->li_lock);
320 void lfsck_component_cleanup(const struct lu_env *env,
321 struct lfsck_component *com)
323 if (!list_empty(&com->lc_link))
324 list_del_init(&com->lc_link);
325 if (!list_empty(&com->lc_link_dir))
326 list_del_init(&com->lc_link_dir);
328 lfsck_component_put(env, com);
331 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
332 struct lu_fid *fid, bool locked)
334 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
339 mutex_lock(&lfsck->li_mutex);
341 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
343 bk->lb_last_fid = *fid;
344 /* We do not care about whether the subsequent sub-operations
345 * failed or not. The worst case is that one FID is lost that
346 * is not a big issue for the LFSCK since it is relative rare
347 * for LFSCK create. */
348 rc = lfsck_bookmark_store(env, lfsck);
352 mutex_unlock(&lfsck->li_mutex);
357 static int __lfsck_ibits_lock(const struct lu_env *env,
358 struct lfsck_instance *lfsck,
359 struct dt_object *obj, struct ldlm_res_id *resid,
360 struct lustre_handle *lh, __u64 bits,
363 struct lfsck_thread_info *info = lfsck_env_info(env);
364 union ldlm_policy_data *policy = &info->lti_policy;
365 __u64 flags = LDLM_FL_ATOMIC_CB;
368 LASSERT(lfsck->li_namespace != NULL);
370 memset(policy, 0, sizeof(*policy));
371 policy->l_inodebits.bits = bits;
372 if (dt_object_remote(obj)) {
373 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
375 memset(einfo, 0, sizeof(*einfo));
376 einfo->ei_type = LDLM_IBITS;
377 einfo->ei_mode = mode;
378 einfo->ei_cb_bl = ldlm_blocking_ast;
379 einfo->ei_cb_cp = ldlm_completion_ast;
380 einfo->ei_res_id = resid;
382 rc = dt_object_lock(env, obj, lh, einfo, policy);
384 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
385 LDLM_IBITS, policy, mode,
386 &flags, ldlm_blocking_ast,
387 ldlm_completion_ast, NULL, NULL,
388 0, LVB_T_NONE, NULL, lh);
391 if (rc == ELDLM_OK) {
394 memset(lh, 0, sizeof(*lh));
402 * Request the specified ibits lock for the given object.
404 * Before the LFSCK modifying on the namespace visible object,
405 * it needs to acquire related ibits ldlm lock.
407 * \param[in] env pointer to the thread context
408 * \param[in] lfsck pointer to the lfsck instance
409 * \param[in] obj pointer to the dt_object to be locked
410 * \param[out] lh pointer to the lock handle
411 * \param[in] bits the bits for the ldlm lock to be acquired
412 * \param[in] mode the mode for the ldlm lock to be acquired
414 * \retval 0 for success
415 * \retval negative error number on failure
417 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
418 struct dt_object *obj, struct lustre_handle *lh,
419 __u64 bits, enum ldlm_mode mode)
421 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
423 LASSERT(!lustre_handle_is_used(lh));
425 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
426 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
430 * Release the the specified ibits lock.
432 * If the lock has been acquired before, release it
433 * and cleanup the handle. Otherwise, do nothing.
435 * \param[in] lh pointer to the lock handle
436 * \param[in] mode the mode for the ldlm lock to be released
438 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
440 if (lustre_handle_is_used(lh)) {
441 ldlm_lock_decref(lh, mode);
442 memset(lh, 0, sizeof(*lh));
447 * Request compound ibits locks for the given <obj, name> pairs.
449 * Before the LFSCK modifying on the namespace visible object, it needs to
450 * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
451 * the lock purpose. But the simple lfsck_ibits_lock for directory-based
452 * modificationis (such as insert name entry to the directory) may be too
453 * coarse-grained and not efficient.
455 * The lfsck_lock() will request compound ibits locks on the specified
456 * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
457 * lock on the directory object, and the regular ibits lock on the name hash.
459 * \param[in] env pointer to the thread context
460 * \param[in] lfsck pointer to the lfsck instance
461 * \param[in] obj pointer to the dt_object to be locked
462 * \param[in] name used for building the PDO lock resource
463 * \param[out] llh pointer to the lfsck_lock_handle
464 * \param[in] bits the bits for the ldlm lock to be acquired
465 * \param[in] mode the mode for the ldlm lock to be acquired
467 * \retval 0 for success
468 * \retval negative error number on failure
470 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
471 struct dt_object *obj, const char *name,
472 struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
474 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
477 LASSERT(S_ISDIR(lfsck_object_type(obj)));
478 LASSERT(name != NULL);
479 LASSERT(name[0] != 0);
480 LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
481 LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
485 llh->llh_pdo_mode = LCK_EX;
488 llh->llh_pdo_mode = LCK_CW;
491 llh->llh_pdo_mode = LCK_CR;
494 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
495 DFID"\n", lfsck_lfsck2name(lfsck), mode,
496 PFID(lfsck_dto2fid(obj)));
500 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
501 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
502 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
506 llh->llh_reg_mode = mode;
507 resid->name[LUSTRE_RES_ID_HSH_OFF] = ll_full_name_hash(NULL, name,
509 LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
510 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
511 bits, llh->llh_reg_mode);
513 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
519 * Release the the compound ibits locks.
521 * \param[in] llh pointer to the lfsck_lock_handle to be released
523 void lfsck_unlock(struct lfsck_lock_handle *llh)
525 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
526 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
529 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
530 struct lfsck_instance *lfsck,
531 const struct lu_fid *fid)
533 struct seq_server_site *ss = lfsck_dev_site(lfsck);
534 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
537 if (unlikely(fid_seq(fid) == FID_SEQ_LOCAL_FILE)) {
538 /* "ROOT" is always on the MDT0. */
539 if (lu_fid_eq(fid, &lfsck->li_global_root_fid))
542 return lfsck_dev_idx(lfsck);
545 fld_range_set_mdt(range);
546 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
548 rc = range->lsr_index;
553 const char dot[] = ".";
554 const char dotdot[] = "..";
555 static const char dotlustre[] = ".lustre";
556 static const char lostfound[] = "lost+found";
559 * Remove the name entry from the .lustre/lost+found directory.
561 * No need to care about the object referenced by the name entry,
562 * either the name entry is invalid or redundant, or the referenced
563 * object has been processed or will be handled by others.
565 * \param[in] env pointer to the thread context
566 * \param[in] lfsck pointer to the lfsck instance
567 * \param[in] name the name for the name entry to be removed
569 * \retval 0 for success
570 * \retval negative error number on failure
572 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
573 struct lfsck_instance *lfsck,
576 struct dt_object *parent = lfsck->li_lpf_root_obj;
577 struct dt_device *dev = lfsck_obj2dev(parent);
579 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
583 rc = lfsck_lock(env, lfsck, parent, name, llh,
584 MDS_INODELOCK_UPDATE, LCK_PW);
588 th = dt_trans_create(env, dev);
590 GOTO(unlock, rc = PTR_ERR(th));
592 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
596 rc = dt_declare_ref_del(env, parent, th);
600 rc = dt_trans_start_local(env, dev, th);
604 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
608 dt_write_lock(env, parent, 0);
609 rc = dt_ref_del(env, parent, th);
610 dt_write_unlock(env, parent);
615 dt_trans_stop(env, dev, th);
620 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
621 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
626 static int lfsck_create_lpf_local(const struct lu_env *env,
627 struct lfsck_instance *lfsck,
628 struct dt_object *child,
630 struct dt_object_format *dof,
633 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
634 struct dt_object *parent = lfsck->li_lpf_root_obj;
635 struct dt_device *dev = lfsck_obj2dev(child);
636 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
637 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
638 const struct lu_fid *cfid = lfsck_dto2fid(child);
639 struct thandle *th = NULL;
640 struct linkea_data ldata = { NULL };
641 struct lu_buf linkea_buf;
642 const struct lu_name *cname;
644 int len = sizeof(struct lfsck_bookmark);
648 cname = lfsck_name_get_const(env, name, strlen(name));
649 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
650 cname, lfsck_dto2fid(parent));
654 th = dt_trans_create(env, dev);
658 /* 1a. create child */
659 rc = dt_declare_create(env, child, la, NULL, dof, th);
663 if (!dt_try_as_dir(env, child))
664 GOTO(stop, rc = -ENOTDIR);
666 /* 2a. increase child nlink */
667 rc = dt_declare_ref_add(env, child, th);
671 /* 3a. insert dot into child dir */
672 rec->rec_type = S_IFDIR;
674 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
675 (const struct dt_key *)dot, th);
679 /* 4a. insert dotdot into child dir */
680 rec->rec_fid = &LU_LPF_FID;
681 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
682 (const struct dt_key *)dotdot, th);
686 /* 5a. insert linkEA for child */
687 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
688 ldata.ld_leh->leh_len);
689 rc = dt_declare_xattr_set(env, child, &linkea_buf,
690 XATTR_NAME_LINK, 0, th);
694 /* 6a. insert name into parent dir */
695 rec->rec_type = S_IFDIR;
697 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
698 (const struct dt_key *)name, th);
702 /* 7a. increase parent nlink */
703 rc = dt_declare_ref_add(env, parent, th);
707 /* 8a. update bookmark */
708 rc = dt_declare_record_write(env, bk_obj,
709 lfsck_buf_get(env, bk, len), 0, th);
713 rc = dt_trans_start_local(env, dev, th);
717 dt_write_lock(env, child, 0);
718 /* 1b. create child */
719 rc = dt_create(env, child, la, NULL, dof, th);
723 /* 2b. increase child nlink */
724 rc = dt_ref_add(env, child, th);
728 /* 3b. insert dot into child dir */
730 rc = dt_insert(env, child, (const struct dt_rec *)rec,
731 (const struct dt_key *)dot, th, 1);
735 /* 4b. insert dotdot into child dir */
736 rec->rec_fid = &LU_LPF_FID;
737 rc = dt_insert(env, child, (const struct dt_rec *)rec,
738 (const struct dt_key *)dotdot, th, 1);
742 /* 5b. insert linkEA for child. */
743 rc = dt_xattr_set(env, child, &linkea_buf,
744 XATTR_NAME_LINK, 0, th);
745 dt_write_unlock(env, child);
749 /* 6b. insert name into parent dir */
751 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
752 (const struct dt_key *)name, th, 1);
756 dt_write_lock(env, parent, 0);
757 /* 7b. increase parent nlink */
758 rc = dt_ref_add(env, parent, th);
759 dt_write_unlock(env, parent);
763 bk->lb_lpf_fid = *cfid;
764 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
766 /* 8b. update bookmark */
767 rc = dt_record_write(env, bk_obj,
768 lfsck_buf_get(env, bk, len), &pos, th);
773 dt_write_unlock(env, child);
776 dt_trans_stop(env, dev, th);
781 static int lfsck_create_lpf_remote(const struct lu_env *env,
782 struct lfsck_instance *lfsck,
783 struct dt_object *child,
785 struct dt_object_format *dof,
788 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
789 struct dt_object *parent = lfsck->li_lpf_root_obj;
790 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
791 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
792 const struct lu_fid *cfid = lfsck_dto2fid(child);
793 struct thandle *th = NULL;
794 struct linkea_data ldata = { NULL };
795 struct lu_buf linkea_buf;
796 const struct lu_name *cname;
797 struct dt_device *dev;
799 int len = sizeof(struct lfsck_bookmark);
803 cname = lfsck_name_get_const(env, name, strlen(name));
804 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf2,
805 cname, lfsck_dto2fid(parent));
809 /* Create .lustre/lost+found/MDTxxxx. */
811 /* XXX: Currently, cross-MDT create operation needs to create the child
812 * object firstly, then insert name into the parent directory. For
813 * this case, the child object resides on current MDT (local), but
814 * the parent ".lustre/lost+found" may be on remote MDT. It is not
815 * easy to contain all the sub-modifications orderly within single
818 * To avoid more inconsistency, we split the create operation into
821 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
823 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
826 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
827 * repair such inconsistency when LFSCK run next time. */
829 /* Transaction I: locally */
831 dev = lfsck_obj2dev(child);
832 th = dt_trans_create(env, dev);
836 /* 1a. create child */
837 rc = dt_declare_create(env, child, la, NULL, dof, th);
841 if (!dt_try_as_dir(env, child))
842 GOTO(stop, rc = -ENOTDIR);
844 /* 2a. increase child nlink */
845 rc = dt_declare_ref_add(env, child, th);
849 /* 3a. insert dot into child dir */
850 rec->rec_type = S_IFDIR;
852 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
853 (const struct dt_key *)dot, th);
857 /* 4a. insert dotdot into child dir */
858 rec->rec_fid = &LU_LPF_FID;
859 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
860 (const struct dt_key *)dotdot, th);
864 /* 5a. insert linkEA for child */
865 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
866 ldata.ld_leh->leh_len);
867 rc = dt_declare_xattr_set(env, child, &linkea_buf,
868 XATTR_NAME_LINK, 0, th);
872 /* 6a. update bookmark */
873 rc = dt_declare_record_write(env, bk_obj,
874 lfsck_buf_get(env, bk, len), 0, th);
878 rc = dt_trans_start_local(env, dev, th);
882 dt_write_lock(env, child, 0);
883 /* 1b. create child */
884 rc = dt_create(env, child, la, NULL, dof, th);
888 /* 2b. increase child nlink */
889 rc = dt_ref_add(env, child, th);
893 /* 3b. insert dot into child dir */
894 rec->rec_type = S_IFDIR;
896 rc = dt_insert(env, child, (const struct dt_rec *)rec,
897 (const struct dt_key *)dot, th, 1);
901 /* 4b. insert dotdot into child dir */
902 rec->rec_fid = &LU_LPF_FID;
903 rc = dt_insert(env, child, (const struct dt_rec *)rec,
904 (const struct dt_key *)dotdot, th, 1);
908 /* 5b. insert linkEA for child */
909 rc = dt_xattr_set(env, child, &linkea_buf,
910 XATTR_NAME_LINK, 0, th);
914 bk->lb_lpf_fid = *cfid;
915 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
917 /* 6b. update bookmark */
918 rc = dt_record_write(env, bk_obj,
919 lfsck_buf_get(env, bk, len), &pos, th);
921 dt_write_unlock(env, child);
922 dt_trans_stop(env, dev, th);
926 /* Transaction II: remotely */
928 dev = lfsck_obj2dev(parent);
929 th = dt_trans_create(env, dev);
934 /* 5a. insert name into parent dir */
936 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
937 (const struct dt_key *)name, th);
941 /* 6a. increase parent nlink */
942 rc = dt_declare_ref_add(env, parent, th);
946 rc = dt_trans_start_local(env, dev, th);
950 /* 5b. insert name into parent dir */
951 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
952 (const struct dt_key *)name, th, 1);
956 dt_write_lock(env, parent, 0);
957 /* 6b. increase parent nlink */
958 rc = dt_ref_add(env, parent, th);
959 dt_write_unlock(env, parent);
964 dt_write_unlock(env, child);
966 dt_trans_stop(env, dev, th);
968 if (rc != 0 && dev == lfsck_obj2dev(parent))
969 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
970 "for orphans, but failed to insert the name %s "
971 "to the .lustre/lost+found/. Such inconsistency "
972 "will be repaired when LFSCK run next time: rc = %d\n",
973 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
979 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
981 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
982 * orphans and other uncertain inconsistent objects found during the
983 * LFSCK. Such directory will be created by the LFSCK engine on the
984 * local MDT before the LFSCK scanning.
986 * \param[in] env pointer to the thread context
987 * \param[in] lfsck pointer to the lfsck instance
989 * \retval 0 for success
990 * \retval negative error number on failure
992 static int lfsck_create_lpf(const struct lu_env *env,
993 struct lfsck_instance *lfsck)
995 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
996 struct lfsck_thread_info *info = lfsck_env_info(env);
997 struct lu_fid *cfid = &info->lti_fid2;
998 struct lu_attr *la = &info->lti_la;
999 struct dt_object_format *dof = &info->lti_dof;
1000 struct dt_object *parent = lfsck->li_lpf_root_obj;
1001 struct dt_object *child = NULL;
1002 struct lfsck_lock_handle *llh = &info->lti_llh;
1004 int node = lfsck_dev_idx(lfsck);
1008 LASSERT(lfsck->li_master);
1009 LASSERT(parent != NULL);
1010 LASSERT(lfsck->li_lpf_obj == NULL);
1012 snprintf(name, 8, "MDT%04x", node);
1013 rc = lfsck_lock(env, lfsck, parent, name, llh,
1014 MDS_INODELOCK_UPDATE, LCK_PW);
1018 if (fid_is_zero(&bk->lb_lpf_fid)) {
1019 /* There is corner case that: in former LFSCK scanning we have
1020 * created the .lustre/lost+found/MDTxxxx but failed to update
1021 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1022 * it from MDT0 firstly. */
1023 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1024 (const struct dt_key *)name);
1025 if (rc != 0 && rc != -ENOENT)
1029 bk->lb_lpf_fid = *cfid;
1030 rc = lfsck_bookmark_store(env, lfsck);
1032 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1037 *cfid = bk->lb_lpf_fid;
1040 child = lfsck_object_find_bottom_new(env, lfsck, cfid);
1042 GOTO(unlock, rc = PTR_ERR(child));
1044 if (dt_object_exists(child) != 0) {
1045 if (unlikely(!dt_try_as_dir(env, child)))
1048 lfsck->li_lpf_obj = child;
1053 memset(la, 0, sizeof(*la));
1054 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1055 la->la_mode = S_IFDIR | S_IRWXU;
1056 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1058 memset(dof, 0, sizeof(*dof));
1059 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1062 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1064 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1066 lfsck->li_lpf_obj = child;
1072 if (rc != 0 && child != NULL && !IS_ERR(child))
1073 lfsck_object_put(env, child);
1079 * Scan .lustre/lost+found for bad name entries and remove them.
1081 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1082 * index in the system. Any other formatted name is invalid and should be
1085 * \param[in] env pointer to the thread context
1086 * \param[in] lfsck pointer to the lfsck instance
1088 * \retval 0 for success
1089 * \retval negative error number on failure
1091 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1092 struct lfsck_instance *lfsck)
1094 struct dt_object *parent = lfsck->li_lpf_root_obj;
1095 struct lu_dirent *ent =
1096 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1097 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1102 it = iops->init(env, parent, LUDA_64BITHASH);
1104 RETURN(PTR_ERR(it));
1106 rc = iops->load(env, it, 0);
1108 rc = iops->next(env, it);
1115 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1119 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1120 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1123 /* name length must be strlen("MDTxxxx") */
1124 if (ent->lde_namelen != 7)
1127 if (memcmp(ent->lde_name, "MDT", off) != 0)
1130 while (off < 7 && isxdigit(ent->lde_name[off]))
1136 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1143 rc = iops->next(env, it);
1147 iops->fini(env, it);
1149 RETURN(rc > 0 ? 0 : rc);
1152 static int lfsck_update_lpf_entry(const struct lu_env *env,
1153 struct lfsck_instance *lfsck,
1154 struct dt_object *parent,
1155 struct dt_object *child,
1157 enum lfsck_verify_lpf_types type)
1161 if (type == LVLT_BY_BOOKMARK) {
1162 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1163 lfsck_dto2fid(child), S_IFDIR);
1164 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1165 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1166 rc = lfsck_bookmark_store(env, lfsck);
1168 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1169 " in the bookmark file: rc = %d\n",
1170 lfsck_lfsck2name(lfsck),
1171 PFID(lfsck_dto2fid(child)), rc);
1178 * Check whether the @child back references the @parent.
1181 * 1) The child's FID is stored in the bookmark file. If the child back
1182 * references the parent (LU_LPF_FID object) via its ".." entry, then
1183 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1184 * the child back references another parent2, then:
1185 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1186 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1187 * references the child. So keep them there. As the LFSCK processing,
1188 * the parent3 may be found, then when the LFSCK run next time, the
1189 * inconsistency can be repaired.
1191 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1192 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1193 * via its ".." entry, then update the bookmark file, otherwise, if the child
1194 * back references another parent2, then:
1195 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1196 * from .lustre/lost+found/;
1197 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1198 * sub-directory name entry and update the child;
1199 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1200 * or not, then keep them there.
1202 * \param[in] env pointer to the thread context
1203 * \param[in] lfsck pointer to the lfsck instance
1204 * \param[in] child pointer to the lost+found sub-directory object
1205 * \param[in] name the name for lost+found sub-directory object
1206 * \param[out] fid pointer to the buffer to hold the FID of the object
1207 * (called it as parent2) that is referenced via the
1208 * child's dotdot entry; it also can be the FID that
1209 * is referenced by the name entry under the parent2.
1210 * \param[in] type to indicate where the child's FID is stored in
1212 * \retval positive number for uncertain inconsistency
1213 * \retval 0 for success
1214 * \retval negative error number on failure
1216 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1217 struct lfsck_instance *lfsck,
1218 struct dt_object *child, const char *name,
1220 enum lfsck_verify_lpf_types type)
1222 struct dt_object *parent = lfsck->li_lpf_root_obj;
1223 struct lfsck_thread_info *info = lfsck_env_info(env);
1224 char *name2 = info->lti_key;
1225 struct lu_fid *fid2 = &info->lti_fid3;
1226 struct dt_object *parent2 = NULL;
1227 struct lustre_handle lh = { 0 };
1232 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1233 (const struct dt_key *)dotdot);
1237 if (!fid_is_sane(fid))
1238 GOTO(linkea, rc = -EINVAL);
1240 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1241 const struct lu_name *cname;
1243 if (lfsck->li_lpf_obj == NULL) {
1244 lu_object_get(&child->do_lu);
1245 lfsck->li_lpf_obj = child;
1248 cname = lfsck_name_get_const(env, name, strlen(name));
1249 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1251 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1257 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1258 if (IS_ERR(parent2))
1259 GOTO(linkea, parent2);
1261 if (!dt_object_exists(parent2)) {
1262 lfsck_object_put(env, parent2);
1264 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1267 if (!dt_try_as_dir(env, parent2)) {
1268 lfsck_object_put(env, parent2);
1270 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1274 /* To prevent rename/unlink race */
1275 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1276 MDS_INODELOCK_UPDATE, LCK_PR);
1280 dt_read_lock(env, child, 0);
1281 rc = lfsck_links_get_first(env, child, name2, fid2);
1283 dt_read_unlock(env, child);
1284 lfsck_ibits_unlock(&lh, LCK_PR);
1286 GOTO(out_put, rc = 1);
1289 /* It is almost impossible that the bookmark file (or the name entry)
1290 * and the linkEA hit the same data corruption. Trust the linkEA. */
1291 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1292 dt_read_unlock(env, child);
1293 lfsck_ibits_unlock(&lh, LCK_PR);
1296 if (lfsck->li_lpf_obj == NULL) {
1297 lu_object_get(&child->do_lu);
1298 lfsck->li_lpf_obj = child;
1301 /* Update the child's dotdot entry */
1302 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1303 &LU_LPF_FID, S_IFDIR);
1305 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1311 if (parent2 == NULL || IS_ERR(parent2)) {
1312 dt_read_unlock(env, child);
1313 lfsck_ibits_unlock(&lh, LCK_PR);
1315 GOTO(out_done, rc = 1);
1318 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1319 (const struct dt_key *)name2);
1320 dt_read_unlock(env, child);
1321 lfsck_ibits_unlock(&lh, LCK_PR);
1322 if (rc != 0 && rc != -ENOENT)
1325 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1326 if (type == LVLT_BY_BOOKMARK)
1327 GOTO(out_put, rc = 1);
1329 /* Trust the name entry, update the child's dotdot entry. */
1330 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1331 &LU_LPF_FID, S_IFDIR);
1336 if (type == LVLT_BY_BOOKMARK) {
1337 /* Invalid FID record in the bookmark file, reset it. */
1338 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1339 rc = lfsck_bookmark_store(env, lfsck);
1341 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1342 " in the bookmark file: rc = %d\n",
1343 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1344 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1345 /* The name entry is wrong, remove it. */
1346 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1352 if (parent2 != NULL && !IS_ERR(parent2))
1353 lfsck_object_put(env, parent2);
1360 * Verify the /ROOT/.lustre/lost+found/ directory.
1362 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1363 * the LFSCK does not exactly know how to handle, such as orphans. So before
1364 * the LFSCK scanning the system, the consistency of such directory needs to
1365 * be verified firstly to allow the users to use it during the LFSCK.
1367 * \param[in] env pointer to the thread context
1368 * \param[in] lfsck pointer to the lfsck instance
1370 * \retval positive number for uncertain inconsistency
1371 * \retval 0 for success
1372 * \retval negative error number on failure
1374 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1376 struct lfsck_thread_info *info = lfsck_env_info(env);
1377 struct lu_fid *pfid = &info->lti_fid;
1378 struct lu_fid *cfid = &info->lti_fid2;
1379 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1380 struct dt_object *parent;
1381 /* child1's FID is in the bookmark file. */
1382 struct dt_object *child1 = NULL;
1383 /* child2's FID is in the name entry MDTxxxx. */
1384 struct dt_object *child2 = NULL;
1385 const struct lu_name *cname;
1387 int node = lfsck_dev_idx(lfsck);
1391 LASSERT(lfsck->li_master);
1393 if (lfsck->li_lpf_root_obj != NULL)
1397 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1400 struct lfsck_tgt_desc *ltd;
1402 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1403 if (unlikely(ltd == NULL))
1406 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1412 RETURN(PTR_ERR(parent));
1414 LASSERT(dt_object_exists(parent));
1416 if (unlikely(!dt_try_as_dir(env, parent))) {
1417 lfsck_object_put(env, parent);
1419 GOTO(put, rc = -ENOTDIR);
1422 lfsck->li_lpf_root_obj = parent;
1424 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1426 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1427 "for bad sub-directories: rc = %d\n",
1428 lfsck_lfsck2name(lfsck), rc);
1432 snprintf(name, 8, "MDT%04x", node);
1433 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1434 (const struct dt_key *)name);
1435 if (rc == -ENOENT) {
1443 /* Invalid FID in the name entry, remove the name entry. */
1444 if (!fid_is_norm(cfid)) {
1445 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1452 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1454 GOTO(put, rc = PTR_ERR(child2));
1456 if (unlikely(!dt_object_exists(child2) ||
1457 dt_object_remote(child2)) ||
1458 !S_ISDIR(lfsck_object_type(child2))) {
1459 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1466 if (unlikely(!dt_try_as_dir(env, child2))) {
1467 lfsck_object_put(env, child2);
1473 if (fid_is_zero(&bk->lb_lpf_fid))
1476 if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1477 if (lfsck->li_lpf_obj == NULL) {
1478 lu_object_get(&child2->do_lu);
1479 lfsck->li_lpf_obj = child2;
1482 cname = lfsck_name_get_const(env, name, strlen(name));
1483 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1488 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1489 struct lu_fid tfid = bk->lb_lpf_fid;
1491 /* Invalid FID record in the bookmark file, reset it. */
1492 fid_zero(&bk->lb_lpf_fid);
1493 rc = lfsck_bookmark_store(env, lfsck);
1495 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1496 " in the bookmark file: rc = %d\n",
1497 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1505 child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1506 if (IS_ERR(child1)) {
1511 if (unlikely(!dt_object_exists(child1) ||
1512 dt_object_remote(child1)) ||
1513 !S_ISDIR(lfsck_object_type(child1))) {
1514 /* Invalid FID record in the bookmark file, reset it. */
1515 fid_zero(&bk->lb_lpf_fid);
1516 rc = lfsck_bookmark_store(env, lfsck);
1518 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1519 " in the bookmark file: rc = %d\n",
1520 lfsck_lfsck2name(lfsck),
1521 PFID(lfsck_dto2fid(child1)), rc);
1526 lfsck_object_put(env, child1);
1531 if (unlikely(!dt_try_as_dir(env, child1))) {
1532 lfsck_object_put(env, child1);
1538 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1540 if (lu_fid_eq(pfid, &LU_LPF_FID))
1545 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1546 pfid, LVLT_BY_NAMEENTRY);
1551 if (lfsck->li_lpf_obj != NULL) {
1552 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1553 lfsck_object_put(env, lfsck->li_lpf_obj);
1554 lfsck->li_lpf_obj = NULL;
1557 } else if (rc == 0) {
1558 rc = lfsck_create_lpf(env, lfsck);
1561 if (child2 != NULL && !IS_ERR(child2))
1562 lfsck_object_put(env, child2);
1563 if (child1 != NULL && !IS_ERR(child1))
1564 lfsck_object_put(env, child1);
1569 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1571 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1572 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1577 if (unlikely(ss == NULL))
1580 OBD_ALLOC_PTR(lfsck->li_seq);
1581 if (lfsck->li_seq == NULL)
1584 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1586 GOTO(out, rc = -ENOMEM);
1588 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1589 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1591 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1595 if (fid_is_sane(&bk->lb_last_fid))
1596 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1601 OBD_FREE_PTR(lfsck->li_seq);
1602 lfsck->li_seq = NULL;
1607 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1609 if (lfsck->li_seq != NULL) {
1610 seq_client_fini(lfsck->li_seq);
1611 OBD_FREE_PTR(lfsck->li_seq);
1612 lfsck->li_seq = NULL;
1616 void lfsck_instance_cleanup(const struct lu_env *env,
1617 struct lfsck_instance *lfsck)
1619 struct ptlrpc_thread *thread = &lfsck->li_thread;
1620 struct lfsck_component *com;
1621 struct lfsck_component *next;
1622 struct lfsck_lmv_unit *llu;
1623 struct lfsck_lmv_unit *llu_next;
1624 struct lfsck_lmv *llmv;
1627 LASSERT(list_empty(&lfsck->li_link));
1628 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1630 if (lfsck->li_obj_oit != NULL) {
1631 lfsck_object_put(env, lfsck->li_obj_oit);
1632 lfsck->li_obj_oit = NULL;
1635 LASSERT(lfsck->li_obj_dir == NULL);
1636 LASSERT(lfsck->li_lmv == NULL);
1638 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1639 llmv = &llu->llu_lmv;
1641 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1642 "still in using: %u\n",
1643 atomic_read(&llmv->ll_ref));
1645 lfsck_lmv_put(env, llmv);
1648 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1649 lfsck_component_cleanup(env, com);
1652 LASSERT(list_empty(&lfsck->li_list_dir));
1654 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1656 lfsck_component_cleanup(env, com);
1659 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1660 lfsck_component_cleanup(env, com);
1663 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1664 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1666 if (lfsck->li_lfsck_dir != NULL) {
1667 lfsck_object_put(env, lfsck->li_lfsck_dir);
1668 lfsck->li_lfsck_dir = NULL;
1671 if (lfsck->li_bookmark_obj != NULL) {
1672 lfsck_object_put(env, lfsck->li_bookmark_obj);
1673 lfsck->li_bookmark_obj = NULL;
1676 if (lfsck->li_lpf_obj != NULL) {
1677 lfsck_object_put(env, lfsck->li_lpf_obj);
1678 lfsck->li_lpf_obj = NULL;
1681 if (lfsck->li_lpf_root_obj != NULL) {
1682 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1683 lfsck->li_lpf_root_obj = NULL;
1686 if (lfsck->li_los != NULL) {
1687 local_oid_storage_fini(env, lfsck->li_los);
1688 lfsck->li_los = NULL;
1691 lfsck_fid_fini(lfsck);
1693 OBD_FREE_PTR(lfsck);
1696 static inline struct lfsck_instance *
1697 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1699 struct lfsck_instance *lfsck;
1701 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1702 if (lfsck->li_bottom == key) {
1704 lfsck_instance_get(lfsck);
1706 list_del_init(&lfsck->li_link);
1715 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1718 struct lfsck_instance *lfsck;
1720 spin_lock(&lfsck_instance_lock);
1721 lfsck = __lfsck_instance_find(key, ref, unlink);
1722 spin_unlock(&lfsck_instance_lock);
1727 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1729 struct lfsck_instance *tmp;
1731 spin_lock(&lfsck_instance_lock);
1732 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1733 if (lfsck->li_bottom == tmp->li_bottom) {
1734 spin_unlock(&lfsck_instance_lock);
1739 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1740 spin_unlock(&lfsck_instance_lock);
1744 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1749 bool newline = (bits != 0 ? false : true);
1751 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1753 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1756 if (names[i] != NULL) {
1760 seq_printf(m, "%s%c", names[i],
1761 newline ? '\n' : ',');
1770 void lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1773 seq_printf(m, "%s_time: N/A\n", name);
1774 seq_printf(m, "time_since_%s: N/A\n", name);
1776 seq_printf(m, "%s_time: %llu\n", name, time);
1777 seq_printf(m, "time_since_%s: %llu seconds\n",
1778 name, cfs_time_current_sec() - time);
1782 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1785 if (fid_is_zero(&pos->lp_dir_parent)) {
1786 if (pos->lp_oit_cookie == 0) {
1787 seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1790 seq_printf(m, "%s: %llu, N/A, N/A\n",
1791 prefix, pos->lp_oit_cookie);
1793 seq_printf(m, "%s: %llu, "DFID", %#llx\n",
1794 prefix, pos->lp_oit_cookie,
1795 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1799 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1800 struct lfsck_position *pos, bool init)
1802 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1804 if (unlikely(lfsck->li_di_oit == NULL)) {
1805 memset(pos, 0, sizeof(*pos));
1809 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1810 if (!lfsck->li_current_oit_processed && !init)
1811 pos->lp_oit_cookie--;
1813 LASSERT(pos->lp_oit_cookie > 0);
1815 if (lfsck->li_di_dir != NULL) {
1816 struct dt_object *dto = lfsck->li_obj_dir;
1818 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1821 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1822 fid_zero(&pos->lp_dir_parent);
1823 pos->lp_dir_cookie = 0;
1825 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1828 fid_zero(&pos->lp_dir_parent);
1829 pos->lp_dir_cookie = 0;
1833 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1837 if (limit != LFSCK_SPEED_NO_LIMIT) {
1838 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1839 lfsck->li_sleep_rate = limit /
1840 msecs_to_jiffies(MSEC_PER_SEC);
1841 lfsck->li_sleep_jif = 1;
1843 lfsck->li_sleep_rate = 1;
1844 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1848 lfsck->li_sleep_jif = 0;
1849 lfsck->li_sleep_rate = 0;
1852 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1853 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1860 void lfsck_control_speed(struct lfsck_instance *lfsck)
1862 struct ptlrpc_thread *thread = &lfsck->li_thread;
1863 struct l_wait_info lwi;
1865 if (lfsck->li_sleep_jif > 0 &&
1866 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1867 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1868 LWI_ON_SIGNAL_NOOP, NULL);
1870 l_wait_event(thread->t_ctl_waitq,
1871 !thread_is_running(thread),
1873 lfsck->li_new_scanned = 0;
1877 void lfsck_control_speed_by_self(struct lfsck_component *com)
1879 struct lfsck_instance *lfsck = com->lc_lfsck;
1880 struct ptlrpc_thread *thread = &lfsck->li_thread;
1881 struct l_wait_info lwi;
1883 if (lfsck->li_sleep_jif > 0 &&
1884 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1885 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1886 LWI_ON_SIGNAL_NOOP, NULL);
1888 l_wait_event(thread->t_ctl_waitq,
1889 !thread_is_running(thread),
1891 com->lc_new_scanned = 0;
1895 static struct lfsck_thread_args *
1896 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1897 struct lfsck_component *com,
1898 struct lfsck_start_param *lsp)
1900 struct lfsck_thread_args *lta;
1905 return ERR_PTR(-ENOMEM);
1907 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1913 lta->lta_lfsck = lfsck_instance_get(lfsck);
1915 lta->lta_com = lfsck_component_get(com);
1922 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1924 if (lta->lta_com != NULL)
1925 lfsck_component_put(<a->lta_env, lta->lta_com);
1926 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1927 lu_env_fini(<a->lta_env);
1931 struct lfsck_assistant_data *
1932 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1935 struct lfsck_assistant_data *lad;
1939 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1940 if (lad->lad_bitmap == NULL) {
1945 INIT_LIST_HEAD(&lad->lad_req_list);
1946 spin_lock_init(&lad->lad_lock);
1947 INIT_LIST_HEAD(&lad->lad_ost_list);
1948 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1949 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1950 INIT_LIST_HEAD(&lad->lad_mdt_list);
1951 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1952 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1953 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1955 lad->lad_name = name;
1961 struct lfsck_assistant_object *
1962 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1963 const struct lu_attr *attr, __u64 cookie,
1966 struct lfsck_assistant_object *lso;
1970 return ERR_PTR(-ENOMEM);
1972 lso->lso_fid = *fid;
1974 lso->lso_attr = *attr;
1976 atomic_set(&lso->lso_ref, 1);
1977 lso->lso_oit_cookie = cookie;
1979 lso->lso_is_dir = 1;
1985 lfsck_assistant_object_load(const struct lu_env *env,
1986 struct lfsck_instance *lfsck,
1987 struct lfsck_assistant_object *lso)
1989 struct dt_object *obj;
1991 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1995 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
1997 lfsck_object_put(env, obj);
1999 return ERR_PTR(-ENOENT);
2002 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2003 lfsck_object_put(env, obj);
2005 return ERR_PTR(-ENOTDIR);
2012 * Generic LFSCK asynchronous communication interpretor function.
2013 * The LFSCK RPC reply for both the event notification and status
2014 * querying will be handled here.
2016 * \param[in] env pointer to the thread context
2017 * \param[in] req pointer to the LFSCK request
2018 * \param[in] args pointer to the lfsck_async_interpret_args
2019 * \param[in] rc the result for handling the LFSCK request
2021 * \retval 0 for success
2022 * \retval negative error number on failure
2024 int lfsck_async_interpret_common(const struct lu_env *env,
2025 struct ptlrpc_request *req,
2028 struct lfsck_async_interpret_args *laia = args;
2029 struct lfsck_component *com = laia->laia_com;
2030 struct lfsck_assistant_data *lad = com->lc_data;
2031 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2032 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2033 struct lfsck_request *lr = laia->laia_lr;
2035 LASSERT(com->lc_lfsck->li_master);
2037 switch (lr->lr_event) {
2039 if (unlikely(rc == -EINPROGRESS)) {
2040 ltd->ltd_retry_start = 1;
2045 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2047 lfsck_lfsck2name(com->lc_lfsck),
2048 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2049 ltd->ltd_index, lad->lad_name, rc);
2051 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2052 struct lfsck_layout *lo = com->lc_file_ram;
2054 if (lr->lr_flags & LEF_TO_OST)
2055 lfsck_lad_set_bitmap(env, com,
2058 lo->ll_flags |= LF_INCOMPLETE;
2060 struct lfsck_namespace *ns = com->lc_file_ram;
2062 /* If some MDT does not join the namespace
2063 * LFSCK, then we cannot know whether there
2064 * is some name entry on such MDT that with
2065 * the referenced MDT-object on this MDT or
2066 * not. So the namespace LFSCK on this MDT
2067 * cannot handle orphan MDT-objects properly.
2068 * So we mark the LFSCK as LF_INCOMPLETE and
2069 * skip orphan MDT-objects handling. */
2070 ns->ln_flags |= LF_INCOMPLETE;
2075 spin_lock(<ds->ltd_lock);
2076 if (ltd->ltd_dead) {
2077 spin_unlock(<ds->ltd_lock);
2081 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2082 struct list_head *list;
2083 struct list_head *phase_list;
2085 if (ltd->ltd_layout_done) {
2086 spin_unlock(<ds->ltd_lock);
2090 if (lr->lr_flags & LEF_TO_OST) {
2091 list = &lad->lad_ost_list;
2092 phase_list = &lad->lad_ost_phase1_list;
2094 list = &lad->lad_mdt_list;
2095 phase_list = &lad->lad_mdt_phase1_list;
2098 if (list_empty(<d->ltd_layout_list))
2099 list_add_tail(<d->ltd_layout_list, list);
2100 if (list_empty(<d->ltd_layout_phase_list))
2101 list_add_tail(<d->ltd_layout_phase_list,
2104 if (ltd->ltd_namespace_done) {
2105 spin_unlock(<ds->ltd_lock);
2109 if (list_empty(<d->ltd_namespace_list))
2110 list_add_tail(<d->ltd_namespace_list,
2111 &lad->lad_mdt_list);
2112 if (list_empty(<d->ltd_namespace_phase_list))
2113 list_add_tail(<d->ltd_namespace_phase_list,
2114 &lad->lad_mdt_phase1_list);
2116 spin_unlock(<ds->ltd_lock);
2119 case LE_PHASE1_DONE:
2120 case LE_PHASE2_DONE:
2122 if (rc != 0 && rc != -EALREADY)
2123 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2124 "event = %d, rc = %d\n",
2125 lfsck_lfsck2name(com->lc_lfsck),
2126 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2127 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2130 struct lfsck_reply *reply;
2131 struct list_head *list;
2132 struct list_head *phase_list;
2134 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2135 list = <d->ltd_layout_list;
2136 phase_list = <d->ltd_layout_phase_list;
2138 list = <d->ltd_namespace_list;
2139 phase_list = <d->ltd_namespace_phase_list;
2143 if (lr->lr_flags & LEF_QUERY_ALL) {
2144 lfsck_reset_ltd_status(ltd, com->lc_type);
2148 spin_lock(<ds->ltd_lock);
2149 list_del_init(phase_list);
2150 list_del_init(list);
2151 spin_unlock(<ds->ltd_lock);
2155 reply = req_capsule_server_get(&req->rq_pill,
2157 if (reply == NULL) {
2159 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2160 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2163 if (lr->lr_flags & LEF_QUERY_ALL) {
2164 lfsck_reset_ltd_status(ltd, com->lc_type);
2168 spin_lock(<ds->ltd_lock);
2169 list_del_init(phase_list);
2170 list_del_init(list);
2171 spin_unlock(<ds->ltd_lock);
2175 if (lr->lr_flags & LEF_QUERY_ALL) {
2176 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2177 ltd->ltd_layout_status = reply->lr_status;
2178 ltd->ltd_layout_repaired = reply->lr_repaired;
2180 ltd->ltd_namespace_status = reply->lr_status;
2181 ltd->ltd_namespace_repaired =
2187 switch (reply->lr_status) {
2188 case LS_SCANNING_PHASE1:
2190 case LS_SCANNING_PHASE2:
2191 spin_lock(<ds->ltd_lock);
2192 list_del_init(phase_list);
2193 if (ltd->ltd_dead) {
2194 spin_unlock(<ds->ltd_lock);
2198 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2199 if (ltd->ltd_layout_done) {
2200 spin_unlock(<ds->ltd_lock);
2204 if (lr->lr_flags & LEF_TO_OST)
2205 list_add_tail(phase_list,
2206 &lad->lad_ost_phase2_list);
2208 list_add_tail(phase_list,
2209 &lad->lad_mdt_phase2_list);
2211 if (ltd->ltd_namespace_done) {
2212 spin_unlock(<ds->ltd_lock);
2216 list_add_tail(phase_list,
2217 &lad->lad_mdt_phase2_list);
2219 spin_unlock(<ds->ltd_lock);
2222 spin_lock(<ds->ltd_lock);
2223 list_del_init(phase_list);
2224 list_del_init(list);
2225 spin_unlock(<ds->ltd_lock);
2231 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2232 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2236 if (!laia->laia_shared) {
2238 lfsck_component_put(env, com);
2244 static void lfsck_interpret(const struct lu_env *env,
2245 struct lfsck_instance *lfsck,
2246 struct ptlrpc_request *req, void *args, int result)
2248 struct lfsck_async_interpret_args *laia = args;
2249 struct lfsck_component *com;
2251 LASSERT(laia->laia_com == NULL);
2252 LASSERT(laia->laia_shared);
2254 spin_lock(&lfsck->li_lock);
2255 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2256 laia->laia_com = com;
2257 lfsck_async_interpret_common(env, req, laia, result);
2260 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2261 laia->laia_com = com;
2262 lfsck_async_interpret_common(env, req, laia, result);
2264 spin_unlock(&lfsck->li_lock);
2267 static int lfsck_stop_notify(const struct lu_env *env,
2268 struct lfsck_instance *lfsck,
2269 struct lfsck_tgt_descs *ltds,
2270 struct lfsck_tgt_desc *ltd, __u16 type)
2272 struct lfsck_component *com;
2276 LASSERT(lfsck->li_master);
2278 spin_lock(&lfsck->li_lock);
2279 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2281 com = __lfsck_component_find(lfsck, type,
2282 &lfsck->li_list_double_scan);
2284 lfsck_component_get(com);
2285 spin_unlock(&lfsck->li_lock);
2288 struct lfsck_thread_info *info = lfsck_env_info(env);
2289 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2290 struct lfsck_request *lr = &info->lti_lr;
2291 struct lfsck_assistant_data *lad = com->lc_data;
2292 struct list_head *list;
2293 struct list_head *phase_list;
2294 struct ptlrpc_request_set *set;
2296 set = ptlrpc_prep_set();
2298 lfsck_component_put(env, com);
2303 if (type == LFSCK_TYPE_LAYOUT) {
2304 list = <d->ltd_layout_list;
2305 phase_list = <d->ltd_layout_phase_list;
2307 list = <d->ltd_namespace_list;
2308 phase_list = <d->ltd_namespace_phase_list;
2311 spin_lock(<ds->ltd_lock);
2312 if (list_empty(list)) {
2313 LASSERT(list_empty(phase_list));
2314 spin_unlock(<ds->ltd_lock);
2315 ptlrpc_set_destroy(set);
2320 list_del_init(phase_list);
2321 list_del_init(list);
2322 spin_unlock(<ds->ltd_lock);
2324 memset(lr, 0, sizeof(*lr));
2325 lr->lr_index = lfsck_dev_idx(lfsck);
2326 lr->lr_event = LE_PEER_EXIT;
2327 lr->lr_active = type;
2328 lr->lr_status = LS_CO_PAUSED;
2329 if (ltds == &lfsck->li_ost_descs)
2330 lr->lr_flags = LEF_TO_OST;
2332 memset(laia, 0, sizeof(*laia));
2333 laia->laia_com = com;
2334 laia->laia_ltds = ltds;
2335 atomic_inc(<d->ltd_ref);
2336 laia->laia_ltd = ltd;
2339 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2340 lfsck_async_interpret_common,
2341 laia, LFSCK_NOTIFY);
2343 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2344 "co-stop for %s: rc = %d\n",
2345 lfsck_lfsck2name(lfsck),
2346 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2347 ltd->ltd_index, lad->lad_name, rc);
2350 rc = ptlrpc_set_wait(set);
2353 ptlrpc_set_destroy(set);
2354 lfsck_component_put(env, com);
2360 static int lfsck_async_interpret(const struct lu_env *env,
2361 struct ptlrpc_request *req,
2364 struct lfsck_async_interpret_args *laia = args;
2365 struct lfsck_instance *lfsck;
2367 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2369 lfsck_interpret(env, lfsck, req, laia, rc);
2370 lfsck_tgt_put(laia->laia_ltd);
2371 if (rc != 0 && laia->laia_result != -EALREADY)
2372 laia->laia_result = rc;
2377 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2378 struct lfsck_request *lr,
2379 struct ptlrpc_request_set *set,
2380 ptlrpc_interpterer_t interpreter,
2381 void *args, int request)
2383 struct lfsck_async_interpret_args *laia;
2384 struct ptlrpc_request *req;
2385 struct lfsck_request *tmp;
2386 struct req_format *format;
2391 format = &RQF_LFSCK_NOTIFY;
2394 format = &RQF_LFSCK_QUERY;
2397 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2398 exp->exp_obd->obd_name, request, -EINVAL);
2402 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2406 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2408 ptlrpc_request_free(req);
2413 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2415 ptlrpc_request_set_replen(req);
2417 laia = ptlrpc_req_async_args(req);
2418 *laia = *(struct lfsck_async_interpret_args *)args;
2419 if (laia->laia_com != NULL)
2420 lfsck_component_get(laia->laia_com);
2421 req->rq_interpret_reply = interpreter;
2422 req->rq_allow_intr = 1;
2423 req->rq_no_delay = 1;
2424 ptlrpc_set_add_req(set, req);
2429 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2431 struct lfsck_thread_info *info = lfsck_env_info(env);
2432 struct lfsck_request *lr = &info->lti_lr;
2433 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2434 struct lfsck_instance *lfsck = com->lc_lfsck;
2435 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2436 struct lfsck_tgt_desc *ltd;
2437 struct ptlrpc_request_set *set;
2442 memset(lr, 0, sizeof(*lr));
2443 lr->lr_event = LE_QUERY;
2444 lr->lr_active = com->lc_type;
2445 lr->lr_flags = LEF_QUERY_ALL;
2447 memset(laia, 0, sizeof(*laia));
2448 laia->laia_com = com;
2451 set = ptlrpc_prep_set();
2456 laia->laia_ltds = ltds;
2457 down_read(<ds->ltd_rw_sem);
2458 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2459 ltd = lfsck_tgt_get(ltds, idx);
2460 LASSERT(ltd != NULL);
2462 laia->laia_ltd = ltd;
2463 up_read(<ds->ltd_rw_sem);
2464 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2465 lfsck_async_interpret_common,
2468 struct lfsck_assistant_data *lad = com->lc_data;
2470 CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2471 "rc = %d\n", lfsck_lfsck2name(lfsck),
2472 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2473 ltd->ltd_index, lad->lad_name, rc);
2474 lfsck_reset_ltd_status(ltd, com->lc_type);
2477 down_read(<ds->ltd_rw_sem);
2479 up_read(<ds->ltd_rw_sem);
2481 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2482 ltds = &lfsck->li_ost_descs;
2483 lr->lr_flags |= LEF_TO_OST;
2487 rc = ptlrpc_set_wait(set);
2488 ptlrpc_set_destroy(set);
2493 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2494 struct lfsck_start_param *lsp)
2496 struct lfsck_instance *lfsck = com->lc_lfsck;
2497 struct lfsck_assistant_data *lad = com->lc_data;
2498 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2499 struct ptlrpc_thread *athread = &lad->lad_thread;
2500 struct lfsck_thread_args *lta;
2501 struct task_struct *task;
2505 lad->lad_assistant_status = 0;
2506 lad->lad_post_result = 0;
2507 lad->lad_to_post = 0;
2508 lad->lad_to_double_scan = 0;
2509 lad->lad_in_double_scan = 0;
2511 lad->lad_advance_lock = false;
2512 thread_set_flags(athread, 0);
2514 lta = lfsck_thread_args_init(lfsck, com, lsp);
2516 RETURN(PTR_ERR(lta));
2518 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2521 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2522 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2523 lfsck_thread_args_fini(lta);
2525 struct l_wait_info lwi = { 0 };
2527 l_wait_event(mthread->t_ctl_waitq,
2528 thread_is_running(athread) ||
2529 thread_is_stopped(athread) ||
2530 !thread_is_starting(mthread),
2532 if (unlikely(!thread_is_starting(mthread)))
2533 /* stopped by race */
2535 else if (unlikely(!thread_is_running(athread)))
2536 rc = lad->lad_assistant_status;
2544 int lfsck_checkpoint_generic(const struct lu_env *env,
2545 struct lfsck_component *com)
2547 struct lfsck_assistant_data *lad = com->lc_data;
2548 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2549 struct ptlrpc_thread *athread = &lad->lad_thread;
2550 struct l_wait_info lwi = { 0 };
2552 l_wait_event(mthread->t_ctl_waitq,
2553 list_empty(&lad->lad_req_list) ||
2554 !thread_is_running(mthread) ||
2555 thread_is_stopped(athread),
2558 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2559 return LFSCK_CHECKPOINT_SKIP;
2564 void lfsck_post_generic(const struct lu_env *env,
2565 struct lfsck_component *com, int *result)
2567 struct lfsck_assistant_data *lad = com->lc_data;
2568 struct ptlrpc_thread *athread = &lad->lad_thread;
2569 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2570 struct l_wait_info lwi = { 0 };
2572 lad->lad_post_result = *result;
2575 lad->lad_to_post = 1;
2577 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2578 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2580 wake_up_all(&athread->t_ctl_waitq);
2581 l_wait_event(mthread->t_ctl_waitq,
2582 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2583 thread_is_stopped(athread),
2586 if (lad->lad_assistant_status < 0)
2587 *result = lad->lad_assistant_status;
2589 CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2590 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2593 int lfsck_double_scan_generic(const struct lu_env *env,
2594 struct lfsck_component *com, int status)
2596 struct lfsck_assistant_data *lad = com->lc_data;
2597 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2598 struct ptlrpc_thread *athread = &lad->lad_thread;
2599 struct l_wait_info lwi = { 0 };
2601 if (status != LS_SCANNING_PHASE2)
2604 lad->lad_to_double_scan = 1;
2606 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2608 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2610 wake_up_all(&athread->t_ctl_waitq);
2611 l_wait_event(mthread->t_ctl_waitq,
2612 lad->lad_in_double_scan ||
2613 thread_is_stopped(athread),
2616 CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2617 "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2618 lad->lad_assistant_status);
2620 if (lad->lad_assistant_status < 0)
2621 return lad->lad_assistant_status;
2626 void lfsck_quit_generic(const struct lu_env *env,
2627 struct lfsck_component *com)
2629 struct lfsck_assistant_data *lad = com->lc_data;
2630 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2631 struct ptlrpc_thread *athread = &lad->lad_thread;
2632 struct l_wait_info lwi = { 0 };
2635 wake_up_all(&athread->t_ctl_waitq);
2636 l_wait_event(mthread->t_ctl_waitq,
2637 thread_is_init(athread) ||
2638 thread_is_stopped(athread),
2642 int lfsck_load_one_trace_file(const struct lu_env *env,
2643 struct lfsck_component *com,
2644 struct dt_object *parent,
2645 struct dt_object **child,
2646 const struct dt_index_features *ft,
2647 const char *name, bool reset)
2649 struct lfsck_instance *lfsck = com->lc_lfsck;
2650 struct dt_object *obj;
2654 if (*child != NULL) {
2656 const struct dt_it_ops *iops;
2657 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid3;
2663 rc = obj->do_ops->do_index_try(env, obj, ft);
2665 /* unlink by force */
2668 iops = &obj->do_index_ops->dio_it;
2669 it = iops->init(env, obj, 0);
2671 /* unlink by force */
2675 rc = iops->get(env, it, (const struct dt_key *)fid);
2677 rc = iops->next(env, it);
2680 iops->fini(env, it);
2682 /* "rc > 0" means the index file is empty. */
2686 /* The old index is not empty, remove it firstly. */
2687 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
2688 CDEBUG_LIMIT(rc ? D_ERROR : D_LFSCK,
2689 "%s: unlink lfsck sub trace file %s: rc = %d\n",
2690 lfsck_lfsck2name(com->lc_lfsck), name, rc);
2695 lfsck_object_put(env, *child);
2702 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
2703 S_IFREG | S_IRUGO | S_IWUSR, ft);
2705 RETURN(PTR_ERR(obj));
2707 rc = obj->do_ops->do_index_try(env, obj, ft);
2709 lfsck_object_put(env, obj);
2716 int lfsck_load_sub_trace_files(const struct lu_env *env,
2717 struct lfsck_component *com,
2718 const struct dt_index_features *ft,
2719 const char *prefix, bool reset)
2721 char *name = lfsck_env_info(env)->lti_key;
2722 struct lfsck_sub_trace_obj *lsto;
2726 for (i = 0, rc = 0, lsto = &com->lc_sub_trace_objs[0];
2727 i < LFSCK_STF_COUNT && rc == 0; i++, lsto++) {
2728 snprintf(name, NAME_MAX, "%s_%02d", prefix, i);
2729 rc = lfsck_load_one_trace_file(env, com,
2730 com->lc_lfsck->li_lfsck_dir,
2731 &lsto->lsto_obj, ft, name, reset);
2737 /* external interfaces */
2739 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2742 struct lfsck_instance *lfsck;
2746 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2750 lfsck = lfsck_instance_find(key, true, false);
2751 if (likely(lfsck != NULL)) {
2752 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2753 lfsck_instance_put(&env, lfsck);
2762 EXPORT_SYMBOL(lfsck_get_speed);
2764 int lfsck_set_speed(struct dt_device *key, __u32 val)
2767 struct lfsck_instance *lfsck;
2771 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2775 lfsck = lfsck_instance_find(key, true, false);
2776 if (likely(lfsck != NULL)) {
2777 mutex_lock(&lfsck->li_mutex);
2778 if (__lfsck_set_speed(lfsck, val))
2779 rc = lfsck_bookmark_store(&env, lfsck);
2780 mutex_unlock(&lfsck->li_mutex);
2781 lfsck_instance_put(&env, lfsck);
2790 EXPORT_SYMBOL(lfsck_set_speed);
2792 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2795 struct lfsck_instance *lfsck;
2799 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2803 lfsck = lfsck_instance_find(key, true, false);
2804 if (likely(lfsck != NULL)) {
2805 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2806 lfsck_instance_put(&env, lfsck);
2815 EXPORT_SYMBOL(lfsck_get_windows);
2817 int lfsck_set_windows(struct dt_device *key, int val)
2820 struct lfsck_instance *lfsck;
2824 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2828 lfsck = lfsck_instance_find(key, true, false);
2829 if (likely(lfsck != NULL)) {
2830 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2831 CWARN("%s: invalid async windows size that may "
2832 "cause memory issues. The valid range is "
2834 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2836 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2837 mutex_lock(&lfsck->li_mutex);
2838 lfsck->li_bookmark_ram.lb_async_windows = val;
2839 rc = lfsck_bookmark_store(&env, lfsck);
2840 mutex_unlock(&lfsck->li_mutex);
2842 lfsck_instance_put(&env, lfsck);
2851 EXPORT_SYMBOL(lfsck_set_windows);
2853 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2856 struct lfsck_instance *lfsck;
2857 struct lfsck_component *com;
2861 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2865 lfsck = lfsck_instance_find(key, true, false);
2866 if (likely(lfsck != NULL)) {
2867 com = lfsck_component_find(lfsck, type);
2868 if (likely(com != NULL)) {
2869 com->lc_ops->lfsck_dump(&env, com, m);
2870 lfsck_component_put(&env, com);
2875 lfsck_instance_put(&env, lfsck);
2884 EXPORT_SYMBOL(lfsck_dump);
2886 static int lfsck_stop_all(const struct lu_env *env,
2887 struct lfsck_instance *lfsck,
2888 struct lfsck_stop *stop)
2890 struct lfsck_thread_info *info = lfsck_env_info(env);
2891 struct lfsck_request *lr = &info->lti_lr;
2892 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2893 struct ptlrpc_request_set *set;
2894 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2895 struct lfsck_tgt_desc *ltd;
2896 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2902 LASSERT(stop->ls_flags & LPF_BROADCAST);
2904 set = ptlrpc_prep_set();
2905 if (unlikely(set == NULL))
2908 memset(lr, 0, sizeof(*lr));
2909 lr->lr_event = LE_STOP;
2910 lr->lr_index = lfsck_dev_idx(lfsck);
2911 lr->lr_status = stop->ls_status;
2912 lr->lr_version = bk->lb_version;
2913 lr->lr_active = LFSCK_TYPES_ALL;
2914 lr->lr_param = stop->ls_flags;
2916 memset(laia, 0, sizeof(*laia));
2917 laia->laia_ltds = ltds;
2919 laia->laia_shared = 1;
2921 down_read(<ds->ltd_rw_sem);
2922 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2923 ltd = lfsck_tgt_get(ltds, idx);
2924 LASSERT(ltd != NULL);
2926 laia->laia_ltd = ltd;
2927 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2928 lfsck_async_interpret, laia,
2931 lfsck_interpret(env, lfsck, NULL, laia, rc);
2933 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2934 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2938 up_read(<ds->ltd_rw_sem);
2940 rc = ptlrpc_set_wait(set);
2941 ptlrpc_set_destroy(set);
2944 rc = laia->laia_result;
2946 if (rc == -EALREADY)
2950 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2951 lfsck_lfsck2name(lfsck), rc);
2953 RETURN(rc != 0 ? rc : rc1);
2956 static int lfsck_start_all(const struct lu_env *env,
2957 struct lfsck_instance *lfsck,
2958 struct lfsck_start *start)
2960 struct lfsck_thread_info *info = lfsck_env_info(env);
2961 struct lfsck_request *lr = &info->lti_lr;
2962 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2963 struct ptlrpc_request_set *set;
2964 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2965 struct lfsck_tgt_desc *ltd;
2966 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2972 LASSERT(start->ls_flags & LPF_BROADCAST);
2974 memset(lr, 0, sizeof(*lr));
2975 lr->lr_event = LE_START;
2976 lr->lr_index = lfsck_dev_idx(lfsck);
2977 lr->lr_speed = bk->lb_speed_limit;
2978 lr->lr_version = bk->lb_version;
2979 lr->lr_active = start->ls_active;
2980 lr->lr_param = start->ls_flags;
2981 lr->lr_async_windows = bk->lb_async_windows;
2982 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2983 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2986 memset(laia, 0, sizeof(*laia));
2987 laia->laia_ltds = ltds;
2989 laia->laia_shared = 1;
2992 set = ptlrpc_prep_set();
2996 down_read(<ds->ltd_rw_sem);
2997 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2998 ltd = lfsck_tgt_get(ltds, idx);
2999 LASSERT(ltd != NULL);
3001 if (retry && !ltd->ltd_retry_start) {
3006 laia->laia_ltd = ltd;
3007 ltd->ltd_retry_start = 0;
3008 ltd->ltd_layout_done = 0;
3009 ltd->ltd_namespace_done = 0;
3010 ltd->ltd_synced_failures = 0;
3011 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
3012 lfsck_async_interpret, laia,
3015 lfsck_interpret(env, lfsck, NULL, laia, rc);
3017 CERROR("%s: cannot notify MDT %x for LFSCK "
3018 "start, failout: rc = %d\n",
3019 lfsck_lfsck2name(lfsck), idx, rc);
3023 up_read(<ds->ltd_rw_sem);
3026 ptlrpc_set_destroy(set);
3031 rc = ptlrpc_set_wait(set);
3032 ptlrpc_set_destroy(set);
3035 rc = laia->laia_result;
3037 if (unlikely(rc == -EINPROGRESS)) {
3039 set_current_state(TASK_INTERRUPTIBLE);
3040 schedule_timeout(msecs_to_jiffies(MSEC_PER_SEC));
3041 set_current_state(TASK_RUNNING);
3042 if (!signal_pending(current) &&
3043 thread_is_running(&lfsck->li_thread))
3050 struct lfsck_stop *stop = &info->lti_stop;
3052 CERROR("%s: cannot start LFSCK on some MDTs, "
3053 "stop all: rc = %d\n",
3054 lfsck_lfsck2name(lfsck), rc);
3055 if (rc != -EALREADY) {
3056 stop->ls_status = LS_FAILED;
3057 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
3058 lfsck_stop_all(env, lfsck, stop);
3065 int lfsck_start(const struct lu_env *env, struct dt_device *key,
3066 struct lfsck_start_param *lsp)
3068 struct lfsck_start *start = lsp->lsp_start;
3069 struct lfsck_instance *lfsck;
3070 struct lfsck_bookmark *bk;
3071 struct ptlrpc_thread *thread;
3072 struct lfsck_component *com;
3073 struct l_wait_info lwi = { 0 };
3074 struct lfsck_thread_args *lta;
3075 struct task_struct *task;
3076 struct lfsck_tgt_descs *ltds;
3077 struct lfsck_tgt_desc *ltd;
3088 lfsck = lfsck_instance_find(key, true, false);
3089 if (unlikely(lfsck == NULL))
3092 if (unlikely(lfsck->li_stopping))
3093 GOTO(put, rc = -ENXIO);
3095 /* System is not ready, try again later. */
3096 if (unlikely(lfsck->li_namespace == NULL ||
3097 lfsck_dev_site(lfsck)->ss_server_fld == NULL))
3098 GOTO(put, rc = -EINPROGRESS);
3100 /* start == NULL means auto trigger paused LFSCK. */
3102 if (list_empty(&lfsck->li_list_scan) ||
3103 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO))
3105 } else if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3106 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3107 lfsck_lfsck2name(lfsck));
3109 GOTO(put, rc = -EPERM);
3112 bk = &lfsck->li_bookmark_ram;
3113 thread = &lfsck->li_thread;
3114 mutex_lock(&lfsck->li_mutex);
3115 spin_lock(&lfsck->li_lock);
3116 if (unlikely(thread_is_stopping(thread))) {
3117 /* Someone is stopping the LFSCK. */
3118 spin_unlock(&lfsck->li_lock);
3119 GOTO(out, rc = -EBUSY);
3122 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
3124 if (unlikely(start == NULL)) {
3125 spin_unlock(&lfsck->li_lock);
3129 while (start->ls_active != 0) {
3130 if (!(type & start->ls_active)) {
3135 com = __lfsck_component_find(lfsck, type,
3136 &lfsck->li_list_scan);
3138 com = __lfsck_component_find(lfsck, type,
3139 &lfsck->li_list_double_scan);
3145 if (com->lc_ops->lfsck_join != NULL) {
3146 rc = com->lc_ops->lfsck_join( env, com, lsp);
3147 if (rc != 0 && rc != -EALREADY)
3150 start->ls_active &= ~type;
3153 spin_unlock(&lfsck->li_lock);
3156 spin_unlock(&lfsck->li_lock);
3158 lfsck->li_status = 0;
3159 lfsck->li_oit_over = 0;
3160 lfsck->li_start_unplug = 0;
3161 lfsck->li_drop_dryrun = 0;
3162 lfsck->li_new_scanned = 0;
3164 /* For auto trigger. */
3168 start->ls_version = bk->lb_version;
3170 if (start->ls_active != 0) {
3171 struct lfsck_component *next;
3173 if (start->ls_active == LFSCK_TYPES_ALL)
3174 start->ls_active = LFSCK_TYPES_SUPPORTED;
3176 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3177 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3178 GOTO(out, rc = -ENOTSUPP);
3181 list_for_each_entry_safe(com, next,
3182 &lfsck->li_list_scan, lc_link) {
3183 if (!(com->lc_type & start->ls_active)) {
3184 rc = com->lc_ops->lfsck_post(env, com, 0,
3191 while (start->ls_active != 0) {
3192 if (type & start->ls_active) {
3193 com = __lfsck_component_find(lfsck, type,
3194 &lfsck->li_list_idle);
3196 /* The component status will be updated
3197 * when its prep() is called later by
3198 * the LFSCK main engine. */
3199 list_move_tail(&com->lc_link,
3200 &lfsck->li_list_scan);
3201 start->ls_active &= ~type;
3207 if (list_empty(&lfsck->li_list_scan)) {
3208 /* The speed limit will be used to control both the LFSCK and
3209 * low layer scrub (if applied), need to be handled firstly. */
3210 if (start->ls_valid & LSV_SPEED_LIMIT) {
3211 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3212 rc = lfsck_bookmark_store(env, lfsck);
3221 if (start->ls_flags & LPF_RESET)
3222 flags |= DOIF_RESET;
3224 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3228 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3229 start->ls_active |= com->lc_type;
3230 if (flags & DOIF_RESET) {
3231 rc = com->lc_ops->lfsck_reset(env, com, false);
3237 ltds = &lfsck->li_mdt_descs;
3238 down_read(<ds->ltd_rw_sem);
3239 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3240 ltd = lfsck_ltd2tgt(ltds, idx);
3241 LASSERT(ltd != NULL);
3243 ltd->ltd_layout_done = 0;
3244 ltd->ltd_namespace_done = 0;
3245 ltd->ltd_synced_failures = 0;
3246 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3247 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3248 list_del_init(<d->ltd_layout_phase_list);
3249 list_del_init(<d->ltd_layout_list);
3250 list_del_init(<d->ltd_namespace_phase_list);
3251 list_del_init(<d->ltd_namespace_list);
3253 up_read(<ds->ltd_rw_sem);
3255 ltds = &lfsck->li_ost_descs;
3256 down_read(<ds->ltd_rw_sem);
3257 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3258 ltd = lfsck_ltd2tgt(ltds, idx);
3259 LASSERT(ltd != NULL);
3261 ltd->ltd_layout_done = 0;
3262 ltd->ltd_synced_failures = 0;
3263 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3264 list_del_init(<d->ltd_layout_phase_list);
3265 list_del_init(<d->ltd_layout_list);
3267 up_read(<ds->ltd_rw_sem);
3270 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3271 if (bk->lb_param & LPF_DRYRUN)
3272 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3274 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3275 valid |= DOIV_ERROR_HANDLE;
3276 if (start->ls_flags & LPF_FAILOUT)
3277 flags |= DOIF_FAILOUT;
3280 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3281 valid |= DOIV_DRYRUN;
3282 if (start->ls_flags & LPF_DRYRUN)
3283 flags |= DOIF_DRYRUN;
3286 if (!list_empty(&lfsck->li_list_scan))
3287 flags |= DOIF_OUTUSED;
3289 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3290 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3292 GOTO(out, rc = PTR_ERR(lta));
3294 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3295 spin_lock(&lfsck->li_lock);
3296 thread_set_flags(thread, SVC_STARTING);
3297 spin_unlock(&lfsck->li_lock);
3298 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3301 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3302 lfsck_lfsck2name(lfsck), rc);
3303 lfsck_thread_args_fini(lta);
3308 l_wait_event(thread->t_ctl_waitq,
3309 thread_is_running(thread) ||
3310 thread_is_stopped(thread),
3312 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3313 lfsck->li_start_unplug = 1;
3314 wake_up_all(&thread->t_ctl_waitq);
3319 /* release lfsck::li_mutex to avoid deadlock. */
3320 mutex_unlock(&lfsck->li_mutex);
3321 rc = lfsck_start_all(env, lfsck, start);
3323 spin_lock(&lfsck->li_lock);
3324 if (thread_is_stopped(thread)) {
3325 spin_unlock(&lfsck->li_lock);
3327 lfsck->li_status = LS_FAILED;
3328 lfsck->li_flags = 0;
3329 thread_set_flags(thread, SVC_STOPPING);
3330 spin_unlock(&lfsck->li_lock);
3332 lfsck->li_start_unplug = 1;
3333 wake_up_all(&thread->t_ctl_waitq);
3334 l_wait_event(thread->t_ctl_waitq,
3335 thread_is_stopped(thread),
3339 lfsck->li_start_unplug = 1;
3340 wake_up_all(&thread->t_ctl_waitq);
3346 mutex_unlock(&lfsck->li_mutex);
3349 lfsck_instance_put(env, lfsck);
3351 return rc < 0 ? rc : 0;
3353 EXPORT_SYMBOL(lfsck_start);
3355 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3356 struct lfsck_stop *stop)
3358 struct lfsck_instance *lfsck;
3359 struct ptlrpc_thread *thread;
3360 struct l_wait_info lwi = { 0 };
3365 lfsck = lfsck_instance_find(key, true, false);
3366 if (unlikely(lfsck == NULL))
3369 thread = &lfsck->li_thread;
3370 if (stop && stop->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3371 CERROR("%s: only allow to specify '-A' via MDS\n",
3372 lfsck_lfsck2name(lfsck));
3373 GOTO(put, rc = -EPERM);
3376 spin_lock(&lfsck->li_lock);
3377 /* The target is umounted */
3378 if (stop && stop->ls_status == LS_PAUSED)
3379 lfsck->li_stopping = 1;
3381 if (thread_is_init(thread) || thread_is_stopped(thread))
3382 /* no error if LFSCK stopped already, or not started */
3383 GOTO(unlock, rc = 0);
3385 if (thread_is_stopping(thread))
3386 /* Someone is stopping LFSCK. */
3387 GOTO(unlock, rc = -EINPROGRESS);
3390 lfsck->li_status = stop->ls_status;
3391 lfsck->li_flags = stop->ls_flags;
3393 lfsck->li_status = LS_STOPPED;
3394 lfsck->li_flags = 0;
3397 thread_set_flags(thread, SVC_STOPPING);
3399 if (lfsck->li_master) {
3400 struct lfsck_component *com;
3401 struct lfsck_assistant_data *lad;
3403 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3405 spin_lock(&lad->lad_lock);
3406 if (lad->lad_task != NULL)
3407 force_sig(SIGINT, lad->lad_task);
3408 spin_unlock(&lad->lad_lock);
3411 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3413 spin_lock(&lad->lad_lock);
3414 if (lad->lad_task != NULL)
3415 force_sig(SIGINT, lad->lad_task);
3416 spin_unlock(&lad->lad_lock);
3420 wake_up_all(&thread->t_ctl_waitq);
3421 spin_unlock(&lfsck->li_lock);
3422 if (stop && stop->ls_flags & LPF_BROADCAST)
3423 rc1 = lfsck_stop_all(env, lfsck, stop);
3425 /* It was me set the status as 'stopping' just now, if it is not
3426 * 'stopping' now, then either stopped, or re-started by race. */
3427 l_wait_event(thread->t_ctl_waitq,
3428 !thread_is_stopping(thread),
3434 spin_unlock(&lfsck->li_lock);
3436 lfsck_instance_put(env, lfsck);
3438 return rc != 0 ? rc : rc1;
3440 EXPORT_SYMBOL(lfsck_stop);
3442 int lfsck_in_notify_local(const struct lu_env *env, struct dt_device *key,
3443 struct lfsck_req_local *lrl, struct thandle *th)
3445 struct lfsck_instance *lfsck;
3446 struct lfsck_component *com;
3447 int rc = -EOPNOTSUPP;
3450 lfsck = lfsck_instance_find(key, true, false);
3451 if (unlikely(!lfsck))
3454 com = lfsck_component_find(lfsck, lrl->lrl_active);
3455 if (likely(com && com->lc_ops->lfsck_in_notify_local)) {
3456 rc = com->lc_ops->lfsck_in_notify_local(env, com, lrl, th);
3457 lfsck_component_put(env, com);
3460 lfsck_instance_put(env, lfsck);
3464 EXPORT_SYMBOL(lfsck_in_notify_local);
3466 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3467 struct lfsck_request *lr)
3469 int rc = -EOPNOTSUPP;
3472 switch (lr->lr_event) {
3474 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3475 struct lfsck_start_param lsp;
3477 memset(start, 0, sizeof(*start));
3478 start->ls_valid = lr->lr_valid;
3479 start->ls_speed_limit = lr->lr_speed;
3480 start->ls_version = lr->lr_version;
3481 start->ls_active = lr->lr_active;
3482 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3483 start->ls_async_windows = lr->lr_async_windows;
3485 lsp.lsp_start = start;
3486 lsp.lsp_index = lr->lr_index;
3487 lsp.lsp_index_valid = 1;
3488 rc = lfsck_start(env, key, &lsp);
3492 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3494 memset(stop, 0, sizeof(*stop));
3495 stop->ls_status = lr->lr_status;
3496 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3497 rc = lfsck_stop(env, key, stop);
3500 case LE_PHASE1_DONE:
3501 case LE_PHASE2_DONE:
3503 case LE_CONDITIONAL_DESTROY:
3504 case LE_SET_LMV_MASTER:
3505 case LE_SET_LMV_SLAVE:
3506 case LE_PAIRS_VERIFY: {
3507 struct lfsck_instance *lfsck;
3508 struct lfsck_component *com;
3510 lfsck = lfsck_instance_find(key, true, false);
3511 if (unlikely(lfsck == NULL))
3514 com = lfsck_component_find(lfsck, lr->lr_active);
3516 rc = com->lc_ops->lfsck_in_notify(env, com, lr);
3517 lfsck_component_put(env, com);
3520 lfsck_instance_put(env, lfsck);
3529 EXPORT_SYMBOL(lfsck_in_notify);
3531 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3532 struct lfsck_request *req, struct lfsck_reply *rep,
3533 struct lfsck_query *que)
3535 struct lfsck_instance *lfsck;
3536 struct lfsck_component *com;
3542 lfsck = lfsck_instance_find(key, true, false);
3543 if (unlikely(lfsck == NULL))
3547 if (que->lu_types == LFSCK_TYPES_ALL)
3549 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3551 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3552 que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3554 GOTO(out, rc = -ENOTSUPP);
3557 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3558 i++, type = 1 << i) {
3559 if (!(que->lu_types & type))
3563 com = lfsck_component_find(lfsck, type);
3564 if (unlikely(com == NULL))
3565 GOTO(out, rc = -ENOTSUPP);
3567 memset(que->lu_mdts_count[i], 0,
3568 sizeof(__u32) * (LS_MAX + 1));
3569 memset(que->lu_osts_count[i], 0,
3570 sizeof(__u32) * (LS_MAX + 1));
3571 que->lu_repaired[i] = 0;
3572 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3574 lfsck_component_put(env, com);
3579 if (!(que->lu_flags & LPF_WAIT))
3582 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3583 i++, type = 1 << i) {
3584 if (!(que->lu_types & type))
3587 if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3588 que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3589 que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3590 que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3591 struct l_wait_info lwi;
3593 /* If it is required to wait, then sleep
3594 * 3 seconds and try to query again. */
3595 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3599 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3601 if (rc == -ETIMEDOUT)
3606 com = lfsck_component_find(lfsck, req->lr_active);
3607 if (likely(com != NULL)) {
3608 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3610 lfsck_component_put(env, com);
3619 lfsck_instance_put(env, lfsck);
3622 EXPORT_SYMBOL(lfsck_query);
3624 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3625 struct ldlm_namespace *ns)
3627 struct lfsck_instance *lfsck;
3630 lfsck = lfsck_instance_find(key, true, false);
3631 if (likely(lfsck != NULL)) {
3632 lfsck->li_namespace = ns;
3633 lfsck_instance_put(env, lfsck);
3639 EXPORT_SYMBOL(lfsck_register_namespace);
3641 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3642 struct dt_device *next, struct obd_device *obd,
3643 lfsck_out_notify notify, void *notify_data, bool master)
3645 struct lfsck_instance *lfsck;
3646 struct dt_object *root = NULL;
3647 struct dt_object *obj = NULL;
3648 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3652 lfsck = lfsck_instance_find(key, false, false);
3653 if (unlikely(lfsck != NULL))
3656 OBD_ALLOC_PTR(lfsck);
3660 mutex_init(&lfsck->li_mutex);
3661 spin_lock_init(&lfsck->li_lock);
3662 INIT_LIST_HEAD(&lfsck->li_link);
3663 INIT_LIST_HEAD(&lfsck->li_list_scan);
3664 INIT_LIST_HEAD(&lfsck->li_list_dir);
3665 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3666 INIT_LIST_HEAD(&lfsck->li_list_idle);
3667 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3668 atomic_set(&lfsck->li_ref, 1);
3669 atomic_set(&lfsck->li_double_scan_count, 0);
3670 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3671 lfsck->li_out_notify = notify;
3672 lfsck->li_out_notify_data = notify_data;
3673 lfsck->li_next = next;
3674 lfsck->li_bottom = key;
3675 lfsck->li_obd = obd;
3677 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3681 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3685 fid->f_seq = FID_SEQ_LOCAL_NAME;
3688 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3692 rc = dt_root_get(env, key, fid);
3696 root = dt_locate(env, key, fid);
3698 GOTO(out, rc = PTR_ERR(root));
3700 if (unlikely(!dt_try_as_dir(env, root)))
3701 GOTO(out, rc = -ENOTDIR);
3703 lfsck->li_local_root_fid = *fid;
3705 lfsck->li_master = 1;
3706 if (lfsck_dev_idx(lfsck) == 0) {
3707 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3708 const struct lu_name *cname;
3710 rc = dt_lookup(env, root,
3711 (struct dt_rec *)(&lfsck->li_global_root_fid),
3712 (const struct dt_key *)"ROOT");
3716 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3718 GOTO(out, rc = PTR_ERR(obj));
3720 if (unlikely(!dt_try_as_dir(env, obj)))
3721 GOTO(out, rc = -ENOTDIR);
3723 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3724 (const struct dt_key *)dotlustre);
3728 lfsck_object_put(env, obj);
3729 obj = dt_locate(env, key, fid);
3731 GOTO(out, rc = PTR_ERR(obj));
3733 cname = lfsck_name_get_const(env, dotlustre,
3735 rc = lfsck_verify_linkea(env, obj, cname,
3736 &lfsck->li_global_root_fid);
3740 if (unlikely(!dt_try_as_dir(env, obj)))
3741 GOTO(out, rc = -ENOTDIR);
3744 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3745 (const struct dt_key *)lostfound);
3749 lfsck_object_put(env, obj);
3750 obj = dt_locate(env, key, fid);
3752 GOTO(out, rc = PTR_ERR(obj));
3754 cname = lfsck_name_get_const(env, lostfound,
3756 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3760 lfsck_object_put(env, obj);
3765 fid->f_seq = FID_SEQ_LOCAL_FILE;
3766 fid->f_oid = OTABLE_IT_OID;
3768 obj = dt_locate(env, key, fid);
3770 GOTO(out, rc = PTR_ERR(obj));
3772 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3776 lfsck->li_obj_oit = obj;
3777 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3778 S_IFDIR | S_IRUGO | S_IWUSR);
3780 GOTO(out, rc = PTR_ERR(obj));
3782 lu_object_get(&obj->do_lu);
3783 lfsck->li_lfsck_dir = obj;
3784 rc = lfsck_bookmark_setup(env, lfsck);
3789 rc = lfsck_fid_init(lfsck);
3793 rc = lfsck_namespace_setup(env, lfsck);
3798 rc = lfsck_layout_setup(env, lfsck);
3802 /* XXX: more LFSCK components initialization to be added here. */
3804 rc = lfsck_instance_add(lfsck);
3806 rc = lfsck_add_target_from_orphan(env, lfsck);
3808 if (obj != NULL && !IS_ERR(obj))
3809 lfsck_object_put(env, obj);
3810 if (root != NULL && !IS_ERR(root))
3811 lfsck_object_put(env, root);
3813 lfsck_instance_cleanup(env, lfsck);
3816 EXPORT_SYMBOL(lfsck_register);
3818 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3820 struct lfsck_instance *lfsck;
3822 lfsck = lfsck_instance_find(key, false, true);
3824 lfsck_instance_put(env, lfsck);
3826 EXPORT_SYMBOL(lfsck_degister);
3828 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3829 struct dt_device *tgt, struct obd_export *exp,
3830 __u32 index, bool for_ost)
3832 struct lfsck_instance *lfsck;
3833 struct lfsck_tgt_desc *ltd;
3844 INIT_LIST_HEAD(<d->ltd_orphan_list);
3845 INIT_LIST_HEAD(<d->ltd_layout_list);
3846 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3847 INIT_LIST_HEAD(<d->ltd_namespace_list);
3848 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3849 atomic_set(<d->ltd_ref, 1);
3850 ltd->ltd_index = index;
3852 spin_lock(&lfsck_instance_lock);
3853 lfsck = __lfsck_instance_find(key, true, false);
3854 if (lfsck == NULL) {
3856 list_add_tail(<d->ltd_orphan_list,
3857 &lfsck_ost_orphan_list);
3859 list_add_tail(<d->ltd_orphan_list,
3860 &lfsck_mdt_orphan_list);
3861 spin_unlock(&lfsck_instance_lock);
3865 spin_unlock(&lfsck_instance_lock);
3867 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3871 lfsck_instance_put(env, lfsck);
3875 EXPORT_SYMBOL(lfsck_add_target);
3877 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3878 struct dt_device *tgt, __u32 index, bool for_ost)
3880 struct lfsck_instance *lfsck;
3881 struct lfsck_tgt_descs *ltds;
3882 struct lfsck_tgt_desc *ltd;
3883 struct list_head *head;
3886 head = &lfsck_ost_orphan_list;
3888 head = &lfsck_mdt_orphan_list;
3890 spin_lock(&lfsck_instance_lock);
3891 list_for_each_entry(ltd, head, ltd_orphan_list) {
3892 if (ltd->ltd_tgt == tgt) {
3893 list_del_init(<d->ltd_orphan_list);
3894 spin_unlock(&lfsck_instance_lock);
3902 lfsck = __lfsck_instance_find(key, true, false);
3903 spin_unlock(&lfsck_instance_lock);
3904 if (unlikely(lfsck == NULL))
3908 ltds = &lfsck->li_ost_descs;
3910 ltds = &lfsck->li_mdt_descs;
3912 down_write(<ds->ltd_rw_sem);
3913 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3915 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3918 ltd = lfsck_ltd2tgt(ltds, index);
3919 if (unlikely(ltd == NULL))
3922 LASSERT(ltds->ltd_tgtnr > 0);
3925 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3926 lfsck_assign_tgt(ltds, NULL, index);
3931 head = &lfsck->li_ost_descs.ltd_orphan;
3933 head = &lfsck->li_mdt_descs.ltd_orphan;
3935 list_for_each_entry(ltd, head, ltd_orphan_list) {
3936 if (ltd->ltd_tgt == tgt) {
3937 list_del_init(<d->ltd_orphan_list);
3943 up_write(<ds->ltd_rw_sem);
3945 spin_lock(<ds->ltd_lock);
3947 spin_unlock(<ds->ltd_lock);
3948 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3949 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3953 lfsck_instance_put(env, lfsck);
3955 EXPORT_SYMBOL(lfsck_del_target);
3957 static int __init lfsck_init(void)
3961 INIT_LIST_HEAD(&lfsck_instance_list);
3962 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3963 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3964 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3965 rc = lu_context_key_register(&lfsck_thread_key);
3967 tgt_register_lfsck_in_notify_local(lfsck_in_notify_local);
3968 tgt_register_lfsck_in_notify(lfsck_in_notify);
3969 tgt_register_lfsck_query(lfsck_query);
3975 static void __exit lfsck_exit(void)
3977 struct lfsck_tgt_desc *ltd;
3978 struct lfsck_tgt_desc *next;
3980 LASSERT(list_empty(&lfsck_instance_list));
3982 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3984 list_del_init(<d->ltd_orphan_list);
3988 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3990 list_del_init(<d->ltd_orphan_list);
3994 lu_context_key_degister(&lfsck_thread_key);
3997 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3998 MODULE_DESCRIPTION("Lustre File System Checker");
3999 MODULE_VERSION(LUSTRE_VERSION_STRING);
4000 MODULE_LICENSE("GPL");
4002 module_init(lfsck_init);
4003 module_exit(lfsck_exit);