4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2012, 2013, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
42 #include "lfsck_internal.h"
44 #define LFSCK_NAMESPACE_MAGIC 0xA0629D03
46 enum lfsck_nameentry_check {
47 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
48 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
49 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
52 static const char lfsck_namespace_name[] = "lfsck_namespace";
54 static struct lfsck_namespace_req *
55 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
56 struct lu_dirent *ent, __u16 type)
58 struct lfsck_namespace_req *lnr;
61 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
64 return ERR_PTR(-ENOMEM);
66 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
67 lnr->lnr_obj = lfsck_object_get(lfsck->li_obj_dir);
68 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
69 lnr->lnr_fid = ent->lde_fid;
70 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
71 lnr->lnr_dir_cookie = ent->lde_hash;
72 lnr->lnr_attr = ent->lde_attrs;
75 lnr->lnr_namelen = ent->lde_namelen;
76 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
81 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
82 struct lfsck_assistant_req *lar)
84 struct lfsck_namespace_req *lnr =
85 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
87 if (lnr->lnr_lmv != NULL)
88 lfsck_lmv_put(env, lnr->lnr_lmv);
90 lu_object_put(env, &lnr->lnr_obj->do_lu);
91 OBD_FREE(lnr, lnr->lnr_size);
94 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
95 struct lfsck_namespace *src)
97 dst->ln_magic = le32_to_cpu(src->ln_magic);
98 dst->ln_status = le32_to_cpu(src->ln_status);
99 dst->ln_flags = le32_to_cpu(src->ln_flags);
100 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
101 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
102 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
103 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
104 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
105 dst->ln_time_last_checkpoint =
106 le64_to_cpu(src->ln_time_last_checkpoint);
107 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
108 &src->ln_pos_latest_start);
109 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
110 &src->ln_pos_last_checkpoint);
111 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
112 &src->ln_pos_first_inconsistent);
113 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
114 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
115 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
116 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
117 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
118 dst->ln_objs_repaired_phase2 =
119 le64_to_cpu(src->ln_objs_repaired_phase2);
120 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
121 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
122 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
123 &src->ln_fid_latest_scanned_phase2);
124 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
125 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
126 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
127 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
128 dst->ln_unknown_inconsistency =
129 le64_to_cpu(src->ln_unknown_inconsistency);
130 dst->ln_unmatched_pairs_repaired =
131 le64_to_cpu(src->ln_unmatched_pairs_repaired);
132 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
133 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
134 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
135 dst->ln_lost_dirent_repaired =
136 le64_to_cpu(src->ln_lost_dirent_repaired);
137 dst->ln_striped_dirs_scanned =
138 le64_to_cpu(src->ln_striped_dirs_scanned);
139 dst->ln_striped_dirs_repaired =
140 le64_to_cpu(src->ln_striped_dirs_repaired);
141 dst->ln_striped_dirs_failed =
142 le64_to_cpu(src->ln_striped_dirs_failed);
143 dst->ln_striped_dirs_disabled =
144 le64_to_cpu(src->ln_striped_dirs_disabled);
145 dst->ln_striped_dirs_skipped =
146 le64_to_cpu(src->ln_striped_dirs_skipped);
147 dst->ln_striped_shards_scanned =
148 le64_to_cpu(src->ln_striped_shards_scanned);
149 dst->ln_striped_shards_repaired =
150 le64_to_cpu(src->ln_striped_shards_repaired);
151 dst->ln_striped_shards_failed =
152 le64_to_cpu(src->ln_striped_shards_failed);
153 dst->ln_striped_shards_skipped =
154 le64_to_cpu(src->ln_striped_shards_skipped);
155 dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
156 dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
157 dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
158 dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
159 dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
160 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
163 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
164 struct lfsck_namespace *src)
166 dst->ln_magic = cpu_to_le32(src->ln_magic);
167 dst->ln_status = cpu_to_le32(src->ln_status);
168 dst->ln_flags = cpu_to_le32(src->ln_flags);
169 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
170 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
171 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
172 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
173 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
174 dst->ln_time_last_checkpoint =
175 cpu_to_le64(src->ln_time_last_checkpoint);
176 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
177 &src->ln_pos_latest_start);
178 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
179 &src->ln_pos_last_checkpoint);
180 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
181 &src->ln_pos_first_inconsistent);
182 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
183 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
184 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
185 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
186 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
187 dst->ln_objs_repaired_phase2 =
188 cpu_to_le64(src->ln_objs_repaired_phase2);
189 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
190 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
191 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
192 &src->ln_fid_latest_scanned_phase2);
193 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
194 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
195 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
196 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
197 dst->ln_unknown_inconsistency =
198 cpu_to_le64(src->ln_unknown_inconsistency);
199 dst->ln_unmatched_pairs_repaired =
200 cpu_to_le64(src->ln_unmatched_pairs_repaired);
201 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
202 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
203 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
204 dst->ln_lost_dirent_repaired =
205 cpu_to_le64(src->ln_lost_dirent_repaired);
206 dst->ln_striped_dirs_scanned =
207 cpu_to_le64(src->ln_striped_dirs_scanned);
208 dst->ln_striped_dirs_repaired =
209 cpu_to_le64(src->ln_striped_dirs_repaired);
210 dst->ln_striped_dirs_failed =
211 cpu_to_le64(src->ln_striped_dirs_failed);
212 dst->ln_striped_dirs_disabled =
213 cpu_to_le64(src->ln_striped_dirs_disabled);
214 dst->ln_striped_dirs_skipped =
215 cpu_to_le64(src->ln_striped_dirs_skipped);
216 dst->ln_striped_shards_scanned =
217 cpu_to_le64(src->ln_striped_shards_scanned);
218 dst->ln_striped_shards_repaired =
219 cpu_to_le64(src->ln_striped_shards_repaired);
220 dst->ln_striped_shards_failed =
221 cpu_to_le64(src->ln_striped_shards_failed);
222 dst->ln_striped_shards_skipped =
223 cpu_to_le64(src->ln_striped_shards_skipped);
224 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
225 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
226 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
227 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
228 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
229 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
232 static void lfsck_namespace_record_failure(const struct lu_env *env,
233 struct lfsck_instance *lfsck,
234 struct lfsck_namespace *ns)
236 struct lfsck_position pos;
238 ns->ln_items_failed++;
239 lfsck_pos_fill(env, lfsck, &pos, false);
240 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
241 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
242 ns->ln_pos_first_inconsistent = pos;
244 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
245 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
246 lfsck_lfsck2name(lfsck),
247 ns->ln_pos_first_inconsistent.lp_oit_cookie,
248 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
249 ns->ln_pos_first_inconsistent.lp_dir_cookie);
254 * Load the MDT bitmap from the lfsck_namespace trace file.
256 * \param[in] env pointer to the thread context
257 * \param[in] com pointer to the lfsck component
259 * \retval 0 for success
260 * \retval negative error number on failure or data corruption
262 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
263 struct lfsck_component *com)
265 struct dt_object *obj = com->lc_obj;
266 struct lfsck_assistant_data *lad = com->lc_data;
267 struct lfsck_namespace *ns = com->lc_file_ram;
268 cfs_bitmap_t *bitmap = lad->lad_bitmap;
274 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
276 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
278 nbits = ns->ln_bitmap_size;
280 if (unlikely(nbits < BITS_PER_LONG))
281 nbits = BITS_PER_LONG;
283 if (nbits > bitmap->size) {
284 __u32 new_bits = bitmap->size;
285 cfs_bitmap_t *new_bitmap;
287 while (new_bits < nbits)
290 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
291 if (new_bitmap == NULL)
294 lad->lad_bitmap = new_bitmap;
295 CFS_FREE_BITMAP(bitmap);
299 if (ns->ln_bitmap_size == 0) {
300 lad->lad_incomplete = 0;
301 CFS_RESET_BITMAP(bitmap);
306 size = (ns->ln_bitmap_size + 7) >> 3;
307 rc = dt_xattr_get(env, obj,
308 lfsck_buf_get(env, bitmap->data, size),
309 XATTR_NAME_LFSCK_BITMAP, BYPASS_CAPA);
311 RETURN(rc >= 0 ? -EINVAL : rc);
313 if (cfs_bitmap_check_empty(bitmap))
314 lad->lad_incomplete = 0;
316 lad->lad_incomplete = 1;
322 * 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);
1003 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
1005 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1008 dt_write_unlock(env, orphan);
1011 dt_trans_stop(env, dev, th);
1014 lfsck_ibits_unlock(&clh, LCK_EX);
1015 lfsck_ibits_unlock(&plh, LCK_EX);
1016 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1017 "object "DFID", name = %s: rc = %d\n",
1018 lfsck_lfsck2name(lfsck), PFID(cfid),
1019 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1022 struct lfsck_namespace *ns = com->lc_file_ram;
1024 ns->ln_flags |= LF_INCONSISTENT;
1031 * Add the specified name entry back to namespace.
1033 * If there is a linkEA entry that back references a name entry under
1034 * some parent directory, but such parent directory does not have the
1035 * claimed name entry. On the other hand, the linkEA entries count is
1036 * not larger than the MDT-object's hard link count. Under such case,
1037 * it is quite possible that the name entry is lost. Then the LFSCK
1038 * should add the name entry back to the namespace.
1040 * \param[in] env pointer to the thread context
1041 * \param[in] com pointer to the lfsck component
1042 * \param[in] parent pointer to the directory under which the name entry
1043 * will be inserted into
1044 * \param[in] child pointer to the object referenced by the name entry
1045 * that to be inserted into the parent
1046 * \param[in] name the name for the child in the parent directory
1048 * \retval positive number for repaired cases
1049 * \retval 0 if nothing to be repaired
1050 * \retval negative error number on failure
1052 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1053 struct lfsck_component *com,
1054 struct dt_object *parent,
1055 struct dt_object *child,
1058 struct lfsck_thread_info *info = lfsck_env_info(env);
1059 struct lu_attr *la = &info->lti_la;
1060 struct dt_insert_rec *rec = &info->lti_dt_rec;
1061 struct lfsck_instance *lfsck = com->lc_lfsck;
1062 struct dt_device *dev = lfsck->li_next;
1063 struct thandle *th = NULL;
1064 struct lustre_handle lh = { 0 };
1068 if (unlikely(!dt_try_as_dir(env, parent)))
1069 GOTO(log, rc = -ENOTDIR);
1071 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1074 /* Hold update lock on the parent to prevent others to access. */
1075 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1076 MDS_INODELOCK_UPDATE, LCK_EX);
1080 th = dt_trans_create(env, dev);
1082 GOTO(unlock, rc = PTR_ERR(th));
1084 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1085 rec->rec_fid = lfsck_dto2fid(child);
1086 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1087 (const struct dt_key *)name, th);
1091 if (S_ISDIR(rec->rec_type)) {
1092 rc = dt_declare_ref_add(env, parent, th);
1097 memset(la, 0, sizeof(*la));
1098 la->la_ctime = cfs_time_current_sec();
1099 la->la_valid = LA_CTIME;
1100 rc = dt_declare_attr_set(env, parent, la, th);
1104 rc = dt_declare_attr_set(env, child, la, th);
1108 rc = dt_trans_start_local(env, dev, th);
1112 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1113 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1117 if (S_ISDIR(rec->rec_type)) {
1118 dt_write_lock(env, parent, 0);
1119 rc = dt_ref_add(env, parent, th);
1120 dt_write_unlock(env, parent);
1125 la->la_ctime = cfs_time_current_sec();
1126 rc = dt_attr_set(env, parent, la, th, BYPASS_CAPA);
1130 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
1132 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1135 dt_trans_stop(env, dev, th);
1138 lfsck_ibits_unlock(&lh, LCK_EX);
1141 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1142 "the name %s and type %o to the parent "DFID": rc = %d\n",
1143 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1144 lfsck_object_type(child) & S_IFMT,
1145 PFID(lfsck_dto2fid(parent)), rc);
1148 struct lfsck_namespace *ns = com->lc_file_ram;
1150 ns->ln_flags |= LF_INCONSISTENT;
1152 ns->ln_lost_dirent_repaired++;
1159 * Create the specified orphan MDT-object on remote MDT.
1161 * The LFSCK instance on this MDT will send LFSCK RPC to remote MDT to
1162 * ask the remote LFSCK instance to create the specified orphan object
1163 * under .lustre/lost+found/MDTxxxx/ directory with the name:
1164 * ${FID}-P-${conflict_version}.
1166 * \param[in] env pointer to the thread context
1167 * \param[in] com pointer to the lfsck component
1168 * \param[in] orphan pointer to the orphan MDT-object
1169 * \param[in] type the orphan's type to be created
1171 * type "P": The orphan object to be created was a parent directory
1172 * of some MDT-object which linkEA shows that the @orphan
1173 * object is missing.
1175 * \see lfsck_layout_recreate_parent() for more types.
1177 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1179 * \retval positive number for repaired cases
1180 * \retval 0 if needs to repair nothing
1181 * \retval negative error number on failure
1183 static int lfsck_namespace_create_orphan_remote(const struct lu_env *env,
1184 struct lfsck_component *com,
1185 struct dt_object *orphan,
1187 struct lmv_mds_md_v1 *lmv)
1189 struct lfsck_thread_info *info = lfsck_env_info(env);
1190 struct lfsck_request *lr = &info->lti_lr;
1191 struct lu_seq_range *range = &info->lti_range;
1192 const struct lu_fid *fid = lfsck_dto2fid(orphan);
1193 struct lfsck_namespace *ns = com->lc_file_ram;
1194 struct lfsck_instance *lfsck = com->lc_lfsck;
1195 struct seq_server_site *ss =
1196 lu_site2seq(lfsck->li_bottom->dd_lu_dev.ld_site);
1197 struct lfsck_tgt_desc *ltd = NULL;
1198 struct ptlrpc_request *req = NULL;
1202 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1205 fld_range_set_mdt(range);
1206 rc = fld_server_lookup(env, ss->ss_server_fld, fid_seq(fid), range);
1210 ltd = lfsck_tgt_get(&lfsck->li_mdt_descs, range->lsr_index);
1212 ns->ln_flags |= LF_INCOMPLETE;
1214 GOTO(out, rc = -ENODEV);
1217 req = ptlrpc_request_alloc(class_exp2cliimp(ltd->ltd_exp),
1220 GOTO(out, rc = -ENOMEM);
1222 rc = ptlrpc_request_pack(req, LUSTRE_OBD_VERSION, LFSCK_NOTIFY);
1224 ptlrpc_request_free(req);
1229 lr = req_capsule_client_get(&req->rq_pill, &RMF_LFSCK_REQUEST);
1230 memset(lr, 0, sizeof(*lr));
1231 lr->lr_event = LE_CREATE_ORPHAN;
1232 lr->lr_index = lfsck_dev_idx(lfsck->li_bottom);
1233 lr->lr_active = LFSCK_TYPE_NAMESPACE;
1237 lr->lr_hash_type = lmv->lmv_hash_type;
1238 lr->lr_stripe_count = lmv->lmv_stripe_count;
1239 lr->lr_layout_version = lmv->lmv_layout_version;
1240 memcpy(lr->lr_pool_name, lmv->lmv_pool_name,
1241 sizeof(lr->lr_pool_name));
1244 ptlrpc_request_set_replen(req);
1245 rc = ptlrpc_queue_wait(req);
1246 ptlrpc_req_finished(req);
1250 else if (rc == -EEXIST)
1256 CDEBUG(D_LFSCK, "%s: namespace LFSCK create object "
1257 DFID" on the MDT %x remotely: rc = %d\n",
1258 lfsck_lfsck2name(lfsck), PFID(fid),
1259 ltd != NULL ? ltd->ltd_index : -1, rc);
1268 * Create the specified orphan MDT-object locally.
1270 * For the case that the parent MDT-object stored in some MDT-object's
1271 * linkEA entry is lost, the LFSCK will re-create the parent object as
1272 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1273 * with the name ${FID}-P-${conflict_version}.
1275 * \param[in] env pointer to the thread context
1276 * \param[in] com pointer to the lfsck component
1277 * \param[in] orphan pointer to the orphan MDT-object to be created
1278 * \param[in] type the orphan's type to be created
1280 * type "P": The orphan object to be created was a parent directory
1281 * of some MDT-object which linkEA shows that the @orphan
1282 * object is missing.
1284 * \see lfsck_layout_recreate_parent() for more types.
1286 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1288 * \retval positive number for repaired cases
1289 * \retval negative error number on failure
1291 static int lfsck_namespace_create_orphan_local(const struct lu_env *env,
1292 struct lfsck_component *com,
1293 struct dt_object *orphan,
1295 struct lmv_mds_md_v1 *lmv)
1297 struct lfsck_thread_info *info = lfsck_env_info(env);
1298 struct lu_attr *la = &info->lti_la;
1299 struct dt_allocation_hint *hint = &info->lti_hint;
1300 struct dt_object_format *dof = &info->lti_dof;
1301 struct lu_name *cname = &info->lti_name2;
1302 struct dt_insert_rec *rec = &info->lti_dt_rec;
1303 struct lu_fid *tfid = &info->lti_fid;
1304 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1305 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1306 const struct lu_fid *pfid;
1307 struct lfsck_instance *lfsck = com->lc_lfsck;
1308 struct dt_device *dev = lfsck->li_bottom;
1309 struct dt_object *parent = NULL;
1310 struct dt_object *child = NULL;
1311 struct thandle *th = NULL;
1312 struct lustre_handle lh = { 0 };
1313 struct linkea_data ldata = { 0 };
1314 struct lu_buf linkea_buf;
1315 struct lu_buf lmv_buf;
1322 LASSERT(!dt_object_exists(orphan));
1323 LASSERT(!dt_object_remote(orphan));
1325 /* @orphan maybe not attached to lfsck->li_bottom */
1326 child = lfsck_object_find_by_dev(env, dev, cfid);
1328 GOTO(log, rc = PTR_ERR(child));
1330 cname->ln_name = NULL;
1331 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1334 /* Create .lustre/lost+found/MDTxxxx when needed. */
1335 if (unlikely(lfsck->li_lpf_obj == NULL)) {
1336 rc = lfsck_create_lpf(env, lfsck);
1341 parent = lfsck->li_lpf_obj;
1342 pfid = lfsck_dto2fid(parent);
1344 /* Hold update lock on the parent to prevent others to access. */
1345 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1346 MDS_INODELOCK_UPDATE, LCK_EX);
1351 namelen = snprintf(name, 31, DFID"-P-%d",
1353 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1354 (const struct dt_key *)name, BYPASS_CAPA);
1355 if (rc != 0 && rc != -ENOENT)
1359 cname->ln_name = name;
1360 cname->ln_namelen = namelen;
1362 memset(la, 0, sizeof(*la));
1363 la->la_mode = type | (S_ISDIR(type) ? 0700 : 0600);
1364 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1365 LA_ATIME | LA_MTIME | LA_CTIME;
1367 child->do_ops->do_ah_init(env, hint, parent, child,
1368 la->la_mode & S_IFMT);
1370 memset(dof, 0, sizeof(*dof));
1371 dof->dof_type = dt_mode_to_dft(type);
1373 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1377 rc = linkea_add_buf(&ldata, cname, pfid);
1381 th = dt_trans_create(env, dev);
1383 GOTO(unlock1, rc = PTR_ERR(th));
1385 rc = dt_declare_create(env, child, la, hint, dof, th);
1386 if (rc == 0 && S_ISDIR(type))
1387 rc = dt_declare_ref_add(env, child, th);
1393 lmv->lmv_magic = LMV_MAGIC;
1394 lmv->lmv_master_mdt_index = lfsck_dev_idx(dev);
1395 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1396 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1397 rc = dt_declare_xattr_set(env, child, &lmv_buf,
1398 XATTR_NAME_LMV, 0, th);
1403 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1404 ldata.ld_leh->leh_len);
1405 rc = dt_declare_xattr_set(env, child, &linkea_buf,
1406 XATTR_NAME_LINK, 0, th);
1410 rec->rec_type = type;
1411 rec->rec_fid = cfid;
1412 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1413 (const struct dt_key *)name, th);
1414 if (rc == 0 && S_ISDIR(type))
1415 rc = dt_declare_ref_add(env, parent, th);
1420 rc = dt_trans_start_local(env, dev, th);
1424 dt_write_lock(env, child, 0);
1425 rc = dt_create(env, child, la, hint, dof, th);
1429 if (S_ISDIR(type)) {
1430 if (unlikely(!dt_try_as_dir(env, child)))
1431 GOTO(unlock2, rc = -ENOTDIR);
1433 rec->rec_type = S_IFDIR;
1434 rec->rec_fid = cfid;
1435 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1436 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1440 rec->rec_fid = pfid;
1441 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1442 (const struct dt_key *)dotdot, th,
1447 rc = dt_ref_add(env, child, th);
1453 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV, 0,
1459 rc = dt_xattr_set(env, child, &linkea_buf,
1460 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1461 dt_write_unlock(env, child);
1465 rec->rec_type = type;
1466 rec->rec_fid = cfid;
1467 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1468 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1469 if (rc == 0 && S_ISDIR(type)) {
1470 dt_write_lock(env, parent, 0);
1471 rc = dt_ref_add(env, parent, th);
1472 dt_write_unlock(env, parent);
1475 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1478 dt_write_unlock(env, child);
1481 dt_trans_stop(env, dev, th);
1484 lfsck_ibits_unlock(&lh, LCK_EX);
1487 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan locally for "
1488 "the object "DFID", name = %s, type %o: rc = %d\n",
1489 lfsck_lfsck2name(lfsck), PFID(cfid),
1490 cname->ln_name != NULL ? cname->ln_name : "<NULL>", type, rc);
1492 if (child != NULL && !IS_ERR(child))
1493 lfsck_object_put(env, child);
1499 * Create the specified orphan MDT-object.
1501 * For the case that the parent MDT-object stored in some MDT-object's
1502 * linkEA entry is lost, the LFSCK will re-create the parent object as
1503 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1504 * with the name: ${FID}-P-${conflict_version}.
1506 * \param[in] env pointer to the thread context
1507 * \param[in] com pointer to the lfsck component
1508 * \param[in] orphan pointer to the orphan MDT-object
1510 * type "P": The orphan object to be created was a parent directory
1511 * of some MDT-object which linkEA shows that the @orphan
1512 * object is missing.
1514 * \see lfsck_layout_recreate_parent() for more types.
1516 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1518 * \retval positive number for repaired cases
1519 * \retval 0 if needs to repair nothing
1520 * \retval negative error number on failure
1522 static int lfsck_namespace_create_orphan(const struct lu_env *env,
1523 struct lfsck_component *com,
1524 struct dt_object *orphan,
1525 struct lmv_mds_md_v1 *lmv)
1527 struct lfsck_namespace *ns = com->lc_file_ram;
1530 if (dt_object_remote(orphan))
1531 rc = lfsck_namespace_create_orphan_remote(env, com, orphan,
1534 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
1538 ns->ln_flags |= LF_INCONSISTENT;
1544 * Remove the specified entry from the linkEA.
1546 * Locate the linkEA entry with the given @cname and @pfid, then
1547 * remove this entry or the other entries those are repeated with
1550 * \param[in] env pointer to the thread context
1551 * \param[in] com pointer to the lfsck component
1552 * \param[in] obj pointer to the dt_object to be handled
1553 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1554 * \param[in] cname the name for the child in the parent directory
1555 * \param[in] pfid the parent directory's FID for the linkEA
1556 * \param[in] next if true, then remove the first found linkEA
1557 * entry, and move the ldata->ld_lee to next entry
1559 * \retval positive number for repaired cases
1560 * \retval 0 if nothing to be repaired
1561 * \retval negative error number on failure
1563 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1564 struct lfsck_component *com,
1565 struct dt_object *obj,
1566 struct linkea_data *ldata,
1567 struct lu_name *cname,
1568 struct lu_fid *pfid,
1571 struct lfsck_instance *lfsck = com->lc_lfsck;
1572 struct dt_device *dev = lfsck->li_bottom;
1573 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1574 struct thandle *th = NULL;
1575 struct lustre_handle lh = { 0 };
1576 struct linkea_data ldata_new = { 0 };
1577 struct lu_buf linkea_buf;
1581 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1582 MDS_INODELOCK_UPDATE |
1583 MDS_INODELOCK_XATTR, LCK_EX);
1588 linkea_del_buf(ldata, cname);
1590 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1592 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1593 ldata->ld_leh->leh_len);
1596 th = dt_trans_create(env, dev);
1598 GOTO(unlock1, rc = PTR_ERR(th));
1600 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1601 XATTR_NAME_LINK, 0, th);
1605 rc = dt_trans_start_local(env, dev, th);
1609 dt_write_lock(env, obj, 0);
1610 if (unlikely(lfsck_is_dead_obj(obj)))
1611 GOTO(unlock2, rc = -ENOENT);
1613 rc = lfsck_links_read2(env, obj, &ldata_new);
1617 /* The specified linkEA entry has been removed by race. */
1618 rc = linkea_links_find(&ldata_new, cname, pfid);
1620 GOTO(unlock2, rc = 0);
1622 if (bk->lb_param & LPF_DRYRUN)
1623 GOTO(unlock2, rc = 1);
1626 linkea_del_buf(&ldata_new, cname);
1628 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1631 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1632 dt_write_unlock(env, obj);
1633 dt_trans_stop(env, dev, th);
1634 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1635 ldata_new.ld_leh->leh_len);
1639 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1640 ldata_new.ld_leh->leh_len);
1641 rc = dt_xattr_set(env, obj, &linkea_buf,
1642 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1644 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1647 dt_write_unlock(env, obj);
1650 dt_trans_stop(env, dev, th);
1653 lfsck_ibits_unlock(&lh, LCK_EX);
1656 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1657 "for the object: "DFID", parent "DFID", name %.*s\n",
1658 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1659 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1663 struct lfsck_namespace *ns = com->lc_file_ram;
1665 ns->ln_flags |= LF_INCONSISTENT;
1672 * Conditionally remove the specified entry from the linkEA.
1674 * Take the parent lock firstly, then check whether the specified
1675 * name entry exists or not: if yes, do nothing; otherwise, call
1676 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1678 * \param[in] env pointer to the thread context
1679 * \param[in] com pointer to the lfsck component
1680 * \param[in] parent pointer to the parent directory
1681 * \param[in] child pointer to the child object that holds the linkEA
1682 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1683 * \param[in] cname the name for the child in the parent directory
1684 * \param[in] pfid the parent directory's FID for the linkEA
1686 * \retval positive number for repaired cases
1687 * \retval 0 if nothing to be repaired
1688 * \retval negative error number on failure
1690 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1691 struct lfsck_component *com,
1692 struct dt_object *parent,
1693 struct dt_object *child,
1694 struct linkea_data *ldata,
1695 struct lu_name *cname,
1696 struct lu_fid *pfid)
1698 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1699 struct lustre_handle lh = { 0 };
1703 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1704 MDS_INODELOCK_UPDATE, LCK_EX);
1708 dt_read_lock(env, parent, 0);
1709 if (unlikely(lfsck_is_dead_obj(parent))) {
1710 dt_read_unlock(env, parent);
1711 lfsck_ibits_unlock(&lh, LCK_EX);
1712 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1718 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1719 (const struct dt_key *)cname->ln_name,
1721 dt_read_unlock(env, parent);
1723 /* It is safe to release the ldlm lock, because when the logic come
1724 * here, we have got all the needed information above whether the
1725 * linkEA entry is valid or not. It is not important that others
1726 * may add new linkEA entry after the ldlm lock released. If other
1727 * has removed the specified linkEA entry by race, then it is OK,
1728 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1730 lfsck_ibits_unlock(&lh, LCK_EX);
1731 if (rc == -ENOENT) {
1732 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1741 /* The LFSCK just found some internal status of cross-MDTs
1742 * create operation. That is normal. */
1743 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1744 linkea_next_entry(ldata);
1749 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1756 * Conditionally replace name entry in the parent.
1758 * As required, the LFSCK may re-create the lost MDT-object for dangling
1759 * name entry, but such repairing may be wrong because of bad FID in the
1760 * name entry. As the LFSCK processing, the real MDT-object may be found,
1761 * then the LFSCK should check whether the former re-created MDT-object
1762 * has been modified or not, if not, then destroy it and update the name
1763 * entry in the parent to reference the real MDT-object.
1765 * \param[in] env pointer to the thread context
1766 * \param[in] com pointer to the lfsck component
1767 * \param[in] parent pointer to the parent directory
1768 * \param[in] child pointer to the MDT-object that may be the real
1769 * MDT-object corresponding to the name entry in parent
1770 * \param[in] cfid the current FID in the name entry
1771 * \param[in] cname contains the name of the child in the parent directory
1773 * \retval positive number for repaired cases
1774 * \retval 0 if nothing to be repaired
1775 * \retval negative error number on failure
1777 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1778 struct lfsck_component *com,
1779 struct dt_object *parent,
1780 struct dt_object *child,
1781 const struct lu_fid *cfid,
1782 const struct lu_name *cname)
1784 struct lfsck_thread_info *info = lfsck_env_info(env);
1785 struct lu_fid *tfid = &info->lti_fid5;
1786 struct lu_attr *la = &info->lti_la;
1787 struct dt_insert_rec *rec = &info->lti_dt_rec;
1788 struct lfsck_instance *lfsck = com->lc_lfsck;
1789 struct dt_device *dev = lfsck->li_next;
1790 const char *name = cname->ln_name;
1791 struct dt_object *obj = NULL;
1792 struct lustre_handle plh = { 0 };
1793 struct lustre_handle clh = { 0 };
1794 struct linkea_data ldata = { 0 };
1795 struct thandle *th = NULL;
1800 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1801 MDS_INODELOCK_UPDATE, LCK_EX);
1805 if (!fid_is_sane(cfid)) {
1810 obj = lfsck_object_find(env, lfsck, cfid);
1813 if (rc == -ENOENT) {
1821 if (!dt_object_exists(obj)) {
1826 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
1827 (const struct dt_key *)name, BYPASS_CAPA);
1828 if (rc == -ENOENT) {
1836 /* Someone changed the name entry, cannot replace it. */
1837 if (!lu_fid_eq(cfid, tfid))
1840 /* lock the object to be destroyed. */
1841 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1842 MDS_INODELOCK_UPDATE |
1843 MDS_INODELOCK_XATTR, LCK_EX);
1847 if (unlikely(lfsck_is_dead_obj(obj))) {
1852 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1856 /* The object has been modified by other(s), or it is not created by
1857 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1858 if (la->la_ctime != 0)
1861 if (S_ISREG(la->la_mode)) {
1862 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1864 /* If someone has created related OST-object(s),
1866 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1867 GOTO(log, rc = (rc > 0 ? 0 : rc));
1871 dt_read_lock(env, child, 0);
1872 rc = lfsck_links_read2(env, child, &ldata);
1873 dt_read_unlock(env, child);
1875 /* Someone changed the child, no need to replace. */
1882 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1883 /* Someone moved the child, no need to replace. */
1887 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1890 th = dt_trans_create(env, dev);
1892 GOTO(log, rc = PTR_ERR(th));
1895 rc = dt_declare_destroy(env, obj, th);
1900 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1904 rec->rec_type = S_IFDIR;
1905 rec->rec_fid = lfsck_dto2fid(child);
1906 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1907 (const struct dt_key *)name, th);
1911 rc = dt_trans_start(env, dev, th);
1916 rc = dt_destroy(env, obj, th);
1921 /* The old name entry maybe not exist. */
1922 dt_delete(env, parent, (const struct dt_key *)name, th,
1925 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1926 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1928 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1931 dt_trans_stop(env, dev, th);
1934 lfsck_ibits_unlock(&clh, LCK_EX);
1935 lfsck_ibits_unlock(&plh, LCK_EX);
1936 if (obj != NULL && !IS_ERR(obj))
1937 lfsck_object_put(env, obj);
1939 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1940 "object "DFID" because of conflict with the object "DFID
1941 " under the parent "DFID" with name %s: rc = %d\n",
1942 lfsck_lfsck2name(lfsck), PFID(cfid),
1943 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1950 * Overwrite the linkEA for the object with the given ldata.
1952 * The caller should take the ldlm lock before the calling.
1954 * \param[in] env pointer to the thread context
1955 * \param[in] com pointer to the lfsck component
1956 * \param[in] obj pointer to the dt_object to be handled
1957 * \param[in] ldata pointer to the new linkEA data
1959 * \retval positive number for repaired cases
1960 * \retval 0 if nothing to be repaired
1961 * \retval negative error number on failure
1963 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1964 struct lfsck_component *com,
1965 struct dt_object *obj,
1966 struct linkea_data *ldata)
1968 struct lfsck_instance *lfsck = com->lc_lfsck;
1969 struct dt_device *dev = lfsck->li_bottom;
1970 struct thandle *th = NULL;
1971 struct lu_buf linkea_buf;
1975 LASSERT(!dt_object_remote(obj));
1977 th = dt_trans_create(env, dev);
1979 GOTO(log, rc = PTR_ERR(th));
1981 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1982 ldata->ld_leh->leh_len);
1983 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1984 XATTR_NAME_LINK, 0, th);
1988 rc = dt_trans_start_local(env, dev, th);
1992 dt_write_lock(env, obj, 0);
1993 if (unlikely(lfsck_is_dead_obj(obj)))
1994 GOTO(unlock, rc = 0);
1996 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1997 GOTO(unlock, rc = 1);
1999 rc = dt_xattr_set(env, obj, &linkea_buf,
2000 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2002 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2005 dt_write_unlock(env, obj);
2008 dt_trans_stop(env, dev, th);
2011 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
2012 "object "DFID": rc = %d\n",
2013 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
2016 struct lfsck_namespace *ns = com->lc_file_ram;
2018 ns->ln_flags |= LF_INCONSISTENT;
2025 * Repair invalid name entry.
2027 * If the name entry contains invalid information, such as bad file type
2028 * or (and) corrupted object FID, then either remove the name entry or
2029 * udpate the name entry with the given (right) information.
2031 * \param[in] env pointer to the thread context
2032 * \param[in] com pointer to the lfsck component
2033 * \param[in] parent pointer to the parent directory
2034 * \param[in] child pointer to the object referenced by the name entry
2035 * \param[in] name the old name of the child under the parent directory
2036 * \param[in] name2 the new name of the child under the parent directory
2037 * \param[in] type the type claimed by the name entry
2038 * \param[in] update update the name entry if true; otherwise, remove it
2039 * \param[in] dec decrease the parent nlink count if true
2041 * \retval positive number for repaired successfully
2042 * \retval 0 if nothing to be repaired
2043 * \retval negative error number on failure
2045 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2046 struct lfsck_component *com,
2047 struct dt_object *parent,
2048 struct dt_object *child,
2049 const char *name, const char *name2,
2050 __u16 type, bool update, bool dec)
2052 struct lfsck_thread_info *info = lfsck_env_info(env);
2053 struct dt_insert_rec *rec = &info->lti_dt_rec;
2054 const struct lu_fid *cfid = lfsck_dto2fid(child);
2055 struct lu_fid *tfid = &info->lti_fid5;
2056 struct lfsck_instance *lfsck = com->lc_lfsck;
2057 struct dt_device *dev = lfsck->li_next;
2058 struct thandle *th = NULL;
2059 struct lustre_handle lh = { 0 };
2063 if (unlikely(!dt_try_as_dir(env, parent)))
2064 GOTO(log, rc = -ENOTDIR);
2066 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2067 MDS_INODELOCK_UPDATE, LCK_EX);
2071 th = dt_trans_create(env, dev);
2073 GOTO(unlock1, rc = PTR_ERR(th));
2075 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2080 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2081 rec->rec_fid = cfid;
2082 rc = dt_declare_insert(env, parent,
2083 (const struct dt_rec *)rec,
2084 (const struct dt_key *)name2, th);
2090 rc = dt_declare_ref_del(env, parent, th);
2095 rc = dt_trans_start(env, dev, th);
2099 dt_write_lock(env, parent, 0);
2100 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2101 (const struct dt_key *)name, BYPASS_CAPA);
2102 /* Someone has removed the bad name entry by race. */
2104 GOTO(unlock2, rc = 0);
2109 /* Someone has removed the bad name entry and reused it for other
2110 * object by race. */
2111 if (!lu_fid_eq(tfid, cfid))
2112 GOTO(unlock2, rc = 0);
2114 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2115 GOTO(unlock2, rc = 1);
2117 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2123 rc = dt_insert(env, parent,
2124 (const struct dt_rec *)rec,
2125 (const struct dt_key *)name2, th,
2132 rc = dt_ref_del(env, parent, th);
2137 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2140 dt_write_unlock(env, parent);
2143 dt_trans_stop(env, dev, th);
2145 /* We are not sure whether the child will become orphan or not.
2146 * Record it in the LFSCK trace file for further checking in
2147 * the second-stage scanning. */
2148 if (!update && !dec && rc == 0)
2149 lfsck_namespace_trace_update(env, com, cfid,
2150 LNTF_CHECK_LINKEA, true);
2153 lfsck_ibits_unlock(&lh, LCK_EX);
2156 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2157 "entry for: parent "DFID", child "DFID", name %s, type "
2158 "in name entry %o, type claimed by child %o. repair it "
2159 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2160 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2161 name, type, update ? lfsck_object_type(child) : 0,
2162 update ? "updating" : "removing", name2, rc);
2165 struct lfsck_namespace *ns = com->lc_file_ram;
2167 ns->ln_flags |= LF_INCONSISTENT;
2174 * Update the ".." name entry for the given object.
2176 * The object's ".." is corrupted, this function will update the ".." name
2177 * entry with the given pfid, and the linkEA with the given ldata.
2179 * The caller should take the ldlm lock before the calling.
2181 * \param[in] env pointer to the thread context
2182 * \param[in] com pointer to the lfsck component
2183 * \param[in] obj pointer to the dt_object to be handled
2184 * \param[in] pfid the new fid for the object's ".." name entry
2185 * \param[in] cname the name for the @obj in the parent directory
2187 * \retval positive number for repaired cases
2188 * \retval 0 if nothing to be repaired
2189 * \retval negative error number on failure
2191 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2192 struct lfsck_component *com,
2193 struct dt_object *obj,
2194 const struct lu_fid *pfid,
2195 struct lu_name *cname)
2197 struct lfsck_thread_info *info = lfsck_env_info(env);
2198 struct dt_insert_rec *rec = &info->lti_dt_rec;
2199 struct lfsck_instance *lfsck = com->lc_lfsck;
2200 struct dt_device *dev = lfsck->li_bottom;
2201 struct thandle *th = NULL;
2202 struct linkea_data ldata = { 0 };
2203 struct lu_buf linkea_buf;
2207 LASSERT(!dt_object_remote(obj));
2208 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2210 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2214 rc = linkea_add_buf(&ldata, cname, pfid);
2218 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2219 ldata.ld_leh->leh_len);
2221 th = dt_trans_create(env, dev);
2223 GOTO(log, rc = PTR_ERR(th));
2225 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2229 rec->rec_type = S_IFDIR;
2230 rec->rec_fid = pfid;
2231 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2232 (const struct dt_key *)dotdot, th);
2236 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2237 XATTR_NAME_LINK, 0, th);
2241 rc = dt_trans_start_local(env, dev, th);
2245 dt_write_lock(env, obj, 0);
2246 if (unlikely(lfsck_is_dead_obj(obj)))
2247 GOTO(unlock, rc = 0);
2249 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2250 GOTO(unlock, rc = 1);
2252 /* The old ".." name entry maybe not exist. */
2253 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2256 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2257 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2261 rc = dt_xattr_set(env, obj, &linkea_buf,
2262 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2264 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2267 dt_write_unlock(env, obj);
2270 dt_trans_stop(env, dev, th);
2273 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2274 "the object "DFID", new parent "DFID": rc = %d\n",
2275 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2279 struct lfsck_namespace *ns = com->lc_file_ram;
2281 ns->ln_flags |= LF_INCONSISTENT;
2288 * Handle orphan @obj during Double Scan Directory.
2290 * Remove the @obj's current (invalid) linkEA entries, and insert
2291 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2292 * ${FID}-${PFID}-D-${conflict_version}
2294 * The caller should take the ldlm lock before the calling.
2296 * \param[in] env pointer to the thread context
2297 * \param[in] com pointer to the lfsck component
2298 * \param[in] obj pointer to the orphan object to be handled
2299 * \param[in] pfid the new fid for the object's ".." name entry
2300 * \param[in,out] lh ldlm lock handler for the given @obj
2301 * \param[out] type to tell the caller what the inconsistency is
2303 * \retval positive number for repaired cases
2304 * \retval 0 if nothing to be repaired
2305 * \retval negative error number on failure
2308 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2309 struct lfsck_component *com,
2310 struct dt_object *obj,
2311 const struct lu_fid *pfid,
2312 struct lustre_handle *lh,
2313 enum lfsck_namespace_inconsistency_type *type)
2315 struct lfsck_thread_info *info = lfsck_env_info(env);
2316 struct lfsck_namespace *ns = com->lc_file_ram;
2320 /* Remove the unrecognized linkEA. */
2321 rc = lfsck_namespace_links_remove(env, com, obj);
2322 lfsck_ibits_unlock(lh, LCK_EX);
2323 if (rc < 0 && rc != -ENODATA)
2326 *type = LNIT_MUL_REF;
2328 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2329 * ever tried to verify some remote MDT-object that resides on this
2330 * MDT, but this MDT failed to respond such request. So means there
2331 * may be some remote name entry on other MDT that references this
2332 * object with another name, so we cannot know whether this linkEA
2333 * is valid or not. So keep it there and maybe resolved when next
2335 if (ns->ln_flags & LF_INCOMPLETE)
2338 /* The unique linkEA is invalid, even if the ".." name entry may be
2339 * valid, we still cannot know via which name entry this directory
2340 * will be referenced. Then handle it as pure orphan. */
2341 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2342 "-"DFID, PFID(pfid));
2343 rc = lfsck_namespace_insert_orphan(env, com, obj,
2344 info->lti_tmpbuf, "D", NULL);
2350 * Double Scan Directory object for single linkEA entry case.
2352 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2353 * then check whether the name is in the namespace or not, if not, add the
2354 * missing name entry back to namespace. If the linkEA entry is invalid,
2355 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2358 * \param[in] env pointer to the thread context
2359 * \param[in] com pointer to the lfsck component
2360 * \param[in] child pointer to the directory to be double scanned
2361 * \param[in] pfid the FID corresponding to the ".." entry
2362 * \param[in] ldata pointer to the linkEA data for the given @child
2363 * \param[in,out] lh ldlm lock handler for the given @child
2364 * \param[out] type to tell the caller what the inconsistency is
2365 * \param[in] retry if found inconsistency, but the caller does not hold
2366 * ldlm lock on the @child, then set @retry as true
2368 * \retval positive number for repaired cases
2369 * \retval 0 if nothing to be repaired
2370 * \retval negative error number on failure
2373 lfsck_namespace_dsd_single(const struct lu_env *env,
2374 struct lfsck_component *com,
2375 struct dt_object *child,
2376 const struct lu_fid *pfid,
2377 struct linkea_data *ldata,
2378 struct lustre_handle *lh,
2379 enum lfsck_namespace_inconsistency_type *type,
2382 struct lfsck_thread_info *info = lfsck_env_info(env);
2383 struct lu_name *cname = &info->lti_name;
2384 const struct lu_fid *cfid = lfsck_dto2fid(child);
2385 struct lu_fid *tfid = &info->lti_fid3;
2386 struct lfsck_namespace *ns = com->lc_file_ram;
2387 struct lfsck_instance *lfsck = com->lc_lfsck;
2388 struct dt_object *parent = NULL;
2389 struct lmv_mds_md_v1 *lmv;
2393 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid, info->lti_key);
2394 /* The unique linkEA entry with bad parent will be handled as orphan. */
2395 if (!fid_is_sane(tfid)) {
2396 if (!lustre_handle_is_used(lh) && retry != NULL)
2399 rc = lfsck_namespace_dsd_orphan(env, com, child,
2405 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2407 GOTO(out, rc = PTR_ERR(parent));
2409 /* We trust the unique linkEA entry in spite of whether it matches the
2410 * ".." name entry or not. Because even if the linkEA entry is wrong
2411 * and the ".." name entry is right, we still cannot know via which
2412 * name entry the child will be referenced, since all known entries
2413 * have been verified during the first-stage scanning. */
2414 if (!dt_object_exists(parent)) {
2415 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2416 * has ever tried to verify some remote MDT-object that resides
2417 * on this MDT, but this MDT failed to respond such request. So
2418 * means there may be some remote name entry on other MDT that
2419 * references this object with another name, so we cannot know
2420 * whether this linkEA is valid or not. So keep it there and
2421 * maybe resolved when next LFSCK run. */
2422 if (ns->ln_flags & LF_INCOMPLETE)
2425 if (!lustre_handle_is_used(lh) && retry != NULL) {
2431 lfsck_ibits_unlock(lh, LCK_EX);
2434 lmv = &info->lti_lmv;
2435 rc = lfsck_read_stripe_lmv(env, child, lmv);
2436 if (rc != 0 && rc != -ENODATA)
2439 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2441 } else if (lfsck_shard_name_to_index(env,
2442 cname->ln_name, cname->ln_namelen,
2443 S_IFDIR, cfid) < 0) {
2444 /* It is an invalid name entry, we
2445 * cannot trust the parent also. */
2446 rc = lfsck_namespace_shrink_linkea(env, com, child,
2447 ldata, cname, tfid, true);
2451 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2452 "-"DFID, PFID(pfid));
2453 rc = lfsck_namespace_insert_orphan(env, com, child,
2454 info->lti_tmpbuf, "S", NULL);
2459 /* Create the lost parent as an orphan. */
2460 rc = lfsck_namespace_create_orphan(env, com, parent, lmv);
2462 /* Add the missing name entry to the parent. */
2463 rc = lfsck_namespace_insert_normal(env, com, parent,
2464 child, cname->ln_name);
2465 if (unlikely(rc == -EEXIST)) {
2466 /* Unfortunately, someone reused the name
2467 * under the parent by race. So we have
2468 * to remove the linkEA entry from
2469 * current child object. It means that the
2470 * LFSCK cannot recover the system
2471 * totally back to its original status,
2472 * but it is necessary to make the
2473 * current system to be consistent. */
2474 rc = lfsck_namespace_shrink_linkea(env,
2478 snprintf(info->lti_tmpbuf,
2479 sizeof(info->lti_tmpbuf),
2480 "-"DFID, PFID(pfid));
2481 rc = lfsck_namespace_insert_orphan(env,
2482 com, child, info->lti_tmpbuf,
2491 /* The unique linkEA entry with bad parent will be handled as orphan. */
2492 if (unlikely(!dt_try_as_dir(env, parent))) {
2493 if (!lustre_handle_is_used(lh) && retry != NULL)
2496 rc = lfsck_namespace_dsd_orphan(env, com, child,
2502 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2503 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2504 if (rc == -ENOENT) {
2505 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2506 * has ever tried to verify some remote MDT-object that resides
2507 * on this MDT, but this MDT failed to respond such request. So
2508 * means there may be some remote name entry on other MDT that
2509 * references this object with another name, so we cannot know
2510 * whether this linkEA is valid or not. So keep it there and
2511 * maybe resolved when next LFSCK run. */
2512 if (ns->ln_flags & LF_INCOMPLETE)
2515 if (!lustre_handle_is_used(lh) && retry != NULL) {
2521 lfsck_ibits_unlock(lh, LCK_EX);
2522 rc = lfsck_namespace_check_name(env, parent, child, cname);
2529 /* It is an invalid name entry, drop it. */
2530 if (unlikely(rc > 0)) {
2531 rc = lfsck_namespace_shrink_linkea(env, com, child,
2532 ldata, cname, tfid, true);
2534 snprintf(info->lti_tmpbuf,
2535 sizeof(info->lti_tmpbuf),
2536 "-"DFID, PFID(pfid));
2537 rc = lfsck_namespace_insert_orphan(env, com,
2538 child, info->lti_tmpbuf, "D", NULL);
2544 /* Add the missing name entry back to the namespace. */
2545 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2547 if (unlikely(rc == -ESTALE))
2548 /* It may happen when the remote object has been
2549 * removed, but the local MDT is not aware of that. */
2552 if (unlikely(rc == -EEXIST)) {
2553 /* Unfortunately, someone reused the name under the
2554 * parent by race. So we have to remove the linkEA
2555 * entry from current child object. It means that the
2556 * LFSCK cannot recover the system totally back to
2557 * its original status, but it is necessary to make
2558 * the current system to be consistent.
2560 * It also may be because of the LFSCK found some
2561 * internal status of create operation. Under such
2562 * case, nothing to be done. */
2563 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2564 parent, child, ldata, cname, tfid);
2566 snprintf(info->lti_tmpbuf,
2567 sizeof(info->lti_tmpbuf),
2568 "-"DFID, PFID(pfid));
2569 rc = lfsck_namespace_insert_orphan(env, com,
2570 child, info->lti_tmpbuf, "D", NULL);
2580 if (!lu_fid_eq(tfid, cfid)) {
2581 if (!lustre_handle_is_used(lh) && retry != NULL) {
2587 lfsck_ibits_unlock(lh, LCK_EX);
2588 /* The name entry references another MDT-object that
2589 * may be created by the LFSCK for repairing dangling
2590 * name entry. Try to replace it. */
2591 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2594 rc = lfsck_namespace_dsd_orphan(env, com, child,
2600 if (fid_is_zero(pfid))
2603 /* The ".." name entry is wrong, update it. */
2604 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2605 if (!lustre_handle_is_used(lh) && retry != NULL) {
2611 *type = LNIT_UNMATCHED_PAIRS;
2612 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2613 lfsck_dto2fid(parent), cname);
2619 if (parent != NULL && !IS_ERR(parent))
2620 lfsck_object_put(env, parent);
2626 * Double Scan Directory object for multiple linkEA entries case.
2628 * The given @child has multiple linkEA entries. There is at most one linkEA
2629 * entry will be valid, all the others will be removed. Firstly, the function
2630 * will try to find out the linkEA entry for which the name entry exists under
2631 * the given parent (@pfid). If there is no linkEA entry that matches the given
2632 * ".." name entry, then tries to find out the first linkEA entry that both the
2633 * parent and the name entry exist to rebuild a new ".." name entry.
2635 * \param[in] env pointer to the thread context
2636 * \param[in] com pointer to the lfsck component
2637 * \param[in] child pointer to the directory to be double scanned
2638 * \param[in] pfid the FID corresponding to the ".." entry
2639 * \param[in] ldata pointer to the linkEA data for the given @child
2640 * \param[in,out] lh ldlm lock handler for the given @child
2641 * \param[out] type to tell the caller what the inconsistency is
2642 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2644 * \retval positive number for repaired cases
2645 * \retval 0 if nothing to be repaired
2646 * \retval negative error number on failure
2649 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2650 struct lfsck_component *com,
2651 struct dt_object *child,
2652 const struct lu_fid *pfid,
2653 struct linkea_data *ldata,
2654 struct lustre_handle *lh,
2655 enum lfsck_namespace_inconsistency_type *type,
2658 struct lfsck_thread_info *info = lfsck_env_info(env);
2659 struct lu_name *cname = &info->lti_name;
2660 const struct lu_fid *cfid = lfsck_dto2fid(child);
2661 struct lu_fid *tfid = &info->lti_fid3;
2662 struct lu_fid *pfid2 = &info->lti_fid4;
2663 struct lfsck_namespace *ns = com->lc_file_ram;
2664 struct lfsck_instance *lfsck = com->lc_lfsck;
2665 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2666 struct dt_object *parent = NULL;
2667 struct linkea_data ldata_new = { 0 };
2668 int dirent_count = 0;
2669 int linkea_count = 0;
2675 while (ldata->ld_lee != NULL) {
2676 lfsck_namespace_unpack_linkea_entry(ldata, cname, tfid,
2678 /* Drop repeated linkEA entries. */
2679 lfsck_namespace_filter_linkea_entry(ldata, cname, tfid, true);
2680 /* Drop invalid linkEA entry. */
2681 if (!fid_is_sane(tfid)) {
2682 linkea_del_buf(ldata, cname);
2687 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2688 * then it is possible that: the directry object has ever
2689 * been lost, but its name entry was there. In the former
2690 * LFSCK run, during the first-stage scanning, the LFSCK
2691 * found the dangling name entry, but it did not recreate
2692 * the lost object, and when moved to the second-stage
2693 * scanning, some children objects of the lost directory
2694 * object were found, then the LFSCK recreated such lost
2695 * directory object as an orphan.
2697 * When the LFSCK runs again, if the dangling name is still
2698 * there, the LFSCK should move the orphan directory object
2699 * back to the normal namespace. */
2700 if (!lpf && !lu_fid_eq(pfid, tfid) && once) {
2701 linkea_next_entry(ldata);
2705 parent = lfsck_object_find_bottom(env, lfsck, tfid);
2707 RETURN(PTR_ERR(parent));
2709 if (!dt_object_exists(parent)) {
2710 lfsck_object_put(env, parent);
2711 if (ldata->ld_leh->leh_reccount > 1) {
2712 /* If it is NOT the last linkEA entry, then
2713 * there is still other chance to make the
2714 * child to be visible via other parent, then
2715 * remove this linkEA entry. */
2716 linkea_del_buf(ldata, cname);
2724 /* The linkEA entry with bad parent will be removed. */
2725 if (unlikely(!dt_try_as_dir(env, parent))) {
2726 lfsck_object_put(env, parent);
2727 linkea_del_buf(ldata, cname);
2732 rc = dt_lookup(env, parent, (struct dt_rec *)tfid,
2733 (const struct dt_key *)cname->ln_name,
2735 *pfid2 = *lfsck_dto2fid(parent);
2736 if (rc == -ENOENT) {
2737 lfsck_object_put(env, parent);
2738 linkea_next_entry(ldata);
2743 lfsck_object_put(env, parent);
2748 if (lu_fid_eq(tfid, cfid)) {
2749 lfsck_object_put(env, parent);
2750 if (!lu_fid_eq(pfid, pfid2)) {
2751 *type = LNIT_UNMATCHED_PAIRS;
2752 rc = lfsck_namespace_repair_unmatched_pairs(env,
2753 com, child, pfid2, cname);
2759 /* It is the most common case that we find the
2760 * name entry corresponding to the linkEA entry
2761 * that matches the ".." name entry. */
2762 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2766 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2770 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2775 linkea_del_buf(ldata, cname);
2777 linkea_first_entry(ldata);
2778 /* There may be some invalid dangling name entries under
2779 * other parent directories, remove all of them. */
2780 while (ldata->ld_lee != NULL) {
2781 lfsck_namespace_unpack_linkea_entry(ldata,
2782 cname, tfid, info->lti_key);
2783 if (!fid_is_sane(tfid))
2786 parent = lfsck_object_find_bottom(env, lfsck,
2788 if (IS_ERR(parent)) {
2789 rc = PTR_ERR(parent);
2790 if (rc != -ENOENT &&
2791 bk->lb_param & LPF_FAILOUT)
2797 if (!dt_object_exists(parent)) {
2798 lfsck_object_put(env, parent);
2802 rc = lfsck_namespace_repair_dirent(env, com,
2803 parent, child, cname->ln_name,
2804 cname->ln_name, S_IFDIR, false, true);
2805 lfsck_object_put(env, parent);
2807 if (bk->lb_param & LPF_FAILOUT)
2816 linkea_del_buf(ldata, cname);
2819 ns->ln_dirent_repaired += dirent_count;
2824 lfsck_ibits_unlock(lh, LCK_EX);
2825 /* The name entry references another MDT-object that may be
2826 * created by the LFSCK for repairing dangling name entry.
2827 * Try to replace it. */
2828 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2830 lfsck_object_put(env, parent);
2837 linkea_del_buf(ldata, cname);
2840 linkea_first_entry(ldata);
2841 if (ldata->ld_leh->leh_reccount == 1) {
2842 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2845 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2846 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2852 /* All linkEA entries are invalid and removed, then handle the @child
2854 if (ldata->ld_leh->leh_reccount == 0) {
2855 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2861 /* If the dangling name entry for the orphan directory object has
2862 * been remvoed, then just check whether the directory object is
2863 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2869 /* There is no linkEA entry that matches the ".." name entry. Find
2870 * the first linkEA entry that both parent and name entry exist to
2871 * rebuild a new ".." name entry. */
2881 * Repair the object's nlink attribute.
2883 * If all the known name entries have been verified, then the object's hard
2884 * link attribute should match the object's linkEA entries count unless the
2885 * object's has too much hard link to be recorded in the linkEA. Such cases
2886 * should have been marked in the LFSCK trace file. Otherwise, trust the
2887 * linkEA to update the object's nlink attribute.
2889 * \param[in] env pointer to the thread context
2890 * \param[in] com pointer to the lfsck component
2891 * \param[in] obj pointer to the dt_object to be handled
2892 * \param[in,out] nlink pointer to buffer to object's hard lock count before
2893 * and after the repairing
2895 * \retval positive number for repaired cases
2896 * \retval 0 if nothing to be repaired
2897 * \retval negative error number on failure
2899 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2900 struct lfsck_component *com,
2901 struct dt_object *obj, __u32 *nlink)
2903 struct lfsck_thread_info *info = lfsck_env_info(env);
2904 struct lu_attr *la = &info->lti_la3;
2905 struct lu_fid *tfid = &info->lti_fid3;
2906 struct lfsck_namespace *ns = com->lc_file_ram;
2907 struct lfsck_instance *lfsck = com->lc_lfsck;
2908 struct dt_device *dev = lfsck->li_bottom;
2909 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2910 struct dt_object *child = NULL;
2911 struct thandle *th = NULL;
2912 struct linkea_data ldata = { 0 };
2913 struct lustre_handle lh = { 0 };
2919 LASSERT(!dt_object_remote(obj));
2920 LASSERT(S_ISREG(lfsck_object_type(obj)));
2922 child = lfsck_object_find_by_dev(env, dev, cfid);
2924 GOTO(log, rc = PTR_ERR(child));
2926 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2927 MDS_INODELOCK_UPDATE |
2928 MDS_INODELOCK_XATTR, LCK_EX);
2932 th = dt_trans_create(env, dev);
2934 GOTO(log, rc = PTR_ERR(th));
2936 la->la_valid = LA_NLINK;
2937 rc = dt_declare_attr_set(env, child, la, th);
2941 rc = dt_trans_start_local(env, dev, th);
2945 dt_write_lock(env, child, 0);
2946 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2947 * ever tried to verify some remote MDT-object that resides on this
2948 * MDT, but this MDT failed to respond such request. So means there
2949 * may be some remote name entry on other MDT that references this
2950 * object with another name, so we cannot know whether this linkEA
2951 * is valid or not. So keep it there and maybe resolved when next
2953 if (ns->ln_flags & LF_INCOMPLETE)
2954 GOTO(unlock, rc = 0);
2956 fid_cpu_to_be(tfid, cfid);
2957 rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)&flags,
2958 (const struct dt_key *)tfid, BYPASS_CAPA);
2962 if (flags & LNTF_SKIP_NLINK)
2963 GOTO(unlock, rc = 0);
2965 rc = lfsck_links_read2(env, child, &ldata);
2967 GOTO(unlock, rc = 0);
2972 if (*nlink == ldata.ld_leh->leh_reccount)
2973 GOTO(unlock, rc = 0);
2975 la->la_nlink = *nlink = ldata.ld_leh->leh_reccount;
2976 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2977 GOTO(unlock, rc = 1);
2979 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
2981 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2984 dt_write_unlock(env, child);
2987 dt_trans_stop(env, dev, th);
2990 lfsck_ibits_unlock(&lh, LCK_EX);
2991 if (child != NULL && !IS_ERR(child))
2992 lfsck_object_put(env, child);
2994 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2995 "nlink count from %u to %u: rc = %d\n",
2996 lfsck_lfsck2name(lfsck), PFID(cfid), old, *nlink, rc);
2999 ns->ln_flags |= LF_INCONSISTENT;
3005 * Double scan the directory object for namespace LFSCK.
3007 * This function will verify the <parent, child> pairs in the namespace tree:
3008 * the parent references the child via some name entry that should be in the
3009 * child's linkEA entry, the child should back references the parent via its
3012 * The LFSCK will scan every linkEA entry in turn until find out the first
3013 * matched pairs. If found, then all other linkEA entries will be dropped.
3014 * If all the linkEA entries cannot match the ".." name entry, then there
3015 * are serveral possible cases:
3017 * 1) If there is only one linkEA entry, then trust it as long as the PFID
3018 * in the linkEA entry is valid.
3020 * 2) If there are multiple linkEA entries, then try to find the linkEA
3021 * that matches the ".." name entry. If found, then all other entries
3022 * are invalid; otherwise, it is quite possible that the ".." name entry
3023 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
3024 * entry according to the first valid linkEA entry (both the parent and
3025 * the name entry should exist).
3027 * 3) If the directory object has no (valid) linkEA entry, then the
3028 * directory object will be handled as pure orphan and inserted
3029 * in the .lustre/lost+found/MDTxxxx/ with the name:
3030 * ${self_FID}-${PFID}-D-${conflict_version}
3032 * \param[in] env pointer to the thread context
3033 * \param[in] com pointer to the lfsck component
3034 * \param[in] child pointer to the directory object to be handled
3035 * \param[in] flags to indicate the specical checking on the @child
3037 * \retval positive number for repaired cases
3038 * \retval 0 if nothing to be repaired
3039 * \retval negative error number on failure
3041 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3042 struct lfsck_component *com,
3043 struct dt_object *child, __u8 flags)
3045 struct lfsck_thread_info *info = lfsck_env_info(env);
3046 const struct lu_fid *cfid = lfsck_dto2fid(child);
3047 struct lu_fid *pfid = &info->lti_fid2;
3048 struct lfsck_namespace *ns = com->lc_file_ram;
3049 struct lfsck_instance *lfsck = com->lc_lfsck;
3050 struct lustre_handle lh = { 0 };
3051 struct linkea_data ldata = { 0 };
3052 bool unknown = false;
3055 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3059 LASSERT(!dt_object_remote(child));
3061 if (flags & LNTF_UNCERTAIN_LMV) {
3062 if (flags & LNTF_RECHECK_NAME_HASH) {
3063 rc = lfsck_namespace_scan_shard(env, com, child);
3067 ns->ln_striped_shards_scanned++;
3069 ns->ln_striped_shards_skipped++;
3073 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3077 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3078 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3079 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3080 "the namespace LFSCK, then the LFSCK cannot guarantee"
3081 "all the name entries have been verified in first-stage"
3082 "scanning. So have to skip orphan related handling for"
3083 "the directory object "DFID" with remote name entry\n",
3084 lfsck_lfsck2name(lfsck), PFID(cfid));
3089 if (unlikely(!dt_try_as_dir(env, child)))
3090 GOTO(out, rc = -ENOTDIR);
3092 /* We only take ldlm lock on the @child when required. When the
3093 * logic comes here for the first time, it is always false. */
3097 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3098 MDS_INODELOCK_UPDATE |
3099 MDS_INODELOCK_XATTR, LCK_EX);
3104 dt_read_lock(env, child, 0);
3105 if (unlikely(lfsck_is_dead_obj(child))) {
3106 dt_read_unlock(env, child);
3111 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3112 (const struct dt_key *)dotdot, BYPASS_CAPA);
3114 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3115 dt_read_unlock(env, child);
3120 if (!lustre_handle_is_used(&lh)) {
3121 dt_read_unlock(env, child);
3126 } else if (lfsck->li_lpf_obj != NULL &&
3127 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3129 } else if (unlikely(!fid_is_sane(pfid))) {
3133 rc = lfsck_links_read(env, child, &ldata);
3134 dt_read_unlock(env, child);
3136 if (rc != -ENODATA && rc != -EINVAL)
3139 if (!lustre_handle_is_used(&lh))
3142 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3143 /* Remove the corrupted linkEA. */
3144 rc = lfsck_namespace_links_remove(env, com, child);
3146 /* Here, because of the crashed linkEA, we
3147 * cannot know whether there is some parent
3148 * that references the child directory via
3149 * some name entry or not. So keep it there,
3150 * when the LFSCK run next time, if there is
3151 * some parent that references this object,
3152 * then the LFSCK can rebuild the linkEA;
3153 * otherwise, this object will be handled
3154 * as orphan as above. */
3157 /* 1. If we have neither ".." nor linkEA,
3158 * then it is an orphan.
3160 * 2. If we only have the ".." name entry,
3161 * but no parent references this child
3162 * directory, then handle it as orphan. */
3163 lfsck_ibits_unlock(&lh, LCK_EX);
3164 type = LNIT_MUL_REF;
3166 /* If the LFSCK is marked as LF_INCOMPLETE,
3167 * then means some MDT has ever tried to
3168 * verify some remote MDT-object that resides
3169 * on this MDT, but this MDT failed to respond
3170 * such request. So means there may be some
3171 * remote name entry on other MDT that
3172 * references this object with another name,
3173 * so we cannot know whether this linkEA is
3174 * valid or not. So keep it there and maybe
3175 * resolved when next LFSCK run. */
3176 if (ns->ln_flags & LF_INCOMPLETE)
3179 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3180 "-"DFID, PFID(pfid));
3181 rc = lfsck_namespace_insert_orphan(env, com, child,
3182 info->lti_tmpbuf, "D", NULL);
3188 linkea_first_entry(&ldata);
3189 /* This is the most common case: the object has unique linkEA entry. */
3190 if (ldata.ld_leh->leh_reccount == 1) {
3191 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3192 &lh, &type, &retry);
3194 LASSERT(!lustre_handle_is_used(&lh));
3203 if (!lustre_handle_is_used(&lh))
3206 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3207 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3213 /* When we come here, the cases usually like that:
3214 * 1) The directory object has a corrupted linkEA entry. During the
3215 * first-stage scanning, the LFSCK cannot know such corruption,
3216 * then it appends the right linkEA entry according to the found
3217 * name entry after the bad one.
3219 * 2) The directory object has a right linkEA entry. During the
3220 * first-stage scanning, the LFSCK finds some bad name entry,
3221 * but the LFSCK cannot aware that at that time, then it adds
3222 * the bad linkEA entry for further processing. */
3223 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3229 lfsck_ibits_unlock(&lh, LCK_EX);
3232 case LNIT_BAD_LINKEA:
3233 ns->ln_linkea_repaired++;
3235 case LNIT_UNMATCHED_PAIRS:
3236 ns->ln_unmatched_pairs_repaired++;
3239 ns->ln_mul_ref_repaired++;
3247 ns->ln_unknown_inconsistency++;
3253 * Double scan the MDT-object for namespace LFSCK.
3255 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3256 * those entries from the linkEA; if the linkEA becomes empty or the object
3257 * has no linkEA, then it is an orphan and will be added into the directory
3258 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3259 * the remote parent; if the name entry corresponding to some linkEA entry
3260 * is lost, then add the name entry back to the namespace.
3262 * \param[in] env pointer to the thread context
3263 * \param[in] com pointer to the lfsck component
3264 * \param[in] child pointer to the dt_object to be handled
3265 * \param[in] flags some hints to indicate how the @child should be handled
3267 * \retval positive number for repaired cases
3268 * \retval 0 if nothing to be repaired
3269 * \retval negative error number on failure
3271 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3272 struct lfsck_component *com,
3273 struct dt_object *child, __u8 flags)
3275 struct lfsck_thread_info *info = lfsck_env_info(env);
3276 struct lu_attr *la = &info->lti_la;
3277 struct lu_name *cname = &info->lti_name;
3278 struct lu_fid *pfid = &info->lti_fid;
3279 struct lu_fid *cfid = &info->lti_fid2;
3280 struct lfsck_instance *lfsck = com->lc_lfsck;
3281 struct lfsck_namespace *ns = com->lc_file_ram;
3282 struct dt_object *parent = NULL;
3283 struct linkea_data ldata = { 0 };
3284 bool repaired = false;
3289 dt_read_lock(env, child, 0);
3290 if (unlikely(lfsck_is_dead_obj(child))) {
3291 dt_read_unlock(env, child);
3296 if (S_ISDIR(lfsck_object_type(child))) {
3297 dt_read_unlock(env, child);
3298 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3303 rc = lfsck_links_read(env, child, &ldata);
3304 dt_read_unlock(env, child);
3308 linkea_first_entry(&ldata);
3309 while (ldata.ld_lee != NULL) {
3310 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3312 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3314 /* Found repeated linkEA entries */
3316 rc = lfsck_namespace_shrink_linkea(env, com, child,
3317 &ldata, cname, pfid, false);
3329 /* Invalid PFID in the linkEA entry. */
3330 if (!fid_is_sane(pfid)) {
3331 rc = lfsck_namespace_shrink_linkea(env, com, child,
3332 &ldata, cname, pfid, true);
3342 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3344 GOTO(out, rc = PTR_ERR(parent));
3346 if (!dt_object_exists(parent)) {
3349 if (ldata.ld_leh->leh_reccount > 1) {
3350 /* If it is NOT the last linkEA entry, then
3351 * there is still other chance to make the
3352 * child to be visible via other parent, then
3353 * remove this linkEA entry. */
3354 rc = lfsck_namespace_shrink_linkea(env, com,
3355 child, &ldata, cname, pfid, true);
3357 /* If the LFSCK is marked as LF_INCOMPLETE,
3358 * then means some MDT has ever tried to
3359 * verify some remote MDT-object that resides
3360 * on this MDT, but this MDT failed to respond
3361 * such request. So means there may be some
3362 * remote name entry on other MDT that
3363 * references this object with another name,
3364 * so we cannot know whether this linkEA is
3365 * valid or not. So keep it there and maybe
3366 * resolved when next LFSCK run. */
3367 if (ns->ln_flags & LF_INCOMPLETE) {
3368 lfsck_object_put(env, parent);
3373 /* Create the lost parent as an orphan. */
3374 rc = lfsck_namespace_create_orphan(env, com,
3377 lfsck_object_put(env, parent);
3385 /* Add the missing name entry to the parent. */
3386 rc = lfsck_namespace_insert_normal(env, com,
3387 parent, child, cname->ln_name);
3388 if (unlikely(rc == -EEXIST))
3389 /* Unfortunately, someone reused the
3390 * name under the parent by race. So we
3391 * have to remove the linkEA entry from
3392 * current child object. It means that
3393 * the LFSCK cannot recover the system
3394 * totally back to its original status,
3395 * but it is necessary to make the
3396 * current system to be consistent. */
3397 rc = lfsck_namespace_shrink_linkea(env,
3401 linkea_next_entry(&ldata);
3404 lfsck_object_put(env, parent);
3414 /* The linkEA entry with bad parent will be removed. */
3415 if (unlikely(!dt_try_as_dir(env, parent))) {
3416 lfsck_object_put(env, parent);
3417 rc = lfsck_namespace_shrink_linkea(env, com, child,
3418 &ldata, cname, pfid, true);
3428 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3429 (const struct dt_key *)cname->ln_name,
3431 if (rc != 0 && rc != -ENOENT) {
3432 lfsck_object_put(env, parent);
3438 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3439 /* It is the most common case that we
3440 * find the name entry corresponding
3441 * to the linkEA entry. */
3442 lfsck_object_put(env, parent);
3443 linkea_next_entry(&ldata);
3445 /* The name entry references another
3446 * MDT-object that may be created by
3447 * the LFSCK for repairing dangling
3448 * name entry. Try to replace it. */
3449 rc = lfsck_namespace_replace_cond(env, com,
3450 parent, child, cfid, cname);
3451 lfsck_object_put(env, parent);
3457 linkea_next_entry(&ldata);
3459 rc = lfsck_namespace_shrink_linkea(env,
3473 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3477 /* If there is no name entry in the parent dir and the object
3478 * link count is less than the linkea entries count, then the
3479 * linkea entry should be removed. */
3480 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3481 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3482 parent, child, &ldata, cname, pfid);
3483 lfsck_object_put(env, parent);
3493 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3494 * MDT has ever tried to verify some remote MDT-object that
3495 * resides on this MDT, but this MDT failed to respond such
3496 * request. So means there may be some remote name entry on
3497 * other MDT that references this object with another name,
3498 * so we cannot know whether this linkEA is valid or not.
3499 * So keep it there and maybe resolved when next LFSCK run. */
3500 if (ns->ln_flags & LF_INCOMPLETE) {
3501 lfsck_object_put(env, parent);
3506 rc = lfsck_namespace_check_name(env, parent, child, cname);
3511 lfsck_object_put(env, parent);
3516 /* It is an invalid name entry, drop it. */
3517 if (unlikely(rc > 0)) {
3518 lfsck_object_put(env, parent);
3519 rc = lfsck_namespace_shrink_linkea(env, com, child,
3520 &ldata, cname, pfid, true);
3530 /* Add the missing name entry back to the namespace. */
3531 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3533 if (unlikely(rc == -ESTALE))
3534 /* It may happen when the remote object has been
3535 * removed, but the local MDT is not aware of that. */
3538 if (unlikely(rc == -EEXIST))
3539 /* Unfortunately, someone reused the name under the
3540 * parent by race. So we have to remove the linkEA
3541 * entry from current child object. It means that the
3542 * LFSCK cannot recover the system totally back to
3543 * its original status, but it is necessary to make
3544 * the current system to be consistent.
3546 * It also may be because of the LFSCK found some
3547 * internal status of create operation. Under such
3548 * case, nothing to be done. */
3549 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3550 parent, child, &ldata, cname, pfid);
3552 linkea_next_entry(&ldata);
3554 lfsck_object_put(env, parent);
3565 if (rc < 0 && rc != -ENODATA)
3569 LASSERT(ldata.ld_leh != NULL);
3571 count = ldata.ld_leh->leh_reccount;
3574 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3575 * MDT has ever tried to verify some remote MDT-object that
3576 * resides on this MDT, but this MDT failed to respond such
3577 * request. So means there may be some remote name entry on
3578 * other MDT that references this object with another name,
3579 * so we cannot know whether this linkEA is valid or not.
3580 * So keep it there and maybe resolved when next LFSCK run. */
3581 if (count == 0 && !(ns->ln_flags & LF_INCOMPLETE)) {
3582 /* If the child becomes orphan, then insert it into
3583 * the global .lustre/lost+found/MDTxxxx directory. */
3584 rc = lfsck_namespace_insert_orphan(env, com, child, "", "O",
3590 ns->ln_mul_ref_repaired++;
3595 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3599 if (la->la_nlink != count) {
3600 rc = lfsck_namespace_repair_nlink(env, com, child,
3603 ns->ln_objs_nlink_repaired++;
3609 if (la->la_nlink > 1)
3610 ns->ln_mul_linked_repaired++;
3619 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3620 struct lfsck_namespace *ns,
3621 __u64 checked_phase1,
3622 __u64 checked_phase2,
3626 seq_printf(m, "checked_phase1: "LPU64"\n"
3627 "checked_phase2: "LPU64"\n"
3628 "updated_phase1: "LPU64"\n"
3629 "updated_phase2: "LPU64"\n"
3630 "failed_phase1: "LPU64"\n"
3631 "failed_phase2: "LPU64"\n"
3632 "directories: "LPU64"\n"
3633 "dirent_repaired: "LPU64"\n"
3634 "linkea_repaired: "LPU64"\n"
3635 "nlinks_repaired: "LPU64"\n"
3636 "multiple_linked_checked: "LPU64"\n"
3637 "multiple_linked_repaired: "LPU64"\n"
3638 "unknown_inconsistency: "LPU64"\n"
3639 "unmatched_pairs_repaired: "LPU64"\n"
3640 "dangling_repaired: "LPU64"\n"
3641 "multiple_referenced_repaired: "LPU64"\n"
3642 "bad_file_type_repaired: "LPU64"\n"
3643 "lost_dirent_repaired: "LPU64"\n"
3644 "local_lost_found_scanned: "LPU64"\n"
3645 "local_lost_found_moved: "LPU64"\n"
3646 "local_lost_found_skipped: "LPU64"\n"
3647 "local_lost_found_failed: "LPU64"\n"
3648 "striped_dirs_scanned: "LPU64"\n"
3649 "striped_dirs_repaired: "LPU64"\n"
3650 "striped_dirs_failed: "LPU64"\n"
3651 "striped_dirs_disabled: "LPU64"\n"
3652 "striped_dirs_skipped: "LPU64"\n"
3653 "striped_shards_scanned: "LPU64"\n"
3654 "striped_shards_repaired: "LPU64"\n"
3655 "striped_shards_failed: "LPU64"\n"
3656 "striped_shards_skipped: "LPU64"\n"
3657 "name_hash_repaired: "LPU64"\n"
3658 "success_count: %u\n"
3659 "run_time_phase1: %u seconds\n"
3660 "run_time_phase2: %u seconds\n",
3663 ns->ln_items_repaired,
3664 ns->ln_objs_repaired_phase2,
3665 ns->ln_items_failed,
3666 ns->ln_objs_failed_phase2,
3667 ns->ln_dirs_checked,
3668 ns->ln_dirent_repaired,
3669 ns->ln_linkea_repaired,
3670 ns->ln_objs_nlink_repaired,
3671 ns->ln_mul_linked_checked,
3672 ns->ln_mul_linked_repaired,
3673 ns->ln_unknown_inconsistency,
3674 ns->ln_unmatched_pairs_repaired,
3675 ns->ln_dangling_repaired,
3676 ns->ln_mul_ref_repaired,
3677 ns->ln_bad_type_repaired,
3678 ns->ln_lost_dirent_repaired,
3679 ns->ln_local_lpf_scanned,
3680 ns->ln_local_lpf_moved,
3681 ns->ln_local_lpf_skipped,
3682 ns->ln_local_lpf_failed,
3683 ns->ln_striped_dirs_scanned,
3684 ns->ln_striped_dirs_repaired,
3685 ns->ln_striped_dirs_failed,
3686 ns->ln_striped_dirs_disabled,
3687 ns->ln_striped_dirs_skipped,
3688 ns->ln_striped_shards_scanned,
3689 ns->ln_striped_shards_repaired,
3690 ns->ln_striped_shards_failed,
3691 ns->ln_striped_shards_skipped,
3692 ns->ln_name_hash_repaired,
3693 ns->ln_success_count,
3698 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3699 struct lfsck_component *com)
3701 struct lfsck_instance *lfsck = com->lc_lfsck;
3702 struct lfsck_namespace *ns = com->lc_file_ram;
3704 while (!list_empty(&lfsck->li_list_lmv)) {
3705 struct lfsck_lmv_unit *llu;
3706 struct lfsck_lmv *llmv;
3708 llu = list_entry(lfsck->li_list_lmv.next,
3709 struct lfsck_lmv_unit, llu_link);
3710 llmv = &llu->llu_lmv;
3712 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3713 "still in using: %u\n",
3714 atomic_read(&llmv->ll_ref));
3716 ns->ln_striped_dirs_skipped++;
3717 lfsck_lmv_put(env, llmv);
3721 /* namespace APIs */
3723 static int lfsck_namespace_reset(const struct lu_env *env,
3724 struct lfsck_component *com, bool init)
3726 struct lfsck_instance *lfsck = com->lc_lfsck;
3727 struct lfsck_namespace *ns = com->lc_file_ram;
3728 struct lfsck_assistant_data *lad = com->lc_data;
3729 struct dt_object *root;
3730 struct dt_object *dto;
3734 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3736 GOTO(log, rc = PTR_ERR(root));
3738 if (unlikely(!dt_try_as_dir(env, root)))
3739 GOTO(put, rc = -ENOTDIR);
3741 down_write(&com->lc_sem);
3743 memset(ns, 0, sizeof(*ns));
3745 __u32 count = ns->ln_success_count;
3746 __u64 last_time = ns->ln_time_last_complete;
3748 memset(ns, 0, sizeof(*ns));
3749 ns->ln_success_count = count;
3750 ns->ln_time_last_complete = last_time;
3752 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3753 ns->ln_status = LS_INIT;
3755 rc = local_object_unlink(env, lfsck->li_bottom, root,
3756 lfsck_namespace_name);
3760 lfsck_object_put(env, com->lc_obj);
3762 dto = local_index_find_or_create(env, lfsck->li_los, root,
3763 lfsck_namespace_name,
3764 S_IFREG | S_IRUGO | S_IWUSR,
3765 &dt_lfsck_features);
3767 GOTO(out, rc = PTR_ERR(dto));
3770 rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
3774 lad->lad_incomplete = 0;
3775 CFS_RESET_BITMAP(lad->lad_bitmap);
3777 rc = lfsck_namespace_store(env, com, true);
3782 up_write(&com->lc_sem);
3785 lu_object_put(env, &root->do_lu);
3787 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3788 lfsck_lfsck2name(lfsck), rc);
3793 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3796 struct lfsck_namespace *ns = com->lc_file_ram;
3798 down_write(&com->lc_sem);
3800 com->lc_new_checked++;
3801 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3802 up_write(&com->lc_sem);
3805 static void lfsck_namespace_close_dir(const struct lu_env *env,
3806 struct lfsck_component *com)
3808 struct lfsck_namespace *ns = com->lc_file_ram;
3809 struct lfsck_assistant_data *lad = com->lc_data;
3810 struct lfsck_instance *lfsck = com->lc_lfsck;
3811 struct lfsck_lmv *llmv = lfsck->li_lmv;
3812 struct lfsck_namespace_req *lnr;
3814 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3815 bool wakeup = false;
3821 OBD_ALLOC(lnr, size);
3823 ns->ln_striped_dirs_skipped++;
3828 /* Generate a dummy request to indicate that all shards' name entry
3829 * in this striped directory has been scanned for the first time. */
3830 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3831 lnr->lnr_obj = lfsck_object_get(lfsck->li_obj_dir);
3832 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3833 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3834 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3835 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3836 lnr->lnr_size = size;
3838 spin_lock(&lad->lad_lock);
3839 if (lad->lad_assistant_status < 0) {
3840 spin_unlock(&lad->lad_lock);
3841 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3842 ns->ln_striped_dirs_skipped++;
3847 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3848 if (lad->lad_prefetched == 0)
3851 lad->lad_prefetched++;
3852 spin_unlock(&lad->lad_lock);
3854 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3859 static int lfsck_namespace_open_dir(const struct lu_env *env,
3860 struct lfsck_component *com)
3862 struct lfsck_instance *lfsck = com->lc_lfsck;
3863 struct lfsck_namespace *ns = com->lc_file_ram;
3864 struct lfsck_lmv *llmv = lfsck->li_lmv;
3871 if (llmv->ll_lmv_master) {
3872 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3874 if (lmv->lmv_master_mdt_index !=
3875 lfsck_dev_idx(lfsck->li_bottom)) {
3876 lmv->lmv_master_mdt_index =
3877 lfsck_dev_idx(lfsck->li_bottom);
3878 ns->ln_flags |= LF_INCONSISTENT;
3879 llmv->ll_lmv_updated = 1;
3882 rc = lfsck_namespace_verify_stripe_slave(env, com,
3883 lfsck->li_obj_dir, llmv);
3886 RETURN(rc > 0 ? 0 : rc);
3889 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3890 struct lfsck_component *com, bool init)
3892 struct lfsck_instance *lfsck = com->lc_lfsck;
3893 struct lfsck_namespace *ns = com->lc_file_ram;
3897 rc = lfsck_checkpoint_generic(env, com);
3902 down_write(&com->lc_sem);
3904 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3906 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3907 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3908 HALF_SEC - lfsck->li_time_last_checkpoint);
3909 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3910 ns->ln_items_checked += com->lc_new_checked;
3911 com->lc_new_checked = 0;
3914 rc = lfsck_namespace_store(env, com, false);
3915 up_write(&com->lc_sem);
3918 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3919 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3920 lfsck->li_pos_current.lp_oit_cookie,
3921 PFID(&lfsck->li_pos_current.lp_dir_parent),
3922 lfsck->li_pos_current.lp_dir_cookie, rc);
3924 return rc > 0 ? 0 : rc;
3927 static int lfsck_namespace_prep(const struct lu_env *env,
3928 struct lfsck_component *com,
3929 struct lfsck_start_param *lsp)
3931 struct lfsck_instance *lfsck = com->lc_lfsck;
3932 struct lfsck_namespace *ns = com->lc_file_ram;
3933 struct lfsck_position *pos = &com->lc_pos_start;
3936 rc = lfsck_namespace_load_bitmap(env, com);
3937 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3938 rc = lfsck_namespace_reset(env, com, false);
3940 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3943 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3944 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3950 down_write(&com->lc_sem);
3951 ns->ln_time_latest_start = cfs_time_current_sec();
3952 spin_lock(&lfsck->li_lock);
3954 if (ns->ln_flags & LF_SCANNED_ONCE) {
3955 if (!lfsck->li_drop_dryrun ||
3956 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3957 ns->ln_status = LS_SCANNING_PHASE2;
3958 list_move_tail(&com->lc_link,
3959 &lfsck->li_list_double_scan);
3960 if (!list_empty(&com->lc_link_dir))
3961 list_del_init(&com->lc_link_dir);
3962 lfsck_pos_set_zero(pos);
3964 ns->ln_status = LS_SCANNING_PHASE1;
3965 ns->ln_run_time_phase1 = 0;
3966 ns->ln_run_time_phase2 = 0;
3967 ns->ln_items_checked = 0;
3968 ns->ln_items_repaired = 0;
3969 ns->ln_items_failed = 0;
3970 ns->ln_dirs_checked = 0;
3971 ns->ln_objs_checked_phase2 = 0;
3972 ns->ln_objs_repaired_phase2 = 0;
3973 ns->ln_objs_failed_phase2 = 0;
3974 ns->ln_objs_nlink_repaired = 0;
3975 ns->ln_dirent_repaired = 0;
3976 ns->ln_linkea_repaired = 0;
3977 ns->ln_mul_linked_checked = 0;
3978 ns->ln_mul_linked_repaired = 0;
3979 ns->ln_unknown_inconsistency = 0;
3980 ns->ln_unmatched_pairs_repaired = 0;
3981 ns->ln_dangling_repaired = 0;
3982 ns->ln_mul_ref_repaired = 0;
3983 ns->ln_bad_type_repaired = 0;
3984 ns->ln_lost_dirent_repaired = 0;
3985 ns->ln_striped_dirs_scanned = 0;
3986 ns->ln_striped_dirs_repaired = 0;
3987 ns->ln_striped_dirs_failed = 0;
3988 ns->ln_striped_dirs_disabled = 0;
3989 ns->ln_striped_dirs_skipped = 0;
3990 ns->ln_striped_shards_scanned = 0;
3991 ns->ln_striped_shards_repaired = 0;
3992 ns->ln_striped_shards_failed = 0;
3993 ns->ln_striped_shards_skipped = 0;
3994 ns->ln_name_hash_repaired = 0;
3995 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3996 if (list_empty(&com->lc_link_dir))
3997 list_add_tail(&com->lc_link_dir,
3998 &lfsck->li_list_dir);
3999 *pos = ns->ln_pos_first_inconsistent;
4002 ns->ln_status = LS_SCANNING_PHASE1;
4003 if (list_empty(&com->lc_link_dir))
4004 list_add_tail(&com->lc_link_dir,
4005 &lfsck->li_list_dir);
4006 if (!lfsck->li_drop_dryrun ||
4007 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4008 *pos = ns->ln_pos_last_checkpoint;
4009 pos->lp_oit_cookie++;
4011 *pos = ns->ln_pos_first_inconsistent;
4015 spin_unlock(&lfsck->li_lock);
4016 up_write(&com->lc_sem);
4018 rc = lfsck_start_assistant(env, com, lsp);
4020 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
4021 DFID", "LPX64"]: rc = %d\n",
4022 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4023 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4028 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4029 struct lfsck_component *com,
4030 struct dt_object *obj)
4032 struct lfsck_thread_info *info = lfsck_env_info(env);
4033 struct lfsck_namespace *ns = com->lc_file_ram;
4034 struct lfsck_instance *lfsck = com->lc_lfsck;
4035 const struct lu_fid *fid = lfsck_dto2fid(obj);
4036 struct lu_attr *la = &info->lti_la;
4037 struct lu_fid *pfid = &info->lti_fid2;
4038 struct lu_name *cname = &info->lti_name;
4039 struct lu_seq_range *range = &info->lti_range;
4040 struct dt_device *dev = lfsck->li_bottom;
4041 struct seq_server_site *ss =
4042 lu_site2seq(dev->dd_lu_dev.ld_site);
4043 struct linkea_data ldata = { 0 };
4044 __u32 idx = lfsck_dev_idx(dev);
4048 rc = lfsck_links_read(env, obj, &ldata);
4052 /* -EINVAL means crashed linkEA, should be verified. */
4053 if (rc == -EINVAL) {
4054 rc = lfsck_namespace_trace_update(env, com, fid,
4055 LNTF_CHECK_LINKEA, true);
4057 struct lustre_handle lh = { 0 };
4059 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4060 MDS_INODELOCK_UPDATE |
4061 MDS_INODELOCK_XATTR, LCK_EX);
4063 rc = lfsck_namespace_links_remove(env, com,
4065 lfsck_ibits_unlock(&lh, LCK_EX);
4069 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4072 /* zero-linkEA object may be orphan, but it also maybe because
4073 * of upgrading. Currently, we cannot record it for double scan.
4074 * Because it may cause the LFSCK trace file to be too large. */
4075 if (rc == -ENODATA) {
4076 if (S_ISDIR(lfsck_object_type(obj)))
4079 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4083 /* "la_ctime" == 1 means that it has ever been removed from
4084 * backend /lost+found directory but not been added back to
4085 * the normal namespace yet. */
4086 if (la->la_nlink > 1 || unlikely(la->la_ctime == 1))
4087 rc = lfsck_namespace_trace_update(env, com, fid,
4088 LNTF_CHECK_LINKEA, true);
4096 /* Record multiple-linked object. */
4097 if (ldata.ld_leh->leh_reccount > 1) {
4098 rc = lfsck_namespace_trace_update(env, com, fid,
4099 LNTF_CHECK_LINKEA, true);
4104 linkea_first_entry(&ldata);
4105 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4106 if (!fid_is_sane(pfid)) {
4107 rc = lfsck_namespace_trace_update(env, com, fid,
4108 LNTF_CHECK_PARENT, true);
4110 fld_range_set_mdt(range);
4111 rc = fld_local_lookup(env, ss->ss_server_fld,
4112 fid_seq(pfid), range);
4113 if ((rc == -ENOENT) ||
4114 (rc == 0 && range->lsr_index != idx)) {
4115 rc = lfsck_namespace_trace_update(env, com, fid,
4116 LNTF_CHECK_LINKEA, true);
4118 if (S_ISDIR(lfsck_object_type(obj)))
4121 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
4125 /* "la_ctime" == 1 means that it has ever been
4126 * removed from backend /lost+found directory but
4127 * not been added back to the normal namespace yet. */
4128 if (la->la_nlink > 1 || unlikely(la->la_ctime == 1))
4129 rc = lfsck_namespace_trace_update(env, com,
4130 fid, LNTF_CHECK_LINKEA, true);
4137 down_write(&com->lc_sem);
4138 com->lc_new_checked++;
4139 if (S_ISDIR(lfsck_object_type(obj)))
4140 ns->ln_dirs_checked++;
4142 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4143 up_write(&com->lc_sem);
4148 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4149 struct lfsck_component *com,
4150 struct lu_dirent *ent, __u16 type)
4152 struct lfsck_assistant_data *lad = com->lc_data;
4153 struct lfsck_namespace_req *lnr;
4154 bool wakeup = false;
4156 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4158 struct lfsck_namespace *ns = com->lc_file_ram;
4160 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4161 return PTR_ERR(lnr);
4164 spin_lock(&lad->lad_lock);
4165 if (lad->lad_assistant_status < 0) {
4166 spin_unlock(&lad->lad_lock);
4167 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4168 return lad->lad_assistant_status;
4171 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4172 if (lad->lad_prefetched == 0)
4175 lad->lad_prefetched++;
4176 spin_unlock(&lad->lad_lock);
4178 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4180 down_write(&com->lc_sem);
4181 com->lc_new_checked++;
4182 up_write(&com->lc_sem);
4187 static int lfsck_namespace_post(const struct lu_env *env,
4188 struct lfsck_component *com,
4189 int result, bool init)
4191 struct lfsck_instance *lfsck = com->lc_lfsck;
4192 struct lfsck_namespace *ns = com->lc_file_ram;
4196 lfsck_post_generic(env, com, &result);
4198 down_write(&com->lc_sem);
4199 lfsck_namespace_release_lmv(env, com);
4201 spin_lock(&lfsck->li_lock);
4203 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4205 ns->ln_status = LS_SCANNING_PHASE2;
4206 ns->ln_flags |= LF_SCANNED_ONCE;
4207 ns->ln_flags &= ~LF_UPGRADE;
4208 list_del_init(&com->lc_link_dir);
4209 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4210 } else if (result == 0) {
4211 if (lfsck->li_status != 0)
4212 ns->ln_status = lfsck->li_status;
4214 ns->ln_status = LS_STOPPED;
4215 if (ns->ln_status != LS_PAUSED) {
4216 list_del_init(&com->lc_link_dir);
4217 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4220 ns->ln_status = LS_FAILED;
4221 list_del_init(&com->lc_link_dir);
4222 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4224 spin_unlock(&lfsck->li_lock);
4227 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4228 HALF_SEC - lfsck->li_time_last_checkpoint);
4229 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4230 ns->ln_items_checked += com->lc_new_checked;
4231 com->lc_new_checked = 0;
4234 rc = lfsck_namespace_store(env, com, false);
4235 up_write(&com->lc_sem);
4237 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4238 lfsck_lfsck2name(lfsck), rc);
4244 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4247 struct lfsck_instance *lfsck = com->lc_lfsck;
4248 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4249 struct lfsck_namespace *ns = com->lc_file_ram;
4252 down_read(&com->lc_sem);
4253 seq_printf(m, "name: lfsck_namespace\n"
4259 lfsck_status2names(ns->ln_status));
4261 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4265 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4269 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4270 "time_since_last_completed");
4274 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4275 "time_since_latest_start");
4279 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4280 "time_since_last_checkpoint");
4284 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4285 "latest_start_position");
4289 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4290 "last_checkpoint_position");
4294 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4295 "first_failure_position");
4299 if (ns->ln_status == LS_SCANNING_PHASE1) {
4300 struct lfsck_position pos;
4301 const struct dt_it_ops *iops;
4302 cfs_duration_t duration = cfs_time_current() -
4303 lfsck->li_time_last_checkpoint;
4304 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4305 __u64 speed = checked;
4306 __u64 new_checked = msecs_to_jiffies(com->lc_new_checked *
4308 __u32 rtime = ns->ln_run_time_phase1 +
4309 cfs_duration_sec(duration + HALF_SEC);
4312 do_div(new_checked, duration);
4314 do_div(speed, rtime);
4315 lfsck_namespace_dump_statistics(m, ns, checked,
4316 ns->ln_objs_checked_phase2,
4317 rtime, ns->ln_run_time_phase2);
4319 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4320 "average_speed_phase2: N/A\n"
4321 "real_time_speed_phase1: "LPU64" items/sec\n"
4322 "real_time_speed_phase2: N/A\n",
4326 LASSERT(lfsck->li_di_oit != NULL);
4328 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4330 /* The low layer otable-based iteration position may NOT
4331 * exactly match the namespace-based directory traversal
4332 * cookie. Generally, it is not a serious issue. But the
4333 * caller should NOT make assumption on that. */
4334 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4335 if (!lfsck->li_current_oit_processed)
4336 pos.lp_oit_cookie--;
4338 spin_lock(&lfsck->li_lock);
4339 if (lfsck->li_di_dir != NULL) {
4340 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4341 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4342 fid_zero(&pos.lp_dir_parent);
4343 pos.lp_dir_cookie = 0;
4346 *lfsck_dto2fid(lfsck->li_obj_dir);
4349 fid_zero(&pos.lp_dir_parent);
4350 pos.lp_dir_cookie = 0;
4352 spin_unlock(&lfsck->li_lock);
4353 lfsck_pos_dump(m, &pos, "current_position");
4354 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4355 cfs_duration_t duration = cfs_time_current() -
4356 lfsck->li_time_last_checkpoint;
4357 __u64 checked = ns->ln_objs_checked_phase2 +
4358 com->lc_new_checked;
4359 __u64 speed1 = ns->ln_items_checked;
4360 __u64 speed2 = checked;
4361 __u64 new_checked = msecs_to_jiffies(com->lc_new_checked *
4363 __u32 rtime = ns->ln_run_time_phase2 +
4364 cfs_duration_sec(duration + HALF_SEC);
4367 do_div(new_checked, duration);
4368 if (ns->ln_run_time_phase1 != 0)
4369 do_div(speed1, ns->ln_run_time_phase1);
4371 do_div(speed2, rtime);
4372 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4374 ns->ln_run_time_phase1, rtime);
4376 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4377 "average_speed_phase2: "LPU64" objs/sec\n"
4378 "real_time_speed_phase1: N/A\n"
4379 "real_time_speed_phase2: "LPU64" objs/sec\n"
4380 "current_position: "DFID"\n",
4384 PFID(&ns->ln_fid_latest_scanned_phase2));
4386 __u64 speed1 = ns->ln_items_checked;
4387 __u64 speed2 = ns->ln_objs_checked_phase2;
4389 if (ns->ln_run_time_phase1 != 0)
4390 do_div(speed1, ns->ln_run_time_phase1);
4391 if (ns->ln_run_time_phase2 != 0)
4392 do_div(speed2, ns->ln_run_time_phase2);
4393 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4394 ns->ln_objs_checked_phase2,
4395 ns->ln_run_time_phase1,
4396 ns->ln_run_time_phase2);
4398 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4399 "average_speed_phase2: "LPU64" objs/sec\n"
4400 "real_time_speed_phase1: N/A\n"
4401 "real_time_speed_phase2: N/A\n"
4402 "current_position: N/A\n",
4407 up_read(&com->lc_sem);
4411 static int lfsck_namespace_double_scan(const struct lu_env *env,
4412 struct lfsck_component *com)
4414 struct lfsck_namespace *ns = com->lc_file_ram;
4415 struct lfsck_assistant_data *lad = com->lc_data;
4416 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4417 struct lfsck_tgt_desc *ltd;
4418 struct lfsck_tgt_desc *next;
4421 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4422 if (thread_is_stopped(&lad->lad_thread)) {
4423 LASSERT(list_empty(&lad->lad_req_list));
4424 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4426 spin_lock(<ds->ltd_lock);
4427 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4428 ltd_namespace_phase_list) {
4429 list_del_init(<d->ltd_namespace_phase_list);
4431 spin_unlock(<ds->ltd_lock);
4437 static void lfsck_namespace_data_release(const struct lu_env *env,
4438 struct lfsck_component *com)
4440 struct lfsck_assistant_data *lad = com->lc_data;
4441 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4442 struct lfsck_tgt_desc *ltd;
4443 struct lfsck_tgt_desc *next;
4445 LASSERT(lad != NULL);
4446 LASSERT(thread_is_init(&lad->lad_thread) ||
4447 thread_is_stopped(&lad->lad_thread));
4448 LASSERT(list_empty(&lad->lad_req_list));
4450 com->lc_data = NULL;
4451 lfsck_namespace_release_lmv(env, com);
4453 spin_lock(<ds->ltd_lock);
4454 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4455 ltd_namespace_phase_list) {
4456 list_del_init(<d->ltd_namespace_phase_list);
4458 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4459 ltd_namespace_phase_list) {
4460 list_del_init(<d->ltd_namespace_phase_list);
4462 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4463 ltd_namespace_list) {
4464 list_del_init(<d->ltd_namespace_list);
4466 spin_unlock(<ds->ltd_lock);
4468 if (likely(lad->lad_bitmap != NULL))
4469 CFS_FREE_BITMAP(lad->lad_bitmap);
4474 static void lfsck_namespace_quit(const struct lu_env *env,
4475 struct lfsck_component *com)
4477 struct lfsck_assistant_data *lad = com->lc_data;
4478 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4479 struct lfsck_tgt_desc *ltd;
4480 struct lfsck_tgt_desc *next;
4482 LASSERT(lad != NULL);
4484 lfsck_quit_generic(env, com);
4486 LASSERT(thread_is_init(&lad->lad_thread) ||
4487 thread_is_stopped(&lad->lad_thread));
4488 LASSERT(list_empty(&lad->lad_req_list));
4490 lfsck_namespace_release_lmv(env, com);
4492 spin_lock(<ds->ltd_lock);
4493 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4494 ltd_namespace_phase_list) {
4495 list_del_init(<d->ltd_namespace_phase_list);
4497 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4498 ltd_namespace_phase_list) {
4499 list_del_init(<d->ltd_namespace_phase_list);
4501 spin_unlock(<ds->ltd_lock);
4504 static int lfsck_namespace_in_notify(const struct lu_env *env,
4505 struct lfsck_component *com,
4506 struct lfsck_request *lr,
4509 struct lfsck_instance *lfsck = com->lc_lfsck;
4510 struct lfsck_namespace *ns = com->lc_file_ram;
4511 struct lfsck_assistant_data *lad = com->lc_data;
4512 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4513 struct lfsck_tgt_desc *ltd;
4518 switch (lr->lr_event) {
4519 case LE_CREATE_ORPHAN: {
4520 struct dt_object *orphan = NULL;
4521 struct lmv_mds_md_v1 *lmv;
4523 CDEBUG(D_LFSCK, "%s: namespace LFSCK handling notify from "
4524 "MDT %x to create orphan"DFID" with type %o\n",
4525 lfsck_lfsck2name(lfsck), lr->lr_index,
4526 PFID(&lr->lr_fid), lr->lr_type);
4528 orphan = lfsck_object_find(env, lfsck, &lr->lr_fid);
4530 GOTO(out_create, rc = PTR_ERR(orphan));
4532 if (dt_object_exists(orphan))
4533 GOTO(out_create, rc = -EEXIST);
4535 if (lr->lr_stripe_count > 0) {
4536 lmv = &lfsck_env_info(env)->lti_lmv;
4537 memset(lmv, 0, sizeof(*lmv));
4538 lmv->lmv_hash_type = lr->lr_hash_type;
4539 lmv->lmv_stripe_count = lr->lr_stripe_count;
4540 lmv->lmv_layout_version = lr->lr_layout_version;
4541 memcpy(lmv->lmv_pool_name, lr->lr_pool_name,
4542 sizeof(lmv->lmv_pool_name));
4547 rc = lfsck_namespace_create_orphan_local(env, com, orphan,
4550 GOTO(out_create, rc = (rc == 1) ? 0 : rc);
4553 CDEBUG(D_LFSCK, "%s: namespace LFSCK handled notify from "
4554 "MDT %x to create orphan"DFID" with type %o: rc = %d\n",
4555 lfsck_lfsck2name(lfsck), lr->lr_index,
4556 PFID(&lr->lr_fid), lr->lr_type, rc);
4558 if (orphan != NULL && !IS_ERR(orphan))
4559 lfsck_object_put(env, orphan);
4563 case LE_SKIP_NLINK_DECLARE: {
4564 struct dt_object *obj = com->lc_obj;
4565 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4568 LASSERT(th != NULL);
4570 rc = dt_declare_delete(env, obj,
4571 (const struct dt_key *)key, th);
4573 rc = dt_declare_insert(env, obj,
4574 (const struct dt_rec *)&flags,
4575 (const struct dt_key *)key, th);
4579 case LE_SKIP_NLINK: {
4580 struct dt_object *obj = com->lc_obj;
4581 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4586 LASSERT(th != NULL);
4588 fid_cpu_to_be(key, &lr->lr_fid);
4589 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4590 (const struct dt_key *)key, BYPASS_CAPA);
4592 if (flags & LNTF_SKIP_NLINK)
4596 } else if (rc != -ENOENT) {
4600 flags |= LNTF_SKIP_NLINK;
4602 rc = dt_delete(env, obj, (const struct dt_key *)key,
4608 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4609 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4614 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4615 " to be skipped for namespace double scan: rc = %d\n",
4616 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4619 /* If we cannot record this object in the LFSCK tracing,
4620 * we have to mark the LFSC as LF_INCOMPLETE, then the
4621 * LFSCK will skip nlink attribute verification for
4623 ns->ln_flags |= LF_INCOMPLETE;
4627 case LE_SET_LMV_MASTER: {
4628 struct dt_object *obj;
4630 obj = lfsck_object_find_by_dev(env, lfsck->li_bottom,
4633 RETURN(PTR_ERR(obj));
4635 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4636 lfsck_object_put(env, obj);
4638 RETURN(rc > 0 ? 0 : rc);
4640 case LE_SET_LMV_SLAVE: {
4641 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4642 ns->ln_striped_shards_repaired++;
4644 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4645 LNTF_RECHECK_NAME_HASH, true);
4647 RETURN(rc > 0 ? 0 : rc);
4649 case LE_PHASE1_DONE:
4650 case LE_PHASE2_DONE:
4657 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4658 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4659 lr->lr_index, lr->lr_status, lr->lr_flags2);
4661 spin_lock(<ds->ltd_lock);
4662 ltd = LTD_TGT(ltds, lr->lr_index);
4664 spin_unlock(<ds->ltd_lock);
4669 list_del_init(<d->ltd_namespace_phase_list);
4670 switch (lr->lr_event) {
4671 case LE_PHASE1_DONE:
4672 if (lr->lr_status <= 0) {
4673 ltd->ltd_namespace_done = 1;
4674 list_del_init(<d->ltd_namespace_list);
4675 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4676 "phase1 for namespace LFSCK: rc = %d.\n",
4677 lfsck_lfsck2name(lfsck),
4678 ltd->ltd_index, lr->lr_status);
4679 ns->ln_flags |= LF_INCOMPLETE;
4684 if (lr->lr_flags2 & LF_INCOMPLETE)
4685 ns->ln_flags |= LF_INCOMPLETE;
4687 if (list_empty(<d->ltd_namespace_list))
4688 list_add_tail(<d->ltd_namespace_list,
4689 &lad->lad_mdt_list);
4690 list_add_tail(<d->ltd_namespace_phase_list,
4691 &lad->lad_mdt_phase2_list);
4693 case LE_PHASE2_DONE:
4694 ltd->ltd_namespace_done = 1;
4695 list_del_init(<d->ltd_namespace_list);
4699 ltd->ltd_namespace_done = 1;
4700 list_del_init(<d->ltd_namespace_list);
4701 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4703 "%s: the peer MDT %x exit namespace LFSCK\n",
4704 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4705 ns->ln_flags |= LF_INCOMPLETE;
4711 spin_unlock(<ds->ltd_lock);
4713 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4714 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4716 memset(stop, 0, sizeof(*stop));
4717 stop->ls_status = lr->lr_status;
4718 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4719 lfsck_stop(env, lfsck->li_bottom, stop);
4720 } else if (lfsck_phase2_next_ready(lad)) {
4721 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4727 static int lfsck_namespace_query(const struct lu_env *env,
4728 struct lfsck_component *com)
4730 struct lfsck_namespace *ns = com->lc_file_ram;
4732 return ns->ln_status;
4735 static struct lfsck_operations lfsck_namespace_ops = {
4736 .lfsck_reset = lfsck_namespace_reset,
4737 .lfsck_fail = lfsck_namespace_fail,
4738 .lfsck_close_dir = lfsck_namespace_close_dir,
4739 .lfsck_open_dir = lfsck_namespace_open_dir,
4740 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4741 .lfsck_prep = lfsck_namespace_prep,
4742 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4743 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4744 .lfsck_post = lfsck_namespace_post,
4745 .lfsck_dump = lfsck_namespace_dump,
4746 .lfsck_double_scan = lfsck_namespace_double_scan,
4747 .lfsck_data_release = lfsck_namespace_data_release,
4748 .lfsck_quit = lfsck_namespace_quit,
4749 .lfsck_in_notify = lfsck_namespace_in_notify,
4750 .lfsck_query = lfsck_namespace_query,
4754 * Repair dangling name entry.
4756 * For the name entry with dangling reference, we need to repare the
4757 * inconsistency according to the LFSCK sponsor's requirement:
4759 * 1) Keep the inconsistency there and report the inconsistency case,
4760 * then give the chance to the application to find related issues,
4761 * and the users can make the decision about how to handle it with
4762 * more human knownledge. (by default)
4764 * 2) Re-create the missing MDT-object with the FID information.
4766 * \param[in] env pointer to the thread context
4767 * \param[in] com pointer to the lfsck component
4768 * \param[in] child pointer to the object corresponding to the dangling
4770 * \param[in] lnr pointer to the namespace request that contains the
4771 * name's name, parent object, parent's LMV, and ect.
4773 * \retval positive number if no need to repair
4774 * \retval zero for repaired successfully
4775 * \retval negative error number on failure
4777 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4778 struct lfsck_component *com,
4779 struct dt_object *child,
4780 struct lfsck_namespace_req *lnr)
4782 struct lfsck_thread_info *info = lfsck_env_info(env);
4783 struct lu_attr *la = &info->lti_la;
4784 struct dt_allocation_hint *hint = &info->lti_hint;
4785 struct dt_object_format *dof = &info->lti_dof;
4786 struct dt_insert_rec *rec = &info->lti_dt_rec;
4787 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4788 struct dt_object *parent = lnr->lnr_obj;
4789 const struct lu_name *cname;
4790 struct linkea_data ldata = { 0 };
4791 struct lustre_handle lh = { 0 };
4792 struct lu_buf linkea_buf;
4793 struct lu_buf lmv_buf;
4794 struct lfsck_instance *lfsck = com->lc_lfsck;
4795 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4796 struct dt_device *dev = lfsck_obj2dt_dev(child);
4797 struct thandle *th = NULL;
4799 __u16 type = lnr->lnr_type;
4803 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4804 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4809 if (!create || bk->lb_param & LPF_DRYRUN)
4812 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4816 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
4820 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4821 MDS_INODELOCK_UPDATE, LCK_EX);
4825 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4829 th = dt_trans_create(env, dev);
4831 GOTO(log, rc = PTR_ERR(th));
4833 /* Set the ctime as zero, then others can know it is created for
4834 * repairing dangling name entry by LFSCK. And if the LFSCK made
4835 * wrong decision and the real MDT-object has been found later,
4836 * then the LFSCK has chance to fix the incosistency properly. */
4837 memset(la, 0, sizeof(*la));
4838 la->la_mode = (type & S_IFMT) | 0600;
4839 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4840 LA_ATIME | LA_MTIME | LA_CTIME;
4842 child->do_ops->do_ah_init(env, hint, parent, child,
4843 la->la_mode & S_IFMT);
4845 memset(dof, 0, sizeof(*dof));
4846 dof->dof_type = dt_mode_to_dft(type);
4847 /* If the target is a regular file, then the LFSCK will only create
4848 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4849 * OST-objects will be created when write open. */
4851 /* 1a. create child. */
4852 rc = dt_declare_create(env, child, la, hint, dof, th);
4856 if (S_ISDIR(type)) {
4857 if (unlikely(!dt_try_as_dir(env, child)))
4858 GOTO(stop, rc = -ENOTDIR);
4860 /* 2a. insert dot into child dir */
4861 rec->rec_type = S_IFDIR;
4862 rec->rec_fid = lfsck_dto2fid(child);
4863 rc = dt_declare_insert(env, child,
4864 (const struct dt_rec *)rec,
4865 (const struct dt_key *)dot, th);
4869 /* 3a. insert dotdot into child dir */
4870 rec->rec_fid = lfsck_dto2fid(parent);
4871 rc = dt_declare_insert(env, child,
4872 (const struct dt_rec *)rec,
4873 (const struct dt_key *)dotdot, th);
4877 /* 4a. increase child nlink */
4878 rc = dt_declare_ref_add(env, child, th);
4882 /* 5a. generate slave LMV EA. */
4883 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4886 idx = lfsck_shard_name_to_index(env,
4887 lnr->lnr_name, lnr->lnr_namelen,
4888 type, lfsck_dto2fid(child));
4889 if (unlikely(idx < 0))
4890 GOTO(stop, rc = idx);
4892 *lmv2 = lnr->lnr_lmv->ll_lmv;
4893 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4894 lmv2->lmv_master_mdt_index = idx;
4896 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4897 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4898 rc = dt_declare_xattr_set(env, child, &lmv_buf,
4899 XATTR_NAME_LMV, 0, th);
4905 /* 6a. insert linkEA for child */
4906 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4907 ldata.ld_leh->leh_len);
4908 rc = dt_declare_xattr_set(env, child, &linkea_buf,
4909 XATTR_NAME_LINK, 0, th);
4913 rc = dt_trans_start(env, dev, th);
4915 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4917 dt_write_lock(env, child, 0);
4918 /* 1b. create child */
4919 rc = dt_create(env, child, la, hint, dof, th);
4921 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4923 if (S_ISDIR(type)) {
4924 if (unlikely(!dt_try_as_dir(env, child)))
4925 GOTO(unlock, rc = -ENOTDIR);
4927 /* 2b. insert dot into child dir */
4928 rec->rec_type = S_IFDIR;
4929 rec->rec_fid = lfsck_dto2fid(child);
4930 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4931 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4935 /* 3b. insert dotdot into child dir */
4936 rec->rec_fid = lfsck_dto2fid(parent);
4937 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4938 (const struct dt_key *)dotdot, th,
4943 /* 4b. increase child nlink */
4944 rc = dt_ref_add(env, child, th);
4948 /* 5b. generate slave LMV EA. */
4949 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4950 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
4951 0, th, BYPASS_CAPA);
4957 /* 6b. insert linkEA for child. */
4958 rc = dt_xattr_set(env, child, &linkea_buf,
4959 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4964 dt_write_unlock(env, child);
4967 dt_trans_stop(env, dev, th);
4970 lfsck_ibits_unlock(&lh, LCK_EX);
4971 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4972 "reference for: parent "DFID", child "DFID", type %u, "
4973 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4974 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4975 type, cname->ln_name,
4976 create ? "Create the lost OST-object as required" :
4977 "Keep the MDT-object there by default", rc);
4980 struct lfsck_namespace *ns = com->lc_file_ram;
4982 ns->ln_flags |= LF_INCONSISTENT;
4988 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4989 struct lfsck_component *com,
4990 struct lfsck_assistant_req *lar)
4992 struct lfsck_thread_info *info = lfsck_env_info(env);
4993 struct lu_attr *la = &info->lti_la;
4994 struct lfsck_instance *lfsck = com->lc_lfsck;
4995 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4996 struct lfsck_namespace *ns = com->lc_file_ram;
4997 struct linkea_data ldata = { 0 };
4998 const struct lu_name *cname;
4999 struct thandle *handle = NULL;
5000 struct lfsck_namespace_req *lnr =
5001 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5002 struct dt_object *dir = lnr->lnr_obj;
5003 struct dt_object *obj = NULL;
5004 const struct lu_fid *pfid = lfsck_dto2fid(dir);
5005 struct dt_device *dev = NULL;
5006 struct lustre_handle lh = { 0 };
5007 bool repaired = false;
5008 bool dtlocked = false;
5012 bool bad_hash = false;
5016 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5019 if (lnr->lnr_attr & LUDA_UPGRADE) {
5020 ns->ln_flags |= LF_UPGRADE;
5021 ns->ln_dirent_repaired++;
5023 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5024 ns->ln_flags |= LF_INCONSISTENT;
5025 ns->ln_dirent_repaired++;
5029 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5030 if (strcmp(lnr->lnr_name, dotdot) != 0)
5033 rc = lfsck_namespace_trace_update(env, com, pfid,
5034 LNTF_CHECK_PARENT, true);
5039 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5040 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5045 if (lnr->lnr_name[0] == '.' &&
5046 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
5049 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5050 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5055 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5057 GOTO(out, rc = idx);
5059 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
5060 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5063 dev = lfsck->li_next;
5065 struct lfsck_tgt_desc *ltd;
5067 /* Usually, some local filesystem consistency verification
5068 * tools can guarantee the local namespace tree consistenct.
5069 * So the LFSCK will only verify the remote directory. */
5070 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5071 rc = lfsck_namespace_trace_update(env, com, pfid,
5072 LNTF_CHECK_PARENT, true);
5077 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5078 if (unlikely(ltd == NULL)) {
5079 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5080 "did not join the namespace LFSCK\n",
5081 lfsck_lfsck2name(lfsck), idx);
5082 lfsck_lad_set_bitmap(env, com, idx);
5084 GOTO(out, rc = -ENODEV);
5090 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5092 GOTO(out, rc = PTR_ERR(obj));
5094 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5095 if (dt_object_exists(obj) == 0) {
5098 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5100 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5101 lnr->lnr_name, lnr->lnr_namelen)) {
5102 type = LNIT_BAD_DIRENT;
5107 type = LNIT_DANGLING;
5108 rc = lfsck_namespace_repair_dangling(env, com,
5117 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5120 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5121 MDS_INODELOCK_UPDATE |
5122 MDS_INODELOCK_XATTR, LCK_EX);
5126 handle = dt_trans_create(env, dev);
5128 GOTO(out, rc = PTR_ERR(handle));
5130 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5134 rc = dt_trans_start(env, dev, handle);
5138 dt_write_lock(env, obj, 0);
5142 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5146 rc = lfsck_links_read(env, obj, &ldata);
5147 if (unlikely(rc == -ENOENT)) {
5148 if (handle != NULL) {
5149 dt_write_unlock(env, obj);
5152 dt_trans_stop(env, dev, handle);
5155 lfsck_ibits_unlock(&lh, LCK_EX);
5158 /* It may happen when the remote object has been removed,
5159 * but the local MDT is not aware of that. */
5161 } else if (rc == 0) {
5162 count = ldata.ld_leh->leh_reccount;
5163 rc = linkea_links_find(&ldata, cname, pfid);
5165 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5166 if ((lfsck_object_type(obj) & S_IFMT) !=
5168 ns->ln_flags |= LF_INCONSISTENT;
5169 type = LNIT_BAD_TYPE;
5175 ns->ln_flags |= LF_INCONSISTENT;
5177 /* If the name entry hash does not match the slave striped
5178 * directory, and the name entry does not match also, then
5179 * it is quite possible that name entry is corrupted. */
5180 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5181 lnr->lnr_name, lnr->lnr_namelen)) {
5182 type = LNIT_BAD_DIRENT;
5187 /* If the file type stored in the name entry does not match
5188 * the file type claimed by the object, and the object does
5189 * not recognize the name entry, then it is quite possible
5190 * that the name entry is corrupted. */
5191 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5192 type = LNIT_BAD_DIRENT;
5197 /* For sub-dir object, we cannot make sure whether the sub-dir
5198 * back references the parent via ".." name entry correctly or
5199 * not in the LFSCK first-stage scanning. It may be that the
5200 * (remote) sub-dir ".." name entry has no parent FID after
5201 * file-level backup/restore and its linkEA may be wrong.
5202 * So under such case, we should replace the linkEA according
5203 * to current name entry. But this needs to be done during the
5204 * LFSCK second-stage scanning. The LFSCK will record the name
5205 * entry for further possible using. */
5209 } else if (unlikely(rc == -EINVAL)) {
5210 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5211 type = LNIT_BAD_TYPE;
5214 ns->ln_flags |= LF_INCONSISTENT;
5215 /* The magic crashed, we are not sure whether there are more
5216 * corrupt data in the linkea, so remove all linkea entries. */
5220 } else if (rc == -ENODATA) {
5221 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5222 type = LNIT_BAD_TYPE;
5225 ns->ln_flags |= LF_UPGRADE;
5230 if (bk->lb_param & LPF_DRYRUN) {
5231 ns->ln_linkea_repaired++;
5237 if (!lustre_handle_is_used(&lh))
5243 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5250 rc = linkea_data_new(&ldata,
5251 &lfsck_env_info(env)->lti_linkea_buf);
5256 rc = linkea_add_buf(&ldata, cname, pfid);
5260 rc = lfsck_links_write(env, obj, &ldata, handle);
5261 if (unlikely(rc == -ENOSPC) &&
5262 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5263 if (handle != NULL) {
5264 LASSERT(dt_write_locked(env, obj));
5266 dt_write_unlock(env, obj);
5269 dt_trans_stop(env, dev, handle);
5272 lfsck_ibits_unlock(&lh, LCK_EX);
5275 rc = lfsck_namespace_trace_update(env, com,
5276 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5278 /* If we cannot record this object in the
5279 * LFSCK tracing, we have to mark the LFSCK
5280 * as LF_INCOMPLETE, then the LFSCK will
5281 * skip nlink attribute verification for
5283 ns->ln_flags |= LF_INCOMPLETE;
5291 count = ldata.ld_leh->leh_reccount;
5292 if (!S_ISDIR(lfsck_object_type(obj)) ||
5293 !dt_object_remote(obj)) {
5294 ns->ln_linkea_repaired++;
5305 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
5309 if ((count == 1 && la->la_nlink == 1) ||
5310 S_ISDIR(lfsck_object_type(obj)))
5311 /* Usually, it is for single linked object or dir, do nothing.*/
5314 /* Following modification will be in another transaction. */
5315 if (handle != NULL) {
5316 dt_write_unlock(env, obj);
5319 dt_trans_stop(env, dev, handle);
5322 lfsck_ibits_unlock(&lh, LCK_EX);
5325 ns->ln_mul_linked_checked++;
5326 rc = lfsck_namespace_trace_update(env, com, &lnr->lnr_fid,
5327 LNTF_CHECK_LINKEA, true);
5333 dt_write_unlock(env, obj);
5335 if (handle != NULL && !IS_ERR(handle))
5336 dt_trans_stop(env, dev, handle);
5339 lfsck_ibits_unlock(&lh, LCK_EX);
5341 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5342 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5343 lnr->lnr_name, lnr->lnr_namelen) &&
5344 type != LNIT_BAD_DIRENT) {
5345 ns->ln_flags |= LF_INCONSISTENT;
5348 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5349 lnr->lnr_lmv, lnr->lnr_name);
5358 rc = lfsck_namespace_repair_dirent(env, com, dir,
5359 obj, lnr->lnr_name, lnr->lnr_name,
5360 lnr->lnr_type, true, false);
5364 case LNIT_BAD_DIRENT:
5366 /* XXX: This is a bad dirent, we do not know whether
5367 * the original name entry reference a regular
5368 * file or a directory, then keep the parent's
5369 * nlink count unchanged here. */
5370 rc = lfsck_namespace_repair_dirent(env, com, dir,
5371 obj, lnr->lnr_name, lnr->lnr_name,
5372 lnr->lnr_type, false, false);
5381 down_write(&com->lc_sem);
5383 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5384 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5385 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5386 PFID(lfsck_dto2fid(lnr->lnr_obj)),
5387 lnr->lnr_namelen, lnr->lnr_name, rc);
5389 lfsck_namespace_record_failure(env, lfsck, ns);
5390 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5391 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5392 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5393 dev != NULL && dev != lfsck->li_next)
5394 lfsck_lad_set_bitmap(env, com, idx);
5396 if (!(bk->lb_param & LPF_FAILOUT))
5400 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5401 "repaired the entry: "DFID", parent "DFID
5402 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5403 PFID(&lnr->lnr_fid),
5404 PFID(lfsck_dto2fid(lnr->lnr_obj)),
5405 lnr->lnr_namelen, lnr->lnr_name);
5408 ns->ln_items_repaired++;
5412 ns->ln_dangling_repaired++;
5415 ns->ln_bad_type_repaired++;
5417 case LNIT_BAD_DIRENT:
5418 ns->ln_dirent_repaired++;
5424 if (bk->lb_param & LPF_DRYRUN &&
5425 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5426 lfsck_pos_fill(env, lfsck,
5427 &ns->ln_pos_first_inconsistent,
5432 ns->ln_name_hash_repaired++;
5434 /* Not count repeatedly. */
5436 ns->ln_items_repaired++;
5438 if (bk->lb_param & LPF_DRYRUN &&
5439 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5440 lfsck_pos_fill(env, lfsck,
5441 &ns->ln_pos_first_inconsistent,
5448 up_write(&com->lc_sem);
5450 if (obj != NULL && !IS_ERR(obj))
5451 lfsck_object_put(env, obj);
5457 * Handle one orphan under the backend /lost+found directory
5459 * Insert the orphan FID into the namespace LFSCK trace file for further
5460 * processing (via the subsequent namespace LFSCK second-stage scanning).
5461 * At the same time, remove the orphan name entry from backend /lost+found
5462 * directory. There is an interval between the orphan name entry removed
5463 * from the backend /lost+found directory and the orphan FID in the LFSCK
5464 * trace file handled. In such interval, the LFSCK can be reset, then
5465 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5466 * To guarantee that the orphans can be found when LFSCK run next time
5467 * without e2fsck again, when remove the orphan name entry, the LFSCK
5468 * will set the orphan's ctime attribute as 1. Since normal applications
5469 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5470 * next time, it can record the object (that ctime is 1) in the namespace
5471 * LFSCK trace file during the first-stage scanning.
5473 * \param[in] env pointer to the thread context
5474 * \param[in] com pointer to the lfsck component
5475 * \param[in] parent pointer to the object for the backend /lost+found
5476 * \param[in] ent pointer to the name entry for the target under the
5477 * backend /lost+found
5479 * \retval positive for repaired
5480 * \retval 0 if needs to repair nothing
5481 * \retval negative error number on failure
5483 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5484 struct lfsck_component *com,
5485 struct dt_object *parent,
5486 struct lu_dirent *ent)
5488 struct lfsck_thread_info *info = lfsck_env_info(env);
5489 struct lu_fid *key = &info->lti_fid;
5490 struct lu_attr *la = &info->lti_la;
5491 struct lfsck_instance *lfsck = com->lc_lfsck;
5492 struct dt_object *obj = com->lc_obj;
5493 struct dt_device *dev = lfsck->li_bottom;
5494 struct dt_object *child = NULL;
5495 struct thandle *th = NULL;
5501 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5503 RETURN(PTR_ERR(child));
5505 LASSERT(dt_object_exists(child));
5506 LASSERT(!dt_object_remote(child));
5508 fid_cpu_to_be(key, &ent->lde_fid);
5509 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5510 (const struct dt_key *)key, BYPASS_CAPA);
5513 flags |= LNTF_CHECK_ORPHAN;
5514 } else if (rc == -ENOENT) {
5515 flags = LNTF_CHECK_ORPHAN;
5520 th = dt_trans_create(env, dev);
5522 GOTO(out, rc = PTR_ERR(th));
5524 /* a1. remove name entry from backend /lost+found */
5525 rc = dt_declare_delete(env, parent,
5526 (const struct dt_key *)ent->lde_name, th);
5530 if (S_ISDIR(lfsck_object_type(child))) {
5531 /* a2. decrease parent's nlink */
5532 rc = dt_declare_ref_del(env, parent, th);
5538 /* a3. remove child's FID from the LFSCK trace file. */
5539 rc = dt_declare_delete(env, obj,
5540 (const struct dt_key *)key, th);
5544 /* a4. set child's ctime as 1 */
5545 memset(la, 0, sizeof(*la));
5547 la->la_valid = LA_CTIME;
5548 rc = dt_declare_attr_set(env, child, la, th);
5553 /* a5. insert child's FID into the LFSCK trace file. */
5554 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5555 (const struct dt_key *)key, th);
5559 rc = dt_trans_start_local(env, dev, th);
5563 /* b1. remove name entry from backend /lost+found */
5564 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5569 if (S_ISDIR(lfsck_object_type(child))) {
5570 /* b2. decrease parent's nlink */
5571 dt_write_lock(env, parent, 0);
5572 rc = dt_ref_del(env, parent, th);
5573 dt_write_unlock(env, parent);
5579 /* a3. remove child's FID from the LFSCK trace file. */
5580 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5585 /* b4. set child's ctime as 1 */
5586 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5591 /* b5. insert child's FID into the LFSCK trace file. */
5592 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5593 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5595 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5598 dt_trans_stop(env, dev, th);
5601 lu_object_put(env, &child->do_lu);
5607 * Handle orphans under the backend /lost+found directory
5609 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5610 * and put them under the backend /lost+found directory that is invisible
5611 * to client. The LFSCK will scan such directory, for the original client
5612 * visible orphans, add their fids into the namespace LFSCK trace file,
5613 * then the subsenquent namespace LFSCK second-stage scanning can handle
5614 * them as other objects to be double scanned: either move back to normal
5615 * namespace, or to the global visible orphan directory:
5616 * /ROOT/.lustre/lost+found/MDTxxxx/
5618 * \param[in] env pointer to the thread context
5619 * \param[in] com pointer to the lfsck component
5621 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5622 struct lfsck_component *com)
5624 struct lfsck_thread_info *info = lfsck_env_info(env);
5625 struct lu_dirent *ent =
5626 (struct lu_dirent *)info->lti_key;
5627 struct lu_seq_range *range = &info->lti_range;
5628 struct lfsck_instance *lfsck = com->lc_lfsck;
5629 struct ptlrpc_thread *thread = &lfsck->li_thread;
5630 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5631 struct dt_device *dev = lfsck->li_bottom;
5632 struct lfsck_namespace *ns = com->lc_file_ram;
5633 struct dt_object *parent;
5634 const struct dt_it_ops *iops;
5636 struct seq_server_site *ss =
5637 lu_site2seq(dev->dd_lu_dev.ld_site);
5643 parent = lfsck_object_find_by_dev(env, dev, &LU_BACKEND_LPF_FID);
5644 if (IS_ERR(parent)) {
5645 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5646 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5650 /* It is normal that the /lost+found does not exist for ZFS backend. */
5651 if (!dt_object_exists(parent))
5654 if (unlikely(!dt_try_as_dir(env, parent)))
5655 GOTO(out, rc = -ENOTDIR);
5657 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5658 lfsck_lfsck2name(lfsck));
5660 com->lc_new_scanned = 0;
5661 iops = &parent->do_index_ops->dio_it;
5662 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5664 GOTO(out, rc = PTR_ERR(di));
5666 rc = iops->load(env, di, 0);
5668 rc = iops->next(env, di);
5673 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
5675 struct l_wait_info lwi;
5677 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
5679 l_wait_event(thread->t_ctl_waitq,
5680 !thread_is_running(thread),
5683 if (unlikely(!thread_is_running(thread)))
5687 rc = iops->rec(env, di, (struct dt_rec *)ent,
5688 LUDA_64BITHASH | LUDA_TYPE);
5690 rc = lfsck_unpack_ent(ent, &cookie, &type);
5692 if (unlikely(rc != 0)) {
5693 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5694 "/lost+found: rc = %d\n",
5695 lfsck_lfsck2name(lfsck), rc);
5700 /* skip dot and dotdot entries */
5701 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5704 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5707 if (fid_is_norm(&ent->lde_fid)) {
5708 fld_range_set_mdt(range);
5709 rc = fld_local_lookup(env, ss->ss_server_fld,
5710 fid_seq(&ent->lde_fid), range);
5713 } else if (lfsck_dev_idx(dev) != 0) {
5714 /* If the returned FID is IGIF, then there are three
5717 * 1) The object is upgraded from old Lustre-1.8 with
5718 * IGIF assigned to such object.
5719 * 2) The object is a backend local object and is
5720 * invisible to client.
5721 * 3) The object lost its LMV EA, and since there is
5722 * no FID-in-dirent for the orphan in the backend
5723 * /lost+found directory, then the low layer will
5724 * return IGIF for such object.
5726 * For MDTx (x != 0), it is either case 2) or case 3),
5727 * but from the LFSCK view, they are indistinguishable.
5728 * To be safe, the LFSCK will keep it there and report
5729 * some message, then the adminstrator can handle that
5732 * For MDT0, it is more possible the case 1). The LFSCK
5733 * will handle the orphan as an upgraded object. */
5734 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5735 "in the backend /lost+found on the MDT %04x, "
5736 "to be safe, skip it.\n",
5737 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5738 ent->lde_name, PFID(&ent->lde_fid),
5739 lfsck_dev_idx(dev));
5743 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5746 down_write(&com->lc_sem);
5747 com->lc_new_scanned++;
5748 ns->ln_local_lpf_scanned++;
5750 ns->ln_local_lpf_moved++;
5752 ns->ln_local_lpf_skipped++;
5754 ns->ln_local_lpf_failed++;
5755 up_write(&com->lc_sem);
5757 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5761 lfsck_control_speed_by_self(com);
5762 if (unlikely(!thread_is_running(thread))) {
5767 rc = iops->next(env, di);
5771 iops->fini(env, di);
5776 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5777 lfsck_lfsck2name(lfsck), rc);
5779 lu_object_put(env, &parent->do_lu);
5783 * Rescan the striped directory after the master LMV EA reset.
5785 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5786 * the namespace LFSCK engine scan the striped directory for the first time,
5787 * it will be regarded as a normal directory. As the LFSCK processing, some
5788 * other LFSCK instance on other MDT will find the shard of this striped dir,
5789 * and find that the master MDT-object of the striped directory lost its LMV
5790 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5791 * notify the LFSCK instance on this MDT to rescan the striped directory.
5793 * \param[in] env pointer to the thread context
5794 * \param[in] com pointer to the lfsck component
5795 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5798 * \retval positive number for success
5799 * \retval 0 for LFSCK stopped/paused
5800 * \retval negative error number on failure
5802 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5803 struct lfsck_component *com,
5804 struct lfsck_lmv_unit *llu)
5806 struct lfsck_thread_info *info = lfsck_env_info(env);
5807 struct lfsck_instance *lfsck = com->lc_lfsck;
5808 struct lfsck_assistant_data *lad = com->lc_data;
5809 struct dt_object *dir;
5810 const struct dt_it_ops *iops;
5812 struct lu_dirent *ent =
5813 (struct lu_dirent *)info->lti_key;
5814 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5815 struct ptlrpc_thread *thread = &lfsck->li_thread;
5816 struct lfsck_namespace_req *lnr;
5817 struct lfsck_assistant_req *lar;
5822 LASSERT(list_empty(&lad->lad_req_list));
5824 lfsck->li_lmv = &llu->llu_lmv;
5825 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5826 rc = lfsck_open_dir(env, lfsck, 0);
5830 dir = lfsck->li_obj_dir;
5831 di = lfsck->li_di_dir;
5832 iops = &dir->do_index_ops->dio_it;
5834 rc = iops->rec(env, di, (struct dt_rec *)ent,
5835 lfsck->li_args_dir);
5837 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5841 if (bk->lb_param & LPF_FAILOUT)
5847 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5850 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5852 if (bk->lb_param & LPF_FAILOUT)
5853 GOTO(out, rc = PTR_ERR(lnr));
5858 lar = &lnr->lnr_lar;
5859 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5860 lfsck_namespace_assistant_req_fini(env, lar);
5861 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5864 if (unlikely(!thread_is_running(thread)))
5868 rc = iops->next(env, di);
5872 lfsck_close_dir(env, lfsck, rc);
5876 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5877 if (list_empty(&lad->lad_req_list))
5880 spin_lock(&lad->lad_lock);
5881 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5883 list_del_init(&lar->lar_list);
5884 spin_unlock(&lad->lad_lock);
5886 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5887 lfsck_namespace_assistant_req_fini(env, lar);
5889 RETURN(rc == 0 ? 1 : rc);
5892 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5893 struct lfsck_component *com)
5895 struct lfsck_instance *lfsck = com->lc_lfsck;
5896 struct ptlrpc_thread *thread = &lfsck->li_thread;
5897 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5898 struct lfsck_namespace *ns = com->lc_file_ram;
5899 struct dt_object *obj = com->lc_obj;
5900 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5901 struct dt_object *target;
5909 while (!list_empty(&lfsck->li_list_lmv)) {
5910 struct lfsck_lmv_unit *llu;
5912 spin_lock(&lfsck->li_lock);
5913 llu = list_entry(lfsck->li_list_lmv.next,
5914 struct lfsck_lmv_unit, llu_link);
5915 list_del_init(&llu->llu_link);
5916 spin_unlock(&lfsck->li_lock);
5918 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
5923 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
5924 lfsck_lfsck2name(lfsck));
5926 lfsck_namespace_scan_local_lpf(env, com);
5928 com->lc_new_checked = 0;
5929 com->lc_new_scanned = 0;
5930 com->lc_time_last_checkpoint = cfs_time_current();
5931 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5932 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5934 di = iops->init(env, obj, 0, BYPASS_CAPA);
5936 RETURN(PTR_ERR(di));
5938 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5939 rc = iops->get(env, di, (const struct dt_key *)&fid);
5943 /* Skip the start one, which either has been processed or non-exist. */
5944 rc = iops->next(env, di);
5949 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
5951 struct l_wait_info lwi;
5953 lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
5955 l_wait_event(thread->t_ctl_waitq,
5956 !thread_is_running(thread),
5959 if (unlikely(!thread_is_running(thread)))
5963 key = iops->key(env, di);
5964 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5965 if (!fid_is_sane(&fid)) {
5970 target = lfsck_object_find_by_dev(env, lfsck->li_bottom, &fid);
5971 if (IS_ERR(target)) {
5972 rc = PTR_ERR(target);
5976 if (dt_object_exists(target)) {
5977 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5979 rc = lfsck_namespace_double_scan_one(env, com,
5986 lfsck_object_put(env, target);
5989 down_write(&com->lc_sem);
5990 com->lc_new_checked++;
5991 com->lc_new_scanned++;
5992 ns->ln_fid_latest_scanned_phase2 = fid;
5994 ns->ln_objs_repaired_phase2++;
5996 ns->ln_objs_failed_phase2++;
5997 up_write(&com->lc_sem);
5999 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6002 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
6003 cfs_time_current())) &&
6004 com->lc_new_checked != 0) {
6005 down_write(&com->lc_sem);
6006 ns->ln_run_time_phase2 +=
6007 cfs_duration_sec(cfs_time_current() +
6008 HALF_SEC - com->lc_time_last_checkpoint);
6009 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6010 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6011 com->lc_new_checked = 0;
6012 rc = lfsck_namespace_store(env, com, false);
6013 up_write(&com->lc_sem);
6017 com->lc_time_last_checkpoint = cfs_time_current();
6018 com->lc_time_next_checkpoint =
6019 com->lc_time_last_checkpoint +
6020 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6023 lfsck_control_speed_by_self(com);
6024 if (unlikely(!thread_is_running(thread)))
6027 rc = iops->next(env, di);
6036 iops->fini(env, di);
6038 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop: rc = %d\n",
6039 lfsck_lfsck2name(lfsck), rc);
6044 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6045 struct lfsck_component *com,
6046 struct lfsck_position *pos)
6048 struct lfsck_assistant_data *lad = com->lc_data;
6049 struct lfsck_namespace_req *lnr;
6051 if (list_empty(&lad->lad_req_list))
6054 lnr = list_entry(lad->lad_req_list.next,
6055 struct lfsck_namespace_req,
6057 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6058 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6059 pos->lp_dir_parent = *lfsck_dto2fid(lnr->lnr_obj);
6062 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6063 struct lfsck_component *com,
6066 struct lfsck_instance *lfsck = com->lc_lfsck;
6067 struct lfsck_namespace *ns = com->lc_file_ram;
6069 down_write(&com->lc_sem);
6070 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6071 HALF_SEC - lfsck->li_time_last_checkpoint);
6072 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6073 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6074 com->lc_new_checked = 0;
6077 if (ns->ln_flags & LF_INCOMPLETE)
6078 ns->ln_status = LS_PARTIAL;
6080 ns->ln_status = LS_COMPLETED;
6081 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6082 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6083 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6084 ns->ln_success_count++;
6085 } else if (rc == 0) {
6086 if (lfsck->li_status != 0)
6087 ns->ln_status = lfsck->li_status;
6089 ns->ln_status = LS_STOPPED;
6091 ns->ln_status = LS_FAILED;
6094 rc = lfsck_namespace_store(env, com, false);
6095 up_write(&com->lc_sem);
6101 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6102 struct ptlrpc_request *req,
6109 * Notify remote LFSCK instances about former failures.
6111 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6112 * some LFSCK verification requests (maybe because of network issues or the MDT
6113 * itself trouble). During the respond gap the MDT may missed some name entries
6114 * verification, then the MDT cannot know whether related MDT-objects have been
6115 * referenced by related name entries or not, then in the second-stage scanning,
6116 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6117 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6118 * name entry for repairing the orphan.
6120 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6121 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6122 * have ever missed some name entries verification and should skip the handling
6123 * for orphan MDT-objects.
6125 * \param[in] env pointer to the thread context
6126 * \param[in] com pointer to the lfsck component
6127 * \param[in] lr pointer to the lfsck request
6129 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6130 struct lfsck_component *com,
6131 struct lfsck_request *lr)
6133 struct lfsck_async_interpret_args *laia =
6134 &lfsck_env_info(env)->lti_laia2;
6135 struct lfsck_assistant_data *lad = com->lc_data;
6136 struct lfsck_namespace *ns = com->lc_file_ram;
6137 struct lfsck_instance *lfsck = com->lc_lfsck;
6138 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6139 struct lfsck_tgt_desc *ltd;
6140 struct ptlrpc_request_set *set;
6144 set = ptlrpc_prep_set();
6146 GOTO(out, rc = -ENOMEM);
6148 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6149 memset(laia, 0, sizeof(*laia));
6150 lad->lad_touch_gen++;
6152 spin_lock(<ds->ltd_lock);
6153 while (!list_empty(&lad->lad_mdt_list)) {
6154 ltd = list_entry(lad->lad_mdt_list.next,
6155 struct lfsck_tgt_desc,
6156 ltd_namespace_list);
6157 if (ltd->ltd_namespace_gen == lad->lad_touch_gen)
6160 ltd->ltd_namespace_gen = lad->lad_touch_gen;
6161 list_move_tail(<d->ltd_namespace_list,
6162 &lad->lad_mdt_list);
6163 if (!lad->lad_incomplete ||
6164 !cfs_bitmap_check(lad->lad_bitmap, ltd->ltd_index)) {
6165 ltd->ltd_namespace_failed = 0;
6169 ltd->ltd_namespace_failed = 1;
6170 spin_unlock(<ds->ltd_lock);
6171 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6172 lfsck_namespace_assistant_sync_failures_interpret,
6173 laia, LFSCK_NOTIFY);
6175 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6176 "to sync failure with MDT %x: rc = %d\n",
6177 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6179 spin_lock(<ds->ltd_lock);
6181 spin_unlock(<ds->ltd_lock);
6183 rc = ptlrpc_set_wait(set);
6184 ptlrpc_set_destroy(set);
6190 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6191 "to sync failure with MDTs, and related MDTs "
6192 "may handle orphan improperly: rc = %d\n",
6193 lfsck_lfsck2name(lfsck), rc);
6198 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6199 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6200 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6201 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6202 .la_double_scan_result = lfsck_namespace_double_scan_result,
6203 .la_req_fini = lfsck_namespace_assistant_req_fini,
6204 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6208 * Verify the specified linkEA entry for the given directory object.
6209 * If the object has no such linkEA entry or it has more other linkEA
6210 * entries, then re-generate the linkEA with the given information.
6212 * \param[in] env pointer to the thread context
6213 * \param[in] dev pointer to the dt_device
6214 * \param[in] obj pointer to the dt_object to be handled
6215 * \param[in] cname the name for the child in the parent directory
6216 * \param[in] pfid the parent directory's FID for the linkEA
6218 * \retval 0 for success
6219 * \retval negative error number on failure
6221 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
6222 struct dt_object *obj, const struct lu_name *cname,
6223 const struct lu_fid *pfid)
6225 struct linkea_data ldata = { 0 };
6226 struct lu_buf linkea_buf;
6229 int fl = LU_XATTR_CREATE;
6233 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6235 rc = lfsck_links_read(env, obj, &ldata);
6236 if (rc == -ENODATA) {
6238 } else if (rc == 0) {
6239 fl = LU_XATTR_REPLACE;
6240 if (ldata.ld_leh->leh_reccount != 1) {
6243 rc = linkea_links_find(&ldata, cname, pfid);
6252 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6256 rc = linkea_add_buf(&ldata, cname, pfid);
6260 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6261 ldata.ld_leh->leh_len);
6262 th = dt_trans_create(env, dev);
6264 RETURN(PTR_ERR(th));
6266 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6267 XATTR_NAME_LINK, fl, th);
6271 rc = dt_trans_start_local(env, dev, th);
6275 dt_write_lock(env, obj, 0);
6276 rc = dt_xattr_set(env, obj, &linkea_buf,
6277 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6278 dt_write_unlock(env, obj);
6283 dt_trans_stop(env, dev, th);
6288 * Get the name and parent directory's FID from the first linkEA entry.
6290 * \param[in] env pointer to the thread context
6291 * \param[in] obj pointer to the object which get linkEA from
6292 * \param[out] name pointer to the buffer to hold the name
6293 * in the first linkEA entry
6294 * \param[out] pfid pointer to the buffer to hold the parent
6295 * directory's FID in the first linkEA entry
6297 * \retval 0 for success
6298 * \retval negative error number on failure
6300 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6301 char *name, struct lu_fid *pfid)
6303 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6304 struct linkea_data ldata = { 0 };
6307 rc = lfsck_links_read(env, obj, &ldata);
6311 linkea_first_entry(&ldata);
6312 if (ldata.ld_lee == NULL)
6315 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6316 /* To guarantee the 'name' is terminated with '0'. */
6317 memcpy(name, cname->ln_name, cname->ln_namelen);
6318 name[cname->ln_namelen] = 0;
6324 * Remove the name entry from the parent directory.
6326 * No need to care about the object referenced by the name entry,
6327 * either the name entry is invalid or redundant, or the referenced
6328 * object has been processed has been or will be handled by others.
6330 * \param[in] env pointer to the thread context
6331 * \param[in] lfsck pointer to the lfsck instance
6332 * \param[in] parent pointer to the lost+found object
6333 * \param[in] name the name for the name entry to be removed
6334 * \param[in] type the type for the name entry to be removed
6336 * \retval 0 for success
6337 * \retval negative error number on failure
6339 int lfsck_remove_name_entry(const struct lu_env *env,
6340 struct lfsck_instance *lfsck,
6341 struct dt_object *parent,
6342 const char *name, __u32 type)
6344 struct dt_device *dev = lfsck->li_next;
6346 struct lustre_handle lh = { 0 };
6350 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6351 MDS_INODELOCK_UPDATE, LCK_EX);
6355 th = dt_trans_create(env, dev);
6357 GOTO(unlock, rc = PTR_ERR(th));
6359 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6363 if (S_ISDIR(type)) {
6364 rc = dt_declare_ref_del(env, parent, th);
6369 rc = dt_trans_start(env, dev, th);
6373 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6378 if (S_ISDIR(type)) {
6379 dt_write_lock(env, parent, 0);
6380 rc = dt_ref_del(env, parent, th);
6381 dt_write_unlock(env, parent);
6387 dt_trans_stop(env, dev, th);
6390 lfsck_ibits_unlock(&lh, LCK_EX);
6392 CDEBUG(D_LFSCK, "%s: remove name entry "DFID"/%s "
6393 "with type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6394 PFID(lfsck_dto2fid(parent)), name, type, rc);
6400 * Update the object's name entry with the given FID.
6402 * \param[in] env pointer to the thread context
6403 * \param[in] lfsck pointer to the lfsck instance
6404 * \param[in] parent pointer to the parent directory that holds
6406 * \param[in] name the name for the entry to be updated
6407 * \param[in] pfid the new PFID for the name entry
6408 * \param[in] type the type for the name entry to be updated
6410 * \retval 0 for success
6411 * \retval negative error number on failure
6413 int lfsck_update_name_entry(const struct lu_env *env,
6414 struct lfsck_instance *lfsck,
6415 struct dt_object *parent, const char *name,
6416 const struct lu_fid *pfid, __u32 type)
6418 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
6419 struct dt_device *dev = lfsck->li_next;
6420 struct lustre_handle lh = { 0 };
6426 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6427 MDS_INODELOCK_UPDATE, LCK_EX);
6431 th = dt_trans_create(env, dev);
6433 GOTO(unlock, rc = PTR_ERR(th));
6435 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6439 rec->rec_type = type;
6440 rec->rec_fid = pfid;
6441 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
6442 (const struct dt_key *)name, th);
6446 rc = dt_declare_ref_add(env, parent, th);
6450 rc = dt_trans_start(env, dev, th);
6454 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6456 if (rc == -ENOENT) {
6464 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
6465 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6466 if (rc == 0 && S_ISDIR(type) && !exists) {
6467 dt_write_lock(env, parent, 0);
6468 rc = dt_ref_add(env, parent, th);
6469 dt_write_unlock(env, parent);
6475 dt_trans_stop(env, dev, th);
6478 lfsck_ibits_unlock(&lh, LCK_EX);
6480 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6481 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6482 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
6487 int lfsck_namespace_setup(const struct lu_env *env,
6488 struct lfsck_instance *lfsck)
6490 struct lfsck_component *com;
6491 struct lfsck_namespace *ns;
6492 struct dt_object *root = NULL;
6493 struct dt_object *obj;
6497 LASSERT(lfsck->li_master);
6503 INIT_LIST_HEAD(&com->lc_link);
6504 INIT_LIST_HEAD(&com->lc_link_dir);
6505 init_rwsem(&com->lc_sem);
6506 atomic_set(&com->lc_ref, 1);
6507 com->lc_lfsck = lfsck;
6508 com->lc_type = LFSCK_TYPE_NAMESPACE;
6509 com->lc_ops = &lfsck_namespace_ops;
6510 com->lc_data = lfsck_assistant_data_init(
6511 &lfsck_namespace_assistant_ops,
6513 if (com->lc_data == NULL)
6514 GOTO(out, rc = -ENOMEM);
6516 com->lc_file_size = sizeof(struct lfsck_namespace);
6517 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6518 if (com->lc_file_ram == NULL)
6519 GOTO(out, rc = -ENOMEM);
6521 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6522 if (com->lc_file_disk == NULL)
6523 GOTO(out, rc = -ENOMEM);
6525 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6527 GOTO(out, rc = PTR_ERR(root));
6529 if (unlikely(!dt_try_as_dir(env, root)))
6530 GOTO(out, rc = -ENOTDIR);
6532 obj = local_index_find_or_create(env, lfsck->li_los, root,
6533 lfsck_namespace_name,
6534 S_IFREG | S_IRUGO | S_IWUSR,
6535 &dt_lfsck_features);
6537 GOTO(out, rc = PTR_ERR(obj));
6540 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
6544 rc = lfsck_namespace_load(env, com);
6546 rc = lfsck_namespace_init(env, com);
6548 rc = lfsck_namespace_reset(env, com, true);
6552 ns = com->lc_file_ram;
6553 switch (ns->ln_status) {
6558 spin_lock(&lfsck->li_lock);
6559 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6560 spin_unlock(&lfsck->li_lock);
6563 CERROR("%s: unknown lfsck_namespace status %d\n",
6564 lfsck_lfsck2name(lfsck), ns->ln_status);
6566 case LS_SCANNING_PHASE1:
6567 case LS_SCANNING_PHASE2:
6568 /* No need to store the status to disk right now.
6569 * If the system crashed before the status stored,
6570 * it will be loaded back when next time. */
6571 ns->ln_status = LS_CRASHED;
6575 spin_lock(&lfsck->li_lock);
6576 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6577 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6578 spin_unlock(&lfsck->li_lock);
6585 if (root != NULL && !IS_ERR(root))
6586 lu_object_put(env, &root->do_lu);
6588 lfsck_component_cleanup(env, com);
6589 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6590 lfsck_lfsck2name(lfsck), rc);