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, 2014, Intel Corporation.
26 * lustre/lfsck/lfsck_lib.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <libcfs/list.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fld.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre_lfsck.h>
41 #include <lustre/lustre_lfsck_user.h>
43 #include "lfsck_internal.h"
45 #define LFSCK_CHECKPOINT_SKIP 1
47 /* define lfsck thread key */
48 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
50 static void lfsck_key_fini(const struct lu_context *ctx,
51 struct lu_context_key *key, void *data)
53 struct lfsck_thread_info *info = data;
55 lu_buf_free(&info->lti_linkea_buf);
56 lu_buf_free(&info->lti_linkea_buf2);
57 lu_buf_free(&info->lti_big_buf);
61 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
62 LU_KEY_INIT_GENERIC(lfsck);
64 static struct list_head lfsck_instance_list;
65 static struct list_head lfsck_ost_orphan_list;
66 static struct list_head lfsck_mdt_orphan_list;
67 static DEFINE_SPINLOCK(lfsck_instance_lock);
69 static const char *lfsck_status_names[] = {
71 [LS_SCANNING_PHASE1] = "scanning-phase1",
72 [LS_SCANNING_PHASE2] = "scanning-phase2",
73 [LS_COMPLETED] = "completed",
74 [LS_FAILED] = "failed",
75 [LS_STOPPED] = "stopped",
76 [LS_PAUSED] = "paused",
77 [LS_CRASHED] = "crashed",
78 [LS_PARTIAL] = "partial",
79 [LS_CO_FAILED] = "co-failed",
80 [LS_CO_STOPPED] = "co-stopped",
81 [LS_CO_PAUSED] = "co-paused"
84 const char *lfsck_flags_names[] = {
93 const char *lfsck_param_names[] = {
105 enum lfsck_verify_lpf_types {
106 LVLT_BY_BOOKMARK = 0,
107 LVLT_BY_NAMEENTRY = 1,
110 const char *lfsck_status2names(enum lfsck_status status)
112 if (unlikely(status < 0 || status >= LS_MAX))
115 return lfsck_status_names[status];
118 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
120 spin_lock_init(<ds->ltd_lock);
121 init_rwsem(<ds->ltd_rw_sem);
122 INIT_LIST_HEAD(<ds->ltd_orphan);
123 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
124 if (ltds->ltd_tgts_bitmap == NULL)
130 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
132 struct lfsck_tgt_desc *ltd;
133 struct lfsck_tgt_desc *next;
136 down_write(<ds->ltd_rw_sem);
138 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
140 list_del_init(<d->ltd_orphan_list);
144 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
145 up_write(<ds->ltd_rw_sem);
150 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
151 ltd = LTD_TGT(ltds, idx);
152 if (likely(ltd != NULL)) {
153 LASSERT(list_empty(<d->ltd_layout_list));
154 LASSERT(list_empty(<d->ltd_layout_phase_list));
155 LASSERT(list_empty(<d->ltd_namespace_list));
156 LASSERT(list_empty(<d->ltd_namespace_phase_list));
159 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
160 LTD_TGT(ltds, idx) = NULL;
165 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
168 for (idx = 0; idx < TGT_PTRS; idx++) {
169 if (ltds->ltd_tgts_idx[idx] != NULL) {
170 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
171 ltds->ltd_tgts_idx[idx] = NULL;
175 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
176 ltds->ltd_tgts_bitmap = NULL;
177 up_write(<ds->ltd_rw_sem);
180 static int __lfsck_add_target(const struct lu_env *env,
181 struct lfsck_instance *lfsck,
182 struct lfsck_tgt_desc *ltd,
183 bool for_ost, bool locked)
185 struct lfsck_tgt_descs *ltds;
186 __u32 index = ltd->ltd_index;
191 ltds = &lfsck->li_ost_descs;
193 ltds = &lfsck->li_mdt_descs;
196 down_write(<ds->ltd_rw_sem);
198 LASSERT(ltds->ltd_tgts_bitmap != NULL);
200 if (index >= ltds->ltd_tgts_bitmap->size) {
201 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
202 (__u32)BITS_PER_LONG);
203 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
204 cfs_bitmap_t *new_bitmap;
206 while (newsize < index + 1)
209 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
210 if (new_bitmap == NULL)
211 GOTO(unlock, rc = -ENOMEM);
213 if (ltds->ltd_tgtnr > 0)
214 cfs_bitmap_copy(new_bitmap, old_bitmap);
215 ltds->ltd_tgts_bitmap = new_bitmap;
216 CFS_FREE_BITMAP(old_bitmap);
219 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
220 CERROR("%s: the device %s (%u) is registered already\n",
221 lfsck_lfsck2name(lfsck),
222 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
223 GOTO(unlock, rc = -EEXIST);
226 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
227 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
228 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
229 GOTO(unlock, rc = -ENOMEM);
232 LTD_TGT(ltds, index) = ltd;
233 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
236 GOTO(unlock, rc = 0);
240 up_write(<ds->ltd_rw_sem);
245 static int lfsck_add_target_from_orphan(const struct lu_env *env,
246 struct lfsck_instance *lfsck)
248 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
249 struct lfsck_tgt_desc *ltd;
250 struct lfsck_tgt_desc *next;
251 struct list_head *head = &lfsck_ost_orphan_list;
256 spin_lock(&lfsck_instance_lock);
257 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
258 if (ltd->ltd_key == lfsck->li_bottom)
259 list_move_tail(<d->ltd_orphan_list,
262 spin_unlock(&lfsck_instance_lock);
264 down_write(<ds->ltd_rw_sem);
265 while (!list_empty(<ds->ltd_orphan)) {
266 ltd = list_entry(ltds->ltd_orphan.next,
267 struct lfsck_tgt_desc,
269 list_del_init(<d->ltd_orphan_list);
270 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
271 /* Do not hold the semaphore for too long time. */
272 up_write(<ds->ltd_rw_sem);
276 down_write(<ds->ltd_rw_sem);
278 up_write(<ds->ltd_rw_sem);
281 ltds = &lfsck->li_mdt_descs;
282 head = &lfsck_mdt_orphan_list;
290 static inline struct lfsck_component *
291 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
292 struct list_head *list)
294 struct lfsck_component *com;
296 list_for_each_entry(com, list, lc_link) {
297 if (com->lc_type == type)
303 struct lfsck_component *
304 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
306 struct lfsck_component *com;
308 spin_lock(&lfsck->li_lock);
309 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
313 com = __lfsck_component_find(lfsck, type,
314 &lfsck->li_list_double_scan);
318 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
322 lfsck_component_get(com);
323 spin_unlock(&lfsck->li_lock);
327 void lfsck_component_cleanup(const struct lu_env *env,
328 struct lfsck_component *com)
330 if (!list_empty(&com->lc_link))
331 list_del_init(&com->lc_link);
332 if (!list_empty(&com->lc_link_dir))
333 list_del_init(&com->lc_link_dir);
335 lfsck_component_put(env, com);
338 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
339 struct lu_fid *fid, bool locked)
341 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
346 mutex_lock(&lfsck->li_mutex);
348 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
350 bk->lb_last_fid = *fid;
351 /* We do not care about whether the subsequent sub-operations
352 * failed or not. The worst case is that one FID is lost that
353 * is not a big issue for the LFSCK since it is relative rare
354 * for LFSCK create. */
355 rc = lfsck_bookmark_store(env, lfsck);
359 mutex_unlock(&lfsck->li_mutex);
364 static int __lfsck_ibits_lock(const struct lu_env *env,
365 struct lfsck_instance *lfsck,
366 struct dt_object *obj,
367 struct ldlm_res_id *resid,
368 struct lustre_handle *lh,
369 __u64 bits, ldlm_mode_t mode)
371 struct lfsck_thread_info *info = lfsck_env_info(env);
372 ldlm_policy_data_t *policy = &info->lti_policy;
373 __u64 flags = LDLM_FL_ATOMIC_CB;
376 LASSERT(lfsck->li_namespace != NULL);
378 memset(policy, 0, sizeof(*policy));
379 policy->l_inodebits.bits = bits;
380 if (dt_object_remote(obj)) {
381 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
383 memset(einfo, 0, sizeof(*einfo));
384 einfo->ei_type = LDLM_IBITS;
385 einfo->ei_mode = mode;
386 einfo->ei_cb_bl = ldlm_blocking_ast;
387 einfo->ei_cb_cp = ldlm_completion_ast;
388 einfo->ei_res_id = resid;
390 rc = dt_object_lock(env, obj, lh, einfo, policy);
392 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
393 LDLM_IBITS, policy, mode,
394 &flags, ldlm_blocking_ast,
395 ldlm_completion_ast, NULL, NULL,
396 0, LVB_T_NONE, NULL, lh);
399 if (rc == ELDLM_OK) {
402 memset(lh, 0, sizeof(*lh));
410 * Request the specified ibits lock for the given object.
412 * Before the LFSCK modifying on the namespace visible object,
413 * it needs to acquire related ibits ldlm lock.
415 * \param[in] env pointer to the thread context
416 * \param[in] lfsck pointer to the lfsck instance
417 * \param[in] obj pointer to the dt_object to be locked
418 * \param[out] lh pointer to the lock handle
419 * \param[in] bits the bits for the ldlm lock to be acquired
420 * \param[in] mode the mode for the ldlm lock to be acquired
422 * \retval 0 for success
423 * \retval negative error number on failure
425 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
426 struct dt_object *obj, struct lustre_handle *lh,
427 __u64 bits, ldlm_mode_t mode)
429 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
431 LASSERT(!lustre_handle_is_used(lh));
433 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
434 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
438 * Release the the specified ibits lock.
440 * If the lock has been acquired before, release it
441 * and cleanup the handle. Otherwise, do nothing.
443 * \param[in] lh pointer to the lock handle
444 * \param[in] mode the mode for the ldlm lock to be released
446 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
448 if (lustre_handle_is_used(lh)) {
449 ldlm_lock_decref(lh, mode);
450 memset(lh, 0, sizeof(*lh));
455 * Request compound ibits locks for the given <obj, name> pairs.
457 * Before the LFSCK modifying on the namespace visible object, it needs to
458 * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
459 * the lock purpose. But the simple lfsck_ibits_lock for directory-based
460 * modificationis (such as insert name entry to the directory) may be too
461 * coarse-grained and not efficient.
463 * The lfsck_lock() will request compound ibits locks on the specified
464 * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
465 * lock on the directory object, and the regular ibits lock on the name hash.
467 * \param[in] env pointer to the thread context
468 * \param[in] lfsck pointer to the lfsck instance
469 * \param[in] obj pointer to the dt_object to be locked
470 * \param[in] name used for building the PDO lock resource
471 * \param[out] llh pointer to the lfsck_lock_handle
472 * \param[in] bits the bits for the ldlm lock to be acquired
473 * \param[in] mode the mode for the ldlm lock to be acquired
475 * \retval 0 for success
476 * \retval negative error number on failure
478 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
479 struct dt_object *obj, const char *name,
480 struct lfsck_lock_handle *llh, __u64 bits, ldlm_mode_t mode)
482 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
485 LASSERT(S_ISDIR(lfsck_object_type(obj)));
486 LASSERT(name != NULL);
487 LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
488 LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
492 llh->llh_pdo_mode = LCK_EX;
495 llh->llh_pdo_mode = LCK_CW;
498 llh->llh_pdo_mode = LCK_CR;
501 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
502 DFID"\n", lfsck_lfsck2name(lfsck), mode,
503 PFID(lfsck_dto2fid(obj)));
507 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
508 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
509 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
513 llh->llh_reg_mode = mode;
514 resid->name[LUSTRE_RES_ID_HSH_OFF] = full_name_hash(name, strlen(name));
515 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
516 bits, llh->llh_reg_mode);
518 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
524 * Release the the compound ibits locks.
526 * \param[in] llh pointer to the lfsck_lock_handle to be released
528 void lfsck_unlock(struct lfsck_lock_handle *llh)
530 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
531 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
534 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
535 struct lfsck_instance *lfsck,
536 const struct lu_fid *fid)
538 struct seq_server_site *ss = lfsck_dev_site(lfsck);
539 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
542 fld_range_set_mdt(range);
543 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
545 rc = range->lsr_index;
550 const char dot[] = ".";
551 const char dotdot[] = "..";
552 static const char dotlustre[] = ".lustre";
553 static const char lostfound[] = "lost+found";
556 * Remove the name entry from the .lustre/lost+found directory.
558 * No need to care about the object referenced by the name entry,
559 * either the name entry is invalid or redundant, or the referenced
560 * object has been processed or will be handled by others.
562 * \param[in] env pointer to the thread context
563 * \param[in] lfsck pointer to the lfsck instance
564 * \param[in] name the name for the name entry to be removed
566 * \retval 0 for success
567 * \retval negative error number on failure
569 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
570 struct lfsck_instance *lfsck,
573 struct dt_object *parent = lfsck->li_lpf_root_obj;
574 struct dt_device *dev = lfsck_obj2dev(parent);
576 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
580 rc = lfsck_lock(env, lfsck, parent, name, llh,
581 MDS_INODELOCK_UPDATE, LCK_PW);
585 th = dt_trans_create(env, dev);
587 GOTO(unlock, rc = PTR_ERR(th));
589 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
593 rc = dt_declare_ref_del(env, parent, th);
597 rc = dt_trans_start_local(env, dev, th);
601 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
606 dt_write_lock(env, parent, 0);
607 rc = dt_ref_del(env, parent, th);
608 dt_write_unlock(env, parent);
613 dt_trans_stop(env, dev, th);
618 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
619 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
624 static int lfsck_create_lpf_local(const struct lu_env *env,
625 struct lfsck_instance *lfsck,
626 struct dt_object *child,
628 struct dt_object_format *dof,
631 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
632 struct dt_object *parent = lfsck->li_lpf_root_obj;
633 struct dt_device *dev = lfsck_obj2dev(child);
634 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
635 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
636 const struct lu_fid *cfid = lfsck_dto2fid(child);
637 struct thandle *th = NULL;
638 struct linkea_data ldata = { NULL };
639 struct lu_buf linkea_buf;
640 const struct lu_name *cname;
642 int len = sizeof(struct lfsck_bookmark);
646 rc = linkea_data_new(&ldata,
647 &lfsck_env_info(env)->lti_linkea_buf2);
651 cname = lfsck_name_get_const(env, name, strlen(name));
652 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
656 th = dt_trans_create(env, dev);
660 /* 1a. create child */
661 rc = dt_declare_create(env, child, la, NULL, dof, th);
665 /* 2a. increase child nlink */
666 rc = dt_declare_ref_add(env, child, th);
670 /* 3a. insert linkEA for child */
671 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
672 ldata.ld_leh->leh_len);
673 rc = dt_declare_xattr_set(env, child, &linkea_buf,
674 XATTR_NAME_LINK, 0, th);
678 /* 4a. insert name into parent dir */
679 rec->rec_type = S_IFDIR;
681 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
682 (const struct dt_key *)name, th);
686 /* 5a. increase parent nlink */
687 rc = dt_declare_ref_add(env, parent, th);
691 /* 6a. update bookmark */
692 rc = dt_declare_record_write(env, bk_obj,
693 lfsck_buf_get(env, bk, len), 0, th);
697 rc = dt_trans_start_local(env, dev, th);
701 dt_write_lock(env, child, 0);
702 /* 1b.1. create child */
703 rc = dt_create(env, child, la, NULL, dof, th);
707 if (unlikely(!dt_try_as_dir(env, child)))
708 GOTO(unlock, rc = -ENOTDIR);
710 /* 1b.2. insert dot into child dir */
712 rc = dt_insert(env, child, (const struct dt_rec *)rec,
713 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
717 /* 1b.3. insert dotdot into child dir */
718 rec->rec_fid = &LU_LPF_FID;
719 rc = dt_insert(env, child, (const struct dt_rec *)rec,
720 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
724 /* 2b. increase child nlink */
725 rc = dt_ref_add(env, child, th);
729 /* 3b. insert linkEA for child. */
730 rc = dt_xattr_set(env, child, &linkea_buf,
731 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
732 dt_write_unlock(env, child);
736 /* 4b. insert name into parent dir */
738 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
739 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
743 dt_write_lock(env, parent, 0);
744 /* 5b. increase parent nlink */
745 rc = dt_ref_add(env, parent, th);
746 dt_write_unlock(env, parent);
750 bk->lb_lpf_fid = *cfid;
751 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
753 /* 6b. update bookmark */
754 rc = dt_record_write(env, bk_obj,
755 lfsck_buf_get(env, bk, len), &pos, th);
760 dt_write_unlock(env, child);
763 dt_trans_stop(env, dev, th);
768 static int lfsck_create_lpf_remote(const struct lu_env *env,
769 struct lfsck_instance *lfsck,
770 struct dt_object *child,
772 struct dt_object_format *dof,
775 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
776 struct dt_object *parent = lfsck->li_lpf_root_obj;
777 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
778 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
779 const struct lu_fid *cfid = lfsck_dto2fid(child);
780 struct thandle *th = NULL;
781 struct linkea_data ldata = { NULL };
782 struct lu_buf linkea_buf;
783 const struct lu_name *cname;
784 struct dt_device *dev;
786 int len = sizeof(struct lfsck_bookmark);
790 rc = linkea_data_new(&ldata,
791 &lfsck_env_info(env)->lti_linkea_buf2);
795 cname = lfsck_name_get_const(env, name, strlen(name));
796 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
800 /* Create .lustre/lost+found/MDTxxxx. */
802 /* XXX: Currently, cross-MDT create operation needs to create the child
803 * object firstly, then insert name into the parent directory. For
804 * this case, the child object resides on current MDT (local), but
805 * the parent ".lustre/lost+found" may be on remote MDT. It is not
806 * easy to contain all the sub-modifications orderly within single
809 * To avoid more inconsistency, we split the create operation into
812 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
814 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
817 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
818 * repair such inconsistency when LFSCK run next time. */
820 /* Transaction I: locally */
822 dev = lfsck_obj2dev(child);
823 th = dt_trans_create(env, dev);
827 /* 1a. create child */
828 rc = dt_declare_create(env, child, la, NULL, dof, th);
832 /* 2a. increase child nlink */
833 rc = dt_declare_ref_add(env, child, th);
837 /* 3a. insert linkEA for child */
838 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
839 ldata.ld_leh->leh_len);
840 rc = dt_declare_xattr_set(env, child, &linkea_buf,
841 XATTR_NAME_LINK, 0, th);
845 /* 4a. update bookmark */
846 rc = dt_declare_record_write(env, bk_obj,
847 lfsck_buf_get(env, bk, len), 0, th);
851 rc = dt_trans_start_local(env, dev, th);
855 dt_write_lock(env, child, 0);
856 /* 1b.1. create child */
857 rc = dt_create(env, child, la, NULL, dof, th);
861 if (unlikely(!dt_try_as_dir(env, child)))
862 GOTO(unlock, rc = -ENOTDIR);
864 /* 1b.2. insert dot into child dir */
865 rec->rec_type = S_IFDIR;
867 rc = dt_insert(env, child, (const struct dt_rec *)rec,
868 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
872 /* 1b.3. insert dotdot into child dir */
873 rec->rec_fid = &LU_LPF_FID;
874 rc = dt_insert(env, child, (const struct dt_rec *)rec,
875 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
879 /* 2b. increase child nlink */
880 rc = dt_ref_add(env, child, th);
884 /* 3b. insert linkEA for child */
885 rc = dt_xattr_set(env, child, &linkea_buf,
886 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
890 bk->lb_lpf_fid = *cfid;
891 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
893 /* 4b. update bookmark */
894 rc = dt_record_write(env, bk_obj,
895 lfsck_buf_get(env, bk, len), &pos, th);
897 dt_write_unlock(env, child);
898 dt_trans_stop(env, dev, th);
902 /* Transaction II: remotely */
904 dev = lfsck_obj2dev(parent);
905 th = dt_trans_create(env, dev);
910 /* 5a. insert name into parent dir */
912 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
913 (const struct dt_key *)name, th);
917 /* 6a. increase parent nlink */
918 rc = dt_declare_ref_add(env, parent, th);
922 rc = dt_trans_start_local(env, dev, th);
926 /* 5b. insert name into parent dir */
927 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
928 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
932 dt_write_lock(env, parent, 0);
933 /* 6b. increase parent nlink */
934 rc = dt_ref_add(env, parent, th);
935 dt_write_unlock(env, parent);
940 dt_write_unlock(env, child);
942 dt_trans_stop(env, dev, th);
944 if (rc != 0 && dev == lfsck_obj2dev(parent))
945 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
946 "for orphans, but failed to insert the name %s "
947 "to the .lustre/lost+found/. Such inconsistency "
948 "will be repaired when LFSCK run next time: rc = %d\n",
949 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
955 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
957 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
958 * orphans and other uncertain inconsistent objects found during the
959 * LFSCK. Such directory will be created by the LFSCK engine on the
960 * local MDT before the LFSCK scanning.
962 * \param[in] env pointer to the thread context
963 * \param[in] lfsck pointer to the lfsck instance
965 * \retval 0 for success
966 * \retval negative error number on failure
968 static int lfsck_create_lpf(const struct lu_env *env,
969 struct lfsck_instance *lfsck)
971 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
972 struct lfsck_thread_info *info = lfsck_env_info(env);
973 struct lu_fid *cfid = &info->lti_fid2;
974 struct lu_attr *la = &info->lti_la;
975 struct dt_object_format *dof = &info->lti_dof;
976 struct dt_object *parent = lfsck->li_lpf_root_obj;
977 struct dt_object *child = NULL;
978 struct lfsck_lock_handle *llh = &info->lti_llh;
980 int node = lfsck_dev_idx(lfsck);
984 LASSERT(lfsck->li_master);
985 LASSERT(parent != NULL);
986 LASSERT(lfsck->li_lpf_obj == NULL);
988 rc = lfsck_lock(env, lfsck, parent, name, llh,
989 MDS_INODELOCK_UPDATE, LCK_PW);
993 snprintf(name, 8, "MDT%04x", node);
994 if (fid_is_zero(&bk->lb_lpf_fid)) {
995 /* There is corner case that: in former LFSCK scanning we have
996 * created the .lustre/lost+found/MDTxxxx but failed to update
997 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
998 * it from MDT0 firstly. */
999 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1000 (const struct dt_key *)name, BYPASS_CAPA);
1001 if (rc != 0 && rc != -ENOENT)
1005 bk->lb_lpf_fid = *cfid;
1006 rc = lfsck_bookmark_store(env, lfsck);
1008 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1013 *cfid = bk->lb_lpf_fid;
1016 child = lfsck_object_find_bottom(env, lfsck, cfid);
1018 GOTO(unlock, rc = PTR_ERR(child));
1020 if (dt_object_exists(child) != 0) {
1021 if (unlikely(!dt_try_as_dir(env, child)))
1024 lfsck->li_lpf_obj = child;
1029 memset(la, 0, sizeof(*la));
1030 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1031 la->la_mode = S_IFDIR | S_IRWXU;
1032 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1034 memset(dof, 0, sizeof(*dof));
1035 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1038 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1040 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1042 lfsck->li_lpf_obj = child;
1048 if (rc != 0 && child != NULL && !IS_ERR(child))
1049 lfsck_object_put(env, child);
1055 * Scan .lustre/lost+found for bad name entries and remove them.
1057 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1058 * index in the system. Any other formatted name is invalid and should be
1061 * \param[in] env pointer to the thread context
1062 * \param[in] lfsck pointer to the lfsck instance
1064 * \retval 0 for success
1065 * \retval negative error number on failure
1067 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1068 struct lfsck_instance *lfsck)
1070 struct dt_object *parent = lfsck->li_lpf_root_obj;
1071 struct lu_dirent *ent =
1072 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1073 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1078 it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA);
1080 RETURN(PTR_ERR(it));
1082 rc = iops->load(env, it, 0);
1084 rc = iops->next(env, it);
1091 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1095 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1096 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1099 /* name length must be strlen("MDTxxxx") */
1100 if (ent->lde_namelen != 7)
1103 if (memcmp(ent->lde_name, "MDT", off) != 0)
1106 while (off < 7 && isxdigit(ent->lde_name[off]))
1112 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1119 rc = iops->next(env, it);
1123 iops->fini(env, it);
1125 RETURN(rc > 0 ? 0 : rc);
1128 static int lfsck_update_lpf_entry(const struct lu_env *env,
1129 struct lfsck_instance *lfsck,
1130 struct dt_object *parent,
1131 struct dt_object *child,
1133 enum lfsck_verify_lpf_types type)
1137 if (type == LVLT_BY_BOOKMARK) {
1138 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1139 lfsck_dto2fid(child), S_IFDIR);
1140 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1141 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1142 rc = lfsck_bookmark_store(env, lfsck);
1144 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1145 " in the bookmark file: rc = %d\n",
1146 lfsck_lfsck2name(lfsck),
1147 PFID(lfsck_dto2fid(child)), rc);
1154 * Check whether the @child back references the @parent.
1157 * 1) The child's FID is stored in the bookmark file. If the child back
1158 * references the parent (LU_LPF_FID object) via its ".." entry, then
1159 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1160 * the child back references another parent2, then:
1161 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1162 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1163 * references the child. So keep them there. As the LFSCK processing,
1164 * the parent3 may be found, then when the LFSCK run next time, the
1165 * inconsistency can be repaired.
1167 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1168 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1169 * via its ".." entry, then update the bookmark file, otherwise, if the child
1170 * back references another parent2, then:
1171 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1172 * from .lustre/lost+found/;
1173 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1174 * sub-directory name entry and update the child;
1175 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1176 * or not, then keep them there.
1178 * \param[in] env pointer to the thread context
1179 * \param[in] lfsck pointer to the lfsck instance
1180 * \param[in] child pointer to the lost+found sub-directory object
1181 * \param[in] name the name for lost+found sub-directory object
1182 * \param[out] fid pointer to the buffer to hold the FID of the object
1183 * (called it as parent2) that is referenced via the
1184 * child's dotdot entry; it also can be the FID that
1185 * is referenced by the name entry under the parent2.
1186 * \param[in] type to indicate where the child's FID is stored in
1188 * \retval positive number for uncertain inconsistency
1189 * \retval 0 for success
1190 * \retval negative error number on failure
1192 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1193 struct lfsck_instance *lfsck,
1194 struct dt_object *child, const char *name,
1196 enum lfsck_verify_lpf_types type)
1198 struct dt_object *parent = lfsck->li_lpf_root_obj;
1199 struct lfsck_thread_info *info = lfsck_env_info(env);
1200 char *name2 = info->lti_key;
1201 struct lu_fid *fid2 = &info->lti_fid3;
1202 struct dt_object *parent2 = NULL;
1203 struct lustre_handle lh = { 0 };
1208 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1209 (const struct dt_key *)dotdot, BYPASS_CAPA);
1213 if (!fid_is_sane(fid))
1214 GOTO(linkea, rc = -EINVAL);
1216 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1217 const struct lu_name *cname;
1219 if (lfsck->li_lpf_obj == NULL) {
1220 lu_object_get(&child->do_lu);
1221 lfsck->li_lpf_obj = child;
1224 cname = lfsck_name_get_const(env, name, strlen(name));
1225 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1227 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1233 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1234 if (IS_ERR(parent2))
1235 GOTO(linkea, parent2);
1237 if (!dt_object_exists(parent2)) {
1238 lfsck_object_put(env, parent2);
1240 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1243 if (!dt_try_as_dir(env, parent2)) {
1244 lfsck_object_put(env, parent2);
1246 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1250 /* To prevent rename/unlink race */
1251 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1252 MDS_INODELOCK_UPDATE, LCK_PR);
1256 dt_read_lock(env, child, 0);
1257 rc = lfsck_links_get_first(env, child, name2, fid2);
1259 dt_read_unlock(env, child);
1260 lfsck_ibits_unlock(&lh, LCK_PR);
1262 GOTO(out_put, rc = 1);
1265 /* It is almost impossible that the bookmark file (or the name entry)
1266 * and the linkEA hit the same data corruption. Trust the linkEA. */
1267 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1268 dt_read_unlock(env, child);
1269 lfsck_ibits_unlock(&lh, LCK_PR);
1272 if (lfsck->li_lpf_obj == NULL) {
1273 lu_object_get(&child->do_lu);
1274 lfsck->li_lpf_obj = child;
1277 /* Update the child's dotdot entry */
1278 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1279 &LU_LPF_FID, S_IFDIR);
1281 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1287 if (parent2 == NULL || IS_ERR(parent2)) {
1288 dt_read_unlock(env, child);
1289 lfsck_ibits_unlock(&lh, LCK_PR);
1291 GOTO(out_done, rc = 1);
1294 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1295 (const struct dt_key *)name2, BYPASS_CAPA);
1296 dt_read_unlock(env, child);
1297 lfsck_ibits_unlock(&lh, LCK_PR);
1298 if (rc != 0 && rc != -ENOENT)
1301 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1302 if (type == LVLT_BY_BOOKMARK)
1303 GOTO(out_put, rc = 1);
1305 /* Trust the name entry, update the child's dotdot entry. */
1306 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1307 &LU_LPF_FID, S_IFDIR);
1312 if (type == LVLT_BY_BOOKMARK) {
1313 /* Invalid FID record in the bookmark file, reset it. */
1314 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1315 rc = lfsck_bookmark_store(env, lfsck);
1317 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1318 " in the bookmark file: rc = %d\n",
1319 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1320 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1321 /* The name entry is wrong, remove it. */
1322 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1328 if (parent2 != NULL && !IS_ERR(parent2))
1329 lfsck_object_put(env, parent2);
1336 * Verify the /ROOT/.lustre/lost+found/ directory.
1338 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1339 * the LFSCK does not exactly know how to handle, such as orphans. So before
1340 * the LFSCK scanning the system, the consistency of such directory needs to
1341 * be verified firstly to allow the users to use it during the LFSCK.
1343 * \param[in] env pointer to the thread context
1344 * \param[in] lfsck pointer to the lfsck instance
1346 * \retval positive number for uncertain inconsistency
1347 * \retval 0 for success
1348 * \retval negative error number on failure
1350 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1352 struct lfsck_thread_info *info = lfsck_env_info(env);
1353 struct lu_fid *pfid = &info->lti_fid;
1354 struct lu_fid *cfid = &info->lti_fid2;
1355 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1356 struct dt_object *parent;
1357 /* child1's FID is in the bookmark file. */
1358 struct dt_object *child1 = NULL;
1359 /* child2's FID is in the name entry MDTxxxx. */
1360 struct dt_object *child2 = NULL;
1361 const struct lu_name *cname;
1363 int node = lfsck_dev_idx(lfsck);
1367 LASSERT(lfsck->li_master);
1369 if (lfsck->li_lpf_root_obj != NULL)
1373 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1376 struct lfsck_tgt_desc *ltd;
1378 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1379 if (unlikely(ltd == NULL))
1382 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1388 RETURN(PTR_ERR(parent));
1390 LASSERT(dt_object_exists(parent));
1392 if (unlikely(!dt_try_as_dir(env, parent))) {
1393 lfsck_object_put(env, parent);
1395 GOTO(put, rc = -ENOTDIR);
1398 lfsck->li_lpf_root_obj = parent;
1400 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1402 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1403 "for bad sub-directories: rc = %d\n",
1404 lfsck_lfsck2name(lfsck), rc);
1407 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1408 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1409 struct lu_fid tfid = bk->lb_lpf_fid;
1411 /* Invalid FID record in the bookmark file, reset it. */
1412 fid_zero(&bk->lb_lpf_fid);
1413 rc = lfsck_bookmark_store(env, lfsck);
1415 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1416 " in the bookmark file: rc = %d\n",
1417 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1422 child1 = lfsck_object_find_bottom(env, lfsck,
1424 if (IS_ERR(child1)) {
1429 if (unlikely(!dt_object_exists(child1) ||
1430 dt_object_remote(child1)) ||
1431 !S_ISDIR(lfsck_object_type(child1))) {
1432 /* Invalid FID record in the bookmark file,
1434 fid_zero(&bk->lb_lpf_fid);
1435 rc = lfsck_bookmark_store(env, lfsck);
1437 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1438 " in the bookmark file: rc = %d\n",
1439 lfsck_lfsck2name(lfsck),
1440 PFID(lfsck_dto2fid(child1)), rc);
1445 lfsck_object_put(env, child1);
1447 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1448 GOTO(put, rc = -ENOTDIR);
1454 snprintf(name, 8, "MDT%04x", node);
1455 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1456 (const struct dt_key *)name, BYPASS_CAPA);
1457 if (rc == -ENOENT) {
1458 if (!fid_is_zero(&bk->lb_lpf_fid))
1467 /* Invalid FID in the name entry, remove the name entry. */
1468 if (!fid_is_norm(cfid)) {
1469 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1476 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1478 GOTO(put, rc = PTR_ERR(child2));
1480 if (unlikely(!dt_object_exists(child2) ||
1481 dt_object_remote(child2)) ||
1482 !S_ISDIR(lfsck_object_type(child2))) {
1483 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1490 if (unlikely(!dt_try_as_dir(env, child2)))
1491 GOTO(put, rc = -ENOTDIR);
1493 if (child1 == NULL) {
1494 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1495 pfid, LVLT_BY_NAMEENTRY);
1496 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1497 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1498 pfid, LVLT_BY_BOOKMARK);
1499 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1500 rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1504 if (lfsck->li_lpf_obj == NULL) {
1505 lu_object_get(&child2->do_lu);
1506 lfsck->li_lpf_obj = child2;
1509 cname = lfsck_name_get_const(env, name, strlen(name));
1510 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1517 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1518 pfid, LVLT_BY_BOOKMARK);
1523 if (lfsck->li_lpf_obj != NULL) {
1524 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1525 lfsck_object_put(env, lfsck->li_lpf_obj);
1526 lfsck->li_lpf_obj = NULL;
1529 } else if (rc == 0) {
1530 rc = lfsck_create_lpf(env, lfsck);
1533 if (child2 != NULL && !IS_ERR(child2))
1534 lfsck_object_put(env, child2);
1535 if (child1 != NULL && !IS_ERR(child1))
1536 lfsck_object_put(env, child1);
1541 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1543 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1544 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1549 if (unlikely(ss == NULL))
1552 OBD_ALLOC_PTR(lfsck->li_seq);
1553 if (lfsck->li_seq == NULL)
1556 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1558 GOTO(out, rc = -ENOMEM);
1560 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1561 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1563 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1567 if (fid_is_sane(&bk->lb_last_fid))
1568 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1573 OBD_FREE_PTR(lfsck->li_seq);
1574 lfsck->li_seq = NULL;
1579 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1581 if (lfsck->li_seq != NULL) {
1582 seq_client_fini(lfsck->li_seq);
1583 OBD_FREE_PTR(lfsck->li_seq);
1584 lfsck->li_seq = NULL;
1588 void lfsck_instance_cleanup(const struct lu_env *env,
1589 struct lfsck_instance *lfsck)
1591 struct ptlrpc_thread *thread = &lfsck->li_thread;
1592 struct lfsck_component *com;
1593 struct lfsck_component *next;
1594 struct lfsck_lmv_unit *llu;
1595 struct lfsck_lmv_unit *llu_next;
1596 struct lfsck_lmv *llmv;
1599 LASSERT(list_empty(&lfsck->li_link));
1600 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1602 if (lfsck->li_obj_oit != NULL) {
1603 lfsck_object_put(env, lfsck->li_obj_oit);
1604 lfsck->li_obj_oit = NULL;
1607 LASSERT(lfsck->li_obj_dir == NULL);
1608 LASSERT(lfsck->li_lmv == NULL);
1610 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1611 llmv = &llu->llu_lmv;
1613 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1614 "still in using: %u\n",
1615 atomic_read(&llmv->ll_ref));
1617 lfsck_lmv_put(env, llmv);
1620 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1621 lfsck_component_cleanup(env, com);
1624 LASSERT(list_empty(&lfsck->li_list_dir));
1626 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1628 lfsck_component_cleanup(env, com);
1631 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1632 lfsck_component_cleanup(env, com);
1635 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1636 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1638 if (lfsck->li_lfsck_dir != NULL) {
1639 lfsck_object_put(env, lfsck->li_lfsck_dir);
1640 lfsck->li_lfsck_dir = NULL;
1643 if (lfsck->li_bookmark_obj != NULL) {
1644 lfsck_object_put(env, lfsck->li_bookmark_obj);
1645 lfsck->li_bookmark_obj = NULL;
1648 if (lfsck->li_lpf_obj != NULL) {
1649 lfsck_object_put(env, lfsck->li_lpf_obj);
1650 lfsck->li_lpf_obj = NULL;
1653 if (lfsck->li_lpf_root_obj != NULL) {
1654 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1655 lfsck->li_lpf_root_obj = NULL;
1658 if (lfsck->li_los != NULL) {
1659 local_oid_storage_fini(env, lfsck->li_los);
1660 lfsck->li_los = NULL;
1663 lfsck_fid_fini(lfsck);
1665 OBD_FREE_PTR(lfsck);
1668 static inline struct lfsck_instance *
1669 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1671 struct lfsck_instance *lfsck;
1673 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1674 if (lfsck->li_bottom == key) {
1676 lfsck_instance_get(lfsck);
1678 list_del_init(&lfsck->li_link);
1687 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1690 struct lfsck_instance *lfsck;
1692 spin_lock(&lfsck_instance_lock);
1693 lfsck = __lfsck_instance_find(key, ref, unlink);
1694 spin_unlock(&lfsck_instance_lock);
1699 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1701 struct lfsck_instance *tmp;
1703 spin_lock(&lfsck_instance_lock);
1704 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1705 if (lfsck->li_bottom == tmp->li_bottom) {
1706 spin_unlock(&lfsck_instance_lock);
1711 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1712 spin_unlock(&lfsck_instance_lock);
1716 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1721 bool newline = (bits != 0 ? false : true);
1723 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1725 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1728 if (names[i] != NULL) {
1732 seq_printf(m, "%s%c", names[i],
1733 newline ? '\n' : ',');
1739 seq_printf(m, "\n");
1743 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix)
1746 seq_printf(m, "%s: "LPU64" seconds\n", prefix,
1747 cfs_time_current_sec() - time);
1749 seq_printf(m, "%s: N/A\n", prefix);
1753 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1756 if (fid_is_zero(&pos->lp_dir_parent)) {
1757 if (pos->lp_oit_cookie == 0)
1758 seq_printf(m, "%s: N/A, N/A, N/A\n",
1761 seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1762 prefix, pos->lp_oit_cookie);
1764 seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1765 prefix, pos->lp_oit_cookie,
1766 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1771 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1772 struct lfsck_position *pos, bool init)
1774 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1776 if (unlikely(lfsck->li_di_oit == NULL)) {
1777 memset(pos, 0, sizeof(*pos));
1781 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1782 if (!lfsck->li_current_oit_processed && !init)
1783 pos->lp_oit_cookie--;
1785 LASSERT(pos->lp_oit_cookie > 0);
1787 if (lfsck->li_di_dir != NULL) {
1788 struct dt_object *dto = lfsck->li_obj_dir;
1790 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1793 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1794 fid_zero(&pos->lp_dir_parent);
1795 pos->lp_dir_cookie = 0;
1797 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1800 fid_zero(&pos->lp_dir_parent);
1801 pos->lp_dir_cookie = 0;
1805 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1809 if (limit != LFSCK_SPEED_NO_LIMIT) {
1810 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1811 lfsck->li_sleep_rate = limit /
1812 msecs_to_jiffies(MSEC_PER_SEC);
1813 lfsck->li_sleep_jif = 1;
1815 lfsck->li_sleep_rate = 1;
1816 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1820 lfsck->li_sleep_jif = 0;
1821 lfsck->li_sleep_rate = 0;
1824 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1825 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1832 void lfsck_control_speed(struct lfsck_instance *lfsck)
1834 struct ptlrpc_thread *thread = &lfsck->li_thread;
1835 struct l_wait_info lwi;
1837 if (lfsck->li_sleep_jif > 0 &&
1838 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1839 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1840 LWI_ON_SIGNAL_NOOP, NULL);
1842 l_wait_event(thread->t_ctl_waitq,
1843 !thread_is_running(thread),
1845 lfsck->li_new_scanned = 0;
1849 void lfsck_control_speed_by_self(struct lfsck_component *com)
1851 struct lfsck_instance *lfsck = com->lc_lfsck;
1852 struct ptlrpc_thread *thread = &lfsck->li_thread;
1853 struct l_wait_info lwi;
1855 if (lfsck->li_sleep_jif > 0 &&
1856 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1857 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1858 LWI_ON_SIGNAL_NOOP, NULL);
1860 l_wait_event(thread->t_ctl_waitq,
1861 !thread_is_running(thread),
1863 com->lc_new_scanned = 0;
1867 static struct lfsck_thread_args *
1868 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1869 struct lfsck_component *com,
1870 struct lfsck_start_param *lsp)
1872 struct lfsck_thread_args *lta;
1877 return ERR_PTR(-ENOMEM);
1879 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1885 lta->lta_lfsck = lfsck_instance_get(lfsck);
1887 lta->lta_com = lfsck_component_get(com);
1894 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1896 if (lta->lta_com != NULL)
1897 lfsck_component_put(<a->lta_env, lta->lta_com);
1898 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1899 lu_env_fini(<a->lta_env);
1903 struct lfsck_assistant_data *
1904 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1907 struct lfsck_assistant_data *lad;
1911 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1912 if (lad->lad_bitmap == NULL) {
1917 INIT_LIST_HEAD(&lad->lad_req_list);
1918 spin_lock_init(&lad->lad_lock);
1919 INIT_LIST_HEAD(&lad->lad_ost_list);
1920 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1921 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1922 INIT_LIST_HEAD(&lad->lad_mdt_list);
1923 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1924 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1925 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1927 lad->lad_name = name;
1934 * Generic LFSCK asynchronous communication interpretor function.
1935 * The LFSCK RPC reply for both the event notification and status
1936 * querying will be handled here.
1938 * \param[in] env pointer to the thread context
1939 * \param[in] req pointer to the LFSCK request
1940 * \param[in] args pointer to the lfsck_async_interpret_args
1941 * \param[in] rc the result for handling the LFSCK request
1943 * \retval 0 for success
1944 * \retval negative error number on failure
1946 int lfsck_async_interpret_common(const struct lu_env *env,
1947 struct ptlrpc_request *req,
1950 struct lfsck_async_interpret_args *laia = args;
1951 struct lfsck_component *com = laia->laia_com;
1952 struct lfsck_assistant_data *lad = com->lc_data;
1953 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1954 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1955 struct lfsck_request *lr = laia->laia_lr;
1957 LASSERT(com->lc_lfsck->li_master);
1959 switch (lr->lr_event) {
1962 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1964 lfsck_lfsck2name(com->lc_lfsck),
1965 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1966 ltd->ltd_index, lad->lad_name, rc);
1968 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1969 struct lfsck_layout *lo = com->lc_file_ram;
1971 if (lr->lr_flags & LEF_TO_OST)
1972 lfsck_lad_set_bitmap(env, com,
1975 lo->ll_flags |= LF_INCOMPLETE;
1977 struct lfsck_namespace *ns = com->lc_file_ram;
1979 /* If some MDT does not join the namespace
1980 * LFSCK, then we cannot know whether there
1981 * is some name entry on such MDT that with
1982 * the referenced MDT-object on this MDT or
1983 * not. So the namespace LFSCK on this MDT
1984 * cannot handle orphan MDT-objects properly.
1985 * So we mark the LFSCK as LF_INCOMPLETE and
1986 * skip orphan MDT-objects handling. */
1987 ns->ln_flags |= LF_INCOMPLETE;
1992 spin_lock(<ds->ltd_lock);
1993 if (ltd->ltd_dead) {
1994 spin_unlock(<ds->ltd_lock);
1998 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1999 struct list_head *list;
2000 struct list_head *phase_list;
2002 if (ltd->ltd_layout_done) {
2003 spin_unlock(<ds->ltd_lock);
2007 if (lr->lr_flags & LEF_TO_OST) {
2008 list = &lad->lad_ost_list;
2009 phase_list = &lad->lad_ost_phase1_list;
2011 list = &lad->lad_mdt_list;
2012 phase_list = &lad->lad_mdt_phase1_list;
2015 if (list_empty(<d->ltd_layout_list))
2016 list_add_tail(<d->ltd_layout_list, list);
2017 if (list_empty(<d->ltd_layout_phase_list))
2018 list_add_tail(<d->ltd_layout_phase_list,
2021 if (ltd->ltd_namespace_done) {
2022 spin_unlock(<ds->ltd_lock);
2026 if (list_empty(<d->ltd_namespace_list))
2027 list_add_tail(<d->ltd_namespace_list,
2028 &lad->lad_mdt_list);
2029 if (list_empty(<d->ltd_namespace_phase_list))
2030 list_add_tail(<d->ltd_namespace_phase_list,
2031 &lad->lad_mdt_phase1_list);
2033 spin_unlock(<ds->ltd_lock);
2036 case LE_PHASE1_DONE:
2037 case LE_PHASE2_DONE:
2039 if (rc != 0 && rc != -EALREADY)
2040 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2041 "event = %d, rc = %d\n",
2042 lfsck_lfsck2name(com->lc_lfsck),
2043 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2044 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2047 struct lfsck_reply *reply;
2048 struct list_head *list;
2049 struct list_head *phase_list;
2051 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2052 list = <d->ltd_layout_list;
2053 phase_list = <d->ltd_layout_phase_list;
2055 list = <d->ltd_namespace_list;
2056 phase_list = <d->ltd_namespace_phase_list;
2060 spin_lock(<ds->ltd_lock);
2061 list_del_init(phase_list);
2062 list_del_init(list);
2063 spin_unlock(<ds->ltd_lock);
2067 reply = req_capsule_server_get(&req->rq_pill,
2069 if (reply == NULL) {
2071 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2072 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2074 spin_lock(<ds->ltd_lock);
2075 list_del_init(phase_list);
2076 list_del_init(list);
2077 spin_unlock(<ds->ltd_lock);
2081 switch (reply->lr_status) {
2082 case LS_SCANNING_PHASE1:
2084 case LS_SCANNING_PHASE2:
2085 spin_lock(<ds->ltd_lock);
2086 list_del_init(phase_list);
2087 if (ltd->ltd_dead) {
2088 spin_unlock(<ds->ltd_lock);
2092 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2093 if (ltd->ltd_layout_done) {
2094 spin_unlock(<ds->ltd_lock);
2098 if (lr->lr_flags & LEF_TO_OST)
2099 list_add_tail(phase_list,
2100 &lad->lad_ost_phase2_list);
2102 list_add_tail(phase_list,
2103 &lad->lad_mdt_phase2_list);
2105 if (ltd->ltd_namespace_done) {
2106 spin_unlock(<ds->ltd_lock);
2110 list_add_tail(phase_list,
2111 &lad->lad_mdt_phase2_list);
2113 spin_unlock(<ds->ltd_lock);
2116 spin_lock(<ds->ltd_lock);
2117 list_del_init(phase_list);
2118 list_del_init(list);
2119 spin_unlock(<ds->ltd_lock);
2125 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2126 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2130 if (!laia->laia_shared) {
2132 lfsck_component_put(env, com);
2138 static void lfsck_interpret(const struct lu_env *env,
2139 struct lfsck_instance *lfsck,
2140 struct ptlrpc_request *req, void *args, int result)
2142 struct lfsck_async_interpret_args *laia = args;
2143 struct lfsck_component *com;
2145 LASSERT(laia->laia_com == NULL);
2146 LASSERT(laia->laia_shared);
2148 spin_lock(&lfsck->li_lock);
2149 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2150 laia->laia_com = com;
2151 lfsck_async_interpret_common(env, req, laia, result);
2154 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2155 laia->laia_com = com;
2156 lfsck_async_interpret_common(env, req, laia, result);
2158 spin_unlock(&lfsck->li_lock);
2161 static int lfsck_stop_notify(const struct lu_env *env,
2162 struct lfsck_instance *lfsck,
2163 struct lfsck_tgt_descs *ltds,
2164 struct lfsck_tgt_desc *ltd, __u16 type)
2166 struct lfsck_component *com;
2170 LASSERT(lfsck->li_master);
2172 spin_lock(&lfsck->li_lock);
2173 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2175 com = __lfsck_component_find(lfsck, type,
2176 &lfsck->li_list_double_scan);
2178 lfsck_component_get(com);
2179 spin_unlock(&lfsck->li_lock);
2182 struct lfsck_thread_info *info = lfsck_env_info(env);
2183 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2184 struct lfsck_request *lr = &info->lti_lr;
2185 struct lfsck_assistant_data *lad = com->lc_data;
2186 struct list_head *list;
2187 struct list_head *phase_list;
2188 struct ptlrpc_request_set *set;
2190 set = ptlrpc_prep_set();
2192 lfsck_component_put(env, com);
2197 if (type == LFSCK_TYPE_LAYOUT) {
2198 list = <d->ltd_layout_list;
2199 phase_list = <d->ltd_layout_phase_list;
2201 list = <d->ltd_namespace_list;
2202 phase_list = <d->ltd_namespace_phase_list;
2205 spin_lock(<ds->ltd_lock);
2206 if (list_empty(list)) {
2207 LASSERT(list_empty(phase_list));
2208 spin_unlock(<ds->ltd_lock);
2209 ptlrpc_set_destroy(set);
2214 list_del_init(phase_list);
2215 list_del_init(list);
2216 spin_unlock(<ds->ltd_lock);
2218 memset(lr, 0, sizeof(*lr));
2219 lr->lr_index = lfsck_dev_idx(lfsck);
2220 lr->lr_event = LE_PEER_EXIT;
2221 lr->lr_active = type;
2222 lr->lr_status = LS_CO_PAUSED;
2223 if (ltds == &lfsck->li_ost_descs)
2224 lr->lr_flags = LEF_TO_OST;
2226 laia->laia_com = com;
2227 laia->laia_ltds = ltds;
2228 atomic_inc(<d->ltd_ref);
2229 laia->laia_ltd = ltd;
2231 laia->laia_shared = 0;
2233 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2234 lfsck_async_interpret_common,
2235 laia, LFSCK_NOTIFY);
2237 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2238 "co-stop for %s: rc = %d\n",
2239 lfsck_lfsck2name(lfsck),
2240 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2241 ltd->ltd_index, lad->lad_name, rc);
2244 rc = ptlrpc_set_wait(set);
2247 ptlrpc_set_destroy(set);
2248 lfsck_component_put(env, com);
2254 static int lfsck_async_interpret(const struct lu_env *env,
2255 struct ptlrpc_request *req,
2258 struct lfsck_async_interpret_args *laia = args;
2259 struct lfsck_instance *lfsck;
2261 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2263 lfsck_interpret(env, lfsck, req, laia, rc);
2264 lfsck_tgt_put(laia->laia_ltd);
2265 if (rc != 0 && laia->laia_result != -EALREADY)
2266 laia->laia_result = rc;
2271 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2272 struct lfsck_request *lr,
2273 struct ptlrpc_request_set *set,
2274 ptlrpc_interpterer_t interpreter,
2275 void *args, int request)
2277 struct lfsck_async_interpret_args *laia;
2278 struct ptlrpc_request *req;
2279 struct lfsck_request *tmp;
2280 struct req_format *format;
2285 format = &RQF_LFSCK_NOTIFY;
2288 format = &RQF_LFSCK_QUERY;
2291 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2292 exp->exp_obd->obd_name, request, -EINVAL);
2296 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2300 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2302 ptlrpc_request_free(req);
2307 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2309 ptlrpc_request_set_replen(req);
2311 laia = ptlrpc_req_async_args(req);
2312 *laia = *(struct lfsck_async_interpret_args *)args;
2313 if (laia->laia_com != NULL)
2314 lfsck_component_get(laia->laia_com);
2315 req->rq_interpret_reply = interpreter;
2316 ptlrpc_set_add_req(set, req);
2321 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2322 struct lfsck_start_param *lsp)
2324 struct lfsck_instance *lfsck = com->lc_lfsck;
2325 struct lfsck_assistant_data *lad = com->lc_data;
2326 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2327 struct ptlrpc_thread *athread = &lad->lad_thread;
2328 struct lfsck_thread_args *lta;
2329 struct task_struct *task;
2333 lad->lad_assistant_status = 0;
2334 lad->lad_post_result = 0;
2335 lad->lad_to_post = 0;
2336 lad->lad_to_double_scan = 0;
2337 lad->lad_in_double_scan = 0;
2339 thread_set_flags(athread, 0);
2341 lta = lfsck_thread_args_init(lfsck, com, lsp);
2343 RETURN(PTR_ERR(lta));
2345 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2348 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2349 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2350 lfsck_thread_args_fini(lta);
2352 struct l_wait_info lwi = { 0 };
2354 l_wait_event(mthread->t_ctl_waitq,
2355 thread_is_running(athread) ||
2356 thread_is_stopped(athread),
2358 if (unlikely(!thread_is_running(athread)))
2359 rc = lad->lad_assistant_status;
2367 int lfsck_checkpoint_generic(const struct lu_env *env,
2368 struct lfsck_component *com)
2370 struct lfsck_assistant_data *lad = com->lc_data;
2371 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2372 struct ptlrpc_thread *athread = &lad->lad_thread;
2373 struct l_wait_info lwi = { 0 };
2375 l_wait_event(mthread->t_ctl_waitq,
2376 list_empty(&lad->lad_req_list) ||
2377 !thread_is_running(mthread) ||
2378 thread_is_stopped(athread),
2381 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2382 return LFSCK_CHECKPOINT_SKIP;
2387 void lfsck_post_generic(const struct lu_env *env,
2388 struct lfsck_component *com, int *result)
2390 struct lfsck_assistant_data *lad = com->lc_data;
2391 struct ptlrpc_thread *athread = &lad->lad_thread;
2392 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2393 struct l_wait_info lwi = { 0 };
2395 lad->lad_post_result = *result;
2398 lad->lad_to_post = 1;
2400 wake_up_all(&athread->t_ctl_waitq);
2401 l_wait_event(mthread->t_ctl_waitq,
2402 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2403 thread_is_stopped(athread),
2406 if (lad->lad_assistant_status < 0)
2407 *result = lad->lad_assistant_status;
2410 int lfsck_double_scan_generic(const struct lu_env *env,
2411 struct lfsck_component *com, int status)
2413 struct lfsck_assistant_data *lad = com->lc_data;
2414 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2415 struct ptlrpc_thread *athread = &lad->lad_thread;
2416 struct l_wait_info lwi = { 0 };
2418 if (status != LS_SCANNING_PHASE2)
2421 lad->lad_to_double_scan = 1;
2423 wake_up_all(&athread->t_ctl_waitq);
2424 l_wait_event(mthread->t_ctl_waitq,
2425 lad->lad_in_double_scan ||
2426 thread_is_stopped(athread),
2429 if (lad->lad_assistant_status < 0)
2430 return lad->lad_assistant_status;
2435 void lfsck_quit_generic(const struct lu_env *env,
2436 struct lfsck_component *com)
2438 struct lfsck_assistant_data *lad = com->lc_data;
2439 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2440 struct ptlrpc_thread *athread = &lad->lad_thread;
2441 struct l_wait_info lwi = { 0 };
2444 wake_up_all(&athread->t_ctl_waitq);
2445 l_wait_event(mthread->t_ctl_waitq,
2446 thread_is_init(athread) ||
2447 thread_is_stopped(athread),
2451 /* external interfaces */
2453 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2456 struct lfsck_instance *lfsck;
2460 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2464 lfsck = lfsck_instance_find(key, true, false);
2465 if (likely(lfsck != NULL)) {
2466 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2467 lfsck_instance_put(&env, lfsck);
2476 EXPORT_SYMBOL(lfsck_get_speed);
2478 int lfsck_set_speed(struct dt_device *key, int val)
2481 struct lfsck_instance *lfsck;
2485 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2489 lfsck = lfsck_instance_find(key, true, false);
2490 if (likely(lfsck != NULL)) {
2491 mutex_lock(&lfsck->li_mutex);
2492 if (__lfsck_set_speed(lfsck, val))
2493 rc = lfsck_bookmark_store(&env, lfsck);
2494 mutex_unlock(&lfsck->li_mutex);
2495 lfsck_instance_put(&env, lfsck);
2504 EXPORT_SYMBOL(lfsck_set_speed);
2506 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2509 struct lfsck_instance *lfsck;
2513 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2517 lfsck = lfsck_instance_find(key, true, false);
2518 if (likely(lfsck != NULL)) {
2519 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2520 lfsck_instance_put(&env, lfsck);
2529 EXPORT_SYMBOL(lfsck_get_windows);
2531 int lfsck_set_windows(struct dt_device *key, int val)
2534 struct lfsck_instance *lfsck;
2538 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2542 lfsck = lfsck_instance_find(key, true, false);
2543 if (likely(lfsck != NULL)) {
2544 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2545 CWARN("%s: invalid async windows size that may "
2546 "cause memory issues. The valid range is "
2548 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2550 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2551 mutex_lock(&lfsck->li_mutex);
2552 lfsck->li_bookmark_ram.lb_async_windows = val;
2553 rc = lfsck_bookmark_store(&env, lfsck);
2554 mutex_unlock(&lfsck->li_mutex);
2556 lfsck_instance_put(&env, lfsck);
2565 EXPORT_SYMBOL(lfsck_set_windows);
2567 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2570 struct lfsck_instance *lfsck;
2571 struct lfsck_component *com;
2575 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2579 lfsck = lfsck_instance_find(key, true, false);
2580 if (likely(lfsck != NULL)) {
2581 com = lfsck_component_find(lfsck, type);
2582 if (likely(com != NULL)) {
2583 rc = com->lc_ops->lfsck_dump(&env, com, m);
2584 lfsck_component_put(&env, com);
2589 lfsck_instance_put(&env, lfsck);
2598 EXPORT_SYMBOL(lfsck_dump);
2600 static int lfsck_stop_all(const struct lu_env *env,
2601 struct lfsck_instance *lfsck,
2602 struct lfsck_stop *stop)
2604 struct lfsck_thread_info *info = lfsck_env_info(env);
2605 struct lfsck_request *lr = &info->lti_lr;
2606 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2607 struct ptlrpc_request_set *set;
2608 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2609 struct lfsck_tgt_desc *ltd;
2610 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2616 LASSERT(stop->ls_flags & LPF_BROADCAST);
2618 set = ptlrpc_prep_set();
2619 if (unlikely(set == NULL))
2622 memset(lr, 0, sizeof(*lr));
2623 lr->lr_event = LE_STOP;
2624 lr->lr_index = lfsck_dev_idx(lfsck);
2625 lr->lr_status = stop->ls_status;
2626 lr->lr_version = bk->lb_version;
2627 lr->lr_active = LFSCK_TYPES_ALL;
2628 lr->lr_param = stop->ls_flags;
2630 laia->laia_com = NULL;
2631 laia->laia_ltds = ltds;
2633 laia->laia_result = 0;
2634 laia->laia_shared = 1;
2636 down_read(<ds->ltd_rw_sem);
2637 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2638 ltd = lfsck_tgt_get(ltds, idx);
2639 LASSERT(ltd != NULL);
2641 laia->laia_ltd = ltd;
2642 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2643 lfsck_async_interpret, laia,
2646 lfsck_interpret(env, lfsck, NULL, laia, rc);
2648 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2649 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2653 up_read(<ds->ltd_rw_sem);
2655 rc = ptlrpc_set_wait(set);
2656 ptlrpc_set_destroy(set);
2659 rc = laia->laia_result;
2661 if (rc == -EALREADY)
2665 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2666 lfsck_lfsck2name(lfsck), rc);
2668 RETURN(rc != 0 ? rc : rc1);
2671 static int lfsck_start_all(const struct lu_env *env,
2672 struct lfsck_instance *lfsck,
2673 struct lfsck_start *start)
2675 struct lfsck_thread_info *info = lfsck_env_info(env);
2676 struct lfsck_request *lr = &info->lti_lr;
2677 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2678 struct ptlrpc_request_set *set;
2679 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2680 struct lfsck_tgt_desc *ltd;
2681 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2686 LASSERT(start->ls_flags & LPF_BROADCAST);
2688 set = ptlrpc_prep_set();
2689 if (unlikely(set == NULL))
2692 memset(lr, 0, sizeof(*lr));
2693 lr->lr_event = LE_START;
2694 lr->lr_index = lfsck_dev_idx(lfsck);
2695 lr->lr_speed = bk->lb_speed_limit;
2696 lr->lr_version = bk->lb_version;
2697 lr->lr_active = start->ls_active;
2698 lr->lr_param = start->ls_flags;
2699 lr->lr_async_windows = bk->lb_async_windows;
2700 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2701 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2704 laia->laia_com = NULL;
2705 laia->laia_ltds = ltds;
2707 laia->laia_result = 0;
2708 laia->laia_shared = 1;
2710 down_read(<ds->ltd_rw_sem);
2711 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2712 ltd = lfsck_tgt_get(ltds, idx);
2713 LASSERT(ltd != NULL);
2715 laia->laia_ltd = ltd;
2716 ltd->ltd_layout_done = 0;
2717 ltd->ltd_namespace_done = 0;
2718 ltd->ltd_synced_failures = 0;
2719 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2720 lfsck_async_interpret, laia,
2723 lfsck_interpret(env, lfsck, NULL, laia, rc);
2725 CERROR("%s: cannot notify MDT %x for LFSCK "
2726 "start, failout: rc = %d\n",
2727 lfsck_lfsck2name(lfsck), idx, rc);
2731 up_read(<ds->ltd_rw_sem);
2734 ptlrpc_set_destroy(set);
2739 rc = ptlrpc_set_wait(set);
2740 ptlrpc_set_destroy(set);
2743 rc = laia->laia_result;
2746 struct lfsck_stop *stop = &info->lti_stop;
2748 CERROR("%s: cannot start LFSCK on some MDTs, "
2749 "stop all: rc = %d\n",
2750 lfsck_lfsck2name(lfsck), rc);
2751 if (rc != -EALREADY) {
2752 stop->ls_status = LS_FAILED;
2753 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2754 lfsck_stop_all(env, lfsck, stop);
2761 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2762 struct lfsck_start_param *lsp)
2764 struct lfsck_start *start = lsp->lsp_start;
2765 struct lfsck_instance *lfsck;
2766 struct lfsck_bookmark *bk;
2767 struct ptlrpc_thread *thread;
2768 struct lfsck_component *com;
2769 struct l_wait_info lwi = { 0 };
2770 struct lfsck_thread_args *lta;
2771 struct task_struct *task;
2778 lfsck = lfsck_instance_find(key, true, false);
2779 if (unlikely(lfsck == NULL))
2782 /* System is not ready, try again later. */
2783 if (unlikely(lfsck->li_namespace == NULL))
2784 GOTO(put, rc = -EAGAIN);
2786 /* start == NULL means auto trigger paused LFSCK. */
2787 if ((start == NULL) &&
2788 (list_empty(&lfsck->li_list_scan) ||
2789 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2792 bk = &lfsck->li_bookmark_ram;
2793 thread = &lfsck->li_thread;
2794 mutex_lock(&lfsck->li_mutex);
2795 spin_lock(&lfsck->li_lock);
2796 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2798 if (unlikely(start == NULL)) {
2799 spin_unlock(&lfsck->li_lock);
2803 while (start->ls_active != 0) {
2804 if (!(type & start->ls_active)) {
2809 com = __lfsck_component_find(lfsck, type,
2810 &lfsck->li_list_scan);
2812 com = __lfsck_component_find(lfsck, type,
2813 &lfsck->li_list_double_scan);
2819 if (com->lc_ops->lfsck_join != NULL) {
2820 rc = com->lc_ops->lfsck_join( env, com, lsp);
2821 if (rc != 0 && rc != -EALREADY)
2824 start->ls_active &= ~type;
2827 spin_unlock(&lfsck->li_lock);
2830 spin_unlock(&lfsck->li_lock);
2832 lfsck->li_status = 0;
2833 lfsck->li_oit_over = 0;
2834 lfsck->li_start_unplug = 0;
2835 lfsck->li_drop_dryrun = 0;
2836 lfsck->li_new_scanned = 0;
2838 /* For auto trigger. */
2842 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2843 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2844 lfsck_lfsck2name(lfsck));
2846 GOTO(out, rc = -EPERM);
2849 start->ls_version = bk->lb_version;
2851 if (start->ls_active != 0) {
2852 struct lfsck_component *next;
2854 if (start->ls_active == LFSCK_TYPES_ALL)
2855 start->ls_active = LFSCK_TYPES_SUPPORTED;
2857 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2858 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2859 GOTO(out, rc = -ENOTSUPP);
2862 list_for_each_entry_safe(com, next,
2863 &lfsck->li_list_scan, lc_link) {
2864 if (!(com->lc_type & start->ls_active)) {
2865 rc = com->lc_ops->lfsck_post(env, com, 0,
2872 while (start->ls_active != 0) {
2873 if (type & start->ls_active) {
2874 com = __lfsck_component_find(lfsck, type,
2875 &lfsck->li_list_idle);
2877 /* The component status will be updated
2878 * when its prep() is called later by
2879 * the LFSCK main engine. */
2880 list_move_tail(&com->lc_link,
2881 &lfsck->li_list_scan);
2882 start->ls_active &= ~type;
2888 if (list_empty(&lfsck->li_list_scan)) {
2889 /* The speed limit will be used to control both the LFSCK and
2890 * low layer scrub (if applied), need to be handled firstly. */
2891 if (start->ls_valid & LSV_SPEED_LIMIT) {
2892 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2893 rc = lfsck_bookmark_store(env, lfsck);
2902 if (start->ls_flags & LPF_RESET)
2903 flags |= DOIF_RESET;
2905 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2909 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2910 start->ls_active |= com->lc_type;
2911 if (flags & DOIF_RESET) {
2912 rc = com->lc_ops->lfsck_reset(env, com, false);
2919 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2920 if (bk->lb_param & LPF_DRYRUN)
2921 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2923 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2924 valid |= DOIV_ERROR_HANDLE;
2925 if (start->ls_flags & LPF_FAILOUT)
2926 flags |= DOIF_FAILOUT;
2929 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2930 valid |= DOIV_DRYRUN;
2931 if (start->ls_flags & LPF_DRYRUN)
2932 flags |= DOIF_DRYRUN;
2935 if (!list_empty(&lfsck->li_list_scan))
2936 flags |= DOIF_OUTUSED;
2938 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2939 thread_set_flags(thread, 0);
2940 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2942 GOTO(out, rc = PTR_ERR(lta));
2944 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2945 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2948 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2949 lfsck_lfsck2name(lfsck), rc);
2950 lfsck_thread_args_fini(lta);
2955 l_wait_event(thread->t_ctl_waitq,
2956 thread_is_running(thread) ||
2957 thread_is_stopped(thread),
2959 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2960 lfsck->li_start_unplug = 1;
2961 wake_up_all(&thread->t_ctl_waitq);
2966 /* release lfsck::li_mutex to avoid deadlock. */
2967 mutex_unlock(&lfsck->li_mutex);
2968 rc = lfsck_start_all(env, lfsck, start);
2970 spin_lock(&lfsck->li_lock);
2971 if (thread_is_stopped(thread)) {
2972 spin_unlock(&lfsck->li_lock);
2974 lfsck->li_status = LS_FAILED;
2975 lfsck->li_flags = 0;
2976 thread_set_flags(thread, SVC_STOPPING);
2977 spin_unlock(&lfsck->li_lock);
2979 lfsck->li_start_unplug = 1;
2980 wake_up_all(&thread->t_ctl_waitq);
2981 l_wait_event(thread->t_ctl_waitq,
2982 thread_is_stopped(thread),
2986 lfsck->li_start_unplug = 1;
2987 wake_up_all(&thread->t_ctl_waitq);
2993 mutex_unlock(&lfsck->li_mutex);
2996 lfsck_instance_put(env, lfsck);
2998 return rc < 0 ? rc : 0;
3000 EXPORT_SYMBOL(lfsck_start);
3002 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3003 struct lfsck_stop *stop)
3005 struct lfsck_instance *lfsck;
3006 struct ptlrpc_thread *thread;
3007 struct l_wait_info lwi = { 0 };
3012 lfsck = lfsck_instance_find(key, true, false);
3013 if (unlikely(lfsck == NULL))
3016 thread = &lfsck->li_thread;
3017 /* release lfsck::li_mutex to avoid deadlock. */
3018 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3019 if (!lfsck->li_master) {
3020 CERROR("%s: only allow to specify '-A' via MDS\n",
3021 lfsck_lfsck2name(lfsck));
3023 GOTO(out, rc = -EPERM);
3026 rc1 = lfsck_stop_all(env, lfsck, stop);
3029 mutex_lock(&lfsck->li_mutex);
3030 spin_lock(&lfsck->li_lock);
3031 /* no error if LFSCK is already stopped, or was never started */
3032 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3033 spin_unlock(&lfsck->li_lock);
3038 lfsck->li_status = stop->ls_status;
3039 lfsck->li_flags = stop->ls_flags;
3041 lfsck->li_status = LS_STOPPED;
3042 lfsck->li_flags = 0;
3045 thread_set_flags(thread, SVC_STOPPING);
3046 spin_unlock(&lfsck->li_lock);
3048 wake_up_all(&thread->t_ctl_waitq);
3049 l_wait_event(thread->t_ctl_waitq,
3050 thread_is_stopped(thread),
3056 mutex_unlock(&lfsck->li_mutex);
3057 lfsck_instance_put(env, lfsck);
3059 return rc != 0 ? rc : rc1;
3061 EXPORT_SYMBOL(lfsck_stop);
3063 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3064 struct lfsck_request *lr, struct thandle *th)
3066 int rc = -EOPNOTSUPP;
3069 switch (lr->lr_event) {
3071 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3072 struct lfsck_start_param lsp;
3074 memset(start, 0, sizeof(*start));
3075 start->ls_valid = lr->lr_valid;
3076 start->ls_speed_limit = lr->lr_speed;
3077 start->ls_version = lr->lr_version;
3078 start->ls_active = lr->lr_active;
3079 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3080 start->ls_async_windows = lr->lr_async_windows;
3082 lsp.lsp_start = start;
3083 lsp.lsp_index = lr->lr_index;
3084 lsp.lsp_index_valid = 1;
3085 rc = lfsck_start(env, key, &lsp);
3089 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3091 memset(stop, 0, sizeof(*stop));
3092 stop->ls_status = lr->lr_status;
3093 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3094 rc = lfsck_stop(env, key, stop);
3097 case LE_PHASE1_DONE:
3098 case LE_PHASE2_DONE:
3099 case LE_FID_ACCESSED:
3101 case LE_CONDITIONAL_DESTROY:
3102 case LE_SKIP_NLINK_DECLARE:
3104 case LE_SET_LMV_MASTER:
3105 case LE_SET_LMV_SLAVE:
3106 case LE_PAIRS_VERIFY: {
3107 struct lfsck_instance *lfsck;
3108 struct lfsck_component *com;
3110 lfsck = lfsck_instance_find(key, true, false);
3111 if (unlikely(lfsck == NULL))
3114 com = lfsck_component_find(lfsck, lr->lr_active);
3115 if (likely(com != NULL)) {
3116 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3117 lfsck_component_put(env, com);
3120 lfsck_instance_put(env, lfsck);
3129 EXPORT_SYMBOL(lfsck_in_notify);
3131 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3132 struct lfsck_request *lr)
3134 struct lfsck_instance *lfsck;
3135 struct lfsck_component *com;
3139 lfsck = lfsck_instance_find(key, true, false);
3140 if (unlikely(lfsck == NULL))
3143 com = lfsck_component_find(lfsck, lr->lr_active);
3144 if (likely(com != NULL)) {
3145 rc = com->lc_ops->lfsck_query(env, com);
3146 lfsck_component_put(env, com);
3151 lfsck_instance_put(env, lfsck);
3156 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3157 struct ldlm_namespace *ns)
3159 struct lfsck_instance *lfsck;
3162 lfsck = lfsck_instance_find(key, true, false);
3163 if (likely(lfsck != NULL)) {
3164 lfsck->li_namespace = ns;
3165 lfsck_instance_put(env, lfsck);
3171 EXPORT_SYMBOL(lfsck_register_namespace);
3173 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3174 struct dt_device *next, struct obd_device *obd,
3175 lfsck_out_notify notify, void *notify_data, bool master)
3177 struct lfsck_instance *lfsck;
3178 struct dt_object *root = NULL;
3179 struct dt_object *obj = NULL;
3180 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3184 lfsck = lfsck_instance_find(key, false, false);
3185 if (unlikely(lfsck != NULL))
3188 OBD_ALLOC_PTR(lfsck);
3192 mutex_init(&lfsck->li_mutex);
3193 spin_lock_init(&lfsck->li_lock);
3194 INIT_LIST_HEAD(&lfsck->li_link);
3195 INIT_LIST_HEAD(&lfsck->li_list_scan);
3196 INIT_LIST_HEAD(&lfsck->li_list_dir);
3197 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3198 INIT_LIST_HEAD(&lfsck->li_list_idle);
3199 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3200 atomic_set(&lfsck->li_ref, 1);
3201 atomic_set(&lfsck->li_double_scan_count, 0);
3202 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3203 lfsck->li_out_notify = notify;
3204 lfsck->li_out_notify_data = notify_data;
3205 lfsck->li_next = next;
3206 lfsck->li_bottom = key;
3207 lfsck->li_obd = obd;
3209 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3213 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3217 fid->f_seq = FID_SEQ_LOCAL_NAME;
3220 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3224 rc = dt_root_get(env, key, fid);
3228 root = dt_locate(env, key, fid);
3230 GOTO(out, rc = PTR_ERR(root));
3232 if (unlikely(!dt_try_as_dir(env, root)))
3233 GOTO(out, rc = -ENOTDIR);
3235 lfsck->li_local_root_fid = *fid;
3237 lfsck->li_master = 1;
3238 if (lfsck_dev_idx(lfsck) == 0) {
3239 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3240 const struct lu_name *cname;
3242 rc = dt_lookup(env, root,
3243 (struct dt_rec *)(&lfsck->li_global_root_fid),
3244 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3248 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3250 GOTO(out, rc = PTR_ERR(obj));
3252 if (unlikely(!dt_try_as_dir(env, obj)))
3253 GOTO(out, rc = -ENOTDIR);
3255 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3256 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3260 lfsck_object_put(env, obj);
3261 obj = dt_locate(env, key, fid);
3263 GOTO(out, rc = PTR_ERR(obj));
3265 cname = lfsck_name_get_const(env, dotlustre,
3267 rc = lfsck_verify_linkea(env, obj, cname,
3268 &lfsck->li_global_root_fid);
3272 if (unlikely(!dt_try_as_dir(env, obj)))
3273 GOTO(out, rc = -ENOTDIR);
3276 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3277 (const struct dt_key *)lostfound,
3282 lfsck_object_put(env, obj);
3283 obj = dt_locate(env, key, fid);
3285 GOTO(out, rc = PTR_ERR(obj));
3287 cname = lfsck_name_get_const(env, lostfound,
3289 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3293 lfsck_object_put(env, obj);
3298 fid->f_seq = FID_SEQ_LOCAL_FILE;
3299 fid->f_oid = OTABLE_IT_OID;
3301 obj = dt_locate(env, key, fid);
3303 GOTO(out, rc = PTR_ERR(obj));
3305 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3309 lfsck->li_obj_oit = obj;
3310 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3311 S_IFDIR | S_IRUGO | S_IWUSR);
3313 GOTO(out, rc = PTR_ERR(obj));
3315 lu_object_get(&obj->do_lu);
3316 lfsck->li_lfsck_dir = obj;
3317 rc = lfsck_bookmark_setup(env, lfsck);
3322 rc = lfsck_fid_init(lfsck);
3326 rc = lfsck_namespace_setup(env, lfsck);
3331 rc = lfsck_layout_setup(env, lfsck);
3335 /* XXX: more LFSCK components initialization to be added here. */
3337 rc = lfsck_instance_add(lfsck);
3339 rc = lfsck_add_target_from_orphan(env, lfsck);
3341 if (obj != NULL && !IS_ERR(obj))
3342 lfsck_object_put(env, obj);
3343 if (root != NULL && !IS_ERR(root))
3344 lfsck_object_put(env, root);
3346 lfsck_instance_cleanup(env, lfsck);
3349 EXPORT_SYMBOL(lfsck_register);
3351 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3353 struct lfsck_instance *lfsck;
3355 lfsck = lfsck_instance_find(key, false, true);
3357 lfsck_instance_put(env, lfsck);
3359 EXPORT_SYMBOL(lfsck_degister);
3361 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3362 struct dt_device *tgt, struct obd_export *exp,
3363 __u32 index, bool for_ost)
3365 struct lfsck_instance *lfsck;
3366 struct lfsck_tgt_desc *ltd;
3377 INIT_LIST_HEAD(<d->ltd_orphan_list);
3378 INIT_LIST_HEAD(<d->ltd_layout_list);
3379 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3380 INIT_LIST_HEAD(<d->ltd_namespace_list);
3381 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3382 atomic_set(<d->ltd_ref, 1);
3383 ltd->ltd_index = index;
3385 spin_lock(&lfsck_instance_lock);
3386 lfsck = __lfsck_instance_find(key, true, false);
3387 if (lfsck == NULL) {
3389 list_add_tail(<d->ltd_orphan_list,
3390 &lfsck_ost_orphan_list);
3392 list_add_tail(<d->ltd_orphan_list,
3393 &lfsck_mdt_orphan_list);
3394 spin_unlock(&lfsck_instance_lock);
3398 spin_unlock(&lfsck_instance_lock);
3400 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3404 lfsck_instance_put(env, lfsck);
3408 EXPORT_SYMBOL(lfsck_add_target);
3410 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3411 struct dt_device *tgt, __u32 index, bool for_ost)
3413 struct lfsck_instance *lfsck;
3414 struct lfsck_tgt_descs *ltds;
3415 struct lfsck_tgt_desc *ltd;
3416 struct list_head *head;
3419 head = &lfsck_ost_orphan_list;
3421 head = &lfsck_mdt_orphan_list;
3423 spin_lock(&lfsck_instance_lock);
3424 list_for_each_entry(ltd, head, ltd_orphan_list) {
3425 if (ltd->ltd_tgt == tgt) {
3426 list_del_init(<d->ltd_orphan_list);
3427 spin_unlock(&lfsck_instance_lock);
3435 lfsck = __lfsck_instance_find(key, true, false);
3436 spin_unlock(&lfsck_instance_lock);
3437 if (unlikely(lfsck == NULL))
3441 ltds = &lfsck->li_ost_descs;
3443 ltds = &lfsck->li_mdt_descs;
3445 down_write(<ds->ltd_rw_sem);
3446 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3448 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3451 ltd = LTD_TGT(ltds, index);
3452 if (unlikely(ltd == NULL))
3455 LASSERT(ltds->ltd_tgtnr > 0);
3458 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3459 LTD_TGT(ltds, index) = NULL;
3464 head = &lfsck->li_ost_descs.ltd_orphan;
3466 head = &lfsck->li_mdt_descs.ltd_orphan;
3468 list_for_each_entry(ltd, head, ltd_orphan_list) {
3469 if (ltd->ltd_tgt == tgt) {
3470 list_del_init(<d->ltd_orphan_list);
3476 up_write(<ds->ltd_rw_sem);
3478 spin_lock(<ds->ltd_lock);
3480 spin_unlock(<ds->ltd_lock);
3481 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3482 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3486 lfsck_instance_put(env, lfsck);
3488 EXPORT_SYMBOL(lfsck_del_target);
3490 static int __init lfsck_init(void)
3494 INIT_LIST_HEAD(&lfsck_instance_list);
3495 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3496 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3497 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3498 rc = lu_context_key_register(&lfsck_thread_key);
3500 tgt_register_lfsck_in_notify(lfsck_in_notify);
3501 tgt_register_lfsck_query(lfsck_query);
3507 static void __exit lfsck_exit(void)
3509 struct lfsck_tgt_desc *ltd;
3510 struct lfsck_tgt_desc *next;
3512 LASSERT(list_empty(&lfsck_instance_list));
3514 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3516 list_del_init(<d->ltd_orphan_list);
3520 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3522 list_del_init(<d->ltd_orphan_list);
3526 lu_context_key_degister(&lfsck_thread_key);
3529 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3530 MODULE_DESCRIPTION("LFSCK");
3531 MODULE_LICENSE("GPL");
3533 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);