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);
605 dt_write_lock(env, parent, 0);
606 rc = dt_ref_del(env, parent, th);
607 dt_write_unlock(env, parent);
612 dt_trans_stop(env, dev, th);
617 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
618 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
623 static int lfsck_create_lpf_local(const struct lu_env *env,
624 struct lfsck_instance *lfsck,
625 struct dt_object *child,
627 struct dt_object_format *dof,
630 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
631 struct dt_object *parent = lfsck->li_lpf_root_obj;
632 struct dt_device *dev = lfsck_obj2dev(child);
633 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
634 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
635 const struct lu_fid *cfid = lfsck_dto2fid(child);
636 struct thandle *th = NULL;
637 struct linkea_data ldata = { NULL };
638 struct lu_buf linkea_buf;
639 const struct lu_name *cname;
641 int len = sizeof(struct lfsck_bookmark);
645 rc = linkea_data_new(&ldata,
646 &lfsck_env_info(env)->lti_linkea_buf2);
650 cname = lfsck_name_get_const(env, name, strlen(name));
651 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
655 th = dt_trans_create(env, dev);
659 /* 1a. create child */
660 rc = dt_declare_create(env, child, la, NULL, dof, th);
664 /* 2a. increase child nlink */
665 rc = dt_declare_ref_add(env, child, th);
669 /* 3a. insert linkEA for child */
670 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
671 ldata.ld_leh->leh_len);
672 rc = dt_declare_xattr_set(env, child, &linkea_buf,
673 XATTR_NAME_LINK, 0, th);
677 /* 4a. insert name into parent dir */
678 rec->rec_type = S_IFDIR;
680 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
681 (const struct dt_key *)name, th);
685 /* 5a. increase parent nlink */
686 rc = dt_declare_ref_add(env, parent, th);
690 /* 6a. update bookmark */
691 rc = dt_declare_record_write(env, bk_obj,
692 lfsck_buf_get(env, bk, len), 0, th);
696 rc = dt_trans_start_local(env, dev, th);
700 dt_write_lock(env, child, 0);
701 /* 1b.1. create child */
702 rc = dt_create(env, child, la, NULL, dof, th);
706 if (unlikely(!dt_try_as_dir(env, child)))
707 GOTO(unlock, rc = -ENOTDIR);
709 /* 1b.2. insert dot into child dir */
711 rc = dt_insert(env, child, (const struct dt_rec *)rec,
712 (const struct dt_key *)dot, th, 1);
716 /* 1b.3. insert dotdot into child dir */
717 rec->rec_fid = &LU_LPF_FID;
718 rc = dt_insert(env, child, (const struct dt_rec *)rec,
719 (const struct dt_key *)dotdot, th, 1);
723 /* 2b. increase child nlink */
724 rc = dt_ref_add(env, child, th);
728 /* 3b. insert linkEA for child. */
729 rc = dt_xattr_set(env, child, &linkea_buf,
730 XATTR_NAME_LINK, 0, th);
731 dt_write_unlock(env, child);
735 /* 4b. insert name into parent dir */
737 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
738 (const struct dt_key *)name, th, 1);
742 dt_write_lock(env, parent, 0);
743 /* 5b. increase parent nlink */
744 rc = dt_ref_add(env, parent, th);
745 dt_write_unlock(env, parent);
749 bk->lb_lpf_fid = *cfid;
750 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
752 /* 6b. update bookmark */
753 rc = dt_record_write(env, bk_obj,
754 lfsck_buf_get(env, bk, len), &pos, th);
759 dt_write_unlock(env, child);
762 dt_trans_stop(env, dev, th);
767 static int lfsck_create_lpf_remote(const struct lu_env *env,
768 struct lfsck_instance *lfsck,
769 struct dt_object *child,
771 struct dt_object_format *dof,
774 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
775 struct dt_object *parent = lfsck->li_lpf_root_obj;
776 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
777 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
778 const struct lu_fid *cfid = lfsck_dto2fid(child);
779 struct thandle *th = NULL;
780 struct linkea_data ldata = { NULL };
781 struct lu_buf linkea_buf;
782 const struct lu_name *cname;
783 struct dt_device *dev;
785 int len = sizeof(struct lfsck_bookmark);
789 rc = linkea_data_new(&ldata,
790 &lfsck_env_info(env)->lti_linkea_buf2);
794 cname = lfsck_name_get_const(env, name, strlen(name));
795 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
799 /* Create .lustre/lost+found/MDTxxxx. */
801 /* XXX: Currently, cross-MDT create operation needs to create the child
802 * object firstly, then insert name into the parent directory. For
803 * this case, the child object resides on current MDT (local), but
804 * the parent ".lustre/lost+found" may be on remote MDT. It is not
805 * easy to contain all the sub-modifications orderly within single
808 * To avoid more inconsistency, we split the create operation into
811 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
813 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
816 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
817 * repair such inconsistency when LFSCK run next time. */
819 /* Transaction I: locally */
821 dev = lfsck_obj2dev(child);
822 th = dt_trans_create(env, dev);
826 /* 1a. create child */
827 rc = dt_declare_create(env, child, la, NULL, dof, th);
831 /* 2a. increase child nlink */
832 rc = dt_declare_ref_add(env, child, th);
836 /* 3a. insert linkEA for child */
837 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
838 ldata.ld_leh->leh_len);
839 rc = dt_declare_xattr_set(env, child, &linkea_buf,
840 XATTR_NAME_LINK, 0, th);
844 /* 4a. update bookmark */
845 rc = dt_declare_record_write(env, bk_obj,
846 lfsck_buf_get(env, bk, len), 0, th);
850 rc = dt_trans_start_local(env, dev, th);
854 dt_write_lock(env, child, 0);
855 /* 1b.1. create child */
856 rc = dt_create(env, child, la, NULL, dof, th);
860 if (unlikely(!dt_try_as_dir(env, child)))
861 GOTO(unlock, rc = -ENOTDIR);
863 /* 1b.2. insert dot into child dir */
864 rec->rec_type = S_IFDIR;
866 rc = dt_insert(env, child, (const struct dt_rec *)rec,
867 (const struct dt_key *)dot, th, 1);
871 /* 1b.3. insert dotdot into child dir */
872 rec->rec_fid = &LU_LPF_FID;
873 rc = dt_insert(env, child, (const struct dt_rec *)rec,
874 (const struct dt_key *)dotdot, th, 1);
878 /* 2b. increase child nlink */
879 rc = dt_ref_add(env, child, th);
883 /* 3b. insert linkEA for child */
884 rc = dt_xattr_set(env, child, &linkea_buf,
885 XATTR_NAME_LINK, 0, th);
889 bk->lb_lpf_fid = *cfid;
890 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
892 /* 4b. update bookmark */
893 rc = dt_record_write(env, bk_obj,
894 lfsck_buf_get(env, bk, len), &pos, th);
896 dt_write_unlock(env, child);
897 dt_trans_stop(env, dev, th);
901 /* Transaction II: remotely */
903 dev = lfsck_obj2dev(parent);
904 th = dt_trans_create(env, dev);
909 /* 5a. insert name into parent dir */
911 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
912 (const struct dt_key *)name, th);
916 /* 6a. increase parent nlink */
917 rc = dt_declare_ref_add(env, parent, th);
921 rc = dt_trans_start_local(env, dev, th);
925 /* 5b. insert name into parent dir */
926 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
927 (const struct dt_key *)name, th, 1);
931 dt_write_lock(env, parent, 0);
932 /* 6b. increase parent nlink */
933 rc = dt_ref_add(env, parent, th);
934 dt_write_unlock(env, parent);
939 dt_write_unlock(env, child);
941 dt_trans_stop(env, dev, th);
943 if (rc != 0 && dev == lfsck_obj2dev(parent))
944 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
945 "for orphans, but failed to insert the name %s "
946 "to the .lustre/lost+found/. Such inconsistency "
947 "will be repaired when LFSCK run next time: rc = %d\n",
948 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
954 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
956 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
957 * orphans and other uncertain inconsistent objects found during the
958 * LFSCK. Such directory will be created by the LFSCK engine on the
959 * local MDT before the LFSCK scanning.
961 * \param[in] env pointer to the thread context
962 * \param[in] lfsck pointer to the lfsck instance
964 * \retval 0 for success
965 * \retval negative error number on failure
967 static int lfsck_create_lpf(const struct lu_env *env,
968 struct lfsck_instance *lfsck)
970 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
971 struct lfsck_thread_info *info = lfsck_env_info(env);
972 struct lu_fid *cfid = &info->lti_fid2;
973 struct lu_attr *la = &info->lti_la;
974 struct dt_object_format *dof = &info->lti_dof;
975 struct dt_object *parent = lfsck->li_lpf_root_obj;
976 struct dt_object *child = NULL;
977 struct lfsck_lock_handle *llh = &info->lti_llh;
979 int node = lfsck_dev_idx(lfsck);
983 LASSERT(lfsck->li_master);
984 LASSERT(parent != NULL);
985 LASSERT(lfsck->li_lpf_obj == NULL);
987 rc = lfsck_lock(env, lfsck, parent, name, llh,
988 MDS_INODELOCK_UPDATE, LCK_PW);
992 snprintf(name, 8, "MDT%04x", node);
993 if (fid_is_zero(&bk->lb_lpf_fid)) {
994 /* There is corner case that: in former LFSCK scanning we have
995 * created the .lustre/lost+found/MDTxxxx but failed to update
996 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
997 * it from MDT0 firstly. */
998 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
999 (const struct dt_key *)name);
1000 if (rc != 0 && rc != -ENOENT)
1004 bk->lb_lpf_fid = *cfid;
1005 rc = lfsck_bookmark_store(env, lfsck);
1007 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1012 *cfid = bk->lb_lpf_fid;
1015 child = lfsck_object_find_bottom(env, lfsck, cfid);
1017 GOTO(unlock, rc = PTR_ERR(child));
1019 if (dt_object_exists(child) != 0) {
1020 if (unlikely(!dt_try_as_dir(env, child)))
1023 lfsck->li_lpf_obj = child;
1028 memset(la, 0, sizeof(*la));
1029 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1030 la->la_mode = S_IFDIR | S_IRWXU;
1031 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1033 memset(dof, 0, sizeof(*dof));
1034 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1037 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1039 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1041 lfsck->li_lpf_obj = child;
1047 if (rc != 0 && child != NULL && !IS_ERR(child))
1048 lfsck_object_put(env, child);
1054 * Scan .lustre/lost+found for bad name entries and remove them.
1056 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1057 * index in the system. Any other formatted name is invalid and should be
1060 * \param[in] env pointer to the thread context
1061 * \param[in] lfsck pointer to the lfsck instance
1063 * \retval 0 for success
1064 * \retval negative error number on failure
1066 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1067 struct lfsck_instance *lfsck)
1069 struct dt_object *parent = lfsck->li_lpf_root_obj;
1070 struct lu_dirent *ent =
1071 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1072 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1077 it = iops->init(env, parent, LUDA_64BITHASH);
1079 RETURN(PTR_ERR(it));
1081 rc = iops->load(env, it, 0);
1083 rc = iops->next(env, it);
1090 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1094 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1095 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1098 /* name length must be strlen("MDTxxxx") */
1099 if (ent->lde_namelen != 7)
1102 if (memcmp(ent->lde_name, "MDT", off) != 0)
1105 while (off < 7 && isxdigit(ent->lde_name[off]))
1111 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1118 rc = iops->next(env, it);
1122 iops->fini(env, it);
1124 RETURN(rc > 0 ? 0 : rc);
1127 static int lfsck_update_lpf_entry(const struct lu_env *env,
1128 struct lfsck_instance *lfsck,
1129 struct dt_object *parent,
1130 struct dt_object *child,
1132 enum lfsck_verify_lpf_types type)
1136 if (type == LVLT_BY_BOOKMARK) {
1137 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1138 lfsck_dto2fid(child), S_IFDIR);
1139 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1140 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1141 rc = lfsck_bookmark_store(env, lfsck);
1143 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1144 " in the bookmark file: rc = %d\n",
1145 lfsck_lfsck2name(lfsck),
1146 PFID(lfsck_dto2fid(child)), rc);
1153 * Check whether the @child back references the @parent.
1156 * 1) The child's FID is stored in the bookmark file. If the child back
1157 * references the parent (LU_LPF_FID object) via its ".." entry, then
1158 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1159 * the child back references another parent2, then:
1160 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1161 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1162 * references the child. So keep them there. As the LFSCK processing,
1163 * the parent3 may be found, then when the LFSCK run next time, the
1164 * inconsistency can be repaired.
1166 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1167 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1168 * via its ".." entry, then update the bookmark file, otherwise, if the child
1169 * back references another parent2, then:
1170 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1171 * from .lustre/lost+found/;
1172 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1173 * sub-directory name entry and update the child;
1174 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1175 * or not, then keep them there.
1177 * \param[in] env pointer to the thread context
1178 * \param[in] lfsck pointer to the lfsck instance
1179 * \param[in] child pointer to the lost+found sub-directory object
1180 * \param[in] name the name for lost+found sub-directory object
1181 * \param[out] fid pointer to the buffer to hold the FID of the object
1182 * (called it as parent2) that is referenced via the
1183 * child's dotdot entry; it also can be the FID that
1184 * is referenced by the name entry under the parent2.
1185 * \param[in] type to indicate where the child's FID is stored in
1187 * \retval positive number for uncertain inconsistency
1188 * \retval 0 for success
1189 * \retval negative error number on failure
1191 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1192 struct lfsck_instance *lfsck,
1193 struct dt_object *child, const char *name,
1195 enum lfsck_verify_lpf_types type)
1197 struct dt_object *parent = lfsck->li_lpf_root_obj;
1198 struct lfsck_thread_info *info = lfsck_env_info(env);
1199 char *name2 = info->lti_key;
1200 struct lu_fid *fid2 = &info->lti_fid3;
1201 struct dt_object *parent2 = NULL;
1202 struct lustre_handle lh = { 0 };
1207 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1208 (const struct dt_key *)dotdot);
1212 if (!fid_is_sane(fid))
1213 GOTO(linkea, rc = -EINVAL);
1215 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1216 const struct lu_name *cname;
1218 if (lfsck->li_lpf_obj == NULL) {
1219 lu_object_get(&child->do_lu);
1220 lfsck->li_lpf_obj = child;
1223 cname = lfsck_name_get_const(env, name, strlen(name));
1224 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1226 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1232 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1233 if (IS_ERR(parent2))
1234 GOTO(linkea, parent2);
1236 if (!dt_object_exists(parent2)) {
1237 lfsck_object_put(env, parent2);
1239 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1242 if (!dt_try_as_dir(env, parent2)) {
1243 lfsck_object_put(env, parent2);
1245 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1249 /* To prevent rename/unlink race */
1250 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1251 MDS_INODELOCK_UPDATE, LCK_PR);
1255 dt_read_lock(env, child, 0);
1256 rc = lfsck_links_get_first(env, child, name2, fid2);
1258 dt_read_unlock(env, child);
1259 lfsck_ibits_unlock(&lh, LCK_PR);
1261 GOTO(out_put, rc = 1);
1264 /* It is almost impossible that the bookmark file (or the name entry)
1265 * and the linkEA hit the same data corruption. Trust the linkEA. */
1266 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1267 dt_read_unlock(env, child);
1268 lfsck_ibits_unlock(&lh, LCK_PR);
1271 if (lfsck->li_lpf_obj == NULL) {
1272 lu_object_get(&child->do_lu);
1273 lfsck->li_lpf_obj = child;
1276 /* Update the child's dotdot entry */
1277 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1278 &LU_LPF_FID, S_IFDIR);
1280 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1286 if (parent2 == NULL || IS_ERR(parent2)) {
1287 dt_read_unlock(env, child);
1288 lfsck_ibits_unlock(&lh, LCK_PR);
1290 GOTO(out_done, rc = 1);
1293 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1294 (const struct dt_key *)name2);
1295 dt_read_unlock(env, child);
1296 lfsck_ibits_unlock(&lh, LCK_PR);
1297 if (rc != 0 && rc != -ENOENT)
1300 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1301 if (type == LVLT_BY_BOOKMARK)
1302 GOTO(out_put, rc = 1);
1304 /* Trust the name entry, update the child's dotdot entry. */
1305 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1306 &LU_LPF_FID, S_IFDIR);
1311 if (type == LVLT_BY_BOOKMARK) {
1312 /* Invalid FID record in the bookmark file, reset it. */
1313 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1314 rc = lfsck_bookmark_store(env, lfsck);
1316 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1317 " in the bookmark file: rc = %d\n",
1318 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1319 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1320 /* The name entry is wrong, remove it. */
1321 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1327 if (parent2 != NULL && !IS_ERR(parent2))
1328 lfsck_object_put(env, parent2);
1335 * Verify the /ROOT/.lustre/lost+found/ directory.
1337 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1338 * the LFSCK does not exactly know how to handle, such as orphans. So before
1339 * the LFSCK scanning the system, the consistency of such directory needs to
1340 * be verified firstly to allow the users to use it during the LFSCK.
1342 * \param[in] env pointer to the thread context
1343 * \param[in] lfsck pointer to the lfsck instance
1345 * \retval positive number for uncertain inconsistency
1346 * \retval 0 for success
1347 * \retval negative error number on failure
1349 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1351 struct lfsck_thread_info *info = lfsck_env_info(env);
1352 struct lu_fid *pfid = &info->lti_fid;
1353 struct lu_fid *cfid = &info->lti_fid2;
1354 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1355 struct dt_object *parent;
1356 /* child1's FID is in the bookmark file. */
1357 struct dt_object *child1 = NULL;
1358 /* child2's FID is in the name entry MDTxxxx. */
1359 struct dt_object *child2 = NULL;
1360 const struct lu_name *cname;
1362 int node = lfsck_dev_idx(lfsck);
1366 LASSERT(lfsck->li_master);
1368 if (lfsck->li_lpf_root_obj != NULL)
1372 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1375 struct lfsck_tgt_desc *ltd;
1377 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1378 if (unlikely(ltd == NULL))
1381 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1387 RETURN(PTR_ERR(parent));
1389 LASSERT(dt_object_exists(parent));
1391 if (unlikely(!dt_try_as_dir(env, parent))) {
1392 lfsck_object_put(env, parent);
1394 GOTO(put, rc = -ENOTDIR);
1397 lfsck->li_lpf_root_obj = parent;
1399 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1401 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1402 "for bad sub-directories: rc = %d\n",
1403 lfsck_lfsck2name(lfsck), rc);
1406 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1407 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1408 struct lu_fid tfid = bk->lb_lpf_fid;
1410 /* Invalid FID record in the bookmark file, reset it. */
1411 fid_zero(&bk->lb_lpf_fid);
1412 rc = lfsck_bookmark_store(env, lfsck);
1414 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1415 " in the bookmark file: rc = %d\n",
1416 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1421 child1 = lfsck_object_find_bottom(env, lfsck,
1423 if (IS_ERR(child1)) {
1428 if (unlikely(!dt_object_exists(child1) ||
1429 dt_object_remote(child1)) ||
1430 !S_ISDIR(lfsck_object_type(child1))) {
1431 /* Invalid FID record in the bookmark file,
1433 fid_zero(&bk->lb_lpf_fid);
1434 rc = lfsck_bookmark_store(env, lfsck);
1436 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1437 " in the bookmark file: rc = %d\n",
1438 lfsck_lfsck2name(lfsck),
1439 PFID(lfsck_dto2fid(child1)), rc);
1444 lfsck_object_put(env, child1);
1446 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1447 GOTO(put, rc = -ENOTDIR);
1453 snprintf(name, 8, "MDT%04x", node);
1454 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1455 (const struct dt_key *)name);
1456 if (rc == -ENOENT) {
1457 if (!fid_is_zero(&bk->lb_lpf_fid))
1466 /* Invalid FID in the name entry, remove the name entry. */
1467 if (!fid_is_norm(cfid)) {
1468 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1475 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1477 GOTO(put, rc = PTR_ERR(child2));
1479 if (unlikely(!dt_object_exists(child2) ||
1480 dt_object_remote(child2)) ||
1481 !S_ISDIR(lfsck_object_type(child2))) {
1482 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1489 if (unlikely(!dt_try_as_dir(env, child2)))
1490 GOTO(put, rc = -ENOTDIR);
1492 if (child1 == NULL) {
1493 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1494 pfid, LVLT_BY_NAMEENTRY);
1495 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1496 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1497 pfid, LVLT_BY_BOOKMARK);
1498 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1499 rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1503 if (lfsck->li_lpf_obj == NULL) {
1504 lu_object_get(&child2->do_lu);
1505 lfsck->li_lpf_obj = child2;
1508 cname = lfsck_name_get_const(env, name, strlen(name));
1509 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1516 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1517 pfid, LVLT_BY_BOOKMARK);
1522 if (lfsck->li_lpf_obj != NULL) {
1523 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1524 lfsck_object_put(env, lfsck->li_lpf_obj);
1525 lfsck->li_lpf_obj = NULL;
1528 } else if (rc == 0) {
1529 rc = lfsck_create_lpf(env, lfsck);
1532 if (child2 != NULL && !IS_ERR(child2))
1533 lfsck_object_put(env, child2);
1534 if (child1 != NULL && !IS_ERR(child1))
1535 lfsck_object_put(env, child1);
1540 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1542 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1543 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1548 if (unlikely(ss == NULL))
1551 OBD_ALLOC_PTR(lfsck->li_seq);
1552 if (lfsck->li_seq == NULL)
1555 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1557 GOTO(out, rc = -ENOMEM);
1559 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1560 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1562 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1566 if (fid_is_sane(&bk->lb_last_fid))
1567 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1572 OBD_FREE_PTR(lfsck->li_seq);
1573 lfsck->li_seq = NULL;
1578 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1580 if (lfsck->li_seq != NULL) {
1581 seq_client_fini(lfsck->li_seq);
1582 OBD_FREE_PTR(lfsck->li_seq);
1583 lfsck->li_seq = NULL;
1587 void lfsck_instance_cleanup(const struct lu_env *env,
1588 struct lfsck_instance *lfsck)
1590 struct ptlrpc_thread *thread = &lfsck->li_thread;
1591 struct lfsck_component *com;
1592 struct lfsck_component *next;
1593 struct lfsck_lmv_unit *llu;
1594 struct lfsck_lmv_unit *llu_next;
1595 struct lfsck_lmv *llmv;
1598 LASSERT(list_empty(&lfsck->li_link));
1599 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1601 if (lfsck->li_obj_oit != NULL) {
1602 lfsck_object_put(env, lfsck->li_obj_oit);
1603 lfsck->li_obj_oit = NULL;
1606 LASSERT(lfsck->li_obj_dir == NULL);
1607 LASSERT(lfsck->li_lmv == NULL);
1609 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1610 llmv = &llu->llu_lmv;
1612 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1613 "still in using: %u\n",
1614 atomic_read(&llmv->ll_ref));
1616 lfsck_lmv_put(env, llmv);
1619 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1620 lfsck_component_cleanup(env, com);
1623 LASSERT(list_empty(&lfsck->li_list_dir));
1625 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1627 lfsck_component_cleanup(env, com);
1630 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1631 lfsck_component_cleanup(env, com);
1634 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1635 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1637 if (lfsck->li_lfsck_dir != NULL) {
1638 lfsck_object_put(env, lfsck->li_lfsck_dir);
1639 lfsck->li_lfsck_dir = NULL;
1642 if (lfsck->li_bookmark_obj != NULL) {
1643 lfsck_object_put(env, lfsck->li_bookmark_obj);
1644 lfsck->li_bookmark_obj = NULL;
1647 if (lfsck->li_lpf_obj != NULL) {
1648 lfsck_object_put(env, lfsck->li_lpf_obj);
1649 lfsck->li_lpf_obj = NULL;
1652 if (lfsck->li_lpf_root_obj != NULL) {
1653 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1654 lfsck->li_lpf_root_obj = NULL;
1657 if (lfsck->li_los != NULL) {
1658 local_oid_storage_fini(env, lfsck->li_los);
1659 lfsck->li_los = NULL;
1662 lfsck_fid_fini(lfsck);
1664 OBD_FREE_PTR(lfsck);
1667 static inline struct lfsck_instance *
1668 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1670 struct lfsck_instance *lfsck;
1672 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1673 if (lfsck->li_bottom == key) {
1675 lfsck_instance_get(lfsck);
1677 list_del_init(&lfsck->li_link);
1686 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1689 struct lfsck_instance *lfsck;
1691 spin_lock(&lfsck_instance_lock);
1692 lfsck = __lfsck_instance_find(key, ref, unlink);
1693 spin_unlock(&lfsck_instance_lock);
1698 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1700 struct lfsck_instance *tmp;
1702 spin_lock(&lfsck_instance_lock);
1703 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1704 if (lfsck->li_bottom == tmp->li_bottom) {
1705 spin_unlock(&lfsck_instance_lock);
1710 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1711 spin_unlock(&lfsck_instance_lock);
1715 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1720 bool newline = (bits != 0 ? false : true);
1723 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1727 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1730 if (names[i] != NULL) {
1734 rc = seq_printf(m, "%s%c", names[i],
1735 newline ? '\n' : ',');
1743 rc = seq_printf(m, "\n");
1748 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1753 rc = seq_printf(m, "%s_time: N/A\n", name);
1755 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1760 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1762 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1763 name, cfs_time_current_sec() - time);
1768 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1771 if (fid_is_zero(&pos->lp_dir_parent)) {
1772 if (pos->lp_oit_cookie == 0)
1773 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1775 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1776 prefix, pos->lp_oit_cookie);
1779 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1780 prefix, pos->lp_oit_cookie,
1781 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1784 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1785 struct lfsck_position *pos, bool init)
1787 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1789 if (unlikely(lfsck->li_di_oit == NULL)) {
1790 memset(pos, 0, sizeof(*pos));
1794 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1795 if (!lfsck->li_current_oit_processed && !init)
1796 pos->lp_oit_cookie--;
1798 LASSERT(pos->lp_oit_cookie > 0);
1800 if (lfsck->li_di_dir != NULL) {
1801 struct dt_object *dto = lfsck->li_obj_dir;
1803 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1806 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1807 fid_zero(&pos->lp_dir_parent);
1808 pos->lp_dir_cookie = 0;
1810 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1813 fid_zero(&pos->lp_dir_parent);
1814 pos->lp_dir_cookie = 0;
1818 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1822 if (limit != LFSCK_SPEED_NO_LIMIT) {
1823 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1824 lfsck->li_sleep_rate = limit /
1825 msecs_to_jiffies(MSEC_PER_SEC);
1826 lfsck->li_sleep_jif = 1;
1828 lfsck->li_sleep_rate = 1;
1829 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1833 lfsck->li_sleep_jif = 0;
1834 lfsck->li_sleep_rate = 0;
1837 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1838 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1845 void lfsck_control_speed(struct lfsck_instance *lfsck)
1847 struct ptlrpc_thread *thread = &lfsck->li_thread;
1848 struct l_wait_info lwi;
1850 if (lfsck->li_sleep_jif > 0 &&
1851 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1852 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1853 LWI_ON_SIGNAL_NOOP, NULL);
1855 l_wait_event(thread->t_ctl_waitq,
1856 !thread_is_running(thread),
1858 lfsck->li_new_scanned = 0;
1862 void lfsck_control_speed_by_self(struct lfsck_component *com)
1864 struct lfsck_instance *lfsck = com->lc_lfsck;
1865 struct ptlrpc_thread *thread = &lfsck->li_thread;
1866 struct l_wait_info lwi;
1868 if (lfsck->li_sleep_jif > 0 &&
1869 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1870 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1871 LWI_ON_SIGNAL_NOOP, NULL);
1873 l_wait_event(thread->t_ctl_waitq,
1874 !thread_is_running(thread),
1876 com->lc_new_scanned = 0;
1880 static struct lfsck_thread_args *
1881 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1882 struct lfsck_component *com,
1883 struct lfsck_start_param *lsp)
1885 struct lfsck_thread_args *lta;
1890 return ERR_PTR(-ENOMEM);
1892 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1898 lta->lta_lfsck = lfsck_instance_get(lfsck);
1900 lta->lta_com = lfsck_component_get(com);
1907 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1909 if (lta->lta_com != NULL)
1910 lfsck_component_put(<a->lta_env, lta->lta_com);
1911 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1912 lu_env_fini(<a->lta_env);
1916 struct lfsck_assistant_data *
1917 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1920 struct lfsck_assistant_data *lad;
1924 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1925 if (lad->lad_bitmap == NULL) {
1930 INIT_LIST_HEAD(&lad->lad_req_list);
1931 spin_lock_init(&lad->lad_lock);
1932 INIT_LIST_HEAD(&lad->lad_ost_list);
1933 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1934 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1935 INIT_LIST_HEAD(&lad->lad_mdt_list);
1936 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1937 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1938 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1940 lad->lad_name = name;
1947 * Generic LFSCK asynchronous communication interpretor function.
1948 * The LFSCK RPC reply for both the event notification and status
1949 * querying will be handled here.
1951 * \param[in] env pointer to the thread context
1952 * \param[in] req pointer to the LFSCK request
1953 * \param[in] args pointer to the lfsck_async_interpret_args
1954 * \param[in] rc the result for handling the LFSCK request
1956 * \retval 0 for success
1957 * \retval negative error number on failure
1959 int lfsck_async_interpret_common(const struct lu_env *env,
1960 struct ptlrpc_request *req,
1963 struct lfsck_async_interpret_args *laia = args;
1964 struct lfsck_component *com = laia->laia_com;
1965 struct lfsck_assistant_data *lad = com->lc_data;
1966 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1967 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1968 struct lfsck_request *lr = laia->laia_lr;
1970 LASSERT(com->lc_lfsck->li_master);
1972 switch (lr->lr_event) {
1975 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1977 lfsck_lfsck2name(com->lc_lfsck),
1978 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1979 ltd->ltd_index, lad->lad_name, rc);
1981 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1982 struct lfsck_layout *lo = com->lc_file_ram;
1984 if (lr->lr_flags & LEF_TO_OST)
1985 lfsck_lad_set_bitmap(env, com,
1988 lo->ll_flags |= LF_INCOMPLETE;
1990 struct lfsck_namespace *ns = com->lc_file_ram;
1992 /* If some MDT does not join the namespace
1993 * LFSCK, then we cannot know whether there
1994 * is some name entry on such MDT that with
1995 * the referenced MDT-object on this MDT or
1996 * not. So the namespace LFSCK on this MDT
1997 * cannot handle orphan MDT-objects properly.
1998 * So we mark the LFSCK as LF_INCOMPLETE and
1999 * skip orphan MDT-objects handling. */
2000 ns->ln_flags |= LF_INCOMPLETE;
2005 spin_lock(<ds->ltd_lock);
2006 if (ltd->ltd_dead) {
2007 spin_unlock(<ds->ltd_lock);
2011 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2012 struct list_head *list;
2013 struct list_head *phase_list;
2015 if (ltd->ltd_layout_done) {
2016 spin_unlock(<ds->ltd_lock);
2020 if (lr->lr_flags & LEF_TO_OST) {
2021 list = &lad->lad_ost_list;
2022 phase_list = &lad->lad_ost_phase1_list;
2024 list = &lad->lad_mdt_list;
2025 phase_list = &lad->lad_mdt_phase1_list;
2028 if (list_empty(<d->ltd_layout_list))
2029 list_add_tail(<d->ltd_layout_list, list);
2030 if (list_empty(<d->ltd_layout_phase_list))
2031 list_add_tail(<d->ltd_layout_phase_list,
2034 if (ltd->ltd_namespace_done) {
2035 spin_unlock(<ds->ltd_lock);
2039 if (list_empty(<d->ltd_namespace_list))
2040 list_add_tail(<d->ltd_namespace_list,
2041 &lad->lad_mdt_list);
2042 if (list_empty(<d->ltd_namespace_phase_list))
2043 list_add_tail(<d->ltd_namespace_phase_list,
2044 &lad->lad_mdt_phase1_list);
2046 spin_unlock(<ds->ltd_lock);
2049 case LE_PHASE1_DONE:
2050 case LE_PHASE2_DONE:
2052 if (rc != 0 && rc != -EALREADY)
2053 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2054 "event = %d, rc = %d\n",
2055 lfsck_lfsck2name(com->lc_lfsck),
2056 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2057 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2060 struct lfsck_reply *reply;
2061 struct list_head *list;
2062 struct list_head *phase_list;
2064 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2065 list = <d->ltd_layout_list;
2066 phase_list = <d->ltd_layout_phase_list;
2068 list = <d->ltd_namespace_list;
2069 phase_list = <d->ltd_namespace_phase_list;
2073 spin_lock(<ds->ltd_lock);
2074 list_del_init(phase_list);
2075 list_del_init(list);
2076 spin_unlock(<ds->ltd_lock);
2080 reply = req_capsule_server_get(&req->rq_pill,
2082 if (reply == NULL) {
2084 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2085 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2087 spin_lock(<ds->ltd_lock);
2088 list_del_init(phase_list);
2089 list_del_init(list);
2090 spin_unlock(<ds->ltd_lock);
2094 switch (reply->lr_status) {
2095 case LS_SCANNING_PHASE1:
2097 case LS_SCANNING_PHASE2:
2098 spin_lock(<ds->ltd_lock);
2099 list_del_init(phase_list);
2100 if (ltd->ltd_dead) {
2101 spin_unlock(<ds->ltd_lock);
2105 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2106 if (ltd->ltd_layout_done) {
2107 spin_unlock(<ds->ltd_lock);
2111 if (lr->lr_flags & LEF_TO_OST)
2112 list_add_tail(phase_list,
2113 &lad->lad_ost_phase2_list);
2115 list_add_tail(phase_list,
2116 &lad->lad_mdt_phase2_list);
2118 if (ltd->ltd_namespace_done) {
2119 spin_unlock(<ds->ltd_lock);
2123 list_add_tail(phase_list,
2124 &lad->lad_mdt_phase2_list);
2126 spin_unlock(<ds->ltd_lock);
2129 spin_lock(<ds->ltd_lock);
2130 list_del_init(phase_list);
2131 list_del_init(list);
2132 spin_unlock(<ds->ltd_lock);
2138 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2139 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2143 if (!laia->laia_shared) {
2145 lfsck_component_put(env, com);
2151 static void lfsck_interpret(const struct lu_env *env,
2152 struct lfsck_instance *lfsck,
2153 struct ptlrpc_request *req, void *args, int result)
2155 struct lfsck_async_interpret_args *laia = args;
2156 struct lfsck_component *com;
2158 LASSERT(laia->laia_com == NULL);
2159 LASSERT(laia->laia_shared);
2161 spin_lock(&lfsck->li_lock);
2162 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2163 laia->laia_com = com;
2164 lfsck_async_interpret_common(env, req, laia, result);
2167 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2168 laia->laia_com = com;
2169 lfsck_async_interpret_common(env, req, laia, result);
2171 spin_unlock(&lfsck->li_lock);
2174 static int lfsck_stop_notify(const struct lu_env *env,
2175 struct lfsck_instance *lfsck,
2176 struct lfsck_tgt_descs *ltds,
2177 struct lfsck_tgt_desc *ltd, __u16 type)
2179 struct lfsck_component *com;
2183 LASSERT(lfsck->li_master);
2185 spin_lock(&lfsck->li_lock);
2186 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2188 com = __lfsck_component_find(lfsck, type,
2189 &lfsck->li_list_double_scan);
2191 lfsck_component_get(com);
2192 spin_unlock(&lfsck->li_lock);
2195 struct lfsck_thread_info *info = lfsck_env_info(env);
2196 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2197 struct lfsck_request *lr = &info->lti_lr;
2198 struct lfsck_assistant_data *lad = com->lc_data;
2199 struct list_head *list;
2200 struct list_head *phase_list;
2201 struct ptlrpc_request_set *set;
2203 set = ptlrpc_prep_set();
2205 lfsck_component_put(env, com);
2210 if (type == LFSCK_TYPE_LAYOUT) {
2211 list = <d->ltd_layout_list;
2212 phase_list = <d->ltd_layout_phase_list;
2214 list = <d->ltd_namespace_list;
2215 phase_list = <d->ltd_namespace_phase_list;
2218 spin_lock(<ds->ltd_lock);
2219 if (list_empty(list)) {
2220 LASSERT(list_empty(phase_list));
2221 spin_unlock(<ds->ltd_lock);
2222 ptlrpc_set_destroy(set);
2227 list_del_init(phase_list);
2228 list_del_init(list);
2229 spin_unlock(<ds->ltd_lock);
2231 memset(lr, 0, sizeof(*lr));
2232 lr->lr_index = lfsck_dev_idx(lfsck);
2233 lr->lr_event = LE_PEER_EXIT;
2234 lr->lr_active = type;
2235 lr->lr_status = LS_CO_PAUSED;
2236 if (ltds == &lfsck->li_ost_descs)
2237 lr->lr_flags = LEF_TO_OST;
2239 laia->laia_com = com;
2240 laia->laia_ltds = ltds;
2241 atomic_inc(<d->ltd_ref);
2242 laia->laia_ltd = ltd;
2244 laia->laia_shared = 0;
2246 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2247 lfsck_async_interpret_common,
2248 laia, LFSCK_NOTIFY);
2250 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2251 "co-stop for %s: rc = %d\n",
2252 lfsck_lfsck2name(lfsck),
2253 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2254 ltd->ltd_index, lad->lad_name, rc);
2257 rc = ptlrpc_set_wait(set);
2260 ptlrpc_set_destroy(set);
2261 lfsck_component_put(env, com);
2267 static int lfsck_async_interpret(const struct lu_env *env,
2268 struct ptlrpc_request *req,
2271 struct lfsck_async_interpret_args *laia = args;
2272 struct lfsck_instance *lfsck;
2274 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2276 lfsck_interpret(env, lfsck, req, laia, rc);
2277 lfsck_tgt_put(laia->laia_ltd);
2278 if (rc != 0 && laia->laia_result != -EALREADY)
2279 laia->laia_result = rc;
2284 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2285 struct lfsck_request *lr,
2286 struct ptlrpc_request_set *set,
2287 ptlrpc_interpterer_t interpreter,
2288 void *args, int request)
2290 struct lfsck_async_interpret_args *laia;
2291 struct ptlrpc_request *req;
2292 struct lfsck_request *tmp;
2293 struct req_format *format;
2298 format = &RQF_LFSCK_NOTIFY;
2301 format = &RQF_LFSCK_QUERY;
2304 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2305 exp->exp_obd->obd_name, request, -EINVAL);
2309 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2313 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2315 ptlrpc_request_free(req);
2320 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2322 ptlrpc_request_set_replen(req);
2324 laia = ptlrpc_req_async_args(req);
2325 *laia = *(struct lfsck_async_interpret_args *)args;
2326 if (laia->laia_com != NULL)
2327 lfsck_component_get(laia->laia_com);
2328 req->rq_interpret_reply = interpreter;
2329 ptlrpc_set_add_req(set, req);
2334 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2335 struct lfsck_start_param *lsp)
2337 struct lfsck_instance *lfsck = com->lc_lfsck;
2338 struct lfsck_assistant_data *lad = com->lc_data;
2339 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2340 struct ptlrpc_thread *athread = &lad->lad_thread;
2341 struct lfsck_thread_args *lta;
2342 struct task_struct *task;
2346 lad->lad_assistant_status = 0;
2347 lad->lad_post_result = 0;
2348 lad->lad_to_post = 0;
2349 lad->lad_to_double_scan = 0;
2350 lad->lad_in_double_scan = 0;
2352 thread_set_flags(athread, 0);
2354 lta = lfsck_thread_args_init(lfsck, com, lsp);
2356 RETURN(PTR_ERR(lta));
2358 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2361 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2362 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2363 lfsck_thread_args_fini(lta);
2365 struct l_wait_info lwi = { 0 };
2367 l_wait_event(mthread->t_ctl_waitq,
2368 thread_is_running(athread) ||
2369 thread_is_stopped(athread),
2371 if (unlikely(!thread_is_running(athread)))
2372 rc = lad->lad_assistant_status;
2380 int lfsck_checkpoint_generic(const struct lu_env *env,
2381 struct lfsck_component *com)
2383 struct lfsck_assistant_data *lad = com->lc_data;
2384 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2385 struct ptlrpc_thread *athread = &lad->lad_thread;
2386 struct l_wait_info lwi = { 0 };
2388 l_wait_event(mthread->t_ctl_waitq,
2389 list_empty(&lad->lad_req_list) ||
2390 !thread_is_running(mthread) ||
2391 thread_is_stopped(athread),
2394 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2395 return LFSCK_CHECKPOINT_SKIP;
2400 void lfsck_post_generic(const struct lu_env *env,
2401 struct lfsck_component *com, int *result)
2403 struct lfsck_assistant_data *lad = com->lc_data;
2404 struct ptlrpc_thread *athread = &lad->lad_thread;
2405 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2406 struct l_wait_info lwi = { 0 };
2408 lad->lad_post_result = *result;
2411 lad->lad_to_post = 1;
2413 wake_up_all(&athread->t_ctl_waitq);
2414 l_wait_event(mthread->t_ctl_waitq,
2415 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2416 thread_is_stopped(athread),
2419 if (lad->lad_assistant_status < 0)
2420 *result = lad->lad_assistant_status;
2423 int lfsck_double_scan_generic(const struct lu_env *env,
2424 struct lfsck_component *com, int status)
2426 struct lfsck_assistant_data *lad = com->lc_data;
2427 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2428 struct ptlrpc_thread *athread = &lad->lad_thread;
2429 struct l_wait_info lwi = { 0 };
2431 if (status != LS_SCANNING_PHASE2)
2434 lad->lad_to_double_scan = 1;
2436 wake_up_all(&athread->t_ctl_waitq);
2437 l_wait_event(mthread->t_ctl_waitq,
2438 lad->lad_in_double_scan ||
2439 thread_is_stopped(athread),
2442 if (lad->lad_assistant_status < 0)
2443 return lad->lad_assistant_status;
2448 void lfsck_quit_generic(const struct lu_env *env,
2449 struct lfsck_component *com)
2451 struct lfsck_assistant_data *lad = com->lc_data;
2452 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2453 struct ptlrpc_thread *athread = &lad->lad_thread;
2454 struct l_wait_info lwi = { 0 };
2457 wake_up_all(&athread->t_ctl_waitq);
2458 l_wait_event(mthread->t_ctl_waitq,
2459 thread_is_init(athread) ||
2460 thread_is_stopped(athread),
2464 /* external interfaces */
2466 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2469 struct lfsck_instance *lfsck;
2473 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2477 lfsck = lfsck_instance_find(key, true, false);
2478 if (likely(lfsck != NULL)) {
2479 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2480 lfsck_instance_put(&env, lfsck);
2489 EXPORT_SYMBOL(lfsck_get_speed);
2491 int lfsck_set_speed(struct dt_device *key, int val)
2494 struct lfsck_instance *lfsck;
2498 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2502 lfsck = lfsck_instance_find(key, true, false);
2503 if (likely(lfsck != NULL)) {
2504 mutex_lock(&lfsck->li_mutex);
2505 if (__lfsck_set_speed(lfsck, val))
2506 rc = lfsck_bookmark_store(&env, lfsck);
2507 mutex_unlock(&lfsck->li_mutex);
2508 lfsck_instance_put(&env, lfsck);
2517 EXPORT_SYMBOL(lfsck_set_speed);
2519 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2522 struct lfsck_instance *lfsck;
2526 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2530 lfsck = lfsck_instance_find(key, true, false);
2531 if (likely(lfsck != NULL)) {
2532 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2533 lfsck_instance_put(&env, lfsck);
2542 EXPORT_SYMBOL(lfsck_get_windows);
2544 int lfsck_set_windows(struct dt_device *key, int val)
2547 struct lfsck_instance *lfsck;
2551 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2555 lfsck = lfsck_instance_find(key, true, false);
2556 if (likely(lfsck != NULL)) {
2557 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2558 CWARN("%s: invalid async windows size that may "
2559 "cause memory issues. The valid range is "
2561 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2563 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2564 mutex_lock(&lfsck->li_mutex);
2565 lfsck->li_bookmark_ram.lb_async_windows = val;
2566 rc = lfsck_bookmark_store(&env, lfsck);
2567 mutex_unlock(&lfsck->li_mutex);
2569 lfsck_instance_put(&env, lfsck);
2578 EXPORT_SYMBOL(lfsck_set_windows);
2580 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2583 struct lfsck_instance *lfsck;
2584 struct lfsck_component *com;
2588 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2592 lfsck = lfsck_instance_find(key, true, false);
2593 if (likely(lfsck != NULL)) {
2594 com = lfsck_component_find(lfsck, type);
2595 if (likely(com != NULL)) {
2596 rc = com->lc_ops->lfsck_dump(&env, com, m);
2597 lfsck_component_put(&env, com);
2602 lfsck_instance_put(&env, lfsck);
2611 EXPORT_SYMBOL(lfsck_dump);
2613 static int lfsck_stop_all(const struct lu_env *env,
2614 struct lfsck_instance *lfsck,
2615 struct lfsck_stop *stop)
2617 struct lfsck_thread_info *info = lfsck_env_info(env);
2618 struct lfsck_request *lr = &info->lti_lr;
2619 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2620 struct ptlrpc_request_set *set;
2621 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2622 struct lfsck_tgt_desc *ltd;
2623 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2629 LASSERT(stop->ls_flags & LPF_BROADCAST);
2631 set = ptlrpc_prep_set();
2632 if (unlikely(set == NULL))
2635 memset(lr, 0, sizeof(*lr));
2636 lr->lr_event = LE_STOP;
2637 lr->lr_index = lfsck_dev_idx(lfsck);
2638 lr->lr_status = stop->ls_status;
2639 lr->lr_version = bk->lb_version;
2640 lr->lr_active = LFSCK_TYPES_ALL;
2641 lr->lr_param = stop->ls_flags;
2643 laia->laia_com = NULL;
2644 laia->laia_ltds = ltds;
2646 laia->laia_result = 0;
2647 laia->laia_shared = 1;
2649 down_read(<ds->ltd_rw_sem);
2650 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2651 ltd = lfsck_tgt_get(ltds, idx);
2652 LASSERT(ltd != NULL);
2654 laia->laia_ltd = ltd;
2655 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2656 lfsck_async_interpret, laia,
2659 lfsck_interpret(env, lfsck, NULL, laia, rc);
2661 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2662 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2666 up_read(<ds->ltd_rw_sem);
2668 rc = ptlrpc_set_wait(set);
2669 ptlrpc_set_destroy(set);
2672 rc = laia->laia_result;
2674 if (rc == -EALREADY)
2678 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2679 lfsck_lfsck2name(lfsck), rc);
2681 RETURN(rc != 0 ? rc : rc1);
2684 static int lfsck_start_all(const struct lu_env *env,
2685 struct lfsck_instance *lfsck,
2686 struct lfsck_start *start)
2688 struct lfsck_thread_info *info = lfsck_env_info(env);
2689 struct lfsck_request *lr = &info->lti_lr;
2690 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2691 struct ptlrpc_request_set *set;
2692 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2693 struct lfsck_tgt_desc *ltd;
2694 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2699 LASSERT(start->ls_flags & LPF_BROADCAST);
2701 set = ptlrpc_prep_set();
2702 if (unlikely(set == NULL))
2705 memset(lr, 0, sizeof(*lr));
2706 lr->lr_event = LE_START;
2707 lr->lr_index = lfsck_dev_idx(lfsck);
2708 lr->lr_speed = bk->lb_speed_limit;
2709 lr->lr_version = bk->lb_version;
2710 lr->lr_active = start->ls_active;
2711 lr->lr_param = start->ls_flags;
2712 lr->lr_async_windows = bk->lb_async_windows;
2713 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2714 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2717 laia->laia_com = NULL;
2718 laia->laia_ltds = ltds;
2720 laia->laia_result = 0;
2721 laia->laia_shared = 1;
2723 down_read(<ds->ltd_rw_sem);
2724 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2725 ltd = lfsck_tgt_get(ltds, idx);
2726 LASSERT(ltd != NULL);
2728 laia->laia_ltd = ltd;
2729 ltd->ltd_layout_done = 0;
2730 ltd->ltd_namespace_done = 0;
2731 ltd->ltd_synced_failures = 0;
2732 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2733 lfsck_async_interpret, laia,
2736 lfsck_interpret(env, lfsck, NULL, laia, rc);
2738 CERROR("%s: cannot notify MDT %x for LFSCK "
2739 "start, failout: rc = %d\n",
2740 lfsck_lfsck2name(lfsck), idx, rc);
2744 up_read(<ds->ltd_rw_sem);
2747 ptlrpc_set_destroy(set);
2752 rc = ptlrpc_set_wait(set);
2753 ptlrpc_set_destroy(set);
2756 rc = laia->laia_result;
2759 struct lfsck_stop *stop = &info->lti_stop;
2761 CERROR("%s: cannot start LFSCK on some MDTs, "
2762 "stop all: rc = %d\n",
2763 lfsck_lfsck2name(lfsck), rc);
2764 if (rc != -EALREADY) {
2765 stop->ls_status = LS_FAILED;
2766 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2767 lfsck_stop_all(env, lfsck, stop);
2774 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2775 struct lfsck_start_param *lsp)
2777 struct lfsck_start *start = lsp->lsp_start;
2778 struct lfsck_instance *lfsck;
2779 struct lfsck_bookmark *bk;
2780 struct ptlrpc_thread *thread;
2781 struct lfsck_component *com;
2782 struct l_wait_info lwi = { 0 };
2783 struct lfsck_thread_args *lta;
2784 struct task_struct *task;
2791 lfsck = lfsck_instance_find(key, true, false);
2792 if (unlikely(lfsck == NULL))
2795 /* System is not ready, try again later. */
2796 if (unlikely(lfsck->li_namespace == NULL))
2797 GOTO(put, rc = -EAGAIN);
2799 /* start == NULL means auto trigger paused LFSCK. */
2800 if ((start == NULL) &&
2801 (list_empty(&lfsck->li_list_scan) ||
2802 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2805 bk = &lfsck->li_bookmark_ram;
2806 thread = &lfsck->li_thread;
2807 mutex_lock(&lfsck->li_mutex);
2808 spin_lock(&lfsck->li_lock);
2809 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2811 if (unlikely(start == NULL)) {
2812 spin_unlock(&lfsck->li_lock);
2816 while (start->ls_active != 0) {
2817 if (!(type & start->ls_active)) {
2822 com = __lfsck_component_find(lfsck, type,
2823 &lfsck->li_list_scan);
2825 com = __lfsck_component_find(lfsck, type,
2826 &lfsck->li_list_double_scan);
2832 if (com->lc_ops->lfsck_join != NULL) {
2833 rc = com->lc_ops->lfsck_join( env, com, lsp);
2834 if (rc != 0 && rc != -EALREADY)
2837 start->ls_active &= ~type;
2840 spin_unlock(&lfsck->li_lock);
2843 spin_unlock(&lfsck->li_lock);
2845 lfsck->li_status = 0;
2846 lfsck->li_oit_over = 0;
2847 lfsck->li_start_unplug = 0;
2848 lfsck->li_drop_dryrun = 0;
2849 lfsck->li_new_scanned = 0;
2851 /* For auto trigger. */
2855 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2856 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2857 lfsck_lfsck2name(lfsck));
2859 GOTO(out, rc = -EPERM);
2862 start->ls_version = bk->lb_version;
2864 if (start->ls_active != 0) {
2865 struct lfsck_component *next;
2867 if (start->ls_active == LFSCK_TYPES_ALL)
2868 start->ls_active = LFSCK_TYPES_SUPPORTED;
2870 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2871 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2872 GOTO(out, rc = -ENOTSUPP);
2875 list_for_each_entry_safe(com, next,
2876 &lfsck->li_list_scan, lc_link) {
2877 if (!(com->lc_type & start->ls_active)) {
2878 rc = com->lc_ops->lfsck_post(env, com, 0,
2885 while (start->ls_active != 0) {
2886 if (type & start->ls_active) {
2887 com = __lfsck_component_find(lfsck, type,
2888 &lfsck->li_list_idle);
2890 /* The component status will be updated
2891 * when its prep() is called later by
2892 * the LFSCK main engine. */
2893 list_move_tail(&com->lc_link,
2894 &lfsck->li_list_scan);
2895 start->ls_active &= ~type;
2901 if (list_empty(&lfsck->li_list_scan)) {
2902 /* The speed limit will be used to control both the LFSCK and
2903 * low layer scrub (if applied), need to be handled firstly. */
2904 if (start->ls_valid & LSV_SPEED_LIMIT) {
2905 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2906 rc = lfsck_bookmark_store(env, lfsck);
2915 if (start->ls_flags & LPF_RESET)
2916 flags |= DOIF_RESET;
2918 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2922 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2923 start->ls_active |= com->lc_type;
2924 if (flags & DOIF_RESET) {
2925 rc = com->lc_ops->lfsck_reset(env, com, false);
2932 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2933 if (bk->lb_param & LPF_DRYRUN)
2934 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2936 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2937 valid |= DOIV_ERROR_HANDLE;
2938 if (start->ls_flags & LPF_FAILOUT)
2939 flags |= DOIF_FAILOUT;
2942 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2943 valid |= DOIV_DRYRUN;
2944 if (start->ls_flags & LPF_DRYRUN)
2945 flags |= DOIF_DRYRUN;
2948 if (!list_empty(&lfsck->li_list_scan))
2949 flags |= DOIF_OUTUSED;
2951 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2952 thread_set_flags(thread, 0);
2953 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2955 GOTO(out, rc = PTR_ERR(lta));
2957 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2958 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2961 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2962 lfsck_lfsck2name(lfsck), rc);
2963 lfsck_thread_args_fini(lta);
2968 l_wait_event(thread->t_ctl_waitq,
2969 thread_is_running(thread) ||
2970 thread_is_stopped(thread),
2972 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2973 lfsck->li_start_unplug = 1;
2974 wake_up_all(&thread->t_ctl_waitq);
2979 /* release lfsck::li_mutex to avoid deadlock. */
2980 mutex_unlock(&lfsck->li_mutex);
2981 rc = lfsck_start_all(env, lfsck, start);
2983 spin_lock(&lfsck->li_lock);
2984 if (thread_is_stopped(thread)) {
2985 spin_unlock(&lfsck->li_lock);
2987 lfsck->li_status = LS_FAILED;
2988 lfsck->li_flags = 0;
2989 thread_set_flags(thread, SVC_STOPPING);
2990 spin_unlock(&lfsck->li_lock);
2992 lfsck->li_start_unplug = 1;
2993 wake_up_all(&thread->t_ctl_waitq);
2994 l_wait_event(thread->t_ctl_waitq,
2995 thread_is_stopped(thread),
2999 lfsck->li_start_unplug = 1;
3000 wake_up_all(&thread->t_ctl_waitq);
3006 mutex_unlock(&lfsck->li_mutex);
3009 lfsck_instance_put(env, lfsck);
3011 return rc < 0 ? rc : 0;
3013 EXPORT_SYMBOL(lfsck_start);
3015 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3016 struct lfsck_stop *stop)
3018 struct lfsck_instance *lfsck;
3019 struct ptlrpc_thread *thread;
3020 struct l_wait_info lwi = { 0 };
3025 lfsck = lfsck_instance_find(key, true, false);
3026 if (unlikely(lfsck == NULL))
3029 thread = &lfsck->li_thread;
3030 /* release lfsck::li_mutex to avoid deadlock. */
3031 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3032 if (!lfsck->li_master) {
3033 CERROR("%s: only allow to specify '-A' via MDS\n",
3034 lfsck_lfsck2name(lfsck));
3036 GOTO(out, rc = -EPERM);
3039 rc1 = lfsck_stop_all(env, lfsck, stop);
3042 mutex_lock(&lfsck->li_mutex);
3043 spin_lock(&lfsck->li_lock);
3044 /* no error if LFSCK is already stopped, or was never started */
3045 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3046 spin_unlock(&lfsck->li_lock);
3051 lfsck->li_status = stop->ls_status;
3052 lfsck->li_flags = stop->ls_flags;
3054 lfsck->li_status = LS_STOPPED;
3055 lfsck->li_flags = 0;
3058 thread_set_flags(thread, SVC_STOPPING);
3059 spin_unlock(&lfsck->li_lock);
3061 wake_up_all(&thread->t_ctl_waitq);
3062 l_wait_event(thread->t_ctl_waitq,
3063 thread_is_stopped(thread),
3069 mutex_unlock(&lfsck->li_mutex);
3070 lfsck_instance_put(env, lfsck);
3072 return rc != 0 ? rc : rc1;
3074 EXPORT_SYMBOL(lfsck_stop);
3076 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3077 struct lfsck_request *lr, struct thandle *th)
3079 int rc = -EOPNOTSUPP;
3082 switch (lr->lr_event) {
3084 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3085 struct lfsck_start_param lsp;
3087 memset(start, 0, sizeof(*start));
3088 start->ls_valid = lr->lr_valid;
3089 start->ls_speed_limit = lr->lr_speed;
3090 start->ls_version = lr->lr_version;
3091 start->ls_active = lr->lr_active;
3092 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3093 start->ls_async_windows = lr->lr_async_windows;
3095 lsp.lsp_start = start;
3096 lsp.lsp_index = lr->lr_index;
3097 lsp.lsp_index_valid = 1;
3098 rc = lfsck_start(env, key, &lsp);
3102 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3104 memset(stop, 0, sizeof(*stop));
3105 stop->ls_status = lr->lr_status;
3106 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3107 rc = lfsck_stop(env, key, stop);
3110 case LE_PHASE1_DONE:
3111 case LE_PHASE2_DONE:
3112 case LE_FID_ACCESSED:
3114 case LE_CONDITIONAL_DESTROY:
3115 case LE_SKIP_NLINK_DECLARE:
3117 case LE_SET_LMV_MASTER:
3118 case LE_SET_LMV_SLAVE:
3119 case LE_PAIRS_VERIFY: {
3120 struct lfsck_instance *lfsck;
3121 struct lfsck_component *com;
3123 lfsck = lfsck_instance_find(key, true, false);
3124 if (unlikely(lfsck == NULL))
3127 com = lfsck_component_find(lfsck, lr->lr_active);
3128 if (likely(com != NULL)) {
3129 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3130 lfsck_component_put(env, com);
3133 lfsck_instance_put(env, lfsck);
3142 EXPORT_SYMBOL(lfsck_in_notify);
3144 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3145 struct lfsck_request *lr)
3147 struct lfsck_instance *lfsck;
3148 struct lfsck_component *com;
3152 lfsck = lfsck_instance_find(key, true, false);
3153 if (unlikely(lfsck == NULL))
3156 com = lfsck_component_find(lfsck, lr->lr_active);
3157 if (likely(com != NULL)) {
3158 rc = com->lc_ops->lfsck_query(env, com);
3159 lfsck_component_put(env, com);
3164 lfsck_instance_put(env, lfsck);
3169 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3170 struct ldlm_namespace *ns)
3172 struct lfsck_instance *lfsck;
3175 lfsck = lfsck_instance_find(key, true, false);
3176 if (likely(lfsck != NULL)) {
3177 lfsck->li_namespace = ns;
3178 lfsck_instance_put(env, lfsck);
3184 EXPORT_SYMBOL(lfsck_register_namespace);
3186 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3187 struct dt_device *next, struct obd_device *obd,
3188 lfsck_out_notify notify, void *notify_data, bool master)
3190 struct lfsck_instance *lfsck;
3191 struct dt_object *root = NULL;
3192 struct dt_object *obj = NULL;
3193 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3197 lfsck = lfsck_instance_find(key, false, false);
3198 if (unlikely(lfsck != NULL))
3201 OBD_ALLOC_PTR(lfsck);
3205 mutex_init(&lfsck->li_mutex);
3206 spin_lock_init(&lfsck->li_lock);
3207 INIT_LIST_HEAD(&lfsck->li_link);
3208 INIT_LIST_HEAD(&lfsck->li_list_scan);
3209 INIT_LIST_HEAD(&lfsck->li_list_dir);
3210 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3211 INIT_LIST_HEAD(&lfsck->li_list_idle);
3212 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3213 atomic_set(&lfsck->li_ref, 1);
3214 atomic_set(&lfsck->li_double_scan_count, 0);
3215 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3216 lfsck->li_out_notify = notify;
3217 lfsck->li_out_notify_data = notify_data;
3218 lfsck->li_next = next;
3219 lfsck->li_bottom = key;
3220 lfsck->li_obd = obd;
3222 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3226 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3230 fid->f_seq = FID_SEQ_LOCAL_NAME;
3233 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3237 rc = dt_root_get(env, key, fid);
3241 root = dt_locate(env, key, fid);
3243 GOTO(out, rc = PTR_ERR(root));
3245 if (unlikely(!dt_try_as_dir(env, root)))
3246 GOTO(out, rc = -ENOTDIR);
3248 lfsck->li_local_root_fid = *fid;
3250 lfsck->li_master = 1;
3251 if (lfsck_dev_idx(lfsck) == 0) {
3252 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3253 const struct lu_name *cname;
3255 rc = dt_lookup(env, root,
3256 (struct dt_rec *)(&lfsck->li_global_root_fid),
3257 (const struct dt_key *)"ROOT");
3261 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3263 GOTO(out, rc = PTR_ERR(obj));
3265 if (unlikely(!dt_try_as_dir(env, obj)))
3266 GOTO(out, rc = -ENOTDIR);
3268 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3269 (const struct dt_key *)dotlustre);
3273 lfsck_object_put(env, obj);
3274 obj = dt_locate(env, key, fid);
3276 GOTO(out, rc = PTR_ERR(obj));
3278 cname = lfsck_name_get_const(env, dotlustre,
3280 rc = lfsck_verify_linkea(env, obj, cname,
3281 &lfsck->li_global_root_fid);
3285 if (unlikely(!dt_try_as_dir(env, obj)))
3286 GOTO(out, rc = -ENOTDIR);
3289 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3290 (const struct dt_key *)lostfound);
3294 lfsck_object_put(env, obj);
3295 obj = dt_locate(env, key, fid);
3297 GOTO(out, rc = PTR_ERR(obj));
3299 cname = lfsck_name_get_const(env, lostfound,
3301 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3305 lfsck_object_put(env, obj);
3310 fid->f_seq = FID_SEQ_LOCAL_FILE;
3311 fid->f_oid = OTABLE_IT_OID;
3313 obj = dt_locate(env, key, fid);
3315 GOTO(out, rc = PTR_ERR(obj));
3317 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3321 lfsck->li_obj_oit = obj;
3322 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3323 S_IFDIR | S_IRUGO | S_IWUSR);
3325 GOTO(out, rc = PTR_ERR(obj));
3327 lu_object_get(&obj->do_lu);
3328 lfsck->li_lfsck_dir = obj;
3329 rc = lfsck_bookmark_setup(env, lfsck);
3334 rc = lfsck_fid_init(lfsck);
3338 rc = lfsck_namespace_setup(env, lfsck);
3343 rc = lfsck_layout_setup(env, lfsck);
3347 /* XXX: more LFSCK components initialization to be added here. */
3349 rc = lfsck_instance_add(lfsck);
3351 rc = lfsck_add_target_from_orphan(env, lfsck);
3353 if (obj != NULL && !IS_ERR(obj))
3354 lfsck_object_put(env, obj);
3355 if (root != NULL && !IS_ERR(root))
3356 lfsck_object_put(env, root);
3358 lfsck_instance_cleanup(env, lfsck);
3361 EXPORT_SYMBOL(lfsck_register);
3363 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3365 struct lfsck_instance *lfsck;
3367 lfsck = lfsck_instance_find(key, false, true);
3369 lfsck_instance_put(env, lfsck);
3371 EXPORT_SYMBOL(lfsck_degister);
3373 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3374 struct dt_device *tgt, struct obd_export *exp,
3375 __u32 index, bool for_ost)
3377 struct lfsck_instance *lfsck;
3378 struct lfsck_tgt_desc *ltd;
3389 INIT_LIST_HEAD(<d->ltd_orphan_list);
3390 INIT_LIST_HEAD(<d->ltd_layout_list);
3391 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3392 INIT_LIST_HEAD(<d->ltd_namespace_list);
3393 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3394 atomic_set(<d->ltd_ref, 1);
3395 ltd->ltd_index = index;
3397 spin_lock(&lfsck_instance_lock);
3398 lfsck = __lfsck_instance_find(key, true, false);
3399 if (lfsck == NULL) {
3401 list_add_tail(<d->ltd_orphan_list,
3402 &lfsck_ost_orphan_list);
3404 list_add_tail(<d->ltd_orphan_list,
3405 &lfsck_mdt_orphan_list);
3406 spin_unlock(&lfsck_instance_lock);
3410 spin_unlock(&lfsck_instance_lock);
3412 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3416 lfsck_instance_put(env, lfsck);
3420 EXPORT_SYMBOL(lfsck_add_target);
3422 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3423 struct dt_device *tgt, __u32 index, bool for_ost)
3425 struct lfsck_instance *lfsck;
3426 struct lfsck_tgt_descs *ltds;
3427 struct lfsck_tgt_desc *ltd;
3428 struct list_head *head;
3431 head = &lfsck_ost_orphan_list;
3433 head = &lfsck_mdt_orphan_list;
3435 spin_lock(&lfsck_instance_lock);
3436 list_for_each_entry(ltd, head, ltd_orphan_list) {
3437 if (ltd->ltd_tgt == tgt) {
3438 list_del_init(<d->ltd_orphan_list);
3439 spin_unlock(&lfsck_instance_lock);
3447 lfsck = __lfsck_instance_find(key, true, false);
3448 spin_unlock(&lfsck_instance_lock);
3449 if (unlikely(lfsck == NULL))
3453 ltds = &lfsck->li_ost_descs;
3455 ltds = &lfsck->li_mdt_descs;
3457 down_write(<ds->ltd_rw_sem);
3458 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3460 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3463 ltd = LTD_TGT(ltds, index);
3464 if (unlikely(ltd == NULL))
3467 LASSERT(ltds->ltd_tgtnr > 0);
3470 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3471 LTD_TGT(ltds, index) = NULL;
3476 head = &lfsck->li_ost_descs.ltd_orphan;
3478 head = &lfsck->li_mdt_descs.ltd_orphan;
3480 list_for_each_entry(ltd, head, ltd_orphan_list) {
3481 if (ltd->ltd_tgt == tgt) {
3482 list_del_init(<d->ltd_orphan_list);
3488 up_write(<ds->ltd_rw_sem);
3490 spin_lock(<ds->ltd_lock);
3492 spin_unlock(<ds->ltd_lock);
3493 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3494 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3498 lfsck_instance_put(env, lfsck);
3500 EXPORT_SYMBOL(lfsck_del_target);
3502 static int __init lfsck_init(void)
3506 INIT_LIST_HEAD(&lfsck_instance_list);
3507 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3508 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3509 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3510 rc = lu_context_key_register(&lfsck_thread_key);
3512 tgt_register_lfsck_in_notify(lfsck_in_notify);
3513 tgt_register_lfsck_query(lfsck_query);
3519 static void __exit lfsck_exit(void)
3521 struct lfsck_tgt_desc *ltd;
3522 struct lfsck_tgt_desc *next;
3524 LASSERT(list_empty(&lfsck_instance_list));
3526 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3528 list_del_init(<d->ltd_orphan_list);
3532 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3534 list_del_init(<d->ltd_orphan_list);
3538 lu_context_key_degister(&lfsck_thread_key);
3541 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3542 MODULE_DESCRIPTION("LFSCK");
3543 MODULE_LICENSE("GPL");
3545 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);