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 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
120 &src->ln_fid_latest_scanned_phase2);
121 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
122 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
123 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
124 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
125 dst->ln_unknown_inconsistency =
126 le64_to_cpu(src->ln_unknown_inconsistency);
127 dst->ln_unmatched_pairs_repaired =
128 le64_to_cpu(src->ln_unmatched_pairs_repaired);
129 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
130 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
131 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
132 dst->ln_lost_dirent_repaired =
133 le64_to_cpu(src->ln_lost_dirent_repaired);
134 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
137 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
138 struct lfsck_namespace *src)
140 dst->ln_magic = cpu_to_le32(src->ln_magic);
141 dst->ln_status = cpu_to_le32(src->ln_status);
142 dst->ln_flags = cpu_to_le32(src->ln_flags);
143 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
144 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
145 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
146 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
147 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
148 dst->ln_time_last_checkpoint =
149 cpu_to_le64(src->ln_time_last_checkpoint);
150 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
151 &src->ln_pos_latest_start);
152 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
153 &src->ln_pos_last_checkpoint);
154 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
155 &src->ln_pos_first_inconsistent);
156 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
157 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
158 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
159 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
160 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
161 dst->ln_objs_repaired_phase2 =
162 cpu_to_le64(src->ln_objs_repaired_phase2);
163 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
164 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
165 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
166 &src->ln_fid_latest_scanned_phase2);
167 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
168 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
169 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
170 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
171 dst->ln_unknown_inconsistency =
172 cpu_to_le64(src->ln_unknown_inconsistency);
173 dst->ln_unmatched_pairs_repaired =
174 cpu_to_le64(src->ln_unmatched_pairs_repaired);
175 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
176 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
177 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
178 dst->ln_lost_dirent_repaired =
179 cpu_to_le64(src->ln_lost_dirent_repaired);
180 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
183 static void lfsck_namespace_record_failure(const struct lu_env *env,
184 struct lfsck_instance *lfsck,
185 struct lfsck_namespace *ns)
187 struct lfsck_position pos;
189 ns->ln_items_failed++;
190 lfsck_pos_fill(env, lfsck, &pos, false);
191 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
192 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
193 ns->ln_pos_first_inconsistent = pos;
195 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
196 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
197 lfsck_lfsck2name(lfsck),
198 ns->ln_pos_first_inconsistent.lp_oit_cookie,
199 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
200 ns->ln_pos_first_inconsistent.lp_dir_cookie);
205 * Load the MDT bitmap from the lfsck_namespace tracing file.
207 * \param[in] env pointer to the thread context
208 * \param[in] com pointer to the lfsck component
210 * \retval 0 for success
211 * \retval negative error number on failure or data corruption
213 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
214 struct lfsck_component *com)
216 struct dt_object *obj = com->lc_obj;
217 struct lfsck_assistant_data *lad = com->lc_data;
218 struct lfsck_namespace *ns = com->lc_file_ram;
219 cfs_bitmap_t *bitmap = lad->lad_bitmap;
225 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
227 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
229 nbits = ns->ln_bitmap_size;
231 if (unlikely(nbits < BITS_PER_LONG))
232 nbits = BITS_PER_LONG;
234 if (nbits > bitmap->size) {
235 __u32 new_bits = bitmap->size;
236 cfs_bitmap_t *new_bitmap;
238 while (new_bits < nbits)
241 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
242 if (new_bitmap == NULL)
245 lad->lad_bitmap = new_bitmap;
246 CFS_FREE_BITMAP(bitmap);
250 if (ns->ln_bitmap_size == 0) {
251 lad->lad_incomplete = 0;
252 CFS_RESET_BITMAP(bitmap);
257 size = (ns->ln_bitmap_size + 7) >> 3;
258 rc = dt_xattr_get(env, obj,
259 lfsck_buf_get(env, bitmap->data, size),
260 XATTR_NAME_LFSCK_BITMAP, BYPASS_CAPA);
262 RETURN(rc >= 0 ? -EINVAL : rc);
264 if (cfs_bitmap_check_empty(bitmap))
265 lad->lad_incomplete = 0;
267 lad->lad_incomplete = 1;
273 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
274 * \retval 0: succeed.
275 * \retval -ve: failed cases.
277 static int lfsck_namespace_load(const struct lu_env *env,
278 struct lfsck_component *com)
280 int len = com->lc_file_size;
283 rc = dt_xattr_get(env, com->lc_obj,
284 lfsck_buf_get(env, com->lc_file_disk, len),
285 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
287 struct lfsck_namespace *ns = com->lc_file_ram;
289 lfsck_namespace_le_to_cpu(ns,
290 (struct lfsck_namespace *)com->lc_file_disk);
291 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
292 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
293 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
294 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
299 } else if (rc != -ENODATA) {
300 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
301 "expected = %d: rc = %d\n",
302 lfsck_lfsck2name(com->lc_lfsck), len, rc);
309 static int lfsck_namespace_store(const struct lu_env *env,
310 struct lfsck_component *com)
312 struct dt_object *obj = com->lc_obj;
313 struct lfsck_instance *lfsck = com->lc_lfsck;
314 struct lfsck_namespace *ns = com->lc_file_ram;
315 struct lfsck_assistant_data *lad = com->lc_data;
316 cfs_bitmap_t *bitmap = NULL;
317 struct thandle *handle;
319 int len = com->lc_file_size;
324 bitmap = lad->lad_bitmap;
325 nbits = bitmap->size;
328 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
331 ns->ln_bitmap_size = nbits;
332 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
334 handle = dt_trans_create(env, lfsck->li_bottom);
336 GOTO(log, rc = PTR_ERR(handle));
338 rc = dt_declare_xattr_set(env, obj,
339 lfsck_buf_get(env, com->lc_file_disk, len),
340 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
344 if (bitmap != NULL) {
345 rc = dt_declare_xattr_set(env, obj,
346 lfsck_buf_get(env, bitmap->data, nbits >> 3),
347 XATTR_NAME_LFSCK_BITMAP, 0, handle);
352 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
356 rc = dt_xattr_set(env, obj,
357 lfsck_buf_get(env, com->lc_file_disk, len),
358 XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
359 if (rc == 0 && bitmap != NULL)
360 rc = dt_xattr_set(env, obj,
361 lfsck_buf_get(env, bitmap->data, nbits >> 3),
362 XATTR_NAME_LFSCK_BITMAP, 0, handle,
368 dt_trans_stop(env, lfsck->li_bottom, handle);
372 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
373 lfsck_lfsck2name(lfsck), rc);
377 static int lfsck_namespace_init(const struct lu_env *env,
378 struct lfsck_component *com)
380 struct lfsck_namespace *ns = com->lc_file_ram;
383 memset(ns, 0, sizeof(*ns));
384 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
385 ns->ln_status = LS_INIT;
386 down_write(&com->lc_sem);
387 rc = lfsck_namespace_store(env, com);
388 up_write(&com->lc_sem);
393 * Update the namespace LFSCK tracing file for the given @fid
395 * \param[in] env pointer to the thread context
396 * \param[in] com pointer to the lfsck component
397 * \param[in] fid the fid which flags to be updated in the lfsck
399 * \param[in] add true if add new flags, otherwise remove flags
401 * \retval 0 for succeed or nothing to be done
402 * \retval negative error number on failure
404 int lfsck_namespace_trace_update(const struct lu_env *env,
405 struct lfsck_component *com,
406 const struct lu_fid *fid,
407 const __u8 flags, bool add)
409 struct lfsck_instance *lfsck = com->lc_lfsck;
410 struct dt_object *obj = com->lc_obj;
411 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
412 struct dt_device *dev = lfsck->li_bottom;
413 struct thandle *th = NULL;
421 down_write(&com->lc_sem);
422 fid_cpu_to_be(key, fid);
423 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
424 (const struct dt_key *)key, BYPASS_CAPA);
427 GOTO(unlock, rc = 0);
431 } else if (rc == 0) {
433 if ((old & flags) == flags)
434 GOTO(unlock, rc = 0);
438 if ((old & flags) == 0)
439 GOTO(unlock, rc = 0);
447 th = dt_trans_create(env, dev);
449 GOTO(log, rc = PTR_ERR(th));
452 rc = dt_declare_delete(env, obj,
453 (const struct dt_key *)key, th);
459 rc = dt_declare_insert(env, obj,
460 (const struct dt_rec *)&new,
461 (const struct dt_key *)key, th);
466 rc = dt_trans_start_local(env, dev, th);
471 rc = dt_delete(env, obj, (const struct dt_key *)key,
478 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
479 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
487 if (th != NULL && !IS_ERR(th))
488 dt_trans_stop(env, dev, th);
490 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
491 "tracing file, flags %x, old %x, new %x: rc = %d\n",
492 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
493 (__u32)flags, (__u32)old, (__u32)new, rc);
496 up_write(&com->lc_sem);
501 static int lfsck_namespace_check_exist(const struct lu_env *env,
502 struct dt_object *dir,
503 struct dt_object *obj, const char *name)
505 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
509 if (unlikely(lfsck_is_dead_obj(obj)))
510 RETURN(LFSCK_NAMEENTRY_DEAD);
512 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
513 (const struct dt_key *)name, BYPASS_CAPA);
515 RETURN(LFSCK_NAMEENTRY_REMOVED);
520 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
521 RETURN(LFSCK_NAMEENTRY_RECREATED);
526 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
527 struct dt_object *obj,
528 struct thandle *handle)
532 /* For destroying all invalid linkEA entries. */
533 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
537 /* For insert new linkEA entry. */
538 rc = dt_declare_xattr_set(env, obj,
539 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
540 XATTR_NAME_LINK, 0, handle);
544 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
545 struct linkea_data *ldata)
549 if (ldata->ld_buf->lb_buf == NULL)
552 if (!dt_object_exists(obj))
555 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
557 /* Buf was too small, figure out what we need. */
558 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
563 lu_buf_realloc(ldata->ld_buf, rc);
564 if (ldata->ld_buf->lb_buf == NULL)
567 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
572 rc = linkea_init(ldata);
578 * Remove linkEA for the given object.
580 * The caller should take the ldlm lock before the calling.
582 * \param[in] env pointer to the thread context
583 * \param[in] com pointer to the lfsck component
584 * \param[in] obj pointer to the dt_object to be handled
586 * \retval 0 for repaired cases
587 * \retval negative error number on failure
589 static int lfsck_namespace_links_remove(const struct lu_env *env,
590 struct lfsck_component *com,
591 struct dt_object *obj)
593 struct lfsck_instance *lfsck = com->lc_lfsck;
594 struct dt_device *dev = lfsck->li_bottom;
595 struct thandle *th = NULL;
599 LASSERT(dt_object_remote(obj) == 0);
601 th = dt_trans_create(env, dev);
603 GOTO(log, rc = PTR_ERR(th));
605 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
609 rc = dt_trans_start_local(env, dev, th);
613 dt_write_lock(env, obj, 0);
614 if (unlikely(lfsck_is_dead_obj(obj)))
615 GOTO(unlock, rc = -ENOENT);
617 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
618 GOTO(unlock, rc = 0);
620 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
625 dt_write_unlock(env, obj);
628 dt_trans_stop(env, dev, th);
631 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
632 "for the object "DFID": rc = %d\n",
633 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
636 struct lfsck_namespace *ns = com->lc_file_ram;
638 ns->ln_flags |= LF_INCONSISTENT;
644 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
645 struct linkea_data *ldata, struct thandle *handle)
647 const struct lu_buf *buf = lfsck_buf_get_const(env,
648 ldata->ld_buf->lb_buf,
649 ldata->ld_leh->leh_len);
651 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
655 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
656 struct lu_name *cname,
660 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
661 /* To guarantee the 'name' is terminated with '0'. */
662 memcpy(buf, cname->ln_name, cname->ln_namelen);
663 buf[cname->ln_namelen] = 0;
664 cname->ln_name = buf;
667 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
668 struct lu_name *cname,
672 struct link_ea_entry *oldlee;
676 oldlee = ldata->ld_lee;
677 oldlen = ldata->ld_reclen;
678 linkea_next_entry(ldata);
679 while (ldata->ld_lee != NULL) {
680 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
681 ldata->ld_lee->lee_reclen[1];
682 if (unlikely(ldata->ld_reclen == oldlen &&
683 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
688 linkea_del_buf(ldata, cname);
690 linkea_next_entry(ldata);
693 ldata->ld_lee = oldlee;
694 ldata->ld_reclen = oldlen;
700 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
702 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
703 * with the given type to generate the name, the detailed rules for name
704 * have been described as following.
706 * The function also generates the linkEA corresponding to the name entry
707 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
709 * \param[in] env pointer to the thread context
710 * \param[in] com pointer to the lfsck component
711 * \param[in] orphan pointer to the orphan MDT-object
712 * \param[in] infix additional information for the orphan name, such as
713 * the FID for original
714 * \param[in] type the type for describing why the orphan MDT-object is
715 * created. The rules are as following:
717 * type "D": The MDT-object is a directory, it may knows its parent
718 * but because there is no valid linkEA, the LFSCK cannot
719 * know where to put it back to the namespace.
720 * type "O": The MDT-object has no linkEA, and there is no name
721 * entry that references the MDT-object.
723 * \see lfsck_layout_recreate_parent() for more types.
725 * The orphan name will be like:
726 * ${FID}-${infix}-${type}-${conflict_version}
728 * \param[out] count if some others inserted some linkEA entries by race,
729 * then return the linkEA entries count.
731 * \retval positive number for repaired cases
732 * \retval 0 if needs to repair nothing
733 * \retval negative error number on failure
735 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
736 struct lfsck_component *com,
737 struct dt_object *orphan,
738 const char *infix, const char *type,
741 struct lfsck_thread_info *info = lfsck_env_info(env);
742 struct lu_name *cname = &info->lti_name;
743 struct dt_insert_rec *rec = &info->lti_dt_rec;
744 struct lu_fid *tfid = &info->lti_fid5;
745 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
746 const struct lu_fid *pfid;
747 struct lfsck_instance *lfsck = com->lc_lfsck;
748 struct dt_device *dev = lfsck->li_bottom;
749 struct dt_object *parent;
750 struct thandle *th = NULL;
751 struct lustre_handle plh = { 0 };
752 struct lustre_handle clh = { 0 };
753 struct linkea_data ldata = { 0 };
754 struct lu_buf linkea_buf;
761 cname->ln_name = NULL;
762 /* Create .lustre/lost+found/MDTxxxx when needed. */
763 if (unlikely(lfsck->li_lpf_obj == NULL)) {
764 rc = lfsck_create_lpf(env, lfsck);
769 parent = lfsck->li_lpf_obj;
770 pfid = lfsck_dto2fid(parent);
772 /* Hold update lock on the parent to prevent others to access. */
773 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
774 MDS_INODELOCK_UPDATE, LCK_EX);
779 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
780 PFID(cfid), infix, type, idx++);
781 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
782 (const struct dt_key *)info->lti_key,
784 if (rc != 0 && rc != -ENOENT)
787 if (unlikely(rc == 0 && lu_fid_eq(cfid, tfid)))
789 } while (rc == 0 && !exist);
791 cname->ln_name = info->lti_key;
792 cname->ln_namelen = namelen;
793 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
797 rc = linkea_add_buf(&ldata, cname, pfid);
801 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
802 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
807 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
808 ldata.ld_leh->leh_len);
809 th = dt_trans_create(env, dev);
811 GOTO(log, rc = PTR_ERR(th));
813 if (S_ISDIR(lfsck_object_type(orphan))) {
814 rc = dt_declare_delete(env, orphan,
815 (const struct dt_key *)dotdot, th);
819 rec->rec_type = S_IFDIR;
821 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
822 (const struct dt_key *)dotdot, th);
827 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
828 XATTR_NAME_LINK, 0, th);
833 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
835 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
836 (const struct dt_key *)cname->ln_name,
841 if (S_ISDIR(rec->rec_type)) {
842 rc = dt_declare_ref_add(env, parent, th);
848 rc = dt_trans_start_local(env, dev, th);
852 dt_write_lock(env, orphan, 0);
853 rc = lfsck_links_read(env, orphan, &ldata);
854 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
855 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
856 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
857 GOTO(unlock, rc = 1);
859 if (S_ISDIR(lfsck_object_type(orphan))) {
860 rc = dt_delete(env, orphan,
861 (const struct dt_key *)dotdot, th,
866 rec->rec_type = S_IFDIR;
868 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
869 (const struct dt_key *)dotdot, th,
875 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
878 if (rc == 0 && count != NULL)
879 *count = ldata.ld_leh->leh_reccount;
883 dt_write_unlock(env, orphan);
885 if (rc == 0 && !exist) {
886 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
888 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
889 (const struct dt_key *)cname->ln_name,
891 if (rc == 0 && S_ISDIR(rec->rec_type)) {
892 dt_write_lock(env, parent, 0);
893 rc = dt_ref_add(env, parent, th);
894 dt_write_unlock(env, parent);
898 GOTO(stop, rc = (rc == 0 ? 1 : rc));
901 dt_write_unlock(env, orphan);
904 dt_trans_stop(env, dev, th);
907 lfsck_ibits_unlock(&clh, LCK_EX);
908 lfsck_ibits_unlock(&plh, LCK_EX);
909 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
910 "object "DFID", name = %s: rc = %d\n",
911 lfsck_lfsck2name(lfsck), PFID(cfid),
912 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
915 struct lfsck_namespace *ns = com->lc_file_ram;
917 ns->ln_flags |= LF_INCONSISTENT;
924 * Add the specified name entry back to namespace.
926 * If there is a linkEA entry that back references a name entry under
927 * some parent directory, but such parent directory does not have the
928 * claimed name entry. On the other hand, the linkEA entries count is
929 * not larger than the MDT-object's hard link count. Under such case,
930 * it is quite possible that the name entry is lost. Then the LFSCK
931 * should add the name entry back to the namespace.
933 * \param[in] env pointer to the thread context
934 * \param[in] com pointer to the lfsck component
935 * \param[in] parent pointer to the directory under which the name entry
936 * will be inserted into
937 * \param[in] child pointer to the object referenced by the name entry
938 * that to be inserted into the parent
939 * \param[in] name the name for the child in the parent directory
941 * \retval positive number for repaired cases
942 * \retval 0 if nothing to be repaired
943 * \retval negative error number on failure
945 static int lfsck_namespace_insert_normal(const struct lu_env *env,
946 struct lfsck_component *com,
947 struct dt_object *parent,
948 struct dt_object *child,
951 struct lfsck_thread_info *info = lfsck_env_info(env);
952 struct lu_attr *la = &info->lti_la;
953 struct dt_insert_rec *rec = &info->lti_dt_rec;
954 struct lfsck_instance *lfsck = com->lc_lfsck;
955 struct dt_device *dev = lfsck->li_next;
956 struct thandle *th = NULL;
957 struct lustre_handle lh = { 0 };
961 if (unlikely(!dt_try_as_dir(env, parent)))
962 GOTO(log, rc = -ENOTDIR);
964 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
967 /* Hold update lock on the parent to prevent others to access. */
968 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
969 MDS_INODELOCK_UPDATE, LCK_EX);
973 th = dt_trans_create(env, dev);
975 GOTO(unlock, rc = PTR_ERR(th));
977 rec->rec_type = lfsck_object_type(child) & S_IFMT;
978 rec->rec_fid = lfsck_dto2fid(child);
979 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
980 (const struct dt_key *)name, th);
984 if (S_ISDIR(rec->rec_type)) {
985 rc = dt_declare_ref_add(env, parent, th);
990 memset(la, 0, sizeof(*la));
991 la->la_ctime = cfs_time_current_sec();
992 la->la_valid = LA_CTIME;
993 rc = dt_declare_attr_set(env, parent, la, th);
997 rc = dt_trans_start_local(env, dev, th);
1001 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1002 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1006 if (S_ISDIR(rec->rec_type)) {
1007 dt_write_lock(env, parent, 0);
1008 rc = dt_ref_add(env, parent, th);
1009 dt_write_unlock(env, parent);
1014 la->la_ctime = cfs_time_current_sec();
1015 rc = dt_attr_set(env, parent, la, th, BYPASS_CAPA);
1017 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1020 dt_trans_stop(env, dev, th);
1023 lfsck_ibits_unlock(&lh, LCK_EX);
1026 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1027 "the name %s and type %o to the parent "DFID": rc = %d\n",
1028 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1029 lfsck_object_type(child) & S_IFMT,
1030 PFID(lfsck_dto2fid(parent)), rc);
1033 struct lfsck_namespace *ns = com->lc_file_ram;
1035 ns->ln_flags |= LF_INCONSISTENT;
1037 ns->ln_lost_dirent_repaired++;
1044 * Create the specified orphan MDT-object on remote MDT.
1046 * The LFSCK instance on this MDT will send LFSCK RPC to remote MDT to
1047 * ask the remote LFSCK instance to create the specified orphan object
1048 * under .lustre/lost+found/MDTxxxx/ directory with the name:
1049 * ${FID}-P-${conflict_version}.
1051 * \param[in] env pointer to the thread context
1052 * \param[in] com pointer to the lfsck component
1053 * \param[in] orphan pointer to the orphan MDT-object
1054 * \param[in] type the orphan's type to be created
1056 * type "P": The orphan object to be created was a parent directory
1057 * of some MDT-object which linkEA shows that the @orphan
1058 * object is missing.
1060 * \see lfsck_layout_recreate_parent() for more types.
1062 * \retval positive number for repaired cases
1063 * \retval 0 if needs to repair nothing
1064 * \retval negative error number on failure
1066 static int lfsck_namespace_create_orphan_remote(const struct lu_env *env,
1067 struct lfsck_component *com,
1068 struct dt_object *orphan,
1071 struct lfsck_thread_info *info = lfsck_env_info(env);
1072 struct lfsck_request *lr = &info->lti_lr;
1073 struct lu_seq_range *range = &info->lti_range;
1074 const struct lu_fid *fid = lfsck_dto2fid(orphan);
1075 struct lfsck_namespace *ns = com->lc_file_ram;
1076 struct lfsck_instance *lfsck = com->lc_lfsck;
1077 struct seq_server_site *ss =
1078 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1079 struct lfsck_tgt_desc *ltd = NULL;
1080 struct ptlrpc_request *req = NULL;
1084 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1087 fld_range_set_mdt(range);
1088 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1092 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, range->lsr_index);
1094 ns->ln_flags |= LF_INCOMPLETE;
1096 GOTO(out, rc = -ENODEV);
1099 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1102 GOTO(out, rc = -ENOMEM);
1104 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1106 ptlrpc_request_free(req);
1111 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1112 memset(lr, 0, sizeof(*lr));
1113 lr->lr_event = LE_CREATE_ORPHAN;
1114 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1115 lr->lr_active = LFSCK_TYPE_NAMESPACE;
1119 ptlrpc_request_set_replen(req);
1120 rc = ptlrpc_queue_wait(req);
1121 ptlrpc_req_finished(req);
1125 else if (rc == -EEXIST)
1131 CDEBUG(D_LFSCK, "%s: namespace LFSCK create object "
1132 DFID" on the MDT %x remotely: rc = %d\n",
1133 lfsck_lfsck2name(lfsck), PFID(fid),
1134 ltd != NULL ? ltd->ltd_index : -1, rc);
1143 * Create the specified orphan MDT-object locally.
1145 * For the case that the parent MDT-object stored in some MDT-object's
1146 * linkEA entry is lost, the LFSCK will re-create the parent object as
1147 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1148 * with the name ${FID}-P-${conflict_version}.
1150 * \param[in] env pointer to the thread context
1151 * \param[in] com pointer to the lfsck component
1152 * \param[in] orphan pointer to the orphan MDT-object to be created
1153 * \param[in] type the orphan's type to be created
1155 * type "P": The orphan object to be created was a parent directory
1156 * of some MDT-object which linkEA shows that the @orphan
1157 * object is missing.
1159 * \see lfsck_layout_recreate_parent() for more types.
1161 * \retval positive number for repaired cases
1162 * \retval negative error number on failure
1164 static int lfsck_namespace_create_orphan_local(const struct lu_env *env,
1165 struct lfsck_component *com,
1166 struct dt_object *orphan,
1169 struct lfsck_thread_info *info = lfsck_env_info(env);
1170 struct lu_attr *la = &info->lti_la;
1171 struct dt_allocation_hint *hint = &info->lti_hint;
1172 struct dt_object_format *dof = &info->lti_dof;
1173 struct lu_name *cname = &info->lti_name2;
1174 struct dt_insert_rec *rec = &info->lti_dt_rec;
1175 struct lu_fid *tfid = &info->lti_fid;
1176 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1177 const struct lu_fid *pfid;
1178 struct lfsck_instance *lfsck = com->lc_lfsck;
1179 struct dt_device *dev = lfsck->li_bottom;
1180 struct dt_object *parent = NULL;
1181 struct dt_object *child = NULL;
1182 struct thandle *th = NULL;
1183 struct lustre_handle lh = { 0 };
1184 struct linkea_data ldata = { 0 };
1185 struct lu_buf linkea_buf;
1192 LASSERT(!dt_object_exists(orphan));
1193 LASSERT(!dt_object_remote(orphan));
1195 /* @orphan maybe not attached to lfsck->li_bottom */
1196 child = lfsck_object_find_by_dev(env, dev, cfid);
1198 GOTO(log, rc = PTR_ERR(child));
1200 cname->ln_name = NULL;
1201 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1204 /* Create .lustre/lost+found/MDTxxxx when needed. */
1205 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1206 rc = lfsck_create_lpf(env, lfsck);
1211 parent = lfsck->li_lpf_obj;
1212 pfid = lfsck_dto2fid(parent);
1214 /* Hold update lock on the parent to prevent others to access. */
1215 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1216 MDS_INODELOCK_UPDATE, LCK_EX);
1221 namelen = snprintf(name, 31, DFID"-P-%d",
1223 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1224 (const struct dt_key *)name, BYPASS_CAPA);
1225 if (rc != 0 && rc != -ENOENT)
1229 cname->ln_name = name;
1230 cname->ln_namelen = namelen;
1232 memset(la, 0, sizeof(*la));
1233 la->la_mode = type | (S_ISDIR(type) ? 0700 : 0600);
1234 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1235 LA_ATIME | LA_MTIME | LA_CTIME;
1237 child->do_ops->do_ah_init(env, hint, parent, child,
1238 la->la_mode & S_IFMT);
1240 memset(dof, 0, sizeof(*dof));
1241 dof->dof_type = dt_mode_to_dft(type);
1243 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1247 rc = linkea_add_buf(&ldata, cname, pfid);
1251 th = dt_trans_create(env, dev);
1253 GOTO(unlock1, rc = PTR_ERR(th));
1255 rc = dt_declare_create(env, child, la, hint, dof, th);
1256 if (rc == 0 && S_ISDIR(type))
1257 rc = dt_declare_ref_add(env, child, th);
1262 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1263 ldata.ld_leh->leh_len);
1264 rc = dt_declare_xattr_set(env, child, &linkea_buf,
1265 XATTR_NAME_LINK, 0, th);
1269 rec->rec_type = type;
1270 rec->rec_fid = cfid;
1271 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1272 (const struct dt_key *)name, th);
1273 if (rc == 0 && S_ISDIR(type))
1274 rc = dt_declare_ref_add(env, parent, th);
1279 rc = dt_trans_start_local(env, dev, th);
1283 dt_write_lock(env, child, 0);
1284 rc = dt_create(env, child, la, hint, dof, th);
1288 if (S_ISDIR(type)) {
1289 if (unlikely(!dt_try_as_dir(env, child)))
1290 GOTO(unlock2, rc = -ENOTDIR);
1292 rec->rec_type = S_IFDIR;
1293 rec->rec_fid = cfid;
1294 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1295 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1299 rec->rec_fid = pfid;
1300 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1301 (const struct dt_key *)dotdot, th,
1306 rc = dt_ref_add(env, child, th);
1311 rc = dt_xattr_set(env, child, &linkea_buf,
1312 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1313 dt_write_unlock(env, child);
1317 rec->rec_type = type;
1318 rec->rec_fid = cfid;
1319 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1320 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1321 if (rc == 0 && S_ISDIR(type)) {
1322 dt_write_lock(env, parent, 0);
1323 rc = dt_ref_add(env, parent, th);
1324 dt_write_unlock(env, parent);
1327 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1330 dt_write_unlock(env, child);
1333 dt_trans_stop(env, dev, th);
1336 lfsck_ibits_unlock(&lh, LCK_EX);
1339 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan locally for "
1340 "the object "DFID", name = %s, type %o: rc = %d\n",
1341 lfsck_lfsck2name(lfsck), PFID(cfid),
1342 cname->ln_name != NULL ? cname->ln_name : "<NULL>", type, rc);
1344 if (child != NULL && !IS_ERR(child))
1345 lfsck_object_put(env, child);
1351 * Create the specified orphan MDT-object.
1353 * For the case that the parent MDT-object stored in some MDT-object's
1354 * linkEA entry is lost, the LFSCK will re-create the parent object as
1355 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1356 * with the name: ${FID}-P-${conflict_version}.
1358 * \param[in] env pointer to the thread context
1359 * \param[in] com pointer to the lfsck component
1360 * \param[in] orphan pointer to the orphan MDT-object
1362 * type "P": The orphan object to be created was a parent directory
1363 * of some MDT-object which linkEA shows that the @orphan
1364 * object is missing.
1366 * \see lfsck_layout_recreate_parent() for more types.
1368 * \retval positive number for repaired cases
1369 * \retval 0 if needs to repair nothing
1370 * \retval negative error number on failure
1372 static int lfsck_namespace_create_orphan(const struct lu_env *env,
1373 struct lfsck_component *com,
1374 struct dt_object *orphan)
1376 struct lfsck_namespace *ns = com->lc_file_ram;
1379 if (dt_object_remote(orphan))
1380 rc = lfsck_namespace_create_orphan_remote(env, com, orphan,
1383 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
1387 ns->ln_flags |= LF_INCONSISTENT;
1393 * Remove the specified entry from the linkEA.
1395 * Locate the linkEA entry with the given @cname and @pfid, then
1396 * remove this entry or the other entries those are repeated with
1399 * \param[in] env pointer to the thread context
1400 * \param[in] com pointer to the lfsck component
1401 * \param[in] obj pointer to the dt_object to be handled
1402 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1403 * \param[in] cname the name for the child in the parent directory
1404 * \param[in] pfid the parent directory's FID for the linkEA
1405 * \param[in] next if true, then remove the first found linkEA
1406 * entry, and move the ldata->ld_lee to next entry
1408 * \retval positive number for repaired cases
1409 * \retval 0 if nothing to be repaired
1410 * \retval negative error number on failure
1412 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1413 struct lfsck_component *com,
1414 struct dt_object *obj,
1415 struct linkea_data *ldata,
1416 struct lu_name *cname,
1417 struct lu_fid *pfid,
1420 struct lfsck_instance *lfsck = com->lc_lfsck;
1421 struct dt_device *dev = lfsck->li_bottom;
1422 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1423 struct thandle *th = NULL;
1424 struct lustre_handle lh = { 0 };
1425 struct linkea_data ldata_new = { 0 };
1426 struct lu_buf linkea_buf;
1430 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1431 MDS_INODELOCK_UPDATE |
1432 MDS_INODELOCK_XATTR, LCK_EX);
1437 linkea_del_buf(ldata, cname);
1439 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1441 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1442 ldata->ld_leh->leh_len);
1445 th = dt_trans_create(env, dev);
1447 GOTO(unlock1, rc = PTR_ERR(th));
1449 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1450 XATTR_NAME_LINK, 0, th);
1454 rc = dt_trans_start_local(env, dev, th);
1458 dt_write_lock(env, obj, 0);
1459 if (unlikely(lfsck_is_dead_obj(obj)))
1460 GOTO(unlock2, rc = -ENOENT);
1462 rc = lfsck_links_read2(env, obj, &ldata_new);
1466 /* The specified linkEA entry has been removed by race. */
1467 rc = linkea_links_find(&ldata_new, cname, pfid);
1469 GOTO(unlock2, rc = 0);
1471 if (bk->lb_param & LPF_DRYRUN)
1472 GOTO(unlock2, rc = 1);
1475 linkea_del_buf(&ldata_new, cname);
1477 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1480 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1481 dt_write_unlock(env, obj);
1482 dt_trans_stop(env, dev, th);
1483 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1484 ldata_new.ld_leh->leh_len);
1488 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1489 ldata_new.ld_leh->leh_len);
1490 rc = dt_xattr_set(env, obj, &linkea_buf,
1491 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1493 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1496 dt_write_unlock(env, obj);
1499 dt_trans_stop(env, dev, th);
1502 lfsck_ibits_unlock(&lh, LCK_EX);
1505 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1506 "for the object: "DFID", parent "DFID", name %.*s\n",
1507 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1508 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1512 struct lfsck_namespace *ns = com->lc_file_ram;
1514 ns->ln_flags |= LF_INCONSISTENT;
1521 * Conditionally remove the specified entry from the linkEA.
1523 * Take the parent lock firstly, then check whether the specified
1524 * name entry exists or not: if yes, do nothing; otherwise, call
1525 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1527 * \param[in] env pointer to the thread context
1528 * \param[in] com pointer to the lfsck component
1529 * \param[in] parent pointer to the parent directory
1530 * \param[in] child pointer to the child object that holds the linkEA
1531 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1532 * \param[in] cname the name for the child in the parent directory
1533 * \param[in] pfid the parent directory's FID for the linkEA
1535 * \retval positive number for repaired cases
1536 * \retval 0 if nothing to be repaired
1537 * \retval negative error number on failure
1539 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1540 struct lfsck_component *com,
1541 struct dt_object *parent,
1542 struct dt_object *child,
1543 struct linkea_data *ldata,
1544 struct lu_name *cname,
1545 struct lu_fid *pfid)
1547 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1548 struct lustre_handle lh = { 0 };
1552 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1553 MDS_INODELOCK_UPDATE, LCK_EX);
1557 dt_read_lock(env, parent, 0);
1558 if (unlikely(lfsck_is_dead_obj(parent))) {
1559 dt_read_unlock(env, parent);
1560 lfsck_ibits_unlock(&lh, LCK_EX);
1561 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1567 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1568 (const struct dt_key *)cname->ln_name,
1570 dt_read_unlock(env, parent);
1572 /* It is safe to release the ldlm lock, because when the logic come
1573 * here, we have got all the needed information above whether the
1574 * linkEA entry is valid or not. It is not important that others
1575 * may add new linkEA entry after the ldlm lock released. If other
1576 * has removed the specified linkEA entry by race, then it is OK,
1577 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1579 lfsck_ibits_unlock(&lh, LCK_EX);
1580 if (rc == -ENOENT) {
1581 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1590 /* The LFSCK just found some internal status of cross-MDTs
1591 * create operation. That is normal. */
1592 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1593 linkea_next_entry(ldata);
1598 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1605 * Conditionally replace name entry in the parent.
1607 * As required, the LFSCK may re-create the lost MDT-object for dangling
1608 * name entry, but such repairing may be wrong because of bad FID in the
1609 * name entry. As the LFSCK processing, the real MDT-object may be found,
1610 * then the LFSCK should check whether the former re-created MDT-object
1611 * has been modified or not, if not, then destroy it and update the name
1612 * entry in the parent to reference the real MDT-object.
1614 * \param[in] env pointer to the thread context
1615 * \param[in] com pointer to the lfsck component
1616 * \param[in] parent pointer to the parent directory
1617 * \param[in] child pointer to the MDT-object that may be the real
1618 * MDT-object corresponding to the name entry in parent
1619 * \param[in] cfid the current FID in the name entry
1620 * \param[in] cname contains the name of the child in the parent directory
1622 * \retval positive number for repaired cases
1623 * \retval 0 if nothing to be repaired
1624 * \retval negative error number on failure
1626 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1627 struct lfsck_component *com,
1628 struct dt_object *parent,
1629 struct dt_object *child,
1630 const struct lu_fid *cfid,
1631 const struct lu_name *cname)
1633 struct lfsck_thread_info *info = lfsck_env_info(env);
1634 struct lu_fid *tfid = &info->lti_fid5;
1635 struct lu_attr *la = &info->lti_la;
1636 struct dt_insert_rec *rec = &info->lti_dt_rec;
1637 struct lfsck_instance *lfsck = com->lc_lfsck;
1638 struct dt_device *dev = lfsck->li_next;
1639 const char *name = cname->ln_name;
1640 struct dt_object *obj = NULL;
1641 struct lustre_handle plh = { 0 };
1642 struct lustre_handle clh = { 0 };
1643 struct linkea_data ldata = { 0 };
1644 struct thandle *th = NULL;
1649 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1650 MDS_INODELOCK_UPDATE, LCK_EX);
1654 if (!fid_is_sane(cfid)) {
1659 obj = lfsck_object_find(env, lfsck, cfid);
1662 if (rc == -ENOENT) {
1670 if (!dt_object_exists(obj)) {
1675 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1676 (const struct dt_key *)name, BYPASS_CAPA);
1677 if (rc == -ENOENT) {
1685 /* Someone changed the name entry, cannot replace it. */
1686 if (!lu_fid_eq(cfid, tfid))
1689 /* lock the object to be destroyed. */
1690 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1691 MDS_INODELOCK_UPDATE |
1692 MDS_INODELOCK_XATTR, LCK_EX);
1696 if (unlikely(lfsck_is_dead_obj(obj))) {
1701 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1705 /* The object has been modified by other(s), or it is not created by
1706 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1707 if (la->la_ctime != 0)
1710 if (S_ISREG(la->la_mode)) {
1711 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1713 /* If someone has created related OST-object(s),
1715 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1716 GOTO(log, rc = (rc > 0 ? 0 : rc));
1720 dt_read_lock(env, child, 0);
1721 rc = lfsck_links_read2(env, child, &ldata);
1722 dt_read_unlock(env, child);
1724 /* Someone changed the child, no need to replace. */
1731 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1732 /* Someone moved the child, no need to replace. */
1736 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1739 th = dt_trans_create(env, dev);
1741 GOTO(log, rc = PTR_ERR(th));
1744 rc = dt_declare_destroy(env, obj, th);
1749 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1753 rec->rec_type = S_IFDIR;
1754 rec->rec_fid = lfsck_dto2fid(child);
1755 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1756 (const struct dt_key *)name, th);
1760 rc = dt_trans_start(env, dev, th);
1765 rc = dt_destroy(env, obj, th);
1770 /* The old name entry maybe not exist. */
1771 dt_delete(env, parent, (const struct dt_key *)name, th,
1774 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1775 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1777 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1780 dt_trans_stop(env, dev, th);
1783 lfsck_ibits_unlock(&clh, LCK_EX);
1784 lfsck_ibits_unlock(&plh, LCK_EX);
1785 if (obj != NULL && !IS_ERR(obj))
1786 lfsck_object_put(env, obj);
1788 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1789 "object "DFID" because of conflict with the object "DFID
1790 " under the parent "DFID" with name %s: rc = %d\n",
1791 lfsck_lfsck2name(lfsck), PFID(cfid),
1792 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1799 * Overwrite the linkEA for the object with the given ldata.
1801 * The caller should take the ldlm lock before the calling.
1803 * \param[in] env pointer to the thread context
1804 * \param[in] com pointer to the lfsck component
1805 * \param[in] obj pointer to the dt_object to be handled
1806 * \param[in] ldata pointer to the new linkEA data
1808 * \retval positive number for repaired cases
1809 * \retval 0 if nothing to be repaired
1810 * \retval negative error number on failure
1812 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1813 struct lfsck_component *com,
1814 struct dt_object *obj,
1815 struct linkea_data *ldata)
1817 struct lfsck_instance *lfsck = com->lc_lfsck;
1818 struct dt_device *dev = lfsck->li_bottom;
1819 struct thandle *th = NULL;
1820 struct lu_buf linkea_buf;
1824 LASSERT(!dt_object_remote(obj));
1826 th = dt_trans_create(env, dev);
1828 GOTO(log, rc = PTR_ERR(th));
1830 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1831 ldata->ld_leh->leh_len);
1832 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1833 XATTR_NAME_LINK, 0, th);
1837 rc = dt_trans_start_local(env, dev, th);
1841 dt_write_lock(env, obj, 0);
1842 if (unlikely(lfsck_is_dead_obj(obj)))
1843 GOTO(unlock, rc = 0);
1845 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1846 GOTO(unlock, rc = 1);
1848 rc = dt_xattr_set(env, obj, &linkea_buf,
1849 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1851 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1854 dt_write_unlock(env, obj);
1857 dt_trans_stop(env, dev, th);
1860 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1861 "object "DFID": rc = %d\n",
1862 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1865 struct lfsck_namespace *ns = com->lc_file_ram;
1867 ns->ln_flags |= LF_INCONSISTENT;
1874 * Repair invalid name entry.
1876 * If the name entry contains invalid information, such as bad file type
1877 * or (and) corrupted object FID, then either remove the name entry or
1878 * udpate the name entry with the given (right) information.
1880 * \param[in] env pointer to the thread context
1881 * \param[in] com pointer to the lfsck component
1882 * \param[in] parent pointer to the parent directory
1883 * \param[in] child pointer to the object referenced by the name entry
1884 * \param[in] name the old name of the child under the parent directory
1885 * \param[in] name2 the new name of the child under the parent directory
1886 * \param[in] type the type claimed by the name entry
1887 * \param[in] update update the name entry if true; otherwise, remove it
1888 * \param[in] dec decrease the parent nlink count if true
1890 * \retval positive number for repaired successfully
1891 * \retval 0 if nothing to be repaired
1892 * \retval negative error number on failure
1894 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1895 struct lfsck_component *com,
1896 struct dt_object *parent,
1897 struct dt_object *child,
1898 const char *name, const char *name2,
1899 __u16 type, bool update, bool dec)
1901 struct lfsck_thread_info *info = lfsck_env_info(env);
1902 struct dt_insert_rec *rec = &info->lti_dt_rec;
1903 const struct lu_fid *cfid = lfsck_dto2fid(child);
1904 struct lu_fid *tfid = &info->lti_fid5;
1905 struct lfsck_instance *lfsck = com->lc_lfsck;
1906 struct dt_device *dev = lfsck->li_next;
1907 struct thandle *th = NULL;
1908 struct lustre_handle lh = { 0 };
1912 if (unlikely(!dt_try_as_dir(env, parent)))
1913 GOTO(log, rc = -ENOTDIR);
1915 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1916 MDS_INODELOCK_UPDATE, LCK_EX);
1920 th = dt_trans_create(env, dev);
1922 GOTO(unlock1, rc = PTR_ERR(th));
1924 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1929 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1930 rec->rec_fid = cfid;
1931 rc = dt_declare_insert(env, parent,
1932 (const struct dt_rec *)rec,
1933 (const struct dt_key *)name2, th);
1939 rc = dt_declare_ref_del(env, parent, th);
1944 rc = dt_trans_start(env, dev, th);
1948 dt_write_lock(env, parent, 0);
1949 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1950 (const struct dt_key *)name, BYPASS_CAPA);
1951 /* Someone has removed the bad name entry by race. */
1953 GOTO(unlock2, rc = 0);
1958 /* Someone has removed the bad name entry and reused it for other
1959 * object by race. */
1960 if (!lu_fid_eq(tfid, cfid))
1961 GOTO(unlock2, rc = 0);
1963 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1964 GOTO(unlock2, rc = 1);
1966 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
1972 rc = dt_insert(env, parent,
1973 (const struct dt_rec *)rec,
1974 (const struct dt_key *)name2, th,
1981 rc = dt_ref_del(env, parent, th);
1986 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1989 dt_write_unlock(env, parent);
1992 dt_trans_stop(env, dev, th);
1994 /* We are not sure whether the child will become orphan or not.
1995 * Record it in the LFSCK tracing file for further checking in
1996 * the second-stage scanning. */
1997 if (!update && !dec && rc == 0)
1998 lfsck_namespace_trace_update(env, com, cfid,
1999 LNTF_CHECK_LINKEA, true);
2002 lfsck_ibits_unlock(&lh, LCK_EX);
2005 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2006 "entry for: parent "DFID", child "DFID", name %s, type "
2007 "in name entry %o, type claimed by child %o. repair it "
2008 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2009 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2010 name, type, update ? lfsck_object_type(child) : 0,
2011 update ? "updating" : "removing", name2, rc);
2014 struct lfsck_namespace *ns = com->lc_file_ram;
2016 ns->ln_flags |= LF_INCONSISTENT;
2023 * Update the ".." name entry for the given object.
2025 * The object's ".." is corrupted, this function will update the ".." name
2026 * entry with the given pfid, and the linkEA with the given ldata.
2028 * The caller should take the ldlm lock before the calling.
2030 * \param[in] env pointer to the thread context
2031 * \param[in] com pointer to the lfsck component
2032 * \param[in] obj pointer to the dt_object to be handled
2033 * \param[in] pfid the new fid for the object's ".." name entry
2034 * \param[in] cname the name for the @obj in the parent directory
2036 * \retval positive number for repaired cases
2037 * \retval 0 if nothing to be repaired
2038 * \retval negative error number on failure
2040 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2041 struct lfsck_component *com,
2042 struct dt_object *obj,
2043 const struct lu_fid *pfid,
2044 struct lu_name *cname)
2046 struct lfsck_thread_info *info = lfsck_env_info(env);
2047 struct dt_insert_rec *rec = &info->lti_dt_rec;
2048 struct lfsck_instance *lfsck = com->lc_lfsck;
2049 struct dt_device *dev = lfsck->li_bottom;
2050 struct thandle *th = NULL;
2051 struct linkea_data ldata = { 0 };
2052 struct lu_buf linkea_buf;
2056 LASSERT(!dt_object_remote(obj));
2057 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2059 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2063 rc = linkea_add_buf(&ldata, cname, pfid);
2067 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2068 ldata.ld_leh->leh_len);
2070 th = dt_trans_create(env, dev);
2072 GOTO(log, rc = PTR_ERR(th));
2074 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2078 rec->rec_type = S_IFDIR;
2079 rec->rec_fid = pfid;
2080 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2081 (const struct dt_key *)dotdot, th);
2085 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2086 XATTR_NAME_LINK, 0, th);
2090 rc = dt_trans_start_local(env, dev, th);
2094 dt_write_lock(env, obj, 0);
2095 if (unlikely(lfsck_is_dead_obj(obj)))
2096 GOTO(unlock, rc = 0);
2098 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2099 GOTO(unlock, rc = 1);
2101 /* The old ".." name entry maybe not exist. */
2102 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2105 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2106 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2110 rc = dt_xattr_set(env, obj, &linkea_buf,
2111 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2113 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2116 dt_write_unlock(env, obj);
2119 dt_trans_stop(env, dev, th);
2122 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2123 "the object "DFID", new parent "DFID": rc = %d\n",
2124 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2128 struct lfsck_namespace *ns = com->lc_file_ram;
2130 ns->ln_flags |= LF_INCONSISTENT;
2137 * Handle orphan @obj during Double Scan Directory.
2139 * Remove the @obj's current (invalid) linkEA entries, and insert
2140 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2141 * ${FID}-${PFID}-D-${conflict_version}
2143 * The caller should take the ldlm lock before the calling.
2145 * \param[in] env pointer to the thread context
2146 * \param[in] com pointer to the lfsck component
2147 * \param[in] obj pointer to the orphan object to be handled
2148 * \param[in] pfid the new fid for the object's ".." name entry
2149 * \param[in,out] lh ldlm lock handler for the given @obj
2150 * \param[out] type to tell the caller what the inconsistency is
2152 * \retval positive number for repaired cases
2153 * \retval 0 if nothing to be repaired
2154 * \retval negative error number on failure
2157 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2158 struct lfsck_component *com,
2159 struct dt_object *obj,
2160 const struct lu_fid *pfid,
2161 struct lustre_handle *lh,
2162 enum lfsck_namespace_inconsistency_type *type)
2164 struct lfsck_thread_info *info = lfsck_env_info(env);
2165 struct lfsck_namespace *ns = com->lc_file_ram;
2169 /* Remove the unrecognized linkEA. */
2170 rc = lfsck_namespace_links_remove(env, com, obj);
2171 lfsck_ibits_unlock(lh, LCK_EX);
2172 if (rc < 0 && rc != -ENODATA)
2175 *type = LNIT_MUL_REF;
2177 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2178 * ever tried to verify some remote MDT-object that resides on this
2179 * MDT, but this MDT failed to respond such request. So means there
2180 * may be some remote name entry on other MDT that references this
2181 * object with another name, so we cannot know whether this linkEA
2182 * is valid or not. So keep it there and maybe resolved when next
2184 if (ns->ln_flags & LF_INCOMPLETE)
2187 /* The unique linkEA is invalid, even if the ".." name entry may be
2188 * valid, we still cannot know via which name entry this directory
2189 * will be referenced. Then handle it as pure orphan. */
2190 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2191 "-"DFID, PFID(pfid));
2192 rc = lfsck_namespace_insert_orphan(env, com, obj,
2193 info->lti_tmpbuf, "D", NULL);
2199 * Double Scan Directory object for single linkEA entry case.
2201 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2202 * then check whether the name is in the namespace or not, if not, add the
2203 * missing name entry back to namespace. If the linkEA entry is invalid,
2204 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2207 * \param[in] env pointer to the thread context
2208 * \param[in] com pointer to the lfsck component
2209 * \param[in] child pointer to the directory to be double scanned
2210 * \param[in] pfid the FID corresponding to the ".." entry
2211 * \param[in] ldata pointer to the linkEA data for the given @child
2212 * \param[in,out] lh ldlm lock handler for the given @child
2213 * \param[out] type to tell the caller what the inconsistency is
2214 * \param[in] retry if found inconsistency, but the caller does not hold
2215 * ldlm lock on the @child, then set @retry as true
2217 * \retval positive number for repaired cases
2218 * \retval 0 if nothing to be repaired
2219 * \retval negative error number on failure
2222 lfsck_namespace_dsd_single(const struct lu_env *env,
2223 struct lfsck_component *com,
2224 struct dt_object *child,
2225 const struct lu_fid *pfid,
2226 struct linkea_data *ldata,
2227 struct lustre_handle *lh,
2228 enum lfsck_namespace_inconsistency_type *type,
2231 struct lfsck_thread_info *info = lfsck_env_info(env);
2232 struct lu_name *cname = &info->lti_name;
2233 const struct lu_fid *cfid = lfsck_dto2fid(child);
2234 struct lu_fid *tfid = &info->lti_fid3;
2235 struct lfsck_namespace *ns = com->lc_file_ram;
2236 struct lfsck_instance *lfsck = com->lc_lfsck;
2237 struct dt_object *parent = NULL;
2241 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
2242 /* The unique linkEA entry with bad parent will be handled as orphan. */
2243 if (!fid_is_sane(tfid)) {
2244 if (!lustre_handle_is_used(lh) && retry != NULL)
2247 rc = lfsck_namespace_dsd_orphan(env, com, child,
2253 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2255 GOTO(out, rc = PTR_ERR(parent));
2257 /* We trust the unique linkEA entry in spite of whether it matches the
2258 * ".." name entry or not. Because even if the linkEA entry is wrong
2259 * and the ".." name entry is right, we still cannot know via which
2260 * name entry the child will be referenced, since all known entries
2261 * have been verified during the first-stage scanning. */
2262 if (!dt_object_exists(parent)) {
2263 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2264 * has ever tried to verify some remote MDT-object that resides
2265 * on this MDT, but this MDT failed to respond such request. So
2266 * means there may be some remote name entry on other MDT that
2267 * references this object with another name, so we cannot know
2268 * whether this linkEA is valid or not. So keep it there and
2269 * maybe resolved when next LFSCK run. */
2270 if (ns->ln_flags & LF_INCOMPLETE)
2273 if (!lustre_handle_is_used(lh) && retry != NULL) {
2279 lfsck_ibits_unlock(lh, LCK_EX);
2282 /* Create the lost parent as an orphan. */
2283 rc = lfsck_namespace_create_orphan(env, com, parent);
2285 /* Add the missing name entry to the parent. */
2286 rc = lfsck_namespace_insert_normal(env, com, parent,
2287 child, cname->ln_name);
2288 if (unlikely(rc == -EEXIST)) {
2289 /* Unfortunately, someone reused the name
2290 * under the parent by race. So we have
2291 * to remove the linkEA entry from
2292 * current child object. It means that the
2293 * LFSCK cannot recover the system
2294 * totally back to its original status,
2295 * but it is necessary to make the
2296 * current system to be consistent. */
2297 rc = lfsck_namespace_shrink_linkea(env,
2301 snprintf(info->lti_tmpbuf,
2302 sizeof(info->lti_tmpbuf),
2303 "-"DFID, PFID(pfid));
2304 rc = lfsck_namespace_insert_orphan(env,
2305 com, child, info->lti_tmpbuf,
2314 /* The unique linkEA entry with bad parent will be handled as orphan. */
2315 if (unlikely(!dt_try_as_dir(env, parent))) {
2316 if (!lustre_handle_is_used(lh) && retry != NULL)
2319 rc = lfsck_namespace_dsd_orphan(env, com, child,
2325 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2326 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2327 if (rc == -ENOENT) {
2328 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2329 * has ever tried to verify some remote MDT-object that resides
2330 * on this MDT, but this MDT failed to respond such request. So
2331 * means there may be some remote name entry on other MDT that
2332 * references this object with another name, so we cannot know
2333 * whether this linkEA is valid or not. So keep it there and
2334 * maybe resolved when next LFSCK run. */
2335 if (ns->ln_flags & LF_INCOMPLETE)
2338 if (!lustre_handle_is_used(lh) && retry != NULL) {
2344 lfsck_ibits_unlock(lh, LCK_EX);
2345 /* Add the missing name entry back to the namespace. */
2346 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2348 if (unlikely(rc == -ESTALE))
2349 /* It may happen when the remote object has been
2350 * removed, but the local MDT is not aware of that. */
2353 if (unlikely(rc == -EEXIST)) {
2354 /* Unfortunately, someone reused the name under the
2355 * parent by race. So we have to remove the linkEA
2356 * entry from current child object. It means that the
2357 * LFSCK cannot recover the system totally back to
2358 * its original status, but it is necessary to make
2359 * the current system to be consistent.
2361 * It also may be because of the LFSCK found some
2362 * internal status of create operation. Under such
2363 * case, nothing to be done. */
2364 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2365 parent, child, ldata, cname, tfid);
2367 snprintf(info->lti_tmpbuf,
2368 sizeof(info->lti_tmpbuf),
2369 "-"DFID, PFID(pfid));
2370 rc = lfsck_namespace_insert_orphan(env, com,
2371 child, info->lti_tmpbuf, "D", NULL);
2381 if (!lu_fid_eq(tfid, cfid)) {
2382 if (!lustre_handle_is_used(lh) && retry != NULL) {
2388 lfsck_ibits_unlock(lh, LCK_EX);
2389 /* The name entry references another MDT-object that
2390 * may be created by the LFSCK for repairing dangling
2391 * name entry. Try to replace it. */
2392 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2395 rc = lfsck_namespace_dsd_orphan(env, com, child,
2401 /* The ".." name entry is wrong, update it. */
2402 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2403 if (!lustre_handle_is_used(lh) && retry != NULL) {
2409 *type = LNIT_UNMATCHED_PAIRS;
2410 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2411 lfsck_dto2fid(parent), cname);
2417 if (parent != NULL && !IS_ERR(parent))
2418 lfsck_object_put(env, parent);
2424 * Double Scan Directory object for multiple linkEA entries case.
2426 * The given @child has multiple linkEA entries. There is at most one linkEA
2427 * entry will be valid, all the others will be removed. Firstly, the function
2428 * will try to find out the linkEA entry for which the name entry exists under
2429 * the given parent (@pfid). If there is no linkEA entry that matches the given
2430 * ".." name entry, then tries to find out the first linkEA entry that both the
2431 * parent and the name entry exist to rebuild a new ".." name entry.
2433 * \param[in] env pointer to the thread context
2434 * \param[in] com pointer to the lfsck component
2435 * \param[in] child pointer to the directory to be double scanned
2436 * \param[in] pfid the FID corresponding to the ".." entry
2437 * \param[in] ldata pointer to the linkEA data for the given @child
2438 * \param[in,out] lh ldlm lock handler for the given @child
2439 * \param[out] type to tell the caller what the inconsistency is
2440 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2442 * \retval positive number for repaired cases
2443 * \retval 0 if nothing to be repaired
2444 * \retval negative error number on failure
2447 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2448 struct lfsck_component *com,
2449 struct dt_object *child,
2450 const struct lu_fid *pfid,
2451 struct linkea_data *ldata,
2452 struct lustre_handle *lh,
2453 enum lfsck_namespace_inconsistency_type *type,
2456 struct lfsck_thread_info *info = lfsck_env_info(env);
2457 struct lu_name *cname = &info->lti_name;
2458 const struct lu_fid *cfid = lfsck_dto2fid(child);
2459 struct lu_fid *tfid = &info->lti_fid3;
2460 struct lu_fid *pfid2 = &info->lti_fid4;
2461 struct lfsck_namespace *ns = com->lc_file_ram;
2462 struct lfsck_instance *lfsck = com->lc_lfsck;
2463 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2464 struct dt_object *parent = NULL;
2465 struct linkea_data ldata_new = { 0 };
2472 while (ldata->ld_lee != NULL) {
2473 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
2475 /* Drop repeated linkEA entries. */
2476 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
2477 /* Drop invalid linkEA entry. */
2478 if (!fid_is_sane(tfid)) {
2479 linkea_del_buf(ldata, cname);
2483 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2484 * then it is possible that: the directry object has ever
2485 * been lost, but its name entry was there. In the former
2486 * LFSCK run, during the first-stage scanning, the LFSCK
2487 * found the dangling name entry, but it did not recreate
2488 * the lost object, and when moved to the second-stage
2489 * scanning, some children objects of the lost directory
2490 * object were found, then the LFSCK recreated such lost
2491 * directory object as an orphan.
2493 * When the LFSCK runs again, if the dangling name is still
2494 * there, the LFSCK should move the orphan directory object
2495 * back to the normal namespace. */
2496 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
2497 linkea_next_entry(ldata);
2501 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2503 RETURN(PTR_ERR(parent));
2505 if (!dt_object_exists(parent)) {
2506 lfsck_object_put(env, parent);
2507 if (ldata->ld_leh->leh_reccount > 1) {
2508 /* If it is NOT the last linkEA entry, then
2509 * there is still other chance to make the
2510 * child to be visible via other parent, then
2511 * remove this linkEA entry. */
2512 linkea_del_buf(ldata, cname);
2519 /* The linkEA entry with bad parent will be removed. */
2520 if (unlikely(!dt_try_as_dir(env, parent))) {
2521 lfsck_object_put(env, parent);
2522 linkea_del_buf(ldata, cname);
2526 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2527 (const struct dt_key *)cname->ln_name,
2529 *pfid2 = *lfsck_dto2fid(parent);
2530 if (rc == -ENOENT) {
2531 lfsck_object_put(env, parent);
2532 linkea_next_entry(ldata);
2537 lfsck_object_put(env, parent);
2542 if (lu_fid_eq(tfid, cfid)) {
2543 lfsck_object_put(env, parent);
2544 if (!lu_fid_eq(pfid, pfid2)) {
2545 *type = LNIT_UNMATCHED_PAIRS;
2546 rc = lfsck_namespace_repair_unmatched_pairs(env,
2547 com, child, pfid2, cname);
2553 /* It is the most common case that we find the
2554 * name entry corresponding to the linkEA entry
2555 * that matches the ".." name entry. */
2556 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2560 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2564 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2569 linkea_del_buf(ldata, cname);
2570 linkea_first_entry(ldata);
2571 /* There may be some invalid dangling name entries under
2572 * other parent directories, remove all of them. */
2573 while (ldata->ld_lee != NULL) {
2574 lfsck_namespace_unpack_linkea_entry(ldata,
2575 cname, tfid, info->lti_key);
2576 if (!fid_is_sane(tfid))
2579 parent = lfsck_object_find_bottom(env, lfsck,
2581 if (IS_ERR(parent)) {
2582 rc = PTR_ERR(parent);
2583 if (rc != -ENOENT &&
2584 bk->lb_param & LPF_FAILOUT)
2590 if (!dt_object_exists(parent)) {
2591 lfsck_object_put(env, parent);
2595 rc = lfsck_namespace_repair_dirent(env, com,
2596 parent, child, cname->ln_name,
2597 cname->ln_name, S_IFDIR, false, true);
2598 lfsck_object_put(env, parent);
2600 if (bk->lb_param & LPF_FAILOUT)
2609 linkea_del_buf(ldata, cname);
2612 ns->ln_dirent_repaired += count;
2617 lfsck_ibits_unlock(lh, LCK_EX);
2618 /* The name entry references another MDT-object that may be
2619 * created by the LFSCK for repairing dangling name entry.
2620 * Try to replace it. */
2621 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2623 lfsck_object_put(env, parent);
2630 linkea_del_buf(ldata, cname);
2633 if (ldata->ld_leh->leh_reccount == 1) {
2634 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2640 /* All linkEA entries are invalid and removed, then handle the @child
2642 if (ldata->ld_leh->leh_reccount == 0) {
2643 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2649 linkea_first_entry(ldata);
2650 /* If the dangling name entry for the orphan directory object has
2651 * been remvoed, then just check whether the directory object is
2652 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2658 /* There is no linkEA entry that matches the ".." name entry. Find
2659 * the first linkEA entry that both parent and name entry exist to
2660 * rebuild a new ".." name entry. */
2670 * Repair the object's nlink attribute.
2672 * If all the known name entries have been verified, then the object's hard
2673 * link attribute should match the object's linkEA entries count unless the
2674 * object's has too much hard link to be recorded in the linkEA. Such cases
2675 * should have been marked in the LFSCK tracing file. Otherwise, trust the
2676 * linkEA to update the object's nlink attribute.
2678 * \param[in] env pointer to the thread context
2679 * \param[in] com pointer to the lfsck component
2680 * \param[in] obj pointer to the dt_object to be handled
2681 * \param[in,out] nlink pointer to buffer to object's hard lock count before
2682 * and after the repairing
2684 * \retval positive number for repaired cases
2685 * \retval 0 if nothing to be repaired
2686 * \retval negative error number on failure
2688 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2689 struct lfsck_component *com,
2690 struct dt_object *obj, __u32 *nlink)
2692 struct lfsck_thread_info *info = lfsck_env_info(env);
2693 struct lu_attr *la = &info->lti_la3;
2694 struct lu_fid *tfid = &info->lti_fid3;
2695 struct lfsck_namespace *ns = com->lc_file_ram;
2696 struct lfsck_instance *lfsck = com->lc_lfsck;
2697 struct dt_device *dev = lfsck->li_bottom;
2698 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2699 struct dt_object *child = NULL;
2700 struct thandle *th = NULL;
2701 struct linkea_data ldata = { 0 };
2702 struct lustre_handle lh = { 0 };
2708 LASSERT(!dt_object_remote(obj));
2709 LASSERT(S_ISREG(lfsck_object_type(obj)));
2711 child = lfsck_object_find_by_dev(env, dev, cfid);
2713 GOTO(log, rc = PTR_ERR(child));
2715 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2716 MDS_INODELOCK_UPDATE |
2717 MDS_INODELOCK_XATTR, LCK_EX);
2721 th = dt_trans_create(env, dev);
2723 GOTO(log, rc = PTR_ERR(th));
2725 la->la_valid = LA_NLINK;
2726 rc = dt_declare_attr_set(env, child, la, th);
2730 rc = dt_trans_start_local(env, dev, th);
2734 dt_write_lock(env, child, 0);
2735 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2736 * ever tried to verify some remote MDT-object that resides on this
2737 * MDT, but this MDT failed to respond such request. So means there
2738 * may be some remote name entry on other MDT that references this
2739 * object with another name, so we cannot know whether this linkEA
2740 * is valid or not. So keep it there and maybe resolved when next
2742 if (ns->ln_flags & LF_INCOMPLETE)
2743 GOTO(unlock, rc = 0);
2745 fid_cpu_to_be(tfid, cfid);
2746 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)&flags,
2747 (const struct dt_key *)tfid, BYPASS_CAPA);
2751 if (flags & LNTF_SKIP_NLINK)
2752 GOTO(unlock, rc = 0);
2754 rc = lfsck_links_read2(env, child, &ldata);
2756 GOTO(unlock, rc = 0);
2761 if (*nlink == ldata.ld_leh->leh_reccount)
2762 GOTO(unlock, rc = 0);
2764 la->la_nlink = *nlink = ldata.ld_leh->leh_reccount;
2765 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2766 GOTO(unlock, rc = 1);
2768 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
2770 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2773 dt_write_unlock(env, child);
2776 dt_trans_stop(env, dev, th);
2779 lfsck_ibits_unlock(&lh, LCK_EX);
2780 if (child != NULL && !IS_ERR(child))
2781 lfsck_object_put(env, child);
2783 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2784 "nlink count from %u to %u: rc = %d\n",
2785 lfsck_lfsck2name(lfsck), PFID(cfid), old, *nlink, rc);
2788 ns->ln_flags |= LF_INCONSISTENT;
2794 * Double scan the directory object for namespace LFSCK.
2796 * This function will verify the <parent, child> pairs in the namespace tree:
2797 * the parent references the child via some name entry that should be in the
2798 * child's linkEA entry, the child should back references the parent via its
2801 * The LFSCK will scan every linkEA entry in turn until find out the first
2802 * matched pairs. If found, then all other linkEA entries will be dropped.
2803 * If all the linkEA entries cannot match the ".." name entry, then there
2804 * are serveral possible cases:
2806 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2807 * in the linkEA entry is valid.
2809 * 2) If there are multiple linkEA entries, then try to find the linkEA
2810 * that matches the ".." name entry. If found, then all other entries
2811 * are invalid; otherwise, it is quite possible that the ".." name entry
2812 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2813 * entry according to the first valid linkEA entry (both the parent and
2814 * the name entry should exist).
2816 * 3) If the directory object has no (valid) linkEA entry, then the
2817 * directory object will be handled as pure orphan and inserted
2818 * in the .lustre/lost+found/MDTxxxx/ with the name:
2819 * ${self_FID}-${PFID}-D-${conflict_version}
2821 * \param[in] env pointer to the thread context
2822 * \param[in] com pointer to the lfsck component
2823 * \param[in] child pointer to the directory object to be handled
2824 * \param[in] flags to indicate the specical checking on the @child
2826 * \retval positive number for repaired cases
2827 * \retval 0 if nothing to be repaired
2828 * \retval negative error number on failure
2830 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
2831 struct lfsck_component *com,
2832 struct dt_object *child, __u8 flags)
2834 struct lfsck_thread_info *info = lfsck_env_info(env);
2835 const struct lu_fid *cfid = lfsck_dto2fid(child);
2836 struct lu_fid *pfid = &info->lti_fid2;
2837 struct lfsck_namespace *ns = com->lc_file_ram;
2838 struct lfsck_instance *lfsck = com->lc_lfsck;
2839 struct lustre_handle lh = { 0 };
2840 struct linkea_data ldata = { 0 };
2841 bool unknown = false;
2844 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
2848 LASSERT(!dt_object_remote(child));
2850 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
2851 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
2852 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
2853 "the namespace LFSCK, then the LFSCK cannot guarantee"
2854 "all the name entries have been verified in first-stage"
2855 "scanning. So have to skip orphan related handling for"
2856 "the directory object "DFID" with remote name entry\n",
2857 lfsck_lfsck2name(lfsck), PFID(cfid));
2862 if (unlikely(!dt_try_as_dir(env, child)))
2863 GOTO(out, rc = -ENOTDIR);
2865 /* We only take ldlm lock on the @child when required. When the
2866 * logic comes here for the first time, it is always false. */
2870 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2871 MDS_INODELOCK_UPDATE |
2872 MDS_INODELOCK_XATTR, LCK_EX);
2877 dt_read_lock(env, child, 0);
2878 if (unlikely(lfsck_is_dead_obj(child))) {
2879 dt_read_unlock(env, child);
2884 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
2885 (const struct dt_key *)dotdot, BYPASS_CAPA);
2887 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
2888 dt_read_unlock(env, child);
2893 if (!lustre_handle_is_used(&lh)) {
2894 dt_read_unlock(env, child);
2899 } else if (lfsck->li_lpf_obj != NULL &&
2900 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
2904 rc = lfsck_links_read(env, child, &ldata);
2905 dt_read_unlock(env, child);
2907 if (rc != -ENODATA && rc != -EINVAL)
2910 if (!lustre_handle_is_used(&lh))
2913 if (rc == -EINVAL && !fid_is_zero(pfid)) {
2914 /* Remove the corrupted linkEA. */
2915 rc = lfsck_namespace_links_remove(env, com, child);
2917 /* Here, because of the crashed linkEA, we
2918 * cannot know whether there is some parent
2919 * that references the child directory via
2920 * some name entry or not. So keep it there,
2921 * when the LFSCK run next time, if there is
2922 * some parent that references this object,
2923 * then the LFSCK can rebuild the linkEA;
2924 * otherwise, this object will be handled
2925 * as orphan as above. */
2928 /* 1. If we have neither ".." nor linkEA,
2929 * then it is an orphan.
2931 * 2. If we only have the ".." name entry,
2932 * but no parent references this child
2933 * directory, then handle it as orphan. */
2934 lfsck_ibits_unlock(&lh, LCK_EX);
2935 type = LNIT_MUL_REF;
2937 /* If the LFSCK is marked as LF_INCOMPLETE,
2938 * then means some MDT has ever tried to
2939 * verify some remote MDT-object that resides
2940 * on this MDT, but this MDT failed to respond
2941 * such request. So means there may be some
2942 * remote name entry on other MDT that
2943 * references this object with another name,
2944 * so we cannot know whether this linkEA is
2945 * valid or not. So keep it there and maybe
2946 * resolved when next LFSCK run. */
2947 if (ns->ln_flags & LF_INCOMPLETE)
2950 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2951 "-"DFID, PFID(pfid));
2952 rc = lfsck_namespace_insert_orphan(env, com, child,
2953 info->lti_tmpbuf, "D", NULL);
2959 linkea_first_entry(&ldata);
2960 /* This is the most common case: the object has unique linkEA entry. */
2961 if (ldata.ld_leh->leh_reccount == 1) {
2962 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
2963 &lh, &type, &retry);
2965 LASSERT(!lustre_handle_is_used(&lh));
2974 if (!lustre_handle_is_used(&lh))
2977 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
2978 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
2984 /* When we come here, the cases usually like that:
2985 * 1) The directory object has a corrupted linkEA entry. During the
2986 * first-stage scanning, the LFSCK cannot know such corruption,
2987 * then it appends the right linkEA entry according to the found
2988 * name entry after the bad one.
2990 * 2) The directory object has a right linkEA entry. During the
2991 * first-stage scanning, the LFSCK finds some bad name entry,
2992 * but the LFSCK cannot aware that at that time, then it adds
2993 * the bad linkEA entry for further processing. */
2994 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3000 lfsck_ibits_unlock(&lh, LCK_EX);
3003 case LNIT_BAD_LINKEA:
3004 ns->ln_linkea_repaired++;
3006 case LNIT_UNMATCHED_PAIRS:
3007 ns->ln_unmatched_pairs_repaired++;
3010 ns->ln_mul_ref_repaired++;
3018 ns->ln_unknown_inconsistency++;
3024 * Double scan the MDT-object for namespace LFSCK.
3026 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3027 * those entries from the linkEA; if the linkEA becomes empty or the object
3028 * has no linkEA, then it is an orphan and will be added into the directory
3029 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3030 * the remote parent; if the name entry corresponding to some linkEA entry
3031 * is lost, then add the name entry back to the namespace.
3033 * \param[in] env pointer to the thread context
3034 * \param[in] com pointer to the lfsck component
3035 * \param[in] child pointer to the dt_object to be handled
3036 * \param[in] flags some hints to indicate how the @child should be handled
3038 * \retval positive number for repaired cases
3039 * \retval 0 if nothing to be repaired
3040 * \retval negative error number on failure
3042 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3043 struct lfsck_component *com,
3044 struct dt_object *child, __u8 flags)
3046 struct lfsck_thread_info *info = lfsck_env_info(env);
3047 struct lu_attr *la = &info->lti_la;
3048 struct lu_name *cname = &info->lti_name;
3049 struct lu_fid *pfid = &info->lti_fid;
3050 struct lu_fid *cfid = &info->lti_fid2;
3051 struct lfsck_instance *lfsck = com->lc_lfsck;
3052 struct lfsck_namespace *ns = com->lc_file_ram;
3053 struct dt_object *parent = NULL;
3054 struct linkea_data ldata = { 0 };
3055 bool repaired = false;
3060 dt_read_lock(env, child, 0);
3061 if (unlikely(lfsck_is_dead_obj(child))) {
3062 dt_read_unlock(env, child);
3067 if (S_ISDIR(lfsck_object_type(child))) {
3068 dt_read_unlock(env, child);
3069 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3074 rc = lfsck_links_read(env, child, &ldata);
3075 dt_read_unlock(env, child);
3079 linkea_first_entry(&ldata);
3080 while (ldata.ld_lee != NULL) {
3081 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3083 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3085 /* Found repeated linkEA entries */
3087 rc = lfsck_namespace_shrink_linkea(env, com, child,
3088 &ldata, cname, pfid, false);
3100 /* Invalid PFID in the linkEA entry. */
3101 if (!fid_is_sane(pfid)) {
3102 rc = lfsck_namespace_shrink_linkea(env, com, child,
3103 &ldata, cname, pfid, true);
3113 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3115 GOTO(out, rc = PTR_ERR(parent));
3117 if (!dt_object_exists(parent)) {
3120 if (ldata.ld_leh->leh_reccount > 1) {
3121 /* If it is NOT the last linkEA entry, then
3122 * there is still other chance to make the
3123 * child to be visible via other parent, then
3124 * remove this linkEA entry. */
3125 rc = lfsck_namespace_shrink_linkea(env, com,
3126 child, &ldata, cname, pfid, true);
3128 /* If the LFSCK is marked as LF_INCOMPLETE,
3129 * then means some MDT has ever tried to
3130 * verify some remote MDT-object that resides
3131 * on this MDT, but this MDT failed to respond
3132 * such request. So means there may be some
3133 * remote name entry on other MDT that
3134 * references this object with another name,
3135 * so we cannot know whether this linkEA is
3136 * valid or not. So keep it there and maybe
3137 * resolved when next LFSCK run. */
3138 if (ns->ln_flags & LF_INCOMPLETE) {
3139 lfsck_object_put(env, parent);
3144 /* Create the lost parent as an orphan. */
3145 rc = lfsck_namespace_create_orphan(env, com,
3148 lfsck_object_put(env, parent);
3156 /* Add the missing name entry to the parent. */
3157 rc = lfsck_namespace_insert_normal(env, com,
3158 parent, child, cname->ln_name);
3159 if (unlikely(rc == -EEXIST))
3160 /* Unfortunately, someone reused the
3161 * name under the parent by race. So we
3162 * have to remove the linkEA entry from
3163 * current child object. It means that
3164 * the LFSCK cannot recover the system
3165 * totally back to its original status,
3166 * but it is necessary to make the
3167 * current system to be consistent. */
3168 rc = lfsck_namespace_shrink_linkea(env,
3172 linkea_next_entry(&ldata);
3175 lfsck_object_put(env, parent);
3185 /* The linkEA entry with bad parent will be removed. */
3186 if (unlikely(!dt_try_as_dir(env, parent))) {
3187 lfsck_object_put(env, parent);
3188 rc = lfsck_namespace_shrink_linkea(env, com, child,
3189 &ldata, cname, pfid, true);
3199 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3200 (const struct dt_key *)cname->ln_name,
3202 if (rc != 0 && rc != -ENOENT) {
3203 lfsck_object_put(env, parent);
3209 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3210 /* It is the most common case that we
3211 * find the name entry corresponding
3212 * to the linkEA entry. */
3213 lfsck_object_put(env, parent);
3214 linkea_next_entry(&ldata);
3216 /* The name entry references another
3217 * MDT-object that may be created by
3218 * the LFSCK for repairing dangling
3219 * name entry. Try to replace it. */
3220 rc = lfsck_namespace_replace_cond(env, com,
3221 parent, child, cfid, cname);
3222 lfsck_object_put(env, parent);
3228 linkea_next_entry(&ldata);
3230 rc = lfsck_namespace_shrink_linkea(env,
3244 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3248 /* If there is no name entry in the parent dir and the object
3249 * link count is less than the linkea entries count, then the
3250 * linkea entry should be removed. */
3251 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3252 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3253 parent, child, &ldata, cname, pfid);
3254 lfsck_object_put(env, parent);
3264 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3265 * MDT has ever tried to verify some remote MDT-object that
3266 * resides on this MDT, but this MDT failed to respond such
3267 * request. So means there may be some remote name entry on
3268 * other MDT that references this object with another name,
3269 * so we cannot know whether this linkEA is valid or not.
3270 * So keep it there and maybe resolved when next LFSCK run. */
3271 if (ns->ln_flags & LF_INCOMPLETE) {
3272 lfsck_object_put(env, parent);
3277 /* Add the missing name entry back to the namespace. */
3278 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3280 if (unlikely(rc == -ESTALE))
3281 /* It may happen when the remote object has been
3282 * removed, but the local MDT is not aware of that. */
3285 if (unlikely(rc == -EEXIST))
3286 /* Unfortunately, someone reused the name under the
3287 * parent by race. So we have to remove the linkEA
3288 * entry from current child object. It means that the
3289 * LFSCK cannot recover the system totally back to
3290 * its original status, but it is necessary to make
3291 * the current system to be consistent.
3293 * It also may be because of the LFSCK found some
3294 * internal status of create operation. Under such
3295 * case, nothing to be done. */
3296 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3297 parent, child, &ldata, cname, pfid);
3299 linkea_next_entry(&ldata);
3301 lfsck_object_put(env, parent);
3312 if (rc < 0 && rc != -ENODATA)
3316 LASSERT(ldata.ld_leh != NULL);
3318 count = ldata.ld_leh->leh_reccount;
3321 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3322 * MDT has ever tried to verify some remote MDT-object that
3323 * resides on this MDT, but this MDT failed to respond such
3324 * request. So means there may be some remote name entry on
3325 * other MDT that references this object with another name,
3326 * so we cannot know whether this linkEA is valid or not.
3327 * So keep it there and maybe resolved when next LFSCK run. */
3328 if (count == 0 && !(ns->ln_flags & LF_INCOMPLETE)) {
3329 /* If the child becomes orphan, then insert it into
3330 * the global .lustre/lost+found/MDTxxxx directory. */
3331 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
3337 ns->ln_mul_ref_repaired++;
3342 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3346 if (la->la_nlink != count) {
3347 rc = lfsck_namespace_repair_nlink(env, com, child,
3350 ns->ln_objs_nlink_repaired++;
3356 if (la->la_nlink > 1)
3357 ns->ln_mul_linked_repaired++;
3366 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3367 struct lfsck_namespace *ns,
3368 __u64 checked_phase1,
3369 __u64 checked_phase2,
3373 seq_printf(m, "checked_phase1: "LPU64"\n"
3374 "checked_phase2: "LPU64"\n"
3375 "updated_phase1: "LPU64"\n"
3376 "updated_phase2: "LPU64"\n"
3377 "failed_phase1: "LPU64"\n"
3378 "failed_phase2: "LPU64"\n"
3379 "directories: "LPU64"\n"
3380 "dirent_repaired: "LPU64"\n"
3381 "linkea_repaired: "LPU64"\n"
3382 "nlinks_repaired: "LPU64"\n"
3383 "multiple_linked_checked: "LPU64"\n"
3384 "multiple_linked_repaired: "LPU64"\n"
3385 "unknown_inconsistency: "LPU64"\n"
3386 "unmatched_pairs_repaired: "LPU64"\n"
3387 "dangling_repaired: "LPU64"\n"
3388 "multiple_referenced_repaired: "LPU64"\n"
3389 "bad_file_type_repaired: "LPU64"\n"
3390 "lost_dirent_repaired: "LPU64"\n"
3391 "success_count: %u\n"
3392 "run_time_phase1: %u seconds\n"
3393 "run_time_phase2: %u seconds\n",
3396 ns->ln_items_repaired,
3397 ns->ln_objs_repaired_phase2,
3398 ns->ln_items_failed,
3399 ns->ln_objs_failed_phase2,
3400 ns->ln_dirs_checked,
3401 ns->ln_dirent_repaired,
3402 ns->ln_linkea_repaired,
3403 ns->ln_objs_nlink_repaired,
3404 ns->ln_mul_linked_checked,
3405 ns->ln_mul_linked_repaired,
3406 ns->ln_unknown_inconsistency,
3407 ns->ln_unmatched_pairs_repaired,
3408 ns->ln_dangling_repaired,
3409 ns->ln_mul_ref_repaired,
3410 ns->ln_bad_type_repaired,
3411 ns->ln_lost_dirent_repaired,
3412 ns->ln_success_count,
3417 /* namespace APIs */
3419 static int lfsck_namespace_reset(const struct lu_env *env,
3420 struct lfsck_component *com, bool init)
3422 struct lfsck_instance *lfsck = com->lc_lfsck;
3423 struct lfsck_namespace *ns = com->lc_file_ram;
3424 struct lfsck_assistant_data *lad = com->lc_data;
3425 struct dt_object *root;
3426 struct dt_object *dto;