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) {
1647 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1648 lfsck->li_sleep_rate = jiffies_to_msecs(limit) /
1650 lfsck->li_sleep_jif = 1;
1652 lfsck->li_sleep_rate = 1;
1653 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC /
1657 lfsck->li_sleep_jif = 0;
1658 lfsck->li_sleep_rate = 0;
1661 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1662 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1669 void lfsck_control_speed(struct lfsck_instance *lfsck)
1671 struct ptlrpc_thread *thread = &lfsck->li_thread;
1672 struct l_wait_info lwi;
1674 if (lfsck->li_sleep_jif > 0 &&
1675 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1676 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1677 LWI_ON_SIGNAL_NOOP, NULL);
1679 l_wait_event(thread->t_ctl_waitq,
1680 !thread_is_running(thread),
1682 lfsck->li_new_scanned = 0;
1686 void lfsck_control_speed_by_self(struct lfsck_component *com)
1688 struct lfsck_instance *lfsck = com->lc_lfsck;
1689 struct ptlrpc_thread *thread = &lfsck->li_thread;
1690 struct l_wait_info lwi;
1692 if (lfsck->li_sleep_jif > 0 &&
1693 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1694 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1695 LWI_ON_SIGNAL_NOOP, NULL);
1697 l_wait_event(thread->t_ctl_waitq,
1698 !thread_is_running(thread),
1700 com->lc_new_scanned = 0;
1704 static struct lfsck_thread_args *
1705 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1706 struct lfsck_component *com,
1707 struct lfsck_start_param *lsp)
1709 struct lfsck_thread_args *lta;
1714 return ERR_PTR(-ENOMEM);
1716 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1722 lta->lta_lfsck = lfsck_instance_get(lfsck);
1724 lta->lta_com = lfsck_component_get(com);
1731 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1733 if (lta->lta_com != NULL)
1734 lfsck_component_put(<a->lta_env, lta->lta_com);
1735 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1736 lu_env_fini(<a->lta_env);
1740 struct lfsck_assistant_data *
1741 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1744 struct lfsck_assistant_data *lad;
1748 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1749 if (lad->lad_bitmap == NULL) {
1754 INIT_LIST_HEAD(&lad->lad_req_list);
1755 spin_lock_init(&lad->lad_lock);
1756 INIT_LIST_HEAD(&lad->lad_ost_list);
1757 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1758 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1759 INIT_LIST_HEAD(&lad->lad_mdt_list);
1760 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1761 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1762 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1764 lad->lad_name = name;
1771 * Generic LFSCK asynchronous communication interpretor function.
1772 * The LFSCK RPC reply for both the event notification and status
1773 * querying will be handled here.
1775 * \param[in] env pointer to the thread context
1776 * \param[in] req pointer to the LFSCK request
1777 * \param[in] args pointer to the lfsck_async_interpret_args
1778 * \param[in] rc the result for handling the LFSCK request
1780 * \retval 0 for success
1781 * \retval negative error number on failure
1783 int lfsck_async_interpret_common(const struct lu_env *env,
1784 struct ptlrpc_request *req,
1787 struct lfsck_async_interpret_args *laia = args;
1788 struct lfsck_component *com = laia->laia_com;
1789 struct lfsck_assistant_data *lad = com->lc_data;
1790 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
1791 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
1792 struct lfsck_request *lr = laia->laia_lr;
1794 LASSERT(com->lc_lfsck->li_master);
1796 switch (lr->lr_event) {
1799 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
1801 lfsck_lfsck2name(com->lc_lfsck),
1802 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1803 ltd->ltd_index, lad->lad_name, rc);
1805 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1806 struct lfsck_layout *lo = com->lc_file_ram;
1808 if (lr->lr_flags & LEF_TO_OST)
1809 lfsck_lad_set_bitmap(env, com,
1812 lo->ll_flags |= LF_INCOMPLETE;
1814 struct lfsck_namespace *ns = com->lc_file_ram;
1816 /* If some MDT does not join the namespace
1817 * LFSCK, then we cannot know whether there
1818 * is some name entry on such MDT that with
1819 * the referenced MDT-object on this MDT or
1820 * not. So the namespace LFSCK on this MDT
1821 * cannot handle orphan MDT-objects properly.
1822 * So we mark the LFSCK as LF_INCOMPLETE and
1823 * skip orphan MDT-objects handling. */
1824 ns->ln_flags |= LF_INCOMPLETE;
1829 spin_lock(<ds->ltd_lock);
1830 if (ltd->ltd_dead) {
1831 spin_unlock(<ds->ltd_lock);
1835 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1836 struct list_head *list;
1837 struct list_head *phase_list;
1839 if (ltd->ltd_layout_done) {
1840 spin_unlock(<ds->ltd_lock);
1844 if (lr->lr_flags & LEF_TO_OST) {
1845 list = &lad->lad_ost_list;
1846 phase_list = &lad->lad_ost_phase1_list;
1848 list = &lad->lad_mdt_list;
1849 phase_list = &lad->lad_mdt_phase1_list;
1852 if (list_empty(<d->ltd_layout_list))
1853 list_add_tail(<d->ltd_layout_list, list);
1854 if (list_empty(<d->ltd_layout_phase_list))
1855 list_add_tail(<d->ltd_layout_phase_list,
1858 if (ltd->ltd_namespace_done) {
1859 spin_unlock(<ds->ltd_lock);
1863 if (list_empty(<d->ltd_namespace_list))
1864 list_add_tail(<d->ltd_namespace_list,
1865 &lad->lad_mdt_list);
1866 if (list_empty(<d->ltd_namespace_phase_list))
1867 list_add_tail(<d->ltd_namespace_phase_list,
1868 &lad->lad_mdt_phase1_list);
1870 spin_unlock(<ds->ltd_lock);
1873 case LE_PHASE1_DONE:
1874 case LE_PHASE2_DONE:
1876 if (rc != 0 && rc != -EALREADY)
1877 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
1878 "event = %d, rc = %d\n",
1879 lfsck_lfsck2name(com->lc_lfsck),
1880 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
1881 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
1884 struct lfsck_reply *reply;
1885 struct list_head *list;
1886 struct list_head *phase_list;
1888 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1889 list = <d->ltd_layout_list;
1890 phase_list = <d->ltd_layout_phase_list;
1892 list = <d->ltd_namespace_list;
1893 phase_list = <d->ltd_namespace_phase_list;
1897 spin_lock(<ds->ltd_lock);
1898 list_del_init(phase_list);
1899 list_del_init(list);
1900 spin_unlock(<ds->ltd_lock);
1904 reply = req_capsule_server_get(&req->rq_pill,
1906 if (reply == NULL) {
1908 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
1909 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
1911 spin_lock(<ds->ltd_lock);
1912 list_del_init(phase_list);
1913 list_del_init(list);
1914 spin_unlock(<ds->ltd_lock);
1918 switch (reply->lr_status) {
1919 case LS_SCANNING_PHASE1:
1921 case LS_SCANNING_PHASE2:
1922 spin_lock(<ds->ltd_lock);
1923 list_del_init(phase_list);
1924 if (ltd->ltd_dead) {
1925 spin_unlock(<ds->ltd_lock);
1929 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
1930 if (ltd->ltd_layout_done) {
1931 spin_unlock(<ds->ltd_lock);
1935 if (lr->lr_flags & LEF_TO_OST)
1936 list_add_tail(phase_list,
1937 &lad->lad_ost_phase2_list);
1939 list_add_tail(phase_list,
1940 &lad->lad_mdt_phase2_list);
1942 if (ltd->ltd_namespace_done) {
1943 spin_unlock(<ds->ltd_lock);
1947 list_add_tail(phase_list,
1948 &lad->lad_mdt_phase2_list);
1950 spin_unlock(<ds->ltd_lock);
1953 spin_lock(<ds->ltd_lock);
1954 list_del_init(phase_list);
1955 list_del_init(list);
1956 spin_unlock(<ds->ltd_lock);
1962 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
1963 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
1967 if (!laia->laia_shared) {
1969 lfsck_component_put(env, com);
1975 static void lfsck_interpret(const struct lu_env *env,
1976 struct lfsck_instance *lfsck,
1977 struct ptlrpc_request *req, void *args, int result)
1979 struct lfsck_async_interpret_args *laia = args;
1980 struct lfsck_component *com;
1982 LASSERT(laia->laia_com == NULL);
1983 LASSERT(laia->laia_shared);
1985 spin_lock(&lfsck->li_lock);
1986 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1987 laia->laia_com = com;
1988 lfsck_async_interpret_common(env, req, laia, result);
1991 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
1992 laia->laia_com = com;
1993 lfsck_async_interpret_common(env, req, laia, result);
1995 spin_unlock(&lfsck->li_lock);
1998 static int lfsck_stop_notify(const struct lu_env *env,
1999 struct lfsck_instance *lfsck,
2000 struct lfsck_tgt_descs *ltds,
2001 struct lfsck_tgt_desc *ltd, __u16 type)
2003 struct lfsck_component *com;
2007 LASSERT(lfsck->li_master);
2009 spin_lock(&lfsck->li_lock);
2010 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2012 com = __lfsck_component_find(lfsck, type,
2013 &lfsck->li_list_double_scan);
2015 lfsck_component_get(com);
2016 spin_unlock(&lfsck->li_lock);
2019 struct lfsck_thread_info *info = lfsck_env_info(env);
2020 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2021 struct lfsck_request *lr = &info->lti_lr;
2022 struct lfsck_assistant_data *lad = com->lc_data;
2023 struct list_head *list;
2024 struct list_head *phase_list;
2025 struct ptlrpc_request_set *set;
2027 set = ptlrpc_prep_set();
2029 lfsck_component_put(env, com);
2034 if (type == LFSCK_TYPE_LAYOUT) {
2035 list = <d->ltd_layout_list;
2036 phase_list = <d->ltd_layout_phase_list;
2038 list = <d->ltd_namespace_list;
2039 phase_list = <d->ltd_namespace_phase_list;
2042 spin_lock(<ds->ltd_lock);
2043 if (list_empty(list)) {
2044 LASSERT(list_empty(phase_list));
2045 spin_unlock(<ds->ltd_lock);
2046 ptlrpc_set_destroy(set);
2051 list_del_init(phase_list);
2052 list_del_init(list);
2053 spin_unlock(<ds->ltd_lock);
2055 memset(lr, 0, sizeof(*lr));
2056 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2057 lr->lr_event = LE_PEER_EXIT;
2058 lr->lr_active = type;
2059 lr->lr_status = LS_CO_PAUSED;
2060 if (ltds == &lfsck->li_ost_descs)
2061 lr->lr_flags = LEF_TO_OST;
2063 laia->laia_com = com;
2064 laia->laia_ltds = ltds;
2065 atomic_inc(<d->ltd_ref);
2066 laia->laia_ltd = ltd;
2068 laia->laia_shared = 0;
2070 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2071 lfsck_async_interpret_common,
2072 laia, LFSCK_NOTIFY);
2074 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2075 "co-stop for %s: rc = %d\n",
2076 lfsck_lfsck2name(lfsck),
2077 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2078 ltd->ltd_index, lad->lad_name, rc);
2081 rc = ptlrpc_set_wait(set);
2084 ptlrpc_set_destroy(set);
2085 lfsck_component_put(env, com);
2091 static int lfsck_async_interpret(const struct lu_env *env,
2092 struct ptlrpc_request *req,
2095 struct lfsck_async_interpret_args *laia = args;
2096 struct lfsck_instance *lfsck;
2098 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2100 lfsck_interpret(env, lfsck, req, laia, rc);
2101 lfsck_tgt_put(laia->laia_ltd);
2102 if (rc != 0 && laia->laia_result != -EALREADY)
2103 laia->laia_result = rc;
2108 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2109 struct lfsck_request *lr,
2110 struct ptlrpc_request_set *set,
2111 ptlrpc_interpterer_t interpreter,
2112 void *args, int request)
2114 struct lfsck_async_interpret_args *laia;
2115 struct ptlrpc_request *req;
2116 struct lfsck_request *tmp;
2117 struct req_format *format;
2122 format = &RQF_LFSCK_NOTIFY;
2125 format = &RQF_LFSCK_QUERY;
2128 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2129 exp->exp_obd->obd_name, request, -EINVAL);
2133 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2137 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2139 ptlrpc_request_free(req);
2144 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2146 ptlrpc_request_set_replen(req);
2148 laia = ptlrpc_req_async_args(req);
2149 *laia = *(struct lfsck_async_interpret_args *)args;
2150 if (laia->laia_com != NULL)
2151 lfsck_component_get(laia->laia_com);
2152 req->rq_interpret_reply = interpreter;
2153 ptlrpc_set_add_req(set, req);
2158 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2159 struct lfsck_start_param *lsp)
2161 struct lfsck_instance *lfsck = com->lc_lfsck;
2162 struct lfsck_assistant_data *lad = com->lc_data;
2163 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2164 struct ptlrpc_thread *athread = &lad->lad_thread;
2165 struct lfsck_thread_args *lta;
2166 struct task_struct *task;
2170 lad->lad_assistant_status = 0;
2171 lad->lad_post_result = 0;
2172 lad->lad_to_post = 0;
2173 lad->lad_to_double_scan = 0;
2174 lad->lad_in_double_scan = 0;
2176 thread_set_flags(athread, 0);
2178 lta = lfsck_thread_args_init(lfsck, com, lsp);
2180 RETURN(PTR_ERR(lta));
2182 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2185 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2186 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2187 lfsck_thread_args_fini(lta);
2189 struct l_wait_info lwi = { 0 };
2191 l_wait_event(mthread->t_ctl_waitq,
2192 thread_is_running(athread) ||
2193 thread_is_stopped(athread),
2195 if (unlikely(!thread_is_running(athread)))
2196 rc = lad->lad_assistant_status;
2204 int lfsck_checkpoint_generic(const struct lu_env *env,
2205 struct lfsck_component *com)
2207 struct lfsck_assistant_data *lad = com->lc_data;
2208 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2209 struct ptlrpc_thread *athread = &lad->lad_thread;
2210 struct l_wait_info lwi = { 0 };
2212 if (com->lc_new_checked == 0)
2213 return LFSCK_CHECKPOINT_SKIP;
2215 l_wait_event(mthread->t_ctl_waitq,
2216 list_empty(&lad->lad_req_list) ||
2217 !thread_is_running(mthread) ||
2218 thread_is_stopped(athread),
2221 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2222 return LFSCK_CHECKPOINT_SKIP;
2227 void lfsck_post_generic(const struct lu_env *env,
2228 struct lfsck_component *com, int *result)
2230 struct lfsck_assistant_data *lad = com->lc_data;
2231 struct ptlrpc_thread *athread = &lad->lad_thread;
2232 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2233 struct l_wait_info lwi = { 0 };
2235 lad->lad_post_result = *result;
2238 lad->lad_to_post = 1;
2240 wake_up_all(&athread->t_ctl_waitq);
2241 l_wait_event(mthread->t_ctl_waitq,
2242 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2243 thread_is_stopped(athread),
2246 if (lad->lad_assistant_status < 0)
2247 *result = lad->lad_assistant_status;
2250 int lfsck_double_scan_generic(const struct lu_env *env,
2251 struct lfsck_component *com, int status)
2253 struct lfsck_assistant_data *lad = com->lc_data;
2254 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2255 struct ptlrpc_thread *athread = &lad->lad_thread;
2256 struct l_wait_info lwi = { 0 };
2258 if (status != LS_SCANNING_PHASE2)
2261 lad->lad_to_double_scan = 1;
2263 wake_up_all(&athread->t_ctl_waitq);
2264 l_wait_event(mthread->t_ctl_waitq,
2265 lad->lad_in_double_scan ||
2266 thread_is_stopped(athread),
2269 if (lad->lad_assistant_status < 0)
2270 return lad->lad_assistant_status;
2275 void lfsck_quit_generic(const struct lu_env *env,
2276 struct lfsck_component *com)
2278 struct lfsck_assistant_data *lad = com->lc_data;
2279 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2280 struct ptlrpc_thread *athread = &lad->lad_thread;
2281 struct l_wait_info lwi = { 0 };
2284 wake_up_all(&athread->t_ctl_waitq);
2285 l_wait_event(mthread->t_ctl_waitq,
2286 thread_is_init(athread) ||
2287 thread_is_stopped(athread),
2291 /* external interfaces */
2293 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2296 struct lfsck_instance *lfsck;
2300 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2304 lfsck = lfsck_instance_find(key, true, false);
2305 if (likely(lfsck != NULL)) {
2306 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2307 lfsck_instance_put(&env, lfsck);
2316 EXPORT_SYMBOL(lfsck_get_speed);
2318 int lfsck_set_speed(struct dt_device *key, int val)
2321 struct lfsck_instance *lfsck;
2325 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2329 lfsck = lfsck_instance_find(key, true, false);
2330 if (likely(lfsck != NULL)) {
2331 mutex_lock(&lfsck->li_mutex);
2332 if (__lfsck_set_speed(lfsck, val))
2333 rc = lfsck_bookmark_store(&env, lfsck);
2334 mutex_unlock(&lfsck->li_mutex);
2335 lfsck_instance_put(&env, lfsck);
2344 EXPORT_SYMBOL(lfsck_set_speed);
2346 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2349 struct lfsck_instance *lfsck;
2353 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2357 lfsck = lfsck_instance_find(key, true, false);
2358 if (likely(lfsck != NULL)) {
2359 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2360 lfsck_instance_put(&env, lfsck);
2369 EXPORT_SYMBOL(lfsck_get_windows);
2371 int lfsck_set_windows(struct dt_device *key, int val)
2374 struct lfsck_instance *lfsck;
2378 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2382 lfsck = lfsck_instance_find(key, true, false);
2383 if (likely(lfsck != NULL)) {
2384 if (val > LFSCK_ASYNC_WIN_MAX) {
2385 CWARN("%s: Too large async window size, which "
2386 "may cause memory issues. The valid range "
2387 "is [0 - %u]. If you do not want to restrict "
2388 "the window size for async requests pipeline, "
2389 "just set it as 0.\n",
2390 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2392 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2393 mutex_lock(&lfsck->li_mutex);
2394 lfsck->li_bookmark_ram.lb_async_windows = val;
2395 rc = lfsck_bookmark_store(&env, lfsck);
2396 mutex_unlock(&lfsck->li_mutex);
2398 lfsck_instance_put(&env, lfsck);
2407 EXPORT_SYMBOL(lfsck_set_windows);
2409 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2412 struct lfsck_instance *lfsck;
2413 struct lfsck_component *com;
2417 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2421 lfsck = lfsck_instance_find(key, true, false);
2422 if (likely(lfsck != NULL)) {
2423 com = lfsck_component_find(lfsck, type);
2424 if (likely(com != NULL)) {
2425 rc = com->lc_ops->lfsck_dump(&env, com, m);
2426 lfsck_component_put(&env, com);
2431 lfsck_instance_put(&env, lfsck);
2440 EXPORT_SYMBOL(lfsck_dump);
2442 static int lfsck_stop_all(const struct lu_env *env,
2443 struct lfsck_instance *lfsck,
2444 struct lfsck_stop *stop)
2446 struct lfsck_thread_info *info = lfsck_env_info(env);
2447 struct lfsck_request *lr = &info->lti_lr;
2448 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2449 struct ptlrpc_request_set *set;
2450 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2451 struct lfsck_tgt_desc *ltd;
2452 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2458 LASSERT(stop->ls_flags & LPF_BROADCAST);
2460 set = ptlrpc_prep_set();
2461 if (unlikely(set == NULL))
2464 memset(lr, 0, sizeof(*lr));
2465 lr->lr_event = LE_STOP;
2466 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2467 lr->lr_status = stop->ls_status;
2468 lr->lr_version = bk->lb_version;
2469 lr->lr_active = LFSCK_TYPES_ALL;
2470 lr->lr_param = stop->ls_flags;
2472 laia->laia_com = NULL;
2473 laia->laia_ltds = ltds;
2475 laia->laia_result = 0;
2476 laia->laia_shared = 1;
2478 down_read(<ds->ltd_rw_sem);
2479 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2480 ltd = lfsck_tgt_get(ltds, idx);
2481 LASSERT(ltd != NULL);
2483 laia->laia_ltd = ltd;
2484 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2485 lfsck_async_interpret, laia,
2488 lfsck_interpret(env, lfsck, NULL, laia, rc);
2490 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2491 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2495 up_read(<ds->ltd_rw_sem);
2497 rc = ptlrpc_set_wait(set);
2498 ptlrpc_set_destroy(set);
2501 rc = laia->laia_result;
2503 if (rc == -EALREADY)
2507 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2508 lfsck_lfsck2name(lfsck), rc);
2510 RETURN(rc != 0 ? rc : rc1);
2513 static int lfsck_start_all(const struct lu_env *env,
2514 struct lfsck_instance *lfsck,
2515 struct lfsck_start *start)
2517 struct lfsck_thread_info *info = lfsck_env_info(env);
2518 struct lfsck_request *lr = &info->lti_lr;
2519 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2520 struct ptlrpc_request_set *set;
2521 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2522 struct lfsck_tgt_desc *ltd;
2523 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2528 LASSERT(start->ls_flags & LPF_BROADCAST);
2530 set = ptlrpc_prep_set();
2531 if (unlikely(set == NULL))
2534 memset(lr, 0, sizeof(*lr));
2535 lr->lr_event = LE_START;
2536 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2537 lr->lr_speed = bk->lb_speed_limit;
2538 lr->lr_version = bk->lb_version;
2539 lr->lr_active = start->ls_active;
2540 lr->lr_param = start->ls_flags;
2541 lr->lr_async_windows = bk->lb_async_windows;
2542 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2545 laia->laia_com = NULL;
2546 laia->laia_ltds = ltds;
2548 laia->laia_result = 0;
2549 laia->laia_shared = 1;
2551 down_read(<ds->ltd_rw_sem);
2552 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2553 ltd = lfsck_tgt_get(ltds, idx);
2554 LASSERT(ltd != NULL);
2556 laia->laia_ltd = ltd;
2557 ltd->ltd_layout_done = 0;
2558 ltd->ltd_namespace_done = 0;
2559 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2560 lfsck_async_interpret, laia,
2563 lfsck_interpret(env, lfsck, NULL, laia, rc);
2565 CERROR("%s: cannot notify MDT %x for LFSCK "
2566 "start, failout: rc = %d\n",
2567 lfsck_lfsck2name(lfsck), idx, rc);
2571 up_read(<ds->ltd_rw_sem);
2574 ptlrpc_set_destroy(set);
2579 rc = ptlrpc_set_wait(set);
2580 ptlrpc_set_destroy(set);
2583 rc = laia->laia_result;
2586 struct lfsck_stop *stop = &info->lti_stop;
2588 CERROR("%s: cannot start LFSCK on some MDTs, "
2589 "stop all: rc = %d\n",
2590 lfsck_lfsck2name(lfsck), rc);
2591 if (rc != -EALREADY) {
2592 stop->ls_status = LS_FAILED;
2593 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2594 lfsck_stop_all(env, lfsck, stop);
2601 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2602 struct lfsck_start_param *lsp)
2604 struct lfsck_start *start = lsp->lsp_start;
2605 struct lfsck_instance *lfsck;
2606 struct lfsck_bookmark *bk;
2607 struct ptlrpc_thread *thread;
2608 struct lfsck_component *com;
2609 struct l_wait_info lwi = { 0 };
2610 struct lfsck_thread_args *lta;
2611 struct task_struct *task;
2618 lfsck = lfsck_instance_find(key, true, false);
2619 if (unlikely(lfsck == NULL))
2622 /* System is not ready, try again later. */
2623 if (unlikely(lfsck->li_namespace == NULL))
2624 GOTO(put, rc = -EAGAIN);
2626 /* start == NULL means auto trigger paused LFSCK. */
2627 if ((start == NULL) &&
2628 (list_empty(&lfsck->li_list_scan) ||
2629 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2632 bk = &lfsck->li_bookmark_ram;
2633 thread = &lfsck->li_thread;
2634 mutex_lock(&lfsck->li_mutex);
2635 spin_lock(&lfsck->li_lock);
2636 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2638 if (unlikely(start == NULL)) {
2639 spin_unlock(&lfsck->li_lock);
2643 while (start->ls_active != 0) {
2644 if (!(type & start->ls_active)) {
2649 com = __lfsck_component_find(lfsck, type,
2650 &lfsck->li_list_scan);
2652 com = __lfsck_component_find(lfsck, type,
2653 &lfsck->li_list_double_scan);
2659 if (com->lc_ops->lfsck_join != NULL) {
2660 rc = com->lc_ops->lfsck_join( env, com, lsp);
2661 if (rc != 0 && rc != -EALREADY)
2664 start->ls_active &= ~type;
2667 spin_unlock(&lfsck->li_lock);
2670 spin_unlock(&lfsck->li_lock);
2672 lfsck->li_status = 0;
2673 lfsck->li_oit_over = 0;
2674 lfsck->li_start_unplug = 0;
2675 lfsck->li_drop_dryrun = 0;
2676 lfsck->li_new_scanned = 0;
2678 /* For auto trigger. */
2682 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2683 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2684 lfsck_lfsck2name(lfsck));
2686 GOTO(out, rc = -EPERM);
2689 start->ls_version = bk->lb_version;
2691 if (start->ls_active != 0) {
2692 struct lfsck_component *next;
2694 if (start->ls_active == LFSCK_TYPES_ALL)
2695 start->ls_active = LFSCK_TYPES_SUPPORTED;
2697 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2698 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2699 GOTO(out, rc = -ENOTSUPP);
2702 list_for_each_entry_safe(com, next,
2703 &lfsck->li_list_scan, lc_link) {
2704 if (!(com->lc_type & start->ls_active)) {
2705 rc = com->lc_ops->lfsck_post(env, com, 0,
2712 while (start->ls_active != 0) {
2713 if (type & start->ls_active) {
2714 com = __lfsck_component_find(lfsck, type,
2715 &lfsck->li_list_idle);
2717 /* The component status will be updated
2718 * when its prep() is called later by
2719 * the LFSCK main engine. */
2720 list_move_tail(&com->lc_link,
2721 &lfsck->li_list_scan);
2722 start->ls_active &= ~type;
2728 if (list_empty(&lfsck->li_list_scan)) {
2729 /* The speed limit will be used to control both the LFSCK and
2730 * low layer scrub (if applied), need to be handled firstly. */
2731 if (start->ls_valid & LSV_SPEED_LIMIT) {
2732 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2733 rc = lfsck_bookmark_store(env, lfsck);
2742 if (start->ls_flags & LPF_RESET)
2743 flags |= DOIF_RESET;
2745 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2749 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2750 start->ls_active |= com->lc_type;
2751 if (flags & DOIF_RESET) {
2752 rc = com->lc_ops->lfsck_reset(env, com, false);
2759 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
2760 if (bk->lb_param & LPF_DRYRUN)
2761 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2763 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2764 valid |= DOIV_ERROR_HANDLE;
2765 if (start->ls_flags & LPF_FAILOUT)
2766 flags |= DOIF_FAILOUT;
2769 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2770 valid |= DOIV_DRYRUN;
2771 if (start->ls_flags & LPF_DRYRUN)
2772 flags |= DOIF_DRYRUN;
2775 if (!list_empty(&lfsck->li_list_scan))
2776 flags |= DOIF_OUTUSED;
2778 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2779 thread_set_flags(thread, 0);
2780 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2782 GOTO(out, rc = PTR_ERR(lta));
2784 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2785 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2788 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2789 lfsck_lfsck2name(lfsck), rc);
2790 lfsck_thread_args_fini(lta);
2795 l_wait_event(thread->t_ctl_waitq,
2796 thread_is_running(thread) ||
2797 thread_is_stopped(thread),
2799 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2800 lfsck->li_start_unplug = 1;
2801 wake_up_all(&thread->t_ctl_waitq);
2806 /* release lfsck::li_mutex to avoid deadlock. */
2807 mutex_unlock(&lfsck->li_mutex);
2808 rc = lfsck_start_all(env, lfsck, start);
2810 spin_lock(&lfsck->li_lock);
2811 if (thread_is_stopped(thread)) {
2812 spin_unlock(&lfsck->li_lock);
2814 lfsck->li_status = LS_FAILED;
2815 lfsck->li_flags = 0;
2816 thread_set_flags(thread, SVC_STOPPING);
2817 spin_unlock(&lfsck->li_lock);
2819 lfsck->li_start_unplug = 1;
2820 wake_up_all(&thread->t_ctl_waitq);
2821 l_wait_event(thread->t_ctl_waitq,
2822 thread_is_stopped(thread),
2826 lfsck->li_start_unplug = 1;
2827 wake_up_all(&thread->t_ctl_waitq);
2833 mutex_unlock(&lfsck->li_mutex);
2836 lfsck_instance_put(env, lfsck);
2838 return rc < 0 ? rc : 0;
2840 EXPORT_SYMBOL(lfsck_start);
2842 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
2843 struct lfsck_stop *stop)
2845 struct lfsck_instance *lfsck;
2846 struct ptlrpc_thread *thread;
2847 struct l_wait_info lwi = { 0 };
2852 lfsck = lfsck_instance_find(key, true, false);
2853 if (unlikely(lfsck == NULL))
2856 thread = &lfsck->li_thread;
2857 /* release lfsck::li_mutex to avoid deadlock. */
2858 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
2859 if (!lfsck->li_master) {
2860 CERROR("%s: only allow to specify '-A' via MDS\n",
2861 lfsck_lfsck2name(lfsck));
2863 GOTO(out, rc = -EPERM);
2866 rc1 = lfsck_stop_all(env, lfsck, stop);
2869 mutex_lock(&lfsck->li_mutex);
2870 spin_lock(&lfsck->li_lock);
2871 /* no error if LFSCK is already stopped, or was never started */
2872 if (thread_is_init(thread) || thread_is_stopped(thread)) {
2873 spin_unlock(&lfsck->li_lock);
2878 lfsck->li_status = stop->ls_status;
2879 lfsck->li_flags = stop->ls_flags;
2881 lfsck->li_status = LS_STOPPED;
2882 lfsck->li_flags = 0;
2885 thread_set_flags(thread, SVC_STOPPING);
2886 spin_unlock(&lfsck->li_lock);
2888 wake_up_all(&thread->t_ctl_waitq);
2889 l_wait_event(thread->t_ctl_waitq,
2890 thread_is_stopped(thread),
2896 mutex_unlock(&lfsck->li_mutex);
2897 lfsck_instance_put(env, lfsck);
2899 return rc != 0 ? rc : rc1;
2901 EXPORT_SYMBOL(lfsck_stop);
2903 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
2904 struct lfsck_request *lr, struct thandle *th)
2906 int rc = -EOPNOTSUPP;
2909 switch (lr->lr_event) {
2911 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
2912 struct lfsck_start_param lsp;
2914 memset(start, 0, sizeof(*start));
2915 start->ls_valid = lr->lr_valid;
2916 start->ls_speed_limit = lr->lr_speed;
2917 start->ls_version = lr->lr_version;
2918 start->ls_active = lr->lr_active;
2919 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2920 start->ls_async_windows = lr->lr_async_windows;
2922 lsp.lsp_start = start;
2923 lsp.lsp_index = lr->lr_index;
2924 lsp.lsp_index_valid = 1;
2925 rc = lfsck_start(env, key, &lsp);
2929 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2931 memset(stop, 0, sizeof(*stop));
2932 stop->ls_status = lr->lr_status;
2933 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2934 rc = lfsck_stop(env, key, stop);
2937 case LE_PHASE1_DONE:
2938 case LE_PHASE2_DONE:
2939 case LE_FID_ACCESSED:
2941 case LE_CONDITIONAL_DESTROY:
2942 case LE_CREATE_ORPHAN:
2943 case LE_SKIP_NLINK_DECLARE:
2945 case LE_SET_LMV_MASTER:
2946 case LE_SET_LMV_SLAVE:
2947 case LE_PAIRS_VERIFY: {
2948 struct lfsck_instance *lfsck;
2949 struct lfsck_component *com;
2951 lfsck = lfsck_instance_find(key, true, false);
2952 if (unlikely(lfsck == NULL))
2955 com = lfsck_component_find(lfsck, lr->lr_active);
2956 if (likely(com != NULL)) {
2957 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
2958 lfsck_component_put(env, com);
2961 lfsck_instance_put(env, lfsck);
2970 EXPORT_SYMBOL(lfsck_in_notify);
2972 int lfsck_query(const struct lu_env *env, struct dt_device *key,
2973 struct lfsck_request *lr)
2975 struct lfsck_instance *lfsck;
2976 struct lfsck_component *com;
2980 lfsck = lfsck_instance_find(key, true, false);
2981 if (unlikely(lfsck == NULL))
2984 com = lfsck_component_find(lfsck, lr->lr_active);
2985 if (likely(com != NULL)) {
2986 rc = com->lc_ops->lfsck_query(env, com);
2987 lfsck_component_put(env, com);
2992 lfsck_instance_put(env, lfsck);
2996 EXPORT_SYMBOL(lfsck_query);
2998 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
2999 struct ldlm_namespace *ns)
3001 struct lfsck_instance *lfsck;
3004 lfsck = lfsck_instance_find(key, true, false);
3005 if (likely(lfsck != NULL)) {
3006 lfsck->li_namespace = ns;
3007 lfsck_instance_put(env, lfsck);
3013 EXPORT_SYMBOL(lfsck_register_namespace);
3015 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3016 struct dt_device *next, struct obd_device *obd,
3017 lfsck_out_notify notify, void *notify_data, bool master)
3019 struct lfsck_instance *lfsck;
3020 struct dt_object *root = NULL;
3021 struct dt_object *obj = NULL;
3022 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3026 lfsck = lfsck_instance_find(key, false, false);
3027 if (unlikely(lfsck != NULL))
3030 OBD_ALLOC_PTR(lfsck);
3034 mutex_init(&lfsck->li_mutex);
3035 spin_lock_init(&lfsck->li_lock);
3036 INIT_LIST_HEAD(&lfsck->li_link);
3037 INIT_LIST_HEAD(&lfsck->li_list_scan);
3038 INIT_LIST_HEAD(&lfsck->li_list_dir);
3039 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3040 INIT_LIST_HEAD(&lfsck->li_list_idle);
3041 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3042 atomic_set(&lfsck->li_ref, 1);
3043 atomic_set(&lfsck->li_double_scan_count, 0);
3044 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3045 lfsck->li_out_notify = notify;
3046 lfsck->li_out_notify_data = notify_data;
3047 lfsck->li_next = next;
3048 lfsck->li_bottom = key;
3049 lfsck->li_obd = obd;
3051 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3055 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3059 fid->f_seq = FID_SEQ_LOCAL_NAME;
3062 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3066 rc = dt_root_get(env, key, fid);
3070 root = dt_locate(env, key, fid);
3072 GOTO(out, rc = PTR_ERR(root));
3074 if (unlikely(!dt_try_as_dir(env, root)))
3075 GOTO(out, rc = -ENOTDIR);
3077 lfsck->li_local_root_fid = *fid;
3079 lfsck->li_master = 1;
3080 if (lfsck_dev_idx(key) == 0) {
3081 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3082 const struct lu_name *cname;
3084 rc = dt_lookup(env, root,
3085 (struct dt_rec *)(&lfsck->li_global_root_fid),
3086 (const struct dt_key *)"ROOT", BYPASS_CAPA);
3090 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3092 GOTO(out, rc = PTR_ERR(obj));
3094 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3095 (const struct dt_key *)dotlustre, BYPASS_CAPA);
3099 lu_object_put(env, &obj->do_lu);
3100 obj = dt_locate(env, key, fid);
3102 GOTO(out, rc = PTR_ERR(obj));
3104 cname = lfsck_name_get_const(env, dotlustre,
3106 rc = lfsck_verify_linkea(env, key, obj, cname,
3107 &lfsck->li_global_root_fid);
3112 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3113 (const struct dt_key *)lostfound,
3118 lu_object_put(env, &obj->do_lu);
3119 obj = dt_locate(env, key, fid);
3121 GOTO(out, rc = PTR_ERR(obj));
3123 cname = lfsck_name_get_const(env, lostfound,
3125 rc = lfsck_verify_linkea(env, key, obj, cname, pfid);
3129 lu_object_put(env, &obj->do_lu);
3134 fid->f_seq = FID_SEQ_LOCAL_FILE;
3135 fid->f_oid = OTABLE_IT_OID;
3137 obj = dt_locate(env, key, fid);
3139 GOTO(out, rc = PTR_ERR(obj));
3141 lu_object_get(&obj->do_lu);
3142 lfsck->li_obj_oit = obj;
3143 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3147 rc = lfsck_bookmark_setup(env, lfsck);
3152 rc = lfsck_fid_init(lfsck);
3156 rc = lfsck_namespace_setup(env, lfsck);
3161 rc = lfsck_layout_setup(env, lfsck);
3165 /* XXX: more LFSCK components initialization to be added here. */
3167 rc = lfsck_instance_add(lfsck);
3169 rc = lfsck_add_target_from_orphan(env, lfsck);
3171 if (obj != NULL && !IS_ERR(obj))
3172 lu_object_put(env, &obj->do_lu);
3173 if (root != NULL && !IS_ERR(root))
3174 lu_object_put(env, &root->do_lu);
3176 lfsck_instance_cleanup(env, lfsck);
3179 EXPORT_SYMBOL(lfsck_register);
3181 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3183 struct lfsck_instance *lfsck;
3185 lfsck = lfsck_instance_find(key, false, true);
3187 lfsck_instance_put(env, lfsck);
3189 EXPORT_SYMBOL(lfsck_degister);
3191 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3192 struct dt_device *tgt, struct obd_export *exp,
3193 __u32 index, bool for_ost)
3195 struct lfsck_instance *lfsck;
3196 struct lfsck_tgt_desc *ltd;
3207 INIT_LIST_HEAD(<d->ltd_orphan_list);
3208 INIT_LIST_HEAD(<d->ltd_layout_list);
3209 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3210 INIT_LIST_HEAD(<d->ltd_namespace_list);
3211 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3212 atomic_set(<d->ltd_ref, 1);
3213 ltd->ltd_index = index;
3215 spin_lock(&lfsck_instance_lock);
3216 lfsck = __lfsck_instance_find(key, true, false);
3217 if (lfsck == NULL) {
3219 list_add_tail(<d->ltd_orphan_list,
3220 &lfsck_ost_orphan_list);
3222 list_add_tail(<d->ltd_orphan_list,
3223 &lfsck_mdt_orphan_list);
3224 spin_unlock(&lfsck_instance_lock);
3228 spin_unlock(&lfsck_instance_lock);
3230 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3234 lfsck_instance_put(env, lfsck);
3238 EXPORT_SYMBOL(lfsck_add_target);
3240 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3241 struct dt_device *tgt, __u32 index, bool for_ost)
3243 struct lfsck_instance *lfsck;
3244 struct lfsck_tgt_descs *ltds;
3245 struct lfsck_tgt_desc *ltd;
3246 struct list_head *head;
3249 head = &lfsck_ost_orphan_list;
3251 head = &lfsck_mdt_orphan_list;
3253 spin_lock(&lfsck_instance_lock);
3254 list_for_each_entry(ltd, head, ltd_orphan_list) {
3255 if (ltd->ltd_tgt == tgt) {
3256 list_del_init(<d->ltd_orphan_list);
3257 spin_unlock(&lfsck_instance_lock);
3265 lfsck = __lfsck_instance_find(key, true, false);
3266 spin_unlock(&lfsck_instance_lock);
3267 if (unlikely(lfsck == NULL))
3271 ltds = &lfsck->li_ost_descs;
3273 ltds = &lfsck->li_mdt_descs;
3275 down_write(<ds->ltd_rw_sem);
3276 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3278 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3281 ltd = LTD_TGT(ltds, index);
3282 if (unlikely(ltd == NULL))
3285 LASSERT(ltds->ltd_tgtnr > 0);
3288 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3289 LTD_TGT(ltds, index) = NULL;
3294 head = &lfsck->li_ost_descs.ltd_orphan;
3296 head = &lfsck->li_mdt_descs.ltd_orphan;
3298 list_for_each_entry(ltd, head, ltd_orphan_list) {
3299 if (ltd->ltd_tgt == tgt) {
3300 list_del_init(<d->ltd_orphan_list);
3306 up_write(<ds->ltd_rw_sem);
3308 spin_lock(<ds->ltd_lock);
3310 spin_unlock(<ds->ltd_lock);
3311 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3312 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3316 lfsck_instance_put(env, lfsck);
3318 EXPORT_SYMBOL(lfsck_del_target);
3320 static int __init lfsck_init(void)
3324 INIT_LIST_HEAD(&lfsck_instance_list);
3325 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3326 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3327 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3328 rc = lu_context_key_register(&lfsck_thread_key);
3330 tgt_register_lfsck_in_notify(lfsck_in_notify);
3331 tgt_register_lfsck_query(lfsck_query);
3337 static void __exit lfsck_exit(void)
3339 struct lfsck_tgt_desc *ltd;
3340 struct lfsck_tgt_desc *next;
3342 LASSERT(list_empty(&lfsck_instance_list));
3344 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3346 list_del_init(<d->ltd_orphan_list);
3350 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3352 list_del_init(<d->ltd_orphan_list);
3356 lu_context_key_degister(&lfsck_thread_key);
3359 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
3360 MODULE_DESCRIPTION("LFSCK");
3361 MODULE_LICENSE("GPL");
3363 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);