4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, 2014, Intel Corporation.
26 * lustre/lfsck/lfsck_lib.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <libcfs/list.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fld.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre_lfsck.h>
41 #include <lustre/lustre_lfsck_user.h>
43 #include "lfsck_internal.h"
45 #define LFSCK_CHECKPOINT_SKIP 1
47 /* define lfsck thread key */
48 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
50 static void lfsck_key_fini(const struct lu_context *ctx,
51 struct lu_context_key *key, void *data)
53 struct lfsck_thread_info *info = data;
55 lu_buf_free(&info->lti_linkea_buf);
56 lu_buf_free(&info->lti_linkea_buf2);
57 lu_buf_free(&info->lti_big_buf);
61 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
62 LU_KEY_INIT_GENERIC(lfsck);
64 static struct list_head lfsck_instance_list;
65 static struct list_head lfsck_ost_orphan_list;
66 static struct list_head lfsck_mdt_orphan_list;
67 static DEFINE_SPINLOCK(lfsck_instance_lock);
69 static const char *lfsck_status_names[] = {
71 [LS_SCANNING_PHASE1] = "scanning-phase1",
72 [LS_SCANNING_PHASE2] = "scanning-phase2",
73 [LS_COMPLETED] = "completed",
74 [LS_FAILED] = "failed",
75 [LS_STOPPED] = "stopped",
76 [LS_PAUSED] = "paused",
77 [LS_CRASHED] = "crashed",
78 [LS_PARTIAL] = "partial",
79 [LS_CO_FAILED] = "co-failed",
80 [LS_CO_STOPPED] = "co-stopped",
81 [LS_CO_PAUSED] = "co-paused"
84 const char *lfsck_flags_names[] = {
93 const char *lfsck_param_names[] = {
105 enum lfsck_verify_lpf_types {
106 LVLT_BY_BOOKMARK = 0,
107 LVLT_BY_NAMEENTRY = 1,
110 const char *lfsck_status2names(enum lfsck_status status)
112 if (unlikely(status < 0 || status >= LS_MAX))
115 return lfsck_status_names[status];
118 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
120 spin_lock_init(<ds->ltd_lock);
121 init_rwsem(<ds->ltd_rw_sem);
122 INIT_LIST_HEAD(<ds->ltd_orphan);
123 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
124 if (ltds->ltd_tgts_bitmap == NULL)
130 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
132 struct lfsck_tgt_desc *ltd;
133 struct lfsck_tgt_desc *next;
136 down_write(<ds->ltd_rw_sem);
138 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
140 list_del_init(<d->ltd_orphan_list);
144 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
145 up_write(<ds->ltd_rw_sem);
150 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
151 ltd = LTD_TGT(ltds, idx);
152 if (likely(ltd != NULL)) {
153 LASSERT(list_empty(<d->ltd_layout_list));
154 LASSERT(list_empty(<d->ltd_layout_phase_list));
155 LASSERT(list_empty(<d->ltd_namespace_list));
156 LASSERT(list_empty(<d->ltd_namespace_phase_list));
159 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
160 LTD_TGT(ltds, idx) = NULL;
165 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
168 for (idx = 0; idx < TGT_PTRS; idx++) {
169 if (ltds->ltd_tgts_idx[idx] != NULL) {
170 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
171 ltds->ltd_tgts_idx[idx] = NULL;
175 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
176 ltds->ltd_tgts_bitmap = NULL;
177 up_write(<ds->ltd_rw_sem);
180 static int __lfsck_add_target(const struct lu_env *env,
181 struct lfsck_instance *lfsck,
182 struct lfsck_tgt_desc *ltd,
183 bool for_ost, bool locked)
185 struct lfsck_tgt_descs *ltds;
186 __u32 index = ltd->ltd_index;
191 ltds = &lfsck->li_ost_descs;
193 ltds = &lfsck->li_mdt_descs;
196 down_write(<ds->ltd_rw_sem);
198 LASSERT(ltds->ltd_tgts_bitmap != NULL);
200 if (index >= ltds->ltd_tgts_bitmap->size) {
201 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
202 (__u32)BITS_PER_LONG);
203 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
204 cfs_bitmap_t *new_bitmap;
206 while (newsize < index + 1)
209 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
210 if (new_bitmap == NULL)
211 GOTO(unlock, rc = -ENOMEM);
213 if (ltds->ltd_tgtnr > 0)
214 cfs_bitmap_copy(new_bitmap, old_bitmap);
215 ltds->ltd_tgts_bitmap = new_bitmap;
216 CFS_FREE_BITMAP(old_bitmap);
219 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
220 CERROR("%s: the device %s (%u) is registered already\n",
221 lfsck_lfsck2name(lfsck),
222 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
223 GOTO(unlock, rc = -EEXIST);
226 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
227 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
228 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
229 GOTO(unlock, rc = -ENOMEM);
232 LTD_TGT(ltds, index) = ltd;
233 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
236 GOTO(unlock, rc = 0);
240 up_write(<ds->ltd_rw_sem);
245 static int lfsck_add_target_from_orphan(const struct lu_env *env,
246 struct lfsck_instance *lfsck)
248 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
249 struct lfsck_tgt_desc *ltd;
250 struct lfsck_tgt_desc *next;
251 struct list_head *head = &lfsck_ost_orphan_list;
256 spin_lock(&lfsck_instance_lock);
257 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
258 if (ltd->ltd_key == lfsck->li_bottom)
259 list_move_tail(<d->ltd_orphan_list,
262 spin_unlock(&lfsck_instance_lock);
264 down_write(<ds->ltd_rw_sem);
265 while (!list_empty(<ds->ltd_orphan)) {
266 ltd = list_entry(ltds->ltd_orphan.next,
267 struct lfsck_tgt_desc,
269 list_del_init(<d->ltd_orphan_list);
270 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
271 /* Do not hold the semaphore for too long time. */
272 up_write(<ds->ltd_rw_sem);
276 down_write(<ds->ltd_rw_sem);
278 up_write(<ds->ltd_rw_sem);
281 ltds = &lfsck->li_mdt_descs;
282 head = &lfsck_mdt_orphan_list;
290 static inline struct lfsck_component *
291 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
292 struct list_head *list)
294 struct lfsck_component *com;
296 list_for_each_entry(com, list, lc_link) {
297 if (com->lc_type == type)
303 struct lfsck_component *
304 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
306 struct lfsck_component *com;
308 spin_lock(&lfsck->li_lock);
309 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
313 com = __lfsck_component_find(lfsck, type,
314 &lfsck->li_list_double_scan);
318 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
322 lfsck_component_get(com);
323 spin_unlock(&lfsck->li_lock);
327 void lfsck_component_cleanup(const struct lu_env *env,
328 struct lfsck_component *com)
330 if (!list_empty(&com->lc_link))
331 list_del_init(&com->lc_link);
332 if (!list_empty(&com->lc_link_dir))
333 list_del_init(&com->lc_link_dir);
335 lfsck_component_put(env, com);
338 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
339 struct lu_fid *fid, bool locked)
341 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
346 mutex_lock(&lfsck->li_mutex);
348 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
350 bk->lb_last_fid = *fid;
351 /* We do not care about whether the subsequent sub-operations
352 * failed or not. The worst case is that one FID is lost that
353 * is not a big issue for the LFSCK since it is relative rare
354 * for LFSCK create. */
355 rc = lfsck_bookmark_store(env, lfsck);
359 mutex_unlock(&lfsck->li_mutex);
365 * Request the specified ibits lock for the given object.
367 * Before the LFSCK modifying on the namespace visible object,
368 * it needs to acquire related ibits ldlm lock.
370 * \param[in] env pointer to the thread context
371 * \param[in] lfsck pointer to the lfsck instance
372 * \param[in] obj pointer to the dt_object to be locked
373 * \param[out] lh pointer to the lock handle
374 * \param[in] ibits the bits for the ldlm lock to be acquired
375 * \param[in] mode the mode for the ldlm lock to be acquired
377 * \retval 0 for success
378 * \retval negative error number on failure
380 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
381 struct dt_object *obj, struct lustre_handle *lh,
382 __u64 bits, ldlm_mode_t mode)
384 struct lfsck_thread_info *info = lfsck_env_info(env);
385 ldlm_policy_data_t *policy = &info->lti_policy;
386 struct ldlm_res_id *resid = &info->lti_resid;
387 __u64 flags = LDLM_FL_ATOMIC_CB;
390 LASSERT(lfsck->li_namespace != NULL);
392 memset(policy, 0, sizeof(*policy));
393 policy->l_inodebits.bits = bits;
394 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
395 if (dt_object_remote(obj)) {
396 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
398 memset(einfo, 0, sizeof(*einfo));
399 einfo->ei_type = LDLM_IBITS;
400 einfo->ei_mode = mode;
401 einfo->ei_cb_bl = ldlm_blocking_ast;
402 einfo->ei_cb_cp = ldlm_completion_ast;
403 einfo->ei_res_id = resid;
405 rc = dt_object_lock(env, obj, lh, einfo, policy);
407 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
408 LDLM_IBITS, policy, mode,
409 &flags, ldlm_blocking_ast,
410 ldlm_completion_ast, NULL, NULL,
411 0, LVB_T_NONE, NULL, lh);
414 if (rc == ELDLM_OK) {
417 memset(lh, 0, sizeof(*lh));
425 * Release the the specified ibits lock.
427 * If the lock has been acquired before, release it
428 * and cleanup the handle. Otherwise, do nothing.
430 * \param[in] lh pointer to the lock handle
431 * \param[in] mode the mode for the ldlm lock to be released
433 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
435 if (lustre_handle_is_used(lh)) {
436 ldlm_lock_decref(lh, mode);
437 memset(lh, 0, sizeof(*lh));
441 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
442 struct lfsck_instance *lfsck,
443 const struct lu_fid *fid)
445 struct seq_server_site *ss =
446 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
447 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
450 fld_range_set_mdt(range);
451 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
453 rc = range->lsr_index;
458 const char dot[] = ".";
459 const char dotdot[] = "..";
460 static const char dotlustre[] = ".lustre";
461 static const char lostfound[] = "lost+found";
464 * Remove the name entry from the .lustre/lost+found directory.
466 * No need to care about the object referenced by the name entry,
467 * either the name entry is invalid or redundant, or the referenced
468 * object has been processed or will be handled by others.
470 * \param[in] env pointer to the thread context
471 * \param[in] lfsck pointer to the lfsck instance
472 * \param[in] name the name for the name entry to be removed
474 * \retval 0 for success
475 * \retval negative error number on failure
477 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
478 struct lfsck_instance *lfsck,
481 struct dt_object *parent = lfsck->li_lpf_root_obj;
482 struct dt_device *dev = lfsck->li_next;
484 struct lustre_handle lh = { 0 };
488 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
489 MDS_INODELOCK_UPDATE, LCK_EX);
493 th = dt_trans_create(env, dev);
495 GOTO(unlock, rc = PTR_ERR(th));
497 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
501 rc = dt_declare_ref_del(env, parent, th);
505 rc = dt_trans_start(env, dev, th);
509 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
514 dt_write_lock(env, parent, 0);
515 rc = dt_ref_del(env, parent, th);
516 dt_write_unlock(env, parent);
521 dt_trans_stop(env, dev, th);
524 lfsck_ibits_unlock(&lh, LCK_EX);
526 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
527 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
532 static int lfsck_create_lpf_local(const struct lu_env *env,
533 struct lfsck_instance *lfsck,
534 struct dt_object *child,
536 struct dt_object_format *dof,
539 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
540 struct dt_object *parent = lfsck->li_lpf_root_obj;
541 struct dt_device *dev = lfsck->li_bottom;
542 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
543 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
544 const struct lu_fid *cfid = lfsck_dto2fid(child);
545 struct thandle *th = NULL;
546 struct linkea_data ldata = { NULL };
547 struct lu_buf linkea_buf;
548 const struct lu_name *cname;
550 int len = sizeof(struct lfsck_bookmark);
554 rc = linkea_data_new(&ldata,
555 &lfsck_env_info(env)->lti_linkea_buf2);
559 cname = lfsck_name_get_const(env, name, strlen(name));
560 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
564 th = dt_trans_create(env, dev);
568 /* 1a. create child */
569 rc = dt_declare_create(env, child, la, NULL, dof, th);
573 /* 2a. increase child nlink */
574 rc = dt_declare_ref_add(env, child, th);
578 /* 3a. insert linkEA for child */
579 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
580 ldata.ld_leh->leh_len);
581 rc = dt_declare_xattr_set(env, child, &linkea_buf,
582 XATTR_NAME_LINK, 0, th);
586 /* 4a. insert name into parent dir */
587 rec->rec_type = S_IFDIR;
589 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
590 (const struct dt_key *)name, th);
594 /* 5a. increase parent nlink */
595 rc = dt_declare_ref_add(env, parent, th);
599 /* 6a. update bookmark */
600 rc = dt_declare_record_write(env, bk_obj,
601 lfsck_buf_get(env, bk, len), 0, th);
605 rc = dt_trans_start_local(env, dev, th);
609 dt_write_lock(env, child, 0);
610 /* 1b.1. create child */
611 rc = dt_create(env, child, la, NULL, dof, th);
615 if (unlikely(!dt_try_as_dir(env, child)))
616 GOTO(unlock, rc = -ENOTDIR);
618 /* 1b.2. insert dot into child dir */
620 rc = dt_insert(env, child, (const struct dt_rec *)rec,
621 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
625 /* 1b.3. insert dotdot into child dir */
626 rec->rec_fid = &LU_LPF_FID;
627 rc = dt_insert(env, child, (const struct dt_rec *)rec,
628 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
632 /* 2b. increase child nlink */
633 rc = dt_ref_add(env, child, th);
637 /* 3b. insert linkEA for child. */
638 rc = dt_xattr_set(env, child, &linkea_buf,
639 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
640 dt_write_unlock(env, child);
644 /* 4b. insert name into parent dir */
646 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
647 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
651 dt_write_lock(env, parent, 0);
652 /* 5b. increase parent nlink */
653 rc = dt_ref_add(env, parent, th);
654 dt_write_unlock(env, parent);
658 bk->lb_lpf_fid = *cfid;
659 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
661 /* 6b. update bookmark */
662 rc = dt_record_write(env, bk_obj,
663 lfsck_buf_get(env, bk, len), &pos, th);
668 dt_write_unlock(env, child);
671 dt_trans_stop(env, dev, th);
676 static int lfsck_create_lpf_remote(const struct lu_env *env,
677 struct lfsck_instance *lfsck,
678 struct dt_object *child,
680 struct dt_object_format *dof,
683 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
684 struct dt_object *parent = lfsck->li_lpf_root_obj;
685 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
686 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
687 const struct lu_fid *cfid = lfsck_dto2fid(child);
688 struct thandle *th = NULL;
689 struct linkea_data ldata = { NULL };
690 struct lu_buf linkea_buf;
691 const struct lu_name *cname;
692 struct dt_device *dev;
694 int len = sizeof(struct lfsck_bookmark);
698 rc = linkea_data_new(&ldata,
699 &lfsck_env_info(env)->lti_linkea_buf2);
703 cname = lfsck_name_get_const(env, name, strlen(name));
704 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
708 /* Create .lustre/lost+found/MDTxxxx. */
710 /* XXX: Currently, cross-MDT create operation needs to create the child
711 * object firstly, then insert name into the parent directory. For
712 * this case, the child object resides on current MDT (local), but
713 * the parent ".lustre/lost+found" may be on remote MDT. It is not
714 * easy to contain all the sub-modifications orderly within single
717 * To avoid more inconsistency, we split the create operation into
720 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
722 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
725 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
726 * repair such inconsistency when LFSCK run next time. */
728 /* Transaction I: locally */
730 dev = lfsck->li_bottom;
731 th = dt_trans_create(env, dev);
735 /* 1a. create child */
736 rc = dt_declare_create(env, child, la, NULL, dof, th);
740 /* 2a. increase child nlink */
741 rc = dt_declare_ref_add(env, child, th);
745 /* 3a. insert linkEA for child */
746 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
747 ldata.ld_leh->leh_len);
748 rc = dt_declare_xattr_set(env, child, &linkea_buf,
749 XATTR_NAME_LINK, 0, th);
753 /* 4a. update bookmark */
754 rc = dt_declare_record_write(env, bk_obj,
755 lfsck_buf_get(env, bk, len), 0, th);
759 rc = dt_trans_start_local(env, dev, th);
763 dt_write_lock(env, child, 0);
764 /* 1b.1. create child */
765 rc = dt_create(env, child, la, NULL, dof, th);
769 if (unlikely(!dt_try_as_dir(env, child)))
770 GOTO(unlock, rc = -ENOTDIR);
772 /* 1b.2. insert dot into child dir */
773 rec->rec_type = S_IFDIR;
775 rc = dt_insert(env, child, (const struct dt_rec *)rec,
776 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
780 /* 1b.3. insert dotdot into child dir */
781 rec->rec_fid = &LU_LPF_FID;
782 rc = dt_insert(env, child, (const struct dt_rec *)rec,
783 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
787 /* 2b. increase child nlink */
788 rc = dt_ref_add(env, child, th);
792 /* 3b. insert linkEA for child */
793 rc = dt_xattr_set(env, child, &linkea_buf,
794 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
798 bk->lb_lpf_fid = *cfid;
799 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
801 /* 4b. update bookmark */
802 rc = dt_record_write(env, bk_obj,
803 lfsck_buf_get(env, bk, len), &pos, th);
805 dt_write_unlock(env, child);
806 dt_trans_stop(env, dev, th);
810 /* Transaction II: remotely */
812 dev = lfsck->li_next;
813 th = dt_trans_create(env, dev);
817 /* 5a. insert name into parent dir */
819 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
820 (const struct dt_key *)name, th);
824 /* 6a. increase parent nlink */
825 rc = dt_declare_ref_add(env, parent, th);
829 rc = dt_trans_start(env, dev, th);
833 /* 5b. insert name into parent dir */
834 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
835 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
839 dt_write_lock(env, parent, 0);
840 /* 6b. increase parent nlink */
841 rc = dt_ref_add(env, parent, th);
842 dt_write_unlock(env, parent);
847 dt_write_unlock(env, child);
849 dt_trans_stop(env, dev, th);
851 if (rc != 0 && dev == lfsck->li_next)
852 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
853 "for orphans, but failed to insert the name %s "
854 "to the .lustre/lost+found/. Such inconsistency "
855 "will be repaired when LFSCK run next time: rc = %d\n",
856 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
862 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
864 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
865 * orphans and other uncertain inconsistent objects found during the
866 * LFSCK. Such directory will be created by the LFSCK engine on the
867 * local MDT before the LFSCK scanning.
869 * \param[in] env pointer to the thread context
870 * \param[in] lfsck pointer to the lfsck instance
872 * \retval 0 for success
873 * \retval negative error number on failure
875 static int lfsck_create_lpf(const struct lu_env *env,
876 struct lfsck_instance *lfsck)
878 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
879 struct lfsck_thread_info *info = lfsck_env_info(env);
880 struct lu_fid *cfid = &info->lti_fid2;
881 struct lu_attr *la = &info->lti_la;
882 struct dt_object_format *dof = &info->lti_dof;
883 struct dt_object *parent = lfsck->li_lpf_root_obj;
884 struct dt_object *child = NULL;
885 struct lustre_handle lh = { 0 };
887 int node = lfsck_dev_idx(lfsck->li_bottom);
891 LASSERT(lfsck->li_master);
892 LASSERT(parent != NULL);
893 LASSERT(lfsck->li_lpf_obj == NULL);
895 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
896 MDS_INODELOCK_UPDATE, LCK_EX);
900 snprintf(name, 8, "MDT%04x", node);
901 if (fid_is_zero(&bk->lb_lpf_fid)) {
902 /* There is corner case that: in former LFSCK scanning we have
903 * created the .lustre/lost+found/MDTxxxx but failed to update
904 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
905 * it from MDT0 firstly. */
906 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
907 (const struct dt_key *)name, BYPASS_CAPA);
908 if (rc != 0 && rc != -ENOENT)
912 bk->lb_lpf_fid = *cfid;
913 rc = lfsck_bookmark_store(env, lfsck);
915 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
920 *cfid = bk->lb_lpf_fid;
923 child = lfsck_object_find_by_dev(env, lfsck->li_bottom, cfid);
925 GOTO(unlock, rc = PTR_ERR(child));
927 if (dt_object_exists(child) != 0) {
928 if (unlikely(!dt_try_as_dir(env, child)))
931 lfsck->li_lpf_obj = child;
936 memset(la, 0, sizeof(*la));
937 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
938 la->la_mode = S_IFDIR | S_IRWXU;
939 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
941 memset(dof, 0, sizeof(*dof));
942 dof->dof_type = dt_mode_to_dft(S_IFDIR);
945 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
947 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
949 lfsck->li_lpf_obj = child;
954 lfsck_ibits_unlock(&lh, LCK_EX);
955 if (rc != 0 && child != NULL && !IS_ERR(child))
956 lu_object_put(env, &child->do_lu);
962 * Scan .lustre/lost+found for bad name entries and remove them.
964 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
965 * index in the system. Any other formatted name is invalid and should be
968 * \param[in] env pointer to the thread context
969 * \param[in] lfsck pointer to the lfsck instance
971 * \retval 0 for success
972 * \retval negative error number on failure
974 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
975 struct lfsck_instance *lfsck)
977 struct dt_object *parent = lfsck->li_lpf_root_obj;
978 struct lu_dirent *ent =
979 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
980 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
985 it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA);
989 rc = iops->load(env, it, 0);
991 rc = iops->next(env, it);
998 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1002 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1003 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1006 /* name length must be strlen("MDTxxxx") */
1007 if (ent->lde_namelen != 7)
1010 if (memcmp(ent->lde_name, "MDT", off) != 0)
1013 while (off < 7 && isxdigit(ent->lde_name[off]))
1019 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1026 rc = iops->next(env, it);
1030 iops->fini(env, it);
1032 RETURN(rc > 0 ? 0 : rc);
1035 static int lfsck_update_lpf_entry(const struct lu_env *env,
1036 struct lfsck_instance *lfsck,
1037 struct dt_object *parent,
1038 struct dt_object *child,
1040 enum lfsck_verify_lpf_types type)
1044 if (type == LVLT_BY_BOOKMARK) {
1045 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1046 lfsck_dto2fid(child), S_IFDIR);
1047 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1048 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1049 rc = lfsck_bookmark_store(env, lfsck);
1051 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1052 " in the bookmark file: rc = %d\n",
1053 lfsck_lfsck2name(lfsck),
1054 PFID(lfsck_dto2fid(child)), rc);
1061 * Check whether the @child back references the @parent.
1064 * 1) The child's FID is stored in the bookmark file. If the child back
1065 * references the parent (LU_LPF_FID object) via its ".." entry, then
1066 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1067 * the child back references another parent2, then:
1068 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1069 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1070 * references the child. So keep them there. As the LFSCK processing,
1071 * the parent3 may be found, then when the LFSCK run next time, the
1072 * inconsistency can be repaired.
1074 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1075 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1076 * via its ".." entry, then update the bookmark file, otherwise, if the child
1077 * back references another parent2, then:
1078 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1079 * from .lustre/lost+found/;
1080 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1081 * sub-directory name entry and update the child;
1082 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1083 * or not, then keep them there.
1085 * \param[in] env pointer to the thread context
1086 * \param[in] lfsck pointer to the lfsck instance
1087 * \param[in] child pointer to the lost+found sub-directory object
1088 * \param[in] name the name for lost+found sub-directory object
1089 * \param[out] fid pointer to the buffer to hold the FID of the object
1090 * (called it as parent2) that is referenced via the
1091 * child's dotdot entry; it also can be the FID that
1092 * is referenced by the name entry under the parent2.
1093 * \param[in] type to indicate where the child's FID is stored in
1095 * \retval positive number for uncertain inconsistency
1096 * \retval 0 for success
1097 * \retval negative error number on failure
1099 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1100 struct lfsck_instance *lfsck,
1101 struct dt_object *child, const char *name,
1103 enum lfsck_verify_lpf_types type)
1105 struct dt_object *parent = lfsck->li_lpf_root_obj;
1106 struct lfsck_thread_info *info = lfsck_env_info(env);
1107 char *name2 = info->lti_key;
1108 struct lu_fid *fid2 = &info->lti_fid3;
1109 struct dt_object *parent2 = NULL;
1110 struct lustre_handle lh = { 0 };
1115 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1116 (const struct dt_key *)dotdot, BYPASS_CAPA);
1120 if (!fid_is_sane(fid))
1121 GOTO(linkea, rc = -EINVAL);
1123 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1124 const struct lu_name *cname;
1126 if (lfsck->li_lpf_obj == NULL) {
1127 lu_object_get(&child->do_lu);
1128 lfsck->li_lpf_obj = child;
1131 cname = lfsck_name_get_const(env, name, strlen(name));
1132 rc = lfsck_verify_linkea(env, lfsck->li_bottom, child, cname,
1135 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1141 parent2 = lfsck_object_find_by_dev(env, lfsck->li_next, fid);
1142 if (IS_ERR(parent2))
1143 GOTO(linkea, parent2);
1145 if (!dt_object_exists(parent2)) {
1146 lu_object_put(env, &parent2->do_lu);
1148 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1151 if (!dt_try_as_dir(env, parent2)) {
1152 lu_object_put(env, &parent2->do_lu);
1154 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1158 /* To prevent rename/unlink race */
1159 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1160 MDS_INODELOCK_UPDATE, LCK_PR);
1164 dt_read_lock(env, child, 0);
1165 rc = lfsck_links_get_first(env, child, name2, fid2);
1167 dt_read_unlock(env, child);
1168 lfsck_ibits_unlock(&lh, LCK_PR);
1170 GOTO(out_put, rc = 1);
1173 /* It is almost impossible that the bookmark file (or the name entry)
1174 * and the linkEA hit the same data corruption. Trust the linkEA. */
1175 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1176 dt_read_unlock(env, child);
1177 lfsck_ibits_unlock(&lh, LCK_PR);
1180 if (lfsck->li_lpf_obj == NULL) {
1181 lu_object_get(&child->do_lu);
1182 lfsck->li_lpf_obj = child;
1185 /* Update the child's dotdot entry */
1186 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1187 &LU_LPF_FID, S_IFDIR);
1189 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1195 if (parent2 == NULL || IS_ERR(parent2)) {
1196 dt_read_unlock(env, child);
1197 lfsck_ibits_unlock(&lh, LCK_PR);
1199 GOTO(out_done, rc = 1);
1202 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1203 (const struct dt_key *)name2, BYPASS_CAPA);
1204 dt_read_unlock(env, child);
1205 lfsck_ibits_unlock(&lh, LCK_PR);
1206 if (rc != 0 && rc != -ENOENT)
1209 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1210 if (type == LVLT_BY_BOOKMARK)
1211 GOTO(out_put, rc = 1);
1213 /* Trust the name entry, update the child's dotdot entry. */
1214 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1215 &LU_LPF_FID, S_IFDIR);
1220 if (type == LVLT_BY_BOOKMARK) {
1221 /* Invalid FID record in the bookmark file, reset it. */
1222 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1223 rc = lfsck_bookmark_store(env, lfsck);
1225 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1226 " in the bookmark file: rc = %d\n",
1227 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1228 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1229 /* The name entry is wrong, remove it. */
1230 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1236 if (parent2 != NULL && !IS_ERR(parent2))
1237 lu_object_put(env, &parent2->do_lu);
1244 * Verify the /ROOT/.lustre/lost+found/ directory.
1246 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1247 * the LFSCK does not exactly know how to handle, such as orphans. So before
1248 * the LFSCK scanning the system, the consistency of such directory needs to
1249 * be verified firstly to allow the users to use it during the LFSCK.
1251 * \param[in] env pointer to the thread context
1252 * \param[in] lfsck pointer to the lfsck instance
1254 * \retval positive number for uncertain inconsistency
1255 * \retval 0 for success
1256 * \retval negative error number on failure
1258 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1260 struct lfsck_thread_info *info = lfsck_env_info(env);
1261 struct lu_fid *pfid = &info->lti_fid;
1262 struct lu_fid *cfid = &info->lti_fid2;
1263 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1264 struct dt_object *parent;
1265 /* child1's FID is in the bookmark file. */
1266 struct dt_object *child1 = NULL;
1267 /* child2's FID is in the name entry MDTxxxx. */
1268 struct dt_object *child2 = NULL;
1269 struct dt_device *dev = lfsck->li_bottom;
1270 const struct lu_name *cname;
1272 int node = lfsck_dev_idx(dev);
1276 LASSERT(lfsck->li_master);
1278 if (lfsck->li_lpf_root_obj != NULL)
1282 parent = lfsck_object_find_by_dev(env, dev, &LU_LPF_FID);
1284 struct lfsck_tgt_desc *ltd;
1286 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1287 if (unlikely(ltd == NULL))
1290 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1296 RETURN(PTR_ERR(parent));
1298 LASSERT(dt_object_exists(parent));
1300 if (unlikely(!dt_try_as_dir(env, parent))) {
1301 lu_object_put(env, &parent->do_lu);
1303 GOTO(put, rc = -ENOTDIR);
1306 lfsck->li_lpf_root_obj = parent;
1308 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1310 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1311 "for bad sub-directories: rc = %d\n",
1312 lfsck_lfsck2name(lfsck), rc);
1315 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1316 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1317 struct lu_fid tfid = bk->lb_lpf_fid;
1319 /* Invalid FID record in the bookmark file, reset it. */
1320 fid_zero(&bk->lb_lpf_fid);
1321 rc = lfsck_bookmark_store(env, lfsck);
1323 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1324 " in the bookmark file: rc = %d\n",
1325 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1330 child1 = lfsck_object_find_by_dev(env, dev,
1332 if (IS_ERR(child1)) {
1337 if (unlikely(!dt_object_exists(child1) ||
1338 dt_object_remote(child1)) ||
1339 !S_ISDIR(lfsck_object_type(child1))) {
1340 /* Invalid FID record in the bookmark file,
1342 fid_zero(&bk->lb_lpf_fid);
1343 rc = lfsck_bookmark_store(env, lfsck);
1345 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1346 " in the bookmark file: rc = %d\n",
1347 lfsck_lfsck2name(lfsck),
1348 PFID(lfsck_dto2fid(child1)), rc);
1353 lu_object_put(env, &child1->do_lu);
1355 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1356 GOTO(put, rc = -ENOTDIR);
1362 snprintf(name, 8, "MDT%04x", node);
1363 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1364 (const struct dt_key *)name, BYPASS_CAPA);
1365 if (rc == -ENOENT) {
1366 if (!fid_is_zero(&bk->lb_lpf_fid))
1375 /* Invalid FID in the name entry, remove the name entry. */
1376 if (!fid_is_norm(cfid)) {
1377 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1384 child2 = lfsck_object_find_by_dev(env, dev, cfid);
1386 GOTO(put, rc = PTR_ERR(child2));
1388 if (unlikely(!dt_object_exists(child2) ||
1389 dt_object_remote(child2)) ||
1390 !S_ISDIR(lfsck_object_type(child2))) {
1391 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1398 if (unlikely(!dt_try_as_dir(env, child2)))
1399 GOTO(put, rc = -ENOTDIR);
1401 if (child1 == NULL) {
1402 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1403 pfid, LVLT_BY_NAMEENTRY);
1404 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1405 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1406 pfid, LVLT_BY_BOOKMARK);
1407 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1408 rc = lfsck_verify_lpf_pairs(env, lfsck, child2,
1412 if (lfsck->li_lpf_obj == NULL) {
1413 lu_object_get(&child2->do_lu);
1414 lfsck->li_lpf_obj = child2;
1417 cname = lfsck_name_get_const(env, name, strlen(name));
1418 rc = lfsck_verify_linkea(env, dev, child2, cname, &LU_LPF_FID);
1425 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name,
1426 pfid, LVLT_BY_BOOKMARK);
1431 if (lfsck->li_lpf_obj != NULL) {
1432 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1433 lu_object_put(env, &lfsck->li_lpf_obj->do_lu);
1434 lfsck->li_lpf_obj = NULL;
1437 } else if (rc == 0) {
1438 rc = lfsck_create_lpf(env, lfsck);
1441 if (child2 != NULL && !IS_ERR(child2))
1442 lu_object_put(env, &child2->do_lu);
1443 if (child1 != NULL && !IS_ERR(child1))
1444 lu_object_put(env, &child1->do_lu);
1449 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1451 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1452 struct seq_server_site *ss;
1457 ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1458 if (unlikely(ss == NULL))
1461 OBD_ALLOC_PTR(lfsck->li_seq);
1462 if (lfsck->li_seq == NULL)
1465 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1467 GOTO(out, rc = -ENOMEM);
1469 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1470 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1472 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1476 if (fid_is_sane(&bk->lb_last_fid))
1477 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1482 OBD_FREE_PTR(lfsck->li_seq);
1483 lfsck->li_seq = NULL;
1488 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1490 if (lfsck->li_seq != NULL) {
1491 seq_client_fini(lfsck->li_seq);
1492 OBD_FREE_PTR(lfsck->li_seq);
1493 lfsck->li_seq = NULL;
1497 void lfsck_instance_cleanup(const struct lu_env *env,
1498 struct lfsck_instance *lfsck)
1500 struct ptlrpc_thread *thread = &lfsck->li_thread;
1501 struct lfsck_component *com;
1502 struct lfsck_component *next;
1503 struct lfsck_lmv_unit *llu;
1504 struct lfsck_lmv_unit *llu_next;
1505 struct lfsck_lmv *llmv;
1508 LASSERT(list_empty(&lfsck->li_link));
1509 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1511 if (lfsck->li_obj_oit != NULL) {
1512 lu_object_put_nocache(env, &lfsck->li_obj_oit->do_lu);
1513 lfsck->li_obj_oit = NULL;
1516 LASSERT(lfsck->li_obj_dir == NULL);
1517 LASSERT(lfsck->li_lmv == NULL);
1519 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1520 llmv = &llu->llu_lmv;
1522 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1523 "still in using: %u\n",
1524 atomic_read(&llmv->ll_ref));
1526 lfsck_lmv_put(env, llmv);
1529 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1530 lfsck_component_cleanup(env, com);
1533 LASSERT(list_empty(&lfsck->li_list_dir));
1535 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1537 lfsck_component_cleanup(env, com);
1540 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1541 lfsck_component_cleanup(env, com);
1544 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1545 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1547 if (lfsck->li_bookmark_obj != NULL) {
1548 lu_object_put_nocache(env, &lfsck->li_bookmark_obj->do_lu);
1549 lfsck->li_bookmark_obj = NULL;
1552 if (lfsck->li_lpf_obj != NULL) {
1553 lu_object_put(env, &lfsck->li_lpf_obj->do_lu);
1554 lfsck->li_lpf_obj = NULL;
1557 if (lfsck->li_lpf_root_obj != NULL) {
1558 lu_object_put(env, &lfsck->li_lpf_root_obj->do_lu);
1559 lfsck->li_lpf_root_obj = NULL;
1562 if (lfsck->li_los != NULL) {
1563 local_oid_storage_fini(env, lfsck->li_los);
1564 lfsck->li_los = NULL;
1567 lfsck_fid_fini(lfsck);
1569 OBD_FREE_PTR(lfsck);
1572 static inline struct lfsck_instance *
1573 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1575 struct lfsck_instance *lfsck;
1577 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1578 if (lfsck->li_bottom == key) {
1580 lfsck_instance_get(lfsck);
1582 list_del_init(&lfsck->li_link);
1591 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1594 struct lfsck_instance *lfsck;
1596 spin_lock(&lfsck_instance_lock);
1597 lfsck = __lfsck_instance_find(key, ref, unlink);
1598 spin_unlock(&lfsck_instance_lock);
1603 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1605 struct lfsck_instance *tmp;
1607 spin_lock(&lfsck_instance_lock);
1608 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1609 if (lfsck->li_bottom == tmp->li_bottom) {
1610 spin_unlock(&lfsck_instance_lock);
1615 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1616 spin_unlock(&lfsck_instance_lock);
1620 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1625 bool newline = (bits != 0 ? false : true);
1627 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1629 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1632 if (names[i] != NULL) {
1636 seq_printf(m, "%s%c", names[i],
1637 newline ? '\n' : ',');
1643 seq_printf(m, "\n");
1647 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix)
1650 seq_printf(m, "%s: "LPU64" seconds\n", prefix,
1651 cfs_time_current_sec() - time);
1653 seq_printf(m, "%s: N/A\n", prefix);
1657 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1660 if (fid_is_zero(&pos->lp_dir_parent)) {
1661 if (pos->lp_oit_cookie == 0)
1662 seq_printf(m, "%s: N/A, N/A, N/A\n",
1665 seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1666 prefix, pos->lp_oit_cookie);
1668 seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1669 prefix, pos->lp_oit_cookie,
1670 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1675 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1676 struct lfsck_position *pos, bool init)
1678 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1680 if (unlikely(lfsck->li_di_oit == NULL)) {
1681 memset(pos, 0, sizeof(*pos));
1685 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1686 if (!lfsck->li_current_oit_processed && !init)
1687 pos->lp_oit_cookie--;
1689 LASSERT(pos->lp_oit_cookie > 0);
1691 if (lfsck->li_di_dir != NULL) {
1692 struct dt_object *dto = lfsck->li_obj_dir;
1694 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1697 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1698 fid_zero(&pos->lp_dir_parent);
1699 pos->lp_dir_cookie = 0;
1701 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1704 fid_zero(&pos->lp_dir_parent);
1705 pos->lp_dir_cookie = 0;
1709 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1713 if (limit != LFSCK_SPEED_NO_LIMIT) {
1714 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1715 lfsck->li_sleep_rate = limit /
1716 msecs_to_jiffies(MSEC_PER_SEC);
1717 lfsck->li_sleep_jif = 1;
1719 lfsck->li_sleep_rate = 1;
1720 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1724 lfsck->li_sleep_jif = 0;
1725 lfsck->li_sleep_rate = 0;
1728 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1729 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1736 void lfsck_control_speed(struct lfsck_instance *lfsck)
1738 struct ptlrpc_thread *thread = &lfsck->li_thread;
1739 struct l_wait_info lwi;
1741 if (lfsck->li_sleep_jif > 0 &&
1742 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1743 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1744 LWI_ON_SIGNAL_NOOP, NULL);
1746 l_wait_event(thread->t_ctl_waitq,
1747 !thread_is_running(thread),
1749 lfsck->li_new_scanned = 0;
1753 void lfsck_control_speed_by_self(struct lfsck_component *com)
1755 struct lfsck_instance *lfsck = com->lc_lfsck;
1756 struct ptlrpc_thread *thread = &lfsck->li_thread;
1757 struct l_wait_info lwi;
1759 if (lfsck->li_sleep_jif > 0 &&
1760 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1761 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1762 LWI_ON_SIGNAL_NOOP, NULL);
1764 l_wait_event(thread->t_ctl_waitq,
1765 !thread_is_running(thread),
1767 com->lc_new_scanned = 0;
1771 static struct lfsck_thread_args *
1772 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1773 struct lfsck_component *com,
1774 struct lfsck_start_param *lsp)
1776 struct lfsck_thread_args *lta;
1781 return ERR_PTR(-ENOMEM);
1783 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1789 lta->lta_lfsck = lfsck_instance_get(lfsck);
1791 lta->lta_com = lfsck_component_get(com);
1798 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1800 if (lta->lta_com != NULL)
1801 lfsck_component_put(<a->lta_env, lta->lta_com);
1802 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1803 lu_env_fini(<a->lta_env);
1807 struct lfsck_assistant_data *
1808 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1811 struct lfsck_assistant_data *lad;
1815 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1816 if (lad->lad_bitmap == NULL) {
1821 INIT_LIST_HEAD(&lad->lad_req_list);
1822 spin_lock_init(&lad->lad_lock);
1823 INIT_LIST_HEAD(&lad->lad_ost_list);
1824 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1825 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1826 INIT_LIST_HEAD(&lad->lad_mdt_list);
1827 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1828 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1829 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1831 lad->lad_name = name;
1838 * Generic LFSCK asynchronous communication interpretor function.
1839 * The LFSCK RPC reply for both the event notification and status
1840 * querying will be handled here.
1842 * \param[in] env pointer to the thread context
1843 * \param[in] req pointer to the LFSCK request
1844 * \param[in] args pointer to the lfsck_async_interpret_args
1845 * \param[in] rc the result for handling the LFSCK request
1847 * \retval 0 for success
1848 * \retval negative error number on failure
1850 int lfsck_async_interpret_common(const struct lu_env *env,
1851 struct ptlrpc_request *req,
1854 struct lfsck_async_interpret_args *laia = args;
1855 struct lfsck_component *com = laia->laia_com;
1856 struct lfsck_assistant_data *lad = com->lc_data;
1857 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1858 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1859 struct lfsck_request *lr = laia->laia_lr;
1861 LASSERT(com->lc_lfsck->li_master);
1863 switch (lr->lr_event) {
1866 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1868 lfsck_lfsck2name(com->lc_lfsck),
1869 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1870 ltd->ltd_index, lad->lad_name, rc);
1872 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1873 struct lfsck_layout *lo = com->lc_file_ram;
1875 if (lr->lr_flags & LEF_TO_OST)
1876 lfsck_lad_set_bitmap(env, com,
1879 lo->ll_flags |= LF_INCOMPLETE;
1881 struct lfsck_namespace *ns = com->lc_file_ram;
1883 /* If some MDT does not join the namespace
1884 * LFSCK, then we cannot know whether there
1885 * is some name entry on such MDT that with
1886 * the referenced MDT-object on this MDT or
1887 * not. So the namespace LFSCK on this MDT
1888 * cannot handle orphan MDT-objects properly.
1889 * So we mark the LFSCK as LF_INCOMPLETE and
1890 * skip orphan MDT-objects handling. */
1891 ns->ln_flags |= LF_INCOMPLETE;
1896 spin_lock(<ds->ltd_lock);
1897 if (ltd->ltd_dead) {
1898 spin_unlock(<ds->ltd_lock);
1902 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1903 struct list_head *list;
1904 struct list_head *phase_list;
1906 if (ltd->ltd_layout_done) {
1907 spin_unlock(<ds->ltd_lock);
1911 if (lr->lr_flags & LEF_TO_OST) {
1912 list = &lad->lad_ost_list;
1913 phase_list = &lad->lad_ost_phase1_list;
1915 list = &lad->lad_mdt_list;
1916 phase_list = &lad->lad_mdt_phase1_list;
1919 if (list_empty(<d->ltd_layout_list))
1920 list_add_tail(<d->ltd_layout_list, list);
1921 if (list_empty(<d->ltd_layout_phase_list))
1922 list_add_tail(<d->ltd_layout_phase_list,
1925 if (ltd->ltd_namespace_done) {
1926 spin_unlock(<ds->ltd_lock);
1930 if (list_empty(<d->ltd_namespace_list))
1931 list_add_tail(<d->ltd_namespace_list,
1932 &lad->lad_mdt_list);
1933 if (list_empty(<d->ltd_namespace_phase_list))
1934 list_add_tail(<d->ltd_namespace_phase_list,
1935 &lad->lad_mdt_phase1_list);
1937 spin_unlock(<ds->ltd_lock);
1940 case LE_PHASE1_DONE:
1941 case LE_PHASE2_DONE:
1943 if (rc != 0 && rc != -EALREADY)
1944 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
1945 "event = %d, rc = %d\n",
1946 lfsck_lfsck2name(com->lc_lfsck),
1947 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1948 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
1951 struct lfsck_reply *reply;
1952 struct list_head *list;
1953 struct list_head *phase_list;
1955 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1956 list = <d->ltd_layout_list;
1957 phase_list = <d->ltd_layout_phase_list;
1959 list = <d->ltd_namespace_list;
1960 phase_list = <d->ltd_namespace_phase_list;
1964 spin_lock(<ds->ltd_lock);
1965 list_del_init(phase_list);
1966 list_del_init(list);
1967 spin_unlock(<ds->ltd_lock);
1971 reply = req_capsule_server_get(&req->rq_pill,
1973 if (reply == NULL) {
1975 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
1976 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1978 spin_lock(<ds->ltd_lock);
1979 list_del_init(phase_list);
1980 list_del_init(list);
1981 spin_unlock(<ds->ltd_lock);
1985 switch (reply->lr_status) {
1986 case LS_SCANNING_PHASE1:
1988 case LS_SCANNING_PHASE2:
1989 spin_lock(<ds->ltd_lock);
1990 list_del_init(phase_list);
1991 if (ltd->ltd_dead) {
1992 spin_unlock(<ds->ltd_lock);
1996 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1997 if (ltd->ltd_layout_done) {
1998 spin_unlock(<ds->ltd_lock);
2002 if (lr->lr_flags & LEF_TO_OST)
2003 list_add_tail(phase_list,
2004 &lad->lad_ost_phase2_list);
2006 list_add_tail(phase_list,
2007 &lad->lad_mdt_phase2_list);
2009 if (ltd->ltd_namespace_done) {
2010 spin_unlock(<ds->ltd_lock);
2014 list_add_tail(phase_list,
2015 &lad->lad_mdt_phase2_list);
2017 spin_unlock(<ds->ltd_lock);
2020 spin_lock(<ds->ltd_lock);
2021 list_del_init(phase_list);
2022 list_del_init(list);
2023 spin_unlock(<ds->ltd_lock);
2029 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2030 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2034 if (!laia->laia_shared) {
2036 lfsck_component_put(env, com);
2042 static void lfsck_interpret(const struct lu_env *env,
2043 struct lfsck_instance *lfsck,
2044 struct ptlrpc_request *req, void *args, int result)
2046 struct lfsck_async_interpret_args *laia = args;
2047 struct lfsck_component *com;
2049 LASSERT(laia->laia_com == NULL);
2050 LASSERT(laia->laia_shared);
2052 spin_lock(&lfsck->li_lock);
2053 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2054 laia->laia_com = com;
2055 lfsck_async_interpret_common(env, req, laia, result);
2058 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2059 laia->laia_com = com;
2060 lfsck_async_interpret_common(env, req, laia, result);
2062 spin_unlock(&lfsck->li_lock);
2065 static int lfsck_stop_notify(const struct lu_env *env,
2066 struct lfsck_instance *lfsck,
2067 struct lfsck_tgt_descs *ltds,
2068 struct lfsck_tgt_desc *ltd, __u16 type)
2070 struct lfsck_component *com;
2074 LASSERT(lfsck->li_master);
2076 spin_lock(&lfsck->li_lock);
2077 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2079 com = __lfsck_component_find(lfsck, type,
2080 &lfsck->li_list_double_scan);
2082 lfsck_component_get(com);
2083 spin_unlock(&lfsck->li_lock);
2086 struct lfsck_thread_info *info = lfsck_env_info(env);
2087 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2088 struct lfsck_request *lr = &info->lti_lr;
2089 struct lfsck_assistant_data *lad = com->lc_data;
2090 struct list_head *list;
2091 struct list_head *phase_list;
2092 struct ptlrpc_request_set *set;
2094 set = ptlrpc_prep_set();
2096 lfsck_component_put(env, com);
2101 if (type == LFSCK_TYPE_LAYOUT) {
2102 list = <d->ltd_layout_list;
2103 phase_list = <d->ltd_layout_phase_list;
2105 list = <d->ltd_namespace_list;
2106 phase_list = <d->ltd_namespace_phase_list;
2109 spin_lock(<ds->ltd_lock);
2110 if (list_empty(list)) {
2111 LASSERT(list_empty(phase_list));
2112 spin_unlock(<ds->ltd_lock);
2113 ptlrpc_set_destroy(set);
2118 list_del_init(phase_list);
2119 list_del_init(list);
2120 spin_unlock(<ds->ltd_lock);
2122 memset(lr, 0, sizeof(*lr));
2123 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2124 lr->lr_event = LE_PEER_EXIT;
2125 lr->lr_active = type;
2126 lr->lr_status = LS_CO_PAUSED;
2127 if (ltds == &lfsck->li_ost_descs)
2128 lr->lr_flags = LEF_TO_OST;
2130 laia->laia_com = com;
2131 laia->laia_ltds = ltds;
2132 atomic_inc(<d->ltd_ref);
2133 laia->laia_ltd = ltd;
2135 laia->laia_shared = 0;
2137 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2138 lfsck_async_interpret_common,
2139 laia, LFSCK_NOTIFY);
2141 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2142 "co-stop for %s: rc = %d\n",
2143 lfsck_lfsck2name(lfsck),
2144 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2145 ltd->ltd_index, lad->lad_name, rc);
2148 rc = ptlrpc_set_wait(set);
2151 ptlrpc_set_destroy(set);
2152 lfsck_component_put(env, com);
2158 static int lfsck_async_interpret(const struct lu_env *env,
2159 struct ptlrpc_request *req,
2162 struct lfsck_async_interpret_args *laia = args;
2163 struct lfsck_instance *lfsck;
2165 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2167 lfsck_interpret(env, lfsck, req, laia, rc);
2168 lfsck_tgt_put(laia->laia_ltd);
2169 if (rc != 0 && laia->laia_result != -EALREADY)
2170 laia->laia_result = rc;
2175 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2176 struct lfsck_request *lr,
2177 struct ptlrpc_request_set *set,
2178 ptlrpc_interpterer_t interpreter,
2179 void *args, int request)
2181 struct lfsck_async_interpret_args *laia;
2182 struct ptlrpc_request *req;
2183 struct lfsck_request *tmp;
2184 struct req_format *format;
2189 format = &RQF_LFSCK_NOTIFY;
2192 format = &RQF_LFSCK_QUERY;
2195 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2196 exp->exp_obd->obd_name, request, -EINVAL);
2200 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2204 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2206 ptlrpc_request_free(req);
2211 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2213 ptlrpc_request_set_replen(req);
2215 laia = ptlrpc_req_async_args(req);
2216 *laia = *(struct lfsck_async_interpret_args *)args;
2217 if (laia->laia_com != NULL)
2218 lfsck_component_get(laia->laia_com);
2219 req->rq_interpret_reply = interpreter;
2220 ptlrpc_set_add_req(set, req);
2225 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2226 struct lfsck_start_param *lsp)
2228 struct lfsck_instance *lfsck = com->lc_lfsck;
2229 struct lfsck_assistant_data *lad = com->lc_data;
2230 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2231 struct ptlrpc_thread *athread = &lad->lad_thread;
2232 struct lfsck_thread_args *lta;
2233 struct task_struct *task;
2237 lad->lad_assistant_status = 0;
2238 lad->lad_post_result = 0;
2239 lad->lad_to_post = 0;
2240 lad->lad_to_double_scan = 0;
2241 lad->lad_in_double_scan = 0;
2243 thread_set_flags(athread, 0);
2245 lta = lfsck_thread_args_init(lfsck, com, lsp);
2247 RETURN(PTR_ERR(lta));
2249 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2252 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2253 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2254 lfsck_thread_args_fini(lta);
2256 struct l_wait_info lwi = { 0 };
2258 l_wait_event(mthread->t_ctl_waitq,
2259 thread_is_running(athread) ||
2260 thread_is_stopped(athread),
2262 if (unlikely(!thread_is_running(athread)))
2263 rc = lad->lad_assistant_status;
2271 int lfsck_checkpoint_generic(const struct lu_env *env,
2272 struct lfsck_component *com)
2274 struct lfsck_assistant_data *lad = com->lc_data;
2275 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2276 struct ptlrpc_thread *athread = &lad->lad_thread;
2277 struct l_wait_info lwi = { 0 };
2279 if (com->lc_new_checked == 0)
2280 return LFSCK_CHECKPOINT_SKIP;
2282 l_wait_event(mthread->t_ctl_waitq,
2283 list_empty(&lad->lad_req_list) ||
2284 !thread_is_running(mthread) ||
2285 thread_is_stopped(athread),
2288 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2289 return LFSCK_CHECKPOINT_SKIP;
2294 void lfsck_post_generic(const struct lu_env *env,
2295 struct lfsck_component *com, int *result)
2297 struct lfsck_assistant_data *lad = com->lc_data;
2298 struct ptlrpc_thread *athread = &lad->lad_thread;
2299 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2300 struct l_wait_info lwi = { 0 };
2302 lad->lad_post_result = *result;
2305 lad->lad_to_post = 1;
2307 wake_up_all(&athread->t_ctl_waitq);
2308 l_wait_event(mthread->t_ctl_waitq,
2309 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2310 thread_is_stopped(athread),
2313 if (lad->lad_assistant_status < 0)
2314 *result = lad->lad_assistant_status;
2317 int lfsck_double_scan_generic(const struct lu_env *env,
2318 struct lfsck_component *com, int status)
2320 struct lfsck_assistant_data *lad = com->lc_data;
2321 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2322 struct ptlrpc_thread *athread = &lad->lad_thread;
2323 struct l_wait_info lwi = { 0 };
2325 if (status != LS_SCANNING_PHASE2)
2328 lad->lad_to_double_scan = 1;
2330 wake_up_all(&athread->t_ctl_waitq);
2331 l_wait_event(mthread->t_ctl_waitq,
2332 lad->lad_in_double_scan ||
2333 thread_is_stopped(athread),
2336 if (lad->lad_assistant_status < 0)
2337 return lad->lad_assistant_status;
2342 void lfsck_quit_generic(const struct lu_env *env,
2343 struct lfsck_component *com)
2345 struct lfsck_assistant_data *lad = com->lc_data;
2346 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2347 struct ptlrpc_thread *athread = &lad->lad_thread;
2348 struct l_wait_info lwi = { 0 };
2351 wake_up_all(&athread->t_ctl_waitq);
2352 l_wait_event(mthread->t_ctl_waitq,
2353 thread_is_init(athread) ||
2354 thread_is_stopped(athread),
2358 /* external interfaces */
2360 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2363 struct lfsck_instance *lfsck;
2367 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2371 lfsck = lfsck_instance_find(key, true, false);
2372 if (likely(lfsck != NULL)) {
2373 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2374 lfsck_instance_put(&env, lfsck);
2383 EXPORT_SYMBOL(lfsck_get_speed);
2385 int lfsck_set_speed(struct dt_device *key, int val)
2388 struct lfsck_instance *lfsck;
2392 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2396 lfsck = lfsck_instance_find(key, true, false);
2397 if (likely(lfsck != NULL)) {
2398 mutex_lock(&lfsck->li_mutex);
2399 if (__lfsck_set_speed(lfsck, val))
2400 rc = lfsck_bookmark_store(&env, lfsck);
2401 mutex_unlock(&lfsck->li_mutex);
2402 lfsck_instance_put(&env, lfsck);
2411 EXPORT_SYMBOL(lfsck_set_speed);
2413 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2416 struct lfsck_instance *lfsck;
2420 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2424 lfsck = lfsck_instance_find(key, true, false);
2425 if (likely(lfsck != NULL)) {
2426 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2427 lfsck_instance_put(&env, lfsck);
2436 EXPORT_SYMBOL(lfsck_get_windows);
2438 int lfsck_set_windows(struct dt_device *key, int val)
2441 struct lfsck_instance *lfsck;
2445 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2449 lfsck = lfsck_instance_find(key, true, false);
2450 if (likely(lfsck != NULL)) {
2451 if (val > LFSCK_ASYNC_WIN_MAX) {
2452 CWARN("%s: Too large async window size, which "
2453 "may cause memory issues. The valid range "
2454 "is [0 - %u]. If you do not want to restrict "
2455 "the window size for async requests pipeline, "
2456 "just set it as 0.\n",
2457 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2459 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2460 mutex_lock(&lfsck->li_mutex);
2461 lfsck->li_bookmark_ram.lb_async_windows = val;
2462 rc = lfsck_bookmark_store(&env, lfsck);
2463 mutex_unlock(&lfsck->li_mutex);
2465 lfsck_instance_put(&env, lfsck);
2474 EXPORT_SYMBOL(lfsck_set_windows);
2476 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2479 struct lfsck_instance *lfsck;
2480 struct lfsck_component *com;
2484 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2488 lfsck = lfsck_instance_find(key, true, false);
2489 if (likely(lfsck != NULL)) {
2490 com = lfsck_component_find(lfsck, type);
2491 if (likely(com != NULL)) {
2492 rc = com->lc_ops->lfsck_dump(&env, com, m);
2493 lfsck_component_put(&env, com);
2498 lfsck_instance_put(&env, lfsck);
2507 EXPORT_SYMBOL(lfsck_dump);
2509 static int lfsck_stop_all(const struct lu_env *env,
2510 struct lfsck_instance *lfsck,
2511 struct lfsck_stop *stop)
2513 struct lfsck_thread_info *info = lfsck_env_info(env);
2514 struct lfsck_request *lr = &info->lti_lr;
2515 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2516 struct ptlrpc_request_set *set;
2517 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2518 struct lfsck_tgt_desc *ltd;
2519 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2525 LASSERT(stop->ls_flags & LPF_BROADCAST);
2527 set = ptlrpc_prep_set();
2528 if (unlikely(set == NULL))
2531 memset(lr, 0, sizeof(*lr));
2532 lr->lr_event = LE_STOP;
2533 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2534 lr->lr_status = stop->ls_status;
2535 lr->lr_version = bk->lb_version;
2536 lr->lr_active = LFSCK_TYPES_ALL;
2537 lr->lr_param = stop->ls_flags;
2539 laia->laia_com = NULL;
2540 laia->laia_ltds = ltds;
2542 laia->laia_result = 0;
2543 laia->laia_shared = 1;
2545 down_read(<ds->ltd_rw_sem);
2546 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2547 ltd = lfsck_tgt_get(ltds, idx);
2548 LASSERT(ltd != NULL);
2550 laia->laia_ltd = ltd;
2551 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2552 lfsck_async_interpret, laia,
2555 lfsck_interpret(env, lfsck, NULL, laia, rc);
2557 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2558 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2562 up_read(<ds->ltd_rw_sem);
2564 rc = ptlrpc_set_wait(set);
2565 ptlrpc_set_destroy(set);
2568 rc = laia->laia_result;
2570 if (rc == -EALREADY)
2574 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2575 lfsck_lfsck2name(lfsck), rc);
2577 RETURN(rc != 0 ? rc : rc1);
2580 static int lfsck_start_all(const struct lu_env *env,
2581 struct lfsck_instance *lfsck,
2582 struct lfsck_start *start)
2584 struct lfsck_thread_info *info = lfsck_env_info(env);
2585 struct lfsck_request *lr = &info->lti_lr;
2586 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2587 struct ptlrpc_request_set *set;
2588 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2589 struct lfsck_tgt_desc *ltd;
2590 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2595 LASSERT(start->ls_flags & LPF_BROADCAST);
2597 set = ptlrpc_prep_set();
2598 if (unlikely(set == NULL))
2601 memset(lr, 0, sizeof(*lr));
2602 lr->lr_event = LE_START;
2603 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2604 lr->lr_speed = bk->lb_speed_limit;
2605 lr->lr_version = bk->lb_version;
2606 lr->lr_active = start->ls_active;
2607 lr->lr_param = start->ls_flags;
2608 lr->lr_async_windows = bk->lb_async_windows;
2609 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2610 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2613 laia->laia_com = NULL;
2614 laia->laia_ltds = ltds;
2616 laia->laia_result = 0;
2617 laia->laia_shared = 1;
2619 down_read(<ds->ltd_rw_sem);
2620 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2621 ltd = lfsck_tgt_get(ltds, idx);
2622 LASSERT(ltd != NULL);
2624 laia->laia_ltd = ltd;
2625 ltd->ltd_layout_done = 0;
2626 ltd->ltd_namespace_done = 0;
2627 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2628 lfsck_async_interpret, laia,
2631 lfsck_interpret(env, lfsck, NULL, laia, rc);
2633 CERROR("%s: cannot notify MDT %x for LFSCK "
2634 "start, failout: rc = %d\n",
2635 lfsck_lfsck2name(lfsck), idx, rc);
2639 up_read(<ds->ltd_rw_sem);
2642 ptlrpc_set_destroy(set);
2647 rc = ptlrpc_set_wait(set);
2648 ptlrpc_set_destroy(set);
2651 rc = laia->laia_result;
2654 struct lfsck_stop *stop = &info->lti_stop;
2656 CERROR("%s: cannot start LFSCK on some MDTs, "
2657 "stop all: rc = %d\n",
2658 lfsck_lfsck2name(lfsck), rc);
2659 if (rc != -EALREADY) {
2660 stop->ls_status = LS_FAILED;
2661 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2662 lfsck_stop_all(env, lfsck, stop);
2669 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2670 struct lfsck_start_param *lsp)
2672 struct lfsck_start *start = lsp->lsp_start;
2673 struct lfsck_instance *lfsck;
2674 struct lfsck_bookmark *bk;
2675 struct ptlrpc_thread *thread;
2676 struct lfsck_component *com;
2677 struct l_wait_info lwi = { 0 };
2678 struct lfsck_thread_args *lta;
2679 struct task_struct *task;
2686 lfsck = lfsck_instance_find(key, true, false);
2687 if (unlikely(lfsck == NULL))
2690 /* System is not ready, try again later. */
2691 if (unlikely(lfsck->li_namespace == NULL))
2692 GOTO(put, rc = -EAGAIN);
2694 /* start == NULL means auto trigger paused LFSCK. */
2695 if ((start == NULL) &&
2696 (list_empty(&lfsck->li_list_scan) ||
2697 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2700 bk = &lfsck->li_bookmark_ram;
2701 thread = &lfsck->li_thread;
2702 mutex_lock(&lfsck->li_mutex);
2703 spin_lock(&lfsck->li_lock);
2704 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2706 if (unlikely(start == NULL)) {
2707 spin_unlock(&lfsck->li_lock);
2711 while (start->ls_active != 0) {
2712 if (!(type & start->ls_active)) {
2717 com = __lfsck_component_find(lfsck, type,
2718 &lfsck->li_list_scan);
2720 com = __lfsck_component_find(lfsck, type,
2721 &lfsck->li_list_double_scan);
2727 if (com->lc_ops->lfsck_join != NULL) {
2728 rc = com->lc_ops->lfsck_join( env, com, lsp);
2729 if (rc != 0 && rc != -EALREADY)
2732 start->ls_active &= ~type;
2735 spin_unlock(&lfsck->li_lock);
2738 spin_unlock(&lfsck->li_lock);
2740 lfsck->li_status = 0;
2741 lfsck->li_oit_over = 0;
2742 lfsck->li_start_unplug = 0;
2743 lfsck->li_drop_dryrun = 0;
2744 lfsck->li_new_scanned = 0;
2746 /* For auto trigger. */
2750 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2751 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2752 lfsck_lfsck2name(lfsck));
2754 GOTO(out, rc = -EPERM);
2757 start->ls_version = bk->lb_version;
2759 if (start->ls_active != 0) {
2760 struct lfsck_component *next;
2762 if (start->ls_active == LFSCK_TYPES_ALL)
2763 start->ls_active = LFSCK_TYPES_SUPPORTED;
2765 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2766 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2767 GOTO(out, rc = -ENOTSUPP);
2770 list_for_each_entry_safe(com, next,
2771 &lfsck->li_list_scan, lc_link) {
2772 if (!(com->lc_type & start->ls_active)) {
2773 rc = com->lc_ops->lfsck_post(env, com, 0,
2780 while (start->ls_active != 0) {
2781 if (type & start->ls_active) {
2782 com = __lfsck_component_find(lfsck, type,
2783 &lfsck->li_list_idle);
2785 /* The component status will be updated
2786 * when its prep() is called later by
2787 * the LFSCK main engine. */
2788 list_move_tail(&com->lc_link,
2789 &lfsck->li_list_scan);
2790 start->ls_active &= ~type;
2796 if (list_empty(&lfsck->li_list_scan)) {
2797 /* The speed limit will be used to control both the LFSCK and
2798 * low layer scrub (if applied), need to be handled firstly. */
2799 if (start->ls_valid & LSV_SPEED_LIMIT) {
2800 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2801 rc = lfsck_bookmark_store(env, lfsck);
2810 if (start->ls_flags & LPF_RESET)
2811 flags |= DOIF_RESET;
2813 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2817 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2818 start->ls_active |= com->lc_type;
2819 if (flags & DOIF_RESET) {
2820 rc = com->lc_ops->lfsck_reset(env, com, false);
2827 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2828 if (bk->lb_param & LPF_DRYRUN)
2829 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2831 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2832 valid |= DOIV_ERROR_HANDLE;
2833 if (start->ls_flags & LPF_FAILOUT)
2834 flags |= DOIF_FAILOUT;
2837 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2838 valid |= DOIV_DRYRUN;
2839 if (start->ls_flags & LPF_DRYRUN)
2840 flags |= DOIF_DRYRUN;
2843 if (!list_empty(&lfsck->li_list_scan))
2844 flags |= DOIF_OUTUSED;
2846 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2847 thread_set_flags(thread, 0);
2848 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2850 GOTO(out, rc = PTR_ERR(lta));
2852 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2853 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2856 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2857 lfsck_lfsck2name(lfsck), rc);
2858 lfsck_thread_args_fini(lta);
2863 l_wait_event(thread->t_ctl_waitq,
2864 thread_is_running(thread) ||
2865 thread_is_stopped(thread),
2867 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2868 lfsck->li_start_unplug = 1;
2869 wake_up_all(&thread->t_ctl_waitq);
2874 /* release lfsck::li_mutex to avoid deadlock. */
2875 mutex_unlock(&lfsck->li_mutex);
2876 rc = lfsck_start_all(env, lfsck, start);
2878 spin_lock(&lfsck->li_lock);
2879 if (thread_is_stopped(thread)) {
2880 spin_unlock(&lfsck->li_lock);
2882 lfsck->li_status = LS_FAILED;
2883 lfsck->li_flags = 0;
2884 thread_set_flags(thread, SVC_STOPPING);
2885 spin_unlock(&lfsck->li_lock);
2887 lfsck->li_start_unplug = 1;
2888 wake_up_all(&thread->t_ctl_waitq);
2889 l_wait_event(thread->t_ctl_waitq,
2890 thread_is_stopped(thread),
2894 lfsck->li_start_unplug = 1;
2895 wake_up_all(&thread->t_ctl_waitq);
2901 mutex_unlock(&lfsck->li_mutex);
2904 lfsck_instance_put(env, lfsck);
2906 return rc < 0 ? rc : 0;
2908 EXPORT_SYMBOL(lfsck_start);
2910 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
2911 struct lfsck_stop *stop)
2913 struct lfsck_instance *lfsck;
2914 struct ptlrpc_thread *thread;
2915 struct l_wait_info lwi = { 0 };
2920 lfsck = lfsck_instance_find(key, true, false);
2921 if (unlikely(lfsck == NULL))
2924 thread = &lfsck->li_thread;
2925 /* release lfsck::li_mutex to avoid deadlock. */
2926 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
2927 if (!lfsck->li_master) {
2928 CERROR("%s: only allow to specify '-A' via MDS\n",
2929 lfsck_lfsck2name(lfsck));
2931 GOTO(out, rc = -EPERM);
2934 rc1 = lfsck_stop_all(env, lfsck, stop);
2937 mutex_lock(&lfsck->li_mutex);
2938 spin_lock(&lfsck->li_lock);
2939 /* no error if LFSCK is already stopped, or was never started */
2940 if (thread_is_init(thread) || thread_is_stopped(thread)) {
2941 spin_unlock(&lfsck->li_lock);
2946 lfsck->li_status = stop->ls_status;
2947 lfsck->li_flags = stop->ls_flags;
2949 lfsck->li_status = LS_STOPPED;
2950 lfsck->li_flags = 0;
2953 thread_set_flags(thread, SVC_STOPPING);
2954 spin_unlock(&lfsck->li_lock);
2956 wake_up_all(&thread->t_ctl_waitq);
2957 l_wait_event(thread->t_ctl_waitq,
2958 thread_is_stopped(thread),
2964 mutex_unlock(&lfsck->li_mutex);
2965 lfsck_instance_put(env, lfsck);
2967 return rc != 0 ? rc : rc1;
2969 EXPORT_SYMBOL(lfsck_stop);
2971 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
2972 struct lfsck_request *lr, struct thandle *th)
2974 int rc = -EOPNOTSUPP;
2977 switch (lr->lr_event) {
2979 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
2980 struct lfsck_start_param lsp;
2982 memset(start, 0, sizeof(*start));
2983 start->ls_valid = lr->lr_valid;
2984 start->ls_speed_limit = lr->lr_speed;
2985 start->ls_version = lr->lr_version;
2986 start->ls_active = lr->lr_active;
2987 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2988 start->ls_async_windows = lr->lr_async_windows;
2990 lsp.lsp_start = start;
2991 lsp.lsp_index = lr->lr_index;
2992 lsp.lsp_index_valid = 1;
2993 rc = lfsck_start(env, key, &lsp);
2997 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2999 memset(stop, 0, sizeof(*stop));
3000 stop->ls_status = lr->lr_status;
3001 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3002 rc = lfsck_stop(env, key, stop);
3005 case LE_PHASE1_DONE:
3006 case LE_PHASE2_DONE:
3007 case LE_FID_ACCESSED:
3009 case LE_CONDITIONAL_DESTROY:
3010 case LE_SKIP_NLINK_DECLARE:
3012 case LE_SET_LMV_MASTER:
3013 case LE_SET_LMV_SLAVE:
3014 case LE_PAIRS_VERIFY: {
3015 struct lfsck_instance *lfsck;
3016 struct lfsck_component *com;
3018 lfsck = lfsck_instance_find(key, true, false);
3019 if (unlikely(lfsck == NULL))
3022 com = lfsck_component_find(lfsck, lr->lr_active);
3023 if (likely(com != NULL)) {
3024 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3025 lfsck_component_put(env, com);
3028 lfsck_instance_put(env, lfsck);
3037 EXPORT_SYMBOL(lfsck_in_notify);
3039 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3040 struct lfsck_request *lr)
3042 struct lfsck_instance *lfsck;
3043 struct lfsck_component *com;
3047 lfsck = lfsck_instance_find(key, true, false);
3048 if (unlikely(lfsck == NULL))
3051 com = lfsck_component_find(lfsck, lr->lr_active);
3052 if (likely(com != NULL)) {
3053 rc = com->lc_ops->lfsck_query(env, com);
3054 lfsck_component_put(env, com);
3059 lfsck_instance_put(env, lfsck);
3063 EXPORT_SYMBOL(lfsck_query);
3065 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3066 struct ldlm_namespace *ns)
3068 struct lfsck_instance *lfsck;
3071 lfsck = lfsck_instance_find(key, true, false);
3072 if (likely(lfsck != NULL)) {
3073 lfsck->li_namespace = ns;
3074 lfsck_instance_put(env, lfsck);
3080 EXPORT_SYMBOL(lfsck_register_namespace);
3082 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3083 struct dt_device *next, struct obd_device *obd,
3084 lfsck_out_notify notify, void *notify_data, bool master)
3086 struct lfsck_instance *lfsck;
3087 struct dt_object *root = NULL;
3088 struct dt_object *obj = NULL;
3089 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3093 lfsck = lfsck_instance_find(key, false, false);
3094 if (unlikely(lfsck != NULL))
3097 OBD_ALLOC_PTR(lfsck);
3101 mutex_init(&lfsck->li_mutex);
3102 spin_lock_init(&lfsck->li_lock);
3103 INIT_LIST_HEAD(&lfsck->li_link);
3104 INIT_LIST_HEAD(&lfsck->li_list_scan);
3105 INIT_LIST_HEAD(&lfsck->li_list_dir);
3106 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3107 INIT_LIST_HEAD(&lfsck->li_list_idle);
3108 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3109 atomic_set(&lfsck->li_ref, 1);
3110 atomic_set(&lfsck->li_double_scan_count, 0);
3111 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3112 lfsck->li_out_notify = notify;
3113 lfsck->li_out_notify_data = notify_data;
3114 lfsck->li_next = next;
3115 lfsck->li_bottom = key;
3116 lfsck->li_obd = obd;
3118 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3122 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3126 fid->f_seq = FID_SEQ_LOCAL_NAME;
3129 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3133 rc = dt_root_get(env, key, fid);
3137 root = dt_locate(env, key, fid);
3139 GOTO(out, rc = PTR_ERR(root));
3141 if (unlikely(!dt_try_as_dir(env, root)))
3142 GOTO(out, rc = -ENOTDIR);
3144 lfsck->li_local_root_fid = *fid;
3146 lfsck->li_master = 1;
3147 if (lfsck_dev_idx(key) == 0) {
3148 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3149 const struct lu_name *cname;
3151 rc = dt_lookup(env, root,
3152 (struct dt_rec *)(&lfsck->li_global_root_fid),
3153 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3157 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3159 GOTO(out, rc = PTR_ERR(obj));
3161 if (unlikely(!dt_try_as_dir(env, obj)))
3162 GOTO(out, rc = -ENOTDIR);
3164 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3165 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3169 lu_object_put(env, &obj->do_lu);
3170 obj = dt_locate(env, key, fid);
3172 GOTO(out, rc = PTR_ERR(obj));
3174 cname = lfsck_name_get_const(env, dotlustre,
3176 rc = lfsck_verify_linkea(env, key, obj, cname,
3177 &lfsck->li_global_root_fid);
3181 if (unlikely(!dt_try_as_dir(env, obj)))
3182 GOTO(out, rc = -ENOTDIR);
3185 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3186 (const struct dt_key *)lostfound,
3191 lu_object_put(env, &obj->do_lu);
3192 obj = dt_locate(env, key, fid);
3194 GOTO(out, rc = PTR_ERR(obj));
3196 cname = lfsck_name_get_const(env, lostfound,
3198 rc = lfsck_verify_linkea(env, key, obj, cname, pfid);
3202 lu_object_put(env, &obj->do_lu);
3207 fid->f_seq = FID_SEQ_LOCAL_FILE;
3208 fid->f_oid = OTABLE_IT_OID;
3210 obj = dt_locate(env, key, fid);
3212 GOTO(out, rc = PTR_ERR(obj));
3214 lu_object_get(&obj->do_lu);
3215 lfsck->li_obj_oit = obj;
3216 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3220 rc = lfsck_bookmark_setup(env, lfsck);
3225 rc = lfsck_fid_init(lfsck);
3229 rc = lfsck_namespace_setup(env, lfsck);
3234 rc = lfsck_layout_setup(env, lfsck);
3238 /* XXX: more LFSCK components initialization to be added here. */
3240 rc = lfsck_instance_add(lfsck);
3242 rc = lfsck_add_target_from_orphan(env, lfsck);
3244 if (obj != NULL && !IS_ERR(obj))
3245 lu_object_put(env, &obj->do_lu);
3246 if (root != NULL && !IS_ERR(root))
3247 lu_object_put(env, &root->do_lu);
3249 lfsck_instance_cleanup(env, lfsck);
3252 EXPORT_SYMBOL(lfsck_register);
3254 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3256 struct lfsck_instance *lfsck;
3258 lfsck = lfsck_instance_find(key, false, true);
3260 lfsck_instance_put(env, lfsck);
3262 EXPORT_SYMBOL(lfsck_degister);
3264 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3265 struct dt_device *tgt, struct obd_export *exp,
3266 __u32 index, bool for_ost)
3268 struct lfsck_instance *lfsck;
3269 struct lfsck_tgt_desc *ltd;
3280 INIT_LIST_HEAD(<d->ltd_orphan_list);
3281 INIT_LIST_HEAD(<d->ltd_layout_list);
3282 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3283 INIT_LIST_HEAD(<d->ltd_namespace_list);
3284 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3285 atomic_set(<d->ltd_ref, 1);
3286 ltd->ltd_index = index;
3288 spin_lock(&lfsck_instance_lock);
3289 lfsck = __lfsck_instance_find(key, true, false);
3290 if (lfsck == NULL) {
3292 list_add_tail(<d->ltd_orphan_list,
3293 &lfsck_ost_orphan_list);
3295 list_add_tail(<d->ltd_orphan_list,
3296 &lfsck_mdt_orphan_list);
3297 spin_unlock(&lfsck_instance_lock);
3301 spin_unlock(&lfsck_instance_lock);
3303 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3307 lfsck_instance_put(env, lfsck);
3311 EXPORT_SYMBOL(lfsck_add_target);
3313 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3314 struct dt_device *tgt, __u32 index, bool for_ost)
3316 struct lfsck_instance *lfsck;
3317 struct lfsck_tgt_descs *ltds;
3318 struct lfsck_tgt_desc *ltd;
3319 struct list_head *head;
3322 head = &lfsck_ost_orphan_list;
3324 head = &lfsck_mdt_orphan_list;
3326 spin_lock(&lfsck_instance_lock);
3327 list_for_each_entry(ltd, head, ltd_orphan_list) {
3328 if (ltd->ltd_tgt == tgt) {
3329 list_del_init(<d->ltd_orphan_list);
3330 spin_unlock(&lfsck_instance_lock);
3338 lfsck = __lfsck_instance_find(key, true, false);
3339 spin_unlock(&lfsck_instance_lock);
3340 if (unlikely(lfsck == NULL))
3344 ltds = &lfsck->li_ost_descs;
3346 ltds = &lfsck->li_mdt_descs;
3348 down_write(<ds->ltd_rw_sem);
3349 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3351 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3354 ltd = LTD_TGT(ltds, index);
3355 if (unlikely(ltd == NULL))
3358 LASSERT(ltds->ltd_tgtnr > 0);
3361 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3362 LTD_TGT(ltds, index) = NULL;
3367 head = &lfsck->li_ost_descs.ltd_orphan;
3369 head = &lfsck->li_mdt_descs.ltd_orphan;
3371 list_for_each_entry(ltd, head, ltd_orphan_list) {
3372 if (ltd->ltd_tgt == tgt) {
3373 list_del_init(<d->ltd_orphan_list);
3379 up_write(<ds->ltd_rw_sem);
3381 spin_lock(<ds->ltd_lock);
3383 spin_unlock(<ds->ltd_lock);
3384 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3385 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3389 lfsck_instance_put(env, lfsck);
3391 EXPORT_SYMBOL(lfsck_del_target);
3393 static int __init lfsck_init(void)
3397 INIT_LIST_HEAD(&lfsck_instance_list);
3398 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3399 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3400 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3401 rc = lu_context_key_register(&lfsck_thread_key);
3403 tgt_register_lfsck_in_notify(lfsck_in_notify);
3404 tgt_register_lfsck_query(lfsck_query);
3410 static void __exit lfsck_exit(void)
3412 struct lfsck_tgt_desc *ltd;
3413 struct lfsck_tgt_desc *next;
3415 LASSERT(list_empty(&lfsck_instance_list));
3417 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3419 list_del_init(<d->ltd_orphan_list);
3423 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3425 list_del_init(<d->ltd_orphan_list);
3429 lu_context_key_degister(&lfsck_thread_key);
3432 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3433 MODULE_DESCRIPTION("LFSCK");
3434 MODULE_LICENSE("GPL");
3436 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);