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 <linux/kthread.h>
34 #include <libcfs/list.h>
35 #include <lu_object.h>
36 #include <dt_object.h>
37 #include <md_object.h>
38 #include <lustre_fld.h>
39 #include <lustre_lib.h>
40 #include <lustre_net.h>
41 #include <lustre_lfsck.h>
42 #include <lustre/lustre_lfsck_user.h>
44 #include "lfsck_internal.h"
46 #define LFSCK_CHECKPOINT_SKIP 1
48 /* define lfsck thread key */
49 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
51 static void lfsck_key_fini(const struct lu_context *ctx,
52 struct lu_context_key *key, void *data)
54 struct lfsck_thread_info *info = data;
56 lu_buf_free(&info->lti_linkea_buf);
57 lu_buf_free(&info->lti_linkea_buf2);
58 lu_buf_free(&info->lti_big_buf);
62 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
63 LU_KEY_INIT_GENERIC(lfsck);
65 static struct list_head lfsck_instance_list;
66 static struct list_head lfsck_ost_orphan_list;
67 static struct list_head lfsck_mdt_orphan_list;
68 static DEFINE_SPINLOCK(lfsck_instance_lock);
70 static const char *lfsck_status_names[] = {
72 [LS_SCANNING_PHASE1] = "scanning-phase1",
73 [LS_SCANNING_PHASE2] = "scanning-phase2",
74 [LS_COMPLETED] = "completed",
75 [LS_FAILED] = "failed",
76 [LS_STOPPED] = "stopped",
77 [LS_PAUSED] = "paused",
78 [LS_CRASHED] = "crashed",
79 [LS_PARTIAL] = "partial",
80 [LS_CO_FAILED] = "co-failed",
81 [LS_CO_STOPPED] = "co-stopped",
82 [LS_CO_PAUSED] = "co-paused"
85 const char *lfsck_flags_names[] = {
94 const char *lfsck_param_names[] = {
106 enum lfsck_verify_lpf_types {
107 LVLT_BY_BOOKMARK = 0,
108 LVLT_BY_NAMEENTRY = 1,
111 const char *lfsck_status2names(enum lfsck_status status)
113 if (unlikely(status < 0 || status >= LS_MAX))
116 return lfsck_status_names[status];
119 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
121 spin_lock_init(<ds->ltd_lock);
122 init_rwsem(<ds->ltd_rw_sem);
123 INIT_LIST_HEAD(<ds->ltd_orphan);
124 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
125 if (ltds->ltd_tgts_bitmap == NULL)
131 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
133 struct lfsck_tgt_desc *ltd;
134 struct lfsck_tgt_desc *next;
137 down_write(<ds->ltd_rw_sem);
139 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
141 list_del_init(<d->ltd_orphan_list);
145 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
146 up_write(<ds->ltd_rw_sem);
151 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
152 ltd = LTD_TGT(ltds, idx);
153 if (likely(ltd != NULL)) {
154 LASSERT(list_empty(<d->ltd_layout_list));
155 LASSERT(list_empty(<d->ltd_layout_phase_list));
156 LASSERT(list_empty(<d->ltd_namespace_list));
157 LASSERT(list_empty(<d->ltd_namespace_phase_list));
160 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
161 LTD_TGT(ltds, idx) = NULL;
166 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
169 for (idx = 0; idx < TGT_PTRS; idx++) {
170 if (ltds->ltd_tgts_idx[idx] != NULL) {
171 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
172 ltds->ltd_tgts_idx[idx] = NULL;
176 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
177 ltds->ltd_tgts_bitmap = NULL;
178 up_write(<ds->ltd_rw_sem);
181 static int __lfsck_add_target(const struct lu_env *env,
182 struct lfsck_instance *lfsck,
183 struct lfsck_tgt_desc *ltd,
184 bool for_ost, bool locked)
186 struct lfsck_tgt_descs *ltds;
187 __u32 index = ltd->ltd_index;
192 ltds = &lfsck->li_ost_descs;
194 ltds = &lfsck->li_mdt_descs;
197 down_write(<ds->ltd_rw_sem);
199 LASSERT(ltds->ltd_tgts_bitmap != NULL);
201 if (index >= ltds->ltd_tgts_bitmap->size) {
202 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
203 (__u32)BITS_PER_LONG);
204 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
205 cfs_bitmap_t *new_bitmap;
207 while (newsize < index + 1)
210 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
211 if (new_bitmap == NULL)
212 GOTO(unlock, rc = -ENOMEM);
214 if (ltds->ltd_tgtnr > 0)
215 cfs_bitmap_copy(new_bitmap, old_bitmap);
216 ltds->ltd_tgts_bitmap = new_bitmap;
217 CFS_FREE_BITMAP(old_bitmap);
220 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
221 CERROR("%s: the device %s (%u) is registered already\n",
222 lfsck_lfsck2name(lfsck),
223 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
224 GOTO(unlock, rc = -EEXIST);
227 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
228 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
229 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
230 GOTO(unlock, rc = -ENOMEM);
233 LTD_TGT(ltds, index) = ltd;
234 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
237 GOTO(unlock, rc = 0);
241 up_write(<ds->ltd_rw_sem);
246 static int lfsck_add_target_from_orphan(const struct lu_env *env,
247 struct lfsck_instance *lfsck)
249 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
250 struct lfsck_tgt_desc *ltd;
251 struct lfsck_tgt_desc *next;
252 struct list_head *head = &lfsck_ost_orphan_list;
257 spin_lock(&lfsck_instance_lock);
258 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
259 if (ltd->ltd_key == lfsck->li_bottom)
260 list_move_tail(<d->ltd_orphan_list,
263 spin_unlock(&lfsck_instance_lock);
265 down_write(<ds->ltd_rw_sem);
266 while (!list_empty(<ds->ltd_orphan)) {
267 ltd = list_entry(ltds->ltd_orphan.next,
268 struct lfsck_tgt_desc,
270 list_del_init(<d->ltd_orphan_list);
271 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
272 /* Do not hold the semaphore for too long time. */
273 up_write(<ds->ltd_rw_sem);
277 down_write(<ds->ltd_rw_sem);
279 up_write(<ds->ltd_rw_sem);
282 ltds = &lfsck->li_mdt_descs;
283 head = &lfsck_mdt_orphan_list;
291 static inline struct lfsck_component *
292 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
293 struct list_head *list)
295 struct lfsck_component *com;
297 list_for_each_entry(com, list, lc_link) {
298 if (com->lc_type == type)
304 struct lfsck_component *
305 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
307 struct lfsck_component *com;
309 spin_lock(&lfsck->li_lock);
310 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
314 com = __lfsck_component_find(lfsck, type,
315 &lfsck->li_list_double_scan);
319 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
323 lfsck_component_get(com);
324 spin_unlock(&lfsck->li_lock);
328 void lfsck_component_cleanup(const struct lu_env *env,
329 struct lfsck_component *com)
331 if (!list_empty(&com->lc_link))
332 list_del_init(&com->lc_link);
333 if (!list_empty(&com->lc_link_dir))
334 list_del_init(&com->lc_link_dir);
336 lfsck_component_put(env, com);
339 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
340 struct lu_fid *fid, bool locked)
342 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
347 mutex_lock(&lfsck->li_mutex);
349 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
351 bk->lb_last_fid = *fid;
352 /* We do not care about whether the subsequent sub-operations
353 * failed or not. The worst case is that one FID is lost that
354 * is not a big issue for the LFSCK since it is relative rare
355 * for LFSCK create. */
356 rc = lfsck_bookmark_store(env, lfsck);
360 mutex_unlock(&lfsck->li_mutex);
365 static int __lfsck_ibits_lock(const struct lu_env *env,
366 struct lfsck_instance *lfsck,
367 struct dt_object *obj,
368 struct ldlm_res_id *resid,
369 struct lustre_handle *lh,
370 __u64 bits, ldlm_mode_t mode)
372 struct lfsck_thread_info *info = lfsck_env_info(env);
373 ldlm_policy_data_t *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, ldlm_mode_t 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, ldlm_mode_t 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, ldlm_mode_t 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(!lustre_handle_is_used(&llh->llh_pdo_lh));
489 LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
493 llh->llh_pdo_mode = LCK_EX;
496 llh->llh_pdo_mode = LCK_CW;
499 llh->llh_pdo_mode = LCK_CR;
502 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
503 DFID"\n", lfsck_lfsck2name(lfsck), mode,
504 PFID(lfsck_dto2fid(obj)));
508 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
509 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
510 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
514 llh->llh_reg_mode = mode;
515 resid->name[LUSTRE_RES_ID_HSH_OFF] = full_name_hash(name, strlen(name));
516 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
517 bits, llh->llh_reg_mode);
519 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
525 * Release the the compound ibits locks.
527 * \param[in] llh pointer to the lfsck_lock_handle to be released
529 void lfsck_unlock(struct lfsck_lock_handle *llh)
531 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
532 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
535 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
536 struct lfsck_instance *lfsck,
537 const struct lu_fid *fid)
539 struct seq_server_site *ss = lfsck_dev_site(lfsck);
540 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
543 fld_range_set_mdt(range);
544 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
546 rc = range->lsr_index;
551 const char dot[] = ".";
552 const char dotdot[] = "..";
553 static const char dotlustre[] = ".lustre";
554 static const char lostfound[] = "lost+found";
557 * Remove the name entry from the .lustre/lost+found directory.
559 * No need to care about the object referenced by the name entry,
560 * either the name entry is invalid or redundant, or the referenced
561 * object has been processed or will be handled by others.
563 * \param[in] env pointer to the thread context
564 * \param[in] lfsck pointer to the lfsck instance
565 * \param[in] name the name for the name entry to be removed
567 * \retval 0 for success
568 * \retval negative error number on failure
570 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
571 struct lfsck_instance *lfsck,
574 struct dt_object *parent = lfsck->li_lpf_root_obj;
575 struct dt_device *dev = lfsck_obj2dev(parent);
577 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
581 rc = lfsck_lock(env, lfsck, parent, name, llh,
582 MDS_INODELOCK_UPDATE, LCK_PW);
586 th = dt_trans_create(env, dev);
588 GOTO(unlock, rc = PTR_ERR(th));
590 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
594 rc = dt_declare_ref_del(env, parent, th);
598 rc = dt_trans_start_local(env, dev, th);
602 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
606 dt_write_lock(env, parent, 0);
607 rc = dt_ref_del(env, parent, th);
608 dt_write_unlock(env, parent);
613 dt_trans_stop(env, dev, th);
618 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
619 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
624 static int lfsck_create_lpf_local(const struct lu_env *env,
625 struct lfsck_instance *lfsck,
626 struct dt_object *child,
628 struct dt_object_format *dof,
631 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
632 struct dt_object *parent = lfsck->li_lpf_root_obj;
633 struct dt_device *dev = lfsck_obj2dev(child);
634 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
635 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
636 const struct lu_fid *cfid = lfsck_dto2fid(child);
637 struct thandle *th = NULL;
638 struct linkea_data ldata = { NULL };
639 struct lu_buf linkea_buf;
640 const struct lu_name *cname;
642 int len = sizeof(struct lfsck_bookmark);
646 rc = linkea_data_new(&ldata,
647 &lfsck_env_info(env)->lti_linkea_buf2);
651 cname = lfsck_name_get_const(env, name, strlen(name));
652 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
656 th = dt_trans_create(env, dev);
660 /* 1a. create child */
661 rc = dt_declare_create(env, child, la, NULL, dof, th);
665 /* 2a. increase child nlink */
666 rc = dt_declare_ref_add(env, child, th);
670 /* 3a. insert linkEA for child */
671 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
672 ldata.ld_leh->leh_len);
673 rc = dt_declare_xattr_set(env, child, &linkea_buf,
674 XATTR_NAME_LINK, 0, th);
678 /* 4a. insert name into parent dir */
679 rec->rec_type = S_IFDIR;
681 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
682 (const struct dt_key *)name, th);
686 /* 5a. increase parent nlink */
687 rc = dt_declare_ref_add(env, parent, th);
691 /* 6a. update bookmark */
692 rc = dt_declare_record_write(env, bk_obj,
693 lfsck_buf_get(env, bk, len), 0, th);
697 rc = dt_trans_start_local(env, dev, th);
701 dt_write_lock(env, child, 0);
702 /* 1b.1. create child */
703 rc = dt_create(env, child, la, NULL, dof, th);
707 if (unlikely(!dt_try_as_dir(env, child)))
708 GOTO(unlock, rc = -ENOTDIR);
710 /* 1b.2. insert dot into child dir */
712 rc = dt_insert(env, child, (const struct dt_rec *)rec,
713 (const struct dt_key *)dot, th, 1);
717 /* 1b.3. insert dotdot into child dir */
718 rec->rec_fid = &LU_LPF_FID;
719 rc = dt_insert(env, child, (const struct dt_rec *)rec,
720 (const struct dt_key *)dotdot, th, 1);
724 /* 2b. increase child nlink */
725 rc = dt_ref_add(env, child, th);
729 /* 3b. insert linkEA for child. */
730 rc = dt_xattr_set(env, child, &linkea_buf,
731 XATTR_NAME_LINK, 0, th);
732 dt_write_unlock(env, child);
736 /* 4b. insert name into parent dir */
738 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
739 (const struct dt_key *)name, th, 1);
743 dt_write_lock(env, parent, 0);
744 /* 5b. increase parent nlink */
745 rc = dt_ref_add(env, parent, th);
746 dt_write_unlock(env, parent);
750 bk->lb_lpf_fid = *cfid;
751 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
753 /* 6b. update bookmark */
754 rc = dt_record_write(env, bk_obj,
755 lfsck_buf_get(env, bk, len), &pos, th);
760 dt_write_unlock(env, child);
763 dt_trans_stop(env, dev, th);
768 static int lfsck_create_lpf_remote(const struct lu_env *env,
769 struct lfsck_instance *lfsck,
770 struct dt_object *child,
772 struct dt_object_format *dof,
775 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
776 struct dt_object *parent = lfsck->li_lpf_root_obj;
777 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
778 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
779 const struct lu_fid *cfid = lfsck_dto2fid(child);
780 struct thandle *th = NULL;
781 struct linkea_data ldata = { NULL };
782 struct lu_buf linkea_buf;
783 const struct lu_name *cname;
784 struct dt_device *dev;
786 int len = sizeof(struct lfsck_bookmark);
790 rc = linkea_data_new(&ldata,
791 &lfsck_env_info(env)->lti_linkea_buf2);
795 cname = lfsck_name_get_const(env, name, strlen(name));
796 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
800 /* Create .lustre/lost+found/MDTxxxx. */
802 /* XXX: Currently, cross-MDT create operation needs to create the child
803 * object firstly, then insert name into the parent directory. For
804 * this case, the child object resides on current MDT (local), but
805 * the parent ".lustre/lost+found" may be on remote MDT. It is not
806 * easy to contain all the sub-modifications orderly within single
809 * To avoid more inconsistency, we split the create operation into
812 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
814 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
817 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
818 * repair such inconsistency when LFSCK run next time. */
820 /* Transaction I: locally */
822 dev = lfsck_obj2dev(child);
823 th = dt_trans_create(env, dev);
827 /* 1a. create child */
828 rc = dt_declare_create(env, child, la, NULL, dof, th);
832 /* 2a. increase child nlink */
833 rc = dt_declare_ref_add(env, child, th);
837 /* 3a. insert linkEA for child */
838 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
839 ldata.ld_leh->leh_len);
840 rc = dt_declare_xattr_set(env, child, &linkea_buf,
841 XATTR_NAME_LINK, 0, th);
845 /* 4a. update bookmark */
846 rc = dt_declare_record_write(env, bk_obj,
847 lfsck_buf_get(env, bk, len), 0, th);
851 rc = dt_trans_start_local(env, dev, th);
855 dt_write_lock(env, child, 0);
856 /* 1b.1. create child */
857 rc = dt_create(env, child, la, NULL, dof, th);
861 if (unlikely(!dt_try_as_dir(env, child)))
862 GOTO(unlock, rc = -ENOTDIR);
864 /* 1b.2. insert dot into child dir */
865 rec->rec_type = S_IFDIR;
867 rc = dt_insert(env, child, (const struct dt_rec *)rec,
868 (const struct dt_key *)dot, th, 1);
872 /* 1b.3. insert dotdot into child dir */
873 rec->rec_fid = &LU_LPF_FID;
874 rc = dt_insert(env, child, (const struct dt_rec *)rec,
875 (const struct dt_key *)dotdot, th, 1);
879 /* 2b. increase child nlink */
880 rc = dt_ref_add(env, child, th);
884 /* 3b. insert linkEA for child */
885 rc = dt_xattr_set(env, child, &linkea_buf,
886 XATTR_NAME_LINK, 0, th);
890 bk->lb_lpf_fid = *cfid;
891 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
893 /* 4b. update bookmark */
894 rc = dt_record_write(env, bk_obj,
895 lfsck_buf_get(env, bk, len), &pos, th);
897 dt_write_unlock(env, child);
898 dt_trans_stop(env, dev, th);
902 /* Transaction II: remotely */
904 dev = lfsck_obj2dev(parent);
905 th = dt_trans_create(env, dev);
910 /* 5a. insert name into parent dir */
912 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
913 (const struct dt_key *)name, th);
917 /* 6a. increase parent nlink */
918 rc = dt_declare_ref_add(env, parent, th);
922 rc = dt_trans_start_local(env, dev, th);
926 /* 5b. insert name into parent dir */
927 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
928 (const struct dt_key *)name, th, 1);
932 dt_write_lock(env, parent, 0);
933 /* 6b. increase parent nlink */
934 rc = dt_ref_add(env, parent, th);
935 dt_write_unlock(env, parent);
940 dt_write_unlock(env, child);
942 dt_trans_stop(env, dev, th);
944 if (rc != 0 && dev == lfsck_obj2dev(parent))
945 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
946 "for orphans, but failed to insert the name %s "
947 "to the .lustre/lost+found/. Such inconsistency "
948 "will be repaired when LFSCK run next time: rc = %d\n",
949 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
955 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
957 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
958 * orphans and other uncertain inconsistent objects found during the
959 * LFSCK. Such directory will be created by the LFSCK engine on the
960 * local MDT before the LFSCK scanning.
962 * \param[in] env pointer to the thread context
963 * \param[in] lfsck pointer to the lfsck instance
965 * \retval 0 for success
966 * \retval negative error number on failure
968 static int lfsck_create_lpf(const struct lu_env *env,
969 struct lfsck_instance *lfsck)
971 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
972 struct lfsck_thread_info *info = lfsck_env_info(env);
973 struct lu_fid *cfid = &info->lti_fid2;
974 struct lu_attr *la = &info->lti_la;
975 struct dt_object_format *dof = &info->lti_dof;
976 struct dt_object *parent = lfsck->li_lpf_root_obj;
977 struct dt_object *child = NULL;
978 struct lfsck_lock_handle *llh = &info->lti_llh;
980 int node = lfsck_dev_idx(lfsck);
984 LASSERT(lfsck->li_master);
985 LASSERT(parent != NULL);
986 LASSERT(lfsck->li_lpf_obj == NULL);
988 rc = lfsck_lock(env, lfsck, parent, name, llh,
989 MDS_INODELOCK_UPDATE, LCK_PW);
993 snprintf(name, 8, "MDT%04x", node);
994 if (fid_is_zero(&bk->lb_lpf_fid)) {
995 /* There is corner case that: in former LFSCK scanning we have
996 * created the .lustre/lost+found/MDTxxxx but failed to update
997 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
998 * it from MDT0 firstly. */
999 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1000 (const struct dt_key *)name);
1001 if (rc != 0 && rc != -ENOENT)
1005 bk->lb_lpf_fid = *cfid;
1006 rc = lfsck_bookmark_store(env, lfsck);
1008 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1013 *cfid = bk->lb_lpf_fid;
1016 child = lfsck_object_find_bottom(env, lfsck, cfid);
1018 GOTO(unlock, rc = PTR_ERR(child));
1020 if (dt_object_exists(child) != 0) {
1021 if (unlikely(!dt_try_as_dir(env, child)))
1024 lfsck->li_lpf_obj = child;
1029 memset(la, 0, sizeof(*la));
1030 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1031 la->la_mode = S_IFDIR | S_IRWXU;
1032 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1034 memset(dof, 0, sizeof(*dof));
1035 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1038 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1040 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1042 lfsck->li_lpf_obj = child;
1048 if (rc != 0 && child != NULL && !IS_ERR(child))
1049 lfsck_object_put(env, child);
1055 * Scan .lustre/lost+found for bad name entries and remove them.
1057 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1058 * index in the system. Any other formatted name is invalid and should be
1061 * \param[in] env pointer to the thread context
1062 * \param[in] lfsck pointer to the lfsck instance
1064 * \retval 0 for success
1065 * \retval negative error number on failure
1067 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1068 struct lfsck_instance *lfsck)
1070 struct dt_object *parent = lfsck->li_lpf_root_obj;
1071 struct lu_dirent *ent =
1072 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1073 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1078 it = iops->init(env, parent, LUDA_64BITHASH);
1080 RETURN(PTR_ERR(it));
1082 rc = iops->load(env, it, 0);
1084 rc = iops->next(env, it);
1091 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1095 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1096 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1099 /* name length must be strlen("MDTxxxx") */
1100 if (ent->lde_namelen != 7)
1103 if (memcmp(ent->lde_name, "MDT", off) != 0)
1106 while (off < 7 && isxdigit(ent->lde_name[off]))
1112 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1119 rc = iops->next(env, it);
1123 iops->fini(env, it);
1125 RETURN(rc > 0 ? 0 : rc);
1128 static int lfsck_update_lpf_entry(const struct lu_env *env,
1129 struct lfsck_instance *lfsck,
1130 struct dt_object *parent,
1131 struct dt_object *child,
1133 enum lfsck_verify_lpf_types type)
1137 if (type == LVLT_BY_BOOKMARK) {
1138 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1139 lfsck_dto2fid(child), S_IFDIR);
1140 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1141 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1142 rc = lfsck_bookmark_store(env, lfsck);
1144 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1145 " in the bookmark file: rc = %d\n",
1146 lfsck_lfsck2name(lfsck),
1147 PFID(lfsck_dto2fid(child)), rc);
1154 * Check whether the @child back references the @parent.
1157 * 1) The child's FID is stored in the bookmark file. If the child back
1158 * references the parent (LU_LPF_FID object) via its ".." entry, then
1159 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1160 * the child back references another parent2, then:
1161 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1162 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1163 * references the child. So keep them there. As the LFSCK processing,
1164 * the parent3 may be found, then when the LFSCK run next time, the
1165 * inconsistency can be repaired.
1167 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1168 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1169 * via its ".." entry, then update the bookmark file, otherwise, if the child
1170 * back references another parent2, then:
1171 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1172 * from .lustre/lost+found/;
1173 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1174 * sub-directory name entry and update the child;
1175 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1176 * or not, then keep them there.
1178 * \param[in] env pointer to the thread context
1179 * \param[in] lfsck pointer to the lfsck instance
1180 * \param[in] child pointer to the lost+found sub-directory object
1181 * \param[in] name the name for lost+found sub-directory object
1182 * \param[out] fid pointer to the buffer to hold the FID of the object
1183 * (called it as parent2) that is referenced via the
1184 * child's dotdot entry; it also can be the FID that
1185 * is referenced by the name entry under the parent2.
1186 * \param[in] type to indicate where the child's FID is stored in
1188 * \retval positive number for uncertain inconsistency
1189 * \retval 0 for success
1190 * \retval negative error number on failure
1192 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1193 struct lfsck_instance *lfsck,
1194 struct dt_object *child, const char *name,
1196 enum lfsck_verify_lpf_types type)
1198 struct dt_object *parent = lfsck->li_lpf_root_obj;
1199 struct lfsck_thread_info *info = lfsck_env_info(env);
1200 char *name2 = info->lti_key;
1201 struct lu_fid *fid2 = &info->lti_fid3;
1202 struct dt_object *parent2 = NULL;
1203 struct lustre_handle lh = { 0 };
1208 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1209 (const struct dt_key *)dotdot);
1213 if (!fid_is_sane(fid))
1214 GOTO(linkea, rc = -EINVAL);
1216 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1217 const struct lu_name *cname;
1219 if (lfsck->li_lpf_obj == NULL) {
1220 lu_object_get(&child->do_lu);
1221 lfsck->li_lpf_obj = child;
1224 cname = lfsck_name_get_const(env, name, strlen(name));
1225 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1227 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1233 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1234 if (IS_ERR(parent2))
1235 GOTO(linkea, parent2);
1237 if (!dt_object_exists(parent2)) {
1238 lfsck_object_put(env, parent2);
1240 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1243 if (!dt_try_as_dir(env, parent2)) {
1244 lfsck_object_put(env, parent2);
1246 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1250 /* To prevent rename/unlink race */
1251 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1252 MDS_INODELOCK_UPDATE, LCK_PR);
1256 dt_read_lock(env, child, 0);
1257 rc = lfsck_links_get_first(env, child, name2, fid2);
1259 dt_read_unlock(env, child);
1260 lfsck_ibits_unlock(&lh, LCK_PR);
1262 GOTO(out_put, rc = 1);
1265 /* It is almost impossible that the bookmark file (or the name entry)
1266 * and the linkEA hit the same data corruption. Trust the linkEA. */
1267 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1268 dt_read_unlock(env, child);
1269 lfsck_ibits_unlock(&lh, LCK_PR);
1272 if (lfsck->li_lpf_obj == NULL) {
1273 lu_object_get(&child->do_lu);
1274 lfsck->li_lpf_obj = child;
1277 /* Update the child's dotdot entry */
1278 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1279 &LU_LPF_FID, S_IFDIR);
1281 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1287 if (parent2 == NULL || IS_ERR(parent2)) {
1288 dt_read_unlock(env, child);
1289 lfsck_ibits_unlock(&lh, LCK_PR);
1291 GOTO(out_done, rc = 1);
1294 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1295 (const struct dt_key *)name2);
1296 dt_read_unlock(env, child);
1297 lfsck_ibits_unlock(&lh, LCK_PR);
1298 if (rc != 0 && rc != -ENOENT)
1301 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1302 if (type == LVLT_BY_BOOKMARK)
1303 GOTO(out_put, rc = 1);
1305 /* Trust the name entry, update the child's dotdot entry. */
1306 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1307 &LU_LPF_FID, S_IFDIR);
1312 if (type == LVLT_BY_BOOKMARK) {
1313 /* Invalid FID record in the bookmark file, reset it. */
1314 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1315 rc = lfsck_bookmark_store(env, lfsck);
1317 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1318 " in the bookmark file: rc = %d\n",
1319 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1320 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1321 /* The name entry is wrong, remove it. */
1322 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1328 if (parent2 != NULL && !IS_ERR(parent2))
1329 lfsck_object_put(env, parent2);
1336 * Verify the /ROOT/.lustre/lost+found/ directory.
1338 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1339 * the LFSCK does not exactly know how to handle, such as orphans. So before
1340 * the LFSCK scanning the system, the consistency of such directory needs to
1341 * be verified firstly to allow the users to use it during the LFSCK.
1343 * \param[in] env pointer to the thread context
1344 * \param[in] lfsck pointer to the lfsck instance
1346 * \retval positive number for uncertain inconsistency
1347 * \retval 0 for success
1348 * \retval negative error number on failure
1350 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1352 struct lfsck_thread_info *info = lfsck_env_info(env);
1353 struct lu_fid *pfid = &info->lti_fid;
1354 struct lu_fid *cfid = &info->lti_fid2;
1355 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1356 struct dt_object *parent;
1357 /* child1's FID is in the bookmark file. */
1358 struct dt_object *child1 = NULL;
1359 /* child2's FID is in the name entry MDTxxxx. */
1360 struct dt_object *child2 = NULL;
1361 const struct lu_name *cname;
1363 int node = lfsck_dev_idx(lfsck);
1367 LASSERT(lfsck->li_master);
1369 if (lfsck->li_lpf_root_obj != NULL)
1373 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1376 struct lfsck_tgt_desc *ltd;
1378 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1379 if (unlikely(ltd == NULL))
1382 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1388 RETURN(PTR_ERR(parent));
1390 LASSERT(dt_object_exists(parent));
1392 if (unlikely(!dt_try_as_dir(env, parent))) {
1393 lfsck_object_put(env, parent);
1395 GOTO(put, rc = -ENOTDIR);
1398 lfsck->li_lpf_root_obj = parent;
1400 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1402 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1403 "for bad sub-directories: rc = %d\n",
1404 lfsck_lfsck2name(lfsck), rc);
1407 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1408 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1409 struct lu_fid tfid = bk->lb_lpf_fid;
1411 /* Invalid FID record in the bookmark file, reset it. */
1412 fid_zero(&bk->lb_lpf_fid);
1413 rc = lfsck_bookmark_store(env, lfsck);
1415 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1416 " in the bookmark file: rc = %d\n",
1417 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1422 child1 = lfsck_object_find_bottom(env, lfsck,
1424 if (IS_ERR(child1)) {
1429 if (unlikely(!dt_object_exists(child1) ||
1430 dt_object_remote(child1)) ||
1431 !S_ISDIR(lfsck_object_type(child1))) {
1432 /* Invalid FID record in the bookmark file,
1434 fid_zero(&bk->lb_lpf_fid);
1435 rc = lfsck_bookmark_store(env, lfsck);
1437 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1438 " in the bookmark file: rc = %d\n",
1439 lfsck_lfsck2name(lfsck),
1440 PFID(lfsck_dto2fid(child1)), rc);
1445 lfsck_object_put(env, child1);
1447 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1448 GOTO(put, rc = -ENOTDIR);
1454 snprintf(name, 8, "MDT%04x", node);
1455 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1456 (const struct dt_key *)name);
1457 if (rc == -ENOENT) {
1458 if (!fid_is_zero(&bk->lb_lpf_fid))
1467 /* Invalid FID in the name entry, remove the name entry. */
1468 if (!fid_is_norm(cfid)) {
1469 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1476 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1478 GOTO(put, rc = PTR_ERR(child2));
1480 if (unlikely(!dt_object_exists(child2) ||
1481 dt_object_remote(child2)) ||
1482 !S_ISDIR(lfsck_object_type(child2))) {
1483 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1490 if (unlikely(!dt_try_as_dir(env, child2)))
1491 GOTO(put, rc = -ENOTDIR);
1493 if (child1 == NULL) {
1494 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1495 pfid, LVLT_BY_NAMEENTRY);
1496 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1497 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1498 pfid, LVLT_BY_BOOKMARK);
1499 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1500 rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1504 if (lfsck->li_lpf_obj == NULL) {
1505 lu_object_get(&child2->do_lu);
1506 lfsck->li_lpf_obj = child2;
1509 cname = lfsck_name_get_const(env, name, strlen(name));
1510 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1517 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1518 pfid, LVLT_BY_BOOKMARK);
1523 if (lfsck->li_lpf_obj != NULL) {
1524 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1525 lfsck_object_put(env, lfsck->li_lpf_obj);
1526 lfsck->li_lpf_obj = NULL;
1529 } else if (rc == 0) {
1530 rc = lfsck_create_lpf(env, lfsck);
1533 if (child2 != NULL && !IS_ERR(child2))
1534 lfsck_object_put(env, child2);
1535 if (child1 != NULL && !IS_ERR(child1))
1536 lfsck_object_put(env, child1);
1541 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1543 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1544 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1549 if (unlikely(ss == NULL))
1552 OBD_ALLOC_PTR(lfsck->li_seq);
1553 if (lfsck->li_seq == NULL)
1556 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1558 GOTO(out, rc = -ENOMEM);
1560 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1561 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1563 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1567 if (fid_is_sane(&bk->lb_last_fid))
1568 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1573 OBD_FREE_PTR(lfsck->li_seq);
1574 lfsck->li_seq = NULL;
1579 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1581 if (lfsck->li_seq != NULL) {
1582 seq_client_fini(lfsck->li_seq);
1583 OBD_FREE_PTR(lfsck->li_seq);
1584 lfsck->li_seq = NULL;
1588 void lfsck_instance_cleanup(const struct lu_env *env,
1589 struct lfsck_instance *lfsck)
1591 struct ptlrpc_thread *thread = &lfsck->li_thread;
1592 struct lfsck_component *com;
1593 struct lfsck_component *next;
1594 struct lfsck_lmv_unit *llu;
1595 struct lfsck_lmv_unit *llu_next;
1596 struct lfsck_lmv *llmv;
1599 LASSERT(list_empty(&lfsck->li_link));
1600 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1602 if (lfsck->li_obj_oit != NULL) {
1603 lfsck_object_put(env, lfsck->li_obj_oit);
1604 lfsck->li_obj_oit = NULL;
1607 LASSERT(lfsck->li_obj_dir == NULL);
1608 LASSERT(lfsck->li_lmv == NULL);
1610 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1611 llmv = &llu->llu_lmv;
1613 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1614 "still in using: %u\n",
1615 atomic_read(&llmv->ll_ref));
1617 lfsck_lmv_put(env, llmv);
1620 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1621 lfsck_component_cleanup(env, com);
1624 LASSERT(list_empty(&lfsck->li_list_dir));
1626 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1628 lfsck_component_cleanup(env, com);
1631 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1632 lfsck_component_cleanup(env, com);
1635 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1636 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1638 if (lfsck->li_lfsck_dir != NULL) {
1639 lfsck_object_put(env, lfsck->li_lfsck_dir);
1640 lfsck->li_lfsck_dir = NULL;
1643 if (lfsck->li_bookmark_obj != NULL) {
1644 lfsck_object_put(env, lfsck->li_bookmark_obj);
1645 lfsck->li_bookmark_obj = NULL;
1648 if (lfsck->li_lpf_obj != NULL) {
1649 lfsck_object_put(env, lfsck->li_lpf_obj);
1650 lfsck->li_lpf_obj = NULL;
1653 if (lfsck->li_lpf_root_obj != NULL) {
1654 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1655 lfsck->li_lpf_root_obj = NULL;
1658 if (lfsck->li_los != NULL) {
1659 local_oid_storage_fini(env, lfsck->li_los);
1660 lfsck->li_los = NULL;
1663 lfsck_fid_fini(lfsck);
1665 OBD_FREE_PTR(lfsck);
1668 static inline struct lfsck_instance *
1669 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1671 struct lfsck_instance *lfsck;
1673 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1674 if (lfsck->li_bottom == key) {
1676 lfsck_instance_get(lfsck);
1678 list_del_init(&lfsck->li_link);
1687 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1690 struct lfsck_instance *lfsck;
1692 spin_lock(&lfsck_instance_lock);
1693 lfsck = __lfsck_instance_find(key, ref, unlink);
1694 spin_unlock(&lfsck_instance_lock);
1699 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1701 struct lfsck_instance *tmp;
1703 spin_lock(&lfsck_instance_lock);
1704 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1705 if (lfsck->li_bottom == tmp->li_bottom) {
1706 spin_unlock(&lfsck_instance_lock);
1711 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1712 spin_unlock(&lfsck_instance_lock);
1716 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1721 bool newline = (bits != 0 ? false : true);
1724 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1728 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1731 if (names[i] != NULL) {
1735 rc = seq_printf(m, "%s%c", names[i],
1736 newline ? '\n' : ',');
1744 rc = seq_printf(m, "\n");
1749 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1754 rc = seq_printf(m, "%s_time: N/A\n", name);
1756 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1761 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1763 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1764 name, cfs_time_current_sec() - time);
1769 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1772 if (fid_is_zero(&pos->lp_dir_parent)) {
1773 if (pos->lp_oit_cookie == 0)
1774 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1776 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1777 prefix, pos->lp_oit_cookie);
1780 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1781 prefix, pos->lp_oit_cookie,
1782 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1785 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1786 struct lfsck_position *pos, bool init)
1788 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1790 if (unlikely(lfsck->li_di_oit == NULL)) {
1791 memset(pos, 0, sizeof(*pos));
1795 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1796 if (!lfsck->li_current_oit_processed && !init)
1797 pos->lp_oit_cookie--;
1799 LASSERT(pos->lp_oit_cookie > 0);
1801 if (lfsck->li_di_dir != NULL) {
1802 struct dt_object *dto = lfsck->li_obj_dir;
1804 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1807 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1808 fid_zero(&pos->lp_dir_parent);
1809 pos->lp_dir_cookie = 0;
1811 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1814 fid_zero(&pos->lp_dir_parent);
1815 pos->lp_dir_cookie = 0;
1819 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1823 if (limit != LFSCK_SPEED_NO_LIMIT) {
1824 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1825 lfsck->li_sleep_rate = limit /
1826 msecs_to_jiffies(MSEC_PER_SEC);
1827 lfsck->li_sleep_jif = 1;
1829 lfsck->li_sleep_rate = 1;
1830 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1834 lfsck->li_sleep_jif = 0;
1835 lfsck->li_sleep_rate = 0;
1838 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1839 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1846 void lfsck_control_speed(struct lfsck_instance *lfsck)
1848 struct ptlrpc_thread *thread = &lfsck->li_thread;
1849 struct l_wait_info lwi;
1851 if (lfsck->li_sleep_jif > 0 &&
1852 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1853 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1854 LWI_ON_SIGNAL_NOOP, NULL);
1856 l_wait_event(thread->t_ctl_waitq,
1857 !thread_is_running(thread),
1859 lfsck->li_new_scanned = 0;
1863 void lfsck_control_speed_by_self(struct lfsck_component *com)
1865 struct lfsck_instance *lfsck = com->lc_lfsck;
1866 struct ptlrpc_thread *thread = &lfsck->li_thread;
1867 struct l_wait_info lwi;
1869 if (lfsck->li_sleep_jif > 0 &&
1870 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1871 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1872 LWI_ON_SIGNAL_NOOP, NULL);
1874 l_wait_event(thread->t_ctl_waitq,
1875 !thread_is_running(thread),
1877 com->lc_new_scanned = 0;
1881 static struct lfsck_thread_args *
1882 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1883 struct lfsck_component *com,
1884 struct lfsck_start_param *lsp)
1886 struct lfsck_thread_args *lta;
1891 return ERR_PTR(-ENOMEM);
1893 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1899 lta->lta_lfsck = lfsck_instance_get(lfsck);
1901 lta->lta_com = lfsck_component_get(com);
1908 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1910 if (lta->lta_com != NULL)
1911 lfsck_component_put(<a->lta_env, lta->lta_com);
1912 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1913 lu_env_fini(<a->lta_env);
1917 struct lfsck_assistant_data *
1918 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1921 struct lfsck_assistant_data *lad;
1925 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1926 if (lad->lad_bitmap == NULL) {
1931 INIT_LIST_HEAD(&lad->lad_req_list);
1932 spin_lock_init(&lad->lad_lock);
1933 INIT_LIST_HEAD(&lad->lad_ost_list);
1934 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1935 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1936 INIT_LIST_HEAD(&lad->lad_mdt_list);
1937 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1938 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1939 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1941 lad->lad_name = name;
1947 struct lfsck_assistant_object *
1948 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1949 const struct lu_attr *attr, __u64 cookie,
1952 struct lfsck_assistant_object *lso;
1956 return ERR_PTR(-ENOMEM);
1958 lso->lso_fid = *fid;
1960 lso->lso_attr = *attr;
1962 atomic_set(&lso->lso_ref, 1);
1963 lso->lso_oit_cookie = cookie;
1965 lso->lso_is_dir = 1;
1971 lfsck_assistant_object_load(const struct lu_env *env,
1972 struct lfsck_instance *lfsck,
1973 struct lfsck_assistant_object *lso)
1975 struct dt_object *obj;
1977 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1981 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
1983 lfsck_object_put(env, obj);
1985 return ERR_PTR(-ENOENT);
1988 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
1989 lfsck_object_put(env, obj);
1991 return ERR_PTR(-ENOTDIR);
1998 * Generic LFSCK asynchronous communication interpretor function.
1999 * The LFSCK RPC reply for both the event notification and status
2000 * querying will be handled here.
2002 * \param[in] env pointer to the thread context
2003 * \param[in] req pointer to the LFSCK request
2004 * \param[in] args pointer to the lfsck_async_interpret_args
2005 * \param[in] rc the result for handling the LFSCK request
2007 * \retval 0 for success
2008 * \retval negative error number on failure
2010 int lfsck_async_interpret_common(const struct lu_env *env,
2011 struct ptlrpc_request *req,
2014 struct lfsck_async_interpret_args *laia = args;
2015 struct lfsck_component *com = laia->laia_com;
2016 struct lfsck_assistant_data *lad = com->lc_data;
2017 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2018 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2019 struct lfsck_request *lr = laia->laia_lr;
2021 LASSERT(com->lc_lfsck->li_master);
2023 switch (lr->lr_event) {
2026 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2028 lfsck_lfsck2name(com->lc_lfsck),
2029 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2030 ltd->ltd_index, lad->lad_name, rc);
2032 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2033 struct lfsck_layout *lo = com->lc_file_ram;
2035 if (lr->lr_flags & LEF_TO_OST)
2036 lfsck_lad_set_bitmap(env, com,
2039 lo->ll_flags |= LF_INCOMPLETE;
2041 struct lfsck_namespace *ns = com->lc_file_ram;
2043 /* If some MDT does not join the namespace
2044 * LFSCK, then we cannot know whether there
2045 * is some name entry on such MDT that with
2046 * the referenced MDT-object on this MDT or
2047 * not. So the namespace LFSCK on this MDT
2048 * cannot handle orphan MDT-objects properly.
2049 * So we mark the LFSCK as LF_INCOMPLETE and
2050 * skip orphan MDT-objects handling. */
2051 ns->ln_flags |= LF_INCOMPLETE;
2056 spin_lock(<ds->ltd_lock);
2057 if (ltd->ltd_dead) {
2058 spin_unlock(<ds->ltd_lock);
2062 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2063 struct list_head *list;
2064 struct list_head *phase_list;
2066 if (ltd->ltd_layout_done) {
2067 spin_unlock(<ds->ltd_lock);
2071 if (lr->lr_flags & LEF_TO_OST) {
2072 list = &lad->lad_ost_list;
2073 phase_list = &lad->lad_ost_phase1_list;
2075 list = &lad->lad_mdt_list;
2076 phase_list = &lad->lad_mdt_phase1_list;
2079 if (list_empty(<d->ltd_layout_list))
2080 list_add_tail(<d->ltd_layout_list, list);
2081 if (list_empty(<d->ltd_layout_phase_list))
2082 list_add_tail(<d->ltd_layout_phase_list,
2085 if (ltd->ltd_namespace_done) {
2086 spin_unlock(<ds->ltd_lock);
2090 if (list_empty(<d->ltd_namespace_list))
2091 list_add_tail(<d->ltd_namespace_list,
2092 &lad->lad_mdt_list);
2093 if (list_empty(<d->ltd_namespace_phase_list))
2094 list_add_tail(<d->ltd_namespace_phase_list,
2095 &lad->lad_mdt_phase1_list);
2097 spin_unlock(<ds->ltd_lock);
2100 case LE_PHASE1_DONE:
2101 case LE_PHASE2_DONE:
2103 if (rc != 0 && rc != -EALREADY)
2104 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2105 "event = %d, rc = %d\n",
2106 lfsck_lfsck2name(com->lc_lfsck),
2107 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2108 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2111 struct lfsck_reply *reply;
2112 struct list_head *list;
2113 struct list_head *phase_list;
2115 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2116 list = <d->ltd_layout_list;
2117 phase_list = <d->ltd_layout_phase_list;
2119 list = <d->ltd_namespace_list;
2120 phase_list = <d->ltd_namespace_phase_list;
2124 spin_lock(<ds->ltd_lock);
2125 list_del_init(phase_list);
2126 list_del_init(list);
2127 spin_unlock(<ds->ltd_lock);
2131 reply = req_capsule_server_get(&req->rq_pill,
2133 if (reply == NULL) {
2135 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2136 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2138 spin_lock(<ds->ltd_lock);
2139 list_del_init(phase_list);
2140 list_del_init(list);
2141 spin_unlock(<ds->ltd_lock);
2145 switch (reply->lr_status) {
2146 case LS_SCANNING_PHASE1:
2148 case LS_SCANNING_PHASE2:
2149 spin_lock(<ds->ltd_lock);
2150 list_del_init(phase_list);
2151 if (ltd->ltd_dead) {
2152 spin_unlock(<ds->ltd_lock);
2156 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2157 if (ltd->ltd_layout_done) {
2158 spin_unlock(<ds->ltd_lock);
2162 if (lr->lr_flags & LEF_TO_OST)
2163 list_add_tail(phase_list,
2164 &lad->lad_ost_phase2_list);
2166 list_add_tail(phase_list,
2167 &lad->lad_mdt_phase2_list);
2169 if (ltd->ltd_namespace_done) {
2170 spin_unlock(<ds->ltd_lock);
2174 list_add_tail(phase_list,
2175 &lad->lad_mdt_phase2_list);
2177 spin_unlock(<ds->ltd_lock);
2180 spin_lock(<ds->ltd_lock);
2181 list_del_init(phase_list);
2182 list_del_init(list);
2183 spin_unlock(<ds->ltd_lock);
2189 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2190 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2194 if (!laia->laia_shared) {
2196 lfsck_component_put(env, com);
2202 static void lfsck_interpret(const struct lu_env *env,
2203 struct lfsck_instance *lfsck,
2204 struct ptlrpc_request *req, void *args, int result)
2206 struct lfsck_async_interpret_args *laia = args;
2207 struct lfsck_component *com;
2209 LASSERT(laia->laia_com == NULL);
2210 LASSERT(laia->laia_shared);
2212 spin_lock(&lfsck->li_lock);
2213 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2214 laia->laia_com = com;
2215 lfsck_async_interpret_common(env, req, laia, result);
2218 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2219 laia->laia_com = com;
2220 lfsck_async_interpret_common(env, req, laia, result);
2222 spin_unlock(&lfsck->li_lock);
2225 static int lfsck_stop_notify(const struct lu_env *env,
2226 struct lfsck_instance *lfsck,
2227 struct lfsck_tgt_descs *ltds,
2228 struct lfsck_tgt_desc *ltd, __u16 type)
2230 struct lfsck_component *com;
2234 LASSERT(lfsck->li_master);
2236 spin_lock(&lfsck->li_lock);
2237 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2239 com = __lfsck_component_find(lfsck, type,
2240 &lfsck->li_list_double_scan);
2242 lfsck_component_get(com);
2243 spin_unlock(&lfsck->li_lock);
2246 struct lfsck_thread_info *info = lfsck_env_info(env);
2247 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2248 struct lfsck_request *lr = &info->lti_lr;
2249 struct lfsck_assistant_data *lad = com->lc_data;
2250 struct list_head *list;
2251 struct list_head *phase_list;
2252 struct ptlrpc_request_set *set;
2254 set = ptlrpc_prep_set();
2256 lfsck_component_put(env, com);
2261 if (type == LFSCK_TYPE_LAYOUT) {
2262 list = <d->ltd_layout_list;
2263 phase_list = <d->ltd_layout_phase_list;
2265 list = <d->ltd_namespace_list;
2266 phase_list = <d->ltd_namespace_phase_list;
2269 spin_lock(<ds->ltd_lock);
2270 if (list_empty(list)) {
2271 LASSERT(list_empty(phase_list));
2272 spin_unlock(<ds->ltd_lock);
2273 ptlrpc_set_destroy(set);
2278 list_del_init(phase_list);
2279 list_del_init(list);
2280 spin_unlock(<ds->ltd_lock);
2282 memset(lr, 0, sizeof(*lr));
2283 lr->lr_index = lfsck_dev_idx(lfsck);
2284 lr->lr_event = LE_PEER_EXIT;
2285 lr->lr_active = type;
2286 lr->lr_status = LS_CO_PAUSED;
2287 if (ltds == &lfsck->li_ost_descs)
2288 lr->lr_flags = LEF_TO_OST;
2290 laia->laia_com = com;
2291 laia->laia_ltds = ltds;
2292 atomic_inc(<d->ltd_ref);
2293 laia->laia_ltd = ltd;
2295 laia->laia_shared = 0;
2297 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2298 lfsck_async_interpret_common,
2299 laia, LFSCK_NOTIFY);
2301 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2302 "co-stop for %s: rc = %d\n",
2303 lfsck_lfsck2name(lfsck),
2304 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2305 ltd->ltd_index, lad->lad_name, rc);
2308 rc = ptlrpc_set_wait(set);
2311 ptlrpc_set_destroy(set);
2312 lfsck_component_put(env, com);
2318 static int lfsck_async_interpret(const struct lu_env *env,
2319 struct ptlrpc_request *req,
2322 struct lfsck_async_interpret_args *laia = args;
2323 struct lfsck_instance *lfsck;
2325 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2327 lfsck_interpret(env, lfsck, req, laia, rc);
2328 lfsck_tgt_put(laia->laia_ltd);
2329 if (rc != 0 && laia->laia_result != -EALREADY)
2330 laia->laia_result = rc;
2335 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2336 struct lfsck_request *lr,
2337 struct ptlrpc_request_set *set,
2338 ptlrpc_interpterer_t interpreter,
2339 void *args, int request)
2341 struct lfsck_async_interpret_args *laia;
2342 struct ptlrpc_request *req;
2343 struct lfsck_request *tmp;
2344 struct req_format *format;
2349 format = &RQF_LFSCK_NOTIFY;
2352 format = &RQF_LFSCK_QUERY;
2355 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2356 exp->exp_obd->obd_name, request, -EINVAL);
2360 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2364 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2366 ptlrpc_request_free(req);
2371 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2373 ptlrpc_request_set_replen(req);
2375 laia = ptlrpc_req_async_args(req);
2376 *laia = *(struct lfsck_async_interpret_args *)args;
2377 if (laia->laia_com != NULL)
2378 lfsck_component_get(laia->laia_com);
2379 req->rq_interpret_reply = interpreter;
2380 ptlrpc_set_add_req(set, req);
2385 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2386 struct lfsck_start_param *lsp)
2388 struct lfsck_instance *lfsck = com->lc_lfsck;
2389 struct lfsck_assistant_data *lad = com->lc_data;
2390 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2391 struct ptlrpc_thread *athread = &lad->lad_thread;
2392 struct lfsck_thread_args *lta;
2393 struct task_struct *task;
2397 lad->lad_assistant_status = 0;
2398 lad->lad_post_result = 0;
2399 lad->lad_to_post = 0;
2400 lad->lad_to_double_scan = 0;
2401 lad->lad_in_double_scan = 0;
2403 lad->lad_advance_lock = false;
2404 thread_set_flags(athread, 0);
2406 lta = lfsck_thread_args_init(lfsck, com, lsp);
2408 RETURN(PTR_ERR(lta));
2410 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2413 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2414 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2415 lfsck_thread_args_fini(lta);
2417 struct l_wait_info lwi = { 0 };
2419 l_wait_event(mthread->t_ctl_waitq,
2420 thread_is_running(athread) ||
2421 thread_is_stopped(athread),
2423 if (unlikely(!thread_is_running(athread)))
2424 rc = lad->lad_assistant_status;
2432 int lfsck_checkpoint_generic(const struct lu_env *env,
2433 struct lfsck_component *com)
2435 struct lfsck_assistant_data *lad = com->lc_data;
2436 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2437 struct ptlrpc_thread *athread = &lad->lad_thread;
2438 struct l_wait_info lwi = { 0 };
2440 l_wait_event(mthread->t_ctl_waitq,
2441 list_empty(&lad->lad_req_list) ||
2442 !thread_is_running(mthread) ||
2443 thread_is_stopped(athread),
2446 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2447 return LFSCK_CHECKPOINT_SKIP;
2452 void lfsck_post_generic(const struct lu_env *env,
2453 struct lfsck_component *com, int *result)
2455 struct lfsck_assistant_data *lad = com->lc_data;
2456 struct ptlrpc_thread *athread = &lad->lad_thread;
2457 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2458 struct l_wait_info lwi = { 0 };
2460 lad->lad_post_result = *result;
2463 lad->lad_to_post = 1;
2465 wake_up_all(&athread->t_ctl_waitq);
2466 l_wait_event(mthread->t_ctl_waitq,
2467 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2468 thread_is_stopped(athread),
2471 if (lad->lad_assistant_status < 0)
2472 *result = lad->lad_assistant_status;
2475 int lfsck_double_scan_generic(const struct lu_env *env,
2476 struct lfsck_component *com, int status)
2478 struct lfsck_assistant_data *lad = com->lc_data;
2479 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2480 struct ptlrpc_thread *athread = &lad->lad_thread;
2481 struct l_wait_info lwi = { 0 };
2483 if (status != LS_SCANNING_PHASE2)
2486 lad->lad_to_double_scan = 1;
2488 wake_up_all(&athread->t_ctl_waitq);
2489 l_wait_event(mthread->t_ctl_waitq,
2490 lad->lad_in_double_scan ||
2491 thread_is_stopped(athread),
2494 if (lad->lad_assistant_status < 0)
2495 return lad->lad_assistant_status;
2500 void lfsck_quit_generic(const struct lu_env *env,
2501 struct lfsck_component *com)
2503 struct lfsck_assistant_data *lad = com->lc_data;
2504 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2505 struct ptlrpc_thread *athread = &lad->lad_thread;
2506 struct l_wait_info lwi = { 0 };
2509 wake_up_all(&athread->t_ctl_waitq);
2510 l_wait_event(mthread->t_ctl_waitq,
2511 thread_is_init(athread) ||
2512 thread_is_stopped(athread),
2516 /* external interfaces */
2518 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2521 struct lfsck_instance *lfsck;
2525 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2529 lfsck = lfsck_instance_find(key, true, false);
2530 if (likely(lfsck != NULL)) {
2531 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2532 lfsck_instance_put(&env, lfsck);
2541 EXPORT_SYMBOL(lfsck_get_speed);
2543 int lfsck_set_speed(struct dt_device *key, int val)
2546 struct lfsck_instance *lfsck;
2550 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2554 lfsck = lfsck_instance_find(key, true, false);
2555 if (likely(lfsck != NULL)) {
2556 mutex_lock(&lfsck->li_mutex);
2557 if (__lfsck_set_speed(lfsck, val))
2558 rc = lfsck_bookmark_store(&env, lfsck);
2559 mutex_unlock(&lfsck->li_mutex);
2560 lfsck_instance_put(&env, lfsck);
2569 EXPORT_SYMBOL(lfsck_set_speed);
2571 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2574 struct lfsck_instance *lfsck;
2578 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2582 lfsck = lfsck_instance_find(key, true, false);
2583 if (likely(lfsck != NULL)) {
2584 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2585 lfsck_instance_put(&env, lfsck);
2594 EXPORT_SYMBOL(lfsck_get_windows);
2596 int lfsck_set_windows(struct dt_device *key, int val)
2599 struct lfsck_instance *lfsck;
2603 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2607 lfsck = lfsck_instance_find(key, true, false);
2608 if (likely(lfsck != NULL)) {
2609 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2610 CWARN("%s: invalid async windows size that may "
2611 "cause memory issues. The valid range is "
2613 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2615 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2616 mutex_lock(&lfsck->li_mutex);
2617 lfsck->li_bookmark_ram.lb_async_windows = val;
2618 rc = lfsck_bookmark_store(&env, lfsck);
2619 mutex_unlock(&lfsck->li_mutex);
2621 lfsck_instance_put(&env, lfsck);
2630 EXPORT_SYMBOL(lfsck_set_windows);
2632 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2635 struct lfsck_instance *lfsck;
2636 struct lfsck_component *com;
2640 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2644 lfsck = lfsck_instance_find(key, true, false);
2645 if (likely(lfsck != NULL)) {
2646 com = lfsck_component_find(lfsck, type);
2647 if (likely(com != NULL)) {
2648 rc = com->lc_ops->lfsck_dump(&env, com, m);
2649 lfsck_component_put(&env, com);
2654 lfsck_instance_put(&env, lfsck);
2663 EXPORT_SYMBOL(lfsck_dump);
2665 static int lfsck_stop_all(const struct lu_env *env,
2666 struct lfsck_instance *lfsck,
2667 struct lfsck_stop *stop)
2669 struct lfsck_thread_info *info = lfsck_env_info(env);
2670 struct lfsck_request *lr = &info->lti_lr;
2671 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2672 struct ptlrpc_request_set *set;
2673 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2674 struct lfsck_tgt_desc *ltd;
2675 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2681 LASSERT(stop->ls_flags & LPF_BROADCAST);
2683 set = ptlrpc_prep_set();
2684 if (unlikely(set == NULL))
2687 memset(lr, 0, sizeof(*lr));
2688 lr->lr_event = LE_STOP;
2689 lr->lr_index = lfsck_dev_idx(lfsck);
2690 lr->lr_status = stop->ls_status;
2691 lr->lr_version = bk->lb_version;
2692 lr->lr_active = LFSCK_TYPES_ALL;
2693 lr->lr_param = stop->ls_flags;
2695 laia->laia_com = NULL;
2696 laia->laia_ltds = ltds;
2698 laia->laia_result = 0;
2699 laia->laia_shared = 1;
2701 down_read(<ds->ltd_rw_sem);
2702 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2703 ltd = lfsck_tgt_get(ltds, idx);
2704 LASSERT(ltd != NULL);
2706 laia->laia_ltd = ltd;
2707 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2708 lfsck_async_interpret, laia,
2711 lfsck_interpret(env, lfsck, NULL, laia, rc);
2713 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2714 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2718 up_read(<ds->ltd_rw_sem);
2720 rc = ptlrpc_set_wait(set);
2721 ptlrpc_set_destroy(set);
2724 rc = laia->laia_result;
2726 if (rc == -EALREADY)
2730 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2731 lfsck_lfsck2name(lfsck), rc);
2733 RETURN(rc != 0 ? rc : rc1);
2736 static int lfsck_start_all(const struct lu_env *env,
2737 struct lfsck_instance *lfsck,
2738 struct lfsck_start *start)
2740 struct lfsck_thread_info *info = lfsck_env_info(env);
2741 struct lfsck_request *lr = &info->lti_lr;
2742 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2743 struct ptlrpc_request_set *set;
2744 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2745 struct lfsck_tgt_desc *ltd;
2746 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2751 LASSERT(start->ls_flags & LPF_BROADCAST);
2753 set = ptlrpc_prep_set();
2754 if (unlikely(set == NULL))
2757 memset(lr, 0, sizeof(*lr));
2758 lr->lr_event = LE_START;
2759 lr->lr_index = lfsck_dev_idx(lfsck);
2760 lr->lr_speed = bk->lb_speed_limit;
2761 lr->lr_version = bk->lb_version;
2762 lr->lr_active = start->ls_active;
2763 lr->lr_param = start->ls_flags;
2764 lr->lr_async_windows = bk->lb_async_windows;
2765 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2766 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2769 laia->laia_com = NULL;
2770 laia->laia_ltds = ltds;
2772 laia->laia_result = 0;
2773 laia->laia_shared = 1;
2775 down_read(<ds->ltd_rw_sem);
2776 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2777 ltd = lfsck_tgt_get(ltds, idx);
2778 LASSERT(ltd != NULL);
2780 laia->laia_ltd = ltd;
2781 ltd->ltd_layout_done = 0;
2782 ltd->ltd_namespace_done = 0;
2783 ltd->ltd_synced_failures = 0;
2784 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2785 lfsck_async_interpret, laia,
2788 lfsck_interpret(env, lfsck, NULL, laia, rc);
2790 CERROR("%s: cannot notify MDT %x for LFSCK "
2791 "start, failout: rc = %d\n",
2792 lfsck_lfsck2name(lfsck), idx, rc);
2796 up_read(<ds->ltd_rw_sem);
2799 ptlrpc_set_destroy(set);
2804 rc = ptlrpc_set_wait(set);
2805 ptlrpc_set_destroy(set);
2808 rc = laia->laia_result;
2811 struct lfsck_stop *stop = &info->lti_stop;
2813 CERROR("%s: cannot start LFSCK on some MDTs, "
2814 "stop all: rc = %d\n",
2815 lfsck_lfsck2name(lfsck), rc);
2816 if (rc != -EALREADY) {
2817 stop->ls_status = LS_FAILED;
2818 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2819 lfsck_stop_all(env, lfsck, stop);
2826 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2827 struct lfsck_start_param *lsp)
2829 struct lfsck_start *start = lsp->lsp_start;
2830 struct lfsck_instance *lfsck;
2831 struct lfsck_bookmark *bk;
2832 struct ptlrpc_thread *thread;
2833 struct lfsck_component *com;
2834 struct l_wait_info lwi = { 0 };
2835 struct lfsck_thread_args *lta;
2836 struct task_struct *task;
2843 lfsck = lfsck_instance_find(key, true, false);
2844 if (unlikely(lfsck == NULL))
2847 /* System is not ready, try again later. */
2848 if (unlikely(lfsck->li_namespace == NULL))
2849 GOTO(put, rc = -EAGAIN);
2851 /* start == NULL means auto trigger paused LFSCK. */
2852 if ((start == NULL) &&
2853 (list_empty(&lfsck->li_list_scan) ||
2854 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2857 bk = &lfsck->li_bookmark_ram;
2858 thread = &lfsck->li_thread;
2859 mutex_lock(&lfsck->li_mutex);
2860 spin_lock(&lfsck->li_lock);
2861 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2863 if (unlikely(start == NULL)) {
2864 spin_unlock(&lfsck->li_lock);
2868 while (start->ls_active != 0) {
2869 if (!(type & start->ls_active)) {
2874 com = __lfsck_component_find(lfsck, type,
2875 &lfsck->li_list_scan);
2877 com = __lfsck_component_find(lfsck, type,
2878 &lfsck->li_list_double_scan);
2884 if (com->lc_ops->lfsck_join != NULL) {
2885 rc = com->lc_ops->lfsck_join( env, com, lsp);
2886 if (rc != 0 && rc != -EALREADY)
2889 start->ls_active &= ~type;
2892 spin_unlock(&lfsck->li_lock);
2895 spin_unlock(&lfsck->li_lock);
2897 lfsck->li_status = 0;
2898 lfsck->li_oit_over = 0;
2899 lfsck->li_start_unplug = 0;
2900 lfsck->li_drop_dryrun = 0;
2901 lfsck->li_new_scanned = 0;
2903 /* For auto trigger. */
2907 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2908 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2909 lfsck_lfsck2name(lfsck));
2911 GOTO(out, rc = -EPERM);
2914 start->ls_version = bk->lb_version;
2916 if (start->ls_active != 0) {
2917 struct lfsck_component *next;
2919 if (start->ls_active == LFSCK_TYPES_ALL)
2920 start->ls_active = LFSCK_TYPES_SUPPORTED;
2922 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2923 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2924 GOTO(out, rc = -ENOTSUPP);
2927 list_for_each_entry_safe(com, next,
2928 &lfsck->li_list_scan, lc_link) {
2929 if (!(com->lc_type & start->ls_active)) {
2930 rc = com->lc_ops->lfsck_post(env, com, 0,
2937 while (start->ls_active != 0) {
2938 if (type & start->ls_active) {
2939 com = __lfsck_component_find(lfsck, type,
2940 &lfsck->li_list_idle);
2942 /* The component status will be updated
2943 * when its prep() is called later by
2944 * the LFSCK main engine. */
2945 list_move_tail(&com->lc_link,
2946 &lfsck->li_list_scan);
2947 start->ls_active &= ~type;
2953 if (list_empty(&lfsck->li_list_scan)) {
2954 /* The speed limit will be used to control both the LFSCK and
2955 * low layer scrub (if applied), need to be handled firstly. */
2956 if (start->ls_valid & LSV_SPEED_LIMIT) {
2957 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2958 rc = lfsck_bookmark_store(env, lfsck);
2967 if (start->ls_flags & LPF_RESET)
2968 flags |= DOIF_RESET;
2970 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2974 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2975 start->ls_active |= com->lc_type;
2976 if (flags & DOIF_RESET) {
2977 rc = com->lc_ops->lfsck_reset(env, com, false);
2984 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2985 if (bk->lb_param & LPF_DRYRUN)
2986 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2988 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2989 valid |= DOIV_ERROR_HANDLE;
2990 if (start->ls_flags & LPF_FAILOUT)
2991 flags |= DOIF_FAILOUT;
2994 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2995 valid |= DOIV_DRYRUN;
2996 if (start->ls_flags & LPF_DRYRUN)
2997 flags |= DOIF_DRYRUN;
3000 if (!list_empty(&lfsck->li_list_scan))
3001 flags |= DOIF_OUTUSED;
3003 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3004 thread_set_flags(thread, 0);
3005 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3007 GOTO(out, rc = PTR_ERR(lta));
3009 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3010 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3013 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3014 lfsck_lfsck2name(lfsck), rc);
3015 lfsck_thread_args_fini(lta);
3020 l_wait_event(thread->t_ctl_waitq,
3021 thread_is_running(thread) ||
3022 thread_is_stopped(thread),
3024 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3025 lfsck->li_start_unplug = 1;
3026 wake_up_all(&thread->t_ctl_waitq);
3031 /* release lfsck::li_mutex to avoid deadlock. */
3032 mutex_unlock(&lfsck->li_mutex);
3033 rc = lfsck_start_all(env, lfsck, start);
3035 spin_lock(&lfsck->li_lock);
3036 if (thread_is_stopped(thread)) {
3037 spin_unlock(&lfsck->li_lock);
3039 lfsck->li_status = LS_FAILED;
3040 lfsck->li_flags = 0;
3041 thread_set_flags(thread, SVC_STOPPING);
3042 spin_unlock(&lfsck->li_lock);
3044 lfsck->li_start_unplug = 1;
3045 wake_up_all(&thread->t_ctl_waitq);
3046 l_wait_event(thread->t_ctl_waitq,
3047 thread_is_stopped(thread),
3051 lfsck->li_start_unplug = 1;
3052 wake_up_all(&thread->t_ctl_waitq);
3058 mutex_unlock(&lfsck->li_mutex);
3061 lfsck_instance_put(env, lfsck);
3063 return rc < 0 ? rc : 0;
3065 EXPORT_SYMBOL(lfsck_start);
3067 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3068 struct lfsck_stop *stop)
3070 struct lfsck_instance *lfsck;
3071 struct ptlrpc_thread *thread;
3072 struct l_wait_info lwi = { 0 };
3077 lfsck = lfsck_instance_find(key, true, false);
3078 if (unlikely(lfsck == NULL))
3081 thread = &lfsck->li_thread;
3082 /* release lfsck::li_mutex to avoid deadlock. */
3083 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3084 if (!lfsck->li_master) {
3085 CERROR("%s: only allow to specify '-A' via MDS\n",
3086 lfsck_lfsck2name(lfsck));
3088 GOTO(out, rc = -EPERM);
3091 rc1 = lfsck_stop_all(env, lfsck, stop);
3094 mutex_lock(&lfsck->li_mutex);
3095 spin_lock(&lfsck->li_lock);
3096 /* no error if LFSCK is already stopped, or was never started */
3097 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3098 spin_unlock(&lfsck->li_lock);
3103 lfsck->li_status = stop->ls_status;
3104 lfsck->li_flags = stop->ls_flags;
3106 lfsck->li_status = LS_STOPPED;
3107 lfsck->li_flags = 0;
3110 thread_set_flags(thread, SVC_STOPPING);
3111 spin_unlock(&lfsck->li_lock);
3113 wake_up_all(&thread->t_ctl_waitq);
3114 l_wait_event(thread->t_ctl_waitq,
3115 thread_is_stopped(thread),
3121 mutex_unlock(&lfsck->li_mutex);
3122 lfsck_instance_put(env, lfsck);
3124 return rc != 0 ? rc : rc1;
3126 EXPORT_SYMBOL(lfsck_stop);
3128 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3129 struct lfsck_request *lr, struct thandle *th)
3131 int rc = -EOPNOTSUPP;
3134 switch (lr->lr_event) {
3136 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3137 struct lfsck_start_param lsp;
3139 memset(start, 0, sizeof(*start));
3140 start->ls_valid = lr->lr_valid;
3141 start->ls_speed_limit = lr->lr_speed;
3142 start->ls_version = lr->lr_version;
3143 start->ls_active = lr->lr_active;
3144 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3145 start->ls_async_windows = lr->lr_async_windows;
3147 lsp.lsp_start = start;
3148 lsp.lsp_index = lr->lr_index;
3149 lsp.lsp_index_valid = 1;
3150 rc = lfsck_start(env, key, &lsp);
3154 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3156 memset(stop, 0, sizeof(*stop));
3157 stop->ls_status = lr->lr_status;
3158 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3159 rc = lfsck_stop(env, key, stop);
3162 case LE_PHASE1_DONE:
3163 case LE_PHASE2_DONE:
3164 case LE_FID_ACCESSED:
3166 case LE_CONDITIONAL_DESTROY:
3167 case LE_SKIP_NLINK_DECLARE:
3169 case LE_SET_LMV_MASTER:
3170 case LE_SET_LMV_SLAVE:
3171 case LE_PAIRS_VERIFY: {
3172 struct lfsck_instance *lfsck;
3173 struct lfsck_component *com;
3175 lfsck = lfsck_instance_find(key, true, false);
3176 if (unlikely(lfsck == NULL))
3179 com = lfsck_component_find(lfsck, lr->lr_active);
3180 if (likely(com != NULL)) {
3181 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3182 lfsck_component_put(env, com);
3185 lfsck_instance_put(env, lfsck);
3194 EXPORT_SYMBOL(lfsck_in_notify);
3196 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3197 struct lfsck_request *lr)
3199 struct lfsck_instance *lfsck;
3200 struct lfsck_component *com;
3204 lfsck = lfsck_instance_find(key, true, false);
3205 if (unlikely(lfsck == NULL))
3208 com = lfsck_component_find(lfsck, lr->lr_active);
3209 if (likely(com != NULL)) {
3210 rc = com->lc_ops->lfsck_query(env, com);
3211 lfsck_component_put(env, com);
3216 lfsck_instance_put(env, lfsck);
3221 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3222 struct ldlm_namespace *ns)
3224 struct lfsck_instance *lfsck;
3227 lfsck = lfsck_instance_find(key, true, false);
3228 if (likely(lfsck != NULL)) {
3229 lfsck->li_namespace = ns;
3230 lfsck_instance_put(env, lfsck);
3236 EXPORT_SYMBOL(lfsck_register_namespace);
3238 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3239 struct dt_device *next, struct obd_device *obd,
3240 lfsck_out_notify notify, void *notify_data, bool master)
3242 struct lfsck_instance *lfsck;
3243 struct dt_object *root = NULL;
3244 struct dt_object *obj = NULL;
3245 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3249 lfsck = lfsck_instance_find(key, false, false);
3250 if (unlikely(lfsck != NULL))
3253 OBD_ALLOC_PTR(lfsck);
3257 mutex_init(&lfsck->li_mutex);
3258 spin_lock_init(&lfsck->li_lock);
3259 INIT_LIST_HEAD(&lfsck->li_link);
3260 INIT_LIST_HEAD(&lfsck->li_list_scan);
3261 INIT_LIST_HEAD(&lfsck->li_list_dir);
3262 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3263 INIT_LIST_HEAD(&lfsck->li_list_idle);
3264 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3265 atomic_set(&lfsck->li_ref, 1);
3266 atomic_set(&lfsck->li_double_scan_count, 0);
3267 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3268 lfsck->li_out_notify = notify;
3269 lfsck->li_out_notify_data = notify_data;
3270 lfsck->li_next = next;
3271 lfsck->li_bottom = key;
3272 lfsck->li_obd = obd;
3274 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3278 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3282 fid->f_seq = FID_SEQ_LOCAL_NAME;
3285 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3289 rc = dt_root_get(env, key, fid);
3293 root = dt_locate(env, key, fid);
3295 GOTO(out, rc = PTR_ERR(root));
3297 if (unlikely(!dt_try_as_dir(env, root)))
3298 GOTO(out, rc = -ENOTDIR);
3300 lfsck->li_local_root_fid = *fid;
3302 lfsck->li_master = 1;
3303 if (lfsck_dev_idx(lfsck) == 0) {
3304 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3305 const struct lu_name *cname;
3307 rc = dt_lookup(env, root,
3308 (struct dt_rec *)(&lfsck->li_global_root_fid),
3309 (const struct dt_key *)"ROOT");
3313 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3315 GOTO(out, rc = PTR_ERR(obj));
3317 if (unlikely(!dt_try_as_dir(env, obj)))
3318 GOTO(out, rc = -ENOTDIR);
3320 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3321 (const struct dt_key *)dotlustre);
3325 lfsck_object_put(env, obj);
3326 obj = dt_locate(env, key, fid);
3328 GOTO(out, rc = PTR_ERR(obj));
3330 cname = lfsck_name_get_const(env, dotlustre,
3332 rc = lfsck_verify_linkea(env, obj, cname,
3333 &lfsck->li_global_root_fid);
3337 if (unlikely(!dt_try_as_dir(env, obj)))
3338 GOTO(out, rc = -ENOTDIR);
3341 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3342 (const struct dt_key *)lostfound);
3346 lfsck_object_put(env, obj);
3347 obj = dt_locate(env, key, fid);
3349 GOTO(out, rc = PTR_ERR(obj));
3351 cname = lfsck_name_get_const(env, lostfound,
3353 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3357 lfsck_object_put(env, obj);
3362 fid->f_seq = FID_SEQ_LOCAL_FILE;
3363 fid->f_oid = OTABLE_IT_OID;
3365 obj = dt_locate(env, key, fid);
3367 GOTO(out, rc = PTR_ERR(obj));
3369 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3373 lfsck->li_obj_oit = obj;
3374 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3375 S_IFDIR | S_IRUGO | S_IWUSR);
3377 GOTO(out, rc = PTR_ERR(obj));
3379 lu_object_get(&obj->do_lu);
3380 lfsck->li_lfsck_dir = obj;
3381 rc = lfsck_bookmark_setup(env, lfsck);
3386 rc = lfsck_fid_init(lfsck);
3390 rc = lfsck_namespace_setup(env, lfsck);
3395 rc = lfsck_layout_setup(env, lfsck);
3399 /* XXX: more LFSCK components initialization to be added here. */
3401 rc = lfsck_instance_add(lfsck);
3403 rc = lfsck_add_target_from_orphan(env, lfsck);
3405 if (obj != NULL && !IS_ERR(obj))
3406 lfsck_object_put(env, obj);
3407 if (root != NULL && !IS_ERR(root))
3408 lfsck_object_put(env, root);
3410 lfsck_instance_cleanup(env, lfsck);
3413 EXPORT_SYMBOL(lfsck_register);
3415 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3417 struct lfsck_instance *lfsck;
3419 lfsck = lfsck_instance_find(key, false, true);
3421 lfsck_instance_put(env, lfsck);
3423 EXPORT_SYMBOL(lfsck_degister);
3425 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3426 struct dt_device *tgt, struct obd_export *exp,
3427 __u32 index, bool for_ost)
3429 struct lfsck_instance *lfsck;
3430 struct lfsck_tgt_desc *ltd;
3441 INIT_LIST_HEAD(<d->ltd_orphan_list);
3442 INIT_LIST_HEAD(<d->ltd_layout_list);
3443 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3444 INIT_LIST_HEAD(<d->ltd_namespace_list);
3445 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3446 atomic_set(<d->ltd_ref, 1);
3447 ltd->ltd_index = index;
3449 spin_lock(&lfsck_instance_lock);
3450 lfsck = __lfsck_instance_find(key, true, false);
3451 if (lfsck == NULL) {
3453 list_add_tail(<d->ltd_orphan_list,
3454 &lfsck_ost_orphan_list);
3456 list_add_tail(<d->ltd_orphan_list,
3457 &lfsck_mdt_orphan_list);
3458 spin_unlock(&lfsck_instance_lock);
3462 spin_unlock(&lfsck_instance_lock);
3464 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3468 lfsck_instance_put(env, lfsck);
3472 EXPORT_SYMBOL(lfsck_add_target);
3474 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3475 struct dt_device *tgt, __u32 index, bool for_ost)
3477 struct lfsck_instance *lfsck;
3478 struct lfsck_tgt_descs *ltds;
3479 struct lfsck_tgt_desc *ltd;
3480 struct list_head *head;
3483 head = &lfsck_ost_orphan_list;
3485 head = &lfsck_mdt_orphan_list;
3487 spin_lock(&lfsck_instance_lock);
3488 list_for_each_entry(ltd, head, ltd_orphan_list) {
3489 if (ltd->ltd_tgt == tgt) {
3490 list_del_init(<d->ltd_orphan_list);
3491 spin_unlock(&lfsck_instance_lock);
3499 lfsck = __lfsck_instance_find(key, true, false);
3500 spin_unlock(&lfsck_instance_lock);
3501 if (unlikely(lfsck == NULL))
3505 ltds = &lfsck->li_ost_descs;
3507 ltds = &lfsck->li_mdt_descs;
3509 down_write(<ds->ltd_rw_sem);
3510 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3512 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3515 ltd = LTD_TGT(ltds, index);
3516 if (unlikely(ltd == NULL))
3519 LASSERT(ltds->ltd_tgtnr > 0);
3522 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3523 LTD_TGT(ltds, index) = NULL;
3528 head = &lfsck->li_ost_descs.ltd_orphan;
3530 head = &lfsck->li_mdt_descs.ltd_orphan;
3532 list_for_each_entry(ltd, head, ltd_orphan_list) {
3533 if (ltd->ltd_tgt == tgt) {
3534 list_del_init(<d->ltd_orphan_list);
3540 up_write(<ds->ltd_rw_sem);
3542 spin_lock(<ds->ltd_lock);
3544 spin_unlock(<ds->ltd_lock);
3545 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3546 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3550 lfsck_instance_put(env, lfsck);
3552 EXPORT_SYMBOL(lfsck_del_target);
3554 static int __init lfsck_init(void)
3558 INIT_LIST_HEAD(&lfsck_instance_list);
3559 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3560 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3561 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3562 rc = lu_context_key_register(&lfsck_thread_key);
3564 tgt_register_lfsck_in_notify(lfsck_in_notify);
3565 tgt_register_lfsck_query(lfsck_query);
3571 static void __exit lfsck_exit(void)
3573 struct lfsck_tgt_desc *ltd;
3574 struct lfsck_tgt_desc *next;
3576 LASSERT(list_empty(&lfsck_instance_list));
3578 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3580 list_del_init(<d->ltd_orphan_list);
3584 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3586 list_del_init(<d->ltd_orphan_list);
3590 lu_context_key_degister(&lfsck_thread_key);
3593 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3594 MODULE_DESCRIPTION("LFSCK");
3595 MODULE_VERSION(LUSTRE_VERSION_STRING);
3596 MODULE_LICENSE("GPL");
3598 module_init(lfsck_init);
3599 module_exit(lfsck_exit);