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, 2015, 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);
169 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
170 struct lfsck_namespace *src)
172 dst->ln_magic = cpu_to_le32(src->ln_magic);
173 dst->ln_status = cpu_to_le32(src->ln_status);
174 dst->ln_flags = cpu_to_le32(src->ln_flags);
175 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
176 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
177 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
178 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
179 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
180 dst->ln_time_last_checkpoint =
181 cpu_to_le64(src->ln_time_last_checkpoint);
182 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
183 &src->ln_pos_latest_start);
184 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
185 &src->ln_pos_last_checkpoint);
186 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
187 &src->ln_pos_first_inconsistent);
188 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
189 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
190 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
191 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
192 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
193 dst->ln_objs_repaired_phase2 =
194 cpu_to_le64(src->ln_objs_repaired_phase2);
195 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
196 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
197 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
198 &src->ln_fid_latest_scanned_phase2);
199 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
200 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
201 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
202 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
203 dst->ln_unknown_inconsistency =
204 cpu_to_le64(src->ln_unknown_inconsistency);
205 dst->ln_unmatched_pairs_repaired =
206 cpu_to_le64(src->ln_unmatched_pairs_repaired);
207 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
208 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
209 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
210 dst->ln_lost_dirent_repaired =
211 cpu_to_le64(src->ln_lost_dirent_repaired);
212 dst->ln_striped_dirs_scanned =
213 cpu_to_le64(src->ln_striped_dirs_scanned);
214 dst->ln_striped_dirs_repaired =
215 cpu_to_le64(src->ln_striped_dirs_repaired);
216 dst->ln_striped_dirs_failed =
217 cpu_to_le64(src->ln_striped_dirs_failed);
218 dst->ln_striped_dirs_disabled =
219 cpu_to_le64(src->ln_striped_dirs_disabled);
220 dst->ln_striped_dirs_skipped =
221 cpu_to_le64(src->ln_striped_dirs_skipped);
222 dst->ln_striped_shards_scanned =
223 cpu_to_le64(src->ln_striped_shards_scanned);
224 dst->ln_striped_shards_repaired =
225 cpu_to_le64(src->ln_striped_shards_repaired);
226 dst->ln_striped_shards_failed =
227 cpu_to_le64(src->ln_striped_shards_failed);
228 dst->ln_striped_shards_skipped =
229 cpu_to_le64(src->ln_striped_shards_skipped);
230 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
231 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
232 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
233 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
234 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
235 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
238 static void lfsck_namespace_record_failure(const struct lu_env *env,
239 struct lfsck_instance *lfsck,
240 struct lfsck_namespace *ns)
242 struct lfsck_position pos;
244 ns->ln_items_failed++;
245 lfsck_pos_fill(env, lfsck, &pos, false);
246 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
247 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
248 ns->ln_pos_first_inconsistent = pos;
250 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
251 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
252 lfsck_lfsck2name(lfsck),
253 ns->ln_pos_first_inconsistent.lp_oit_cookie,
254 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
255 ns->ln_pos_first_inconsistent.lp_dir_cookie);
260 * Load the MDT bitmap from the lfsck_namespace trace file.
262 * \param[in] env pointer to the thread context
263 * \param[in] com pointer to the lfsck component
265 * \retval 0 for success
266 * \retval negative error number on failure or data corruption
268 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
269 struct lfsck_component *com)
271 struct dt_object *obj = com->lc_obj;
272 struct lfsck_assistant_data *lad = com->lc_data;
273 struct lfsck_namespace *ns = com->lc_file_ram;
274 struct cfs_bitmap *bitmap = lad->lad_bitmap;
280 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
282 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
284 nbits = ns->ln_bitmap_size;
286 if (unlikely(nbits < BITS_PER_LONG))
287 nbits = BITS_PER_LONG;
289 if (nbits > bitmap->size) {
290 __u32 new_bits = bitmap->size;
291 struct cfs_bitmap *new_bitmap;
293 while (new_bits < nbits)
296 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
297 if (new_bitmap == NULL)
300 lad->lad_bitmap = new_bitmap;
301 CFS_FREE_BITMAP(bitmap);
305 if (ns->ln_bitmap_size == 0) {
306 lad->lad_incomplete = 0;
307 CFS_RESET_BITMAP(bitmap);
312 size = (ns->ln_bitmap_size + 7) >> 3;
313 rc = dt_xattr_get(env, obj,
314 lfsck_buf_get(env, bitmap->data, size),
315 XATTR_NAME_LFSCK_BITMAP);
317 RETURN(rc >= 0 ? -EINVAL : rc);
319 if (cfs_bitmap_check_empty(bitmap))
320 lad->lad_incomplete = 0;
322 lad->lad_incomplete = 1;
328 * Load namespace LFSCK statistics information from the trace file.
330 * \param[in] env pointer to the thread context
331 * \param[in] com pointer to the lfsck component
333 * \retval 0 for success
334 * \retval negative error number on failure
336 static int lfsck_namespace_load(const struct lu_env *env,
337 struct lfsck_component *com)
339 int len = com->lc_file_size;
342 rc = dt_xattr_get(env, com->lc_obj,
343 lfsck_buf_get(env, com->lc_file_disk, len),
344 XATTR_NAME_LFSCK_NAMESPACE);
346 struct lfsck_namespace *ns = com->lc_file_ram;
348 lfsck_namespace_le_to_cpu(ns,
349 (struct lfsck_namespace *)com->lc_file_disk);
350 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
351 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
352 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
353 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
358 } else if (rc != -ENODATA) {
359 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
360 "expected = %d: rc = %d\n",
361 lfsck_lfsck2name(com->lc_lfsck), len, rc);
369 static int lfsck_namespace_store(const struct lu_env *env,
370 struct lfsck_component *com)
372 struct dt_object *obj = com->lc_obj;
373 struct lfsck_instance *lfsck = com->lc_lfsck;
374 struct lfsck_namespace *ns = com->lc_file_ram;
375 struct lfsck_assistant_data *lad = com->lc_data;
376 struct dt_device *dev = lfsck_obj2dev(obj);
377 struct cfs_bitmap *bitmap = NULL;
378 struct thandle *handle;
380 int len = com->lc_file_size;
385 bitmap = lad->lad_bitmap;
386 nbits = bitmap->size;
389 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
392 ns->ln_bitmap_size = nbits;
393 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
395 handle = dt_trans_create(env, dev);
397 GOTO(log, rc = PTR_ERR(handle));
399 rc = dt_declare_xattr_set(env, obj,
400 lfsck_buf_get(env, com->lc_file_disk, len),
401 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
405 if (bitmap != NULL) {
406 rc = dt_declare_xattr_set(env, obj,
407 lfsck_buf_get(env, bitmap->data, nbits >> 3),
408 XATTR_NAME_LFSCK_BITMAP, 0, handle);
413 rc = dt_trans_start_local(env, dev, handle);
417 rc = dt_xattr_set(env, obj,
418 lfsck_buf_get(env, com->lc_file_disk, len),
419 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
420 if (rc == 0 && bitmap != NULL)
421 rc = dt_xattr_set(env, obj,
422 lfsck_buf_get(env, bitmap->data, nbits >> 3),
423 XATTR_NAME_LFSCK_BITMAP, 0, handle);
428 dt_trans_stop(env, dev, handle);
432 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
433 lfsck_lfsck2name(lfsck), rc);
437 static struct dt_object *
438 lfsck_namespace_load_one_trace_file(const struct lu_env *env,
439 struct lfsck_component *com,
440 struct dt_object *parent,
441 const char *name, bool reset)
443 struct lfsck_instance *lfsck = com->lc_lfsck;
444 struct dt_object *obj;
448 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
449 if (rc != 0 && rc != -ENOENT)
453 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
454 S_IFREG | S_IRUGO | S_IWUSR,
460 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
461 struct lfsck_component *com,
464 char *name = lfsck_env_info(env)->lti_key;
465 struct lfsck_sub_trace_obj *lsto;
466 struct dt_object *obj;
470 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
471 i < LFSCK_STF_COUNT; i++, lsto++) {
472 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
473 if (lsto->lsto_obj != NULL) {
477 lfsck_object_put(env, lsto->lsto_obj);
478 lsto->lsto_obj = NULL;
481 obj = lfsck_namespace_load_one_trace_file(env, com,
482 com->lc_lfsck->li_lfsck_dir, name, reset);
486 lsto->lsto_obj = obj;
487 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
495 static int lfsck_namespace_init(const struct lu_env *env,
496 struct lfsck_component *com)
498 struct lfsck_namespace *ns = com->lc_file_ram;
501 memset(ns, 0, sizeof(*ns));
502 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
503 ns->ln_status = LS_INIT;
504 down_write(&com->lc_sem);
505 rc = lfsck_namespace_store(env, com);
506 up_write(&com->lc_sem);
508 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
514 * Update the namespace LFSCK trace file for the given @fid
516 * \param[in] env pointer to the thread context
517 * \param[in] com pointer to the lfsck component
518 * \param[in] fid the fid which flags to be updated in the lfsck
520 * \param[in] add true if add new flags, otherwise remove flags
522 * \retval 0 for success or nothing to be done
523 * \retval negative error number on failure
525 int lfsck_namespace_trace_update(const struct lu_env *env,
526 struct lfsck_component *com,
527 const struct lu_fid *fid,
528 const __u8 flags, bool add)
530 struct lfsck_instance *lfsck = com->lc_lfsck;
531 struct dt_object *obj;
532 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
533 struct dt_device *dev;
534 struct thandle *th = NULL;
543 if (unlikely(!fid_is_sane(fid)))
546 idx = lfsck_sub_trace_file_fid2idx(fid);
547 obj = com->lc_sub_trace_objs[idx].lsto_obj;
548 dev = lfsck_obj2dev(obj);
549 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
550 fid_cpu_to_be(key, fid);
551 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
552 (const struct dt_key *)key);
555 GOTO(unlock, rc = 0);
559 } else if (rc == 0) {
561 if ((old & flags) == flags)
562 GOTO(unlock, rc = 0);
566 if ((old & flags) == 0)
567 GOTO(unlock, rc = 0);
575 th = dt_trans_create(env, dev);
577 GOTO(log, rc = PTR_ERR(th));
580 rc = dt_declare_delete(env, obj,
581 (const struct dt_key *)key, th);
587 rc = dt_declare_insert(env, obj,
588 (const struct dt_rec *)&new,
589 (const struct dt_key *)key, th);
594 rc = dt_trans_start_local(env, dev, th);
599 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
605 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
606 (const struct dt_key *)key, th, 1);
614 if (th != NULL && !IS_ERR(th))
615 dt_trans_stop(env, dev, th);
617 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
618 "trace file, flags %x, old %x, new %x: rc = %d\n",
619 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
620 (__u32)flags, (__u32)old, (__u32)new, rc);
623 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
628 int lfsck_namespace_check_exist(const struct lu_env *env,
629 struct dt_object *dir,
630 struct dt_object *obj, const char *name)
632 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
636 if (unlikely(lfsck_is_dead_obj(obj)))
637 RETURN(LFSCK_NAMEENTRY_DEAD);
639 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
640 (const struct dt_key *)name);
642 RETURN(LFSCK_NAMEENTRY_REMOVED);
647 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
648 RETURN(LFSCK_NAMEENTRY_RECREATED);
653 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
654 struct dt_object *obj,
655 struct thandle *handle)
659 /* For destroying all invalid linkEA entries. */
660 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
664 /* For insert new linkEA entry. */
665 rc = dt_declare_xattr_set(env, obj,
666 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
667 XATTR_NAME_LINK, 0, handle);
671 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
672 struct linkea_data *ldata)
676 if (ldata->ld_buf->lb_buf == NULL)
679 if (!dt_object_exists(obj))
682 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
684 /* Buf was too small, figure out what we need. */
685 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
689 lu_buf_realloc(ldata->ld_buf, rc);
690 if (ldata->ld_buf->lb_buf == NULL)
693 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
697 rc = linkea_init(ldata);
703 * Remove linkEA for the given object.
705 * The caller should take the ldlm lock before the calling.
707 * \param[in] env pointer to the thread context
708 * \param[in] com pointer to the lfsck component
709 * \param[in] obj pointer to the dt_object to be handled
711 * \retval 0 for repaired cases
712 * \retval negative error number on failure
714 static int lfsck_namespace_links_remove(const struct lu_env *env,
715 struct lfsck_component *com,
716 struct dt_object *obj)
718 struct lfsck_instance *lfsck = com->lc_lfsck;
719 struct dt_device *dev = lfsck_obj2dev(obj);
720 struct thandle *th = NULL;
724 LASSERT(dt_object_remote(obj) == 0);
726 th = dt_trans_create(env, dev);
728 GOTO(log, rc = PTR_ERR(th));
730 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
734 rc = dt_trans_start_local(env, dev, th);
738 dt_write_lock(env, obj, 0);
739 if (unlikely(lfsck_is_dead_obj(obj)))
740 GOTO(unlock, rc = -ENOENT);
742 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
743 GOTO(unlock, rc = 0);
745 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
750 dt_write_unlock(env, obj);
753 dt_trans_stop(env, dev, th);
756 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
757 "for the object "DFID": rc = %d\n",
758 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
761 struct lfsck_namespace *ns = com->lc_file_ram;
763 ns->ln_flags |= LF_INCONSISTENT;
769 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
770 struct linkea_data *ldata, struct thandle *handle)
772 const struct lu_buf *buf = lfsck_buf_get_const(env,
773 ldata->ld_buf->lb_buf,
774 ldata->ld_leh->leh_len);
776 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle);
779 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
780 struct lu_name *cname,
784 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
785 /* To guarantee the 'name' is terminated with '0'. */
786 memcpy(buf, cname->ln_name, cname->ln_namelen);
787 buf[cname->ln_namelen] = 0;
788 cname->ln_name = buf;
791 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
792 struct lu_name *cname,
796 struct link_ea_entry *oldlee;
800 oldlee = ldata->ld_lee;
801 oldlen = ldata->ld_reclen;
802 linkea_next_entry(ldata);
803 while (ldata->ld_lee != NULL) {
804 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
805 ldata->ld_lee->lee_reclen[1];
806 if (unlikely(ldata->ld_reclen == oldlen &&
807 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
812 linkea_del_buf(ldata, cname);
814 linkea_next_entry(ldata);
817 ldata->ld_lee = oldlee;
818 ldata->ld_reclen = oldlen;
824 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
826 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
827 * with the given type to generate the name, the detailed rules for name
828 * have been described as following.
830 * The function also generates the linkEA corresponding to the name entry
831 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
833 * \param[in] env pointer to the thread context
834 * \param[in] com pointer to the lfsck component
835 * \param[in] orphan pointer to the orphan MDT-object
836 * \param[in] infix additional information for the orphan name, such as
837 * the FID for original
838 * \param[in] type the type for describing why the orphan MDT-object is
839 * created. The rules are as following:
841 * type "D": The MDT-object is a directory, it may knows its parent
842 * but because there is no valid linkEA, the LFSCK cannot
843 * know where to put it back to the namespace.
844 * type "O": The MDT-object has no linkEA, and there is no name
845 * entry that references the MDT-object.
847 * type "S": The orphan MDT-object is a shard of a striped directory
849 * \see lfsck_layout_recreate_parent() for more types.
851 * The orphan name will be like:
852 * ${FID}-${infix}-${type}-${conflict_version}
854 * \param[out] count if some others inserted some linkEA entries by race,
855 * then return the linkEA entries count.
857 * \retval positive number for repaired cases
858 * \retval 0 if needs to repair nothing
859 * \retval negative error number on failure
861 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
862 struct lfsck_component *com,
863 struct dt_object *orphan,
864 const char *infix, const char *type,
867 struct lfsck_thread_info *info = lfsck_env_info(env);
868 struct lu_name *cname = &info->lti_name;
869 struct dt_insert_rec *rec = &info->lti_dt_rec;
870 struct lu_attr *la = &info->lti_la2;
871 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
872 const struct lu_fid *pfid;
874 struct lfsck_instance *lfsck = com->lc_lfsck;
875 struct dt_device *dev = lfsck_obj2dev(orphan);
876 struct dt_object *parent;
877 struct thandle *th = NULL;
878 struct lfsck_lock_handle *pllh = &info->lti_llh;
879 struct lustre_handle clh = { 0 };
880 struct linkea_data ldata = { NULL };
881 struct lu_buf linkea_buf;
888 cname->ln_name = NULL;
889 if (unlikely(lfsck->li_lpf_obj == NULL))
890 GOTO(log, rc = -ENXIO);
892 parent = lfsck->li_lpf_obj;
893 pfid = lfsck_dto2fid(parent);
897 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
898 PFID(cfid), infix, type, idx++);
899 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
900 (const struct dt_key *)info->lti_key);
901 if (rc != 0 && rc != -ENOENT)
904 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
906 } while (rc == 0 && !exist);
908 rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
909 MDS_INODELOCK_UPDATE, LCK_PW);
913 /* Re-check whether the name conflict with othrs after taken
915 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
916 (const struct dt_key *)info->lti_key);
918 if (!lu_fid_eq(cfid, &tfid)) {
925 } else if (rc != -ENOENT) {
931 cname->ln_name = info->lti_key;
932 cname->ln_namelen = namelen;
933 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
937 rc = linkea_add_buf(&ldata, cname, pfid);
941 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
942 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
943 MDS_INODELOCK_XATTR, LCK_EX);
947 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
948 ldata.ld_leh->leh_len);
949 th = dt_trans_create(env, dev);
951 GOTO(log, rc = PTR_ERR(th));
953 if (S_ISDIR(lfsck_object_type(orphan))) {
954 rc = dt_declare_delete(env, orphan,
955 (const struct dt_key *)dotdot, th);
959 rec->rec_type = S_IFDIR;
961 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
962 (const struct dt_key *)dotdot, th);
967 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
968 XATTR_NAME_LINK, 0, th);
973 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
975 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
976 (const struct dt_key *)cname->ln_name,
981 if (S_ISDIR(rec->rec_type)) {
982 rc = dt_declare_ref_add(env, parent, th);
988 memset(la, 0, sizeof(*la));
989 la->la_ctime = cfs_time_current_sec();
990 la->la_valid = LA_CTIME;
991 rc = dt_declare_attr_set(env, orphan, la, th);
995 rc = dt_trans_start_local(env, dev, th);
999 dt_write_lock(env, orphan, 0);
1000 rc = lfsck_links_read(env, orphan, &ldata);
1001 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
1002 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
1003 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1004 GOTO(unlock, rc = 1);
1006 if (S_ISDIR(lfsck_object_type(orphan))) {
1007 rc = dt_delete(env, orphan,
1008 (const struct dt_key *)dotdot, th);
1012 rec->rec_type = S_IFDIR;
1013 rec->rec_fid = pfid;
1014 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1015 (const struct dt_key *)dotdot, th, 1);
1020 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1023 if (rc == 0 && count != NULL)
1024 *count = ldata.ld_leh->leh_reccount;
1028 dt_write_unlock(env, orphan);
1030 if (rc == 0 && !exist) {
1031 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1032 rec->rec_fid = cfid;
1033 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1034 (const struct dt_key *)cname->ln_name, th, 1);
1035 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1036 dt_write_lock(env, parent, 0);
1037 rc = dt_ref_add(env, parent, th);
1038 dt_write_unlock(env, parent);
1043 rc = dt_attr_set(env, orphan, la, th);
1045 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1048 dt_write_unlock(env, orphan);
1051 dt_trans_stop(env, dev, th);
1054 lfsck_ibits_unlock(&clh, LCK_EX);
1056 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1057 "object "DFID", name = %s: rc = %d\n",
1058 lfsck_lfsck2name(lfsck), PFID(cfid),
1059 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1062 struct lfsck_namespace *ns = com->lc_file_ram;
1064 ns->ln_flags |= LF_INCONSISTENT;
1071 * Add the specified name entry back to namespace.
1073 * If there is a linkEA entry that back references a name entry under
1074 * some parent directory, but such parent directory does not have the
1075 * claimed name entry. On the other hand, the linkEA entries count is
1076 * not larger than the MDT-object's hard link count. Under such case,
1077 * it is quite possible that the name entry is lost. Then the LFSCK
1078 * should add the name entry back to the namespace.
1080 * \param[in] env pointer to the thread context
1081 * \param[in] com pointer to the lfsck component
1082 * \param[in] parent pointer to the directory under which the name entry
1083 * will be inserted into
1084 * \param[in] child pointer to the object referenced by the name entry
1085 * that to be inserted into the parent
1086 * \param[in] name the name for the child in the parent directory
1088 * \retval positive number for repaired cases
1089 * \retval 0 if nothing to be repaired
1090 * \retval negative error number on failure
1092 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1093 struct lfsck_component *com,
1094 struct dt_object *parent,
1095 struct dt_object *child,
1098 struct lfsck_thread_info *info = lfsck_env_info(env);
1099 struct lu_attr *la = &info->lti_la;
1100 struct dt_insert_rec *rec = &info->lti_dt_rec;
1101 struct lfsck_instance *lfsck = com->lc_lfsck;
1102 /* The child and its name may be on different MDTs. */
1103 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1104 const struct lu_fid *cfid = lfsck_dto2fid(child);
1105 struct dt_device *dev = lfsck->li_next;
1106 struct thandle *th = NULL;
1107 struct lfsck_lock_handle *llh = &info->lti_llh;
1111 /* @parent/@child may be based on lfsck->li_bottom,
1112 * but here we need the object based on the lfsck->li_next. */
1114 parent = lfsck_object_locate(dev, parent);
1116 GOTO(log, rc = PTR_ERR(parent));
1118 if (unlikely(!dt_try_as_dir(env, parent)))
1119 GOTO(log, rc = -ENOTDIR);
1121 child = lfsck_object_locate(dev, child);
1123 GOTO(log, rc = PTR_ERR(child));
1125 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1128 rc = lfsck_lock(env, lfsck, parent, name, llh,
1129 MDS_INODELOCK_UPDATE, LCK_PW);
1133 th = dt_trans_create(env, dev);
1135 GOTO(unlock, rc = PTR_ERR(th));
1137 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1138 rec->rec_fid = cfid;
1139 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1140 (const struct dt_key *)name, th);
1144 if (S_ISDIR(rec->rec_type)) {
1145 rc = dt_declare_ref_add(env, parent, th);
1150 memset(la, 0, sizeof(*la));
1151 la->la_ctime = cfs_time_current_sec();
1152 la->la_valid = LA_CTIME;
1153 rc = dt_declare_attr_set(env, parent, la, th);
1157 rc = dt_declare_attr_set(env, child, la, th);
1161 rc = dt_trans_start_local(env, dev, th);
1165 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1166 (const struct dt_key *)name, th, 1);
1170 if (S_ISDIR(rec->rec_type)) {
1171 dt_write_lock(env, parent, 0);
1172 rc = dt_ref_add(env, parent, th);
1173 dt_write_unlock(env, parent);
1178 la->la_ctime = cfs_time_current_sec();
1179 rc = dt_attr_set(env, parent, la, th);
1183 rc = dt_attr_set(env, child, la, th);
1185 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1188 dt_trans_stop(env, dev, th);
1194 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1195 "the name %s and type %o to the parent "DFID": rc = %d\n",
1196 lfsck_lfsck2name(lfsck), PFID(cfid), name,
1197 lfsck_object_type(child) & S_IFMT, PFID(pfid), rc);
1200 struct lfsck_namespace *ns = com->lc_file_ram;
1202 ns->ln_flags |= LF_INCONSISTENT;
1204 ns->ln_lost_dirent_repaired++;
1211 * Create the specified orphan directory.
1213 * For the case that the parent MDT-object stored in some MDT-object's
1214 * linkEA entry is lost, the LFSCK will re-create the parent object as
1215 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1216 * with the name ${FID}-P-${conflict_version}.
1218 * \param[in] env pointer to the thread context
1219 * \param[in] com pointer to the lfsck component
1220 * \param[in] orphan pointer to the orphan MDT-object to be created
1221 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1223 * \retval positive number for repaired cases
1224 * \retval negative error number on failure
1226 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1227 struct lfsck_component *com,
1228 struct dt_object *orphan,
1229 struct lmv_mds_md_v1 *lmv)
1231 struct lfsck_thread_info *info = lfsck_env_info(env);
1232 struct lu_attr *la = &info->lti_la;
1233 struct dt_allocation_hint *hint = &info->lti_hint;
1234 struct dt_object_format *dof = &info->lti_dof;
1235 struct lu_name *cname = &info->lti_name2;
1236 struct dt_insert_rec *rec = &info->lti_dt_rec;
1237 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1238 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1240 struct lfsck_instance *lfsck = com->lc_lfsck;
1241 struct lfsck_namespace *ns = com->lc_file_ram;
1242 struct dt_device *dev = lfsck_obj2dev(orphan);
1243 struct dt_object *parent = NULL;
1244 struct thandle *th = NULL;
1245 struct lfsck_lock_handle *llh = &info->lti_llh;
1246 struct linkea_data ldata = { NULL };
1247 struct lu_buf linkea_buf;
1248 struct lu_buf lmv_buf;
1256 LASSERT(!dt_object_exists(orphan));
1258 cname->ln_name = NULL;
1259 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1262 if (dt_object_remote(orphan)) {
1263 LASSERT(lfsck->li_lpf_root_obj != NULL);
1265 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1267 GOTO(log, rc = idx);
1269 snprintf(name, 8, "MDT%04x", idx);
1270 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1271 (struct dt_rec *)&tfid,
1272 (const struct dt_key *)name);
1274 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1276 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1278 GOTO(log, rc = PTR_ERR(parent));
1280 if (unlikely(!dt_try_as_dir(env, parent)))
1281 GOTO(log, rc = -ENOTDIR);
1283 if (unlikely(lfsck->li_lpf_obj == NULL))
1284 GOTO(log, rc = -ENXIO);
1286 parent = lfsck->li_lpf_obj;
1289 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1291 GOTO(log, rc = PTR_ERR(dev));
1297 namelen = snprintf(name, 31, DFID"-P-%d",
1299 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1300 (const struct dt_key *)name);
1301 if (rc != 0 && rc != -ENOENT)
1305 rc = lfsck_lock(env, lfsck, parent, name, llh,
1306 MDS_INODELOCK_UPDATE, LCK_PW);
1310 /* Re-check whether the name conflict with othrs after taken
1312 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1313 (const struct dt_key *)name);
1314 if (unlikely(rc == 0)) {
1322 cname->ln_name = name;
1323 cname->ln_namelen = namelen;
1325 memset(la, 0, sizeof(*la));
1326 la->la_mode = S_IFDIR | 0700;
1327 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1328 LA_ATIME | LA_MTIME | LA_CTIME;
1330 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1331 la->la_mode & S_IFMT);
1333 memset(dof, 0, sizeof(*dof));
1334 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1336 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1340 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
1344 th = dt_trans_create(env, dev);
1346 GOTO(unlock1, rc = PTR_ERR(th));
1348 /* Sync the remote transaction to guarantee that the subsequent
1349 * lock against the @orphan can find the @orphan in time. */
1350 if (dt_object_remote(orphan))
1353 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1357 if (unlikely(!dt_try_as_dir(env, orphan)))
1358 GOTO(stop, rc = -ENOTDIR);
1360 rc = dt_declare_ref_add(env, orphan, th);
1364 rec->rec_type = S_IFDIR;
1365 rec->rec_fid = cfid;
1366 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1367 (const struct dt_key *)dot, th);
1371 rec->rec_fid = lfsck_dto2fid(parent);
1372 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1373 (const struct dt_key *)dotdot, th);
1378 lmv->lmv_magic = LMV_MAGIC;
1379 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1380 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1381 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1382 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1383 XATTR_NAME_LMV, 0, th);
1388 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1389 ldata.ld_leh->leh_len);
1390 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1391 XATTR_NAME_LINK, 0, th);
1395 rec->rec_fid = cfid;
1396 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1397 (const struct dt_key *)name, th);
1399 rc = dt_declare_ref_add(env, parent, th);
1404 rc = dt_trans_start_local(env, dev, th);
1408 dt_write_lock(env, orphan, 0);
1409 rc = dt_create(env, orphan, la, hint, dof, th);
1413 rc = dt_ref_add(env, orphan, th);
1417 rec->rec_fid = cfid;
1418 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1419 (const struct dt_key *)dot, th, 1);
1423 rec->rec_fid = lfsck_dto2fid(parent);
1424 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1425 (const struct dt_key *)dotdot, th, 1);
1430 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1435 rc = dt_xattr_set(env, orphan, &linkea_buf,
1436 XATTR_NAME_LINK, 0, th);
1437 dt_write_unlock(env, orphan);
1441 rec->rec_fid = cfid;
1442 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1443 (const struct dt_key *)name, th, 1);
1445 dt_write_lock(env, parent, 0);
1446 rc = dt_ref_add(env, parent, th);
1447 dt_write_unlock(env, parent);
1450 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1453 dt_write_unlock(env, orphan);
1456 rc1 = dt_trans_stop(env, dev, th);
1457 if (rc1 != 0 && rc > 0)
1464 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1465 "the object "DFID", name = %s: rc = %d\n",
1466 lfsck_lfsck2name(lfsck), PFID(cfid),
1467 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1469 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1470 lfsck_object_put(env, parent);
1473 ns->ln_flags |= LF_INCONSISTENT;
1479 * Remove the specified entry from the linkEA.
1481 * Locate the linkEA entry with the given @cname and @pfid, then
1482 * remove this entry or the other entries those are repeated with
1485 * \param[in] env pointer to the thread context
1486 * \param[in] com pointer to the lfsck component
1487 * \param[in] obj pointer to the dt_object to be handled
1488 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1489 * \param[in] cname the name for the child in the parent directory
1490 * \param[in] pfid the parent directory's FID for the linkEA
1491 * \param[in] next if true, then remove the first found linkEA
1492 * entry, and move the ldata->ld_lee to next entry
1494 * \retval positive number for repaired cases
1495 * \retval 0 if nothing to be repaired
1496 * \retval negative error number on failure
1498 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1499 struct lfsck_component *com,
1500 struct dt_object *obj,
1501 struct linkea_data *ldata,
1502 struct lu_name *cname,
1503 struct lu_fid *pfid,
1506 struct lfsck_instance *lfsck = com->lc_lfsck;
1507 struct dt_device *dev = lfsck_obj2dev(obj);
1508 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1509 struct thandle *th = NULL;
1510 struct lustre_handle lh = { 0 };
1511 struct linkea_data ldata_new = { NULL };
1512 struct lu_buf linkea_buf;
1516 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1517 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1523 linkea_del_buf(ldata, cname);
1525 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1527 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1528 ldata->ld_leh->leh_len);
1531 th = dt_trans_create(env, dev);
1533 GOTO(unlock1, rc = PTR_ERR(th));
1535 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1536 XATTR_NAME_LINK, 0, th);
1540 rc = dt_trans_start_local(env, dev, th);
1544 dt_write_lock(env, obj, 0);
1545 if (unlikely(lfsck_is_dead_obj(obj)))
1546 GOTO(unlock2, rc = -ENOENT);
1548 rc = lfsck_links_read2(env, obj, &ldata_new);
1552 /* The specified linkEA entry has been removed by race. */
1553 rc = linkea_links_find(&ldata_new, cname, pfid);
1555 GOTO(unlock2, rc = 0);
1557 if (bk->lb_param & LPF_DRYRUN)
1558 GOTO(unlock2, rc = 1);
1561 linkea_del_buf(&ldata_new, cname);
1563 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1566 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1567 dt_write_unlock(env, obj);
1568 dt_trans_stop(env, dev, th);
1569 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1570 ldata_new.ld_leh->leh_len);
1574 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1575 ldata_new.ld_leh->leh_len);
1576 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK, 0, th);
1578 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1581 dt_write_unlock(env, obj);
1584 dt_trans_stop(env, dev, th);
1587 lfsck_ibits_unlock(&lh, LCK_EX);
1590 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1591 "for the object: "DFID", parent "DFID", name %.*s\n",
1592 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1593 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1597 struct lfsck_namespace *ns = com->lc_file_ram;
1599 ns->ln_flags |= LF_INCONSISTENT;
1606 * Conditionally remove the specified entry from the linkEA.
1608 * Take the parent lock firstly, then check whether the specified
1609 * name entry exists or not: if yes, do nothing; otherwise, call
1610 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1612 * \param[in] env pointer to the thread context
1613 * \param[in] com pointer to the lfsck component
1614 * \param[in] parent pointer to the parent directory
1615 * \param[in] child pointer to the child object that holds the linkEA
1616 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1617 * \param[in] cname the name for the child in the parent directory
1618 * \param[in] pfid the parent directory's FID for the linkEA
1620 * \retval positive number for repaired cases
1621 * \retval 0 if nothing to be repaired
1622 * \retval negative error number on failure
1624 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1625 struct lfsck_component *com,
1626 struct dt_object *parent,
1627 struct dt_object *child,
1628 struct linkea_data *ldata,
1629 struct lu_name *cname,
1630 struct lu_fid *pfid)
1632 struct lfsck_thread_info *info = lfsck_env_info(env);
1633 struct lu_fid *cfid = &info->lti_fid3;
1634 struct lfsck_lock_handle *llh = &info->lti_llh;
1638 rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1639 MDS_INODELOCK_UPDATE, LCK_PR);
1643 dt_read_lock(env, parent, 0);
1644 if (unlikely(lfsck_is_dead_obj(parent))) {
1645 dt_read_unlock(env, parent);
1647 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1653 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1654 (const struct dt_key *)cname->ln_name);
1655 dt_read_unlock(env, parent);
1657 /* It is safe to release the ldlm lock, because when the logic come
1658 * here, we have got all the needed information above whether the
1659 * linkEA entry is valid or not. It is not important that others
1660 * may add new linkEA entry after the ldlm lock released. If other
1661 * has removed the specified linkEA entry by race, then it is OK,
1662 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1665 if (rc == -ENOENT) {
1666 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1675 /* The LFSCK just found some internal status of cross-MDTs
1676 * create operation. That is normal. */
1677 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1678 linkea_next_entry(ldata);
1683 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1690 * Conditionally replace name entry in the parent.
1692 * As required, the LFSCK may re-create the lost MDT-object for dangling
1693 * name entry, but such repairing may be wrong because of bad FID in the
1694 * name entry. As the LFSCK processing, the real MDT-object may be found,
1695 * then the LFSCK should check whether the former re-created MDT-object
1696 * has been modified or not, if not, then destroy it and update the name
1697 * entry in the parent to reference the real MDT-object.
1699 * \param[in] env pointer to the thread context
1700 * \param[in] com pointer to the lfsck component
1701 * \param[in] parent pointer to the parent directory
1702 * \param[in] child pointer to the MDT-object that may be the real
1703 * MDT-object corresponding to the name entry in parent
1704 * \param[in] cfid the current FID in the name entry
1705 * \param[in] cname contains the name of the child in the parent directory
1707 * \retval positive number for repaired cases
1708 * \retval 0 if nothing to be repaired
1709 * \retval negative error number on failure
1711 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1712 struct lfsck_component *com,
1713 struct dt_object *parent,
1714 struct dt_object *child,
1715 const struct lu_fid *cfid,
1716 const struct lu_name *cname)
1718 struct lfsck_thread_info *info = lfsck_env_info(env);
1719 struct lu_attr *la = &info->lti_la;
1720 struct dt_insert_rec *rec = &info->lti_dt_rec;
1722 struct lfsck_instance *lfsck = com->lc_lfsck;
1723 /* The child and its name may be on different MDTs. */
1724 struct dt_device *dev = lfsck->li_next;
1725 const char *name = cname->ln_name;
1726 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1727 struct dt_object *cobj = NULL;
1728 struct lfsck_lock_handle *pllh = &info->lti_llh;
1729 struct lustre_handle clh = { 0 };
1730 struct linkea_data ldata = { NULL };
1731 struct thandle *th = NULL;
1736 /* @parent/@child may be based on lfsck->li_bottom,
1737 * but here we need the object based on the lfsck->li_next. */
1739 parent = lfsck_object_locate(dev, parent);
1741 GOTO(log, rc = PTR_ERR(parent));
1743 if (unlikely(!dt_try_as_dir(env, parent)))
1744 GOTO(log, rc = -ENOTDIR);
1746 rc = lfsck_lock(env, lfsck, parent, name, pllh,
1747 MDS_INODELOCK_UPDATE, LCK_PW);
1751 if (!fid_is_sane(cfid)) {
1756 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1759 if (rc == -ENOENT) {
1767 if (!dt_object_exists(cobj)) {
1772 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1773 (const struct dt_key *)name);
1774 if (rc == -ENOENT) {
1782 /* Someone changed the name entry, cannot replace it. */
1783 if (!lu_fid_eq(cfid, &tfid))
1786 /* lock the object to be destroyed. */
1787 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1788 MDS_INODELOCK_UPDATE |
1789 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1794 if (unlikely(lfsck_is_dead_obj(cobj))) {
1799 rc = dt_attr_get(env, cobj, la);
1803 /* The object has been modified by other(s), or it is not created by
1804 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1805 if (la->la_ctime != 0)
1808 if (S_ISREG(la->la_mode)) {
1809 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
1810 /* If someone has created related OST-object(s),
1812 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1813 GOTO(log, rc = (rc > 0 ? 0 : rc));
1817 dt_read_lock(env, child, 0);
1818 rc = lfsck_links_read2(env, child, &ldata);
1819 dt_read_unlock(env, child);
1821 /* Someone changed the child, no need to replace. */
1828 rc = linkea_links_find(&ldata, cname, pfid);
1829 /* Someone moved the child, no need to replace. */
1833 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1836 th = dt_trans_create(env, dev);
1838 GOTO(log, rc = PTR_ERR(th));
1841 rc = dt_declare_destroy(env, cobj, th);
1846 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1850 rec->rec_type = S_IFDIR;
1851 rec->rec_fid = lfsck_dto2fid(child);
1852 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1853 (const struct dt_key *)name, th);
1857 rc = dt_trans_start_local(env, dev, th);
1862 rc = dt_destroy(env, cobj, th);
1867 /* The old name entry maybe not exist. */
1868 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
1869 if (rc != 0 && rc != -ENOENT)
1872 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1873 (const struct dt_key *)name, th, 1);
1875 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1878 dt_trans_stop(env, dev, th);
1881 lfsck_ibits_unlock(&clh, LCK_EX);
1884 if (cobj != NULL && !IS_ERR(cobj))
1885 lfsck_object_put(env, cobj);
1887 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1888 "object "DFID" because of conflict with the object "DFID
1889 " under the parent "DFID" with name %s: rc = %d\n",
1890 lfsck_lfsck2name(lfsck), PFID(cfid),
1891 PFID(lfsck_dto2fid(child)), PFID(pfid), name, rc);
1897 * Overwrite the linkEA for the object with the given ldata.
1899 * The caller should take the ldlm lock before the calling.
1901 * \param[in] env pointer to the thread context
1902 * \param[in] com pointer to the lfsck component
1903 * \param[in] obj pointer to the dt_object to be handled
1904 * \param[in] ldata pointer to the new linkEA data
1906 * \retval positive number for repaired cases
1907 * \retval 0 if nothing to be repaired
1908 * \retval negative error number on failure
1910 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1911 struct lfsck_component *com,
1912 struct dt_object *obj,
1913 struct linkea_data *ldata)
1915 struct lfsck_instance *lfsck = com->lc_lfsck;
1916 struct dt_device *dev = lfsck_obj2dev(obj);
1917 struct thandle *th = NULL;
1918 struct lu_buf linkea_buf;
1922 th = dt_trans_create(env, dev);
1924 GOTO(log, rc = PTR_ERR(th));
1926 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1927 ldata->ld_leh->leh_len);
1928 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1929 XATTR_NAME_LINK, 0, th);
1933 rc = dt_trans_start_local(env, dev, th);
1937 dt_write_lock(env, obj, 0);
1938 if (unlikely(lfsck_is_dead_obj(obj)))
1939 GOTO(unlock, rc = 0);
1941 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1942 GOTO(unlock, rc = 1);
1944 rc = dt_xattr_set(env, obj, &linkea_buf,
1945 XATTR_NAME_LINK, 0, th);
1947 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1950 dt_write_unlock(env, obj);
1953 dt_trans_stop(env, dev, th);
1956 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1957 "object "DFID": rc = %d\n",
1958 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1961 struct lfsck_namespace *ns = com->lc_file_ram;
1963 ns->ln_flags |= LF_INCONSISTENT;
1970 * Repair invalid name entry.
1972 * If the name entry contains invalid information, such as bad file type
1973 * or (and) corrupted object FID, then either remove the name entry or
1974 * udpate the name entry with the given (right) information.
1976 * \param[in] env pointer to the thread context
1977 * \param[in] com pointer to the lfsck component
1978 * \param[in] parent pointer to the parent directory
1979 * \param[in] child pointer to the object referenced by the name entry
1980 * \param[in] name the old name of the child under the parent directory
1981 * \param[in] name2 the new name of the child under the parent directory
1982 * \param[in] type the type claimed by the name entry
1983 * \param[in] update update the name entry if true; otherwise, remove it
1984 * \param[in] dec decrease the parent nlink count if true
1986 * \retval positive number for repaired successfully
1987 * \retval 0 if nothing to be repaired
1988 * \retval negative error number on failure
1990 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1991 struct lfsck_component *com,
1992 struct dt_object *parent,
1993 struct dt_object *child,
1994 const char *name, const char *name2,
1995 __u16 type, bool update, bool dec)
1997 struct lfsck_thread_info *info = lfsck_env_info(env);
1998 struct dt_insert_rec *rec = &info->lti_dt_rec;
1999 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2000 const struct lu_fid *cfid = lfsck_dto2fid(child);
2002 struct lfsck_instance *lfsck = com->lc_lfsck;
2003 struct dt_device *dev = lfsck->li_next;
2004 struct thandle *th = NULL;
2005 struct lfsck_lock_handle *llh = &info->lti_llh;
2006 struct lustre_handle lh = { 0 };
2010 parent = lfsck_object_locate(dev, parent);
2012 GOTO(log, rc = PTR_ERR(parent));
2014 if (unlikely(!dt_try_as_dir(env, parent)))
2015 GOTO(log, rc = -ENOTDIR);
2017 if (!update || strcmp(name, name2) == 0)
2018 rc = lfsck_lock(env, lfsck, parent, name, llh,
2019 MDS_INODELOCK_UPDATE, LCK_PW);
2021 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2022 MDS_INODELOCK_UPDATE, LCK_PW);
2026 th = dt_trans_create(env, dev);
2028 GOTO(unlock1, rc = PTR_ERR(th));
2030 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2035 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2036 rec->rec_fid = cfid;
2037 rc = dt_declare_insert(env, parent,
2038 (const struct dt_rec *)rec,
2039 (const struct dt_key *)name2, th);
2045 rc = dt_declare_ref_del(env, parent, th);
2050 rc = dt_trans_start_local(env, dev, th);
2055 dt_write_lock(env, parent, 0);
2056 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2057 (const struct dt_key *)name);
2058 /* Someone has removed the bad name entry by race. */
2060 GOTO(unlock2, rc = 0);
2065 /* Someone has removed the bad name entry and reused it for other
2066 * object by race. */
2067 if (!lu_fid_eq(&tfid, cfid))
2068 GOTO(unlock2, rc = 0);
2070 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2071 GOTO(unlock2, rc = 1);
2073 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2078 rc = dt_insert(env, parent,
2079 (const struct dt_rec *)rec,
2080 (const struct dt_key *)name2, th, 1);
2086 rc = dt_ref_del(env, parent, th);
2091 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2094 dt_write_unlock(env, parent);
2097 dt_trans_stop(env, dev, th);
2099 /* We are not sure whether the child will become orphan or not.
2100 * Record it in the LFSCK trace file for further checking in
2101 * the second-stage scanning. */
2102 if (!update && !dec && rc == 0)
2103 lfsck_namespace_trace_update(env, com, cfid,
2104 LNTF_CHECK_LINKEA, true);
2107 /* It is harmless even if unlock the unused lock_handle */
2108 lfsck_ibits_unlock(&lh, LCK_PW);
2112 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2113 "entry for: parent "DFID", child "DFID", name %s, type "
2114 "in name entry %o, type claimed by child %o. repair it "
2115 "by %s with new name2 %s: rc = %d\n",
2116 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2117 name, type, update ? lfsck_object_type(child) : 0,
2118 update ? "updating" : "removing", name2, rc);
2121 struct lfsck_namespace *ns = com->lc_file_ram;
2123 ns->ln_flags |= LF_INCONSISTENT;
2130 * Update the ".." name entry for the given object.
2132 * The object's ".." is corrupted, this function will update the ".." name
2133 * entry with the given pfid, and the linkEA with the given ldata.
2135 * The caller should take the ldlm lock before the calling.
2137 * \param[in] env pointer to the thread context
2138 * \param[in] com pointer to the lfsck component
2139 * \param[in] obj pointer to the dt_object to be handled
2140 * \param[in] pfid the new fid for the object's ".." name entry
2141 * \param[in] cname the name for the @obj in the parent directory
2143 * \retval positive number for repaired cases
2144 * \retval 0 if nothing to be repaired
2145 * \retval negative error number on failure
2147 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2148 struct lfsck_component *com,
2149 struct dt_object *obj,
2150 const struct lu_fid *pfid,
2151 struct lu_name *cname)
2153 struct lfsck_thread_info *info = lfsck_env_info(env);
2154 struct dt_insert_rec *rec = &info->lti_dt_rec;
2155 struct lfsck_instance *lfsck = com->lc_lfsck;
2156 struct dt_device *dev = lfsck_obj2dev(obj);
2157 struct thandle *th = NULL;
2158 struct linkea_data ldata = { NULL };
2159 struct lu_buf linkea_buf;
2163 LASSERT(!dt_object_remote(obj));
2164 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2166 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2170 rc = linkea_add_buf(&ldata, cname, pfid);
2174 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2175 ldata.ld_leh->leh_len);
2177 th = dt_trans_create(env, dev);
2179 GOTO(log, rc = PTR_ERR(th));
2181 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2185 rec->rec_type = S_IFDIR;
2186 rec->rec_fid = pfid;
2187 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2188 (const struct dt_key *)dotdot, th);
2192 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2193 XATTR_NAME_LINK, 0, th);
2197 rc = dt_trans_start_local(env, dev, th);
2201 dt_write_lock(env, obj, 0);
2202 if (unlikely(lfsck_is_dead_obj(obj)))
2203 GOTO(unlock, rc = 0);
2205 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2206 GOTO(unlock, rc = 1);
2208 /* The old ".." name entry maybe not exist. */
2209 dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2211 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2212 (const struct dt_key *)dotdot, th, 1);
2216 rc = dt_xattr_set(env, obj, &linkea_buf,
2217 XATTR_NAME_LINK, 0, th);
2219 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2222 dt_write_unlock(env, obj);
2225 dt_trans_stop(env, dev, th);
2228 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2229 "the object "DFID", new parent "DFID": rc = %d\n",
2230 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2234 struct lfsck_namespace *ns = com->lc_file_ram;
2236 ns->ln_flags |= LF_INCONSISTENT;
2243 * Handle orphan @obj during Double Scan Directory.
2245 * Remove the @obj's current (invalid) linkEA entries, and insert
2246 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2247 * ${FID}-${PFID}-D-${conflict_version}
2249 * The caller should take the ldlm lock before the calling.
2251 * \param[in] env pointer to the thread context
2252 * \param[in] com pointer to the lfsck component
2253 * \param[in] obj pointer to the orphan object to be handled
2254 * \param[in] pfid the new fid for the object's ".." name entry
2255 * \param[in,out] lh ldlm lock handler for the given @obj
2256 * \param[out] type to tell the caller what the inconsistency is
2258 * \retval positive number for repaired cases
2259 * \retval 0 if nothing to be repaired
2260 * \retval negative error number on failure
2263 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2264 struct lfsck_component *com,
2265 struct dt_object *obj,
2266 const struct lu_fid *pfid,
2267 struct lustre_handle *lh,
2268 enum lfsck_namespace_inconsistency_type *type)
2270 struct lfsck_thread_info *info = lfsck_env_info(env);
2271 struct lfsck_namespace *ns = com->lc_file_ram;
2275 /* Remove the unrecognized linkEA. */
2276 rc = lfsck_namespace_links_remove(env, com, obj);
2277 lfsck_ibits_unlock(lh, LCK_EX);
2278 if (rc < 0 && rc != -ENODATA)
2281 *type = LNIT_MUL_REF;
2283 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2284 * ever tried to verify some remote MDT-object that resides on this
2285 * MDT, but this MDT failed to respond such request. So means there
2286 * may be some remote name entry on other MDT that references this
2287 * object with another name, so we cannot know whether this linkEA
2288 * is valid or not. So keep it there and maybe resolved when next
2290 if (ns->ln_flags & LF_INCOMPLETE)
2293 /* The unique linkEA is invalid, even if the ".." name entry may be
2294 * valid, we still cannot know via which name entry this directory
2295 * will be referenced. Then handle it as pure orphan. */
2296 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2297 "-"DFID, PFID(pfid));
2298 rc = lfsck_namespace_insert_orphan(env, com, obj,
2299 info->lti_tmpbuf, "D", NULL);
2305 * Double Scan Directory object for single linkEA entry case.
2307 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2308 * then check whether the name is in the namespace or not, if not, add the
2309 * missing name entry back to namespace. If the linkEA entry is invalid,
2310 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2313 * \param[in] env pointer to the thread context
2314 * \param[in] com pointer to the lfsck component
2315 * \param[in] child pointer to the directory to be double scanned
2316 * \param[in] pfid the FID corresponding to the ".." entry
2317 * \param[in] ldata pointer to the linkEA data for the given @child
2318 * \param[in,out] lh ldlm lock handler for the given @child
2319 * \param[out] type to tell the caller what the inconsistency is
2320 * \param[in] retry if found inconsistency, but the caller does not hold
2321 * ldlm lock on the @child, then set @retry as true
2323 * \retval positive number for repaired cases
2324 * \retval 0 if nothing to be repaired
2325 * \retval negative error number on failure
2328 lfsck_namespace_dsd_single(const struct lu_env *env,
2329 struct lfsck_component *com,
2330 struct dt_object *child,
2331 const struct lu_fid *pfid,
2332 struct linkea_data *ldata,
2333 struct lustre_handle *lh,
2334 enum lfsck_namespace_inconsistency_type *type,
2337 struct lfsck_thread_info *info = lfsck_env_info(env);
2338 struct lu_name *cname = &info->lti_name;
2339 const struct lu_fid *cfid = lfsck_dto2fid(child);
2341 struct lfsck_namespace *ns = com->lc_file_ram;
2342 struct lfsck_instance *lfsck = com->lc_lfsck;
2343 struct dt_object *parent = NULL;
2344 struct lmv_mds_md_v1 *lmv;
2348 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid, info->lti_key);
2349 /* The unique linkEA entry with bad parent will be handled as orphan. */
2350 if (!fid_is_sane(&tfid)) {
2351 if (!lustre_handle_is_used(lh) && retry != NULL)
2354 rc = lfsck_namespace_dsd_orphan(env, com, child,
2360 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2362 GOTO(out, rc = PTR_ERR(parent));
2364 /* We trust the unique linkEA entry in spite of whether it matches the
2365 * ".." name entry or not. Because even if the linkEA entry is wrong
2366 * and the ".." name entry is right, we still cannot know via which
2367 * name entry the child will be referenced, since all known entries
2368 * have been verified during the first-stage scanning. */
2369 if (!dt_object_exists(parent)) {
2370 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2371 * has ever tried to verify some remote MDT-object that resides
2372 * on this MDT, but this MDT failed to respond such request. So
2373 * means there may be some remote name entry on other MDT that
2374 * references this object with another name, so we cannot know
2375 * whether this linkEA is valid or not. So keep it there and
2376 * maybe resolved when next LFSCK run. */
2377 if (ns->ln_flags & LF_INCOMPLETE)
2380 if (!lustre_handle_is_used(lh) && retry != NULL) {
2386 lfsck_ibits_unlock(lh, LCK_EX);
2389 lmv = &info->lti_lmv;
2390 rc = lfsck_read_stripe_lmv(env, child, lmv);
2391 if (rc != 0 && rc != -ENODATA)
2394 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2396 } else if (lfsck_shard_name_to_index(env,
2397 cname->ln_name, cname->ln_namelen,
2398 S_IFDIR, cfid) < 0) {
2399 /* It is an invalid name entry, we
2400 * cannot trust the parent also. */
2401 rc = lfsck_namespace_shrink_linkea(env, com, child,
2402 ldata, cname, &tfid, true);
2406 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2407 "-"DFID, PFID(pfid));
2408 rc = lfsck_namespace_insert_orphan(env, com, child,
2409 info->lti_tmpbuf, "S", NULL);
2414 /* Create the lost parent as an orphan. */
2415 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2417 /* Add the missing name entry to the parent. */
2418 rc = lfsck_namespace_insert_normal(env, com, parent,
2419 child, cname->ln_name);
2420 if (unlikely(rc == -EEXIST)) {
2421 /* Unfortunately, someone reused the name
2422 * under the parent by race. So we have
2423 * to remove the linkEA entry from
2424 * current child object. It means that the
2425 * LFSCK cannot recover the system
2426 * totally back to its original status,
2427 * but it is necessary to make the
2428 * current system to be consistent. */
2429 rc = lfsck_namespace_shrink_linkea(env,
2431 cname, &tfid, true);
2433 snprintf(info->lti_tmpbuf,
2434 sizeof(info->lti_tmpbuf),
2435 "-"DFID, PFID(pfid));
2436 rc = lfsck_namespace_insert_orphan(env,
2437 com, child, info->lti_tmpbuf,
2446 /* The unique linkEA entry with bad parent will be handled as orphan. */
2447 if (unlikely(!dt_try_as_dir(env, parent))) {
2448 if (!lustre_handle_is_used(lh) && retry != NULL)
2451 rc = lfsck_namespace_dsd_orphan(env, com, child,
2457 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2458 (const struct dt_key *)cname->ln_name);
2459 if (rc == -ENOENT) {
2460 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2461 * has ever tried to verify some remote MDT-object that resides
2462 * on this MDT, but this MDT failed to respond such request. So
2463 * means there may be some remote name entry on other MDT that
2464 * references this object with another name, so we cannot know
2465 * whether this linkEA is valid or not. So keep it there and
2466 * maybe resolved when next LFSCK run. */
2467 if (ns->ln_flags & LF_INCOMPLETE)
2470 if (!lustre_handle_is_used(lh) && retry != NULL) {
2476 lfsck_ibits_unlock(lh, LCK_EX);
2477 rc = lfsck_namespace_check_name(env, parent, child, cname);
2484 /* It is an invalid name entry, drop it. */
2485 if (unlikely(rc > 0)) {
2486 rc = lfsck_namespace_shrink_linkea(env, com, child,
2487 ldata, cname, &tfid, true);
2489 snprintf(info->lti_tmpbuf,
2490 sizeof(info->lti_tmpbuf),
2491 "-"DFID, PFID(pfid));
2492 rc = lfsck_namespace_insert_orphan(env, com,
2493 child, info->lti_tmpbuf, "D", NULL);
2499 /* Add the missing name entry back to the namespace. */
2500 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2502 if (unlikely(rc == -ESTALE))
2503 /* It may happen when the remote object has been
2504 * removed, but the local MDT is not aware of that. */
2507 if (unlikely(rc == -EEXIST)) {
2508 /* Unfortunately, someone reused the name under the
2509 * parent by race. So we have to remove the linkEA
2510 * entry from current child object. It means that the
2511 * LFSCK cannot recover the system totally back to
2512 * its original status, but it is necessary to make
2513 * the current system to be consistent.
2515 * It also may be because of the LFSCK found some
2516 * internal status of create operation. Under such
2517 * case, nothing to be done. */
2518 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2519 parent, child, ldata, cname, &tfid);
2521 snprintf(info->lti_tmpbuf,
2522 sizeof(info->lti_tmpbuf),
2523 "-"DFID, PFID(pfid));
2524 rc = lfsck_namespace_insert_orphan(env, com,
2525 child, info->lti_tmpbuf, "D", NULL);
2535 if (!lu_fid_eq(&tfid, cfid)) {
2536 if (!lustre_handle_is_used(lh) && retry != NULL) {
2542 lfsck_ibits_unlock(lh, LCK_EX);
2543 /* The name entry references another MDT-object that
2544 * may be created by the LFSCK for repairing dangling
2545 * name entry. Try to replace it. */
2546 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2549 rc = lfsck_namespace_dsd_orphan(env, com, child,
2555 if (fid_is_zero(pfid))
2558 /* The ".." name entry is wrong, update it. */
2559 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2560 if (!lustre_handle_is_used(lh) && retry != NULL) {
2566 *type = LNIT_UNMATCHED_PAIRS;
2567 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2568 lfsck_dto2fid(parent), cname);
2574 if (parent != NULL && !IS_ERR(parent))
2575 lfsck_object_put(env, parent);
2581 * Double Scan Directory object for multiple linkEA entries case.
2583 * The given @child has multiple linkEA entries. There is at most one linkEA
2584 * entry will be valid, all the others will be removed. Firstly, the function
2585 * will try to find out the linkEA entry for which the name entry exists under
2586 * the given parent (@pfid). If there is no linkEA entry that matches the given
2587 * ".." name entry, then tries to find out the first linkEA entry that both the
2588 * parent and the name entry exist to rebuild a new ".." name entry.
2590 * \param[in] env pointer to the thread context
2591 * \param[in] com pointer to the lfsck component
2592 * \param[in] child pointer to the directory to be double scanned
2593 * \param[in] pfid the FID corresponding to the ".." entry
2594 * \param[in] ldata pointer to the linkEA data for the given @child
2595 * \param[in,out] lh ldlm lock handler for the given @child
2596 * \param[out] type to tell the caller what the inconsistency is
2597 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2599 * \retval positive number for repaired cases
2600 * \retval 0 if nothing to be repaired
2601 * \retval negative error number on failure
2604 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2605 struct lfsck_component *com,
2606 struct dt_object *child,
2607 const struct lu_fid *pfid,
2608 struct linkea_data *ldata,
2609 struct lustre_handle *lh,
2610 enum lfsck_namespace_inconsistency_type *type,
2613 struct lfsck_thread_info *info = lfsck_env_info(env);
2614 struct lu_name *cname = &info->lti_name;
2615 const struct lu_fid *cfid = lfsck_dto2fid(child);
2616 struct lu_fid *pfid2 = &info->lti_fid3;
2618 struct lfsck_namespace *ns = com->lc_file_ram;
2619 struct lfsck_instance *lfsck = com->lc_lfsck;
2620 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2621 struct dt_object *parent = NULL;
2622 struct linkea_data ldata_new = { NULL };
2623 int dirent_count = 0;
2624 int linkea_count = 0;
2630 while (ldata->ld_lee != NULL) {
2631 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2633 /* Drop repeated linkEA entries. */
2634 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2635 /* Drop invalid linkEA entry. */
2636 if (!fid_is_sane(&tfid)) {
2637 linkea_del_buf(ldata, cname);
2642 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2643 * then it is possible that: the directry object has ever
2644 * been lost, but its name entry was there. In the former
2645 * LFSCK run, during the first-stage scanning, the LFSCK
2646 * found the dangling name entry, but it did not recreate
2647 * the lost object, and when moved to the second-stage
2648 * scanning, some children objects of the lost directory
2649 * object were found, then the LFSCK recreated such lost
2650 * directory object as an orphan.
2652 * When the LFSCK runs again, if the dangling name is still
2653 * there, the LFSCK should move the orphan directory object
2654 * back to the normal namespace. */
2655 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2656 linkea_next_entry(ldata);
2660 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2662 RETURN(PTR_ERR(parent));
2664 if (!dt_object_exists(parent)) {
2665 lfsck_object_put(env, parent);
2666 if (ldata->ld_leh->leh_reccount > 1) {
2667 /* If it is NOT the last linkEA entry, then
2668 * there is still other chance to make the
2669 * child to be visible via other parent, then
2670 * remove this linkEA entry. */
2671 linkea_del_buf(ldata, cname);
2679 /* The linkEA entry with bad parent will be removed. */
2680 if (unlikely(!dt_try_as_dir(env, parent))) {
2681 lfsck_object_put(env, parent);
2682 linkea_del_buf(ldata, cname);
2687 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2688 (const struct dt_key *)cname->ln_name);
2689 *pfid2 = *lfsck_dto2fid(parent);
2690 if (rc == -ENOENT) {
2691 lfsck_object_put(env, parent);
2692 linkea_next_entry(ldata);
2697 lfsck_object_put(env, parent);
2702 if (lu_fid_eq(&tfid, cfid)) {
2703 lfsck_object_put(env, parent);
2704 if (!lu_fid_eq(pfid, pfid2)) {
2705 *type = LNIT_UNMATCHED_PAIRS;
2706 rc = lfsck_namespace_repair_unmatched_pairs(env,
2707 com, child, pfid2, cname);
2713 /* It is the most common case that we find the
2714 * name entry corresponding to the linkEA entry
2715 * that matches the ".." name entry. */
2716 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2720 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2724 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2729 linkea_del_buf(ldata, cname);
2731 linkea_first_entry(ldata);
2732 /* There may be some invalid dangling name entries under
2733 * other parent directories, remove all of them. */
2734 while (ldata->ld_lee != NULL) {
2735 lfsck_namespace_unpack_linkea_entry(ldata,
2736 cname, &tfid, info->lti_key);
2737 if (!fid_is_sane(&tfid))
2740 parent = lfsck_object_find_bottom(env, lfsck,
2742 if (IS_ERR(parent)) {
2743 rc = PTR_ERR(parent);
2744 if (rc != -ENOENT &&
2745 bk->lb_param & LPF_FAILOUT)
2751 if (!dt_object_exists(parent)) {
2752 lfsck_object_put(env, parent);
2756 rc = lfsck_namespace_repair_dirent(env, com,
2757 parent, child, cname->ln_name,
2758 cname->ln_name, S_IFDIR, false, true);
2759 lfsck_object_put(env, parent);
2761 if (bk->lb_param & LPF_FAILOUT)
2770 linkea_del_buf(ldata, cname);
2773 ns->ln_dirent_repaired += dirent_count;
2778 lfsck_ibits_unlock(lh, LCK_EX);
2779 /* The name entry references another MDT-object that may be
2780 * created by the LFSCK for repairing dangling name entry.
2781 * Try to replace it. */
2782 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2784 lfsck_object_put(env, parent);
2791 linkea_del_buf(ldata, cname);
2794 linkea_first_entry(ldata);
2795 if (ldata->ld_leh->leh_reccount == 1) {
2796 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2799 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2800 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2806 /* All linkEA entries are invalid and removed, then handle the @child
2808 if (ldata->ld_leh->leh_reccount == 0) {
2809 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2815 /* If the dangling name entry for the orphan directory object has
2816 * been remvoed, then just check whether the directory object is
2817 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2823 /* There is no linkEA entry that matches the ".." name entry. Find
2824 * the first linkEA entry that both parent and name entry exist to
2825 * rebuild a new ".." name entry. */
2835 * Repair the object's nlink attribute.
2837 * If all the known name entries have been verified, then the object's hard
2838 * link attribute should match the object's linkEA entries count unless the
2839 * object's has too much hard link to be recorded in the linkEA. Such cases
2840 * should have been marked in the LFSCK trace file. Otherwise, trust the
2841 * linkEA to update the object's nlink attribute.
2843 * \param[in] env pointer to the thread context
2844 * \param[in] com pointer to the lfsck component
2845 * \param[in] obj pointer to the dt_object to be handled
2846 * \param[in,out] la pointer to buffer to object's attribute before
2847 * and after the repairing
2849 * \retval positive number for repaired cases
2850 * \retval 0 if nothing to be repaired
2851 * \retval negative error number on failure
2853 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2854 struct lfsck_component *com,
2855 struct dt_object *obj,
2858 struct lfsck_thread_info *info = lfsck_env_info(env);
2859 struct lu_fid *tfid = &info->lti_fid3;
2860 struct lfsck_namespace *ns = com->lc_file_ram;
2861 struct lfsck_instance *lfsck = com->lc_lfsck;
2862 struct dt_device *dev = lfsck_obj2dev(obj);
2863 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2864 struct thandle *th = NULL;
2865 struct linkea_data ldata = { NULL };
2866 struct lustre_handle lh = { 0 };
2867 __u32 old = la->la_nlink;
2873 LASSERT(!dt_object_remote(obj));
2874 LASSERT(S_ISREG(lfsck_object_type(obj)));
2876 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2877 MDS_INODELOCK_UPDATE, LCK_PW);
2881 th = dt_trans_create(env, dev);
2883 GOTO(log, rc = PTR_ERR(th));
2885 la->la_valid = LA_NLINK;
2886 rc = dt_declare_attr_set(env, obj, la, th);
2890 rc = dt_trans_start_local(env, dev, th);
2894 dt_write_lock(env, obj, 0);
2895 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2896 * ever tried to verify some remote MDT-object that resides on this
2897 * MDT, but this MDT failed to respond such request. So means there
2898 * may be some remote name entry on other MDT that references this
2899 * object with another name, so we cannot know whether this linkEA
2900 * is valid or not. So keep it there and maybe resolved when next
2902 if (ns->ln_flags & LF_INCOMPLETE)
2903 GOTO(unlock, rc = 0);
2905 fid_cpu_to_be(tfid, cfid);
2906 idx = lfsck_sub_trace_file_fid2idx(cfid);
2907 rc = dt_lookup(env, com->lc_sub_trace_objs[idx].lsto_obj,
2908 (struct dt_rec *)&flags, (const struct dt_key *)tfid);
2912 if (flags & LNTF_SKIP_NLINK)
2913 GOTO(unlock, rc = 0);
2915 rc = dt_attr_get(env, obj, la);
2917 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2919 rc = lfsck_links_read2(env, obj, &ldata);
2921 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2923 if (la->la_nlink == ldata.ld_leh->leh_reccount ||
2924 unlikely(la->la_nlink == 0))
2925 GOTO(unlock, rc = 0);
2927 la->la_nlink = ldata.ld_leh->leh_reccount;
2928 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2929 GOTO(unlock, rc = 1);
2931 rc = dt_attr_set(env, obj, la, th);
2933 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2936 dt_write_unlock(env, obj);
2939 dt_trans_stop(env, dev, th);
2942 lfsck_ibits_unlock(&lh, LCK_PW);
2943 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2944 "nlink count from %u to %u: rc = %d\n",
2945 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2948 ns->ln_flags |= LF_INCONSISTENT;
2954 * Double scan the directory object for namespace LFSCK.
2956 * This function will verify the <parent, child> pairs in the namespace tree:
2957 * the parent references the child via some name entry that should be in the
2958 * child's linkEA entry, the child should back references the parent via its
2961 * The LFSCK will scan every linkEA entry in turn until find out the first
2962 * matched pairs. If found, then all other linkEA entries will be dropped.
2963 * If all the linkEA entries cannot match the ".." name entry, then there
2964 * are serveral possible cases:
2966 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2967 * in the linkEA entry is valid.
2969 * 2) If there are multiple linkEA entries, then try to find the linkEA
2970 * that matches the ".." name entry. If found, then all other entries
2971 * are invalid; otherwise, it is quite possible that the ".." name entry
2972 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2973 * entry according to the first valid linkEA entry (both the parent and
2974 * the name entry should exist).
2976 * 3) If the directory object has no (valid) linkEA entry, then the
2977 * directory object will be handled as pure orphan and inserted
2978 * in the .lustre/lost+found/MDTxxxx/ with the name:
2979 * ${self_FID}-${PFID}-D-${conflict_version}
2981 * \param[in] env pointer to the thread context
2982 * \param[in] com pointer to the lfsck component
2983 * \param[in] child pointer to the directory object to be handled
2984 * \param[in] flags to indicate the specical checking on the @child
2986 * \retval positive number for repaired cases
2987 * \retval 0 if nothing to be repaired
2988 * \retval negative error number on failure
2990 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
2991 struct lfsck_component *com,
2992 struct dt_object *child, __u8 flags)
2994 struct lfsck_thread_info *info = lfsck_env_info(env);
2995 const struct lu_fid *cfid = lfsck_dto2fid(child);
2996 struct lu_fid *pfid = &info->lti_fid2;
2997 struct lfsck_namespace *ns = com->lc_file_ram;
2998 struct lfsck_instance *lfsck = com->lc_lfsck;
2999 struct lustre_handle lh = { 0 };
3000 struct linkea_data ldata = { NULL };
3001 bool unknown = false;
3004 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3008 LASSERT(!dt_object_remote(child));
3010 if (flags & LNTF_UNCERTAIN_LMV) {
3011 if (flags & LNTF_RECHECK_NAME_HASH) {
3012 rc = lfsck_namespace_scan_shard(env, com, child);
3016 ns->ln_striped_shards_scanned++;
3018 ns->ln_striped_shards_skipped++;
3022 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3026 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3027 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3028 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3029 "the namespace LFSCK, then the LFSCK cannot guarantee"
3030 "all the name entries have been verified in first-stage"
3031 "scanning. So have to skip orphan related handling for"
3032 "the directory object "DFID" with remote name entry\n",
3033 lfsck_lfsck2name(lfsck), PFID(cfid));
3038 if (unlikely(!dt_try_as_dir(env, child)))
3039 GOTO(out, rc = -ENOTDIR);
3041 /* We only take ldlm lock on the @child when required. When the
3042 * logic comes here for the first time, it is always false. */
3046 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3047 MDS_INODELOCK_UPDATE |
3048 MDS_INODELOCK_XATTR, LCK_EX);
3053 dt_read_lock(env, child, 0);
3054 if (unlikely(lfsck_is_dead_obj(child))) {
3055 dt_read_unlock(env, child);
3060 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3061 (const struct dt_key *)dotdot);
3063 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3064 dt_read_unlock(env, child);
3069 if (!lustre_handle_is_used(&lh)) {
3070 dt_read_unlock(env, child);
3075 } else if (lfsck->li_lpf_obj != NULL &&
3076 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3078 } else if (unlikely(!fid_is_sane(pfid))) {
3082 rc = lfsck_links_read(env, child, &ldata);
3083 dt_read_unlock(env, child);
3085 if (rc != -ENODATA && rc != -EINVAL)
3088 if (!lustre_handle_is_used(&lh))
3091 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3092 /* Remove the corrupted linkEA. */
3093 rc = lfsck_namespace_links_remove(env, com, child);
3095 /* Here, because of the crashed linkEA, we
3096 * cannot know whether there is some parent
3097 * that references the child directory via
3098 * some name entry or not. So keep it there,
3099 * when the LFSCK run next time, if there is
3100 * some parent that references this object,
3101 * then the LFSCK can rebuild the linkEA;
3102 * otherwise, this object will be handled
3103 * as orphan as above. */
3106 /* 1. If we have neither ".." nor linkEA,
3107 * then it is an orphan.
3109 * 2. If we only have the ".." name entry,
3110 * but no parent references this child
3111 * directory, then handle it as orphan. */
3112 lfsck_ibits_unlock(&lh, LCK_EX);
3113 type = LNIT_MUL_REF;
3115 /* If the LFSCK is marked as LF_INCOMPLETE,
3116 * then means some MDT has ever tried to
3117 * verify some remote MDT-object that resides
3118 * on this MDT, but this MDT failed to respond
3119 * such request. So means there may be some
3120 * remote name entry on other MDT that
3121 * references this object with another name,
3122 * so we cannot know whether this linkEA is
3123 * valid or not. So keep it there and maybe
3124 * resolved when next LFSCK run. */
3125 if (ns->ln_flags & LF_INCOMPLETE)
3128 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3129 "-"DFID, PFID(pfid));
3130 rc = lfsck_namespace_insert_orphan(env, com, child,
3131 info->lti_tmpbuf, "D", NULL);
3137 linkea_first_entry(&ldata);
3138 /* This is the most common case: the object has unique linkEA entry. */
3139 if (ldata.ld_leh->leh_reccount == 1) {
3140 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3141 &lh, &type, &retry);
3143 LASSERT(!lustre_handle_is_used(&lh));
3152 if (!lustre_handle_is_used(&lh))
3155 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3156 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3162 /* When we come here, the cases usually like that:
3163 * 1) The directory object has a corrupted linkEA entry. During the
3164 * first-stage scanning, the LFSCK cannot know such corruption,
3165 * then it appends the right linkEA entry according to the found
3166 * name entry after the bad one.
3168 * 2) The directory object has a right linkEA entry. During the
3169 * first-stage scanning, the LFSCK finds some bad name entry,
3170 * but the LFSCK cannot aware that at that time, then it adds
3171 * the bad linkEA entry for further processing. */
3172 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3178 lfsck_ibits_unlock(&lh, LCK_EX);
3181 case LNIT_BAD_LINKEA:
3182 ns->ln_linkea_repaired++;
3184 case LNIT_UNMATCHED_PAIRS:
3185 ns->ln_unmatched_pairs_repaired++;
3188 ns->ln_mul_ref_repaired++;
3196 ns->ln_unknown_inconsistency++;
3202 * Double scan the MDT-object for namespace LFSCK.
3204 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3205 * those entries from the linkEA; if the linkEA becomes empty or the object
3206 * has no linkEA, then it is an orphan and will be added into the directory
3207 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3208 * the remote parent; if the name entry corresponding to some linkEA entry
3209 * is lost, then add the name entry back to the namespace.
3211 * \param[in] env pointer to the thread context
3212 * \param[in] com pointer to the lfsck component
3213 * \param[in] child pointer to the dt_object to be handled
3214 * \param[in] flags some hints to indicate how the @child should be handled
3216 * \retval positive number for repaired cases
3217 * \retval 0 if nothing to be repaired
3218 * \retval negative error number on failure
3220 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3221 struct lfsck_component *com,
3222 struct dt_object *child, __u8 flags)
3224 struct lfsck_thread_info *info = lfsck_env_info(env);
3225 struct lu_attr *la = &info->lti_la;
3226 struct lu_name *cname = &info->lti_name;
3227 struct lu_fid *pfid = &info->lti_fid;
3228 struct lu_fid *cfid = &info->lti_fid2;
3229 struct lfsck_instance *lfsck = com->lc_lfsck;
3230 struct lfsck_namespace *ns = com->lc_file_ram;
3231 struct dt_object *parent = NULL;
3232 struct linkea_data ldata = { NULL };
3233 bool repaired = false;
3238 dt_read_lock(env, child, 0);
3239 if (unlikely(lfsck_is_dead_obj(child))) {
3240 dt_read_unlock(env, child);
3245 if (S_ISDIR(lfsck_object_type(child))) {
3246 dt_read_unlock(env, child);
3247 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3252 rc = lfsck_links_read(env, child, &ldata);
3253 dt_read_unlock(env, child);
3257 linkea_first_entry(&ldata);
3258 while (ldata.ld_lee != NULL) {
3259 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3261 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3263 /* Found repeated linkEA entries */
3265 rc = lfsck_namespace_shrink_linkea(env, com, child,
3266 &ldata, cname, pfid, false);
3278 /* Invalid PFID in the linkEA entry. */
3279 if (!fid_is_sane(pfid)) {
3280 rc = lfsck_namespace_shrink_linkea(env, com, child,
3281 &ldata, cname, pfid, true);
3291 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3293 GOTO(out, rc = PTR_ERR(parent));
3295 if (!dt_object_exists(parent)) {
3298 if (ldata.ld_leh->leh_reccount > 1) {
3299 /* If it is NOT the last linkEA entry, then
3300 * there is still other chance to make the
3301 * child to be visible via other parent, then
3302 * remove this linkEA entry. */
3303 rc = lfsck_namespace_shrink_linkea(env, com,
3304 child, &ldata, cname, pfid, true);
3306 /* If the LFSCK is marked as LF_INCOMPLETE,
3307 * then means some MDT has ever tried to
3308 * verify some remote MDT-object that resides
3309 * on this MDT, but this MDT failed to respond
3310 * such request. So means there may be some
3311 * remote name entry on other MDT that
3312 * references this object with another name,
3313 * so we cannot know whether this linkEA is
3314 * valid or not. So keep it there and maybe
3315 * resolved when next LFSCK run. */
3316 if (ns->ln_flags & LF_INCOMPLETE) {
3317 lfsck_object_put(env, parent);
3322 /* Create the lost parent as an orphan. */
3323 rc = lfsck_namespace_create_orphan_dir(env, com,
3326 lfsck_object_put(env, parent);
3334 /* Add the missing name entry to the parent. */
3335 rc = lfsck_namespace_insert_normal(env, com,
3336 parent, child, cname->ln_name);
3337 if (unlikely(rc == -EEXIST))
3338 /* Unfortunately, someone reused the
3339 * name under the parent by race. So we
3340 * have to remove the linkEA entry from
3341 * current child object. It means that
3342 * the LFSCK cannot recover the system
3343 * totally back to its original status,
3344 * but it is necessary to make the
3345 * current system to be consistent. */
3346 rc = lfsck_namespace_shrink_linkea(env,
3350 linkea_next_entry(&ldata);
3353 lfsck_object_put(env, parent);
3363 /* The linkEA entry with bad parent will be removed. */
3364 if (unlikely(!dt_try_as_dir(env, parent))) {
3365 lfsck_object_put(env, parent);
3366 rc = lfsck_namespace_shrink_linkea(env, com, child,
3367 &ldata, cname, pfid, true);
3377 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3378 (const struct dt_key *)cname->ln_name);
3379 if (rc != 0 && rc != -ENOENT) {
3380 lfsck_object_put(env, parent);
3386 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3387 /* It is the most common case that we
3388 * find the name entry corresponding
3389 * to the linkEA entry. */
3390 lfsck_object_put(env, parent);
3391 linkea_next_entry(&ldata);
3393 /* The name entry references another
3394 * MDT-object that may be created by
3395 * the LFSCK for repairing dangling
3396 * name entry. Try to replace it. */
3397 rc = lfsck_namespace_replace_cond(env, com,
3398 parent, child, cfid, cname);
3399 lfsck_object_put(env, parent);
3405 linkea_next_entry(&ldata);
3407 rc = lfsck_namespace_shrink_linkea(env,
3421 rc = dt_attr_get(env, child, la);
3425 /* If there is no name entry in the parent dir and the object
3426 * link count is less than the linkea entries count, then the
3427 * linkea entry should be removed. */
3428 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3429 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3430 parent, child, &ldata, cname, pfid);
3431 lfsck_object_put(env, parent);
3441 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3442 * MDT has ever tried to verify some remote MDT-object that
3443 * resides on this MDT, but this MDT failed to respond such
3444 * request. So means there may be some remote name entry on
3445 * other MDT that references this object with another name,
3446 * so we cannot know whether this linkEA is valid or not.
3447 * So keep it there and maybe resolved when next LFSCK run. */
3448 if (ns->ln_flags & LF_INCOMPLETE) {
3449 lfsck_object_put(env, parent);
3454 rc = lfsck_namespace_check_name(env, parent, child, cname);
3459 lfsck_object_put(env, parent);
3464 /* It is an invalid name entry, drop it. */
3465 if (unlikely(rc > 0)) {
3466 lfsck_object_put(env, parent);
3467 rc = lfsck_namespace_shrink_linkea(env, com, child,
3468 &ldata, cname, pfid, true);
3478 /* Add the missing name entry back to the namespace. */
3479 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3481 if (unlikely(rc == -ESTALE))
3482 /* It may happen when the remote object has been
3483 * removed, but the local MDT is not aware of that. */
3486 if (unlikely(rc == -EEXIST))
3487 /* Unfortunately, someone reused the name under the
3488 * parent by race. So we have to remove the linkEA
3489 * entry from current child object. It means that the
3490 * LFSCK cannot recover the system totally back to
3491 * its original status, but it is necessary to make
3492 * the current system to be consistent.
3494 * It also may be because of the LFSCK found some
3495 * internal status of create operation. Under such
3496 * case, nothing to be done. */
3497 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3498 parent, child, &ldata, cname, pfid);
3500 linkea_next_entry(&ldata);
3502 lfsck_object_put(env, parent);
3513 if (rc < 0 && rc != -ENODATA)
3517 LASSERT(ldata.ld_leh != NULL);
3519 count = ldata.ld_leh->leh_reccount;
3523 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3524 * MDT has ever tried to verify some remote MDT-object that
3525 * resides on this MDT, but this MDT failed to respond such
3526 * request. So means there may be some remote name entry on
3527 * other MDT that references this object with another name,
3528 * so we cannot know whether this linkEA is valid or not.
3529 * So keep it there and maybe resolved when next LFSCK run. */
3530 if (!(ns->ln_flags & LF_INCOMPLETE)) {
3531 /* If the child becomes orphan, then insert it into
3532 * the global .lustre/lost+found/MDTxxxx directory. */
3533 rc = lfsck_namespace_insert_orphan(env, com, child,
3539 ns->ln_mul_ref_repaired++;
3544 rc = dt_attr_get(env, child, la);
3548 if (la->la_nlink != 0 && la->la_nlink != count) {
3549 rc = lfsck_namespace_repair_nlink(env, com, child, la);
3551 ns->ln_objs_nlink_repaired++;
3558 if (la->la_nlink > 1)
3559 ns->ln_mul_linked_repaired++;
3568 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3569 struct lfsck_namespace *ns,
3570 __u64 checked_phase1,
3571 __u64 checked_phase2,
3575 seq_printf(m, "checked_phase1: "LPU64"\n"
3576 "checked_phase2: "LPU64"\n"
3577 "updated_phase1: "LPU64"\n"
3578 "updated_phase2: "LPU64"\n"
3579 "failed_phase1: "LPU64"\n"
3580 "failed_phase2: "LPU64"\n"
3581 "directories: "LPU64"\n"
3582 "dirent_repaired: "LPU64"\n"
3583 "linkea_repaired: "LPU64"\n"
3584 "nlinks_repaired: "LPU64"\n"
3585 "multiple_linked_checked: "LPU64"\n"
3586 "multiple_linked_repaired: "LPU64"\n"
3587 "unknown_inconsistency: "LPU64"\n"
3588 "unmatched_pairs_repaired: "LPU64"\n"
3589 "dangling_repaired: "LPU64"\n"
3590 "multiple_referenced_repaired: "LPU64"\n"
3591 "bad_file_type_repaired: "LPU64"\n"
3592 "lost_dirent_repaired: "LPU64"\n"
3593 "local_lost_found_scanned: "LPU64"\n"
3594 "local_lost_found_moved: "LPU64"\n"
3595 "local_lost_found_skipped: "LPU64"\n"
3596 "local_lost_found_failed: "LPU64"\n"
3597 "striped_dirs_scanned: "LPU64"\n"
3598 "striped_dirs_repaired: "LPU64"\n"
3599 "striped_dirs_failed: "LPU64"\n"
3600 "striped_dirs_disabled: "LPU64"\n"
3601 "striped_dirs_skipped: "LPU64"\n"
3602 "striped_shards_scanned: "LPU64"\n"
3603 "striped_shards_repaired: "LPU64"\n"
3604 "striped_shards_failed: "LPU64"\n"
3605 "striped_shards_skipped: "LPU64"\n"
3606 "name_hash_repaired: "LPU64"\n"
3607 "success_count: %u\n"
3608 "run_time_phase1: %u seconds\n"
3609 "run_time_phase2: %u seconds\n",
3612 ns->ln_items_repaired,
3613 ns->ln_objs_repaired_phase2,
3614 ns->ln_items_failed,
3615 ns->ln_objs_failed_phase2,
3616 ns->ln_dirs_checked,
3617 ns->ln_dirent_repaired,
3618 ns->ln_linkea_repaired,
3619 ns->ln_objs_nlink_repaired,
3620 ns->ln_mul_linked_checked,
3621 ns->ln_mul_linked_repaired,
3622 ns->ln_unknown_inconsistency,
3623 ns->ln_unmatched_pairs_repaired,
3624 ns->ln_dangling_repaired,
3625 ns->ln_mul_ref_repaired,
3626 ns->ln_bad_type_repaired,
3627 ns->ln_lost_dirent_repaired,
3628 ns->ln_local_lpf_scanned,
3629 ns->ln_local_lpf_moved,
3630 ns->ln_local_lpf_skipped,
3631 ns->ln_local_lpf_failed,
3632 ns->ln_striped_dirs_scanned,
3633 ns->ln_striped_dirs_repaired,
3634 ns->ln_striped_dirs_failed,
3635 ns->ln_striped_dirs_disabled,
3636 ns->ln_striped_dirs_skipped,
3637 ns->ln_striped_shards_scanned,
3638 ns->ln_striped_shards_repaired,
3639 ns->ln_striped_shards_failed,
3640 ns->ln_striped_shards_skipped,
3641 ns->ln_name_hash_repaired,
3642 ns->ln_success_count,
3647 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3648 struct lfsck_component *com)
3650 struct lfsck_instance *lfsck = com->lc_lfsck;
3651 struct lfsck_namespace *ns = com->lc_file_ram;
3653 while (!list_empty(&lfsck->li_list_lmv)) {
3654 struct lfsck_lmv_unit *llu;
3655 struct lfsck_lmv *llmv;
3657 llu = list_entry(lfsck->li_list_lmv.next,
3658 struct lfsck_lmv_unit, llu_link);
3659 llmv = &llu->llu_lmv;
3661 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3662 "still in using: %u\n",
3663 atomic_read(&llmv->ll_ref));
3665 ns->ln_striped_dirs_skipped++;
3666 lfsck_lmv_put(env, llmv);
3670 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3671 struct lfsck_component *com,
3672 struct dt_object *obj)
3674 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3677 rc = dt_attr_get(env, obj, la);
3681 /* zero-linkEA object may be orphan, but it also maybe because
3682 * of upgrading. Currently, we cannot record it for double scan.
3683 * Because it may cause the LFSCK trace file to be too large. */
3685 /* "la_ctime" == 1 means that it has ever been removed from
3686 * backend /lost+found directory but not been added back to
3687 * the normal namespace yet. */
3689 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3690 unlikely(la->la_ctime == 1))
3691 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3692 LNTF_CHECK_LINKEA, true);
3697 /* namespace APIs */
3699 static int lfsck_namespace_reset(const struct lu_env *env,
3700 struct lfsck_component *com, bool init)
3702 struct lfsck_instance *lfsck = com->lc_lfsck;
3703 struct lfsck_namespace *ns = com->lc_file_ram;
3704 struct lfsck_assistant_data *lad = com->lc_data;
3705 struct dt_object *root;
3706 struct dt_object *dto;
3710 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3712 GOTO(log, rc = PTR_ERR(root));
3714 if (unlikely(!dt_try_as_dir(env, root)))
3715 GOTO(put, rc = -ENOTDIR);
3717 down_write(&com->lc_sem);
3719 memset(ns, 0, sizeof(*ns));
3721 __u32 count = ns->ln_success_count;
3722 __u64 last_time = ns->ln_time_last_complete;
3724 memset(ns, 0, sizeof(*ns));
3725 ns->ln_success_count = count;
3726 ns->ln_time_last_complete = last_time;
3728 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3729 ns->ln_status = LS_INIT;
3731 lfsck_object_put(env, com->lc_obj);
3733 dto = lfsck_namespace_load_one_trace_file(env, com, root,
3734 LFSCK_NAMESPACE, true);
3736 GOTO(out, rc = PTR_ERR(dto));
3739 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3743 lad->lad_incomplete = 0;
3744 CFS_RESET_BITMAP(lad->lad_bitmap);
3746 rc = lfsck_namespace_store(env, com);
3751 up_write(&com->lc_sem);
3754 lfsck_object_put(env, root);
3756 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3757 lfsck_lfsck2name(lfsck), rc);
3762 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3765 struct lfsck_namespace *ns = com->lc_file_ram;
3767 down_write(&com->lc_sem);
3769 com->lc_new_checked++;
3770 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3771 up_write(&com->lc_sem);
3774 static void lfsck_namespace_close_dir(const struct lu_env *env,
3775 struct lfsck_component *com)
3777 struct lfsck_namespace *ns = com->lc_file_ram;
3778 struct lfsck_assistant_data *lad = com->lc_data;
3779 struct lfsck_assistant_object *lso = NULL;
3780 struct lfsck_instance *lfsck = com->lc_lfsck;
3781 struct lfsck_lmv *llmv = lfsck->li_lmv;
3782 struct lfsck_namespace_req *lnr;
3784 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3785 bool wakeup = false;
3791 OBD_ALLOC(lnr, size);
3793 ns->ln_striped_dirs_skipped++;
3798 lso = lfsck_assistant_object_init(env, lfsck_dto2fid(lfsck->li_obj_dir),
3799 NULL, lfsck->li_pos_current.lp_oit_cookie, true);
3801 OBD_FREE(lnr, size);
3802 ns->ln_striped_dirs_skipped++;
3807 /* Generate a dummy request to indicate that all shards' name entry
3808 * in this striped directory has been scanned for the first time. */
3809 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3810 lnr->lnr_lar.lar_parent = lso;
3811 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3812 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3813 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3814 lnr->lnr_size = size;
3816 spin_lock(&lad->lad_lock);
3817 if (lad->lad_assistant_status < 0) {
3818 spin_unlock(&lad->lad_lock);
3819 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3820 ns->ln_striped_dirs_skipped++;
3825 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3826 if (lad->lad_prefetched == 0)
3829 lad->lad_prefetched++;
3830 spin_unlock(&lad->lad_lock);
3832 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3837 static int lfsck_namespace_open_dir(const struct lu_env *env,
3838 struct lfsck_component *com)
3840 struct lfsck_instance *lfsck = com->lc_lfsck;
3841 struct lfsck_namespace *ns = com->lc_file_ram;
3842 struct lfsck_lmv *llmv = lfsck->li_lmv;
3849 if (llmv->ll_lmv_master) {
3850 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3852 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
3853 lmv->lmv_master_mdt_index =
3854 lfsck_dev_idx(lfsck);
3855 ns->ln_flags |= LF_INCONSISTENT;
3856 llmv->ll_lmv_updated = 1;
3859 rc = lfsck_namespace_verify_stripe_slave(env, com,
3860 lfsck->li_obj_dir, llmv);
3863 RETURN(rc > 0 ? 0 : rc);
3866 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3867 struct lfsck_component *com, bool init)
3869 struct lfsck_instance *lfsck = com->lc_lfsck;
3870 struct lfsck_namespace *ns = com->lc_file_ram;
3874 rc = lfsck_checkpoint_generic(env, com);
3879 down_write(&com->lc_sem);
3881 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3883 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3884 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3885 HALF_SEC - lfsck->li_time_last_checkpoint);
3886 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3887 ns->ln_items_checked += com->lc_new_checked;
3888 com->lc_new_checked = 0;
3891 rc = lfsck_namespace_store(env, com);
3892 up_write(&com->lc_sem);
3895 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3896 ", "DFID", "LPX64"], status = %d: rc = %d\n",
3897 lfsck_lfsck2name(lfsck), lfsck->li_pos_current.lp_oit_cookie,
3898 PFID(&lfsck->li_pos_current.lp_dir_parent),
3899 lfsck->li_pos_current.lp_dir_cookie, ns->ln_status, rc);
3901 return rc > 0 ? 0 : rc;
3904 static int lfsck_namespace_prep(const struct lu_env *env,
3905 struct lfsck_component *com,
3906 struct lfsck_start_param *lsp)
3908 struct lfsck_instance *lfsck = com->lc_lfsck;
3909 struct lfsck_namespace *ns = com->lc_file_ram;
3910 struct lfsck_position *pos = &com->lc_pos_start;
3913 rc = lfsck_namespace_load_bitmap(env, com);
3914 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3915 rc = lfsck_namespace_reset(env, com, false);
3917 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3920 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3921 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3927 down_write(&com->lc_sem);
3928 ns->ln_time_latest_start = cfs_time_current_sec();
3929 spin_lock(&lfsck->li_lock);
3931 if (ns->ln_flags & LF_SCANNED_ONCE) {
3932 if (!lfsck->li_drop_dryrun ||
3933 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3934 ns->ln_status = LS_SCANNING_PHASE2;
3935 list_move_tail(&com->lc_link,
3936 &lfsck->li_list_double_scan);
3937 if (!list_empty(&com->lc_link_dir))
3938 list_del_init(&com->lc_link_dir);
3939 lfsck_pos_set_zero(pos);
3941 ns->ln_status = LS_SCANNING_PHASE1;
3942 ns->ln_run_time_phase1 = 0;
3943 ns->ln_run_time_phase2 = 0;
3944 ns->ln_items_checked = 0;
3945 ns->ln_items_repaired = 0;
3946 ns->ln_items_failed = 0;
3947 ns->ln_dirs_checked = 0;
3948 ns->ln_objs_checked_phase2 = 0;
3949 ns->ln_objs_repaired_phase2 = 0;
3950 ns->ln_objs_failed_phase2 = 0;
3951 ns->ln_objs_nlink_repaired = 0;
3952 ns->ln_dirent_repaired = 0;
3953 ns->ln_linkea_repaired = 0;
3954 ns->ln_mul_linked_checked = 0;
3955 ns->ln_mul_linked_repaired = 0;
3956 ns->ln_unknown_inconsistency = 0;
3957 ns->ln_unmatched_pairs_repaired = 0;
3958 ns->ln_dangling_repaired = 0;
3959 ns->ln_mul_ref_repaired = 0;
3960 ns->ln_bad_type_repaired = 0;
3961 ns->ln_lost_dirent_repaired = 0;
3962 ns->ln_striped_dirs_scanned = 0;
3963 ns->ln_striped_dirs_repaired = 0;
3964 ns->ln_striped_dirs_failed = 0;
3965 ns->ln_striped_dirs_disabled = 0;
3966 ns->ln_striped_dirs_skipped = 0;
3967 ns->ln_striped_shards_scanned = 0;
3968 ns->ln_striped_shards_repaired = 0;
3969 ns->ln_striped_shards_failed = 0;
3970 ns->ln_striped_shards_skipped = 0;
3971 ns->ln_name_hash_repaired = 0;
3972 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3973 if (list_empty(&com->lc_link_dir))
3974 list_add_tail(&com->lc_link_dir,
3975 &lfsck->li_list_dir);
3976 *pos = ns->ln_pos_first_inconsistent;
3979 ns->ln_status = LS_SCANNING_PHASE1;
3980 if (list_empty(&com->lc_link_dir))
3981 list_add_tail(&com->lc_link_dir,
3982 &lfsck->li_list_dir);
3983 if (!lfsck->li_drop_dryrun ||
3984 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3985 *pos = ns->ln_pos_last_checkpoint;
3986 pos->lp_oit_cookie++;
3988 *pos = ns->ln_pos_first_inconsistent;
3992 spin_unlock(&lfsck->li_lock);
3993 up_write(&com->lc_sem);
3995 rc = lfsck_start_assistant(env, com, lsp);
3997 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
3998 DFID", "LPX64"]: rc = %d\n",
3999 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4000 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4005 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4006 struct lfsck_component *com,
4007 struct dt_object *obj)
4009 struct lfsck_thread_info *info = lfsck_env_info(env);
4010 struct lfsck_namespace *ns = com->lc_file_ram;
4011 struct lfsck_instance *lfsck = com->lc_lfsck;
4012 const struct lu_fid *fid = lfsck_dto2fid(obj);
4013 struct lu_fid *pfid = &info->lti_fid2;
4014 struct lu_name *cname = &info->lti_name;
4015 struct lu_seq_range *range = &info->lti_range;
4016 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4017 struct linkea_data ldata = { NULL };
4018 __u32 idx = lfsck_dev_idx(lfsck);
4022 rc = lfsck_links_read(env, obj, &ldata);
4026 /* -EINVAL means crashed linkEA, should be verified. */
4027 if (rc == -EINVAL) {
4028 rc = lfsck_namespace_trace_update(env, com, fid,
4029 LNTF_CHECK_LINKEA, true);
4031 struct lustre_handle lh = { 0 };
4033 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4034 MDS_INODELOCK_UPDATE |
4035 MDS_INODELOCK_XATTR, LCK_EX);
4037 rc = lfsck_namespace_links_remove(env, com,
4039 lfsck_ibits_unlock(&lh, LCK_EX);
4043 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4046 if (rc == -ENODATA) {
4047 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4055 /* Record multiple-linked object. */
4056 if (ldata.ld_leh->leh_reccount > 1) {
4057 rc = lfsck_namespace_trace_update(env, com, fid,
4058 LNTF_CHECK_LINKEA, true);
4063 linkea_first_entry(&ldata);
4064 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4065 if (!fid_is_sane(pfid)) {
4066 rc = lfsck_namespace_trace_update(env, com, fid,
4067 LNTF_CHECK_PARENT, true);
4069 fld_range_set_mdt(range);
4070 rc = fld_local_lookup(env, ss->ss_server_fld,
4071 fid_seq(pfid), range);
4072 if ((rc == -ENOENT) ||
4073 (rc == 0 && range->lsr_index != idx))
4074 rc = lfsck_namespace_trace_update(env, com, fid,
4075 LNTF_CHECK_LINKEA, true);
4077 rc = lfsck_namespace_check_for_double_scan(env, com,
4084 down_write(&com->lc_sem);
4085 if (S_ISDIR(lfsck_object_type(obj)))
4086 ns->ln_dirs_checked++;
4088 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4089 up_write(&com->lc_sem);
4094 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4095 struct lfsck_component *com,
4096 struct lfsck_assistant_object *lso,
4097 struct lu_dirent *ent, __u16 type)
4099 struct lfsck_assistant_data *lad = com->lc_data;
4100 struct lfsck_instance *lfsck = com->lc_lfsck;
4101 struct lfsck_namespace_req *lnr;
4102 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4103 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4104 struct ptlrpc_thread *athread = &lad->lad_thread;
4105 struct l_wait_info lwi = { 0 };
4106 bool wakeup = false;
4108 l_wait_event(mthread->t_ctl_waitq,
4109 lad->lad_prefetched < bk->lb_async_windows ||
4110 !thread_is_running(mthread) ||
4111 thread_is_stopped(athread),
4114 if (unlikely(!thread_is_running(mthread)) ||
4115 thread_is_stopped(athread))
4118 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4121 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, lso, ent, type);
4123 struct lfsck_namespace *ns = com->lc_file_ram;
4125 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4126 return PTR_ERR(lnr);
4129 spin_lock(&lad->lad_lock);
4130 if (lad->lad_assistant_status < 0) {
4131 spin_unlock(&lad->lad_lock);
4132 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4133 return lad->lad_assistant_status;
4136 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4137 if (lad->lad_prefetched == 0)
4140 lad->lad_prefetched++;
4141 spin_unlock(&lad->lad_lock);
4143 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4145 down_write(&com->lc_sem);
4146 com->lc_new_checked++;
4147 up_write(&com->lc_sem);
4152 static int lfsck_namespace_post(const struct lu_env *env,
4153 struct lfsck_component *com,
4154 int result, bool init)
4156 struct lfsck_instance *lfsck = com->lc_lfsck;
4157 struct lfsck_namespace *ns = com->lc_file_ram;
4161 lfsck_post_generic(env, com, &result);
4163 down_write(&com->lc_sem);
4164 lfsck_namespace_release_lmv(env, com);
4166 spin_lock(&lfsck->li_lock);
4168 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4170 ns->ln_status = LS_SCANNING_PHASE2;
4171 ns->ln_flags |= LF_SCANNED_ONCE;
4172 ns->ln_flags &= ~LF_UPGRADE;
4173 list_del_init(&com->lc_link_dir);
4174 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4175 } else if (result == 0) {
4176 if (lfsck->li_status != 0)
4177 ns->ln_status = lfsck->li_status;
4179 ns->ln_status = LS_STOPPED;
4180 if (ns->ln_status != LS_PAUSED) {
4181 list_del_init(&com->lc_link_dir);
4182 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4185 ns->ln_status = LS_FAILED;
4186 list_del_init(&com->lc_link_dir);
4187 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4189 spin_unlock(&lfsck->li_lock);
4192 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4193 HALF_SEC - lfsck->li_time_last_checkpoint);
4194 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4195 ns->ln_items_checked += com->lc_new_checked;
4196 com->lc_new_checked = 0;
4199 rc = lfsck_namespace_store(env, com);
4200 up_write(&com->lc_sem);
4202 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4203 lfsck_lfsck2name(lfsck), rc);
4209 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4212 struct lfsck_instance *lfsck = com->lc_lfsck;
4213 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4214 struct lfsck_namespace *ns = com->lc_file_ram;
4216 down_read(&com->lc_sem);
4217 seq_printf(m, "name: lfsck_namespace\n"
4223 lfsck_status2name(ns->ln_status));
4225 lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4227 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4229 lfsck_time_dump(m, ns->ln_time_last_complete, "last_completed");
4231 lfsck_time_dump(m, ns->ln_time_latest_start, "latest_start");
4233 lfsck_time_dump(m, ns->ln_time_last_checkpoint, "last_checkpoint");
4235 lfsck_pos_dump(m, &ns->ln_pos_latest_start, "latest_start_position");
4237 lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4238 "last_checkpoint_position");
4240 lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4241 "first_failure_position");
4243 if (ns->ln_status == LS_SCANNING_PHASE1) {
4244 struct lfsck_position pos;
4245 const struct dt_it_ops *iops;
4246 cfs_duration_t duration = cfs_time_current() -
4247 lfsck->li_time_last_checkpoint;
4248 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4249 __u64 speed = checked;
4250 __u64 new_checked = com->lc_new_checked *
4251 msecs_to_jiffies(MSEC_PER_SEC);
4252 __u32 rtime = ns->ln_run_time_phase1 +
4253 cfs_duration_sec(duration + HALF_SEC);
4256 do_div(new_checked, duration);
4259 do_div(speed, rtime);
4261 lfsck_namespace_dump_statistics(m, ns, checked, 0, rtime, 0);
4262 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4263 "average_speed_phase2: N/A\n"
4264 "average_speed_total: "LPU64" items/sec\n"
4265 "real_time_speed_phase1: "LPU64" items/sec\n"
4266 "real_time_speed_phase2: N/A\n",
4271 LASSERT(lfsck->li_di_oit != NULL);
4273 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4275 /* The low layer otable-based iteration position may NOT
4276 * exactly match the namespace-based directory traversal
4277 * cookie. Generally, it is not a serious issue. But the
4278 * caller should NOT make assumption on that. */
4279 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4280 if (!lfsck->li_current_oit_processed)
4281 pos.lp_oit_cookie--;
4283 spin_lock(&lfsck->li_lock);
4284 if (lfsck->li_di_dir != NULL) {
4285 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4286 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4287 fid_zero(&pos.lp_dir_parent);
4288 pos.lp_dir_cookie = 0;
4291 *lfsck_dto2fid(lfsck->li_obj_dir);
4294 fid_zero(&pos.lp_dir_parent);
4295 pos.lp_dir_cookie = 0;
4297 spin_unlock(&lfsck->li_lock);
4298 lfsck_pos_dump(m, &pos, "current_position");
4299 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4300 cfs_duration_t duration = cfs_time_current() -
4301 com->lc_time_last_checkpoint;
4302 __u64 checked = ns->ln_objs_checked_phase2 +
4303 com->lc_new_checked;
4304 __u64 speed1 = ns->ln_items_checked;
4305 __u64 speed2 = checked;
4306 __u64 speed0 = speed1 + speed2;
4307 __u64 new_checked = com->lc_new_checked *
4308 msecs_to_jiffies(MSEC_PER_SEC);
4309 __u32 rtime = ns->ln_run_time_phase2 +
4310 cfs_duration_sec(duration + HALF_SEC);
4311 __u32 time0 = ns->ln_run_time_phase1 + rtime;
4314 do_div(new_checked, duration);
4316 if (ns->ln_run_time_phase1 != 0)
4317 do_div(speed1, ns->ln_run_time_phase1);
4318 else if (ns->ln_items_checked != 0)
4322 do_div(speed2, rtime);
4323 else if (checked != 0)
4327 do_div(speed0, time0);
4329 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4331 ns->ln_run_time_phase1, rtime);
4332 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4333 "average_speed_phase2: "LPU64" objs/sec\n"
4334 "average_speed_total: "LPU64" items/sec\n"
4335 "real_time_speed_phase1: N/A\n"
4336 "real_time_speed_phase2: "LPU64" objs/sec\n"
4337 "current_position: "DFID"\n",
4342 PFID(&ns->ln_fid_latest_scanned_phase2));
4344 __u64 speed1 = ns->ln_items_checked;
4345 __u64 speed2 = ns->ln_objs_checked_phase2;
4346 __u64 speed0 = speed1 + speed2;
4347 __u32 time0 = ns->ln_run_time_phase1 + ns->ln_run_time_phase2;
4349 if (ns->ln_run_time_phase1 != 0)
4350 do_div(speed1, ns->ln_run_time_phase1);
4351 else if (ns->ln_items_checked != 0)
4354 if (ns->ln_run_time_phase2 != 0)
4355 do_div(speed2, ns->ln_run_time_phase2);
4356 else if (ns->ln_objs_checked_phase2 != 0)
4360 do_div(speed0, time0);
4362 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4363 ns->ln_objs_checked_phase2,
4364 ns->ln_run_time_phase1,
4365 ns->ln_run_time_phase2);
4366 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4367 "average_speed_phase2: "LPU64" objs/sec\n"
4368 "average_speed_total: "LPU64" items/sec\n"
4369 "real_time_speed_phase1: N/A\n"
4370 "real_time_speed_phase2: N/A\n"
4371 "current_position: N/A\n",
4377 up_read(&com->lc_sem);
4380 static int lfsck_namespace_double_scan(const struct lu_env *env,
4381 struct lfsck_component *com)
4383 struct lfsck_namespace *ns = com->lc_file_ram;
4384 struct lfsck_assistant_data *lad = com->lc_data;
4385 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4386 struct lfsck_tgt_desc *ltd;
4387 struct lfsck_tgt_desc *next;
4390 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4391 if (thread_is_stopped(&lad->lad_thread)) {
4392 LASSERT(list_empty(&lad->lad_req_list));
4393 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4395 spin_lock(<ds->ltd_lock);
4396 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4397 ltd_namespace_phase_list) {
4398 list_del_init(<d->ltd_namespace_phase_list);
4400 spin_unlock(<ds->ltd_lock);
4406 static void lfsck_namespace_data_release(const struct lu_env *env,
4407 struct lfsck_component *com)
4409 struct lfsck_assistant_data *lad = com->lc_data;
4410 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4411 struct lfsck_tgt_desc *ltd;
4412 struct lfsck_tgt_desc *next;
4414 LASSERT(lad != NULL);
4415 LASSERT(thread_is_init(&lad->lad_thread) ||
4416 thread_is_stopped(&lad->lad_thread));
4417 LASSERT(list_empty(&lad->lad_req_list));
4419 com->lc_data = NULL;
4420 lfsck_namespace_release_lmv(env, com);
4422 spin_lock(<ds->ltd_lock);
4423 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4424 ltd_namespace_phase_list) {
4425 list_del_init(<d->ltd_namespace_phase_list);
4427 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4428 ltd_namespace_phase_list) {
4429 list_del_init(<d->ltd_namespace_phase_list);
4431 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4432 ltd_namespace_list) {
4433 list_del_init(<d->ltd_namespace_list);
4435 spin_unlock(<ds->ltd_lock);
4437 if (likely(lad->lad_bitmap != NULL))
4438 CFS_FREE_BITMAP(lad->lad_bitmap);
4443 static void lfsck_namespace_quit(const struct lu_env *env,
4444 struct lfsck_component *com)
4446 struct lfsck_assistant_data *lad = com->lc_data;
4447 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4448 struct lfsck_tgt_desc *ltd;
4449 struct lfsck_tgt_desc *next;
4451 LASSERT(lad != NULL);
4453 lfsck_quit_generic(env, com);
4455 LASSERT(thread_is_init(&lad->lad_thread) ||
4456 thread_is_stopped(&lad->lad_thread));
4457 LASSERT(list_empty(&lad->lad_req_list));
4459 lfsck_namespace_release_lmv(env, com);
4461 spin_lock(<ds->ltd_lock);
4462 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4463 ltd_namespace_phase_list) {
4464 list_del_init(<d->ltd_namespace_phase_list);
4466 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4467 ltd_namespace_phase_list) {
4468 list_del_init(<d->ltd_namespace_phase_list);
4470 spin_unlock(<ds->ltd_lock);
4473 static int lfsck_namespace_in_notify(const struct lu_env *env,
4474 struct lfsck_component *com,
4475 struct lfsck_request *lr,
4478 struct lfsck_instance *lfsck = com->lc_lfsck;
4479 struct lfsck_namespace *ns = com->lc_file_ram;
4480 struct lfsck_assistant_data *lad = com->lc_data;
4481 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4482 struct lfsck_tgt_desc *ltd;
4487 switch (lr->lr_event) {
4488 case LE_SKIP_NLINK_DECLARE: {
4489 struct dt_object *obj;
4490 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4494 LASSERT(th != NULL);
4496 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4497 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4498 fid_cpu_to_be(key, &lr->lr_fid);
4499 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4500 rc = dt_declare_delete(env, obj,
4501 (const struct dt_key *)key, th);
4503 rc = dt_declare_insert(env, obj,
4504 (const struct dt_rec *)&flags,
4505 (const struct dt_key *)key, th);
4506 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4510 case LE_SKIP_NLINK: {
4511 struct dt_object *obj;
4512 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4518 LASSERT(th != NULL);
4520 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4521 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4522 fid_cpu_to_be(key, &lr->lr_fid);
4523 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4524 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4525 (const struct dt_key *)key);
4527 if (flags & LNTF_SKIP_NLINK) {
4529 &com->lc_sub_trace_objs[idx].lsto_mutex);
4535 } else if (rc != -ENOENT) {
4539 flags |= LNTF_SKIP_NLINK;
4541 rc = dt_delete(env, obj, (const struct dt_key *)key,
4547 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4548 (const struct dt_key *)key, th, 1);
4553 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4554 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4555 " to be skipped for namespace double scan: rc = %d\n",
4556 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4559 /* If we cannot record this object in the LFSCK tracing,
4560 * we have to mark the LFSC as LF_INCOMPLETE, then the
4561 * LFSCK will skip nlink attribute verification for
4563 ns->ln_flags |= LF_INCOMPLETE;
4567 case LE_SET_LMV_MASTER: {
4568 struct dt_object *obj;
4570 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4572 RETURN(PTR_ERR(obj));
4574 if (likely(dt_object_exists(obj)))
4575 rc = lfsck_namespace_notify_lmv_master_local(env, com,
4578 lfsck_object_put(env, obj);
4580 RETURN(rc > 0 ? 0 : rc);
4582 case LE_SET_LMV_SLAVE: {
4583 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4584 ns->ln_striped_shards_repaired++;
4586 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4587 LNTF_RECHECK_NAME_HASH, true);
4589 RETURN(rc > 0 ? 0 : rc);
4591 case LE_PHASE1_DONE:
4592 case LE_PHASE2_DONE:
4599 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4600 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4601 lr->lr_index, lr->lr_status, lr->lr_flags2);
4603 spin_lock(<ds->ltd_lock);
4604 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
4606 spin_unlock(<ds->ltd_lock);
4611 list_del_init(<d->ltd_namespace_phase_list);
4612 switch (lr->lr_event) {
4613 case LE_PHASE1_DONE:
4614 if (lr->lr_status <= 0) {
4615 ltd->ltd_namespace_done = 1;
4616 list_del_init(<d->ltd_namespace_list);
4617 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4618 "phase1 for namespace LFSCK: rc = %d.\n",
4619 lfsck_lfsck2name(lfsck),
4620 ltd->ltd_index, lr->lr_status);
4621 ns->ln_flags |= LF_INCOMPLETE;
4626 if (lr->lr_flags2 & LF_INCOMPLETE)
4627 ns->ln_flags |= LF_INCOMPLETE;
4629 if (list_empty(<d->ltd_namespace_list))
4630 list_add_tail(<d->ltd_namespace_list,
4631 &lad->lad_mdt_list);
4632 list_add_tail(<d->ltd_namespace_phase_list,
4633 &lad->lad_mdt_phase2_list);
4635 case LE_PHASE2_DONE:
4636 ltd->ltd_namespace_done = 1;
4637 list_del_init(<d->ltd_namespace_list);
4641 ltd->ltd_namespace_done = 1;
4642 list_del_init(<d->ltd_namespace_list);
4643 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4645 "%s: the peer MDT %x exit namespace LFSCK\n",
4646 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4647 ns->ln_flags |= LF_INCOMPLETE;
4653 spin_unlock(<ds->ltd_lock);
4655 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4656 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4658 memset(stop, 0, sizeof(*stop));
4659 stop->ls_status = lr->lr_status;
4660 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4661 lfsck_stop(env, lfsck->li_bottom, stop);
4662 } else if (lfsck_phase2_next_ready(lad)) {
4663 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4669 static void lfsck_namespace_repaired(struct lfsck_namespace *ns, __u64 *count)
4671 *count += ns->ln_objs_nlink_repaired;
4672 *count += ns->ln_dirent_repaired;
4673 *count += ns->ln_linkea_repaired;
4674 *count += ns->ln_mul_linked_repaired;
4675 *count += ns->ln_unmatched_pairs_repaired;
4676 *count += ns->ln_dangling_repaired;
4677 *count += ns->ln_mul_ref_repaired;
4678 *count += ns->ln_bad_type_repaired;
4679 *count += ns->ln_lost_dirent_repaired;
4680 *count += ns->ln_striped_dirs_disabled;
4681 *count += ns->ln_striped_dirs_repaired;
4682 *count += ns->ln_striped_shards_repaired;
4683 *count += ns->ln_name_hash_repaired;
4684 *count += ns->ln_local_lpf_moved;
4687 static int lfsck_namespace_query_all(const struct lu_env *env,
4688 struct lfsck_component *com,
4689 __u32 *mdts_count, __u64 *repaired)
4691 struct lfsck_namespace *ns = com->lc_file_ram;
4692 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4693 struct lfsck_tgt_desc *ltd;
4698 rc = lfsck_query_all(env, com);
4702 down_read(<ds->ltd_rw_sem);
4703 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
4704 ltd = lfsck_ltd2tgt(ltds, idx);
4705 LASSERT(ltd != NULL);
4707 mdts_count[ltd->ltd_namespace_status]++;
4708 *repaired += ltd->ltd_namespace_repaired;
4710 up_read(<ds->ltd_rw_sem);
4712 down_read(&com->lc_sem);
4713 mdts_count[ns->ln_status]++;
4714 lfsck_namespace_repaired(ns, repaired);
4715 up_read(&com->lc_sem);
4720 static int lfsck_namespace_query(const struct lu_env *env,
4721 struct lfsck_component *com,
4722 struct lfsck_request *req,
4723 struct lfsck_reply *rep,
4724 struct lfsck_query *que, int idx)
4726 struct lfsck_namespace *ns = com->lc_file_ram;
4730 LASSERT(com->lc_lfsck->li_master);
4732 rc = lfsck_namespace_query_all(env, com,
4733 que->lu_mdts_count[idx],
4734 &que->lu_repaired[idx]);
4736 down_read(&com->lc_sem);
4737 rep->lr_status = ns->ln_status;
4738 if (req->lr_flags & LEF_QUERY_ALL)
4739 lfsck_namespace_repaired(ns, &rep->lr_repaired);
4740 up_read(&com->lc_sem);
4746 static struct lfsck_operations lfsck_namespace_ops = {
4747 .lfsck_reset = lfsck_namespace_reset,
4748 .lfsck_fail = lfsck_namespace_fail,
4749 .lfsck_close_dir = lfsck_namespace_close_dir,
4750 .lfsck_open_dir = lfsck_namespace_open_dir,
4751 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4752 .lfsck_prep = lfsck_namespace_prep,
4753 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4754 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4755 .lfsck_post = lfsck_namespace_post,
4756 .lfsck_dump = lfsck_namespace_dump,
4757 .lfsck_double_scan = lfsck_namespace_double_scan,
4758 .lfsck_data_release = lfsck_namespace_data_release,
4759 .lfsck_quit = lfsck_namespace_quit,
4760 .lfsck_in_notify = lfsck_namespace_in_notify,
4761 .lfsck_query = lfsck_namespace_query,
4765 * Repair dangling name entry.
4767 * For the name entry with dangling reference, we need to repare the
4768 * inconsistency according to the LFSCK sponsor's requirement:
4770 * 1) Keep the inconsistency there and report the inconsistency case,
4771 * then give the chance to the application to find related issues,
4772 * and the users can make the decision about how to handle it with
4773 * more human knownledge. (by default)
4775 * 2) Re-create the missing MDT-object with the FID information.
4777 * \param[in] env pointer to the thread context
4778 * \param[in] com pointer to the lfsck component
4779 * \param[in] parent pointer to the dir object that contains the dangling
4781 * \param[in] child pointer to the object corresponding to the dangling
4783 * \param[in] lnr pointer to the namespace request that contains the
4784 * name's name, parent object, parent's LMV, and ect.
4786 * \retval positive number if no need to repair
4787 * \retval zero for repaired successfully
4788 * \retval negative error number on failure
4790 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4791 struct lfsck_component *com,
4792 struct dt_object *parent,
4793 struct dt_object *child,
4794 struct lfsck_namespace_req *lnr)
4796 struct lfsck_thread_info *info = lfsck_env_info(env);
4797 struct lu_attr *la = &info->lti_la;
4798 struct dt_allocation_hint *hint = &info->lti_hint;
4799 struct dt_object_format *dof = &info->lti_dof;
4800 struct dt_insert_rec *rec = &info->lti_dt_rec;
4801 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4802 const struct lu_name *cname;
4803 const struct lu_fid *pfid = lfsck_dto2fid(parent);
4804 const struct lu_fid *cfid = lfsck_dto2fid(child);
4805 struct linkea_data ldata = { NULL };
4806 struct lfsck_lock_handle *llh = &info->lti_llh;
4807 struct lu_buf linkea_buf;
4808 struct lu_buf lmv_buf;
4809 struct lfsck_instance *lfsck = com->lc_lfsck;
4810 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4811 struct dt_device *dev = lfsck->li_next;
4812 struct thandle *th = NULL;
4814 __u16 type = lnr->lnr_type;
4818 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4819 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4824 if (!create || bk->lb_param & LPF_DRYRUN)
4827 /* We may need to create the sub-objects of the @child via LOD,
4828 * so make the modification based on lfsck->li_next. */
4830 parent = lfsck_object_locate(dev, parent);
4832 GOTO(log, rc = PTR_ERR(parent));
4834 if (unlikely(!dt_try_as_dir(env, parent)))
4835 GOTO(log, rc = -ENOTDIR);
4837 child = lfsck_object_locate(dev, child);
4839 GOTO(log, rc = PTR_ERR(child));
4841 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4845 rc = linkea_add_buf(&ldata, cname, pfid);
4849 rc = lfsck_lock(env, lfsck, parent, lnr->lnr_name, llh,
4850 MDS_INODELOCK_UPDATE, LCK_PR);
4854 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4858 /* Set the ctime as zero, then others can know it is created for
4859 * repairing dangling name entry by LFSCK. And if the LFSCK made
4860 * wrong decision and the real MDT-object has been found later,
4861 * then the LFSCK has chance to fix the incosistency properly. */
4862 memset(la, 0, sizeof(*la));
4863 la->la_mode = (type & S_IFMT) | 0600;
4864 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4865 LA_ATIME | LA_MTIME | LA_CTIME;
4867 child->do_ops->do_ah_init(env, hint, parent, child,
4868 la->la_mode & S_IFMT);
4870 memset(dof, 0, sizeof(*dof));
4871 dof->dof_type = dt_mode_to_dft(type);
4872 /* If the target is a regular file, then the LFSCK will only create
4873 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4874 * OST-objects will be created when write open. */
4876 th = dt_trans_create(env, dev);
4878 GOTO(log, rc = PTR_ERR(th));
4880 /* 1a. create child. */
4881 rc = dt_declare_create(env, child, la, hint, dof, th);
4885 if (S_ISDIR(type)) {
4886 if (unlikely(!dt_try_as_dir(env, child)))
4887 GOTO(stop, rc = -ENOTDIR);
4889 /* 2a. increase child nlink */
4890 rc = dt_declare_ref_add(env, child, th);
4894 /* 3a. insert dot into child dir */
4895 rec->rec_type = S_IFDIR;
4896 rec->rec_fid = cfid;
4897 rc = dt_declare_insert(env, child,
4898 (const struct dt_rec *)rec,
4899 (const struct dt_key *)dot, th);
4903 /* 4a. insert dotdot into child dir */
4904 rec->rec_fid = pfid;
4905 rc = dt_declare_insert(env, child,
4906 (const struct dt_rec *)rec,
4907 (const struct dt_key *)dotdot, th);
4911 /* 5a. generate slave LMV EA. */
4912 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4915 idx = lfsck_shard_name_to_index(env,
4916 lnr->lnr_name, lnr->lnr_namelen,
4918 if (unlikely(idx < 0))
4919 GOTO(stop, rc = idx);
4921 *lmv2 = lnr->lnr_lmv->ll_lmv;
4922 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4923 lmv2->lmv_master_mdt_index = idx;
4925 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4926 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4927 rc = dt_declare_xattr_set(env, child, &lmv_buf,
4928 XATTR_NAME_LMV, 0, th);
4934 /* 6a. insert linkEA for child */
4935 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4936 ldata.ld_leh->leh_len);
4937 rc = dt_declare_xattr_set(env, child, &linkea_buf,
4938 XATTR_NAME_LINK, 0, th);
4942 rc = dt_trans_start_local(env, dev, th);
4944 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4946 dt_write_lock(env, child, 0);
4947 /* 1b. create child */
4948 rc = dt_create(env, child, la, hint, dof, th);
4950 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4952 if (S_ISDIR(type)) {
4953 /* 2b. increase child nlink */
4954 rc = dt_ref_add(env, child, th);
4958 /* 3b. insert dot into child dir */
4959 rec->rec_type = S_IFDIR;
4960 rec->rec_fid = cfid;
4961 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4962 (const struct dt_key *)dot, th, 1);
4966 /* 4b. insert dotdot into child dir */
4967 rec->rec_fid = pfid;
4968 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4969 (const struct dt_key *)dotdot, th, 1);
4973 /* 5b. generate slave LMV EA. */
4974 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4975 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
4982 /* 6b. insert linkEA for child. */
4983 rc = dt_xattr_set(env, child, &linkea_buf,
4984 XATTR_NAME_LINK, 0, th);
4989 dt_write_unlock(env, child);
4992 dt_trans_stop(env, dev, th);
4996 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4997 "reference for: parent "DFID", child "DFID", type %u, "
4998 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4999 PFID(pfid), PFID(cfid), type, cname->ln_name,
5000 create ? "Create the lost MDT-object as required" :
5001 "Keep the MDT-object there by default", rc);
5004 struct lfsck_namespace *ns = com->lc_file_ram;
5006 ns->ln_flags |= LF_INCONSISTENT;
5012 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
5013 struct lfsck_component *com,
5014 struct lfsck_assistant_req *lar)
5016 struct lfsck_thread_info *info = lfsck_env_info(env);
5017 struct lu_attr *la = &info->lti_la;
5018 struct lfsck_instance *lfsck = com->lc_lfsck;
5019 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5020 struct lfsck_namespace *ns = com->lc_file_ram;
5021 struct lfsck_assistant_data *lad = com->lc_data;
5022 struct linkea_data ldata = { NULL };
5023 const struct lu_name *cname;
5024 struct thandle *handle = NULL;
5025 struct lfsck_namespace_req *lnr =
5026 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5027 struct dt_object *dir = NULL;
5028 struct dt_object *obj = NULL;
5029 struct lfsck_assistant_object *lso = lar->lar_parent;
5030 const struct lu_fid *pfid = &lso->lso_fid;
5031 struct dt_device *dev = NULL;
5032 struct lustre_handle lh = { 0 };
5033 bool repaired = false;
5034 bool dtlocked = false;
5038 bool bad_hash = false;
5039 bool bad_linkea = false;
5043 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5050 if (lnr->lnr_attr & LUDA_UPGRADE) {
5051 ns->ln_flags |= LF_UPGRADE;
5052 ns->ln_dirent_repaired++;
5054 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5055 ns->ln_flags |= LF_INCONSISTENT;
5056 ns->ln_dirent_repaired++;
5060 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5061 if (strcmp(lnr->lnr_name, dotdot) != 0)
5064 rc = lfsck_namespace_trace_update(env, com, pfid,
5065 LNTF_CHECK_PARENT, true);
5070 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5071 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5072 " for the name entry %.*s under "DFID"\n",
5073 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5074 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5076 if (strcmp(lnr->lnr_name, dotdot) != 0)
5077 /* invalid FID means bad name entry, remove it. */
5078 type = LNIT_BAD_DIRENT;
5080 /* If the parent FID is invalid, we cannot remove
5081 * the ".." entry directly. */
5082 rc = lfsck_namespace_trace_update(env, com, pfid,
5083 LNTF_CHECK_PARENT, true);
5088 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5089 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5094 if (fid_seq_is_dot(fid_seq(&lnr->lnr_fid)))
5097 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5098 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5103 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5105 GOTO(out, rc = idx);
5107 if (idx == lfsck_dev_idx(lfsck)) {
5108 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5111 dev = lfsck->li_bottom;
5113 struct lfsck_tgt_desc *ltd;
5115 /* Usually, some local filesystem consistency verification
5116 * tools can guarantee the local namespace tree consistenct.
5117 * So the LFSCK will only verify the remote directory. */
5118 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5119 rc = lfsck_namespace_trace_update(env, com, pfid,
5120 LNTF_CHECK_PARENT, true);
5125 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
5126 if (unlikely(ltd == NULL)) {
5127 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5128 "did not join the namespace LFSCK\n",
5129 lfsck_lfsck2name(lfsck), idx);
5130 lfsck_lad_set_bitmap(env, com, idx);
5132 GOTO(out, rc = -ENODEV);
5138 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5140 GOTO(out, rc = PTR_ERR(obj));
5142 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5143 if (dt_object_exists(obj) == 0) {
5147 dir = lfsck_assistant_object_load(env, lfsck, lso);
5151 GOTO(trace, rc == -ENOENT ? 0 : rc);
5155 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5157 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5158 lnr->lnr_name, lnr->lnr_namelen)) {
5159 type = LNIT_BAD_DIRENT;
5164 type = LNIT_DANGLING;
5165 rc = lfsck_namespace_repair_dangling(env, com, dir,
5174 if (!(bk->lb_param & LPF_DRYRUN) && lad->lad_advance_lock) {
5177 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5178 MDS_INODELOCK_UPDATE |
5179 MDS_INODELOCK_XATTR, LCK_EX);
5183 handle = dt_trans_create(env, dev);
5185 GOTO(out, rc = PTR_ERR(handle));
5187 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5191 rc = dt_trans_start_local(env, dev, handle);
5195 dt_write_lock(env, obj, 0);
5199 rc = lfsck_links_read(env, obj, &ldata);
5200 if (unlikely(rc == -ENOENT)) {
5201 if (handle != NULL) {
5202 dt_write_unlock(env, obj);
5205 dt_trans_stop(env, dev, handle);
5208 lfsck_ibits_unlock(&lh, LCK_EX);
5211 /* It may happen when the remote object has been removed,
5212 * but the local MDT is not aware of that. */
5214 } else if (rc == 0) {
5215 count = ldata.ld_leh->leh_reccount;
5216 rc = linkea_links_find(&ldata, cname, pfid);
5218 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5219 if ((lfsck_object_type(obj) & S_IFMT) !=
5221 ns->ln_flags |= LF_INCONSISTENT;
5222 type = LNIT_BAD_TYPE;
5228 /* If the name entry hash does not match the slave striped
5229 * directory, and the name entry does not match also, then
5230 * it is quite possible that name entry is corrupted. */
5231 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5232 lnr->lnr_name, lnr->lnr_namelen)) {
5233 ns->ln_flags |= LF_INCONSISTENT;
5234 type = LNIT_BAD_DIRENT;
5239 /* If the file type stored in the name entry does not match
5240 * the file type claimed by the object, and the object does
5241 * not recognize the name entry, then it is quite possible
5242 * that the name entry is corrupted. */
5243 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5244 ns->ln_flags |= LF_INCONSISTENT;
5245 type = LNIT_BAD_DIRENT;
5250 /* For sub-dir object, we cannot make sure whether the sub-dir
5251 * back references the parent via ".." name entry correctly or
5252 * not in the LFSCK first-stage scanning. It may be that the
5253 * (remote) sub-dir ".." name entry has no parent FID after
5254 * file-level backup/restore and its linkEA may be wrong.
5255 * So under such case, we should replace the linkEA according
5256 * to current name entry. But this needs to be done during the
5257 * LFSCK second-stage scanning. The LFSCK will record the name
5258 * entry for further possible using. */
5262 } else if (unlikely(rc == -EINVAL)) {
5263 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5264 type = LNIT_BAD_TYPE;
5267 /* The magic crashed, we are not sure whether there are more
5268 * corrupt data in the linkea, so remove all linkea entries. */
5272 } else if (rc == -ENODATA) {
5273 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5274 type = LNIT_BAD_TYPE;
5281 if (bk->lb_param & LPF_DRYRUN) {
5283 ns->ln_flags |= LF_UPGRADE;
5285 ns->ln_flags |= LF_INCONSISTENT;
5286 ns->ln_linkea_repaired++;
5292 if (!lustre_handle_is_used(&lh)) {
5301 dir = lfsck_assistant_object_load(env, lfsck, lso);
5305 GOTO(stop, rc == -ENOENT ? 0 : rc);
5309 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5314 if (!remove && newdata)
5315 ns->ln_flags |= LF_UPGRADE;
5316 else if (remove || !(ns->ln_flags & LF_UPGRADE))
5317 ns->ln_flags |= LF_INCONSISTENT;
5322 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle);
5328 rc = linkea_data_new(&ldata,
5329 &lfsck_env_info(env)->lti_linkea_buf);
5334 rc = linkea_add_buf(&ldata, cname, pfid);
5338 rc = lfsck_links_write(env, obj, &ldata, handle);
5339 if (unlikely(rc == -ENOSPC) &&
5340 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5341 if (handle != NULL) {
5342 LASSERT(dt_write_locked(env, obj));
5344 dt_write_unlock(env, obj);
5347 dt_trans_stop(env, dev, handle);
5350 lfsck_ibits_unlock(&lh, LCK_EX);
5353 rc = lfsck_namespace_trace_update(env, com,
5354 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5356 /* If we cannot record this object in the
5357 * LFSCK tracing, we have to mark the LFSCK
5358 * as LF_INCOMPLETE, then the LFSCK will
5359 * skip nlink attribute verification for
5361 ns->ln_flags |= LF_INCOMPLETE;
5369 count = ldata.ld_leh->leh_reccount;
5370 if (!S_ISDIR(lfsck_object_type(obj)) ||
5371 !dt_object_remote(obj)) {
5372 ns->ln_linkea_repaired++;
5382 dt_write_unlock(env, obj);
5384 if (handle != NULL && !IS_ERR(handle))
5385 dt_trans_stop(env, dev, handle);
5388 lfsck_ibits_unlock(&lh, LCK_EX);
5390 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5391 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5392 lnr->lnr_name, lnr->lnr_namelen) &&
5393 type != LNIT_BAD_DIRENT) {
5394 ns->ln_flags |= LF_INCONSISTENT;
5398 dir = lfsck_assistant_object_load(env, lfsck, lso);
5402 GOTO(trace, rc == -ENOENT ? 0 : rc);
5406 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5407 lnr->lnr_lmv, lnr->lnr_name);
5413 if (type != LNIT_NONE && dir == NULL) {
5414 dir = lfsck_assistant_object_load(env, lfsck, lso);
5418 GOTO(trace, rc == -ENOENT ? 0 : rc);
5425 rc = lfsck_namespace_repair_dirent(env, com, dir,
5426 obj, lnr->lnr_name, lnr->lnr_name,
5427 lnr->lnr_type, true, false);
5431 case LNIT_BAD_DIRENT:
5433 /* XXX: This is a bad dirent, we do not know whether
5434 * the original name entry reference a regular
5435 * file or a directory, then keep the parent's
5436 * nlink count unchanged here. */
5437 rc = lfsck_namespace_repair_dirent(env, com, dir,
5438 obj, lnr->lnr_name, lnr->lnr_name,
5439 lnr->lnr_type, false, false);
5447 if (count == 1 && S_ISREG(lfsck_object_type(obj)))
5448 dt_attr_get(env, obj, la);
5452 down_write(&com->lc_sem);
5454 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5455 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5456 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid), PFID(pfid),
5457 lnr->lnr_namelen, lnr->lnr_name, rc);
5459 lfsck_namespace_record_failure(env, lfsck, ns);
5460 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5461 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5462 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5463 dev != NULL && dev != lfsck->li_bottom)
5464 lfsck_lad_set_bitmap(env, com, idx);
5466 if (!(bk->lb_param & LPF_FAILOUT))
5470 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5471 "repaired the entry: "DFID", parent "DFID
5472 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5473 PFID(&lnr->lnr_fid), PFID(pfid),
5474 lnr->lnr_namelen, lnr->lnr_name);
5477 ns->ln_items_repaired++;
5481 ns->ln_dangling_repaired++;
5484 ns->ln_bad_type_repaired++;
5486 case LNIT_BAD_DIRENT:
5487 ns->ln_dirent_repaired++;
5493 if (bk->lb_param & LPF_DRYRUN &&
5494 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5495 lfsck_pos_fill(env, lfsck,
5496 &ns->ln_pos_first_inconsistent,
5501 ns->ln_name_hash_repaired++;
5503 /* Not count repeatedly. */
5505 ns->ln_items_repaired++;
5507 if (bk->lb_param & LPF_DRYRUN &&
5508 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5509 lfsck_pos_fill(env, lfsck,
5510 &ns->ln_pos_first_inconsistent,
5517 if (count > 1 || la->la_nlink > 1)
5518 ns->ln_mul_linked_checked++;
5520 up_write(&com->lc_sem);
5522 if (obj != NULL && !IS_ERR(obj))
5523 lfsck_object_put(env, obj);
5525 if (dir != NULL && !IS_ERR(dir))
5526 lfsck_object_put(env, dir);
5528 lad->lad_advance_lock = bad_linkea;
5534 * Handle one orphan under the backend /lost+found directory
5536 * Insert the orphan FID into the namespace LFSCK trace file for further
5537 * processing (via the subsequent namespace LFSCK second-stage scanning).
5538 * At the same time, remove the orphan name entry from backend /lost+found
5539 * directory. There is an interval between the orphan name entry removed
5540 * from the backend /lost+found directory and the orphan FID in the LFSCK
5541 * trace file handled. In such interval, the LFSCK can be reset, then
5542 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5543 * To guarantee that the orphans can be found when LFSCK run next time
5544 * without e2fsck again, when remove the orphan name entry, the LFSCK
5545 * will set the orphan's ctime attribute as 1. Since normal applications
5546 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5547 * next time, it can record the object (that ctime is 1) in the namespace
5548 * LFSCK trace file during the first-stage scanning.
5550 * \param[in] env pointer to the thread context
5551 * \param[in] com pointer to the lfsck component
5552 * \param[in] parent pointer to the object for the backend /lost+found
5553 * \param[in] ent pointer to the name entry for the target under the
5554 * backend /lost+found
5556 * \retval positive for repaired
5557 * \retval 0 if needs to repair nothing
5558 * \retval negative error number on failure
5560 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5561 struct lfsck_component *com,
5562 struct dt_object *parent,
5563 struct lu_dirent *ent)
5565 struct lfsck_thread_info *info = lfsck_env_info(env);
5566 struct lu_fid *key = &info->lti_fid;
5567 struct lu_attr *la = &info->lti_la;
5568 struct lfsck_instance *lfsck = com->lc_lfsck;
5569 struct dt_object *obj;
5570 struct dt_device *dev = lfsck->li_bottom;
5571 struct dt_object *child = NULL;
5572 struct thandle *th = NULL;
5579 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5581 RETURN(PTR_ERR(child));
5583 LASSERT(dt_object_exists(child));
5584 LASSERT(!dt_object_remote(child));
5586 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5587 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5588 fid_cpu_to_be(key, &ent->lde_fid);
5589 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5590 (const struct dt_key *)key);
5593 flags |= LNTF_CHECK_ORPHAN;
5594 } else if (rc == -ENOENT) {
5595 flags = LNTF_CHECK_ORPHAN;
5600 th = dt_trans_create(env, dev);
5602 GOTO(out, rc = PTR_ERR(th));
5604 /* a1. remove name entry from backend /lost+found */
5605 rc = dt_declare_delete(env, parent,
5606 (const struct dt_key *)ent->lde_name, th);
5610 if (S_ISDIR(lfsck_object_type(child))) {
5611 /* a2. decrease parent's nlink */
5612 rc = dt_declare_ref_del(env, parent, th);
5618 /* a3. remove child's FID from the LFSCK trace file. */
5619 rc = dt_declare_delete(env, obj,
5620 (const struct dt_key *)key, th);
5624 /* a4. set child's ctime as 1 */
5625 memset(la, 0, sizeof(*la));
5627 la->la_valid = LA_CTIME;
5628 rc = dt_declare_attr_set(env, child, la, th);
5633 /* a5. insert child's FID into the LFSCK trace file. */
5634 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5635 (const struct dt_key *)key, th);
5639 rc = dt_trans_start_local(env, dev, th);
5643 /* b1. remove name entry from backend /lost+found */
5644 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th);
5648 if (S_ISDIR(lfsck_object_type(child))) {
5649 /* b2. decrease parent's nlink */
5650 dt_write_lock(env, parent, 0);
5651 rc = dt_ref_del(env, parent, th);
5652 dt_write_unlock(env, parent);
5658 /* a3. remove child's FID from the LFSCK trace file. */
5659 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
5663 /* b4. set child's ctime as 1 */
5664 rc = dt_attr_set(env, child, la, th);
5669 /* b5. insert child's FID into the LFSCK trace file. */
5670 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5671 (const struct dt_key *)key, th, 1);
5673 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5676 dt_trans_stop(env, dev, th);
5679 lfsck_object_put(env, child);
5685 * Handle orphans under the backend /lost+found directory
5687 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5688 * and put them under the backend /lost+found directory that is invisible
5689 * to client. The LFSCK will scan such directory, for the original client
5690 * visible orphans, add their fids into the namespace LFSCK trace file,
5691 * then the subsenquent namespace LFSCK second-stage scanning can handle
5692 * them as other objects to be double scanned: either move back to normal
5693 * namespace, or to the global visible orphan directory:
5694 * /ROOT/.lustre/lost+found/MDTxxxx/
5696 * \param[in] env pointer to the thread context
5697 * \param[in] com pointer to the lfsck component
5699 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5700 struct lfsck_component *com)
5702 struct lfsck_thread_info *info = lfsck_env_info(env);
5703 struct lu_dirent *ent =
5704 (struct lu_dirent *)info->lti_key;
5705 struct lu_seq_range *range = &info->lti_range;
5706 struct lfsck_instance *lfsck = com->lc_lfsck;
5707 struct ptlrpc_thread *thread = &lfsck->li_thread;
5708 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5709 struct lfsck_namespace *ns = com->lc_file_ram;
5710 struct dt_object *parent;
5711 const struct dt_it_ops *iops;
5713 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5715 __u32 idx = lfsck_dev_idx(lfsck);
5720 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5721 &LU_BACKEND_LPF_FID);
5722 if (IS_ERR(parent)) {
5723 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5724 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5728 /* It is normal that the /lost+found does not exist for ZFS backend. */
5729 if (!dt_object_exists(parent))
5732 if (unlikely(!dt_try_as_dir(env, parent)))
5733 GOTO(out, rc = -ENOTDIR);
5735 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5736 lfsck_lfsck2name(lfsck));
5738 com->lc_new_scanned = 0;
5739 iops = &parent->do_index_ops->dio_it;
5740 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE);
5742 GOTO(out, rc = PTR_ERR(di));
5744 rc = iops->load(env, di, 0);
5746 rc = iops->next(env, di);
5751 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5752 unlikely(!thread_is_running(thread)))
5755 rc = iops->rec(env, di, (struct dt_rec *)ent,
5756 LUDA_64BITHASH | LUDA_TYPE);
5758 rc = lfsck_unpack_ent(ent, &cookie, &type);
5760 if (unlikely(rc != 0)) {
5761 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5762 "/lost+found: rc = %d\n",
5763 lfsck_lfsck2name(lfsck), rc);
5768 /* skip dot and dotdot entries */
5769 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5772 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5775 if (fid_is_norm(&ent->lde_fid)) {
5776 fld_range_set_mdt(range);
5777 rc = fld_local_lookup(env, ss->ss_server_fld,
5778 fid_seq(&ent->lde_fid), range);
5781 } else if (idx != 0) {
5782 /* If the returned FID is IGIF, then there are three
5785 * 1) The object is upgraded from old Lustre-1.8 with
5786 * IGIF assigned to such object.
5787 * 2) The object is a backend local object and is
5788 * invisible to client.
5789 * 3) The object lost its LMV EA, and since there is
5790 * no FID-in-dirent for the orphan in the backend
5791 * /lost+found directory, then the low layer will
5792 * return IGIF for such object.
5794 * For MDTx (x != 0), it is either case 2) or case 3),
5795 * but from the LFSCK view, they are indistinguishable.
5796 * To be safe, the LFSCK will keep it there and report
5797 * some message, then the adminstrator can handle that
5800 * For MDT0, it is more possible the case 1). The LFSCK
5801 * will handle the orphan as an upgraded object. */
5802 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5803 "in the backend /lost+found on the MDT %04x, "
5804 "to be safe, skip it.\n",
5805 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5806 ent->lde_name, PFID(&ent->lde_fid), idx);
5810 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5813 down_write(&com->lc_sem);
5814 com->lc_new_scanned++;
5815 ns->ln_local_lpf_scanned++;
5817 ns->ln_local_lpf_moved++;
5819 ns->ln_local_lpf_skipped++;
5821 ns->ln_local_lpf_failed++;
5822 up_write(&com->lc_sem);
5824 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5828 lfsck_control_speed_by_self(com);
5829 if (unlikely(!thread_is_running(thread))) {
5834 rc = iops->next(env, di);
5838 iops->fini(env, di);
5843 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5844 lfsck_lfsck2name(lfsck), rc);
5846 lfsck_object_put(env, parent);
5850 * Rescan the striped directory after the master LMV EA reset.
5852 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5853 * the namespace LFSCK engine scan the striped directory for the first time,
5854 * it will be regarded as a normal directory. As the LFSCK processing, some
5855 * other LFSCK instance on other MDT will find the shard of this striped dir,
5856 * and find that the master MDT-object of the striped directory lost its LMV
5857 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5858 * notify the LFSCK instance on this MDT to rescan the striped directory.
5860 * \param[in] env pointer to the thread context
5861 * \param[in] com pointer to the lfsck component
5862 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5865 * \retval positive number for success
5866 * \retval 0 for LFSCK stopped/paused
5867 * \retval negative error number on failure
5869 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5870 struct lfsck_component *com,
5871 struct lfsck_lmv_unit *llu)
5873 struct lfsck_thread_info *info = lfsck_env_info(env);
5874 struct lfsck_instance *lfsck = com->lc_lfsck;
5875 struct lfsck_assistant_data *lad = com->lc_data;
5876 struct dt_object *dir;
5877 const struct dt_it_ops *iops;
5879 struct lu_dirent *ent =
5880 (struct lu_dirent *)info->lti_key;
5881 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5882 struct ptlrpc_thread *thread = &lfsck->li_thread;
5883 struct lfsck_assistant_object *lso = NULL;
5884 struct lfsck_namespace_req *lnr;
5885 struct lfsck_assistant_req *lar;
5890 LASSERT(list_empty(&lad->lad_req_list));
5892 lfsck->li_lmv = &llu->llu_lmv;
5893 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5894 rc = lfsck_open_dir(env, lfsck, 0);
5898 dir = lfsck->li_obj_dir;
5899 di = lfsck->li_di_dir;
5900 iops = &dir->do_index_ops->dio_it;
5902 rc = iops->rec(env, di, (struct dt_rec *)ent,
5903 lfsck->li_args_dir);
5905 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5909 if (bk->lb_param & LPF_FAILOUT)
5915 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5919 lso = lfsck_assistant_object_init(env,
5920 lfsck_dto2fid(dir), NULL,
5921 lfsck->li_pos_current.lp_oit_cookie, true);
5923 if (bk->lb_param & LPF_FAILOUT)
5924 GOTO(out, rc = PTR_ERR(lso));
5931 lnr = lfsck_namespace_assistant_req_init(lfsck, lso, ent, type);
5933 if (bk->lb_param & LPF_FAILOUT)
5934 GOTO(out, rc = PTR_ERR(lnr));
5939 lar = &lnr->lnr_lar;
5940 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5941 lfsck_namespace_assistant_req_fini(env, lar);
5942 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5945 if (unlikely(!thread_is_running(thread)))
5949 rc = iops->next(env, di);
5953 if (lso != NULL && !IS_ERR(lso))
5954 lfsck_assistant_object_put(env, lso);
5956 lfsck_close_dir(env, lfsck, rc);
5960 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5961 if (list_empty(&lad->lad_req_list))
5964 spin_lock(&lad->lad_lock);
5965 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5967 list_del_init(&lar->lar_list);
5968 spin_unlock(&lad->lad_lock);
5970 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5971 lfsck_namespace_assistant_req_fini(env, lar);
5973 RETURN(rc == 0 ? 1 : rc);
5977 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
5978 struct lfsck_component *com,
5979 struct dt_object *obj, bool first)
5981 struct lfsck_instance *lfsck = com->lc_lfsck;
5982 struct ptlrpc_thread *thread = &lfsck->li_thread;
5983 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5984 struct lfsck_namespace *ns = com->lc_file_ram;
5985 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5986 struct dt_object *target;
5994 di = iops->init(env, obj, 0);
5996 RETURN(PTR_ERR(di));
5999 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
6002 rc = iops->get(env, di, (const struct dt_key *)&fid);
6007 /* The start one either has been processed or does not exist,
6009 rc = iops->next(env, di);
6015 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6016 unlikely(!thread_is_running(thread)))
6019 key = iops->key(env, di);
6028 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
6029 if (!fid_is_sane(&fid)) {
6034 target = lfsck_object_find_bottom(env, lfsck, &fid);
6035 if (IS_ERR(target)) {
6036 rc = PTR_ERR(target);
6040 if (dt_object_exists(target)) {
6041 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
6043 rc = lfsck_namespace_double_scan_one(env, com,
6050 lfsck_object_put(env, target);
6053 down_write(&com->lc_sem);
6054 com->lc_new_checked++;
6055 com->lc_new_scanned++;
6056 if (rc >= 0 && fid_is_sane(&fid))
6057 ns->ln_fid_latest_scanned_phase2 = fid;
6059 ns->ln_objs_repaired_phase2++;
6061 ns->ln_objs_failed_phase2++;
6062 up_write(&com->lc_sem);
6064 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6067 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
6068 cfs_time_current())) &&
6069 com->lc_new_checked != 0) {
6070 down_write(&com->lc_sem);
6071 ns->ln_run_time_phase2 +=
6072 cfs_duration_sec(cfs_time_current() +
6073 HALF_SEC - com->lc_time_last_checkpoint);
6074 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6075 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6076 com->lc_new_checked = 0;
6077 rc = lfsck_namespace_store(env, com);
6078 up_write(&com->lc_sem);
6082 com->lc_time_last_checkpoint = cfs_time_current();
6083 com->lc_time_next_checkpoint =
6084 com->lc_time_last_checkpoint +
6085 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6088 lfsck_control_speed_by_self(com);
6089 if (unlikely(!thread_is_running(thread)))
6092 rc = iops->next(env, di);
6101 iops->fini(env, di);
6106 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
6107 struct lfsck_component *com)
6109 struct lfsck_instance *lfsck = com->lc_lfsck;
6110 struct lfsck_namespace *ns = com->lc_file_ram;
6115 while (!list_empty(&lfsck->li_list_lmv)) {
6116 struct lfsck_lmv_unit *llu;
6118 spin_lock(&lfsck->li_lock);
6119 llu = list_entry(lfsck->li_list_lmv.next,
6120 struct lfsck_lmv_unit, llu_link);
6121 list_del_init(&llu->llu_link);
6122 spin_unlock(&lfsck->li_lock);
6124 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6129 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6130 lfsck_lfsck2name(lfsck));
6132 lfsck_namespace_scan_local_lpf(env, com);
6134 com->lc_new_checked = 0;
6135 com->lc_new_scanned = 0;
6136 com->lc_time_last_checkpoint = cfs_time_current();
6137 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6138 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6140 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6141 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6142 com->lc_sub_trace_objs[i].lsto_obj, true);
6143 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6144 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6145 com->lc_sub_trace_objs[i].lsto_obj, false);
6147 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6148 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6153 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6154 struct lfsck_component *com,
6155 struct lfsck_position *pos)
6157 struct lfsck_assistant_data *lad = com->lc_data;
6158 struct lfsck_namespace_req *lnr;
6160 if (list_empty(&lad->lad_req_list))
6163 lnr = list_entry(lad->lad_req_list.next,
6164 struct lfsck_namespace_req,
6166 pos->lp_oit_cookie = lnr->lnr_lar.lar_parent->lso_oit_cookie;
6167 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6168 pos->lp_dir_parent = lnr->lnr_lar.lar_parent->lso_fid;
6171 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6172 struct lfsck_component *com,
6175 struct lfsck_instance *lfsck = com->lc_lfsck;
6176 struct lfsck_namespace *ns = com->lc_file_ram;
6178 down_write(&com->lc_sem);
6179 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6180 HALF_SEC - com->lc_time_last_checkpoint);
6181 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6182 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6183 com->lc_new_checked = 0;
6186 if (ns->ln_flags & LF_INCOMPLETE)
6187 ns->ln_status = LS_PARTIAL;
6189 ns->ln_status = LS_COMPLETED;
6190 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6191 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6192 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6193 ns->ln_success_count++;
6194 } else if (rc == 0) {
6195 if (lfsck->li_status != 0)
6196 ns->ln_status = lfsck->li_status;
6198 ns->ln_status = LS_STOPPED;
6200 ns->ln_status = LS_FAILED;
6203 rc = lfsck_namespace_store(env, com);
6204 up_write(&com->lc_sem);
6210 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6211 struct ptlrpc_request *req,
6215 struct lfsck_async_interpret_args *laia = args;
6216 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6218 ltd->ltd_synced_failures = 1;
6225 * Notify remote LFSCK instances about former failures.
6227 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6228 * some LFSCK verification requests (maybe because of network issues or the MDT
6229 * itself trouble). During the respond gap the MDT may missed some name entries
6230 * verification, then the MDT cannot know whether related MDT-objects have been
6231 * referenced by related name entries or not, then in the second-stage scanning,
6232 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6233 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6234 * name entry for repairing the orphan.
6236 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6237 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6238 * have ever missed some name entries verification and should skip the handling
6239 * for orphan MDT-objects.
6241 * \param[in] env pointer to the thread context
6242 * \param[in] com pointer to the lfsck component
6243 * \param[in] lr pointer to the lfsck request
6245 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6246 struct lfsck_component *com,
6247 struct lfsck_request *lr)
6249 struct lfsck_async_interpret_args *laia =
6250 &lfsck_env_info(env)->lti_laia2;
6251 struct lfsck_assistant_data *lad = com->lc_data;
6252 struct lfsck_namespace *ns = com->lc_file_ram;
6253 struct lfsck_instance *lfsck = com->lc_lfsck;
6254 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6255 struct lfsck_tgt_desc *ltd;
6256 struct ptlrpc_request_set *set;
6261 if (!lad->lad_incomplete)
6264 set = ptlrpc_prep_set();
6266 GOTO(out, rc = -ENOMEM);
6268 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6269 memset(laia, 0, sizeof(*laia));
6270 lad->lad_touch_gen++;
6272 down_read(<ds->ltd_rw_sem);
6273 cfs_foreach_bit(lad->lad_bitmap, idx) {
6274 ltd = lfsck_ltd2tgt(ltds, idx);
6275 LASSERT(ltd != NULL);
6277 laia->laia_ltd = ltd;
6278 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6279 lfsck_namespace_assistant_sync_failures_interpret,
6280 laia, LFSCK_NOTIFY);
6282 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6283 "to sync failure with MDT %x: rc = %d\n",
6284 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6286 up_read(<ds->ltd_rw_sem);
6288 rc = ptlrpc_set_wait(set);
6289 ptlrpc_set_destroy(set);
6295 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6296 "to sync failure with MDTs, and related MDTs "
6297 "may handle orphan improperly: rc = %d\n",
6298 lfsck_lfsck2name(lfsck), rc);
6303 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6304 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6305 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6306 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6307 .la_double_scan_result = lfsck_namespace_double_scan_result,
6308 .la_req_fini = lfsck_namespace_assistant_req_fini,
6309 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6313 * Verify the specified linkEA entry for the given directory object.
6314 * If the object has no such linkEA entry or it has more other linkEA
6315 * entries, then re-generate the linkEA with the given information.
6317 * \param[in] env pointer to the thread context
6318 * \param[in] obj pointer to the dt_object to be handled
6319 * \param[in] cname the name for the child in the parent directory
6320 * \param[in] pfid the parent directory's FID for the linkEA
6322 * \retval 0 for success
6323 * \retval negative error number on failure
6325 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6326 const struct lu_name *cname, const struct lu_fid *pfid)
6328 struct dt_device *dev = lfsck_obj2dev(obj);
6329 struct linkea_data ldata = { NULL };
6330 struct lu_buf linkea_buf;
6333 int fl = LU_XATTR_CREATE;
6337 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6339 rc = lfsck_links_read(env, obj, &ldata);
6340 if (rc == -ENODATA) {
6342 } else if (rc == 0) {
6343 fl = LU_XATTR_REPLACE;
6344 if (ldata.ld_leh->leh_reccount != 1) {
6347 rc = linkea_links_find(&ldata, cname, pfid);
6356 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6360 rc = linkea_add_buf(&ldata, cname, pfid);
6364 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6365 ldata.ld_leh->leh_len);
6366 th = dt_trans_create(env, dev);
6368 RETURN(PTR_ERR(th));
6370 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6371 XATTR_NAME_LINK, fl, th);
6375 rc = dt_trans_start_local(env, dev, th);
6379 dt_write_lock(env, obj, 0);
6380 rc = dt_xattr_set(env, obj, &linkea_buf,
6381 XATTR_NAME_LINK, fl, th);
6382 dt_write_unlock(env, obj);
6387 dt_trans_stop(env, dev, th);
6392 * Get the name and parent directory's FID from the first linkEA entry.
6394 * \param[in] env pointer to the thread context
6395 * \param[in] obj pointer to the object which get linkEA from
6396 * \param[out] name pointer to the buffer to hold the name
6397 * in the first linkEA entry
6398 * \param[out] pfid pointer to the buffer to hold the parent
6399 * directory's FID in the first linkEA entry
6401 * \retval 0 for success
6402 * \retval negative error number on failure
6404 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6405 char *name, struct lu_fid *pfid)
6407 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6408 struct linkea_data ldata = { NULL };
6411 rc = lfsck_links_read(env, obj, &ldata);
6415 linkea_first_entry(&ldata);
6416 if (ldata.ld_lee == NULL)
6419 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6420 /* To guarantee the 'name' is terminated with '0'. */
6421 memcpy(name, cname->ln_name, cname->ln_namelen);
6422 name[cname->ln_namelen] = 0;
6428 * Update the object's name entry with the given FID.
6430 * \param[in] env pointer to the thread context
6431 * \param[in] lfsck pointer to the lfsck instance
6432 * \param[in] dir pointer to the directory that holds
6434 * \param[in] name the name for the entry to be updated
6435 * \param[in] fid the new FID for the name entry referenced
6436 * \param[in] type the type for the name entry to be updated
6438 * \retval 0 for success
6439 * \retval negative error number on failure
6441 int lfsck_update_name_entry(const struct lu_env *env,
6442 struct lfsck_instance *lfsck,
6443 struct dt_object *dir, const char *name,
6444 const struct lu_fid *fid, __u32 type)
6446 struct lfsck_thread_info *info = lfsck_env_info(env);
6447 struct dt_insert_rec *rec = &info->lti_dt_rec;
6448 struct lfsck_lock_handle *llh = &info->lti_llh;
6449 struct dt_device *dev = lfsck_obj2dev(dir);
6455 rc = lfsck_lock(env, lfsck, dir, name, llh,
6456 MDS_INODELOCK_UPDATE, LCK_PW);
6460 th = dt_trans_create(env, dev);
6462 GOTO(unlock, rc = PTR_ERR(th));
6464 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6468 rec->rec_type = type;
6470 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6471 (const struct dt_key *)name, th);
6475 rc = dt_declare_ref_add(env, dir, th);
6479 rc = dt_trans_start_local(env, dev, th);
6483 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
6484 if (rc == -ENOENT) {
6492 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6493 (const struct dt_key *)name, th, 1);
6494 if (rc == 0 && S_ISDIR(type) && !exists) {
6495 dt_write_lock(env, dir, 0);
6496 rc = dt_ref_add(env, dir, th);
6497 dt_write_unlock(env, dir);
6503 dt_trans_stop(env, dev, th);
6507 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6508 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6509 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6514 int lfsck_namespace_setup(const struct lu_env *env,
6515 struct lfsck_instance *lfsck)
6517 struct lfsck_component *com;
6518 struct lfsck_namespace *ns;
6519 struct dt_object *root = NULL;
6520 struct dt_object *obj;
6525 LASSERT(lfsck->li_master);
6531 INIT_LIST_HEAD(&com->lc_link);
6532 INIT_LIST_HEAD(&com->lc_link_dir);
6533 init_rwsem(&com->lc_sem);
6534 atomic_set(&com->lc_ref, 1);
6535 com->lc_lfsck = lfsck;
6536 com->lc_type = LFSCK_TYPE_NAMESPACE;
6537 com->lc_ops = &lfsck_namespace_ops;
6538 com->lc_data = lfsck_assistant_data_init(
6539 &lfsck_namespace_assistant_ops,
6541 if (com->lc_data == NULL)
6542 GOTO(out, rc = -ENOMEM);
6544 com->lc_file_size = sizeof(struct lfsck_namespace);
6545 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6546 if (com->lc_file_ram == NULL)
6547 GOTO(out, rc = -ENOMEM);
6549 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6550 if (com->lc_file_disk == NULL)
6551 GOTO(out, rc = -ENOMEM);
6553 for (i = 0; i < LFSCK_STF_COUNT; i++)
6554 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6556 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6558 GOTO(out, rc = PTR_ERR(root));
6560 if (unlikely(!dt_try_as_dir(env, root)))
6561 GOTO(out, rc = -ENOTDIR);
6563 obj = local_index_find_or_create(env, lfsck->li_los, root,
6565 S_IFREG | S_IRUGO | S_IWUSR,
6566 &dt_lfsck_features);
6568 GOTO(out, rc = PTR_ERR(obj));
6571 rc = lfsck_namespace_load(env, com);
6573 rc = lfsck_namespace_init(env, com);
6575 rc = lfsck_namespace_reset(env, com, true);
6577 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6581 ns = com->lc_file_ram;
6582 switch (ns->ln_status) {
6587 spin_lock(&lfsck->li_lock);
6588 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6589 spin_unlock(&lfsck->li_lock);
6592 CERROR("%s: unknown lfsck_namespace status %d\n",
6593 lfsck_lfsck2name(lfsck), ns->ln_status);
6595 case LS_SCANNING_PHASE1:
6596 case LS_SCANNING_PHASE2:
6597 /* No need to store the status to disk right now.
6598 * If the system crashed before the status stored,
6599 * it will be loaded back when next time. */
6600 ns->ln_status = LS_CRASHED;
6604 spin_lock(&lfsck->li_lock);
6605 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6606 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6607 spin_unlock(&lfsck->li_lock);
6614 if (root != NULL && !IS_ERR(root))
6615 lfsck_object_put(env, root);
6617 lfsck_component_cleanup(env, com);
6618 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6619 lfsck_lfsck2name(lfsck), rc);