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, 2015, Intel Corporation.
26 * lustre/lfsck/lfsck_lib.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <linux/kthread.h>
34 #include <linux/sched.h>
35 #include <libcfs/list.h>
36 #include <lu_object.h>
37 #include <dt_object.h>
38 #include <md_object.h>
39 #include <lustre_fld.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre_lfsck.h>
43 #include <lustre/lustre_lfsck_user.h>
45 #include "lfsck_internal.h"
47 #define LFSCK_CHECKPOINT_SKIP 1
49 /* define lfsck thread key */
50 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
52 static void lfsck_key_fini(const struct lu_context *ctx,
53 struct lu_context_key *key, void *data)
55 struct lfsck_thread_info *info = data;
57 lu_buf_free(&info->lti_linkea_buf);
58 lu_buf_free(&info->lti_linkea_buf2);
59 lu_buf_free(&info->lti_big_buf);
63 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
64 LU_KEY_INIT_GENERIC(lfsck);
66 static struct list_head lfsck_instance_list;
67 static struct list_head lfsck_ost_orphan_list;
68 static struct list_head lfsck_mdt_orphan_list;
69 static DEFINE_SPINLOCK(lfsck_instance_lock);
71 static const char *lfsck_status_names[] = {
73 [LS_SCANNING_PHASE1] = "scanning-phase1",
74 [LS_SCANNING_PHASE2] = "scanning-phase2",
75 [LS_COMPLETED] = "completed",
76 [LS_FAILED] = "failed",
77 [LS_STOPPED] = "stopped",
78 [LS_PAUSED] = "paused",
79 [LS_CRASHED] = "crashed",
80 [LS_PARTIAL] = "partial",
81 [LS_CO_FAILED] = "co-failed",
82 [LS_CO_STOPPED] = "co-stopped",
83 [LS_CO_PAUSED] = "co-paused"
86 const char *lfsck_flags_names[] = {
95 const char *lfsck_param_names[] = {
107 enum lfsck_verify_lpf_types {
108 LVLT_BY_BOOKMARK = 0,
109 LVLT_BY_NAMEENTRY = 1,
112 const char *lfsck_status2names(enum lfsck_status status)
114 if (unlikely(status < 0 || status >= LS_MAX))
117 return lfsck_status_names[status];
120 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
122 spin_lock_init(<ds->ltd_lock);
123 init_rwsem(<ds->ltd_rw_sem);
124 INIT_LIST_HEAD(<ds->ltd_orphan);
125 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
126 if (ltds->ltd_tgts_bitmap == NULL)
132 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
134 struct lfsck_tgt_desc *ltd;
135 struct lfsck_tgt_desc *next;
138 down_write(<ds->ltd_rw_sem);
140 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
142 list_del_init(<d->ltd_orphan_list);
146 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
147 up_write(<ds->ltd_rw_sem);
152 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
153 ltd = lfsck_ltd2tgt(ltds, idx);
154 if (likely(ltd != NULL)) {
155 LASSERT(list_empty(<d->ltd_layout_list));
156 LASSERT(list_empty(<d->ltd_layout_phase_list));
157 LASSERT(list_empty(<d->ltd_namespace_list));
158 LASSERT(list_empty(<d->ltd_namespace_phase_list));
161 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
162 lfsck_assign_tgt(ltds, NULL, idx);
167 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
170 for (idx = 0; idx < TGT_PTRS; idx++) {
171 if (ltds->ltd_tgts_idx[idx] != NULL) {
172 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
173 ltds->ltd_tgts_idx[idx] = NULL;
177 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
178 ltds->ltd_tgts_bitmap = NULL;
179 up_write(<ds->ltd_rw_sem);
182 static int __lfsck_add_target(const struct lu_env *env,
183 struct lfsck_instance *lfsck,
184 struct lfsck_tgt_desc *ltd,
185 bool for_ost, bool locked)
187 struct lfsck_tgt_descs *ltds;
188 __u32 index = ltd->ltd_index;
193 ltds = &lfsck->li_ost_descs;
195 ltds = &lfsck->li_mdt_descs;
198 down_write(<ds->ltd_rw_sem);
200 LASSERT(ltds->ltd_tgts_bitmap != NULL);
202 if (index >= ltds->ltd_tgts_bitmap->size) {
203 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
204 (__u32)BITS_PER_LONG);
205 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
206 cfs_bitmap_t *new_bitmap;
208 while (newsize < index + 1)
211 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
212 if (new_bitmap == NULL)
213 GOTO(unlock, rc = -ENOMEM);
215 if (ltds->ltd_tgtnr > 0)
216 cfs_bitmap_copy(new_bitmap, old_bitmap);
217 ltds->ltd_tgts_bitmap = new_bitmap;
218 CFS_FREE_BITMAP(old_bitmap);
221 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
222 CERROR("%s: the device %s (%u) is registered already\n",
223 lfsck_lfsck2name(lfsck),
224 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
225 GOTO(unlock, rc = -EEXIST);
228 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
229 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
230 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
231 GOTO(unlock, rc = -ENOMEM);
234 lfsck_assign_tgt(ltds, ltd, index);
235 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
238 GOTO(unlock, rc = 0);
242 up_write(<ds->ltd_rw_sem);
247 static int lfsck_add_target_from_orphan(const struct lu_env *env,
248 struct lfsck_instance *lfsck)
250 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
251 struct lfsck_tgt_desc *ltd;
252 struct lfsck_tgt_desc *next;
253 struct list_head *head = &lfsck_ost_orphan_list;
258 spin_lock(&lfsck_instance_lock);
259 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
260 if (ltd->ltd_key == lfsck->li_bottom)
261 list_move_tail(<d->ltd_orphan_list,
264 spin_unlock(&lfsck_instance_lock);
266 down_write(<ds->ltd_rw_sem);
267 while (!list_empty(<ds->ltd_orphan)) {
268 ltd = list_entry(ltds->ltd_orphan.next,
269 struct lfsck_tgt_desc,
271 list_del_init(<d->ltd_orphan_list);
272 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
273 /* Do not hold the semaphore for too long time. */
274 up_write(<ds->ltd_rw_sem);
278 down_write(<ds->ltd_rw_sem);
280 up_write(<ds->ltd_rw_sem);
283 ltds = &lfsck->li_mdt_descs;
284 head = &lfsck_mdt_orphan_list;
292 static inline struct lfsck_component *
293 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
294 struct list_head *list)
296 struct lfsck_component *com;
298 list_for_each_entry(com, list, lc_link) {
299 if (com->lc_type == type)
305 struct lfsck_component *
306 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
308 struct lfsck_component *com;
310 spin_lock(&lfsck->li_lock);
311 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
315 com = __lfsck_component_find(lfsck, type,
316 &lfsck->li_list_double_scan);
320 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
324 lfsck_component_get(com);
325 spin_unlock(&lfsck->li_lock);
329 void lfsck_component_cleanup(const struct lu_env *env,
330 struct lfsck_component *com)
332 if (!list_empty(&com->lc_link))
333 list_del_init(&com->lc_link);
334 if (!list_empty(&com->lc_link_dir))
335 list_del_init(&com->lc_link_dir);
337 lfsck_component_put(env, com);
340 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
341 struct lu_fid *fid, bool locked)
343 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
348 mutex_lock(&lfsck->li_mutex);
350 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
352 bk->lb_last_fid = *fid;
353 /* We do not care about whether the subsequent sub-operations
354 * failed or not. The worst case is that one FID is lost that
355 * is not a big issue for the LFSCK since it is relative rare
356 * for LFSCK create. */
357 rc = lfsck_bookmark_store(env, lfsck);
361 mutex_unlock(&lfsck->li_mutex);
366 static int __lfsck_ibits_lock(const struct lu_env *env,
367 struct lfsck_instance *lfsck,
368 struct dt_object *obj, struct ldlm_res_id *resid,
369 struct lustre_handle *lh, __u64 bits,
372 struct lfsck_thread_info *info = lfsck_env_info(env);
373 union ldlm_policy_data *policy = &info->lti_policy;
374 __u64 flags = LDLM_FL_ATOMIC_CB;
377 LASSERT(lfsck->li_namespace != NULL);
379 memset(policy, 0, sizeof(*policy));
380 policy->l_inodebits.bits = bits;
381 if (dt_object_remote(obj)) {
382 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
384 memset(einfo, 0, sizeof(*einfo));
385 einfo->ei_type = LDLM_IBITS;
386 einfo->ei_mode = mode;
387 einfo->ei_cb_bl = ldlm_blocking_ast;
388 einfo->ei_cb_cp = ldlm_completion_ast;
389 einfo->ei_res_id = resid;
391 rc = dt_object_lock(env, obj, lh, einfo, policy);
393 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
394 LDLM_IBITS, policy, mode,
395 &flags, ldlm_blocking_ast,
396 ldlm_completion_ast, NULL, NULL,
397 0, LVB_T_NONE, NULL, lh);
400 if (rc == ELDLM_OK) {
403 memset(lh, 0, sizeof(*lh));
411 * Request the specified ibits lock for the given object.
413 * Before the LFSCK modifying on the namespace visible object,
414 * it needs to acquire related ibits ldlm lock.
416 * \param[in] env pointer to the thread context
417 * \param[in] lfsck pointer to the lfsck instance
418 * \param[in] obj pointer to the dt_object to be locked
419 * \param[out] lh pointer to the lock handle
420 * \param[in] bits the bits for the ldlm lock to be acquired
421 * \param[in] mode the mode for the ldlm lock to be acquired
423 * \retval 0 for success
424 * \retval negative error number on failure
426 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
427 struct dt_object *obj, struct lustre_handle *lh,
428 __u64 bits, enum ldlm_mode mode)
430 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
432 LASSERT(!lustre_handle_is_used(lh));
434 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
435 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
439 * Release the the specified ibits lock.
441 * If the lock has been acquired before, release it
442 * and cleanup the handle. Otherwise, do nothing.
444 * \param[in] lh pointer to the lock handle
445 * \param[in] mode the mode for the ldlm lock to be released
447 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
449 if (lustre_handle_is_used(lh)) {
450 ldlm_lock_decref(lh, mode);
451 memset(lh, 0, sizeof(*lh));
456 * Request compound ibits locks for the given <obj, name> pairs.
458 * Before the LFSCK modifying on the namespace visible object, it needs to
459 * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
460 * the lock purpose. But the simple lfsck_ibits_lock for directory-based
461 * modificationis (such as insert name entry to the directory) may be too
462 * coarse-grained and not efficient.
464 * The lfsck_lock() will request compound ibits locks on the specified
465 * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
466 * lock on the directory object, and the regular ibits lock on the name hash.
468 * \param[in] env pointer to the thread context
469 * \param[in] lfsck pointer to the lfsck instance
470 * \param[in] obj pointer to the dt_object to be locked
471 * \param[in] name used for building the PDO lock resource
472 * \param[out] llh pointer to the lfsck_lock_handle
473 * \param[in] bits the bits for the ldlm lock to be acquired
474 * \param[in] mode the mode for the ldlm lock to be acquired
476 * \retval 0 for success
477 * \retval negative error number on failure
479 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
480 struct dt_object *obj, const char *name,
481 struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
483 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
486 LASSERT(S_ISDIR(lfsck_object_type(obj)));
487 LASSERT(name != NULL);
488 LASSERT(name[0] != 0);
489 LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
490 LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
494 llh->llh_pdo_mode = LCK_EX;
497 llh->llh_pdo_mode = LCK_CW;
500 llh->llh_pdo_mode = LCK_CR;
503 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
504 DFID"\n", lfsck_lfsck2name(lfsck), mode,
505 PFID(lfsck_dto2fid(obj)));
509 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
510 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
511 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
515 llh->llh_reg_mode = mode;
516 resid->name[LUSTRE_RES_ID_HSH_OFF] = full_name_hash(name, strlen(name));
517 LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
518 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
519 bits, llh->llh_reg_mode);
521 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
527 * Release the the compound ibits locks.
529 * \param[in] llh pointer to the lfsck_lock_handle to be released
531 void lfsck_unlock(struct lfsck_lock_handle *llh)
533 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
534 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
537 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
538 struct lfsck_instance *lfsck,
539 const struct lu_fid *fid)
541 struct seq_server_site *ss = lfsck_dev_site(lfsck);
542 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
545 fld_range_set_mdt(range);
546 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
548 rc = range->lsr_index;
553 const char dot[] = ".";
554 const char dotdot[] = "..";
555 static const char dotlustre[] = ".lustre";
556 static const char lostfound[] = "lost+found";
559 * Remove the name entry from the .lustre/lost+found directory.
561 * No need to care about the object referenced by the name entry,
562 * either the name entry is invalid or redundant, or the referenced
563 * object has been processed or will be handled by others.
565 * \param[in] env pointer to the thread context
566 * \param[in] lfsck pointer to the lfsck instance
567 * \param[in] name the name for the name entry to be removed
569 * \retval 0 for success
570 * \retval negative error number on failure
572 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
573 struct lfsck_instance *lfsck,
576 struct dt_object *parent = lfsck->li_lpf_root_obj;
577 struct dt_device *dev = lfsck_obj2dev(parent);
579 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
583 rc = lfsck_lock(env, lfsck, parent, name, llh,
584 MDS_INODELOCK_UPDATE, LCK_PW);
588 th = dt_trans_create(env, dev);
590 GOTO(unlock, rc = PTR_ERR(th));
592 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
596 rc = dt_declare_ref_del(env, parent, th);
600 rc = dt_trans_start_local(env, dev, th);
604 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
608 dt_write_lock(env, parent, 0);
609 rc = dt_ref_del(env, parent, th);
610 dt_write_unlock(env, parent);
615 dt_trans_stop(env, dev, th);
620 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
621 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
626 static int lfsck_create_lpf_local(const struct lu_env *env,
627 struct lfsck_instance *lfsck,
628 struct dt_object *child,
630 struct dt_object_format *dof,
633 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
634 struct dt_object *parent = lfsck->li_lpf_root_obj;
635 struct dt_device *dev = lfsck_obj2dev(child);
636 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
637 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
638 const struct lu_fid *cfid = lfsck_dto2fid(child);
639 struct thandle *th = NULL;
640 struct linkea_data ldata = { NULL };
641 struct lu_buf linkea_buf;
642 const struct lu_name *cname;
644 int len = sizeof(struct lfsck_bookmark);
648 rc = linkea_data_new(&ldata,
649 &lfsck_env_info(env)->lti_linkea_buf2);
653 cname = lfsck_name_get_const(env, name, strlen(name));
654 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
658 th = dt_trans_create(env, dev);
662 /* 1a. create child */
663 rc = dt_declare_create(env, child, la, NULL, dof, th);
667 if (!dt_try_as_dir(env, child))
668 GOTO(stop, rc = -ENOTDIR);
670 /* 2a. increase child nlink */
671 rc = dt_declare_ref_add(env, child, th);
675 /* 3a. insert dot into child dir */
676 rec->rec_type = S_IFDIR;
678 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
679 (const struct dt_key *)dot, th);
683 /* 4a. insert dotdot into child dir */
684 rec->rec_fid = &LU_LPF_FID;
685 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
686 (const struct dt_key *)dotdot, th);
690 /* 5a. insert linkEA for child */
691 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
692 ldata.ld_leh->leh_len);
693 rc = dt_declare_xattr_set(env, child, &linkea_buf,
694 XATTR_NAME_LINK, 0, th);
698 /* 6a. insert name into parent dir */
699 rec->rec_type = S_IFDIR;
701 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
702 (const struct dt_key *)name, th);
706 /* 7a. increase parent nlink */
707 rc = dt_declare_ref_add(env, parent, th);
711 /* 8a. update bookmark */
712 rc = dt_declare_record_write(env, bk_obj,
713 lfsck_buf_get(env, bk, len), 0, th);
717 rc = dt_trans_start_local(env, dev, th);
721 dt_write_lock(env, child, 0);
722 /* 1b. create child */
723 rc = dt_create(env, child, la, NULL, dof, th);
727 /* 2b. increase child nlink */
728 rc = dt_ref_add(env, child, th);
732 /* 3b. insert dot into child dir */
734 rc = dt_insert(env, child, (const struct dt_rec *)rec,
735 (const struct dt_key *)dot, th, 1);
739 /* 4b. insert dotdot into child dir */
740 rec->rec_fid = &LU_LPF_FID;
741 rc = dt_insert(env, child, (const struct dt_rec *)rec,
742 (const struct dt_key *)dotdot, th, 1);
746 /* 5b. insert linkEA for child. */
747 rc = dt_xattr_set(env, child, &linkea_buf,
748 XATTR_NAME_LINK, 0, th);
749 dt_write_unlock(env, child);
753 /* 6b. insert name into parent dir */
755 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
756 (const struct dt_key *)name, th, 1);
760 dt_write_lock(env, parent, 0);
761 /* 7b. increase parent nlink */
762 rc = dt_ref_add(env, parent, th);
763 dt_write_unlock(env, parent);
767 bk->lb_lpf_fid = *cfid;
768 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
770 /* 8b. update bookmark */
771 rc = dt_record_write(env, bk_obj,
772 lfsck_buf_get(env, bk, len), &pos, th);
777 dt_write_unlock(env, child);
780 dt_trans_stop(env, dev, th);
785 static int lfsck_create_lpf_remote(const struct lu_env *env,
786 struct lfsck_instance *lfsck,
787 struct dt_object *child,
789 struct dt_object_format *dof,
792 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
793 struct dt_object *parent = lfsck->li_lpf_root_obj;
794 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
795 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
796 const struct lu_fid *cfid = lfsck_dto2fid(child);
797 struct thandle *th = NULL;
798 struct linkea_data ldata = { NULL };
799 struct lu_buf linkea_buf;
800 const struct lu_name *cname;
801 struct dt_device *dev;
803 int len = sizeof(struct lfsck_bookmark);
807 rc = linkea_data_new(&ldata,
808 &lfsck_env_info(env)->lti_linkea_buf2);
812 cname = lfsck_name_get_const(env, name, strlen(name));
813 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
817 /* Create .lustre/lost+found/MDTxxxx. */
819 /* XXX: Currently, cross-MDT create operation needs to create the child
820 * object firstly, then insert name into the parent directory. For
821 * this case, the child object resides on current MDT (local), but
822 * the parent ".lustre/lost+found" may be on remote MDT. It is not
823 * easy to contain all the sub-modifications orderly within single
826 * To avoid more inconsistency, we split the create operation into
829 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
831 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
834 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
835 * repair such inconsistency when LFSCK run next time. */
837 /* Transaction I: locally */
839 dev = lfsck_obj2dev(child);
840 th = dt_trans_create(env, dev);
844 /* 1a. create child */
845 rc = dt_declare_create(env, child, la, NULL, dof, th);
849 if (!dt_try_as_dir(env, child))
850 GOTO(stop, rc = -ENOTDIR);
852 /* 2a. increase child nlink */
853 rc = dt_declare_ref_add(env, child, th);
857 /* 3a. insert dot into child dir */
858 rec->rec_type = S_IFDIR;
860 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
861 (const struct dt_key *)dot, th);
865 /* 4a. insert dotdot into child dir */
866 rec->rec_fid = &LU_LPF_FID;
867 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
868 (const struct dt_key *)dotdot, th);
872 /* 5a. insert linkEA for child */
873 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
874 ldata.ld_leh->leh_len);
875 rc = dt_declare_xattr_set(env, child, &linkea_buf,
876 XATTR_NAME_LINK, 0, th);
880 /* 6a. update bookmark */
881 rc = dt_declare_record_write(env, bk_obj,
882 lfsck_buf_get(env, bk, len), 0, th);
886 rc = dt_trans_start_local(env, dev, th);
890 dt_write_lock(env, child, 0);
891 /* 1b. create child */
892 rc = dt_create(env, child, la, NULL, dof, th);
896 /* 2b. increase child nlink */
897 rc = dt_ref_add(env, child, th);
901 /* 3b. insert dot into child dir */
902 rec->rec_type = S_IFDIR;
904 rc = dt_insert(env, child, (const struct dt_rec *)rec,
905 (const struct dt_key *)dot, th, 1);
909 /* 4b. insert dotdot into child dir */
910 rec->rec_fid = &LU_LPF_FID;
911 rc = dt_insert(env, child, (const struct dt_rec *)rec,
912 (const struct dt_key *)dotdot, th, 1);
916 /* 5b. insert linkEA for child */
917 rc = dt_xattr_set(env, child, &linkea_buf,
918 XATTR_NAME_LINK, 0, th);
922 bk->lb_lpf_fid = *cfid;
923 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
925 /* 6b. update bookmark */
926 rc = dt_record_write(env, bk_obj,
927 lfsck_buf_get(env, bk, len), &pos, th);
929 dt_write_unlock(env, child);
930 dt_trans_stop(env, dev, th);
934 /* Transaction II: remotely */
936 dev = lfsck_obj2dev(parent);
937 th = dt_trans_create(env, dev);
942 /* 5a. insert name into parent dir */
944 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
945 (const struct dt_key *)name, th);
949 /* 6a. increase parent nlink */
950 rc = dt_declare_ref_add(env, parent, th);
954 rc = dt_trans_start_local(env, dev, th);
958 /* 5b. insert name into parent dir */
959 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
960 (const struct dt_key *)name, th, 1);
964 dt_write_lock(env, parent, 0);
965 /* 6b. increase parent nlink */
966 rc = dt_ref_add(env, parent, th);
967 dt_write_unlock(env, parent);
972 dt_write_unlock(env, child);
974 dt_trans_stop(env, dev, th);
976 if (rc != 0 && dev == lfsck_obj2dev(parent))
977 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
978 "for orphans, but failed to insert the name %s "
979 "to the .lustre/lost+found/. Such inconsistency "
980 "will be repaired when LFSCK run next time: rc = %d\n",
981 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
987 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
989 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
990 * orphans and other uncertain inconsistent objects found during the
991 * LFSCK. Such directory will be created by the LFSCK engine on the
992 * local MDT before the LFSCK scanning.
994 * \param[in] env pointer to the thread context
995 * \param[in] lfsck pointer to the lfsck instance
997 * \retval 0 for success
998 * \retval negative error number on failure
1000 static int lfsck_create_lpf(const struct lu_env *env,
1001 struct lfsck_instance *lfsck)
1003 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1004 struct lfsck_thread_info *info = lfsck_env_info(env);
1005 struct lu_fid *cfid = &info->lti_fid2;
1006 struct lu_attr *la = &info->lti_la;
1007 struct dt_object_format *dof = &info->lti_dof;
1008 struct dt_object *parent = lfsck->li_lpf_root_obj;
1009 struct dt_object *child = NULL;
1010 struct lfsck_lock_handle *llh = &info->lti_llh;
1012 int node = lfsck_dev_idx(lfsck);
1016 LASSERT(lfsck->li_master);
1017 LASSERT(parent != NULL);
1018 LASSERT(lfsck->li_lpf_obj == NULL);
1020 snprintf(name, 8, "MDT%04x", node);
1021 rc = lfsck_lock(env, lfsck, parent, name, llh,
1022 MDS_INODELOCK_UPDATE, LCK_PW);
1026 if (fid_is_zero(&bk->lb_lpf_fid)) {
1027 /* There is corner case that: in former LFSCK scanning we have
1028 * created the .lustre/lost+found/MDTxxxx but failed to update
1029 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1030 * it from MDT0 firstly. */
1031 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1032 (const struct dt_key *)name);
1033 if (rc != 0 && rc != -ENOENT)
1037 bk->lb_lpf_fid = *cfid;
1038 rc = lfsck_bookmark_store(env, lfsck);
1040 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1045 *cfid = bk->lb_lpf_fid;
1048 child = lfsck_object_find_bottom(env, lfsck, cfid);
1050 GOTO(unlock, rc = PTR_ERR(child));
1052 if (dt_object_exists(child) != 0) {
1053 if (unlikely(!dt_try_as_dir(env, child)))
1056 lfsck->li_lpf_obj = child;
1061 memset(la, 0, sizeof(*la));
1062 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1063 la->la_mode = S_IFDIR | S_IRWXU;
1064 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1066 memset(dof, 0, sizeof(*dof));
1067 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1070 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1072 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1074 lfsck->li_lpf_obj = child;
1080 if (rc != 0 && child != NULL && !IS_ERR(child))
1081 lfsck_object_put(env, child);
1087 * Scan .lustre/lost+found for bad name entries and remove them.
1089 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1090 * index in the system. Any other formatted name is invalid and should be
1093 * \param[in] env pointer to the thread context
1094 * \param[in] lfsck pointer to the lfsck instance
1096 * \retval 0 for success
1097 * \retval negative error number on failure
1099 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1100 struct lfsck_instance *lfsck)
1102 struct dt_object *parent = lfsck->li_lpf_root_obj;
1103 struct lu_dirent *ent =
1104 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1105 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1110 it = iops->init(env, parent, LUDA_64BITHASH);
1112 RETURN(PTR_ERR(it));
1114 rc = iops->load(env, it, 0);
1116 rc = iops->next(env, it);
1123 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1127 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1128 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1131 /* name length must be strlen("MDTxxxx") */
1132 if (ent->lde_namelen != 7)
1135 if (memcmp(ent->lde_name, "MDT", off) != 0)
1138 while (off < 7 && isxdigit(ent->lde_name[off]))
1144 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1151 rc = iops->next(env, it);
1155 iops->fini(env, it);
1157 RETURN(rc > 0 ? 0 : rc);
1160 static int lfsck_update_lpf_entry(const struct lu_env *env,
1161 struct lfsck_instance *lfsck,
1162 struct dt_object *parent,
1163 struct dt_object *child,
1165 enum lfsck_verify_lpf_types type)
1169 if (type == LVLT_BY_BOOKMARK) {
1170 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1171 lfsck_dto2fid(child), S_IFDIR);
1172 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1173 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1174 rc = lfsck_bookmark_store(env, lfsck);
1176 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1177 " in the bookmark file: rc = %d\n",
1178 lfsck_lfsck2name(lfsck),
1179 PFID(lfsck_dto2fid(child)), rc);
1186 * Check whether the @child back references the @parent.
1189 * 1) The child's FID is stored in the bookmark file. If the child back
1190 * references the parent (LU_LPF_FID object) via its ".." entry, then
1191 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1192 * the child back references another parent2, then:
1193 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1194 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1195 * references the child. So keep them there. As the LFSCK processing,
1196 * the parent3 may be found, then when the LFSCK run next time, the
1197 * inconsistency can be repaired.
1199 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1200 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1201 * via its ".." entry, then update the bookmark file, otherwise, if the child
1202 * back references another parent2, then:
1203 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1204 * from .lustre/lost+found/;
1205 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1206 * sub-directory name entry and update the child;
1207 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1208 * or not, then keep them there.
1210 * \param[in] env pointer to the thread context
1211 * \param[in] lfsck pointer to the lfsck instance
1212 * \param[in] child pointer to the lost+found sub-directory object
1213 * \param[in] name the name for lost+found sub-directory object
1214 * \param[out] fid pointer to the buffer to hold the FID of the object
1215 * (called it as parent2) that is referenced via the
1216 * child's dotdot entry; it also can be the FID that
1217 * is referenced by the name entry under the parent2.
1218 * \param[in] type to indicate where the child's FID is stored in
1220 * \retval positive number for uncertain inconsistency
1221 * \retval 0 for success
1222 * \retval negative error number on failure
1224 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1225 struct lfsck_instance *lfsck,
1226 struct dt_object *child, const char *name,
1228 enum lfsck_verify_lpf_types type)
1230 struct dt_object *parent = lfsck->li_lpf_root_obj;
1231 struct lfsck_thread_info *info = lfsck_env_info(env);
1232 char *name2 = info->lti_key;
1233 struct lu_fid *fid2 = &info->lti_fid3;
1234 struct dt_object *parent2 = NULL;
1235 struct lustre_handle lh = { 0 };
1240 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1241 (const struct dt_key *)dotdot);
1245 if (!fid_is_sane(fid))
1246 GOTO(linkea, rc = -EINVAL);
1248 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1249 const struct lu_name *cname;
1251 if (lfsck->li_lpf_obj == NULL) {
1252 lu_object_get(&child->do_lu);
1253 lfsck->li_lpf_obj = child;
1256 cname = lfsck_name_get_const(env, name, strlen(name));
1257 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1259 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1265 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1266 if (IS_ERR(parent2))
1267 GOTO(linkea, parent2);
1269 if (!dt_object_exists(parent2)) {
1270 lfsck_object_put(env, parent2);
1272 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1275 if (!dt_try_as_dir(env, parent2)) {
1276 lfsck_object_put(env, parent2);
1278 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1282 /* To prevent rename/unlink race */
1283 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1284 MDS_INODELOCK_UPDATE, LCK_PR);
1288 dt_read_lock(env, child, 0);
1289 rc = lfsck_links_get_first(env, child, name2, fid2);
1291 dt_read_unlock(env, child);
1292 lfsck_ibits_unlock(&lh, LCK_PR);
1294 GOTO(out_put, rc = 1);
1297 /* It is almost impossible that the bookmark file (or the name entry)
1298 * and the linkEA hit the same data corruption. Trust the linkEA. */
1299 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1300 dt_read_unlock(env, child);
1301 lfsck_ibits_unlock(&lh, LCK_PR);
1304 if (lfsck->li_lpf_obj == NULL) {
1305 lu_object_get(&child->do_lu);
1306 lfsck->li_lpf_obj = child;
1309 /* Update the child's dotdot entry */
1310 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1311 &LU_LPF_FID, S_IFDIR);
1313 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1319 if (parent2 == NULL || IS_ERR(parent2)) {
1320 dt_read_unlock(env, child);
1321 lfsck_ibits_unlock(&lh, LCK_PR);
1323 GOTO(out_done, rc = 1);
1326 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1327 (const struct dt_key *)name2);
1328 dt_read_unlock(env, child);
1329 lfsck_ibits_unlock(&lh, LCK_PR);
1330 if (rc != 0 && rc != -ENOENT)
1333 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1334 if (type == LVLT_BY_BOOKMARK)
1335 GOTO(out_put, rc = 1);
1337 /* Trust the name entry, update the child's dotdot entry. */
1338 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1339 &LU_LPF_FID, S_IFDIR);
1344 if (type == LVLT_BY_BOOKMARK) {
1345 /* Invalid FID record in the bookmark file, reset it. */
1346 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1347 rc = lfsck_bookmark_store(env, lfsck);
1349 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1350 " in the bookmark file: rc = %d\n",
1351 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1352 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1353 /* The name entry is wrong, remove it. */
1354 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1360 if (parent2 != NULL && !IS_ERR(parent2))
1361 lfsck_object_put(env, parent2);
1368 * Verify the /ROOT/.lustre/lost+found/ directory.
1370 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1371 * the LFSCK does not exactly know how to handle, such as orphans. So before
1372 * the LFSCK scanning the system, the consistency of such directory needs to
1373 * be verified firstly to allow the users to use it during the LFSCK.
1375 * \param[in] env pointer to the thread context
1376 * \param[in] lfsck pointer to the lfsck instance
1378 * \retval positive number for uncertain inconsistency
1379 * \retval 0 for success
1380 * \retval negative error number on failure
1382 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1384 struct lfsck_thread_info *info = lfsck_env_info(env);
1385 struct lu_fid *pfid = &info->lti_fid;
1386 struct lu_fid *cfid = &info->lti_fid2;
1387 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1388 struct dt_object *parent;
1389 /* child1's FID is in the bookmark file. */
1390 struct dt_object *child1 = NULL;
1391 /* child2's FID is in the name entry MDTxxxx. */
1392 struct dt_object *child2 = NULL;
1393 const struct lu_name *cname;
1395 int node = lfsck_dev_idx(lfsck);
1399 LASSERT(lfsck->li_master);
1401 if (lfsck->li_lpf_root_obj != NULL)
1405 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1408 struct lfsck_tgt_desc *ltd;
1410 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1411 if (unlikely(ltd == NULL))
1414 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1420 RETURN(PTR_ERR(parent));
1422 LASSERT(dt_object_exists(parent));
1424 if (unlikely(!dt_try_as_dir(env, parent))) {
1425 lfsck_object_put(env, parent);
1427 GOTO(put, rc = -ENOTDIR);
1430 lfsck->li_lpf_root_obj = parent;
1432 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1434 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1435 "for bad sub-directories: rc = %d\n",
1436 lfsck_lfsck2name(lfsck), rc);
1440 snprintf(name, 8, "MDT%04x", node);
1441 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1442 (const struct dt_key *)name);
1443 if (rc == -ENOENT) {
1451 /* Invalid FID in the name entry, remove the name entry. */
1452 if (!fid_is_norm(cfid)) {
1453 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1460 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1462 GOTO(put, rc = PTR_ERR(child2));
1464 if (unlikely(!dt_object_exists(child2) ||
1465 dt_object_remote(child2)) ||
1466 !S_ISDIR(lfsck_object_type(child2))) {
1467 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1474 if (unlikely(!dt_try_as_dir(env, child2))) {
1475 lfsck_object_put(env, child2);
1481 if (fid_is_zero(&bk->lb_lpf_fid))
1484 if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1485 if (lfsck->li_lpf_obj == NULL) {
1486 lu_object_get(&child2->do_lu);
1487 lfsck->li_lpf_obj = child2;
1490 cname = lfsck_name_get_const(env, name, strlen(name));
1491 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1496 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1497 struct lu_fid tfid = bk->lb_lpf_fid;
1499 /* Invalid FID record in the bookmark file, reset it. */
1500 fid_zero(&bk->lb_lpf_fid);
1501 rc = lfsck_bookmark_store(env, lfsck);
1503 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1504 " in the bookmark file: rc = %d\n",
1505 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1513 child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1514 if (IS_ERR(child1)) {
1519 if (unlikely(!dt_object_exists(child1) ||
1520 dt_object_remote(child1)) ||
1521 !S_ISDIR(lfsck_object_type(child1))) {
1522 /* Invalid FID record in the bookmark file, reset it. */
1523 fid_zero(&bk->lb_lpf_fid);
1524 rc = lfsck_bookmark_store(env, lfsck);
1526 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1527 " in the bookmark file: rc = %d\n",
1528 lfsck_lfsck2name(lfsck),
1529 PFID(lfsck_dto2fid(child1)), rc);
1534 lfsck_object_put(env, child1);
1539 if (unlikely(!dt_try_as_dir(env, child1))) {
1540 lfsck_object_put(env, child1);
1546 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1548 if (lu_fid_eq(pfid, &LU_LPF_FID))
1553 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1554 pfid, LVLT_BY_NAMEENTRY);
1559 if (lfsck->li_lpf_obj != NULL) {
1560 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1561 lfsck_object_put(env, lfsck->li_lpf_obj);
1562 lfsck->li_lpf_obj = NULL;
1565 } else if (rc == 0) {
1566 rc = lfsck_create_lpf(env, lfsck);
1569 if (child2 != NULL && !IS_ERR(child2))
1570 lfsck_object_put(env, child2);
1571 if (child1 != NULL && !IS_ERR(child1))
1572 lfsck_object_put(env, child1);
1577 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1579 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1580 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1585 if (unlikely(ss == NULL))
1588 OBD_ALLOC_PTR(lfsck->li_seq);
1589 if (lfsck->li_seq == NULL)
1592 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1594 GOTO(out, rc = -ENOMEM);
1596 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1597 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1599 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1603 if (fid_is_sane(&bk->lb_last_fid))
1604 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1609 OBD_FREE_PTR(lfsck->li_seq);
1610 lfsck->li_seq = NULL;
1615 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1617 if (lfsck->li_seq != NULL) {
1618 seq_client_fini(lfsck->li_seq);
1619 OBD_FREE_PTR(lfsck->li_seq);
1620 lfsck->li_seq = NULL;
1624 void lfsck_instance_cleanup(const struct lu_env *env,
1625 struct lfsck_instance *lfsck)
1627 struct ptlrpc_thread *thread = &lfsck->li_thread;
1628 struct lfsck_component *com;
1629 struct lfsck_component *next;
1630 struct lfsck_lmv_unit *llu;
1631 struct lfsck_lmv_unit *llu_next;
1632 struct lfsck_lmv *llmv;
1635 LASSERT(list_empty(&lfsck->li_link));
1636 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1638 if (lfsck->li_obj_oit != NULL) {
1639 lfsck_object_put(env, lfsck->li_obj_oit);
1640 lfsck->li_obj_oit = NULL;
1643 LASSERT(lfsck->li_obj_dir == NULL);
1644 LASSERT(lfsck->li_lmv == NULL);
1646 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1647 llmv = &llu->llu_lmv;
1649 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1650 "still in using: %u\n",
1651 atomic_read(&llmv->ll_ref));
1653 lfsck_lmv_put(env, llmv);
1656 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1657 lfsck_component_cleanup(env, com);
1660 LASSERT(list_empty(&lfsck->li_list_dir));
1662 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1664 lfsck_component_cleanup(env, com);
1667 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1668 lfsck_component_cleanup(env, com);
1671 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1672 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1674 if (lfsck->li_lfsck_dir != NULL) {
1675 lfsck_object_put(env, lfsck->li_lfsck_dir);
1676 lfsck->li_lfsck_dir = NULL;
1679 if (lfsck->li_bookmark_obj != NULL) {
1680 lfsck_object_put(env, lfsck->li_bookmark_obj);
1681 lfsck->li_bookmark_obj = NULL;
1684 if (lfsck->li_lpf_obj != NULL) {
1685 lfsck_object_put(env, lfsck->li_lpf_obj);
1686 lfsck->li_lpf_obj = NULL;
1689 if (lfsck->li_lpf_root_obj != NULL) {
1690 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1691 lfsck->li_lpf_root_obj = NULL;
1694 if (lfsck->li_los != NULL) {
1695 local_oid_storage_fini(env, lfsck->li_los);
1696 lfsck->li_los = NULL;
1699 lfsck_fid_fini(lfsck);
1701 OBD_FREE_PTR(lfsck);
1704 static inline struct lfsck_instance *
1705 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1707 struct lfsck_instance *lfsck;
1709 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1710 if (lfsck->li_bottom == key) {
1712 lfsck_instance_get(lfsck);
1714 list_del_init(&lfsck->li_link);
1723 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1726 struct lfsck_instance *lfsck;
1728 spin_lock(&lfsck_instance_lock);
1729 lfsck = __lfsck_instance_find(key, ref, unlink);
1730 spin_unlock(&lfsck_instance_lock);
1735 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1737 struct lfsck_instance *tmp;
1739 spin_lock(&lfsck_instance_lock);
1740 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1741 if (lfsck->li_bottom == tmp->li_bottom) {
1742 spin_unlock(&lfsck_instance_lock);
1747 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1748 spin_unlock(&lfsck_instance_lock);
1752 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1757 bool newline = (bits != 0 ? false : true);
1760 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1764 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1767 if (names[i] != NULL) {
1771 rc = seq_printf(m, "%s%c", names[i],
1772 newline ? '\n' : ',');
1780 rc = seq_printf(m, "\n");
1785 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1790 rc = seq_printf(m, "%s_time: N/A\n", name);
1792 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1797 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1799 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1800 name, cfs_time_current_sec() - time);
1805 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1808 if (fid_is_zero(&pos->lp_dir_parent)) {
1809 if (pos->lp_oit_cookie == 0)
1810 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1812 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1813 prefix, pos->lp_oit_cookie);
1816 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1817 prefix, pos->lp_oit_cookie,
1818 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1821 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1822 struct lfsck_position *pos, bool init)
1824 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1826 if (unlikely(lfsck->li_di_oit == NULL)) {
1827 memset(pos, 0, sizeof(*pos));
1831 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1832 if (!lfsck->li_current_oit_processed && !init)
1833 pos->lp_oit_cookie--;
1835 LASSERT(pos->lp_oit_cookie > 0);
1837 if (lfsck->li_di_dir != NULL) {
1838 struct dt_object *dto = lfsck->li_obj_dir;
1840 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1843 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1844 fid_zero(&pos->lp_dir_parent);
1845 pos->lp_dir_cookie = 0;
1847 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1850 fid_zero(&pos->lp_dir_parent);
1851 pos->lp_dir_cookie = 0;
1855 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1859 if (limit != LFSCK_SPEED_NO_LIMIT) {
1860 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1861 lfsck->li_sleep_rate = limit /
1862 msecs_to_jiffies(MSEC_PER_SEC);
1863 lfsck->li_sleep_jif = 1;
1865 lfsck->li_sleep_rate = 1;
1866 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1870 lfsck->li_sleep_jif = 0;
1871 lfsck->li_sleep_rate = 0;
1874 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1875 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1882 void lfsck_control_speed(struct lfsck_instance *lfsck)
1884 struct ptlrpc_thread *thread = &lfsck->li_thread;
1885 struct l_wait_info lwi;
1887 if (lfsck->li_sleep_jif > 0 &&
1888 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1889 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1890 LWI_ON_SIGNAL_NOOP, NULL);
1892 l_wait_event(thread->t_ctl_waitq,
1893 !thread_is_running(thread),
1895 lfsck->li_new_scanned = 0;
1899 void lfsck_control_speed_by_self(struct lfsck_component *com)
1901 struct lfsck_instance *lfsck = com->lc_lfsck;
1902 struct ptlrpc_thread *thread = &lfsck->li_thread;
1903 struct l_wait_info lwi;
1905 if (lfsck->li_sleep_jif > 0 &&
1906 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1907 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1908 LWI_ON_SIGNAL_NOOP, NULL);
1910 l_wait_event(thread->t_ctl_waitq,
1911 !thread_is_running(thread),
1913 com->lc_new_scanned = 0;
1917 static struct lfsck_thread_args *
1918 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1919 struct lfsck_component *com,
1920 struct lfsck_start_param *lsp)
1922 struct lfsck_thread_args *lta;
1927 return ERR_PTR(-ENOMEM);
1929 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1935 lta->lta_lfsck = lfsck_instance_get(lfsck);
1937 lta->lta_com = lfsck_component_get(com);
1944 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1946 if (lta->lta_com != NULL)
1947 lfsck_component_put(<a->lta_env, lta->lta_com);
1948 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1949 lu_env_fini(<a->lta_env);
1953 struct lfsck_assistant_data *
1954 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1957 struct lfsck_assistant_data *lad;
1961 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1962 if (lad->lad_bitmap == NULL) {
1967 INIT_LIST_HEAD(&lad->lad_req_list);
1968 spin_lock_init(&lad->lad_lock);
1969 INIT_LIST_HEAD(&lad->lad_ost_list);
1970 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1971 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1972 INIT_LIST_HEAD(&lad->lad_mdt_list);
1973 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1974 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1975 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1977 lad->lad_name = name;
1983 struct lfsck_assistant_object *
1984 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1985 const struct lu_attr *attr, __u64 cookie,
1988 struct lfsck_assistant_object *lso;
1992 return ERR_PTR(-ENOMEM);
1994 lso->lso_fid = *fid;
1996 lso->lso_attr = *attr;
1998 atomic_set(&lso->lso_ref, 1);
1999 lso->lso_oit_cookie = cookie;
2001 lso->lso_is_dir = 1;
2007 lfsck_assistant_object_load(const struct lu_env *env,
2008 struct lfsck_instance *lfsck,
2009 struct lfsck_assistant_object *lso)
2011 struct dt_object *obj;
2013 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2017 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2019 lfsck_object_put(env, obj);
2021 return ERR_PTR(-ENOENT);
2024 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2025 lfsck_object_put(env, obj);
2027 return ERR_PTR(-ENOTDIR);
2034 * Generic LFSCK asynchronous communication interpretor function.
2035 * The LFSCK RPC reply for both the event notification and status
2036 * querying will be handled here.
2038 * \param[in] env pointer to the thread context
2039 * \param[in] req pointer to the LFSCK request
2040 * \param[in] args pointer to the lfsck_async_interpret_args
2041 * \param[in] rc the result for handling the LFSCK request
2043 * \retval 0 for success
2044 * \retval negative error number on failure
2046 int lfsck_async_interpret_common(const struct lu_env *env,
2047 struct ptlrpc_request *req,
2050 struct lfsck_async_interpret_args *laia = args;
2051 struct lfsck_component *com = laia->laia_com;
2052 struct lfsck_assistant_data *lad = com->lc_data;
2053 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2054 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2055 struct lfsck_request *lr = laia->laia_lr;
2057 LASSERT(com->lc_lfsck->li_master);
2059 switch (lr->lr_event) {
2062 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2064 lfsck_lfsck2name(com->lc_lfsck),
2065 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2066 ltd->ltd_index, lad->lad_name, rc);
2068 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2069 struct lfsck_layout *lo = com->lc_file_ram;
2071 if (lr->lr_flags & LEF_TO_OST)
2072 lfsck_lad_set_bitmap(env, com,
2075 lo->ll_flags |= LF_INCOMPLETE;
2077 struct lfsck_namespace *ns = com->lc_file_ram;
2079 /* If some MDT does not join the namespace
2080 * LFSCK, then we cannot know whether there
2081 * is some name entry on such MDT that with
2082 * the referenced MDT-object on this MDT or
2083 * not. So the namespace LFSCK on this MDT
2084 * cannot handle orphan MDT-objects properly.
2085 * So we mark the LFSCK as LF_INCOMPLETE and
2086 * skip orphan MDT-objects handling. */
2087 ns->ln_flags |= LF_INCOMPLETE;
2092 spin_lock(<ds->ltd_lock);
2093 if (ltd->ltd_dead) {
2094 spin_unlock(<ds->ltd_lock);
2098 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2099 struct list_head *list;
2100 struct list_head *phase_list;
2102 if (ltd->ltd_layout_done) {
2103 spin_unlock(<ds->ltd_lock);
2107 if (lr->lr_flags & LEF_TO_OST) {
2108 list = &lad->lad_ost_list;
2109 phase_list = &lad->lad_ost_phase1_list;
2111 list = &lad->lad_mdt_list;
2112 phase_list = &lad->lad_mdt_phase1_list;
2115 if (list_empty(<d->ltd_layout_list))
2116 list_add_tail(<d->ltd_layout_list, list);
2117 if (list_empty(<d->ltd_layout_phase_list))
2118 list_add_tail(<d->ltd_layout_phase_list,
2121 if (ltd->ltd_namespace_done) {
2122 spin_unlock(<ds->ltd_lock);
2126 if (list_empty(<d->ltd_namespace_list))
2127 list_add_tail(<d->ltd_namespace_list,
2128 &lad->lad_mdt_list);
2129 if (list_empty(<d->ltd_namespace_phase_list))
2130 list_add_tail(<d->ltd_namespace_phase_list,
2131 &lad->lad_mdt_phase1_list);
2133 spin_unlock(<ds->ltd_lock);
2136 case LE_PHASE1_DONE:
2137 case LE_PHASE2_DONE:
2139 if (rc != 0 && rc != -EALREADY)
2140 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2141 "event = %d, rc = %d\n",
2142 lfsck_lfsck2name(com->lc_lfsck),
2143 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2144 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2147 struct lfsck_reply *reply;
2148 struct list_head *list;
2149 struct list_head *phase_list;
2151 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2152 list = <d->ltd_layout_list;
2153 phase_list = <d->ltd_layout_phase_list;
2155 list = <d->ltd_namespace_list;
2156 phase_list = <d->ltd_namespace_phase_list;
2160 spin_lock(<ds->ltd_lock);
2161 list_del_init(phase_list);
2162 list_del_init(list);
2163 spin_unlock(<ds->ltd_lock);
2167 reply = req_capsule_server_get(&req->rq_pill,
2169 if (reply == NULL) {
2171 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2172 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2174 spin_lock(<ds->ltd_lock);
2175 list_del_init(phase_list);
2176 list_del_init(list);
2177 spin_unlock(<ds->ltd_lock);
2181 switch (reply->lr_status) {
2182 case LS_SCANNING_PHASE1:
2184 case LS_SCANNING_PHASE2:
2185 spin_lock(<ds->ltd_lock);
2186 list_del_init(phase_list);
2187 if (ltd->ltd_dead) {
2188 spin_unlock(<ds->ltd_lock);
2192 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2193 if (ltd->ltd_layout_done) {
2194 spin_unlock(<ds->ltd_lock);
2198 if (lr->lr_flags & LEF_TO_OST)
2199 list_add_tail(phase_list,
2200 &lad->lad_ost_phase2_list);
2202 list_add_tail(phase_list,
2203 &lad->lad_mdt_phase2_list);
2205 if (ltd->ltd_namespace_done) {
2206 spin_unlock(<ds->ltd_lock);
2210 list_add_tail(phase_list,
2211 &lad->lad_mdt_phase2_list);
2213 spin_unlock(<ds->ltd_lock);
2216 spin_lock(<ds->ltd_lock);
2217 list_del_init(phase_list);
2218 list_del_init(list);
2219 spin_unlock(<ds->ltd_lock);
2225 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2226 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2230 if (!laia->laia_shared) {
2232 lfsck_component_put(env, com);
2238 static void lfsck_interpret(const struct lu_env *env,
2239 struct lfsck_instance *lfsck,
2240 struct ptlrpc_request *req, void *args, int result)
2242 struct lfsck_async_interpret_args *laia = args;
2243 struct lfsck_component *com;
2245 LASSERT(laia->laia_com == NULL);
2246 LASSERT(laia->laia_shared);
2248 spin_lock(&lfsck->li_lock);
2249 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2250 laia->laia_com = com;
2251 lfsck_async_interpret_common(env, req, laia, result);
2254 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2255 laia->laia_com = com;
2256 lfsck_async_interpret_common(env, req, laia, result);
2258 spin_unlock(&lfsck->li_lock);
2261 static int lfsck_stop_notify(const struct lu_env *env,
2262 struct lfsck_instance *lfsck,
2263 struct lfsck_tgt_descs *ltds,
2264 struct lfsck_tgt_desc *ltd, __u16 type)
2266 struct lfsck_component *com;
2270 LASSERT(lfsck->li_master);
2272 spin_lock(&lfsck->li_lock);
2273 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2275 com = __lfsck_component_find(lfsck, type,
2276 &lfsck->li_list_double_scan);
2278 lfsck_component_get(com);
2279 spin_unlock(&lfsck->li_lock);
2282 struct lfsck_thread_info *info = lfsck_env_info(env);
2283 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2284 struct lfsck_request *lr = &info->lti_lr;
2285 struct lfsck_assistant_data *lad = com->lc_data;
2286 struct list_head *list;
2287 struct list_head *phase_list;
2288 struct ptlrpc_request_set *set;
2290 set = ptlrpc_prep_set();
2292 lfsck_component_put(env, com);
2297 if (type == LFSCK_TYPE_LAYOUT) {
2298 list = <d->ltd_layout_list;
2299 phase_list = <d->ltd_layout_phase_list;
2301 list = <d->ltd_namespace_list;
2302 phase_list = <d->ltd_namespace_phase_list;
2305 spin_lock(<ds->ltd_lock);
2306 if (list_empty(list)) {
2307 LASSERT(list_empty(phase_list));
2308 spin_unlock(<ds->ltd_lock);
2309 ptlrpc_set_destroy(set);
2314 list_del_init(phase_list);
2315 list_del_init(list);
2316 spin_unlock(<ds->ltd_lock);
2318 memset(lr, 0, sizeof(*lr));
2319 lr->lr_index = lfsck_dev_idx(lfsck);
2320 lr->lr_event = LE_PEER_EXIT;
2321 lr->lr_active = type;
2322 lr->lr_status = LS_CO_PAUSED;
2323 if (ltds == &lfsck->li_ost_descs)
2324 lr->lr_flags = LEF_TO_OST;
2326 laia->laia_com = com;
2327 laia->laia_ltds = ltds;
2328 atomic_inc(<d->ltd_ref);
2329 laia->laia_ltd = ltd;
2331 laia->laia_shared = 0;
2333 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2334 lfsck_async_interpret_common,
2335 laia, LFSCK_NOTIFY);
2337 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2338 "co-stop for %s: rc = %d\n",
2339 lfsck_lfsck2name(lfsck),
2340 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2341 ltd->ltd_index, lad->lad_name, rc);
2344 rc = ptlrpc_set_wait(set);
2347 ptlrpc_set_destroy(set);
2348 lfsck_component_put(env, com);
2354 static int lfsck_async_interpret(const struct lu_env *env,
2355 struct ptlrpc_request *req,
2358 struct lfsck_async_interpret_args *laia = args;
2359 struct lfsck_instance *lfsck;
2361 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2363 lfsck_interpret(env, lfsck, req, laia, rc);
2364 lfsck_tgt_put(laia->laia_ltd);
2365 if (rc != 0 && laia->laia_result != -EALREADY)
2366 laia->laia_result = rc;
2371 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2372 struct lfsck_request *lr,
2373 struct ptlrpc_request_set *set,
2374 ptlrpc_interpterer_t interpreter,
2375 void *args, int request)
2377 struct lfsck_async_interpret_args *laia;
2378 struct ptlrpc_request *req;
2379 struct lfsck_request *tmp;
2380 struct req_format *format;
2385 format = &RQF_LFSCK_NOTIFY;
2388 format = &RQF_LFSCK_QUERY;
2391 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2392 exp->exp_obd->obd_name, request, -EINVAL);
2396 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2400 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2402 ptlrpc_request_free(req);
2407 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2409 ptlrpc_request_set_replen(req);
2411 laia = ptlrpc_req_async_args(req);
2412 *laia = *(struct lfsck_async_interpret_args *)args;
2413 if (laia->laia_com != NULL)
2414 lfsck_component_get(laia->laia_com);
2415 req->rq_interpret_reply = interpreter;
2416 ptlrpc_set_add_req(set, req);
2421 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2422 struct lfsck_start_param *lsp)
2424 struct lfsck_instance *lfsck = com->lc_lfsck;
2425 struct lfsck_assistant_data *lad = com->lc_data;
2426 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2427 struct ptlrpc_thread *athread = &lad->lad_thread;
2428 struct lfsck_thread_args *lta;
2429 struct task_struct *task;
2433 lad->lad_assistant_status = 0;
2434 lad->lad_post_result = 0;
2435 lad->lad_to_post = 0;
2436 lad->lad_to_double_scan = 0;
2437 lad->lad_in_double_scan = 0;
2439 lad->lad_advance_lock = false;
2440 thread_set_flags(athread, 0);
2442 lta = lfsck_thread_args_init(lfsck, com, lsp);
2444 RETURN(PTR_ERR(lta));
2446 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2449 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2450 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2451 lfsck_thread_args_fini(lta);
2453 struct l_wait_info lwi = { 0 };
2455 l_wait_event(mthread->t_ctl_waitq,
2456 thread_is_running(athread) ||
2457 thread_is_stopped(athread),
2459 if (unlikely(!thread_is_running(athread)))
2460 rc = lad->lad_assistant_status;
2468 int lfsck_checkpoint_generic(const struct lu_env *env,
2469 struct lfsck_component *com)
2471 struct lfsck_assistant_data *lad = com->lc_data;
2472 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2473 struct ptlrpc_thread *athread = &lad->lad_thread;
2474 struct l_wait_info lwi = { 0 };
2476 l_wait_event(mthread->t_ctl_waitq,
2477 list_empty(&lad->lad_req_list) ||
2478 !thread_is_running(mthread) ||
2479 thread_is_stopped(athread),
2482 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2483 return LFSCK_CHECKPOINT_SKIP;
2488 void lfsck_post_generic(const struct lu_env *env,
2489 struct lfsck_component *com, int *result)
2491 struct lfsck_assistant_data *lad = com->lc_data;
2492 struct ptlrpc_thread *athread = &lad->lad_thread;
2493 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2494 struct l_wait_info lwi = { 0 };
2496 lad->lad_post_result = *result;
2499 lad->lad_to_post = 1;
2501 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2502 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2504 wake_up_all(&athread->t_ctl_waitq);
2505 l_wait_event(mthread->t_ctl_waitq,
2506 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2507 thread_is_stopped(athread),
2510 if (lad->lad_assistant_status < 0)
2511 *result = lad->lad_assistant_status;
2513 CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2514 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2517 int lfsck_double_scan_generic(const struct lu_env *env,
2518 struct lfsck_component *com, int status)
2520 struct lfsck_assistant_data *lad = com->lc_data;
2521 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2522 struct ptlrpc_thread *athread = &lad->lad_thread;
2523 struct l_wait_info lwi = { 0 };
2525 if (status != LS_SCANNING_PHASE2)
2528 lad->lad_to_double_scan = 1;
2530 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2532 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2534 wake_up_all(&athread->t_ctl_waitq);
2535 l_wait_event(mthread->t_ctl_waitq,
2536 lad->lad_in_double_scan ||
2537 thread_is_stopped(athread),
2540 CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2541 "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2542 lad->lad_assistant_status);
2544 if (lad->lad_assistant_status < 0)
2545 return lad->lad_assistant_status;
2550 void lfsck_quit_generic(const struct lu_env *env,
2551 struct lfsck_component *com)
2553 struct lfsck_assistant_data *lad = com->lc_data;
2554 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2555 struct ptlrpc_thread *athread = &lad->lad_thread;
2556 struct l_wait_info lwi = { 0 };
2559 wake_up_all(&athread->t_ctl_waitq);
2560 l_wait_event(mthread->t_ctl_waitq,
2561 thread_is_init(athread) ||
2562 thread_is_stopped(athread),
2566 /* external interfaces */
2568 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2571 struct lfsck_instance *lfsck;
2575 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2579 lfsck = lfsck_instance_find(key, true, false);
2580 if (likely(lfsck != NULL)) {
2581 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2582 lfsck_instance_put(&env, lfsck);
2591 EXPORT_SYMBOL(lfsck_get_speed);
2593 int lfsck_set_speed(struct dt_device *key, int val)
2596 struct lfsck_instance *lfsck;
2600 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2604 lfsck = lfsck_instance_find(key, true, false);
2605 if (likely(lfsck != NULL)) {
2606 mutex_lock(&lfsck->li_mutex);
2607 if (__lfsck_set_speed(lfsck, val))
2608 rc = lfsck_bookmark_store(&env, lfsck);
2609 mutex_unlock(&lfsck->li_mutex);
2610 lfsck_instance_put(&env, lfsck);
2619 EXPORT_SYMBOL(lfsck_set_speed);
2621 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2624 struct lfsck_instance *lfsck;
2628 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2632 lfsck = lfsck_instance_find(key, true, false);
2633 if (likely(lfsck != NULL)) {
2634 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2635 lfsck_instance_put(&env, lfsck);
2644 EXPORT_SYMBOL(lfsck_get_windows);
2646 int lfsck_set_windows(struct dt_device *key, int val)
2649 struct lfsck_instance *lfsck;
2653 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2657 lfsck = lfsck_instance_find(key, true, false);
2658 if (likely(lfsck != NULL)) {
2659 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2660 CWARN("%s: invalid async windows size that may "
2661 "cause memory issues. The valid range is "
2663 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2665 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2666 mutex_lock(&lfsck->li_mutex);
2667 lfsck->li_bookmark_ram.lb_async_windows = val;
2668 rc = lfsck_bookmark_store(&env, lfsck);
2669 mutex_unlock(&lfsck->li_mutex);
2671 lfsck_instance_put(&env, lfsck);
2680 EXPORT_SYMBOL(lfsck_set_windows);
2682 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2685 struct lfsck_instance *lfsck;
2686 struct lfsck_component *com;
2690 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2694 lfsck = lfsck_instance_find(key, true, false);
2695 if (likely(lfsck != NULL)) {
2696 com = lfsck_component_find(lfsck, type);
2697 if (likely(com != NULL)) {
2698 rc = com->lc_ops->lfsck_dump(&env, com, m);
2699 lfsck_component_put(&env, com);
2704 lfsck_instance_put(&env, lfsck);
2713 EXPORT_SYMBOL(lfsck_dump);
2715 static int lfsck_stop_all(const struct lu_env *env,
2716 struct lfsck_instance *lfsck,
2717 struct lfsck_stop *stop)
2719 struct lfsck_thread_info *info = lfsck_env_info(env);
2720 struct lfsck_request *lr = &info->lti_lr;
2721 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2722 struct ptlrpc_request_set *set;
2723 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2724 struct lfsck_tgt_desc *ltd;
2725 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2731 LASSERT(stop->ls_flags & LPF_BROADCAST);
2733 set = ptlrpc_prep_set();
2734 if (unlikely(set == NULL))
2737 memset(lr, 0, sizeof(*lr));
2738 lr->lr_event = LE_STOP;
2739 lr->lr_index = lfsck_dev_idx(lfsck);
2740 lr->lr_status = stop->ls_status;
2741 lr->lr_version = bk->lb_version;
2742 lr->lr_active = LFSCK_TYPES_ALL;
2743 lr->lr_param = stop->ls_flags;
2745 laia->laia_com = NULL;
2746 laia->laia_ltds = ltds;
2748 laia->laia_result = 0;
2749 laia->laia_shared = 1;
2751 down_read(<ds->ltd_rw_sem);
2752 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2753 ltd = lfsck_tgt_get(ltds, idx);
2754 LASSERT(ltd != NULL);
2756 laia->laia_ltd = ltd;
2757 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2758 lfsck_async_interpret, laia,
2761 lfsck_interpret(env, lfsck, NULL, laia, rc);
2763 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2764 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2768 up_read(<ds->ltd_rw_sem);
2770 rc = ptlrpc_set_wait(set);
2771 ptlrpc_set_destroy(set);
2774 rc = laia->laia_result;
2776 if (rc == -EALREADY)
2780 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2781 lfsck_lfsck2name(lfsck), rc);
2783 RETURN(rc != 0 ? rc : rc1);
2786 static int lfsck_start_all(const struct lu_env *env,
2787 struct lfsck_instance *lfsck,
2788 struct lfsck_start *start)
2790 struct lfsck_thread_info *info = lfsck_env_info(env);
2791 struct lfsck_request *lr = &info->lti_lr;
2792 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2793 struct ptlrpc_request_set *set;
2794 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2795 struct lfsck_tgt_desc *ltd;
2796 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2801 LASSERT(start->ls_flags & LPF_BROADCAST);
2803 set = ptlrpc_prep_set();
2804 if (unlikely(set == NULL))
2807 memset(lr, 0, sizeof(*lr));
2808 lr->lr_event = LE_START;
2809 lr->lr_index = lfsck_dev_idx(lfsck);
2810 lr->lr_speed = bk->lb_speed_limit;
2811 lr->lr_version = bk->lb_version;
2812 lr->lr_active = start->ls_active;
2813 lr->lr_param = start->ls_flags;
2814 lr->lr_async_windows = bk->lb_async_windows;
2815 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2816 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2819 laia->laia_com = NULL;
2820 laia->laia_ltds = ltds;
2822 laia->laia_result = 0;
2823 laia->laia_shared = 1;
2825 down_read(<ds->ltd_rw_sem);
2826 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2827 ltd = lfsck_tgt_get(ltds, idx);
2828 LASSERT(ltd != NULL);
2830 laia->laia_ltd = ltd;
2831 ltd->ltd_layout_done = 0;
2832 ltd->ltd_namespace_done = 0;
2833 ltd->ltd_synced_failures = 0;
2834 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2835 lfsck_async_interpret, laia,
2838 lfsck_interpret(env, lfsck, NULL, laia, rc);
2840 CERROR("%s: cannot notify MDT %x for LFSCK "
2841 "start, failout: rc = %d\n",
2842 lfsck_lfsck2name(lfsck), idx, rc);
2846 up_read(<ds->ltd_rw_sem);
2849 ptlrpc_set_destroy(set);
2854 rc = ptlrpc_set_wait(set);
2855 ptlrpc_set_destroy(set);
2858 rc = laia->laia_result;
2861 struct lfsck_stop *stop = &info->lti_stop;
2863 CERROR("%s: cannot start LFSCK on some MDTs, "
2864 "stop all: rc = %d\n",
2865 lfsck_lfsck2name(lfsck), rc);
2866 if (rc != -EALREADY) {
2867 stop->ls_status = LS_FAILED;
2868 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2869 lfsck_stop_all(env, lfsck, stop);
2876 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2877 struct lfsck_start_param *lsp)
2879 struct lfsck_start *start = lsp->lsp_start;
2880 struct lfsck_instance *lfsck;
2881 struct lfsck_bookmark *bk;
2882 struct ptlrpc_thread *thread;
2883 struct lfsck_component *com;
2884 struct l_wait_info lwi = { 0 };
2885 struct lfsck_thread_args *lta;
2886 struct task_struct *task;
2893 lfsck = lfsck_instance_find(key, true, false);
2894 if (unlikely(lfsck == NULL))
2897 /* System is not ready, try again later. */
2898 if (unlikely(lfsck->li_namespace == NULL))
2899 GOTO(put, rc = -EAGAIN);
2901 /* start == NULL means auto trigger paused LFSCK. */
2902 if ((start == NULL) &&
2903 (list_empty(&lfsck->li_list_scan) ||
2904 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2907 bk = &lfsck->li_bookmark_ram;
2908 thread = &lfsck->li_thread;
2909 mutex_lock(&lfsck->li_mutex);
2910 spin_lock(&lfsck->li_lock);
2911 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2913 if (unlikely(start == NULL)) {
2914 spin_unlock(&lfsck->li_lock);
2918 while (start->ls_active != 0) {
2919 if (!(type & start->ls_active)) {
2924 com = __lfsck_component_find(lfsck, type,
2925 &lfsck->li_list_scan);
2927 com = __lfsck_component_find(lfsck, type,
2928 &lfsck->li_list_double_scan);
2934 if (com->lc_ops->lfsck_join != NULL) {
2935 rc = com->lc_ops->lfsck_join( env, com, lsp);
2936 if (rc != 0 && rc != -EALREADY)
2939 start->ls_active &= ~type;
2942 spin_unlock(&lfsck->li_lock);
2945 spin_unlock(&lfsck->li_lock);
2947 lfsck->li_status = 0;
2948 lfsck->li_oit_over = 0;
2949 lfsck->li_start_unplug = 0;
2950 lfsck->li_drop_dryrun = 0;
2951 lfsck->li_new_scanned = 0;
2953 /* For auto trigger. */
2957 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2958 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2959 lfsck_lfsck2name(lfsck));
2961 GOTO(out, rc = -EPERM);
2964 start->ls_version = bk->lb_version;
2966 if (start->ls_active != 0) {
2967 struct lfsck_component *next;
2969 if (start->ls_active == LFSCK_TYPES_ALL)
2970 start->ls_active = LFSCK_TYPES_SUPPORTED;
2972 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2973 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2974 GOTO(out, rc = -ENOTSUPP);
2977 list_for_each_entry_safe(com, next,
2978 &lfsck->li_list_scan, lc_link) {
2979 if (!(com->lc_type & start->ls_active)) {
2980 rc = com->lc_ops->lfsck_post(env, com, 0,
2987 while (start->ls_active != 0) {
2988 if (type & start->ls_active) {
2989 com = __lfsck_component_find(lfsck, type,
2990 &lfsck->li_list_idle);
2992 /* The component status will be updated
2993 * when its prep() is called later by
2994 * the LFSCK main engine. */
2995 list_move_tail(&com->lc_link,
2996 &lfsck->li_list_scan);
2997 start->ls_active &= ~type;
3003 if (list_empty(&lfsck->li_list_scan)) {
3004 /* The speed limit will be used to control both the LFSCK and
3005 * low layer scrub (if applied), need to be handled firstly. */
3006 if (start->ls_valid & LSV_SPEED_LIMIT) {
3007 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3008 rc = lfsck_bookmark_store(env, lfsck);
3017 if (start->ls_flags & LPF_RESET)
3018 flags |= DOIF_RESET;
3020 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3024 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3025 start->ls_active |= com->lc_type;
3026 if (flags & DOIF_RESET) {
3027 rc = com->lc_ops->lfsck_reset(env, com, false);
3034 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3035 if (bk->lb_param & LPF_DRYRUN)
3036 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3038 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3039 valid |= DOIV_ERROR_HANDLE;
3040 if (start->ls_flags & LPF_FAILOUT)
3041 flags |= DOIF_FAILOUT;
3044 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3045 valid |= DOIV_DRYRUN;
3046 if (start->ls_flags & LPF_DRYRUN)
3047 flags |= DOIF_DRYRUN;
3050 if (!list_empty(&lfsck->li_list_scan))
3051 flags |= DOIF_OUTUSED;
3053 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3054 thread_set_flags(thread, 0);
3055 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3057 GOTO(out, rc = PTR_ERR(lta));
3059 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3060 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3063 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3064 lfsck_lfsck2name(lfsck), rc);
3065 lfsck_thread_args_fini(lta);
3070 l_wait_event(thread->t_ctl_waitq,
3071 thread_is_running(thread) ||
3072 thread_is_stopped(thread),
3074 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3075 lfsck->li_start_unplug = 1;
3076 wake_up_all(&thread->t_ctl_waitq);
3081 /* release lfsck::li_mutex to avoid deadlock. */
3082 mutex_unlock(&lfsck->li_mutex);
3083 rc = lfsck_start_all(env, lfsck, start);
3085 spin_lock(&lfsck->li_lock);
3086 if (thread_is_stopped(thread)) {
3087 spin_unlock(&lfsck->li_lock);
3089 lfsck->li_status = LS_FAILED;
3090 lfsck->li_flags = 0;
3091 thread_set_flags(thread, SVC_STOPPING);
3092 spin_unlock(&lfsck->li_lock);
3094 lfsck->li_start_unplug = 1;
3095 wake_up_all(&thread->t_ctl_waitq);
3096 l_wait_event(thread->t_ctl_waitq,
3097 thread_is_stopped(thread),
3101 lfsck->li_start_unplug = 1;
3102 wake_up_all(&thread->t_ctl_waitq);
3108 mutex_unlock(&lfsck->li_mutex);
3111 lfsck_instance_put(env, lfsck);
3113 return rc < 0 ? rc : 0;
3115 EXPORT_SYMBOL(lfsck_start);
3117 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3118 struct lfsck_stop *stop)
3120 struct lfsck_instance *lfsck;
3121 struct ptlrpc_thread *thread;
3122 struct l_wait_info lwi = { 0 };
3127 lfsck = lfsck_instance_find(key, true, false);
3128 if (unlikely(lfsck == NULL))
3131 thread = &lfsck->li_thread;
3132 /* release lfsck::li_mutex to avoid deadlock. */
3133 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3134 if (!lfsck->li_master) {
3135 CERROR("%s: only allow to specify '-A' via MDS\n",
3136 lfsck_lfsck2name(lfsck));
3138 GOTO(out, rc = -EPERM);
3141 rc1 = lfsck_stop_all(env, lfsck, stop);
3144 mutex_lock(&lfsck->li_mutex);
3145 spin_lock(&lfsck->li_lock);
3146 /* no error if LFSCK is already stopped, or was never started */
3147 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3148 spin_unlock(&lfsck->li_lock);
3153 lfsck->li_status = stop->ls_status;
3154 lfsck->li_flags = stop->ls_flags;
3156 lfsck->li_status = LS_STOPPED;
3157 lfsck->li_flags = 0;
3160 thread_set_flags(thread, SVC_STOPPING);
3162 if (lfsck->li_master) {
3163 struct lfsck_component *com;
3164 struct lfsck_assistant_data *lad;
3166 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3168 spin_lock(&lad->lad_lock);
3169 if (lad->lad_task != NULL)
3170 force_sig(SIGINT, lad->lad_task);
3171 spin_unlock(&lad->lad_lock);
3174 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3176 spin_lock(&lad->lad_lock);
3177 if (lad->lad_task != NULL)
3178 force_sig(SIGINT, lad->lad_task);
3179 spin_unlock(&lad->lad_lock);
3183 spin_unlock(&lfsck->li_lock);
3185 wake_up_all(&thread->t_ctl_waitq);
3186 l_wait_event(thread->t_ctl_waitq,
3187 thread_is_stopped(thread),
3193 mutex_unlock(&lfsck->li_mutex);
3194 lfsck_instance_put(env, lfsck);
3196 return rc != 0 ? rc : rc1;
3198 EXPORT_SYMBOL(lfsck_stop);
3200 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3201 struct lfsck_request *lr, struct thandle *th)
3203 int rc = -EOPNOTSUPP;
3206 switch (lr->lr_event) {
3208 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3209 struct lfsck_start_param lsp;
3211 memset(start, 0, sizeof(*start));
3212 start->ls_valid = lr->lr_valid;
3213 start->ls_speed_limit = lr->lr_speed;
3214 start->ls_version = lr->lr_version;
3215 start->ls_active = lr->lr_active;
3216 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3217 start->ls_async_windows = lr->lr_async_windows;
3219 lsp.lsp_start = start;
3220 lsp.lsp_index = lr->lr_index;
3221 lsp.lsp_index_valid = 1;
3222 rc = lfsck_start(env, key, &lsp);
3226 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3228 memset(stop, 0, sizeof(*stop));
3229 stop->ls_status = lr->lr_status;
3230 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3231 rc = lfsck_stop(env, key, stop);
3234 case LE_PHASE1_DONE:
3235 case LE_PHASE2_DONE:
3236 case LE_FID_ACCESSED:
3238 case LE_CONDITIONAL_DESTROY:
3239 case LE_SKIP_NLINK_DECLARE:
3241 case LE_SET_LMV_MASTER:
3242 case LE_SET_LMV_SLAVE:
3243 case LE_PAIRS_VERIFY: {
3244 struct lfsck_instance *lfsck;
3245 struct lfsck_component *com;
3247 lfsck = lfsck_instance_find(key, true, false);
3248 if (unlikely(lfsck == NULL))
3251 com = lfsck_component_find(lfsck, lr->lr_active);
3252 if (likely(com != NULL)) {
3253 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3254 lfsck_component_put(env, com);
3257 lfsck_instance_put(env, lfsck);
3266 EXPORT_SYMBOL(lfsck_in_notify);
3268 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3269 struct lfsck_request *lr)
3271 struct lfsck_instance *lfsck;
3272 struct lfsck_component *com;
3276 lfsck = lfsck_instance_find(key, true, false);
3277 if (unlikely(lfsck == NULL))
3280 com = lfsck_component_find(lfsck, lr->lr_active);
3281 if (likely(com != NULL)) {
3282 rc = com->lc_ops->lfsck_query(env, com);
3283 lfsck_component_put(env, com);
3288 lfsck_instance_put(env, lfsck);
3293 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3294 struct ldlm_namespace *ns)
3296 struct lfsck_instance *lfsck;
3299 lfsck = lfsck_instance_find(key, true, false);
3300 if (likely(lfsck != NULL)) {
3301 lfsck->li_namespace = ns;
3302 lfsck_instance_put(env, lfsck);
3308 EXPORT_SYMBOL(lfsck_register_namespace);
3310 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3311 struct dt_device *next, struct obd_device *obd,
3312 lfsck_out_notify notify, void *notify_data, bool master)
3314 struct lfsck_instance *lfsck;
3315 struct dt_object *root = NULL;
3316 struct dt_object *obj = NULL;
3317 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3321 lfsck = lfsck_instance_find(key, false, false);
3322 if (unlikely(lfsck != NULL))
3325 OBD_ALLOC_PTR(lfsck);
3329 mutex_init(&lfsck->li_mutex);
3330 spin_lock_init(&lfsck->li_lock);
3331 INIT_LIST_HEAD(&lfsck->li_link);
3332 INIT_LIST_HEAD(&lfsck->li_list_scan);
3333 INIT_LIST_HEAD(&lfsck->li_list_dir);
3334 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3335 INIT_LIST_HEAD(&lfsck->li_list_idle);
3336 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3337 atomic_set(&lfsck->li_ref, 1);
3338 atomic_set(&lfsck->li_double_scan_count, 0);
3339 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3340 lfsck->li_out_notify = notify;
3341 lfsck->li_out_notify_data = notify_data;
3342 lfsck->li_next = next;
3343 lfsck->li_bottom = key;
3344 lfsck->li_obd = obd;
3346 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3350 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3354 fid->f_seq = FID_SEQ_LOCAL_NAME;
3357 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3361 rc = dt_root_get(env, key, fid);
3365 root = dt_locate(env, key, fid);
3367 GOTO(out, rc = PTR_ERR(root));
3369 if (unlikely(!dt_try_as_dir(env, root)))
3370 GOTO(out, rc = -ENOTDIR);
3372 lfsck->li_local_root_fid = *fid;
3374 lfsck->li_master = 1;
3375 if (lfsck_dev_idx(lfsck) == 0) {
3376 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3377 const struct lu_name *cname;
3379 rc = dt_lookup(env, root,
3380 (struct dt_rec *)(&lfsck->li_global_root_fid),
3381 (const struct dt_key *)"ROOT");
3385 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3387 GOTO(out, rc = PTR_ERR(obj));
3389 if (unlikely(!dt_try_as_dir(env, obj)))
3390 GOTO(out, rc = -ENOTDIR);
3392 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3393 (const struct dt_key *)dotlustre);
3397 lfsck_object_put(env, obj);
3398 obj = dt_locate(env, key, fid);
3400 GOTO(out, rc = PTR_ERR(obj));
3402 cname = lfsck_name_get_const(env, dotlustre,
3404 rc = lfsck_verify_linkea(env, obj, cname,
3405 &lfsck->li_global_root_fid);
3409 if (unlikely(!dt_try_as_dir(env, obj)))
3410 GOTO(out, rc = -ENOTDIR);
3413 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3414 (const struct dt_key *)lostfound);
3418 lfsck_object_put(env, obj);
3419 obj = dt_locate(env, key, fid);
3421 GOTO(out, rc = PTR_ERR(obj));
3423 cname = lfsck_name_get_const(env, lostfound,
3425 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3429 lfsck_object_put(env, obj);
3434 fid->f_seq = FID_SEQ_LOCAL_FILE;
3435 fid->f_oid = OTABLE_IT_OID;
3437 obj = dt_locate(env, key, fid);
3439 GOTO(out, rc = PTR_ERR(obj));
3441 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3445 lfsck->li_obj_oit = obj;
3446 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3447 S_IFDIR | S_IRUGO | S_IWUSR);
3449 GOTO(out, rc = PTR_ERR(obj));
3451 lu_object_get(&obj->do_lu);
3452 lfsck->li_lfsck_dir = obj;
3453 rc = lfsck_bookmark_setup(env, lfsck);
3458 rc = lfsck_fid_init(lfsck);
3462 rc = lfsck_namespace_setup(env, lfsck);
3467 rc = lfsck_layout_setup(env, lfsck);
3471 /* XXX: more LFSCK components initialization to be added here. */
3473 rc = lfsck_instance_add(lfsck);
3475 rc = lfsck_add_target_from_orphan(env, lfsck);
3477 if (obj != NULL && !IS_ERR(obj))
3478 lfsck_object_put(env, obj);
3479 if (root != NULL && !IS_ERR(root))
3480 lfsck_object_put(env, root);
3482 lfsck_instance_cleanup(env, lfsck);
3485 EXPORT_SYMBOL(lfsck_register);
3487 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3489 struct lfsck_instance *lfsck;
3491 lfsck = lfsck_instance_find(key, false, true);
3493 lfsck_instance_put(env, lfsck);
3495 EXPORT_SYMBOL(lfsck_degister);
3497 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3498 struct dt_device *tgt, struct obd_export *exp,
3499 __u32 index, bool for_ost)
3501 struct lfsck_instance *lfsck;
3502 struct lfsck_tgt_desc *ltd;
3513 INIT_LIST_HEAD(<d->ltd_orphan_list);
3514 INIT_LIST_HEAD(<d->ltd_layout_list);
3515 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3516 INIT_LIST_HEAD(<d->ltd_namespace_list);
3517 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3518 atomic_set(<d->ltd_ref, 1);
3519 ltd->ltd_index = index;
3521 spin_lock(&lfsck_instance_lock);
3522 lfsck = __lfsck_instance_find(key, true, false);
3523 if (lfsck == NULL) {
3525 list_add_tail(<d->ltd_orphan_list,
3526 &lfsck_ost_orphan_list);
3528 list_add_tail(<d->ltd_orphan_list,
3529 &lfsck_mdt_orphan_list);
3530 spin_unlock(&lfsck_instance_lock);
3534 spin_unlock(&lfsck_instance_lock);
3536 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3540 lfsck_instance_put(env, lfsck);
3544 EXPORT_SYMBOL(lfsck_add_target);
3546 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3547 struct dt_device *tgt, __u32 index, bool for_ost)
3549 struct lfsck_instance *lfsck;
3550 struct lfsck_tgt_descs *ltds;
3551 struct lfsck_tgt_desc *ltd;
3552 struct list_head *head;
3555 head = &lfsck_ost_orphan_list;
3557 head = &lfsck_mdt_orphan_list;
3559 spin_lock(&lfsck_instance_lock);
3560 list_for_each_entry(ltd, head, ltd_orphan_list) {
3561 if (ltd->ltd_tgt == tgt) {
3562 list_del_init(<d->ltd_orphan_list);
3563 spin_unlock(&lfsck_instance_lock);
3571 lfsck = __lfsck_instance_find(key, true, false);
3572 spin_unlock(&lfsck_instance_lock);
3573 if (unlikely(lfsck == NULL))
3577 ltds = &lfsck->li_ost_descs;
3579 ltds = &lfsck->li_mdt_descs;
3581 down_write(<ds->ltd_rw_sem);
3582 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3584 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3587 ltd = lfsck_ltd2tgt(ltds, index);
3588 if (unlikely(ltd == NULL))
3591 LASSERT(ltds->ltd_tgtnr > 0);
3594 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3595 lfsck_assign_tgt(ltds, NULL, index);
3600 head = &lfsck->li_ost_descs.ltd_orphan;
3602 head = &lfsck->li_mdt_descs.ltd_orphan;
3604 list_for_each_entry(ltd, head, ltd_orphan_list) {
3605 if (ltd->ltd_tgt == tgt) {
3606 list_del_init(<d->ltd_orphan_list);
3612 up_write(<ds->ltd_rw_sem);
3614 spin_lock(<ds->ltd_lock);
3616 spin_unlock(<ds->ltd_lock);
3617 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3618 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3622 lfsck_instance_put(env, lfsck);
3624 EXPORT_SYMBOL(lfsck_del_target);
3626 static int __init lfsck_init(void)
3630 INIT_LIST_HEAD(&lfsck_instance_list);
3631 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3632 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3633 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3634 rc = lu_context_key_register(&lfsck_thread_key);
3636 tgt_register_lfsck_in_notify(lfsck_in_notify);
3637 tgt_register_lfsck_query(lfsck_query);
3643 static void __exit lfsck_exit(void)
3645 struct lfsck_tgt_desc *ltd;
3646 struct lfsck_tgt_desc *next;
3648 LASSERT(list_empty(&lfsck_instance_list));
3650 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3652 list_del_init(<d->ltd_orphan_list);
3656 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3658 list_del_init(<d->ltd_orphan_list);
3662 lu_context_key_degister(&lfsck_thread_key);
3665 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3666 MODULE_DESCRIPTION("Lustre File System Checker");
3667 MODULE_VERSION(LUSTRE_VERSION_STRING);
3668 MODULE_LICENSE("GPL");
3670 module_init(lfsck_init);
3671 module_exit(lfsck_exit);