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 thread key */
46 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
48 static void lfsck_key_fini(const struct lu_context *ctx,
49 struct lu_context_key *key, void *data)
51 struct lfsck_thread_info *info = data;
53 lu_buf_free(&info->lti_linkea_buf);
54 lu_buf_free(&info->lti_big_buf);
58 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
59 LU_KEY_INIT_GENERIC(lfsck);
61 static struct list_head lfsck_instance_list;
62 static struct list_head lfsck_ost_orphan_list;
63 static struct list_head lfsck_mdt_orphan_list;
64 static DEFINE_SPINLOCK(lfsck_instance_lock);
66 static const char *lfsck_status_names[] = {
68 [LS_SCANNING_PHASE1] = "scanning-phase1",
69 [LS_SCANNING_PHASE2] = "scanning-phase2",
70 [LS_COMPLETED] = "completed",
71 [LS_FAILED] = "failed",
72 [LS_STOPPED] = "stopped",
73 [LS_PAUSED] = "paused",
74 [LS_CRASHED] = "crashed",
75 [LS_PARTIAL] = "partial",
76 [LS_CO_FAILED] = "co-failed",
77 [LS_CO_STOPPED] = "co-stopped",
78 [LS_CO_PAUSED] = "co-paused"
81 const char *lfsck_flags_names[] = {
90 const char *lfsck_param_names[] = {
101 enum lfsck_verify_lpf_types {
102 LVLT_BY_BOOKMARK = 0,
103 LVLT_BY_NAMEENTRY = 1,
106 const char *lfsck_status2names(enum lfsck_status status)
108 if (unlikely(status < 0 || status >= LS_MAX))
111 return lfsck_status_names[status];
114 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
116 spin_lock_init(<ds->ltd_lock);
117 init_rwsem(<ds->ltd_rw_sem);
118 INIT_LIST_HEAD(<ds->ltd_orphan);
119 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
120 if (ltds->ltd_tgts_bitmap == NULL)
126 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
128 struct lfsck_tgt_desc *ltd;
129 struct lfsck_tgt_desc *next;
132 down_write(<ds->ltd_rw_sem);
134 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
136 list_del_init(<d->ltd_orphan_list);
140 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
141 up_write(<ds->ltd_rw_sem);
146 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
147 ltd = LTD_TGT(ltds, idx);
148 if (likely(ltd != NULL)) {
149 LASSERT(list_empty(<d->ltd_layout_list));
150 LASSERT(list_empty(<d->ltd_layout_phase_list));
153 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
154 LTD_TGT(ltds, idx) = NULL;
159 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
162 for (idx = 0; idx < TGT_PTRS; idx++) {
163 if (ltds->ltd_tgts_idx[idx] != NULL) {
164 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
165 ltds->ltd_tgts_idx[idx] = NULL;
169 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
170 ltds->ltd_tgts_bitmap = NULL;
171 up_write(<ds->ltd_rw_sem);
174 static int __lfsck_add_target(const struct lu_env *env,
175 struct lfsck_instance *lfsck,
176 struct lfsck_tgt_desc *ltd,
177 bool for_ost, bool locked)
179 struct lfsck_tgt_descs *ltds;
180 __u32 index = ltd->ltd_index;
185 ltds = &lfsck->li_ost_descs;
187 ltds = &lfsck->li_mdt_descs;
190 down_write(<ds->ltd_rw_sem);
192 LASSERT(ltds->ltd_tgts_bitmap != NULL);
194 if (index >= ltds->ltd_tgts_bitmap->size) {
195 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
196 (__u32)BITS_PER_LONG);
197 cfs_bitmap_t *old_bitmap = ltds->ltd_tgts_bitmap;
198 cfs_bitmap_t *new_bitmap;
200 while (newsize < index + 1)
203 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
204 if (new_bitmap == NULL)
205 GOTO(unlock, rc = -ENOMEM);
207 if (ltds->ltd_tgtnr > 0)
208 cfs_bitmap_copy(new_bitmap, old_bitmap);
209 ltds->ltd_tgts_bitmap = new_bitmap;
210 CFS_FREE_BITMAP(old_bitmap);
213 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
214 CERROR("%s: the device %s (%u) is registered already\n",
215 lfsck_lfsck2name(lfsck),
216 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
217 GOTO(unlock, rc = -EEXIST);
220 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
221 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
222 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
223 GOTO(unlock, rc = -ENOMEM);
226 LTD_TGT(ltds, index) = ltd;
227 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
230 GOTO(unlock, rc = 0);
234 up_write(<ds->ltd_rw_sem);
239 static int lfsck_add_target_from_orphan(const struct lu_env *env,
240 struct lfsck_instance *lfsck)
242 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
243 struct lfsck_tgt_desc *ltd;
244 struct lfsck_tgt_desc *next;
245 struct list_head *head = &lfsck_ost_orphan_list;
250 spin_lock(&lfsck_instance_lock);
251 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
252 if (ltd->ltd_key == lfsck->li_bottom)
253 list_move_tail(<d->ltd_orphan_list,
256 spin_unlock(&lfsck_instance_lock);
258 down_write(<ds->ltd_rw_sem);
259 while (!list_empty(<ds->ltd_orphan)) {
260 ltd = list_entry(ltds->ltd_orphan.next,
261 struct lfsck_tgt_desc,
263 list_del_init(<d->ltd_orphan_list);
264 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
265 /* Do not hold the semaphore for too long time. */
266 up_write(<ds->ltd_rw_sem);
270 down_write(<ds->ltd_rw_sem);
272 up_write(<ds->ltd_rw_sem);
275 ltds = &lfsck->li_mdt_descs;
276 head = &lfsck_mdt_orphan_list;
284 static inline struct lfsck_component *
285 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
286 struct list_head *list)
288 struct lfsck_component *com;
290 list_for_each_entry(com, list, lc_link) {
291 if (com->lc_type == type)
297 struct lfsck_component *
298 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
300 struct lfsck_component *com;
302 spin_lock(&lfsck->li_lock);
303 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
307 com = __lfsck_component_find(lfsck, type,
308 &lfsck->li_list_double_scan);
312 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
316 lfsck_component_get(com);
317 spin_unlock(&lfsck->li_lock);
321 void lfsck_component_cleanup(const struct lu_env *env,
322 struct lfsck_component *com)
324 if (!list_empty(&com->lc_link))
325 list_del_init(&com->lc_link);
326 if (!list_empty(&com->lc_link_dir))
327 list_del_init(&com->lc_link_dir);
329 lfsck_component_put(env, com);
332 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
333 struct lu_fid *fid, bool locked)
335 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
340 mutex_lock(&lfsck->li_mutex);
342 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
344 bk->lb_last_fid = *fid;
345 /* We do not care about whether the subsequent sub-operations
346 * failed or not. The worst case is that one FID is lost that
347 * is not a big issue for the LFSCK since it is relative rare
348 * for LFSCK create. */
349 rc = lfsck_bookmark_store(env, lfsck);
353 mutex_unlock(&lfsck->li_mutex);
359 * Request the specified ibits lock for the given object.
361 * Before the LFSCK modifying on the namespace visible object,
362 * it needs to acquire related ibits ldlm lock.
364 * \param[in] env pointer to the thread context
365 * \param[in] lfsck pointer to the lfsck instance
366 * \param[in] obj pointer to the dt_object to be locked
367 * \param[out] lh pointer to the lock handle
368 * \param[in] ibits the bits for the ldlm lock to be acquired
369 * \param[in] mode the mode for the ldlm lock to be acquired
371 * \retval 0 for success
372 * \retval negative error number on failure
374 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
375 struct dt_object *obj, struct lustre_handle *lh,
376 __u64 bits, ldlm_mode_t mode)
378 struct lfsck_thread_info *info = lfsck_env_info(env);
379 ldlm_policy_data_t *policy = &info->lti_policy;
380 struct ldlm_res_id *resid = &info->lti_resid;
381 __u64 flags = LDLM_FL_ATOMIC_CB;
384 LASSERT(lfsck->li_namespace != NULL);
386 memset(policy, 0, sizeof(*policy));
387 policy->l_inodebits.bits = bits;
388 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
389 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid, LDLM_IBITS,
390 policy, mode, &flags, ldlm_blocking_ast,
391 ldlm_completion_ast, NULL, NULL, 0,
392 LVB_T_NONE, NULL, lh);
393 if (rc == ELDLM_OK) {
396 memset(lh, 0, sizeof(*lh));
404 * Release the the specified ibits lock.
406 * If the lock has been acquired before, release it
407 * and cleanup the handle. Otherwise, do nothing.
409 * \param[in] lh pointer to the lock handle
410 * \param[in] mode the mode for the ldlm lock to be released
412 void lfsck_ibits_unlock(struct lustre_handle *lh, ldlm_mode_t mode)
414 if (lustre_handle_is_used(lh)) {
415 ldlm_lock_decref(lh, mode);
416 memset(lh, 0, sizeof(*lh));
420 static const char dot[] = ".";
421 static const char dotdot[] = "..";
422 static const char dotlustre[] = ".lustre";
423 static const char lostfound[] = "lost+found";
425 static int lfsck_create_lpf_local(const struct lu_env *env,
426 struct lfsck_instance *lfsck,
427 struct dt_object *parent,
428 struct dt_object *child,
430 struct dt_object_format *dof,
433 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
434 struct dt_device *dev = lfsck->li_bottom;
435 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
436 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
437 const struct lu_fid *cfid = lfsck_dto2fid(child);
438 struct thandle *th = NULL;
439 struct linkea_data ldata = { 0 };
440 struct lu_buf linkea_buf;
441 const struct lu_name *cname;
443 int len = sizeof(struct lfsck_bookmark);
447 rc = linkea_data_new(&ldata,
448 &lfsck_env_info(env)->lti_linkea_buf);
452 cname = lfsck_name_get_const(env, name, strlen(name));
453 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
457 th = dt_trans_create(env, dev);
461 /* 1a. create child */
462 rc = dt_declare_create(env, child, la, NULL, dof, th);
466 /* 2a. increase child nlink */
467 rc = dt_declare_ref_add(env, child, th);
471 /* 3a. insert linkEA for child */
472 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
473 ldata.ld_leh->leh_len);
474 rc = dt_declare_xattr_set(env, child, &linkea_buf,
475 XATTR_NAME_LINK, 0, th);
479 /* 4a. insert name into parent dir */
480 rec->rec_type = S_IFDIR;
482 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
483 (const struct dt_key *)name, th);
487 /* 5a. increase parent nlink */
488 rc = dt_declare_ref_add(env, parent, th);
492 /* 6a. update bookmark */
493 rc = dt_declare_record_write(env, bk_obj,
494 lfsck_buf_get(env, bk, len), 0, th);
498 rc = dt_trans_start_local(env, dev, th);
502 dt_write_lock(env, child, 0);
503 /* 1b.1. create child */
504 rc = dt_create(env, child, la, NULL, dof, th);
508 if (unlikely(!dt_try_as_dir(env, child)))
509 GOTO(unlock, rc = -ENOTDIR);
511 /* 1b.2. insert dot into child dir */
513 rc = dt_insert(env, child, (const struct dt_rec *)rec,
514 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
518 /* 1b.3. insert dotdot into child dir */
519 rec->rec_fid = &LU_LPF_FID;
520 rc = dt_insert(env, child, (const struct dt_rec *)rec,
521 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
525 /* 2b. increase child nlink */
526 rc = dt_ref_add(env, child, th);
530 /* 3b. insert linkEA for child. */
531 rc = dt_xattr_set(env, child, &linkea_buf,
532 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
533 dt_write_unlock(env, child);
537 /* 4b. insert name into parent dir */
539 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
540 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
544 dt_write_lock(env, parent, 0);
545 /* 5b. increase parent nlink */
546 rc = dt_ref_add(env, parent, th);
547 dt_write_unlock(env, parent);
551 bk->lb_lpf_fid = *cfid;
552 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
554 /* 6b. update bookmark */
555 rc = dt_record_write(env, bk_obj,
556 lfsck_buf_get(env, bk, len), &pos, th);
561 dt_write_unlock(env, child);
564 dt_trans_stop(env, dev, th);
569 static int lfsck_create_lpf_remote(const struct lu_env *env,
570 struct lfsck_instance *lfsck,
571 struct dt_object *parent,
572 struct dt_object *child,
574 struct dt_object_format *dof,
577 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
578 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
579 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
580 const struct lu_fid *cfid = lfsck_dto2fid(child);
581 struct thandle *th = NULL;
582 struct linkea_data ldata = { 0 };
583 struct lu_buf linkea_buf;
584 const struct lu_name *cname;
585 struct dt_device *dev;
587 int len = sizeof(struct lfsck_bookmark);
591 rc = linkea_data_new(&ldata,
592 &lfsck_env_info(env)->lti_linkea_buf);
596 cname = lfsck_name_get_const(env, name, strlen(name));
597 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
601 /* Create .lustre/lost+found/MDTxxxx. */
603 /* XXX: Currently, cross-MDT create operation needs to create the child
604 * object firstly, then insert name into the parent directory. For
605 * this case, the child object resides on current MDT (local), but
606 * the parent ".lustre/lost+found" may be on remote MDT. It is not
607 * easy to contain all the sub-modifications orderly within single
610 * To avoid more inconsistency, we split the create operation into
613 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
615 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
618 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
619 * repair such inconsistency when LFSCK run next time. */
621 /* Transaction I: locally */
623 dev = lfsck->li_bottom;
624 th = dt_trans_create(env, dev);
628 /* 1a. create child */
629 rc = dt_declare_create(env, child, la, NULL, dof, th);
633 /* 2a. increase child nlink */
634 rc = dt_declare_ref_add(env, child, th);
638 /* 3a. insert linkEA for child */
639 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
640 ldata.ld_leh->leh_len);
641 rc = dt_declare_xattr_set(env, child, &linkea_buf,
642 XATTR_NAME_LINK, 0, th);
646 /* 4a. update bookmark */
647 rc = dt_declare_record_write(env, bk_obj,
648 lfsck_buf_get(env, bk, len), 0, th);
652 rc = dt_trans_start_local(env, dev, th);
656 dt_write_lock(env, child, 0);
657 /* 1b.1. create child */
658 rc = dt_create(env, child, la, NULL, dof, th);
662 if (unlikely(!dt_try_as_dir(env, child)))
663 GOTO(unlock, rc = -ENOTDIR);
665 /* 1b.2. insert dot into child dir */
666 rec->rec_type = S_IFDIR;
668 rc = dt_insert(env, child, (const struct dt_rec *)rec,
669 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
673 /* 1b.3. insert dotdot into child dir */
674 rec->rec_fid = &LU_LPF_FID;
675 rc = dt_insert(env, child, (const struct dt_rec *)rec,
676 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
680 /* 2b. increase child nlink */
681 rc = dt_ref_add(env, child, th);
685 /* 3b. insert linkEA for child */
686 rc = dt_xattr_set(env, child, &linkea_buf,
687 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
691 bk->lb_lpf_fid = *cfid;
692 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
694 /* 4b. update bookmark */
695 rc = dt_record_write(env, bk_obj,
696 lfsck_buf_get(env, bk, len), &pos, th);
698 dt_write_unlock(env, child);
699 dt_trans_stop(env, dev, th);
703 /* Transaction II: remotely */
705 dev = lfsck->li_next;
706 th = dt_trans_create(env, dev);
710 /* 5a. insert name into parent dir */
712 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
713 (const struct dt_key *)name, th);
717 /* 6a. increase parent nlink */
718 rc = dt_declare_ref_add(env, parent, th);
722 rc = dt_trans_start(env, dev, th);
726 /* 5b. insert name into parent dir */
727 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
728 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
732 dt_write_lock(env, parent, 0);
733 /* 6b. increase parent nlink */
734 rc = dt_ref_add(env, parent, th);
735 dt_write_unlock(env, parent);
740 dt_write_unlock(env, child);
742 dt_trans_stop(env, dev, th);
744 if (rc != 0 && dev == lfsck->li_next)
745 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
746 "for orphans, but failed to insert the name %s "
747 "to the .lustre/lost+found/. Such inconsistency "
748 "will be repaired when LFSCK run next time: rc = %d\n",
749 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
754 /* Do NOT create .lustre/lost+found/MDTxxxx when register the lfsck instance,
755 * because the MDT0 maybe not reaady for sequence allocation yet. We do that
756 * only when it is required, such as orphan OST-objects repairing. */
757 int lfsck_create_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
759 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
760 struct lfsck_thread_info *info = lfsck_env_info(env);
761 struct lu_fid *cfid = &info->lti_fid2;
762 struct lu_attr *la = &info->lti_la;
763 struct dt_object_format *dof = &info->lti_dof;
764 struct dt_object *parent = NULL;
765 struct dt_object *child = NULL;
766 struct lustre_handle lh = { 0 };
768 int node = lfsck_dev_idx(lfsck->li_bottom);
772 LASSERT(lfsck->li_master);
774 sprintf(name, "MDT%04x", node);
776 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
779 struct lfsck_tgt_desc *ltd;
781 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
782 if (unlikely(ltd == NULL))
785 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
790 RETURN(PTR_ERR(parent));
792 if (lfsck->li_lpf_obj != NULL)
795 if (unlikely(!dt_try_as_dir(env, parent)))
796 GOTO(out, rc = -ENOTDIR);
798 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
799 MDS_INODELOCK_UPDATE, LCK_EX);
803 mutex_lock(&lfsck->li_mutex);
804 if (lfsck->li_lpf_obj != NULL)
805 GOTO(unlock, rc = 0);
807 if (fid_is_zero(&bk->lb_lpf_fid)) {
808 /* There is corner case that: in former LFSCK scanning we have
809 * created the .lustre/lost+found/MDTxxxx but failed to update
810 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
811 * it from MDT0 firstly. */
812 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
813 (const struct dt_key *)name, BYPASS_CAPA);
814 if (rc != 0 && rc != -ENOENT)
818 bk->lb_lpf_fid = *cfid;
819 rc = lfsck_bookmark_store(env, lfsck);
821 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
826 *cfid = bk->lb_lpf_fid;
829 child = lfsck_object_find_by_dev(env, lfsck->li_bottom, cfid);
831 GOTO(unlock, rc = PTR_ERR(child));
833 if (dt_object_exists(child) != 0) {
834 if (unlikely(!dt_try_as_dir(env, child)))
837 lfsck->li_lpf_obj = child;
842 memset(la, 0, sizeof(*la));
843 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
844 la->la_mode = S_IFDIR | S_IRWXU;
845 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
847 memset(dof, 0, sizeof(*dof));
848 dof->dof_type = dt_mode_to_dft(S_IFDIR);
851 rc = lfsck_create_lpf_local(env, lfsck, parent, child, la,
854 rc = lfsck_create_lpf_remote(env, lfsck, parent, child, la,
857 lfsck->li_lpf_obj = child;
862 mutex_unlock(&lfsck->li_mutex);
863 lfsck_ibits_unlock(&lh, LCK_EX);
864 if (rc != 0 && child != NULL && !IS_ERR(child))
865 lu_object_put(env, &child->do_lu);
867 if (parent != NULL && !IS_ERR(parent))
868 lu_object_put(env, &parent->do_lu);
874 * Scan .lustre/lost+found for bad name entries and remove them.
876 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
877 * index in the system. Any other formatted name is invalid and should be
880 * \param[in] env pointer to the thread context
881 * \param[in] lfsck pointer to the lfsck instance
882 * \param[in] parent pointer to the lost+found object
884 * \retval 0 for success
885 * \retval negative error number on failure
887 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
888 struct lfsck_instance *lfsck,
889 struct dt_object *parent)
891 struct lu_dirent *ent =
892 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
893 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
898 it = iops->init(env, parent, LUDA_64BITHASH, BYPASS_CAPA);
902 rc = iops->load(env, it, 0);
904 rc = iops->next(env, it);
911 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
915 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
916 if (ent->lde_name[0] == '.') {
917 if (ent->lde_namelen == 1)
920 if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
924 /* name length must be strlen("MDTxxxx") */
925 if (ent->lde_namelen != 7)
928 if (memcmp(ent->lde_name, "MDT", off) != 0)
931 while (off < 7 && isxdigit(ent->lde_name[off]))
937 rc = lfsck_remove_name_entry(env, lfsck, parent,
938 ent->lde_name, S_IFDIR);
944 rc = iops->next(env, it);
950 RETURN(rc > 0 ? 0 : rc);
953 static int lfsck_update_lpf_entry(const struct lu_env *env,
954 struct lfsck_instance *lfsck,
955 struct dt_object *parent,
956 struct dt_object *child,
958 enum lfsck_verify_lpf_types type)
962 if (type == LVLT_BY_BOOKMARK) {
963 rc = lfsck_update_name_entry(env, lfsck, parent, name,
964 lfsck_dto2fid(child), S_IFDIR);
965 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
966 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
967 rc = lfsck_bookmark_store(env, lfsck);
969 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
970 " in the bookmark file: rc = %d\n",
971 lfsck_lfsck2name(lfsck),
972 PFID(lfsck_dto2fid(child)), rc);
979 * Check whether the @child back references the @parent.
982 * 1) The child's FID is stored in the bookmark file. If the child back
983 * references the parent (LU_LPF_FID object) via its ".." entry, then
984 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
985 * the child back references another parent2, then:
986 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
987 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
988 * references the child. So keep them there. As the LFSCK processing,
989 * the parent3 may be found, then when the LFSCK run next time, the
990 * inconsistency can be repaired.
992 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
993 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
994 * via its ".." entry, then update the bookmark file, otherwise, if the child
995 * back references another parent2, then:
996 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
997 * from .lustre/lost+found/;
998 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
999 * sub-directory name entry and update the child;
1000 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1001 * or not, then keep them there.
1003 * \param[in] env pointer to the thread context
1004 * \param[in] lfsck pointer to the lfsck instance
1005 * \param[in] parent pointer to the lost+found object
1006 * \param[in] child pointer to the lost+found sub-directory object
1007 * \param[in] name the name for lost+found sub-directory object
1008 * \param[out] fid pointer to the buffer to hold the FID of the object
1009 * (called it as parent2) that is referenced via the
1010 * child's dotdot entry; it also can be the FID that
1011 * is referenced by the name entry under the parent2.
1012 * \param[in] type to indicate where the child's FID is stored in
1014 * \retval positive number for uncertain inconsistency
1015 * \retval 0 for success
1016 * \retval negative error number on failure
1018 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1019 struct lfsck_instance *lfsck,
1020 struct dt_object *parent,
1021 struct dt_object *child, const char *name,
1023 enum lfsck_verify_lpf_types type)
1025 struct lfsck_thread_info *info = lfsck_env_info(env);
1026 char *name2 = info->lti_key;
1027 struct lu_fid *fid2 = &info->lti_fid3;
1028 struct dt_object *parent2 = NULL;
1029 struct lustre_handle lh = { 0 };
1034 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1035 (const struct dt_key *)dotdot, BYPASS_CAPA);
1039 if (!fid_is_sane(fid))
1040 GOTO(linkea, rc = -EINVAL);
1042 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1043 const struct lu_name *cname;
1045 if (lfsck->li_lpf_obj == NULL) {
1046 lu_object_get(&child->do_lu);
1047 lfsck->li_lpf_obj = child;
1050 cname = lfsck_name_get_const(env, name, strlen(name));
1051 rc = lfsck_verify_linkea(env, lfsck->li_bottom, child, cname,
1054 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1060 parent2 = lfsck_object_find_by_dev(env, lfsck->li_next, fid);
1061 if (IS_ERR(parent2))
1062 GOTO(linkea, parent2);
1064 if (!dt_object_exists(parent2)) {
1065 lu_object_put(env, &parent2->do_lu);
1067 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1070 if (!dt_try_as_dir(env, parent2)) {
1071 lu_object_put(env, &parent2->do_lu);
1073 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1077 /* To prevent rename/unlink race */
1078 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1079 MDS_INODELOCK_UPDATE, LCK_PR);
1083 dt_read_lock(env, child, 0);
1084 rc = lfsck_links_get_first(env, child, name2, fid2);
1086 dt_read_unlock(env, child);
1087 lfsck_ibits_unlock(&lh, LCK_PR);
1089 GOTO(out_put, rc = 1);
1092 /* It is almost impossible that the bookmark file (or the name entry)
1093 * and the linkEA hit the same data corruption. Trust the linkEA. */
1094 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1095 dt_read_unlock(env, child);
1096 lfsck_ibits_unlock(&lh, LCK_PR);
1099 if (lfsck->li_lpf_obj == NULL) {
1100 lu_object_get(&child->do_lu);
1101 lfsck->li_lpf_obj = child;
1104 /* Update the child's dotdot entry */
1105 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1106 &LU_LPF_FID, S_IFDIR);
1108 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1114 if (parent2 == NULL || IS_ERR(parent2)) {
1115 dt_read_unlock(env, child);
1116 lfsck_ibits_unlock(&lh, LCK_PR);
1118 GOTO(out_done, rc = 1);
1121 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1122 (const struct dt_key *)name2, BYPASS_CAPA);
1123 dt_read_unlock(env, child);
1124 lfsck_ibits_unlock(&lh, LCK_PR);
1125 if (rc != 0 && rc != -ENOENT)
1128 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1129 if (type == LVLT_BY_BOOKMARK)
1130 GOTO(out_put, rc = 1);
1132 /* Trust the name entry, update the child's dotdot entry. */
1133 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1134 &LU_LPF_FID, S_IFDIR);
1139 if (type == LVLT_BY_BOOKMARK) {
1140 /* Invalid FID record in the bookmark file, reset it. */
1141 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1142 rc = lfsck_bookmark_store(env, lfsck);
1144 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1145 " in the bookmark file: rc = %d\n",
1146 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1147 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1148 /* The name entry is wrong, remove it. */
1149 rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR);
1155 if (parent2 != NULL && !IS_ERR(parent2))
1156 lu_object_put(env, &parent2->do_lu);
1163 * Verify the /ROOT/.lustre/lost+found/ directory.
1165 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1166 * the LFSCK does not exactly know how to handle, such as orphans. So before
1167 * the LFSCK scanning the system, the consistency of such directory needs to
1168 * be verified firstly to allow the users to use it during the LFSCK.
1170 * \param[in] env pointer to the thread context
1171 * \param[in] lfsck pointer to the lfsck instance
1173 * \retval positive number for uncertain inconsistency
1174 * \retval 0 for success
1175 * \retval negative error number on failure
1177 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1179 struct lfsck_thread_info *info = lfsck_env_info(env);
1180 struct lu_fid *pfid = &info->lti_fid;
1181 struct lu_fid *cfid = &info->lti_fid2;
1182 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1183 struct dt_object *parent = NULL;
1184 /* child1's FID is in the bookmark file. */
1185 struct dt_object *child1 = NULL;
1186 /* child2's FID is in the name entry MDTxxxx. */
1187 struct dt_object *child2 = NULL;
1188 struct dt_device *dev = lfsck->li_bottom;
1189 const struct lu_name *cname;
1191 int node = lfsck_dev_idx(dev);
1195 LASSERT(lfsck->li_master);
1198 parent = lfsck_object_find_by_dev(env, dev, &LU_LPF_FID);
1200 struct lfsck_tgt_desc *ltd;
1202 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1203 if (unlikely(ltd == NULL))
1206 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1212 RETURN(PTR_ERR(parent));
1214 LASSERT(dt_object_exists(parent));
1216 if (unlikely(!dt_try_as_dir(env, parent)))
1217 GOTO(put, rc = -ENOTDIR);
1220 rc = lfsck_scan_lpf_bad_entries(env, lfsck, parent);
1222 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1223 "for bad sub-directories: rc = %d\n",
1224 lfsck_lfsck2name(lfsck), rc);
1227 if (!fid_is_zero(&bk->lb_lpf_fid)) {
1228 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1229 struct lu_fid tfid = bk->lb_lpf_fid;
1231 /* Invalid FID record in the bookmark file, reset it. */
1232 fid_zero(&bk->lb_lpf_fid);
1233 rc = lfsck_bookmark_store(env, lfsck);
1235 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1236 " in the bookmark file: rc = %d\n",
1237 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1242 child1 = lfsck_object_find_by_dev(env, dev,
1245 GOTO(put, rc = PTR_ERR(child1));
1247 if (unlikely(!dt_object_exists(child1) ||
1248 dt_object_remote(child1)) ||
1249 !S_ISDIR(lfsck_object_type(child1))) {
1250 /* Invalid FID record in the bookmark file,
1252 fid_zero(&bk->lb_lpf_fid);
1253 rc = lfsck_bookmark_store(env, lfsck);
1255 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1256 " in the bookmark file: rc = %d\n",
1257 lfsck_lfsck2name(lfsck),
1258 PFID(lfsck_dto2fid(child1)), rc);
1263 lu_object_put(env, &child1->do_lu);
1265 } else if (unlikely(!dt_try_as_dir(env, child1))) {
1266 GOTO(put, rc = -ENOTDIR);
1271 snprintf(name, 8, "MDT%04x", node);
1272 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1273 (const struct dt_key *)name, BYPASS_CAPA);
1280 /* Invalid FID in the name entry, remove the name entry. */
1281 if (!fid_is_norm(cfid)) {
1282 rc = lfsck_remove_name_entry(env, lfsck, parent, name, S_IFDIR);
1289 child2 = lfsck_object_find_by_dev(env, dev, cfid);
1291 GOTO(put, rc = PTR_ERR(child2));
1293 if (unlikely(!dt_object_exists(child2) ||
1294 dt_object_remote(child2)) ||
1295 !S_ISDIR(lfsck_object_type(child2))) {
1296 rc = lfsck_remove_name_entry(env, lfsck, parent, name,
1304 if (unlikely(!dt_try_as_dir(env, child2)))
1305 GOTO(put, rc = -ENOTDIR);
1307 if (child1 == NULL) {
1308 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2, name,
1309 pfid, LVLT_BY_NAMEENTRY);
1310 } else if (!lu_fid_eq(cfid, &bk->lb_lpf_fid)) {
1311 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name,
1312 pfid, LVLT_BY_BOOKMARK);
1313 if (!lu_fid_eq(pfid, &LU_LPF_FID))
1314 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child2,
1318 if (lfsck->li_lpf_obj == NULL) {
1319 lu_object_get(&child2->do_lu);
1320 lfsck->li_lpf_obj = child2;
1323 cname = lfsck_name_get_const(env, name, strlen(name));
1324 rc = lfsck_verify_linkea(env, dev, child2, cname, &LU_LPF_FID);
1331 rc = lfsck_verify_lpf_pairs(env, lfsck, parent, child1, name,
1332 pfid, LVLT_BY_BOOKMARK);
1337 if (lfsck->li_lpf_obj != NULL &&
1338 unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj)))
1341 if (child2 != NULL && !IS_ERR(child2))
1342 lu_object_put(env, &child2->do_lu);
1343 if (child1 != NULL && !IS_ERR(child1))
1344 lu_object_put(env, &child1->do_lu);
1345 if (parent != NULL && !IS_ERR(parent))
1346 lu_object_put(env, &parent->do_lu);
1351 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1353 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1354 struct seq_server_site *ss;
1359 ss = lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1360 if (unlikely(ss == NULL))
1363 OBD_ALLOC_PTR(lfsck->li_seq);
1364 if (lfsck->li_seq == NULL)
1367 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1369 GOTO(out, rc = -ENOMEM);
1371 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1372 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1374 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1378 if (fid_is_sane(&bk->lb_last_fid))
1379 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1384 OBD_FREE_PTR(lfsck->li_seq);
1385 lfsck->li_seq = NULL;
1390 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1392 if (lfsck->li_seq != NULL) {
1393 seq_client_fini(lfsck->li_seq);
1394 OBD_FREE_PTR(lfsck->li_seq);
1395 lfsck->li_seq = NULL;
1399 void lfsck_instance_cleanup(const struct lu_env *env,
1400 struct lfsck_instance *lfsck)
1402 struct ptlrpc_thread *thread = &lfsck->li_thread;
1403 struct lfsck_component *com;
1404 struct lfsck_component *next;
1407 LASSERT(list_empty(&lfsck->li_link));
1408 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1410 if (lfsck->li_obj_oit != NULL) {
1411 lu_object_put_nocache(env, &lfsck->li_obj_oit->do_lu);
1412 lfsck->li_obj_oit = NULL;
1415 LASSERT(lfsck->li_obj_dir == NULL);
1417 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1418 lfsck_component_cleanup(env, com);
1421 LASSERT(list_empty(&lfsck->li_list_dir));
1423 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1425 lfsck_component_cleanup(env, com);
1428 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1429 lfsck_component_cleanup(env, com);
1432 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1433 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1435 if (lfsck->li_bookmark_obj != NULL) {
1436 lu_object_put_nocache(env, &lfsck->li_bookmark_obj->do_lu);
1437 lfsck->li_bookmark_obj = NULL;
1440 if (lfsck->li_lpf_obj != NULL) {
1441 lu_object_put(env, &lfsck->li_lpf_obj->do_lu);
1442 lfsck->li_lpf_obj = NULL;
1445 if (lfsck->li_los != NULL) {
1446 local_oid_storage_fini(env, lfsck->li_los);
1447 lfsck->li_los = NULL;
1450 lfsck_fid_fini(lfsck);
1452 OBD_FREE_PTR(lfsck);
1455 static inline struct lfsck_instance *
1456 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1458 struct lfsck_instance *lfsck;
1460 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1461 if (lfsck->li_bottom == key) {
1463 lfsck_instance_get(lfsck);
1465 list_del_init(&lfsck->li_link);
1474 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1477 struct lfsck_instance *lfsck;
1479 spin_lock(&lfsck_instance_lock);
1480 lfsck = __lfsck_instance_find(key, ref, unlink);
1481 spin_unlock(&lfsck_instance_lock);
1486 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1488 struct lfsck_instance *tmp;
1490 spin_lock(&lfsck_instance_lock);
1491 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1492 if (lfsck->li_bottom == tmp->li_bottom) {
1493 spin_unlock(&lfsck_instance_lock);
1498 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1499 spin_unlock(&lfsck_instance_lock);
1503 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1508 bool newline = (bits != 0 ? false : true);
1510 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1512 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1515 if (names[i] != NULL) {
1519 seq_printf(m, "%s%c", names[i],
1520 newline ? '\n' : ',');
1526 seq_printf(m, "\n");
1530 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *prefix)
1533 seq_printf(m, "%s: "LPU64" seconds\n", prefix,
1534 cfs_time_current_sec() - time);
1536 seq_printf(m, "%s: N/A\n", prefix);
1540 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1543 if (fid_is_zero(&pos->lp_dir_parent)) {
1544 if (pos->lp_oit_cookie == 0)
1545 seq_printf(m, "%s: N/A, N/A, N/A\n",
1548 seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1549 prefix, pos->lp_oit_cookie);
1551 seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1552 prefix, pos->lp_oit_cookie,
1553 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1558 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1559 struct lfsck_position *pos, bool init)
1561 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1563 if (unlikely(lfsck->li_di_oit == NULL)) {
1564 memset(pos, 0, sizeof(*pos));
1568 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1569 if (!lfsck->li_current_oit_processed && !init)
1570 pos->lp_oit_cookie--;
1572 LASSERT(pos->lp_oit_cookie > 0);
1574 if (lfsck->li_di_dir != NULL) {
1575 struct dt_object *dto = lfsck->li_obj_dir;
1577 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1580 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1581 fid_zero(&pos->lp_dir_parent);
1582 pos->lp_dir_cookie = 0;
1584 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1587 fid_zero(&pos->lp_dir_parent);
1588 pos->lp_dir_cookie = 0;
1592 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1596 if (limit != LFSCK_SPEED_NO_LIMIT) {
1598 lfsck->li_sleep_rate = limit / HZ;
1599 lfsck->li_sleep_jif = 1;
1601 lfsck->li_sleep_rate = 1;
1602 lfsck->li_sleep_jif = HZ / limit;
1605 lfsck->li_sleep_jif = 0;
1606 lfsck->li_sleep_rate = 0;
1609 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1610 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1617 void lfsck_control_speed(struct lfsck_instance *lfsck)
1619 struct ptlrpc_thread *thread = &lfsck->li_thread;
1620 struct l_wait_info lwi;
1622 if (lfsck->li_sleep_jif > 0 &&
1623 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1624 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1625 LWI_ON_SIGNAL_NOOP, NULL);
1627 l_wait_event(thread->t_ctl_waitq,
1628 !thread_is_running(thread),
1630 lfsck->li_new_scanned = 0;
1634 void lfsck_control_speed_by_self(struct lfsck_component *com)
1636 struct lfsck_instance *lfsck = com->lc_lfsck;
1637 struct ptlrpc_thread *thread = &lfsck->li_thread;
1638 struct l_wait_info lwi;
1640 if (lfsck->li_sleep_jif > 0 &&
1641 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1642 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1643 LWI_ON_SIGNAL_NOOP, NULL);
1645 l_wait_event(thread->t_ctl_waitq,
1646 !thread_is_running(thread),
1648 com->lc_new_scanned = 0;
1652 struct lfsck_thread_args *lfsck_thread_args_init(struct lfsck_instance *lfsck,
1653 struct lfsck_component *com,
1654 struct lfsck_start_param *lsp)
1656 struct lfsck_thread_args *lta;
1661 return ERR_PTR(-ENOMEM);
1663 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1669 lta->lta_lfsck = lfsck_instance_get(lfsck);
1671 lta->lta_com = lfsck_component_get(com);
1678 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1680 if (lta->lta_com != NULL)
1681 lfsck_component_put(<a->lta_env, lta->lta_com);
1682 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1683 lu_env_fini(<a->lta_env);
1687 static void lfsck_interpret(const struct lu_env *env,
1688 struct lfsck_instance *lfsck,
1689 struct ptlrpc_request *req, void *args, int result)
1691 struct lfsck_async_interpret_args *laia = args;
1692 struct lfsck_component *com;
1694 LASSERT(laia->laia_com == NULL);
1695 LASSERT(laia->laia_shared);
1697 spin_lock(&lfsck->li_lock);
1698 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
1699 if (com->lc_ops->lfsck_interpret != NULL) {
1700 laia->laia_com = com;
1701 com->lc_ops->lfsck_interpret(env, req, laia, result);
1705 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
1706 if (com->lc_ops->lfsck_interpret != NULL) {
1707 laia->laia_com = com;
1708 com->lc_ops->lfsck_interpret(env, req, laia, result);
1711 spin_unlock(&lfsck->li_lock);
1714 static int lfsck_stop_notify(const struct lu_env *env,
1715 struct lfsck_instance *lfsck,
1716 struct lfsck_tgt_descs *ltds,
1717 struct lfsck_tgt_desc *ltd, __u16 type)
1719 struct ptlrpc_request_set *set;
1720 struct lfsck_component *com;
1724 spin_lock(&lfsck->li_lock);
1725 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
1727 com = __lfsck_component_find(lfsck, type,
1728 &lfsck->li_list_double_scan);
1730 lfsck_component_get(com);
1731 spin_unlock(&lfsck->li_lock);
1734 if (com->lc_ops->lfsck_stop_notify != NULL) {
1735 set = ptlrpc_prep_set();
1737 lfsck_component_put(env, com);
1742 rc = com->lc_ops->lfsck_stop_notify(env, com, ltds,
1745 rc = ptlrpc_set_wait(set);
1747 ptlrpc_set_destroy(set);
1750 lfsck_component_put(env, com);
1756 static int lfsck_async_interpret(const struct lu_env *env,
1757 struct ptlrpc_request *req,
1760 struct lfsck_async_interpret_args *laia = args;
1761 struct lfsck_instance *lfsck;
1763 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
1765 lfsck_interpret(env, lfsck, req, laia, rc);
1766 lfsck_tgt_put(laia->laia_ltd);
1767 if (rc != 0 && laia->laia_result != -EALREADY)
1768 laia->laia_result = rc;
1773 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
1774 struct lfsck_request *lr,
1775 struct ptlrpc_request_set *set,
1776 ptlrpc_interpterer_t interpreter,
1777 void *args, int request)
1779 struct lfsck_async_interpret_args *laia;
1780 struct ptlrpc_request *req;
1781 struct lfsck_request *tmp;
1782 struct req_format *format;
1787 format = &RQF_LFSCK_NOTIFY;
1790 format = &RQF_LFSCK_QUERY;
1793 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
1794 exp->exp_obd->obd_name, request, -EINVAL);
1798 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
1802 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
1804 ptlrpc_request_free(req);
1809 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1811 ptlrpc_request_set_replen(req);
1813 laia = ptlrpc_req_async_args(req);
1814 *laia = *(struct lfsck_async_interpret_args *)args;
1815 if (laia->laia_com != NULL)
1816 lfsck_component_get(laia->laia_com);
1817 req->rq_interpret_reply = interpreter;
1818 ptlrpc_set_add_req(set, req);
1823 /* external interfaces */
1825 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
1828 struct lfsck_instance *lfsck;
1832 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1836 lfsck = lfsck_instance_find(key, true, false);
1837 if (likely(lfsck != NULL)) {
1838 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
1839 lfsck_instance_put(&env, lfsck);
1848 EXPORT_SYMBOL(lfsck_get_speed);
1850 int lfsck_set_speed(struct dt_device *key, int val)
1853 struct lfsck_instance *lfsck;
1857 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1861 lfsck = lfsck_instance_find(key, true, false);
1862 if (likely(lfsck != NULL)) {
1863 mutex_lock(&lfsck->li_mutex);
1864 if (__lfsck_set_speed(lfsck, val))
1865 rc = lfsck_bookmark_store(&env, lfsck);
1866 mutex_unlock(&lfsck->li_mutex);
1867 lfsck_instance_put(&env, lfsck);
1876 EXPORT_SYMBOL(lfsck_set_speed);
1878 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
1881 struct lfsck_instance *lfsck;
1885 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1889 lfsck = lfsck_instance_find(key, true, false);
1890 if (likely(lfsck != NULL)) {
1891 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
1892 lfsck_instance_put(&env, lfsck);
1901 EXPORT_SYMBOL(lfsck_get_windows);
1903 int lfsck_set_windows(struct dt_device *key, int val)
1906 struct lfsck_instance *lfsck;
1910 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1914 lfsck = lfsck_instance_find(key, true, false);
1915 if (likely(lfsck != NULL)) {
1916 if (val > LFSCK_ASYNC_WIN_MAX) {
1917 CWARN("%s: Too large async window size, which "
1918 "may cause memory issues. The valid range "
1919 "is [0 - %u]. If you do not want to restrict "
1920 "the window size for async requests pipeline, "
1921 "just set it as 0.\n",
1922 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
1924 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
1925 mutex_lock(&lfsck->li_mutex);
1926 lfsck->li_bookmark_ram.lb_async_windows = val;
1927 rc = lfsck_bookmark_store(&env, lfsck);
1928 mutex_unlock(&lfsck->li_mutex);
1930 lfsck_instance_put(&env, lfsck);
1939 EXPORT_SYMBOL(lfsck_set_windows);
1941 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
1944 struct lfsck_instance *lfsck;
1945 struct lfsck_component *com;
1949 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
1953 lfsck = lfsck_instance_find(key, true, false);
1954 if (likely(lfsck != NULL)) {
1955 com = lfsck_component_find(lfsck, type);
1956 if (likely(com != NULL)) {
1957 rc = com->lc_ops->lfsck_dump(&env, com, m);
1958 lfsck_component_put(&env, com);
1963 lfsck_instance_put(&env, lfsck);
1972 EXPORT_SYMBOL(lfsck_dump);
1974 static int lfsck_stop_all(const struct lu_env *env,
1975 struct lfsck_instance *lfsck,
1976 struct lfsck_stop *stop)
1978 struct lfsck_thread_info *info = lfsck_env_info(env);
1979 struct lfsck_request *lr = &info->lti_lr;
1980 struct lfsck_async_interpret_args *laia = &info->lti_laia;
1981 struct ptlrpc_request_set *set;
1982 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
1983 struct lfsck_tgt_desc *ltd;
1984 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1990 LASSERT(stop->ls_flags & LPF_BROADCAST);
1992 set = ptlrpc_prep_set();
1993 if (unlikely(set == NULL))
1996 memset(lr, 0, sizeof(*lr));
1997 lr->lr_event = LE_STOP;
1998 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1999 lr->lr_status = stop->ls_status;
2000 lr->lr_version = bk->lb_version;
2001 lr->lr_active = LFSCK_TYPES_ALL;
2002 lr->lr_param = stop->ls_flags;
2004 laia->laia_com = NULL;
2005 laia->laia_ltds = ltds;
2007 laia->laia_result = 0;
2008 laia->laia_shared = 1;
2010 down_read(<ds->ltd_rw_sem);
2011 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2012 ltd = lfsck_tgt_get(ltds, idx);
2013 LASSERT(ltd != NULL);
2015 laia->laia_ltd = ltd;
2016 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2017 lfsck_async_interpret, laia,
2020 lfsck_interpret(env, lfsck, NULL, laia, rc);
2022 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2023 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2027 up_read(<ds->ltd_rw_sem);
2029 rc = ptlrpc_set_wait(set);
2030 ptlrpc_set_destroy(set);
2033 rc = laia->laia_result;
2035 if (rc == -EALREADY)
2039 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2040 lfsck_lfsck2name(lfsck), rc);
2042 RETURN(rc != 0 ? rc : rc1);
2045 static int lfsck_start_all(const struct lu_env *env,
2046 struct lfsck_instance *lfsck,
2047 struct lfsck_start *start)
2049 struct lfsck_thread_info *info = lfsck_env_info(env);
2050 struct lfsck_request *lr = &info->lti_lr;
2051 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2052 struct ptlrpc_request_set *set;
2053 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2054 struct lfsck_tgt_desc *ltd;
2055 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2060 LASSERT(start->ls_flags & LPF_BROADCAST);
2062 set = ptlrpc_prep_set();
2063 if (unlikely(set == NULL))
2066 memset(lr, 0, sizeof(*lr));
2067 lr->lr_event = LE_START;
2068 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
2069 lr->lr_speed = bk->lb_speed_limit;
2070 lr->lr_version = bk->lb_version;
2071 lr->lr_active = start->ls_active;
2072 lr->lr_param = start->ls_flags;
2073 lr->lr_async_windows = bk->lb_async_windows;
2074 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2077 laia->laia_com = NULL;
2078 laia->laia_ltds = ltds;
2080 laia->laia_result = 0;
2081 laia->laia_shared = 1;
2083 down_read(<ds->ltd_rw_sem);
2084 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2085 ltd = lfsck_tgt_get(ltds, idx);
2086 LASSERT(ltd != NULL);
2088 laia->laia_ltd = ltd;
2089 ltd->ltd_layout_done = 0;
2090 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2091 lfsck_async_interpret, laia,
2094 lfsck_interpret(env, lfsck, NULL, laia, rc);
2096 CERROR("%s: cannot notify MDT %x for LFSCK "
2097 "start, failout: rc = %d\n",
2098 lfsck_lfsck2name(lfsck), idx, rc);
2102 up_read(<ds->ltd_rw_sem);
2105 ptlrpc_set_destroy(set);
2110 rc = ptlrpc_set_wait(set);
2111 ptlrpc_set_destroy(set);
2114 rc = laia->laia_result;
2117 struct lfsck_stop *stop = &info->lti_stop;
2119 CERROR("%s: cannot start LFSCK on some MDTs, "
2120 "stop all: rc = %d\n",
2121 lfsck_lfsck2name(lfsck), rc);
2122 if (rc != -EALREADY) {
2123 stop->ls_status = LS_FAILED;
2124 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2125 lfsck_stop_all(env, lfsck, stop);
2132 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2133 struct lfsck_start_param *lsp)
2135 struct lfsck_start *start = lsp->lsp_start;
2136 struct lfsck_instance *lfsck;
2137 struct lfsck_bookmark *bk;
2138 struct ptlrpc_thread *thread;
2139 struct lfsck_component *com;
2140 struct l_wait_info lwi = { 0 };
2141 struct lfsck_thread_args *lta;
2142 struct task_struct *task;
2149 lfsck = lfsck_instance_find(key, true, false);
2150 if (unlikely(lfsck == NULL))
2153 /* System is not ready, try again later. */
2154 if (unlikely(lfsck->li_namespace == NULL))
2155 GOTO(put, rc = -EAGAIN);
2157 /* start == NULL means auto trigger paused LFSCK. */
2158 if ((start == NULL) &&
2159 (list_empty(&lfsck->li_list_scan) ||
2160 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2163 bk = &lfsck->li_bookmark_ram;
2164 thread = &lfsck->li_thread;
2165 mutex_lock(&lfsck->li_mutex);
2166 spin_lock(&lfsck->li_lock);
2167 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2169 if (unlikely(start == NULL)) {
2170 spin_unlock(&lfsck->li_lock);
2174 while (start->ls_active != 0) {
2175 if (!(type & start->ls_active)) {
2180 com = __lfsck_component_find(lfsck, type,
2181 &lfsck->li_list_scan);
2183 com = __lfsck_component_find(lfsck, type,
2184 &lfsck->li_list_double_scan);
2190 if (com->lc_ops->lfsck_join != NULL) {
2191 rc = com->lc_ops->lfsck_join( env, com, lsp);
2192 if (rc != 0 && rc != -EALREADY)
2195 start->ls_active &= ~type;
2198 spin_unlock(&lfsck->li_lock);
2201 spin_unlock(&lfsck->li_lock);
2203 lfsck->li_status = 0;
2204 lfsck->li_oit_over = 0;
2205 lfsck->li_start_unplug = 0;
2206 lfsck->li_drop_dryrun = 0;
2207 lfsck->li_new_scanned = 0;
2209 /* For auto trigger. */
2213 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
2214 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
2215 lfsck_lfsck2name(lfsck));
2217 GOTO(out, rc = -EPERM);
2220 start->ls_version = bk->lb_version;
2222 if (start->ls_active != 0) {
2223 struct lfsck_component *next;
2225 if (start->ls_active == LFSCK_TYPES_ALL)
2226 start->ls_active = LFSCK_TYPES_SUPPORTED;
2228 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
2229 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
2230 GOTO(out, rc = -ENOTSUPP);
2233 list_for_each_entry_safe(com, next,
2234 &lfsck->li_list_scan, lc_link) {
2235 if (!(com->lc_type & start->ls_active)) {
2236 rc = com->lc_ops->lfsck_post(env, com, 0,
2243 while (start->ls_active != 0) {
2244 if (type & start->ls_active) {
2245 com = __lfsck_component_find(lfsck, type,
2246 &lfsck->li_list_idle);
2248 /* The component status will be updated
2249 * when its prep() is called later by
2250 * the LFSCK main engine. */
2251 list_move_tail(&com->lc_link,
2252 &lfsck->li_list_scan);
2253 start->ls_active &= ~type;
2259 if (list_empty(&lfsck->li_list_scan)) {
2260 /* The speed limit will be used to control both the LFSCK and
2261 * low layer scrub (if applied), need to be handled firstly. */
2262 if (start->ls_valid & LSV_SPEED_LIMIT) {
2263 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
2264 rc = lfsck_bookmark_store(env, lfsck);
2273 if (start->ls_flags & LPF_RESET)
2274 flags |= DOIF_RESET;
2276 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
2280 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2281 start->ls_active |= com->lc_type;
2282 if (flags & DOIF_RESET) {
2283 rc = com->lc_ops->lfsck_reset(env, com, false);
2290 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY;
2291 if (bk->lb_param & LPF_DRYRUN)
2292 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
2294 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
2295 valid |= DOIV_ERROR_HANDLE;
2296 if (start->ls_flags & LPF_FAILOUT)
2297 flags |= DOIF_FAILOUT;
2300 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
2301 valid |= DOIV_DRYRUN;
2302 if (start->ls_flags & LPF_DRYRUN)
2303 flags |= DOIF_DRYRUN;
2306 if (!list_empty(&lfsck->li_list_scan))
2307 flags |= DOIF_OUTUSED;
2309 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
2310 thread_set_flags(thread, 0);
2311 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
2313 GOTO(out, rc = PTR_ERR(lta));
2315 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
2316 task = kthread_run(lfsck_master_engine, lta, "lfsck");
2319 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
2320 lfsck_lfsck2name(lfsck), rc);
2321 lfsck_thread_args_fini(lta);
2326 l_wait_event(thread->t_ctl_waitq,
2327 thread_is_running(thread) ||
2328 thread_is_stopped(thread),
2330 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
2331 lfsck->li_start_unplug = 1;
2332 wake_up_all(&thread->t_ctl_waitq);
2337 /* release lfsck::li_mutex to avoid deadlock. */
2338 mutex_unlock(&lfsck->li_mutex);
2339 rc = lfsck_start_all(env, lfsck, start);
2341 spin_lock(&lfsck->li_lock);
2342 if (thread_is_stopped(thread)) {
2343 spin_unlock(&lfsck->li_lock);
2345 lfsck->li_status = LS_FAILED;
2346 lfsck->li_flags = 0;
2347 thread_set_flags(thread, SVC_STOPPING);
2348 spin_unlock(&lfsck->li_lock);
2350 lfsck->li_start_unplug = 1;
2351 wake_up_all(&thread->t_ctl_waitq);
2352 l_wait_event(thread->t_ctl_waitq,
2353 thread_is_stopped(thread),
2357 lfsck->li_start_unplug = 1;
2358 wake_up_all(&thread->t_ctl_waitq);
2364 mutex_unlock(&lfsck->li_mutex);
2367 lfsck_instance_put(env, lfsck);
2369 return rc < 0 ? rc : 0;
2371 EXPORT_SYMBOL(lfsck_start);
2373 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
2374 struct lfsck_stop *stop)
2376 struct lfsck_instance *lfsck;
2377 struct ptlrpc_thread *thread;
2378 struct l_wait_info lwi = { 0 };
2383 lfsck = lfsck_instance_find(key, true, false);
2384 if (unlikely(lfsck == NULL))
2387 thread = &lfsck->li_thread;
2388 /* release lfsck::li_mutex to avoid deadlock. */
2389 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
2390 if (!lfsck->li_master) {
2391 CERROR("%s: only allow to specify '-A' via MDS\n",
2392 lfsck_lfsck2name(lfsck));
2394 GOTO(out, rc = -EPERM);
2397 rc1 = lfsck_stop_all(env, lfsck, stop);
2400 mutex_lock(&lfsck->li_mutex);
2401 spin_lock(&lfsck->li_lock);
2402 /* no error if LFSCK is already stopped, or was never started */
2403 if (thread_is_init(thread) || thread_is_stopped(thread)) {
2404 spin_unlock(&lfsck->li_lock);
2409 lfsck->li_status = stop->ls_status;
2410 lfsck->li_flags = stop->ls_flags;
2412 lfsck->li_status = LS_STOPPED;
2413 lfsck->li_flags = 0;
2416 thread_set_flags(thread, SVC_STOPPING);
2417 spin_unlock(&lfsck->li_lock);
2419 wake_up_all(&thread->t_ctl_waitq);
2420 l_wait_event(thread->t_ctl_waitq,
2421 thread_is_stopped(thread),
2427 mutex_unlock(&lfsck->li_mutex);
2428 lfsck_instance_put(env, lfsck);
2430 return rc != 0 ? rc : rc1;
2432 EXPORT_SYMBOL(lfsck_stop);
2434 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
2435 struct lfsck_request *lr)
2437 int rc = -EOPNOTSUPP;
2440 switch (lr->lr_event) {
2442 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
2443 struct lfsck_start_param lsp;
2445 memset(start, 0, sizeof(*start));
2446 start->ls_valid = lr->lr_valid;
2447 start->ls_speed_limit = lr->lr_speed;
2448 start->ls_version = lr->lr_version;
2449 start->ls_active = lr->lr_active;
2450 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2451 start->ls_async_windows = lr->lr_async_windows;
2453 lsp.lsp_start = start;
2454 lsp.lsp_index = lr->lr_index;
2455 lsp.lsp_index_valid = 1;
2456 rc = lfsck_start(env, key, &lsp);
2460 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2462 memset(stop, 0, sizeof(*stop));
2463 stop->ls_status = lr->lr_status;
2464 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2465 rc = lfsck_stop(env, key, stop);
2468 case LE_PHASE1_DONE:
2469 case LE_PHASE2_DONE:
2470 case LE_FID_ACCESSED:
2472 case LE_CONDITIONAL_DESTROY:
2473 case LE_PAIRS_VERIFY: {
2474 struct lfsck_instance *lfsck;
2475 struct lfsck_component *com;
2477 lfsck = lfsck_instance_find(key, true, false);
2478 if (unlikely(lfsck == NULL))
2481 com = lfsck_component_find(lfsck, lr->lr_active);
2482 if (likely(com != NULL)) {
2483 rc = com->lc_ops->lfsck_in_notify(env, com, lr);
2484 lfsck_component_put(env, com);
2487 lfsck_instance_put(env, lfsck);
2496 EXPORT_SYMBOL(lfsck_in_notify);
2498 int lfsck_query(const struct lu_env *env, struct dt_device *key,
2499 struct lfsck_request *lr)
2501 struct lfsck_instance *lfsck;
2502 struct lfsck_component *com;
2506 lfsck = lfsck_instance_find(key, true, false);
2507 if (unlikely(lfsck == NULL))
2510 com = lfsck_component_find(lfsck, lr->lr_active);
2511 if (likely(com != NULL)) {
2512 rc = com->lc_ops->lfsck_query(env, com);
2513 lfsck_component_put(env, com);
2518 lfsck_instance_put(env, lfsck);
2522 EXPORT_SYMBOL(lfsck_query);
2524 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
2525 struct ldlm_namespace *ns)
2527 struct lfsck_instance *lfsck;
2530 lfsck = lfsck_instance_find(key, true, false);
2531 if (likely(lfsck != NULL)) {
2532 lfsck->li_namespace = ns;
2533 lfsck_instance_put(env, lfsck);
2539 EXPORT_SYMBOL(lfsck_register_namespace);
2541 int lfsck_register(const struct lu_env *env, struct dt_device *key,
2542 struct dt_device *next, struct obd_device *obd,
2543 lfsck_out_notify notify, void *notify_data, bool master)
2545 struct lfsck_instance *lfsck;
2546 struct dt_object *root = NULL;
2547 struct dt_object *obj = NULL;
2548 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
2552 lfsck = lfsck_instance_find(key, false, false);
2553 if (unlikely(lfsck != NULL))
2556 OBD_ALLOC_PTR(lfsck);
2560 mutex_init(&lfsck->li_mutex);
2561 spin_lock_init(&lfsck->li_lock);
2562 INIT_LIST_HEAD(&lfsck->li_link);
2563 INIT_LIST_HEAD(&lfsck->li_list_scan);
2564 INIT_LIST_HEAD(&lfsck->li_list_dir);
2565 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
2566 INIT_LIST_HEAD(&lfsck->li_list_idle);
2567 atomic_set(&lfsck->li_ref, 1);
2568 atomic_set(&lfsck->li_double_scan_count, 0);
2569 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
2570 lfsck->li_out_notify = notify;
2571 lfsck->li_out_notify_data = notify_data;
2572 lfsck->li_next = next;
2573 lfsck->li_bottom = key;
2574 lfsck->li_obd = obd;
2576 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
2580 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
2584 fid->f_seq = FID_SEQ_LOCAL_NAME;
2587 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
2591 rc = dt_root_get(env, key, fid);
2595 root = dt_locate(env, key, fid);
2597 GOTO(out, rc = PTR_ERR(root));
2599 if (unlikely(!dt_try_as_dir(env, root)))
2600 GOTO(out, rc = -ENOTDIR);
2602 lfsck->li_local_root_fid = *fid;
2604 lfsck->li_master = 1;
2605 if (lfsck_dev_idx(key) == 0) {
2606 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
2607 const struct lu_name *cname;
2609 rc = dt_lookup(env, root,
2610 (struct dt_rec *)(&lfsck->li_global_root_fid),
2611 (const struct dt_key *)"ROOT", BYPASS_CAPA);
2615 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
2617 GOTO(out, rc = PTR_ERR(obj));
2619 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
2620 (const struct dt_key *)dotlustre, BYPASS_CAPA);
2624 lu_object_put(env, &obj->do_lu);
2625 obj = dt_locate(env, key, fid);
2627 GOTO(out, rc = PTR_ERR(obj));
2629 cname = lfsck_name_get_const(env, dotlustre,
2631 rc = lfsck_verify_linkea(env, key, obj, cname,
2632 &lfsck->li_global_root_fid);
2637 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
2638 (const struct dt_key *)lostfound,
2643 lu_object_put(env, &obj->do_lu);
2644 obj = dt_locate(env, key, fid);
2646 GOTO(out, rc = PTR_ERR(obj));
2648 cname = lfsck_name_get_const(env, lostfound,
2650 rc = lfsck_verify_linkea(env, key, obj, cname, pfid);
2654 lu_object_put(env, &obj->do_lu);
2659 fid->f_seq = FID_SEQ_LOCAL_FILE;
2660 fid->f_oid = OTABLE_IT_OID;
2662 obj = dt_locate(env, key, fid);
2664 GOTO(out, rc = PTR_ERR(obj));
2666 lu_object_get(&obj->do_lu);
2667 lfsck->li_obj_oit = obj;
2668 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
2672 rc = lfsck_bookmark_setup(env, lfsck);
2677 rc = lfsck_fid_init(lfsck);
2681 rc = lfsck_namespace_setup(env, lfsck);
2686 rc = lfsck_layout_setup(env, lfsck);
2690 /* XXX: more LFSCK components initialization to be added here. */
2692 rc = lfsck_instance_add(lfsck);
2694 rc = lfsck_add_target_from_orphan(env, lfsck);
2696 if (obj != NULL && !IS_ERR(obj))
2697 lu_object_put(env, &obj->do_lu);
2698 if (root != NULL && !IS_ERR(root))
2699 lu_object_put(env, &root->do_lu);
2701 lfsck_instance_cleanup(env, lfsck);
2704 EXPORT_SYMBOL(lfsck_register);
2706 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
2708 struct lfsck_instance *lfsck;
2710 lfsck = lfsck_instance_find(key, false, true);
2712 lfsck_instance_put(env, lfsck);
2714 EXPORT_SYMBOL(lfsck_degister);
2716 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
2717 struct dt_device *tgt, struct obd_export *exp,
2718 __u32 index, bool for_ost)
2720 struct lfsck_instance *lfsck;
2721 struct lfsck_tgt_desc *ltd;
2732 INIT_LIST_HEAD(<d->ltd_orphan_list);
2733 INIT_LIST_HEAD(<d->ltd_layout_list);
2734 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
2735 atomic_set(<d->ltd_ref, 1);
2736 ltd->ltd_index = index;
2738 spin_lock(&lfsck_instance_lock);
2739 lfsck = __lfsck_instance_find(key, true, false);
2740 if (lfsck == NULL) {
2742 list_add_tail(<d->ltd_orphan_list,
2743 &lfsck_ost_orphan_list);
2745 list_add_tail(<d->ltd_orphan_list,
2746 &lfsck_mdt_orphan_list);
2747 spin_unlock(&lfsck_instance_lock);
2751 spin_unlock(&lfsck_instance_lock);
2753 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
2757 lfsck_instance_put(env, lfsck);
2761 EXPORT_SYMBOL(lfsck_add_target);
2763 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
2764 struct dt_device *tgt, __u32 index, bool for_ost)
2766 struct lfsck_instance *lfsck;
2767 struct lfsck_tgt_descs *ltds;
2768 struct lfsck_tgt_desc *ltd;
2769 struct list_head *head;
2772 head = &lfsck_ost_orphan_list;
2774 head = &lfsck_mdt_orphan_list;
2776 spin_lock(&lfsck_instance_lock);
2777 list_for_each_entry(ltd, head, ltd_orphan_list) {
2778 if (ltd->ltd_tgt == tgt) {
2779 list_del_init(<d->ltd_orphan_list);
2780 spin_unlock(&lfsck_instance_lock);
2788 lfsck = __lfsck_instance_find(key, true, false);
2789 spin_unlock(&lfsck_instance_lock);
2790 if (unlikely(lfsck == NULL))
2794 ltds = &lfsck->li_ost_descs;
2796 ltds = &lfsck->li_mdt_descs;
2798 down_write(<ds->ltd_rw_sem);
2799 LASSERT(ltds->ltd_tgts_bitmap != NULL);
2801 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
2804 ltd = LTD_TGT(ltds, index);
2805 if (unlikely(ltd == NULL))
2808 LASSERT(ltds->ltd_tgtnr > 0);
2811 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
2812 LTD_TGT(ltds, index) = NULL;
2817 head = &lfsck->li_ost_descs.ltd_orphan;
2819 head = &lfsck->li_mdt_descs.ltd_orphan;
2821 list_for_each_entry(ltd, head, ltd_orphan_list) {
2822 if (ltd->ltd_tgt == tgt) {
2823 list_del_init(<d->ltd_orphan_list);
2829 up_write(<ds->ltd_rw_sem);
2831 spin_lock(<ds->ltd_lock);
2833 spin_unlock(<ds->ltd_lock);
2834 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
2838 lfsck_instance_put(env, lfsck);
2840 EXPORT_SYMBOL(lfsck_del_target);
2842 static int __init lfsck_init(void)
2846 INIT_LIST_HEAD(&lfsck_instance_list);
2847 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
2848 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
2849 lfsck_key_init_generic(&lfsck_thread_key, NULL);
2850 rc = lu_context_key_register(&lfsck_thread_key);
2852 tgt_register_lfsck_in_notify(lfsck_in_notify);
2853 tgt_register_lfsck_query(lfsck_query);
2859 static void __exit lfsck_exit(void)
2861 struct lfsck_tgt_desc *ltd;
2862 struct lfsck_tgt_desc *next;
2864 LASSERT(list_empty(&lfsck_instance_list));
2866 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
2868 list_del_init(<d->ltd_orphan_list);
2872 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
2874 list_del_init(<d->ltd_orphan_list);
2878 lu_context_key_degister(&lfsck_thread_key);
2881 MODULE_AUTHOR("Intel Corporation <http://www.intel.com/>");
2882 MODULE_DESCRIPTION("LFSCK");
2883 MODULE_LICENSE("GPL");
2885 cfs_module(lfsck, LUSTRE_VERSION_STRING, lfsck_init, lfsck_exit);