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);
141 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
142 struct lfsck_namespace *src)
144 dst->ln_magic = cpu_to_le32(src->ln_magic);
145 dst->ln_status = cpu_to_le32(src->ln_status);
146 dst->ln_flags = cpu_to_le32(src->ln_flags);
147 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
148 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
149 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
150 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
151 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
152 dst->ln_time_last_checkpoint =
153 cpu_to_le64(src->ln_time_last_checkpoint);
154 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
155 &src->ln_pos_latest_start);
156 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
157 &src->ln_pos_last_checkpoint);
158 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
159 &src->ln_pos_first_inconsistent);
160 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
161 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
162 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
163 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
164 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
165 dst->ln_objs_repaired_phase2 =
166 cpu_to_le64(src->ln_objs_repaired_phase2);
167 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
168 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
169 dst->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
170 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
171 &src->ln_fid_latest_scanned_phase2);
172 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
173 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
174 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
175 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
178 static void lfsck_namespace_record_failure(const struct lu_env *env,
179 struct lfsck_instance *lfsck,
180 struct lfsck_namespace *ns)
182 struct lfsck_position pos;
184 ns->ln_items_failed++;
185 lfsck_pos_fill(env, lfsck, &pos, false);
186 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
187 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
188 ns->ln_pos_first_inconsistent = pos;
190 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
191 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
192 lfsck_lfsck2name(lfsck),
193 ns->ln_pos_first_inconsistent.lp_oit_cookie,
194 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
195 ns->ln_pos_first_inconsistent.lp_dir_cookie);
200 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
201 * \retval 0: succeed.
202 * \retval -ve: failed cases.
204 static int lfsck_namespace_load(const struct lu_env *env,
205 struct lfsck_component *com)
207 int len = com->lc_file_size;
210 rc = dt_xattr_get(env, com->lc_obj,
211 lfsck_buf_get(env, com->lc_file_disk, len),
212 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
214 struct lfsck_namespace *ns = com->lc_file_ram;
216 lfsck_namespace_le_to_cpu(ns,
217 (struct lfsck_namespace *)com->lc_file_disk);
218 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
219 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
220 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
221 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
226 } else if (rc != -ENODATA) {
227 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
228 "expected = %d: rc = %d\n",
229 lfsck_lfsck2name(com->lc_lfsck), len, rc);
236 static int lfsck_namespace_store(const struct lu_env *env,
237 struct lfsck_component *com, bool init)
239 struct dt_object *obj = com->lc_obj;
240 struct lfsck_instance *lfsck = com->lc_lfsck;
241 struct thandle *handle;
242 int len = com->lc_file_size;
246 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
247 (struct lfsck_namespace *)com->lc_file_ram);
248 handle = dt_trans_create(env, lfsck->li_bottom);
250 GOTO(log, rc = PTR_ERR(handle));
252 rc = dt_declare_xattr_set(env, obj,
253 lfsck_buf_get(env, com->lc_file_disk, len),
254 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
258 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
262 rc = dt_xattr_set(env, obj,
263 lfsck_buf_get(env, com->lc_file_disk, len),
264 XATTR_NAME_LFSCK_NAMESPACE,
265 init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
266 handle, BYPASS_CAPA);
271 dt_trans_stop(env, lfsck->li_bottom, handle);
275 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
276 lfsck_lfsck2name(lfsck), rc);
280 static int lfsck_namespace_init(const struct lu_env *env,
281 struct lfsck_component *com)
283 struct lfsck_namespace *ns = com->lc_file_ram;
286 memset(ns, 0, sizeof(*ns));
287 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
288 ns->ln_status = LS_INIT;
289 down_write(&com->lc_sem);
290 rc = lfsck_namespace_store(env, com, true);
291 up_write(&com->lc_sem);
296 * Update the namespace LFSCK tracing file for the given @fid
298 * \param[in] env pointer to the thread context
299 * \param[in] com pointer to the lfsck component
300 * \param[in] fid the fid which flags to be updated in the lfsck
302 * \param[in] add true if add new flags, otherwise remove flags
304 * \retval 0 for succeed or nothing to be done
305 * \retval negative error number on failure
307 int lfsck_namespace_trace_update(const struct lu_env *env,
308 struct lfsck_component *com,
309 const struct lu_fid *fid,
310 const __u8 flags, bool add)
312 struct lfsck_instance *lfsck = com->lc_lfsck;
313 struct dt_object *obj = com->lc_obj;
314 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
315 struct dt_device *dev = lfsck->li_bottom;
316 struct thandle *th = NULL;
324 down_write(&com->lc_sem);
325 fid_cpu_to_be(key, fid);
326 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
327 (const struct dt_key *)key, BYPASS_CAPA);
330 GOTO(unlock, rc = 0);
334 } else if (rc == 0) {
336 if ((old & flags) == flags)
337 GOTO(unlock, rc = 0);
341 if ((old & flags) == 0)
342 GOTO(unlock, rc = 0);
350 th = dt_trans_create(env, dev);
352 GOTO(log, rc = PTR_ERR(th));
355 rc = dt_declare_delete(env, obj,
356 (const struct dt_key *)key, th);
362 rc = dt_declare_insert(env, obj,
363 (const struct dt_rec *)&new,
364 (const struct dt_key *)key, th);
369 rc = dt_trans_start_local(env, dev, th);
374 rc = dt_delete(env, obj, (const struct dt_key *)key,
381 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
382 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
390 if (th != NULL && !IS_ERR(th))
391 dt_trans_stop(env, dev, th);
393 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
394 "tracing file, flags %x, old %x, new %x: rc = %d\n",
395 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
396 (__u32)flags, (__u32)old, (__u32)new, rc);
399 up_write(&com->lc_sem);
404 static int lfsck_namespace_check_exist(const struct lu_env *env,
405 struct dt_object *dir,
406 struct dt_object *obj, const char *name)
408 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
412 if (unlikely(lfsck_is_dead_obj(obj)))
413 RETURN(LFSCK_NAMEENTRY_DEAD);
415 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
416 (const struct dt_key *)name, BYPASS_CAPA);
418 RETURN(LFSCK_NAMEENTRY_REMOVED);
423 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
424 RETURN(LFSCK_NAMEENTRY_RECREATED);
429 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
430 struct dt_object *obj,
431 struct thandle *handle)
435 /* For destroying all invalid linkEA entries. */
436 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
440 /* For insert new linkEA entry. */
441 rc = dt_declare_xattr_set(env, obj,
442 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
443 XATTR_NAME_LINK, 0, handle);
447 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
448 struct linkea_data *ldata)
452 if (ldata->ld_buf->lb_buf == NULL)
455 if (!dt_object_exists(obj))
458 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
460 /* Buf was too small, figure out what we need. */
461 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
466 lu_buf_realloc(ldata->ld_buf, rc);
467 if (ldata->ld_buf->lb_buf == NULL)
470 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
475 rc = linkea_init(ldata);
481 * Remove linkEA for the given object.
483 * The caller should take the ldlm lock before the calling.
485 * \param[in] env pointer to the thread context
486 * \param[in] com pointer to the lfsck component
487 * \param[in] obj pointer to the dt_object to be handled
489 * \retval 0 for repaired cases
490 * \retval negative error number on failure
492 static int lfsck_namespace_links_remove(const struct lu_env *env,
493 struct lfsck_component *com,
494 struct dt_object *obj)
496 struct lfsck_instance *lfsck = com->lc_lfsck;
497 struct dt_device *dev = lfsck->li_bottom;
498 struct thandle *th = NULL;
502 LASSERT(dt_object_remote(obj) == 0);
504 th = dt_trans_create(env, dev);
506 GOTO(log, rc = PTR_ERR(th));
508 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
512 rc = dt_trans_start_local(env, dev, th);
516 dt_write_lock(env, obj, 0);
517 if (unlikely(lfsck_is_dead_obj(obj)))
518 GOTO(unlock, rc = -ENOENT);
520 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
521 GOTO(unlock, rc = 0);
523 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
528 dt_write_unlock(env, obj);
531 dt_trans_stop(env, dev, th);
534 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
535 "for the object "DFID": rc = %d\n",
536 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
539 struct lfsck_namespace *ns = com->lc_file_ram;
541 ns->ln_flags |= LF_INCONSISTENT;
547 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
548 struct linkea_data *ldata, struct thandle *handle)
550 const struct lu_buf *buf = lfsck_buf_get_const(env,
551 ldata->ld_buf->lb_buf,
552 ldata->ld_leh->leh_len);
554 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
558 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
559 struct lu_name *cname,
563 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
564 /* To guarantee the 'name' is terminated with '0'. */
565 memcpy(buf, cname->ln_name, cname->ln_namelen);
566 buf[cname->ln_namelen] = 0;
567 cname->ln_name = buf;
570 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
571 struct lu_name *cname,
575 struct link_ea_entry *oldlee;
579 oldlee = ldata->ld_lee;
580 oldlen = ldata->ld_reclen;
581 linkea_next_entry(ldata);
582 while (ldata->ld_lee != NULL) {
583 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
584 ldata->ld_lee->lee_reclen[1];
585 if (unlikely(ldata->ld_reclen == oldlen &&
586 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
591 linkea_del_buf(ldata, cname);
593 linkea_next_entry(ldata);
596 ldata->ld_lee = oldlee;
597 ldata->ld_reclen = oldlen;
602 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
603 struct lfsck_component *com,
604 struct dt_object *orphan,
605 const char *infix, const char *type,
612 static int lfsck_namespace_insert_normal(const struct lu_env *env,
613 struct lfsck_component *com,
614 struct dt_object *parent,
615 struct dt_object *child,
622 static int lfsck_namespace_create_orphan(const struct lu_env *env,
623 struct lfsck_component *com,
624 struct dt_object *orphan)
631 * Remove the specified entry from the linkEA.
633 * Locate the linkEA entry with the given @cname and @pfid, then
634 * remove this entry or the other entries those are repeated with
637 * \param[in] env pointer to the thread context
638 * \param[in] com pointer to the lfsck component
639 * \param[in] obj pointer to the dt_object to be handled
640 * \param[in,out]ldata pointer to the buffer that holds the linkEA
641 * \param[in] cname the name for the child in the parent directory
642 * \param[in] pfid the parent directory's FID for the linkEA
643 * \param[in] next if true, then remove the first found linkEA
644 * entry, and move the ldata->ld_lee to next entry
646 * \retval positive number for repaired cases
647 * \retval 0 if nothing to be repaired
648 * \retval negative error number on failure
650 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
651 struct lfsck_component *com,
652 struct dt_object *obj,
653 struct linkea_data *ldata,
654 struct lu_name *cname,
658 struct lfsck_instance *lfsck = com->lc_lfsck;
659 struct dt_device *dev = lfsck->li_bottom;
660 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
661 struct thandle *th = NULL;
662 struct lustre_handle lh = { 0 };
663 struct linkea_data ldata_new = { 0 };
664 struct lu_buf linkea_buf;
668 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
669 MDS_INODELOCK_UPDATE |
670 MDS_INODELOCK_XATTR, LCK_EX);
675 linkea_del_buf(ldata, cname);
677 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
679 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
680 ldata->ld_leh->leh_len);
683 th = dt_trans_create(env, dev);
685 GOTO(unlock1, rc = PTR_ERR(th));
687 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
688 XATTR_NAME_LINK, 0, th);
692 rc = dt_trans_start_local(env, dev, th);
696 dt_write_lock(env, obj, 0);
697 if (unlikely(lfsck_is_dead_obj(obj)))
698 GOTO(unlock2, rc = -ENOENT);
700 rc = lfsck_links_read2(env, obj, &ldata_new);
704 /* The specified linkEA entry has been removed by race. */
705 rc = linkea_links_find(&ldata_new, cname, pfid);
707 GOTO(unlock2, rc = 0);
709 if (bk->lb_param & LPF_DRYRUN)
710 GOTO(unlock2, rc = 1);
713 linkea_del_buf(&ldata_new, cname);
715 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
718 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
719 dt_write_unlock(env, obj);
720 dt_trans_stop(env, dev, th);
721 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
722 ldata_new.ld_leh->leh_len);
726 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
727 ldata_new.ld_leh->leh_len);
728 rc = dt_xattr_set(env, obj, &linkea_buf,
729 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
731 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
734 dt_write_unlock(env, obj);
737 dt_trans_stop(env, dev, th);
740 lfsck_ibits_unlock(&lh, LCK_EX);
743 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
744 "for the object: "DFID", parent "DFID", name %.*s\n",
745 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
746 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
750 struct lfsck_namespace *ns = com->lc_file_ram;
752 ns->ln_flags |= LF_INCONSISTENT;
759 * Conditionally remove the specified entry from the linkEA.
761 * Take the parent lock firstly, then check whether the specified
762 * name entry exists or not: if yes, do nothing; otherwise, call
763 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
765 * \param[in] env pointer to the thread context
766 * \param[in] com pointer to the lfsck component
767 * \param[in] parent pointer to the parent directory
768 * \param[in] child pointer to the child object that holds the linkEA
769 * \param[in,out]ldata pointer to the buffer that holds the linkEA
770 * \param[in] cname the name for the child in the parent directory
771 * \param[in] pfid the parent directory's FID for the linkEA
773 * \retval positive number for repaired cases
774 * \retval 0 if nothing to be repaired
775 * \retval negative error number on failure
777 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
778 struct lfsck_component *com,
779 struct dt_object *parent,
780 struct dt_object *child,
781 struct linkea_data *ldata,
782 struct lu_name *cname,
785 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
786 struct lustre_handle lh = { 0 };
790 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
791 MDS_INODELOCK_UPDATE, LCK_EX);
795 dt_read_lock(env, parent, 0);
796 if (unlikely(lfsck_is_dead_obj(parent))) {
797 dt_read_unlock(env, parent);
798 lfsck_ibits_unlock(&lh, LCK_EX);
799 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
805 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
806 (const struct dt_key *)cname->ln_name,
808 dt_read_unlock(env, parent);
810 /* It is safe to release the ldlm lock, because when the logic come
811 * here, we have got all the needed information above whether the
812 * linkEA entry is valid or not. It is not important that others
813 * may add new linkEA entry after the ldlm lock released. If other
814 * has removed the specified linkEA entry by race, then it is OK,
815 * because the subsequent lfsck_namespace_shrink_linkea() can handle
817 lfsck_ibits_unlock(&lh, LCK_EX);
819 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
828 /* The LFSCK just found some internal status of cross-MDTs
829 * create operation. That is normal. */
830 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
831 linkea_next_entry(ldata);
836 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
843 * Double scan the MDT-object for namespace LFSCK.
845 * If the MDT-object contains invalid or repeated linkEA entries, then drop
846 * those entries from the linkEA; if the linkEA becomes empty or the object
847 * has no linkEA, then it is an orphan and will be added into the directory
848 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
849 * the remote parent; if the name entry corresponding to some linkEA entry
850 * is lost, then add the name entry back to the namespace.
852 * \param[in] env pointer to the thread context
853 * \param[in] com pointer to the lfsck component
854 * \param[in] child pointer to the dt_object to be handled
855 * \param[in] flags some hints to indicate how the @child should be handled
857 * \retval positive number for repaired cases
858 * \retval 0 if nothing to be repaired
859 * \retval negative error number on failure
861 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
862 struct lfsck_component *com,
863 struct dt_object *child, __u8 flags)
865 struct lfsck_thread_info *info = lfsck_env_info(env);
866 struct lu_attr *la = &info->lti_la;
867 struct lu_name *cname = &info->lti_name;
868 struct lu_fid *pfid = &info->lti_fid;
869 struct lu_fid *cfid = &info->lti_fid2;
870 struct lfsck_instance *lfsck = com->lc_lfsck;
871 struct lfsck_namespace *ns = com->lc_file_ram;
872 struct dt_object *parent = NULL;
873 struct linkea_data ldata = { 0 };
874 bool repaired = false;
879 dt_read_lock(env, child, 0);
880 if (unlikely(lfsck_is_dead_obj(child))) {
881 dt_read_unlock(env, child);
886 rc = lfsck_links_read(env, child, &ldata);
887 dt_read_unlock(env, child);
891 linkea_first_entry(&ldata);
892 while (ldata.ld_lee != NULL) {
893 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
895 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
897 /* Found repeated linkEA entries */
899 rc = lfsck_namespace_shrink_linkea(env, com, child,
900 &ldata, cname, pfid, false);
912 /* Invalid PFID in the linkEA entry. */
913 if (!fid_is_sane(pfid)) {
914 rc = lfsck_namespace_shrink_linkea(env, com, child,
915 &ldata, cname, pfid, true);
925 parent = lfsck_object_find(env, lfsck, pfid);
927 GOTO(out, rc = PTR_ERR(parent));
929 if (!dt_object_exists(parent)) {
930 if (ldata.ld_leh->leh_reccount > 1) {
931 /* If it is NOT the last linkEA entry, then
932 * there is still other chance to make the
933 * child to be visible via other parent, then
934 * remove this linkEA entry. */
935 rc = lfsck_namespace_shrink_linkea(env, com,
936 child, &ldata, cname, pfid, true);
938 /* Create the lost parent as an orphan. */
939 rc = lfsck_namespace_create_orphan(env, com,
942 lfsck_object_put(env, parent);
950 /* Add the missed name entry to the parent. */
951 rc = lfsck_namespace_insert_normal(env, com,
952 parent, child, cname->ln_name);
953 linkea_next_entry(&ldata);
956 lfsck_object_put(env, parent);
966 /* The linkEA entry with bad parent will be removed. */
967 if (unlikely(!dt_try_as_dir(env, parent))) {
968 lfsck_object_put(env, parent);
969 rc = lfsck_namespace_shrink_linkea(env, com, child,
970 &ldata, cname, pfid, true);
980 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
981 (const struct dt_key *)cname->ln_name,
983 if (rc != 0 && rc != -ENOENT) {
984 lfsck_object_put(env, parent);
990 lfsck_object_put(env, parent);
991 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
992 /* It is the most common case that we
993 * find the name entry corresponding
994 * to the linkEA entry. */
995 linkea_next_entry(&ldata);
997 /* XXX: The name entry references another
998 * MDT-object that may be created by
999 * the LFSCK for repairing dangling
1000 * name entry. There will be another
1001 * patch for further processing. */
1002 rc = lfsck_namespace_shrink_linkea(env, com,
1003 child, &ldata, cname, pfid, true);
1014 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
1018 /* If there is no name entry in the parent dir and the object
1019 * link count is less than the linkea entries count, then the
1020 * linkea entry should be removed. */
1021 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
1022 rc = lfsck_namespace_shrink_linkea_cond(env, com,
1023 parent, child, &ldata, cname, pfid);
1024 lfsck_object_put(env, parent);
1034 /* Add the missed name entry back to the namespace. */
1035 rc = lfsck_namespace_insert_normal(env, com, parent, child,
1037 lfsck_object_put(env, parent);
1044 linkea_next_entry(&ldata);
1050 if (rc < 0 && rc != -ENODATA)
1054 LASSERT(ldata.ld_leh != NULL);
1056 count = ldata.ld_leh->leh_reccount;
1060 /* If the child becomes orphan, then insert it into
1061 * the global .lustre/lost+found/MDTxxxx directory. */
1062 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
1071 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
1075 if (la->la_nlink != count) {
1076 /* XXX: there will be other patch(es) for MDT-object
1077 * hard links verification. */
1081 if (la->la_nlink > 1) {
1082 down_write(&com->lc_sem);
1083 ns->ln_mul_linked_repaired++;
1084 up_write(&com->lc_sem);
1094 static void lfsck_namespace_dump_statistics(struct seq_file *m,
1095 struct lfsck_namespace *ns,
1096 __u64 checked_phase1,
1097 __u64 checked_phase2,
1101 seq_printf(m, "checked_phase1: "LPU64"\n"
1102 "checked_phase2: "LPU64"\n"
1103 "updated_phase1: "LPU64"\n"
1104 "updated_phase2: "LPU64"\n"
1105 "failed_phase1: "LPU64"\n"
1106 "failed_phase2: "LPU64"\n"
1107 "directories: "LPU64"\n"
1108 "dirent_repaired: "LPU64"\n"
1109 "linkea_repaired: "LPU64"\n"
1110 "nlinks_repaired: "LPU64"\n"
1111 "lost_found: "LPU64"\n"
1112 "multiple_linked_checked: "LPU64"\n"
1113 "multiple_linked_repaired: "LPU64"\n"
1114 "success_count: %u\n"
1115 "run_time_phase1: %u seconds\n"
1116 "run_time_phase2: %u seconds\n",
1119 ns->ln_items_repaired,
1120 ns->ln_objs_repaired_phase2,
1121 ns->ln_items_failed,
1122 ns->ln_objs_failed_phase2,
1123 ns->ln_dirs_checked,
1124 ns->ln_dirent_repaired,
1125 ns->ln_linkea_repaired,
1126 ns->ln_objs_nlink_repaired,
1127 ns->ln_objs_lost_found,
1128 ns->ln_mul_linked_checked,
1129 ns->ln_mul_linked_repaired,
1130 ns->ln_success_count,
1135 /* namespace APIs */
1137 static int lfsck_namespace_reset(const struct lu_env *env,
1138 struct lfsck_component *com, bool init)
1140 struct lfsck_instance *lfsck = com->lc_lfsck;
1141 struct lfsck_namespace *ns = com->lc_file_ram;
1142 struct dt_object *root;
1143 struct dt_object *dto;
1147 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
1149 GOTO(log, rc = PTR_ERR(root));
1151 if (unlikely(!dt_try_as_dir(env, root)))
1152 GOTO(put, rc = -ENOTDIR);
1154 down_write(&com->lc_sem);
1156 memset(ns, 0, sizeof(*ns));
1158 __u32 count = ns->ln_success_count;
1159 __u64 last_time = ns->ln_time_last_complete;
1161 memset(ns, 0, sizeof(*ns));
1162 ns->ln_success_count = count;
1163 ns->ln_time_last_complete = last_time;
1165 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
1166 ns->ln_status = LS_INIT;
1168 rc = local_object_unlink(env, lfsck->li_bottom, root,
1169 lfsck_namespace_name);
1173 lfsck_object_put(env, com->lc_obj);
1175 dto = local_index_find_or_create(env, lfsck->li_los, root,
1176 lfsck_namespace_name,
1177 S_IFREG | S_IRUGO | S_IWUSR,
1178 &dt_lfsck_features);
1180 GOTO(out, rc = PTR_ERR(dto));
1183 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
1187 rc = lfsck_namespace_store(env, com, true);
1192 up_write(&com->lc_sem);
1195 lu_object_put(env, &root->do_lu);
1197 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
1198 lfsck_lfsck2name(lfsck), rc);
1203 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
1206 struct lfsck_namespace *ns = com->lc_file_ram;
1208 down_write(&com->lc_sem);
1210 com->lc_new_checked++;
1211 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
1212 up_write(&com->lc_sem);
1215 static int lfsck_namespace_checkpoint(const struct lu_env *env,
1216 struct lfsck_component *com, bool init)
1218 struct lfsck_instance *lfsck = com->lc_lfsck;
1219 struct lfsck_namespace *ns = com->lc_file_ram;
1223 rc = lfsck_checkpoint_generic(env, com);
1228 down_write(&com->lc_sem);
1230 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
1232 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
1233 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1234 HALF_SEC - lfsck->li_time_last_checkpoint);
1235 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1236 ns->ln_items_checked += com->lc_new_checked;
1237 com->lc_new_checked = 0;
1240 rc = lfsck_namespace_store(env, com, false);
1241 up_write(&com->lc_sem);
1244 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
1245 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
1246 lfsck->li_pos_current.lp_oit_cookie,
1247 PFID(&lfsck->li_pos_current.lp_dir_parent),
1248 lfsck->li_pos_current.lp_dir_cookie, rc);
1250 return rc > 0 ? 0 : rc;
1253 static int lfsck_namespace_prep(const struct lu_env *env,
1254 struct lfsck_component *com,
1255 struct lfsck_start_param *lsp)
1257 struct lfsck_instance *lfsck = com->lc_lfsck;
1258 struct lfsck_namespace *ns = com->lc_file_ram;
1259 struct lfsck_position *pos = &com->lc_pos_start;
1262 if (ns->ln_status == LS_COMPLETED) {
1263 rc = lfsck_namespace_reset(env, com, false);
1265 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
1268 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
1269 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
1275 down_write(&com->lc_sem);
1276 ns->ln_time_latest_start = cfs_time_current_sec();
1277 spin_lock(&lfsck->li_lock);
1279 if (ns->ln_flags & LF_SCANNED_ONCE) {
1280 if (!lfsck->li_drop_dryrun ||
1281 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
1282 ns->ln_status = LS_SCANNING_PHASE2;
1283 list_move_tail(&com->lc_link,
1284 &lfsck->li_list_double_scan);
1285 if (!list_empty(&com->lc_link_dir))
1286 list_del_init(&com->lc_link_dir);
1287 lfsck_pos_set_zero(pos);
1289 ns->ln_status = LS_SCANNING_PHASE1;
1290 ns->ln_run_time_phase1 = 0;
1291 ns->ln_run_time_phase2 = 0;
1292 ns->ln_items_checked = 0;
1293 ns->ln_items_repaired = 0;
1294 ns->ln_items_failed = 0;
1295 ns->ln_dirs_checked = 0;
1296 ns->ln_objs_checked_phase2 = 0;
1297 ns->ln_objs_repaired_phase2 = 0;
1298 ns->ln_objs_failed_phase2 = 0;
1299 ns->ln_objs_nlink_repaired = 0;
1300 ns->ln_objs_lost_found = 0;
1301 ns->ln_dirent_repaired = 0;
1302 ns->ln_linkea_repaired = 0;
1303 ns->ln_mul_linked_checked = 0;
1304 ns->ln_mul_linked_repaired = 0;
1305 fid_zero(&ns->ln_fid_latest_scanned_phase2);
1306 if (list_empty(&com->lc_link_dir))
1307 list_add_tail(&com->lc_link_dir,
1308 &lfsck->li_list_dir);
1309 *pos = ns->ln_pos_first_inconsistent;
1312 ns->ln_status = LS_SCANNING_PHASE1;
1313 if (list_empty(&com->lc_link_dir))
1314 list_add_tail(&com->lc_link_dir,
1315 &lfsck->li_list_dir);
1316 if (!lfsck->li_drop_dryrun ||
1317 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
1318 *pos = ns->ln_pos_last_checkpoint;
1319 pos->lp_oit_cookie++;
1321 *pos = ns->ln_pos_first_inconsistent;
1325 spin_unlock(&lfsck->li_lock);
1326 up_write(&com->lc_sem);
1328 rc = lfsck_start_assistant(env, com, lsp);
1330 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
1331 DFID", "LPX64"]: rc = %d\n",
1332 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
1333 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
1338 static int lfsck_namespace_exec_oit(const struct lu_env *env,
1339 struct lfsck_component *com,
1340 struct dt_object *obj)
1342 struct lfsck_thread_info *info = lfsck_env_info(env);
1343 struct lfsck_namespace *ns = com->lc_file_ram;
1344 struct lfsck_instance *lfsck = com->lc_lfsck;
1345 const struct lu_fid *fid = lfsck_dto2fid(obj);
1346 struct lu_attr *la = &info->lti_la;
1347 struct lu_fid *pfid = &info->lti_fid2;
1348 struct lu_name *cname = &info->lti_name;
1349 struct lu_seq_range *range = &info->lti_range;
1350 struct dt_device *dev = lfsck->li_bottom;
1351 struct seq_server_site *ss =
1352 lu_site2seq(dev->dd_lu_dev.ld_site);
1353 struct linkea_data ldata = { 0 };
1354 __u32 idx = lfsck_dev_idx(dev);
1358 rc = lfsck_links_read(env, obj, &ldata);
1362 /* -EINVAL means crashed linkEA, should be verified. */
1363 if (rc == -EINVAL) {
1364 rc = lfsck_namespace_trace_update(env, com, fid,
1365 LNTF_CHECK_LINKEA, true);
1367 struct lustre_handle lh = { 0 };
1369 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1370 MDS_INODELOCK_UPDATE |
1371 MDS_INODELOCK_XATTR, LCK_EX);
1373 rc = lfsck_namespace_links_remove(env, com,
1375 lfsck_ibits_unlock(&lh, LCK_EX);
1379 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
1382 /* zero-linkEA object may be orphan, but it also maybe because
1383 * of upgrading. Currently, we cannot record it for double scan.
1384 * Because it may cause the LFSCK tracing file to be too large. */
1385 if (rc == -ENODATA) {
1386 if (S_ISDIR(lfsck_object_type(obj)))
1389 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1393 if (la->la_nlink > 1)
1394 rc = lfsck_namespace_trace_update(env, com, fid,
1395 LNTF_CHECK_LINKEA, true);
1403 /* Record multiple-linked object. */
1404 if (ldata.ld_leh->leh_reccount > 1) {
1405 rc = lfsck_namespace_trace_update(env, com, fid,
1406 LNTF_CHECK_LINKEA, true);
1411 linkea_first_entry(&ldata);
1412 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
1413 if (!fid_is_sane(pfid)) {
1414 rc = lfsck_namespace_trace_update(env, com, fid,
1415 LNTF_CHECK_PARENT, true);
1417 fld_range_set_mdt(range);
1418 rc = fld_local_lookup(env, ss->ss_server_fld,
1419 fid_seq(pfid), range);
1420 if ((rc == -ENOENT) ||
1421 (rc == 0 && range->lsr_index != idx)) {
1422 rc = lfsck_namespace_trace_update(env, com, fid,
1423 LNTF_CHECK_LINKEA, true);
1425 if (S_ISDIR(lfsck_object_type(obj)))
1428 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1432 if (la->la_nlink > 1)
1433 rc = lfsck_namespace_trace_update(env, com,
1434 fid, LNTF_CHECK_LINKEA, true);
1441 down_write(&com->lc_sem);
1442 com->lc_new_checked++;
1443 if (S_ISDIR(lfsck_object_type(obj)))
1444 ns->ln_dirs_checked++;
1446 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
1447 up_write(&com->lc_sem);
1452 static int lfsck_namespace_exec_dir(const struct lu_env *env,
1453 struct lfsck_component *com,
1454 struct lu_dirent *ent, __u16 type)
1456 struct lfsck_assistant_data *lad = com->lc_data;
1457 struct lfsck_namespace_req *lnr;
1458 bool wakeup = false;
1460 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
1462 struct lfsck_namespace *ns = com->lc_file_ram;
1464 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
1465 return PTR_ERR(lnr);
1468 spin_lock(&lad->lad_lock);
1469 if (lad->lad_assistant_status < 0) {
1470 spin_unlock(&lad->lad_lock);
1471 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
1472 return lad->lad_assistant_status;
1475 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
1476 if (lad->lad_prefetched == 0)
1479 lad->lad_prefetched++;
1480 spin_unlock(&lad->lad_lock);
1482 wake_up_all(&lad->lad_thread.t_ctl_waitq);
1484 down_write(&com->lc_sem);
1485 com->lc_new_checked++;
1486 up_write(&com->lc_sem);
1491 static int lfsck_namespace_post(const struct lu_env *env,
1492 struct lfsck_component *com,
1493 int result, bool init)
1495 struct lfsck_instance *lfsck = com->lc_lfsck;
1496 struct lfsck_namespace *ns = com->lc_file_ram;
1500 lfsck_post_generic(env, com, &result);
1502 down_write(&com->lc_sem);
1503 spin_lock(&lfsck->li_lock);
1505 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
1507 ns->ln_status = LS_SCANNING_PHASE2;
1508 ns->ln_flags |= LF_SCANNED_ONCE;
1509 ns->ln_flags &= ~LF_UPGRADE;
1510 list_del_init(&com->lc_link_dir);
1511 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
1512 } else if (result == 0) {
1513 ns->ln_status = lfsck->li_status;
1514 if (ns->ln_status == 0)
1515 ns->ln_status = LS_STOPPED;
1516 if (ns->ln_status != LS_PAUSED) {
1517 list_del_init(&com->lc_link_dir);
1518 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
1521 ns->ln_status = LS_FAILED;
1522 list_del_init(&com->lc_link_dir);
1523 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
1525 spin_unlock(&lfsck->li_lock);
1528 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1529 HALF_SEC - lfsck->li_time_last_checkpoint);
1530 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1531 ns->ln_items_checked += com->lc_new_checked;
1532 com->lc_new_checked = 0;
1535 rc = lfsck_namespace_store(env, com, false);
1536 up_write(&com->lc_sem);
1538 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
1539 lfsck_lfsck2name(lfsck), rc);
1545 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1548 struct lfsck_instance *lfsck = com->lc_lfsck;
1549 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1550 struct lfsck_namespace *ns = com->lc_file_ram;
1553 down_read(&com->lc_sem);
1554 seq_printf(m, "name: lfsck_namespace\n"
1560 lfsck_status2names(ns->ln_status));
1562 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
1566 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
1570 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
1571 "time_since_last_completed");
1575 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
1576 "time_since_latest_start");
1580 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
1581 "time_since_last_checkpoint");
1585 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
1586 "latest_start_position");
1590 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
1591 "last_checkpoint_position");
1595 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
1596 "first_failure_position");
1600 if (ns->ln_status == LS_SCANNING_PHASE1) {
1601 struct lfsck_position pos;
1602 const struct dt_it_ops *iops;
1603 cfs_duration_t duration = cfs_time_current() -
1604 lfsck->li_time_last_checkpoint;
1605 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1606 __u64 speed = checked;
1607 __u64 new_checked = com->lc_new_checked * HZ;
1608 __u32 rtime = ns->ln_run_time_phase1 +
1609 cfs_duration_sec(duration + HALF_SEC);
1612 do_div(new_checked, duration);
1614 do_div(speed, rtime);
1615 lfsck_namespace_dump_statistics(m, ns, checked,
1616 ns->ln_objs_checked_phase2,
1617 rtime, ns->ln_run_time_phase2);
1619 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
1620 "average_speed_phase2: N/A\n"
1621 "real_time_speed_phase1: "LPU64" items/sec\n"
1622 "real_time_speed_phase2: N/A\n",
1626 LASSERT(lfsck->li_di_oit != NULL);
1628 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1630 /* The low layer otable-based iteration position may NOT
1631 * exactly match the namespace-based directory traversal
1632 * cookie. Generally, it is not a serious issue. But the
1633 * caller should NOT make assumption on that. */
1634 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1635 if (!lfsck->li_current_oit_processed)
1636 pos.lp_oit_cookie--;
1638 spin_lock(&lfsck->li_lock);
1639 if (lfsck->li_di_dir != NULL) {
1640 pos.lp_dir_cookie = lfsck->li_cookie_dir;
1641 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
1642 fid_zero(&pos.lp_dir_parent);
1643 pos.lp_dir_cookie = 0;
1646 *lfsck_dto2fid(lfsck->li_obj_dir);
1649 fid_zero(&pos.lp_dir_parent);
1650 pos.lp_dir_cookie = 0;
1652 spin_unlock(&lfsck->li_lock);
1653 lfsck_pos_dump(m, &pos, "current_position");
1654 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1655 cfs_duration_t duration = cfs_time_current() -
1656 lfsck->li_time_last_checkpoint;
1657 __u64 checked = ns->ln_objs_checked_phase2 +
1658 com->lc_new_checked;
1659 __u64 speed1 = ns->ln_items_checked;
1660 __u64 speed2 = checked;
1661 __u64 new_checked = com->lc_new_checked * HZ;
1662 __u32 rtime = ns->ln_run_time_phase2 +
1663 cfs_duration_sec(duration + HALF_SEC);
1666 do_div(new_checked, duration);
1667 if (ns->ln_run_time_phase1 != 0)
1668 do_div(speed1, ns->ln_run_time_phase1);
1670 do_div(speed2, rtime);
1671 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
1673 ns->ln_run_time_phase1, rtime);
1675 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
1676 "average_speed_phase2: "LPU64" objs/sec\n"
1677 "real_time_speed_phase1: N/A\n"
1678 "real_time_speed_phase2: "LPU64" objs/sec\n"
1679 "current_position: "DFID"\n",
1683 PFID(&ns->ln_fid_latest_scanned_phase2));
1685 __u64 speed1 = ns->ln_items_checked;
1686 __u64 speed2 = ns->ln_objs_checked_phase2;
1688 if (ns->ln_run_time_phase1 != 0)
1689 do_div(speed1, ns->ln_run_time_phase1);
1690 if (ns->ln_run_time_phase2 != 0)
1691 do_div(speed2, ns->ln_run_time_phase2);
1692 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
1693 ns->ln_objs_checked_phase2,
1694 ns->ln_run_time_phase1,
1695 ns->ln_run_time_phase2);
1697 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
1698 "average_speed_phase2: "LPU64" objs/sec\n"
1699 "real_time_speed_phase1: N/A\n"
1700 "real_time_speed_phase2: N/A\n"
1701 "current_position: N/A\n",
1706 up_read(&com->lc_sem);
1710 static int lfsck_namespace_double_scan(const struct lu_env *env,
1711 struct lfsck_component *com)
1713 struct lfsck_namespace *ns = com->lc_file_ram;
1715 return lfsck_double_scan_generic(env, com, ns->ln_status);
1718 static void lfsck_namespace_data_release(const struct lu_env *env,
1719 struct lfsck_component *com)
1721 struct lfsck_assistant_data *lad = com->lc_data;
1722 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
1723 struct lfsck_tgt_desc *ltd;
1724 struct lfsck_tgt_desc *next;
1726 LASSERT(lad != NULL);
1727 LASSERT(thread_is_init(&lad->lad_thread) ||
1728 thread_is_stopped(&lad->lad_thread));
1729 LASSERT(list_empty(&lad->lad_req_list));
1731 com->lc_data = NULL;
1733 spin_lock(<ds->ltd_lock);
1734 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
1735 ltd_namespace_phase_list) {
1736 list_del_init(<d->ltd_namespace_phase_list);
1738 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
1739 ltd_namespace_phase_list) {
1740 list_del_init(<d->ltd_namespace_phase_list);
1742 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
1743 ltd_namespace_list) {
1744 list_del_init(<d->ltd_namespace_list);
1746 spin_unlock(<ds->ltd_lock);
1748 CFS_FREE_BITMAP(lad->lad_bitmap);
1753 static int lfsck_namespace_in_notify(const struct lu_env *env,
1754 struct lfsck_component *com,
1755 struct lfsck_request *lr)
1757 struct lfsck_instance *lfsck = com->lc_lfsck;
1758 struct lfsck_namespace *ns = com->lc_file_ram;
1759 struct lfsck_assistant_data *lad = com->lc_data;
1760 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
1761 struct lfsck_tgt_desc *ltd;
1765 if (lr->lr_event != LE_PHASE1_DONE &&
1766 lr->lr_event != LE_PHASE2_DONE &&
1767 lr->lr_event != LE_PEER_EXIT)
1770 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
1771 "status %d\n", lfsck_lfsck2name(lfsck), lr->lr_event,
1772 lr->lr_index, lr->lr_status);
1774 spin_lock(<ds->ltd_lock);
1775 ltd = LTD_TGT(ltds, lr->lr_index);
1777 spin_unlock(<ds->ltd_lock);
1782 list_del_init(<d->ltd_namespace_phase_list);
1783 switch (lr->lr_event) {
1784 case LE_PHASE1_DONE:
1785 if (lr->lr_status <= 0) {
1786 ltd->ltd_namespace_done = 1;
1787 list_del_init(<d->ltd_namespace_list);
1788 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
1789 "phase1 for namespace LFSCK: rc = %d.\n",
1790 lfsck_lfsck2name(lfsck),
1791 ltd->ltd_index, lr->lr_status);
1792 ns->ln_flags |= LF_INCOMPLETE;
1797 if (list_empty(<d->ltd_namespace_list))
1798 list_add_tail(<d->ltd_namespace_list,
1799 &lad->lad_mdt_list);
1800 list_add_tail(<d->ltd_namespace_phase_list,
1801 &lad->lad_mdt_phase2_list);
1803 case LE_PHASE2_DONE:
1804 ltd->ltd_namespace_done = 1;
1805 list_del_init(<d->ltd_namespace_list);
1809 ltd->ltd_namespace_done = 1;
1810 list_del_init(<d->ltd_namespace_list);
1811 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
1813 "%s: the peer MDT %x exit namespace LFSCK\n",
1814 lfsck_lfsck2name(lfsck), ltd->ltd_index);
1815 ns->ln_flags |= LF_INCOMPLETE;
1821 spin_unlock(<ds->ltd_lock);
1823 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
1824 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
1826 memset(stop, 0, sizeof(*stop));
1827 stop->ls_status = lr->lr_status;
1828 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
1829 lfsck_stop(env, lfsck->li_bottom, stop);
1830 } else if (lfsck_phase2_next_ready(lad)) {
1831 wake_up_all(&lad->lad_thread.t_ctl_waitq);
1837 static int lfsck_namespace_query(const struct lu_env *env,
1838 struct lfsck_component *com)
1840 struct lfsck_namespace *ns = com->lc_file_ram;
1842 return ns->ln_status;
1845 static struct lfsck_operations lfsck_namespace_ops = {
1846 .lfsck_reset = lfsck_namespace_reset,
1847 .lfsck_fail = lfsck_namespace_fail,
1848 .lfsck_checkpoint = lfsck_namespace_checkpoint,
1849 .lfsck_prep = lfsck_namespace_prep,
1850 .lfsck_exec_oit = lfsck_namespace_exec_oit,
1851 .lfsck_exec_dir = lfsck_namespace_exec_dir,
1852 .lfsck_post = lfsck_namespace_post,
1853 .lfsck_dump = lfsck_namespace_dump,
1854 .lfsck_double_scan = lfsck_namespace_double_scan,
1855 .lfsck_data_release = lfsck_namespace_data_release,
1856 .lfsck_quit = lfsck_quit_generic,
1857 .lfsck_in_notify = lfsck_namespace_in_notify,
1858 .lfsck_query = lfsck_namespace_query,
1861 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
1862 struct lfsck_component *com,
1863 struct lfsck_assistant_req *lar)
1865 struct lfsck_thread_info *info = lfsck_env_info(env);
1866 struct lu_attr *la = &info->lti_la;
1867 struct lfsck_instance *lfsck = com->lc_lfsck;
1868 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1869 struct lfsck_namespace *ns = com->lc_file_ram;
1870 struct linkea_data ldata = { 0 };
1871 const struct lu_name *cname;
1872 struct thandle *handle = NULL;
1873 struct lfsck_namespace_req *lnr =
1874 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
1875 struct dt_object *dir = lnr->lnr_obj;
1876 struct dt_object *obj = NULL;
1877 const struct lu_fid *pfid = lfsck_dto2fid(dir);
1878 struct dt_device *dev;
1879 struct lustre_handle lh = { 0 };
1880 bool repaired = false;
1881 bool dtlocked = false;
1890 if (lnr->lnr_attr & LUDA_UPGRADE) {
1891 ns->ln_flags |= LF_UPGRADE;
1892 ns->ln_dirent_repaired++;
1894 } else if (lnr->lnr_attr & LUDA_REPAIR) {
1895 ns->ln_flags |= LF_INCONSISTENT;
1896 ns->ln_dirent_repaired++;
1900 if (lnr->lnr_name[0] == '.' &&
1901 (lnr->lnr_namelen == 1 ||
1902 (lnr->lnr_namelen == 2 && lnr->lnr_name[1] == '.') ||
1903 fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
1906 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
1908 GOTO(out, rc = idx);
1910 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
1911 dev = lfsck->li_next;
1913 struct lfsck_tgt_desc *ltd;
1915 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
1916 if (unlikely(ltd == NULL)) {
1917 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
1918 "did not join the namespace LFSCK\n",
1919 lfsck_lfsck2name(lfsck), idx);
1920 ns->ln_flags |= LF_INCOMPLETE;
1922 GOTO(out, rc = -ENODEV);
1928 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
1930 GOTO(out, rc = PTR_ERR(obj));
1932 if (dt_object_exists(obj) == 0) {
1933 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
1937 /* XXX: dangling name entry, will handle it in other patch. */
1941 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
1942 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
1945 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1946 MDS_INODELOCK_UPDATE |
1947 MDS_INODELOCK_XATTR, LCK_EX);
1951 handle = dt_trans_create(env, dev);
1953 GOTO(out, rc = PTR_ERR(handle));
1955 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
1959 rc = dt_trans_start(env, dev, handle);
1963 dt_write_lock(env, obj, 0);
1967 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
1971 rc = lfsck_links_read(env, obj, &ldata);
1973 count = ldata.ld_leh->leh_reccount;
1974 rc = linkea_links_find(&ldata, cname, pfid);
1976 (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
1979 ns->ln_flags |= LF_INCONSISTENT;
1980 /* For dir, if there are more than one linkea entries, or the
1981 * linkea entry does not match the name entry, then remove all
1982 * and add the correct one. */
1983 if (S_ISDIR(lfsck_object_type(obj))) {
1991 } else if (unlikely(rc == -EINVAL)) {
1993 ns->ln_flags |= LF_INCONSISTENT;
1994 /* The magic crashed, we are not sure whether there are more
1995 * corrupt data in the linkea, so remove all linkea entries. */
1999 } else if (rc == -ENODATA) {
2001 ns->ln_flags |= LF_UPGRADE;
2006 if (bk->lb_param & LPF_DRYRUN) {
2007 down_write(&com->lc_sem);
2008 ns->ln_linkea_repaired++;
2009 up_write(&com->lc_sem);
2015 if (!lustre_handle_is_used(&lh))
2021 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
2028 rc = linkea_data_new(&ldata,
2029 &lfsck_env_info(env)->lti_linkea_buf);
2034 rc = linkea_add_buf(&ldata, cname, pfid);
2038 rc = lfsck_links_write(env, obj, &ldata, handle);
2042 count = ldata.ld_leh->leh_reccount;
2043 down_write(&com->lc_sem);
2044 ns->ln_linkea_repaired++;
2045 up_write(&com->lc_sem);
2048 } else if (rc == -ENOENT) {
2060 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2065 (la->la_nlink == 1 || S_ISDIR(lfsck_object_type(obj))))
2066 /* Usually, it is for single linked object or dir, do nothing.*/
2069 /* Following modification will be in another transaction. */
2070 if (handle != NULL) {
2071 LASSERT(dt_write_locked(env, obj));
2073 dt_write_unlock(env, obj);
2076 dt_trans_stop(env, dev, handle);
2079 lfsck_ibits_unlock(&lh, LCK_EX);
2082 down_write(&com->lc_sem);
2083 ns->ln_mul_linked_checked++;
2084 up_write(&com->lc_sem);
2085 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
2086 LNTF_CHECK_LINKEA, true);
2092 dt_write_unlock(env, obj);
2094 if (handle != NULL && !IS_ERR(handle))
2095 dt_trans_stop(env, dev, handle);
2098 lfsck_ibits_unlock(&lh, LCK_EX);
2099 down_write(&com->lc_sem);
2101 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
2102 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
2103 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
2104 PFID(lfsck_dto2fid(lnr->lnr_obj)),
2105 lnr->lnr_namelen, lnr->lnr_name, rc);
2107 lfsck_namespace_record_failure(env, lfsck, ns);
2108 if (!(bk->lb_param & LPF_FAILOUT))
2112 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
2113 "repaired the entry: "DFID", parent "DFID
2114 ", name %.*s\n", lfsck_lfsck2name(lfsck),
2115 PFID(&lnr->lnr_fid),
2116 PFID(lfsck_dto2fid(lnr->lnr_obj)),
2117 lnr->lnr_namelen, lnr->lnr_name);
2120 ns->ln_items_repaired++;
2121 if (bk->lb_param & LPF_DRYRUN &&
2122 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
2123 lfsck_pos_fill(env, lfsck,
2124 &ns->ln_pos_first_inconsistent,
2129 up_write(&com->lc_sem);
2131 if (obj != NULL && !IS_ERR(obj))
2132 lfsck_object_put(env, obj);
2136 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
2137 struct lfsck_component *com)
2139 struct lfsck_instance *lfsck = com->lc_lfsck;
2140 struct ptlrpc_thread *thread = &lfsck->li_thread;
2141 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2142 struct lfsck_namespace *ns = com->lc_file_ram;
2143 struct dt_object *obj = com->lc_obj;
2144 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
2145 struct dt_object *target;
2153 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
2154 lfsck_lfsck2name(lfsck));
2156 com->lc_new_checked = 0;
2157 com->lc_new_scanned = 0;
2158 com->lc_time_last_checkpoint = cfs_time_current();
2159 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
2160 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2162 di = iops->init(env, obj, 0, BYPASS_CAPA);
2164 RETURN(PTR_ERR(di));
2166 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
2167 rc = iops->get(env, di, (const struct dt_key *)&fid);
2171 /* Skip the start one, which either has been processed or non-exist. */
2172 rc = iops->next(env, di);
2177 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
2179 struct l_wait_info lwi;
2181 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
2183 l_wait_event(thread->t_ctl_waitq,
2184 !thread_is_running(thread),
2187 if (unlikely(!thread_is_running(thread)))
2191 key = iops->key(env, di);
2192 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
2193 if (!fid_is_sane(&fid)) {
2198 target = lfsck_object_find(env, lfsck, &fid);
2199 if (IS_ERR(target)) {
2200 rc = PTR_ERR(target);
2204 if (dt_object_exists(target)) {
2205 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
2207 rc = lfsck_namespace_double_scan_one(env, com,
2214 lfsck_object_put(env, target);
2217 down_write(&com->lc_sem);
2218 com->lc_new_checked++;
2219 com->lc_new_scanned++;
2220 ns->ln_fid_latest_scanned_phase2 = fid;
2222 ns->ln_objs_repaired_phase2++;
2224 ns->ln_objs_failed_phase2++;
2225 up_write(&com->lc_sem);
2227 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
2230 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
2231 cfs_time_current())) &&
2232 com->lc_new_checked != 0) {
2233 down_write(&com->lc_sem);
2234 ns->ln_run_time_phase2 +=
2235 cfs_duration_sec(cfs_time_current() +
2236 HALF_SEC - com->lc_time_last_checkpoint);
2237 ns->ln_time_last_checkpoint = cfs_time_current_sec();
2238 ns->ln_objs_checked_phase2 += com->lc_new_checked;
2239 com->lc_new_checked = 0;
2240 rc = lfsck_namespace_store(env, com, false);
2241 up_write(&com->lc_sem);
2245 com->lc_time_last_checkpoint = cfs_time_current();
2246 com->lc_time_next_checkpoint =
2247 com->lc_time_last_checkpoint +
2248 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
2251 lfsck_control_speed_by_self(com);
2252 if (unlikely(!thread_is_running(thread)))
2255 rc = iops->next(env, di);
2264 iops->fini(env, di);
2266 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
2267 lfsck_lfsck2name(lfsck), rc);
2272 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
2273 struct lfsck_component *com,
2274 struct lfsck_position *pos)
2276 struct lfsck_assistant_data *lad = com->lc_data;
2277 struct lfsck_namespace_req *lnr;
2279 if (list_empty(&lad->lad_req_list))
2282 lnr = list_entry(lad->lad_req_list.next,
2283 struct lfsck_namespace_req,
2285 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
2286 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
2287 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
2290 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
2291 struct lfsck_component *com,
2294 struct lfsck_instance *lfsck = com->lc_lfsck;
2295 struct lfsck_namespace *ns = com->lc_file_ram;
2297 down_write(&com->lc_sem);
2298 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
2299 HALF_SEC - lfsck->li_time_last_checkpoint);
2300 ns->ln_time_last_checkpoint = cfs_time_current_sec();
2301 ns->ln_objs_checked_phase2 += com->lc_new_checked;
2302 com->lc_new_checked = 0;
2305 if (ns->ln_flags & LF_INCOMPLETE)
2306 ns->ln_status = LS_PARTIAL;
2308 ns->ln_status = LS_COMPLETED;
2309 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
2310 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
2311 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
2312 ns->ln_success_count++;
2313 } else if (rc == 0) {
2314 ns->ln_status = lfsck->li_status;
2315 if (ns->ln_status == 0)
2316 ns->ln_status = LS_STOPPED;
2318 ns->ln_status = LS_FAILED;
2321 rc = lfsck_namespace_store(env, com, false);
2322 up_write(&com->lc_sem);
2327 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
2328 struct lfsck_component *com,
2329 struct lfsck_request *lr)
2334 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
2335 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
2336 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
2337 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
2338 .la_double_scan_result = lfsck_namespace_double_scan_result,
2339 .la_req_fini = lfsck_namespace_assistant_req_fini,
2340 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
2344 * Verify the specified linkEA entry for the given directory object.
2345 * If the object has no such linkEA entry or it has more other linkEA
2346 * entries, then re-generate the linkEA with the given information.
2348 * \param[in] env pointer to the thread context
2349 * \param[in] dev pointer to the dt_device
2350 * \param[in] obj pointer to the dt_object to be handled
2351 * \param[in] cname the name for the child in the parent directory
2352 * \param[in] pfid the parent directory's FID for the linkEA
2354 * \retval 0 for success
2355 * \retval negative error number on failure
2357 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
2358 struct dt_object *obj, const struct lu_name *cname,
2359 const struct lu_fid *pfid)
2361 struct linkea_data ldata = { 0 };
2362 struct lu_buf linkea_buf;
2365 int fl = LU_XATTR_CREATE;
2369 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2371 rc = lfsck_links_read(env, obj, &ldata);
2372 if (rc == -ENODATA) {
2374 } else if (rc == 0) {
2375 fl = LU_XATTR_REPLACE;
2376 if (ldata.ld_leh->leh_reccount != 1) {
2379 rc = linkea_links_find(&ldata, cname, pfid);
2388 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
2392 rc = linkea_add_buf(&ldata, cname, pfid);
2396 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2397 ldata.ld_leh->leh_len);
2398 th = dt_trans_create(env, dev);
2400 RETURN(PTR_ERR(th));
2402 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2403 XATTR_NAME_LINK, fl, th);
2407 rc = dt_trans_start_local(env, dev, th);
2411 dt_write_lock(env, obj, 0);
2412 rc = dt_xattr_set(env, obj, &linkea_buf,
2413 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
2414 dt_write_unlock(env, obj);
2419 dt_trans_stop(env, dev, th);
2424 * Get the name and parent directory's FID from the first linkEA entry.
2426 * \param[in] env pointer to the thread context
2427 * \param[in] obj pointer to the object which get linkEA from
2428 * \param[out] name pointer to the buffer to hold the name
2429 * in the first linkEA entry
2430 * \param[out] pfid pointer to the buffer to hold the parent
2431 * directory's FID in the first linkEA entry
2433 * \retval 0 for success
2434 * \retval negative error number on failure
2436 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
2437 char *name, struct lu_fid *pfid)
2439 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
2440 struct linkea_data ldata = { 0 };
2443 rc = lfsck_links_read(env, obj, &ldata);
2447 linkea_first_entry(&ldata);
2448 if (ldata.ld_lee == NULL)
2451 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
2452 /* To guarantee the 'name' is terminated with '0'. */
2453 memcpy(name, cname->ln_name, cname->ln_namelen);
2454 name[cname->ln_namelen] = 0;
2460 * Remove the name entry from the parent directory.
2462 * No need to care about the object referenced by the name entry,
2463 * either the name entry is invalid or redundant, or the referenced
2464 * object has been processed has been or will be handled by others.
2466 * \param[in] env pointer to the thread context
2467 * \param[in] lfsck pointer to the lfsck instance
2468 * \param[in] parent pointer to the lost+found object
2469 * \param[in] name the name for the name entry to be removed
2470 * \param[in] type the type for the name entry to be removed
2472 * \retval 0 for success
2473 * \retval negative error number on failure
2475 int lfsck_remove_name_entry(const struct lu_env *env,
2476 struct lfsck_instance *lfsck,
2477 struct dt_object *parent,
2478 const char *name, __u32 type)
2480 struct dt_device *dev = lfsck->li_next;
2482 struct lustre_handle lh = { 0 };
2486 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2487 MDS_INODELOCK_UPDATE, LCK_EX);
2491 th = dt_trans_create(env, dev);
2493 GOTO(unlock, rc = PTR_ERR(th));
2495 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2499 if (S_ISDIR(type)) {
2500 rc = dt_declare_ref_del(env, parent, th);
2505 rc = dt_trans_start(env, dev, th);
2509 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2514 if (S_ISDIR(type)) {
2515 dt_write_lock(env, parent, 0);
2516 rc = dt_ref_del(env, parent, th);
2517 dt_write_unlock(env, parent);
2523 dt_trans_stop(env, dev, th);
2526 lfsck_ibits_unlock(&lh, LCK_EX);
2528 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
2529 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
2530 PFID(lfsck_dto2fid(parent)), name, type, rc);
2536 * Update the object's name entry with the given FID.
2538 * \param[in] env pointer to the thread context
2539 * \param[in] lfsck pointer to the lfsck instance
2540 * \param[in] parent pointer to the parent directory that holds
2542 * \param[in] name the name for the entry to be updated
2543 * \param[in] pfid the new PFID for the name entry
2544 * \param[in] type the type for the name entry to be updated
2546 * \retval 0 for success
2547 * \retval negative error number on failure
2549 int lfsck_update_name_entry(const struct lu_env *env,
2550 struct lfsck_instance *lfsck,
2551 struct dt_object *parent, const char *name,
2552 const struct lu_fid *pfid, __u32 type)
2554 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
2555 struct dt_device *dev = lfsck->li_next;
2556 struct lustre_handle lh = { 0 };
2562 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2563 MDS_INODELOCK_UPDATE, LCK_EX);
2567 th = dt_trans_create(env, dev);
2569 GOTO(unlock, rc = PTR_ERR(th));
2571 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2575 rec->rec_type = type;
2576 rec->rec_fid = pfid;
2577 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
2578 (const struct dt_key *)name, th);
2582 rc = dt_declare_ref_add(env, parent, th);
2586 rc = dt_trans_start(env, dev, th);
2590 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2592 if (rc == -ENOENT) {
2600 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
2601 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
2602 if (rc == 0 && S_ISDIR(type) && !exists) {
2603 dt_write_lock(env, parent, 0);
2604 rc = dt_ref_add(env, parent, th);
2605 dt_write_unlock(env, parent);
2611 dt_trans_stop(env, dev, th);
2614 lfsck_ibits_unlock(&lh, LCK_EX);
2616 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
2617 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
2618 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
2623 int lfsck_namespace_setup(const struct lu_env *env,
2624 struct lfsck_instance *lfsck)
2626 struct lfsck_component *com;
2627 struct lfsck_namespace *ns;
2628 struct dt_object *root = NULL;
2629 struct dt_object *obj;
2633 LASSERT(lfsck->li_master);
2639 INIT_LIST_HEAD(&com->lc_link);
2640 INIT_LIST_HEAD(&com->lc_link_dir);
2641 init_rwsem(&com->lc_sem);
2642 atomic_set(&com->lc_ref, 1);
2643 com->lc_lfsck = lfsck;
2644 com->lc_type = LFSCK_TYPE_NAMESPACE;
2645 com->lc_ops = &lfsck_namespace_ops;
2646 com->lc_data = lfsck_assistant_data_init(
2647 &lfsck_namespace_assistant_ops,
2649 if (com->lc_data == NULL)
2650 GOTO(out, rc = -ENOMEM);
2652 com->lc_file_size = sizeof(struct lfsck_namespace);
2653 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
2654 if (com->lc_file_ram == NULL)
2655 GOTO(out, rc = -ENOMEM);
2657 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
2658 if (com->lc_file_disk == NULL)
2659 GOTO(out, rc = -ENOMEM);
2661 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
2663 GOTO(out, rc = PTR_ERR(root));
2665 if (unlikely(!dt_try_as_dir(env, root)))
2666 GOTO(out, rc = -ENOTDIR);
2668 obj = local_index_find_or_create(env, lfsck->li_los, root,
2669 lfsck_namespace_name,
2670 S_IFREG | S_IRUGO | S_IWUSR,
2671 &dt_lfsck_features);
2673 GOTO(out, rc = PTR_ERR(obj));
2676 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
2680 rc = lfsck_namespace_load(env, com);
2682 rc = lfsck_namespace_reset(env, com, true);
2683 else if (rc == -ENODATA)
2684 rc = lfsck_namespace_init(env, com);
2688 ns = com->lc_file_ram;
2689 switch (ns->ln_status) {
2694 spin_lock(&lfsck->li_lock);
2695 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
2696 spin_unlock(&lfsck->li_lock);
2699 CERROR("%s: unknown lfsck_namespace status %d\n",
2700 lfsck_lfsck2name(lfsck), ns->ln_status);
2702 case LS_SCANNING_PHASE1:
2703 case LS_SCANNING_PHASE2:
2704 /* No need to store the status to disk right now.
2705 * If the system crashed before the status stored,
2706 * it will be loaded back when next time. */
2707 ns->ln_status = LS_CRASHED;
2711 spin_lock(&lfsck->li_lock);
2712 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
2713 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
2714 spin_unlock(&lfsck->li_lock);
2721 if (root != NULL && !IS_ERR(root))
2722 lu_object_put(env, &root->do_lu);
2724 lfsck_component_cleanup(env, com);
2725 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
2726 lfsck_lfsck2name(lfsck), rc);