4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
42 #include "lfsck_internal.h"
44 #define LFSCK_NAMESPACE_MAGIC 0xA0629D03
46 enum lfsck_nameentry_check {
47 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
48 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
49 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
52 static const char lfsck_namespace_name[] = "lfsck_namespace";
54 static struct lfsck_namespace_req *
55 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
56 struct lu_dirent *ent, __u16 type)
58 struct lfsck_namespace_req *lnr;
61 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
64 return ERR_PTR(-ENOMEM);
66 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
67 lnr->lnr_obj = lfsck_object_get(lfsck->li_obj_dir);
68 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
69 lnr->lnr_fid = ent->lde_fid;
70 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
71 lnr->lnr_dir_cookie = ent->lde_hash;
72 lnr->lnr_attr = ent->lde_attrs;
75 lnr->lnr_namelen = ent->lde_namelen;
76 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
81 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
82 struct lfsck_assistant_req *lar)
84 struct lfsck_namespace_req *lnr =
85 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
87 if (lnr->lnr_lmv != NULL)
88 lfsck_lmv_put(env, lnr->lnr_lmv);
90 lu_object_put(env, &lnr->lnr_obj->do_lu);
91 OBD_FREE(lnr, lnr->lnr_size);
94 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
95 struct lfsck_namespace *src)
97 dst->ln_magic = le32_to_cpu(src->ln_magic);
98 dst->ln_status = le32_to_cpu(src->ln_status);
99 dst->ln_flags = le32_to_cpu(src->ln_flags);
100 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
101 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
102 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
103 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
104 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
105 dst->ln_time_last_checkpoint =
106 le64_to_cpu(src->ln_time_last_checkpoint);
107 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
108 &src->ln_pos_latest_start);
109 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
110 &src->ln_pos_last_checkpoint);
111 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
112 &src->ln_pos_first_inconsistent);
113 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
114 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
115 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
116 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
117 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
118 dst->ln_objs_repaired_phase2 =
119 le64_to_cpu(src->ln_objs_repaired_phase2);
120 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
121 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
122 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
123 &src->ln_fid_latest_scanned_phase2);
124 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
125 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
126 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
127 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
128 dst->ln_unknown_inconsistency =
129 le64_to_cpu(src->ln_unknown_inconsistency);
130 dst->ln_unmatched_pairs_repaired =
131 le64_to_cpu(src->ln_unmatched_pairs_repaired);
132 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
133 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
134 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
135 dst->ln_lost_dirent_repaired =
136 le64_to_cpu(src->ln_lost_dirent_repaired);
137 dst->ln_striped_dirs_scanned =
138 le64_to_cpu(src->ln_striped_dirs_scanned);
139 dst->ln_striped_dirs_repaired =
140 le64_to_cpu(src->ln_striped_dirs_repaired);
141 dst->ln_striped_dirs_failed =
142 le64_to_cpu(src->ln_striped_dirs_failed);
143 dst->ln_striped_dirs_disabled =
144 le64_to_cpu(src->ln_striped_dirs_disabled);
145 dst->ln_striped_dirs_skipped =
146 le64_to_cpu(src->ln_striped_dirs_skipped);
147 dst->ln_striped_shards_scanned =
148 le64_to_cpu(src->ln_striped_shards_scanned);
149 dst->ln_striped_shards_repaired =
150 le64_to_cpu(src->ln_striped_shards_repaired);
151 dst->ln_striped_shards_failed =
152 le64_to_cpu(src->ln_striped_shards_failed);
153 dst->ln_striped_shards_skipped =
154 le64_to_cpu(src->ln_striped_shards_skipped);
155 dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
156 dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
157 dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
158 dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
159 dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
160 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
163 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
164 struct lfsck_namespace *src)
166 dst->ln_magic = cpu_to_le32(src->ln_magic);
167 dst->ln_status = cpu_to_le32(src->ln_status);
168 dst->ln_flags = cpu_to_le32(src->ln_flags);
169 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
170 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
171 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
172 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
173 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
174 dst->ln_time_last_checkpoint =
175 cpu_to_le64(src->ln_time_last_checkpoint);
176 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
177 &src->ln_pos_latest_start);
178 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
179 &src->ln_pos_last_checkpoint);
180 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
181 &src->ln_pos_first_inconsistent);
182 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
183 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
184 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
185 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
186 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
187 dst->ln_objs_repaired_phase2 =
188 cpu_to_le64(src->ln_objs_repaired_phase2);
189 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
190 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
191 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
192 &src->ln_fid_latest_scanned_phase2);
193 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
194 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
195 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
196 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
197 dst->ln_unknown_inconsistency =
198 cpu_to_le64(src->ln_unknown_inconsistency);
199 dst->ln_unmatched_pairs_repaired =
200 cpu_to_le64(src->ln_unmatched_pairs_repaired);
201 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
202 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
203 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
204 dst->ln_lost_dirent_repaired =
205 cpu_to_le64(src->ln_lost_dirent_repaired);
206 dst->ln_striped_dirs_scanned =
207 cpu_to_le64(src->ln_striped_dirs_scanned);
208 dst->ln_striped_dirs_repaired =
209 cpu_to_le64(src->ln_striped_dirs_repaired);
210 dst->ln_striped_dirs_failed =
211 cpu_to_le64(src->ln_striped_dirs_failed);
212 dst->ln_striped_dirs_disabled =
213 cpu_to_le64(src->ln_striped_dirs_disabled);
214 dst->ln_striped_dirs_skipped =
215 cpu_to_le64(src->ln_striped_dirs_skipped);
216 dst->ln_striped_shards_scanned =
217 cpu_to_le64(src->ln_striped_shards_scanned);
218 dst->ln_striped_shards_repaired =
219 cpu_to_le64(src->ln_striped_shards_repaired);
220 dst->ln_striped_shards_failed =
221 cpu_to_le64(src->ln_striped_shards_failed);
222 dst->ln_striped_shards_skipped =
223 cpu_to_le64(src->ln_striped_shards_skipped);
224 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
225 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
226 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
227 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
228 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
229 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
232 static void lfsck_namespace_record_failure(const struct lu_env *env,
233 struct lfsck_instance *lfsck,
234 struct lfsck_namespace *ns)
236 struct lfsck_position pos;
238 ns->ln_items_failed++;
239 lfsck_pos_fill(env, lfsck, &pos, false);
240 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
241 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
242 ns->ln_pos_first_inconsistent = pos;
244 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
245 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
246 lfsck_lfsck2name(lfsck),
247 ns->ln_pos_first_inconsistent.lp_oit_cookie,
248 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
249 ns->ln_pos_first_inconsistent.lp_dir_cookie);
254 * Load the MDT bitmap from the lfsck_namespace trace file.
256 * \param[in] env pointer to the thread context
257 * \param[in] com pointer to the lfsck component
259 * \retval 0 for success
260 * \retval negative error number on failure or data corruption
262 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
263 struct lfsck_component *com)
265 struct dt_object *obj = com->lc_obj;
266 struct lfsck_assistant_data *lad = com->lc_data;
267 struct lfsck_namespace *ns = com->lc_file_ram;
268 cfs_bitmap_t *bitmap = lad->lad_bitmap;
274 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
276 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
278 nbits = ns->ln_bitmap_size;
280 if (unlikely(nbits < BITS_PER_LONG))
281 nbits = BITS_PER_LONG;
283 if (nbits > bitmap->size) {
284 __u32 new_bits = bitmap->size;
285 cfs_bitmap_t *new_bitmap;
287 while (new_bits < nbits)
290 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
291 if (new_bitmap == NULL)
294 lad->lad_bitmap = new_bitmap;
295 CFS_FREE_BITMAP(bitmap);
299 if (ns->ln_bitmap_size == 0) {
300 lad->lad_incomplete = 0;
301 CFS_RESET_BITMAP(bitmap);
306 size = (ns->ln_bitmap_size + 7) >> 3;
307 rc = dt_xattr_get(env, obj,
308 lfsck_buf_get(env, bitmap->data, size),
309 XATTR_NAME_LFSCK_BITMAP, BYPASS_CAPA);
311 RETURN(rc >= 0 ? -EINVAL : rc);
313 if (cfs_bitmap_check_empty(bitmap))
314 lad->lad_incomplete = 0;
316 lad->lad_incomplete = 1;
322 * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
323 * \retval 0: succeed.
324 * \retval -ve: failed cases.
326 static int lfsck_namespace_load(const struct lu_env *env,
327 struct lfsck_component *com)
329 int len = com->lc_file_size;
332 rc = dt_xattr_get(env, com->lc_obj,
333 lfsck_buf_get(env, com->lc_file_disk, len),
334 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
336 struct lfsck_namespace *ns = com->lc_file_ram;
338 lfsck_namespace_le_to_cpu(ns,
339 (struct lfsck_namespace *)com->lc_file_disk);
340 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
341 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
342 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
343 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
348 } else if (rc != -ENODATA) {
349 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
350 "expected = %d: rc = %d\n",
351 lfsck_lfsck2name(com->lc_lfsck), len, rc);
358 static int lfsck_namespace_store(const struct lu_env *env,
359 struct lfsck_component *com)
361 struct dt_object *obj = com->lc_obj;
362 struct lfsck_instance *lfsck = com->lc_lfsck;
363 struct lfsck_namespace *ns = com->lc_file_ram;
364 struct lfsck_assistant_data *lad = com->lc_data;
365 cfs_bitmap_t *bitmap = NULL;
366 struct thandle *handle;
368 int len = com->lc_file_size;
373 bitmap = lad->lad_bitmap;
374 nbits = bitmap->size;
377 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
380 ns->ln_bitmap_size = nbits;
381 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
383 handle = dt_trans_create(env, lfsck->li_bottom);
385 GOTO(log, rc = PTR_ERR(handle));
387 rc = dt_declare_xattr_set(env, obj,
388 lfsck_buf_get(env, com->lc_file_disk, len),
389 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
393 if (bitmap != NULL) {
394 rc = dt_declare_xattr_set(env, obj,
395 lfsck_buf_get(env, bitmap->data, nbits >> 3),
396 XATTR_NAME_LFSCK_BITMAP, 0, handle);
401 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
405 rc = dt_xattr_set(env, obj,
406 lfsck_buf_get(env, com->lc_file_disk, len),
407 XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
408 if (rc == 0 && bitmap != NULL)
409 rc = dt_xattr_set(env, obj,
410 lfsck_buf_get(env, bitmap->data, nbits >> 3),
411 XATTR_NAME_LFSCK_BITMAP, 0, handle,
417 dt_trans_stop(env, lfsck->li_bottom, handle);
421 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
422 lfsck_lfsck2name(lfsck), rc);
426 static int lfsck_namespace_init(const struct lu_env *env,
427 struct lfsck_component *com)
429 struct lfsck_namespace *ns = com->lc_file_ram;
432 memset(ns, 0, sizeof(*ns));
433 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
434 ns->ln_status = LS_INIT;
435 down_write(&com->lc_sem);
436 rc = lfsck_namespace_store(env, com);
437 up_write(&com->lc_sem);
442 * Update the namespace LFSCK trace file for the given @fid
444 * \param[in] env pointer to the thread context
445 * \param[in] com pointer to the lfsck component
446 * \param[in] fid the fid which flags to be updated in the lfsck
448 * \param[in] add true if add new flags, otherwise remove flags
450 * \retval 0 for succeed or nothing to be done
451 * \retval negative error number on failure
453 int lfsck_namespace_trace_update(const struct lu_env *env,
454 struct lfsck_component *com,
455 const struct lu_fid *fid,
456 const __u8 flags, bool add)
458 struct lfsck_instance *lfsck = com->lc_lfsck;
459 struct dt_object *obj = com->lc_obj;
460 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
461 struct dt_device *dev = lfsck->li_bottom;
462 struct thandle *th = NULL;
470 down_write(&com->lc_sem);
471 fid_cpu_to_be(key, fid);
472 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
473 (const struct dt_key *)key, BYPASS_CAPA);
476 GOTO(unlock, rc = 0);
480 } else if (rc == 0) {
482 if ((old & flags) == flags)
483 GOTO(unlock, rc = 0);
487 if ((old & flags) == 0)
488 GOTO(unlock, rc = 0);
496 th = dt_trans_create(env, dev);
498 GOTO(log, rc = PTR_ERR(th));
501 rc = dt_declare_delete(env, obj,
502 (const struct dt_key *)key, th);
508 rc = dt_declare_insert(env, obj,
509 (const struct dt_rec *)&new,
510 (const struct dt_key *)key, th);
515 rc = dt_trans_start_local(env, dev, th);
520 rc = dt_delete(env, obj, (const struct dt_key *)key,
527 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
528 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
536 if (th != NULL && !IS_ERR(th))
537 dt_trans_stop(env, dev, th);
539 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
540 "trace file, flags %x, old %x, new %x: rc = %d\n",
541 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
542 (__u32)flags, (__u32)old, (__u32)new, rc);
545 up_write(&com->lc_sem);
550 int lfsck_namespace_check_exist(const struct lu_env *env,
551 struct dt_object *dir,
552 struct dt_object *obj, const char *name)
554 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
558 if (unlikely(lfsck_is_dead_obj(obj)))
559 RETURN(LFSCK_NAMEENTRY_DEAD);
561 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
562 (const struct dt_key *)name, BYPASS_CAPA);
564 RETURN(LFSCK_NAMEENTRY_REMOVED);
569 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
570 RETURN(LFSCK_NAMEENTRY_RECREATED);
575 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
576 struct dt_object *obj,
577 struct thandle *handle)
581 /* For destroying all invalid linkEA entries. */
582 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
586 /* For insert new linkEA entry. */
587 rc = dt_declare_xattr_set(env, obj,
588 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
589 XATTR_NAME_LINK, 0, handle);
593 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
594 struct linkea_data *ldata)
598 if (ldata->ld_buf->lb_buf == NULL)
601 if (!dt_object_exists(obj))
604 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
606 /* Buf was too small, figure out what we need. */
607 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
612 lu_buf_realloc(ldata->ld_buf, rc);
613 if (ldata->ld_buf->lb_buf == NULL)
616 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
621 rc = linkea_init(ldata);
627 * Remove linkEA for the given object.
629 * The caller should take the ldlm lock before the calling.
631 * \param[in] env pointer to the thread context
632 * \param[in] com pointer to the lfsck component
633 * \param[in] obj pointer to the dt_object to be handled
635 * \retval 0 for repaired cases
636 * \retval negative error number on failure
638 static int lfsck_namespace_links_remove(const struct lu_env *env,
639 struct lfsck_component *com,
640 struct dt_object *obj)
642 struct lfsck_instance *lfsck = com->lc_lfsck;
643 struct dt_device *dev = lfsck->li_bottom;
644 struct thandle *th = NULL;
648 LASSERT(dt_object_remote(obj) == 0);
650 th = dt_trans_create(env, dev);
652 GOTO(log, rc = PTR_ERR(th));
654 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
658 rc = dt_trans_start_local(env, dev, th);
662 dt_write_lock(env, obj, 0);
663 if (unlikely(lfsck_is_dead_obj(obj)))
664 GOTO(unlock, rc = -ENOENT);
666 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
667 GOTO(unlock, rc = 0);
669 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
674 dt_write_unlock(env, obj);
677 dt_trans_stop(env, dev, th);
680 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
681 "for the object "DFID": rc = %d\n",
682 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
685 struct lfsck_namespace *ns = com->lc_file_ram;
687 ns->ln_flags |= LF_INCONSISTENT;
693 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
694 struct linkea_data *ldata, struct thandle *handle)
696 const struct lu_buf *buf = lfsck_buf_get_const(env,
697 ldata->ld_buf->lb_buf,
698 ldata->ld_leh->leh_len);
700 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
704 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
705 struct lu_name *cname,
709 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
710 /* To guarantee the 'name' is terminated with '0'. */
711 memcpy(buf, cname->ln_name, cname->ln_namelen);
712 buf[cname->ln_namelen] = 0;
713 cname->ln_name = buf;
716 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
717 struct lu_name *cname,
721 struct link_ea_entry *oldlee;
725 oldlee = ldata->ld_lee;
726 oldlen = ldata->ld_reclen;
727 linkea_next_entry(ldata);
728 while (ldata->ld_lee != NULL) {
729 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
730 ldata->ld_lee->lee_reclen[1];
731 if (unlikely(ldata->ld_reclen == oldlen &&
732 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
737 linkea_del_buf(ldata, cname);
739 linkea_next_entry(ldata);
742 ldata->ld_lee = oldlee;
743 ldata->ld_reclen = oldlen;
749 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
751 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
752 * with the given type to generate the name, the detailed rules for name
753 * have been described as following.
755 * The function also generates the linkEA corresponding to the name entry
756 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
758 * \param[in] env pointer to the thread context
759 * \param[in] com pointer to the lfsck component
760 * \param[in] orphan pointer to the orphan MDT-object
761 * \param[in] infix additional information for the orphan name, such as
762 * the FID for original
763 * \param[in] type the type for describing why the orphan MDT-object is
764 * created. The rules are as following:
766 * type "D": The MDT-object is a directory, it may knows its parent
767 * but because there is no valid linkEA, the LFSCK cannot
768 * know where to put it back to the namespace.
769 * type "O": The MDT-object has no linkEA, and there is no name
770 * entry that references the MDT-object.
772 * type "S": The orphan MDT-object is a shard of a striped directory
774 * \see lfsck_layout_recreate_parent() for more types.
776 * The orphan name will be like:
777 * ${FID}-${infix}-${type}-${conflict_version}
779 * \param[out] count if some others inserted some linkEA entries by race,
780 * then return the linkEA entries count.
782 * \retval positive number for repaired cases
783 * \retval 0 if needs to repair nothing
784 * \retval negative error number on failure
786 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
787 struct lfsck_component *com,
788 struct dt_object *orphan,
789 const char *infix, const char *type,
792 struct lfsck_thread_info *info = lfsck_env_info(env);
793 struct lu_name *cname = &info->lti_name;
794 struct dt_insert_rec *rec = &info->lti_dt_rec;
795 struct lu_fid *tfid = &info->lti_fid5;
796 struct lu_attr *la = &info->lti_la3;
797 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
798 const struct lu_fid *pfid;
799 struct lfsck_instance *lfsck = com->lc_lfsck;
800 struct dt_device *dev = lfsck->li_bottom;
801 struct dt_object *parent;
802 struct thandle *th = NULL;
803 struct lustre_handle plh = { 0 };
804 struct lustre_handle clh = { 0 };
805 struct linkea_data ldata = { 0 };
806 struct lu_buf linkea_buf;
813 cname->ln_name = NULL;
814 /* Create .lustre/lost+found/MDTxxxx when needed. */
815 if (unlikely(lfsck->li_lpf_obj == NULL)) {
816 rc = lfsck_create_lpf(env, lfsck);
821 parent = lfsck->li_lpf_obj;
822 pfid = lfsck_dto2fid(parent);
824 /* Hold update lock on the parent to prevent others to access. */
825 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
826 MDS_INODELOCK_UPDATE, LCK_EX);
831 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
832 PFID(cfid), infix, type, idx++);
833 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
834 (const struct dt_key *)info->lti_key,
836 if (rc != 0 && rc != -ENOENT)
839 if (unlikely(rc == 0 && lu_fid_eq(cfid, tfid)))
841 } while (rc == 0 && !exist);
843 cname->ln_name = info->lti_key;
844 cname->ln_namelen = namelen;
845 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
849 rc = linkea_add_buf(&ldata, cname, pfid);
853 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
854 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
859 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
860 ldata.ld_leh->leh_len);
861 th = dt_trans_create(env, dev);
863 GOTO(log, rc = PTR_ERR(th));
865 if (S_ISDIR(lfsck_object_type(orphan))) {
866 rc = dt_declare_delete(env, orphan,
867 (const struct dt_key *)dotdot, th);
871 rec->rec_type = S_IFDIR;
873 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
874 (const struct dt_key *)dotdot, th);
879 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
880 XATTR_NAME_LINK, 0, th);
885 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
887 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
888 (const struct dt_key *)cname->ln_name,
893 if (S_ISDIR(rec->rec_type)) {
894 rc = dt_declare_ref_add(env, parent, th);
900 memset(la, 0, sizeof(*la));
901 la->la_ctime = cfs_time_current_sec();
902 la->la_valid = LA_CTIME;
903 rc = dt_declare_attr_set(env, orphan, la, th);
907 rc = dt_trans_start_local(env, dev, th);
911 dt_write_lock(env, orphan, 0);
912 rc = lfsck_links_read(env, orphan, &ldata);
913 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
914 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
915 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
916 GOTO(unlock, rc = 1);
918 if (S_ISDIR(lfsck_object_type(orphan))) {
919 rc = dt_delete(env, orphan,
920 (const struct dt_key *)dotdot, th,
925 rec->rec_type = S_IFDIR;
927 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
928 (const struct dt_key *)dotdot, th,
934 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
937 if (rc == 0 && count != NULL)
938 *count = ldata.ld_leh->leh_reccount;
942 dt_write_unlock(env, orphan);
944 if (rc == 0 && !exist) {
945 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
947 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
948 (const struct dt_key *)cname->ln_name,
950 if (rc == 0 && S_ISDIR(rec->rec_type)) {
951 dt_write_lock(env, parent, 0);
952 rc = dt_ref_add(env, parent, th);
953 dt_write_unlock(env, parent);
960 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
962 GOTO(stop, rc = (rc == 0 ? 1 : rc));
965 dt_write_unlock(env, orphan);
968 dt_trans_stop(env, dev, th);
971 lfsck_ibits_unlock(&clh, LCK_EX);
972 lfsck_ibits_unlock(&plh, LCK_EX);
973 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
974 "object "DFID", name = %s: rc = %d\n",
975 lfsck_lfsck2name(lfsck), PFID(cfid),
976 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
979 struct lfsck_namespace *ns = com->lc_file_ram;
981 ns->ln_flags |= LF_INCONSISTENT;
988 * Add the specified name entry back to namespace.
990 * If there is a linkEA entry that back references a name entry under
991 * some parent directory, but such parent directory does not have the
992 * claimed name entry. On the other hand, the linkEA entries count is
993 * not larger than the MDT-object's hard link count. Under such case,
994 * it is quite possible that the name entry is lost. Then the LFSCK
995 * should add the name entry back to the namespace.
997 * \param[in] env pointer to the thread context
998 * \param[in] com pointer to the lfsck component
999 * \param[in] parent pointer to the directory under which the name entry
1000 * will be inserted into
1001 * \param[in] child pointer to the object referenced by the name entry
1002 * that to be inserted into the parent
1003 * \param[in] name the name for the child in the parent directory
1005 * \retval positive number for repaired cases
1006 * \retval 0 if nothing to be repaired
1007 * \retval negative error number on failure
1009 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1010 struct lfsck_component *com,
1011 struct dt_object *parent,
1012 struct dt_object *child,
1015 struct lfsck_thread_info *info = lfsck_env_info(env);
1016 struct lu_attr *la = &info->lti_la;
1017 struct dt_insert_rec *rec = &info->lti_dt_rec;
1018 struct lfsck_instance *lfsck = com->lc_lfsck;
1019 struct dt_device *dev = lfsck->li_next;
1020 struct thandle *th = NULL;
1021 struct lustre_handle lh = { 0 };
1025 if (unlikely(!dt_try_as_dir(env, parent)))
1026 GOTO(log, rc = -ENOTDIR);
1028 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1031 /* Hold update lock on the parent to prevent others to access. */
1032 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1033 MDS_INODELOCK_UPDATE, LCK_EX);
1037 th = dt_trans_create(env, dev);
1039 GOTO(unlock, rc = PTR_ERR(th));
1041 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1042 rec->rec_fid = lfsck_dto2fid(child);
1043 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1044 (const struct dt_key *)name, th);
1048 if (S_ISDIR(rec->rec_type)) {
1049 rc = dt_declare_ref_add(env, parent, th);
1054 memset(la, 0, sizeof(*la));
1055 la->la_ctime = cfs_time_current_sec();
1056 la->la_valid = LA_CTIME;
1057 rc = dt_declare_attr_set(env, parent, la, th);
1061 rc = dt_declare_attr_set(env, child, la, th);
1065 rc = dt_trans_start_local(env, dev, th);
1069 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1070 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1074 if (S_ISDIR(rec->rec_type)) {
1075 dt_write_lock(env, parent, 0);
1076 rc = dt_ref_add(env, parent, th);
1077 dt_write_unlock(env, parent);
1082 la->la_ctime = cfs_time_current_sec();
1083 rc = dt_attr_set(env, parent, la, th, BYPASS_CAPA);
1087 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
1089 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1092 dt_trans_stop(env, dev, th);
1095 lfsck_ibits_unlock(&lh, LCK_EX);
1098 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1099 "the name %s and type %o to the parent "DFID": rc = %d\n",
1100 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1101 lfsck_object_type(child) & S_IFMT,
1102 PFID(lfsck_dto2fid(parent)), rc);
1105 struct lfsck_namespace *ns = com->lc_file_ram;
1107 ns->ln_flags |= LF_INCONSISTENT;
1109 ns->ln_lost_dirent_repaired++;
1116 * Create the specified orphan MDT-object on remote MDT.
1118 * The LFSCK instance on this MDT will send LFSCK RPC to remote MDT to
1119 * ask the remote LFSCK instance to create the specified orphan object
1120 * under .lustre/lost+found/MDTxxxx/ directory with the name:
1121 * ${FID}-P-${conflict_version}.
1123 * \param[in] env pointer to the thread context
1124 * \param[in] com pointer to the lfsck component
1125 * \param[in] orphan pointer to the orphan MDT-object
1126 * \param[in] type the orphan's type to be created
1128 * type "P": The orphan object to be created was a parent directory
1129 * of some MDT-object which linkEA shows that the @orphan
1130 * object is missing.
1132 * \see lfsck_layout_recreate_parent() for more types.
1134 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1136 * \retval positive number for repaired cases
1137 * \retval 0 if needs to repair nothing
1138 * \retval negative error number on failure
1140 static int lfsck_namespace_create_orphan_remote(const struct lu_env *env,
1141 struct lfsck_component *com,
1142 struct dt_object *orphan,
1144 struct lmv_mds_md_v1 *lmv)
1146 struct lfsck_thread_info *info = lfsck_env_info(env);
1147 struct lfsck_request *lr = &info->lti_lr;
1148 struct lu_seq_range *range = &info->lti_range;
1149 const struct lu_fid *fid = lfsck_dto2fid(orphan);
1150 struct lfsck_namespace *ns = com->lc_file_ram;
1151 struct lfsck_instance *lfsck = com->lc_lfsck;
1152 struct seq_server_site *ss =
1153 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1154 struct lfsck_tgt_desc *ltd = NULL;
1155 struct ptlrpc_request *req = NULL;
1159 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1162 fld_range_set_mdt(range);
1163 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1167 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, range->lsr_index);
1169 ns->ln_flags |= LF_INCOMPLETE;
1171 GOTO(out, rc = -ENODEV);
1174 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1177 GOTO(out, rc = -ENOMEM);
1179 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1181 ptlrpc_request_free(req);
1186 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1187 memset(lr, 0, sizeof(*lr));
1188 lr->lr_event = LE_CREATE_ORPHAN;
1189 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1190 lr->lr_active = LFSCK_TYPE_NAMESPACE;
1194 lr->lr_hash_type = lmv->lmv_hash_type;
1195 lr->lr_stripe_count = lmv->lmv_stripe_count;
1196 lr->lr_layout_version = lmv->lmv_layout_version;
1197 memcpy(lr->lr_pool_name, lmv->lmv_pool_name,
1198 sizeof(lr->lr_pool_name));
1201 ptlrpc_request_set_replen(req);
1202 rc = ptlrpc_queue_wait(req);
1203 ptlrpc_req_finished(req);
1207 else if (rc == -EEXIST)
1213 CDEBUG(D_LFSCK, "%s: namespace LFSCK create object "
1214 DFID" on the MDT %x remotely: rc = %d\n",
1215 lfsck_lfsck2name(lfsck), PFID(fid),
1216 ltd != NULL ? ltd->ltd_index : -1, rc);
1225 * Create the specified orphan MDT-object locally.
1227 * For the case that the parent MDT-object stored in some MDT-object's
1228 * linkEA entry is lost, the LFSCK will re-create the parent object as
1229 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1230 * with the name ${FID}-P-${conflict_version}.
1232 * \param[in] env pointer to the thread context
1233 * \param[in] com pointer to the lfsck component
1234 * \param[in] orphan pointer to the orphan MDT-object to be created
1235 * \param[in] type the orphan's type to be created
1237 * type "P": The orphan object to be created was a parent directory
1238 * of some MDT-object which linkEA shows that the @orphan
1239 * object is missing.
1241 * \see lfsck_layout_recreate_parent() for more types.
1243 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1245 * \retval positive number for repaired cases
1246 * \retval negative error number on failure
1248 static int lfsck_namespace_create_orphan_local(const struct lu_env *env,
1249 struct lfsck_component *com,
1250 struct dt_object *orphan,
1252 struct lmv_mds_md_v1 *lmv)
1254 struct lfsck_thread_info *info = lfsck_env_info(env);
1255 struct lu_attr *la = &info->lti_la;
1256 struct dt_allocation_hint *hint = &info->lti_hint;
1257 struct dt_object_format *dof = &info->lti_dof;
1258 struct lu_name *cname = &info->lti_name2;
1259 struct dt_insert_rec *rec = &info->lti_dt_rec;
1260 struct lu_fid *tfid = &info->lti_fid;
1261 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1262 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1263 const struct lu_fid *pfid;
1264 struct lfsck_instance *lfsck = com->lc_lfsck;
1265 struct dt_device *dev = lfsck->li_bottom;
1266 struct dt_object *parent = NULL;
1267 struct dt_object *child = NULL;
1268 struct thandle *th = NULL;
1269 struct lustre_handle lh = { 0 };
1270 struct linkea_data ldata = { 0 };
1271 struct lu_buf linkea_buf;
1272 struct lu_buf lmv_buf;
1279 LASSERT(!dt_object_exists(orphan));
1280 LASSERT(!dt_object_remote(orphan));
1282 /* @orphan maybe not attached to lfsck->li_bottom */
1283 child = lfsck_object_find_by_dev(env, dev, cfid);
1285 GOTO(log, rc = PTR_ERR(child));
1287 cname->ln_name = NULL;
1288 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1291 /* Create .lustre/lost+found/MDTxxxx when needed. */
1292 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1293 rc = lfsck_create_lpf(env, lfsck);
1298 parent = lfsck->li_lpf_obj;
1299 pfid = lfsck_dto2fid(parent);
1301 /* Hold update lock on the parent to prevent others to access. */
1302 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1303 MDS_INODELOCK_UPDATE, LCK_EX);
1308 namelen = snprintf(name, 31, DFID"-P-%d",
1310 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1311 (const struct dt_key *)name, BYPASS_CAPA);
1312 if (rc != 0 && rc != -ENOENT)
1316 cname->ln_name = name;
1317 cname->ln_namelen = namelen;
1319 memset(la, 0, sizeof(*la));
1320 la->la_mode = type | (S_ISDIR(type) ? 0700 : 0600);
1321 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1322 LA_ATIME | LA_MTIME | LA_CTIME;
1324 child->do_ops->do_ah_init(env, hint, parent, child,
1325 la->la_mode & S_IFMT);
1327 memset(dof, 0, sizeof(*dof));
1328 dof->dof_type = dt_mode_to_dft(type);
1330 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1334 rc = linkea_add_buf(&ldata, cname, pfid);
1338 th = dt_trans_create(env, dev);
1340 GOTO(unlock1, rc = PTR_ERR(th));
1342 rc = dt_declare_create(env, child, la, hint, dof, th);
1343 if (rc == 0 && S_ISDIR(type))
1344 rc = dt_declare_ref_add(env, child, th);
1350 lmv->lmv_magic = LMV_MAGIC;
1351 lmv->lmv_master_mdt_index = lfsck_dev_idx(dev);
1352 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1353 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1354 rc = dt_declare_xattr_set(env, child, &lmv_buf,
1355 XATTR_NAME_LMV, 0, th);
1360 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1361 ldata.ld_leh->leh_len);
1362 rc = dt_declare_xattr_set(env, child, &linkea_buf,
1363 XATTR_NAME_LINK, 0, th);
1367 rec->rec_type = type;
1368 rec->rec_fid = cfid;
1369 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1370 (const struct dt_key *)name, th);
1371 if (rc == 0 && S_ISDIR(type))
1372 rc = dt_declare_ref_add(env, parent, th);
1377 rc = dt_trans_start_local(env, dev, th);
1381 dt_write_lock(env, child, 0);
1382 rc = dt_create(env, child, la, hint, dof, th);
1386 if (S_ISDIR(type)) {
1387 if (unlikely(!dt_try_as_dir(env, child)))
1388 GOTO(unlock2, rc = -ENOTDIR);
1390 rec->rec_type = S_IFDIR;
1391 rec->rec_fid = cfid;
1392 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1393 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1397 rec->rec_fid = pfid;
1398 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1399 (const struct dt_key *)dotdot, th,
1404 rc = dt_ref_add(env, child, th);
1410 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV, 0,
1416 rc = dt_xattr_set(env, child, &linkea_buf,
1417 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1418 dt_write_unlock(env, child);
1422 rec->rec_type = type;
1423 rec->rec_fid = cfid;
1424 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1425 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1426 if (rc == 0 && S_ISDIR(type)) {
1427 dt_write_lock(env, parent, 0);
1428 rc = dt_ref_add(env, parent, th);
1429 dt_write_unlock(env, parent);
1432 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1435 dt_write_unlock(env, child);
1438 dt_trans_stop(env, dev, th);
1441 lfsck_ibits_unlock(&lh, LCK_EX);
1444 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan locally for "
1445 "the object "DFID", name = %s, type %o: rc = %d\n",
1446 lfsck_lfsck2name(lfsck), PFID(cfid),
1447 cname->ln_name != NULL ? cname->ln_name : "<NULL>", type, rc);
1449 if (child != NULL && !IS_ERR(child))
1450 lfsck_object_put(env, child);
1456 * Create the specified orphan MDT-object.
1458 * For the case that the parent MDT-object stored in some MDT-object's
1459 * linkEA entry is lost, the LFSCK will re-create the parent object as
1460 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1461 * with the name: ${FID}-P-${conflict_version}.
1463 * \param[in] env pointer to the thread context
1464 * \param[in] com pointer to the lfsck component
1465 * \param[in] orphan pointer to the orphan MDT-object
1467 * type "P": The orphan object to be created was a parent directory
1468 * of some MDT-object which linkEA shows that the @orphan
1469 * object is missing.
1471 * \see lfsck_layout_recreate_parent() for more types.
1473 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1475 * \retval positive number for repaired cases
1476 * \retval 0 if needs to repair nothing
1477 * \retval negative error number on failure
1479 static int lfsck_namespace_create_orphan(const struct lu_env *env,
1480 struct lfsck_component *com,
1481 struct dt_object *orphan,
1482 struct lmv_mds_md_v1 *lmv)
1484 struct lfsck_namespace *ns = com->lc_file_ram;
1487 if (dt_object_remote(orphan))
1488 rc = lfsck_namespace_create_orphan_remote(env, com, orphan,
1491 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
1495 ns->ln_flags |= LF_INCONSISTENT;
1501 * Remove the specified entry from the linkEA.
1503 * Locate the linkEA entry with the given @cname and @pfid, then
1504 * remove this entry or the other entries those are repeated with
1507 * \param[in] env pointer to the thread context
1508 * \param[in] com pointer to the lfsck component
1509 * \param[in] obj pointer to the dt_object to be handled
1510 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1511 * \param[in] cname the name for the child in the parent directory
1512 * \param[in] pfid the parent directory's FID for the linkEA
1513 * \param[in] next if true, then remove the first found linkEA
1514 * entry, and move the ldata->ld_lee to next entry
1516 * \retval positive number for repaired cases
1517 * \retval 0 if nothing to be repaired
1518 * \retval negative error number on failure
1520 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1521 struct lfsck_component *com,
1522 struct dt_object *obj,
1523 struct linkea_data *ldata,
1524 struct lu_name *cname,
1525 struct lu_fid *pfid,
1528 struct lfsck_instance *lfsck = com->lc_lfsck;
1529 struct dt_device *dev = lfsck->li_bottom;
1530 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1531 struct thandle *th = NULL;
1532 struct lustre_handle lh = { 0 };
1533 struct linkea_data ldata_new = { 0 };
1534 struct lu_buf linkea_buf;
1538 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1539 MDS_INODELOCK_UPDATE |
1540 MDS_INODELOCK_XATTR, LCK_EX);
1545 linkea_del_buf(ldata, cname);
1547 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1549 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1550 ldata->ld_leh->leh_len);
1553 th = dt_trans_create(env, dev);
1555 GOTO(unlock1, rc = PTR_ERR(th));
1557 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1558 XATTR_NAME_LINK, 0, th);
1562 rc = dt_trans_start_local(env, dev, th);
1566 dt_write_lock(env, obj, 0);
1567 if (unlikely(lfsck_is_dead_obj(obj)))
1568 GOTO(unlock2, rc = -ENOENT);
1570 rc = lfsck_links_read2(env, obj, &ldata_new);
1574 /* The specified linkEA entry has been removed by race. */
1575 rc = linkea_links_find(&ldata_new, cname, pfid);
1577 GOTO(unlock2, rc = 0);
1579 if (bk->lb_param & LPF_DRYRUN)
1580 GOTO(unlock2, rc = 1);
1583 linkea_del_buf(&ldata_new, cname);
1585 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1588 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1589 dt_write_unlock(env, obj);
1590 dt_trans_stop(env, dev, th);
1591 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1592 ldata_new.ld_leh->leh_len);
1596 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1597 ldata_new.ld_leh->leh_len);
1598 rc = dt_xattr_set(env, obj, &linkea_buf,
1599 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1601 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1604 dt_write_unlock(env, obj);
1607 dt_trans_stop(env, dev, th);
1610 lfsck_ibits_unlock(&lh, LCK_EX);
1613 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1614 "for the object: "DFID", parent "DFID", name %.*s\n",
1615 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1616 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1620 struct lfsck_namespace *ns = com->lc_file_ram;
1622 ns->ln_flags |= LF_INCONSISTENT;
1629 * Conditionally remove the specified entry from the linkEA.
1631 * Take the parent lock firstly, then check whether the specified
1632 * name entry exists or not: if yes, do nothing; otherwise, call
1633 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1635 * \param[in] env pointer to the thread context
1636 * \param[in] com pointer to the lfsck component
1637 * \param[in] parent pointer to the parent directory
1638 * \param[in] child pointer to the child object that holds the linkEA
1639 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1640 * \param[in] cname the name for the child in the parent directory
1641 * \param[in] pfid the parent directory's FID for the linkEA
1643 * \retval positive number for repaired cases
1644 * \retval 0 if nothing to be repaired
1645 * \retval negative error number on failure
1647 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1648 struct lfsck_component *com,
1649 struct dt_object *parent,
1650 struct dt_object *child,
1651 struct linkea_data *ldata,
1652 struct lu_name *cname,
1653 struct lu_fid *pfid)
1655 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1656 struct lustre_handle lh = { 0 };
1660 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1661 MDS_INODELOCK_UPDATE, LCK_EX);
1665 dt_read_lock(env, parent, 0);
1666 if (unlikely(lfsck_is_dead_obj(parent))) {
1667 dt_read_unlock(env, parent);
1668 lfsck_ibits_unlock(&lh, LCK_EX);
1669 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1675 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1676 (const struct dt_key *)cname->ln_name,
1678 dt_read_unlock(env, parent);
1680 /* It is safe to release the ldlm lock, because when the logic come
1681 * here, we have got all the needed information above whether the
1682 * linkEA entry is valid or not. It is not important that others
1683 * may add new linkEA entry after the ldlm lock released. If other
1684 * has removed the specified linkEA entry by race, then it is OK,
1685 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1687 lfsck_ibits_unlock(&lh, LCK_EX);
1688 if (rc == -ENOENT) {
1689 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1698 /* The LFSCK just found some internal status of cross-MDTs
1699 * create operation. That is normal. */
1700 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1701 linkea_next_entry(ldata);
1706 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1713 * Conditionally replace name entry in the parent.
1715 * As required, the LFSCK may re-create the lost MDT-object for dangling
1716 * name entry, but such repairing may be wrong because of bad FID in the
1717 * name entry. As the LFSCK processing, the real MDT-object may be found,
1718 * then the LFSCK should check whether the former re-created MDT-object
1719 * has been modified or not, if not, then destroy it and update the name
1720 * entry in the parent to reference the real MDT-object.
1722 * \param[in] env pointer to the thread context
1723 * \param[in] com pointer to the lfsck component
1724 * \param[in] parent pointer to the parent directory
1725 * \param[in] child pointer to the MDT-object that may be the real
1726 * MDT-object corresponding to the name entry in parent
1727 * \param[in] cfid the current FID in the name entry
1728 * \param[in] cname contains the name of the child in the parent directory
1730 * \retval positive number for repaired cases
1731 * \retval 0 if nothing to be repaired
1732 * \retval negative error number on failure
1734 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1735 struct lfsck_component *com,
1736 struct dt_object *parent,
1737 struct dt_object *child,
1738 const struct lu_fid *cfid,
1739 const struct lu_name *cname)
1741 struct lfsck_thread_info *info = lfsck_env_info(env);
1742 struct lu_fid *tfid = &info->lti_fid5;
1743 struct lu_attr *la = &info->lti_la;
1744 struct dt_insert_rec *rec = &info->lti_dt_rec;
1745 struct lfsck_instance *lfsck = com->lc_lfsck;
1746 struct dt_device *dev = lfsck->li_next;
1747 const char *name = cname->ln_name;
1748 struct dt_object *obj = NULL;
1749 struct lustre_handle plh = { 0 };
1750 struct lustre_handle clh = { 0 };
1751 struct linkea_data ldata = { 0 };
1752 struct thandle *th = NULL;
1757 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1758 MDS_INODELOCK_UPDATE, LCK_EX);
1762 if (!fid_is_sane(cfid)) {
1767 obj = lfsck_object_find(env, lfsck, cfid);
1770 if (rc == -ENOENT) {
1778 if (!dt_object_exists(obj)) {
1783 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1784 (const struct dt_key *)name, BYPASS_CAPA);
1785 if (rc == -ENOENT) {
1793 /* Someone changed the name entry, cannot replace it. */
1794 if (!lu_fid_eq(cfid, tfid))
1797 /* lock the object to be destroyed. */
1798 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1799 MDS_INODELOCK_UPDATE |
1800 MDS_INODELOCK_XATTR, LCK_EX);
1804 if (unlikely(lfsck_is_dead_obj(obj))) {
1809 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1813 /* The object has been modified by other(s), or it is not created by
1814 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1815 if (la->la_ctime != 0)
1818 if (S_ISREG(la->la_mode)) {
1819 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1821 /* If someone has created related OST-object(s),
1823 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1824 GOTO(log, rc = (rc > 0 ? 0 : rc));
1828 dt_read_lock(env, child, 0);
1829 rc = lfsck_links_read2(env, child, &ldata);
1830 dt_read_unlock(env, child);
1832 /* Someone changed the child, no need to replace. */
1839 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1840 /* Someone moved the child, no need to replace. */
1844 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1847 th = dt_trans_create(env, dev);
1849 GOTO(log, rc = PTR_ERR(th));
1852 rc = dt_declare_destroy(env, obj, th);
1857 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1861 rec->rec_type = S_IFDIR;
1862 rec->rec_fid = lfsck_dto2fid(child);
1863 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1864 (const struct dt_key *)name, th);
1868 rc = dt_trans_start(env, dev, th);
1873 rc = dt_destroy(env, obj, th);
1878 /* The old name entry maybe not exist. */
1879 dt_delete(env, parent, (const struct dt_key *)name, th,
1882 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1883 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1885 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1888 dt_trans_stop(env, dev, th);
1891 lfsck_ibits_unlock(&clh, LCK_EX);
1892 lfsck_ibits_unlock(&plh, LCK_EX);
1893 if (obj != NULL && !IS_ERR(obj))
1894 lfsck_object_put(env, obj);
1896 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1897 "object "DFID" because of conflict with the object "DFID
1898 " under the parent "DFID" with name %s: rc = %d\n",
1899 lfsck_lfsck2name(lfsck), PFID(cfid),
1900 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1907 * Overwrite the linkEA for the object with the given ldata.
1909 * The caller should take the ldlm lock before the calling.
1911 * \param[in] env pointer to the thread context
1912 * \param[in] com pointer to the lfsck component
1913 * \param[in] obj pointer to the dt_object to be handled
1914 * \param[in] ldata pointer to the new linkEA data
1916 * \retval positive number for repaired cases
1917 * \retval 0 if nothing to be repaired
1918 * \retval negative error number on failure
1920 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1921 struct lfsck_component *com,
1922 struct dt_object *obj,
1923 struct linkea_data *ldata)
1925 struct lfsck_instance *lfsck = com->lc_lfsck;
1926 struct dt_device *dev = lfsck->li_bottom;
1927 struct thandle *th = NULL;
1928 struct lu_buf linkea_buf;
1932 LASSERT(!dt_object_remote(obj));
1934 th = dt_trans_create(env, dev);
1936 GOTO(log, rc = PTR_ERR(th));
1938 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1939 ldata->ld_leh->leh_len);
1940 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1941 XATTR_NAME_LINK, 0, th);
1945 rc = dt_trans_start_local(env, dev, th);
1949 dt_write_lock(env, obj, 0);
1950 if (unlikely(lfsck_is_dead_obj(obj)))
1951 GOTO(unlock, rc = 0);
1953 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1954 GOTO(unlock, rc = 1);
1956 rc = dt_xattr_set(env, obj, &linkea_buf,
1957 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1959 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1962 dt_write_unlock(env, obj);
1965 dt_trans_stop(env, dev, th);
1968 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1969 "object "DFID": rc = %d\n",
1970 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1973 struct lfsck_namespace *ns = com->lc_file_ram;
1975 ns->ln_flags |= LF_INCONSISTENT;
1982 * Repair invalid name entry.
1984 * If the name entry contains invalid information, such as bad file type
1985 * or (and) corrupted object FID, then either remove the name entry or
1986 * udpate the name entry with the given (right) information.
1988 * \param[in] env pointer to the thread context
1989 * \param[in] com pointer to the lfsck component
1990 * \param[in] parent pointer to the parent directory
1991 * \param[in] child pointer to the object referenced by the name entry
1992 * \param[in] name the old name of the child under the parent directory
1993 * \param[in] name2 the new name of the child under the parent directory
1994 * \param[in] type the type claimed by the name entry
1995 * \param[in] update update the name entry if true; otherwise, remove it
1996 * \param[in] dec decrease the parent nlink count if true
1998 * \retval positive number for repaired successfully
1999 * \retval 0 if nothing to be repaired
2000 * \retval negative error number on failure
2002 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2003 struct lfsck_component *com,
2004 struct dt_object *parent,
2005 struct dt_object *child,
2006 const char *name, const char *name2,
2007 __u16 type, bool update, bool dec)
2009 struct lfsck_thread_info *info = lfsck_env_info(env);
2010 struct dt_insert_rec *rec = &info->lti_dt_rec;
2011 const struct lu_fid *cfid = lfsck_dto2fid(child);
2012 struct lu_fid *tfid = &info->lti_fid5;
2013 struct lfsck_instance *lfsck = com->lc_lfsck;
2014 struct dt_device *dev = lfsck->li_next;
2015 struct thandle *th = NULL;
2016 struct lustre_handle lh = { 0 };
2020 if (unlikely(!dt_try_as_dir(env, parent)))
2021 GOTO(log, rc = -ENOTDIR);
2023 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2024 MDS_INODELOCK_UPDATE, LCK_EX);
2028 th = dt_trans_create(env, dev);
2030 GOTO(unlock1, rc = PTR_ERR(th));
2032 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2037 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2038 rec->rec_fid = cfid;
2039 rc = dt_declare_insert(env, parent,
2040 (const struct dt_rec *)rec,
2041 (const struct dt_key *)name2, th);
2047 rc = dt_declare_ref_del(env, parent, th);
2052 rc = dt_trans_start(env, dev, th);
2056 dt_write_lock(env, parent, 0);
2057 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2058 (const struct dt_key *)name, BYPASS_CAPA);
2059 /* Someone has removed the bad name entry by race. */
2061 GOTO(unlock2, rc = 0);
2066 /* Someone has removed the bad name entry and reused it for other
2067 * object by race. */
2068 if (!lu_fid_eq(tfid, cfid))
2069 GOTO(unlock2, rc = 0);
2071 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2072 GOTO(unlock2, rc = 1);
2074 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2080 rc = dt_insert(env, parent,
2081 (const struct dt_rec *)rec,
2082 (const struct dt_key *)name2, th,
2089 rc = dt_ref_del(env, parent, th);
2094 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2097 dt_write_unlock(env, parent);
2100 dt_trans_stop(env, dev, th);
2102 /* We are not sure whether the child will become orphan or not.
2103 * Record it in the LFSCK trace file for further checking in
2104 * the second-stage scanning. */
2105 if (!update && !dec && rc == 0)
2106 lfsck_namespace_trace_update(env, com, cfid,
2107 LNTF_CHECK_LINKEA, true);
2110 lfsck_ibits_unlock(&lh, LCK_EX);
2113 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2114 "entry for: parent "DFID", child "DFID", name %s, type "
2115 "in name entry %o, type claimed by child %o. repair it "
2116 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2117 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2118 name, type, update ? lfsck_object_type(child) : 0,
2119 update ? "updating" : "removing", name2, rc);
2122 struct lfsck_namespace *ns = com->lc_file_ram;
2124 ns->ln_flags |= LF_INCONSISTENT;
2131 * Update the ".." name entry for the given object.
2133 * The object's ".." is corrupted, this function will update the ".." name
2134 * entry with the given pfid, and the linkEA with the given ldata.
2136 * The caller should take the ldlm lock before the calling.
2138 * \param[in] env pointer to the thread context
2139 * \param[in] com pointer to the lfsck component
2140 * \param[in] obj pointer to the dt_object to be handled
2141 * \param[in] pfid the new fid for the object's ".." name entry
2142 * \param[in] cname the name for the @obj in the parent directory
2144 * \retval positive number for repaired cases
2145 * \retval 0 if nothing to be repaired
2146 * \retval negative error number on failure
2148 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2149 struct lfsck_component *com,
2150 struct dt_object *obj,
2151 const struct lu_fid *pfid,
2152 struct lu_name *cname)
2154 struct lfsck_thread_info *info = lfsck_env_info(env);
2155 struct dt_insert_rec *rec = &info->lti_dt_rec;
2156 struct lfsck_instance *lfsck = com->lc_lfsck;
2157 struct dt_device *dev = lfsck->li_bottom;
2158 struct thandle *th = NULL;
2159 struct linkea_data ldata = { 0 };
2160 struct lu_buf linkea_buf;
2164 LASSERT(!dt_object_remote(obj));
2165 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2167 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2171 rc = linkea_add_buf(&ldata, cname, pfid);
2175 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2176 ldata.ld_leh->leh_len);
2178 th = dt_trans_create(env, dev);
2180 GOTO(log, rc = PTR_ERR(th));
2182 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2186 rec->rec_type = S_IFDIR;
2187 rec->rec_fid = pfid;
2188 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2189 (const struct dt_key *)dotdot, th);
2193 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2194 XATTR_NAME_LINK, 0, th);
2198 rc = dt_trans_start_local(env, dev, th);
2202 dt_write_lock(env, obj, 0);
2203 if (unlikely(lfsck_is_dead_obj(obj)))
2204 GOTO(unlock, rc = 0);
2206 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2207 GOTO(unlock, rc = 1);
2209 /* The old ".." name entry maybe not exist. */
2210 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2213 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2214 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2218 rc = dt_xattr_set(env, obj, &linkea_buf,
2219 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2221 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2224 dt_write_unlock(env, obj);
2227 dt_trans_stop(env, dev, th);
2230 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2231 "the object "DFID", new parent "DFID": rc = %d\n",
2232 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2236 struct lfsck_namespace *ns = com->lc_file_ram;
2238 ns->ln_flags |= LF_INCONSISTENT;
2245 * Handle orphan @obj during Double Scan Directory.
2247 * Remove the @obj's current (invalid) linkEA entries, and insert
2248 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2249 * ${FID}-${PFID}-D-${conflict_version}
2251 * The caller should take the ldlm lock before the calling.
2253 * \param[in] env pointer to the thread context
2254 * \param[in] com pointer to the lfsck component
2255 * \param[in] obj pointer to the orphan object to be handled
2256 * \param[in] pfid the new fid for the object's ".." name entry
2257 * \param[in,out] lh ldlm lock handler for the given @obj
2258 * \param[out] type to tell the caller what the inconsistency is
2260 * \retval positive number for repaired cases
2261 * \retval 0 if nothing to be repaired
2262 * \retval negative error number on failure
2265 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2266 struct lfsck_component *com,
2267 struct dt_object *obj,
2268 const struct lu_fid *pfid,
2269 struct lustre_handle *lh,
2270 enum lfsck_namespace_inconsistency_type *type)
2272 struct lfsck_thread_info *info = lfsck_env_info(env);
2273 struct lfsck_namespace *ns = com->lc_file_ram;
2277 /* Remove the unrecognized linkEA. */
2278 rc = lfsck_namespace_links_remove(env, com, obj);
2279 lfsck_ibits_unlock(lh, LCK_EX);
2280 if (rc < 0 && rc != -ENODATA)
2283 *type = LNIT_MUL_REF;
2285 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2286 * ever tried to verify some remote MDT-object that resides on this
2287 * MDT, but this MDT failed to respond such request. So means there
2288 * may be some remote name entry on other MDT that references this
2289 * object with another name, so we cannot know whether this linkEA
2290 * is valid or not. So keep it there and maybe resolved when next
2292 if (ns->ln_flags & LF_INCOMPLETE)
2295 /* The unique linkEA is invalid, even if the ".." name entry may be
2296 * valid, we still cannot know via which name entry this directory
2297 * will be referenced. Then handle it as pure orphan. */
2298 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2299 "-"DFID, PFID(pfid));
2300 rc = lfsck_namespace_insert_orphan(env, com, obj,
2301 info->lti_tmpbuf, "D", NULL);
2307 * Double Scan Directory object for single linkEA entry case.
2309 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2310 * then check whether the name is in the namespace or not, if not, add the
2311 * missing name entry back to namespace. If the linkEA entry is invalid,
2312 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2315 * \param[in] env pointer to the thread context
2316 * \param[in] com pointer to the lfsck component
2317 * \param[in] child pointer to the directory to be double scanned
2318 * \param[in] pfid the FID corresponding to the ".." entry
2319 * \param[in] ldata pointer to the linkEA data for the given @child
2320 * \param[in,out] lh ldlm lock handler for the given @child
2321 * \param[out] type to tell the caller what the inconsistency is
2322 * \param[in] retry if found inconsistency, but the caller does not hold
2323 * ldlm lock on the @child, then set @retry as true
2325 * \retval positive number for repaired cases
2326 * \retval 0 if nothing to be repaired
2327 * \retval negative error number on failure
2330 lfsck_namespace_dsd_single(const struct lu_env *env,
2331 struct lfsck_component *com,
2332 struct dt_object *child,
2333 const struct lu_fid *pfid,
2334 struct linkea_data *ldata,
2335 struct lustre_handle *lh,
2336 enum lfsck_namespace_inconsistency_type *type,
2339 struct lfsck_thread_info *info = lfsck_env_info(env);
2340 struct lu_name *cname = &info->lti_name;
2341 const struct lu_fid *cfid = lfsck_dto2fid(child);
2342 struct lu_fid *tfid = &info->lti_fid3;
2343 struct lfsck_namespace *ns = com->lc_file_ram;
2344 struct lfsck_instance *lfsck = com->lc_lfsck;
2345 struct dt_object *parent = NULL;
2346 struct lmv_mds_md_v1 *lmv;
2350 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
2351 /* The unique linkEA entry with bad parent will be handled as orphan. */
2352 if (!fid_is_sane(tfid)) {
2353 if (!lustre_handle_is_used(lh) && retry != NULL)
2356 rc = lfsck_namespace_dsd_orphan(env, com, child,
2362 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2364 GOTO(out, rc = PTR_ERR(parent));
2366 /* We trust the unique linkEA entry in spite of whether it matches the
2367 * ".." name entry or not. Because even if the linkEA entry is wrong
2368 * and the ".." name entry is right, we still cannot know via which
2369 * name entry the child will be referenced, since all known entries
2370 * have been verified during the first-stage scanning. */
2371 if (!dt_object_exists(parent)) {
2372 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2373 * has ever tried to verify some remote MDT-object that resides
2374 * on this MDT, but this MDT failed to respond such request. So
2375 * means there may be some remote name entry on other MDT that
2376 * references this object with another name, so we cannot know
2377 * whether this linkEA is valid or not. So keep it there and
2378 * maybe resolved when next LFSCK run. */
2379 if (ns->ln_flags & LF_INCOMPLETE)
2382 if (!lustre_handle_is_used(lh) && retry != NULL) {
2388 lfsck_ibits_unlock(lh, LCK_EX);
2391 lmv = &info->lti_lmv;
2392 rc = lfsck_read_stripe_lmv(env, child, lmv);
2393 if (rc != 0 && rc != -ENODATA)
2396 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2398 } else if (lfsck_shard_name_to_index(env,
2399 cname->ln_name, cname->ln_namelen,
2400 S_IFDIR, cfid) < 0) {
2401 /* It is an invalid name entry, we
2402 * cannot trust the parent also. */
2403 rc = lfsck_namespace_shrink_linkea(env, com, child,
2404 ldata, cname, tfid, true);
2408 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2409 "-"DFID, PFID(pfid));
2410 rc = lfsck_namespace_insert_orphan(env, com, child,
2411 info->lti_tmpbuf, "S", NULL);
2416 /* Create the lost parent as an orphan. */
2417 rc = lfsck_namespace_create_orphan(env, com, parent, lmv);
2419 /* Add the missing name entry to the parent. */
2420 rc = lfsck_namespace_insert_normal(env, com, parent,
2421 child, cname->ln_name);
2422 if (unlikely(rc == -EEXIST)) {
2423 /* Unfortunately, someone reused the name
2424 * under the parent by race. So we have
2425 * to remove the linkEA entry from
2426 * current child object. It means that the
2427 * LFSCK cannot recover the system
2428 * totally back to its original status,
2429 * but it is necessary to make the
2430 * current system to be consistent. */
2431 rc = lfsck_namespace_shrink_linkea(env,
2435 snprintf(info->lti_tmpbuf,
2436 sizeof(info->lti_tmpbuf),
2437 "-"DFID, PFID(pfid));
2438 rc = lfsck_namespace_insert_orphan(env,
2439 com, child, info->lti_tmpbuf,
2448 /* The unique linkEA entry with bad parent will be handled as orphan. */
2449 if (unlikely(!dt_try_as_dir(env, parent))) {
2450 if (!lustre_handle_is_used(lh) && retry != NULL)
2453 rc = lfsck_namespace_dsd_orphan(env, com, child,
2459 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2460 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2461 if (rc == -ENOENT) {
2462 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2463 * has ever tried to verify some remote MDT-object that resides
2464 * on this MDT, but this MDT failed to respond such request. So
2465 * means there may be some remote name entry on other MDT that
2466 * references this object with another name, so we cannot know
2467 * whether this linkEA is valid or not. So keep it there and
2468 * maybe resolved when next LFSCK run. */
2469 if (ns->ln_flags & LF_INCOMPLETE)
2472 if (!lustre_handle_is_used(lh) && retry != NULL) {
2478 lfsck_ibits_unlock(lh, LCK_EX);
2479 rc = lfsck_namespace_check_name(env, parent, child, cname);
2486 /* It is an invalid name entry, drop it. */
2487 if (unlikely(rc > 0)) {
2488 rc = lfsck_namespace_shrink_linkea(env, com, child,
2489 ldata, cname, tfid, true);
2491 snprintf(info->lti_tmpbuf,
2492 sizeof(info->lti_tmpbuf),
2493 "-"DFID, PFID(pfid));
2494 rc = lfsck_namespace_insert_orphan(env, com,
2495 child, info->lti_tmpbuf, "D", NULL);
2501 /* Add the missing name entry back to the namespace. */
2502 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2504 if (unlikely(rc == -ESTALE))
2505 /* It may happen when the remote object has been
2506 * removed, but the local MDT is not aware of that. */
2509 if (unlikely(rc == -EEXIST)) {
2510 /* Unfortunately, someone reused the name under the
2511 * parent by race. So we have to remove the linkEA
2512 * entry from current child object. It means that the
2513 * LFSCK cannot recover the system totally back to
2514 * its original status, but it is necessary to make
2515 * the current system to be consistent.
2517 * It also may be because of the LFSCK found some
2518 * internal status of create operation. Under such
2519 * case, nothing to be done. */
2520 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2521 parent, child, ldata, cname, tfid);
2523 snprintf(info->lti_tmpbuf,
2524 sizeof(info->lti_tmpbuf),
2525 "-"DFID, PFID(pfid));
2526 rc = lfsck_namespace_insert_orphan(env, com,
2527 child, info->lti_tmpbuf, "D", NULL);
2537 if (!lu_fid_eq(tfid, cfid)) {
2538 if (!lustre_handle_is_used(lh) && retry != NULL) {
2544 lfsck_ibits_unlock(lh, LCK_EX);
2545 /* The name entry references another MDT-object that
2546 * may be created by the LFSCK for repairing dangling
2547 * name entry. Try to replace it. */
2548 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2551 rc = lfsck_namespace_dsd_orphan(env, com, child,
2557 /* The ".." name entry is wrong, update it. */
2558 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2559 if (!lustre_handle_is_used(lh) && retry != NULL) {
2565 *type = LNIT_UNMATCHED_PAIRS;
2566 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2567 lfsck_dto2fid(parent), cname);
2573 if (parent != NULL && !IS_ERR(parent))
2574 lfsck_object_put(env, parent);
2580 * Double Scan Directory object for multiple linkEA entries case.
2582 * The given @child has multiple linkEA entries. There is at most one linkEA
2583 * entry will be valid, all the others will be removed. Firstly, the function
2584 * will try to find out the linkEA entry for which the name entry exists under
2585 * the given parent (@pfid). If there is no linkEA entry that matches the given
2586 * ".." name entry, then tries to find out the first linkEA entry that both the
2587 * parent and the name entry exist to rebuild a new ".." name entry.
2589 * \param[in] env pointer to the thread context
2590 * \param[in] com pointer to the lfsck component
2591 * \param[in] child pointer to the directory to be double scanned
2592 * \param[in] pfid the FID corresponding to the ".." entry
2593 * \param[in] ldata pointer to the linkEA data for the given @child
2594 * \param[in,out] lh ldlm lock handler for the given @child
2595 * \param[out] type to tell the caller what the inconsistency is
2596 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2598 * \retval positive number for repaired cases
2599 * \retval 0 if nothing to be repaired
2600 * \retval negative error number on failure
2603 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2604 struct lfsck_component *com,
2605 struct dt_object *child,
2606 const struct lu_fid *pfid,
2607 struct linkea_data *ldata,
2608 struct lustre_handle *lh,
2609 enum lfsck_namespace_inconsistency_type *type,
2612 struct lfsck_thread_info *info = lfsck_env_info(env);
2613 struct lu_name *cname = &info->lti_name;
2614 const struct lu_fid *cfid = lfsck_dto2fid(child);
2615 struct lu_fid *tfid = &info->lti_fid3;
2616 struct lu_fid *pfid2 = &info->lti_fid4;
2617 struct lfsck_namespace *ns = com->lc_file_ram;
2618 struct lfsck_instance *lfsck = com->lc_lfsck;
2619 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2620 struct dt_object *parent = NULL;
2621 struct linkea_data ldata_new = { 0 };
2628 while (ldata->ld_lee != NULL) {
2629 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
2631 /* Drop repeated linkEA entries. */
2632 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
2633 /* Drop invalid linkEA entry. */
2634 if (!fid_is_sane(tfid)) {
2635 linkea_del_buf(ldata, cname);
2639 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2640 * then it is possible that: the directry object has ever
2641 * been lost, but its name entry was there. In the former
2642 * LFSCK run, during the first-stage scanning, the LFSCK
2643 * found the dangling name entry, but it did not recreate
2644 * the lost object, and when moved to the second-stage
2645 * scanning, some children objects of the lost directory
2646 * object were found, then the LFSCK recreated such lost
2647 * directory object as an orphan.
2649 * When the LFSCK runs again, if the dangling name is still
2650 * there, the LFSCK should move the orphan directory object
2651 * back to the normal namespace. */
2652 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
2653 linkea_next_entry(ldata);
2657 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2659 RETURN(PTR_ERR(parent));
2661 if (!dt_object_exists(parent)) {
2662 lfsck_object_put(env, parent);
2663 if (ldata->ld_leh->leh_reccount > 1) {
2664 /* If it is NOT the last linkEA entry, then
2665 * there is still other chance to make the
2666 * child to be visible via other parent, then
2667 * remove this linkEA entry. */
2668 linkea_del_buf(ldata, cname);
2675 /* The linkEA entry with bad parent will be removed. */
2676 if (unlikely(!dt_try_as_dir(env, parent))) {
2677 lfsck_object_put(env, parent);
2678 linkea_del_buf(ldata, cname);
2682 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2683 (const struct dt_key *)cname->ln_name,
2685 *pfid2 = *lfsck_dto2fid(parent);
2686 if (rc == -ENOENT) {
2687 lfsck_object_put(env, parent);
2688 linkea_next_entry(ldata);
2693 lfsck_object_put(env, parent);
2698 if (lu_fid_eq(tfid, cfid)) {
2699 lfsck_object_put(env, parent);
2700 if (!lu_fid_eq(pfid, pfid2)) {
2701 *type = LNIT_UNMATCHED_PAIRS;
2702 rc = lfsck_namespace_repair_unmatched_pairs(env,
2703 com, child, pfid2, cname);
2709 /* It is the most common case that we find the
2710 * name entry corresponding to the linkEA entry
2711 * that matches the ".." name entry. */
2712 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2716 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2720 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2725 linkea_del_buf(ldata, cname);
2726 linkea_first_entry(ldata);
2727 /* There may be some invalid dangling name entries under
2728 * other parent directories, remove all of them. */
2729 while (ldata->ld_lee != NULL) {
2730 lfsck_namespace_unpack_linkea_entry(ldata,
2731 cname, tfid, info->lti_key);
2732 if (!fid_is_sane(tfid))
2735 parent = lfsck_object_find_bottom(env, lfsck,
2737 if (IS_ERR(parent)) {
2738 rc = PTR_ERR(parent);
2739 if (rc != -ENOENT &&
2740 bk->lb_param & LPF_FAILOUT)
2746 if (!dt_object_exists(parent)) {
2747 lfsck_object_put(env, parent);
2751 rc = lfsck_namespace_repair_dirent(env, com,
2752 parent, child, cname->ln_name,
2753 cname->ln_name, S_IFDIR, false, true);
2754 lfsck_object_put(env, parent);
2756 if (bk->lb_param & LPF_FAILOUT)
2765 linkea_del_buf(ldata, cname);
2768 ns->ln_dirent_repaired += count;
2773 lfsck_ibits_unlock(lh, LCK_EX);
2774 /* The name entry references another MDT-object that may be
2775 * created by the LFSCK for repairing dangling name entry.
2776 * Try to replace it. */
2777 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2779 lfsck_object_put(env, parent);
2786 linkea_del_buf(ldata, cname);
2789 if (ldata->ld_leh->leh_reccount == 1) {
2790 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2796 /* All linkEA entries are invalid and removed, then handle the @child
2798 if (ldata->ld_leh->leh_reccount == 0) {
2799 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2805 linkea_first_entry(ldata);
2806 /* If the dangling name entry for the orphan directory object has
2807 * been remvoed, then just check whether the directory object is
2808 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2814 /* There is no linkEA entry that matches the ".." name entry. Find
2815 * the first linkEA entry that both parent and name entry exist to
2816 * rebuild a new ".." name entry. */
2826 * Repair the object's nlink attribute.
2828 * If all the known name entries have been verified, then the object's hard
2829 * link attribute should match the object's linkEA entries count unless the
2830 * object's has too much hard link to be recorded in the linkEA. Such cases
2831 * should have been marked in the LFSCK trace file. Otherwise, trust the
2832 * linkEA to update the object's nlink attribute.
2834 * \param[in] env pointer to the thread context
2835 * \param[in] com pointer to the lfsck component
2836 * \param[in] obj pointer to the dt_object to be handled
2837 * \param[in,out] nlink pointer to buffer to object's hard lock count before
2838 * and after the repairing
2840 * \retval positive number for repaired cases
2841 * \retval 0 if nothing to be repaired
2842 * \retval negative error number on failure
2844 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2845 struct lfsck_component *com,
2846 struct dt_object *obj, __u32 *nlink)
2848 struct lfsck_thread_info *info = lfsck_env_info(env);
2849 struct lu_attr *la = &info->lti_la3;
2850 struct lu_fid *tfid = &info->lti_fid3;
2851 struct lfsck_namespace *ns = com->lc_file_ram;
2852 struct lfsck_instance *lfsck = com->lc_lfsck;
2853 struct dt_device *dev = lfsck->li_bottom;
2854 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2855 struct dt_object *child = NULL;
2856 struct thandle *th = NULL;
2857 struct linkea_data ldata = { 0 };
2858 struct lustre_handle lh = { 0 };
2864 LASSERT(!dt_object_remote(obj));
2865 LASSERT(S_ISREG(lfsck_object_type(obj)));
2867 child = lfsck_object_find_by_dev(env, dev, cfid);
2869 GOTO(log, rc = PTR_ERR(child));
2871 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2872 MDS_INODELOCK_UPDATE |
2873 MDS_INODELOCK_XATTR, LCK_EX);
2877 th = dt_trans_create(env, dev);
2879 GOTO(log, rc = PTR_ERR(th));
2881 la->la_valid = LA_NLINK;
2882 rc = dt_declare_attr_set(env, child, la, th);
2886 rc = dt_trans_start_local(env, dev, th);
2890 dt_write_lock(env, child, 0);
2891 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2892 * ever tried to verify some remote MDT-object that resides on this
2893 * MDT, but this MDT failed to respond such request. So means there
2894 * may be some remote name entry on other MDT that references this
2895 * object with another name, so we cannot know whether this linkEA
2896 * is valid or not. So keep it there and maybe resolved when next
2898 if (ns->ln_flags & LF_INCOMPLETE)
2899 GOTO(unlock, rc = 0);
2901 fid_cpu_to_be(tfid, cfid);
2902 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)&flags,
2903 (const struct dt_key *)tfid, BYPASS_CAPA);
2907 if (flags & LNTF_SKIP_NLINK)
2908 GOTO(unlock, rc = 0);
2910 rc = lfsck_links_read2(env, child, &ldata);
2912 GOTO(unlock, rc = 0);
2917 if (*nlink == ldata.ld_leh->leh_reccount)
2918 GOTO(unlock, rc = 0);
2920 la->la_nlink = *nlink = ldata.ld_leh->leh_reccount;
2921 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2922 GOTO(unlock, rc = 1);
2924 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
2926 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2929 dt_write_unlock(env, child);
2932 dt_trans_stop(env, dev, th);
2935 lfsck_ibits_unlock(&lh, LCK_EX);
2936 if (child != NULL && !IS_ERR(child))
2937 lfsck_object_put(env, child);
2939 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2940 "nlink count from %u to %u: rc = %d\n",
2941 lfsck_lfsck2name(lfsck), PFID(cfid), old, *nlink, rc);
2944 ns->ln_flags |= LF_INCONSISTENT;
2950 * Double scan the directory object for namespace LFSCK.
2952 * This function will verify the <parent, child> pairs in the namespace tree:
2953 * the parent references the child via some name entry that should be in the
2954 * child's linkEA entry, the child should back references the parent via its
2957 * The LFSCK will scan every linkEA entry in turn until find out the first
2958 * matched pairs. If found, then all other linkEA entries will be dropped.
2959 * If all the linkEA entries cannot match the ".." name entry, then there
2960 * are serveral possible cases:
2962 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2963 * in the linkEA entry is valid.
2965 * 2) If there are multiple linkEA entries, then try to find the linkEA
2966 * that matches the ".." name entry. If found, then all other entries
2967 * are invalid; otherwise, it is quite possible that the ".." name entry
2968 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2969 * entry according to the first valid linkEA entry (both the parent and
2970 * the name entry should exist).
2972 * 3) If the directory object has no (valid) linkEA entry, then the
2973 * directory object will be handled as pure orphan and inserted
2974 * in the .lustre/lost+found/MDTxxxx/ with the name:
2975 * ${self_FID}-${PFID}-D-${conflict_version}
2977 * \param[in] env pointer to the thread context
2978 * \param[in] com pointer to the lfsck component
2979 * \param[in] child pointer to the directory object to be handled
2980 * \param[in] flags to indicate the specical checking on the @child
2982 * \retval positive number for repaired cases
2983 * \retval 0 if nothing to be repaired
2984 * \retval negative error number on failure
2986 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
2987 struct lfsck_component *com,
2988 struct dt_object *child, __u8 flags)
2990 struct lfsck_thread_info *info = lfsck_env_info(env);
2991 const struct lu_fid *cfid = lfsck_dto2fid(child);
2992 struct lu_fid *pfid = &info->lti_fid2;
2993 struct lfsck_namespace *ns = com->lc_file_ram;
2994 struct lfsck_instance *lfsck = com->lc_lfsck;
2995 struct lustre_handle lh = { 0 };
2996 struct linkea_data ldata = { 0 };
2997 bool unknown = false;
3000 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3004 LASSERT(!dt_object_remote(child));
3006 if (flags & LNTF_UNCERTAIN_LMV) {
3007 if (flags & LNTF_RECHECK_NAME_HASH) {
3008 rc = lfsck_namespace_scan_shard(env, com, child);
3012 ns->ln_striped_shards_scanned++;
3014 ns->ln_striped_shards_skipped++;
3018 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3022 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3023 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3024 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3025 "the namespace LFSCK, then the LFSCK cannot guarantee"
3026 "all the name entries have been verified in first-stage"
3027 "scanning. So have to skip orphan related handling for"
3028 "the directory object "DFID" with remote name entry\n",
3029 lfsck_lfsck2name(lfsck), PFID(cfid));
3034 if (unlikely(!dt_try_as_dir(env, child)))
3035 GOTO(out, rc = -ENOTDIR);
3037 /* We only take ldlm lock on the @child when required. When the
3038 * logic comes here for the first time, it is always false. */
3042 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3043 MDS_INODELOCK_UPDATE |
3044 MDS_INODELOCK_XATTR, LCK_EX);
3049 dt_read_lock(env, child, 0);
3050 if (unlikely(lfsck_is_dead_obj(child))) {
3051 dt_read_unlock(env, child);
3056 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3057 (const struct dt_key *)dotdot, BYPASS_CAPA);
3059 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3060 dt_read_unlock(env, child);
3065 if (!lustre_handle_is_used(&lh)) {
3066 dt_read_unlock(env, child);
3071 } else if (lfsck->li_lpf_obj != NULL &&
3072 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3076 rc = lfsck_links_read(env, child, &ldata);
3077 dt_read_unlock(env, child);
3079 if (rc != -ENODATA && rc != -EINVAL)
3082 if (!lustre_handle_is_used(&lh))
3085 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3086 /* Remove the corrupted linkEA. */
3087 rc = lfsck_namespace_links_remove(env, com, child);
3089 /* Here, because of the crashed linkEA, we
3090 * cannot know whether there is some parent
3091 * that references the child directory via
3092 * some name entry or not. So keep it there,
3093 * when the LFSCK run next time, if there is
3094 * some parent that references this object,
3095 * then the LFSCK can rebuild the linkEA;
3096 * otherwise, this object will be handled
3097 * as orphan as above. */
3100 /* 1. If we have neither ".." nor linkEA,
3101 * then it is an orphan.
3103 * 2. If we only have the ".." name entry,
3104 * but no parent references this child
3105 * directory, then handle it as orphan. */
3106 lfsck_ibits_unlock(&lh, LCK_EX);
3107 type = LNIT_MUL_REF;
3109 /* If the LFSCK is marked as LF_INCOMPLETE,
3110 * then means some MDT has ever tried to
3111 * verify some remote MDT-object that resides
3112 * on this MDT, but this MDT failed to respond
3113 * such request. So means there may be some
3114 * remote name entry on other MDT that
3115 * references this object with another name,
3116 * so we cannot know whether this linkEA is
3117 * valid or not. So keep it there and maybe
3118 * resolved when next LFSCK run. */
3119 if (ns->ln_flags & LF_INCOMPLETE)
3122 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3123 "-"DFID, PFID(pfid));
3124 rc = lfsck_namespace_insert_orphan(env, com, child,
3125 info->lti_tmpbuf, "D", NULL);
3131 linkea_first_entry(&ldata);
3132 /* This is the most common case: the object has unique linkEA entry. */
3133 if (ldata.ld_leh->leh_reccount == 1) {
3134 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3135 &lh, &type, &retry);
3137 LASSERT(!lustre_handle_is_used(&lh));
3146 if (!lustre_handle_is_used(&lh))
3149 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3150 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3156 /* When we come here, the cases usually like that:
3157 * 1) The directory object has a corrupted linkEA entry. During the
3158 * first-stage scanning, the LFSCK cannot know such corruption,
3159 * then it appends the right linkEA entry according to the found
3160 * name entry after the bad one.
3162 * 2) The directory object has a right linkEA entry. During the
3163 * first-stage scanning, the LFSCK finds some bad name entry,
3164 * but the LFSCK cannot aware that at that time, then it adds
3165 * the bad linkEA entry for further processing. */
3166 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3172 lfsck_ibits_unlock(&lh, LCK_EX);
3175 case LNIT_BAD_LINKEA:
3176 ns->ln_linkea_repaired++;
3178 case LNIT_UNMATCHED_PAIRS:
3179 ns->ln_unmatched_pairs_repaired++;
3182 ns->ln_mul_ref_repaired++;
3190 ns->ln_unknown_inconsistency++;
3196 * Double scan the MDT-object for namespace LFSCK.
3198 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3199 * those entries from the linkEA; if the linkEA becomes empty or the object
3200 * has no linkEA, then it is an orphan and will be added into the directory
3201 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3202 * the remote parent; if the name entry corresponding to some linkEA entry
3203 * is lost, then add the name entry back to the namespace.
3205 * \param[in] env pointer to the thread context
3206 * \param[in] com pointer to the lfsck component
3207 * \param[in] child pointer to the dt_object to be handled
3208 * \param[in] flags some hints to indicate how the @child should be handled
3210 * \retval positive number for repaired cases
3211 * \retval 0 if nothing to be repaired
3212 * \retval negative error number on failure
3214 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3215 struct lfsck_component *com,
3216 struct dt_object *child, __u8 flags)
3218 struct lfsck_thread_info *info = lfsck_env_info(env);
3219 struct lu_attr *la = &info->lti_la;
3220 struct lu_name *cname = &info->lti_name;
3221 struct lu_fid *pfid = &info->lti_fid;
3222 struct lu_fid *cfid = &info->lti_fid2;
3223 struct lfsck_instance *lfsck = com->lc_lfsck;
3224 struct lfsck_namespace *ns = com->lc_file_ram;
3225 struct dt_object *parent = NULL;
3226 struct linkea_data ldata = { 0 };
3227 bool repaired = false;
3232 dt_read_lock(env, child, 0);
3233 if (unlikely(lfsck_is_dead_obj(child))) {
3234 dt_read_unlock(env, child);
3239 if (S_ISDIR(lfsck_object_type(child))) {
3240 dt_read_unlock(env, child);
3241 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3246 rc = lfsck_links_read(env, child, &ldata);
3247 dt_read_unlock(env, child);
3251 linkea_first_entry(&ldata);
3252 while (ldata.ld_lee != NULL) {
3253 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3255 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3257 /* Found repeated linkEA entries */
3259 rc = lfsck_namespace_shrink_linkea(env, com, child,
3260 &ldata, cname, pfid, false);
3272 /* Invalid PFID in the linkEA entry. */
3273 if (!fid_is_sane(pfid)) {
3274 rc = lfsck_namespace_shrink_linkea(env, com, child,
3275 &ldata, cname, pfid, true);
3285 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3287 GOTO(out, rc = PTR_ERR(parent));
3289 if (!dt_object_exists(parent)) {
3292 if (ldata.ld_leh->leh_reccount > 1) {
3293 /* If it is NOT the last linkEA entry, then
3294 * there is still other chance to make the
3295 * child to be visible via other parent, then
3296 * remove this linkEA entry. */
3297 rc = lfsck_namespace_shrink_linkea(env, com,
3298 child, &ldata, cname, pfid, true);
3300 /* If the LFSCK is marked as LF_INCOMPLETE,
3301 * then means some MDT has ever tried to
3302 * verify some remote MDT-object that resides
3303 * on this MDT, but this MDT failed to respond
3304 * such request. So means there may be some
3305 * remote name entry on other MDT that
3306 * references this object with another name,
3307 * so we cannot know whether this linkEA is
3308 * valid or not. So keep it there and maybe
3309 * resolved when next LFSCK run. */
3310 if (ns->ln_flags & LF_INCOMPLETE) {
3311 lfsck_object_put(env, parent);
3316 /* Create the lost parent as an orphan. */
3317 rc = lfsck_namespace_create_orphan(env, com,
3320 lfsck_object_put(env, parent);
3328 /* Add the missing name entry to the parent. */
3329 rc = lfsck_namespace_insert_normal(env, com,
3330 parent, child, cname->ln_name);
3331 if (unlikely(rc == -EEXIST))
3332 /* Unfortunately, someone reused the
3333 * name under the parent by race. So we
3334 * have to remove the linkEA entry from
3335 * current child object. It means that
3336 * the LFSCK cannot recover the system
3337 * totally back to its original status,
3338 * but it is necessary to make the
3339 * current system to be consistent. */
3340 rc = lfsck_namespace_shrink_linkea(env,
3344 linkea_next_entry(&ldata);
3347 lfsck_object_put(env, parent);
3357 /* The linkEA entry with bad parent will be removed. */
3358 if (unlikely(!dt_try_as_dir(env, parent))) {
3359 lfsck_object_put(env, parent);
3360 rc = lfsck_namespace_shrink_linkea(env, com, child,
3361 &ldata, cname, pfid, true);
3371 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3372 (const struct dt_key *)cname->ln_name,
3374 if (rc != 0 && rc != -ENOENT) {
3375 lfsck_object_put(env, parent);
3381 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3382 /* It is the most common case that we
3383 * find the name entry corresponding
3384 * to the linkEA entry. */
3385 lfsck_object_put(env, parent);
3386 linkea_next_entry(&ldata);
3388 /* The name entry references another
3389 * MDT-object that may be created by
3390 * the LFSCK for repairing dangling
3391 * name entry. Try to replace it. */
3392 rc = lfsck_namespace_replace_cond(env, com,
3393 parent, child, cfid, cname);
3394 lfsck_object_put(env, parent);
3400 linkea_next_entry(&ldata);
3402 rc = lfsck_namespace_shrink_linkea(env,
3416 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3420 /* If there is no name entry in the parent dir and the object
3421 * link count is less than the linkea entries count, then the
3422 * linkea entry should be removed. */
3423 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3424 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3425 parent, child, &ldata, cname, pfid);
3426 lfsck_object_put(env, parent);
3436 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3437 * MDT has ever tried to verify some remote MDT-object that
3438 * resides on this MDT, but this MDT failed to respond such
3439 * request. So means there may be some remote name entry on
3440 * other MDT that references this object with another name,
3441 * so we cannot know whether this linkEA is valid or not.
3442 * So keep it there and maybe resolved when next LFSCK run. */
3443 if (ns->ln_flags & LF_INCOMPLETE) {
3444 lfsck_object_put(env, parent);
3449 rc = lfsck_namespace_check_name(env, parent, child, cname);
3454 lfsck_object_put(env, parent);
3459 /* It is an invalid name entry, drop it. */
3460 if (unlikely(rc > 0)) {
3461 lfsck_object_put(env, parent);
3462 rc = lfsck_namespace_shrink_linkea(env, com, child,
3463 &ldata, cname, pfid, true);
3473 /* Add the missing name entry back to the namespace. */
3474 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3476 if (unlikely(rc == -ESTALE))
3477 /* It may happen when the remote object has been
3478 * removed, but the local MDT is not aware of that. */
3481 if (unlikely(rc == -EEXIST))
3482 /* Unfortunately, someone reused the name under the
3483 * parent by race. So we have to remove the linkEA
3484 * entry from current child object. It means that the
3485 * LFSCK cannot recover the system totally back to
3486 * its original status, but it is necessary to make
3487 * the current system to be consistent.
3489 * It also may be because of the LFSCK found some
3490 * internal status of create operation. Under such
3491 * case, nothing to be done. */
3492 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3493 parent, child, &ldata, cname, pfid);
3495 linkea_next_entry(&ldata);
3497 lfsck_object_put(env, parent);
3508 if (rc < 0 && rc != -ENODATA)
3512 LASSERT(ldata.ld_leh != NULL);
3514 count = ldata.ld_leh->leh_reccount;
3517 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3518 * MDT has ever tried to verify some remote MDT-object that
3519 * resides on this MDT, but this MDT failed to respond such
3520 * request. So means there may be some remote name entry on
3521 * other MDT that references this object with another name,
3522 * so we cannot know whether this linkEA is valid or not.
3523 * So keep it there and maybe resolved when next LFSCK run. */
3524 if (count == 0 && !(ns->ln_flags & LF_INCOMPLETE)) {
3525 /* If the child becomes orphan, then insert it into
3526 * the global .lustre/lost+found/MDTxxxx directory. */
3527 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
3533 ns->ln_mul_ref_repaired++;
3538 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3542 if (la->la_nlink != count) {
3543 rc = lfsck_namespace_repair_nlink(env, com, child,
3546 ns->ln_objs_nlink_repaired++;
3552 if (la->la_nlink > 1)
3553 ns->ln_mul_linked_repaired++;
3562 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3563 struct lfsck_namespace *ns,
3564 __u64 checked_phase1,
3565 __u64 checked_phase2,
3569 seq_printf(m, "checked_phase1: "LPU64"\n"
3570 "checked_phase2: "LPU64"\n"
3571 "updated_phase1: "LPU64"\n"
3572 "updated_phase2: "LPU64"\n"
3573 "failed_phase1: "LPU64"\n"
3574 "failed_phase2: "LPU64"\n"
3575 "directories: "LPU64"\n"
3576 "dirent_repaired: "LPU64"\n"
3577 "linkea_repaired: "LPU64"\n"
3578 "nlinks_repaired: "LPU64"\n"
3579 "multiple_linked_checked: "LPU64"\n"
3580 "multiple_linked_repaired: "LPU64"\n"
3581 "unknown_inconsistency: "LPU64"\n"
3582 "unmatched_pairs_repaired: "LPU64"\n"
3583 "dangling_repaired: "LPU64"\n"
3584 "multiple_referenced_repaired: "LPU64"\n"
3585 "bad_file_type_repaired: "LPU64"\n"
3586 "lost_dirent_repaired: "LPU64"\n"
3587 "local_lost_found_scanned: "LPU64"\n"
3588 "local_lost_found_moved: "LPU64"\n"
3589 "local_lost_found_skipped: "LPU64"\n"
3590 "local_lost_found_failed: "LPU64"\n"
3591 "striped_dirs_scanned: "LPU64"\n"
3592 "striped_dirs_repaired: "LPU64"\n"
3593 "striped_dirs_failed: "LPU64"\n"
3594 "striped_dirs_disabled: "LPU64"\n"
3595 "striped_dirs_skipped: "LPU64"\n"
3596 "striped_shards_scanned: "LPU64"\n"
3597 "striped_shards_repaired: "LPU64"\n"
3598 "striped_shards_failed: "LPU64"\n"
3599 "striped_shards_skipped: "LPU64"\n"
3600 "name_hash_repaired: "LPU64"\n"
3601 "success_count: %u\n"
3602 "run_time_phase1: %u seconds\n"
3603 "run_time_phase2: %u seconds\n",
3606 ns->ln_items_repaired,
3607 ns->ln_objs_repaired_phase2,
3608 ns->ln_items_failed,
3609 ns->ln_objs_failed_phase2,
3610 ns->ln_dirs_checked,
3611 ns->ln_dirent_repaired,
3612 ns->ln_linkea_repaired,
3613 ns->ln_objs_nlink_repaired,
3614 ns->ln_mul_linked_checked,
3615 ns->ln_mul_linked_repaired,
3616 ns->ln_unknown_inconsistency,
3617 ns->ln_unmatched_pairs_repaired,
3618 ns->ln_dangling_repaired,
3619 ns->ln_mul_ref_repaired,
3620 ns->ln_bad_type_repaired,
3621 ns->ln_lost_dirent_repaired,
3622 ns->ln_local_lpf_scanned,
3623 ns->ln_local_lpf_moved,
3624 ns->ln_local_lpf_skipped,
3625 ns->ln_local_lpf_failed,
3626 ns->ln_striped_dirs_scanned,
3627 ns->ln_striped_dirs_repaired,
3628 ns->ln_striped_dirs_failed,
3629 ns->ln_striped_dirs_disabled,
3630 ns->ln_striped_dirs_skipped,
3631 ns->ln_striped_shards_scanned,
3632 ns->ln_striped_shards_repaired,
3633 ns->ln_striped_shards_failed,
3634 ns->ln_striped_shards_skipped,
3635 ns->ln_name_hash_repaired,
3636 ns->ln_success_count,
3641 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3642 struct lfsck_component *com)
3644 struct lfsck_instance *lfsck = com->lc_lfsck;
3645 struct lfsck_namespace *ns = com->lc_file_ram;
3647 while (!list_empty(&lfsck->li_list_lmv)) {
3648 struct lfsck_lmv_unit *llu;
3649 struct lfsck_lmv *llmv;
3651 llu = list_entry(lfsck->li_list_lmv.next,
3652 struct lfsck_lmv_unit, llu_link);
3653 llmv = &llu->llu_lmv;
3655 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3656 "still in using: %u\n",
3657 atomic_read(&llmv->ll_ref));
3659 ns->ln_striped_dirs_skipped++;
3660 lfsck_lmv_put(env, llmv);
3664 /* namespace APIs */
3666 static int lfsck_namespace_reset(const struct lu_env *env,
3667 struct lfsck_component *com, bool init)
3669 struct lfsck_instance *lfsck = com->lc_lfsck;
3670 struct lfsck_namespace *ns = com->lc_file_ram;
3671 struct lfsck_assistant_data *lad = com->lc_data;
3672 struct dt_object *root;
3673 struct dt_object *dto;
3677 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3679 GOTO(log, rc = PTR_ERR(root));
3681 if (unlikely(!dt_try_as_dir(env, root)))
3682 GOTO(put, rc = -ENOTDIR);
3684 down_write(&com->lc_sem);
3686 memset(ns, 0, sizeof(*ns));
3688 __u32 count = ns->ln_success_count;
3689 __u64 last_time = ns->ln_time_last_complete;
3691 memset(ns, 0, sizeof(*ns));
3692 ns->ln_success_count = count;
3693 ns->ln_time_last_complete = last_time;
3695 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3696 ns->ln_status = LS_INIT;
3698 rc = local_object_unlink(env, lfsck->li_bottom, root,
3699 lfsck_namespace_name);
3703 lfsck_object_put(env, com->lc_obj);
3705 dto = local_index_find_or_create(env, lfsck->li_los, root,
3706 lfsck_namespace_name,
3707 S_IFREG | S_IRUGO | S_IWUSR,
3708 &dt_lfsck_features);
3710 GOTO(out, rc = PTR_ERR(dto));
3713 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
3717 lad->lad_incomplete = 0;
3718 CFS_RESET_BITMAP(lad->lad_bitmap);
3720 rc = lfsck_namespace_store(env, com);
3725 up_write(&com->lc_sem);
3728 lu_object_put(env, &root->do_lu);
3730 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3731 lfsck_lfsck2name(lfsck), rc);
3736 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3739 struct lfsck_namespace *ns = com->lc_file_ram;
3741 down_write(&com->lc_sem);
3743 com->lc_new_checked++;
3744 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3745 up_write(&com->lc_sem);
3748 static void lfsck_namespace_close_dir(const struct lu_env *env,
3749 struct lfsck_component *com)
3751 struct lfsck_namespace *ns = com->lc_file_ram;
3752 struct lfsck_assistant_data *lad = com->lc_data;
3753 struct lfsck_instance *lfsck = com->lc_lfsck;
3754 struct lfsck_lmv *llmv = lfsck->li_lmv;
3755 struct lfsck_namespace_req *lnr;
3757 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3758 bool wakeup = false;
3764 OBD_ALLOC(lnr, size);
3766 ns->ln_striped_dirs_skipped++;
3771 /* Generate a dummy request to indicate that all shards' name entry
3772 * in this striped directory has been scanned for the first time. */
3773 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3774 lnr->lnr_obj = lfsck_object_get(lfsck->li_obj_dir);
3775 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3776 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3777 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3778 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3779 lnr->lnr_size = size;
3781 spin_lock(&lad->lad_lock);
3782 if (lad->lad_assistant_status < 0) {
3783 spin_unlock(&lad->lad_lock);
3784 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3785 ns->ln_striped_dirs_skipped++;
3790 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3791 if (lad->lad_prefetched == 0)
3794 lad->lad_prefetched++;
3795 spin_unlock(&lad->lad_lock);
3797 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3802 static int lfsck_namespace_open_dir(const struct lu_env *env,
3803 struct lfsck_component *com)
3805 struct lfsck_instance *lfsck = com->lc_lfsck;
3806 struct lfsck_namespace *ns = com->lc_file_ram;
3807 struct lfsck_lmv *llmv = lfsck->li_lmv;
3814 if (llmv->ll_lmv_master) {
3815 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3817 if (lmv->lmv_master_mdt_index !=
3818 lfsck_dev_idx(lfsck->li_bottom)) {
3819 lmv->lmv_master_mdt_index =
3820 lfsck_dev_idx(lfsck->li_bottom);
3821 ns->ln_flags |= LF_INCONSISTENT;
3822 llmv->ll_lmv_updated = 1;
3825 rc = lfsck_namespace_verify_stripe_slave(env, com,
3826 lfsck->li_obj_dir, llmv);
3829 RETURN(rc > 0 ? 0 : rc);
3832 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3833 struct lfsck_component *com, bool init)
3835 struct lfsck_instance *lfsck = com->lc_lfsck;
3836 struct lfsck_namespace *ns = com->lc_file_ram;
3840 rc = lfsck_checkpoint_generic(env, com);
3845 down_write(&com->lc_sem);
3847 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3849 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3850 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3851 HALF_SEC - lfsck->li_time_last_checkpoint);
3852 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3853 ns->ln_items_checked += com->lc_new_checked;
3854 com->lc_new_checked = 0;
3857 rc = lfsck_namespace_store(env, com);
3858 up_write(&com->lc_sem);
3861 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3862 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3863 lfsck->li_pos_current.lp_oit_cookie,
3864 PFID(&lfsck->li_pos_current.lp_dir_parent),
3865 lfsck->li_pos_current.lp_dir_cookie, rc);
3867 return rc > 0 ? 0 : rc;
3870 static int lfsck_namespace_prep(const struct lu_env *env,
3871 struct lfsck_component *com,
3872 struct lfsck_start_param *lsp)
3874 struct lfsck_instance *lfsck = com->lc_lfsck;
3875 struct lfsck_namespace *ns = com->lc_file_ram;
3876 struct lfsck_position *pos = &com->lc_pos_start;
3879 rc = lfsck_namespace_load_bitmap(env, com);
3880 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3881 rc = lfsck_namespace_reset(env, com, false);
3883 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3886 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3887 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3893 down_write(&com->lc_sem);
3894 ns->ln_time_latest_start = cfs_time_current_sec();
3895 spin_lock(&lfsck->li_lock);
3897 if (ns->ln_flags & LF_SCANNED_ONCE) {
3898 if (!lfsck->li_drop_dryrun ||
3899 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3900 ns->ln_status = LS_SCANNING_PHASE2;
3901 list_move_tail(&com->lc_link,
3902 &lfsck->li_list_double_scan);
3903 if (!list_empty(&com->lc_link_dir))
3904 list_del_init(&com->lc_link_dir);
3905 lfsck_pos_set_zero(pos);
3907 ns->ln_status = LS_SCANNING_PHASE1;
3908 ns->ln_run_time_phase1 = 0;
3909 ns->ln_run_time_phase2 = 0;
3910 ns->ln_items_checked = 0;
3911 ns->ln_items_repaired = 0;
3912 ns->ln_items_failed = 0;
3913 ns->ln_dirs_checked = 0;
3914 ns->ln_objs_checked_phase2 = 0;
3915 ns->ln_objs_repaired_phase2 = 0;
3916 ns->ln_objs_failed_phase2 = 0;
3917 ns->ln_objs_nlink_repaired = 0;
3918 ns->ln_dirent_repaired = 0;
3919 ns->ln_linkea_repaired = 0;
3920 ns->ln_mul_linked_checked = 0;
3921 ns->ln_mul_linked_repaired = 0;
3922 ns->ln_unknown_inconsistency = 0;
3923 ns->ln_unmatched_pairs_repaired = 0;
3924 ns->ln_dangling_repaired = 0;
3925 ns->ln_mul_ref_repaired = 0;
3926 ns->ln_bad_type_repaired = 0;
3927 ns->ln_lost_dirent_repaired = 0;
3928 ns->ln_striped_dirs_scanned = 0;
3929 ns->ln_striped_dirs_repaired = 0;
3930 ns->ln_striped_dirs_failed = 0;
3931 ns->ln_striped_dirs_disabled = 0;
3932 ns->ln_striped_dirs_skipped = 0;
3933 ns->ln_striped_shards_scanned = 0;
3934 ns->ln_striped_shards_repaired = 0;
3935 ns->ln_striped_shards_failed = 0;
3936 ns->ln_striped_shards_skipped = 0;
3937 ns->ln_name_hash_repaired = 0;
3938 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3939 if (list_empty(&com->lc_link_dir))
3940 list_add_tail(&com->lc_link_dir,
3941 &lfsck->li_list_dir);
3942 *pos = ns->ln_pos_first_inconsistent;
3945 ns->ln_status = LS_SCANNING_PHASE1;
3946 if (list_empty(&com->lc_link_dir))
3947 list_add_tail(&com->lc_link_dir,
3948 &lfsck->li_list_dir);
3949 if (!lfsck->li_drop_dryrun ||
3950 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3951 *pos = ns->ln_pos_last_checkpoint;
3952 pos->lp_oit_cookie++;
3954 *pos = ns->ln_pos_first_inconsistent;
3958 spin_unlock(&lfsck->li_lock);
3959 up_write(&com->lc_sem);
3961 rc = lfsck_start_assistant(env, com, lsp);
3963 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
3964 DFID", "LPX64"]: rc = %d\n",
3965 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
3966 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
3971 static int lfsck_namespace_exec_oit(const struct lu_env *env,
3972 struct lfsck_component *com,
3973 struct dt_object *obj)
3975 struct lfsck_thread_info *info = lfsck_env_info(env);
3976 struct lfsck_namespace *ns = com->lc_file_ram;
3977 struct lfsck_instance *lfsck = com->lc_lfsck;
3978 const struct lu_fid *fid = lfsck_dto2fid(obj);
3979 struct lu_attr *la = &info->lti_la;
3980 struct lu_fid *pfid = &info->lti_fid2;
3981 struct lu_name *cname = &info->lti_name;
3982 struct lu_seq_range *range = &info->lti_range;
3983 struct dt_device *dev = lfsck->li_bottom;
3984 struct seq_server_site *ss =
3985 lu_site2seq(dev->dd_lu_dev.ld_site);
3986 struct linkea_data ldata = { 0 };
3987 __u32 idx = lfsck_dev_idx(dev);
3991 rc = lfsck_links_read(env, obj, &ldata);
3995 /* -EINVAL means crashed linkEA, should be verified. */
3996 if (rc == -EINVAL) {
3997 rc = lfsck_namespace_trace_update(env, com, fid,
3998 LNTF_CHECK_LINKEA, true);
4000 struct lustre_handle lh = { 0 };
4002 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4003 MDS_INODELOCK_UPDATE |
4004 MDS_INODELOCK_XATTR, LCK_EX);
4006 rc = lfsck_namespace_links_remove(env, com,
4008 lfsck_ibits_unlock(&lh, LCK_EX);
4012 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4015 /* zero-linkEA object may be orphan, but it also maybe because
4016 * of upgrading. Currently, we cannot record it for double scan.
4017 * Because it may cause the LFSCK trace file to be too large. */
4018 if (rc == -ENODATA) {
4019 if (S_ISDIR(lfsck_object_type(obj)))
4022 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4026 /* "la_ctime" == 1 means that it has ever been removed from
4027 * backend /lost+found directory but not been added back to
4028 * the normal namespace yet. */
4029 if (la->la_nlink > 1 || unlikely(la->la_ctime == 1))
4030 rc = lfsck_namespace_trace_update(env, com, fid,
4031 LNTF_CHECK_LINKEA, true);
4039 /* Record multiple-linked object. */
4040 if (ldata.ld_leh->leh_reccount > 1) {
4041 rc = lfsck_namespace_trace_update(env, com, fid,
4042 LNTF_CHECK_LINKEA, true);
4047 linkea_first_entry(&ldata);
4048 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4049 if (!fid_is_sane(pfid)) {
4050 rc = lfsck_namespace_trace_update(env, com, fid,
4051 LNTF_CHECK_PARENT, true);
4053 fld_range_set_mdt(range);
4054 rc = fld_local_lookup(env, ss->ss_server_fld,
4055 fid_seq(pfid), range);
4056 if ((rc == -ENOENT) ||
4057 (rc == 0 && range->lsr_index != idx)) {
4058 rc = lfsck_namespace_trace_update(env, com, fid,
4059 LNTF_CHECK_LINKEA, true);
4061 if (S_ISDIR(lfsck_object_type(obj)))
4064 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4068 /* "la_ctime" == 1 means that it has ever been
4069 * removed from backend /lost+found directory but
4070 * not been added back to the normal namespace yet. */
4071 if (la->la_nlink > 1 || unlikely(la->la_ctime == 1))
4072 rc = lfsck_namespace_trace_update(env, com,
4073 fid, LNTF_CHECK_LINKEA, true);
4080 down_write(&com->lc_sem);
4081 com->lc_new_checked++;
4082 if (S_ISDIR(lfsck_object_type(obj)))
4083 ns->ln_dirs_checked++;
4085 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4086 up_write(&com->lc_sem);
4091 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4092 struct lfsck_component *com,
4093 struct lu_dirent *ent, __u16 type)
4095 struct lfsck_assistant_data *lad = com->lc_data;
4096 struct lfsck_namespace_req *lnr;
4097 bool wakeup = false;
4099 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4101 struct lfsck_namespace *ns = com->lc_file_ram;
4103 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4104 return PTR_ERR(lnr);
4107 spin_lock(&lad->lad_lock);
4108 if (lad->lad_assistant_status < 0) {
4109 spin_unlock(&lad->lad_lock);
4110 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4111 return lad->lad_assistant_status;
4114 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4115 if (lad->lad_prefetched == 0)
4118 lad->lad_prefetched++;
4119 spin_unlock(&lad->lad_lock);
4121 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4123 down_write(&com->lc_sem);
4124 com->lc_new_checked++;
4125 up_write(&com->lc_sem);
4130 static int lfsck_namespace_post(const struct lu_env *env,
4131 struct lfsck_component *com,
4132 int result, bool init)
4134 struct lfsck_instance *lfsck = com->lc_lfsck;
4135 struct lfsck_namespace *ns = com->lc_file_ram;
4139 lfsck_post_generic(env, com, &result);
4141 down_write(&com->lc_sem);
4142 lfsck_namespace_release_lmv(env, com);
4144 spin_lock(&lfsck->li_lock);
4146 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4148 ns->ln_status = LS_SCANNING_PHASE2;
4149 ns->ln_flags |= LF_SCANNED_ONCE;
4150 ns->ln_flags &= ~LF_UPGRADE;
4151 list_del_init(&com->lc_link_dir);
4152 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4153 } else if (result == 0) {
4154 if (lfsck->li_status != 0)
4155 ns->ln_status = lfsck->li_status;
4157 ns->ln_status = LS_STOPPED;
4158 if (ns->ln_status != LS_PAUSED) {
4159 list_del_init(&com->lc_link_dir);
4160 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4163 ns->ln_status = LS_FAILED;
4164 list_del_init(&com->lc_link_dir);
4165 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4167 spin_unlock(&lfsck->li_lock);
4170 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4171 HALF_SEC - lfsck->li_time_last_checkpoint);
4172 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4173 ns->ln_items_checked += com->lc_new_checked;
4174 com->lc_new_checked = 0;
4177 rc = lfsck_namespace_store(env, com);
4178 up_write(&com->lc_sem);
4180 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4181 lfsck_lfsck2name(lfsck), rc);
4187 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4190 struct lfsck_instance *lfsck = com->lc_lfsck;
4191 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4192 struct lfsck_namespace *ns = com->lc_file_ram;
4195 down_read(&com->lc_sem);
4196 seq_printf(m, "name: lfsck_namespace\n"
4202 lfsck_status2names(ns->ln_status));
4204 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4208 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4212 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4213 "time_since_last_completed");
4217 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4218 "time_since_latest_start");
4222 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4223 "time_since_last_checkpoint");
4227 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4228 "latest_start_position");
4232 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4233 "last_checkpoint_position");
4237 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4238 "first_failure_position");
4242 if (ns->ln_status == LS_SCANNING_PHASE1) {
4243 struct lfsck_position pos;
4244 const struct dt_it_ops *iops;
4245 cfs_duration_t duration = cfs_time_current() -
4246 lfsck->li_time_last_checkpoint;
4247 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4248 __u64 speed = checked;
4249 __u64 new_checked = msecs_to_jiffies(com->lc_new_checked *
4251 __u32 rtime = ns->ln_run_time_phase1 +
4252 cfs_duration_sec(duration + HALF_SEC);
4255 do_div(new_checked, duration);
4257 do_div(speed, rtime);
4258 lfsck_namespace_dump_statistics(m, ns, checked,
4259 ns->ln_objs_checked_phase2,
4260 rtime, ns->ln_run_time_phase2);
4262 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4263 "average_speed_phase2: N/A\n"
4264 "real_time_speed_phase1: "LPU64" items/sec\n"
4265 "real_time_speed_phase2: N/A\n",
4269 LASSERT(lfsck->li_di_oit != NULL);
4271 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4273 /* The low layer otable-based iteration position may NOT
4274 * exactly match the namespace-based directory traversal
4275 * cookie. Generally, it is not a serious issue. But the
4276 * caller should NOT make assumption on that. */
4277 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4278 if (!lfsck->li_current_oit_processed)
4279 pos.lp_oit_cookie--;
4281 spin_lock(&lfsck->li_lock);
4282 if (lfsck->li_di_dir != NULL) {
4283 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4284 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4285 fid_zero(&pos.lp_dir_parent);
4286 pos.lp_dir_cookie = 0;
4289 *lfsck_dto2fid(lfsck->li_obj_dir);
4292 fid_zero(&pos.lp_dir_parent);
4293 pos.lp_dir_cookie = 0;
4295 spin_unlock(&lfsck->li_lock);
4296 lfsck_pos_dump(m, &pos, "current_position");
4297 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4298 cfs_duration_t duration = cfs_time_current() -
4299 lfsck->li_time_last_checkpoint;
4300 __u64 checked = ns->ln_objs_checked_phase2 +
4301 com->lc_new_checked;
4302 __u64 speed1 = ns->ln_items_checked;
4303 __u64 speed2 = checked;
4304 __u64 new_checked = msecs_to_jiffies(com->lc_new_checked *
4306 __u32 rtime = ns->ln_run_time_phase2 +
4307 cfs_duration_sec(duration + HALF_SEC);
4310 do_div(new_checked, duration);
4311 if (ns->ln_run_time_phase1 != 0)
4312 do_div(speed1, ns->ln_run_time_phase1);
4314 do_div(speed2, rtime);
4315 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4317 ns->ln_run_time_phase1, rtime);
4319 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4320 "average_speed_phase2: "LPU64" objs/sec\n"
4321 "real_time_speed_phase1: N/A\n"
4322 "real_time_speed_phase2: "LPU64" objs/sec\n"
4323 "current_position: "DFID"\n",
4327 PFID(&ns->ln_fid_latest_scanned_phase2));
4329 __u64 speed1 = ns->ln_items_checked;
4330 __u64 speed2 = ns->ln_objs_checked_phase2;
4332 if (ns->ln_run_time_phase1 != 0)
4333 do_div(speed1, ns->ln_run_time_phase1);
4334 if (ns->ln_run_time_phase2 != 0)
4335 do_div(speed2, ns->ln_run_time_phase2);
4336 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4337 ns->ln_objs_checked_phase2,
4338 ns->ln_run_time_phase1,
4339 ns->ln_run_time_phase2);
4341 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4342 "average_speed_phase2: "LPU64" objs/sec\n"
4343 "real_time_speed_phase1: N/A\n"
4344 "real_time_speed_phase2: N/A\n"
4345 "current_position: N/A\n",
4350 up_read(&com->lc_sem);
4354 static int lfsck_namespace_double_scan(const struct lu_env *env,
4355 struct lfsck_component *com)
4357 struct lfsck_namespace *ns = com->lc_file_ram;
4358 struct lfsck_assistant_data *lad = com->lc_data;
4359 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4360 struct lfsck_tgt_desc *ltd;
4361 struct lfsck_tgt_desc *next;
4364 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4365 if (thread_is_stopped(&lad->lad_thread)) {
4366 LASSERT(list_empty(&lad->lad_req_list));
4367 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4369 spin_lock(<ds->ltd_lock);
4370 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4371 ltd_namespace_phase_list) {
4372 list_del_init(<d->ltd_namespace_phase_list);
4374 spin_unlock(<ds->ltd_lock);
4380 static void lfsck_namespace_data_release(const struct lu_env *env,
4381 struct lfsck_component *com)
4383 struct lfsck_assistant_data *lad = com->lc_data;
4384 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4385 struct lfsck_tgt_desc *ltd;
4386 struct lfsck_tgt_desc *next;
4388 LASSERT(lad != NULL);
4389 LASSERT(thread_is_init(&lad->lad_thread) ||
4390 thread_is_stopped(&lad->lad_thread));
4391 LASSERT(list_empty(&lad->lad_req_list));
4393 com->lc_data = NULL;
4394 lfsck_namespace_release_lmv(env, com);
4396 spin_lock(<ds->ltd_lock);
4397 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4398 ltd_namespace_phase_list) {
4399 list_del_init(<d->ltd_namespace_phase_list);
4401 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4402 ltd_namespace_phase_list) {
4403 list_del_init(<d->ltd_namespace_phase_list);
4405 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4406 ltd_namespace_list) {
4407 list_del_init(<d->ltd_namespace_list);
4409 spin_unlock(<ds->ltd_lock);
4411 if (likely(lad->lad_bitmap != NULL))
4412 CFS_FREE_BITMAP(lad->lad_bitmap);
4417 static void lfsck_namespace_quit(const struct lu_env *env,
4418 struct lfsck_component *com)
4420 struct lfsck_assistant_data *lad = com->lc_data;
4421 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4422 struct lfsck_tgt_desc *ltd;
4423 struct lfsck_tgt_desc *next;
4425 LASSERT(lad != NULL);
4427 lfsck_quit_generic(env, com);
4429 LASSERT(thread_is_init(&lad->lad_thread) ||
4430 thread_is_stopped(&lad->lad_thread));
4431 LASSERT(list_empty(&lad->lad_req_list));
4433 lfsck_namespace_release_lmv(env, com);
4435 spin_lock(<ds->ltd_lock);
4436 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4437 ltd_namespace_phase_list) {
4438 list_del_init(<d->ltd_namespace_phase_list);
4440 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4441 ltd_namespace_phase_list) {
4442 list_del_init(<d->ltd_namespace_phase_list);
4444 spin_unlock(<ds->ltd_lock);
4447 static int lfsck_namespace_in_notify(const struct lu_env *env,
4448 struct lfsck_component *com,
4449 struct lfsck_request *lr,
4452 struct lfsck_instance *lfsck = com->lc_lfsck;
4453 struct lfsck_namespace *ns = com->lc_file_ram;
4454 struct lfsck_assistant_data *lad = com->lc_data;
4455 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4456 struct lfsck_tgt_desc *ltd;
4461 switch (lr->lr_event) {
4462 case LE_CREATE_ORPHAN: {
4463 struct dt_object *orphan = NULL;
4464 struct lmv_mds_md_v1 *lmv;
4466 CDEBUG(D_LFSCK, "%s: namespace LFSCK handling notify from "
4467 "MDT %x to create orphan"DFID" with type %o\n",
4468 lfsck_lfsck2name(lfsck), lr->lr_index,
4469 PFID(&lr->lr_fid), lr->lr_type);
4471 orphan = lfsck_object_find(env, lfsck, &lr->lr_fid);
4473 GOTO(out_create, rc = PTR_ERR(orphan));
4475 if (dt_object_exists(orphan))
4476 GOTO(out_create, rc = -EEXIST);
4478 if (lr->lr_stripe_count > 0) {
4479 lmv = &lfsck_env_info(env)->lti_lmv;
4480 memset(lmv, 0, sizeof(*lmv));
4481 lmv->lmv_hash_type = lr->lr_hash_type;
4482 lmv->lmv_stripe_count = lr->lr_stripe_count;
4483 lmv->lmv_layout_version = lr->lr_layout_version;
4484 memcpy(lmv->lmv_pool_name, lr->lr_pool_name,
4485 sizeof(lmv->lmv_pool_name));
4490 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
4493 GOTO(out_create, rc = (rc == 1) ? 0 : rc);
4496 CDEBUG(D_LFSCK, "%s: namespace LFSCK handled notify from "
4497 "MDT %x to create orphan"DFID" with type %o: rc = %d\n",
4498 lfsck_lfsck2name(lfsck), lr->lr_index,
4499 PFID(&lr->lr_fid), lr->lr_type, rc);
4501 if (orphan != NULL && !IS_ERR(orphan))
4502 lfsck_object_put(env, orphan);
4506 case LE_SKIP_NLINK_DECLARE: {
4507 struct dt_object *obj = com->lc_obj;
4508 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4511 LASSERT(th != NULL);
4513 rc = dt_declare_delete(env, obj,
4514 (const struct dt_key *)key, th);
4516 rc = dt_declare_insert(env, obj,
4517 (const struct dt_rec *)&flags,
4518 (const struct dt_key *)key, th);
4522 case LE_SKIP_NLINK: {
4523 struct dt_object *obj = com->lc_obj;
4524 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4529 LASSERT(th != NULL);
4531 fid_cpu_to_be(key, &lr->lr_fid);
4532 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4533 (const struct dt_key *)key, BYPASS_CAPA);
4535 if (flags & LNTF_SKIP_NLINK)
4539 } else if (rc != -ENOENT) {
4543 flags |= LNTF_SKIP_NLINK;
4545 rc = dt_delete(env, obj, (const struct dt_key *)key,
4551 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4552 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4557 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4558 " to be skipped for namespace double scan: rc = %d\n",
4559 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4562 /* If we cannot record this object in the LFSCK tracing,
4563 * we have to mark the LFSC as LF_INCOMPLETE, then the
4564 * LFSCK will skip nlink attribute verification for
4566 ns->ln_flags |= LF_INCOMPLETE;
4570 case LE_SET_LMV_MASTER: {
4571 struct dt_object *obj;
4573 obj = lfsck_object_find_by_dev(env, lfsck->li_bottom,
4576 RETURN(PTR_ERR(obj));
4578 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4579 lfsck_object_put(env, obj);
4581 RETURN(rc > 0 ? 0 : rc);
4583 case LE_SET_LMV_SLAVE: {
4584 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4585 ns->ln_striped_shards_repaired++;
4587 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4588 LNTF_RECHECK_NAME_HASH, true);
4590 RETURN(rc > 0 ? 0 : rc);
4592 case LE_PHASE1_DONE:
4593 case LE_PHASE2_DONE:
4600 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4601 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4602 lr->lr_index, lr->lr_status, lr->lr_flags2);
4604 spin_lock(<ds->ltd_lock);
4605 ltd = LTD_TGT(ltds, lr->lr_index);
4607 spin_unlock(<ds->ltd_lock);
4612 list_del_init(<d->ltd_namespace_phase_list);
4613 switch (lr->lr_event) {
4614 case LE_PHASE1_DONE:
4615 if (lr->lr_status <= 0) {
4616 ltd->ltd_namespace_done = 1;
4617 list_del_init(<d->ltd_namespace_list);
4618 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4619 "phase1 for namespace LFSCK: rc = %d.\n",
4620 lfsck_lfsck2name(lfsck),
4621 ltd->ltd_index, lr->lr_status);
4622 ns->ln_flags |= LF_INCOMPLETE;
4627 if (lr->lr_flags2 & LF_INCOMPLETE)
4628 ns->ln_flags |= LF_INCOMPLETE;
4630 if (list_empty(<d->ltd_namespace_list))
4631 list_add_tail(<d->ltd_namespace_list,
4632 &lad->lad_mdt_list);
4633 list_add_tail(<d->ltd_namespace_phase_list,
4634 &lad->lad_mdt_phase2_list);
4636 case LE_PHASE2_DONE:
4637 ltd->ltd_namespace_done = 1;
4638 list_del_init(<d->ltd_namespace_list);
4642 ltd->ltd_namespace_done = 1;
4643 list_del_init(<d->ltd_namespace_list);
4644 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4646 "%s: the peer MDT %x exit namespace LFSCK\n",
4647 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4648 ns->ln_flags |= LF_INCOMPLETE;
4654 spin_unlock(<ds->ltd_lock);
4656 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4657 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4659 memset(stop, 0, sizeof(*stop));
4660 stop->ls_status = lr->lr_status;
4661 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4662 lfsck_stop(env, lfsck->li_bottom, stop);
4663 } else if (lfsck_phase2_next_ready(lad)) {
4664 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4670 static int lfsck_namespace_query(const struct lu_env *env,
4671 struct lfsck_component *com)
4673 struct lfsck_namespace *ns = com->lc_file_ram;
4675 return ns->ln_status;
4678 static struct lfsck_operations lfsck_namespace_ops = {
4679 .lfsck_reset = lfsck_namespace_reset,
4680 .lfsck_fail = lfsck_namespace_fail,
4681 .lfsck_close_dir = lfsck_namespace_close_dir,
4682 .lfsck_open_dir = lfsck_namespace_open_dir,
4683 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4684 .lfsck_prep = lfsck_namespace_prep,
4685 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4686 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4687 .lfsck_post = lfsck_namespace_post,
4688 .lfsck_dump = lfsck_namespace_dump,
4689 .lfsck_double_scan = lfsck_namespace_double_scan,
4690 .lfsck_data_release = lfsck_namespace_data_release,
4691 .lfsck_quit = lfsck_namespace_quit,
4692 .lfsck_in_notify = lfsck_namespace_in_notify,
4693 .lfsck_query = lfsck_namespace_query,
4697 * Repair dangling name entry.
4699 * For the name entry with dangling reference, we need to repare the
4700 * inconsistency according to the LFSCK sponsor's requirement:
4702 * 1) Keep the inconsistency there and report the inconsistency case,
4703 * then give the chance to the application to find related issues,
4704 * and the users can make the decision about how to handle it with
4705 * more human knownledge. (by default)
4707 * 2) Re-create the missing MDT-object with the FID information.
4709 * \param[in] env pointer to the thread context
4710 * \param[in] com pointer to the lfsck component
4711 * \param[in] child pointer to the object corresponding to the dangling
4713 * \param[in] lnr pointer to the namespace request that contains the
4714 * name's name, parent object, parent's LMV, and ect.
4716 * \retval positive number if no need to repair
4717 * \retval zero for repaired successfully
4718 * \retval negative error number on failure
4720 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4721 struct lfsck_component *com,
4722 struct dt_object *child,
4723 struct lfsck_namespace_req *lnr)
4725 struct lfsck_thread_info *info = lfsck_env_info(env);
4726 struct lu_attr *la = &info->lti_la;
4727 struct dt_allocation_hint *hint = &info->lti_hint;
4728 struct dt_object_format *dof = &info->lti_dof;
4729 struct dt_insert_rec *rec = &info->lti_dt_rec;
4730 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4731 struct dt_object *parent = lnr->lnr_obj;
4732 const struct lu_name *cname;
4733 struct linkea_data ldata = { 0 };
4734 struct lustre_handle lh = { 0 };
4735 struct lu_buf linkea_buf;
4736 struct lu_buf lmv_buf;
4737 struct lfsck_instance *lfsck = com->lc_lfsck;
4738 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4739 struct dt_device *dev = lfsck_obj2dt_dev(child);
4740 struct thandle *th = NULL;
4742 __u16 type = lnr->lnr_type;
4746 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4747 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4752 if (!create || bk->lb_param & LPF_DRYRUN)
4755 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4759 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
4763 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4764 MDS_INODELOCK_UPDATE, LCK_EX);
4768 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4772 th = dt_trans_create(env, dev);
4774 GOTO(log, rc = PTR_ERR(th));
4776 /* Set the ctime as zero, then others can know it is created for
4777 * repairing dangling name entry by LFSCK. And if the LFSCK made
4778 * wrong decision and the real MDT-object has been found later,
4779 * then the LFSCK has chance to fix the incosistency properly. */
4780 memset(la, 0, sizeof(*la));
4781 la->la_mode = (type & S_IFMT) | 0600;
4782 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4783 LA_ATIME | LA_MTIME | LA_CTIME;
4785 child->do_ops->do_ah_init(env, hint, parent, child,
4786 la->la_mode & S_IFMT);
4788 memset(dof, 0, sizeof(*dof));
4789 dof->dof_type = dt_mode_to_dft(type);
4790 /* If the target is a regular file, then the LFSCK will only create
4791 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4792 * OST-objects will be created when write open. */
4794 /* 1a. create child. */
4795 rc = dt_declare_create(env, child, la, hint, dof, th);
4799 if (S_ISDIR(type)) {
4800 if (unlikely(!dt_try_as_dir(env, child)))
4801 GOTO(stop, rc = -ENOTDIR);
4803 /* 2a. insert dot into child dir */
4804 rec->rec_type = S_IFDIR;
4805 rec->rec_fid = lfsck_dto2fid(child);
4806 rc = dt_declare_insert(env, child,
4807 (const struct dt_rec *)rec,
4808 (const struct dt_key *)dot, th);
4812 /* 3a. insert dotdot into child dir */
4813 rec->rec_fid = lfsck_dto2fid(parent);
4814 rc = dt_declare_insert(env, child,
4815 (const struct dt_rec *)rec,
4816 (const struct dt_key *)dotdot, th);
4820 /* 4a. increase child nlink */
4821 rc = dt_declare_ref_add(env, child, th);
4825 /* 5a. generate slave LMV EA. */
4826 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4829 idx = lfsck_shard_name_to_index(env,
4830 lnr->lnr_name, lnr->lnr_namelen,
4831 type, lfsck_dto2fid(child));
4832 if (unlikely(idx < 0))
4833 GOTO(stop, rc = idx);
4835 *lmv2 = lnr->lnr_lmv->ll_lmv;
4836 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4837 lmv2->lmv_master_mdt_index = idx;
4839 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4840 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4841 rc = dt_declare_xattr_set(env, child, &lmv_buf,
4842 XATTR_NAME_LMV, 0, th);
4848 /* 6a. insert linkEA for child */
4849 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4850 ldata.ld_leh->leh_len);
4851 rc = dt_declare_xattr_set(env, child, &linkea_buf,
4852 XATTR_NAME_LINK, 0, th);
4856 rc = dt_trans_start(env, dev, th);
4858 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4860 dt_write_lock(env, child, 0);
4861 /* 1b. create child */
4862 rc = dt_create(env, child, la, hint, dof, th);
4864 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4866 if (S_ISDIR(type)) {
4867 if (unlikely(!dt_try_as_dir(env, child)))
4868 GOTO(unlock, rc = -ENOTDIR);
4870 /* 2b. insert dot into child dir */
4871 rec->rec_type = S_IFDIR;
4872 rec->rec_fid = lfsck_dto2fid(child);
4873 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4874 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4878 /* 3b. insert dotdot into child dir */
4879 rec->rec_fid = lfsck_dto2fid(parent);
4880 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4881 (const struct dt_key *)dotdot, th,
4886 /* 4b. increase child nlink */
4887 rc = dt_ref_add(env, child, th);
4891 /* 5b. generate slave LMV EA. */
4892 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4893 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
4894 0, th, BYPASS_CAPA);
4900 /* 6b. insert linkEA for child. */
4901 rc = dt_xattr_set(env, child, &linkea_buf,
4902 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4907 dt_write_unlock(env, child);
4910 dt_trans_stop(env, dev, th);
4913 lfsck_ibits_unlock(&lh, LCK_EX);
4914 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4915 "reference for: parent "DFID", child "DFID", type %u, "
4916 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4917 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4918 type, cname->ln_name,
4919 create ? "Create the lost OST-object as required" :
4920 "Keep the MDT-object there by default", rc);
4923 struct lfsck_namespace *ns = com->lc_file_ram;
4925 ns->ln_flags |= LF_INCONSISTENT;
4931 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4932 struct lfsck_component *com,
4933 struct lfsck_assistant_req *lar)
4935 struct lfsck_thread_info *info = lfsck_env_info(env);
4936 struct lu_attr *la = &info->lti_la;
4937 struct lfsck_instance *lfsck = com->lc_lfsck;
4938 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4939 struct lfsck_namespace *ns = com->lc_file_ram;
4940 struct linkea_data ldata = { 0 };
4941 const struct lu_name *cname;
4942 struct thandle *handle = NULL;
4943 struct lfsck_namespace_req *lnr =
4944 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
4945 struct dt_object *dir = lnr->lnr_obj;
4946 struct dt_object *obj = NULL;
4947 const struct lu_fid *pfid = lfsck_dto2fid(dir);
4948 struct dt_device *dev = NULL;
4949 struct lustre_handle lh = { 0 };
4950 bool repaired = false;
4951 bool dtlocked = false;
4955 bool bad_hash = false;
4959 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
4962 if (lnr->lnr_attr & LUDA_UPGRADE) {
4963 ns->ln_flags |= LF_UPGRADE;
4964 ns->ln_dirent_repaired++;
4966 } else if (lnr->lnr_attr & LUDA_REPAIR) {
4967 ns->ln_flags |= LF_INCONSISTENT;
4968 ns->ln_dirent_repaired++;
4972 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
4973 if (strcmp(lnr->lnr_name, dotdot) != 0)
4976 rc = lfsck_namespace_trace_update(env, com, pfid,
4977 LNTF_CHECK_PARENT, true);
4982 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
4983 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
4988 if (lnr->lnr_name[0] == '.' &&
4989 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
4992 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4993 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
4998 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5000 GOTO(out, rc = idx);
5002 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
5003 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5006 dev = lfsck->li_next;
5008 struct lfsck_tgt_desc *ltd;
5010 /* Usually, some local filesystem consistency verification
5011 * tools can guarantee the local namespace tree consistenct.
5012 * So the LFSCK will only verify the remote directory. */
5013 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5014 rc = lfsck_namespace_trace_update(env, com, pfid,
5015 LNTF_CHECK_PARENT, true);
5020 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5021 if (unlikely(ltd == NULL)) {
5022 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5023 "did not join the namespace LFSCK\n",
5024 lfsck_lfsck2name(lfsck), idx);
5025 lfsck_lad_set_bitmap(env, com, idx);
5027 GOTO(out, rc = -ENODEV);
5033 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5035 GOTO(out, rc = PTR_ERR(obj));
5037 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5038 if (dt_object_exists(obj) == 0) {
5041 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5043 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5044 lnr->lnr_name, lnr->lnr_namelen)) {
5045 type = LNIT_BAD_DIRENT;
5050 type = LNIT_DANGLING;
5051 rc = lfsck_namespace_repair_dangling(env, com,
5060 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5063 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5064 MDS_INODELOCK_UPDATE |
5065 MDS_INODELOCK_XATTR, LCK_EX);
5069 handle = dt_trans_create(env, dev);
5071 GOTO(out, rc = PTR_ERR(handle));
5073 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5077 rc = dt_trans_start(env, dev, handle);
5081 dt_write_lock(env, obj, 0);
5085 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5089 rc = lfsck_links_read(env, obj, &ldata);
5090 if (unlikely(rc == -ENOENT)) {
5091 if (handle != NULL) {
5092 dt_write_unlock(env, obj);
5095 dt_trans_stop(env, dev, handle);
5098 lfsck_ibits_unlock(&lh, LCK_EX);
5101 /* It may happen when the remote object has been removed,
5102 * but the local MDT is not aware of that. */
5104 } else if (rc == 0) {
5105 count = ldata.ld_leh->leh_reccount;
5106 rc = linkea_links_find(&ldata, cname, pfid);
5108 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5109 if ((lfsck_object_type(obj) & S_IFMT) !=
5111 ns->ln_flags |= LF_INCONSISTENT;
5112 type = LNIT_BAD_TYPE;
5118 ns->ln_flags |= LF_INCONSISTENT;
5120 /* If the name entry hash does not match the slave striped
5121 * directory, and the name entry does not match also, then
5122 * it is quite possible that name entry is corrupted. */
5123 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5124 lnr->lnr_name, lnr->lnr_namelen)) {
5125 type = LNIT_BAD_DIRENT;
5130 /* If the file type stored in the name entry does not match
5131 * the file type claimed by the object, and the object does
5132 * not recognize the name entry, then it is quite possible
5133 * that the name entry is corrupted. */
5134 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5135 type = LNIT_BAD_DIRENT;
5140 /* For sub-dir object, we cannot make sure whether the sub-dir
5141 * back references the parent via ".." name entry correctly or
5142 * not in the LFSCK first-stage scanning. It may be that the
5143 * (remote) sub-dir ".." name entry has no parent FID after
5144 * file-level backup/restore and its linkEA may be wrong.
5145 * So under such case, we should replace the linkEA according
5146 * to current name entry. But this needs to be done during the
5147 * LFSCK second-stage scanning. The LFSCK will record the name
5148 * entry for further possible using. */
5152 } else if (unlikely(rc == -EINVAL)) {
5153 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5154 type = LNIT_BAD_TYPE;
5157 ns->ln_flags |= LF_INCONSISTENT;
5158 /* The magic crashed, we are not sure whether there are more
5159 * corrupt data in the linkea, so remove all linkea entries. */
5163 } else if (rc == -ENODATA) {
5164 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5165 type = LNIT_BAD_TYPE;
5168 ns->ln_flags |= LF_UPGRADE;
5173 if (bk->lb_param & LPF_DRYRUN) {
5174 ns->ln_linkea_repaired++;
5180 if (!lustre_handle_is_used(&lh))
5186 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5193 rc = linkea_data_new(&ldata,
5194 &lfsck_env_info(env)->lti_linkea_buf);
5199 rc = linkea_add_buf(&ldata, cname, pfid);
5203 rc = lfsck_links_write(env, obj, &ldata, handle);
5204 if (unlikely(rc == -ENOSPC) &&
5205 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5206 if (handle != NULL) {
5207 LASSERT(dt_write_locked(env, obj));
5209 dt_write_unlock(env, obj);
5212 dt_trans_stop(env, dev, handle);
5215 lfsck_ibits_unlock(&lh, LCK_EX);
5218 rc = lfsck_namespace_trace_update(env, com,
5219 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5221 /* If we cannot record this object in the
5222 * LFSCK tracing, we have to mark the LFSCK
5223 * as LF_INCOMPLETE, then the LFSCK will
5224 * skip nlink attribute verification for
5226 ns->ln_flags |= LF_INCOMPLETE;
5234 count = ldata.ld_leh->leh_reccount;
5235 if (!S_ISDIR(lfsck_object_type(obj)) ||
5236 !dt_object_remote(obj)) {
5237 ns->ln_linkea_repaired++;
5248 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
5252 if ((count == 1 && la->la_nlink == 1) ||
5253 S_ISDIR(lfsck_object_type(obj)))
5254 /* Usually, it is for single linked object or dir, do nothing.*/
5257 /* Following modification will be in another transaction. */
5258 if (handle != NULL) {
5259 dt_write_unlock(env, obj);
5262 dt_trans_stop(env, dev, handle);
5265 lfsck_ibits_unlock(&lh, LCK_EX);
5268 ns->ln_mul_linked_checked++;
5269 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
5270 LNTF_CHECK_LINKEA, true);
5276 dt_write_unlock(env, obj);
5278 if (handle != NULL && !IS_ERR(handle))
5279 dt_trans_stop(env, dev, handle);
5282 lfsck_ibits_unlock(&lh, LCK_EX);
5284 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5285 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5286 lnr->lnr_name, lnr->lnr_namelen) &&
5287 type != LNIT_BAD_DIRENT) {
5288 ns->ln_flags |= LF_INCONSISTENT;
5291 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5292 lnr->lnr_lmv, lnr->lnr_name);
5301 rc = lfsck_namespace_repair_dirent(env, com, dir,
5302 obj, lnr->lnr_name, lnr->lnr_name,
5303 lnr->lnr_type, true, false);
5307 case LNIT_BAD_DIRENT:
5309 /* XXX: This is a bad dirent, we do not know whether
5310 * the original name entry reference a regular
5311 * file or a directory, then keep the parent's
5312 * nlink count unchanged here. */
5313 rc = lfsck_namespace_repair_dirent(env, com, dir,
5314 obj, lnr->lnr_name, lnr->lnr_name,
5315 lnr->lnr_type, false, false);
5324 down_write(&com->lc_sem);
5326 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5327 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5328 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5329 PFID(lfsck_dto2fid(lnr->lnr_obj)),
5330 lnr->lnr_namelen, lnr->lnr_name, rc);
5332 lfsck_namespace_record_failure(env, lfsck, ns);
5333 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5334 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5335 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5336 dev != NULL && dev != lfsck->li_next)
5337 lfsck_lad_set_bitmap(env, com, idx);
5339 if (!(bk->lb_param & LPF_FAILOUT))
5343 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5344 "repaired the entry: "DFID", parent "DFID
5345 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5346 PFID(&lnr->lnr_fid),
5347 PFID(lfsck_dto2fid(lnr->lnr_obj)),
5348 lnr->lnr_namelen, lnr->lnr_name);
5351 ns->ln_items_repaired++;
5355 ns->ln_dangling_repaired++;
5358 ns->ln_bad_type_repaired++;
5360 case LNIT_BAD_DIRENT:
5361 ns->ln_dirent_repaired++;
5367 if (bk->lb_param & LPF_DRYRUN &&
5368 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5369 lfsck_pos_fill(env, lfsck,
5370 &ns->ln_pos_first_inconsistent,
5375 ns->ln_name_hash_repaired++;
5377 /* Not count repeatedly. */
5379 ns->ln_items_repaired++;
5381 if (bk->lb_param & LPF_DRYRUN &&
5382 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5383 lfsck_pos_fill(env, lfsck,
5384 &ns->ln_pos_first_inconsistent,
5391 up_write(&com->lc_sem);
5393 if (obj != NULL && !IS_ERR(obj))
5394 lfsck_object_put(env, obj);
5400 * Handle one orphan under the backend /lost+found directory
5402 * Insert the orphan FID into the namespace LFSCK trace file for further
5403 * processing (via the subsequent namespace LFSCK second-stage scanning).
5404 * At the same time, remove the orphan name entry from backend /lost+found
5405 * directory. There is an interval between the orphan name entry removed
5406 * from the backend /lost+found directory and the orphan FID in the LFSCK
5407 * trace file handled. In such interval, the LFSCK can be reset, then
5408 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5409 * To guarantee that the orphans can be found when LFSCK run next time
5410 * without e2fsck again, when remove the orphan name entry, the LFSCK
5411 * will set the orphan's ctime attribute as 1. Since normal applications
5412 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5413 * next time, it can record the object (that ctime is 1) in the namespace
5414 * LFSCK trace file during the first-stage scanning.
5416 * \param[in] env pointer to the thread context
5417 * \param[in] com pointer to the lfsck component
5418 * \param[in] parent pointer to the object for the backend /lost+found
5419 * \param[in] ent pointer to the name entry for the target under the
5420 * backend /lost+found
5422 * \retval positive for repaired
5423 * \retval 0 if needs to repair nothing
5424 * \retval negative error number on failure
5426 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5427 struct lfsck_component *com,
5428 struct dt_object *parent,
5429 struct lu_dirent *ent)
5431 struct lfsck_thread_info *info = lfsck_env_info(env);
5432 struct lu_fid *key = &info->lti_fid;
5433 struct lu_attr *la = &info->lti_la;
5434 struct lfsck_instance *lfsck = com->lc_lfsck;
5435 struct dt_object *obj = com->lc_obj;
5436 struct dt_device *dev = lfsck->li_bottom;
5437 struct dt_object *child = NULL;
5438 struct thandle *th = NULL;
5444 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5446 RETURN(PTR_ERR(child));
5448 LASSERT(dt_object_exists(child));
5449 LASSERT(!dt_object_remote(child));
5451 fid_cpu_to_be(key, &ent->lde_fid);
5452 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5453 (const struct dt_key *)key, BYPASS_CAPA);
5456 flags |= LNTF_CHECK_ORPHAN;
5457 } else if (rc == -ENOENT) {
5458 flags = LNTF_CHECK_ORPHAN;
5463 th = dt_trans_create(env, dev);
5465 GOTO(out, rc = PTR_ERR(th));
5467 /* a1. remove name entry from backend /lost+found */
5468 rc = dt_declare_delete(env, parent,
5469 (const struct dt_key *)ent->lde_name, th);
5473 if (S_ISDIR(lfsck_object_type(child))) {
5474 /* a2. decrease parent's nlink */
5475 rc = dt_declare_ref_del(env, parent, th);
5481 /* a3. remove child's FID from the LFSCK trace file. */
5482 rc = dt_declare_delete(env, obj,
5483 (const struct dt_key *)key, th);
5487 /* a4. set child's ctime as 1 */
5488 memset(la, 0, sizeof(*la));
5490 la->la_valid = LA_CTIME;
5491 rc = dt_declare_attr_set(env, child, la, th);
5496 /* a5. insert child's FID into the LFSCK trace file. */
5497 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5498 (const struct dt_key *)key, th);
5502 rc = dt_trans_start_local(env, dev, th);
5506 /* b1. remove name entry from backend /lost+found */
5507 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5512 if (S_ISDIR(lfsck_object_type(child))) {
5513 /* b2. decrease parent's nlink */
5514 dt_write_lock(env, parent, 0);
5515 rc = dt_ref_del(env, parent, th);
5516 dt_write_unlock(env, parent);
5522 /* a3. remove child's FID from the LFSCK trace file. */
5523 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5528 /* b4. set child's ctime as 1 */
5529 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5534 /* b5. insert child's FID into the LFSCK trace file. */
5535 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5536 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5538 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5541 dt_trans_stop(env, dev, th);
5544 lu_object_put(env, &child->do_lu);
5550 * Handle orphans under the backend /lost+found directory
5552 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5553 * and put them under the backend /lost+found directory that is invisible
5554 * to client. The LFSCK will scan such directory, for the original client
5555 * visible orphans, add their fids into the namespace LFSCK trace file,
5556 * then the subsenquent namespace LFSCK second-stage scanning can handle
5557 * them as other objects to be double scanned: either move back to normal
5558 * namespace, or to the global visible orphan directory:
5559 * /ROOT/.lustre/lost+found/MDTxxxx/
5561 * \param[in] env pointer to the thread context
5562 * \param[in] com pointer to the lfsck component
5564 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5565 struct lfsck_component *com)
5567 struct lfsck_thread_info *info = lfsck_env_info(env);
5568 struct lu_dirent *ent =
5569 (struct lu_dirent *)info->lti_key;
5570 struct lu_seq_range *range = &info->lti_range;
5571 struct lfsck_instance *lfsck = com->lc_lfsck;
5572 struct ptlrpc_thread *thread = &lfsck->li_thread;
5573 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5574 struct dt_device *dev = lfsck->li_bottom;
5575 struct lfsck_namespace *ns = com->lc_file_ram;
5576 struct dt_object *parent;
5577 const struct dt_it_ops *iops;
5579 struct seq_server_site *ss =
5580 lu_site2seq(dev->dd_lu_dev.ld_site);
5586 parent = lfsck_object_find_by_dev(env, dev, &LU_BACKEND_LPF_FID);
5587 if (IS_ERR(parent)) {
5588 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5589 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5593 /* It is normal that the /lost+found does not exist for ZFS backend. */
5594 if (!dt_object_exists(parent))
5597 if (unlikely(!dt_try_as_dir(env, parent)))
5598 GOTO(out, rc = -ENOTDIR);
5600 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5601 lfsck_lfsck2name(lfsck));
5603 com->lc_new_scanned = 0;
5604 iops = &parent->do_index_ops->dio_it;
5605 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5607 GOTO(out, rc = PTR_ERR(di));
5609 rc = iops->load(env, di, 0);
5611 rc = iops->next(env, di);
5616 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
5618 struct l_wait_info lwi;
5620 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
5622 l_wait_event(thread->t_ctl_waitq,
5623 !thread_is_running(thread),
5626 if (unlikely(!thread_is_running(thread)))
5630 rc = iops->rec(env, di, (struct dt_rec *)ent,
5631 LUDA_64BITHASH | LUDA_TYPE);
5633 rc = lfsck_unpack_ent(ent, &cookie, &type);
5635 if (unlikely(rc != 0)) {
5636 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5637 "/lost+found: rc = %d\n",
5638 lfsck_lfsck2name(lfsck), rc);
5643 /* skip dot and dotdot entries */
5644 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5647 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5650 if (fid_is_norm(&ent->lde_fid)) {
5651 fld_range_set_mdt(range);
5652 rc = fld_local_lookup(env, ss->ss_server_fld,
5653 fid_seq(&ent->lde_fid), range);
5656 } else if (lfsck_dev_idx(dev) != 0) {
5657 /* If the returned FID is IGIF, then there are three
5660 * 1) The object is upgraded from old Lustre-1.8 with
5661 * IGIF assigned to such object.
5662 * 2) The object is a backend local object and is
5663 * invisible to client.
5664 * 3) The object lost its LMV EA, and since there is
5665 * no FID-in-dirent for the orphan in the backend
5666 * /lost+found directory, then the low layer will
5667 * return IGIF for such object.
5669 * For MDTx (x != 0), it is either case 2) or case 3),
5670 * but from the LFSCK view, they are indistinguishable.
5671 * To be safe, the LFSCK will keep it there and report
5672 * some message, then the adminstrator can handle that
5675 * For MDT0, it is more possible the case 1). The LFSCK
5676 * will handle the orphan as an upgraded object. */
5677 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5678 "in the backend /lost+found on the MDT %04x, "
5679 "to be safe, skip it.\n",
5680 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5681 ent->lde_name, PFID(&ent->lde_fid),
5682 lfsck_dev_idx(dev));
5686 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5689 down_write(&com->lc_sem);
5690 com->lc_new_scanned++;
5691 ns->ln_local_lpf_scanned++;
5693 ns->ln_local_lpf_moved++;
5695 ns->ln_local_lpf_skipped++;
5697 ns->ln_local_lpf_failed++;
5698 up_write(&com->lc_sem);
5700 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5704 lfsck_control_speed_by_self(com);
5705 if (unlikely(!thread_is_running(thread))) {
5710 rc = iops->next(env, di);
5714 iops->fini(env, di);
5719 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5720 lfsck_lfsck2name(lfsck), rc);
5722 lu_object_put(env, &parent->do_lu);
5726 * Rescan the striped directory after the master LMV EA reset.
5728 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5729 * the namespace LFSCK engine scan the striped directory for the first time,
5730 * it will be regarded as a normal directory. As the LFSCK processing, some
5731 * other LFSCK instance on other MDT will find the shard of this striped dir,
5732 * and find that the master MDT-object of the striped directory lost its LMV
5733 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5734 * notify the LFSCK instance on this MDT to rescan the striped directory.
5736 * \param[in] env pointer to the thread context
5737 * \param[in] com pointer to the lfsck component
5738 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5741 * \retval positive number for success
5742 * \retval 0 for LFSCK stopped/paused
5743 * \retval negative error number on failure
5745 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5746 struct lfsck_component *com,
5747 struct lfsck_lmv_unit *llu)
5749 struct lfsck_thread_info *info = lfsck_env_info(env);
5750 struct lfsck_instance *lfsck = com->lc_lfsck;
5751 struct lfsck_assistant_data *lad = com->lc_data;
5752 struct dt_object *dir;
5753 const struct dt_it_ops *iops;
5755 struct lu_dirent *ent =
5756 (struct lu_dirent *)info->lti_key;
5757 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5758 struct ptlrpc_thread *thread = &lfsck->li_thread;
5759 struct lfsck_namespace_req *lnr;
5760 struct lfsck_assistant_req *lar;
5765 LASSERT(list_empty(&lad->lad_req_list));
5767 lfsck->li_lmv = &llu->llu_lmv;
5768 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5769 rc = lfsck_open_dir(env, lfsck, 0);
5773 dir = lfsck->li_obj_dir;
5774 di = lfsck->li_di_dir;
5775 iops = &dir->do_index_ops->dio_it;
5777 rc = iops->rec(env, di, (struct dt_rec *)ent,
5778 lfsck->li_args_dir);
5780 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5784 if (bk->lb_param & LPF_FAILOUT)
5790 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5793 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5795 if (bk->lb_param & LPF_FAILOUT)
5796 GOTO(out, rc = PTR_ERR(lnr));
5801 lar = &lnr->lnr_lar;
5802 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5803 lfsck_namespace_assistant_req_fini(env, lar);
5804 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5807 if (unlikely(!thread_is_running(thread)))
5811 rc = iops->next(env, di);
5815 lfsck_close_dir(env, lfsck, rc);
5819 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5820 if (list_empty(&lad->lad_req_list))
5823 spin_lock(&lad->lad_lock);
5824 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5826 list_del_init(&lar->lar_list);
5827 spin_unlock(&lad->lad_lock);
5829 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5830 lfsck_namespace_assistant_req_fini(env, lar);
5832 RETURN(rc == 0 ? 1 : rc);
5835 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5836 struct lfsck_component *com)
5838 struct lfsck_instance *lfsck = com->lc_lfsck;
5839 struct ptlrpc_thread *thread = &lfsck->li_thread;
5840 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5841 struct lfsck_namespace *ns = com->lc_file_ram;
5842 struct dt_object *obj = com->lc_obj;
5843 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5844 struct dt_object *target;
5852 while (!list_empty(&lfsck->li_list_lmv)) {
5853 struct lfsck_lmv_unit *llu;
5855 spin_lock(&lfsck->li_lock);
5856 llu = list_entry(lfsck->li_list_lmv.next,
5857 struct lfsck_lmv_unit, llu_link);
5858 list_del_init(&llu->llu_link);
5859 spin_unlock(&lfsck->li_lock);
5861 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
5866 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
5867 lfsck_lfsck2name(lfsck));
5869 lfsck_namespace_scan_local_lpf(env, com);
5871 com->lc_new_checked = 0;
5872 com->lc_new_scanned = 0;
5873 com->lc_time_last_checkpoint = cfs_time_current();
5874 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5875 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5877 di = iops->init(env, obj, 0, BYPASS_CAPA);
5879 RETURN(PTR_ERR(di));
5881 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5882 rc = iops->get(env, di, (const struct dt_key *)&fid);
5886 /* Skip the start one, which either has been processed or non-exist. */
5887 rc = iops->next(env, di);
5892 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
5894 struct l_wait_info lwi;
5896 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
5898 l_wait_event(thread->t_ctl_waitq,
5899 !thread_is_running(thread),
5902 if (unlikely(!thread_is_running(thread)))
5906 key = iops->key(env, di);
5907 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5908 if (!fid_is_sane(&fid)) {
5913 target = lfsck_object_find_by_dev(env, lfsck->li_bottom, &fid);
5914 if (IS_ERR(target)) {
5915 rc = PTR_ERR(target);
5919 if (dt_object_exists(target)) {
5920 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5922 rc = lfsck_namespace_double_scan_one(env, com,
5929 lfsck_object_put(env, target);
5932 down_write(&com->lc_sem);
5933 com->lc_new_checked++;
5934 com->lc_new_scanned++;
5935 ns->ln_fid_latest_scanned_phase2 = fid;
5937 ns->ln_objs_repaired_phase2++;
5939 ns->ln_objs_failed_phase2++;
5940 up_write(&com->lc_sem);
5942 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5945 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
5946 cfs_time_current())) &&
5947 com->lc_new_checked != 0) {
5948 down_write(&com->lc_sem);
5949 ns->ln_run_time_phase2 +=
5950 cfs_duration_sec(cfs_time_current() +
5951 HALF_SEC - com->lc_time_last_checkpoint);
5952 ns->ln_time_last_checkpoint = cfs_time_current_sec();
5953 ns->ln_objs_checked_phase2 += com->lc_new_checked;
5954 com->lc_new_checked = 0;
5955 rc = lfsck_namespace_store(env, com);
5956 up_write(&com->lc_sem);
5960 com->lc_time_last_checkpoint = cfs_time_current();
5961 com->lc_time_next_checkpoint =
5962 com->lc_time_last_checkpoint +
5963 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5966 lfsck_control_speed_by_self(com);
5967 if (unlikely(!thread_is_running(thread)))
5970 rc = iops->next(env, di);
5979 iops->fini(env, di);
5981 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
5982 lfsck_lfsck2name(lfsck), rc);
5987 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
5988 struct lfsck_component *com,
5989 struct lfsck_position *pos)
5991 struct lfsck_assistant_data *lad = com->lc_data;
5992 struct lfsck_namespace_req *lnr;
5994 if (list_empty(&lad->lad_req_list))
5997 lnr = list_entry(lad->lad_req_list.next,
5998 struct lfsck_namespace_req,
6000 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6001 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6002 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
6005 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6006 struct lfsck_component *com,
6009 struct lfsck_instance *lfsck = com->lc_lfsck;
6010 struct lfsck_namespace *ns = com->lc_file_ram;
6012 down_write(&com->lc_sem);
6013 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6014 HALF_SEC - lfsck->li_time_last_checkpoint);
6015 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6016 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6017 com->lc_new_checked = 0;
6020 if (ns->ln_flags & LF_INCOMPLETE)
6021 ns->ln_status = LS_PARTIAL;
6023 ns->ln_status = LS_COMPLETED;
6024 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6025 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6026 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6027 ns->ln_success_count++;
6028 } else if (rc == 0) {
6029 if (lfsck->li_status != 0)
6030 ns->ln_status = lfsck->li_status;
6032 ns->ln_status = LS_STOPPED;
6034 ns->ln_status = LS_FAILED;
6037 rc = lfsck_namespace_store(env, com);
6038 up_write(&com->lc_sem);
6044 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6045 struct ptlrpc_request *req,
6052 * Notify remote LFSCK instances about former failures.
6054 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6055 * some LFSCK verification requests (maybe because of network issues or the MDT
6056 * itself trouble). During the respond gap the MDT may missed some name entries
6057 * verification, then the MDT cannot know whether related MDT-objects have been
6058 * referenced by related name entries or not, then in the second-stage scanning,
6059 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6060 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6061 * name entry for repairing the orphan.
6063 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6064 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6065 * have ever missed some name entries verification and should skip the handling
6066 * for orphan MDT-objects.
6068 * \param[in] env pointer to the thread context
6069 * \param[in] com pointer to the lfsck component
6070 * \param[in] lr pointer to the lfsck request
6072 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6073 struct lfsck_component *com,
6074 struct lfsck_request *lr)
6076 struct lfsck_async_interpret_args *laia =
6077 &lfsck_env_info(env)->lti_laia2;
6078 struct lfsck_assistant_data *lad = com->lc_data;
6079 struct lfsck_namespace *ns = com->lc_file_ram;
6080 struct lfsck_instance *lfsck = com->lc_lfsck;
6081 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6082 struct lfsck_tgt_desc *ltd;
6083 struct ptlrpc_request_set *set;
6087 set = ptlrpc_prep_set();
6089 GOTO(out, rc = -ENOMEM);
6091 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6092 memset(laia, 0, sizeof(*laia));
6093 lad->lad_touch_gen++;
6095 spin_lock(<ds->ltd_lock);
6096 while (!list_empty(&lad->lad_mdt_list)) {
6097 ltd = list_entry(lad->lad_mdt_list.next,
6098 struct lfsck_tgt_desc,
6099 ltd_namespace_list);
6100 if (ltd->ltd_namespace_gen == lad->lad_touch_gen)
6103 ltd->ltd_namespace_gen = lad->lad_touch_gen;
6104 list_move_tail(<d->ltd_namespace_list,
6105 &lad->lad_mdt_list);
6106 if (!lad->lad_incomplete ||
6107 !cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
6108 ltd->ltd_namespace_failed = 0;
6112 ltd->ltd_namespace_failed = 1;
6113 spin_unlock(<ds->ltd_lock);
6114 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6115 lfsck_namespace_assistant_sync_failures_interpret,
6116 laia, LFSCK_NOTIFY);
6118 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6119 "to sync failure with MDT %x: rc = %d\n",
6120 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6122 spin_lock(<ds->ltd_lock);
6124 spin_unlock(<ds->ltd_lock);
6126 rc = ptlrpc_set_wait(set);
6127 ptlrpc_set_destroy(set);
6133 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6134 "to sync failure with MDTs, and related MDTs "
6135 "may handle orphan improperly: rc = %d\n",
6136 lfsck_lfsck2name(lfsck), rc);
6141 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6142 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6143 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6144 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6145 .la_double_scan_result = lfsck_namespace_double_scan_result,
6146 .la_req_fini = lfsck_namespace_assistant_req_fini,
6147 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6151 * Verify the specified linkEA entry for the given directory object.
6152 * If the object has no such linkEA entry or it has more other linkEA
6153 * entries, then re-generate the linkEA with the given information.
6155 * \param[in] env pointer to the thread context
6156 * \param[in] dev pointer to the dt_device
6157 * \param[in] obj pointer to the dt_object to be handled
6158 * \param[in] cname the name for the child in the parent directory
6159 * \param[in] pfid the parent directory's FID for the linkEA
6161 * \retval 0 for success
6162 * \retval negative error number on failure
6164 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
6165 struct dt_object *obj, const struct lu_name *cname,
6166 const struct lu_fid *pfid)
6168 struct linkea_data ldata = { 0 };
6169 struct lu_buf linkea_buf;
6172 int fl = LU_XATTR_CREATE;
6176 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6178 rc = lfsck_links_read(env, obj, &ldata);
6179 if (rc == -ENODATA) {
6181 } else if (rc == 0) {
6182 fl = LU_XATTR_REPLACE;
6183 if (ldata.ld_leh->leh_reccount != 1) {
6186 rc = linkea_links_find(&ldata, cname, pfid);
6195 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6199 rc = linkea_add_buf(&ldata, cname, pfid);
6203 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6204 ldata.ld_leh->leh_len);
6205 th = dt_trans_create(env, dev);
6207 RETURN(PTR_ERR(th));
6209 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6210 XATTR_NAME_LINK, fl, th);
6214 rc = dt_trans_start_local(env, dev, th);
6218 dt_write_lock(env, obj, 0);
6219 rc = dt_xattr_set(env, obj, &linkea_buf,
6220 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6221 dt_write_unlock(env, obj);
6226 dt_trans_stop(env, dev, th);
6231 * Get the name and parent directory's FID from the first linkEA entry.
6233 * \param[in] env pointer to the thread context
6234 * \param[in] obj pointer to the object which get linkEA from
6235 * \param[out] name pointer to the buffer to hold the name
6236 * in the first linkEA entry
6237 * \param[out] pfid pointer to the buffer to hold the parent
6238 * directory's FID in the first linkEA entry
6240 * \retval 0 for success
6241 * \retval negative error number on failure
6243 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6244 char *name, struct lu_fid *pfid)
6246 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6247 struct linkea_data ldata = { 0 };
6250 rc = lfsck_links_read(env, obj, &ldata);
6254 linkea_first_entry(&ldata);
6255 if (ldata.ld_lee == NULL)
6258 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6259 /* To guarantee the 'name' is terminated with '0'. */
6260 memcpy(name, cname->ln_name, cname->ln_namelen);
6261 name[cname->ln_namelen] = 0;
6267 * Remove the name entry from the parent directory.
6269 * No need to care about the object referenced by the name entry,
6270 * either the name entry is invalid or redundant, or the referenced
6271 * object has been processed has been or will be handled by others.
6273 * \param[in] env pointer to the thread context
6274 * \param[in] lfsck pointer to the lfsck instance
6275 * \param[in] parent pointer to the lost+found object
6276 * \param[in] name the name for the name entry to be removed
6277 * \param[in] type the type for the name entry to be removed
6279 * \retval 0 for success
6280 * \retval negative error number on failure
6282 int lfsck_remove_name_entry(const struct lu_env *env,
6283 struct lfsck_instance *lfsck,
6284 struct dt_object *parent,
6285 const char *name, __u32 type)
6287 struct dt_device *dev = lfsck->li_next;
6289 struct lustre_handle lh = { 0 };
6293 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6294 MDS_INODELOCK_UPDATE, LCK_EX);
6298 th = dt_trans_create(env, dev);
6300 GOTO(unlock, rc = PTR_ERR(th));
6302 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6306 if (S_ISDIR(type)) {
6307 rc = dt_declare_ref_del(env, parent, th);
6312 rc = dt_trans_start(env, dev, th);
6316 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6321 if (S_ISDIR(type)) {
6322 dt_write_lock(env, parent, 0);
6323 rc = dt_ref_del(env, parent, th);
6324 dt_write_unlock(env, parent);
6330 dt_trans_stop(env, dev, th);
6333 lfsck_ibits_unlock(&lh, LCK_EX);
6335 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
6336 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6337 PFID(lfsck_dto2fid(parent)), name, type, rc);
6343 * Update the object's name entry with the given FID.
6345 * \param[in] env pointer to the thread context
6346 * \param[in] lfsck pointer to the lfsck instance
6347 * \param[in] parent pointer to the parent directory that holds
6349 * \param[in] name the name for the entry to be updated
6350 * \param[in] pfid the new PFID for the name entry
6351 * \param[in] type the type for the name entry to be updated
6353 * \retval 0 for success
6354 * \retval negative error number on failure
6356 int lfsck_update_name_entry(const struct lu_env *env,
6357 struct lfsck_instance *lfsck,
6358 struct dt_object *parent, const char *name,
6359 const struct lu_fid *pfid, __u32 type)
6361 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
6362 struct dt_device *dev = lfsck->li_next;
6363 struct lustre_handle lh = { 0 };
6369 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6370 MDS_INODELOCK_UPDATE, LCK_EX);
6374 th = dt_trans_create(env, dev);
6376 GOTO(unlock, rc = PTR_ERR(th));
6378 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6382 rec->rec_type = type;
6383 rec->rec_fid = pfid;
6384 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
6385 (const struct dt_key *)name, th);
6389 rc = dt_declare_ref_add(env, parent, th);
6393 rc = dt_trans_start(env, dev, th);
6397 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6399 if (rc == -ENOENT) {
6407 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
6408 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6409 if (rc == 0 && S_ISDIR(type) && !exists) {
6410 dt_write_lock(env, parent, 0);
6411 rc = dt_ref_add(env, parent, th);
6412 dt_write_unlock(env, parent);
6418 dt_trans_stop(env, dev, th);
6421 lfsck_ibits_unlock(&lh, LCK_EX);
6423 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6424 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6425 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
6430 int lfsck_namespace_setup(const struct lu_env *env,
6431 struct lfsck_instance *lfsck)
6433 struct lfsck_component *com;
6434 struct lfsck_namespace *ns;
6435 struct dt_object *root = NULL;
6436 struct dt_object *obj;
6440 LASSERT(lfsck->li_master);
6446 INIT_LIST_HEAD(&com->lc_link);
6447 INIT_LIST_HEAD(&com->lc_link_dir);
6448 init_rwsem(&com->lc_sem);
6449 atomic_set(&com->lc_ref, 1);
6450 com->lc_lfsck = lfsck;
6451 com->lc_type = LFSCK_TYPE_NAMESPACE;
6452 com->lc_ops = &lfsck_namespace_ops;
6453 com->lc_data = lfsck_assistant_data_init(
6454 &lfsck_namespace_assistant_ops,
6456 if (com->lc_data == NULL)
6457 GOTO(out, rc = -ENOMEM);
6459 com->lc_file_size = sizeof(struct lfsck_namespace);
6460 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6461 if (com->lc_file_ram == NULL)
6462 GOTO(out, rc = -ENOMEM);
6464 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6465 if (com->lc_file_disk == NULL)
6466 GOTO(out, rc = -ENOMEM);
6468 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6470 GOTO(out, rc = PTR_ERR(root));
6472 if (unlikely(!dt_try_as_dir(env, root)))
6473 GOTO(out, rc = -ENOTDIR);
6475 obj = local_index_find_or_create(env, lfsck->li_los, root,
6476 lfsck_namespace_name,
6477 S_IFREG | S_IRUGO | S_IWUSR,
6478 &dt_lfsck_features);
6480 GOTO(out, rc = PTR_ERR(obj));
6483 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
6487 rc = lfsck_namespace_load(env, com);
6489 rc = lfsck_namespace_reset(env, com, true);
6490 else if (rc == -ENODATA)
6491 rc = lfsck_namespace_init(env, com);
6495 ns = com->lc_file_ram;
6496 switch (ns->ln_status) {
6501 spin_lock(&lfsck->li_lock);
6502 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6503 spin_unlock(&lfsck->li_lock);
6506 CERROR("%s: unknown lfsck_namespace status %d\n",
6507 lfsck_lfsck2name(lfsck), ns->ln_status);
6509 case LS_SCANNING_PHASE1:
6510 case LS_SCANNING_PHASE2:
6511 /* No need to store the status to disk right now.
6512 * If the system crashed before the status stored,
6513 * it will be loaded back when next time. */
6514 ns->ln_status = LS_CRASHED;
6518 spin_lock(&lfsck->li_lock);
6519 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6520 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6521 spin_unlock(&lfsck->li_lock);
6528 if (root != NULL && !IS_ERR(root))
6529 lu_object_put(env, &root->do_lu);
6531 lfsck_component_cleanup(env, com);
6532 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6533 lfsck_lfsck2name(lfsck), rc);