4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, 2015, Intel Corporation.
26 * lustre/lfsck/lfsck_lib.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <linux/kthread.h>
34 #include <linux/sched.h>
35 #include <libcfs/list.h>
36 #include <lu_object.h>
37 #include <dt_object.h>
38 #include <md_object.h>
39 #include <lustre_fld.h>
40 #include <lustre_lib.h>
41 #include <lustre_net.h>
42 #include <lustre_lfsck.h>
43 #include <lustre/lustre_lfsck_user.h>
45 #include "lfsck_internal.h"
47 #define LFSCK_CHECKPOINT_SKIP 1
49 /* define lfsck thread key */
50 LU_KEY_INIT(lfsck, struct lfsck_thread_info);
52 static void lfsck_key_fini(const struct lu_context *ctx,
53 struct lu_context_key *key, void *data)
55 struct lfsck_thread_info *info = data;
57 lu_buf_free(&info->lti_linkea_buf);
58 lu_buf_free(&info->lti_linkea_buf2);
59 lu_buf_free(&info->lti_big_buf);
63 LU_CONTEXT_KEY_DEFINE(lfsck, LCT_MD_THREAD | LCT_DT_THREAD);
64 LU_KEY_INIT_GENERIC(lfsck);
66 static struct list_head lfsck_instance_list;
67 static struct list_head lfsck_ost_orphan_list;
68 static struct list_head lfsck_mdt_orphan_list;
69 static DEFINE_SPINLOCK(lfsck_instance_lock);
71 const char *lfsck_flags_names[] = {
80 const char *lfsck_param_names[] = {
92 enum lfsck_verify_lpf_types {
94 LVLT_BY_NAMEENTRY = 1,
98 lfsck_reset_ltd_status(struct lfsck_tgt_desc *ltd, enum lfsck_type type)
100 if (type == LFSCK_TYPE_LAYOUT) {
101 ltd->ltd_layout_status = LS_MAX;
102 ltd->ltd_layout_repaired = 0;
104 ltd->ltd_namespace_status = LS_MAX;
105 ltd->ltd_namespace_repaired = 0;
109 static int lfsck_tgt_descs_init(struct lfsck_tgt_descs *ltds)
111 spin_lock_init(<ds->ltd_lock);
112 init_rwsem(<ds->ltd_rw_sem);
113 INIT_LIST_HEAD(<ds->ltd_orphan);
114 ltds->ltd_tgts_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
115 if (ltds->ltd_tgts_bitmap == NULL)
121 static void lfsck_tgt_descs_fini(struct lfsck_tgt_descs *ltds)
123 struct lfsck_tgt_desc *ltd;
124 struct lfsck_tgt_desc *next;
127 down_write(<ds->ltd_rw_sem);
129 list_for_each_entry_safe(ltd, next, <ds->ltd_orphan,
131 list_del_init(<d->ltd_orphan_list);
135 if (unlikely(ltds->ltd_tgts_bitmap == NULL)) {
136 up_write(<ds->ltd_rw_sem);
141 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
142 ltd = lfsck_ltd2tgt(ltds, idx);
143 if (likely(ltd != NULL)) {
144 LASSERT(list_empty(<d->ltd_layout_list));
145 LASSERT(list_empty(<d->ltd_layout_phase_list));
146 LASSERT(list_empty(<d->ltd_namespace_list));
147 LASSERT(list_empty(<d->ltd_namespace_phase_list));
150 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, idx);
151 lfsck_assign_tgt(ltds, NULL, idx);
156 LASSERTF(ltds->ltd_tgtnr == 0, "tgt count unmatched: %d\n",
159 for (idx = 0; idx < TGT_PTRS; idx++) {
160 if (ltds->ltd_tgts_idx[idx] != NULL) {
161 OBD_FREE_PTR(ltds->ltd_tgts_idx[idx]);
162 ltds->ltd_tgts_idx[idx] = NULL;
166 CFS_FREE_BITMAP(ltds->ltd_tgts_bitmap);
167 ltds->ltd_tgts_bitmap = NULL;
168 up_write(<ds->ltd_rw_sem);
171 static int __lfsck_add_target(const struct lu_env *env,
172 struct lfsck_instance *lfsck,
173 struct lfsck_tgt_desc *ltd,
174 bool for_ost, bool locked)
176 struct lfsck_tgt_descs *ltds;
177 __u32 index = ltd->ltd_index;
182 ltds = &lfsck->li_ost_descs;
184 ltds = &lfsck->li_mdt_descs;
187 down_write(<ds->ltd_rw_sem);
189 LASSERT(ltds->ltd_tgts_bitmap != NULL);
191 if (index >= ltds->ltd_tgts_bitmap->size) {
192 __u32 newsize = max((__u32)ltds->ltd_tgts_bitmap->size,
193 (__u32)BITS_PER_LONG);
194 struct cfs_bitmap *old_bitmap = ltds->ltd_tgts_bitmap;
195 struct cfs_bitmap *new_bitmap;
197 while (newsize < index + 1)
200 new_bitmap = CFS_ALLOCATE_BITMAP(newsize);
201 if (new_bitmap == NULL)
202 GOTO(unlock, rc = -ENOMEM);
204 if (ltds->ltd_tgtnr > 0)
205 cfs_bitmap_copy(new_bitmap, old_bitmap);
206 ltds->ltd_tgts_bitmap = new_bitmap;
207 CFS_FREE_BITMAP(old_bitmap);
210 if (cfs_bitmap_check(ltds->ltd_tgts_bitmap, index)) {
211 CERROR("%s: the device %s (%u) is registered already\n",
212 lfsck_lfsck2name(lfsck),
213 ltd->ltd_tgt->dd_lu_dev.ld_obd->obd_name, index);
214 GOTO(unlock, rc = -EEXIST);
217 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL) {
218 OBD_ALLOC_PTR(ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK]);
219 if (ltds->ltd_tgts_idx[index / TGT_PTRS_PER_BLOCK] == NULL)
220 GOTO(unlock, rc = -ENOMEM);
223 lfsck_assign_tgt(ltds, ltd, index);
224 cfs_bitmap_set(ltds->ltd_tgts_bitmap, index);
227 GOTO(unlock, rc = 0);
231 up_write(<ds->ltd_rw_sem);
236 static int lfsck_add_target_from_orphan(const struct lu_env *env,
237 struct lfsck_instance *lfsck)
239 struct lfsck_tgt_descs *ltds = &lfsck->li_ost_descs;
240 struct lfsck_tgt_desc *ltd;
241 struct lfsck_tgt_desc *next;
242 struct list_head *head = &lfsck_ost_orphan_list;
247 spin_lock(&lfsck_instance_lock);
248 list_for_each_entry_safe(ltd, next, head, ltd_orphan_list) {
249 if (ltd->ltd_key == lfsck->li_bottom)
250 list_move_tail(<d->ltd_orphan_list,
253 spin_unlock(&lfsck_instance_lock);
255 down_write(<ds->ltd_rw_sem);
256 while (!list_empty(<ds->ltd_orphan)) {
257 ltd = list_entry(ltds->ltd_orphan.next,
258 struct lfsck_tgt_desc,
260 list_del_init(<d->ltd_orphan_list);
261 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, true);
262 /* Do not hold the semaphore for too long time. */
263 up_write(<ds->ltd_rw_sem);
267 down_write(<ds->ltd_rw_sem);
269 up_write(<ds->ltd_rw_sem);
272 ltds = &lfsck->li_mdt_descs;
273 head = &lfsck_mdt_orphan_list;
281 static inline struct lfsck_component *
282 __lfsck_component_find(struct lfsck_instance *lfsck, __u16 type,
283 struct list_head *list)
285 struct lfsck_component *com;
287 list_for_each_entry(com, list, lc_link) {
288 if (com->lc_type == type)
294 struct lfsck_component *
295 lfsck_component_find(struct lfsck_instance *lfsck, __u16 type)
297 struct lfsck_component *com;
299 spin_lock(&lfsck->li_lock);
300 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
304 com = __lfsck_component_find(lfsck, type,
305 &lfsck->li_list_double_scan);
309 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_idle);
313 lfsck_component_get(com);
314 spin_unlock(&lfsck->li_lock);
318 void lfsck_component_cleanup(const struct lu_env *env,
319 struct lfsck_component *com)
321 if (!list_empty(&com->lc_link))
322 list_del_init(&com->lc_link);
323 if (!list_empty(&com->lc_link_dir))
324 list_del_init(&com->lc_link_dir);
326 lfsck_component_put(env, com);
329 int lfsck_fid_alloc(const struct lu_env *env, struct lfsck_instance *lfsck,
330 struct lu_fid *fid, bool locked)
332 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
337 mutex_lock(&lfsck->li_mutex);
339 rc = seq_client_alloc_fid(env, lfsck->li_seq, fid);
341 bk->lb_last_fid = *fid;
342 /* We do not care about whether the subsequent sub-operations
343 * failed or not. The worst case is that one FID is lost that
344 * is not a big issue for the LFSCK since it is relative rare
345 * for LFSCK create. */
346 rc = lfsck_bookmark_store(env, lfsck);
350 mutex_unlock(&lfsck->li_mutex);
355 static int __lfsck_ibits_lock(const struct lu_env *env,
356 struct lfsck_instance *lfsck,
357 struct dt_object *obj, struct ldlm_res_id *resid,
358 struct lustre_handle *lh, __u64 bits,
361 struct lfsck_thread_info *info = lfsck_env_info(env);
362 union ldlm_policy_data *policy = &info->lti_policy;
363 __u64 flags = LDLM_FL_ATOMIC_CB;
366 LASSERT(lfsck->li_namespace != NULL);
368 memset(policy, 0, sizeof(*policy));
369 policy->l_inodebits.bits = bits;
370 if (dt_object_remote(obj)) {
371 struct ldlm_enqueue_info *einfo = &info->lti_einfo;
373 memset(einfo, 0, sizeof(*einfo));
374 einfo->ei_type = LDLM_IBITS;
375 einfo->ei_mode = mode;
376 einfo->ei_cb_bl = ldlm_blocking_ast;
377 einfo->ei_cb_cp = ldlm_completion_ast;
378 einfo->ei_res_id = resid;
380 rc = dt_object_lock(env, obj, lh, einfo, policy);
382 rc = ldlm_cli_enqueue_local(lfsck->li_namespace, resid,
383 LDLM_IBITS, policy, mode,
384 &flags, ldlm_blocking_ast,
385 ldlm_completion_ast, NULL, NULL,
386 0, LVB_T_NONE, NULL, lh);
389 if (rc == ELDLM_OK) {
392 memset(lh, 0, sizeof(*lh));
400 * Request the specified ibits lock for the given object.
402 * Before the LFSCK modifying on the namespace visible object,
403 * it needs to acquire related ibits ldlm lock.
405 * \param[in] env pointer to the thread context
406 * \param[in] lfsck pointer to the lfsck instance
407 * \param[in] obj pointer to the dt_object to be locked
408 * \param[out] lh pointer to the lock handle
409 * \param[in] bits the bits for the ldlm lock to be acquired
410 * \param[in] mode the mode for the ldlm lock to be acquired
412 * \retval 0 for success
413 * \retval negative error number on failure
415 int lfsck_ibits_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
416 struct dt_object *obj, struct lustre_handle *lh,
417 __u64 bits, enum ldlm_mode mode)
419 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
421 LASSERT(!lustre_handle_is_used(lh));
423 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
424 return __lfsck_ibits_lock(env, lfsck, obj, resid, lh, bits, mode);
428 * Release the the specified ibits lock.
430 * If the lock has been acquired before, release it
431 * and cleanup the handle. Otherwise, do nothing.
433 * \param[in] lh pointer to the lock handle
434 * \param[in] mode the mode for the ldlm lock to be released
436 void lfsck_ibits_unlock(struct lustre_handle *lh, enum ldlm_mode mode)
438 if (lustre_handle_is_used(lh)) {
439 ldlm_lock_decref(lh, mode);
440 memset(lh, 0, sizeof(*lh));
445 * Request compound ibits locks for the given <obj, name> pairs.
447 * Before the LFSCK modifying on the namespace visible object, it needs to
448 * acquire related ibits ldlm lock. Usually, we can use lfsck_ibits_lock for
449 * the lock purpose. But the simple lfsck_ibits_lock for directory-based
450 * modificationis (such as insert name entry to the directory) may be too
451 * coarse-grained and not efficient.
453 * The lfsck_lock() will request compound ibits locks on the specified
454 * <obj, name> pairs: the PDO (Parallel Directory Operations) ibits (UPDATE)
455 * lock on the directory object, and the regular ibits lock on the name hash.
457 * \param[in] env pointer to the thread context
458 * \param[in] lfsck pointer to the lfsck instance
459 * \param[in] obj pointer to the dt_object to be locked
460 * \param[in] name used for building the PDO lock resource
461 * \param[out] llh pointer to the lfsck_lock_handle
462 * \param[in] bits the bits for the ldlm lock to be acquired
463 * \param[in] mode the mode for the ldlm lock to be acquired
465 * \retval 0 for success
466 * \retval negative error number on failure
468 int lfsck_lock(const struct lu_env *env, struct lfsck_instance *lfsck,
469 struct dt_object *obj, const char *name,
470 struct lfsck_lock_handle *llh, __u64 bits, enum ldlm_mode mode)
472 struct ldlm_res_id *resid = &lfsck_env_info(env)->lti_resid;
475 LASSERT(S_ISDIR(lfsck_object_type(obj)));
476 LASSERT(name != NULL);
477 LASSERT(name[0] != 0);
478 LASSERT(!lustre_handle_is_used(&llh->llh_pdo_lh));
479 LASSERT(!lustre_handle_is_used(&llh->llh_reg_lh));
483 llh->llh_pdo_mode = LCK_EX;
486 llh->llh_pdo_mode = LCK_CW;
489 llh->llh_pdo_mode = LCK_CR;
492 CDEBUG(D_LFSCK, "%s: unexpected PDO lock mode %u on the obj "
493 DFID"\n", lfsck_lfsck2name(lfsck), mode,
494 PFID(lfsck_dto2fid(obj)));
498 fid_build_reg_res_name(lfsck_dto2fid(obj), resid);
499 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_pdo_lh,
500 MDS_INODELOCK_UPDATE, llh->llh_pdo_mode);
504 llh->llh_reg_mode = mode;
505 resid->name[LUSTRE_RES_ID_HSH_OFF] = full_name_hash(name, strlen(name));
506 LASSERT(resid->name[LUSTRE_RES_ID_HSH_OFF] != 0);
507 rc = __lfsck_ibits_lock(env, lfsck, obj, resid, &llh->llh_reg_lh,
508 bits, llh->llh_reg_mode);
510 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
516 * Release the the compound ibits locks.
518 * \param[in] llh pointer to the lfsck_lock_handle to be released
520 void lfsck_unlock(struct lfsck_lock_handle *llh)
522 lfsck_ibits_unlock(&llh->llh_reg_lh, llh->llh_reg_mode);
523 lfsck_ibits_unlock(&llh->llh_pdo_lh, llh->llh_pdo_mode);
526 int lfsck_find_mdt_idx_by_fid(const struct lu_env *env,
527 struct lfsck_instance *lfsck,
528 const struct lu_fid *fid)
530 struct seq_server_site *ss = lfsck_dev_site(lfsck);
531 struct lu_seq_range *range = &lfsck_env_info(env)->lti_range;
534 fld_range_set_mdt(range);
535 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
537 rc = range->lsr_index;
542 const char dot[] = ".";
543 const char dotdot[] = "..";
544 static const char dotlustre[] = ".lustre";
545 static const char lostfound[] = "lost+found";
548 * Remove the name entry from the .lustre/lost+found directory.
550 * No need to care about the object referenced by the name entry,
551 * either the name entry is invalid or redundant, or the referenced
552 * object has been processed or will be handled by others.
554 * \param[in] env pointer to the thread context
555 * \param[in] lfsck pointer to the lfsck instance
556 * \param[in] name the name for the name entry to be removed
558 * \retval 0 for success
559 * \retval negative error number on failure
561 static int lfsck_lpf_remove_name_entry(const struct lu_env *env,
562 struct lfsck_instance *lfsck,
565 struct dt_object *parent = lfsck->li_lpf_root_obj;
566 struct dt_device *dev = lfsck_obj2dev(parent);
568 struct lfsck_lock_handle *llh = &lfsck_env_info(env)->lti_llh;
572 rc = lfsck_lock(env, lfsck, parent, name, llh,
573 MDS_INODELOCK_UPDATE, LCK_PW);
577 th = dt_trans_create(env, dev);
579 GOTO(unlock, rc = PTR_ERR(th));
581 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
585 rc = dt_declare_ref_del(env, parent, th);
589 rc = dt_trans_start_local(env, dev, th);
593 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
597 dt_write_lock(env, parent, 0);
598 rc = dt_ref_del(env, parent, th);
599 dt_write_unlock(env, parent);
604 dt_trans_stop(env, dev, th);
609 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s: rc = %d\n",
610 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(parent)), name, rc);
615 static int lfsck_create_lpf_local(const struct lu_env *env,
616 struct lfsck_instance *lfsck,
617 struct dt_object *child,
619 struct dt_object_format *dof,
622 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
623 struct dt_object *parent = lfsck->li_lpf_root_obj;
624 struct dt_device *dev = lfsck_obj2dev(child);
625 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
626 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
627 const struct lu_fid *cfid = lfsck_dto2fid(child);
628 struct thandle *th = NULL;
629 struct linkea_data ldata = { NULL };
630 struct lu_buf linkea_buf;
631 const struct lu_name *cname;
633 int len = sizeof(struct lfsck_bookmark);
637 rc = linkea_data_new(&ldata,
638 &lfsck_env_info(env)->lti_linkea_buf2);
642 cname = lfsck_name_get_const(env, name, strlen(name));
643 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
647 th = dt_trans_create(env, dev);
651 /* 1a. create child */
652 rc = dt_declare_create(env, child, la, NULL, dof, th);
656 if (!dt_try_as_dir(env, child))
657 GOTO(stop, rc = -ENOTDIR);
659 /* 2a. increase child nlink */
660 rc = dt_declare_ref_add(env, child, th);
664 /* 3a. insert dot into child dir */
665 rec->rec_type = S_IFDIR;
667 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
668 (const struct dt_key *)dot, th);
672 /* 4a. insert dotdot into child dir */
673 rec->rec_fid = &LU_LPF_FID;
674 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
675 (const struct dt_key *)dotdot, th);
679 /* 5a. insert linkEA for child */
680 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
681 ldata.ld_leh->leh_len);
682 rc = dt_declare_xattr_set(env, child, &linkea_buf,
683 XATTR_NAME_LINK, 0, th);
687 /* 6a. insert name into parent dir */
688 rec->rec_type = S_IFDIR;
690 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
691 (const struct dt_key *)name, th);
695 /* 7a. increase parent nlink */
696 rc = dt_declare_ref_add(env, parent, th);
700 /* 8a. update bookmark */
701 rc = dt_declare_record_write(env, bk_obj,
702 lfsck_buf_get(env, bk, len), 0, th);
706 rc = dt_trans_start_local(env, dev, th);
710 dt_write_lock(env, child, 0);
711 /* 1b. create child */
712 rc = dt_create(env, child, la, NULL, dof, th);
716 /* 2b. increase child nlink */
717 rc = dt_ref_add(env, child, th);
721 /* 3b. insert dot into child dir */
723 rc = dt_insert(env, child, (const struct dt_rec *)rec,
724 (const struct dt_key *)dot, th, 1);
728 /* 4b. insert dotdot into child dir */
729 rec->rec_fid = &LU_LPF_FID;
730 rc = dt_insert(env, child, (const struct dt_rec *)rec,
731 (const struct dt_key *)dotdot, th, 1);
735 /* 5b. insert linkEA for child. */
736 rc = dt_xattr_set(env, child, &linkea_buf,
737 XATTR_NAME_LINK, 0, th);
738 dt_write_unlock(env, child);
742 /* 6b. insert name into parent dir */
744 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
745 (const struct dt_key *)name, th, 1);
749 dt_write_lock(env, parent, 0);
750 /* 7b. increase parent nlink */
751 rc = dt_ref_add(env, parent, th);
752 dt_write_unlock(env, parent);
756 bk->lb_lpf_fid = *cfid;
757 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
759 /* 8b. update bookmark */
760 rc = dt_record_write(env, bk_obj,
761 lfsck_buf_get(env, bk, len), &pos, th);
766 dt_write_unlock(env, child);
769 dt_trans_stop(env, dev, th);
774 static int lfsck_create_lpf_remote(const struct lu_env *env,
775 struct lfsck_instance *lfsck,
776 struct dt_object *child,
778 struct dt_object_format *dof,
781 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
782 struct dt_object *parent = lfsck->li_lpf_root_obj;
783 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
784 struct dt_object *bk_obj = lfsck->li_bookmark_obj;
785 const struct lu_fid *cfid = lfsck_dto2fid(child);
786 struct thandle *th = NULL;
787 struct linkea_data ldata = { NULL };
788 struct lu_buf linkea_buf;
789 const struct lu_name *cname;
790 struct dt_device *dev;
792 int len = sizeof(struct lfsck_bookmark);
796 rc = linkea_data_new(&ldata,
797 &lfsck_env_info(env)->lti_linkea_buf2);
801 cname = lfsck_name_get_const(env, name, strlen(name));
802 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
806 /* Create .lustre/lost+found/MDTxxxx. */
808 /* XXX: Currently, cross-MDT create operation needs to create the child
809 * object firstly, then insert name into the parent directory. For
810 * this case, the child object resides on current MDT (local), but
811 * the parent ".lustre/lost+found" may be on remote MDT. It is not
812 * easy to contain all the sub-modifications orderly within single
815 * To avoid more inconsistency, we split the create operation into
818 * 1) create the child and update the lfsck_bookmark::lb_lpf_fid
820 * 2) insert the name "MDTXXXX" in the parent ".lustre/lost+found"
823 * If 1) done, but 2) failed, then go ahead, the LFSCK will try to
824 * repair such inconsistency when LFSCK run next time. */
826 /* Transaction I: locally */
828 dev = lfsck_obj2dev(child);
829 th = dt_trans_create(env, dev);
833 /* 1a. create child */
834 rc = dt_declare_create(env, child, la, NULL, dof, th);
838 if (!dt_try_as_dir(env, child))
839 GOTO(stop, rc = -ENOTDIR);
841 /* 2a. increase child nlink */
842 rc = dt_declare_ref_add(env, child, th);
846 /* 3a. insert dot into child dir */
847 rec->rec_type = S_IFDIR;
849 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
850 (const struct dt_key *)dot, th);
854 /* 4a. insert dotdot into child dir */
855 rec->rec_fid = &LU_LPF_FID;
856 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
857 (const struct dt_key *)dotdot, th);
861 /* 5a. insert linkEA for child */
862 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
863 ldata.ld_leh->leh_len);
864 rc = dt_declare_xattr_set(env, child, &linkea_buf,
865 XATTR_NAME_LINK, 0, th);
869 /* 6a. update bookmark */
870 rc = dt_declare_record_write(env, bk_obj,
871 lfsck_buf_get(env, bk, len), 0, th);
875 rc = dt_trans_start_local(env, dev, th);
879 dt_write_lock(env, child, 0);
880 /* 1b. create child */
881 rc = dt_create(env, child, la, NULL, dof, th);
885 /* 2b. increase child nlink */
886 rc = dt_ref_add(env, child, th);
890 /* 3b. insert dot into child dir */
891 rec->rec_type = S_IFDIR;
893 rc = dt_insert(env, child, (const struct dt_rec *)rec,
894 (const struct dt_key *)dot, th, 1);
898 /* 4b. insert dotdot into child dir */
899 rec->rec_fid = &LU_LPF_FID;
900 rc = dt_insert(env, child, (const struct dt_rec *)rec,
901 (const struct dt_key *)dotdot, th, 1);
905 /* 5b. insert linkEA for child */
906 rc = dt_xattr_set(env, child, &linkea_buf,
907 XATTR_NAME_LINK, 0, th);
911 bk->lb_lpf_fid = *cfid;
912 lfsck_bookmark_cpu_to_le(&lfsck->li_bookmark_disk, bk);
914 /* 6b. update bookmark */
915 rc = dt_record_write(env, bk_obj,
916 lfsck_buf_get(env, bk, len), &pos, th);
918 dt_write_unlock(env, child);
919 dt_trans_stop(env, dev, th);
923 /* Transaction II: remotely */
925 dev = lfsck_obj2dev(parent);
926 th = dt_trans_create(env, dev);
931 /* 5a. insert name into parent dir */
933 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
934 (const struct dt_key *)name, th);
938 /* 6a. increase parent nlink */
939 rc = dt_declare_ref_add(env, parent, th);
943 rc = dt_trans_start_local(env, dev, th);
947 /* 5b. insert name into parent dir */
948 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
949 (const struct dt_key *)name, th, 1);
953 dt_write_lock(env, parent, 0);
954 /* 6b. increase parent nlink */
955 rc = dt_ref_add(env, parent, th);
956 dt_write_unlock(env, parent);
961 dt_write_unlock(env, child);
963 dt_trans_stop(env, dev, th);
965 if (rc != 0 && dev == lfsck_obj2dev(parent))
966 CDEBUG(D_LFSCK, "%s: partially created the object "DFID
967 "for orphans, but failed to insert the name %s "
968 "to the .lustre/lost+found/. Such inconsistency "
969 "will be repaired when LFSCK run next time: rc = %d\n",
970 lfsck_lfsck2name(lfsck), PFID(cfid), name, rc);
976 * Create the MDTxxxx directory under /ROOT/.lustre/lost+found/
978 * The /ROOT/.lustre/lost+found/MDTxxxx/ directory is used for holding
979 * orphans and other uncertain inconsistent objects found during the
980 * LFSCK. Such directory will be created by the LFSCK engine on the
981 * local MDT before the LFSCK scanning.
983 * \param[in] env pointer to the thread context
984 * \param[in] lfsck pointer to the lfsck instance
986 * \retval 0 for success
987 * \retval negative error number on failure
989 static int lfsck_create_lpf(const struct lu_env *env,
990 struct lfsck_instance *lfsck)
992 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
993 struct lfsck_thread_info *info = lfsck_env_info(env);
994 struct lu_fid *cfid = &info->lti_fid2;
995 struct lu_attr *la = &info->lti_la;
996 struct dt_object_format *dof = &info->lti_dof;
997 struct dt_object *parent = lfsck->li_lpf_root_obj;
998 struct dt_object *child = NULL;
999 struct lfsck_lock_handle *llh = &info->lti_llh;
1001 int node = lfsck_dev_idx(lfsck);
1005 LASSERT(lfsck->li_master);
1006 LASSERT(parent != NULL);
1007 LASSERT(lfsck->li_lpf_obj == NULL);
1009 snprintf(name, 8, "MDT%04x", node);
1010 rc = lfsck_lock(env, lfsck, parent, name, llh,
1011 MDS_INODELOCK_UPDATE, LCK_PW);
1015 if (fid_is_zero(&bk->lb_lpf_fid)) {
1016 /* There is corner case that: in former LFSCK scanning we have
1017 * created the .lustre/lost+found/MDTxxxx but failed to update
1018 * the lfsck_bookmark::lb_lpf_fid successfully. So need lookup
1019 * it from MDT0 firstly. */
1020 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1021 (const struct dt_key *)name);
1022 if (rc != 0 && rc != -ENOENT)
1026 bk->lb_lpf_fid = *cfid;
1027 rc = lfsck_bookmark_store(env, lfsck);
1029 rc = lfsck_fid_alloc(env, lfsck, cfid, true);
1034 *cfid = bk->lb_lpf_fid;
1037 child = lfsck_object_find_bottom(env, lfsck, cfid);
1039 GOTO(unlock, rc = PTR_ERR(child));
1041 if (dt_object_exists(child) != 0) {
1042 if (unlikely(!dt_try_as_dir(env, child)))
1045 lfsck->li_lpf_obj = child;
1050 memset(la, 0, sizeof(*la));
1051 la->la_atime = la->la_mtime = la->la_ctime = cfs_time_current_sec();
1052 la->la_mode = S_IFDIR | S_IRWXU;
1053 la->la_valid = LA_ATIME | LA_MTIME | LA_CTIME | LA_MODE |
1055 memset(dof, 0, sizeof(*dof));
1056 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1059 rc = lfsck_create_lpf_local(env, lfsck, child, la, dof, name);
1061 rc = lfsck_create_lpf_remote(env, lfsck, child, la, dof, name);
1063 lfsck->li_lpf_obj = child;
1069 if (rc != 0 && child != NULL && !IS_ERR(child))
1070 lfsck_object_put(env, child);
1076 * Scan .lustre/lost+found for bad name entries and remove them.
1078 * The valid name entry should be "MDTxxxx", the "xxxx" is the MDT device
1079 * index in the system. Any other formatted name is invalid and should be
1082 * \param[in] env pointer to the thread context
1083 * \param[in] lfsck pointer to the lfsck instance
1085 * \retval 0 for success
1086 * \retval negative error number on failure
1088 static int lfsck_scan_lpf_bad_entries(const struct lu_env *env,
1089 struct lfsck_instance *lfsck)
1091 struct dt_object *parent = lfsck->li_lpf_root_obj;
1092 struct lu_dirent *ent =
1093 (struct lu_dirent *)lfsck_env_info(env)->lti_key;
1094 const struct dt_it_ops *iops = &parent->do_index_ops->dio_it;
1099 it = iops->init(env, parent, LUDA_64BITHASH);
1101 RETURN(PTR_ERR(it));
1103 rc = iops->load(env, it, 0);
1105 rc = iops->next(env, it);
1112 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1116 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1117 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
1120 /* name length must be strlen("MDTxxxx") */
1121 if (ent->lde_namelen != 7)
1124 if (memcmp(ent->lde_name, "MDT", off) != 0)
1127 while (off < 7 && isxdigit(ent->lde_name[off]))
1133 rc = lfsck_lpf_remove_name_entry(env, lfsck,
1140 rc = iops->next(env, it);
1144 iops->fini(env, it);
1146 RETURN(rc > 0 ? 0 : rc);
1149 static int lfsck_update_lpf_entry(const struct lu_env *env,
1150 struct lfsck_instance *lfsck,
1151 struct dt_object *parent,
1152 struct dt_object *child,
1154 enum lfsck_verify_lpf_types type)
1158 if (type == LVLT_BY_BOOKMARK) {
1159 rc = lfsck_update_name_entry(env, lfsck, parent, name,
1160 lfsck_dto2fid(child), S_IFDIR);
1161 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1162 lfsck->li_bookmark_ram.lb_lpf_fid = *lfsck_dto2fid(child);
1163 rc = lfsck_bookmark_store(env, lfsck);
1165 CDEBUG(D_LFSCK, "%s: update LPF fid "DFID
1166 " in the bookmark file: rc = %d\n",
1167 lfsck_lfsck2name(lfsck),
1168 PFID(lfsck_dto2fid(child)), rc);
1175 * Check whether the @child back references the @parent.
1178 * 1) The child's FID is stored in the bookmark file. If the child back
1179 * references the parent (LU_LPF_FID object) via its ".." entry, then
1180 * insert the name (MDTxxxx) to the .lustre/lost+found; otherwise, if
1181 * the child back references another parent2, then:
1182 * 1.1) If the parent2 recognizes the child, then update the bookmark file;
1183 * 1.2) Otherwise, the LFSCK cannot know whether there will be parent3 that
1184 * references the child. So keep them there. As the LFSCK processing,
1185 * the parent3 may be found, then when the LFSCK run next time, the
1186 * inconsistency can be repaired.
1188 * 2) The child's FID is stored in the .lustre/lost+found/ sub-directory name
1189 * entry (MDTxxxx). If the child back references the parent (LU_LPF_FID obj)
1190 * via its ".." entry, then update the bookmark file, otherwise, if the child
1191 * back references another parent2, then:
1192 * 2.1) If the parent2 recognizes the child, then remove the sub-directory
1193 * from .lustre/lost+found/;
1194 * 2.2) Otherwise, if the parent2 does not recognizes the child, trust the
1195 * sub-directory name entry and update the child;
1196 * 2.3) Otherwise, if we do not know whether the parent2 recognizes the child
1197 * or not, then keep them there.
1199 * \param[in] env pointer to the thread context
1200 * \param[in] lfsck pointer to the lfsck instance
1201 * \param[in] child pointer to the lost+found sub-directory object
1202 * \param[in] name the name for lost+found sub-directory object
1203 * \param[out] fid pointer to the buffer to hold the FID of the object
1204 * (called it as parent2) that is referenced via the
1205 * child's dotdot entry; it also can be the FID that
1206 * is referenced by the name entry under the parent2.
1207 * \param[in] type to indicate where the child's FID is stored in
1209 * \retval positive number for uncertain inconsistency
1210 * \retval 0 for success
1211 * \retval negative error number on failure
1213 static int lfsck_verify_lpf_pairs(const struct lu_env *env,
1214 struct lfsck_instance *lfsck,
1215 struct dt_object *child, const char *name,
1217 enum lfsck_verify_lpf_types type)
1219 struct dt_object *parent = lfsck->li_lpf_root_obj;
1220 struct lfsck_thread_info *info = lfsck_env_info(env);
1221 char *name2 = info->lti_key;
1222 struct lu_fid *fid2 = &info->lti_fid3;
1223 struct dt_object *parent2 = NULL;
1224 struct lustre_handle lh = { 0 };
1229 rc = dt_lookup(env, child, (struct dt_rec *)fid,
1230 (const struct dt_key *)dotdot);
1234 if (!fid_is_sane(fid))
1235 GOTO(linkea, rc = -EINVAL);
1237 if (lu_fid_eq(fid, &LU_LPF_FID)) {
1238 const struct lu_name *cname;
1240 if (lfsck->li_lpf_obj == NULL) {
1241 lu_object_get(&child->do_lu);
1242 lfsck->li_lpf_obj = child;
1245 cname = lfsck_name_get_const(env, name, strlen(name));
1246 rc = lfsck_verify_linkea(env, child, cname, &LU_LPF_FID);
1248 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1254 parent2 = lfsck_object_find_bottom(env, lfsck, fid);
1255 if (IS_ERR(parent2))
1256 GOTO(linkea, parent2);
1258 if (!dt_object_exists(parent2)) {
1259 lfsck_object_put(env, parent2);
1261 GOTO(linkea, parent2 = ERR_PTR(-ENOENT));
1264 if (!dt_try_as_dir(env, parent2)) {
1265 lfsck_object_put(env, parent2);
1267 GOTO(linkea, parent2 = ERR_PTR(-ENOTDIR));
1271 /* To prevent rename/unlink race */
1272 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1273 MDS_INODELOCK_UPDATE, LCK_PR);
1277 dt_read_lock(env, child, 0);
1278 rc = lfsck_links_get_first(env, child, name2, fid2);
1280 dt_read_unlock(env, child);
1281 lfsck_ibits_unlock(&lh, LCK_PR);
1283 GOTO(out_put, rc = 1);
1286 /* It is almost impossible that the bookmark file (or the name entry)
1287 * and the linkEA hit the same data corruption. Trust the linkEA. */
1288 if (lu_fid_eq(fid2, &LU_LPF_FID) && strcmp(name, name2) == 0) {
1289 dt_read_unlock(env, child);
1290 lfsck_ibits_unlock(&lh, LCK_PR);
1293 if (lfsck->li_lpf_obj == NULL) {
1294 lu_object_get(&child->do_lu);
1295 lfsck->li_lpf_obj = child;
1298 /* Update the child's dotdot entry */
1299 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1300 &LU_LPF_FID, S_IFDIR);
1302 rc = lfsck_update_lpf_entry(env, lfsck, parent, child,
1308 if (parent2 == NULL || IS_ERR(parent2)) {
1309 dt_read_unlock(env, child);
1310 lfsck_ibits_unlock(&lh, LCK_PR);
1312 GOTO(out_done, rc = 1);
1315 rc = dt_lookup(env, parent2, (struct dt_rec *)fid,
1316 (const struct dt_key *)name2);
1317 dt_read_unlock(env, child);
1318 lfsck_ibits_unlock(&lh, LCK_PR);
1319 if (rc != 0 && rc != -ENOENT)
1322 if (rc == -ENOENT || !lu_fid_eq(fid, lfsck_dto2fid(child))) {
1323 if (type == LVLT_BY_BOOKMARK)
1324 GOTO(out_put, rc = 1);
1326 /* Trust the name entry, update the child's dotdot entry. */
1327 rc = lfsck_update_name_entry(env, lfsck, child, dotdot,
1328 &LU_LPF_FID, S_IFDIR);
1333 if (type == LVLT_BY_BOOKMARK) {
1334 /* Invalid FID record in the bookmark file, reset it. */
1335 fid_zero(&lfsck->li_bookmark_ram.lb_lpf_fid);
1336 rc = lfsck_bookmark_store(env, lfsck);
1338 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1339 " in the bookmark file: rc = %d\n",
1340 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), rc);
1341 } else /* if (type == LVLT_BY_NAMEENTRY) */ {
1342 /* The name entry is wrong, remove it. */
1343 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1349 if (parent2 != NULL && !IS_ERR(parent2))
1350 lfsck_object_put(env, parent2);
1357 * Verify the /ROOT/.lustre/lost+found/ directory.
1359 * /ROOT/.lustre/lost+found/ is a special directory to hold the objects that
1360 * the LFSCK does not exactly know how to handle, such as orphans. So before
1361 * the LFSCK scanning the system, the consistency of such directory needs to
1362 * be verified firstly to allow the users to use it during the LFSCK.
1364 * \param[in] env pointer to the thread context
1365 * \param[in] lfsck pointer to the lfsck instance
1367 * \retval positive number for uncertain inconsistency
1368 * \retval 0 for success
1369 * \retval negative error number on failure
1371 int lfsck_verify_lpf(const struct lu_env *env, struct lfsck_instance *lfsck)
1373 struct lfsck_thread_info *info = lfsck_env_info(env);
1374 struct lu_fid *pfid = &info->lti_fid;
1375 struct lu_fid *cfid = &info->lti_fid2;
1376 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1377 struct dt_object *parent;
1378 /* child1's FID is in the bookmark file. */
1379 struct dt_object *child1 = NULL;
1380 /* child2's FID is in the name entry MDTxxxx. */
1381 struct dt_object *child2 = NULL;
1382 const struct lu_name *cname;
1384 int node = lfsck_dev_idx(lfsck);
1388 LASSERT(lfsck->li_master);
1390 if (lfsck->li_lpf_root_obj != NULL)
1394 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
1397 struct lfsck_tgt_desc *ltd;
1399 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, 0);
1400 if (unlikely(ltd == NULL))
1403 parent = lfsck_object_find_by_dev(env, ltd->ltd_tgt,
1409 RETURN(PTR_ERR(parent));
1411 LASSERT(dt_object_exists(parent));
1413 if (unlikely(!dt_try_as_dir(env, parent))) {
1414 lfsck_object_put(env, parent);
1416 GOTO(put, rc = -ENOTDIR);
1419 lfsck->li_lpf_root_obj = parent;
1421 rc = lfsck_scan_lpf_bad_entries(env, lfsck);
1423 CDEBUG(D_LFSCK, "%s: scan .lustre/lost+found/ "
1424 "for bad sub-directories: rc = %d\n",
1425 lfsck_lfsck2name(lfsck), rc);
1429 snprintf(name, 8, "MDT%04x", node);
1430 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1431 (const struct dt_key *)name);
1432 if (rc == -ENOENT) {
1440 /* Invalid FID in the name entry, remove the name entry. */
1441 if (!fid_is_norm(cfid)) {
1442 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1449 child2 = lfsck_object_find_bottom(env, lfsck, cfid);
1451 GOTO(put, rc = PTR_ERR(child2));
1453 if (unlikely(!dt_object_exists(child2) ||
1454 dt_object_remote(child2)) ||
1455 !S_ISDIR(lfsck_object_type(child2))) {
1456 rc = lfsck_lpf_remove_name_entry(env, lfsck, name);
1463 if (unlikely(!dt_try_as_dir(env, child2))) {
1464 lfsck_object_put(env, child2);
1470 if (fid_is_zero(&bk->lb_lpf_fid))
1473 if (likely(lu_fid_eq(cfid, &bk->lb_lpf_fid))) {
1474 if (lfsck->li_lpf_obj == NULL) {
1475 lu_object_get(&child2->do_lu);
1476 lfsck->li_lpf_obj = child2;
1479 cname = lfsck_name_get_const(env, name, strlen(name));
1480 rc = lfsck_verify_linkea(env, child2, cname, &LU_LPF_FID);
1485 if (unlikely(!fid_is_norm(&bk->lb_lpf_fid))) {
1486 struct lu_fid tfid = bk->lb_lpf_fid;
1488 /* Invalid FID record in the bookmark file, reset it. */
1489 fid_zero(&bk->lb_lpf_fid);
1490 rc = lfsck_bookmark_store(env, lfsck);
1492 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1493 " in the bookmark file: rc = %d\n",
1494 lfsck_lfsck2name(lfsck), PFID(&tfid), rc);
1502 child1 = lfsck_object_find_bottom(env, lfsck, &bk->lb_lpf_fid);
1503 if (IS_ERR(child1)) {
1508 if (unlikely(!dt_object_exists(child1) ||
1509 dt_object_remote(child1)) ||
1510 !S_ISDIR(lfsck_object_type(child1))) {
1511 /* Invalid FID record in the bookmark file, reset it. */
1512 fid_zero(&bk->lb_lpf_fid);
1513 rc = lfsck_bookmark_store(env, lfsck);
1515 CDEBUG(D_LFSCK, "%s: reset invalid LPF fid "DFID
1516 " in the bookmark file: rc = %d\n",
1517 lfsck_lfsck2name(lfsck),
1518 PFID(lfsck_dto2fid(child1)), rc);
1523 lfsck_object_put(env, child1);
1528 if (unlikely(!dt_try_as_dir(env, child1))) {
1529 lfsck_object_put(env, child1);
1535 rc = lfsck_verify_lpf_pairs(env, lfsck, child1, name, pfid,
1537 if (lu_fid_eq(pfid, &LU_LPF_FID))
1542 rc = lfsck_verify_lpf_pairs(env, lfsck, child2, name,
1543 pfid, LVLT_BY_NAMEENTRY);
1548 if (lfsck->li_lpf_obj != NULL) {
1549 if (unlikely(!dt_try_as_dir(env, lfsck->li_lpf_obj))) {
1550 lfsck_object_put(env, lfsck->li_lpf_obj);
1551 lfsck->li_lpf_obj = NULL;
1554 } else if (rc == 0) {
1555 rc = lfsck_create_lpf(env, lfsck);
1558 if (child2 != NULL && !IS_ERR(child2))
1559 lfsck_object_put(env, child2);
1560 if (child1 != NULL && !IS_ERR(child1))
1561 lfsck_object_put(env, child1);
1566 static int lfsck_fid_init(struct lfsck_instance *lfsck)
1568 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1569 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1574 if (unlikely(ss == NULL))
1577 OBD_ALLOC_PTR(lfsck->li_seq);
1578 if (lfsck->li_seq == NULL)
1581 OBD_ALLOC(prefix, MAX_OBD_NAME + 7);
1583 GOTO(out, rc = -ENOMEM);
1585 snprintf(prefix, MAX_OBD_NAME + 7, "lfsck-%s", lfsck_lfsck2name(lfsck));
1586 rc = seq_client_init(lfsck->li_seq, NULL, LUSTRE_SEQ_METADATA, prefix,
1588 OBD_FREE(prefix, MAX_OBD_NAME + 7);
1592 if (fid_is_sane(&bk->lb_last_fid))
1593 lfsck->li_seq->lcs_fid = bk->lb_last_fid;
1598 OBD_FREE_PTR(lfsck->li_seq);
1599 lfsck->li_seq = NULL;
1604 static void lfsck_fid_fini(struct lfsck_instance *lfsck)
1606 if (lfsck->li_seq != NULL) {
1607 seq_client_fini(lfsck->li_seq);
1608 OBD_FREE_PTR(lfsck->li_seq);
1609 lfsck->li_seq = NULL;
1613 void lfsck_instance_cleanup(const struct lu_env *env,
1614 struct lfsck_instance *lfsck)
1616 struct ptlrpc_thread *thread = &lfsck->li_thread;
1617 struct lfsck_component *com;
1618 struct lfsck_component *next;
1619 struct lfsck_lmv_unit *llu;
1620 struct lfsck_lmv_unit *llu_next;
1621 struct lfsck_lmv *llmv;
1624 LASSERT(list_empty(&lfsck->li_link));
1625 LASSERT(thread_is_init(thread) || thread_is_stopped(thread));
1627 if (lfsck->li_obj_oit != NULL) {
1628 lfsck_object_put(env, lfsck->li_obj_oit);
1629 lfsck->li_obj_oit = NULL;
1632 LASSERT(lfsck->li_obj_dir == NULL);
1633 LASSERT(lfsck->li_lmv == NULL);
1635 list_for_each_entry_safe(llu, llu_next, &lfsck->li_list_lmv, llu_link) {
1636 llmv = &llu->llu_lmv;
1638 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
1639 "still in using: %u\n",
1640 atomic_read(&llmv->ll_ref));
1642 lfsck_lmv_put(env, llmv);
1645 list_for_each_entry_safe(com, next, &lfsck->li_list_scan, lc_link) {
1646 lfsck_component_cleanup(env, com);
1649 LASSERT(list_empty(&lfsck->li_list_dir));
1651 list_for_each_entry_safe(com, next, &lfsck->li_list_double_scan,
1653 lfsck_component_cleanup(env, com);
1656 list_for_each_entry_safe(com, next, &lfsck->li_list_idle, lc_link) {
1657 lfsck_component_cleanup(env, com);
1660 lfsck_tgt_descs_fini(&lfsck->li_ost_descs);
1661 lfsck_tgt_descs_fini(&lfsck->li_mdt_descs);
1663 if (lfsck->li_lfsck_dir != NULL) {
1664 lfsck_object_put(env, lfsck->li_lfsck_dir);
1665 lfsck->li_lfsck_dir = NULL;
1668 if (lfsck->li_bookmark_obj != NULL) {
1669 lfsck_object_put(env, lfsck->li_bookmark_obj);
1670 lfsck->li_bookmark_obj = NULL;
1673 if (lfsck->li_lpf_obj != NULL) {
1674 lfsck_object_put(env, lfsck->li_lpf_obj);
1675 lfsck->li_lpf_obj = NULL;
1678 if (lfsck->li_lpf_root_obj != NULL) {
1679 lfsck_object_put(env, lfsck->li_lpf_root_obj);
1680 lfsck->li_lpf_root_obj = NULL;
1683 if (lfsck->li_los != NULL) {
1684 local_oid_storage_fini(env, lfsck->li_los);
1685 lfsck->li_los = NULL;
1688 lfsck_fid_fini(lfsck);
1690 OBD_FREE_PTR(lfsck);
1693 static inline struct lfsck_instance *
1694 __lfsck_instance_find(struct dt_device *key, bool ref, bool unlink)
1696 struct lfsck_instance *lfsck;
1698 list_for_each_entry(lfsck, &lfsck_instance_list, li_link) {
1699 if (lfsck->li_bottom == key) {
1701 lfsck_instance_get(lfsck);
1703 list_del_init(&lfsck->li_link);
1712 struct lfsck_instance *lfsck_instance_find(struct dt_device *key, bool ref,
1715 struct lfsck_instance *lfsck;
1717 spin_lock(&lfsck_instance_lock);
1718 lfsck = __lfsck_instance_find(key, ref, unlink);
1719 spin_unlock(&lfsck_instance_lock);
1724 static inline int lfsck_instance_add(struct lfsck_instance *lfsck)
1726 struct lfsck_instance *tmp;
1728 spin_lock(&lfsck_instance_lock);
1729 list_for_each_entry(tmp, &lfsck_instance_list, li_link) {
1730 if (lfsck->li_bottom == tmp->li_bottom) {
1731 spin_unlock(&lfsck_instance_lock);
1736 list_add_tail(&lfsck->li_link, &lfsck_instance_list);
1737 spin_unlock(&lfsck_instance_lock);
1741 void lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1746 bool newline = (bits != 0 ? false : true);
1748 seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1750 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1753 if (names[i] != NULL) {
1757 seq_printf(m, "%s%c", names[i],
1758 newline ? '\n' : ',');
1767 void lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1770 seq_printf(m, "%s_time: N/A\n", name);
1771 seq_printf(m, "time_since_%s: N/A\n", name);
1773 seq_printf(m, "%s_time: "LPU64"\n", name, time);
1774 seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1775 name, cfs_time_current_sec() - time);
1779 void lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1782 if (fid_is_zero(&pos->lp_dir_parent)) {
1783 if (pos->lp_oit_cookie == 0) {
1784 seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1787 seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1788 prefix, pos->lp_oit_cookie);
1790 seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1791 prefix, pos->lp_oit_cookie,
1792 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1796 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1797 struct lfsck_position *pos, bool init)
1799 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1801 if (unlikely(lfsck->li_di_oit == NULL)) {
1802 memset(pos, 0, sizeof(*pos));
1806 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1807 if (!lfsck->li_current_oit_processed && !init)
1808 pos->lp_oit_cookie--;
1810 LASSERT(pos->lp_oit_cookie > 0);
1812 if (lfsck->li_di_dir != NULL) {
1813 struct dt_object *dto = lfsck->li_obj_dir;
1815 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1818 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1819 fid_zero(&pos->lp_dir_parent);
1820 pos->lp_dir_cookie = 0;
1822 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1825 fid_zero(&pos->lp_dir_parent);
1826 pos->lp_dir_cookie = 0;
1830 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1834 if (limit != LFSCK_SPEED_NO_LIMIT) {
1835 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1836 lfsck->li_sleep_rate = limit /
1837 msecs_to_jiffies(MSEC_PER_SEC);
1838 lfsck->li_sleep_jif = 1;
1840 lfsck->li_sleep_rate = 1;
1841 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1845 lfsck->li_sleep_jif = 0;
1846 lfsck->li_sleep_rate = 0;
1849 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1850 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1857 void lfsck_control_speed(struct lfsck_instance *lfsck)
1859 struct ptlrpc_thread *thread = &lfsck->li_thread;
1860 struct l_wait_info lwi;
1862 if (lfsck->li_sleep_jif > 0 &&
1863 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1864 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1865 LWI_ON_SIGNAL_NOOP, NULL);
1867 l_wait_event(thread->t_ctl_waitq,
1868 !thread_is_running(thread),
1870 lfsck->li_new_scanned = 0;
1874 void lfsck_control_speed_by_self(struct lfsck_component *com)
1876 struct lfsck_instance *lfsck = com->lc_lfsck;
1877 struct ptlrpc_thread *thread = &lfsck->li_thread;
1878 struct l_wait_info lwi;
1880 if (lfsck->li_sleep_jif > 0 &&
1881 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1882 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1883 LWI_ON_SIGNAL_NOOP, NULL);
1885 l_wait_event(thread->t_ctl_waitq,
1886 !thread_is_running(thread),
1888 com->lc_new_scanned = 0;
1892 static struct lfsck_thread_args *
1893 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1894 struct lfsck_component *com,
1895 struct lfsck_start_param *lsp)
1897 struct lfsck_thread_args *lta;
1902 return ERR_PTR(-ENOMEM);
1904 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1910 lta->lta_lfsck = lfsck_instance_get(lfsck);
1912 lta->lta_com = lfsck_component_get(com);
1919 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1921 if (lta->lta_com != NULL)
1922 lfsck_component_put(<a->lta_env, lta->lta_com);
1923 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1924 lu_env_fini(<a->lta_env);
1928 struct lfsck_assistant_data *
1929 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1932 struct lfsck_assistant_data *lad;
1936 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1937 if (lad->lad_bitmap == NULL) {
1942 INIT_LIST_HEAD(&lad->lad_req_list);
1943 spin_lock_init(&lad->lad_lock);
1944 INIT_LIST_HEAD(&lad->lad_ost_list);
1945 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1946 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1947 INIT_LIST_HEAD(&lad->lad_mdt_list);
1948 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1949 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1950 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1952 lad->lad_name = name;
1958 struct lfsck_assistant_object *
1959 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1960 const struct lu_attr *attr, __u64 cookie,
1963 struct lfsck_assistant_object *lso;
1967 return ERR_PTR(-ENOMEM);
1969 lso->lso_fid = *fid;
1971 lso->lso_attr = *attr;
1973 atomic_set(&lso->lso_ref, 1);
1974 lso->lso_oit_cookie = cookie;
1976 lso->lso_is_dir = 1;
1982 lfsck_assistant_object_load(const struct lu_env *env,
1983 struct lfsck_instance *lfsck,
1984 struct lfsck_assistant_object *lso)
1986 struct dt_object *obj;
1988 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
1992 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
1994 lfsck_object_put(env, obj);
1996 return ERR_PTR(-ENOENT);
1999 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2000 lfsck_object_put(env, obj);
2002 return ERR_PTR(-ENOTDIR);
2009 * Generic LFSCK asynchronous communication interpretor function.
2010 * The LFSCK RPC reply for both the event notification and status
2011 * querying will be handled here.
2013 * \param[in] env pointer to the thread context
2014 * \param[in] req pointer to the LFSCK request
2015 * \param[in] args pointer to the lfsck_async_interpret_args
2016 * \param[in] rc the result for handling the LFSCK request
2018 * \retval 0 for success
2019 * \retval negative error number on failure
2021 int lfsck_async_interpret_common(const struct lu_env *env,
2022 struct ptlrpc_request *req,
2025 struct lfsck_async_interpret_args *laia = args;
2026 struct lfsck_component *com = laia->laia_com;
2027 struct lfsck_assistant_data *lad = com->lc_data;
2028 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2029 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2030 struct lfsck_request *lr = laia->laia_lr;
2032 LASSERT(com->lc_lfsck->li_master);
2034 switch (lr->lr_event) {
2037 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2039 lfsck_lfsck2name(com->lc_lfsck),
2040 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2041 ltd->ltd_index, lad->lad_name, rc);
2043 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2044 struct lfsck_layout *lo = com->lc_file_ram;
2046 if (lr->lr_flags & LEF_TO_OST)
2047 lfsck_lad_set_bitmap(env, com,
2050 lo->ll_flags |= LF_INCOMPLETE;
2052 struct lfsck_namespace *ns = com->lc_file_ram;
2054 /* If some MDT does not join the namespace
2055 * LFSCK, then we cannot know whether there
2056 * is some name entry on such MDT that with
2057 * the referenced MDT-object on this MDT or
2058 * not. So the namespace LFSCK on this MDT
2059 * cannot handle orphan MDT-objects properly.
2060 * So we mark the LFSCK as LF_INCOMPLETE and
2061 * skip orphan MDT-objects handling. */
2062 ns->ln_flags |= LF_INCOMPLETE;
2067 spin_lock(<ds->ltd_lock);
2068 if (ltd->ltd_dead) {
2069 spin_unlock(<ds->ltd_lock);
2073 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2074 struct list_head *list;
2075 struct list_head *phase_list;
2077 if (ltd->ltd_layout_done) {
2078 spin_unlock(<ds->ltd_lock);
2082 if (lr->lr_flags & LEF_TO_OST) {
2083 list = &lad->lad_ost_list;
2084 phase_list = &lad->lad_ost_phase1_list;
2086 list = &lad->lad_mdt_list;
2087 phase_list = &lad->lad_mdt_phase1_list;
2090 if (list_empty(<d->ltd_layout_list))
2091 list_add_tail(<d->ltd_layout_list, list);
2092 if (list_empty(<d->ltd_layout_phase_list))
2093 list_add_tail(<d->ltd_layout_phase_list,
2096 if (ltd->ltd_namespace_done) {
2097 spin_unlock(<ds->ltd_lock);
2101 if (list_empty(<d->ltd_namespace_list))
2102 list_add_tail(<d->ltd_namespace_list,
2103 &lad->lad_mdt_list);
2104 if (list_empty(<d->ltd_namespace_phase_list))
2105 list_add_tail(<d->ltd_namespace_phase_list,
2106 &lad->lad_mdt_phase1_list);
2108 spin_unlock(<ds->ltd_lock);
2111 case LE_PHASE1_DONE:
2112 case LE_PHASE2_DONE:
2114 if (rc != 0 && rc != -EALREADY)
2115 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2116 "event = %d, rc = %d\n",
2117 lfsck_lfsck2name(com->lc_lfsck),
2118 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2119 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2122 struct lfsck_reply *reply;
2123 struct list_head *list;
2124 struct list_head *phase_list;
2126 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2127 list = <d->ltd_layout_list;
2128 phase_list = <d->ltd_layout_phase_list;
2130 list = <d->ltd_namespace_list;
2131 phase_list = <d->ltd_namespace_phase_list;
2135 if (lr->lr_flags & LEF_QUERY_ALL) {
2136 lfsck_reset_ltd_status(ltd, com->lc_type);
2140 spin_lock(<ds->ltd_lock);
2141 list_del_init(phase_list);
2142 list_del_init(list);
2143 spin_unlock(<ds->ltd_lock);
2147 reply = req_capsule_server_get(&req->rq_pill,
2149 if (reply == NULL) {
2151 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2152 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2155 if (lr->lr_flags & LEF_QUERY_ALL) {
2156 lfsck_reset_ltd_status(ltd, com->lc_type);
2160 spin_lock(<ds->ltd_lock);
2161 list_del_init(phase_list);
2162 list_del_init(list);
2163 spin_unlock(<ds->ltd_lock);
2167 if (lr->lr_flags & LEF_QUERY_ALL) {
2168 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2169 ltd->ltd_layout_status = reply->lr_status;
2170 ltd->ltd_layout_repaired = reply->lr_repaired;
2172 ltd->ltd_namespace_status = reply->lr_status;
2173 ltd->ltd_namespace_repaired =
2179 switch (reply->lr_status) {
2180 case LS_SCANNING_PHASE1:
2182 case LS_SCANNING_PHASE2:
2183 spin_lock(<ds->ltd_lock);
2184 list_del_init(phase_list);
2185 if (ltd->ltd_dead) {
2186 spin_unlock(<ds->ltd_lock);
2190 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2191 if (ltd->ltd_layout_done) {
2192 spin_unlock(<ds->ltd_lock);
2196 if (lr->lr_flags & LEF_TO_OST)
2197 list_add_tail(phase_list,
2198 &lad->lad_ost_phase2_list);
2200 list_add_tail(phase_list,
2201 &lad->lad_mdt_phase2_list);
2203 if (ltd->ltd_namespace_done) {
2204 spin_unlock(<ds->ltd_lock);
2208 list_add_tail(phase_list,
2209 &lad->lad_mdt_phase2_list);
2211 spin_unlock(<ds->ltd_lock);
2214 spin_lock(<ds->ltd_lock);
2215 list_del_init(phase_list);
2216 list_del_init(list);
2217 spin_unlock(<ds->ltd_lock);
2223 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2224 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2228 if (!laia->laia_shared) {
2230 lfsck_component_put(env, com);
2236 static void lfsck_interpret(const struct lu_env *env,
2237 struct lfsck_instance *lfsck,
2238 struct ptlrpc_request *req, void *args, int result)
2240 struct lfsck_async_interpret_args *laia = args;
2241 struct lfsck_component *com;
2243 LASSERT(laia->laia_com == NULL);
2244 LASSERT(laia->laia_shared);
2246 spin_lock(&lfsck->li_lock);
2247 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2248 laia->laia_com = com;
2249 lfsck_async_interpret_common(env, req, laia, result);
2252 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2253 laia->laia_com = com;
2254 lfsck_async_interpret_common(env, req, laia, result);
2256 spin_unlock(&lfsck->li_lock);
2259 static int lfsck_stop_notify(const struct lu_env *env,
2260 struct lfsck_instance *lfsck,
2261 struct lfsck_tgt_descs *ltds,
2262 struct lfsck_tgt_desc *ltd, __u16 type)
2264 struct lfsck_component *com;
2268 LASSERT(lfsck->li_master);
2270 spin_lock(&lfsck->li_lock);
2271 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2273 com = __lfsck_component_find(lfsck, type,
2274 &lfsck->li_list_double_scan);
2276 lfsck_component_get(com);
2277 spin_unlock(&lfsck->li_lock);
2280 struct lfsck_thread_info *info = lfsck_env_info(env);
2281 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2282 struct lfsck_request *lr = &info->lti_lr;
2283 struct lfsck_assistant_data *lad = com->lc_data;
2284 struct list_head *list;
2285 struct list_head *phase_list;
2286 struct ptlrpc_request_set *set;
2288 set = ptlrpc_prep_set();
2290 lfsck_component_put(env, com);
2295 if (type == LFSCK_TYPE_LAYOUT) {
2296 list = <d->ltd_layout_list;
2297 phase_list = <d->ltd_layout_phase_list;
2299 list = <d->ltd_namespace_list;
2300 phase_list = <d->ltd_namespace_phase_list;
2303 spin_lock(<ds->ltd_lock);
2304 if (list_empty(list)) {
2305 LASSERT(list_empty(phase_list));
2306 spin_unlock(<ds->ltd_lock);
2307 ptlrpc_set_destroy(set);
2312 list_del_init(phase_list);
2313 list_del_init(list);
2314 spin_unlock(<ds->ltd_lock);
2316 memset(lr, 0, sizeof(*lr));
2317 lr->lr_index = lfsck_dev_idx(lfsck);
2318 lr->lr_event = LE_PEER_EXIT;
2319 lr->lr_active = type;
2320 lr->lr_status = LS_CO_PAUSED;
2321 if (ltds == &lfsck->li_ost_descs)
2322 lr->lr_flags = LEF_TO_OST;
2324 memset(laia, 0, sizeof(*laia));
2325 laia->laia_com = com;
2326 laia->laia_ltds = ltds;
2327 atomic_inc(<d->ltd_ref);
2328 laia->laia_ltd = ltd;
2331 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2332 lfsck_async_interpret_common,
2333 laia, LFSCK_NOTIFY);
2335 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2336 "co-stop for %s: rc = %d\n",
2337 lfsck_lfsck2name(lfsck),
2338 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2339 ltd->ltd_index, lad->lad_name, rc);
2342 rc = ptlrpc_set_wait(set);
2345 ptlrpc_set_destroy(set);
2346 lfsck_component_put(env, com);
2352 static int lfsck_async_interpret(const struct lu_env *env,
2353 struct ptlrpc_request *req,
2356 struct lfsck_async_interpret_args *laia = args;
2357 struct lfsck_instance *lfsck;
2359 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2361 lfsck_interpret(env, lfsck, req, laia, rc);
2362 lfsck_tgt_put(laia->laia_ltd);
2363 if (rc != 0 && laia->laia_result != -EALREADY)
2364 laia->laia_result = rc;
2369 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2370 struct lfsck_request *lr,
2371 struct ptlrpc_request_set *set,
2372 ptlrpc_interpterer_t interpreter,
2373 void *args, int request)
2375 struct lfsck_async_interpret_args *laia;
2376 struct ptlrpc_request *req;
2377 struct lfsck_request *tmp;
2378 struct req_format *format;
2383 format = &RQF_LFSCK_NOTIFY;
2386 format = &RQF_LFSCK_QUERY;
2389 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2390 exp->exp_obd->obd_name, request, -EINVAL);
2394 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2398 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2400 ptlrpc_request_free(req);
2405 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2407 ptlrpc_request_set_replen(req);
2409 laia = ptlrpc_req_async_args(req);
2410 *laia = *(struct lfsck_async_interpret_args *)args;
2411 if (laia->laia_com != NULL)
2412 lfsck_component_get(laia->laia_com);
2413 req->rq_interpret_reply = interpreter;
2414 req->rq_allow_intr = 1;
2415 ptlrpc_set_add_req(set, req);
2420 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2422 struct lfsck_thread_info *info = lfsck_env_info(env);
2423 struct lfsck_request *lr = &info->lti_lr;
2424 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2425 struct lfsck_instance *lfsck = com->lc_lfsck;
2426 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2427 struct lfsck_tgt_desc *ltd;
2428 struct ptlrpc_request_set *set;
2433 memset(lr, 0, sizeof(*lr));
2434 lr->lr_event = LE_QUERY;
2435 lr->lr_active = com->lc_type;
2436 lr->lr_flags = LEF_QUERY_ALL;
2438 memset(laia, 0, sizeof(*laia));
2439 laia->laia_com = com;
2442 set = ptlrpc_prep_set();
2447 laia->laia_ltds = ltds;
2448 down_read(<ds->ltd_rw_sem);
2449 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2450 ltd = lfsck_tgt_get(ltds, idx);
2451 LASSERT(ltd != NULL);
2453 laia->laia_ltd = ltd;
2454 up_read(<ds->ltd_rw_sem);
2455 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2456 lfsck_async_interpret_common,
2459 struct lfsck_assistant_data *lad = com->lc_data;
2461 CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2462 "rc = %d\n", lfsck_lfsck2name(lfsck),
2463 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2464 ltd->ltd_index, lad->lad_name, rc);
2465 lfsck_reset_ltd_status(ltd, com->lc_type);
2468 down_read(<ds->ltd_rw_sem);
2470 up_read(<ds->ltd_rw_sem);
2472 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2473 ltds = &lfsck->li_ost_descs;
2474 lr->lr_flags |= LEF_TO_OST;
2478 rc = ptlrpc_set_wait(set);
2479 ptlrpc_set_destroy(set);
2484 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2485 struct lfsck_start_param *lsp)
2487 struct lfsck_instance *lfsck = com->lc_lfsck;
2488 struct lfsck_assistant_data *lad = com->lc_data;
2489 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2490 struct ptlrpc_thread *athread = &lad->lad_thread;
2491 struct lfsck_thread_args *lta;
2492 struct task_struct *task;
2496 lad->lad_assistant_status = 0;
2497 lad->lad_post_result = 0;
2498 lad->lad_to_post = 0;
2499 lad->lad_to_double_scan = 0;
2500 lad->lad_in_double_scan = 0;
2502 lad->lad_advance_lock = false;
2503 thread_set_flags(athread, 0);
2505 lta = lfsck_thread_args_init(lfsck, com, lsp);
2507 RETURN(PTR_ERR(lta));
2509 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2512 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2513 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2514 lfsck_thread_args_fini(lta);
2516 struct l_wait_info lwi = { 0 };
2518 l_wait_event(mthread->t_ctl_waitq,
2519 thread_is_running(athread) ||
2520 thread_is_stopped(athread),
2522 if (unlikely(!thread_is_running(athread)))
2523 rc = lad->lad_assistant_status;
2531 int lfsck_checkpoint_generic(const struct lu_env *env,
2532 struct lfsck_component *com)
2534 struct lfsck_assistant_data *lad = com->lc_data;
2535 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2536 struct ptlrpc_thread *athread = &lad->lad_thread;
2537 struct l_wait_info lwi = { 0 };
2539 l_wait_event(mthread->t_ctl_waitq,
2540 list_empty(&lad->lad_req_list) ||
2541 !thread_is_running(mthread) ||
2542 thread_is_stopped(athread),
2545 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2546 return LFSCK_CHECKPOINT_SKIP;
2551 void lfsck_post_generic(const struct lu_env *env,
2552 struct lfsck_component *com, int *result)
2554 struct lfsck_assistant_data *lad = com->lc_data;
2555 struct ptlrpc_thread *athread = &lad->lad_thread;
2556 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2557 struct l_wait_info lwi = { 0 };
2559 lad->lad_post_result = *result;
2562 lad->lad_to_post = 1;
2564 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2565 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2567 wake_up_all(&athread->t_ctl_waitq);
2568 l_wait_event(mthread->t_ctl_waitq,
2569 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2570 thread_is_stopped(athread),
2573 if (lad->lad_assistant_status < 0)
2574 *result = lad->lad_assistant_status;
2576 CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2577 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2580 int lfsck_double_scan_generic(const struct lu_env *env,
2581 struct lfsck_component *com, int status)
2583 struct lfsck_assistant_data *lad = com->lc_data;
2584 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2585 struct ptlrpc_thread *athread = &lad->lad_thread;
2586 struct l_wait_info lwi = { 0 };
2588 if (status != LS_SCANNING_PHASE2)
2591 lad->lad_to_double_scan = 1;
2593 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2595 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2597 wake_up_all(&athread->t_ctl_waitq);
2598 l_wait_event(mthread->t_ctl_waitq,
2599 lad->lad_in_double_scan ||
2600 thread_is_stopped(athread),
2603 CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2604 "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2605 lad->lad_assistant_status);
2607 if (lad->lad_assistant_status < 0)
2608 return lad->lad_assistant_status;
2613 void lfsck_quit_generic(const struct lu_env *env,
2614 struct lfsck_component *com)
2616 struct lfsck_assistant_data *lad = com->lc_data;
2617 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2618 struct ptlrpc_thread *athread = &lad->lad_thread;
2619 struct l_wait_info lwi = { 0 };
2622 wake_up_all(&athread->t_ctl_waitq);
2623 l_wait_event(mthread->t_ctl_waitq,
2624 thread_is_init(athread) ||
2625 thread_is_stopped(athread),
2629 /* external interfaces */
2631 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2634 struct lfsck_instance *lfsck;
2638 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2642 lfsck = lfsck_instance_find(key, true, false);
2643 if (likely(lfsck != NULL)) {
2644 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2645 lfsck_instance_put(&env, lfsck);
2654 EXPORT_SYMBOL(lfsck_get_speed);
2656 int lfsck_set_speed(struct dt_device *key, __u32 val)
2659 struct lfsck_instance *lfsck;
2663 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2667 lfsck = lfsck_instance_find(key, true, false);
2668 if (likely(lfsck != NULL)) {
2669 mutex_lock(&lfsck->li_mutex);
2670 if (__lfsck_set_speed(lfsck, val))
2671 rc = lfsck_bookmark_store(&env, lfsck);
2672 mutex_unlock(&lfsck->li_mutex);
2673 lfsck_instance_put(&env, lfsck);
2682 EXPORT_SYMBOL(lfsck_set_speed);
2684 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2687 struct lfsck_instance *lfsck;
2691 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2695 lfsck = lfsck_instance_find(key, true, false);
2696 if (likely(lfsck != NULL)) {
2697 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2698 lfsck_instance_put(&env, lfsck);
2707 EXPORT_SYMBOL(lfsck_get_windows);
2709 int lfsck_set_windows(struct dt_device *key, int val)
2712 struct lfsck_instance *lfsck;
2716 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2720 lfsck = lfsck_instance_find(key, true, false);
2721 if (likely(lfsck != NULL)) {
2722 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2723 CWARN("%s: invalid async windows size that may "
2724 "cause memory issues. The valid range is "
2726 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2728 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2729 mutex_lock(&lfsck->li_mutex);
2730 lfsck->li_bookmark_ram.lb_async_windows = val;
2731 rc = lfsck_bookmark_store(&env, lfsck);
2732 mutex_unlock(&lfsck->li_mutex);
2734 lfsck_instance_put(&env, lfsck);
2743 EXPORT_SYMBOL(lfsck_set_windows);
2745 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2748 struct lfsck_instance *lfsck;
2749 struct lfsck_component *com;
2753 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2757 lfsck = lfsck_instance_find(key, true, false);
2758 if (likely(lfsck != NULL)) {
2759 com = lfsck_component_find(lfsck, type);
2760 if (likely(com != NULL)) {
2761 com->lc_ops->lfsck_dump(&env, com, m);
2762 lfsck_component_put(&env, com);
2767 lfsck_instance_put(&env, lfsck);
2776 EXPORT_SYMBOL(lfsck_dump);
2778 static int lfsck_stop_all(const struct lu_env *env,
2779 struct lfsck_instance *lfsck,
2780 struct lfsck_stop *stop)
2782 struct lfsck_thread_info *info = lfsck_env_info(env);
2783 struct lfsck_request *lr = &info->lti_lr;
2784 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2785 struct ptlrpc_request_set *set;
2786 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2787 struct lfsck_tgt_desc *ltd;
2788 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2794 LASSERT(stop->ls_flags & LPF_BROADCAST);
2796 set = ptlrpc_prep_set();
2797 if (unlikely(set == NULL))
2800 memset(lr, 0, sizeof(*lr));
2801 lr->lr_event = LE_STOP;
2802 lr->lr_index = lfsck_dev_idx(lfsck);
2803 lr->lr_status = stop->ls_status;
2804 lr->lr_version = bk->lb_version;
2805 lr->lr_active = LFSCK_TYPES_ALL;
2806 lr->lr_param = stop->ls_flags;
2808 memset(laia, 0, sizeof(*laia));
2809 laia->laia_ltds = ltds;
2811 laia->laia_shared = 1;
2813 down_read(<ds->ltd_rw_sem);
2814 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2815 ltd = lfsck_tgt_get(ltds, idx);
2816 LASSERT(ltd != NULL);
2818 laia->laia_ltd = ltd;
2819 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2820 lfsck_async_interpret, laia,
2823 lfsck_interpret(env, lfsck, NULL, laia, rc);
2825 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2826 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2830 up_read(<ds->ltd_rw_sem);
2832 rc = ptlrpc_set_wait(set);
2833 ptlrpc_set_destroy(set);
2836 rc = laia->laia_result;
2838 if (rc == -EALREADY)
2842 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2843 lfsck_lfsck2name(lfsck), rc);
2845 RETURN(rc != 0 ? rc : rc1);
2848 static int lfsck_start_all(const struct lu_env *env,
2849 struct lfsck_instance *lfsck,
2850 struct lfsck_start *start)
2852 struct lfsck_thread_info *info = lfsck_env_info(env);
2853 struct lfsck_request *lr = &info->lti_lr;
2854 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2855 struct ptlrpc_request_set *set;
2856 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2857 struct lfsck_tgt_desc *ltd;
2858 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2863 LASSERT(start->ls_flags & LPF_BROADCAST);
2865 set = ptlrpc_prep_set();
2866 if (unlikely(set == NULL))
2869 memset(lr, 0, sizeof(*lr));
2870 lr->lr_event = LE_START;
2871 lr->lr_index = lfsck_dev_idx(lfsck);
2872 lr->lr_speed = bk->lb_speed_limit;
2873 lr->lr_version = bk->lb_version;
2874 lr->lr_active = start->ls_active;
2875 lr->lr_param = start->ls_flags;
2876 lr->lr_async_windows = bk->lb_async_windows;
2877 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2878 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2881 memset(laia, 0, sizeof(*laia));
2882 laia->laia_ltds = ltds;
2884 laia->laia_shared = 1;
2886 down_read(<ds->ltd_rw_sem);
2887 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2888 ltd = lfsck_tgt_get(ltds, idx);
2889 LASSERT(ltd != NULL);
2891 laia->laia_ltd = ltd;
2892 ltd->ltd_layout_done = 0;
2893 ltd->ltd_namespace_done = 0;
2894 ltd->ltd_synced_failures = 0;
2895 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2896 lfsck_async_interpret, laia,
2899 lfsck_interpret(env, lfsck, NULL, laia, rc);
2901 CERROR("%s: cannot notify MDT %x for LFSCK "
2902 "start, failout: rc = %d\n",
2903 lfsck_lfsck2name(lfsck), idx, rc);
2907 up_read(<ds->ltd_rw_sem);
2910 ptlrpc_set_destroy(set);
2915 rc = ptlrpc_set_wait(set);
2916 ptlrpc_set_destroy(set);
2919 rc = laia->laia_result;
2922 struct lfsck_stop *stop = &info->lti_stop;
2924 CERROR("%s: cannot start LFSCK on some MDTs, "
2925 "stop all: rc = %d\n",
2926 lfsck_lfsck2name(lfsck), rc);
2927 if (rc != -EALREADY) {
2928 stop->ls_status = LS_FAILED;
2929 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2930 lfsck_stop_all(env, lfsck, stop);
2937 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2938 struct lfsck_start_param *lsp)
2940 struct lfsck_start *start = lsp->lsp_start;
2941 struct lfsck_instance *lfsck;
2942 struct lfsck_bookmark *bk;
2943 struct ptlrpc_thread *thread;
2944 struct lfsck_component *com;
2945 struct l_wait_info lwi = { 0 };
2946 struct lfsck_thread_args *lta;
2947 struct task_struct *task;
2948 struct lfsck_tgt_descs *ltds;
2949 struct lfsck_tgt_desc *ltd;
2957 lfsck = lfsck_instance_find(key, true, false);
2958 if (unlikely(lfsck == NULL))
2961 /* System is not ready, try again later. */
2962 if (unlikely(lfsck->li_namespace == NULL))
2963 GOTO(put, rc = -EAGAIN);
2965 /* start == NULL means auto trigger paused LFSCK. */
2966 if ((start == NULL) &&
2967 (list_empty(&lfsck->li_list_scan) ||
2968 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2971 bk = &lfsck->li_bookmark_ram;
2972 thread = &lfsck->li_thread;
2973 mutex_lock(&lfsck->li_mutex);
2974 spin_lock(&lfsck->li_lock);
2975 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2977 if (unlikely(start == NULL)) {
2978 spin_unlock(&lfsck->li_lock);
2982 while (start->ls_active != 0) {
2983 if (!(type & start->ls_active)) {
2988 com = __lfsck_component_find(lfsck, type,
2989 &lfsck->li_list_scan);
2991 com = __lfsck_component_find(lfsck, type,
2992 &lfsck->li_list_double_scan);
2998 if (com->lc_ops->lfsck_join != NULL) {
2999 rc = com->lc_ops->lfsck_join( env, com, lsp);
3000 if (rc != 0 && rc != -EALREADY)
3003 start->ls_active &= ~type;
3006 spin_unlock(&lfsck->li_lock);
3009 spin_unlock(&lfsck->li_lock);
3011 lfsck->li_status = 0;
3012 lfsck->li_oit_over = 0;
3013 lfsck->li_start_unplug = 0;
3014 lfsck->li_drop_dryrun = 0;
3015 lfsck->li_new_scanned = 0;
3017 /* For auto trigger. */
3021 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3022 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3023 lfsck_lfsck2name(lfsck));
3025 GOTO(out, rc = -EPERM);
3028 start->ls_version = bk->lb_version;
3030 if (start->ls_active != 0) {
3031 struct lfsck_component *next;
3033 if (start->ls_active == LFSCK_TYPES_ALL)
3034 start->ls_active = LFSCK_TYPES_SUPPORTED;
3036 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3037 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3038 GOTO(out, rc = -ENOTSUPP);
3041 list_for_each_entry_safe(com, next,
3042 &lfsck->li_list_scan, lc_link) {
3043 if (!(com->lc_type & start->ls_active)) {
3044 rc = com->lc_ops->lfsck_post(env, com, 0,
3051 while (start->ls_active != 0) {
3052 if (type & start->ls_active) {
3053 com = __lfsck_component_find(lfsck, type,
3054 &lfsck->li_list_idle);
3056 /* The component status will be updated
3057 * when its prep() is called later by
3058 * the LFSCK main engine. */
3059 list_move_tail(&com->lc_link,
3060 &lfsck->li_list_scan);
3061 start->ls_active &= ~type;
3067 if (list_empty(&lfsck->li_list_scan)) {
3068 /* The speed limit will be used to control both the LFSCK and
3069 * low layer scrub (if applied), need to be handled firstly. */
3070 if (start->ls_valid & LSV_SPEED_LIMIT) {
3071 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3072 rc = lfsck_bookmark_store(env, lfsck);
3081 if (start->ls_flags & LPF_RESET)
3082 flags |= DOIF_RESET;
3084 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3088 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3089 start->ls_active |= com->lc_type;
3090 if (flags & DOIF_RESET) {
3091 rc = com->lc_ops->lfsck_reset(env, com, false);
3097 ltds = &lfsck->li_mdt_descs;
3098 down_read(<ds->ltd_rw_sem);
3099 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3100 ltd = lfsck_ltd2tgt(ltds, idx);
3101 LASSERT(ltd != NULL);
3103 ltd->ltd_layout_done = 0;
3104 ltd->ltd_namespace_done = 0;
3105 ltd->ltd_synced_failures = 0;
3106 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3107 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3108 list_del_init(<d->ltd_layout_phase_list);
3109 list_del_init(<d->ltd_layout_list);
3110 list_del_init(<d->ltd_namespace_phase_list);
3111 list_del_init(<d->ltd_namespace_list);
3113 up_read(<ds->ltd_rw_sem);
3115 ltds = &lfsck->li_ost_descs;
3116 down_read(<ds->ltd_rw_sem);
3117 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3118 ltd = lfsck_ltd2tgt(ltds, idx);
3119 LASSERT(ltd != NULL);
3121 ltd->ltd_layout_done = 0;
3122 ltd->ltd_synced_failures = 0;
3123 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3124 list_del_init(<d->ltd_layout_phase_list);
3125 list_del_init(<d->ltd_layout_list);
3127 up_read(<ds->ltd_rw_sem);
3130 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3131 if (bk->lb_param & LPF_DRYRUN)
3132 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3134 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3135 valid |= DOIV_ERROR_HANDLE;
3136 if (start->ls_flags & LPF_FAILOUT)
3137 flags |= DOIF_FAILOUT;
3140 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3141 valid |= DOIV_DRYRUN;
3142 if (start->ls_flags & LPF_DRYRUN)
3143 flags |= DOIF_DRYRUN;
3146 if (!list_empty(&lfsck->li_list_scan))
3147 flags |= DOIF_OUTUSED;
3149 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3150 thread_set_flags(thread, 0);
3151 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3153 GOTO(out, rc = PTR_ERR(lta));
3155 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3156 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3159 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3160 lfsck_lfsck2name(lfsck), rc);
3161 lfsck_thread_args_fini(lta);
3166 l_wait_event(thread->t_ctl_waitq,
3167 thread_is_running(thread) ||
3168 thread_is_stopped(thread),
3170 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3171 lfsck->li_start_unplug = 1;
3172 wake_up_all(&thread->t_ctl_waitq);
3177 /* release lfsck::li_mutex to avoid deadlock. */
3178 mutex_unlock(&lfsck->li_mutex);
3179 rc = lfsck_start_all(env, lfsck, start);
3181 spin_lock(&lfsck->li_lock);
3182 if (thread_is_stopped(thread)) {
3183 spin_unlock(&lfsck->li_lock);
3185 lfsck->li_status = LS_FAILED;
3186 lfsck->li_flags = 0;
3187 thread_set_flags(thread, SVC_STOPPING);
3188 spin_unlock(&lfsck->li_lock);
3190 lfsck->li_start_unplug = 1;
3191 wake_up_all(&thread->t_ctl_waitq);
3192 l_wait_event(thread->t_ctl_waitq,
3193 thread_is_stopped(thread),
3197 lfsck->li_start_unplug = 1;
3198 wake_up_all(&thread->t_ctl_waitq);
3204 mutex_unlock(&lfsck->li_mutex);
3207 lfsck_instance_put(env, lfsck);
3209 return rc < 0 ? rc : 0;
3211 EXPORT_SYMBOL(lfsck_start);
3213 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3214 struct lfsck_stop *stop)
3216 struct lfsck_instance *lfsck;
3217 struct ptlrpc_thread *thread;
3218 struct l_wait_info lwi = { 0 };
3223 lfsck = lfsck_instance_find(key, true, false);
3224 if (unlikely(lfsck == NULL))
3227 thread = &lfsck->li_thread;
3228 /* release lfsck::li_mutex to avoid deadlock. */
3229 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3230 if (!lfsck->li_master) {
3231 CERROR("%s: only allow to specify '-A' via MDS\n",
3232 lfsck_lfsck2name(lfsck));
3234 GOTO(out, rc = -EPERM);
3237 rc1 = lfsck_stop_all(env, lfsck, stop);
3240 mutex_lock(&lfsck->li_mutex);
3241 spin_lock(&lfsck->li_lock);
3242 /* no error if LFSCK is already stopped, or was never started */
3243 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3244 spin_unlock(&lfsck->li_lock);
3249 lfsck->li_status = stop->ls_status;
3250 lfsck->li_flags = stop->ls_flags;
3252 lfsck->li_status = LS_STOPPED;
3253 lfsck->li_flags = 0;
3256 thread_set_flags(thread, SVC_STOPPING);
3258 if (lfsck->li_master) {
3259 struct lfsck_component *com;
3260 struct lfsck_assistant_data *lad;
3262 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3264 spin_lock(&lad->lad_lock);
3265 if (lad->lad_task != NULL)
3266 force_sig(SIGINT, lad->lad_task);
3267 spin_unlock(&lad->lad_lock);
3270 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3272 spin_lock(&lad->lad_lock);
3273 if (lad->lad_task != NULL)
3274 force_sig(SIGINT, lad->lad_task);
3275 spin_unlock(&lad->lad_lock);
3279 spin_unlock(&lfsck->li_lock);
3281 wake_up_all(&thread->t_ctl_waitq);
3282 l_wait_event(thread->t_ctl_waitq,
3283 thread_is_stopped(thread),
3289 mutex_unlock(&lfsck->li_mutex);
3290 lfsck_instance_put(env, lfsck);
3292 return rc != 0 ? rc : rc1;
3294 EXPORT_SYMBOL(lfsck_stop);
3296 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3297 struct lfsck_request *lr, struct thandle *th)
3299 int rc = -EOPNOTSUPP;
3302 switch (lr->lr_event) {
3304 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3305 struct lfsck_start_param lsp;
3307 memset(start, 0, sizeof(*start));
3308 start->ls_valid = lr->lr_valid;
3309 start->ls_speed_limit = lr->lr_speed;
3310 start->ls_version = lr->lr_version;
3311 start->ls_active = lr->lr_active;
3312 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3313 start->ls_async_windows = lr->lr_async_windows;
3315 lsp.lsp_start = start;
3316 lsp.lsp_index = lr->lr_index;
3317 lsp.lsp_index_valid = 1;
3318 rc = lfsck_start(env, key, &lsp);
3322 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3324 memset(stop, 0, sizeof(*stop));
3325 stop->ls_status = lr->lr_status;
3326 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3327 rc = lfsck_stop(env, key, stop);
3330 case LE_PHASE1_DONE:
3331 case LE_PHASE2_DONE:
3332 case LE_FID_ACCESSED:
3334 case LE_CONDITIONAL_DESTROY:
3335 case LE_SKIP_NLINK_DECLARE:
3337 case LE_SET_LMV_MASTER:
3338 case LE_SET_LMV_SLAVE:
3339 case LE_PAIRS_VERIFY: {
3340 struct lfsck_instance *lfsck;
3341 struct lfsck_component *com;
3343 lfsck = lfsck_instance_find(key, true, false);
3344 if (unlikely(lfsck == NULL))
3347 com = lfsck_component_find(lfsck, lr->lr_active);
3348 if (likely(com != NULL)) {
3349 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3350 lfsck_component_put(env, com);
3353 lfsck_instance_put(env, lfsck);
3362 EXPORT_SYMBOL(lfsck_in_notify);
3364 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3365 struct lfsck_request *req, struct lfsck_reply *rep,
3366 struct lfsck_query *que)
3368 struct lfsck_instance *lfsck;
3369 struct lfsck_component *com;
3375 lfsck = lfsck_instance_find(key, true, false);
3376 if (unlikely(lfsck == NULL))
3380 if (que->lu_types == LFSCK_TYPES_ALL)
3382 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3384 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3385 que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3387 GOTO(out, rc = -ENOTSUPP);
3390 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3391 i++, type = 1 << i) {
3392 if (!(que->lu_types & type))
3396 com = lfsck_component_find(lfsck, type);
3397 if (unlikely(com == NULL))
3398 GOTO(out, rc = -ENOTSUPP);
3400 memset(que->lu_mdts_count[i], 0,
3401 sizeof(__u32) * (LS_MAX + 1));
3402 memset(que->lu_osts_count[i], 0,
3403 sizeof(__u32) * (LS_MAX + 1));
3404 que->lu_repaired[i] = 0;
3405 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3407 lfsck_component_put(env, com);
3412 if (!(que->lu_flags & LPF_WAIT))
3415 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3416 i++, type = 1 << i) {
3417 if (!(que->lu_types & type))
3420 if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3421 que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3422 que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3423 que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3424 struct l_wait_info lwi;
3426 /* If it is required to wait, then sleep
3427 * 3 seconds and try to query again. */
3428 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3432 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3434 if (rc == -ETIMEDOUT)
3439 com = lfsck_component_find(lfsck, req->lr_active);
3440 if (likely(com != NULL)) {
3441 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3443 lfsck_component_put(env, com);
3452 lfsck_instance_put(env, lfsck);
3455 EXPORT_SYMBOL(lfsck_query);
3457 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3458 struct ldlm_namespace *ns)
3460 struct lfsck_instance *lfsck;
3463 lfsck = lfsck_instance_find(key, true, false);
3464 if (likely(lfsck != NULL)) {
3465 lfsck->li_namespace = ns;
3466 lfsck_instance_put(env, lfsck);
3472 EXPORT_SYMBOL(lfsck_register_namespace);
3474 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3475 struct dt_device *next, struct obd_device *obd,
3476 lfsck_out_notify notify, void *notify_data, bool master)
3478 struct lfsck_instance *lfsck;
3479 struct dt_object *root = NULL;
3480 struct dt_object *obj = NULL;
3481 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3485 lfsck = lfsck_instance_find(key, false, false);
3486 if (unlikely(lfsck != NULL))
3489 OBD_ALLOC_PTR(lfsck);
3493 mutex_init(&lfsck->li_mutex);
3494 spin_lock_init(&lfsck->li_lock);
3495 INIT_LIST_HEAD(&lfsck->li_link);
3496 INIT_LIST_HEAD(&lfsck->li_list_scan);
3497 INIT_LIST_HEAD(&lfsck->li_list_dir);
3498 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3499 INIT_LIST_HEAD(&lfsck->li_list_idle);
3500 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3501 atomic_set(&lfsck->li_ref, 1);
3502 atomic_set(&lfsck->li_double_scan_count, 0);
3503 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3504 lfsck->li_out_notify = notify;
3505 lfsck->li_out_notify_data = notify_data;
3506 lfsck->li_next = next;
3507 lfsck->li_bottom = key;
3508 lfsck->li_obd = obd;
3510 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3514 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3518 fid->f_seq = FID_SEQ_LOCAL_NAME;
3521 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3525 rc = dt_root_get(env, key, fid);
3529 root = dt_locate(env, key, fid);
3531 GOTO(out, rc = PTR_ERR(root));
3533 if (unlikely(!dt_try_as_dir(env, root)))
3534 GOTO(out, rc = -ENOTDIR);
3536 lfsck->li_local_root_fid = *fid;
3538 lfsck->li_master = 1;
3539 if (lfsck_dev_idx(lfsck) == 0) {
3540 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3541 const struct lu_name *cname;
3543 rc = dt_lookup(env, root,
3544 (struct dt_rec *)(&lfsck->li_global_root_fid),
3545 (const struct dt_key *)"ROOT");
3549 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3551 GOTO(out, rc = PTR_ERR(obj));
3553 if (unlikely(!dt_try_as_dir(env, obj)))
3554 GOTO(out, rc = -ENOTDIR);
3556 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3557 (const struct dt_key *)dotlustre);
3561 lfsck_object_put(env, obj);
3562 obj = dt_locate(env, key, fid);
3564 GOTO(out, rc = PTR_ERR(obj));
3566 cname = lfsck_name_get_const(env, dotlustre,
3568 rc = lfsck_verify_linkea(env, obj, cname,
3569 &lfsck->li_global_root_fid);
3573 if (unlikely(!dt_try_as_dir(env, obj)))
3574 GOTO(out, rc = -ENOTDIR);
3577 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3578 (const struct dt_key *)lostfound);
3582 lfsck_object_put(env, obj);
3583 obj = dt_locate(env, key, fid);
3585 GOTO(out, rc = PTR_ERR(obj));
3587 cname = lfsck_name_get_const(env, lostfound,
3589 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3593 lfsck_object_put(env, obj);
3598 fid->f_seq = FID_SEQ_LOCAL_FILE;
3599 fid->f_oid = OTABLE_IT_OID;
3601 obj = dt_locate(env, key, fid);
3603 GOTO(out, rc = PTR_ERR(obj));
3605 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3609 lfsck->li_obj_oit = obj;
3610 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3611 S_IFDIR | S_IRUGO | S_IWUSR);
3613 GOTO(out, rc = PTR_ERR(obj));
3615 lu_object_get(&obj->do_lu);
3616 lfsck->li_lfsck_dir = obj;
3617 rc = lfsck_bookmark_setup(env, lfsck);
3622 rc = lfsck_fid_init(lfsck);
3626 rc = lfsck_namespace_setup(env, lfsck);
3631 rc = lfsck_layout_setup(env, lfsck);
3635 /* XXX: more LFSCK components initialization to be added here. */
3637 rc = lfsck_instance_add(lfsck);
3639 rc = lfsck_add_target_from_orphan(env, lfsck);
3641 if (obj != NULL && !IS_ERR(obj))
3642 lfsck_object_put(env, obj);
3643 if (root != NULL && !IS_ERR(root))
3644 lfsck_object_put(env, root);
3646 lfsck_instance_cleanup(env, lfsck);
3649 EXPORT_SYMBOL(lfsck_register);
3651 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3653 struct lfsck_instance *lfsck;
3655 lfsck = lfsck_instance_find(key, false, true);
3657 lfsck_instance_put(env, lfsck);
3659 EXPORT_SYMBOL(lfsck_degister);
3661 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3662 struct dt_device *tgt, struct obd_export *exp,
3663 __u32 index, bool for_ost)
3665 struct lfsck_instance *lfsck;
3666 struct lfsck_tgt_desc *ltd;
3677 INIT_LIST_HEAD(<d->ltd_orphan_list);
3678 INIT_LIST_HEAD(<d->ltd_layout_list);
3679 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3680 INIT_LIST_HEAD(<d->ltd_namespace_list);
3681 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3682 atomic_set(<d->ltd_ref, 1);
3683 ltd->ltd_index = index;
3685 spin_lock(&lfsck_instance_lock);
3686 lfsck = __lfsck_instance_find(key, true, false);
3687 if (lfsck == NULL) {
3689 list_add_tail(<d->ltd_orphan_list,
3690 &lfsck_ost_orphan_list);
3692 list_add_tail(<d->ltd_orphan_list,
3693 &lfsck_mdt_orphan_list);
3694 spin_unlock(&lfsck_instance_lock);
3698 spin_unlock(&lfsck_instance_lock);
3700 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3704 lfsck_instance_put(env, lfsck);
3708 EXPORT_SYMBOL(lfsck_add_target);
3710 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3711 struct dt_device *tgt, __u32 index, bool for_ost)
3713 struct lfsck_instance *lfsck;
3714 struct lfsck_tgt_descs *ltds;
3715 struct lfsck_tgt_desc *ltd;
3716 struct list_head *head;
3719 head = &lfsck_ost_orphan_list;
3721 head = &lfsck_mdt_orphan_list;
3723 spin_lock(&lfsck_instance_lock);
3724 list_for_each_entry(ltd, head, ltd_orphan_list) {
3725 if (ltd->ltd_tgt == tgt) {
3726 list_del_init(<d->ltd_orphan_list);
3727 spin_unlock(&lfsck_instance_lock);
3735 lfsck = __lfsck_instance_find(key, true, false);
3736 spin_unlock(&lfsck_instance_lock);
3737 if (unlikely(lfsck == NULL))
3741 ltds = &lfsck->li_ost_descs;
3743 ltds = &lfsck->li_mdt_descs;
3745 down_write(<ds->ltd_rw_sem);
3746 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3748 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3751 ltd = lfsck_ltd2tgt(ltds, index);
3752 if (unlikely(ltd == NULL))
3755 LASSERT(ltds->ltd_tgtnr > 0);
3758 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3759 lfsck_assign_tgt(ltds, NULL, index);
3764 head = &lfsck->li_ost_descs.ltd_orphan;
3766 head = &lfsck->li_mdt_descs.ltd_orphan;
3768 list_for_each_entry(ltd, head, ltd_orphan_list) {
3769 if (ltd->ltd_tgt == tgt) {
3770 list_del_init(<d->ltd_orphan_list);
3776 up_write(<ds->ltd_rw_sem);
3778 spin_lock(<ds->ltd_lock);
3780 spin_unlock(<ds->ltd_lock);
3781 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3782 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3786 lfsck_instance_put(env, lfsck);
3788 EXPORT_SYMBOL(lfsck_del_target);
3790 static int __init lfsck_init(void)
3794 INIT_LIST_HEAD(&lfsck_instance_list);
3795 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3796 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3797 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3798 rc = lu_context_key_register(&lfsck_thread_key);
3800 tgt_register_lfsck_in_notify(lfsck_in_notify);
3801 tgt_register_lfsck_query(lfsck_query);
3807 static void __exit lfsck_exit(void)
3809 struct lfsck_tgt_desc *ltd;
3810 struct lfsck_tgt_desc *next;
3812 LASSERT(list_empty(&lfsck_instance_list));
3814 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3816 list_del_init(<d->ltd_orphan_list);
3820 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3822 list_del_init(<d->ltd_orphan_list);
3826 lu_context_key_degister(&lfsck_thread_key);
3829 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3830 MODULE_DESCRIPTION("Lustre File System Checker");
3831 MODULE_VERSION(LUSTRE_VERSION_STRING);
3832 MODULE_LICENSE("GPL");
3834 module_init(lfsck_init);
3835 module_exit(lfsck_exit);