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) 2012, 2013, 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";
463 static int lfsck_create_lpf_local(const struct lu_env *env,
464 struct lfsck_instance *lfsck,
465 struct dt_object *parent,
466 struct dt_object *child,
468 struct dt_object_format *dof,
471 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
472 struct dt_device *dev = lfsck->li_bottom;
473 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
474 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
475 const struct lu_fid *cfid = lfsck_dto2fid(child);
476 struct thandle *th = NULL;
477 struct linkea_data ldata = { 0 };
478 struct lu_buf linkea_buf;
479 const struct lu_name *cname;
481 int len = sizeof(struct lfsck_bookmark);
485 rc = linkea_data_new(&ldata,
486 &lfsck_env_info(env)->lti_linkea_buf2);
490 cname = lfsck_name_get_const(env, name, strlen(name));
491 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
495 th = dt_trans_create(env, dev);
499 /* 1a. create child */
500 rc = dt_declare_create(env, child, la, NULL, dof, th);
504 /* 2a. increase child nlink */
505 rc = dt_declare_ref_add(env, child, th);
509 /* 3a. insert linkEA for child */
510 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
511 ldata.ld_leh->leh_len);
512 rc = dt_declare_xattr_set(env, child, &linkea_buf,
513 XATTR_NAME_LINK, 0, th);
517 /* 4a. insert name into parent dir */
518 rec->rec_type = S_IFDIR;
520 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
521 (const struct dt_key *)name, th);
525 /* 5a. increase parent nlink */
526 rc = dt_declare_ref_add(env, parent, th);
530 /* 6a. update bookmark */
531 rc = dt_declare_record_write(env, bk_obj,
532 lfsck_buf_get(env, bk, len), 0, th);
536 rc = dt_trans_start_local(env, dev, th);
540 dt_write_lock(env, child, 0);
541 /* 1b.1. create child */
542 rc = dt_create(env, child, la, NULL, dof, th);
546 if (unlikely(!dt_try_as_dir(env, child)))
547 GOTO(unlock, rc = -ENOTDIR);
549 /* 1b.2. insert dot into child dir */
551 rc = dt_insert(env, child, (const struct dt_rec *)rec,
552 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
556 /* 1b.3. insert dotdot into child dir */
557 rec->rec_fid = &LU_LPF_FID;
558 rc = dt_insert(env, child, (const struct dt_rec *)rec,
559 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
563 /* 2b. increase child nlink */
564 rc = dt_ref_add(env, child, th);
568 /* 3b. insert linkEA for child. */
569 rc = dt_xattr_set(env, child, &linkea_buf,
570 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
571 dt_write_unlock(env, child);
575 /* 4b. insert name into parent dir */
577 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
578 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
582 dt_write_lock(env, parent, 0);
583 /* 5b. increase parent nlink */
584 rc = dt_ref_add(env, parent, th);
585 dt_write_unlock(env, parent);
589 bk->lb_lpf_fid = *cfid;
590 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
592 /* 6b. update bookmark */
593 rc = dt_record_write(env, bk_obj,
594 lfsck_buf_get(env, bk, len), &pos, th);
599 dt_write_unlock(env, child);
602 dt_trans_stop(env, dev, th);
607 static int lfsck_create_lpf_remote(const struct lu_env *env,
608 struct lfsck_instance *lfsck,
609 struct dt_object *parent,
610 struct dt_object *child,
612 struct dt_object_format *dof,
615 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
616 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
617 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
618 const struct lu_fid *cfid = lfsck_dto2fid(child);
619 struct thandle *th = NULL;
620 struct linkea_data ldata = { 0 };
621 struct lu_buf linkea_buf;
622 const struct lu_name *cname;
623 struct dt_device *dev;
625 int len = sizeof(struct lfsck_bookmark);
629 rc = linkea_data_new(&ldata,
630 &lfsck_env_info(env)->lti_linkea_buf2);
634 cname = lfsck_name_get_const(env, name, strlen(name));
635 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
639 /* Create .lustre/lost+found/MDTxxxx. */
641 /* XXX: Currently, cross-MDT create operation needs to create the child
642 * object firstly, then insert name into the parent directory. For
643 * this case, the child object resides on current MDT (local), but
644 * the parent ".lustre/lost+found" may be on remote MDT. It is not
645 * easy to contain all the sub-modifications orderly within single
648 * To avoid more inconsistency, we split the create operation into
651 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
653 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
656 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
657 * repair such inconsistency when LFSCK run next time. */
659 /* Transaction I: locally */
661 dev = lfsck->li_bottom;
662 th = dt_trans_create(env, dev);
666 /* 1a. create child */
667 rc = dt_declare_create(env, child, la, NULL, dof, th);
671 /* 2a. increase child nlink */
672 rc = dt_declare_ref_add(env, child, th);
676 /* 3a. insert linkEA for child */
677 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
678 ldata.ld_leh->leh_len);
679 rc = dt_declare_xattr_set(env, child, &linkea_buf,
680 XATTR_NAME_LINK, 0, th);
684 /* 4a. update bookmark */
685 rc = dt_declare_record_write(env, bk_obj,
686 lfsck_buf_get(env, bk, len), 0, th);
690 rc = dt_trans_start_local(env, dev, th);
694 dt_write_lock(env, child, 0);
695 /* 1b.1. create child */
696 rc = dt_create(env, child, la, NULL, dof, th);
700 if (unlikely(!dt_try_as_dir(env, child)))
701 GOTO(unlock, rc = -ENOTDIR);
703 /* 1b.2. insert dot into child dir */
704 rec->rec_type = S_IFDIR;
706 rc = dt_insert(env, child, (const struct dt_rec *)rec,
707 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
711 /* 1b.3. insert dotdot into child dir */
712 rec->rec_fid = &LU_LPF_FID;
713 rc = dt_insert(env, child, (const struct dt_rec *)rec,
714 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
718 /* 2b. increase child nlink */
719 rc = dt_ref_add(env, child, th);
723 /* 3b. insert linkEA for child */
724 rc = dt_xattr_set(env, child, &linkea_buf,
725 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
729 bk->lb_lpf_fid = *cfid;
730 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
732 /* 4b. update bookmark */
733 rc = dt_record_write(env, bk_obj,
734 lfsck_buf_get(env, bk, len), &pos, th);
736 dt_write_unlock(env, child);
737 dt_trans_stop(env, dev, th);
741 /* Transaction II: remotely */
743 dev = lfsck->li_next;
744 th = dt_trans_create(env, dev);
748 /* 5a. insert name into parent dir */
750 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
751 (const struct dt_key *)name, th);
755 /* 6a. increase parent nlink */
756 rc = dt_declare_ref_add(env, parent, th);
760 rc = dt_trans_start(env, dev, th);
764 /* 5b. insert name into parent dir */
765 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
766 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
770 dt_write_lock(env, parent, 0);
771 /* 6b. increase parent nlink */
772 rc = dt_ref_add(env, parent, th);
773 dt_write_unlock(env, parent);
778 dt_write_unlock(env, child);
780 dt_trans_stop(env, dev, th);
782 if (rc != 0 && dev == lfsck->li_next)
783 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
784 "for orphans, but failed to insert the name %s "
785 "to the .lustre/lost+found/. Such inconsistency "
786 "will be repaired when LFSCK run next time: rc = %d\n",
787 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
792 /* Do NOT create .lustre/lost+found/MDTxxxx when register the lfsck instance,
793 * because the MDT0 maybe not reaady for sequence allocation yet. We do that
794 * only when it is required, such as orphan OST-objects repairing. */
795 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
797 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
798 struct lfsck_thread_info *info = lfsck_env_info(env);
799 struct lu_fid *cfid = &info->lti_fid2;
800 struct lu_attr *la = &info->lti_la;
801 struct dt_object_format *dof = &info->lti_dof;
802 struct dt_object *parent = NULL;
803 struct dt_object *child = NULL;
804 struct lustre_handle lh = { 0 };
806 int node = lfsck_dev_idx(lfsck->li_bottom);
810 LASSERT(lfsck->li_master);
812 sprintf(name, "MDT%04x", node);
814 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
817 struct lfsck_tgt_desc *ltd;
819 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
820 if (unlikely(ltd == NULL))
823 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
828 RETURN(PTR_ERR(parent));
830 if (lfsck->li_lpf_obj != NULL)
833 if (unlikely(!dt_try_as_dir(env, parent)))
834 GOTO(out, rc = -ENOTDIR);
836 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
837 MDS_INODELOCK_UPDATE, LCK_EX);
841 mutex_lock(&lfsck->li_mutex);
842 if (lfsck->li_lpf_obj != NULL)
843 GOTO(unlock, rc = 0);
845 if (fid_is_zero(&bk->lb_lpf_fid)) {
846 /* There is corner case that: in former LFSCK scanning we have
847 * created the .lustre/lost+found/MDTxxxx but failed to update
848 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
849 * it from MDT0 firstly. */
850 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
851 (const struct dt_key *)name, BYPASS_CAPA);
852 if (rc != 0 && rc != -ENOENT)
856 bk->lb_lpf_fid = *cfid;
857 rc = lfsck_bookmark_store(env, lfsck);
859 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
864 *cfid = bk->lb_lpf_fid;
867 child = lfsck_object_find_by_dev(env, lfsck->li_bottom, cfid);
869 GOTO(unlock, rc = PTR_ERR(child));
871 if (dt_object_exists(child) != 0) {
872 if (unlikely(!dt_try_as_dir(env, child)))
875 lfsck->li_lpf_obj = child;
880 memset(la, 0, sizeof(*la));
881 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
882 la->la_mode = S_IFDIR | S_IRWXU;
883 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
885 memset(dof, 0, sizeof(*dof));
886 dof->dof_type = dt_mode_to_dft(S_IFDIR);
889 rc = lfsck_create_lpf_local(env, lfsck, parent, child, la,
892 rc = lfsck_create_lpf_remote(env, lfsck, parent, child, la,
895 lfsck->li_lpf_obj = child;
900 mutex_unlock(&lfsck->li_mutex);
901 lfsck_ibits_unlock(&lh, LCK_EX);
902 if (rc != 0 && child != NULL && !IS_ERR(child))
903 lu_object_put(env, &child->do_lu);
905 if (parent != NULL && !IS_ERR(parent))
906 lu_object_put(env, &parent->do_lu);
912 * Scan .lustre/lost+found for bad name entries and remove them.
914 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
915 * index in the system. Any other formatted name is invalid and should be
918 * \param[in] env pointer to the thread context
919 * \param[in] lfsck pointer to the lfsck instance
920 * \param[in] parent pointer to the lost+found object
922 * \retval 0 for success
923 * \retval negative error number on failure
925 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
926 struct lfsck_instance *lfsck,
927 struct dt_object *parent)
929 struct lu_dirent *ent =
930 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
931 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
936 it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA);
940 rc = iops->load(env, it, 0);
942 rc = iops->next(env, it);
949 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
953 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
954 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
957 /* name length must be strlen("MDTxxxx") */
958 if (ent->lde_namelen != 7)
961 if (memcmp(ent->lde_name, "MDT", off) != 0)
964 while (off < 7 && isxdigit(ent->lde_name[off]))
970 rc = lfsck_remove_name_entry(env, lfsck, parent,
971 ent->lde_name, S_IFDIR);
977 rc = iops->next(env, it);
983 RETURN(rc > 0 ? 0 : rc);
986 static int lfsck_update_lpf_entry(const struct lu_env *env,
987 struct lfsck_instance *lfsck,
988 struct dt_object *parent,
989 struct dt_object *child,
991 enum lfsck_verify_lpf_types type)
995 if (type == LVLT_BY_BOOKMARK) {
996 rc = lfsck_update_name_entry(env, lfsck, parent, name,
997 lfsck_dto2fid(child), S_IFDIR);
998 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
999 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1000 rc = lfsck_bookmark_store(env, lfsck);
1002 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1003 " in the bookmark file: rc = %d\n",
1004 lfsck_lfsck2name(lfsck),
1005 PFID(lfsck_dto2fid(child)), rc);
1012 * Check whether the @child back references the @parent.
1015 * 1) The child's FID is stored in the bookmark file. If the child back
1016 * references the parent (LU_LPF_FID object) via its ".." entry, then
1017 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1018 * the child back references another parent2, then:
1019 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1020 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1021 * references the child. So keep them there. As the LFSCK processing,
1022 * the parent3 may be found, then when the LFSCK run next time, the
1023 * inconsistency can be repaired.
1025 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1026 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1027 * via its ".." entry, then update the bookmark file, otherwise, if the child
1028 * back references another parent2, then:
1029 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1030 * from .lustre/lost+found/;
1031 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1032 * sub-directory name entry and update the child;
1033 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1034 * or not, then keep them there.
1036 * \param[in] env pointer to the thread context
1037 * \param[in] lfsck pointer to the lfsck instance
1038 * \param[in] parent pointer to the lost+found object
1039 * \param[in] child pointer to the lost+found sub-directory object
1040 * \param[in] name the name for lost+found sub-directory object
1041 * \param[out] fid pointer to the buffer to hold the FID of the object
1042 * (called it as parent2) that is referenced via the
1043 * child's dotdot entry; it also can be the FID that
1044 * is referenced by the name entry under the parent2.
1045 * \param[in] type to indicate where the child's FID is stored in
1047 * \retval positive number for uncertain inconsistency
1048 * \retval 0 for success
1049 * \retval negative error number on failure
1051 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1052 struct lfsck_instance *lfsck,
1053 struct dt_object *parent,
1054 struct dt_object *child, const char *name,
1056 enum lfsck_verify_lpf_types type)
1058 struct lfsck_thread_info *info = lfsck_env_info(env);
1059 char *name2 = info->lti_key;
1060 struct lu_fid *fid2 = &info->lti_fid3;
1061 struct dt_object *parent2 = NULL;
1062 struct lustre_handle lh = { 0 };
1067 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1068 (const struct dt_key *)dotdot, BYPASS_CAPA);
1072 if (!fid_is_sane(fid))
1073 GOTO(linkea, rc = -EINVAL);
1075 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1076 const struct lu_name *cname;
1078 if (lfsck->li_lpf_obj == NULL) {
1079 lu_object_get(&child->do_lu);
1080 lfsck->li_lpf_obj = child;
1083 cname = lfsck_name_get_const(env, name, strlen(name));
1084 rc = lfsck_verify_linkea(env, lfsck->li_bottom, child, cname,
1087 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1093 parent2 = lfsck_object_find_by_dev(env, lfsck->li_next, fid);
1094 if (IS_ERR(parent2))
1095 GOTO(linkea, parent2);
1097 if (!dt_object_exists(parent2)) {
1098 lu_object_put(env, &parent2->do_lu);
1100 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1103 if (!dt_try_as_dir(env, parent2)) {
1104 lu_object_put(env, &parent2->do_lu);
1106 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1110 /* To prevent rename/unlink race */
1111 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1112 MDS_INODELOCK_UPDATE, LCK_PR);
1116 dt_read_lock(env, child, 0);
1117 rc = lfsck_links_get_first(env, child, name2, fid2);
1119 dt_read_unlock(env, child);
1120 lfsck_ibits_unlock(&lh, LCK_PR);
1122 GOTO(out_put, rc = 1);
1125 /* It is almost impossible that the bookmark file (or the name entry)
1126 * and the linkEA hit the same data corruption. Trust the linkEA. */
1127 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1128 dt_read_unlock(env, child);
1129 lfsck_ibits_unlock(&lh, LCK_PR);
1132 if (lfsck->li_lpf_obj == NULL) {
1133 lu_object_get(&child->do_lu);
1134 lfsck->li_lpf_obj = child;
1137 /* Update the child's dotdot entry */
1138 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1139 &LU_LPF_FID, S_IFDIR);
1141 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1147 if (parent2 == NULL || IS_ERR(parent2)) {
1148 dt_read_unlock(env, child);
1149 lfsck_ibits_unlock(&lh, LCK_PR);
1151 GOTO(out_done, rc = 1);
1154 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1155 (const struct dt_key *)name2, BYPASS_CAPA);
1156 dt_read_unlock(env, child);
1157 lfsck_ibits_unlock(&lh, LCK_PR);
1158 if (rc != 0 && rc != -ENOENT)
1161 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1162 if (type == LVLT_BY_BOOKMARK)
1163 GOTO(out_put, rc = 1);
1165 /* Trust the name entry, update the child's dotdot entry. */
1166 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1167 &LU_LPF_FID, S_IFDIR);
1172 if (type == LVLT_BY_BOOKMARK) {
1173 /* Invalid FID record in the bookmark file, reset it. */
1174 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1175 rc = lfsck_bookmark_store(env, lfsck);
1177 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1178 " in the bookmark file: rc = %d\n",
1179 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1180 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1181 /* The name entry is wrong, remove it. */
1182 rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR);
1188 if (parent2 != NULL && !IS_ERR(parent2))
1189 lu_object_put(env, &parent2->do_lu);
1196 * Verify the /ROOT/.lustre/lost+found/ directory.
1198 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1199 * the LFSCK does not exactly know how to handle, such as orphans. So before
1200 * the LFSCK scanning the system, the consistency of such directory needs to
1201 * be verified firstly to allow the users to use it during the LFSCK.
1203 * \param[in] env pointer to the thread context
1204 * \param[in] lfsck pointer to the lfsck instance
1206 * \retval positive number for uncertain inconsistency
1207 * \retval 0 for success
1208 * \retval negative error number on failure
1210 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1212 struct lfsck_thread_info *info = lfsck_env_info(env);
1213 struct lu_fid *pfid = &info->lti_fid;
1214 struct lu_fid *cfid = &info->lti_fid2;
1215 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1216 struct dt_object *parent = NULL;
1217 /* child1's FID is in the bookmark file. */
1218 struct dt_object *child1 = NULL;
1219 /* child2's FID is in the name entry MDTxxxx. */
1220 struct dt_object *child2 = NULL;
1221 struct dt_device *dev = lfsck->li_bottom;
1222 const struct lu_name *cname;
1224 int node = lfsck_dev_idx(dev);
1228 LASSERT(lfsck->li_master);
1231 parent = lfsck_object_find_by_dev(env, dev, &LU_LPF_FID);
1233 struct lfsck_tgt_desc *ltd;
1235 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1236 if (unlikely(ltd == NULL))
1239 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1245 RETURN(PTR_ERR(parent));
1247 LASSERT(dt_object_exists(parent));
1249 if (unlikely(!dt_try_as_dir(env, parent)))
1250 GOTO(put, rc = -ENOTDIR);
1253 rc = lfsck_scan_lpf_bad_entries(env, lfsck, parent);
1255 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1256 "for bad sub-directories: rc = %d\n",
1257 lfsck_lfsck2name(lfsck), rc);
1260 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1261 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1262 struct lu_fid tfid = bk->lb_lpf_fid;
1264 /* Invalid FID record in the bookmark file, reset it. */
1265 fid_zero(&bk->lb_lpf_fid);
1266 rc = lfsck_bookmark_store(env, lfsck);
1268 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1269 " in the bookmark file: rc = %d\n",
1270 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1275 child1 = lfsck_object_find_by_dev(env, dev,
1278 GOTO(put, rc = PTR_ERR(child1));
1280 if (unlikely(!dt_object_exists(child1) ||
1281 dt_object_remote(child1)) ||
1282 !S_ISDIR(lfsck_object_type(child1))) {
1283 /* Invalid FID record in the bookmark file,
1285 fid_zero(&bk->lb_lpf_fid);
1286 rc = lfsck_bookmark_store(env, lfsck);
1288 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1289 " in the bookmark file: rc = %d\n",
1290 lfsck_lfsck2name(lfsck),
1291 PFID(lfsck_dto2fid(child1)), rc);
1296 lu_object_put(env, &child1->do_lu);
1298 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1299 GOTO(put, rc = -ENOTDIR);
1304 snprintf(name, 8, "MDT%04x", node);
1305 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1306 (const struct dt_key *)name, BYPASS_CAPA);
1307 if (rc == -ENOENT) {
1308 if (!fid_is_zero(&bk->lb_lpf_fid))
1317 /* Invalid FID in the name entry, remove the name entry. */
1318 if (!fid_is_norm(cfid)) {
1319 rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR);
1326 child2 = lfsck_object_find_by_dev(env, dev, cfid);
1328 GOTO(put, rc = PTR_ERR(child2));
1330 if (unlikely(!dt_object_exists(child2) ||
1331 dt_object_remote(child2)) ||
1332 !S_ISDIR(lfsck_object_type(child2))) {
1333 rc = lfsck_remove_name_entry(env, lfsck, parent, name,
1341 if (unlikely(!dt_try_as_dir(env, child2)))
1342 GOTO(put, rc = -ENOTDIR);
1344 if (child1 == NULL) {
1345 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2, name,
1346 pfid, LVLT_BY_NAMEENTRY);
1347 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1348 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name,
1349 pfid, LVLT_BY_BOOKMARK);
1350 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1351 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2,
1355 if (lfsck->li_lpf_obj == NULL) {
1356 lu_object_get(&child2->do_lu);
1357 lfsck->li_lpf_obj = child2;
1360 cname = lfsck_name_get_const(env, name, strlen(name));
1361 rc = lfsck_verify_linkea(env, dev, child2, cname, &LU_LPF_FID);
1368 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name,
1369 pfid, LVLT_BY_BOOKMARK);
1374 if (lfsck->li_lpf_obj != NULL &&
1375 unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj)))
1378 if (child2 != NULL && !IS_ERR(child2))
1379 lu_object_put(env, &child2->do_lu);
1380 if (child1 != NULL && !IS_ERR(child1))
1381 lu_object_put(env, &child1->do_lu);
1382 if (parent != NULL && !IS_ERR(parent))
1383 lu_object_put(env, &parent->do_lu);
1388 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1390 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1391 struct seq_server_site *ss;
1396 ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1397 if (unlikely(ss == NULL))
1400 OBD_ALLOC_PTR(lfsck->li_seq);
1401 if (lfsck->li_seq == NULL)
1404 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1406 GOTO(out, rc = -ENOMEM);
1408 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1409 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1411 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1415 if (fid_is_sane(&bk->lb_last_fid))
1416 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1421 OBD_FREE_PTR(lfsck->li_seq);
1422 lfsck->li_seq = NULL;
1427 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1429 if (lfsck->li_seq != NULL) {
1430 seq_client_fini(lfsck->li_seq);
1431 OBD_FREE_PTR(lfsck->li_seq);
1432 lfsck->li_seq = NULL;
1436 void lfsck_instance_cleanup(const struct lu_env *env,
1437 struct lfsck_instance *lfsck)
1439 struct ptlrpc_thread *thread = &lfsck->li_thread;
1440 struct lfsck_component *com;
1441 struct lfsck_component *next;
1442 struct lfsck_lmv_unit *llu;
1443 struct lfsck_lmv_unit *llu_next;
1444 struct lfsck_lmv *llmv;
1447 LASSERT(list_empty(&lfsck->li_link));
1448 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1450 if (lfsck->li_obj_oit != NULL) {
1451 lu_object_put_nocache(env, &lfsck->li_obj_oit->do_lu);
1452 lfsck->li_obj_oit = NULL;
1455 LASSERT(lfsck->li_obj_dir == NULL);
1456 LASSERT(lfsck->li_lmv == NULL);
1458 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1459 llmv = &llu->llu_lmv;
1461 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1462 "still in using: %u\n",
1463 atomic_read(&llmv->ll_ref));
1465 lfsck_lmv_put(env, llmv);
1468 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1469 lfsck_component_cleanup(env, com);
1472 LASSERT(list_empty(&lfsck->li_list_dir));
1474 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1476 lfsck_component_cleanup(env, com);
1479 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1480 lfsck_component_cleanup(env, com);
1483 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1484 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1486 if (lfsck->li_bookmark_obj != NULL) {
1487 lu_object_put_nocache(env, &lfsck->li_bookmark_obj->do_lu);
1488 lfsck->li_bookmark_obj = NULL;
1491 if (lfsck->li_lpf_obj != NULL) {
1492 lu_object_put(env, &lfsck->li_lpf_obj->do_lu);
1493 lfsck->li_lpf_obj = NULL;
1496 if (lfsck->li_los != NULL) {
1497 local_oid_storage_fini(env, lfsck->li_los);
1498 lfsck->li_los = NULL;
1501 lfsck_fid_fini(lfsck);
1503 OBD_FREE_PTR(lfsck);
1506 static inline struct lfsck_instance *
1507 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1509 struct lfsck_instance *lfsck;
1511 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1512 if (lfsck->li_bottom == key) {
1514 lfsck_instance_get(lfsck);
1516 list_del_init(&lfsck->li_link);
1525 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1528 struct lfsck_instance *lfsck;
1530 spin_lock(&lfsck_instance_lock);
1531 lfsck = __lfsck_instance_find(key, ref, unlink);
1532 spin_unlock(&lfsck_instance_lock);
1537 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1539 struct lfsck_instance *tmp;
1541 spin_lock(&lfsck_instance_lock);
1542 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1543 if (lfsck->li_bottom == tmp->li_bottom) {
1544 spin_unlock(&lfsck_instance_lock);
1549 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1550 spin_unlock(&lfsck_instance_lock);
1554 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1559 bool newline = (bits != 0 ? false : true);
1561 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1563 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1566 if (names[i] != NULL) {
1570 seq_printf(m, "%s%c", names[i],
1571 newline ? '\n' : ',');
1577 seq_printf(m, "\n");
1581 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix)
1584 seq_printf(m, "%s: "LPU64" seconds\n", prefix,
1585 cfs_time_current_sec() - time);
1587 seq_printf(m, "%s: N/A\n", prefix);
1591 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1594 if (fid_is_zero(&pos->lp_dir_parent)) {
1595 if (pos->lp_oit_cookie == 0)
1596 seq_printf(m, "%s: N/A, N/A, N/A\n",
1599 seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1600 prefix, pos->lp_oit_cookie);
1602 seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1603 prefix, pos->lp_oit_cookie,
1604 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1609 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1610 struct lfsck_position *pos, bool init)
1612 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1614 if (unlikely(lfsck->li_di_oit == NULL)) {
1615 memset(pos, 0, sizeof(*pos));
1619 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1620 if (!lfsck->li_current_oit_processed && !init)
1621 pos->lp_oit_cookie--;
1623 LASSERT(pos->lp_oit_cookie > 0);
1625 if (lfsck->li_di_dir != NULL) {
1626 struct dt_object *dto = lfsck->li_obj_dir;
1628 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1631 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1632 fid_zero(&pos->lp_dir_parent);
1633 pos->lp_dir_cookie = 0;
1635 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1638 fid_zero(&pos->lp_dir_parent);
1639 pos->lp_dir_cookie = 0;
1643 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1647 if (limit != LFSCK_SPEED_NO_LIMIT) {
1648 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1649 lfsck->li_sleep_rate = limit /
1650 msecs_to_jiffies(MSEC_PER_SEC);
1651 lfsck->li_sleep_jif = 1;
1653 lfsck->li_sleep_rate = 1;
1654 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1658 lfsck->li_sleep_jif = 0;
1659 lfsck->li_sleep_rate = 0;
1662 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1663 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1670 void lfsck_control_speed(struct lfsck_instance *lfsck)
1672 struct ptlrpc_thread *thread = &lfsck->li_thread;
1673 struct l_wait_info lwi;
1675 if (lfsck->li_sleep_jif > 0 &&
1676 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1677 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1678 LWI_ON_SIGNAL_NOOP, NULL);
1680 l_wait_event(thread->t_ctl_waitq,
1681 !thread_is_running(thread),
1683 lfsck->li_new_scanned = 0;
1687 void lfsck_control_speed_by_self(struct lfsck_component *com)
1689 struct lfsck_instance *lfsck = com->lc_lfsck;
1690 struct ptlrpc_thread *thread = &lfsck->li_thread;
1691 struct l_wait_info lwi;
1693 if (lfsck->li_sleep_jif > 0 &&
1694 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1695 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1696 LWI_ON_SIGNAL_NOOP, NULL);
1698 l_wait_event(thread->t_ctl_waitq,
1699 !thread_is_running(thread),
1701 com->lc_new_scanned = 0;
1705 static struct lfsck_thread_args *
1706 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1707 struct lfsck_component *com,
1708 struct lfsck_start_param *lsp)
1710 struct lfsck_thread_args *lta;
1715 return ERR_PTR(-ENOMEM);
1717 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1723 lta->lta_lfsck = lfsck_instance_get(lfsck);
1725 lta->lta_com = lfsck_component_get(com);
1732 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1734 if (lta->lta_com != NULL)
1735 lfsck_component_put(<a->lta_env, lta->lta_com);
1736 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1737 lu_env_fini(<a->lta_env);
1741 struct lfsck_assistant_data *
1742 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1745 struct lfsck_assistant_data *lad;
1749 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1750 if (lad->lad_bitmap == NULL) {
1755 INIT_LIST_HEAD(&lad->lad_req_list);
1756 spin_lock_init(&lad->lad_lock);
1757 INIT_LIST_HEAD(&lad->lad_ost_list);
1758 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1759 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1760 INIT_LIST_HEAD(&lad->lad_mdt_list);
1761 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1762 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1763 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1765 lad->lad_name = name;
1772 * Generic LFSCK asynchronous communication interpretor function.
1773 * The LFSCK RPC reply for both the event notification and status
1774 * querying will be handled here.
1776 * \param[in] env pointer to the thread context
1777 * \param[in] req pointer to the LFSCK request
1778 * \param[in] args pointer to the lfsck_async_interpret_args
1779 * \param[in] rc the result for handling the LFSCK request
1781 * \retval 0 for success
1782 * \retval negative error number on failure
1784 int lfsck_async_interpret_common(const struct lu_env *env,
1785 struct ptlrpc_request *req,
1788 struct lfsck_async_interpret_args *laia = args;
1789 struct lfsck_component *com = laia->laia_com;
1790 struct lfsck_assistant_data *lad = com->lc_data;
1791 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1792 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1793 struct lfsck_request *lr = laia->laia_lr;
1795 LASSERT(com->lc_lfsck->li_master);
1797 switch (lr->lr_event) {
1800 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1802 lfsck_lfsck2name(com->lc_lfsck),
1803 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1804 ltd->ltd_index, lad->lad_name, rc);
1806 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1807 struct lfsck_layout *lo = com->lc_file_ram;
1809 if (lr->lr_flags & LEF_TO_OST)
1810 lfsck_lad_set_bitmap(env, com,
1813 lo->ll_flags |= LF_INCOMPLETE;
1815 struct lfsck_namespace *ns = com->lc_file_ram;
1817 /* If some MDT does not join the namespace
1818 * LFSCK, then we cannot know whether there
1819 * is some name entry on such MDT that with
1820 * the referenced MDT-object on this MDT or
1821 * not. So the namespace LFSCK on this MDT
1822 * cannot handle orphan MDT-objects properly.
1823 * So we mark the LFSCK as LF_INCOMPLETE and
1824 * skip orphan MDT-objects handling. */
1825 ns->ln_flags |= LF_INCOMPLETE;
1830 spin_lock(<ds->ltd_lock);
1831 if (ltd->ltd_dead) {
1832 spin_unlock(<ds->ltd_lock);
1836 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1837 struct list_head *list;
1838 struct list_head *phase_list;
1840 if (ltd->ltd_layout_done) {
1841 spin_unlock(<ds->ltd_lock);
1845 if (lr->lr_flags & LEF_TO_OST) {
1846 list = &lad->lad_ost_list;
1847 phase_list = &lad->lad_ost_phase1_list;
1849 list = &lad->lad_mdt_list;
1850 phase_list = &lad->lad_mdt_phase1_list;
1853 if (list_empty(<d->ltd_layout_list))
1854 list_add_tail(<d->ltd_layout_list, list);
1855 if (list_empty(<d->ltd_layout_phase_list))
1856 list_add_tail(<d->ltd_layout_phase_list,
1859 if (ltd->ltd_namespace_done) {
1860 spin_unlock(<ds->ltd_lock);
1864 if (list_empty(<d->ltd_namespace_list))
1865 list_add_tail(<d->ltd_namespace_list,
1866 &lad->lad_mdt_list);
1867 if (list_empty(<d->ltd_namespace_phase_list))
1868 list_add_tail(<d->ltd_namespace_phase_list,
1869 &lad->lad_mdt_phase1_list);
1871 spin_unlock(<ds->ltd_lock);
1874 case LE_PHASE1_DONE:
1875 case LE_PHASE2_DONE:
1877 if (rc != 0 && rc != -EALREADY)
1878 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
1879 "event = %d, rc = %d\n",
1880 lfsck_lfsck2name(com->lc_lfsck),
1881 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1882 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
1885 struct lfsck_reply *reply;
1886 struct list_head *list;
1887 struct list_head *phase_list;
1889 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1890 list = <d->ltd_layout_list;
1891 phase_list = <d->ltd_layout_phase_list;
1893 list = <d->ltd_namespace_list;
1894 phase_list = <d->ltd_namespace_phase_list;
1898 spin_lock(<ds->ltd_lock);
1899 list_del_init(phase_list);
1900 list_del_init(list);
1901 spin_unlock(<ds->ltd_lock);
1905 reply = req_capsule_server_get(&req->rq_pill,
1907 if (reply == NULL) {
1909 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
1910 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1912 spin_lock(<ds->ltd_lock);
1913 list_del_init(phase_list);
1914 list_del_init(list);
1915 spin_unlock(<ds->ltd_lock);
1919 switch (reply->lr_status) {
1920 case LS_SCANNING_PHASE1:
1922 case LS_SCANNING_PHASE2:
1923 spin_lock(<ds->ltd_lock);
1924 list_del_init(phase_list);
1925 if (ltd->ltd_dead) {
1926 spin_unlock(<ds->ltd_lock);
1930 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1931 if (ltd->ltd_layout_done) {
1932 spin_unlock(<ds->ltd_lock);
1936 if (lr->lr_flags & LEF_TO_OST)
1937 list_add_tail(phase_list,
1938 &lad->lad_ost_phase2_list);
1940 list_add_tail(phase_list,
1941 &lad->lad_mdt_phase2_list);
1943 if (ltd->ltd_namespace_done) {
1944 spin_unlock(<ds->ltd_lock);
1948 list_add_tail(phase_list,
1949 &lad->lad_mdt_phase2_list);
1951 spin_unlock(<ds->ltd_lock);
1954 spin_lock(<ds->ltd_lock);
1955 list_del_init(phase_list);
1956 list_del_init(list);
1957 spin_unlock(<ds->ltd_lock);
1963 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
1964 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
1968 if (!laia->laia_shared) {
1970 lfsck_component_put(env, com);
1976 static void lfsck_interpret(const struct lu_env *env,
1977 struct lfsck_instance *lfsck,
1978 struct ptlrpc_request *req, void *args, int result)
1980 struct lfsck_async_interpret_args *laia = args;
1981 struct lfsck_component *com;
1983 LASSERT(laia->laia_com == NULL);
1984 LASSERT(laia->laia_shared);
1986 spin_lock(&lfsck->li_lock);
1987 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1988 laia->laia_com = com;
1989 lfsck_async_interpret_common(env, req, laia, result);
1992 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
1993 laia->laia_com = com;
1994 lfsck_async_interpret_common(env, req, laia, result);
1996 spin_unlock(&lfsck->li_lock);
1999 static int lfsck_stop_notify(const struct lu_env *env,
2000 struct lfsck_instance *lfsck,
2001 struct lfsck_tgt_descs *ltds,
2002 struct lfsck_tgt_desc *ltd, __u16 type)
2004 struct lfsck_component *com;
2008 LASSERT(lfsck->li_master);
2010 spin_lock(&lfsck->li_lock);
2011 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2013 com = __lfsck_component_find(lfsck, type,
2014 &lfsck->li_list_double_scan);
2016 lfsck_component_get(com);
2017 spin_unlock(&lfsck->li_lock);
2020 struct lfsck_thread_info *info = lfsck_env_info(env);
2021 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2022 struct lfsck_request *lr = &info->lti_lr;
2023 struct lfsck_assistant_data *lad = com->lc_data;
2024 struct list_head *list;
2025 struct list_head *phase_list;
2026 struct ptlrpc_request_set *set;
2028 set = ptlrpc_prep_set();
2030 lfsck_component_put(env, com);
2035 if (type == LFSCK_TYPE_LAYOUT) {
2036 list = <d->ltd_layout_list;
2037 phase_list = <d->ltd_layout_phase_list;
2039 list = <d->ltd_namespace_list;
2040 phase_list = <d->ltd_namespace_phase_list;
2043 spin_lock(<ds->ltd_lock);
2044 if (list_empty(list)) {
2045 LASSERT(list_empty(phase_list));
2046 spin_unlock(<ds->ltd_lock);
2047 ptlrpc_set_destroy(set);
2052 list_del_init(phase_list);
2053 list_del_init(list);
2054 spin_unlock(<ds->ltd_lock);
2056 memset(lr, 0, sizeof(*lr));
2057 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2058 lr->lr_event = LE_PEER_EXIT;
2059 lr->lr_active = type;
2060 lr->lr_status = LS_CO_PAUSED;
2061 if (ltds == &lfsck->li_ost_descs)
2062 lr->lr_flags = LEF_TO_OST;
2064 laia->laia_com = com;
2065 laia->laia_ltds = ltds;
2066 atomic_inc(<d->ltd_ref);
2067 laia->laia_ltd = ltd;
2069 laia->laia_shared = 0;
2071 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2072 lfsck_async_interpret_common,
2073 laia, LFSCK_NOTIFY);
2075 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2076 "co-stop for %s: rc = %d\n",
2077 lfsck_lfsck2name(lfsck),
2078 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2079 ltd->ltd_index, lad->lad_name, rc);
2082 rc = ptlrpc_set_wait(set);
2085 ptlrpc_set_destroy(set);
2086 lfsck_component_put(env, com);
2092 static int lfsck_async_interpret(const struct lu_env *env,
2093 struct ptlrpc_request *req,
2096 struct lfsck_async_interpret_args *laia = args;
2097 struct lfsck_instance *lfsck;
2099 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2101 lfsck_interpret(env, lfsck, req, laia, rc);
2102 lfsck_tgt_put(laia->laia_ltd);
2103 if (rc != 0 && laia->laia_result != -EALREADY)
2104 laia->laia_result = rc;
2109 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2110 struct lfsck_request *lr,
2111 struct ptlrpc_request_set *set,
2112 ptlrpc_interpterer_t interpreter,
2113 void *args, int request)
2115 struct lfsck_async_interpret_args *laia;
2116 struct ptlrpc_request *req;
2117 struct lfsck_request *tmp;
2118 struct req_format *format;
2123 format = &RQF_LFSCK_NOTIFY;
2126 format = &RQF_LFSCK_QUERY;
2129 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2130 exp->exp_obd->obd_name, request, -EINVAL);
2134 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2138 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2140 ptlrpc_request_free(req);
2145 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2147 ptlrpc_request_set_replen(req);
2149 laia = ptlrpc_req_async_args(req);
2150 *laia = *(struct lfsck_async_interpret_args *)args;
2151 if (laia->laia_com != NULL)
2152 lfsck_component_get(laia->laia_com);
2153 req->rq_interpret_reply = interpreter;
2154 ptlrpc_set_add_req(set, req);
2159 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2160 struct lfsck_start_param *lsp)
2162 struct lfsck_instance *lfsck = com->lc_lfsck;
2163 struct lfsck_assistant_data *lad = com->lc_data;
2164 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2165 struct ptlrpc_thread *athread = &lad->lad_thread;
2166 struct lfsck_thread_args *lta;
2167 struct task_struct *task;
2171 lad->lad_assistant_status = 0;
2172 lad->lad_post_result = 0;
2173 lad->lad_to_post = 0;
2174 lad->lad_to_double_scan = 0;
2175 lad->lad_in_double_scan = 0;
2177 thread_set_flags(athread, 0);
2179 lta = lfsck_thread_args_init(lfsck, com, lsp);
2181 RETURN(PTR_ERR(lta));
2183 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2186 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2187 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2188 lfsck_thread_args_fini(lta);
2190 struct l_wait_info lwi = { 0 };
2192 l_wait_event(mthread->t_ctl_waitq,
2193 thread_is_running(athread) ||
2194 thread_is_stopped(athread),
2196 if (unlikely(!thread_is_running(athread)))
2197 rc = lad->lad_assistant_status;
2205 int lfsck_checkpoint_generic(const struct lu_env *env,
2206 struct lfsck_component *com)
2208 struct lfsck_assistant_data *lad = com->lc_data;
2209 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2210 struct ptlrpc_thread *athread = &lad->lad_thread;
2211 struct l_wait_info lwi = { 0 };
2213 if (com->lc_new_checked == 0)
2214 return LFSCK_CHECKPOINT_SKIP;
2216 l_wait_event(mthread->t_ctl_waitq,
2217 list_empty(&lad->lad_req_list) ||
2218 !thread_is_running(mthread) ||
2219 thread_is_stopped(athread),
2222 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2223 return LFSCK_CHECKPOINT_SKIP;
2228 void lfsck_post_generic(const struct lu_env *env,
2229 struct lfsck_component *com, int *result)
2231 struct lfsck_assistant_data *lad = com->lc_data;
2232 struct ptlrpc_thread *athread = &lad->lad_thread;
2233 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2234 struct l_wait_info lwi = { 0 };
2236 lad->lad_post_result = *result;
2239 lad->lad_to_post = 1;
2241 wake_up_all(&athread->t_ctl_waitq);
2242 l_wait_event(mthread->t_ctl_waitq,
2243 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2244 thread_is_stopped(athread),
2247 if (lad->lad_assistant_status < 0)
2248 *result = lad->lad_assistant_status;
2251 int lfsck_double_scan_generic(const struct lu_env *env,
2252 struct lfsck_component *com, int status)
2254 struct lfsck_assistant_data *lad = com->lc_data;
2255 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2256 struct ptlrpc_thread *athread = &lad->lad_thread;
2257 struct l_wait_info lwi = { 0 };
2259 if (status != LS_SCANNING_PHASE2)
2262 lad->lad_to_double_scan = 1;
2264 wake_up_all(&athread->t_ctl_waitq);
2265 l_wait_event(mthread->t_ctl_waitq,
2266 lad->lad_in_double_scan ||
2267 thread_is_stopped(athread),
2270 if (lad->lad_assistant_status < 0)
2271 return lad->lad_assistant_status;
2276 void lfsck_quit_generic(const struct lu_env *env,
2277 struct lfsck_component *com)
2279 struct lfsck_assistant_data *lad = com->lc_data;
2280 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2281 struct ptlrpc_thread *athread = &lad->lad_thread;
2282 struct l_wait_info lwi = { 0 };
2285 wake_up_all(&athread->t_ctl_waitq);
2286 l_wait_event(mthread->t_ctl_waitq,
2287 thread_is_init(athread) ||
2288 thread_is_stopped(athread),
2292 /* external interfaces */
2294 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2297 struct lfsck_instance *lfsck;
2301 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2305 lfsck = lfsck_instance_find(key, true, false);
2306 if (likely(lfsck != NULL)) {
2307 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2308 lfsck_instance_put(&env, lfsck);
2317 EXPORT_SYMBOL(lfsck_get_speed);
2319 int lfsck_set_speed(struct dt_device *key, int val)
2322 struct lfsck_instance *lfsck;
2326 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2330 lfsck = lfsck_instance_find(key, true, false);
2331 if (likely(lfsck != NULL)) {
2332 mutex_lock(&lfsck->li_mutex);
2333 if (__lfsck_set_speed(lfsck, val))
2334 rc = lfsck_bookmark_store(&env, lfsck);
2335 mutex_unlock(&lfsck->li_mutex);
2336 lfsck_instance_put(&env, lfsck);
2345 EXPORT_SYMBOL(lfsck_set_speed);
2347 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2350 struct lfsck_instance *lfsck;
2354 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2358 lfsck = lfsck_instance_find(key, true, false);
2359 if (likely(lfsck != NULL)) {
2360 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2361 lfsck_instance_put(&env, lfsck);
2370 EXPORT_SYMBOL(lfsck_get_windows);
2372 int lfsck_set_windows(struct dt_device *key, int val)
2375 struct lfsck_instance *lfsck;
2379 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2383 lfsck = lfsck_instance_find(key, true, false);
2384 if (likely(lfsck != NULL)) {
2385 if (val > LFSCK_ASYNC_WIN_MAX) {
2386 CWARN("%s: Too large async window size, which "
2387 "may cause memory issues. The valid range "
2388 "is [0 - %u]. If you do not want to restrict "
2389 "the window size for async requests pipeline, "
2390 "just set it as 0.\n",
2391 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2393 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2394 mutex_lock(&lfsck->li_mutex);
2395 lfsck->li_bookmark_ram.lb_async_windows = val;
2396 rc = lfsck_bookmark_store(&env, lfsck);
2397 mutex_unlock(&lfsck->li_mutex);
2399 lfsck_instance_put(&env, lfsck);
2408 EXPORT_SYMBOL(lfsck_set_windows);
2410 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2413 struct lfsck_instance *lfsck;
2414 struct lfsck_component *com;
2418 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2422 lfsck = lfsck_instance_find(key, true, false);
2423 if (likely(lfsck != NULL)) {
2424 com = lfsck_component_find(lfsck, type);
2425 if (likely(com != NULL)) {
2426 rc = com->lc_ops->lfsck_dump(&env, com, m);
2427 lfsck_component_put(&env, com);
2432 lfsck_instance_put(&env, lfsck);
2441 EXPORT_SYMBOL(lfsck_dump);
2443 static int lfsck_stop_all(const struct lu_env *env,
2444 struct lfsck_instance *lfsck,
2445 struct lfsck_stop *stop)
2447 struct lfsck_thread_info *info = lfsck_env_info(env);
2448 struct lfsck_request *lr = &info->lti_lr;
2449 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2450 struct ptlrpc_request_set *set;
2451 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2452 struct lfsck_tgt_desc *ltd;
2453 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2459 LASSERT(stop->ls_flags & LPF_BROADCAST);
2461 set = ptlrpc_prep_set();
2462 if (unlikely(set == NULL))
2465 memset(lr, 0, sizeof(*lr));
2466 lr->lr_event = LE_STOP;
2467 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2468 lr->lr_status = stop->ls_status;
2469 lr->lr_version = bk->lb_version;
2470 lr->lr_active = LFSCK_TYPES_ALL;
2471 lr->lr_param = stop->ls_flags;
2473 laia->laia_com = NULL;
2474 laia->laia_ltds = ltds;
2476 laia->laia_result = 0;
2477 laia->laia_shared = 1;
2479 down_read(<ds->ltd_rw_sem);
2480 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2481 ltd = lfsck_tgt_get(ltds, idx);
2482 LASSERT(ltd != NULL);
2484 laia->laia_ltd = ltd;
2485 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2486 lfsck_async_interpret, laia,
2489 lfsck_interpret(env, lfsck, NULL, laia, rc);
2491 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2492 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2496 up_read(<ds->ltd_rw_sem);
2498 rc = ptlrpc_set_wait(set);
2499 ptlrpc_set_destroy(set);
2502 rc = laia->laia_result;
2504 if (rc == -EALREADY)
2508 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2509 lfsck_lfsck2name(lfsck), rc);
2511 RETURN(rc != 0 ? rc : rc1);
2514 static int lfsck_start_all(const struct lu_env *env,
2515 struct lfsck_instance *lfsck,
2516 struct lfsck_start *start)
2518 struct lfsck_thread_info *info = lfsck_env_info(env);
2519 struct lfsck_request *lr = &info->lti_lr;
2520 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2521 struct ptlrpc_request_set *set;
2522 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2523 struct lfsck_tgt_desc *ltd;
2524 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2529 LASSERT(start->ls_flags & LPF_BROADCAST);
2531 set = ptlrpc_prep_set();
2532 if (unlikely(set == NULL))
2535 memset(lr, 0, sizeof(*lr));
2536 lr->lr_event = LE_START;
2537 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2538 lr->lr_speed = bk->lb_speed_limit;
2539 lr->lr_version = bk->lb_version;
2540 lr->lr_active = start->ls_active;
2541 lr->lr_param = start->ls_flags;
2542 lr->lr_async_windows = bk->lb_async_windows;
2543 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2544 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2547 laia->laia_com = NULL;
2548 laia->laia_ltds = ltds;
2550 laia->laia_result = 0;
2551 laia->laia_shared = 1;
2553 down_read(<ds->ltd_rw_sem);
2554 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2555 ltd = lfsck_tgt_get(ltds, idx);
2556 LASSERT(ltd != NULL);
2558 laia->laia_ltd = ltd;
2559 ltd->ltd_layout_done = 0;
2560 ltd->ltd_namespace_done = 0;
2561 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2562 lfsck_async_interpret, laia,
2565 lfsck_interpret(env, lfsck, NULL, laia, rc);
2567 CERROR("%s: cannot notify MDT %x for LFSCK "
2568 "start, failout: rc = %d\n",
2569 lfsck_lfsck2name(lfsck), idx, rc);
2573 up_read(<ds->ltd_rw_sem);
2576 ptlrpc_set_destroy(set);
2581 rc = ptlrpc_set_wait(set);
2582 ptlrpc_set_destroy(set);
2585 rc = laia->laia_result;
2588 struct lfsck_stop *stop = &info->lti_stop;
2590 CERROR("%s: cannot start LFSCK on some MDTs, "
2591 "stop all: rc = %d\n",
2592 lfsck_lfsck2name(lfsck), rc);
2593 if (rc != -EALREADY) {
2594 stop->ls_status = LS_FAILED;
2595 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2596 lfsck_stop_all(env, lfsck, stop);
2603 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2604 struct lfsck_start_param *lsp)
2606 struct lfsck_start *start = lsp->lsp_start;
2607 struct lfsck_instance *lfsck;
2608 struct lfsck_bookmark *bk;
2609 struct ptlrpc_thread *thread;
2610 struct lfsck_component *com;
2611 struct l_wait_info lwi = { 0 };
2612 struct lfsck_thread_args *lta;
2613 struct task_struct *task;
2620 lfsck = lfsck_instance_find(key, true, false);
2621 if (unlikely(lfsck == NULL))
2624 /* System is not ready, try again later. */
2625 if (unlikely(lfsck->li_namespace == NULL))
2626 GOTO(put, rc = -EAGAIN);
2628 /* start == NULL means auto trigger paused LFSCK. */
2629 if ((start == NULL) &&
2630 (list_empty(&lfsck->li_list_scan) ||
2631 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2634 bk = &lfsck->li_bookmark_ram;
2635 thread = &lfsck->li_thread;
2636 mutex_lock(&lfsck->li_mutex);
2637 spin_lock(&lfsck->li_lock);
2638 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2640 if (unlikely(start == NULL)) {
2641 spin_unlock(&lfsck->li_lock);
2645 while (start->ls_active != 0) {
2646 if (!(type & start->ls_active)) {
2651 com = __lfsck_component_find(lfsck, type,
2652 &lfsck->li_list_scan);
2654 com = __lfsck_component_find(lfsck, type,
2655 &lfsck->li_list_double_scan);
2661 if (com->lc_ops->lfsck_join != NULL) {
2662 rc = com->lc_ops->lfsck_join( env, com, lsp);
2663 if (rc != 0 && rc != -EALREADY)
2666 start->ls_active &= ~type;
2669 spin_unlock(&lfsck->li_lock);
2672 spin_unlock(&lfsck->li_lock);
2674 lfsck->li_status = 0;
2675 lfsck->li_oit_over = 0;
2676 lfsck->li_start_unplug = 0;
2677 lfsck->li_drop_dryrun = 0;
2678 lfsck->li_new_scanned = 0;
2680 /* For auto trigger. */
2684 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2685 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2686 lfsck_lfsck2name(lfsck));
2688 GOTO(out, rc = -EPERM);
2691 start->ls_version = bk->lb_version;
2693 if (start->ls_active != 0) {
2694 struct lfsck_component *next;
2696 if (start->ls_active == LFSCK_TYPES_ALL)
2697 start->ls_active = LFSCK_TYPES_SUPPORTED;
2699 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2700 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2701 GOTO(out, rc = -ENOTSUPP);
2704 list_for_each_entry_safe(com, next,
2705 &lfsck->li_list_scan, lc_link) {
2706 if (!(com->lc_type & start->ls_active)) {
2707 rc = com->lc_ops->lfsck_post(env, com, 0,
2714 while (start->ls_active != 0) {
2715 if (type & start->ls_active) {
2716 com = __lfsck_component_find(lfsck, type,
2717 &lfsck->li_list_idle);
2719 /* The component status will be updated
2720 * when its prep() is called later by
2721 * the LFSCK main engine. */
2722 list_move_tail(&com->lc_link,
2723 &lfsck->li_list_scan);
2724 start->ls_active &= ~type;
2730 if (list_empty(&lfsck->li_list_scan)) {
2731 /* The speed limit will be used to control both the LFSCK and
2732 * low layer scrub (if applied), need to be handled firstly. */
2733 if (start->ls_valid & LSV_SPEED_LIMIT) {
2734 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2735 rc = lfsck_bookmark_store(env, lfsck);
2744 if (start->ls_flags & LPF_RESET)
2745 flags |= DOIF_RESET;
2747 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2751 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2752 start->ls_active |= com->lc_type;
2753 if (flags & DOIF_RESET) {
2754 rc = com->lc_ops->lfsck_reset(env, com, false);
2761 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2762 if (bk->lb_param & LPF_DRYRUN)
2763 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2765 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2766 valid |= DOIV_ERROR_HANDLE;
2767 if (start->ls_flags & LPF_FAILOUT)
2768 flags |= DOIF_FAILOUT;
2771 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2772 valid |= DOIV_DRYRUN;
2773 if (start->ls_flags & LPF_DRYRUN)
2774 flags |= DOIF_DRYRUN;
2777 if (!list_empty(&lfsck->li_list_scan))
2778 flags |= DOIF_OUTUSED;
2780 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2781 thread_set_flags(thread, 0);
2782 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2784 GOTO(out, rc = PTR_ERR(lta));
2786 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2787 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2790 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2791 lfsck_lfsck2name(lfsck), rc);
2792 lfsck_thread_args_fini(lta);
2797 l_wait_event(thread->t_ctl_waitq,
2798 thread_is_running(thread) ||
2799 thread_is_stopped(thread),
2801 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2802 lfsck->li_start_unplug = 1;
2803 wake_up_all(&thread->t_ctl_waitq);
2808 /* release lfsck::li_mutex to avoid deadlock. */
2809 mutex_unlock(&lfsck->li_mutex);
2810 rc = lfsck_start_all(env, lfsck, start);
2812 spin_lock(&lfsck->li_lock);
2813 if (thread_is_stopped(thread)) {
2814 spin_unlock(&lfsck->li_lock);
2816 lfsck->li_status = LS_FAILED;
2817 lfsck->li_flags = 0;
2818 thread_set_flags(thread, SVC_STOPPING);
2819 spin_unlock(&lfsck->li_lock);
2821 lfsck->li_start_unplug = 1;
2822 wake_up_all(&thread->t_ctl_waitq);
2823 l_wait_event(thread->t_ctl_waitq,
2824 thread_is_stopped(thread),
2828 lfsck->li_start_unplug = 1;
2829 wake_up_all(&thread->t_ctl_waitq);
2835 mutex_unlock(&lfsck->li_mutex);
2838 lfsck_instance_put(env, lfsck);
2840 return rc < 0 ? rc : 0;
2842 EXPORT_SYMBOL(lfsck_start);
2844 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
2845 struct lfsck_stop *stop)
2847 struct lfsck_instance *lfsck;
2848 struct ptlrpc_thread *thread;
2849 struct l_wait_info lwi = { 0 };
2854 lfsck = lfsck_instance_find(key, true, false);
2855 if (unlikely(lfsck == NULL))
2858 thread = &lfsck->li_thread;
2859 /* release lfsck::li_mutex to avoid deadlock. */
2860 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
2861 if (!lfsck->li_master) {
2862 CERROR("%s: only allow to specify '-A' via MDS\n",
2863 lfsck_lfsck2name(lfsck));
2865 GOTO(out, rc = -EPERM);
2868 rc1 = lfsck_stop_all(env, lfsck, stop);
2871 mutex_lock(&lfsck->li_mutex);
2872 spin_lock(&lfsck->li_lock);
2873 /* no error if LFSCK is already stopped, or was never started */
2874 if (thread_is_init(thread) || thread_is_stopped(thread)) {
2875 spin_unlock(&lfsck->li_lock);
2880 lfsck->li_status = stop->ls_status;
2881 lfsck->li_flags = stop->ls_flags;
2883 lfsck->li_status = LS_STOPPED;
2884 lfsck->li_flags = 0;
2887 thread_set_flags(thread, SVC_STOPPING);
2888 spin_unlock(&lfsck->li_lock);
2890 wake_up_all(&thread->t_ctl_waitq);
2891 l_wait_event(thread->t_ctl_waitq,
2892 thread_is_stopped(thread),
2898 mutex_unlock(&lfsck->li_mutex);
2899 lfsck_instance_put(env, lfsck);
2901 return rc != 0 ? rc : rc1;
2903 EXPORT_SYMBOL(lfsck_stop);
2905 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
2906 struct lfsck_request *lr, struct thandle *th)
2908 int rc = -EOPNOTSUPP;
2911 switch (lr->lr_event) {
2913 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
2914 struct lfsck_start_param lsp;
2916 memset(start, 0, sizeof(*start));
2917 start->ls_valid = lr->lr_valid;
2918 start->ls_speed_limit = lr->lr_speed;
2919 start->ls_version = lr->lr_version;
2920 start->ls_active = lr->lr_active;
2921 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2922 start->ls_async_windows = lr->lr_async_windows;
2924 lsp.lsp_start = start;
2925 lsp.lsp_index = lr->lr_index;
2926 lsp.lsp_index_valid = 1;
2927 rc = lfsck_start(env, key, &lsp);
2931 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2933 memset(stop, 0, sizeof(*stop));
2934 stop->ls_status = lr->lr_status;
2935 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2936 rc = lfsck_stop(env, key, stop);
2939 case LE_PHASE1_DONE:
2940 case LE_PHASE2_DONE:
2941 case LE_FID_ACCESSED:
2943 case LE_CONDITIONAL_DESTROY:
2944 case LE_CREATE_ORPHAN:
2945 case LE_SKIP_NLINK_DECLARE:
2947 case LE_SET_LMV_MASTER:
2948 case LE_SET_LMV_SLAVE:
2949 case LE_PAIRS_VERIFY: {
2950 struct lfsck_instance *lfsck;
2951 struct lfsck_component *com;
2953 lfsck = lfsck_instance_find(key, true, false);
2954 if (unlikely(lfsck == NULL))
2957 com = lfsck_component_find(lfsck, lr->lr_active);
2958 if (likely(com != NULL)) {
2959 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
2960 lfsck_component_put(env, com);
2963 lfsck_instance_put(env, lfsck);
2972 EXPORT_SYMBOL(lfsck_in_notify);
2974 int lfsck_query(const struct lu_env *env, struct dt_device *key,
2975 struct lfsck_request *lr)
2977 struct lfsck_instance *lfsck;
2978 struct lfsck_component *com;
2982 lfsck = lfsck_instance_find(key, true, false);
2983 if (unlikely(lfsck == NULL))
2986 com = lfsck_component_find(lfsck, lr->lr_active);
2987 if (likely(com != NULL)) {
2988 rc = com->lc_ops->lfsck_query(env, com);
2989 lfsck_component_put(env, com);
2994 lfsck_instance_put(env, lfsck);
2998 EXPORT_SYMBOL(lfsck_query);
3000 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3001 struct ldlm_namespace *ns)
3003 struct lfsck_instance *lfsck;
3006 lfsck = lfsck_instance_find(key, true, false);
3007 if (likely(lfsck != NULL)) {
3008 lfsck->li_namespace = ns;
3009 lfsck_instance_put(env, lfsck);
3015 EXPORT_SYMBOL(lfsck_register_namespace);
3017 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3018 struct dt_device *next, struct obd_device *obd,
3019 lfsck_out_notify notify, void *notify_data, bool master)
3021 struct lfsck_instance *lfsck;
3022 struct dt_object *root = NULL;
3023 struct dt_object *obj = NULL;
3024 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3028 lfsck = lfsck_instance_find(key, false, false);
3029 if (unlikely(lfsck != NULL))
3032 OBD_ALLOC_PTR(lfsck);
3036 mutex_init(&lfsck->li_mutex);
3037 spin_lock_init(&lfsck->li_lock);
3038 INIT_LIST_HEAD(&lfsck->li_link);
3039 INIT_LIST_HEAD(&lfsck->li_list_scan);
3040 INIT_LIST_HEAD(&lfsck->li_list_dir);
3041 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3042 INIT_LIST_HEAD(&lfsck->li_list_idle);
3043 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3044 atomic_set(&lfsck->li_ref, 1);
3045 atomic_set(&lfsck->li_double_scan_count, 0);
3046 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3047 lfsck->li_out_notify = notify;
3048 lfsck->li_out_notify_data = notify_data;
3049 lfsck->li_next = next;
3050 lfsck->li_bottom = key;
3051 lfsck->li_obd = obd;
3053 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3057 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3061 fid->f_seq = FID_SEQ_LOCAL_NAME;
3064 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3068 rc = dt_root_get(env, key, fid);
3072 root = dt_locate(env, key, fid);
3074 GOTO(out, rc = PTR_ERR(root));
3076 if (unlikely(!dt_try_as_dir(env, root)))
3077 GOTO(out, rc = -ENOTDIR);
3079 lfsck->li_local_root_fid = *fid;
3081 lfsck->li_master = 1;
3082 if (lfsck_dev_idx(key) == 0) {
3083 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3084 const struct lu_name *cname;
3086 rc = dt_lookup(env, root,
3087 (struct dt_rec *)(&lfsck->li_global_root_fid),
3088 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3092 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3094 GOTO(out, rc = PTR_ERR(obj));
3096 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3097 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3101 lu_object_put(env, &obj->do_lu);
3102 obj = dt_locate(env, key, fid);
3104 GOTO(out, rc = PTR_ERR(obj));
3106 cname = lfsck_name_get_const(env, dotlustre,
3108 rc = lfsck_verify_linkea(env, key, obj, cname,
3109 &lfsck->li_global_root_fid);
3114 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3115 (const struct dt_key *)lostfound,
3120 lu_object_put(env, &obj->do_lu);
3121 obj = dt_locate(env, key, fid);
3123 GOTO(out, rc = PTR_ERR(obj));
3125 cname = lfsck_name_get_const(env, lostfound,
3127 rc = lfsck_verify_linkea(env, key, obj, cname, pfid);
3131 lu_object_put(env, &obj->do_lu);
3136 fid->f_seq = FID_SEQ_LOCAL_FILE;
3137 fid->f_oid = OTABLE_IT_OID;
3139 obj = dt_locate(env, key, fid);
3141 GOTO(out, rc = PTR_ERR(obj));
3143 lu_object_get(&obj->do_lu);
3144 lfsck->li_obj_oit = obj;
3145 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3149 rc = lfsck_bookmark_setup(env, lfsck);
3154 rc = lfsck_fid_init(lfsck);
3158 rc = lfsck_namespace_setup(env, lfsck);
3163 rc = lfsck_layout_setup(env, lfsck);
3167 /* XXX: more LFSCK components initialization to be added here. */
3169 rc = lfsck_instance_add(lfsck);
3171 rc = lfsck_add_target_from_orphan(env, lfsck);
3173 if (obj != NULL && !IS_ERR(obj))
3174 lu_object_put(env, &obj->do_lu);
3175 if (root != NULL && !IS_ERR(root))
3176 lu_object_put(env, &root->do_lu);
3178 lfsck_instance_cleanup(env, lfsck);
3181 EXPORT_SYMBOL(lfsck_register);
3183 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3185 struct lfsck_instance *lfsck;
3187 lfsck = lfsck_instance_find(key, false, true);
3189 lfsck_instance_put(env, lfsck);
3191 EXPORT_SYMBOL(lfsck_degister);
3193 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3194 struct dt_device *tgt, struct obd_export *exp,
3195 __u32 index, bool for_ost)
3197 struct lfsck_instance *lfsck;
3198 struct lfsck_tgt_desc *ltd;
3209 INIT_LIST_HEAD(<d->ltd_orphan_list);
3210 INIT_LIST_HEAD(<d->ltd_layout_list);
3211 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3212 INIT_LIST_HEAD(<d->ltd_namespace_list);
3213 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3214 atomic_set(<d->ltd_ref, 1);
3215 ltd->ltd_index = index;
3217 spin_lock(&lfsck_instance_lock);
3218 lfsck = __lfsck_instance_find(key, true, false);
3219 if (lfsck == NULL) {
3221 list_add_tail(<d->ltd_orphan_list,
3222 &lfsck_ost_orphan_list);
3224 list_add_tail(<d->ltd_orphan_list,
3225 &lfsck_mdt_orphan_list);
3226 spin_unlock(&lfsck_instance_lock);
3230 spin_unlock(&lfsck_instance_lock);
3232 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3236 lfsck_instance_put(env, lfsck);
3240 EXPORT_SYMBOL(lfsck_add_target);
3242 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3243 struct dt_device *tgt, __u32 index, bool for_ost)
3245 struct lfsck_instance *lfsck;
3246 struct lfsck_tgt_descs *ltds;
3247 struct lfsck_tgt_desc *ltd;
3248 struct list_head *head;
3251 head = &lfsck_ost_orphan_list;
3253 head = &lfsck_mdt_orphan_list;
3255 spin_lock(&lfsck_instance_lock);
3256 list_for_each_entry(ltd, head, ltd_orphan_list) {
3257 if (ltd->ltd_tgt == tgt) {
3258 list_del_init(<d->ltd_orphan_list);
3259 spin_unlock(&lfsck_instance_lock);
3267 lfsck = __lfsck_instance_find(key, true, false);
3268 spin_unlock(&lfsck_instance_lock);
3269 if (unlikely(lfsck == NULL))
3273 ltds = &lfsck->li_ost_descs;
3275 ltds = &lfsck->li_mdt_descs;
3277 down_write(<ds->ltd_rw_sem);
3278 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3280 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3283 ltd = LTD_TGT(ltds, index);
3284 if (unlikely(ltd == NULL))
3287 LASSERT(ltds->ltd_tgtnr > 0);
3290 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3291 LTD_TGT(ltds, index) = NULL;
3296 head = &lfsck->li_ost_descs.ltd_orphan;
3298 head = &lfsck->li_mdt_descs.ltd_orphan;
3300 list_for_each_entry(ltd, head, ltd_orphan_list) {
3301 if (ltd->ltd_tgt == tgt) {
3302 list_del_init(<d->ltd_orphan_list);
3308 up_write(<ds->ltd_rw_sem);
3310 spin_lock(<ds->ltd_lock);
3312 spin_unlock(<ds->ltd_lock);
3313 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3314 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3318 lfsck_instance_put(env, lfsck);
3320 EXPORT_SYMBOL(lfsck_del_target);
3322 static int __init lfsck_init(void)
3326 INIT_LIST_HEAD(&lfsck_instance_list);
3327 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3328 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3329 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3330 rc = lu_context_key_register(&lfsck_thread_key);
3332 tgt_register_lfsck_in_notify(lfsck_in_notify);
3333 tgt_register_lfsck_query(lfsck_query);
3339 static void __exit lfsck_exit(void)
3341 struct lfsck_tgt_desc *ltd;
3342 struct lfsck_tgt_desc *next;
3344 LASSERT(list_empty(&lfsck_instance_list));
3346 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3348 list_del_init(<d->ltd_orphan_list);
3352 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3354 list_del_init(<d->ltd_orphan_list);
3358 lu_context_key_degister(&lfsck_thread_key);
3361 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3362 MODULE_DESCRIPTION("LFSCK");
3363 MODULE_LICENSE("GPL");
3365 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);