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 * Load namespace LFSCK statistics information from the trace file.
120 * \param[in] env pointer to the thread context
121 * \param[in] com pointer to the lfsck component
123 * \retval 0 for success
124 * \retval negative error number on failure or absence the
125 * namespace LFSCK trace file
127 static int lfsck_namespace_load(const struct lu_env *env,
128 struct lfsck_component *com)
130 int len = com->lc_file_size;
133 rc = dt_xattr_get(env, com->lc_obj,
134 lfsck_buf_get(env, com->lc_file_disk, len),
135 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
137 struct lfsck_namespace *ns = com->lc_file_ram;
139 lfsck_namespace_le_to_cpu(ns,
140 (struct lfsck_namespace *)com->lc_file_disk);
141 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
142 CWARN("%.16s: invalid lfsck_namespace magic "
144 lfsck_lfsck2name(com->lc_lfsck),
145 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
152 CERROR("%.16s: fail to load lfsck_namespace, "
153 "expected = %d, rc = %d\n",
154 lfsck_lfsck2name(com->lc_lfsck), len, rc);
162 static int lfsck_namespace_store(const struct lu_env *env,
163 struct lfsck_component *com)
165 struct dt_object *obj = com->lc_obj;
166 struct lfsck_instance *lfsck = com->lc_lfsck;
167 struct thandle *handle;
168 int len = com->lc_file_size;
172 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
173 (struct lfsck_namespace *)com->lc_file_ram);
174 handle = dt_trans_create(env, lfsck->li_bottom);
175 if (IS_ERR(handle)) {
176 rc = PTR_ERR(handle);
177 CERROR("%.16s: fail to create trans for storing "
178 "lfsck_namespace: %d\n,", lfsck_lfsck2name(lfsck), rc);
182 rc = dt_declare_xattr_set(env, obj,
183 lfsck_buf_get(env, com->lc_file_disk, len),
184 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
186 CERROR("%.16s: fail to declare trans for storing "
187 "lfsck_namespace: %d\n,", lfsck_lfsck2name(lfsck), rc);
191 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
193 CERROR("%.16s: fail to start trans for storing "
194 "lfsck_namespace: %d\n,", lfsck_lfsck2name(lfsck), rc);
198 rc = dt_xattr_set(env, obj,
199 lfsck_buf_get(env, com->lc_file_disk, len),
200 XATTR_NAME_LFSCK_NAMESPACE, 0,
201 handle, BYPASS_CAPA);
203 CERROR("%.16s: fail to store lfsck_namespace, len = %d, "
204 "rc = %d\n", lfsck_lfsck2name(lfsck), len, rc);
209 dt_trans_stop(env, lfsck->li_bottom, handle);
213 static int lfsck_namespace_init(const struct lu_env *env,
214 struct lfsck_component *com)
216 struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
219 memset(ns, 0, sizeof(*ns));
220 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
221 ns->ln_status = LS_INIT;
222 down_write(&com->lc_sem);
223 rc = lfsck_namespace_store(env, com);
224 up_write(&com->lc_sem);
228 static int lfsck_namespace_lookup(const struct lu_env *env,
229 struct lfsck_component *com,
230 const struct lu_fid *fid, __u8 *flags)
232 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
235 fid_cpu_to_be(key, fid);
236 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)flags,
237 (const struct dt_key *)key, BYPASS_CAPA);
241 static int lfsck_namespace_delete(const struct lu_env *env,
242 struct lfsck_component *com,
243 const struct lu_fid *fid)
245 struct lfsck_instance *lfsck = com->lc_lfsck;
246 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
247 struct thandle *handle;
248 struct dt_object *obj = com->lc_obj;
252 handle = dt_trans_create(env, lfsck->li_bottom);
254 RETURN(PTR_ERR(handle));
256 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid, handle);
260 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
264 fid_cpu_to_be(key, fid);
265 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
271 dt_trans_stop(env, lfsck->li_bottom, handle);
275 static int lfsck_namespace_update(const struct lu_env *env,
276 struct lfsck_component *com,
277 const struct lu_fid *fid,
278 __u8 flags, bool force)
280 struct lfsck_instance *lfsck = com->lc_lfsck;
281 struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
282 struct thandle *handle;
283 struct dt_object *obj = com->lc_obj;
289 rc = lfsck_namespace_lookup(env, com, fid, &tf);
290 if (rc != 0 && rc != -ENOENT)
294 if (!force || flags == tf)
298 handle = dt_trans_create(env, lfsck->li_bottom);
300 RETURN(PTR_ERR(handle));
302 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid,
307 handle = dt_trans_create(env, lfsck->li_bottom);
309 RETURN(PTR_ERR(handle));
312 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
313 (const struct dt_key *)fid, handle);
317 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
321 fid_cpu_to_be(key, fid);
323 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
326 CERROR("%s: fail to insert "DFID", rc = %d\n",
327 lfsck_lfsck2name(com->lc_lfsck), PFID(fid), rc);
332 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
333 (const struct dt_key *)key, handle, BYPASS_CAPA, 1);
338 dt_trans_stop(env, lfsck->li_bottom, handle);
342 static int lfsck_namespace_check_exist(const struct lu_env *env,
343 struct lfsck_instance *lfsck,
344 struct dt_object *obj, const char *name)
346 struct dt_object *dir = lfsck->li_obj_dir;
347 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
351 if (unlikely(lfsck_is_dead_obj(obj)))
352 RETURN(LFSCK_NAMEENTRY_DEAD);
354 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
355 (const struct dt_key *)name, BYPASS_CAPA);
357 RETURN(LFSCK_NAMEENTRY_REMOVED);
362 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
363 RETURN(LFSCK_NAMEENTRY_RECREATED);
368 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
369 struct dt_object *obj,
370 struct thandle *handle)
374 /* For destroying all invalid linkEA entries. */
375 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
379 /* For insert new linkEA entry. */
380 rc = dt_declare_xattr_set(env, obj,
381 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
382 XATTR_NAME_LINK, 0, handle);
386 static int lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
387 struct linkea_data *ldata)
392 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
394 if (ldata->ld_buf->lb_buf == NULL)
397 if (!dt_object_exists(obj))
400 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
402 /* Buf was too small, figure out what we need. */
403 lu_buf_free(ldata->ld_buf);
404 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
409 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
410 if (ldata->ld_buf->lb_buf == NULL)
413 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
424 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
425 struct linkea_data *ldata, struct thandle *handle)
427 const struct lu_buf *buf = lfsck_buf_get_const(env,
428 ldata->ld_buf->lb_buf,
429 ldata->ld_leh->leh_len);
431 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
436 * \retval ve: removed entries
438 static int lfsck_linkea_entry_unpack(struct lfsck_instance *lfsck,
439 struct linkea_data *ldata,
440 struct lu_name *cname,
443 struct link_ea_entry *oldlee;
447 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
448 oldlee = ldata->ld_lee;
449 oldlen = ldata->ld_reclen;
450 linkea_next_entry(ldata);
451 while (ldata->ld_lee != NULL) {
452 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
453 ldata->ld_lee->lee_reclen[1];
454 if (unlikely(ldata->ld_reclen == oldlen &&
455 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
456 linkea_del_buf(ldata, cname);
459 linkea_next_entry(ldata);
462 ldata->ld_lee = oldlee;
463 ldata->ld_reclen = oldlen;
468 * \retval +ve repaired
469 * \retval 0 no need to repair
470 * \retval -ve error cases
472 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
473 struct lfsck_component *com,
474 struct dt_object *child, __u8 flags)
476 struct lfsck_thread_info *info = lfsck_env_info(env);
477 struct lu_attr *la = &info->lti_la;
478 struct lu_name *cname = &info->lti_name;
479 struct lu_fid *pfid = &info->lti_fid;
480 struct lu_fid *cfid = &info->lti_fid2;
481 struct lfsck_instance *lfsck = com->lc_lfsck;
482 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
483 struct lfsck_namespace *ns =
484 (struct lfsck_namespace *)com->lc_file_ram;
485 struct linkea_data ldata = { 0 };
486 struct thandle *handle = NULL;
492 if (com->lc_journal) {
499 handle = dt_trans_create(env, lfsck->li_next);
501 RETURN(rc = PTR_ERR(handle));
503 rc = dt_declare_xattr_set(env, child,
504 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
505 XATTR_NAME_LINK, 0, handle);
509 rc = dt_trans_start(env, lfsck->li_next, handle);
513 dt_write_lock(env, child, MOR_TGT_CHILD);
517 if (unlikely(lfsck_is_dead_obj(child)))
520 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
522 rc = lfsck_links_read(env, child, &ldata);
524 if ((bk->lb_param & LPF_DRYRUN) &&
525 (rc == -EINVAL || rc == -ENODATA))
531 linkea_first_entry(&ldata);
532 while (ldata.ld_lee != NULL) {
533 struct dt_object *parent = NULL;
535 rc = lfsck_linkea_entry_unpack(lfsck, &ldata, cname, pfid);
539 if (!fid_is_sane(pfid))
542 parent = lfsck_object_find(env, lfsck, pfid);
545 else if (IS_ERR(parent))
546 GOTO(stop, rc = PTR_ERR(parent));
548 if (!dt_object_exists(parent))
551 /* XXX: Currently, skip remote object, the consistency for
552 * remote object will be processed in LFSCK phase III. */
553 if (dt_object_remote(parent)) {
554 lfsck_object_put(env, parent);
555 linkea_next_entry(&ldata);
559 if (unlikely(!dt_try_as_dir(env, parent)))
562 /* To guarantee the 'name' is terminated with '0'. */
563 memcpy(info->lti_key, cname->ln_name, cname->ln_namelen);
564 info->lti_key[cname->ln_namelen] = 0;
565 cname->ln_name = info->lti_key;
566 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
567 (const struct dt_key *)cname->ln_name,
569 if (rc != 0 && rc != -ENOENT) {
570 lfsck_object_put(env, parent);
575 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
576 lfsck_object_put(env, parent);
577 linkea_next_entry(&ldata);
584 /* If there is no name entry in the parent dir and the object
585 * link count is less than the linkea entries count, then the
586 * linkea entry should be removed. */
587 if (ldata.ld_leh->leh_reccount > la->la_nlink)
590 /* XXX: For the case of there is a linkea entry, but without
591 * name entry pointing to the object and its hard links
592 * count is not less than the object name entries count,
593 * then seems we should add the 'missed' name entry back
594 * to namespace, but before LFSCK phase III finished, we
595 * do not know whether the object has some inconsistency
596 * on other MDTs. So now, do NOT add the name entry back
597 * to the namespace, but keep the linkEA entry. LU-2914 */
598 lfsck_object_put(env, parent);
599 linkea_next_entry(&ldata);
604 lfsck_object_put(env, parent);
605 if (bk->lb_param & LPF_DRYRUN)
608 CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
609 PFID(lfsck_dto2fid(child)), cname->ln_namelen, cname->ln_name,
611 linkea_del_buf(&ldata, cname);
616 if (!com->lc_journal) {
621 rc = lfsck_links_write(env, child, &ldata, handle);
628 /* XXX: For the case linkea entries count does not match the object hard
629 * links count, we cannot update the later one simply. Before LFSCK
630 * phase III finished, we cannot know whether there are some remote
631 * name entries to be repaired or not. LU-2914 */
632 if (rc == 0 && !lfsck_is_dead_obj(child) &&
633 ldata.ld_leh != NULL &&
634 ldata.ld_leh->leh_reccount != la->la_nlink)
635 CWARN("%.16s: the object "DFID" linkEA entry count %u "
636 "may not match its hardlink count %u\n",
637 lfsck_lfsck2name(lfsck), PFID(cfid),
638 ldata.ld_leh->leh_reccount, la->la_nlink);
640 dt_write_unlock(env, child);
644 dt_trans_stop(env, lfsck->li_next, handle);
646 if (rc == 0 && update) {
647 ns->ln_objs_nlink_repaired++;
656 static int lfsck_namespace_reset(const struct lu_env *env,
657 struct lfsck_component *com, bool init)
659 struct lfsck_instance *lfsck = com->lc_lfsck;
660 struct lfsck_namespace *ns =
661 (struct lfsck_namespace *)com->lc_file_ram;
662 struct dt_object *root;
663 struct dt_object *dto;
667 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
669 RETURN(PTR_ERR(root));
671 dt_try_as_dir(env, root);
673 down_write(&com->lc_sem);
675 memset(ns, 0, sizeof(*ns));
677 __u32 count = ns->ln_success_count;
678 __u64 last_time = ns->ln_time_last_complete;
680 memset(ns, 0, sizeof(*ns));
681 ns->ln_success_count = count;
682 ns->ln_time_last_complete = last_time;
684 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
685 ns->ln_status = LS_INIT;
687 rc = local_object_unlink(env, lfsck->li_bottom, root,
688 lfsck_namespace_name);
692 lfsck_object_put(env, com->lc_obj);
694 dto = local_index_find_or_create(env, lfsck->li_los, root,
695 lfsck_namespace_name,
696 S_IFREG | S_IRUGO | S_IWUSR,
699 GOTO(out, rc = PTR_ERR(dto));
702 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
706 rc = lfsck_namespace_store(env, com);
711 up_write(&com->lc_sem);
712 lu_object_put(env, &root->do_lu);
717 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
720 struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
722 down_write(&com->lc_sem);
724 com->lc_new_checked++;
725 ns->ln_items_failed++;
726 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
727 lfsck_pos_fill(env, com->lc_lfsck,
728 &ns->ln_pos_first_inconsistent, false);
729 up_write(&com->lc_sem);
732 static int lfsck_namespace_checkpoint(const struct lu_env *env,
733 struct lfsck_component *com, bool init)
735 struct lfsck_instance *lfsck = com->lc_lfsck;
736 struct lfsck_namespace *ns =
737 (struct lfsck_namespace *)com->lc_file_ram;
740 if (com->lc_new_checked == 0 && !init)
743 down_write(&com->lc_sem);
746 ns->ln_pos_latest_start = lfsck->li_pos_current;
748 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
749 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
750 HALF_SEC - lfsck->li_time_last_checkpoint);
751 ns->ln_time_last_checkpoint = cfs_time_current_sec();
752 ns->ln_items_checked += com->lc_new_checked;
753 com->lc_new_checked = 0;
756 rc = lfsck_namespace_store(env, com);
758 up_write(&com->lc_sem);
762 static int lfsck_namespace_prep(const struct lu_env *env,
763 struct lfsck_component *com)
765 struct lfsck_instance *lfsck = com->lc_lfsck;
766 struct lfsck_namespace *ns =
767 (struct lfsck_namespace *)com->lc_file_ram;
768 struct lfsck_position *pos = &com->lc_pos_start;
770 if (ns->ln_status == LS_COMPLETED) {
773 rc = lfsck_namespace_reset(env, com, false);
778 down_write(&com->lc_sem);
780 ns->ln_time_latest_start = cfs_time_current_sec();
782 spin_lock(&lfsck->li_lock);
783 if (ns->ln_flags & LF_SCANNED_ONCE) {
784 if (!lfsck->li_drop_dryrun ||
785 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
786 ns->ln_status = LS_SCANNING_PHASE2;
787 cfs_list_del_init(&com->lc_link);
788 cfs_list_add_tail(&com->lc_link,
789 &lfsck->li_list_double_scan);
790 if (!cfs_list_empty(&com->lc_link_dir))
791 cfs_list_del_init(&com->lc_link_dir);
792 lfsck_pos_set_zero(pos);
794 ns->ln_status = LS_SCANNING_PHASE1;
795 ns->ln_run_time_phase1 = 0;
796 ns->ln_run_time_phase2 = 0;
797 ns->ln_items_checked = 0;
798 ns->ln_items_repaired = 0;
799 ns->ln_items_failed = 0;
800 ns->ln_dirs_checked = 0;
801 ns->ln_mlinked_checked = 0;
802 ns->ln_objs_checked_phase2 = 0;
803 ns->ln_objs_repaired_phase2 = 0;
804 ns->ln_objs_failed_phase2 = 0;
805 ns->ln_objs_nlink_repaired = 0;
806 ns->ln_objs_lost_found = 0;
807 fid_zero(&ns->ln_fid_latest_scanned_phase2);
808 if (cfs_list_empty(&com->lc_link_dir))
809 cfs_list_add_tail(&com->lc_link_dir,
810 &lfsck->li_list_dir);
811 *pos = ns->ln_pos_first_inconsistent;
814 ns->ln_status = LS_SCANNING_PHASE1;
815 if (cfs_list_empty(&com->lc_link_dir))
816 cfs_list_add_tail(&com->lc_link_dir,
817 &lfsck->li_list_dir);
818 if (!lfsck->li_drop_dryrun ||
819 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
820 *pos = ns->ln_pos_last_checkpoint;
821 pos->lp_oit_cookie++;
823 *pos = ns->ln_pos_first_inconsistent;
826 spin_unlock(&lfsck->li_lock);
828 up_write(&com->lc_sem);
832 static int lfsck_namespace_exec_oit(const struct lu_env *env,
833 struct lfsck_component *com,
834 struct dt_object *obj)
836 down_write(&com->lc_sem);
837 com->lc_new_checked++;
838 if (S_ISDIR(lfsck_object_type(obj)))
839 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
840 up_write(&com->lc_sem);
844 static int lfsck_namespace_exec_dir(const struct lu_env *env,
845 struct lfsck_component *com,
846 struct dt_object *obj,
847 struct lu_dirent *ent)
849 struct lfsck_thread_info *info = lfsck_env_info(env);
850 struct lu_attr *la = &info->lti_la;
851 struct lfsck_instance *lfsck = com->lc_lfsck;
852 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
853 struct lfsck_namespace *ns =
854 (struct lfsck_namespace *)com->lc_file_ram;
855 struct linkea_data ldata = { 0 };
856 const struct lu_fid *pfid =
857 lu_object_fid(&lfsck->li_obj_dir->do_lu);
858 const struct lu_fid *cfid = lfsck_dto2fid(obj);
859 const struct lu_name *cname;
860 struct thandle *handle = NULL;
861 bool repaired = false;
869 cname = lfsck_name_get_const(env, ent->lde_name, ent->lde_namelen);
870 down_write(&com->lc_sem);
871 com->lc_new_checked++;
873 if (ent->lde_attrs & LUDA_UPGRADE) {
874 ns->ln_flags |= LF_UPGRADE;
876 } else if (ent->lde_attrs & LUDA_REPAIR) {
877 ns->ln_flags |= LF_INCONSISTENT;
881 if (ent->lde_name[0] == '.' &&
882 (ent->lde_namelen == 1 ||
883 (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
884 fid_is_dot_lustre(&ent->lde_fid)))
887 if (!(bk->lb_param & LPF_DRYRUN) &&
888 (com->lc_journal || repaired)) {
894 handle = dt_trans_create(env, lfsck->li_next);
896 GOTO(out, rc = PTR_ERR(handle));
898 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
902 rc = dt_trans_start(env, lfsck->li_next, handle);
906 dt_write_lock(env, obj, MOR_TGT_CHILD);
910 rc = lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
914 rc = lfsck_links_read(env, obj, &ldata);
916 count = ldata.ld_leh->leh_reccount;
917 rc = linkea_links_find(&ldata, cname, pfid);
919 (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
922 ns->ln_flags |= LF_INCONSISTENT;
923 /* For dir, if there are more than one linkea entries, or the
924 * linkea entry does not match the name entry, then remove all
925 * and add the correct one. */
926 if (S_ISDIR(lfsck_object_type(obj))) {
934 } else if (unlikely(rc == -EINVAL)) {
936 ns->ln_flags |= LF_INCONSISTENT;
937 /* The magic crashed, we are not sure whether there are more
938 * corrupt data in the linkea, so remove all linkea entries. */
942 } else if (rc == -ENODATA) {
944 ns->ln_flags |= LF_UPGRADE;
949 if (bk->lb_param & LPF_DRYRUN) {
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;
990 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
995 (la->la_nlink == 1 || S_ISDIR(lfsck_object_type(obj))))
996 /* Usually, it is for single linked object or dir, do nothing.*/
999 /* Following modification will be in another transaction. */
1000 if (handle != NULL) {
1001 LASSERT(dt_write_locked(env, obj));
1003 dt_write_unlock(env, obj);
1006 dt_trans_stop(env, lfsck->li_next, handle);
1010 ns->ln_mlinked_checked++;
1011 rc = lfsck_namespace_update(env, com, cfid,
1012 count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
1018 dt_write_unlock(env, obj);
1021 dt_trans_stop(env, lfsck->li_next, handle);
1025 ns->ln_items_failed++;
1026 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1027 lfsck_pos_fill(env, lfsck,
1028 &ns->ln_pos_first_inconsistent, false);
1029 if (!(bk->lb_param & LPF_FAILOUT))
1033 ns->ln_items_repaired++;
1034 if (bk->lb_param & LPF_DRYRUN &&
1035 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1036 lfsck_pos_fill(env, lfsck,
1037 &ns->ln_pos_first_inconsistent,
1040 com->lc_journal = 0;
1044 up_write(&com->lc_sem);
1048 static int lfsck_namespace_post(const struct lu_env *env,
1049 struct lfsck_component *com,
1050 int result, bool init)
1052 struct lfsck_instance *lfsck = com->lc_lfsck;
1053 struct lfsck_namespace *ns =
1054 (struct lfsck_namespace *)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 if (lfsck->li_paused) {
1071 ns->ln_status = LS_PAUSED;
1073 ns->ln_status = LS_STOPPED;
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);
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 =
1107 (struct lfsck_namespace *)com->lc_file_ram;
1112 down_read(&com->lc_sem);
1113 rc = snprintf(buf, len,
1114 "name: lfsck_namespace\n"
1120 lfsck_status_names[ns->ln_status]);
1126 rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1131 rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1136 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1137 "time_since_last_completed");
1141 rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1142 "time_since_latest_start");
1146 rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1147 "time_since_last_checkpoint");
1151 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1152 "latest_start_position");
1156 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1157 "last_checkpoint_position");
1161 rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1162 "first_failure_position");
1166 if (ns->ln_status == LS_SCANNING_PHASE1) {
1167 struct lfsck_position pos;
1168 const struct dt_it_ops *iops;
1169 cfs_duration_t duration = cfs_time_current() -
1170 lfsck->li_time_last_checkpoint;
1171 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1172 __u64 speed = checked;
1173 __u64 new_checked = com->lc_new_checked * HZ;
1174 __u32 rtime = ns->ln_run_time_phase1 +
1175 cfs_duration_sec(duration + HALF_SEC);
1178 do_div(new_checked, duration);
1180 do_div(speed, rtime);
1181 rc = snprintf(buf, len,
1182 "checked_phase1: "LPU64"\n"
1183 "checked_phase2: "LPU64"\n"
1184 "updated_phase1: "LPU64"\n"
1185 "updated_phase2: "LPU64"\n"
1186 "failed_phase1: "LPU64"\n"
1187 "failed_phase2: "LPU64"\n"
1189 "M-linked: "LPU64"\n"
1190 "nlinks_repaired: "LPU64"\n"
1191 "lost_found: "LPU64"\n"
1192 "success_count: %u\n"
1193 "run_time_phase1: %u seconds\n"
1194 "run_time_phase2: %u seconds\n"
1195 "average_speed_phase1: "LPU64" items/sec\n"
1196 "average_speed_phase2: N/A\n"
1197 "real-time_speed_phase1: "LPU64" items/sec\n"
1198 "real-time_speed_phase2: N/A\n",
1200 ns->ln_objs_checked_phase2,
1201 ns->ln_items_repaired,
1202 ns->ln_objs_repaired_phase2,
1203 ns->ln_items_failed,
1204 ns->ln_objs_failed_phase2,
1205 ns->ln_dirs_checked,
1206 ns->ln_mlinked_checked,
1207 ns->ln_objs_nlink_repaired,
1208 ns->ln_objs_lost_found,
1209 ns->ln_success_count,
1211 ns->ln_run_time_phase2,
1220 LASSERT(lfsck->li_di_oit != NULL);
1222 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1224 /* The low layer otable-based iteration position may NOT
1225 * exactly match the namespace-based directory traversal
1226 * cookie. Generally, it is not a serious issue. But the
1227 * caller should NOT make assumption on that. */
1228 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1229 if (!lfsck->li_current_oit_processed)
1230 pos.lp_oit_cookie--;
1232 spin_lock(&lfsck->li_lock);
1233 if (lfsck->li_di_dir != NULL) {
1234 pos.lp_dir_cookie = lfsck->li_cookie_dir;
1235 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
1236 fid_zero(&pos.lp_dir_parent);
1237 pos.lp_dir_cookie = 0;
1240 *lu_object_fid(&lfsck->li_obj_dir->do_lu);
1243 fid_zero(&pos.lp_dir_parent);
1244 pos.lp_dir_cookie = 0;
1246 spin_unlock(&lfsck->li_lock);
1247 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1250 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1251 cfs_duration_t duration = cfs_time_current() -
1252 lfsck->li_time_last_checkpoint;
1253 __u64 checked = ns->ln_objs_checked_phase2 +
1254 com->lc_new_checked;
1255 __u64 speed1 = ns->ln_items_checked;
1256 __u64 speed2 = checked;
1257 __u64 new_checked = com->lc_new_checked * HZ;
1258 __u32 rtime = ns->ln_run_time_phase2 +
1259 cfs_duration_sec(duration + HALF_SEC);
1262 do_div(new_checked, duration);
1263 if (ns->ln_run_time_phase1 != 0)
1264 do_div(speed1, ns->ln_run_time_phase1);
1266 do_div(speed2, rtime);
1267 rc = snprintf(buf, len,
1268 "checked_phase1: "LPU64"\n"
1269 "checked_phase2: "LPU64"\n"
1270 "updated_phase1: "LPU64"\n"
1271 "updated_phase2: "LPU64"\n"
1272 "failed_phase1: "LPU64"\n"
1273 "failed_phase2: "LPU64"\n"
1275 "M-linked: "LPU64"\n"
1276 "nlinks_repaired: "LPU64"\n"
1277 "lost_found: "LPU64"\n"
1278 "success_count: %u\n"
1279 "run_time_phase1: %u seconds\n"
1280 "run_time_phase2: %u seconds\n"
1281 "average_speed_phase1: "LPU64" items/sec\n"
1282 "average_speed_phase2: "LPU64" objs/sec\n"
1283 "real-time_speed_phase1: N/A\n"
1284 "real-time_speed_phase2: "LPU64" objs/sec\n"
1285 "current_position: "DFID"\n",
1286 ns->ln_items_checked,
1288 ns->ln_items_repaired,
1289 ns->ln_objs_repaired_phase2,
1290 ns->ln_items_failed,
1291 ns->ln_objs_failed_phase2,
1292 ns->ln_dirs_checked,
1293 ns->ln_mlinked_checked,
1294 ns->ln_objs_nlink_repaired,
1295 ns->ln_objs_lost_found,
1296 ns->ln_success_count,
1297 ns->ln_run_time_phase1,
1302 PFID(&ns->ln_fid_latest_scanned_phase2));
1309 __u64 speed1 = ns->ln_items_checked;
1310 __u64 speed2 = ns->ln_objs_checked_phase2;
1312 if (ns->ln_run_time_phase1 != 0)
1313 do_div(speed1, ns->ln_run_time_phase1);
1314 if (ns->ln_run_time_phase2 != 0)
1315 do_div(speed2, ns->ln_run_time_phase2);
1316 rc = snprintf(buf, len,
1317 "checked_phase1: "LPU64"\n"
1318 "checked_phase2: "LPU64"\n"
1319 "updated_phase1: "LPU64"\n"
1320 "updated_phase2: "LPU64"\n"
1321 "failed_phase1: "LPU64"\n"
1322 "failed_phase2: "LPU64"\n"
1324 "M-linked: "LPU64"\n"
1325 "nlinks_repaired: "LPU64"\n"
1326 "lost_found: "LPU64"\n"
1327 "success_count: %u\n"
1328 "run_time_phase1: %u seconds\n"
1329 "run_time_phase2: %u seconds\n"
1330 "average_speed_phase1: "LPU64" items/sec\n"
1331 "average_speed_phase2: "LPU64" objs/sec\n"
1332 "real-time_speed_phase1: N/A\n"
1333 "real-time_speed_phase2: N/A\n"
1334 "current_position: N/A\n",
1335 ns->ln_items_checked,
1336 ns->ln_objs_checked_phase2,
1337 ns->ln_items_repaired,
1338 ns->ln_objs_repaired_phase2,
1339 ns->ln_items_failed,
1340 ns->ln_objs_failed_phase2,
1341 ns->ln_dirs_checked,
1342 ns->ln_mlinked_checked,
1343 ns->ln_objs_nlink_repaired,
1344 ns->ln_objs_lost_found,
1345 ns->ln_success_count,
1346 ns->ln_run_time_phase1,
1347 ns->ln_run_time_phase2,
1359 up_read(&com->lc_sem);
1363 static int lfsck_namespace_double_scan(const struct lu_env *env,
1364 struct lfsck_component *com)
1366 struct lfsck_instance *lfsck = com->lc_lfsck;
1367 struct ptlrpc_thread *thread = &lfsck->li_thread;
1368 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1369 struct lfsck_namespace *ns =
1370 (struct lfsck_namespace *)com->lc_file_ram;
1371 struct dt_object *obj = com->lc_obj;
1372 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
1373 struct dt_object *target;
1381 lfsck->li_new_scanned = 0;
1382 lfsck->li_time_last_checkpoint = cfs_time_current();
1383 lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
1384 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1386 di = iops->init(env, obj, 0, BYPASS_CAPA);
1388 RETURN(PTR_ERR(di));
1390 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1391 rc = iops->get(env, di, (const struct dt_key *)&fid);
1395 /* Skip the start one, which either has been processed or non-exist. */
1396 rc = iops->next(env, di);
1400 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1404 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1406 struct l_wait_info lwi;
1408 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1410 l_wait_event(thread->t_ctl_waitq,
1411 !thread_is_running(thread),
1415 key = iops->key(env, di);
1416 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1417 target = lfsck_object_find(env, lfsck, &fid);
1418 down_write(&com->lc_sem);
1419 if (target == NULL) {
1422 } else if (IS_ERR(target)) {
1423 rc = PTR_ERR(target);
1427 /* XXX: Currently, skip remote object, the consistency for
1428 * remote object will be processed in LFSCK phase III. */
1429 if (dt_object_exists(target) && !dt_object_remote(target)) {
1430 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1432 rc = lfsck_namespace_double_scan_one(env, com,
1436 lfsck_object_put(env, target);
1439 lfsck->li_new_scanned++;
1440 com->lc_new_checked++;
1441 ns->ln_fid_latest_scanned_phase2 = fid;
1443 ns->ln_objs_repaired_phase2++;
1445 ns->ln_objs_failed_phase2++;
1446 up_write(&com->lc_sem);
1448 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1449 lfsck_namespace_delete(env, com, &fid);
1450 } else if (rc < 0) {
1451 flags |= LLF_REPAIR_FAILED;
1452 lfsck_namespace_update(env, com, &fid, flags, true);
1455 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1458 if (unlikely(cfs_time_beforeq(lfsck->li_time_next_checkpoint,
1459 cfs_time_current())) &&
1460 com->lc_new_checked != 0) {
1461 down_write(&com->lc_sem);
1462 ns->ln_run_time_phase2 +=
1463 cfs_duration_sec(cfs_time_current() +
1464 HALF_SEC - lfsck->li_time_last_checkpoint);
1465 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1466 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1467 com->lc_new_checked = 0;
1468 rc = lfsck_namespace_store(env, com);
1469 up_write(&com->lc_sem);
1473 lfsck->li_time_last_checkpoint = cfs_time_current();
1474 lfsck->li_time_next_checkpoint =
1475 lfsck->li_time_last_checkpoint +
1476 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1479 lfsck_control_speed(lfsck);
1480 if (unlikely(!thread_is_running(thread)))
1483 rc = iops->next(env, di);
1492 iops->fini(env, di);
1493 down_write(&com->lc_sem);
1495 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1496 HALF_SEC - lfsck->li_time_last_checkpoint);
1497 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1498 ns->ln_objs_checked_phase2 += com->lc_new_checked;
1499 com->lc_new_checked = 0;
1502 com->lc_journal = 0;
1503 ns->ln_status = LS_COMPLETED;
1504 if (!(bk->lb_param & LPF_DRYRUN))
1506 ~(LF_SCANNED_ONCE | LF_INCONSISTENT | LF_UPGRADE);
1507 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
1508 ns->ln_success_count++;
1509 } else if (rc == 0) {
1510 if (lfsck->li_paused)
1511 ns->ln_status = LS_PAUSED;
1513 ns->ln_status = LS_STOPPED;
1515 ns->ln_status = LS_FAILED;
1518 if (ns->ln_status != LS_PAUSED) {
1519 spin_lock(&lfsck->li_lock);
1520 cfs_list_del_init(&com->lc_link);
1521 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1522 spin_unlock(&lfsck->li_lock);
1525 rc = lfsck_namespace_store(env, com);
1527 up_write(&com->lc_sem);
1531 static struct lfsck_operations lfsck_namespace_ops = {
1532 .lfsck_reset = lfsck_namespace_reset,
1533 .lfsck_fail = lfsck_namespace_fail,
1534 .lfsck_checkpoint = lfsck_namespace_checkpoint,
1535 .lfsck_prep = lfsck_namespace_prep,
1536 .lfsck_exec_oit = lfsck_namespace_exec_oit,
1537 .lfsck_exec_dir = lfsck_namespace_exec_dir,
1538 .lfsck_post = lfsck_namespace_post,
1539 .lfsck_dump = lfsck_namespace_dump,
1540 .lfsck_double_scan = lfsck_namespace_double_scan,
1543 int lfsck_namespace_setup(const struct lu_env *env,
1544 struct lfsck_instance *lfsck)
1546 struct lfsck_component *com;
1547 struct lfsck_namespace *ns;
1548 struct dt_object *root = NULL;
1549 struct dt_object *obj;
1553 LASSERT(lfsck->li_master);
1559 CFS_INIT_LIST_HEAD(&com->lc_link);
1560 CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1561 init_rwsem(&com->lc_sem);
1562 atomic_set(&com->lc_ref, 1);
1563 com->lc_lfsck = lfsck;
1564 com->lc_type = LT_NAMESPACE;
1565 com->lc_ops = &lfsck_namespace_ops;
1566 com->lc_file_size = sizeof(struct lfsck_namespace);
1567 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1568 if (com->lc_file_ram == NULL)
1569 GOTO(out, rc = -ENOMEM);
1571 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1572 if (com->lc_file_disk == NULL)
1573 GOTO(out, rc = -ENOMEM);
1575 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1577 GOTO(out, rc = PTR_ERR(root));
1579 dt_try_as_dir(env, root);
1580 obj = local_index_find_or_create(env, lfsck->li_los, root,
1581 lfsck_namespace_name,
1582 S_IFREG | S_IRUGO | S_IWUSR,
1583 &dt_lfsck_features);
1585 GOTO(out, rc = PTR_ERR(obj));
1588 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1592 rc = lfsck_namespace_load(env, com);
1594 rc = lfsck_namespace_init(env, com);
1596 rc = lfsck_namespace_reset(env, com, true);
1600 ns = (struct lfsck_namespace *)com->lc_file_ram;
1601 switch (ns->ln_status) {
1606 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1609 CERROR("%s: unknown lfsck_namespace status: %u\n",
1610 lfsck_lfsck2name(lfsck), ns->ln_status);
1612 case LS_SCANNING_PHASE1:
1613 case LS_SCANNING_PHASE2:
1614 /* No need to store the status to disk right now.
1615 * If the system crashed before the status stored,
1616 * it will be loaded back when next time. */
1617 ns->ln_status = LS_CRASHED;
1621 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1622 cfs_list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
1629 if (root != NULL && !IS_ERR(root))
1630 lu_object_put(env, &root->do_lu);
1632 lfsck_component_cleanup(env, com);