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 static struct lfsck_namespace_req *
55 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
56 struct lu_dirent *ent, __u16 type)
58 struct lfsck_namespace_req *lnr;
61 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
64 return ERR_PTR(-ENOMEM);
66 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
67 lu_object_get(&lfsck->li_obj_dir->do_lu);
68 lnr->lnr_obj = lfsck->li_obj_dir;
69 lnr->lnr_fid = ent->lde_fid;
70 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
71 lnr->lnr_dir_cookie = ent->lde_hash;
72 lnr->lnr_attr = ent->lde_attrs;
75 lnr->lnr_namelen = ent->lde_namelen;
76 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
81 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
82 struct lfsck_assistant_req *lar)
84 struct lfsck_namespace_req *lnr =
85 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
87 lu_object_put(env, &lnr->lnr_obj->do_lu);
88 OBD_FREE(lnr, lnr->lnr_size);
91 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
92 struct lfsck_namespace *src)
94 dst->ln_magic = le32_to_cpu(src->ln_magic);
95 dst->ln_status = le32_to_cpu(src->ln_status);
96 dst->ln_flags = le32_to_cpu(src->ln_flags);
97 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
98 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
99 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
100 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
101 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
102 dst->ln_time_last_checkpoint =
103 le64_to_cpu(src->ln_time_last_checkpoint);
104 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
105 &src->ln_pos_latest_start);
106 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
107 &src->ln_pos_last_checkpoint);
108 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
109 &src->ln_pos_first_inconsistent);
110 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
111 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
112 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
113 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
114 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
115 dst->ln_objs_repaired_phase2 =
116 le64_to_cpu(src->ln_objs_repaired_phase2);
117 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
118 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
119 dst->ln_objs_lost_found = le64_to_cpu(src->ln_objs_lost_found);
120 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
121 &src->ln_fid_latest_scanned_phase2);
122 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
123 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
124 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
125 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
126 dst->ln_unknown_inconsistency =
127 le64_to_cpu(src->ln_unknown_inconsistency);
128 dst->ln_unmatched_pairs_repaired =
129 le64_to_cpu(src->ln_unmatched_pairs_repaired);
130 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
131 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
132 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
135 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
136 struct lfsck_namespace *src)
138 dst->ln_magic = cpu_to_le32(src->ln_magic);
139 dst->ln_status = cpu_to_le32(src->ln_status);
140 dst->ln_flags = cpu_to_le32(src->ln_flags);
141 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
142 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
143 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
144 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
145 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
146 dst->ln_time_last_checkpoint =
147 cpu_to_le64(src->ln_time_last_checkpoint);
148 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
149 &src->ln_pos_latest_start);
150 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
151 &src->ln_pos_last_checkpoint);
152 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
153 &src->ln_pos_first_inconsistent);
154 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
155 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
156 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
157 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
158 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
159 dst->ln_objs_repaired_phase2 =
160 cpu_to_le64(src->ln_objs_repaired_phase2);
161 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
162 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
163 dst->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
164 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
165 &src->ln_fid_latest_scanned_phase2);
166 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
167 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
168 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
169 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
170 dst->ln_unknown_inconsistency =
171 cpu_to_le64(src->ln_unknown_inconsistency);
172 dst->ln_unmatched_pairs_repaired =
173 cpu_to_le64(src->ln_unmatched_pairs_repaired);
174 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
175 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
176 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
179 static void lfsck_namespace_record_failure(const struct lu_env *env,
180 struct lfsck_instance *lfsck,
181 struct lfsck_namespace *ns)
183 struct lfsck_position pos;
185 ns->ln_items_failed++;
186 lfsck_pos_fill(env, lfsck, &pos, false);
187 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
188 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
189 ns->ln_pos_first_inconsistent = pos;
191 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
192 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
193 lfsck_lfsck2name(lfsck),
194 ns->ln_pos_first_inconsistent.lp_oit_cookie,
195 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
196 ns->ln_pos_first_inconsistent.lp_dir_cookie);
201 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
202 * \retval 0: succeed.
203 * \retval -ve: failed cases.
205 static int lfsck_namespace_load(const struct lu_env *env,
206 struct lfsck_component *com)
208 int len = com->lc_file_size;
211 rc = dt_xattr_get(env, com->lc_obj,
212 lfsck_buf_get(env, com->lc_file_disk, len),
213 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
215 struct lfsck_namespace *ns = com->lc_file_ram;
217 lfsck_namespace_le_to_cpu(ns,
218 (struct lfsck_namespace *)com->lc_file_disk);
219 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
220 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
221 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
222 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
227 } else if (rc != -ENODATA) {
228 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
229 "expected = %d: rc = %d\n",
230 lfsck_lfsck2name(com->lc_lfsck), len, rc);
237 static int lfsck_namespace_store(const struct lu_env *env,
238 struct lfsck_component *com, bool init)
240 struct dt_object *obj = com->lc_obj;
241 struct lfsck_instance *lfsck = com->lc_lfsck;
242 struct thandle *handle;
243 int len = com->lc_file_size;
247 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
248 (struct lfsck_namespace *)com->lc_file_ram);
249 handle = dt_trans_create(env, lfsck->li_bottom);
251 GOTO(log, rc = PTR_ERR(handle));
253 rc = dt_declare_xattr_set(env, obj,
254 lfsck_buf_get(env, com->lc_file_disk, len),
255 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
259 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
263 rc = dt_xattr_set(env, obj,
264 lfsck_buf_get(env, com->lc_file_disk, len),
265 XATTR_NAME_LFSCK_NAMESPACE,
266 init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
267 handle, BYPASS_CAPA);
272 dt_trans_stop(env, lfsck->li_bottom, handle);
276 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
277 lfsck_lfsck2name(lfsck), rc);
281 static int lfsck_namespace_init(const struct lu_env *env,
282 struct lfsck_component *com)
284 struct lfsck_namespace *ns = com->lc_file_ram;
287 memset(ns, 0, sizeof(*ns));
288 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
289 ns->ln_status = LS_INIT;
290 down_write(&com->lc_sem);
291 rc = lfsck_namespace_store(env, com, true);
292 up_write(&com->lc_sem);
297 * Update the namespace LFSCK tracing file for the given @fid
299 * \param[in] env pointer to the thread context
300 * \param[in] com pointer to the lfsck component
301 * \param[in] fid the fid which flags to be updated in the lfsck
303 * \param[in] add true if add new flags, otherwise remove flags
305 * \retval 0 for succeed or nothing to be done
306 * \retval negative error number on failure
308 int lfsck_namespace_trace_update(const struct lu_env *env,
309 struct lfsck_component *com,
310 const struct lu_fid *fid,
311 const __u8 flags, bool add)
313 struct lfsck_instance *lfsck = com->lc_lfsck;
314 struct dt_object *obj = com->lc_obj;
315 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
316 struct dt_device *dev = lfsck->li_bottom;
317 struct thandle *th = NULL;
325 down_write(&com->lc_sem);
326 fid_cpu_to_be(key, fid);
327 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
328 (const struct dt_key *)key, BYPASS_CAPA);
331 GOTO(unlock, rc = 0);
335 } else if (rc == 0) {
337 if ((old & flags) == flags)
338 GOTO(unlock, rc = 0);
342 if ((old & flags) == 0)
343 GOTO(unlock, rc = 0);
351 th = dt_trans_create(env, dev);
353 GOTO(log, rc = PTR_ERR(th));
356 rc = dt_declare_delete(env, obj,
357 (const struct dt_key *)key, th);
363 rc = dt_declare_insert(env, obj,
364 (const struct dt_rec *)&new,
365 (const struct dt_key *)key, th);
370 rc = dt_trans_start_local(env, dev, th);
375 rc = dt_delete(env, obj, (const struct dt_key *)key,
382 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
383 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
391 if (th != NULL && !IS_ERR(th))
392 dt_trans_stop(env, dev, th);
394 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
395 "tracing file, flags %x, old %x, new %x: rc = %d\n",
396 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
397 (__u32)flags, (__u32)old, (__u32)new, rc);
400 up_write(&com->lc_sem);
405 static int lfsck_namespace_check_exist(const struct lu_env *env,
406 struct dt_object *dir,
407 struct dt_object *obj, const char *name)
409 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
413 if (unlikely(lfsck_is_dead_obj(obj)))
414 RETURN(LFSCK_NAMEENTRY_DEAD);
416 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
417 (const struct dt_key *)name, BYPASS_CAPA);
419 RETURN(LFSCK_NAMEENTRY_REMOVED);
424 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
425 RETURN(LFSCK_NAMEENTRY_RECREATED);
430 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
431 struct dt_object *obj,
432 struct thandle *handle)
436 /* For destroying all invalid linkEA entries. */
437 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
441 /* For insert new linkEA entry. */
442 rc = dt_declare_xattr_set(env, obj,
443 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
444 XATTR_NAME_LINK, 0, handle);
448 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
449 struct linkea_data *ldata)
453 if (ldata->ld_buf->lb_buf == NULL)
456 if (!dt_object_exists(obj))
459 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
461 /* Buf was too small, figure out what we need. */
462 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
467 lu_buf_realloc(ldata->ld_buf, rc);
468 if (ldata->ld_buf->lb_buf == NULL)
471 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
476 rc = linkea_init(ldata);
482 * Remove linkEA for the given object.
484 * The caller should take the ldlm lock before the calling.
486 * \param[in] env pointer to the thread context
487 * \param[in] com pointer to the lfsck component
488 * \param[in] obj pointer to the dt_object to be handled
490 * \retval 0 for repaired cases
491 * \retval negative error number on failure
493 static int lfsck_namespace_links_remove(const struct lu_env *env,
494 struct lfsck_component *com,
495 struct dt_object *obj)
497 struct lfsck_instance *lfsck = com->lc_lfsck;
498 struct dt_device *dev = lfsck->li_bottom;
499 struct thandle *th = NULL;
503 LASSERT(dt_object_remote(obj) == 0);
505 th = dt_trans_create(env, dev);
507 GOTO(log, rc = PTR_ERR(th));
509 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
513 rc = dt_trans_start_local(env, dev, th);
517 dt_write_lock(env, obj, 0);
518 if (unlikely(lfsck_is_dead_obj(obj)))
519 GOTO(unlock, rc = -ENOENT);
521 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
522 GOTO(unlock, rc = 0);
524 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
529 dt_write_unlock(env, obj);
532 dt_trans_stop(env, dev, th);
535 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
536 "for the object "DFID": rc = %d\n",
537 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
540 struct lfsck_namespace *ns = com->lc_file_ram;
542 ns->ln_flags |= LF_INCONSISTENT;
548 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
549 struct linkea_data *ldata, struct thandle *handle)
551 const struct lu_buf *buf = lfsck_buf_get_const(env,
552 ldata->ld_buf->lb_buf,
553 ldata->ld_leh->leh_len);
555 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
559 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
560 struct lu_name *cname,
564 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
565 /* To guarantee the 'name' is terminated with '0'. */
566 memcpy(buf, cname->ln_name, cname->ln_namelen);
567 buf[cname->ln_namelen] = 0;
568 cname->ln_name = buf;
571 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
572 struct lu_name *cname,
576 struct link_ea_entry *oldlee;
580 oldlee = ldata->ld_lee;
581 oldlen = ldata->ld_reclen;
582 linkea_next_entry(ldata);
583 while (ldata->ld_lee != NULL) {
584 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
585 ldata->ld_lee->lee_reclen[1];
586 if (unlikely(ldata->ld_reclen == oldlen &&
587 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
592 linkea_del_buf(ldata, cname);
594 linkea_next_entry(ldata);
597 ldata->ld_lee = oldlee;
598 ldata->ld_reclen = oldlen;
604 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
606 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
607 * with the given type to generate the name, the detailed rules for name
608 * have been described as following.
610 * The function also generates the linkEA corresponding to the name entry
611 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
613 * \param[in] env pointer to the thread context
614 * \param[in] com pointer to the lfsck component
615 * \param[in] orphan pointer to the orphan MDT-object
616 * \param[in] infix additional information for the orphan name, such as
617 * the FID for original
618 * \param[in] type the type for describing why the orphan MDT-object is
619 * created. The rules are as following:
621 * type "D": The MDT-object is a directory, it may knows its parent
622 * but because there is no valid linkEA, the LFSCK cannot
623 * know where to put it back to the namespace.
624 * type "O": The MDT-object has no linkEA, and there is no name
625 * entry that references the MDT-object.
627 * \see lfsck_layout_recreate_parent() for more types.
629 * The orphan name will be like:
630 * ${FID}-${infix}-${type}-${conflict_version}
632 * \param[out] count if some others inserted some linkEA entries by race,
633 * then return the linkEA entries count.
635 * \retval positive number for repaired cases
636 * \retval 0 if needs to repair nothing
637 * \retval negative error number on failure
639 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
640 struct lfsck_component *com,
641 struct dt_object *orphan,
642 const char *infix, const char *type,
645 struct lfsck_thread_info *info = lfsck_env_info(env);
646 struct lu_name *cname = &info->lti_name;
647 struct dt_insert_rec *rec = &info->lti_dt_rec;
648 struct lu_fid *tfid = &info->lti_fid5;
649 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
650 const struct lu_fid *pfid;
651 struct lfsck_instance *lfsck = com->lc_lfsck;
652 struct dt_device *dev = lfsck->li_bottom;
653 struct dt_object *parent;
654 struct thandle *th = NULL;
655 struct lustre_handle plh = { 0 };
656 struct lustre_handle clh = { 0 };
657 struct linkea_data ldata = { 0 };
658 struct lu_buf linkea_buf;
665 cname->ln_name = NULL;
666 /* Create .lustre/lost+found/MDTxxxx when needed. */
667 if (unlikely(lfsck->li_lpf_obj == NULL)) {
668 rc = lfsck_create_lpf(env, lfsck);
673 parent = lfsck->li_lpf_obj;
674 pfid = lfsck_dto2fid(parent);
676 /* Hold update lock on the parent to prevent others to access. */
677 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
678 MDS_INODELOCK_UPDATE, LCK_EX);
683 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
684 PFID(cfid), infix, type, idx++);
685 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
686 (const struct dt_key *)info->lti_key,
688 if (rc != 0 && rc != -ENOENT)
691 if (unlikely(rc == 0 && lu_fid_eq(cfid, tfid)))
693 } while (rc == 0 && !exist);
695 cname->ln_name = info->lti_key;
696 cname->ln_namelen = namelen;
697 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
701 rc = linkea_add_buf(&ldata, cname, pfid);
705 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
706 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
711 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
712 ldata.ld_leh->leh_len);
713 th = dt_trans_create(env, dev);
715 GOTO(log, rc = PTR_ERR(th));
717 if (S_ISDIR(lfsck_object_type(orphan))) {
718 rc = dt_declare_delete(env, orphan,
719 (const struct dt_key *)dotdot, th);
723 rec->rec_type = S_IFDIR;
725 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
726 (const struct dt_key *)dotdot, th);
731 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
732 XATTR_NAME_LINK, 0, th);
737 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
739 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
740 (const struct dt_key *)cname->ln_name,
745 if (S_ISDIR(rec->rec_type)) {
746 rc = dt_declare_ref_add(env, parent, th);
752 rc = dt_trans_start_local(env, dev, th);
756 dt_write_lock(env, orphan, 0);
757 rc = lfsck_links_read(env, orphan, &ldata);
758 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
759 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
760 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
761 GOTO(unlock, rc = 1);
763 if (S_ISDIR(lfsck_object_type(orphan))) {
764 rc = dt_delete(env, orphan,
765 (const struct dt_key *)dotdot, th,
770 rec->rec_type = S_IFDIR;
772 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
773 (const struct dt_key *)dotdot, th,
779 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
782 if (rc == 0 && count != NULL)
783 *count = ldata.ld_leh->leh_reccount;
787 dt_write_unlock(env, orphan);
789 if (rc == 0 && !exist) {
790 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
792 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
793 (const struct dt_key *)cname->ln_name,
795 if (rc == 0 && S_ISDIR(rec->rec_type)) {
796 dt_write_lock(env, parent, 0);
797 rc = dt_ref_add(env, parent, th);
798 dt_write_unlock(env, parent);
802 GOTO(stop, rc = (rc == 0 ? 1 : rc));
805 dt_write_unlock(env, orphan);
808 dt_trans_stop(env, dev, th);
811 lfsck_ibits_unlock(&clh, LCK_EX);
812 lfsck_ibits_unlock(&plh, LCK_EX);
813 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
814 "object "DFID", name = %s: rc = %d\n",
815 lfsck_lfsck2name(lfsck), PFID(cfid),
816 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
819 struct lfsck_namespace *ns = com->lc_file_ram;
821 ns->ln_flags |= LF_INCONSISTENT;
827 static int lfsck_namespace_insert_normal(const struct lu_env *env,
828 struct lfsck_component *com,
829 struct dt_object *parent,
830 struct dt_object *child,
837 static int lfsck_namespace_create_orphan(const struct lu_env *env,
838 struct lfsck_component *com,
839 struct dt_object *orphan)
846 * Remove the specified entry from the linkEA.
848 * Locate the linkEA entry with the given @cname and @pfid, then
849 * remove this entry or the other entries those are repeated with
852 * \param[in] env pointer to the thread context
853 * \param[in] com pointer to the lfsck component
854 * \param[in] obj pointer to the dt_object to be handled
855 * \param[in,out]ldata pointer to the buffer that holds the linkEA
856 * \param[in] cname the name for the child in the parent directory
857 * \param[in] pfid the parent directory's FID for the linkEA
858 * \param[in] next if true, then remove the first found linkEA
859 * entry, and move the ldata->ld_lee to next entry
861 * \retval positive number for repaired cases
862 * \retval 0 if nothing to be repaired
863 * \retval negative error number on failure
865 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
866 struct lfsck_component *com,
867 struct dt_object *obj,
868 struct linkea_data *ldata,
869 struct lu_name *cname,
873 struct lfsck_instance *lfsck = com->lc_lfsck;
874 struct dt_device *dev = lfsck->li_bottom;
875 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
876 struct thandle *th = NULL;
877 struct lustre_handle lh = { 0 };
878 struct linkea_data ldata_new = { 0 };
879 struct lu_buf linkea_buf;
883 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
884 MDS_INODELOCK_UPDATE |
885 MDS_INODELOCK_XATTR, LCK_EX);
890 linkea_del_buf(ldata, cname);
892 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
894 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
895 ldata->ld_leh->leh_len);
898 th = dt_trans_create(env, dev);
900 GOTO(unlock1, rc = PTR_ERR(th));
902 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
903 XATTR_NAME_LINK, 0, th);
907 rc = dt_trans_start_local(env, dev, th);
911 dt_write_lock(env, obj, 0);
912 if (unlikely(lfsck_is_dead_obj(obj)))
913 GOTO(unlock2, rc = -ENOENT);
915 rc = lfsck_links_read2(env, obj, &ldata_new);
919 /* The specified linkEA entry has been removed by race. */
920 rc = linkea_links_find(&ldata_new, cname, pfid);
922 GOTO(unlock2, rc = 0);
924 if (bk->lb_param & LPF_DRYRUN)
925 GOTO(unlock2, rc = 1);
928 linkea_del_buf(&ldata_new, cname);
930 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
933 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
934 dt_write_unlock(env, obj);
935 dt_trans_stop(env, dev, th);
936 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
937 ldata_new.ld_leh->leh_len);
941 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
942 ldata_new.ld_leh->leh_len);
943 rc = dt_xattr_set(env, obj, &linkea_buf,
944 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
946 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
949 dt_write_unlock(env, obj);
952 dt_trans_stop(env, dev, th);
955 lfsck_ibits_unlock(&lh, LCK_EX);
958 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
959 "for the object: "DFID", parent "DFID", name %.*s\n",
960 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
961 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
965 struct lfsck_namespace *ns = com->lc_file_ram;
967 ns->ln_flags |= LF_INCONSISTENT;
974 * Conditionally remove the specified entry from the linkEA.
976 * Take the parent lock firstly, then check whether the specified
977 * name entry exists or not: if yes, do nothing; otherwise, call
978 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
980 * \param[in] env pointer to the thread context
981 * \param[in] com pointer to the lfsck component
982 * \param[in] parent pointer to the parent directory
983 * \param[in] child pointer to the child object that holds the linkEA
984 * \param[in,out]ldata pointer to the buffer that holds the linkEA
985 * \param[in] cname the name for the child in the parent directory
986 * \param[in] pfid the parent directory's FID for the linkEA
988 * \retval positive number for repaired cases
989 * \retval 0 if nothing to be repaired
990 * \retval negative error number on failure
992 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
993 struct lfsck_component *com,
994 struct dt_object *parent,
995 struct dt_object *child,
996 struct linkea_data *ldata,
997 struct lu_name *cname,
1000 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1001 struct lustre_handle lh = { 0 };
1005 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1006 MDS_INODELOCK_UPDATE, LCK_EX);
1010 dt_read_lock(env, parent, 0);
1011 if (unlikely(lfsck_is_dead_obj(parent))) {
1012 dt_read_unlock(env, parent);
1013 lfsck_ibits_unlock(&lh, LCK_EX);
1014 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1020 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1021 (const struct dt_key *)cname->ln_name,
1023 dt_read_unlock(env, parent);
1025 /* It is safe to release the ldlm lock, because when the logic come
1026 * here, we have got all the needed information above whether the
1027 * linkEA entry is valid or not. It is not important that others
1028 * may add new linkEA entry after the ldlm lock released. If other
1029 * has removed the specified linkEA entry by race, then it is OK,
1030 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1032 lfsck_ibits_unlock(&lh, LCK_EX);
1033 if (rc == -ENOENT) {
1034 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1043 /* The LFSCK just found some internal status of cross-MDTs
1044 * create operation. That is normal. */
1045 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1046 linkea_next_entry(ldata);
1051 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1058 * Conditionally replace name entry in the parent.
1060 * As required, the LFSCK may re-create the lost MDT-object for dangling
1061 * name entry, but such repairing may be wrong because of bad FID in the
1062 * name entry. As the LFSCK processing, the real MDT-object may be found,
1063 * then the LFSCK should check whether the former re-created MDT-object
1064 * has been modified or not, if not, then destroy it and update the name
1065 * entry in the parent to reference the real MDT-object.
1067 * \param[in] env pointer to the thread context
1068 * \param[in] com pointer to the lfsck component
1069 * \param[in] parent pointer to the parent directory
1070 * \param[in] child pointer to the MDT-object that may be the real
1071 * MDT-object corresponding to the name entry in parent
1072 * \param[in] cfid the current FID in the name entry
1073 * \param[in] cname contains the name of the child in the parent directory
1075 * \retval positive number for repaired cases
1076 * \retval 0 if nothing to be repaired
1077 * \retval negative error number on failure
1079 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1080 struct lfsck_component *com,
1081 struct dt_object *parent,
1082 struct dt_object *child,
1083 const struct lu_fid *cfid,
1084 const struct lu_name *cname)
1086 struct lfsck_thread_info *info = lfsck_env_info(env);
1087 struct lu_fid *tfid = &info->lti_fid5;
1088 struct lu_attr *la = &info->lti_la;
1089 struct dt_insert_rec *rec = &info->lti_dt_rec;
1090 struct lfsck_instance *lfsck = com->lc_lfsck;
1091 struct dt_device *dev = lfsck->li_next;
1092 const char *name = cname->ln_name;
1093 struct dt_object *obj = NULL;
1094 struct lustre_handle plh = { 0 };
1095 struct lustre_handle clh = { 0 };
1096 struct linkea_data ldata = { 0 };
1097 struct thandle *th = NULL;
1102 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1103 MDS_INODELOCK_UPDATE, LCK_EX);
1107 if (!fid_is_sane(cfid)) {
1112 obj = lfsck_object_find(env, lfsck, cfid);
1115 if (rc == -ENOENT) {
1123 if (!dt_object_exists(obj)) {
1128 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1129 (const struct dt_key *)name, BYPASS_CAPA);
1130 if (rc == -ENOENT) {
1138 /* Someone changed the name entry, cannot replace it. */
1139 if (!lu_fid_eq(cfid, tfid))
1142 /* lock the object to be destroyed. */
1143 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1144 MDS_INODELOCK_UPDATE |
1145 MDS_INODELOCK_XATTR, LCK_EX);
1149 if (unlikely(lfsck_is_dead_obj(obj))) {
1154 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1158 /* The object has been modified by other(s), or it is not created by
1159 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1160 if (la->la_ctime != 0)
1163 if (S_ISREG(la->la_mode)) {
1164 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1166 /* If someone has created related OST-object(s),
1168 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1169 GOTO(log, rc = (rc > 0 ? 0 : rc));
1173 dt_read_lock(env, child, 0);
1174 rc = lfsck_links_read2(env, child, &ldata);
1175 dt_read_unlock(env, child);
1177 /* Someone changed the child, no need to replace. */
1184 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1185 /* Someone moved the child, no need to replace. */
1189 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1192 th = dt_trans_create(env, dev);
1194 GOTO(log, rc = PTR_ERR(th));
1197 rc = dt_declare_destroy(env, obj, th);
1202 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1206 rec->rec_type = S_IFDIR;
1207 rec->rec_fid = lfsck_dto2fid(child);
1208 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1209 (const struct dt_key *)name, th);
1213 rc = dt_trans_start(env, dev, th);
1218 rc = dt_destroy(env, obj, th);
1223 /* The old name entry maybe not exist. */
1224 dt_delete(env, parent, (const struct dt_key *)name, th,
1227 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1228 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1230 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1233 dt_trans_stop(env, dev, th);
1236 lfsck_ibits_unlock(&clh, LCK_EX);
1237 lfsck_ibits_unlock(&plh, LCK_EX);
1238 if (obj != NULL && !IS_ERR(obj))
1239 lfsck_object_put(env, obj);
1241 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1242 "object "DFID" because of conflict with the object "DFID
1243 " under the parent "DFID" with name %s: rc = %d\n",
1244 lfsck_lfsck2name(lfsck), PFID(cfid),
1245 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1252 * Overwrite the linkEA for the object with the given ldata.
1254 * The caller should take the ldlm lock before the calling.
1256 * \param[in] env pointer to the thread context
1257 * \param[in] com pointer to the lfsck component
1258 * \param[in] obj pointer to the dt_object to be handled
1259 * \param[in] ldata pointer to the new linkEA data
1261 * \retval positive number for repaired cases
1262 * \retval 0 if nothing to be repaired
1263 * \retval negative error number on failure
1265 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1266 struct lfsck_component *com,
1267 struct dt_object *obj,
1268 struct linkea_data *ldata)
1270 struct lfsck_instance *lfsck = com->lc_lfsck;
1271 struct dt_device *dev = lfsck->li_bottom;
1272 struct thandle *th = NULL;
1273 struct lu_buf linkea_buf;
1277 LASSERT(!dt_object_remote(obj));
1279 th = dt_trans_create(env, dev);
1281 GOTO(log, rc = PTR_ERR(th));
1283 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1284 ldata->ld_leh->leh_len);
1285 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1286 XATTR_NAME_LINK, 0, th);
1290 rc = dt_trans_start_local(env, dev, th);
1294 dt_write_lock(env, obj, 0);
1295 if (unlikely(lfsck_is_dead_obj(obj)))
1296 GOTO(unlock, rc = 0);
1298 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1299 GOTO(unlock, rc = 1);
1301 rc = dt_xattr_set(env, obj, &linkea_buf,
1302 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1304 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1307 dt_write_unlock(env, obj);
1310 dt_trans_stop(env, dev, th);
1313 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1314 "object "DFID": rc = %d\n",
1315 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1318 struct lfsck_namespace *ns = com->lc_file_ram;
1320 ns->ln_flags |= LF_INCONSISTENT;
1327 * Repair invalid name entry.
1329 * If the name entry contains invalid information, such as bad file type
1330 * or (and) corrupted object FID, then either remove the name entry or
1331 * udpate the name entry with the given (right) information.
1333 * \param[in] env pointer to the thread context
1334 * \param[in] com pointer to the lfsck component
1335 * \param[in] parent pointer to the parent directory
1336 * \param[in] child pointer to the object referenced by the name entry
1337 * \param[in] name the old name of the child under the parent directory
1338 * \param[in] name2 the new name of the child under the parent directory
1339 * \param[in] type the type claimed by the name entry
1340 * \param[in] update update the name entry if true; otherwise, remove it
1341 * \param[in] dec decrease the parent nlink count if true
1343 * \retval positive number for repaired successfully
1344 * \retval 0 if nothing to be repaired
1345 * \retval negative error number on failure
1347 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1348 struct lfsck_component *com,
1349 struct dt_object *parent,
1350 struct dt_object *child,
1351 const char *name, const char *name2,
1352 __u16 type, bool update, bool dec)
1354 struct lfsck_thread_info *info = lfsck_env_info(env);
1355 struct dt_insert_rec *rec = &info->lti_dt_rec;
1356 const struct lu_fid *cfid = lfsck_dto2fid(child);
1357 struct lu_fid *tfid = &info->lti_fid5;
1358 struct lfsck_instance *lfsck = com->lc_lfsck;
1359 struct dt_device *dev = lfsck->li_next;
1360 struct thandle *th = NULL;
1361 struct lustre_handle lh = { 0 };
1365 if (unlikely(!dt_try_as_dir(env, parent)))
1366 GOTO(log, rc = -ENOTDIR);
1368 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1369 MDS_INODELOCK_UPDATE, LCK_EX);
1373 th = dt_trans_create(env, dev);
1375 GOTO(unlock1, rc = PTR_ERR(th));
1377 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1382 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1383 rec->rec_fid = cfid;
1384 rc = dt_declare_insert(env, parent,
1385 (const struct dt_rec *)rec,
1386 (const struct dt_key *)name2, th);
1392 rc = dt_declare_ref_del(env, parent, th);
1397 rc = dt_trans_start(env, dev, th);
1401 dt_write_lock(env, parent, 0);
1402 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1403 (const struct dt_key *)name, BYPASS_CAPA);
1404 /* Someone has removed the bad name entry by race. */
1406 GOTO(unlock2, rc = 0);
1411 /* Someone has removed the bad name entry and reuse it for other
1412 * object by race. */
1413 if (!lu_fid_eq(tfid, cfid))
1414 GOTO(unlock2, rc = 0);
1416 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1417 GOTO(unlock2, rc = 1);
1419 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
1425 rc = dt_insert(env, parent,
1426 (const struct dt_rec *)rec,
1427 (const struct dt_key *)name2, th,
1434 rc = dt_ref_del(env, parent, th);
1439 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1442 dt_write_unlock(env, parent);
1445 dt_trans_stop(env, dev, th);
1447 /* We are not sure whether the child will become orphan or not.
1448 * Record it in the LFSCK tracing file for further checking in
1449 * the second-stage scanning. */
1450 if (!update && !dec && rc == 0)
1451 lfsck_namespace_trace_update(env, com, cfid,
1452 LNTF_CHECK_LINKEA, true);
1455 lfsck_ibits_unlock(&lh, LCK_EX);
1458 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
1459 "entry for: parent "DFID", child "DFID", name %s, type "
1460 "in name entry %o, type claimed by child %o. repair it "
1461 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
1462 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
1463 name, type, update ? lfsck_object_type(child) : 0,
1464 update ? "updating" : "removing", name2, rc);
1467 struct lfsck_namespace *ns = com->lc_file_ram;
1469 ns->ln_flags |= LF_INCONSISTENT;
1476 * Update the ".." name entry for the given object.
1478 * The object's ".." is corrupted, this function will update the ".." name
1479 * entry with the given pfid, and the linkEA with the given ldata.
1481 * The caller should take the ldlm lock before the calling.
1483 * \param[in] env pointer to the thread context
1484 * \param[in] com pointer to the lfsck component
1485 * \param[in] obj pointer to the dt_object to be handled
1486 * \param[in] pfid the new fid for the object's ".." name entry
1487 * \param[in] cname the name for the @obj in the parent directory
1489 * \retval positive number for repaired cases
1490 * \retval 0 if nothing to be repaired
1491 * \retval negative error number on failure
1493 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
1494 struct lfsck_component *com,
1495 struct dt_object *obj,
1496 const struct lu_fid *pfid,
1497 struct lu_name *cname)
1499 struct lfsck_thread_info *info = lfsck_env_info(env);
1500 struct dt_insert_rec *rec = &info->lti_dt_rec;
1501 struct lfsck_instance *lfsck = com->lc_lfsck;
1502 struct dt_device *dev = lfsck->li_bottom;
1503 struct thandle *th = NULL;
1504 struct linkea_data ldata = { 0 };
1505 struct lu_buf linkea_buf;
1509 LASSERT(!dt_object_remote(obj));
1510 LASSERT(S_ISDIR(lfsck_object_type(obj)));
1512 rc = linkea_data_new(&ldata, &info->lti_big_buf);
1516 rc = linkea_add_buf(&ldata, cname, pfid);
1520 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1521 ldata.ld_leh->leh_len);
1523 th = dt_trans_create(env, dev);
1525 GOTO(log, rc = PTR_ERR(th));
1527 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
1531 rec->rec_type = S_IFDIR;
1532 rec->rec_fid = pfid;
1533 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
1534 (const struct dt_key *)dotdot, th);
1538 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1539 XATTR_NAME_LINK, 0, th);
1543 rc = dt_trans_start_local(env, dev, th);
1547 dt_write_lock(env, obj, 0);
1548 if (unlikely(lfsck_is_dead_obj(obj)))
1549 GOTO(unlock, rc = 0);
1551 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1552 GOTO(unlock, rc = 1);
1554 /* The old ".." name entry maybe not exist. */
1555 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
1558 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
1559 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
1563 rc = dt_xattr_set(env, obj, &linkea_buf,
1564 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1566 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1569 dt_write_unlock(env, obj);
1572 dt_trans_stop(env, dev, th);
1575 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
1576 "the object "DFID", new parent "DFID": rc = %d\n",
1577 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
1581 struct lfsck_namespace *ns = com->lc_file_ram;
1583 ns->ln_flags |= LF_INCONSISTENT;
1590 * Handle orphan @obj during Double Scan Directory.
1592 * Remove the @obj's current (invalid) linkEA entries, and insert
1593 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
1594 * ${FID}-${PFID}-D-${conflict_version}
1596 * The caller should take the ldlm lock before the calling.
1598 * \param[in] env pointer to the thread context
1599 * \param[in] com pointer to the lfsck component
1600 * \param[in] obj pointer to the orphan object to be handled
1601 * \param[in] pfid the new fid for the object's ".." name entry
1602 * \param[in,out] lh ldlm lock handler for the given @obj
1603 * \param[out] type to tell the caller what the inconsistency is
1605 * \retval positive number for repaired cases
1606 * \retval 0 if nothing to be repaired
1607 * \retval negative error number on failure
1610 lfsck_namespace_dsd_orphan(const struct lu_env *env,
1611 struct lfsck_component *com,
1612 struct dt_object *obj,
1613 const struct lu_fid *pfid,
1614 struct lustre_handle *lh,
1615 enum lfsck_namespace_inconsistency_type *type)
1617 struct lfsck_thread_info *info = lfsck_env_info(env);
1621 /* Remove the unrecognized linkEA. */
1622 rc = lfsck_namespace_links_remove(env, com, obj);
1623 lfsck_ibits_unlock(lh, LCK_EX);
1624 if (rc < 0 && rc != -ENODATA)
1627 *type = LNIT_MUL_REF;
1628 /* The unique linkEA is invalid, even if the ".." name entry may be
1629 * valid, we still cannot know via which name entry this directory
1630 * will be referenced. Then handle it as pure orphan. */
1631 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
1632 "-"DFID, PFID(pfid));
1633 rc = lfsck_namespace_insert_orphan(env, com, obj,
1634 info->lti_tmpbuf, "D", NULL);
1640 * Double Scan Directory object for single linkEA entry case.
1642 * The given @child has unique linkEA entry. If the linkEA entry is valid,
1643 * then check whether the name is in the namespace or not, if not, add the
1644 * missing name entry back to namespace. If the linkEA entry is invalid,
1645 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
1648 * \param[in] env pointer to the thread context
1649 * \param[in] com pointer to the lfsck component
1650 * \param[in] child pointer to the directory to be double scanned
1651 * \param[in] pfid the FID corresponding to the ".." entry
1652 * \param[in] ldata pointer to the linkEA data for the given @child
1653 * \param[in,out] lh ldlm lock handler for the given @child
1654 * \param[out] type to tell the caller what the inconsistency is
1655 * \param[in] retry if found inconsistency, but the caller does not hold
1656 * ldlm lock on the @child, then set @retry as true
1658 * \retval positive number for repaired cases
1659 * \retval 0 if nothing to be repaired
1660 * \retval negative error number on failure
1663 lfsck_namespace_dsd_single(const struct lu_env *env,
1664 struct lfsck_component *com,
1665 struct dt_object *child,
1666 const struct lu_fid *pfid,
1667 struct linkea_data *ldata,
1668 struct lustre_handle *lh,
1669 enum lfsck_namespace_inconsistency_type *type,
1672 struct lfsck_thread_info *info = lfsck_env_info(env);
1673 struct lu_name *cname = &info->lti_name;
1674 const struct lu_fid *cfid = lfsck_dto2fid(child);
1675 struct lu_fid *tfid = &info->lti_fid3;
1676 struct lfsck_instance *lfsck = com->lc_lfsck;
1677 struct dt_object *parent = NULL;
1681 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
1682 /* The unique linkEA entry with bad parent will be handled as orphan. */
1683 if (!fid_is_sane(tfid)) {
1684 if (!lustre_handle_is_used(lh) && retry != NULL)
1687 rc = lfsck_namespace_dsd_orphan(env, com, child,
1693 parent = lfsck_object_find_bottom(env, lfsck, tfid);
1695 GOTO(out, rc = PTR_ERR(parent));
1697 /* We trust the unique linkEA entry in spite of whether it matches the
1698 * ".." name entry or not. Because even if the linkEA entry is wrong
1699 * and the ".." name entry is right, we still cannot know via which
1700 * name entry the child will be referenced, since all known entries
1701 * have been verified during the first-stage scanning. */
1702 if (!dt_object_exists(parent)) {
1703 if (!lustre_handle_is_used(lh) && retry != NULL) {
1709 lfsck_ibits_unlock(lh, LCK_EX);
1710 /* Create the lost parent as an orphan. */
1711 rc = lfsck_namespace_create_orphan(env, com, parent);
1713 /* Add the missing name entry to the parent. */
1714 rc = lfsck_namespace_insert_normal(env, com, parent,
1715 child, cname->ln_name);
1720 /* The unique linkEA entry with bad parent will be handled as orphan. */
1721 if (unlikely(!dt_try_as_dir(env, parent))) {
1722 if (!lustre_handle_is_used(lh) && retry != NULL)
1725 rc = lfsck_namespace_dsd_orphan(env, com, child,
1731 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1732 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
1733 if (rc == -ENOENT) {
1734 if (!lustre_handle_is_used(lh) && retry != NULL) {
1740 lfsck_ibits_unlock(lh, LCK_EX);
1741 /* Add the missing name entry back to the namespace. */
1742 rc = lfsck_namespace_insert_normal(env, com, parent, child,
1751 if (!lu_fid_eq(tfid, cfid)) {
1752 if (!lustre_handle_is_used(lh) && retry != NULL) {
1758 lfsck_ibits_unlock(lh, LCK_EX);
1759 /* The name entry references another MDT-object that
1760 * may be created by the LFSCK for repairing dangling
1761 * name entry. Try to replace it. */
1762 rc = lfsck_namespace_replace_cond(env, com, parent, child,
1765 rc = lfsck_namespace_dsd_orphan(env, com, child,
1771 /* The ".." name entry is wrong, update it. */
1772 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
1773 if (!lustre_handle_is_used(lh) && retry != NULL) {
1779 *type = LNIT_UNMATCHED_PAIRS;
1780 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
1781 lfsck_dto2fid(parent), cname);
1787 if (parent != NULL && !IS_ERR(parent))
1788 lfsck_object_put(env, parent);
1794 * Double Scan Directory object for multiple linkEA entries case.
1796 * The given @child has multiple linkEA entries. There is at most one linkEA
1797 * entry will be valid, all the others will be removed. Firstly, the function
1798 * will try to find out the linkEA entry for which the name entry exists under
1799 * the given parent (@pfid). If there is no linkEA entry that matches the given
1800 * ".." name entry, then tries to find out the first linkEA entry that both the
1801 * parent and the name entry exist to rebuild a new ".." name entry.
1803 * \param[in] env pointer to the thread context
1804 * \param[in] com pointer to the lfsck component
1805 * \param[in] child pointer to the directory to be double scanned
1806 * \param[in] pfid the FID corresponding to the ".." entry
1807 * \param[in] ldata pointer to the linkEA data for the given @child
1808 * \param[in,out] lh ldlm lock handler for the given @child
1809 * \param[out] type to tell the caller what the inconsistency is
1810 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
1812 * \retval positive number for repaired cases
1813 * \retval 0 if nothing to be repaired
1814 * \retval negative error number on failure
1817 lfsck_namespace_dsd_multiple(const struct lu_env *env,
1818 struct lfsck_component *com,
1819 struct dt_object *child,
1820 const struct lu_fid *pfid,
1821 struct linkea_data *ldata,
1822 struct lustre_handle *lh,
1823 enum lfsck_namespace_inconsistency_type *type,
1826 struct lfsck_thread_info *info = lfsck_env_info(env);
1827 struct lu_name *cname = &info->lti_name;
1828 const struct lu_fid *cfid = lfsck_dto2fid(child);
1829 struct lu_fid *tfid = &info->lti_fid3;
1830 struct lu_fid *pfid2 = &info->lti_fid4;
1831 struct lfsck_namespace *ns = com->lc_file_ram;
1832 struct lfsck_instance *lfsck = com->lc_lfsck;
1833 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1834 struct dt_object *parent = NULL;
1835 struct linkea_data ldata_new = { 0 };
1842 while (ldata->ld_lee != NULL) {
1843 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
1845 /* Drop repeated linkEA entries. */
1846 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
1847 /* Drop invalid linkEA entry. */
1848 if (!fid_is_sane(tfid)) {
1849 linkea_del_buf(ldata, cname);
1853 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
1854 * then it is possible that: the directry object has ever
1855 * been lost, but its name entry was there. In the former
1856 * LFSCK run, during the first-stage scanning, the LFSCK
1857 * found the dangling name entry, but it did not recreate
1858 * the lost object, and when moved to the second-stage
1859 * scanning, some children objects of the lost directory
1860 * object were found, then the LFSCK recreated such lost
1861 * directory object as an orphan.
1863 * When the LFSCK runs again, if the dangling name is still
1864 * there, the LFSCK should move the orphan directory object
1865 * back to the normal namespace. */
1866 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
1867 linkea_next_entry(ldata);
1871 parent = lfsck_object_find_bottom(env, lfsck, tfid);
1873 RETURN(PTR_ERR(parent));
1875 if (!dt_object_exists(parent)) {
1876 lfsck_object_put(env, parent);
1877 if (ldata->ld_leh->leh_reccount > 1) {
1878 /* If it is NOT the last linkEA entry, then
1879 * there is still other chance to make the
1880 * child to be visible via other parent, then
1881 * remove this linkEA entry. */
1882 linkea_del_buf(ldata, cname);
1889 /* The linkEA entry with bad parent will be removed. */
1890 if (unlikely(!dt_try_as_dir(env, parent))) {
1891 lfsck_object_put(env, parent);
1892 linkea_del_buf(ldata, cname);
1896 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1897 (const struct dt_key *)cname->ln_name,
1899 *pfid2 = *lfsck_dto2fid(parent);
1900 if (rc == -ENOENT) {
1901 lfsck_object_put(env, parent);
1902 linkea_next_entry(ldata);
1907 lfsck_object_put(env, parent);
1912 if (lu_fid_eq(tfid, cfid)) {
1913 lfsck_object_put(env, parent);
1914 if (!lu_fid_eq(pfid, pfid2)) {
1915 *type = LNIT_UNMATCHED_PAIRS;
1916 rc = lfsck_namespace_repair_unmatched_pairs(env,
1917 com, child, pfid2, cname);
1923 /* It is the most common case that we find the
1924 * name entry corresponding to the linkEA entry
1925 * that matches the ".." name entry. */
1926 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
1930 rc = linkea_add_buf(&ldata_new, cname, pfid2);
1934 rc = lfsck_namespace_rebuild_linkea(env, com, child,
1939 linkea_del_buf(ldata, cname);
1940 linkea_first_entry(ldata);
1941 /* There may be some invalid dangling name entries under
1942 * other parent directories, remove all of them. */
1943 while (ldata->ld_lee != NULL) {
1944 lfsck_namespace_unpack_linkea_entry(ldata,
1945 cname, tfid, info->lti_key);
1946 if (!fid_is_sane(tfid))
1949 parent = lfsck_object_find_bottom(env, lfsck,
1951 if (IS_ERR(parent)) {
1952 rc = PTR_ERR(parent);
1953 if (rc != -ENOENT &&
1954 bk->lb_param & LPF_FAILOUT)
1960 if (!dt_object_exists(parent)) {
1961 lfsck_object_put(env, parent);
1965 rc = lfsck_namespace_repair_dirent(env, com,
1966 parent, child, cname->ln_name,
1967 cname->ln_name, S_IFDIR, false, true);
1968 lfsck_object_put(env, parent);
1970 if (bk->lb_param & LPF_FAILOUT)
1979 linkea_del_buf(ldata, cname);
1982 ns->ln_dirent_repaired += count;
1987 lfsck_ibits_unlock(lh, LCK_EX);
1988 /* The name entry references another MDT-object that may be
1989 * created by the LFSCK for repairing dangling name entry.
1990 * Try to replace it. */
1991 rc = lfsck_namespace_replace_cond(env, com, parent, child,
1993 lfsck_object_put(env, parent);
2000 linkea_del_buf(ldata, cname);
2003 if (ldata->ld_leh->leh_reccount == 1) {
2004 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2010 /* All linkEA entries are invalid and removed, then handle the @child
2012 if (ldata->ld_leh->leh_reccount == 0) {
2013 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2019 linkea_first_entry(ldata);
2020 /* If the dangling name entry for the orphan directory object has
2021 * been remvoed, then just check whether the directory object is
2022 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2028 /* There is no linkEA entry that matches the ".." name entry. Find
2029 * the first linkEA entry that both parent and name entry exist to
2030 * rebuild a new ".." name entry. */
2040 * Double scan the directory object for namespace LFSCK.
2042 * This function will verify the <parent, child> pairs in the namespace tree:
2043 * the parent references the child via some name entry that should be in the
2044 * child's linkEA entry, the child should back references the parent via its
2047 * The LFSCK will scan every linkEA entry in turn until find out the first
2048 * matched pairs. If found, then all other linkEA entries will be dropped.
2049 * If all the linkEA entries cannot match the ".." name entry, then there
2050 * are serveral possible cases:
2052 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2053 * in the linkEA entry is valid.
2055 * 2) If there are multiple linkEA entries, then try to find the linkEA
2056 * that matches the ".." name entry. If found, then all other entries
2057 * are invalid; otherwise, it is quite possible that the ".." name entry
2058 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2059 * entry according to the first valid linkEA entry (both the parent and
2060 * the name entry should exist).
2062 * 3) If the directory object has no (valid) linkEA entry, then the
2063 * directory object will be handled as pure orphan and inserted
2064 * in the .lustre/lost+found/MDTxxxx/ with the name:
2065 * ${self_FID}-${PFID}-D-${conflict_version}
2067 * \param[in] env pointer to the thread context
2068 * \param[in] com pointer to the lfsck component
2069 * \param[in] child pointer to the directory object to be handled
2070 * \param[in] flags to indicate the specical checking on the @child
2072 * \retval positive number for repaired cases
2073 * \retval 0 if nothing to be repaired
2074 * \retval negative error number on failure
2076 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
2077 struct lfsck_component *com,
2078 struct dt_object *child, __u8 flags)
2080 struct lfsck_thread_info *info = lfsck_env_info(env);
2081 const struct lu_fid *cfid = lfsck_dto2fid(child);
2082 struct lu_fid *pfid = &info->lti_fid2;
2083 struct lfsck_namespace *ns = com->lc_file_ram;
2084 struct lfsck_instance *lfsck = com->lc_lfsck;
2085 struct lustre_handle lh = { 0 };
2086 struct linkea_data ldata = { 0 };
2087 bool unknown = false;
2090 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
2094 LASSERT(!dt_object_remote(child));
2096 if (!(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
2097 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
2098 "the namespace LFSCK, then the LFSCK cannot guarantee"
2099 "all the name entries have been verified in first-stage"
2100 "scanning. So have to skip orphan related handling for"
2101 "the directory object "DFID" with remote name entry\n",
2102 lfsck_lfsck2name(lfsck), PFID(cfid));
2107 if (unlikely(!dt_try_as_dir(env, child)))
2108 GOTO(out, rc = -ENOTDIR);
2110 /* We only take ldlm lock on the @child when required. When the
2111 * logic comes here for the first time, it is always false. */
2115 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2116 MDS_INODELOCK_UPDATE |
2117 MDS_INODELOCK_XATTR, LCK_EX);
2122 dt_read_lock(env, child, 0);
2123 if (unlikely(lfsck_is_dead_obj(child))) {
2124 dt_read_unlock(env, child);
2129 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
2130 (const struct dt_key *)dotdot, BYPASS_CAPA);
2132 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
2133 dt_read_unlock(env, child);
2138 if (!lustre_handle_is_used(&lh)) {
2139 dt_read_unlock(env, child);
2144 } else if (lfsck->li_lpf_obj != NULL &&
2145 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
2149 rc = lfsck_links_read(env, child, &ldata);
2150 dt_read_unlock(env, child);
2152 if (rc != -ENODATA && rc != -EINVAL)
2155 if (!lustre_handle_is_used(&lh))
2158 if (rc == -EINVAL && !fid_is_zero(pfid)) {
2159 /* Remove the corrupted linkEA. */
2160 rc = lfsck_namespace_links_remove(env, com, child);
2162 /* Here, because of the crashed linkEA, we
2163 * cannot know whether there is some parent
2164 * that references the child directory via
2165 * some name entry or not. So keep it there,
2166 * when the LFSCK run next time, if there is
2167 * some parent that references this object,
2168 * then the LFSCK can rebuild the linkEA;
2169 * otherwise, this object will be handled
2170 * as orphan as above. */
2173 /* 1. If we have neither ".." nor linkEA,
2174 * then it is an orphan.
2176 * 2. If we only have the ".." name entry,
2177 * but no parent references this child
2178 * directory, then handle it as orphan. */
2179 lfsck_ibits_unlock(&lh, LCK_EX);
2180 type = LNIT_MUL_REF;
2181 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2182 "-"DFID, PFID(pfid));
2183 rc = lfsck_namespace_insert_orphan(env, com, child,
2184 info->lti_tmpbuf, "D", NULL);
2190 linkea_first_entry(&ldata);
2191 /* This is the most common case: the object has unique linkEA entry. */
2192 if (ldata.ld_leh->leh_reccount == 1) {
2193 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
2194 &lh, &type, &retry);
2196 LASSERT(!lustre_handle_is_used(&lh));
2205 if (!lustre_handle_is_used(&lh))
2208 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
2209 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
2215 /* When we come here, the cases usually like that:
2216 * 1) The directory object has a corrupted linkEA entry. During the
2217 * first-stage scanning, the LFSCK cannot know such corruption,
2218 * then it appends the right linkEA entry according to the found
2219 * name entry after the bad one.
2221 * 2) The directory object has a right linkEA entry. During the
2222 * first-stage scanning, the LFSCK finds some bad name entry,
2223 * but the LFSCK cannot aware that at that time, then it adds
2224 * the bad linkEA entry for further processing. */
2225 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
2231 lfsck_ibits_unlock(&lh, LCK_EX);
2234 case LNIT_BAD_LINKEA:
2235 ns->ln_linkea_repaired++;
2237 case LNIT_UNMATCHED_PAIRS:
2238 ns->ln_unmatched_pairs_repaired++;
2241 ns->ln_mul_ref_repaired++;
2249 ns->ln_unknown_inconsistency++;
2255 * Double scan the MDT-object for namespace LFSCK.
2257 * If the MDT-object contains invalid or repeated linkEA entries, then drop
2258 * those entries from the linkEA; if the linkEA becomes empty or the object
2259 * has no linkEA, then it is an orphan and will be added into the directory
2260 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
2261 * the remote parent; if the name entry corresponding to some linkEA entry
2262 * is lost, then add the name entry back to the namespace.
2264 * \param[in] env pointer to the thread context
2265 * \param[in] com pointer to the lfsck component
2266 * \param[in] child pointer to the dt_object to be handled
2267 * \param[in] flags some hints to indicate how the @child should be handled
2269 * \retval positive number for repaired cases
2270 * \retval 0 if nothing to be repaired
2271 * \retval negative error number on failure
2273 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
2274 struct lfsck_component *com,
2275 struct dt_object *child, __u8 flags)
2277 struct lfsck_thread_info *info = lfsck_env_info(env);
2278 struct lu_attr *la = &info->lti_la;
2279 struct lu_name *cname = &info->lti_name;
2280 struct lu_fid *pfid = &info->lti_fid;
2281 struct lu_fid *cfid = &info->lti_fid2;
2282 struct lfsck_instance *lfsck = com->lc_lfsck;
2283 struct lfsck_namespace *ns = com->lc_file_ram;
2284 struct dt_object *parent = NULL;
2285 struct linkea_data ldata = { 0 };
2286 bool repaired = false;
2291 dt_read_lock(env, child, 0);
2292 if (unlikely(lfsck_is_dead_obj(child))) {
2293 dt_read_unlock(env, child);
2298 if (S_ISDIR(lfsck_object_type(child))) {
2299 dt_read_unlock(env, child);
2300 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
2305 rc = lfsck_links_read(env, child, &ldata);
2306 dt_read_unlock(env, child);
2310 linkea_first_entry(&ldata);
2311 while (ldata.ld_lee != NULL) {
2312 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
2314 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
2316 /* Found repeated linkEA entries */
2318 rc = lfsck_namespace_shrink_linkea(env, com, child,
2319 &ldata, cname, pfid, false);
2331 /* Invalid PFID in the linkEA entry. */
2332 if (!fid_is_sane(pfid)) {
2333 rc = lfsck_namespace_shrink_linkea(env, com, child,
2334 &ldata, cname, pfid, true);
2344 parent = lfsck_object_find_bottom(env, lfsck, pfid);
2346 GOTO(out, rc = PTR_ERR(parent));
2348 if (!dt_object_exists(parent)) {
2349 if (ldata.ld_leh->leh_reccount > 1) {
2350 /* If it is NOT the last linkEA entry, then
2351 * there is still other chance to make the
2352 * child to be visible via other parent, then
2353 * remove this linkEA entry. */
2354 rc = lfsck_namespace_shrink_linkea(env, com,
2355 child, &ldata, cname, pfid, true);
2357 /* Create the lost parent as an orphan. */
2358 rc = lfsck_namespace_create_orphan(env, com,
2361 lfsck_object_put(env, parent);
2369 /* Add the missing name entry to the parent. */
2370 rc = lfsck_namespace_insert_normal(env, com,
2371 parent, child, cname->ln_name);
2372 linkea_next_entry(&ldata);
2375 lfsck_object_put(env, parent);
2385 /* The linkEA entry with bad parent will be removed. */
2386 if (unlikely(!dt_try_as_dir(env, parent))) {
2387 lfsck_object_put(env, parent);
2388 rc = lfsck_namespace_shrink_linkea(env, com, child,
2389 &ldata, cname, pfid, true);
2399 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
2400 (const struct dt_key *)cname->ln_name,
2402 if (rc != 0 && rc != -ENOENT) {
2403 lfsck_object_put(env, parent);
2409 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
2410 /* It is the most common case that we
2411 * find the name entry corresponding
2412 * to the linkEA entry. */
2413 lfsck_object_put(env, parent);
2414 linkea_next_entry(&ldata);
2416 /* The name entry references another
2417 * MDT-object that may be created by
2418 * the LFSCK for repairing dangling
2419 * name entry. Try to replace it. */
2420 rc = lfsck_namespace_replace_cond(env, com,
2421 parent, child, cfid, cname);
2422 lfsck_object_put(env, parent);
2428 linkea_next_entry(&ldata);
2430 rc = lfsck_namespace_shrink_linkea(env,
2444 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
2448 /* If there is no name entry in the parent dir and the object
2449 * link count is less than the linkea entries count, then the
2450 * linkea entry should be removed. */
2451 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
2452 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2453 parent, child, &ldata, cname, pfid);
2454 lfsck_object_put(env, parent);
2464 /* Add the missing name entry back to the namespace. */
2465 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2467 lfsck_object_put(env, parent);
2474 linkea_next_entry(&ldata);
2480 if (rc < 0 && rc != -ENODATA)
2484 LASSERT(ldata.ld_leh != NULL);
2486 count = ldata.ld_leh->leh_reccount;
2490 /* If the child becomes orphan, then insert it into
2491 * the global .lustre/lost+found/MDTxxxx directory. */
2492 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
2498 ns->ln_mul_ref_repaired++;
2503 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
2507 if (la->la_nlink != count) {
2508 /* XXX: there will be other patch(es) for MDT-object
2509 * hard links verification. */
2513 if (la->la_nlink > 1)
2514 ns->ln_mul_linked_repaired++;
2523 static void lfsck_namespace_dump_statistics(struct seq_file *m,
2524 struct lfsck_namespace *ns,
2525 __u64 checked_phase1,
2526 __u64 checked_phase2,
2530 seq_printf(m, "checked_phase1: "LPU64"\n"
2531 "checked_phase2: "LPU64"\n"
2532 "updated_phase1: "LPU64"\n"
2533 "updated_phase2: "LPU64"\n"
2534 "failed_phase1: "LPU64"\n"
2535 "failed_phase2: "LPU64"\n"
2536 "directories: "LPU64"\n"
2537 "dirent_repaired: "LPU64"\n"
2538 "linkea_repaired: "LPU64"\n"
2539 "nlinks_repaired: "LPU64"\n"
2540 "lost_found: "LPU64"\n"
2541 "multiple_linked_checked: "LPU64"\n"
2542 "multiple_linked_repaired: "LPU64"\n"
2543 "unknown_inconsistency: "LPU64"\n"
2544 "unmatched_pairs_repaired: "LPU64"\n"
2545 "dangling_repaired: "LPU64"\n"
2546 "multiple_referenced_repaired: "LPU64"\n"
2547 "bad_file_type_repaired: "LPU64"\n"
2548 "success_count: %u\n"
2549 "run_time_phase1: %u seconds\n"
2550 "run_time_phase2: %u seconds\n",
2553 ns->ln_items_repaired,
2554 ns->ln_objs_repaired_phase2,
2555 ns->ln_items_failed,
2556 ns->ln_objs_failed_phase2,
2557 ns->ln_dirs_checked,
2558 ns->ln_dirent_repaired,
2559 ns->ln_linkea_repaired,
2560 ns->ln_objs_nlink_repaired,
2561 ns->ln_objs_lost_found,
2562 ns->ln_mul_linked_checked,
2563 ns->ln_mul_linked_repaired,
2564 ns->ln_unknown_inconsistency,
2565 ns->ln_unmatched_pairs_repaired,
2566 ns->ln_dangling_repaired,
2567 ns->ln_mul_ref_repaired,
2568 ns->ln_bad_type_repaired,
2569 ns->ln_success_count,
2574 /* namespace APIs */
2576 static int lfsck_namespace_reset(const struct lu_env *env,
2577 struct lfsck_component *com, bool init)
2579 struct lfsck_instance *lfsck = com->lc_lfsck;
2580 struct lfsck_namespace *ns = com->lc_file_ram;
2581 struct dt_object *root;
2582 struct dt_object *dto;
2586 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
2588 GOTO(log, rc = PTR_ERR(root));
2590 if (unlikely(!dt_try_as_dir(env, root)))
2591 GOTO(put, rc = -ENOTDIR);
2593 down_write(&com->lc_sem);
2595 memset(ns, 0, sizeof(*ns));
2597 __u32 count = ns->ln_success_count;
2598 __u64 last_time = ns->ln_time_last_complete;
2600 memset(ns, 0, sizeof(*ns));
2601 ns->ln_success_count = count;
2602 ns->ln_time_last_complete = last_time;
2604 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
2605 ns->ln_status = LS_INIT;
2607 rc = local_object_unlink(env, lfsck->li_bottom, root,
2608 lfsck_namespace_name);
2612 lfsck_object_put(env, com->lc_obj);
2614 dto = local_index_find_or_create(env, lfsck->li_los, root,
2615 lfsck_namespace_name,
2616 S_IFREG | S_IRUGO | S_IWUSR,
2617 &dt_lfsck_features);
2619 GOTO(out, rc = PTR_ERR(dto));
2622 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
2626 rc = lfsck_namespace_store(env, com, true);
2631 up_write(&com->lc_sem);
2634 lu_object_put(env, &root->do_lu);
2636 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
2637 lfsck_lfsck2name(lfsck), rc);
2642 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
2645 struct lfsck_namespace *ns = com->lc_file_ram;
2647 down_write(&com->lc_sem);
2649 com->lc_new_checked++;
2650 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
2651 up_write(&com->lc_sem);
2654 static int lfsck_namespace_checkpoint(const struct lu_env *env,
2655 struct lfsck_component *com, bool init)
2657 struct lfsck_instance *lfsck = com->lc_lfsck;
2658 struct lfsck_namespace *ns = com->lc_file_ram;
2662 rc = lfsck_checkpoint_generic(env, com);
2667 down_write(&com->lc_sem);
2669 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
2671 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
2672 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
2673 HALF_SEC - lfsck->li_time_last_checkpoint);
2674 ns->ln_time_last_checkpoint = cfs_time_current_sec();
2675 ns->ln_items_checked += com->lc_new_checked;
2676 com->lc_new_checked = 0;
2679 rc = lfsck_namespace_store(env, com, false);
2680 up_write(&com->lc_sem);
2683 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
2684 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
2685 lfsck->li_pos_current.lp_oit_cookie,
2686 PFID(&lfsck->li_pos_current.lp_dir_parent),
2687 lfsck->li_pos_current.lp_dir_cookie, rc);
2689 return rc > 0 ? 0 : rc;
2692 static int lfsck_namespace_prep(const struct lu_env *env,
2693 struct lfsck_component *com,
2694 struct lfsck_start_param *lsp)
2696 struct lfsck_instance *lfsck = com->lc_lfsck;
2697 struct lfsck_namespace *ns = com->lc_file_ram;
2698 struct lfsck_position *pos = &com->lc_pos_start;
2701 if (ns->ln_status == LS_COMPLETED) {
2702 rc = lfsck_namespace_reset(env, com, false);
2704 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
2707 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
2708 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
2714 down_write(&com->lc_sem);
2715 ns->ln_time_latest_start = cfs_time_current_sec();
2716 spin_lock(&lfsck->li_lock);
2718 if (ns->ln_flags & LF_SCANNED_ONCE) {
2719 if (!lfsck->li_drop_dryrun ||
2720 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
2721 ns->ln_status = LS_SCANNING_PHASE2;
2722 list_move_tail(&com->lc_link,
2723 &lfsck->li_list_double_scan);
2724 if (!list_empty(&com->lc_link_dir))
2725 list_del_init(&com->lc_link_dir);
2726 lfsck_pos_set_zero(pos);
2728 ns->ln_status = LS_SCANNING_PHASE1;
2729 ns->ln_run_time_phase1 = 0;
2730 ns->ln_run_time_phase2 = 0;
2731 ns->ln_items_checked = 0;
2732 ns->ln_items_repaired = 0;
2733 ns->ln_items_failed = 0;
2734 ns->ln_dirs_checked = 0;
2735 ns->ln_objs_checked_phase2 = 0;
2736 ns->ln_objs_repaired_phase2 = 0;
2737 ns->ln_objs_failed_phase2 = 0;
2738 ns->ln_objs_nlink_repaired = 0;
2739 ns->ln_objs_lost_found = 0;
2740 ns->ln_dirent_repaired = 0;
2741 ns->ln_linkea_repaired = 0;
2742 ns->ln_mul_linked_checked = 0;
2743 ns->ln_mul_linked_repaired = 0;
2744 ns->ln_unknown_inconsistency = 0;
2745 ns->ln_unmatched_pairs_repaired = 0;
2746 ns->ln_dangling_repaired = 0;
2747 ns->ln_mul_ref_repaired = 0;
2748 ns->ln_bad_type_repaired = 0;
2749 fid_zero(&ns->ln_fid_latest_scanned_phase2);
2750 if (list_empty(&com->lc_link_dir))
2751 list_add_tail(&com->lc_link_dir,
2752 &lfsck->li_list_dir);
2753 *pos = ns->ln_pos_first_inconsistent;
2756 ns->ln_status = LS_SCANNING_PHASE1;
2757 if (list_empty(&com->lc_link_dir))
2758 list_add_tail(&com->lc_link_dir,
2759 &lfsck->li_list_dir);
2760 if (!lfsck->li_drop_dryrun ||
2761 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
2762 *pos = ns->ln_pos_last_checkpoint;
2763 pos->lp_oit_cookie++;
2765 *pos = ns->ln_pos_first_inconsistent;
2769 spin_unlock(&lfsck->li_lock);
2770 up_write(&com->lc_sem);
2772 rc = lfsck_start_assistant(env, com, lsp);
2774 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
2775 DFID", "LPX64"]: rc = %d\n",
2776 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
2777 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
2782 static int lfsck_namespace_exec_oit(const struct lu_env *env,
2783 struct lfsck_component *com,
2784 struct dt_object *obj)
2786 struct lfsck_thread_info *info = lfsck_env_info(env);
2787 struct lfsck_namespace *ns = com->lc_file_ram;
2788 struct lfsck_instance *lfsck = com->lc_lfsck;
2789 const struct lu_fid *fid = lfsck_dto2fid(obj);
2790 struct lu_attr *la = &info->lti_la;
2791 struct lu_fid *pfid = &info->lti_fid2;
2792 struct lu_name *cname = &info->lti_name;
2793 struct lu_seq_range *range = &info->lti_range;
2794 struct dt_device *dev = lfsck->li_bottom;
2795 struct seq_server_site *ss =
2796 lu_site2seq(dev->dd_lu_dev.ld_site);
2797 struct linkea_data ldata = { 0 };
2798 __u32 idx = lfsck_dev_idx(dev);
2802 rc = lfsck_links_read(env, obj, &ldata);
2806 /* -EINVAL means crashed linkEA, should be verified. */
2807 if (rc == -EINVAL) {
2808 rc = lfsck_namespace_trace_update(env, com, fid,
2809 LNTF_CHECK_LINKEA, true);
2811 struct lustre_handle lh = { 0 };
2813 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2814 MDS_INODELOCK_UPDATE |
2815 MDS_INODELOCK_XATTR, LCK_EX);
2817 rc = lfsck_namespace_links_remove(env, com,
2819 lfsck_ibits_unlock(&lh, LCK_EX);
2823 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
2826 /* zero-linkEA object may be orphan, but it also maybe because
2827 * of upgrading. Currently, we cannot record it for double scan.
2828 * Because it may cause the LFSCK tracing file to be too large. */
2829 if (rc == -ENODATA) {
2830 if (S_ISDIR(lfsck_object_type(obj)))
2833 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2837 if (la->la_nlink > 1)
2838 rc = lfsck_namespace_trace_update(env, com, fid,
2839 LNTF_CHECK_LINKEA, true);
2847 /* Record multiple-linked object. */
2848 if (ldata.ld_leh->leh_reccount > 1) {
2849 rc = lfsck_namespace_trace_update(env, com, fid,
2850 LNTF_CHECK_LINKEA, true);
2855 linkea_first_entry(&ldata);
2856 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
2857 if (!fid_is_sane(pfid)) {
2858 rc = lfsck_namespace_trace_update(env, com, fid,
2859 LNTF_CHECK_PARENT, true);
2861 fld_range_set_mdt(range);
2862 rc = fld_local_lookup(env, ss->ss_server_fld,
2863 fid_seq(pfid), range);
2864 if ((rc == -ENOENT) ||
2865 (rc == 0 && range->lsr_index != idx)) {
2866 rc = lfsck_namespace_trace_update(env, com, fid,
2867 LNTF_CHECK_LINKEA, true);
2869 if (S_ISDIR(lfsck_object_type(obj)))
2872 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2876 if (la->la_nlink > 1)
2877 rc = lfsck_namespace_trace_update(env, com,
2878 fid, LNTF_CHECK_LINKEA, true);
2885 down_write(&com->lc_sem);
2886 com->lc_new_checked++;
2887 if (S_ISDIR(lfsck_object_type(obj)))
2888 ns->ln_dirs_checked++;
2890 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
2891 up_write(&com->lc_sem);
2896 static int lfsck_namespace_exec_dir(const struct lu_env *env,
2897 struct lfsck_component *com,
2898 struct lu_dirent *ent, __u16 type)
2900 struct lfsck_assistant_data *lad = com->lc_data;
2901 struct lfsck_namespace_req *lnr;
2902 bool wakeup = false;
2904 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
2906 struct lfsck_namespace *ns = com->lc_file_ram;
2908 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
2909 return PTR_ERR(lnr);
2912 spin_lock(&lad->lad_lock);
2913 if (lad->lad_assistant_status < 0) {
2914 spin_unlock(&lad->lad_lock);
2915 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
2916 return lad->lad_assistant_status;
2919 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
2920 if (lad->lad_prefetched == 0)
2923 lad->lad_prefetched++;
2924 spin_unlock(&lad->lad_lock);
2926 wake_up_all(&lad->lad_thread.t_ctl_waitq);
2928 down_write(&com->lc_sem);
2929 com->lc_new_checked++;
2930 up_write(&com->lc_sem);
2935 static int lfsck_namespace_post(const struct lu_env *env,
2936 struct lfsck_component *com,
2937 int result, bool init)
2939 struct lfsck_instance *lfsck = com->lc_lfsck;
2940 struct lfsck_namespace *ns = com->lc_file_ram;
2944 lfsck_post_generic(env, com, &result);
2946 down_write(&com->lc_sem);
2947 spin_lock(&lfsck->li_lock);
2949 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
2951 ns->ln_status = LS_SCANNING_PHASE2;
2952 ns->ln_flags |= LF_SCANNED_ONCE;
2953 ns->ln_flags &= ~LF_UPGRADE;
2954 list_del_init(&com->lc_link_dir);
2955 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
2956 } else if (result == 0) {
2957 ns->ln_status = lfsck->li_status;
2958 if (ns->ln_status == 0)
2959 ns->ln_status = LS_STOPPED;
2960 if (ns->ln_status != LS_PAUSED) {
2961 list_del_init(&com->lc_link_dir);
2962 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
2965 ns->ln_status = LS_FAILED;
2966 list_del_init(&com->lc_link_dir);
2967 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
2969 spin_unlock(&lfsck->li_lock);
2972 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
2973 HALF_SEC - lfsck->li_time_last_checkpoint);
2974 ns->ln_time_last_checkpoint = cfs_time_current_sec();
2975 ns->ln_items_checked += com->lc_new_checked;
2976 com->lc_new_checked = 0;
2979 rc = lfsck_namespace_store(env, com, false);
2980 up_write(&com->lc_sem);
2982 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
2983 lfsck_lfsck2name(lfsck), rc);
2989 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
2992 struct lfsck_instance *lfsck = com->lc_lfsck;
2993 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2994 struct lfsck_namespace *ns = com->lc_file_ram;
2997 down_read(&com->lc_sem);
2998 seq_printf(m, "name: lfsck_namespace\n"
3004 lfsck_status2names(ns->ln_status));
3006 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
3010 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
3014 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
3015 "time_since_last_completed");
3019 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
3020 "time_since_latest_start");
3024 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
3025 "time_since_last_checkpoint");
3029 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
3030 "latest_start_position");
3034 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
3035 "last_checkpoint_position");
3039 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
3040 "first_failure_position");
3044 if (ns->ln_status == LS_SCANNING_PHASE1) {
3045 struct lfsck_position pos;
3046 const struct dt_it_ops *iops;
3047 cfs_duration_t duration = cfs_time_current() -
3048 lfsck->li_time_last_checkpoint;
3049 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
3050 __u64 speed = checked;
3051 __u64 new_checked = com->lc_new_checked * HZ;
3052 __u32 rtime = ns->ln_run_time_phase1 +
3053 cfs_duration_sec(duration + HALF_SEC);
3056 do_div(new_checked, duration);
3058 do_div(speed, rtime);
3059 lfsck_namespace_dump_statistics(m, ns, checked,
3060 ns->ln_objs_checked_phase2,
3061 rtime, ns->ln_run_time_phase2);
3063 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
3064 "average_speed_phase2: N/A\n"
3065 "real_time_speed_phase1: "LPU64" items/sec\n"
3066 "real_time_speed_phase2: N/A\n",
3070 LASSERT(lfsck->li_di_oit != NULL);
3072 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
3074 /* The low layer otable-based iteration position may NOT
3075 * exactly match the namespace-based directory traversal
3076 * cookie. Generally, it is not a serious issue. But the
3077 * caller should NOT make assumption on that. */
3078 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
3079 if (!lfsck->li_current_oit_processed)
3080 pos.lp_oit_cookie--;
3082 spin_lock(&lfsck->li_lock);
3083 if (lfsck->li_di_dir != NULL) {
3084 pos.lp_dir_cookie = lfsck->li_cookie_dir;
3085 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
3086 fid_zero(&pos.lp_dir_parent);
3087 pos.lp_dir_cookie = 0;
3090 *lfsck_dto2fid(lfsck->li_obj_dir);
3093 fid_zero(&pos.lp_dir_parent);
3094 pos.lp_dir_cookie = 0;
3096 spin_unlock(&lfsck->li_lock);
3097 lfsck_pos_dump(m, &pos, "current_position");
3098 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
3099 cfs_duration_t duration = cfs_time_current() -
3100 lfsck->li_time_last_checkpoint;
3101 __u64 checked = ns->ln_objs_checked_phase2 +
3102 com->lc_new_checked;
3103 __u64 speed1 = ns->ln_items_checked;
3104 __u64 speed2 = checked;
3105 __u64 new_checked = com->lc_new_checked * HZ;
3106 __u32 rtime = ns->ln_run_time_phase2 +
3107 cfs_duration_sec(duration + HALF_SEC);
3110 do_div(new_checked, duration);
3111 if (ns->ln_run_time_phase1 != 0)
3112 do_div(speed1, ns->ln_run_time_phase1);
3114 do_div(speed2, rtime);
3115 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
3117 ns->ln_run_time_phase1, rtime);
3119 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
3120 "average_speed_phase2: "LPU64" objs/sec\n"
3121 "real_time_speed_phase1: N/A\n"
3122 "real_time_speed_phase2: "LPU64" objs/sec\n"
3123 "current_position: "DFID"\n",
3127 PFID(&ns->ln_fid_latest_scanned_phase2));
3129 __u64 speed1 = ns->ln_items_checked;
3130 __u64 speed2 = ns->ln_objs_checked_phase2;
3132 if (ns->ln_run_time_phase1 != 0)
3133 do_div(speed1, ns->ln_run_time_phase1);
3134 if (ns->ln_run_time_phase2 != 0)
3135 do_div(speed2, ns->ln_run_time_phase2);
3136 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
3137 ns->ln_objs_checked_phase2,
3138 ns->ln_run_time_phase1,
3139 ns->ln_run_time_phase2);
3141 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
3142 "average_speed_phase2: "LPU64" objs/sec\n"
3143 "real_time_speed_phase1: N/A\n"
3144 "real_time_speed_phase2: N/A\n"
3145 "current_position: N/A\n",
3150 up_read(&com->lc_sem);
3154 static int lfsck_namespace_double_scan(const struct lu_env *env,
3155 struct lfsck_component *com)
3157 struct lfsck_namespace *ns = com->lc_file_ram;
3159 return lfsck_double_scan_generic(env, com, ns->ln_status);
3162 static void lfsck_namespace_data_release(const struct lu_env *env,
3163 struct lfsck_component *com)
3165 struct lfsck_assistant_data *lad = com->lc_data;
3166 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
3167 struct lfsck_tgt_desc *ltd;
3168 struct lfsck_tgt_desc *next;
3170 LASSERT(lad != NULL);
3171 LASSERT(thread_is_init(&lad->lad_thread) ||
3172 thread_is_stopped(&lad->lad_thread));
3173 LASSERT(list_empty(&lad->lad_req_list));
3175 com->lc_data = NULL;
3177 spin_lock(<ds->ltd_lock);
3178 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
3179 ltd_namespace_phase_list) {
3180 list_del_init(<d->ltd_namespace_phase_list);
3182 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
3183 ltd_namespace_phase_list) {
3184 list_del_init(<d->ltd_namespace_phase_list);
3186 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
3187 ltd_namespace_list) {
3188 list_del_init(<d->ltd_namespace_list);
3190 spin_unlock(<ds->ltd_lock);
3192 CFS_FREE_BITMAP(lad->lad_bitmap);
3197 static int lfsck_namespace_in_notify(const struct lu_env *env,
3198 struct lfsck_component *com,
3199 struct lfsck_request *lr)
3201 struct lfsck_instance *lfsck = com->lc_lfsck;
3202 struct lfsck_namespace *ns = com->lc_file_ram;
3203 struct lfsck_assistant_data *lad = com->lc_data;
3204 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
3205 struct lfsck_tgt_desc *ltd;
3209 if (lr->lr_event != LE_PHASE1_DONE &&
3210 lr->lr_event != LE_PHASE2_DONE &&
3211 lr->lr_event != LE_PEER_EXIT)
3214 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
3215 "status %d\n", lfsck_lfsck2name(lfsck), lr->lr_event,
3216 lr->lr_index, lr->lr_status);
3218 spin_lock(<ds->ltd_lock);
3219 ltd = LTD_TGT(ltds, lr->lr_index);
3221 spin_unlock(<ds->ltd_lock);
3226 list_del_init(<d->ltd_namespace_phase_list);
3227 switch (lr->lr_event) {
3228 case LE_PHASE1_DONE:
3229 if (lr->lr_status <= 0) {
3230 ltd->ltd_namespace_done = 1;
3231 list_del_init(<d->ltd_namespace_list);
3232 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
3233 "phase1 for namespace LFSCK: rc = %d.\n",
3234 lfsck_lfsck2name(lfsck),
3235 ltd->ltd_index, lr->lr_status);
3236 ns->ln_flags |= LF_INCOMPLETE;
3241 if (list_empty(<d->ltd_namespace_list))
3242 list_add_tail(<d->ltd_namespace_list,
3243 &lad->lad_mdt_list);
3244 list_add_tail(<d->ltd_namespace_phase_list,
3245 &lad->lad_mdt_phase2_list);
3247 case LE_PHASE2_DONE:
3248 ltd->ltd_namespace_done = 1;
3249 list_del_init(<d->ltd_namespace_list);
3253 ltd->ltd_namespace_done = 1;
3254 list_del_init(<d->ltd_namespace_list);
3255 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
3257 "%s: the peer MDT %x exit namespace LFSCK\n",
3258 lfsck_lfsck2name(lfsck), ltd->ltd_index);
3259 ns->ln_flags |= LF_INCOMPLETE;
3265 spin_unlock(<ds->ltd_lock);
3267 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
3268 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
3270 memset(stop, 0, sizeof(*stop));
3271 stop->ls_status = lr->lr_status;
3272 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
3273 lfsck_stop(env, lfsck->li_bottom, stop);
3274 } else if (lfsck_phase2_next_ready(lad)) {
3275 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3281 static int lfsck_namespace_query(const struct lu_env *env,
3282 struct lfsck_component *com)
3284 struct lfsck_namespace *ns = com->lc_file_ram;
3286 return ns->ln_status;
3289 static struct lfsck_operations lfsck_namespace_ops = {
3290 .lfsck_reset = lfsck_namespace_reset,
3291 .lfsck_fail = lfsck_namespace_fail,
3292 .lfsck_checkpoint = lfsck_namespace_checkpoint,
3293 .lfsck_prep = lfsck_namespace_prep,
3294 .lfsck_exec_oit = lfsck_namespace_exec_oit,
3295 .lfsck_exec_dir = lfsck_namespace_exec_dir,
3296 .lfsck_post = lfsck_namespace_post,
3297 .lfsck_dump = lfsck_namespace_dump,
3298 .lfsck_double_scan = lfsck_namespace_double_scan,
3299 .lfsck_data_release = lfsck_namespace_data_release,
3300 .lfsck_quit = lfsck_quit_generic,
3301 .lfsck_in_notify = lfsck_namespace_in_notify,
3302 .lfsck_query = lfsck_namespace_query,
3306 * Repair dangling name entry.
3308 * For the name entry with dangling reference, we need to repare the
3309 * inconsistency according to the LFSCK sponsor's requirement:
3311 * 1) Keep the inconsistency there and report the inconsistency case,
3312 * then give the chance to the application to find related issues,
3313 * and the users can make the decision about how to handle it with
3314 * more human knownledge. (by default)
3316 * 2) Re-create the missed MDT-object with the FID information.
3318 * \param[in] env pointer to the thread context
3319 * \param[in] com pointer to the lfsck component
3320 * \param[in] child pointer to the object corresponding to the dangling
3322 * \param[in] lnr pointer to the namespace request that contains the
3323 * name's name, parent object, parent's LMV, and ect.
3325 * \retval positive number if no need to repair
3326 * \retval zero for repaired successfully
3327 * \retval negative error number on failure
3329 int lfsck_namespace_repair_dangling(const struct lu_env *env,
3330 struct lfsck_component *com,
3331 struct dt_object *child,
3332 struct lfsck_namespace_req *lnr)
3334 struct lfsck_thread_info *info = lfsck_env_info(env);
3335 struct lu_attr *la = &info->lti_la;
3336 struct dt_allocation_hint *hint = &info->lti_hint;
3337 struct dt_object_format *dof = &info->lti_dof;
3338 struct dt_insert_rec *rec = &info->lti_dt_rec;
3339 struct dt_object *parent = lnr->lnr_obj;
3340 const struct lu_name *cname;
3341 struct linkea_data ldata = { 0 };
3342 struct lustre_handle lh = { 0 };
3343 struct lu_buf linkea_buf;
3344 struct lfsck_instance *lfsck = com->lc_lfsck;
3345 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3346 struct dt_device *dev = lfsck_obj2dt_dev(child);
3347 struct thandle *th = NULL;
3349 __u16 type = lnr->lnr_type;
3353 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
3354 if (bk->lb_param & LPF_CREATE_MDTOBJ)
3359 if (!create || bk->lb_param & LPF_DRYRUN)
3362 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
3366 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
3370 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
3371 MDS_INODELOCK_UPDATE, LCK_EX);
3375 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
3379 th = dt_trans_create(env, dev);
3381 GOTO(log, rc = PTR_ERR(th));
3383 /* Set the ctime as zero, then others can know it is created for
3384 * repairing dangling name entry by LFSCK. And if the LFSCK made
3385 * wrong decision and the real MDT-object has been found later,
3386 * then the LFSCK has chance to fix the incosistency properly. */
3387 memset(la, 0, sizeof(*la));
3388 la->la_mode = (type & S_IFMT) | 0600;
3389 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
3390 LA_ATIME | LA_MTIME | LA_CTIME;
3392 child->do_ops->do_ah_init(env, hint, parent, child,
3393 la->la_mode & S_IFMT);
3395 memset(dof, 0, sizeof(*dof));
3396 dof->dof_type = dt_mode_to_dft(type);
3397 /* If the target is a regular file, then the LFSCK will only create
3398 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
3399 * OST-objects will be created when write open. */
3401 /* 1a. create child. */
3402 rc = dt_declare_create(env, child, la, hint, dof, th);
3406 if (S_ISDIR(type)) {
3407 if (unlikely(!dt_try_as_dir(env, child)))
3408 GOTO(stop, rc = -ENOTDIR);
3410 /* 2a. insert dot into child dir */
3411 rec->rec_type = S_IFDIR;
3412 rec->rec_fid = lfsck_dto2fid(child);
3413 rc = dt_declare_insert(env, child,
3414 (const struct dt_rec *)rec,
3415 (const struct dt_key *)dot, th);
3419 /* 3a. insert dotdot into child dir */
3420 rec->rec_fid = lfsck_dto2fid(parent);
3421 rc = dt_declare_insert(env, child,
3422 (const struct dt_rec *)rec,
3423 (const struct dt_key *)dotdot, th);
3427 /* 4a. increase child nlink */
3428 rc = dt_declare_ref_add(env, child, th);
3433 /* 5a. insert linkEA for child */
3434 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
3435 ldata.ld_leh->leh_len);
3436 rc = dt_declare_xattr_set(env, child, &linkea_buf,
3437 XATTR_NAME_LINK, 0, th);
3441 rc = dt_trans_start(env, dev, th);
3443 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
3445 dt_write_lock(env, child, 0);
3446 /* 1b. create child */
3447 rc = dt_create(env, child, la, hint, dof, th);
3449 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
3451 if (S_ISDIR(type)) {
3452 if (unlikely(!dt_try_as_dir(env, child)))
3453 GOTO(unlock, rc = -ENOTDIR);
3455 /* 2b. insert dot into child dir */
3456 rec->rec_type = S_IFDIR;
3457 rec->rec_fid = lfsck_dto2fid(child);
3458 rc = dt_insert(env, child, (const struct dt_rec *)rec,
3459 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
3463 /* 3b. insert dotdot into child dir */
3464 rec->rec_fid = lfsck_dto2fid(parent);
3465 rc = dt_insert(env, child, (const struct dt_rec *)rec,
3466 (const struct dt_key *)dotdot, th,
3471 /* 4b. increase child nlink */
3472 rc = dt_ref_add(env, child, th);
3477 /* 5b. insert linkEA for child. */
3478 rc = dt_xattr_set(env, child, &linkea_buf,
3479 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
3484 dt_write_unlock(env, child);
3487 dt_trans_stop(env, dev, th);
3490 lfsck_ibits_unlock(&lh, LCK_EX);
3491 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
3492 "reference for: parent "DFID", child "DFID", type %u, "
3493 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
3494 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
3495 type, cname->ln_name,
3496 create ? "Create the lost OST-object as required" :
3497 "Keep the MDT-object there by default", rc);
3500 struct lfsck_namespace *ns = com->lc_file_ram;
3502 ns->ln_flags |= LF_INCONSISTENT;
3508 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
3509 struct lfsck_component *com,
3510 struct lfsck_assistant_req *lar)
3512 struct lfsck_thread_info *info = lfsck_env_info(env);
3513 struct lu_attr *la = &info->lti_la;
3514 struct lfsck_instance *lfsck = com->lc_lfsck;
3515 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3516 struct lfsck_namespace *ns = com->lc_file_ram;
3517 struct linkea_data ldata = { 0 };
3518 const struct lu_name *cname;
3519 struct thandle *handle = NULL;
3520 struct lfsck_namespace_req *lnr =
3521 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
3522 struct dt_object *dir = lnr->lnr_obj;
3523 struct dt_object *obj = NULL;
3524 const struct lu_fid *pfid = lfsck_dto2fid(dir);
3525 struct dt_device *dev;
3526 struct lustre_handle lh = { 0 };
3527 bool repaired = false;
3528 bool dtlocked = false;
3535 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
3538 if (lnr->lnr_attr & LUDA_UPGRADE) {
3539 ns->ln_flags |= LF_UPGRADE;
3540 ns->ln_dirent_repaired++;
3542 } else if (lnr->lnr_attr & LUDA_REPAIR) {
3543 ns->ln_flags |= LF_INCONSISTENT;
3544 ns->ln_dirent_repaired++;
3548 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
3549 if (strcmp(lnr->lnr_name, dotdot) != 0)
3552 rc = lfsck_namespace_trace_update(env, com, pfid,
3553 LNTF_CHECK_PARENT, true);
3558 if (lnr->lnr_name[0] == '.' &&
3559 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
3562 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
3564 GOTO(out, rc = idx);
3566 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
3567 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
3570 dev = lfsck->li_next;
3572 struct lfsck_tgt_desc *ltd;
3574 /* Usually, some local filesystem consistency verification
3575 * tools can guarantee the local namespace tree consistenct.
3576 * So the LFSCK will only verify the remote directory. */
3577 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
3578 rc = lfsck_namespace_trace_update(env, com, pfid,
3579 LNTF_CHECK_PARENT, true);
3584 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
3585 if (unlikely(ltd == NULL)) {
3586 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
3587 "did not join the namespace LFSCK\n",
3588 lfsck_lfsck2name(lfsck), idx);
3589 ns->ln_flags |= LF_INCOMPLETE;
3591 GOTO(out, rc = -ENODEV);
3597 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
3599 GOTO(out, rc = PTR_ERR(obj));
3601 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
3602 if (dt_object_exists(obj) == 0) {
3605 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
3607 type = LNIT_DANGLING;
3608 rc = lfsck_namespace_repair_dangling(env, com,
3617 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
3620 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3621 MDS_INODELOCK_UPDATE |
3622 MDS_INODELOCK_XATTR, LCK_EX);
3626 handle = dt_trans_create(env, dev);
3628 GOTO(out, rc = PTR_ERR(handle));
3630 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
3634 rc = dt_trans_start(env, dev, handle);
3638 dt_write_lock(env, obj, 0);
3642 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
3646 rc = lfsck_links_read(env, obj, &ldata);
3647 if (unlikely(rc == -ENOENT)) {
3648 if (handle != NULL) {
3649 dt_write_unlock(env, obj);
3652 dt_trans_stop(env, dev, handle);
3655 lfsck_ibits_unlock(&lh, LCK_EX);
3658 /* It may happen when the remote object has been removed,
3659 * but the local MDT is not aware of that. */
3661 } else if (rc == 0) {
3662 count = ldata.ld_leh->leh_reccount;
3663 rc = linkea_links_find(&ldata, cname, pfid);
3665 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
3666 if ((lfsck_object_type(obj) & S_IFMT) !=
3668 ns->ln_flags |= LF_INCONSISTENT;
3669 type = LNIT_BAD_TYPE;
3675 ns->ln_flags |= LF_INCONSISTENT;
3677 /* If the file type stored in the name entry does not match
3678 * the file type claimed by the object, and the object does
3679 * not recognize the name entry, then it is quite possible
3680 * that the name entry is corrupted. */
3681 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
3682 type = LNIT_BAD_DIRENT;
3687 /* For sub-dir object, we cannot make sure whether the sub-dir
3688 * back references the parent via ".." name entry correctly or
3689 * not in the LFSCK first-stage scanning. It may be that the
3690 * (remote) sub-dir ".." name entry has no parent FID after
3691 * file-level backup/restore and its linkEA may be wrong.
3692 * So under such case, we should replace the linkEA according
3693 * to current name entry. But this needs to be done during the
3694 * LFSCK second-stage scanning. The LFSCK will record the name
3695 * entry for further possible using. */
3699 } else if (unlikely(rc == -EINVAL)) {
3700 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
3701 type = LNIT_BAD_TYPE;
3704 ns->ln_flags |= LF_INCONSISTENT;
3705 /* The magic crashed, we are not sure whether there are more
3706 * corrupt data in the linkea, so remove all linkea entries. */
3710 } else if (rc == -ENODATA) {
3711 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
3712 type = LNIT_BAD_TYPE;
3715 ns->ln_flags |= LF_UPGRADE;
3720 if (bk->lb_param & LPF_DRYRUN) {
3721 ns->ln_linkea_repaired++;
3727 if (!lustre_handle_is_used(&lh))
3733 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
3740 rc = linkea_data_new(&ldata,
3741 &lfsck_env_info(env)->lti_linkea_buf);
3746 rc = linkea_add_buf(&ldata, cname, pfid);
3750 rc = lfsck_links_write(env, obj, &ldata, handle);
3754 count = ldata.ld_leh->leh_reccount;
3755 if (!S_ISDIR(lfsck_object_type(obj)) ||
3756 !dt_object_remote(obj)) {
3757 ns->ln_linkea_repaired++;
3768 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3772 if ((count == 1 && la->la_nlink == 1) ||
3773 S_ISDIR(lfsck_object_type(obj)))
3774 /* Usually, it is for single linked object or dir, do nothing.*/
3777 /* Following modification will be in another transaction. */
3778 if (handle != NULL) {
3779 dt_write_unlock(env, obj);
3782 dt_trans_stop(env, dev, handle);
3785 lfsck_ibits_unlock(&lh, LCK_EX);
3788 ns->ln_mul_linked_checked++;
3789 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
3790 LNTF_CHECK_LINKEA, true);
3796 dt_write_unlock(env, obj);
3798 if (handle != NULL && !IS_ERR(handle))
3799 dt_trans_stop(env, dev, handle);
3802 lfsck_ibits_unlock(&lh, LCK_EX);
3808 rc = lfsck_namespace_repair_dirent(env, com, dir,
3809 obj, lnr->lnr_name, lnr->lnr_name,
3810 lnr->lnr_type, true, false);
3814 case LNIT_BAD_DIRENT:
3816 /* XXX: This is a bad dirent, we do not know whether
3817 * the original name entry reference a regular
3818 * file or a directory, then keep the parent's
3819 * nlink count unchanged here. */
3820 rc = lfsck_namespace_repair_dirent(env, com, dir,
3821 obj, lnr->lnr_name, lnr->lnr_name,
3822 lnr->lnr_type, false, false);
3831 down_write(&com->lc_sem);
3833 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
3834 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
3835 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
3836 PFID(lfsck_dto2fid(lnr->lnr_obj)),
3837 lnr->lnr_namelen, lnr->lnr_name, rc);
3839 lfsck_namespace_record_failure(env, lfsck, ns);
3840 if (!(bk->lb_param & LPF_FAILOUT))
3844 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
3845 "repaired the entry: "DFID", parent "DFID
3846 ", name %.*s\n", lfsck_lfsck2name(lfsck),
3847 PFID(&lnr->lnr_fid),
3848 PFID(lfsck_dto2fid(lnr->lnr_obj)),
3849 lnr->lnr_namelen, lnr->lnr_name);
3852 ns->ln_items_repaired++;
3856 ns->ln_dangling_repaired++;
3859 ns->ln_bad_type_repaired++;
3861 case LNIT_BAD_DIRENT:
3862 ns->ln_dirent_repaired++;
3868 if (bk->lb_param & LPF_DRYRUN &&
3869 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
3870 lfsck_pos_fill(env, lfsck,
3871 &ns->ln_pos_first_inconsistent,
3877 up_write(&com->lc_sem);
3879 if (obj != NULL && !IS_ERR(obj))
3880 lfsck_object_put(env, obj);
3884 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
3885 struct lfsck_component *com)
3887 struct lfsck_instance *lfsck = com->lc_lfsck;
3888 struct ptlrpc_thread *thread = &lfsck->li_thread;
3889 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3890 struct lfsck_namespace *ns = com->lc_file_ram;
3891 struct dt_object *obj = com->lc_obj;
3892 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
3893 struct dt_object *target;
3901 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
3902 lfsck_lfsck2name(lfsck));
3904 com->lc_new_checked = 0;
3905 com->lc_new_scanned = 0;
3906 com->lc_time_last_checkpoint = cfs_time_current();
3907 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
3908 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
3910 di = iops->init(env, obj, 0, BYPASS_CAPA);
3912 RETURN(PTR_ERR(di));
3914 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
3915 rc = iops->get(env, di, (const struct dt_key *)&fid);
3919 /* Skip the start one, which either has been processed or non-exist. */
3920 rc = iops->next(env, di);
3925 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
3927 struct l_wait_info lwi;
3929 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
3931 l_wait_event(thread->t_ctl_waitq,
3932 !thread_is_running(thread),
3935 if (unlikely(!thread_is_running(thread)))
3939 key = iops->key(env, di);
3940 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
3941 if (!fid_is_sane(&fid)) {
3946 target = lfsck_object_find(env, lfsck, &fid);
3947 if (IS_ERR(target)) {
3948 rc = PTR_ERR(target);
3952 if (dt_object_exists(target)) {
3953 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
3955 rc = lfsck_namespace_double_scan_one(env, com,
3962 lfsck_object_put(env, target);
3965 down_write(&com->lc_sem);
3966 com->lc_new_checked++;
3967 com->lc_new_scanned++;
3968 ns->ln_fid_latest_scanned_phase2 = fid;
3970 ns->ln_objs_repaired_phase2++;
3972 ns->ln_objs_failed_phase2++;
3973 up_write(&com->lc_sem);
3975 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
3978 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
3979 cfs_time_current())) &&
3980 com->lc_new_checked != 0) {
3981 down_write(&com->lc_sem);
3982 ns->ln_run_time_phase2 +=
3983 cfs_duration_sec(cfs_time_current() +
3984 HALF_SEC - com->lc_time_last_checkpoint);
3985 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3986 ns->ln_objs_checked_phase2 += com->lc_new_checked;
3987 com->lc_new_checked = 0;
3988 rc = lfsck_namespace_store(env, com, false);
3989 up_write(&com->lc_sem);
3993 com->lc_time_last_checkpoint = cfs_time_current();
3994 com->lc_time_next_checkpoint =
3995 com->lc_time_last_checkpoint +
3996 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
3999 lfsck_control_speed_by_self(com);
4000 if (unlikely(!thread_is_running(thread)))
4003 rc = iops->next(env, di);
4012 iops->fini(env, di);
4014 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
4015 lfsck_lfsck2name(lfsck), rc);
4020 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
4021 struct lfsck_component *com,
4022 struct lfsck_position *pos)
4024 struct lfsck_assistant_data *lad = com->lc_data;
4025 struct lfsck_namespace_req *lnr;
4027 if (list_empty(&lad->lad_req_list))
4030 lnr = list_entry(lad->lad_req_list.next,
4031 struct lfsck_namespace_req,
4033 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
4034 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
4035 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
4038 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
4039 struct lfsck_component *com,
4042 struct lfsck_instance *lfsck = com->lc_lfsck;
4043 struct lfsck_namespace *ns = com->lc_file_ram;
4045 down_write(&com->lc_sem);
4046 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
4047 HALF_SEC - lfsck->li_time_last_checkpoint);
4048 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4049 ns->ln_objs_checked_phase2 += com->lc_new_checked;
4050 com->lc_new_checked = 0;
4053 if (ns->ln_flags & LF_INCOMPLETE)
4054 ns->ln_status = LS_PARTIAL;
4056 ns->ln_status = LS_COMPLETED;
4057 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
4058 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
4059 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
4060 ns->ln_success_count++;
4061 } else if (rc == 0) {
4062 ns->ln_status = lfsck->li_status;
4063 if (ns->ln_status == 0)
4064 ns->ln_status = LS_STOPPED;
4066 ns->ln_status = LS_FAILED;
4069 rc = lfsck_namespace_store(env, com, false);
4070 up_write(&com->lc_sem);
4075 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
4076 struct lfsck_component *com,
4077 struct lfsck_request *lr)
4082 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
4083 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
4084 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
4085 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
4086 .la_double_scan_result = lfsck_namespace_double_scan_result,
4087 .la_req_fini = lfsck_namespace_assistant_req_fini,
4088 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
4092 * Verify the specified linkEA entry for the given directory object.
4093 * If the object has no such linkEA entry or it has more other linkEA
4094 * entries, then re-generate the linkEA with the given information.
4096 * \param[in] env pointer to the thread context
4097 * \param[in] dev pointer to the dt_device
4098 * \param[in] obj pointer to the dt_object to be handled
4099 * \param[in] cname the name for the child in the parent directory
4100 * \param[in] pfid the parent directory's FID for the linkEA
4102 * \retval 0 for success
4103 * \retval negative error number on failure
4105 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
4106 struct dt_object *obj, const struct lu_name *cname,
4107 const struct lu_fid *pfid)
4109 struct linkea_data ldata = { 0 };
4110 struct lu_buf linkea_buf;
4113 int fl = LU_XATTR_CREATE;
4117 LASSERT(S_ISDIR(lfsck_object_type(obj)));
4119 rc = lfsck_links_read(env, obj, &ldata);
4120 if (rc == -ENODATA) {
4122 } else if (rc == 0) {
4123 fl = LU_XATTR_REPLACE;
4124 if (ldata.ld_leh->leh_reccount != 1) {
4127 rc = linkea_links_find(&ldata, cname, pfid);
4136 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
4140 rc = linkea_add_buf(&ldata, cname, pfid);
4144 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4145 ldata.ld_leh->leh_len);
4146 th = dt_trans_create(env, dev);
4148 RETURN(PTR_ERR(th));
4150 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
4151 XATTR_NAME_LINK, fl, th);
4155 rc = dt_trans_start_local(env, dev, th);
4159 dt_write_lock(env, obj, 0);
4160 rc = dt_xattr_set(env, obj, &linkea_buf,
4161 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
4162 dt_write_unlock(env, obj);
4167 dt_trans_stop(env, dev, th);
4172 * Get the name and parent directory's FID from the first linkEA entry.
4174 * \param[in] env pointer to the thread context
4175 * \param[in] obj pointer to the object which get linkEA from
4176 * \param[out] name pointer to the buffer to hold the name
4177 * in the first linkEA entry
4178 * \param[out] pfid pointer to the buffer to hold the parent
4179 * directory's FID in the first linkEA entry
4181 * \retval 0 for success
4182 * \retval negative error number on failure
4184 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
4185 char *name, struct lu_fid *pfid)
4187 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
4188 struct linkea_data ldata = { 0 };
4191 rc = lfsck_links_read(env, obj, &ldata);
4195 linkea_first_entry(&ldata);
4196 if (ldata.ld_lee == NULL)
4199 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4200 /* To guarantee the 'name' is terminated with '0'. */
4201 memcpy(name, cname->ln_name, cname->ln_namelen);
4202 name[cname->ln_namelen] = 0;
4208 * Remove the name entry from the parent directory.
4210 * No need to care about the object referenced by the name entry,
4211 * either the name entry is invalid or redundant, or the referenced
4212 * object has been processed has been or will be handled by others.
4214 * \param[in] env pointer to the thread context
4215 * \param[in] lfsck pointer to the lfsck instance
4216 * \param[in] parent pointer to the lost+found object
4217 * \param[in] name the name for the name entry to be removed
4218 * \param[in] type the type for the name entry to be removed
4220 * \retval 0 for success
4221 * \retval negative error number on failure
4223 int lfsck_remove_name_entry(const struct lu_env *env,
4224 struct lfsck_instance *lfsck,
4225 struct dt_object *parent,
4226 const char *name, __u32 type)
4228 struct dt_device *dev = lfsck->li_next;
4230 struct lustre_handle lh = { 0 };
4234 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4235 MDS_INODELOCK_UPDATE, LCK_EX);
4239 th = dt_trans_create(env, dev);
4241 GOTO(unlock, rc = PTR_ERR(th));
4243 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
4247 if (S_ISDIR(type)) {
4248 rc = dt_declare_ref_del(env, parent, th);
4253 rc = dt_trans_start(env, dev, th);
4257 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
4262 if (S_ISDIR(type)) {
4263 dt_write_lock(env, parent, 0);
4264 rc = dt_ref_del(env, parent, th);
4265 dt_write_unlock(env, parent);
4271 dt_trans_stop(env, dev, th);
4274 lfsck_ibits_unlock(&lh, LCK_EX);
4276 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
4277 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
4278 PFID(lfsck_dto2fid(parent)), name, type, rc);
4284 * Update the object's name entry with the given FID.
4286 * \param[in] env pointer to the thread context
4287 * \param[in] lfsck pointer to the lfsck instance
4288 * \param[in] parent pointer to the parent directory that holds
4290 * \param[in] name the name for the entry to be updated
4291 * \param[in] pfid the new PFID for the name entry
4292 * \param[in] type the type for the name entry to be updated
4294 * \retval 0 for success
4295 * \retval negative error number on failure
4297 int lfsck_update_name_entry(const struct lu_env *env,
4298 struct lfsck_instance *lfsck,
4299 struct dt_object *parent, const char *name,
4300 const struct lu_fid *pfid, __u32 type)
4302 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
4303 struct dt_device *dev = lfsck->li_next;
4304 struct lustre_handle lh = { 0 };
4310 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4311 MDS_INODELOCK_UPDATE, LCK_EX);
4315 th = dt_trans_create(env, dev);
4317 GOTO(unlock, rc = PTR_ERR(th));
4319 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
4323 rec->rec_type = type;
4324 rec->rec_fid = pfid;
4325 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
4326 (const struct dt_key *)name, th);
4330 rc = dt_declare_ref_add(env, parent, th);
4334 rc = dt_trans_start(env, dev, th);
4338 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
4340 if (rc == -ENOENT) {
4348 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
4349 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
4350 if (rc == 0 && S_ISDIR(type) && !exists) {
4351 dt_write_lock(env, parent, 0);
4352 rc = dt_ref_add(env, parent, th);
4353 dt_write_unlock(env, parent);
4359 dt_trans_stop(env, dev, th);
4362 lfsck_ibits_unlock(&lh, LCK_EX);
4364 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
4365 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
4366 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
4371 int lfsck_namespace_setup(const struct lu_env *env,
4372 struct lfsck_instance *lfsck)
4374 struct lfsck_component *com;
4375 struct lfsck_namespace *ns;
4376 struct dt_object *root = NULL;
4377 struct dt_object *obj;
4381 LASSERT(lfsck->li_master);
4387 INIT_LIST_HEAD(&com->lc_link);
4388 INIT_LIST_HEAD(&com->lc_link_dir);
4389 init_rwsem(&com->lc_sem);
4390 atomic_set(&com->lc_ref, 1);
4391 com->lc_lfsck = lfsck;
4392 com->lc_type = LFSCK_TYPE_NAMESPACE;
4393 com->lc_ops = &lfsck_namespace_ops;
4394 com->lc_data = lfsck_assistant_data_init(
4395 &lfsck_namespace_assistant_ops,
4397 if (com->lc_data == NULL)
4398 GOTO(out, rc = -ENOMEM);
4400 com->lc_file_size = sizeof(struct lfsck_namespace);
4401 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
4402 if (com->lc_file_ram == NULL)
4403 GOTO(out, rc = -ENOMEM);
4405 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
4406 if (com->lc_file_disk == NULL)
4407 GOTO(out, rc = -ENOMEM);
4409 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
4411 GOTO(out, rc = PTR_ERR(root));
4413 if (unlikely(!dt_try_as_dir(env, root)))
4414 GOTO(out, rc = -ENOTDIR);
4416 obj = local_index_find_or_create(env, lfsck->li_los, root,
4417 lfsck_namespace_name,
4418 S_IFREG | S_IRUGO | S_IWUSR,
4419 &dt_lfsck_features);
4421 GOTO(out, rc = PTR_ERR(obj));
4424 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
4428 rc = lfsck_namespace_load(env, com);
4430 rc = lfsck_namespace_reset(env, com, true);
4431 else if (rc == -ENODATA)
4432 rc = lfsck_namespace_init(env, com);
4436 ns = com->lc_file_ram;
4437 switch (ns->ln_status) {
4442 spin_lock(&lfsck->li_lock);
4443 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
4444 spin_unlock(&lfsck->li_lock);
4447 CERROR("%s: unknown lfsck_namespace status %d\n",
4448 lfsck_lfsck2name(lfsck), ns->ln_status);
4450 case LS_SCANNING_PHASE1:
4451 case LS_SCANNING_PHASE2:
4452 /* No need to store the status to disk right now.
4453 * If the system crashed before the status stored,
4454 * it will be loaded back when next time. */
4455 ns->ln_status = LS_CRASHED;
4459 spin_lock(&lfsck->li_lock);
4460 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
4461 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
4462 spin_unlock(&lfsck->li_lock);
4469 if (root != NULL && !IS_ERR(root))
4470 lu_object_put(env, &root->do_lu);
4472 lfsck_component_cleanup(env, com);
4473 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
4474 lfsck_lfsck2name(lfsck), rc);