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, 2016, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
42 #include "lfsck_internal.h"
44 #define LFSCK_NAMESPACE_MAGIC_V1 0xA0629D03
45 #define LFSCK_NAMESPACE_MAGIC_V2 0xA0621A0B
47 /* For Lustre-2.x (x <= 6), the namespace LFSCK used LFSCK_NAMESPACE_MAGIC_V1
48 * as the trace file magic. When downgrade to such old release, the old LFSCK
49 * will not recognize the new LFSCK_NAMESPACE_MAGIC_V2 in the new trace file,
50 * then it will reset the whole LFSCK, and will not cause start failure. The
51 * similar case will happen when upgrade from such old release. */
52 #define LFSCK_NAMESPACE_MAGIC LFSCK_NAMESPACE_MAGIC_V2
54 enum lfsck_nameentry_check {
55 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
56 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
57 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
60 static struct lfsck_namespace_req *
61 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
62 struct lfsck_assistant_object *lso,
63 struct lu_dirent *ent, __u16 type)
65 struct lfsck_namespace_req *lnr;
68 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
71 return ERR_PTR(-ENOMEM);
73 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
74 lnr->lnr_lar.lar_parent = lfsck_assistant_object_get(lso);
75 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
76 lnr->lnr_fid = ent->lde_fid;
77 lnr->lnr_dir_cookie = ent->lde_hash;
78 lnr->lnr_attr = ent->lde_attrs;
81 lnr->lnr_namelen = ent->lde_namelen;
82 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
87 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
88 struct lfsck_assistant_req *lar)
90 struct lfsck_namespace_req *lnr =
91 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
93 if (lnr->lnr_lmv != NULL)
94 lfsck_lmv_put(env, lnr->lnr_lmv);
96 lfsck_assistant_object_put(env, lar->lar_parent);
97 OBD_FREE(lnr, lnr->lnr_size);
100 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
101 struct lfsck_namespace *src)
103 dst->ln_magic = le32_to_cpu(src->ln_magic);
104 dst->ln_status = le32_to_cpu(src->ln_status);
105 dst->ln_flags = le32_to_cpu(src->ln_flags);
106 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
107 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
108 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
109 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
110 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
111 dst->ln_time_last_checkpoint =
112 le64_to_cpu(src->ln_time_last_checkpoint);
113 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
114 &src->ln_pos_latest_start);
115 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
116 &src->ln_pos_last_checkpoint);
117 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
118 &src->ln_pos_first_inconsistent);
119 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
120 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
121 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
122 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
123 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
124 dst->ln_objs_repaired_phase2 =
125 le64_to_cpu(src->ln_objs_repaired_phase2);
126 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
127 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
128 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
129 &src->ln_fid_latest_scanned_phase2);
130 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
131 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
132 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
133 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
134 dst->ln_unknown_inconsistency =
135 le64_to_cpu(src->ln_unknown_inconsistency);
136 dst->ln_unmatched_pairs_repaired =
137 le64_to_cpu(src->ln_unmatched_pairs_repaired);
138 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
139 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
140 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
141 dst->ln_lost_dirent_repaired =
142 le64_to_cpu(src->ln_lost_dirent_repaired);
143 dst->ln_striped_dirs_scanned =
144 le64_to_cpu(src->ln_striped_dirs_scanned);
145 dst->ln_striped_dirs_repaired =
146 le64_to_cpu(src->ln_striped_dirs_repaired);
147 dst->ln_striped_dirs_failed =
148 le64_to_cpu(src->ln_striped_dirs_failed);
149 dst->ln_striped_dirs_disabled =
150 le64_to_cpu(src->ln_striped_dirs_disabled);
151 dst->ln_striped_dirs_skipped =
152 le64_to_cpu(src->ln_striped_dirs_skipped);
153 dst->ln_striped_shards_scanned =
154 le64_to_cpu(src->ln_striped_shards_scanned);
155 dst->ln_striped_shards_repaired =
156 le64_to_cpu(src->ln_striped_shards_repaired);
157 dst->ln_striped_shards_failed =
158 le64_to_cpu(src->ln_striped_shards_failed);
159 dst->ln_striped_shards_skipped =
160 le64_to_cpu(src->ln_striped_shards_skipped);
161 dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
162 dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
163 dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
164 dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
165 dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
166 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
167 dst->ln_time_latest_reset = le32_to_cpu(src->ln_time_latest_reset);
168 dst->ln_linkea_overflow_cleared =
169 le64_to_cpu(src->ln_linkea_overflow_cleared);
172 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
173 struct lfsck_namespace *src)
175 dst->ln_magic = cpu_to_le32(src->ln_magic);
176 dst->ln_status = cpu_to_le32(src->ln_status);
177 dst->ln_flags = cpu_to_le32(src->ln_flags);
178 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
179 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
180 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
181 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
182 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
183 dst->ln_time_last_checkpoint =
184 cpu_to_le64(src->ln_time_last_checkpoint);
185 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
186 &src->ln_pos_latest_start);
187 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
188 &src->ln_pos_last_checkpoint);
189 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
190 &src->ln_pos_first_inconsistent);
191 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
192 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
193 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
194 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
195 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
196 dst->ln_objs_repaired_phase2 =
197 cpu_to_le64(src->ln_objs_repaired_phase2);
198 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
199 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
200 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
201 &src->ln_fid_latest_scanned_phase2);
202 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
203 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
204 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
205 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
206 dst->ln_unknown_inconsistency =
207 cpu_to_le64(src->ln_unknown_inconsistency);
208 dst->ln_unmatched_pairs_repaired =
209 cpu_to_le64(src->ln_unmatched_pairs_repaired);
210 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
211 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
212 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
213 dst->ln_lost_dirent_repaired =
214 cpu_to_le64(src->ln_lost_dirent_repaired);
215 dst->ln_striped_dirs_scanned =
216 cpu_to_le64(src->ln_striped_dirs_scanned);
217 dst->ln_striped_dirs_repaired =
218 cpu_to_le64(src->ln_striped_dirs_repaired);
219 dst->ln_striped_dirs_failed =
220 cpu_to_le64(src->ln_striped_dirs_failed);
221 dst->ln_striped_dirs_disabled =
222 cpu_to_le64(src->ln_striped_dirs_disabled);
223 dst->ln_striped_dirs_skipped =
224 cpu_to_le64(src->ln_striped_dirs_skipped);
225 dst->ln_striped_shards_scanned =
226 cpu_to_le64(src->ln_striped_shards_scanned);
227 dst->ln_striped_shards_repaired =
228 cpu_to_le64(src->ln_striped_shards_repaired);
229 dst->ln_striped_shards_failed =
230 cpu_to_le64(src->ln_striped_shards_failed);
231 dst->ln_striped_shards_skipped =
232 cpu_to_le64(src->ln_striped_shards_skipped);
233 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
234 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
235 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
236 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
237 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
238 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
239 dst->ln_time_latest_reset = cpu_to_le32(src->ln_time_latest_reset);
240 dst->ln_linkea_overflow_cleared =
241 cpu_to_le64(src->ln_linkea_overflow_cleared);
244 static void lfsck_namespace_record_failure(const struct lu_env *env,
245 struct lfsck_instance *lfsck,
246 struct lfsck_namespace *ns)
248 struct lfsck_position pos;
250 ns->ln_items_failed++;
251 lfsck_pos_fill(env, lfsck, &pos, false);
252 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
253 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
254 ns->ln_pos_first_inconsistent = pos;
256 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
257 "inconsistency at the pos [%llu, "DFID", %#llx]\n",
258 lfsck_lfsck2name(lfsck),
259 ns->ln_pos_first_inconsistent.lp_oit_cookie,
260 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
261 ns->ln_pos_first_inconsistent.lp_dir_cookie);
266 * Load the MDT bitmap from the lfsck_namespace trace file.
268 * \param[in] env pointer to the thread context
269 * \param[in] com pointer to the lfsck component
271 * \retval 0 for success
272 * \retval negative error number on failure or data corruption
274 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
275 struct lfsck_component *com)
277 struct dt_object *obj = com->lc_obj;
278 struct lfsck_assistant_data *lad = com->lc_data;
279 struct lfsck_namespace *ns = com->lc_file_ram;
280 struct cfs_bitmap *bitmap = lad->lad_bitmap;
286 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
288 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
290 nbits = ns->ln_bitmap_size;
292 if (unlikely(nbits < BITS_PER_LONG))
293 nbits = BITS_PER_LONG;
295 if (nbits > bitmap->size) {
296 __u32 new_bits = bitmap->size;
297 struct cfs_bitmap *new_bitmap;
299 while (new_bits < nbits)
302 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
303 if (new_bitmap == NULL)
306 lad->lad_bitmap = new_bitmap;
307 CFS_FREE_BITMAP(bitmap);
311 if (ns->ln_bitmap_size == 0) {
312 lad->lad_incomplete = 0;
313 CFS_RESET_BITMAP(bitmap);
318 size = (ns->ln_bitmap_size + 7) >> 3;
319 rc = dt_xattr_get(env, obj,
320 lfsck_buf_get(env, bitmap->data, size),
321 XATTR_NAME_LFSCK_BITMAP);
323 RETURN(rc >= 0 ? -EINVAL : rc);
325 if (cfs_bitmap_check_empty(bitmap))
326 lad->lad_incomplete = 0;
328 lad->lad_incomplete = 1;
334 * Load namespace LFSCK statistics information from the trace file.
336 * \param[in] env pointer to the thread context
337 * \param[in] com pointer to the lfsck component
339 * \retval 0 for success
340 * \retval negative error number on failure
342 static int lfsck_namespace_load(const struct lu_env *env,
343 struct lfsck_component *com)
345 int len = com->lc_file_size;
348 rc = dt_xattr_get(env, com->lc_obj,
349 lfsck_buf_get(env, com->lc_file_disk, len),
350 XATTR_NAME_LFSCK_NAMESPACE);
352 struct lfsck_namespace *ns = com->lc_file_ram;
354 lfsck_namespace_le_to_cpu(ns,
355 (struct lfsck_namespace *)com->lc_file_disk);
356 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
357 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
358 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
359 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
364 } else if (rc != -ENODATA) {
365 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
366 "expected = %d: rc = %d\n",
367 lfsck_lfsck2name(com->lc_lfsck), len, rc);
375 static int lfsck_namespace_store(const struct lu_env *env,
376 struct lfsck_component *com)
378 struct dt_object *obj = com->lc_obj;
379 struct lfsck_instance *lfsck = com->lc_lfsck;
380 struct lfsck_namespace *ns = com->lc_file_ram;
381 struct lfsck_assistant_data *lad = com->lc_data;
382 struct dt_device *dev = lfsck_obj2dev(obj);
383 struct cfs_bitmap *bitmap = NULL;
384 struct thandle *handle;
386 int len = com->lc_file_size;
391 bitmap = lad->lad_bitmap;
392 nbits = bitmap->size;
395 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
398 ns->ln_bitmap_size = nbits;
399 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
401 handle = dt_trans_create(env, dev);
403 GOTO(log, rc = PTR_ERR(handle));
405 rc = dt_declare_xattr_set(env, obj,
406 lfsck_buf_get(env, com->lc_file_disk, len),
407 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
411 if (bitmap != NULL) {
412 rc = dt_declare_xattr_set(env, obj,
413 lfsck_buf_get(env, bitmap->data, nbits >> 3),
414 XATTR_NAME_LFSCK_BITMAP, 0, handle);
419 rc = dt_trans_start_local(env, dev, handle);
423 rc = dt_xattr_set(env, obj,
424 lfsck_buf_get(env, com->lc_file_disk, len),
425 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
426 if (rc == 0 && bitmap != NULL)
427 rc = dt_xattr_set(env, obj,
428 lfsck_buf_get(env, bitmap->data, nbits >> 3),
429 XATTR_NAME_LFSCK_BITMAP, 0, handle);
434 dt_trans_stop(env, dev, handle);
438 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
439 lfsck_lfsck2name(lfsck), rc);
443 static struct dt_object *
444 lfsck_namespace_load_one_trace_file(const struct lu_env *env,
445 struct lfsck_component *com,
446 struct dt_object *parent,
447 const char *name, bool reset)
449 struct lfsck_instance *lfsck = com->lc_lfsck;
450 struct dt_object *obj;
454 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
455 if (rc != 0 && rc != -ENOENT)
459 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
460 S_IFREG | S_IRUGO | S_IWUSR,
466 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
467 struct lfsck_component *com,
470 char *name = lfsck_env_info(env)->lti_key;
471 struct lfsck_sub_trace_obj *lsto;
472 struct dt_object *obj;
476 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
477 i < LFSCK_STF_COUNT; i++, lsto++) {
478 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
479 mutex_lock(&lsto->lsto_mutex);
480 if (lsto->lsto_obj != NULL) {
482 mutex_unlock(&lsto->lsto_mutex);
486 lfsck_object_put(env, lsto->lsto_obj);
487 lsto->lsto_obj = NULL;
490 obj = lfsck_namespace_load_one_trace_file(env, com,
491 com->lc_lfsck->li_lfsck_dir, name, reset);
492 LASSERT(obj != NULL);
496 lsto->lsto_obj = obj;
497 rc = obj->do_ops->do_index_try(env, obj,
500 mutex_unlock(&lsto->lsto_mutex);
508 static int lfsck_namespace_init(const struct lu_env *env,
509 struct lfsck_component *com)
511 struct lfsck_namespace *ns = com->lc_file_ram;
514 memset(ns, 0, sizeof(*ns));
515 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
516 ns->ln_status = LS_INIT;
517 ns->ln_time_latest_reset = cfs_time_current_sec();
518 down_write(&com->lc_sem);
519 rc = lfsck_namespace_store(env, com);
520 up_write(&com->lc_sem);
522 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
528 * Update the namespace LFSCK trace file for the given @fid
530 * \param[in] env pointer to the thread context
531 * \param[in] com pointer to the lfsck component
532 * \param[in] fid the fid which flags to be updated in the lfsck
534 * \param[in] add true if add new flags, otherwise remove flags
536 * \retval 0 for success or nothing to be done
537 * \retval negative error number on failure
539 int lfsck_namespace_trace_update(const struct lu_env *env,
540 struct lfsck_component *com,
541 const struct lu_fid *fid,
542 const __u8 flags, bool add)
544 struct lfsck_instance *lfsck = com->lc_lfsck;
545 struct dt_object *obj;
546 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
547 struct dt_device *dev;
548 struct thandle *th = NULL;
557 if (unlikely(!fid_is_sane(fid)))
560 idx = lfsck_sub_trace_file_fid2idx(fid);
561 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
562 obj = com->lc_sub_trace_objs[idx].lsto_obj;
563 if (unlikely(obj == NULL)) {
564 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
568 lfsck_object_get(obj);
569 dev = lfsck_obj2dev(obj);
570 fid_cpu_to_be(key, fid);
571 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
572 (const struct dt_key *)key);
575 GOTO(unlock, rc = 0);
579 } else if (rc == 0) {
581 if ((old & flags) == flags)
582 GOTO(unlock, rc = 0);
586 if ((old & flags) == 0)
587 GOTO(unlock, rc = 0);
595 th = dt_trans_create(env, dev);
597 GOTO(log, rc = PTR_ERR(th));
600 rc = dt_declare_delete(env, obj,
601 (const struct dt_key *)key, th);
607 rc = dt_declare_insert(env, obj,
608 (const struct dt_rec *)&new,
609 (const struct dt_key *)key, th);
614 rc = dt_trans_start_local(env, dev, th);
619 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
625 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
626 (const struct dt_key *)key, th, 1);
634 if (th != NULL && !IS_ERR(th))
635 dt_trans_stop(env, dev, th);
637 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
638 "trace file, flags %x, old %x, new %x: rc = %d\n",
639 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
640 (__u32)flags, (__u32)old, (__u32)new, rc);
643 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
644 lfsck_object_put(env, obj);
649 int lfsck_namespace_check_exist(const struct lu_env *env,
650 struct dt_object *dir,
651 struct dt_object *obj, const char *name)
653 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
657 if (unlikely(lfsck_is_dead_obj(obj)))
658 RETURN(LFSCK_NAMEENTRY_DEAD);
660 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
661 (const struct dt_key *)name);
663 RETURN(LFSCK_NAMEENTRY_REMOVED);
668 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
669 RETURN(LFSCK_NAMEENTRY_RECREATED);
674 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
675 struct dt_object *obj,
676 struct thandle *handle)
680 /* For remote updating LINKEA, there may be further LFSCK action
681 * on remote MDT after the updating, so update the LINKEA ASAP. */
682 if (dt_object_remote(obj))
685 /* For destroying all invalid linkEA entries. */
686 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
688 /* For insert new linkEA entry. */
689 rc = dt_declare_xattr_set(env, obj,
690 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
691 XATTR_NAME_LINK, 0, handle);
695 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
696 struct linkea_data *ldata, bool with_rec)
700 if (ldata->ld_buf->lb_buf == NULL)
703 if (!dt_object_exists(obj))
706 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
708 /* Buf was too small, figure out what we need. */
709 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
710 if (unlikely(rc == 0))
716 lu_buf_realloc(ldata->ld_buf, rc);
717 if (ldata->ld_buf->lb_buf == NULL)
720 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
723 if (unlikely(rc == 0))
728 rc = linkea_init_with_rec(ldata);
730 rc = linkea_init(ldata);
737 * Remove linkEA for the given object.
739 * The caller should take the ldlm lock before the calling.
741 * \param[in] env pointer to the thread context
742 * \param[in] com pointer to the lfsck component
743 * \param[in] obj pointer to the dt_object to be handled
745 * \retval 0 for repaired cases
746 * \retval negative error number on failure
748 static int lfsck_namespace_links_remove(const struct lu_env *env,
749 struct lfsck_component *com,
750 struct dt_object *obj)
752 struct lfsck_instance *lfsck = com->lc_lfsck;
753 struct dt_device *dev = lfsck_obj2dev(obj);
754 struct thandle *th = NULL;
758 LASSERT(dt_object_remote(obj) == 0);
760 th = dt_trans_create(env, dev);
762 GOTO(log, rc = PTR_ERR(th));
764 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
768 rc = dt_trans_start_local(env, dev, th);
772 dt_write_lock(env, obj, 0);
773 if (unlikely(lfsck_is_dead_obj(obj)))
774 GOTO(unlock, rc = -ENOENT);
776 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
777 GOTO(unlock, rc = 0);
779 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
784 dt_write_unlock(env, obj);
787 dt_trans_stop(env, dev, th);
790 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
791 "for the object "DFID": rc = %d\n",
792 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
795 struct lfsck_namespace *ns = com->lc_file_ram;
797 ns->ln_flags |= LF_INCONSISTENT;
803 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
804 struct linkea_data *ldata, struct thandle *handle)
809 lfsck_buf_init(&buf, ldata->ld_buf->lb_buf, ldata->ld_leh->leh_len);
812 rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LINK, 0, handle);
813 if (unlikely(rc == -ENOSPC)) {
814 rc = linkea_overflow_shrink(ldata);
815 if (likely(rc > 0)) {
824 static int lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
825 struct lu_name *cname,
827 char *buf, const int buflen)
829 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
830 if (unlikely(ldata->ld_reclen <= 0 ||
831 ldata->ld_reclen + sizeof(struct link_ea_header) >
832 ldata->ld_leh->leh_len ||
833 cname->ln_namelen <= 0 ||
834 cname->ln_namelen > NAME_MAX ||
835 cname->ln_namelen >= buflen ||
839 /* To guarantee the 'name' is terminated with '0'. */
840 memcpy(buf, cname->ln_name, cname->ln_namelen);
841 buf[cname->ln_namelen] = 0;
842 cname->ln_name = buf;
847 static void lfsck_linkea_del_buf(struct linkea_data *ldata,
848 const struct lu_name *lname)
850 LASSERT(ldata->ld_leh != NULL && ldata->ld_lee != NULL);
852 /* If current record is corrupted, all the subsequent
853 * records will be dropped. */
854 if (unlikely(ldata->ld_reclen <= 0 ||
855 ldata->ld_reclen + sizeof(struct link_ea_header) >
856 ldata->ld_leh->leh_len)) {
857 void *ptr = ldata->ld_lee;
859 ldata->ld_leh->leh_len = sizeof(struct link_ea_header);
860 ldata->ld_leh->leh_reccount = 0;
861 linkea_first_entry(ldata);
862 while (ldata->ld_lee != NULL &&
863 (char *)ldata->ld_lee < (char *)ptr) {
864 int reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
865 ldata->ld_lee->lee_reclen[1];
867 ldata->ld_leh->leh_len += reclen;
868 ldata->ld_leh->leh_reccount++;
869 ldata->ld_lee = (struct link_ea_entry *)
870 ((char *)ldata->ld_lee + reclen);
873 ldata->ld_lee = NULL;
875 linkea_del_buf(ldata, lname);
879 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
880 struct lu_name *cname,
884 struct link_ea_entry *oldlee;
888 oldlee = ldata->ld_lee;
889 oldlen = ldata->ld_reclen;
890 linkea_next_entry(ldata);
891 while (ldata->ld_lee != NULL) {
892 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
893 ldata->ld_lee->lee_reclen[1];
894 if (unlikely(ldata->ld_reclen == oldlen &&
895 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
900 lfsck_linkea_del_buf(ldata, cname);
902 linkea_next_entry(ldata);
905 ldata->ld_lee = oldlee;
906 ldata->ld_reclen = oldlen;
912 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
914 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
915 * with the given type to generate the name, the detailed rules for name
916 * have been described as following.
918 * The function also generates the linkEA corresponding to the name entry
919 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
921 * \param[in] env pointer to the thread context
922 * \param[in] com pointer to the lfsck component
923 * \param[in] orphan pointer to the orphan MDT-object
924 * \param[in] infix additional information for the orphan name, such as
925 * the FID for original
926 * \param[in] type the type for describing why the orphan MDT-object is
927 * created. The rules are as following:
929 * type "D": The MDT-object is a directory, it may knows its parent
930 * but because there is no valid linkEA, the LFSCK cannot
931 * know where to put it back to the namespace.
932 * type "O": The MDT-object has no linkEA, and there is no name
933 * entry that references the MDT-object.
935 * type "S": The orphan MDT-object is a shard of a striped directory
937 * \see lfsck_layout_recreate_parent() for more types.
939 * The orphan name will be like:
940 * ${FID}-${infix}-${type}-${conflict_version}
942 * \param[out] count if some others inserted some linkEA entries by race,
943 * then return the linkEA entries count.
945 * \retval positive number for repaired cases
946 * \retval 0 if needs to repair nothing
947 * \retval negative error number on failure
949 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
950 struct lfsck_component *com,
951 struct dt_object *orphan,
952 const char *infix, const char *type,
955 struct lfsck_thread_info *info = lfsck_env_info(env);
956 struct lu_name *cname = &info->lti_name;
957 struct dt_insert_rec *rec = &info->lti_dt_rec;
958 struct lu_attr *la = &info->lti_la2;
959 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
960 const struct lu_fid *pfid;
962 struct lfsck_instance *lfsck = com->lc_lfsck;
963 struct dt_device *dev = lfsck_obj2dev(orphan);
964 struct dt_object *parent;
965 struct thandle *th = NULL;
966 struct lfsck_lock_handle *pllh = &info->lti_llh;
967 struct lustre_handle clh = { 0 };
968 struct linkea_data ldata2 = { NULL };
969 struct lu_buf linkea_buf;
976 cname->ln_name = NULL;
977 if (unlikely(lfsck->li_lpf_obj == NULL))
978 GOTO(log, rc = -ENXIO);
980 parent = lfsck->li_lpf_obj;
981 pfid = lfsck_dto2fid(parent);
985 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
986 PFID(cfid), infix, type, idx++);
987 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
988 (const struct dt_key *)info->lti_key);
989 if (rc != 0 && rc != -ENOENT)
992 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
994 } while (rc == 0 && !exist);
996 rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
997 MDS_INODELOCK_UPDATE, LCK_PW);
1001 /* Re-check whether the name conflict with othrs after taken
1003 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1004 (const struct dt_key *)info->lti_key);
1006 if (!lu_fid_eq(cfid, &tfid)) {
1013 } else if (rc != -ENOENT) {
1019 cname->ln_name = info->lti_key;
1020 cname->ln_namelen = namelen;
1021 rc = linkea_links_new(&ldata2, &info->lti_linkea_buf2,
1026 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
1027 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
1028 MDS_INODELOCK_XATTR, LCK_EX);
1032 lfsck_buf_init(&linkea_buf, ldata2.ld_buf->lb_buf,
1033 ldata2.ld_leh->leh_len);
1034 th = dt_trans_create(env, dev);
1036 GOTO(log, rc = PTR_ERR(th));
1038 if (S_ISDIR(lfsck_object_type(orphan))) {
1039 rc = dt_declare_delete(env, orphan,
1040 (const struct dt_key *)dotdot, th);
1044 rec->rec_type = S_IFDIR;
1045 rec->rec_fid = pfid;
1046 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1047 (const struct dt_key *)dotdot, th);
1052 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1053 XATTR_NAME_LINK, 0, th);
1058 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1059 rec->rec_fid = cfid;
1060 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1061 (const struct dt_key *)cname->ln_name,
1066 if (S_ISDIR(rec->rec_type)) {
1067 rc = dt_declare_ref_add(env, parent, th);
1073 memset(la, 0, sizeof(*la));
1074 la->la_ctime = cfs_time_current_sec();
1075 la->la_valid = LA_CTIME;
1076 rc = dt_declare_attr_set(env, orphan, la, th);
1080 rc = dt_trans_start_local(env, dev, th);
1084 dt_write_lock(env, orphan, 0);
1085 rc = lfsck_links_read2_with_rec(env, orphan, &ldata2);
1086 if (likely(rc == -ENODATA || rc == -EINVAL)) {
1087 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1088 GOTO(unlock, rc = 1);
1090 if (S_ISDIR(lfsck_object_type(orphan))) {
1091 rc = dt_delete(env, orphan,
1092 (const struct dt_key *)dotdot, th);
1096 rec->rec_type = S_IFDIR;
1097 rec->rec_fid = pfid;
1098 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1099 (const struct dt_key *)dotdot, th, 1);
1104 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1107 if (rc == 0 && count != NULL)
1108 *count = ldata2.ld_leh->leh_reccount;
1112 dt_write_unlock(env, orphan);
1114 if (rc == 0 && !exist) {
1115 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1116 rec->rec_fid = cfid;
1117 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1118 (const struct dt_key *)cname->ln_name, th, 1);
1119 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1120 dt_write_lock(env, parent, 0);
1121 rc = dt_ref_add(env, parent, th);
1122 dt_write_unlock(env, parent);
1127 rc = dt_attr_set(env, orphan, la, th);
1129 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1132 dt_write_unlock(env, orphan);
1135 dt_trans_stop(env, dev, th);
1138 lfsck_ibits_unlock(&clh, LCK_EX);
1140 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1141 "object "DFID", name = %s: rc = %d\n",
1142 lfsck_lfsck2name(lfsck), PFID(cfid),
1143 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1146 struct lfsck_namespace *ns = com->lc_file_ram;
1148 ns->ln_flags |= LF_INCONSISTENT;
1155 * Add the specified name entry back to namespace.
1157 * If there is a linkEA entry that back references a name entry under
1158 * some parent directory, but such parent directory does not have the
1159 * claimed name entry. On the other hand, the linkEA entries count is
1160 * not larger than the MDT-object's hard link count. Under such case,
1161 * it is quite possible that the name entry is lost. Then the LFSCK
1162 * should add the name entry back to the namespace.
1164 * \param[in] env pointer to the thread context
1165 * \param[in] com pointer to the lfsck component
1166 * \param[in] parent pointer to the directory under which the name entry
1167 * will be inserted into
1168 * \param[in] child pointer to the object referenced by the name entry
1169 * that to be inserted into the parent
1170 * \param[in] name the name for the child in the parent directory
1172 * \retval positive number for repaired cases
1173 * \retval 0 if nothing to be repaired
1174 * \retval negative error number on failure
1176 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1177 struct lfsck_component *com,
1178 struct dt_object *parent,
1179 struct dt_object *child,
1182 struct lfsck_thread_info *info = lfsck_env_info(env);
1183 struct lu_attr *la = &info->lti_la;
1184 struct dt_insert_rec *rec = &info->lti_dt_rec;
1185 struct lfsck_instance *lfsck = com->lc_lfsck;
1186 /* The child and its name may be on different MDTs. */
1187 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1188 const struct lu_fid *cfid = lfsck_dto2fid(child);
1189 struct dt_device *dev = lfsck->li_next;
1190 struct thandle *th = NULL;
1191 struct lfsck_lock_handle *llh = &info->lti_llh;
1195 /* @parent/@child may be based on lfsck->li_bottom,
1196 * but here we need the object based on the lfsck->li_next. */
1198 parent = lfsck_object_locate(dev, parent);
1200 GOTO(log, rc = PTR_ERR(parent));
1202 if (unlikely(!dt_try_as_dir(env, parent)))
1203 GOTO(log, rc = -ENOTDIR);
1205 child = lfsck_object_locate(dev, child);
1207 GOTO(log, rc = PTR_ERR(child));
1209 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1212 rc = lfsck_lock(env, lfsck, parent, name, llh,
1213 MDS_INODELOCK_UPDATE, LCK_PW);
1217 th = dt_trans_create(env, dev);
1219 GOTO(unlock, rc = PTR_ERR(th));
1221 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1222 rec->rec_fid = cfid;
1223 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1224 (const struct dt_key *)name, th);
1228 if (S_ISDIR(rec->rec_type)) {
1229 rc = dt_declare_ref_add(env, parent, th);
1234 memset(la, 0, sizeof(*la));
1235 la->la_ctime = cfs_time_current_sec();
1236 la->la_valid = LA_CTIME;
1237 rc = dt_declare_attr_set(env, parent, la, th);
1241 rc = dt_declare_attr_set(env, child, la, th);
1245 rc = dt_trans_start_local(env, dev, th);
1249 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1250 (const struct dt_key *)name, th, 1);
1254 if (S_ISDIR(rec->rec_type)) {
1255 dt_write_lock(env, parent, 0);
1256 rc = dt_ref_add(env, parent, th);
1257 dt_write_unlock(env, parent);
1262 la->la_ctime = cfs_time_current_sec();
1263 rc = dt_attr_set(env, parent, la, th);
1267 rc = dt_attr_set(env, child, la, th);
1269 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1272 dt_trans_stop(env, dev, th);
1278 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1279 "the name %s and type %o to the parent "DFID": rc = %d\n",
1280 lfsck_lfsck2name(lfsck), PFID(cfid), name,
1281 lfsck_object_type(child) & S_IFMT, PFID(pfid), rc);
1284 struct lfsck_namespace *ns = com->lc_file_ram;
1286 ns->ln_flags |= LF_INCONSISTENT;
1288 ns->ln_lost_dirent_repaired++;
1295 * Create the specified orphan directory.
1297 * For the case that the parent MDT-object stored in some MDT-object's
1298 * linkEA entry is lost, the LFSCK will re-create the parent object as
1299 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1300 * with the name ${FID}-P-${conflict_version}.
1302 * \param[in] env pointer to the thread context
1303 * \param[in] com pointer to the lfsck component
1304 * \param[in] orphan pointer to the orphan MDT-object to be created
1305 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1307 * \retval positive number for repaired cases
1308 * \retval negative error number on failure
1310 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1311 struct lfsck_component *com,
1312 struct dt_object *orphan,
1313 struct lmv_mds_md_v1 *lmv)
1315 struct lfsck_thread_info *info = lfsck_env_info(env);
1316 struct lu_attr *la = &info->lti_la;
1317 struct dt_allocation_hint *hint = &info->lti_hint;
1318 struct dt_object_format *dof = &info->lti_dof;
1319 struct lu_name *cname = &info->lti_name2;
1320 struct dt_insert_rec *rec = &info->lti_dt_rec;
1321 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1322 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1324 struct lfsck_instance *lfsck = com->lc_lfsck;
1325 struct lfsck_namespace *ns = com->lc_file_ram;
1326 struct dt_device *dev = lfsck_obj2dev(orphan);
1327 struct dt_object *parent = NULL;
1328 struct thandle *th = NULL;
1329 struct lfsck_lock_handle *llh = &info->lti_llh;
1330 struct linkea_data ldata = { NULL };
1331 struct lu_buf linkea_buf;
1332 struct lu_buf lmv_buf;
1340 LASSERT(!dt_object_exists(orphan));
1342 cname->ln_name = NULL;
1343 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1346 if (dt_object_remote(orphan)) {
1347 LASSERT(lfsck->li_lpf_root_obj != NULL);
1349 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1351 GOTO(log, rc = idx);
1353 snprintf(name, 8, "MDT%04x", idx);
1354 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1355 (struct dt_rec *)&tfid,
1356 (const struct dt_key *)name);
1358 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1360 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1362 GOTO(log, rc = PTR_ERR(parent));
1364 if (unlikely(!dt_try_as_dir(env, parent)))
1365 GOTO(log, rc = -ENOTDIR);
1367 if (unlikely(lfsck->li_lpf_obj == NULL))
1368 GOTO(log, rc = -ENXIO);
1370 parent = lfsck->li_lpf_obj;
1373 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1375 GOTO(log, rc = PTR_ERR(dev));
1381 namelen = snprintf(name, 31, DFID"-P-%d",
1383 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1384 (const struct dt_key *)name);
1385 if (rc != 0 && rc != -ENOENT)
1389 rc = lfsck_lock(env, lfsck, parent, name, llh,
1390 MDS_INODELOCK_UPDATE, LCK_PW);
1394 /* Re-check whether the name conflict with othrs after taken
1396 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1397 (const struct dt_key *)name);
1398 if (unlikely(rc == 0)) {
1406 cname->ln_name = name;
1407 cname->ln_namelen = namelen;
1409 memset(la, 0, sizeof(*la));
1410 la->la_mode = S_IFDIR | 0700;
1411 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1412 LA_ATIME | LA_MTIME | LA_CTIME;
1414 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1415 la->la_mode & S_IFMT);
1417 memset(dof, 0, sizeof(*dof));
1418 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1420 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
1421 cname, lfsck_dto2fid(parent));
1425 th = dt_trans_create(env, dev);
1427 GOTO(unlock1, rc = PTR_ERR(th));
1429 /* Sync the remote transaction to guarantee that the subsequent
1430 * lock against the @orphan can find the @orphan in time. */
1431 if (dt_object_remote(orphan))
1434 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1438 if (unlikely(!dt_try_as_dir(env, orphan)))
1439 GOTO(stop, rc = -ENOTDIR);
1441 rc = dt_declare_ref_add(env, orphan, th);
1445 rec->rec_type = S_IFDIR;
1446 rec->rec_fid = cfid;
1447 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1448 (const struct dt_key *)dot, th);
1452 rec->rec_fid = lfsck_dto2fid(parent);
1453 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1454 (const struct dt_key *)dotdot, th);
1459 lmv->lmv_magic = LMV_MAGIC;
1460 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1461 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1462 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1463 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1464 XATTR_NAME_LMV, 0, th);
1469 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1470 ldata.ld_leh->leh_len);
1471 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1472 XATTR_NAME_LINK, 0, th);
1476 rec->rec_fid = cfid;
1477 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1478 (const struct dt_key *)name, th);
1480 rc = dt_declare_ref_add(env, parent, th);
1485 rc = dt_trans_start_local(env, dev, th);
1489 dt_write_lock(env, orphan, 0);
1490 rc = dt_create(env, orphan, la, hint, dof, th);
1494 rc = dt_ref_add(env, orphan, th);
1498 rec->rec_fid = cfid;
1499 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1500 (const struct dt_key *)dot, th, 1);
1504 rec->rec_fid = lfsck_dto2fid(parent);
1505 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1506 (const struct dt_key *)dotdot, th, 1);
1511 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1516 rc = dt_xattr_set(env, orphan, &linkea_buf,
1517 XATTR_NAME_LINK, 0, th);
1518 dt_write_unlock(env, orphan);
1522 rec->rec_fid = cfid;
1523 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1524 (const struct dt_key *)name, th, 1);
1526 dt_write_lock(env, parent, 0);
1527 rc = dt_ref_add(env, parent, th);
1528 dt_write_unlock(env, parent);
1531 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1534 dt_write_unlock(env, orphan);
1537 rc1 = dt_trans_stop(env, dev, th);
1538 if (rc1 != 0 && rc > 0)
1545 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1546 "the object "DFID", name = %s: rc = %d\n",
1547 lfsck_lfsck2name(lfsck), PFID(cfid),
1548 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1550 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1551 lfsck_object_put(env, parent);
1554 ns->ln_flags |= LF_INCONSISTENT;
1560 * Remove the specified entry from the linkEA.
1562 * Locate the linkEA entry with the given @cname and @pfid, then
1563 * remove this entry or the other entries those are repeated with
1566 * \param[in] env pointer to the thread context
1567 * \param[in] com pointer to the lfsck component
1568 * \param[in] obj pointer to the dt_object to be handled
1569 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1570 * \param[in] cname the name for the child in the parent directory
1571 * \param[in] pfid the parent directory's FID for the linkEA
1572 * \param[in] next if true, then remove the first found linkEA
1573 * entry, and move the ldata->ld_lee to next entry
1575 * \retval positive number for repaired cases
1576 * \retval 0 if nothing to be repaired
1577 * \retval negative error number on failure
1579 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1580 struct lfsck_component *com,
1581 struct dt_object *obj,
1582 struct linkea_data *ldata,
1583 struct lu_name *cname,
1584 struct lu_fid *pfid,
1587 struct lfsck_instance *lfsck = com->lc_lfsck;
1588 struct dt_device *dev = lfsck_obj2dev(obj);
1589 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1590 struct thandle *th = NULL;
1591 struct lustre_handle lh = { 0 };
1592 struct linkea_data ldata_new = { NULL };
1593 struct lu_buf linkea_buf;
1598 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1599 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1605 lfsck_linkea_del_buf(ldata, cname);
1607 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1609 if (ldata->ld_leh->leh_reccount > 0 ||
1610 unlikely(ldata->ld_leh->leh_overflow_time)) {
1611 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1612 ldata->ld_leh->leh_len);
1613 buflen = linkea_buf.lb_len;
1617 th = dt_trans_create(env, dev);
1619 GOTO(unlock1, rc = PTR_ERR(th));
1622 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1623 XATTR_NAME_LINK, 0, th);
1625 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
1629 rc = dt_trans_start_local(env, dev, th);
1633 dt_write_lock(env, obj, 0);
1634 if (unlikely(lfsck_is_dead_obj(obj)))
1635 GOTO(unlock2, rc = -ENOENT);
1637 rc = lfsck_links_read2_with_rec(env, obj, &ldata_new);
1639 GOTO(unlock2, rc = (rc == -ENODATA ? 0 : rc));
1641 /* The specified linkEA entry has been removed by race. */
1642 rc = linkea_links_find(&ldata_new, cname, pfid);
1644 GOTO(unlock2, rc = 0);
1646 if (bk->lb_param & LPF_DRYRUN)
1647 GOTO(unlock2, rc = 1);
1650 lfsck_linkea_del_buf(&ldata_new, cname);
1652 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1655 if (buflen < ldata_new.ld_leh->leh_len) {
1656 dt_write_unlock(env, obj);
1657 dt_trans_stop(env, dev, th);
1658 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1659 ldata_new.ld_leh->leh_len);
1663 if (ldata_new.ld_leh->leh_reccount > 0 ||
1664 unlikely(ldata->ld_leh->leh_overflow_time))
1665 rc = lfsck_links_write(env, obj, &ldata_new, th);
1667 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
1669 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1672 dt_write_unlock(env, obj);
1675 dt_trans_stop(env, dev, th);
1678 lfsck_ibits_unlock(&lh, LCK_EX);
1681 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1682 "for the object: "DFID", parent "DFID", name %.*s\n",
1683 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1684 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1688 struct lfsck_namespace *ns = com->lc_file_ram;
1690 ns->ln_flags |= LF_INCONSISTENT;
1697 * Conditionally remove the specified entry from the linkEA.
1699 * Take the parent lock firstly, then check whether the specified
1700 * name entry exists or not: if yes, do nothing; otherwise, call
1701 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1703 * \param[in] env pointer to the thread context
1704 * \param[in] com pointer to the lfsck component
1705 * \param[in] parent pointer to the parent directory
1706 * \param[in] child pointer to the child object that holds the linkEA
1707 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1708 * \param[in] cname the name for the child in the parent directory
1709 * \param[in] pfid the parent directory's FID for the linkEA
1711 * \retval positive number for repaired cases
1712 * \retval 0 if nothing to be repaired
1713 * \retval negative error number on failure
1715 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1716 struct lfsck_component *com,
1717 struct dt_object *parent,
1718 struct dt_object *child,
1719 struct linkea_data *ldata,
1720 struct lu_name *cname,
1721 struct lu_fid *pfid)
1723 struct lfsck_thread_info *info = lfsck_env_info(env);
1724 struct lu_fid *cfid = &info->lti_fid3;
1725 struct lfsck_lock_handle *llh = &info->lti_llh;
1729 rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1730 MDS_INODELOCK_UPDATE, LCK_PR);
1734 dt_read_lock(env, parent, 0);
1735 if (unlikely(lfsck_is_dead_obj(parent))) {
1736 dt_read_unlock(env, parent);
1738 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1744 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1745 (const struct dt_key *)cname->ln_name);
1746 dt_read_unlock(env, parent);
1748 /* It is safe to release the ldlm lock, because when the logic come
1749 * here, we have got all the needed information above whether the
1750 * linkEA entry is valid or not. It is not important that others
1751 * may add new linkEA entry after the ldlm lock released. If other
1752 * has removed the specified linkEA entry by race, then it is OK,
1753 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1756 if (rc == -ENOENT) {
1757 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1766 /* The LFSCK just found some internal status of cross-MDTs
1767 * create operation. That is normal. */
1768 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1769 linkea_next_entry(ldata);
1774 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1781 * Conditionally replace name entry in the parent.
1783 * As required, the LFSCK may re-create the lost MDT-object for dangling
1784 * name entry, but such repairing may be wrong because of bad FID in the
1785 * name entry. As the LFSCK processing, the real MDT-object may be found,
1786 * then the LFSCK should check whether the former re-created MDT-object
1787 * has been modified or not, if not, then destroy it and update the name
1788 * entry in the parent to reference the real MDT-object.
1790 * \param[in] env pointer to the thread context
1791 * \param[in] com pointer to the lfsck component
1792 * \param[in] parent pointer to the parent directory
1793 * \param[in] child pointer to the MDT-object that may be the real
1794 * MDT-object corresponding to the name entry in parent
1795 * \param[in] cfid the current FID in the name entry
1796 * \param[in] cname contains the name of the child in the parent directory
1798 * \retval positive number for repaired cases
1799 * \retval 0 if nothing to be repaired
1800 * \retval negative error number on failure
1802 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1803 struct lfsck_component *com,
1804 struct dt_object *parent,
1805 struct dt_object *child,
1806 const struct lu_fid *cfid,
1807 const struct lu_name *cname)
1809 struct lfsck_thread_info *info = lfsck_env_info(env);
1810 struct lu_attr *la = &info->lti_la;
1811 struct dt_insert_rec *rec = &info->lti_dt_rec;
1813 struct lfsck_instance *lfsck = com->lc_lfsck;
1814 /* The child and its name may be on different MDTs. */
1815 struct dt_device *dev = lfsck->li_next;
1816 const char *name = cname->ln_name;
1817 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1818 struct dt_object *cobj = NULL;
1819 struct lfsck_lock_handle *pllh = &info->lti_llh;
1820 struct lustre_handle clh = { 0 };
1821 struct linkea_data ldata = { NULL };
1822 struct thandle *th = NULL;
1827 /* @parent/@child may be based on lfsck->li_bottom,
1828 * but here we need the object based on the lfsck->li_next. */
1830 parent = lfsck_object_locate(dev, parent);
1832 GOTO(log, rc = PTR_ERR(parent));
1834 if (unlikely(!dt_try_as_dir(env, parent)))
1835 GOTO(log, rc = -ENOTDIR);
1837 rc = lfsck_lock(env, lfsck, parent, name, pllh,
1838 MDS_INODELOCK_UPDATE, LCK_PW);
1842 if (!fid_is_sane(cfid)) {
1847 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1850 if (rc == -ENOENT) {
1858 if (!dt_object_exists(cobj)) {
1863 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1864 (const struct dt_key *)name);
1865 if (rc == -ENOENT) {
1873 /* Someone changed the name entry, cannot replace it. */
1874 if (!lu_fid_eq(cfid, &tfid))
1877 /* lock the object to be destroyed. */
1878 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1879 MDS_INODELOCK_UPDATE |
1880 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1885 if (unlikely(lfsck_is_dead_obj(cobj))) {
1890 rc = dt_attr_get(env, cobj, la);
1894 /* The object has been modified by other(s), or it is not created by
1895 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1896 if (la->la_ctime != 0)
1899 if (S_ISREG(la->la_mode)) {
1900 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
1901 /* If someone has created related OST-object(s),
1903 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1904 GOTO(log, rc = (rc > 0 ? 0 : rc));
1908 dt_read_lock(env, child, 0);
1909 rc = lfsck_links_read2_with_rec(env, child, &ldata);
1910 dt_read_unlock(env, child);
1912 /* Someone changed the child, no need to replace. */
1919 rc = linkea_links_find(&ldata, cname, pfid);
1920 /* Someone moved the child, no need to replace. */
1924 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1927 th = dt_trans_create(env, dev);
1929 GOTO(log, rc = PTR_ERR(th));
1932 rc = dt_declare_destroy(env, cobj, th);
1937 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1941 rec->rec_type = S_IFDIR;
1942 rec->rec_fid = lfsck_dto2fid(child);
1943 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1944 (const struct dt_key *)name, th);
1948 rc = dt_trans_start_local(env, dev, th);
1953 rc = dt_destroy(env, cobj, th);
1958 /* The old name entry maybe not exist. */
1959 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
1960 if (rc != 0 && rc != -ENOENT)
1963 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1964 (const struct dt_key *)name, th, 1);
1966 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1969 dt_trans_stop(env, dev, th);
1972 lfsck_ibits_unlock(&clh, LCK_EX);
1975 if (cobj != NULL && !IS_ERR(cobj))
1976 lfsck_object_put(env, cobj);
1978 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1979 "object "DFID" because of conflict with the object "DFID
1980 " under the parent "DFID" with name %s: rc = %d\n",
1981 lfsck_lfsck2name(lfsck), PFID(cfid),
1982 PFID(lfsck_dto2fid(child)), PFID(pfid), name, rc);
1988 * Overwrite the linkEA for the object with the given ldata.
1990 * The caller should take the ldlm lock before the calling.
1992 * \param[in] env pointer to the thread context
1993 * \param[in] com pointer to the lfsck component
1994 * \param[in] obj pointer to the dt_object to be handled
1995 * \param[in] ldata pointer to the new linkEA data
1997 * \retval positive number for repaired cases
1998 * \retval 0 if nothing to be repaired
1999 * \retval negative error number on failure
2001 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
2002 struct lfsck_component *com,
2003 struct dt_object *obj,
2004 struct linkea_data *ldata)
2006 struct lfsck_instance *lfsck = com->lc_lfsck;
2007 struct dt_device *dev = lfsck_obj2dev(obj);
2008 struct thandle *th = NULL;
2009 struct lu_buf linkea_buf;
2013 th = dt_trans_create(env, dev);
2015 GOTO(log, rc = PTR_ERR(th));
2017 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
2018 ldata->ld_leh->leh_len);
2019 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2020 XATTR_NAME_LINK, 0, th);
2024 rc = dt_trans_start_local(env, dev, th);
2028 dt_write_lock(env, obj, 0);
2029 if (unlikely(lfsck_is_dead_obj(obj)))
2030 GOTO(unlock, rc = 0);
2032 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2033 GOTO(unlock, rc = 1);
2035 rc = dt_xattr_set(env, obj, &linkea_buf,
2036 XATTR_NAME_LINK, 0, th);
2038 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2041 dt_write_unlock(env, obj);
2044 dt_trans_stop(env, dev, th);
2047 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
2048 "object "DFID": rc = %d\n",
2049 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
2052 struct lfsck_namespace *ns = com->lc_file_ram;
2054 ns->ln_flags |= LF_INCONSISTENT;
2061 * Repair invalid name entry.
2063 * If the name entry contains invalid information, such as bad file type
2064 * or (and) corrupted object FID, then either remove the name entry or
2065 * udpate the name entry with the given (right) information.
2067 * \param[in] env pointer to the thread context
2068 * \param[in] com pointer to the lfsck component
2069 * \param[in] parent pointer to the parent directory
2070 * \param[in] child pointer to the object referenced by the name entry
2071 * \param[in] name the old name of the child under the parent directory
2072 * \param[in] name2 the new name of the child under the parent directory
2073 * \param[in] type the type claimed by the name entry
2074 * \param[in] update update the name entry if true; otherwise, remove it
2075 * \param[in] dec decrease the parent nlink count if true
2077 * \retval positive number for repaired successfully
2078 * \retval 0 if nothing to be repaired
2079 * \retval negative error number on failure
2081 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2082 struct lfsck_component *com,
2083 struct dt_object *parent,
2084 struct dt_object *child,
2085 const char *name, const char *name2,
2086 __u16 type, bool update, bool dec)
2088 struct lfsck_thread_info *info = lfsck_env_info(env);
2089 struct dt_insert_rec *rec = &info->lti_dt_rec;
2090 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2091 const struct lu_fid *cfid = lfsck_dto2fid(child);
2093 struct lfsck_instance *lfsck = com->lc_lfsck;
2094 struct dt_device *dev = lfsck->li_next;
2095 struct thandle *th = NULL;
2096 struct lfsck_lock_handle *llh = &info->lti_llh;
2097 struct lustre_handle lh = { 0 };
2101 parent = lfsck_object_locate(dev, parent);
2103 GOTO(log, rc = PTR_ERR(parent));
2105 if (unlikely(!dt_try_as_dir(env, parent)))
2106 GOTO(log, rc = -ENOTDIR);
2108 if (!update || strcmp(name, name2) == 0)
2109 rc = lfsck_lock(env, lfsck, parent, name, llh,
2110 MDS_INODELOCK_UPDATE, LCK_PW);
2112 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2113 MDS_INODELOCK_UPDATE, LCK_PW);
2117 th = dt_trans_create(env, dev);
2119 GOTO(unlock1, rc = PTR_ERR(th));
2121 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2126 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2127 rec->rec_fid = cfid;
2128 rc = dt_declare_insert(env, parent,
2129 (const struct dt_rec *)rec,
2130 (const struct dt_key *)name2, th);
2135 if (dec && S_ISDIR(type)) {
2136 rc = dt_declare_ref_del(env, parent, th);
2141 rc = dt_trans_start_local(env, dev, th);
2146 dt_write_lock(env, parent, 0);
2147 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2148 (const struct dt_key *)name);
2149 /* Someone has removed the bad name entry by race. */
2151 GOTO(unlock2, rc = 0);
2156 /* Someone has removed the bad name entry and reused it for other
2157 * object by race. */
2158 if (!lu_fid_eq(&tfid, cfid))
2159 GOTO(unlock2, rc = 0);
2161 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2162 GOTO(unlock2, rc = 1);
2164 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2169 rc = dt_insert(env, parent,
2170 (const struct dt_rec *)rec,
2171 (const struct dt_key *)name2, th, 1);
2176 if (dec && S_ISDIR(type)) {
2177 rc = dt_ref_del(env, parent, th);
2182 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2185 dt_write_unlock(env, parent);
2188 dt_trans_stop(env, dev, th);
2190 /* We are not sure whether the child will become orphan or not.
2191 * Record it in the LFSCK trace file for further checking in
2192 * the second-stage scanning. */
2193 if (!update && !dec && rc == 0)
2194 lfsck_namespace_trace_update(env, com, cfid,
2195 LNTF_CHECK_LINKEA, true);
2198 /* It is harmless even if unlock the unused lock_handle */
2199 lfsck_ibits_unlock(&lh, LCK_PW);
2203 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2204 "entry for: parent "DFID", child "DFID", name %s, type "
2205 "in name entry %o, type claimed by child %o. repair it "
2206 "by %s with new name2 %s: rc = %d\n",
2207 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2208 name, type, update ? lfsck_object_type(child) : 0,
2209 update ? "updating" : "removing", name2, rc);
2212 struct lfsck_namespace *ns = com->lc_file_ram;
2214 ns->ln_flags |= LF_INCONSISTENT;
2221 * Update the ".." name entry for the given object.
2223 * The object's ".." is corrupted, this function will update the ".." name
2224 * entry with the given pfid, and the linkEA with the given ldata.
2226 * The caller should take the ldlm lock before the calling.
2228 * \param[in] env pointer to the thread context
2229 * \param[in] com pointer to the lfsck component
2230 * \param[in] obj pointer to the dt_object to be handled
2231 * \param[in] pfid the new fid for the object's ".." name entry
2232 * \param[in] cname the name for the @obj in the parent directory
2234 * \retval positive number for repaired cases
2235 * \retval 0 if nothing to be repaired
2236 * \retval negative error number on failure
2238 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2239 struct lfsck_component *com,
2240 struct dt_object *obj,
2241 const struct lu_fid *pfid,
2242 struct lu_name *cname)
2244 struct lfsck_thread_info *info = lfsck_env_info(env);
2245 struct dt_insert_rec *rec = &info->lti_dt_rec;
2246 struct lfsck_instance *lfsck = com->lc_lfsck;
2247 struct dt_device *dev = lfsck_obj2dev(obj);
2248 struct thandle *th = NULL;
2249 struct linkea_data ldata = { NULL };
2250 struct lu_buf linkea_buf;
2254 LASSERT(!dt_object_remote(obj));
2255 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2257 rc = linkea_links_new(&ldata, &info->lti_big_buf, cname, pfid);
2261 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2262 ldata.ld_leh->leh_len);
2264 th = dt_trans_create(env, dev);
2266 GOTO(log, rc = PTR_ERR(th));
2268 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2272 rec->rec_type = S_IFDIR;
2273 rec->rec_fid = pfid;
2274 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2275 (const struct dt_key *)dotdot, th);
2279 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2280 XATTR_NAME_LINK, 0, th);
2284 rc = dt_trans_start_local(env, dev, th);
2288 dt_write_lock(env, obj, 0);
2289 if (unlikely(lfsck_is_dead_obj(obj)))
2290 GOTO(unlock, rc = 0);
2292 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2293 GOTO(unlock, rc = 1);
2295 /* The old ".." name entry maybe not exist. */
2296 dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2298 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2299 (const struct dt_key *)dotdot, th, 1);
2303 rc = lfsck_links_write(env, obj, &ldata, th);
2305 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2308 dt_write_unlock(env, obj);
2311 dt_trans_stop(env, dev, th);
2314 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2315 "the object "DFID", new parent "DFID": rc = %d\n",
2316 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2320 struct lfsck_namespace *ns = com->lc_file_ram;
2322 ns->ln_flags |= LF_INCONSISTENT;
2329 * Handle orphan @obj during Double Scan Directory.
2331 * Remove the @obj's current (invalid) linkEA entries, and insert
2332 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2333 * ${FID}-${PFID}-D-${conflict_version}
2335 * The caller should take the ldlm lock before the calling.
2337 * \param[in] env pointer to the thread context
2338 * \param[in] com pointer to the lfsck component
2339 * \param[in] obj pointer to the orphan object to be handled
2340 * \param[in] pfid the new fid for the object's ".." name entry
2341 * \param[in,out] lh ldlm lock handler for the given @obj
2342 * \param[out] type to tell the caller what the inconsistency is
2344 * \retval positive number for repaired cases
2345 * \retval 0 if nothing to be repaired
2346 * \retval negative error number on failure
2349 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2350 struct lfsck_component *com,
2351 struct dt_object *obj,
2352 const struct lu_fid *pfid,
2353 struct lustre_handle *lh,
2354 enum lfsck_namespace_inconsistency_type *type)
2356 struct lfsck_thread_info *info = lfsck_env_info(env);
2357 struct lfsck_namespace *ns = com->lc_file_ram;
2361 /* Remove the unrecognized linkEA. */
2362 rc = lfsck_namespace_links_remove(env, com, obj);
2363 lfsck_ibits_unlock(lh, LCK_EX);
2364 if (rc < 0 && rc != -ENODATA)
2367 *type = LNIT_MUL_REF;
2369 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2370 * ever tried to verify some remote MDT-object that resides on this
2371 * MDT, but this MDT failed to respond such request. So means there
2372 * may be some remote name entry on other MDT that references this
2373 * object with another name, so we cannot know whether this linkEA
2374 * is valid or not. So keep it there and maybe resolved when next
2376 if (ns->ln_flags & LF_INCOMPLETE)
2379 /* The unique linkEA is invalid, even if the ".." name entry may be
2380 * valid, we still cannot know via which name entry this directory
2381 * will be referenced. Then handle it as pure orphan. */
2382 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2383 "-"DFID, PFID(pfid));
2384 rc = lfsck_namespace_insert_orphan(env, com, obj,
2385 info->lti_tmpbuf, "D", NULL);
2391 * Double Scan Directory object for single linkEA entry case.
2393 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2394 * then check whether the name is in the namespace or not, if not, add the
2395 * missing name entry back to namespace. If the linkEA entry is invalid,
2396 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2399 * \param[in] env pointer to the thread context
2400 * \param[in] com pointer to the lfsck component
2401 * \param[in] child pointer to the directory to be double scanned
2402 * \param[in] pfid the FID corresponding to the ".." entry
2403 * \param[in] ldata pointer to the linkEA data for the given @child
2404 * \param[in,out] lh ldlm lock handler for the given @child
2405 * \param[out] type to tell the caller what the inconsistency is
2406 * \param[in] retry if found inconsistency, but the caller does not hold
2407 * ldlm lock on the @child, then set @retry as true
2408 * \param[in] unknown set if does not know how to repair the inconsistency
2410 * \retval positive number for repaired cases
2411 * \retval 0 if nothing to be repaired
2412 * \retval negative error number on failure
2415 lfsck_namespace_dsd_single(const struct lu_env *env,
2416 struct lfsck_component *com,
2417 struct dt_object *child,
2418 const struct lu_fid *pfid,
2419 struct linkea_data *ldata,
2420 struct lustre_handle *lh,
2421 enum lfsck_namespace_inconsistency_type *type,
2422 bool *retry, bool *unknown)
2424 struct lfsck_thread_info *info = lfsck_env_info(env);
2425 struct lu_name *cname = &info->lti_name;
2426 const struct lu_fid *cfid = lfsck_dto2fid(child);
2428 struct lfsck_namespace *ns = com->lc_file_ram;
2429 struct lfsck_instance *lfsck = com->lc_lfsck;
2430 struct dt_object *parent = NULL;
2431 struct lmv_mds_md_v1 *lmv;
2435 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2437 sizeof(info->lti_key));
2438 /* The unique linkEA entry with bad parent will be handled as orphan. */
2440 if (!lustre_handle_is_used(lh) && retry != NULL)
2443 rc = lfsck_namespace_dsd_orphan(env, com, child,
2449 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2451 GOTO(out, rc = PTR_ERR(parent));
2453 /* We trust the unique linkEA entry in spite of whether it matches the
2454 * ".." name entry or not. Because even if the linkEA entry is wrong
2455 * and the ".." name entry is right, we still cannot know via which
2456 * name entry the child will be referenced, since all known entries
2457 * have been verified during the first-stage scanning. */
2458 if (!dt_object_exists(parent)) {
2459 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2460 * has ever tried to verify some remote MDT-object that resides
2461 * on this MDT, but this MDT failed to respond such request. So
2462 * means there may be some remote name entry on other MDT that
2463 * references this object with another name, so we cannot know
2464 * whether this linkEA is valid or not. So keep it there and
2465 * maybe resolved when next LFSCK run. */
2466 if (ns->ln_flags & LF_INCOMPLETE)
2469 if (!lustre_handle_is_used(lh) && retry != NULL) {
2475 lfsck_ibits_unlock(lh, LCK_EX);
2478 lmv = &info->lti_lmv;
2479 rc = lfsck_read_stripe_lmv(env, child, lmv);
2480 if (rc != 0 && rc != -ENODATA)
2483 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2485 } else if (lfsck_shard_name_to_index(env,
2486 cname->ln_name, cname->ln_namelen,
2487 S_IFDIR, cfid) < 0) {
2488 /* It is an invalid name entry, we
2489 * cannot trust the parent also. */
2490 rc = lfsck_namespace_shrink_linkea(env, com, child,
2491 ldata, cname, &tfid, true);
2495 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2496 "-"DFID, PFID(pfid));
2497 rc = lfsck_namespace_insert_orphan(env, com, child,
2498 info->lti_tmpbuf, "S", NULL);
2503 /* Create the lost parent as an orphan. */
2504 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2506 /* Add the missing name entry to the parent. */
2507 rc = lfsck_namespace_insert_normal(env, com, parent,
2508 child, cname->ln_name);
2509 if (unlikely(rc == -EEXIST)) {
2510 /* Unfortunately, someone reused the name
2511 * under the parent by race. So we have
2512 * to remove the linkEA entry from
2513 * current child object. It means that the
2514 * LFSCK cannot recover the system
2515 * totally back to its original status,
2516 * but it is necessary to make the
2517 * current system to be consistent. */
2518 rc = lfsck_namespace_shrink_linkea(env,
2520 cname, &tfid, true);
2522 snprintf(info->lti_tmpbuf,
2523 sizeof(info->lti_tmpbuf),
2524 "-"DFID, PFID(pfid));
2525 rc = lfsck_namespace_insert_orphan(env,
2526 com, child, info->lti_tmpbuf,
2533 } /* !dt_object_exists(parent) */
2535 /* The unique linkEA entry with bad parent will be handled as orphan. */
2536 if (unlikely(!dt_try_as_dir(env, parent))) {
2537 if (!lustre_handle_is_used(lh) && retry != NULL)
2540 rc = lfsck_namespace_dsd_orphan(env, com, child,
2546 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2547 (const struct dt_key *)cname->ln_name);
2548 if (rc == -ENOENT) {
2549 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2550 * has ever tried to verify some remote MDT-object that resides
2551 * on this MDT, but this MDT failed to respond such request. So
2552 * means there may be some remote name entry on other MDT that
2553 * references this object with another name, so we cannot know
2554 * whether this linkEA is valid or not. So keep it there and
2555 * maybe resolved when next LFSCK run. */
2556 if (ns->ln_flags & LF_INCOMPLETE)
2559 if (!lustre_handle_is_used(lh) && retry != NULL) {
2565 lfsck_ibits_unlock(lh, LCK_EX);
2566 rc = lfsck_namespace_check_name(env, parent, child, cname);
2573 /* It is an invalid name entry, drop it. */
2574 if (unlikely(rc > 0)) {
2575 rc = lfsck_namespace_shrink_linkea(env, com, child,
2576 ldata, cname, &tfid, true);
2578 snprintf(info->lti_tmpbuf,
2579 sizeof(info->lti_tmpbuf),
2580 "-"DFID, PFID(pfid));
2581 rc = lfsck_namespace_insert_orphan(env, com,
2582 child, info->lti_tmpbuf, "D", NULL);
2588 /* Add the missing name entry back to the namespace. */
2589 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2591 if (unlikely(rc == -ESTALE))
2592 /* It may happen when the remote object has been
2593 * removed, but the local MDT is not aware of that. */
2596 if (unlikely(rc == -EEXIST)) {
2597 /* Unfortunately, someone reused the name under the
2598 * parent by race. So we have to remove the linkEA
2599 * entry from current child object. It means that the
2600 * LFSCK cannot recover the system totally back to
2601 * its original status, but it is necessary to make
2602 * the current system to be consistent.
2604 * It also may be because of the LFSCK found some
2605 * internal status of create operation. Under such
2606 * case, nothing to be done. */
2607 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2608 parent, child, ldata, cname, &tfid);
2610 snprintf(info->lti_tmpbuf,
2611 sizeof(info->lti_tmpbuf),
2612 "-"DFID, PFID(pfid));
2613 rc = lfsck_namespace_insert_orphan(env, com,
2614 child, info->lti_tmpbuf, "D", NULL);
2619 } /* rc == -ENOENT */
2624 if (!lu_fid_eq(&tfid, cfid)) {
2625 if (!lustre_handle_is_used(lh) && retry != NULL) {
2631 lfsck_ibits_unlock(lh, LCK_EX);
2632 /* The name entry references another MDT-object that
2633 * may be created by the LFSCK for repairing dangling
2634 * name entry. Try to replace it. */
2635 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2638 rc = lfsck_namespace_dsd_orphan(env, com, child,
2644 /* Zero FID may because the remote directroy object has invalid linkEA,
2645 * or lost linkEA. Under such case, the LFSCK on this MDT does not know
2646 * how to repair the inconsistency, but the namespace LFSCK on the MDT
2647 * where its name entry resides may has more information (name, FID) to
2648 * repair such inconsistency. So here, keep the inconsistency to avoid
2649 * some imporper repairing. */
2650 if (fid_is_zero(pfid)) {
2657 /* The ".." name entry is wrong, update it. */
2658 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2659 if (!lustre_handle_is_used(lh) && retry != NULL) {
2665 *type = LNIT_UNMATCHED_PAIRS;
2666 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2667 lfsck_dto2fid(parent), cname);
2673 if (parent != NULL && !IS_ERR(parent))
2674 lfsck_object_put(env, parent);
2680 * Double Scan Directory object for multiple linkEA entries case.
2682 * The given @child has multiple linkEA entries. There is at most one linkEA
2683 * entry will be valid, all the others will be removed. Firstly, the function
2684 * will try to find out the linkEA entry for which the name entry exists under
2685 * the given parent (@pfid). If there is no linkEA entry that matches the given
2686 * ".." name entry, then tries to find out the first linkEA entry that both the
2687 * parent and the name entry exist to rebuild a new ".." name entry.
2689 * \param[in] env pointer to the thread context
2690 * \param[in] com pointer to the lfsck component
2691 * \param[in] child pointer to the directory to be double scanned
2692 * \param[in] pfid the FID corresponding to the ".." entry
2693 * \param[in] ldata pointer to the linkEA data for the given @child
2694 * \param[in,out] lh ldlm lock handler for the given @child
2695 * \param[out] type to tell the caller what the inconsistency is
2696 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2697 * \param[in] unknown set if does not know how to repair the inconsistency
2699 * \retval positive number for repaired cases
2700 * \retval 0 if nothing to be repaired
2701 * \retval negative error number on failure
2704 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2705 struct lfsck_component *com,
2706 struct dt_object *child,
2707 const struct lu_fid *pfid,
2708 struct linkea_data *ldata,
2709 struct lustre_handle *lh,
2710 enum lfsck_namespace_inconsistency_type *type,
2711 bool lpf, bool *unknown)
2713 struct lfsck_thread_info *info = lfsck_env_info(env);
2714 struct lu_name *cname = &info->lti_name;
2715 const struct lu_fid *cfid = lfsck_dto2fid(child);
2716 struct lu_fid *pfid2 = &info->lti_fid3;
2718 struct lfsck_namespace *ns = com->lc_file_ram;
2719 struct lfsck_instance *lfsck = com->lc_lfsck;
2720 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2721 struct dt_object *parent = NULL;
2722 struct linkea_data ldata_new = { NULL };
2723 int dirent_count = 0;
2729 while (ldata->ld_lee != NULL) {
2730 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2732 sizeof(info->lti_key));
2733 /* Drop invalid linkEA entry. */
2735 lfsck_linkea_del_buf(ldata, cname);
2739 /* Drop repeated linkEA entries. */
2740 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2742 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2743 * then it is possible that: the directry object has ever
2744 * been lost, but its name entry was there. In the former
2745 * LFSCK run, during the first-stage scanning, the LFSCK
2746 * found the dangling name entry, but it did not recreate
2747 * the lost object, and when moved to the second-stage
2748 * scanning, some children objects of the lost directory
2749 * object were found, then the LFSCK recreated such lost
2750 * directory object as an orphan.
2752 * When the LFSCK runs again, if the dangling name is still
2753 * there, the LFSCK should move the orphan directory object
2754 * back to the normal namespace. */
2755 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2756 linkea_next_entry(ldata);
2760 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2762 RETURN(PTR_ERR(parent));
2764 if (!dt_object_exists(parent)) {
2765 lfsck_object_put(env, parent);
2766 if (ldata->ld_leh->leh_reccount > 1) {
2767 /* If it is NOT the last linkEA entry, then
2768 * there is still other chance to make the
2769 * child to be visible via other parent, then
2770 * remove this linkEA entry. */
2771 lfsck_linkea_del_buf(ldata, cname);
2778 /* The linkEA entry with bad parent will be removed. */
2779 if (unlikely(!dt_try_as_dir(env, parent))) {
2780 lfsck_object_put(env, parent);
2781 lfsck_linkea_del_buf(ldata, cname);
2785 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2786 (const struct dt_key *)cname->ln_name);
2787 *pfid2 = *lfsck_dto2fid(parent);
2788 if (rc == -ENOENT) {
2789 lfsck_object_put(env, parent);
2790 linkea_next_entry(ldata);
2795 lfsck_object_put(env, parent);
2800 if (lu_fid_eq(&tfid, cfid)) {
2801 lfsck_object_put(env, parent);
2802 /* If the parent (that is declared via linkEA entry)
2803 * directory contains the specified child, but such
2804 * parent does not match the dotdot name entry, then
2805 * trust the linkEA. */
2806 if (!lu_fid_eq(pfid, pfid2)) {
2807 *type = LNIT_UNMATCHED_PAIRS;
2808 rc = lfsck_namespace_repair_unmatched_pairs(env,
2809 com, child, pfid2, cname);
2815 /* It is the most common case that we find the
2816 * name entry corresponding to the linkEA entry
2817 * that matches the ".." name entry. */
2818 rc = linkea_links_new(&ldata_new, &info->lti_big_buf,
2823 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2828 lfsck_linkea_del_buf(ldata, cname);
2829 linkea_first_entry(ldata);
2830 /* There may be some invalid dangling name entries under
2831 * other parent directories, remove all of them. */
2832 while (ldata->ld_lee != NULL) {
2833 rc = lfsck_namespace_unpack_linkea_entry(ldata,
2834 cname, &tfid, info->lti_key,
2835 sizeof(info->lti_key));
2839 parent = lfsck_object_find_bottom(env, lfsck,
2841 if (IS_ERR(parent)) {
2842 rc = PTR_ERR(parent);
2843 if (rc != -ENOENT &&
2844 bk->lb_param & LPF_FAILOUT)
2850 if (!dt_object_exists(parent)) {
2851 lfsck_object_put(env, parent);
2855 rc = lfsck_namespace_repair_dirent(env, com,
2856 parent, child, cname->ln_name,
2857 cname->ln_name, S_IFDIR, false, true);
2858 lfsck_object_put(env, parent);
2860 if (bk->lb_param & LPF_FAILOUT)
2869 lfsck_linkea_del_buf(ldata, cname);
2872 ns->ln_dirent_repaired += dirent_count;
2875 } /* lu_fid_eq(&tfid, lfsck_dto2fid(child)) */
2877 lfsck_ibits_unlock(lh, LCK_EX);
2878 /* The name entry references another MDT-object that may be
2879 * created by the LFSCK for repairing dangling name entry.
2880 * Try to replace it. */
2881 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2883 lfsck_object_put(env, parent);
2890 lfsck_linkea_del_buf(ldata, cname);
2891 } /* while (ldata->ld_lee != NULL) */
2893 /* If there is still linkEA overflow, return. */
2894 if (unlikely(ldata->ld_leh->leh_overflow_time))
2897 linkea_first_entry(ldata);
2898 if (ldata->ld_leh->leh_reccount == 1) {
2899 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2900 lh, type, NULL, unknown);
2905 /* All linkEA entries are invalid and removed, then handle the @child
2907 if (ldata->ld_leh->leh_reccount == 0) {
2908 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2914 /* If the dangling name entry for the orphan directory object has
2915 * been remvoed, then just check whether the directory object is
2916 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2922 /* There is no linkEA entry that matches the ".." name entry. Find
2923 * the first linkEA entry that both parent and name entry exist to
2924 * rebuild a new ".." name entry. */
2934 * Repair the object's nlink attribute.
2936 * If all the known name entries have been verified, then the object's hard
2937 * link attribute should match the object's linkEA entries count unless the
2938 * object's has too many hard link to be recorded in the linkEA. Such cases
2939 * should have been marked in the LFSCK trace file. Otherwise, trust the
2940 * linkEA to update the object's nlink attribute.
2942 * \param[in] env pointer to the thread context
2943 * \param[in] com pointer to the lfsck component
2944 * \param[in] obj pointer to the dt_object to be handled
2945 * \param[in,out] la pointer to buffer to object's attribute before
2946 * and after the repairing
2948 * \retval positive number for repaired cases
2949 * \retval 0 if nothing to be repaired
2950 * \retval negative error number on failure
2952 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2953 struct lfsck_component *com,
2954 struct dt_object *obj,
2957 struct lfsck_namespace *ns = com->lc_file_ram;
2958 struct lfsck_instance *lfsck = com->lc_lfsck;
2959 struct dt_device *dev = lfsck_obj2dev(obj);
2960 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2961 struct thandle *th = NULL;
2962 struct linkea_data ldata = { NULL };
2963 struct lustre_handle lh = { 0 };
2964 __u32 old = la->la_nlink;
2968 LASSERT(!dt_object_remote(obj));
2970 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2971 MDS_INODELOCK_UPDATE, LCK_PW);
2975 th = dt_trans_create(env, dev);
2977 GOTO(log, rc = PTR_ERR(th));
2979 la->la_valid = LA_NLINK;
2980 rc = dt_declare_attr_set(env, obj, la, th);
2984 rc = dt_trans_start_local(env, dev, th);
2988 dt_write_lock(env, obj, 0);
2989 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2990 * ever tried to verify some remote MDT-object that resides on this
2991 * MDT, but this MDT failed to respond such request. So means there
2992 * may be some remote name entry on other MDT that references this
2993 * object with another name, so we cannot know whether this linkEA
2994 * is valid or not. So keep it there and maybe resolved when next
2996 if (ns->ln_flags & LF_INCOMPLETE)
2997 GOTO(unlock, rc = 0);
2999 rc = dt_attr_get(env, obj, la);
3001 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
3003 rc = lfsck_links_read2_with_rec(env, obj, &ldata);
3005 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
3007 /* XXX: Currently, we only update the nlink attribute if the known
3008 * linkEA entries is larger than the nlink attribute. That is
3010 if (la->la_nlink >= ldata.ld_leh->leh_reccount ||
3011 unlikely(la->la_nlink == 0 ||
3012 ldata.ld_leh->leh_overflow_time))
3013 GOTO(unlock, rc = 0);
3015 la->la_nlink = ldata.ld_leh->leh_reccount;
3016 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3017 GOTO(unlock, rc = 1);
3019 rc = dt_attr_set(env, obj, la, th);
3021 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
3024 dt_write_unlock(env, obj);
3027 dt_trans_stop(env, dev, th);
3030 lfsck_ibits_unlock(&lh, LCK_PW);
3031 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
3032 "nlink count from %u to %u: rc = %d\n",
3033 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
3036 ns->ln_flags |= LF_INCONSISTENT;
3042 * Double scan the directory object for namespace LFSCK.
3044 * This function will verify the <parent, child> pairs in the namespace tree:
3045 * the parent references the child via some name entry that should be in the
3046 * child's linkEA entry, the child should back references the parent via its
3049 * The LFSCK will scan every linkEA entry in turn until find out the first
3050 * matched pairs. If found, then all other linkEA entries will be dropped.
3051 * If all the linkEA entries cannot match the ".." name entry, then there
3052 * are serveral possible cases:
3054 * 1) If there is only one linkEA entry, then trust it as long as the PFID
3055 * in the linkEA entry is valid.
3057 * 2) If there are multiple linkEA entries, then try to find the linkEA
3058 * that matches the ".." name entry. If found, then all other entries
3059 * are invalid; otherwise, it is quite possible that the ".." name entry
3060 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
3061 * entry according to the first valid linkEA entry (both the parent and
3062 * the name entry should exist).
3064 * 3) If the directory object has no (valid) linkEA entry, then the
3065 * directory object will be handled as pure orphan and inserted
3066 * in the .lustre/lost+found/MDTxxxx/ with the name:
3067 * ${self_FID}-${PFID}-D-${conflict_version}
3069 * \param[in] env pointer to the thread context
3070 * \param[in] com pointer to the lfsck component
3071 * \param[in] child pointer to the directory object to be handled
3072 * \param[in] flags to indicate the specical checking on the @child
3074 * \retval positive number for repaired cases
3075 * \retval 0 if nothing to be repaired
3076 * \retval negative error number on failure
3078 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3079 struct lfsck_component *com,
3080 struct dt_object *child, __u8 flags)
3082 struct lfsck_thread_info *info = lfsck_env_info(env);
3083 const struct lu_fid *cfid = lfsck_dto2fid(child);
3084 struct lu_fid *pfid = &info->lti_fid2;
3085 struct lfsck_namespace *ns = com->lc_file_ram;
3086 struct lfsck_instance *lfsck = com->lc_lfsck;
3087 struct lustre_handle lh = { 0 };
3088 struct linkea_data ldata = { NULL };
3089 bool unknown = false;
3092 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3096 LASSERT(!dt_object_remote(child));
3098 if (flags & LNTF_UNCERTAIN_LMV) {
3099 if (flags & LNTF_RECHECK_NAME_HASH) {
3100 rc = lfsck_namespace_scan_shard(env, com, child);
3104 ns->ln_striped_shards_scanned++;
3106 ns->ln_striped_shards_skipped++;
3110 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3114 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3115 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3116 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3117 "the namespace LFSCK, then the LFSCK cannot guarantee"
3118 "all the name entries have been verified in first-stage"
3119 "scanning. So have to skip orphan related handling for"
3120 "the directory object "DFID" with remote name entry\n",
3121 lfsck_lfsck2name(lfsck), PFID(cfid));
3126 if (unlikely(!dt_try_as_dir(env, child)))
3127 GOTO(out, rc = -ENOTDIR);
3129 /* We only take ldlm lock on the @child when required. When the
3130 * logic comes here for the first time, it is always false. */
3134 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3135 MDS_INODELOCK_UPDATE |
3136 MDS_INODELOCK_XATTR, LCK_EX);
3141 dt_read_lock(env, child, 0);
3142 if (unlikely(lfsck_is_dead_obj(child))) {
3143 dt_read_unlock(env, child);
3148 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3149 (const struct dt_key *)dotdot);
3151 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3152 dt_read_unlock(env, child);
3157 if (!lustre_handle_is_used(&lh)) {
3158 dt_read_unlock(env, child);
3163 } else if (lfsck->li_lpf_obj != NULL &&
3164 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3166 } else if (unlikely(!fid_is_sane(pfid))) {
3170 rc = lfsck_links_read(env, child, &ldata);
3171 dt_read_unlock(env, child);
3173 if (rc != -ENODATA && rc != -EINVAL)
3176 if (!lustre_handle_is_used(&lh))
3179 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3180 /* Remove the corrupted linkEA. */
3181 rc = lfsck_namespace_links_remove(env, com, child);
3183 /* Here, because of the crashed linkEA, we
3184 * cannot know whether there is some parent
3185 * that references the child directory via
3186 * some name entry or not. So keep it there,
3187 * when the LFSCK run next time, if there is
3188 * some parent that references this object,
3189 * then the LFSCK can rebuild the linkEA;
3190 * otherwise, this object will be handled
3191 * as orphan as above. */
3194 /* 1. If we have neither ".." nor linkEA,
3195 * then it is an orphan.
3197 * 2. If we only have the ".." name entry,
3198 * but no parent references this child
3199 * directory, then handle it as orphan. */
3200 lfsck_ibits_unlock(&lh, LCK_EX);
3201 type = LNIT_MUL_REF;
3203 /* If the LFSCK is marked as LF_INCOMPLETE,
3204 * then means some MDT has ever tried to
3205 * verify some remote MDT-object that resides
3206 * on this MDT, but this MDT failed to respond
3207 * such request. So means there may be some
3208 * remote name entry on other MDT that
3209 * references this object with another name,
3210 * so we cannot know whether this linkEA is
3211 * valid or not. So keep it there and maybe
3212 * resolved when next LFSCK run. */
3213 if (ns->ln_flags & LF_INCOMPLETE)
3216 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3217 "-"DFID, PFID(pfid));
3218 rc = lfsck_namespace_insert_orphan(env, com, child,
3219 info->lti_tmpbuf, "D", NULL);
3225 linkea_first_entry(&ldata);
3226 /* This is the most common case: the object has unique linkEA entry. */
3227 if (ldata.ld_leh->leh_reccount == 1) {
3228 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3229 &lh, &type, &retry, &unknown);
3231 LASSERT(!lustre_handle_is_used(&lh));
3240 if (!lustre_handle_is_used(&lh))
3243 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3244 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3250 /* When we come here, the cases usually like that:
3251 * 1) The directory object has a corrupted linkEA entry. During the
3252 * first-stage scanning, the LFSCK cannot know such corruption,
3253 * then it appends the right linkEA entry according to the found
3254 * name entry after the bad one.
3256 * 2) The directory object has a right linkEA entry. During the
3257 * first-stage scanning, the LFSCK finds some bad name entry,
3258 * but the LFSCK cannot aware that at that time, then it adds
3259 * the bad linkEA entry for further processing. */
3260 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3261 &lh, &type, lpf, &unknown);
3266 lfsck_ibits_unlock(&lh, LCK_EX);
3269 case LNIT_BAD_LINKEA:
3270 ns->ln_linkea_repaired++;
3272 case LNIT_UNMATCHED_PAIRS:
3273 ns->ln_unmatched_pairs_repaired++;
3276 ns->ln_mul_ref_repaired++;
3284 ns->ln_unknown_inconsistency++;
3289 #define lfsck_time_before(a, b) \
3290 (typecheck(__u32, a) && \
3291 typecheck(__u32, b) && \
3292 ((int)(a) - (int)(b) < 0))
3295 lfsck_namespace_linkea_stale_overflow(struct linkea_data *ldata,
3296 struct lfsck_namespace *ns)
3298 /* Both the leh_overflow_time and ln_time_latest_reset are
3299 * local time based, so need NOT to care about clock drift
3300 * among the servers. */
3301 return ldata->ld_leh->leh_overflow_time &&
3302 lfsck_time_before(ldata->ld_leh->leh_overflow_time,
3303 ns->ln_time_latest_reset);
3307 * Clear the object's linkEA overflow timestamp.
3309 * If the MDT-object has too many hard links as to the linkEA cannot hold
3310 * all of them, then overflow timestamp will be set in the linkEA header.
3311 * If some hard links are removed after that, then it is possible to hold
3312 * other missed linkEA entries. If the namespace LFSCK have added all the
3313 * related linkEA entries, then it will remove the overflow timestamp.
3315 * \param[in] env pointer to the thread context
3316 * \param[in] com pointer to the lfsck component
3317 * \param[in] ldata pointer to the linkEA data for the given @obj
3318 * \param[in] obj pointer to the dt_object to be handled
3320 * \retval positive number for repaired cases
3321 * \retval 0 if nothing to be repaired
3322 * \retval negative error number on failure
3324 static int lfsck_namespace_linkea_clear_overflow(const struct lu_env *env,
3325 struct lfsck_component *com,
3326 struct linkea_data *ldata,
3327 struct dt_object *obj)
3329 struct lfsck_namespace *ns = com->lc_file_ram;
3330 struct lfsck_instance *lfsck = com->lc_lfsck;
3331 struct dt_device *dev = lfsck_obj2dev(obj);
3332 struct thandle *th = NULL;
3333 struct lustre_handle lh = { 0 };
3334 struct lu_buf linkea_buf;
3338 LASSERT(!dt_object_remote(obj));
3340 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3341 MDS_INODELOCK_UPDATE, LCK_PW);
3345 th = dt_trans_create(env, dev);
3347 GOTO(log, rc = PTR_ERR(th));
3349 rc = dt_declare_xattr_set(env, obj,
3350 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
3351 XATTR_NAME_LINK, 0, th);
3355 rc = dt_trans_start_local(env, dev, th);
3359 dt_write_lock(env, obj, 0);
3360 rc = lfsck_links_read(env, obj, ldata);
3364 if (unlikely(!lfsck_namespace_linkea_stale_overflow(ldata, ns)))
3365 GOTO(unlock, rc = 0);
3367 ldata->ld_leh->leh_overflow_time = 0;
3368 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3369 GOTO(unlock, rc = 1);
3371 /* If all known entries are in the linkEA, then the 'leh_reccount'
3372 * should NOT be zero. */
3373 LASSERT(ldata->ld_leh->leh_reccount > 0);
3375 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
3376 ldata->ld_leh->leh_len);
3377 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK, 0, th);
3378 if (unlikely(rc == -ENOSPC))
3386 dt_write_unlock(env, obj);
3389 dt_trans_stop(env, dev, th);
3392 lfsck_ibits_unlock(&lh, LCK_PW);
3393 CDEBUG(D_LFSCK, "%s: clear linkea overflow timestamp for the object "
3395 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
3401 * Double scan the MDT-object for namespace LFSCK.
3403 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3404 * those entries from the linkEA; if the linkEA becomes empty or the object
3405 * has no linkEA, then it is an orphan and will be added into the directory
3406 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3407 * the remote parent; if the name entry corresponding to some linkEA entry
3408 * is lost, then add the name entry back to the namespace.
3410 * \param[in] env pointer to the thread context
3411 * \param[in] com pointer to the lfsck component
3412 * \param[in] child pointer to the dt_object to be handled
3413 * \param[in] flags some hints to indicate how the @child should be handled
3415 * \retval positive number for repaired cases
3416 * \retval 0 if nothing to be repaired
3417 * \retval negative error number on failure
3419 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3420 struct lfsck_component *com,
3421 struct dt_object *child, __u8 flags)
3423 struct lfsck_thread_info *info = lfsck_env_info(env);
3424 struct lu_attr *la = &info->lti_la;
3425 struct lu_name *cname = &info->lti_name;
3426 struct lu_fid *pfid = &info->lti_fid;
3427 struct lu_fid *cfid = &info->lti_fid2;
3428 struct lfsck_instance *lfsck = com->lc_lfsck;
3429 struct lfsck_namespace *ns = com->lc_file_ram;
3430 struct dt_object *parent = NULL;
3431 struct linkea_data ldata = { NULL };
3432 bool repaired = false;
3437 dt_read_lock(env, child, 0);
3438 if (unlikely(lfsck_is_dead_obj(child))) {
3439 dt_read_unlock(env, child);
3444 if (S_ISDIR(lfsck_object_type(child))) {
3445 dt_read_unlock(env, child);
3446 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3451 rc = lfsck_links_read(env, child, &ldata);
3452 dt_read_unlock(env, child);
3454 if (rc == -EINVAL) {
3455 struct lustre_handle lh = { 0 };
3457 rc = lfsck_ibits_lock(env, com->lc_lfsck, child, &lh,
3458 MDS_INODELOCK_UPDATE |
3459 MDS_INODELOCK_XATTR, LCK_EX);
3461 rc = lfsck_namespace_links_remove(env, com, child);
3462 lfsck_ibits_unlock(&lh, LCK_EX);
3471 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3472 unlikely(lfsck_namespace_linkea_stale_overflow(&ldata, ns))) {
3473 rc = lfsck_namespace_linkea_clear_overflow(env, com, &ldata,
3479 ns->ln_linkea_overflow_cleared++;
3482 linkea_first_entry(&ldata);
3483 while (ldata.ld_lee != NULL) {
3484 rc = lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3486 sizeof(info->lti_key));
3487 /* Invalid PFID in the linkEA entry. */
3489 rc = lfsck_namespace_shrink_linkea(env, com, child,
3490 &ldata, cname, pfid, true);
3500 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3502 /* Found repeated linkEA entries */
3504 rc = lfsck_namespace_shrink_linkea(env, com, child,
3505 &ldata, cname, pfid, false);
3517 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3519 GOTO(out, rc = PTR_ERR(parent));
3521 if (!dt_object_exists(parent)) {
3524 if (ldata.ld_leh->leh_reccount > 1) {
3525 /* If it is NOT the last linkEA entry, then
3526 * there is still other chance to make the
3527 * child to be visible via other parent, then
3528 * remove this linkEA entry. */
3529 rc = lfsck_namespace_shrink_linkea(env, com,
3530 child, &ldata, cname, pfid, true);
3532 /* If the LFSCK is marked as LF_INCOMPLETE,
3533 * then means some MDT has ever tried to
3534 * verify some remote MDT-object that resides
3535 * on this MDT, but this MDT failed to respond
3536 * such request. So means there may be some
3537 * remote name entry on other MDT that
3538 * references this object with another name,
3539 * so we cannot know whether this linkEA is
3540 * valid or not. So keep it there and maybe
3541 * resolved when next LFSCK run. */
3542 if (ns->ln_flags & LF_INCOMPLETE) {
3543 lfsck_object_put(env, parent);
3548 /* Create the lost parent as an orphan. */
3549 rc = lfsck_namespace_create_orphan_dir(env, com,
3552 lfsck_object_put(env, parent);
3560 /* Add the missing name entry to the parent. */
3561 rc = lfsck_namespace_insert_normal(env, com,
3562 parent, child, cname->ln_name);
3563 if (unlikely(rc == -EEXIST))
3564 /* Unfortunately, someone reused the
3565 * name under the parent by race. So we
3566 * have to remove the linkEA entry from
3567 * current child object. It means that
3568 * the LFSCK cannot recover the system
3569 * totally back to its original status,
3570 * but it is necessary to make the
3571 * current system to be consistent. */
3572 rc = lfsck_namespace_shrink_linkea(env,
3576 linkea_next_entry(&ldata);
3579 lfsck_object_put(env, parent);
3587 } /* !dt_object_exists(parent) */
3589 /* The linkEA entry with bad parent will be removed. */
3590 if (unlikely(!dt_try_as_dir(env, parent))) {
3591 lfsck_object_put(env, parent);
3592 rc = lfsck_namespace_shrink_linkea(env, com, child,
3593 &ldata, cname, pfid, true);
3603 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3604 (const struct dt_key *)cname->ln_name);
3605 if (rc != 0 && rc != -ENOENT) {
3606 lfsck_object_put(env, parent);
3612 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3613 /* It is the most common case that we
3614 * find the name entry corresponding
3615 * to the linkEA entry. */
3616 lfsck_object_put(env, parent);
3617 linkea_next_entry(&ldata);
3619 /* The name entry references another
3620 * MDT-object that may be created by
3621 * the LFSCK for repairing dangling
3622 * name entry. Try to replace it. */
3623 rc = lfsck_namespace_replace_cond(env, com,
3624 parent, child, cfid, cname);
3625 lfsck_object_put(env, parent);
3631 linkea_next_entry(&ldata);
3633 rc = lfsck_namespace_shrink_linkea(env,
3647 /* The following handles -ENOENT case */
3649 rc = dt_attr_get(env, child, la);
3653 /* If there is no name entry in the parent dir and the object
3654 * link count is fewer than the linkea entries count, then the
3655 * linkea entry should be removed. */
3656 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3657 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3658 parent, child, &ldata, cname, pfid);
3659 lfsck_object_put(env, parent);
3669 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3670 * MDT has ever tried to verify some remote MDT-object that
3671 * resides on this MDT, but this MDT failed to respond such
3672 * request. So means there may be some remote name entry on
3673 * other MDT that references this object with another name,
3674 * so we cannot know whether this linkEA is valid or not.
3675 * So keep it there and maybe resolved when next LFSCK run. */
3676 if (ns->ln_flags & LF_INCOMPLETE) {
3677 lfsck_object_put(env, parent);
3682 rc = lfsck_namespace_check_name(env, parent, child, cname);
3687 lfsck_object_put(env, parent);
3692 /* It is an invalid name entry, drop it. */
3693 if (unlikely(rc > 0)) {
3694 lfsck_object_put(env, parent);
3695 rc = lfsck_namespace_shrink_linkea(env, com, child,
3696 &ldata, cname, pfid, true);
3706 /* Add the missing name entry back to the namespace. */
3707 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3709 if (unlikely(rc == -ESTALE))
3710 /* It may happen when the remote object has been
3711 * removed, but the local MDT is not aware of that. */
3714 if (unlikely(rc == -EEXIST))
3715 /* Unfortunately, someone reused the name under the
3716 * parent by race. So we have to remove the linkEA
3717 * entry from current child object. It means that the
3718 * LFSCK cannot recover the system totally back to
3719 * its original status, but it is necessary to make
3720 * the current system to be consistent.
3722 * It also may be because of the LFSCK found some
3723 * internal status of create operation. Under such
3724 * case, nothing to be done. */
3725 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3726 parent, child, &ldata, cname, pfid);
3728 linkea_next_entry(&ldata);
3730 lfsck_object_put(env, parent);
3741 if (rc < 0 && rc != -ENODATA)
3744 if (rc == 0 && ldata.ld_leh != NULL)
3745 count = ldata.ld_leh->leh_reccount;
3748 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3749 * MDT has ever tried to verify some remote MDT-object that
3750 * resides on this MDT, but this MDT failed to respond such
3751 * request. So means there may be some remote name entry on
3752 * other MDT that references this object with another name,
3753 * so we cannot know whether this linkEA is valid or not.
3754 * So keep it there and maybe resolved when next LFSCK run. */
3755 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3756 (ldata.ld_leh == NULL ||
3757 !ldata.ld_leh->leh_overflow_time)) {
3758 /* If the child becomes orphan, then insert it into
3759 * the global .lustre/lost+found/MDTxxxx directory. */
3760 rc = lfsck_namespace_insert_orphan(env, com, child,
3766 ns->ln_mul_ref_repaired++;
3771 rc = dt_attr_get(env, child, la);
3775 if (la->la_nlink != 0 && la->la_nlink != count) {
3776 if (unlikely(!S_ISREG(lfsck_object_type(child)) &&
3777 !S_ISLNK(lfsck_object_type(child)))) {
3778 CDEBUG(D_LFSCK, "%s: namespace LFSCK finds "
3779 "the object "DFID"'s nlink count %d "
3780 "does not match linkEA count %d, "
3781 "type %o, skip it.\n",
3782 lfsck_lfsck2name(lfsck),
3783 PFID(lfsck_dto2fid(child)),
3784 la->la_nlink, count,
3785 lfsck_object_type(child));
3786 } else if (la->la_nlink < count &&
3787 likely(!ldata.ld_leh->leh_overflow_time)) {
3788 rc = lfsck_namespace_repair_nlink(env, com,
3791 ns->ln_objs_nlink_repaired++;
3799 if (la->la_nlink > 1)
3800 ns->ln_mul_linked_repaired++;
3809 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3810 struct lfsck_namespace *ns,
3811 __u64 checked_phase1,
3812 __u64 checked_phase2,
3816 seq_printf(m, "checked_phase1: %llu\n"
3817 "checked_phase2: %llu\n"
3818 "updated_phase1: %llu\n"
3819 "updated_phase2: %llu\n"
3820 "failed_phase1: %llu\n"
3821 "failed_phase2: %llu\n"
3822 "directories: %llu\n"
3823 "dirent_repaired: %llu\n"
3824 "linkea_repaired: %llu\n"
3825 "nlinks_repaired: %llu\n"
3826 "multiple_linked_checked: %llu\n"
3827 "multiple_linked_repaired: %llu\n"
3828 "unknown_inconsistency: %llu\n"
3829 "unmatched_pairs_repaired: %llu\n"
3830 "dangling_repaired: %llu\n"
3831 "multiple_referenced_repaired: %llu\n"
3832 "bad_file_type_repaired: %llu\n"
3833 "lost_dirent_repaired: %llu\n"
3834 "local_lost_found_scanned: %llu\n"
3835 "local_lost_found_moved: %llu\n"
3836 "local_lost_found_skipped: %llu\n"
3837 "local_lost_found_failed: %llu\n"
3838 "striped_dirs_scanned: %llu\n"
3839 "striped_dirs_repaired: %llu\n"
3840 "striped_dirs_failed: %llu\n"
3841 "striped_dirs_disabled: %llu\n"
3842 "striped_dirs_skipped: %llu\n"
3843 "striped_shards_scanned: %llu\n"
3844 "striped_shards_repaired: %llu\n"
3845 "striped_shards_failed: %llu\n"
3846 "striped_shards_skipped: %llu\n"
3847 "name_hash_repaired: %llu\n"
3848 "linkea_overflow_cleared: %llu\n"
3849 "success_count: %u\n"
3850 "run_time_phase1: %u seconds\n"
3851 "run_time_phase2: %u seconds\n",
3854 ns->ln_items_repaired,
3855 ns->ln_objs_repaired_phase2,
3856 ns->ln_items_failed,
3857 ns->ln_objs_failed_phase2,
3858 ns->ln_dirs_checked,
3859 ns->ln_dirent_repaired,
3860 ns->ln_linkea_repaired,
3861 ns->ln_objs_nlink_repaired,
3862 ns->ln_mul_linked_checked,
3863 ns->ln_mul_linked_repaired,
3864 ns->ln_unknown_inconsistency,
3865 ns->ln_unmatched_pairs_repaired,
3866 ns->ln_dangling_repaired,
3867 ns->ln_mul_ref_repaired,
3868 ns->ln_bad_type_repaired,
3869 ns->ln_lost_dirent_repaired,
3870 ns->ln_local_lpf_scanned,
3871 ns->ln_local_lpf_moved,
3872 ns->ln_local_lpf_skipped,
3873 ns->ln_local_lpf_failed,
3874 ns->ln_striped_dirs_scanned,
3875 ns->ln_striped_dirs_repaired,
3876 ns->ln_striped_dirs_failed,
3877 ns->ln_striped_dirs_disabled,
3878 ns->ln_striped_dirs_skipped,
3879 ns->ln_striped_shards_scanned,
3880 ns->ln_striped_shards_repaired,
3881 ns->ln_striped_shards_failed,
3882 ns->ln_striped_shards_skipped,
3883 ns->ln_name_hash_repaired,
3884 ns->ln_linkea_overflow_cleared,
3885 ns->ln_success_count,
3890 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3891 struct lfsck_component *com)
3893 struct lfsck_instance *lfsck = com->lc_lfsck;
3894 struct lfsck_namespace *ns = com->lc_file_ram;
3896 while (!list_empty(&lfsck->li_list_lmv)) {
3897 struct lfsck_lmv_unit *llu;
3898 struct lfsck_lmv *llmv;
3900 llu = list_entry(lfsck->li_list_lmv.next,
3901 struct lfsck_lmv_unit, llu_link);
3902 llmv = &llu->llu_lmv;
3904 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3905 "still in using: %u\n",
3906 atomic_read(&llmv->ll_ref));
3908 ns->ln_striped_dirs_skipped++;
3909 lfsck_lmv_put(env, llmv);
3913 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3914 struct lfsck_component *com,
3915 struct dt_object *obj)
3917 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3920 rc = dt_attr_get(env, obj, la);
3924 /* zero-linkEA object may be orphan, but it also maybe because
3925 * of upgrading. Currently, we cannot record it for double scan.
3926 * Because it may cause the LFSCK trace file to be too large. */
3928 /* "la_ctime" == 1 means that it has ever been removed from
3929 * backend /lost+found directory but not been added back to
3930 * the normal namespace yet. */
3932 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3933 unlikely(la->la_ctime == 1))
3934 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3935 LNTF_CHECK_LINKEA, true);
3940 /* namespace APIs */
3942 static int lfsck_namespace_reset(const struct lu_env *env,
3943 struct lfsck_component *com, bool init)
3945 struct lfsck_instance *lfsck = com->lc_lfsck;
3946 struct lfsck_namespace *ns = com->lc_file_ram;
3947 struct lfsck_assistant_data *lad = com->lc_data;
3948 struct dt_object *root;
3949 struct dt_object *dto;
3953 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3955 GOTO(log, rc = PTR_ERR(root));
3957 if (unlikely(!dt_try_as_dir(env, root)))
3958 GOTO(put, rc = -ENOTDIR);
3960 down_write(&com->lc_sem);
3962 memset(ns, 0, sizeof(*ns));
3964 __u32 count = ns->ln_success_count;
3965 __u64 last_time = ns->ln_time_last_complete;
3967 memset(ns, 0, sizeof(*ns));
3968 ns->ln_success_count = count;
3969 ns->ln_time_last_complete = last_time;
3971 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3972 ns->ln_status = LS_INIT;
3973 ns->ln_time_latest_reset = cfs_time_current_sec();
3975 lfsck_object_put(env, com->lc_obj);
3977 dto = lfsck_namespace_load_one_trace_file(env, com, root,
3978 LFSCK_NAMESPACE, true);
3980 GOTO(out, rc = PTR_ERR(dto));
3983 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3987 lad->lad_incomplete = 0;
3988 CFS_RESET_BITMAP(lad->lad_bitmap);
3990 rc = lfsck_namespace_store(env, com);
3995 up_write(&com->lc_sem);
3998 lfsck_object_put(env, root);
4000 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
4001 lfsck_lfsck2name(lfsck), rc);
4006 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
4009 struct lfsck_namespace *ns = com->lc_file_ram;
4011 down_write(&com->lc_sem);
4013 com->lc_new_checked++;
4014 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4015 up_write(&com->lc_sem);
4018 static void lfsck_namespace_close_dir(const struct lu_env *env,
4019 struct lfsck_component *com)
4021 struct lfsck_namespace *ns = com->lc_file_ram;
4022 struct lfsck_assistant_data *lad = com->lc_data;
4023 struct lfsck_assistant_object *lso = NULL;
4024 struct lfsck_instance *lfsck = com->lc_lfsck;
4025 struct lfsck_lmv *llmv = lfsck->li_lmv;
4026 struct lfsck_namespace_req *lnr;
4028 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
4029 bool wakeup = false;
4035 OBD_ALLOC(lnr, size);
4037 ns->ln_striped_dirs_skipped++;
4042 lso = lfsck_assistant_object_init(env, lfsck_dto2fid(lfsck->li_obj_dir),
4043 NULL, lfsck->li_pos_current.lp_oit_cookie, true);
4045 OBD_FREE(lnr, size);
4046 ns->ln_striped_dirs_skipped++;
4051 /* Generate a dummy request to indicate that all shards' name entry
4052 * in this striped directory has been scanned for the first time. */
4053 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
4054 lnr->lnr_lar.lar_parent = lso;
4055 lnr->lnr_lmv = lfsck_lmv_get(llmv);
4056 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
4057 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
4058 lnr->lnr_size = size;
4060 spin_lock(&lad->lad_lock);
4061 if (lad->lad_assistant_status < 0) {
4062 spin_unlock(&lad->lad_lock);
4063 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4064 ns->ln_striped_dirs_skipped++;
4069 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4070 if (lad->lad_prefetched == 0)
4073 lad->lad_prefetched++;
4074 spin_unlock(&lad->lad_lock);
4076 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4081 static int lfsck_namespace_open_dir(const struct lu_env *env,
4082 struct lfsck_component *com)
4084 struct lfsck_instance *lfsck = com->lc_lfsck;
4085 struct lfsck_namespace *ns = com->lc_file_ram;
4086 struct lfsck_lmv *llmv = lfsck->li_lmv;
4093 if (llmv->ll_lmv_master) {
4094 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
4096 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
4097 lmv->lmv_master_mdt_index =
4098 lfsck_dev_idx(lfsck);
4099 ns->ln_flags |= LF_INCONSISTENT;
4100 llmv->ll_lmv_updated = 1;
4103 rc = lfsck_namespace_verify_stripe_slave(env, com,
4104 lfsck->li_obj_dir, llmv);
4107 RETURN(rc > 0 ? 0 : rc);
4110 static int lfsck_namespace_checkpoint(const struct lu_env *env,
4111 struct lfsck_component *com, bool init)
4113 struct lfsck_instance *lfsck = com->lc_lfsck;
4114 struct lfsck_namespace *ns = com->lc_file_ram;
4118 rc = lfsck_checkpoint_generic(env, com);
4123 down_write(&com->lc_sem);
4125 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
4127 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4128 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4129 HALF_SEC - lfsck->li_time_last_checkpoint);
4130 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4131 ns->ln_items_checked += com->lc_new_checked;
4132 com->lc_new_checked = 0;
4135 rc = lfsck_namespace_store(env, com);
4136 up_write(&com->lc_sem);
4139 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos [%llu"
4140 ", "DFID", %#llx], status = %d: rc = %d\n",
4141 lfsck_lfsck2name(lfsck), lfsck->li_pos_current.lp_oit_cookie,
4142 PFID(&lfsck->li_pos_current.lp_dir_parent),
4143 lfsck->li_pos_current.lp_dir_cookie, ns->ln_status, rc);
4145 return rc > 0 ? 0 : rc;
4148 static int lfsck_namespace_prep(const struct lu_env *env,
4149 struct lfsck_component *com,
4150 struct lfsck_start_param *lsp)
4152 struct lfsck_instance *lfsck = com->lc_lfsck;
4153 struct lfsck_namespace *ns = com->lc_file_ram;
4154 struct lfsck_position *pos = &com->lc_pos_start;
4157 rc = lfsck_namespace_load_bitmap(env, com);
4158 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
4159 rc = lfsck_namespace_reset(env, com, false);
4161 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
4164 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
4165 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4171 down_write(&com->lc_sem);
4172 ns->ln_time_latest_start = cfs_time_current_sec();
4173 spin_lock(&lfsck->li_lock);
4175 if (ns->ln_flags & LF_SCANNED_ONCE) {
4176 if (!lfsck->li_drop_dryrun ||
4177 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4178 ns->ln_status = LS_SCANNING_PHASE2;
4179 list_move_tail(&com->lc_link,
4180 &lfsck->li_list_double_scan);
4181 if (!list_empty(&com->lc_link_dir))
4182 list_del_init(&com->lc_link_dir);
4183 lfsck_pos_set_zero(pos);
4185 ns->ln_status = LS_SCANNING_PHASE1;
4186 ns->ln_run_time_phase1 = 0;
4187 ns->ln_run_time_phase2 = 0;
4188 ns->ln_items_checked = 0;
4189 ns->ln_items_repaired = 0;
4190 ns->ln_items_failed = 0;
4191 ns->ln_dirs_checked = 0;
4192 ns->ln_objs_checked_phase2 = 0;
4193 ns->ln_objs_repaired_phase2 = 0;
4194 ns->ln_objs_failed_phase2 = 0;
4195 ns->ln_objs_nlink_repaired = 0;
4196 ns->ln_dirent_repaired = 0;
4197 ns->ln_linkea_repaired = 0;
4198 ns->ln_mul_linked_checked = 0;
4199 ns->ln_mul_linked_repaired = 0;
4200 ns->ln_unknown_inconsistency = 0;
4201 ns->ln_unmatched_pairs_repaired = 0;
4202 ns->ln_dangling_repaired = 0;
4203 ns->ln_mul_ref_repaired = 0;
4204 ns->ln_bad_type_repaired = 0;
4205 ns->ln_lost_dirent_repaired = 0;
4206 ns->ln_striped_dirs_scanned = 0;
4207 ns->ln_striped_dirs_repaired = 0;
4208 ns->ln_striped_dirs_failed = 0;
4209 ns->ln_striped_dirs_disabled = 0;
4210 ns->ln_striped_dirs_skipped = 0;
4211 ns->ln_striped_shards_scanned = 0;
4212 ns->ln_striped_shards_repaired = 0;
4213 ns->ln_striped_shards_failed = 0;
4214 ns->ln_striped_shards_skipped = 0;
4215 ns->ln_name_hash_repaired = 0;
4216 fid_zero(&ns->ln_fid_latest_scanned_phase2);
4217 if (list_empty(&com->lc_link_dir))
4218 list_add_tail(&com->lc_link_dir,
4219 &lfsck->li_list_dir);
4220 *pos = ns->ln_pos_first_inconsistent;
4223 ns->ln_status = LS_SCANNING_PHASE1;
4224 if (list_empty(&com->lc_link_dir))
4225 list_add_tail(&com->lc_link_dir,
4226 &lfsck->li_list_dir);
4227 if (!lfsck->li_drop_dryrun ||
4228 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4229 *pos = ns->ln_pos_last_checkpoint;
4230 pos->lp_oit_cookie++;
4232 *pos = ns->ln_pos_first_inconsistent;
4236 spin_unlock(&lfsck->li_lock);
4237 up_write(&com->lc_sem);
4239 rc = lfsck_start_assistant(env, com, lsp);
4241 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos [%llu, "
4242 DFID", %#llx]: rc = %d\n",
4243 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4244 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4249 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4250 struct lfsck_component *com,
4251 struct dt_object *obj)
4253 struct lfsck_thread_info *info = lfsck_env_info(env);
4254 struct lfsck_namespace *ns = com->lc_file_ram;
4255 struct lfsck_instance *lfsck = com->lc_lfsck;
4256 const struct lu_fid *fid = lfsck_dto2fid(obj);
4257 struct lu_fid *pfid = &info->lti_fid2;
4258 struct lu_name *cname = &info->lti_name;
4259 struct lu_seq_range *range = &info->lti_range;
4260 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4261 struct linkea_data ldata = { NULL };
4262 __u32 idx = lfsck_dev_idx(lfsck);
4266 rc = lfsck_links_read(env, obj, &ldata);
4270 /* -EINVAL means crashed linkEA, should be verified. */
4271 if (rc == -EINVAL) {
4272 rc = lfsck_namespace_trace_update(env, com, fid,
4273 LNTF_CHECK_LINKEA, true);
4275 struct lustre_handle lh = { 0 };
4277 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4278 MDS_INODELOCK_UPDATE |
4279 MDS_INODELOCK_XATTR, LCK_EX);
4281 rc = lfsck_namespace_links_remove(env, com,
4283 lfsck_ibits_unlock(&lh, LCK_EX);
4287 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4290 if (rc == -ENODATA || unlikely(!ldata.ld_leh->leh_reccount)) {
4291 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4299 /* Record multiple-linked object. */
4300 if (ldata.ld_leh->leh_reccount > 1) {
4301 rc = lfsck_namespace_trace_update(env, com, fid,
4302 LNTF_CHECK_LINKEA, true);
4307 linkea_first_entry(&ldata);
4308 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4309 if (!fid_is_sane(pfid)) {
4310 rc = lfsck_namespace_trace_update(env, com, fid,
4311 LNTF_CHECK_PARENT, true);
4313 fld_range_set_mdt(range);
4314 rc = fld_local_lookup(env, ss->ss_server_fld,
4315 fid_seq(pfid), range);
4316 if ((rc == -ENOENT) ||
4317 (rc == 0 && range->lsr_index != idx))
4318 rc = lfsck_namespace_trace_update(env, com, fid,
4319 LNTF_CHECK_LINKEA, true);
4321 rc = lfsck_namespace_check_for_double_scan(env, com,
4328 down_write(&com->lc_sem);
4329 if (S_ISDIR(lfsck_object_type(obj)))
4330 ns->ln_dirs_checked++;
4332 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4333 up_write(&com->lc_sem);
4338 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4339 struct lfsck_component *com,
4340 struct lfsck_assistant_object *lso,
4341 struct lu_dirent *ent, __u16 type)
4343 struct lfsck_assistant_data *lad = com->lc_data;
4344 struct lfsck_instance *lfsck = com->lc_lfsck;
4345 struct lfsck_namespace_req *lnr;
4346 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4347 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4348 struct ptlrpc_thread *athread = &lad->lad_thread;
4349 struct l_wait_info lwi = { 0 };
4350 bool wakeup = false;
4352 l_wait_event(mthread->t_ctl_waitq,
4353 lad->lad_prefetched < bk->lb_async_windows ||
4354 !thread_is_running(mthread) ||
4355 thread_is_stopped(athread),
4358 if (unlikely(!thread_is_running(mthread)) ||
4359 thread_is_stopped(athread))
4362 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4365 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, lso, ent, type);
4367 struct lfsck_namespace *ns = com->lc_file_ram;
4369 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4370 return PTR_ERR(lnr);
4373 spin_lock(&lad->lad_lock);
4374 if (lad->lad_assistant_status < 0) {
4375 spin_unlock(&lad->lad_lock);
4376 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4377 return lad->lad_assistant_status;
4380 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4381 if (lad->lad_prefetched == 0)
4384 lad->lad_prefetched++;
4385 spin_unlock(&lad->lad_lock);
4387 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4389 down_write(&com->lc_sem);
4390 com->lc_new_checked++;
4391 up_write(&com->lc_sem);
4396 static int lfsck_namespace_post(const struct lu_env *env,
4397 struct lfsck_component *com,
4398 int result, bool init)
4400 struct lfsck_instance *lfsck = com->lc_lfsck;
4401 struct lfsck_namespace *ns = com->lc_file_ram;
4405 lfsck_post_generic(env, com, &result);
4407 down_write(&com->lc_sem);
4408 lfsck_namespace_release_lmv(env, com);
4410 spin_lock(&lfsck->li_lock);
4412 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4414 ns->ln_status = LS_SCANNING_PHASE2;
4415 ns->ln_flags |= LF_SCANNED_ONCE;
4416 ns->ln_flags &= ~LF_UPGRADE;
4417 list_del_init(&com->lc_link_dir);
4418 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4419 } else if (result == 0) {
4420 if (lfsck->li_status != 0)
4421 ns->ln_status = lfsck->li_status;
4423 ns->ln_status = LS_STOPPED;
4424 if (ns->ln_status != LS_PAUSED) {
4425 list_del_init(&com->lc_link_dir);
4426 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4429 ns->ln_status = LS_FAILED;
4430 list_del_init(&com->lc_link_dir);
4431 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4433 spin_unlock(&lfsck->li_lock);
4436 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4437 HALF_SEC - lfsck->li_time_last_checkpoint);
4438 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4439 ns->ln_items_checked += com->lc_new_checked;
4440 com->lc_new_checked = 0;
4443 rc = lfsck_namespace_store(env, com);
4444 up_write(&com->lc_sem);
4446 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4447 lfsck_lfsck2name(lfsck), rc);
4453 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4456 struct lfsck_instance *lfsck = com->lc_lfsck;
4457 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4458 struct lfsck_namespace *ns = com->lc_file_ram;
4460 down_read(&com->lc_sem);
4461 seq_printf(m, "name: lfsck_namespace\n"
4467 lfsck_status2name(ns->ln_status));
4469 lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4471 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4473 lfsck_time_dump(m, ns->ln_time_last_complete, "last_completed");
4475 lfsck_time_dump(m, ns->ln_time_latest_start, "latest_start");
4477 lfsck_time_dump(m, ns->ln_time_last_checkpoint, "last_checkpoint");
4479 lfsck_pos_dump(m, &ns->ln_pos_latest_start, "latest_start_position");
4481 lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4482 "last_checkpoint_position");
4484 lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4485 "first_failure_position");
4487 if (ns->ln_status == LS_SCANNING_PHASE1) {
4488 struct lfsck_position pos;
4489 const struct dt_it_ops *iops;
4490 cfs_duration_t duration = cfs_time_current() -
4491 lfsck->li_time_last_checkpoint;
4492 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4493 __u64 speed = checked;
4494 __u64 new_checked = com->lc_new_checked *
4495 msecs_to_jiffies(MSEC_PER_SEC);
4496 __u32 rtime = ns->ln_run_time_phase1 +
4497 cfs_duration_sec(duration + HALF_SEC);
4500 do_div(new_checked, duration);
4503 do_div(speed, rtime);
4505 lfsck_namespace_dump_statistics(m, ns, checked, 0, rtime, 0);
4506 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4507 "average_speed_phase2: N/A\n"
4508 "average_speed_total: %llu items/sec\n"
4509 "real_time_speed_phase1: %llu items/sec\n"
4510 "real_time_speed_phase2: N/A\n",
4515 LASSERT(lfsck->li_di_oit != NULL);
4517 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4519 /* The low layer otable-based iteration position may NOT
4520 * exactly match the namespace-based directory traversal
4521 * cookie. Generally, it is not a serious issue. But the
4522 * caller should NOT make assumption on that. */
4523 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4524 if (!lfsck->li_current_oit_processed)
4525 pos.lp_oit_cookie--;
4527 spin_lock(&lfsck->li_lock);
4528 if (lfsck->li_di_dir != NULL) {
4529 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4530 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4531 fid_zero(&pos.lp_dir_parent);
4532 pos.lp_dir_cookie = 0;
4535 *lfsck_dto2fid(lfsck->li_obj_dir);
4538 fid_zero(&pos.lp_dir_parent);
4539 pos.lp_dir_cookie = 0;
4541 spin_unlock(&lfsck->li_lock);
4542 lfsck_pos_dump(m, &pos, "current_position");
4543 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4544 cfs_duration_t duration = cfs_time_current() -
4545 com->lc_time_last_checkpoint;
4546 __u64 checked = ns->ln_objs_checked_phase2 +
4547 com->lc_new_checked;
4548 __u64 speed1 = ns->ln_items_checked;
4549 __u64 speed2 = checked;
4550 __u64 speed0 = speed1 + speed2;
4551 __u64 new_checked = com->lc_new_checked *
4552 msecs_to_jiffies(MSEC_PER_SEC);
4553 __u32 rtime = ns->ln_run_time_phase2 +
4554 cfs_duration_sec(duration + HALF_SEC);
4555 __u32 time0 = ns->ln_run_time_phase1 + rtime;
4558 do_div(new_checked, duration);
4560 if (ns->ln_run_time_phase1 != 0)
4561 do_div(speed1, ns->ln_run_time_phase1);
4562 else if (ns->ln_items_checked != 0)
4566 do_div(speed2, rtime);
4567 else if (checked != 0)
4571 do_div(speed0, time0);
4573 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4575 ns->ln_run_time_phase1, rtime);
4576 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4577 "average_speed_phase2: %llu objs/sec\n"
4578 "average_speed_total: %llu items/sec\n"
4579 "real_time_speed_phase1: N/A\n"
4580 "real_time_speed_phase2: %llu objs/sec\n"
4581 "current_position: "DFID"\n",
4586 PFID(&ns->ln_fid_latest_scanned_phase2));
4588 __u64 speed1 = ns->ln_items_checked;
4589 __u64 speed2 = ns->ln_objs_checked_phase2;
4590 __u64 speed0 = speed1 + speed2;
4591 __u32 time0 = ns->ln_run_time_phase1 + ns->ln_run_time_phase2;
4593 if (ns->ln_run_time_phase1 != 0)
4594 do_div(speed1, ns->ln_run_time_phase1);
4595 else if (ns->ln_items_checked != 0)
4598 if (ns->ln_run_time_phase2 != 0)
4599 do_div(speed2, ns->ln_run_time_phase2);
4600 else if (ns->ln_objs_checked_phase2 != 0)
4604 do_div(speed0, time0);
4606 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4607 ns->ln_objs_checked_phase2,
4608 ns->ln_run_time_phase1,
4609 ns->ln_run_time_phase2);
4610 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4611 "average_speed_phase2: %llu objs/sec\n"
4612 "average_speed_total: %llu items/sec\n"
4613 "real_time_speed_phase1: N/A\n"
4614 "real_time_speed_phase2: N/A\n"
4615 "current_position: N/A\n",
4621 up_read(&com->lc_sem);
4624 static int lfsck_namespace_double_scan(const struct lu_env *env,
4625 struct lfsck_component *com)
4627 struct lfsck_namespace *ns = com->lc_file_ram;
4628 struct lfsck_assistant_data *lad = com->lc_data;
4629 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4630 struct lfsck_tgt_desc *ltd;
4631 struct lfsck_tgt_desc *next;
4634 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4635 if (thread_is_stopped(&lad->lad_thread)) {
4636 LASSERT(list_empty(&lad->lad_req_list));
4637 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4639 spin_lock(<ds->ltd_lock);
4640 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4641 ltd_namespace_phase_list) {
4642 list_del_init(<d->ltd_namespace_phase_list);
4644 spin_unlock(<ds->ltd_lock);
4650 static void lfsck_namespace_data_release(const struct lu_env *env,
4651 struct lfsck_component *com)
4653 struct lfsck_assistant_data *lad = com->lc_data;
4654 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4655 struct lfsck_tgt_desc *ltd;
4656 struct lfsck_tgt_desc *next;
4658 LASSERT(lad != NULL);
4659 LASSERT(thread_is_init(&lad->lad_thread) ||
4660 thread_is_stopped(&lad->lad_thread));
4661 LASSERT(list_empty(&lad->lad_req_list));
4663 com->lc_data = NULL;
4664 lfsck_namespace_release_lmv(env, com);
4666 spin_lock(<ds->ltd_lock);
4667 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4668 ltd_namespace_phase_list) {
4669 list_del_init(<d->ltd_namespace_phase_list);
4671 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4672 ltd_namespace_phase_list) {
4673 list_del_init(<d->ltd_namespace_phase_list);
4675 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4676 ltd_namespace_list) {
4677 list_del_init(<d->ltd_namespace_list);
4679 spin_unlock(<ds->ltd_lock);
4681 if (likely(lad->lad_bitmap != NULL))
4682 CFS_FREE_BITMAP(lad->lad_bitmap);
4687 static void lfsck_namespace_quit(const struct lu_env *env,
4688 struct lfsck_component *com)
4690 struct lfsck_assistant_data *lad = com->lc_data;
4691 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4692 struct lfsck_tgt_desc *ltd;
4693 struct lfsck_tgt_desc *next;
4695 LASSERT(lad != NULL);
4697 lfsck_quit_generic(env, com);
4699 LASSERT(thread_is_init(&lad->lad_thread) ||
4700 thread_is_stopped(&lad->lad_thread));
4701 LASSERT(list_empty(&lad->lad_req_list));
4703 lfsck_namespace_release_lmv(env, com);
4705 spin_lock(<ds->ltd_lock);
4706 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4707 ltd_namespace_phase_list) {
4708 list_del_init(<d->ltd_namespace_phase_list);
4710 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4711 ltd_namespace_phase_list) {
4712 list_del_init(<d->ltd_namespace_phase_list);
4714 spin_unlock(<ds->ltd_lock);
4717 static int lfsck_namespace_in_notify(const struct lu_env *env,
4718 struct lfsck_component *com,
4719 struct lfsck_request *lr,
4722 struct lfsck_instance *lfsck = com->lc_lfsck;
4723 struct lfsck_namespace *ns = com->lc_file_ram;
4724 struct lfsck_assistant_data *lad = com->lc_data;
4725 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4726 struct lfsck_tgt_desc *ltd;
4731 switch (lr->lr_event) {
4732 case LE_SET_LMV_MASTER: {
4733 struct dt_object *obj;
4735 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4737 RETURN(PTR_ERR(obj));
4739 if (likely(dt_object_exists(obj)))
4740 rc = lfsck_namespace_notify_lmv_master_local(env, com,
4743 lfsck_object_put(env, obj);
4745 RETURN(rc > 0 ? 0 : rc);
4747 case LE_SET_LMV_SLAVE: {
4748 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4749 ns->ln_striped_shards_repaired++;
4751 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4752 LNTF_RECHECK_NAME_HASH, true);
4754 RETURN(rc > 0 ? 0 : rc);
4756 case LE_PHASE1_DONE:
4757 case LE_PHASE2_DONE:
4764 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4765 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4766 lr->lr_index, lr->lr_status, lr->lr_flags2);
4768 spin_lock(<ds->ltd_lock);
4769 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
4771 spin_unlock(<ds->ltd_lock);
4776 list_del_init(<d->ltd_namespace_phase_list);
4777 switch (lr->lr_event) {
4778 case LE_PHASE1_DONE:
4779 if (lr->lr_status <= 0) {
4780 ltd->ltd_namespace_done = 1;
4781 list_del_init(<d->ltd_namespace_list);
4782 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4783 "phase1 for namespace LFSCK: rc = %d.\n",
4784 lfsck_lfsck2name(lfsck),
4785 ltd->ltd_index, lr->lr_status);
4786 ns->ln_flags |= LF_INCOMPLETE;
4791 if (lr->lr_flags2 & LF_INCOMPLETE)
4792 ns->ln_flags |= LF_INCOMPLETE;
4794 if (list_empty(<d->ltd_namespace_list))
4795 list_add_tail(<d->ltd_namespace_list,
4796 &lad->lad_mdt_list);
4797 list_add_tail(<d->ltd_namespace_phase_list,
4798 &lad->lad_mdt_phase2_list);
4800 case LE_PHASE2_DONE:
4801 ltd->ltd_namespace_done = 1;
4802 list_del_init(<d->ltd_namespace_list);
4806 ltd->ltd_namespace_done = 1;
4807 list_del_init(<d->ltd_namespace_list);
4808 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4810 "%s: the peer MDT %x exit namespace LFSCK\n",
4811 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4812 ns->ln_flags |= LF_INCOMPLETE;
4818 spin_unlock(<ds->ltd_lock);
4820 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4821 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4823 memset(stop, 0, sizeof(*stop));
4824 stop->ls_status = lr->lr_status;
4825 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4826 lfsck_stop(env, lfsck->li_bottom, stop);
4827 } else if (lfsck_phase2_next_ready(lad)) {
4828 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4834 static void lfsck_namespace_repaired(struct lfsck_namespace *ns, __u64 *count)
4836 *count += ns->ln_objs_nlink_repaired;
4837 *count += ns->ln_dirent_repaired;
4838 *count += ns->ln_linkea_repaired;
4839 *count += ns->ln_mul_linked_repaired;
4840 *count += ns->ln_unmatched_pairs_repaired;
4841 *count += ns->ln_dangling_repaired;
4842 *count += ns->ln_mul_ref_repaired;
4843 *count += ns->ln_bad_type_repaired;
4844 *count += ns->ln_lost_dirent_repaired;
4845 *count += ns->ln_striped_dirs_disabled;
4846 *count += ns->ln_striped_dirs_repaired;
4847 *count += ns->ln_striped_shards_repaired;
4848 *count += ns->ln_name_hash_repaired;
4849 *count += ns->ln_local_lpf_moved;
4852 static int lfsck_namespace_query_all(const struct lu_env *env,
4853 struct lfsck_component *com,
4854 __u32 *mdts_count, __u64 *repaired)
4856 struct lfsck_namespace *ns = com->lc_file_ram;
4857 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4858 struct lfsck_tgt_desc *ltd;
4863 rc = lfsck_query_all(env, com);
4867 down_read(<ds->ltd_rw_sem);
4868 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
4869 ltd = lfsck_ltd2tgt(ltds, idx);
4870 LASSERT(ltd != NULL);
4872 mdts_count[ltd->ltd_namespace_status]++;
4873 *repaired += ltd->ltd_namespace_repaired;
4875 up_read(<ds->ltd_rw_sem);
4877 down_read(&com->lc_sem);
4878 mdts_count[ns->ln_status]++;
4879 lfsck_namespace_repaired(ns, repaired);
4880 up_read(&com->lc_sem);
4885 static int lfsck_namespace_query(const struct lu_env *env,
4886 struct lfsck_component *com,
4887 struct lfsck_request *req,
4888 struct lfsck_reply *rep,
4889 struct lfsck_query *que, int idx)
4891 struct lfsck_namespace *ns = com->lc_file_ram;
4895 LASSERT(com->lc_lfsck->li_master);
4897 rc = lfsck_namespace_query_all(env, com,
4898 que->lu_mdts_count[idx],
4899 &que->lu_repaired[idx]);
4901 down_read(&com->lc_sem);
4902 rep->lr_status = ns->ln_status;
4903 if (req->lr_flags & LEF_QUERY_ALL)
4904 lfsck_namespace_repaired(ns, &rep->lr_repaired);
4905 up_read(&com->lc_sem);
4911 static struct lfsck_operations lfsck_namespace_ops = {
4912 .lfsck_reset = lfsck_namespace_reset,
4913 .lfsck_fail = lfsck_namespace_fail,
4914 .lfsck_close_dir = lfsck_namespace_close_dir,
4915 .lfsck_open_dir = lfsck_namespace_open_dir,
4916 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4917 .lfsck_prep = lfsck_namespace_prep,
4918 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4919 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4920 .lfsck_post = lfsck_namespace_post,
4921 .lfsck_dump = lfsck_namespace_dump,
4922 .lfsck_double_scan = lfsck_namespace_double_scan,
4923 .lfsck_data_release = lfsck_namespace_data_release,
4924 .lfsck_quit = lfsck_namespace_quit,
4925 .lfsck_in_notify = lfsck_namespace_in_notify,
4926 .lfsck_query = lfsck_namespace_query,
4930 * Repair dangling name entry.
4932 * For the name entry with dangling reference, we need to repare the
4933 * inconsistency according to the LFSCK sponsor's requirement:
4935 * 1) Keep the inconsistency there and report the inconsistency case,
4936 * then give the chance to the application to find related issues,
4937 * and the users can make the decision about how to handle it with
4938 * more human knownledge. (by default)
4940 * 2) Re-create the missing MDT-object with the FID information.
4942 * \param[in] env pointer to the thread context
4943 * \param[in] com pointer to the lfsck component
4944 * \param[in] parent pointer to the dir object that contains the dangling
4946 * \param[in] child pointer to the object corresponding to the dangling
4948 * \param[in] lnr pointer to the namespace request that contains the
4949 * name's name, parent object, parent's LMV, and ect.
4951 * \retval positive number if no need to repair
4952 * \retval zero for repaired successfully
4953 * \retval negative error number on failure
4955 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4956 struct lfsck_component *com,
4957 struct dt_object *parent,
4958 struct dt_object *child,
4959 struct lfsck_namespace_req *lnr)
4961 struct lfsck_thread_info *info = lfsck_env_info(env);
4962 struct lu_attr *la = &info->lti_la;
4963 struct dt_allocation_hint *hint = &info->lti_hint;
4964 struct dt_object_format *dof = &info->lti_dof;
4965 struct dt_insert_rec *rec = &info->lti_dt_rec;
4966 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4967 const struct lu_name *cname;
4968 const struct lu_fid *pfid = lfsck_dto2fid(parent);
4969 const struct lu_fid *cfid = lfsck_dto2fid(child);
4970 struct linkea_data ldata = { NULL };
4971 struct lfsck_lock_handle *llh = &info->lti_llh;
4972 struct lu_buf linkea_buf;
4973 struct lu_buf lmv_buf;
4974 struct lfsck_instance *lfsck = com->lc_lfsck;
4975 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4976 struct dt_device *dev = lfsck->li_next;
4977 struct thandle *th = NULL;
4979 __u16 type = lnr->lnr_type;
4983 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4984 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4989 if (!create || bk->lb_param & LPF_DRYRUN)
4992 /* We may need to create the sub-objects of the @child via LOD,
4993 * so make the modification based on lfsck->li_next. */
4995 parent = lfsck_object_locate(dev, parent);
4997 GOTO(log, rc = PTR_ERR(parent));
4999 if (unlikely(!dt_try_as_dir(env, parent)))
5000 GOTO(log, rc = -ENOTDIR);
5002 child = lfsck_object_locate(dev, child);
5004 GOTO(log, rc = PTR_ERR(child));
5006 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
5011 rc = lfsck_lock(env, lfsck, parent, lnr->lnr_name, llh,
5012 MDS_INODELOCK_UPDATE, LCK_PR);
5016 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
5020 /* Set the ctime as zero, then others can know it is created for
5021 * repairing dangling name entry by LFSCK. And if the LFSCK made
5022 * wrong decision and the real MDT-object has been found later,
5023 * then the LFSCK has chance to fix the incosistency properly. */
5024 memset(la, 0, sizeof(*la));
5025 la->la_mode = (type & S_IFMT) | 0600;
5026 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
5027 LA_ATIME | LA_MTIME | LA_CTIME;
5029 child->do_ops->do_ah_init(env, hint, parent, child,
5030 la->la_mode & S_IFMT);
5032 memset(dof, 0, sizeof(*dof));
5033 dof->dof_type = dt_mode_to_dft(type);
5034 /* If the target is a regular file, then the LFSCK will only create
5035 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
5036 * OST-objects will be created when write open. */
5038 th = dt_trans_create(env, dev);
5040 GOTO(log, rc = PTR_ERR(th));
5042 /* 1a. create child. */
5043 rc = dt_declare_create(env, child, la, hint, dof, th);
5047 if (S_ISDIR(type)) {
5048 if (unlikely(!dt_try_as_dir(env, child)))
5049 GOTO(stop, rc = -ENOTDIR);
5051 /* 2a. increase child nlink */
5052 rc = dt_declare_ref_add(env, child, th);
5056 /* 3a. insert dot into child dir */
5057 rec->rec_type = S_IFDIR;
5058 rec->rec_fid = cfid;
5059 rc = dt_declare_insert(env, child,
5060 (const struct dt_rec *)rec,
5061 (const struct dt_key *)dot, th);
5065 /* 4a. insert dotdot into child dir */
5066 rec->rec_fid = pfid;
5067 rc = dt_declare_insert(env, child,
5068 (const struct dt_rec *)rec,
5069 (const struct dt_key *)dotdot, th);
5073 /* 5a. generate slave LMV EA. */
5074 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5077 idx = lfsck_shard_name_to_index(env,
5078 lnr->lnr_name, lnr->lnr_namelen,
5080 if (unlikely(idx < 0))
5081 GOTO(stop, rc = idx);
5083 *lmv2 = lnr->lnr_lmv->ll_lmv;
5084 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
5085 lmv2->lmv_master_mdt_index = idx;
5087 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
5088 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
5089 rc = dt_declare_xattr_set(env, child, &lmv_buf,
5090 XATTR_NAME_LMV, 0, th);
5096 /* 6a. insert linkEA for child */
5097 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
5098 ldata.ld_leh->leh_len);
5099 rc = dt_declare_xattr_set(env, child, &linkea_buf,
5100 XATTR_NAME_LINK, 0, th);
5104 rc = dt_trans_start_local(env, dev, th);
5106 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
5108 dt_write_lock(env, child, 0);
5109 /* 1b. create child */
5110 rc = dt_create(env, child, la, hint, dof, th);
5112 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
5114 if (S_ISDIR(type)) {
5115 /* 2b. increase child nlink */
5116 rc = dt_ref_add(env, child, th);
5120 /* 3b. insert dot into child dir */
5121 rec->rec_type = S_IFDIR;
5122 rec->rec_fid = cfid;
5123 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5124 (const struct dt_key *)dot, th, 1);
5128 /* 4b. insert dotdot into child dir */
5129 rec->rec_fid = pfid;
5130 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5131 (const struct dt_key *)dotdot, th, 1);
5135 /* 5b. generate slave LMV EA. */
5136 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5137 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
5144 /* 6b. insert linkEA for child. */
5145 rc = dt_xattr_set(env, child, &linkea_buf,
5146 XATTR_NAME_LINK, 0, th);
5151 dt_write_unlock(env, child);
5154 dt_trans_stop(env, dev, th);
5158 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
5159 "reference for: parent "DFID", child "DFID", type %u, "
5160 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
5161 PFID(pfid), PFID(cfid), type, cname->ln_name,
5162 create ? "Create the lost MDT-object as required" :
5163 "Keep the MDT-object there by default", rc);
5166 struct lfsck_namespace *ns = com->lc_file_ram;
5168 ns->ln_flags |= LF_INCONSISTENT;
5174 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
5175 struct lfsck_component *com,
5176 struct lfsck_assistant_req *lar)
5178 struct lfsck_thread_info *info = lfsck_env_info(env);
5179 struct lu_attr *la = &info->lti_la;
5180 struct lfsck_instance *lfsck = com->lc_lfsck;
5181 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5182 struct lfsck_namespace *ns = com->lc_file_ram;
5183 struct lfsck_assistant_data *lad = com->lc_data;
5184 struct linkea_data ldata = { NULL };
5185 const struct lu_name *cname;
5186 struct thandle *handle = NULL;
5187 struct lfsck_namespace_req *lnr =
5188 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5189 struct dt_object *dir = NULL;
5190 struct dt_object *obj = NULL;
5191 struct lfsck_assistant_object *lso = lar->lar_parent;
5192 const struct lu_fid *pfid = &lso->lso_fid;
5193 struct dt_device *dev = NULL;
5194 struct lustre_handle lh = { 0 };
5195 bool repaired = false;
5196 bool dtlocked = false;
5200 bool bad_hash = false;
5201 bool bad_linkea = false;
5205 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5212 if (lnr->lnr_attr & LUDA_UPGRADE) {
5213 ns->ln_flags |= LF_UPGRADE;
5214 ns->ln_dirent_repaired++;
5216 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5217 ns->ln_flags |= LF_INCONSISTENT;
5218 ns->ln_dirent_repaired++;
5222 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5223 if (strcmp(lnr->lnr_name, dotdot) != 0)
5226 rc = lfsck_namespace_trace_update(env, com, pfid,
5227 LNTF_CHECK_PARENT, true);
5232 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5233 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5234 " for the name entry %.*s under "DFID"\n",
5235 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5236 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5238 if (strcmp(lnr->lnr_name, dotdot) != 0)
5239 /* invalid FID means bad name entry, remove it. */
5240 type = LNIT_BAD_DIRENT;
5242 /* If the parent FID is invalid, we cannot remove
5243 * the ".." entry directly. */
5244 rc = lfsck_namespace_trace_update(env, com, pfid,
5245 LNTF_CHECK_PARENT, true);
5250 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5251 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5256 if (fid_seq_is_dot(fid_seq(&lnr->lnr_fid)))
5259 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5260 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5265 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5267 GOTO(out, rc = idx);
5269 if (idx == lfsck_dev_idx(lfsck)) {
5270 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5273 dev = lfsck->li_bottom;
5275 struct lfsck_tgt_desc *ltd;
5277 /* Usually, some local filesystem consistency verification
5278 * tools can guarantee the local namespace tree consistenct.
5279 * So the LFSCK will only verify the remote directory. */
5280 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5281 rc = lfsck_namespace_trace_update(env, com, pfid,
5282 LNTF_CHECK_PARENT, true);
5287 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
5288 if (unlikely(ltd == NULL)) {
5289 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5290 "did not join the namespace LFSCK\n",
5291 lfsck_lfsck2name(lfsck), idx);
5292 lfsck_lad_set_bitmap(env, com, idx);
5294 GOTO(out, rc = -ENODEV);
5300 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5302 GOTO(out, rc = PTR_ERR(obj));
5304 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5305 if (dt_object_exists(obj) == 0) {
5309 dir = lfsck_assistant_object_load(env, lfsck, lso);
5313 GOTO(trace, rc == -ENOENT ? 0 : rc);
5317 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5319 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5320 lnr->lnr_name, lnr->lnr_namelen)) {
5321 type = LNIT_BAD_DIRENT;
5326 type = LNIT_DANGLING;
5327 rc = lfsck_namespace_repair_dangling(env, com, dir,
5336 if (!(bk->lb_param & LPF_DRYRUN) && lad->lad_advance_lock) {
5339 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5340 MDS_INODELOCK_UPDATE |
5341 MDS_INODELOCK_XATTR, LCK_EX);
5345 handle = dt_trans_create(env, dev);
5347 GOTO(out, rc = PTR_ERR(handle));
5349 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5353 rc = dt_trans_start_local(env, dev, handle);
5357 dt_write_lock(env, obj, 0);
5361 rc = lfsck_links_read(env, obj, &ldata);
5362 if (unlikely(rc == -ENOENT)) {
5363 if (handle != NULL) {
5364 dt_write_unlock(env, obj);
5367 dt_trans_stop(env, dev, handle);
5370 lfsck_ibits_unlock(&lh, LCK_EX);
5373 /* It may happen when the remote object has been removed,
5374 * but the local MDT is not aware of that. */
5376 } else if (rc == 0) {
5377 count = ldata.ld_leh->leh_reccount;
5378 rc = linkea_links_find(&ldata, cname, pfid);
5380 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5381 if ((lfsck_object_type(obj) & S_IFMT) !=
5383 ns->ln_flags |= LF_INCONSISTENT;
5384 type = LNIT_BAD_TYPE;
5390 /* If the name entry hash does not match the slave striped
5391 * directory, and the name entry does not match also, then
5392 * it is quite possible that name entry is corrupted. */
5393 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5394 lnr->lnr_name, lnr->lnr_namelen)) {
5395 ns->ln_flags |= LF_INCONSISTENT;
5396 type = LNIT_BAD_DIRENT;
5401 /* If the file type stored in the name entry does not match
5402 * the file type claimed by the object, and the object does
5403 * not recognize the name entry, then it is quite possible
5404 * that the name entry is corrupted. */
5405 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5406 ns->ln_flags |= LF_INCONSISTENT;
5407 type = LNIT_BAD_DIRENT;
5412 /* For sub-dir object, we cannot make sure whether the sub-dir
5413 * back references the parent via ".." name entry correctly or
5414 * not in the LFSCK first-stage scanning. It may be that the
5415 * (remote) sub-dir ".." name entry has no parent FID after
5416 * file-level backup/restore and its linkEA may be wrong.
5417 * So under such case, we should replace the linkEA according
5418 * to current name entry. But this needs to be done during the
5419 * LFSCK second-stage scanning. The LFSCK will record the name
5420 * entry for further possible using. */
5424 } else if (unlikely(rc == -EINVAL)) {
5425 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5426 type = LNIT_BAD_TYPE;
5429 /* The magic crashed, we are not sure whether there are more
5430 * corrupt data in the linkea, so remove all linkea entries. */
5434 } else if (rc == -ENODATA) {
5435 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5436 type = LNIT_BAD_TYPE;
5443 if (bk->lb_param & LPF_DRYRUN) {
5445 ns->ln_flags |= LF_UPGRADE;
5447 ns->ln_flags |= LF_INCONSISTENT;
5448 ns->ln_linkea_repaired++;
5454 if (!lustre_handle_is_used(&lh)) {
5462 LASSERT(handle != NULL);
5465 dir = lfsck_assistant_object_load(env, lfsck, lso);
5469 GOTO(stop, rc == -ENOENT ? 0 : rc);
5473 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5478 if (!remove && newdata)
5479 ns->ln_flags |= LF_UPGRADE;
5480 else if (remove || !(ns->ln_flags & LF_UPGRADE))
5481 ns->ln_flags |= LF_INCONSISTENT;
5486 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle);
5487 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
5492 rc = linkea_data_new(&ldata,
5493 &lfsck_env_info(env)->lti_linkea_buf);
5498 rc = linkea_add_buf(&ldata, cname, pfid);
5500 rc = lfsck_links_write(env, obj, &ldata, handle);
5504 count = ldata.ld_leh->leh_reccount;
5505 if (!S_ISDIR(lfsck_object_type(obj)) ||
5506 !dt_object_remote(obj)) {
5507 ns->ln_linkea_repaired++;
5517 dt_write_unlock(env, obj);
5519 if (handle != NULL && !IS_ERR(handle))
5520 dt_trans_stop(env, dev, handle);
5523 lfsck_ibits_unlock(&lh, LCK_EX);
5525 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5526 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5527 lnr->lnr_name, lnr->lnr_namelen) &&
5528 type != LNIT_BAD_DIRENT) {
5529 ns->ln_flags |= LF_INCONSISTENT;
5533 dir = lfsck_assistant_object_load(env, lfsck, lso);
5537 GOTO(trace, rc == -ENOENT ? 0 : rc);
5541 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5542 lnr->lnr_lmv, lnr->lnr_name);
5548 if (type != LNIT_NONE && dir == NULL) {
5549 dir = lfsck_assistant_object_load(env, lfsck, lso);
5553 GOTO(trace, rc == -ENOENT ? 0 : rc);
5560 rc = lfsck_namespace_repair_dirent(env, com, dir,
5561 obj, lnr->lnr_name, lnr->lnr_name,
5562 lnr->lnr_type, true, false);
5566 case LNIT_BAD_DIRENT:
5568 /* XXX: This is a bad dirent, we do not know whether
5569 * the original name entry reference a regular
5570 * file or a directory, then keep the parent's
5571 * nlink count unchanged here. */
5572 rc = lfsck_namespace_repair_dirent(env, com, dir,
5573 obj, lnr->lnr_name, lnr->lnr_name,
5574 lnr->lnr_type, false, false);
5582 if (obj != NULL && count == 1 &&
5583 S_ISREG(lfsck_object_type(obj)))
5584 dt_attr_get(env, obj, la);
5588 down_write(&com->lc_sem);
5590 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5591 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5592 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid), PFID(pfid),
5593 lnr->lnr_namelen, lnr->lnr_name, rc);
5595 lfsck_namespace_record_failure(env, lfsck, ns);
5596 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5597 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5598 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5599 dev != NULL && dev != lfsck->li_bottom)
5600 lfsck_lad_set_bitmap(env, com, idx);
5602 if (!(bk->lb_param & LPF_FAILOUT))
5606 ns->ln_items_repaired++;
5608 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5609 "repaired the entry: "DFID", parent "DFID
5610 ", name %.*s, type %d\n",
5611 lfsck_lfsck2name(lfsck),
5612 PFID(&lnr->lnr_fid), PFID(pfid),
5613 lnr->lnr_namelen, lnr->lnr_name, type);
5617 ns->ln_dangling_repaired++;
5620 ns->ln_bad_type_repaired++;
5622 case LNIT_BAD_DIRENT:
5623 ns->ln_dirent_repaired++;
5629 if (bk->lb_param & LPF_DRYRUN &&
5630 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5631 lfsck_pos_fill(env, lfsck,
5632 &ns->ln_pos_first_inconsistent,
5637 ns->ln_name_hash_repaired++;
5639 /* Not count repeatedly. */
5641 ns->ln_items_repaired++;
5643 CDEBUG(D_LFSCK, "%s: namespace LFSCK "
5644 "assistant repaired the entry: "
5647 lfsck_lfsck2name(lfsck),
5648 PFID(&lnr->lnr_fid), PFID(pfid),
5649 lnr->lnr_namelen, lnr->lnr_name);
5652 if (bk->lb_param & LPF_DRYRUN &&
5653 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5654 lfsck_pos_fill(env, lfsck,
5655 &ns->ln_pos_first_inconsistent,
5662 if (count > 1 || la->la_nlink > 1)
5663 ns->ln_mul_linked_checked++;
5665 up_write(&com->lc_sem);
5667 if (obj != NULL && !IS_ERR(obj))
5668 lfsck_object_put(env, obj);
5670 if (dir != NULL && !IS_ERR(dir))
5671 lfsck_object_put(env, dir);
5673 lad->lad_advance_lock = bad_linkea;
5679 * Handle one orphan under the backend /lost+found directory
5681 * Insert the orphan FID into the namespace LFSCK trace file for further
5682 * processing (via the subsequent namespace LFSCK second-stage scanning).
5683 * At the same time, remove the orphan name entry from backend /lost+found
5684 * directory. There is an interval between the orphan name entry removed
5685 * from the backend /lost+found directory and the orphan FID in the LFSCK
5686 * trace file handled. In such interval, the LFSCK can be reset, then
5687 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5688 * To guarantee that the orphans can be found when LFSCK run next time
5689 * without e2fsck again, when remove the orphan name entry, the LFSCK
5690 * will set the orphan's ctime attribute as 1. Since normal applications
5691 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5692 * next time, it can record the object (that ctime is 1) in the namespace
5693 * LFSCK trace file during the first-stage scanning.
5695 * \param[in] env pointer to the thread context
5696 * \param[in] com pointer to the lfsck component
5697 * \param[in] parent pointer to the object for the backend /lost+found
5698 * \param[in] ent pointer to the name entry for the target under the
5699 * backend /lost+found
5701 * \retval positive for repaired
5702 * \retval 0 if needs to repair nothing
5703 * \retval negative error number on failure
5705 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5706 struct lfsck_component *com,
5707 struct dt_object *parent,
5708 struct lu_dirent *ent)
5710 struct lfsck_thread_info *info = lfsck_env_info(env);
5711 struct lu_fid *key = &info->lti_fid;
5712 struct lu_attr *la = &info->lti_la;
5713 struct lfsck_instance *lfsck = com->lc_lfsck;
5714 struct dt_object *obj;
5715 struct dt_device *dev = lfsck->li_bottom;
5716 struct dt_object *child = NULL;
5717 struct thandle *th = NULL;
5724 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5726 RETURN(PTR_ERR(child));
5728 LASSERT(dt_object_exists(child));
5729 LASSERT(!dt_object_remote(child));
5731 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5732 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5733 fid_cpu_to_be(key, &ent->lde_fid);
5734 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5735 (const struct dt_key *)key);
5738 flags |= LNTF_CHECK_ORPHAN;
5739 } else if (rc == -ENOENT) {
5740 flags = LNTF_CHECK_ORPHAN;
5745 th = dt_trans_create(env, dev);
5747 GOTO(out, rc = PTR_ERR(th));
5749 /* a1. remove name entry from backend /lost+found */
5750 rc = dt_declare_delete(env, parent,
5751 (const struct dt_key *)ent->lde_name, th);
5755 if (S_ISDIR(lfsck_object_type(child))) {
5756 /* a2. decrease parent's nlink */
5757 rc = dt_declare_ref_del(env, parent, th);
5763 /* a3. remove child's FID from the LFSCK trace file. */
5764 rc = dt_declare_delete(env, obj,
5765 (const struct dt_key *)key, th);
5769 /* a4. set child's ctime as 1 */
5770 memset(la, 0, sizeof(*la));
5772 la->la_valid = LA_CTIME;
5773 rc = dt_declare_attr_set(env, child, la, th);
5778 /* a5. insert child's FID into the LFSCK trace file. */
5779 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5780 (const struct dt_key *)key, th);
5784 rc = dt_trans_start_local(env, dev, th);
5788 /* b1. remove name entry from backend /lost+found */
5789 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th);
5793 if (S_ISDIR(lfsck_object_type(child))) {
5794 /* b2. decrease parent's nlink */
5795 dt_write_lock(env, parent, 0);
5796 rc = dt_ref_del(env, parent, th);
5797 dt_write_unlock(env, parent);
5803 /* a3. remove child's FID from the LFSCK trace file. */
5804 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
5808 /* b4. set child's ctime as 1 */
5809 rc = dt_attr_set(env, child, la, th);
5814 /* b5. insert child's FID into the LFSCK trace file. */
5815 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5816 (const struct dt_key *)key, th, 1);
5818 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5821 dt_trans_stop(env, dev, th);
5824 lfsck_object_put(env, child);
5830 * Handle orphans under the backend /lost+found directory
5832 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5833 * and put them under the backend /lost+found directory that is invisible
5834 * to client. The LFSCK will scan such directory, for the original client
5835 * visible orphans, add their fids into the namespace LFSCK trace file,
5836 * then the subsenquent namespace LFSCK second-stage scanning can handle
5837 * them as other objects to be double scanned: either move back to normal
5838 * namespace, or to the global visible orphan directory:
5839 * /ROOT/.lustre/lost+found/MDTxxxx/
5841 * \param[in] env pointer to the thread context
5842 * \param[in] com pointer to the lfsck component
5844 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5845 struct lfsck_component *com)
5847 struct lfsck_thread_info *info = lfsck_env_info(env);
5848 struct lu_dirent *ent =
5849 (struct lu_dirent *)info->lti_key;
5850 struct lu_seq_range *range = &info->lti_range;
5851 struct lfsck_instance *lfsck = com->lc_lfsck;
5852 struct ptlrpc_thread *thread = &lfsck->li_thread;
5853 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5854 struct lfsck_namespace *ns = com->lc_file_ram;
5855 struct dt_object *parent;
5856 const struct dt_it_ops *iops;
5858 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5860 __u32 idx = lfsck_dev_idx(lfsck);
5865 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5866 &LU_BACKEND_LPF_FID);
5867 if (IS_ERR(parent)) {
5868 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5869 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5873 /* It is normal that the /lost+found does not exist for ZFS backend. */
5874 if (!dt_object_exists(parent))
5877 if (unlikely(!dt_try_as_dir(env, parent)))
5878 GOTO(out, rc = -ENOTDIR);
5880 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5881 lfsck_lfsck2name(lfsck));
5883 com->lc_new_scanned = 0;
5884 iops = &parent->do_index_ops->dio_it;
5885 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE);
5887 GOTO(out, rc = PTR_ERR(di));
5889 rc = iops->load(env, di, 0);
5891 rc = iops->next(env, di);
5896 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5897 unlikely(!thread_is_running(thread)))
5900 rc = iops->rec(env, di, (struct dt_rec *)ent,
5901 LUDA_64BITHASH | LUDA_TYPE);
5903 rc = lfsck_unpack_ent(ent, &cookie, &type);
5905 if (unlikely(rc != 0)) {
5906 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5907 "/lost+found: rc = %d\n",
5908 lfsck_lfsck2name(lfsck), rc);
5913 /* skip dot and dotdot entries */
5914 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5917 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5920 if (fid_is_norm(&ent->lde_fid)) {
5921 fld_range_set_mdt(range);
5922 rc = fld_local_lookup(env, ss->ss_server_fld,
5923 fid_seq(&ent->lde_fid), range);
5926 } else if (idx != 0) {
5927 /* If the returned FID is IGIF, then there are three
5930 * 1) The object is upgraded from old Lustre-1.8 with
5931 * IGIF assigned to such object.
5932 * 2) The object is a backend local object and is
5933 * invisible to client.
5934 * 3) The object lost its LMV EA, and since there is
5935 * no FID-in-dirent for the orphan in the backend
5936 * /lost+found directory, then the low layer will
5937 * return IGIF for such object.
5939 * For MDTx (x != 0), it is either case 2) or case 3),
5940 * but from the LFSCK view, they are indistinguishable.
5941 * To be safe, the LFSCK will keep it there and report
5942 * some message, then the adminstrator can handle that
5945 * For MDT0, it is more possible the case 1). The LFSCK
5946 * will handle the orphan as an upgraded object. */
5947 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5948 "in the backend /lost+found on the MDT %04x, "
5949 "to be safe, skip it.\n",
5950 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5951 ent->lde_name, PFID(&ent->lde_fid), idx);
5955 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5958 down_write(&com->lc_sem);
5959 com->lc_new_scanned++;
5960 ns->ln_local_lpf_scanned++;
5962 ns->ln_local_lpf_moved++;
5964 ns->ln_local_lpf_skipped++;
5966 ns->ln_local_lpf_failed++;
5967 up_write(&com->lc_sem);
5969 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5973 lfsck_control_speed_by_self(com);
5974 if (unlikely(!thread_is_running(thread))) {
5979 rc = iops->next(env, di);
5983 iops->fini(env, di);
5988 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5989 lfsck_lfsck2name(lfsck), rc);
5991 lfsck_object_put(env, parent);
5995 * Rescan the striped directory after the master LMV EA reset.
5997 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5998 * the namespace LFSCK engine scan the striped directory for the first time,
5999 * it will be regarded as a normal directory. As the LFSCK processing, some
6000 * other LFSCK instance on other MDT will find the shard of this striped dir,
6001 * and find that the master MDT-object of the striped directory lost its LMV
6002 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
6003 * notify the LFSCK instance on this MDT to rescan the striped directory.
6005 * \param[in] env pointer to the thread context
6006 * \param[in] com pointer to the lfsck component
6007 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
6010 * \retval positive number for success
6011 * \retval 0 for LFSCK stopped/paused
6012 * \retval negative error number on failure
6014 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
6015 struct lfsck_component *com,
6016 struct lfsck_lmv_unit *llu)
6018 struct lfsck_thread_info *info = lfsck_env_info(env);
6019 struct lfsck_instance *lfsck = com->lc_lfsck;
6020 struct lfsck_assistant_data *lad = com->lc_data;
6021 struct dt_object *dir;
6022 const struct dt_it_ops *iops;
6024 struct lu_dirent *ent =
6025 (struct lu_dirent *)info->lti_key;
6026 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6027 struct ptlrpc_thread *thread = &lfsck->li_thread;
6028 struct lfsck_assistant_object *lso = NULL;
6029 struct lfsck_namespace_req *lnr;
6030 struct lfsck_assistant_req *lar;
6035 LASSERT(list_empty(&lad->lad_req_list));
6037 lfsck->li_lmv = &llu->llu_lmv;
6038 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
6039 rc = lfsck_open_dir(env, lfsck, 0);
6043 dir = lfsck->li_obj_dir;
6044 di = lfsck->li_di_dir;
6045 iops = &dir->do_index_ops->dio_it;
6047 rc = iops->rec(env, di, (struct dt_rec *)ent,
6048 lfsck->li_args_dir);
6050 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
6054 if (bk->lb_param & LPF_FAILOUT)
6060 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
6064 lso = lfsck_assistant_object_init(env,
6065 lfsck_dto2fid(dir), NULL,
6066 lfsck->li_pos_current.lp_oit_cookie, true);
6068 if (bk->lb_param & LPF_FAILOUT)
6069 GOTO(out, rc = PTR_ERR(lso));
6076 lnr = lfsck_namespace_assistant_req_init(lfsck, lso, ent, type);
6078 if (bk->lb_param & LPF_FAILOUT)
6079 GOTO(out, rc = PTR_ERR(lnr));
6084 lar = &lnr->lnr_lar;
6085 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6086 lfsck_namespace_assistant_req_fini(env, lar);
6087 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
6090 if (unlikely(!thread_is_running(thread)))
6094 rc = iops->next(env, di);
6098 if (lso != NULL && !IS_ERR(lso))
6099 lfsck_assistant_object_put(env, lso);
6101 lfsck_close_dir(env, lfsck, rc);
6105 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
6106 if (list_empty(&lad->lad_req_list))
6109 spin_lock(&lad->lad_lock);
6110 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
6112 list_del_init(&lar->lar_list);
6113 spin_unlock(&lad->lad_lock);
6115 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6116 lfsck_namespace_assistant_req_fini(env, lar);
6118 RETURN(rc == 0 ? 1 : rc);
6122 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
6123 struct lfsck_component *com,
6124 struct dt_object *obj, bool first)
6126 struct lfsck_instance *lfsck = com->lc_lfsck;
6127 struct ptlrpc_thread *thread = &lfsck->li_thread;
6128 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6129 struct lfsck_namespace *ns = com->lc_file_ram;
6130 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
6131 struct dt_object *target;
6139 di = iops->init(env, obj, 0);
6141 RETURN(PTR_ERR(di));
6144 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
6147 rc = iops->get(env, di, (const struct dt_key *)&fid);
6152 /* The start one either has been processed or does not exist,
6154 rc = iops->next(env, di);
6160 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6161 unlikely(!thread_is_running(thread)))
6164 key = iops->key(env, di);
6173 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
6174 if (!fid_is_sane(&fid)) {
6179 target = lfsck_object_find_bottom(env, lfsck, &fid);
6180 if (IS_ERR(target)) {
6181 rc = PTR_ERR(target);
6185 if (dt_object_exists(target)) {
6186 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
6188 rc = lfsck_namespace_double_scan_one(env, com,
6195 lfsck_object_put(env, target);
6198 down_write(&com->lc_sem);
6199 com->lc_new_checked++;
6200 com->lc_new_scanned++;
6201 if (rc >= 0 && fid_is_sane(&fid))
6202 ns->ln_fid_latest_scanned_phase2 = fid;
6204 ns->ln_objs_repaired_phase2++;
6206 ns->ln_objs_failed_phase2++;
6207 up_write(&com->lc_sem);
6209 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6212 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
6213 cfs_time_current())) &&
6214 com->lc_new_checked != 0) {
6215 down_write(&com->lc_sem);
6216 ns->ln_run_time_phase2 +=
6217 cfs_duration_sec(cfs_time_current() +
6218 HALF_SEC - com->lc_time_last_checkpoint);
6219 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6220 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6221 com->lc_new_checked = 0;
6222 rc = lfsck_namespace_store(env, com);
6223 up_write(&com->lc_sem);
6227 com->lc_time_last_checkpoint = cfs_time_current();
6228 com->lc_time_next_checkpoint =
6229 com->lc_time_last_checkpoint +
6230 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6233 lfsck_control_speed_by_self(com);
6234 if (unlikely(!thread_is_running(thread)))
6237 rc = iops->next(env, di);
6246 iops->fini(env, di);
6251 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
6252 struct lfsck_component *com)
6254 struct lfsck_instance *lfsck = com->lc_lfsck;
6255 struct lfsck_namespace *ns = com->lc_file_ram;
6260 while (!list_empty(&lfsck->li_list_lmv)) {
6261 struct lfsck_lmv_unit *llu;
6263 spin_lock(&lfsck->li_lock);
6264 llu = list_entry(lfsck->li_list_lmv.next,
6265 struct lfsck_lmv_unit, llu_link);
6266 list_del_init(&llu->llu_link);
6267 spin_unlock(&lfsck->li_lock);
6269 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6274 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6275 lfsck_lfsck2name(lfsck));
6277 lfsck_namespace_scan_local_lpf(env, com);
6279 com->lc_new_checked = 0;
6280 com->lc_new_scanned = 0;
6281 com->lc_time_last_checkpoint = cfs_time_current();
6282 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6283 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6285 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6286 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6287 com->lc_sub_trace_objs[i].lsto_obj, true);
6288 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6289 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6290 com->lc_sub_trace_objs[i].lsto_obj, false);
6292 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6293 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6298 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6299 struct lfsck_component *com,
6300 struct lfsck_position *pos)
6302 struct lfsck_assistant_data *lad = com->lc_data;
6303 struct lfsck_namespace_req *lnr;
6305 if (((struct lfsck_namespace *)(com->lc_file_ram))->ln_status !=
6309 if (list_empty(&lad->lad_req_list))
6312 lnr = list_entry(lad->lad_req_list.next,
6313 struct lfsck_namespace_req,
6315 pos->lp_oit_cookie = lnr->lnr_lar.lar_parent->lso_oit_cookie;
6316 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6317 pos->lp_dir_parent = lnr->lnr_lar.lar_parent->lso_fid;
6320 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6321 struct lfsck_component *com,
6324 struct lfsck_instance *lfsck = com->lc_lfsck;
6325 struct lfsck_namespace *ns = com->lc_file_ram;
6327 down_write(&com->lc_sem);
6328 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6329 HALF_SEC - com->lc_time_last_checkpoint);
6330 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6331 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6332 com->lc_new_checked = 0;
6335 if (ns->ln_flags & LF_INCOMPLETE)
6336 ns->ln_status = LS_PARTIAL;
6338 ns->ln_status = LS_COMPLETED;
6339 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6340 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6341 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6342 ns->ln_success_count++;
6343 } else if (rc == 0) {
6344 if (lfsck->li_status != 0)
6345 ns->ln_status = lfsck->li_status;
6347 ns->ln_status = LS_STOPPED;
6349 ns->ln_status = LS_FAILED;
6352 rc = lfsck_namespace_store(env, com);
6353 up_write(&com->lc_sem);
6359 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6360 struct ptlrpc_request *req,
6364 struct lfsck_async_interpret_args *laia = args;
6365 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6367 ltd->ltd_synced_failures = 1;
6374 * Notify remote LFSCK instances about former failures.
6376 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6377 * some LFSCK verification requests (maybe because of network issues or the MDT
6378 * itself trouble). During the respond gap the MDT may missed some name entries
6379 * verification, then the MDT cannot know whether related MDT-objects have been
6380 * referenced by related name entries or not, then in the second-stage scanning,
6381 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6382 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6383 * name entry for repairing the orphan.
6385 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6386 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6387 * have ever missed some name entries verification and should skip the handling
6388 * for orphan MDT-objects.
6390 * \param[in] env pointer to the thread context
6391 * \param[in] com pointer to the lfsck component
6392 * \param[in] lr pointer to the lfsck request
6394 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6395 struct lfsck_component *com,
6396 struct lfsck_request *lr)
6398 struct lfsck_async_interpret_args *laia =
6399 &lfsck_env_info(env)->lti_laia2;
6400 struct lfsck_assistant_data *lad = com->lc_data;
6401 struct lfsck_namespace *ns = com->lc_file_ram;
6402 struct lfsck_instance *lfsck = com->lc_lfsck;
6403 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6404 struct lfsck_tgt_desc *ltd;
6405 struct ptlrpc_request_set *set;
6410 if (!lad->lad_incomplete)
6413 set = ptlrpc_prep_set();
6415 GOTO(out, rc = -ENOMEM);
6417 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6418 memset(laia, 0, sizeof(*laia));
6419 lad->lad_touch_gen++;
6421 down_read(<ds->ltd_rw_sem);
6422 cfs_foreach_bit(lad->lad_bitmap, idx) {
6423 ltd = lfsck_ltd2tgt(ltds, idx);
6424 LASSERT(ltd != NULL);
6426 laia->laia_ltd = ltd;
6427 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6428 lfsck_namespace_assistant_sync_failures_interpret,
6429 laia, LFSCK_NOTIFY);
6431 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6432 "to sync failure with MDT %x: rc = %d\n",
6433 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6435 up_read(<ds->ltd_rw_sem);
6437 rc = ptlrpc_set_wait(set);
6438 ptlrpc_set_destroy(set);
6444 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6445 "to sync failure with MDTs, and related MDTs "
6446 "may handle orphan improperly: rc = %d\n",
6447 lfsck_lfsck2name(lfsck), rc);
6452 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6453 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6454 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6455 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6456 .la_double_scan_result = lfsck_namespace_double_scan_result,
6457 .la_req_fini = lfsck_namespace_assistant_req_fini,
6458 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6462 * Verify the specified linkEA entry for the given directory object.
6463 * If the object has no such linkEA entry or it has more other linkEA
6464 * entries, then re-generate the linkEA with the given information.
6466 * \param[in] env pointer to the thread context
6467 * \param[in] obj pointer to the dt_object to be handled
6468 * \param[in] cname the name for the child in the parent directory
6469 * \param[in] pfid the parent directory's FID for the linkEA
6471 * \retval 0 for success
6472 * \retval negative error number on failure
6474 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6475 const struct lu_name *cname, const struct lu_fid *pfid)
6477 struct dt_device *dev = lfsck_obj2dev(obj);
6478 struct linkea_data ldata = { NULL };
6479 struct lu_buf linkea_buf;
6482 int fl = LU_XATTR_CREATE;
6486 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6488 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6489 if (rc == -ENODATA) {
6491 } else if (rc == 0) {
6492 fl = LU_XATTR_REPLACE;
6493 if (ldata.ld_leh->leh_reccount != 1) {
6496 rc = linkea_links_find(&ldata, cname, pfid);
6505 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
6510 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6511 ldata.ld_leh->leh_len);
6512 th = dt_trans_create(env, dev);
6514 RETURN(PTR_ERR(th));
6516 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6517 XATTR_NAME_LINK, fl, th);
6521 rc = dt_trans_start_local(env, dev, th);
6525 dt_write_lock(env, obj, 0);
6526 rc = dt_xattr_set(env, obj, &linkea_buf,
6527 XATTR_NAME_LINK, fl, th);
6528 dt_write_unlock(env, obj);
6533 dt_trans_stop(env, dev, th);
6538 * Get the name and parent directory's FID from the first linkEA entry.
6540 * \param[in] env pointer to the thread context
6541 * \param[in] obj pointer to the object which get linkEA from
6542 * \param[out] name pointer to the buffer to hold the name
6543 * in the first linkEA entry
6544 * \param[out] pfid pointer to the buffer to hold the parent
6545 * directory's FID in the first linkEA entry
6547 * \retval 0 for success
6548 * \retval negative error number on failure
6550 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6551 char *name, struct lu_fid *pfid)
6553 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6554 struct linkea_data ldata = { NULL };
6557 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6561 linkea_first_entry(&ldata);
6562 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6563 /* To guarantee the 'name' is terminated with '0'. */
6564 memcpy(name, cname->ln_name, cname->ln_namelen);
6565 name[cname->ln_namelen] = 0;
6571 * Update the object's name entry with the given FID.
6573 * \param[in] env pointer to the thread context
6574 * \param[in] lfsck pointer to the lfsck instance
6575 * \param[in] dir pointer to the directory that holds
6577 * \param[in] name the name for the entry to be updated
6578 * \param[in] fid the new FID for the name entry referenced
6579 * \param[in] type the type for the name entry to be updated
6581 * \retval 0 for success
6582 * \retval negative error number on failure
6584 int lfsck_update_name_entry(const struct lu_env *env,
6585 struct lfsck_instance *lfsck,
6586 struct dt_object *dir, const char *name,
6587 const struct lu_fid *fid, __u32 type)
6589 struct lfsck_thread_info *info = lfsck_env_info(env);
6590 struct dt_insert_rec *rec = &info->lti_dt_rec;
6591 struct lfsck_lock_handle *llh = &info->lti_llh;
6592 struct dt_device *dev = lfsck_obj2dev(dir);
6598 rc = lfsck_lock(env, lfsck, dir, name, llh,
6599 MDS_INODELOCK_UPDATE, LCK_PW);
6603 th = dt_trans_create(env, dev);
6605 GOTO(unlock, rc = PTR_ERR(th));
6607 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6611 rec->rec_type = type;
6613 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6614 (const struct dt_key *)name, th);
6618 rc = dt_declare_ref_add(env, dir, th);
6622 rc = dt_trans_start_local(env, dev, th);
6626 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
6627 if (rc == -ENOENT) {
6635 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6636 (const struct dt_key *)name, th, 1);
6637 if (rc == 0 && S_ISDIR(type) && !exists) {
6638 dt_write_lock(env, dir, 0);
6639 rc = dt_ref_add(env, dir, th);
6640 dt_write_unlock(env, dir);
6646 dt_trans_stop(env, dev, th);
6650 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6651 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6652 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6657 int lfsck_namespace_setup(const struct lu_env *env,
6658 struct lfsck_instance *lfsck)
6660 struct lfsck_component *com;
6661 struct lfsck_namespace *ns;
6662 struct dt_object *root = NULL;
6663 struct dt_object *obj;
6668 LASSERT(lfsck->li_master);
6674 INIT_LIST_HEAD(&com->lc_link);
6675 INIT_LIST_HEAD(&com->lc_link_dir);
6676 init_rwsem(&com->lc_sem);
6677 atomic_set(&com->lc_ref, 1);
6678 com->lc_lfsck = lfsck;
6679 com->lc_type = LFSCK_TYPE_NAMESPACE;
6680 com->lc_ops = &lfsck_namespace_ops;
6681 com->lc_data = lfsck_assistant_data_init(
6682 &lfsck_namespace_assistant_ops,
6684 if (com->lc_data == NULL)
6685 GOTO(out, rc = -ENOMEM);
6687 com->lc_file_size = sizeof(struct lfsck_namespace);
6688 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6689 if (com->lc_file_ram == NULL)
6690 GOTO(out, rc = -ENOMEM);
6692 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6693 if (com->lc_file_disk == NULL)
6694 GOTO(out, rc = -ENOMEM);
6696 for (i = 0; i < LFSCK_STF_COUNT; i++)
6697 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6699 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6701 GOTO(out, rc = PTR_ERR(root));
6703 if (unlikely(!dt_try_as_dir(env, root)))
6704 GOTO(out, rc = -ENOTDIR);
6706 obj = local_index_find_or_create(env, lfsck->li_los, root,
6708 S_IFREG | S_IRUGO | S_IWUSR,
6709 &dt_lfsck_features);
6711 GOTO(out, rc = PTR_ERR(obj));
6714 rc = lfsck_namespace_load(env, com);
6716 rc = lfsck_namespace_init(env, com);
6718 rc = lfsck_namespace_reset(env, com, true);
6720 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6724 ns = com->lc_file_ram;
6725 switch (ns->ln_status) {
6730 spin_lock(&lfsck->li_lock);
6731 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6732 spin_unlock(&lfsck->li_lock);
6735 CERROR("%s: unknown lfsck_namespace status %d\n",
6736 lfsck_lfsck2name(lfsck), ns->ln_status);
6738 case LS_SCANNING_PHASE1:
6739 case LS_SCANNING_PHASE2:
6740 /* No need to store the status to disk right now.
6741 * If the system crashed before the status stored,
6742 * it will be loaded back when next time. */
6743 ns->ln_status = LS_CRASHED;
6747 spin_lock(&lfsck->li_lock);
6748 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6749 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6750 spin_unlock(&lfsck->li_lock);
6757 if (root != NULL && !IS_ERR(root))
6758 lfsck_object_put(env, root);
6760 lfsck_component_cleanup(env, com);
6761 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6762 lfsck_lfsck2name(lfsck), rc);