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 if (com->lc_new_checked == 0)
2376 return LFSCK_CHECKPOINT_SKIP;
2378 l_wait_event(mthread->t_ctl_waitq,
2379 list_empty(&lad->lad_req_list) ||
2380 !thread_is_running(mthread) ||
2381 thread_is_stopped(athread),
2384 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2385 return LFSCK_CHECKPOINT_SKIP;
2390 void lfsck_post_generic(const struct lu_env *env,
2391 struct lfsck_component *com, int *result)
2393 struct lfsck_assistant_data *lad = com->lc_data;
2394 struct ptlrpc_thread *athread = &lad->lad_thread;
2395 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2396 struct l_wait_info lwi = { 0 };
2398 lad->lad_post_result = *result;
2401 lad->lad_to_post = 1;
2403 wake_up_all(&athread->t_ctl_waitq);
2404 l_wait_event(mthread->t_ctl_waitq,
2405 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2406 thread_is_stopped(athread),
2409 if (lad->lad_assistant_status < 0)
2410 *result = lad->lad_assistant_status;
2413 int lfsck_double_scan_generic(const struct lu_env *env,
2414 struct lfsck_component *com, int status)
2416 struct lfsck_assistant_data *lad = com->lc_data;
2417 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2418 struct ptlrpc_thread *athread = &lad->lad_thread;
2419 struct l_wait_info lwi = { 0 };
2421 if (status != LS_SCANNING_PHASE2)
2424 lad->lad_to_double_scan = 1;
2426 wake_up_all(&athread->t_ctl_waitq);
2427 l_wait_event(mthread->t_ctl_waitq,
2428 lad->lad_in_double_scan ||
2429 thread_is_stopped(athread),
2432 if (lad->lad_assistant_status < 0)
2433 return lad->lad_assistant_status;
2438 void lfsck_quit_generic(const struct lu_env *env,
2439 struct lfsck_component *com)
2441 struct lfsck_assistant_data *lad = com->lc_data;
2442 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2443 struct ptlrpc_thread *athread = &lad->lad_thread;
2444 struct l_wait_info lwi = { 0 };
2447 wake_up_all(&athread->t_ctl_waitq);
2448 l_wait_event(mthread->t_ctl_waitq,
2449 thread_is_init(athread) ||
2450 thread_is_stopped(athread),
2454 /* external interfaces */
2456 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2459 struct lfsck_instance *lfsck;
2463 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2467 lfsck = lfsck_instance_find(key, true, false);
2468 if (likely(lfsck != NULL)) {
2469 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2470 lfsck_instance_put(&env, lfsck);
2479 EXPORT_SYMBOL(lfsck_get_speed);
2481 int lfsck_set_speed(struct dt_device *key, int val)
2484 struct lfsck_instance *lfsck;
2488 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2492 lfsck = lfsck_instance_find(key, true, false);
2493 if (likely(lfsck != NULL)) {
2494 mutex_lock(&lfsck->li_mutex);
2495 if (__lfsck_set_speed(lfsck, val))
2496 rc = lfsck_bookmark_store(&env, lfsck);
2497 mutex_unlock(&lfsck->li_mutex);
2498 lfsck_instance_put(&env, lfsck);
2507 EXPORT_SYMBOL(lfsck_set_speed);
2509 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2512 struct lfsck_instance *lfsck;
2516 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2520 lfsck = lfsck_instance_find(key, true, false);
2521 if (likely(lfsck != NULL)) {
2522 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2523 lfsck_instance_put(&env, lfsck);
2532 EXPORT_SYMBOL(lfsck_get_windows);
2534 int lfsck_set_windows(struct dt_device *key, int val)
2537 struct lfsck_instance *lfsck;
2541 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2545 lfsck = lfsck_instance_find(key, true, false);
2546 if (likely(lfsck != NULL)) {
2547 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2548 CWARN("%s: invalid async windows size that may "
2549 "cause memory issues. The valid range is "
2551 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2553 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2554 mutex_lock(&lfsck->li_mutex);
2555 lfsck->li_bookmark_ram.lb_async_windows = val;
2556 rc = lfsck_bookmark_store(&env, lfsck);
2557 mutex_unlock(&lfsck->li_mutex);
2559 lfsck_instance_put(&env, lfsck);
2568 EXPORT_SYMBOL(lfsck_set_windows);
2570 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2573 struct lfsck_instance *lfsck;
2574 struct lfsck_component *com;
2578 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2582 lfsck = lfsck_instance_find(key, true, false);
2583 if (likely(lfsck != NULL)) {
2584 com = lfsck_component_find(lfsck, type);
2585 if (likely(com != NULL)) {
2586 rc = com->lc_ops->lfsck_dump(&env, com, m);
2587 lfsck_component_put(&env, com);
2592 lfsck_instance_put(&env, lfsck);
2601 EXPORT_SYMBOL(lfsck_dump);
2603 static int lfsck_stop_all(const struct lu_env *env,
2604 struct lfsck_instance *lfsck,
2605 struct lfsck_stop *stop)
2607 struct lfsck_thread_info *info = lfsck_env_info(env);
2608 struct lfsck_request *lr = &info->lti_lr;
2609 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2610 struct ptlrpc_request_set *set;
2611 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2612 struct lfsck_tgt_desc *ltd;
2613 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2619 LASSERT(stop->ls_flags & LPF_BROADCAST);
2621 set = ptlrpc_prep_set();
2622 if (unlikely(set == NULL))
2625 memset(lr, 0, sizeof(*lr));
2626 lr->lr_event = LE_STOP;
2627 lr->lr_index = lfsck_dev_idx(lfsck);
2628 lr->lr_status = stop->ls_status;
2629 lr->lr_version = bk->lb_version;
2630 lr->lr_active = LFSCK_TYPES_ALL;
2631 lr->lr_param = stop->ls_flags;
2633 laia->laia_com = NULL;
2634 laia->laia_ltds = ltds;
2636 laia->laia_result = 0;
2637 laia->laia_shared = 1;
2639 down_read(<ds->ltd_rw_sem);
2640 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2641 ltd = lfsck_tgt_get(ltds, idx);
2642 LASSERT(ltd != NULL);
2644 laia->laia_ltd = ltd;
2645 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2646 lfsck_async_interpret, laia,
2649 lfsck_interpret(env, lfsck, NULL, laia, rc);
2651 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2652 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2656 up_read(<ds->ltd_rw_sem);
2658 rc = ptlrpc_set_wait(set);
2659 ptlrpc_set_destroy(set);
2662 rc = laia->laia_result;
2664 if (rc == -EALREADY)
2668 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2669 lfsck_lfsck2name(lfsck), rc);
2671 RETURN(rc != 0 ? rc : rc1);
2674 static int lfsck_start_all(const struct lu_env *env,
2675 struct lfsck_instance *lfsck,
2676 struct lfsck_start *start)
2678 struct lfsck_thread_info *info = lfsck_env_info(env);
2679 struct lfsck_request *lr = &info->lti_lr;
2680 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2681 struct ptlrpc_request_set *set;
2682 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2683 struct lfsck_tgt_desc *ltd;
2684 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2689 LASSERT(start->ls_flags & LPF_BROADCAST);
2691 set = ptlrpc_prep_set();
2692 if (unlikely(set == NULL))
2695 memset(lr, 0, sizeof(*lr));
2696 lr->lr_event = LE_START;
2697 lr->lr_index = lfsck_dev_idx(lfsck);
2698 lr->lr_speed = bk->lb_speed_limit;
2699 lr->lr_version = bk->lb_version;
2700 lr->lr_active = start->ls_active;
2701 lr->lr_param = start->ls_flags;
2702 lr->lr_async_windows = bk->lb_async_windows;
2703 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2704 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2707 laia->laia_com = NULL;
2708 laia->laia_ltds = ltds;
2710 laia->laia_result = 0;
2711 laia->laia_shared = 1;
2713 down_read(<ds->ltd_rw_sem);
2714 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2715 ltd = lfsck_tgt_get(ltds, idx);
2716 LASSERT(ltd != NULL);
2718 laia->laia_ltd = ltd;
2719 ltd->ltd_layout_done = 0;
2720 ltd->ltd_namespace_done = 0;
2721 ltd->ltd_synced_failures = 0;
2722 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2723 lfsck_async_interpret, laia,
2726 lfsck_interpret(env, lfsck, NULL, laia, rc);
2728 CERROR("%s: cannot notify MDT %x for LFSCK "
2729 "start, failout: rc = %d\n",
2730 lfsck_lfsck2name(lfsck), idx, rc);
2734 up_read(<ds->ltd_rw_sem);
2737 ptlrpc_set_destroy(set);
2742 rc = ptlrpc_set_wait(set);
2743 ptlrpc_set_destroy(set);
2746 rc = laia->laia_result;
2749 struct lfsck_stop *stop = &info->lti_stop;
2751 CERROR("%s: cannot start LFSCK on some MDTs, "
2752 "stop all: rc = %d\n",
2753 lfsck_lfsck2name(lfsck), rc);
2754 if (rc != -EALREADY) {
2755 stop->ls_status = LS_FAILED;
2756 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2757 lfsck_stop_all(env, lfsck, stop);
2764 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2765 struct lfsck_start_param *lsp)
2767 struct lfsck_start *start = lsp->lsp_start;
2768 struct lfsck_instance *lfsck;
2769 struct lfsck_bookmark *bk;
2770 struct ptlrpc_thread *thread;
2771 struct lfsck_component *com;
2772 struct l_wait_info lwi = { 0 };
2773 struct lfsck_thread_args *lta;
2774 struct task_struct *task;
2781 lfsck = lfsck_instance_find(key, true, false);
2782 if (unlikely(lfsck == NULL))
2785 /* System is not ready, try again later. */
2786 if (unlikely(lfsck->li_namespace == NULL))
2787 GOTO(put, rc = -EAGAIN);
2789 /* start == NULL means auto trigger paused LFSCK. */
2790 if ((start == NULL) &&
2791 (list_empty(&lfsck->li_list_scan) ||
2792 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2795 bk = &lfsck->li_bookmark_ram;
2796 thread = &lfsck->li_thread;
2797 mutex_lock(&lfsck->li_mutex);
2798 spin_lock(&lfsck->li_lock);
2799 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2801 if (unlikely(start == NULL)) {
2802 spin_unlock(&lfsck->li_lock);
2806 while (start->ls_active != 0) {
2807 if (!(type & start->ls_active)) {
2812 com = __lfsck_component_find(lfsck, type,
2813 &lfsck->li_list_scan);
2815 com = __lfsck_component_find(lfsck, type,
2816 &lfsck->li_list_double_scan);
2822 if (com->lc_ops->lfsck_join != NULL) {
2823 rc = com->lc_ops->lfsck_join( env, com, lsp);
2824 if (rc != 0 && rc != -EALREADY)
2827 start->ls_active &= ~type;
2830 spin_unlock(&lfsck->li_lock);
2833 spin_unlock(&lfsck->li_lock);
2835 lfsck->li_status = 0;
2836 lfsck->li_oit_over = 0;
2837 lfsck->li_start_unplug = 0;
2838 lfsck->li_drop_dryrun = 0;
2839 lfsck->li_new_scanned = 0;
2841 /* For auto trigger. */
2845 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2846 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2847 lfsck_lfsck2name(lfsck));
2849 GOTO(out, rc = -EPERM);
2852 start->ls_version = bk->lb_version;
2854 if (start->ls_active != 0) {
2855 struct lfsck_component *next;
2857 if (start->ls_active == LFSCK_TYPES_ALL)
2858 start->ls_active = LFSCK_TYPES_SUPPORTED;
2860 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2861 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2862 GOTO(out, rc = -ENOTSUPP);
2865 list_for_each_entry_safe(com, next,
2866 &lfsck->li_list_scan, lc_link) {
2867 if (!(com->lc_type & start->ls_active)) {
2868 rc = com->lc_ops->lfsck_post(env, com, 0,
2875 while (start->ls_active != 0) {
2876 if (type & start->ls_active) {
2877 com = __lfsck_component_find(lfsck, type,
2878 &lfsck->li_list_idle);
2880 /* The component status will be updated
2881 * when its prep() is called later by
2882 * the LFSCK main engine. */
2883 list_move_tail(&com->lc_link,
2884 &lfsck->li_list_scan);
2885 start->ls_active &= ~type;
2891 if (list_empty(&lfsck->li_list_scan)) {
2892 /* The speed limit will be used to control both the LFSCK and
2893 * low layer scrub (if applied), need to be handled firstly. */
2894 if (start->ls_valid & LSV_SPEED_LIMIT) {
2895 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2896 rc = lfsck_bookmark_store(env, lfsck);
2905 if (start->ls_flags & LPF_RESET)
2906 flags |= DOIF_RESET;
2908 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2912 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2913 start->ls_active |= com->lc_type;
2914 if (flags & DOIF_RESET) {
2915 rc = com->lc_ops->lfsck_reset(env, com, false);
2922 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2923 if (bk->lb_param & LPF_DRYRUN)
2924 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2926 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2927 valid |= DOIV_ERROR_HANDLE;
2928 if (start->ls_flags & LPF_FAILOUT)
2929 flags |= DOIF_FAILOUT;
2932 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2933 valid |= DOIV_DRYRUN;
2934 if (start->ls_flags & LPF_DRYRUN)
2935 flags |= DOIF_DRYRUN;
2938 if (!list_empty(&lfsck->li_list_scan))
2939 flags |= DOIF_OUTUSED;
2941 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2942 thread_set_flags(thread, 0);
2943 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2945 GOTO(out, rc = PTR_ERR(lta));
2947 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2948 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2951 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2952 lfsck_lfsck2name(lfsck), rc);
2953 lfsck_thread_args_fini(lta);
2958 l_wait_event(thread->t_ctl_waitq,
2959 thread_is_running(thread) ||
2960 thread_is_stopped(thread),
2962 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2963 lfsck->li_start_unplug = 1;
2964 wake_up_all(&thread->t_ctl_waitq);
2969 /* release lfsck::li_mutex to avoid deadlock. */
2970 mutex_unlock(&lfsck->li_mutex);
2971 rc = lfsck_start_all(env, lfsck, start);
2973 spin_lock(&lfsck->li_lock);
2974 if (thread_is_stopped(thread)) {
2975 spin_unlock(&lfsck->li_lock);
2977 lfsck->li_status = LS_FAILED;
2978 lfsck->li_flags = 0;
2979 thread_set_flags(thread, SVC_STOPPING);
2980 spin_unlock(&lfsck->li_lock);
2982 lfsck->li_start_unplug = 1;
2983 wake_up_all(&thread->t_ctl_waitq);
2984 l_wait_event(thread->t_ctl_waitq,
2985 thread_is_stopped(thread),
2989 lfsck->li_start_unplug = 1;
2990 wake_up_all(&thread->t_ctl_waitq);
2996 mutex_unlock(&lfsck->li_mutex);
2999 lfsck_instance_put(env, lfsck);
3001 return rc < 0 ? rc : 0;
3003 EXPORT_SYMBOL(lfsck_start);
3005 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3006 struct lfsck_stop *stop)
3008 struct lfsck_instance *lfsck;
3009 struct ptlrpc_thread *thread;
3010 struct l_wait_info lwi = { 0 };
3015 lfsck = lfsck_instance_find(key, true, false);
3016 if (unlikely(lfsck == NULL))
3019 thread = &lfsck->li_thread;
3020 /* release lfsck::li_mutex to avoid deadlock. */
3021 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3022 if (!lfsck->li_master) {
3023 CERROR("%s: only allow to specify '-A' via MDS\n",
3024 lfsck_lfsck2name(lfsck));
3026 GOTO(out, rc = -EPERM);
3029 rc1 = lfsck_stop_all(env, lfsck, stop);
3032 mutex_lock(&lfsck->li_mutex);
3033 spin_lock(&lfsck->li_lock);
3034 /* no error if LFSCK is already stopped, or was never started */
3035 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3036 spin_unlock(&lfsck->li_lock);
3041 lfsck->li_status = stop->ls_status;
3042 lfsck->li_flags = stop->ls_flags;
3044 lfsck->li_status = LS_STOPPED;
3045 lfsck->li_flags = 0;
3048 thread_set_flags(thread, SVC_STOPPING);
3049 spin_unlock(&lfsck->li_lock);
3051 wake_up_all(&thread->t_ctl_waitq);
3052 l_wait_event(thread->t_ctl_waitq,
3053 thread_is_stopped(thread),
3059 mutex_unlock(&lfsck->li_mutex);
3060 lfsck_instance_put(env, lfsck);
3062 return rc != 0 ? rc : rc1;
3064 EXPORT_SYMBOL(lfsck_stop);
3066 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3067 struct lfsck_request *lr, struct thandle *th)
3069 int rc = -EOPNOTSUPP;
3072 switch (lr->lr_event) {
3074 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3075 struct lfsck_start_param lsp;
3077 memset(start, 0, sizeof(*start));
3078 start->ls_valid = lr->lr_valid;
3079 start->ls_speed_limit = lr->lr_speed;
3080 start->ls_version = lr->lr_version;
3081 start->ls_active = lr->lr_active;
3082 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3083 start->ls_async_windows = lr->lr_async_windows;
3085 lsp.lsp_start = start;
3086 lsp.lsp_index = lr->lr_index;
3087 lsp.lsp_index_valid = 1;
3088 rc = lfsck_start(env, key, &lsp);
3092 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3094 memset(stop, 0, sizeof(*stop));
3095 stop->ls_status = lr->lr_status;
3096 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3097 rc = lfsck_stop(env, key, stop);
3100 case LE_PHASE1_DONE:
3101 case LE_PHASE2_DONE:
3102 case LE_FID_ACCESSED:
3104 case LE_CONDITIONAL_DESTROY:
3105 case LE_SKIP_NLINK_DECLARE:
3107 case LE_SET_LMV_MASTER:
3108 case LE_SET_LMV_SLAVE:
3109 case LE_PAIRS_VERIFY: {
3110 struct lfsck_instance *lfsck;
3111 struct lfsck_component *com;
3113 lfsck = lfsck_instance_find(key, true, false);
3114 if (unlikely(lfsck == NULL))
3117 com = lfsck_component_find(lfsck, lr->lr_active);
3118 if (likely(com != NULL)) {
3119 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3120 lfsck_component_put(env, com);
3123 lfsck_instance_put(env, lfsck);
3132 EXPORT_SYMBOL(lfsck_in_notify);
3134 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3135 struct lfsck_request *lr)
3137 struct lfsck_instance *lfsck;
3138 struct lfsck_component *com;
3142 lfsck = lfsck_instance_find(key, true, false);
3143 if (unlikely(lfsck == NULL))
3146 com = lfsck_component_find(lfsck, lr->lr_active);
3147 if (likely(com != NULL)) {
3148 rc = com->lc_ops->lfsck_query(env, com);
3149 lfsck_component_put(env, com);
3154 lfsck_instance_put(env, lfsck);
3159 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3160 struct ldlm_namespace *ns)
3162 struct lfsck_instance *lfsck;
3165 lfsck = lfsck_instance_find(key, true, false);
3166 if (likely(lfsck != NULL)) {
3167 lfsck->li_namespace = ns;
3168 lfsck_instance_put(env, lfsck);
3174 EXPORT_SYMBOL(lfsck_register_namespace);
3176 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3177 struct dt_device *next, struct obd_device *obd,
3178 lfsck_out_notify notify, void *notify_data, bool master)
3180 struct lfsck_instance *lfsck;
3181 struct dt_object *root = NULL;
3182 struct dt_object *obj = NULL;
3183 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3187 lfsck = lfsck_instance_find(key, false, false);
3188 if (unlikely(lfsck != NULL))
3191 OBD_ALLOC_PTR(lfsck);
3195 mutex_init(&lfsck->li_mutex);
3196 spin_lock_init(&lfsck->li_lock);
3197 INIT_LIST_HEAD(&lfsck->li_link);
3198 INIT_LIST_HEAD(&lfsck->li_list_scan);
3199 INIT_LIST_HEAD(&lfsck->li_list_dir);
3200 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3201 INIT_LIST_HEAD(&lfsck->li_list_idle);
3202 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3203 atomic_set(&lfsck->li_ref, 1);
3204 atomic_set(&lfsck->li_double_scan_count, 0);
3205 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3206 lfsck->li_out_notify = notify;
3207 lfsck->li_out_notify_data = notify_data;
3208 lfsck->li_next = next;
3209 lfsck->li_bottom = key;
3210 lfsck->li_obd = obd;
3212 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3216 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3220 fid->f_seq = FID_SEQ_LOCAL_NAME;
3223 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3227 rc = dt_root_get(env, key, fid);
3231 root = dt_locate(env, key, fid);
3233 GOTO(out, rc = PTR_ERR(root));
3235 if (unlikely(!dt_try_as_dir(env, root)))
3236 GOTO(out, rc = -ENOTDIR);
3238 lfsck->li_local_root_fid = *fid;
3240 lfsck->li_master = 1;
3241 if (lfsck_dev_idx(lfsck) == 0) {
3242 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3243 const struct lu_name *cname;
3245 rc = dt_lookup(env, root,
3246 (struct dt_rec *)(&lfsck->li_global_root_fid),
3247 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3251 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3253 GOTO(out, rc = PTR_ERR(obj));
3255 if (unlikely(!dt_try_as_dir(env, obj)))
3256 GOTO(out, rc = -ENOTDIR);
3258 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3259 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3263 lfsck_object_put(env, obj);
3264 obj = dt_locate(env, key, fid);
3266 GOTO(out, rc = PTR_ERR(obj));
3268 cname = lfsck_name_get_const(env, dotlustre,
3270 rc = lfsck_verify_linkea(env, obj, cname,
3271 &lfsck->li_global_root_fid);
3275 if (unlikely(!dt_try_as_dir(env, obj)))
3276 GOTO(out, rc = -ENOTDIR);
3279 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3280 (const struct dt_key *)lostfound,
3285 lfsck_object_put(env, obj);
3286 obj = dt_locate(env, key, fid);
3288 GOTO(out, rc = PTR_ERR(obj));
3290 cname = lfsck_name_get_const(env, lostfound,
3292 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3296 lfsck_object_put(env, obj);
3301 fid->f_seq = FID_SEQ_LOCAL_FILE;
3302 fid->f_oid = OTABLE_IT_OID;
3304 obj = dt_locate(env, key, fid);
3306 GOTO(out, rc = PTR_ERR(obj));
3308 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3312 lfsck->li_obj_oit = obj;
3313 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3314 S_IFDIR | S_IRUGO | S_IWUSR);
3316 GOTO(out, rc = PTR_ERR(obj));
3318 lu_object_get(&obj->do_lu);
3319 lfsck->li_lfsck_dir = obj;
3320 rc = lfsck_bookmark_setup(env, lfsck);
3325 rc = lfsck_fid_init(lfsck);
3329 rc = lfsck_namespace_setup(env, lfsck);
3334 rc = lfsck_layout_setup(env, lfsck);
3338 /* XXX: more LFSCK components initialization to be added here. */
3340 rc = lfsck_instance_add(lfsck);
3342 rc = lfsck_add_target_from_orphan(env, lfsck);
3344 if (obj != NULL && !IS_ERR(obj))
3345 lfsck_object_put(env, obj);
3346 if (root != NULL && !IS_ERR(root))
3347 lfsck_object_put(env, root);
3349 lfsck_instance_cleanup(env, lfsck);
3352 EXPORT_SYMBOL(lfsck_register);
3354 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3356 struct lfsck_instance *lfsck;
3358 lfsck = lfsck_instance_find(key, false, true);
3360 lfsck_instance_put(env, lfsck);
3362 EXPORT_SYMBOL(lfsck_degister);
3364 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3365 struct dt_device *tgt, struct obd_export *exp,
3366 __u32 index, bool for_ost)
3368 struct lfsck_instance *lfsck;
3369 struct lfsck_tgt_desc *ltd;
3380 INIT_LIST_HEAD(<d->ltd_orphan_list);
3381 INIT_LIST_HEAD(<d->ltd_layout_list);
3382 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3383 INIT_LIST_HEAD(<d->ltd_namespace_list);
3384 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3385 atomic_set(<d->ltd_ref, 1);
3386 ltd->ltd_index = index;
3388 spin_lock(&lfsck_instance_lock);
3389 lfsck = __lfsck_instance_find(key, true, false);
3390 if (lfsck == NULL) {
3392 list_add_tail(<d->ltd_orphan_list,
3393 &lfsck_ost_orphan_list);
3395 list_add_tail(<d->ltd_orphan_list,
3396 &lfsck_mdt_orphan_list);
3397 spin_unlock(&lfsck_instance_lock);
3401 spin_unlock(&lfsck_instance_lock);
3403 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3407 lfsck_instance_put(env, lfsck);
3411 EXPORT_SYMBOL(lfsck_add_target);
3413 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3414 struct dt_device *tgt, __u32 index, bool for_ost)
3416 struct lfsck_instance *lfsck;
3417 struct lfsck_tgt_descs *ltds;
3418 struct lfsck_tgt_desc *ltd;
3419 struct list_head *head;
3422 head = &lfsck_ost_orphan_list;
3424 head = &lfsck_mdt_orphan_list;
3426 spin_lock(&lfsck_instance_lock);
3427 list_for_each_entry(ltd, head, ltd_orphan_list) {
3428 if (ltd->ltd_tgt == tgt) {
3429 list_del_init(<d->ltd_orphan_list);
3430 spin_unlock(&lfsck_instance_lock);
3438 lfsck = __lfsck_instance_find(key, true, false);
3439 spin_unlock(&lfsck_instance_lock);
3440 if (unlikely(lfsck == NULL))
3444 ltds = &lfsck->li_ost_descs;
3446 ltds = &lfsck->li_mdt_descs;
3448 down_write(<ds->ltd_rw_sem);
3449 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3451 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3454 ltd = LTD_TGT(ltds, index);
3455 if (unlikely(ltd == NULL))
3458 LASSERT(ltds->ltd_tgtnr > 0);
3461 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3462 LTD_TGT(ltds, index) = NULL;
3467 head = &lfsck->li_ost_descs.ltd_orphan;
3469 head = &lfsck->li_mdt_descs.ltd_orphan;
3471 list_for_each_entry(ltd, head, ltd_orphan_list) {
3472 if (ltd->ltd_tgt == tgt) {
3473 list_del_init(<d->ltd_orphan_list);
3479 up_write(<ds->ltd_rw_sem);
3481 spin_lock(<ds->ltd_lock);
3483 spin_unlock(<ds->ltd_lock);
3484 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3485 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3489 lfsck_instance_put(env, lfsck);
3491 EXPORT_SYMBOL(lfsck_del_target);
3493 static int __init lfsck_init(void)
3497 INIT_LIST_HEAD(&lfsck_instance_list);
3498 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3499 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3500 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3501 rc = lu_context_key_register(&lfsck_thread_key);
3503 tgt_register_lfsck_in_notify(lfsck_in_notify);
3504 tgt_register_lfsck_query(lfsck_query);
3510 static void __exit lfsck_exit(void)
3512 struct lfsck_tgt_desc *ltd;
3513 struct lfsck_tgt_desc *next;
3515 LASSERT(list_empty(&lfsck_instance_list));
3517 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3519 list_del_init(<d->ltd_orphan_list);
3523 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3525 list_del_init(<d->ltd_orphan_list);
3529 lu_context_key_degister(&lfsck_thread_key);
3532 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3533 MODULE_DESCRIPTION("LFSCK");
3534 MODULE_LICENSE("GPL");
3536 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);