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) 2013, 2017, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lu_object.h>
34 #include <dt_object.h>
35 #include <md_object.h>
36 #include <lustre_fid.h>
37 #include <lustre_lib.h>
38 #include <lustre_net.h>
40 #include "lfsck_internal.h"
42 #define LFSCK_NAMESPACE_MAGIC_V1 0xA0629D03
43 #define LFSCK_NAMESPACE_MAGIC_V2 0xA0621A0B
44 #define LFSCK_NAMESPACE_MAGIC_V3 0xA06249FF
46 /* For Lustre-2.x (x <= 6), the namespace LFSCK used LFSCK_NAMESPACE_MAGIC_V1
47 * as the trace file magic. When downgrade to such old release, the old LFSCK
48 * will not recognize the new LFSCK_NAMESPACE_MAGIC_V2 in the new trace file,
49 * then it will reset the whole LFSCK, and will not cause start failure. The
50 * similar case will happen when upgrade from such old release. */
51 #define LFSCK_NAMESPACE_MAGIC LFSCK_NAMESPACE_MAGIC_V3
53 enum lfsck_nameentry_check {
54 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
55 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
56 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
59 static struct lfsck_namespace_req *
60 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
61 struct lfsck_assistant_object *lso,
62 struct lu_dirent *ent, __u16 type)
64 struct lfsck_namespace_req *lnr;
67 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
70 return ERR_PTR(-ENOMEM);
72 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
73 lnr->lnr_lar.lar_parent = lfsck_assistant_object_get(lso);
74 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
75 lnr->lnr_fid = ent->lde_fid;
76 lnr->lnr_dir_cookie = ent->lde_hash;
77 lnr->lnr_attr = ent->lde_attrs;
80 lnr->lnr_namelen = ent->lde_namelen;
81 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
86 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
87 struct lfsck_assistant_req *lar)
89 struct lfsck_namespace_req *lnr =
90 container_of(lar, struct lfsck_namespace_req, lnr_lar);
92 if (lnr->lnr_lmv != NULL)
93 lfsck_lmv_put(env, lnr->lnr_lmv);
95 lfsck_assistant_object_put(env, lar->lar_parent);
96 OBD_FREE(lnr, lnr->lnr_size);
99 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
100 struct lfsck_namespace *src)
102 dst->ln_magic = le32_to_cpu(src->ln_magic);
103 dst->ln_status = le32_to_cpu(src->ln_status);
104 dst->ln_flags = le32_to_cpu(src->ln_flags);
105 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
106 dst->ln_run_time_phase1 = le64_to_cpu(src->ln_run_time_phase1);
107 dst->ln_run_time_phase2 = le64_to_cpu(src->ln_run_time_phase2);
108 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
109 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
110 dst->ln_time_last_checkpoint =
111 le64_to_cpu(src->ln_time_last_checkpoint);
112 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
113 &src->ln_pos_latest_start);
114 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
115 &src->ln_pos_last_checkpoint);
116 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
117 &src->ln_pos_first_inconsistent);
118 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
119 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
120 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
121 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
122 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
123 dst->ln_objs_repaired_phase2 =
124 le64_to_cpu(src->ln_objs_repaired_phase2);
125 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
126 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
127 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
128 &src->ln_fid_latest_scanned_phase2);
129 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
130 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
131 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
132 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
133 dst->ln_unknown_inconsistency =
134 le64_to_cpu(src->ln_unknown_inconsistency);
135 dst->ln_unmatched_pairs_repaired =
136 le64_to_cpu(src->ln_unmatched_pairs_repaired);
137 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
138 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
139 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
140 dst->ln_lost_dirent_repaired =
141 le64_to_cpu(src->ln_lost_dirent_repaired);
142 dst->ln_striped_dirs_scanned =
143 le64_to_cpu(src->ln_striped_dirs_scanned);
144 dst->ln_striped_dirs_repaired =
145 le64_to_cpu(src->ln_striped_dirs_repaired);
146 dst->ln_striped_dirs_failed =
147 le64_to_cpu(src->ln_striped_dirs_failed);
148 dst->ln_striped_dirs_disabled =
149 le64_to_cpu(src->ln_striped_dirs_disabled);
150 dst->ln_striped_dirs_skipped =
151 le64_to_cpu(src->ln_striped_dirs_skipped);
152 dst->ln_striped_shards_scanned =
153 le64_to_cpu(src->ln_striped_shards_scanned);
154 dst->ln_striped_shards_repaired =
155 le64_to_cpu(src->ln_striped_shards_repaired);
156 dst->ln_striped_shards_failed =
157 le64_to_cpu(src->ln_striped_shards_failed);
158 dst->ln_striped_shards_skipped =
159 le64_to_cpu(src->ln_striped_shards_skipped);
160 dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
161 dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
162 dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
163 dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
164 dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
165 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
166 dst->ln_time_latest_reset = le64_to_cpu(src->ln_time_latest_reset);
167 dst->ln_linkea_overflow_cleared =
168 le64_to_cpu(src->ln_linkea_overflow_cleared);
169 dst->ln_agent_entries_repaired =
170 le64_to_cpu(src->ln_agent_entries_repaired);
173 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
174 struct lfsck_namespace *src)
176 dst->ln_magic = cpu_to_le32(src->ln_magic);
177 dst->ln_status = cpu_to_le32(src->ln_status);
178 dst->ln_flags = cpu_to_le32(src->ln_flags);
179 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
180 dst->ln_run_time_phase1 = cpu_to_le64(src->ln_run_time_phase1);
181 dst->ln_run_time_phase2 = cpu_to_le64(src->ln_run_time_phase2);
182 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
183 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
184 dst->ln_time_last_checkpoint =
185 cpu_to_le64(src->ln_time_last_checkpoint);
186 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
187 &src->ln_pos_latest_start);
188 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
189 &src->ln_pos_last_checkpoint);
190 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
191 &src->ln_pos_first_inconsistent);
192 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
193 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
194 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
195 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
196 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
197 dst->ln_objs_repaired_phase2 =
198 cpu_to_le64(src->ln_objs_repaired_phase2);
199 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
200 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
201 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
202 &src->ln_fid_latest_scanned_phase2);
203 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
204 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
205 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
206 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
207 dst->ln_unknown_inconsistency =
208 cpu_to_le64(src->ln_unknown_inconsistency);
209 dst->ln_unmatched_pairs_repaired =
210 cpu_to_le64(src->ln_unmatched_pairs_repaired);
211 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
212 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
213 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
214 dst->ln_lost_dirent_repaired =
215 cpu_to_le64(src->ln_lost_dirent_repaired);
216 dst->ln_striped_dirs_scanned =
217 cpu_to_le64(src->ln_striped_dirs_scanned);
218 dst->ln_striped_dirs_repaired =
219 cpu_to_le64(src->ln_striped_dirs_repaired);
220 dst->ln_striped_dirs_failed =
221 cpu_to_le64(src->ln_striped_dirs_failed);
222 dst->ln_striped_dirs_disabled =
223 cpu_to_le64(src->ln_striped_dirs_disabled);
224 dst->ln_striped_dirs_skipped =
225 cpu_to_le64(src->ln_striped_dirs_skipped);
226 dst->ln_striped_shards_scanned =
227 cpu_to_le64(src->ln_striped_shards_scanned);
228 dst->ln_striped_shards_repaired =
229 cpu_to_le64(src->ln_striped_shards_repaired);
230 dst->ln_striped_shards_failed =
231 cpu_to_le64(src->ln_striped_shards_failed);
232 dst->ln_striped_shards_skipped =
233 cpu_to_le64(src->ln_striped_shards_skipped);
234 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
235 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
236 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
237 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
238 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
239 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
240 dst->ln_time_latest_reset = cpu_to_le64(src->ln_time_latest_reset);
241 dst->ln_linkea_overflow_cleared =
242 cpu_to_le64(src->ln_linkea_overflow_cleared);
243 dst->ln_agent_entries_repaired =
244 cpu_to_le64(src->ln_agent_entries_repaired);
247 static void lfsck_namespace_record_failure(const struct lu_env *env,
248 struct lfsck_instance *lfsck,
249 struct lfsck_namespace *ns)
251 struct lfsck_position pos;
253 ns->ln_items_failed++;
254 lfsck_pos_fill(env, lfsck, &pos, false);
255 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
256 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
257 ns->ln_pos_first_inconsistent = pos;
259 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
260 "inconsistency at the pos [%llu, "DFID", %#llx]\n",
261 lfsck_lfsck2name(lfsck),
262 ns->ln_pos_first_inconsistent.lp_oit_cookie,
263 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
264 ns->ln_pos_first_inconsistent.lp_dir_cookie);
269 * Load the MDT bitmap from the lfsck_namespace trace file.
271 * \param[in] env pointer to the thread context
272 * \param[in] com pointer to the lfsck component
274 * \retval 0 for success
275 * \retval negative error number on failure or data corruption
277 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
278 struct lfsck_component *com)
280 struct dt_object *obj = com->lc_obj;
281 struct lfsck_assistant_data *lad = com->lc_data;
282 struct lfsck_namespace *ns = com->lc_file_ram;
283 struct cfs_bitmap *bitmap = lad->lad_bitmap;
289 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
291 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
293 nbits = ns->ln_bitmap_size;
295 if (unlikely(nbits < BITS_PER_LONG))
296 nbits = BITS_PER_LONG;
298 if (nbits > bitmap->size) {
299 __u32 new_bits = bitmap->size;
300 struct cfs_bitmap *new_bitmap;
302 while (new_bits < nbits)
305 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
306 if (new_bitmap == NULL)
309 lad->lad_bitmap = new_bitmap;
310 CFS_FREE_BITMAP(bitmap);
314 if (ns->ln_bitmap_size == 0) {
315 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
316 CFS_RESET_BITMAP(bitmap);
321 size = (ns->ln_bitmap_size + 7) >> 3;
322 rc = dt_xattr_get(env, obj,
323 lfsck_buf_get(env, bitmap->data, size),
324 XATTR_NAME_LFSCK_BITMAP);
326 RETURN(rc >= 0 ? -EINVAL : rc);
328 if (cfs_bitmap_check_empty(bitmap))
329 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
331 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
337 * Load namespace LFSCK statistics information from the trace file.
339 * \param[in] env pointer to the thread context
340 * \param[in] com pointer to the lfsck component
342 * \retval 0 for success
343 * \retval negative error number on failure
345 static int lfsck_namespace_load(const struct lu_env *env,
346 struct lfsck_component *com)
348 int len = com->lc_file_size;
351 rc = dt_xattr_get(env, com->lc_obj,
352 lfsck_buf_get(env, com->lc_file_disk, len),
353 XATTR_NAME_LFSCK_NAMESPACE);
355 struct lfsck_namespace *ns = com->lc_file_ram;
357 lfsck_namespace_le_to_cpu(ns,
358 (struct lfsck_namespace *)com->lc_file_disk);
359 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
360 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
361 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
362 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
367 } else if (rc != -ENODATA) {
368 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
369 "expected = %d: rc = %d\n",
370 lfsck_lfsck2name(com->lc_lfsck), len, rc);
378 static int lfsck_namespace_store(const struct lu_env *env,
379 struct lfsck_component *com)
381 struct dt_object *obj = com->lc_obj;
382 struct lfsck_instance *lfsck = com->lc_lfsck;
383 struct lfsck_namespace *ns = com->lc_file_ram;
384 struct lfsck_assistant_data *lad = com->lc_data;
385 struct dt_device *dev = lfsck_obj2dev(obj);
386 struct cfs_bitmap *bitmap = NULL;
387 struct thandle *handle;
389 int len = com->lc_file_size;
394 bitmap = lad->lad_bitmap;
395 nbits = bitmap->size;
398 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
401 ns->ln_bitmap_size = nbits;
402 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
404 handle = dt_trans_create(env, dev);
406 GOTO(log, rc = PTR_ERR(handle));
408 rc = dt_declare_xattr_set(env, obj,
409 lfsck_buf_get(env, com->lc_file_disk, len),
410 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
414 if (bitmap != NULL) {
415 rc = dt_declare_xattr_set(env, obj,
416 lfsck_buf_get(env, bitmap->data, nbits >> 3),
417 XATTR_NAME_LFSCK_BITMAP, 0, handle);
422 rc = dt_trans_start_local(env, dev, handle);
426 rc = dt_xattr_set(env, obj,
427 lfsck_buf_get(env, com->lc_file_disk, len),
428 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
429 if (rc == 0 && bitmap != NULL)
430 rc = dt_xattr_set(env, obj,
431 lfsck_buf_get(env, bitmap->data, nbits >> 3),
432 XATTR_NAME_LFSCK_BITMAP, 0, handle);
437 dt_trans_stop(env, dev, handle);
441 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
442 lfsck_lfsck2name(lfsck), rc);
446 static int lfsck_namespace_init(const struct lu_env *env,
447 struct lfsck_component *com)
449 struct lfsck_namespace *ns = com->lc_file_ram;
452 memset(ns, 0, sizeof(*ns));
453 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
454 ns->ln_status = LS_INIT;
455 ns->ln_time_latest_reset = ktime_get_real_seconds();
456 down_write(&com->lc_sem);
457 rc = lfsck_namespace_store(env, com);
459 rc = lfsck_load_sub_trace_files(env, com,
460 &dt_lfsck_namespace_features, LFSCK_NAMESPACE, true);
461 up_write(&com->lc_sem);
467 * Update the namespace LFSCK trace file for the given @fid
469 * \param[in] env pointer to the thread context
470 * \param[in] com pointer to the lfsck component
471 * \param[in] fid the fid which flags to be updated in the lfsck
473 * \param[in] add true if add new flags, otherwise remove flags
475 * \retval 0 for success or nothing to be done
476 * \retval negative error number on failure
478 int lfsck_namespace_trace_update(const struct lu_env *env,
479 struct lfsck_component *com,
480 const struct lu_fid *fid,
481 const __u8 flags, bool add)
483 struct lfsck_instance *lfsck = com->lc_lfsck;
484 struct dt_object *obj;
485 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
486 struct dt_device *dev;
487 struct thandle *th = NULL;
496 if (unlikely(!fid_is_sane(fid)))
499 idx = lfsck_sub_trace_file_fid2idx(fid);
500 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
501 obj = com->lc_sub_trace_objs[idx].lsto_obj;
502 if (unlikely(obj == NULL)) {
503 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
507 lfsck_object_get(obj);
508 dev = lfsck_obj2dev(obj);
509 fid_cpu_to_be(key, fid);
510 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
511 (const struct dt_key *)key);
514 GOTO(unlock, rc = 0);
518 } else if (rc == 0) {
520 if ((old & flags) == flags)
521 GOTO(unlock, rc = 0);
525 if ((old & flags) == 0)
526 GOTO(unlock, rc = 0);
534 th = dt_trans_create(env, dev);
536 GOTO(log, rc = PTR_ERR(th));
539 rc = dt_declare_delete(env, obj,
540 (const struct dt_key *)key, th);
546 rc = dt_declare_insert(env, obj,
547 (const struct dt_rec *)&new,
548 (const struct dt_key *)key, th);
553 rc = dt_trans_start_local(env, dev, th);
558 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
564 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
565 (const struct dt_key *)key, th);
573 if (th != NULL && !IS_ERR(th))
574 dt_trans_stop(env, dev, th);
576 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
577 "trace file, flags %x, old %x, new %x: rc = %d\n",
578 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
579 (__u32)flags, (__u32)old, (__u32)new, rc);
582 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
583 lfsck_object_put(env, obj);
588 int lfsck_namespace_check_exist(const struct lu_env *env,
589 struct dt_object *dir,
590 struct dt_object *obj, const char *name)
592 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
596 if (unlikely(lfsck_is_dead_obj(obj)))
597 RETURN(LFSCK_NAMEENTRY_DEAD);
599 rc = dt_lookup_dir(env, dir, name, fid);
601 RETURN(LFSCK_NAMEENTRY_REMOVED);
606 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
607 RETURN(LFSCK_NAMEENTRY_RECREATED);
612 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
613 struct dt_object *obj,
614 struct thandle *handle)
618 /* For remote updating LINKEA, there may be further LFSCK action
619 * on remote MDT after the updating, so update the LINKEA ASAP. */
620 if (dt_object_remote(obj))
623 /* For destroying all invalid linkEA entries. */
624 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
626 /* For insert new linkEA entry. */
627 rc = dt_declare_xattr_set(env, obj,
628 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
629 XATTR_NAME_LINK, 0, handle);
633 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
634 struct linkea_data *ldata, bool with_rec)
638 if (ldata->ld_buf->lb_buf == NULL)
641 if (!dt_object_exists(obj))
644 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
646 /* Buf was too small, figure out what we need. */
647 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
648 if (unlikely(rc == 0))
654 lu_buf_realloc(ldata->ld_buf, rc);
655 if (ldata->ld_buf->lb_buf == NULL)
658 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
661 if (unlikely(rc == 0))
666 rc = linkea_init_with_rec(ldata);
668 rc = linkea_init(ldata);
675 * Remove linkEA for the given object.
677 * The caller should take the ldlm lock before the calling.
679 * \param[in] env pointer to the thread context
680 * \param[in] com pointer to the lfsck component
681 * \param[in] obj pointer to the dt_object to be handled
683 * \retval 0 for repaired cases
684 * \retval negative error number on failure
686 static int lfsck_namespace_links_remove(const struct lu_env *env,
687 struct lfsck_component *com,
688 struct dt_object *obj)
690 struct lfsck_instance *lfsck = com->lc_lfsck;
691 struct dt_device *dev = lfsck_obj2dev(obj);
692 struct thandle *th = NULL;
696 LASSERT(dt_object_remote(obj) == 0);
698 th = dt_trans_create(env, dev);
700 GOTO(log, rc = PTR_ERR(th));
702 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
706 rc = dt_trans_start_local(env, dev, th);
710 dt_write_lock(env, obj, 0);
711 if (unlikely(lfsck_is_dead_obj(obj)))
712 GOTO(unlock, rc = -ENOENT);
714 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
715 GOTO(unlock, rc = 0);
717 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
722 dt_write_unlock(env, obj);
725 dt_trans_stop(env, dev, th);
728 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
729 "for the object "DFID": rc = %d\n",
730 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
733 struct lfsck_namespace *ns = com->lc_file_ram;
735 ns->ln_flags |= LF_INCONSISTENT;
741 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
742 struct linkea_data *ldata, struct thandle *handle)
747 lfsck_buf_init(&buf, ldata->ld_buf->lb_buf, ldata->ld_leh->leh_len);
750 rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LINK, 0, handle);
751 if (unlikely(rc == -ENOSPC)) {
752 rc = linkea_overflow_shrink(ldata);
753 if (likely(rc > 0)) {
762 static inline bool linkea_reclen_is_valid(const struct linkea_data *ldata)
764 if (ldata->ld_reclen <= 0)
767 if ((char *)ldata->ld_lee + ldata->ld_reclen >
768 (char *)ldata->ld_leh + ldata->ld_leh->leh_len)
774 static inline bool linkea_entry_is_valid(const struct linkea_data *ldata,
775 const struct lu_name *cname,
776 const struct lu_fid *pfid)
778 if (!linkea_reclen_is_valid(ldata))
781 if (cname->ln_namelen <= 0 || cname->ln_namelen > NAME_MAX)
784 if (!fid_is_sane(pfid))
790 static int lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
791 struct lu_name *cname,
793 char *buf, const int buflen)
795 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
796 if (unlikely(!linkea_entry_is_valid(ldata, cname, pfid)))
799 /* To guarantee the 'name' is terminated with '0'. */
800 memcpy(buf, cname->ln_name, cname->ln_namelen);
801 buf[cname->ln_namelen] = 0;
802 cname->ln_name = buf;
807 static void lfsck_linkea_del_buf(struct linkea_data *ldata,
808 const struct lu_name *lname)
810 LASSERT(ldata->ld_leh != NULL && ldata->ld_lee != NULL);
812 /* If current record is corrupted, all the subsequent
813 * records will be dropped. */
814 if (unlikely(!linkea_reclen_is_valid(ldata))) {
815 void *ptr = ldata->ld_lee;
817 ldata->ld_leh->leh_len = sizeof(struct link_ea_header);
818 ldata->ld_leh->leh_reccount = 0;
819 linkea_first_entry(ldata);
820 while (ldata->ld_lee != NULL &&
821 (char *)ldata->ld_lee < (char *)ptr) {
822 int reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
823 ldata->ld_lee->lee_reclen[1];
825 ldata->ld_leh->leh_len += reclen;
826 ldata->ld_leh->leh_reccount++;
827 ldata->ld_lee = (struct link_ea_entry *)
828 ((char *)ldata->ld_lee + reclen);
831 ldata->ld_lee = NULL;
833 linkea_del_buf(ldata, lname);
837 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
838 struct lu_name *cname,
842 struct link_ea_entry *oldlee;
846 oldlee = ldata->ld_lee;
847 oldlen = ldata->ld_reclen;
848 linkea_next_entry(ldata);
849 while (ldata->ld_lee != NULL) {
850 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
851 ldata->ld_lee->lee_reclen[1];
852 if (unlikely(!linkea_reclen_is_valid(ldata))) {
853 lfsck_linkea_del_buf(ldata, NULL);
854 LASSERT(ldata->ld_lee == NULL);
855 } else if (unlikely(ldata->ld_reclen == oldlen &&
856 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
861 lfsck_linkea_del_buf(ldata, cname);
863 linkea_next_entry(ldata);
866 ldata->ld_lee = oldlee;
867 ldata->ld_reclen = oldlen;
873 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
875 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
876 * with the given type to generate the name, the detailed rules for name
877 * have been described as following.
879 * The function also generates the linkEA corresponding to the name entry
880 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
882 * \param[in] env pointer to the thread context
883 * \param[in] com pointer to the lfsck component
884 * \param[in] orphan pointer to the orphan MDT-object
885 * \param[in] infix additional information for the orphan name, such as
886 * the FID for original
887 * \param[in] type the type for describing why the orphan MDT-object is
888 * created. The rules are as following:
890 * type "D": The MDT-object is a directory, it may knows its parent
891 * but because there is no valid linkEA, the LFSCK cannot
892 * know where to put it back to the namespace.
893 * type "O": The MDT-object has no linkEA, and there is no name
894 * entry that references the MDT-object.
896 * type "S": The orphan MDT-object is a shard of a striped directory
898 * \see lfsck_layout_recreate_parent() for more types.
900 * The orphan name will be like:
901 * ${FID}-${infix}-${type}-${conflict_version}
903 * \param[out] count if some others inserted some linkEA entries by race,
904 * then return the linkEA entries count.
906 * \retval positive number for repaired cases
907 * \retval 0 if needs to repair nothing
908 * \retval negative error number on failure
910 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
911 struct lfsck_component *com,
912 struct dt_object *orphan,
913 const char *infix, const char *type,
916 struct lfsck_thread_info *info = lfsck_env_info(env);
917 struct lu_name *cname = &info->lti_name;
918 struct dt_insert_rec *rec = &info->lti_dt_rec;
919 struct lu_attr *la = &info->lti_la2;
920 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
921 const struct lu_fid *pfid;
923 struct lfsck_instance *lfsck = com->lc_lfsck;
924 struct dt_device *dev = lfsck_obj2dev(orphan);
925 struct dt_object *parent;
926 struct thandle *th = NULL;
927 struct lfsck_lock_handle *pllh = &info->lti_llh;
928 struct lustre_handle clh = { 0 };
929 struct linkea_data ldata2 = { NULL };
930 struct lu_buf linkea_buf;
937 cname->ln_name = NULL;
938 if (unlikely(lfsck->li_lpf_obj == NULL))
939 GOTO(log, rc = -ENXIO);
941 parent = lfsck->li_lpf_obj;
942 pfid = lfsck_dto2fid(parent);
946 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
947 PFID(cfid), infix, type, idx++);
948 rc = dt_lookup_dir(env, parent, info->lti_key, &tfid);
949 if (rc != 0 && rc != -ENOENT)
952 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
954 } while (rc == 0 && !exist);
956 rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
957 MDS_INODELOCK_UPDATE, LCK_PW);
961 /* Re-check whether the name conflict with othrs after taken
963 rc = dt_lookup_dir(env, parent, info->lti_key, &tfid);
965 if (!lu_fid_eq(cfid, &tfid)) {
972 } else if (rc != -ENOENT) {
978 cname->ln_name = info->lti_key;
979 cname->ln_namelen = namelen;
980 rc = linkea_links_new(&ldata2, &info->lti_linkea_buf2,
985 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
986 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
987 MDS_INODELOCK_XATTR, LCK_EX);
991 lfsck_buf_init(&linkea_buf, ldata2.ld_buf->lb_buf,
992 ldata2.ld_leh->leh_len);
993 th = dt_trans_create(env, dev);
995 GOTO(log, rc = PTR_ERR(th));
997 if (S_ISDIR(lfsck_object_type(orphan))) {
998 rc = dt_declare_delete(env, orphan,
999 (const struct dt_key *)dotdot, th);
1003 rec->rec_type = S_IFDIR;
1004 rec->rec_fid = pfid;
1005 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1006 (const struct dt_key *)dotdot, th);
1011 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1012 XATTR_NAME_LINK, 0, th);
1017 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1018 rec->rec_fid = cfid;
1019 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1020 (const struct dt_key *)cname->ln_name,
1025 if (S_ISDIR(rec->rec_type)) {
1026 rc = dt_declare_ref_add(env, parent, th);
1032 memset(la, 0, sizeof(*la));
1033 la->la_ctime = ktime_get_real_seconds();
1034 la->la_valid = LA_CTIME;
1035 rc = dt_declare_attr_set(env, orphan, la, th);
1039 rc = dt_trans_start_local(env, dev, th);
1043 dt_write_lock(env, orphan, 0);
1044 rc = lfsck_links_read2_with_rec(env, orphan, &ldata2);
1045 if (likely(rc == -ENODATA || rc == -EINVAL)) {
1046 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1047 GOTO(unlock, rc = 1);
1049 if (S_ISDIR(lfsck_object_type(orphan))) {
1050 rc = dt_delete(env, orphan,
1051 (const struct dt_key *)dotdot, th);
1055 rec->rec_type = S_IFDIR;
1056 rec->rec_fid = pfid;
1057 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1058 (const struct dt_key *)dotdot, th);
1063 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1066 if (rc == 0 && count != NULL)
1067 *count = ldata2.ld_leh->leh_reccount;
1071 dt_write_unlock(env, orphan);
1073 if (rc == 0 && !exist) {
1074 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1075 rec->rec_fid = cfid;
1076 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1077 (const struct dt_key *)cname->ln_name, th);
1078 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1079 dt_write_lock(env, parent, 0);
1080 rc = dt_ref_add(env, parent, th);
1081 dt_write_unlock(env, parent);
1086 rc = dt_attr_set(env, orphan, la, th);
1088 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1091 dt_write_unlock(env, orphan);
1094 dt_trans_stop(env, dev, th);
1097 lfsck_ibits_unlock(&clh, LCK_EX);
1099 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1100 "object "DFID", name = %s: rc = %d\n",
1101 lfsck_lfsck2name(lfsck), PFID(cfid),
1102 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1105 struct lfsck_namespace *ns = com->lc_file_ram;
1107 ns->ln_flags |= LF_INCONSISTENT;
1113 static int lfsck_lmv_set(const struct lu_env *env,
1114 struct lfsck_instance *lfsck,
1115 struct dt_object *obj,
1116 struct lmv_mds_md_v1 *lmv)
1118 struct dt_device *dev = lfsck->li_next;
1119 struct thandle *th = NULL;
1120 struct lu_buf buf = { lmv, sizeof(*lmv) };
1125 th = dt_trans_create(env, dev);
1127 RETURN(PTR_ERR(th));
1129 rc = dt_declare_xattr_set(env, obj, &buf, XATTR_NAME_LMV, 0, th);
1133 rc = dt_trans_start_local(env, dev, th);
1137 rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LMV, 0, th);
1143 dt_trans_stop(env, dev, th);
1148 static int lfsck_lmv_delete(const struct lu_env *env,
1149 struct lfsck_instance *lfsck,
1150 struct dt_object *obj)
1152 struct dt_device *dev = lfsck->li_next;
1153 struct thandle *th = NULL;
1158 th = dt_trans_create(env, dev);
1160 RETURN(PTR_ERR(th));
1162 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LMV, th);
1166 rc = dt_trans_start_local(env, dev, th);
1170 rc = dt_xattr_del(env, obj, XATTR_NAME_LMV, th);
1176 dt_trans_stop(env, dev, th);
1181 static inline int lfsck_object_is_shard(const struct lu_env *env,
1182 struct lfsck_instance *lfsck,
1183 struct dt_object *obj,
1184 const struct lu_name *lname)
1186 struct lfsck_thread_info *info = lfsck_env_info(env);
1187 struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
1190 rc = lfsck_shard_name_to_index(env, lname->ln_name, lname->ln_namelen,
1191 lfsck_object_type(obj),
1192 lfsck_dto2fid(obj));
1196 rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv);
1200 if (!rc && lmv->lmv_magic == LMV_MAGIC_STRIPE)
1207 * Add the specified name entry back to namespace.
1209 * If there is a linkEA entry that back references a name entry under
1210 * some parent directory, but such parent directory does not have the
1211 * claimed name entry. On the other hand, the linkEA entries count is
1212 * not larger than the MDT-object's hard link count. Under such case,
1213 * it is quite possible that the name entry is lost. Then the LFSCK
1214 * should add the name entry back to the namespace.
1216 * If \a child is shard, which means \a parent is a striped directory,
1217 * if \a parent has LMV, we need to delete it before insertion because
1218 * now parent's striping is broken and can't be parsed correctly.
1220 * \param[in] env pointer to the thread context
1221 * \param[in] com pointer to the lfsck component
1222 * \param[in] parent pointer to the directory under which the name entry
1223 * will be inserted into
1224 * \param[in] child pointer to the object referenced by the name entry
1225 * that to be inserted into the parent
1226 * \param[in] lname the name for the child in the parent directory
1228 * \retval positive number for repaired cases
1229 * \retval 0 if nothing to be repaired
1230 * \retval negative error number on failure
1232 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1233 struct lfsck_component *com,
1234 struct dt_object *parent,
1235 struct dt_object *child,
1236 const struct lu_name *lname)
1238 struct lfsck_thread_info *info = lfsck_env_info(env);
1239 struct lu_attr *la = &info->lti_la;
1240 struct dt_insert_rec *rec = &info->lti_dt_rec;
1241 struct lfsck_instance *lfsck = com->lc_lfsck;
1242 /* The child and its name may be on different MDTs. */
1243 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1244 const struct lu_fid *cfid = lfsck_dto2fid(child);
1245 struct dt_device *dev = lfsck->li_next;
1246 struct thandle *th = NULL;
1247 struct lfsck_lock_handle *llh = &info->lti_llh;
1248 struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
1249 struct lu_buf buf = { lmv, sizeof(*lmv) };
1250 /* whether parent's LMV is deleted before insertion */
1251 bool parent_lmv_deleted = false;
1252 /* whether parent's LMV is missing */
1253 bool parent_lmv_lost = false;
1258 /* @parent/@child may be based on lfsck->li_bottom,
1259 * but here we need the object based on the lfsck->li_next. */
1261 parent = lfsck_object_locate(dev, parent);
1263 GOTO(log, rc = PTR_ERR(parent));
1265 child = lfsck_object_locate(dev, child);
1267 GOTO(log, rc = PTR_ERR(child));
1269 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1272 rc = lfsck_lock(env, lfsck, parent, lname->ln_name, llh,
1273 MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1274 MDS_INODELOCK_XATTR, LCK_EX);
1278 rc = lfsck_object_is_shard(env, lfsck, child, lname);
1283 rc = lfsck_read_stripe_lmv(env, lfsck, parent, lmv);
1286 * To add a shard, we need to convert parent to a
1287 * plain directory by deleting its LMV, and after
1288 * insertion set it back.
1290 rc = lfsck_lmv_delete(env, lfsck, parent);
1293 parent_lmv_deleted = true;
1294 lmv->lmv_layout_version++;
1295 lfsck_lmv_header_cpu_to_le(lmv, lmv);
1296 } else if (rc == -ENODATA) {
1297 struct lu_seq_range *range = &info->lti_range;
1298 struct seq_server_site *ss = lfsck_dev_site(lfsck);
1300 rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
1304 fld_range_set_mdt(range);
1305 rc = fld_server_lookup(env, ss->ss_server_fld,
1306 fid_seq(lfsck_dto2fid(parent)), range);
1310 parent_lmv_lost = true;
1311 lmv->lmv_magic = LMV_MAGIC;
1312 lmv->lmv_master_mdt_index = range->lsr_index;
1313 lmv->lmv_layout_version++;
1314 lfsck_lmv_header_cpu_to_le(lmv, lmv);
1320 if (unlikely(!dt_try_as_dir(env, parent)))
1321 GOTO(unlock, rc = -ENOTDIR);
1323 th = dt_trans_create(env, dev);
1325 GOTO(unlock, rc = PTR_ERR(th));
1327 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1328 rec->rec_fid = cfid;
1329 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1330 (const struct dt_key *)lname->ln_name, th);
1334 if (S_ISDIR(rec->rec_type)) {
1335 rc = dt_declare_ref_add(env, parent, th);
1340 if (parent_lmv_lost) {
1341 rc = dt_declare_xattr_set(env, parent, &buf, XATTR_NAME_LMV,
1347 la->la_ctime = ktime_get_real_seconds();
1348 la->la_valid = LA_CTIME;
1349 rc = dt_declare_attr_set(env, parent, la, th);
1353 rc = dt_declare_attr_set(env, child, la, th);
1357 rc = dt_trans_start_local(env, dev, th);
1361 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1362 (const struct dt_key *)lname->ln_name, th);
1366 if (S_ISDIR(rec->rec_type)) {
1367 dt_write_lock(env, parent, 0);
1368 rc = dt_ref_add(env, parent, th);
1369 dt_write_unlock(env, parent);
1374 if (parent_lmv_lost) {
1375 rc = dt_xattr_set(env, parent, &buf, XATTR_NAME_LMV, 0, th);
1380 rc = dt_attr_set(env, parent, la, th);
1384 rc = dt_attr_set(env, child, la, th);
1386 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1389 dt_trans_stop(env, dev, th);
1392 if (parent_lmv_deleted)
1393 lfsck_lmv_set(env, lfsck, parent, lmv);
1398 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1399 "the name %s and type %o to the parent "DFID": rc = %d\n",
1400 lfsck_lfsck2name(lfsck), PFID(cfid), lname->ln_name,
1401 lfsck_object_type(child) & S_IFMT, PFID(pfid), rc);
1404 struct lfsck_namespace *ns = com->lc_file_ram;
1406 ns->ln_flags |= LF_INCONSISTENT;
1408 ns->ln_lost_dirent_repaired++;
1415 * Create the specified orphan directory.
1417 * For the case that the parent MDT-object stored in some MDT-object's
1418 * linkEA entry is lost, the LFSCK will re-create the parent object as
1419 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1420 * with the name ${FID}-P-${conflict_version}.
1422 * \param[in] env pointer to the thread context
1423 * \param[in] com pointer to the lfsck component
1424 * \param[in] orphan pointer to the orphan MDT-object to be created
1425 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1427 * \retval positive number for repaired cases
1428 * \retval negative error number on failure
1430 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1431 struct lfsck_component *com,
1432 struct dt_object *orphan,
1433 struct lmv_mds_md_v1 *lmv)
1435 struct lfsck_thread_info *info = lfsck_env_info(env);
1436 struct lu_attr *la = &info->lti_la;
1437 struct dt_allocation_hint *hint = &info->lti_hint;
1438 struct dt_object_format *dof = &info->lti_dof;
1439 struct lu_name *cname = &info->lti_name2;
1440 struct dt_insert_rec *rec = &info->lti_dt_rec;
1441 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1442 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1444 struct lfsck_instance *lfsck = com->lc_lfsck;
1445 struct lfsck_namespace *ns = com->lc_file_ram;
1446 struct dt_device *dev = lfsck_obj2dev(orphan);
1447 struct dt_object *parent = NULL;
1448 struct thandle *th = NULL;
1449 struct lfsck_lock_handle *llh = &info->lti_llh;
1450 struct linkea_data ldata = { NULL };
1451 struct lu_buf linkea_buf;
1452 struct lu_buf lmv_buf;
1460 LASSERT(!dt_object_exists(orphan));
1462 cname->ln_name = NULL;
1463 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1466 if (dt_object_remote(orphan)) {
1467 LASSERT(lfsck->li_lpf_root_obj != NULL);
1469 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1471 GOTO(log, rc = idx);
1473 snprintf(name, 8, "MDT%04x", idx);
1474 rc = dt_lookup_dir(env, lfsck->li_lpf_root_obj, name, &tfid);
1476 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1478 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1480 GOTO(log, rc = PTR_ERR(parent));
1482 if (unlikely(!dt_try_as_dir(env, parent)))
1483 GOTO(log, rc = -ENOTDIR);
1485 if (unlikely(lfsck->li_lpf_obj == NULL))
1486 GOTO(log, rc = -ENXIO);
1488 parent = lfsck->li_lpf_obj;
1491 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1493 GOTO(log, rc = PTR_ERR(dev));
1499 namelen = snprintf(name, 31, DFID"-P-%d",
1501 rc = dt_lookup_dir(env, parent, name, &tfid);
1502 if (rc != 0 && rc != -ENOENT)
1506 rc = lfsck_lock(env, lfsck, parent, name, llh,
1507 MDS_INODELOCK_UPDATE, LCK_PW);
1511 /* Re-check whether the name conflict with othrs after taken
1513 rc = dt_lookup_dir(env, parent, name, &tfid);
1514 if (unlikely(rc == 0)) {
1522 cname->ln_name = name;
1523 cname->ln_namelen = namelen;
1525 memset(la, 0, sizeof(*la));
1526 la->la_mode = S_IFDIR | 0700;
1527 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1528 LA_ATIME | LA_MTIME | LA_CTIME;
1530 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1531 la->la_mode & S_IFMT);
1533 memset(dof, 0, sizeof(*dof));
1534 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1536 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
1537 cname, lfsck_dto2fid(parent));
1541 th = dt_trans_create(env, dev);
1543 GOTO(unlock1, rc = PTR_ERR(th));
1545 /* Sync the remote transaction to guarantee that the subsequent
1546 * lock against the @orphan can find the @orphan in time. */
1547 if (dt_object_remote(orphan))
1550 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1554 if (unlikely(!dt_try_as_dir(env, orphan)))
1555 GOTO(stop, rc = -ENOTDIR);
1557 rc = dt_declare_ref_add(env, orphan, th);
1561 rec->rec_type = S_IFDIR;
1562 rec->rec_fid = cfid;
1563 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1564 (const struct dt_key *)dot, th);
1568 rec->rec_fid = lfsck_dto2fid(parent);
1569 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1570 (const struct dt_key *)dotdot, th);
1575 lmv->lmv_magic = LMV_MAGIC;
1576 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1577 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1578 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1579 rc = dt_declare_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV,
1585 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1586 ldata.ld_leh->leh_len);
1587 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1588 XATTR_NAME_LINK, 0, th);
1592 rec->rec_fid = cfid;
1593 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1594 (const struct dt_key *)name, th);
1596 rc = dt_declare_ref_add(env, parent, th);
1601 rc = dt_trans_start_local(env, dev, th);
1605 dt_write_lock(env, orphan, 0);
1606 rc = dt_create(env, orphan, la, hint, dof, th);
1610 rc = dt_ref_add(env, orphan, th);
1614 rec->rec_fid = cfid;
1615 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1616 (const struct dt_key *)dot, th);
1620 rec->rec_fid = lfsck_dto2fid(parent);
1621 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1622 (const struct dt_key *)dotdot, th);
1627 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1632 rc = dt_xattr_set(env, orphan, &linkea_buf,
1633 XATTR_NAME_LINK, 0, th);
1634 dt_write_unlock(env, orphan);
1638 rec->rec_fid = cfid;
1639 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1640 (const struct dt_key *)name, th);
1642 dt_write_lock(env, parent, 0);
1643 rc = dt_ref_add(env, parent, th);
1644 dt_write_unlock(env, parent);
1647 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1650 dt_write_unlock(env, orphan);
1653 rc1 = dt_trans_stop(env, dev, th);
1654 if (rc1 != 0 && rc > 0)
1661 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1662 "the object "DFID", name = %s: rc = %d\n",
1663 lfsck_lfsck2name(lfsck), PFID(cfid),
1664 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1666 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1667 lfsck_object_put(env, parent);
1670 ns->ln_flags |= LF_INCONSISTENT;
1676 * Remove the specified entry from the linkEA.
1678 * Locate the linkEA entry with the given @cname and @pfid, then
1679 * remove this entry or the other entries those are repeated with
1682 * \param[in] env pointer to the thread context
1683 * \param[in] com pointer to the lfsck component
1684 * \param[in] obj pointer to the dt_object to be handled
1685 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1686 * \param[in] cname the name for the child in the parent directory
1687 * \param[in] pfid the parent directory's FID for the linkEA
1688 * \param[in] next if true, then remove the first found linkEA
1689 * entry, and move the ldata->ld_lee to next entry
1691 * \retval positive number for repaired cases
1692 * \retval 0 if nothing to be repaired
1693 * \retval negative error number on failure
1695 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1696 struct lfsck_component *com,
1697 struct dt_object *obj,
1698 struct linkea_data *ldata,
1699 struct lu_name *cname,
1700 struct lu_fid *pfid,
1703 struct lfsck_instance *lfsck = com->lc_lfsck;
1704 struct dt_device *dev = lfsck_obj2dev(obj);
1705 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1706 struct thandle *th = NULL;
1707 struct lustre_handle lh = { 0 };
1708 struct linkea_data ldata_new = { NULL };
1709 struct lu_buf linkea_buf;
1714 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1715 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1721 lfsck_linkea_del_buf(ldata, cname);
1723 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1725 if (ldata->ld_leh->leh_reccount > 0 ||
1726 unlikely(ldata->ld_leh->leh_overflow_time)) {
1727 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1728 ldata->ld_leh->leh_len);
1729 buflen = linkea_buf.lb_len;
1733 th = dt_trans_create(env, dev);
1735 GOTO(unlock1, rc = PTR_ERR(th));
1738 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1739 XATTR_NAME_LINK, 0, th);
1741 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
1745 rc = dt_trans_start_local(env, dev, th);
1749 dt_write_lock(env, obj, 0);
1750 if (unlikely(lfsck_is_dead_obj(obj)))
1751 GOTO(unlock2, rc = -ENOENT);
1753 rc = lfsck_links_read2_with_rec(env, obj, &ldata_new);
1755 GOTO(unlock2, rc = (rc == -ENODATA ? 0 : rc));
1757 /* The specified linkEA entry has been removed by race. */
1758 rc = linkea_links_find(&ldata_new, cname, pfid);
1760 GOTO(unlock2, rc = 0);
1762 if (bk->lb_param & LPF_DRYRUN)
1763 GOTO(unlock2, rc = 1);
1766 lfsck_linkea_del_buf(&ldata_new, cname);
1768 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1772 * linkea may change because it doesn't take lock in the first read, if
1773 * it becomes larger, restart from beginning.
1775 if ((ldata_new.ld_leh->leh_reccount > 0 ||
1776 unlikely(ldata_new.ld_leh->leh_overflow_time)) &&
1777 buflen < ldata_new.ld_leh->leh_len) {
1778 dt_write_unlock(env, obj);
1779 dt_trans_stop(env, dev, th);
1780 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1781 ldata_new.ld_leh->leh_len);
1782 buflen = linkea_buf.lb_len;
1787 rc = lfsck_links_write(env, obj, &ldata_new, th);
1789 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
1791 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1794 dt_write_unlock(env, obj);
1797 dt_trans_stop(env, dev, th);
1800 lfsck_ibits_unlock(&lh, LCK_EX);
1803 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1804 "for the object: "DFID", parent "DFID", name %.*s\n",
1805 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1806 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1810 struct lfsck_namespace *ns = com->lc_file_ram;
1812 ns->ln_flags |= LF_INCONSISTENT;
1819 * Conditionally remove the specified entry from the linkEA.
1821 * Take the parent lock firstly, then check whether the specified
1822 * name entry exists or not: if yes, do nothing; otherwise, call
1823 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1825 * \param[in] env pointer to the thread context
1826 * \param[in] com pointer to the lfsck component
1827 * \param[in] parent pointer to the parent directory
1828 * \param[in] child pointer to the child object that holds the linkEA
1829 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1830 * \param[in] cname the name for the child in the parent directory
1831 * \param[in] pfid the parent directory's FID for the linkEA
1833 * \retval positive number for repaired cases
1834 * \retval 0 if nothing to be repaired
1835 * \retval negative error number on failure
1837 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1838 struct lfsck_component *com,
1839 struct dt_object *parent,
1840 struct dt_object *child,
1841 struct linkea_data *ldata,
1842 struct lu_name *cname,
1843 struct lu_fid *pfid)
1845 struct lfsck_thread_info *info = lfsck_env_info(env);
1846 struct lu_fid *cfid = &info->lti_fid3;
1847 struct lfsck_lock_handle *llh = &info->lti_llh;
1851 rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1852 MDS_INODELOCK_UPDATE, LCK_PR);
1856 dt_read_lock(env, parent, 0);
1857 if (unlikely(lfsck_is_dead_obj(parent))) {
1858 dt_read_unlock(env, parent);
1860 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1866 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1867 (const struct dt_key *)cname->ln_name);
1868 dt_read_unlock(env, parent);
1870 /* It is safe to release the ldlm lock, because when the logic come
1871 * here, we have got all the needed information above whether the
1872 * linkEA entry is valid or not. It is not important that others
1873 * may add new linkEA entry after the ldlm lock released. If other
1874 * has removed the specified linkEA entry by race, then it is OK,
1875 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1878 if (rc == -ENOENT) {
1879 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1888 /* The LFSCK just found some internal status of cross-MDTs
1889 * create operation. That is normal. */
1890 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1891 linkea_next_entry(ldata);
1896 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1903 * Conditionally replace name entry in the parent.
1905 * As required, the LFSCK may re-create the lost MDT-object for dangling
1906 * name entry, but such repairing may be wrong because of bad FID in the
1907 * name entry. As the LFSCK processing, the real MDT-object may be found,
1908 * then the LFSCK should check whether the former re-created MDT-object
1909 * has been modified or not, if not, then destroy it and update the name
1910 * entry in the parent to reference the real MDT-object.
1912 * \param[in] env pointer to the thread context
1913 * \param[in] com pointer to the lfsck component
1914 * \param[in] parent pointer to the parent directory
1915 * \param[in] child pointer to the MDT-object that may be the real
1916 * MDT-object corresponding to the name entry in parent
1917 * \param[in] cfid the current FID in the name entry
1918 * \param[in] cname contains the name of the child in the parent directory
1920 * \retval positive number for repaired cases
1921 * \retval 0 if nothing to be repaired
1922 * \retval negative error number on failure
1924 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1925 struct lfsck_component *com,
1926 struct dt_object *parent,
1927 struct dt_object *child,
1928 const struct lu_fid *cfid,
1929 const struct lu_name *cname)
1931 struct lfsck_thread_info *info = lfsck_env_info(env);
1932 struct lu_attr *la = &info->lti_la;
1933 struct dt_insert_rec *rec = &info->lti_dt_rec;
1935 struct lfsck_instance *lfsck = com->lc_lfsck;
1936 /* The child and its name may be on different MDTs. */
1937 struct dt_device *dev = lfsck->li_next;
1938 const char *name = cname->ln_name;
1939 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1940 struct dt_object *cobj = NULL;
1941 struct lfsck_lock_handle *pllh = &info->lti_llh;
1942 struct lustre_handle clh = { 0 };
1943 struct linkea_data ldata = { NULL };
1944 struct thandle *th = NULL;
1949 /* @parent/@child may be based on lfsck->li_bottom,
1950 * but here we need the object based on the lfsck->li_next. */
1952 parent = lfsck_object_locate(dev, parent);
1954 GOTO(log, rc = PTR_ERR(parent));
1956 if (unlikely(!dt_try_as_dir(env, parent)))
1957 GOTO(log, rc = -ENOTDIR);
1959 rc = lfsck_lock(env, lfsck, parent, name, pllh,
1960 MDS_INODELOCK_UPDATE, LCK_PW);
1964 if (!fid_is_sane(cfid)) {
1969 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1972 if (rc == -ENOENT) {
1980 if (!dt_object_exists(cobj)) {
1985 rc = dt_lookup_dir(env, parent, name, &tfid);
1986 if (rc == -ENOENT) {
1994 /* Someone changed the name entry, cannot replace it. */
1995 if (!lu_fid_eq(cfid, &tfid))
1998 /* lock the object to be destroyed. */
1999 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
2000 MDS_INODELOCK_UPDATE |
2001 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
2006 if (unlikely(lfsck_is_dead_obj(cobj))) {
2011 rc = dt_attr_get(env, cobj, la);
2015 /* The object has been modified by other(s), or it is not created by
2016 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
2017 if (la->la_ctime != 0)
2020 if (S_ISREG(la->la_mode)) {
2021 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
2022 /* If someone has created related OST-object(s),
2024 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
2025 GOTO(log, rc = (rc > 0 ? 0 : rc));
2029 dt_read_lock(env, child, 0);
2030 rc = lfsck_links_read2_with_rec(env, child, &ldata);
2031 dt_read_unlock(env, child);
2033 /* Someone changed the child, no need to replace. */
2040 rc = linkea_links_find(&ldata, cname, pfid);
2041 /* Someone moved the child, no need to replace. */
2045 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2048 th = dt_trans_create(env, dev);
2050 GOTO(log, rc = PTR_ERR(th));
2053 rc = dt_declare_destroy(env, cobj, th);
2058 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2062 rec->rec_type = S_IFDIR;
2063 rec->rec_fid = lfsck_dto2fid(child);
2064 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
2065 (const struct dt_key *)name, th);
2069 rc = dt_trans_start_local(env, dev, th);
2074 rc = dt_destroy(env, cobj, th);
2079 /* The old name entry maybe not exist. */
2080 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2081 if (rc != 0 && rc != -ENOENT)
2084 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
2085 (const struct dt_key *)name, th);
2087 GOTO(stop, rc = (rc == 0 ? 1 : rc));
2090 dt_trans_stop(env, dev, th);
2093 lfsck_ibits_unlock(&clh, LCK_EX);
2096 if (cobj != NULL && !IS_ERR(cobj))
2097 lfsck_object_put(env, cobj);
2099 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
2100 "object "DFID" because of conflict with the object "DFID
2101 " under the parent "DFID" with name %s: rc = %d\n",
2102 lfsck_lfsck2name(lfsck), PFID(cfid),
2103 PFID(lfsck_dto2fid(child)), PFID(pfid), name, rc);
2109 * Overwrite the linkEA for the object with the given ldata.
2111 * The caller should take the ldlm lock before the calling.
2113 * \param[in] env pointer to the thread context
2114 * \param[in] com pointer to the lfsck component
2115 * \param[in] obj pointer to the dt_object to be handled
2116 * \param[in] ldata pointer to the new linkEA data
2118 * \retval positive number for repaired cases
2119 * \retval 0 if nothing to be repaired
2120 * \retval negative error number on failure
2122 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
2123 struct lfsck_component *com,
2124 struct dt_object *obj,
2125 struct linkea_data *ldata)
2127 struct lfsck_instance *lfsck = com->lc_lfsck;
2128 struct dt_device *dev = lfsck_obj2dev(obj);
2129 struct thandle *th = NULL;
2130 struct lu_buf linkea_buf;
2134 th = dt_trans_create(env, dev);
2136 GOTO(log, rc = PTR_ERR(th));
2138 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
2139 ldata->ld_leh->leh_len);
2140 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2141 XATTR_NAME_LINK, 0, th);
2145 rc = dt_trans_start_local(env, dev, th);
2149 dt_write_lock(env, obj, 0);
2150 if (unlikely(lfsck_is_dead_obj(obj)))
2151 GOTO(unlock, rc = 0);
2153 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2154 GOTO(unlock, rc = 1);
2156 rc = dt_xattr_set(env, obj, &linkea_buf,
2157 XATTR_NAME_LINK, 0, th);
2159 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2162 dt_write_unlock(env, obj);
2165 dt_trans_stop(env, dev, th);
2168 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
2169 "object "DFID": rc = %d\n",
2170 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
2173 struct lfsck_namespace *ns = com->lc_file_ram;
2175 ns->ln_flags |= LF_INCONSISTENT;
2182 * Repair invalid name entry.
2184 * If the name entry contains invalid information, such as bad file type
2185 * or (and) corrupted object FID, then either remove the name entry or
2186 * udpate the name entry with the given (right) information.
2188 * \param[in] env pointer to the thread context
2189 * \param[in] com pointer to the lfsck component
2190 * \param[in] parent pointer to the parent directory
2191 * \param[in] child pointer to the object referenced by the name entry
2192 * \param[in] name the old name of the child under the parent directory
2193 * \param[in] name2 the new name of the child under the parent directory
2194 * \param[in] type the type claimed by the name entry
2195 * \param[in] update update the name entry if true; otherwise, remove it
2196 * \param[in] dec decrease the parent nlink count if true
2198 * \retval positive number for repaired successfully
2199 * \retval 0 if nothing to be repaired
2200 * \retval negative error number on failure
2202 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2203 struct lfsck_component *com,
2204 struct dt_object *parent,
2205 struct dt_object *child,
2206 const char *name, const char *name2,
2207 __u16 type, bool update, bool dec)
2209 struct lfsck_thread_info *info = lfsck_env_info(env);
2210 struct dt_insert_rec *rec = &info->lti_dt_rec;
2211 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2212 struct lu_fid cfid = {0};
2214 struct lfsck_instance *lfsck = com->lc_lfsck;
2215 struct dt_device *dev = lfsck->li_next;
2216 struct thandle *th = NULL;
2217 struct lfsck_lock_handle *llh = &info->lti_llh;
2218 struct lustre_handle lh = { 0 };
2223 cfid = *lfsck_dto2fid(child);
2224 parent = lfsck_object_locate(dev, parent);
2226 GOTO(log, rc = PTR_ERR(parent));
2228 if (unlikely(!dt_try_as_dir(env, parent)))
2229 GOTO(log, rc = -ENOTDIR);
2231 if (!update || strcmp(name, name2) == 0)
2232 rc = lfsck_lock(env, lfsck, parent, name, llh,
2233 MDS_INODELOCK_UPDATE, LCK_PW);
2235 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2236 MDS_INODELOCK_UPDATE, LCK_PW);
2240 th = dt_trans_create(env, dev);
2242 GOTO(unlock1, rc = PTR_ERR(th));
2244 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2249 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2250 LASSERT(!fid_is_zero(&cfid));
2251 rec->rec_fid = &cfid;
2252 rc = dt_declare_insert(env, parent,
2253 (const struct dt_rec *)rec,
2254 (const struct dt_key *)name2, th);
2259 if (dec && S_ISDIR(type)) {
2260 rc = dt_declare_ref_del(env, parent, th);
2265 rc = dt_trans_start_local(env, dev, th);
2270 dt_write_lock(env, parent, 0);
2271 rc = dt_lookup_dir(env, dt_object_child(parent), name, &tfid);
2272 /* Someone has removed the bad name entry by race. */
2274 GOTO(unlock2, rc = 0);
2279 /* Someone has removed the bad name entry and reused it for other
2280 * object by race. */
2281 if (!lu_fid_eq(&tfid, &cfid))
2282 GOTO(unlock2, rc = 0);
2284 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2285 GOTO(unlock2, rc = 1);
2287 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2292 rc = dt_insert(env, parent,
2293 (const struct dt_rec *)rec,
2294 (const struct dt_key *)name2, th);
2299 if (dec && S_ISDIR(type)) {
2300 rc = dt_ref_del(env, parent, th);
2305 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2308 dt_write_unlock(env, parent);
2311 dt_trans_stop(env, dev, th);
2313 /* We are not sure whether the child will become orphan or not.
2314 * Record it in the LFSCK trace file for further checking in
2315 * the second-stage scanning. */
2316 if (!update && !dec && child && rc == 0)
2317 lfsck_namespace_trace_update(env, com, &cfid,
2318 LNTF_CHECK_LINKEA, true);
2321 /* It is harmless even if unlock the unused lock_handle */
2322 lfsck_ibits_unlock(&lh, LCK_PW);
2326 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2327 "entry for: parent "DFID", child "DFID", name %s, type "
2328 "in name entry %o, type claimed by child %o. repair it "
2329 "by %s with new name2 %s: rc = %d\n",
2330 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(&cfid),
2331 name, type, update ? lfsck_object_type(child) : 0,
2332 update ? "updating" : "removing", name2, rc);
2335 struct lfsck_namespace *ns = com->lc_file_ram;
2337 ns->ln_flags |= LF_INCONSISTENT;
2344 * Update the ".." name entry for the given object.
2346 * The object's ".." is corrupted, this function will update the ".." name
2347 * entry with the given pfid, and the linkEA with the given ldata.
2349 * The caller should take the ldlm lock before the calling.
2351 * \param[in] env pointer to the thread context
2352 * \param[in] com pointer to the lfsck component
2353 * \param[in] obj pointer to the dt_object to be handled
2354 * \param[in] pfid the new fid for the object's ".." name entry
2355 * \param[in] cname the name for the @obj in the parent directory
2357 * \retval positive number for repaired cases
2358 * \retval 0 if nothing to be repaired
2359 * \retval negative error number on failure
2361 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2362 struct lfsck_component *com,
2363 struct dt_object *obj,
2364 const struct lu_fid *pfid,
2365 struct lu_name *cname)
2367 struct lfsck_thread_info *info = lfsck_env_info(env);
2368 struct dt_insert_rec *rec = &info->lti_dt_rec;
2369 struct lfsck_instance *lfsck = com->lc_lfsck;
2370 struct dt_device *dev = lfsck_obj2dev(obj);
2371 struct thandle *th = NULL;
2372 struct linkea_data ldata = { NULL };
2373 struct lu_buf linkea_buf;
2377 LASSERT(!dt_object_remote(obj));
2378 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2380 rc = linkea_links_new(&ldata, &info->lti_big_buf, cname, pfid);
2384 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2385 ldata.ld_leh->leh_len);
2387 th = dt_trans_create(env, dev);
2389 GOTO(log, rc = PTR_ERR(th));
2391 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2395 rec->rec_type = S_IFDIR;
2396 rec->rec_fid = pfid;
2397 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2398 (const struct dt_key *)dotdot, th);
2402 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2403 XATTR_NAME_LINK, 0, th);
2407 rc = dt_trans_start_local(env, dev, th);
2411 dt_write_lock(env, obj, 0);
2412 if (unlikely(lfsck_is_dead_obj(obj)))
2413 GOTO(unlock, rc = 0);
2415 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2416 GOTO(unlock, rc = 1);
2418 /* The old ".." name entry maybe not exist. */
2419 dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2421 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2422 (const struct dt_key *)dotdot, th);
2426 rc = lfsck_links_write(env, obj, &ldata, th);
2428 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2431 dt_write_unlock(env, obj);
2434 dt_trans_stop(env, dev, th);
2437 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2438 "the object "DFID", new parent "DFID": rc = %d\n",
2439 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2443 struct lfsck_namespace *ns = com->lc_file_ram;
2445 ns->ln_flags |= LF_INCONSISTENT;
2452 * Handle orphan @obj during Double Scan Directory.
2454 * Remove the @obj's current (invalid) linkEA entries, and insert
2455 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2456 * ${FID}-${PFID}-D-${conflict_version}
2458 * The caller should take the ldlm lock before the calling.
2460 * \param[in] env pointer to the thread context
2461 * \param[in] com pointer to the lfsck component
2462 * \param[in] obj pointer to the orphan object to be handled
2463 * \param[in] pfid the new fid for the object's ".." name entry
2464 * \param[in,out] lh ldlm lock handler for the given @obj
2465 * \param[out] type to tell the caller what the inconsistency is
2467 * \retval positive number for repaired cases
2468 * \retval 0 if nothing to be repaired
2469 * \retval negative error number on failure
2472 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2473 struct lfsck_component *com,
2474 struct dt_object *obj,
2475 const struct lu_fid *pfid,
2476 struct lustre_handle *lh,
2477 enum lfsck_namespace_inconsistency_type *type)
2479 struct lfsck_thread_info *info = lfsck_env_info(env);
2480 struct lfsck_namespace *ns = com->lc_file_ram;
2484 /* Remove the unrecognized linkEA. */
2485 rc = lfsck_namespace_links_remove(env, com, obj);
2486 lfsck_ibits_unlock(lh, LCK_EX);
2487 if (rc < 0 && rc != -ENODATA)
2490 *type = LNIT_MUL_REF;
2492 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2493 * ever tried to verify some remote MDT-object that resides on this
2494 * MDT, but this MDT failed to respond such request. So means there
2495 * may be some remote name entry on other MDT that references this
2496 * object with another name, so we cannot know whether this linkEA
2497 * is valid or not. So keep it there and maybe resolved when next
2499 if (ns->ln_flags & LF_INCOMPLETE)
2502 /* The unique linkEA is invalid, even if the ".." name entry may be
2503 * valid, we still cannot know via which name entry this directory
2504 * will be referenced. Then handle it as pure orphan. */
2505 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2506 "-"DFID, PFID(pfid));
2507 rc = lfsck_namespace_insert_orphan(env, com, obj,
2508 info->lti_tmpbuf, "D", NULL);
2514 * Double Scan Directory object for single linkEA entry case.
2516 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2517 * then check whether the name is in the namespace or not, if not, add the
2518 * missing name entry back to namespace. If the linkEA entry is invalid,
2519 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2522 * \param[in] env pointer to the thread context
2523 * \param[in] com pointer to the lfsck component
2524 * \param[in] child pointer to the directory to be double scanned
2525 * \param[in] pfid the FID corresponding to the ".." entry
2526 * \param[in] ldata pointer to the linkEA data for the given @child
2527 * \param[in,out] lh ldlm lock handler for the given @child
2528 * \param[out] type to tell the caller what the inconsistency is
2529 * \param[in] retry if found inconsistency, but the caller does not hold
2530 * ldlm lock on the @child, then set @retry as true
2531 * \param[in] unknown set if does not know how to repair the inconsistency
2533 * \retval positive number for repaired cases
2534 * \retval 0 if nothing to be repaired
2535 * \retval negative error number on failure
2538 lfsck_namespace_dsd_single(const struct lu_env *env,
2539 struct lfsck_component *com,
2540 struct dt_object *child,
2541 const struct lu_fid *pfid,
2542 struct linkea_data *ldata,
2543 struct lustre_handle *lh,
2544 enum lfsck_namespace_inconsistency_type *type,
2545 bool *retry, bool *unknown)
2547 struct lfsck_thread_info *info = lfsck_env_info(env);
2548 struct lu_name *cname = &info->lti_name;
2549 const struct lu_fid *cfid = lfsck_dto2fid(child);
2551 struct lfsck_namespace *ns = com->lc_file_ram;
2552 struct lfsck_instance *lfsck = com->lc_lfsck;
2553 struct dt_object *parent = NULL;
2554 struct lmv_mds_md_v1 *lmv;
2558 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2560 sizeof(info->lti_key));
2561 /* The unique linkEA entry with bad parent will be handled as orphan. */
2563 if (!lustre_handle_is_used(lh) && retry != NULL)
2566 rc = lfsck_namespace_dsd_orphan(env, com, child,
2572 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2574 GOTO(out, rc = PTR_ERR(parent));
2576 /* We trust the unique linkEA entry in spite of whether it matches the
2577 * ".." name entry or not. Because even if the linkEA entry is wrong
2578 * and the ".." name entry is right, we still cannot know via which
2579 * name entry the child will be referenced, since all known entries
2580 * have been verified during the first-stage scanning. */
2581 if (!dt_object_exists(parent)) {
2582 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2583 * has ever tried to verify some remote MDT-object that resides
2584 * on this MDT, but this MDT failed to respond such request. So
2585 * means there may be some remote name entry on other MDT that
2586 * references this object with another name, so we cannot know
2587 * whether this linkEA is valid or not. So keep it there and
2588 * maybe resolved when next LFSCK run. */
2589 if (ns->ln_flags & LF_INCOMPLETE)
2592 if (!lustre_handle_is_used(lh) && retry != NULL) {
2598 lfsck_ibits_unlock(lh, LCK_EX);
2601 lmv = &info->lti_lmv;
2602 rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
2603 if (rc != 0 && rc != -ENODATA)
2606 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2608 } else if (lfsck_shard_name_to_index(env,
2609 cname->ln_name, cname->ln_namelen,
2610 S_IFDIR, cfid) < 0) {
2611 /* It is an invalid name entry, we
2612 * cannot trust the parent also. */
2613 rc = lfsck_namespace_shrink_linkea(env, com, child,
2614 ldata, cname, &tfid, true);
2618 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2619 "-"DFID, PFID(pfid));
2620 rc = lfsck_namespace_insert_orphan(env, com, child,
2621 info->lti_tmpbuf, "S", NULL);
2626 /* Create the lost parent as an orphan. */
2627 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2629 /* Add the missing name entry to the parent. */
2630 rc = lfsck_namespace_insert_normal(env, com, parent,
2632 if (unlikely(rc == -EEXIST)) {
2633 /* Unfortunately, someone reused the name
2634 * under the parent by race. So we have
2635 * to remove the linkEA entry from
2636 * current child object. It means that the
2637 * LFSCK cannot recover the system
2638 * totally back to its original status,
2639 * but it is necessary to make the
2640 * current system to be consistent. */
2641 rc = lfsck_namespace_shrink_linkea(env,
2643 cname, &tfid, true);
2645 snprintf(info->lti_tmpbuf,
2646 sizeof(info->lti_tmpbuf),
2647 "-"DFID, PFID(pfid));
2648 rc = lfsck_namespace_insert_orphan(env,
2649 com, child, info->lti_tmpbuf,
2656 } /* !dt_object_exists(parent) */
2658 /* The unique linkEA entry with bad parent will be handled as orphan. */
2659 if (unlikely(!dt_try_as_dir(env, parent))) {
2660 if (!lustre_handle_is_used(lh) && retry != NULL)
2663 rc = lfsck_namespace_dsd_orphan(env, com, child,
2669 rc = dt_lookup_dir(env, parent, cname->ln_name, &tfid);
2670 if (rc == -ENOENT) {
2671 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2672 * has ever tried to verify some remote MDT-object that resides
2673 * on this MDT, but this MDT failed to respond such request. So
2674 * means there may be some remote name entry on other MDT that
2675 * references this object with another name, so we cannot know
2676 * whether this linkEA is valid or not. So keep it there and
2677 * maybe resolved when next LFSCK run. */
2678 if (ns->ln_flags & LF_INCOMPLETE)
2681 if (!lustre_handle_is_used(lh) && retry != NULL) {
2687 lfsck_ibits_unlock(lh, LCK_EX);
2688 rc = lfsck_namespace_check_name(env, lfsck, parent, child,
2696 /* It is an invalid name entry, drop it. */
2697 if (unlikely(rc > 0)) {
2698 rc = lfsck_namespace_shrink_linkea(env, com, child,
2699 ldata, cname, &tfid, true);
2701 snprintf(info->lti_tmpbuf,
2702 sizeof(info->lti_tmpbuf),
2703 "-"DFID, PFID(pfid));
2704 rc = lfsck_namespace_insert_orphan(env, com,
2705 child, info->lti_tmpbuf, "D", NULL);
2711 /* Add the missing name entry back to the namespace. */
2712 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2714 if (unlikely(rc == -ESTALE))
2715 /* It may happen when the remote object has been
2716 * removed, but the local MDT is not aware of that. */
2719 if (unlikely(rc == -EEXIST)) {
2720 /* Unfortunately, someone reused the name under the
2721 * parent by race. So we have to remove the linkEA
2722 * entry from current child object. It means that the
2723 * LFSCK cannot recover the system totally back to
2724 * its original status, but it is necessary to make
2725 * the current system to be consistent.
2727 * It also may be because of the LFSCK found some
2728 * internal status of create operation. Under such
2729 * case, nothing to be done. */
2730 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2731 parent, child, ldata, cname, &tfid);
2733 snprintf(info->lti_tmpbuf,
2734 sizeof(info->lti_tmpbuf),
2735 "-"DFID, PFID(pfid));
2736 rc = lfsck_namespace_insert_orphan(env, com,
2737 child, info->lti_tmpbuf, "D", NULL);
2742 } /* rc == -ENOENT */
2747 if (!lu_fid_eq(&tfid, cfid)) {
2748 if (!lustre_handle_is_used(lh) && retry != NULL) {
2754 lfsck_ibits_unlock(lh, LCK_EX);
2755 /* The name entry references another MDT-object that
2756 * may be created by the LFSCK for repairing dangling
2757 * name entry. Try to replace it. */
2758 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2761 rc = lfsck_namespace_dsd_orphan(env, com, child,
2767 /* Zero FID may because the remote directroy object has invalid linkEA,
2768 * or lost linkEA. Under such case, the LFSCK on this MDT does not know
2769 * how to repair the inconsistency, but the namespace LFSCK on the MDT
2770 * where its name entry resides may has more information (name, FID) to
2771 * repair such inconsistency. So here, keep the inconsistency to avoid
2772 * some imporper repairing. */
2773 if (fid_is_zero(pfid)) {
2780 /* The ".." name entry is wrong, update it. */
2781 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2782 if (!lustre_handle_is_used(lh) && retry != NULL) {
2788 *type = LNIT_UNMATCHED_PAIRS;
2789 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2790 lfsck_dto2fid(parent), cname);
2796 if (parent != NULL && !IS_ERR(parent))
2797 lfsck_object_put(env, parent);
2803 * Double Scan Directory object for multiple linkEA entries case.
2805 * The given @child has multiple linkEA entries. There is at most one linkEA
2806 * entry will be valid, all the others will be removed. Firstly, the function
2807 * will try to find out the linkEA entry for which the name entry exists under
2808 * the given parent (@pfid). If there is no linkEA entry that matches the given
2809 * ".." name entry, then tries to find out the first linkEA entry that both the
2810 * parent and the name entry exist to rebuild a new ".." name entry.
2812 * \param[in] env pointer to the thread context
2813 * \param[in] com pointer to the lfsck component
2814 * \param[in] child pointer to the directory to be double scanned
2815 * \param[in] pfid the FID corresponding to the ".." entry
2816 * \param[in] ldata pointer to the linkEA data for the given @child
2817 * \param[in,out] lh ldlm lock handler for the given @child
2818 * \param[out] type to tell the caller what the inconsistency is
2819 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2820 * \param[in] unknown set if does not know how to repair the inconsistency
2822 * \retval positive number for repaired cases
2823 * \retval 0 if nothing to be repaired
2824 * \retval negative error number on failure
2827 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2828 struct lfsck_component *com,
2829 struct dt_object *child,
2830 const struct lu_fid *pfid,
2831 struct linkea_data *ldata,
2832 struct lustre_handle *lh,
2833 enum lfsck_namespace_inconsistency_type *type,
2834 bool lpf, bool *unknown)
2836 struct lfsck_thread_info *info = lfsck_env_info(env);
2837 struct lu_name *cname = &info->lti_name;
2838 const struct lu_fid *cfid = lfsck_dto2fid(child);
2839 struct lu_fid *pfid2 = &info->lti_fid3;
2841 struct lfsck_namespace *ns = com->lc_file_ram;
2842 struct lfsck_instance *lfsck = com->lc_lfsck;
2843 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2844 struct dt_object *parent = NULL;
2845 struct linkea_data ldata_new = { NULL };
2846 int dirent_count = 0;
2852 while (ldata->ld_lee != NULL) {
2853 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2855 sizeof(info->lti_key));
2856 /* Drop invalid linkEA entry. */
2858 lfsck_linkea_del_buf(ldata, cname);
2862 /* Drop repeated linkEA entries. */
2863 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2865 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2866 * then it is possible that: the directry object has ever
2867 * been lost, but its name entry was there. In the former
2868 * LFSCK run, during the first-stage scanning, the LFSCK
2869 * found the dangling name entry, but it did not recreate
2870 * the lost object, and when moved to the second-stage
2871 * scanning, some children objects of the lost directory
2872 * object were found, then the LFSCK recreated such lost
2873 * directory object as an orphan.
2875 * When the LFSCK runs again, if the dangling name is still
2876 * there, the LFSCK should move the orphan directory object
2877 * back to the normal namespace. */
2878 if (!lpf && !fid_is_zero(pfid) &&
2879 !lu_fid_eq(pfid, &tfid) && once) {
2880 linkea_next_entry(ldata);
2884 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2886 RETURN(PTR_ERR(parent));
2888 if (!dt_object_exists(parent)) {
2889 lfsck_object_put(env, parent);
2890 if (ldata->ld_leh->leh_reccount > 1) {
2891 /* If it is NOT the last linkEA entry, then
2892 * there is still other chance to make the
2893 * child to be visible via other parent, then
2894 * remove this linkEA entry. */
2895 lfsck_linkea_del_buf(ldata, cname);
2902 /* The linkEA entry with bad parent will be removed. */
2903 if (unlikely(!dt_try_as_dir(env, parent))) {
2904 lfsck_object_put(env, parent);
2905 lfsck_linkea_del_buf(ldata, cname);
2909 rc = dt_lookup_dir(env, parent, cname->ln_name, &tfid);
2910 *pfid2 = *lfsck_dto2fid(parent);
2911 if (rc == -ENOENT) {
2912 lfsck_object_put(env, parent);
2913 linkea_next_entry(ldata);
2918 lfsck_object_put(env, parent);
2923 if (lu_fid_eq(&tfid, cfid)) {
2924 lfsck_object_put(env, parent);
2925 /* If the parent (that is declared via linkEA entry)
2926 * directory contains the specified child, but such
2927 * parent does not match the dotdot name entry, then
2928 * trust the linkEA. */
2929 if (!fid_is_zero(pfid) && !lu_fid_eq(pfid, pfid2)) {
2930 *type = LNIT_UNMATCHED_PAIRS;
2931 rc = lfsck_namespace_repair_unmatched_pairs(env,
2932 com, child, pfid2, cname);
2938 /* It is the most common case that we find the
2939 * name entry corresponding to the linkEA entry
2940 * that matches the ".." name entry. */
2941 rc = linkea_links_new(&ldata_new, &info->lti_big_buf,
2946 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2951 lfsck_linkea_del_buf(ldata, cname);
2952 linkea_first_entry(ldata);
2953 /* There may be some invalid dangling name entries under
2954 * other parent directories, remove all of them. */
2955 while (ldata->ld_lee != NULL) {
2956 rc = lfsck_namespace_unpack_linkea_entry(ldata,
2957 cname, &tfid, info->lti_key,
2958 sizeof(info->lti_key));
2962 parent = lfsck_object_find_bottom(env, lfsck,
2964 if (IS_ERR(parent)) {
2965 rc = PTR_ERR(parent);
2966 if (rc != -ENOENT &&
2967 bk->lb_param & LPF_FAILOUT)
2973 if (!dt_object_exists(parent)) {
2974 lfsck_object_put(env, parent);
2978 rc = lfsck_namespace_repair_dirent(env, com,
2979 parent, child, cname->ln_name,
2980 cname->ln_name, S_IFDIR, false, true);
2981 lfsck_object_put(env, parent);
2983 if (bk->lb_param & LPF_FAILOUT)
2992 lfsck_linkea_del_buf(ldata, cname);
2995 ns->ln_dirent_repaired += dirent_count;
2998 } /* lu_fid_eq(&tfid, lfsck_dto2fid(child)) */
3000 lfsck_ibits_unlock(lh, LCK_EX);
3001 /* The name entry references another MDT-object that may be
3002 * created by the LFSCK for repairing dangling name entry.
3003 * Try to replace it. */
3004 rc = lfsck_namespace_replace_cond(env, com, parent, child,
3006 lfsck_object_put(env, parent);
3013 lfsck_linkea_del_buf(ldata, cname);
3014 } /* while (ldata->ld_lee != NULL) */
3016 /* If there is still linkEA overflow, return. */
3017 if (unlikely(ldata->ld_leh->leh_overflow_time))
3020 linkea_first_entry(ldata);
3021 if (ldata->ld_leh->leh_reccount == 1) {
3022 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
3023 lh, type, NULL, unknown);
3028 /* All linkEA entries are invalid and removed, then handle the @child
3030 if (ldata->ld_leh->leh_reccount == 0) {
3031 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
3037 /* If the dangling name entry for the orphan directory object has
3038 * been remvoed, then just check whether the directory object is
3039 * still under the .lustre/lost+found/MDTxxxx/ or not. */
3045 /* There is no linkEA entry that matches the ".." name entry. Find
3046 * the first linkEA entry that both parent and name entry exist to
3047 * rebuild a new ".." name entry. */
3057 * Repair the object's nlink attribute.
3059 * If all the known name entries have been verified, then the object's hard
3060 * link attribute should match the object's linkEA entries count unless the
3061 * object's has too many hard link to be recorded in the linkEA. Such cases
3062 * should have been marked in the LFSCK trace file. Otherwise, trust the
3063 * linkEA to update the object's nlink attribute.
3065 * \param[in] env pointer to the thread context
3066 * \param[in] com pointer to the lfsck component
3067 * \param[in] obj pointer to the dt_object to be handled
3068 * \param[in,out] la pointer to buffer to object's attribute before
3069 * and after the repairing
3071 * \retval positive number for repaired cases
3072 * \retval 0 if nothing to be repaired
3073 * \retval negative error number on failure
3075 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
3076 struct lfsck_component *com,
3077 struct dt_object *obj,
3080 struct lfsck_namespace *ns = com->lc_file_ram;
3081 struct lfsck_instance *lfsck = com->lc_lfsck;
3082 struct dt_device *dev = lfsck_obj2dev(obj);
3083 const struct lu_fid *cfid = lfsck_dto2fid(obj);
3084 struct thandle *th = NULL;
3085 struct linkea_data ldata = { NULL };
3086 struct lustre_handle lh = { 0 };
3087 __u32 old = la->la_nlink;
3091 LASSERT(!dt_object_remote(obj));
3093 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3094 MDS_INODELOCK_UPDATE, LCK_PW);
3098 th = dt_trans_create(env, dev);
3100 GOTO(log, rc = PTR_ERR(th));
3102 la->la_valid = LA_NLINK;
3103 rc = dt_declare_attr_set(env, obj, la, th);
3107 rc = dt_trans_start_local(env, dev, th);
3111 dt_write_lock(env, obj, 0);
3112 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
3113 * ever tried to verify some remote MDT-object that resides on this
3114 * MDT, but this MDT failed to respond such request. So means there
3115 * may be some remote name entry on other MDT that references this
3116 * object with another name, so we cannot know whether this linkEA
3117 * is valid or not. So keep it there and maybe resolved when next
3119 if (ns->ln_flags & LF_INCOMPLETE)
3120 GOTO(unlock, rc = 0);
3122 rc = dt_attr_get(env, obj, la);
3124 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
3126 rc = lfsck_links_read2_with_rec(env, obj, &ldata);
3128 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
3130 /* XXX: Currently, we only update the nlink attribute if the known
3131 * linkEA entries is larger than the nlink attribute. That is
3133 if (la->la_nlink >= ldata.ld_leh->leh_reccount ||
3134 unlikely(la->la_nlink == 0 ||
3135 ldata.ld_leh->leh_overflow_time))
3136 GOTO(unlock, rc = 0);
3138 la->la_nlink = ldata.ld_leh->leh_reccount;
3139 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3140 GOTO(unlock, rc = 1);
3142 rc = dt_attr_set(env, obj, la, th);
3144 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3147 dt_write_unlock(env, obj);
3150 dt_trans_stop(env, dev, th);
3153 lfsck_ibits_unlock(&lh, LCK_PW);
3154 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
3155 "nlink count from %u to %u: rc = %d\n",
3156 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
3159 ns->ln_flags |= LF_INCONSISTENT;
3165 * Double scan the directory object for namespace LFSCK.
3167 * This function will verify the <parent, child> pairs in the namespace tree:
3168 * the parent references the child via some name entry that should be in the
3169 * child's linkEA entry, the child should back references the parent via its
3172 * The LFSCK will scan every linkEA entry in turn until find out the first
3173 * matched pairs. If found, then all other linkEA entries will be dropped.
3174 * If all the linkEA entries cannot match the ".." name entry, then there
3175 * are serveral possible cases:
3177 * 1) If there is only one linkEA entry, then trust it as long as the PFID
3178 * in the linkEA entry is valid.
3180 * 2) If there are multiple linkEA entries, then try to find the linkEA
3181 * that matches the ".." name entry. If found, then all other entries
3182 * are invalid; otherwise, it is quite possible that the ".." name entry
3183 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
3184 * entry according to the first valid linkEA entry (both the parent and
3185 * the name entry should exist).
3187 * 3) If the directory object has no (valid) linkEA entry, then the
3188 * directory object will be handled as pure orphan and inserted
3189 * in the .lustre/lost+found/MDTxxxx/ with the name:
3190 * ${self_FID}-${PFID}-D-${conflict_version}
3192 * \param[in] env pointer to the thread context
3193 * \param[in] com pointer to the lfsck component
3194 * \param[in] child pointer to the directory object to be handled
3195 * \param[in] flags to indicate the specical checking on the @child
3197 * \retval positive number for repaired cases
3198 * \retval 0 if nothing to be repaired
3199 * \retval negative error number on failure
3201 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3202 struct lfsck_component *com,
3203 struct dt_object *child, __u8 flags)
3205 struct lfsck_thread_info *info = lfsck_env_info(env);
3206 const struct lu_fid *cfid = lfsck_dto2fid(child);
3207 struct lu_fid *pfid = &info->lti_fid2;
3208 struct lfsck_namespace *ns = com->lc_file_ram;
3209 struct lfsck_instance *lfsck = com->lc_lfsck;
3210 struct lustre_handle lh = { 0 };
3211 struct linkea_data ldata = { NULL };
3212 bool unknown = false;
3215 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3219 LASSERT(!dt_object_remote(child));
3221 if (flags & LNTF_UNCERTAIN_LMV) {
3222 if (flags & LNTF_RECHECK_NAME_HASH) {
3223 rc = lfsck_namespace_scan_shard(env, com, child);
3227 ns->ln_striped_shards_scanned++;
3229 ns->ln_striped_shards_skipped++;
3233 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3237 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3238 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3240 "%s: some MDT(s) maybe NOT take part in the the namespace LFSCK, then the LFSCK cannot guarantee all the name entries have been verified in first-stage scanning. So have to skip orphan related handling for the directory object "DFID" with remote name entry\n",
3241 lfsck_lfsck2name(lfsck), PFID(cfid));
3246 if (unlikely(!dt_try_as_dir(env, child)))
3247 GOTO(out, rc = -ENOTDIR);
3249 /* We only take ldlm lock on the @child when required. When the
3250 * logic comes here for the first time, it is always false. */
3254 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3255 MDS_INODELOCK_UPDATE |
3256 MDS_INODELOCK_XATTR, LCK_EX);
3261 dt_read_lock(env, child, 0);
3262 if (unlikely(lfsck_is_dead_obj(child))) {
3263 dt_read_unlock(env, child);
3268 rc = dt_lookup_dir(env, child, dotdot, pfid);
3270 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3271 dt_read_unlock(env, child);
3276 if (!lustre_handle_is_used(&lh)) {
3277 dt_read_unlock(env, child);
3282 } else if (lfsck->li_lpf_obj != NULL &&
3283 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3285 } else if (unlikely(!fid_is_sane(pfid))) {
3289 rc = lfsck_links_read(env, child, &ldata);
3290 dt_read_unlock(env, child);
3292 if (rc != -ENODATA && rc != -EINVAL)
3295 if (!lustre_handle_is_used(&lh))
3298 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3299 /* Remove the corrupted linkEA. */
3300 rc = lfsck_namespace_links_remove(env, com, child);
3302 /* Here, because of the crashed linkEA, we
3303 * cannot know whether there is some parent
3304 * that references the child directory via
3305 * some name entry or not. So keep it there,
3306 * when the LFSCK run next time, if there is
3307 * some parent that references this object,
3308 * then the LFSCK can rebuild the linkEA;
3309 * otherwise, this object will be handled
3310 * as orphan as above. */
3313 /* 1. If we have neither ".." nor linkEA,
3314 * then it is an orphan.
3316 * 2. If we only have the ".." name entry,
3317 * but no parent references this child
3318 * directory, then handle it as orphan. */
3319 lfsck_ibits_unlock(&lh, LCK_EX);
3320 type = LNIT_MUL_REF;
3322 /* If the LFSCK is marked as LF_INCOMPLETE,
3323 * then means some MDT has ever tried to
3324 * verify some remote MDT-object that resides
3325 * on this MDT, but this MDT failed to respond
3326 * such request. So means there may be some
3327 * remote name entry on other MDT that
3328 * references this object with another name,
3329 * so we cannot know whether this linkEA is
3330 * valid or not. So keep it there and maybe
3331 * resolved when next LFSCK run. */
3332 if (ns->ln_flags & LF_INCOMPLETE)
3335 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3336 "-"DFID, PFID(pfid));
3337 rc = lfsck_namespace_insert_orphan(env, com, child,
3338 info->lti_tmpbuf, "D", NULL);
3344 linkea_first_entry(&ldata);
3345 /* This is the most common case: the object has unique linkEA entry. */
3346 if (ldata.ld_leh->leh_reccount == 1) {
3347 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3348 &lh, &type, &retry, &unknown);
3350 LASSERT(!lustre_handle_is_used(&lh));
3359 if (!lustre_handle_is_used(&lh))
3362 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3363 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3369 /* When we come here, the cases usually like that:
3370 * 1) The directory object has a corrupted linkEA entry. During the
3371 * first-stage scanning, the LFSCK cannot know such corruption,
3372 * then it appends the right linkEA entry according to the found
3373 * name entry after the bad one.
3375 * 2) The directory object has a right linkEA entry. During the
3376 * first-stage scanning, the LFSCK finds some bad name entry,
3377 * but the LFSCK cannot aware that at that time, then it adds
3378 * the bad linkEA entry for further processing. */
3379 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3380 &lh, &type, lpf, &unknown);
3385 lfsck_ibits_unlock(&lh, LCK_EX);
3388 case LNIT_BAD_LINKEA:
3389 ns->ln_linkea_repaired++;
3391 case LNIT_UNMATCHED_PAIRS:
3392 ns->ln_unmatched_pairs_repaired++;
3395 ns->ln_mul_ref_repaired++;
3403 ns->ln_unknown_inconsistency++;
3409 lfsck_namespace_linkea_stale_overflow(struct linkea_data *ldata,
3410 struct lfsck_namespace *ns)
3412 /* Both the leh_overflow_time and ln_time_latest_reset are
3413 * local time based, so need NOT to care about clock drift
3414 * among the servers. */
3415 return ldata->ld_leh->leh_overflow_time &&
3416 ldata->ld_leh->leh_overflow_time < ns->ln_time_latest_reset;
3420 * Clear the object's linkEA overflow timestamp.
3422 * If the MDT-object has too many hard links as to the linkEA cannot hold
3423 * all of them, then overflow timestamp will be set in the linkEA header.
3424 * If some hard links are removed after that, then it is possible to hold
3425 * other missed linkEA entries. If the namespace LFSCK have added all the
3426 * related linkEA entries, then it will remove the overflow timestamp.
3428 * \param[in] env pointer to the thread context
3429 * \param[in] com pointer to the lfsck component
3430 * \param[in] ldata pointer to the linkEA data for the given @obj
3431 * \param[in] obj pointer to the dt_object to be handled
3433 * \retval positive number for repaired cases
3434 * \retval 0 if nothing to be repaired
3435 * \retval negative error number on failure
3437 static int lfsck_namespace_linkea_clear_overflow(const struct lu_env *env,
3438 struct lfsck_component *com,
3439 struct linkea_data *ldata,
3440 struct dt_object *obj)
3442 struct lfsck_namespace *ns = com->lc_file_ram;
3443 struct lfsck_instance *lfsck = com->lc_lfsck;
3444 struct dt_device *dev = lfsck_obj2dev(obj);
3445 struct thandle *th = NULL;
3446 struct lustre_handle lh = { 0 };
3447 struct lu_buf linkea_buf;
3451 LASSERT(!dt_object_remote(obj));
3453 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3454 MDS_INODELOCK_UPDATE, LCK_PW);
3458 th = dt_trans_create(env, dev);
3460 GOTO(log, rc = PTR_ERR(th));
3462 rc = dt_declare_xattr_set(env, obj,
3463 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
3464 XATTR_NAME_LINK, 0, th);
3468 rc = dt_trans_start_local(env, dev, th);
3472 dt_write_lock(env, obj, 0);
3473 rc = lfsck_links_read(env, obj, ldata);
3477 if (unlikely(!lfsck_namespace_linkea_stale_overflow(ldata, ns)))
3478 GOTO(unlock, rc = 0);
3480 ldata->ld_leh->leh_overflow_time = 0;
3481 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3482 GOTO(unlock, rc = 1);
3484 /* If all known entries are in the linkEA, then the 'leh_reccount'
3485 * should NOT be zero. */
3486 LASSERT(ldata->ld_leh->leh_reccount > 0);
3488 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
3489 ldata->ld_leh->leh_len);
3490 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK, 0, th);
3491 if (unlikely(rc == -ENOSPC))
3499 dt_write_unlock(env, obj);
3502 dt_trans_stop(env, dev, th);
3505 lfsck_ibits_unlock(&lh, LCK_PW);
3506 CDEBUG(D_LFSCK, "%s: clear linkea overflow timestamp for the object "
3508 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
3514 * Verify the object's agent entry.
3516 * If the object claims to have agent entry but the linkEA does not contain
3517 * remote parent, then remove the agent entry. Otherwise, if the object has
3518 * no agent entry but its linkEA contains remote parent, then will generate
3519 * agent entry for it.
3521 * \param[in] env pointer to the thread context
3522 * \param[in] com pointer to the lfsck component
3523 * \param[in] obj pointer to the dt_object to be handled
3525 * \retval positive number for repaired cases
3526 * \retval 0 if nothing to be repaired
3527 * \retval negative error number on failure
3529 static int lfsck_namespace_check_agent_entry(const struct lu_env *env,
3530 struct lfsck_component *com,
3531 struct dt_object *obj)
3533 struct linkea_data ldata = { NULL };
3534 struct lfsck_thread_info *info = lfsck_env_info(env);
3535 struct lfsck_namespace *ns = com->lc_file_ram;
3536 struct lfsck_instance *lfsck = com->lc_lfsck;
3537 struct lu_fid *pfid = &info->lti_fid2;
3538 struct lu_name *cname = &info->lti_name;
3539 struct lu_seq_range *range = &info->lti_range;
3540 struct seq_server_site *ss = lfsck_dev_site(lfsck);
3541 __u32 idx = lfsck_dev_idx(lfsck);
3543 bool remote = false;
3546 if (!(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT))
3549 rc = lfsck_links_read_with_rec(env, obj, &ldata);
3550 if (rc == -ENOENT || rc == -ENODATA)
3553 if (rc && rc != -EINVAL)
3556 /* We check the agent entry again after verifying the linkEA
3557 * successfully. So invalid linkEA should be dryrun mode. */
3558 if (rc == -EINVAL || unlikely(!ldata.ld_leh->leh_reccount))
3561 linkea_first_entry(&ldata);
3562 while (ldata.ld_lee != NULL && !remote) {
3563 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3565 if (!linkea_entry_is_valid(&ldata, cname, pfid))
3568 fld_range_set_mdt(range);
3569 rc = fld_server_lookup(env, ss->ss_server_fld,
3570 fid_seq(pfid), range);
3572 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
3574 if (range->lsr_index != idx)
3577 linkea_next_entry(&ldata);
3580 if ((lu_object_has_agent_entry(&obj->do_lu) && !remote) ||
3581 (!lu_object_has_agent_entry(&obj->do_lu) && remote)) {
3582 struct dt_device *dev = lfsck_obj2dev(obj);
3583 struct linkea_data ldata2 = { NULL };
3584 struct lustre_handle lh = { 0 };
3585 struct lu_buf linkea_buf;
3586 struct thandle *handle;
3588 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3591 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3592 MDS_INODELOCK_UPDATE |
3593 MDS_INODELOCK_XATTR, LCK_EX);
3597 handle = dt_trans_create(env, dev);
3599 GOTO(unlock, rc = PTR_ERR(handle));
3601 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
3602 ldata.ld_leh->leh_len);
3603 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
3604 XATTR_NAME_LINK, LU_XATTR_REPLACE, handle);
3608 rc = dt_trans_start_local(env, dev, handle);
3612 dt_write_lock(env, obj, 0);
3613 rc = lfsck_links_read2_with_rec(env, obj, &ldata2);
3615 if (rc == -ENOENT || rc == -ENODATA)
3620 /* If someone changed linkEA by race, then the agent
3621 * entry will be updated by lower layer automatically. */
3622 if (ldata.ld_leh->leh_len != ldata2.ld_leh->leh_len ||
3623 memcmp(ldata.ld_buf->lb_buf, ldata2.ld_buf->lb_buf,
3624 ldata.ld_leh->leh_len) != 0)
3625 GOTO(unlock2, rc = 0);
3627 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK,
3628 LU_XATTR_REPLACE, handle);
3635 dt_write_unlock(env, obj);
3637 dt_trans_stop(env, dev, handle);
3639 lfsck_ibits_unlock(&lh, LCK_EX);
3646 ns->ln_agent_entries_repaired++;
3648 CDEBUG(D_LFSCK, "%s: repair agent entry for "DFID": rc = %d\n",
3649 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
3654 * Double scan the MDT-object for namespace LFSCK.
3656 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3657 * those entries from the linkEA; if the linkEA becomes empty or the object
3658 * has no linkEA, then it is an orphan and will be added into the directory
3659 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3660 * the remote parent; if the name entry corresponding to some linkEA entry
3661 * is lost, then add the name entry back to the namespace.
3663 * \param[in] env pointer to the thread context
3664 * \param[in] com pointer to the lfsck component
3665 * \param[in] child pointer to the dt_object to be handled
3666 * \param[in] flags some hints to indicate how the @child should be handled
3668 * \retval positive number for repaired cases
3669 * \retval 0 if nothing to be repaired
3670 * \retval negative error number on failure
3672 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3673 struct lfsck_component *com,
3674 struct dt_object *child, __u8 flags)
3676 struct lfsck_thread_info *info = lfsck_env_info(env);
3677 struct lu_attr *la = &info->lti_la;
3678 struct lu_name *cname = &info->lti_name;
3679 struct lu_fid *pfid = &info->lti_fid;
3680 struct lu_fid *cfid = &info->lti_fid2;
3681 struct lfsck_instance *lfsck = com->lc_lfsck;
3682 struct lfsck_namespace *ns = com->lc_file_ram;
3683 struct dt_object *parent = NULL;
3684 struct linkea_data ldata = { NULL };
3685 bool repaired = false;
3690 dt_read_lock(env, child, 0);
3691 if (unlikely(lfsck_is_dead_obj(child))) {
3692 dt_read_unlock(env, child);
3697 if (S_ISDIR(lfsck_object_type(child))) {
3698 dt_read_unlock(env, child);
3699 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3700 if (!rc && flags & LNTF_CHECK_AGENT_ENTRY)
3701 rc = lfsck_namespace_check_agent_entry(env, com, child);
3706 rc = lfsck_links_read(env, child, &ldata);
3707 dt_read_unlock(env, child);
3709 if (rc == -EINVAL) {
3710 struct lustre_handle lh = { 0 };
3712 rc = lfsck_ibits_lock(env, com->lc_lfsck, child, &lh,
3713 MDS_INODELOCK_UPDATE |
3714 MDS_INODELOCK_XATTR, LCK_EX);
3716 rc = lfsck_namespace_links_remove(env, com, child);
3717 lfsck_ibits_unlock(&lh, LCK_EX);
3726 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3727 unlikely(lfsck_namespace_linkea_stale_overflow(&ldata, ns))) {
3728 rc = lfsck_namespace_linkea_clear_overflow(env, com, &ldata,
3734 ns->ln_linkea_overflow_cleared++;
3737 linkea_first_entry(&ldata);
3738 while (ldata.ld_lee != NULL) {
3739 rc = lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3741 sizeof(info->lti_key));
3742 /* Invalid PFID in the linkEA entry. */
3744 rc = lfsck_namespace_shrink_linkea(env, com, child,
3745 &ldata, cname, pfid, true);
3755 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3757 /* Found repeated linkEA entries */
3759 rc = lfsck_namespace_shrink_linkea(env, com, child,
3760 &ldata, cname, pfid, false);
3772 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3774 GOTO(out, rc = PTR_ERR(parent));
3776 if (!dt_object_exists(parent)) {
3779 if (ldata.ld_leh->leh_reccount > 1) {
3780 /* If it is NOT the last linkEA entry, then
3781 * there is still other chance to make the
3782 * child to be visible via other parent, then
3783 * remove this linkEA entry. */
3784 rc = lfsck_namespace_shrink_linkea(env, com,
3785 child, &ldata, cname, pfid, true);
3787 /* If the LFSCK is marked as LF_INCOMPLETE,
3788 * then means some MDT has ever tried to
3789 * verify some remote MDT-object that resides
3790 * on this MDT, but this MDT failed to respond
3791 * such request. So means there may be some
3792 * remote name entry on other MDT that
3793 * references this object with another name,
3794 * so we cannot know whether this linkEA is
3795 * valid or not. So keep it there and maybe
3796 * resolved when next LFSCK run. */
3797 if (ns->ln_flags & LF_INCOMPLETE) {
3798 lfsck_object_put(env, parent);
3803 /* Create the lost parent as an orphan. */
3804 rc = lfsck_namespace_create_orphan_dir(env, com,
3807 lfsck_object_put(env, parent);
3815 /* Add the missing name entry to the parent. */
3816 rc = lfsck_namespace_insert_normal(env, com,
3817 parent, child, cname);
3818 if (unlikely(rc == -EEXIST))
3819 /* Unfortunately, someone reused the
3820 * name under the parent by race. So we
3821 * have to remove the linkEA entry from
3822 * current child object. It means that
3823 * the LFSCK cannot recover the system
3824 * totally back to its original status,
3825 * but it is necessary to make the
3826 * current system to be consistent. */
3827 rc = lfsck_namespace_shrink_linkea(env,
3831 linkea_next_entry(&ldata);
3834 lfsck_object_put(env, parent);
3842 } /* !dt_object_exists(parent) */
3844 /* The linkEA entry with bad parent will be removed. */
3845 if (unlikely(!dt_try_as_dir(env, parent))) {
3846 lfsck_object_put(env, parent);
3847 rc = lfsck_namespace_shrink_linkea(env, com, child,
3848 &ldata, cname, pfid, true);
3858 rc = dt_lookup_dir(env, parent, cname->ln_name, cfid);
3859 if (rc != 0 && rc != -ENOENT) {
3860 lfsck_object_put(env, parent);
3866 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3867 /* It is the most common case that we
3868 * find the name entry corresponding
3869 * to the linkEA entry. */
3870 lfsck_object_put(env, parent);
3871 linkea_next_entry(&ldata);
3873 /* The name entry references another
3874 * MDT-object that may be created by
3875 * the LFSCK for repairing dangling
3876 * name entry. Try to replace it. */
3877 rc = lfsck_namespace_replace_cond(env, com,
3878 parent, child, cfid, cname);
3879 lfsck_object_put(env, parent);
3885 linkea_next_entry(&ldata);
3887 rc = lfsck_namespace_shrink_linkea(env,
3901 /* The following handles -ENOENT case */
3903 rc = dt_attr_get(env, child, la);
3907 /* If there is no name entry in the parent dir and the object
3908 * link count is fewer than the linkea entries count, then the
3909 * linkea entry should be removed. */
3910 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3911 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3912 parent, child, &ldata, cname, pfid);
3913 lfsck_object_put(env, parent);
3923 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3924 * MDT has ever tried to verify some remote MDT-object that
3925 * resides on this MDT, but this MDT failed to respond such
3926 * request. So means there may be some remote name entry on
3927 * other MDT that references this object with another name,
3928 * so we cannot know whether this linkEA is valid or not.
3929 * So keep it there and maybe resolved when next LFSCK run. */
3930 if (ns->ln_flags & LF_INCOMPLETE) {
3931 lfsck_object_put(env, parent);
3936 rc = lfsck_namespace_check_name(env, lfsck, parent, child,
3942 lfsck_object_put(env, parent);
3947 /* It is an invalid name entry, drop it. */
3948 if (unlikely(rc > 0)) {
3949 lfsck_object_put(env, parent);
3950 rc = lfsck_namespace_shrink_linkea(env, com, child,
3951 &ldata, cname, pfid, true);
3961 /* Add the missing name entry back to the namespace. */
3962 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3964 if (unlikely(rc == -ESTALE))
3965 /* It may happen when the remote object has been
3966 * removed, but the local MDT is not aware of that. */
3969 if (unlikely(rc == -EEXIST))
3970 /* Unfortunately, someone reused the name under the
3971 * parent by race. So we have to remove the linkEA
3972 * entry from current child object. It means that the
3973 * LFSCK cannot recover the system totally back to
3974 * its original status, but it is necessary to make
3975 * the current system to be consistent.
3977 * It also may be because of the LFSCK found some
3978 * internal status of create operation. Under such
3979 * case, nothing to be done. */
3980 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3981 parent, child, &ldata, cname, pfid);
3983 linkea_next_entry(&ldata);
3985 lfsck_object_put(env, parent);
3996 if (rc < 0 && rc != -ENODATA)
3999 if (rc == 0 && ldata.ld_leh != NULL)
4000 count = ldata.ld_leh->leh_reccount;
4003 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
4004 * MDT has ever tried to verify some remote MDT-object that
4005 * resides on this MDT, but this MDT failed to respond such
4006 * request. So means there may be some remote name entry on
4007 * other MDT that references this object with another name,
4008 * so we cannot know whether this linkEA is valid or not.
4009 * So keep it there and maybe resolved when next LFSCK run. */
4010 if (!(ns->ln_flags & LF_INCOMPLETE) &&
4011 (ldata.ld_leh == NULL ||
4012 !ldata.ld_leh->leh_overflow_time)) {
4013 /* If the child becomes orphan, then insert it into
4014 * the global .lustre/lost+found/MDTxxxx directory. */
4015 rc = lfsck_namespace_insert_orphan(env, com, child,
4021 ns->ln_mul_ref_repaired++;
4026 rc = dt_attr_get(env, child, la);
4030 if (la->la_nlink != 0 && la->la_nlink != count) {
4031 if (unlikely(!S_ISREG(lfsck_object_type(child)) &&
4032 !S_ISLNK(lfsck_object_type(child)))) {
4033 CDEBUG(D_LFSCK, "%s: namespace LFSCK finds "
4034 "the object "DFID"'s nlink count %d "
4035 "does not match linkEA count %d, "
4036 "type %o, skip it.\n",
4037 lfsck_lfsck2name(lfsck),
4038 PFID(lfsck_dto2fid(child)),
4039 la->la_nlink, count,
4040 lfsck_object_type(child));
4041 } else if (la->la_nlink < count &&
4042 likely(!ldata.ld_leh->leh_overflow_time)) {
4043 rc = lfsck_namespace_repair_nlink(env, com,
4046 ns->ln_objs_nlink_repaired++;
4054 if (la->la_nlink > 1)
4055 ns->ln_mul_linked_repaired++;
4061 if (!rc && flags & LNTF_CHECK_AGENT_ENTRY)
4062 rc = lfsck_namespace_check_agent_entry(env, com, child);
4067 static void lfsck_namespace_dump_statistics(struct seq_file *m,
4068 struct lfsck_namespace *ns,
4069 __u64 checked_phase1,
4070 __u64 checked_phase2,
4071 time64_t time_phase1,
4072 time64_t time_phase2, bool dryrun)
4074 const char *postfix = dryrun ? "inconsistent" : "repaired";
4076 seq_printf(m, "checked_phase1: %llu\n"
4077 "checked_phase2: %llu\n"
4080 "failed_phase1: %llu\n"
4081 "failed_phase2: %llu\n"
4082 "directories: %llu\n"
4086 "multiple_linked_checked: %llu\n"
4087 "multiple_linked_%s: %llu\n"
4088 "unknown_inconsistency: %llu\n"
4089 "unmatched_pairs_%s: %llu\n"
4090 "dangling_%s: %llu\n"
4091 "multiple_referenced_%s: %llu\n"
4092 "bad_file_type_%s: %llu\n"
4093 "lost_dirent_%s: %llu\n"
4094 "local_lost_found_scanned: %llu\n"
4095 "local_lost_found_moved: %llu\n"
4096 "local_lost_found_skipped: %llu\n"
4097 "local_lost_found_failed: %llu\n"
4098 "striped_dirs_scanned: %llu\n"
4099 "striped_dirs_%s: %llu\n"
4100 "striped_dirs_failed: %llu\n"
4101 "striped_dirs_disabled: %llu\n"
4102 "striped_dirs_skipped: %llu\n"
4103 "striped_shards_scanned: %llu\n"
4104 "striped_shards_%s: %llu\n"
4105 "striped_shards_failed: %llu\n"
4106 "striped_shards_skipped: %llu\n"
4107 "name_hash_%s: %llu\n"
4108 "linkea_overflow_%s: %llu\n"
4109 "agent_entries_%s: %llu\n"
4110 "success_count: %u\n"
4111 "run_time_phase1: %lld seconds\n"
4112 "run_time_phase2: %lld seconds\n",
4115 dryrun ? "inconsistent" : "updated",
4116 ns->ln_items_repaired,
4117 dryrun ? "inconsistent" : "updated",
4118 ns->ln_objs_repaired_phase2,
4119 ns->ln_items_failed,
4120 ns->ln_objs_failed_phase2,
4121 ns->ln_dirs_checked,
4122 postfix, ns->ln_dirent_repaired,
4123 postfix, ns->ln_linkea_repaired,
4124 postfix, ns->ln_objs_nlink_repaired,
4125 ns->ln_mul_linked_checked,
4126 postfix, ns->ln_mul_linked_repaired,
4127 ns->ln_unknown_inconsistency,
4128 postfix, ns->ln_unmatched_pairs_repaired,
4129 postfix, ns->ln_dangling_repaired,
4130 postfix, ns->ln_mul_ref_repaired,
4131 postfix, ns->ln_bad_type_repaired,
4132 postfix, ns->ln_lost_dirent_repaired,
4133 ns->ln_local_lpf_scanned,
4134 ns->ln_local_lpf_moved,
4135 ns->ln_local_lpf_skipped,
4136 ns->ln_local_lpf_failed,
4137 ns->ln_striped_dirs_scanned,
4138 postfix, ns->ln_striped_dirs_repaired,
4139 ns->ln_striped_dirs_failed,
4140 ns->ln_striped_dirs_disabled,
4141 ns->ln_striped_dirs_skipped,
4142 ns->ln_striped_shards_scanned,
4143 postfix, ns->ln_striped_shards_repaired,
4144 ns->ln_striped_shards_failed,
4145 ns->ln_striped_shards_skipped,
4146 postfix, ns->ln_name_hash_repaired,
4147 dryrun ? "inconsistent" : "cleared",
4148 ns->ln_linkea_overflow_cleared,
4149 postfix, ns->ln_agent_entries_repaired,
4150 ns->ln_success_count,
4155 static void lfsck_namespace_release_lmv(const struct lu_env *env,
4156 struct lfsck_component *com)
4158 struct lfsck_instance *lfsck = com->lc_lfsck;
4159 struct lfsck_namespace *ns = com->lc_file_ram;
4161 while (!list_empty(&lfsck->li_list_lmv)) {
4162 struct lfsck_lmv_unit *llu;
4163 struct lfsck_lmv *llmv;
4165 llu = list_entry(lfsck->li_list_lmv.next,
4166 struct lfsck_lmv_unit, llu_link);
4167 llmv = &llu->llu_lmv;
4169 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
4170 "still in using: %u\n",
4171 atomic_read(&llmv->ll_ref));
4173 ns->ln_striped_dirs_skipped++;
4174 lfsck_lmv_put(env, llmv);
4178 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
4179 struct lfsck_component *com,
4180 struct dt_object *obj)
4182 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
4185 rc = dt_attr_get(env, obj, la);
4189 /* zero-linkEA object may be orphan, but it also maybe because
4190 * of upgrading. Currently, we cannot record it for double scan.
4191 * Because it may cause the LFSCK trace file to be too large. */
4193 /* "la_ctime" == 1 means that it has ever been removed from
4194 * backend /lost+found directory but not been added back to
4195 * the normal namespace yet. */
4197 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
4198 unlikely(la->la_ctime == 1))
4199 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
4200 LNTF_CHECK_LINKEA, true);
4205 /* namespace APIs */
4207 static int lfsck_namespace_reset(const struct lu_env *env,
4208 struct lfsck_component *com, bool init)
4210 struct lfsck_instance *lfsck = com->lc_lfsck;
4211 struct lfsck_namespace *ns = com->lc_file_ram;
4212 struct lfsck_assistant_data *lad = com->lc_data;
4213 struct dt_object *root;
4217 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
4219 GOTO(log, rc = PTR_ERR(root));
4221 if (unlikely(!dt_try_as_dir(env, root)))
4222 GOTO(put, rc = -ENOTDIR);
4224 down_write(&com->lc_sem);
4226 memset(ns, 0, sizeof(*ns));
4228 __u32 count = ns->ln_success_count;
4229 time64_t last_time = ns->ln_time_last_complete;
4231 memset(ns, 0, sizeof(*ns));
4232 ns->ln_success_count = count;
4233 ns->ln_time_last_complete = last_time;
4235 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
4236 ns->ln_status = LS_INIT;
4237 ns->ln_time_latest_reset = ktime_get_real_seconds();
4239 rc = lfsck_load_one_trace_file(env, com, root, &com->lc_obj,
4240 &dt_lfsck_namespace_features,
4241 LFSCK_NAMESPACE, true);
4245 rc = lfsck_load_sub_trace_files(env, com, &dt_lfsck_namespace_features,
4246 LFSCK_NAMESPACE, true);
4250 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
4251 CFS_RESET_BITMAP(lad->lad_bitmap);
4253 rc = lfsck_namespace_store(env, com);
4258 up_write(&com->lc_sem);
4261 lfsck_object_put(env, root);
4263 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
4264 lfsck_lfsck2name(lfsck), rc);
4269 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
4272 struct lfsck_namespace *ns = com->lc_file_ram;
4274 down_write(&com->lc_sem);
4276 com->lc_new_checked++;
4277 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4278 up_write(&com->lc_sem);
4281 static void lfsck_namespace_close_dir(const struct lu_env *env,
4282 struct lfsck_component *com)
4284 struct lfsck_namespace *ns = com->lc_file_ram;
4285 struct lfsck_assistant_data *lad = com->lc_data;
4286 struct lfsck_assistant_object *lso = NULL;
4287 struct lfsck_instance *lfsck = com->lc_lfsck;
4288 struct lfsck_lmv *llmv = lfsck->li_lmv;
4289 struct lfsck_namespace_req *lnr;
4290 struct lu_attr *la = &lfsck_env_info(env)->lti_la2;
4291 __u32 size = sizeof(*lnr) + LFSCK_TMPBUF_LEN;
4293 bool wakeup = false;
4299 rc = dt_attr_get(env, lfsck->li_obj_dir, la);
4303 OBD_ALLOC(lnr, size);
4305 ns->ln_striped_dirs_skipped++;
4310 lso = lfsck_assistant_object_init(env, lfsck_dto2fid(lfsck->li_obj_dir),
4311 la, lfsck->li_pos_current.lp_oit_cookie, true);
4313 OBD_FREE(lnr, size);
4314 ns->ln_striped_dirs_skipped++;
4319 /* Generate a dummy request to indicate that all shards' name entry
4320 * in this striped directory has been scanned for the first time. */
4321 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
4322 lnr->lnr_lar.lar_parent = lso;
4323 lnr->lnr_lmv = lfsck_lmv_get(llmv);
4324 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
4325 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
4326 lnr->lnr_size = size;
4327 lnr->lnr_type = lso->lso_attr.la_mode;
4329 spin_lock(&lad->lad_lock);
4330 if (lad->lad_assistant_status < 0 ||
4331 unlikely(!thread_is_running(&lfsck->li_thread) ||
4332 !thread_is_running(&lad->lad_thread))) {
4333 spin_unlock(&lad->lad_lock);
4334 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4335 ns->ln_striped_dirs_skipped++;
4340 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4341 if (lad->lad_prefetched == 0)
4344 lad->lad_prefetched++;
4345 spin_unlock(&lad->lad_lock);
4347 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4352 static int lfsck_namespace_open_dir(const struct lu_env *env,
4353 struct lfsck_component *com)
4355 struct lfsck_instance *lfsck = com->lc_lfsck;
4356 struct lfsck_namespace *ns = com->lc_file_ram;
4357 struct lfsck_lmv *llmv = lfsck->li_lmv;
4364 if (llmv->ll_lmv_master) {
4365 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
4367 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
4368 lmv->lmv_master_mdt_index =
4369 lfsck_dev_idx(lfsck);
4370 ns->ln_flags |= LF_INCONSISTENT;
4371 llmv->ll_lmv_updated = 1;
4374 rc = lfsck_namespace_verify_stripe_slave(env, com,
4375 lfsck->li_obj_dir, llmv);
4378 RETURN(rc > 0 ? 0 : rc);
4381 static int lfsck_namespace_checkpoint(const struct lu_env *env,
4382 struct lfsck_component *com, bool init)
4384 struct lfsck_instance *lfsck = com->lc_lfsck;
4385 struct lfsck_namespace *ns = com->lc_file_ram;
4389 rc = lfsck_checkpoint_generic(env, com);
4394 down_write(&com->lc_sem);
4396 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
4398 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4399 ns->ln_run_time_phase1 += ktime_get_seconds() -
4400 lfsck->li_time_last_checkpoint;
4401 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
4402 ns->ln_items_checked += com->lc_new_checked;
4403 com->lc_new_checked = 0;
4406 rc = lfsck_namespace_store(env, com);
4407 up_write(&com->lc_sem);
4410 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos [%llu"
4411 ", "DFID", %#llx], status = %d: rc = %d\n",
4412 lfsck_lfsck2name(lfsck), lfsck->li_pos_current.lp_oit_cookie,
4413 PFID(&lfsck->li_pos_current.lp_dir_parent),
4414 lfsck->li_pos_current.lp_dir_cookie, ns->ln_status, rc);
4416 return rc > 0 ? 0 : rc;
4419 static int lfsck_namespace_prep(const struct lu_env *env,
4420 struct lfsck_component *com,
4421 struct lfsck_start_param *lsp)
4423 struct lfsck_instance *lfsck = com->lc_lfsck;
4424 struct lfsck_namespace *ns = com->lc_file_ram;
4425 struct lfsck_position *pos = &com->lc_pos_start;
4428 rc = lfsck_namespace_load_bitmap(env, com);
4429 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
4430 rc = lfsck_namespace_reset(env, com, false);
4432 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
4435 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
4436 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4442 down_write(&com->lc_sem);
4443 ns->ln_time_latest_start = ktime_get_real_seconds();
4444 spin_lock(&lfsck->li_lock);
4446 if (ns->ln_flags & LF_SCANNED_ONCE) {
4447 if (!lfsck->li_drop_dryrun ||
4448 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4449 ns->ln_status = LS_SCANNING_PHASE2;
4450 list_move_tail(&com->lc_link,
4451 &lfsck->li_list_double_scan);
4452 if (!list_empty(&com->lc_link_dir))
4453 list_del_init(&com->lc_link_dir);
4454 lfsck_pos_set_zero(pos);
4456 ns->ln_status = LS_SCANNING_PHASE1;
4457 ns->ln_run_time_phase1 = 0;
4458 ns->ln_run_time_phase2 = 0;
4459 ns->ln_items_checked = 0;
4460 ns->ln_items_repaired = 0;
4461 ns->ln_items_failed = 0;
4462 ns->ln_dirs_checked = 0;
4463 ns->ln_objs_checked_phase2 = 0;
4464 ns->ln_objs_repaired_phase2 = 0;
4465 ns->ln_objs_failed_phase2 = 0;
4466 ns->ln_objs_nlink_repaired = 0;
4467 ns->ln_dirent_repaired = 0;
4468 ns->ln_linkea_repaired = 0;
4469 ns->ln_mul_linked_checked = 0;
4470 ns->ln_mul_linked_repaired = 0;
4471 ns->ln_unknown_inconsistency = 0;
4472 ns->ln_unmatched_pairs_repaired = 0;
4473 ns->ln_dangling_repaired = 0;
4474 ns->ln_mul_ref_repaired = 0;
4475 ns->ln_bad_type_repaired = 0;
4476 ns->ln_lost_dirent_repaired = 0;
4477 ns->ln_striped_dirs_scanned = 0;
4478 ns->ln_striped_dirs_repaired = 0;
4479 ns->ln_striped_dirs_failed = 0;
4480 ns->ln_striped_dirs_disabled = 0;
4481 ns->ln_striped_dirs_skipped = 0;
4482 ns->ln_striped_shards_scanned = 0;
4483 ns->ln_striped_shards_repaired = 0;
4484 ns->ln_striped_shards_failed = 0;
4485 ns->ln_striped_shards_skipped = 0;
4486 ns->ln_name_hash_repaired = 0;
4487 fid_zero(&ns->ln_fid_latest_scanned_phase2);
4488 if (list_empty(&com->lc_link_dir))
4489 list_add_tail(&com->lc_link_dir,
4490 &lfsck->li_list_dir);
4491 *pos = ns->ln_pos_first_inconsistent;
4494 ns->ln_status = LS_SCANNING_PHASE1;
4495 if (list_empty(&com->lc_link_dir))
4496 list_add_tail(&com->lc_link_dir,
4497 &lfsck->li_list_dir);
4498 if (!lfsck->li_drop_dryrun ||
4499 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4500 *pos = ns->ln_pos_last_checkpoint;
4501 pos->lp_oit_cookie++;
4503 *pos = ns->ln_pos_first_inconsistent;
4507 spin_unlock(&lfsck->li_lock);
4508 up_write(&com->lc_sem);
4510 rc = lfsck_start_assistant(env, com, lsp);
4512 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos [%llu, "
4513 DFID", %#llx]: rc = %d\n",
4514 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4515 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4520 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4521 struct lfsck_component *com,
4522 struct dt_object *obj)
4524 struct lfsck_thread_info *info = lfsck_env_info(env);
4525 struct lfsck_namespace *ns = com->lc_file_ram;
4526 struct lfsck_instance *lfsck = com->lc_lfsck;
4527 const struct lu_fid *fid = lfsck_dto2fid(obj);
4528 struct lu_fid *pfid = &info->lti_fid2;
4529 struct lu_name *cname = &info->lti_name;
4530 struct lu_seq_range *range = &info->lti_range;
4531 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4532 struct linkea_data ldata = { NULL };
4533 __u32 idx = lfsck_dev_idx(lfsck);
4534 struct lu_attr la = { .la_valid = 0 };
4535 bool remote = false;
4539 rc = dt_attr_get(env, obj, &la);
4540 if (unlikely(rc || (la.la_valid & LA_FLAGS &&
4541 la.la_flags & LUSTRE_ORPHAN_FL))) {
4543 "%s: skip orphan "DFID", %llx/%x: rc = %d\n",
4544 lfsck_lfsck2name(lfsck), PFID(fid),
4545 la.la_valid, la.la_flags, rc);
4550 rc = lfsck_links_read(env, obj, &ldata);
4554 /* -EINVAL means crashed linkEA, should be verified. */
4555 if (rc == -EINVAL) {
4556 rc = lfsck_namespace_trace_update(env, com, fid,
4557 LNTF_CHECK_LINKEA, true);
4559 struct lustre_handle lh = { 0 };
4561 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4562 MDS_INODELOCK_UPDATE |
4563 MDS_INODELOCK_XATTR, LCK_EX);
4565 rc = lfsck_namespace_links_remove(env, com,
4567 lfsck_ibits_unlock(&lh, LCK_EX);
4571 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4574 if (rc && rc != -ENODATA)
4577 if (rc == -ENODATA || unlikely(!ldata.ld_leh->leh_reccount)) {
4578 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4583 linkea_first_entry(&ldata);
4584 while (ldata.ld_lee != NULL) {
4585 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4587 if (!fid_is_sane(pfid)) {
4588 rc = lfsck_namespace_trace_update(env, com, fid,
4589 LNTF_CHECK_PARENT, true);
4590 } else if (!linkea_entry_is_valid(&ldata, cname, pfid)) {
4593 fld_range_set_mdt(range);
4594 rc = fld_server_lookup(env, ss->ss_server_fld,
4595 fid_seq(pfid), range);
4596 if ((rc == -ENOENT) ||
4597 (!rc && range->lsr_index != idx)) {
4605 linkea_next_entry(&ldata);
4608 if ((lu_object_has_agent_entry(&obj->do_lu) && !remote) ||
4609 (!lu_object_has_agent_entry(&obj->do_lu) && remote)) {
4610 rc = lfsck_namespace_trace_update(env, com, fid,
4611 LNTF_CHECK_AGENT_ENTRY, true);
4616 /* Record multiple-linked object. */
4617 if (ldata.ld_leh->leh_reccount > 1) {
4618 rc = lfsck_namespace_trace_update(env, com, fid,
4619 LNTF_CHECK_LINKEA, true);
4625 rc = lfsck_namespace_trace_update(env, com, fid,
4626 LNTF_CHECK_LINKEA, true);
4628 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4633 down_write(&com->lc_sem);
4634 if (S_ISDIR(lfsck_object_type(obj)))
4635 ns->ln_dirs_checked++;
4637 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4638 up_write(&com->lc_sem);
4643 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4644 struct lfsck_component *com,
4645 struct lfsck_assistant_object *lso,
4646 struct lu_dirent *ent, __u16 type)
4648 struct lfsck_assistant_data *lad = com->lc_data;
4649 struct lfsck_instance *lfsck = com->lc_lfsck;
4650 struct lfsck_namespace_req *lnr;
4651 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4652 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4653 struct ptlrpc_thread *athread = &lad->lad_thread;
4654 bool wakeup = false;
4656 wait_event_idle(mthread->t_ctl_waitq,
4657 lad->lad_prefetched < bk->lb_async_windows ||
4658 !thread_is_running(mthread) ||
4659 !thread_is_running(athread));
4661 if (unlikely(!thread_is_running(mthread) ||
4662 !thread_is_running(athread)))
4665 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4668 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, lso, ent, type);
4670 struct lfsck_namespace *ns = com->lc_file_ram;
4672 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4673 return PTR_ERR(lnr);
4676 spin_lock(&lad->lad_lock);
4677 if (lad->lad_assistant_status < 0 ||
4678 unlikely(!thread_is_running(mthread) ||
4679 !thread_is_running(athread))) {
4680 spin_unlock(&lad->lad_lock);
4681 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4682 return lad->lad_assistant_status;
4685 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4686 if (lad->lad_prefetched == 0)
4689 lad->lad_prefetched++;
4690 spin_unlock(&lad->lad_lock);
4692 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4694 down_write(&com->lc_sem);
4695 com->lc_new_checked++;
4696 up_write(&com->lc_sem);
4701 static int lfsck_namespace_post(const struct lu_env *env,
4702 struct lfsck_component *com,
4703 int result, bool init)
4705 struct lfsck_instance *lfsck = com->lc_lfsck;
4706 struct lfsck_namespace *ns = com->lc_file_ram;
4710 lfsck_post_generic(env, com, &result);
4712 down_write(&com->lc_sem);
4713 lfsck_namespace_release_lmv(env, com);
4715 spin_lock(&lfsck->li_lock);
4717 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4719 ns->ln_status = LS_SCANNING_PHASE2;
4720 ns->ln_flags |= LF_SCANNED_ONCE;
4721 ns->ln_flags &= ~LF_UPGRADE;
4722 list_del_init(&com->lc_link_dir);
4723 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4724 } else if (result == 0) {
4725 if (lfsck->li_status != 0)
4726 ns->ln_status = lfsck->li_status;
4728 ns->ln_status = LS_STOPPED;
4729 if (ns->ln_status != LS_PAUSED) {
4730 list_del_init(&com->lc_link_dir);
4731 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4734 ns->ln_status = LS_FAILED;
4735 list_del_init(&com->lc_link_dir);
4736 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4738 spin_unlock(&lfsck->li_lock);
4741 ns->ln_run_time_phase1 += ktime_get_seconds() -
4742 lfsck->li_time_last_checkpoint;
4743 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
4744 ns->ln_items_checked += com->lc_new_checked;
4745 com->lc_new_checked = 0;
4748 rc = lfsck_namespace_store(env, com);
4749 up_write(&com->lc_sem);
4751 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4752 lfsck_lfsck2name(lfsck), rc);
4758 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4761 struct lfsck_instance *lfsck = com->lc_lfsck;
4762 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4763 struct lfsck_namespace *ns = com->lc_file_ram;
4765 down_read(&com->lc_sem);
4766 seq_printf(m, "name: lfsck_namespace\n"
4772 lfsck_status2name(ns->ln_status));
4774 lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4776 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4778 lfsck_time_dump(m, ns->ln_time_last_complete, "last_completed");
4780 lfsck_time_dump(m, ns->ln_time_latest_start, "latest_start");
4782 lfsck_time_dump(m, ns->ln_time_last_checkpoint, "last_checkpoint");
4784 lfsck_pos_dump(m, &ns->ln_pos_latest_start, "latest_start_position");
4786 lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4787 "last_checkpoint_position");
4789 lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4790 "first_failure_position");
4792 if (ns->ln_status == LS_SCANNING_PHASE1) {
4793 struct lfsck_position pos;
4794 time64_t duration = ktime_get_seconds() -
4795 lfsck->li_time_last_checkpoint;
4796 u64 checked = ns->ln_items_checked + com->lc_new_checked;
4797 u64 speed = checked;
4798 u64 new_checked = com->lc_new_checked;
4799 time64_t rtime = ns->ln_run_time_phase1 + duration;
4802 new_checked = div64_s64(new_checked, duration);
4805 speed = div64_s64(speed, rtime);
4807 lfsck_namespace_dump_statistics(m, ns, checked, 0, rtime, 0,
4808 bk->lb_param & LPF_DRYRUN);
4809 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4810 "average_speed_phase2: N/A\n"
4811 "average_speed_total: %llu items/sec\n"
4812 "real_time_speed_phase1: %llu items/sec\n"
4813 "real_time_speed_phase2: N/A\n",
4818 if (likely(lfsck->li_di_oit)) {
4819 const struct dt_it_ops *iops =
4820 &lfsck->li_obj_oit->do_index_ops->dio_it;
4822 /* The low layer otable-based iteration position may NOT
4823 * exactly match the namespace-based directory traversal
4824 * cookie. Generally, it is not a serious issue. But the
4825 * caller should NOT make assumption on that. */
4826 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4827 if (!lfsck->li_current_oit_processed)
4828 pos.lp_oit_cookie--;
4830 spin_lock(&lfsck->li_lock);
4831 if (lfsck->li_di_dir) {
4832 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4833 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4834 fid_zero(&pos.lp_dir_parent);
4835 pos.lp_dir_cookie = 0;
4838 *lfsck_dto2fid(lfsck->li_obj_dir);
4841 fid_zero(&pos.lp_dir_parent);
4842 pos.lp_dir_cookie = 0;
4844 spin_unlock(&lfsck->li_lock);
4846 pos = ns->ln_pos_last_checkpoint;
4849 lfsck_pos_dump(m, &pos, "current_position");
4850 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4851 time64_t duration = ktime_get_seconds() -
4852 com->lc_time_last_checkpoint;
4853 __u64 checked = ns->ln_objs_checked_phase2 +
4854 com->lc_new_checked;
4855 __u64 speed1 = ns->ln_items_checked;
4856 __u64 speed2 = checked;
4857 __u64 speed0 = speed1 + speed2;
4858 __u64 new_checked = com->lc_new_checked;
4859 time64_t rtime = ns->ln_run_time_phase2 + duration;
4860 time64_t time0 = ns->ln_run_time_phase1 + rtime;
4863 new_checked = div64_s64(new_checked, duration);
4865 if (ns->ln_run_time_phase1 != 0)
4866 speed1 = div64_s64(speed1, ns->ln_run_time_phase1);
4867 else if (ns->ln_items_checked != 0)
4871 speed2 = div64_s64(speed2, rtime);
4872 else if (checked != 0)
4876 speed0 = div64_s64(speed0, time0);
4878 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4880 ns->ln_run_time_phase1, rtime,
4881 bk->lb_param & LPF_DRYRUN);
4882 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4883 "average_speed_phase2: %llu objs/sec\n"
4884 "average_speed_total: %llu items/sec\n"
4885 "real_time_speed_phase1: N/A\n"
4886 "real_time_speed_phase2: %llu objs/sec\n"
4887 "current_position: "DFID"\n",
4892 PFID(&ns->ln_fid_latest_scanned_phase2));
4894 __u64 speed1 = ns->ln_items_checked;
4895 __u64 speed2 = ns->ln_objs_checked_phase2;
4896 __u64 speed0 = speed1 + speed2;
4897 time64_t time0 = ns->ln_run_time_phase1 + ns->ln_run_time_phase2;
4899 if (ns->ln_run_time_phase1 != 0)
4900 speed1 = div64_s64(speed1, ns->ln_run_time_phase1);
4901 else if (ns->ln_items_checked != 0)
4904 if (ns->ln_run_time_phase2 != 0)
4905 speed2 = div64_s64(speed2, ns->ln_run_time_phase2);
4906 else if (ns->ln_objs_checked_phase2 != 0)
4910 speed0 = div64_s64(speed0, time0);
4912 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4913 ns->ln_objs_checked_phase2,
4914 ns->ln_run_time_phase1,
4915 ns->ln_run_time_phase2,
4916 bk->lb_param & LPF_DRYRUN);
4917 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4918 "average_speed_phase2: %llu objs/sec\n"
4919 "average_speed_total: %llu items/sec\n"
4920 "real_time_speed_phase1: N/A\n"
4921 "real_time_speed_phase2: N/A\n"
4922 "current_position: N/A\n",
4928 up_read(&com->lc_sem);
4931 static int lfsck_namespace_double_scan(const struct lu_env *env,
4932 struct lfsck_component *com)
4934 struct lfsck_namespace *ns = com->lc_file_ram;
4935 struct lfsck_assistant_data *lad = com->lc_data;
4936 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4937 struct lfsck_tgt_desc *ltd;
4938 struct lfsck_tgt_desc *next;
4941 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4942 if (thread_is_stopped(&lad->lad_thread)) {
4943 LASSERT(list_empty(&lad->lad_req_list));
4944 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4946 spin_lock(<ds->ltd_lock);
4947 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4948 ltd_namespace_phase_list) {
4949 list_del_init(<d->ltd_namespace_phase_list);
4951 spin_unlock(<ds->ltd_lock);
4957 static void lfsck_namespace_data_release(const struct lu_env *env,
4958 struct lfsck_component *com)
4960 struct lfsck_assistant_data *lad = com->lc_data;
4961 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4962 struct lfsck_tgt_desc *ltd;
4963 struct lfsck_tgt_desc *next;
4965 LASSERT(lad != NULL);
4966 LASSERT(thread_is_init(&lad->lad_thread) ||
4967 thread_is_stopped(&lad->lad_thread));
4968 LASSERT(list_empty(&lad->lad_req_list));
4970 com->lc_data = NULL;
4971 lfsck_namespace_release_lmv(env, com);
4973 spin_lock(<ds->ltd_lock);
4974 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4975 ltd_namespace_phase_list) {
4976 list_del_init(<d->ltd_namespace_phase_list);
4978 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4979 ltd_namespace_phase_list) {
4980 list_del_init(<d->ltd_namespace_phase_list);
4982 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4983 ltd_namespace_list) {
4984 list_del_init(<d->ltd_namespace_list);
4986 spin_unlock(<ds->ltd_lock);
4988 if (likely(lad->lad_bitmap != NULL))
4989 CFS_FREE_BITMAP(lad->lad_bitmap);
4994 static void lfsck_namespace_quit(const struct lu_env *env,
4995 struct lfsck_component *com)
4997 struct lfsck_assistant_data *lad = com->lc_data;
4998 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4999 struct lfsck_tgt_desc *ltd;
5000 struct lfsck_tgt_desc *next;
5002 LASSERT(lad != NULL);
5004 lfsck_quit_generic(env, com);
5006 LASSERT(thread_is_init(&lad->lad_thread) ||
5007 thread_is_stopped(&lad->lad_thread));
5008 LASSERT(list_empty(&lad->lad_req_list));
5010 lfsck_namespace_release_lmv(env, com);
5012 spin_lock(<ds->ltd_lock);
5013 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
5014 ltd_namespace_phase_list) {
5015 list_del_init(<d->ltd_namespace_phase_list);
5017 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
5018 ltd_namespace_phase_list) {
5019 list_del_init(<d->ltd_namespace_phase_list);
5021 spin_unlock(<ds->ltd_lock);
5024 static int lfsck_namespace_in_notify(const struct lu_env *env,
5025 struct lfsck_component *com,
5026 struct lfsck_request *lr)
5028 struct lfsck_instance *lfsck = com->lc_lfsck;
5029 struct lfsck_namespace *ns = com->lc_file_ram;
5030 struct lfsck_assistant_data *lad = com->lc_data;
5031 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
5032 struct lfsck_tgt_desc *ltd;
5037 switch (lr->lr_event) {
5038 case LE_SET_LMV_MASTER: {
5039 struct dt_object *obj;
5041 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
5043 RETURN(PTR_ERR(obj));
5045 if (likely(dt_object_exists(obj)))
5046 rc = lfsck_namespace_notify_lmv_master_local(env, com,
5049 lfsck_object_put(env, obj);
5051 RETURN(rc > 0 ? 0 : rc);
5053 case LE_SET_LMV_SLAVE: {
5054 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
5055 ns->ln_striped_shards_repaired++;
5057 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
5058 LNTF_RECHECK_NAME_HASH, true);
5060 RETURN(rc > 0 ? 0 : rc);
5062 case LE_PHASE1_DONE:
5063 case LE_PHASE2_DONE:
5070 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
5071 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
5072 lr->lr_index, lr->lr_status, lr->lr_flags2);
5074 spin_lock(<ds->ltd_lock);
5075 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
5077 spin_unlock(<ds->ltd_lock);
5082 list_del_init(<d->ltd_namespace_phase_list);
5083 switch (lr->lr_event) {
5084 case LE_PHASE1_DONE:
5085 if (lr->lr_status <= 0) {
5086 ltd->ltd_namespace_done = 1;
5087 list_del_init(<d->ltd_namespace_list);
5088 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
5089 "phase1 for namespace LFSCK: rc = %d.\n",
5090 lfsck_lfsck2name(lfsck),
5091 ltd->ltd_index, lr->lr_status);
5092 ns->ln_flags |= LF_INCOMPLETE;
5097 if (lr->lr_flags2 & LF_INCOMPLETE)
5098 ns->ln_flags |= LF_INCOMPLETE;
5100 if (list_empty(<d->ltd_namespace_list))
5101 list_add_tail(<d->ltd_namespace_list,
5102 &lad->lad_mdt_list);
5103 list_add_tail(<d->ltd_namespace_phase_list,
5104 &lad->lad_mdt_phase2_list);
5106 case LE_PHASE2_DONE:
5107 ltd->ltd_namespace_done = 1;
5108 list_del_init(<d->ltd_namespace_list);
5112 ltd->ltd_namespace_done = 1;
5113 list_del_init(<d->ltd_namespace_list);
5114 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
5116 "%s: the peer MDT %x exit namespace LFSCK\n",
5117 lfsck_lfsck2name(lfsck), ltd->ltd_index);
5118 ns->ln_flags |= LF_INCOMPLETE;
5124 spin_unlock(<ds->ltd_lock);
5126 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
5127 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
5129 memset(stop, 0, sizeof(*stop));
5130 stop->ls_status = lr->lr_status;
5131 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
5132 lfsck_stop(env, lfsck->li_bottom, stop);
5133 } else if (lfsck_phase2_next_ready(lad)) {
5134 wake_up_all(&lad->lad_thread.t_ctl_waitq);
5140 static void lfsck_namespace_repaired(struct lfsck_namespace *ns, __u64 *count)
5142 *count += ns->ln_objs_nlink_repaired;
5143 *count += ns->ln_dirent_repaired;
5144 *count += ns->ln_linkea_repaired;
5145 *count += ns->ln_mul_linked_repaired;
5146 *count += ns->ln_unmatched_pairs_repaired;
5147 *count += ns->ln_dangling_repaired;
5148 *count += ns->ln_mul_ref_repaired;
5149 *count += ns->ln_bad_type_repaired;
5150 *count += ns->ln_lost_dirent_repaired;
5151 *count += ns->ln_striped_dirs_disabled;
5152 *count += ns->ln_striped_dirs_repaired;
5153 *count += ns->ln_striped_shards_repaired;
5154 *count += ns->ln_name_hash_repaired;
5155 *count += ns->ln_local_lpf_moved;
5158 static int lfsck_namespace_query_all(const struct lu_env *env,
5159 struct lfsck_component *com,
5160 __u32 *mdts_count, __u64 *repaired)
5162 struct lfsck_namespace *ns = com->lc_file_ram;
5163 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
5164 struct lfsck_tgt_desc *ltd;
5169 rc = lfsck_query_all(env, com);
5173 down_read(<ds->ltd_rw_sem);
5174 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
5175 ltd = lfsck_ltd2tgt(ltds, idx);
5176 LASSERT(ltd != NULL);
5178 mdts_count[ltd->ltd_namespace_status]++;
5179 *repaired += ltd->ltd_namespace_repaired;
5181 up_read(<ds->ltd_rw_sem);
5183 down_read(&com->lc_sem);
5184 mdts_count[ns->ln_status]++;
5185 lfsck_namespace_repaired(ns, repaired);
5186 up_read(&com->lc_sem);
5191 static int lfsck_namespace_query(const struct lu_env *env,
5192 struct lfsck_component *com,
5193 struct lfsck_request *req,
5194 struct lfsck_reply *rep,
5195 struct lfsck_query *que, int idx)
5197 struct lfsck_namespace *ns = com->lc_file_ram;
5201 LASSERT(com->lc_lfsck->li_master);
5203 rc = lfsck_namespace_query_all(env, com,
5204 que->lu_mdts_count[idx],
5205 &que->lu_repaired[idx]);
5207 down_read(&com->lc_sem);
5208 rep->lr_status = ns->ln_status;
5209 if (req->lr_flags & LEF_QUERY_ALL)
5210 lfsck_namespace_repaired(ns, &rep->lr_repaired);
5211 up_read(&com->lc_sem);
5217 static const struct lfsck_operations lfsck_namespace_ops = {
5218 .lfsck_reset = lfsck_namespace_reset,
5219 .lfsck_fail = lfsck_namespace_fail,
5220 .lfsck_close_dir = lfsck_namespace_close_dir,
5221 .lfsck_open_dir = lfsck_namespace_open_dir,
5222 .lfsck_checkpoint = lfsck_namespace_checkpoint,
5223 .lfsck_prep = lfsck_namespace_prep,
5224 .lfsck_exec_oit = lfsck_namespace_exec_oit,
5225 .lfsck_exec_dir = lfsck_namespace_exec_dir,
5226 .lfsck_post = lfsck_namespace_post,
5227 .lfsck_dump = lfsck_namespace_dump,
5228 .lfsck_double_scan = lfsck_namespace_double_scan,
5229 .lfsck_data_release = lfsck_namespace_data_release,
5230 .lfsck_quit = lfsck_namespace_quit,
5231 .lfsck_in_notify = lfsck_namespace_in_notify,
5232 .lfsck_query = lfsck_namespace_query,
5236 * Repair dangling name entry.
5238 * For the name entry with dangling reference, we need to repare the
5239 * inconsistency according to the LFSCK sponsor's requirement:
5241 * 1) Keep the inconsistency there and report the inconsistency case,
5242 * then give the chance to the application to find related issues,
5243 * and the users can make the decision about how to handle it with
5244 * more human knownledge. (by default)
5246 * 2) Re-create the missing MDT-object with the FID information.
5248 * \param[in] env pointer to the thread context
5249 * \param[in] com pointer to the lfsck component
5250 * \param[in] parent pointer to the dir object that contains the dangling
5252 * \param[in] child pointer to the object corresponding to the dangling
5254 * \param[in] lnr pointer to the namespace request that contains the
5255 * name's name, parent object, parent's LMV, and ect.
5257 * \retval positive number if no need to repair
5258 * \retval zero for repaired successfully
5259 * \retval negative error number on failure
5261 int lfsck_namespace_repair_dangling(const struct lu_env *env,
5262 struct lfsck_component *com,
5263 struct dt_object *parent,
5264 struct dt_object *child,
5265 struct lfsck_namespace_req *lnr)
5267 struct lfsck_thread_info *info = lfsck_env_info(env);
5268 struct lu_attr *la = &info->lti_la;
5269 struct dt_allocation_hint *hint = &info->lti_hint;
5270 struct dt_object_format *dof = &info->lti_dof;
5271 struct dt_insert_rec *rec = &info->lti_dt_rec;
5272 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
5273 const struct lu_name *cname;
5274 const struct lu_fid *pfid = lfsck_dto2fid(parent);
5275 const struct lu_fid *cfid = lfsck_dto2fid(child);
5276 struct linkea_data ldata = { NULL };
5277 struct lfsck_lock_handle *llh = &info->lti_llh;
5278 struct lustre_handle rlh = { 0 };
5279 struct lustre_handle clh = { 0 };
5280 struct lu_buf linkea_buf;
5281 struct lu_buf lmv_buf;
5282 struct lfsck_instance *lfsck = com->lc_lfsck;
5283 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5284 struct dt_device *dev = lfsck->li_next;
5285 struct thandle *th = NULL;
5287 __u16 type = lnr->lnr_type;
5291 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5292 if (bk->lb_param & LPF_CREATE_MDTOBJ)
5297 if (!create || bk->lb_param & LPF_DRYRUN)
5300 /* We may need to create the sub-objects of the @child via LOD,
5301 * so make the modification based on lfsck->li_next. */
5303 parent = lfsck_object_locate(dev, parent);
5305 GOTO(log, rc = PTR_ERR(parent));
5307 if (unlikely(!dt_try_as_dir(env, parent)))
5308 GOTO(log, rc = -ENOTDIR);
5310 child = lfsck_object_locate(dev, child);
5312 GOTO(log, rc = PTR_ERR(child));
5314 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
5319 rc = lfsck_lock(env, lfsck, parent, lnr->lnr_name, llh,
5320 MDS_INODELOCK_UPDATE, LCK_PW);
5324 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
5328 if (dt_object_remote(child)) {
5329 rc = lfsck_remote_lookup_lock(env, lfsck, parent, child, &rlh,
5335 rc = lfsck_ibits_lock(env, lfsck, child, &clh,
5336 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
5337 MDS_INODELOCK_XATTR, LCK_EX);
5339 GOTO(unlock_remote_lookup, rc);
5341 /* Set the ctime as zero, then others can know it is created for
5342 * repairing dangling name entry by LFSCK. And if the LFSCK made
5343 * wrong decision and the real MDT-object has been found later,
5344 * then the LFSCK has chance to fix the incosistency properly. */
5345 memset(la, 0, sizeof(*la));
5347 la->la_mode = (type & S_IFMT) | 0700;
5349 la->la_mode = (type & S_IFMT) | 0600;
5350 la->la_valid = LA_TYPE | LA_MODE | LA_CTIME;
5353 * if it's directory, skip do_ah_init() to create a plain directory
5354 * because it may have shards already, which will be inserted back
5355 * later, besides, it may be remote, and creating stripe directory
5356 * remotely is not supported.
5359 child->do_ops->do_ah_init(env, hint, parent, child, type);
5360 else if (S_ISDIR(type))
5361 child->do_ops->do_ah_init(env, hint, NULL, child, type);
5363 memset(dof, 0, sizeof(*dof));
5364 dof->dof_type = dt_mode_to_dft(type);
5365 /* If the target is a regular file, then the LFSCK will only create
5366 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
5367 * OST-objects will be created when write open. */
5369 th = dt_trans_create(env, dev);
5371 GOTO(unlock_child, rc = PTR_ERR(th));
5373 /* 1a. create child. */
5374 rc = dt_declare_create(env, child, la, hint, dof, th);
5378 if (S_ISDIR(type)) {
5379 if (unlikely(!dt_try_as_dir(env, child)))
5380 GOTO(stop, rc = -ENOTDIR);
5382 /* 2a. increase child nlink */
5383 rc = dt_declare_ref_add(env, child, th);
5387 /* 3a. insert dot into child dir */
5388 rec->rec_type = S_IFDIR;
5389 rec->rec_fid = cfid;
5390 rc = dt_declare_insert(env, child,
5391 (const struct dt_rec *)rec,
5392 (const struct dt_key *)dot, th);
5396 /* 4a. insert dotdot into child dir */
5397 rec->rec_fid = pfid;
5398 rc = dt_declare_insert(env, child,
5399 (const struct dt_rec *)rec,
5400 (const struct dt_key *)dotdot, th);
5404 /* 5a. generate slave LMV EA. */
5405 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5408 idx = lfsck_shard_name_to_index(env,
5409 lnr->lnr_name, lnr->lnr_namelen,
5411 if (unlikely(idx < 0))
5412 GOTO(stop, rc = idx);
5414 *lmv2 = lnr->lnr_lmv->ll_lmv;
5415 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
5416 lmv2->lmv_master_mdt_index = idx;
5418 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
5419 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
5420 rc = dt_declare_xattr_set(env, child, &lmv_buf,
5421 XATTR_NAME_LMV, 0, th);
5427 /* 6a. insert linkEA for child */
5428 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
5429 ldata.ld_leh->leh_len);
5430 rc = dt_declare_xattr_set(env, child, &linkea_buf,
5431 XATTR_NAME_LINK, 0, th);
5435 /* 7a. if child is remote, delete and insert to generate local agent */
5436 if (dt_object_remote(child)) {
5437 rc = dt_declare_delete(env, parent,
5438 (const struct dt_key *)lnr->lnr_name,
5443 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
5444 (const struct dt_key *)lnr->lnr_name,
5450 rc = dt_trans_start_local(env, dev, th);
5452 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
5454 dt_write_lock(env, child, 0);
5455 /* 1b. create child */
5456 rc = dt_create(env, child, la, hint, dof, th);
5458 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
5460 if (S_ISDIR(type)) {
5461 /* 2b. increase child nlink */
5462 rc = dt_ref_add(env, child, th);
5466 /* 3b. insert dot into child dir */
5467 rec->rec_type = S_IFDIR;
5468 rec->rec_fid = cfid;
5469 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5470 (const struct dt_key *)dot, th);
5474 /* 4b. insert dotdot into child dir */
5475 rec->rec_fid = pfid;
5476 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5477 (const struct dt_key *)dotdot, th);
5481 /* 5b. generate slave LMV EA. */
5482 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5483 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
5490 /* 6b. insert linkEA for child. */
5491 rc = dt_xattr_set(env, child, &linkea_buf,
5492 XATTR_NAME_LINK, 0, th);
5496 /* 7b. if child is remote, delete and insert to generate local agent */
5497 if (dt_object_remote(child)) {
5498 rc = dt_delete(env, parent,
5499 (const struct dt_key *)lnr->lnr_name, th);
5503 rec->rec_type = type;
5504 rec->rec_fid = cfid;
5505 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
5506 (const struct dt_key *)lnr->lnr_name, th);
5514 dt_write_unlock(env, child);
5517 dt_trans_stop(env, dev, th);
5520 lfsck_ibits_unlock(&clh, LCK_EX);
5521 unlock_remote_lookup:
5522 if (dt_object_remote(child))
5523 lfsck_ibits_unlock(&rlh, LCK_EX);
5526 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
5527 "reference for: parent "DFID", child "DFID", type %u, "
5528 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
5529 PFID(pfid), PFID(cfid), type, cname->ln_name,
5530 create ? "Create the lost MDT-object as required" :
5531 "Keep the MDT-object there by default", rc);
5534 struct lfsck_namespace *ns = com->lc_file_ram;
5536 ns->ln_flags |= LF_INCONSISTENT;
5542 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
5543 struct lfsck_component *com,
5544 struct lfsck_assistant_req *lar)
5546 struct lfsck_thread_info *info = lfsck_env_info(env);
5547 struct lu_attr *la = &info->lti_la;
5548 struct lfsck_instance *lfsck = com->lc_lfsck;
5549 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5550 struct lfsck_namespace *ns = com->lc_file_ram;
5551 struct lfsck_assistant_data *lad = com->lc_data;
5552 struct linkea_data ldata = { NULL };
5553 const struct lu_name *cname;
5554 struct thandle *handle = NULL;
5555 struct lfsck_namespace_req *lnr =
5556 container_of(lar, struct lfsck_namespace_req, lnr_lar);
5557 struct dt_object *dir = NULL;
5558 struct dt_object *obj = NULL;
5559 struct lfsck_assistant_object *lso = lar->lar_parent;
5560 const struct lu_fid *pfid = &lso->lso_fid;
5561 struct dt_device *dev = NULL;
5562 struct lustre_handle lh = { 0 };
5563 bool repaired = false;
5564 bool dtlocked = false;
5568 bool bad_hash = false;
5569 bool bad_linkea = false;
5573 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5580 if (lnr->lnr_attr & LUDA_UPGRADE) {
5581 ns->ln_flags |= LF_UPGRADE;
5582 ns->ln_dirent_repaired++;
5584 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5585 ns->ln_flags |= LF_INCONSISTENT;
5586 ns->ln_dirent_repaired++;
5590 if (unlikely(fid_is_zero(&lnr->lnr_fid) &&
5591 strcmp(lnr->lnr_name, dotdot) == 0)) {
5592 rc = lfsck_namespace_trace_update(env, com, pfid,
5593 LNTF_CHECK_PARENT, true);
5598 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5599 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5600 " for the name entry %.*s under "DFID"\n",
5601 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5602 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5604 if (strcmp(lnr->lnr_name, dotdot) != 0)
5605 /* invalid FID means bad name entry, remove it. */
5606 type = LNIT_BAD_DIRENT;
5608 /* If the parent FID is invalid, we cannot remove
5609 * the ".." entry directly. */
5610 rc = lfsck_namespace_trace_update(env, com, pfid,
5611 LNTF_CHECK_PARENT, true);
5616 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5617 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5622 if (fid_seq_is_dot(fid_seq(&lnr->lnr_fid)))
5625 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5626 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5631 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5633 GOTO(out, rc = idx);
5635 if (idx == lfsck_dev_idx(lfsck)) {
5636 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5639 dev = lfsck->li_bottom;
5641 struct lfsck_tgt_desc *ltd;
5643 /* Usually, some local filesystem consistency verification
5644 * tools can guarantee the local namespace tree consistenct.
5645 * So the LFSCK will only verify the remote directory. */
5646 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5647 rc = lfsck_namespace_trace_update(env, com, pfid,
5648 LNTF_CHECK_PARENT, true);
5653 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
5654 if (unlikely(ltd == NULL)) {
5655 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5656 "did not join the namespace LFSCK\n",
5657 lfsck_lfsck2name(lfsck), idx);
5658 lfsck_lad_set_bitmap(env, com, idx);
5660 GOTO(out, rc = -ENODEV);
5666 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5668 GOTO(out, rc = PTR_ERR(obj));
5670 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5671 if (dt_object_exists(obj) == 0) {
5675 dir = lfsck_assistant_object_load(env, lfsck, lso);
5679 GOTO(trace, rc == -ENOENT ? 0 : rc);
5683 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5685 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5686 lnr->lnr_name, lnr->lnr_namelen)) {
5687 type = LNIT_BAD_DIRENT;
5692 type = LNIT_DANGLING;
5693 rc = lfsck_namespace_repair_dangling(env, com, dir,
5702 if (!(bk->lb_param & LPF_DRYRUN) && lad->lad_advance_lock) {
5705 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5706 MDS_INODELOCK_UPDATE |
5707 MDS_INODELOCK_XATTR, LCK_EX);
5711 handle = dt_trans_create(env, dev);
5713 GOTO(out, rc = PTR_ERR(handle));
5715 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5719 rc = dt_trans_start_local(env, dev, handle);
5723 dt_write_lock(env, obj, 0);
5727 rc = lfsck_links_read(env, obj, &ldata);
5728 if (unlikely(rc == -ENOENT)) {
5729 if (handle != NULL) {
5730 dt_write_unlock(env, obj);
5733 dt_trans_stop(env, dev, handle);
5736 lfsck_ibits_unlock(&lh, LCK_EX);
5739 /* It may happen when the remote object has been removed,
5740 * but the local MDT is not aware of that. */
5742 } else if (rc == 0) {
5743 count = ldata.ld_leh->leh_reccount;
5744 rc = linkea_links_find(&ldata, cname, pfid);
5746 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5747 if ((lfsck_object_type(obj) & S_IFMT) !=
5749 ns->ln_flags |= LF_INCONSISTENT;
5750 type = LNIT_BAD_TYPE;
5756 /* If the name entry hash does not match the slave striped
5757 * directory, and the name entry does not match also, then
5758 * it is quite possible that name entry is corrupted. */
5759 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5760 lnr->lnr_name, lnr->lnr_namelen)) {
5761 ns->ln_flags |= LF_INCONSISTENT;
5762 type = LNIT_BAD_DIRENT;
5767 /* If the file type stored in the name entry does not match
5768 * the file type claimed by the object, and the object does
5769 * not recognize the name entry, then it is quite possible
5770 * that the name entry is corrupted. */
5771 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5772 ns->ln_flags |= LF_INCONSISTENT;
5773 type = LNIT_BAD_DIRENT;
5778 /* For sub-dir object, we cannot make sure whether the sub-dir
5779 * back references the parent via ".." name entry correctly or
5780 * not in the LFSCK first-stage scanning. It may be that the
5781 * (remote) sub-dir ".." name entry has no parent FID after
5782 * file-level backup/restore and its linkEA may be wrong.
5783 * So under such case, we should replace the linkEA according
5784 * to current name entry. But this needs to be done during the
5785 * LFSCK second-stage scanning. The LFSCK will record the name
5786 * entry for further possible using. */
5790 } else if (unlikely(rc == -EINVAL)) {
5791 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5792 type = LNIT_BAD_TYPE;
5795 /* The magic crashed, we are not sure whether there are more
5796 * corrupt data in the linkea, so remove all linkea entries. */
5800 } else if (rc == -ENODATA) {
5801 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5802 type = LNIT_BAD_TYPE;
5809 if (bk->lb_param & LPF_DRYRUN) {
5811 ns->ln_flags |= LF_UPGRADE;
5813 ns->ln_flags |= LF_INCONSISTENT;
5814 ns->ln_linkea_repaired++;
5820 if (!lustre_handle_is_used(&lh)) {
5828 LASSERT(handle != NULL);
5831 dir = lfsck_assistant_object_load(env, lfsck, lso);
5835 GOTO(stop, rc == -ENOENT ? 0 : rc);
5839 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5844 if (!remove && newdata)
5845 ns->ln_flags |= LF_UPGRADE;
5846 else if (remove || !(ns->ln_flags & LF_UPGRADE))
5847 ns->ln_flags |= LF_INCONSISTENT;
5852 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle);
5853 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
5858 rc = linkea_data_new(&ldata,
5859 &lfsck_env_info(env)->lti_linkea_buf);
5864 rc = linkea_add_buf(&ldata, cname, pfid);
5866 rc = lfsck_links_write(env, obj, &ldata, handle);
5870 count = ldata.ld_leh->leh_reccount;
5871 if (!S_ISDIR(lfsck_object_type(obj)) ||
5872 !dt_object_remote(obj)) {
5873 ns->ln_linkea_repaired++;
5883 dt_write_unlock(env, obj);
5885 if (handle != NULL && !IS_ERR(handle))
5886 dt_trans_stop(env, dev, handle);
5889 lfsck_ibits_unlock(&lh, LCK_EX);
5891 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5892 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5893 lnr->lnr_name, lnr->lnr_namelen) &&
5894 type != LNIT_BAD_DIRENT) {
5895 ns->ln_flags |= LF_INCONSISTENT;
5899 dir = lfsck_assistant_object_load(env, lfsck, lso);
5903 GOTO(trace, rc == -ENOENT ? 0 : rc);
5907 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5908 lnr->lnr_lmv, lnr->lnr_name);
5914 if (type != LNIT_NONE && dir == NULL) {
5915 dir = lfsck_assistant_object_load(env, lfsck, lso);
5919 GOTO(trace, rc == -ENOENT ? 0 : rc);
5926 rc = lfsck_namespace_repair_dirent(env, com, dir,
5927 obj, lnr->lnr_name, lnr->lnr_name,
5928 lnr->lnr_type, true, false);
5932 case LNIT_BAD_DIRENT:
5934 /* XXX: This is a bad dirent, we do not know whether
5935 * the original name entry reference a regular
5936 * file or a directory, then keep the parent's
5937 * nlink count unchanged here. */
5938 rc = lfsck_namespace_repair_dirent(env, com, dir,
5939 obj, lnr->lnr_name, lnr->lnr_name,
5940 lnr->lnr_type, false, false);
5948 if (obj != NULL && count == 1 &&
5949 S_ISREG(lfsck_object_type(obj)))
5950 dt_attr_get(env, obj, la);
5954 down_write(&com->lc_sem);
5956 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5957 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5958 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid), PFID(pfid),
5959 lnr->lnr_namelen, lnr->lnr_name, rc);
5961 lfsck_namespace_record_failure(env, lfsck, ns);
5962 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5963 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5964 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5965 dev != NULL && dev != lfsck->li_bottom)
5966 lfsck_lad_set_bitmap(env, com, idx);
5968 if (!(bk->lb_param & LPF_FAILOUT))
5972 ns->ln_items_repaired++;
5974 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5975 "repaired the entry: "DFID", parent "DFID
5976 ", name %.*s, type %d\n",
5977 lfsck_lfsck2name(lfsck),
5978 PFID(&lnr->lnr_fid), PFID(pfid),
5979 lnr->lnr_namelen, lnr->lnr_name, type);
5983 ns->ln_dangling_repaired++;
5986 ns->ln_bad_type_repaired++;
5988 case LNIT_BAD_DIRENT:
5989 ns->ln_dirent_repaired++;
5995 if (bk->lb_param & LPF_DRYRUN &&
5996 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5997 lfsck_pos_fill(env, lfsck,
5998 &ns->ln_pos_first_inconsistent,
6003 ns->ln_name_hash_repaired++;
6005 /* Not count repeatedly. */
6007 ns->ln_items_repaired++;
6009 CDEBUG(D_LFSCK, "%s: namespace LFSCK "
6010 "assistant repaired the entry: "
6013 lfsck_lfsck2name(lfsck),
6014 PFID(&lnr->lnr_fid), PFID(pfid),
6015 lnr->lnr_namelen, lnr->lnr_name);
6018 if (bk->lb_param & LPF_DRYRUN &&
6019 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
6020 lfsck_pos_fill(env, lfsck,
6021 &ns->ln_pos_first_inconsistent,
6028 if (count > 1 || la->la_nlink > 1)
6029 ns->ln_mul_linked_checked++;
6031 up_write(&com->lc_sem);
6033 if (obj != NULL && !IS_ERR(obj))
6034 lfsck_object_put(env, obj);
6036 if (dir != NULL && !IS_ERR(dir))
6037 lfsck_object_put(env, dir);
6039 lad->lad_advance_lock = bad_linkea;
6045 * Handle one orphan under the backend /lost+found directory
6047 * Insert the orphan FID into the namespace LFSCK trace file for further
6048 * processing (via the subsequent namespace LFSCK second-stage scanning).
6049 * At the same time, remove the orphan name entry from backend /lost+found
6050 * directory. There is an interval between the orphan name entry removed
6051 * from the backend /lost+found directory and the orphan FID in the LFSCK
6052 * trace file handled. In such interval, the LFSCK can be reset, then
6053 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
6054 * To guarantee that the orphans can be found when LFSCK run next time
6055 * without e2fsck again, when remove the orphan name entry, the LFSCK
6056 * will set the orphan's ctime attribute as 1. Since normal applications
6057 * cannot change the object's ctime attribute as 1. Then when LFSCK run
6058 * next time, it can record the object (that ctime is 1) in the namespace
6059 * LFSCK trace file during the first-stage scanning.
6061 * \param[in] env pointer to the thread context
6062 * \param[in] com pointer to the lfsck component
6063 * \param[in] parent pointer to the object for the backend /lost+found
6064 * \param[in] ent pointer to the name entry for the target under the
6065 * backend /lost+found
6067 * \retval positive for repaired
6068 * \retval 0 if needs to repair nothing
6069 * \retval negative error number on failure
6071 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
6072 struct lfsck_component *com,
6073 struct dt_object *parent,
6074 struct lu_dirent *ent)
6076 struct lfsck_thread_info *info = lfsck_env_info(env);
6077 struct lu_fid *key = &info->lti_fid;
6078 struct lu_attr *la = &info->lti_la;
6079 struct lfsck_instance *lfsck = com->lc_lfsck;
6080 struct dt_object *obj;
6081 struct dt_device *dev = lfsck->li_bottom;
6082 struct dt_object *child = NULL;
6083 struct thandle *th = NULL;
6090 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
6092 RETURN(PTR_ERR(child));
6094 LASSERT(dt_object_exists(child));
6095 LASSERT(!dt_object_remote(child));
6097 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
6098 obj = com->lc_sub_trace_objs[idx].lsto_obj;
6099 fid_cpu_to_be(key, &ent->lde_fid);
6100 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
6101 (const struct dt_key *)key);
6104 flags |= LNTF_CHECK_ORPHAN;
6105 } else if (rc == -ENOENT) {
6106 flags = LNTF_CHECK_ORPHAN;
6111 th = dt_trans_create(env, dev);
6113 GOTO(out, rc = PTR_ERR(th));
6115 /* a1. remove name entry from backend /lost+found */
6116 rc = dt_declare_delete(env, parent,
6117 (const struct dt_key *)ent->lde_name, th);
6121 if (S_ISDIR(lfsck_object_type(child))) {
6122 /* a2. decrease parent's nlink */
6123 rc = dt_declare_ref_del(env, parent, th);
6129 /* a3. remove child's FID from the LFSCK trace file. */
6130 rc = dt_declare_delete(env, obj,
6131 (const struct dt_key *)key, th);
6135 /* a4. set child's ctime as 1 */
6136 memset(la, 0, sizeof(*la));
6138 la->la_valid = LA_CTIME;
6139 rc = dt_declare_attr_set(env, child, la, th);
6144 /* a5. insert child's FID into the LFSCK trace file. */
6145 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
6146 (const struct dt_key *)key, th);
6150 rc = dt_trans_start_local(env, dev, th);
6154 /* b1. remove name entry from backend /lost+found */
6155 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th);
6159 if (S_ISDIR(lfsck_object_type(child))) {
6160 /* b2. decrease parent's nlink */
6161 dt_write_lock(env, parent, 0);
6162 rc = dt_ref_del(env, parent, th);
6163 dt_write_unlock(env, parent);
6169 /* a3. remove child's FID from the LFSCK trace file. */
6170 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
6174 /* b4. set child's ctime as 1 */
6175 rc = dt_attr_set(env, child, la, th);
6180 /* b5. insert child's FID into the LFSCK trace file. */
6181 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
6182 (const struct dt_key *)key, th);
6184 GOTO(stop, rc = (rc == 0 ? 1 : rc));
6187 dt_trans_stop(env, dev, th);
6190 lfsck_object_put(env, child);
6196 * Handle orphans under the backend /lost+found directory
6198 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
6199 * and put them under the backend /lost+found directory that is invisible
6200 * to client. The LFSCK will scan such directory, for the original client
6201 * visible orphans, add their fids into the namespace LFSCK trace file,
6202 * then the subsenquent namespace LFSCK second-stage scanning can handle
6203 * them as other objects to be double scanned: either move back to normal
6204 * namespace, or to the global visible orphan directory:
6205 * /ROOT/.lustre/lost+found/MDTxxxx/
6207 * \param[in] env pointer to the thread context
6208 * \param[in] com pointer to the lfsck component
6210 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
6211 struct lfsck_component *com)
6213 struct lfsck_thread_info *info = lfsck_env_info(env);
6214 struct lu_dirent *ent =
6215 (struct lu_dirent *)info->lti_key;
6216 struct lu_seq_range *range = &info->lti_range;
6217 struct lfsck_instance *lfsck = com->lc_lfsck;
6218 struct ptlrpc_thread *thread = &lfsck->li_thread;
6219 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6220 struct lfsck_namespace *ns = com->lc_file_ram;
6221 struct dt_object *parent;
6222 const struct dt_it_ops *iops;
6224 struct seq_server_site *ss = lfsck_dev_site(lfsck);
6226 __u32 idx = lfsck_dev_idx(lfsck);
6231 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
6232 &LU_BACKEND_LPF_FID);
6233 if (IS_ERR(parent)) {
6234 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
6235 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
6239 /* It is normal that the /lost+found does not exist for ZFS backend. */
6240 if (!dt_object_exists(parent))
6243 if (unlikely(!dt_try_as_dir(env, parent)))
6244 GOTO(out, rc = -ENOTDIR);
6246 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
6247 lfsck_lfsck2name(lfsck));
6249 com->lc_new_scanned = 0;
6250 iops = &parent->do_index_ops->dio_it;
6251 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE);
6253 GOTO(out, rc = PTR_ERR(di));
6255 rc = iops->load(env, di, 0);
6257 rc = iops->next(env, di);
6262 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6263 unlikely(!thread_is_running(thread)))
6266 rc = iops->rec(env, di, (struct dt_rec *)ent,
6267 LUDA_64BITHASH | LUDA_TYPE);
6269 rc = lfsck_unpack_ent(ent, &cookie, &type);
6271 if (unlikely(rc != 0)) {
6272 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
6273 "/lost+found: rc = %d\n",
6274 lfsck_lfsck2name(lfsck), rc);
6279 /* skip dot and dotdot entries */
6280 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
6283 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
6286 if (fid_is_norm(&ent->lde_fid)) {
6287 fld_range_set_mdt(range);
6288 rc = fld_local_lookup(env, ss->ss_server_fld,
6289 fid_seq(&ent->lde_fid), range);
6292 } else if (idx != 0) {
6293 /* If the returned FID is IGIF, then there are three
6296 * 1) The object is upgraded from old Lustre-1.8 with
6297 * IGIF assigned to such object.
6298 * 2) The object is a backend local object and is
6299 * invisible to client.
6300 * 3) The object lost its LMV EA, and since there is
6301 * no FID-in-dirent for the orphan in the backend
6302 * /lost+found directory, then the low layer will
6303 * return IGIF for such object.
6305 * For MDTx (x != 0), it is either case 2) or case 3),
6306 * but from the LFSCK view, they are indistinguishable.
6307 * To be safe, the LFSCK will keep it there and report
6308 * some message, then the adminstrator can handle that
6311 * For MDT0, it is more possible the case 1). The LFSCK
6312 * will handle the orphan as an upgraded object. */
6313 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
6314 "in the backend /lost+found on the MDT %04x, "
6315 "to be safe, skip it.\n",
6316 lfsck_lfsck2name(lfsck), ent->lde_namelen,
6317 ent->lde_name, PFID(&ent->lde_fid), idx);
6321 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
6324 down_write(&com->lc_sem);
6325 com->lc_new_scanned++;
6326 ns->ln_local_lpf_scanned++;
6328 ns->ln_local_lpf_moved++;
6330 ns->ln_local_lpf_skipped++;
6332 ns->ln_local_lpf_failed++;
6333 up_write(&com->lc_sem);
6335 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6339 lfsck_control_speed_by_self(com);
6340 if (unlikely(!thread_is_running(thread))) {
6345 rc = iops->next(env, di);
6349 iops->fini(env, di);
6354 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
6355 lfsck_lfsck2name(lfsck), rc);
6357 lfsck_object_put(env, parent);
6361 * Rescan the striped directory after the master LMV EA reset.
6363 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
6364 * the namespace LFSCK engine scan the striped directory for the first time,
6365 * it will be regarded as a normal directory. As the LFSCK processing, some
6366 * other LFSCK instance on other MDT will find the shard of this striped dir,
6367 * and find that the master MDT-object of the striped directory lost its LMV
6368 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
6369 * notify the LFSCK instance on this MDT to rescan the striped directory.
6371 * \param[in] env pointer to the thread context
6372 * \param[in] com pointer to the lfsck component
6373 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
6376 * \retval positive number for success
6377 * \retval 0 for LFSCK stopped/paused
6378 * \retval negative error number on failure
6380 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
6381 struct lfsck_component *com,
6382 struct lfsck_lmv_unit *llu)
6384 struct lfsck_thread_info *info = lfsck_env_info(env);
6385 struct lfsck_instance *lfsck = com->lc_lfsck;
6386 struct lfsck_assistant_data *lad = com->lc_data;
6387 struct dt_object *dir;
6388 const struct dt_it_ops *iops;
6390 struct lu_dirent *ent =
6391 (struct lu_dirent *)info->lti_key;
6392 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6393 struct ptlrpc_thread *thread = &lfsck->li_thread;
6394 struct lfsck_assistant_object *lso = NULL;
6395 struct lfsck_namespace_req *lnr;
6396 struct lfsck_assistant_req *lar;
6401 LASSERT(list_empty(&lad->lad_req_list));
6403 lfsck->li_lmv = &llu->llu_lmv;
6404 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
6405 rc = lfsck_open_dir(env, lfsck, 0);
6409 dir = lfsck->li_obj_dir;
6410 di = lfsck->li_di_dir;
6411 iops = &dir->do_index_ops->dio_it;
6413 rc = iops->rec(env, di, (struct dt_rec *)ent,
6414 lfsck->li_args_dir);
6416 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
6420 if (bk->lb_param & LPF_FAILOUT)
6426 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
6430 lso = lfsck_assistant_object_init(env,
6431 lfsck_dto2fid(dir), NULL,
6432 lfsck->li_pos_current.lp_oit_cookie, true);
6434 if (bk->lb_param & LPF_FAILOUT)
6435 GOTO(out, rc = PTR_ERR(lso));
6442 lnr = lfsck_namespace_assistant_req_init(lfsck, lso, ent, type);
6444 if (bk->lb_param & LPF_FAILOUT)
6445 GOTO(out, rc = PTR_ERR(lnr));
6450 lar = &lnr->lnr_lar;
6451 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6452 lfsck_namespace_assistant_req_fini(env, lar);
6453 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
6456 if (unlikely(!thread_is_running(thread)))
6460 rc = iops->next(env, di);
6464 if (lso != NULL && !IS_ERR(lso))
6465 lfsck_assistant_object_put(env, lso);
6467 lfsck_close_dir(env, lfsck, rc);
6471 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
6472 if (list_empty(&lad->lad_req_list))
6475 spin_lock(&lad->lad_lock);
6476 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
6478 list_del_init(&lar->lar_list);
6479 spin_unlock(&lad->lad_lock);
6481 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6482 lfsck_namespace_assistant_req_fini(env, lar);
6484 RETURN(rc == 0 ? 1 : rc);
6488 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
6489 struct lfsck_component *com,
6490 struct dt_object *obj, bool first)
6492 struct lfsck_instance *lfsck = com->lc_lfsck;
6493 struct ptlrpc_thread *thread = &lfsck->li_thread;
6494 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6495 struct lfsck_namespace *ns = com->lc_file_ram;
6496 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
6497 struct dt_object *target;
6505 di = iops->init(env, obj, 0);
6507 RETURN(PTR_ERR(di));
6510 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
6513 rc = iops->get(env, di, (const struct dt_key *)&fid);
6518 /* The start one either has been processed or does not exist,
6520 rc = iops->next(env, di);
6526 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6527 unlikely(!thread_is_running(thread)))
6530 key = iops->key(env, di);
6539 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
6540 if (!fid_is_sane(&fid)) {
6545 target = lfsck_object_find_bottom(env, lfsck, &fid);
6546 if (IS_ERR(target)) {
6547 rc = PTR_ERR(target);
6551 if (dt_object_exists(target)) {
6552 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
6554 rc = lfsck_namespace_double_scan_one(env, com,
6561 lfsck_object_put(env, target);
6564 down_write(&com->lc_sem);
6565 com->lc_new_checked++;
6566 com->lc_new_scanned++;
6568 ns->ln_fid_latest_scanned_phase2 = fid;
6571 ns->ln_objs_repaired_phase2++;
6573 ns->ln_objs_failed_phase2++;
6574 up_write(&com->lc_sem);
6576 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6579 if (unlikely(com->lc_time_next_checkpoint <=
6580 ktime_get_seconds()) &&
6581 com->lc_new_checked != 0) {
6582 down_write(&com->lc_sem);
6583 ns->ln_run_time_phase2 += ktime_get_seconds() -
6584 com->lc_time_last_checkpoint;
6585 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
6586 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6587 com->lc_new_checked = 0;
6588 lfsck_namespace_store(env, com);
6589 up_write(&com->lc_sem);
6591 com->lc_time_last_checkpoint = ktime_get_seconds();
6592 com->lc_time_next_checkpoint =
6593 com->lc_time_last_checkpoint +
6594 LFSCK_CHECKPOINT_INTERVAL;
6597 lfsck_control_speed_by_self(com);
6598 if (unlikely(!thread_is_running(thread)))
6601 rc = iops->next(env, di);
6610 iops->fini(env, di);
6615 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
6616 struct lfsck_component *com)
6618 struct lfsck_instance *lfsck = com->lc_lfsck;
6619 struct lfsck_namespace *ns = com->lc_file_ram;
6624 while (!list_empty(&lfsck->li_list_lmv)) {
6625 struct lfsck_lmv_unit *llu;
6627 spin_lock(&lfsck->li_lock);
6628 llu = list_entry(lfsck->li_list_lmv.next,
6629 struct lfsck_lmv_unit, llu_link);
6630 list_del_init(&llu->llu_link);
6631 spin_unlock(&lfsck->li_lock);
6633 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6638 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6639 lfsck_lfsck2name(lfsck));
6641 lfsck_namespace_scan_local_lpf(env, com);
6643 com->lc_new_checked = 0;
6644 com->lc_new_scanned = 0;
6645 com->lc_time_last_checkpoint = ktime_get_seconds();
6646 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6647 LFSCK_CHECKPOINT_INTERVAL;
6649 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6650 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6651 com->lc_sub_trace_objs[i].lsto_obj, true);
6652 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6653 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6654 com->lc_sub_trace_objs[i].lsto_obj, false);
6656 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6657 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6662 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6663 struct lfsck_component *com,
6664 struct lfsck_position *pos)
6666 struct lfsck_assistant_data *lad = com->lc_data;
6667 struct lfsck_namespace_req *lnr;
6669 if (((struct lfsck_namespace *)(com->lc_file_ram))->ln_status !=
6673 if (list_empty(&lad->lad_req_list))
6676 lnr = list_entry(lad->lad_req_list.next,
6677 struct lfsck_namespace_req,
6679 pos->lp_oit_cookie = lnr->lnr_lar.lar_parent->lso_oit_cookie;
6680 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6681 pos->lp_dir_parent = lnr->lnr_lar.lar_parent->lso_fid;
6684 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6685 struct lfsck_component *com,
6688 struct lfsck_instance *lfsck = com->lc_lfsck;
6689 struct lfsck_namespace *ns = com->lc_file_ram;
6691 down_write(&com->lc_sem);
6692 ns->ln_run_time_phase2 += ktime_get_seconds() -
6693 com->lc_time_last_checkpoint;
6694 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
6695 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6696 com->lc_new_checked = 0;
6699 if (ns->ln_flags & LF_INCOMPLETE)
6700 ns->ln_status = LS_PARTIAL;
6702 ns->ln_status = LS_COMPLETED;
6703 ns->ln_flags &= ~LF_SCANNED_ONCE;
6704 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6705 ns->ln_flags &= ~LF_INCONSISTENT;
6706 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6707 ns->ln_success_count++;
6708 } else if (rc == 0) {
6709 if (lfsck->li_status != 0)
6710 ns->ln_status = lfsck->li_status;
6712 ns->ln_status = LS_STOPPED;
6714 ns->ln_status = LS_FAILED;
6717 rc = lfsck_namespace_store(env, com);
6718 up_write(&com->lc_sem);
6724 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6725 struct ptlrpc_request *req,
6729 struct lfsck_async_interpret_args *laia = args;
6730 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6732 ltd->ltd_synced_failures = 1;
6739 * Notify remote LFSCK instances about former failures.
6741 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6742 * some LFSCK verification requests (maybe because of network issues or the MDT
6743 * itself trouble). During the respond gap the MDT may missed some name entries
6744 * verification, then the MDT cannot know whether related MDT-objects have been
6745 * referenced by related name entries or not, then in the second-stage scanning,
6746 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6747 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6748 * name entry for repairing the orphan.
6750 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6751 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6752 * have ever missed some name entries verification and should skip the handling
6753 * for orphan MDT-objects.
6755 * \param[in] env pointer to the thread context
6756 * \param[in] com pointer to the lfsck component
6757 * \param[in] lr pointer to the lfsck request
6759 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6760 struct lfsck_component *com,
6761 struct lfsck_request *lr)
6763 struct lfsck_async_interpret_args *laia =
6764 &lfsck_env_info(env)->lti_laia2;
6765 struct lfsck_assistant_data *lad = com->lc_data;
6766 struct lfsck_namespace *ns = com->lc_file_ram;
6767 struct lfsck_instance *lfsck = com->lc_lfsck;
6768 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6769 struct lfsck_tgt_desc *ltd;
6770 struct ptlrpc_request_set *set;
6775 if (!test_bit(LAD_INCOMPLETE, &lad->lad_flags))
6778 set = ptlrpc_prep_set();
6780 GOTO(out, rc = -ENOMEM);
6782 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6783 memset(laia, 0, sizeof(*laia));
6784 lad->lad_touch_gen++;
6786 down_read(<ds->ltd_rw_sem);
6787 cfs_foreach_bit(lad->lad_bitmap, idx) {
6788 ltd = lfsck_ltd2tgt(ltds, idx);
6792 laia->laia_ltd = ltd;
6793 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6794 lfsck_namespace_assistant_sync_failures_interpret,
6795 laia, LFSCK_NOTIFY);
6797 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6798 "to sync failure with MDT %x: rc = %d\n",
6799 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6801 up_read(<ds->ltd_rw_sem);
6803 rc = ptlrpc_set_wait(env, set);
6804 ptlrpc_set_destroy(set);
6810 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6811 "to sync failure with MDTs, and related MDTs "
6812 "may handle orphan improperly: rc = %d\n",
6813 lfsck_lfsck2name(lfsck), rc);
6818 const struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6819 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6820 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6821 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6822 .la_double_scan_result = lfsck_namespace_double_scan_result,
6823 .la_req_fini = lfsck_namespace_assistant_req_fini,
6824 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6828 * Verify the specified linkEA entry for the given directory object.
6829 * If the object has no such linkEA entry or it has more other linkEA
6830 * entries, then re-generate the linkEA with the given information.
6832 * \param[in] env pointer to the thread context
6833 * \param[in] obj pointer to the dt_object to be handled
6834 * \param[in] cname the name for the child in the parent directory
6835 * \param[in] pfid the parent directory's FID for the linkEA
6837 * \retval 0 for success
6838 * \retval negative error number on failure
6840 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6841 const struct lu_name *cname, const struct lu_fid *pfid)
6843 struct dt_device *dev = lfsck_obj2dev(obj);
6844 struct linkea_data ldata = { NULL };
6845 struct lu_buf linkea_buf;
6848 int fl = LU_XATTR_CREATE;
6852 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6854 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6855 if (rc == -ENODATA) {
6857 } else if (rc == 0) {
6858 fl = LU_XATTR_REPLACE;
6859 if (ldata.ld_leh->leh_reccount != 1) {
6862 rc = linkea_links_find(&ldata, cname, pfid);
6871 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
6876 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6877 ldata.ld_leh->leh_len);
6878 th = dt_trans_create(env, dev);
6880 RETURN(PTR_ERR(th));
6882 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6883 XATTR_NAME_LINK, fl, th);
6887 rc = dt_trans_start_local(env, dev, th);
6891 dt_write_lock(env, obj, 0);
6892 rc = dt_xattr_set(env, obj, &linkea_buf,
6893 XATTR_NAME_LINK, fl, th);
6894 dt_write_unlock(env, obj);
6899 dt_trans_stop(env, dev, th);
6904 * Get the name and parent directory's FID from the first linkEA entry.
6906 * \param[in] env pointer to the thread context
6907 * \param[in] obj pointer to the object which get linkEA from
6908 * \param[out] name pointer to the buffer to hold the name
6909 * in the first linkEA entry
6910 * \param[out] pfid pointer to the buffer to hold the parent
6911 * directory's FID in the first linkEA entry
6913 * \retval 0 for success
6914 * \retval negative error number on failure
6916 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6917 char *name, struct lu_fid *pfid)
6919 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6920 struct linkea_data ldata = { NULL };
6923 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6927 linkea_first_entry(&ldata);
6928 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6929 if (!linkea_entry_is_valid(&ldata, cname, pfid))
6932 /* To guarantee the 'name' is terminated with '0'. */
6933 memcpy(name, cname->ln_name, cname->ln_namelen);
6934 name[cname->ln_namelen] = 0;
6940 * Update the object's name entry with the given FID.
6942 * \param[in] env pointer to the thread context
6943 * \param[in] lfsck pointer to the lfsck instance
6944 * \param[in] dir pointer to the directory that holds
6946 * \param[in] name the name for the entry to be updated
6947 * \param[in] fid the new FID for the name entry referenced
6948 * \param[in] type the type for the name entry to be updated
6950 * \retval 0 for success
6951 * \retval negative error number on failure
6953 int lfsck_update_name_entry(const struct lu_env *env,
6954 struct lfsck_instance *lfsck,
6955 struct dt_object *dir, const char *name,
6956 const struct lu_fid *fid, __u32 type)
6958 struct lfsck_thread_info *info = lfsck_env_info(env);
6959 struct dt_insert_rec *rec = &info->lti_dt_rec;
6960 struct lfsck_lock_handle *llh = &info->lti_llh;
6961 struct dt_device *dev = lfsck_obj2dev(dir);
6967 rc = lfsck_lock(env, lfsck, dir, name, llh,
6968 MDS_INODELOCK_UPDATE, LCK_PW);
6972 th = dt_trans_create(env, dev);
6974 GOTO(unlock, rc = PTR_ERR(th));
6976 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6980 rec->rec_type = type;
6982 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6983 (const struct dt_key *)name, th);
6987 rc = dt_declare_ref_add(env, dir, th);
6991 rc = dt_trans_start_local(env, dev, th);
6995 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
6996 if (rc == -ENOENT) {
7004 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
7005 (const struct dt_key *)name, th);
7006 if (rc == 0 && S_ISDIR(type) && !exists) {
7007 dt_write_lock(env, dir, 0);
7008 rc = dt_ref_add(env, dir, th);
7009 dt_write_unlock(env, dir);
7015 dt_trans_stop(env, dev, th);
7019 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
7020 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
7021 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
7026 int lfsck_namespace_setup(const struct lu_env *env,
7027 struct lfsck_instance *lfsck)
7029 struct lfsck_component *com;
7030 struct lfsck_namespace *ns;
7031 struct dt_object *root = NULL;
7032 struct dt_object *obj;
7037 LASSERT(lfsck->li_master);
7043 INIT_LIST_HEAD(&com->lc_link);
7044 INIT_LIST_HEAD(&com->lc_link_dir);
7045 init_rwsem(&com->lc_sem);
7046 atomic_set(&com->lc_ref, 1);
7047 com->lc_lfsck = lfsck;
7048 com->lc_type = LFSCK_TYPE_NAMESPACE;
7049 com->lc_ops = &lfsck_namespace_ops;
7050 com->lc_data = lfsck_assistant_data_init(
7051 &lfsck_namespace_assistant_ops,
7053 if (com->lc_data == NULL)
7054 GOTO(out, rc = -ENOMEM);
7056 com->lc_file_size = sizeof(struct lfsck_namespace);
7057 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
7058 if (com->lc_file_ram == NULL)
7059 GOTO(out, rc = -ENOMEM);
7061 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
7062 if (com->lc_file_disk == NULL)
7063 GOTO(out, rc = -ENOMEM);
7065 for (i = 0; i < LFSCK_STF_COUNT; i++)
7066 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
7068 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
7070 GOTO(out, rc = PTR_ERR(root));
7072 if (unlikely(!dt_try_as_dir(env, root)))
7073 GOTO(out, rc = -ENOTDIR);
7075 obj = local_index_find_or_create(env, lfsck->li_los, root,
7077 S_IFREG | S_IRUGO | S_IWUSR,
7078 &dt_lfsck_namespace_features);
7080 GOTO(out, rc = PTR_ERR(obj));
7083 rc = lfsck_namespace_load(env, com);
7084 if (rc == -ENODATA) {
7085 rc = lfsck_namespace_init(env, com);
7086 } else if (rc < 0) {
7087 rc = lfsck_namespace_reset(env, com, true);
7089 rc = lfsck_load_sub_trace_files(env, com,
7090 &dt_lfsck_namespace_features, LFSCK_NAMESPACE, false);
7092 rc = lfsck_namespace_reset(env, com, true);
7097 ns = com->lc_file_ram;
7098 switch (ns->ln_status) {
7103 spin_lock(&lfsck->li_lock);
7104 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
7105 spin_unlock(&lfsck->li_lock);
7108 CERROR("%s: unknown lfsck_namespace status %d\n",
7109 lfsck_lfsck2name(lfsck), ns->ln_status);
7111 case LS_SCANNING_PHASE1:
7112 case LS_SCANNING_PHASE2:
7113 /* No need to store the status to disk right now.
7114 * If the system crashed before the status stored,
7115 * it will be loaded back when next time. */
7116 ns->ln_status = LS_CRASHED;
7120 spin_lock(&lfsck->li_lock);
7121 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
7122 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
7123 spin_unlock(&lfsck->li_lock);
7130 if (root != NULL && !IS_ERR(root))
7131 lfsck_object_put(env, root);
7133 lfsck_component_cleanup(env, com);
7134 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
7135 lfsck_lfsck2name(lfsck), rc);