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 <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 = lfsck_ltd2tgt(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 lfsck_assign_tgt(ltds, NULL, idx);
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 lfsck_assign_tgt(ltds, ltd, index);
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, struct ldlm_res_id *resid,
368 struct lustre_handle *lh, __u64 bits,
371 struct lfsck_thread_info *info = lfsck_env_info(env);
372 union ldlm_policy_data *policy = &info->lti_policy;
373 __u64 flags = LDLM_FL_ATOMIC_CB;
376 LASSERT(lfsck->li_namespace != NULL);
378 memset(policy, 0, sizeof(*policy));
379 policy->l_inodebits.bits = bits;
380 if (dt_object_remote(obj)) {
381 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
383 memset(einfo, 0, sizeof(*einfo));
384 einfo->ei_type = LDLM_IBITS;
385 einfo->ei_mode = mode;
386 einfo->ei_cb_bl = ldlm_blocking_ast;
387 einfo->ei_cb_cp = ldlm_completion_ast;
388 einfo->ei_res_id = resid;
390 rc = dt_object_lock(env, obj, lh, einfo, policy);
392 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
393 LDLM_IBITS, policy, mode,
394 &flags, ldlm_blocking_ast,
395 ldlm_completion_ast, NULL, NULL,
396 0, LVB_T_NONE, NULL, lh);
399 if (rc == ELDLM_OK) {
402 memset(lh, 0, sizeof(*lh));
410 * Request the specified ibits lock for the given object.
412 * Before the LFSCK modifying on the namespace visible object,
413 * it needs to acquire related ibits ldlm lock.
415 * \param[in] env pointer to the thread context
416 * \param[in] lfsck pointer to the lfsck instance
417 * \param[in] obj pointer to the dt_object to be locked
418 * \param[out] lh pointer to the lock handle
419 * \param[in] bits the bits for the ldlm lock to be acquired
420 * \param[in] mode the mode for the ldlm lock to be acquired
422 * \retval 0 for success
423 * \retval negative error number on failure
425 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
426 struct dt_object *obj, struct lustre_handle *lh,
427 __u64 bits, enum ldlm_mode mode)
429 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
431 LASSERT(!lustre_handle_is_used(lh));
433 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
434 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
438 * Release the the specified ibits lock.
440 * If the lock has been acquired before, release it
441 * and cleanup the handle. Otherwise, do nothing.
443 * \param[in] lh pointer to the lock handle
444 * \param[in] mode the mode for the ldlm lock to be released
446 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
448 if (lustre_handle_is_used(lh)) {
449 ldlm_lock_decref(lh, mode);
450 memset(lh, 0, sizeof(*lh));
455 * Request compound ibits locks for the given <obj, name> pairs.
457 * Before the LFSCK modifying on the namespace visible object, it needs to
458 * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
459 * the lock purpose. But the simple lfsck_ibits_lock for directory-based
460 * modificationis (such as insert name entry to the directory) may be too
461 * coarse-grained and not efficient.
463 * The lfsck_lock() will request compound ibits locks on the specified
464 * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
465 * lock on the directory object, and the regular ibits lock on the name hash.
467 * \param[in] env pointer to the thread context
468 * \param[in] lfsck pointer to the lfsck instance
469 * \param[in] obj pointer to the dt_object to be locked
470 * \param[in] name used for building the PDO lock resource
471 * \param[out] llh pointer to the lfsck_lock_handle
472 * \param[in] bits the bits for the ldlm lock to be acquired
473 * \param[in] mode the mode for the ldlm lock to be acquired
475 * \retval 0 for success
476 * \retval negative error number on failure
478 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
479 struct dt_object *obj, const char *name,
480 struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
482 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
485 LASSERT(S_ISDIR(lfsck_object_type(obj)));
486 LASSERT(name != NULL);
487 LASSERT(name[0] != 0);
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 LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
517 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
518 bits, llh->llh_reg_mode);
520 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
526 * Release the the compound ibits locks.
528 * \param[in] llh pointer to the lfsck_lock_handle to be released
530 void lfsck_unlock(struct lfsck_lock_handle *llh)
532 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
533 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
536 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
537 struct lfsck_instance *lfsck,
538 const struct lu_fid *fid)
540 struct seq_server_site *ss = lfsck_dev_site(lfsck);
541 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
544 fld_range_set_mdt(range);
545 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
547 rc = range->lsr_index;
552 const char dot[] = ".";
553 const char dotdot[] = "..";
554 static const char dotlustre[] = ".lustre";
555 static const char lostfound[] = "lost+found";
558 * Remove the name entry from the .lustre/lost+found directory.
560 * No need to care about the object referenced by the name entry,
561 * either the name entry is invalid or redundant, or the referenced
562 * object has been processed or will be handled by others.
564 * \param[in] env pointer to the thread context
565 * \param[in] lfsck pointer to the lfsck instance
566 * \param[in] name the name for the name entry to be removed
568 * \retval 0 for success
569 * \retval negative error number on failure
571 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
572 struct lfsck_instance *lfsck,
575 struct dt_object *parent = lfsck->li_lpf_root_obj;
576 struct dt_device *dev = lfsck_obj2dev(parent);
578 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
582 rc = lfsck_lock(env, lfsck, parent, name, llh,
583 MDS_INODELOCK_UPDATE, LCK_PW);
587 th = dt_trans_create(env, dev);
589 GOTO(unlock, rc = PTR_ERR(th));
591 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
595 rc = dt_declare_ref_del(env, parent, th);
599 rc = dt_trans_start_local(env, dev, th);
603 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
607 dt_write_lock(env, parent, 0);
608 rc = dt_ref_del(env, parent, th);
609 dt_write_unlock(env, parent);
614 dt_trans_stop(env, dev, th);
619 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
620 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
625 static int lfsck_create_lpf_local(const struct lu_env *env,
626 struct lfsck_instance *lfsck,
627 struct dt_object *child,
629 struct dt_object_format *dof,
632 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
633 struct dt_object *parent = lfsck->li_lpf_root_obj;
634 struct dt_device *dev = lfsck_obj2dev(child);
635 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
636 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
637 const struct lu_fid *cfid = lfsck_dto2fid(child);
638 struct thandle *th = NULL;
639 struct linkea_data ldata = { NULL };
640 struct lu_buf linkea_buf;
641 const struct lu_name *cname;
643 int len = sizeof(struct lfsck_bookmark);
647 rc = linkea_data_new(&ldata,
648 &lfsck_env_info(env)->lti_linkea_buf2);
652 cname = lfsck_name_get_const(env, name, strlen(name));
653 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
657 th = dt_trans_create(env, dev);
661 /* 1a. create child */
662 rc = dt_declare_create(env, child, la, NULL, dof, th);
666 /* 2a. increase child nlink */
667 rc = dt_declare_ref_add(env, child, th);
671 /* 3a. insert linkEA for child */
672 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
673 ldata.ld_leh->leh_len);
674 rc = dt_declare_xattr_set(env, child, &linkea_buf,
675 XATTR_NAME_LINK, 0, th);
679 /* 4a. insert name into parent dir */
680 rec->rec_type = S_IFDIR;
682 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
683 (const struct dt_key *)name, th);
687 /* 5a. increase parent nlink */
688 rc = dt_declare_ref_add(env, parent, th);
692 /* 6a. update bookmark */
693 rc = dt_declare_record_write(env, bk_obj,
694 lfsck_buf_get(env, bk, len), 0, th);
698 rc = dt_trans_start_local(env, dev, th);
702 dt_write_lock(env, child, 0);
703 /* 1b.1. create child */
704 rc = dt_create(env, child, la, NULL, dof, th);
708 if (unlikely(!dt_try_as_dir(env, child)))
709 GOTO(unlock, rc = -ENOTDIR);
711 /* 1b.2. insert dot into child dir */
713 rc = dt_insert(env, child, (const struct dt_rec *)rec,
714 (const struct dt_key *)dot, th, 1);
718 /* 1b.3. insert dotdot into child dir */
719 rec->rec_fid = &LU_LPF_FID;
720 rc = dt_insert(env, child, (const struct dt_rec *)rec,
721 (const struct dt_key *)dotdot, th, 1);
725 /* 2b. increase child nlink */
726 rc = dt_ref_add(env, child, th);
730 /* 3b. insert linkEA for child. */
731 rc = dt_xattr_set(env, child, &linkea_buf,
732 XATTR_NAME_LINK, 0, th);
733 dt_write_unlock(env, child);
737 /* 4b. insert name into parent dir */
739 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
740 (const struct dt_key *)name, th, 1);
744 dt_write_lock(env, parent, 0);
745 /* 5b. increase parent nlink */
746 rc = dt_ref_add(env, parent, th);
747 dt_write_unlock(env, parent);
751 bk->lb_lpf_fid = *cfid;
752 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
754 /* 6b. update bookmark */
755 rc = dt_record_write(env, bk_obj,
756 lfsck_buf_get(env, bk, len), &pos, th);
761 dt_write_unlock(env, child);
764 dt_trans_stop(env, dev, th);
769 static int lfsck_create_lpf_remote(const struct lu_env *env,
770 struct lfsck_instance *lfsck,
771 struct dt_object *child,
773 struct dt_object_format *dof,
776 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
777 struct dt_object *parent = lfsck->li_lpf_root_obj;
778 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
779 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
780 const struct lu_fid *cfid = lfsck_dto2fid(child);
781 struct thandle *th = NULL;
782 struct linkea_data ldata = { NULL };
783 struct lu_buf linkea_buf;
784 const struct lu_name *cname;
785 struct dt_device *dev;
787 int len = sizeof(struct lfsck_bookmark);
791 rc = linkea_data_new(&ldata,
792 &lfsck_env_info(env)->lti_linkea_buf2);
796 cname = lfsck_name_get_const(env, name, strlen(name));
797 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
801 /* Create .lustre/lost+found/MDTxxxx. */
803 /* XXX: Currently, cross-MDT create operation needs to create the child
804 * object firstly, then insert name into the parent directory. For
805 * this case, the child object resides on current MDT (local), but
806 * the parent ".lustre/lost+found" may be on remote MDT. It is not
807 * easy to contain all the sub-modifications orderly within single
810 * To avoid more inconsistency, we split the create operation into
813 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
815 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
818 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
819 * repair such inconsistency when LFSCK run next time. */
821 /* Transaction I: locally */
823 dev = lfsck_obj2dev(child);
824 th = dt_trans_create(env, dev);
828 /* 1a. create child */
829 rc = dt_declare_create(env, child, la, NULL, dof, th);
833 /* 2a. increase child nlink */
834 rc = dt_declare_ref_add(env, child, th);
838 /* 3a. insert linkEA for child */
839 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
840 ldata.ld_leh->leh_len);
841 rc = dt_declare_xattr_set(env, child, &linkea_buf,
842 XATTR_NAME_LINK, 0, th);
846 /* 4a. update bookmark */
847 rc = dt_declare_record_write(env, bk_obj,
848 lfsck_buf_get(env, bk, len), 0, th);
852 rc = dt_trans_start_local(env, dev, th);
856 dt_write_lock(env, child, 0);
857 /* 1b.1. create child */
858 rc = dt_create(env, child, la, NULL, dof, th);
862 if (unlikely(!dt_try_as_dir(env, child)))
863 GOTO(unlock, rc = -ENOTDIR);
865 /* 1b.2. insert dot into child dir */
866 rec->rec_type = S_IFDIR;
868 rc = dt_insert(env, child, (const struct dt_rec *)rec,
869 (const struct dt_key *)dot, th, 1);
873 /* 1b.3. insert dotdot into child dir */
874 rec->rec_fid = &LU_LPF_FID;
875 rc = dt_insert(env, child, (const struct dt_rec *)rec,
876 (const struct dt_key *)dotdot, th, 1);
880 /* 2b. increase child nlink */
881 rc = dt_ref_add(env, child, th);
885 /* 3b. insert linkEA for child */
886 rc = dt_xattr_set(env, child, &linkea_buf,
887 XATTR_NAME_LINK, 0, th);
891 bk->lb_lpf_fid = *cfid;
892 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
894 /* 4b. update bookmark */
895 rc = dt_record_write(env, bk_obj,
896 lfsck_buf_get(env, bk, len), &pos, th);
898 dt_write_unlock(env, child);
899 dt_trans_stop(env, dev, th);
903 /* Transaction II: remotely */
905 dev = lfsck_obj2dev(parent);
906 th = dt_trans_create(env, dev);
911 /* 5a. insert name into parent dir */
913 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
914 (const struct dt_key *)name, th);
918 /* 6a. increase parent nlink */
919 rc = dt_declare_ref_add(env, parent, th);
923 rc = dt_trans_start_local(env, dev, th);
927 /* 5b. insert name into parent dir */
928 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
929 (const struct dt_key *)name, th, 1);
933 dt_write_lock(env, parent, 0);
934 /* 6b. increase parent nlink */
935 rc = dt_ref_add(env, parent, th);
936 dt_write_unlock(env, parent);
941 dt_write_unlock(env, child);
943 dt_trans_stop(env, dev, th);
945 if (rc != 0 && dev == lfsck_obj2dev(parent))
946 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
947 "for orphans, but failed to insert the name %s "
948 "to the .lustre/lost+found/. Such inconsistency "
949 "will be repaired when LFSCK run next time: rc = %d\n",
950 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
956 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
958 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
959 * orphans and other uncertain inconsistent objects found during the
960 * LFSCK. Such directory will be created by the LFSCK engine on the
961 * local MDT before the LFSCK scanning.
963 * \param[in] env pointer to the thread context
964 * \param[in] lfsck pointer to the lfsck instance
966 * \retval 0 for success
967 * \retval negative error number on failure
969 static int lfsck_create_lpf(const struct lu_env *env,
970 struct lfsck_instance *lfsck)
972 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
973 struct lfsck_thread_info *info = lfsck_env_info(env);
974 struct lu_fid *cfid = &info->lti_fid2;
975 struct lu_attr *la = &info->lti_la;
976 struct dt_object_format *dof = &info->lti_dof;
977 struct dt_object *parent = lfsck->li_lpf_root_obj;
978 struct dt_object *child = NULL;
979 struct lfsck_lock_handle *llh = &info->lti_llh;
981 int node = lfsck_dev_idx(lfsck);
985 LASSERT(lfsck->li_master);
986 LASSERT(parent != NULL);
987 LASSERT(lfsck->li_lpf_obj == NULL);
989 snprintf(name, 8, "MDT%04x", node);
990 rc = lfsck_lock(env, lfsck, parent, name, llh,
991 MDS_INODELOCK_UPDATE, LCK_PW);
995 if (fid_is_zero(&bk->lb_lpf_fid)) {
996 /* There is corner case that: in former LFSCK scanning we have
997 * created the .lustre/lost+found/MDTxxxx but failed to update
998 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
999 * it from MDT0 firstly. */
1000 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1001 (const struct dt_key *)name);
1002 if (rc != 0 && rc != -ENOENT)
1006 bk->lb_lpf_fid = *cfid;
1007 rc = lfsck_bookmark_store(env, lfsck);
1009 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1014 *cfid = bk->lb_lpf_fid;
1017 child = lfsck_object_find_bottom(env, lfsck, cfid);
1019 GOTO(unlock, rc = PTR_ERR(child));
1021 if (dt_object_exists(child) != 0) {
1022 if (unlikely(!dt_try_as_dir(env, child)))
1025 lfsck->li_lpf_obj = child;
1030 memset(la, 0, sizeof(*la));
1031 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1032 la->la_mode = S_IFDIR | S_IRWXU;
1033 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1035 memset(dof, 0, sizeof(*dof));
1036 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1039 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1041 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1043 lfsck->li_lpf_obj = child;
1049 if (rc != 0 && child != NULL && !IS_ERR(child))
1050 lfsck_object_put(env, child);
1056 * Scan .lustre/lost+found for bad name entries and remove them.
1058 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1059 * index in the system. Any other formatted name is invalid and should be
1062 * \param[in] env pointer to the thread context
1063 * \param[in] lfsck pointer to the lfsck instance
1065 * \retval 0 for success
1066 * \retval negative error number on failure
1068 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1069 struct lfsck_instance *lfsck)
1071 struct dt_object *parent = lfsck->li_lpf_root_obj;
1072 struct lu_dirent *ent =
1073 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1074 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1079 it = iops->init(env, parent, LUDA_64BITHASH);
1081 RETURN(PTR_ERR(it));
1083 rc = iops->load(env, it, 0);
1085 rc = iops->next(env, it);
1092 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1096 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1097 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1100 /* name length must be strlen("MDTxxxx") */
1101 if (ent->lde_namelen != 7)
1104 if (memcmp(ent->lde_name, "MDT", off) != 0)
1107 while (off < 7 && isxdigit(ent->lde_name[off]))
1113 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1120 rc = iops->next(env, it);
1124 iops->fini(env, it);
1126 RETURN(rc > 0 ? 0 : rc);
1129 static int lfsck_update_lpf_entry(const struct lu_env *env,
1130 struct lfsck_instance *lfsck,
1131 struct dt_object *parent,
1132 struct dt_object *child,
1134 enum lfsck_verify_lpf_types type)
1138 if (type == LVLT_BY_BOOKMARK) {
1139 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1140 lfsck_dto2fid(child), S_IFDIR);
1141 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1142 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1143 rc = lfsck_bookmark_store(env, lfsck);
1145 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1146 " in the bookmark file: rc = %d\n",
1147 lfsck_lfsck2name(lfsck),
1148 PFID(lfsck_dto2fid(child)), rc);
1155 * Check whether the @child back references the @parent.
1158 * 1) The child's FID is stored in the bookmark file. If the child back
1159 * references the parent (LU_LPF_FID object) via its ".." entry, then
1160 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1161 * the child back references another parent2, then:
1162 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1163 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1164 * references the child. So keep them there. As the LFSCK processing,
1165 * the parent3 may be found, then when the LFSCK run next time, the
1166 * inconsistency can be repaired.
1168 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1169 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1170 * via its ".." entry, then update the bookmark file, otherwise, if the child
1171 * back references another parent2, then:
1172 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1173 * from .lustre/lost+found/;
1174 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1175 * sub-directory name entry and update the child;
1176 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1177 * or not, then keep them there.
1179 * \param[in] env pointer to the thread context
1180 * \param[in] lfsck pointer to the lfsck instance
1181 * \param[in] child pointer to the lost+found sub-directory object
1182 * \param[in] name the name for lost+found sub-directory object
1183 * \param[out] fid pointer to the buffer to hold the FID of the object
1184 * (called it as parent2) that is referenced via the
1185 * child's dotdot entry; it also can be the FID that
1186 * is referenced by the name entry under the parent2.
1187 * \param[in] type to indicate where the child's FID is stored in
1189 * \retval positive number for uncertain inconsistency
1190 * \retval 0 for success
1191 * \retval negative error number on failure
1193 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1194 struct lfsck_instance *lfsck,
1195 struct dt_object *child, const char *name,
1197 enum lfsck_verify_lpf_types type)
1199 struct dt_object *parent = lfsck->li_lpf_root_obj;
1200 struct lfsck_thread_info *info = lfsck_env_info(env);
1201 char *name2 = info->lti_key;
1202 struct lu_fid *fid2 = &info->lti_fid3;
1203 struct dt_object *parent2 = NULL;
1204 struct lustre_handle lh = { 0 };
1209 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1210 (const struct dt_key *)dotdot);
1214 if (!fid_is_sane(fid))
1215 GOTO(linkea, rc = -EINVAL);
1217 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1218 const struct lu_name *cname;
1220 if (lfsck->li_lpf_obj == NULL) {
1221 lu_object_get(&child->do_lu);
1222 lfsck->li_lpf_obj = child;
1225 cname = lfsck_name_get_const(env, name, strlen(name));
1226 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1228 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1234 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1235 if (IS_ERR(parent2))
1236 GOTO(linkea, parent2);
1238 if (!dt_object_exists(parent2)) {
1239 lfsck_object_put(env, parent2);
1241 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1244 if (!dt_try_as_dir(env, parent2)) {
1245 lfsck_object_put(env, parent2);
1247 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1251 /* To prevent rename/unlink race */
1252 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1253 MDS_INODELOCK_UPDATE, LCK_PR);
1257 dt_read_lock(env, child, 0);
1258 rc = lfsck_links_get_first(env, child, name2, fid2);
1260 dt_read_unlock(env, child);
1261 lfsck_ibits_unlock(&lh, LCK_PR);
1263 GOTO(out_put, rc = 1);
1266 /* It is almost impossible that the bookmark file (or the name entry)
1267 * and the linkEA hit the same data corruption. Trust the linkEA. */
1268 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1269 dt_read_unlock(env, child);
1270 lfsck_ibits_unlock(&lh, LCK_PR);
1273 if (lfsck->li_lpf_obj == NULL) {
1274 lu_object_get(&child->do_lu);
1275 lfsck->li_lpf_obj = child;
1278 /* Update the child's dotdot entry */
1279 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1280 &LU_LPF_FID, S_IFDIR);
1282 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1288 if (parent2 == NULL || IS_ERR(parent2)) {
1289 dt_read_unlock(env, child);
1290 lfsck_ibits_unlock(&lh, LCK_PR);
1292 GOTO(out_done, rc = 1);
1295 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1296 (const struct dt_key *)name2);
1297 dt_read_unlock(env, child);
1298 lfsck_ibits_unlock(&lh, LCK_PR);
1299 if (rc != 0 && rc != -ENOENT)
1302 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1303 if (type == LVLT_BY_BOOKMARK)
1304 GOTO(out_put, rc = 1);
1306 /* Trust the name entry, update the child's dotdot entry. */
1307 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1308 &LU_LPF_FID, S_IFDIR);
1313 if (type == LVLT_BY_BOOKMARK) {
1314 /* Invalid FID record in the bookmark file, reset it. */
1315 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1316 rc = lfsck_bookmark_store(env, lfsck);
1318 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1319 " in the bookmark file: rc = %d\n",
1320 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1321 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1322 /* The name entry is wrong, remove it. */
1323 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1329 if (parent2 != NULL && !IS_ERR(parent2))
1330 lfsck_object_put(env, parent2);
1337 * Verify the /ROOT/.lustre/lost+found/ directory.
1339 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1340 * the LFSCK does not exactly know how to handle, such as orphans. So before
1341 * the LFSCK scanning the system, the consistency of such directory needs to
1342 * be verified firstly to allow the users to use it during the LFSCK.
1344 * \param[in] env pointer to the thread context
1345 * \param[in] lfsck pointer to the lfsck instance
1347 * \retval positive number for uncertain inconsistency
1348 * \retval 0 for success
1349 * \retval negative error number on failure
1351 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1353 struct lfsck_thread_info *info = lfsck_env_info(env);
1354 struct lu_fid *pfid = &info->lti_fid;
1355 struct lu_fid *cfid = &info->lti_fid2;
1356 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1357 struct dt_object *parent;
1358 /* child1's FID is in the bookmark file. */
1359 struct dt_object *child1 = NULL;
1360 /* child2's FID is in the name entry MDTxxxx. */
1361 struct dt_object *child2 = NULL;
1362 const struct lu_name *cname;
1364 int node = lfsck_dev_idx(lfsck);
1368 LASSERT(lfsck->li_master);
1370 if (lfsck->li_lpf_root_obj != NULL)
1374 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1377 struct lfsck_tgt_desc *ltd;
1379 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1380 if (unlikely(ltd == NULL))
1383 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1389 RETURN(PTR_ERR(parent));
1391 LASSERT(dt_object_exists(parent));
1393 if (unlikely(!dt_try_as_dir(env, parent))) {
1394 lfsck_object_put(env, parent);
1396 GOTO(put, rc = -ENOTDIR);
1399 lfsck->li_lpf_root_obj = parent;
1401 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1403 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1404 "for bad sub-directories: rc = %d\n",
1405 lfsck_lfsck2name(lfsck), rc);
1408 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1409 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1410 struct lu_fid tfid = bk->lb_lpf_fid;
1412 /* Invalid FID record in the bookmark file, reset it. */
1413 fid_zero(&bk->lb_lpf_fid);
1414 rc = lfsck_bookmark_store(env, lfsck);
1416 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1417 " in the bookmark file: rc = %d\n",
1418 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1423 child1 = lfsck_object_find_bottom(env, lfsck,
1425 if (IS_ERR(child1)) {
1430 if (unlikely(!dt_object_exists(child1) ||
1431 dt_object_remote(child1)) ||
1432 !S_ISDIR(lfsck_object_type(child1))) {
1433 /* Invalid FID record in the bookmark file,
1435 fid_zero(&bk->lb_lpf_fid);
1436 rc = lfsck_bookmark_store(env, lfsck);
1438 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1439 " in the bookmark file: rc = %d\n",
1440 lfsck_lfsck2name(lfsck),
1441 PFID(lfsck_dto2fid(child1)), rc);
1446 lfsck_object_put(env, child1);
1448 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1449 GOTO(put, rc = -ENOTDIR);
1455 snprintf(name, 8, "MDT%04x", node);
1456 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1457 (const struct dt_key *)name);
1458 if (rc == -ENOENT) {
1459 if (!fid_is_zero(&bk->lb_lpf_fid))
1468 /* Invalid FID in the name entry, remove the name entry. */
1469 if (!fid_is_norm(cfid)) {
1470 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1477 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1479 GOTO(put, rc = PTR_ERR(child2));
1481 if (unlikely(!dt_object_exists(child2) ||
1482 dt_object_remote(child2)) ||
1483 !S_ISDIR(lfsck_object_type(child2))) {
1484 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1491 if (unlikely(!dt_try_as_dir(env, child2)))
1492 GOTO(put, rc = -ENOTDIR);
1494 if (child1 == NULL) {
1495 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1496 pfid, LVLT_BY_NAMEENTRY);
1497 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1498 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1499 pfid, LVLT_BY_BOOKMARK);
1500 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1501 rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1505 if (lfsck->li_lpf_obj == NULL) {
1506 lu_object_get(&child2->do_lu);
1507 lfsck->li_lpf_obj = child2;
1510 cname = lfsck_name_get_const(env, name, strlen(name));
1511 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1518 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1519 pfid, LVLT_BY_BOOKMARK);
1524 if (lfsck->li_lpf_obj != NULL) {
1525 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1526 lfsck_object_put(env, lfsck->li_lpf_obj);
1527 lfsck->li_lpf_obj = NULL;
1530 } else if (rc == 0) {
1531 rc = lfsck_create_lpf(env, lfsck);
1534 if (child2 != NULL && !IS_ERR(child2))
1535 lfsck_object_put(env, child2);
1536 if (child1 != NULL && !IS_ERR(child1))
1537 lfsck_object_put(env, child1);
1542 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1544 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1545 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1550 if (unlikely(ss == NULL))
1553 OBD_ALLOC_PTR(lfsck->li_seq);
1554 if (lfsck->li_seq == NULL)
1557 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1559 GOTO(out, rc = -ENOMEM);
1561 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1562 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1564 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1568 if (fid_is_sane(&bk->lb_last_fid))
1569 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1574 OBD_FREE_PTR(lfsck->li_seq);
1575 lfsck->li_seq = NULL;
1580 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1582 if (lfsck->li_seq != NULL) {
1583 seq_client_fini(lfsck->li_seq);
1584 OBD_FREE_PTR(lfsck->li_seq);
1585 lfsck->li_seq = NULL;
1589 void lfsck_instance_cleanup(const struct lu_env *env,
1590 struct lfsck_instance *lfsck)
1592 struct ptlrpc_thread *thread = &lfsck->li_thread;
1593 struct lfsck_component *com;
1594 struct lfsck_component *next;
1595 struct lfsck_lmv_unit *llu;
1596 struct lfsck_lmv_unit *llu_next;
1597 struct lfsck_lmv *llmv;
1600 LASSERT(list_empty(&lfsck->li_link));
1601 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1603 if (lfsck->li_obj_oit != NULL) {
1604 lfsck_object_put(env, lfsck->li_obj_oit);
1605 lfsck->li_obj_oit = NULL;
1608 LASSERT(lfsck->li_obj_dir == NULL);
1609 LASSERT(lfsck->li_lmv == NULL);
1611 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1612 llmv = &llu->llu_lmv;
1614 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1615 "still in using: %u\n",
1616 atomic_read(&llmv->ll_ref));
1618 lfsck_lmv_put(env, llmv);
1621 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1622 lfsck_component_cleanup(env, com);
1625 LASSERT(list_empty(&lfsck->li_list_dir));
1627 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1629 lfsck_component_cleanup(env, com);
1632 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1633 lfsck_component_cleanup(env, com);
1636 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1637 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1639 if (lfsck->li_lfsck_dir != NULL) {
1640 lfsck_object_put(env, lfsck->li_lfsck_dir);
1641 lfsck->li_lfsck_dir = NULL;
1644 if (lfsck->li_bookmark_obj != NULL) {
1645 lfsck_object_put(env, lfsck->li_bookmark_obj);
1646 lfsck->li_bookmark_obj = NULL;
1649 if (lfsck->li_lpf_obj != NULL) {
1650 lfsck_object_put(env, lfsck->li_lpf_obj);
1651 lfsck->li_lpf_obj = NULL;
1654 if (lfsck->li_lpf_root_obj != NULL) {
1655 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1656 lfsck->li_lpf_root_obj = NULL;
1659 if (lfsck->li_los != NULL) {
1660 local_oid_storage_fini(env, lfsck->li_los);
1661 lfsck->li_los = NULL;
1664 lfsck_fid_fini(lfsck);
1666 OBD_FREE_PTR(lfsck);
1669 static inline struct lfsck_instance *
1670 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1672 struct lfsck_instance *lfsck;
1674 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1675 if (lfsck->li_bottom == key) {
1677 lfsck_instance_get(lfsck);
1679 list_del_init(&lfsck->li_link);
1688 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1691 struct lfsck_instance *lfsck;
1693 spin_lock(&lfsck_instance_lock);
1694 lfsck = __lfsck_instance_find(key, ref, unlink);
1695 spin_unlock(&lfsck_instance_lock);
1700 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1702 struct lfsck_instance *tmp;
1704 spin_lock(&lfsck_instance_lock);
1705 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1706 if (lfsck->li_bottom == tmp->li_bottom) {
1707 spin_unlock(&lfsck_instance_lock);
1712 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1713 spin_unlock(&lfsck_instance_lock);
1717 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1722 bool newline = (bits != 0 ? false : true);
1725 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1729 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1732 if (names[i] != NULL) {
1736 rc = seq_printf(m, "%s%c", names[i],
1737 newline ? '\n' : ',');
1745 rc = seq_printf(m, "\n");
1750 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1755 rc = seq_printf(m, "%s_time: N/A\n", name);
1757 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1762 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1764 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1765 name, cfs_time_current_sec() - time);
1770 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1773 if (fid_is_zero(&pos->lp_dir_parent)) {
1774 if (pos->lp_oit_cookie == 0)
1775 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1777 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1778 prefix, pos->lp_oit_cookie);
1781 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1782 prefix, pos->lp_oit_cookie,
1783 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1786 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1787 struct lfsck_position *pos, bool init)
1789 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1791 if (unlikely(lfsck->li_di_oit == NULL)) {
1792 memset(pos, 0, sizeof(*pos));
1796 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1797 if (!lfsck->li_current_oit_processed && !init)
1798 pos->lp_oit_cookie--;
1800 LASSERT(pos->lp_oit_cookie > 0);
1802 if (lfsck->li_di_dir != NULL) {
1803 struct dt_object *dto = lfsck->li_obj_dir;
1805 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1808 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1809 fid_zero(&pos->lp_dir_parent);
1810 pos->lp_dir_cookie = 0;
1812 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1815 fid_zero(&pos->lp_dir_parent);
1816 pos->lp_dir_cookie = 0;
1820 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1824 if (limit != LFSCK_SPEED_NO_LIMIT) {
1825 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1826 lfsck->li_sleep_rate = limit /
1827 msecs_to_jiffies(MSEC_PER_SEC);
1828 lfsck->li_sleep_jif = 1;
1830 lfsck->li_sleep_rate = 1;
1831 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1835 lfsck->li_sleep_jif = 0;
1836 lfsck->li_sleep_rate = 0;
1839 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1840 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1847 void lfsck_control_speed(struct lfsck_instance *lfsck)
1849 struct ptlrpc_thread *thread = &lfsck->li_thread;
1850 struct l_wait_info lwi;
1852 if (lfsck->li_sleep_jif > 0 &&
1853 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1854 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1855 LWI_ON_SIGNAL_NOOP, NULL);
1857 l_wait_event(thread->t_ctl_waitq,
1858 !thread_is_running(thread),
1860 lfsck->li_new_scanned = 0;
1864 void lfsck_control_speed_by_self(struct lfsck_component *com)
1866 struct lfsck_instance *lfsck = com->lc_lfsck;
1867 struct ptlrpc_thread *thread = &lfsck->li_thread;
1868 struct l_wait_info lwi;
1870 if (lfsck->li_sleep_jif > 0 &&
1871 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1872 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1873 LWI_ON_SIGNAL_NOOP, NULL);
1875 l_wait_event(thread->t_ctl_waitq,
1876 !thread_is_running(thread),
1878 com->lc_new_scanned = 0;
1882 static struct lfsck_thread_args *
1883 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1884 struct lfsck_component *com,
1885 struct lfsck_start_param *lsp)
1887 struct lfsck_thread_args *lta;
1892 return ERR_PTR(-ENOMEM);
1894 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1900 lta->lta_lfsck = lfsck_instance_get(lfsck);
1902 lta->lta_com = lfsck_component_get(com);
1909 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1911 if (lta->lta_com != NULL)
1912 lfsck_component_put(<a->lta_env, lta->lta_com);
1913 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1914 lu_env_fini(<a->lta_env);
1918 struct lfsck_assistant_data *
1919 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1922 struct lfsck_assistant_data *lad;
1926 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1927 if (lad->lad_bitmap == NULL) {
1932 INIT_LIST_HEAD(&lad->lad_req_list);
1933 spin_lock_init(&lad->lad_lock);
1934 INIT_LIST_HEAD(&lad->lad_ost_list);
1935 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1936 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1937 INIT_LIST_HEAD(&lad->lad_mdt_list);
1938 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1939 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1940 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1942 lad->lad_name = name;
1948 struct lfsck_assistant_object *
1949 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1950 const struct lu_attr *attr, __u64 cookie,
1953 struct lfsck_assistant_object *lso;
1957 return ERR_PTR(-ENOMEM);
1959 lso->lso_fid = *fid;
1961 lso->lso_attr = *attr;
1963 atomic_set(&lso->lso_ref, 1);
1964 lso->lso_oit_cookie = cookie;
1966 lso->lso_is_dir = 1;
1972 lfsck_assistant_object_load(const struct lu_env *env,
1973 struct lfsck_instance *lfsck,
1974 struct lfsck_assistant_object *lso)
1976 struct dt_object *obj;
1978 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1982 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
1984 lfsck_object_put(env, obj);
1986 return ERR_PTR(-ENOENT);
1989 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
1990 lfsck_object_put(env, obj);
1992 return ERR_PTR(-ENOTDIR);
1999 * Generic LFSCK asynchronous communication interpretor function.
2000 * The LFSCK RPC reply for both the event notification and status
2001 * querying will be handled here.
2003 * \param[in] env pointer to the thread context
2004 * \param[in] req pointer to the LFSCK request
2005 * \param[in] args pointer to the lfsck_async_interpret_args
2006 * \param[in] rc the result for handling the LFSCK request
2008 * \retval 0 for success
2009 * \retval negative error number on failure
2011 int lfsck_async_interpret_common(const struct lu_env *env,
2012 struct ptlrpc_request *req,
2015 struct lfsck_async_interpret_args *laia = args;
2016 struct lfsck_component *com = laia->laia_com;
2017 struct lfsck_assistant_data *lad = com->lc_data;
2018 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2019 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2020 struct lfsck_request *lr = laia->laia_lr;
2022 LASSERT(com->lc_lfsck->li_master);
2024 switch (lr->lr_event) {
2027 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2029 lfsck_lfsck2name(com->lc_lfsck),
2030 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2031 ltd->ltd_index, lad->lad_name, rc);
2033 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2034 struct lfsck_layout *lo = com->lc_file_ram;
2036 if (lr->lr_flags & LEF_TO_OST)
2037 lfsck_lad_set_bitmap(env, com,
2040 lo->ll_flags |= LF_INCOMPLETE;
2042 struct lfsck_namespace *ns = com->lc_file_ram;
2044 /* If some MDT does not join the namespace
2045 * LFSCK, then we cannot know whether there
2046 * is some name entry on such MDT that with
2047 * the referenced MDT-object on this MDT or
2048 * not. So the namespace LFSCK on this MDT
2049 * cannot handle orphan MDT-objects properly.
2050 * So we mark the LFSCK as LF_INCOMPLETE and
2051 * skip orphan MDT-objects handling. */
2052 ns->ln_flags |= LF_INCOMPLETE;
2057 spin_lock(<ds->ltd_lock);
2058 if (ltd->ltd_dead) {
2059 spin_unlock(<ds->ltd_lock);
2063 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2064 struct list_head *list;
2065 struct list_head *phase_list;
2067 if (ltd->ltd_layout_done) {
2068 spin_unlock(<ds->ltd_lock);
2072 if (lr->lr_flags & LEF_TO_OST) {
2073 list = &lad->lad_ost_list;
2074 phase_list = &lad->lad_ost_phase1_list;
2076 list = &lad->lad_mdt_list;
2077 phase_list = &lad->lad_mdt_phase1_list;
2080 if (list_empty(<d->ltd_layout_list))
2081 list_add_tail(<d->ltd_layout_list, list);
2082 if (list_empty(<d->ltd_layout_phase_list))
2083 list_add_tail(<d->ltd_layout_phase_list,
2086 if (ltd->ltd_namespace_done) {
2087 spin_unlock(<ds->ltd_lock);
2091 if (list_empty(<d->ltd_namespace_list))
2092 list_add_tail(<d->ltd_namespace_list,
2093 &lad->lad_mdt_list);
2094 if (list_empty(<d->ltd_namespace_phase_list))
2095 list_add_tail(<d->ltd_namespace_phase_list,
2096 &lad->lad_mdt_phase1_list);
2098 spin_unlock(<ds->ltd_lock);
2101 case LE_PHASE1_DONE:
2102 case LE_PHASE2_DONE:
2104 if (rc != 0 && rc != -EALREADY)
2105 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2106 "event = %d, rc = %d\n",
2107 lfsck_lfsck2name(com->lc_lfsck),
2108 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2109 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2112 struct lfsck_reply *reply;
2113 struct list_head *list;
2114 struct list_head *phase_list;
2116 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2117 list = <d->ltd_layout_list;
2118 phase_list = <d->ltd_layout_phase_list;
2120 list = <d->ltd_namespace_list;
2121 phase_list = <d->ltd_namespace_phase_list;
2125 spin_lock(<ds->ltd_lock);
2126 list_del_init(phase_list);
2127 list_del_init(list);
2128 spin_unlock(<ds->ltd_lock);
2132 reply = req_capsule_server_get(&req->rq_pill,
2134 if (reply == NULL) {
2136 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2137 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2139 spin_lock(<ds->ltd_lock);
2140 list_del_init(phase_list);
2141 list_del_init(list);
2142 spin_unlock(<ds->ltd_lock);
2146 switch (reply->lr_status) {
2147 case LS_SCANNING_PHASE1:
2149 case LS_SCANNING_PHASE2:
2150 spin_lock(<ds->ltd_lock);
2151 list_del_init(phase_list);
2152 if (ltd->ltd_dead) {
2153 spin_unlock(<ds->ltd_lock);
2157 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2158 if (ltd->ltd_layout_done) {
2159 spin_unlock(<ds->ltd_lock);
2163 if (lr->lr_flags & LEF_TO_OST)
2164 list_add_tail(phase_list,
2165 &lad->lad_ost_phase2_list);
2167 list_add_tail(phase_list,
2168 &lad->lad_mdt_phase2_list);
2170 if (ltd->ltd_namespace_done) {
2171 spin_unlock(<ds->ltd_lock);
2175 list_add_tail(phase_list,
2176 &lad->lad_mdt_phase2_list);
2178 spin_unlock(<ds->ltd_lock);
2181 spin_lock(<ds->ltd_lock);
2182 list_del_init(phase_list);
2183 list_del_init(list);
2184 spin_unlock(<ds->ltd_lock);
2190 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2191 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2195 if (!laia->laia_shared) {
2197 lfsck_component_put(env, com);
2203 static void lfsck_interpret(const struct lu_env *env,
2204 struct lfsck_instance *lfsck,
2205 struct ptlrpc_request *req, void *args, int result)
2207 struct lfsck_async_interpret_args *laia = args;
2208 struct lfsck_component *com;
2210 LASSERT(laia->laia_com == NULL);
2211 LASSERT(laia->laia_shared);
2213 spin_lock(&lfsck->li_lock);
2214 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2215 laia->laia_com = com;
2216 lfsck_async_interpret_common(env, req, laia, result);
2219 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2220 laia->laia_com = com;
2221 lfsck_async_interpret_common(env, req, laia, result);
2223 spin_unlock(&lfsck->li_lock);
2226 static int lfsck_stop_notify(const struct lu_env *env,
2227 struct lfsck_instance *lfsck,
2228 struct lfsck_tgt_descs *ltds,
2229 struct lfsck_tgt_desc *ltd, __u16 type)
2231 struct lfsck_component *com;
2235 LASSERT(lfsck->li_master);
2237 spin_lock(&lfsck->li_lock);
2238 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2240 com = __lfsck_component_find(lfsck, type,
2241 &lfsck->li_list_double_scan);
2243 lfsck_component_get(com);
2244 spin_unlock(&lfsck->li_lock);
2247 struct lfsck_thread_info *info = lfsck_env_info(env);
2248 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2249 struct lfsck_request *lr = &info->lti_lr;
2250 struct lfsck_assistant_data *lad = com->lc_data;
2251 struct list_head *list;
2252 struct list_head *phase_list;
2253 struct ptlrpc_request_set *set;
2255 set = ptlrpc_prep_set();
2257 lfsck_component_put(env, com);
2262 if (type == LFSCK_TYPE_LAYOUT) {
2263 list = <d->ltd_layout_list;
2264 phase_list = <d->ltd_layout_phase_list;
2266 list = <d->ltd_namespace_list;
2267 phase_list = <d->ltd_namespace_phase_list;
2270 spin_lock(<ds->ltd_lock);
2271 if (list_empty(list)) {
2272 LASSERT(list_empty(phase_list));
2273 spin_unlock(<ds->ltd_lock);
2274 ptlrpc_set_destroy(set);
2279 list_del_init(phase_list);
2280 list_del_init(list);
2281 spin_unlock(<ds->ltd_lock);
2283 memset(lr, 0, sizeof(*lr));
2284 lr->lr_index = lfsck_dev_idx(lfsck);
2285 lr->lr_event = LE_PEER_EXIT;
2286 lr->lr_active = type;
2287 lr->lr_status = LS_CO_PAUSED;
2288 if (ltds == &lfsck->li_ost_descs)
2289 lr->lr_flags = LEF_TO_OST;
2291 laia->laia_com = com;
2292 laia->laia_ltds = ltds;
2293 atomic_inc(<d->ltd_ref);
2294 laia->laia_ltd = ltd;
2296 laia->laia_shared = 0;
2298 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2299 lfsck_async_interpret_common,
2300 laia, LFSCK_NOTIFY);
2302 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2303 "co-stop for %s: rc = %d\n",
2304 lfsck_lfsck2name(lfsck),
2305 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2306 ltd->ltd_index, lad->lad_name, rc);
2309 rc = ptlrpc_set_wait(set);
2312 ptlrpc_set_destroy(set);
2313 lfsck_component_put(env, com);
2319 static int lfsck_async_interpret(const struct lu_env *env,
2320 struct ptlrpc_request *req,
2323 struct lfsck_async_interpret_args *laia = args;
2324 struct lfsck_instance *lfsck;
2326 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2328 lfsck_interpret(env, lfsck, req, laia, rc);
2329 lfsck_tgt_put(laia->laia_ltd);
2330 if (rc != 0 && laia->laia_result != -EALREADY)
2331 laia->laia_result = rc;
2336 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2337 struct lfsck_request *lr,
2338 struct ptlrpc_request_set *set,
2339 ptlrpc_interpterer_t interpreter,
2340 void *args, int request)
2342 struct lfsck_async_interpret_args *laia;
2343 struct ptlrpc_request *req;
2344 struct lfsck_request *tmp;
2345 struct req_format *format;
2350 format = &RQF_LFSCK_NOTIFY;
2353 format = &RQF_LFSCK_QUERY;
2356 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2357 exp->exp_obd->obd_name, request, -EINVAL);
2361 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2365 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2367 ptlrpc_request_free(req);
2372 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2374 ptlrpc_request_set_replen(req);
2376 laia = ptlrpc_req_async_args(req);
2377 *laia = *(struct lfsck_async_interpret_args *)args;
2378 if (laia->laia_com != NULL)
2379 lfsck_component_get(laia->laia_com);
2380 req->rq_interpret_reply = interpreter;
2381 ptlrpc_set_add_req(set, req);
2386 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2387 struct lfsck_start_param *lsp)
2389 struct lfsck_instance *lfsck = com->lc_lfsck;
2390 struct lfsck_assistant_data *lad = com->lc_data;
2391 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2392 struct ptlrpc_thread *athread = &lad->lad_thread;
2393 struct lfsck_thread_args *lta;
2394 struct task_struct *task;
2398 lad->lad_assistant_status = 0;
2399 lad->lad_post_result = 0;
2400 lad->lad_to_post = 0;
2401 lad->lad_to_double_scan = 0;
2402 lad->lad_in_double_scan = 0;
2404 lad->lad_advance_lock = false;
2405 thread_set_flags(athread, 0);
2407 lta = lfsck_thread_args_init(lfsck, com, lsp);
2409 RETURN(PTR_ERR(lta));
2411 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2414 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2415 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2416 lfsck_thread_args_fini(lta);
2418 struct l_wait_info lwi = { 0 };
2420 l_wait_event(mthread->t_ctl_waitq,
2421 thread_is_running(athread) ||
2422 thread_is_stopped(athread),
2424 if (unlikely(!thread_is_running(athread)))
2425 rc = lad->lad_assistant_status;
2433 int lfsck_checkpoint_generic(const struct lu_env *env,
2434 struct lfsck_component *com)
2436 struct lfsck_assistant_data *lad = com->lc_data;
2437 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2438 struct ptlrpc_thread *athread = &lad->lad_thread;
2439 struct l_wait_info lwi = { 0 };
2441 l_wait_event(mthread->t_ctl_waitq,
2442 list_empty(&lad->lad_req_list) ||
2443 !thread_is_running(mthread) ||
2444 thread_is_stopped(athread),
2447 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2448 return LFSCK_CHECKPOINT_SKIP;
2453 void lfsck_post_generic(const struct lu_env *env,
2454 struct lfsck_component *com, int *result)
2456 struct lfsck_assistant_data *lad = com->lc_data;
2457 struct ptlrpc_thread *athread = &lad->lad_thread;
2458 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2459 struct l_wait_info lwi = { 0 };
2461 lad->lad_post_result = *result;
2464 lad->lad_to_post = 1;
2466 wake_up_all(&athread->t_ctl_waitq);
2467 l_wait_event(mthread->t_ctl_waitq,
2468 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2469 thread_is_stopped(athread),
2472 if (lad->lad_assistant_status < 0)
2473 *result = lad->lad_assistant_status;
2476 int lfsck_double_scan_generic(const struct lu_env *env,
2477 struct lfsck_component *com, int status)
2479 struct lfsck_assistant_data *lad = com->lc_data;
2480 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2481 struct ptlrpc_thread *athread = &lad->lad_thread;
2482 struct l_wait_info lwi = { 0 };
2484 if (status != LS_SCANNING_PHASE2)
2487 lad->lad_to_double_scan = 1;
2489 wake_up_all(&athread->t_ctl_waitq);
2490 l_wait_event(mthread->t_ctl_waitq,
2491 lad->lad_in_double_scan ||
2492 thread_is_stopped(athread),
2495 if (lad->lad_assistant_status < 0)
2496 return lad->lad_assistant_status;
2501 void lfsck_quit_generic(const struct lu_env *env,
2502 struct lfsck_component *com)
2504 struct lfsck_assistant_data *lad = com->lc_data;
2505 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2506 struct ptlrpc_thread *athread = &lad->lad_thread;
2507 struct l_wait_info lwi = { 0 };
2510 wake_up_all(&athread->t_ctl_waitq);
2511 l_wait_event(mthread->t_ctl_waitq,
2512 thread_is_init(athread) ||
2513 thread_is_stopped(athread),
2517 /* external interfaces */
2519 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2522 struct lfsck_instance *lfsck;
2526 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2530 lfsck = lfsck_instance_find(key, true, false);
2531 if (likely(lfsck != NULL)) {
2532 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2533 lfsck_instance_put(&env, lfsck);
2542 EXPORT_SYMBOL(lfsck_get_speed);
2544 int lfsck_set_speed(struct dt_device *key, int val)
2547 struct lfsck_instance *lfsck;
2551 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2555 lfsck = lfsck_instance_find(key, true, false);
2556 if (likely(lfsck != NULL)) {
2557 mutex_lock(&lfsck->li_mutex);
2558 if (__lfsck_set_speed(lfsck, val))
2559 rc = lfsck_bookmark_store(&env, lfsck);
2560 mutex_unlock(&lfsck->li_mutex);
2561 lfsck_instance_put(&env, lfsck);
2570 EXPORT_SYMBOL(lfsck_set_speed);
2572 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2575 struct lfsck_instance *lfsck;
2579 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2583 lfsck = lfsck_instance_find(key, true, false);
2584 if (likely(lfsck != NULL)) {
2585 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2586 lfsck_instance_put(&env, lfsck);
2595 EXPORT_SYMBOL(lfsck_get_windows);
2597 int lfsck_set_windows(struct dt_device *key, int val)
2600 struct lfsck_instance *lfsck;
2604 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2608 lfsck = lfsck_instance_find(key, true, false);
2609 if (likely(lfsck != NULL)) {
2610 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2611 CWARN("%s: invalid async windows size that may "
2612 "cause memory issues. The valid range is "
2614 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2616 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2617 mutex_lock(&lfsck->li_mutex);
2618 lfsck->li_bookmark_ram.lb_async_windows = val;
2619 rc = lfsck_bookmark_store(&env, lfsck);
2620 mutex_unlock(&lfsck->li_mutex);
2622 lfsck_instance_put(&env, lfsck);
2631 EXPORT_SYMBOL(lfsck_set_windows);
2633 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2636 struct lfsck_instance *lfsck;
2637 struct lfsck_component *com;
2641 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2645 lfsck = lfsck_instance_find(key, true, false);
2646 if (likely(lfsck != NULL)) {
2647 com = lfsck_component_find(lfsck, type);
2648 if (likely(com != NULL)) {
2649 rc = com->lc_ops->lfsck_dump(&env, com, m);
2650 lfsck_component_put(&env, com);
2655 lfsck_instance_put(&env, lfsck);
2664 EXPORT_SYMBOL(lfsck_dump);
2666 static int lfsck_stop_all(const struct lu_env *env,
2667 struct lfsck_instance *lfsck,
2668 struct lfsck_stop *stop)
2670 struct lfsck_thread_info *info = lfsck_env_info(env);
2671 struct lfsck_request *lr = &info->lti_lr;
2672 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2673 struct ptlrpc_request_set *set;
2674 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2675 struct lfsck_tgt_desc *ltd;
2676 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2682 LASSERT(stop->ls_flags & LPF_BROADCAST);
2684 set = ptlrpc_prep_set();
2685 if (unlikely(set == NULL))
2688 memset(lr, 0, sizeof(*lr));
2689 lr->lr_event = LE_STOP;
2690 lr->lr_index = lfsck_dev_idx(lfsck);
2691 lr->lr_status = stop->ls_status;
2692 lr->lr_version = bk->lb_version;
2693 lr->lr_active = LFSCK_TYPES_ALL;
2694 lr->lr_param = stop->ls_flags;
2696 laia->laia_com = NULL;
2697 laia->laia_ltds = ltds;
2699 laia->laia_result = 0;
2700 laia->laia_shared = 1;
2702 down_read(<ds->ltd_rw_sem);
2703 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2704 ltd = lfsck_tgt_get(ltds, idx);
2705 LASSERT(ltd != NULL);
2707 laia->laia_ltd = ltd;
2708 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2709 lfsck_async_interpret, laia,
2712 lfsck_interpret(env, lfsck, NULL, laia, rc);
2714 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2715 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2719 up_read(<ds->ltd_rw_sem);
2721 rc = ptlrpc_set_wait(set);
2722 ptlrpc_set_destroy(set);
2725 rc = laia->laia_result;
2727 if (rc == -EALREADY)
2731 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2732 lfsck_lfsck2name(lfsck), rc);
2734 RETURN(rc != 0 ? rc : rc1);
2737 static int lfsck_start_all(const struct lu_env *env,
2738 struct lfsck_instance *lfsck,
2739 struct lfsck_start *start)
2741 struct lfsck_thread_info *info = lfsck_env_info(env);
2742 struct lfsck_request *lr = &info->lti_lr;
2743 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2744 struct ptlrpc_request_set *set;
2745 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2746 struct lfsck_tgt_desc *ltd;
2747 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2752 LASSERT(start->ls_flags & LPF_BROADCAST);
2754 set = ptlrpc_prep_set();
2755 if (unlikely(set == NULL))
2758 memset(lr, 0, sizeof(*lr));
2759 lr->lr_event = LE_START;
2760 lr->lr_index = lfsck_dev_idx(lfsck);
2761 lr->lr_speed = bk->lb_speed_limit;
2762 lr->lr_version = bk->lb_version;
2763 lr->lr_active = start->ls_active;
2764 lr->lr_param = start->ls_flags;
2765 lr->lr_async_windows = bk->lb_async_windows;
2766 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2767 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2770 laia->laia_com = NULL;
2771 laia->laia_ltds = ltds;
2773 laia->laia_result = 0;
2774 laia->laia_shared = 1;
2776 down_read(<ds->ltd_rw_sem);
2777 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2778 ltd = lfsck_tgt_get(ltds, idx);
2779 LASSERT(ltd != NULL);
2781 laia->laia_ltd = ltd;
2782 ltd->ltd_layout_done = 0;
2783 ltd->ltd_namespace_done = 0;
2784 ltd->ltd_synced_failures = 0;
2785 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2786 lfsck_async_interpret, laia,
2789 lfsck_interpret(env, lfsck, NULL, laia, rc);
2791 CERROR("%s: cannot notify MDT %x for LFSCK "
2792 "start, failout: rc = %d\n",
2793 lfsck_lfsck2name(lfsck), idx, rc);
2797 up_read(<ds->ltd_rw_sem);
2800 ptlrpc_set_destroy(set);
2805 rc = ptlrpc_set_wait(set);
2806 ptlrpc_set_destroy(set);
2809 rc = laia->laia_result;
2812 struct lfsck_stop *stop = &info->lti_stop;
2814 CERROR("%s: cannot start LFSCK on some MDTs, "
2815 "stop all: rc = %d\n",
2816 lfsck_lfsck2name(lfsck), rc);
2817 if (rc != -EALREADY) {
2818 stop->ls_status = LS_FAILED;
2819 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2820 lfsck_stop_all(env, lfsck, stop);
2827 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2828 struct lfsck_start_param *lsp)
2830 struct lfsck_start *start = lsp->lsp_start;
2831 struct lfsck_instance *lfsck;
2832 struct lfsck_bookmark *bk;
2833 struct ptlrpc_thread *thread;
2834 struct lfsck_component *com;
2835 struct l_wait_info lwi = { 0 };
2836 struct lfsck_thread_args *lta;
2837 struct task_struct *task;
2844 lfsck = lfsck_instance_find(key, true, false);
2845 if (unlikely(lfsck == NULL))
2848 /* System is not ready, try again later. */
2849 if (unlikely(lfsck->li_namespace == NULL))
2850 GOTO(put, rc = -EAGAIN);
2852 /* start == NULL means auto trigger paused LFSCK. */
2853 if ((start == NULL) &&
2854 (list_empty(&lfsck->li_list_scan) ||
2855 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2858 bk = &lfsck->li_bookmark_ram;
2859 thread = &lfsck->li_thread;
2860 mutex_lock(&lfsck->li_mutex);
2861 spin_lock(&lfsck->li_lock);
2862 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2864 if (unlikely(start == NULL)) {
2865 spin_unlock(&lfsck->li_lock);
2869 while (start->ls_active != 0) {
2870 if (!(type & start->ls_active)) {
2875 com = __lfsck_component_find(lfsck, type,
2876 &lfsck->li_list_scan);
2878 com = __lfsck_component_find(lfsck, type,
2879 &lfsck->li_list_double_scan);
2885 if (com->lc_ops->lfsck_join != NULL) {
2886 rc = com->lc_ops->lfsck_join( env, com, lsp);
2887 if (rc != 0 && rc != -EALREADY)
2890 start->ls_active &= ~type;
2893 spin_unlock(&lfsck->li_lock);
2896 spin_unlock(&lfsck->li_lock);
2898 lfsck->li_status = 0;
2899 lfsck->li_oit_over = 0;
2900 lfsck->li_start_unplug = 0;
2901 lfsck->li_drop_dryrun = 0;
2902 lfsck->li_new_scanned = 0;
2904 /* For auto trigger. */
2908 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2909 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2910 lfsck_lfsck2name(lfsck));
2912 GOTO(out, rc = -EPERM);
2915 start->ls_version = bk->lb_version;
2917 if (start->ls_active != 0) {
2918 struct lfsck_component *next;
2920 if (start->ls_active == LFSCK_TYPES_ALL)
2921 start->ls_active = LFSCK_TYPES_SUPPORTED;
2923 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2924 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2925 GOTO(out, rc = -ENOTSUPP);
2928 list_for_each_entry_safe(com, next,
2929 &lfsck->li_list_scan, lc_link) {
2930 if (!(com->lc_type & start->ls_active)) {
2931 rc = com->lc_ops->lfsck_post(env, com, 0,
2938 while (start->ls_active != 0) {
2939 if (type & start->ls_active) {
2940 com = __lfsck_component_find(lfsck, type,
2941 &lfsck->li_list_idle);
2943 /* The component status will be updated
2944 * when its prep() is called later by
2945 * the LFSCK main engine. */
2946 list_move_tail(&com->lc_link,
2947 &lfsck->li_list_scan);
2948 start->ls_active &= ~type;
2954 if (list_empty(&lfsck->li_list_scan)) {
2955 /* The speed limit will be used to control both the LFSCK and
2956 * low layer scrub (if applied), need to be handled firstly. */
2957 if (start->ls_valid & LSV_SPEED_LIMIT) {
2958 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2959 rc = lfsck_bookmark_store(env, lfsck);
2968 if (start->ls_flags & LPF_RESET)
2969 flags |= DOIF_RESET;
2971 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2975 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2976 start->ls_active |= com->lc_type;
2977 if (flags & DOIF_RESET) {
2978 rc = com->lc_ops->lfsck_reset(env, com, false);
2985 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2986 if (bk->lb_param & LPF_DRYRUN)
2987 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2989 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2990 valid |= DOIV_ERROR_HANDLE;
2991 if (start->ls_flags & LPF_FAILOUT)
2992 flags |= DOIF_FAILOUT;
2995 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2996 valid |= DOIV_DRYRUN;
2997 if (start->ls_flags & LPF_DRYRUN)
2998 flags |= DOIF_DRYRUN;
3001 if (!list_empty(&lfsck->li_list_scan))
3002 flags |= DOIF_OUTUSED;
3004 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3005 thread_set_flags(thread, 0);
3006 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3008 GOTO(out, rc = PTR_ERR(lta));
3010 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3011 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3014 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3015 lfsck_lfsck2name(lfsck), rc);
3016 lfsck_thread_args_fini(lta);
3021 l_wait_event(thread->t_ctl_waitq,
3022 thread_is_running(thread) ||
3023 thread_is_stopped(thread),
3025 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3026 lfsck->li_start_unplug = 1;
3027 wake_up_all(&thread->t_ctl_waitq);
3032 /* release lfsck::li_mutex to avoid deadlock. */
3033 mutex_unlock(&lfsck->li_mutex);
3034 rc = lfsck_start_all(env, lfsck, start);
3036 spin_lock(&lfsck->li_lock);
3037 if (thread_is_stopped(thread)) {
3038 spin_unlock(&lfsck->li_lock);
3040 lfsck->li_status = LS_FAILED;
3041 lfsck->li_flags = 0;
3042 thread_set_flags(thread, SVC_STOPPING);
3043 spin_unlock(&lfsck->li_lock);
3045 lfsck->li_start_unplug = 1;
3046 wake_up_all(&thread->t_ctl_waitq);
3047 l_wait_event(thread->t_ctl_waitq,
3048 thread_is_stopped(thread),
3052 lfsck->li_start_unplug = 1;
3053 wake_up_all(&thread->t_ctl_waitq);
3059 mutex_unlock(&lfsck->li_mutex);
3062 lfsck_instance_put(env, lfsck);
3064 return rc < 0 ? rc : 0;
3066 EXPORT_SYMBOL(lfsck_start);
3068 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3069 struct lfsck_stop *stop)
3071 struct lfsck_instance *lfsck;
3072 struct ptlrpc_thread *thread;
3073 struct l_wait_info lwi = { 0 };
3078 lfsck = lfsck_instance_find(key, true, false);
3079 if (unlikely(lfsck == NULL))
3082 thread = &lfsck->li_thread;
3083 /* release lfsck::li_mutex to avoid deadlock. */
3084 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3085 if (!lfsck->li_master) {
3086 CERROR("%s: only allow to specify '-A' via MDS\n",
3087 lfsck_lfsck2name(lfsck));
3089 GOTO(out, rc = -EPERM);
3092 rc1 = lfsck_stop_all(env, lfsck, stop);
3095 mutex_lock(&lfsck->li_mutex);
3096 spin_lock(&lfsck->li_lock);
3097 /* no error if LFSCK is already stopped, or was never started */
3098 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3099 spin_unlock(&lfsck->li_lock);
3104 lfsck->li_status = stop->ls_status;
3105 lfsck->li_flags = stop->ls_flags;
3107 lfsck->li_status = LS_STOPPED;
3108 lfsck->li_flags = 0;
3111 thread_set_flags(thread, SVC_STOPPING);
3112 spin_unlock(&lfsck->li_lock);
3114 wake_up_all(&thread->t_ctl_waitq);
3115 l_wait_event(thread->t_ctl_waitq,
3116 thread_is_stopped(thread),
3122 mutex_unlock(&lfsck->li_mutex);
3123 lfsck_instance_put(env, lfsck);
3125 return rc != 0 ? rc : rc1;
3127 EXPORT_SYMBOL(lfsck_stop);
3129 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3130 struct lfsck_request *lr, struct thandle *th)
3132 int rc = -EOPNOTSUPP;
3135 switch (lr->lr_event) {
3137 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3138 struct lfsck_start_param lsp;
3140 memset(start, 0, sizeof(*start));
3141 start->ls_valid = lr->lr_valid;
3142 start->ls_speed_limit = lr->lr_speed;
3143 start->ls_version = lr->lr_version;
3144 start->ls_active = lr->lr_active;
3145 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3146 start->ls_async_windows = lr->lr_async_windows;
3148 lsp.lsp_start = start;
3149 lsp.lsp_index = lr->lr_index;
3150 lsp.lsp_index_valid = 1;
3151 rc = lfsck_start(env, key, &lsp);
3155 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3157 memset(stop, 0, sizeof(*stop));
3158 stop->ls_status = lr->lr_status;
3159 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3160 rc = lfsck_stop(env, key, stop);
3163 case LE_PHASE1_DONE:
3164 case LE_PHASE2_DONE:
3165 case LE_FID_ACCESSED:
3167 case LE_CONDITIONAL_DESTROY:
3168 case LE_SKIP_NLINK_DECLARE:
3170 case LE_SET_LMV_MASTER:
3171 case LE_SET_LMV_SLAVE:
3172 case LE_PAIRS_VERIFY: {
3173 struct lfsck_instance *lfsck;
3174 struct lfsck_component *com;
3176 lfsck = lfsck_instance_find(key, true, false);
3177 if (unlikely(lfsck == NULL))
3180 com = lfsck_component_find(lfsck, lr->lr_active);
3181 if (likely(com != NULL)) {
3182 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3183 lfsck_component_put(env, com);
3186 lfsck_instance_put(env, lfsck);
3195 EXPORT_SYMBOL(lfsck_in_notify);
3197 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3198 struct lfsck_request *lr)
3200 struct lfsck_instance *lfsck;
3201 struct lfsck_component *com;
3205 lfsck = lfsck_instance_find(key, true, false);
3206 if (unlikely(lfsck == NULL))
3209 com = lfsck_component_find(lfsck, lr->lr_active);
3210 if (likely(com != NULL)) {
3211 rc = com->lc_ops->lfsck_query(env, com);
3212 lfsck_component_put(env, com);
3217 lfsck_instance_put(env, lfsck);
3222 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3223 struct ldlm_namespace *ns)
3225 struct lfsck_instance *lfsck;
3228 lfsck = lfsck_instance_find(key, true, false);
3229 if (likely(lfsck != NULL)) {
3230 lfsck->li_namespace = ns;
3231 lfsck_instance_put(env, lfsck);
3237 EXPORT_SYMBOL(lfsck_register_namespace);
3239 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3240 struct dt_device *next, struct obd_device *obd,
3241 lfsck_out_notify notify, void *notify_data, bool master)
3243 struct lfsck_instance *lfsck;
3244 struct dt_object *root = NULL;
3245 struct dt_object *obj = NULL;
3246 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3250 lfsck = lfsck_instance_find(key, false, false);
3251 if (unlikely(lfsck != NULL))
3254 OBD_ALLOC_PTR(lfsck);
3258 mutex_init(&lfsck->li_mutex);
3259 spin_lock_init(&lfsck->li_lock);
3260 INIT_LIST_HEAD(&lfsck->li_link);
3261 INIT_LIST_HEAD(&lfsck->li_list_scan);
3262 INIT_LIST_HEAD(&lfsck->li_list_dir);
3263 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3264 INIT_LIST_HEAD(&lfsck->li_list_idle);
3265 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3266 atomic_set(&lfsck->li_ref, 1);
3267 atomic_set(&lfsck->li_double_scan_count, 0);
3268 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3269 lfsck->li_out_notify = notify;
3270 lfsck->li_out_notify_data = notify_data;
3271 lfsck->li_next = next;
3272 lfsck->li_bottom = key;
3273 lfsck->li_obd = obd;
3275 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3279 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3283 fid->f_seq = FID_SEQ_LOCAL_NAME;
3286 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3290 rc = dt_root_get(env, key, fid);
3294 root = dt_locate(env, key, fid);
3296 GOTO(out, rc = PTR_ERR(root));
3298 if (unlikely(!dt_try_as_dir(env, root)))
3299 GOTO(out, rc = -ENOTDIR);
3301 lfsck->li_local_root_fid = *fid;
3303 lfsck->li_master = 1;
3304 if (lfsck_dev_idx(lfsck) == 0) {
3305 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3306 const struct lu_name *cname;
3308 rc = dt_lookup(env, root,
3309 (struct dt_rec *)(&lfsck->li_global_root_fid),
3310 (const struct dt_key *)"ROOT");
3314 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3316 GOTO(out, rc = PTR_ERR(obj));
3318 if (unlikely(!dt_try_as_dir(env, obj)))
3319 GOTO(out, rc = -ENOTDIR);
3321 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3322 (const struct dt_key *)dotlustre);
3326 lfsck_object_put(env, obj);
3327 obj = dt_locate(env, key, fid);
3329 GOTO(out, rc = PTR_ERR(obj));
3331 cname = lfsck_name_get_const(env, dotlustre,
3333 rc = lfsck_verify_linkea(env, obj, cname,
3334 &lfsck->li_global_root_fid);
3338 if (unlikely(!dt_try_as_dir(env, obj)))
3339 GOTO(out, rc = -ENOTDIR);
3342 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3343 (const struct dt_key *)lostfound);
3347 lfsck_object_put(env, obj);
3348 obj = dt_locate(env, key, fid);
3350 GOTO(out, rc = PTR_ERR(obj));
3352 cname = lfsck_name_get_const(env, lostfound,
3354 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3358 lfsck_object_put(env, obj);
3363 fid->f_seq = FID_SEQ_LOCAL_FILE;
3364 fid->f_oid = OTABLE_IT_OID;
3366 obj = dt_locate(env, key, fid);
3368 GOTO(out, rc = PTR_ERR(obj));
3370 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3374 lfsck->li_obj_oit = obj;
3375 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3376 S_IFDIR | S_IRUGO | S_IWUSR);
3378 GOTO(out, rc = PTR_ERR(obj));
3380 lu_object_get(&obj->do_lu);
3381 lfsck->li_lfsck_dir = obj;
3382 rc = lfsck_bookmark_setup(env, lfsck);
3387 rc = lfsck_fid_init(lfsck);
3391 rc = lfsck_namespace_setup(env, lfsck);
3396 rc = lfsck_layout_setup(env, lfsck);
3400 /* XXX: more LFSCK components initialization to be added here. */
3402 rc = lfsck_instance_add(lfsck);
3404 rc = lfsck_add_target_from_orphan(env, lfsck);
3406 if (obj != NULL && !IS_ERR(obj))
3407 lfsck_object_put(env, obj);
3408 if (root != NULL && !IS_ERR(root))
3409 lfsck_object_put(env, root);
3411 lfsck_instance_cleanup(env, lfsck);
3414 EXPORT_SYMBOL(lfsck_register);
3416 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3418 struct lfsck_instance *lfsck;
3420 lfsck = lfsck_instance_find(key, false, true);
3422 lfsck_instance_put(env, lfsck);
3424 EXPORT_SYMBOL(lfsck_degister);
3426 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3427 struct dt_device *tgt, struct obd_export *exp,
3428 __u32 index, bool for_ost)
3430 struct lfsck_instance *lfsck;
3431 struct lfsck_tgt_desc *ltd;
3442 INIT_LIST_HEAD(<d->ltd_orphan_list);
3443 INIT_LIST_HEAD(<d->ltd_layout_list);
3444 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3445 INIT_LIST_HEAD(<d->ltd_namespace_list);
3446 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3447 atomic_set(<d->ltd_ref, 1);
3448 ltd->ltd_index = index;
3450 spin_lock(&lfsck_instance_lock);
3451 lfsck = __lfsck_instance_find(key, true, false);
3452 if (lfsck == NULL) {
3454 list_add_tail(<d->ltd_orphan_list,
3455 &lfsck_ost_orphan_list);
3457 list_add_tail(<d->ltd_orphan_list,
3458 &lfsck_mdt_orphan_list);
3459 spin_unlock(&lfsck_instance_lock);
3463 spin_unlock(&lfsck_instance_lock);
3465 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3469 lfsck_instance_put(env, lfsck);
3473 EXPORT_SYMBOL(lfsck_add_target);
3475 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3476 struct dt_device *tgt, __u32 index, bool for_ost)
3478 struct lfsck_instance *lfsck;
3479 struct lfsck_tgt_descs *ltds;
3480 struct lfsck_tgt_desc *ltd;
3481 struct list_head *head;
3484 head = &lfsck_ost_orphan_list;
3486 head = &lfsck_mdt_orphan_list;
3488 spin_lock(&lfsck_instance_lock);
3489 list_for_each_entry(ltd, head, ltd_orphan_list) {
3490 if (ltd->ltd_tgt == tgt) {
3491 list_del_init(<d->ltd_orphan_list);
3492 spin_unlock(&lfsck_instance_lock);
3500 lfsck = __lfsck_instance_find(key, true, false);
3501 spin_unlock(&lfsck_instance_lock);
3502 if (unlikely(lfsck == NULL))
3506 ltds = &lfsck->li_ost_descs;
3508 ltds = &lfsck->li_mdt_descs;
3510 down_write(<ds->ltd_rw_sem);
3511 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3513 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3516 ltd = lfsck_ltd2tgt(ltds, index);
3517 if (unlikely(ltd == NULL))
3520 LASSERT(ltds->ltd_tgtnr > 0);
3523 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3524 lfsck_assign_tgt(ltds, NULL, index);
3529 head = &lfsck->li_ost_descs.ltd_orphan;
3531 head = &lfsck->li_mdt_descs.ltd_orphan;
3533 list_for_each_entry(ltd, head, ltd_orphan_list) {
3534 if (ltd->ltd_tgt == tgt) {
3535 list_del_init(<d->ltd_orphan_list);
3541 up_write(<ds->ltd_rw_sem);
3543 spin_lock(<ds->ltd_lock);
3545 spin_unlock(<ds->ltd_lock);
3546 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3547 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3551 lfsck_instance_put(env, lfsck);
3553 EXPORT_SYMBOL(lfsck_del_target);
3555 static int __init lfsck_init(void)
3559 INIT_LIST_HEAD(&lfsck_instance_list);
3560 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3561 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3562 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3563 rc = lu_context_key_register(&lfsck_thread_key);
3565 tgt_register_lfsck_in_notify(lfsck_in_notify);
3566 tgt_register_lfsck_query(lfsck_query);
3572 static void __exit lfsck_exit(void)
3574 struct lfsck_tgt_desc *ltd;
3575 struct lfsck_tgt_desc *next;
3577 LASSERT(list_empty(&lfsck_instance_list));
3579 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3581 list_del_init(<d->ltd_orphan_list);
3585 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3587 list_del_init(<d->ltd_orphan_list);
3591 lu_context_key_degister(&lfsck_thread_key);
3594 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3595 MODULE_DESCRIPTION("LFSCK");
3596 MODULE_VERSION(LUSTRE_VERSION_STRING);
3597 MODULE_LICENSE("GPL");
3599 module_init(lfsck_init);
3600 module_exit(lfsck_exit);