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 positive number for data corruption
211 * \retval 0 for success
212 * \retval negative error number on failure
214 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
215 struct lfsck_component *com)
217 struct dt_object *obj = com->lc_obj;
218 struct lfsck_assistant_data *lad = com->lc_data;
219 struct lfsck_namespace *ns = com->lc_file_ram;
220 cfs_bitmap_t *bitmap = lad->lad_bitmap;
226 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
228 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
230 nbits = ns->ln_bitmap_size;
232 if (unlikely(nbits < BITS_PER_LONG))
233 nbits = BITS_PER_LONG;
235 if (nbits > bitmap->size) {
236 __u32 new_bits = bitmap->size;
237 cfs_bitmap_t *new_bitmap;
239 while (new_bits < nbits)
242 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
243 if (new_bitmap == NULL)
246 lad->lad_bitmap = new_bitmap;
247 CFS_FREE_BITMAP(bitmap);
251 if (ns->ln_bitmap_size == 0) {
252 lad->lad_incomplete = 0;
253 CFS_RESET_BITMAP(bitmap);
258 size = (ns->ln_bitmap_size + 7) >> 3;
259 rc = dt_xattr_get(env, obj,
260 lfsck_buf_get(env, bitmap->data, size),
261 XATTR_NAME_LFSCK_BITMAP, BYPASS_CAPA);
262 if (rc == -ERANGE || rc == -ENODATA || rc == 0)
271 if (cfs_bitmap_check_empty(bitmap))
272 lad->lad_incomplete = 0;
274 lad->lad_incomplete = 1;
280 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
281 * \retval 0: succeed.
282 * \retval -ve: failed cases.
284 static int lfsck_namespace_load(const struct lu_env *env,
285 struct lfsck_component *com)
287 int len = com->lc_file_size;
290 rc = dt_xattr_get(env, com->lc_obj,
291 lfsck_buf_get(env, com->lc_file_disk, len),
292 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
294 struct lfsck_namespace *ns = com->lc_file_ram;
296 lfsck_namespace_le_to_cpu(ns,
297 (struct lfsck_namespace *)com->lc_file_disk);
298 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
299 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
300 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
301 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
306 } else if (rc != -ENODATA) {
307 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
308 "expected = %d: rc = %d\n",
309 lfsck_lfsck2name(com->lc_lfsck), len, rc);
316 static int lfsck_namespace_store(const struct lu_env *env,
317 struct lfsck_component *com)
319 struct dt_object *obj = com->lc_obj;
320 struct lfsck_instance *lfsck = com->lc_lfsck;
321 struct lfsck_namespace *ns = com->lc_file_ram;
322 struct lfsck_assistant_data *lad = com->lc_data;
323 cfs_bitmap_t *bitmap = NULL;
324 struct thandle *handle;
326 int len = com->lc_file_size;
331 bitmap = lad->lad_bitmap;
332 nbits = bitmap->size;
335 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
338 ns->ln_bitmap_size = nbits;
339 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
341 handle = dt_trans_create(env, lfsck->li_bottom);
343 GOTO(log, rc = PTR_ERR(handle));
345 rc = dt_declare_xattr_set(env, obj,
346 lfsck_buf_get(env, com->lc_file_disk, len),
347 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
351 if (bitmap != NULL) {
352 rc = dt_declare_xattr_set(env, obj,
353 lfsck_buf_get(env, bitmap->data, nbits >> 3),
354 XATTR_NAME_LFSCK_BITMAP, 0, handle);
359 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
363 rc = dt_xattr_set(env, obj,
364 lfsck_buf_get(env, com->lc_file_disk, len),
365 XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
366 if (rc == 0 && bitmap != NULL)
367 rc = dt_xattr_set(env, obj,
368 lfsck_buf_get(env, bitmap->data, nbits >> 3),
369 XATTR_NAME_LFSCK_BITMAP, 0, handle,
375 dt_trans_stop(env, lfsck->li_bottom, handle);
379 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
380 lfsck_lfsck2name(lfsck), rc);
384 static int lfsck_namespace_init(const struct lu_env *env,
385 struct lfsck_component *com)
387 struct lfsck_namespace *ns = com->lc_file_ram;
390 memset(ns, 0, sizeof(*ns));
391 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
392 ns->ln_status = LS_INIT;
393 down_write(&com->lc_sem);
394 rc = lfsck_namespace_store(env, com);
395 up_write(&com->lc_sem);
400 * Update the namespace LFSCK tracing file for the given @fid
402 * \param[in] env pointer to the thread context
403 * \param[in] com pointer to the lfsck component
404 * \param[in] fid the fid which flags to be updated in the lfsck
406 * \param[in] add true if add new flags, otherwise remove flags
408 * \retval 0 for succeed or nothing to be done
409 * \retval negative error number on failure
411 int lfsck_namespace_trace_update(const struct lu_env *env,
412 struct lfsck_component *com,
413 const struct lu_fid *fid,
414 const __u8 flags, bool add)
416 struct lfsck_instance *lfsck = com->lc_lfsck;
417 struct dt_object *obj = com->lc_obj;
418 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
419 struct dt_device *dev = lfsck->li_bottom;
420 struct thandle *th = NULL;
428 down_write(&com->lc_sem);
429 fid_cpu_to_be(key, fid);
430 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
431 (const struct dt_key *)key, BYPASS_CAPA);
434 GOTO(unlock, rc = 0);
438 } else if (rc == 0) {
440 if ((old & flags) == flags)
441 GOTO(unlock, rc = 0);
445 if ((old & flags) == 0)
446 GOTO(unlock, rc = 0);
454 th = dt_trans_create(env, dev);
456 GOTO(log, rc = PTR_ERR(th));
459 rc = dt_declare_delete(env, obj,
460 (const struct dt_key *)key, th);
466 rc = dt_declare_insert(env, obj,
467 (const struct dt_rec *)&new,
468 (const struct dt_key *)key, th);
473 rc = dt_trans_start_local(env, dev, th);
478 rc = dt_delete(env, obj, (const struct dt_key *)key,
485 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
486 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
494 if (th != NULL && !IS_ERR(th))
495 dt_trans_stop(env, dev, th);
497 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
498 "tracing file, flags %x, old %x, new %x: rc = %d\n",
499 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
500 (__u32)flags, (__u32)old, (__u32)new, rc);
503 up_write(&com->lc_sem);
508 static int lfsck_namespace_check_exist(const struct lu_env *env,
509 struct dt_object *dir,
510 struct dt_object *obj, const char *name)
512 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
516 if (unlikely(lfsck_is_dead_obj(obj)))
517 RETURN(LFSCK_NAMEENTRY_DEAD);
519 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
520 (const struct dt_key *)name, BYPASS_CAPA);
522 RETURN(LFSCK_NAMEENTRY_REMOVED);
527 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
528 RETURN(LFSCK_NAMEENTRY_RECREATED);
533 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
534 struct dt_object *obj,
535 struct thandle *handle)
539 /* For destroying all invalid linkEA entries. */
540 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
544 /* For insert new linkEA entry. */
545 rc = dt_declare_xattr_set(env, obj,
546 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
547 XATTR_NAME_LINK, 0, handle);
551 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
552 struct linkea_data *ldata)
556 if (ldata->ld_buf->lb_buf == NULL)
559 if (!dt_object_exists(obj))
562 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
564 /* Buf was too small, figure out what we need. */
565 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
570 lu_buf_realloc(ldata->ld_buf, rc);
571 if (ldata->ld_buf->lb_buf == NULL)
574 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
579 rc = linkea_init(ldata);
585 * Remove linkEA for the given object.
587 * The caller should take the ldlm lock before the calling.
589 * \param[in] env pointer to the thread context
590 * \param[in] com pointer to the lfsck component
591 * \param[in] obj pointer to the dt_object to be handled
593 * \retval 0 for repaired cases
594 * \retval negative error number on failure
596 static int lfsck_namespace_links_remove(const struct lu_env *env,
597 struct lfsck_component *com,
598 struct dt_object *obj)
600 struct lfsck_instance *lfsck = com->lc_lfsck;
601 struct dt_device *dev = lfsck->li_bottom;
602 struct thandle *th = NULL;
606 LASSERT(dt_object_remote(obj) == 0);
608 th = dt_trans_create(env, dev);
610 GOTO(log, rc = PTR_ERR(th));
612 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
616 rc = dt_trans_start_local(env, dev, th);
620 dt_write_lock(env, obj, 0);
621 if (unlikely(lfsck_is_dead_obj(obj)))
622 GOTO(unlock, rc = -ENOENT);
624 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
625 GOTO(unlock, rc = 0);
627 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
632 dt_write_unlock(env, obj);
635 dt_trans_stop(env, dev, th);
638 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
639 "for the object "DFID": rc = %d\n",
640 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
643 struct lfsck_namespace *ns = com->lc_file_ram;
645 ns->ln_flags |= LF_INCONSISTENT;
651 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
652 struct linkea_data *ldata, struct thandle *handle)
654 const struct lu_buf *buf = lfsck_buf_get_const(env,
655 ldata->ld_buf->lb_buf,
656 ldata->ld_leh->leh_len);
658 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
662 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
663 struct lu_name *cname,
667 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
668 /* To guarantee the 'name' is terminated with '0'. */
669 memcpy(buf, cname->ln_name, cname->ln_namelen);
670 buf[cname->ln_namelen] = 0;
671 cname->ln_name = buf;
674 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
675 struct lu_name *cname,
679 struct link_ea_entry *oldlee;
683 oldlee = ldata->ld_lee;
684 oldlen = ldata->ld_reclen;
685 linkea_next_entry(ldata);
686 while (ldata->ld_lee != NULL) {
687 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
688 ldata->ld_lee->lee_reclen[1];
689 if (unlikely(ldata->ld_reclen == oldlen &&
690 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
695 linkea_del_buf(ldata, cname);
697 linkea_next_entry(ldata);
700 ldata->ld_lee = oldlee;
701 ldata->ld_reclen = oldlen;
707 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
709 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
710 * with the given type to generate the name, the detailed rules for name
711 * have been described as following.
713 * The function also generates the linkEA corresponding to the name entry
714 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
716 * \param[in] env pointer to the thread context
717 * \param[in] com pointer to the lfsck component
718 * \param[in] orphan pointer to the orphan MDT-object
719 * \param[in] infix additional information for the orphan name, such as
720 * the FID for original
721 * \param[in] type the type for describing why the orphan MDT-object is
722 * created. The rules are as following:
724 * type "D": The MDT-object is a directory, it may knows its parent
725 * but because there is no valid linkEA, the LFSCK cannot
726 * know where to put it back to the namespace.
727 * type "O": The MDT-object has no linkEA, and there is no name
728 * entry that references the MDT-object.
730 * \see lfsck_layout_recreate_parent() for more types.
732 * The orphan name will be like:
733 * ${FID}-${infix}-${type}-${conflict_version}
735 * \param[out] count if some others inserted some linkEA entries by race,
736 * then return the linkEA entries count.
738 * \retval positive number for repaired cases
739 * \retval 0 if needs to repair nothing
740 * \retval negative error number on failure
742 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
743 struct lfsck_component *com,
744 struct dt_object *orphan,
745 const char *infix, const char *type,
748 struct lfsck_thread_info *info = lfsck_env_info(env);
749 struct lu_name *cname = &info->lti_name;
750 struct dt_insert_rec *rec = &info->lti_dt_rec;
751 struct lu_fid *tfid = &info->lti_fid5;
752 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
753 const struct lu_fid *pfid;
754 struct lfsck_instance *lfsck = com->lc_lfsck;
755 struct dt_device *dev = lfsck->li_bottom;
756 struct dt_object *parent;
757 struct thandle *th = NULL;
758 struct lustre_handle plh = { 0 };
759 struct lustre_handle clh = { 0 };
760 struct linkea_data ldata = { 0 };
761 struct lu_buf linkea_buf;
768 cname->ln_name = NULL;
769 /* Create .lustre/lost+found/MDTxxxx when needed. */
770 if (unlikely(lfsck->li_lpf_obj == NULL)) {
771 rc = lfsck_create_lpf(env, lfsck);
776 parent = lfsck->li_lpf_obj;
777 pfid = lfsck_dto2fid(parent);
779 /* Hold update lock on the parent to prevent others to access. */
780 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
781 MDS_INODELOCK_UPDATE, LCK_EX);
786 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
787 PFID(cfid), infix, type, idx++);
788 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
789 (const struct dt_key *)info->lti_key,
791 if (rc != 0 && rc != -ENOENT)
794 if (unlikely(rc == 0 && lu_fid_eq(cfid, tfid)))
796 } while (rc == 0 && !exist);
798 cname->ln_name = info->lti_key;
799 cname->ln_namelen = namelen;
800 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
804 rc = linkea_add_buf(&ldata, cname, pfid);
808 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
809 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
814 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
815 ldata.ld_leh->leh_len);
816 th = dt_trans_create(env, dev);
818 GOTO(log, rc = PTR_ERR(th));
820 if (S_ISDIR(lfsck_object_type(orphan))) {
821 rc = dt_declare_delete(env, orphan,
822 (const struct dt_key *)dotdot, th);
826 rec->rec_type = S_IFDIR;
828 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
829 (const struct dt_key *)dotdot, th);
834 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
835 XATTR_NAME_LINK, 0, th);
840 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
842 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
843 (const struct dt_key *)cname->ln_name,
848 if (S_ISDIR(rec->rec_type)) {
849 rc = dt_declare_ref_add(env, parent, th);
855 rc = dt_trans_start_local(env, dev, th);
859 dt_write_lock(env, orphan, 0);
860 rc = lfsck_links_read(env, orphan, &ldata);
861 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
862 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
863 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
864 GOTO(unlock, rc = 1);
866 if (S_ISDIR(lfsck_object_type(orphan))) {
867 rc = dt_delete(env, orphan,
868 (const struct dt_key *)dotdot, th,
873 rec->rec_type = S_IFDIR;
875 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
876 (const struct dt_key *)dotdot, th,
882 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
885 if (rc == 0 && count != NULL)
886 *count = ldata.ld_leh->leh_reccount;
890 dt_write_unlock(env, orphan);
892 if (rc == 0 && !exist) {
893 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
895 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
896 (const struct dt_key *)cname->ln_name,
898 if (rc == 0 && S_ISDIR(rec->rec_type)) {
899 dt_write_lock(env, parent, 0);
900 rc = dt_ref_add(env, parent, th);
901 dt_write_unlock(env, parent);
905 GOTO(stop, rc = (rc == 0 ? 1 : rc));
908 dt_write_unlock(env, orphan);
911 dt_trans_stop(env, dev, th);
914 lfsck_ibits_unlock(&clh, LCK_EX);
915 lfsck_ibits_unlock(&plh, LCK_EX);
916 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
917 "object "DFID", name = %s: rc = %d\n",
918 lfsck_lfsck2name(lfsck), PFID(cfid),
919 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
922 struct lfsck_namespace *ns = com->lc_file_ram;
924 ns->ln_flags |= LF_INCONSISTENT;
931 * Add the specified name entry back to namespace.
933 * If there is a linkEA entry that back references a name entry under
934 * some parent directory, but such parent directory does not have the
935 * claimed name entry. On the other hand, the linkEA entries count is
936 * not larger than the MDT-object's hard link count. Under such case,
937 * it is quite possible that the name entry is lost. Then the LFSCK
938 * should add the name entry back to the namespace.
940 * \param[in] env pointer to the thread context
941 * \param[in] com pointer to the lfsck component
942 * \param[in] parent pointer to the directory under which the name entry
943 * will be inserted into
944 * \param[in] child pointer to the object referenced by the name entry
945 * that to be inserted into the parent
946 * \param[in] name the name for the child in the parent directory
948 * \retval positive number for repaired cases
949 * \retval 0 if nothing to be repaired
950 * \retval negative error number on failure
952 static int lfsck_namespace_insert_normal(const struct lu_env *env,
953 struct lfsck_component *com,
954 struct dt_object *parent,
955 struct dt_object *child,
958 struct lfsck_thread_info *info = lfsck_env_info(env);
959 struct lu_attr *la = &info->lti_la;
960 struct dt_insert_rec *rec = &info->lti_dt_rec;
961 struct lfsck_instance *lfsck = com->lc_lfsck;
962 struct dt_device *dev = lfsck->li_next;
963 struct thandle *th = NULL;
964 struct lustre_handle lh = { 0 };
968 if (unlikely(!dt_try_as_dir(env, parent)))
969 GOTO(log, rc = -ENOTDIR);
971 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
974 /* Hold update lock on the parent to prevent others to access. */
975 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
976 MDS_INODELOCK_UPDATE, LCK_EX);
980 th = dt_trans_create(env, dev);
982 GOTO(unlock, rc = PTR_ERR(th));
984 rec->rec_type = lfsck_object_type(child) & S_IFMT;
985 rec->rec_fid = lfsck_dto2fid(child);
986 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
987 (const struct dt_key *)name, th);
991 if (S_ISDIR(rec->rec_type)) {
992 rc = dt_declare_ref_add(env, parent, th);
997 memset(la, 0, sizeof(*la));
998 la->la_ctime = cfs_time_current_sec();
999 la->la_valid = LA_CTIME;
1000 rc = dt_declare_attr_set(env, parent, la, th);
1004 rc = dt_trans_start_local(env, dev, th);
1008 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1009 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1013 if (S_ISDIR(rec->rec_type)) {
1014 dt_write_lock(env, parent, 0);
1015 rc = dt_ref_add(env, parent, th);
1016 dt_write_unlock(env, parent);
1021 la->la_ctime = cfs_time_current_sec();
1022 rc = dt_attr_set(env, parent, la, th, BYPASS_CAPA);
1024 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1027 dt_trans_stop(env, dev, th);
1030 lfsck_ibits_unlock(&lh, LCK_EX);
1033 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1034 "the name %s and type %o to the parent "DFID": rc = %d\n",
1035 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1036 lfsck_object_type(child) & S_IFMT,
1037 PFID(lfsck_dto2fid(parent)), rc);
1040 struct lfsck_namespace *ns = com->lc_file_ram;
1042 ns->ln_flags |= LF_INCONSISTENT;
1044 ns->ln_lost_dirent_repaired++;
1051 * Create the specified orphan MDT-object on remote MDT.
1053 * The LFSCK instance on this MDT will send LFSCK RPC to remote MDT to
1054 * ask the remote LFSCK instance to create the specified orphan object
1055 * under .lustre/lost+found/MDTxxxx/ directory with the name:
1056 * ${FID}-P-${conflict_version}.
1058 * \param[in] env pointer to the thread context
1059 * \param[in] com pointer to the lfsck component
1060 * \param[in] orphan pointer to the orphan MDT-object
1061 * \param[in] type the orphan's type to be created
1063 * type "P": The orphan object to be created was a parent directory
1064 * of some DMT-object which linkEA shows that the @orphan
1065 * object is missing.
1067 * \see lfsck_layout_recreate_parent() for more types.
1069 * \retval positive number for repaired cases
1070 * \retval 0 if needs to repair nothing
1071 * \retval negative error number on failure
1073 static int lfsck_namespace_create_orphan_remote(const struct lu_env *env,
1074 struct lfsck_component *com,
1075 struct dt_object *orphan,
1078 struct lfsck_thread_info *info = lfsck_env_info(env);
1079 struct lfsck_request *lr = &info->lti_lr;
1080 struct lu_seq_range *range = &info->lti_range;
1081 const struct lu_fid *fid = lfsck_dto2fid(orphan);
1082 struct lfsck_namespace *ns = com->lc_file_ram;
1083 struct lfsck_instance *lfsck = com->lc_lfsck;
1084 struct seq_server_site *ss =
1085 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1086 struct lfsck_tgt_desc *ltd = NULL;
1087 struct ptlrpc_request *req = NULL;
1091 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1094 fld_range_set_mdt(range);
1095 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1099 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, range->lsr_index);
1101 ns->ln_flags |= LF_INCOMPLETE;
1103 GOTO(out, rc = -ENODEV);
1106 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1109 GOTO(out, rc = -ENOMEM);
1111 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1113 ptlrpc_request_free(req);
1118 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1119 memset(lr, 0, sizeof(*lr));
1120 lr->lr_event = LE_CREATE_ORPHAN;
1121 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1122 lr->lr_active = LFSCK_TYPE_NAMESPACE;
1126 ptlrpc_request_set_replen(req);
1127 rc = ptlrpc_queue_wait(req);
1128 ptlrpc_req_finished(req);
1132 else if (rc == -EEXIST)
1138 CDEBUG(D_LFSCK, "%s: namespace LFSCK create object "
1139 DFID" on the MDT %x remotely: rc = %d\n",
1140 lfsck_lfsck2name(lfsck), PFID(fid),
1141 ltd != NULL ? ltd->ltd_index : -1, rc);
1150 * Create the specified orphan MDT-object locally.
1152 * For the case that the parent MDT-object stored in some MDT-object's
1153 * linkEA entry is lost, the LFSCK will re-create the parent object as
1154 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1155 * with the name ${FID}-P-${conflict_version}.
1157 * \param[in] env pointer to the thread context
1158 * \param[in] com pointer to the lfsck component
1159 * \param[in] orphan pointer to the orphan MDT-object to be created
1160 * \param[in] type the orphan's type to be created
1162 * type "P": The orphan object to be created was a parent directory
1163 * of some DMT-object which linkEA shows that the @orphan
1164 * object is missing.
1166 * \see lfsck_layout_recreate_parent() for more types.
1168 * \retval positive number for repaired cases
1169 * \retval negative error number on failure
1171 static int lfsck_namespace_create_orphan_local(const struct lu_env *env,
1172 struct lfsck_component *com,
1173 struct dt_object *orphan,
1176 struct lfsck_thread_info *info = lfsck_env_info(env);
1177 struct lu_attr *la = &info->lti_la;
1178 struct dt_allocation_hint *hint = &info->lti_hint;
1179 struct dt_object_format *dof = &info->lti_dof;
1180 struct lu_name *cname = &info->lti_name2;
1181 struct dt_insert_rec *rec = &info->lti_dt_rec;
1182 struct lu_fid *tfid = &info->lti_fid;
1183 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1184 const struct lu_fid *pfid;
1185 struct lfsck_instance *lfsck = com->lc_lfsck;
1186 struct dt_device *dev = lfsck->li_bottom;
1187 struct dt_object *parent = NULL;
1188 struct dt_object *child = NULL;
1189 struct thandle *th = NULL;
1190 struct lustre_handle lh = { 0 };
1191 struct linkea_data ldata = { 0 };
1192 struct lu_buf linkea_buf;
1199 LASSERT(!dt_object_exists(orphan));
1200 LASSERT(!dt_object_remote(orphan));
1202 /* @orphan maybe not attached to lfsck->li_bottom */
1203 child = lfsck_object_find_by_dev(env, dev, cfid);
1205 GOTO(log, rc = PTR_ERR(child));
1207 cname->ln_name = NULL;
1208 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1211 /* Create .lustre/lost+found/MDTxxxx when needed. */
1212 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1213 rc = lfsck_create_lpf(env, lfsck);
1218 parent = lfsck->li_lpf_obj;
1219 pfid = lfsck_dto2fid(parent);
1221 /* Hold update lock on the parent to prevent others to access. */
1222 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1223 MDS_INODELOCK_UPDATE, LCK_EX);
1228 namelen = snprintf(name, 31, DFID"-P-%d",
1230 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1231 (const struct dt_key *)name, BYPASS_CAPA);
1232 if (rc != 0 && rc != -ENOENT)
1236 cname->ln_name = name;
1237 cname->ln_namelen = namelen;
1239 memset(la, 0, sizeof(*la));
1240 la->la_mode = type | (S_ISDIR(type) ? 0700 : 0600);
1241 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1242 LA_ATIME | LA_MTIME | LA_CTIME;
1244 child->do_ops->do_ah_init(env, hint, parent, child,
1245 la->la_mode & S_IFMT);
1247 memset(dof, 0, sizeof(*dof));
1248 dof->dof_type = dt_mode_to_dft(type);
1250 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1254 rc = linkea_add_buf(&ldata, cname, pfid);
1258 th = dt_trans_create(env, dev);
1260 GOTO(unlock1, rc = PTR_ERR(th));
1262 rc = dt_declare_create(env, child, la, hint, dof, th);
1263 if (rc == 0 && S_ISDIR(type))
1264 rc = dt_declare_ref_add(env, child, th);
1269 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1270 ldata.ld_leh->leh_len);
1271 rc = dt_declare_xattr_set(env, child, &linkea_buf,
1272 XATTR_NAME_LINK, 0, th);
1276 rec->rec_type = type;
1277 rec->rec_fid = cfid;
1278 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1279 (const struct dt_key *)name, th);
1280 if (rc == 0 && S_ISDIR(type))
1281 rc = dt_declare_ref_add(env, parent, th);
1286 rc = dt_trans_start_local(env, dev, th);
1290 dt_write_lock(env, child, 0);
1291 rc = dt_create(env, child, la, hint, dof, th);
1295 if (S_ISDIR(type)) {
1296 if (unlikely(!dt_try_as_dir(env, child)))
1297 GOTO(unlock2, rc = -ENOTDIR);
1299 rec->rec_type = S_IFDIR;
1300 rec->rec_fid = cfid;
1301 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1302 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1306 rec->rec_fid = pfid;
1307 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1308 (const struct dt_key *)dotdot, th,
1313 rc = dt_ref_add(env, child, th);
1318 rc = dt_xattr_set(env, child, &linkea_buf,
1319 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1320 dt_write_unlock(env, child);
1324 rec->rec_type = type;
1325 rec->rec_fid = cfid;
1326 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1327 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1328 if (rc == 0 && S_ISDIR(type)) {
1329 dt_write_lock(env, parent, 0);
1330 rc = dt_ref_add(env, parent, th);
1331 dt_write_unlock(env, parent);
1334 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1337 dt_write_unlock(env, child);
1340 dt_trans_stop(env, dev, th);
1343 lfsck_ibits_unlock(&lh, LCK_EX);
1346 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan locally for "
1347 "the object "DFID", name = %s, type %o: rc = %d\n",
1348 lfsck_lfsck2name(lfsck), PFID(cfid),
1349 cname->ln_name != NULL ? cname->ln_name : "<NULL>", type, rc);
1351 if (child != NULL && !IS_ERR(child))
1352 lfsck_object_put(env, child);
1358 * Create the specified orphan MDT-object.
1360 * For the case that the parent MDT-object stored in some MDT-object's
1361 * linkEA entry is lost, the LFSCK will re-create the parent object as
1362 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1363 * with the name: ${FID}-P-${conflict_version}.
1365 * \param[in] env pointer to the thread context
1366 * \param[in] com pointer to the lfsck component
1367 * \param[in] orphan pointer to the orphan MDT-object
1369 * type "P": The orphan object to be created was a parent directory
1370 * of some DMT-object which linkEA shows that the @orphan
1371 * object is missing.
1373 * \see lfsck_layout_recreate_parent() for more types.
1375 * \retval positive number for repaired cases
1376 * \retval 0 if needs to repair nothing
1377 * \retval negative error number on failure
1379 static int lfsck_namespace_create_orphan(const struct lu_env *env,
1380 struct lfsck_component *com,
1381 struct dt_object *orphan)
1383 struct lfsck_namespace *ns = com->lc_file_ram;
1386 if (dt_object_remote(orphan))
1387 rc = lfsck_namespace_create_orphan_remote(env, com, orphan,
1390 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
1394 ns->ln_flags |= LF_INCONSISTENT;
1400 * Remove the specified entry from the linkEA.
1402 * Locate the linkEA entry with the given @cname and @pfid, then
1403 * remove this entry or the other entries those are repeated with
1406 * \param[in] env pointer to the thread context
1407 * \param[in] com pointer to the lfsck component
1408 * \param[in] obj pointer to the dt_object to be handled
1409 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1410 * \param[in] cname the name for the child in the parent directory
1411 * \param[in] pfid the parent directory's FID for the linkEA
1412 * \param[in] next if true, then remove the first found linkEA
1413 * entry, and move the ldata->ld_lee to next entry
1415 * \retval positive number for repaired cases
1416 * \retval 0 if nothing to be repaired
1417 * \retval negative error number on failure
1419 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1420 struct lfsck_component *com,
1421 struct dt_object *obj,
1422 struct linkea_data *ldata,
1423 struct lu_name *cname,
1424 struct lu_fid *pfid,
1427 struct lfsck_instance *lfsck = com->lc_lfsck;
1428 struct dt_device *dev = lfsck->li_bottom;
1429 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1430 struct thandle *th = NULL;
1431 struct lustre_handle lh = { 0 };
1432 struct linkea_data ldata_new = { 0 };
1433 struct lu_buf linkea_buf;
1437 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1438 MDS_INODELOCK_UPDATE |
1439 MDS_INODELOCK_XATTR, LCK_EX);
1444 linkea_del_buf(ldata, cname);
1446 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1448 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1449 ldata->ld_leh->leh_len);
1452 th = dt_trans_create(env, dev);
1454 GOTO(unlock1, rc = PTR_ERR(th));
1456 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1457 XATTR_NAME_LINK, 0, th);
1461 rc = dt_trans_start_local(env, dev, th);
1465 dt_write_lock(env, obj, 0);
1466 if (unlikely(lfsck_is_dead_obj(obj)))
1467 GOTO(unlock2, rc = -ENOENT);
1469 rc = lfsck_links_read2(env, obj, &ldata_new);
1473 /* The specified linkEA entry has been removed by race. */
1474 rc = linkea_links_find(&ldata_new, cname, pfid);
1476 GOTO(unlock2, rc = 0);
1478 if (bk->lb_param & LPF_DRYRUN)
1479 GOTO(unlock2, rc = 1);
1482 linkea_del_buf(&ldata_new, cname);
1484 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1487 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1488 dt_write_unlock(env, obj);
1489 dt_trans_stop(env, dev, th);
1490 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1491 ldata_new.ld_leh->leh_len);
1495 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1496 ldata_new.ld_leh->leh_len);
1497 rc = dt_xattr_set(env, obj, &linkea_buf,
1498 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1500 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1503 dt_write_unlock(env, obj);
1506 dt_trans_stop(env, dev, th);
1509 lfsck_ibits_unlock(&lh, LCK_EX);
1512 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1513 "for the object: "DFID", parent "DFID", name %.*s\n",
1514 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1515 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1519 struct lfsck_namespace *ns = com->lc_file_ram;
1521 ns->ln_flags |= LF_INCONSISTENT;
1528 * Conditionally remove the specified entry from the linkEA.
1530 * Take the parent lock firstly, then check whether the specified
1531 * name entry exists or not: if yes, do nothing; otherwise, call
1532 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1534 * \param[in] env pointer to the thread context
1535 * \param[in] com pointer to the lfsck component
1536 * \param[in] parent pointer to the parent directory
1537 * \param[in] child pointer to the child object that holds the linkEA
1538 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1539 * \param[in] cname the name for the child in the parent directory
1540 * \param[in] pfid the parent directory's FID for the linkEA
1542 * \retval positive number for repaired cases
1543 * \retval 0 if nothing to be repaired
1544 * \retval negative error number on failure
1546 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1547 struct lfsck_component *com,
1548 struct dt_object *parent,
1549 struct dt_object *child,
1550 struct linkea_data *ldata,
1551 struct lu_name *cname,
1552 struct lu_fid *pfid)
1554 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1555 struct lustre_handle lh = { 0 };
1559 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1560 MDS_INODELOCK_UPDATE, LCK_EX);
1564 dt_read_lock(env, parent, 0);
1565 if (unlikely(lfsck_is_dead_obj(parent))) {
1566 dt_read_unlock(env, parent);
1567 lfsck_ibits_unlock(&lh, LCK_EX);
1568 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1574 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1575 (const struct dt_key *)cname->ln_name,
1577 dt_read_unlock(env, parent);
1579 /* It is safe to release the ldlm lock, because when the logic come
1580 * here, we have got all the needed information above whether the
1581 * linkEA entry is valid or not. It is not important that others
1582 * may add new linkEA entry after the ldlm lock released. If other
1583 * has removed the specified linkEA entry by race, then it is OK,
1584 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1586 lfsck_ibits_unlock(&lh, LCK_EX);
1587 if (rc == -ENOENT) {
1588 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1597 /* The LFSCK just found some internal status of cross-MDTs
1598 * create operation. That is normal. */
1599 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1600 linkea_next_entry(ldata);
1605 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1612 * Conditionally replace name entry in the parent.
1614 * As required, the LFSCK may re-create the lost MDT-object for dangling
1615 * name entry, but such repairing may be wrong because of bad FID in the
1616 * name entry. As the LFSCK processing, the real MDT-object may be found,
1617 * then the LFSCK should check whether the former re-created MDT-object
1618 * has been modified or not, if not, then destroy it and update the name
1619 * entry in the parent to reference the real MDT-object.
1621 * \param[in] env pointer to the thread context
1622 * \param[in] com pointer to the lfsck component
1623 * \param[in] parent pointer to the parent directory
1624 * \param[in] child pointer to the MDT-object that may be the real
1625 * MDT-object corresponding to the name entry in parent
1626 * \param[in] cfid the current FID in the name entry
1627 * \param[in] cname contains the name of the child in the parent directory
1629 * \retval positive number for repaired cases
1630 * \retval 0 if nothing to be repaired
1631 * \retval negative error number on failure
1633 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1634 struct lfsck_component *com,
1635 struct dt_object *parent,
1636 struct dt_object *child,
1637 const struct lu_fid *cfid,
1638 const struct lu_name *cname)
1640 struct lfsck_thread_info *info = lfsck_env_info(env);
1641 struct lu_fid *tfid = &info->lti_fid5;
1642 struct lu_attr *la = &info->lti_la;
1643 struct dt_insert_rec *rec = &info->lti_dt_rec;
1644 struct lfsck_instance *lfsck = com->lc_lfsck;
1645 struct dt_device *dev = lfsck->li_next;
1646 const char *name = cname->ln_name;
1647 struct dt_object *obj = NULL;
1648 struct lustre_handle plh = { 0 };
1649 struct lustre_handle clh = { 0 };
1650 struct linkea_data ldata = { 0 };
1651 struct thandle *th = NULL;
1656 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1657 MDS_INODELOCK_UPDATE, LCK_EX);
1661 if (!fid_is_sane(cfid)) {
1666 obj = lfsck_object_find(env, lfsck, cfid);
1669 if (rc == -ENOENT) {
1677 if (!dt_object_exists(obj)) {
1682 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1683 (const struct dt_key *)name, BYPASS_CAPA);
1684 if (rc == -ENOENT) {
1692 /* Someone changed the name entry, cannot replace it. */
1693 if (!lu_fid_eq(cfid, tfid))
1696 /* lock the object to be destroyed. */
1697 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1698 MDS_INODELOCK_UPDATE |
1699 MDS_INODELOCK_XATTR, LCK_EX);
1703 if (unlikely(lfsck_is_dead_obj(obj))) {
1708 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1712 /* The object has been modified by other(s), or it is not created by
1713 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1714 if (la->la_ctime != 0)
1717 if (S_ISREG(la->la_mode)) {
1718 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1720 /* If someone has created related OST-object(s),
1722 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1723 GOTO(log, rc = (rc > 0 ? 0 : rc));
1727 dt_read_lock(env, child, 0);
1728 rc = lfsck_links_read2(env, child, &ldata);
1729 dt_read_unlock(env, child);
1731 /* Someone changed the child, no need to replace. */
1738 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1739 /* Someone moved the child, no need to replace. */
1743 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1746 th = dt_trans_create(env, dev);
1748 GOTO(log, rc = PTR_ERR(th));
1751 rc = dt_declare_destroy(env, obj, th);
1756 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1760 rec->rec_type = S_IFDIR;
1761 rec->rec_fid = lfsck_dto2fid(child);
1762 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1763 (const struct dt_key *)name, th);
1767 rc = dt_trans_start(env, dev, th);
1772 rc = dt_destroy(env, obj, th);
1777 /* The old name entry maybe not exist. */
1778 dt_delete(env, parent, (const struct dt_key *)name, th,
1781 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1782 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1784 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1787 dt_trans_stop(env, dev, th);
1790 lfsck_ibits_unlock(&clh, LCK_EX);
1791 lfsck_ibits_unlock(&plh, LCK_EX);
1792 if (obj != NULL && !IS_ERR(obj))
1793 lfsck_object_put(env, obj);
1795 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1796 "object "DFID" because of conflict with the object "DFID
1797 " under the parent "DFID" with name %s: rc = %d\n",
1798 lfsck_lfsck2name(lfsck), PFID(cfid),
1799 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1806 * Overwrite the linkEA for the object with the given ldata.
1808 * The caller should take the ldlm lock before the calling.
1810 * \param[in] env pointer to the thread context
1811 * \param[in] com pointer to the lfsck component
1812 * \param[in] obj pointer to the dt_object to be handled
1813 * \param[in] ldata pointer to the new linkEA data
1815 * \retval positive number for repaired cases
1816 * \retval 0 if nothing to be repaired
1817 * \retval negative error number on failure
1819 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1820 struct lfsck_component *com,
1821 struct dt_object *obj,
1822 struct linkea_data *ldata)
1824 struct lfsck_instance *lfsck = com->lc_lfsck;
1825 struct dt_device *dev = lfsck->li_bottom;
1826 struct thandle *th = NULL;
1827 struct lu_buf linkea_buf;
1831 LASSERT(!dt_object_remote(obj));
1833 th = dt_trans_create(env, dev);
1835 GOTO(log, rc = PTR_ERR(th));
1837 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1838 ldata->ld_leh->leh_len);
1839 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1840 XATTR_NAME_LINK, 0, th);
1844 rc = dt_trans_start_local(env, dev, th);
1848 dt_write_lock(env, obj, 0);
1849 if (unlikely(lfsck_is_dead_obj(obj)))
1850 GOTO(unlock, rc = 0);
1852 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1853 GOTO(unlock, rc = 1);
1855 rc = dt_xattr_set(env, obj, &linkea_buf,
1856 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1858 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1861 dt_write_unlock(env, obj);
1864 dt_trans_stop(env, dev, th);
1867 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1868 "object "DFID": rc = %d\n",
1869 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1872 struct lfsck_namespace *ns = com->lc_file_ram;
1874 ns->ln_flags |= LF_INCONSISTENT;
1881 * Repair invalid name entry.
1883 * If the name entry contains invalid information, such as bad file type
1884 * or (and) corrupted object FID, then either remove the name entry or
1885 * udpate the name entry with the given (right) information.
1887 * \param[in] env pointer to the thread context
1888 * \param[in] com pointer to the lfsck component
1889 * \param[in] parent pointer to the parent directory
1890 * \param[in] child pointer to the object referenced by the name entry
1891 * \param[in] name the old name of the child under the parent directory
1892 * \param[in] name2 the new name of the child under the parent directory
1893 * \param[in] type the type claimed by the name entry
1894 * \param[in] update update the name entry if true; otherwise, remove it
1895 * \param[in] dec decrease the parent nlink count if true
1897 * \retval positive number for repaired successfully
1898 * \retval 0 if nothing to be repaired
1899 * \retval negative error number on failure
1901 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1902 struct lfsck_component *com,
1903 struct dt_object *parent,
1904 struct dt_object *child,
1905 const char *name, const char *name2,
1906 __u16 type, bool update, bool dec)
1908 struct lfsck_thread_info *info = lfsck_env_info(env);
1909 struct dt_insert_rec *rec = &info->lti_dt_rec;
1910 const struct lu_fid *cfid = lfsck_dto2fid(child);
1911 struct lu_fid *tfid = &info->lti_fid5;
1912 struct lfsck_instance *lfsck = com->lc_lfsck;
1913 struct dt_device *dev = lfsck->li_next;
1914 struct thandle *th = NULL;
1915 struct lustre_handle lh = { 0 };
1919 if (unlikely(!dt_try_as_dir(env, parent)))
1920 GOTO(log, rc = -ENOTDIR);
1922 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1923 MDS_INODELOCK_UPDATE, LCK_EX);
1927 th = dt_trans_create(env, dev);
1929 GOTO(unlock1, rc = PTR_ERR(th));
1931 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1936 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1937 rec->rec_fid = cfid;
1938 rc = dt_declare_insert(env, parent,
1939 (const struct dt_rec *)rec,
1940 (const struct dt_key *)name2, th);
1946 rc = dt_declare_ref_del(env, parent, th);
1951 rc = dt_trans_start(env, dev, th);
1955 dt_write_lock(env, parent, 0);
1956 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1957 (const struct dt_key *)name, BYPASS_CAPA);
1958 /* Someone has removed the bad name entry by race. */
1960 GOTO(unlock2, rc = 0);
1965 /* Someone has removed the bad name entry and reused it for other
1966 * object by race. */
1967 if (!lu_fid_eq(tfid, cfid))
1968 GOTO(unlock2, rc = 0);
1970 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1971 GOTO(unlock2, rc = 1);
1973 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
1979 rc = dt_insert(env, parent,
1980 (const struct dt_rec *)rec,
1981 (const struct dt_key *)name2, th,
1988 rc = dt_ref_del(env, parent, th);
1993 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1996 dt_write_unlock(env, parent);
1999 dt_trans_stop(env, dev, th);
2001 /* We are not sure whether the child will become orphan or not.
2002 * Record it in the LFSCK tracing file for further checking in
2003 * the second-stage scanning. */
2004 if (!update && !dec && rc == 0)
2005 lfsck_namespace_trace_update(env, com, cfid,
2006 LNTF_CHECK_LINKEA, true);
2009 lfsck_ibits_unlock(&lh, LCK_EX);
2012 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2013 "entry for: parent "DFID", child "DFID", name %s, type "
2014 "in name entry %o, type claimed by child %o. repair it "
2015 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2016 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2017 name, type, update ? lfsck_object_type(child) : 0,
2018 update ? "updating" : "removing", name2, rc);
2021 struct lfsck_namespace *ns = com->lc_file_ram;
2023 ns->ln_flags |= LF_INCONSISTENT;
2030 * Update the ".." name entry for the given object.
2032 * The object's ".." is corrupted, this function will update the ".." name
2033 * entry with the given pfid, and the linkEA with the given ldata.
2035 * The caller should take the ldlm lock before the calling.
2037 * \param[in] env pointer to the thread context
2038 * \param[in] com pointer to the lfsck component
2039 * \param[in] obj pointer to the dt_object to be handled
2040 * \param[in] pfid the new fid for the object's ".." name entry
2041 * \param[in] cname the name for the @obj in the parent directory
2043 * \retval positive number for repaired cases
2044 * \retval 0 if nothing to be repaired
2045 * \retval negative error number on failure
2047 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2048 struct lfsck_component *com,
2049 struct dt_object *obj,
2050 const struct lu_fid *pfid,
2051 struct lu_name *cname)
2053 struct lfsck_thread_info *info = lfsck_env_info(env);
2054 struct dt_insert_rec *rec = &info->lti_dt_rec;
2055 struct lfsck_instance *lfsck = com->lc_lfsck;
2056 struct dt_device *dev = lfsck->li_bottom;
2057 struct thandle *th = NULL;
2058 struct linkea_data ldata = { 0 };
2059 struct lu_buf linkea_buf;
2063 LASSERT(!dt_object_remote(obj));
2064 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2066 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2070 rc = linkea_add_buf(&ldata, cname, pfid);
2074 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2075 ldata.ld_leh->leh_len);
2077 th = dt_trans_create(env, dev);
2079 GOTO(log, rc = PTR_ERR(th));
2081 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2085 rec->rec_type = S_IFDIR;
2086 rec->rec_fid = pfid;
2087 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2088 (const struct dt_key *)dotdot, th);
2092 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2093 XATTR_NAME_LINK, 0, th);
2097 rc = dt_trans_start_local(env, dev, th);
2101 dt_write_lock(env, obj, 0);
2102 if (unlikely(lfsck_is_dead_obj(obj)))
2103 GOTO(unlock, rc = 0);
2105 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2106 GOTO(unlock, rc = 1);
2108 /* The old ".." name entry maybe not exist. */
2109 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2112 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2113 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2117 rc = dt_xattr_set(env, obj, &linkea_buf,
2118 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2120 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2123 dt_write_unlock(env, obj);
2126 dt_trans_stop(env, dev, th);
2129 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2130 "the object "DFID", new parent "DFID": rc = %d\n",
2131 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2135 struct lfsck_namespace *ns = com->lc_file_ram;
2137 ns->ln_flags |= LF_INCONSISTENT;
2144 * Handle orphan @obj during Double Scan Directory.
2146 * Remove the @obj's current (invalid) linkEA entries, and insert
2147 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2148 * ${FID}-${PFID}-D-${conflict_version}
2150 * The caller should take the ldlm lock before the calling.
2152 * \param[in] env pointer to the thread context
2153 * \param[in] com pointer to the lfsck component
2154 * \param[in] obj pointer to the orphan object to be handled
2155 * \param[in] pfid the new fid for the object's ".." name entry
2156 * \param[in,out] lh ldlm lock handler for the given @obj
2157 * \param[out] type to tell the caller what the inconsistency is
2159 * \retval positive number for repaired cases
2160 * \retval 0 if nothing to be repaired
2161 * \retval negative error number on failure
2164 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2165 struct lfsck_component *com,
2166 struct dt_object *obj,
2167 const struct lu_fid *pfid,
2168 struct lustre_handle *lh,
2169 enum lfsck_namespace_inconsistency_type *type)
2171 struct lfsck_thread_info *info = lfsck_env_info(env);
2172 struct lfsck_namespace *ns = com->lc_file_ram;
2176 /* Remove the unrecognized linkEA. */
2177 rc = lfsck_namespace_links_remove(env, com, obj);
2178 lfsck_ibits_unlock(lh, LCK_EX);
2179 if (rc < 0 && rc != -ENODATA)
2182 *type = LNIT_MUL_REF;
2184 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2185 * ever tried to verify some remote MDT-object that resides on this
2186 * MDT, but this MDT failed to respond such request. So means there
2187 * may be some remote name entry on other MDT that references this
2188 * object with another name, so we cannot know whether this linkEA
2189 * is valid or not. So keep it there and maybe resolved when next
2191 if (ns->ln_flags & LF_INCOMPLETE)
2194 /* The unique linkEA is invalid, even if the ".." name entry may be
2195 * valid, we still cannot know via which name entry this directory
2196 * will be referenced. Then handle it as pure orphan. */
2197 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2198 "-"DFID, PFID(pfid));
2199 rc = lfsck_namespace_insert_orphan(env, com, obj,
2200 info->lti_tmpbuf, "D", NULL);
2206 * Double Scan Directory object for single linkEA entry case.
2208 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2209 * then check whether the name is in the namespace or not, if not, add the
2210 * missing name entry back to namespace. If the linkEA entry is invalid,
2211 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2214 * \param[in] env pointer to the thread context
2215 * \param[in] com pointer to the lfsck component
2216 * \param[in] child pointer to the directory to be double scanned
2217 * \param[in] pfid the FID corresponding to the ".." entry
2218 * \param[in] ldata pointer to the linkEA data for the given @child
2219 * \param[in,out] lh ldlm lock handler for the given @child
2220 * \param[out] type to tell the caller what the inconsistency is
2221 * \param[in] retry if found inconsistency, but the caller does not hold
2222 * ldlm lock on the @child, then set @retry as true
2224 * \retval positive number for repaired cases
2225 * \retval 0 if nothing to be repaired
2226 * \retval negative error number on failure
2229 lfsck_namespace_dsd_single(const struct lu_env *env,
2230 struct lfsck_component *com,
2231 struct dt_object *child,
2232 const struct lu_fid *pfid,
2233 struct linkea_data *ldata,
2234 struct lustre_handle *lh,
2235 enum lfsck_namespace_inconsistency_type *type,
2238 struct lfsck_thread_info *info = lfsck_env_info(env);
2239 struct lu_name *cname = &info->lti_name;
2240 const struct lu_fid *cfid = lfsck_dto2fid(child);
2241 struct lu_fid *tfid = &info->lti_fid3;
2242 struct lfsck_namespace *ns = com->lc_file_ram;
2243 struct lfsck_instance *lfsck = com->lc_lfsck;
2244 struct dt_object *parent = NULL;
2248 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
2249 /* The unique linkEA entry with bad parent will be handled as orphan. */
2250 if (!fid_is_sane(tfid)) {
2251 if (!lustre_handle_is_used(lh) && retry != NULL)
2254 rc = lfsck_namespace_dsd_orphan(env, com, child,
2260 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2262 GOTO(out, rc = PTR_ERR(parent));
2264 /* We trust the unique linkEA entry in spite of whether it matches the
2265 * ".." name entry or not. Because even if the linkEA entry is wrong
2266 * and the ".." name entry is right, we still cannot know via which
2267 * name entry the child will be referenced, since all known entries
2268 * have been verified during the first-stage scanning. */
2269 if (!dt_object_exists(parent)) {
2270 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2271 * has ever tried to verify some remote MDT-object that resides
2272 * on this MDT, but this MDT failed to respond such request. So
2273 * means there may be some remote name entry on other MDT that
2274 * references this object with another name, so we cannot know
2275 * whether this linkEA is valid or not. So keep it there and
2276 * maybe resolved when next LFSCK run. */
2277 if (ns->ln_flags & LF_INCOMPLETE)
2280 if (!lustre_handle_is_used(lh) && retry != NULL) {
2286 lfsck_ibits_unlock(lh, LCK_EX);
2289 /* Create the lost parent as an orphan. */
2290 rc = lfsck_namespace_create_orphan(env, com, parent);
2292 /* Add the missing name entry to the parent. */
2293 rc = lfsck_namespace_insert_normal(env, com, parent,
2294 child, cname->ln_name);
2295 if (unlikely(rc == -EEXIST)) {
2296 /* Unfortunately, someone reused the name
2297 * under the parent by race. So we have
2298 * to remove the linkEA entry from
2299 * current child object. It means that the
2300 * LFSCK cannot recover the system
2301 * totally back to its original status,
2302 * but it is necessary to make the
2303 * current system to be consistent. */
2304 rc = lfsck_namespace_shrink_linkea(env,
2308 snprintf(info->lti_tmpbuf,
2309 sizeof(info->lti_tmpbuf),
2310 "-"DFID, PFID(pfid));
2311 rc = lfsck_namespace_insert_orphan(env,
2312 com, child, info->lti_tmpbuf,
2321 /* The unique linkEA entry with bad parent will be handled as orphan. */
2322 if (unlikely(!dt_try_as_dir(env, parent))) {
2323 if (!lustre_handle_is_used(lh) && retry != NULL)
2326 rc = lfsck_namespace_dsd_orphan(env, com, child,
2332 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2333 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2334 if (rc == -ENOENT) {
2335 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2336 * has ever tried to verify some remote MDT-object that resides
2337 * on this MDT, but this MDT failed to respond such request. So
2338 * means there may be some remote name entry on other MDT that
2339 * references this object with another name, so we cannot know
2340 * whether this linkEA is valid or not. So keep it there and
2341 * maybe resolved when next LFSCK run. */
2342 if (ns->ln_flags & LF_INCOMPLETE)
2345 if (!lustre_handle_is_used(lh) && retry != NULL) {
2351 lfsck_ibits_unlock(lh, LCK_EX);
2352 /* Add the missing name entry back to the namespace. */
2353 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2355 if (unlikely(rc == -ESTALE))
2356 /* It may happen when the remote object has been
2357 * removed, but the local MDT is not aware of that. */
2360 if (unlikely(rc == -EEXIST)) {
2361 /* Unfortunately, someone reused the name under the
2362 * parent by race. So we have to remove the linkEA
2363 * entry from current child object. It means that the
2364 * LFSCK cannot recover the system totally back to
2365 * its original status, but it is necessary to make
2366 * the current system to be consistent.
2368 * It also may be because of the LFSCK found some
2369 * internal status of create operation. Under such
2370 * case, nothing to be done. */
2371 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2372 parent, child, ldata, cname, tfid);
2374 snprintf(info->lti_tmpbuf,
2375 sizeof(info->lti_tmpbuf),
2376 "-"DFID, PFID(pfid));
2377 rc = lfsck_namespace_insert_orphan(env, com,
2378 child, info->lti_tmpbuf, "D", NULL);
2388 if (!lu_fid_eq(tfid, cfid)) {
2389 if (!lustre_handle_is_used(lh) && retry != NULL) {
2395 lfsck_ibits_unlock(lh, LCK_EX);
2396 /* The name entry references another MDT-object that
2397 * may be created by the LFSCK for repairing dangling
2398 * name entry. Try to replace it. */
2399 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2402 rc = lfsck_namespace_dsd_orphan(env, com, child,
2408 /* The ".." name entry is wrong, update it. */
2409 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2410 if (!lustre_handle_is_used(lh) && retry != NULL) {
2416 *type = LNIT_UNMATCHED_PAIRS;
2417 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2418 lfsck_dto2fid(parent), cname);
2424 if (parent != NULL && !IS_ERR(parent))
2425 lfsck_object_put(env, parent);
2431 * Double Scan Directory object for multiple linkEA entries case.
2433 * The given @child has multiple linkEA entries. There is at most one linkEA
2434 * entry will be valid, all the others will be removed. Firstly, the function
2435 * will try to find out the linkEA entry for which the name entry exists under
2436 * the given parent (@pfid). If there is no linkEA entry that matches the given
2437 * ".." name entry, then tries to find out the first linkEA entry that both the
2438 * parent and the name entry exist to rebuild a new ".." name entry.
2440 * \param[in] env pointer to the thread context
2441 * \param[in] com pointer to the lfsck component
2442 * \param[in] child pointer to the directory to be double scanned
2443 * \param[in] pfid the FID corresponding to the ".." entry
2444 * \param[in] ldata pointer to the linkEA data for the given @child
2445 * \param[in,out] lh ldlm lock handler for the given @child
2446 * \param[out] type to tell the caller what the inconsistency is
2447 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2449 * \retval positive number for repaired cases
2450 * \retval 0 if nothing to be repaired
2451 * \retval negative error number on failure
2454 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2455 struct lfsck_component *com,
2456 struct dt_object *child,
2457 const struct lu_fid *pfid,
2458 struct linkea_data *ldata,
2459 struct lustre_handle *lh,
2460 enum lfsck_namespace_inconsistency_type *type,
2463 struct lfsck_thread_info *info = lfsck_env_info(env);
2464 struct lu_name *cname = &info->lti_name;
2465 const struct lu_fid *cfid = lfsck_dto2fid(child);
2466 struct lu_fid *tfid = &info->lti_fid3;
2467 struct lu_fid *pfid2 = &info->lti_fid4;
2468 struct lfsck_namespace *ns = com->lc_file_ram;
2469 struct lfsck_instance *lfsck = com->lc_lfsck;
2470 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2471 struct dt_object *parent = NULL;
2472 struct linkea_data ldata_new = { 0 };
2479 while (ldata->ld_lee != NULL) {
2480 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
2482 /* Drop repeated linkEA entries. */
2483 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
2484 /* Drop invalid linkEA entry. */
2485 if (!fid_is_sane(tfid)) {
2486 linkea_del_buf(ldata, cname);
2490 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2491 * then it is possible that: the directry object has ever
2492 * been lost, but its name entry was there. In the former
2493 * LFSCK run, during the first-stage scanning, the LFSCK
2494 * found the dangling name entry, but it did not recreate
2495 * the lost object, and when moved to the second-stage
2496 * scanning, some children objects of the lost directory
2497 * object were found, then the LFSCK recreated such lost
2498 * directory object as an orphan.
2500 * When the LFSCK runs again, if the dangling name is still
2501 * there, the LFSCK should move the orphan directory object
2502 * back to the normal namespace. */
2503 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
2504 linkea_next_entry(ldata);
2508 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2510 RETURN(PTR_ERR(parent));
2512 if (!dt_object_exists(parent)) {
2513 lfsck_object_put(env, parent);
2514 if (ldata->ld_leh->leh_reccount > 1) {
2515 /* If it is NOT the last linkEA entry, then
2516 * there is still other chance to make the
2517 * child to be visible via other parent, then
2518 * remove this linkEA entry. */
2519 linkea_del_buf(ldata, cname);
2526 /* The linkEA entry with bad parent will be removed. */
2527 if (unlikely(!dt_try_as_dir(env, parent))) {
2528 lfsck_object_put(env, parent);
2529 linkea_del_buf(ldata, cname);
2533 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2534 (const struct dt_key *)cname->ln_name,
2536 *pfid2 = *lfsck_dto2fid(parent);
2537 if (rc == -ENOENT) {
2538 lfsck_object_put(env, parent);
2539 linkea_next_entry(ldata);
2544 lfsck_object_put(env, parent);
2549 if (lu_fid_eq(tfid, cfid)) {
2550 lfsck_object_put(env, parent);
2551 if (!lu_fid_eq(pfid, pfid2)) {
2552 *type = LNIT_UNMATCHED_PAIRS;
2553 rc = lfsck_namespace_repair_unmatched_pairs(env,
2554 com, child, pfid2, cname);
2560 /* It is the most common case that we find the
2561 * name entry corresponding to the linkEA entry
2562 * that matches the ".." name entry. */
2563 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2567 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2571 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2576 linkea_del_buf(ldata, cname);
2577 linkea_first_entry(ldata);
2578 /* There may be some invalid dangling name entries under
2579 * other parent directories, remove all of them. */
2580 while (ldata->ld_lee != NULL) {
2581 lfsck_namespace_unpack_linkea_entry(ldata,
2582 cname, tfid, info->lti_key);
2583 if (!fid_is_sane(tfid))
2586 parent = lfsck_object_find_bottom(env, lfsck,
2588 if (IS_ERR(parent)) {
2589 rc = PTR_ERR(parent);
2590 if (rc != -ENOENT &&
2591 bk->lb_param & LPF_FAILOUT)
2597 if (!dt_object_exists(parent)) {
2598 lfsck_object_put(env, parent);
2602 rc = lfsck_namespace_repair_dirent(env, com,
2603 parent, child, cname->ln_name,
2604 cname->ln_name, S_IFDIR, false, true);
2605 lfsck_object_put(env, parent);
2607 if (bk->lb_param & LPF_FAILOUT)
2616 linkea_del_buf(ldata, cname);
2619 ns->ln_dirent_repaired += count;
2624 lfsck_ibits_unlock(lh, LCK_EX);
2625 /* The name entry references another MDT-object that may be
2626 * created by the LFSCK for repairing dangling name entry.
2627 * Try to replace it. */
2628 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2630 lfsck_object_put(env, parent);
2637 linkea_del_buf(ldata, cname);
2640 if (ldata->ld_leh->leh_reccount == 1) {
2641 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2647 /* All linkEA entries are invalid and removed, then handle the @child
2649 if (ldata->ld_leh->leh_reccount == 0) {
2650 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2656 linkea_first_entry(ldata);
2657 /* If the dangling name entry for the orphan directory object has
2658 * been remvoed, then just check whether the directory object is
2659 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2665 /* There is no linkEA entry that matches the ".." name entry. Find
2666 * the first linkEA entry that both parent and name entry exist to
2667 * rebuild a new ".." name entry. */
2677 * Double scan the directory object for namespace LFSCK.
2679 * This function will verify the <parent, child> pairs in the namespace tree:
2680 * the parent references the child via some name entry that should be in the
2681 * child's linkEA entry, the child should back references the parent via its
2684 * The LFSCK will scan every linkEA entry in turn until find out the first
2685 * matched pairs. If found, then all other linkEA entries will be dropped.
2686 * If all the linkEA entries cannot match the ".." name entry, then there
2687 * are serveral possible cases:
2689 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2690 * in the linkEA entry is valid.
2692 * 2) If there are multiple linkEA entries, then try to find the linkEA
2693 * that matches the ".." name entry. If found, then all other entries
2694 * are invalid; otherwise, it is quite possible that the ".." name entry
2695 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2696 * entry according to the first valid linkEA entry (both the parent and
2697 * the name entry should exist).
2699 * 3) If the directory object has no (valid) linkEA entry, then the
2700 * directory object will be handled as pure orphan and inserted
2701 * in the .lustre/lost+found/MDTxxxx/ with the name:
2702 * ${self_FID}-${PFID}-D-${conflict_version}
2704 * \param[in] env pointer to the thread context
2705 * \param[in] com pointer to the lfsck component
2706 * \param[in] child pointer to the directory object to be handled
2707 * \param[in] flags to indicate the specical checking on the @child
2709 * \retval positive number for repaired cases
2710 * \retval 0 if nothing to be repaired
2711 * \retval negative error number on failure
2713 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
2714 struct lfsck_component *com,
2715 struct dt_object *child, __u8 flags)
2717 struct lfsck_thread_info *info = lfsck_env_info(env);
2718 const struct lu_fid *cfid = lfsck_dto2fid(child);
2719 struct lu_fid *pfid = &info->lti_fid2;
2720 struct lfsck_namespace *ns = com->lc_file_ram;
2721 struct lfsck_instance *lfsck = com->lc_lfsck;
2722 struct lustre_handle lh = { 0 };
2723 struct linkea_data ldata = { 0 };
2724 bool unknown = false;
2727 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
2731 LASSERT(!dt_object_remote(child));
2733 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
2734 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
2735 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
2736 "the namespace LFSCK, then the LFSCK cannot guarantee"
2737 "all the name entries have been verified in first-stage"
2738 "scanning. So have to skip orphan related handling for"
2739 "the directory object "DFID" with remote name entry\n",
2740 lfsck_lfsck2name(lfsck), PFID(cfid));
2745 if (unlikely(!dt_try_as_dir(env, child)))
2746 GOTO(out, rc = -ENOTDIR);
2748 /* We only take ldlm lock on the @child when required. When the
2749 * logic comes here for the first time, it is always false. */
2753 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2754 MDS_INODELOCK_UPDATE |
2755 MDS_INODELOCK_XATTR, LCK_EX);
2760 dt_read_lock(env, child, 0);
2761 if (unlikely(lfsck_is_dead_obj(child))) {
2762 dt_read_unlock(env, child);
2767 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
2768 (const struct dt_key *)dotdot, BYPASS_CAPA);
2770 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
2771 dt_read_unlock(env, child);
2776 if (!lustre_handle_is_used(&lh)) {
2777 dt_read_unlock(env, child);
2782 } else if (lfsck->li_lpf_obj != NULL &&
2783 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
2787 rc = lfsck_links_read(env, child, &ldata);
2788 dt_read_unlock(env, child);
2790 if (rc != -ENODATA && rc != -EINVAL)
2793 if (!lustre_handle_is_used(&lh))
2796 if (rc == -EINVAL && !fid_is_zero(pfid)) {
2797 /* Remove the corrupted linkEA. */
2798 rc = lfsck_namespace_links_remove(env, com, child);
2800 /* Here, because of the crashed linkEA, we
2801 * cannot know whether there is some parent
2802 * that references the child directory via
2803 * some name entry or not. So keep it there,
2804 * when the LFSCK run next time, if there is
2805 * some parent that references this object,
2806 * then the LFSCK can rebuild the linkEA;
2807 * otherwise, this object will be handled
2808 * as orphan as above. */
2811 /* 1. If we have neither ".." nor linkEA,
2812 * then it is an orphan.
2814 * 2. If we only have the ".." name entry,
2815 * but no parent references this child
2816 * directory, then handle it as orphan. */
2817 lfsck_ibits_unlock(&lh, LCK_EX);
2818 type = LNIT_MUL_REF;
2820 /* If the LFSCK is marked as LF_INCOMPLETE,
2821 * then means some MDT has ever tried to
2822 * verify some remote MDT-object that resides
2823 * on this MDT, but this MDT failed to respond
2824 * such request. So means there may be some
2825 * remote name entry on other MDT that
2826 * references this object with another name,
2827 * so we cannot know whether this linkEA is
2828 * valid or not. So keep it there and maybe
2829 * resolved when next LFSCK run. */
2830 if (ns->ln_flags & LF_INCOMPLETE)
2833 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2834 "-"DFID, PFID(pfid));
2835 rc = lfsck_namespace_insert_orphan(env, com, child,
2836 info->lti_tmpbuf, "D", NULL);
2842 linkea_first_entry(&ldata);
2843 /* This is the most common case: the object has unique linkEA entry. */
2844 if (ldata.ld_leh->leh_reccount == 1) {
2845 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
2846 &lh, &type, &retry);
2848 LASSERT(!lustre_handle_is_used(&lh));
2857 if (!lustre_handle_is_used(&lh))
2860 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
2861 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
2867 /* When we come here, the cases usually like that:
2868 * 1) The directory object has a corrupted linkEA entry. During the
2869 * first-stage scanning, the LFSCK cannot know such corruption,
2870 * then it appends the right linkEA entry according to the found
2871 * name entry after the bad one.
2873 * 2) The directory object has a right linkEA entry. During the
2874 * first-stage scanning, the LFSCK finds some bad name entry,
2875 * but the LFSCK cannot aware that at that time, then it adds
2876 * the bad linkEA entry for further processing. */
2877 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
2883 lfsck_ibits_unlock(&lh, LCK_EX);
2886 case LNIT_BAD_LINKEA:
2887 ns->ln_linkea_repaired++;
2889 case LNIT_UNMATCHED_PAIRS:
2890 ns->ln_unmatched_pairs_repaired++;
2893 ns->ln_mul_ref_repaired++;
2901 ns->ln_unknown_inconsistency++;
2907 * Double scan the MDT-object for namespace LFSCK.
2909 * If the MDT-object contains invalid or repeated linkEA entries, then drop
2910 * those entries from the linkEA; if the linkEA becomes empty or the object
2911 * has no linkEA, then it is an orphan and will be added into the directory
2912 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
2913 * the remote parent; if the name entry corresponding to some linkEA entry
2914 * is lost, then add the name entry back to the namespace.
2916 * \param[in] env pointer to the thread context
2917 * \param[in] com pointer to the lfsck component
2918 * \param[in] child pointer to the dt_object to be handled
2919 * \param[in] flags some hints to indicate how the @child should be handled
2921 * \retval positive number for repaired cases
2922 * \retval 0 if nothing to be repaired
2923 * \retval negative error number on failure
2925 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
2926 struct lfsck_component *com,
2927 struct dt_object *child, __u8 flags)
2929 struct lfsck_thread_info *info = lfsck_env_info(env);
2930 struct lu_attr *la = &info->lti_la;
2931 struct lu_name *cname = &info->lti_name;
2932 struct lu_fid *pfid = &info->lti_fid;
2933 struct lu_fid *cfid = &info->lti_fid2;
2934 struct lfsck_instance *lfsck = com->lc_lfsck;
2935 struct lfsck_namespace *ns = com->lc_file_ram;
2936 struct dt_object *parent = NULL;
2937 struct linkea_data ldata = { 0 };
2938 bool repaired = false;
2943 dt_read_lock(env, child, 0);
2944 if (unlikely(lfsck_is_dead_obj(child))) {
2945 dt_read_unlock(env, child);
2950 if (S_ISDIR(lfsck_object_type(child))) {
2951 dt_read_unlock(env, child);
2952 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
2957 rc = lfsck_links_read(env, child, &ldata);
2958 dt_read_unlock(env, child);
2962 linkea_first_entry(&ldata);
2963 while (ldata.ld_lee != NULL) {
2964 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
2966 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
2968 /* Found repeated linkEA entries */
2970 rc = lfsck_namespace_shrink_linkea(env, com, child,
2971 &ldata, cname, pfid, false);
2983 /* Invalid PFID in the linkEA entry. */
2984 if (!fid_is_sane(pfid)) {
2985 rc = lfsck_namespace_shrink_linkea(env, com, child,
2986 &ldata, cname, pfid, true);
2996 parent = lfsck_object_find_bottom(env, lfsck, pfid);
2998 GOTO(out, rc = PTR_ERR(parent));
3000 if (!dt_object_exists(parent)) {
3003 if (ldata.ld_leh->leh_reccount > 1) {
3004 /* If it is NOT the last linkEA entry, then
3005 * there is still other chance to make the
3006 * child to be visible via other parent, then
3007 * remove this linkEA entry. */
3008 rc = lfsck_namespace_shrink_linkea(env, com,
3009 child, &ldata, cname, pfid, true);
3011 /* If the LFSCK is marked as LF_INCOMPLETE,
3012 * then means some MDT has ever tried to
3013 * verify some remote MDT-object that resides
3014 * on this MDT, but this MDT failed to respond
3015 * such request. So means there may be some
3016 * remote name entry on other MDT that
3017 * references this object with another name,
3018 * so we cannot know whether this linkEA is
3019 * valid or not. So keep it there and maybe
3020 * resolved when next LFSCK run. */
3021 if (ns->ln_flags & LF_INCOMPLETE) {
3022 lfsck_object_put(env, parent);
3027 /* Create the lost parent as an orphan. */
3028 rc = lfsck_namespace_create_orphan(env, com,
3031 lfsck_object_put(env, parent);
3039 /* Add the missing name entry to the parent. */
3040 rc = lfsck_namespace_insert_normal(env, com,
3041 parent, child, cname->ln_name);
3042 if (unlikely(rc == -EEXIST))
3043 /* Unfortunately, someone reused the
3044 * name under the parent by race. So we
3045 * have to remove the linkEA entry from
3046 * current child object. It means that
3047 * the LFSCK cannot recover the system
3048 * totally back to its original status,
3049 * but it is necessary to make the
3050 * current system to be consistent. */
3051 rc = lfsck_namespace_shrink_linkea(env,
3055 linkea_next_entry(&ldata);
3058 lfsck_object_put(env, parent);
3068 /* The linkEA entry with bad parent will be removed. */
3069 if (unlikely(!dt_try_as_dir(env, parent))) {
3070 lfsck_object_put(env, parent);
3071 rc = lfsck_namespace_shrink_linkea(env, com, child,
3072 &ldata, cname, pfid, true);
3082 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3083 (const struct dt_key *)cname->ln_name,
3085 if (rc != 0 && rc != -ENOENT) {
3086 lfsck_object_put(env, parent);
3092 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3093 /* It is the most common case that we
3094 * find the name entry corresponding
3095 * to the linkEA entry. */
3096 lfsck_object_put(env, parent);
3097 linkea_next_entry(&ldata);
3099 /* The name entry references another
3100 * MDT-object that may be created by
3101 * the LFSCK for repairing dangling
3102 * name entry. Try to replace it. */
3103 rc = lfsck_namespace_replace_cond(env, com,
3104 parent, child, cfid, cname);
3105 lfsck_object_put(env, parent);
3111 linkea_next_entry(&ldata);
3113 rc = lfsck_namespace_shrink_linkea(env,
3127 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3131 /* If there is no name entry in the parent dir and the object
3132 * link count is less than the linkea entries count, then the
3133 * linkea entry should be removed. */
3134 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3135 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3136 parent, child, &ldata, cname, pfid);
3137 lfsck_object_put(env, parent);
3147 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3148 * MDT has ever tried to verify some remote MDT-object that
3149 * resides on this MDT, but this MDT failed to respond such
3150 * request. So means there may be some remote name entry on
3151 * other MDT that references this object with another name,
3152 * so we cannot know whether this linkEA is valid or not.
3153 * So keep it there and maybe resolved when next LFSCK run. */
3154 if (ns->ln_flags & LF_INCOMPLETE) {
3155 lfsck_object_put(env, parent);
3160 /* Add the missing name entry back to the namespace. */
3161 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3163 if (unlikely(rc == -ESTALE))
3164 /* It may happen when the remote object has been
3165 * removed, but the local MDT is not aware of that. */
3168 if (unlikely(rc == -EEXIST))
3169 /* Unfortunately, someone reused the name under the
3170 * parent by race. So we have to remove the linkEA
3171 * entry from current child object. It means that the
3172 * LFSCK cannot recover the system totally back to
3173 * its original status, but it is necessary to make
3174 * the current system to be consistent.
3176 * It also may be because of the LFSCK found some
3177 * internal status of create operation. Under such
3178 * case, nothing to be done. */
3179 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3180 parent, child, &ldata, cname, pfid);
3182 linkea_next_entry(&ldata);
3184 lfsck_object_put(env, parent);
3195 if (rc < 0 && rc != -ENODATA)
3199 LASSERT(ldata.ld_leh != NULL);
3201 count = ldata.ld_leh->leh_reccount;
3204 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3205 * MDT has ever tried to verify some remote MDT-object that
3206 * resides on this MDT, but this MDT failed to respond such
3207 * request. So means there may be some remote name entry on
3208 * other MDT that references this object with another name,
3209 * so we cannot know whether this linkEA is valid or not.
3210 * So keep it there and maybe resolved when next LFSCK run. */
3211 if (count == 0 && !(ns->ln_flags & LF_INCOMPLETE)) {
3212 /* If the child becomes orphan, then insert it into
3213 * the global .lustre/lost+found/MDTxxxx directory. */
3214 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
3220 ns->ln_mul_ref_repaired++;
3225 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3229 if (la->la_nlink != count) {
3230 /* XXX: there will be other patch(es) for MDT-object
3231 * hard links verification. */
3235 if (la->la_nlink > 1)
3236 ns->ln_mul_linked_repaired++;
3245 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3246 struct lfsck_namespace *ns,
3247 __u64 checked_phase1,
3248 __u64 checked_phase2,
3252 seq_printf(m, "checked_phase1: "LPU64"\n"
3253 "checked_phase2: "LPU64"\n"
3254 "updated_phase1: "LPU64"\n"
3255 "updated_phase2: "LPU64"\n"
3256 "failed_phase1: "LPU64"\n"
3257 "failed_phase2: "LPU64"\n"
3258 "directories: "LPU64"\n"
3259 "dirent_repaired: "LPU64"\n"
3260 "linkea_repaired: "LPU64"\n"
3261 "nlinks_repaired: "LPU64"\n"
3262 "multiple_linked_checked: "LPU64"\n"
3263 "multiple_linked_repaired: "LPU64"\n"
3264 "unknown_inconsistency: "LPU64"\n"
3265 "unmatched_pairs_repaired: "LPU64"\n"
3266 "dangling_repaired: "LPU64"\n"
3267 "multiple_referenced_repaired: "LPU64"\n"
3268 "bad_file_type_repaired: "LPU64"\n"
3269 "lost_dirent_repaired: "LPU64"\n"
3270 "success_count: %u\n"
3271 "run_time_phase1: %u seconds\n"
3272 "run_time_phase2: %u seconds\n",
3275 ns->ln_items_repaired,
3276 ns->ln_objs_repaired_phase2,
3277 ns->ln_items_failed,
3278 ns->ln_objs_failed_phase2,
3279 ns->ln_dirs_checked,
3280 ns->ln_dirent_repaired,
3281 ns->ln_linkea_repaired,
3282 ns->ln_objs_nlink_repaired,
3283 ns->ln_mul_linked_checked,
3284 ns->ln_mul_linked_repaired,
3285 ns->ln_unknown_inconsistency,
3286 ns->ln_unmatched_pairs_repaired,
3287 ns->ln_dangling_repaired,
3288 ns->ln_mul_ref_repaired,
3289 ns->ln_bad_type_repaired,
3290 ns->ln_lost_dirent_repaired,
3291 ns->ln_success_count,
3296 /* namespace APIs */
3298 static int lfsck_namespace_reset(const struct lu_env *env,
3299 struct lfsck_component *com, bool init)
3301 struct lfsck_instance *lfsck = com->lc_lfsck;
3302 struct lfsck_namespace *ns = com->lc_file_ram;
3303 struct lfsck_assistant_data *lad = com->lc_data;
3304 struct dt_object *root;
3305 struct dt_object *dto;
3309 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3311 GOTO(log, rc = PTR_ERR(root));
3313 if (unlikely(!dt_try_as_dir(env, root)))
3314 GOTO(put, rc = -ENOTDIR);
3316 down_write(&com->lc_sem);
3318 memset(ns, 0, sizeof(*ns));
3320 __u32 count = ns->ln_success_count;
3321 __u64 last_time = ns->ln_time_last_complete;
3323 memset(ns, 0, sizeof(*ns));
3324 ns->ln_success_count = count;
3325 ns->ln_time_last_complete = last_time;
3327 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3328 ns->ln_status = LS_INIT;
3330 rc = local_object_unlink(env, lfsck->li_bottom, root,
3331 lfsck_namespace_name);
3335 lfsck_object_put(env, com->lc_obj);
3337 dto = local_index_find_or_create(env, lfsck->li_los, root,
3338 lfsck_namespace_name,
3339 S_IFREG | S_IRUGO | S_IWUSR,
3340 &dt_lfsck_features);
3342 GOTO(out, rc = PTR_ERR(dto));
3345 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
3349 lad->lad_incomplete = 0;
3350 CFS_RESET_BITMAP(lad->lad_bitmap);
3352 rc = lfsck_namespace_store(env, com);
3357 up_write(&com->lc_sem);
3360 lu_object_put(env, &root->do_lu);
3362 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3363 lfsck_lfsck2name(lfsck), rc);
3368 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3371 struct lfsck_namespace *ns = com->lc_file_ram;
3373 down_write(&com->lc_sem);
3375 com->lc_new_checked++;
3376 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3377 up_write(&com->lc_sem);
3380 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3381 struct lfsck_component *com, bool init)
3383 struct lfsck_instance *lfsck = com->lc_lfsck;
3384 struct lfsck_namespace *ns = com->lc_file_ram;
3388 rc = lfsck_checkpoint_generic(env, com);
3393 down_write(&com->lc_sem);
3395 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3397 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3398 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3399 HALF_SEC - lfsck->li_time_last_checkpoint);
3400 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3401 ns->ln_items_checked += com->lc_new_checked;
3402 com->lc_new_checked = 0;
3405 rc = lfsck_namespace_store(env, com);
3406 up_write(&com->lc_sem);
3409 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3410 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3411 lfsck->li_pos_current.lp_oit_cookie,
3412 PFID(&lfsck->li_pos_current.lp_dir_parent),
3413 lfsck->li_pos_current.lp_dir_cookie, rc);
3415 return rc > 0 ? 0 : rc;
3418 static int lfsck_namespace_prep(const struct lu_env *env,
3419 struct lfsck_component *com,
3420 struct lfsck_start_param *lsp)
3422 struct lfsck_instance *lfsck = com->lc_lfsck;
3423 struct lfsck_namespace *ns = com->lc_file_ram;
3424 struct lfsck_position *pos = &com->lc_pos_start;
3427 rc = lfsck_namespace_load_bitmap(env, com);
3428 if (rc > 0 || (rc == 0 && ns->ln_status == LS_COMPLETED)) {
3429 rc = lfsck_namespace_reset(env, com, false);
3431 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3435 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: rc = %d\n",
3436 lfsck_lfsck2name(lfsck), rc);
3441 down_write(&com->lc_sem);
3442 ns->ln_time_latest_start = cfs_time_current_sec();
3443 spin_lock(&lfsck->li_lock);
3445 if (ns->ln_flags & LF_SCANNED_ONCE) {
3446 if (!lfsck->li_drop_dryrun ||
3447 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3448 ns->ln_status = LS_SCANNING_PHASE2;
3449 list_move_tail(&com->lc_link,
3450 &lfsck->li_list_double_scan);
3451 if (!list_empty(&com->lc_link_dir))
3452 list_del_init(&com->lc_link_dir);
3453 lfsck_pos_set_zero(pos);
3455 ns->ln_status = LS_SCANNING_PHASE1;
3456 ns->ln_run_time_phase1 = 0;
3457 ns->ln_run_time_phase2 = 0;
3458 ns->ln_items_checked = 0;
3459 ns->ln_items_repaired = 0;
3460 ns->ln_items_failed = 0;
3461 ns->ln_dirs_checked = 0;
3462 ns->ln_objs_checked_phase2 = 0;
3463 ns->ln_objs_repaired_phase2 = 0;
3464 ns->ln_objs_failed_phase2 = 0;
3465 ns->ln_objs_nlink_repaired = 0;
3466 ns->ln_dirent_repaired = 0;
3467 ns->ln_linkea_repaired = 0;
3468 ns->ln_mul_linked_checked = 0;
3469 ns->ln_mul_linked_repaired = 0;
3470 ns->ln_unknown_inconsistency = 0;
3471 ns->ln_unmatched_pairs_repaired = 0;
3472 ns->ln_dangling_repaired = 0;
3473 ns->ln_mul_ref_repaired = 0;
3474 ns->ln_bad_type_repaired = 0;
3475 ns->ln_lost_dirent_repaired = 0;
3476 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3477 if (list_empty(&com->lc_link_dir))
3478 list_add_tail(&com->lc_link_dir,
3479 &lfsck->li_list_dir);
3480 *pos = ns->ln_pos_first_inconsistent;
3483 ns->ln_status = LS_SCANNING_PHASE1;
3484 if (list_empty(&com->lc_link_dir))
3485 list_add_tail(&com->lc_link_dir,
3486 &lfsck->li_list_dir);
3487 if (!lfsck->li_drop_dryrun ||
3488 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3489 *pos = ns->ln_pos_last_checkpoint;
3490 pos->lp_oit_cookie++;
3492 *pos = ns->ln_pos_first_inconsistent;
3496 spin_unlock(&lfsck->li_lock);
3497 up_write(&com->lc_sem);
3499 rc = lfsck_start_assistant(env, com, lsp);
3501 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
3502 DFID", "LPX64"]: rc = %d\n",
3503 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
3504 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
3509 static int lfsck_namespace_exec_oit(const struct lu_env *env,
3510 struct lfsck_component *com,
3511 struct dt_object *obj)
3513 struct lfsck_thread_info *info = lfsck_env_info(env);
3514 struct lfsck_namespace *ns = com->lc_file_ram;
3515 struct lfsck_instance *lfsck = com->lc_lfsck;
3516 const struct lu_fid *fid = lfsck_dto2fid(obj);
3517 struct lu_attr *la = &info->lti_la;
3518 struct lu_fid *pfid = &info->lti_fid2;
3519 struct lu_name *cname = &info->lti_name;
3520 struct lu_seq_range *range = &info->lti_range;
3521 struct dt_device *dev = lfsck->li_bottom;
3522 struct seq_server_site *ss =
3523 lu_site2seq(dev->dd_lu_dev.ld_site);
3524 struct linkea_data ldata = { 0 };
3525 __u32 idx = lfsck_dev_idx(dev);
3529 rc = lfsck_links_read(env, obj, &ldata);
3533 /* -EINVAL means crashed linkEA, should be verified. */
3534 if (rc == -EINVAL) {
3535 rc = lfsck_namespace_trace_update(env, com, fid,
3536 LNTF_CHECK_LINKEA, true);
3538 struct lustre_handle lh = { 0 };
3540 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3541 MDS_INODELOCK_UPDATE |
3542 MDS_INODELOCK_XATTR, LCK_EX);
3544 rc = lfsck_namespace_links_remove(env, com,
3546 lfsck_ibits_unlock(&lh, LCK_EX);
3550 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
3553 /* zero-linkEA object may be orphan, but it also maybe because
3554 * of upgrading. Currently, we cannot record it for double scan.
3555 * Because it may cause the LFSCK tracing file to be too large. */
3556 if (rc == -ENODATA) {
3557 if (S_ISDIR(lfsck_object_type(obj)))
3560 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3564 if (la->la_nlink > 1)
3565 rc = lfsck_namespace_trace_update(env, com, fid,
3566 LNTF_CHECK_LINKEA, true);
3574 /* Record multiple-linked object. */
3575 if (ldata.ld_leh->leh_reccount > 1) {
3576 rc = lfsck_namespace_trace_update(env, com, fid,
3577 LNTF_CHECK_LINKEA, true);
3582 linkea_first_entry(&ldata);
3583 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
3584 if (!fid_is_sane(pfid)) {
3585 rc = lfsck_namespace_trace_update(env, com, fid,
3586 LNTF_CHECK_PARENT, true);
3588 fld_range_set_mdt(range);
3589 rc = fld_local_lookup(env, ss->ss_server_fld,
3590 fid_seq(pfid), range);
3591 if ((rc == -ENOENT) ||
3592 (rc == 0 && range->lsr_index != idx)) {
3593 rc = lfsck_namespace_trace_update(env, com, fid,
3594 LNTF_CHECK_LINKEA, true);
3596 if (S_ISDIR(lfsck_object_type(obj)))
3599 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3603 if (la->la_nlink > 1)
3604 rc = lfsck_namespace_trace_update(env, com,
3605 fid, LNTF_CHECK_LINKEA, true);
3612 down_write(&com->lc_sem);
3613 com->lc_new_checked++;
3614 if (S_ISDIR(lfsck_object_type(obj)))
3615 ns->ln_dirs_checked++;
3617 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3618 up_write(&com->lc_sem);
3623 static int lfsck_namespace_exec_dir(const struct lu_env *env,
3624 struct lfsck_component *com,
3625 struct lu_dirent *ent, __u16 type)
3627 struct lfsck_assistant_data *lad = com->lc_data;
3628 struct lfsck_namespace_req *lnr;
3629 bool wakeup = false;
3631 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
3633 struct lfsck_namespace *ns = com->lc_file_ram;
3635 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3636 return PTR_ERR(lnr);
3639 spin_lock(&lad->lad_lock);
3640 if (lad->lad_assistant_status < 0) {
3641 spin_unlock(&lad->lad_lock);
3642 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3643 return lad->lad_assistant_status;
3646 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3647 if (lad->lad_prefetched == 0)
3650 lad->lad_prefetched++;
3651 spin_unlock(&lad->lad_lock);
3653 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3655 down_write(&com->lc_sem);
3656 com->lc_new_checked++;
3657 up_write(&com->lc_sem);
3662 static int lfsck_namespace_post(const struct lu_env *env,
3663 struct lfsck_component *com,
3664 int result, bool init)
3666 struct lfsck_instance *lfsck = com->lc_lfsck;
3667 struct lfsck_namespace *ns = com->lc_file_ram;
3671 lfsck_post_generic(env, com, &result);
3673 down_write(&com->lc_sem);
3674 spin_lock(&lfsck->li_lock);
3676 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3678 ns->ln_status = LS_SCANNING_PHASE2;
3679 ns->ln_flags |= LF_SCANNED_ONCE;
3680 ns->ln_flags &= ~LF_UPGRADE;
3681 list_del_init(&com->lc_link_dir);
3682 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
3683 } else if (result == 0) {
3684 ns->ln_status = lfsck->li_status;
3685 if (ns->ln_status == 0)
3686 ns->ln_status = LS_STOPPED;
3687 if (ns->ln_status != LS_PAUSED) {
3688 list_del_init(&com->lc_link_dir);
3689 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
3692 ns->ln_status = LS_FAILED;
3693 list_del_init(&com->lc_link_dir);
3694 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
3696 spin_unlock(&lfsck->li_lock);
3699 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3700 HALF_SEC - lfsck->li_time_last_checkpoint);
3701 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3702 ns->ln_items_checked += com->lc_new_checked;
3703 com->lc_new_checked = 0;
3706 rc = lfsck_namespace_store(env, com);
3707 up_write(&com->lc_sem);
3709 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
3710 lfsck_lfsck2name(lfsck), rc);
3716 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
3719 struct lfsck_instance *lfsck = com->lc_lfsck;
3720 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
3721 struct lfsck_namespace *ns = com->lc_file_ram;
3724 down_read(&com->lc_sem);
3725 seq_printf(m, "name: lfsck_namespace\n"
3731 lfsck_status2names(ns->ln_status));
3733 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
3737 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
3741 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
3742 "time_since_last_completed");
3746 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
3747 "time_since_latest_start");
3751 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
3752 "time_since_last_checkpoint");
3756 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
3757 "latest_start_position");
3761 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
3762 "last_checkpoint_position");
3766 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
3767 "first_failure_position");
3771 if (ns->ln_status == LS_SCANNING_PHASE1) {
3772 struct lfsck_position pos;
3773 const struct dt_it_ops *iops;
3774 cfs_duration_t duration = cfs_time_current() -
3775 lfsck->li_time_last_checkpoint;
3776 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
3777 __u64 speed = checked;
3778 __u64 new_checked = com->lc_new_checked * HZ;
3779 __u32 rtime = ns->ln_run_time_phase1 +
3780 cfs_duration_sec(duration + HALF_SEC);
3783 do_div(new_checked, duration);
3785 do_div(speed, rtime);
3786 lfsck_namespace_dump_statistics(m, ns, checked,
3787 ns->ln_objs_checked_phase2,
3788 rtime, ns->ln_run_time_phase2);
3790 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
3791 "average_speed_phase2: N/A\n"
3792 "real_time_speed_phase1: "LPU64" items/sec\n"
3793 "real_time_speed_phase2: N/A\n",
3797 LASSERT(lfsck->li_di_oit != NULL);
3799 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
3801 /* The low layer otable-based iteration position may NOT
3802 * exactly match the namespace-based directory traversal
3803 * cookie. Generally, it is not a serious issue. But the
3804 * caller should NOT make assumption on that. */
3805 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
3806 if (!lfsck->li_current_oit_processed)
3807 pos.lp_oit_cookie--;
3809 spin_lock(&lfsck->li_lock);
3810 if (lfsck->li_di_dir != NULL) {
3811 pos.lp_dir_cookie = lfsck->li_cookie_dir;
3812 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
3813 fid_zero(&pos.lp_dir_parent);
3814 pos.lp_dir_cookie = 0;
3817 *lfsck_dto2fid(lfsck->li_obj_dir);
3820 fid_zero(&pos.lp_dir_parent);
3821 pos.lp_dir_cookie = 0;
3823 spin_unlock(&lfsck->li_lock);
3824 lfsck_pos_dump(m, &pos, "current_position");
3825 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
3826 cfs_duration_t duration = cfs_time_current() -
3827 lfsck->li_time_last_checkpoint;
3828 __u64 checked = ns->ln_objs_checked_phase2 +
3829 com->lc_new_checked;
3830 __u64 speed1 = ns->ln_items_checked;
3831 __u64 speed2 = checked;
3832 __u64 new_checked = com->lc_new_checked * HZ;
3833 __u32 rtime = ns->ln_run_time_phase2 +
3834 cfs_duration_sec(duration + HALF_SEC);
3837 do_div(new_checked, duration);
3838 if (ns->ln_run_time_phase1 != 0)
3839 do_div(speed1, ns->ln_run_time_phase1);
3841 do_div(speed2, rtime);
3842 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
3844 ns->ln_run_time_phase1, rtime);
3846 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
3847 "average_speed_phase2: "LPU64" objs/sec\n"
3848 "real_time_speed_phase1: N/A\n"
3849 "real_time_speed_phase2: "LPU64" objs/sec\n"
3850 "current_position: "DFID"\n",
3854 PFID(&ns->ln_fid_latest_scanned_phase2));
3856 __u64 speed1 = ns->ln_items_checked;
3857 __u64 speed2 = ns->ln_objs_checked_phase2;
3859 if (ns->ln_run_time_phase1 != 0)
3860 do_div(speed1, ns->ln_run_time_phase1);
3861 if (ns->ln_run_time_phase2 != 0)
3862 do_div(speed2, ns->ln_run_time_phase2);
3863 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
3864 ns->ln_objs_checked_phase2,
3865 ns->ln_run_time_phase1,
3866 ns->ln_run_time_phase2);
3868 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
3869 "average_speed_phase2: "LPU64" objs/sec\n"
3870 "real_time_speed_phase1: N/A\n"
3871 "real_time_speed_phase2: N/A\n"
3872 "current_position: N/A\n",
3877 up_read(&com->lc_sem);
3881 static int lfsck_namespace_double_scan(const struct lu_env *env,
3882 struct lfsck_component *com)
3884 struct lfsck_namespace *ns = com->lc_file_ram;
3886 return lfsck_double_scan_generic(env, com, ns->ln_status);
3889 static void lfsck_namespace_data_release(const struct lu_env *env,
3890 struct lfsck_component *com)
3892 struct lfsck_assistant_data *lad = com->lc_data;
3893 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
3894 struct lfsck_tgt_desc *ltd;
3895 struct lfsck_tgt_desc *next;
3897 LASSERT(lad != NULL);
3898 LASSERT(thread_is_init(&lad->lad_thread) ||
3899 thread_is_stopped(&lad->lad_thread));
3900 LASSERT(list_empty(&lad->lad_req_list));
3902 com->lc_data = NULL;
3904 spin_lock(<ds->ltd_lock);
3905 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
3906 ltd_namespace_phase_list) {
3907 list_del_init(<d->ltd_namespace_phase_list);
3909 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
3910 ltd_namespace_phase_list) {
3911 list_del_init(<d->ltd_namespace_phase_list);
3913 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
3914 ltd_namespace_list) {
3915 list_del_init(<d->ltd_namespace_list);
3917 spin_unlock(<ds->ltd_lock);
3919 CFS_FREE_BITMAP(lad->lad_bitmap);
3924 static int lfsck_namespace_in_notify(const struct lu_env *env,
3925 struct lfsck_component *com,
3926 struct lfsck_request *lr)
3928 struct lfsck_instance *lfsck = com->lc_lfsck;
3929 struct lfsck_namespace *ns = com->lc_file_ram;
3930 struct lfsck_assistant_data *lad = com->lc_data;
3931 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
3932 struct lfsck_tgt_desc *ltd;
3937 switch (lr->lr_event) {
3938 case LE_CREATE_ORPHAN: {
3939 struct dt_object *orphan = NULL;
3941 CDEBUG(D_LFSCK, "%s: namespace LFSCK handling notify from "
3942 "MDT %x to create orphan"DFID" with type %o\n",
3943 lfsck_lfsck2name(lfsck), lr->lr_index,
3944 PFID(&lr->lr_fid), lr->lr_type);
3946 orphan = lfsck_object_find(env, lfsck, &lr->lr_fid);
3948 GOTO(out_create, rc = PTR_ERR(orphan));
3950 if (dt_object_exists(orphan))
3951 GOTO(out_create, rc = -EEXIST);
3953 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
3956 GOTO(out_create, rc = (rc == 1) ? 0 : rc);
3959 CDEBUG(D_LFSCK, "%s: namespace LFSCK handled notify from "
3960 "MDT %x to create orphan"DFID" with type %o: rc = %d\n",
3961 lfsck_lfsck2name(lfsck), lr->lr_index,
3962 PFID(&lr->lr_fid), lr->lr_type, rc);
3964 if (orphan != NULL && !IS_ERR(orphan))
3965 lfsck_object_put(env, orphan);
3969 case LE_PHASE1_DONE:
3970 case LE_PHASE2_DONE:
3977 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
3978 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
3979 lr->lr_index, lr->lr_status, lr->lr_flags2);
3981 spin_lock(<ds->ltd_lock);
3982 ltd = LTD_TGT(ltds, lr->lr_index);
3984 spin_unlock(<ds->ltd_lock);
3989 list_del_init(<d->ltd_namespace_phase_list);
3990 switch (lr->lr_event) {
3991 case LE_PHASE1_DONE:
3992 if (lr->lr_status <= 0) {
3993 ltd->ltd_namespace_done = 1;
3994 list_del_init(<d->ltd_namespace_list);
3995 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
3996 "phase1 for namespace LFSCK: rc = %d.\n",
3997 lfsck_lfsck2name(lfsck),
3998 ltd->ltd_index, lr->lr_status);
3999 ns->ln_flags |= LF_INCOMPLETE;
4004 if (lr->lr_flags2 & LF_INCOMPLETE)
4005 ns->ln_flags |= LF_INCOMPLETE;
4007 if (list_empty(<d->ltd_namespace_list))
4008 list_add_tail(<d->ltd_namespace_list,
4009 &lad->lad_mdt_list);
4010 list_add_tail(<d->ltd_namespace_phase_list,
4011 &lad->lad_mdt_phase2_list);
4013 case LE_PHASE2_DONE:
4014 ltd->ltd_namespace_done = 1;
4015 list_del_init(<d->ltd_namespace_list);
4019 ltd->ltd_namespace_done = 1;
4020 list_del_init(<d->ltd_namespace_list);
4021 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4023 "%s: the peer MDT %x exit namespace LFSCK\n",
4024 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4025 ns->ln_flags |= LF_INCOMPLETE;
4031 spin_unlock(<ds->ltd_lock);
4033 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4034 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4036 memset(stop, 0, sizeof(*stop));
4037 stop->ls_status = lr->lr_status;
4038 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4039 lfsck_stop(env, lfsck->li_bottom, stop);
4040 } else if (lfsck_phase2_next_ready(lad)) {
4041 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4047 static int lfsck_namespace_query(const struct lu_env *env,
4048 struct lfsck_component *com)
4050 struct lfsck_namespace *ns = com->lc_file_ram;
4052 return ns->ln_status;
4055 static struct lfsck_operations lfsck_namespace_ops = {
4056 .lfsck_reset = lfsck_namespace_reset,
4057 .lfsck_fail = lfsck_namespace_fail,
4058 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4059 .lfsck_prep = lfsck_namespace_prep,
4060 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4061 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4062 .lfsck_post = lfsck_namespace_post,
4063 .lfsck_dump = lfsck_namespace_dump,
4064 .lfsck_double_scan = lfsck_namespace_double_scan,
4065 .lfsck_data_release = lfsck_namespace_data_release,
4066 .lfsck_quit = lfsck_quit_generic,
4067 .lfsck_in_notify = lfsck_namespace_in_notify,
4068 .lfsck_query = lfsck_namespace_query,
4072 * Repair dangling name entry.
4074 * For the name entry with dangling reference, we need to repare the
4075 * inconsistency according to the LFSCK sponsor's requirement:
4077 * 1) Keep the inconsistency there and report the inconsistency case,
4078 * then give the chance to the application to find related issues,
4079 * and the users can make the decision about how to handle it with
4080 * more human knownledge. (by default)
4082 * 2) Re-create the missing MDT-object with the FID information.
4084 * \param[in] env pointer to the thread context
4085 * \param[in] com pointer to the lfsck component
4086 * \param[in] child pointer to the object corresponding to the dangling
4088 * \param[in] lnr pointer to the namespace request that contains the
4089 * name's name, parent object, parent's LMV, and ect.
4091 * \retval positive number if no need to repair
4092 * \retval zero for repaired successfully
4093 * \retval negative error number on failure
4095 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4096 struct lfsck_component *com,
4097 struct dt_object *child,
4098 struct lfsck_namespace_req *lnr)
4100 struct lfsck_thread_info *info = lfsck_env_info(env);
4101 struct lu_attr *la = &info->lti_la;
4102 struct dt_allocation_hint *hint = &info->lti_hint;
4103 struct dt_object_format *dof = &info->lti_dof;
4104 struct dt_insert_rec *rec = &info->lti_dt_rec;
4105 struct dt_object *parent = lnr->lnr_obj;
4106 const struct lu_name *cname;
4107 struct linkea_data ldata = { 0 };
4108 struct lustre_handle lh = { 0 };
4109 struct lu_buf linkea_buf;
4110 struct lfsck_instance *lfsck = com->lc_lfsck;
4111 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4112 struct dt_device *dev = lfsck_obj2dt_dev(child);
4113 struct thandle *th = NULL;
4115 __u16 type = lnr->lnr_type;
4119 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4120 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4125 if (!create || bk->lb_param & LPF_DRYRUN)
4128 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4132 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
4136 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4137 MDS_INODELOCK_UPDATE, LCK_EX);
4141 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4145 th = dt_trans_create(env, dev);
4147 GOTO(log, rc = PTR_ERR(th));
4149 /* Set the ctime as zero, then others can know it is created for
4150 * repairing dangling name entry by LFSCK. And if the LFSCK made
4151 * wrong decision and the real MDT-object has been found later,
4152 * then the LFSCK has chance to fix the incosistency properly. */
4153 memset(la, 0, sizeof(*la));
4154 la->la_mode = (type & S_IFMT) | 0600;
4155 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4156 LA_ATIME | LA_MTIME | LA_CTIME;
4158 child->do_ops->do_ah_init(env, hint, parent, child,
4159 la->la_mode & S_IFMT);
4161 memset(dof, 0, sizeof(*dof));
4162 dof->dof_type = dt_mode_to_dft(type);
4163 /* If the target is a regular file, then the LFSCK will only create
4164 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4165 * OST-objects will be created when write open. */
4167 /* 1a. create child. */
4168 rc = dt_declare_create(env, child, la, hint, dof, th);
4172 if (S_ISDIR(type)) {
4173 if (unlikely(!dt_try_as_dir(env, child)))
4174 GOTO(stop, rc = -ENOTDIR);
4176 /* 2a. insert dot into child dir */
4177 rec->rec_type = S_IFDIR;
4178 rec->rec_fid = lfsck_dto2fid(child);
4179 rc = dt_declare_insert(env, child,
4180 (const struct dt_rec *)rec,
4181 (const struct dt_key *)dot, th);
4185 /* 3a. insert dotdot into child dir */
4186 rec->rec_fid = lfsck_dto2fid(parent);
4187 rc = dt_declare_insert(env, child,
4188 (const struct dt_rec *)rec,
4189 (const struct dt_key *)dotdot, th);
4193 /* 4a. increase child nlink */
4194 rc = dt_declare_ref_add(env, child, th);
4199 /* 5a. insert linkEA for child */
4200 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4201 ldata.ld_leh->leh_len);
4202 rc = dt_declare_xattr_set(env, child, &linkea_buf,
4203 XATTR_NAME_LINK, 0, th);
4207 rc = dt_trans_start(env, dev, th);
4209 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4211 dt_write_lock(env, child, 0);
4212 /* 1b. create child */
4213 rc = dt_create(env, child, la, hint, dof, th);
4215 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4217 if (S_ISDIR(type)) {
4218 if (unlikely(!dt_try_as_dir(env, child)))
4219 GOTO(unlock, rc = -ENOTDIR);
4221 /* 2b. insert dot into child dir */
4222 rec->rec_type = S_IFDIR;
4223 rec->rec_fid = lfsck_dto2fid(child);
4224 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4225 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4229 /* 3b. insert dotdot into child dir */
4230 rec->rec_fid = lfsck_dto2fid(parent);
4231 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4232 (const struct dt_key *)dotdot, th,
4237 /* 4b. increase child nlink */
4238 rc = dt_ref_add(env, child, th);
4243 /* 5b. insert linkEA for child. */
4244 rc = dt_xattr_set(env, child, &linkea_buf,
4245 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4250 dt_write_unlock(env, child);
4253 dt_trans_stop(env, dev, th);
4256 lfsck_ibits_unlock(&lh, LCK_EX);
4257 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4258 "reference for: parent "DFID", child "DFID", type %u, "
4259 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4260 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4261 type, cname->ln_name,
4262 create ? "Create the lost OST-object as required" :
4263 "Keep the MDT-object there by default", rc);
4266 struct lfsck_namespace *ns = com->lc_file_ram;
4268 ns->ln_flags |= LF_INCONSISTENT;
4274 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4275 struct lfsck_component *com,
4276 struct lfsck_assistant_req *lar)
4278 struct lfsck_thread_info *info = lfsck_env_info(env);
4279 struct lu_attr *la = &info->lti_la;
4280 struct lfsck_instance *lfsck = com->lc_lfsck;
4281 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4282 struct lfsck_namespace *ns = com->lc_file_ram;
4283 struct linkea_data ldata = { 0 };
4284 const struct lu_name *cname;
4285 struct thandle *handle = NULL;
4286 struct lfsck_namespace_req *lnr =
4287 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
4288 struct dt_object *dir = lnr->lnr_obj;
4289 struct dt_object *obj = NULL;
4290 const struct lu_fid *pfid = lfsck_dto2fid(dir);
4291 struct dt_device *dev = NULL;
4292 struct lustre_handle lh = { 0 };
4293 bool repaired = false;
4294 bool dtlocked = false;
4301 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
4304 if (lnr->lnr_attr & LUDA_UPGRADE) {
4305 ns->ln_flags |= LF_UPGRADE;
4306 ns->ln_dirent_repaired++;
4308 } else if (lnr->lnr_attr & LUDA_REPAIR) {
4309 ns->ln_flags |= LF_INCONSISTENT;
4310 ns->ln_dirent_repaired++;
4314 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
4315 if (strcmp(lnr->lnr_name, dotdot) != 0)
4318 rc = lfsck_namespace_trace_update(env, com, pfid,
4319 LNTF_CHECK_PARENT, true);
4324 if (lnr->lnr_name[0] == '.' &&
4325 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
4328 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
4330 GOTO(out, rc = idx);
4332 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
4333 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
4336 dev = lfsck->li_next;
4338 struct lfsck_tgt_desc *ltd;
4340 /* Usually, some local filesystem consistency verification
4341 * tools can guarantee the local namespace tree consistenct.
4342 * So the LFSCK will only verify the remote directory. */
4343 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
4344 rc = lfsck_namespace_trace_update(env, com, pfid,
4345 LNTF_CHECK_PARENT, true);
4350 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
4351 if (unlikely(ltd == NULL)) {
4352 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
4353 "did not join the namespace LFSCK\n",
4354 lfsck_lfsck2name(lfsck), idx);
4355 lfsck_lad_set_bitmap(env, com, idx);
4357 GOTO(out, rc = -ENODEV);
4363 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
4365 GOTO(out, rc = PTR_ERR(obj));
4367 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4368 if (dt_object_exists(obj) == 0) {
4371 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
4373 type = LNIT_DANGLING;
4374 rc = lfsck_namespace_repair_dangling(env, com,
4383 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
4386 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4387 MDS_INODELOCK_UPDATE |
4388 MDS_INODELOCK_XATTR, LCK_EX);
4392 handle = dt_trans_create(env, dev);
4394 GOTO(out, rc = PTR_ERR(handle));
4396 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
4400 rc = dt_trans_start(env, dev, handle);
4404 dt_write_lock(env, obj, 0);
4408 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
4412 rc = lfsck_links_read(env, obj, &ldata);
4413 if (unlikely(rc == -ENOENT)) {
4414 if (handle != NULL) {
4415 dt_write_unlock(env, obj);
4418 dt_trans_stop(env, dev, handle);
4421 lfsck_ibits_unlock(&lh, LCK_EX);
4424 /* It may happen when the remote object has been removed,
4425 * but the local MDT is not aware of that. */
4427 } else if (rc == 0) {
4428 count = ldata.ld_leh->leh_reccount;
4429 rc = linkea_links_find(&ldata, cname, pfid);
4431 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
4432 if ((lfsck_object_type(obj) & S_IFMT) !=
4434 ns->ln_flags |= LF_INCONSISTENT;
4435 type = LNIT_BAD_TYPE;
4441 ns->ln_flags |= LF_INCONSISTENT;
4443 /* If the file type stored in the name entry does not match
4444 * the file type claimed by the object, and the object does
4445 * not recognize the name entry, then it is quite possible
4446 * that the name entry is corrupted. */
4447 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
4448 type = LNIT_BAD_DIRENT;
4453 /* For sub-dir object, we cannot make sure whether the sub-dir
4454 * back references the parent via ".." name entry correctly or
4455 * not in the LFSCK first-stage scanning. It may be that the
4456 * (remote) sub-dir ".." name entry has no parent FID after
4457 * file-level backup/restore and its linkEA may be wrong.
4458 * So under such case, we should replace the linkEA according
4459 * to current name entry. But this needs to be done during the
4460 * LFSCK second-stage scanning. The LFSCK will record the name
4461 * entry for further possible using. */
4465 } else if (unlikely(rc == -EINVAL)) {
4466 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
4467 type = LNIT_BAD_TYPE;
4470 ns->ln_flags |= LF_INCONSISTENT;
4471 /* The magic crashed, we are not sure whether there are more
4472 * corrupt data in the linkea, so remove all linkea entries. */
4476 } else if (rc == -ENODATA) {
4477 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
4478 type = LNIT_BAD_TYPE;
4481 ns->ln_flags |= LF_UPGRADE;
4486 if (bk->lb_param & LPF_DRYRUN) {
4487 ns->ln_linkea_repaired++;
4493 if (!lustre_handle_is_used(&lh))
4499 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
4506 rc = linkea_data_new(&ldata,
4507 &lfsck_env_info(env)->lti_linkea_buf);
4512 rc = linkea_add_buf(&ldata, cname, pfid);
4516 rc = lfsck_links_write(env, obj, &ldata, handle);
4520 count = ldata.ld_leh->leh_reccount;
4521 if (!S_ISDIR(lfsck_object_type(obj)) ||
4522 !dt_object_remote(obj)) {
4523 ns->ln_linkea_repaired++;
4534 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4538 if ((count == 1 && la->la_nlink == 1) ||
4539 S_ISDIR(lfsck_object_type(obj)))
4540 /* Usually, it is for single linked object or dir, do nothing.*/
4543 /* Following modification will be in another transaction. */
4544 if (handle != NULL) {
4545 dt_write_unlock(env, obj);
4548 dt_trans_stop(env, dev, handle);
4551 lfsck_ibits_unlock(&lh, LCK_EX);
4554 ns->ln_mul_linked_checked++;
4555 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
4556 LNTF_CHECK_LINKEA, true);
4562 dt_write_unlock(env, obj);
4564 if (handle != NULL && !IS_ERR(handle))
4565 dt_trans_stop(env, dev, handle);
4568 lfsck_ibits_unlock(&lh, LCK_EX);
4574 rc = lfsck_namespace_repair_dirent(env, com, dir,
4575 obj, lnr->lnr_name, lnr->lnr_name,
4576 lnr->lnr_type, true, false);
4580 case LNIT_BAD_DIRENT:
4582 /* XXX: This is a bad dirent, we do not know whether
4583 * the original name entry reference a regular
4584 * file or a directory, then keep the parent's
4585 * nlink count unchanged here. */
4586 rc = lfsck_namespace_repair_dirent(env, com, dir,
4587 obj, lnr->lnr_name, lnr->lnr_name,
4588 lnr->lnr_type, false, false);
4597 down_write(&com->lc_sem);
4599 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
4600 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
4601 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
4602 PFID(lfsck_dto2fid(lnr->lnr_obj)),
4603 lnr->lnr_namelen, lnr->lnr_name, rc);
4605 lfsck_namespace_record_failure(env, lfsck, ns);
4606 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
4607 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
4608 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
4609 dev != NULL && dev != lfsck->li_next)
4610 lfsck_lad_set_bitmap(env, com, idx);
4612 if (!(bk->lb_param & LPF_FAILOUT))
4616 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
4617 "repaired the entry: "DFID", parent "DFID
4618 ", name %.*s\n", lfsck_lfsck2name(lfsck),
4619 PFID(&lnr->lnr_fid),
4620 PFID(lfsck_dto2fid(lnr->lnr_obj)),
4621 lnr->lnr_namelen, lnr->lnr_name);
4624 ns->ln_items_repaired++;
4628 ns->ln_dangling_repaired++;
4631 ns->ln_bad_type_repaired++;
4633 case LNIT_BAD_DIRENT:
4634 ns->ln_dirent_repaired++;
4640 if (bk->lb_param & LPF_DRYRUN &&
4641 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
4642 lfsck_pos_fill(env, lfsck,
4643 &ns->ln_pos_first_inconsistent,
4649 up_write(&com->lc_sem);
4651 if (obj != NULL && !IS_ERR(obj))
4652 lfsck_object_put(env, obj);
4657 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
4658 struct lfsck_component *com)
4660 struct lfsck_instance *lfsck = com->lc_lfsck;
4661 struct ptlrpc_thread *thread = &lfsck->li_thread;
4662 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4663 struct lfsck_namespace *ns = com->lc_file_ram;
4664 struct dt_object *obj = com->lc_obj;
4665 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
4666 struct dt_object *target;
4674 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
4675 lfsck_lfsck2name(lfsck));
4677 com->lc_new_checked = 0;
4678 com->lc_new_scanned = 0;
4679 com->lc_time_last_checkpoint = cfs_time_current();
4680 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
4681 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4683 di = iops->init(env, obj, 0, BYPASS_CAPA);
4685 RETURN(PTR_ERR(di));
4687 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
4688 rc = iops->get(env, di, (const struct dt_key *)&fid);
4692 /* Skip the start one, which either has been processed or non-exist. */
4693 rc = iops->next(env, di);
4698 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
4700 struct l_wait_info lwi;
4702 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
4704 l_wait_event(thread->t_ctl_waitq,
4705 !thread_is_running(thread),
4708 if (unlikely(!thread_is_running(thread)))
4712 key = iops->key(env, di);
4713 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
4714 if (!fid_is_sane(&fid)) {
4719 target = lfsck_object_find(env, lfsck, &fid);
4720 if (IS_ERR(target)) {
4721 rc = PTR_ERR(target);
4725 if (dt_object_exists(target)) {
4726 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
4728 rc = lfsck_namespace_double_scan_one(env, com,
4735 lfsck_object_put(env, target);
4738 down_write(&com->lc_sem);
4739 com->lc_new_checked++;
4740 com->lc_new_scanned++;
4741 ns->ln_fid_latest_scanned_phase2 = fid;
4743 ns->ln_objs_repaired_phase2++;
4745 ns->ln_objs_failed_phase2++;
4746 up_write(&com->lc_sem);
4748 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
4751 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
4752 cfs_time_current())) &&
4753 com->lc_new_checked != 0) {
4754 down_write(&com->lc_sem);
4755 ns->ln_run_time_phase2 +=
4756 cfs_duration_sec(cfs_time_current() +
4757 HALF_SEC - com->lc_time_last_checkpoint);
4758 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4759 ns->ln_objs_checked_phase2 += com->lc_new_checked;
4760 com->lc_new_checked = 0;
4761 rc = lfsck_namespace_store(env, com);
4762 up_write(&com->lc_sem);
4766 com->lc_time_last_checkpoint = cfs_time_current();
4767 com->lc_time_next_checkpoint =
4768 com->lc_time_last_checkpoint +
4769 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
4772 lfsck_control_speed_by_self(com);
4773 if (unlikely(!thread_is_running(thread)))
4776 rc = iops->next(env, di);
4785 iops->fini(env, di);
4787 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
4788 lfsck_lfsck2name(lfsck), rc);
4793 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
4794 struct lfsck_component *com,
4795 struct lfsck_position *pos)
4797 struct lfsck_assistant_data *lad = com->lc_data;
4798 struct lfsck_namespace_req *lnr;
4800 if (list_empty(&lad->lad_req_list))
4803 lnr = list_entry(lad->lad_req_list.next,
4804 struct lfsck_namespace_req,
4806 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
4807 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
4808 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
4811 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
4812 struct lfsck_component *com,
4815 struct lfsck_instance *lfsck = com->lc_lfsck;
4816 struct lfsck_namespace *ns = com->lc_file_ram;
4818 down_write(&com->lc_sem);
4819 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
4820 HALF_SEC - lfsck->li_time_last_checkpoint);
4821 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4822 ns->ln_objs_checked_phase2 += com->lc_new_checked;
4823 com->lc_new_checked = 0;
4826 if (ns->ln_flags & LF_INCOMPLETE)
4827 ns->ln_status = LS_PARTIAL;
4829 ns->ln_status = LS_COMPLETED;
4830 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
4831 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
4832 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
4833 ns->ln_success_count++;
4834 } else if (rc == 0) {
4835 ns->ln_status = lfsck->li_status;
4836 if (ns->ln_status == 0)
4837 ns->ln_status = LS_STOPPED;
4839 ns->ln_status = LS_FAILED;
4842 rc = lfsck_namespace_store(env, com);
4843 up_write(&com->lc_sem);
4849 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
4850 struct ptlrpc_request *req,
4857 * Notify remote LFSCK instances about former failures.
4859 * The local LFSCK instance has recorded which MDTs have ever failed to respond
4860 * some LFSCK verification requests (maybe because of network issues or the MDT
4861 * itself trouble). During the respond gap the MDT may missed some name entries
4862 * verification, then the MDT cannot know whether related MDT-objects have been
4863 * referenced by related name entries or not, then in the second-stage scanning,
4864 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
4865 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
4866 * name entry for repairing the orphan.
4868 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
4869 * it will scan the bitmap for the ever failed MDTs, and notify them that they
4870 * have ever missed some name entries verification and should skip the handling
4871 * for orphan MDT-objects.
4873 * \param[in] env pointer to the thread context
4874 * \param[in] com pointer to the lfsck component
4875 * \param[in] lr pointer to the lfsck request
4877 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
4878 struct lfsck_component *com,
4879 struct lfsck_request *lr)
4881 struct lfsck_async_interpret_args *laia =
4882 &lfsck_env_info(env)->lti_laia2;
4883 struct lfsck_assistant_data *lad = com->lc_data;
4884 struct lfsck_namespace *ns = com->lc_file_ram;
4885 struct lfsck_instance *lfsck = com->lc_lfsck;
4886 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4887 struct lfsck_tgt_desc *ltd;
4888 struct ptlrpc_request_set *set;
4892 set = ptlrpc_prep_set();
4894 GOTO(out, rc = -ENOMEM);
4896 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
4897 memset(laia, 0, sizeof(*laia));
4898 lad->lad_touch_gen++;
4900 spin_lock(<ds->ltd_lock);
4901 while (!list_empty(&lad->lad_mdt_list)) {
4902 ltd = list_entry(lad->lad_mdt_list.next,
4903 struct lfsck_tgt_desc,
4904 ltd_namespace_list);
4905 if (ltd->ltd_namespace_gen == lad->lad_touch_gen)
4908 ltd->ltd_namespace_gen = lad->lad_touch_gen;
4909 list_move_tail(<d->ltd_namespace_list,
4910 &lad->lad_mdt_list);
4911 if (!lad->lad_incomplete ||
4912 !cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
4913 ltd->ltd_namespace_failed = 0;
4917 ltd->ltd_namespace_failed = 1;
4918 spin_unlock(<ds->ltd_lock);
4919 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
4920 lfsck_namespace_assistant_sync_failures_interpret,
4921 laia, LFSCK_NOTIFY);
4923 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
4924 "to sync failure with MDT %x: rc = %d\n",
4925 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
4927 spin_lock(<ds->ltd_lock);
4929 spin_unlock(<ds->ltd_lock);
4931 rc = ptlrpc_set_wait(set);
4932 ptlrpc_set_destroy(set);
4938 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
4939 "to sync failure with MDTs, and related MDTs "
4940 "may handle orphan un-properly: rc = %d\n",
4941 lfsck_lfsck2name(lfsck), rc);
4946 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
4947 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
4948 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
4949 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
4950 .la_double_scan_result = lfsck_namespace_double_scan_result,
4951 .la_req_fini = lfsck_namespace_assistant_req_fini,
4952 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
4956 * Verify the specified linkEA entry for the given directory object.
4957 * If the object has no such linkEA entry or it has more other linkEA
4958 * entries, then re-generate the linkEA with the given information.
4960 * \param[in] env pointer to the thread context
4961 * \param[in] dev pointer to the dt_device
4962 * \param[in] obj pointer to the dt_object to be handled
4963 * \param[in] cname the name for the child in the parent directory
4964 * \param[in] pfid the parent directory's FID for the linkEA
4966 * \retval 0 for success
4967 * \retval negative error number on failure
4969 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
4970 struct dt_object *obj, const struct lu_name *cname,
4971 const struct lu_fid *pfid)
4973 struct linkea_data ldata = { 0 };
4974 struct lu_buf linkea_buf;
4977 int fl = LU_XATTR_CREATE;
4981 LASSERT(S_ISDIR(lfsck_object_type(obj)));
4983 rc = lfsck_links_read(env, obj, &ldata);
4984 if (rc == -ENODATA) {
4986 } else if (rc == 0) {
4987 fl = LU_XATTR_REPLACE;
4988 if (ldata.ld_leh->leh_reccount != 1) {
4991 rc = linkea_links_find(&ldata, cname, pfid);
5000 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
5004 rc = linkea_add_buf(&ldata, cname, pfid);
5008 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
5009 ldata.ld_leh->leh_len);
5010 th = dt_trans_create(env, dev);
5012 RETURN(PTR_ERR(th));
5014 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
5015 XATTR_NAME_LINK, fl, th);
5019 rc = dt_trans_start_local(env, dev, th);
5023 dt_write_lock(env, obj, 0);
5024 rc = dt_xattr_set(env, obj, &linkea_buf,
5025 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
5026 dt_write_unlock(env, obj);
5031 dt_trans_stop(env, dev, th);
5036 * Get the name and parent directory's FID from the first linkEA entry.
5038 * \param[in] env pointer to the thread context
5039 * \param[in] obj pointer to the object which get linkEA from
5040 * \param[out] name pointer to the buffer to hold the name
5041 * in the first linkEA entry
5042 * \param[out] pfid pointer to the buffer to hold the parent
5043 * directory's FID in the first linkEA entry
5045 * \retval 0 for success
5046 * \retval negative error number on failure
5048 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
5049 char *name, struct lu_fid *pfid)
5051 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
5052 struct linkea_data ldata = { 0 };
5055 rc = lfsck_links_read(env, obj, &ldata);
5059 linkea_first_entry(&ldata);
5060 if (ldata.ld_lee == NULL)
5063 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
5064 /* To guarantee the 'name' is terminated with '0'. */
5065 memcpy(name, cname->ln_name, cname->ln_namelen);
5066 name[cname->ln_namelen] = 0;
5072 * Remove the name entry from the parent directory.
5074 * No need to care about the object referenced by the name entry,
5075 * either the name entry is invalid or redundant, or the referenced
5076 * object has been processed has been or will be handled by others.
5078 * \param[in] env pointer to the thread context
5079 * \param[in] lfsck pointer to the lfsck instance
5080 * \param[in] parent pointer to the lost+found object
5081 * \param[in] name the name for the name entry to be removed
5082 * \param[in] type the type for the name entry to be removed
5084 * \retval 0 for success
5085 * \retval negative error number on failure
5087 int lfsck_remove_name_entry(const struct lu_env *env,
5088 struct lfsck_instance *lfsck,
5089 struct dt_object *parent,
5090 const char *name, __u32 type)
5092 struct dt_device *dev = lfsck->li_next;
5094 struct lustre_handle lh = { 0 };
5098 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
5099 MDS_INODELOCK_UPDATE, LCK_EX);
5103 th = dt_trans_create(env, dev);
5105 GOTO(unlock, rc = PTR_ERR(th));
5107 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
5111 if (S_ISDIR(type)) {
5112 rc = dt_declare_ref_del(env, parent, th);
5117 rc = dt_trans_start(env, dev, th);
5121 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
5126 if (S_ISDIR(type)) {
5127 dt_write_lock(env, parent, 0);
5128 rc = dt_ref_del(env, parent, th);
5129 dt_write_unlock(env, parent);
5135 dt_trans_stop(env, dev, th);
5138 lfsck_ibits_unlock(&lh, LCK_EX);
5140 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
5141 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
5142 PFID(lfsck_dto2fid(parent)), name, type, rc);
5148 * Update the object's name entry with the given FID.
5150 * \param[in] env pointer to the thread context
5151 * \param[in] lfsck pointer to the lfsck instance
5152 * \param[in] parent pointer to the parent directory that holds
5154 * \param[in] name the name for the entry to be updated
5155 * \param[in] pfid the new PFID for the name entry
5156 * \param[in] type the type for the name entry to be updated
5158 * \retval 0 for success
5159 * \retval negative error number on failure
5161 int lfsck_update_name_entry(const struct lu_env *env,
5162 struct lfsck_instance *lfsck,
5163 struct dt_object *parent, const char *name,
5164 const struct lu_fid *pfid, __u32 type)
5166 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
5167 struct dt_device *dev = lfsck->li_next;
5168 struct lustre_handle lh = { 0 };
5174 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
5175 MDS_INODELOCK_UPDATE, LCK_EX);
5179 th = dt_trans_create(env, dev);
5181 GOTO(unlock, rc = PTR_ERR(th));
5183 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
5187 rec->rec_type = type;
5188 rec->rec_fid = pfid;
5189 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
5190 (const struct dt_key *)name, th);
5194 rc = dt_declare_ref_add(env, parent, th);
5198 rc = dt_trans_start(env, dev, th);
5202 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
5204 if (rc == -ENOENT) {
5212 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
5213 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
5214 if (rc == 0 && S_ISDIR(type) && !exists) {
5215 dt_write_lock(env, parent, 0);
5216 rc = dt_ref_add(env, parent, th);
5217 dt_write_unlock(env, parent);
5223 dt_trans_stop(env, dev, th);
5226 lfsck_ibits_unlock(&lh, LCK_EX);
5228 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
5229 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
5230 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
5235 int lfsck_namespace_setup(const struct lu_env *env,
5236 struct lfsck_instance *lfsck)
5238 struct lfsck_component *com;
5239 struct lfsck_namespace *ns;
5240 struct dt_object *root = NULL;
5241 struct dt_object *obj;
5245 LASSERT(lfsck->li_master);
5251 INIT_LIST_HEAD(&com->lc_link);
5252 INIT_LIST_HEAD(&com->lc_link_dir);
5253 init_rwsem(&com->lc_sem);
5254 atomic_set(&com->lc_ref, 1);
5255 com->lc_lfsck = lfsck;
5256 com->lc_type = LFSCK_TYPE_NAMESPACE;
5257 com->lc_ops = &lfsck_namespace_ops;
5258 com->lc_data = lfsck_assistant_data_init(
5259 &lfsck_namespace_assistant_ops,
5261 if (com->lc_data == NULL)
5262 GOTO(out, rc = -ENOMEM);
5264 com->lc_file_size = sizeof(struct lfsck_namespace);
5265 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
5266 if (com->lc_file_ram == NULL)
5267 GOTO(out, rc = -ENOMEM);
5269 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
5270 if (com->lc_file_disk == NULL)
5271 GOTO(out, rc = -ENOMEM);
5273 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
5275 GOTO(out, rc = PTR_ERR(root));
5277 if (unlikely(!dt_try_as_dir(env, root)))
5278 GOTO(out, rc = -ENOTDIR);
5280 obj = local_index_find_or_create(env, lfsck->li_los, root,
5281 lfsck_namespace_name,
5282 S_IFREG | S_IRUGO | S_IWUSR,
5283 &dt_lfsck_features);
5285 GOTO(out, rc = PTR_ERR(obj));
5288 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
5292 rc = lfsck_namespace_load(env, com);
5294 rc = lfsck_namespace_reset(env, com, true);
5295 else if (rc == -ENODATA)
5296 rc = lfsck_namespace_init(env, com);
5300 ns = com->lc_file_ram;
5301 switch (ns->ln_status) {
5306 spin_lock(&lfsck->li_lock);
5307 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
5308 spin_unlock(&lfsck->li_lock);
5311 CERROR("%s: unknown lfsck_namespace status %d\n",
5312 lfsck_lfsck2name(lfsck), ns->ln_status);
5314 case LS_SCANNING_PHASE1:
5315 case LS_SCANNING_PHASE2:
5316 /* No need to store the status to disk right now.
5317 * If the system crashed before the status stored,
5318 * it will be loaded back when next time. */
5319 ns->ln_status = LS_CRASHED;
5323 spin_lock(&lfsck->li_lock);
5324 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
5325 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
5326 spin_unlock(&lfsck->li_lock);
5333 if (root != NULL && !IS_ERR(root))
5334 lu_object_put(env, &root->do_lu);
5336 lfsck_component_cleanup(env, com);
5337 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
5338 lfsck_lfsck2name(lfsck), rc);