4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, 2016, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
42 #include "lfsck_internal.h"
44 #define LFSCK_NAMESPACE_MAGIC_V1 0xA0629D03
45 #define LFSCK_NAMESPACE_MAGIC_V2 0xA0621A0B
47 /* For Lustre-2.x (x <= 6), the namespace LFSCK used LFSCK_NAMESPACE_MAGIC_V1
48 * as the trace file magic. When downgrade to such old release, the old LFSCK
49 * will not recognize the new LFSCK_NAMESPACE_MAGIC_V2 in the new trace file,
50 * then it will reset the whole LFSCK, and will not cause start failure. The
51 * similar case will happen when upgrade from such old release. */
52 #define LFSCK_NAMESPACE_MAGIC LFSCK_NAMESPACE_MAGIC_V2
54 enum lfsck_nameentry_check {
55 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
56 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
57 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
60 static struct lfsck_namespace_req *
61 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
62 struct lfsck_assistant_object *lso,
63 struct lu_dirent *ent, __u16 type)
65 struct lfsck_namespace_req *lnr;
68 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
71 return ERR_PTR(-ENOMEM);
73 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
74 lnr->lnr_lar.lar_parent = lfsck_assistant_object_get(lso);
75 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
76 lnr->lnr_fid = ent->lde_fid;
77 lnr->lnr_dir_cookie = ent->lde_hash;
78 lnr->lnr_attr = ent->lde_attrs;
81 lnr->lnr_namelen = ent->lde_namelen;
82 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
87 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
88 struct lfsck_assistant_req *lar)
90 struct lfsck_namespace_req *lnr =
91 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
93 if (lnr->lnr_lmv != NULL)
94 lfsck_lmv_put(env, lnr->lnr_lmv);
96 lfsck_assistant_object_put(env, lar->lar_parent);
97 OBD_FREE(lnr, lnr->lnr_size);
100 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
101 struct lfsck_namespace *src)
103 dst->ln_magic = le32_to_cpu(src->ln_magic);
104 dst->ln_status = le32_to_cpu(src->ln_status);
105 dst->ln_flags = le32_to_cpu(src->ln_flags);
106 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
107 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
108 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
109 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
110 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
111 dst->ln_time_last_checkpoint =
112 le64_to_cpu(src->ln_time_last_checkpoint);
113 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
114 &src->ln_pos_latest_start);
115 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
116 &src->ln_pos_last_checkpoint);
117 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
118 &src->ln_pos_first_inconsistent);
119 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
120 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
121 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
122 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
123 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
124 dst->ln_objs_repaired_phase2 =
125 le64_to_cpu(src->ln_objs_repaired_phase2);
126 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
127 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
128 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
129 &src->ln_fid_latest_scanned_phase2);
130 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
131 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
132 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
133 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
134 dst->ln_unknown_inconsistency =
135 le64_to_cpu(src->ln_unknown_inconsistency);
136 dst->ln_unmatched_pairs_repaired =
137 le64_to_cpu(src->ln_unmatched_pairs_repaired);
138 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
139 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
140 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
141 dst->ln_lost_dirent_repaired =
142 le64_to_cpu(src->ln_lost_dirent_repaired);
143 dst->ln_striped_dirs_scanned =
144 le64_to_cpu(src->ln_striped_dirs_scanned);
145 dst->ln_striped_dirs_repaired =
146 le64_to_cpu(src->ln_striped_dirs_repaired);
147 dst->ln_striped_dirs_failed =
148 le64_to_cpu(src->ln_striped_dirs_failed);
149 dst->ln_striped_dirs_disabled =
150 le64_to_cpu(src->ln_striped_dirs_disabled);
151 dst->ln_striped_dirs_skipped =
152 le64_to_cpu(src->ln_striped_dirs_skipped);
153 dst->ln_striped_shards_scanned =
154 le64_to_cpu(src->ln_striped_shards_scanned);
155 dst->ln_striped_shards_repaired =
156 le64_to_cpu(src->ln_striped_shards_repaired);
157 dst->ln_striped_shards_failed =
158 le64_to_cpu(src->ln_striped_shards_failed);
159 dst->ln_striped_shards_skipped =
160 le64_to_cpu(src->ln_striped_shards_skipped);
161 dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
162 dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
163 dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
164 dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
165 dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
166 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
167 dst->ln_time_latest_reset = le32_to_cpu(src->ln_time_latest_reset);
168 dst->ln_linkea_overflow_cleared =
169 le64_to_cpu(src->ln_linkea_overflow_cleared);
172 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
173 struct lfsck_namespace *src)
175 dst->ln_magic = cpu_to_le32(src->ln_magic);
176 dst->ln_status = cpu_to_le32(src->ln_status);
177 dst->ln_flags = cpu_to_le32(src->ln_flags);
178 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
179 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
180 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
181 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
182 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
183 dst->ln_time_last_checkpoint =
184 cpu_to_le64(src->ln_time_last_checkpoint);
185 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
186 &src->ln_pos_latest_start);
187 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
188 &src->ln_pos_last_checkpoint);
189 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
190 &src->ln_pos_first_inconsistent);
191 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
192 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
193 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
194 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
195 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
196 dst->ln_objs_repaired_phase2 =
197 cpu_to_le64(src->ln_objs_repaired_phase2);
198 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
199 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
200 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
201 &src->ln_fid_latest_scanned_phase2);
202 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
203 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
204 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
205 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
206 dst->ln_unknown_inconsistency =
207 cpu_to_le64(src->ln_unknown_inconsistency);
208 dst->ln_unmatched_pairs_repaired =
209 cpu_to_le64(src->ln_unmatched_pairs_repaired);
210 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
211 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
212 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
213 dst->ln_lost_dirent_repaired =
214 cpu_to_le64(src->ln_lost_dirent_repaired);
215 dst->ln_striped_dirs_scanned =
216 cpu_to_le64(src->ln_striped_dirs_scanned);
217 dst->ln_striped_dirs_repaired =
218 cpu_to_le64(src->ln_striped_dirs_repaired);
219 dst->ln_striped_dirs_failed =
220 cpu_to_le64(src->ln_striped_dirs_failed);
221 dst->ln_striped_dirs_disabled =
222 cpu_to_le64(src->ln_striped_dirs_disabled);
223 dst->ln_striped_dirs_skipped =
224 cpu_to_le64(src->ln_striped_dirs_skipped);
225 dst->ln_striped_shards_scanned =
226 cpu_to_le64(src->ln_striped_shards_scanned);
227 dst->ln_striped_shards_repaired =
228 cpu_to_le64(src->ln_striped_shards_repaired);
229 dst->ln_striped_shards_failed =
230 cpu_to_le64(src->ln_striped_shards_failed);
231 dst->ln_striped_shards_skipped =
232 cpu_to_le64(src->ln_striped_shards_skipped);
233 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
234 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
235 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
236 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
237 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
238 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
239 dst->ln_time_latest_reset = cpu_to_le32(src->ln_time_latest_reset);
240 dst->ln_linkea_overflow_cleared =
241 cpu_to_le64(src->ln_linkea_overflow_cleared);
244 static void lfsck_namespace_record_failure(const struct lu_env *env,
245 struct lfsck_instance *lfsck,
246 struct lfsck_namespace *ns)
248 struct lfsck_position pos;
250 ns->ln_items_failed++;
251 lfsck_pos_fill(env, lfsck, &pos, false);
252 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
253 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
254 ns->ln_pos_first_inconsistent = pos;
256 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
257 "inconsistency at the pos [%llu, "DFID", %#llx]\n",
258 lfsck_lfsck2name(lfsck),
259 ns->ln_pos_first_inconsistent.lp_oit_cookie,
260 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
261 ns->ln_pos_first_inconsistent.lp_dir_cookie);
266 * Load the MDT bitmap from the lfsck_namespace trace file.
268 * \param[in] env pointer to the thread context
269 * \param[in] com pointer to the lfsck component
271 * \retval 0 for success
272 * \retval negative error number on failure or data corruption
274 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
275 struct lfsck_component *com)
277 struct dt_object *obj = com->lc_obj;
278 struct lfsck_assistant_data *lad = com->lc_data;
279 struct lfsck_namespace *ns = com->lc_file_ram;
280 struct cfs_bitmap *bitmap = lad->lad_bitmap;
286 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
288 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
290 nbits = ns->ln_bitmap_size;
292 if (unlikely(nbits < BITS_PER_LONG))
293 nbits = BITS_PER_LONG;
295 if (nbits > bitmap->size) {
296 __u32 new_bits = bitmap->size;
297 struct cfs_bitmap *new_bitmap;
299 while (new_bits < nbits)
302 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
303 if (new_bitmap == NULL)
306 lad->lad_bitmap = new_bitmap;
307 CFS_FREE_BITMAP(bitmap);
311 if (ns->ln_bitmap_size == 0) {
312 lad->lad_incomplete = 0;
313 CFS_RESET_BITMAP(bitmap);
318 size = (ns->ln_bitmap_size + 7) >> 3;
319 rc = dt_xattr_get(env, obj,
320 lfsck_buf_get(env, bitmap->data, size),
321 XATTR_NAME_LFSCK_BITMAP);
323 RETURN(rc >= 0 ? -EINVAL : rc);
325 if (cfs_bitmap_check_empty(bitmap))
326 lad->lad_incomplete = 0;
328 lad->lad_incomplete = 1;
334 * Load namespace LFSCK statistics information from the trace file.
336 * \param[in] env pointer to the thread context
337 * \param[in] com pointer to the lfsck component
339 * \retval 0 for success
340 * \retval negative error number on failure
342 static int lfsck_namespace_load(const struct lu_env *env,
343 struct lfsck_component *com)
345 int len = com->lc_file_size;
348 rc = dt_xattr_get(env, com->lc_obj,
349 lfsck_buf_get(env, com->lc_file_disk, len),
350 XATTR_NAME_LFSCK_NAMESPACE);
352 struct lfsck_namespace *ns = com->lc_file_ram;
354 lfsck_namespace_le_to_cpu(ns,
355 (struct lfsck_namespace *)com->lc_file_disk);
356 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
357 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
358 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
359 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
364 } else if (rc != -ENODATA) {
365 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
366 "expected = %d: rc = %d\n",
367 lfsck_lfsck2name(com->lc_lfsck), len, rc);
375 static int lfsck_namespace_store(const struct lu_env *env,
376 struct lfsck_component *com)
378 struct dt_object *obj = com->lc_obj;
379 struct lfsck_instance *lfsck = com->lc_lfsck;
380 struct lfsck_namespace *ns = com->lc_file_ram;
381 struct lfsck_assistant_data *lad = com->lc_data;
382 struct dt_device *dev = lfsck_obj2dev(obj);
383 struct cfs_bitmap *bitmap = NULL;
384 struct thandle *handle;
386 int len = com->lc_file_size;
391 bitmap = lad->lad_bitmap;
392 nbits = bitmap->size;
395 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
398 ns->ln_bitmap_size = nbits;
399 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
401 handle = dt_trans_create(env, dev);
403 GOTO(log, rc = PTR_ERR(handle));
405 rc = dt_declare_xattr_set(env, obj,
406 lfsck_buf_get(env, com->lc_file_disk, len),
407 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
411 if (bitmap != NULL) {
412 rc = dt_declare_xattr_set(env, obj,
413 lfsck_buf_get(env, bitmap->data, nbits >> 3),
414 XATTR_NAME_LFSCK_BITMAP, 0, handle);
419 rc = dt_trans_start_local(env, dev, handle);
423 rc = dt_xattr_set(env, obj,
424 lfsck_buf_get(env, com->lc_file_disk, len),
425 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
426 if (rc == 0 && bitmap != NULL)
427 rc = dt_xattr_set(env, obj,
428 lfsck_buf_get(env, bitmap->data, nbits >> 3),
429 XATTR_NAME_LFSCK_BITMAP, 0, handle);
434 dt_trans_stop(env, dev, handle);
438 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
439 lfsck_lfsck2name(lfsck), rc);
443 static int lfsck_namespace_init(const struct lu_env *env,
444 struct lfsck_component *com)
446 struct lfsck_namespace *ns = com->lc_file_ram;
449 memset(ns, 0, sizeof(*ns));
450 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
451 ns->ln_status = LS_INIT;
452 ns->ln_time_latest_reset = cfs_time_current_sec();
453 down_write(&com->lc_sem);
454 rc = lfsck_namespace_store(env, com);
456 rc = lfsck_load_sub_trace_files(env, com,
457 &dt_lfsck_namespace_features, LFSCK_NAMESPACE, true);
458 up_write(&com->lc_sem);
464 * Update the namespace LFSCK trace file for the given @fid
466 * \param[in] env pointer to the thread context
467 * \param[in] com pointer to the lfsck component
468 * \param[in] fid the fid which flags to be updated in the lfsck
470 * \param[in] add true if add new flags, otherwise remove flags
472 * \retval 0 for success or nothing to be done
473 * \retval negative error number on failure
475 int lfsck_namespace_trace_update(const struct lu_env *env,
476 struct lfsck_component *com,
477 const struct lu_fid *fid,
478 const __u8 flags, bool add)
480 struct lfsck_instance *lfsck = com->lc_lfsck;
481 struct dt_object *obj;
482 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
483 struct dt_device *dev;
484 struct thandle *th = NULL;
493 if (unlikely(!fid_is_sane(fid)))
496 idx = lfsck_sub_trace_file_fid2idx(fid);
497 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
498 obj = com->lc_sub_trace_objs[idx].lsto_obj;
499 if (unlikely(obj == NULL)) {
500 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
504 lfsck_object_get(obj);
505 dev = lfsck_obj2dev(obj);
506 fid_cpu_to_be(key, fid);
507 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
508 (const struct dt_key *)key);
511 GOTO(unlock, rc = 0);
515 } else if (rc == 0) {
517 if ((old & flags) == flags)
518 GOTO(unlock, rc = 0);
522 if ((old & flags) == 0)
523 GOTO(unlock, rc = 0);
531 th = dt_trans_create(env, dev);
533 GOTO(log, rc = PTR_ERR(th));
536 rc = dt_declare_delete(env, obj,
537 (const struct dt_key *)key, th);
543 rc = dt_declare_insert(env, obj,
544 (const struct dt_rec *)&new,
545 (const struct dt_key *)key, th);
550 rc = dt_trans_start_local(env, dev, th);
555 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
561 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
562 (const struct dt_key *)key, th, 1);
570 if (th != NULL && !IS_ERR(th))
571 dt_trans_stop(env, dev, th);
573 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
574 "trace file, flags %x, old %x, new %x: rc = %d\n",
575 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
576 (__u32)flags, (__u32)old, (__u32)new, rc);
579 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
580 lfsck_object_put(env, obj);
585 int lfsck_namespace_check_exist(const struct lu_env *env,
586 struct dt_object *dir,
587 struct dt_object *obj, const char *name)
589 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
593 if (unlikely(lfsck_is_dead_obj(obj)))
594 RETURN(LFSCK_NAMEENTRY_DEAD);
596 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
597 (const struct dt_key *)name);
599 RETURN(LFSCK_NAMEENTRY_REMOVED);
604 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
605 RETURN(LFSCK_NAMEENTRY_RECREATED);
610 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
611 struct dt_object *obj,
612 struct thandle *handle)
616 /* For remote updating LINKEA, there may be further LFSCK action
617 * on remote MDT after the updating, so update the LINKEA ASAP. */
618 if (dt_object_remote(obj))
621 /* For destroying all invalid linkEA entries. */
622 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
624 /* For insert new linkEA entry. */
625 rc = dt_declare_xattr_set(env, obj,
626 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
627 XATTR_NAME_LINK, 0, handle);
631 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
632 struct linkea_data *ldata, bool with_rec)
636 if (ldata->ld_buf->lb_buf == NULL)
639 if (!dt_object_exists(obj))
642 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
644 /* Buf was too small, figure out what we need. */
645 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
646 if (unlikely(rc == 0))
652 lu_buf_realloc(ldata->ld_buf, rc);
653 if (ldata->ld_buf->lb_buf == NULL)
656 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
659 if (unlikely(rc == 0))
664 rc = linkea_init_with_rec(ldata);
666 rc = linkea_init(ldata);
673 * Remove linkEA for the given object.
675 * The caller should take the ldlm lock before the calling.
677 * \param[in] env pointer to the thread context
678 * \param[in] com pointer to the lfsck component
679 * \param[in] obj pointer to the dt_object to be handled
681 * \retval 0 for repaired cases
682 * \retval negative error number on failure
684 static int lfsck_namespace_links_remove(const struct lu_env *env,
685 struct lfsck_component *com,
686 struct dt_object *obj)
688 struct lfsck_instance *lfsck = com->lc_lfsck;
689 struct dt_device *dev = lfsck_obj2dev(obj);
690 struct thandle *th = NULL;
694 LASSERT(dt_object_remote(obj) == 0);
696 th = dt_trans_create(env, dev);
698 GOTO(log, rc = PTR_ERR(th));
700 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
704 rc = dt_trans_start_local(env, dev, th);
708 dt_write_lock(env, obj, 0);
709 if (unlikely(lfsck_is_dead_obj(obj)))
710 GOTO(unlock, rc = -ENOENT);
712 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
713 GOTO(unlock, rc = 0);
715 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
720 dt_write_unlock(env, obj);
723 dt_trans_stop(env, dev, th);
726 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
727 "for the object "DFID": rc = %d\n",
728 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
731 struct lfsck_namespace *ns = com->lc_file_ram;
733 ns->ln_flags |= LF_INCONSISTENT;
739 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
740 struct linkea_data *ldata, struct thandle *handle)
745 lfsck_buf_init(&buf, ldata->ld_buf->lb_buf, ldata->ld_leh->leh_len);
748 rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LINK, 0, handle);
749 if (unlikely(rc == -ENOSPC)) {
750 rc = linkea_overflow_shrink(ldata);
751 if (likely(rc > 0)) {
760 static int lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
761 struct lu_name *cname,
763 char *buf, const int buflen)
765 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
766 if (unlikely(ldata->ld_reclen <= 0 ||
767 ldata->ld_reclen + sizeof(struct link_ea_header) >
768 ldata->ld_leh->leh_len ||
769 cname->ln_namelen <= 0 ||
770 cname->ln_namelen > NAME_MAX ||
771 cname->ln_namelen >= buflen ||
775 /* To guarantee the 'name' is terminated with '0'. */
776 memcpy(buf, cname->ln_name, cname->ln_namelen);
777 buf[cname->ln_namelen] = 0;
778 cname->ln_name = buf;
783 static void lfsck_linkea_del_buf(struct linkea_data *ldata,
784 const struct lu_name *lname)
786 LASSERT(ldata->ld_leh != NULL && ldata->ld_lee != NULL);
788 /* If current record is corrupted, all the subsequent
789 * records will be dropped. */
790 if (unlikely(ldata->ld_reclen <= 0 ||
791 ldata->ld_reclen + sizeof(struct link_ea_header) >
792 ldata->ld_leh->leh_len)) {
793 void *ptr = ldata->ld_lee;
795 ldata->ld_leh->leh_len = sizeof(struct link_ea_header);
796 ldata->ld_leh->leh_reccount = 0;
797 linkea_first_entry(ldata);
798 while (ldata->ld_lee != NULL &&
799 (char *)ldata->ld_lee < (char *)ptr) {
800 int reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
801 ldata->ld_lee->lee_reclen[1];
803 ldata->ld_leh->leh_len += reclen;
804 ldata->ld_leh->leh_reccount++;
805 ldata->ld_lee = (struct link_ea_entry *)
806 ((char *)ldata->ld_lee + reclen);
809 ldata->ld_lee = NULL;
811 linkea_del_buf(ldata, lname);
815 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
816 struct lu_name *cname,
820 struct link_ea_entry *oldlee;
824 oldlee = ldata->ld_lee;
825 oldlen = ldata->ld_reclen;
826 linkea_next_entry(ldata);
827 while (ldata->ld_lee != NULL) {
828 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
829 ldata->ld_lee->lee_reclen[1];
830 if (unlikely(ldata->ld_reclen == oldlen &&
831 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
836 lfsck_linkea_del_buf(ldata, cname);
838 linkea_next_entry(ldata);
841 ldata->ld_lee = oldlee;
842 ldata->ld_reclen = oldlen;
848 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
850 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
851 * with the given type to generate the name, the detailed rules for name
852 * have been described as following.
854 * The function also generates the linkEA corresponding to the name entry
855 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
857 * \param[in] env pointer to the thread context
858 * \param[in] com pointer to the lfsck component
859 * \param[in] orphan pointer to the orphan MDT-object
860 * \param[in] infix additional information for the orphan name, such as
861 * the FID for original
862 * \param[in] type the type for describing why the orphan MDT-object is
863 * created. The rules are as following:
865 * type "D": The MDT-object is a directory, it may knows its parent
866 * but because there is no valid linkEA, the LFSCK cannot
867 * know where to put it back to the namespace.
868 * type "O": The MDT-object has no linkEA, and there is no name
869 * entry that references the MDT-object.
871 * type "S": The orphan MDT-object is a shard of a striped directory
873 * \see lfsck_layout_recreate_parent() for more types.
875 * The orphan name will be like:
876 * ${FID}-${infix}-${type}-${conflict_version}
878 * \param[out] count if some others inserted some linkEA entries by race,
879 * then return the linkEA entries count.
881 * \retval positive number for repaired cases
882 * \retval 0 if needs to repair nothing
883 * \retval negative error number on failure
885 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
886 struct lfsck_component *com,
887 struct dt_object *orphan,
888 const char *infix, const char *type,
891 struct lfsck_thread_info *info = lfsck_env_info(env);
892 struct lu_name *cname = &info->lti_name;
893 struct dt_insert_rec *rec = &info->lti_dt_rec;
894 struct lu_attr *la = &info->lti_la2;
895 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
896 const struct lu_fid *pfid;
898 struct lfsck_instance *lfsck = com->lc_lfsck;
899 struct dt_device *dev = lfsck_obj2dev(orphan);
900 struct dt_object *parent;
901 struct thandle *th = NULL;
902 struct lfsck_lock_handle *pllh = &info->lti_llh;
903 struct lustre_handle clh = { 0 };
904 struct linkea_data ldata2 = { NULL };
905 struct lu_buf linkea_buf;
912 cname->ln_name = NULL;
913 if (unlikely(lfsck->li_lpf_obj == NULL))
914 GOTO(log, rc = -ENXIO);
916 parent = lfsck->li_lpf_obj;
917 pfid = lfsck_dto2fid(parent);
921 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
922 PFID(cfid), infix, type, idx++);
923 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
924 (const struct dt_key *)info->lti_key);
925 if (rc != 0 && rc != -ENOENT)
928 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
930 } while (rc == 0 && !exist);
932 rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
933 MDS_INODELOCK_UPDATE, LCK_PW);
937 /* Re-check whether the name conflict with othrs after taken
939 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
940 (const struct dt_key *)info->lti_key);
942 if (!lu_fid_eq(cfid, &tfid)) {
949 } else if (rc != -ENOENT) {
955 cname->ln_name = info->lti_key;
956 cname->ln_namelen = namelen;
957 rc = linkea_links_new(&ldata2, &info->lti_linkea_buf2,
962 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
963 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
964 MDS_INODELOCK_XATTR, LCK_EX);
968 lfsck_buf_init(&linkea_buf, ldata2.ld_buf->lb_buf,
969 ldata2.ld_leh->leh_len);
970 th = dt_trans_create(env, dev);
972 GOTO(log, rc = PTR_ERR(th));
974 if (S_ISDIR(lfsck_object_type(orphan))) {
975 rc = dt_declare_delete(env, orphan,
976 (const struct dt_key *)dotdot, th);
980 rec->rec_type = S_IFDIR;
982 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
983 (const struct dt_key *)dotdot, th);
988 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
989 XATTR_NAME_LINK, 0, th);
994 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
996 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
997 (const struct dt_key *)cname->ln_name,
1002 if (S_ISDIR(rec->rec_type)) {
1003 rc = dt_declare_ref_add(env, parent, th);
1009 memset(la, 0, sizeof(*la));
1010 la->la_ctime = cfs_time_current_sec();
1011 la->la_valid = LA_CTIME;
1012 rc = dt_declare_attr_set(env, orphan, la, th);
1016 rc = dt_trans_start_local(env, dev, th);
1020 dt_write_lock(env, orphan, 0);
1021 rc = lfsck_links_read2_with_rec(env, orphan, &ldata2);
1022 if (likely(rc == -ENODATA || rc == -EINVAL)) {
1023 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1024 GOTO(unlock, rc = 1);
1026 if (S_ISDIR(lfsck_object_type(orphan))) {
1027 rc = dt_delete(env, orphan,
1028 (const struct dt_key *)dotdot, th);
1032 rec->rec_type = S_IFDIR;
1033 rec->rec_fid = pfid;
1034 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1035 (const struct dt_key *)dotdot, th, 1);
1040 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1043 if (rc == 0 && count != NULL)
1044 *count = ldata2.ld_leh->leh_reccount;
1048 dt_write_unlock(env, orphan);
1050 if (rc == 0 && !exist) {
1051 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1052 rec->rec_fid = cfid;
1053 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1054 (const struct dt_key *)cname->ln_name, th, 1);
1055 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1056 dt_write_lock(env, parent, 0);
1057 rc = dt_ref_add(env, parent, th);
1058 dt_write_unlock(env, parent);
1063 rc = dt_attr_set(env, orphan, la, th);
1065 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1068 dt_write_unlock(env, orphan);
1071 dt_trans_stop(env, dev, th);
1074 lfsck_ibits_unlock(&clh, LCK_EX);
1076 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1077 "object "DFID", name = %s: rc = %d\n",
1078 lfsck_lfsck2name(lfsck), PFID(cfid),
1079 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1082 struct lfsck_namespace *ns = com->lc_file_ram;
1084 ns->ln_flags |= LF_INCONSISTENT;
1091 * Add the specified name entry back to namespace.
1093 * If there is a linkEA entry that back references a name entry under
1094 * some parent directory, but such parent directory does not have the
1095 * claimed name entry. On the other hand, the linkEA entries count is
1096 * not larger than the MDT-object's hard link count. Under such case,
1097 * it is quite possible that the name entry is lost. Then the LFSCK
1098 * should add the name entry back to the namespace.
1100 * \param[in] env pointer to the thread context
1101 * \param[in] com pointer to the lfsck component
1102 * \param[in] parent pointer to the directory under which the name entry
1103 * will be inserted into
1104 * \param[in] child pointer to the object referenced by the name entry
1105 * that to be inserted into the parent
1106 * \param[in] name the name for the child in the parent directory
1108 * \retval positive number for repaired cases
1109 * \retval 0 if nothing to be repaired
1110 * \retval negative error number on failure
1112 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1113 struct lfsck_component *com,
1114 struct dt_object *parent,
1115 struct dt_object *child,
1118 struct lfsck_thread_info *info = lfsck_env_info(env);
1119 struct lu_attr *la = &info->lti_la;
1120 struct dt_insert_rec *rec = &info->lti_dt_rec;
1121 struct lfsck_instance *lfsck = com->lc_lfsck;
1122 /* The child and its name may be on different MDTs. */
1123 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1124 const struct lu_fid *cfid = lfsck_dto2fid(child);
1125 struct dt_device *dev = lfsck->li_next;
1126 struct thandle *th = NULL;
1127 struct lfsck_lock_handle *llh = &info->lti_llh;
1131 /* @parent/@child may be based on lfsck->li_bottom,
1132 * but here we need the object based on the lfsck->li_next. */
1134 parent = lfsck_object_locate(dev, parent);
1136 GOTO(log, rc = PTR_ERR(parent));
1138 if (unlikely(!dt_try_as_dir(env, parent)))
1139 GOTO(log, rc = -ENOTDIR);
1141 child = lfsck_object_locate(dev, child);
1143 GOTO(log, rc = PTR_ERR(child));
1145 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1148 rc = lfsck_lock(env, lfsck, parent, name, llh,
1149 MDS_INODELOCK_UPDATE, LCK_PW);
1153 th = dt_trans_create(env, dev);
1155 GOTO(unlock, rc = PTR_ERR(th));
1157 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1158 rec->rec_fid = cfid;
1159 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1160 (const struct dt_key *)name, th);
1164 if (S_ISDIR(rec->rec_type)) {
1165 rc = dt_declare_ref_add(env, parent, th);
1170 memset(la, 0, sizeof(*la));
1171 la->la_ctime = cfs_time_current_sec();
1172 la->la_valid = LA_CTIME;
1173 rc = dt_declare_attr_set(env, parent, la, th);
1177 rc = dt_declare_attr_set(env, child, la, th);
1181 rc = dt_trans_start_local(env, dev, th);
1185 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1186 (const struct dt_key *)name, th, 1);
1190 if (S_ISDIR(rec->rec_type)) {
1191 dt_write_lock(env, parent, 0);
1192 rc = dt_ref_add(env, parent, th);
1193 dt_write_unlock(env, parent);
1198 la->la_ctime = cfs_time_current_sec();
1199 rc = dt_attr_set(env, parent, la, th);
1203 rc = dt_attr_set(env, child, la, th);
1205 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1208 dt_trans_stop(env, dev, th);
1214 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1215 "the name %s and type %o to the parent "DFID": rc = %d\n",
1216 lfsck_lfsck2name(lfsck), PFID(cfid), name,
1217 lfsck_object_type(child) & S_IFMT, PFID(pfid), rc);
1220 struct lfsck_namespace *ns = com->lc_file_ram;
1222 ns->ln_flags |= LF_INCONSISTENT;
1224 ns->ln_lost_dirent_repaired++;
1231 * Create the specified orphan directory.
1233 * For the case that the parent MDT-object stored in some MDT-object's
1234 * linkEA entry is lost, the LFSCK will re-create the parent object as
1235 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1236 * with the name ${FID}-P-${conflict_version}.
1238 * \param[in] env pointer to the thread context
1239 * \param[in] com pointer to the lfsck component
1240 * \param[in] orphan pointer to the orphan MDT-object to be created
1241 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1243 * \retval positive number for repaired cases
1244 * \retval negative error number on failure
1246 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1247 struct lfsck_component *com,
1248 struct dt_object *orphan,
1249 struct lmv_mds_md_v1 *lmv)
1251 struct lfsck_thread_info *info = lfsck_env_info(env);
1252 struct lu_attr *la = &info->lti_la;
1253 struct dt_allocation_hint *hint = &info->lti_hint;
1254 struct dt_object_format *dof = &info->lti_dof;
1255 struct lu_name *cname = &info->lti_name2;
1256 struct dt_insert_rec *rec = &info->lti_dt_rec;
1257 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1258 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1260 struct lfsck_instance *lfsck = com->lc_lfsck;
1261 struct lfsck_namespace *ns = com->lc_file_ram;
1262 struct dt_device *dev = lfsck_obj2dev(orphan);
1263 struct dt_object *parent = NULL;
1264 struct thandle *th = NULL;
1265 struct lfsck_lock_handle *llh = &info->lti_llh;
1266 struct linkea_data ldata = { NULL };
1267 struct lu_buf linkea_buf;
1268 struct lu_buf lmv_buf;
1276 LASSERT(!dt_object_exists(orphan));
1278 cname->ln_name = NULL;
1279 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1282 if (dt_object_remote(orphan)) {
1283 LASSERT(lfsck->li_lpf_root_obj != NULL);
1285 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1287 GOTO(log, rc = idx);
1289 snprintf(name, 8, "MDT%04x", idx);
1290 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1291 (struct dt_rec *)&tfid,
1292 (const struct dt_key *)name);
1294 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1296 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1298 GOTO(log, rc = PTR_ERR(parent));
1300 if (unlikely(!dt_try_as_dir(env, parent)))
1301 GOTO(log, rc = -ENOTDIR);
1303 if (unlikely(lfsck->li_lpf_obj == NULL))
1304 GOTO(log, rc = -ENXIO);
1306 parent = lfsck->li_lpf_obj;
1309 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1311 GOTO(log, rc = PTR_ERR(dev));
1317 namelen = snprintf(name, 31, DFID"-P-%d",
1319 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1320 (const struct dt_key *)name);
1321 if (rc != 0 && rc != -ENOENT)
1325 rc = lfsck_lock(env, lfsck, parent, name, llh,
1326 MDS_INODELOCK_UPDATE, LCK_PW);
1330 /* Re-check whether the name conflict with othrs after taken
1332 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1333 (const struct dt_key *)name);
1334 if (unlikely(rc == 0)) {
1342 cname->ln_name = name;
1343 cname->ln_namelen = namelen;
1345 memset(la, 0, sizeof(*la));
1346 la->la_mode = S_IFDIR | 0700;
1347 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1348 LA_ATIME | LA_MTIME | LA_CTIME;
1350 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1351 la->la_mode & S_IFMT);
1353 memset(dof, 0, sizeof(*dof));
1354 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1356 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
1357 cname, lfsck_dto2fid(parent));
1361 th = dt_trans_create(env, dev);
1363 GOTO(unlock1, rc = PTR_ERR(th));
1365 /* Sync the remote transaction to guarantee that the subsequent
1366 * lock against the @orphan can find the @orphan in time. */
1367 if (dt_object_remote(orphan))
1370 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1374 if (unlikely(!dt_try_as_dir(env, orphan)))
1375 GOTO(stop, rc = -ENOTDIR);
1377 rc = dt_declare_ref_add(env, orphan, th);
1381 rec->rec_type = S_IFDIR;
1382 rec->rec_fid = cfid;
1383 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1384 (const struct dt_key *)dot, th);
1388 rec->rec_fid = lfsck_dto2fid(parent);
1389 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1390 (const struct dt_key *)dotdot, th);
1395 lmv->lmv_magic = LMV_MAGIC;
1396 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1397 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1398 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1399 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1400 XATTR_NAME_LMV, 0, th);
1405 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1406 ldata.ld_leh->leh_len);
1407 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1408 XATTR_NAME_LINK, 0, th);
1412 rec->rec_fid = cfid;
1413 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1414 (const struct dt_key *)name, th);
1416 rc = dt_declare_ref_add(env, parent, th);
1421 rc = dt_trans_start_local(env, dev, th);
1425 dt_write_lock(env, orphan, 0);
1426 rc = dt_create(env, orphan, la, hint, dof, th);
1430 rc = dt_ref_add(env, orphan, th);
1434 rec->rec_fid = cfid;
1435 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1436 (const struct dt_key *)dot, th, 1);
1440 rec->rec_fid = lfsck_dto2fid(parent);
1441 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1442 (const struct dt_key *)dotdot, th, 1);
1447 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1452 rc = dt_xattr_set(env, orphan, &linkea_buf,
1453 XATTR_NAME_LINK, 0, th);
1454 dt_write_unlock(env, orphan);
1458 rec->rec_fid = cfid;
1459 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1460 (const struct dt_key *)name, th, 1);
1462 dt_write_lock(env, parent, 0);
1463 rc = dt_ref_add(env, parent, th);
1464 dt_write_unlock(env, parent);
1467 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1470 dt_write_unlock(env, orphan);
1473 rc1 = dt_trans_stop(env, dev, th);
1474 if (rc1 != 0 && rc > 0)
1481 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1482 "the object "DFID", name = %s: rc = %d\n",
1483 lfsck_lfsck2name(lfsck), PFID(cfid),
1484 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1486 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1487 lfsck_object_put(env, parent);
1490 ns->ln_flags |= LF_INCONSISTENT;
1496 * Remove the specified entry from the linkEA.
1498 * Locate the linkEA entry with the given @cname and @pfid, then
1499 * remove this entry or the other entries those are repeated with
1502 * \param[in] env pointer to the thread context
1503 * \param[in] com pointer to the lfsck component
1504 * \param[in] obj pointer to the dt_object to be handled
1505 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1506 * \param[in] cname the name for the child in the parent directory
1507 * \param[in] pfid the parent directory's FID for the linkEA
1508 * \param[in] next if true, then remove the first found linkEA
1509 * entry, and move the ldata->ld_lee to next entry
1511 * \retval positive number for repaired cases
1512 * \retval 0 if nothing to be repaired
1513 * \retval negative error number on failure
1515 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1516 struct lfsck_component *com,
1517 struct dt_object *obj,
1518 struct linkea_data *ldata,
1519 struct lu_name *cname,
1520 struct lu_fid *pfid,
1523 struct lfsck_instance *lfsck = com->lc_lfsck;
1524 struct dt_device *dev = lfsck_obj2dev(obj);
1525 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1526 struct thandle *th = NULL;
1527 struct lustre_handle lh = { 0 };
1528 struct linkea_data ldata_new = { NULL };
1529 struct lu_buf linkea_buf;
1534 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1535 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1541 lfsck_linkea_del_buf(ldata, cname);
1543 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1545 if (ldata->ld_leh->leh_reccount > 0 ||
1546 unlikely(ldata->ld_leh->leh_overflow_time)) {
1547 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1548 ldata->ld_leh->leh_len);
1549 buflen = linkea_buf.lb_len;
1553 th = dt_trans_create(env, dev);
1555 GOTO(unlock1, rc = PTR_ERR(th));
1558 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1559 XATTR_NAME_LINK, 0, th);
1561 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
1565 rc = dt_trans_start_local(env, dev, th);
1569 dt_write_lock(env, obj, 0);
1570 if (unlikely(lfsck_is_dead_obj(obj)))
1571 GOTO(unlock2, rc = -ENOENT);
1573 rc = lfsck_links_read2_with_rec(env, obj, &ldata_new);
1575 GOTO(unlock2, rc = (rc == -ENODATA ? 0 : rc));
1577 /* The specified linkEA entry has been removed by race. */
1578 rc = linkea_links_find(&ldata_new, cname, pfid);
1580 GOTO(unlock2, rc = 0);
1582 if (bk->lb_param & LPF_DRYRUN)
1583 GOTO(unlock2, rc = 1);
1586 lfsck_linkea_del_buf(&ldata_new, cname);
1588 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1591 if (buflen < ldata_new.ld_leh->leh_len) {
1592 dt_write_unlock(env, obj);
1593 dt_trans_stop(env, dev, th);
1594 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1595 ldata_new.ld_leh->leh_len);
1599 if (ldata_new.ld_leh->leh_reccount > 0 ||
1600 unlikely(ldata->ld_leh->leh_overflow_time))
1601 rc = lfsck_links_write(env, obj, &ldata_new, th);
1603 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
1605 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1608 dt_write_unlock(env, obj);
1611 dt_trans_stop(env, dev, th);
1614 lfsck_ibits_unlock(&lh, LCK_EX);
1617 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1618 "for the object: "DFID", parent "DFID", name %.*s\n",
1619 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1620 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1624 struct lfsck_namespace *ns = com->lc_file_ram;
1626 ns->ln_flags |= LF_INCONSISTENT;
1633 * Conditionally remove the specified entry from the linkEA.
1635 * Take the parent lock firstly, then check whether the specified
1636 * name entry exists or not: if yes, do nothing; otherwise, call
1637 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1639 * \param[in] env pointer to the thread context
1640 * \param[in] com pointer to the lfsck component
1641 * \param[in] parent pointer to the parent directory
1642 * \param[in] child pointer to the child object that holds the linkEA
1643 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1644 * \param[in] cname the name for the child in the parent directory
1645 * \param[in] pfid the parent directory's FID for the linkEA
1647 * \retval positive number for repaired cases
1648 * \retval 0 if nothing to be repaired
1649 * \retval negative error number on failure
1651 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1652 struct lfsck_component *com,
1653 struct dt_object *parent,
1654 struct dt_object *child,
1655 struct linkea_data *ldata,
1656 struct lu_name *cname,
1657 struct lu_fid *pfid)
1659 struct lfsck_thread_info *info = lfsck_env_info(env);
1660 struct lu_fid *cfid = &info->lti_fid3;
1661 struct lfsck_lock_handle *llh = &info->lti_llh;
1665 rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1666 MDS_INODELOCK_UPDATE, LCK_PR);
1670 dt_read_lock(env, parent, 0);
1671 if (unlikely(lfsck_is_dead_obj(parent))) {
1672 dt_read_unlock(env, parent);
1674 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1680 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1681 (const struct dt_key *)cname->ln_name);
1682 dt_read_unlock(env, parent);
1684 /* It is safe to release the ldlm lock, because when the logic come
1685 * here, we have got all the needed information above whether the
1686 * linkEA entry is valid or not. It is not important that others
1687 * may add new linkEA entry after the ldlm lock released. If other
1688 * has removed the specified linkEA entry by race, then it is OK,
1689 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1692 if (rc == -ENOENT) {
1693 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1702 /* The LFSCK just found some internal status of cross-MDTs
1703 * create operation. That is normal. */
1704 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1705 linkea_next_entry(ldata);
1710 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1717 * Conditionally replace name entry in the parent.
1719 * As required, the LFSCK may re-create the lost MDT-object for dangling
1720 * name entry, but such repairing may be wrong because of bad FID in the
1721 * name entry. As the LFSCK processing, the real MDT-object may be found,
1722 * then the LFSCK should check whether the former re-created MDT-object
1723 * has been modified or not, if not, then destroy it and update the name
1724 * entry in the parent to reference the real MDT-object.
1726 * \param[in] env pointer to the thread context
1727 * \param[in] com pointer to the lfsck component
1728 * \param[in] parent pointer to the parent directory
1729 * \param[in] child pointer to the MDT-object that may be the real
1730 * MDT-object corresponding to the name entry in parent
1731 * \param[in] cfid the current FID in the name entry
1732 * \param[in] cname contains the name of the child in the parent directory
1734 * \retval positive number for repaired cases
1735 * \retval 0 if nothing to be repaired
1736 * \retval negative error number on failure
1738 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1739 struct lfsck_component *com,
1740 struct dt_object *parent,
1741 struct dt_object *child,
1742 const struct lu_fid *cfid,
1743 const struct lu_name *cname)
1745 struct lfsck_thread_info *info = lfsck_env_info(env);
1746 struct lu_attr *la = &info->lti_la;
1747 struct dt_insert_rec *rec = &info->lti_dt_rec;
1749 struct lfsck_instance *lfsck = com->lc_lfsck;
1750 /* The child and its name may be on different MDTs. */
1751 struct dt_device *dev = lfsck->li_next;
1752 const char *name = cname->ln_name;
1753 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1754 struct dt_object *cobj = NULL;
1755 struct lfsck_lock_handle *pllh = &info->lti_llh;
1756 struct lustre_handle clh = { 0 };
1757 struct linkea_data ldata = { NULL };
1758 struct thandle *th = NULL;
1763 /* @parent/@child may be based on lfsck->li_bottom,
1764 * but here we need the object based on the lfsck->li_next. */
1766 parent = lfsck_object_locate(dev, parent);
1768 GOTO(log, rc = PTR_ERR(parent));
1770 if (unlikely(!dt_try_as_dir(env, parent)))
1771 GOTO(log, rc = -ENOTDIR);
1773 rc = lfsck_lock(env, lfsck, parent, name, pllh,
1774 MDS_INODELOCK_UPDATE, LCK_PW);
1778 if (!fid_is_sane(cfid)) {
1783 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1786 if (rc == -ENOENT) {
1794 if (!dt_object_exists(cobj)) {
1799 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1800 (const struct dt_key *)name);
1801 if (rc == -ENOENT) {
1809 /* Someone changed the name entry, cannot replace it. */
1810 if (!lu_fid_eq(cfid, &tfid))
1813 /* lock the object to be destroyed. */
1814 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1815 MDS_INODELOCK_UPDATE |
1816 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1821 if (unlikely(lfsck_is_dead_obj(cobj))) {
1826 rc = dt_attr_get(env, cobj, la);
1830 /* The object has been modified by other(s), or it is not created by
1831 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1832 if (la->la_ctime != 0)
1835 if (S_ISREG(la->la_mode)) {
1836 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
1837 /* If someone has created related OST-object(s),
1839 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1840 GOTO(log, rc = (rc > 0 ? 0 : rc));
1844 dt_read_lock(env, child, 0);
1845 rc = lfsck_links_read2_with_rec(env, child, &ldata);
1846 dt_read_unlock(env, child);
1848 /* Someone changed the child, no need to replace. */
1855 rc = linkea_links_find(&ldata, cname, pfid);
1856 /* Someone moved the child, no need to replace. */
1860 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1863 th = dt_trans_create(env, dev);
1865 GOTO(log, rc = PTR_ERR(th));
1868 rc = dt_declare_destroy(env, cobj, th);
1873 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1877 rec->rec_type = S_IFDIR;
1878 rec->rec_fid = lfsck_dto2fid(child);
1879 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1880 (const struct dt_key *)name, th);
1884 rc = dt_trans_start_local(env, dev, th);
1889 rc = dt_destroy(env, cobj, th);
1894 /* The old name entry maybe not exist. */
1895 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
1896 if (rc != 0 && rc != -ENOENT)
1899 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1900 (const struct dt_key *)name, th, 1);
1902 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1905 dt_trans_stop(env, dev, th);
1908 lfsck_ibits_unlock(&clh, LCK_EX);
1911 if (cobj != NULL && !IS_ERR(cobj))
1912 lfsck_object_put(env, cobj);
1914 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1915 "object "DFID" because of conflict with the object "DFID
1916 " under the parent "DFID" with name %s: rc = %d\n",
1917 lfsck_lfsck2name(lfsck), PFID(cfid),
1918 PFID(lfsck_dto2fid(child)), PFID(pfid), name, rc);
1924 * Overwrite the linkEA for the object with the given ldata.
1926 * The caller should take the ldlm lock before the calling.
1928 * \param[in] env pointer to the thread context
1929 * \param[in] com pointer to the lfsck component
1930 * \param[in] obj pointer to the dt_object to be handled
1931 * \param[in] ldata pointer to the new linkEA data
1933 * \retval positive number for repaired cases
1934 * \retval 0 if nothing to be repaired
1935 * \retval negative error number on failure
1937 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1938 struct lfsck_component *com,
1939 struct dt_object *obj,
1940 struct linkea_data *ldata)
1942 struct lfsck_instance *lfsck = com->lc_lfsck;
1943 struct dt_device *dev = lfsck_obj2dev(obj);
1944 struct thandle *th = NULL;
1945 struct lu_buf linkea_buf;
1949 th = dt_trans_create(env, dev);
1951 GOTO(log, rc = PTR_ERR(th));
1953 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1954 ldata->ld_leh->leh_len);
1955 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1956 XATTR_NAME_LINK, 0, th);
1960 rc = dt_trans_start_local(env, dev, th);
1964 dt_write_lock(env, obj, 0);
1965 if (unlikely(lfsck_is_dead_obj(obj)))
1966 GOTO(unlock, rc = 0);
1968 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1969 GOTO(unlock, rc = 1);
1971 rc = dt_xattr_set(env, obj, &linkea_buf,
1972 XATTR_NAME_LINK, 0, th);
1974 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1977 dt_write_unlock(env, obj);
1980 dt_trans_stop(env, dev, th);
1983 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1984 "object "DFID": rc = %d\n",
1985 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1988 struct lfsck_namespace *ns = com->lc_file_ram;
1990 ns->ln_flags |= LF_INCONSISTENT;
1997 * Repair invalid name entry.
1999 * If the name entry contains invalid information, such as bad file type
2000 * or (and) corrupted object FID, then either remove the name entry or
2001 * udpate the name entry with the given (right) information.
2003 * \param[in] env pointer to the thread context
2004 * \param[in] com pointer to the lfsck component
2005 * \param[in] parent pointer to the parent directory
2006 * \param[in] child pointer to the object referenced by the name entry
2007 * \param[in] name the old name of the child under the parent directory
2008 * \param[in] name2 the new name of the child under the parent directory
2009 * \param[in] type the type claimed by the name entry
2010 * \param[in] update update the name entry if true; otherwise, remove it
2011 * \param[in] dec decrease the parent nlink count if true
2013 * \retval positive number for repaired successfully
2014 * \retval 0 if nothing to be repaired
2015 * \retval negative error number on failure
2017 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2018 struct lfsck_component *com,
2019 struct dt_object *parent,
2020 struct dt_object *child,
2021 const char *name, const char *name2,
2022 __u16 type, bool update, bool dec)
2024 struct lfsck_thread_info *info = lfsck_env_info(env);
2025 struct dt_insert_rec *rec = &info->lti_dt_rec;
2026 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2027 const struct lu_fid *cfid = lfsck_dto2fid(child);
2029 struct lfsck_instance *lfsck = com->lc_lfsck;
2030 struct dt_device *dev = lfsck->li_next;
2031 struct thandle *th = NULL;
2032 struct lfsck_lock_handle *llh = &info->lti_llh;
2033 struct lustre_handle lh = { 0 };
2037 parent = lfsck_object_locate(dev, parent);
2039 GOTO(log, rc = PTR_ERR(parent));
2041 if (unlikely(!dt_try_as_dir(env, parent)))
2042 GOTO(log, rc = -ENOTDIR);
2044 if (!update || strcmp(name, name2) == 0)
2045 rc = lfsck_lock(env, lfsck, parent, name, llh,
2046 MDS_INODELOCK_UPDATE, LCK_PW);
2048 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2049 MDS_INODELOCK_UPDATE, LCK_PW);
2053 th = dt_trans_create(env, dev);
2055 GOTO(unlock1, rc = PTR_ERR(th));
2057 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2062 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2063 rec->rec_fid = cfid;
2064 rc = dt_declare_insert(env, parent,
2065 (const struct dt_rec *)rec,
2066 (const struct dt_key *)name2, th);
2071 if (dec && S_ISDIR(type)) {
2072 rc = dt_declare_ref_del(env, parent, th);
2077 rc = dt_trans_start_local(env, dev, th);
2082 dt_write_lock(env, parent, 0);
2083 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2084 (const struct dt_key *)name);
2085 /* Someone has removed the bad name entry by race. */
2087 GOTO(unlock2, rc = 0);
2092 /* Someone has removed the bad name entry and reused it for other
2093 * object by race. */
2094 if (!lu_fid_eq(&tfid, cfid))
2095 GOTO(unlock2, rc = 0);
2097 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2098 GOTO(unlock2, rc = 1);
2100 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2105 rc = dt_insert(env, parent,
2106 (const struct dt_rec *)rec,
2107 (const struct dt_key *)name2, th, 1);
2112 if (dec && S_ISDIR(type)) {
2113 rc = dt_ref_del(env, parent, th);
2118 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2121 dt_write_unlock(env, parent);
2124 dt_trans_stop(env, dev, th);
2126 /* We are not sure whether the child will become orphan or not.
2127 * Record it in the LFSCK trace file for further checking in
2128 * the second-stage scanning. */
2129 if (!update && !dec && rc == 0)
2130 lfsck_namespace_trace_update(env, com, cfid,
2131 LNTF_CHECK_LINKEA, true);
2134 /* It is harmless even if unlock the unused lock_handle */
2135 lfsck_ibits_unlock(&lh, LCK_PW);
2139 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2140 "entry for: parent "DFID", child "DFID", name %s, type "
2141 "in name entry %o, type claimed by child %o. repair it "
2142 "by %s with new name2 %s: rc = %d\n",
2143 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2144 name, type, update ? lfsck_object_type(child) : 0,
2145 update ? "updating" : "removing", name2, rc);
2148 struct lfsck_namespace *ns = com->lc_file_ram;
2150 ns->ln_flags |= LF_INCONSISTENT;
2157 * Update the ".." name entry for the given object.
2159 * The object's ".." is corrupted, this function will update the ".." name
2160 * entry with the given pfid, and the linkEA with the given ldata.
2162 * The caller should take the ldlm lock before the calling.
2164 * \param[in] env pointer to the thread context
2165 * \param[in] com pointer to the lfsck component
2166 * \param[in] obj pointer to the dt_object to be handled
2167 * \param[in] pfid the new fid for the object's ".." name entry
2168 * \param[in] cname the name for the @obj in the parent directory
2170 * \retval positive number for repaired cases
2171 * \retval 0 if nothing to be repaired
2172 * \retval negative error number on failure
2174 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2175 struct lfsck_component *com,
2176 struct dt_object *obj,
2177 const struct lu_fid *pfid,
2178 struct lu_name *cname)
2180 struct lfsck_thread_info *info = lfsck_env_info(env);
2181 struct dt_insert_rec *rec = &info->lti_dt_rec;
2182 struct lfsck_instance *lfsck = com->lc_lfsck;
2183 struct dt_device *dev = lfsck_obj2dev(obj);
2184 struct thandle *th = NULL;
2185 struct linkea_data ldata = { NULL };
2186 struct lu_buf linkea_buf;
2190 LASSERT(!dt_object_remote(obj));
2191 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2193 rc = linkea_links_new(&ldata, &info->lti_big_buf, cname, pfid);
2197 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2198 ldata.ld_leh->leh_len);
2200 th = dt_trans_create(env, dev);
2202 GOTO(log, rc = PTR_ERR(th));
2204 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2208 rec->rec_type = S_IFDIR;
2209 rec->rec_fid = pfid;
2210 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2211 (const struct dt_key *)dotdot, th);
2215 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2216 XATTR_NAME_LINK, 0, th);
2220 rc = dt_trans_start_local(env, dev, th);
2224 dt_write_lock(env, obj, 0);
2225 if (unlikely(lfsck_is_dead_obj(obj)))
2226 GOTO(unlock, rc = 0);
2228 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2229 GOTO(unlock, rc = 1);
2231 /* The old ".." name entry maybe not exist. */
2232 dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2234 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2235 (const struct dt_key *)dotdot, th, 1);
2239 rc = lfsck_links_write(env, obj, &ldata, th);
2241 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2244 dt_write_unlock(env, obj);
2247 dt_trans_stop(env, dev, th);
2250 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2251 "the object "DFID", new parent "DFID": rc = %d\n",
2252 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2256 struct lfsck_namespace *ns = com->lc_file_ram;
2258 ns->ln_flags |= LF_INCONSISTENT;
2265 * Handle orphan @obj during Double Scan Directory.
2267 * Remove the @obj's current (invalid) linkEA entries, and insert
2268 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2269 * ${FID}-${PFID}-D-${conflict_version}
2271 * The caller should take the ldlm lock before the calling.
2273 * \param[in] env pointer to the thread context
2274 * \param[in] com pointer to the lfsck component
2275 * \param[in] obj pointer to the orphan object to be handled
2276 * \param[in] pfid the new fid for the object's ".." name entry
2277 * \param[in,out] lh ldlm lock handler for the given @obj
2278 * \param[out] type to tell the caller what the inconsistency is
2280 * \retval positive number for repaired cases
2281 * \retval 0 if nothing to be repaired
2282 * \retval negative error number on failure
2285 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2286 struct lfsck_component *com,
2287 struct dt_object *obj,
2288 const struct lu_fid *pfid,
2289 struct lustre_handle *lh,
2290 enum lfsck_namespace_inconsistency_type *type)
2292 struct lfsck_thread_info *info = lfsck_env_info(env);
2293 struct lfsck_namespace *ns = com->lc_file_ram;
2297 /* Remove the unrecognized linkEA. */
2298 rc = lfsck_namespace_links_remove(env, com, obj);
2299 lfsck_ibits_unlock(lh, LCK_EX);
2300 if (rc < 0 && rc != -ENODATA)
2303 *type = LNIT_MUL_REF;
2305 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2306 * ever tried to verify some remote MDT-object that resides on this
2307 * MDT, but this MDT failed to respond such request. So means there
2308 * may be some remote name entry on other MDT that references this
2309 * object with another name, so we cannot know whether this linkEA
2310 * is valid or not. So keep it there and maybe resolved when next
2312 if (ns->ln_flags & LF_INCOMPLETE)
2315 /* The unique linkEA is invalid, even if the ".." name entry may be
2316 * valid, we still cannot know via which name entry this directory
2317 * will be referenced. Then handle it as pure orphan. */
2318 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2319 "-"DFID, PFID(pfid));
2320 rc = lfsck_namespace_insert_orphan(env, com, obj,
2321 info->lti_tmpbuf, "D", NULL);
2327 * Double Scan Directory object for single linkEA entry case.
2329 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2330 * then check whether the name is in the namespace or not, if not, add the
2331 * missing name entry back to namespace. If the linkEA entry is invalid,
2332 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2335 * \param[in] env pointer to the thread context
2336 * \param[in] com pointer to the lfsck component
2337 * \param[in] child pointer to the directory to be double scanned
2338 * \param[in] pfid the FID corresponding to the ".." entry
2339 * \param[in] ldata pointer to the linkEA data for the given @child
2340 * \param[in,out] lh ldlm lock handler for the given @child
2341 * \param[out] type to tell the caller what the inconsistency is
2342 * \param[in] retry if found inconsistency, but the caller does not hold
2343 * ldlm lock on the @child, then set @retry as true
2344 * \param[in] unknown set if does not know how to repair the inconsistency
2346 * \retval positive number for repaired cases
2347 * \retval 0 if nothing to be repaired
2348 * \retval negative error number on failure
2351 lfsck_namespace_dsd_single(const struct lu_env *env,
2352 struct lfsck_component *com,
2353 struct dt_object *child,
2354 const struct lu_fid *pfid,
2355 struct linkea_data *ldata,
2356 struct lustre_handle *lh,
2357 enum lfsck_namespace_inconsistency_type *type,
2358 bool *retry, bool *unknown)
2360 struct lfsck_thread_info *info = lfsck_env_info(env);
2361 struct lu_name *cname = &info->lti_name;
2362 const struct lu_fid *cfid = lfsck_dto2fid(child);
2364 struct lfsck_namespace *ns = com->lc_file_ram;
2365 struct lfsck_instance *lfsck = com->lc_lfsck;
2366 struct dt_object *parent = NULL;
2367 struct lmv_mds_md_v1 *lmv;
2371 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2373 sizeof(info->lti_key));
2374 /* The unique linkEA entry with bad parent will be handled as orphan. */
2376 if (!lustre_handle_is_used(lh) && retry != NULL)
2379 rc = lfsck_namespace_dsd_orphan(env, com, child,
2385 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2387 GOTO(out, rc = PTR_ERR(parent));
2389 /* We trust the unique linkEA entry in spite of whether it matches the
2390 * ".." name entry or not. Because even if the linkEA entry is wrong
2391 * and the ".." name entry is right, we still cannot know via which
2392 * name entry the child will be referenced, since all known entries
2393 * have been verified during the first-stage scanning. */
2394 if (!dt_object_exists(parent)) {
2395 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2396 * has ever tried to verify some remote MDT-object that resides
2397 * on this MDT, but this MDT failed to respond such request. So
2398 * means there may be some remote name entry on other MDT that
2399 * references this object with another name, so we cannot know
2400 * whether this linkEA is valid or not. So keep it there and
2401 * maybe resolved when next LFSCK run. */
2402 if (ns->ln_flags & LF_INCOMPLETE)
2405 if (!lustre_handle_is_used(lh) && retry != NULL) {
2411 lfsck_ibits_unlock(lh, LCK_EX);
2414 lmv = &info->lti_lmv;
2415 rc = lfsck_read_stripe_lmv(env, child, lmv);
2416 if (rc != 0 && rc != -ENODATA)
2419 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2421 } else if (lfsck_shard_name_to_index(env,
2422 cname->ln_name, cname->ln_namelen,
2423 S_IFDIR, cfid) < 0) {
2424 /* It is an invalid name entry, we
2425 * cannot trust the parent also. */
2426 rc = lfsck_namespace_shrink_linkea(env, com, child,
2427 ldata, cname, &tfid, true);
2431 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2432 "-"DFID, PFID(pfid));
2433 rc = lfsck_namespace_insert_orphan(env, com, child,
2434 info->lti_tmpbuf, "S", NULL);
2439 /* Create the lost parent as an orphan. */
2440 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2442 /* Add the missing name entry to the parent. */
2443 rc = lfsck_namespace_insert_normal(env, com, parent,
2444 child, cname->ln_name);
2445 if (unlikely(rc == -EEXIST)) {
2446 /* Unfortunately, someone reused the name
2447 * under the parent by race. So we have
2448 * to remove the linkEA entry from
2449 * current child object. It means that the
2450 * LFSCK cannot recover the system
2451 * totally back to its original status,
2452 * but it is necessary to make the
2453 * current system to be consistent. */
2454 rc = lfsck_namespace_shrink_linkea(env,
2456 cname, &tfid, true);
2458 snprintf(info->lti_tmpbuf,
2459 sizeof(info->lti_tmpbuf),
2460 "-"DFID, PFID(pfid));
2461 rc = lfsck_namespace_insert_orphan(env,
2462 com, child, info->lti_tmpbuf,
2469 } /* !dt_object_exists(parent) */
2471 /* The unique linkEA entry with bad parent will be handled as orphan. */
2472 if (unlikely(!dt_try_as_dir(env, parent))) {
2473 if (!lustre_handle_is_used(lh) && retry != NULL)
2476 rc = lfsck_namespace_dsd_orphan(env, com, child,
2482 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2483 (const struct dt_key *)cname->ln_name);
2484 if (rc == -ENOENT) {
2485 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2486 * has ever tried to verify some remote MDT-object that resides
2487 * on this MDT, but this MDT failed to respond such request. So
2488 * means there may be some remote name entry on other MDT that
2489 * references this object with another name, so we cannot know
2490 * whether this linkEA is valid or not. So keep it there and
2491 * maybe resolved when next LFSCK run. */
2492 if (ns->ln_flags & LF_INCOMPLETE)
2495 if (!lustre_handle_is_used(lh) && retry != NULL) {
2501 lfsck_ibits_unlock(lh, LCK_EX);
2502 rc = lfsck_namespace_check_name(env, parent, child, cname);
2509 /* It is an invalid name entry, drop it. */
2510 if (unlikely(rc > 0)) {
2511 rc = lfsck_namespace_shrink_linkea(env, com, child,
2512 ldata, cname, &tfid, true);
2514 snprintf(info->lti_tmpbuf,
2515 sizeof(info->lti_tmpbuf),
2516 "-"DFID, PFID(pfid));
2517 rc = lfsck_namespace_insert_orphan(env, com,
2518 child, info->lti_tmpbuf, "D", NULL);
2524 /* Add the missing name entry back to the namespace. */
2525 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2527 if (unlikely(rc == -ESTALE))
2528 /* It may happen when the remote object has been
2529 * removed, but the local MDT is not aware of that. */
2532 if (unlikely(rc == -EEXIST)) {
2533 /* Unfortunately, someone reused the name under the
2534 * parent by race. So we have to remove the linkEA
2535 * entry from current child object. It means that the
2536 * LFSCK cannot recover the system totally back to
2537 * its original status, but it is necessary to make
2538 * the current system to be consistent.
2540 * It also may be because of the LFSCK found some
2541 * internal status of create operation. Under such
2542 * case, nothing to be done. */
2543 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2544 parent, child, ldata, cname, &tfid);
2546 snprintf(info->lti_tmpbuf,
2547 sizeof(info->lti_tmpbuf),
2548 "-"DFID, PFID(pfid));
2549 rc = lfsck_namespace_insert_orphan(env, com,
2550 child, info->lti_tmpbuf, "D", NULL);
2555 } /* rc == -ENOENT */
2560 if (!lu_fid_eq(&tfid, cfid)) {
2561 if (!lustre_handle_is_used(lh) && retry != NULL) {
2567 lfsck_ibits_unlock(lh, LCK_EX);
2568 /* The name entry references another MDT-object that
2569 * may be created by the LFSCK for repairing dangling
2570 * name entry. Try to replace it. */
2571 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2574 rc = lfsck_namespace_dsd_orphan(env, com, child,
2580 /* Zero FID may because the remote directroy object has invalid linkEA,
2581 * or lost linkEA. Under such case, the LFSCK on this MDT does not know
2582 * how to repair the inconsistency, but the namespace LFSCK on the MDT
2583 * where its name entry resides may has more information (name, FID) to
2584 * repair such inconsistency. So here, keep the inconsistency to avoid
2585 * some imporper repairing. */
2586 if (fid_is_zero(pfid)) {
2593 /* The ".." name entry is wrong, update it. */
2594 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2595 if (!lustre_handle_is_used(lh) && retry != NULL) {
2601 *type = LNIT_UNMATCHED_PAIRS;
2602 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2603 lfsck_dto2fid(parent), cname);
2609 if (parent != NULL && !IS_ERR(parent))
2610 lfsck_object_put(env, parent);
2616 * Double Scan Directory object for multiple linkEA entries case.
2618 * The given @child has multiple linkEA entries. There is at most one linkEA
2619 * entry will be valid, all the others will be removed. Firstly, the function
2620 * will try to find out the linkEA entry for which the name entry exists under
2621 * the given parent (@pfid). If there is no linkEA entry that matches the given
2622 * ".." name entry, then tries to find out the first linkEA entry that both the
2623 * parent and the name entry exist to rebuild a new ".." name entry.
2625 * \param[in] env pointer to the thread context
2626 * \param[in] com pointer to the lfsck component
2627 * \param[in] child pointer to the directory to be double scanned
2628 * \param[in] pfid the FID corresponding to the ".." entry
2629 * \param[in] ldata pointer to the linkEA data for the given @child
2630 * \param[in,out] lh ldlm lock handler for the given @child
2631 * \param[out] type to tell the caller what the inconsistency is
2632 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2633 * \param[in] unknown set if does not know how to repair the inconsistency
2635 * \retval positive number for repaired cases
2636 * \retval 0 if nothing to be repaired
2637 * \retval negative error number on failure
2640 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2641 struct lfsck_component *com,
2642 struct dt_object *child,
2643 const struct lu_fid *pfid,
2644 struct linkea_data *ldata,
2645 struct lustre_handle *lh,
2646 enum lfsck_namespace_inconsistency_type *type,
2647 bool lpf, bool *unknown)
2649 struct lfsck_thread_info *info = lfsck_env_info(env);
2650 struct lu_name *cname = &info->lti_name;
2651 const struct lu_fid *cfid = lfsck_dto2fid(child);
2652 struct lu_fid *pfid2 = &info->lti_fid3;
2654 struct lfsck_namespace *ns = com->lc_file_ram;
2655 struct lfsck_instance *lfsck = com->lc_lfsck;
2656 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2657 struct dt_object *parent = NULL;
2658 struct linkea_data ldata_new = { NULL };
2659 int dirent_count = 0;
2665 while (ldata->ld_lee != NULL) {
2666 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2668 sizeof(info->lti_key));
2669 /* Drop invalid linkEA entry. */
2671 lfsck_linkea_del_buf(ldata, cname);
2675 /* Drop repeated linkEA entries. */
2676 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2678 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2679 * then it is possible that: the directry object has ever
2680 * been lost, but its name entry was there. In the former
2681 * LFSCK run, during the first-stage scanning, the LFSCK
2682 * found the dangling name entry, but it did not recreate
2683 * the lost object, and when moved to the second-stage
2684 * scanning, some children objects of the lost directory
2685 * object were found, then the LFSCK recreated such lost
2686 * directory object as an orphan.
2688 * When the LFSCK runs again, if the dangling name is still
2689 * there, the LFSCK should move the orphan directory object
2690 * back to the normal namespace. */
2691 if (!lpf && !fid_is_zero(pfid) &&
2692 !lu_fid_eq(pfid, &tfid) && once) {
2693 linkea_next_entry(ldata);
2697 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2699 RETURN(PTR_ERR(parent));
2701 if (!dt_object_exists(parent)) {
2702 lfsck_object_put(env, parent);
2703 if (ldata->ld_leh->leh_reccount > 1) {
2704 /* If it is NOT the last linkEA entry, then
2705 * there is still other chance to make the
2706 * child to be visible via other parent, then
2707 * remove this linkEA entry. */
2708 lfsck_linkea_del_buf(ldata, cname);
2715 /* The linkEA entry with bad parent will be removed. */
2716 if (unlikely(!dt_try_as_dir(env, parent))) {
2717 lfsck_object_put(env, parent);
2718 lfsck_linkea_del_buf(ldata, cname);
2722 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2723 (const struct dt_key *)cname->ln_name);
2724 *pfid2 = *lfsck_dto2fid(parent);
2725 if (rc == -ENOENT) {
2726 lfsck_object_put(env, parent);
2727 linkea_next_entry(ldata);
2732 lfsck_object_put(env, parent);
2737 if (lu_fid_eq(&tfid, cfid)) {
2738 lfsck_object_put(env, parent);
2739 /* If the parent (that is declared via linkEA entry)
2740 * directory contains the specified child, but such
2741 * parent does not match the dotdot name entry, then
2742 * trust the linkEA. */
2743 if (!fid_is_zero(pfid) && !lu_fid_eq(pfid, pfid2)) {
2744 *type = LNIT_UNMATCHED_PAIRS;
2745 rc = lfsck_namespace_repair_unmatched_pairs(env,
2746 com, child, pfid2, cname);
2752 /* It is the most common case that we find the
2753 * name entry corresponding to the linkEA entry
2754 * that matches the ".." name entry. */
2755 rc = linkea_links_new(&ldata_new, &info->lti_big_buf,
2760 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2765 lfsck_linkea_del_buf(ldata, cname);
2766 linkea_first_entry(ldata);
2767 /* There may be some invalid dangling name entries under
2768 * other parent directories, remove all of them. */
2769 while (ldata->ld_lee != NULL) {
2770 rc = lfsck_namespace_unpack_linkea_entry(ldata,
2771 cname, &tfid, info->lti_key,
2772 sizeof(info->lti_key));
2776 parent = lfsck_object_find_bottom(env, lfsck,
2778 if (IS_ERR(parent)) {
2779 rc = PTR_ERR(parent);
2780 if (rc != -ENOENT &&
2781 bk->lb_param & LPF_FAILOUT)
2787 if (!dt_object_exists(parent)) {
2788 lfsck_object_put(env, parent);
2792 rc = lfsck_namespace_repair_dirent(env, com,
2793 parent, child, cname->ln_name,
2794 cname->ln_name, S_IFDIR, false, true);
2795 lfsck_object_put(env, parent);
2797 if (bk->lb_param & LPF_FAILOUT)
2806 lfsck_linkea_del_buf(ldata, cname);
2809 ns->ln_dirent_repaired += dirent_count;
2812 } /* lu_fid_eq(&tfid, lfsck_dto2fid(child)) */
2814 lfsck_ibits_unlock(lh, LCK_EX);
2815 /* The name entry references another MDT-object that may be
2816 * created by the LFSCK for repairing dangling name entry.
2817 * Try to replace it. */
2818 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2820 lfsck_object_put(env, parent);
2827 lfsck_linkea_del_buf(ldata, cname);
2828 } /* while (ldata->ld_lee != NULL) */
2830 /* If there is still linkEA overflow, return. */
2831 if (unlikely(ldata->ld_leh->leh_overflow_time))
2834 linkea_first_entry(ldata);
2835 if (ldata->ld_leh->leh_reccount == 1) {
2836 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2837 lh, type, NULL, unknown);
2842 /* All linkEA entries are invalid and removed, then handle the @child
2844 if (ldata->ld_leh->leh_reccount == 0) {
2845 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2851 /* If the dangling name entry for the orphan directory object has
2852 * been remvoed, then just check whether the directory object is
2853 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2859 /* There is no linkEA entry that matches the ".." name entry. Find
2860 * the first linkEA entry that both parent and name entry exist to
2861 * rebuild a new ".." name entry. */
2871 * Repair the object's nlink attribute.
2873 * If all the known name entries have been verified, then the object's hard
2874 * link attribute should match the object's linkEA entries count unless the
2875 * object's has too many hard link to be recorded in the linkEA. Such cases
2876 * should have been marked in the LFSCK trace file. Otherwise, trust the
2877 * linkEA to update the object's nlink attribute.
2879 * \param[in] env pointer to the thread context
2880 * \param[in] com pointer to the lfsck component
2881 * \param[in] obj pointer to the dt_object to be handled
2882 * \param[in,out] la pointer to buffer to object's attribute before
2883 * and after the repairing
2885 * \retval positive number for repaired cases
2886 * \retval 0 if nothing to be repaired
2887 * \retval negative error number on failure
2889 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2890 struct lfsck_component *com,
2891 struct dt_object *obj,
2894 struct lfsck_namespace *ns = com->lc_file_ram;
2895 struct lfsck_instance *lfsck = com->lc_lfsck;
2896 struct dt_device *dev = lfsck_obj2dev(obj);
2897 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2898 struct thandle *th = NULL;
2899 struct linkea_data ldata = { NULL };
2900 struct lustre_handle lh = { 0 };
2901 __u32 old = la->la_nlink;
2905 LASSERT(!dt_object_remote(obj));
2907 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2908 MDS_INODELOCK_UPDATE, LCK_PW);
2912 th = dt_trans_create(env, dev);
2914 GOTO(log, rc = PTR_ERR(th));
2916 la->la_valid = LA_NLINK;
2917 rc = dt_declare_attr_set(env, obj, la, th);
2921 rc = dt_trans_start_local(env, dev, th);
2925 dt_write_lock(env, obj, 0);
2926 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2927 * ever tried to verify some remote MDT-object that resides on this
2928 * MDT, but this MDT failed to respond such request. So means there
2929 * may be some remote name entry on other MDT that references this
2930 * object with another name, so we cannot know whether this linkEA
2931 * is valid or not. So keep it there and maybe resolved when next
2933 if (ns->ln_flags & LF_INCOMPLETE)
2934 GOTO(unlock, rc = 0);
2936 rc = dt_attr_get(env, obj, la);
2938 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2940 rc = lfsck_links_read2_with_rec(env, obj, &ldata);
2942 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2944 /* XXX: Currently, we only update the nlink attribute if the known
2945 * linkEA entries is larger than the nlink attribute. That is
2947 if (la->la_nlink >= ldata.ld_leh->leh_reccount ||
2948 unlikely(la->la_nlink == 0 ||
2949 ldata.ld_leh->leh_overflow_time))
2950 GOTO(unlock, rc = 0);
2952 la->la_nlink = ldata.ld_leh->leh_reccount;
2953 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2954 GOTO(unlock, rc = 1);
2956 rc = dt_attr_set(env, obj, la, th);
2958 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2961 dt_write_unlock(env, obj);
2964 dt_trans_stop(env, dev, th);
2967 lfsck_ibits_unlock(&lh, LCK_PW);
2968 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2969 "nlink count from %u to %u: rc = %d\n",
2970 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2973 ns->ln_flags |= LF_INCONSISTENT;
2979 * Double scan the directory object for namespace LFSCK.
2981 * This function will verify the <parent, child> pairs in the namespace tree:
2982 * the parent references the child via some name entry that should be in the
2983 * child's linkEA entry, the child should back references the parent via its
2986 * The LFSCK will scan every linkEA entry in turn until find out the first
2987 * matched pairs. If found, then all other linkEA entries will be dropped.
2988 * If all the linkEA entries cannot match the ".." name entry, then there
2989 * are serveral possible cases:
2991 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2992 * in the linkEA entry is valid.
2994 * 2) If there are multiple linkEA entries, then try to find the linkEA
2995 * that matches the ".." name entry. If found, then all other entries
2996 * are invalid; otherwise, it is quite possible that the ".." name entry
2997 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2998 * entry according to the first valid linkEA entry (both the parent and
2999 * the name entry should exist).
3001 * 3) If the directory object has no (valid) linkEA entry, then the
3002 * directory object will be handled as pure orphan and inserted
3003 * in the .lustre/lost+found/MDTxxxx/ with the name:
3004 * ${self_FID}-${PFID}-D-${conflict_version}
3006 * \param[in] env pointer to the thread context
3007 * \param[in] com pointer to the lfsck component
3008 * \param[in] child pointer to the directory object to be handled
3009 * \param[in] flags to indicate the specical checking on the @child
3011 * \retval positive number for repaired cases
3012 * \retval 0 if nothing to be repaired
3013 * \retval negative error number on failure
3015 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3016 struct lfsck_component *com,
3017 struct dt_object *child, __u8 flags)
3019 struct lfsck_thread_info *info = lfsck_env_info(env);
3020 const struct lu_fid *cfid = lfsck_dto2fid(child);
3021 struct lu_fid *pfid = &info->lti_fid2;
3022 struct lfsck_namespace *ns = com->lc_file_ram;
3023 struct lfsck_instance *lfsck = com->lc_lfsck;
3024 struct lustre_handle lh = { 0 };
3025 struct linkea_data ldata = { NULL };
3026 bool unknown = false;
3029 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3033 LASSERT(!dt_object_remote(child));
3035 if (flags & LNTF_UNCERTAIN_LMV) {
3036 if (flags & LNTF_RECHECK_NAME_HASH) {
3037 rc = lfsck_namespace_scan_shard(env, com, child);
3041 ns->ln_striped_shards_scanned++;
3043 ns->ln_striped_shards_skipped++;
3047 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3051 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3052 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3053 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3054 "the namespace LFSCK, then the LFSCK cannot guarantee"
3055 "all the name entries have been verified in first-stage"
3056 "scanning. So have to skip orphan related handling for"
3057 "the directory object "DFID" with remote name entry\n",
3058 lfsck_lfsck2name(lfsck), PFID(cfid));
3063 if (unlikely(!dt_try_as_dir(env, child)))
3064 GOTO(out, rc = -ENOTDIR);
3066 /* We only take ldlm lock on the @child when required. When the
3067 * logic comes here for the first time, it is always false. */
3071 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3072 MDS_INODELOCK_UPDATE |
3073 MDS_INODELOCK_XATTR, LCK_EX);
3078 dt_read_lock(env, child, 0);
3079 if (unlikely(lfsck_is_dead_obj(child))) {
3080 dt_read_unlock(env, child);
3085 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3086 (const struct dt_key *)dotdot);
3088 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3089 dt_read_unlock(env, child);
3094 if (!lustre_handle_is_used(&lh)) {
3095 dt_read_unlock(env, child);
3100 } else if (lfsck->li_lpf_obj != NULL &&
3101 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3103 } else if (unlikely(!fid_is_sane(pfid))) {
3107 rc = lfsck_links_read(env, child, &ldata);
3108 dt_read_unlock(env, child);
3110 if (rc != -ENODATA && rc != -EINVAL)
3113 if (!lustre_handle_is_used(&lh))
3116 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3117 /* Remove the corrupted linkEA. */
3118 rc = lfsck_namespace_links_remove(env, com, child);
3120 /* Here, because of the crashed linkEA, we
3121 * cannot know whether there is some parent
3122 * that references the child directory via
3123 * some name entry or not. So keep it there,
3124 * when the LFSCK run next time, if there is
3125 * some parent that references this object,
3126 * then the LFSCK can rebuild the linkEA;
3127 * otherwise, this object will be handled
3128 * as orphan as above. */
3131 /* 1. If we have neither ".." nor linkEA,
3132 * then it is an orphan.
3134 * 2. If we only have the ".." name entry,
3135 * but no parent references this child
3136 * directory, then handle it as orphan. */
3137 lfsck_ibits_unlock(&lh, LCK_EX);
3138 type = LNIT_MUL_REF;
3140 /* If the LFSCK is marked as LF_INCOMPLETE,
3141 * then means some MDT has ever tried to
3142 * verify some remote MDT-object that resides
3143 * on this MDT, but this MDT failed to respond
3144 * such request. So means there may be some
3145 * remote name entry on other MDT that
3146 * references this object with another name,
3147 * so we cannot know whether this linkEA is
3148 * valid or not. So keep it there and maybe
3149 * resolved when next LFSCK run. */
3150 if (ns->ln_flags & LF_INCOMPLETE)
3153 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3154 "-"DFID, PFID(pfid));
3155 rc = lfsck_namespace_insert_orphan(env, com, child,
3156 info->lti_tmpbuf, "D", NULL);
3162 linkea_first_entry(&ldata);
3163 /* This is the most common case: the object has unique linkEA entry. */
3164 if (ldata.ld_leh->leh_reccount == 1) {
3165 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3166 &lh, &type, &retry, &unknown);
3168 LASSERT(!lustre_handle_is_used(&lh));
3177 if (!lustre_handle_is_used(&lh))
3180 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3181 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3187 /* When we come here, the cases usually like that:
3188 * 1) The directory object has a corrupted linkEA entry. During the
3189 * first-stage scanning, the LFSCK cannot know such corruption,
3190 * then it appends the right linkEA entry according to the found
3191 * name entry after the bad one.
3193 * 2) The directory object has a right linkEA entry. During the
3194 * first-stage scanning, the LFSCK finds some bad name entry,
3195 * but the LFSCK cannot aware that at that time, then it adds
3196 * the bad linkEA entry for further processing. */
3197 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3198 &lh, &type, lpf, &unknown);
3203 lfsck_ibits_unlock(&lh, LCK_EX);
3206 case LNIT_BAD_LINKEA:
3207 ns->ln_linkea_repaired++;
3209 case LNIT_UNMATCHED_PAIRS:
3210 ns->ln_unmatched_pairs_repaired++;
3213 ns->ln_mul_ref_repaired++;
3221 ns->ln_unknown_inconsistency++;
3226 #define lfsck_time_before(a, b) \
3227 (typecheck(__u32, a) && \
3228 typecheck(__u32, b) && \
3229 ((int)(a) - (int)(b) < 0))
3232 lfsck_namespace_linkea_stale_overflow(struct linkea_data *ldata,
3233 struct lfsck_namespace *ns)
3235 /* Both the leh_overflow_time and ln_time_latest_reset are
3236 * local time based, so need NOT to care about clock drift
3237 * among the servers. */
3238 return ldata->ld_leh->leh_overflow_time &&
3239 lfsck_time_before(ldata->ld_leh->leh_overflow_time,
3240 ns->ln_time_latest_reset);
3244 * Clear the object's linkEA overflow timestamp.
3246 * If the MDT-object has too many hard links as to the linkEA cannot hold
3247 * all of them, then overflow timestamp will be set in the linkEA header.
3248 * If some hard links are removed after that, then it is possible to hold
3249 * other missed linkEA entries. If the namespace LFSCK have added all the
3250 * related linkEA entries, then it will remove the overflow timestamp.
3252 * \param[in] env pointer to the thread context
3253 * \param[in] com pointer to the lfsck component
3254 * \param[in] ldata pointer to the linkEA data for the given @obj
3255 * \param[in] obj pointer to the dt_object to be handled
3257 * \retval positive number for repaired cases
3258 * \retval 0 if nothing to be repaired
3259 * \retval negative error number on failure
3261 static int lfsck_namespace_linkea_clear_overflow(const struct lu_env *env,
3262 struct lfsck_component *com,
3263 struct linkea_data *ldata,
3264 struct dt_object *obj)
3266 struct lfsck_namespace *ns = com->lc_file_ram;
3267 struct lfsck_instance *lfsck = com->lc_lfsck;
3268 struct dt_device *dev = lfsck_obj2dev(obj);
3269 struct thandle *th = NULL;
3270 struct lustre_handle lh = { 0 };
3271 struct lu_buf linkea_buf;
3275 LASSERT(!dt_object_remote(obj));
3277 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3278 MDS_INODELOCK_UPDATE, LCK_PW);
3282 th = dt_trans_create(env, dev);
3284 GOTO(log, rc = PTR_ERR(th));
3286 rc = dt_declare_xattr_set(env, obj,
3287 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
3288 XATTR_NAME_LINK, 0, th);
3292 rc = dt_trans_start_local(env, dev, th);
3296 dt_write_lock(env, obj, 0);
3297 rc = lfsck_links_read(env, obj, ldata);
3301 if (unlikely(!lfsck_namespace_linkea_stale_overflow(ldata, ns)))
3302 GOTO(unlock, rc = 0);
3304 ldata->ld_leh->leh_overflow_time = 0;
3305 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3306 GOTO(unlock, rc = 1);
3308 /* If all known entries are in the linkEA, then the 'leh_reccount'
3309 * should NOT be zero. */
3310 LASSERT(ldata->ld_leh->leh_reccount > 0);
3312 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
3313 ldata->ld_leh->leh_len);
3314 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK, 0, th);
3315 if (unlikely(rc == -ENOSPC))
3323 dt_write_unlock(env, obj);
3326 dt_trans_stop(env, dev, th);
3329 lfsck_ibits_unlock(&lh, LCK_PW);
3330 CDEBUG(D_LFSCK, "%s: clear linkea overflow timestamp for the object "
3332 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
3338 * Double scan the MDT-object for namespace LFSCK.
3340 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3341 * those entries from the linkEA; if the linkEA becomes empty or the object
3342 * has no linkEA, then it is an orphan and will be added into the directory
3343 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3344 * the remote parent; if the name entry corresponding to some linkEA entry
3345 * is lost, then add the name entry back to the namespace.
3347 * \param[in] env pointer to the thread context
3348 * \param[in] com pointer to the lfsck component
3349 * \param[in] child pointer to the dt_object to be handled
3350 * \param[in] flags some hints to indicate how the @child should be handled
3352 * \retval positive number for repaired cases
3353 * \retval 0 if nothing to be repaired
3354 * \retval negative error number on failure
3356 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3357 struct lfsck_component *com,
3358 struct dt_object *child, __u8 flags)
3360 struct lfsck_thread_info *info = lfsck_env_info(env);
3361 struct lu_attr *la = &info->lti_la;
3362 struct lu_name *cname = &info->lti_name;
3363 struct lu_fid *pfid = &info->lti_fid;
3364 struct lu_fid *cfid = &info->lti_fid2;
3365 struct lfsck_instance *lfsck = com->lc_lfsck;
3366 struct lfsck_namespace *ns = com->lc_file_ram;
3367 struct dt_object *parent = NULL;
3368 struct linkea_data ldata = { NULL };
3369 bool repaired = false;
3374 dt_read_lock(env, child, 0);
3375 if (unlikely(lfsck_is_dead_obj(child))) {
3376 dt_read_unlock(env, child);
3381 if (S_ISDIR(lfsck_object_type(child))) {
3382 dt_read_unlock(env, child);
3383 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3388 rc = lfsck_links_read(env, child, &ldata);
3389 dt_read_unlock(env, child);
3391 if (rc == -EINVAL) {
3392 struct lustre_handle lh = { 0 };
3394 rc = lfsck_ibits_lock(env, com->lc_lfsck, child, &lh,
3395 MDS_INODELOCK_UPDATE |
3396 MDS_INODELOCK_XATTR, LCK_EX);
3398 rc = lfsck_namespace_links_remove(env, com, child);
3399 lfsck_ibits_unlock(&lh, LCK_EX);
3408 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3409 unlikely(lfsck_namespace_linkea_stale_overflow(&ldata, ns))) {
3410 rc = lfsck_namespace_linkea_clear_overflow(env, com, &ldata,
3416 ns->ln_linkea_overflow_cleared++;
3419 linkea_first_entry(&ldata);
3420 while (ldata.ld_lee != NULL) {
3421 rc = lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3423 sizeof(info->lti_key));
3424 /* Invalid PFID in the linkEA entry. */
3426 rc = lfsck_namespace_shrink_linkea(env, com, child,
3427 &ldata, cname, pfid, true);
3437 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3439 /* Found repeated linkEA entries */
3441 rc = lfsck_namespace_shrink_linkea(env, com, child,
3442 &ldata, cname, pfid, false);
3454 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3456 GOTO(out, rc = PTR_ERR(parent));
3458 if (!dt_object_exists(parent)) {
3461 if (ldata.ld_leh->leh_reccount > 1) {
3462 /* If it is NOT the last linkEA entry, then
3463 * there is still other chance to make the
3464 * child to be visible via other parent, then
3465 * remove this linkEA entry. */
3466 rc = lfsck_namespace_shrink_linkea(env, com,
3467 child, &ldata, cname, pfid, true);
3469 /* If the LFSCK is marked as LF_INCOMPLETE,
3470 * then means some MDT has ever tried to
3471 * verify some remote MDT-object that resides
3472 * on this MDT, but this MDT failed to respond
3473 * such request. So means there may be some
3474 * remote name entry on other MDT that
3475 * references this object with another name,
3476 * so we cannot know whether this linkEA is
3477 * valid or not. So keep it there and maybe
3478 * resolved when next LFSCK run. */
3479 if (ns->ln_flags & LF_INCOMPLETE) {
3480 lfsck_object_put(env, parent);
3485 /* Create the lost parent as an orphan. */
3486 rc = lfsck_namespace_create_orphan_dir(env, com,
3489 lfsck_object_put(env, parent);
3497 /* Add the missing name entry to the parent. */
3498 rc = lfsck_namespace_insert_normal(env, com,
3499 parent, child, cname->ln_name);
3500 if (unlikely(rc == -EEXIST))
3501 /* Unfortunately, someone reused the
3502 * name under the parent by race. So we
3503 * have to remove the linkEA entry from
3504 * current child object. It means that
3505 * the LFSCK cannot recover the system
3506 * totally back to its original status,
3507 * but it is necessary to make the
3508 * current system to be consistent. */
3509 rc = lfsck_namespace_shrink_linkea(env,
3513 linkea_next_entry(&ldata);
3516 lfsck_object_put(env, parent);
3524 } /* !dt_object_exists(parent) */
3526 /* The linkEA entry with bad parent will be removed. */
3527 if (unlikely(!dt_try_as_dir(env, parent))) {
3528 lfsck_object_put(env, parent);
3529 rc = lfsck_namespace_shrink_linkea(env, com, child,
3530 &ldata, cname, pfid, true);
3540 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3541 (const struct dt_key *)cname->ln_name);
3542 if (rc != 0 && rc != -ENOENT) {
3543 lfsck_object_put(env, parent);
3549 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3550 /* It is the most common case that we
3551 * find the name entry corresponding
3552 * to the linkEA entry. */
3553 lfsck_object_put(env, parent);
3554 linkea_next_entry(&ldata);
3556 /* The name entry references another
3557 * MDT-object that may be created by
3558 * the LFSCK for repairing dangling
3559 * name entry. Try to replace it. */
3560 rc = lfsck_namespace_replace_cond(env, com,
3561 parent, child, cfid, cname);
3562 lfsck_object_put(env, parent);
3568 linkea_next_entry(&ldata);
3570 rc = lfsck_namespace_shrink_linkea(env,
3584 /* The following handles -ENOENT case */
3586 rc = dt_attr_get(env, child, la);
3590 /* If there is no name entry in the parent dir and the object
3591 * link count is fewer than the linkea entries count, then the
3592 * linkea entry should be removed. */
3593 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3594 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3595 parent, child, &ldata, cname, pfid);
3596 lfsck_object_put(env, parent);
3606 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3607 * MDT has ever tried to verify some remote MDT-object that
3608 * resides on this MDT, but this MDT failed to respond such
3609 * request. So means there may be some remote name entry on
3610 * other MDT that references this object with another name,
3611 * so we cannot know whether this linkEA is valid or not.
3612 * So keep it there and maybe resolved when next LFSCK run. */
3613 if (ns->ln_flags & LF_INCOMPLETE) {
3614 lfsck_object_put(env, parent);
3619 rc = lfsck_namespace_check_name(env, parent, child, cname);
3624 lfsck_object_put(env, parent);
3629 /* It is an invalid name entry, drop it. */
3630 if (unlikely(rc > 0)) {
3631 lfsck_object_put(env, parent);
3632 rc = lfsck_namespace_shrink_linkea(env, com, child,
3633 &ldata, cname, pfid, true);
3643 /* Add the missing name entry back to the namespace. */
3644 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3646 if (unlikely(rc == -ESTALE))
3647 /* It may happen when the remote object has been
3648 * removed, but the local MDT is not aware of that. */
3651 if (unlikely(rc == -EEXIST))
3652 /* Unfortunately, someone reused the name under the
3653 * parent by race. So we have to remove the linkEA
3654 * entry from current child object. It means that the
3655 * LFSCK cannot recover the system totally back to
3656 * its original status, but it is necessary to make
3657 * the current system to be consistent.
3659 * It also may be because of the LFSCK found some
3660 * internal status of create operation. Under such
3661 * case, nothing to be done. */
3662 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3663 parent, child, &ldata, cname, pfid);
3665 linkea_next_entry(&ldata);
3667 lfsck_object_put(env, parent);
3678 if (rc < 0 && rc != -ENODATA)
3681 if (rc == 0 && ldata.ld_leh != NULL)
3682 count = ldata.ld_leh->leh_reccount;
3685 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3686 * MDT has ever tried to verify some remote MDT-object that
3687 * resides on this MDT, but this MDT failed to respond such
3688 * request. So means there may be some remote name entry on
3689 * other MDT that references this object with another name,
3690 * so we cannot know whether this linkEA is valid or not.
3691 * So keep it there and maybe resolved when next LFSCK run. */
3692 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3693 (ldata.ld_leh == NULL ||
3694 !ldata.ld_leh->leh_overflow_time)) {
3695 /* If the child becomes orphan, then insert it into
3696 * the global .lustre/lost+found/MDTxxxx directory. */
3697 rc = lfsck_namespace_insert_orphan(env, com, child,
3703 ns->ln_mul_ref_repaired++;
3708 rc = dt_attr_get(env, child, la);
3712 if (la->la_nlink != 0 && la->la_nlink != count) {
3713 if (unlikely(!S_ISREG(lfsck_object_type(child)) &&
3714 !S_ISLNK(lfsck_object_type(child)))) {
3715 CDEBUG(D_LFSCK, "%s: namespace LFSCK finds "
3716 "the object "DFID"'s nlink count %d "
3717 "does not match linkEA count %d, "
3718 "type %o, skip it.\n",
3719 lfsck_lfsck2name(lfsck),
3720 PFID(lfsck_dto2fid(child)),
3721 la->la_nlink, count,
3722 lfsck_object_type(child));
3723 } else if (la->la_nlink < count &&
3724 likely(!ldata.ld_leh->leh_overflow_time)) {
3725 rc = lfsck_namespace_repair_nlink(env, com,
3728 ns->ln_objs_nlink_repaired++;
3736 if (la->la_nlink > 1)
3737 ns->ln_mul_linked_repaired++;
3746 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3747 struct lfsck_namespace *ns,
3748 __u64 checked_phase1,
3749 __u64 checked_phase2,
3751 __u32 time_phase2, bool dryrun)
3753 const char *postfix = dryrun ? "inconsistent" : "repaired";
3755 seq_printf(m, "checked_phase1: %llu\n"
3756 "checked_phase2: %llu\n"
3759 "failed_phase1: %llu\n"
3760 "failed_phase2: %llu\n"
3761 "directories: %llu\n"
3765 "multiple_linked_checked: %llu\n"
3766 "multiple_linked_%s: %llu\n"
3767 "unknown_inconsistency: %llu\n"
3768 "unmatched_pairs_%s: %llu\n"
3769 "dangling_%s: %llu\n"
3770 "multiple_referenced_%s: %llu\n"
3771 "bad_file_type_%s: %llu\n"
3772 "lost_dirent_%s: %llu\n"
3773 "local_lost_found_scanned: %llu\n"
3774 "local_lost_found_moved: %llu\n"
3775 "local_lost_found_skipped: %llu\n"
3776 "local_lost_found_failed: %llu\n"
3777 "striped_dirs_scanned: %llu\n"
3778 "striped_dirs_%s: %llu\n"
3779 "striped_dirs_failed: %llu\n"
3780 "striped_dirs_disabled: %llu\n"
3781 "striped_dirs_skipped: %llu\n"
3782 "striped_shards_scanned: %llu\n"
3783 "striped_shards_%s: %llu\n"
3784 "striped_shards_failed: %llu\n"
3785 "striped_shards_skipped: %llu\n"
3786 "name_hash_%s: %llu\n"
3787 "linkea_overflow_%s: %llu\n"
3788 "success_count: %u\n"
3789 "run_time_phase1: %u seconds\n"
3790 "run_time_phase2: %u seconds\n",
3793 dryrun ? "inconsistent" : "updated",
3794 ns->ln_items_repaired,
3795 dryrun ? "inconsistent" : "updated",
3796 ns->ln_objs_repaired_phase2,
3797 ns->ln_items_failed,
3798 ns->ln_objs_failed_phase2,
3799 ns->ln_dirs_checked,
3800 postfix, ns->ln_dirent_repaired,
3801 postfix, ns->ln_linkea_repaired,
3802 postfix, ns->ln_objs_nlink_repaired,
3803 ns->ln_mul_linked_checked,
3804 postfix, ns->ln_mul_linked_repaired,
3805 ns->ln_unknown_inconsistency,
3806 postfix, ns->ln_unmatched_pairs_repaired,
3807 postfix, ns->ln_dangling_repaired,
3808 postfix, ns->ln_mul_ref_repaired,
3809 postfix, ns->ln_bad_type_repaired,
3810 postfix, ns->ln_lost_dirent_repaired,
3811 ns->ln_local_lpf_scanned,
3812 ns->ln_local_lpf_moved,
3813 ns->ln_local_lpf_skipped,
3814 ns->ln_local_lpf_failed,
3815 ns->ln_striped_dirs_scanned,
3816 postfix, ns->ln_striped_dirs_repaired,
3817 ns->ln_striped_dirs_failed,
3818 ns->ln_striped_dirs_disabled,
3819 ns->ln_striped_dirs_skipped,
3820 ns->ln_striped_shards_scanned,
3821 postfix, ns->ln_striped_shards_repaired,
3822 ns->ln_striped_shards_failed,
3823 ns->ln_striped_shards_skipped,
3824 postfix, ns->ln_name_hash_repaired,
3825 dryrun ? "inconsistent" : "cleared",
3826 ns->ln_linkea_overflow_cleared,
3827 ns->ln_success_count,
3832 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3833 struct lfsck_component *com)
3835 struct lfsck_instance *lfsck = com->lc_lfsck;
3836 struct lfsck_namespace *ns = com->lc_file_ram;
3838 while (!list_empty(&lfsck->li_list_lmv)) {
3839 struct lfsck_lmv_unit *llu;
3840 struct lfsck_lmv *llmv;
3842 llu = list_entry(lfsck->li_list_lmv.next,
3843 struct lfsck_lmv_unit, llu_link);
3844 llmv = &llu->llu_lmv;
3846 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3847 "still in using: %u\n",
3848 atomic_read(&llmv->ll_ref));
3850 ns->ln_striped_dirs_skipped++;
3851 lfsck_lmv_put(env, llmv);
3855 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3856 struct lfsck_component *com,
3857 struct dt_object *obj)
3859 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3862 rc = dt_attr_get(env, obj, la);
3866 /* zero-linkEA object may be orphan, but it also maybe because
3867 * of upgrading. Currently, we cannot record it for double scan.
3868 * Because it may cause the LFSCK trace file to be too large. */
3870 /* "la_ctime" == 1 means that it has ever been removed from
3871 * backend /lost+found directory but not been added back to
3872 * the normal namespace yet. */
3874 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3875 unlikely(la->la_ctime == 1))
3876 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3877 LNTF_CHECK_LINKEA, true);
3882 /* namespace APIs */
3884 static int lfsck_namespace_reset(const struct lu_env *env,
3885 struct lfsck_component *com, bool init)
3887 struct lfsck_instance *lfsck = com->lc_lfsck;
3888 struct lfsck_namespace *ns = com->lc_file_ram;
3889 struct lfsck_assistant_data *lad = com->lc_data;
3890 struct dt_object *root;
3894 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3896 GOTO(log, rc = PTR_ERR(root));
3898 if (unlikely(!dt_try_as_dir(env, root)))
3899 GOTO(put, rc = -ENOTDIR);
3901 down_write(&com->lc_sem);
3903 memset(ns, 0, sizeof(*ns));
3905 __u32 count = ns->ln_success_count;
3906 __u64 last_time = ns->ln_time_last_complete;
3908 memset(ns, 0, sizeof(*ns));
3909 ns->ln_success_count = count;
3910 ns->ln_time_last_complete = last_time;
3912 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3913 ns->ln_status = LS_INIT;
3914 ns->ln_time_latest_reset = cfs_time_current_sec();
3916 rc = lfsck_load_one_trace_file(env, com, root, &com->lc_obj,
3917 &dt_lfsck_namespace_features,
3918 LFSCK_NAMESPACE, true);
3922 rc = lfsck_load_sub_trace_files(env, com, &dt_lfsck_namespace_features,
3923 LFSCK_NAMESPACE, true);
3927 lad->lad_incomplete = 0;
3928 CFS_RESET_BITMAP(lad->lad_bitmap);
3930 rc = lfsck_namespace_store(env, com);
3935 up_write(&com->lc_sem);
3938 lfsck_object_put(env, root);
3940 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3941 lfsck_lfsck2name(lfsck), rc);
3946 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3949 struct lfsck_namespace *ns = com->lc_file_ram;
3951 down_write(&com->lc_sem);
3953 com->lc_new_checked++;
3954 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3955 up_write(&com->lc_sem);
3958 static void lfsck_namespace_close_dir(const struct lu_env *env,
3959 struct lfsck_component *com)
3961 struct lfsck_namespace *ns = com->lc_file_ram;
3962 struct lfsck_assistant_data *lad = com->lc_data;
3963 struct lfsck_assistant_object *lso = NULL;
3964 struct lfsck_instance *lfsck = com->lc_lfsck;
3965 struct lfsck_lmv *llmv = lfsck->li_lmv;
3966 struct lfsck_namespace_req *lnr;
3968 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3969 bool wakeup = false;
3975 OBD_ALLOC(lnr, size);
3977 ns->ln_striped_dirs_skipped++;
3982 lso = lfsck_assistant_object_init(env, lfsck_dto2fid(lfsck->li_obj_dir),
3983 NULL, lfsck->li_pos_current.lp_oit_cookie, true);
3985 OBD_FREE(lnr, size);
3986 ns->ln_striped_dirs_skipped++;
3991 /* Generate a dummy request to indicate that all shards' name entry
3992 * in this striped directory has been scanned for the first time. */
3993 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3994 lnr->lnr_lar.lar_parent = lso;
3995 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3996 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3997 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3998 lnr->lnr_size = size;
3999 lnr->lnr_type = lso->lso_attr.la_mode;
4001 spin_lock(&lad->lad_lock);
4002 if (lad->lad_assistant_status < 0 ||
4003 unlikely(!thread_is_running(&lfsck->li_thread) ||
4004 !thread_is_running(&lad->lad_thread))) {
4005 spin_unlock(&lad->lad_lock);
4006 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4007 ns->ln_striped_dirs_skipped++;
4012 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4013 if (lad->lad_prefetched == 0)
4016 lad->lad_prefetched++;
4017 spin_unlock(&lad->lad_lock);
4019 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4024 static int lfsck_namespace_open_dir(const struct lu_env *env,
4025 struct lfsck_component *com)
4027 struct lfsck_instance *lfsck = com->lc_lfsck;
4028 struct lfsck_namespace *ns = com->lc_file_ram;
4029 struct lfsck_lmv *llmv = lfsck->li_lmv;
4036 if (llmv->ll_lmv_master) {
4037 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
4039 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
4040 lmv->lmv_master_mdt_index =
4041 lfsck_dev_idx(lfsck);
4042 ns->ln_flags |= LF_INCONSISTENT;
4043 llmv->ll_lmv_updated = 1;
4046 rc = lfsck_namespace_verify_stripe_slave(env, com,
4047 lfsck->li_obj_dir, llmv);
4050 RETURN(rc > 0 ? 0 : rc);
4053 static int lfsck_namespace_checkpoint(const struct lu_env *env,
4054 struct lfsck_component *com, bool init)
4056 struct lfsck_instance *lfsck = com->lc_lfsck;
4057 struct lfsck_namespace *ns = com->lc_file_ram;
4061 rc = lfsck_checkpoint_generic(env, com);
4066 down_write(&com->lc_sem);
4068 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
4070 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4071 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4072 HALF_SEC - lfsck->li_time_last_checkpoint);
4073 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4074 ns->ln_items_checked += com->lc_new_checked;
4075 com->lc_new_checked = 0;
4078 rc = lfsck_namespace_store(env, com);
4079 up_write(&com->lc_sem);
4082 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos [%llu"
4083 ", "DFID", %#llx], status = %d: rc = %d\n",
4084 lfsck_lfsck2name(lfsck), lfsck->li_pos_current.lp_oit_cookie,
4085 PFID(&lfsck->li_pos_current.lp_dir_parent),
4086 lfsck->li_pos_current.lp_dir_cookie, ns->ln_status, rc);
4088 return rc > 0 ? 0 : rc;
4091 static int lfsck_namespace_prep(const struct lu_env *env,
4092 struct lfsck_component *com,
4093 struct lfsck_start_param *lsp)
4095 struct lfsck_instance *lfsck = com->lc_lfsck;
4096 struct lfsck_namespace *ns = com->lc_file_ram;
4097 struct lfsck_position *pos = &com->lc_pos_start;
4100 rc = lfsck_namespace_load_bitmap(env, com);
4101 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
4102 rc = lfsck_namespace_reset(env, com, false);
4104 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
4107 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
4108 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4114 down_write(&com->lc_sem);
4115 ns->ln_time_latest_start = cfs_time_current_sec();
4116 spin_lock(&lfsck->li_lock);
4118 if (ns->ln_flags & LF_SCANNED_ONCE) {
4119 if (!lfsck->li_drop_dryrun ||
4120 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4121 ns->ln_status = LS_SCANNING_PHASE2;
4122 list_move_tail(&com->lc_link,
4123 &lfsck->li_list_double_scan);
4124 if (!list_empty(&com->lc_link_dir))
4125 list_del_init(&com->lc_link_dir);
4126 lfsck_pos_set_zero(pos);
4128 ns->ln_status = LS_SCANNING_PHASE1;
4129 ns->ln_run_time_phase1 = 0;
4130 ns->ln_run_time_phase2 = 0;
4131 ns->ln_items_checked = 0;
4132 ns->ln_items_repaired = 0;
4133 ns->ln_items_failed = 0;
4134 ns->ln_dirs_checked = 0;
4135 ns->ln_objs_checked_phase2 = 0;
4136 ns->ln_objs_repaired_phase2 = 0;
4137 ns->ln_objs_failed_phase2 = 0;
4138 ns->ln_objs_nlink_repaired = 0;
4139 ns->ln_dirent_repaired = 0;
4140 ns->ln_linkea_repaired = 0;
4141 ns->ln_mul_linked_checked = 0;
4142 ns->ln_mul_linked_repaired = 0;
4143 ns->ln_unknown_inconsistency = 0;
4144 ns->ln_unmatched_pairs_repaired = 0;
4145 ns->ln_dangling_repaired = 0;
4146 ns->ln_mul_ref_repaired = 0;
4147 ns->ln_bad_type_repaired = 0;
4148 ns->ln_lost_dirent_repaired = 0;
4149 ns->ln_striped_dirs_scanned = 0;
4150 ns->ln_striped_dirs_repaired = 0;
4151 ns->ln_striped_dirs_failed = 0;
4152 ns->ln_striped_dirs_disabled = 0;
4153 ns->ln_striped_dirs_skipped = 0;
4154 ns->ln_striped_shards_scanned = 0;
4155 ns->ln_striped_shards_repaired = 0;
4156 ns->ln_striped_shards_failed = 0;
4157 ns->ln_striped_shards_skipped = 0;
4158 ns->ln_name_hash_repaired = 0;
4159 fid_zero(&ns->ln_fid_latest_scanned_phase2);
4160 if (list_empty(&com->lc_link_dir))
4161 list_add_tail(&com->lc_link_dir,
4162 &lfsck->li_list_dir);
4163 *pos = ns->ln_pos_first_inconsistent;
4166 ns->ln_status = LS_SCANNING_PHASE1;
4167 if (list_empty(&com->lc_link_dir))
4168 list_add_tail(&com->lc_link_dir,
4169 &lfsck->li_list_dir);
4170 if (!lfsck->li_drop_dryrun ||
4171 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4172 *pos = ns->ln_pos_last_checkpoint;
4173 pos->lp_oit_cookie++;
4175 *pos = ns->ln_pos_first_inconsistent;
4179 spin_unlock(&lfsck->li_lock);
4180 up_write(&com->lc_sem);
4182 rc = lfsck_start_assistant(env, com, lsp);
4184 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos [%llu, "
4185 DFID", %#llx]: rc = %d\n",
4186 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4187 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4192 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4193 struct lfsck_component *com,
4194 struct dt_object *obj)
4196 struct lfsck_thread_info *info = lfsck_env_info(env);
4197 struct lfsck_namespace *ns = com->lc_file_ram;
4198 struct lfsck_instance *lfsck = com->lc_lfsck;
4199 const struct lu_fid *fid = lfsck_dto2fid(obj);
4200 struct lu_fid *pfid = &info->lti_fid2;
4201 struct lu_name *cname = &info->lti_name;
4202 struct lu_seq_range *range = &info->lti_range;
4203 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4204 struct linkea_data ldata = { NULL };
4205 __u32 idx = lfsck_dev_idx(lfsck);
4209 rc = lfsck_links_read(env, obj, &ldata);
4213 /* -EINVAL means crashed linkEA, should be verified. */
4214 if (rc == -EINVAL) {
4215 rc = lfsck_namespace_trace_update(env, com, fid,
4216 LNTF_CHECK_LINKEA, true);
4218 struct lustre_handle lh = { 0 };
4220 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4221 MDS_INODELOCK_UPDATE |
4222 MDS_INODELOCK_XATTR, LCK_EX);
4224 rc = lfsck_namespace_links_remove(env, com,
4226 lfsck_ibits_unlock(&lh, LCK_EX);
4230 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4233 if (rc == -ENODATA || unlikely(!ldata.ld_leh->leh_reccount)) {
4234 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4242 /* Record multiple-linked object. */
4243 if (ldata.ld_leh->leh_reccount > 1) {
4244 rc = lfsck_namespace_trace_update(env, com, fid,
4245 LNTF_CHECK_LINKEA, true);
4250 linkea_first_entry(&ldata);
4251 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4252 if (!fid_is_sane(pfid)) {
4253 rc = lfsck_namespace_trace_update(env, com, fid,
4254 LNTF_CHECK_PARENT, true);
4256 fld_range_set_mdt(range);
4257 rc = fld_local_lookup(env, ss->ss_server_fld,
4258 fid_seq(pfid), range);
4259 if ((rc == -ENOENT) ||
4260 (rc == 0 && range->lsr_index != idx))
4261 rc = lfsck_namespace_trace_update(env, com, fid,
4262 LNTF_CHECK_LINKEA, true);
4264 rc = lfsck_namespace_check_for_double_scan(env, com,
4271 down_write(&com->lc_sem);
4272 if (S_ISDIR(lfsck_object_type(obj)))
4273 ns->ln_dirs_checked++;
4275 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4276 up_write(&com->lc_sem);
4281 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4282 struct lfsck_component *com,
4283 struct lfsck_assistant_object *lso,
4284 struct lu_dirent *ent, __u16 type)
4286 struct lfsck_assistant_data *lad = com->lc_data;
4287 struct lfsck_instance *lfsck = com->lc_lfsck;
4288 struct lfsck_namespace_req *lnr;
4289 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4290 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4291 struct ptlrpc_thread *athread = &lad->lad_thread;
4292 struct l_wait_info lwi = { 0 };
4293 bool wakeup = false;
4295 l_wait_event(mthread->t_ctl_waitq,
4296 lad->lad_prefetched < bk->lb_async_windows ||
4297 !thread_is_running(mthread) ||
4298 !thread_is_running(athread),
4301 if (unlikely(!thread_is_running(mthread) ||
4302 !thread_is_running(athread)))
4305 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4308 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, lso, ent, type);
4310 struct lfsck_namespace *ns = com->lc_file_ram;
4312 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4313 return PTR_ERR(lnr);
4316 spin_lock(&lad->lad_lock);
4317 if (lad->lad_assistant_status < 0 ||
4318 unlikely(!thread_is_running(mthread) ||
4319 !thread_is_running(athread))) {
4320 spin_unlock(&lad->lad_lock);
4321 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4322 return lad->lad_assistant_status;
4325 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4326 if (lad->lad_prefetched == 0)
4329 lad->lad_prefetched++;
4330 spin_unlock(&lad->lad_lock);
4332 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4334 down_write(&com->lc_sem);
4335 com->lc_new_checked++;
4336 up_write(&com->lc_sem);
4341 static int lfsck_namespace_post(const struct lu_env *env,
4342 struct lfsck_component *com,
4343 int result, bool init)
4345 struct lfsck_instance *lfsck = com->lc_lfsck;
4346 struct lfsck_namespace *ns = com->lc_file_ram;
4350 lfsck_post_generic(env, com, &result);
4352 down_write(&com->lc_sem);
4353 lfsck_namespace_release_lmv(env, com);
4355 spin_lock(&lfsck->li_lock);
4357 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4359 ns->ln_status = LS_SCANNING_PHASE2;
4360 ns->ln_flags |= LF_SCANNED_ONCE;
4361 ns->ln_flags &= ~LF_UPGRADE;
4362 list_del_init(&com->lc_link_dir);
4363 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4364 } else if (result == 0) {
4365 if (lfsck->li_status != 0)
4366 ns->ln_status = lfsck->li_status;
4368 ns->ln_status = LS_STOPPED;
4369 if (ns->ln_status != LS_PAUSED) {
4370 list_del_init(&com->lc_link_dir);
4371 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4374 ns->ln_status = LS_FAILED;
4375 list_del_init(&com->lc_link_dir);
4376 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4378 spin_unlock(&lfsck->li_lock);
4381 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4382 HALF_SEC - lfsck->li_time_last_checkpoint);
4383 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4384 ns->ln_items_checked += com->lc_new_checked;
4385 com->lc_new_checked = 0;
4388 rc = lfsck_namespace_store(env, com);
4389 up_write(&com->lc_sem);
4391 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4392 lfsck_lfsck2name(lfsck), rc);
4398 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4401 struct lfsck_instance *lfsck = com->lc_lfsck;
4402 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4403 struct lfsck_namespace *ns = com->lc_file_ram;
4405 down_read(&com->lc_sem);
4406 seq_printf(m, "name: lfsck_namespace\n"
4412 lfsck_status2name(ns->ln_status));
4414 lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4416 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4418 lfsck_time_dump(m, ns->ln_time_last_complete, "last_completed");
4420 lfsck_time_dump(m, ns->ln_time_latest_start, "latest_start");
4422 lfsck_time_dump(m, ns->ln_time_last_checkpoint, "last_checkpoint");
4424 lfsck_pos_dump(m, &ns->ln_pos_latest_start, "latest_start_position");
4426 lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4427 "last_checkpoint_position");
4429 lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4430 "first_failure_position");
4432 if (ns->ln_status == LS_SCANNING_PHASE1) {
4433 struct lfsck_position pos;
4434 cfs_duration_t duration = cfs_time_current() -
4435 lfsck->li_time_last_checkpoint;
4436 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4437 __u64 speed = checked;
4438 __u64 new_checked = com->lc_new_checked *
4439 msecs_to_jiffies(MSEC_PER_SEC);
4440 __u32 rtime = ns->ln_run_time_phase1 +
4441 cfs_duration_sec(duration + HALF_SEC);
4444 new_checked = div64_s64(new_checked, duration);
4447 speed = div64_s64(speed, rtime);
4449 lfsck_namespace_dump_statistics(m, ns, checked, 0, rtime, 0,
4450 bk->lb_param & LPF_DRYRUN);
4451 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4452 "average_speed_phase2: N/A\n"
4453 "average_speed_total: %llu items/sec\n"
4454 "real_time_speed_phase1: %llu items/sec\n"
4455 "real_time_speed_phase2: N/A\n",
4460 if (likely(lfsck->li_di_oit)) {
4461 const struct dt_it_ops *iops =
4462 &lfsck->li_obj_oit->do_index_ops->dio_it;
4464 /* The low layer otable-based iteration position may NOT
4465 * exactly match the namespace-based directory traversal
4466 * cookie. Generally, it is not a serious issue. But the
4467 * caller should NOT make assumption on that. */
4468 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4469 if (!lfsck->li_current_oit_processed)
4470 pos.lp_oit_cookie--;
4472 spin_lock(&lfsck->li_lock);
4473 if (lfsck->li_di_dir) {
4474 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4475 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4476 fid_zero(&pos.lp_dir_parent);
4477 pos.lp_dir_cookie = 0;
4480 *lfsck_dto2fid(lfsck->li_obj_dir);
4483 fid_zero(&pos.lp_dir_parent);
4484 pos.lp_dir_cookie = 0;
4486 spin_unlock(&lfsck->li_lock);
4488 pos = ns->ln_pos_last_checkpoint;
4491 lfsck_pos_dump(m, &pos, "current_position");
4492 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4493 cfs_duration_t duration = cfs_time_current() -
4494 com->lc_time_last_checkpoint;
4495 __u64 checked = ns->ln_objs_checked_phase2 +
4496 com->lc_new_checked;
4497 __u64 speed1 = ns->ln_items_checked;
4498 __u64 speed2 = checked;
4499 __u64 speed0 = speed1 + speed2;
4500 __u64 new_checked = com->lc_new_checked *
4501 msecs_to_jiffies(MSEC_PER_SEC);
4502 __u32 rtime = ns->ln_run_time_phase2 +
4503 cfs_duration_sec(duration + HALF_SEC);
4504 __u32 time0 = ns->ln_run_time_phase1 + rtime;
4507 new_checked = div64_s64(new_checked, duration);
4509 if (ns->ln_run_time_phase1 != 0)
4510 speed1 = div64_s64(speed1, ns->ln_run_time_phase1);
4511 else if (ns->ln_items_checked != 0)
4515 speed2 = div64_s64(speed2, rtime);
4516 else if (checked != 0)
4520 speed0 = div64_s64(speed0, time0);
4522 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4524 ns->ln_run_time_phase1, rtime,
4525 bk->lb_param & LPF_DRYRUN);
4526 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4527 "average_speed_phase2: %llu objs/sec\n"
4528 "average_speed_total: %llu items/sec\n"
4529 "real_time_speed_phase1: N/A\n"
4530 "real_time_speed_phase2: %llu objs/sec\n"
4531 "current_position: "DFID"\n",
4536 PFID(&ns->ln_fid_latest_scanned_phase2));
4538 __u64 speed1 = ns->ln_items_checked;
4539 __u64 speed2 = ns->ln_objs_checked_phase2;
4540 __u64 speed0 = speed1 + speed2;
4541 __u32 time0 = ns->ln_run_time_phase1 + ns->ln_run_time_phase2;
4543 if (ns->ln_run_time_phase1 != 0)
4544 speed1 = div64_s64(speed1, ns->ln_run_time_phase1);
4545 else if (ns->ln_items_checked != 0)
4548 if (ns->ln_run_time_phase2 != 0)
4549 speed2 = div64_s64(speed2, ns->ln_run_time_phase2);
4550 else if (ns->ln_objs_checked_phase2 != 0)
4554 speed0 = div64_s64(speed0, time0);
4556 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4557 ns->ln_objs_checked_phase2,
4558 ns->ln_run_time_phase1,
4559 ns->ln_run_time_phase2,
4560 bk->lb_param & LPF_DRYRUN);
4561 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4562 "average_speed_phase2: %llu objs/sec\n"
4563 "average_speed_total: %llu items/sec\n"
4564 "real_time_speed_phase1: N/A\n"
4565 "real_time_speed_phase2: N/A\n"
4566 "current_position: N/A\n",
4572 up_read(&com->lc_sem);
4575 static int lfsck_namespace_double_scan(const struct lu_env *env,
4576 struct lfsck_component *com)
4578 struct lfsck_namespace *ns = com->lc_file_ram;
4579 struct lfsck_assistant_data *lad = com->lc_data;
4580 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4581 struct lfsck_tgt_desc *ltd;
4582 struct lfsck_tgt_desc *next;
4585 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4586 if (thread_is_stopped(&lad->lad_thread)) {
4587 LASSERT(list_empty(&lad->lad_req_list));
4588 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4590 spin_lock(<ds->ltd_lock);
4591 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4592 ltd_namespace_phase_list) {
4593 list_del_init(<d->ltd_namespace_phase_list);
4595 spin_unlock(<ds->ltd_lock);
4601 static void lfsck_namespace_data_release(const struct lu_env *env,
4602 struct lfsck_component *com)
4604 struct lfsck_assistant_data *lad = com->lc_data;
4605 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4606 struct lfsck_tgt_desc *ltd;
4607 struct lfsck_tgt_desc *next;
4609 LASSERT(lad != NULL);
4610 LASSERT(thread_is_init(&lad->lad_thread) ||
4611 thread_is_stopped(&lad->lad_thread));
4612 LASSERT(list_empty(&lad->lad_req_list));
4614 com->lc_data = NULL;
4615 lfsck_namespace_release_lmv(env, com);
4617 spin_lock(<ds->ltd_lock);
4618 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4619 ltd_namespace_phase_list) {
4620 list_del_init(<d->ltd_namespace_phase_list);
4622 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4623 ltd_namespace_phase_list) {
4624 list_del_init(<d->ltd_namespace_phase_list);
4626 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4627 ltd_namespace_list) {
4628 list_del_init(<d->ltd_namespace_list);
4630 spin_unlock(<ds->ltd_lock);
4632 if (likely(lad->lad_bitmap != NULL))
4633 CFS_FREE_BITMAP(lad->lad_bitmap);
4638 static void lfsck_namespace_quit(const struct lu_env *env,
4639 struct lfsck_component *com)
4641 struct lfsck_assistant_data *lad = com->lc_data;
4642 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4643 struct lfsck_tgt_desc *ltd;
4644 struct lfsck_tgt_desc *next;
4646 LASSERT(lad != NULL);
4648 lfsck_quit_generic(env, com);
4650 LASSERT(thread_is_init(&lad->lad_thread) ||
4651 thread_is_stopped(&lad->lad_thread));
4652 LASSERT(list_empty(&lad->lad_req_list));
4654 lfsck_namespace_release_lmv(env, com);
4656 spin_lock(<ds->ltd_lock);
4657 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4658 ltd_namespace_phase_list) {
4659 list_del_init(<d->ltd_namespace_phase_list);
4661 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4662 ltd_namespace_phase_list) {
4663 list_del_init(<d->ltd_namespace_phase_list);
4665 spin_unlock(<ds->ltd_lock);
4668 static int lfsck_namespace_in_notify(const struct lu_env *env,
4669 struct lfsck_component *com,
4670 struct lfsck_request *lr)
4672 struct lfsck_instance *lfsck = com->lc_lfsck;
4673 struct lfsck_namespace *ns = com->lc_file_ram;
4674 struct lfsck_assistant_data *lad = com->lc_data;
4675 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4676 struct lfsck_tgt_desc *ltd;
4681 switch (lr->lr_event) {
4682 case LE_SET_LMV_MASTER: {
4683 struct dt_object *obj;
4685 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4687 RETURN(PTR_ERR(obj));
4689 if (likely(dt_object_exists(obj)))
4690 rc = lfsck_namespace_notify_lmv_master_local(env, com,
4693 lfsck_object_put(env, obj);
4695 RETURN(rc > 0 ? 0 : rc);
4697 case LE_SET_LMV_SLAVE: {
4698 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4699 ns->ln_striped_shards_repaired++;
4701 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4702 LNTF_RECHECK_NAME_HASH, true);
4704 RETURN(rc > 0 ? 0 : rc);
4706 case LE_PHASE1_DONE:
4707 case LE_PHASE2_DONE:
4714 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4715 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4716 lr->lr_index, lr->lr_status, lr->lr_flags2);
4718 spin_lock(<ds->ltd_lock);
4719 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
4721 spin_unlock(<ds->ltd_lock);
4726 list_del_init(<d->ltd_namespace_phase_list);
4727 switch (lr->lr_event) {
4728 case LE_PHASE1_DONE:
4729 if (lr->lr_status <= 0) {
4730 ltd->ltd_namespace_done = 1;
4731 list_del_init(<d->ltd_namespace_list);
4732 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4733 "phase1 for namespace LFSCK: rc = %d.\n",
4734 lfsck_lfsck2name(lfsck),
4735 ltd->ltd_index, lr->lr_status);
4736 ns->ln_flags |= LF_INCOMPLETE;
4741 if (lr->lr_flags2 & LF_INCOMPLETE)
4742 ns->ln_flags |= LF_INCOMPLETE;
4744 if (list_empty(<d->ltd_namespace_list))
4745 list_add_tail(<d->ltd_namespace_list,
4746 &lad->lad_mdt_list);
4747 list_add_tail(<d->ltd_namespace_phase_list,
4748 &lad->lad_mdt_phase2_list);
4750 case LE_PHASE2_DONE:
4751 ltd->ltd_namespace_done = 1;
4752 list_del_init(<d->ltd_namespace_list);
4756 ltd->ltd_namespace_done = 1;
4757 list_del_init(<d->ltd_namespace_list);
4758 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4760 "%s: the peer MDT %x exit namespace LFSCK\n",
4761 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4762 ns->ln_flags |= LF_INCOMPLETE;
4768 spin_unlock(<ds->ltd_lock);
4770 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4771 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4773 memset(stop, 0, sizeof(*stop));
4774 stop->ls_status = lr->lr_status;
4775 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4776 lfsck_stop(env, lfsck->li_bottom, stop);
4777 } else if (lfsck_phase2_next_ready(lad)) {
4778 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4784 static void lfsck_namespace_repaired(struct lfsck_namespace *ns, __u64 *count)
4786 *count += ns->ln_objs_nlink_repaired;
4787 *count += ns->ln_dirent_repaired;
4788 *count += ns->ln_linkea_repaired;
4789 *count += ns->ln_mul_linked_repaired;
4790 *count += ns->ln_unmatched_pairs_repaired;
4791 *count += ns->ln_dangling_repaired;
4792 *count += ns->ln_mul_ref_repaired;
4793 *count += ns->ln_bad_type_repaired;
4794 *count += ns->ln_lost_dirent_repaired;
4795 *count += ns->ln_striped_dirs_disabled;
4796 *count += ns->ln_striped_dirs_repaired;
4797 *count += ns->ln_striped_shards_repaired;
4798 *count += ns->ln_name_hash_repaired;
4799 *count += ns->ln_local_lpf_moved;
4802 static int lfsck_namespace_query_all(const struct lu_env *env,
4803 struct lfsck_component *com,
4804 __u32 *mdts_count, __u64 *repaired)
4806 struct lfsck_namespace *ns = com->lc_file_ram;
4807 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4808 struct lfsck_tgt_desc *ltd;
4813 rc = lfsck_query_all(env, com);
4817 down_read(<ds->ltd_rw_sem);
4818 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
4819 ltd = lfsck_ltd2tgt(ltds, idx);
4820 LASSERT(ltd != NULL);
4822 mdts_count[ltd->ltd_namespace_status]++;
4823 *repaired += ltd->ltd_namespace_repaired;
4825 up_read(<ds->ltd_rw_sem);
4827 down_read(&com->lc_sem);
4828 mdts_count[ns->ln_status]++;
4829 lfsck_namespace_repaired(ns, repaired);
4830 up_read(&com->lc_sem);
4835 static int lfsck_namespace_query(const struct lu_env *env,
4836 struct lfsck_component *com,
4837 struct lfsck_request *req,
4838 struct lfsck_reply *rep,
4839 struct lfsck_query *que, int idx)
4841 struct lfsck_namespace *ns = com->lc_file_ram;
4845 LASSERT(com->lc_lfsck->li_master);
4847 rc = lfsck_namespace_query_all(env, com,
4848 que->lu_mdts_count[idx],
4849 &que->lu_repaired[idx]);
4851 down_read(&com->lc_sem);
4852 rep->lr_status = ns->ln_status;
4853 if (req->lr_flags & LEF_QUERY_ALL)
4854 lfsck_namespace_repaired(ns, &rep->lr_repaired);
4855 up_read(&com->lc_sem);
4861 static struct lfsck_operations lfsck_namespace_ops = {
4862 .lfsck_reset = lfsck_namespace_reset,
4863 .lfsck_fail = lfsck_namespace_fail,
4864 .lfsck_close_dir = lfsck_namespace_close_dir,
4865 .lfsck_open_dir = lfsck_namespace_open_dir,
4866 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4867 .lfsck_prep = lfsck_namespace_prep,
4868 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4869 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4870 .lfsck_post = lfsck_namespace_post,
4871 .lfsck_dump = lfsck_namespace_dump,
4872 .lfsck_double_scan = lfsck_namespace_double_scan,
4873 .lfsck_data_release = lfsck_namespace_data_release,
4874 .lfsck_quit = lfsck_namespace_quit,
4875 .lfsck_in_notify = lfsck_namespace_in_notify,
4876 .lfsck_query = lfsck_namespace_query,
4880 * Repair dangling name entry.
4882 * For the name entry with dangling reference, we need to repare the
4883 * inconsistency according to the LFSCK sponsor's requirement:
4885 * 1) Keep the inconsistency there and report the inconsistency case,
4886 * then give the chance to the application to find related issues,
4887 * and the users can make the decision about how to handle it with
4888 * more human knownledge. (by default)
4890 * 2) Re-create the missing MDT-object with the FID information.
4892 * \param[in] env pointer to the thread context
4893 * \param[in] com pointer to the lfsck component
4894 * \param[in] parent pointer to the dir object that contains the dangling
4896 * \param[in] child pointer to the object corresponding to the dangling
4898 * \param[in] lnr pointer to the namespace request that contains the
4899 * name's name, parent object, parent's LMV, and ect.
4901 * \retval positive number if no need to repair
4902 * \retval zero for repaired successfully
4903 * \retval negative error number on failure
4905 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4906 struct lfsck_component *com,
4907 struct dt_object *parent,
4908 struct dt_object *child,
4909 struct lfsck_namespace_req *lnr)
4911 struct lfsck_thread_info *info = lfsck_env_info(env);
4912 struct lu_attr *la = &info->lti_la;
4913 struct dt_allocation_hint *hint = &info->lti_hint;
4914 struct dt_object_format *dof = &info->lti_dof;
4915 struct dt_insert_rec *rec = &info->lti_dt_rec;
4916 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4917 const struct lu_name *cname;
4918 const struct lu_fid *pfid = lfsck_dto2fid(parent);
4919 const struct lu_fid *cfid = lfsck_dto2fid(child);
4920 struct linkea_data ldata = { NULL };
4921 struct lfsck_lock_handle *llh = &info->lti_llh;
4922 struct lu_buf linkea_buf;
4923 struct lu_buf lmv_buf;
4924 struct lfsck_instance *lfsck = com->lc_lfsck;
4925 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4926 struct dt_device *dev = lfsck->li_next;
4927 struct thandle *th = NULL;
4929 __u16 type = lnr->lnr_type;
4933 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4934 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4939 if (!create || bk->lb_param & LPF_DRYRUN)
4942 /* We may need to create the sub-objects of the @child via LOD,
4943 * so make the modification based on lfsck->li_next. */
4945 parent = lfsck_object_locate(dev, parent);
4947 GOTO(log, rc = PTR_ERR(parent));
4949 if (unlikely(!dt_try_as_dir(env, parent)))
4950 GOTO(log, rc = -ENOTDIR);
4952 child = lfsck_object_locate(dev, child);
4954 GOTO(log, rc = PTR_ERR(child));
4956 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
4961 rc = lfsck_lock(env, lfsck, parent, lnr->lnr_name, llh,
4962 MDS_INODELOCK_UPDATE, LCK_PR);
4966 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4970 /* Set the ctime as zero, then others can know it is created for
4971 * repairing dangling name entry by LFSCK. And if the LFSCK made
4972 * wrong decision and the real MDT-object has been found later,
4973 * then the LFSCK has chance to fix the incosistency properly. */
4974 memset(la, 0, sizeof(*la));
4975 la->la_mode = (type & S_IFMT) | 0600;
4976 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4977 LA_ATIME | LA_MTIME | LA_CTIME;
4979 child->do_ops->do_ah_init(env, hint, parent, child,
4980 la->la_mode & S_IFMT);
4982 memset(dof, 0, sizeof(*dof));
4983 dof->dof_type = dt_mode_to_dft(type);
4984 /* If the target is a regular file, then the LFSCK will only create
4985 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4986 * OST-objects will be created when write open. */
4988 th = dt_trans_create(env, dev);
4990 GOTO(log, rc = PTR_ERR(th));
4992 /* 1a. create child. */
4993 rc = dt_declare_create(env, child, la, hint, dof, th);
4997 if (S_ISDIR(type)) {
4998 if (unlikely(!dt_try_as_dir(env, child)))
4999 GOTO(stop, rc = -ENOTDIR);
5001 /* 2a. increase child nlink */
5002 rc = dt_declare_ref_add(env, child, th);
5006 /* 3a. insert dot into child dir */
5007 rec->rec_type = S_IFDIR;
5008 rec->rec_fid = cfid;
5009 rc = dt_declare_insert(env, child,
5010 (const struct dt_rec *)rec,
5011 (const struct dt_key *)dot, th);
5015 /* 4a. insert dotdot into child dir */
5016 rec->rec_fid = pfid;
5017 rc = dt_declare_insert(env, child,
5018 (const struct dt_rec *)rec,
5019 (const struct dt_key *)dotdot, th);
5023 /* 5a. generate slave LMV EA. */
5024 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5027 idx = lfsck_shard_name_to_index(env,
5028 lnr->lnr_name, lnr->lnr_namelen,
5030 if (unlikely(idx < 0))
5031 GOTO(stop, rc = idx);
5033 *lmv2 = lnr->lnr_lmv->ll_lmv;
5034 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
5035 lmv2->lmv_master_mdt_index = idx;
5037 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
5038 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
5039 rc = dt_declare_xattr_set(env, child, &lmv_buf,
5040 XATTR_NAME_LMV, 0, th);
5046 /* 6a. insert linkEA for child */
5047 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
5048 ldata.ld_leh->leh_len);
5049 rc = dt_declare_xattr_set(env, child, &linkea_buf,
5050 XATTR_NAME_LINK, 0, th);
5054 rc = dt_trans_start_local(env, dev, th);
5056 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
5058 dt_write_lock(env, child, 0);
5059 /* 1b. create child */
5060 rc = dt_create(env, child, la, hint, dof, th);
5062 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
5064 if (S_ISDIR(type)) {
5065 /* 2b. increase child nlink */
5066 rc = dt_ref_add(env, child, th);
5070 /* 3b. insert dot into child dir */
5071 rec->rec_type = S_IFDIR;
5072 rec->rec_fid = cfid;
5073 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5074 (const struct dt_key *)dot, th, 1);
5078 /* 4b. insert dotdot into child dir */
5079 rec->rec_fid = pfid;
5080 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5081 (const struct dt_key *)dotdot, th, 1);
5085 /* 5b. generate slave LMV EA. */
5086 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5087 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
5094 /* 6b. insert linkEA for child. */
5095 rc = dt_xattr_set(env, child, &linkea_buf,
5096 XATTR_NAME_LINK, 0, th);
5101 dt_write_unlock(env, child);
5104 dt_trans_stop(env, dev, th);
5108 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
5109 "reference for: parent "DFID", child "DFID", type %u, "
5110 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
5111 PFID(pfid), PFID(cfid), type, cname->ln_name,
5112 create ? "Create the lost MDT-object as required" :
5113 "Keep the MDT-object there by default", rc);
5116 struct lfsck_namespace *ns = com->lc_file_ram;
5118 ns->ln_flags |= LF_INCONSISTENT;
5124 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
5125 struct lfsck_component *com,
5126 struct lfsck_assistant_req *lar)
5128 struct lfsck_thread_info *info = lfsck_env_info(env);
5129 struct lu_attr *la = &info->lti_la;
5130 struct lfsck_instance *lfsck = com->lc_lfsck;
5131 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5132 struct lfsck_namespace *ns = com->lc_file_ram;
5133 struct lfsck_assistant_data *lad = com->lc_data;
5134 struct linkea_data ldata = { NULL };
5135 const struct lu_name *cname;
5136 struct thandle *handle = NULL;
5137 struct lfsck_namespace_req *lnr =
5138 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5139 struct dt_object *dir = NULL;
5140 struct dt_object *obj = NULL;
5141 struct lfsck_assistant_object *lso = lar->lar_parent;
5142 const struct lu_fid *pfid = &lso->lso_fid;
5143 struct dt_device *dev = NULL;
5144 struct lustre_handle lh = { 0 };
5145 bool repaired = false;
5146 bool dtlocked = false;
5150 bool bad_hash = false;
5151 bool bad_linkea = false;
5155 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5162 if (lnr->lnr_attr & LUDA_UPGRADE) {
5163 ns->ln_flags |= LF_UPGRADE;
5164 ns->ln_dirent_repaired++;
5166 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5167 ns->ln_flags |= LF_INCONSISTENT;
5168 ns->ln_dirent_repaired++;
5172 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5173 if (strcmp(lnr->lnr_name, dotdot) != 0)
5176 rc = lfsck_namespace_trace_update(env, com, pfid,
5177 LNTF_CHECK_PARENT, true);
5182 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5183 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5184 " for the name entry %.*s under "DFID"\n",
5185 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5186 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5188 if (strcmp(lnr->lnr_name, dotdot) != 0)
5189 /* invalid FID means bad name entry, remove it. */
5190 type = LNIT_BAD_DIRENT;
5192 /* If the parent FID is invalid, we cannot remove
5193 * the ".." entry directly. */
5194 rc = lfsck_namespace_trace_update(env, com, pfid,
5195 LNTF_CHECK_PARENT, true);
5200 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5201 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5206 if (fid_seq_is_dot(fid_seq(&lnr->lnr_fid)))
5209 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5210 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5215 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5217 GOTO(out, rc = idx);
5219 if (idx == lfsck_dev_idx(lfsck)) {
5220 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5223 dev = lfsck->li_bottom;
5225 struct lfsck_tgt_desc *ltd;
5227 /* Usually, some local filesystem consistency verification
5228 * tools can guarantee the local namespace tree consistenct.
5229 * So the LFSCK will only verify the remote directory. */
5230 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5231 rc = lfsck_namespace_trace_update(env, com, pfid,
5232 LNTF_CHECK_PARENT, true);
5237 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
5238 if (unlikely(ltd == NULL)) {
5239 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5240 "did not join the namespace LFSCK\n",
5241 lfsck_lfsck2name(lfsck), idx);
5242 lfsck_lad_set_bitmap(env, com, idx);
5244 GOTO(out, rc = -ENODEV);
5250 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5252 GOTO(out, rc = PTR_ERR(obj));
5254 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5255 if (dt_object_exists(obj) == 0) {
5259 dir = lfsck_assistant_object_load(env, lfsck, lso);
5263 GOTO(trace, rc == -ENOENT ? 0 : rc);
5267 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5269 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5270 lnr->lnr_name, lnr->lnr_namelen)) {
5271 type = LNIT_BAD_DIRENT;
5276 type = LNIT_DANGLING;
5277 rc = lfsck_namespace_repair_dangling(env, com, dir,
5286 if (!(bk->lb_param & LPF_DRYRUN) && lad->lad_advance_lock) {
5289 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5290 MDS_INODELOCK_UPDATE |
5291 MDS_INODELOCK_XATTR, LCK_EX);
5295 handle = dt_trans_create(env, dev);
5297 GOTO(out, rc = PTR_ERR(handle));
5299 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5303 rc = dt_trans_start_local(env, dev, handle);
5307 dt_write_lock(env, obj, 0);
5311 rc = lfsck_links_read(env, obj, &ldata);
5312 if (unlikely(rc == -ENOENT)) {
5313 if (handle != NULL) {
5314 dt_write_unlock(env, obj);
5317 dt_trans_stop(env, dev, handle);
5320 lfsck_ibits_unlock(&lh, LCK_EX);
5323 /* It may happen when the remote object has been removed,
5324 * but the local MDT is not aware of that. */
5326 } else if (rc == 0) {
5327 count = ldata.ld_leh->leh_reccount;
5328 rc = linkea_links_find(&ldata, cname, pfid);
5330 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5331 if ((lfsck_object_type(obj) & S_IFMT) !=
5333 ns->ln_flags |= LF_INCONSISTENT;
5334 type = LNIT_BAD_TYPE;
5340 /* If the name entry hash does not match the slave striped
5341 * directory, and the name entry does not match also, then
5342 * it is quite possible that name entry is corrupted. */
5343 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5344 lnr->lnr_name, lnr->lnr_namelen)) {
5345 ns->ln_flags |= LF_INCONSISTENT;
5346 type = LNIT_BAD_DIRENT;
5351 /* If the file type stored in the name entry does not match
5352 * the file type claimed by the object, and the object does
5353 * not recognize the name entry, then it is quite possible
5354 * that the name entry is corrupted. */
5355 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5356 ns->ln_flags |= LF_INCONSISTENT;
5357 type = LNIT_BAD_DIRENT;
5362 /* For sub-dir object, we cannot make sure whether the sub-dir
5363 * back references the parent via ".." name entry correctly or
5364 * not in the LFSCK first-stage scanning. It may be that the
5365 * (remote) sub-dir ".." name entry has no parent FID after
5366 * file-level backup/restore and its linkEA may be wrong.
5367 * So under such case, we should replace the linkEA according
5368 * to current name entry. But this needs to be done during the
5369 * LFSCK second-stage scanning. The LFSCK will record the name
5370 * entry for further possible using. */
5374 } else if (unlikely(rc == -EINVAL)) {
5375 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5376 type = LNIT_BAD_TYPE;
5379 /* The magic crashed, we are not sure whether there are more
5380 * corrupt data in the linkea, so remove all linkea entries. */
5384 } else if (rc == -ENODATA) {
5385 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5386 type = LNIT_BAD_TYPE;
5393 if (bk->lb_param & LPF_DRYRUN) {
5395 ns->ln_flags |= LF_UPGRADE;
5397 ns->ln_flags |= LF_INCONSISTENT;
5398 ns->ln_linkea_repaired++;
5404 if (!lustre_handle_is_used(&lh)) {
5412 LASSERT(handle != NULL);
5415 dir = lfsck_assistant_object_load(env, lfsck, lso);
5419 GOTO(stop, rc == -ENOENT ? 0 : rc);
5423 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5428 if (!remove && newdata)
5429 ns->ln_flags |= LF_UPGRADE;
5430 else if (remove || !(ns->ln_flags & LF_UPGRADE))
5431 ns->ln_flags |= LF_INCONSISTENT;
5436 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle);
5437 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
5442 rc = linkea_data_new(&ldata,
5443 &lfsck_env_info(env)->lti_linkea_buf);
5448 rc = linkea_add_buf(&ldata, cname, pfid);
5450 rc = lfsck_links_write(env, obj, &ldata, handle);
5454 count = ldata.ld_leh->leh_reccount;
5455 if (!S_ISDIR(lfsck_object_type(obj)) ||
5456 !dt_object_remote(obj)) {
5457 ns->ln_linkea_repaired++;
5467 dt_write_unlock(env, obj);
5469 if (handle != NULL && !IS_ERR(handle))
5470 dt_trans_stop(env, dev, handle);
5473 lfsck_ibits_unlock(&lh, LCK_EX);
5475 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5476 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5477 lnr->lnr_name, lnr->lnr_namelen) &&
5478 type != LNIT_BAD_DIRENT) {
5479 ns->ln_flags |= LF_INCONSISTENT;
5483 dir = lfsck_assistant_object_load(env, lfsck, lso);
5487 GOTO(trace, rc == -ENOENT ? 0 : rc);
5491 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5492 lnr->lnr_lmv, lnr->lnr_name);
5498 if (type != LNIT_NONE && dir == NULL) {
5499 dir = lfsck_assistant_object_load(env, lfsck, lso);
5503 GOTO(trace, rc == -ENOENT ? 0 : rc);
5510 rc = lfsck_namespace_repair_dirent(env, com, dir,
5511 obj, lnr->lnr_name, lnr->lnr_name,
5512 lnr->lnr_type, true, false);
5516 case LNIT_BAD_DIRENT:
5518 /* XXX: This is a bad dirent, we do not know whether
5519 * the original name entry reference a regular
5520 * file or a directory, then keep the parent's
5521 * nlink count unchanged here. */
5522 rc = lfsck_namespace_repair_dirent(env, com, dir,
5523 obj, lnr->lnr_name, lnr->lnr_name,
5524 lnr->lnr_type, false, false);
5532 if (obj != NULL && count == 1 &&
5533 S_ISREG(lfsck_object_type(obj)))
5534 dt_attr_get(env, obj, la);
5538 down_write(&com->lc_sem);
5540 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5541 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5542 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid), PFID(pfid),
5543 lnr->lnr_namelen, lnr->lnr_name, rc);
5545 lfsck_namespace_record_failure(env, lfsck, ns);
5546 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5547 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5548 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5549 dev != NULL && dev != lfsck->li_bottom)
5550 lfsck_lad_set_bitmap(env, com, idx);
5552 if (!(bk->lb_param & LPF_FAILOUT))
5556 ns->ln_items_repaired++;
5558 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5559 "repaired the entry: "DFID", parent "DFID
5560 ", name %.*s, type %d\n",
5561 lfsck_lfsck2name(lfsck),
5562 PFID(&lnr->lnr_fid), PFID(pfid),
5563 lnr->lnr_namelen, lnr->lnr_name, type);
5567 ns->ln_dangling_repaired++;
5570 ns->ln_bad_type_repaired++;
5572 case LNIT_BAD_DIRENT:
5573 ns->ln_dirent_repaired++;
5579 if (bk->lb_param & LPF_DRYRUN &&
5580 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5581 lfsck_pos_fill(env, lfsck,
5582 &ns->ln_pos_first_inconsistent,
5587 ns->ln_name_hash_repaired++;
5589 /* Not count repeatedly. */
5591 ns->ln_items_repaired++;
5593 CDEBUG(D_LFSCK, "%s: namespace LFSCK "
5594 "assistant repaired the entry: "
5597 lfsck_lfsck2name(lfsck),
5598 PFID(&lnr->lnr_fid), PFID(pfid),
5599 lnr->lnr_namelen, lnr->lnr_name);
5602 if (bk->lb_param & LPF_DRYRUN &&
5603 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5604 lfsck_pos_fill(env, lfsck,
5605 &ns->ln_pos_first_inconsistent,
5612 if (count > 1 || la->la_nlink > 1)
5613 ns->ln_mul_linked_checked++;
5615 up_write(&com->lc_sem);
5617 if (obj != NULL && !IS_ERR(obj))
5618 lfsck_object_put(env, obj);
5620 if (dir != NULL && !IS_ERR(dir))
5621 lfsck_object_put(env, dir);
5623 lad->lad_advance_lock = bad_linkea;
5629 * Handle one orphan under the backend /lost+found directory
5631 * Insert the orphan FID into the namespace LFSCK trace file for further
5632 * processing (via the subsequent namespace LFSCK second-stage scanning).
5633 * At the same time, remove the orphan name entry from backend /lost+found
5634 * directory. There is an interval between the orphan name entry removed
5635 * from the backend /lost+found directory and the orphan FID in the LFSCK
5636 * trace file handled. In such interval, the LFSCK can be reset, then
5637 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5638 * To guarantee that the orphans can be found when LFSCK run next time
5639 * without e2fsck again, when remove the orphan name entry, the LFSCK
5640 * will set the orphan's ctime attribute as 1. Since normal applications
5641 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5642 * next time, it can record the object (that ctime is 1) in the namespace
5643 * LFSCK trace file during the first-stage scanning.
5645 * \param[in] env pointer to the thread context
5646 * \param[in] com pointer to the lfsck component
5647 * \param[in] parent pointer to the object for the backend /lost+found
5648 * \param[in] ent pointer to the name entry for the target under the
5649 * backend /lost+found
5651 * \retval positive for repaired
5652 * \retval 0 if needs to repair nothing
5653 * \retval negative error number on failure
5655 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5656 struct lfsck_component *com,
5657 struct dt_object *parent,
5658 struct lu_dirent *ent)
5660 struct lfsck_thread_info *info = lfsck_env_info(env);
5661 struct lu_fid *key = &info->lti_fid;
5662 struct lu_attr *la = &info->lti_la;
5663 struct lfsck_instance *lfsck = com->lc_lfsck;
5664 struct dt_object *obj;
5665 struct dt_device *dev = lfsck->li_bottom;
5666 struct dt_object *child = NULL;
5667 struct thandle *th = NULL;
5674 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5676 RETURN(PTR_ERR(child));
5678 LASSERT(dt_object_exists(child));
5679 LASSERT(!dt_object_remote(child));
5681 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5682 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5683 fid_cpu_to_be(key, &ent->lde_fid);
5684 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5685 (const struct dt_key *)key);
5688 flags |= LNTF_CHECK_ORPHAN;
5689 } else if (rc == -ENOENT) {
5690 flags = LNTF_CHECK_ORPHAN;
5695 th = dt_trans_create(env, dev);
5697 GOTO(out, rc = PTR_ERR(th));
5699 /* a1. remove name entry from backend /lost+found */
5700 rc = dt_declare_delete(env, parent,
5701 (const struct dt_key *)ent->lde_name, th);
5705 if (S_ISDIR(lfsck_object_type(child))) {
5706 /* a2. decrease parent's nlink */
5707 rc = dt_declare_ref_del(env, parent, th);
5713 /* a3. remove child's FID from the LFSCK trace file. */
5714 rc = dt_declare_delete(env, obj,
5715 (const struct dt_key *)key, th);
5719 /* a4. set child's ctime as 1 */
5720 memset(la, 0, sizeof(*la));
5722 la->la_valid = LA_CTIME;
5723 rc = dt_declare_attr_set(env, child, la, th);
5728 /* a5. insert child's FID into the LFSCK trace file. */
5729 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5730 (const struct dt_key *)key, th);
5734 rc = dt_trans_start_local(env, dev, th);
5738 /* b1. remove name entry from backend /lost+found */
5739 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th);
5743 if (S_ISDIR(lfsck_object_type(child))) {
5744 /* b2. decrease parent's nlink */
5745 dt_write_lock(env, parent, 0);
5746 rc = dt_ref_del(env, parent, th);
5747 dt_write_unlock(env, parent);
5753 /* a3. remove child's FID from the LFSCK trace file. */
5754 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
5758 /* b4. set child's ctime as 1 */
5759 rc = dt_attr_set(env, child, la, th);
5764 /* b5. insert child's FID into the LFSCK trace file. */
5765 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5766 (const struct dt_key *)key, th, 1);
5768 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5771 dt_trans_stop(env, dev, th);
5774 lfsck_object_put(env, child);
5780 * Handle orphans under the backend /lost+found directory
5782 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5783 * and put them under the backend /lost+found directory that is invisible
5784 * to client. The LFSCK will scan such directory, for the original client
5785 * visible orphans, add their fids into the namespace LFSCK trace file,
5786 * then the subsenquent namespace LFSCK second-stage scanning can handle
5787 * them as other objects to be double scanned: either move back to normal
5788 * namespace, or to the global visible orphan directory:
5789 * /ROOT/.lustre/lost+found/MDTxxxx/
5791 * \param[in] env pointer to the thread context
5792 * \param[in] com pointer to the lfsck component
5794 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5795 struct lfsck_component *com)
5797 struct lfsck_thread_info *info = lfsck_env_info(env);
5798 struct lu_dirent *ent =
5799 (struct lu_dirent *)info->lti_key;
5800 struct lu_seq_range *range = &info->lti_range;
5801 struct lfsck_instance *lfsck = com->lc_lfsck;
5802 struct ptlrpc_thread *thread = &lfsck->li_thread;
5803 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5804 struct lfsck_namespace *ns = com->lc_file_ram;
5805 struct dt_object *parent;
5806 const struct dt_it_ops *iops;
5808 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5810 __u32 idx = lfsck_dev_idx(lfsck);
5815 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5816 &LU_BACKEND_LPF_FID);
5817 if (IS_ERR(parent)) {
5818 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5819 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5823 /* It is normal that the /lost+found does not exist for ZFS backend. */
5824 if (!dt_object_exists(parent))
5827 if (unlikely(!dt_try_as_dir(env, parent)))
5828 GOTO(out, rc = -ENOTDIR);
5830 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5831 lfsck_lfsck2name(lfsck));
5833 com->lc_new_scanned = 0;
5834 iops = &parent->do_index_ops->dio_it;
5835 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE);
5837 GOTO(out, rc = PTR_ERR(di));
5839 rc = iops->load(env, di, 0);
5841 rc = iops->next(env, di);
5846 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5847 unlikely(!thread_is_running(thread)))
5850 rc = iops->rec(env, di, (struct dt_rec *)ent,
5851 LUDA_64BITHASH | LUDA_TYPE);
5853 rc = lfsck_unpack_ent(ent, &cookie, &type);
5855 if (unlikely(rc != 0)) {
5856 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5857 "/lost+found: rc = %d\n",
5858 lfsck_lfsck2name(lfsck), rc);
5863 /* skip dot and dotdot entries */
5864 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5867 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5870 if (fid_is_norm(&ent->lde_fid)) {
5871 fld_range_set_mdt(range);
5872 rc = fld_local_lookup(env, ss->ss_server_fld,
5873 fid_seq(&ent->lde_fid), range);
5876 } else if (idx != 0) {
5877 /* If the returned FID is IGIF, then there are three
5880 * 1) The object is upgraded from old Lustre-1.8 with
5881 * IGIF assigned to such object.
5882 * 2) The object is a backend local object and is
5883 * invisible to client.
5884 * 3) The object lost its LMV EA, and since there is
5885 * no FID-in-dirent for the orphan in the backend
5886 * /lost+found directory, then the low layer will
5887 * return IGIF for such object.
5889 * For MDTx (x != 0), it is either case 2) or case 3),
5890 * but from the LFSCK view, they are indistinguishable.
5891 * To be safe, the LFSCK will keep it there and report
5892 * some message, then the adminstrator can handle that
5895 * For MDT0, it is more possible the case 1). The LFSCK
5896 * will handle the orphan as an upgraded object. */
5897 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5898 "in the backend /lost+found on the MDT %04x, "
5899 "to be safe, skip it.\n",
5900 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5901 ent->lde_name, PFID(&ent->lde_fid), idx);
5905 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5908 down_write(&com->lc_sem);
5909 com->lc_new_scanned++;
5910 ns->ln_local_lpf_scanned++;
5912 ns->ln_local_lpf_moved++;
5914 ns->ln_local_lpf_skipped++;
5916 ns->ln_local_lpf_failed++;
5917 up_write(&com->lc_sem);
5919 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5923 lfsck_control_speed_by_self(com);
5924 if (unlikely(!thread_is_running(thread))) {
5929 rc = iops->next(env, di);
5933 iops->fini(env, di);
5938 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5939 lfsck_lfsck2name(lfsck), rc);
5941 lfsck_object_put(env, parent);
5945 * Rescan the striped directory after the master LMV EA reset.
5947 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5948 * the namespace LFSCK engine scan the striped directory for the first time,
5949 * it will be regarded as a normal directory. As the LFSCK processing, some
5950 * other LFSCK instance on other MDT will find the shard of this striped dir,
5951 * and find that the master MDT-object of the striped directory lost its LMV
5952 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5953 * notify the LFSCK instance on this MDT to rescan the striped directory.
5955 * \param[in] env pointer to the thread context
5956 * \param[in] com pointer to the lfsck component
5957 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5960 * \retval positive number for success
5961 * \retval 0 for LFSCK stopped/paused
5962 * \retval negative error number on failure
5964 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5965 struct lfsck_component *com,
5966 struct lfsck_lmv_unit *llu)
5968 struct lfsck_thread_info *info = lfsck_env_info(env);
5969 struct lfsck_instance *lfsck = com->lc_lfsck;
5970 struct lfsck_assistant_data *lad = com->lc_data;
5971 struct dt_object *dir;
5972 const struct dt_it_ops *iops;
5974 struct lu_dirent *ent =
5975 (struct lu_dirent *)info->lti_key;
5976 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5977 struct ptlrpc_thread *thread = &lfsck->li_thread;
5978 struct lfsck_assistant_object *lso = NULL;
5979 struct lfsck_namespace_req *lnr;
5980 struct lfsck_assistant_req *lar;
5985 LASSERT(list_empty(&lad->lad_req_list));
5987 lfsck->li_lmv = &llu->llu_lmv;
5988 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5989 rc = lfsck_open_dir(env, lfsck, 0);
5993 dir = lfsck->li_obj_dir;
5994 di = lfsck->li_di_dir;
5995 iops = &dir->do_index_ops->dio_it;
5997 rc = iops->rec(env, di, (struct dt_rec *)ent,
5998 lfsck->li_args_dir);
6000 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
6004 if (bk->lb_param & LPF_FAILOUT)
6010 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
6014 lso = lfsck_assistant_object_init(env,
6015 lfsck_dto2fid(dir), NULL,
6016 lfsck->li_pos_current.lp_oit_cookie, true);
6018 if (bk->lb_param & LPF_FAILOUT)
6019 GOTO(out, rc = PTR_ERR(lso));
6026 lnr = lfsck_namespace_assistant_req_init(lfsck, lso, ent, type);
6028 if (bk->lb_param & LPF_FAILOUT)
6029 GOTO(out, rc = PTR_ERR(lnr));
6034 lar = &lnr->lnr_lar;
6035 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6036 lfsck_namespace_assistant_req_fini(env, lar);
6037 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
6040 if (unlikely(!thread_is_running(thread)))
6044 rc = iops->next(env, di);
6048 if (lso != NULL && !IS_ERR(lso))
6049 lfsck_assistant_object_put(env, lso);
6051 lfsck_close_dir(env, lfsck, rc);
6055 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
6056 if (list_empty(&lad->lad_req_list))
6059 spin_lock(&lad->lad_lock);
6060 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
6062 list_del_init(&lar->lar_list);
6063 spin_unlock(&lad->lad_lock);
6065 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6066 lfsck_namespace_assistant_req_fini(env, lar);
6068 RETURN(rc == 0 ? 1 : rc);
6072 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
6073 struct lfsck_component *com,
6074 struct dt_object *obj, bool first)
6076 struct lfsck_instance *lfsck = com->lc_lfsck;
6077 struct ptlrpc_thread *thread = &lfsck->li_thread;
6078 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6079 struct lfsck_namespace *ns = com->lc_file_ram;
6080 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
6081 struct dt_object *target;
6089 di = iops->init(env, obj, 0);
6091 RETURN(PTR_ERR(di));
6094 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
6097 rc = iops->get(env, di, (const struct dt_key *)&fid);
6102 /* The start one either has been processed or does not exist,
6104 rc = iops->next(env, di);
6110 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6111 unlikely(!thread_is_running(thread)))
6114 key = iops->key(env, di);
6123 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
6124 if (!fid_is_sane(&fid)) {
6129 target = lfsck_object_find_bottom(env, lfsck, &fid);
6130 if (IS_ERR(target)) {
6131 rc = PTR_ERR(target);
6135 if (dt_object_exists(target)) {
6136 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
6138 rc = lfsck_namespace_double_scan_one(env, com,
6145 lfsck_object_put(env, target);
6148 down_write(&com->lc_sem);
6149 com->lc_new_checked++;
6150 com->lc_new_scanned++;
6152 ns->ln_fid_latest_scanned_phase2 = fid;
6155 ns->ln_objs_repaired_phase2++;
6157 ns->ln_objs_failed_phase2++;
6158 up_write(&com->lc_sem);
6160 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6163 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
6164 cfs_time_current())) &&
6165 com->lc_new_checked != 0) {
6166 down_write(&com->lc_sem);
6167 ns->ln_run_time_phase2 +=
6168 cfs_duration_sec(cfs_time_current() +
6169 HALF_SEC - com->lc_time_last_checkpoint);
6170 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6171 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6172 com->lc_new_checked = 0;
6173 lfsck_namespace_store(env, com);
6174 up_write(&com->lc_sem);
6176 com->lc_time_last_checkpoint = cfs_time_current();
6177 com->lc_time_next_checkpoint =
6178 com->lc_time_last_checkpoint +
6179 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6182 lfsck_control_speed_by_self(com);
6183 if (unlikely(!thread_is_running(thread)))
6186 rc = iops->next(env, di);
6195 iops->fini(env, di);
6200 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
6201 struct lfsck_component *com)
6203 struct lfsck_instance *lfsck = com->lc_lfsck;
6204 struct lfsck_namespace *ns = com->lc_file_ram;
6209 while (!list_empty(&lfsck->li_list_lmv)) {
6210 struct lfsck_lmv_unit *llu;
6212 spin_lock(&lfsck->li_lock);
6213 llu = list_entry(lfsck->li_list_lmv.next,
6214 struct lfsck_lmv_unit, llu_link);
6215 list_del_init(&llu->llu_link);
6216 spin_unlock(&lfsck->li_lock);
6218 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6223 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6224 lfsck_lfsck2name(lfsck));
6226 lfsck_namespace_scan_local_lpf(env, com);
6228 com->lc_new_checked = 0;
6229 com->lc_new_scanned = 0;
6230 com->lc_time_last_checkpoint = cfs_time_current();
6231 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6232 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6234 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6235 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6236 com->lc_sub_trace_objs[i].lsto_obj, true);
6237 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6238 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6239 com->lc_sub_trace_objs[i].lsto_obj, false);
6241 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6242 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6247 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6248 struct lfsck_component *com,
6249 struct lfsck_position *pos)
6251 struct lfsck_assistant_data *lad = com->lc_data;
6252 struct lfsck_namespace_req *lnr;
6254 if (((struct lfsck_namespace *)(com->lc_file_ram))->ln_status !=
6258 if (list_empty(&lad->lad_req_list))
6261 lnr = list_entry(lad->lad_req_list.next,
6262 struct lfsck_namespace_req,
6264 pos->lp_oit_cookie = lnr->lnr_lar.lar_parent->lso_oit_cookie;
6265 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6266 pos->lp_dir_parent = lnr->lnr_lar.lar_parent->lso_fid;
6269 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6270 struct lfsck_component *com,
6273 struct lfsck_instance *lfsck = com->lc_lfsck;
6274 struct lfsck_namespace *ns = com->lc_file_ram;
6276 down_write(&com->lc_sem);
6277 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6278 HALF_SEC - com->lc_time_last_checkpoint);
6279 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6280 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6281 com->lc_new_checked = 0;
6284 if (ns->ln_flags & LF_INCOMPLETE)
6285 ns->ln_status = LS_PARTIAL;
6287 ns->ln_status = LS_COMPLETED;
6288 ns->ln_flags &= ~LF_SCANNED_ONCE;
6289 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6290 ns->ln_flags &= ~LF_INCONSISTENT;
6291 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6292 ns->ln_success_count++;
6293 } else if (rc == 0) {
6294 if (lfsck->li_status != 0)
6295 ns->ln_status = lfsck->li_status;
6297 ns->ln_status = LS_STOPPED;
6299 ns->ln_status = LS_FAILED;
6302 rc = lfsck_namespace_store(env, com);
6303 up_write(&com->lc_sem);
6309 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6310 struct ptlrpc_request *req,
6314 struct lfsck_async_interpret_args *laia = args;
6315 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6317 ltd->ltd_synced_failures = 1;
6324 * Notify remote LFSCK instances about former failures.
6326 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6327 * some LFSCK verification requests (maybe because of network issues or the MDT
6328 * itself trouble). During the respond gap the MDT may missed some name entries
6329 * verification, then the MDT cannot know whether related MDT-objects have been
6330 * referenced by related name entries or not, then in the second-stage scanning,
6331 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6332 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6333 * name entry for repairing the orphan.
6335 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6336 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6337 * have ever missed some name entries verification and should skip the handling
6338 * for orphan MDT-objects.
6340 * \param[in] env pointer to the thread context
6341 * \param[in] com pointer to the lfsck component
6342 * \param[in] lr pointer to the lfsck request
6344 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6345 struct lfsck_component *com,
6346 struct lfsck_request *lr)
6348 struct lfsck_async_interpret_args *laia =
6349 &lfsck_env_info(env)->lti_laia2;
6350 struct lfsck_assistant_data *lad = com->lc_data;
6351 struct lfsck_namespace *ns = com->lc_file_ram;
6352 struct lfsck_instance *lfsck = com->lc_lfsck;
6353 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6354 struct lfsck_tgt_desc *ltd;
6355 struct ptlrpc_request_set *set;
6360 if (!lad->lad_incomplete)
6363 set = ptlrpc_prep_set();
6365 GOTO(out, rc = -ENOMEM);
6367 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6368 memset(laia, 0, sizeof(*laia));
6369 lad->lad_touch_gen++;
6371 down_read(<ds->ltd_rw_sem);
6372 cfs_foreach_bit(lad->lad_bitmap, idx) {
6373 ltd = lfsck_ltd2tgt(ltds, idx);
6377 laia->laia_ltd = ltd;
6378 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6379 lfsck_namespace_assistant_sync_failures_interpret,
6380 laia, LFSCK_NOTIFY);
6382 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6383 "to sync failure with MDT %x: rc = %d\n",
6384 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6386 up_read(<ds->ltd_rw_sem);
6388 rc = ptlrpc_set_wait(set);
6389 ptlrpc_set_destroy(set);
6395 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6396 "to sync failure with MDTs, and related MDTs "
6397 "may handle orphan improperly: rc = %d\n",
6398 lfsck_lfsck2name(lfsck), rc);
6403 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6404 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6405 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6406 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6407 .la_double_scan_result = lfsck_namespace_double_scan_result,
6408 .la_req_fini = lfsck_namespace_assistant_req_fini,
6409 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6413 * Verify the specified linkEA entry for the given directory object.
6414 * If the object has no such linkEA entry or it has more other linkEA
6415 * entries, then re-generate the linkEA with the given information.
6417 * \param[in] env pointer to the thread context
6418 * \param[in] obj pointer to the dt_object to be handled
6419 * \param[in] cname the name for the child in the parent directory
6420 * \param[in] pfid the parent directory's FID for the linkEA
6422 * \retval 0 for success
6423 * \retval negative error number on failure
6425 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6426 const struct lu_name *cname, const struct lu_fid *pfid)
6428 struct dt_device *dev = lfsck_obj2dev(obj);
6429 struct linkea_data ldata = { NULL };
6430 struct lu_buf linkea_buf;
6433 int fl = LU_XATTR_CREATE;
6437 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6439 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6440 if (rc == -ENODATA) {
6442 } else if (rc == 0) {
6443 fl = LU_XATTR_REPLACE;
6444 if (ldata.ld_leh->leh_reccount != 1) {
6447 rc = linkea_links_find(&ldata, cname, pfid);
6456 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
6461 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6462 ldata.ld_leh->leh_len);
6463 th = dt_trans_create(env, dev);
6465 RETURN(PTR_ERR(th));
6467 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6468 XATTR_NAME_LINK, fl, th);
6472 rc = dt_trans_start_local(env, dev, th);
6476 dt_write_lock(env, obj, 0);
6477 rc = dt_xattr_set(env, obj, &linkea_buf,
6478 XATTR_NAME_LINK, fl, th);
6479 dt_write_unlock(env, obj);
6484 dt_trans_stop(env, dev, th);
6489 * Get the name and parent directory's FID from the first linkEA entry.
6491 * \param[in] env pointer to the thread context
6492 * \param[in] obj pointer to the object which get linkEA from
6493 * \param[out] name pointer to the buffer to hold the name
6494 * in the first linkEA entry
6495 * \param[out] pfid pointer to the buffer to hold the parent
6496 * directory's FID in the first linkEA entry
6498 * \retval 0 for success
6499 * \retval negative error number on failure
6501 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6502 char *name, struct lu_fid *pfid)
6504 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6505 struct linkea_data ldata = { NULL };
6508 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6512 linkea_first_entry(&ldata);
6513 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6514 /* To guarantee the 'name' is terminated with '0'. */
6515 memcpy(name, cname->ln_name, cname->ln_namelen);
6516 name[cname->ln_namelen] = 0;
6522 * Update the object's name entry with the given FID.
6524 * \param[in] env pointer to the thread context
6525 * \param[in] lfsck pointer to the lfsck instance
6526 * \param[in] dir pointer to the directory that holds
6528 * \param[in] name the name for the entry to be updated
6529 * \param[in] fid the new FID for the name entry referenced
6530 * \param[in] type the type for the name entry to be updated
6532 * \retval 0 for success
6533 * \retval negative error number on failure
6535 int lfsck_update_name_entry(const struct lu_env *env,
6536 struct lfsck_instance *lfsck,
6537 struct dt_object *dir, const char *name,
6538 const struct lu_fid *fid, __u32 type)
6540 struct lfsck_thread_info *info = lfsck_env_info(env);
6541 struct dt_insert_rec *rec = &info->lti_dt_rec;
6542 struct lfsck_lock_handle *llh = &info->lti_llh;
6543 struct dt_device *dev = lfsck_obj2dev(dir);
6549 rc = lfsck_lock(env, lfsck, dir, name, llh,
6550 MDS_INODELOCK_UPDATE, LCK_PW);
6554 th = dt_trans_create(env, dev);
6556 GOTO(unlock, rc = PTR_ERR(th));
6558 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6562 rec->rec_type = type;
6564 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6565 (const struct dt_key *)name, th);
6569 rc = dt_declare_ref_add(env, dir, th);
6573 rc = dt_trans_start_local(env, dev, th);
6577 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
6578 if (rc == -ENOENT) {
6586 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6587 (const struct dt_key *)name, th, 1);
6588 if (rc == 0 && S_ISDIR(type) && !exists) {
6589 dt_write_lock(env, dir, 0);
6590 rc = dt_ref_add(env, dir, th);
6591 dt_write_unlock(env, dir);
6597 dt_trans_stop(env, dev, th);
6601 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6602 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6603 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6608 int lfsck_namespace_setup(const struct lu_env *env,
6609 struct lfsck_instance *lfsck)
6611 struct lfsck_component *com;
6612 struct lfsck_namespace *ns;
6613 struct dt_object *root = NULL;
6614 struct dt_object *obj;
6619 LASSERT(lfsck->li_master);
6625 INIT_LIST_HEAD(&com->lc_link);
6626 INIT_LIST_HEAD(&com->lc_link_dir);
6627 init_rwsem(&com->lc_sem);
6628 atomic_set(&com->lc_ref, 1);
6629 com->lc_lfsck = lfsck;
6630 com->lc_type = LFSCK_TYPE_NAMESPACE;
6631 com->lc_ops = &lfsck_namespace_ops;
6632 com->lc_data = lfsck_assistant_data_init(
6633 &lfsck_namespace_assistant_ops,
6635 if (com->lc_data == NULL)
6636 GOTO(out, rc = -ENOMEM);
6638 com->lc_file_size = sizeof(struct lfsck_namespace);
6639 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6640 if (com->lc_file_ram == NULL)
6641 GOTO(out, rc = -ENOMEM);
6643 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6644 if (com->lc_file_disk == NULL)
6645 GOTO(out, rc = -ENOMEM);
6647 for (i = 0; i < LFSCK_STF_COUNT; i++)
6648 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6650 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6652 GOTO(out, rc = PTR_ERR(root));
6654 if (unlikely(!dt_try_as_dir(env, root)))
6655 GOTO(out, rc = -ENOTDIR);
6657 obj = local_index_find_or_create(env, lfsck->li_los, root,
6659 S_IFREG | S_IRUGO | S_IWUSR,
6660 &dt_lfsck_namespace_features);
6662 GOTO(out, rc = PTR_ERR(obj));
6665 rc = lfsck_namespace_load(env, com);
6667 rc = lfsck_namespace_init(env, com);
6669 rc = lfsck_namespace_reset(env, com, true);
6671 rc = lfsck_load_sub_trace_files(env, com,
6672 &dt_lfsck_namespace_features, LFSCK_NAMESPACE, false);
6676 ns = com->lc_file_ram;
6677 switch (ns->ln_status) {
6682 spin_lock(&lfsck->li_lock);
6683 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6684 spin_unlock(&lfsck->li_lock);
6687 CERROR("%s: unknown lfsck_namespace status %d\n",
6688 lfsck_lfsck2name(lfsck), ns->ln_status);
6690 case LS_SCANNING_PHASE1:
6691 case LS_SCANNING_PHASE2:
6692 /* No need to store the status to disk right now.
6693 * If the system crashed before the status stored,
6694 * it will be loaded back when next time. */
6695 ns->ln_status = LS_CRASHED;
6699 spin_lock(&lfsck->li_lock);
6700 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6701 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6702 spin_unlock(&lfsck->li_lock);
6709 if (root != NULL && !IS_ERR(root))
6710 lfsck_object_put(env, root);
6712 lfsck_component_cleanup(env, com);
6713 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6714 lfsck_lfsck2name(lfsck), rc);