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 int lfsck_bits_dump(struct seq_file *m, int bits, const char *names[],
1746 bool newline = (bits != 0 ? false : true);
1749 rc = seq_printf(m, "%s:%c", prefix, bits != 0 ? ' ' : '\n');
1753 for (i = 0, flag = 1; bits != 0; i++, flag = 1 << i) {
1756 if (names[i] != NULL) {
1760 rc = seq_printf(m, "%s%c", names[i],
1761 newline ? '\n' : ',');
1769 rc = seq_printf(m, "\n");
1774 int lfsck_time_dump(struct seq_file *m, __u64 time, const char *name)
1779 rc = seq_printf(m, "%s_time: N/A\n", name);
1781 rc = seq_printf(m, "time_since_%s: N/A\n", name);
1786 rc = seq_printf(m, "%s_time: "LPU64"\n", name, time);
1788 rc = seq_printf(m, "time_since_%s: "LPU64" seconds\n",
1789 name, cfs_time_current_sec() - time);
1794 int lfsck_pos_dump(struct seq_file *m, struct lfsck_position *pos,
1797 if (fid_is_zero(&pos->lp_dir_parent)) {
1798 if (pos->lp_oit_cookie == 0)
1799 return seq_printf(m, "%s: N/A, N/A, N/A\n", prefix);
1801 return seq_printf(m, "%s: "LPU64", N/A, N/A\n",
1802 prefix, pos->lp_oit_cookie);
1805 return seq_printf(m, "%s: "LPU64", "DFID", "LPX64"\n",
1806 prefix, pos->lp_oit_cookie,
1807 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie);
1810 void lfsck_pos_fill(const struct lu_env *env, struct lfsck_instance *lfsck,
1811 struct lfsck_position *pos, bool init)
1813 const struct dt_it_ops *iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1815 if (unlikely(lfsck->li_di_oit == NULL)) {
1816 memset(pos, 0, sizeof(*pos));
1820 pos->lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1821 if (!lfsck->li_current_oit_processed && !init)
1822 pos->lp_oit_cookie--;
1824 LASSERT(pos->lp_oit_cookie > 0);
1826 if (lfsck->li_di_dir != NULL) {
1827 struct dt_object *dto = lfsck->li_obj_dir;
1829 pos->lp_dir_cookie = dto->do_index_ops->dio_it.store(env,
1832 if (pos->lp_dir_cookie >= MDS_DIR_END_OFF) {
1833 fid_zero(&pos->lp_dir_parent);
1834 pos->lp_dir_cookie = 0;
1836 pos->lp_dir_parent = *lfsck_dto2fid(dto);
1839 fid_zero(&pos->lp_dir_parent);
1840 pos->lp_dir_cookie = 0;
1844 bool __lfsck_set_speed(struct lfsck_instance *lfsck, __u32 limit)
1848 if (limit != LFSCK_SPEED_NO_LIMIT) {
1849 if (limit > msecs_to_jiffies(MSEC_PER_SEC)) {
1850 lfsck->li_sleep_rate = limit /
1851 msecs_to_jiffies(MSEC_PER_SEC);
1852 lfsck->li_sleep_jif = 1;
1854 lfsck->li_sleep_rate = 1;
1855 lfsck->li_sleep_jif = msecs_to_jiffies(MSEC_PER_SEC) /
1859 lfsck->li_sleep_jif = 0;
1860 lfsck->li_sleep_rate = 0;
1863 if (lfsck->li_bookmark_ram.lb_speed_limit != limit) {
1864 lfsck->li_bookmark_ram.lb_speed_limit = limit;
1871 void lfsck_control_speed(struct lfsck_instance *lfsck)
1873 struct ptlrpc_thread *thread = &lfsck->li_thread;
1874 struct l_wait_info lwi;
1876 if (lfsck->li_sleep_jif > 0 &&
1877 lfsck->li_new_scanned >= lfsck->li_sleep_rate) {
1878 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1879 LWI_ON_SIGNAL_NOOP, NULL);
1881 l_wait_event(thread->t_ctl_waitq,
1882 !thread_is_running(thread),
1884 lfsck->li_new_scanned = 0;
1888 void lfsck_control_speed_by_self(struct lfsck_component *com)
1890 struct lfsck_instance *lfsck = com->lc_lfsck;
1891 struct ptlrpc_thread *thread = &lfsck->li_thread;
1892 struct l_wait_info lwi;
1894 if (lfsck->li_sleep_jif > 0 &&
1895 com->lc_new_scanned >= lfsck->li_sleep_rate) {
1896 lwi = LWI_TIMEOUT_INTR(lfsck->li_sleep_jif, NULL,
1897 LWI_ON_SIGNAL_NOOP, NULL);
1899 l_wait_event(thread->t_ctl_waitq,
1900 !thread_is_running(thread),
1902 com->lc_new_scanned = 0;
1906 static struct lfsck_thread_args *
1907 lfsck_thread_args_init(struct lfsck_instance *lfsck,
1908 struct lfsck_component *com,
1909 struct lfsck_start_param *lsp)
1911 struct lfsck_thread_args *lta;
1916 return ERR_PTR(-ENOMEM);
1918 rc = lu_env_init(<a->lta_env, LCT_MD_THREAD | LCT_DT_THREAD);
1924 lta->lta_lfsck = lfsck_instance_get(lfsck);
1926 lta->lta_com = lfsck_component_get(com);
1933 void lfsck_thread_args_fini(struct lfsck_thread_args *lta)
1935 if (lta->lta_com != NULL)
1936 lfsck_component_put(<a->lta_env, lta->lta_com);
1937 lfsck_instance_put(<a->lta_env, lta->lta_lfsck);
1938 lu_env_fini(<a->lta_env);
1942 struct lfsck_assistant_data *
1943 lfsck_assistant_data_init(struct lfsck_assistant_operations *lao,
1946 struct lfsck_assistant_data *lad;
1950 lad->lad_bitmap = CFS_ALLOCATE_BITMAP(BITS_PER_LONG);
1951 if (lad->lad_bitmap == NULL) {
1956 INIT_LIST_HEAD(&lad->lad_req_list);
1957 spin_lock_init(&lad->lad_lock);
1958 INIT_LIST_HEAD(&lad->lad_ost_list);
1959 INIT_LIST_HEAD(&lad->lad_ost_phase1_list);
1960 INIT_LIST_HEAD(&lad->lad_ost_phase2_list);
1961 INIT_LIST_HEAD(&lad->lad_mdt_list);
1962 INIT_LIST_HEAD(&lad->lad_mdt_phase1_list);
1963 INIT_LIST_HEAD(&lad->lad_mdt_phase2_list);
1964 init_waitqueue_head(&lad->lad_thread.t_ctl_waitq);
1966 lad->lad_name = name;
1972 struct lfsck_assistant_object *
1973 lfsck_assistant_object_init(const struct lu_env *env, const struct lu_fid *fid,
1974 const struct lu_attr *attr, __u64 cookie,
1977 struct lfsck_assistant_object *lso;
1981 return ERR_PTR(-ENOMEM);
1983 lso->lso_fid = *fid;
1985 lso->lso_attr = *attr;
1987 atomic_set(&lso->lso_ref, 1);
1988 lso->lso_oit_cookie = cookie;
1990 lso->lso_is_dir = 1;
1996 lfsck_assistant_object_load(const struct lu_env *env,
1997 struct lfsck_instance *lfsck,
1998 struct lfsck_assistant_object *lso)
2000 struct dt_object *obj;
2002 obj = lfsck_object_find_bottom(env, lfsck, &lso->lso_fid);
2006 if (unlikely(!dt_object_exists(obj) || lfsck_is_dead_obj(obj))) {
2008 lfsck_object_put(env, obj);
2010 return ERR_PTR(-ENOENT);
2013 if (lso->lso_is_dir && unlikely(!dt_try_as_dir(env, obj))) {
2014 lfsck_object_put(env, obj);
2016 return ERR_PTR(-ENOTDIR);
2023 * Generic LFSCK asynchronous communication interpretor function.
2024 * The LFSCK RPC reply for both the event notification and status
2025 * querying will be handled here.
2027 * \param[in] env pointer to the thread context
2028 * \param[in] req pointer to the LFSCK request
2029 * \param[in] args pointer to the lfsck_async_interpret_args
2030 * \param[in] rc the result for handling the LFSCK request
2032 * \retval 0 for success
2033 * \retval negative error number on failure
2035 int lfsck_async_interpret_common(const struct lu_env *env,
2036 struct ptlrpc_request *req,
2039 struct lfsck_async_interpret_args *laia = args;
2040 struct lfsck_component *com = laia->laia_com;
2041 struct lfsck_assistant_data *lad = com->lc_data;
2042 struct lfsck_tgt_descs *ltds = laia->laia_ltds;
2043 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
2044 struct lfsck_request *lr = laia->laia_lr;
2046 LASSERT(com->lc_lfsck->li_master);
2048 switch (lr->lr_event) {
2051 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s "
2053 lfsck_lfsck2name(com->lc_lfsck),
2054 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2055 ltd->ltd_index, lad->lad_name, rc);
2057 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2058 struct lfsck_layout *lo = com->lc_file_ram;
2060 if (lr->lr_flags & LEF_TO_OST)
2061 lfsck_lad_set_bitmap(env, com,
2064 lo->ll_flags |= LF_INCOMPLETE;
2066 struct lfsck_namespace *ns = com->lc_file_ram;
2068 /* If some MDT does not join the namespace
2069 * LFSCK, then we cannot know whether there
2070 * is some name entry on such MDT that with
2071 * the referenced MDT-object on this MDT or
2072 * not. So the namespace LFSCK on this MDT
2073 * cannot handle orphan MDT-objects properly.
2074 * So we mark the LFSCK as LF_INCOMPLETE and
2075 * skip orphan MDT-objects handling. */
2076 ns->ln_flags |= LF_INCOMPLETE;
2081 spin_lock(<ds->ltd_lock);
2082 if (ltd->ltd_dead) {
2083 spin_unlock(<ds->ltd_lock);
2087 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2088 struct list_head *list;
2089 struct list_head *phase_list;
2091 if (ltd->ltd_layout_done) {
2092 spin_unlock(<ds->ltd_lock);
2096 if (lr->lr_flags & LEF_TO_OST) {
2097 list = &lad->lad_ost_list;
2098 phase_list = &lad->lad_ost_phase1_list;
2100 list = &lad->lad_mdt_list;
2101 phase_list = &lad->lad_mdt_phase1_list;
2104 if (list_empty(<d->ltd_layout_list))
2105 list_add_tail(<d->ltd_layout_list, list);
2106 if (list_empty(<d->ltd_layout_phase_list))
2107 list_add_tail(<d->ltd_layout_phase_list,
2110 if (ltd->ltd_namespace_done) {
2111 spin_unlock(<ds->ltd_lock);
2115 if (list_empty(<d->ltd_namespace_list))
2116 list_add_tail(<d->ltd_namespace_list,
2117 &lad->lad_mdt_list);
2118 if (list_empty(<d->ltd_namespace_phase_list))
2119 list_add_tail(<d->ltd_namespace_phase_list,
2120 &lad->lad_mdt_phase1_list);
2122 spin_unlock(<ds->ltd_lock);
2125 case LE_PHASE1_DONE:
2126 case LE_PHASE2_DONE:
2128 if (rc != 0 && rc != -EALREADY)
2129 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for %s: "
2130 "event = %d, rc = %d\n",
2131 lfsck_lfsck2name(com->lc_lfsck),
2132 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2133 ltd->ltd_index, lad->lad_name, lr->lr_event, rc);
2136 struct lfsck_reply *reply;
2137 struct list_head *list;
2138 struct list_head *phase_list;
2140 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2141 list = <d->ltd_layout_list;
2142 phase_list = <d->ltd_layout_phase_list;
2144 list = <d->ltd_namespace_list;
2145 phase_list = <d->ltd_namespace_phase_list;
2149 if (lr->lr_flags & LEF_QUERY_ALL) {
2150 lfsck_reset_ltd_status(ltd, com->lc_type);
2154 spin_lock(<ds->ltd_lock);
2155 list_del_init(phase_list);
2156 list_del_init(list);
2157 spin_unlock(<ds->ltd_lock);
2161 reply = req_capsule_server_get(&req->rq_pill,
2163 if (reply == NULL) {
2165 CDEBUG(D_LFSCK, "%s: invalid query reply for %s: "
2166 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck),
2169 if (lr->lr_flags & LEF_QUERY_ALL) {
2170 lfsck_reset_ltd_status(ltd, com->lc_type);
2174 spin_lock(<ds->ltd_lock);
2175 list_del_init(phase_list);
2176 list_del_init(list);
2177 spin_unlock(<ds->ltd_lock);
2181 if (lr->lr_flags & LEF_QUERY_ALL) {
2182 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2183 ltd->ltd_layout_status = reply->lr_status;
2184 ltd->ltd_layout_repaired = reply->lr_repaired;
2186 ltd->ltd_namespace_status = reply->lr_status;
2187 ltd->ltd_namespace_repaired =
2193 switch (reply->lr_status) {
2194 case LS_SCANNING_PHASE1:
2196 case LS_SCANNING_PHASE2:
2197 spin_lock(<ds->ltd_lock);
2198 list_del_init(phase_list);
2199 if (ltd->ltd_dead) {
2200 spin_unlock(<ds->ltd_lock);
2204 if (com->lc_type == LFSCK_TYPE_LAYOUT) {
2205 if (ltd->ltd_layout_done) {
2206 spin_unlock(<ds->ltd_lock);
2210 if (lr->lr_flags & LEF_TO_OST)
2211 list_add_tail(phase_list,
2212 &lad->lad_ost_phase2_list);
2214 list_add_tail(phase_list,
2215 &lad->lad_mdt_phase2_list);
2217 if (ltd->ltd_namespace_done) {
2218 spin_unlock(<ds->ltd_lock);
2222 list_add_tail(phase_list,
2223 &lad->lad_mdt_phase2_list);
2225 spin_unlock(<ds->ltd_lock);
2228 spin_lock(<ds->ltd_lock);
2229 list_del_init(phase_list);
2230 list_del_init(list);
2231 spin_unlock(<ds->ltd_lock);
2237 CDEBUG(D_LFSCK, "%s: unexpected event: rc = %d\n",
2238 lfsck_lfsck2name(com->lc_lfsck), lr->lr_event);
2242 if (!laia->laia_shared) {
2244 lfsck_component_put(env, com);
2250 static void lfsck_interpret(const struct lu_env *env,
2251 struct lfsck_instance *lfsck,
2252 struct ptlrpc_request *req, void *args, int result)
2254 struct lfsck_async_interpret_args *laia = args;
2255 struct lfsck_component *com;
2257 LASSERT(laia->laia_com == NULL);
2258 LASSERT(laia->laia_shared);
2260 spin_lock(&lfsck->li_lock);
2261 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
2262 laia->laia_com = com;
2263 lfsck_async_interpret_common(env, req, laia, result);
2266 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
2267 laia->laia_com = com;
2268 lfsck_async_interpret_common(env, req, laia, result);
2270 spin_unlock(&lfsck->li_lock);
2273 static int lfsck_stop_notify(const struct lu_env *env,
2274 struct lfsck_instance *lfsck,
2275 struct lfsck_tgt_descs *ltds,
2276 struct lfsck_tgt_desc *ltd, __u16 type)
2278 struct lfsck_component *com;
2282 LASSERT(lfsck->li_master);
2284 spin_lock(&lfsck->li_lock);
2285 com = __lfsck_component_find(lfsck, type, &lfsck->li_list_scan);
2287 com = __lfsck_component_find(lfsck, type,
2288 &lfsck->li_list_double_scan);
2290 lfsck_component_get(com);
2291 spin_unlock(&lfsck->li_lock);
2294 struct lfsck_thread_info *info = lfsck_env_info(env);
2295 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2296 struct lfsck_request *lr = &info->lti_lr;
2297 struct lfsck_assistant_data *lad = com->lc_data;
2298 struct list_head *list;
2299 struct list_head *phase_list;
2300 struct ptlrpc_request_set *set;
2302 set = ptlrpc_prep_set();
2304 lfsck_component_put(env, com);
2309 if (type == LFSCK_TYPE_LAYOUT) {
2310 list = <d->ltd_layout_list;
2311 phase_list = <d->ltd_layout_phase_list;
2313 list = <d->ltd_namespace_list;
2314 phase_list = <d->ltd_namespace_phase_list;
2317 spin_lock(<ds->ltd_lock);
2318 if (list_empty(list)) {
2319 LASSERT(list_empty(phase_list));
2320 spin_unlock(<ds->ltd_lock);
2321 ptlrpc_set_destroy(set);
2326 list_del_init(phase_list);
2327 list_del_init(list);
2328 spin_unlock(<ds->ltd_lock);
2330 memset(lr, 0, sizeof(*lr));
2331 lr->lr_index = lfsck_dev_idx(lfsck);
2332 lr->lr_event = LE_PEER_EXIT;
2333 lr->lr_active = type;
2334 lr->lr_status = LS_CO_PAUSED;
2335 if (ltds == &lfsck->li_ost_descs)
2336 lr->lr_flags = LEF_TO_OST;
2338 memset(laia, 0, sizeof(*laia));
2339 laia->laia_com = com;
2340 laia->laia_ltds = ltds;
2341 atomic_inc(<d->ltd_ref);
2342 laia->laia_ltd = ltd;
2345 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2346 lfsck_async_interpret_common,
2347 laia, LFSCK_NOTIFY);
2349 CDEBUG(D_LFSCK, "%s: fail to notify %s %x for "
2350 "co-stop for %s: rc = %d\n",
2351 lfsck_lfsck2name(lfsck),
2352 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2353 ltd->ltd_index, lad->lad_name, rc);
2356 rc = ptlrpc_set_wait(set);
2359 ptlrpc_set_destroy(set);
2360 lfsck_component_put(env, com);
2366 static int lfsck_async_interpret(const struct lu_env *env,
2367 struct ptlrpc_request *req,
2370 struct lfsck_async_interpret_args *laia = args;
2371 struct lfsck_instance *lfsck;
2373 lfsck = container_of0(laia->laia_ltds, struct lfsck_instance,
2375 lfsck_interpret(env, lfsck, req, laia, rc);
2376 lfsck_tgt_put(laia->laia_ltd);
2377 if (rc != 0 && laia->laia_result != -EALREADY)
2378 laia->laia_result = rc;
2383 int lfsck_async_request(const struct lu_env *env, struct obd_export *exp,
2384 struct lfsck_request *lr,
2385 struct ptlrpc_request_set *set,
2386 ptlrpc_interpterer_t interpreter,
2387 void *args, int request)
2389 struct lfsck_async_interpret_args *laia;
2390 struct ptlrpc_request *req;
2391 struct lfsck_request *tmp;
2392 struct req_format *format;
2397 format = &RQF_LFSCK_NOTIFY;
2400 format = &RQF_LFSCK_QUERY;
2403 CDEBUG(D_LFSCK, "%s: unknown async request %d: rc = %d\n",
2404 exp->exp_obd->obd_name, request, -EINVAL);
2408 req = ptlrpc_request_alloc(class_exp2cliimp(exp), format);
2412 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, request);
2414 ptlrpc_request_free(req);
2419 tmp = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
2421 ptlrpc_request_set_replen(req);
2423 laia = ptlrpc_req_async_args(req);
2424 *laia = *(struct lfsck_async_interpret_args *)args;
2425 if (laia->laia_com != NULL)
2426 lfsck_component_get(laia->laia_com);
2427 req->rq_interpret_reply = interpreter;
2428 req->rq_allow_intr = 1;
2429 ptlrpc_set_add_req(set, req);
2434 int lfsck_query_all(const struct lu_env *env, struct lfsck_component *com)
2436 struct lfsck_thread_info *info = lfsck_env_info(env);
2437 struct lfsck_request *lr = &info->lti_lr;
2438 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2439 struct lfsck_instance *lfsck = com->lc_lfsck;
2440 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2441 struct lfsck_tgt_desc *ltd;
2442 struct ptlrpc_request_set *set;
2447 memset(lr, 0, sizeof(*lr));
2448 lr->lr_event = LE_QUERY;
2449 lr->lr_active = com->lc_type;
2450 lr->lr_flags = LEF_QUERY_ALL;
2452 memset(laia, 0, sizeof(*laia));
2453 laia->laia_com = com;
2456 set = ptlrpc_prep_set();
2461 laia->laia_ltds = ltds;
2462 down_read(<ds->ltd_rw_sem);
2463 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2464 ltd = lfsck_tgt_get(ltds, idx);
2465 LASSERT(ltd != NULL);
2467 laia->laia_ltd = ltd;
2468 up_read(<ds->ltd_rw_sem);
2469 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2470 lfsck_async_interpret_common,
2473 struct lfsck_assistant_data *lad = com->lc_data;
2475 CDEBUG(D_LFSCK, "%s: Fail to query %s %x for stat %s: "
2476 "rc = %d\n", lfsck_lfsck2name(lfsck),
2477 (lr->lr_flags & LEF_TO_OST) ? "OST" : "MDT",
2478 ltd->ltd_index, lad->lad_name, rc);
2479 lfsck_reset_ltd_status(ltd, com->lc_type);
2482 down_read(<ds->ltd_rw_sem);
2484 up_read(<ds->ltd_rw_sem);
2486 if (com->lc_type == LFSCK_TYPE_LAYOUT && !(lr->lr_flags & LEF_TO_OST)) {
2487 ltds = &lfsck->li_ost_descs;
2488 lr->lr_flags |= LEF_TO_OST;
2492 rc = ptlrpc_set_wait(set);
2493 ptlrpc_set_destroy(set);
2498 int lfsck_start_assistant(const struct lu_env *env, struct lfsck_component *com,
2499 struct lfsck_start_param *lsp)
2501 struct lfsck_instance *lfsck = com->lc_lfsck;
2502 struct lfsck_assistant_data *lad = com->lc_data;
2503 struct ptlrpc_thread *mthread = &lfsck->li_thread;
2504 struct ptlrpc_thread *athread = &lad->lad_thread;
2505 struct lfsck_thread_args *lta;
2506 struct task_struct *task;
2510 lad->lad_assistant_status = 0;
2511 lad->lad_post_result = 0;
2512 lad->lad_to_post = 0;
2513 lad->lad_to_double_scan = 0;
2514 lad->lad_in_double_scan = 0;
2516 lad->lad_advance_lock = false;
2517 thread_set_flags(athread, 0);
2519 lta = lfsck_thread_args_init(lfsck, com, lsp);
2521 RETURN(PTR_ERR(lta));
2523 task = kthread_run(lfsck_assistant_engine, lta, lad->lad_name);
2526 CERROR("%s: cannot start LFSCK assistant thread for %s: "
2527 "rc = %d\n", lfsck_lfsck2name(lfsck), lad->lad_name, rc);
2528 lfsck_thread_args_fini(lta);
2530 struct l_wait_info lwi = { 0 };
2532 l_wait_event(mthread->t_ctl_waitq,
2533 thread_is_running(athread) ||
2534 thread_is_stopped(athread),
2536 if (unlikely(!thread_is_running(athread)))
2537 rc = lad->lad_assistant_status;
2545 int lfsck_checkpoint_generic(const struct lu_env *env,
2546 struct lfsck_component *com)
2548 struct lfsck_assistant_data *lad = com->lc_data;
2549 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2550 struct ptlrpc_thread *athread = &lad->lad_thread;
2551 struct l_wait_info lwi = { 0 };
2553 l_wait_event(mthread->t_ctl_waitq,
2554 list_empty(&lad->lad_req_list) ||
2555 !thread_is_running(mthread) ||
2556 thread_is_stopped(athread),
2559 if (!thread_is_running(mthread) || thread_is_stopped(athread))
2560 return LFSCK_CHECKPOINT_SKIP;
2565 void lfsck_post_generic(const struct lu_env *env,
2566 struct lfsck_component *com, int *result)
2568 struct lfsck_assistant_data *lad = com->lc_data;
2569 struct ptlrpc_thread *athread = &lad->lad_thread;
2570 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2571 struct l_wait_info lwi = { 0 };
2573 lad->lad_post_result = *result;
2576 lad->lad_to_post = 1;
2578 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s post, rc = %d\n",
2579 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2581 wake_up_all(&athread->t_ctl_waitq);
2582 l_wait_event(mthread->t_ctl_waitq,
2583 (*result > 0 && list_empty(&lad->lad_req_list)) ||
2584 thread_is_stopped(athread),
2587 if (lad->lad_assistant_status < 0)
2588 *result = lad->lad_assistant_status;
2590 CDEBUG(D_LFSCK, "%s: the assistant has done %s post, rc = %d\n",
2591 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, *result);
2594 int lfsck_double_scan_generic(const struct lu_env *env,
2595 struct lfsck_component *com, int status)
2597 struct lfsck_assistant_data *lad = com->lc_data;
2598 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2599 struct ptlrpc_thread *athread = &lad->lad_thread;
2600 struct l_wait_info lwi = { 0 };
2602 if (status != LS_SCANNING_PHASE2)
2605 lad->lad_to_double_scan = 1;
2607 CDEBUG(D_LFSCK, "%s: waiting for assistant to do %s double_scan, "
2609 lfsck_lfsck2name(com->lc_lfsck), lad->lad_name, status);
2611 wake_up_all(&athread->t_ctl_waitq);
2612 l_wait_event(mthread->t_ctl_waitq,
2613 lad->lad_in_double_scan ||
2614 thread_is_stopped(athread),
2617 CDEBUG(D_LFSCK, "%s: the assistant has done %s double_scan, "
2618 "status %d\n", lfsck_lfsck2name(com->lc_lfsck), lad->lad_name,
2619 lad->lad_assistant_status);
2621 if (lad->lad_assistant_status < 0)
2622 return lad->lad_assistant_status;
2627 void lfsck_quit_generic(const struct lu_env *env,
2628 struct lfsck_component *com)
2630 struct lfsck_assistant_data *lad = com->lc_data;
2631 struct ptlrpc_thread *mthread = &com->lc_lfsck->li_thread;
2632 struct ptlrpc_thread *athread = &lad->lad_thread;
2633 struct l_wait_info lwi = { 0 };
2636 wake_up_all(&athread->t_ctl_waitq);
2637 l_wait_event(mthread->t_ctl_waitq,
2638 thread_is_init(athread) ||
2639 thread_is_stopped(athread),
2643 /* external interfaces */
2645 int lfsck_get_speed(struct seq_file *m, struct dt_device *key)
2648 struct lfsck_instance *lfsck;
2652 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2656 lfsck = lfsck_instance_find(key, true, false);
2657 if (likely(lfsck != NULL)) {
2658 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_speed_limit);
2659 lfsck_instance_put(&env, lfsck);
2668 EXPORT_SYMBOL(lfsck_get_speed);
2670 int lfsck_set_speed(struct dt_device *key, int val)
2673 struct lfsck_instance *lfsck;
2677 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2681 lfsck = lfsck_instance_find(key, true, false);
2682 if (likely(lfsck != NULL)) {
2683 mutex_lock(&lfsck->li_mutex);
2684 if (__lfsck_set_speed(lfsck, val))
2685 rc = lfsck_bookmark_store(&env, lfsck);
2686 mutex_unlock(&lfsck->li_mutex);
2687 lfsck_instance_put(&env, lfsck);
2696 EXPORT_SYMBOL(lfsck_set_speed);
2698 int lfsck_get_windows(struct seq_file *m, struct dt_device *key)
2701 struct lfsck_instance *lfsck;
2705 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2709 lfsck = lfsck_instance_find(key, true, false);
2710 if (likely(lfsck != NULL)) {
2711 seq_printf(m, "%u\n", lfsck->li_bookmark_ram.lb_async_windows);
2712 lfsck_instance_put(&env, lfsck);
2721 EXPORT_SYMBOL(lfsck_get_windows);
2723 int lfsck_set_windows(struct dt_device *key, int val)
2726 struct lfsck_instance *lfsck;
2730 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2734 lfsck = lfsck_instance_find(key, true, false);
2735 if (likely(lfsck != NULL)) {
2736 if (val < 1 || val > LFSCK_ASYNC_WIN_MAX) {
2737 CWARN("%s: invalid async windows size that may "
2738 "cause memory issues. The valid range is "
2740 lfsck_lfsck2name(lfsck), LFSCK_ASYNC_WIN_MAX);
2742 } else if (lfsck->li_bookmark_ram.lb_async_windows != val) {
2743 mutex_lock(&lfsck->li_mutex);
2744 lfsck->li_bookmark_ram.lb_async_windows = val;
2745 rc = lfsck_bookmark_store(&env, lfsck);
2746 mutex_unlock(&lfsck->li_mutex);
2748 lfsck_instance_put(&env, lfsck);
2757 EXPORT_SYMBOL(lfsck_set_windows);
2759 int lfsck_dump(struct seq_file *m, struct dt_device *key, enum lfsck_type type)
2762 struct lfsck_instance *lfsck;
2763 struct lfsck_component *com;
2767 rc = lu_env_init(&env, LCT_MD_THREAD | LCT_DT_THREAD);
2771 lfsck = lfsck_instance_find(key, true, false);
2772 if (likely(lfsck != NULL)) {
2773 com = lfsck_component_find(lfsck, type);
2774 if (likely(com != NULL)) {
2775 rc = com->lc_ops->lfsck_dump(&env, com, m);
2776 lfsck_component_put(&env, com);
2781 lfsck_instance_put(&env, lfsck);
2790 EXPORT_SYMBOL(lfsck_dump);
2792 static int lfsck_stop_all(const struct lu_env *env,
2793 struct lfsck_instance *lfsck,
2794 struct lfsck_stop *stop)
2796 struct lfsck_thread_info *info = lfsck_env_info(env);
2797 struct lfsck_request *lr = &info->lti_lr;
2798 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2799 struct ptlrpc_request_set *set;
2800 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2801 struct lfsck_tgt_desc *ltd;
2802 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2808 LASSERT(stop->ls_flags & LPF_BROADCAST);
2810 set = ptlrpc_prep_set();
2811 if (unlikely(set == NULL))
2814 memset(lr, 0, sizeof(*lr));
2815 lr->lr_event = LE_STOP;
2816 lr->lr_index = lfsck_dev_idx(lfsck);
2817 lr->lr_status = stop->ls_status;
2818 lr->lr_version = bk->lb_version;
2819 lr->lr_active = LFSCK_TYPES_ALL;
2820 lr->lr_param = stop->ls_flags;
2822 memset(laia, 0, sizeof(*laia));
2823 laia->laia_ltds = ltds;
2825 laia->laia_shared = 1;
2827 down_read(<ds->ltd_rw_sem);
2828 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2829 ltd = lfsck_tgt_get(ltds, idx);
2830 LASSERT(ltd != NULL);
2832 laia->laia_ltd = ltd;
2833 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2834 lfsck_async_interpret, laia,
2837 lfsck_interpret(env, lfsck, NULL, laia, rc);
2839 CERROR("%s: cannot notify MDT %x for LFSCK stop: "
2840 "rc = %d\n", lfsck_lfsck2name(lfsck), idx, rc);
2844 up_read(<ds->ltd_rw_sem);
2846 rc = ptlrpc_set_wait(set);
2847 ptlrpc_set_destroy(set);
2850 rc = laia->laia_result;
2852 if (rc == -EALREADY)
2856 CERROR("%s: fail to stop LFSCK on some MDTs: rc = %d\n",
2857 lfsck_lfsck2name(lfsck), rc);
2859 RETURN(rc != 0 ? rc : rc1);
2862 static int lfsck_start_all(const struct lu_env *env,
2863 struct lfsck_instance *lfsck,
2864 struct lfsck_start *start)
2866 struct lfsck_thread_info *info = lfsck_env_info(env);
2867 struct lfsck_request *lr = &info->lti_lr;
2868 struct lfsck_async_interpret_args *laia = &info->lti_laia;
2869 struct ptlrpc_request_set *set;
2870 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2871 struct lfsck_tgt_desc *ltd;
2872 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2877 LASSERT(start->ls_flags & LPF_BROADCAST);
2879 set = ptlrpc_prep_set();
2880 if (unlikely(set == NULL))
2883 memset(lr, 0, sizeof(*lr));
2884 lr->lr_event = LE_START;
2885 lr->lr_index = lfsck_dev_idx(lfsck);
2886 lr->lr_speed = bk->lb_speed_limit;
2887 lr->lr_version = bk->lb_version;
2888 lr->lr_active = start->ls_active;
2889 lr->lr_param = start->ls_flags;
2890 lr->lr_async_windows = bk->lb_async_windows;
2891 lr->lr_valid = LSV_SPEED_LIMIT | LSV_ERROR_HANDLE | LSV_DRYRUN |
2892 LSV_ASYNC_WINDOWS | LSV_CREATE_OSTOBJ |
2895 memset(laia, 0, sizeof(*laia));
2896 laia->laia_ltds = ltds;
2898 laia->laia_shared = 1;
2900 down_read(<ds->ltd_rw_sem);
2901 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
2902 ltd = lfsck_tgt_get(ltds, idx);
2903 LASSERT(ltd != NULL);
2905 laia->laia_ltd = ltd;
2906 ltd->ltd_layout_done = 0;
2907 ltd->ltd_namespace_done = 0;
2908 ltd->ltd_synced_failures = 0;
2909 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
2910 lfsck_async_interpret, laia,
2913 lfsck_interpret(env, lfsck, NULL, laia, rc);
2915 CERROR("%s: cannot notify MDT %x for LFSCK "
2916 "start, failout: rc = %d\n",
2917 lfsck_lfsck2name(lfsck), idx, rc);
2921 up_read(<ds->ltd_rw_sem);
2924 ptlrpc_set_destroy(set);
2929 rc = ptlrpc_set_wait(set);
2930 ptlrpc_set_destroy(set);
2933 rc = laia->laia_result;
2936 struct lfsck_stop *stop = &info->lti_stop;
2938 CERROR("%s: cannot start LFSCK on some MDTs, "
2939 "stop all: rc = %d\n",
2940 lfsck_lfsck2name(lfsck), rc);
2941 if (rc != -EALREADY) {
2942 stop->ls_status = LS_FAILED;
2943 stop->ls_flags = LPF_ALL_TGT | LPF_BROADCAST;
2944 lfsck_stop_all(env, lfsck, stop);
2951 int lfsck_start(const struct lu_env *env, struct dt_device *key,
2952 struct lfsck_start_param *lsp)
2954 struct lfsck_start *start = lsp->lsp_start;
2955 struct lfsck_instance *lfsck;
2956 struct lfsck_bookmark *bk;
2957 struct ptlrpc_thread *thread;
2958 struct lfsck_component *com;
2959 struct l_wait_info lwi = { 0 };
2960 struct lfsck_thread_args *lta;
2961 struct task_struct *task;
2962 struct lfsck_tgt_descs *ltds;
2963 struct lfsck_tgt_desc *ltd;
2971 lfsck = lfsck_instance_find(key, true, false);
2972 if (unlikely(lfsck == NULL))
2975 /* System is not ready, try again later. */
2976 if (unlikely(lfsck->li_namespace == NULL))
2977 GOTO(put, rc = -EAGAIN);
2979 /* start == NULL means auto trigger paused LFSCK. */
2980 if ((start == NULL) &&
2981 (list_empty(&lfsck->li_list_scan) ||
2982 OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_AUTO)))
2985 bk = &lfsck->li_bookmark_ram;
2986 thread = &lfsck->li_thread;
2987 mutex_lock(&lfsck->li_mutex);
2988 spin_lock(&lfsck->li_lock);
2989 if (!thread_is_init(thread) && !thread_is_stopped(thread)) {
2991 if (unlikely(start == NULL)) {
2992 spin_unlock(&lfsck->li_lock);
2996 while (start->ls_active != 0) {
2997 if (!(type & start->ls_active)) {
3002 com = __lfsck_component_find(lfsck, type,
3003 &lfsck->li_list_scan);
3005 com = __lfsck_component_find(lfsck, type,
3006 &lfsck->li_list_double_scan);
3012 if (com->lc_ops->lfsck_join != NULL) {
3013 rc = com->lc_ops->lfsck_join( env, com, lsp);
3014 if (rc != 0 && rc != -EALREADY)
3017 start->ls_active &= ~type;
3020 spin_unlock(&lfsck->li_lock);
3023 spin_unlock(&lfsck->li_lock);
3025 lfsck->li_status = 0;
3026 lfsck->li_oit_over = 0;
3027 lfsck->li_start_unplug = 0;
3028 lfsck->li_drop_dryrun = 0;
3029 lfsck->li_new_scanned = 0;
3031 /* For auto trigger. */
3035 if (start->ls_flags & LPF_BROADCAST && !lfsck->li_master) {
3036 CERROR("%s: only allow to specify '-A | -o' via MDS\n",
3037 lfsck_lfsck2name(lfsck));
3039 GOTO(out, rc = -EPERM);
3042 start->ls_version = bk->lb_version;
3044 if (start->ls_active != 0) {
3045 struct lfsck_component *next;
3047 if (start->ls_active == LFSCK_TYPES_ALL)
3048 start->ls_active = LFSCK_TYPES_SUPPORTED;
3050 if (start->ls_active & ~LFSCK_TYPES_SUPPORTED) {
3051 start->ls_active &= ~LFSCK_TYPES_SUPPORTED;
3052 GOTO(out, rc = -ENOTSUPP);
3055 list_for_each_entry_safe(com, next,
3056 &lfsck->li_list_scan, lc_link) {
3057 if (!(com->lc_type & start->ls_active)) {
3058 rc = com->lc_ops->lfsck_post(env, com, 0,
3065 while (start->ls_active != 0) {
3066 if (type & start->ls_active) {
3067 com = __lfsck_component_find(lfsck, type,
3068 &lfsck->li_list_idle);
3070 /* The component status will be updated
3071 * when its prep() is called later by
3072 * the LFSCK main engine. */
3073 list_move_tail(&com->lc_link,
3074 &lfsck->li_list_scan);
3075 start->ls_active &= ~type;
3081 if (list_empty(&lfsck->li_list_scan)) {
3082 /* The speed limit will be used to control both the LFSCK and
3083 * low layer scrub (if applied), need to be handled firstly. */
3084 if (start->ls_valid & LSV_SPEED_LIMIT) {
3085 if (__lfsck_set_speed(lfsck, start->ls_speed_limit)) {
3086 rc = lfsck_bookmark_store(env, lfsck);
3095 if (start->ls_flags & LPF_RESET)
3096 flags |= DOIF_RESET;
3098 rc = lfsck_set_param(env, lfsck, start, !!(flags & DOIF_RESET));
3102 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3103 start->ls_active |= com->lc_type;
3104 if (flags & DOIF_RESET) {
3105 rc = com->lc_ops->lfsck_reset(env, com, false);
3111 ltds = &lfsck->li_mdt_descs;
3112 down_read(<ds->ltd_rw_sem);
3113 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3114 ltd = lfsck_ltd2tgt(ltds, idx);
3115 LASSERT(ltd != NULL);
3117 ltd->ltd_layout_done = 0;
3118 ltd->ltd_namespace_done = 0;
3119 ltd->ltd_synced_failures = 0;
3120 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_NAMESPACE);
3121 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3122 list_del_init(<d->ltd_layout_phase_list);
3123 list_del_init(<d->ltd_layout_list);
3124 list_del_init(<d->ltd_namespace_phase_list);
3125 list_del_init(<d->ltd_namespace_list);
3127 up_read(<ds->ltd_rw_sem);
3129 ltds = &lfsck->li_ost_descs;
3130 down_read(<ds->ltd_rw_sem);
3131 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
3132 ltd = lfsck_ltd2tgt(ltds, idx);
3133 LASSERT(ltd != NULL);
3135 ltd->ltd_layout_done = 0;
3136 ltd->ltd_synced_failures = 0;
3137 lfsck_reset_ltd_status(ltd, LFSCK_TYPE_LAYOUT);
3138 list_del_init(<d->ltd_layout_phase_list);
3139 list_del_init(<d->ltd_layout_list);
3141 up_read(<ds->ltd_rw_sem);
3144 lfsck->li_args_dir = LUDA_64BITHASH | LUDA_VERIFY | LUDA_TYPE;
3145 if (bk->lb_param & LPF_DRYRUN)
3146 lfsck->li_args_dir |= LUDA_VERIFY_DRYRUN;
3148 if (start != NULL && start->ls_valid & LSV_ERROR_HANDLE) {
3149 valid |= DOIV_ERROR_HANDLE;
3150 if (start->ls_flags & LPF_FAILOUT)
3151 flags |= DOIF_FAILOUT;
3154 if (start != NULL && start->ls_valid & LSV_DRYRUN) {
3155 valid |= DOIV_DRYRUN;
3156 if (start->ls_flags & LPF_DRYRUN)
3157 flags |= DOIF_DRYRUN;
3160 if (!list_empty(&lfsck->li_list_scan))
3161 flags |= DOIF_OUTUSED;
3163 lfsck->li_args_oit = (flags << DT_OTABLE_IT_FLAGS_SHIFT) | valid;
3164 thread_set_flags(thread, 0);
3165 lta = lfsck_thread_args_init(lfsck, NULL, lsp);
3167 GOTO(out, rc = PTR_ERR(lta));
3169 __lfsck_set_speed(lfsck, bk->lb_speed_limit);
3170 task = kthread_run(lfsck_master_engine, lta, "lfsck");
3173 CERROR("%s: cannot start LFSCK thread: rc = %d\n",
3174 lfsck_lfsck2name(lfsck), rc);
3175 lfsck_thread_args_fini(lta);
3180 l_wait_event(thread->t_ctl_waitq,
3181 thread_is_running(thread) ||
3182 thread_is_stopped(thread),
3184 if (start == NULL || !(start->ls_flags & LPF_BROADCAST)) {
3185 lfsck->li_start_unplug = 1;
3186 wake_up_all(&thread->t_ctl_waitq);
3191 /* release lfsck::li_mutex to avoid deadlock. */
3192 mutex_unlock(&lfsck->li_mutex);
3193 rc = lfsck_start_all(env, lfsck, start);
3195 spin_lock(&lfsck->li_lock);
3196 if (thread_is_stopped(thread)) {
3197 spin_unlock(&lfsck->li_lock);
3199 lfsck->li_status = LS_FAILED;
3200 lfsck->li_flags = 0;
3201 thread_set_flags(thread, SVC_STOPPING);
3202 spin_unlock(&lfsck->li_lock);
3204 lfsck->li_start_unplug = 1;
3205 wake_up_all(&thread->t_ctl_waitq);
3206 l_wait_event(thread->t_ctl_waitq,
3207 thread_is_stopped(thread),
3211 lfsck->li_start_unplug = 1;
3212 wake_up_all(&thread->t_ctl_waitq);
3218 mutex_unlock(&lfsck->li_mutex);
3221 lfsck_instance_put(env, lfsck);
3223 return rc < 0 ? rc : 0;
3225 EXPORT_SYMBOL(lfsck_start);
3227 int lfsck_stop(const struct lu_env *env, struct dt_device *key,
3228 struct lfsck_stop *stop)
3230 struct lfsck_instance *lfsck;
3231 struct ptlrpc_thread *thread;
3232 struct l_wait_info lwi = { 0 };
3237 lfsck = lfsck_instance_find(key, true, false);
3238 if (unlikely(lfsck == NULL))
3241 thread = &lfsck->li_thread;
3242 /* release lfsck::li_mutex to avoid deadlock. */
3243 if (stop != NULL && stop->ls_flags & LPF_BROADCAST) {
3244 if (!lfsck->li_master) {
3245 CERROR("%s: only allow to specify '-A' via MDS\n",
3246 lfsck_lfsck2name(lfsck));
3248 GOTO(out, rc = -EPERM);
3251 rc1 = lfsck_stop_all(env, lfsck, stop);
3254 mutex_lock(&lfsck->li_mutex);
3255 spin_lock(&lfsck->li_lock);
3256 /* no error if LFSCK is already stopped, or was never started */
3257 if (thread_is_init(thread) || thread_is_stopped(thread)) {
3258 spin_unlock(&lfsck->li_lock);
3263 lfsck->li_status = stop->ls_status;
3264 lfsck->li_flags = stop->ls_flags;
3266 lfsck->li_status = LS_STOPPED;
3267 lfsck->li_flags = 0;
3270 thread_set_flags(thread, SVC_STOPPING);
3272 if (lfsck->li_master) {
3273 struct lfsck_component *com;
3274 struct lfsck_assistant_data *lad;
3276 list_for_each_entry(com, &lfsck->li_list_scan, lc_link) {
3278 spin_lock(&lad->lad_lock);
3279 if (lad->lad_task != NULL)
3280 force_sig(SIGINT, lad->lad_task);
3281 spin_unlock(&lad->lad_lock);
3284 list_for_each_entry(com, &lfsck->li_list_double_scan, lc_link) {
3286 spin_lock(&lad->lad_lock);
3287 if (lad->lad_task != NULL)
3288 force_sig(SIGINT, lad->lad_task);
3289 spin_unlock(&lad->lad_lock);
3293 spin_unlock(&lfsck->li_lock);
3295 wake_up_all(&thread->t_ctl_waitq);
3296 l_wait_event(thread->t_ctl_waitq,
3297 thread_is_stopped(thread),
3303 mutex_unlock(&lfsck->li_mutex);
3304 lfsck_instance_put(env, lfsck);
3306 return rc != 0 ? rc : rc1;
3308 EXPORT_SYMBOL(lfsck_stop);
3310 int lfsck_in_notify(const struct lu_env *env, struct dt_device *key,
3311 struct lfsck_request *lr, struct thandle *th)
3313 int rc = -EOPNOTSUPP;
3316 switch (lr->lr_event) {
3318 struct lfsck_start *start = &lfsck_env_info(env)->lti_start;
3319 struct lfsck_start_param lsp;
3321 memset(start, 0, sizeof(*start));
3322 start->ls_valid = lr->lr_valid;
3323 start->ls_speed_limit = lr->lr_speed;
3324 start->ls_version = lr->lr_version;
3325 start->ls_active = lr->lr_active;
3326 start->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3327 start->ls_async_windows = lr->lr_async_windows;
3329 lsp.lsp_start = start;
3330 lsp.lsp_index = lr->lr_index;
3331 lsp.lsp_index_valid = 1;
3332 rc = lfsck_start(env, key, &lsp);
3336 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3338 memset(stop, 0, sizeof(*stop));
3339 stop->ls_status = lr->lr_status;
3340 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3341 rc = lfsck_stop(env, key, stop);
3344 case LE_PHASE1_DONE:
3345 case LE_PHASE2_DONE:
3346 case LE_FID_ACCESSED:
3348 case LE_CONDITIONAL_DESTROY:
3349 case LE_SKIP_NLINK_DECLARE:
3351 case LE_SET_LMV_MASTER:
3352 case LE_SET_LMV_SLAVE:
3353 case LE_PAIRS_VERIFY: {
3354 struct lfsck_instance *lfsck;
3355 struct lfsck_component *com;
3357 lfsck = lfsck_instance_find(key, true, false);
3358 if (unlikely(lfsck == NULL))
3361 com = lfsck_component_find(lfsck, lr->lr_active);
3362 if (likely(com != NULL)) {
3363 rc = com->lc_ops->lfsck_in_notify(env, com, lr, th);
3364 lfsck_component_put(env, com);
3367 lfsck_instance_put(env, lfsck);
3376 EXPORT_SYMBOL(lfsck_in_notify);
3378 int lfsck_query(const struct lu_env *env, struct dt_device *key,
3379 struct lfsck_request *req, struct lfsck_reply *rep,
3380 struct lfsck_query *que)
3382 struct lfsck_instance *lfsck;
3383 struct lfsck_component *com;
3389 lfsck = lfsck_instance_find(key, true, false);
3390 if (unlikely(lfsck == NULL))
3394 if (que->lu_types == LFSCK_TYPES_ALL)
3396 LFSCK_TYPES_SUPPORTED & ~LFSCK_TYPE_SCRUB;
3398 if (que->lu_types & ~LFSCK_TYPES_SUPPORTED) {
3399 que->lu_types &= ~LFSCK_TYPES_SUPPORTED;
3401 GOTO(out, rc = -ENOTSUPP);
3404 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3405 i++, type = 1 << i) {
3406 if (!(que->lu_types & type))
3410 com = lfsck_component_find(lfsck, type);
3411 if (unlikely(com == NULL))
3412 GOTO(out, rc = -ENOTSUPP);
3414 memset(que->lu_mdts_count[i], 0,
3415 sizeof(__u32) * (LS_MAX + 1));
3416 memset(que->lu_osts_count[i], 0,
3417 sizeof(__u32) * (LS_MAX + 1));
3418 que->lu_repaired[i] = 0;
3419 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3421 lfsck_component_put(env, com);
3426 if (!(que->lu_flags & LPF_WAIT))
3429 for (i = 0, type = 1 << i; i < LFSCK_TYPE_BITS;
3430 i++, type = 1 << i) {
3431 if (!(que->lu_types & type))
3434 if (que->lu_mdts_count[i][LS_SCANNING_PHASE1] != 0 ||
3435 que->lu_mdts_count[i][LS_SCANNING_PHASE2] != 0 ||
3436 que->lu_osts_count[i][LS_SCANNING_PHASE1] != 0 ||
3437 que->lu_osts_count[i][LS_SCANNING_PHASE2] != 0) {
3438 struct l_wait_info lwi;
3440 /* If it is required to wait, then sleep
3441 * 3 seconds and try to query again. */
3442 lwi = LWI_TIMEOUT_INTR(cfs_time_seconds(3),
3446 rc = l_wait_event(lfsck->li_thread.t_ctl_waitq,
3448 if (rc == -ETIMEDOUT)
3453 com = lfsck_component_find(lfsck, req->lr_active);
3454 if (likely(com != NULL)) {
3455 rc = com->lc_ops->lfsck_query(env, com, req, rep,
3457 lfsck_component_put(env, com);
3466 lfsck_instance_put(env, lfsck);
3469 EXPORT_SYMBOL(lfsck_query);
3471 int lfsck_register_namespace(const struct lu_env *env, struct dt_device *key,
3472 struct ldlm_namespace *ns)
3474 struct lfsck_instance *lfsck;
3477 lfsck = lfsck_instance_find(key, true, false);
3478 if (likely(lfsck != NULL)) {
3479 lfsck->li_namespace = ns;
3480 lfsck_instance_put(env, lfsck);
3486 EXPORT_SYMBOL(lfsck_register_namespace);
3488 int lfsck_register(const struct lu_env *env, struct dt_device *key,
3489 struct dt_device *next, struct obd_device *obd,
3490 lfsck_out_notify notify, void *notify_data, bool master)
3492 struct lfsck_instance *lfsck;
3493 struct dt_object *root = NULL;
3494 struct dt_object *obj = NULL;
3495 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
3499 lfsck = lfsck_instance_find(key, false, false);
3500 if (unlikely(lfsck != NULL))
3503 OBD_ALLOC_PTR(lfsck);
3507 mutex_init(&lfsck->li_mutex);
3508 spin_lock_init(&lfsck->li_lock);
3509 INIT_LIST_HEAD(&lfsck->li_link);
3510 INIT_LIST_HEAD(&lfsck->li_list_scan);
3511 INIT_LIST_HEAD(&lfsck->li_list_dir);
3512 INIT_LIST_HEAD(&lfsck->li_list_double_scan);
3513 INIT_LIST_HEAD(&lfsck->li_list_idle);
3514 INIT_LIST_HEAD(&lfsck->li_list_lmv);
3515 atomic_set(&lfsck->li_ref, 1);
3516 atomic_set(&lfsck->li_double_scan_count, 0);
3517 init_waitqueue_head(&lfsck->li_thread.t_ctl_waitq);
3518 lfsck->li_out_notify = notify;
3519 lfsck->li_out_notify_data = notify_data;
3520 lfsck->li_next = next;
3521 lfsck->li_bottom = key;
3522 lfsck->li_obd = obd;
3524 rc = lfsck_tgt_descs_init(&lfsck->li_ost_descs);
3528 rc = lfsck_tgt_descs_init(&lfsck->li_mdt_descs);
3532 fid->f_seq = FID_SEQ_LOCAL_NAME;
3535 rc = local_oid_storage_init(env, key, fid, &lfsck->li_los);
3539 rc = dt_root_get(env, key, fid);
3543 root = dt_locate(env, key, fid);
3545 GOTO(out, rc = PTR_ERR(root));
3547 if (unlikely(!dt_try_as_dir(env, root)))
3548 GOTO(out, rc = -ENOTDIR);
3550 lfsck->li_local_root_fid = *fid;
3552 lfsck->li_master = 1;
3553 if (lfsck_dev_idx(lfsck) == 0) {
3554 struct lu_fid *pfid = &lfsck_env_info(env)->lti_fid2;
3555 const struct lu_name *cname;
3557 rc = dt_lookup(env, root,
3558 (struct dt_rec *)(&lfsck->li_global_root_fid),
3559 (const struct dt_key *)"ROOT");
3563 obj = dt_locate(env, key, &lfsck->li_global_root_fid);
3565 GOTO(out, rc = PTR_ERR(obj));
3567 if (unlikely(!dt_try_as_dir(env, obj)))
3568 GOTO(out, rc = -ENOTDIR);
3570 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3571 (const struct dt_key *)dotlustre);
3575 lfsck_object_put(env, obj);
3576 obj = dt_locate(env, key, fid);
3578 GOTO(out, rc = PTR_ERR(obj));
3580 cname = lfsck_name_get_const(env, dotlustre,
3582 rc = lfsck_verify_linkea(env, obj, cname,
3583 &lfsck->li_global_root_fid);
3587 if (unlikely(!dt_try_as_dir(env, obj)))
3588 GOTO(out, rc = -ENOTDIR);
3591 rc = dt_lookup(env, obj, (struct dt_rec *)fid,
3592 (const struct dt_key *)lostfound);
3596 lfsck_object_put(env, obj);
3597 obj = dt_locate(env, key, fid);
3599 GOTO(out, rc = PTR_ERR(obj));
3601 cname = lfsck_name_get_const(env, lostfound,
3603 rc = lfsck_verify_linkea(env, obj, cname, pfid);
3607 lfsck_object_put(env, obj);
3612 fid->f_seq = FID_SEQ_LOCAL_FILE;
3613 fid->f_oid = OTABLE_IT_OID;
3615 obj = dt_locate(env, key, fid);
3617 GOTO(out, rc = PTR_ERR(obj));
3619 rc = obj->do_ops->do_index_try(env, obj, &dt_otable_features);
3623 lfsck->li_obj_oit = obj;
3624 obj = local_file_find_or_create(env, lfsck->li_los, root, LFSCK_DIR,
3625 S_IFDIR | S_IRUGO | S_IWUSR);
3627 GOTO(out, rc = PTR_ERR(obj));
3629 lu_object_get(&obj->do_lu);
3630 lfsck->li_lfsck_dir = obj;
3631 rc = lfsck_bookmark_setup(env, lfsck);
3636 rc = lfsck_fid_init(lfsck);
3640 rc = lfsck_namespace_setup(env, lfsck);
3645 rc = lfsck_layout_setup(env, lfsck);
3649 /* XXX: more LFSCK components initialization to be added here. */
3651 rc = lfsck_instance_add(lfsck);
3653 rc = lfsck_add_target_from_orphan(env, lfsck);
3655 if (obj != NULL && !IS_ERR(obj))
3656 lfsck_object_put(env, obj);
3657 if (root != NULL && !IS_ERR(root))
3658 lfsck_object_put(env, root);
3660 lfsck_instance_cleanup(env, lfsck);
3663 EXPORT_SYMBOL(lfsck_register);
3665 void lfsck_degister(const struct lu_env *env, struct dt_device *key)
3667 struct lfsck_instance *lfsck;
3669 lfsck = lfsck_instance_find(key, false, true);
3671 lfsck_instance_put(env, lfsck);
3673 EXPORT_SYMBOL(lfsck_degister);
3675 int lfsck_add_target(const struct lu_env *env, struct dt_device *key,
3676 struct dt_device *tgt, struct obd_export *exp,
3677 __u32 index, bool for_ost)
3679 struct lfsck_instance *lfsck;
3680 struct lfsck_tgt_desc *ltd;
3691 INIT_LIST_HEAD(<d->ltd_orphan_list);
3692 INIT_LIST_HEAD(<d->ltd_layout_list);
3693 INIT_LIST_HEAD(<d->ltd_layout_phase_list);
3694 INIT_LIST_HEAD(<d->ltd_namespace_list);
3695 INIT_LIST_HEAD(<d->ltd_namespace_phase_list);
3696 atomic_set(<d->ltd_ref, 1);
3697 ltd->ltd_index = index;
3699 spin_lock(&lfsck_instance_lock);
3700 lfsck = __lfsck_instance_find(key, true, false);
3701 if (lfsck == NULL) {
3703 list_add_tail(<d->ltd_orphan_list,
3704 &lfsck_ost_orphan_list);
3706 list_add_tail(<d->ltd_orphan_list,
3707 &lfsck_mdt_orphan_list);
3708 spin_unlock(&lfsck_instance_lock);
3712 spin_unlock(&lfsck_instance_lock);
3714 rc = __lfsck_add_target(env, lfsck, ltd, for_ost, false);
3718 lfsck_instance_put(env, lfsck);
3722 EXPORT_SYMBOL(lfsck_add_target);
3724 void lfsck_del_target(const struct lu_env *env, struct dt_device *key,
3725 struct dt_device *tgt, __u32 index, bool for_ost)
3727 struct lfsck_instance *lfsck;
3728 struct lfsck_tgt_descs *ltds;
3729 struct lfsck_tgt_desc *ltd;
3730 struct list_head *head;
3733 head = &lfsck_ost_orphan_list;
3735 head = &lfsck_mdt_orphan_list;
3737 spin_lock(&lfsck_instance_lock);
3738 list_for_each_entry(ltd, head, ltd_orphan_list) {
3739 if (ltd->ltd_tgt == tgt) {
3740 list_del_init(<d->ltd_orphan_list);
3741 spin_unlock(&lfsck_instance_lock);
3749 lfsck = __lfsck_instance_find(key, true, false);
3750 spin_unlock(&lfsck_instance_lock);
3751 if (unlikely(lfsck == NULL))
3755 ltds = &lfsck->li_ost_descs;
3757 ltds = &lfsck->li_mdt_descs;
3759 down_write(<ds->ltd_rw_sem);
3760 LASSERT(ltds->ltd_tgts_bitmap != NULL);
3762 if (unlikely(index >= ltds->ltd_tgts_bitmap->size))
3765 ltd = lfsck_ltd2tgt(ltds, index);
3766 if (unlikely(ltd == NULL))
3769 LASSERT(ltds->ltd_tgtnr > 0);
3772 cfs_bitmap_clear(ltds->ltd_tgts_bitmap, index);
3773 lfsck_assign_tgt(ltds, NULL, index);
3778 head = &lfsck->li_ost_descs.ltd_orphan;
3780 head = &lfsck->li_mdt_descs.ltd_orphan;
3782 list_for_each_entry(ltd, head, ltd_orphan_list) {
3783 if (ltd->ltd_tgt == tgt) {
3784 list_del_init(<d->ltd_orphan_list);
3790 up_write(<ds->ltd_rw_sem);
3792 spin_lock(<ds->ltd_lock);
3794 spin_unlock(<ds->ltd_lock);
3795 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_NAMESPACE);
3796 lfsck_stop_notify(env, lfsck, ltds, ltd, LFSCK_TYPE_LAYOUT);
3800 lfsck_instance_put(env, lfsck);
3802 EXPORT_SYMBOL(lfsck_del_target);
3804 static int __init lfsck_init(void)
3808 INIT_LIST_HEAD(&lfsck_instance_list);
3809 INIT_LIST_HEAD(&lfsck_ost_orphan_list);
3810 INIT_LIST_HEAD(&lfsck_mdt_orphan_list);
3811 lfsck_key_init_generic(&lfsck_thread_key, NULL);
3812 rc = lu_context_key_register(&lfsck_thread_key);
3814 tgt_register_lfsck_in_notify(lfsck_in_notify);
3815 tgt_register_lfsck_query(lfsck_query);
3821 static void __exit lfsck_exit(void)
3823 struct lfsck_tgt_desc *ltd;
3824 struct lfsck_tgt_desc *next;
3826 LASSERT(list_empty(&lfsck_instance_list));
3828 list_for_each_entry_safe(ltd, next, &lfsck_ost_orphan_list,
3830 list_del_init(<d->ltd_orphan_list);
3834 list_for_each_entry_safe(ltd, next, &lfsck_mdt_orphan_list,
3836 list_del_init(<d->ltd_orphan_list);
3840 lu_context_key_degister(&lfsck_thread_key);
3843 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
3844 MODULE_DESCRIPTION("Lustre File System Checker");
3845 MODULE_VERSION(LUSTRE_VERSION_STRING);
3846 MODULE_LICENSE("GPL");
3848 module_init(lfsck_init);
3849 module_exit(lfsck_exit);