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[] = {
104 enum lfsck_verify_lpf_types {
105 LVLT_BY_BOOKMARK = 0,
106 LVLT_BY_NAMEENTRY = 1,
109 const char *lfsck_status2names(enum lfsck_status status)
111 if (unlikely(status < 0 || status >= LS_MAX))
114 return lfsck_status_names[status];
117 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
119 spin_lock_init(<ds->ltd_lock);
120 init_rwsem(<ds->ltd_rw_sem);
121 INIT_LIST_HEAD(<ds->ltd_orphan);
122 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
123 if (ltds->ltd_tgts_bitmap == NULL)
129 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
131 struct lfsck_tgt_desc *ltd;
132 struct lfsck_tgt_desc *next;
135 down_write(<ds->ltd_rw_sem);
137 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
139 list_del_init(<d->ltd_orphan_list);
143 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
144 up_write(<ds->ltd_rw_sem);
149 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
150 ltd = LTD_TGT(ltds, idx);
151 if (likely(ltd != NULL)) {
152 LASSERT(list_empty(<d->ltd_layout_list));
153 LASSERT(list_empty(<d->ltd_layout_phase_list));
154 LASSERT(list_empty(<d->ltd_namespace_list));
155 LASSERT(list_empty(<d->ltd_namespace_phase_list));
158 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
159 LTD_TGT(ltds, idx) = NULL;
164 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
167 for (idx = 0; idx < TGT_PTRS; idx++) {
168 if (ltds->ltd_tgts_idx[idx] != NULL) {
169 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
170 ltds->ltd_tgts_idx[idx] = NULL;
174 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
175 ltds->ltd_tgts_bitmap = NULL;
176 up_write(<ds->ltd_rw_sem);
179 static int __lfsck_add_target(const struct lu_env *env,
180 struct lfsck_instance *lfsck,
181 struct lfsck_tgt_desc *ltd,
182 bool for_ost, bool locked)
184 struct lfsck_tgt_descs *ltds;
185 __u32 index = ltd->ltd_index;
190 ltds = &lfsck->li_ost_descs;
192 ltds = &lfsck->li_mdt_descs;
195 down_write(<ds->ltd_rw_sem);
197 LASSERT(ltds->ltd_tgts_bitmap != NULL);
199 if (index >= ltds->ltd_tgts_bitmap->size) {
200 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
201 (__u32)BITS_PER_LONG);
202 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
203 cfs_bitmap_t *new_bitmap;
205 while (newsize < index + 1)
208 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
209 if (new_bitmap == NULL)
210 GOTO(unlock, rc = -ENOMEM);
212 if (ltds->ltd_tgtnr > 0)
213 cfs_bitmap_copy(new_bitmap, old_bitmap);
214 ltds->ltd_tgts_bitmap = new_bitmap;
215 CFS_FREE_BITMAP(old_bitmap);
218 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
219 CERROR("%s: the device %s (%u) is registered already\n",
220 lfsck_lfsck2name(lfsck),
221 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
222 GOTO(unlock, rc = -EEXIST);
225 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
226 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
227 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
228 GOTO(unlock, rc = -ENOMEM);
231 LTD_TGT(ltds, index) = ltd;
232 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
235 GOTO(unlock, rc = 0);
239 up_write(<ds->ltd_rw_sem);
244 static int lfsck_add_target_from_orphan(const struct lu_env *env,
245 struct lfsck_instance *lfsck)
247 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
248 struct lfsck_tgt_desc *ltd;
249 struct lfsck_tgt_desc *next;
250 struct list_head *head = &lfsck_ost_orphan_list;
255 spin_lock(&lfsck_instance_lock);
256 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
257 if (ltd->ltd_key == lfsck->li_bottom)
258 list_move_tail(<d->ltd_orphan_list,
261 spin_unlock(&lfsck_instance_lock);
263 down_write(<ds->ltd_rw_sem);
264 while (!list_empty(<ds->ltd_orphan)) {
265 ltd = list_entry(ltds->ltd_orphan.next,
266 struct lfsck_tgt_desc,
268 list_del_init(<d->ltd_orphan_list);
269 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
270 /* Do not hold the semaphore for too long time. */
271 up_write(<ds->ltd_rw_sem);
275 down_write(<ds->ltd_rw_sem);
277 up_write(<ds->ltd_rw_sem);
280 ltds = &lfsck->li_mdt_descs;
281 head = &lfsck_mdt_orphan_list;
289 static inline struct lfsck_component *
290 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
291 struct list_head *list)
293 struct lfsck_component *com;
295 list_for_each_entry(com, list, lc_link) {
296 if (com->lc_type == type)
302 struct lfsck_component *
303 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
305 struct lfsck_component *com;
307 spin_lock(&lfsck->li_lock);
308 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
312 com = __lfsck_component_find(lfsck, type,
313 &lfsck->li_list_double_scan);
317 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
321 lfsck_component_get(com);
322 spin_unlock(&lfsck->li_lock);
326 void lfsck_component_cleanup(const struct lu_env *env,
327 struct lfsck_component *com)
329 if (!list_empty(&com->lc_link))
330 list_del_init(&com->lc_link);
331 if (!list_empty(&com->lc_link_dir))
332 list_del_init(&com->lc_link_dir);
334 lfsck_component_put(env, com);
337 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
338 struct lu_fid *fid, bool locked)
340 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
345 mutex_lock(&lfsck->li_mutex);
347 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
349 bk->lb_last_fid = *fid;
350 /* We do not care about whether the subsequent sub-operations
351 * failed or not. The worst case is that one FID is lost that
352 * is not a big issue for the LFSCK since it is relative rare
353 * for LFSCK create. */
354 rc = lfsck_bookmark_store(env, lfsck);
358 mutex_unlock(&lfsck->li_mutex);
364 * Request the specified ibits lock for the given object.
366 * Before the LFSCK modifying on the namespace visible object,
367 * it needs to acquire related ibits ldlm lock.
369 * \param[in] env pointer to the thread context
370 * \param[in] lfsck pointer to the lfsck instance
371 * \param[in] obj pointer to the dt_object to be locked
372 * \param[out] lh pointer to the lock handle
373 * \param[in] ibits the bits for the ldlm lock to be acquired
374 * \param[in] mode the mode for the ldlm lock to be acquired
376 * \retval 0 for success
377 * \retval negative error number on failure
379 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
380 struct dt_object *obj, struct lustre_handle *lh,
381 __u64 bits, ldlm_mode_t mode)
383 struct lfsck_thread_info *info = lfsck_env_info(env);
384 ldlm_policy_data_t *policy = &info->lti_policy;
385 struct ldlm_res_id *resid = &info->lti_resid;
386 __u64 flags = LDLM_FL_ATOMIC_CB;
389 LASSERT(lfsck->li_namespace != NULL);
391 memset(policy, 0, sizeof(*policy));
392 policy->l_inodebits.bits = bits;
393 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
394 if (dt_object_remote(obj)) {
395 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
397 memset(einfo, 0, sizeof(*einfo));
398 einfo->ei_type = LDLM_IBITS;
399 einfo->ei_mode = mode;
400 einfo->ei_cb_bl = ldlm_blocking_ast;
401 einfo->ei_cb_cp = ldlm_completion_ast;
402 einfo->ei_res_id = resid;
404 rc = dt_object_lock(env, obj, lh, einfo, policy);
406 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
407 LDLM_IBITS, policy, mode,
408 &flags, ldlm_blocking_ast,
409 ldlm_completion_ast, NULL, NULL,
410 0, LVB_T_NONE, NULL, lh);
413 if (rc == ELDLM_OK) {
416 memset(lh, 0, sizeof(*lh));
424 * Release the the specified ibits lock.
426 * If the lock has been acquired before, release it
427 * and cleanup the handle. Otherwise, do nothing.
429 * \param[in] lh pointer to the lock handle
430 * \param[in] mode the mode for the ldlm lock to be released
432 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
434 if (lustre_handle_is_used(lh)) {
435 ldlm_lock_decref(lh, mode);
436 memset(lh, 0, sizeof(*lh));
440 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
441 struct lfsck_instance *lfsck,
442 const struct lu_fid *fid)
444 struct seq_server_site *ss =
445 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
446 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
449 fld_range_set_mdt(range);
450 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
452 rc = range->lsr_index;
457 const char dot[] = ".";
458 const char dotdot[] = "..";
459 static const char dotlustre[] = ".lustre";
460 static const char lostfound[] = "lost+found";
462 static int lfsck_create_lpf_local(const struct lu_env *env,
463 struct lfsck_instance *lfsck,
464 struct dt_object *parent,
465 struct dt_object *child,
467 struct dt_object_format *dof,
470 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
471 struct dt_device *dev = lfsck->li_bottom;
472 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
473 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
474 const struct lu_fid *cfid = lfsck_dto2fid(child);
475 struct thandle *th = NULL;
476 struct linkea_data ldata = { 0 };
477 struct lu_buf linkea_buf;
478 const struct lu_name *cname;
480 int len = sizeof(struct lfsck_bookmark);
484 rc = linkea_data_new(&ldata,
485 &lfsck_env_info(env)->lti_linkea_buf2);
489 cname = lfsck_name_get_const(env, name, strlen(name));
490 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
494 th = dt_trans_create(env, dev);
498 /* 1a. create child */
499 rc = dt_declare_create(env, child, la, NULL, dof, th);
503 /* 2a. increase child nlink */
504 rc = dt_declare_ref_add(env, child, th);
508 /* 3a. insert linkEA for child */
509 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
510 ldata.ld_leh->leh_len);
511 rc = dt_declare_xattr_set(env, child, &linkea_buf,
512 XATTR_NAME_LINK, 0, th);
516 /* 4a. insert name into parent dir */
517 rec->rec_type = S_IFDIR;
519 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
520 (const struct dt_key *)name, th);
524 /* 5a. increase parent nlink */
525 rc = dt_declare_ref_add(env, parent, th);
529 /* 6a. update bookmark */
530 rc = dt_declare_record_write(env, bk_obj,
531 lfsck_buf_get(env, bk, len), 0, th);
535 rc = dt_trans_start_local(env, dev, th);
539 dt_write_lock(env, child, 0);
540 /* 1b.1. create child */
541 rc = dt_create(env, child, la, NULL, dof, th);
545 if (unlikely(!dt_try_as_dir(env, child)))
546 GOTO(unlock, rc = -ENOTDIR);
548 /* 1b.2. insert dot into child dir */
550 rc = dt_insert(env, child, (const struct dt_rec *)rec,
551 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
555 /* 1b.3. insert dotdot into child dir */
556 rec->rec_fid = &LU_LPF_FID;
557 rc = dt_insert(env, child, (const struct dt_rec *)rec,
558 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
562 /* 2b. increase child nlink */
563 rc = dt_ref_add(env, child, th);
567 /* 3b. insert linkEA for child. */
568 rc = dt_xattr_set(env, child, &linkea_buf,
569 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
570 dt_write_unlock(env, child);
574 /* 4b. insert name into parent dir */
576 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
577 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
581 dt_write_lock(env, parent, 0);
582 /* 5b. increase parent nlink */
583 rc = dt_ref_add(env, parent, th);
584 dt_write_unlock(env, parent);
588 bk->lb_lpf_fid = *cfid;
589 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
591 /* 6b. update bookmark */
592 rc = dt_record_write(env, bk_obj,
593 lfsck_buf_get(env, bk, len), &pos, th);
598 dt_write_unlock(env, child);
601 dt_trans_stop(env, dev, th);
606 static int lfsck_create_lpf_remote(const struct lu_env *env,
607 struct lfsck_instance *lfsck,
608 struct dt_object *parent,
609 struct dt_object *child,
611 struct dt_object_format *dof,
614 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
615 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
616 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
617 const struct lu_fid *cfid = lfsck_dto2fid(child);
618 struct thandle *th = NULL;
619 struct linkea_data ldata = { 0 };
620 struct lu_buf linkea_buf;
621 const struct lu_name *cname;
622 struct dt_device *dev;
624 int len = sizeof(struct lfsck_bookmark);
628 rc = linkea_data_new(&ldata,
629 &lfsck_env_info(env)->lti_linkea_buf2);
633 cname = lfsck_name_get_const(env, name, strlen(name));
634 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
638 /* Create .lustre/lost+found/MDTxxxx. */
640 /* XXX: Currently, cross-MDT create operation needs to create the child
641 * object firstly, then insert name into the parent directory. For
642 * this case, the child object resides on current MDT (local), but
643 * the parent ".lustre/lost+found" may be on remote MDT. It is not
644 * easy to contain all the sub-modifications orderly within single
647 * To avoid more inconsistency, we split the create operation into
650 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
652 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
655 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
656 * repair such inconsistency when LFSCK run next time. */
658 /* Transaction I: locally */
660 dev = lfsck->li_bottom;
661 th = dt_trans_create(env, dev);
665 /* 1a. create child */
666 rc = dt_declare_create(env, child, la, NULL, dof, th);
670 /* 2a. increase child nlink */
671 rc = dt_declare_ref_add(env, child, th);
675 /* 3a. insert linkEA for child */
676 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
677 ldata.ld_leh->leh_len);
678 rc = dt_declare_xattr_set(env, child, &linkea_buf,
679 XATTR_NAME_LINK, 0, th);
683 /* 4a. update bookmark */
684 rc = dt_declare_record_write(env, bk_obj,
685 lfsck_buf_get(env, bk, len), 0, th);
689 rc = dt_trans_start_local(env, dev, th);
693 dt_write_lock(env, child, 0);
694 /* 1b.1. create child */
695 rc = dt_create(env, child, la, NULL, dof, th);
699 if (unlikely(!dt_try_as_dir(env, child)))
700 GOTO(unlock, rc = -ENOTDIR);
702 /* 1b.2. insert dot into child dir */
703 rec->rec_type = S_IFDIR;
705 rc = dt_insert(env, child, (const struct dt_rec *)rec,
706 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
710 /* 1b.3. insert dotdot into child dir */
711 rec->rec_fid = &LU_LPF_FID;
712 rc = dt_insert(env, child, (const struct dt_rec *)rec,
713 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
717 /* 2b. increase child nlink */
718 rc = dt_ref_add(env, child, th);
722 /* 3b. insert linkEA for child */
723 rc = dt_xattr_set(env, child, &linkea_buf,
724 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
728 bk->lb_lpf_fid = *cfid;
729 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
731 /* 4b. update bookmark */
732 rc = dt_record_write(env, bk_obj,
733 lfsck_buf_get(env, bk, len), &pos, th);
735 dt_write_unlock(env, child);
736 dt_trans_stop(env, dev, th);
740 /* Transaction II: remotely */
742 dev = lfsck->li_next;
743 th = dt_trans_create(env, dev);
747 /* 5a. insert name into parent dir */
749 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
750 (const struct dt_key *)name, th);
754 /* 6a. increase parent nlink */
755 rc = dt_declare_ref_add(env, parent, th);
759 rc = dt_trans_start(env, dev, th);
763 /* 5b. insert name into parent dir */
764 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
765 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
769 dt_write_lock(env, parent, 0);
770 /* 6b. increase parent nlink */
771 rc = dt_ref_add(env, parent, th);
772 dt_write_unlock(env, parent);
777 dt_write_unlock(env, child);
779 dt_trans_stop(env, dev, th);
781 if (rc != 0 && dev == lfsck->li_next)
782 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
783 "for orphans, but failed to insert the name %s "
784 "to the .lustre/lost+found/. Such inconsistency "
785 "will be repaired when LFSCK run next time: rc = %d\n",
786 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
791 /* Do NOT create .lustre/lost+found/MDTxxxx when register the lfsck instance,
792 * because the MDT0 maybe not reaady for sequence allocation yet. We do that
793 * only when it is required, such as orphan OST-objects repairing. */
794 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
796 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
797 struct lfsck_thread_info *info = lfsck_env_info(env);
798 struct lu_fid *cfid = &info->lti_fid2;
799 struct lu_attr *la = &info->lti_la;
800 struct dt_object_format *dof = &info->lti_dof;
801 struct dt_object *parent = NULL;
802 struct dt_object *child = NULL;
803 struct lustre_handle lh = { 0 };
805 int node = lfsck_dev_idx(lfsck->li_bottom);
809 LASSERT(lfsck->li_master);
811 sprintf(name, "MDT%04x", node);
813 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
816 struct lfsck_tgt_desc *ltd;
818 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
819 if (unlikely(ltd == NULL))
822 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
827 RETURN(PTR_ERR(parent));
829 if (lfsck->li_lpf_obj != NULL)
832 if (unlikely(!dt_try_as_dir(env, parent)))
833 GOTO(out, rc = -ENOTDIR);
835 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
836 MDS_INODELOCK_UPDATE, LCK_EX);
840 mutex_lock(&lfsck->li_mutex);
841 if (lfsck->li_lpf_obj != NULL)
842 GOTO(unlock, rc = 0);
844 if (fid_is_zero(&bk->lb_lpf_fid)) {
845 /* There is corner case that: in former LFSCK scanning we have
846 * created the .lustre/lost+found/MDTxxxx but failed to update
847 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
848 * it from MDT0 firstly. */
849 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
850 (const struct dt_key *)name, BYPASS_CAPA);
851 if (rc != 0 && rc != -ENOENT)
855 bk->lb_lpf_fid = *cfid;
856 rc = lfsck_bookmark_store(env, lfsck);
858 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
863 *cfid = bk->lb_lpf_fid;
866 child = lfsck_object_find_by_dev(env, lfsck->li_bottom, cfid);
868 GOTO(unlock, rc = PTR_ERR(child));
870 if (dt_object_exists(child) != 0) {
871 if (unlikely(!dt_try_as_dir(env, child)))
874 lfsck->li_lpf_obj = child;
879 memset(la, 0, sizeof(*la));
880 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
881 la->la_mode = S_IFDIR | S_IRWXU;
882 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
884 memset(dof, 0, sizeof(*dof));
885 dof->dof_type = dt_mode_to_dft(S_IFDIR);
888 rc = lfsck_create_lpf_local(env, lfsck, parent, child, la,
891 rc = lfsck_create_lpf_remote(env, lfsck, parent, child, la,
894 lfsck->li_lpf_obj = child;
899 mutex_unlock(&lfsck->li_mutex);
900 lfsck_ibits_unlock(&lh, LCK_EX);
901 if (rc != 0 && child != NULL && !IS_ERR(child))
902 lu_object_put(env, &child->do_lu);
904 if (parent != NULL && !IS_ERR(parent))
905 lu_object_put(env, &parent->do_lu);
911 * Scan .lustre/lost+found for bad name entries and remove them.
913 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
914 * index in the system. Any other formatted name is invalid and should be
917 * \param[in] env pointer to the thread context
918 * \param[in] lfsck pointer to the lfsck instance
919 * \param[in] parent pointer to the lost+found object
921 * \retval 0 for success
922 * \retval negative error number on failure
924 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
925 struct lfsck_instance *lfsck,
926 struct dt_object *parent)
928 struct lu_dirent *ent =
929 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
930 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
935 it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA);
939 rc = iops->load(env, it, 0);
941 rc = iops->next(env, it);
948 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
952 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
953 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
956 /* name length must be strlen("MDTxxxx") */
957 if (ent->lde_namelen != 7)
960 if (memcmp(ent->lde_name, "MDT", off) != 0)
963 while (off < 7 && isxdigit(ent->lde_name[off]))
969 rc = lfsck_remove_name_entry(env, lfsck, parent,
970 ent->lde_name, S_IFDIR);
976 rc = iops->next(env, it);
982 RETURN(rc > 0 ? 0 : rc);
985 static int lfsck_update_lpf_entry(const struct lu_env *env,
986 struct lfsck_instance *lfsck,
987 struct dt_object *parent,
988 struct dt_object *child,
990 enum lfsck_verify_lpf_types type)
994 if (type == LVLT_BY_BOOKMARK) {
995 rc = lfsck_update_name_entry(env, lfsck, parent, name,
996 lfsck_dto2fid(child), S_IFDIR);
997 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
998 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
999 rc = lfsck_bookmark_store(env, lfsck);
1001 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1002 " in the bookmark file: rc = %d\n",
1003 lfsck_lfsck2name(lfsck),
1004 PFID(lfsck_dto2fid(child)), rc);
1011 * Check whether the @child back references the @parent.
1014 * 1) The child's FID is stored in the bookmark file. If the child back
1015 * references the parent (LU_LPF_FID object) via its ".." entry, then
1016 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1017 * the child back references another parent2, then:
1018 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1019 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1020 * references the child. So keep them there. As the LFSCK processing,
1021 * the parent3 may be found, then when the LFSCK run next time, the
1022 * inconsistency can be repaired.
1024 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1025 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1026 * via its ".." entry, then update the bookmark file, otherwise, if the child
1027 * back references another parent2, then:
1028 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1029 * from .lustre/lost+found/;
1030 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1031 * sub-directory name entry and update the child;
1032 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1033 * or not, then keep them there.
1035 * \param[in] env pointer to the thread context
1036 * \param[in] lfsck pointer to the lfsck instance
1037 * \param[in] parent pointer to the lost+found object
1038 * \param[in] child pointer to the lost+found sub-directory object
1039 * \param[in] name the name for lost+found sub-directory object
1040 * \param[out] fid pointer to the buffer to hold the FID of the object
1041 * (called it as parent2) that is referenced via the
1042 * child's dotdot entry; it also can be the FID that
1043 * is referenced by the name entry under the parent2.
1044 * \param[in] type to indicate where the child's FID is stored in
1046 * \retval positive number for uncertain inconsistency
1047 * \retval 0 for success
1048 * \retval negative error number on failure
1050 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1051 struct lfsck_instance *lfsck,
1052 struct dt_object *parent,
1053 struct dt_object *child, const char *name,
1055 enum lfsck_verify_lpf_types type)
1057 struct lfsck_thread_info *info = lfsck_env_info(env);
1058 char *name2 = info->lti_key;
1059 struct lu_fid *fid2 = &info->lti_fid3;
1060 struct dt_object *parent2 = NULL;
1061 struct lustre_handle lh = { 0 };
1066 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1067 (const struct dt_key *)dotdot, BYPASS_CAPA);
1071 if (!fid_is_sane(fid))
1072 GOTO(linkea, rc = -EINVAL);
1074 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1075 const struct lu_name *cname;
1077 if (lfsck->li_lpf_obj == NULL) {
1078 lu_object_get(&child->do_lu);
1079 lfsck->li_lpf_obj = child;
1082 cname = lfsck_name_get_const(env, name, strlen(name));
1083 rc = lfsck_verify_linkea(env, lfsck->li_bottom, child, cname,
1086 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1092 parent2 = lfsck_object_find_by_dev(env, lfsck->li_next, fid);
1093 if (IS_ERR(parent2))
1094 GOTO(linkea, parent2);
1096 if (!dt_object_exists(parent2)) {
1097 lu_object_put(env, &parent2->do_lu);
1099 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1102 if (!dt_try_as_dir(env, parent2)) {
1103 lu_object_put(env, &parent2->do_lu);
1105 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1109 /* To prevent rename/unlink race */
1110 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1111 MDS_INODELOCK_UPDATE, LCK_PR);
1115 dt_read_lock(env, child, 0);
1116 rc = lfsck_links_get_first(env, child, name2, fid2);
1118 dt_read_unlock(env, child);
1119 lfsck_ibits_unlock(&lh, LCK_PR);
1121 GOTO(out_put, rc = 1);
1124 /* It is almost impossible that the bookmark file (or the name entry)
1125 * and the linkEA hit the same data corruption. Trust the linkEA. */
1126 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1127 dt_read_unlock(env, child);
1128 lfsck_ibits_unlock(&lh, LCK_PR);
1131 if (lfsck->li_lpf_obj == NULL) {
1132 lu_object_get(&child->do_lu);
1133 lfsck->li_lpf_obj = child;
1136 /* Update the child's dotdot entry */
1137 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1138 &LU_LPF_FID, S_IFDIR);
1140 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1146 if (parent2 == NULL || IS_ERR(parent2)) {
1147 dt_read_unlock(env, child);
1148 lfsck_ibits_unlock(&lh, LCK_PR);
1150 GOTO(out_done, rc = 1);
1153 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1154 (const struct dt_key *)name2, BYPASS_CAPA);
1155 dt_read_unlock(env, child);
1156 lfsck_ibits_unlock(&lh, LCK_PR);
1157 if (rc != 0 && rc != -ENOENT)
1160 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1161 if (type == LVLT_BY_BOOKMARK)
1162 GOTO(out_put, rc = 1);
1164 /* Trust the name entry, update the child's dotdot entry. */
1165 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1166 &LU_LPF_FID, S_IFDIR);
1171 if (type == LVLT_BY_BOOKMARK) {
1172 /* Invalid FID record in the bookmark file, reset it. */
1173 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1174 rc = lfsck_bookmark_store(env, lfsck);
1176 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1177 " in the bookmark file: rc = %d\n",
1178 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1179 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1180 /* The name entry is wrong, remove it. */
1181 rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR);
1187 if (parent2 != NULL && !IS_ERR(parent2))
1188 lu_object_put(env, &parent2->do_lu);
1195 * Verify the /ROOT/.lustre/lost+found/ directory.
1197 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1198 * the LFSCK does not exactly know how to handle, such as orphans. So before
1199 * the LFSCK scanning the system, the consistency of such directory needs to
1200 * be verified firstly to allow the users to use it during the LFSCK.
1202 * \param[in] env pointer to the thread context
1203 * \param[in] lfsck pointer to the lfsck instance
1205 * \retval positive number for uncertain inconsistency
1206 * \retval 0 for success
1207 * \retval negative error number on failure
1209 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1211 struct lfsck_thread_info *info = lfsck_env_info(env);
1212 struct lu_fid *pfid = &info->lti_fid;
1213 struct lu_fid *cfid = &info->lti_fid2;
1214 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1215 struct dt_object *parent = NULL;
1216 /* child1's FID is in the bookmark file. */
1217 struct dt_object *child1 = NULL;
1218 /* child2's FID is in the name entry MDTxxxx. */
1219 struct dt_object *child2 = NULL;
1220 struct dt_device *dev = lfsck->li_bottom;
1221 const struct lu_name *cname;
1223 int node = lfsck_dev_idx(dev);
1227 LASSERT(lfsck->li_master);
1230 parent = lfsck_object_find_by_dev(env, dev, &LU_LPF_FID);
1232 struct lfsck_tgt_desc *ltd;
1234 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1235 if (unlikely(ltd == NULL))
1238 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1244 RETURN(PTR_ERR(parent));
1246 LASSERT(dt_object_exists(parent));
1248 if (unlikely(!dt_try_as_dir(env, parent)))
1249 GOTO(put, rc = -ENOTDIR);
1252 rc = lfsck_scan_lpf_bad_entries(env, lfsck, parent);
1254 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1255 "for bad sub-directories: rc = %d\n",
1256 lfsck_lfsck2name(lfsck), rc);
1259 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1260 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1261 struct lu_fid tfid = bk->lb_lpf_fid;
1263 /* Invalid FID record in the bookmark file, reset it. */
1264 fid_zero(&bk->lb_lpf_fid);
1265 rc = lfsck_bookmark_store(env, lfsck);
1267 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1268 " in the bookmark file: rc = %d\n",
1269 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1274 child1 = lfsck_object_find_by_dev(env, dev,
1277 GOTO(put, rc = PTR_ERR(child1));
1279 if (unlikely(!dt_object_exists(child1) ||
1280 dt_object_remote(child1)) ||
1281 !S_ISDIR(lfsck_object_type(child1))) {
1282 /* Invalid FID record in the bookmark file,
1284 fid_zero(&bk->lb_lpf_fid);
1285 rc = lfsck_bookmark_store(env, lfsck);
1287 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1288 " in the bookmark file: rc = %d\n",
1289 lfsck_lfsck2name(lfsck),
1290 PFID(lfsck_dto2fid(child1)), rc);
1295 lu_object_put(env, &child1->do_lu);
1297 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1298 GOTO(put, rc = -ENOTDIR);
1303 snprintf(name, 8, "MDT%04x", node);
1304 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1305 (const struct dt_key *)name, BYPASS_CAPA);
1306 if (rc == -ENOENT) {
1307 if (!fid_is_zero(&bk->lb_lpf_fid))
1316 /* Invalid FID in the name entry, remove the name entry. */
1317 if (!fid_is_norm(cfid)) {
1318 rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR);
1325 child2 = lfsck_object_find_by_dev(env, dev, cfid);
1327 GOTO(put, rc = PTR_ERR(child2));
1329 if (unlikely(!dt_object_exists(child2) ||
1330 dt_object_remote(child2)) ||
1331 !S_ISDIR(lfsck_object_type(child2))) {
1332 rc = lfsck_remove_name_entry(env, lfsck, parent, name,
1340 if (unlikely(!dt_try_as_dir(env, child2)))
1341 GOTO(put, rc = -ENOTDIR);
1343 if (child1 == NULL) {
1344 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2, name,
1345 pfid, LVLT_BY_NAMEENTRY);
1346 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1347 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name,
1348 pfid, LVLT_BY_BOOKMARK);
1349 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1350 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2,
1354 if (lfsck->li_lpf_obj == NULL) {
1355 lu_object_get(&child2->do_lu);
1356 lfsck->li_lpf_obj = child2;
1359 cname = lfsck_name_get_const(env, name, strlen(name));
1360 rc = lfsck_verify_linkea(env, dev, child2, cname, &LU_LPF_FID);
1367 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name,
1368 pfid, LVLT_BY_BOOKMARK);
1373 if (lfsck->li_lpf_obj != NULL &&
1374 unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj)))
1377 if (child2 != NULL && !IS_ERR(child2))
1378 lu_object_put(env, &child2->do_lu);
1379 if (child1 != NULL && !IS_ERR(child1))
1380 lu_object_put(env, &child1->do_lu);
1381 if (parent != NULL && !IS_ERR(parent))
1382 lu_object_put(env, &parent->do_lu);
1387 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1389 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1390 struct seq_server_site *ss;
1395 ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1396 if (unlikely(ss == NULL))
1399 OBD_ALLOC_PTR(lfsck->li_seq);
1400 if (lfsck->li_seq == NULL)
1403 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1405 GOTO(out, rc = -ENOMEM);
1407 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1408 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1410 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1414 if (fid_is_sane(&bk->lb_last_fid))
1415 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1420 OBD_FREE_PTR(lfsck->li_seq);
1421 lfsck->li_seq = NULL;
1426 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1428 if (lfsck->li_seq != NULL) {
1429 seq_client_fini(lfsck->li_seq);
1430 OBD_FREE_PTR(lfsck->li_seq);
1431 lfsck->li_seq = NULL;
1435 void lfsck_instance_cleanup(const struct lu_env *env,
1436 struct lfsck_instance *lfsck)
1438 struct ptlrpc_thread *thread = &lfsck->li_thread;
1439 struct lfsck_component *com;
1440 struct lfsck_component *next;
1441 struct lfsck_lmv_unit *llu;
1442 struct lfsck_lmv_unit *llu_next;
1443 struct lfsck_lmv *llmv;
1446 LASSERT(list_empty(&lfsck->li_link));
1447 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1449 if (lfsck->li_obj_oit != NULL) {
1450 lu_object_put_nocache(env, &lfsck->li_obj_oit->do_lu);
1451 lfsck->li_obj_oit = NULL;
1454 LASSERT(lfsck->li_obj_dir == NULL);
1455 LASSERT(lfsck->li_lmv == NULL);
1457 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1458 llmv = &llu->llu_lmv;
1460 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1461 "still in using: %u\n",
1462 atomic_read(&llmv->ll_ref));
1464 lfsck_lmv_put(env, llmv);
1467 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1468 lfsck_component_cleanup(env, com);
1471 LASSERT(list_empty(&lfsck->li_list_dir));
1473 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1475 lfsck_component_cleanup(env, com);
1478 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1479 lfsck_component_cleanup(env, com);
1482 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1483 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1485 if (lfsck->li_bookmark_obj != NULL) {
1486 lu_object_put_nocache(env, &lfsck->li_bookmark_obj->do_lu);
1487 lfsck->li_bookmark_obj = NULL;
1490 if (lfsck->li_lpf_obj != NULL) {
1491 lu_object_put(env, &lfsck->li_lpf_obj->do_lu);
1492 lfsck->li_lpf_obj = NULL;
1495 if (lfsck->li_los != NULL) {
1496 local_oid_storage_fini(env, lfsck->li_los);
1497 lfsck->li_los = NULL;
1500 lfsck_fid_fini(lfsck);
1502 OBD_FREE_PTR(lfsck);
1505 static inline struct lfsck_instance *
1506 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1508 struct lfsck_instance *lfsck;
1510 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1511 if (lfsck->li_bottom == key) {
1513 lfsck_instance_get(lfsck);
1515 list_del_init(&lfsck->li_link);
1524 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1527 struct lfsck_instance *lfsck;
1529 spin_lock(&lfsck_instance_lock);
1530 lfsck = __lfsck_instance_find(key, ref, unlink);
1531 spin_unlock(&lfsck_instance_lock);
1536 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1538 struct lfsck_instance *tmp;
1540 spin_lock(&lfsck_instance_lock);
1541 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1542 if (lfsck->li_bottom == tmp->li_bottom) {
1543 spin_unlock(&lfsck_instance_lock);
1548 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1549 spin_unlock(&lfsck_instance_lock);
1553 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1558 bool newline = (bits != 0 ? false : true);
1560 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1562 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1565 if (names[i] != NULL) {
1569 seq_printf(m, "%s%c", names[i],
1570 newline ? '\n' : ',');
1576 seq_printf(m, "\n");
1580 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix)
1583 seq_printf(m, "%s: "LPU64" seconds\n", prefix,
1584 cfs_time_current_sec() - time);
1586 seq_printf(m, "%s: N/A\n", prefix);
1590 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1593 if (fid_is_zero(&pos->lp_dir_parent)) {
1594 if (pos->lp_oit_cookie == 0)
1595 seq_printf(m, "%s: N/A, N/A, N/A\n",
1598 seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1599 prefix, pos->lp_oit_cookie);
1601 seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1602 prefix, pos->lp_oit_cookie,
1603 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1608 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1609 struct lfsck_position *pos, bool init)
1611 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1613 if (unlikely(lfsck->li_di_oit == NULL)) {
1614 memset(pos, 0, sizeof(*pos));
1618 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1619 if (!lfsck->li_current_oit_processed && !init)
1620 pos->lp_oit_cookie--;
1622 LASSERT(pos->lp_oit_cookie > 0);
1624 if (lfsck->li_di_dir != NULL) {
1625 struct dt_object *dto = lfsck->li_obj_dir;
1627 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1630 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1631 fid_zero(&pos->lp_dir_parent);
1632 pos->lp_dir_cookie = 0;
1634 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1637 fid_zero(&pos->lp_dir_parent);
1638 pos->lp_dir_cookie = 0;
1642 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1646 if (limit != LFSCK_SPEED_NO_LIMIT) {
1648 lfsck->li_sleep_rate = limit / HZ;
1649 lfsck->li_sleep_jif = 1;
1651 lfsck->li_sleep_rate = 1;
1652 lfsck->li_sleep_jif = HZ / limit;
1655 lfsck->li_sleep_jif = 0;
1656 lfsck->li_sleep_rate = 0;
1659 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1660 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1667 void lfsck_control_speed(struct lfsck_instance *lfsck)
1669 struct ptlrpc_thread *thread = &lfsck->li_thread;
1670 struct l_wait_info lwi;
1672 if (lfsck->li_sleep_jif > 0 &&
1673 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1674 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1675 LWI_ON_SIGNAL_NOOP, NULL);
1677 l_wait_event(thread->t_ctl_waitq,
1678 !thread_is_running(thread),
1680 lfsck->li_new_scanned = 0;
1684 void lfsck_control_speed_by_self(struct lfsck_component *com)
1686 struct lfsck_instance *lfsck = com->lc_lfsck;
1687 struct ptlrpc_thread *thread = &lfsck->li_thread;
1688 struct l_wait_info lwi;
1690 if (lfsck->li_sleep_jif > 0 &&
1691 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1692 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1693 LWI_ON_SIGNAL_NOOP, NULL);
1695 l_wait_event(thread->t_ctl_waitq,
1696 !thread_is_running(thread),
1698 com->lc_new_scanned = 0;
1702 static struct lfsck_thread_args *
1703 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1704 struct lfsck_component *com,
1705 struct lfsck_start_param *lsp)
1707 struct lfsck_thread_args *lta;
1712 return ERR_PTR(-ENOMEM);
1714 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1720 lta->lta_lfsck = lfsck_instance_get(lfsck);
1722 lta->lta_com = lfsck_component_get(com);
1729 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1731 if (lta->lta_com != NULL)
1732 lfsck_component_put(<a->lta_env, lta->lta_com);
1733 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1734 lu_env_fini(<a->lta_env);
1738 struct lfsck_assistant_data *
1739 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1742 struct lfsck_assistant_data *lad;
1746 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1747 if (lad->lad_bitmap == NULL) {
1752 INIT_LIST_HEAD(&lad->lad_req_list);
1753 spin_lock_init(&lad->lad_lock);
1754 INIT_LIST_HEAD(&lad->lad_ost_list);
1755 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1756 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1757 INIT_LIST_HEAD(&lad->lad_mdt_list);
1758 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1759 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1760 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1762 lad->lad_name = name;
1769 * Generic LFSCK asynchronous communication interpretor function.
1770 * The LFSCK RPC reply for both the event notification and status
1771 * querying will be handled here.
1773 * \param[in] env pointer to the thread context
1774 * \param[in] req pointer to the LFSCK request
1775 * \param[in] args pointer to the lfsck_async_interpret_args
1776 * \param[in] rc the result for handling the LFSCK request
1778 * \retval 0 for success
1779 * \retval negative error number on failure
1781 int lfsck_async_interpret_common(const struct lu_env *env,
1782 struct ptlrpc_request *req,
1785 struct lfsck_async_interpret_args *laia = args;
1786 struct lfsck_component *com = laia->laia_com;
1787 struct lfsck_assistant_data *lad = com->lc_data;
1788 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1789 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1790 struct lfsck_request *lr = laia->laia_lr;
1792 LASSERT(com->lc_lfsck->li_master);
1794 switch (lr->lr_event) {
1797 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1799 lfsck_lfsck2name(com->lc_lfsck),
1800 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1801 ltd->ltd_index, lad->lad_name, rc);
1803 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1804 struct lfsck_layout *lo = com->lc_file_ram;
1806 if (lr->lr_flags & LEF_TO_OST)
1807 lfsck_lad_set_bitmap(env, com,
1810 lo->ll_flags |= LF_INCOMPLETE;
1812 struct lfsck_namespace *ns = com->lc_file_ram;
1814 /* If some MDT does not join the namespace
1815 * LFSCK, then we cannot know whether there
1816 * is some name entry on such MDT that with
1817 * the referenced MDT-object on this MDT or
1818 * not. So the namespace LFSCK on this MDT
1819 * cannot handle orphan MDT-objects properly.
1820 * So we mark the LFSCK as LF_INCOMPLETE and
1821 * skip orphan MDT-objects handling. */
1822 ns->ln_flags |= LF_INCOMPLETE;
1827 spin_lock(<ds->ltd_lock);
1828 if (ltd->ltd_dead) {
1829 spin_unlock(<ds->ltd_lock);
1833 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1834 struct list_head *list;
1835 struct list_head *phase_list;
1837 if (ltd->ltd_layout_done) {
1838 spin_unlock(<ds->ltd_lock);
1842 if (lr->lr_flags & LEF_TO_OST) {
1843 list = &lad->lad_ost_list;
1844 phase_list = &lad->lad_ost_phase1_list;
1846 list = &lad->lad_mdt_list;
1847 phase_list = &lad->lad_mdt_phase1_list;
1850 if (list_empty(<d->ltd_layout_list))
1851 list_add_tail(<d->ltd_layout_list, list);
1852 if (list_empty(<d->ltd_layout_phase_list))
1853 list_add_tail(<d->ltd_layout_phase_list,
1856 if (ltd->ltd_namespace_done) {
1857 spin_unlock(<ds->ltd_lock);
1861 if (list_empty(<d->ltd_namespace_list))
1862 list_add_tail(<d->ltd_namespace_list,
1863 &lad->lad_mdt_list);
1864 if (list_empty(<d->ltd_namespace_phase_list))
1865 list_add_tail(<d->ltd_namespace_phase_list,
1866 &lad->lad_mdt_phase1_list);
1868 spin_unlock(<ds->ltd_lock);
1871 case LE_PHASE1_DONE:
1872 case LE_PHASE2_DONE:
1874 if (rc != 0 && rc != -EALREADY)
1875 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
1876 "event = %d, rc = %d\n",
1877 lfsck_lfsck2name(com->lc_lfsck),
1878 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1879 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
1882 struct lfsck_reply *reply;
1883 struct list_head *list;
1884 struct list_head *phase_list;
1886 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1887 list = <d->ltd_layout_list;
1888 phase_list = <d->ltd_layout_phase_list;
1890 list = <d->ltd_namespace_list;
1891 phase_list = <d->ltd_namespace_phase_list;
1895 spin_lock(<ds->ltd_lock);
1896 list_del_init(phase_list);
1897 list_del_init(list);
1898 spin_unlock(<ds->ltd_lock);
1902 reply = req_capsule_server_get(&req->rq_pill,
1904 if (reply == NULL) {
1906 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
1907 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1909 spin_lock(<ds->ltd_lock);
1910 list_del_init(phase_list);
1911 list_del_init(list);
1912 spin_unlock(<ds->ltd_lock);
1916 switch (reply->lr_status) {
1917 case LS_SCANNING_PHASE1:
1919 case LS_SCANNING_PHASE2:
1920 spin_lock(<ds->ltd_lock);
1921 list_del_init(phase_list);
1922 if (ltd->ltd_dead) {
1923 spin_unlock(<ds->ltd_lock);
1927 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1928 if (ltd->ltd_layout_done) {
1929 spin_unlock(<ds->ltd_lock);
1933 if (lr->lr_flags & LEF_TO_OST)
1934 list_add_tail(phase_list,
1935 &lad->lad_ost_phase2_list);
1937 list_add_tail(phase_list,
1938 &lad->lad_mdt_phase2_list);
1940 if (ltd->ltd_namespace_done) {
1941 spin_unlock(<ds->ltd_lock);
1945 list_add_tail(phase_list,
1946 &lad->lad_mdt_phase2_list);
1948 spin_unlock(<ds->ltd_lock);
1951 spin_lock(<ds->ltd_lock);
1952 list_del_init(phase_list);
1953 list_del_init(list);
1954 spin_unlock(<ds->ltd_lock);
1960 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
1961 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
1965 if (!laia->laia_shared) {
1967 lfsck_component_put(env, com);
1973 static void lfsck_interpret(const struct lu_env *env,
1974 struct lfsck_instance *lfsck,
1975 struct ptlrpc_request *req, void *args, int result)
1977 struct lfsck_async_interpret_args *laia = args;
1978 struct lfsck_component *com;
1980 LASSERT(laia->laia_com == NULL);
1981 LASSERT(laia->laia_shared);
1983 spin_lock(&lfsck->li_lock);
1984 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1985 laia->laia_com = com;
1986 lfsck_async_interpret_common(env, req, laia, result);
1989 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
1990 laia->laia_com = com;
1991 lfsck_async_interpret_common(env, req, laia, result);
1993 spin_unlock(&lfsck->li_lock);
1996 static int lfsck_stop_notify(const struct lu_env *env,
1997 struct lfsck_instance *lfsck,
1998 struct lfsck_tgt_descs *ltds,
1999 struct lfsck_tgt_desc *ltd, __u16 type)
2001 struct lfsck_component *com;
2005 LASSERT(lfsck->li_master);
2007 spin_lock(&lfsck->li_lock);
2008 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2010 com = __lfsck_component_find(lfsck, type,
2011 &lfsck->li_list_double_scan);
2013 lfsck_component_get(com);
2014 spin_unlock(&lfsck->li_lock);
2017 struct lfsck_thread_info *info = lfsck_env_info(env);
2018 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2019 struct lfsck_request *lr = &info->lti_lr;
2020 struct lfsck_assistant_data *lad = com->lc_data;
2021 struct list_head *list;
2022 struct list_head *phase_list;
2023 struct ptlrpc_request_set *set;
2025 set = ptlrpc_prep_set();
2027 lfsck_component_put(env, com);
2032 if (type == LFSCK_TYPE_LAYOUT) {
2033 list = <d->ltd_layout_list;
2034 phase_list = <d->ltd_layout_phase_list;
2036 list = <d->ltd_namespace_list;
2037 phase_list = <d->ltd_namespace_phase_list;
2040 spin_lock(<ds->ltd_lock);
2041 if (list_empty(list)) {
2042 LASSERT(list_empty(phase_list));
2043 spin_unlock(<ds->ltd_lock);
2044 ptlrpc_set_destroy(set);
2049 list_del_init(phase_list);
2050 list_del_init(list);
2051 spin_unlock(<ds->ltd_lock);
2053 memset(lr, 0, sizeof(*lr));
2054 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2055 lr->lr_event = LE_PEER_EXIT;
2056 lr->lr_active = type;
2057 lr->lr_status = LS_CO_PAUSED;
2058 if (ltds == &lfsck->li_ost_descs)
2059 lr->lr_flags = LEF_TO_OST;
2061 laia->laia_com = com;
2062 laia->laia_ltds = ltds;
2063 atomic_inc(<d->ltd_ref);
2064 laia->laia_ltd = ltd;
2066 laia->laia_shared = 0;
2068 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2069 lfsck_async_interpret_common,
2070 laia, LFSCK_NOTIFY);
2072 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2073 "co-stop for %s: rc = %d\n",
2074 lfsck_lfsck2name(lfsck),
2075 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2076 ltd->ltd_index, lad->lad_name, rc);
2079 rc = ptlrpc_set_wait(set);
2082 ptlrpc_set_destroy(set);
2083 lfsck_component_put(env, com);
2089 static int lfsck_async_interpret(const struct lu_env *env,
2090 struct ptlrpc_request *req,
2093 struct lfsck_async_interpret_args *laia = args;
2094 struct lfsck_instance *lfsck;
2096 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2098 lfsck_interpret(env, lfsck, req, laia, rc);
2099 lfsck_tgt_put(laia->laia_ltd);
2100 if (rc != 0 && laia->laia_result != -EALREADY)
2101 laia->laia_result = rc;
2106 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2107 struct lfsck_request *lr,
2108 struct ptlrpc_request_set *set,
2109 ptlrpc_interpterer_t interpreter,
2110 void *args, int request)
2112 struct lfsck_async_interpret_args *laia;
2113 struct ptlrpc_request *req;
2114 struct lfsck_request *tmp;
2115 struct req_format *format;
2120 format = &RQF_LFSCK_NOTIFY;
2123 format = &RQF_LFSCK_QUERY;
2126 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2127 exp->exp_obd->obd_name, request, -EINVAL);
2131 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2135 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2137 ptlrpc_request_free(req);
2142 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2144 ptlrpc_request_set_replen(req);
2146 laia = ptlrpc_req_async_args(req);
2147 *laia = *(struct lfsck_async_interpret_args *)args;
2148 if (laia->laia_com != NULL)
2149 lfsck_component_get(laia->laia_com);
2150 req->rq_interpret_reply = interpreter;
2151 ptlrpc_set_add_req(set, req);
2156 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2157 struct lfsck_start_param *lsp)
2159 struct lfsck_instance *lfsck = com->lc_lfsck;
2160 struct lfsck_assistant_data *lad = com->lc_data;
2161 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2162 struct ptlrpc_thread *athread = &lad->lad_thread;
2163 struct lfsck_thread_args *lta;
2164 struct task_struct *task;
2168 lad->lad_assistant_status = 0;
2169 lad->lad_post_result = 0;
2170 lad->lad_to_post = 0;
2171 lad->lad_to_double_scan = 0;
2172 lad->lad_in_double_scan = 0;
2174 thread_set_flags(athread, 0);
2176 lta = lfsck_thread_args_init(lfsck, com, lsp);
2178 RETURN(PTR_ERR(lta));
2180 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2183 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2184 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2185 lfsck_thread_args_fini(lta);
2187 struct l_wait_info lwi = { 0 };
2189 l_wait_event(mthread->t_ctl_waitq,
2190 thread_is_running(athread) ||
2191 thread_is_stopped(athread),
2193 if (unlikely(!thread_is_running(athread)))
2194 rc = lad->lad_assistant_status;
2202 int lfsck_checkpoint_generic(const struct lu_env *env,
2203 struct lfsck_component *com)
2205 struct lfsck_assistant_data *lad = com->lc_data;
2206 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2207 struct ptlrpc_thread *athread = &lad->lad_thread;
2208 struct l_wait_info lwi = { 0 };
2210 if (com->lc_new_checked == 0)
2211 return LFSCK_CHECKPOINT_SKIP;
2213 l_wait_event(mthread->t_ctl_waitq,
2214 list_empty(&lad->lad_req_list) ||
2215 !thread_is_running(mthread) ||
2216 thread_is_stopped(athread),
2219 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2220 return LFSCK_CHECKPOINT_SKIP;
2225 void lfsck_post_generic(const struct lu_env *env,
2226 struct lfsck_component *com, int *result)
2228 struct lfsck_assistant_data *lad = com->lc_data;
2229 struct ptlrpc_thread *athread = &lad->lad_thread;
2230 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2231 struct l_wait_info lwi = { 0 };
2233 lad->lad_post_result = *result;
2236 lad->lad_to_post = 1;
2238 wake_up_all(&athread->t_ctl_waitq);
2239 l_wait_event(mthread->t_ctl_waitq,
2240 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2241 thread_is_stopped(athread),
2244 if (lad->lad_assistant_status < 0)
2245 *result = lad->lad_assistant_status;
2248 int lfsck_double_scan_generic(const struct lu_env *env,
2249 struct lfsck_component *com, int status)
2251 struct lfsck_assistant_data *lad = com->lc_data;
2252 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2253 struct ptlrpc_thread *athread = &lad->lad_thread;
2254 struct l_wait_info lwi = { 0 };
2256 if (status != LS_SCANNING_PHASE2)
2259 lad->lad_to_double_scan = 1;
2261 wake_up_all(&athread->t_ctl_waitq);
2262 l_wait_event(mthread->t_ctl_waitq,
2263 lad->lad_in_double_scan ||
2264 thread_is_stopped(athread),
2267 if (lad->lad_assistant_status < 0)
2268 return lad->lad_assistant_status;
2273 void lfsck_quit_generic(const struct lu_env *env,
2274 struct lfsck_component *com)
2276 struct lfsck_assistant_data *lad = com->lc_data;
2277 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2278 struct ptlrpc_thread *athread = &lad->lad_thread;
2279 struct l_wait_info lwi = { 0 };
2282 wake_up_all(&athread->t_ctl_waitq);
2283 l_wait_event(mthread->t_ctl_waitq,
2284 thread_is_init(athread) ||
2285 thread_is_stopped(athread),
2289 /* external interfaces */
2291 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2294 struct lfsck_instance *lfsck;
2298 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2302 lfsck = lfsck_instance_find(key, true, false);
2303 if (likely(lfsck != NULL)) {
2304 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2305 lfsck_instance_put(&env, lfsck);
2314 EXPORT_SYMBOL(lfsck_get_speed);
2316 int lfsck_set_speed(struct dt_device *key, int val)
2319 struct lfsck_instance *lfsck;
2323 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2327 lfsck = lfsck_instance_find(key, true, false);
2328 if (likely(lfsck != NULL)) {
2329 mutex_lock(&lfsck->li_mutex);
2330 if (__lfsck_set_speed(lfsck, val))
2331 rc = lfsck_bookmark_store(&env, lfsck);
2332 mutex_unlock(&lfsck->li_mutex);
2333 lfsck_instance_put(&env, lfsck);
2342 EXPORT_SYMBOL(lfsck_set_speed);
2344 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2347 struct lfsck_instance *lfsck;
2351 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2355 lfsck = lfsck_instance_find(key, true, false);
2356 if (likely(lfsck != NULL)) {
2357 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2358 lfsck_instance_put(&env, lfsck);
2367 EXPORT_SYMBOL(lfsck_get_windows);
2369 int lfsck_set_windows(struct dt_device *key, int val)
2372 struct lfsck_instance *lfsck;
2376 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2380 lfsck = lfsck_instance_find(key, true, false);
2381 if (likely(lfsck != NULL)) {
2382 if (val > LFSCK_ASYNC_WIN_MAX) {
2383 CWARN("%s: Too large async window size, which "
2384 "may cause memory issues. The valid range "
2385 "is [0 - %u]. If you do not want to restrict "
2386 "the window size for async requests pipeline, "
2387 "just set it as 0.\n",
2388 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2390 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2391 mutex_lock(&lfsck->li_mutex);
2392 lfsck->li_bookmark_ram.lb_async_windows = val;
2393 rc = lfsck_bookmark_store(&env, lfsck);
2394 mutex_unlock(&lfsck->li_mutex);
2396 lfsck_instance_put(&env, lfsck);
2405 EXPORT_SYMBOL(lfsck_set_windows);
2407 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2410 struct lfsck_instance *lfsck;
2411 struct lfsck_component *com;
2415 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2419 lfsck = lfsck_instance_find(key, true, false);
2420 if (likely(lfsck != NULL)) {
2421 com = lfsck_component_find(lfsck, type);
2422 if (likely(com != NULL)) {
2423 rc = com->lc_ops->lfsck_dump(&env, com, m);
2424 lfsck_component_put(&env, com);
2429 lfsck_instance_put(&env, lfsck);
2438 EXPORT_SYMBOL(lfsck_dump);
2440 static int lfsck_stop_all(const struct lu_env *env,
2441 struct lfsck_instance *lfsck,
2442 struct lfsck_stop *stop)
2444 struct lfsck_thread_info *info = lfsck_env_info(env);
2445 struct lfsck_request *lr = &info->lti_lr;
2446 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2447 struct ptlrpc_request_set *set;
2448 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2449 struct lfsck_tgt_desc *ltd;
2450 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2456 LASSERT(stop->ls_flags & LPF_BROADCAST);
2458 set = ptlrpc_prep_set();
2459 if (unlikely(set == NULL))
2462 memset(lr, 0, sizeof(*lr));
2463 lr->lr_event = LE_STOP;
2464 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2465 lr->lr_status = stop->ls_status;
2466 lr->lr_version = bk->lb_version;
2467 lr->lr_active = LFSCK_TYPES_ALL;
2468 lr->lr_param = stop->ls_flags;
2470 laia->laia_com = NULL;
2471 laia->laia_ltds = ltds;
2473 laia->laia_result = 0;
2474 laia->laia_shared = 1;
2476 down_read(<ds->ltd_rw_sem);
2477 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2478 ltd = lfsck_tgt_get(ltds, idx);
2479 LASSERT(ltd != NULL);
2481 laia->laia_ltd = ltd;
2482 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2483 lfsck_async_interpret, laia,
2486 lfsck_interpret(env, lfsck, NULL, laia, rc);
2488 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2489 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2493 up_read(<ds->ltd_rw_sem);
2495 rc = ptlrpc_set_wait(set);
2496 ptlrpc_set_destroy(set);
2499 rc = laia->laia_result;
2501 if (rc == -EALREADY)
2505 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2506 lfsck_lfsck2name(lfsck), rc);
2508 RETURN(rc != 0 ? rc : rc1);
2511 static int lfsck_start_all(const struct lu_env *env,
2512 struct lfsck_instance *lfsck,
2513 struct lfsck_start *start)
2515 struct lfsck_thread_info *info = lfsck_env_info(env);
2516 struct lfsck_request *lr = &info->lti_lr;
2517 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2518 struct ptlrpc_request_set *set;
2519 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2520 struct lfsck_tgt_desc *ltd;
2521 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2526 LASSERT(start->ls_flags & LPF_BROADCAST);
2528 set = ptlrpc_prep_set();
2529 if (unlikely(set == NULL))
2532 memset(lr, 0, sizeof(*lr));
2533 lr->lr_event = LE_START;
2534 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2535 lr->lr_speed = bk->lb_speed_limit;
2536 lr->lr_version = bk->lb_version;
2537 lr->lr_active = start->ls_active;
2538 lr->lr_param = start->ls_flags;
2539 lr->lr_async_windows = bk->lb_async_windows;
2540 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2543 laia->laia_com = NULL;
2544 laia->laia_ltds = ltds;
2546 laia->laia_result = 0;
2547 laia->laia_shared = 1;
2549 down_read(<ds->ltd_rw_sem);
2550 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2551 ltd = lfsck_tgt_get(ltds, idx);
2552 LASSERT(ltd != NULL);
2554 laia->laia_ltd = ltd;
2555 ltd->ltd_layout_done = 0;
2556 ltd->ltd_namespace_done = 0;
2557 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2558 lfsck_async_interpret, laia,
2561 lfsck_interpret(env, lfsck, NULL, laia, rc);
2563 CERROR("%s: cannot notify MDT %x for LFSCK "
2564 "start, failout: rc = %d\n",
2565 lfsck_lfsck2name(lfsck), idx, rc);
2569 up_read(<ds->ltd_rw_sem);
2572 ptlrpc_set_destroy(set);
2577 rc = ptlrpc_set_wait(set);
2578 ptlrpc_set_destroy(set);
2581 rc = laia->laia_result;
2584 struct lfsck_stop *stop = &info->lti_stop;
2586 CERROR("%s: cannot start LFSCK on some MDTs, "
2587 "stop all: rc = %d\n",
2588 lfsck_lfsck2name(lfsck), rc);
2589 if (rc != -EALREADY) {
2590 stop->ls_status = LS_FAILED;
2591 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2592 lfsck_stop_all(env, lfsck, stop);
2599 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2600 struct lfsck_start_param *lsp)
2602 struct lfsck_start *start = lsp->lsp_start;
2603 struct lfsck_instance *lfsck;
2604 struct lfsck_bookmark *bk;
2605 struct ptlrpc_thread *thread;
2606 struct lfsck_component *com;
2607 struct l_wait_info lwi = { 0 };
2608 struct lfsck_thread_args *lta;
2609 struct task_struct *task;
2616 lfsck = lfsck_instance_find(key, true, false);
2617 if (unlikely(lfsck == NULL))
2620 /* System is not ready, try again later. */
2621 if (unlikely(lfsck->li_namespace == NULL))
2622 GOTO(put, rc = -EAGAIN);
2624 /* start == NULL means auto trigger paused LFSCK. */
2625 if ((start == NULL) &&
2626 (list_empty(&lfsck->li_list_scan) ||
2627 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2630 bk = &lfsck->li_bookmark_ram;
2631 thread = &lfsck->li_thread;
2632 mutex_lock(&lfsck->li_mutex);
2633 spin_lock(&lfsck->li_lock);
2634 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2636 if (unlikely(start == NULL)) {
2637 spin_unlock(&lfsck->li_lock);
2641 while (start->ls_active != 0) {
2642 if (!(type & start->ls_active)) {
2647 com = __lfsck_component_find(lfsck, type,
2648 &lfsck->li_list_scan);
2650 com = __lfsck_component_find(lfsck, type,
2651 &lfsck->li_list_double_scan);
2657 if (com->lc_ops->lfsck_join != NULL) {
2658 rc = com->lc_ops->lfsck_join( env, com, lsp);
2659 if (rc != 0 && rc != -EALREADY)
2662 start->ls_active &= ~type;
2665 spin_unlock(&lfsck->li_lock);
2668 spin_unlock(&lfsck->li_lock);
2670 lfsck->li_status = 0;
2671 lfsck->li_oit_over = 0;
2672 lfsck->li_start_unplug = 0;
2673 lfsck->li_drop_dryrun = 0;
2674 lfsck->li_new_scanned = 0;
2676 /* For auto trigger. */
2680 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2681 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2682 lfsck_lfsck2name(lfsck));
2684 GOTO(out, rc = -EPERM);
2687 start->ls_version = bk->lb_version;
2689 if (start->ls_active != 0) {
2690 struct lfsck_component *next;
2692 if (start->ls_active == LFSCK_TYPES_ALL)
2693 start->ls_active = LFSCK_TYPES_SUPPORTED;
2695 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2696 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2697 GOTO(out, rc = -ENOTSUPP);
2700 list_for_each_entry_safe(com, next,
2701 &lfsck->li_list_scan, lc_link) {
2702 if (!(com->lc_type & start->ls_active)) {
2703 rc = com->lc_ops->lfsck_post(env, com, 0,
2710 while (start->ls_active != 0) {
2711 if (type & start->ls_active) {
2712 com = __lfsck_component_find(lfsck, type,
2713 &lfsck->li_list_idle);
2715 /* The component status will be updated
2716 * when its prep() is called later by
2717 * the LFSCK main engine. */
2718 list_move_tail(&com->lc_link,
2719 &lfsck->li_list_scan);
2720 start->ls_active &= ~type;
2726 if (list_empty(&lfsck->li_list_scan)) {
2727 /* The speed limit will be used to control both the LFSCK and
2728 * low layer scrub (if applied), need to be handled firstly. */
2729 if (start->ls_valid & LSV_SPEED_LIMIT) {
2730 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2731 rc = lfsck_bookmark_store(env, lfsck);
2740 if (start->ls_flags & LPF_RESET)
2741 flags |= DOIF_RESET;
2743 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2747 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2748 start->ls_active |= com->lc_type;
2749 if (flags & DOIF_RESET) {
2750 rc = com->lc_ops->lfsck_reset(env, com, false);
2757 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2758 if (bk->lb_param & LPF_DRYRUN)
2759 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2761 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2762 valid |= DOIV_ERROR_HANDLE;
2763 if (start->ls_flags & LPF_FAILOUT)
2764 flags |= DOIF_FAILOUT;
2767 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2768 valid |= DOIV_DRYRUN;
2769 if (start->ls_flags & LPF_DRYRUN)
2770 flags |= DOIF_DRYRUN;
2773 if (!list_empty(&lfsck->li_list_scan))
2774 flags |= DOIF_OUTUSED;
2776 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2777 thread_set_flags(thread, 0);
2778 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2780 GOTO(out, rc = PTR_ERR(lta));
2782 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2783 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2786 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2787 lfsck_lfsck2name(lfsck), rc);
2788 lfsck_thread_args_fini(lta);
2793 l_wait_event(thread->t_ctl_waitq,
2794 thread_is_running(thread) ||
2795 thread_is_stopped(thread),
2797 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2798 lfsck->li_start_unplug = 1;
2799 wake_up_all(&thread->t_ctl_waitq);
2804 /* release lfsck::li_mutex to avoid deadlock. */
2805 mutex_unlock(&lfsck->li_mutex);
2806 rc = lfsck_start_all(env, lfsck, start);
2808 spin_lock(&lfsck->li_lock);
2809 if (thread_is_stopped(thread)) {
2810 spin_unlock(&lfsck->li_lock);
2812 lfsck->li_status = LS_FAILED;
2813 lfsck->li_flags = 0;
2814 thread_set_flags(thread, SVC_STOPPING);
2815 spin_unlock(&lfsck->li_lock);
2817 lfsck->li_start_unplug = 1;
2818 wake_up_all(&thread->t_ctl_waitq);
2819 l_wait_event(thread->t_ctl_waitq,
2820 thread_is_stopped(thread),
2824 lfsck->li_start_unplug = 1;
2825 wake_up_all(&thread->t_ctl_waitq);
2831 mutex_unlock(&lfsck->li_mutex);
2834 lfsck_instance_put(env, lfsck);
2836 return rc < 0 ? rc : 0;
2838 EXPORT_SYMBOL(lfsck_start);
2840 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
2841 struct lfsck_stop *stop)
2843 struct lfsck_instance *lfsck;
2844 struct ptlrpc_thread *thread;
2845 struct l_wait_info lwi = { 0 };
2850 lfsck = lfsck_instance_find(key, true, false);
2851 if (unlikely(lfsck == NULL))
2854 thread = &lfsck->li_thread;
2855 /* release lfsck::li_mutex to avoid deadlock. */
2856 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
2857 if (!lfsck->li_master) {
2858 CERROR("%s: only allow to specify '-A' via MDS\n",
2859 lfsck_lfsck2name(lfsck));
2861 GOTO(out, rc = -EPERM);
2864 rc1 = lfsck_stop_all(env, lfsck, stop);
2867 mutex_lock(&lfsck->li_mutex);
2868 spin_lock(&lfsck->li_lock);
2869 /* no error if LFSCK is already stopped, or was never started */
2870 if (thread_is_init(thread) || thread_is_stopped(thread)) {
2871 spin_unlock(&lfsck->li_lock);
2876 lfsck->li_status = stop->ls_status;
2877 lfsck->li_flags = stop->ls_flags;
2879 lfsck->li_status = LS_STOPPED;
2880 lfsck->li_flags = 0;
2883 thread_set_flags(thread, SVC_STOPPING);
2884 spin_unlock(&lfsck->li_lock);
2886 wake_up_all(&thread->t_ctl_waitq);
2887 l_wait_event(thread->t_ctl_waitq,
2888 thread_is_stopped(thread),
2894 mutex_unlock(&lfsck->li_mutex);
2895 lfsck_instance_put(env, lfsck);
2897 return rc != 0 ? rc : rc1;
2899 EXPORT_SYMBOL(lfsck_stop);
2901 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
2902 struct lfsck_request *lr, struct thandle *th)
2904 int rc = -EOPNOTSUPP;
2907 switch (lr->lr_event) {
2909 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
2910 struct lfsck_start_param lsp;
2912 memset(start, 0, sizeof(*start));
2913 start->ls_valid = lr->lr_valid;
2914 start->ls_speed_limit = lr->lr_speed;
2915 start->ls_version = lr->lr_version;
2916 start->ls_active = lr->lr_active;
2917 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2918 start->ls_async_windows = lr->lr_async_windows;
2920 lsp.lsp_start = start;
2921 lsp.lsp_index = lr->lr_index;
2922 lsp.lsp_index_valid = 1;
2923 rc = lfsck_start(env, key, &lsp);
2927 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2929 memset(stop, 0, sizeof(*stop));
2930 stop->ls_status = lr->lr_status;
2931 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2932 rc = lfsck_stop(env, key, stop);
2935 case LE_PHASE1_DONE:
2936 case LE_PHASE2_DONE:
2937 case LE_FID_ACCESSED:
2939 case LE_CONDITIONAL_DESTROY:
2940 case LE_CREATE_ORPHAN:
2941 case LE_SKIP_NLINK_DECLARE:
2943 case LE_SET_LMV_MASTER:
2944 case LE_PAIRS_VERIFY: {
2945 struct lfsck_instance *lfsck;
2946 struct lfsck_component *com;
2948 lfsck = lfsck_instance_find(key, true, false);
2949 if (unlikely(lfsck == NULL))
2952 com = lfsck_component_find(lfsck, lr->lr_active);
2953 if (likely(com != NULL)) {
2954 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
2955 lfsck_component_put(env, com);
2958 lfsck_instance_put(env, lfsck);
2967 EXPORT_SYMBOL(lfsck_in_notify);
2969 int lfsck_query(const struct lu_env *env, struct dt_device *key,
2970 struct lfsck_request *lr)
2972 struct lfsck_instance *lfsck;
2973 struct lfsck_component *com;
2977 lfsck = lfsck_instance_find(key, true, false);
2978 if (unlikely(lfsck == NULL))
2981 com = lfsck_component_find(lfsck, lr->lr_active);
2982 if (likely(com != NULL)) {
2983 rc = com->lc_ops->lfsck_query(env, com);
2984 lfsck_component_put(env, com);
2989 lfsck_instance_put(env, lfsck);
2993 EXPORT_SYMBOL(lfsck_query);
2995 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
2996 struct ldlm_namespace *ns)
2998 struct lfsck_instance *lfsck;
3001 lfsck = lfsck_instance_find(key, true, false);
3002 if (likely(lfsck != NULL)) {
3003 lfsck->li_namespace = ns;
3004 lfsck_instance_put(env, lfsck);
3010 EXPORT_SYMBOL(lfsck_register_namespace);
3012 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3013 struct dt_device *next, struct obd_device *obd,
3014 lfsck_out_notify notify, void *notify_data, bool master)
3016 struct lfsck_instance *lfsck;
3017 struct dt_object *root = NULL;
3018 struct dt_object *obj = NULL;
3019 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3023 lfsck = lfsck_instance_find(key, false, false);
3024 if (unlikely(lfsck != NULL))
3027 OBD_ALLOC_PTR(lfsck);
3031 mutex_init(&lfsck->li_mutex);
3032 spin_lock_init(&lfsck->li_lock);
3033 INIT_LIST_HEAD(&lfsck->li_link);
3034 INIT_LIST_HEAD(&lfsck->li_list_scan);
3035 INIT_LIST_HEAD(&lfsck->li_list_dir);
3036 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3037 INIT_LIST_HEAD(&lfsck->li_list_idle);
3038 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3039 atomic_set(&lfsck->li_ref, 1);
3040 atomic_set(&lfsck->li_double_scan_count, 0);
3041 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3042 lfsck->li_out_notify = notify;
3043 lfsck->li_out_notify_data = notify_data;
3044 lfsck->li_next = next;
3045 lfsck->li_bottom = key;
3046 lfsck->li_obd = obd;
3048 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3052 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3056 fid->f_seq = FID_SEQ_LOCAL_NAME;
3059 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3063 rc = dt_root_get(env, key, fid);
3067 root = dt_locate(env, key, fid);
3069 GOTO(out, rc = PTR_ERR(root));
3071 if (unlikely(!dt_try_as_dir(env, root)))
3072 GOTO(out, rc = -ENOTDIR);
3074 lfsck->li_local_root_fid = *fid;
3076 lfsck->li_master = 1;
3077 if (lfsck_dev_idx(key) == 0) {
3078 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3079 const struct lu_name *cname;
3081 rc = dt_lookup(env, root,
3082 (struct dt_rec *)(&lfsck->li_global_root_fid),
3083 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3087 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3089 GOTO(out, rc = PTR_ERR(obj));
3091 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3092 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3096 lu_object_put(env, &obj->do_lu);
3097 obj = dt_locate(env, key, fid);
3099 GOTO(out, rc = PTR_ERR(obj));
3101 cname = lfsck_name_get_const(env, dotlustre,
3103 rc = lfsck_verify_linkea(env, key, obj, cname,
3104 &lfsck->li_global_root_fid);
3109 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3110 (const struct dt_key *)lostfound,
3115 lu_object_put(env, &obj->do_lu);
3116 obj = dt_locate(env, key, fid);
3118 GOTO(out, rc = PTR_ERR(obj));
3120 cname = lfsck_name_get_const(env, lostfound,
3122 rc = lfsck_verify_linkea(env, key, obj, cname, pfid);
3126 lu_object_put(env, &obj->do_lu);
3131 fid->f_seq = FID_SEQ_LOCAL_FILE;
3132 fid->f_oid = OTABLE_IT_OID;
3134 obj = dt_locate(env, key, fid);
3136 GOTO(out, rc = PTR_ERR(obj));
3138 lu_object_get(&obj->do_lu);
3139 lfsck->li_obj_oit = obj;
3140 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3144 rc = lfsck_bookmark_setup(env, lfsck);
3149 rc = lfsck_fid_init(lfsck);
3153 rc = lfsck_namespace_setup(env, lfsck);
3158 rc = lfsck_layout_setup(env, lfsck);
3162 /* XXX: more LFSCK components initialization to be added here. */
3164 rc = lfsck_instance_add(lfsck);
3166 rc = lfsck_add_target_from_orphan(env, lfsck);
3168 if (obj != NULL && !IS_ERR(obj))
3169 lu_object_put(env, &obj->do_lu);
3170 if (root != NULL && !IS_ERR(root))
3171 lu_object_put(env, &root->do_lu);
3173 lfsck_instance_cleanup(env, lfsck);
3176 EXPORT_SYMBOL(lfsck_register);
3178 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3180 struct lfsck_instance *lfsck;
3182 lfsck = lfsck_instance_find(key, false, true);
3184 lfsck_instance_put(env, lfsck);
3186 EXPORT_SYMBOL(lfsck_degister);
3188 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3189 struct dt_device *tgt, struct obd_export *exp,
3190 __u32 index, bool for_ost)
3192 struct lfsck_instance *lfsck;
3193 struct lfsck_tgt_desc *ltd;
3204 INIT_LIST_HEAD(<d->ltd_orphan_list);
3205 INIT_LIST_HEAD(<d->ltd_layout_list);
3206 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3207 INIT_LIST_HEAD(<d->ltd_namespace_list);
3208 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3209 atomic_set(<d->ltd_ref, 1);
3210 ltd->ltd_index = index;
3212 spin_lock(&lfsck_instance_lock);
3213 lfsck = __lfsck_instance_find(key, true, false);
3214 if (lfsck == NULL) {
3216 list_add_tail(<d->ltd_orphan_list,
3217 &lfsck_ost_orphan_list);
3219 list_add_tail(<d->ltd_orphan_list,
3220 &lfsck_mdt_orphan_list);
3221 spin_unlock(&lfsck_instance_lock);
3225 spin_unlock(&lfsck_instance_lock);
3227 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3231 lfsck_instance_put(env, lfsck);
3235 EXPORT_SYMBOL(lfsck_add_target);
3237 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3238 struct dt_device *tgt, __u32 index, bool for_ost)
3240 struct lfsck_instance *lfsck;
3241 struct lfsck_tgt_descs *ltds;
3242 struct lfsck_tgt_desc *ltd;
3243 struct list_head *head;
3246 head = &lfsck_ost_orphan_list;
3248 head = &lfsck_mdt_orphan_list;
3250 spin_lock(&lfsck_instance_lock);
3251 list_for_each_entry(ltd, head, ltd_orphan_list) {
3252 if (ltd->ltd_tgt == tgt) {
3253 list_del_init(<d->ltd_orphan_list);
3254 spin_unlock(&lfsck_instance_lock);
3262 lfsck = __lfsck_instance_find(key, true, false);
3263 spin_unlock(&lfsck_instance_lock);
3264 if (unlikely(lfsck == NULL))
3268 ltds = &lfsck->li_ost_descs;
3270 ltds = &lfsck->li_mdt_descs;
3272 down_write(<ds->ltd_rw_sem);
3273 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3275 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3278 ltd = LTD_TGT(ltds, index);
3279 if (unlikely(ltd == NULL))
3282 LASSERT(ltds->ltd_tgtnr > 0);
3285 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3286 LTD_TGT(ltds, index) = NULL;
3291 head = &lfsck->li_ost_descs.ltd_orphan;
3293 head = &lfsck->li_mdt_descs.ltd_orphan;
3295 list_for_each_entry(ltd, head, ltd_orphan_list) {
3296 if (ltd->ltd_tgt == tgt) {
3297 list_del_init(<d->ltd_orphan_list);
3303 up_write(<ds->ltd_rw_sem);
3305 spin_lock(<ds->ltd_lock);
3307 spin_unlock(<ds->ltd_lock);
3308 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3309 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3313 lfsck_instance_put(env, lfsck);
3315 EXPORT_SYMBOL(lfsck_del_target);
3317 static int __init lfsck_init(void)
3321 INIT_LIST_HEAD(&lfsck_instance_list);
3322 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3323 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3324 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3325 rc = lu_context_key_register(&lfsck_thread_key);
3327 tgt_register_lfsck_in_notify(lfsck_in_notify);
3328 tgt_register_lfsck_query(lfsck_query);
3334 static void __exit lfsck_exit(void)
3336 struct lfsck_tgt_desc *ltd;
3337 struct lfsck_tgt_desc *next;
3339 LASSERT(list_empty(&lfsck_instance_list));
3341 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3343 list_del_init(<d->ltd_orphan_list);
3347 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3349 list_del_init(<d->ltd_orphan_list);
3353 lu_context_key_degister(&lfsck_thread_key);
3356 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3357 MODULE_DESCRIPTION("LFSCK");
3358 MODULE_LICENSE("GPL");
3360 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);