4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, 2014, 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 * Load namespace LFSCK statistics information from the trace file.
324 * For old release (Lustre-2.6 or older), the statistics information was
325 * stored as XATTR_NAME_LFSCK_NAMESPACE_OLD EA. But in Lustre-2.7, we need
326 * more statistics information. To avoid confusing old MDT when downgrade,
327 * Lustre-2.7 stores the namespace LFSCK statistics information as new
328 * XATTR_NAME_LFSCK_NAMESPACE EA.
330 * \param[in] env pointer to the thread context
331 * \param[in] com pointer to the lfsck component
333 * \retval 0 for success
334 * \retval negative error number on failure
336 static int lfsck_namespace_load(const struct lu_env *env,
337 struct lfsck_component *com)
339 int len = com->lc_file_size;
342 rc = dt_xattr_get(env, com->lc_obj,
343 lfsck_buf_get(env, com->lc_file_disk, len),
344 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
346 struct lfsck_namespace *ns = com->lc_file_ram;
348 lfsck_namespace_le_to_cpu(ns,
349 (struct lfsck_namespace *)com->lc_file_disk);
350 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
351 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
352 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
353 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
358 } else if (rc != -ENODATA) {
359 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
360 "expected = %d: rc = %d\n",
361 lfsck_lfsck2name(com->lc_lfsck), len, rc);
365 /* Check whether it is old trace file or not.
366 * If yes, it should be reset via returning -ESTALE. */
367 rc = dt_xattr_get(env, com->lc_obj,
368 lfsck_buf_get(env, com->lc_file_disk, len),
369 XATTR_NAME_LFSCK_NAMESPACE_OLD, BYPASS_CAPA);
377 static int lfsck_namespace_store(const struct lu_env *env,
378 struct lfsck_component *com, bool init)
380 struct dt_object *obj = com->lc_obj;
381 struct lfsck_instance *lfsck = com->lc_lfsck;
382 struct lfsck_namespace *ns = com->lc_file_ram;
383 struct lfsck_assistant_data *lad = com->lc_data;
384 cfs_bitmap_t *bitmap = NULL;
385 struct thandle *handle;
387 int len = com->lc_file_size;
389 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
390 struct lu_buf tbuf = { &len, sizeof(len) };
395 bitmap = lad->lad_bitmap;
396 nbits = bitmap->size;
399 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
402 ns->ln_bitmap_size = nbits;
403 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
405 handle = dt_trans_create(env, lfsck->li_bottom);
407 GOTO(log, rc = PTR_ERR(handle));
409 rc = dt_declare_xattr_set(env, obj,
410 lfsck_buf_get(env, com->lc_file_disk, len),
411 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
415 if (bitmap != NULL) {
416 rc = dt_declare_xattr_set(env, obj,
417 lfsck_buf_get(env, bitmap->data, nbits >> 3),
418 XATTR_NAME_LFSCK_BITMAP, 0, handle);
423 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
424 /* To be compatible with old Lustre-2.x MDT (x <= 6), generate dummy
425 * XATTR_NAME_LFSCK_NAMESPACE_OLD EA, then when downgrade to Lustre-2.x,
426 * the old LFSCK will find "invalid" XATTR_NAME_LFSCK_NAMESPACE_OLD EA,
427 * then reset the namespace LFSCK trace file. */
429 rc = dt_declare_xattr_set(env, obj, &tbuf,
430 XATTR_NAME_LFSCK_NAMESPACE_OLD,
431 LU_XATTR_CREATE, handle);
437 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
441 rc = dt_xattr_set(env, obj,
442 lfsck_buf_get(env, com->lc_file_disk, len),
443 XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
444 if (rc == 0 && bitmap != NULL)
445 rc = dt_xattr_set(env, obj,
446 lfsck_buf_get(env, bitmap->data, nbits >> 3),
447 XATTR_NAME_LFSCK_BITMAP, 0, handle,
450 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
452 rc = dt_xattr_set(env, obj, &tbuf,
453 XATTR_NAME_LFSCK_NAMESPACE_OLD,
454 LU_XATTR_CREATE, handle, BYPASS_CAPA);
460 dt_trans_stop(env, lfsck->li_bottom, handle);
464 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
465 lfsck_lfsck2name(lfsck), rc);
469 static int lfsck_namespace_init(const struct lu_env *env,
470 struct lfsck_component *com)
472 struct lfsck_namespace *ns = com->lc_file_ram;
475 memset(ns, 0, sizeof(*ns));
476 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
477 ns->ln_status = LS_INIT;
478 down_write(&com->lc_sem);
479 rc = lfsck_namespace_store(env, com, true);
480 up_write(&com->lc_sem);
485 * Update the namespace LFSCK trace file for the given @fid
487 * \param[in] env pointer to the thread context
488 * \param[in] com pointer to the lfsck component
489 * \param[in] fid the fid which flags to be updated in the lfsck
491 * \param[in] add true if add new flags, otherwise remove flags
493 * \retval 0 for succeed or nothing to be done
494 * \retval negative error number on failure
496 int lfsck_namespace_trace_update(const struct lu_env *env,
497 struct lfsck_component *com,
498 const struct lu_fid *fid,
499 const __u8 flags, bool add)
501 struct lfsck_instance *lfsck = com->lc_lfsck;
502 struct dt_object *obj = com->lc_obj;
503 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
504 struct dt_device *dev = lfsck->li_bottom;
505 struct thandle *th = NULL;
513 down_write(&com->lc_sem);
514 fid_cpu_to_be(key, fid);
515 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
516 (const struct dt_key *)key, BYPASS_CAPA);
519 GOTO(unlock, rc = 0);
523 } else if (rc == 0) {
525 if ((old & flags) == flags)
526 GOTO(unlock, rc = 0);
530 if ((old & flags) == 0)
531 GOTO(unlock, rc = 0);
539 th = dt_trans_create(env, dev);
541 GOTO(log, rc = PTR_ERR(th));
544 rc = dt_declare_delete(env, obj,
545 (const struct dt_key *)key, th);
551 rc = dt_declare_insert(env, obj,
552 (const struct dt_rec *)&new,
553 (const struct dt_key *)key, th);
558 rc = dt_trans_start_local(env, dev, th);
563 rc = dt_delete(env, obj, (const struct dt_key *)key,
570 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
571 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
579 if (th != NULL && !IS_ERR(th))
580 dt_trans_stop(env, dev, th);
582 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
583 "trace file, flags %x, old %x, new %x: rc = %d\n",
584 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
585 (__u32)flags, (__u32)old, (__u32)new, rc);
588 up_write(&com->lc_sem);
593 int lfsck_namespace_check_exist(const struct lu_env *env,
594 struct dt_object *dir,
595 struct dt_object *obj, const char *name)
597 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
601 if (unlikely(lfsck_is_dead_obj(obj)))
602 RETURN(LFSCK_NAMEENTRY_DEAD);
604 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
605 (const struct dt_key *)name, BYPASS_CAPA);
607 RETURN(LFSCK_NAMEENTRY_REMOVED);
612 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
613 RETURN(LFSCK_NAMEENTRY_RECREATED);
618 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
619 struct dt_object *obj,
620 struct thandle *handle)
624 /* For destroying all invalid linkEA entries. */
625 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
629 /* For insert new linkEA entry. */
630 rc = dt_declare_xattr_set(env, obj,
631 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
632 XATTR_NAME_LINK, 0, handle);
636 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
637 struct linkea_data *ldata)
641 if (ldata->ld_buf->lb_buf == NULL)
644 if (!dt_object_exists(obj))
647 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
649 /* Buf was too small, figure out what we need. */
650 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
655 lu_buf_realloc(ldata->ld_buf, rc);
656 if (ldata->ld_buf->lb_buf == NULL)
659 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
664 rc = linkea_init(ldata);
670 * Remove linkEA for the given object.
672 * The caller should take the ldlm lock before the calling.
674 * \param[in] env pointer to the thread context
675 * \param[in] com pointer to the lfsck component
676 * \param[in] obj pointer to the dt_object to be handled
678 * \retval 0 for repaired cases
679 * \retval negative error number on failure
681 static int lfsck_namespace_links_remove(const struct lu_env *env,
682 struct lfsck_component *com,
683 struct dt_object *obj)
685 struct lfsck_instance *lfsck = com->lc_lfsck;
686 struct dt_device *dev = lfsck->li_bottom;
687 struct thandle *th = NULL;
691 LASSERT(dt_object_remote(obj) == 0);
693 th = dt_trans_create(env, dev);
695 GOTO(log, rc = PTR_ERR(th));
697 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
701 rc = dt_trans_start_local(env, dev, th);
705 dt_write_lock(env, obj, 0);
706 if (unlikely(lfsck_is_dead_obj(obj)))
707 GOTO(unlock, rc = -ENOENT);
709 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
710 GOTO(unlock, rc = 0);
712 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
717 dt_write_unlock(env, obj);
720 dt_trans_stop(env, dev, th);
723 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
724 "for the object "DFID": rc = %d\n",
725 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
728 struct lfsck_namespace *ns = com->lc_file_ram;
730 ns->ln_flags |= LF_INCONSISTENT;
736 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
737 struct linkea_data *ldata, struct thandle *handle)
739 const struct lu_buf *buf = lfsck_buf_get_const(env,
740 ldata->ld_buf->lb_buf,
741 ldata->ld_leh->leh_len);
743 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
747 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
748 struct lu_name *cname,
752 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
753 /* To guarantee the 'name' is terminated with '0'. */
754 memcpy(buf, cname->ln_name, cname->ln_namelen);
755 buf[cname->ln_namelen] = 0;
756 cname->ln_name = buf;
759 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
760 struct lu_name *cname,
764 struct link_ea_entry *oldlee;
768 oldlee = ldata->ld_lee;
769 oldlen = ldata->ld_reclen;
770 linkea_next_entry(ldata);
771 while (ldata->ld_lee != NULL) {
772 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
773 ldata->ld_lee->lee_reclen[1];
774 if (unlikely(ldata->ld_reclen == oldlen &&
775 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
780 linkea_del_buf(ldata, cname);
782 linkea_next_entry(ldata);
785 ldata->ld_lee = oldlee;
786 ldata->ld_reclen = oldlen;
792 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
794 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
795 * with the given type to generate the name, the detailed rules for name
796 * have been described as following.
798 * The function also generates the linkEA corresponding to the name entry
799 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
801 * \param[in] env pointer to the thread context
802 * \param[in] com pointer to the lfsck component
803 * \param[in] orphan pointer to the orphan MDT-object
804 * \param[in] infix additional information for the orphan name, such as
805 * the FID for original
806 * \param[in] type the type for describing why the orphan MDT-object is
807 * created. The rules are as following:
809 * type "D": The MDT-object is a directory, it may knows its parent
810 * but because there is no valid linkEA, the LFSCK cannot
811 * know where to put it back to the namespace.
812 * type "O": The MDT-object has no linkEA, and there is no name
813 * entry that references the MDT-object.
815 * type "S": The orphan MDT-object is a shard of a striped directory
817 * \see lfsck_layout_recreate_parent() for more types.
819 * The orphan name will be like:
820 * ${FID}-${infix}-${type}-${conflict_version}
822 * \param[out] count if some others inserted some linkEA entries by race,
823 * then return the linkEA entries count.
825 * \retval positive number for repaired cases
826 * \retval 0 if needs to repair nothing
827 * \retval negative error number on failure
829 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
830 struct lfsck_component *com,
831 struct dt_object *orphan,
832 const char *infix, const char *type,
835 struct lfsck_thread_info *info = lfsck_env_info(env);
836 struct lu_name *cname = &info->lti_name;
837 struct dt_insert_rec *rec = &info->lti_dt_rec;
838 struct lu_fid *tfid = &info->lti_fid5;
839 struct lu_attr *la = &info->lti_la3;
840 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
841 const struct lu_fid *pfid;
842 struct lfsck_instance *lfsck = com->lc_lfsck;
843 struct dt_device *dev = lfsck->li_bottom;
844 struct dt_object *parent;
845 struct thandle *th = NULL;
846 struct lustre_handle plh = { 0 };
847 struct lustre_handle clh = { 0 };
848 struct linkea_data ldata = { 0 };
849 struct lu_buf linkea_buf;
856 cname->ln_name = NULL;
857 /* Create .lustre/lost+found/MDTxxxx when needed. */
858 if (unlikely(lfsck->li_lpf_obj == NULL)) {
859 rc = lfsck_create_lpf(env, lfsck);
864 parent = lfsck->li_lpf_obj;
865 pfid = lfsck_dto2fid(parent);
867 /* Hold update lock on the parent to prevent others to access. */
868 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
869 MDS_INODELOCK_UPDATE, LCK_EX);
874 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
875 PFID(cfid), infix, type, idx++);
876 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
877 (const struct dt_key *)info->lti_key,
879 if (rc != 0 && rc != -ENOENT)
882 if (unlikely(rc == 0 && lu_fid_eq(cfid, tfid)))
884 } while (rc == 0 && !exist);
886 cname->ln_name = info->lti_key;
887 cname->ln_namelen = namelen;
888 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
892 rc = linkea_add_buf(&ldata, cname, pfid);
896 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
897 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
902 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
903 ldata.ld_leh->leh_len);
904 th = dt_trans_create(env, dev);
906 GOTO(log, rc = PTR_ERR(th));
908 if (S_ISDIR(lfsck_object_type(orphan))) {
909 rc = dt_declare_delete(env, orphan,
910 (const struct dt_key *)dotdot, th);
914 rec->rec_type = S_IFDIR;
916 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
917 (const struct dt_key *)dotdot, th);
922 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
923 XATTR_NAME_LINK, 0, th);
928 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
930 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
931 (const struct dt_key *)cname->ln_name,
936 if (S_ISDIR(rec->rec_type)) {
937 rc = dt_declare_ref_add(env, parent, th);
943 memset(la, 0, sizeof(*la));
944 la->la_ctime = cfs_time_current_sec();
945 la->la_valid = LA_CTIME;
946 rc = dt_declare_attr_set(env, orphan, la, th);
950 rc = dt_trans_start_local(env, dev, th);
954 dt_write_lock(env, orphan, 0);
955 rc = lfsck_links_read(env, orphan, &ldata);
956 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
957 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
958 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
959 GOTO(unlock, rc = 1);
961 if (S_ISDIR(lfsck_object_type(orphan))) {
962 rc = dt_delete(env, orphan,
963 (const struct dt_key *)dotdot, th,
968 rec->rec_type = S_IFDIR;
970 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
971 (const struct dt_key *)dotdot, th,
977 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
980 if (rc == 0 && count != NULL)
981 *count = ldata.ld_leh->leh_reccount;
985 dt_write_unlock(env, orphan);
987 if (rc == 0 && !exist) {
988 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
990 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
991 (const struct dt_key *)cname->ln_name,
993 if (rc == 0 && S_ISDIR(rec->rec_type)) {
994 dt_write_lock(env, parent, 0);
995 rc = dt_ref_add(env, parent, th);
996 dt_write_unlock(env, parent);
1001 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
1003 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1006 dt_write_unlock(env, orphan);
1009 dt_trans_stop(env, dev, th);
1012 lfsck_ibits_unlock(&clh, LCK_EX);
1013 lfsck_ibits_unlock(&plh, LCK_EX);
1014 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1015 "object "DFID", name = %s: rc = %d\n",
1016 lfsck_lfsck2name(lfsck), PFID(cfid),
1017 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1020 struct lfsck_namespace *ns = com->lc_file_ram;
1022 ns->ln_flags |= LF_INCONSISTENT;
1029 * Add the specified name entry back to namespace.
1031 * If there is a linkEA entry that back references a name entry under
1032 * some parent directory, but such parent directory does not have the
1033 * claimed name entry. On the other hand, the linkEA entries count is
1034 * not larger than the MDT-object's hard link count. Under such case,
1035 * it is quite possible that the name entry is lost. Then the LFSCK
1036 * should add the name entry back to the namespace.
1038 * \param[in] env pointer to the thread context
1039 * \param[in] com pointer to the lfsck component
1040 * \param[in] parent pointer to the directory under which the name entry
1041 * will be inserted into
1042 * \param[in] child pointer to the object referenced by the name entry
1043 * that to be inserted into the parent
1044 * \param[in] name the name for the child in the parent directory
1046 * \retval positive number for repaired cases
1047 * \retval 0 if nothing to be repaired
1048 * \retval negative error number on failure
1050 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1051 struct lfsck_component *com,
1052 struct dt_object *parent,
1053 struct dt_object *child,
1056 struct lfsck_thread_info *info = lfsck_env_info(env);
1057 struct lu_attr *la = &info->lti_la;
1058 struct dt_insert_rec *rec = &info->lti_dt_rec;
1059 struct lfsck_instance *lfsck = com->lc_lfsck;
1060 struct dt_device *dev = lfsck->li_next;
1061 struct thandle *th = NULL;
1062 struct lustre_handle lh = { 0 };
1066 if (unlikely(!dt_try_as_dir(env, parent)))
1067 GOTO(log, rc = -ENOTDIR);
1069 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1072 /* Hold update lock on the parent to prevent others to access. */
1073 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1074 MDS_INODELOCK_UPDATE, LCK_EX);
1078 th = dt_trans_create(env, dev);
1080 GOTO(unlock, rc = PTR_ERR(th));
1082 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1083 rec->rec_fid = lfsck_dto2fid(child);
1084 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1085 (const struct dt_key *)name, th);
1089 if (S_ISDIR(rec->rec_type)) {
1090 rc = dt_declare_ref_add(env, parent, th);
1095 memset(la, 0, sizeof(*la));
1096 la->la_ctime = cfs_time_current_sec();
1097 la->la_valid = LA_CTIME;
1098 rc = dt_declare_attr_set(env, parent, la, th);
1102 rc = dt_declare_attr_set(env, child, la, th);
1106 rc = dt_trans_start_local(env, dev, th);
1110 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1111 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1115 if (S_ISDIR(rec->rec_type)) {
1116 dt_write_lock(env, parent, 0);
1117 rc = dt_ref_add(env, parent, th);
1118 dt_write_unlock(env, parent);
1123 la->la_ctime = cfs_time_current_sec();
1124 rc = dt_attr_set(env, parent, la, th, BYPASS_CAPA);
1128 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
1130 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1133 dt_trans_stop(env, dev, th);
1136 lfsck_ibits_unlock(&lh, LCK_EX);
1139 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1140 "the name %s and type %o to the parent "DFID": rc = %d\n",
1141 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1142 lfsck_object_type(child) & S_IFMT,
1143 PFID(lfsck_dto2fid(parent)), rc);
1146 struct lfsck_namespace *ns = com->lc_file_ram;
1148 ns->ln_flags |= LF_INCONSISTENT;
1150 ns->ln_lost_dirent_repaired++;
1157 * Create the specified orphan MDT-object on remote MDT.
1159 * The LFSCK instance on this MDT will send LFSCK RPC to remote MDT to
1160 * ask the remote LFSCK instance to create the specified orphan object
1161 * under .lustre/lost+found/MDTxxxx/ directory with the name:
1162 * ${FID}-P-${conflict_version}.
1164 * \param[in] env pointer to the thread context
1165 * \param[in] com pointer to the lfsck component
1166 * \param[in] orphan pointer to the orphan MDT-object
1167 * \param[in] type the orphan's type to be created
1169 * type "P": The orphan object to be created was a parent directory
1170 * of some MDT-object which linkEA shows that the @orphan
1171 * object is missing.
1173 * \see lfsck_layout_recreate_parent() for more types.
1175 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1177 * \retval positive number for repaired cases
1178 * \retval 0 if needs to repair nothing
1179 * \retval negative error number on failure
1181 static int lfsck_namespace_create_orphan_remote(const struct lu_env *env,
1182 struct lfsck_component *com,
1183 struct dt_object *orphan,
1185 struct lmv_mds_md_v1 *lmv)
1187 struct lfsck_thread_info *info = lfsck_env_info(env);
1188 struct lfsck_request *lr = &info->lti_lr;
1189 struct lu_seq_range *range = &info->lti_range;
1190 const struct lu_fid *fid = lfsck_dto2fid(orphan);
1191 struct lfsck_namespace *ns = com->lc_file_ram;
1192 struct lfsck_instance *lfsck = com->lc_lfsck;
1193 struct seq_server_site *ss =
1194 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1195 struct lfsck_tgt_desc *ltd = NULL;
1196 struct ptlrpc_request *req = NULL;
1200 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1203 fld_range_set_mdt(range);
1204 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1208 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, range->lsr_index);
1210 ns->ln_flags |= LF_INCOMPLETE;
1212 GOTO(out, rc = -ENODEV);
1215 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1218 GOTO(out, rc = -ENOMEM);
1220 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1222 ptlrpc_request_free(req);
1227 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1228 memset(lr, 0, sizeof(*lr));
1229 lr->lr_event = LE_CREATE_ORPHAN;
1230 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1231 lr->lr_active = LFSCK_TYPE_NAMESPACE;
1235 lr->lr_hash_type = lmv->lmv_hash_type;
1236 lr->lr_stripe_count = lmv->lmv_stripe_count;
1237 lr->lr_layout_version = lmv->lmv_layout_version;
1238 memcpy(lr->lr_pool_name, lmv->lmv_pool_name,
1239 sizeof(lr->lr_pool_name));
1242 ptlrpc_request_set_replen(req);
1243 rc = ptlrpc_queue_wait(req);
1244 ptlrpc_req_finished(req);
1247 orphan->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
1249 } else if (rc == -EEXIST) {
1250 orphan->do_lu.lo_header->loh_attr |= LOHA_EXISTS;
1257 CDEBUG(D_LFSCK, "%s: namespace LFSCK create object "
1258 DFID" on the MDT %x remotely: rc = %d\n",
1259 lfsck_lfsck2name(lfsck), PFID(fid),
1260 ltd != NULL ? ltd->ltd_index : -1, rc);
1269 * Create the specified orphan MDT-object locally.
1271 * For the case that the parent MDT-object stored in some MDT-object's
1272 * linkEA entry is lost, the LFSCK will re-create the parent object as
1273 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1274 * with the name ${FID}-P-${conflict_version}.
1276 * \param[in] env pointer to the thread context
1277 * \param[in] com pointer to the lfsck component
1278 * \param[in] orphan pointer to the orphan MDT-object to be created
1279 * \param[in] type the orphan's type to be created
1281 * type "P": The orphan object to be created was a parent directory
1282 * of some MDT-object which linkEA shows that the @orphan
1283 * object is missing.
1285 * \see lfsck_layout_recreate_parent() for more types.
1287 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1289 * \retval positive number for repaired cases
1290 * \retval negative error number on failure
1292 static int lfsck_namespace_create_orphan_local(const struct lu_env *env,
1293 struct lfsck_component *com,
1294 struct dt_object *orphan,
1296 struct lmv_mds_md_v1 *lmv)
1298 struct lfsck_thread_info *info = lfsck_env_info(env);
1299 struct lu_attr *la = &info->lti_la;
1300 struct dt_allocation_hint *hint = &info->lti_hint;
1301 struct dt_object_format *dof = &info->lti_dof;
1302 struct lu_name *cname = &info->lti_name2;
1303 struct dt_insert_rec *rec = &info->lti_dt_rec;
1304 struct lu_fid *tfid = &info->lti_fid;
1305 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1306 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1307 const struct lu_fid *pfid;
1308 struct lfsck_instance *lfsck = com->lc_lfsck;
1309 struct dt_device *dev = lfsck->li_bottom;
1310 struct dt_object *parent = NULL;
1311 struct dt_object *child = NULL;
1312 struct thandle *th = NULL;
1313 struct lustre_handle lh = { 0 };
1314 struct linkea_data ldata = { 0 };
1315 struct lu_buf linkea_buf;
1316 struct lu_buf lmv_buf;
1323 LASSERT(!dt_object_exists(orphan));
1324 LASSERT(!dt_object_remote(orphan));
1326 /* @orphan maybe not attached to lfsck->li_bottom */
1327 child = lfsck_object_find_by_dev(env, dev, cfid);
1329 GOTO(log, rc = PTR_ERR(child));
1331 cname->ln_name = NULL;
1332 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1335 /* Create .lustre/lost+found/MDTxxxx when needed. */
1336 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1337 rc = lfsck_create_lpf(env, lfsck);
1342 parent = lfsck->li_lpf_obj;
1343 pfid = lfsck_dto2fid(parent);
1345 /* Hold update lock on the parent to prevent others to access. */
1346 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1347 MDS_INODELOCK_UPDATE, LCK_EX);
1352 namelen = snprintf(name, 31, DFID"-P-%d",
1354 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1355 (const struct dt_key *)name, BYPASS_CAPA);
1356 if (rc != 0 && rc != -ENOENT)
1360 cname->ln_name = name;
1361 cname->ln_namelen = namelen;
1363 memset(la, 0, sizeof(*la));
1364 la->la_mode = type | (S_ISDIR(type) ? 0700 : 0600);
1365 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1366 LA_ATIME | LA_MTIME | LA_CTIME;
1368 child->do_ops->do_ah_init(env, hint, parent, child,
1369 la->la_mode & S_IFMT);
1371 memset(dof, 0, sizeof(*dof));
1372 dof->dof_type = dt_mode_to_dft(type);
1374 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1378 rc = linkea_add_buf(&ldata, cname, pfid);
1382 th = dt_trans_create(env, dev);
1384 GOTO(unlock1, rc = PTR_ERR(th));
1386 rc = dt_declare_create(env, child, la, hint, dof, th);
1387 if (rc == 0 && S_ISDIR(type))
1388 rc = dt_declare_ref_add(env, child, th);
1394 lmv->lmv_magic = LMV_MAGIC;
1395 lmv->lmv_master_mdt_index = lfsck_dev_idx(dev);
1396 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1397 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1398 rc = dt_declare_xattr_set(env, child, &lmv_buf,
1399 XATTR_NAME_LMV, 0, th);
1404 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1405 ldata.ld_leh->leh_len);
1406 rc = dt_declare_xattr_set(env, child, &linkea_buf,
1407 XATTR_NAME_LINK, 0, th);
1411 rec->rec_type = type;
1412 rec->rec_fid = cfid;
1413 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1414 (const struct dt_key *)name, th);
1415 if (rc == 0 && S_ISDIR(type))
1416 rc = dt_declare_ref_add(env, parent, th);
1421 rc = dt_trans_start_local(env, dev, th);
1425 dt_write_lock(env, child, 0);
1426 rc = dt_create(env, child, la, hint, dof, th);
1430 if (S_ISDIR(type)) {
1431 if (unlikely(!dt_try_as_dir(env, child)))
1432 GOTO(unlock2, rc = -ENOTDIR);
1434 rec->rec_type = S_IFDIR;
1435 rec->rec_fid = cfid;
1436 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1437 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1441 rec->rec_fid = pfid;
1442 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1443 (const struct dt_key *)dotdot, th,
1448 rc = dt_ref_add(env, child, th);
1454 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV, 0,
1460 rc = dt_xattr_set(env, child, &linkea_buf,
1461 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1462 dt_write_unlock(env, child);
1466 rec->rec_type = type;
1467 rec->rec_fid = cfid;
1468 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1469 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1470 if (rc == 0 && S_ISDIR(type)) {
1471 dt_write_lock(env, parent, 0);
1472 rc = dt_ref_add(env, parent, th);
1473 dt_write_unlock(env, parent);
1476 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1479 dt_write_unlock(env, child);
1482 dt_trans_stop(env, dev, th);
1485 lfsck_ibits_unlock(&lh, LCK_EX);
1488 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan locally for "
1489 "the object "DFID", name = %s, type %o: rc = %d\n",
1490 lfsck_lfsck2name(lfsck), PFID(cfid),
1491 cname->ln_name != NULL ? cname->ln_name : "<NULL>", type, rc);
1493 if (child != NULL && !IS_ERR(child))
1494 lfsck_object_put(env, child);
1500 * Create the specified orphan MDT-object.
1502 * For the case that the parent MDT-object stored in some MDT-object's
1503 * linkEA entry is lost, the LFSCK will re-create the parent object as
1504 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1505 * with the name: ${FID}-P-${conflict_version}.
1507 * \param[in] env pointer to the thread context
1508 * \param[in] com pointer to the lfsck component
1509 * \param[in] orphan pointer to the orphan MDT-object
1511 * type "P": The orphan object to be created was a parent directory
1512 * of some MDT-object which linkEA shows that the @orphan
1513 * object is missing.
1515 * \see lfsck_layout_recreate_parent() for more types.
1517 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1519 * \retval positive number for repaired cases
1520 * \retval 0 if needs to repair nothing
1521 * \retval negative error number on failure
1523 static int lfsck_namespace_create_orphan(const struct lu_env *env,
1524 struct lfsck_component *com,
1525 struct dt_object *orphan,
1526 struct lmv_mds_md_v1 *lmv)
1528 struct lfsck_namespace *ns = com->lc_file_ram;
1531 if (dt_object_remote(orphan))
1532 rc = lfsck_namespace_create_orphan_remote(env, com, orphan,
1535 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
1539 ns->ln_flags |= LF_INCONSISTENT;
1545 * Remove the specified entry from the linkEA.
1547 * Locate the linkEA entry with the given @cname and @pfid, then
1548 * remove this entry or the other entries those are repeated with
1551 * \param[in] env pointer to the thread context
1552 * \param[in] com pointer to the lfsck component
1553 * \param[in] obj pointer to the dt_object to be handled
1554 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1555 * \param[in] cname the name for the child in the parent directory
1556 * \param[in] pfid the parent directory's FID for the linkEA
1557 * \param[in] next if true, then remove the first found linkEA
1558 * entry, and move the ldata->ld_lee to next entry
1560 * \retval positive number for repaired cases
1561 * \retval 0 if nothing to be repaired
1562 * \retval negative error number on failure
1564 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1565 struct lfsck_component *com,
1566 struct dt_object *obj,
1567 struct linkea_data *ldata,
1568 struct lu_name *cname,
1569 struct lu_fid *pfid,
1572 struct lfsck_instance *lfsck = com->lc_lfsck;
1573 struct dt_device *dev = lfsck->li_bottom;
1574 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1575 struct thandle *th = NULL;
1576 struct lustre_handle lh = { 0 };
1577 struct linkea_data ldata_new = { 0 };
1578 struct lu_buf linkea_buf;
1582 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1583 MDS_INODELOCK_UPDATE |
1584 MDS_INODELOCK_XATTR, LCK_EX);
1589 linkea_del_buf(ldata, cname);
1591 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1593 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1594 ldata->ld_leh->leh_len);
1597 th = dt_trans_create(env, dev);
1599 GOTO(unlock1, rc = PTR_ERR(th));
1601 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1602 XATTR_NAME_LINK, 0, th);
1606 rc = dt_trans_start_local(env, dev, th);
1610 dt_write_lock(env, obj, 0);
1611 if (unlikely(lfsck_is_dead_obj(obj)))
1612 GOTO(unlock2, rc = -ENOENT);
1614 rc = lfsck_links_read2(env, obj, &ldata_new);
1618 /* The specified linkEA entry has been removed by race. */
1619 rc = linkea_links_find(&ldata_new, cname, pfid);
1621 GOTO(unlock2, rc = 0);
1623 if (bk->lb_param & LPF_DRYRUN)
1624 GOTO(unlock2, rc = 1);
1627 linkea_del_buf(&ldata_new, cname);
1629 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1632 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1633 dt_write_unlock(env, obj);
1634 dt_trans_stop(env, dev, th);
1635 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1636 ldata_new.ld_leh->leh_len);
1640 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1641 ldata_new.ld_leh->leh_len);
1642 rc = dt_xattr_set(env, obj, &linkea_buf,
1643 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1645 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1648 dt_write_unlock(env, obj);
1651 dt_trans_stop(env, dev, th);
1654 lfsck_ibits_unlock(&lh, LCK_EX);
1657 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1658 "for the object: "DFID", parent "DFID", name %.*s\n",
1659 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1660 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1664 struct lfsck_namespace *ns = com->lc_file_ram;
1666 ns->ln_flags |= LF_INCONSISTENT;
1673 * Conditionally remove the specified entry from the linkEA.
1675 * Take the parent lock firstly, then check whether the specified
1676 * name entry exists or not: if yes, do nothing; otherwise, call
1677 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1679 * \param[in] env pointer to the thread context
1680 * \param[in] com pointer to the lfsck component
1681 * \param[in] parent pointer to the parent directory
1682 * \param[in] child pointer to the child object that holds the linkEA
1683 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1684 * \param[in] cname the name for the child in the parent directory
1685 * \param[in] pfid the parent directory's FID for the linkEA
1687 * \retval positive number for repaired cases
1688 * \retval 0 if nothing to be repaired
1689 * \retval negative error number on failure
1691 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1692 struct lfsck_component *com,
1693 struct dt_object *parent,
1694 struct dt_object *child,
1695 struct linkea_data *ldata,
1696 struct lu_name *cname,
1697 struct lu_fid *pfid)
1699 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1700 struct lustre_handle lh = { 0 };
1704 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1705 MDS_INODELOCK_UPDATE, LCK_EX);
1709 dt_read_lock(env, parent, 0);
1710 if (unlikely(lfsck_is_dead_obj(parent))) {
1711 dt_read_unlock(env, parent);
1712 lfsck_ibits_unlock(&lh, LCK_EX);
1713 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1719 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1720 (const struct dt_key *)cname->ln_name,
1722 dt_read_unlock(env, parent);
1724 /* It is safe to release the ldlm lock, because when the logic come
1725 * here, we have got all the needed information above whether the
1726 * linkEA entry is valid or not. It is not important that others
1727 * may add new linkEA entry after the ldlm lock released. If other
1728 * has removed the specified linkEA entry by race, then it is OK,
1729 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1731 lfsck_ibits_unlock(&lh, LCK_EX);
1732 if (rc == -ENOENT) {
1733 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1742 /* The LFSCK just found some internal status of cross-MDTs
1743 * create operation. That is normal. */
1744 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1745 linkea_next_entry(ldata);
1750 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1757 * Conditionally replace name entry in the parent.
1759 * As required, the LFSCK may re-create the lost MDT-object for dangling
1760 * name entry, but such repairing may be wrong because of bad FID in the
1761 * name entry. As the LFSCK processing, the real MDT-object may be found,
1762 * then the LFSCK should check whether the former re-created MDT-object
1763 * has been modified or not, if not, then destroy it and update the name
1764 * entry in the parent to reference the real MDT-object.
1766 * \param[in] env pointer to the thread context
1767 * \param[in] com pointer to the lfsck component
1768 * \param[in] parent pointer to the parent directory
1769 * \param[in] child pointer to the MDT-object that may be the real
1770 * MDT-object corresponding to the name entry in parent
1771 * \param[in] cfid the current FID in the name entry
1772 * \param[in] cname contains the name of the child in the parent directory
1774 * \retval positive number for repaired cases
1775 * \retval 0 if nothing to be repaired
1776 * \retval negative error number on failure
1778 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1779 struct lfsck_component *com,
1780 struct dt_object *parent,
1781 struct dt_object *child,
1782 const struct lu_fid *cfid,
1783 const struct lu_name *cname)
1785 struct lfsck_thread_info *info = lfsck_env_info(env);
1786 struct lu_fid *tfid = &info->lti_fid5;
1787 struct lu_attr *la = &info->lti_la;
1788 struct dt_insert_rec *rec = &info->lti_dt_rec;
1789 struct lfsck_instance *lfsck = com->lc_lfsck;
1790 struct dt_device *dev = lfsck->li_next;
1791 const char *name = cname->ln_name;
1792 struct dt_object *obj = NULL;
1793 struct lustre_handle plh = { 0 };
1794 struct lustre_handle clh = { 0 };
1795 struct linkea_data ldata = { 0 };
1796 struct thandle *th = NULL;
1801 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1802 MDS_INODELOCK_UPDATE, LCK_EX);
1806 if (!fid_is_sane(cfid)) {
1811 obj = lfsck_object_find(env, lfsck, cfid);
1814 if (rc == -ENOENT) {
1822 if (!dt_object_exists(obj)) {
1827 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1828 (const struct dt_key *)name, BYPASS_CAPA);
1829 if (rc == -ENOENT) {
1837 /* Someone changed the name entry, cannot replace it. */
1838 if (!lu_fid_eq(cfid, tfid))
1841 /* lock the object to be destroyed. */
1842 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1843 MDS_INODELOCK_UPDATE |
1844 MDS_INODELOCK_XATTR, LCK_EX);
1848 if (unlikely(lfsck_is_dead_obj(obj))) {
1853 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1857 /* The object has been modified by other(s), or it is not created by
1858 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1859 if (la->la_ctime != 0)
1862 if (S_ISREG(la->la_mode)) {
1863 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1865 /* If someone has created related OST-object(s),
1867 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1868 GOTO(log, rc = (rc > 0 ? 0 : rc));
1872 dt_read_lock(env, child, 0);
1873 rc = lfsck_links_read2(env, child, &ldata);
1874 dt_read_unlock(env, child);
1876 /* Someone changed the child, no need to replace. */
1883 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1884 /* Someone moved the child, no need to replace. */
1888 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1891 th = dt_trans_create(env, dev);
1893 GOTO(log, rc = PTR_ERR(th));
1896 rc = dt_declare_destroy(env, obj, th);
1901 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1905 rec->rec_type = S_IFDIR;
1906 rec->rec_fid = lfsck_dto2fid(child);
1907 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1908 (const struct dt_key *)name, th);
1912 rc = dt_trans_start(env, dev, th);
1917 rc = dt_destroy(env, obj, th);
1922 /* The old name entry maybe not exist. */
1923 dt_delete(env, parent, (const struct dt_key *)name, th,
1926 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1927 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1929 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1932 dt_trans_stop(env, dev, th);
1935 lfsck_ibits_unlock(&clh, LCK_EX);
1936 lfsck_ibits_unlock(&plh, LCK_EX);
1937 if (obj != NULL && !IS_ERR(obj))
1938 lfsck_object_put(env, obj);
1940 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1941 "object "DFID" because of conflict with the object "DFID
1942 " under the parent "DFID" with name %s: rc = %d\n",
1943 lfsck_lfsck2name(lfsck), PFID(cfid),
1944 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1951 * Overwrite the linkEA for the object with the given ldata.
1953 * The caller should take the ldlm lock before the calling.
1955 * \param[in] env pointer to the thread context
1956 * \param[in] com pointer to the lfsck component
1957 * \param[in] obj pointer to the dt_object to be handled
1958 * \param[in] ldata pointer to the new linkEA data
1960 * \retval positive number for repaired cases
1961 * \retval 0 if nothing to be repaired
1962 * \retval negative error number on failure
1964 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1965 struct lfsck_component *com,
1966 struct dt_object *obj,
1967 struct linkea_data *ldata)
1969 struct lfsck_instance *lfsck = com->lc_lfsck;
1970 struct dt_device *dev = lfsck->li_bottom;
1971 struct thandle *th = NULL;
1972 struct lu_buf linkea_buf;
1976 th = dt_trans_create(env, dev);
1978 GOTO(log, rc = PTR_ERR(th));
1980 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1981 ldata->ld_leh->leh_len);
1982 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1983 XATTR_NAME_LINK, 0, th);
1987 rc = dt_trans_start_local(env, dev, th);
1991 dt_write_lock(env, obj, 0);
1992 if (unlikely(lfsck_is_dead_obj(obj)))
1993 GOTO(unlock, rc = 0);
1995 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1996 GOTO(unlock, rc = 1);
1998 rc = dt_xattr_set(env, obj, &linkea_buf,
1999 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2001 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2004 dt_write_unlock(env, obj);
2007 dt_trans_stop(env, dev, th);
2010 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
2011 "object "DFID": rc = %d\n",
2012 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
2015 struct lfsck_namespace *ns = com->lc_file_ram;
2017 ns->ln_flags |= LF_INCONSISTENT;
2024 * Repair invalid name entry.
2026 * If the name entry contains invalid information, such as bad file type
2027 * or (and) corrupted object FID, then either remove the name entry or
2028 * udpate the name entry with the given (right) information.
2030 * \param[in] env pointer to the thread context
2031 * \param[in] com pointer to the lfsck component
2032 * \param[in] parent pointer to the parent directory
2033 * \param[in] child pointer to the object referenced by the name entry
2034 * \param[in] name the old name of the child under the parent directory
2035 * \param[in] name2 the new name of the child under the parent directory
2036 * \param[in] type the type claimed by the name entry
2037 * \param[in] update update the name entry if true; otherwise, remove it
2038 * \param[in] dec decrease the parent nlink count if true
2040 * \retval positive number for repaired successfully
2041 * \retval 0 if nothing to be repaired
2042 * \retval negative error number on failure
2044 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2045 struct lfsck_component *com,
2046 struct dt_object *parent,
2047 struct dt_object *child,
2048 const char *name, const char *name2,
2049 __u16 type, bool update, bool dec)
2051 struct lfsck_thread_info *info = lfsck_env_info(env);
2052 struct dt_insert_rec *rec = &info->lti_dt_rec;
2053 const struct lu_fid *cfid = lfsck_dto2fid(child);
2054 struct lu_fid *tfid = &info->lti_fid5;
2055 struct lfsck_instance *lfsck = com->lc_lfsck;
2056 struct dt_device *dev = lfsck->li_next;
2057 struct thandle *th = NULL;
2058 struct lustre_handle lh = { 0 };
2062 if (unlikely(!dt_try_as_dir(env, parent)))
2063 GOTO(log, rc = -ENOTDIR);
2065 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2066 MDS_INODELOCK_UPDATE, LCK_EX);
2070 th = dt_trans_create(env, dev);
2072 GOTO(unlock1, rc = PTR_ERR(th));
2074 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2079 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2080 rec->rec_fid = cfid;
2081 rc = dt_declare_insert(env, parent,
2082 (const struct dt_rec *)rec,
2083 (const struct dt_key *)name2, th);
2089 rc = dt_declare_ref_del(env, parent, th);
2094 rc = dt_trans_start(env, dev, th);
2098 dt_write_lock(env, parent, 0);
2099 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2100 (const struct dt_key *)name, BYPASS_CAPA);
2101 /* Someone has removed the bad name entry by race. */
2103 GOTO(unlock2, rc = 0);
2108 /* Someone has removed the bad name entry and reused it for other
2109 * object by race. */
2110 if (!lu_fid_eq(tfid, cfid))
2111 GOTO(unlock2, rc = 0);
2113 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2114 GOTO(unlock2, rc = 1);
2116 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2122 rc = dt_insert(env, parent,
2123 (const struct dt_rec *)rec,
2124 (const struct dt_key *)name2, th,
2131 rc = dt_ref_del(env, parent, th);
2136 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2139 dt_write_unlock(env, parent);
2142 dt_trans_stop(env, dev, th);
2144 /* We are not sure whether the child will become orphan or not.
2145 * Record it in the LFSCK trace file for further checking in
2146 * the second-stage scanning. */
2147 if (!update && !dec && rc == 0)
2148 lfsck_namespace_trace_update(env, com, cfid,
2149 LNTF_CHECK_LINKEA, true);
2152 lfsck_ibits_unlock(&lh, LCK_EX);
2155 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2156 "entry for: parent "DFID", child "DFID", name %s, type "
2157 "in name entry %o, type claimed by child %o. repair it "
2158 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2159 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2160 name, type, update ? lfsck_object_type(child) : 0,
2161 update ? "updating" : "removing", name2, rc);
2164 struct lfsck_namespace *ns = com->lc_file_ram;
2166 ns->ln_flags |= LF_INCONSISTENT;
2173 * Update the ".." name entry for the given object.
2175 * The object's ".." is corrupted, this function will update the ".." name
2176 * entry with the given pfid, and the linkEA with the given ldata.
2178 * The caller should take the ldlm lock before the calling.
2180 * \param[in] env pointer to the thread context
2181 * \param[in] com pointer to the lfsck component
2182 * \param[in] obj pointer to the dt_object to be handled
2183 * \param[in] pfid the new fid for the object's ".." name entry
2184 * \param[in] cname the name for the @obj in the parent directory
2186 * \retval positive number for repaired cases
2187 * \retval 0 if nothing to be repaired
2188 * \retval negative error number on failure
2190 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2191 struct lfsck_component *com,
2192 struct dt_object *obj,
2193 const struct lu_fid *pfid,
2194 struct lu_name *cname)
2196 struct lfsck_thread_info *info = lfsck_env_info(env);
2197 struct dt_insert_rec *rec = &info->lti_dt_rec;
2198 struct lfsck_instance *lfsck = com->lc_lfsck;
2199 struct dt_device *dev = lfsck->li_bottom;
2200 struct thandle *th = NULL;
2201 struct linkea_data ldata = { 0 };
2202 struct lu_buf linkea_buf;
2206 LASSERT(!dt_object_remote(obj));
2207 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2209 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2213 rc = linkea_add_buf(&ldata, cname, pfid);
2217 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2218 ldata.ld_leh->leh_len);
2220 th = dt_trans_create(env, dev);
2222 GOTO(log, rc = PTR_ERR(th));
2224 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2228 rec->rec_type = S_IFDIR;
2229 rec->rec_fid = pfid;
2230 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2231 (const struct dt_key *)dotdot, th);
2235 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2236 XATTR_NAME_LINK, 0, th);
2240 rc = dt_trans_start_local(env, dev, th);
2244 dt_write_lock(env, obj, 0);
2245 if (unlikely(lfsck_is_dead_obj(obj)))
2246 GOTO(unlock, rc = 0);
2248 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2249 GOTO(unlock, rc = 1);
2251 /* The old ".." name entry maybe not exist. */
2252 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2255 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2256 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2260 rc = dt_xattr_set(env, obj, &linkea_buf,
2261 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2263 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2266 dt_write_unlock(env, obj);
2269 dt_trans_stop(env, dev, th);
2272 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2273 "the object "DFID", new parent "DFID": rc = %d\n",
2274 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2278 struct lfsck_namespace *ns = com->lc_file_ram;
2280 ns->ln_flags |= LF_INCONSISTENT;
2287 * Handle orphan @obj during Double Scan Directory.
2289 * Remove the @obj's current (invalid) linkEA entries, and insert
2290 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2291 * ${FID}-${PFID}-D-${conflict_version}
2293 * The caller should take the ldlm lock before the calling.
2295 * \param[in] env pointer to the thread context
2296 * \param[in] com pointer to the lfsck component
2297 * \param[in] obj pointer to the orphan object to be handled
2298 * \param[in] pfid the new fid for the object's ".." name entry
2299 * \param[in,out] lh ldlm lock handler for the given @obj
2300 * \param[out] type to tell the caller what the inconsistency is
2302 * \retval positive number for repaired cases
2303 * \retval 0 if nothing to be repaired
2304 * \retval negative error number on failure
2307 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2308 struct lfsck_component *com,
2309 struct dt_object *obj,
2310 const struct lu_fid *pfid,
2311 struct lustre_handle *lh,
2312 enum lfsck_namespace_inconsistency_type *type)
2314 struct lfsck_thread_info *info = lfsck_env_info(env);
2315 struct lfsck_namespace *ns = com->lc_file_ram;
2319 /* Remove the unrecognized linkEA. */
2320 rc = lfsck_namespace_links_remove(env, com, obj);
2321 lfsck_ibits_unlock(lh, LCK_EX);
2322 if (rc < 0 && rc != -ENODATA)
2325 *type = LNIT_MUL_REF;
2327 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2328 * ever tried to verify some remote MDT-object that resides on this
2329 * MDT, but this MDT failed to respond such request. So means there
2330 * may be some remote name entry on other MDT that references this
2331 * object with another name, so we cannot know whether this linkEA
2332 * is valid or not. So keep it there and maybe resolved when next
2334 if (ns->ln_flags & LF_INCOMPLETE)
2337 /* The unique linkEA is invalid, even if the ".." name entry may be
2338 * valid, we still cannot know via which name entry this directory
2339 * will be referenced. Then handle it as pure orphan. */
2340 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2341 "-"DFID, PFID(pfid));
2342 rc = lfsck_namespace_insert_orphan(env, com, obj,
2343 info->lti_tmpbuf, "D", NULL);
2349 * Double Scan Directory object for single linkEA entry case.
2351 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2352 * then check whether the name is in the namespace or not, if not, add the
2353 * missing name entry back to namespace. If the linkEA entry is invalid,
2354 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2357 * \param[in] env pointer to the thread context
2358 * \param[in] com pointer to the lfsck component
2359 * \param[in] child pointer to the directory to be double scanned
2360 * \param[in] pfid the FID corresponding to the ".." entry
2361 * \param[in] ldata pointer to the linkEA data for the given @child
2362 * \param[in,out] lh ldlm lock handler for the given @child
2363 * \param[out] type to tell the caller what the inconsistency is
2364 * \param[in] retry if found inconsistency, but the caller does not hold
2365 * ldlm lock on the @child, then set @retry as true
2367 * \retval positive number for repaired cases
2368 * \retval 0 if nothing to be repaired
2369 * \retval negative error number on failure
2372 lfsck_namespace_dsd_single(const struct lu_env *env,
2373 struct lfsck_component *com,
2374 struct dt_object *child,
2375 const struct lu_fid *pfid,
2376 struct linkea_data *ldata,
2377 struct lustre_handle *lh,
2378 enum lfsck_namespace_inconsistency_type *type,
2381 struct lfsck_thread_info *info = lfsck_env_info(env);
2382 struct lu_name *cname = &info->lti_name;
2383 const struct lu_fid *cfid = lfsck_dto2fid(child);
2384 struct lu_fid *tfid = &info->lti_fid3;
2385 struct lfsck_namespace *ns = com->lc_file_ram;
2386 struct lfsck_instance *lfsck = com->lc_lfsck;
2387 struct dt_object *parent = NULL;
2388 struct lmv_mds_md_v1 *lmv;
2392 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
2393 /* The unique linkEA entry with bad parent will be handled as orphan. */
2394 if (!fid_is_sane(tfid)) {
2395 if (!lustre_handle_is_used(lh) && retry != NULL)
2398 rc = lfsck_namespace_dsd_orphan(env, com, child,
2404 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2406 GOTO(out, rc = PTR_ERR(parent));
2408 /* We trust the unique linkEA entry in spite of whether it matches the
2409 * ".." name entry or not. Because even if the linkEA entry is wrong
2410 * and the ".." name entry is right, we still cannot know via which
2411 * name entry the child will be referenced, since all known entries
2412 * have been verified during the first-stage scanning. */
2413 if (!dt_object_exists(parent)) {
2414 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2415 * has ever tried to verify some remote MDT-object that resides
2416 * on this MDT, but this MDT failed to respond such request. So
2417 * means there may be some remote name entry on other MDT that
2418 * references this object with another name, so we cannot know
2419 * whether this linkEA is valid or not. So keep it there and
2420 * maybe resolved when next LFSCK run. */
2421 if (ns->ln_flags & LF_INCOMPLETE)
2424 if (!lustre_handle_is_used(lh) && retry != NULL) {
2430 lfsck_ibits_unlock(lh, LCK_EX);
2433 lmv = &info->lti_lmv;
2434 rc = lfsck_read_stripe_lmv(env, child, lmv);
2435 if (rc != 0 && rc != -ENODATA)
2438 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2440 } else if (lfsck_shard_name_to_index(env,
2441 cname->ln_name, cname->ln_namelen,
2442 S_IFDIR, cfid) < 0) {
2443 /* It is an invalid name entry, we
2444 * cannot trust the parent also. */
2445 rc = lfsck_namespace_shrink_linkea(env, com, child,
2446 ldata, cname, tfid, true);
2450 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2451 "-"DFID, PFID(pfid));
2452 rc = lfsck_namespace_insert_orphan(env, com, child,
2453 info->lti_tmpbuf, "S", NULL);
2458 /* Create the lost parent as an orphan. */
2459 rc = lfsck_namespace_create_orphan(env, com, parent, lmv);
2461 /* Add the missing name entry to the parent. */
2462 rc = lfsck_namespace_insert_normal(env, com, parent,
2463 child, cname->ln_name);
2464 if (unlikely(rc == -EEXIST)) {
2465 /* Unfortunately, someone reused the name
2466 * under the parent by race. So we have
2467 * to remove the linkEA entry from
2468 * current child object. It means that the
2469 * LFSCK cannot recover the system
2470 * totally back to its original status,
2471 * but it is necessary to make the
2472 * current system to be consistent. */
2473 rc = lfsck_namespace_shrink_linkea(env,
2477 snprintf(info->lti_tmpbuf,
2478 sizeof(info->lti_tmpbuf),
2479 "-"DFID, PFID(pfid));
2480 rc = lfsck_namespace_insert_orphan(env,
2481 com, child, info->lti_tmpbuf,
2490 /* The unique linkEA entry with bad parent will be handled as orphan. */
2491 if (unlikely(!dt_try_as_dir(env, parent))) {
2492 if (!lustre_handle_is_used(lh) && retry != NULL)
2495 rc = lfsck_namespace_dsd_orphan(env, com, child,
2501 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2502 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2503 if (rc == -ENOENT) {
2504 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2505 * has ever tried to verify some remote MDT-object that resides
2506 * on this MDT, but this MDT failed to respond such request. So
2507 * means there may be some remote name entry on other MDT that
2508 * references this object with another name, so we cannot know
2509 * whether this linkEA is valid or not. So keep it there and
2510 * maybe resolved when next LFSCK run. */
2511 if (ns->ln_flags & LF_INCOMPLETE)
2514 if (!lustre_handle_is_used(lh) && retry != NULL) {
2520 lfsck_ibits_unlock(lh, LCK_EX);
2521 rc = lfsck_namespace_check_name(env, parent, child, cname);
2528 /* It is an invalid name entry, drop it. */
2529 if (unlikely(rc > 0)) {
2530 rc = lfsck_namespace_shrink_linkea(env, com, child,
2531 ldata, cname, tfid, true);
2533 snprintf(info->lti_tmpbuf,
2534 sizeof(info->lti_tmpbuf),
2535 "-"DFID, PFID(pfid));
2536 rc = lfsck_namespace_insert_orphan(env, com,
2537 child, info->lti_tmpbuf, "D", NULL);
2543 /* Add the missing name entry back to the namespace. */
2544 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2546 if (unlikely(rc == -ESTALE))
2547 /* It may happen when the remote object has been
2548 * removed, but the local MDT is not aware of that. */
2551 if (unlikely(rc == -EEXIST)) {
2552 /* Unfortunately, someone reused the name under the
2553 * parent by race. So we have to remove the linkEA
2554 * entry from current child object. It means that the
2555 * LFSCK cannot recover the system totally back to
2556 * its original status, but it is necessary to make
2557 * the current system to be consistent.
2559 * It also may be because of the LFSCK found some
2560 * internal status of create operation. Under such
2561 * case, nothing to be done. */
2562 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2563 parent, child, ldata, cname, tfid);
2565 snprintf(info->lti_tmpbuf,
2566 sizeof(info->lti_tmpbuf),
2567 "-"DFID, PFID(pfid));
2568 rc = lfsck_namespace_insert_orphan(env, com,
2569 child, info->lti_tmpbuf, "D", NULL);
2579 if (!lu_fid_eq(tfid, cfid)) {
2580 if (!lustre_handle_is_used(lh) && retry != NULL) {
2586 lfsck_ibits_unlock(lh, LCK_EX);
2587 /* The name entry references another MDT-object that
2588 * may be created by the LFSCK for repairing dangling
2589 * name entry. Try to replace it. */
2590 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2593 rc = lfsck_namespace_dsd_orphan(env, com, child,
2599 if (fid_is_zero(pfid))
2602 /* The ".." name entry is wrong, update it. */
2603 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2604 if (!lustre_handle_is_used(lh) && retry != NULL) {
2610 *type = LNIT_UNMATCHED_PAIRS;
2611 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2612 lfsck_dto2fid(parent), cname);
2618 if (parent != NULL && !IS_ERR(parent))
2619 lfsck_object_put(env, parent);
2625 * Double Scan Directory object for multiple linkEA entries case.
2627 * The given @child has multiple linkEA entries. There is at most one linkEA
2628 * entry will be valid, all the others will be removed. Firstly, the function
2629 * will try to find out the linkEA entry for which the name entry exists under
2630 * the given parent (@pfid). If there is no linkEA entry that matches the given
2631 * ".." name entry, then tries to find out the first linkEA entry that both the
2632 * parent and the name entry exist to rebuild a new ".." name entry.
2634 * \param[in] env pointer to the thread context
2635 * \param[in] com pointer to the lfsck component
2636 * \param[in] child pointer to the directory to be double scanned
2637 * \param[in] pfid the FID corresponding to the ".." entry
2638 * \param[in] ldata pointer to the linkEA data for the given @child
2639 * \param[in,out] lh ldlm lock handler for the given @child
2640 * \param[out] type to tell the caller what the inconsistency is
2641 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2643 * \retval positive number for repaired cases
2644 * \retval 0 if nothing to be repaired
2645 * \retval negative error number on failure
2648 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2649 struct lfsck_component *com,
2650 struct dt_object *child,
2651 const struct lu_fid *pfid,
2652 struct linkea_data *ldata,
2653 struct lustre_handle *lh,
2654 enum lfsck_namespace_inconsistency_type *type,
2657 struct lfsck_thread_info *info = lfsck_env_info(env);
2658 struct lu_name *cname = &info->lti_name;
2659 const struct lu_fid *cfid = lfsck_dto2fid(child);
2660 struct lu_fid *tfid = &info->lti_fid3;
2661 struct lu_fid *pfid2 = &info->lti_fid4;
2662 struct lfsck_namespace *ns = com->lc_file_ram;
2663 struct lfsck_instance *lfsck = com->lc_lfsck;
2664 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2665 struct dt_object *parent = NULL;
2666 struct linkea_data ldata_new = { 0 };
2667 int dirent_count = 0;
2668 int linkea_count = 0;
2674 while (ldata->ld_lee != NULL) {
2675 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
2677 /* Drop repeated linkEA entries. */
2678 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
2679 /* Drop invalid linkEA entry. */
2680 if (!fid_is_sane(tfid)) {
2681 linkea_del_buf(ldata, cname);
2686 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2687 * then it is possible that: the directry object has ever
2688 * been lost, but its name entry was there. In the former
2689 * LFSCK run, during the first-stage scanning, the LFSCK
2690 * found the dangling name entry, but it did not recreate
2691 * the lost object, and when moved to the second-stage
2692 * scanning, some children objects of the lost directory
2693 * object were found, then the LFSCK recreated such lost
2694 * directory object as an orphan.
2696 * When the LFSCK runs again, if the dangling name is still
2697 * there, the LFSCK should move the orphan directory object
2698 * back to the normal namespace. */
2699 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
2700 linkea_next_entry(ldata);
2704 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2706 RETURN(PTR_ERR(parent));
2708 if (!dt_object_exists(parent)) {
2709 lfsck_object_put(env, parent);
2710 if (ldata->ld_leh->leh_reccount > 1) {
2711 /* If it is NOT the last linkEA entry, then
2712 * there is still other chance to make the
2713 * child to be visible via other parent, then
2714 * remove this linkEA entry. */
2715 linkea_del_buf(ldata, cname);
2723 /* The linkEA entry with bad parent will be removed. */
2724 if (unlikely(!dt_try_as_dir(env, parent))) {
2725 lfsck_object_put(env, parent);
2726 linkea_del_buf(ldata, cname);
2731 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2732 (const struct dt_key *)cname->ln_name,
2734 *pfid2 = *lfsck_dto2fid(parent);
2735 if (rc == -ENOENT) {
2736 lfsck_object_put(env, parent);
2737 linkea_next_entry(ldata);
2742 lfsck_object_put(env, parent);
2747 if (lu_fid_eq(tfid, cfid)) {
2748 lfsck_object_put(env, parent);
2749 if (!lu_fid_eq(pfid, pfid2)) {
2750 *type = LNIT_UNMATCHED_PAIRS;
2751 rc = lfsck_namespace_repair_unmatched_pairs(env,
2752 com, child, pfid2, cname);
2758 /* It is the most common case that we find the
2759 * name entry corresponding to the linkEA entry
2760 * that matches the ".." name entry. */
2761 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2765 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2769 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2774 linkea_del_buf(ldata, cname);
2776 linkea_first_entry(ldata);
2777 /* There may be some invalid dangling name entries under
2778 * other parent directories, remove all of them. */
2779 while (ldata->ld_lee != NULL) {
2780 lfsck_namespace_unpack_linkea_entry(ldata,
2781 cname, tfid, info->lti_key);
2782 if (!fid_is_sane(tfid))
2785 parent = lfsck_object_find_bottom(env, lfsck,
2787 if (IS_ERR(parent)) {
2788 rc = PTR_ERR(parent);
2789 if (rc != -ENOENT &&
2790 bk->lb_param & LPF_FAILOUT)
2796 if (!dt_object_exists(parent)) {
2797 lfsck_object_put(env, parent);
2801 rc = lfsck_namespace_repair_dirent(env, com,
2802 parent, child, cname->ln_name,
2803 cname->ln_name, S_IFDIR, false, true);
2804 lfsck_object_put(env, parent);
2806 if (bk->lb_param & LPF_FAILOUT)
2815 linkea_del_buf(ldata, cname);
2818 ns->ln_dirent_repaired += dirent_count;
2823 lfsck_ibits_unlock(lh, LCK_EX);
2824 /* The name entry references another MDT-object that may be
2825 * created by the LFSCK for repairing dangling name entry.
2826 * Try to replace it. */
2827 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2829 lfsck_object_put(env, parent);
2836 linkea_del_buf(ldata, cname);
2839 linkea_first_entry(ldata);
2840 if (ldata->ld_leh->leh_reccount == 1) {
2841 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2844 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2845 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2851 /* All linkEA entries are invalid and removed, then handle the @child
2853 if (ldata->ld_leh->leh_reccount == 0) {
2854 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2860 /* If the dangling name entry for the orphan directory object has
2861 * been remvoed, then just check whether the directory object is
2862 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2868 /* There is no linkEA entry that matches the ".." name entry. Find
2869 * the first linkEA entry that both parent and name entry exist to
2870 * rebuild a new ".." name entry. */
2880 * Repair the object's nlink attribute.
2882 * If all the known name entries have been verified, then the object's hard
2883 * link attribute should match the object's linkEA entries count unless the
2884 * object's has too much hard link to be recorded in the linkEA. Such cases
2885 * should have been marked in the LFSCK trace file. Otherwise, trust the
2886 * linkEA to update the object's nlink attribute.
2888 * \param[in] env pointer to the thread context
2889 * \param[in] com pointer to the lfsck component
2890 * \param[in] obj pointer to the dt_object to be handled
2891 * \param[in,out] nlink pointer to buffer to object's hard lock count before
2892 * and after the repairing
2894 * \retval positive number for repaired cases
2895 * \retval 0 if nothing to be repaired
2896 * \retval negative error number on failure
2898 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2899 struct lfsck_component *com,
2900 struct dt_object *obj, __u32 *nlink)
2902 struct lfsck_thread_info *info = lfsck_env_info(env);
2903 struct lu_attr *la = &info->lti_la3;
2904 struct lu_fid *tfid = &info->lti_fid3;
2905 struct lfsck_namespace *ns = com->lc_file_ram;
2906 struct lfsck_instance *lfsck = com->lc_lfsck;
2907 struct dt_device *dev = lfsck->li_bottom;
2908 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2909 struct dt_object *child = NULL;
2910 struct thandle *th = NULL;
2911 struct linkea_data ldata = { 0 };
2912 struct lustre_handle lh = { 0 };
2918 LASSERT(!dt_object_remote(obj));
2919 LASSERT(S_ISREG(lfsck_object_type(obj)));
2921 child = lfsck_object_find_by_dev(env, dev, cfid);
2923 GOTO(log, rc = PTR_ERR(child));
2925 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2926 MDS_INODELOCK_UPDATE |
2927 MDS_INODELOCK_XATTR, LCK_EX);
2931 th = dt_trans_create(env, dev);
2933 GOTO(log, rc = PTR_ERR(th));
2935 la->la_valid = LA_NLINK;
2936 rc = dt_declare_attr_set(env, child, la, th);
2940 rc = dt_trans_start_local(env, dev, th);
2944 dt_write_lock(env, child, 0);
2945 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2946 * ever tried to verify some remote MDT-object that resides on this
2947 * MDT, but this MDT failed to respond such request. So means there
2948 * may be some remote name entry on other MDT that references this
2949 * object with another name, so we cannot know whether this linkEA
2950 * is valid or not. So keep it there and maybe resolved when next
2952 if (ns->ln_flags & LF_INCOMPLETE)
2953 GOTO(unlock, rc = 0);
2955 fid_cpu_to_be(tfid, cfid);
2956 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)&flags,
2957 (const struct dt_key *)tfid, BYPASS_CAPA);
2961 if (flags & LNTF_SKIP_NLINK)
2962 GOTO(unlock, rc = 0);
2964 rc = lfsck_links_read2(env, child, &ldata);
2966 GOTO(unlock, rc = 0);
2971 if (*nlink == ldata.ld_leh->leh_reccount)
2972 GOTO(unlock, rc = 0);
2974 la->la_nlink = *nlink = ldata.ld_leh->leh_reccount;
2975 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2976 GOTO(unlock, rc = 1);
2978 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
2980 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2983 dt_write_unlock(env, child);
2986 dt_trans_stop(env, dev, th);
2989 lfsck_ibits_unlock(&lh, LCK_EX);
2990 if (child != NULL && !IS_ERR(child))
2991 lfsck_object_put(env, child);
2993 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2994 "nlink count from %u to %u: rc = %d\n",
2995 lfsck_lfsck2name(lfsck), PFID(cfid), old, *nlink, rc);
2998 ns->ln_flags |= LF_INCONSISTENT;
3004 * Double scan the directory object for namespace LFSCK.
3006 * This function will verify the <parent, child> pairs in the namespace tree:
3007 * the parent references the child via some name entry that should be in the
3008 * child's linkEA entry, the child should back references the parent via its
3011 * The LFSCK will scan every linkEA entry in turn until find out the first
3012 * matched pairs. If found, then all other linkEA entries will be dropped.
3013 * If all the linkEA entries cannot match the ".." name entry, then there
3014 * are serveral possible cases:
3016 * 1) If there is only one linkEA entry, then trust it as long as the PFID
3017 * in the linkEA entry is valid.
3019 * 2) If there are multiple linkEA entries, then try to find the linkEA
3020 * that matches the ".." name entry. If found, then all other entries
3021 * are invalid; otherwise, it is quite possible that the ".." name entry
3022 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
3023 * entry according to the first valid linkEA entry (both the parent and
3024 * the name entry should exist).
3026 * 3) If the directory object has no (valid) linkEA entry, then the
3027 * directory object will be handled as pure orphan and inserted
3028 * in the .lustre/lost+found/MDTxxxx/ with the name:
3029 * ${self_FID}-${PFID}-D-${conflict_version}
3031 * \param[in] env pointer to the thread context
3032 * \param[in] com pointer to the lfsck component
3033 * \param[in] child pointer to the directory object to be handled
3034 * \param[in] flags to indicate the specical checking on the @child
3036 * \retval positive number for repaired cases
3037 * \retval 0 if nothing to be repaired
3038 * \retval negative error number on failure
3040 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3041 struct lfsck_component *com,
3042 struct dt_object *child, __u8 flags)
3044 struct lfsck_thread_info *info = lfsck_env_info(env);
3045 const struct lu_fid *cfid = lfsck_dto2fid(child);
3046 struct lu_fid *pfid = &info->lti_fid2;
3047 struct lfsck_namespace *ns = com->lc_file_ram;
3048 struct lfsck_instance *lfsck = com->lc_lfsck;
3049 struct lustre_handle lh = { 0 };
3050 struct linkea_data ldata = { 0 };
3051 bool unknown = false;
3054 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3058 LASSERT(!dt_object_remote(child));
3060 if (flags & LNTF_UNCERTAIN_LMV) {
3061 if (flags & LNTF_RECHECK_NAME_HASH) {
3062 rc = lfsck_namespace_scan_shard(env, com, child);
3066 ns->ln_striped_shards_scanned++;
3068 ns->ln_striped_shards_skipped++;
3072 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3076 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3077 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3078 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3079 "the namespace LFSCK, then the LFSCK cannot guarantee"
3080 "all the name entries have been verified in first-stage"
3081 "scanning. So have to skip orphan related handling for"
3082 "the directory object "DFID" with remote name entry\n",
3083 lfsck_lfsck2name(lfsck), PFID(cfid));
3088 if (unlikely(!dt_try_as_dir(env, child)))
3089 GOTO(out, rc = -ENOTDIR);
3091 /* We only take ldlm lock on the @child when required. When the
3092 * logic comes here for the first time, it is always false. */
3096 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3097 MDS_INODELOCK_UPDATE |
3098 MDS_INODELOCK_XATTR, LCK_EX);
3103 dt_read_lock(env, child, 0);
3104 if (unlikely(lfsck_is_dead_obj(child))) {
3105 dt_read_unlock(env, child);
3110 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3111 (const struct dt_key *)dotdot, BYPASS_CAPA);
3113 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3114 dt_read_unlock(env, child);
3119 if (!lustre_handle_is_used(&lh)) {
3120 dt_read_unlock(env, child);
3125 } else if (lfsck->li_lpf_obj != NULL &&
3126 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3128 } else if (unlikely(!fid_is_sane(pfid))) {
3132 rc = lfsck_links_read(env, child, &ldata);
3133 dt_read_unlock(env, child);
3135 if (rc != -ENODATA && rc != -EINVAL)
3138 if (!lustre_handle_is_used(&lh))
3141 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3142 /* Remove the corrupted linkEA. */
3143 rc = lfsck_namespace_links_remove(env, com, child);
3145 /* Here, because of the crashed linkEA, we
3146 * cannot know whether there is some parent
3147 * that references the child directory via
3148 * some name entry or not. So keep it there,
3149 * when the LFSCK run next time, if there is
3150 * some parent that references this object,
3151 * then the LFSCK can rebuild the linkEA;
3152 * otherwise, this object will be handled
3153 * as orphan as above. */
3156 /* 1. If we have neither ".." nor linkEA,
3157 * then it is an orphan.
3159 * 2. If we only have the ".." name entry,
3160 * but no parent references this child
3161 * directory, then handle it as orphan. */
3162 lfsck_ibits_unlock(&lh, LCK_EX);
3163 type = LNIT_MUL_REF;
3165 /* If the LFSCK is marked as LF_INCOMPLETE,
3166 * then means some MDT has ever tried to
3167 * verify some remote MDT-object that resides
3168 * on this MDT, but this MDT failed to respond
3169 * such request. So means there may be some
3170 * remote name entry on other MDT that
3171 * references this object with another name,
3172 * so we cannot know whether this linkEA is
3173 * valid or not. So keep it there and maybe
3174 * resolved when next LFSCK run. */
3175 if (ns->ln_flags & LF_INCOMPLETE)
3178 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3179 "-"DFID, PFID(pfid));
3180 rc = lfsck_namespace_insert_orphan(env, com, child,
3181 info->lti_tmpbuf, "D", NULL);
3187 linkea_first_entry(&ldata);
3188 /* This is the most common case: the object has unique linkEA entry. */
3189 if (ldata.ld_leh->leh_reccount == 1) {
3190 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3191 &lh, &type, &retry);
3193 LASSERT(!lustre_handle_is_used(&lh));
3202 if (!lustre_handle_is_used(&lh))
3205 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3206 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3212 /* When we come here, the cases usually like that:
3213 * 1) The directory object has a corrupted linkEA entry. During the
3214 * first-stage scanning, the LFSCK cannot know such corruption,
3215 * then it appends the right linkEA entry according to the found
3216 * name entry after the bad one.
3218 * 2) The directory object has a right linkEA entry. During the
3219 * first-stage scanning, the LFSCK finds some bad name entry,
3220 * but the LFSCK cannot aware that at that time, then it adds
3221 * the bad linkEA entry for further processing. */
3222 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3228 lfsck_ibits_unlock(&lh, LCK_EX);
3231 case LNIT_BAD_LINKEA:
3232 ns->ln_linkea_repaired++;
3234 case LNIT_UNMATCHED_PAIRS:
3235 ns->ln_unmatched_pairs_repaired++;
3238 ns->ln_mul_ref_repaired++;
3246 ns->ln_unknown_inconsistency++;
3252 * Double scan the MDT-object for namespace LFSCK.
3254 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3255 * those entries from the linkEA; if the linkEA becomes empty or the object
3256 * has no linkEA, then it is an orphan and will be added into the directory
3257 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3258 * the remote parent; if the name entry corresponding to some linkEA entry
3259 * is lost, then add the name entry back to the namespace.
3261 * \param[in] env pointer to the thread context
3262 * \param[in] com pointer to the lfsck component
3263 * \param[in] child pointer to the dt_object to be handled
3264 * \param[in] flags some hints to indicate how the @child should be handled
3266 * \retval positive number for repaired cases
3267 * \retval 0 if nothing to be repaired
3268 * \retval negative error number on failure
3270 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3271 struct lfsck_component *com,
3272 struct dt_object *child, __u8 flags)
3274 struct lfsck_thread_info *info = lfsck_env_info(env);
3275 struct lu_attr *la = &info->lti_la;
3276 struct lu_name *cname = &info->lti_name;
3277 struct lu_fid *pfid = &info->lti_fid;
3278 struct lu_fid *cfid = &info->lti_fid2;
3279 struct lfsck_instance *lfsck = com->lc_lfsck;
3280 struct lfsck_namespace *ns = com->lc_file_ram;
3281 struct dt_object *parent = NULL;
3282 struct linkea_data ldata = { 0 };
3283 bool repaired = false;
3288 dt_read_lock(env, child, 0);
3289 if (unlikely(lfsck_is_dead_obj(child))) {
3290 dt_read_unlock(env, child);
3295 if (S_ISDIR(lfsck_object_type(child))) {
3296 dt_read_unlock(env, child);
3297 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3302 rc = lfsck_links_read(env, child, &ldata);
3303 dt_read_unlock(env, child);
3307 linkea_first_entry(&ldata);
3308 while (ldata.ld_lee != NULL) {
3309 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3311 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3313 /* Found repeated linkEA entries */
3315 rc = lfsck_namespace_shrink_linkea(env, com, child,
3316 &ldata, cname, pfid, false);
3328 /* Invalid PFID in the linkEA entry. */
3329 if (!fid_is_sane(pfid)) {
3330 rc = lfsck_namespace_shrink_linkea(env, com, child,
3331 &ldata, cname, pfid, true);
3341 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3343 GOTO(out, rc = PTR_ERR(parent));
3345 if (!dt_object_exists(parent)) {
3348 if (ldata.ld_leh->leh_reccount > 1) {
3349 /* If it is NOT the last linkEA entry, then
3350 * there is still other chance to make the
3351 * child to be visible via other parent, then
3352 * remove this linkEA entry. */
3353 rc = lfsck_namespace_shrink_linkea(env, com,
3354 child, &ldata, cname, pfid, true);
3356 /* If the LFSCK is marked as LF_INCOMPLETE,
3357 * then means some MDT has ever tried to
3358 * verify some remote MDT-object that resides
3359 * on this MDT, but this MDT failed to respond
3360 * such request. So means there may be some
3361 * remote name entry on other MDT that
3362 * references this object with another name,
3363 * so we cannot know whether this linkEA is
3364 * valid or not. So keep it there and maybe
3365 * resolved when next LFSCK run. */
3366 if (ns->ln_flags & LF_INCOMPLETE) {
3367 lfsck_object_put(env, parent);
3372 /* Create the lost parent as an orphan. */
3373 rc = lfsck_namespace_create_orphan(env, com,
3376 lfsck_object_put(env, parent);
3384 /* Add the missing name entry to the parent. */
3385 rc = lfsck_namespace_insert_normal(env, com,
3386 parent, child, cname->ln_name);
3387 if (unlikely(rc == -EEXIST))
3388 /* Unfortunately, someone reused the
3389 * name under the parent by race. So we
3390 * have to remove the linkEA entry from
3391 * current child object. It means that
3392 * the LFSCK cannot recover the system
3393 * totally back to its original status,
3394 * but it is necessary to make the
3395 * current system to be consistent. */
3396 rc = lfsck_namespace_shrink_linkea(env,
3400 linkea_next_entry(&ldata);
3403 lfsck_object_put(env, parent);
3413 /* The linkEA entry with bad parent will be removed. */
3414 if (unlikely(!dt_try_as_dir(env, parent))) {
3415 lfsck_object_put(env, parent);
3416 rc = lfsck_namespace_shrink_linkea(env, com, child,
3417 &ldata, cname, pfid, true);
3427 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3428 (const struct dt_key *)cname->ln_name,
3430 if (rc != 0 && rc != -ENOENT) {
3431 lfsck_object_put(env, parent);
3437 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3438 /* It is the most common case that we
3439 * find the name entry corresponding
3440 * to the linkEA entry. */
3441 lfsck_object_put(env, parent);
3442 linkea_next_entry(&ldata);
3444 /* The name entry references another
3445 * MDT-object that may be created by
3446 * the LFSCK for repairing dangling
3447 * name entry. Try to replace it. */
3448 rc = lfsck_namespace_replace_cond(env, com,
3449 parent, child, cfid, cname);
3450 lfsck_object_put(env, parent);
3456 linkea_next_entry(&ldata);
3458 rc = lfsck_namespace_shrink_linkea(env,
3472 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3476 /* If there is no name entry in the parent dir and the object
3477 * link count is less than the linkea entries count, then the
3478 * linkea entry should be removed. */
3479 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3480 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3481 parent, child, &ldata, cname, pfid);
3482 lfsck_object_put(env, parent);
3492 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3493 * MDT has ever tried to verify some remote MDT-object that
3494 * resides on this MDT, but this MDT failed to respond such
3495 * request. So means there may be some remote name entry on
3496 * other MDT that references this object with another name,
3497 * so we cannot know whether this linkEA is valid or not.
3498 * So keep it there and maybe resolved when next LFSCK run. */
3499 if (ns->ln_flags & LF_INCOMPLETE) {
3500 lfsck_object_put(env, parent);
3505 rc = lfsck_namespace_check_name(env, parent, child, cname);
3510 lfsck_object_put(env, parent);
3515 /* It is an invalid name entry, drop it. */
3516 if (unlikely(rc > 0)) {
3517 lfsck_object_put(env, parent);
3518 rc = lfsck_namespace_shrink_linkea(env, com, child,
3519 &ldata, cname, pfid, true);
3529 /* Add the missing name entry back to the namespace. */
3530 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3532 if (unlikely(rc == -ESTALE))
3533 /* It may happen when the remote object has been
3534 * removed, but the local MDT is not aware of that. */
3537 if (unlikely(rc == -EEXIST))
3538 /* Unfortunately, someone reused the name under the
3539 * parent by race. So we have to remove the linkEA
3540 * entry from current child object. It means that the
3541 * LFSCK cannot recover the system totally back to
3542 * its original status, but it is necessary to make
3543 * the current system to be consistent.
3545 * It also may be because of the LFSCK found some
3546 * internal status of create operation. Under such
3547 * case, nothing to be done. */
3548 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3549 parent, child, &ldata, cname, pfid);
3551 linkea_next_entry(&ldata);
3553 lfsck_object_put(env, parent);
3564 if (rc < 0 && rc != -ENODATA)
3568 LASSERT(ldata.ld_leh != NULL);
3570 count = ldata.ld_leh->leh_reccount;
3573 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3574 * MDT has ever tried to verify some remote MDT-object that
3575 * resides on this MDT, but this MDT failed to respond such
3576 * request. So means there may be some remote name entry on
3577 * other MDT that references this object with another name,
3578 * so we cannot know whether this linkEA is valid or not.
3579 * So keep it there and maybe resolved when next LFSCK run. */
3580 if (count == 0 && !(ns->ln_flags & LF_INCOMPLETE)) {
3581 /* If the child becomes orphan, then insert it into
3582 * the global .lustre/lost+found/MDTxxxx directory. */
3583 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
3589 ns->ln_mul_ref_repaired++;
3594 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3598 if (la->la_nlink != count) {
3599 rc = lfsck_namespace_repair_nlink(env, com, child,
3602 ns->ln_objs_nlink_repaired++;
3608 if (la->la_nlink > 1)
3609 ns->ln_mul_linked_repaired++;
3618 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3619 struct lfsck_namespace *ns,
3620 __u64 checked_phase1,
3621 __u64 checked_phase2,
3625 seq_printf(m, "checked_phase1: "LPU64"\n"
3626 "checked_phase2: "LPU64"\n"
3627 "updated_phase1: "LPU64"\n"
3628 "updated_phase2: "LPU64"\n"
3629 "failed_phase1: "LPU64"\n"
3630 "failed_phase2: "LPU64"\n"
3631 "directories: "LPU64"\n"
3632 "dirent_repaired: "LPU64"\n"
3633 "linkea_repaired: "LPU64"\n"
3634 "nlinks_repaired: "LPU64"\n"
3635 "multiple_linked_checked: "LPU64"\n"
3636 "multiple_linked_repaired: "LPU64"\n"
3637 "unknown_inconsistency: "LPU64"\n"
3638 "unmatched_pairs_repaired: "LPU64"\n"
3639 "dangling_repaired: "LPU64"\n"
3640 "multiple_referenced_repaired: "LPU64"\n"
3641 "bad_file_type_repaired: "LPU64"\n"
3642 "lost_dirent_repaired: "LPU64"\n"
3643 "local_lost_found_scanned: "LPU64"\n"
3644 "local_lost_found_moved: "LPU64"\n"
3645 "local_lost_found_skipped: "LPU64"\n"
3646 "local_lost_found_failed: "LPU64"\n"
3647 "striped_dirs_scanned: "LPU64"\n"
3648 "striped_dirs_repaired: "LPU64"\n"
3649 "striped_dirs_failed: "LPU64"\n"
3650 "striped_dirs_disabled: "LPU64"\n"
3651 "striped_dirs_skipped: "LPU64"\n"
3652 "striped_shards_scanned: "LPU64"\n"
3653 "striped_shards_repaired: "LPU64"\n"
3654 "striped_shards_failed: "LPU64"\n"
3655 "striped_shards_skipped: "LPU64"\n"
3656 "name_hash_repaired: "LPU64"\n"
3657 "success_count: %u\n"
3658 "run_time_phase1: %u seconds\n"
3659 "run_time_phase2: %u seconds\n",
3662 ns->ln_items_repaired,
3663 ns->ln_objs_repaired_phase2,
3664 ns->ln_items_failed,
3665 ns->ln_objs_failed_phase2,
3666 ns->ln_dirs_checked,
3667 ns->ln_dirent_repaired,
3668 ns->ln_linkea_repaired,
3669 ns->ln_objs_nlink_repaired,
3670 ns->ln_mul_linked_checked,
3671 ns->ln_mul_linked_repaired,
3672 ns->ln_unknown_inconsistency,
3673 ns->ln_unmatched_pairs_repaired,
3674 ns->ln_dangling_repaired,
3675 ns->ln_mul_ref_repaired,
3676 ns->ln_bad_type_repaired,
3677 ns->ln_lost_dirent_repaired,
3678 ns->ln_local_lpf_scanned,
3679 ns->ln_local_lpf_moved,
3680 ns->ln_local_lpf_skipped,
3681 ns->ln_local_lpf_failed,
3682 ns->ln_striped_dirs_scanned,
3683 ns->ln_striped_dirs_repaired,
3684 ns->ln_striped_dirs_failed,
3685 ns->ln_striped_dirs_disabled,
3686 ns->ln_striped_dirs_skipped,
3687 ns->ln_striped_shards_scanned,
3688 ns->ln_striped_shards_repaired,
3689 ns->ln_striped_shards_failed,
3690 ns->ln_striped_shards_skipped,
3691 ns->ln_name_hash_repaired,
3692 ns->ln_success_count,
3697 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3698 struct lfsck_component *com)
3700 struct lfsck_instance *lfsck = com->lc_lfsck;
3701 struct lfsck_namespace *ns = com->lc_file_ram;
3703 while (!list_empty(&lfsck->li_list_lmv)) {
3704 struct lfsck_lmv_unit *llu;
3705 struct lfsck_lmv *llmv;
3707 llu = list_entry(lfsck->li_list_lmv.next,
3708 struct lfsck_lmv_unit, llu_link);
3709 llmv = &llu->llu_lmv;
3711 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3712 "still in using: %u\n",
3713 atomic_read(&llmv->ll_ref));
3715 ns->ln_striped_dirs_skipped++;
3716 lfsck_lmv_put(env, llmv);
3720 /* namespace APIs */
3722 static int lfsck_namespace_reset(const struct lu_env *env,
3723 struct lfsck_component *com, bool init)
3725 struct lfsck_instance *lfsck = com->lc_lfsck;
3726 struct lfsck_namespace *ns = com->lc_file_ram;
3727 struct lfsck_assistant_data *lad = com->lc_data;
3728 struct dt_object *root;
3729 struct dt_object *dto;
3733 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3735 GOTO(log, rc = PTR_ERR(root));
3737 if (unlikely(!dt_try_as_dir(env, root)))
3738 GOTO(put, rc = -ENOTDIR);
3740 down_write(&com->lc_sem);
3742 memset(ns, 0, sizeof(*ns));
3744 __u32 count = ns->ln_success_count;
3745 __u64 last_time = ns->ln_time_last_complete;
3747 memset(ns, 0, sizeof(*ns));
3748 ns->ln_success_count = count;
3749 ns->ln_time_last_complete = last_time;
3751 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3752 ns->ln_status = LS_INIT;
3754 rc = local_object_unlink(env, lfsck->li_bottom, root,
3755 lfsck_namespace_name);
3759 lfsck_object_put(env, com->lc_obj);
3761 dto = local_index_find_or_create(env, lfsck->li_los, root,
3762 lfsck_namespace_name,
3763 S_IFREG | S_IRUGO | S_IWUSR,
3764 &dt_lfsck_features);
3766 GOTO(out, rc = PTR_ERR(dto));
3769 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
3773 lad->lad_incomplete = 0;
3774 CFS_RESET_BITMAP(lad->lad_bitmap);
3776 rc = lfsck_namespace_store(env, com, true);
3781 up_write(&com->lc_sem);
3784 lu_object_put(env, &root->do_lu);
3786 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3787 lfsck_lfsck2name(lfsck), rc);
3792 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3795 struct lfsck_namespace *ns = com->lc_file_ram;
3797 down_write(&com->lc_sem);
3799 com->lc_new_checked++;
3800 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3801 up_write(&com->lc_sem);
3804 static void lfsck_namespace_close_dir(const struct lu_env *env,
3805 struct lfsck_component *com)
3807 struct lfsck_namespace *ns = com->lc_file_ram;
3808 struct lfsck_assistant_data *lad = com->lc_data;
3809 struct lfsck_instance *lfsck = com->lc_lfsck;
3810 struct lfsck_lmv *llmv = lfsck->li_lmv;
3811 struct lfsck_namespace_req *lnr;
3813 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3814 bool wakeup = false;
3820 OBD_ALLOC(lnr, size);
3822 ns->ln_striped_dirs_skipped++;
3827 /* Generate a dummy request to indicate that all shards' name entry
3828 * in this striped directory has been scanned for the first time. */
3829 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3830 lnr->lnr_obj = lfsck_object_get(lfsck->li_obj_dir);
3831 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3832 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3833 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3834 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3835 lnr->lnr_size = size;
3837 spin_lock(&lad->lad_lock);
3838 if (lad->lad_assistant_status < 0) {
3839 spin_unlock(&lad->lad_lock);
3840 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3841 ns->ln_striped_dirs_skipped++;
3846 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3847 if (lad->lad_prefetched == 0)
3850 lad->lad_prefetched++;
3851 spin_unlock(&lad->lad_lock);
3853 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3858 static int lfsck_namespace_open_dir(const struct lu_env *env,
3859 struct lfsck_component *com)
3861 struct lfsck_instance *lfsck = com->lc_lfsck;
3862 struct lfsck_namespace *ns = com->lc_file_ram;
3863 struct lfsck_lmv *llmv = lfsck->li_lmv;
3870 if (llmv->ll_lmv_master) {
3871 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3873 if (lmv->lmv_master_mdt_index !=
3874 lfsck_dev_idx(lfsck->li_bottom)) {
3875 lmv->lmv_master_mdt_index =
3876 lfsck_dev_idx(lfsck->li_bottom);
3877 ns->ln_flags |= LF_INCONSISTENT;
3878 llmv->ll_lmv_updated = 1;
3881 rc = lfsck_namespace_verify_stripe_slave(env, com,
3882 lfsck->li_obj_dir, llmv);
3885 RETURN(rc > 0 ? 0 : rc);
3888 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3889 struct lfsck_component *com, bool init)
3891 struct lfsck_instance *lfsck = com->lc_lfsck;
3892 struct lfsck_namespace *ns = com->lc_file_ram;
3896 rc = lfsck_checkpoint_generic(env, com);
3901 down_write(&com->lc_sem);
3903 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3905 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3906 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3907 HALF_SEC - lfsck->li_time_last_checkpoint);
3908 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3909 ns->ln_items_checked += com->lc_new_checked;
3910 com->lc_new_checked = 0;
3913 rc = lfsck_namespace_store(env, com, false);
3914 up_write(&com->lc_sem);
3917 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3918 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3919 lfsck->li_pos_current.lp_oit_cookie,
3920 PFID(&lfsck->li_pos_current.lp_dir_parent),
3921 lfsck->li_pos_current.lp_dir_cookie, rc);
3923 return rc > 0 ? 0 : rc;
3926 static int lfsck_namespace_prep(const struct lu_env *env,
3927 struct lfsck_component *com,
3928 struct lfsck_start_param *lsp)
3930 struct lfsck_instance *lfsck = com->lc_lfsck;
3931 struct lfsck_namespace *ns = com->lc_file_ram;
3932 struct lfsck_position *pos = &com->lc_pos_start;
3935 rc = lfsck_namespace_load_bitmap(env, com);
3936 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3937 rc = lfsck_namespace_reset(env, com, false);
3939 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3942 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3943 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3949 down_write(&com->lc_sem);
3950 ns->ln_time_latest_start = cfs_time_current_sec();
3951 spin_lock(&lfsck->li_lock);
3953 if (ns->ln_flags & LF_SCANNED_ONCE) {
3954 if (!lfsck->li_drop_dryrun ||
3955 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3956 ns->ln_status = LS_SCANNING_PHASE2;
3957 list_move_tail(&com->lc_link,
3958 &lfsck->li_list_double_scan);
3959 if (!list_empty(&com->lc_link_dir))
3960 list_del_init(&com->lc_link_dir);
3961 lfsck_pos_set_zero(pos);
3963 ns->ln_status = LS_SCANNING_PHASE1;
3964 ns->ln_run_time_phase1 = 0;
3965 ns->ln_run_time_phase2 = 0;
3966 ns->ln_items_checked = 0;
3967 ns->ln_items_repaired = 0;
3968 ns->ln_items_failed = 0;
3969 ns->ln_dirs_checked = 0;
3970 ns->ln_objs_checked_phase2 = 0;
3971 ns->ln_objs_repaired_phase2 = 0;
3972 ns->ln_objs_failed_phase2 = 0;
3973 ns->ln_objs_nlink_repaired = 0;
3974 ns->ln_dirent_repaired = 0;
3975 ns->ln_linkea_repaired = 0;
3976 ns->ln_mul_linked_checked = 0;
3977 ns->ln_mul_linked_repaired = 0;
3978 ns->ln_unknown_inconsistency = 0;
3979 ns->ln_unmatched_pairs_repaired = 0;
3980 ns->ln_dangling_repaired = 0;
3981 ns->ln_mul_ref_repaired = 0;
3982 ns->ln_bad_type_repaired = 0;
3983 ns->ln_lost_dirent_repaired = 0;
3984 ns->ln_striped_dirs_scanned = 0;
3985 ns->ln_striped_dirs_repaired = 0;
3986 ns->ln_striped_dirs_failed = 0;
3987 ns->ln_striped_dirs_disabled = 0;
3988 ns->ln_striped_dirs_skipped = 0;
3989 ns->ln_striped_shards_scanned = 0;
3990 ns->ln_striped_shards_repaired = 0;
3991 ns->ln_striped_shards_failed = 0;
3992 ns->ln_striped_shards_skipped = 0;
3993 ns->ln_name_hash_repaired = 0;
3994 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3995 if (list_empty(&com->lc_link_dir))
3996 list_add_tail(&com->lc_link_dir,
3997 &lfsck->li_list_dir);
3998 *pos = ns->ln_pos_first_inconsistent;
4001 ns->ln_status = LS_SCANNING_PHASE1;
4002 if (list_empty(&com->lc_link_dir))
4003 list_add_tail(&com->lc_link_dir,
4004 &lfsck->li_list_dir);
4005 if (!lfsck->li_drop_dryrun ||
4006 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4007 *pos = ns->ln_pos_last_checkpoint;
4008 pos->lp_oit_cookie++;
4010 *pos = ns->ln_pos_first_inconsistent;
4014 spin_unlock(&lfsck->li_lock);
4015 up_write(&com->lc_sem);
4017 rc = lfsck_start_assistant(env, com, lsp);
4019 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
4020 DFID", "LPX64"]: rc = %d\n",
4021 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4022 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4027 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4028 struct lfsck_component *com,
4029 struct dt_object *obj)
4031 struct lfsck_thread_info *info = lfsck_env_info(env);
4032 struct lfsck_namespace *ns = com->lc_file_ram;
4033 struct lfsck_instance *lfsck = com->lc_lfsck;
4034 const struct lu_fid *fid = lfsck_dto2fid(obj);
4035 struct lu_attr *la = &info->lti_la;
4036 struct lu_fid *pfid = &info->lti_fid2;
4037 struct lu_name *cname = &info->lti_name;
4038 struct lu_seq_range *range = &info->lti_range;
4039 struct dt_device *dev = lfsck->li_bottom;
4040 struct seq_server_site *ss =
4041 lu_site2seq(dev->dd_lu_dev.ld_site);
4042 struct linkea_data ldata = { 0 };
4043 __u32 idx = lfsck_dev_idx(dev);
4047 rc = lfsck_links_read(env, obj, &ldata);
4051 /* -EINVAL means crashed linkEA, should be verified. */
4052 if (rc == -EINVAL) {
4053 rc = lfsck_namespace_trace_update(env, com, fid,
4054 LNTF_CHECK_LINKEA, true);
4056 struct lustre_handle lh = { 0 };
4058 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4059 MDS_INODELOCK_UPDATE |
4060 MDS_INODELOCK_XATTR, LCK_EX);
4062 rc = lfsck_namespace_links_remove(env, com,
4064 lfsck_ibits_unlock(&lh, LCK_EX);
4068 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4071 /* zero-linkEA object may be orphan, but it also maybe because
4072 * of upgrading. Currently, we cannot record it for double scan.
4073 * Because it may cause the LFSCK trace file to be too large. */
4074 if (rc == -ENODATA) {
4075 if (S_ISDIR(lfsck_object_type(obj)))
4078 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4082 /* "la_ctime" == 1 means that it has ever been removed from
4083 * backend /lost+found directory but not been added back to
4084 * the normal namespace yet. */
4085 if (la->la_nlink > 1 || unlikely(la->la_ctime == 1))
4086 rc = lfsck_namespace_trace_update(env, com, fid,
4087 LNTF_CHECK_LINKEA, true);
4095 /* Record multiple-linked object. */
4096 if (ldata.ld_leh->leh_reccount > 1) {
4097 rc = lfsck_namespace_trace_update(env, com, fid,
4098 LNTF_CHECK_LINKEA, true);
4103 linkea_first_entry(&ldata);
4104 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4105 if (!fid_is_sane(pfid)) {
4106 rc = lfsck_namespace_trace_update(env, com, fid,
4107 LNTF_CHECK_PARENT, true);
4109 fld_range_set_mdt(range);
4110 rc = fld_local_lookup(env, ss->ss_server_fld,
4111 fid_seq(pfid), range);
4112 if ((rc == -ENOENT) ||
4113 (rc == 0 && range->lsr_index != idx)) {
4114 rc = lfsck_namespace_trace_update(env, com, fid,
4115 LNTF_CHECK_LINKEA, true);
4117 if (S_ISDIR(lfsck_object_type(obj)))
4120 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4124 /* "la_ctime" == 1 means that it has ever been
4125 * removed from backend /lost+found directory but
4126 * not been added back to the normal namespace yet. */
4127 if (la->la_nlink > 1 || unlikely(la->la_ctime == 1))
4128 rc = lfsck_namespace_trace_update(env, com,
4129 fid, LNTF_CHECK_LINKEA, true);
4136 down_write(&com->lc_sem);
4137 com->lc_new_checked++;
4138 if (S_ISDIR(lfsck_object_type(obj)))
4139 ns->ln_dirs_checked++;
4141 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4142 up_write(&com->lc_sem);
4147 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4148 struct lfsck_component *com,
4149 struct lu_dirent *ent, __u16 type)
4151 struct lfsck_assistant_data *lad = com->lc_data;
4152 struct lfsck_instance *lfsck = com->lc_lfsck;
4153 struct lfsck_namespace_req *lnr;
4154 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4155 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4156 struct ptlrpc_thread *athread = &lad->lad_thread;
4157 struct l_wait_info lwi = { 0 };
4158 bool wakeup = false;
4160 l_wait_event(mthread->t_ctl_waitq,
4161 bk->lb_async_windows == 0 ||
4162 lad->lad_prefetched < bk->lb_async_windows ||
4163 !thread_is_running(mthread) ||
4164 thread_is_stopped(athread),
4167 if (unlikely(!thread_is_running(mthread)) ||
4168 thread_is_stopped(athread))
4171 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4174 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4176 struct lfsck_namespace *ns = com->lc_file_ram;
4178 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4179 return PTR_ERR(lnr);
4182 spin_lock(&lad->lad_lock);
4183 if (lad->lad_assistant_status < 0) {
4184 spin_unlock(&lad->lad_lock);
4185 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4186 return lad->lad_assistant_status;
4189 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4190 if (lad->lad_prefetched == 0)
4193 lad->lad_prefetched++;
4194 spin_unlock(&lad->lad_lock);
4196 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4198 down_write(&com->lc_sem);
4199 com->lc_new_checked++;
4200 up_write(&com->lc_sem);
4205 static int lfsck_namespace_post(const struct lu_env *env,
4206 struct lfsck_component *com,
4207 int result, bool init)
4209 struct lfsck_instance *lfsck = com->lc_lfsck;
4210 struct lfsck_namespace *ns = com->lc_file_ram;
4214 lfsck_post_generic(env, com, &result);
4216 down_write(&com->lc_sem);
4217 lfsck_namespace_release_lmv(env, com);
4219 spin_lock(&lfsck->li_lock);
4221 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4223 ns->ln_status = LS_SCANNING_PHASE2;
4224 ns->ln_flags |= LF_SCANNED_ONCE;
4225 ns->ln_flags &= ~LF_UPGRADE;
4226 list_del_init(&com->lc_link_dir);
4227 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4228 } else if (result == 0) {
4229 if (lfsck->li_status != 0)
4230 ns->ln_status = lfsck->li_status;
4232 ns->ln_status = LS_STOPPED;
4233 if (ns->ln_status != LS_PAUSED) {
4234 list_del_init(&com->lc_link_dir);
4235 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4238 ns->ln_status = LS_FAILED;
4239 list_del_init(&com->lc_link_dir);
4240 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4242 spin_unlock(&lfsck->li_lock);
4245 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4246 HALF_SEC - lfsck->li_time_last_checkpoint);
4247 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4248 ns->ln_items_checked += com->lc_new_checked;
4249 com->lc_new_checked = 0;
4252 rc = lfsck_namespace_store(env, com, false);
4253 up_write(&com->lc_sem);
4255 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4256 lfsck_lfsck2name(lfsck), rc);
4262 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4265 struct lfsck_instance *lfsck = com->lc_lfsck;
4266 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4267 struct lfsck_namespace *ns = com->lc_file_ram;
4270 down_read(&com->lc_sem);
4271 seq_printf(m, "name: lfsck_namespace\n"
4277 lfsck_status2names(ns->ln_status));
4279 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4283 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4287 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4288 "time_since_last_completed");
4292 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4293 "time_since_latest_start");
4297 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4298 "time_since_last_checkpoint");
4302 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4303 "latest_start_position");
4307 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4308 "last_checkpoint_position");
4312 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4313 "first_failure_position");
4317 if (ns->ln_status == LS_SCANNING_PHASE1) {
4318 struct lfsck_position pos;
4319 const struct dt_it_ops *iops;
4320 cfs_duration_t duration = cfs_time_current() -
4321 lfsck->li_time_last_checkpoint;
4322 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4323 __u64 speed = checked;
4324 __u64 new_checked = com->lc_new_checked *
4325 msecs_to_jiffies(MSEC_PER_SEC);
4326 __u32 rtime = ns->ln_run_time_phase1 +
4327 cfs_duration_sec(duration + HALF_SEC);
4330 do_div(new_checked, duration);
4332 do_div(speed, rtime);
4333 lfsck_namespace_dump_statistics(m, ns, checked,
4334 ns->ln_objs_checked_phase2,
4335 rtime, ns->ln_run_time_phase2);
4337 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4338 "average_speed_phase2: N/A\n"
4339 "real_time_speed_phase1: "LPU64" items/sec\n"
4340 "real_time_speed_phase2: N/A\n",
4344 LASSERT(lfsck->li_di_oit != NULL);
4346 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4348 /* The low layer otable-based iteration position may NOT
4349 * exactly match the namespace-based directory traversal
4350 * cookie. Generally, it is not a serious issue. But the
4351 * caller should NOT make assumption on that. */
4352 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4353 if (!lfsck->li_current_oit_processed)
4354 pos.lp_oit_cookie--;
4356 spin_lock(&lfsck->li_lock);
4357 if (lfsck->li_di_dir != NULL) {
4358 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4359 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4360 fid_zero(&pos.lp_dir_parent);
4361 pos.lp_dir_cookie = 0;
4364 *lfsck_dto2fid(lfsck->li_obj_dir);
4367 fid_zero(&pos.lp_dir_parent);
4368 pos.lp_dir_cookie = 0;
4370 spin_unlock(&lfsck->li_lock);
4371 lfsck_pos_dump(m, &pos, "current_position");
4372 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4373 cfs_duration_t duration = cfs_time_current() -
4374 lfsck->li_time_last_checkpoint;
4375 __u64 checked = ns->ln_objs_checked_phase2 +
4376 com->lc_new_checked;
4377 __u64 speed1 = ns->ln_items_checked;
4378 __u64 speed2 = checked;
4379 __u64 new_checked = com->lc_new_checked *
4380 msecs_to_jiffies(MSEC_PER_SEC);
4381 __u32 rtime = ns->ln_run_time_phase2 +
4382 cfs_duration_sec(duration + HALF_SEC);
4385 do_div(new_checked, duration);
4386 if (ns->ln_run_time_phase1 != 0)
4387 do_div(speed1, ns->ln_run_time_phase1);
4389 do_div(speed2, rtime);
4390 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4392 ns->ln_run_time_phase1, rtime);
4394 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4395 "average_speed_phase2: "LPU64" objs/sec\n"
4396 "real_time_speed_phase1: N/A\n"
4397 "real_time_speed_phase2: "LPU64" objs/sec\n"
4398 "current_position: "DFID"\n",
4402 PFID(&ns->ln_fid_latest_scanned_phase2));
4404 __u64 speed1 = ns->ln_items_checked;
4405 __u64 speed2 = ns->ln_objs_checked_phase2;
4407 if (ns->ln_run_time_phase1 != 0)
4408 do_div(speed1, ns->ln_run_time_phase1);
4409 if (ns->ln_run_time_phase2 != 0)
4410 do_div(speed2, ns->ln_run_time_phase2);
4411 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4412 ns->ln_objs_checked_phase2,
4413 ns->ln_run_time_phase1,
4414 ns->ln_run_time_phase2);
4416 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4417 "average_speed_phase2: "LPU64" objs/sec\n"
4418 "real_time_speed_phase1: N/A\n"
4419 "real_time_speed_phase2: N/A\n"
4420 "current_position: N/A\n",
4425 up_read(&com->lc_sem);
4429 static int lfsck_namespace_double_scan(const struct lu_env *env,
4430 struct lfsck_component *com)
4432 struct lfsck_namespace *ns = com->lc_file_ram;
4433 struct lfsck_assistant_data *lad = com->lc_data;
4434 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4435 struct lfsck_tgt_desc *ltd;
4436 struct lfsck_tgt_desc *next;
4439 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4440 if (thread_is_stopped(&lad->lad_thread)) {
4441 LASSERT(list_empty(&lad->lad_req_list));
4442 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4444 spin_lock(<ds->ltd_lock);
4445 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4446 ltd_namespace_phase_list) {
4447 list_del_init(<d->ltd_namespace_phase_list);
4449 spin_unlock(<ds->ltd_lock);
4455 static void lfsck_namespace_data_release(const struct lu_env *env,
4456 struct lfsck_component *com)
4458 struct lfsck_assistant_data *lad = com->lc_data;
4459 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4460 struct lfsck_tgt_desc *ltd;
4461 struct lfsck_tgt_desc *next;
4463 LASSERT(lad != NULL);
4464 LASSERT(thread_is_init(&lad->lad_thread) ||
4465 thread_is_stopped(&lad->lad_thread));
4466 LASSERT(list_empty(&lad->lad_req_list));
4468 com->lc_data = NULL;
4469 lfsck_namespace_release_lmv(env, com);
4471 spin_lock(<ds->ltd_lock);
4472 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4473 ltd_namespace_phase_list) {
4474 list_del_init(<d->ltd_namespace_phase_list);
4476 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4477 ltd_namespace_phase_list) {
4478 list_del_init(<d->ltd_namespace_phase_list);
4480 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4481 ltd_namespace_list) {
4482 list_del_init(<d->ltd_namespace_list);
4484 spin_unlock(<ds->ltd_lock);
4486 if (likely(lad->lad_bitmap != NULL))
4487 CFS_FREE_BITMAP(lad->lad_bitmap);
4492 static void lfsck_namespace_quit(const struct lu_env *env,
4493 struct lfsck_component *com)
4495 struct lfsck_assistant_data *lad = com->lc_data;
4496 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4497 struct lfsck_tgt_desc *ltd;
4498 struct lfsck_tgt_desc *next;
4500 LASSERT(lad != NULL);
4502 lfsck_quit_generic(env, com);
4504 LASSERT(thread_is_init(&lad->lad_thread) ||
4505 thread_is_stopped(&lad->lad_thread));
4506 LASSERT(list_empty(&lad->lad_req_list));
4508 lfsck_namespace_release_lmv(env, com);
4510 spin_lock(<ds->ltd_lock);
4511 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4512 ltd_namespace_phase_list) {
4513 list_del_init(<d->ltd_namespace_phase_list);
4515 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4516 ltd_namespace_phase_list) {
4517 list_del_init(<d->ltd_namespace_phase_list);
4519 spin_unlock(<ds->ltd_lock);
4522 static int lfsck_namespace_in_notify(const struct lu_env *env,
4523 struct lfsck_component *com,
4524 struct lfsck_request *lr,
4527 struct lfsck_instance *lfsck = com->lc_lfsck;
4528 struct lfsck_namespace *ns = com->lc_file_ram;
4529 struct lfsck_assistant_data *lad = com->lc_data;
4530 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4531 struct lfsck_tgt_desc *ltd;
4536 switch (lr->lr_event) {
4537 case LE_CREATE_ORPHAN: {
4538 struct dt_object *orphan = NULL;
4539 struct lmv_mds_md_v1 *lmv;
4541 CDEBUG(D_LFSCK, "%s: namespace LFSCK handling notify from "
4542 "MDT %x to create orphan"DFID" with type %o\n",
4543 lfsck_lfsck2name(lfsck), lr->lr_index,
4544 PFID(&lr->lr_fid), lr->lr_type);
4546 orphan = lfsck_object_find(env, lfsck, &lr->lr_fid);
4548 GOTO(out_create, rc = PTR_ERR(orphan));
4550 if (dt_object_exists(orphan))
4551 GOTO(out_create, rc = -EEXIST);
4553 if (lr->lr_stripe_count > 0) {
4554 lmv = &lfsck_env_info(env)->lti_lmv;
4555 memset(lmv, 0, sizeof(*lmv));
4556 lmv->lmv_hash_type = lr->lr_hash_type;
4557 lmv->lmv_stripe_count = lr->lr_stripe_count;
4558 lmv->lmv_layout_version = lr->lr_layout_version;
4559 memcpy(lmv->lmv_pool_name, lr->lr_pool_name,
4560 sizeof(lmv->lmv_pool_name));
4565 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
4568 GOTO(out_create, rc = (rc == 1) ? 0 : rc);
4571 CDEBUG(D_LFSCK, "%s: namespace LFSCK handled notify from "
4572 "MDT %x to create orphan"DFID" with type %o: rc = %d\n",
4573 lfsck_lfsck2name(lfsck), lr->lr_index,
4574 PFID(&lr->lr_fid), lr->lr_type, rc);
4576 if (orphan != NULL && !IS_ERR(orphan))
4577 lfsck_object_put(env, orphan);
4581 case LE_SKIP_NLINK_DECLARE: {
4582 struct dt_object *obj = com->lc_obj;
4583 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4586 LASSERT(th != NULL);
4588 rc = dt_declare_delete(env, obj,
4589 (const struct dt_key *)key, th);
4591 rc = dt_declare_insert(env, obj,
4592 (const struct dt_rec *)&flags,
4593 (const struct dt_key *)key, th);
4597 case LE_SKIP_NLINK: {
4598 struct dt_object *obj = com->lc_obj;
4599 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4604 LASSERT(th != NULL);
4606 fid_cpu_to_be(key, &lr->lr_fid);
4607 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4608 (const struct dt_key *)key, BYPASS_CAPA);
4610 if (flags & LNTF_SKIP_NLINK)
4614 } else if (rc != -ENOENT) {
4618 flags |= LNTF_SKIP_NLINK;
4620 rc = dt_delete(env, obj, (const struct dt_key *)key,
4626 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4627 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4632 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4633 " to be skipped for namespace double scan: rc = %d\n",
4634 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4637 /* If we cannot record this object in the LFSCK tracing,
4638 * we have to mark the LFSC as LF_INCOMPLETE, then the
4639 * LFSCK will skip nlink attribute verification for
4641 ns->ln_flags |= LF_INCOMPLETE;
4645 case LE_SET_LMV_MASTER: {
4646 struct dt_object *obj;
4648 obj = lfsck_object_find_by_dev(env, lfsck->li_bottom,
4651 RETURN(PTR_ERR(obj));
4653 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4654 lfsck_object_put(env, obj);
4656 RETURN(rc > 0 ? 0 : rc);
4658 case LE_SET_LMV_SLAVE: {
4659 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4660 ns->ln_striped_shards_repaired++;
4662 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4663 LNTF_RECHECK_NAME_HASH, true);
4665 RETURN(rc > 0 ? 0 : rc);
4667 case LE_PHASE1_DONE:
4668 case LE_PHASE2_DONE:
4675 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4676 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4677 lr->lr_index, lr->lr_status, lr->lr_flags2);
4679 spin_lock(<ds->ltd_lock);
4680 ltd = LTD_TGT(ltds, lr->lr_index);
4682 spin_unlock(<ds->ltd_lock);
4687 list_del_init(<d->ltd_namespace_phase_list);
4688 switch (lr->lr_event) {
4689 case LE_PHASE1_DONE:
4690 if (lr->lr_status <= 0) {
4691 ltd->ltd_namespace_done = 1;
4692 list_del_init(<d->ltd_namespace_list);
4693 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4694 "phase1 for namespace LFSCK: rc = %d.\n",
4695 lfsck_lfsck2name(lfsck),
4696 ltd->ltd_index, lr->lr_status);
4697 ns->ln_flags |= LF_INCOMPLETE;
4702 if (lr->lr_flags2 & LF_INCOMPLETE)
4703 ns->ln_flags |= LF_INCOMPLETE;
4705 if (list_empty(<d->ltd_namespace_list))
4706 list_add_tail(<d->ltd_namespace_list,
4707 &lad->lad_mdt_list);
4708 list_add_tail(<d->ltd_namespace_phase_list,
4709 &lad->lad_mdt_phase2_list);
4711 case LE_PHASE2_DONE:
4712 ltd->ltd_namespace_done = 1;
4713 list_del_init(<d->ltd_namespace_list);
4717 ltd->ltd_namespace_done = 1;
4718 list_del_init(<d->ltd_namespace_list);
4719 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4721 "%s: the peer MDT %x exit namespace LFSCK\n",
4722 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4723 ns->ln_flags |= LF_INCOMPLETE;
4729 spin_unlock(<ds->ltd_lock);
4731 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4732 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4734 memset(stop, 0, sizeof(*stop));
4735 stop->ls_status = lr->lr_status;
4736 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4737 lfsck_stop(env, lfsck->li_bottom, stop);
4738 } else if (lfsck_phase2_next_ready(lad)) {
4739 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4745 static int lfsck_namespace_query(const struct lu_env *env,
4746 struct lfsck_component *com)
4748 struct lfsck_namespace *ns = com->lc_file_ram;
4750 return ns->ln_status;
4753 static struct lfsck_operations lfsck_namespace_ops = {
4754 .lfsck_reset = lfsck_namespace_reset,
4755 .lfsck_fail = lfsck_namespace_fail,
4756 .lfsck_close_dir = lfsck_namespace_close_dir,
4757 .lfsck_open_dir = lfsck_namespace_open_dir,
4758 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4759 .lfsck_prep = lfsck_namespace_prep,
4760 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4761 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4762 .lfsck_post = lfsck_namespace_post,
4763 .lfsck_dump = lfsck_namespace_dump,
4764 .lfsck_double_scan = lfsck_namespace_double_scan,
4765 .lfsck_data_release = lfsck_namespace_data_release,
4766 .lfsck_quit = lfsck_namespace_quit,
4767 .lfsck_in_notify = lfsck_namespace_in_notify,
4768 .lfsck_query = lfsck_namespace_query,
4772 * Repair dangling name entry.
4774 * For the name entry with dangling reference, we need to repare the
4775 * inconsistency according to the LFSCK sponsor's requirement:
4777 * 1) Keep the inconsistency there and report the inconsistency case,
4778 * then give the chance to the application to find related issues,
4779 * and the users can make the decision about how to handle it with
4780 * more human knownledge. (by default)
4782 * 2) Re-create the missing MDT-object with the FID information.
4784 * \param[in] env pointer to the thread context
4785 * \param[in] com pointer to the lfsck component
4786 * \param[in] child pointer to the object corresponding to the dangling
4788 * \param[in] lnr pointer to the namespace request that contains the
4789 * name's name, parent object, parent's LMV, and ect.
4791 * \retval positive number if no need to repair
4792 * \retval zero for repaired successfully
4793 * \retval negative error number on failure
4795 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4796 struct lfsck_component *com,
4797 struct dt_object *child,
4798 struct lfsck_namespace_req *lnr)
4800 struct lfsck_thread_info *info = lfsck_env_info(env);
4801 struct lu_attr *la = &info->lti_la;
4802 struct dt_allocation_hint *hint = &info->lti_hint;
4803 struct dt_object_format *dof = &info->lti_dof;
4804 struct dt_insert_rec *rec = &info->lti_dt_rec;
4805 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4806 struct dt_object *parent = lnr->lnr_obj;
4807 const struct lu_name *cname;
4808 struct linkea_data ldata = { 0 };
4809 struct lustre_handle lh = { 0 };
4810 struct lu_buf linkea_buf;
4811 struct lu_buf lmv_buf;
4812 struct lfsck_instance *lfsck = com->lc_lfsck;
4813 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4814 struct dt_device *dev = lfsck_obj2dt_dev(child);
4815 struct thandle *th = NULL;
4817 __u16 type = lnr->lnr_type;
4821 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4822 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4827 if (!create || bk->lb_param & LPF_DRYRUN)
4830 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4834 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
4838 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4839 MDS_INODELOCK_UPDATE, LCK_EX);
4843 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4847 th = dt_trans_create(env, dev);
4849 GOTO(log, rc = PTR_ERR(th));
4851 /* Set the ctime as zero, then others can know it is created for
4852 * repairing dangling name entry by LFSCK. And if the LFSCK made
4853 * wrong decision and the real MDT-object has been found later,
4854 * then the LFSCK has chance to fix the incosistency properly. */
4855 memset(la, 0, sizeof(*la));
4856 la->la_mode = (type & S_IFMT) | 0600;
4857 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4858 LA_ATIME | LA_MTIME | LA_CTIME;
4860 child->do_ops->do_ah_init(env, hint, parent, child,
4861 la->la_mode & S_IFMT);
4863 memset(dof, 0, sizeof(*dof));
4864 dof->dof_type = dt_mode_to_dft(type);
4865 /* If the target is a regular file, then the LFSCK will only create
4866 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4867 * OST-objects will be created when write open. */
4869 /* 1a. create child. */
4870 rc = dt_declare_create(env, child, la, hint, dof, th);
4874 if (S_ISDIR(type)) {
4875 if (unlikely(!dt_try_as_dir(env, child)))
4876 GOTO(stop, rc = -ENOTDIR);
4878 /* 2a. insert dot into child dir */
4879 rec->rec_type = S_IFDIR;
4880 rec->rec_fid = lfsck_dto2fid(child);
4881 rc = dt_declare_insert(env, child,
4882 (const struct dt_rec *)rec,
4883 (const struct dt_key *)dot, th);
4887 /* 3a. insert dotdot into child dir */
4888 rec->rec_fid = lfsck_dto2fid(parent);
4889 rc = dt_declare_insert(env, child,
4890 (const struct dt_rec *)rec,
4891 (const struct dt_key *)dotdot, th);
4895 /* 4a. increase child nlink */
4896 rc = dt_declare_ref_add(env, child, th);
4900 /* 5a. generate slave LMV EA. */
4901 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4904 idx = lfsck_shard_name_to_index(env,
4905 lnr->lnr_name, lnr->lnr_namelen,
4906 type, lfsck_dto2fid(child));
4907 if (unlikely(idx < 0))
4908 GOTO(stop, rc = idx);
4910 *lmv2 = lnr->lnr_lmv->ll_lmv;
4911 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4912 lmv2->lmv_master_mdt_index = idx;
4914 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4915 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4916 rc = dt_declare_xattr_set(env, child, &lmv_buf,
4917 XATTR_NAME_LMV, 0, th);
4923 /* 6a. insert linkEA for child */
4924 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4925 ldata.ld_leh->leh_len);
4926 rc = dt_declare_xattr_set(env, child, &linkea_buf,
4927 XATTR_NAME_LINK, 0, th);
4931 rc = dt_trans_start(env, dev, th);
4933 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4935 dt_write_lock(env, child, 0);
4936 /* 1b. create child */
4937 rc = dt_create(env, child, la, hint, dof, th);
4939 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4941 if (S_ISDIR(type)) {
4942 if (unlikely(!dt_try_as_dir(env, child)))
4943 GOTO(unlock, rc = -ENOTDIR);
4945 /* 2b. insert dot into child dir */
4946 rec->rec_type = S_IFDIR;
4947 rec->rec_fid = lfsck_dto2fid(child);
4948 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4949 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4953 /* 3b. insert dotdot into child dir */
4954 rec->rec_fid = lfsck_dto2fid(parent);
4955 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4956 (const struct dt_key *)dotdot, th,
4961 /* 4b. increase child nlink */
4962 rc = dt_ref_add(env, child, th);
4966 /* 5b. generate slave LMV EA. */
4967 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4968 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
4969 0, th, BYPASS_CAPA);
4975 /* 6b. insert linkEA for child. */
4976 rc = dt_xattr_set(env, child, &linkea_buf,
4977 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4982 dt_write_unlock(env, child);
4985 dt_trans_stop(env, dev, th);
4988 lfsck_ibits_unlock(&lh, LCK_EX);
4989 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4990 "reference for: parent "DFID", child "DFID", type %u, "
4991 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4992 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4993 type, cname->ln_name,
4994 create ? "Create the lost OST-object as required" :
4995 "Keep the MDT-object there by default", rc);
4998 struct lfsck_namespace *ns = com->lc_file_ram;
5000 ns->ln_flags |= LF_INCONSISTENT;
5006 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
5007 struct lfsck_component *com,
5008 struct lfsck_assistant_req *lar)
5010 struct lfsck_thread_info *info = lfsck_env_info(env);
5011 struct lu_attr *la = &info->lti_la;
5012 struct lfsck_instance *lfsck = com->lc_lfsck;
5013 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5014 struct lfsck_namespace *ns = com->lc_file_ram;
5015 struct linkea_data ldata = { 0 };
5016 const struct lu_name *cname;
5017 struct thandle *handle = NULL;
5018 struct lfsck_namespace_req *lnr =
5019 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5020 struct dt_object *dir = lnr->lnr_obj;
5021 struct dt_object *obj = NULL;
5022 const struct lu_fid *pfid = lfsck_dto2fid(dir);
5023 struct dt_device *dev = NULL;
5024 struct lustre_handle lh = { 0 };
5025 bool repaired = false;
5026 bool dtlocked = false;
5030 bool bad_hash = false;
5034 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5037 if (lnr->lnr_attr & LUDA_UPGRADE) {
5038 ns->ln_flags |= LF_UPGRADE;
5039 ns->ln_dirent_repaired++;
5041 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5042 ns->ln_flags |= LF_INCONSISTENT;
5043 ns->ln_dirent_repaired++;
5047 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5048 if (strcmp(lnr->lnr_name, dotdot) != 0)
5051 rc = lfsck_namespace_trace_update(env, com, pfid,
5052 LNTF_CHECK_PARENT, true);
5057 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5058 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5063 if (lnr->lnr_name[0] == '.' &&
5064 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
5067 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5068 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5073 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5075 GOTO(out, rc = idx);
5077 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
5078 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5081 dev = lfsck->li_next;
5083 struct lfsck_tgt_desc *ltd;
5085 /* Usually, some local filesystem consistency verification
5086 * tools can guarantee the local namespace tree consistenct.
5087 * So the LFSCK will only verify the remote directory. */
5088 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5089 rc = lfsck_namespace_trace_update(env, com, pfid,
5090 LNTF_CHECK_PARENT, true);
5095 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5096 if (unlikely(ltd == NULL)) {
5097 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5098 "did not join the namespace LFSCK\n",
5099 lfsck_lfsck2name(lfsck), idx);
5100 lfsck_lad_set_bitmap(env, com, idx);
5102 GOTO(out, rc = -ENODEV);
5108 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5110 GOTO(out, rc = PTR_ERR(obj));
5112 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5113 if (dt_object_exists(obj) == 0) {
5116 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5118 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5119 lnr->lnr_name, lnr->lnr_namelen)) {
5120 type = LNIT_BAD_DIRENT;
5125 type = LNIT_DANGLING;
5126 rc = lfsck_namespace_repair_dangling(env, com,
5135 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5138 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5139 MDS_INODELOCK_UPDATE |
5140 MDS_INODELOCK_XATTR, LCK_EX);
5144 handle = dt_trans_create(env, dev);
5146 GOTO(out, rc = PTR_ERR(handle));
5148 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5152 rc = dt_trans_start(env, dev, handle);
5156 dt_write_lock(env, obj, 0);
5160 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5164 rc = lfsck_links_read(env, obj, &ldata);
5165 if (unlikely(rc == -ENOENT)) {
5166 if (handle != NULL) {
5167 dt_write_unlock(env, obj);
5170 dt_trans_stop(env, dev, handle);
5173 lfsck_ibits_unlock(&lh, LCK_EX);
5176 /* It may happen when the remote object has been removed,
5177 * but the local MDT is not aware of that. */
5179 } else if (rc == 0) {
5180 count = ldata.ld_leh->leh_reccount;
5181 rc = linkea_links_find(&ldata, cname, pfid);
5183 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5184 if ((lfsck_object_type(obj) & S_IFMT) !=
5186 ns->ln_flags |= LF_INCONSISTENT;
5187 type = LNIT_BAD_TYPE;
5193 ns->ln_flags |= LF_INCONSISTENT;
5195 /* If the name entry hash does not match the slave striped
5196 * directory, and the name entry does not match also, then
5197 * it is quite possible that name entry is corrupted. */
5198 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5199 lnr->lnr_name, lnr->lnr_namelen)) {
5200 type = LNIT_BAD_DIRENT;
5205 /* If the file type stored in the name entry does not match
5206 * the file type claimed by the object, and the object does
5207 * not recognize the name entry, then it is quite possible
5208 * that the name entry is corrupted. */
5209 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5210 type = LNIT_BAD_DIRENT;
5215 /* For sub-dir object, we cannot make sure whether the sub-dir
5216 * back references the parent via ".." name entry correctly or
5217 * not in the LFSCK first-stage scanning. It may be that the
5218 * (remote) sub-dir ".." name entry has no parent FID after
5219 * file-level backup/restore and its linkEA may be wrong.
5220 * So under such case, we should replace the linkEA according
5221 * to current name entry. But this needs to be done during the
5222 * LFSCK second-stage scanning. The LFSCK will record the name
5223 * entry for further possible using. */
5227 } else if (unlikely(rc == -EINVAL)) {
5228 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5229 type = LNIT_BAD_TYPE;
5232 ns->ln_flags |= LF_INCONSISTENT;
5233 /* The magic crashed, we are not sure whether there are more
5234 * corrupt data in the linkea, so remove all linkea entries. */
5238 } else if (rc == -ENODATA) {
5239 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5240 type = LNIT_BAD_TYPE;
5243 ns->ln_flags |= LF_UPGRADE;
5248 if (bk->lb_param & LPF_DRYRUN) {
5249 ns->ln_linkea_repaired++;
5255 if (!lustre_handle_is_used(&lh))
5261 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5268 rc = linkea_data_new(&ldata,
5269 &lfsck_env_info(env)->lti_linkea_buf);
5274 rc = linkea_add_buf(&ldata, cname, pfid);
5278 rc = lfsck_links_write(env, obj, &ldata, handle);
5279 if (unlikely(rc == -ENOSPC) &&
5280 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5281 if (handle != NULL) {
5282 LASSERT(dt_write_locked(env, obj));
5284 dt_write_unlock(env, obj);
5287 dt_trans_stop(env, dev, handle);
5290 lfsck_ibits_unlock(&lh, LCK_EX);
5293 rc = lfsck_namespace_trace_update(env, com,
5294 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5296 /* If we cannot record this object in the
5297 * LFSCK tracing, we have to mark the LFSCK
5298 * as LF_INCOMPLETE, then the LFSCK will
5299 * skip nlink attribute verification for
5301 ns->ln_flags |= LF_INCOMPLETE;
5309 count = ldata.ld_leh->leh_reccount;
5310 if (!S_ISDIR(lfsck_object_type(obj)) ||
5311 !dt_object_remote(obj)) {
5312 ns->ln_linkea_repaired++;
5323 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
5327 if ((count == 1 && la->la_nlink == 1) ||
5328 S_ISDIR(lfsck_object_type(obj)))
5329 /* Usually, it is for single linked object or dir, do nothing.*/
5332 /* Following modification will be in another transaction. */
5333 if (handle != NULL) {
5334 dt_write_unlock(env, obj);
5337 dt_trans_stop(env, dev, handle);
5340 lfsck_ibits_unlock(&lh, LCK_EX);
5343 ns->ln_mul_linked_checked++;
5344 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
5345 LNTF_CHECK_LINKEA, true);
5351 dt_write_unlock(env, obj);
5353 if (handle != NULL && !IS_ERR(handle))
5354 dt_trans_stop(env, dev, handle);
5357 lfsck_ibits_unlock(&lh, LCK_EX);
5359 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5360 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5361 lnr->lnr_name, lnr->lnr_namelen) &&
5362 type != LNIT_BAD_DIRENT) {
5363 ns->ln_flags |= LF_INCONSISTENT;
5366 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5367 lnr->lnr_lmv, lnr->lnr_name);
5376 rc = lfsck_namespace_repair_dirent(env, com, dir,
5377 obj, lnr->lnr_name, lnr->lnr_name,
5378 lnr->lnr_type, true, false);
5382 case LNIT_BAD_DIRENT:
5384 /* XXX: This is a bad dirent, we do not know whether
5385 * the original name entry reference a regular
5386 * file or a directory, then keep the parent's
5387 * nlink count unchanged here. */
5388 rc = lfsck_namespace_repair_dirent(env, com, dir,
5389 obj, lnr->lnr_name, lnr->lnr_name,
5390 lnr->lnr_type, false, false);
5399 down_write(&com->lc_sem);
5401 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5402 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5403 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5404 PFID(lfsck_dto2fid(lnr->lnr_obj)),
5405 lnr->lnr_namelen, lnr->lnr_name, rc);
5407 lfsck_namespace_record_failure(env, lfsck, ns);
5408 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5409 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5410 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5411 dev != NULL && dev != lfsck->li_next)
5412 lfsck_lad_set_bitmap(env, com, idx);
5414 if (!(bk->lb_param & LPF_FAILOUT))
5418 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5419 "repaired the entry: "DFID", parent "DFID
5420 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5421 PFID(&lnr->lnr_fid),
5422 PFID(lfsck_dto2fid(lnr->lnr_obj)),
5423 lnr->lnr_namelen, lnr->lnr_name);
5426 ns->ln_items_repaired++;
5430 ns->ln_dangling_repaired++;
5433 ns->ln_bad_type_repaired++;
5435 case LNIT_BAD_DIRENT:
5436 ns->ln_dirent_repaired++;
5442 if (bk->lb_param & LPF_DRYRUN &&
5443 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5444 lfsck_pos_fill(env, lfsck,
5445 &ns->ln_pos_first_inconsistent,
5450 ns->ln_name_hash_repaired++;
5452 /* Not count repeatedly. */
5454 ns->ln_items_repaired++;
5456 if (bk->lb_param & LPF_DRYRUN &&
5457 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5458 lfsck_pos_fill(env, lfsck,
5459 &ns->ln_pos_first_inconsistent,
5466 up_write(&com->lc_sem);
5468 if (obj != NULL && !IS_ERR(obj))
5469 lfsck_object_put(env, obj);
5475 * Handle one orphan under the backend /lost+found directory
5477 * Insert the orphan FID into the namespace LFSCK trace file for further
5478 * processing (via the subsequent namespace LFSCK second-stage scanning).
5479 * At the same time, remove the orphan name entry from backend /lost+found
5480 * directory. There is an interval between the orphan name entry removed
5481 * from the backend /lost+found directory and the orphan FID in the LFSCK
5482 * trace file handled. In such interval, the LFSCK can be reset, then
5483 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5484 * To guarantee that the orphans can be found when LFSCK run next time
5485 * without e2fsck again, when remove the orphan name entry, the LFSCK
5486 * will set the orphan's ctime attribute as 1. Since normal applications
5487 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5488 * next time, it can record the object (that ctime is 1) in the namespace
5489 * LFSCK trace file during the first-stage scanning.
5491 * \param[in] env pointer to the thread context
5492 * \param[in] com pointer to the lfsck component
5493 * \param[in] parent pointer to the object for the backend /lost+found
5494 * \param[in] ent pointer to the name entry for the target under the
5495 * backend /lost+found
5497 * \retval positive for repaired
5498 * \retval 0 if needs to repair nothing
5499 * \retval negative error number on failure
5501 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5502 struct lfsck_component *com,
5503 struct dt_object *parent,
5504 struct lu_dirent *ent)
5506 struct lfsck_thread_info *info = lfsck_env_info(env);
5507 struct lu_fid *key = &info->lti_fid;
5508 struct lu_attr *la = &info->lti_la;
5509 struct lfsck_instance *lfsck = com->lc_lfsck;
5510 struct dt_object *obj = com->lc_obj;
5511 struct dt_device *dev = lfsck->li_bottom;
5512 struct dt_object *child = NULL;
5513 struct thandle *th = NULL;
5519 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5521 RETURN(PTR_ERR(child));
5523 LASSERT(dt_object_exists(child));
5524 LASSERT(!dt_object_remote(child));
5526 fid_cpu_to_be(key, &ent->lde_fid);
5527 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5528 (const struct dt_key *)key, BYPASS_CAPA);
5531 flags |= LNTF_CHECK_ORPHAN;
5532 } else if (rc == -ENOENT) {
5533 flags = LNTF_CHECK_ORPHAN;
5538 th = dt_trans_create(env, dev);
5540 GOTO(out, rc = PTR_ERR(th));
5542 /* a1. remove name entry from backend /lost+found */
5543 rc = dt_declare_delete(env, parent,
5544 (const struct dt_key *)ent->lde_name, th);
5548 if (S_ISDIR(lfsck_object_type(child))) {
5549 /* a2. decrease parent's nlink */
5550 rc = dt_declare_ref_del(env, parent, th);
5556 /* a3. remove child's FID from the LFSCK trace file. */
5557 rc = dt_declare_delete(env, obj,
5558 (const struct dt_key *)key, th);
5562 /* a4. set child's ctime as 1 */
5563 memset(la, 0, sizeof(*la));
5565 la->la_valid = LA_CTIME;
5566 rc = dt_declare_attr_set(env, child, la, th);
5571 /* a5. insert child's FID into the LFSCK trace file. */
5572 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5573 (const struct dt_key *)key, th);
5577 rc = dt_trans_start_local(env, dev, th);
5581 /* b1. remove name entry from backend /lost+found */
5582 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5587 if (S_ISDIR(lfsck_object_type(child))) {
5588 /* b2. decrease parent's nlink */
5589 dt_write_lock(env, parent, 0);
5590 rc = dt_ref_del(env, parent, th);
5591 dt_write_unlock(env, parent);
5597 /* a3. remove child's FID from the LFSCK trace file. */
5598 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5603 /* b4. set child's ctime as 1 */
5604 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5609 /* b5. insert child's FID into the LFSCK trace file. */
5610 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5611 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5613 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5616 dt_trans_stop(env, dev, th);
5619 lu_object_put(env, &child->do_lu);
5625 * Handle orphans under the backend /lost+found directory
5627 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5628 * and put them under the backend /lost+found directory that is invisible
5629 * to client. The LFSCK will scan such directory, for the original client
5630 * visible orphans, add their fids into the namespace LFSCK trace file,
5631 * then the subsenquent namespace LFSCK second-stage scanning can handle
5632 * them as other objects to be double scanned: either move back to normal
5633 * namespace, or to the global visible orphan directory:
5634 * /ROOT/.lustre/lost+found/MDTxxxx/
5636 * \param[in] env pointer to the thread context
5637 * \param[in] com pointer to the lfsck component
5639 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5640 struct lfsck_component *com)
5642 struct lfsck_thread_info *info = lfsck_env_info(env);
5643 struct lu_dirent *ent =
5644 (struct lu_dirent *)info->lti_key;
5645 struct lu_seq_range *range = &info->lti_range;
5646 struct lfsck_instance *lfsck = com->lc_lfsck;
5647 struct ptlrpc_thread *thread = &lfsck->li_thread;
5648 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5649 struct dt_device *dev = lfsck->li_bottom;
5650 struct lfsck_namespace *ns = com->lc_file_ram;
5651 struct dt_object *parent;
5652 const struct dt_it_ops *iops;
5654 struct seq_server_site *ss =
5655 lu_site2seq(dev->dd_lu_dev.ld_site);
5661 parent = lfsck_object_find_by_dev(env, dev, &LU_BACKEND_LPF_FID);
5662 if (IS_ERR(parent)) {
5663 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5664 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5668 /* It is normal that the /lost+found does not exist for ZFS backend. */
5669 if (!dt_object_exists(parent))
5672 if (unlikely(!dt_try_as_dir(env, parent)))
5673 GOTO(out, rc = -ENOTDIR);
5675 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5676 lfsck_lfsck2name(lfsck));
5678 com->lc_new_scanned = 0;
5679 iops = &parent->do_index_ops->dio_it;
5680 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5682 GOTO(out, rc = PTR_ERR(di));
5684 rc = iops->load(env, di, 0);
5686 rc = iops->next(env, di);
5691 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
5693 struct l_wait_info lwi;
5695 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
5697 l_wait_event(thread->t_ctl_waitq,
5698 !thread_is_running(thread),
5701 if (unlikely(!thread_is_running(thread)))
5705 rc = iops->rec(env, di, (struct dt_rec *)ent,
5706 LUDA_64BITHASH | LUDA_TYPE);
5708 rc = lfsck_unpack_ent(ent, &cookie, &type);
5710 if (unlikely(rc != 0)) {
5711 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5712 "/lost+found: rc = %d\n",
5713 lfsck_lfsck2name(lfsck), rc);
5718 /* skip dot and dotdot entries */
5719 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5722 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5725 if (fid_is_norm(&ent->lde_fid)) {
5726 fld_range_set_mdt(range);
5727 rc = fld_local_lookup(env, ss->ss_server_fld,
5728 fid_seq(&ent->lde_fid), range);
5731 } else if (lfsck_dev_idx(dev) != 0) {
5732 /* If the returned FID is IGIF, then there are three
5735 * 1) The object is upgraded from old Lustre-1.8 with
5736 * IGIF assigned to such object.
5737 * 2) The object is a backend local object and is
5738 * invisible to client.
5739 * 3) The object lost its LMV EA, and since there is
5740 * no FID-in-dirent for the orphan in the backend
5741 * /lost+found directory, then the low layer will
5742 * return IGIF for such object.
5744 * For MDTx (x != 0), it is either case 2) or case 3),
5745 * but from the LFSCK view, they are indistinguishable.
5746 * To be safe, the LFSCK will keep it there and report
5747 * some message, then the adminstrator can handle that
5750 * For MDT0, it is more possible the case 1). The LFSCK
5751 * will handle the orphan as an upgraded object. */
5752 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5753 "in the backend /lost+found on the MDT %04x, "
5754 "to be safe, skip it.\n",
5755 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5756 ent->lde_name, PFID(&ent->lde_fid),
5757 lfsck_dev_idx(dev));
5761 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5764 down_write(&com->lc_sem);
5765 com->lc_new_scanned++;
5766 ns->ln_local_lpf_scanned++;
5768 ns->ln_local_lpf_moved++;
5770 ns->ln_local_lpf_skipped++;
5772 ns->ln_local_lpf_failed++;
5773 up_write(&com->lc_sem);
5775 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5779 lfsck_control_speed_by_self(com);
5780 if (unlikely(!thread_is_running(thread))) {
5785 rc = iops->next(env, di);
5789 iops->fini(env, di);
5794 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5795 lfsck_lfsck2name(lfsck), rc);
5797 lu_object_put(env, &parent->do_lu);
5801 * Rescan the striped directory after the master LMV EA reset.
5803 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5804 * the namespace LFSCK engine scan the striped directory for the first time,
5805 * it will be regarded as a normal directory. As the LFSCK processing, some
5806 * other LFSCK instance on other MDT will find the shard of this striped dir,
5807 * and find that the master MDT-object of the striped directory lost its LMV
5808 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5809 * notify the LFSCK instance on this MDT to rescan the striped directory.
5811 * \param[in] env pointer to the thread context
5812 * \param[in] com pointer to the lfsck component
5813 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5816 * \retval positive number for success
5817 * \retval 0 for LFSCK stopped/paused
5818 * \retval negative error number on failure
5820 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5821 struct lfsck_component *com,
5822 struct lfsck_lmv_unit *llu)
5824 struct lfsck_thread_info *info = lfsck_env_info(env);
5825 struct lfsck_instance *lfsck = com->lc_lfsck;
5826 struct lfsck_assistant_data *lad = com->lc_data;
5827 struct dt_object *dir;
5828 const struct dt_it_ops *iops;
5830 struct lu_dirent *ent =
5831 (struct lu_dirent *)info->lti_key;
5832 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5833 struct ptlrpc_thread *thread = &lfsck->li_thread;
5834 struct lfsck_namespace_req *lnr;
5835 struct lfsck_assistant_req *lar;
5840 LASSERT(list_empty(&lad->lad_req_list));
5842 lfsck->li_lmv = &llu->llu_lmv;
5843 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5844 rc = lfsck_open_dir(env, lfsck, 0);
5848 dir = lfsck->li_obj_dir;
5849 di = lfsck->li_di_dir;
5850 iops = &dir->do_index_ops->dio_it;
5852 rc = iops->rec(env, di, (struct dt_rec *)ent,
5853 lfsck->li_args_dir);
5855 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5859 if (bk->lb_param & LPF_FAILOUT)
5865 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5868 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5870 if (bk->lb_param & LPF_FAILOUT)
5871 GOTO(out, rc = PTR_ERR(lnr));
5876 lar = &lnr->lnr_lar;
5877 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5878 lfsck_namespace_assistant_req_fini(env, lar);
5879 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5882 if (unlikely(!thread_is_running(thread)))
5886 rc = iops->next(env, di);
5890 lfsck_close_dir(env, lfsck, rc);
5894 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5895 if (list_empty(&lad->lad_req_list))
5898 spin_lock(&lad->lad_lock);
5899 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5901 list_del_init(&lar->lar_list);
5902 spin_unlock(&lad->lad_lock);
5904 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5905 lfsck_namespace_assistant_req_fini(env, lar);
5907 RETURN(rc == 0 ? 1 : rc);
5910 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5911 struct lfsck_component *com)
5913 struct lfsck_instance *lfsck = com->lc_lfsck;
5914 struct ptlrpc_thread *thread = &lfsck->li_thread;
5915 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5916 struct lfsck_namespace *ns = com->lc_file_ram;
5917 struct dt_object *obj = com->lc_obj;
5918 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5919 struct dt_object *target;
5927 while (!list_empty(&lfsck->li_list_lmv)) {
5928 struct lfsck_lmv_unit *llu;
5930 spin_lock(&lfsck->li_lock);
5931 llu = list_entry(lfsck->li_list_lmv.next,
5932 struct lfsck_lmv_unit, llu_link);
5933 list_del_init(&llu->llu_link);
5934 spin_unlock(&lfsck->li_lock);
5936 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
5941 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
5942 lfsck_lfsck2name(lfsck));
5944 lfsck_namespace_scan_local_lpf(env, com);
5946 com->lc_new_checked = 0;
5947 com->lc_new_scanned = 0;
5948 com->lc_time_last_checkpoint = cfs_time_current();
5949 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5950 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5952 di = iops->init(env, obj, 0, BYPASS_CAPA);
5954 RETURN(PTR_ERR(di));
5956 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5957 rc = iops->get(env, di, (const struct dt_key *)&fid);
5961 /* Skip the start one, which either has been processed or non-exist. */
5962 rc = iops->next(env, di);
5967 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
5969 struct l_wait_info lwi;
5971 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
5973 l_wait_event(thread->t_ctl_waitq,
5974 !thread_is_running(thread),
5977 if (unlikely(!thread_is_running(thread)))
5981 key = iops->key(env, di);
5982 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5983 if (!fid_is_sane(&fid)) {
5988 target = lfsck_object_find_by_dev(env, lfsck->li_bottom, &fid);
5989 if (IS_ERR(target)) {
5990 rc = PTR_ERR(target);
5994 if (dt_object_exists(target)) {
5995 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5997 rc = lfsck_namespace_double_scan_one(env, com,
6004 lfsck_object_put(env, target);
6007 down_write(&com->lc_sem);
6008 com->lc_new_checked++;
6009 com->lc_new_scanned++;
6010 ns->ln_fid_latest_scanned_phase2 = fid;
6012 ns->ln_objs_repaired_phase2++;
6014 ns->ln_objs_failed_phase2++;
6015 up_write(&com->lc_sem);
6017 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6020 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
6021 cfs_time_current())) &&
6022 com->lc_new_checked != 0) {
6023 down_write(&com->lc_sem);
6024 ns->ln_run_time_phase2 +=
6025 cfs_duration_sec(cfs_time_current() +
6026 HALF_SEC - com->lc_time_last_checkpoint);
6027 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6028 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6029 com->lc_new_checked = 0;
6030 rc = lfsck_namespace_store(env, com, false);
6031 up_write(&com->lc_sem);
6035 com->lc_time_last_checkpoint = cfs_time_current();
6036 com->lc_time_next_checkpoint =
6037 com->lc_time_last_checkpoint +
6038 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6041 lfsck_control_speed_by_self(com);
6042 if (unlikely(!thread_is_running(thread)))
6045 rc = iops->next(env, di);
6054 iops->fini(env, di);
6056 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
6057 lfsck_lfsck2name(lfsck), rc);
6062 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6063 struct lfsck_component *com,
6064 struct lfsck_position *pos)
6066 struct lfsck_assistant_data *lad = com->lc_data;
6067 struct lfsck_namespace_req *lnr;
6069 if (list_empty(&lad->lad_req_list))
6072 lnr = list_entry(lad->lad_req_list.next,
6073 struct lfsck_namespace_req,
6075 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6076 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6077 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
6080 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6081 struct lfsck_component *com,
6084 struct lfsck_instance *lfsck = com->lc_lfsck;
6085 struct lfsck_namespace *ns = com->lc_file_ram;
6087 down_write(&com->lc_sem);
6088 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6089 HALF_SEC - lfsck->li_time_last_checkpoint);
6090 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6091 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6092 com->lc_new_checked = 0;
6095 if (ns->ln_flags & LF_INCOMPLETE)
6096 ns->ln_status = LS_PARTIAL;
6098 ns->ln_status = LS_COMPLETED;
6099 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6100 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6101 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6102 ns->ln_success_count++;
6103 } else if (rc == 0) {
6104 if (lfsck->li_status != 0)
6105 ns->ln_status = lfsck->li_status;
6107 ns->ln_status = LS_STOPPED;
6109 ns->ln_status = LS_FAILED;
6112 rc = lfsck_namespace_store(env, com, false);
6113 up_write(&com->lc_sem);
6119 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6120 struct ptlrpc_request *req,
6127 * Notify remote LFSCK instances about former failures.
6129 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6130 * some LFSCK verification requests (maybe because of network issues or the MDT
6131 * itself trouble). During the respond gap the MDT may missed some name entries
6132 * verification, then the MDT cannot know whether related MDT-objects have been
6133 * referenced by related name entries or not, then in the second-stage scanning,
6134 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6135 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6136 * name entry for repairing the orphan.
6138 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6139 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6140 * have ever missed some name entries verification and should skip the handling
6141 * for orphan MDT-objects.
6143 * \param[in] env pointer to the thread context
6144 * \param[in] com pointer to the lfsck component
6145 * \param[in] lr pointer to the lfsck request
6147 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6148 struct lfsck_component *com,
6149 struct lfsck_request *lr)
6151 struct lfsck_async_interpret_args *laia =
6152 &lfsck_env_info(env)->lti_laia2;
6153 struct lfsck_assistant_data *lad = com->lc_data;
6154 struct lfsck_namespace *ns = com->lc_file_ram;
6155 struct lfsck_instance *lfsck = com->lc_lfsck;
6156 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6157 struct lfsck_tgt_desc *ltd;
6158 struct ptlrpc_request_set *set;
6162 set = ptlrpc_prep_set();
6164 GOTO(out, rc = -ENOMEM);
6166 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6167 memset(laia, 0, sizeof(*laia));
6168 lad->lad_touch_gen++;
6170 spin_lock(<ds->ltd_lock);
6171 while (!list_empty(&lad->lad_mdt_list)) {
6172 ltd = list_entry(lad->lad_mdt_list.next,
6173 struct lfsck_tgt_desc,
6174 ltd_namespace_list);
6175 if (ltd->ltd_namespace_gen == lad->lad_touch_gen)
6178 ltd->ltd_namespace_gen = lad->lad_touch_gen;
6179 list_move_tail(<d->ltd_namespace_list,
6180 &lad->lad_mdt_list);
6181 if (!lad->lad_incomplete ||
6182 !cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
6183 ltd->ltd_namespace_failed = 0;
6187 ltd->ltd_namespace_failed = 1;
6188 spin_unlock(<ds->ltd_lock);
6189 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6190 lfsck_namespace_assistant_sync_failures_interpret,
6191 laia, LFSCK_NOTIFY);
6193 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6194 "to sync failure with MDT %x: rc = %d\n",
6195 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6197 spin_lock(<ds->ltd_lock);
6199 spin_unlock(<ds->ltd_lock);
6201 rc = ptlrpc_set_wait(set);
6202 ptlrpc_set_destroy(set);
6208 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6209 "to sync failure with MDTs, and related MDTs "
6210 "may handle orphan improperly: rc = %d\n",
6211 lfsck_lfsck2name(lfsck), rc);
6216 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6217 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6218 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6219 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6220 .la_double_scan_result = lfsck_namespace_double_scan_result,
6221 .la_req_fini = lfsck_namespace_assistant_req_fini,
6222 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6226 * Verify the specified linkEA entry for the given directory object.
6227 * If the object has no such linkEA entry or it has more other linkEA
6228 * entries, then re-generate the linkEA with the given information.
6230 * \param[in] env pointer to the thread context
6231 * \param[in] dev pointer to the dt_device
6232 * \param[in] obj pointer to the dt_object to be handled
6233 * \param[in] cname the name for the child in the parent directory
6234 * \param[in] pfid the parent directory's FID for the linkEA
6236 * \retval 0 for success
6237 * \retval negative error number on failure
6239 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
6240 struct dt_object *obj, const struct lu_name *cname,
6241 const struct lu_fid *pfid)
6243 struct linkea_data ldata = { 0 };
6244 struct lu_buf linkea_buf;
6247 int fl = LU_XATTR_CREATE;
6251 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6253 rc = lfsck_links_read(env, obj, &ldata);
6254 if (rc == -ENODATA) {
6256 } else if (rc == 0) {
6257 fl = LU_XATTR_REPLACE;
6258 if (ldata.ld_leh->leh_reccount != 1) {
6261 rc = linkea_links_find(&ldata, cname, pfid);
6270 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6274 rc = linkea_add_buf(&ldata, cname, pfid);
6278 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6279 ldata.ld_leh->leh_len);
6280 th = dt_trans_create(env, dev);
6282 RETURN(PTR_ERR(th));
6284 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6285 XATTR_NAME_LINK, fl, th);
6289 rc = dt_trans_start_local(env, dev, th);
6293 dt_write_lock(env, obj, 0);
6294 rc = dt_xattr_set(env, obj, &linkea_buf,
6295 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6296 dt_write_unlock(env, obj);
6301 dt_trans_stop(env, dev, th);
6306 * Get the name and parent directory's FID from the first linkEA entry.
6308 * \param[in] env pointer to the thread context
6309 * \param[in] obj pointer to the object which get linkEA from
6310 * \param[out] name pointer to the buffer to hold the name
6311 * in the first linkEA entry
6312 * \param[out] pfid pointer to the buffer to hold the parent
6313 * directory's FID in the first linkEA entry
6315 * \retval 0 for success
6316 * \retval negative error number on failure
6318 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6319 char *name, struct lu_fid *pfid)
6321 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6322 struct linkea_data ldata = { 0 };
6325 rc = lfsck_links_read(env, obj, &ldata);
6329 linkea_first_entry(&ldata);
6330 if (ldata.ld_lee == NULL)
6333 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6334 /* To guarantee the 'name' is terminated with '0'. */
6335 memcpy(name, cname->ln_name, cname->ln_namelen);
6336 name[cname->ln_namelen] = 0;
6342 * Remove the name entry from the parent directory.
6344 * No need to care about the object referenced by the name entry,
6345 * either the name entry is invalid or redundant, or the referenced
6346 * object has been processed has been or will be handled by others.
6348 * \param[in] env pointer to the thread context
6349 * \param[in] lfsck pointer to the lfsck instance
6350 * \param[in] parent pointer to the lost+found object
6351 * \param[in] name the name for the name entry to be removed
6352 * \param[in] type the type for the name entry to be removed
6354 * \retval 0 for success
6355 * \retval negative error number on failure
6357 int lfsck_remove_name_entry(const struct lu_env *env,
6358 struct lfsck_instance *lfsck,
6359 struct dt_object *parent,
6360 const char *name, __u32 type)
6362 struct dt_device *dev = lfsck->li_next;
6364 struct lustre_handle lh = { 0 };
6368 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6369 MDS_INODELOCK_UPDATE, LCK_EX);
6373 th = dt_trans_create(env, dev);
6375 GOTO(unlock, rc = PTR_ERR(th));
6377 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6381 if (S_ISDIR(type)) {
6382 rc = dt_declare_ref_del(env, parent, th);
6387 rc = dt_trans_start(env, dev, th);
6391 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6396 if (S_ISDIR(type)) {
6397 dt_write_lock(env, parent, 0);
6398 rc = dt_ref_del(env, parent, th);
6399 dt_write_unlock(env, parent);
6405 dt_trans_stop(env, dev, th);
6408 lfsck_ibits_unlock(&lh, LCK_EX);
6410 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
6411 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6412 PFID(lfsck_dto2fid(parent)), name, type, rc);
6418 * Update the object's name entry with the given FID.
6420 * \param[in] env pointer to the thread context
6421 * \param[in] lfsck pointer to the lfsck instance
6422 * \param[in] parent pointer to the parent directory that holds
6424 * \param[in] name the name for the entry to be updated
6425 * \param[in] pfid the new PFID for the name entry
6426 * \param[in] type the type for the name entry to be updated
6428 * \retval 0 for success
6429 * \retval negative error number on failure
6431 int lfsck_update_name_entry(const struct lu_env *env,
6432 struct lfsck_instance *lfsck,
6433 struct dt_object *parent, const char *name,
6434 const struct lu_fid *pfid, __u32 type)
6436 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
6437 struct dt_device *dev = lfsck->li_next;
6438 struct lustre_handle lh = { 0 };
6444 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6445 MDS_INODELOCK_UPDATE, LCK_EX);
6449 th = dt_trans_create(env, dev);
6451 GOTO(unlock, rc = PTR_ERR(th));
6453 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6457 rec->rec_type = type;
6458 rec->rec_fid = pfid;
6459 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
6460 (const struct dt_key *)name, th);
6464 rc = dt_declare_ref_add(env, parent, th);
6468 rc = dt_trans_start(env, dev, th);
6472 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6474 if (rc == -ENOENT) {
6482 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
6483 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6484 if (rc == 0 && S_ISDIR(type) && !exists) {
6485 dt_write_lock(env, parent, 0);
6486 rc = dt_ref_add(env, parent, th);
6487 dt_write_unlock(env, parent);
6493 dt_trans_stop(env, dev, th);
6496 lfsck_ibits_unlock(&lh, LCK_EX);
6498 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6499 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6500 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
6505 int lfsck_namespace_setup(const struct lu_env *env,
6506 struct lfsck_instance *lfsck)
6508 struct lfsck_component *com;
6509 struct lfsck_namespace *ns;
6510 struct dt_object *root = NULL;
6511 struct dt_object *obj;
6515 LASSERT(lfsck->li_master);
6521 INIT_LIST_HEAD(&com->lc_link);
6522 INIT_LIST_HEAD(&com->lc_link_dir);
6523 init_rwsem(&com->lc_sem);
6524 atomic_set(&com->lc_ref, 1);
6525 com->lc_lfsck = lfsck;
6526 com->lc_type = LFSCK_TYPE_NAMESPACE;
6527 com->lc_ops = &lfsck_namespace_ops;
6528 com->lc_data = lfsck_assistant_data_init(
6529 &lfsck_namespace_assistant_ops,
6531 if (com->lc_data == NULL)
6532 GOTO(out, rc = -ENOMEM);
6534 com->lc_file_size = sizeof(struct lfsck_namespace);
6535 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6536 if (com->lc_file_ram == NULL)
6537 GOTO(out, rc = -ENOMEM);
6539 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6540 if (com->lc_file_disk == NULL)
6541 GOTO(out, rc = -ENOMEM);
6543 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6545 GOTO(out, rc = PTR_ERR(root));
6547 if (unlikely(!dt_try_as_dir(env, root)))
6548 GOTO(out, rc = -ENOTDIR);
6550 obj = local_index_find_or_create(env, lfsck->li_los, root,
6551 lfsck_namespace_name,
6552 S_IFREG | S_IRUGO | S_IWUSR,
6553 &dt_lfsck_features);
6555 GOTO(out, rc = PTR_ERR(obj));
6558 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
6562 rc = lfsck_namespace_load(env, com);
6564 rc = lfsck_namespace_init(env, com);
6566 rc = lfsck_namespace_reset(env, com, true);
6570 ns = com->lc_file_ram;
6571 switch (ns->ln_status) {
6576 spin_lock(&lfsck->li_lock);
6577 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6578 spin_unlock(&lfsck->li_lock);
6581 CERROR("%s: unknown lfsck_namespace status %d\n",
6582 lfsck_lfsck2name(lfsck), ns->ln_status);
6584 case LS_SCANNING_PHASE1:
6585 case LS_SCANNING_PHASE2:
6586 /* No need to store the status to disk right now.
6587 * If the system crashed before the status stored,
6588 * it will be loaded back when next time. */
6589 ns->ln_status = LS_CRASHED;
6593 spin_lock(&lfsck->li_lock);
6594 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6595 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6596 spin_unlock(&lfsck->li_lock);
6603 if (root != NULL && !IS_ERR(root))
6604 lu_object_put(env, &root->do_lu);
6606 lfsck_component_cleanup(env, com);
6607 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6608 lfsck_lfsck2name(lfsck), rc);