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 if (!dt_try_as_dir(env, child))
667 GOTO(stop, rc = -ENOTDIR);
669 /* 2a. increase child nlink */
670 rc = dt_declare_ref_add(env, child, th);
674 /* 3a. insert dot into child dir */
675 rec->rec_type = S_IFDIR;
677 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
678 (const struct dt_key *)dot, th);
682 /* 4a. insert dotdot into child dir */
683 rec->rec_fid = &LU_LPF_FID;
684 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
685 (const struct dt_key *)dotdot, th);
689 /* 5a. insert linkEA for child */
690 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
691 ldata.ld_leh->leh_len);
692 rc = dt_declare_xattr_set(env, child, &linkea_buf,
693 XATTR_NAME_LINK, 0, th);
697 /* 6a. insert name into parent dir */
698 rec->rec_type = S_IFDIR;
700 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
701 (const struct dt_key *)name, th);
705 /* 7a. increase parent nlink */
706 rc = dt_declare_ref_add(env, parent, th);
710 /* 8a. update bookmark */
711 rc = dt_declare_record_write(env, bk_obj,
712 lfsck_buf_get(env, bk, len), 0, th);
716 rc = dt_trans_start_local(env, dev, th);
720 dt_write_lock(env, child, 0);
721 /* 1b. create child */
722 rc = dt_create(env, child, la, NULL, dof, th);
726 /* 2b. increase child nlink */
727 rc = dt_ref_add(env, child, th);
731 /* 3b. insert dot into child dir */
733 rc = dt_insert(env, child, (const struct dt_rec *)rec,
734 (const struct dt_key *)dot, th, 1);
738 /* 4b. insert dotdot into child dir */
739 rec->rec_fid = &LU_LPF_FID;
740 rc = dt_insert(env, child, (const struct dt_rec *)rec,
741 (const struct dt_key *)dotdot, th, 1);
745 /* 5b. insert linkEA for child. */
746 rc = dt_xattr_set(env, child, &linkea_buf,
747 XATTR_NAME_LINK, 0, th);
748 dt_write_unlock(env, child);
752 /* 6b. insert name into parent dir */
754 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
755 (const struct dt_key *)name, th, 1);
759 dt_write_lock(env, parent, 0);
760 /* 7b. increase parent nlink */
761 rc = dt_ref_add(env, parent, th);
762 dt_write_unlock(env, parent);
766 bk->lb_lpf_fid = *cfid;
767 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
769 /* 8b. update bookmark */
770 rc = dt_record_write(env, bk_obj,
771 lfsck_buf_get(env, bk, len), &pos, th);
776 dt_write_unlock(env, child);
779 dt_trans_stop(env, dev, th);
784 static int lfsck_create_lpf_remote(const struct lu_env *env,
785 struct lfsck_instance *lfsck,
786 struct dt_object *child,
788 struct dt_object_format *dof,
791 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
792 struct dt_object *parent = lfsck->li_lpf_root_obj;
793 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
794 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
795 const struct lu_fid *cfid = lfsck_dto2fid(child);
796 struct thandle *th = NULL;
797 struct linkea_data ldata = { NULL };
798 struct lu_buf linkea_buf;
799 const struct lu_name *cname;
800 struct dt_device *dev;
802 int len = sizeof(struct lfsck_bookmark);
806 rc = linkea_data_new(&ldata,
807 &lfsck_env_info(env)->lti_linkea_buf2);
811 cname = lfsck_name_get_const(env, name, strlen(name));
812 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
816 /* Create .lustre/lost+found/MDTxxxx. */
818 /* XXX: Currently, cross-MDT create operation needs to create the child
819 * object firstly, then insert name into the parent directory. For
820 * this case, the child object resides on current MDT (local), but
821 * the parent ".lustre/lost+found" may be on remote MDT. It is not
822 * easy to contain all the sub-modifications orderly within single
825 * To avoid more inconsistency, we split the create operation into
828 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
830 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
833 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
834 * repair such inconsistency when LFSCK run next time. */
836 /* Transaction I: locally */
838 dev = lfsck_obj2dev(child);
839 th = dt_trans_create(env, dev);
843 /* 1a. create child */
844 rc = dt_declare_create(env, child, la, NULL, dof, th);
848 if (!dt_try_as_dir(env, child))
849 GOTO(stop, rc = -ENOTDIR);
851 /* 2a. increase child nlink */
852 rc = dt_declare_ref_add(env, child, th);
856 /* 3a. insert dot into child dir */
857 rec->rec_type = S_IFDIR;
859 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
860 (const struct dt_key *)dot, th);
864 /* 4a. insert dotdot into child dir */
865 rec->rec_fid = &LU_LPF_FID;
866 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
867 (const struct dt_key *)dotdot, th);
871 /* 5a. insert linkEA for child */
872 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
873 ldata.ld_leh->leh_len);
874 rc = dt_declare_xattr_set(env, child, &linkea_buf,
875 XATTR_NAME_LINK, 0, th);
879 /* 6a. update bookmark */
880 rc = dt_declare_record_write(env, bk_obj,
881 lfsck_buf_get(env, bk, len), 0, th);
885 rc = dt_trans_start_local(env, dev, th);
889 dt_write_lock(env, child, 0);
890 /* 1b. create child */
891 rc = dt_create(env, child, la, NULL, dof, th);
895 /* 2b. increase child nlink */
896 rc = dt_ref_add(env, child, th);
900 /* 3b. insert dot into child dir */
901 rec->rec_type = S_IFDIR;
903 rc = dt_insert(env, child, (const struct dt_rec *)rec,
904 (const struct dt_key *)dot, th, 1);
908 /* 4b. insert dotdot into child dir */
909 rec->rec_fid = &LU_LPF_FID;
910 rc = dt_insert(env, child, (const struct dt_rec *)rec,
911 (const struct dt_key *)dotdot, th, 1);
915 /* 5b. insert linkEA for child */
916 rc = dt_xattr_set(env, child, &linkea_buf,
917 XATTR_NAME_LINK, 0, th);
921 bk->lb_lpf_fid = *cfid;
922 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
924 /* 6b. update bookmark */
925 rc = dt_record_write(env, bk_obj,
926 lfsck_buf_get(env, bk, len), &pos, th);
928 dt_write_unlock(env, child);
929 dt_trans_stop(env, dev, th);
933 /* Transaction II: remotely */
935 dev = lfsck_obj2dev(parent);
936 th = dt_trans_create(env, dev);
941 /* 5a. insert name into parent dir */
943 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
944 (const struct dt_key *)name, th);
948 /* 6a. increase parent nlink */
949 rc = dt_declare_ref_add(env, parent, th);
953 rc = dt_trans_start_local(env, dev, th);
957 /* 5b. insert name into parent dir */
958 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
959 (const struct dt_key *)name, th, 1);
963 dt_write_lock(env, parent, 0);
964 /* 6b. increase parent nlink */
965 rc = dt_ref_add(env, parent, th);
966 dt_write_unlock(env, parent);
971 dt_write_unlock(env, child);
973 dt_trans_stop(env, dev, th);
975 if (rc != 0 && dev == lfsck_obj2dev(parent))
976 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
977 "for orphans, but failed to insert the name %s "
978 "to the .lustre/lost+found/. Such inconsistency "
979 "will be repaired when LFSCK run next time: rc = %d\n",
980 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
986 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
988 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
989 * orphans and other uncertain inconsistent objects found during the
990 * LFSCK. Such directory will be created by the LFSCK engine on the
991 * local MDT before the LFSCK scanning.
993 * \param[in] env pointer to the thread context
994 * \param[in] lfsck pointer to the lfsck instance
996 * \retval 0 for success
997 * \retval negative error number on failure
999 static int lfsck_create_lpf(const struct lu_env *env,
1000 struct lfsck_instance *lfsck)
1002 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1003 struct lfsck_thread_info *info = lfsck_env_info(env);
1004 struct lu_fid *cfid = &info->lti_fid2;
1005 struct lu_attr *la = &info->lti_la;
1006 struct dt_object_format *dof = &info->lti_dof;
1007 struct dt_object *parent = lfsck->li_lpf_root_obj;
1008 struct dt_object *child = NULL;
1009 struct lfsck_lock_handle *llh = &info->lti_llh;
1011 int node = lfsck_dev_idx(lfsck);
1015 LASSERT(lfsck->li_master);
1016 LASSERT(parent != NULL);
1017 LASSERT(lfsck->li_lpf_obj == NULL);
1019 snprintf(name, 8, "MDT%04x", node);
1020 rc = lfsck_lock(env, lfsck, parent, name, llh,
1021 MDS_INODELOCK_UPDATE, LCK_PW);
1025 if (fid_is_zero(&bk->lb_lpf_fid)) {
1026 /* There is corner case that: in former LFSCK scanning we have
1027 * created the .lustre/lost+found/MDTxxxx but failed to update
1028 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1029 * it from MDT0 firstly. */
1030 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1031 (const struct dt_key *)name);
1032 if (rc != 0 && rc != -ENOENT)
1036 bk->lb_lpf_fid = *cfid;
1037 rc = lfsck_bookmark_store(env, lfsck);
1039 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1044 *cfid = bk->lb_lpf_fid;
1047 child = lfsck_object_find_bottom(env, lfsck, cfid);
1049 GOTO(unlock, rc = PTR_ERR(child));
1051 if (dt_object_exists(child) != 0) {
1052 if (unlikely(!dt_try_as_dir(env, child)))
1055 lfsck->li_lpf_obj = child;
1060 memset(la, 0, sizeof(*la));
1061 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1062 la->la_mode = S_IFDIR | S_IRWXU;
1063 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1065 memset(dof, 0, sizeof(*dof));
1066 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1069 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1071 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1073 lfsck->li_lpf_obj = child;
1079 if (rc != 0 && child != NULL && !IS_ERR(child))
1080 lfsck_object_put(env, child);
1086 * Scan .lustre/lost+found for bad name entries and remove them.
1088 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1089 * index in the system. Any other formatted name is invalid and should be
1092 * \param[in] env pointer to the thread context
1093 * \param[in] lfsck pointer to the lfsck instance
1095 * \retval 0 for success
1096 * \retval negative error number on failure
1098 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1099 struct lfsck_instance *lfsck)
1101 struct dt_object *parent = lfsck->li_lpf_root_obj;
1102 struct lu_dirent *ent =
1103 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1104 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1109 it = iops->init(env, parent, LUDA_64BITHASH);
1111 RETURN(PTR_ERR(it));
1113 rc = iops->load(env, it, 0);
1115 rc = iops->next(env, it);
1122 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1126 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1127 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1130 /* name length must be strlen("MDTxxxx") */
1131 if (ent->lde_namelen != 7)
1134 if (memcmp(ent->lde_name, "MDT", off) != 0)
1137 while (off < 7 && isxdigit(ent->lde_name[off]))
1143 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1150 rc = iops->next(env, it);
1154 iops->fini(env, it);
1156 RETURN(rc > 0 ? 0 : rc);
1159 static int lfsck_update_lpf_entry(const struct lu_env *env,
1160 struct lfsck_instance *lfsck,
1161 struct dt_object *parent,
1162 struct dt_object *child,
1164 enum lfsck_verify_lpf_types type)
1168 if (type == LVLT_BY_BOOKMARK) {
1169 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1170 lfsck_dto2fid(child), S_IFDIR);
1171 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1172 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1173 rc = lfsck_bookmark_store(env, lfsck);
1175 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1176 " in the bookmark file: rc = %d\n",
1177 lfsck_lfsck2name(lfsck),
1178 PFID(lfsck_dto2fid(child)), rc);
1185 * Check whether the @child back references the @parent.
1188 * 1) The child's FID is stored in the bookmark file. If the child back
1189 * references the parent (LU_LPF_FID object) via its ".." entry, then
1190 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1191 * the child back references another parent2, then:
1192 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1193 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1194 * references the child. So keep them there. As the LFSCK processing,
1195 * the parent3 may be found, then when the LFSCK run next time, the
1196 * inconsistency can be repaired.
1198 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1199 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1200 * via its ".." entry, then update the bookmark file, otherwise, if the child
1201 * back references another parent2, then:
1202 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1203 * from .lustre/lost+found/;
1204 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1205 * sub-directory name entry and update the child;
1206 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1207 * or not, then keep them there.
1209 * \param[in] env pointer to the thread context
1210 * \param[in] lfsck pointer to the lfsck instance
1211 * \param[in] child pointer to the lost+found sub-directory object
1212 * \param[in] name the name for lost+found sub-directory object
1213 * \param[out] fid pointer to the buffer to hold the FID of the object
1214 * (called it as parent2) that is referenced via the
1215 * child's dotdot entry; it also can be the FID that
1216 * is referenced by the name entry under the parent2.
1217 * \param[in] type to indicate where the child's FID is stored in
1219 * \retval positive number for uncertain inconsistency
1220 * \retval 0 for success
1221 * \retval negative error number on failure
1223 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1224 struct lfsck_instance *lfsck,
1225 struct dt_object *child, const char *name,
1227 enum lfsck_verify_lpf_types type)
1229 struct dt_object *parent = lfsck->li_lpf_root_obj;
1230 struct lfsck_thread_info *info = lfsck_env_info(env);
1231 char *name2 = info->lti_key;
1232 struct lu_fid *fid2 = &info->lti_fid3;
1233 struct dt_object *parent2 = NULL;
1234 struct lustre_handle lh = { 0 };
1239 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1240 (const struct dt_key *)dotdot);
1244 if (!fid_is_sane(fid))
1245 GOTO(linkea, rc = -EINVAL);
1247 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1248 const struct lu_name *cname;
1250 if (lfsck->li_lpf_obj == NULL) {
1251 lu_object_get(&child->do_lu);
1252 lfsck->li_lpf_obj = child;
1255 cname = lfsck_name_get_const(env, name, strlen(name));
1256 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1258 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1264 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1265 if (IS_ERR(parent2))
1266 GOTO(linkea, parent2);
1268 if (!dt_object_exists(parent2)) {
1269 lfsck_object_put(env, parent2);
1271 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1274 if (!dt_try_as_dir(env, parent2)) {
1275 lfsck_object_put(env, parent2);
1277 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1281 /* To prevent rename/unlink race */
1282 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1283 MDS_INODELOCK_UPDATE, LCK_PR);
1287 dt_read_lock(env, child, 0);
1288 rc = lfsck_links_get_first(env, child, name2, fid2);
1290 dt_read_unlock(env, child);
1291 lfsck_ibits_unlock(&lh, LCK_PR);
1293 GOTO(out_put, rc = 1);
1296 /* It is almost impossible that the bookmark file (or the name entry)
1297 * and the linkEA hit the same data corruption. Trust the linkEA. */
1298 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1299 dt_read_unlock(env, child);
1300 lfsck_ibits_unlock(&lh, LCK_PR);
1303 if (lfsck->li_lpf_obj == NULL) {
1304 lu_object_get(&child->do_lu);
1305 lfsck->li_lpf_obj = child;
1308 /* Update the child's dotdot entry */
1309 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1310 &LU_LPF_FID, S_IFDIR);
1312 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1318 if (parent2 == NULL || IS_ERR(parent2)) {
1319 dt_read_unlock(env, child);
1320 lfsck_ibits_unlock(&lh, LCK_PR);
1322 GOTO(out_done, rc = 1);
1325 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1326 (const struct dt_key *)name2);
1327 dt_read_unlock(env, child);
1328 lfsck_ibits_unlock(&lh, LCK_PR);
1329 if (rc != 0 && rc != -ENOENT)
1332 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1333 if (type == LVLT_BY_BOOKMARK)
1334 GOTO(out_put, rc = 1);
1336 /* Trust the name entry, update the child's dotdot entry. */
1337 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1338 &LU_LPF_FID, S_IFDIR);
1343 if (type == LVLT_BY_BOOKMARK) {
1344 /* Invalid FID record in the bookmark file, reset it. */
1345 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1346 rc = lfsck_bookmark_store(env, lfsck);
1348 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1349 " in the bookmark file: rc = %d\n",
1350 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1351 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1352 /* The name entry is wrong, remove it. */
1353 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1359 if (parent2 != NULL && !IS_ERR(parent2))
1360 lfsck_object_put(env, parent2);
1367 * Verify the /ROOT/.lustre/lost+found/ directory.
1369 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1370 * the LFSCK does not exactly know how to handle, such as orphans. So before
1371 * the LFSCK scanning the system, the consistency of such directory needs to
1372 * be verified firstly to allow the users to use it during the LFSCK.
1374 * \param[in] env pointer to the thread context
1375 * \param[in] lfsck pointer to the lfsck instance
1377 * \retval positive number for uncertain inconsistency
1378 * \retval 0 for success
1379 * \retval negative error number on failure
1381 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1383 struct lfsck_thread_info *info = lfsck_env_info(env);
1384 struct lu_fid *pfid = &info->lti_fid;
1385 struct lu_fid *cfid = &info->lti_fid2;
1386 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1387 struct dt_object *parent;
1388 /* child1's FID is in the bookmark file. */
1389 struct dt_object *child1 = NULL;
1390 /* child2's FID is in the name entry MDTxxxx. */
1391 struct dt_object *child2 = NULL;
1392 const struct lu_name *cname;
1394 int node = lfsck_dev_idx(lfsck);
1398 LASSERT(lfsck->li_master);
1400 if (lfsck->li_lpf_root_obj != NULL)
1404 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1407 struct lfsck_tgt_desc *ltd;
1409 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1410 if (unlikely(ltd == NULL))
1413 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1419 RETURN(PTR_ERR(parent));
1421 LASSERT(dt_object_exists(parent));
1423 if (unlikely(!dt_try_as_dir(env, parent))) {
1424 lfsck_object_put(env, parent);
1426 GOTO(put, rc = -ENOTDIR);
1429 lfsck->li_lpf_root_obj = parent;
1431 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1433 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1434 "for bad sub-directories: rc = %d\n",
1435 lfsck_lfsck2name(lfsck), rc);
1439 snprintf(name, 8, "MDT%04x", node);
1440 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1441 (const struct dt_key *)name);
1442 if (rc == -ENOENT) {
1450 /* Invalid FID in the name entry, remove the name entry. */
1451 if (!fid_is_norm(cfid)) {
1452 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1459 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1461 GOTO(put, rc = PTR_ERR(child2));
1463 if (unlikely(!dt_object_exists(child2) ||
1464 dt_object_remote(child2)) ||
1465 !S_ISDIR(lfsck_object_type(child2))) {
1466 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1473 if (unlikely(!dt_try_as_dir(env, child2))) {
1474 lfsck_object_put(env, child2);
1480 if (fid_is_zero(&bk->lb_lpf_fid))
1483 if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1484 if (lfsck->li_lpf_obj == NULL) {
1485 lu_object_get(&child2->do_lu);
1486 lfsck->li_lpf_obj = child2;
1489 cname = lfsck_name_get_const(env, name, strlen(name));
1490 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1495 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1496 struct lu_fid tfid = bk->lb_lpf_fid;
1498 /* Invalid FID record in the bookmark file, reset it. */
1499 fid_zero(&bk->lb_lpf_fid);
1500 rc = lfsck_bookmark_store(env, lfsck);
1502 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1503 " in the bookmark file: rc = %d\n",
1504 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1512 child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1513 if (IS_ERR(child1)) {
1518 if (unlikely(!dt_object_exists(child1) ||
1519 dt_object_remote(child1)) ||
1520 !S_ISDIR(lfsck_object_type(child1))) {
1521 /* Invalid FID record in the bookmark file, reset it. */
1522 fid_zero(&bk->lb_lpf_fid);
1523 rc = lfsck_bookmark_store(env, lfsck);
1525 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1526 " in the bookmark file: rc = %d\n",
1527 lfsck_lfsck2name(lfsck),
1528 PFID(lfsck_dto2fid(child1)), rc);
1533 lfsck_object_put(env, child1);
1538 if (unlikely(!dt_try_as_dir(env, child1))) {
1539 lfsck_object_put(env, child1);
1545 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1547 if (lu_fid_eq(pfid, &LU_LPF_FID))
1552 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1553 pfid, LVLT_BY_NAMEENTRY);
1558 if (lfsck->li_lpf_obj != NULL) {
1559 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1560 lfsck_object_put(env, lfsck->li_lpf_obj);
1561 lfsck->li_lpf_obj = NULL;
1564 } else if (rc == 0) {
1565 rc = lfsck_create_lpf(env, lfsck);
1568 if (child2 != NULL && !IS_ERR(child2))
1569 lfsck_object_put(env, child2);
1570 if (child1 != NULL && !IS_ERR(child1))
1571 lfsck_object_put(env, child1);
1576 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1578 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1579 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1584 if (unlikely(ss == NULL))
1587 OBD_ALLOC_PTR(lfsck->li_seq);
1588 if (lfsck->li_seq == NULL)
1591 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1593 GOTO(out, rc = -ENOMEM);
1595 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1596 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1598 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1602 if (fid_is_sane(&bk->lb_last_fid))
1603 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1608 OBD_FREE_PTR(lfsck->li_seq);
1609 lfsck->li_seq = NULL;
1614 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1616 if (lfsck->li_seq != NULL) {
1617 seq_client_fini(lfsck->li_seq);
1618 OBD_FREE_PTR(lfsck->li_seq);
1619 lfsck->li_seq = NULL;
1623 void lfsck_instance_cleanup(const struct lu_env *env,
1624 struct lfsck_instance *lfsck)
1626 struct ptlrpc_thread *thread = &lfsck->li_thread;
1627 struct lfsck_component *com;
1628 struct lfsck_component *next;
1629 struct lfsck_lmv_unit *llu;
1630 struct lfsck_lmv_unit *llu_next;
1631 struct lfsck_lmv *llmv;
1634 LASSERT(list_empty(&lfsck->li_link));
1635 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1637 if (lfsck->li_obj_oit != NULL) {
1638 lfsck_object_put(env, lfsck->li_obj_oit);
1639 lfsck->li_obj_oit = NULL;
1642 LASSERT(lfsck->li_obj_dir == NULL);
1643 LASSERT(lfsck->li_lmv == NULL);
1645 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1646 llmv = &llu->llu_lmv;
1648 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1649 "still in using: %u\n",
1650 atomic_read(&llmv->ll_ref));
1652 lfsck_lmv_put(env, llmv);
1655 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1656 lfsck_component_cleanup(env, com);
1659 LASSERT(list_empty(&lfsck->li_list_dir));
1661 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1663 lfsck_component_cleanup(env, com);
1666 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1667 lfsck_component_cleanup(env, com);
1670 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1671 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1673 if (lfsck->li_lfsck_dir != NULL) {
1674 lfsck_object_put(env, lfsck->li_lfsck_dir);
1675 lfsck->li_lfsck_dir = NULL;
1678 if (lfsck->li_bookmark_obj != NULL) {
1679 lfsck_object_put(env, lfsck->li_bookmark_obj);
1680 lfsck->li_bookmark_obj = NULL;
1683 if (lfsck->li_lpf_obj != NULL) {
1684 lfsck_object_put(env, lfsck->li_lpf_obj);
1685 lfsck->li_lpf_obj = NULL;
1688 if (lfsck->li_lpf_root_obj != NULL) {
1689 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1690 lfsck->li_lpf_root_obj = NULL;
1693 if (lfsck->li_los != NULL) {
1694 local_oid_storage_fini(env, lfsck->li_los);
1695 lfsck->li_los = NULL;
1698 lfsck_fid_fini(lfsck);
1700 OBD_FREE_PTR(lfsck);
1703 static inline struct lfsck_instance *
1704 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1706 struct lfsck_instance *lfsck;
1708 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1709 if (lfsck->li_bottom == key) {
1711 lfsck_instance_get(lfsck);
1713 list_del_init(&lfsck->li_link);
1722 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1725 struct lfsck_instance *lfsck;
1727 spin_lock(&lfsck_instance_lock);
1728 lfsck = __lfsck_instance_find(key, ref, unlink);
1729 spin_unlock(&lfsck_instance_lock);
1734 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1736 struct lfsck_instance *tmp;
1738 spin_lock(&lfsck_instance_lock);
1739 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1740 if (lfsck->li_bottom == tmp->li_bottom) {
1741 spin_unlock(&lfsck_instance_lock);
1746 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1747 spin_unlock(&lfsck_instance_lock);
1751 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1756 bool newline = (bits != 0 ? false : true);
1759 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1763 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1766 if (names[i] != NULL) {
1770 rc = seq_printf(m, "%s%c", names[i],
1771 newline ? '\n' : ',');
1779 rc = seq_printf(m, "\n");
1784 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1789 rc = seq_printf(m, "%s_time: N/A\n", name);
1791 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1796 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1798 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1799 name, cfs_time_current_sec() - time);
1804 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1807 if (fid_is_zero(&pos->lp_dir_parent)) {
1808 if (pos->lp_oit_cookie == 0)
1809 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1811 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1812 prefix, pos->lp_oit_cookie);
1815 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1816 prefix, pos->lp_oit_cookie,
1817 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1820 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1821 struct lfsck_position *pos, bool init)
1823 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1825 if (unlikely(lfsck->li_di_oit == NULL)) {
1826 memset(pos, 0, sizeof(*pos));
1830 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1831 if (!lfsck->li_current_oit_processed && !init)
1832 pos->lp_oit_cookie--;
1834 LASSERT(pos->lp_oit_cookie > 0);
1836 if (lfsck->li_di_dir != NULL) {
1837 struct dt_object *dto = lfsck->li_obj_dir;
1839 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1842 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1843 fid_zero(&pos->lp_dir_parent);
1844 pos->lp_dir_cookie = 0;
1846 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1849 fid_zero(&pos->lp_dir_parent);
1850 pos->lp_dir_cookie = 0;
1854 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1858 if (limit != LFSCK_SPEED_NO_LIMIT) {
1859 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1860 lfsck->li_sleep_rate = limit /
1861 msecs_to_jiffies(MSEC_PER_SEC);
1862 lfsck->li_sleep_jif = 1;
1864 lfsck->li_sleep_rate = 1;
1865 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1869 lfsck->li_sleep_jif = 0;
1870 lfsck->li_sleep_rate = 0;
1873 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1874 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1881 void lfsck_control_speed(struct lfsck_instance *lfsck)
1883 struct ptlrpc_thread *thread = &lfsck->li_thread;
1884 struct l_wait_info lwi;
1886 if (lfsck->li_sleep_jif > 0 &&
1887 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1888 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1889 LWI_ON_SIGNAL_NOOP, NULL);
1891 l_wait_event(thread->t_ctl_waitq,
1892 !thread_is_running(thread),
1894 lfsck->li_new_scanned = 0;
1898 void lfsck_control_speed_by_self(struct lfsck_component *com)
1900 struct lfsck_instance *lfsck = com->lc_lfsck;
1901 struct ptlrpc_thread *thread = &lfsck->li_thread;
1902 struct l_wait_info lwi;
1904 if (lfsck->li_sleep_jif > 0 &&
1905 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1906 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1907 LWI_ON_SIGNAL_NOOP, NULL);
1909 l_wait_event(thread->t_ctl_waitq,
1910 !thread_is_running(thread),
1912 com->lc_new_scanned = 0;
1916 static struct lfsck_thread_args *
1917 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1918 struct lfsck_component *com,
1919 struct lfsck_start_param *lsp)
1921 struct lfsck_thread_args *lta;
1926 return ERR_PTR(-ENOMEM);
1928 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1934 lta->lta_lfsck = lfsck_instance_get(lfsck);
1936 lta->lta_com = lfsck_component_get(com);
1943 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1945 if (lta->lta_com != NULL)
1946 lfsck_component_put(<a->lta_env, lta->lta_com);
1947 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1948 lu_env_fini(<a->lta_env);
1952 struct lfsck_assistant_data *
1953 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1956 struct lfsck_assistant_data *lad;
1960 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1961 if (lad->lad_bitmap == NULL) {
1966 INIT_LIST_HEAD(&lad->lad_req_list);
1967 spin_lock_init(&lad->lad_lock);
1968 INIT_LIST_HEAD(&lad->lad_ost_list);
1969 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1970 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1971 INIT_LIST_HEAD(&lad->lad_mdt_list);
1972 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1973 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1974 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1976 lad->lad_name = name;
1982 struct lfsck_assistant_object *
1983 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1984 const struct lu_attr *attr, __u64 cookie,
1987 struct lfsck_assistant_object *lso;
1991 return ERR_PTR(-ENOMEM);
1993 lso->lso_fid = *fid;
1995 lso->lso_attr = *attr;
1997 atomic_set(&lso->lso_ref, 1);
1998 lso->lso_oit_cookie = cookie;
2000 lso->lso_is_dir = 1;
2006 lfsck_assistant_object_load(const struct lu_env *env,
2007 struct lfsck_instance *lfsck,
2008 struct lfsck_assistant_object *lso)
2010 struct dt_object *obj;
2012 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2016 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2018 lfsck_object_put(env, obj);
2020 return ERR_PTR(-ENOENT);
2023 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2024 lfsck_object_put(env, obj);
2026 return ERR_PTR(-ENOTDIR);
2033 * Generic LFSCK asynchronous communication interpretor function.
2034 * The LFSCK RPC reply for both the event notification and status
2035 * querying will be handled here.
2037 * \param[in] env pointer to the thread context
2038 * \param[in] req pointer to the LFSCK request
2039 * \param[in] args pointer to the lfsck_async_interpret_args
2040 * \param[in] rc the result for handling the LFSCK request
2042 * \retval 0 for success
2043 * \retval negative error number on failure
2045 int lfsck_async_interpret_common(const struct lu_env *env,
2046 struct ptlrpc_request *req,
2049 struct lfsck_async_interpret_args *laia = args;
2050 struct lfsck_component *com = laia->laia_com;
2051 struct lfsck_assistant_data *lad = com->lc_data;
2052 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2053 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2054 struct lfsck_request *lr = laia->laia_lr;
2056 LASSERT(com->lc_lfsck->li_master);
2058 switch (lr->lr_event) {
2061 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2063 lfsck_lfsck2name(com->lc_lfsck),
2064 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2065 ltd->ltd_index, lad->lad_name, rc);
2067 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2068 struct lfsck_layout *lo = com->lc_file_ram;
2070 if (lr->lr_flags & LEF_TO_OST)
2071 lfsck_lad_set_bitmap(env, com,
2074 lo->ll_flags |= LF_INCOMPLETE;
2076 struct lfsck_namespace *ns = com->lc_file_ram;
2078 /* If some MDT does not join the namespace
2079 * LFSCK, then we cannot know whether there
2080 * is some name entry on such MDT that with
2081 * the referenced MDT-object on this MDT or
2082 * not. So the namespace LFSCK on this MDT
2083 * cannot handle orphan MDT-objects properly.
2084 * So we mark the LFSCK as LF_INCOMPLETE and
2085 * skip orphan MDT-objects handling. */
2086 ns->ln_flags |= LF_INCOMPLETE;
2091 spin_lock(<ds->ltd_lock);
2092 if (ltd->ltd_dead) {
2093 spin_unlock(<ds->ltd_lock);
2097 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2098 struct list_head *list;
2099 struct list_head *phase_list;
2101 if (ltd->ltd_layout_done) {
2102 spin_unlock(<ds->ltd_lock);
2106 if (lr->lr_flags & LEF_TO_OST) {
2107 list = &lad->lad_ost_list;
2108 phase_list = &lad->lad_ost_phase1_list;
2110 list = &lad->lad_mdt_list;
2111 phase_list = &lad->lad_mdt_phase1_list;
2114 if (list_empty(<d->ltd_layout_list))
2115 list_add_tail(<d->ltd_layout_list, list);
2116 if (list_empty(<d->ltd_layout_phase_list))
2117 list_add_tail(<d->ltd_layout_phase_list,
2120 if (ltd->ltd_namespace_done) {
2121 spin_unlock(<ds->ltd_lock);
2125 if (list_empty(<d->ltd_namespace_list))
2126 list_add_tail(<d->ltd_namespace_list,
2127 &lad->lad_mdt_list);
2128 if (list_empty(<d->ltd_namespace_phase_list))
2129 list_add_tail(<d->ltd_namespace_phase_list,
2130 &lad->lad_mdt_phase1_list);
2132 spin_unlock(<ds->ltd_lock);
2135 case LE_PHASE1_DONE:
2136 case LE_PHASE2_DONE:
2138 if (rc != 0 && rc != -EALREADY)
2139 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2140 "event = %d, rc = %d\n",
2141 lfsck_lfsck2name(com->lc_lfsck),
2142 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2143 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2146 struct lfsck_reply *reply;
2147 struct list_head *list;
2148 struct list_head *phase_list;
2150 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2151 list = <d->ltd_layout_list;
2152 phase_list = <d->ltd_layout_phase_list;
2154 list = <d->ltd_namespace_list;
2155 phase_list = <d->ltd_namespace_phase_list;
2159 spin_lock(<ds->ltd_lock);
2160 list_del_init(phase_list);
2161 list_del_init(list);
2162 spin_unlock(<ds->ltd_lock);
2166 reply = req_capsule_server_get(&req->rq_pill,
2168 if (reply == NULL) {
2170 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2171 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2173 spin_lock(<ds->ltd_lock);
2174 list_del_init(phase_list);
2175 list_del_init(list);
2176 spin_unlock(<ds->ltd_lock);
2180 switch (reply->lr_status) {
2181 case LS_SCANNING_PHASE1:
2183 case LS_SCANNING_PHASE2:
2184 spin_lock(<ds->ltd_lock);
2185 list_del_init(phase_list);
2186 if (ltd->ltd_dead) {
2187 spin_unlock(<ds->ltd_lock);
2191 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2192 if (ltd->ltd_layout_done) {
2193 spin_unlock(<ds->ltd_lock);
2197 if (lr->lr_flags & LEF_TO_OST)
2198 list_add_tail(phase_list,
2199 &lad->lad_ost_phase2_list);
2201 list_add_tail(phase_list,
2202 &lad->lad_mdt_phase2_list);
2204 if (ltd->ltd_namespace_done) {
2205 spin_unlock(<ds->ltd_lock);
2209 list_add_tail(phase_list,
2210 &lad->lad_mdt_phase2_list);
2212 spin_unlock(<ds->ltd_lock);
2215 spin_lock(<ds->ltd_lock);
2216 list_del_init(phase_list);
2217 list_del_init(list);
2218 spin_unlock(<ds->ltd_lock);
2224 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2225 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2229 if (!laia->laia_shared) {
2231 lfsck_component_put(env, com);
2237 static void lfsck_interpret(const struct lu_env *env,
2238 struct lfsck_instance *lfsck,
2239 struct ptlrpc_request *req, void *args, int result)
2241 struct lfsck_async_interpret_args *laia = args;
2242 struct lfsck_component *com;
2244 LASSERT(laia->laia_com == NULL);
2245 LASSERT(laia->laia_shared);
2247 spin_lock(&lfsck->li_lock);
2248 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2249 laia->laia_com = com;
2250 lfsck_async_interpret_common(env, req, laia, result);
2253 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2254 laia->laia_com = com;
2255 lfsck_async_interpret_common(env, req, laia, result);
2257 spin_unlock(&lfsck->li_lock);
2260 static int lfsck_stop_notify(const struct lu_env *env,
2261 struct lfsck_instance *lfsck,
2262 struct lfsck_tgt_descs *ltds,
2263 struct lfsck_tgt_desc *ltd, __u16 type)
2265 struct lfsck_component *com;
2269 LASSERT(lfsck->li_master);
2271 spin_lock(&lfsck->li_lock);
2272 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2274 com = __lfsck_component_find(lfsck, type,
2275 &lfsck->li_list_double_scan);
2277 lfsck_component_get(com);
2278 spin_unlock(&lfsck->li_lock);
2281 struct lfsck_thread_info *info = lfsck_env_info(env);
2282 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2283 struct lfsck_request *lr = &info->lti_lr;
2284 struct lfsck_assistant_data *lad = com->lc_data;
2285 struct list_head *list;
2286 struct list_head *phase_list;
2287 struct ptlrpc_request_set *set;
2289 set = ptlrpc_prep_set();
2291 lfsck_component_put(env, com);
2296 if (type == LFSCK_TYPE_LAYOUT) {
2297 list = <d->ltd_layout_list;
2298 phase_list = <d->ltd_layout_phase_list;
2300 list = <d->ltd_namespace_list;
2301 phase_list = <d->ltd_namespace_phase_list;
2304 spin_lock(<ds->ltd_lock);
2305 if (list_empty(list)) {
2306 LASSERT(list_empty(phase_list));
2307 spin_unlock(<ds->ltd_lock);
2308 ptlrpc_set_destroy(set);
2313 list_del_init(phase_list);
2314 list_del_init(list);
2315 spin_unlock(<ds->ltd_lock);
2317 memset(lr, 0, sizeof(*lr));
2318 lr->lr_index = lfsck_dev_idx(lfsck);
2319 lr->lr_event = LE_PEER_EXIT;
2320 lr->lr_active = type;
2321 lr->lr_status = LS_CO_PAUSED;
2322 if (ltds == &lfsck->li_ost_descs)
2323 lr->lr_flags = LEF_TO_OST;
2325 laia->laia_com = com;
2326 laia->laia_ltds = ltds;
2327 atomic_inc(<d->ltd_ref);
2328 laia->laia_ltd = ltd;
2330 laia->laia_shared = 0;
2332 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2333 lfsck_async_interpret_common,
2334 laia, LFSCK_NOTIFY);
2336 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2337 "co-stop for %s: rc = %d\n",
2338 lfsck_lfsck2name(lfsck),
2339 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2340 ltd->ltd_index, lad->lad_name, rc);
2343 rc = ptlrpc_set_wait(set);
2346 ptlrpc_set_destroy(set);
2347 lfsck_component_put(env, com);
2353 static int lfsck_async_interpret(const struct lu_env *env,
2354 struct ptlrpc_request *req,
2357 struct lfsck_async_interpret_args *laia = args;
2358 struct lfsck_instance *lfsck;
2360 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2362 lfsck_interpret(env, lfsck, req, laia, rc);
2363 lfsck_tgt_put(laia->laia_ltd);
2364 if (rc != 0 && laia->laia_result != -EALREADY)
2365 laia->laia_result = rc;
2370 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2371 struct lfsck_request *lr,
2372 struct ptlrpc_request_set *set,
2373 ptlrpc_interpterer_t interpreter,
2374 void *args, int request)
2376 struct lfsck_async_interpret_args *laia;
2377 struct ptlrpc_request *req;
2378 struct lfsck_request *tmp;
2379 struct req_format *format;
2384 format = &RQF_LFSCK_NOTIFY;
2387 format = &RQF_LFSCK_QUERY;
2390 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2391 exp->exp_obd->obd_name, request, -EINVAL);
2395 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2399 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2401 ptlrpc_request_free(req);
2406 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2408 ptlrpc_request_set_replen(req);
2410 laia = ptlrpc_req_async_args(req);
2411 *laia = *(struct lfsck_async_interpret_args *)args;
2412 if (laia->laia_com != NULL)
2413 lfsck_component_get(laia->laia_com);
2414 req->rq_interpret_reply = interpreter;
2415 ptlrpc_set_add_req(set, req);
2420 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2421 struct lfsck_start_param *lsp)
2423 struct lfsck_instance *lfsck = com->lc_lfsck;
2424 struct lfsck_assistant_data *lad = com->lc_data;
2425 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2426 struct ptlrpc_thread *athread = &lad->lad_thread;
2427 struct lfsck_thread_args *lta;
2428 struct task_struct *task;
2432 lad->lad_assistant_status = 0;
2433 lad->lad_post_result = 0;
2434 lad->lad_to_post = 0;
2435 lad->lad_to_double_scan = 0;
2436 lad->lad_in_double_scan = 0;
2438 lad->lad_advance_lock = false;
2439 thread_set_flags(athread, 0);
2441 lta = lfsck_thread_args_init(lfsck, com, lsp);
2443 RETURN(PTR_ERR(lta));
2445 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2448 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2449 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2450 lfsck_thread_args_fini(lta);
2452 struct l_wait_info lwi = { 0 };
2454 l_wait_event(mthread->t_ctl_waitq,
2455 thread_is_running(athread) ||
2456 thread_is_stopped(athread),
2458 if (unlikely(!thread_is_running(athread)))
2459 rc = lad->lad_assistant_status;
2467 int lfsck_checkpoint_generic(const struct lu_env *env,
2468 struct lfsck_component *com)
2470 struct lfsck_assistant_data *lad = com->lc_data;
2471 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2472 struct ptlrpc_thread *athread = &lad->lad_thread;
2473 struct l_wait_info lwi = { 0 };
2475 l_wait_event(mthread->t_ctl_waitq,
2476 list_empty(&lad->lad_req_list) ||
2477 !thread_is_running(mthread) ||
2478 thread_is_stopped(athread),
2481 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2482 return LFSCK_CHECKPOINT_SKIP;
2487 void lfsck_post_generic(const struct lu_env *env,
2488 struct lfsck_component *com, int *result)
2490 struct lfsck_assistant_data *lad = com->lc_data;
2491 struct ptlrpc_thread *athread = &lad->lad_thread;
2492 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2493 struct l_wait_info lwi = { 0 };
2495 lad->lad_post_result = *result;
2498 lad->lad_to_post = 1;
2500 wake_up_all(&athread->t_ctl_waitq);
2501 l_wait_event(mthread->t_ctl_waitq,
2502 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2503 thread_is_stopped(athread),
2506 if (lad->lad_assistant_status < 0)
2507 *result = lad->lad_assistant_status;
2510 int lfsck_double_scan_generic(const struct lu_env *env,
2511 struct lfsck_component *com, int status)
2513 struct lfsck_assistant_data *lad = com->lc_data;
2514 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2515 struct ptlrpc_thread *athread = &lad->lad_thread;
2516 struct l_wait_info lwi = { 0 };
2518 if (status != LS_SCANNING_PHASE2)
2521 lad->lad_to_double_scan = 1;
2523 wake_up_all(&athread->t_ctl_waitq);
2524 l_wait_event(mthread->t_ctl_waitq,
2525 lad->lad_in_double_scan ||
2526 thread_is_stopped(athread),
2529 if (lad->lad_assistant_status < 0)
2530 return lad->lad_assistant_status;
2535 void lfsck_quit_generic(const struct lu_env *env,
2536 struct lfsck_component *com)
2538 struct lfsck_assistant_data *lad = com->lc_data;
2539 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2540 struct ptlrpc_thread *athread = &lad->lad_thread;
2541 struct l_wait_info lwi = { 0 };
2544 wake_up_all(&athread->t_ctl_waitq);
2545 l_wait_event(mthread->t_ctl_waitq,
2546 thread_is_init(athread) ||
2547 thread_is_stopped(athread),
2551 /* external interfaces */
2553 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2556 struct lfsck_instance *lfsck;
2560 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2564 lfsck = lfsck_instance_find(key, true, false);
2565 if (likely(lfsck != NULL)) {
2566 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2567 lfsck_instance_put(&env, lfsck);
2576 EXPORT_SYMBOL(lfsck_get_speed);
2578 int lfsck_set_speed(struct dt_device *key, int val)
2581 struct lfsck_instance *lfsck;
2585 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2589 lfsck = lfsck_instance_find(key, true, false);
2590 if (likely(lfsck != NULL)) {
2591 mutex_lock(&lfsck->li_mutex);
2592 if (__lfsck_set_speed(lfsck, val))
2593 rc = lfsck_bookmark_store(&env, lfsck);
2594 mutex_unlock(&lfsck->li_mutex);
2595 lfsck_instance_put(&env, lfsck);
2604 EXPORT_SYMBOL(lfsck_set_speed);
2606 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2609 struct lfsck_instance *lfsck;
2613 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2617 lfsck = lfsck_instance_find(key, true, false);
2618 if (likely(lfsck != NULL)) {
2619 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2620 lfsck_instance_put(&env, lfsck);
2629 EXPORT_SYMBOL(lfsck_get_windows);
2631 int lfsck_set_windows(struct dt_device *key, int val)
2634 struct lfsck_instance *lfsck;
2638 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2642 lfsck = lfsck_instance_find(key, true, false);
2643 if (likely(lfsck != NULL)) {
2644 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2645 CWARN("%s: invalid async windows size that may "
2646 "cause memory issues. The valid range is "
2648 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2650 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2651 mutex_lock(&lfsck->li_mutex);
2652 lfsck->li_bookmark_ram.lb_async_windows = val;
2653 rc = lfsck_bookmark_store(&env, lfsck);
2654 mutex_unlock(&lfsck->li_mutex);
2656 lfsck_instance_put(&env, lfsck);
2665 EXPORT_SYMBOL(lfsck_set_windows);
2667 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2670 struct lfsck_instance *lfsck;
2671 struct lfsck_component *com;
2675 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2679 lfsck = lfsck_instance_find(key, true, false);
2680 if (likely(lfsck != NULL)) {
2681 com = lfsck_component_find(lfsck, type);
2682 if (likely(com != NULL)) {
2683 rc = com->lc_ops->lfsck_dump(&env, com, m);
2684 lfsck_component_put(&env, com);
2689 lfsck_instance_put(&env, lfsck);
2698 EXPORT_SYMBOL(lfsck_dump);
2700 static int lfsck_stop_all(const struct lu_env *env,
2701 struct lfsck_instance *lfsck,
2702 struct lfsck_stop *stop)
2704 struct lfsck_thread_info *info = lfsck_env_info(env);
2705 struct lfsck_request *lr = &info->lti_lr;
2706 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2707 struct ptlrpc_request_set *set;
2708 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2709 struct lfsck_tgt_desc *ltd;
2710 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2716 LASSERT(stop->ls_flags & LPF_BROADCAST);
2718 set = ptlrpc_prep_set();
2719 if (unlikely(set == NULL))
2722 memset(lr, 0, sizeof(*lr));
2723 lr->lr_event = LE_STOP;
2724 lr->lr_index = lfsck_dev_idx(lfsck);
2725 lr->lr_status = stop->ls_status;
2726 lr->lr_version = bk->lb_version;
2727 lr->lr_active = LFSCK_TYPES_ALL;
2728 lr->lr_param = stop->ls_flags;
2730 laia->laia_com = NULL;
2731 laia->laia_ltds = ltds;
2733 laia->laia_result = 0;
2734 laia->laia_shared = 1;
2736 down_read(<ds->ltd_rw_sem);
2737 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2738 ltd = lfsck_tgt_get(ltds, idx);
2739 LASSERT(ltd != NULL);
2741 laia->laia_ltd = ltd;
2742 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2743 lfsck_async_interpret, laia,
2746 lfsck_interpret(env, lfsck, NULL, laia, rc);
2748 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2749 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2753 up_read(<ds->ltd_rw_sem);
2755 rc = ptlrpc_set_wait(set);
2756 ptlrpc_set_destroy(set);
2759 rc = laia->laia_result;
2761 if (rc == -EALREADY)
2765 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2766 lfsck_lfsck2name(lfsck), rc);
2768 RETURN(rc != 0 ? rc : rc1);
2771 static int lfsck_start_all(const struct lu_env *env,
2772 struct lfsck_instance *lfsck,
2773 struct lfsck_start *start)
2775 struct lfsck_thread_info *info = lfsck_env_info(env);
2776 struct lfsck_request *lr = &info->lti_lr;
2777 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2778 struct ptlrpc_request_set *set;
2779 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2780 struct lfsck_tgt_desc *ltd;
2781 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2786 LASSERT(start->ls_flags & LPF_BROADCAST);
2788 set = ptlrpc_prep_set();
2789 if (unlikely(set == NULL))
2792 memset(lr, 0, sizeof(*lr));
2793 lr->lr_event = LE_START;
2794 lr->lr_index = lfsck_dev_idx(lfsck);
2795 lr->lr_speed = bk->lb_speed_limit;
2796 lr->lr_version = bk->lb_version;
2797 lr->lr_active = start->ls_active;
2798 lr->lr_param = start->ls_flags;
2799 lr->lr_async_windows = bk->lb_async_windows;
2800 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2801 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2804 laia->laia_com = NULL;
2805 laia->laia_ltds = ltds;
2807 laia->laia_result = 0;
2808 laia->laia_shared = 1;
2810 down_read(<ds->ltd_rw_sem);
2811 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2812 ltd = lfsck_tgt_get(ltds, idx);
2813 LASSERT(ltd != NULL);
2815 laia->laia_ltd = ltd;
2816 ltd->ltd_layout_done = 0;
2817 ltd->ltd_namespace_done = 0;
2818 ltd->ltd_synced_failures = 0;
2819 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2820 lfsck_async_interpret, laia,
2823 lfsck_interpret(env, lfsck, NULL, laia, rc);
2825 CERROR("%s: cannot notify MDT %x for LFSCK "
2826 "start, failout: rc = %d\n",
2827 lfsck_lfsck2name(lfsck), idx, rc);
2831 up_read(<ds->ltd_rw_sem);
2834 ptlrpc_set_destroy(set);
2839 rc = ptlrpc_set_wait(set);
2840 ptlrpc_set_destroy(set);
2843 rc = laia->laia_result;
2846 struct lfsck_stop *stop = &info->lti_stop;
2848 CERROR("%s: cannot start LFSCK on some MDTs, "
2849 "stop all: rc = %d\n",
2850 lfsck_lfsck2name(lfsck), rc);
2851 if (rc != -EALREADY) {
2852 stop->ls_status = LS_FAILED;
2853 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2854 lfsck_stop_all(env, lfsck, stop);
2861 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2862 struct lfsck_start_param *lsp)
2864 struct lfsck_start *start = lsp->lsp_start;
2865 struct lfsck_instance *lfsck;
2866 struct lfsck_bookmark *bk;
2867 struct ptlrpc_thread *thread;
2868 struct lfsck_component *com;
2869 struct l_wait_info lwi = { 0 };
2870 struct lfsck_thread_args *lta;
2871 struct task_struct *task;
2878 lfsck = lfsck_instance_find(key, true, false);
2879 if (unlikely(lfsck == NULL))
2882 /* System is not ready, try again later. */
2883 if (unlikely(lfsck->li_namespace == NULL))
2884 GOTO(put, rc = -EAGAIN);
2886 /* start == NULL means auto trigger paused LFSCK. */
2887 if ((start == NULL) &&
2888 (list_empty(&lfsck->li_list_scan) ||
2889 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2892 bk = &lfsck->li_bookmark_ram;
2893 thread = &lfsck->li_thread;
2894 mutex_lock(&lfsck->li_mutex);
2895 spin_lock(&lfsck->li_lock);
2896 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2898 if (unlikely(start == NULL)) {
2899 spin_unlock(&lfsck->li_lock);
2903 while (start->ls_active != 0) {
2904 if (!(type & start->ls_active)) {
2909 com = __lfsck_component_find(lfsck, type,
2910 &lfsck->li_list_scan);
2912 com = __lfsck_component_find(lfsck, type,
2913 &lfsck->li_list_double_scan);
2919 if (com->lc_ops->lfsck_join != NULL) {
2920 rc = com->lc_ops->lfsck_join( env, com, lsp);
2921 if (rc != 0 && rc != -EALREADY)
2924 start->ls_active &= ~type;
2927 spin_unlock(&lfsck->li_lock);
2930 spin_unlock(&lfsck->li_lock);
2932 lfsck->li_status = 0;
2933 lfsck->li_oit_over = 0;
2934 lfsck->li_start_unplug = 0;
2935 lfsck->li_drop_dryrun = 0;
2936 lfsck->li_new_scanned = 0;
2938 /* For auto trigger. */
2942 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2943 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2944 lfsck_lfsck2name(lfsck));
2946 GOTO(out, rc = -EPERM);
2949 start->ls_version = bk->lb_version;
2951 if (start->ls_active != 0) {
2952 struct lfsck_component *next;
2954 if (start->ls_active == LFSCK_TYPES_ALL)
2955 start->ls_active = LFSCK_TYPES_SUPPORTED;
2957 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2958 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2959 GOTO(out, rc = -ENOTSUPP);
2962 list_for_each_entry_safe(com, next,
2963 &lfsck->li_list_scan, lc_link) {
2964 if (!(com->lc_type & start->ls_active)) {
2965 rc = com->lc_ops->lfsck_post(env, com, 0,
2972 while (start->ls_active != 0) {
2973 if (type & start->ls_active) {
2974 com = __lfsck_component_find(lfsck, type,
2975 &lfsck->li_list_idle);
2977 /* The component status will be updated
2978 * when its prep() is called later by
2979 * the LFSCK main engine. */
2980 list_move_tail(&com->lc_link,
2981 &lfsck->li_list_scan);
2982 start->ls_active &= ~type;
2988 if (list_empty(&lfsck->li_list_scan)) {
2989 /* The speed limit will be used to control both the LFSCK and
2990 * low layer scrub (if applied), need to be handled firstly. */
2991 if (start->ls_valid & LSV_SPEED_LIMIT) {
2992 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2993 rc = lfsck_bookmark_store(env, lfsck);
3002 if (start->ls_flags & LPF_RESET)
3003 flags |= DOIF_RESET;
3005 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3009 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3010 start->ls_active |= com->lc_type;
3011 if (flags & DOIF_RESET) {
3012 rc = com->lc_ops->lfsck_reset(env, com, false);
3019 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3020 if (bk->lb_param & LPF_DRYRUN)
3021 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3023 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3024 valid |= DOIV_ERROR_HANDLE;
3025 if (start->ls_flags & LPF_FAILOUT)
3026 flags |= DOIF_FAILOUT;
3029 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3030 valid |= DOIV_DRYRUN;
3031 if (start->ls_flags & LPF_DRYRUN)
3032 flags |= DOIF_DRYRUN;
3035 if (!list_empty(&lfsck->li_list_scan))
3036 flags |= DOIF_OUTUSED;
3038 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3039 thread_set_flags(thread, 0);
3040 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3042 GOTO(out, rc = PTR_ERR(lta));
3044 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3045 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3048 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3049 lfsck_lfsck2name(lfsck), rc);
3050 lfsck_thread_args_fini(lta);
3055 l_wait_event(thread->t_ctl_waitq,
3056 thread_is_running(thread) ||
3057 thread_is_stopped(thread),
3059 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3060 lfsck->li_start_unplug = 1;
3061 wake_up_all(&thread->t_ctl_waitq);
3066 /* release lfsck::li_mutex to avoid deadlock. */
3067 mutex_unlock(&lfsck->li_mutex);
3068 rc = lfsck_start_all(env, lfsck, start);
3070 spin_lock(&lfsck->li_lock);
3071 if (thread_is_stopped(thread)) {
3072 spin_unlock(&lfsck->li_lock);
3074 lfsck->li_status = LS_FAILED;
3075 lfsck->li_flags = 0;
3076 thread_set_flags(thread, SVC_STOPPING);
3077 spin_unlock(&lfsck->li_lock);
3079 lfsck->li_start_unplug = 1;
3080 wake_up_all(&thread->t_ctl_waitq);
3081 l_wait_event(thread->t_ctl_waitq,
3082 thread_is_stopped(thread),
3086 lfsck->li_start_unplug = 1;
3087 wake_up_all(&thread->t_ctl_waitq);
3093 mutex_unlock(&lfsck->li_mutex);
3096 lfsck_instance_put(env, lfsck);
3098 return rc < 0 ? rc : 0;
3100 EXPORT_SYMBOL(lfsck_start);
3102 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3103 struct lfsck_stop *stop)
3105 struct lfsck_instance *lfsck;
3106 struct ptlrpc_thread *thread;
3107 struct l_wait_info lwi = { 0 };
3112 lfsck = lfsck_instance_find(key, true, false);
3113 if (unlikely(lfsck == NULL))
3116 thread = &lfsck->li_thread;
3117 /* release lfsck::li_mutex to avoid deadlock. */
3118 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3119 if (!lfsck->li_master) {
3120 CERROR("%s: only allow to specify '-A' via MDS\n",
3121 lfsck_lfsck2name(lfsck));
3123 GOTO(out, rc = -EPERM);
3126 rc1 = lfsck_stop_all(env, lfsck, stop);
3129 mutex_lock(&lfsck->li_mutex);
3130 spin_lock(&lfsck->li_lock);
3131 /* no error if LFSCK is already stopped, or was never started */
3132 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3133 spin_unlock(&lfsck->li_lock);
3138 lfsck->li_status = stop->ls_status;
3139 lfsck->li_flags = stop->ls_flags;
3141 lfsck->li_status = LS_STOPPED;
3142 lfsck->li_flags = 0;
3145 thread_set_flags(thread, SVC_STOPPING);
3146 spin_unlock(&lfsck->li_lock);
3148 wake_up_all(&thread->t_ctl_waitq);
3149 l_wait_event(thread->t_ctl_waitq,
3150 thread_is_stopped(thread),
3156 mutex_unlock(&lfsck->li_mutex);
3157 lfsck_instance_put(env, lfsck);
3159 return rc != 0 ? rc : rc1;
3161 EXPORT_SYMBOL(lfsck_stop);
3163 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3164 struct lfsck_request *lr, struct thandle *th)
3166 int rc = -EOPNOTSUPP;
3169 switch (lr->lr_event) {
3171 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3172 struct lfsck_start_param lsp;
3174 memset(start, 0, sizeof(*start));
3175 start->ls_valid = lr->lr_valid;
3176 start->ls_speed_limit = lr->lr_speed;
3177 start->ls_version = lr->lr_version;
3178 start->ls_active = lr->lr_active;
3179 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3180 start->ls_async_windows = lr->lr_async_windows;
3182 lsp.lsp_start = start;
3183 lsp.lsp_index = lr->lr_index;
3184 lsp.lsp_index_valid = 1;
3185 rc = lfsck_start(env, key, &lsp);
3189 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3191 memset(stop, 0, sizeof(*stop));
3192 stop->ls_status = lr->lr_status;
3193 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3194 rc = lfsck_stop(env, key, stop);
3197 case LE_PHASE1_DONE:
3198 case LE_PHASE2_DONE:
3199 case LE_FID_ACCESSED:
3201 case LE_CONDITIONAL_DESTROY:
3202 case LE_SKIP_NLINK_DECLARE:
3204 case LE_SET_LMV_MASTER:
3205 case LE_SET_LMV_SLAVE:
3206 case LE_PAIRS_VERIFY: {
3207 struct lfsck_instance *lfsck;
3208 struct lfsck_component *com;
3210 lfsck = lfsck_instance_find(key, true, false);
3211 if (unlikely(lfsck == NULL))
3214 com = lfsck_component_find(lfsck, lr->lr_active);
3215 if (likely(com != NULL)) {
3216 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3217 lfsck_component_put(env, com);
3220 lfsck_instance_put(env, lfsck);
3229 EXPORT_SYMBOL(lfsck_in_notify);
3231 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3232 struct lfsck_request *lr)
3234 struct lfsck_instance *lfsck;
3235 struct lfsck_component *com;
3239 lfsck = lfsck_instance_find(key, true, false);
3240 if (unlikely(lfsck == NULL))
3243 com = lfsck_component_find(lfsck, lr->lr_active);
3244 if (likely(com != NULL)) {
3245 rc = com->lc_ops->lfsck_query(env, com);
3246 lfsck_component_put(env, com);
3251 lfsck_instance_put(env, lfsck);
3256 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3257 struct ldlm_namespace *ns)
3259 struct lfsck_instance *lfsck;
3262 lfsck = lfsck_instance_find(key, true, false);
3263 if (likely(lfsck != NULL)) {
3264 lfsck->li_namespace = ns;
3265 lfsck_instance_put(env, lfsck);
3271 EXPORT_SYMBOL(lfsck_register_namespace);
3273 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3274 struct dt_device *next, struct obd_device *obd,
3275 lfsck_out_notify notify, void *notify_data, bool master)
3277 struct lfsck_instance *lfsck;
3278 struct dt_object *root = NULL;
3279 struct dt_object *obj = NULL;
3280 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3284 lfsck = lfsck_instance_find(key, false, false);
3285 if (unlikely(lfsck != NULL))
3288 OBD_ALLOC_PTR(lfsck);
3292 mutex_init(&lfsck->li_mutex);
3293 spin_lock_init(&lfsck->li_lock);
3294 INIT_LIST_HEAD(&lfsck->li_link);
3295 INIT_LIST_HEAD(&lfsck->li_list_scan);
3296 INIT_LIST_HEAD(&lfsck->li_list_dir);
3297 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3298 INIT_LIST_HEAD(&lfsck->li_list_idle);
3299 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3300 atomic_set(&lfsck->li_ref, 1);
3301 atomic_set(&lfsck->li_double_scan_count, 0);
3302 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3303 lfsck->li_out_notify = notify;
3304 lfsck->li_out_notify_data = notify_data;
3305 lfsck->li_next = next;
3306 lfsck->li_bottom = key;
3307 lfsck->li_obd = obd;
3309 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3313 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3317 fid->f_seq = FID_SEQ_LOCAL_NAME;
3320 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3324 rc = dt_root_get(env, key, fid);
3328 root = dt_locate(env, key, fid);
3330 GOTO(out, rc = PTR_ERR(root));
3332 if (unlikely(!dt_try_as_dir(env, root)))
3333 GOTO(out, rc = -ENOTDIR);
3335 lfsck->li_local_root_fid = *fid;
3337 lfsck->li_master = 1;
3338 if (lfsck_dev_idx(lfsck) == 0) {
3339 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3340 const struct lu_name *cname;
3342 rc = dt_lookup(env, root,
3343 (struct dt_rec *)(&lfsck->li_global_root_fid),
3344 (const struct dt_key *)"ROOT");
3348 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3350 GOTO(out, rc = PTR_ERR(obj));
3352 if (unlikely(!dt_try_as_dir(env, obj)))
3353 GOTO(out, rc = -ENOTDIR);
3355 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3356 (const struct dt_key *)dotlustre);
3360 lfsck_object_put(env, obj);
3361 obj = dt_locate(env, key, fid);
3363 GOTO(out, rc = PTR_ERR(obj));
3365 cname = lfsck_name_get_const(env, dotlustre,
3367 rc = lfsck_verify_linkea(env, obj, cname,
3368 &lfsck->li_global_root_fid);
3372 if (unlikely(!dt_try_as_dir(env, obj)))
3373 GOTO(out, rc = -ENOTDIR);
3376 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3377 (const struct dt_key *)lostfound);
3381 lfsck_object_put(env, obj);
3382 obj = dt_locate(env, key, fid);
3384 GOTO(out, rc = PTR_ERR(obj));
3386 cname = lfsck_name_get_const(env, lostfound,
3388 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3392 lfsck_object_put(env, obj);
3397 fid->f_seq = FID_SEQ_LOCAL_FILE;
3398 fid->f_oid = OTABLE_IT_OID;
3400 obj = dt_locate(env, key, fid);
3402 GOTO(out, rc = PTR_ERR(obj));
3404 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3408 lfsck->li_obj_oit = obj;
3409 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3410 S_IFDIR | S_IRUGO | S_IWUSR);
3412 GOTO(out, rc = PTR_ERR(obj));
3414 lu_object_get(&obj->do_lu);
3415 lfsck->li_lfsck_dir = obj;
3416 rc = lfsck_bookmark_setup(env, lfsck);
3421 rc = lfsck_fid_init(lfsck);
3425 rc = lfsck_namespace_setup(env, lfsck);
3430 rc = lfsck_layout_setup(env, lfsck);
3434 /* XXX: more LFSCK components initialization to be added here. */
3436 rc = lfsck_instance_add(lfsck);
3438 rc = lfsck_add_target_from_orphan(env, lfsck);
3440 if (obj != NULL && !IS_ERR(obj))
3441 lfsck_object_put(env, obj);
3442 if (root != NULL && !IS_ERR(root))
3443 lfsck_object_put(env, root);
3445 lfsck_instance_cleanup(env, lfsck);
3448 EXPORT_SYMBOL(lfsck_register);
3450 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3452 struct lfsck_instance *lfsck;
3454 lfsck = lfsck_instance_find(key, false, true);
3456 lfsck_instance_put(env, lfsck);
3458 EXPORT_SYMBOL(lfsck_degister);
3460 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3461 struct dt_device *tgt, struct obd_export *exp,
3462 __u32 index, bool for_ost)
3464 struct lfsck_instance *lfsck;
3465 struct lfsck_tgt_desc *ltd;
3476 INIT_LIST_HEAD(<d->ltd_orphan_list);
3477 INIT_LIST_HEAD(<d->ltd_layout_list);
3478 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3479 INIT_LIST_HEAD(<d->ltd_namespace_list);
3480 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3481 atomic_set(<d->ltd_ref, 1);
3482 ltd->ltd_index = index;
3484 spin_lock(&lfsck_instance_lock);
3485 lfsck = __lfsck_instance_find(key, true, false);
3486 if (lfsck == NULL) {
3488 list_add_tail(<d->ltd_orphan_list,
3489 &lfsck_ost_orphan_list);
3491 list_add_tail(<d->ltd_orphan_list,
3492 &lfsck_mdt_orphan_list);
3493 spin_unlock(&lfsck_instance_lock);
3497 spin_unlock(&lfsck_instance_lock);
3499 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3503 lfsck_instance_put(env, lfsck);
3507 EXPORT_SYMBOL(lfsck_add_target);
3509 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3510 struct dt_device *tgt, __u32 index, bool for_ost)
3512 struct lfsck_instance *lfsck;
3513 struct lfsck_tgt_descs *ltds;
3514 struct lfsck_tgt_desc *ltd;
3515 struct list_head *head;
3518 head = &lfsck_ost_orphan_list;
3520 head = &lfsck_mdt_orphan_list;
3522 spin_lock(&lfsck_instance_lock);
3523 list_for_each_entry(ltd, head, ltd_orphan_list) {
3524 if (ltd->ltd_tgt == tgt) {
3525 list_del_init(<d->ltd_orphan_list);
3526 spin_unlock(&lfsck_instance_lock);
3534 lfsck = __lfsck_instance_find(key, true, false);
3535 spin_unlock(&lfsck_instance_lock);
3536 if (unlikely(lfsck == NULL))
3540 ltds = &lfsck->li_ost_descs;
3542 ltds = &lfsck->li_mdt_descs;
3544 down_write(<ds->ltd_rw_sem);
3545 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3547 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3550 ltd = lfsck_ltd2tgt(ltds, index);
3551 if (unlikely(ltd == NULL))
3554 LASSERT(ltds->ltd_tgtnr > 0);
3557 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3558 lfsck_assign_tgt(ltds, NULL, index);
3563 head = &lfsck->li_ost_descs.ltd_orphan;
3565 head = &lfsck->li_mdt_descs.ltd_orphan;
3567 list_for_each_entry(ltd, head, ltd_orphan_list) {
3568 if (ltd->ltd_tgt == tgt) {
3569 list_del_init(<d->ltd_orphan_list);
3575 up_write(<ds->ltd_rw_sem);
3577 spin_lock(<ds->ltd_lock);
3579 spin_unlock(<ds->ltd_lock);
3580 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3581 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3585 lfsck_instance_put(env, lfsck);
3587 EXPORT_SYMBOL(lfsck_del_target);
3589 static int __init lfsck_init(void)
3593 INIT_LIST_HEAD(&lfsck_instance_list);
3594 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3595 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3596 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3597 rc = lu_context_key_register(&lfsck_thread_key);
3599 tgt_register_lfsck_in_notify(lfsck_in_notify);
3600 tgt_register_lfsck_query(lfsck_query);
3606 static void __exit lfsck_exit(void)
3608 struct lfsck_tgt_desc *ltd;
3609 struct lfsck_tgt_desc *next;
3611 LASSERT(list_empty(&lfsck_instance_list));
3613 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3615 list_del_init(<d->ltd_orphan_list);
3619 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3621 list_del_init(<d->ltd_orphan_list);
3625 lu_context_key_degister(&lfsck_thread_key);
3628 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3629 MODULE_DESCRIPTION("Lustre File System Checker");
3630 MODULE_VERSION(LUSTRE_VERSION_STRING);
3631 MODULE_LICENSE("GPL");
3633 module_init(lfsck_init);
3634 module_exit(lfsck_exit);