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_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
42 #include "lfsck_internal.h"
44 #define LFSCK_NAMESPACE_MAGIC 0xA0629D03
46 enum lfsck_nameentry_check {
47 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
48 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
49 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
52 static const char lfsck_namespace_name[] = "lfsck_namespace";
54 struct lfsck_namespace_req {
55 struct lfsck_assistant_req lnr_lar;
56 struct dt_object *lnr_obj;
57 struct lu_fid lnr_fid;
67 static struct lfsck_namespace_req *
68 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
69 struct lu_dirent *ent, __u16 type)
71 struct lfsck_namespace_req *lnr;
74 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
77 return ERR_PTR(-ENOMEM);
79 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
80 lu_object_get(&lfsck->li_obj_dir->do_lu);
81 lnr->lnr_obj = lfsck->li_obj_dir;
82 lnr->lnr_fid = ent->lde_fid;
83 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
84 lnr->lnr_dir_cookie = ent->lde_hash;
85 lnr->lnr_attr = ent->lde_attrs;
88 lnr->lnr_namelen = ent->lde_namelen;
89 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
94 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
95 struct lfsck_assistant_req *lar)
97 struct lfsck_namespace_req *lnr =
98 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
100 lu_object_put(env, &lnr->lnr_obj->do_lu);
101 OBD_FREE(lnr, lnr->lnr_size);
104 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
105 struct lfsck_namespace *src)
107 dst->ln_magic = le32_to_cpu(src->ln_magic);
108 dst->ln_status = le32_to_cpu(src->ln_status);
109 dst->ln_flags = le32_to_cpu(src->ln_flags);
110 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
111 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
112 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
113 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
114 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
115 dst->ln_time_last_checkpoint =
116 le64_to_cpu(src->ln_time_last_checkpoint);
117 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
118 &src->ln_pos_latest_start);
119 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
120 &src->ln_pos_last_checkpoint);
121 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
122 &src->ln_pos_first_inconsistent);
123 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
124 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
125 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
126 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
127 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
128 dst->ln_objs_repaired_phase2 =
129 le64_to_cpu(src->ln_objs_repaired_phase2);
130 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
131 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
132 dst->ln_objs_lost_found = le64_to_cpu(src->ln_objs_lost_found);
133 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
134 &src->ln_fid_latest_scanned_phase2);
135 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
136 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
137 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
138 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
139 dst->ln_unknown_inconsistency =
140 le64_to_cpu(src->ln_unknown_inconsistency);
141 dst->ln_unmatched_pairs_repaired =
142 le64_to_cpu(src->ln_unmatched_pairs_repaired);
145 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
146 struct lfsck_namespace *src)
148 dst->ln_magic = cpu_to_le32(src->ln_magic);
149 dst->ln_status = cpu_to_le32(src->ln_status);
150 dst->ln_flags = cpu_to_le32(src->ln_flags);
151 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
152 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
153 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
154 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
155 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
156 dst->ln_time_last_checkpoint =
157 cpu_to_le64(src->ln_time_last_checkpoint);
158 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
159 &src->ln_pos_latest_start);
160 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
161 &src->ln_pos_last_checkpoint);
162 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
163 &src->ln_pos_first_inconsistent);
164 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
165 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
166 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
167 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
168 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
169 dst->ln_objs_repaired_phase2 =
170 cpu_to_le64(src->ln_objs_repaired_phase2);
171 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
172 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
173 dst->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
174 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
175 &src->ln_fid_latest_scanned_phase2);
176 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
177 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
178 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
179 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
180 dst->ln_unknown_inconsistency =
181 cpu_to_le64(src->ln_unknown_inconsistency);
182 dst->ln_unmatched_pairs_repaired =
183 cpu_to_le64(src->ln_unmatched_pairs_repaired);
186 static void lfsck_namespace_record_failure(const struct lu_env *env,
187 struct lfsck_instance *lfsck,
188 struct lfsck_namespace *ns)
190 struct lfsck_position pos;
192 ns->ln_items_failed++;
193 lfsck_pos_fill(env, lfsck, &pos, false);
194 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
195 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
196 ns->ln_pos_first_inconsistent = pos;
198 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
199 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
200 lfsck_lfsck2name(lfsck),
201 ns->ln_pos_first_inconsistent.lp_oit_cookie,
202 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
203 ns->ln_pos_first_inconsistent.lp_dir_cookie);
208 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
209 * \retval 0: succeed.
210 * \retval -ve: failed cases.
212 static int lfsck_namespace_load(const struct lu_env *env,
213 struct lfsck_component *com)
215 int len = com->lc_file_size;
218 rc = dt_xattr_get(env, com->lc_obj,
219 lfsck_buf_get(env, com->lc_file_disk, len),
220 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
222 struct lfsck_namespace *ns = com->lc_file_ram;
224 lfsck_namespace_le_to_cpu(ns,
225 (struct lfsck_namespace *)com->lc_file_disk);
226 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
227 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
228 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
229 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
234 } else if (rc != -ENODATA) {
235 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
236 "expected = %d: rc = %d\n",
237 lfsck_lfsck2name(com->lc_lfsck), len, rc);
244 static int lfsck_namespace_store(const struct lu_env *env,
245 struct lfsck_component *com, bool init)
247 struct dt_object *obj = com->lc_obj;
248 struct lfsck_instance *lfsck = com->lc_lfsck;
249 struct thandle *handle;
250 int len = com->lc_file_size;
254 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
255 (struct lfsck_namespace *)com->lc_file_ram);
256 handle = dt_trans_create(env, lfsck->li_bottom);
258 GOTO(log, rc = PTR_ERR(handle));
260 rc = dt_declare_xattr_set(env, obj,
261 lfsck_buf_get(env, com->lc_file_disk, len),
262 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
266 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
270 rc = dt_xattr_set(env, obj,
271 lfsck_buf_get(env, com->lc_file_disk, len),
272 XATTR_NAME_LFSCK_NAMESPACE,
273 init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
274 handle, BYPASS_CAPA);
279 dt_trans_stop(env, lfsck->li_bottom, handle);
283 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
284 lfsck_lfsck2name(lfsck), rc);
288 static int lfsck_namespace_init(const struct lu_env *env,
289 struct lfsck_component *com)
291 struct lfsck_namespace *ns = com->lc_file_ram;
294 memset(ns, 0, sizeof(*ns));
295 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
296 ns->ln_status = LS_INIT;
297 down_write(&com->lc_sem);
298 rc = lfsck_namespace_store(env, com, true);
299 up_write(&com->lc_sem);
304 * Update the namespace LFSCK tracing file for the given @fid
306 * \param[in] env pointer to the thread context
307 * \param[in] com pointer to the lfsck component
308 * \param[in] fid the fid which flags to be updated in the lfsck
310 * \param[in] add true if add new flags, otherwise remove flags
312 * \retval 0 for succeed or nothing to be done
313 * \retval negative error number on failure
315 int lfsck_namespace_trace_update(const struct lu_env *env,
316 struct lfsck_component *com,
317 const struct lu_fid *fid,
318 const __u8 flags, bool add)
320 struct lfsck_instance *lfsck = com->lc_lfsck;
321 struct dt_object *obj = com->lc_obj;
322 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
323 struct dt_device *dev = lfsck->li_bottom;
324 struct thandle *th = NULL;
332 down_write(&com->lc_sem);
333 fid_cpu_to_be(key, fid);
334 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
335 (const struct dt_key *)key, BYPASS_CAPA);
338 GOTO(unlock, rc = 0);
342 } else if (rc == 0) {
344 if ((old & flags) == flags)
345 GOTO(unlock, rc = 0);
349 if ((old & flags) == 0)
350 GOTO(unlock, rc = 0);
358 th = dt_trans_create(env, dev);
360 GOTO(log, rc = PTR_ERR(th));
363 rc = dt_declare_delete(env, obj,
364 (const struct dt_key *)key, th);
370 rc = dt_declare_insert(env, obj,
371 (const struct dt_rec *)&new,
372 (const struct dt_key *)key, th);
377 rc = dt_trans_start_local(env, dev, th);
382 rc = dt_delete(env, obj, (const struct dt_key *)key,
389 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
390 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
398 if (th != NULL && !IS_ERR(th))
399 dt_trans_stop(env, dev, th);
401 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
402 "tracing file, flags %x, old %x, new %x: rc = %d\n",
403 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
404 (__u32)flags, (__u32)old, (__u32)new, rc);
407 up_write(&com->lc_sem);
412 static int lfsck_namespace_check_exist(const struct lu_env *env,
413 struct dt_object *dir,
414 struct dt_object *obj, const char *name)
416 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
420 if (unlikely(lfsck_is_dead_obj(obj)))
421 RETURN(LFSCK_NAMEENTRY_DEAD);
423 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
424 (const struct dt_key *)name, BYPASS_CAPA);
426 RETURN(LFSCK_NAMEENTRY_REMOVED);
431 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
432 RETURN(LFSCK_NAMEENTRY_RECREATED);
437 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
438 struct dt_object *obj,
439 struct thandle *handle)
443 /* For destroying all invalid linkEA entries. */
444 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
448 /* For insert new linkEA entry. */
449 rc = dt_declare_xattr_set(env, obj,
450 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
451 XATTR_NAME_LINK, 0, handle);
455 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
456 struct linkea_data *ldata)
460 if (ldata->ld_buf->lb_buf == NULL)
463 if (!dt_object_exists(obj))
466 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
468 /* Buf was too small, figure out what we need. */
469 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
474 lu_buf_realloc(ldata->ld_buf, rc);
475 if (ldata->ld_buf->lb_buf == NULL)
478 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
483 rc = linkea_init(ldata);
489 * Remove linkEA for the given object.
491 * The caller should take the ldlm lock before the calling.
493 * \param[in] env pointer to the thread context
494 * \param[in] com pointer to the lfsck component
495 * \param[in] obj pointer to the dt_object to be handled
497 * \retval 0 for repaired cases
498 * \retval negative error number on failure
500 static int lfsck_namespace_links_remove(const struct lu_env *env,
501 struct lfsck_component *com,
502 struct dt_object *obj)
504 struct lfsck_instance *lfsck = com->lc_lfsck;
505 struct dt_device *dev = lfsck->li_bottom;
506 struct thandle *th = NULL;
510 LASSERT(dt_object_remote(obj) == 0);
512 th = dt_trans_create(env, dev);
514 GOTO(log, rc = PTR_ERR(th));
516 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
520 rc = dt_trans_start_local(env, dev, th);
524 dt_write_lock(env, obj, 0);
525 if (unlikely(lfsck_is_dead_obj(obj)))
526 GOTO(unlock, rc = -ENOENT);
528 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
529 GOTO(unlock, rc = 0);
531 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
536 dt_write_unlock(env, obj);
539 dt_trans_stop(env, dev, th);
542 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
543 "for the object "DFID": rc = %d\n",
544 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
547 struct lfsck_namespace *ns = com->lc_file_ram;
549 ns->ln_flags |= LF_INCONSISTENT;
555 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
556 struct linkea_data *ldata, struct thandle *handle)
558 const struct lu_buf *buf = lfsck_buf_get_const(env,
559 ldata->ld_buf->lb_buf,
560 ldata->ld_leh->leh_len);
562 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
566 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
567 struct lu_name *cname,
571 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
572 /* To guarantee the 'name' is terminated with '0'. */
573 memcpy(buf, cname->ln_name, cname->ln_namelen);
574 buf[cname->ln_namelen] = 0;
575 cname->ln_name = buf;
578 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
579 struct lu_name *cname,
583 struct link_ea_entry *oldlee;
587 oldlee = ldata->ld_lee;
588 oldlen = ldata->ld_reclen;
589 linkea_next_entry(ldata);
590 while (ldata->ld_lee != NULL) {
591 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
592 ldata->ld_lee->lee_reclen[1];
593 if (unlikely(ldata->ld_reclen == oldlen &&
594 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
599 linkea_del_buf(ldata, cname);
601 linkea_next_entry(ldata);
604 ldata->ld_lee = oldlee;
605 ldata->ld_reclen = oldlen;
610 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
611 struct lfsck_component *com,
612 struct dt_object *orphan,
613 const char *infix, const char *type,
620 static int lfsck_namespace_insert_normal(const struct lu_env *env,
621 struct lfsck_component *com,
622 struct dt_object *parent,
623 struct dt_object *child,
630 static int lfsck_namespace_create_orphan(const struct lu_env *env,
631 struct lfsck_component *com,
632 struct dt_object *orphan)
639 * Remove the specified entry from the linkEA.
641 * Locate the linkEA entry with the given @cname and @pfid, then
642 * remove this entry or the other entries those are repeated with
645 * \param[in] env pointer to the thread context
646 * \param[in] com pointer to the lfsck component
647 * \param[in] obj pointer to the dt_object to be handled
648 * \param[in,out]ldata pointer to the buffer that holds the linkEA
649 * \param[in] cname the name for the child in the parent directory
650 * \param[in] pfid the parent directory's FID for the linkEA
651 * \param[in] next if true, then remove the first found linkEA
652 * entry, and move the ldata->ld_lee to next entry
654 * \retval positive number for repaired cases
655 * \retval 0 if nothing to be repaired
656 * \retval negative error number on failure
658 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
659 struct lfsck_component *com,
660 struct dt_object *obj,
661 struct linkea_data *ldata,
662 struct lu_name *cname,
666 struct lfsck_instance *lfsck = com->lc_lfsck;
667 struct dt_device *dev = lfsck->li_bottom;
668 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
669 struct thandle *th = NULL;
670 struct lustre_handle lh = { 0 };
671 struct linkea_data ldata_new = { 0 };
672 struct lu_buf linkea_buf;
676 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
677 MDS_INODELOCK_UPDATE |
678 MDS_INODELOCK_XATTR, LCK_EX);
683 linkea_del_buf(ldata, cname);
685 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
687 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
688 ldata->ld_leh->leh_len);
691 th = dt_trans_create(env, dev);
693 GOTO(unlock1, rc = PTR_ERR(th));
695 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
696 XATTR_NAME_LINK, 0, th);
700 rc = dt_trans_start_local(env, dev, th);
704 dt_write_lock(env, obj, 0);
705 if (unlikely(lfsck_is_dead_obj(obj)))
706 GOTO(unlock2, rc = -ENOENT);
708 rc = lfsck_links_read2(env, obj, &ldata_new);
712 /* The specified linkEA entry has been removed by race. */
713 rc = linkea_links_find(&ldata_new, cname, pfid);
715 GOTO(unlock2, rc = 0);
717 if (bk->lb_param & LPF_DRYRUN)
718 GOTO(unlock2, rc = 1);
721 linkea_del_buf(&ldata_new, cname);
723 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
726 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
727 dt_write_unlock(env, obj);
728 dt_trans_stop(env, dev, th);
729 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
730 ldata_new.ld_leh->leh_len);
734 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
735 ldata_new.ld_leh->leh_len);
736 rc = dt_xattr_set(env, obj, &linkea_buf,
737 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
739 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
742 dt_write_unlock(env, obj);
745 dt_trans_stop(env, dev, th);
748 lfsck_ibits_unlock(&lh, LCK_EX);
751 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
752 "for the object: "DFID", parent "DFID", name %.*s\n",
753 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
754 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
758 struct lfsck_namespace *ns = com->lc_file_ram;
760 ns->ln_flags |= LF_INCONSISTENT;
767 * Conditionally remove the specified entry from the linkEA.
769 * Take the parent lock firstly, then check whether the specified
770 * name entry exists or not: if yes, do nothing; otherwise, call
771 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
773 * \param[in] env pointer to the thread context
774 * \param[in] com pointer to the lfsck component
775 * \param[in] parent pointer to the parent directory
776 * \param[in] child pointer to the child object that holds the linkEA
777 * \param[in,out]ldata pointer to the buffer that holds the linkEA
778 * \param[in] cname the name for the child in the parent directory
779 * \param[in] pfid the parent directory's FID for the linkEA
781 * \retval positive number for repaired cases
782 * \retval 0 if nothing to be repaired
783 * \retval negative error number on failure
785 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
786 struct lfsck_component *com,
787 struct dt_object *parent,
788 struct dt_object *child,
789 struct linkea_data *ldata,
790 struct lu_name *cname,
793 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
794 struct lustre_handle lh = { 0 };
798 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
799 MDS_INODELOCK_UPDATE, LCK_EX);
803 dt_read_lock(env, parent, 0);
804 if (unlikely(lfsck_is_dead_obj(parent))) {
805 dt_read_unlock(env, parent);
806 lfsck_ibits_unlock(&lh, LCK_EX);
807 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
813 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
814 (const struct dt_key *)cname->ln_name,
816 dt_read_unlock(env, parent);
818 /* It is safe to release the ldlm lock, because when the logic come
819 * here, we have got all the needed information above whether the
820 * linkEA entry is valid or not. It is not important that others
821 * may add new linkEA entry after the ldlm lock released. If other
822 * has removed the specified linkEA entry by race, then it is OK,
823 * because the subsequent lfsck_namespace_shrink_linkea() can handle
825 lfsck_ibits_unlock(&lh, LCK_EX);
827 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
836 /* The LFSCK just found some internal status of cross-MDTs
837 * create operation. That is normal. */
838 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
839 linkea_next_entry(ldata);
844 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
851 * Overwrite the linkEA for the object with the given ldata.
853 * The caller should take the ldlm lock before the calling.
855 * \param[in] env pointer to the thread context
856 * \param[in] com pointer to the lfsck component
857 * \param[in] obj pointer to the dt_object to be handled
858 * \param[in] ldata pointer to the new linkEA data
860 * \retval positive number for repaired cases
861 * \retval 0 if nothing to be repaired
862 * \retval negative error number on failure
864 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
865 struct lfsck_component *com,
866 struct dt_object *obj,
867 struct linkea_data *ldata)
869 struct lfsck_instance *lfsck = com->lc_lfsck;
870 struct dt_device *dev = lfsck->li_bottom;
871 struct thandle *th = NULL;
872 struct lu_buf linkea_buf;
876 LASSERT(!dt_object_remote(obj));
878 th = dt_trans_create(env, dev);
880 GOTO(log, rc = PTR_ERR(th));
882 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
883 ldata->ld_leh->leh_len);
884 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
885 XATTR_NAME_LINK, 0, th);
889 rc = dt_trans_start_local(env, dev, th);
893 dt_write_lock(env, obj, 0);
894 if (unlikely(lfsck_is_dead_obj(obj)))
895 GOTO(unlock, rc = 0);
897 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
898 GOTO(unlock, rc = 1);
900 rc = dt_xattr_set(env, obj, &linkea_buf,
901 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
903 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
906 dt_write_unlock(env, obj);
909 dt_trans_stop(env, dev, th);
912 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
913 "object "DFID": rc = %d\n",
914 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
917 struct lfsck_namespace *ns = com->lc_file_ram;
919 ns->ln_flags |= LF_INCONSISTENT;
926 * Update the ".." name entry for the given object.
928 * The object's ".." is corrupted, this function will update the ".." name
929 * entry with the given pfid, and the linkEA with the given ldata.
931 * The caller should take the ldlm lock before the calling.
933 * \param[in] env pointer to the thread context
934 * \param[in] com pointer to the lfsck component
935 * \param[in] obj pointer to the dt_object to be handled
936 * \param[in] pfid the new fid for the object's ".." name entry
937 * \param[in] cname the name for the @obj in the parent directory
939 * \retval positive number for repaired cases
940 * \retval 0 if nothing to be repaired
941 * \retval negative error number on failure
943 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
944 struct lfsck_component *com,
945 struct dt_object *obj,
946 const struct lu_fid *pfid,
947 struct lu_name *cname)
949 struct lfsck_thread_info *info = lfsck_env_info(env);
950 struct dt_insert_rec *rec = &info->lti_dt_rec;
951 struct lfsck_instance *lfsck = com->lc_lfsck;
952 struct dt_device *dev = lfsck->li_bottom;
953 struct thandle *th = NULL;
954 struct linkea_data ldata = { 0 };
955 struct lu_buf linkea_buf;
959 LASSERT(!dt_object_remote(obj));
960 LASSERT(S_ISDIR(lfsck_object_type(obj)));
962 rc = linkea_data_new(&ldata, &info->lti_big_buf);
966 rc = linkea_add_buf(&ldata, cname, pfid);
970 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
971 ldata.ld_leh->leh_len);
973 th = dt_trans_create(env, dev);
975 GOTO(log, rc = PTR_ERR(th));
977 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
981 rec->rec_type = S_IFDIR;
983 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
984 (const struct dt_key *)dotdot, th);
988 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
989 XATTR_NAME_LINK, 0, th);
993 rc = dt_trans_start_local(env, dev, th);
997 dt_write_lock(env, obj, 0);
998 if (unlikely(lfsck_is_dead_obj(obj)))
999 GOTO(unlock, rc = 0);
1001 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1002 GOTO(unlock, rc = 1);
1004 /* The old ".." name entry maybe not exist. */
1005 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
1008 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1009 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
1013 rc = dt_xattr_set(env, obj, &linkea_buf,
1014 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1016 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1019 dt_write_unlock(env, obj);
1022 dt_trans_stop(env, dev, th);
1025 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
1026 "the object "DFID", new parent "DFID": rc = %d\n",
1027 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
1031 struct lfsck_namespace *ns = com->lc_file_ram;
1033 ns->ln_flags |= LF_INCONSISTENT;
1040 * Handle orphan @obj during Double Scan Directory.
1042 * Remove the @obj's current (invalid) linkEA entries, and insert
1043 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
1044 * ${FID}-${PFID}-D-${conflict_version}
1046 * The caller should take the ldlm lock before the calling.
1048 * \param[in] env pointer to the thread context
1049 * \param[in] com pointer to the lfsck component
1050 * \param[in] obj pointer to the orphan object to be handled
1051 * \param[in] pfid the new fid for the object's ".." name entry
1052 * \param[in,out] lh ldlm lock handler for the given @obj
1054 * \retval positive number for repaired cases
1055 * \retval 0 if nothing to be repaired
1056 * \retval negative error number on failure
1058 static int lfsck_namespace_dsd_orphan(const struct lu_env *env,
1059 struct lfsck_component *com,
1060 struct dt_object *obj,
1061 const struct lu_fid *pfid,
1062 struct lustre_handle *lh)
1064 struct lfsck_thread_info *info = lfsck_env_info(env);
1068 /* Remove the unrecognized linkEA. */
1069 rc = lfsck_namespace_links_remove(env, com, obj);
1070 lfsck_ibits_unlock(lh, LCK_EX);
1071 if (rc < 0 && rc != -ENODATA)
1074 /* The unique linkEA is invalid, even if the ".." name entry may be
1075 * valid, we still cannot know via which name entry this directory
1076 * will be referenced. Then handle it as pure orphan. */
1077 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
1078 "-"DFID, PFID(pfid));
1079 rc = lfsck_namespace_insert_orphan(env, com, obj,
1080 info->lti_tmpbuf, "D", NULL);
1086 * Double Scan Directory object for single linkEA entry case.
1088 * The given @child has unique linkEA entry. If the linkEA entry is valid,
1089 * then check whether the name is in the namespace or not, if not, add the
1090 * missing name entry back to namespace. If the linkEA entry is invalid,
1091 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
1094 * \param[in] env pointer to the thread context
1095 * \param[in] com pointer to the lfsck component
1096 * \param[in] child pointer to the directory to be double scanned
1097 * \param[in] pfid the FID corresponding to the ".." entry
1098 * \param[in] ldata pointer to the linkEA data for the given @child
1099 * \param[in,out] lh ldlm lock handler for the given @child
1100 * \param[out] type to tell the caller what the inconsistency is
1101 * \param[in] retry if found inconsistency, but the caller does not hold
1102 * ldlm lock on the @child, then set @retry as true
1104 * \retval positive number for repaired cases
1105 * \retval 0 if nothing to be repaired
1106 * \retval negative error number on failure
1109 lfsck_namespace_dsd_single(const struct lu_env *env,
1110 struct lfsck_component *com,
1111 struct dt_object *child,
1112 const struct lu_fid *pfid,
1113 struct linkea_data *ldata,
1114 struct lustre_handle *lh,
1115 enum lfsck_namespace_inconsistency_type *type,
1118 struct lfsck_thread_info *info = lfsck_env_info(env);
1119 struct lu_name *cname = &info->lti_name;
1120 const struct lu_fid *cfid = lfsck_dto2fid(child);
1121 struct lu_fid *tfid = &info->lti_fid3;
1122 struct lfsck_instance *lfsck = com->lc_lfsck;
1123 struct dt_object *parent = NULL;
1127 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
1128 /* The unique linkEA entry with bad parent will be handled as orphan. */
1129 if (!fid_is_sane(tfid)) {
1130 if (!lustre_handle_is_used(lh) && retry != NULL)
1133 rc = lfsck_namespace_dsd_orphan(env, com, child,
1139 parent = lfsck_object_find_bottom(env, lfsck, tfid);
1141 GOTO(out, rc = PTR_ERR(parent));
1143 /* We trust the unique linkEA entry in spite of whether it matches the
1144 * ".." name entry or not. Because even if the linkEA entry is wrong
1145 * and the ".." name entry is right, we still cannot know via which
1146 * name entry the child will be referenced, since all known entries
1147 * have been verified during the first-stage scanning. */
1148 if (!dt_object_exists(parent)) {
1149 if (!lustre_handle_is_used(lh) && retry != NULL) {
1155 lfsck_ibits_unlock(lh, LCK_EX);
1156 /* Create the lost parent as an orphan. */
1157 rc = lfsck_namespace_create_orphan(env, com, parent);
1159 /* Add the missing name entry to the parent. */
1160 rc = lfsck_namespace_insert_normal(env, com, parent,
1161 child, cname->ln_name);
1166 /* The unique linkEA entry with bad parent will be handled as orphan. */
1167 if (unlikely(!dt_try_as_dir(env, parent))) {
1168 if (!lustre_handle_is_used(lh) && retry != NULL)
1171 rc = lfsck_namespace_dsd_orphan(env, com, child,
1177 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1178 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
1179 if (rc == -ENOENT) {
1180 if (!lustre_handle_is_used(lh) && retry != NULL) {
1186 lfsck_ibits_unlock(lh, LCK_EX);
1187 /* Add the missing name entry back to the namespace. */
1188 rc = lfsck_namespace_insert_normal(env, com, parent, child,
1197 /* XXX: The name entry references another MDT-object that may be
1198 * created by the LFSCK for repairing dangling name entry.
1199 * There will be another patch for further processing. */
1200 if (!lu_fid_eq(tfid, cfid)) {
1201 if (!lustre_handle_is_used(lh) && retry != NULL)
1204 rc = lfsck_namespace_dsd_orphan(env, com, child,
1210 /* The ".." name entry is wrong, update it. */
1211 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
1212 if (!lustre_handle_is_used(lh) && retry != NULL) {
1218 *type = LNIT_UNMATCHED_PAIRS;
1219 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
1220 lfsck_dto2fid(parent), cname);
1226 if (parent != NULL && !IS_ERR(parent))
1227 lfsck_object_put(env, parent);
1233 * Double Scan Directory object for single linkEA entry case.
1235 * The given @child has multiple linkEA entries. There is at most one linkEA
1236 * entry will be valid, all the others will be removed. Firstly, the function
1237 * will try to find out the linkEA entry for which the name entry exists under
1238 * the given parent (@pfid). If there is no linkEA entry that matches the given
1239 * ".." name entry, then tries to find out the first linkEA entry that both the
1240 * parent and the name entry exist to rebuild a new ".." name entry.
1242 * \param[in] env pointer to the thread context
1243 * \param[in] com pointer to the lfsck component
1244 * \param[in] child pointer to the directory to be double scanned
1245 * \param[in] pfid the FID corresponding to the ".." entry
1246 * \param[in] ldata pointer to the linkEA data for the given @child
1247 * \param[in,out] lh ldlm lock handler for the given @child
1248 * \param[out] type to tell the caller what the inconsistency is
1249 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
1251 * \retval positive number for repaired cases
1252 * \retval 0 if nothing to be repaired
1253 * \retval negative error number on failure
1256 lfsck_namespace_dsd_multiple(const struct lu_env *env,
1257 struct lfsck_component *com,
1258 struct dt_object *child,
1259 const struct lu_fid *pfid,
1260 struct linkea_data *ldata,
1261 struct lustre_handle *lh,
1262 enum lfsck_namespace_inconsistency_type *type,
1265 struct lfsck_thread_info *info = lfsck_env_info(env);
1266 struct lu_name *cname = &info->lti_name;
1267 const struct lu_fid *cfid = lfsck_dto2fid(child);
1268 struct lu_fid *tfid = &info->lti_fid3;
1269 struct lu_fid *pfid2 = &info->lti_fid4;
1270 struct lfsck_instance *lfsck = com->lc_lfsck;
1271 struct dt_object *parent = NULL;
1272 struct linkea_data ldata_new = { 0 };
1278 while (ldata->ld_lee != NULL) {
1279 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
1281 /* Drop repeated linkEA entries. */
1282 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
1283 /* Drop invalid linkEA entry. */
1284 if (!fid_is_sane(tfid)) {
1285 linkea_del_buf(ldata, cname);
1289 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
1290 * then it is possible that: the directry object has ever
1291 * been lost, but its name entry was there. In the former
1292 * LFSCK run, during the first-stage scanning, the LFSCK
1293 * found the dangling name entry, but it did not recreate
1294 * the lost object, and when moved to the second-stage
1295 * scanning, some children objects of the lost directory
1296 * object were found, then the LFSCK recreated such lost
1297 * directory object as an orphan.
1299 * When the LFSCK runs again, if the dangling name is still
1300 * there, the LFSCK should move the orphan directory object
1301 * back to the normal namespace. */
1302 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
1303 linkea_next_entry(ldata);
1307 parent = lfsck_object_find_bottom(env, lfsck, tfid);
1309 RETURN(PTR_ERR(parent));
1311 if (!dt_object_exists(parent)) {
1312 lfsck_object_put(env, parent);
1313 if (ldata->ld_leh->leh_reccount > 1) {
1314 /* If it is NOT the last linkEA entry, then
1315 * there is still other chance to make the
1316 * child to be visible via other parent, then
1317 * remove this linkEA entry. */
1318 linkea_del_buf(ldata, cname);
1325 /* The linkEA entry with bad parent will be removed. */
1326 if (unlikely(!dt_try_as_dir(env, parent))) {
1327 lfsck_object_put(env, parent);
1328 linkea_del_buf(ldata, cname);
1332 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1333 (const struct dt_key *)cname->ln_name,
1335 *pfid2 = *lfsck_dto2fid(parent);
1336 lfsck_object_put(env, parent);
1337 if (rc == -ENOENT) {
1338 linkea_next_entry(ldata);
1345 if (lu_fid_eq(tfid, cfid)) {
1346 if (!lu_fid_eq(pfid, pfid2)) {
1347 *type = LNIT_UNMATCHED_PAIRS;
1348 rc = lfsck_namespace_repair_unmatched_pairs(env,
1349 com, child, pfid2, cname);
1354 /* It is the most common case that we find the
1355 * name entry corresponding to the linkEA entry
1356 * that matches the ".." name entry. */
1357 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
1361 rc = linkea_add_buf(&ldata_new, cname, pfid2);
1365 rc = lfsck_namespace_rebuild_linkea(env, com, child,
1368 /* XXX: there will be other patch. */
1373 /* XXX: The name entry references another MDT-object that
1374 * may be created by the LFSCK for repairing dangling
1375 * name entry. There will be another patch for further
1377 linkea_del_buf(ldata, cname);
1380 if (ldata->ld_leh->leh_reccount == 1) {
1381 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
1387 /* All linkEA entries are invalid and removed, then handle the @child
1389 if (ldata->ld_leh->leh_reccount == 0) {
1390 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh);
1395 linkea_first_entry(ldata);
1396 /* If the dangling name entry for the orphan directory object has
1397 * been remvoed, then just check whether the directory object is
1398 * still under the .lustre/lost+found/MDTxxxx/ or not. */
1404 /* There is no linkEA entry that matches the ".." name entry. Find
1405 * the first linkEA entry that both parent and name entry exist to
1406 * rebuild a new ".." name entry. */
1416 * Double scan the directory object for namespace LFSCK.
1418 * This function will verify the <parent, child> pairs in the namespace tree:
1419 * the parent references the child via some name entry that should be in the
1420 * child's linkEA entry, the child should back references the parent via its
1423 * The LFSCK will scan every linkEA entry in turn until find out the first
1424 * matched pairs. If found, then all other linkEA entries will be dropped.
1425 * If all the linkEA entries cannot match the ".." name entry, then there
1426 * are serveral possible cases:
1428 * 1) If there is only one linkEA entry, then trust it as long as the PFID
1429 * in the linkEA entry is valid.
1431 * 2) If there are multiple linkEA entries, then try to find the linkEA
1432 * that matches the ".." name entry. If found, then all other entries
1433 * are invalid; otherwise, it is quite possible that the ".." name entry
1434 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
1435 * entry according to the first valid linkEA entry (both the parent and
1436 * the name entry should exist).
1438 * 3) If the directory object has no (valid) linkEA entry, then the
1439 * directory object will be handled as pure orphan and inserted
1440 * in the .lustre/lost+found/MDTxxxx/ with the name:
1441 * ${self_FID}-${PFID}-D-${conflict_version}
1443 * \param[in] env pointer to the thread context
1444 * \param[in] com pointer to the lfsck component
1445 * \param[in] child pointer to the directory object to be handled
1446 * \param[in] flags to indicate the specical checking on the @child
1448 * \retval positive number for repaired cases
1449 * \retval 0 if nothing to be repaired
1450 * \retval negative error number on failure
1452 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
1453 struct lfsck_component *com,
1454 struct dt_object *child, __u8 flags)
1456 struct lfsck_thread_info *info = lfsck_env_info(env);
1457 const struct lu_fid *cfid = lfsck_dto2fid(child);
1458 struct lu_fid *pfid = &info->lti_fid2;
1459 struct lfsck_namespace *ns = com->lc_file_ram;
1460 struct lfsck_instance *lfsck = com->lc_lfsck;
1461 struct lustre_handle lh = { 0 };
1462 struct linkea_data ldata = { 0 };
1463 bool unknown = false;
1466 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
1470 LASSERT(!dt_object_remote(child));
1472 if (!(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
1473 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
1474 "the namespace LFSCK, then the LFSCK cannot guarantee"
1475 "all the name entries have been verified in first-stage"
1476 "scanning. So have to skip orphan related handling for"
1477 "the directory object "DFID" with remote name entry\n",
1478 lfsck_lfsck2name(lfsck), PFID(cfid));
1483 if (unlikely(!dt_try_as_dir(env, child)))
1484 GOTO(out, rc = -ENOTDIR);
1486 /* We only take ldlm lock on the @child when required. When the
1487 * logic comes here for the first time, it is always false. */
1491 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
1492 MDS_INODELOCK_UPDATE |
1493 MDS_INODELOCK_XATTR, LCK_EX);
1498 dt_read_lock(env, child, 0);
1499 if (unlikely(lfsck_is_dead_obj(child))) {
1500 dt_read_unlock(env, child);
1505 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
1506 (const struct dt_key *)dotdot, BYPASS_CAPA);
1508 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
1509 dt_read_unlock(env, child);
1514 if (!lustre_handle_is_used(&lh)) {
1515 dt_read_unlock(env, child);
1520 } else if (lfsck->li_lpf_obj != NULL &&
1521 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
1525 rc = lfsck_links_read(env, child, &ldata);
1526 dt_read_unlock(env, child);
1528 if (rc != -ENODATA && rc != -EINVAL)
1531 if (!lustre_handle_is_used(&lh))
1534 if (rc == -EINVAL && !fid_is_zero(pfid)) {
1535 /* Remove the corrupted linkEA. */
1536 rc = lfsck_namespace_links_remove(env, com, child);
1538 /* Here, because of the crashed linkEA, we
1539 * cannot know whether there is some parent
1540 * that references the child directory via
1541 * some name entry or not. So keep it there,
1542 * when the LFSCK run next time, if there is
1543 * some parent that references this object,
1544 * then the LFSCK can rebuild the linkEA;
1545 * otherwise, this object will be handled
1546 * as orphan as above. */
1549 /* 1. If we have neither ".." nor linkEA,
1550 * then it is an orphan.
1552 * 2. If we only have the ".." name entry,
1553 * but no parent references this child
1554 * directory, then handle it as orphan. */
1555 lfsck_ibits_unlock(&lh, LCK_EX);
1556 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
1557 "-"DFID, PFID(pfid));
1558 rc = lfsck_namespace_insert_orphan(env, com, child,
1559 info->lti_tmpbuf, "D", NULL);
1565 linkea_first_entry(&ldata);
1566 /* This is the most common case: the object has unique linkEA entry. */
1567 if (ldata.ld_leh->leh_reccount == 1) {
1568 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
1569 &lh, &type, &retry);
1571 LASSERT(!lustre_handle_is_used(&lh));
1580 if (!lustre_handle_is_used(&lh))
1583 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
1584 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh);
1589 /* When we come here, the cases usually like that:
1590 * 1) The directory object has a corrupted linkEA entry. During the
1591 * first-stage scanning, the LFSCK cannot know such corruption,
1592 * then it appends the right linkEA entry according to the found
1593 * name entry after the bad one.
1595 * 2) The directory object has a right linkEA entry. During the
1596 * first-stage scanning, the LFSCK finds some bad name entry,
1597 * but the LFSCK cannot aware that at that time, then it adds
1598 * the bad linkEA entry for further processing. */
1599 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
1605 lfsck_ibits_unlock(&lh, LCK_EX);
1608 case LNIT_BAD_LINKEA:
1609 ns->ln_linkea_repaired++;
1611 case LNIT_UNMATCHED_PAIRS:
1612 ns->ln_unmatched_pairs_repaired++;
1620 ns->ln_unknown_inconsistency++;
1626 * Double scan the MDT-object for namespace LFSCK.
1628 * If the MDT-object contains invalid or repeated linkEA entries, then drop
1629 * those entries from the linkEA; if the linkEA becomes empty or the object
1630 * has no linkEA, then it is an orphan and will be added into the directory
1631 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
1632 * the remote parent; if the name entry corresponding to some linkEA entry
1633 * is lost, then add the name entry back to the namespace.
1635 * \param[in] env pointer to the thread context
1636 * \param[in] com pointer to the lfsck component
1637 * \param[in] child pointer to the dt_object to be handled
1638 * \param[in] flags some hints to indicate how the @child should be handled
1640 * \retval positive number for repaired cases
1641 * \retval 0 if nothing to be repaired
1642 * \retval negative error number on failure
1644 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
1645 struct lfsck_component *com,
1646 struct dt_object *child, __u8 flags)
1648 struct lfsck_thread_info *info = lfsck_env_info(env);
1649 struct lu_attr *la = &info->lti_la;
1650 struct lu_name *cname = &info->lti_name;
1651 struct lu_fid *pfid = &info->lti_fid;
1652 struct lu_fid *cfid = &info->lti_fid2;
1653 struct lfsck_instance *lfsck = com->lc_lfsck;
1654 struct lfsck_namespace *ns = com->lc_file_ram;
1655 struct dt_object *parent = NULL;
1656 struct linkea_data ldata = { 0 };
1657 bool repaired = false;
1662 dt_read_lock(env, child, 0);
1663 if (unlikely(lfsck_is_dead_obj(child))) {
1664 dt_read_unlock(env, child);
1669 if (S_ISDIR(lfsck_object_type(child))) {
1670 dt_read_unlock(env, child);
1671 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
1676 rc = lfsck_links_read(env, child, &ldata);
1677 dt_read_unlock(env, child);
1681 linkea_first_entry(&ldata);
1682 while (ldata.ld_lee != NULL) {
1683 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
1685 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
1687 /* Found repeated linkEA entries */
1689 rc = lfsck_namespace_shrink_linkea(env, com, child,
1690 &ldata, cname, pfid, false);
1702 /* Invalid PFID in the linkEA entry. */
1703 if (!fid_is_sane(pfid)) {
1704 rc = lfsck_namespace_shrink_linkea(env, com, child,
1705 &ldata, cname, pfid, true);
1715 parent = lfsck_object_find_bottom(env, lfsck, pfid);
1717 GOTO(out, rc = PTR_ERR(parent));
1719 if (!dt_object_exists(parent)) {
1720 if (ldata.ld_leh->leh_reccount > 1) {
1721 /* If it is NOT the last linkEA entry, then
1722 * there is still other chance to make the
1723 * child to be visible via other parent, then
1724 * remove this linkEA entry. */
1725 rc = lfsck_namespace_shrink_linkea(env, com,
1726 child, &ldata, cname, pfid, true);
1728 /* Create the lost parent as an orphan. */
1729 rc = lfsck_namespace_create_orphan(env, com,
1732 lfsck_object_put(env, parent);
1740 /* Add the missing name entry to the parent. */
1741 rc = lfsck_namespace_insert_normal(env, com,
1742 parent, child, cname->ln_name);
1743 linkea_next_entry(&ldata);
1746 lfsck_object_put(env, parent);
1756 /* The linkEA entry with bad parent will be removed. */
1757 if (unlikely(!dt_try_as_dir(env, parent))) {
1758 lfsck_object_put(env, parent);
1759 rc = lfsck_namespace_shrink_linkea(env, com, child,
1760 &ldata, cname, pfid, true);
1770 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1771 (const struct dt_key *)cname->ln_name,
1773 if (rc != 0 && rc != -ENOENT) {
1774 lfsck_object_put(env, parent);
1780 lfsck_object_put(env, parent);
1781 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1782 /* It is the most common case that we
1783 * find the name entry corresponding
1784 * to the linkEA entry. */
1785 linkea_next_entry(&ldata);
1787 /* XXX: The name entry references another
1788 * MDT-object that may be created by
1789 * the LFSCK for repairing dangling
1790 * name entry. There will be another
1791 * patch for further processing. */
1792 rc = lfsck_namespace_shrink_linkea(env, com,
1793 child, &ldata, cname, pfid, true);
1804 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
1808 /* If there is no name entry in the parent dir and the object
1809 * link count is less than the linkea entries count, then the
1810 * linkea entry should be removed. */
1811 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
1812 rc = lfsck_namespace_shrink_linkea_cond(env, com,
1813 parent, child, &ldata, cname, pfid);
1814 lfsck_object_put(env, parent);
1824 /* Add the missing name entry back to the namespace. */
1825 rc = lfsck_namespace_insert_normal(env, com, parent, child,
1827 lfsck_object_put(env, parent);
1834 linkea_next_entry(&ldata);
1840 if (rc < 0 && rc != -ENODATA)
1844 LASSERT(ldata.ld_leh != NULL);
1846 count = ldata.ld_leh->leh_reccount;
1850 /* If the child becomes orphan, then insert it into
1851 * the global .lustre/lost+found/MDTxxxx directory. */
1852 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
1861 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
1865 if (la->la_nlink != count) {
1866 /* XXX: there will be other patch(es) for MDT-object
1867 * hard links verification. */
1871 if (la->la_nlink > 1)
1872 ns->ln_mul_linked_repaired++;
1881 static void lfsck_namespace_dump_statistics(struct seq_file *m,
1882 struct lfsck_namespace *ns,
1883 __u64 checked_phase1,
1884 __u64 checked_phase2,
1888 seq_printf(m, "checked_phase1: "LPU64"\n"
1889 "checked_phase2: "LPU64"\n"
1890 "updated_phase1: "LPU64"\n"
1891 "updated_phase2: "LPU64"\n"
1892 "failed_phase1: "LPU64"\n"
1893 "failed_phase2: "LPU64"\n"
1894 "directories: "LPU64"\n"
1895 "dirent_repaired: "LPU64"\n"
1896 "linkea_repaired: "LPU64"\n"
1897 "nlinks_repaired: "LPU64"\n"
1898 "lost_found: "LPU64"\n"
1899 "multiple_linked_checked: "LPU64"\n"
1900 "multiple_linked_repaired: "LPU64"\n"
1901 "unknown_inconsistency: "LPU64"\n"
1902 "unmatched_pairs_repaired: "LPU64"\n"
1903 "success_count: %u\n"
1904 "run_time_phase1: %u seconds\n"
1905 "run_time_phase2: %u seconds\n",
1908 ns->ln_items_repaired,
1909 ns->ln_objs_repaired_phase2,
1910 ns->ln_items_failed,
1911 ns->ln_objs_failed_phase2,
1912 ns->ln_dirs_checked,
1913 ns->ln_dirent_repaired,
1914 ns->ln_linkea_repaired,
1915 ns->ln_objs_nlink_repaired,
1916 ns->ln_objs_lost_found,
1917 ns->ln_mul_linked_checked,
1918 ns->ln_mul_linked_repaired,
1919 ns->ln_unknown_inconsistency,
1920 ns->ln_unmatched_pairs_repaired,
1921 ns->ln_success_count,
1926 /* namespace APIs */
1928 static int lfsck_namespace_reset(const struct lu_env *env,
1929 struct lfsck_component *com, bool init)
1931 struct lfsck_instance *lfsck = com->lc_lfsck;
1932 struct lfsck_namespace *ns = com->lc_file_ram;
1933 struct dt_object *root;
1934 struct dt_object *dto;
1938 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1940 GOTO(log, rc = PTR_ERR(root));
1942 if (unlikely(!dt_try_as_dir(env, root)))
1943 GOTO(put, rc = -ENOTDIR);
1945 down_write(&com->lc_sem);
1947 memset(ns, 0, sizeof(*ns));
1949 __u32 count = ns->ln_success_count;
1950 __u64 last_time = ns->ln_time_last_complete;
1952 memset(ns, 0, sizeof(*ns));
1953 ns->ln_success_count = count;
1954 ns->ln_time_last_complete = last_time;
1956 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
1957 ns->ln_status = LS_INIT;
1959 rc = local_object_unlink(env, lfsck->li_bottom, root,
1960 lfsck_namespace_name);
1964 lfsck_object_put(env, com->lc_obj);
1966 dto = local_index_find_or_create(env, lfsck->li_los, root,
1967 lfsck_namespace_name,
1968 S_IFREG | S_IRUGO | S_IWUSR,
1969 &dt_lfsck_features);
1971 GOTO(out, rc = PTR_ERR(dto));
1974 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
1978 rc = lfsck_namespace_store(env, com, true);
1983 up_write(&com->lc_sem);
1986 lu_object_put(env, &root->do_lu);
1988 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
1989 lfsck_lfsck2name(lfsck), rc);
1994 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
1997 struct lfsck_namespace *ns = com->lc_file_ram;
1999 down_write(&com->lc_sem);
2001 com->lc_new_checked++;
2002 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
2003 up_write(&com->lc_sem);
2006 static int lfsck_namespace_checkpoint(const struct lu_env *env,
2007 struct lfsck_component *com, bool init)
2009 struct lfsck_instance *lfsck = com->lc_lfsck;
2010 struct lfsck_namespace *ns = com->lc_file_ram;
2014 rc = lfsck_checkpoint_generic(env, com);
2019 down_write(&com->lc_sem);
2021 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
2023 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
2024 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
2025 HALF_SEC - lfsck->li_time_last_checkpoint);
2026 ns->ln_time_last_checkpoint = cfs_time_current_sec();
2027 ns->ln_items_checked += com->lc_new_checked;
2028 com->lc_new_checked = 0;
2031 rc = lfsck_namespace_store(env, com, false);
2032 up_write(&com->lc_sem);
2035 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
2036 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
2037 lfsck->li_pos_current.lp_oit_cookie,
2038 PFID(&lfsck->li_pos_current.lp_dir_parent),
2039 lfsck->li_pos_current.lp_dir_cookie, rc);
2041 return rc > 0 ? 0 : rc;
2044 static int lfsck_namespace_prep(const struct lu_env *env,
2045 struct lfsck_component *com,
2046 struct lfsck_start_param *lsp)
2048 struct lfsck_instance *lfsck = com->lc_lfsck;
2049 struct lfsck_namespace *ns = com->lc_file_ram;
2050 struct lfsck_position *pos = &com->lc_pos_start;
2053 if (ns->ln_status == LS_COMPLETED) {
2054 rc = lfsck_namespace_reset(env, com, false);
2056 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
2059 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
2060 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
2066 down_write(&com->lc_sem);
2067 ns->ln_time_latest_start = cfs_time_current_sec();
2068 spin_lock(&lfsck->li_lock);
2070 if (ns->ln_flags & LF_SCANNED_ONCE) {
2071 if (!lfsck->li_drop_dryrun ||
2072 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
2073 ns->ln_status = LS_SCANNING_PHASE2;
2074 list_move_tail(&com->lc_link,
2075 &lfsck->li_list_double_scan);
2076 if (!list_empty(&com->lc_link_dir))
2077 list_del_init(&com->lc_link_dir);
2078 lfsck_pos_set_zero(pos);
2080 ns->ln_status = LS_SCANNING_PHASE1;
2081 ns->ln_run_time_phase1 = 0;
2082 ns->ln_run_time_phase2 = 0;
2083 ns->ln_items_checked = 0;
2084 ns->ln_items_repaired = 0;
2085 ns->ln_items_failed = 0;
2086 ns->ln_dirs_checked = 0;
2087 ns->ln_objs_checked_phase2 = 0;
2088 ns->ln_objs_repaired_phase2 = 0;
2089 ns->ln_objs_failed_phase2 = 0;
2090 ns->ln_objs_nlink_repaired = 0;
2091 ns->ln_objs_lost_found = 0;
2092 ns->ln_dirent_repaired = 0;
2093 ns->ln_linkea_repaired = 0;
2094 ns->ln_mul_linked_checked = 0;
2095 ns->ln_mul_linked_repaired = 0;
2096 ns->ln_unknown_inconsistency = 0;
2097 ns->ln_unmatched_pairs_repaired = 0;
2098 fid_zero(&ns->ln_fid_latest_scanned_phase2);
2099 if (list_empty(&com->lc_link_dir))
2100 list_add_tail(&com->lc_link_dir,
2101 &lfsck->li_list_dir);
2102 *pos = ns->ln_pos_first_inconsistent;
2105 ns->ln_status = LS_SCANNING_PHASE1;
2106 if (list_empty(&com->lc_link_dir))
2107 list_add_tail(&com->lc_link_dir,
2108 &lfsck->li_list_dir);
2109 if (!lfsck->li_drop_dryrun ||
2110 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
2111 *pos = ns->ln_pos_last_checkpoint;
2112 pos->lp_oit_cookie++;
2114 *pos = ns->ln_pos_first_inconsistent;
2118 spin_unlock(&lfsck->li_lock);
2119 up_write(&com->lc_sem);
2121 rc = lfsck_start_assistant(env, com, lsp);
2123 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
2124 DFID", "LPX64"]: rc = %d\n",
2125 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
2126 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
2131 static int lfsck_namespace_exec_oit(const struct lu_env *env,
2132 struct lfsck_component *com,
2133 struct dt_object *obj)
2135 struct lfsck_thread_info *info = lfsck_env_info(env);
2136 struct lfsck_namespace *ns = com->lc_file_ram;
2137 struct lfsck_instance *lfsck = com->lc_lfsck;
2138 const struct lu_fid *fid = lfsck_dto2fid(obj);
2139 struct lu_attr *la = &info->lti_la;
2140 struct lu_fid *pfid = &info->lti_fid2;
2141 struct lu_name *cname = &info->lti_name;
2142 struct lu_seq_range *range = &info->lti_range;
2143 struct dt_device *dev = lfsck->li_bottom;
2144 struct seq_server_site *ss =
2145 lu_site2seq(dev->dd_lu_dev.ld_site);
2146 struct linkea_data ldata = { 0 };
2147 __u32 idx = lfsck_dev_idx(dev);
2151 rc = lfsck_links_read(env, obj, &ldata);
2155 /* -EINVAL means crashed linkEA, should be verified. */
2156 if (rc == -EINVAL) {
2157 rc = lfsck_namespace_trace_update(env, com, fid,
2158 LNTF_CHECK_LINKEA, true);
2160 struct lustre_handle lh = { 0 };
2162 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2163 MDS_INODELOCK_UPDATE |
2164 MDS_INODELOCK_XATTR, LCK_EX);
2166 rc = lfsck_namespace_links_remove(env, com,
2168 lfsck_ibits_unlock(&lh, LCK_EX);
2172 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
2175 /* zero-linkEA object may be orphan, but it also maybe because
2176 * of upgrading. Currently, we cannot record it for double scan.
2177 * Because it may cause the LFSCK tracing file to be too large. */
2178 if (rc == -ENODATA) {
2179 if (S_ISDIR(lfsck_object_type(obj)))
2182 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2186 if (la->la_nlink > 1)
2187 rc = lfsck_namespace_trace_update(env, com, fid,
2188 LNTF_CHECK_LINKEA, true);
2196 /* Record multiple-linked object. */
2197 if (ldata.ld_leh->leh_reccount > 1) {
2198 rc = lfsck_namespace_trace_update(env, com, fid,
2199 LNTF_CHECK_LINKEA, true);
2204 linkea_first_entry(&ldata);
2205 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
2206 if (!fid_is_sane(pfid)) {
2207 rc = lfsck_namespace_trace_update(env, com, fid,
2208 LNTF_CHECK_PARENT, true);
2210 fld_range_set_mdt(range);
2211 rc = fld_local_lookup(env, ss->ss_server_fld,
2212 fid_seq(pfid), range);
2213 if ((rc == -ENOENT) ||
2214 (rc == 0 && range->lsr_index != idx)) {
2215 rc = lfsck_namespace_trace_update(env, com, fid,
2216 LNTF_CHECK_LINKEA, true);
2218 if (S_ISDIR(lfsck_object_type(obj)))
2221 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2225 if (la->la_nlink > 1)
2226 rc = lfsck_namespace_trace_update(env, com,
2227 fid, LNTF_CHECK_LINKEA, true);
2234 down_write(&com->lc_sem);
2235 com->lc_new_checked++;
2236 if (S_ISDIR(lfsck_object_type(obj)))
2237 ns->ln_dirs_checked++;
2239 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
2240 up_write(&com->lc_sem);
2245 static int lfsck_namespace_exec_dir(const struct lu_env *env,
2246 struct lfsck_component *com,
2247 struct lu_dirent *ent, __u16 type)
2249 struct lfsck_assistant_data *lad = com->lc_data;
2250 struct lfsck_namespace_req *lnr;
2251 bool wakeup = false;
2253 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
2255 struct lfsck_namespace *ns = com->lc_file_ram;
2257 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
2258 return PTR_ERR(lnr);
2261 spin_lock(&lad->lad_lock);
2262 if (lad->lad_assistant_status < 0) {
2263 spin_unlock(&lad->lad_lock);
2264 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
2265 return lad->lad_assistant_status;
2268 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
2269 if (lad->lad_prefetched == 0)
2272 lad->lad_prefetched++;
2273 spin_unlock(&lad->lad_lock);
2275 wake_up_all(&lad->lad_thread.t_ctl_waitq);
2277 down_write(&com->lc_sem);
2278 com->lc_new_checked++;
2279 up_write(&com->lc_sem);
2284 static int lfsck_namespace_post(const struct lu_env *env,
2285 struct lfsck_component *com,
2286 int result, bool init)
2288 struct lfsck_instance *lfsck = com->lc_lfsck;
2289 struct lfsck_namespace *ns = com->lc_file_ram;
2293 lfsck_post_generic(env, com, &result);
2295 down_write(&com->lc_sem);
2296 spin_lock(&lfsck->li_lock);
2298 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
2300 ns->ln_status = LS_SCANNING_PHASE2;
2301 ns->ln_flags |= LF_SCANNED_ONCE;
2302 ns->ln_flags &= ~LF_UPGRADE;
2303 list_del_init(&com->lc_link_dir);
2304 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
2305 } else if (result == 0) {
2306 ns->ln_status = lfsck->li_status;
2307 if (ns->ln_status == 0)
2308 ns->ln_status = LS_STOPPED;
2309 if (ns->ln_status != LS_PAUSED) {
2310 list_del_init(&com->lc_link_dir);
2311 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
2314 ns->ln_status = LS_FAILED;
2315 list_del_init(&com->lc_link_dir);
2316 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
2318 spin_unlock(&lfsck->li_lock);
2321 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
2322 HALF_SEC - lfsck->li_time_last_checkpoint);
2323 ns->ln_time_last_checkpoint = cfs_time_current_sec();
2324 ns->ln_items_checked += com->lc_new_checked;
2325 com->lc_new_checked = 0;
2328 rc = lfsck_namespace_store(env, com, false);
2329 up_write(&com->lc_sem);
2331 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
2332 lfsck_lfsck2name(lfsck), rc);
2338 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
2341 struct lfsck_instance *lfsck = com->lc_lfsck;
2342 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2343 struct lfsck_namespace *ns = com->lc_file_ram;
2346 down_read(&com->lc_sem);
2347 seq_printf(m, "name: lfsck_namespace\n"
2353 lfsck_status2names(ns->ln_status));
2355 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
2359 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
2363 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
2364 "time_since_last_completed");
2368 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
2369 "time_since_latest_start");
2373 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
2374 "time_since_last_checkpoint");
2378 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
2379 "latest_start_position");
2383 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
2384 "last_checkpoint_position");
2388 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
2389 "first_failure_position");
2393 if (ns->ln_status == LS_SCANNING_PHASE1) {
2394 struct lfsck_position pos;
2395 const struct dt_it_ops *iops;
2396 cfs_duration_t duration = cfs_time_current() -
2397 lfsck->li_time_last_checkpoint;
2398 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
2399 __u64 speed = checked;
2400 __u64 new_checked = com->lc_new_checked * HZ;
2401 __u32 rtime = ns->ln_run_time_phase1 +
2402 cfs_duration_sec(duration + HALF_SEC);
2405 do_div(new_checked, duration);
2407 do_div(speed, rtime);
2408 lfsck_namespace_dump_statistics(m, ns, checked,
2409 ns->ln_objs_checked_phase2,
2410 rtime, ns->ln_run_time_phase2);
2412 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
2413 "average_speed_phase2: N/A\n"
2414 "real_time_speed_phase1: "LPU64" items/sec\n"
2415 "real_time_speed_phase2: N/A\n",
2419 LASSERT(lfsck->li_di_oit != NULL);
2421 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
2423 /* The low layer otable-based iteration position may NOT
2424 * exactly match the namespace-based directory traversal
2425 * cookie. Generally, it is not a serious issue. But the
2426 * caller should NOT make assumption on that. */
2427 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
2428 if (!lfsck->li_current_oit_processed)
2429 pos.lp_oit_cookie--;
2431 spin_lock(&lfsck->li_lock);
2432 if (lfsck->li_di_dir != NULL) {
2433 pos.lp_dir_cookie = lfsck->li_cookie_dir;
2434 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
2435 fid_zero(&pos.lp_dir_parent);
2436 pos.lp_dir_cookie = 0;
2439 *lfsck_dto2fid(lfsck->li_obj_dir);
2442 fid_zero(&pos.lp_dir_parent);
2443 pos.lp_dir_cookie = 0;
2445 spin_unlock(&lfsck->li_lock);
2446 lfsck_pos_dump(m, &pos, "current_position");
2447 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
2448 cfs_duration_t duration = cfs_time_current() -
2449 lfsck->li_time_last_checkpoint;
2450 __u64 checked = ns->ln_objs_checked_phase2 +
2451 com->lc_new_checked;
2452 __u64 speed1 = ns->ln_items_checked;
2453 __u64 speed2 = checked;
2454 __u64 new_checked = com->lc_new_checked * HZ;
2455 __u32 rtime = ns->ln_run_time_phase2 +
2456 cfs_duration_sec(duration + HALF_SEC);
2459 do_div(new_checked, duration);
2460 if (ns->ln_run_time_phase1 != 0)
2461 do_div(speed1, ns->ln_run_time_phase1);
2463 do_div(speed2, rtime);
2464 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
2466 ns->ln_run_time_phase1, rtime);
2468 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
2469 "average_speed_phase2: "LPU64" objs/sec\n"
2470 "real_time_speed_phase1: N/A\n"
2471 "real_time_speed_phase2: "LPU64" objs/sec\n"
2472 "current_position: "DFID"\n",
2476 PFID(&ns->ln_fid_latest_scanned_phase2));
2478 __u64 speed1 = ns->ln_items_checked;
2479 __u64 speed2 = ns->ln_objs_checked_phase2;
2481 if (ns->ln_run_time_phase1 != 0)
2482 do_div(speed1, ns->ln_run_time_phase1);
2483 if (ns->ln_run_time_phase2 != 0)
2484 do_div(speed2, ns->ln_run_time_phase2);
2485 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
2486 ns->ln_objs_checked_phase2,
2487 ns->ln_run_time_phase1,
2488 ns->ln_run_time_phase2);
2490 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
2491 "average_speed_phase2: "LPU64" objs/sec\n"
2492 "real_time_speed_phase1: N/A\n"
2493 "real_time_speed_phase2: N/A\n"
2494 "current_position: N/A\n",
2499 up_read(&com->lc_sem);
2503 static int lfsck_namespace_double_scan(const struct lu_env *env,
2504 struct lfsck_component *com)
2506 struct lfsck_namespace *ns = com->lc_file_ram;
2508 return lfsck_double_scan_generic(env, com, ns->ln_status);
2511 static void lfsck_namespace_data_release(const struct lu_env *env,
2512 struct lfsck_component *com)
2514 struct lfsck_assistant_data *lad = com->lc_data;
2515 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
2516 struct lfsck_tgt_desc *ltd;
2517 struct lfsck_tgt_desc *next;
2519 LASSERT(lad != NULL);
2520 LASSERT(thread_is_init(&lad->lad_thread) ||
2521 thread_is_stopped(&lad->lad_thread));
2522 LASSERT(list_empty(&lad->lad_req_list));
2524 com->lc_data = NULL;
2526 spin_lock(<ds->ltd_lock);
2527 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
2528 ltd_namespace_phase_list) {
2529 list_del_init(<d->ltd_namespace_phase_list);
2531 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
2532 ltd_namespace_phase_list) {
2533 list_del_init(<d->ltd_namespace_phase_list);
2535 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
2536 ltd_namespace_list) {
2537 list_del_init(<d->ltd_namespace_list);
2539 spin_unlock(<ds->ltd_lock);
2541 CFS_FREE_BITMAP(lad->lad_bitmap);
2546 static int lfsck_namespace_in_notify(const struct lu_env *env,
2547 struct lfsck_component *com,
2548 struct lfsck_request *lr)
2550 struct lfsck_instance *lfsck = com->lc_lfsck;
2551 struct lfsck_namespace *ns = com->lc_file_ram;
2552 struct lfsck_assistant_data *lad = com->lc_data;
2553 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
2554 struct lfsck_tgt_desc *ltd;
2558 if (lr->lr_event != LE_PHASE1_DONE &&
2559 lr->lr_event != LE_PHASE2_DONE &&
2560 lr->lr_event != LE_PEER_EXIT)
2563 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
2564 "status %d\n", lfsck_lfsck2name(lfsck), lr->lr_event,
2565 lr->lr_index, lr->lr_status);
2567 spin_lock(<ds->ltd_lock);
2568 ltd = LTD_TGT(ltds, lr->lr_index);
2570 spin_unlock(<ds->ltd_lock);
2575 list_del_init(<d->ltd_namespace_phase_list);
2576 switch (lr->lr_event) {
2577 case LE_PHASE1_DONE:
2578 if (lr->lr_status <= 0) {
2579 ltd->ltd_namespace_done = 1;
2580 list_del_init(<d->ltd_namespace_list);
2581 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
2582 "phase1 for namespace LFSCK: rc = %d.\n",
2583 lfsck_lfsck2name(lfsck),
2584 ltd->ltd_index, lr->lr_status);
2585 ns->ln_flags |= LF_INCOMPLETE;
2590 if (list_empty(<d->ltd_namespace_list))
2591 list_add_tail(<d->ltd_namespace_list,
2592 &lad->lad_mdt_list);
2593 list_add_tail(<d->ltd_namespace_phase_list,
2594 &lad->lad_mdt_phase2_list);
2596 case LE_PHASE2_DONE:
2597 ltd->ltd_namespace_done = 1;
2598 list_del_init(<d->ltd_namespace_list);
2602 ltd->ltd_namespace_done = 1;
2603 list_del_init(<d->ltd_namespace_list);
2604 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
2606 "%s: the peer MDT %x exit namespace LFSCK\n",
2607 lfsck_lfsck2name(lfsck), ltd->ltd_index);
2608 ns->ln_flags |= LF_INCOMPLETE;
2614 spin_unlock(<ds->ltd_lock);
2616 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
2617 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
2619 memset(stop, 0, sizeof(*stop));
2620 stop->ls_status = lr->lr_status;
2621 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
2622 lfsck_stop(env, lfsck->li_bottom, stop);
2623 } else if (lfsck_phase2_next_ready(lad)) {
2624 wake_up_all(&lad->lad_thread.t_ctl_waitq);
2630 static int lfsck_namespace_query(const struct lu_env *env,
2631 struct lfsck_component *com)
2633 struct lfsck_namespace *ns = com->lc_file_ram;
2635 return ns->ln_status;
2638 static struct lfsck_operations lfsck_namespace_ops = {
2639 .lfsck_reset = lfsck_namespace_reset,
2640 .lfsck_fail = lfsck_namespace_fail,
2641 .lfsck_checkpoint = lfsck_namespace_checkpoint,
2642 .lfsck_prep = lfsck_namespace_prep,
2643 .lfsck_exec_oit = lfsck_namespace_exec_oit,
2644 .lfsck_exec_dir = lfsck_namespace_exec_dir,
2645 .lfsck_post = lfsck_namespace_post,
2646 .lfsck_dump = lfsck_namespace_dump,
2647 .lfsck_double_scan = lfsck_namespace_double_scan,
2648 .lfsck_data_release = lfsck_namespace_data_release,
2649 .lfsck_quit = lfsck_quit_generic,
2650 .lfsck_in_notify = lfsck_namespace_in_notify,
2651 .lfsck_query = lfsck_namespace_query,
2654 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
2655 struct lfsck_component *com,
2656 struct lfsck_assistant_req *lar)
2658 struct lfsck_thread_info *info = lfsck_env_info(env);
2659 struct lu_attr *la = &info->lti_la;
2660 struct lfsck_instance *lfsck = com->lc_lfsck;
2661 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2662 struct lfsck_namespace *ns = com->lc_file_ram;
2663 struct linkea_data ldata = { 0 };
2664 const struct lu_name *cname;
2665 struct thandle *handle = NULL;
2666 struct lfsck_namespace_req *lnr =
2667 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
2668 struct dt_object *dir = lnr->lnr_obj;
2669 struct dt_object *obj = NULL;
2670 const struct lu_fid *pfid = lfsck_dto2fid(dir);
2671 struct dt_device *dev;
2672 struct lustre_handle lh = { 0 };
2673 bool repaired = false;
2674 bool dtlocked = false;
2683 if (lnr->lnr_attr & LUDA_UPGRADE) {
2684 ns->ln_flags |= LF_UPGRADE;
2685 ns->ln_dirent_repaired++;
2687 } else if (lnr->lnr_attr & LUDA_REPAIR) {
2688 ns->ln_flags |= LF_INCONSISTENT;
2689 ns->ln_dirent_repaired++;
2693 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
2694 if (strcmp(lnr->lnr_name, dotdot) != 0)
2697 rc = lfsck_namespace_trace_update(env, com, pfid,
2698 LNTF_CHECK_PARENT, true);
2703 if (lnr->lnr_name[0] == '.' &&
2704 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
2707 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
2709 GOTO(out, rc = idx);
2711 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
2712 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
2715 dev = lfsck->li_next;
2717 struct lfsck_tgt_desc *ltd;
2719 /* Usually, some local filesystem consistency verification
2720 * tools can guarantee the local namespace tree consistenct.
2721 * So the LFSCK will only verify the remote directory. */
2722 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
2723 rc = lfsck_namespace_trace_update(env, com, pfid,
2724 LNTF_CHECK_PARENT, true);
2729 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
2730 if (unlikely(ltd == NULL)) {
2731 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
2732 "did not join the namespace LFSCK\n",
2733 lfsck_lfsck2name(lfsck), idx);
2734 ns->ln_flags |= LF_INCOMPLETE;
2736 GOTO(out, rc = -ENODEV);
2742 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
2744 GOTO(out, rc = PTR_ERR(obj));
2746 if (dt_object_exists(obj) == 0) {
2747 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
2751 /* XXX: dangling name entry, will handle it in other patch. */
2755 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
2756 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
2759 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2760 MDS_INODELOCK_UPDATE |
2761 MDS_INODELOCK_XATTR, LCK_EX);
2765 handle = dt_trans_create(env, dev);
2767 GOTO(out, rc = PTR_ERR(handle));
2769 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
2773 rc = dt_trans_start(env, dev, handle);
2777 dt_write_lock(env, obj, 0);
2781 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
2785 rc = lfsck_links_read(env, obj, &ldata);
2787 count = ldata.ld_leh->leh_reccount;
2788 rc = linkea_links_find(&ldata, cname, pfid);
2790 (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
2793 ns->ln_flags |= LF_INCONSISTENT;
2794 /* For sub-dir object, we cannot make sure whether the sub-dir
2795 * back references the parent via ".." name entry correctly or
2796 * not in the LFSCK first-stage scanning. It may be that the
2797 * (remote) sub-dir ".." name entry has no parent FID after
2798 * file-level backup/restore and its linkEA may be wrong.
2799 * So under such case, we should replace the linkEA according
2800 * to current name entry. But this needs to be done during the
2801 * LFSCK second-stage scanning. The LFSCK will record the name
2802 * entry for further possible using. */
2806 } else if (unlikely(rc == -EINVAL)) {
2808 ns->ln_flags |= LF_INCONSISTENT;
2809 /* The magic crashed, we are not sure whether there are more
2810 * corrupt data in the linkea, so remove all linkea entries. */
2814 } else if (rc == -ENODATA) {
2816 ns->ln_flags |= LF_UPGRADE;
2821 if (bk->lb_param & LPF_DRYRUN) {
2822 ns->ln_linkea_repaired++;
2828 if (!lustre_handle_is_used(&lh))
2834 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
2841 rc = linkea_data_new(&ldata,
2842 &lfsck_env_info(env)->lti_linkea_buf);
2847 rc = linkea_add_buf(&ldata, cname, pfid);
2851 rc = lfsck_links_write(env, obj, &ldata, handle);
2855 count = ldata.ld_leh->leh_reccount;
2856 if (!S_ISDIR(lfsck_object_type(obj)) ||
2857 !dt_object_remote(obj)) {
2858 ns->ln_linkea_repaired++;
2862 } else if (rc == -ENOENT) {
2874 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2878 if ((count == 1 && la->la_nlink == 1) ||
2879 S_ISDIR(lfsck_object_type(obj)))
2880 /* Usually, it is for single linked object or dir, do nothing.*/
2883 /* Following modification will be in another transaction. */
2884 if (handle != NULL) {
2885 LASSERT(dt_write_locked(env, obj));
2887 dt_write_unlock(env, obj);
2890 dt_trans_stop(env, dev, handle);
2893 lfsck_ibits_unlock(&lh, LCK_EX);
2896 ns->ln_mul_linked_checked++;
2897 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
2898 LNTF_CHECK_LINKEA, true);
2904 dt_write_unlock(env, obj);
2906 if (handle != NULL && !IS_ERR(handle))
2907 dt_trans_stop(env, dev, handle);
2910 lfsck_ibits_unlock(&lh, LCK_EX);
2911 down_write(&com->lc_sem);
2913 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
2914 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
2915 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
2916 PFID(lfsck_dto2fid(lnr->lnr_obj)),
2917 lnr->lnr_namelen, lnr->lnr_name, rc);
2919 lfsck_namespace_record_failure(env, lfsck, ns);
2920 if (!(bk->lb_param & LPF_FAILOUT))
2924 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
2925 "repaired the entry: "DFID", parent "DFID
2926 ", name %.*s\n", lfsck_lfsck2name(lfsck),
2927 PFID(&lnr->lnr_fid),
2928 PFID(lfsck_dto2fid(lnr->lnr_obj)),
2929 lnr->lnr_namelen, lnr->lnr_name);
2932 ns->ln_items_repaired++;
2933 if (bk->lb_param & LPF_DRYRUN &&
2934 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
2935 lfsck_pos_fill(env, lfsck,
2936 &ns->ln_pos_first_inconsistent,
2941 up_write(&com->lc_sem);
2943 if (obj != NULL && !IS_ERR(obj))
2944 lfsck_object_put(env, obj);
2948 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
2949 struct lfsck_component *com)
2951 struct lfsck_instance *lfsck = com->lc_lfsck;
2952 struct ptlrpc_thread *thread = &lfsck->li_thread;
2953 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2954 struct lfsck_namespace *ns = com->lc_file_ram;
2955 struct dt_object *obj = com->lc_obj;
2956 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
2957 struct dt_object *target;
2965 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
2966 lfsck_lfsck2name(lfsck));
2968 com->lc_new_checked = 0;
2969 com->lc_new_scanned = 0;
2970 com->lc_time_last_checkpoint = cfs_time_current();
2971 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
2972 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2974 di = iops->init(env, obj, 0, BYPASS_CAPA);
2976 RETURN(PTR_ERR(di));
2978 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
2979 rc = iops->get(env, di, (const struct dt_key *)&fid);
2983 /* Skip the start one, which either has been processed or non-exist. */
2984 rc = iops->next(env, di);
2989 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
2991 struct l_wait_info lwi;
2993 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2995 l_wait_event(thread->t_ctl_waitq,
2996 !thread_is_running(thread),
2999 if (unlikely(!thread_is_running(thread)))
3003 key = iops->key(env, di);
3004 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
3005 if (!fid_is_sane(&fid)) {
3010 target = lfsck_object_find(env, lfsck, &fid);
3011 if (IS_ERR(target)) {
3012 rc = PTR_ERR(target);
3016 if (dt_object_exists(target)) {
3017 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
3019 rc = lfsck_namespace_double_scan_one(env, com,
3026 lfsck_object_put(env, target);
3029 down_write(&com->lc_sem);
3030 com->lc_new_checked++;
3031 com->lc_new_scanned++;
3032 ns->ln_fid_latest_scanned_phase2 = fid;
3034 ns->ln_objs_repaired_phase2++;
3036 ns->ln_objs_failed_phase2++;
3037 up_write(&com->lc_sem);
3039 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
3042 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
3043 cfs_time_current())) &&
3044 com->lc_new_checked != 0) {
3045 down_write(&com->lc_sem);
3046 ns->ln_run_time_phase2 +=
3047 cfs_duration_sec(cfs_time_current() +
3048 HALF_SEC - com->lc_time_last_checkpoint);
3049 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3050 ns->ln_objs_checked_phase2 += com->lc_new_checked;
3051 com->lc_new_checked = 0;
3052 rc = lfsck_namespace_store(env, com, false);
3053 up_write(&com->lc_sem);
3057 com->lc_time_last_checkpoint = cfs_time_current();
3058 com->lc_time_next_checkpoint =
3059 com->lc_time_last_checkpoint +
3060 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
3063 lfsck_control_speed_by_self(com);
3064 if (unlikely(!thread_is_running(thread)))
3067 rc = iops->next(env, di);
3076 iops->fini(env, di);
3078 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
3079 lfsck_lfsck2name(lfsck), rc);
3084 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
3085 struct lfsck_component *com,
3086 struct lfsck_position *pos)
3088 struct lfsck_assistant_data *lad = com->lc_data;
3089 struct lfsck_namespace_req *lnr;
3091 if (list_empty(&lad->lad_req_list))
3094 lnr = list_entry(lad->lad_req_list.next,
3095 struct lfsck_namespace_req,
3097 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
3098 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
3099 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
3102 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
3103 struct lfsck_component *com,
3106 struct lfsck_instance *lfsck = com->lc_lfsck;
3107 struct lfsck_namespace *ns = com->lc_file_ram;
3109 down_write(&com->lc_sem);
3110 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
3111 HALF_SEC - lfsck->li_time_last_checkpoint);
3112 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3113 ns->ln_objs_checked_phase2 += com->lc_new_checked;
3114 com->lc_new_checked = 0;
3117 if (ns->ln_flags & LF_INCOMPLETE)
3118 ns->ln_status = LS_PARTIAL;
3120 ns->ln_status = LS_COMPLETED;
3121 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
3122 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
3123 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
3124 ns->ln_success_count++;
3125 } else if (rc == 0) {
3126 ns->ln_status = lfsck->li_status;
3127 if (ns->ln_status == 0)
3128 ns->ln_status = LS_STOPPED;
3130 ns->ln_status = LS_FAILED;
3133 rc = lfsck_namespace_store(env, com, false);
3134 up_write(&com->lc_sem);
3139 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
3140 struct lfsck_component *com,
3141 struct lfsck_request *lr)
3146 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
3147 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
3148 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
3149 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
3150 .la_double_scan_result = lfsck_namespace_double_scan_result,
3151 .la_req_fini = lfsck_namespace_assistant_req_fini,
3152 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
3156 * Verify the specified linkEA entry for the given directory object.
3157 * If the object has no such linkEA entry or it has more other linkEA
3158 * entries, then re-generate the linkEA with the given information.
3160 * \param[in] env pointer to the thread context
3161 * \param[in] dev pointer to the dt_device
3162 * \param[in] obj pointer to the dt_object to be handled
3163 * \param[in] cname the name for the child in the parent directory
3164 * \param[in] pfid the parent directory's FID for the linkEA
3166 * \retval 0 for success
3167 * \retval negative error number on failure
3169 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
3170 struct dt_object *obj, const struct lu_name *cname,
3171 const struct lu_fid *pfid)
3173 struct linkea_data ldata = { 0 };
3174 struct lu_buf linkea_buf;
3177 int fl = LU_XATTR_CREATE;
3181 LASSERT(S_ISDIR(lfsck_object_type(obj)));
3183 rc = lfsck_links_read(env, obj, &ldata);
3184 if (rc == -ENODATA) {
3186 } else if (rc == 0) {
3187 fl = LU_XATTR_REPLACE;
3188 if (ldata.ld_leh->leh_reccount != 1) {
3191 rc = linkea_links_find(&ldata, cname, pfid);
3200 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
3204 rc = linkea_add_buf(&ldata, cname, pfid);
3208 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
3209 ldata.ld_leh->leh_len);
3210 th = dt_trans_create(env, dev);
3212 RETURN(PTR_ERR(th));
3214 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
3215 XATTR_NAME_LINK, fl, th);
3219 rc = dt_trans_start_local(env, dev, th);
3223 dt_write_lock(env, obj, 0);
3224 rc = dt_xattr_set(env, obj, &linkea_buf,
3225 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
3226 dt_write_unlock(env, obj);
3231 dt_trans_stop(env, dev, th);
3236 * Get the name and parent directory's FID from the first linkEA entry.
3238 * \param[in] env pointer to the thread context
3239 * \param[in] obj pointer to the object which get linkEA from
3240 * \param[out] name pointer to the buffer to hold the name
3241 * in the first linkEA entry
3242 * \param[out] pfid pointer to the buffer to hold the parent
3243 * directory's FID in the first linkEA entry
3245 * \retval 0 for success
3246 * \retval negative error number on failure
3248 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
3249 char *name, struct lu_fid *pfid)
3251 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
3252 struct linkea_data ldata = { 0 };
3255 rc = lfsck_links_read(env, obj, &ldata);
3259 linkea_first_entry(&ldata);
3260 if (ldata.ld_lee == NULL)
3263 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
3264 /* To guarantee the 'name' is terminated with '0'. */
3265 memcpy(name, cname->ln_name, cname->ln_namelen);
3266 name[cname->ln_namelen] = 0;
3272 * Remove the name entry from the parent directory.
3274 * No need to care about the object referenced by the name entry,
3275 * either the name entry is invalid or redundant, or the referenced
3276 * object has been processed has been or will be handled by others.
3278 * \param[in] env pointer to the thread context
3279 * \param[in] lfsck pointer to the lfsck instance
3280 * \param[in] parent pointer to the lost+found object
3281 * \param[in] name the name for the name entry to be removed
3282 * \param[in] type the type for the name entry to be removed
3284 * \retval 0 for success
3285 * \retval negative error number on failure
3287 int lfsck_remove_name_entry(const struct lu_env *env,
3288 struct lfsck_instance *lfsck,
3289 struct dt_object *parent,
3290 const char *name, __u32 type)
3292 struct dt_device *dev = lfsck->li_next;
3294 struct lustre_handle lh = { 0 };
3298 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3299 MDS_INODELOCK_UPDATE, LCK_EX);
3303 th = dt_trans_create(env, dev);
3305 GOTO(unlock, rc = PTR_ERR(th));
3307 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
3311 if (S_ISDIR(type)) {
3312 rc = dt_declare_ref_del(env, parent, th);
3317 rc = dt_trans_start(env, dev, th);
3321 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
3326 if (S_ISDIR(type)) {
3327 dt_write_lock(env, parent, 0);
3328 rc = dt_ref_del(env, parent, th);
3329 dt_write_unlock(env, parent);
3335 dt_trans_stop(env, dev, th);
3338 lfsck_ibits_unlock(&lh, LCK_EX);
3340 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
3341 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
3342 PFID(lfsck_dto2fid(parent)), name, type, rc);
3348 * Update the object's name entry with the given FID.
3350 * \param[in] env pointer to the thread context
3351 * \param[in] lfsck pointer to the lfsck instance
3352 * \param[in] parent pointer to the parent directory that holds
3354 * \param[in] name the name for the entry to be updated
3355 * \param[in] pfid the new PFID for the name entry
3356 * \param[in] type the type for the name entry to be updated
3358 * \retval 0 for success
3359 * \retval negative error number on failure
3361 int lfsck_update_name_entry(const struct lu_env *env,
3362 struct lfsck_instance *lfsck,
3363 struct dt_object *parent, const char *name,
3364 const struct lu_fid *pfid, __u32 type)
3366 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
3367 struct dt_device *dev = lfsck->li_next;
3368 struct lustre_handle lh = { 0 };
3374 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3375 MDS_INODELOCK_UPDATE, LCK_EX);
3379 th = dt_trans_create(env, dev);
3381 GOTO(unlock, rc = PTR_ERR(th));
3383 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
3387 rec->rec_type = type;
3388 rec->rec_fid = pfid;
3389 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
3390 (const struct dt_key *)name, th);
3394 rc = dt_declare_ref_add(env, parent, th);
3398 rc = dt_trans_start(env, dev, th);
3402 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
3404 if (rc == -ENOENT) {
3412 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
3413 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
3414 if (rc == 0 && S_ISDIR(type) && !exists) {
3415 dt_write_lock(env, parent, 0);
3416 rc = dt_ref_add(env, parent, th);
3417 dt_write_unlock(env, parent);
3423 dt_trans_stop(env, dev, th);
3426 lfsck_ibits_unlock(&lh, LCK_EX);
3428 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
3429 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
3430 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
3435 int lfsck_namespace_setup(const struct lu_env *env,
3436 struct lfsck_instance *lfsck)
3438 struct lfsck_component *com;
3439 struct lfsck_namespace *ns;
3440 struct dt_object *root = NULL;
3441 struct dt_object *obj;
3445 LASSERT(lfsck->li_master);
3451 INIT_LIST_HEAD(&com->lc_link);
3452 INIT_LIST_HEAD(&com->lc_link_dir);
3453 init_rwsem(&com->lc_sem);
3454 atomic_set(&com->lc_ref, 1);
3455 com->lc_lfsck = lfsck;
3456 com->lc_type = LFSCK_TYPE_NAMESPACE;
3457 com->lc_ops = &lfsck_namespace_ops;
3458 com->lc_data = lfsck_assistant_data_init(
3459 &lfsck_namespace_assistant_ops,
3461 if (com->lc_data == NULL)
3462 GOTO(out, rc = -ENOMEM);
3464 com->lc_file_size = sizeof(struct lfsck_namespace);
3465 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
3466 if (com->lc_file_ram == NULL)
3467 GOTO(out, rc = -ENOMEM);
3469 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
3470 if (com->lc_file_disk == NULL)
3471 GOTO(out, rc = -ENOMEM);
3473 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3475 GOTO(out, rc = PTR_ERR(root));
3477 if (unlikely(!dt_try_as_dir(env, root)))
3478 GOTO(out, rc = -ENOTDIR);
3480 obj = local_index_find_or_create(env, lfsck->li_los, root,
3481 lfsck_namespace_name,
3482 S_IFREG | S_IRUGO | S_IWUSR,
3483 &dt_lfsck_features);
3485 GOTO(out, rc = PTR_ERR(obj));
3488 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
3492 rc = lfsck_namespace_load(env, com);
3494 rc = lfsck_namespace_reset(env, com, true);
3495 else if (rc == -ENODATA)
3496 rc = lfsck_namespace_init(env, com);
3500 ns = com->lc_file_ram;
3501 switch (ns->ln_status) {
3506 spin_lock(&lfsck->li_lock);
3507 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
3508 spin_unlock(&lfsck->li_lock);
3511 CERROR("%s: unknown lfsck_namespace status %d\n",
3512 lfsck_lfsck2name(lfsck), ns->ln_status);
3514 case LS_SCANNING_PHASE1:
3515 case LS_SCANNING_PHASE2:
3516 /* No need to store the status to disk right now.
3517 * If the system crashed before the status stored,
3518 * it will be loaded back when next time. */
3519 ns->ln_status = LS_CRASHED;
3523 spin_lock(&lfsck->li_lock);
3524 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
3525 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
3526 spin_unlock(&lfsck->li_lock);
3533 if (root != NULL && !IS_ERR(root))
3534 lu_object_put(env, &root->do_lu);
3536 lfsck_component_cleanup(env, com);
3537 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
3538 lfsck_lfsck2name(lfsck), rc);