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);
1438 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1439 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1440 struct lu_fid tfid = bk->lb_lpf_fid;
1442 /* Invalid FID record in the bookmark file, reset it. */
1443 fid_zero(&bk->lb_lpf_fid);
1444 rc = lfsck_bookmark_store(env, lfsck);
1446 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1447 " in the bookmark file: rc = %d\n",
1448 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1453 child1 = lfsck_object_find_bottom(env, lfsck,
1455 if (IS_ERR(child1)) {
1460 if (unlikely(!dt_object_exists(child1) ||
1461 dt_object_remote(child1)) ||
1462 !S_ISDIR(lfsck_object_type(child1))) {
1463 /* Invalid FID record in the bookmark file,
1465 fid_zero(&bk->lb_lpf_fid);
1466 rc = lfsck_bookmark_store(env, lfsck);
1468 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1469 " in the bookmark file: rc = %d\n",
1470 lfsck_lfsck2name(lfsck),
1471 PFID(lfsck_dto2fid(child1)), rc);
1476 lfsck_object_put(env, child1);
1478 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1479 GOTO(put, rc = -ENOTDIR);
1485 snprintf(name, 8, "MDT%04x", node);
1486 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1487 (const struct dt_key *)name);
1488 if (rc == -ENOENT) {
1489 if (!fid_is_zero(&bk->lb_lpf_fid))
1498 /* Invalid FID in the name entry, remove the name entry. */
1499 if (!fid_is_norm(cfid)) {
1500 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1507 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1509 GOTO(put, rc = PTR_ERR(child2));
1511 if (unlikely(!dt_object_exists(child2) ||
1512 dt_object_remote(child2)) ||
1513 !S_ISDIR(lfsck_object_type(child2))) {
1514 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1521 if (unlikely(!dt_try_as_dir(env, child2)))
1522 GOTO(put, rc = -ENOTDIR);
1524 if (child1 == NULL) {
1525 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1526 pfid, LVLT_BY_NAMEENTRY);
1527 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1528 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1529 pfid, LVLT_BY_BOOKMARK);
1530 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1531 rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1535 if (lfsck->li_lpf_obj == NULL) {
1536 lu_object_get(&child2->do_lu);
1537 lfsck->li_lpf_obj = child2;
1540 cname = lfsck_name_get_const(env, name, strlen(name));
1541 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1548 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1549 pfid, LVLT_BY_BOOKMARK);
1554 if (lfsck->li_lpf_obj != NULL) {
1555 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1556 lfsck_object_put(env, lfsck->li_lpf_obj);
1557 lfsck->li_lpf_obj = NULL;
1560 } else if (rc == 0) {
1561 rc = lfsck_create_lpf(env, lfsck);
1564 if (child2 != NULL && !IS_ERR(child2))
1565 lfsck_object_put(env, child2);
1566 if (child1 != NULL && !IS_ERR(child1))
1567 lfsck_object_put(env, child1);
1572 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1574 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1575 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1580 if (unlikely(ss == NULL))
1583 OBD_ALLOC_PTR(lfsck->li_seq);
1584 if (lfsck->li_seq == NULL)
1587 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1589 GOTO(out, rc = -ENOMEM);
1591 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1592 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1594 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1598 if (fid_is_sane(&bk->lb_last_fid))
1599 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1604 OBD_FREE_PTR(lfsck->li_seq);
1605 lfsck->li_seq = NULL;
1610 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1612 if (lfsck->li_seq != NULL) {
1613 seq_client_fini(lfsck->li_seq);
1614 OBD_FREE_PTR(lfsck->li_seq);
1615 lfsck->li_seq = NULL;
1619 void lfsck_instance_cleanup(const struct lu_env *env,
1620 struct lfsck_instance *lfsck)
1622 struct ptlrpc_thread *thread = &lfsck->li_thread;
1623 struct lfsck_component *com;
1624 struct lfsck_component *next;
1625 struct lfsck_lmv_unit *llu;
1626 struct lfsck_lmv_unit *llu_next;
1627 struct lfsck_lmv *llmv;
1630 LASSERT(list_empty(&lfsck->li_link));
1631 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1633 if (lfsck->li_obj_oit != NULL) {
1634 lfsck_object_put(env, lfsck->li_obj_oit);
1635 lfsck->li_obj_oit = NULL;
1638 LASSERT(lfsck->li_obj_dir == NULL);
1639 LASSERT(lfsck->li_lmv == NULL);
1641 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1642 llmv = &llu->llu_lmv;
1644 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1645 "still in using: %u\n",
1646 atomic_read(&llmv->ll_ref));
1648 lfsck_lmv_put(env, llmv);
1651 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1652 lfsck_component_cleanup(env, com);
1655 LASSERT(list_empty(&lfsck->li_list_dir));
1657 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1659 lfsck_component_cleanup(env, com);
1662 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1663 lfsck_component_cleanup(env, com);
1666 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1667 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1669 if (lfsck->li_lfsck_dir != NULL) {
1670 lfsck_object_put(env, lfsck->li_lfsck_dir);
1671 lfsck->li_lfsck_dir = NULL;
1674 if (lfsck->li_bookmark_obj != NULL) {
1675 lfsck_object_put(env, lfsck->li_bookmark_obj);
1676 lfsck->li_bookmark_obj = NULL;
1679 if (lfsck->li_lpf_obj != NULL) {
1680 lfsck_object_put(env, lfsck->li_lpf_obj);
1681 lfsck->li_lpf_obj = NULL;
1684 if (lfsck->li_lpf_root_obj != NULL) {
1685 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1686 lfsck->li_lpf_root_obj = NULL;
1689 if (lfsck->li_los != NULL) {
1690 local_oid_storage_fini(env, lfsck->li_los);
1691 lfsck->li_los = NULL;
1694 lfsck_fid_fini(lfsck);
1696 OBD_FREE_PTR(lfsck);
1699 static inline struct lfsck_instance *
1700 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1702 struct lfsck_instance *lfsck;
1704 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1705 if (lfsck->li_bottom == key) {
1707 lfsck_instance_get(lfsck);
1709 list_del_init(&lfsck->li_link);
1718 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1721 struct lfsck_instance *lfsck;
1723 spin_lock(&lfsck_instance_lock);
1724 lfsck = __lfsck_instance_find(key, ref, unlink);
1725 spin_unlock(&lfsck_instance_lock);
1730 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1732 struct lfsck_instance *tmp;
1734 spin_lock(&lfsck_instance_lock);
1735 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1736 if (lfsck->li_bottom == tmp->li_bottom) {
1737 spin_unlock(&lfsck_instance_lock);
1742 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1743 spin_unlock(&lfsck_instance_lock);
1747 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1752 bool newline = (bits != 0 ? false : true);
1755 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1759 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1762 if (names[i] != NULL) {
1766 rc = seq_printf(m, "%s%c", names[i],
1767 newline ? '\n' : ',');
1775 rc = seq_printf(m, "\n");
1780 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1785 rc = seq_printf(m, "%s_time: N/A\n", name);
1787 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1792 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1794 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1795 name, cfs_time_current_sec() - time);
1800 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1803 if (fid_is_zero(&pos->lp_dir_parent)) {
1804 if (pos->lp_oit_cookie == 0)
1805 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1807 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1808 prefix, pos->lp_oit_cookie);
1811 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1812 prefix, pos->lp_oit_cookie,
1813 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1816 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1817 struct lfsck_position *pos, bool init)
1819 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1821 if (unlikely(lfsck->li_di_oit == NULL)) {
1822 memset(pos, 0, sizeof(*pos));
1826 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1827 if (!lfsck->li_current_oit_processed && !init)
1828 pos->lp_oit_cookie--;
1830 LASSERT(pos->lp_oit_cookie > 0);
1832 if (lfsck->li_di_dir != NULL) {
1833 struct dt_object *dto = lfsck->li_obj_dir;
1835 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1838 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1839 fid_zero(&pos->lp_dir_parent);
1840 pos->lp_dir_cookie = 0;
1842 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1845 fid_zero(&pos->lp_dir_parent);
1846 pos->lp_dir_cookie = 0;
1850 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1854 if (limit != LFSCK_SPEED_NO_LIMIT) {
1855 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1856 lfsck->li_sleep_rate = limit /
1857 msecs_to_jiffies(MSEC_PER_SEC);
1858 lfsck->li_sleep_jif = 1;
1860 lfsck->li_sleep_rate = 1;
1861 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1865 lfsck->li_sleep_jif = 0;
1866 lfsck->li_sleep_rate = 0;
1869 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1870 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1877 void lfsck_control_speed(struct lfsck_instance *lfsck)
1879 struct ptlrpc_thread *thread = &lfsck->li_thread;
1880 struct l_wait_info lwi;
1882 if (lfsck->li_sleep_jif > 0 &&
1883 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1884 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1885 LWI_ON_SIGNAL_NOOP, NULL);
1887 l_wait_event(thread->t_ctl_waitq,
1888 !thread_is_running(thread),
1890 lfsck->li_new_scanned = 0;
1894 void lfsck_control_speed_by_self(struct lfsck_component *com)
1896 struct lfsck_instance *lfsck = com->lc_lfsck;
1897 struct ptlrpc_thread *thread = &lfsck->li_thread;
1898 struct l_wait_info lwi;
1900 if (lfsck->li_sleep_jif > 0 &&
1901 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1902 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1903 LWI_ON_SIGNAL_NOOP, NULL);
1905 l_wait_event(thread->t_ctl_waitq,
1906 !thread_is_running(thread),
1908 com->lc_new_scanned = 0;
1912 static struct lfsck_thread_args *
1913 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1914 struct lfsck_component *com,
1915 struct lfsck_start_param *lsp)
1917 struct lfsck_thread_args *lta;
1922 return ERR_PTR(-ENOMEM);
1924 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1930 lta->lta_lfsck = lfsck_instance_get(lfsck);
1932 lta->lta_com = lfsck_component_get(com);
1939 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1941 if (lta->lta_com != NULL)
1942 lfsck_component_put(<a->lta_env, lta->lta_com);
1943 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1944 lu_env_fini(<a->lta_env);
1948 struct lfsck_assistant_data *
1949 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1952 struct lfsck_assistant_data *lad;
1956 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1957 if (lad->lad_bitmap == NULL) {
1962 INIT_LIST_HEAD(&lad->lad_req_list);
1963 spin_lock_init(&lad->lad_lock);
1964 INIT_LIST_HEAD(&lad->lad_ost_list);
1965 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1966 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1967 INIT_LIST_HEAD(&lad->lad_mdt_list);
1968 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1969 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1970 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1972 lad->lad_name = name;
1978 struct lfsck_assistant_object *
1979 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1980 const struct lu_attr *attr, __u64 cookie,
1983 struct lfsck_assistant_object *lso;
1987 return ERR_PTR(-ENOMEM);
1989 lso->lso_fid = *fid;
1991 lso->lso_attr = *attr;
1993 atomic_set(&lso->lso_ref, 1);
1994 lso->lso_oit_cookie = cookie;
1996 lso->lso_is_dir = 1;
2002 lfsck_assistant_object_load(const struct lu_env *env,
2003 struct lfsck_instance *lfsck,
2004 struct lfsck_assistant_object *lso)
2006 struct dt_object *obj;
2008 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2012 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2014 lfsck_object_put(env, obj);
2016 return ERR_PTR(-ENOENT);
2019 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2020 lfsck_object_put(env, obj);
2022 return ERR_PTR(-ENOTDIR);
2029 * Generic LFSCK asynchronous communication interpretor function.
2030 * The LFSCK RPC reply for both the event notification and status
2031 * querying will be handled here.
2033 * \param[in] env pointer to the thread context
2034 * \param[in] req pointer to the LFSCK request
2035 * \param[in] args pointer to the lfsck_async_interpret_args
2036 * \param[in] rc the result for handling the LFSCK request
2038 * \retval 0 for success
2039 * \retval negative error number on failure
2041 int lfsck_async_interpret_common(const struct lu_env *env,
2042 struct ptlrpc_request *req,
2045 struct lfsck_async_interpret_args *laia = args;
2046 struct lfsck_component *com = laia->laia_com;
2047 struct lfsck_assistant_data *lad = com->lc_data;
2048 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2049 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2050 struct lfsck_request *lr = laia->laia_lr;
2052 LASSERT(com->lc_lfsck->li_master);
2054 switch (lr->lr_event) {
2057 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2059 lfsck_lfsck2name(com->lc_lfsck),
2060 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2061 ltd->ltd_index, lad->lad_name, rc);
2063 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2064 struct lfsck_layout *lo = com->lc_file_ram;
2066 if (lr->lr_flags & LEF_TO_OST)
2067 lfsck_lad_set_bitmap(env, com,
2070 lo->ll_flags |= LF_INCOMPLETE;
2072 struct lfsck_namespace *ns = com->lc_file_ram;
2074 /* If some MDT does not join the namespace
2075 * LFSCK, then we cannot know whether there
2076 * is some name entry on such MDT that with
2077 * the referenced MDT-object on this MDT or
2078 * not. So the namespace LFSCK on this MDT
2079 * cannot handle orphan MDT-objects properly.
2080 * So we mark the LFSCK as LF_INCOMPLETE and
2081 * skip orphan MDT-objects handling. */
2082 ns->ln_flags |= LF_INCOMPLETE;
2087 spin_lock(<ds->ltd_lock);
2088 if (ltd->ltd_dead) {
2089 spin_unlock(<ds->ltd_lock);
2093 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2094 struct list_head *list;
2095 struct list_head *phase_list;
2097 if (ltd->ltd_layout_done) {
2098 spin_unlock(<ds->ltd_lock);
2102 if (lr->lr_flags & LEF_TO_OST) {
2103 list = &lad->lad_ost_list;
2104 phase_list = &lad->lad_ost_phase1_list;
2106 list = &lad->lad_mdt_list;
2107 phase_list = &lad->lad_mdt_phase1_list;
2110 if (list_empty(<d->ltd_layout_list))
2111 list_add_tail(<d->ltd_layout_list, list);
2112 if (list_empty(<d->ltd_layout_phase_list))
2113 list_add_tail(<d->ltd_layout_phase_list,
2116 if (ltd->ltd_namespace_done) {
2117 spin_unlock(<ds->ltd_lock);
2121 if (list_empty(<d->ltd_namespace_list))
2122 list_add_tail(<d->ltd_namespace_list,
2123 &lad->lad_mdt_list);
2124 if (list_empty(<d->ltd_namespace_phase_list))
2125 list_add_tail(<d->ltd_namespace_phase_list,
2126 &lad->lad_mdt_phase1_list);
2128 spin_unlock(<ds->ltd_lock);
2131 case LE_PHASE1_DONE:
2132 case LE_PHASE2_DONE:
2134 if (rc != 0 && rc != -EALREADY)
2135 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2136 "event = %d, rc = %d\n",
2137 lfsck_lfsck2name(com->lc_lfsck),
2138 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2139 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2142 struct lfsck_reply *reply;
2143 struct list_head *list;
2144 struct list_head *phase_list;
2146 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2147 list = <d->ltd_layout_list;
2148 phase_list = <d->ltd_layout_phase_list;
2150 list = <d->ltd_namespace_list;
2151 phase_list = <d->ltd_namespace_phase_list;
2155 spin_lock(<ds->ltd_lock);
2156 list_del_init(phase_list);
2157 list_del_init(list);
2158 spin_unlock(<ds->ltd_lock);
2162 reply = req_capsule_server_get(&req->rq_pill,
2164 if (reply == NULL) {
2166 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2167 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2169 spin_lock(<ds->ltd_lock);
2170 list_del_init(phase_list);
2171 list_del_init(list);
2172 spin_unlock(<ds->ltd_lock);
2176 switch (reply->lr_status) {
2177 case LS_SCANNING_PHASE1:
2179 case LS_SCANNING_PHASE2:
2180 spin_lock(<ds->ltd_lock);
2181 list_del_init(phase_list);
2182 if (ltd->ltd_dead) {
2183 spin_unlock(<ds->ltd_lock);
2187 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2188 if (ltd->ltd_layout_done) {
2189 spin_unlock(<ds->ltd_lock);
2193 if (lr->lr_flags & LEF_TO_OST)
2194 list_add_tail(phase_list,
2195 &lad->lad_ost_phase2_list);
2197 list_add_tail(phase_list,
2198 &lad->lad_mdt_phase2_list);
2200 if (ltd->ltd_namespace_done) {
2201 spin_unlock(<ds->ltd_lock);
2205 list_add_tail(phase_list,
2206 &lad->lad_mdt_phase2_list);
2208 spin_unlock(<ds->ltd_lock);
2211 spin_lock(<ds->ltd_lock);
2212 list_del_init(phase_list);
2213 list_del_init(list);
2214 spin_unlock(<ds->ltd_lock);
2220 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2221 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2225 if (!laia->laia_shared) {
2227 lfsck_component_put(env, com);
2233 static void lfsck_interpret(const struct lu_env *env,
2234 struct lfsck_instance *lfsck,
2235 struct ptlrpc_request *req, void *args, int result)
2237 struct lfsck_async_interpret_args *laia = args;
2238 struct lfsck_component *com;
2240 LASSERT(laia->laia_com == NULL);
2241 LASSERT(laia->laia_shared);
2243 spin_lock(&lfsck->li_lock);
2244 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2245 laia->laia_com = com;
2246 lfsck_async_interpret_common(env, req, laia, result);
2249 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2250 laia->laia_com = com;
2251 lfsck_async_interpret_common(env, req, laia, result);
2253 spin_unlock(&lfsck->li_lock);
2256 static int lfsck_stop_notify(const struct lu_env *env,
2257 struct lfsck_instance *lfsck,
2258 struct lfsck_tgt_descs *ltds,
2259 struct lfsck_tgt_desc *ltd, __u16 type)
2261 struct lfsck_component *com;
2265 LASSERT(lfsck->li_master);
2267 spin_lock(&lfsck->li_lock);
2268 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2270 com = __lfsck_component_find(lfsck, type,
2271 &lfsck->li_list_double_scan);
2273 lfsck_component_get(com);
2274 spin_unlock(&lfsck->li_lock);
2277 struct lfsck_thread_info *info = lfsck_env_info(env);
2278 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2279 struct lfsck_request *lr = &info->lti_lr;
2280 struct lfsck_assistant_data *lad = com->lc_data;
2281 struct list_head *list;
2282 struct list_head *phase_list;
2283 struct ptlrpc_request_set *set;
2285 set = ptlrpc_prep_set();
2287 lfsck_component_put(env, com);
2292 if (type == LFSCK_TYPE_LAYOUT) {
2293 list = <d->ltd_layout_list;
2294 phase_list = <d->ltd_layout_phase_list;
2296 list = <d->ltd_namespace_list;
2297 phase_list = <d->ltd_namespace_phase_list;
2300 spin_lock(<ds->ltd_lock);
2301 if (list_empty(list)) {
2302 LASSERT(list_empty(phase_list));
2303 spin_unlock(<ds->ltd_lock);
2304 ptlrpc_set_destroy(set);
2309 list_del_init(phase_list);
2310 list_del_init(list);
2311 spin_unlock(<ds->ltd_lock);
2313 memset(lr, 0, sizeof(*lr));
2314 lr->lr_index = lfsck_dev_idx(lfsck);
2315 lr->lr_event = LE_PEER_EXIT;
2316 lr->lr_active = type;
2317 lr->lr_status = LS_CO_PAUSED;
2318 if (ltds == &lfsck->li_ost_descs)
2319 lr->lr_flags = LEF_TO_OST;
2321 laia->laia_com = com;
2322 laia->laia_ltds = ltds;
2323 atomic_inc(<d->ltd_ref);
2324 laia->laia_ltd = ltd;
2326 laia->laia_shared = 0;
2328 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2329 lfsck_async_interpret_common,
2330 laia, LFSCK_NOTIFY);
2332 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2333 "co-stop for %s: rc = %d\n",
2334 lfsck_lfsck2name(lfsck),
2335 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2336 ltd->ltd_index, lad->lad_name, rc);
2339 rc = ptlrpc_set_wait(set);
2342 ptlrpc_set_destroy(set);
2343 lfsck_component_put(env, com);
2349 static int lfsck_async_interpret(const struct lu_env *env,
2350 struct ptlrpc_request *req,
2353 struct lfsck_async_interpret_args *laia = args;
2354 struct lfsck_instance *lfsck;
2356 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2358 lfsck_interpret(env, lfsck, req, laia, rc);
2359 lfsck_tgt_put(laia->laia_ltd);
2360 if (rc != 0 && laia->laia_result != -EALREADY)
2361 laia->laia_result = rc;
2366 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2367 struct lfsck_request *lr,
2368 struct ptlrpc_request_set *set,
2369 ptlrpc_interpterer_t interpreter,
2370 void *args, int request)
2372 struct lfsck_async_interpret_args *laia;
2373 struct ptlrpc_request *req;
2374 struct lfsck_request *tmp;
2375 struct req_format *format;
2380 format = &RQF_LFSCK_NOTIFY;
2383 format = &RQF_LFSCK_QUERY;
2386 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2387 exp->exp_obd->obd_name, request, -EINVAL);
2391 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2395 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2397 ptlrpc_request_free(req);
2402 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2404 ptlrpc_request_set_replen(req);
2406 laia = ptlrpc_req_async_args(req);
2407 *laia = *(struct lfsck_async_interpret_args *)args;
2408 if (laia->laia_com != NULL)
2409 lfsck_component_get(laia->laia_com);
2410 req->rq_interpret_reply = interpreter;
2411 ptlrpc_set_add_req(set, req);
2416 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2417 struct lfsck_start_param *lsp)
2419 struct lfsck_instance *lfsck = com->lc_lfsck;
2420 struct lfsck_assistant_data *lad = com->lc_data;
2421 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2422 struct ptlrpc_thread *athread = &lad->lad_thread;
2423 struct lfsck_thread_args *lta;
2424 struct task_struct *task;
2428 lad->lad_assistant_status = 0;
2429 lad->lad_post_result = 0;
2430 lad->lad_to_post = 0;
2431 lad->lad_to_double_scan = 0;
2432 lad->lad_in_double_scan = 0;
2434 lad->lad_advance_lock = false;
2435 thread_set_flags(athread, 0);
2437 lta = lfsck_thread_args_init(lfsck, com, lsp);
2439 RETURN(PTR_ERR(lta));
2441 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2444 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2445 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2446 lfsck_thread_args_fini(lta);
2448 struct l_wait_info lwi = { 0 };
2450 l_wait_event(mthread->t_ctl_waitq,
2451 thread_is_running(athread) ||
2452 thread_is_stopped(athread),
2454 if (unlikely(!thread_is_running(athread)))
2455 rc = lad->lad_assistant_status;
2463 int lfsck_checkpoint_generic(const struct lu_env *env,
2464 struct lfsck_component *com)
2466 struct lfsck_assistant_data *lad = com->lc_data;
2467 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2468 struct ptlrpc_thread *athread = &lad->lad_thread;
2469 struct l_wait_info lwi = { 0 };
2471 l_wait_event(mthread->t_ctl_waitq,
2472 list_empty(&lad->lad_req_list) ||
2473 !thread_is_running(mthread) ||
2474 thread_is_stopped(athread),
2477 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2478 return LFSCK_CHECKPOINT_SKIP;
2483 void lfsck_post_generic(const struct lu_env *env,
2484 struct lfsck_component *com, int *result)
2486 struct lfsck_assistant_data *lad = com->lc_data;
2487 struct ptlrpc_thread *athread = &lad->lad_thread;
2488 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2489 struct l_wait_info lwi = { 0 };
2491 lad->lad_post_result = *result;
2494 lad->lad_to_post = 1;
2496 wake_up_all(&athread->t_ctl_waitq);
2497 l_wait_event(mthread->t_ctl_waitq,
2498 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2499 thread_is_stopped(athread),
2502 if (lad->lad_assistant_status < 0)
2503 *result = lad->lad_assistant_status;
2506 int lfsck_double_scan_generic(const struct lu_env *env,
2507 struct lfsck_component *com, int status)
2509 struct lfsck_assistant_data *lad = com->lc_data;
2510 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2511 struct ptlrpc_thread *athread = &lad->lad_thread;
2512 struct l_wait_info lwi = { 0 };
2514 if (status != LS_SCANNING_PHASE2)
2517 lad->lad_to_double_scan = 1;
2519 wake_up_all(&athread->t_ctl_waitq);
2520 l_wait_event(mthread->t_ctl_waitq,
2521 lad->lad_in_double_scan ||
2522 thread_is_stopped(athread),
2525 if (lad->lad_assistant_status < 0)
2526 return lad->lad_assistant_status;
2531 void lfsck_quit_generic(const struct lu_env *env,
2532 struct lfsck_component *com)
2534 struct lfsck_assistant_data *lad = com->lc_data;
2535 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2536 struct ptlrpc_thread *athread = &lad->lad_thread;
2537 struct l_wait_info lwi = { 0 };
2540 wake_up_all(&athread->t_ctl_waitq);
2541 l_wait_event(mthread->t_ctl_waitq,
2542 thread_is_init(athread) ||
2543 thread_is_stopped(athread),
2547 /* external interfaces */
2549 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2552 struct lfsck_instance *lfsck;
2556 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2560 lfsck = lfsck_instance_find(key, true, false);
2561 if (likely(lfsck != NULL)) {
2562 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2563 lfsck_instance_put(&env, lfsck);
2572 EXPORT_SYMBOL(lfsck_get_speed);
2574 int lfsck_set_speed(struct dt_device *key, int val)
2577 struct lfsck_instance *lfsck;
2581 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2585 lfsck = lfsck_instance_find(key, true, false);
2586 if (likely(lfsck != NULL)) {
2587 mutex_lock(&lfsck->li_mutex);
2588 if (__lfsck_set_speed(lfsck, val))
2589 rc = lfsck_bookmark_store(&env, lfsck);
2590 mutex_unlock(&lfsck->li_mutex);
2591 lfsck_instance_put(&env, lfsck);
2600 EXPORT_SYMBOL(lfsck_set_speed);
2602 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2605 struct lfsck_instance *lfsck;
2609 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2613 lfsck = lfsck_instance_find(key, true, false);
2614 if (likely(lfsck != NULL)) {
2615 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2616 lfsck_instance_put(&env, lfsck);
2625 EXPORT_SYMBOL(lfsck_get_windows);
2627 int lfsck_set_windows(struct dt_device *key, int val)
2630 struct lfsck_instance *lfsck;
2634 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2638 lfsck = lfsck_instance_find(key, true, false);
2639 if (likely(lfsck != NULL)) {
2640 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2641 CWARN("%s: invalid async windows size that may "
2642 "cause memory issues. The valid range is "
2644 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2646 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2647 mutex_lock(&lfsck->li_mutex);
2648 lfsck->li_bookmark_ram.lb_async_windows = val;
2649 rc = lfsck_bookmark_store(&env, lfsck);
2650 mutex_unlock(&lfsck->li_mutex);
2652 lfsck_instance_put(&env, lfsck);
2661 EXPORT_SYMBOL(lfsck_set_windows);
2663 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2666 struct lfsck_instance *lfsck;
2667 struct lfsck_component *com;
2671 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2675 lfsck = lfsck_instance_find(key, true, false);
2676 if (likely(lfsck != NULL)) {
2677 com = lfsck_component_find(lfsck, type);
2678 if (likely(com != NULL)) {
2679 rc = com->lc_ops->lfsck_dump(&env, com, m);
2680 lfsck_component_put(&env, com);
2685 lfsck_instance_put(&env, lfsck);
2694 EXPORT_SYMBOL(lfsck_dump);
2696 static int lfsck_stop_all(const struct lu_env *env,
2697 struct lfsck_instance *lfsck,
2698 struct lfsck_stop *stop)
2700 struct lfsck_thread_info *info = lfsck_env_info(env);
2701 struct lfsck_request *lr = &info->lti_lr;
2702 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2703 struct ptlrpc_request_set *set;
2704 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2705 struct lfsck_tgt_desc *ltd;
2706 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2712 LASSERT(stop->ls_flags & LPF_BROADCAST);
2714 set = ptlrpc_prep_set();
2715 if (unlikely(set == NULL))
2718 memset(lr, 0, sizeof(*lr));
2719 lr->lr_event = LE_STOP;
2720 lr->lr_index = lfsck_dev_idx(lfsck);
2721 lr->lr_status = stop->ls_status;
2722 lr->lr_version = bk->lb_version;
2723 lr->lr_active = LFSCK_TYPES_ALL;
2724 lr->lr_param = stop->ls_flags;
2726 laia->laia_com = NULL;
2727 laia->laia_ltds = ltds;
2729 laia->laia_result = 0;
2730 laia->laia_shared = 1;
2732 down_read(<ds->ltd_rw_sem);
2733 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2734 ltd = lfsck_tgt_get(ltds, idx);
2735 LASSERT(ltd != NULL);
2737 laia->laia_ltd = ltd;
2738 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2739 lfsck_async_interpret, laia,
2742 lfsck_interpret(env, lfsck, NULL, laia, rc);
2744 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2745 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2749 up_read(<ds->ltd_rw_sem);
2751 rc = ptlrpc_set_wait(set);
2752 ptlrpc_set_destroy(set);
2755 rc = laia->laia_result;
2757 if (rc == -EALREADY)
2761 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2762 lfsck_lfsck2name(lfsck), rc);
2764 RETURN(rc != 0 ? rc : rc1);
2767 static int lfsck_start_all(const struct lu_env *env,
2768 struct lfsck_instance *lfsck,
2769 struct lfsck_start *start)
2771 struct lfsck_thread_info *info = lfsck_env_info(env);
2772 struct lfsck_request *lr = &info->lti_lr;
2773 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2774 struct ptlrpc_request_set *set;
2775 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2776 struct lfsck_tgt_desc *ltd;
2777 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2782 LASSERT(start->ls_flags & LPF_BROADCAST);
2784 set = ptlrpc_prep_set();
2785 if (unlikely(set == NULL))
2788 memset(lr, 0, sizeof(*lr));
2789 lr->lr_event = LE_START;
2790 lr->lr_index = lfsck_dev_idx(lfsck);
2791 lr->lr_speed = bk->lb_speed_limit;
2792 lr->lr_version = bk->lb_version;
2793 lr->lr_active = start->ls_active;
2794 lr->lr_param = start->ls_flags;
2795 lr->lr_async_windows = bk->lb_async_windows;
2796 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2797 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2800 laia->laia_com = NULL;
2801 laia->laia_ltds = ltds;
2803 laia->laia_result = 0;
2804 laia->laia_shared = 1;
2806 down_read(<ds->ltd_rw_sem);
2807 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2808 ltd = lfsck_tgt_get(ltds, idx);
2809 LASSERT(ltd != NULL);
2811 laia->laia_ltd = ltd;
2812 ltd->ltd_layout_done = 0;
2813 ltd->ltd_namespace_done = 0;
2814 ltd->ltd_synced_failures = 0;
2815 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2816 lfsck_async_interpret, laia,
2819 lfsck_interpret(env, lfsck, NULL, laia, rc);
2821 CERROR("%s: cannot notify MDT %x for LFSCK "
2822 "start, failout: rc = %d\n",
2823 lfsck_lfsck2name(lfsck), idx, rc);
2827 up_read(<ds->ltd_rw_sem);
2830 ptlrpc_set_destroy(set);
2835 rc = ptlrpc_set_wait(set);
2836 ptlrpc_set_destroy(set);
2839 rc = laia->laia_result;
2842 struct lfsck_stop *stop = &info->lti_stop;
2844 CERROR("%s: cannot start LFSCK on some MDTs, "
2845 "stop all: rc = %d\n",
2846 lfsck_lfsck2name(lfsck), rc);
2847 if (rc != -EALREADY) {
2848 stop->ls_status = LS_FAILED;
2849 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2850 lfsck_stop_all(env, lfsck, stop);
2857 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2858 struct lfsck_start_param *lsp)
2860 struct lfsck_start *start = lsp->lsp_start;
2861 struct lfsck_instance *lfsck;
2862 struct lfsck_bookmark *bk;
2863 struct ptlrpc_thread *thread;
2864 struct lfsck_component *com;
2865 struct l_wait_info lwi = { 0 };
2866 struct lfsck_thread_args *lta;
2867 struct task_struct *task;
2874 lfsck = lfsck_instance_find(key, true, false);
2875 if (unlikely(lfsck == NULL))
2878 /* System is not ready, try again later. */
2879 if (unlikely(lfsck->li_namespace == NULL))
2880 GOTO(put, rc = -EAGAIN);
2882 /* start == NULL means auto trigger paused LFSCK. */
2883 if ((start == NULL) &&
2884 (list_empty(&lfsck->li_list_scan) ||
2885 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2888 bk = &lfsck->li_bookmark_ram;
2889 thread = &lfsck->li_thread;
2890 mutex_lock(&lfsck->li_mutex);
2891 spin_lock(&lfsck->li_lock);
2892 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2894 if (unlikely(start == NULL)) {
2895 spin_unlock(&lfsck->li_lock);
2899 while (start->ls_active != 0) {
2900 if (!(type & start->ls_active)) {
2905 com = __lfsck_component_find(lfsck, type,
2906 &lfsck->li_list_scan);
2908 com = __lfsck_component_find(lfsck, type,
2909 &lfsck->li_list_double_scan);
2915 if (com->lc_ops->lfsck_join != NULL) {
2916 rc = com->lc_ops->lfsck_join( env, com, lsp);
2917 if (rc != 0 && rc != -EALREADY)
2920 start->ls_active &= ~type;
2923 spin_unlock(&lfsck->li_lock);
2926 spin_unlock(&lfsck->li_lock);
2928 lfsck->li_status = 0;
2929 lfsck->li_oit_over = 0;
2930 lfsck->li_start_unplug = 0;
2931 lfsck->li_drop_dryrun = 0;
2932 lfsck->li_new_scanned = 0;
2934 /* For auto trigger. */
2938 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2939 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2940 lfsck_lfsck2name(lfsck));
2942 GOTO(out, rc = -EPERM);
2945 start->ls_version = bk->lb_version;
2947 if (start->ls_active != 0) {
2948 struct lfsck_component *next;
2950 if (start->ls_active == LFSCK_TYPES_ALL)
2951 start->ls_active = LFSCK_TYPES_SUPPORTED;
2953 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2954 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2955 GOTO(out, rc = -ENOTSUPP);
2958 list_for_each_entry_safe(com, next,
2959 &lfsck->li_list_scan, lc_link) {
2960 if (!(com->lc_type & start->ls_active)) {
2961 rc = com->lc_ops->lfsck_post(env, com, 0,
2968 while (start->ls_active != 0) {
2969 if (type & start->ls_active) {
2970 com = __lfsck_component_find(lfsck, type,
2971 &lfsck->li_list_idle);
2973 /* The component status will be updated
2974 * when its prep() is called later by
2975 * the LFSCK main engine. */
2976 list_move_tail(&com->lc_link,
2977 &lfsck->li_list_scan);
2978 start->ls_active &= ~type;
2984 if (list_empty(&lfsck->li_list_scan)) {
2985 /* The speed limit will be used to control both the LFSCK and
2986 * low layer scrub (if applied), need to be handled firstly. */
2987 if (start->ls_valid & LSV_SPEED_LIMIT) {
2988 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2989 rc = lfsck_bookmark_store(env, lfsck);
2998 if (start->ls_flags & LPF_RESET)
2999 flags |= DOIF_RESET;
3001 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3005 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3006 start->ls_active |= com->lc_type;
3007 if (flags & DOIF_RESET) {
3008 rc = com->lc_ops->lfsck_reset(env, com, false);
3015 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3016 if (bk->lb_param & LPF_DRYRUN)
3017 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3019 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3020 valid |= DOIV_ERROR_HANDLE;
3021 if (start->ls_flags & LPF_FAILOUT)
3022 flags |= DOIF_FAILOUT;
3025 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3026 valid |= DOIV_DRYRUN;
3027 if (start->ls_flags & LPF_DRYRUN)
3028 flags |= DOIF_DRYRUN;
3031 if (!list_empty(&lfsck->li_list_scan))
3032 flags |= DOIF_OUTUSED;
3034 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3035 thread_set_flags(thread, 0);
3036 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3038 GOTO(out, rc = PTR_ERR(lta));
3040 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3041 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3044 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3045 lfsck_lfsck2name(lfsck), rc);
3046 lfsck_thread_args_fini(lta);
3051 l_wait_event(thread->t_ctl_waitq,
3052 thread_is_running(thread) ||
3053 thread_is_stopped(thread),
3055 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3056 lfsck->li_start_unplug = 1;
3057 wake_up_all(&thread->t_ctl_waitq);
3062 /* release lfsck::li_mutex to avoid deadlock. */
3063 mutex_unlock(&lfsck->li_mutex);
3064 rc = lfsck_start_all(env, lfsck, start);
3066 spin_lock(&lfsck->li_lock);
3067 if (thread_is_stopped(thread)) {
3068 spin_unlock(&lfsck->li_lock);
3070 lfsck->li_status = LS_FAILED;
3071 lfsck->li_flags = 0;
3072 thread_set_flags(thread, SVC_STOPPING);
3073 spin_unlock(&lfsck->li_lock);
3075 lfsck->li_start_unplug = 1;
3076 wake_up_all(&thread->t_ctl_waitq);
3077 l_wait_event(thread->t_ctl_waitq,
3078 thread_is_stopped(thread),
3082 lfsck->li_start_unplug = 1;
3083 wake_up_all(&thread->t_ctl_waitq);
3089 mutex_unlock(&lfsck->li_mutex);
3092 lfsck_instance_put(env, lfsck);
3094 return rc < 0 ? rc : 0;
3096 EXPORT_SYMBOL(lfsck_start);
3098 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3099 struct lfsck_stop *stop)
3101 struct lfsck_instance *lfsck;
3102 struct ptlrpc_thread *thread;
3103 struct l_wait_info lwi = { 0 };
3108 lfsck = lfsck_instance_find(key, true, false);
3109 if (unlikely(lfsck == NULL))
3112 thread = &lfsck->li_thread;
3113 /* release lfsck::li_mutex to avoid deadlock. */
3114 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3115 if (!lfsck->li_master) {
3116 CERROR("%s: only allow to specify '-A' via MDS\n",
3117 lfsck_lfsck2name(lfsck));
3119 GOTO(out, rc = -EPERM);
3122 rc1 = lfsck_stop_all(env, lfsck, stop);
3125 mutex_lock(&lfsck->li_mutex);
3126 spin_lock(&lfsck->li_lock);
3127 /* no error if LFSCK is already stopped, or was never started */
3128 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3129 spin_unlock(&lfsck->li_lock);
3134 lfsck->li_status = stop->ls_status;
3135 lfsck->li_flags = stop->ls_flags;
3137 lfsck->li_status = LS_STOPPED;
3138 lfsck->li_flags = 0;
3141 thread_set_flags(thread, SVC_STOPPING);
3142 spin_unlock(&lfsck->li_lock);
3144 wake_up_all(&thread->t_ctl_waitq);
3145 l_wait_event(thread->t_ctl_waitq,
3146 thread_is_stopped(thread),
3152 mutex_unlock(&lfsck->li_mutex);
3153 lfsck_instance_put(env, lfsck);
3155 return rc != 0 ? rc : rc1;
3157 EXPORT_SYMBOL(lfsck_stop);
3159 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3160 struct lfsck_request *lr, struct thandle *th)
3162 int rc = -EOPNOTSUPP;
3165 switch (lr->lr_event) {
3167 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3168 struct lfsck_start_param lsp;
3170 memset(start, 0, sizeof(*start));
3171 start->ls_valid = lr->lr_valid;
3172 start->ls_speed_limit = lr->lr_speed;
3173 start->ls_version = lr->lr_version;
3174 start->ls_active = lr->lr_active;
3175 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3176 start->ls_async_windows = lr->lr_async_windows;
3178 lsp.lsp_start = start;
3179 lsp.lsp_index = lr->lr_index;
3180 lsp.lsp_index_valid = 1;
3181 rc = lfsck_start(env, key, &lsp);
3185 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3187 memset(stop, 0, sizeof(*stop));
3188 stop->ls_status = lr->lr_status;
3189 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3190 rc = lfsck_stop(env, key, stop);
3193 case LE_PHASE1_DONE:
3194 case LE_PHASE2_DONE:
3195 case LE_FID_ACCESSED:
3197 case LE_CONDITIONAL_DESTROY:
3198 case LE_SKIP_NLINK_DECLARE:
3200 case LE_SET_LMV_MASTER:
3201 case LE_SET_LMV_SLAVE:
3202 case LE_PAIRS_VERIFY: {
3203 struct lfsck_instance *lfsck;
3204 struct lfsck_component *com;
3206 lfsck = lfsck_instance_find(key, true, false);
3207 if (unlikely(lfsck == NULL))
3210 com = lfsck_component_find(lfsck, lr->lr_active);
3211 if (likely(com != NULL)) {
3212 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3213 lfsck_component_put(env, com);
3216 lfsck_instance_put(env, lfsck);
3225 EXPORT_SYMBOL(lfsck_in_notify);
3227 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3228 struct lfsck_request *lr)
3230 struct lfsck_instance *lfsck;
3231 struct lfsck_component *com;
3235 lfsck = lfsck_instance_find(key, true, false);
3236 if (unlikely(lfsck == NULL))
3239 com = lfsck_component_find(lfsck, lr->lr_active);
3240 if (likely(com != NULL)) {
3241 rc = com->lc_ops->lfsck_query(env, com);
3242 lfsck_component_put(env, com);
3247 lfsck_instance_put(env, lfsck);
3252 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3253 struct ldlm_namespace *ns)
3255 struct lfsck_instance *lfsck;
3258 lfsck = lfsck_instance_find(key, true, false);
3259 if (likely(lfsck != NULL)) {
3260 lfsck->li_namespace = ns;
3261 lfsck_instance_put(env, lfsck);
3267 EXPORT_SYMBOL(lfsck_register_namespace);
3269 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3270 struct dt_device *next, struct obd_device *obd,
3271 lfsck_out_notify notify, void *notify_data, bool master)
3273 struct lfsck_instance *lfsck;
3274 struct dt_object *root = NULL;
3275 struct dt_object *obj = NULL;
3276 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3280 lfsck = lfsck_instance_find(key, false, false);
3281 if (unlikely(lfsck != NULL))
3284 OBD_ALLOC_PTR(lfsck);
3288 mutex_init(&lfsck->li_mutex);
3289 spin_lock_init(&lfsck->li_lock);
3290 INIT_LIST_HEAD(&lfsck->li_link);
3291 INIT_LIST_HEAD(&lfsck->li_list_scan);
3292 INIT_LIST_HEAD(&lfsck->li_list_dir);
3293 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3294 INIT_LIST_HEAD(&lfsck->li_list_idle);
3295 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3296 atomic_set(&lfsck->li_ref, 1);
3297 atomic_set(&lfsck->li_double_scan_count, 0);
3298 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3299 lfsck->li_out_notify = notify;
3300 lfsck->li_out_notify_data = notify_data;
3301 lfsck->li_next = next;
3302 lfsck->li_bottom = key;
3303 lfsck->li_obd = obd;
3305 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3309 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3313 fid->f_seq = FID_SEQ_LOCAL_NAME;
3316 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3320 rc = dt_root_get(env, key, fid);
3324 root = dt_locate(env, key, fid);
3326 GOTO(out, rc = PTR_ERR(root));
3328 if (unlikely(!dt_try_as_dir(env, root)))
3329 GOTO(out, rc = -ENOTDIR);
3331 lfsck->li_local_root_fid = *fid;
3333 lfsck->li_master = 1;
3334 if (lfsck_dev_idx(lfsck) == 0) {
3335 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3336 const struct lu_name *cname;
3338 rc = dt_lookup(env, root,
3339 (struct dt_rec *)(&lfsck->li_global_root_fid),
3340 (const struct dt_key *)"ROOT");
3344 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3346 GOTO(out, rc = PTR_ERR(obj));
3348 if (unlikely(!dt_try_as_dir(env, obj)))
3349 GOTO(out, rc = -ENOTDIR);
3351 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3352 (const struct dt_key *)dotlustre);
3356 lfsck_object_put(env, obj);
3357 obj = dt_locate(env, key, fid);
3359 GOTO(out, rc = PTR_ERR(obj));
3361 cname = lfsck_name_get_const(env, dotlustre,
3363 rc = lfsck_verify_linkea(env, obj, cname,
3364 &lfsck->li_global_root_fid);
3368 if (unlikely(!dt_try_as_dir(env, obj)))
3369 GOTO(out, rc = -ENOTDIR);
3372 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3373 (const struct dt_key *)lostfound);
3377 lfsck_object_put(env, obj);
3378 obj = dt_locate(env, key, fid);
3380 GOTO(out, rc = PTR_ERR(obj));
3382 cname = lfsck_name_get_const(env, lostfound,
3384 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3388 lfsck_object_put(env, obj);
3393 fid->f_seq = FID_SEQ_LOCAL_FILE;
3394 fid->f_oid = OTABLE_IT_OID;
3396 obj = dt_locate(env, key, fid);
3398 GOTO(out, rc = PTR_ERR(obj));
3400 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3404 lfsck->li_obj_oit = obj;
3405 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3406 S_IFDIR | S_IRUGO | S_IWUSR);
3408 GOTO(out, rc = PTR_ERR(obj));
3410 lu_object_get(&obj->do_lu);
3411 lfsck->li_lfsck_dir = obj;
3412 rc = lfsck_bookmark_setup(env, lfsck);
3417 rc = lfsck_fid_init(lfsck);
3421 rc = lfsck_namespace_setup(env, lfsck);
3426 rc = lfsck_layout_setup(env, lfsck);
3430 /* XXX: more LFSCK components initialization to be added here. */
3432 rc = lfsck_instance_add(lfsck);
3434 rc = lfsck_add_target_from_orphan(env, lfsck);
3436 if (obj != NULL && !IS_ERR(obj))
3437 lfsck_object_put(env, obj);
3438 if (root != NULL && !IS_ERR(root))
3439 lfsck_object_put(env, root);
3441 lfsck_instance_cleanup(env, lfsck);
3444 EXPORT_SYMBOL(lfsck_register);
3446 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3448 struct lfsck_instance *lfsck;
3450 lfsck = lfsck_instance_find(key, false, true);
3452 lfsck_instance_put(env, lfsck);
3454 EXPORT_SYMBOL(lfsck_degister);
3456 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3457 struct dt_device *tgt, struct obd_export *exp,
3458 __u32 index, bool for_ost)
3460 struct lfsck_instance *lfsck;
3461 struct lfsck_tgt_desc *ltd;
3472 INIT_LIST_HEAD(<d->ltd_orphan_list);
3473 INIT_LIST_HEAD(<d->ltd_layout_list);
3474 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3475 INIT_LIST_HEAD(<d->ltd_namespace_list);
3476 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3477 atomic_set(<d->ltd_ref, 1);
3478 ltd->ltd_index = index;
3480 spin_lock(&lfsck_instance_lock);
3481 lfsck = __lfsck_instance_find(key, true, false);
3482 if (lfsck == NULL) {
3484 list_add_tail(<d->ltd_orphan_list,
3485 &lfsck_ost_orphan_list);
3487 list_add_tail(<d->ltd_orphan_list,
3488 &lfsck_mdt_orphan_list);
3489 spin_unlock(&lfsck_instance_lock);
3493 spin_unlock(&lfsck_instance_lock);
3495 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3499 lfsck_instance_put(env, lfsck);
3503 EXPORT_SYMBOL(lfsck_add_target);
3505 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3506 struct dt_device *tgt, __u32 index, bool for_ost)
3508 struct lfsck_instance *lfsck;
3509 struct lfsck_tgt_descs *ltds;
3510 struct lfsck_tgt_desc *ltd;
3511 struct list_head *head;
3514 head = &lfsck_ost_orphan_list;
3516 head = &lfsck_mdt_orphan_list;
3518 spin_lock(&lfsck_instance_lock);
3519 list_for_each_entry(ltd, head, ltd_orphan_list) {
3520 if (ltd->ltd_tgt == tgt) {
3521 list_del_init(<d->ltd_orphan_list);
3522 spin_unlock(&lfsck_instance_lock);
3530 lfsck = __lfsck_instance_find(key, true, false);
3531 spin_unlock(&lfsck_instance_lock);
3532 if (unlikely(lfsck == NULL))
3536 ltds = &lfsck->li_ost_descs;
3538 ltds = &lfsck->li_mdt_descs;
3540 down_write(<ds->ltd_rw_sem);
3541 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3543 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3546 ltd = lfsck_ltd2tgt(ltds, index);
3547 if (unlikely(ltd == NULL))
3550 LASSERT(ltds->ltd_tgtnr > 0);
3553 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3554 lfsck_assign_tgt(ltds, NULL, index);
3559 head = &lfsck->li_ost_descs.ltd_orphan;
3561 head = &lfsck->li_mdt_descs.ltd_orphan;
3563 list_for_each_entry(ltd, head, ltd_orphan_list) {
3564 if (ltd->ltd_tgt == tgt) {
3565 list_del_init(<d->ltd_orphan_list);
3571 up_write(<ds->ltd_rw_sem);
3573 spin_lock(<ds->ltd_lock);
3575 spin_unlock(<ds->ltd_lock);
3576 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3577 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3581 lfsck_instance_put(env, lfsck);
3583 EXPORT_SYMBOL(lfsck_del_target);
3585 static int __init lfsck_init(void)
3589 INIT_LIST_HEAD(&lfsck_instance_list);
3590 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3591 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3592 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3593 rc = lu_context_key_register(&lfsck_thread_key);
3595 tgt_register_lfsck_in_notify(lfsck_in_notify);
3596 tgt_register_lfsck_query(lfsck_query);
3602 static void __exit lfsck_exit(void)
3604 struct lfsck_tgt_desc *ltd;
3605 struct lfsck_tgt_desc *next;
3607 LASSERT(list_empty(&lfsck_instance_list));
3609 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3611 list_del_init(<d->ltd_orphan_list);
3615 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3617 list_del_init(<d->ltd_orphan_list);
3621 lu_context_key_degister(&lfsck_thread_key);
3624 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3625 MODULE_DESCRIPTION("Lustre File System Checker");
3626 MODULE_VERSION(LUSTRE_VERSION_STRING);
3627 MODULE_LICENSE("GPL");
3629 module_init(lfsck_init);
3630 module_exit(lfsck_exit);