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 *des,
50 struct lfsck_namespace *src)
52 des->ln_magic = le32_to_cpu(src->ln_magic);
53 des->ln_status = le32_to_cpu(src->ln_status);
54 des->ln_flags = le32_to_cpu(src->ln_flags);
55 des->ln_success_count = le32_to_cpu(src->ln_success_count);
56 des->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
57 des->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
58 des->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
59 des->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
60 des->ln_time_last_checkpoint =
61 le64_to_cpu(src->ln_time_last_checkpoint);
62 lfsck_position_le_to_cpu(&des->ln_pos_latest_start,
63 &src->ln_pos_latest_start);
64 lfsck_position_le_to_cpu(&des->ln_pos_last_checkpoint,
65 &src->ln_pos_last_checkpoint);
66 lfsck_position_le_to_cpu(&des->ln_pos_first_inconsistent,
67 &src->ln_pos_first_inconsistent);
68 des->ln_items_checked = le64_to_cpu(src->ln_items_checked);
69 des->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
70 des->ln_items_failed = le64_to_cpu(src->ln_items_failed);
71 des->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
72 des->ln_mlinked_checked = le64_to_cpu(src->ln_mlinked_checked);
73 des->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
74 des->ln_objs_repaired_phase2 =
75 le64_to_cpu(src->ln_objs_repaired_phase2);
76 des->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
77 des->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
78 des->ln_objs_lost_found = le64_to_cpu(src->ln_objs_lost_found);
79 fid_le_to_cpu(&des->ln_fid_latest_scanned_phase2,
80 &src->ln_fid_latest_scanned_phase2);
83 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *des,
84 struct lfsck_namespace *src)
86 des->ln_magic = cpu_to_le32(src->ln_magic);
87 des->ln_status = cpu_to_le32(src->ln_status);
88 des->ln_flags = cpu_to_le32(src->ln_flags);
89 des->ln_success_count = cpu_to_le32(src->ln_success_count);
90 des->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
91 des->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
92 des->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
93 des->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
94 des->ln_time_last_checkpoint =
95 cpu_to_le64(src->ln_time_last_checkpoint);
96 lfsck_position_cpu_to_le(&des->ln_pos_latest_start,
97 &src->ln_pos_latest_start);
98 lfsck_position_cpu_to_le(&des->ln_pos_last_checkpoint,
99 &src->ln_pos_last_checkpoint);
100 lfsck_position_cpu_to_le(&des->ln_pos_first_inconsistent,
101 &src->ln_pos_first_inconsistent);
102 des->ln_items_checked = cpu_to_le64(src->ln_items_checked);
103 des->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
104 des->ln_items_failed = cpu_to_le64(src->ln_items_failed);
105 des->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
106 des->ln_mlinked_checked = cpu_to_le64(src->ln_mlinked_checked);
107 des->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
108 des->ln_objs_repaired_phase2 =
109 cpu_to_le64(src->ln_objs_repaired_phase2);
110 des->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
111 des->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
112 des->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
113 fid_cpu_to_le(&des->ln_fid_latest_scanned_phase2,
114 &src->ln_fid_latest_scanned_phase2);
118 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
119 * \retval 0: succeed.
120 * \retval -ve: failed cases.
122 static int lfsck_namespace_load(const struct lu_env *env,
123 struct lfsck_component *com)
125 int len = com->lc_file_size;
128 rc = dt_xattr_get(env, com->lc_obj,
129 lfsck_buf_get(env, com->lc_file_disk, len),
130 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
132 struct lfsck_namespace *ns = com->lc_file_ram;
134 lfsck_namespace_le_to_cpu(ns,
135 (struct lfsck_namespace *)com->lc_file_disk);
136 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
137 CWARN("%s: invalid lfsck_namespace magic %#x != %#x\n",
138 lfsck_lfsck2name(com->lc_lfsck), ns->ln_magic,
139 LFSCK_NAMESPACE_MAGIC);
144 } else if (rc != -ENODATA) {
145 CERROR("%s: fail to load lfsck_namespace: expected = %d, "
146 "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck), len, rc);
153 static int lfsck_namespace_store(const struct lu_env *env,
154 struct lfsck_component *com, bool init)
156 struct dt_object *obj = com->lc_obj;
157 struct lfsck_instance *lfsck = com->lc_lfsck;
158 struct thandle *handle;
159 int len = com->lc_file_size;
163 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
164 (struct lfsck_namespace *)com->lc_file_ram);
165 handle = dt_trans_create(env, lfsck->li_bottom);
166 if (IS_ERR(handle)) {
167 rc = PTR_ERR(handle);
168 CERROR("%s: fail to create trans for storing lfsck_namespace: "
169 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
173 rc = dt_declare_xattr_set(env, obj,
174 lfsck_buf_get(env, com->lc_file_disk, len),
175 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
177 CERROR("%s: fail to declare trans for storing lfsck_namespace: "
178 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
182 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
184 CERROR("%s: fail to start trans for storing lfsck_namespace: "
185 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
189 rc = dt_xattr_set(env, obj,
190 lfsck_buf_get(env, com->lc_file_disk, len),
191 XATTR_NAME_LFSCK_NAMESPACE,
192 init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
193 handle, BYPASS_CAPA);
195 CERROR("%s: fail to store lfsck_namespace: len = %d, "
196 "rc = %d\n", lfsck_lfsck2name(lfsck), len, rc);
201 dt_trans_stop(env, lfsck->li_bottom, handle);
205 static int lfsck_namespace_init(const struct lu_env *env,
206 struct lfsck_component *com)
208 struct lfsck_namespace *ns = com->lc_file_ram;
211 memset(ns, 0, sizeof(*ns));
212 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
213 ns->ln_status = LS_INIT;
214 down_write(&com->lc_sem);
215 rc = lfsck_namespace_store(env, com, true);
216 up_write(&com->lc_sem);
220 static int lfsck_namespace_lookup(const struct lu_env *env,
221 struct lfsck_component *com,
222 const struct lu_fid *fid, __u8 *flags)
224 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
227 fid_cpu_to_be(key, fid);
228 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)flags,
229 (const struct dt_key *)key, BYPASS_CAPA);
233 static int lfsck_namespace_delete(const struct lu_env *env,
234 struct lfsck_component *com,
235 const struct lu_fid *fid)
237 struct lfsck_instance *lfsck = com->lc_lfsck;
238 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
239 struct thandle *handle;
240 struct dt_object *obj = com->lc_obj;
244 handle = dt_trans_create(env, lfsck->li_bottom);
246 RETURN(PTR_ERR(handle));
248 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid, handle);
252 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
256 fid_cpu_to_be(key, fid);
257 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
263 dt_trans_stop(env, lfsck->li_bottom, handle);
267 static int lfsck_namespace_update(const struct lu_env *env,
268 struct lfsck_component *com,
269 const struct lu_fid *fid,
270 __u8 flags, bool force)
272 struct lfsck_instance *lfsck = com->lc_lfsck;
273 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
274 struct thandle *handle;
275 struct dt_object *obj = com->lc_obj;
281 rc = lfsck_namespace_lookup(env, com, fid, &tf);
282 if (rc != 0 && rc != -ENOENT)
286 if (!force || flags == tf)
290 handle = dt_trans_create(env, lfsck->li_bottom);
292 RETURN(PTR_ERR(handle));
294 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid,
299 handle = dt_trans_create(env, lfsck->li_bottom);
301 RETURN(PTR_ERR(handle));
304 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
305 (const struct dt_key *)fid, handle);
309 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
313 fid_cpu_to_be(key, fid);
315 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
318 CERROR("%s: fail to insert "DFID": rc = %d\n",
319 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), rc);
324 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
325 (const struct dt_key *)key, handle, BYPASS_CAPA, 1);
330 dt_trans_stop(env, lfsck->li_bottom, handle);
334 static int lfsck_namespace_check_exist(const struct lu_env *env,
335 struct lfsck_instance *lfsck,
336 struct dt_object *obj, const char *name)
338 struct dt_object *dir = lfsck->li_obj_dir;
339 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
343 if (unlikely(lfsck_is_dead_obj(obj)))
344 RETURN(LFSCK_NAMEENTRY_DEAD);
346 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
347 (const struct dt_key *)name, BYPASS_CAPA);
349 RETURN(LFSCK_NAMEENTRY_REMOVED);
354 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
355 RETURN(LFSCK_NAMEENTRY_RECREATED);
360 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
361 struct dt_object *obj,
362 struct thandle *handle)
366 /* For destroying all invalid linkEA entries. */
367 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
371 /* For insert new linkEA entry. */
372 rc = dt_declare_xattr_set(env, obj,
373 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
374 XATTR_NAME_LINK, 0, handle);
378 static int lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
379 struct linkea_data *ldata)
384 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
386 if (ldata->ld_buf->lb_buf == NULL)
389 if (!dt_object_exists(obj))
392 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
394 /* Buf was too small, figure out what we need. */
395 lu_buf_free(ldata->ld_buf);
396 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
401 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
402 if (ldata->ld_buf->lb_buf == NULL)
405 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
416 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
417 struct linkea_data *ldata, struct thandle *handle)
419 const struct lu_buf *buf = lfsck_buf_get_const(env,
420 ldata->ld_buf->lb_buf,
421 ldata->ld_leh->leh_len);
423 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
428 * \retval ve: removed entries
430 static int lfsck_linkea_entry_unpack(struct lfsck_instance *lfsck,
431 struct linkea_data *ldata,
432 struct lu_name *cname,
435 struct link_ea_entry *oldlee;
439 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
440 oldlee = ldata->ld_lee;
441 oldlen = ldata->ld_reclen;
442 linkea_next_entry(ldata);
443 while (ldata->ld_lee != NULL) {
444 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
445 ldata->ld_lee->lee_reclen[1];
446 if (unlikely(ldata->ld_reclen == oldlen &&
447 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
448 linkea_del_buf(ldata, cname);
451 linkea_next_entry(ldata);
454 ldata->ld_lee = oldlee;
455 ldata->ld_reclen = oldlen;
460 * \retval +ve repaired
461 * \retval 0 no need to repair
462 * \retval -ve error cases
464 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
465 struct lfsck_component *com,
466 struct dt_object *child, __u8 flags)
468 struct lfsck_thread_info *info = lfsck_env_info(env);
469 struct lu_attr *la = &info->lti_la;
470 struct lu_name *cname = &info->lti_name;
471 struct lu_fid *pfid = &info->lti_fid;
472 struct lu_fid *cfid = &info->lti_fid2;
473 struct lfsck_instance *lfsck = com->lc_lfsck;
474 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
475 struct lfsck_namespace *ns = com->lc_file_ram;
476 struct linkea_data ldata = { 0 };
477 struct thandle *handle = NULL;
483 if (com->lc_journal) {
490 handle = dt_trans_create(env, lfsck->li_next);
492 RETURN(rc = PTR_ERR(handle));
494 rc = dt_declare_xattr_set(env, child,
495 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
496 XATTR_NAME_LINK, 0, handle);
500 rc = dt_trans_start(env, lfsck->li_next, handle);
504 dt_write_lock(env, child, MOR_TGT_CHILD);
508 if (unlikely(lfsck_is_dead_obj(child)))
511 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
513 rc = lfsck_links_read(env, child, &ldata);
515 if ((bk->lb_param & LPF_DRYRUN) &&
516 (rc == -EINVAL || rc == -ENODATA))
522 linkea_first_entry(&ldata);
523 while (ldata.ld_lee != NULL) {
524 struct dt_object *parent = NULL;
526 rc = lfsck_linkea_entry_unpack(lfsck, &ldata, cname, pfid);
530 if (!fid_is_sane(pfid))
533 parent = lfsck_object_find(env, lfsck, pfid);
536 else if (IS_ERR(parent))
537 GOTO(stop, rc = PTR_ERR(parent));
539 if (!dt_object_exists(parent))
542 /* XXX: Currently, skip remote object, the consistency for
543 * remote object will be processed in LFSCK phase III. */
544 if (dt_object_remote(parent)) {
545 lfsck_object_put(env, parent);
546 linkea_next_entry(&ldata);
550 if (unlikely(!dt_try_as_dir(env, parent)))
553 /* To guarantee the 'name' is terminated with '0'. */
554 memcpy(info->lti_key, cname->ln_name, cname->ln_namelen);
555 info->lti_key[cname->ln_namelen] = 0;
556 cname->ln_name = info->lti_key;
557 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
558 (const struct dt_key *)cname->ln_name,
560 if (rc != 0 && rc != -ENOENT) {
561 lfsck_object_put(env, parent);
566 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
567 lfsck_object_put(env, parent);
568 linkea_next_entry(&ldata);
575 /* If there is no name entry in the parent dir and the object
576 * link count is less than the linkea entries count, then the
577 * linkea entry should be removed. */
578 if (ldata.ld_leh->leh_reccount > la->la_nlink)
581 /* XXX: For the case of there is a linkea entry, but without
582 * name entry pointing to the object and its hard links
583 * count is not less than the object name entries count,
584 * then seems we should add the 'missed' name entry back
585 * to namespace, but before LFSCK phase III finished, we
586 * do not know whether the object has some inconsistency
587 * on other MDTs. So now, do NOT add the name entry back
588 * to the namespace, but keep the linkEA entry. LU-2914 */
589 lfsck_object_put(env, parent);
590 linkea_next_entry(&ldata);
595 lfsck_object_put(env, parent);
596 if (bk->lb_param & LPF_DRYRUN)
599 CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
600 PFID(lfsck_dto2fid(child)), cname->ln_namelen, cname->ln_name,
602 linkea_del_buf(&ldata, cname);
607 if (!com->lc_journal) {
612 rc = lfsck_links_write(env, child, &ldata, handle);
619 /* XXX: For the case linkea entries count does not match the object hard
620 * links count, we cannot update the later one simply. Before LFSCK
621 * phase III finished, we cannot know whether there are some remote
622 * name entries to be repaired or not. LU-2914 */
623 if (rc == 0 && !lfsck_is_dead_obj(child) &&
624 ldata.ld_leh != NULL &&
625 ldata.ld_leh->leh_reccount != la->la_nlink)
626 CWARN("%s: the object "DFID" linkEA entry count %u "
627 "may not match its hardlink count %u\n",
628 lfsck_lfsck2name(lfsck), PFID(cfid),
629 ldata.ld_leh->leh_reccount, la->la_nlink);
631 dt_write_unlock(env, child);
635 dt_trans_stop(env, lfsck->li_next, handle);
637 if (rc == 0 && update) {
638 ns->ln_objs_nlink_repaired++;
647 static int lfsck_namespace_reset(const struct lu_env *env,
648 struct lfsck_component *com, bool init)
650 struct lfsck_instance *lfsck = com->lc_lfsck;
651 struct lfsck_namespace *ns = com->lc_file_ram;
652 struct dt_object *root;
653 struct dt_object *dto;
657 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
659 RETURN(PTR_ERR(root));
661 if (unlikely(!dt_try_as_dir(env, root))) {
662 lu_object_put(env, &root->do_lu);
666 down_write(&com->lc_sem);
668 memset(ns, 0, sizeof(*ns));
670 __u32 count = ns->ln_success_count;
671 __u64 last_time = ns->ln_time_last_complete;
673 memset(ns, 0, sizeof(*ns));
674 ns->ln_success_count = count;
675 ns->ln_time_last_complete = last_time;
677 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
678 ns->ln_status = LS_INIT;
680 rc = local_object_unlink(env, lfsck->li_bottom, root,
681 lfsck_namespace_name);
685 lfsck_object_put(env, com->lc_obj);
687 dto = local_index_find_or_create(env, lfsck->li_los, root,
688 lfsck_namespace_name,
689 S_IFREG | S_IRUGO | S_IWUSR,
692 GOTO(out, rc = PTR_ERR(dto));
695 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
699 rc = lfsck_namespace_store(env, com, true);
704 up_write(&com->lc_sem);
705 lu_object_put(env, &root->do_lu);
710 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
713 struct lfsck_namespace *ns = com->lc_file_ram;
715 down_write(&com->lc_sem);
717 com->lc_new_checked++;
718 ns->ln_items_failed++;
719 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
720 lfsck_pos_fill(env, com->lc_lfsck,
721 &ns->ln_pos_first_inconsistent, false);
722 up_write(&com->lc_sem);
725 static int lfsck_namespace_checkpoint(const struct lu_env *env,
726 struct lfsck_component *com, bool init)
728 struct lfsck_instance *lfsck = com->lc_lfsck;
729 struct lfsck_namespace *ns = com->lc_file_ram;
732 if (com->lc_new_checked == 0 && !init)
735 down_write(&com->lc_sem);
738 ns->ln_pos_latest_start = lfsck->li_pos_current;
740 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
741 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
742 HALF_SEC - lfsck->li_time_last_checkpoint);
743 ns->ln_time_last_checkpoint = cfs_time_current_sec();
744 ns->ln_items_checked += com->lc_new_checked;
745 com->lc_new_checked = 0;
748 rc = lfsck_namespace_store(env, com, false);
750 up_write(&com->lc_sem);
754 static int lfsck_namespace_prep(const struct lu_env *env,
755 struct lfsck_component *com,
756 struct lfsck_start_param *lsp)
758 struct lfsck_instance *lfsck = com->lc_lfsck;
759 struct lfsck_namespace *ns = com->lc_file_ram;
760 struct lfsck_position *pos = &com->lc_pos_start;
762 if (ns->ln_status == LS_COMPLETED) {
765 rc = lfsck_namespace_reset(env, com, false);
770 down_write(&com->lc_sem);
772 ns->ln_time_latest_start = cfs_time_current_sec();
774 spin_lock(&lfsck->li_lock);
775 if (ns->ln_flags & LF_SCANNED_ONCE) {
776 if (!lfsck->li_drop_dryrun ||
777 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
778 ns->ln_status = LS_SCANNING_PHASE2;
779 cfs_list_del_init(&com->lc_link);
780 cfs_list_add_tail(&com->lc_link,
781 &lfsck->li_list_double_scan);
782 if (!cfs_list_empty(&com->lc_link_dir))
783 cfs_list_del_init(&com->lc_link_dir);
784 lfsck_pos_set_zero(pos);
786 ns->ln_status = LS_SCANNING_PHASE1;
787 ns->ln_run_time_phase1 = 0;
788 ns->ln_run_time_phase2 = 0;
789 ns->ln_items_checked = 0;
790 ns->ln_items_repaired = 0;
791 ns->ln_items_failed = 0;
792 ns->ln_dirs_checked = 0;
793 ns->ln_mlinked_checked = 0;
794 ns->ln_objs_checked_phase2 = 0;
795 ns->ln_objs_repaired_phase2 = 0;
796 ns->ln_objs_failed_phase2 = 0;
797 ns->ln_objs_nlink_repaired = 0;
798 ns->ln_objs_lost_found = 0;
799 fid_zero(&ns->ln_fid_latest_scanned_phase2);
800 if (cfs_list_empty(&com->lc_link_dir))
801 cfs_list_add_tail(&com->lc_link_dir,
802 &lfsck->li_list_dir);
803 *pos = ns->ln_pos_first_inconsistent;
806 ns->ln_status = LS_SCANNING_PHASE1;
807 if (cfs_list_empty(&com->lc_link_dir))
808 cfs_list_add_tail(&com->lc_link_dir,
809 &lfsck->li_list_dir);
810 if (!lfsck->li_drop_dryrun ||
811 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
812 *pos = ns->ln_pos_last_checkpoint;
813 pos->lp_oit_cookie++;
815 *pos = ns->ln_pos_first_inconsistent;
818 spin_unlock(&lfsck->li_lock);
820 up_write(&com->lc_sem);
824 static int lfsck_namespace_exec_oit(const struct lu_env *env,
825 struct lfsck_component *com,
826 struct dt_object *obj)
828 down_write(&com->lc_sem);
829 com->lc_new_checked++;
830 if (S_ISDIR(lfsck_object_type(obj)))
831 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
832 up_write(&com->lc_sem);
836 static int lfsck_namespace_exec_dir(const struct lu_env *env,
837 struct lfsck_component *com,
838 struct dt_object *obj,
839 struct lu_dirent *ent)
841 struct lfsck_thread_info *info = lfsck_env_info(env);
842 struct lu_attr *la = &info->lti_la;
843 struct lfsck_instance *lfsck = com->lc_lfsck;
844 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
845 struct lfsck_namespace *ns = com->lc_file_ram;
846 struct linkea_data ldata = { 0 };
847 const struct lu_fid *pfid = lfsck_dto2fid(lfsck->li_obj_dir);
848 const struct lu_fid *cfid = lfsck_dto2fid(obj);
849 const struct lu_name *cname;
850 struct thandle *handle = NULL;
851 bool repaired = false;
859 cname = lfsck_name_get_const(env, ent->lde_name, ent->lde_namelen);
860 down_write(&com->lc_sem);
861 com->lc_new_checked++;
863 if (ent->lde_attrs & LUDA_UPGRADE) {
864 ns->ln_flags |= LF_UPGRADE;
866 } else if (ent->lde_attrs & LUDA_REPAIR) {
867 ns->ln_flags |= LF_INCONSISTENT;
871 if (ent->lde_name[0] == '.' &&
872 (ent->lde_namelen == 1 ||
873 (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
874 fid_is_dot_lustre(&ent->lde_fid)))
877 if (!(bk->lb_param & LPF_DRYRUN) &&
878 (com->lc_journal || repaired)) {
884 handle = dt_trans_create(env, lfsck->li_next);
886 GOTO(out, rc = PTR_ERR(handle));
888 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
892 rc = dt_trans_start(env, lfsck->li_next, handle);
896 dt_write_lock(env, obj, MOR_TGT_CHILD);
900 rc = lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
904 rc = lfsck_links_read(env, obj, &ldata);
906 count = ldata.ld_leh->leh_reccount;
907 rc = linkea_links_find(&ldata, cname, pfid);
909 (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
912 ns->ln_flags |= LF_INCONSISTENT;
913 /* For dir, if there are more than one linkea entries, or the
914 * linkea entry does not match the name entry, then remove all
915 * and add the correct one. */
916 if (S_ISDIR(lfsck_object_type(obj))) {
924 } else if (unlikely(rc == -EINVAL)) {
926 ns->ln_flags |= LF_INCONSISTENT;
927 /* The magic crashed, we are not sure whether there are more
928 * corrupt data in the linkea, so remove all linkea entries. */
932 } else if (rc == -ENODATA) {
934 ns->ln_flags |= LF_UPGRADE;
939 if (bk->lb_param & LPF_DRYRUN) {
944 if (!com->lc_journal)
950 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
957 rc = linkea_data_new(&ldata,
958 &lfsck_env_info(env)->lti_linkea_buf);
963 rc = linkea_add_buf(&ldata, cname, pfid);
967 rc = lfsck_links_write(env, obj, &ldata, handle);
971 count = ldata.ld_leh->leh_reccount;
980 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
985 (la->la_nlink == 1 || S_ISDIR(lfsck_object_type(obj))))
986 /* Usually, it is for single linked object or dir, do nothing.*/
989 /* Following modification will be in another transaction. */
990 if (handle != NULL) {
991 LASSERT(dt_write_locked(env, obj));
993 dt_write_unlock(env, obj);
996 dt_trans_stop(env, lfsck->li_next, handle);
1000 ns->ln_mlinked_checked++;
1001 rc = lfsck_namespace_update(env, com, cfid,
1002 count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
1008 dt_write_unlock(env, obj);
1011 dt_trans_stop(env, lfsck->li_next, handle);
1015 ns->ln_items_failed++;
1016 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1017 lfsck_pos_fill(env, lfsck,
1018 &ns->ln_pos_first_inconsistent, false);
1019 if (!(bk->lb_param & LPF_FAILOUT))
1023 ns->ln_items_repaired++;
1024 if (bk->lb_param & LPF_DRYRUN &&
1025 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1026 lfsck_pos_fill(env, lfsck,
1027 &ns->ln_pos_first_inconsistent,
1030 com->lc_journal = 0;
1034 up_write(&com->lc_sem);
1038 static int lfsck_namespace_post(const struct lu_env *env,
1039 struct lfsck_component *com,
1040 int result, bool init)
1042 struct lfsck_instance *lfsck = com->lc_lfsck;
1043 struct lfsck_namespace *ns = com->lc_file_ram;
1046 down_write(&com->lc_sem);
1048 spin_lock(&lfsck->li_lock);
1050 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
1052 ns->ln_status = LS_SCANNING_PHASE2;
1053 ns->ln_flags |= LF_SCANNED_ONCE;
1054 ns->ln_flags &= ~LF_UPGRADE;
1055 cfs_list_del_init(&com->lc_link);
1056 cfs_list_del_init(&com->lc_link_dir);
1057 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1058 } else if (result == 0) {
1059 ns->ln_status = lfsck->li_status;
1060 if (ns->ln_status == 0)
1061 ns->ln_status = LS_STOPPED;
1062 if (ns->ln_status != LS_PAUSED) {
1063 cfs_list_del_init(&com->lc_link);
1064 cfs_list_del_init(&com->lc_link_dir);
1065 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1068 ns->ln_status = LS_FAILED;
1069 cfs_list_del_init(&com->lc_link);
1070 cfs_list_del_init(&com->lc_link_dir);
1071 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1073 spin_unlock(&lfsck->li_lock);
1076 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1077 HALF_SEC - lfsck->li_time_last_checkpoint);
1078 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1079 ns->ln_items_checked += com->lc_new_checked;
1080 com->lc_new_checked = 0;
1083 rc = lfsck_namespace_store(env, com, false);
1085 up_write(&com->lc_sem);
1090 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1093 struct lfsck_instance *lfsck = com->lc_lfsck;
1094 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1095 struct lfsck_namespace *ns = com->lc_file_ram;
1100 down_read(&com->lc_sem);
1101 rc = snprintf(buf, len,
1102 "name: lfsck_namespace\n"
1108 lfsck_status2names(ns->ln_status));
1114 rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1119 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1124 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1125 "time_since_last_completed");
1129 rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1130 "time_since_latest_start");
1134 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1135 "time_since_last_checkpoint");
1139 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1140 "latest_start_position");
1144 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1145 "last_checkpoint_position");
1149 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1150 "first_failure_position");
1154 if (ns->ln_status == LS_SCANNING_PHASE1) {
1155 struct lfsck_position pos;
1156 const struct dt_it_ops *iops;
1157 cfs_duration_t duration = cfs_time_current() -
1158 lfsck->li_time_last_checkpoint;
1159 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1160 __u64 speed = checked;
1161 __u64 new_checked = com->lc_new_checked * HZ;
1162 __u32 rtime = ns->ln_run_time_phase1 +
1163 cfs_duration_sec(duration + HALF_SEC);
1166 do_div(new_checked, duration);
1168 do_div(speed, rtime);
1169 rc = snprintf(buf, len,
1170 "checked_phase1: "LPU64"\n"
1171 "checked_phase2: "LPU64"\n"
1172 "updated_phase1: "LPU64"\n"
1173 "updated_phase2: "LPU64"\n"
1174 "failed_phase1: "LPU64"\n"
1175 "failed_phase2: "LPU64"\n"
1177 "M-linked: "LPU64"\n"
1178 "nlinks_repaired: "LPU64"\n"
1179 "lost_found: "LPU64"\n"
1180 "success_count: %u\n"
1181 "run_time_phase1: %u seconds\n"
1182 "run_time_phase2: %u seconds\n"
1183 "average_speed_phase1: "LPU64" items/sec\n"
1184 "average_speed_phase2: N/A\n"
1185 "real-time_speed_phase1: "LPU64" items/sec\n"
1186 "real-time_speed_phase2: N/A\n",
1188 ns->ln_objs_checked_phase2,
1189 ns->ln_items_repaired,
1190 ns->ln_objs_repaired_phase2,
1191 ns->ln_items_failed,
1192 ns->ln_objs_failed_phase2,
1193 ns->ln_dirs_checked,
1194 ns->ln_mlinked_checked,
1195 ns->ln_objs_nlink_repaired,
1196 ns->ln_objs_lost_found,
1197 ns->ln_success_count,
1199 ns->ln_run_time_phase2,
1208 LASSERT(lfsck->li_di_oit != NULL);
1210 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1212 /* The low layer otable-based iteration position may NOT
1213 * exactly match the namespace-based directory traversal
1214 * cookie. Generally, it is not a serious issue. But the
1215 * caller should NOT make assumption on that. */
1216 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1217 if (!lfsck->li_current_oit_processed)
1218 pos.lp_oit_cookie--;
1220 spin_lock(&lfsck->li_lock);
1221 if (lfsck->li_di_dir != NULL) {
1222 pos.lp_dir_cookie = lfsck->li_cookie_dir;
1223 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
1224 fid_zero(&pos.lp_dir_parent);
1225 pos.lp_dir_cookie = 0;
1228 *lfsck_dto2fid(lfsck->li_obj_dir);
1231 fid_zero(&pos.lp_dir_parent);
1232 pos.lp_dir_cookie = 0;
1234 spin_unlock(&lfsck->li_lock);
1235 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1238 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1239 cfs_duration_t duration = cfs_time_current() -
1240 lfsck->li_time_last_checkpoint;
1241 __u64 checked = ns->ln_objs_checked_phase2 +
1242 com->lc_new_checked;
1243 __u64 speed1 = ns->ln_items_checked;
1244 __u64 speed2 = checked;
1245 __u64 new_checked = com->lc_new_checked * HZ;
1246 __u32 rtime = ns->ln_run_time_phase2 +
1247 cfs_duration_sec(duration + HALF_SEC);
1250 do_div(new_checked, duration);
1251 if (ns->ln_run_time_phase1 != 0)
1252 do_div(speed1, ns->ln_run_time_phase1);
1254 do_div(speed2, rtime);
1255 rc = snprintf(buf, len,
1256 "checked_phase1: "LPU64"\n"
1257 "checked_phase2: "LPU64"\n"
1258 "updated_phase1: "LPU64"\n"
1259 "updated_phase2: "LPU64"\n"
1260 "failed_phase1: "LPU64"\n"
1261 "failed_phase2: "LPU64"\n"
1263 "M-linked: "LPU64"\n"
1264 "nlinks_repaired: "LPU64"\n"
1265 "lost_found: "LPU64"\n"
1266 "success_count: %u\n"
1267 "run_time_phase1: %u seconds\n"
1268 "run_time_phase2: %u seconds\n"
1269 "average_speed_phase1: "LPU64" items/sec\n"
1270 "average_speed_phase2: "LPU64" objs/sec\n"
1271 "real-time_speed_phase1: N/A\n"
1272 "real-time_speed_phase2: "LPU64" objs/sec\n"
1273 "current_position: "DFID"\n",
1274 ns->ln_items_checked,
1276 ns->ln_items_repaired,
1277 ns->ln_objs_repaired_phase2,
1278 ns->ln_items_failed,
1279 ns->ln_objs_failed_phase2,
1280 ns->ln_dirs_checked,
1281 ns->ln_mlinked_checked,
1282 ns->ln_objs_nlink_repaired,
1283 ns->ln_objs_lost_found,
1284 ns->ln_success_count,
1285 ns->ln_run_time_phase1,
1290 PFID(&ns->ln_fid_latest_scanned_phase2));
1297 __u64 speed1 = ns->ln_items_checked;
1298 __u64 speed2 = ns->ln_objs_checked_phase2;
1300 if (ns->ln_run_time_phase1 != 0)
1301 do_div(speed1, ns->ln_run_time_phase1);
1302 if (ns->ln_run_time_phase2 != 0)
1303 do_div(speed2, ns->ln_run_time_phase2);
1304 rc = snprintf(buf, len,
1305 "checked_phase1: "LPU64"\n"
1306 "checked_phase2: "LPU64"\n"
1307 "updated_phase1: "LPU64"\n"
1308 "updated_phase2: "LPU64"\n"
1309 "failed_phase1: "LPU64"\n"
1310 "failed_phase2: "LPU64"\n"
1312 "M-linked: "LPU64"\n"
1313 "nlinks_repaired: "LPU64"\n"
1314 "lost_found: "LPU64"\n"
1315 "success_count: %u\n"
1316 "run_time_phase1: %u seconds\n"
1317 "run_time_phase2: %u seconds\n"
1318 "average_speed_phase1: "LPU64" items/sec\n"
1319 "average_speed_phase2: "LPU64" objs/sec\n"
1320 "real-time_speed_phase1: N/A\n"
1321 "real-time_speed_phase2: N/A\n"
1322 "current_position: N/A\n",
1323 ns->ln_items_checked,
1324 ns->ln_objs_checked_phase2,
1325 ns->ln_items_repaired,
1326 ns->ln_objs_repaired_phase2,
1327 ns->ln_items_failed,
1328 ns->ln_objs_failed_phase2,
1329 ns->ln_dirs_checked,
1330 ns->ln_mlinked_checked,
1331 ns->ln_objs_nlink_repaired,
1332 ns->ln_objs_lost_found,
1333 ns->ln_success_count,
1334 ns->ln_run_time_phase1,
1335 ns->ln_run_time_phase2,
1347 up_read(&com->lc_sem);
1351 static int lfsck_namespace_double_scan_main(void *args)
1353 struct lfsck_thread_args *lta = args;
1354 const struct lu_env *env = <a->lta_env;
1355 struct lfsck_component *com = lta->lta_com;
1356 struct lfsck_instance *lfsck = com->lc_lfsck;
1357 struct ptlrpc_thread *thread = &lfsck->li_thread;
1358 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1359 struct lfsck_namespace *ns = com->lc_file_ram;
1360 struct dt_object *obj = com->lc_obj;
1361 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
1362 struct dt_object *target;
1370 com->lc_new_checked = 0;
1371 com->lc_new_scanned = 0;
1372 com->lc_time_last_checkpoint = cfs_time_current();
1373 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
1374 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1376 di = iops->init(env, obj, 0, BYPASS_CAPA);
1378 GOTO(out, rc = PTR_ERR(di));
1380 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1381 rc = iops->get(env, di, (const struct dt_key *)&fid);
1385 /* Skip the start one, which either has been processed or non-exist. */
1386 rc = iops->next(env, di);
1390 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1394 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1396 struct l_wait_info lwi;
1398 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1400 l_wait_event(thread->t_ctl_waitq,
1401 !thread_is_running(thread),
1405 key = iops->key(env, di);
1406 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1407 target = lfsck_object_find(env, lfsck, &fid);
1408 down_write(&com->lc_sem);
1409 if (target == NULL) {
1412 } else if (IS_ERR(target)) {
1413 rc = PTR_ERR(target);
1417 /* XXX: Currently, skip remote object, the consistency for
1418 * remote object will be processed in LFSCK phase III. */
1419 if (dt_object_exists(target) && !dt_object_remote(target)) {
1420 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1422 rc = lfsck_namespace_double_scan_one(env, com,
1426 lfsck_object_put(env, target);
1429 com->lc_new_checked++;
1430 com->lc_new_scanned++;
1431 ns->ln_fid_latest_scanned_phase2 = fid;
1433 ns->ln_objs_repaired_phase2++;
1435 ns->ln_objs_failed_phase2++;
1436 up_write(&com->lc_sem);
1438 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1439 lfsck_namespace_delete(env, com, &fid);
1440 } else if (rc < 0) {
1441 flags |= LLF_REPAIR_FAILED;
1442 lfsck_namespace_update(env, com, &fid, flags, true);
1445 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1448 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
1449 cfs_time_current())) &&
1450 com->lc_new_checked != 0) {
1451 down_write(&com->lc_sem);
1452 ns->ln_run_time_phase2 +=
1453 cfs_duration_sec(cfs_time_current() +
1454 HALF_SEC - com->lc_time_last_checkpoint);
1455 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1456 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1457 com->lc_new_checked = 0;
1458 rc = lfsck_namespace_store(env, com, false);
1459 up_write(&com->lc_sem);
1463 com->lc_time_last_checkpoint = cfs_time_current();
1464 com->lc_time_next_checkpoint =
1465 com->lc_time_last_checkpoint +
1466 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1469 lfsck_control_speed_by_self(com);
1470 if (unlikely(!thread_is_running(thread)))
1473 rc = iops->next(env, di);
1482 iops->fini(env, di);
1485 down_write(&com->lc_sem);
1486 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1487 HALF_SEC - lfsck->li_time_last_checkpoint);
1488 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1489 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1490 com->lc_new_checked = 0;
1493 com->lc_journal = 0;
1494 ns->ln_status = LS_COMPLETED;
1495 if (!(bk->lb_param & LPF_DRYRUN))
1496 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
1497 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
1498 ns->ln_success_count++;
1499 } else if (rc == 0) {
1500 ns->ln_status = lfsck->li_status;
1501 if (ns->ln_status == 0)
1502 ns->ln_status = LS_STOPPED;
1504 ns->ln_status = LS_FAILED;
1507 rc = lfsck_namespace_store(env, com, false);
1508 up_write(&com->lc_sem);
1509 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1510 wake_up_all(&thread->t_ctl_waitq);
1512 lfsck_thread_args_fini(lta);
1517 static int lfsck_namespace_double_scan(const struct lu_env *env,
1518 struct lfsck_component *com)
1520 struct lfsck_instance *lfsck = com->lc_lfsck;
1521 struct lfsck_namespace *ns = com->lc_file_ram;
1522 struct lfsck_thread_args *lta;
1526 if (unlikely(ns->ln_status != LS_SCANNING_PHASE2))
1529 lta = lfsck_thread_args_init(lfsck, com, NULL);
1531 RETURN(PTR_ERR(lta));
1533 atomic_inc(&lfsck->li_double_scan_count);
1534 rc = PTR_ERR(kthread_run(lfsck_namespace_double_scan_main, lta,
1535 "lfsck_namespace"));
1536 if (IS_ERR_VALUE(rc)) {
1537 CERROR("%s: cannot start LFSCK namespace thread: rc = %ld\n",
1538 lfsck_lfsck2name(lfsck), rc);
1539 atomic_dec(&lfsck->li_double_scan_count);
1540 lfsck_thread_args_fini(lta);
1548 static int lfsck_namespace_in_notify(const struct lu_env *env,
1549 struct lfsck_component *com,
1550 struct lfsck_request *lr)
1555 static int lfsck_namespace_query(const struct lu_env *env,
1556 struct lfsck_component *com)
1558 struct lfsck_namespace *ns = com->lc_file_ram;
1560 return ns->ln_status;
1563 static struct lfsck_operations lfsck_namespace_ops = {
1564 .lfsck_reset = lfsck_namespace_reset,
1565 .lfsck_fail = lfsck_namespace_fail,
1566 .lfsck_checkpoint = lfsck_namespace_checkpoint,
1567 .lfsck_prep = lfsck_namespace_prep,
1568 .lfsck_exec_oit = lfsck_namespace_exec_oit,
1569 .lfsck_exec_dir = lfsck_namespace_exec_dir,
1570 .lfsck_post = lfsck_namespace_post,
1571 .lfsck_dump = lfsck_namespace_dump,
1572 .lfsck_double_scan = lfsck_namespace_double_scan,
1573 .lfsck_in_notify = lfsck_namespace_in_notify,
1574 .lfsck_query = lfsck_namespace_query,
1577 int lfsck_namespace_setup(const struct lu_env *env,
1578 struct lfsck_instance *lfsck)
1580 struct lfsck_component *com;
1581 struct lfsck_namespace *ns;
1582 struct dt_object *root = NULL;
1583 struct dt_object *obj;
1587 LASSERT(lfsck->li_master);
1593 CFS_INIT_LIST_HEAD(&com->lc_link);
1594 CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1595 init_rwsem(&com->lc_sem);
1596 atomic_set(&com->lc_ref, 1);
1597 com->lc_lfsck = lfsck;
1598 com->lc_type = LT_NAMESPACE;
1599 com->lc_ops = &lfsck_namespace_ops;
1600 com->lc_file_size = sizeof(struct lfsck_namespace);
1601 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1602 if (com->lc_file_ram == NULL)
1603 GOTO(out, rc = -ENOMEM);
1605 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1606 if (com->lc_file_disk == NULL)
1607 GOTO(out, rc = -ENOMEM);
1609 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1611 GOTO(out, rc = PTR_ERR(root));
1613 if (unlikely(!dt_try_as_dir(env, root)))
1614 GOTO(out, rc = -ENOTDIR);
1616 obj = local_index_find_or_create(env, lfsck->li_los, root,
1617 lfsck_namespace_name,
1618 S_IFREG | S_IRUGO | S_IWUSR,
1619 &dt_lfsck_features);
1621 GOTO(out, rc = PTR_ERR(obj));
1624 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1628 rc = lfsck_namespace_load(env, com);
1630 rc = lfsck_namespace_reset(env, com, true);
1631 else if (rc == -ENODATA)
1632 rc = lfsck_namespace_init(env, com);
1636 ns = com->lc_file_ram;
1637 switch (ns->ln_status) {
1642 spin_lock(&lfsck->li_lock);
1643 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1644 spin_unlock(&lfsck->li_lock);
1647 CERROR("%s: unknown lfsck_namespace status: rc = %u\n",
1648 lfsck_lfsck2name(lfsck), ns->ln_status);
1650 case LS_SCANNING_PHASE1:
1651 case LS_SCANNING_PHASE2:
1652 /* No need to store the status to disk right now.
1653 * If the system crashed before the status stored,
1654 * it will be loaded back when next time. */
1655 ns->ln_status = LS_CRASHED;
1659 spin_lock(&lfsck->li_lock);
1660 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1661 cfs_list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
1662 spin_unlock(&lfsck->li_lock);
1669 if (root != NULL && !IS_ERR(root))
1670 lu_object_put(env, &root->do_lu);
1672 lfsck_component_cleanup(env, com);