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)
757 struct lfsck_instance *lfsck = com->lc_lfsck;
758 struct lfsck_namespace *ns = com->lc_file_ram;
759 struct lfsck_position *pos = &com->lc_pos_start;
761 if (ns->ln_status == LS_COMPLETED) {
764 rc = lfsck_namespace_reset(env, com, false);
769 down_write(&com->lc_sem);
771 ns->ln_time_latest_start = cfs_time_current_sec();
773 spin_lock(&lfsck->li_lock);
774 if (ns->ln_flags & LF_SCANNED_ONCE) {
775 if (!lfsck->li_drop_dryrun ||
776 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
777 ns->ln_status = LS_SCANNING_PHASE2;
778 cfs_list_del_init(&com->lc_link);
779 cfs_list_add_tail(&com->lc_link,
780 &lfsck->li_list_double_scan);
781 if (!cfs_list_empty(&com->lc_link_dir))
782 cfs_list_del_init(&com->lc_link_dir);
783 lfsck_pos_set_zero(pos);
785 ns->ln_status = LS_SCANNING_PHASE1;
786 ns->ln_run_time_phase1 = 0;
787 ns->ln_run_time_phase2 = 0;
788 ns->ln_items_checked = 0;
789 ns->ln_items_repaired = 0;
790 ns->ln_items_failed = 0;
791 ns->ln_dirs_checked = 0;
792 ns->ln_mlinked_checked = 0;
793 ns->ln_objs_checked_phase2 = 0;
794 ns->ln_objs_repaired_phase2 = 0;
795 ns->ln_objs_failed_phase2 = 0;
796 ns->ln_objs_nlink_repaired = 0;
797 ns->ln_objs_lost_found = 0;
798 fid_zero(&ns->ln_fid_latest_scanned_phase2);
799 if (cfs_list_empty(&com->lc_link_dir))
800 cfs_list_add_tail(&com->lc_link_dir,
801 &lfsck->li_list_dir);
802 *pos = ns->ln_pos_first_inconsistent;
805 ns->ln_status = LS_SCANNING_PHASE1;
806 if (cfs_list_empty(&com->lc_link_dir))
807 cfs_list_add_tail(&com->lc_link_dir,
808 &lfsck->li_list_dir);
809 if (!lfsck->li_drop_dryrun ||
810 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
811 *pos = ns->ln_pos_last_checkpoint;
812 pos->lp_oit_cookie++;
814 *pos = ns->ln_pos_first_inconsistent;
817 spin_unlock(&lfsck->li_lock);
819 up_write(&com->lc_sem);
823 static int lfsck_namespace_exec_oit(const struct lu_env *env,
824 struct lfsck_component *com,
825 struct dt_object *obj)
827 down_write(&com->lc_sem);
828 com->lc_new_checked++;
829 if (S_ISDIR(lfsck_object_type(obj)))
830 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
831 up_write(&com->lc_sem);
835 static int lfsck_namespace_exec_dir(const struct lu_env *env,
836 struct lfsck_component *com,
837 struct dt_object *obj,
838 struct lu_dirent *ent)
840 struct lfsck_thread_info *info = lfsck_env_info(env);
841 struct lu_attr *la = &info->lti_la;
842 struct lfsck_instance *lfsck = com->lc_lfsck;
843 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
844 struct lfsck_namespace *ns = com->lc_file_ram;
845 struct linkea_data ldata = { 0 };
846 const struct lu_fid *pfid = lfsck_dto2fid(lfsck->li_obj_dir);
847 const struct lu_fid *cfid = lfsck_dto2fid(obj);
848 const struct lu_name *cname;
849 struct thandle *handle = NULL;
850 bool repaired = false;
858 cname = lfsck_name_get_const(env, ent->lde_name, ent->lde_namelen);
859 down_write(&com->lc_sem);
860 com->lc_new_checked++;
862 if (ent->lde_attrs & LUDA_UPGRADE) {
863 ns->ln_flags |= LF_UPGRADE;
865 } else if (ent->lde_attrs & LUDA_REPAIR) {
866 ns->ln_flags |= LF_INCONSISTENT;
870 if (ent->lde_name[0] == '.' &&
871 (ent->lde_namelen == 1 ||
872 (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
873 fid_is_dot_lustre(&ent->lde_fid)))
876 if (!(bk->lb_param & LPF_DRYRUN) &&
877 (com->lc_journal || repaired)) {
883 handle = dt_trans_create(env, lfsck->li_next);
885 GOTO(out, rc = PTR_ERR(handle));
887 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
891 rc = dt_trans_start(env, lfsck->li_next, handle);
895 dt_write_lock(env, obj, MOR_TGT_CHILD);
899 rc = lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
903 rc = lfsck_links_read(env, obj, &ldata);
905 count = ldata.ld_leh->leh_reccount;
906 rc = linkea_links_find(&ldata, cname, pfid);
908 (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
911 ns->ln_flags |= LF_INCONSISTENT;
912 /* For dir, if there are more than one linkea entries, or the
913 * linkea entry does not match the name entry, then remove all
914 * and add the correct one. */
915 if (S_ISDIR(lfsck_object_type(obj))) {
923 } else if (unlikely(rc == -EINVAL)) {
925 ns->ln_flags |= LF_INCONSISTENT;
926 /* The magic crashed, we are not sure whether there are more
927 * corrupt data in the linkea, so remove all linkea entries. */
931 } else if (rc == -ENODATA) {
933 ns->ln_flags |= LF_UPGRADE;
938 if (bk->lb_param & LPF_DRYRUN) {
943 if (!com->lc_journal)
949 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
956 rc = linkea_data_new(&ldata,
957 &lfsck_env_info(env)->lti_linkea_buf);
962 rc = linkea_add_buf(&ldata, cname, pfid);
966 rc = lfsck_links_write(env, obj, &ldata, handle);
970 count = ldata.ld_leh->leh_reccount;
979 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
984 (la->la_nlink == 1 || S_ISDIR(lfsck_object_type(obj))))
985 /* Usually, it is for single linked object or dir, do nothing.*/
988 /* Following modification will be in another transaction. */
989 if (handle != NULL) {
990 LASSERT(dt_write_locked(env, obj));
992 dt_write_unlock(env, obj);
995 dt_trans_stop(env, lfsck->li_next, handle);
999 ns->ln_mlinked_checked++;
1000 rc = lfsck_namespace_update(env, com, cfid,
1001 count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
1007 dt_write_unlock(env, obj);
1010 dt_trans_stop(env, lfsck->li_next, handle);
1014 ns->ln_items_failed++;
1015 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1016 lfsck_pos_fill(env, lfsck,
1017 &ns->ln_pos_first_inconsistent, false);
1018 if (!(bk->lb_param & LPF_FAILOUT))
1022 ns->ln_items_repaired++;
1023 if (bk->lb_param & LPF_DRYRUN &&
1024 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1025 lfsck_pos_fill(env, lfsck,
1026 &ns->ln_pos_first_inconsistent,
1029 com->lc_journal = 0;
1033 up_write(&com->lc_sem);
1037 static int lfsck_namespace_post(const struct lu_env *env,
1038 struct lfsck_component *com,
1039 int result, bool init)
1041 struct lfsck_instance *lfsck = com->lc_lfsck;
1042 struct lfsck_namespace *ns = com->lc_file_ram;
1045 down_write(&com->lc_sem);
1047 spin_lock(&lfsck->li_lock);
1049 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
1051 ns->ln_status = LS_SCANNING_PHASE2;
1052 ns->ln_flags |= LF_SCANNED_ONCE;
1053 ns->ln_flags &= ~LF_UPGRADE;
1054 cfs_list_del_init(&com->lc_link);
1055 cfs_list_del_init(&com->lc_link_dir);
1056 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1057 } else if (result == 0) {
1058 ns->ln_status = lfsck->li_status;
1059 if (ns->ln_status == 0)
1060 ns->ln_status = LS_STOPPED;
1061 if (ns->ln_status != LS_PAUSED) {
1062 cfs_list_del_init(&com->lc_link);
1063 cfs_list_del_init(&com->lc_link_dir);
1064 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1067 ns->ln_status = LS_FAILED;
1068 cfs_list_del_init(&com->lc_link);
1069 cfs_list_del_init(&com->lc_link_dir);
1070 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1072 spin_unlock(&lfsck->li_lock);
1075 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1076 HALF_SEC - lfsck->li_time_last_checkpoint);
1077 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1078 ns->ln_items_checked += com->lc_new_checked;
1079 com->lc_new_checked = 0;
1082 rc = lfsck_namespace_store(env, com, false);
1084 up_write(&com->lc_sem);
1089 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1092 struct lfsck_instance *lfsck = com->lc_lfsck;
1093 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1094 struct lfsck_namespace *ns = com->lc_file_ram;
1099 down_read(&com->lc_sem);
1100 rc = snprintf(buf, len,
1101 "name: lfsck_namespace\n"
1107 lfsck_status2names(ns->ln_status));
1113 rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1118 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1123 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1124 "time_since_last_completed");
1128 rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1129 "time_since_latest_start");
1133 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1134 "time_since_last_checkpoint");
1138 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1139 "latest_start_position");
1143 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1144 "last_checkpoint_position");
1148 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1149 "first_failure_position");
1153 if (ns->ln_status == LS_SCANNING_PHASE1) {
1154 struct lfsck_position pos;
1155 const struct dt_it_ops *iops;
1156 cfs_duration_t duration = cfs_time_current() -
1157 lfsck->li_time_last_checkpoint;
1158 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1159 __u64 speed = checked;
1160 __u64 new_checked = com->lc_new_checked * HZ;
1161 __u32 rtime = ns->ln_run_time_phase1 +
1162 cfs_duration_sec(duration + HALF_SEC);
1165 do_div(new_checked, duration);
1167 do_div(speed, rtime);
1168 rc = snprintf(buf, len,
1169 "checked_phase1: "LPU64"\n"
1170 "checked_phase2: "LPU64"\n"
1171 "updated_phase1: "LPU64"\n"
1172 "updated_phase2: "LPU64"\n"
1173 "failed_phase1: "LPU64"\n"
1174 "failed_phase2: "LPU64"\n"
1176 "M-linked: "LPU64"\n"
1177 "nlinks_repaired: "LPU64"\n"
1178 "lost_found: "LPU64"\n"
1179 "success_count: %u\n"
1180 "run_time_phase1: %u seconds\n"
1181 "run_time_phase2: %u seconds\n"
1182 "average_speed_phase1: "LPU64" items/sec\n"
1183 "average_speed_phase2: N/A\n"
1184 "real-time_speed_phase1: "LPU64" items/sec\n"
1185 "real-time_speed_phase2: N/A\n",
1187 ns->ln_objs_checked_phase2,
1188 ns->ln_items_repaired,
1189 ns->ln_objs_repaired_phase2,
1190 ns->ln_items_failed,
1191 ns->ln_objs_failed_phase2,
1192 ns->ln_dirs_checked,
1193 ns->ln_mlinked_checked,
1194 ns->ln_objs_nlink_repaired,
1195 ns->ln_objs_lost_found,
1196 ns->ln_success_count,
1198 ns->ln_run_time_phase2,
1207 LASSERT(lfsck->li_di_oit != NULL);
1209 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1211 /* The low layer otable-based iteration position may NOT
1212 * exactly match the namespace-based directory traversal
1213 * cookie. Generally, it is not a serious issue. But the
1214 * caller should NOT make assumption on that. */
1215 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1216 if (!lfsck->li_current_oit_processed)
1217 pos.lp_oit_cookie--;
1219 spin_lock(&lfsck->li_lock);
1220 if (lfsck->li_di_dir != NULL) {
1221 pos.lp_dir_cookie = lfsck->li_cookie_dir;
1222 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
1223 fid_zero(&pos.lp_dir_parent);
1224 pos.lp_dir_cookie = 0;
1227 *lfsck_dto2fid(lfsck->li_obj_dir);
1230 fid_zero(&pos.lp_dir_parent);
1231 pos.lp_dir_cookie = 0;
1233 spin_unlock(&lfsck->li_lock);
1234 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1237 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1238 cfs_duration_t duration = cfs_time_current() -
1239 lfsck->li_time_last_checkpoint;
1240 __u64 checked = ns->ln_objs_checked_phase2 +
1241 com->lc_new_checked;
1242 __u64 speed1 = ns->ln_items_checked;
1243 __u64 speed2 = checked;
1244 __u64 new_checked = com->lc_new_checked * HZ;
1245 __u32 rtime = ns->ln_run_time_phase2 +
1246 cfs_duration_sec(duration + HALF_SEC);
1249 do_div(new_checked, duration);
1250 if (ns->ln_run_time_phase1 != 0)
1251 do_div(speed1, ns->ln_run_time_phase1);
1253 do_div(speed2, rtime);
1254 rc = snprintf(buf, len,
1255 "checked_phase1: "LPU64"\n"
1256 "checked_phase2: "LPU64"\n"
1257 "updated_phase1: "LPU64"\n"
1258 "updated_phase2: "LPU64"\n"
1259 "failed_phase1: "LPU64"\n"
1260 "failed_phase2: "LPU64"\n"
1262 "M-linked: "LPU64"\n"
1263 "nlinks_repaired: "LPU64"\n"
1264 "lost_found: "LPU64"\n"
1265 "success_count: %u\n"
1266 "run_time_phase1: %u seconds\n"
1267 "run_time_phase2: %u seconds\n"
1268 "average_speed_phase1: "LPU64" items/sec\n"
1269 "average_speed_phase2: "LPU64" objs/sec\n"
1270 "real-time_speed_phase1: N/A\n"
1271 "real-time_speed_phase2: "LPU64" objs/sec\n"
1272 "current_position: "DFID"\n",
1273 ns->ln_items_checked,
1275 ns->ln_items_repaired,
1276 ns->ln_objs_repaired_phase2,
1277 ns->ln_items_failed,
1278 ns->ln_objs_failed_phase2,
1279 ns->ln_dirs_checked,
1280 ns->ln_mlinked_checked,
1281 ns->ln_objs_nlink_repaired,
1282 ns->ln_objs_lost_found,
1283 ns->ln_success_count,
1284 ns->ln_run_time_phase1,
1289 PFID(&ns->ln_fid_latest_scanned_phase2));
1296 __u64 speed1 = ns->ln_items_checked;
1297 __u64 speed2 = ns->ln_objs_checked_phase2;
1299 if (ns->ln_run_time_phase1 != 0)
1300 do_div(speed1, ns->ln_run_time_phase1);
1301 if (ns->ln_run_time_phase2 != 0)
1302 do_div(speed2, ns->ln_run_time_phase2);
1303 rc = snprintf(buf, len,
1304 "checked_phase1: "LPU64"\n"
1305 "checked_phase2: "LPU64"\n"
1306 "updated_phase1: "LPU64"\n"
1307 "updated_phase2: "LPU64"\n"
1308 "failed_phase1: "LPU64"\n"
1309 "failed_phase2: "LPU64"\n"
1311 "M-linked: "LPU64"\n"
1312 "nlinks_repaired: "LPU64"\n"
1313 "lost_found: "LPU64"\n"
1314 "success_count: %u\n"
1315 "run_time_phase1: %u seconds\n"
1316 "run_time_phase2: %u seconds\n"
1317 "average_speed_phase1: "LPU64" items/sec\n"
1318 "average_speed_phase2: "LPU64" objs/sec\n"
1319 "real-time_speed_phase1: N/A\n"
1320 "real-time_speed_phase2: N/A\n"
1321 "current_position: N/A\n",
1322 ns->ln_items_checked,
1323 ns->ln_objs_checked_phase2,
1324 ns->ln_items_repaired,
1325 ns->ln_objs_repaired_phase2,
1326 ns->ln_items_failed,
1327 ns->ln_objs_failed_phase2,
1328 ns->ln_dirs_checked,
1329 ns->ln_mlinked_checked,
1330 ns->ln_objs_nlink_repaired,
1331 ns->ln_objs_lost_found,
1332 ns->ln_success_count,
1333 ns->ln_run_time_phase1,
1334 ns->ln_run_time_phase2,
1346 up_read(&com->lc_sem);
1350 static int lfsck_namespace_double_scan_main(void *args)
1352 struct lfsck_thread_args *lta = args;
1353 const struct lu_env *env = <a->lta_env;
1354 struct lfsck_component *com = lta->lta_com;
1355 struct lfsck_instance *lfsck = com->lc_lfsck;
1356 struct ptlrpc_thread *thread = &lfsck->li_thread;
1357 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1358 struct lfsck_namespace *ns = com->lc_file_ram;
1359 struct dt_object *obj = com->lc_obj;
1360 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
1361 struct dt_object *target;
1369 com->lc_new_checked = 0;
1370 com->lc_new_scanned = 0;
1371 com->lc_time_last_checkpoint = cfs_time_current();
1372 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
1373 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1375 di = iops->init(env, obj, 0, BYPASS_CAPA);
1377 GOTO(out, rc = PTR_ERR(di));
1379 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1380 rc = iops->get(env, di, (const struct dt_key *)&fid);
1384 /* Skip the start one, which either has been processed or non-exist. */
1385 rc = iops->next(env, di);
1389 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1393 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1395 struct l_wait_info lwi;
1397 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1399 l_wait_event(thread->t_ctl_waitq,
1400 !thread_is_running(thread),
1404 key = iops->key(env, di);
1405 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1406 target = lfsck_object_find(env, lfsck, &fid);
1407 down_write(&com->lc_sem);
1408 if (target == NULL) {
1411 } else if (IS_ERR(target)) {
1412 rc = PTR_ERR(target);
1416 /* XXX: Currently, skip remote object, the consistency for
1417 * remote object will be processed in LFSCK phase III. */
1418 if (dt_object_exists(target) && !dt_object_remote(target)) {
1419 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1421 rc = lfsck_namespace_double_scan_one(env, com,
1425 lfsck_object_put(env, target);
1428 com->lc_new_checked++;
1429 com->lc_new_scanned++;
1430 ns->ln_fid_latest_scanned_phase2 = fid;
1432 ns->ln_objs_repaired_phase2++;
1434 ns->ln_objs_failed_phase2++;
1435 up_write(&com->lc_sem);
1437 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1438 lfsck_namespace_delete(env, com, &fid);
1439 } else if (rc < 0) {
1440 flags |= LLF_REPAIR_FAILED;
1441 lfsck_namespace_update(env, com, &fid, flags, true);
1444 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1447 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
1448 cfs_time_current())) &&
1449 com->lc_new_checked != 0) {
1450 down_write(&com->lc_sem);
1451 ns->ln_run_time_phase2 +=
1452 cfs_duration_sec(cfs_time_current() +
1453 HALF_SEC - com->lc_time_last_checkpoint);
1454 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1455 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1456 com->lc_new_checked = 0;
1457 rc = lfsck_namespace_store(env, com, false);
1458 up_write(&com->lc_sem);
1462 com->lc_time_last_checkpoint = cfs_time_current();
1463 com->lc_time_next_checkpoint =
1464 com->lc_time_last_checkpoint +
1465 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1468 lfsck_control_speed_by_self(com);
1469 if (unlikely(!thread_is_running(thread)))
1472 rc = iops->next(env, di);
1481 iops->fini(env, di);
1484 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 if (ns->ln_status != LS_PAUSED) {
1508 spin_lock(&lfsck->li_lock);
1509 cfs_list_del_init(&com->lc_link);
1510 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1511 spin_unlock(&lfsck->li_lock);
1514 rc = lfsck_namespace_store(env, com, false);
1516 up_write(&com->lc_sem);
1517 if (atomic_dec_and_test(&lfsck->li_double_scan_count))
1518 wake_up_all(&thread->t_ctl_waitq);
1520 lfsck_thread_args_fini(lta);
1525 static int lfsck_namespace_double_scan(const struct lu_env *env,
1526 struct lfsck_component *com)
1528 struct lfsck_instance *lfsck = com->lc_lfsck;
1529 struct lfsck_namespace *ns = com->lc_file_ram;
1530 struct lfsck_thread_args *lta;
1534 if (unlikely(ns->ln_status != LS_SCANNING_PHASE2))
1537 lta = lfsck_thread_args_init(lfsck, com);
1539 RETURN(PTR_ERR(lta));
1541 atomic_inc(&lfsck->li_double_scan_count);
1542 rc = PTR_ERR(kthread_run(lfsck_namespace_double_scan_main, lta,
1543 "lfsck_namespace"));
1544 if (IS_ERR_VALUE(rc)) {
1545 CERROR("%s: cannot start LFSCK namespace thread: rc = %ld\n",
1546 lfsck_lfsck2name(lfsck), rc);
1547 atomic_dec(&lfsck->li_double_scan_count);
1548 lfsck_thread_args_fini(lta);
1556 static int lfsck_namespace_in_notify(const struct lu_env *env,
1557 struct lfsck_component *com,
1558 struct lfsck_request *lr)
1563 static int lfsck_namespace_query(const struct lu_env *env,
1564 struct lfsck_component *com)
1566 struct lfsck_namespace *ns = com->lc_file_ram;
1568 return ns->ln_status;
1571 static struct lfsck_operations lfsck_namespace_ops = {
1572 .lfsck_reset = lfsck_namespace_reset,
1573 .lfsck_fail = lfsck_namespace_fail,
1574 .lfsck_checkpoint = lfsck_namespace_checkpoint,
1575 .lfsck_prep = lfsck_namespace_prep,
1576 .lfsck_exec_oit = lfsck_namespace_exec_oit,
1577 .lfsck_exec_dir = lfsck_namespace_exec_dir,
1578 .lfsck_post = lfsck_namespace_post,
1579 .lfsck_dump = lfsck_namespace_dump,
1580 .lfsck_double_scan = lfsck_namespace_double_scan,
1581 .lfsck_in_notify = lfsck_namespace_in_notify,
1582 .lfsck_query = lfsck_namespace_query,
1585 int lfsck_namespace_setup(const struct lu_env *env,
1586 struct lfsck_instance *lfsck)
1588 struct lfsck_component *com;
1589 struct lfsck_namespace *ns;
1590 struct dt_object *root = NULL;
1591 struct dt_object *obj;
1595 LASSERT(lfsck->li_master);
1601 CFS_INIT_LIST_HEAD(&com->lc_link);
1602 CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1603 init_rwsem(&com->lc_sem);
1604 atomic_set(&com->lc_ref, 1);
1605 com->lc_lfsck = lfsck;
1606 com->lc_type = LT_NAMESPACE;
1607 com->lc_ops = &lfsck_namespace_ops;
1608 com->lc_file_size = sizeof(struct lfsck_namespace);
1609 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1610 if (com->lc_file_ram == NULL)
1611 GOTO(out, rc = -ENOMEM);
1613 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1614 if (com->lc_file_disk == NULL)
1615 GOTO(out, rc = -ENOMEM);
1617 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1619 GOTO(out, rc = PTR_ERR(root));
1621 if (unlikely(!dt_try_as_dir(env, root)))
1622 GOTO(out, rc = -ENOTDIR);
1624 obj = local_index_find_or_create(env, lfsck->li_los, root,
1625 lfsck_namespace_name,
1626 S_IFREG | S_IRUGO | S_IWUSR,
1627 &dt_lfsck_features);
1629 GOTO(out, rc = PTR_ERR(obj));
1632 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1636 rc = lfsck_namespace_load(env, com);
1638 rc = lfsck_namespace_reset(env, com, true);
1639 else if (rc == -ENODATA)
1640 rc = lfsck_namespace_init(env, com);
1644 ns = com->lc_file_ram;
1645 switch (ns->ln_status) {
1650 spin_lock(&lfsck->li_lock);
1651 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1652 spin_unlock(&lfsck->li_lock);
1655 CERROR("%s: unknown lfsck_namespace status: rc = %u\n",
1656 lfsck_lfsck2name(lfsck), ns->ln_status);
1658 case LS_SCANNING_PHASE1:
1659 case LS_SCANNING_PHASE2:
1660 /* No need to store the status to disk right now.
1661 * If the system crashed before the status stored,
1662 * it will be loaded back when next time. */
1663 ns->ln_status = LS_CRASHED;
1667 spin_lock(&lfsck->li_lock);
1668 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1669 cfs_list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
1670 spin_unlock(&lfsck->li_lock);
1677 if (root != NULL && !IS_ERR(root))
1678 lu_object_put(env, &root->do_lu);
1680 lfsck_component_cleanup(env, com);