4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_linkea.h>
38 #include <lustre_fid.h>
39 #include <lustre_lib.h>
40 #include <lustre_net.h>
41 #include <lustre/lustre_user.h>
43 #include "lfsck_internal.h"
45 #define LFSCK_NAMESPACE_MAGIC 0xA0629D03
47 static const char lfsck_namespace_name[] = "lfsck_namespace";
49 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
50 struct lfsck_namespace *src)
52 dst->ln_magic = le32_to_cpu(src->ln_magic);
53 dst->ln_status = le32_to_cpu(src->ln_status);
54 dst->ln_flags = le32_to_cpu(src->ln_flags);
55 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
56 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
57 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
58 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
59 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
60 dst->ln_time_last_checkpoint =
61 le64_to_cpu(src->ln_time_last_checkpoint);
62 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
63 &src->ln_pos_latest_start);
64 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
65 &src->ln_pos_last_checkpoint);
66 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
67 &src->ln_pos_first_inconsistent);
68 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
69 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
70 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
71 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
72 dst->ln_mlinked_checked = le64_to_cpu(src->ln_mlinked_checked);
73 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
74 dst->ln_objs_repaired_phase2 =
75 le64_to_cpu(src->ln_objs_repaired_phase2);
76 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
77 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
78 dst->ln_objs_lost_found = le64_to_cpu(src->ln_objs_lost_found);
79 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
80 &src->ln_fid_latest_scanned_phase2);
81 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
82 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
85 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
86 struct lfsck_namespace *src)
88 dst->ln_magic = cpu_to_le32(src->ln_magic);
89 dst->ln_status = cpu_to_le32(src->ln_status);
90 dst->ln_flags = cpu_to_le32(src->ln_flags);
91 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
92 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
93 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
94 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
95 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
96 dst->ln_time_last_checkpoint =
97 cpu_to_le64(src->ln_time_last_checkpoint);
98 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
99 &src->ln_pos_latest_start);
100 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
101 &src->ln_pos_last_checkpoint);
102 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
103 &src->ln_pos_first_inconsistent);
104 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
105 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
106 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
107 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
108 dst->ln_mlinked_checked = cpu_to_le64(src->ln_mlinked_checked);
109 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
110 dst->ln_objs_repaired_phase2 =
111 cpu_to_le64(src->ln_objs_repaired_phase2);
112 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
113 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
114 dst->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
115 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
116 &src->ln_fid_latest_scanned_phase2);
117 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
118 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
122 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
123 * \retval 0: succeed.
124 * \retval -ve: failed cases.
126 static int lfsck_namespace_load(const struct lu_env *env,
127 struct lfsck_component *com)
129 int len = com->lc_file_size;
132 rc = dt_xattr_get(env, com->lc_obj,
133 lfsck_buf_get(env, com->lc_file_disk, len),
134 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
136 struct lfsck_namespace *ns = com->lc_file_ram;
138 lfsck_namespace_le_to_cpu(ns,
139 (struct lfsck_namespace *)com->lc_file_disk);
140 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
141 CWARN("%s: invalid lfsck_namespace magic %#x != %#x\n",
142 lfsck_lfsck2name(com->lc_lfsck), ns->ln_magic,
143 LFSCK_NAMESPACE_MAGIC);
148 } else if (rc != -ENODATA) {
149 CERROR("%s: fail to load lfsck_namespace: expected = %d, "
150 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck), len, rc);
157 static int lfsck_namespace_store(const struct lu_env *env,
158 struct lfsck_component *com, bool init)
160 struct dt_object *obj = com->lc_obj;
161 struct lfsck_instance *lfsck = com->lc_lfsck;
162 struct thandle *handle;
163 int len = com->lc_file_size;
167 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
168 (struct lfsck_namespace *)com->lc_file_ram);
169 handle = dt_trans_create(env, lfsck->li_bottom);
170 if (IS_ERR(handle)) {
171 rc = PTR_ERR(handle);
172 CERROR("%s: fail to create trans for storing lfsck_namespace: "
173 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
177 rc = dt_declare_xattr_set(env, obj,
178 lfsck_buf_get(env, com->lc_file_disk, len),
179 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
181 CERROR("%s: fail to declare trans for storing lfsck_namespace: "
182 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
186 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
188 CERROR("%s: fail to start trans for storing lfsck_namespace: "
189 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
193 rc = dt_xattr_set(env, obj,
194 lfsck_buf_get(env, com->lc_file_disk, len),
195 XATTR_NAME_LFSCK_NAMESPACE,
196 init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
197 handle, BYPASS_CAPA);
199 CERROR("%s: fail to store lfsck_namespace: len = %d, "
200 "rc = %d\n", lfsck_lfsck2name(lfsck), len, rc);
205 dt_trans_stop(env, lfsck->li_bottom, handle);
209 static int lfsck_namespace_init(const struct lu_env *env,
210 struct lfsck_component *com)
212 struct lfsck_namespace *ns = com->lc_file_ram;
215 memset(ns, 0, sizeof(*ns));
216 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
217 ns->ln_status = LS_INIT;
218 down_write(&com->lc_sem);
219 rc = lfsck_namespace_store(env, com, true);
220 up_write(&com->lc_sem);
224 static int lfsck_namespace_lookup(const struct lu_env *env,
225 struct lfsck_component *com,
226 const struct lu_fid *fid, __u8 *flags)
228 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
231 fid_cpu_to_be(key, fid);
232 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)flags,
233 (const struct dt_key *)key, BYPASS_CAPA);
237 static int lfsck_namespace_delete(const struct lu_env *env,
238 struct lfsck_component *com,
239 const struct lu_fid *fid)
241 struct lfsck_instance *lfsck = com->lc_lfsck;
242 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
243 struct thandle *handle;
244 struct dt_object *obj = com->lc_obj;
248 handle = dt_trans_create(env, lfsck->li_bottom);
250 RETURN(PTR_ERR(handle));
252 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid, handle);
256 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
260 fid_cpu_to_be(key, fid);
261 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
267 dt_trans_stop(env, lfsck->li_bottom, handle);
271 static int lfsck_namespace_update(const struct lu_env *env,
272 struct lfsck_component *com,
273 const struct lu_fid *fid,
274 __u8 flags, bool force)
276 struct lfsck_instance *lfsck = com->lc_lfsck;
277 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
278 struct thandle *handle;
279 struct dt_object *obj = com->lc_obj;
285 rc = lfsck_namespace_lookup(env, com, fid, &tf);
286 if (rc != 0 && rc != -ENOENT)
290 if (!force || flags == tf)
294 handle = dt_trans_create(env, lfsck->li_bottom);
296 RETURN(PTR_ERR(handle));
298 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid,
303 handle = dt_trans_create(env, lfsck->li_bottom);
305 RETURN(PTR_ERR(handle));
308 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
309 (const struct dt_key *)fid, handle);
313 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
317 fid_cpu_to_be(key, fid);
319 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
322 CERROR("%s: fail to insert "DFID": rc = %d\n",
323 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), rc);
328 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
329 (const struct dt_key *)key, handle, BYPASS_CAPA, 1);
334 dt_trans_stop(env, lfsck->li_bottom, handle);
338 static int lfsck_namespace_check_exist(const struct lu_env *env,
339 struct lfsck_instance *lfsck,
340 struct dt_object *obj, const char *name)
342 struct dt_object *dir = lfsck->li_obj_dir;
343 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
347 if (unlikely(lfsck_is_dead_obj(obj)))
348 RETURN(LFSCK_NAMEENTRY_DEAD);
350 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
351 (const struct dt_key *)name, BYPASS_CAPA);
353 RETURN(LFSCK_NAMEENTRY_REMOVED);
358 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
359 RETURN(LFSCK_NAMEENTRY_RECREATED);
364 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
365 struct dt_object *obj,
366 struct thandle *handle)
370 /* For destroying all invalid linkEA entries. */
371 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
375 /* For insert new linkEA entry. */
376 rc = dt_declare_xattr_set(env, obj,
377 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
378 XATTR_NAME_LINK, 0, handle);
382 static int lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
383 struct linkea_data *ldata)
388 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
390 if (ldata->ld_buf->lb_buf == NULL)
393 if (!dt_object_exists(obj))
396 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
398 /* Buf was too small, figure out what we need. */
399 lu_buf_free(ldata->ld_buf);
400 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
405 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
406 if (ldata->ld_buf->lb_buf == NULL)
409 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
420 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
421 struct linkea_data *ldata, struct thandle *handle)
423 const struct lu_buf *buf = lfsck_buf_get_const(env,
424 ldata->ld_buf->lb_buf,
425 ldata->ld_leh->leh_len);
427 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
432 * \retval ve: removed entries
434 static int lfsck_linkea_entry_unpack(struct lfsck_instance *lfsck,
435 struct linkea_data *ldata,
436 struct lu_name *cname,
439 struct link_ea_entry *oldlee;
443 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
444 oldlee = ldata->ld_lee;
445 oldlen = ldata->ld_reclen;
446 linkea_next_entry(ldata);
447 while (ldata->ld_lee != NULL) {
448 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
449 ldata->ld_lee->lee_reclen[1];
450 if (unlikely(ldata->ld_reclen == oldlen &&
451 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
452 linkea_del_buf(ldata, cname);
455 linkea_next_entry(ldata);
458 ldata->ld_lee = oldlee;
459 ldata->ld_reclen = oldlen;
464 * \retval +ve repaired
465 * \retval 0 no need to repair
466 * \retval -ve error cases
468 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
469 struct lfsck_component *com,
470 struct dt_object *child, __u8 flags)
472 struct lfsck_thread_info *info = lfsck_env_info(env);
473 struct lu_attr *la = &info->lti_la;
474 struct lu_name *cname = &info->lti_name;
475 struct lu_fid *pfid = &info->lti_fid;
476 struct lu_fid *cfid = &info->lti_fid2;
477 struct lfsck_instance *lfsck = com->lc_lfsck;
478 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
479 struct lfsck_namespace *ns = com->lc_file_ram;
480 struct linkea_data ldata = { 0 };
481 struct thandle *handle = NULL;
487 if (com->lc_journal) {
494 handle = dt_trans_create(env, lfsck->li_next);
496 RETURN(rc = PTR_ERR(handle));
498 rc = dt_declare_xattr_set(env, child,
499 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
500 XATTR_NAME_LINK, 0, handle);
504 rc = dt_trans_start(env, lfsck->li_next, handle);
508 dt_write_lock(env, child, MOR_TGT_CHILD);
512 if (unlikely(lfsck_is_dead_obj(child)))
515 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
517 rc = lfsck_links_read(env, child, &ldata);
519 if ((bk->lb_param & LPF_DRYRUN) &&
520 (rc == -EINVAL || rc == -ENODATA))
526 linkea_first_entry(&ldata);
527 while (ldata.ld_lee != NULL) {
528 struct dt_object *parent = NULL;
530 rc = lfsck_linkea_entry_unpack(lfsck, &ldata, cname, pfid);
534 if (!fid_is_sane(pfid))
537 parent = lfsck_object_find(env, lfsck, pfid);
540 else if (IS_ERR(parent))
541 GOTO(stop, rc = PTR_ERR(parent));
543 if (!dt_object_exists(parent))
546 /* XXX: Currently, skip remote object, the consistency for
547 * remote object will be processed in LFSCK phase III. */
548 if (dt_object_remote(parent)) {
549 lfsck_object_put(env, parent);
550 linkea_next_entry(&ldata);
554 if (unlikely(!dt_try_as_dir(env, parent)))
557 /* To guarantee the 'name' is terminated with '0'. */
558 memcpy(info->lti_key, cname->ln_name, cname->ln_namelen);
559 info->lti_key[cname->ln_namelen] = 0;
560 cname->ln_name = info->lti_key;
561 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
562 (const struct dt_key *)cname->ln_name,
564 if (rc != 0 && rc != -ENOENT) {
565 lfsck_object_put(env, parent);
570 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
571 lfsck_object_put(env, parent);
572 linkea_next_entry(&ldata);
579 /* If there is no name entry in the parent dir and the object
580 * link count is less than the linkea entries count, then the
581 * linkea entry should be removed. */
582 if (ldata.ld_leh->leh_reccount > la->la_nlink)
585 /* XXX: For the case of there is a linkea entry, but without
586 * name entry pointing to the object and its hard links
587 * count is not less than the object name entries count,
588 * then seems we should add the 'missed' name entry back
589 * to namespace, but before LFSCK phase III finished, we
590 * do not know whether the object has some inconsistency
591 * on other MDTs. So now, do NOT add the name entry back
592 * to the namespace, but keep the linkEA entry. LU-2914 */
593 lfsck_object_put(env, parent);
594 linkea_next_entry(&ldata);
599 lfsck_object_put(env, parent);
600 if (bk->lb_param & LPF_DRYRUN)
603 CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
604 PFID(lfsck_dto2fid(child)), cname->ln_namelen, cname->ln_name,
606 linkea_del_buf(&ldata, cname);
611 if (!com->lc_journal) {
616 rc = lfsck_links_write(env, child, &ldata, handle);
623 /* XXX: For the case linkea entries count does not match the object hard
624 * links count, we cannot update the later one simply. Before LFSCK
625 * phase III finished, we cannot know whether there are some remote
626 * name entries to be repaired or not. LU-2914 */
627 if (rc == 0 && !lfsck_is_dead_obj(child) &&
628 ldata.ld_leh != NULL &&
629 ldata.ld_leh->leh_reccount != la->la_nlink)
630 CWARN("%s: the object "DFID" linkEA entry count %u "
631 "may not match its hardlink count %u\n",
632 lfsck_lfsck2name(lfsck), PFID(cfid),
633 ldata.ld_leh->leh_reccount, la->la_nlink);
635 dt_write_unlock(env, child);
639 dt_trans_stop(env, lfsck->li_next, handle);
641 if (rc == 0 && update) {
642 ns->ln_objs_nlink_repaired++;
651 static int lfsck_namespace_reset(const struct lu_env *env,
652 struct lfsck_component *com, bool init)
654 struct lfsck_instance *lfsck = com->lc_lfsck;
655 struct lfsck_namespace *ns = com->lc_file_ram;
656 struct dt_object *root;
657 struct dt_object *dto;
661 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
663 RETURN(PTR_ERR(root));
665 if (unlikely(!dt_try_as_dir(env, root))) {
666 lu_object_put(env, &root->do_lu);
670 down_write(&com->lc_sem);
672 memset(ns, 0, sizeof(*ns));
674 __u32 count = ns->ln_success_count;
675 __u64 last_time = ns->ln_time_last_complete;
677 memset(ns, 0, sizeof(*ns));
678 ns->ln_success_count = count;
679 ns->ln_time_last_complete = last_time;
681 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
682 ns->ln_status = LS_INIT;
684 rc = local_object_unlink(env, lfsck->li_bottom, root,
685 lfsck_namespace_name);
689 lfsck_object_put(env, com->lc_obj);
691 dto = local_index_find_or_create(env, lfsck->li_los, root,
692 lfsck_namespace_name,
693 S_IFREG | S_IRUGO | S_IWUSR,
696 GOTO(out, rc = PTR_ERR(dto));
699 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
703 rc = lfsck_namespace_store(env, com, true);
708 up_write(&com->lc_sem);
709 lu_object_put(env, &root->do_lu);
714 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
717 struct lfsck_namespace *ns = com->lc_file_ram;
719 down_write(&com->lc_sem);
721 com->lc_new_checked++;
722 ns->ln_items_failed++;
723 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
724 lfsck_pos_fill(env, com->lc_lfsck,
725 &ns->ln_pos_first_inconsistent, false);
726 up_write(&com->lc_sem);
729 static int lfsck_namespace_checkpoint(const struct lu_env *env,
730 struct lfsck_component *com, bool init)
732 struct lfsck_instance *lfsck = com->lc_lfsck;
733 struct lfsck_namespace *ns = com->lc_file_ram;
736 if (com->lc_new_checked == 0 && !init)
739 down_write(&com->lc_sem);
742 ns->ln_pos_latest_start = lfsck->li_pos_current;
744 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
745 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
746 HALF_SEC - lfsck->li_time_last_checkpoint);
747 ns->ln_time_last_checkpoint = cfs_time_current_sec();
748 ns->ln_items_checked += com->lc_new_checked;
749 com->lc_new_checked = 0;
752 rc = lfsck_namespace_store(env, com, false);
754 up_write(&com->lc_sem);
758 static int lfsck_namespace_prep(const struct lu_env *env,
759 struct lfsck_component *com,
760 struct lfsck_start_param *lsp)
762 struct lfsck_instance *lfsck = com->lc_lfsck;
763 struct lfsck_namespace *ns = com->lc_file_ram;
764 struct lfsck_position *pos = &com->lc_pos_start;
766 if (ns->ln_status == LS_COMPLETED) {
769 rc = lfsck_namespace_reset(env, com, false);
771 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
777 down_write(&com->lc_sem);
779 ns->ln_time_latest_start = cfs_time_current_sec();
781 spin_lock(&lfsck->li_lock);
782 if (ns->ln_flags & LF_SCANNED_ONCE) {
783 if (!lfsck->li_drop_dryrun ||
784 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
785 ns->ln_status = LS_SCANNING_PHASE2;
786 cfs_list_del_init(&com->lc_link);
787 cfs_list_add_tail(&com->lc_link,
788 &lfsck->li_list_double_scan);
789 if (!cfs_list_empty(&com->lc_link_dir))
790 cfs_list_del_init(&com->lc_link_dir);
791 lfsck_pos_set_zero(pos);
793 ns->ln_status = LS_SCANNING_PHASE1;
794 ns->ln_run_time_phase1 = 0;
795 ns->ln_run_time_phase2 = 0;
796 ns->ln_items_checked = 0;
797 ns->ln_items_repaired = 0;
798 ns->ln_items_failed = 0;
799 ns->ln_dirs_checked = 0;
800 ns->ln_mlinked_checked = 0;
801 ns->ln_objs_checked_phase2 = 0;
802 ns->ln_objs_repaired_phase2 = 0;
803 ns->ln_objs_failed_phase2 = 0;
804 ns->ln_objs_nlink_repaired = 0;
805 ns->ln_objs_lost_found = 0;
806 fid_zero(&ns->ln_fid_latest_scanned_phase2);
807 if (cfs_list_empty(&com->lc_link_dir))
808 cfs_list_add_tail(&com->lc_link_dir,
809 &lfsck->li_list_dir);
810 *pos = ns->ln_pos_first_inconsistent;
813 ns->ln_status = LS_SCANNING_PHASE1;
814 if (cfs_list_empty(&com->lc_link_dir))
815 cfs_list_add_tail(&com->lc_link_dir,
816 &lfsck->li_list_dir);
817 if (!lfsck->li_drop_dryrun ||
818 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
819 *pos = ns->ln_pos_last_checkpoint;
820 pos->lp_oit_cookie++;
822 *pos = ns->ln_pos_first_inconsistent;
825 spin_unlock(&lfsck->li_lock);
827 up_write(&com->lc_sem);
831 static int lfsck_namespace_exec_oit(const struct lu_env *env,
832 struct lfsck_component *com,
833 struct dt_object *obj)
835 down_write(&com->lc_sem);
836 com->lc_new_checked++;
837 if (S_ISDIR(lfsck_object_type(obj)))
838 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
839 up_write(&com->lc_sem);
843 static int lfsck_namespace_exec_dir(const struct lu_env *env,
844 struct lfsck_component *com,
845 struct dt_object *obj,
846 struct lu_dirent *ent)
848 struct lfsck_thread_info *info = lfsck_env_info(env);
849 struct lu_attr *la = &info->lti_la;
850 struct lfsck_instance *lfsck = com->lc_lfsck;
851 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
852 struct lfsck_namespace *ns = com->lc_file_ram;
853 struct linkea_data ldata = { 0 };
854 const struct lu_fid *pfid = lfsck_dto2fid(lfsck->li_obj_dir);
855 const struct lu_fid *cfid = lfsck_dto2fid(obj);
856 const struct lu_name *cname;
857 struct thandle *handle = NULL;
858 bool repaired = false;
866 cname = lfsck_name_get_const(env, ent->lde_name, ent->lde_namelen);
867 down_write(&com->lc_sem);
868 com->lc_new_checked++;
870 if (ent->lde_attrs & LUDA_UPGRADE) {
871 ns->ln_flags |= LF_UPGRADE;
872 ns->ln_dirent_repaired++;
874 } else if (ent->lde_attrs & LUDA_REPAIR) {
875 ns->ln_flags |= LF_INCONSISTENT;
876 ns->ln_dirent_repaired++;
880 if (ent->lde_name[0] == '.' &&
881 (ent->lde_namelen == 1 ||
882 (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
883 fid_seq_is_dot_lustre(fid_seq(&ent->lde_fid))))
886 if (!(bk->lb_param & LPF_DRYRUN) &&
887 (com->lc_journal || repaired)) {
893 handle = dt_trans_create(env, lfsck->li_next);
895 GOTO(out, rc = PTR_ERR(handle));
897 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
901 rc = dt_trans_start(env, lfsck->li_next, handle);
905 dt_write_lock(env, obj, MOR_TGT_CHILD);
909 rc = lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
913 rc = lfsck_links_read(env, obj, &ldata);
915 count = ldata.ld_leh->leh_reccount;
916 rc = linkea_links_find(&ldata, cname, pfid);
918 (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
921 ns->ln_flags |= LF_INCONSISTENT;
922 /* For dir, if there are more than one linkea entries, or the
923 * linkea entry does not match the name entry, then remove all
924 * and add the correct one. */
925 if (S_ISDIR(lfsck_object_type(obj))) {
933 } else if (unlikely(rc == -EINVAL)) {
935 ns->ln_flags |= LF_INCONSISTENT;
936 /* The magic crashed, we are not sure whether there are more
937 * corrupt data in the linkea, so remove all linkea entries. */
941 } else if (rc == -ENODATA) {
943 ns->ln_flags |= LF_UPGRADE;
948 if (bk->lb_param & LPF_DRYRUN) {
949 ns->ln_linkea_repaired++;
954 if (!com->lc_journal)
960 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
967 rc = linkea_data_new(&ldata,
968 &lfsck_env_info(env)->lti_linkea_buf);
973 rc = linkea_add_buf(&ldata, cname, pfid);
977 rc = lfsck_links_write(env, obj, &ldata, handle);
981 count = ldata.ld_leh->leh_reccount;
982 ns->ln_linkea_repaired++;
991 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
996 (la->la_nlink == 1 || S_ISDIR(lfsck_object_type(obj))))
997 /* Usually, it is for single linked object or dir, do nothing.*/
1000 /* Following modification will be in another transaction. */
1001 if (handle != NULL) {
1002 LASSERT(dt_write_locked(env, obj));
1004 dt_write_unlock(env, obj);
1007 dt_trans_stop(env, lfsck->li_next, handle);
1011 ns->ln_mlinked_checked++;
1012 rc = lfsck_namespace_update(env, com, cfid,
1013 count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
1019 dt_write_unlock(env, obj);
1022 dt_trans_stop(env, lfsck->li_next, handle);
1026 ns->ln_items_failed++;
1027 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1028 lfsck_pos_fill(env, lfsck,
1029 &ns->ln_pos_first_inconsistent, false);
1030 if (!(bk->lb_param & LPF_FAILOUT))
1034 ns->ln_items_repaired++;
1035 if (bk->lb_param & LPF_DRYRUN &&
1036 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1037 lfsck_pos_fill(env, lfsck,
1038 &ns->ln_pos_first_inconsistent,
1041 com->lc_journal = 0;
1045 up_write(&com->lc_sem);
1049 static int lfsck_namespace_post(const struct lu_env *env,
1050 struct lfsck_component *com,
1051 int result, bool init)
1053 struct lfsck_instance *lfsck = com->lc_lfsck;
1054 struct lfsck_namespace *ns = com->lc_file_ram;
1057 down_write(&com->lc_sem);
1059 spin_lock(&lfsck->li_lock);
1061 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
1063 ns->ln_status = LS_SCANNING_PHASE2;
1064 ns->ln_flags |= LF_SCANNED_ONCE;
1065 ns->ln_flags &= ~LF_UPGRADE;
1066 cfs_list_del_init(&com->lc_link);
1067 cfs_list_del_init(&com->lc_link_dir);
1068 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1069 } else if (result == 0) {
1070 ns->ln_status = lfsck->li_status;
1071 if (ns->ln_status == 0)
1072 ns->ln_status = LS_STOPPED;
1073 if (ns->ln_status != LS_PAUSED) {
1074 cfs_list_del_init(&com->lc_link);
1075 cfs_list_del_init(&com->lc_link_dir);
1076 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1079 ns->ln_status = LS_FAILED;
1080 cfs_list_del_init(&com->lc_link);
1081 cfs_list_del_init(&com->lc_link_dir);
1082 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1084 spin_unlock(&lfsck->li_lock);
1087 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1088 HALF_SEC - lfsck->li_time_last_checkpoint);
1089 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1090 ns->ln_items_checked += com->lc_new_checked;
1091 com->lc_new_checked = 0;
1094 rc = lfsck_namespace_store(env, com, false);
1096 up_write(&com->lc_sem);
1101 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1104 struct lfsck_instance *lfsck = com->lc_lfsck;
1105 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1106 struct lfsck_namespace *ns = com->lc_file_ram;
1111 down_read(&com->lc_sem);
1112 rc = snprintf(buf, len,
1113 "name: lfsck_namespace\n"
1119 lfsck_status2names(ns->ln_status));
1125 rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1130 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1135 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1136 "time_since_last_completed");
1140 rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1141 "time_since_latest_start");
1145 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1146 "time_since_last_checkpoint");
1150 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1151 "latest_start_position");
1155 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1156 "last_checkpoint_position");
1160 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1161 "first_failure_position");
1165 if (ns->ln_status == LS_SCANNING_PHASE1) {
1166 struct lfsck_position pos;
1167 const struct dt_it_ops *iops;
1168 cfs_duration_t duration = cfs_time_current() -
1169 lfsck->li_time_last_checkpoint;
1170 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1171 __u64 speed = checked;
1172 __u64 new_checked = com->lc_new_checked * HZ;
1173 __u32 rtime = ns->ln_run_time_phase1 +
1174 cfs_duration_sec(duration + HALF_SEC);
1177 do_div(new_checked, duration);
1179 do_div(speed, rtime);
1180 rc = snprintf(buf, len,
1181 "checked_phase1: "LPU64"\n"
1182 "checked_phase2: "LPU64"\n"
1183 "updated_phase1: "LPU64"\n"
1184 "updated_phase2: "LPU64"\n"
1185 "failed_phase1: "LPU64"\n"
1186 "failed_phase2: "LPU64"\n"
1188 "M-linked: "LPU64"\n"
1189 "dirent_repaired: "LPU64"\n"
1190 "linkea_repaired: "LPU64"\n"
1191 "nlinks_repaired: "LPU64"\n"
1192 "lost_found: "LPU64"\n"
1193 "success_count: %u\n"
1194 "run_time_phase1: %u seconds\n"
1195 "run_time_phase2: %u seconds\n"
1196 "average_speed_phase1: "LPU64" items/sec\n"
1197 "average_speed_phase2: N/A\n"
1198 "real-time_speed_phase1: "LPU64" items/sec\n"
1199 "real-time_speed_phase2: N/A\n",
1201 ns->ln_objs_checked_phase2,
1202 ns->ln_items_repaired,
1203 ns->ln_objs_repaired_phase2,
1204 ns->ln_items_failed,
1205 ns->ln_objs_failed_phase2,
1206 ns->ln_dirs_checked,
1207 ns->ln_mlinked_checked,
1208 ns->ln_dirent_repaired,
1209 ns->ln_linkea_repaired,
1210 ns->ln_objs_nlink_repaired,
1211 ns->ln_objs_lost_found,
1212 ns->ln_success_count,
1214 ns->ln_run_time_phase2,
1223 LASSERT(lfsck->li_di_oit != NULL);
1225 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1227 /* The low layer otable-based iteration position may NOT
1228 * exactly match the namespace-based directory traversal
1229 * cookie. Generally, it is not a serious issue. But the
1230 * caller should NOT make assumption on that. */
1231 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1232 if (!lfsck->li_current_oit_processed)
1233 pos.lp_oit_cookie--;
1235 spin_lock(&lfsck->li_lock);
1236 if (lfsck->li_di_dir != NULL) {
1237 pos.lp_dir_cookie = lfsck->li_cookie_dir;
1238 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
1239 fid_zero(&pos.lp_dir_parent);
1240 pos.lp_dir_cookie = 0;
1243 *lfsck_dto2fid(lfsck->li_obj_dir);
1246 fid_zero(&pos.lp_dir_parent);
1247 pos.lp_dir_cookie = 0;
1249 spin_unlock(&lfsck->li_lock);
1250 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1253 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1254 cfs_duration_t duration = cfs_time_current() -
1255 lfsck->li_time_last_checkpoint;
1256 __u64 checked = ns->ln_objs_checked_phase2 +
1257 com->lc_new_checked;
1258 __u64 speed1 = ns->ln_items_checked;
1259 __u64 speed2 = checked;
1260 __u64 new_checked = com->lc_new_checked * HZ;
1261 __u32 rtime = ns->ln_run_time_phase2 +
1262 cfs_duration_sec(duration + HALF_SEC);
1265 do_div(new_checked, duration);
1266 if (ns->ln_run_time_phase1 != 0)
1267 do_div(speed1, ns->ln_run_time_phase1);
1269 do_div(speed2, rtime);
1270 rc = snprintf(buf, len,
1271 "checked_phase1: "LPU64"\n"
1272 "checked_phase2: "LPU64"\n"
1273 "updated_phase1: "LPU64"\n"
1274 "updated_phase2: "LPU64"\n"
1275 "failed_phase1: "LPU64"\n"
1276 "failed_phase2: "LPU64"\n"
1278 "M-linked: "LPU64"\n"
1279 "dirent_repaired: "LPU64"\n"
1280 "linkea_repaired: "LPU64"\n"
1281 "nlinks_repaired: "LPU64"\n"
1282 "lost_found: "LPU64"\n"
1283 "success_count: %u\n"
1284 "run_time_phase1: %u seconds\n"
1285 "run_time_phase2: %u seconds\n"
1286 "average_speed_phase1: "LPU64" items/sec\n"
1287 "average_speed_phase2: "LPU64" objs/sec\n"
1288 "real-time_speed_phase1: N/A\n"
1289 "real-time_speed_phase2: "LPU64" objs/sec\n"
1290 "current_position: "DFID"\n",
1291 ns->ln_items_checked,
1293 ns->ln_items_repaired,
1294 ns->ln_objs_repaired_phase2,
1295 ns->ln_items_failed,
1296 ns->ln_objs_failed_phase2,
1297 ns->ln_dirs_checked,
1298 ns->ln_mlinked_checked,
1299 ns->ln_dirent_repaired,
1300 ns->ln_linkea_repaired,
1301 ns->ln_objs_nlink_repaired,
1302 ns->ln_objs_lost_found,
1303 ns->ln_success_count,
1304 ns->ln_run_time_phase1,
1309 PFID(&ns->ln_fid_latest_scanned_phase2));
1316 __u64 speed1 = ns->ln_items_checked;
1317 __u64 speed2 = ns->ln_objs_checked_phase2;
1319 if (ns->ln_run_time_phase1 != 0)
1320 do_div(speed1, ns->ln_run_time_phase1);
1321 if (ns->ln_run_time_phase2 != 0)
1322 do_div(speed2, ns->ln_run_time_phase2);
1323 rc = snprintf(buf, len,
1324 "checked_phase1: "LPU64"\n"
1325 "checked_phase2: "LPU64"\n"
1326 "updated_phase1: "LPU64"\n"
1327 "updated_phase2: "LPU64"\n"
1328 "failed_phase1: "LPU64"\n"
1329 "failed_phase2: "LPU64"\n"
1331 "M-linked: "LPU64"\n"
1332 "dirent_repaired: "LPU64"\n"
1333 "linkea_repaired: "LPU64"\n"
1334 "nlinks_repaired: "LPU64"\n"
1335 "lost_found: "LPU64"\n"
1336 "success_count: %u\n"
1337 "run_time_phase1: %u seconds\n"
1338 "run_time_phase2: %u seconds\n"
1339 "average_speed_phase1: "LPU64" items/sec\n"
1340 "average_speed_phase2: "LPU64" objs/sec\n"
1341 "real-time_speed_phase1: N/A\n"
1342 "real-time_speed_phase2: N/A\n"
1343 "current_position: N/A\n",
1344 ns->ln_items_checked,
1345 ns->ln_objs_checked_phase2,
1346 ns->ln_items_repaired,
1347 ns->ln_objs_repaired_phase2,
1348 ns->ln_items_failed,
1349 ns->ln_objs_failed_phase2,
1350 ns->ln_dirs_checked,
1351 ns->ln_mlinked_checked,
1352 ns->ln_dirent_repaired,
1353 ns->ln_linkea_repaired,
1354 ns->ln_objs_nlink_repaired,
1355 ns->ln_objs_lost_found,
1356 ns->ln_success_count,
1357 ns->ln_run_time_phase1,
1358 ns->ln_run_time_phase2,
1370 up_read(&com->lc_sem);
1374 static int lfsck_namespace_double_scan_main(void *args)
1376 struct lfsck_thread_args *lta = args;
1377 const struct lu_env *env = <a->lta_env;
1378 struct lfsck_component *com = lta->lta_com;
1379 struct lfsck_instance *lfsck = com->lc_lfsck;
1380 struct ptlrpc_thread *thread = &lfsck->li_thread;
1381 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1382 struct lfsck_namespace *ns = com->lc_file_ram;
1383 struct dt_object *obj = com->lc_obj;
1384 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
1385 struct dt_object *target;
1393 com->lc_new_checked = 0;
1394 com->lc_new_scanned = 0;
1395 com->lc_time_last_checkpoint = cfs_time_current();
1396 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
1397 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1399 di = iops->init(env, obj, 0, BYPASS_CAPA);
1401 GOTO(out, rc = PTR_ERR(di));
1403 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1404 rc = iops->get(env, di, (const struct dt_key *)&fid);
1408 /* Skip the start one, which either has been processed or non-exist. */
1409 rc = iops->next(env, di);
1413 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1417 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1419 struct l_wait_info lwi;
1421 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1423 l_wait_event(thread->t_ctl_waitq,
1424 !thread_is_running(thread),
1428 key = iops->key(env, di);
1429 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1430 target = lfsck_object_find(env, lfsck, &fid);
1431 down_write(&com->lc_sem);
1432 if (target == NULL) {
1435 } else if (IS_ERR(target)) {
1436 rc = PTR_ERR(target);
1440 /* XXX: Currently, skip remote object, the consistency for
1441 * remote object will be processed in LFSCK phase III. */
1442 if (dt_object_exists(target) && !dt_object_remote(target)) {
1443 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1445 rc = lfsck_namespace_double_scan_one(env, com,
1449 lfsck_object_put(env, target);
1452 com->lc_new_checked++;
1453 com->lc_new_scanned++;
1454 ns->ln_fid_latest_scanned_phase2 = fid;
1456 ns->ln_objs_repaired_phase2++;
1458 ns->ln_objs_failed_phase2++;
1459 up_write(&com->lc_sem);
1461 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1462 lfsck_namespace_delete(env, com, &fid);
1463 } else if (rc < 0) {
1464 flags |= LLF_REPAIR_FAILED;
1465 lfsck_namespace_update(env, com, &fid, flags, true);
1468 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1471 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
1472 cfs_time_current())) &&
1473 com->lc_new_checked != 0) {
1474 down_write(&com->lc_sem);
1475 ns->ln_run_time_phase2 +=
1476 cfs_duration_sec(cfs_time_current() +
1477 HALF_SEC - com->lc_time_last_checkpoint);
1478 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1479 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1480 com->lc_new_checked = 0;
1481 rc = lfsck_namespace_store(env, com, false);
1482 up_write(&com->lc_sem);
1486 com->lc_time_last_checkpoint = cfs_time_current();
1487 com->lc_time_next_checkpoint =
1488 com->lc_time_last_checkpoint +
1489 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1492 lfsck_control_speed_by_self(com);
1493 if (unlikely(!thread_is_running(thread)))
1496 rc = iops->next(env, di);
1505 iops->fini(env, di);
1508 down_write(&com->lc_sem);
1509 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1510 HALF_SEC - lfsck->li_time_last_checkpoint);
1511 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1512 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1513 com->lc_new_checked = 0;
1516 com->lc_journal = 0;
1517 ns->ln_status = LS_COMPLETED;
1518 if (!(bk->lb_param & LPF_DRYRUN))
1519 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1520 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
1521 ns->ln_success_count++;
1522 } else if (rc == 0) {
1523 ns->ln_status = lfsck->li_status;
1524 if (ns->ln_status == 0)
1525 ns->ln_status = LS_STOPPED;
1527 ns->ln_status = LS_FAILED;
1530 rc = lfsck_namespace_store(env, com, false);
1531 up_write(&com->lc_sem);
1532 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1533 wake_up_all(&thread->t_ctl_waitq);
1535 lfsck_thread_args_fini(lta);
1540 static int lfsck_namespace_double_scan(const struct lu_env *env,
1541 struct lfsck_component *com)
1543 struct lfsck_instance *lfsck = com->lc_lfsck;
1544 struct lfsck_namespace *ns = com->lc_file_ram;
1545 struct lfsck_thread_args *lta;
1546 struct task_struct *task;
1550 if (unlikely(ns->ln_status != LS_SCANNING_PHASE2))
1553 lta = lfsck_thread_args_init(lfsck, com, NULL);
1555 RETURN(PTR_ERR(lta));
1557 atomic_inc(&lfsck->li_double_scan_count);
1558 task = kthread_run(lfsck_namespace_double_scan_main, lta,
1562 CERROR("%s: cannot start LFSCK namespace thread: rc = %d\n",
1563 lfsck_lfsck2name(lfsck), rc);
1564 atomic_dec(&lfsck->li_double_scan_count);
1565 lfsck_thread_args_fini(lta);
1573 static int lfsck_namespace_in_notify(const struct lu_env *env,
1574 struct lfsck_component *com,
1575 struct lfsck_request *lr)
1580 static int lfsck_namespace_query(const struct lu_env *env,
1581 struct lfsck_component *com)
1583 struct lfsck_namespace *ns = com->lc_file_ram;
1585 return ns->ln_status;
1588 static struct lfsck_operations lfsck_namespace_ops = {
1589 .lfsck_reset = lfsck_namespace_reset,
1590 .lfsck_fail = lfsck_namespace_fail,
1591 .lfsck_checkpoint = lfsck_namespace_checkpoint,
1592 .lfsck_prep = lfsck_namespace_prep,
1593 .lfsck_exec_oit = lfsck_namespace_exec_oit,
1594 .lfsck_exec_dir = lfsck_namespace_exec_dir,
1595 .lfsck_post = lfsck_namespace_post,
1596 .lfsck_dump = lfsck_namespace_dump,
1597 .lfsck_double_scan = lfsck_namespace_double_scan,
1598 .lfsck_in_notify = lfsck_namespace_in_notify,
1599 .lfsck_query = lfsck_namespace_query,
1602 int lfsck_namespace_setup(const struct lu_env *env,
1603 struct lfsck_instance *lfsck)
1605 struct lfsck_component *com;
1606 struct lfsck_namespace *ns;
1607 struct dt_object *root = NULL;
1608 struct dt_object *obj;
1612 LASSERT(lfsck->li_master);
1618 CFS_INIT_LIST_HEAD(&com->lc_link);
1619 CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1620 init_rwsem(&com->lc_sem);
1621 atomic_set(&com->lc_ref, 1);
1622 com->lc_lfsck = lfsck;
1623 com->lc_type = LT_NAMESPACE;
1624 com->lc_ops = &lfsck_namespace_ops;
1625 com->lc_file_size = sizeof(struct lfsck_namespace);
1626 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1627 if (com->lc_file_ram == NULL)
1628 GOTO(out, rc = -ENOMEM);
1630 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1631 if (com->lc_file_disk == NULL)
1632 GOTO(out, rc = -ENOMEM);
1634 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1636 GOTO(out, rc = PTR_ERR(root));
1638 if (unlikely(!dt_try_as_dir(env, root)))
1639 GOTO(out, rc = -ENOTDIR);
1641 obj = local_index_find_or_create(env, lfsck->li_los, root,
1642 lfsck_namespace_name,
1643 S_IFREG | S_IRUGO | S_IWUSR,
1644 &dt_lfsck_features);
1646 GOTO(out, rc = PTR_ERR(obj));
1649 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1653 rc = lfsck_namespace_load(env, com);
1655 rc = lfsck_namespace_reset(env, com, true);
1656 else if (rc == -ENODATA)
1657 rc = lfsck_namespace_init(env, com);
1661 ns = com->lc_file_ram;
1662 switch (ns->ln_status) {
1667 spin_lock(&lfsck->li_lock);
1668 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1669 spin_unlock(&lfsck->li_lock);
1672 CERROR("%s: unknown lfsck_namespace status: rc = %u\n",
1673 lfsck_lfsck2name(lfsck), ns->ln_status);
1675 case LS_SCANNING_PHASE1:
1676 case LS_SCANNING_PHASE2:
1677 /* No need to store the status to disk right now.
1678 * If the system crashed before the status stored,
1679 * it will be loaded back when next time. */
1680 ns->ln_status = LS_CRASHED;
1684 spin_lock(&lfsck->li_lock);
1685 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1686 cfs_list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
1687 spin_unlock(&lfsck->li_lock);
1694 if (root != NULL && !IS_ERR(root))
1695 lu_object_put(env, &root->do_lu);
1697 lfsck_component_cleanup(env, com);