4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Copyright (c) 2013, 2014, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
42 #include "lfsck_internal.h"
44 #define LFSCK_NAMESPACE_MAGIC_V1 0xA0629D03
45 #define LFSCK_NAMESPACE_MAGIC_V2 0xA0621A0B
47 /* For Lustre-2.x (x <= 6), the namespace LFSCK used LFSCK_NAMESPACE_MAGIC_V1
48 * as the trace file magic. When downgrade to such old release, the old LFSCK
49 * will not recognize the new LFSCK_NAMESPACE_MAGIC_V2 in the new trace file,
50 * then it will reset the whole LFSCK, and will not cause start failure. The
51 * similar case will happen when upgrade from such old release. */
52 #define LFSCK_NAMESPACE_MAGIC LFSCK_NAMESPACE_MAGIC_V2
54 enum lfsck_nameentry_check {
55 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
56 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
57 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
60 static struct lfsck_namespace_req *
61 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
62 struct lu_dirent *ent, __u16 type)
64 struct lfsck_namespace_req *lnr;
67 size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
70 return ERR_PTR(-ENOMEM);
72 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
73 lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
74 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
75 lnr->lnr_fid = ent->lde_fid;
76 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
77 lnr->lnr_dir_cookie = ent->lde_hash;
78 lnr->lnr_attr = ent->lde_attrs;
81 lnr->lnr_namelen = ent->lde_namelen;
82 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
87 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
88 struct lfsck_assistant_req *lar)
90 struct lfsck_namespace_req *lnr =
91 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
93 if (lnr->lnr_lmv != NULL)
94 lfsck_lmv_put(env, lnr->lnr_lmv);
96 OBD_FREE(lnr, lnr->lnr_size);
99 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
100 struct lfsck_namespace *src)
102 dst->ln_magic = le32_to_cpu(src->ln_magic);
103 dst->ln_status = le32_to_cpu(src->ln_status);
104 dst->ln_flags = le32_to_cpu(src->ln_flags);
105 dst->ln_success_count = le32_to_cpu(src->ln_success_count);
106 dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
107 dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
108 dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
109 dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
110 dst->ln_time_last_checkpoint =
111 le64_to_cpu(src->ln_time_last_checkpoint);
112 lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
113 &src->ln_pos_latest_start);
114 lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
115 &src->ln_pos_last_checkpoint);
116 lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
117 &src->ln_pos_first_inconsistent);
118 dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
119 dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
120 dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
121 dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
122 dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
123 dst->ln_objs_repaired_phase2 =
124 le64_to_cpu(src->ln_objs_repaired_phase2);
125 dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
126 dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
127 fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
128 &src->ln_fid_latest_scanned_phase2);
129 dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
130 dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
131 dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
132 dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
133 dst->ln_unknown_inconsistency =
134 le64_to_cpu(src->ln_unknown_inconsistency);
135 dst->ln_unmatched_pairs_repaired =
136 le64_to_cpu(src->ln_unmatched_pairs_repaired);
137 dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
138 dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
139 dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
140 dst->ln_lost_dirent_repaired =
141 le64_to_cpu(src->ln_lost_dirent_repaired);
142 dst->ln_striped_dirs_scanned =
143 le64_to_cpu(src->ln_striped_dirs_scanned);
144 dst->ln_striped_dirs_repaired =
145 le64_to_cpu(src->ln_striped_dirs_repaired);
146 dst->ln_striped_dirs_failed =
147 le64_to_cpu(src->ln_striped_dirs_failed);
148 dst->ln_striped_dirs_disabled =
149 le64_to_cpu(src->ln_striped_dirs_disabled);
150 dst->ln_striped_dirs_skipped =
151 le64_to_cpu(src->ln_striped_dirs_skipped);
152 dst->ln_striped_shards_scanned =
153 le64_to_cpu(src->ln_striped_shards_scanned);
154 dst->ln_striped_shards_repaired =
155 le64_to_cpu(src->ln_striped_shards_repaired);
156 dst->ln_striped_shards_failed =
157 le64_to_cpu(src->ln_striped_shards_failed);
158 dst->ln_striped_shards_skipped =
159 le64_to_cpu(src->ln_striped_shards_skipped);
160 dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
161 dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
162 dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
163 dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
164 dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
165 dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
168 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
169 struct lfsck_namespace *src)
171 dst->ln_magic = cpu_to_le32(src->ln_magic);
172 dst->ln_status = cpu_to_le32(src->ln_status);
173 dst->ln_flags = cpu_to_le32(src->ln_flags);
174 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
175 dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
176 dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
177 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
178 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
179 dst->ln_time_last_checkpoint =
180 cpu_to_le64(src->ln_time_last_checkpoint);
181 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
182 &src->ln_pos_latest_start);
183 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
184 &src->ln_pos_last_checkpoint);
185 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
186 &src->ln_pos_first_inconsistent);
187 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
188 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
189 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
190 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
191 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
192 dst->ln_objs_repaired_phase2 =
193 cpu_to_le64(src->ln_objs_repaired_phase2);
194 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
195 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
196 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
197 &src->ln_fid_latest_scanned_phase2);
198 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
199 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
200 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
201 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
202 dst->ln_unknown_inconsistency =
203 cpu_to_le64(src->ln_unknown_inconsistency);
204 dst->ln_unmatched_pairs_repaired =
205 cpu_to_le64(src->ln_unmatched_pairs_repaired);
206 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
207 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
208 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
209 dst->ln_lost_dirent_repaired =
210 cpu_to_le64(src->ln_lost_dirent_repaired);
211 dst->ln_striped_dirs_scanned =
212 cpu_to_le64(src->ln_striped_dirs_scanned);
213 dst->ln_striped_dirs_repaired =
214 cpu_to_le64(src->ln_striped_dirs_repaired);
215 dst->ln_striped_dirs_failed =
216 cpu_to_le64(src->ln_striped_dirs_failed);
217 dst->ln_striped_dirs_disabled =
218 cpu_to_le64(src->ln_striped_dirs_disabled);
219 dst->ln_striped_dirs_skipped =
220 cpu_to_le64(src->ln_striped_dirs_skipped);
221 dst->ln_striped_shards_scanned =
222 cpu_to_le64(src->ln_striped_shards_scanned);
223 dst->ln_striped_shards_repaired =
224 cpu_to_le64(src->ln_striped_shards_repaired);
225 dst->ln_striped_shards_failed =
226 cpu_to_le64(src->ln_striped_shards_failed);
227 dst->ln_striped_shards_skipped =
228 cpu_to_le64(src->ln_striped_shards_skipped);
229 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
230 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
231 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
232 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
233 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
234 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
237 static void lfsck_namespace_record_failure(const struct lu_env *env,
238 struct lfsck_instance *lfsck,
239 struct lfsck_namespace *ns)
241 struct lfsck_position pos;
243 ns->ln_items_failed++;
244 lfsck_pos_fill(env, lfsck, &pos, false);
245 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
246 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
247 ns->ln_pos_first_inconsistent = pos;
249 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
250 "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
251 lfsck_lfsck2name(lfsck),
252 ns->ln_pos_first_inconsistent.lp_oit_cookie,
253 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
254 ns->ln_pos_first_inconsistent.lp_dir_cookie);
259 * Load the MDT bitmap from the lfsck_namespace trace file.
261 * \param[in] env pointer to the thread context
262 * \param[in] com pointer to the lfsck component
264 * \retval 0 for success
265 * \retval negative error number on failure or data corruption
267 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
268 struct lfsck_component *com)
270 struct dt_object *obj = com->lc_obj;
271 struct lfsck_assistant_data *lad = com->lc_data;
272 struct lfsck_namespace *ns = com->lc_file_ram;
273 cfs_bitmap_t *bitmap = lad->lad_bitmap;
279 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
281 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
283 nbits = ns->ln_bitmap_size;
285 if (unlikely(nbits < BITS_PER_LONG))
286 nbits = BITS_PER_LONG;
288 if (nbits > bitmap->size) {
289 __u32 new_bits = bitmap->size;
290 cfs_bitmap_t *new_bitmap;
292 while (new_bits < nbits)
295 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
296 if (new_bitmap == NULL)
299 lad->lad_bitmap = new_bitmap;
300 CFS_FREE_BITMAP(bitmap);
304 if (ns->ln_bitmap_size == 0) {
305 lad->lad_incomplete = 0;
306 CFS_RESET_BITMAP(bitmap);
311 size = (ns->ln_bitmap_size + 7) >> 3;
312 rc = dt_xattr_get(env, obj,
313 lfsck_buf_get(env, bitmap->data, size),
314 XATTR_NAME_LFSCK_BITMAP, BYPASS_CAPA);
316 RETURN(rc >= 0 ? -EINVAL : rc);
318 if (cfs_bitmap_check_empty(bitmap))
319 lad->lad_incomplete = 0;
321 lad->lad_incomplete = 1;
327 * Load namespace LFSCK statistics information from the trace file.
329 * For old release (Lustre-2.6 or older), the statistics information was
330 * stored as XATTR_NAME_LFSCK_NAMESPACE_OLD EA. But in Lustre-2.7, we need
331 * more statistics information. To avoid confusing old MDT when downgrade,
332 * Lustre-2.7 stores the namespace LFSCK statistics information as new
333 * XATTR_NAME_LFSCK_NAMESPACE EA.
335 * \param[in] env pointer to the thread context
336 * \param[in] com pointer to the lfsck component
338 * \retval 0 for success
339 * \retval negative error number on failure
341 static int lfsck_namespace_load(const struct lu_env *env,
342 struct lfsck_component *com)
344 int len = com->lc_file_size;
347 rc = dt_xattr_get(env, com->lc_obj,
348 lfsck_buf_get(env, com->lc_file_disk, len),
349 XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
351 struct lfsck_namespace *ns = com->lc_file_ram;
353 lfsck_namespace_le_to_cpu(ns,
354 (struct lfsck_namespace *)com->lc_file_disk);
355 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
356 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
357 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
358 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
363 } else if (rc != -ENODATA) {
364 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
365 "expected = %d: rc = %d\n",
366 lfsck_lfsck2name(com->lc_lfsck), len, rc);
370 /* Check whether it is old trace file or not.
371 * If yes, it should be reset via returning -ESTALE. */
372 rc = dt_xattr_get(env, com->lc_obj,
373 lfsck_buf_get(env, com->lc_file_disk, len),
374 XATTR_NAME_LFSCK_NAMESPACE_OLD, BYPASS_CAPA);
382 static int lfsck_namespace_store(const struct lu_env *env,
383 struct lfsck_component *com, bool init)
385 struct dt_object *obj = com->lc_obj;
386 struct lfsck_instance *lfsck = com->lc_lfsck;
387 struct lfsck_namespace *ns = com->lc_file_ram;
388 struct lfsck_assistant_data *lad = com->lc_data;
389 struct dt_device *dev = lfsck_obj2dev(obj);
390 cfs_bitmap_t *bitmap = NULL;
391 struct thandle *handle;
393 int len = com->lc_file_size;
395 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
396 struct lu_buf tbuf = { &len, sizeof(len) };
401 bitmap = lad->lad_bitmap;
402 nbits = bitmap->size;
405 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
408 ns->ln_bitmap_size = nbits;
409 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
411 handle = dt_trans_create(env, dev);
413 GOTO(log, rc = PTR_ERR(handle));
415 rc = dt_declare_xattr_set(env, obj,
416 lfsck_buf_get(env, com->lc_file_disk, len),
417 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
421 if (bitmap != NULL) {
422 rc = dt_declare_xattr_set(env, obj,
423 lfsck_buf_get(env, bitmap->data, nbits >> 3),
424 XATTR_NAME_LFSCK_BITMAP, 0, handle);
429 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
430 /* To be compatible with old Lustre-2.x MDT (x <= 6), generate dummy
431 * XATTR_NAME_LFSCK_NAMESPACE_OLD EA, then when downgrade to Lustre-2.x,
432 * the old LFSCK will find "invalid" XATTR_NAME_LFSCK_NAMESPACE_OLD EA,
433 * then reset the namespace LFSCK trace file. */
435 rc = dt_declare_xattr_set(env, obj, &tbuf,
436 XATTR_NAME_LFSCK_NAMESPACE_OLD,
437 LU_XATTR_CREATE, handle);
443 rc = dt_trans_start_local(env, dev, handle);
447 rc = dt_xattr_set(env, obj,
448 lfsck_buf_get(env, com->lc_file_disk, len),
449 XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
450 if (rc == 0 && bitmap != NULL)
451 rc = dt_xattr_set(env, obj,
452 lfsck_buf_get(env, bitmap->data, nbits >> 3),
453 XATTR_NAME_LFSCK_BITMAP, 0, handle,
456 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
458 rc = dt_xattr_set(env, obj, &tbuf,
459 XATTR_NAME_LFSCK_NAMESPACE_OLD,
460 LU_XATTR_CREATE, handle, BYPASS_CAPA);
466 dt_trans_stop(env, dev, handle);
470 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
471 lfsck_lfsck2name(lfsck), rc);
475 static struct dt_object *
476 lfsck_namespace_load_one_trace_file(const struct lu_env *env,
477 struct lfsck_component *com,
478 struct dt_object *parent,
480 const struct dt_index_features *ft,
483 struct lfsck_instance *lfsck = com->lc_lfsck;
484 struct dt_object *obj;
488 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
489 if (rc != 0 && rc != -ENOENT)
494 obj = local_index_find_or_create(env, lfsck->li_los, parent,
495 name, S_IFREG | S_IRUGO | S_IWUSR, ft);
497 obj = local_file_find_or_create(env, lfsck->li_los, parent,
498 name, S_IFREG | S_IRUGO | S_IWUSR);
503 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
504 struct lfsck_component *com,
507 char *name = lfsck_env_info(env)->lti_key;
508 struct lfsck_sub_trace_obj *lsto;
509 struct dt_object *obj;
513 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
514 i < LFSCK_STF_COUNT; i++, lsto++) {
515 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
516 if (lsto->lsto_obj != NULL) {
520 lfsck_object_put(env, lsto->lsto_obj);
521 lsto->lsto_obj = NULL;
524 obj = lfsck_namespace_load_one_trace_file(env, com,
525 com->lc_lfsck->li_lfsck_dir,
526 name, &dt_lfsck_features, reset);
530 lsto->lsto_obj = obj;
531 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
539 static int lfsck_namespace_init(const struct lu_env *env,
540 struct lfsck_component *com)
542 struct lfsck_namespace *ns = com->lc_file_ram;
545 memset(ns, 0, sizeof(*ns));
546 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
547 ns->ln_status = LS_INIT;
548 down_write(&com->lc_sem);
549 rc = lfsck_namespace_store(env, com, true);
550 up_write(&com->lc_sem);
552 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
558 * Update the namespace LFSCK trace file for the given @fid
560 * \param[in] env pointer to the thread context
561 * \param[in] com pointer to the lfsck component
562 * \param[in] fid the fid which flags to be updated in the lfsck
564 * \param[in] add true if add new flags, otherwise remove flags
566 * \retval 0 for succeed or nothing to be done
567 * \retval negative error number on failure
569 int lfsck_namespace_trace_update(const struct lu_env *env,
570 struct lfsck_component *com,
571 const struct lu_fid *fid,
572 const __u8 flags, bool add)
574 struct lfsck_instance *lfsck = com->lc_lfsck;
575 struct dt_object *obj;
576 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
577 struct dt_device *dev;
578 struct thandle *th = NULL;
587 if (unlikely(!fid_is_sane(fid)))
590 idx = lfsck_sub_trace_file_fid2idx(fid);
591 obj = com->lc_sub_trace_objs[idx].lsto_obj;
592 dev = lfsck_obj2dev(obj);
593 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
594 fid_cpu_to_be(key, fid);
595 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
596 (const struct dt_key *)key, BYPASS_CAPA);
599 GOTO(unlock, rc = 0);
603 } else if (rc == 0) {
605 if ((old & flags) == flags)
606 GOTO(unlock, rc = 0);
610 if ((old & flags) == 0)
611 GOTO(unlock, rc = 0);
619 th = dt_trans_create(env, dev);
621 GOTO(log, rc = PTR_ERR(th));
624 rc = dt_declare_delete(env, obj,
625 (const struct dt_key *)key, th);
631 rc = dt_declare_insert(env, obj,
632 (const struct dt_rec *)&new,
633 (const struct dt_key *)key, th);
638 rc = dt_trans_start_local(env, dev, th);
643 rc = dt_delete(env, obj, (const struct dt_key *)key,
650 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
651 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
659 if (th != NULL && !IS_ERR(th))
660 dt_trans_stop(env, dev, th);
662 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
663 "trace file, flags %x, old %x, new %x: rc = %d\n",
664 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
665 (__u32)flags, (__u32)old, (__u32)new, rc);
668 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
673 int lfsck_namespace_check_exist(const struct lu_env *env,
674 struct dt_object *dir,
675 struct dt_object *obj, const char *name)
677 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
681 if (unlikely(lfsck_is_dead_obj(obj)))
682 RETURN(LFSCK_NAMEENTRY_DEAD);
684 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
685 (const struct dt_key *)name, BYPASS_CAPA);
687 RETURN(LFSCK_NAMEENTRY_REMOVED);
692 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
693 RETURN(LFSCK_NAMEENTRY_RECREATED);
698 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
699 struct dt_object *obj,
700 struct thandle *handle)
704 /* For destroying all invalid linkEA entries. */
705 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
709 /* For insert new linkEA entry. */
710 rc = dt_declare_xattr_set(env, obj,
711 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
712 XATTR_NAME_LINK, 0, handle);
716 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
717 struct linkea_data *ldata)
721 if (ldata->ld_buf->lb_buf == NULL)
724 if (!dt_object_exists(obj))
727 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
729 /* Buf was too small, figure out what we need. */
730 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
735 lu_buf_realloc(ldata->ld_buf, rc);
736 if (ldata->ld_buf->lb_buf == NULL)
739 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
744 rc = linkea_init(ldata);
750 * Remove linkEA for the given object.
752 * The caller should take the ldlm lock before the calling.
754 * \param[in] env pointer to the thread context
755 * \param[in] com pointer to the lfsck component
756 * \param[in] obj pointer to the dt_object to be handled
758 * \retval 0 for repaired cases
759 * \retval negative error number on failure
761 static int lfsck_namespace_links_remove(const struct lu_env *env,
762 struct lfsck_component *com,
763 struct dt_object *obj)
765 struct lfsck_instance *lfsck = com->lc_lfsck;
766 struct dt_device *dev = lfsck_obj2dev(obj);
767 struct thandle *th = NULL;
771 LASSERT(dt_object_remote(obj) == 0);
773 th = dt_trans_create(env, dev);
775 GOTO(log, rc = PTR_ERR(th));
777 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
781 rc = dt_trans_start_local(env, dev, th);
785 dt_write_lock(env, obj, 0);
786 if (unlikely(lfsck_is_dead_obj(obj)))
787 GOTO(unlock, rc = -ENOENT);
789 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
790 GOTO(unlock, rc = 0);
792 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
797 dt_write_unlock(env, obj);
800 dt_trans_stop(env, dev, th);
803 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
804 "for the object "DFID": rc = %d\n",
805 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
808 struct lfsck_namespace *ns = com->lc_file_ram;
810 ns->ln_flags |= LF_INCONSISTENT;
816 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
817 struct linkea_data *ldata, struct thandle *handle)
819 const struct lu_buf *buf = lfsck_buf_get_const(env,
820 ldata->ld_buf->lb_buf,
821 ldata->ld_leh->leh_len);
823 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
827 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
828 struct lu_name *cname,
832 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
833 /* To guarantee the 'name' is terminated with '0'. */
834 memcpy(buf, cname->ln_name, cname->ln_namelen);
835 buf[cname->ln_namelen] = 0;
836 cname->ln_name = buf;
839 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
840 struct lu_name *cname,
844 struct link_ea_entry *oldlee;
848 oldlee = ldata->ld_lee;
849 oldlen = ldata->ld_reclen;
850 linkea_next_entry(ldata);
851 while (ldata->ld_lee != NULL) {
852 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
853 ldata->ld_lee->lee_reclen[1];
854 if (unlikely(ldata->ld_reclen == oldlen &&
855 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
860 linkea_del_buf(ldata, cname);
862 linkea_next_entry(ldata);
865 ldata->ld_lee = oldlee;
866 ldata->ld_reclen = oldlen;
872 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
874 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
875 * with the given type to generate the name, the detailed rules for name
876 * have been described as following.
878 * The function also generates the linkEA corresponding to the name entry
879 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
881 * \param[in] env pointer to the thread context
882 * \param[in] com pointer to the lfsck component
883 * \param[in] orphan pointer to the orphan MDT-object
884 * \param[in] infix additional information for the orphan name, such as
885 * the FID for original
886 * \param[in] type the type for describing why the orphan MDT-object is
887 * created. The rules are as following:
889 * type "D": The MDT-object is a directory, it may knows its parent
890 * but because there is no valid linkEA, the LFSCK cannot
891 * know where to put it back to the namespace.
892 * type "O": The MDT-object has no linkEA, and there is no name
893 * entry that references the MDT-object.
895 * type "S": The orphan MDT-object is a shard of a striped directory
897 * \see lfsck_layout_recreate_parent() for more types.
899 * The orphan name will be like:
900 * ${FID}-${infix}-${type}-${conflict_version}
902 * \param[out] count if some others inserted some linkEA entries by race,
903 * then return the linkEA entries count.
905 * \retval positive number for repaired cases
906 * \retval 0 if needs to repair nothing
907 * \retval negative error number on failure
909 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
910 struct lfsck_component *com,
911 struct dt_object *orphan,
912 const char *infix, const char *type,
915 struct lfsck_thread_info *info = lfsck_env_info(env);
916 struct lu_name *cname = &info->lti_name;
917 struct dt_insert_rec *rec = &info->lti_dt_rec;
918 struct lu_attr *la = &info->lti_la3;
919 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
920 const struct lu_fid *pfid;
922 struct lfsck_instance *lfsck = com->lc_lfsck;
923 struct dt_device *dev = lfsck_obj2dev(orphan);
924 struct dt_object *parent;
925 struct thandle *th = NULL;
926 struct lustre_handle plh = { 0 };
927 struct lustre_handle clh = { 0 };
928 struct linkea_data ldata = { NULL };
929 struct lu_buf linkea_buf;
936 cname->ln_name = NULL;
937 if (unlikely(lfsck->li_lpf_obj == NULL))
938 GOTO(log, rc = -ENXIO);
940 parent = lfsck->li_lpf_obj;
941 pfid = lfsck_dto2fid(parent);
943 /* Hold update lock on the parent to prevent others to access. */
944 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
945 MDS_INODELOCK_UPDATE, LCK_EX);
950 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
951 PFID(cfid), infix, type, idx++);
952 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
953 (const struct dt_key *)info->lti_key,
955 if (rc != 0 && rc != -ENOENT)
958 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
960 } while (rc == 0 && !exist);
962 cname->ln_name = info->lti_key;
963 cname->ln_namelen = namelen;
964 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
968 rc = linkea_add_buf(&ldata, cname, pfid);
972 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
973 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
978 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
979 ldata.ld_leh->leh_len);
980 th = dt_trans_create(env, dev);
982 GOTO(log, rc = PTR_ERR(th));
984 if (S_ISDIR(lfsck_object_type(orphan))) {
985 rc = dt_declare_delete(env, orphan,
986 (const struct dt_key *)dotdot, th);
990 rec->rec_type = S_IFDIR;
992 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
993 (const struct dt_key *)dotdot, th);
998 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
999 XATTR_NAME_LINK, 0, th);
1004 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1005 rec->rec_fid = cfid;
1006 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1007 (const struct dt_key *)cname->ln_name,
1012 if (S_ISDIR(rec->rec_type)) {
1013 rc = dt_declare_ref_add(env, parent, th);
1019 memset(la, 0, sizeof(*la));
1020 la->la_ctime = cfs_time_current_sec();
1021 la->la_valid = LA_CTIME;
1022 rc = dt_declare_attr_set(env, orphan, la, th);
1026 rc = dt_trans_start_local(env, dev, th);
1030 dt_write_lock(env, orphan, 0);
1031 rc = lfsck_links_read(env, orphan, &ldata);
1032 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
1033 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
1034 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1035 GOTO(unlock, rc = 1);
1037 if (S_ISDIR(lfsck_object_type(orphan))) {
1038 rc = dt_delete(env, orphan,
1039 (const struct dt_key *)dotdot, th,
1044 rec->rec_type = S_IFDIR;
1045 rec->rec_fid = pfid;
1046 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1047 (const struct dt_key *)dotdot, th,
1053 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1056 if (rc == 0 && count != NULL)
1057 *count = ldata.ld_leh->leh_reccount;
1061 dt_write_unlock(env, orphan);
1063 if (rc == 0 && !exist) {
1064 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1065 rec->rec_fid = cfid;
1066 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1067 (const struct dt_key *)cname->ln_name,
1068 th, BYPASS_CAPA, 1);
1069 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1070 dt_write_lock(env, parent, 0);
1071 rc = dt_ref_add(env, parent, th);
1072 dt_write_unlock(env, parent);
1077 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
1079 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1082 dt_write_unlock(env, orphan);
1085 dt_trans_stop(env, dev, th);
1088 lfsck_ibits_unlock(&clh, LCK_EX);
1089 lfsck_ibits_unlock(&plh, LCK_EX);
1090 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1091 "object "DFID", name = %s: rc = %d\n",
1092 lfsck_lfsck2name(lfsck), PFID(cfid),
1093 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1096 struct lfsck_namespace *ns = com->lc_file_ram;
1098 ns->ln_flags |= LF_INCONSISTENT;
1105 * Add the specified name entry back to namespace.
1107 * If there is a linkEA entry that back references a name entry under
1108 * some parent directory, but such parent directory does not have the
1109 * claimed name entry. On the other hand, the linkEA entries count is
1110 * not larger than the MDT-object's hard link count. Under such case,
1111 * it is quite possible that the name entry is lost. Then the LFSCK
1112 * should add the name entry back to the namespace.
1114 * \param[in] env pointer to the thread context
1115 * \param[in] com pointer to the lfsck component
1116 * \param[in] parent pointer to the directory under which the name entry
1117 * will be inserted into
1118 * \param[in] child pointer to the object referenced by the name entry
1119 * that to be inserted into the parent
1120 * \param[in] name the name for the child in the parent directory
1122 * \retval positive number for repaired cases
1123 * \retval 0 if nothing to be repaired
1124 * \retval negative error number on failure
1126 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1127 struct lfsck_component *com,
1128 struct dt_object *parent,
1129 struct dt_object *child,
1132 struct lfsck_thread_info *info = lfsck_env_info(env);
1133 struct lu_attr *la = &info->lti_la;
1134 struct dt_insert_rec *rec = &info->lti_dt_rec;
1135 struct lfsck_instance *lfsck = com->lc_lfsck;
1136 /* The child and its name may be on different MDTs. */
1137 struct dt_device *dev = lfsck->li_next;
1138 struct dt_object *pobj = NULL;
1139 struct dt_object *cobj = NULL;
1140 struct thandle *th = NULL;
1141 struct lustre_handle lh = { 0 };
1145 /* @parent/@child may be based on lfsck->li_bottom,
1146 * but here we need the object based on the lfsck->li_next. */
1148 pobj = lfsck_object_locate(dev, parent);
1150 GOTO(log, rc = PTR_ERR(pobj));
1152 if (unlikely(!dt_try_as_dir(env, pobj)))
1153 GOTO(log, rc = -ENOTDIR);
1155 cobj = lfsck_object_locate(dev, child);
1157 GOTO(log, rc = PTR_ERR(cobj));
1159 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1162 /* Hold update lock on the pobj to prevent others to access. */
1163 rc = lfsck_ibits_lock(env, lfsck, pobj, &lh,
1164 MDS_INODELOCK_UPDATE, LCK_EX);
1168 th = dt_trans_create(env, dev);
1170 GOTO(unlock, rc = PTR_ERR(th));
1172 rec->rec_type = lfsck_object_type(cobj) & S_IFMT;
1173 rec->rec_fid = lfsck_dto2fid(cobj);
1174 rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1175 (const struct dt_key *)name, th);
1179 if (S_ISDIR(rec->rec_type)) {
1180 rc = dt_declare_ref_add(env, pobj, th);
1185 memset(la, 0, sizeof(*la));
1186 la->la_ctime = cfs_time_current_sec();
1187 la->la_valid = LA_CTIME;
1188 rc = dt_declare_attr_set(env, pobj, la, th);
1192 rc = dt_declare_attr_set(env, cobj, la, th);
1196 rc = dt_trans_start_local(env, dev, th);
1200 rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1201 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1205 if (S_ISDIR(rec->rec_type)) {
1206 dt_write_lock(env, pobj, 0);
1207 rc = dt_ref_add(env, pobj, th);
1208 dt_write_unlock(env, pobj);
1213 la->la_ctime = cfs_time_current_sec();
1214 rc = dt_attr_set(env, pobj, la, th, BYPASS_CAPA);
1218 rc = dt_attr_set(env, cobj, la, th, BYPASS_CAPA);
1220 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1223 dt_trans_stop(env, dev, th);
1226 lfsck_ibits_unlock(&lh, LCK_EX);
1229 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1230 "the name %s and type %o to the parent "DFID": rc = %d\n",
1231 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1232 lfsck_object_type(child) & S_IFMT,
1233 PFID(lfsck_dto2fid(parent)), rc);
1236 struct lfsck_namespace *ns = com->lc_file_ram;
1238 ns->ln_flags |= LF_INCONSISTENT;
1240 ns->ln_lost_dirent_repaired++;
1247 * Create the specified orphan directory.
1249 * For the case that the parent MDT-object stored in some MDT-object's
1250 * linkEA entry is lost, the LFSCK will re-create the parent object as
1251 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1252 * with the name ${FID}-P-${conflict_version}.
1254 * \param[in] env pointer to the thread context
1255 * \param[in] com pointer to the lfsck component
1256 * \param[in] orphan pointer to the orphan MDT-object to be created
1257 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1259 * \retval positive number for repaired cases
1260 * \retval negative error number on failure
1262 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1263 struct lfsck_component *com,
1264 struct dt_object *orphan,
1265 struct lmv_mds_md_v1 *lmv)
1267 struct lfsck_thread_info *info = lfsck_env_info(env);
1268 struct lu_attr *la = &info->lti_la;
1269 struct dt_allocation_hint *hint = &info->lti_hint;
1270 struct dt_object_format *dof = &info->lti_dof;
1271 struct lu_name *cname = &info->lti_name2;
1272 struct dt_insert_rec *rec = &info->lti_dt_rec;
1273 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1274 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1276 struct lfsck_instance *lfsck = com->lc_lfsck;
1277 struct lfsck_namespace *ns = com->lc_file_ram;
1278 struct dt_device *dev = lfsck_obj2dev(orphan);
1279 struct dt_object *parent = NULL;
1280 struct thandle *th = NULL;
1281 struct lustre_handle lh = { 0 };
1282 struct linkea_data ldata = { NULL };
1283 struct lu_buf linkea_buf;
1284 struct lu_buf lmv_buf;
1291 LASSERT(!dt_object_exists(orphan));
1293 cname->ln_name = NULL;
1294 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1297 if (dt_object_remote(orphan)) {
1298 LASSERT(lfsck->li_lpf_root_obj != NULL);
1300 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1302 GOTO(log, rc = idx);
1304 snprintf(name, 8, "MDT%04x", idx);
1305 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1306 (struct dt_rec *)&tfid,
1307 (const struct dt_key *)name, BYPASS_CAPA);
1309 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1311 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1313 GOTO(log, rc = PTR_ERR(parent));
1315 if (unlikely(!dt_try_as_dir(env, parent)))
1316 GOTO(log, rc = -ENOTDIR);
1318 if (unlikely(lfsck->li_lpf_obj == NULL))
1319 GOTO(log, rc = -ENXIO);
1321 parent = lfsck->li_lpf_obj;
1324 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1326 GOTO(log, rc = PTR_ERR(dev));
1328 /* Hold update lock on the parent to prevent others to access. */
1329 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1330 MDS_INODELOCK_UPDATE, LCK_EX);
1336 namelen = snprintf(name, 31, DFID"-P-%d",
1338 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1339 (const struct dt_key *)name, BYPASS_CAPA);
1340 if (rc != 0 && rc != -ENOENT)
1344 cname->ln_name = name;
1345 cname->ln_namelen = namelen;
1347 memset(la, 0, sizeof(*la));
1348 la->la_mode = S_IFDIR | 0700;
1349 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1350 LA_ATIME | LA_MTIME | LA_CTIME;
1352 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1353 la->la_mode & S_IFMT);
1355 memset(dof, 0, sizeof(*dof));
1356 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1358 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1362 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
1366 th = dt_trans_create(env, dev);
1368 GOTO(unlock1, rc = PTR_ERR(th));
1370 /* Sync the remote transaction to guarantee that the subsequent
1371 * lock against the @orphan can find the @orphan in time. */
1372 if (dt_object_remote(orphan))
1375 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1379 if (unlikely(!dt_try_as_dir(env, orphan)))
1380 GOTO(stop, rc = -ENOTDIR);
1382 rec->rec_type = S_IFDIR;
1383 rec->rec_fid = cfid;
1384 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1385 (const struct dt_key *)dot, th);
1389 rec->rec_fid = lfsck_dto2fid(parent);
1390 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1391 (const struct dt_key *)dotdot, th);
1393 rc = dt_declare_ref_add(env, orphan, th);
1398 rc = dt_declare_ref_add(env, orphan, th);
1403 lmv->lmv_magic = LMV_MAGIC;
1404 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1405 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1406 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1407 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1408 XATTR_NAME_LMV, 0, th);
1413 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1414 ldata.ld_leh->leh_len);
1415 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1416 XATTR_NAME_LINK, 0, th);
1420 rec->rec_fid = cfid;
1421 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1422 (const struct dt_key *)name, th);
1424 rc = dt_declare_ref_add(env, parent, th);
1429 rc = dt_trans_start_local(env, dev, th);
1433 dt_write_lock(env, orphan, 0);
1434 rc = dt_create(env, orphan, la, hint, dof, th);
1438 rec->rec_fid = cfid;
1439 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1440 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1444 rec->rec_fid = lfsck_dto2fid(parent);
1445 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1446 (const struct dt_key *)dotdot, th,
1451 rc = dt_ref_add(env, orphan, th);
1456 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0,
1462 rc = dt_xattr_set(env, orphan, &linkea_buf,
1463 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1464 dt_write_unlock(env, orphan);
1468 rec->rec_fid = cfid;
1469 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1470 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1472 dt_write_lock(env, parent, 0);
1473 rc = dt_ref_add(env, parent, th);
1474 dt_write_unlock(env, parent);
1477 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1480 dt_write_unlock(env, orphan);
1483 dt_trans_stop(env, dev, th);
1486 lfsck_ibits_unlock(&lh, LCK_EX);
1489 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1490 "the object "DFID", name = %s: rc = %d\n",
1491 lfsck_lfsck2name(lfsck), PFID(cfid),
1492 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1494 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1495 lfsck_object_put(env, parent);
1498 ns->ln_flags |= LF_INCONSISTENT;
1504 * Remove the specified entry from the linkEA.
1506 * Locate the linkEA entry with the given @cname and @pfid, then
1507 * remove this entry or the other entries those are repeated with
1510 * \param[in] env pointer to the thread context
1511 * \param[in] com pointer to the lfsck component
1512 * \param[in] obj pointer to the dt_object to be handled
1513 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1514 * \param[in] cname the name for the child in the parent directory
1515 * \param[in] pfid the parent directory's FID for the linkEA
1516 * \param[in] next if true, then remove the first found linkEA
1517 * entry, and move the ldata->ld_lee to next entry
1519 * \retval positive number for repaired cases
1520 * \retval 0 if nothing to be repaired
1521 * \retval negative error number on failure
1523 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1524 struct lfsck_component *com,
1525 struct dt_object *obj,
1526 struct linkea_data *ldata,
1527 struct lu_name *cname,
1528 struct lu_fid *pfid,
1531 struct lfsck_instance *lfsck = com->lc_lfsck;
1532 struct dt_device *dev = lfsck_obj2dev(obj);
1533 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1534 struct thandle *th = NULL;
1535 struct lustre_handle lh = { 0 };
1536 struct linkea_data ldata_new = { NULL };
1537 struct lu_buf linkea_buf;
1541 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1542 MDS_INODELOCK_UPDATE |
1543 MDS_INODELOCK_XATTR, LCK_EX);
1548 linkea_del_buf(ldata, cname);
1550 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1552 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1553 ldata->ld_leh->leh_len);
1556 th = dt_trans_create(env, dev);
1558 GOTO(unlock1, rc = PTR_ERR(th));
1560 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1561 XATTR_NAME_LINK, 0, th);
1565 rc = dt_trans_start_local(env, dev, th);
1569 dt_write_lock(env, obj, 0);
1570 if (unlikely(lfsck_is_dead_obj(obj)))
1571 GOTO(unlock2, rc = -ENOENT);
1573 rc = lfsck_links_read2(env, obj, &ldata_new);
1577 /* The specified linkEA entry has been removed by race. */
1578 rc = linkea_links_find(&ldata_new, cname, pfid);
1580 GOTO(unlock2, rc = 0);
1582 if (bk->lb_param & LPF_DRYRUN)
1583 GOTO(unlock2, rc = 1);
1586 linkea_del_buf(&ldata_new, cname);
1588 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1591 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1592 dt_write_unlock(env, obj);
1593 dt_trans_stop(env, dev, th);
1594 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1595 ldata_new.ld_leh->leh_len);
1599 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1600 ldata_new.ld_leh->leh_len);
1601 rc = dt_xattr_set(env, obj, &linkea_buf,
1602 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1604 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1607 dt_write_unlock(env, obj);
1610 dt_trans_stop(env, dev, th);
1613 lfsck_ibits_unlock(&lh, LCK_EX);
1616 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1617 "for the object: "DFID", parent "DFID", name %.*s\n",
1618 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1619 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1623 struct lfsck_namespace *ns = com->lc_file_ram;
1625 ns->ln_flags |= LF_INCONSISTENT;
1632 * Conditionally remove the specified entry from the linkEA.
1634 * Take the parent lock firstly, then check whether the specified
1635 * name entry exists or not: if yes, do nothing; otherwise, call
1636 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1638 * \param[in] env pointer to the thread context
1639 * \param[in] com pointer to the lfsck component
1640 * \param[in] parent pointer to the parent directory
1641 * \param[in] child pointer to the child object that holds the linkEA
1642 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1643 * \param[in] cname the name for the child in the parent directory
1644 * \param[in] pfid the parent directory's FID for the linkEA
1646 * \retval positive number for repaired cases
1647 * \retval 0 if nothing to be repaired
1648 * \retval negative error number on failure
1650 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1651 struct lfsck_component *com,
1652 struct dt_object *parent,
1653 struct dt_object *child,
1654 struct linkea_data *ldata,
1655 struct lu_name *cname,
1656 struct lu_fid *pfid)
1658 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1659 struct lustre_handle lh = { 0 };
1663 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1664 MDS_INODELOCK_UPDATE, LCK_EX);
1668 dt_read_lock(env, parent, 0);
1669 if (unlikely(lfsck_is_dead_obj(parent))) {
1670 dt_read_unlock(env, parent);
1671 lfsck_ibits_unlock(&lh, LCK_EX);
1672 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1678 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1679 (const struct dt_key *)cname->ln_name,
1681 dt_read_unlock(env, parent);
1683 /* It is safe to release the ldlm lock, because when the logic come
1684 * here, we have got all the needed information above whether the
1685 * linkEA entry is valid or not. It is not important that others
1686 * may add new linkEA entry after the ldlm lock released. If other
1687 * has removed the specified linkEA entry by race, then it is OK,
1688 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1690 lfsck_ibits_unlock(&lh, LCK_EX);
1691 if (rc == -ENOENT) {
1692 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1701 /* The LFSCK just found some internal status of cross-MDTs
1702 * create operation. That is normal. */
1703 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1704 linkea_next_entry(ldata);
1709 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1716 * Conditionally replace name entry in the parent.
1718 * As required, the LFSCK may re-create the lost MDT-object for dangling
1719 * name entry, but such repairing may be wrong because of bad FID in the
1720 * name entry. As the LFSCK processing, the real MDT-object may be found,
1721 * then the LFSCK should check whether the former re-created MDT-object
1722 * has been modified or not, if not, then destroy it and update the name
1723 * entry in the parent to reference the real MDT-object.
1725 * \param[in] env pointer to the thread context
1726 * \param[in] com pointer to the lfsck component
1727 * \param[in] parent pointer to the parent directory
1728 * \param[in] child pointer to the MDT-object that may be the real
1729 * MDT-object corresponding to the name entry in parent
1730 * \param[in] cfid the current FID in the name entry
1731 * \param[in] cname contains the name of the child in the parent directory
1733 * \retval positive number for repaired cases
1734 * \retval 0 if nothing to be repaired
1735 * \retval negative error number on failure
1737 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1738 struct lfsck_component *com,
1739 struct dt_object *parent,
1740 struct dt_object *child,
1741 const struct lu_fid *cfid,
1742 const struct lu_name *cname)
1744 struct lfsck_thread_info *info = lfsck_env_info(env);
1745 struct lu_attr *la = &info->lti_la;
1746 struct dt_insert_rec *rec = &info->lti_dt_rec;
1748 struct lfsck_instance *lfsck = com->lc_lfsck;
1749 /* The child and its name may be on different MDTs. */
1750 struct dt_device *dev = lfsck->li_next;
1751 const char *name = cname->ln_name;
1752 struct dt_object *pobj = NULL;
1753 struct dt_object *cobj = NULL;
1754 struct lustre_handle plh = { 0 };
1755 struct lustre_handle clh = { 0 };
1756 struct linkea_data ldata = { NULL };
1757 struct thandle *th = NULL;
1762 /* @parent/@child may be based on lfsck->li_bottom,
1763 * but here we need the object based on the lfsck->li_next. */
1765 pobj = lfsck_object_locate(dev, parent);
1767 GOTO(log, rc = PTR_ERR(pobj));
1769 if (unlikely(!dt_try_as_dir(env, pobj)))
1770 GOTO(log, rc = -ENOTDIR);
1772 /* Hold update lock on the pobj to prevent others to access. */
1773 rc = lfsck_ibits_lock(env, lfsck, pobj, &plh,
1774 MDS_INODELOCK_UPDATE, LCK_EX);
1778 if (!fid_is_sane(cfid)) {
1783 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1786 if (rc == -ENOENT) {
1794 if (!dt_object_exists(cobj)) {
1799 rc = dt_lookup(env, pobj, (struct dt_rec *)&tfid,
1800 (const struct dt_key *)name, BYPASS_CAPA);
1801 if (rc == -ENOENT) {
1809 /* Someone changed the name entry, cannot replace it. */
1810 if (!lu_fid_eq(cfid, &tfid))
1813 /* lock the object to be destroyed. */
1814 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1815 MDS_INODELOCK_UPDATE |
1816 MDS_INODELOCK_XATTR, LCK_EX);
1820 if (unlikely(lfsck_is_dead_obj(cobj))) {
1825 rc = dt_attr_get(env, cobj, la, BYPASS_CAPA);
1829 /* The object has been modified by other(s), or it is not created by
1830 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1831 if (la->la_ctime != 0)
1834 if (S_ISREG(la->la_mode)) {
1835 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV,
1837 /* If someone has created related OST-object(s),
1839 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1840 GOTO(log, rc = (rc > 0 ? 0 : rc));
1844 dt_read_lock(env, child, 0);
1845 rc = lfsck_links_read2(env, child, &ldata);
1846 dt_read_unlock(env, child);
1848 /* Someone changed the child, no need to replace. */
1855 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(pobj));
1856 /* Someone moved the child, no need to replace. */
1860 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1863 th = dt_trans_create(env, dev);
1865 GOTO(log, rc = PTR_ERR(th));
1868 rc = dt_declare_destroy(env, cobj, th);
1873 rc = dt_declare_delete(env, pobj, (const struct dt_key *)name, th);
1877 rec->rec_type = S_IFDIR;
1878 rec->rec_fid = lfsck_dto2fid(child);
1879 rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1880 (const struct dt_key *)name, th);
1884 rc = dt_trans_start_local(env, dev, th);
1889 rc = dt_destroy(env, cobj, th);
1894 /* The old name entry maybe not exist. */
1895 rc = dt_delete(env, pobj, (const struct dt_key *)name, th, BYPASS_CAPA);
1896 if (rc != 0 && rc != -ENOENT)
1899 rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1900 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1902 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1905 dt_trans_stop(env, dev, th);
1908 lfsck_ibits_unlock(&clh, LCK_EX);
1909 lfsck_ibits_unlock(&plh, LCK_EX);
1911 if (cobj != NULL && !IS_ERR(cobj))
1912 lfsck_object_put(env, cobj);
1914 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1915 "object "DFID" because of conflict with the object "DFID
1916 " under the parent "DFID" with name %s: rc = %d\n",
1917 lfsck_lfsck2name(lfsck), PFID(cfid),
1918 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1925 * Overwrite the linkEA for the object with the given ldata.
1927 * The caller should take the ldlm lock before the calling.
1929 * \param[in] env pointer to the thread context
1930 * \param[in] com pointer to the lfsck component
1931 * \param[in] obj pointer to the dt_object to be handled
1932 * \param[in] ldata pointer to the new linkEA data
1934 * \retval positive number for repaired cases
1935 * \retval 0 if nothing to be repaired
1936 * \retval negative error number on failure
1938 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1939 struct lfsck_component *com,
1940 struct dt_object *obj,
1941 struct linkea_data *ldata)
1943 struct lfsck_instance *lfsck = com->lc_lfsck;
1944 struct dt_device *dev = lfsck_obj2dev(obj);
1945 struct thandle *th = NULL;
1946 struct lu_buf linkea_buf;
1950 th = dt_trans_create(env, dev);
1952 GOTO(log, rc = PTR_ERR(th));
1954 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1955 ldata->ld_leh->leh_len);
1956 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1957 XATTR_NAME_LINK, 0, th);
1961 rc = dt_trans_start_local(env, dev, th);
1965 dt_write_lock(env, obj, 0);
1966 if (unlikely(lfsck_is_dead_obj(obj)))
1967 GOTO(unlock, rc = 0);
1969 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1970 GOTO(unlock, rc = 1);
1972 rc = dt_xattr_set(env, obj, &linkea_buf,
1973 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1975 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1978 dt_write_unlock(env, obj);
1981 dt_trans_stop(env, dev, th);
1984 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1985 "object "DFID": rc = %d\n",
1986 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1989 struct lfsck_namespace *ns = com->lc_file_ram;
1991 ns->ln_flags |= LF_INCONSISTENT;
1998 * Repair invalid name entry.
2000 * If the name entry contains invalid information, such as bad file type
2001 * or (and) corrupted object FID, then either remove the name entry or
2002 * udpate the name entry with the given (right) information.
2004 * \param[in] env pointer to the thread context
2005 * \param[in] com pointer to the lfsck component
2006 * \param[in] parent pointer to the parent directory
2007 * \param[in] child pointer to the object referenced by the name entry
2008 * \param[in] name the old name of the child under the parent directory
2009 * \param[in] name2 the new name of the child under the parent directory
2010 * \param[in] type the type claimed by the name entry
2011 * \param[in] update update the name entry if true; otherwise, remove it
2012 * \param[in] dec decrease the parent nlink count if true
2014 * \retval positive number for repaired successfully
2015 * \retval 0 if nothing to be repaired
2016 * \retval negative error number on failure
2018 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2019 struct lfsck_component *com,
2020 struct dt_object *parent,
2021 struct dt_object *child,
2022 const char *name, const char *name2,
2023 __u16 type, bool update, bool dec)
2025 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
2026 const struct lu_fid *cfid = lfsck_dto2fid(child);
2028 struct lfsck_instance *lfsck = com->lc_lfsck;
2029 struct dt_device *dev = lfsck_obj2dev(parent);
2030 struct thandle *th = NULL;
2031 struct lustre_handle lh = { 0 };
2035 if (unlikely(!dt_try_as_dir(env, parent)))
2036 GOTO(log, rc = -ENOTDIR);
2038 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2039 MDS_INODELOCK_UPDATE, LCK_EX);
2043 th = dt_trans_create(env, dev);
2045 GOTO(unlock1, rc = PTR_ERR(th));
2047 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2052 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2053 rec->rec_fid = cfid;
2054 rc = dt_declare_insert(env, parent,
2055 (const struct dt_rec *)rec,
2056 (const struct dt_key *)name2, th);
2062 rc = dt_declare_ref_del(env, parent, th);
2067 rc = dt_trans_start_local(env, dev, th);
2071 dt_write_lock(env, parent, 0);
2072 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2073 (const struct dt_key *)name, BYPASS_CAPA);
2074 /* Someone has removed the bad name entry by race. */
2076 GOTO(unlock2, rc = 0);
2081 /* Someone has removed the bad name entry and reused it for other
2082 * object by race. */
2083 if (!lu_fid_eq(&tfid, cfid))
2084 GOTO(unlock2, rc = 0);
2086 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2087 GOTO(unlock2, rc = 1);
2089 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2095 rc = dt_insert(env, parent,
2096 (const struct dt_rec *)rec,
2097 (const struct dt_key *)name2, th,
2104 rc = dt_ref_del(env, parent, th);
2109 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2112 dt_write_unlock(env, parent);
2115 dt_trans_stop(env, dev, th);
2117 /* We are not sure whether the child will become orphan or not.
2118 * Record it in the LFSCK trace file for further checking in
2119 * the second-stage scanning. */
2120 if (!update && !dec && rc == 0)
2121 lfsck_namespace_trace_update(env, com, cfid,
2122 LNTF_CHECK_LINKEA, true);
2125 lfsck_ibits_unlock(&lh, LCK_EX);
2128 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2129 "entry for: parent "DFID", child "DFID", name %s, type "
2130 "in name entry %o, type claimed by child %o. repair it "
2131 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2132 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2133 name, type, update ? lfsck_object_type(child) : 0,
2134 update ? "updating" : "removing", name2, rc);
2137 struct lfsck_namespace *ns = com->lc_file_ram;
2139 ns->ln_flags |= LF_INCONSISTENT;
2146 * Update the ".." name entry for the given object.
2148 * The object's ".." is corrupted, this function will update the ".." name
2149 * entry with the given pfid, and the linkEA with the given ldata.
2151 * The caller should take the ldlm lock before the calling.
2153 * \param[in] env pointer to the thread context
2154 * \param[in] com pointer to the lfsck component
2155 * \param[in] obj pointer to the dt_object to be handled
2156 * \param[in] pfid the new fid for the object's ".." name entry
2157 * \param[in] cname the name for the @obj in the parent directory
2159 * \retval positive number for repaired cases
2160 * \retval 0 if nothing to be repaired
2161 * \retval negative error number on failure
2163 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2164 struct lfsck_component *com,
2165 struct dt_object *obj,
2166 const struct lu_fid *pfid,
2167 struct lu_name *cname)
2169 struct lfsck_thread_info *info = lfsck_env_info(env);
2170 struct dt_insert_rec *rec = &info->lti_dt_rec;
2171 struct lfsck_instance *lfsck = com->lc_lfsck;
2172 struct dt_device *dev = lfsck_obj2dev(obj);
2173 struct thandle *th = NULL;
2174 struct linkea_data ldata = { NULL };
2175 struct lu_buf linkea_buf;
2179 LASSERT(!dt_object_remote(obj));
2180 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2182 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2186 rc = linkea_add_buf(&ldata, cname, pfid);
2190 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2191 ldata.ld_leh->leh_len);
2193 th = dt_trans_create(env, dev);
2195 GOTO(log, rc = PTR_ERR(th));
2197 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2201 rec->rec_type = S_IFDIR;
2202 rec->rec_fid = pfid;
2203 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2204 (const struct dt_key *)dotdot, th);
2208 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2209 XATTR_NAME_LINK, 0, th);
2213 rc = dt_trans_start_local(env, dev, th);
2217 dt_write_lock(env, obj, 0);
2218 if (unlikely(lfsck_is_dead_obj(obj)))
2219 GOTO(unlock, rc = 0);
2221 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2222 GOTO(unlock, rc = 1);
2224 /* The old ".." name entry maybe not exist. */
2225 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2228 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2229 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2233 rc = dt_xattr_set(env, obj, &linkea_buf,
2234 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2236 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2239 dt_write_unlock(env, obj);
2242 dt_trans_stop(env, dev, th);
2245 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2246 "the object "DFID", new parent "DFID": rc = %d\n",
2247 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2251 struct lfsck_namespace *ns = com->lc_file_ram;
2253 ns->ln_flags |= LF_INCONSISTENT;
2260 * Handle orphan @obj during Double Scan Directory.
2262 * Remove the @obj's current (invalid) linkEA entries, and insert
2263 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2264 * ${FID}-${PFID}-D-${conflict_version}
2266 * The caller should take the ldlm lock before the calling.
2268 * \param[in] env pointer to the thread context
2269 * \param[in] com pointer to the lfsck component
2270 * \param[in] obj pointer to the orphan object to be handled
2271 * \param[in] pfid the new fid for the object's ".." name entry
2272 * \param[in,out] lh ldlm lock handler for the given @obj
2273 * \param[out] type to tell the caller what the inconsistency is
2275 * \retval positive number for repaired cases
2276 * \retval 0 if nothing to be repaired
2277 * \retval negative error number on failure
2280 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2281 struct lfsck_component *com,
2282 struct dt_object *obj,
2283 const struct lu_fid *pfid,
2284 struct lustre_handle *lh,
2285 enum lfsck_namespace_inconsistency_type *type)
2287 struct lfsck_thread_info *info = lfsck_env_info(env);
2288 struct lfsck_namespace *ns = com->lc_file_ram;
2292 /* Remove the unrecognized linkEA. */
2293 rc = lfsck_namespace_links_remove(env, com, obj);
2294 lfsck_ibits_unlock(lh, LCK_EX);
2295 if (rc < 0 && rc != -ENODATA)
2298 *type = LNIT_MUL_REF;
2300 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2301 * ever tried to verify some remote MDT-object that resides on this
2302 * MDT, but this MDT failed to respond such request. So means there
2303 * may be some remote name entry on other MDT that references this
2304 * object with another name, so we cannot know whether this linkEA
2305 * is valid or not. So keep it there and maybe resolved when next
2307 if (ns->ln_flags & LF_INCOMPLETE)
2310 /* The unique linkEA is invalid, even if the ".." name entry may be
2311 * valid, we still cannot know via which name entry this directory
2312 * will be referenced. Then handle it as pure orphan. */
2313 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2314 "-"DFID, PFID(pfid));
2315 rc = lfsck_namespace_insert_orphan(env, com, obj,
2316 info->lti_tmpbuf, "D", NULL);
2322 * Double Scan Directory object for single linkEA entry case.
2324 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2325 * then check whether the name is in the namespace or not, if not, add the
2326 * missing name entry back to namespace. If the linkEA entry is invalid,
2327 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2330 * \param[in] env pointer to the thread context
2331 * \param[in] com pointer to the lfsck component
2332 * \param[in] child pointer to the directory to be double scanned
2333 * \param[in] pfid the FID corresponding to the ".." entry
2334 * \param[in] ldata pointer to the linkEA data for the given @child
2335 * \param[in,out] lh ldlm lock handler for the given @child
2336 * \param[out] type to tell the caller what the inconsistency is
2337 * \param[in] retry if found inconsistency, but the caller does not hold
2338 * ldlm lock on the @child, then set @retry as true
2340 * \retval positive number for repaired cases
2341 * \retval 0 if nothing to be repaired
2342 * \retval negative error number on failure
2345 lfsck_namespace_dsd_single(const struct lu_env *env,
2346 struct lfsck_component *com,
2347 struct dt_object *child,
2348 const struct lu_fid *pfid,
2349 struct linkea_data *ldata,
2350 struct lustre_handle *lh,
2351 enum lfsck_namespace_inconsistency_type *type,
2354 struct lfsck_thread_info *info = lfsck_env_info(env);
2355 struct lu_name *cname = &info->lti_name;
2356 const struct lu_fid *cfid = lfsck_dto2fid(child);
2358 struct lfsck_namespace *ns = com->lc_file_ram;
2359 struct lfsck_instance *lfsck = com->lc_lfsck;
2360 struct dt_object *parent = NULL;
2361 struct lmv_mds_md_v1 *lmv;
2365 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid, info->lti_key);
2366 /* The unique linkEA entry with bad parent will be handled as orphan. */
2367 if (!fid_is_sane(&tfid)) {
2368 if (!lustre_handle_is_used(lh) && retry != NULL)
2371 rc = lfsck_namespace_dsd_orphan(env, com, child,
2377 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2379 GOTO(out, rc = PTR_ERR(parent));
2381 /* We trust the unique linkEA entry in spite of whether it matches the
2382 * ".." name entry or not. Because even if the linkEA entry is wrong
2383 * and the ".." name entry is right, we still cannot know via which
2384 * name entry the child will be referenced, since all known entries
2385 * have been verified during the first-stage scanning. */
2386 if (!dt_object_exists(parent)) {
2387 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2388 * has ever tried to verify some remote MDT-object that resides
2389 * on this MDT, but this MDT failed to respond such request. So
2390 * means there may be some remote name entry on other MDT that
2391 * references this object with another name, so we cannot know
2392 * whether this linkEA is valid or not. So keep it there and
2393 * maybe resolved when next LFSCK run. */
2394 if (ns->ln_flags & LF_INCOMPLETE)
2397 if (!lustre_handle_is_used(lh) && retry != NULL) {
2403 lfsck_ibits_unlock(lh, LCK_EX);
2406 lmv = &info->lti_lmv;
2407 rc = lfsck_read_stripe_lmv(env, child, lmv);
2408 if (rc != 0 && rc != -ENODATA)
2411 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2413 } else if (lfsck_shard_name_to_index(env,
2414 cname->ln_name, cname->ln_namelen,
2415 S_IFDIR, cfid) < 0) {
2416 /* It is an invalid name entry, we
2417 * cannot trust the parent also. */
2418 rc = lfsck_namespace_shrink_linkea(env, com, child,
2419 ldata, cname, &tfid, true);
2423 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2424 "-"DFID, PFID(pfid));
2425 rc = lfsck_namespace_insert_orphan(env, com, child,
2426 info->lti_tmpbuf, "S", NULL);
2431 /* Create the lost parent as an orphan. */
2432 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2434 /* Add the missing name entry to the parent. */
2435 rc = lfsck_namespace_insert_normal(env, com, parent,
2436 child, cname->ln_name);
2437 if (unlikely(rc == -EEXIST)) {
2438 /* Unfortunately, someone reused the name
2439 * under the parent by race. So we have
2440 * to remove the linkEA entry from
2441 * current child object. It means that the
2442 * LFSCK cannot recover the system
2443 * totally back to its original status,
2444 * but it is necessary to make the
2445 * current system to be consistent. */
2446 rc = lfsck_namespace_shrink_linkea(env,
2448 cname, &tfid, true);
2450 snprintf(info->lti_tmpbuf,
2451 sizeof(info->lti_tmpbuf),
2452 "-"DFID, PFID(pfid));
2453 rc = lfsck_namespace_insert_orphan(env,
2454 com, child, info->lti_tmpbuf,
2463 /* The unique linkEA entry with bad parent will be handled as orphan. */
2464 if (unlikely(!dt_try_as_dir(env, parent))) {
2465 if (!lustre_handle_is_used(lh) && retry != NULL)
2468 rc = lfsck_namespace_dsd_orphan(env, com, child,
2474 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2475 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2476 if (rc == -ENOENT) {
2477 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2478 * has ever tried to verify some remote MDT-object that resides
2479 * on this MDT, but this MDT failed to respond such request. So
2480 * means there may be some remote name entry on other MDT that
2481 * references this object with another name, so we cannot know
2482 * whether this linkEA is valid or not. So keep it there and
2483 * maybe resolved when next LFSCK run. */
2484 if (ns->ln_flags & LF_INCOMPLETE)
2487 if (!lustre_handle_is_used(lh) && retry != NULL) {
2493 lfsck_ibits_unlock(lh, LCK_EX);
2494 rc = lfsck_namespace_check_name(env, parent, child, cname);
2501 /* It is an invalid name entry, drop it. */
2502 if (unlikely(rc > 0)) {
2503 rc = lfsck_namespace_shrink_linkea(env, com, child,
2504 ldata, cname, &tfid, true);
2506 snprintf(info->lti_tmpbuf,
2507 sizeof(info->lti_tmpbuf),
2508 "-"DFID, PFID(pfid));
2509 rc = lfsck_namespace_insert_orphan(env, com,
2510 child, info->lti_tmpbuf, "D", NULL);
2516 /* Add the missing name entry back to the namespace. */
2517 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2519 if (unlikely(rc == -ESTALE))
2520 /* It may happen when the remote object has been
2521 * removed, but the local MDT is not aware of that. */
2524 if (unlikely(rc == -EEXIST)) {
2525 /* Unfortunately, someone reused the name under the
2526 * parent by race. So we have to remove the linkEA
2527 * entry from current child object. It means that the
2528 * LFSCK cannot recover the system totally back to
2529 * its original status, but it is necessary to make
2530 * the current system to be consistent.
2532 * It also may be because of the LFSCK found some
2533 * internal status of create operation. Under such
2534 * case, nothing to be done. */
2535 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2536 parent, child, ldata, cname, &tfid);
2538 snprintf(info->lti_tmpbuf,
2539 sizeof(info->lti_tmpbuf),
2540 "-"DFID, PFID(pfid));
2541 rc = lfsck_namespace_insert_orphan(env, com,
2542 child, info->lti_tmpbuf, "D", NULL);
2552 if (!lu_fid_eq(&tfid, cfid)) {
2553 if (!lustre_handle_is_used(lh) && retry != NULL) {
2559 lfsck_ibits_unlock(lh, LCK_EX);
2560 /* The name entry references another MDT-object that
2561 * may be created by the LFSCK for repairing dangling
2562 * name entry. Try to replace it. */
2563 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2566 rc = lfsck_namespace_dsd_orphan(env, com, child,
2572 if (fid_is_zero(pfid))
2575 /* The ".." name entry is wrong, update it. */
2576 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2577 if (!lustre_handle_is_used(lh) && retry != NULL) {
2583 *type = LNIT_UNMATCHED_PAIRS;
2584 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2585 lfsck_dto2fid(parent), cname);
2591 if (parent != NULL && !IS_ERR(parent))
2592 lfsck_object_put(env, parent);
2598 * Double Scan Directory object for multiple linkEA entries case.
2600 * The given @child has multiple linkEA entries. There is at most one linkEA
2601 * entry will be valid, all the others will be removed. Firstly, the function
2602 * will try to find out the linkEA entry for which the name entry exists under
2603 * the given parent (@pfid). If there is no linkEA entry that matches the given
2604 * ".." name entry, then tries to find out the first linkEA entry that both the
2605 * parent and the name entry exist to rebuild a new ".." name entry.
2607 * \param[in] env pointer to the thread context
2608 * \param[in] com pointer to the lfsck component
2609 * \param[in] child pointer to the directory to be double scanned
2610 * \param[in] pfid the FID corresponding to the ".." entry
2611 * \param[in] ldata pointer to the linkEA data for the given @child
2612 * \param[in,out] lh ldlm lock handler for the given @child
2613 * \param[out] type to tell the caller what the inconsistency is
2614 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2616 * \retval positive number for repaired cases
2617 * \retval 0 if nothing to be repaired
2618 * \retval negative error number on failure
2621 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2622 struct lfsck_component *com,
2623 struct dt_object *child,
2624 const struct lu_fid *pfid,
2625 struct linkea_data *ldata,
2626 struct lustre_handle *lh,
2627 enum lfsck_namespace_inconsistency_type *type,
2630 struct lfsck_thread_info *info = lfsck_env_info(env);
2631 struct lu_name *cname = &info->lti_name;
2632 const struct lu_fid *cfid = lfsck_dto2fid(child);
2633 struct lu_fid *pfid2 = &info->lti_fid3;
2635 struct lfsck_namespace *ns = com->lc_file_ram;
2636 struct lfsck_instance *lfsck = com->lc_lfsck;
2637 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2638 struct dt_object *parent = NULL;
2639 struct linkea_data ldata_new = { NULL };
2640 int dirent_count = 0;
2641 int linkea_count = 0;
2647 while (ldata->ld_lee != NULL) {
2648 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2650 /* Drop repeated linkEA entries. */
2651 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2652 /* Drop invalid linkEA entry. */
2653 if (!fid_is_sane(&tfid)) {
2654 linkea_del_buf(ldata, cname);
2659 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2660 * then it is possible that: the directry object has ever
2661 * been lost, but its name entry was there. In the former
2662 * LFSCK run, during the first-stage scanning, the LFSCK
2663 * found the dangling name entry, but it did not recreate
2664 * the lost object, and when moved to the second-stage
2665 * scanning, some children objects of the lost directory
2666 * object were found, then the LFSCK recreated such lost
2667 * directory object as an orphan.
2669 * When the LFSCK runs again, if the dangling name is still
2670 * there, the LFSCK should move the orphan directory object
2671 * back to the normal namespace. */
2672 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2673 linkea_next_entry(ldata);
2677 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2679 RETURN(PTR_ERR(parent));
2681 if (!dt_object_exists(parent)) {
2682 lfsck_object_put(env, parent);
2683 if (ldata->ld_leh->leh_reccount > 1) {
2684 /* If it is NOT the last linkEA entry, then
2685 * there is still other chance to make the
2686 * child to be visible via other parent, then
2687 * remove this linkEA entry. */
2688 linkea_del_buf(ldata, cname);
2696 /* The linkEA entry with bad parent will be removed. */
2697 if (unlikely(!dt_try_as_dir(env, parent))) {
2698 lfsck_object_put(env, parent);
2699 linkea_del_buf(ldata, cname);
2704 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2705 (const struct dt_key *)cname->ln_name,
2707 *pfid2 = *lfsck_dto2fid(parent);
2708 if (rc == -ENOENT) {
2709 lfsck_object_put(env, parent);
2710 linkea_next_entry(ldata);
2715 lfsck_object_put(env, parent);
2720 if (lu_fid_eq(&tfid, cfid)) {
2721 lfsck_object_put(env, parent);
2722 if (!lu_fid_eq(pfid, pfid2)) {
2723 *type = LNIT_UNMATCHED_PAIRS;
2724 rc = lfsck_namespace_repair_unmatched_pairs(env,
2725 com, child, pfid2, cname);
2731 /* It is the most common case that we find the
2732 * name entry corresponding to the linkEA entry
2733 * that matches the ".." name entry. */
2734 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2738 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2742 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2747 linkea_del_buf(ldata, cname);
2749 linkea_first_entry(ldata);
2750 /* There may be some invalid dangling name entries under
2751 * other parent directories, remove all of them. */
2752 while (ldata->ld_lee != NULL) {
2753 lfsck_namespace_unpack_linkea_entry(ldata,
2754 cname, &tfid, info->lti_key);
2755 if (!fid_is_sane(&tfid))
2758 parent = lfsck_object_find_bottom(env, lfsck,
2760 if (IS_ERR(parent)) {
2761 rc = PTR_ERR(parent);
2762 if (rc != -ENOENT &&
2763 bk->lb_param & LPF_FAILOUT)
2769 if (!dt_object_exists(parent)) {
2770 lfsck_object_put(env, parent);
2774 rc = lfsck_namespace_repair_dirent(env, com,
2775 parent, child, cname->ln_name,
2776 cname->ln_name, S_IFDIR, false, true);
2777 lfsck_object_put(env, parent);
2779 if (bk->lb_param & LPF_FAILOUT)
2788 linkea_del_buf(ldata, cname);
2791 ns->ln_dirent_repaired += dirent_count;
2796 lfsck_ibits_unlock(lh, LCK_EX);
2797 /* The name entry references another MDT-object that may be
2798 * created by the LFSCK for repairing dangling name entry.
2799 * Try to replace it. */
2800 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2802 lfsck_object_put(env, parent);
2809 linkea_del_buf(ldata, cname);
2812 linkea_first_entry(ldata);
2813 if (ldata->ld_leh->leh_reccount == 1) {
2814 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2817 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2818 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2824 /* All linkEA entries are invalid and removed, then handle the @child
2826 if (ldata->ld_leh->leh_reccount == 0) {
2827 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2833 /* If the dangling name entry for the orphan directory object has
2834 * been remvoed, then just check whether the directory object is
2835 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2841 /* There is no linkEA entry that matches the ".." name entry. Find
2842 * the first linkEA entry that both parent and name entry exist to
2843 * rebuild a new ".." name entry. */
2853 * Repair the object's nlink attribute.
2855 * If all the known name entries have been verified, then the object's hard
2856 * link attribute should match the object's linkEA entries count unless the
2857 * object's has too much hard link to be recorded in the linkEA. Such cases
2858 * should have been marked in the LFSCK trace file. Otherwise, trust the
2859 * linkEA to update the object's nlink attribute.
2861 * \param[in] env pointer to the thread context
2862 * \param[in] com pointer to the lfsck component
2863 * \param[in] obj pointer to the dt_object to be handled
2864 * \param[in,out] la pointer to buffer to object's attribute before
2865 * and after the repairing
2867 * \retval positive number for repaired cases
2868 * \retval 0 if nothing to be repaired
2869 * \retval negative error number on failure
2871 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2872 struct lfsck_component *com,
2873 struct dt_object *obj,
2876 struct lfsck_thread_info *info = lfsck_env_info(env);
2877 struct lu_fid *tfid = &info->lti_fid3;
2878 struct lfsck_namespace *ns = com->lc_file_ram;
2879 struct lfsck_instance *lfsck = com->lc_lfsck;
2880 struct dt_device *dev = lfsck_obj2dev(obj);
2881 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2882 struct thandle *th = NULL;
2883 struct linkea_data ldata = { NULL };
2884 struct lustre_handle lh = { 0 };
2885 __u32 old = la->la_nlink;
2891 LASSERT(!dt_object_remote(obj));
2892 LASSERT(S_ISREG(lfsck_object_type(obj)));
2894 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2895 MDS_INODELOCK_UPDATE |
2896 MDS_INODELOCK_XATTR, LCK_EX);
2900 th = dt_trans_create(env, dev);
2902 GOTO(log, rc = PTR_ERR(th));
2904 la->la_valid = LA_NLINK;
2905 rc = dt_declare_attr_set(env, obj, la, th);
2909 rc = dt_trans_start_local(env, dev, th);
2913 dt_write_lock(env, obj, 0);
2914 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2915 * ever tried to verify some remote MDT-object that resides on this
2916 * MDT, but this MDT failed to respond such request. So means there
2917 * may be some remote name entry on other MDT that references this
2918 * object with another name, so we cannot know whether this linkEA
2919 * is valid or not. So keep it there and maybe resolved when next
2921 if (ns->ln_flags & LF_INCOMPLETE)
2922 GOTO(unlock, rc = 0);
2924 fid_cpu_to_be(tfid, cfid);
2925 idx = lfsck_sub_trace_file_fid2idx(cfid);
2926 rc = dt_lookup(env, com->lc_sub_trace_objs[idx].lsto_obj,
2927 (struct dt_rec *)&flags, (const struct dt_key *)tfid,
2932 if (flags & LNTF_SKIP_NLINK)
2933 GOTO(unlock, rc = 0);
2935 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2937 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2939 rc = lfsck_links_read2(env, obj, &ldata);
2941 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2943 if (la->la_nlink == ldata.ld_leh->leh_reccount ||
2944 unlikely(la->la_nlink == 0))
2945 GOTO(unlock, rc = 0);
2947 la->la_nlink = ldata.ld_leh->leh_reccount;
2948 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2949 GOTO(unlock, rc = 1);
2951 rc = dt_attr_set(env, obj, la, th, BYPASS_CAPA);
2953 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2956 dt_write_unlock(env, obj);
2959 dt_trans_stop(env, dev, th);
2962 lfsck_ibits_unlock(&lh, LCK_EX);
2963 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2964 "nlink count from %u to %u: rc = %d\n",
2965 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2968 ns->ln_flags |= LF_INCONSISTENT;
2974 * Double scan the directory object for namespace LFSCK.
2976 * This function will verify the <parent, child> pairs in the namespace tree:
2977 * the parent references the child via some name entry that should be in the
2978 * child's linkEA entry, the child should back references the parent via its
2981 * The LFSCK will scan every linkEA entry in turn until find out the first
2982 * matched pairs. If found, then all other linkEA entries will be dropped.
2983 * If all the linkEA entries cannot match the ".." name entry, then there
2984 * are serveral possible cases:
2986 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2987 * in the linkEA entry is valid.
2989 * 2) If there are multiple linkEA entries, then try to find the linkEA
2990 * that matches the ".." name entry. If found, then all other entries
2991 * are invalid; otherwise, it is quite possible that the ".." name entry
2992 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2993 * entry according to the first valid linkEA entry (both the parent and
2994 * the name entry should exist).
2996 * 3) If the directory object has no (valid) linkEA entry, then the
2997 * directory object will be handled as pure orphan and inserted
2998 * in the .lustre/lost+found/MDTxxxx/ with the name:
2999 * ${self_FID}-${PFID}-D-${conflict_version}
3001 * \param[in] env pointer to the thread context
3002 * \param[in] com pointer to the lfsck component
3003 * \param[in] child pointer to the directory object to be handled
3004 * \param[in] flags to indicate the specical checking on the @child
3006 * \retval positive number for repaired cases
3007 * \retval 0 if nothing to be repaired
3008 * \retval negative error number on failure
3010 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3011 struct lfsck_component *com,
3012 struct dt_object *child, __u8 flags)
3014 struct lfsck_thread_info *info = lfsck_env_info(env);
3015 const struct lu_fid *cfid = lfsck_dto2fid(child);
3016 struct lu_fid *pfid = &info->lti_fid2;
3017 struct lfsck_namespace *ns = com->lc_file_ram;
3018 struct lfsck_instance *lfsck = com->lc_lfsck;
3019 struct lustre_handle lh = { 0 };
3020 struct linkea_data ldata = { NULL };
3021 bool unknown = false;
3024 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3028 LASSERT(!dt_object_remote(child));
3030 if (flags & LNTF_UNCERTAIN_LMV) {
3031 if (flags & LNTF_RECHECK_NAME_HASH) {
3032 rc = lfsck_namespace_scan_shard(env, com, child);
3036 ns->ln_striped_shards_scanned++;
3038 ns->ln_striped_shards_skipped++;
3042 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3046 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3047 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3048 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3049 "the namespace LFSCK, then the LFSCK cannot guarantee"
3050 "all the name entries have been verified in first-stage"
3051 "scanning. So have to skip orphan related handling for"
3052 "the directory object "DFID" with remote name entry\n",
3053 lfsck_lfsck2name(lfsck), PFID(cfid));
3058 if (unlikely(!dt_try_as_dir(env, child)))
3059 GOTO(out, rc = -ENOTDIR);
3061 /* We only take ldlm lock on the @child when required. When the
3062 * logic comes here for the first time, it is always false. */
3066 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3067 MDS_INODELOCK_UPDATE |
3068 MDS_INODELOCK_XATTR, LCK_EX);
3073 dt_read_lock(env, child, 0);
3074 if (unlikely(lfsck_is_dead_obj(child))) {
3075 dt_read_unlock(env, child);
3080 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3081 (const struct dt_key *)dotdot, BYPASS_CAPA);
3083 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3084 dt_read_unlock(env, child);
3089 if (!lustre_handle_is_used(&lh)) {
3090 dt_read_unlock(env, child);
3095 } else if (lfsck->li_lpf_obj != NULL &&
3096 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3098 } else if (unlikely(!fid_is_sane(pfid))) {
3102 rc = lfsck_links_read(env, child, &ldata);
3103 dt_read_unlock(env, child);
3105 if (rc != -ENODATA && rc != -EINVAL)
3108 if (!lustre_handle_is_used(&lh))
3111 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3112 /* Remove the corrupted linkEA. */
3113 rc = lfsck_namespace_links_remove(env, com, child);
3115 /* Here, because of the crashed linkEA, we
3116 * cannot know whether there is some parent
3117 * that references the child directory via
3118 * some name entry or not. So keep it there,
3119 * when the LFSCK run next time, if there is
3120 * some parent that references this object,
3121 * then the LFSCK can rebuild the linkEA;
3122 * otherwise, this object will be handled
3123 * as orphan as above. */
3126 /* 1. If we have neither ".." nor linkEA,
3127 * then it is an orphan.
3129 * 2. If we only have the ".." name entry,
3130 * but no parent references this child
3131 * directory, then handle it as orphan. */
3132 lfsck_ibits_unlock(&lh, LCK_EX);
3133 type = LNIT_MUL_REF;
3135 /* If the LFSCK is marked as LF_INCOMPLETE,
3136 * then means some MDT has ever tried to
3137 * verify some remote MDT-object that resides
3138 * on this MDT, but this MDT failed to respond
3139 * such request. So means there may be some
3140 * remote name entry on other MDT that
3141 * references this object with another name,
3142 * so we cannot know whether this linkEA is
3143 * valid or not. So keep it there and maybe
3144 * resolved when next LFSCK run. */
3145 if (ns->ln_flags & LF_INCOMPLETE)
3148 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3149 "-"DFID, PFID(pfid));
3150 rc = lfsck_namespace_insert_orphan(env, com, child,
3151 info->lti_tmpbuf, "D", NULL);
3157 linkea_first_entry(&ldata);
3158 /* This is the most common case: the object has unique linkEA entry. */
3159 if (ldata.ld_leh->leh_reccount == 1) {
3160 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3161 &lh, &type, &retry);
3163 LASSERT(!lustre_handle_is_used(&lh));
3172 if (!lustre_handle_is_used(&lh))
3175 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3176 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3182 /* When we come here, the cases usually like that:
3183 * 1) The directory object has a corrupted linkEA entry. During the
3184 * first-stage scanning, the LFSCK cannot know such corruption,
3185 * then it appends the right linkEA entry according to the found
3186 * name entry after the bad one.
3188 * 2) The directory object has a right linkEA entry. During the
3189 * first-stage scanning, the LFSCK finds some bad name entry,
3190 * but the LFSCK cannot aware that at that time, then it adds
3191 * the bad linkEA entry for further processing. */
3192 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3198 lfsck_ibits_unlock(&lh, LCK_EX);
3201 case LNIT_BAD_LINKEA:
3202 ns->ln_linkea_repaired++;
3204 case LNIT_UNMATCHED_PAIRS:
3205 ns->ln_unmatched_pairs_repaired++;
3208 ns->ln_mul_ref_repaired++;
3216 ns->ln_unknown_inconsistency++;
3222 * Double scan the MDT-object for namespace LFSCK.
3224 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3225 * those entries from the linkEA; if the linkEA becomes empty or the object
3226 * has no linkEA, then it is an orphan and will be added into the directory
3227 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3228 * the remote parent; if the name entry corresponding to some linkEA entry
3229 * is lost, then add the name entry back to the namespace.
3231 * \param[in] env pointer to the thread context
3232 * \param[in] com pointer to the lfsck component
3233 * \param[in] child pointer to the dt_object to be handled
3234 * \param[in] flags some hints to indicate how the @child should be handled
3236 * \retval positive number for repaired cases
3237 * \retval 0 if nothing to be repaired
3238 * \retval negative error number on failure
3240 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3241 struct lfsck_component *com,
3242 struct dt_object *child, __u8 flags)
3244 struct lfsck_thread_info *info = lfsck_env_info(env);
3245 struct lu_attr *la = &info->lti_la;
3246 struct lu_name *cname = &info->lti_name;
3247 struct lu_fid *pfid = &info->lti_fid;
3248 struct lu_fid *cfid = &info->lti_fid2;
3249 struct lfsck_instance *lfsck = com->lc_lfsck;
3250 struct lfsck_namespace *ns = com->lc_file_ram;
3251 struct dt_object *parent = NULL;
3252 struct linkea_data ldata = { NULL };
3253 bool repaired = false;
3258 dt_read_lock(env, child, 0);
3259 if (unlikely(lfsck_is_dead_obj(child))) {
3260 dt_read_unlock(env, child);
3265 if (S_ISDIR(lfsck_object_type(child))) {
3266 dt_read_unlock(env, child);
3267 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3272 rc = lfsck_links_read(env, child, &ldata);
3273 dt_read_unlock(env, child);
3277 linkea_first_entry(&ldata);
3278 while (ldata.ld_lee != NULL) {
3279 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3281 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3283 /* Found repeated linkEA entries */
3285 rc = lfsck_namespace_shrink_linkea(env, com, child,
3286 &ldata, cname, pfid, false);
3298 /* Invalid PFID in the linkEA entry. */
3299 if (!fid_is_sane(pfid)) {
3300 rc = lfsck_namespace_shrink_linkea(env, com, child,
3301 &ldata, cname, pfid, true);
3311 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3313 GOTO(out, rc = PTR_ERR(parent));
3315 if (!dt_object_exists(parent)) {
3318 if (ldata.ld_leh->leh_reccount > 1) {
3319 /* If it is NOT the last linkEA entry, then
3320 * there is still other chance to make the
3321 * child to be visible via other parent, then
3322 * remove this linkEA entry. */
3323 rc = lfsck_namespace_shrink_linkea(env, com,
3324 child, &ldata, cname, pfid, true);
3326 /* If the LFSCK is marked as LF_INCOMPLETE,
3327 * then means some MDT has ever tried to
3328 * verify some remote MDT-object that resides
3329 * on this MDT, but this MDT failed to respond
3330 * such request. So means there may be some
3331 * remote name entry on other MDT that
3332 * references this object with another name,
3333 * so we cannot know whether this linkEA is
3334 * valid or not. So keep it there and maybe
3335 * resolved when next LFSCK run. */
3336 if (ns->ln_flags & LF_INCOMPLETE) {
3337 lfsck_object_put(env, parent);
3342 /* Create the lost parent as an orphan. */
3343 rc = lfsck_namespace_create_orphan_dir(env, com,
3346 lfsck_object_put(env, parent);
3354 /* Add the missing name entry to the parent. */
3355 rc = lfsck_namespace_insert_normal(env, com,
3356 parent, child, cname->ln_name);
3357 if (unlikely(rc == -EEXIST))
3358 /* Unfortunately, someone reused the
3359 * name under the parent by race. So we
3360 * have to remove the linkEA entry from
3361 * current child object. It means that
3362 * the LFSCK cannot recover the system
3363 * totally back to its original status,
3364 * but it is necessary to make the
3365 * current system to be consistent. */
3366 rc = lfsck_namespace_shrink_linkea(env,
3370 linkea_next_entry(&ldata);
3373 lfsck_object_put(env, parent);
3383 /* The linkEA entry with bad parent will be removed. */
3384 if (unlikely(!dt_try_as_dir(env, parent))) {
3385 lfsck_object_put(env, parent);
3386 rc = lfsck_namespace_shrink_linkea(env, com, child,
3387 &ldata, cname, pfid, true);
3397 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3398 (const struct dt_key *)cname->ln_name,
3400 if (rc != 0 && rc != -ENOENT) {
3401 lfsck_object_put(env, parent);
3407 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3408 /* It is the most common case that we
3409 * find the name entry corresponding
3410 * to the linkEA entry. */
3411 lfsck_object_put(env, parent);
3412 linkea_next_entry(&ldata);
3414 /* The name entry references another
3415 * MDT-object that may be created by
3416 * the LFSCK for repairing dangling
3417 * name entry. Try to replace it. */
3418 rc = lfsck_namespace_replace_cond(env, com,
3419 parent, child, cfid, cname);
3420 lfsck_object_put(env, parent);
3426 linkea_next_entry(&ldata);
3428 rc = lfsck_namespace_shrink_linkea(env,
3442 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3446 /* If there is no name entry in the parent dir and the object
3447 * link count is less than the linkea entries count, then the
3448 * linkea entry should be removed. */
3449 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3450 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3451 parent, child, &ldata, cname, pfid);
3452 lfsck_object_put(env, parent);
3462 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3463 * MDT has ever tried to verify some remote MDT-object that
3464 * resides on this MDT, but this MDT failed to respond such
3465 * request. So means there may be some remote name entry on
3466 * other MDT that references this object with another name,
3467 * so we cannot know whether this linkEA is valid or not.
3468 * So keep it there and maybe resolved when next LFSCK run. */
3469 if (ns->ln_flags & LF_INCOMPLETE) {
3470 lfsck_object_put(env, parent);
3475 rc = lfsck_namespace_check_name(env, parent, child, cname);
3480 lfsck_object_put(env, parent);
3485 /* It is an invalid name entry, drop it. */
3486 if (unlikely(rc > 0)) {
3487 lfsck_object_put(env, parent);
3488 rc = lfsck_namespace_shrink_linkea(env, com, child,
3489 &ldata, cname, pfid, true);
3499 /* Add the missing name entry back to the namespace. */
3500 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3502 if (unlikely(rc == -ESTALE))
3503 /* It may happen when the remote object has been
3504 * removed, but the local MDT is not aware of that. */
3507 if (unlikely(rc == -EEXIST))
3508 /* Unfortunately, someone reused the name under the
3509 * parent by race. So we have to remove the linkEA
3510 * entry from current child object. It means that the
3511 * LFSCK cannot recover the system totally back to
3512 * its original status, but it is necessary to make
3513 * the current system to be consistent.
3515 * It also may be because of the LFSCK found some
3516 * internal status of create operation. Under such
3517 * case, nothing to be done. */
3518 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3519 parent, child, &ldata, cname, pfid);
3521 linkea_next_entry(&ldata);
3523 lfsck_object_put(env, parent);
3534 if (rc < 0 && rc != -ENODATA)
3538 LASSERT(ldata.ld_leh != NULL);
3540 count = ldata.ld_leh->leh_reccount;
3544 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3545 * MDT has ever tried to verify some remote MDT-object that
3546 * resides on this MDT, but this MDT failed to respond such
3547 * request. So means there may be some remote name entry on
3548 * other MDT that references this object with another name,
3549 * so we cannot know whether this linkEA is valid or not.
3550 * So keep it there and maybe resolved when next LFSCK run. */
3551 if (!(ns->ln_flags & LF_INCOMPLETE)) {
3552 /* If the child becomes orphan, then insert it into
3553 * the global .lustre/lost+found/MDTxxxx directory. */
3554 rc = lfsck_namespace_insert_orphan(env, com, child,
3560 ns->ln_mul_ref_repaired++;
3565 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3569 if (la->la_nlink != 0 && la->la_nlink != count) {
3570 rc = lfsck_namespace_repair_nlink(env, com, child, la);
3572 ns->ln_objs_nlink_repaired++;
3579 if (la->la_nlink > 1)
3580 ns->ln_mul_linked_repaired++;
3589 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3590 struct lfsck_namespace *ns,
3591 __u64 checked_phase1,
3592 __u64 checked_phase2,
3596 seq_printf(m, "checked_phase1: "LPU64"\n"
3597 "checked_phase2: "LPU64"\n"
3598 "updated_phase1: "LPU64"\n"
3599 "updated_phase2: "LPU64"\n"
3600 "failed_phase1: "LPU64"\n"
3601 "failed_phase2: "LPU64"\n"
3602 "directories: "LPU64"\n"
3603 "dirent_repaired: "LPU64"\n"
3604 "linkea_repaired: "LPU64"\n"
3605 "nlinks_repaired: "LPU64"\n"
3606 "multiple_linked_checked: "LPU64"\n"
3607 "multiple_linked_repaired: "LPU64"\n"
3608 "unknown_inconsistency: "LPU64"\n"
3609 "unmatched_pairs_repaired: "LPU64"\n"
3610 "dangling_repaired: "LPU64"\n"
3611 "multiple_referenced_repaired: "LPU64"\n"
3612 "bad_file_type_repaired: "LPU64"\n"
3613 "lost_dirent_repaired: "LPU64"\n"
3614 "local_lost_found_scanned: "LPU64"\n"
3615 "local_lost_found_moved: "LPU64"\n"
3616 "local_lost_found_skipped: "LPU64"\n"
3617 "local_lost_found_failed: "LPU64"\n"
3618 "striped_dirs_scanned: "LPU64"\n"
3619 "striped_dirs_repaired: "LPU64"\n"
3620 "striped_dirs_failed: "LPU64"\n"
3621 "striped_dirs_disabled: "LPU64"\n"
3622 "striped_dirs_skipped: "LPU64"\n"
3623 "striped_shards_scanned: "LPU64"\n"
3624 "striped_shards_repaired: "LPU64"\n"
3625 "striped_shards_failed: "LPU64"\n"
3626 "striped_shards_skipped: "LPU64"\n"
3627 "name_hash_repaired: "LPU64"\n"
3628 "success_count: %u\n"
3629 "run_time_phase1: %u seconds\n"
3630 "run_time_phase2: %u seconds\n",
3633 ns->ln_items_repaired,
3634 ns->ln_objs_repaired_phase2,
3635 ns->ln_items_failed,
3636 ns->ln_objs_failed_phase2,
3637 ns->ln_dirs_checked,
3638 ns->ln_dirent_repaired,
3639 ns->ln_linkea_repaired,
3640 ns->ln_objs_nlink_repaired,
3641 ns->ln_mul_linked_checked,
3642 ns->ln_mul_linked_repaired,
3643 ns->ln_unknown_inconsistency,
3644 ns->ln_unmatched_pairs_repaired,
3645 ns->ln_dangling_repaired,
3646 ns->ln_mul_ref_repaired,
3647 ns->ln_bad_type_repaired,
3648 ns->ln_lost_dirent_repaired,
3649 ns->ln_local_lpf_scanned,
3650 ns->ln_local_lpf_moved,
3651 ns->ln_local_lpf_skipped,
3652 ns->ln_local_lpf_failed,
3653 ns->ln_striped_dirs_scanned,
3654 ns->ln_striped_dirs_repaired,
3655 ns->ln_striped_dirs_failed,
3656 ns->ln_striped_dirs_disabled,
3657 ns->ln_striped_dirs_skipped,
3658 ns->ln_striped_shards_scanned,
3659 ns->ln_striped_shards_repaired,
3660 ns->ln_striped_shards_failed,
3661 ns->ln_striped_shards_skipped,
3662 ns->ln_name_hash_repaired,
3663 ns->ln_success_count,
3668 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3669 struct lfsck_component *com)
3671 struct lfsck_instance *lfsck = com->lc_lfsck;
3672 struct lfsck_namespace *ns = com->lc_file_ram;
3674 while (!list_empty(&lfsck->li_list_lmv)) {
3675 struct lfsck_lmv_unit *llu;
3676 struct lfsck_lmv *llmv;
3678 llu = list_entry(lfsck->li_list_lmv.next,
3679 struct lfsck_lmv_unit, llu_link);
3680 llmv = &llu->llu_lmv;
3682 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3683 "still in using: %u\n",
3684 atomic_read(&llmv->ll_ref));
3686 ns->ln_striped_dirs_skipped++;
3687 lfsck_lmv_put(env, llmv);
3691 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3692 struct lfsck_component *com,
3693 struct dt_object *obj)
3695 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3698 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3702 /* zero-linkEA object may be orphan, but it also maybe because
3703 * of upgrading. Currently, we cannot record it for double scan.
3704 * Because it may cause the LFSCK trace file to be too large. */
3706 /* "la_ctime" == 1 means that it has ever been removed from
3707 * backend /lost+found directory but not been added back to
3708 * the normal namespace yet. */
3710 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3711 unlikely(la->la_ctime == 1))
3712 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3713 LNTF_CHECK_LINKEA, true);
3718 /* namespace APIs */
3720 static int lfsck_namespace_reset(const struct lu_env *env,
3721 struct lfsck_component *com, bool init)
3723 struct lfsck_instance *lfsck = com->lc_lfsck;
3724 struct lfsck_namespace *ns = com->lc_file_ram;
3725 struct lfsck_assistant_data *lad = com->lc_data;
3726 struct dt_object *root;
3727 struct dt_object *dto;
3731 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3733 GOTO(log, rc = PTR_ERR(root));
3735 if (unlikely(!dt_try_as_dir(env, root)))
3736 GOTO(put, rc = -ENOTDIR);
3738 down_write(&com->lc_sem);
3740 memset(ns, 0, sizeof(*ns));
3742 __u32 count = ns->ln_success_count;
3743 __u64 last_time = ns->ln_time_last_complete;
3745 memset(ns, 0, sizeof(*ns));
3746 ns->ln_success_count = count;
3747 ns->ln_time_last_complete = last_time;
3749 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3750 ns->ln_status = LS_INIT;
3752 lfsck_object_put(env, com->lc_obj);
3754 dto = lfsck_namespace_load_one_trace_file(env, com, root,
3755 LFSCK_NAMESPACE, NULL, true);
3757 GOTO(out, rc = PTR_ERR(dto));
3760 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3764 lad->lad_incomplete = 0;
3765 CFS_RESET_BITMAP(lad->lad_bitmap);
3767 rc = lfsck_namespace_store(env, com, true);
3772 up_write(&com->lc_sem);
3775 lfsck_object_put(env, root);
3777 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3778 lfsck_lfsck2name(lfsck), rc);
3783 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3786 struct lfsck_namespace *ns = com->lc_file_ram;
3788 down_write(&com->lc_sem);
3790 com->lc_new_checked++;
3791 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3792 up_write(&com->lc_sem);
3795 static void lfsck_namespace_close_dir(const struct lu_env *env,
3796 struct lfsck_component *com)
3798 struct lfsck_namespace *ns = com->lc_file_ram;
3799 struct lfsck_assistant_data *lad = com->lc_data;
3800 struct lfsck_instance *lfsck = com->lc_lfsck;
3801 struct lfsck_lmv *llmv = lfsck->li_lmv;
3802 struct lfsck_namespace_req *lnr;
3804 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3805 bool wakeup = false;
3811 OBD_ALLOC(lnr, size);
3813 ns->ln_striped_dirs_skipped++;
3818 /* Generate a dummy request to indicate that all shards' name entry
3819 * in this striped directory has been scanned for the first time. */
3820 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3821 lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3822 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3823 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3824 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3825 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3826 lnr->lnr_size = size;
3828 spin_lock(&lad->lad_lock);
3829 if (lad->lad_assistant_status < 0) {
3830 spin_unlock(&lad->lad_lock);
3831 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3832 ns->ln_striped_dirs_skipped++;
3837 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3838 if (lad->lad_prefetched == 0)
3841 lad->lad_prefetched++;
3842 spin_unlock(&lad->lad_lock);
3844 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3849 static int lfsck_namespace_open_dir(const struct lu_env *env,
3850 struct lfsck_component *com)
3852 struct lfsck_instance *lfsck = com->lc_lfsck;
3853 struct lfsck_namespace *ns = com->lc_file_ram;
3854 struct lfsck_lmv *llmv = lfsck->li_lmv;
3861 if (llmv->ll_lmv_master) {
3862 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3864 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
3865 lmv->lmv_master_mdt_index =
3866 lfsck_dev_idx(lfsck);
3867 ns->ln_flags |= LF_INCONSISTENT;
3868 llmv->ll_lmv_updated = 1;
3871 rc = lfsck_namespace_verify_stripe_slave(env, com,
3872 lfsck->li_obj_dir, llmv);
3875 RETURN(rc > 0 ? 0 : rc);
3878 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3879 struct lfsck_component *com, bool init)
3881 struct lfsck_instance *lfsck = com->lc_lfsck;
3882 struct lfsck_namespace *ns = com->lc_file_ram;
3886 rc = lfsck_checkpoint_generic(env, com);
3891 down_write(&com->lc_sem);
3893 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3895 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3896 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3897 HALF_SEC - lfsck->li_time_last_checkpoint);
3898 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3899 ns->ln_items_checked += com->lc_new_checked;
3900 com->lc_new_checked = 0;
3903 rc = lfsck_namespace_store(env, com, false);
3904 up_write(&com->lc_sem);
3907 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3908 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3909 lfsck->li_pos_current.lp_oit_cookie,
3910 PFID(&lfsck->li_pos_current.lp_dir_parent),
3911 lfsck->li_pos_current.lp_dir_cookie, rc);
3913 return rc > 0 ? 0 : rc;
3916 static int lfsck_namespace_prep(const struct lu_env *env,
3917 struct lfsck_component *com,
3918 struct lfsck_start_param *lsp)
3920 struct lfsck_instance *lfsck = com->lc_lfsck;
3921 struct lfsck_namespace *ns = com->lc_file_ram;
3922 struct lfsck_position *pos = &com->lc_pos_start;
3925 rc = lfsck_namespace_load_bitmap(env, com);
3926 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3927 rc = lfsck_namespace_reset(env, com, false);
3929 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3932 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3933 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3939 down_write(&com->lc_sem);
3940 ns->ln_time_latest_start = cfs_time_current_sec();
3941 spin_lock(&lfsck->li_lock);
3943 if (ns->ln_flags & LF_SCANNED_ONCE) {
3944 if (!lfsck->li_drop_dryrun ||
3945 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3946 ns->ln_status = LS_SCANNING_PHASE2;
3947 list_move_tail(&com->lc_link,
3948 &lfsck->li_list_double_scan);
3949 if (!list_empty(&com->lc_link_dir))
3950 list_del_init(&com->lc_link_dir);
3951 lfsck_pos_set_zero(pos);
3953 ns->ln_status = LS_SCANNING_PHASE1;
3954 ns->ln_run_time_phase1 = 0;
3955 ns->ln_run_time_phase2 = 0;
3956 ns->ln_items_checked = 0;
3957 ns->ln_items_repaired = 0;
3958 ns->ln_items_failed = 0;
3959 ns->ln_dirs_checked = 0;
3960 ns->ln_objs_checked_phase2 = 0;
3961 ns->ln_objs_repaired_phase2 = 0;
3962 ns->ln_objs_failed_phase2 = 0;
3963 ns->ln_objs_nlink_repaired = 0;
3964 ns->ln_dirent_repaired = 0;
3965 ns->ln_linkea_repaired = 0;
3966 ns->ln_mul_linked_checked = 0;
3967 ns->ln_mul_linked_repaired = 0;
3968 ns->ln_unknown_inconsistency = 0;
3969 ns->ln_unmatched_pairs_repaired = 0;
3970 ns->ln_dangling_repaired = 0;
3971 ns->ln_mul_ref_repaired = 0;
3972 ns->ln_bad_type_repaired = 0;
3973 ns->ln_lost_dirent_repaired = 0;
3974 ns->ln_striped_dirs_scanned = 0;
3975 ns->ln_striped_dirs_repaired = 0;
3976 ns->ln_striped_dirs_failed = 0;
3977 ns->ln_striped_dirs_disabled = 0;
3978 ns->ln_striped_dirs_skipped = 0;
3979 ns->ln_striped_shards_scanned = 0;
3980 ns->ln_striped_shards_repaired = 0;
3981 ns->ln_striped_shards_failed = 0;
3982 ns->ln_striped_shards_skipped = 0;
3983 ns->ln_name_hash_repaired = 0;
3984 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3985 if (list_empty(&com->lc_link_dir))
3986 list_add_tail(&com->lc_link_dir,
3987 &lfsck->li_list_dir);
3988 *pos = ns->ln_pos_first_inconsistent;
3991 ns->ln_status = LS_SCANNING_PHASE1;
3992 if (list_empty(&com->lc_link_dir))
3993 list_add_tail(&com->lc_link_dir,
3994 &lfsck->li_list_dir);
3995 if (!lfsck->li_drop_dryrun ||
3996 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3997 *pos = ns->ln_pos_last_checkpoint;
3998 pos->lp_oit_cookie++;
4000 *pos = ns->ln_pos_first_inconsistent;
4004 spin_unlock(&lfsck->li_lock);
4005 up_write(&com->lc_sem);
4007 rc = lfsck_start_assistant(env, com, lsp);
4009 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
4010 DFID", "LPX64"]: rc = %d\n",
4011 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4012 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4017 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4018 struct lfsck_component *com,
4019 struct dt_object *obj)
4021 struct lfsck_thread_info *info = lfsck_env_info(env);
4022 struct lfsck_namespace *ns = com->lc_file_ram;
4023 struct lfsck_instance *lfsck = com->lc_lfsck;
4024 const struct lu_fid *fid = lfsck_dto2fid(obj);
4025 struct lu_fid *pfid = &info->lti_fid2;
4026 struct lu_name *cname = &info->lti_name;
4027 struct lu_seq_range *range = &info->lti_range;
4028 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4029 struct linkea_data ldata = { NULL };
4030 __u32 idx = lfsck_dev_idx(lfsck);
4034 rc = lfsck_links_read(env, obj, &ldata);
4038 /* -EINVAL means crashed linkEA, should be verified. */
4039 if (rc == -EINVAL) {
4040 rc = lfsck_namespace_trace_update(env, com, fid,
4041 LNTF_CHECK_LINKEA, true);
4043 struct lustre_handle lh = { 0 };
4045 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4046 MDS_INODELOCK_UPDATE |
4047 MDS_INODELOCK_XATTR, LCK_EX);
4049 rc = lfsck_namespace_links_remove(env, com,
4051 lfsck_ibits_unlock(&lh, LCK_EX);
4055 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4058 if (rc == -ENODATA) {
4059 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4067 /* Record multiple-linked object. */
4068 if (ldata.ld_leh->leh_reccount > 1) {
4069 rc = lfsck_namespace_trace_update(env, com, fid,
4070 LNTF_CHECK_LINKEA, true);
4075 linkea_first_entry(&ldata);
4076 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4077 if (!fid_is_sane(pfid)) {
4078 rc = lfsck_namespace_trace_update(env, com, fid,
4079 LNTF_CHECK_PARENT, true);
4081 fld_range_set_mdt(range);
4082 rc = fld_local_lookup(env, ss->ss_server_fld,
4083 fid_seq(pfid), range);
4084 if ((rc == -ENOENT) ||
4085 (rc == 0 && range->lsr_index != idx))
4086 rc = lfsck_namespace_trace_update(env, com, fid,
4087 LNTF_CHECK_LINKEA, true);
4089 rc = lfsck_namespace_check_for_double_scan(env, com,
4096 down_write(&com->lc_sem);
4097 com->lc_new_checked++;
4098 if (S_ISDIR(lfsck_object_type(obj)))
4099 ns->ln_dirs_checked++;
4101 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4102 up_write(&com->lc_sem);
4107 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4108 struct lfsck_component *com,
4109 struct lu_dirent *ent, __u16 type)
4111 struct lfsck_assistant_data *lad = com->lc_data;
4112 struct lfsck_instance *lfsck = com->lc_lfsck;
4113 struct lfsck_namespace_req *lnr;
4114 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4115 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4116 struct ptlrpc_thread *athread = &lad->lad_thread;
4117 struct l_wait_info lwi = { 0 };
4118 bool wakeup = false;
4120 l_wait_event(mthread->t_ctl_waitq,
4121 bk->lb_async_windows == 0 ||
4122 lad->lad_prefetched < bk->lb_async_windows ||
4123 !thread_is_running(mthread) ||
4124 thread_is_stopped(athread),
4127 if (unlikely(!thread_is_running(mthread)) ||
4128 thread_is_stopped(athread))
4131 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4134 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4136 struct lfsck_namespace *ns = com->lc_file_ram;
4138 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4139 return PTR_ERR(lnr);
4142 spin_lock(&lad->lad_lock);
4143 if (lad->lad_assistant_status < 0) {
4144 spin_unlock(&lad->lad_lock);
4145 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4146 return lad->lad_assistant_status;
4149 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4150 if (lad->lad_prefetched == 0)
4153 lad->lad_prefetched++;
4154 spin_unlock(&lad->lad_lock);
4156 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4158 down_write(&com->lc_sem);
4159 com->lc_new_checked++;
4160 up_write(&com->lc_sem);
4165 static int lfsck_namespace_post(const struct lu_env *env,
4166 struct lfsck_component *com,
4167 int result, bool init)
4169 struct lfsck_instance *lfsck = com->lc_lfsck;
4170 struct lfsck_namespace *ns = com->lc_file_ram;
4174 lfsck_post_generic(env, com, &result);
4176 down_write(&com->lc_sem);
4177 lfsck_namespace_release_lmv(env, com);
4179 spin_lock(&lfsck->li_lock);
4181 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4183 ns->ln_status = LS_SCANNING_PHASE2;
4184 ns->ln_flags |= LF_SCANNED_ONCE;
4185 ns->ln_flags &= ~LF_UPGRADE;
4186 list_del_init(&com->lc_link_dir);
4187 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4188 } else if (result == 0) {
4189 if (lfsck->li_status != 0)
4190 ns->ln_status = lfsck->li_status;
4192 ns->ln_status = LS_STOPPED;
4193 if (ns->ln_status != LS_PAUSED) {
4194 list_del_init(&com->lc_link_dir);
4195 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4198 ns->ln_status = LS_FAILED;
4199 list_del_init(&com->lc_link_dir);
4200 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4202 spin_unlock(&lfsck->li_lock);
4205 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4206 HALF_SEC - lfsck->li_time_last_checkpoint);
4207 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4208 ns->ln_items_checked += com->lc_new_checked;
4209 com->lc_new_checked = 0;
4212 rc = lfsck_namespace_store(env, com, false);
4213 up_write(&com->lc_sem);
4215 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4216 lfsck_lfsck2name(lfsck), rc);
4222 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4225 struct lfsck_instance *lfsck = com->lc_lfsck;
4226 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4227 struct lfsck_namespace *ns = com->lc_file_ram;
4230 down_read(&com->lc_sem);
4231 seq_printf(m, "name: lfsck_namespace\n"
4237 lfsck_status2names(ns->ln_status));
4239 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4243 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4247 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4248 "time_since_last_completed");
4252 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4253 "time_since_latest_start");
4257 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4258 "time_since_last_checkpoint");
4262 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4263 "latest_start_position");
4267 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4268 "last_checkpoint_position");
4272 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4273 "first_failure_position");
4277 if (ns->ln_status == LS_SCANNING_PHASE1) {
4278 struct lfsck_position pos;
4279 const struct dt_it_ops *iops;
4280 cfs_duration_t duration = cfs_time_current() -
4281 lfsck->li_time_last_checkpoint;
4282 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4283 __u64 speed = checked;
4284 __u64 new_checked = com->lc_new_checked *
4285 msecs_to_jiffies(MSEC_PER_SEC);
4286 __u32 rtime = ns->ln_run_time_phase1 +
4287 cfs_duration_sec(duration + HALF_SEC);
4290 do_div(new_checked, duration);
4292 do_div(speed, rtime);
4293 lfsck_namespace_dump_statistics(m, ns, checked,
4294 ns->ln_objs_checked_phase2,
4295 rtime, ns->ln_run_time_phase2);
4297 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4298 "average_speed_phase2: N/A\n"
4299 "real_time_speed_phase1: "LPU64" items/sec\n"
4300 "real_time_speed_phase2: N/A\n",
4304 LASSERT(lfsck->li_di_oit != NULL);
4306 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4308 /* The low layer otable-based iteration position may NOT
4309 * exactly match the namespace-based directory traversal
4310 * cookie. Generally, it is not a serious issue. But the
4311 * caller should NOT make assumption on that. */
4312 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4313 if (!lfsck->li_current_oit_processed)
4314 pos.lp_oit_cookie--;
4316 spin_lock(&lfsck->li_lock);
4317 if (lfsck->li_di_dir != NULL) {
4318 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4319 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4320 fid_zero(&pos.lp_dir_parent);
4321 pos.lp_dir_cookie = 0;
4324 *lfsck_dto2fid(lfsck->li_obj_dir);
4327 fid_zero(&pos.lp_dir_parent);
4328 pos.lp_dir_cookie = 0;
4330 spin_unlock(&lfsck->li_lock);
4331 lfsck_pos_dump(m, &pos, "current_position");
4332 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4333 cfs_duration_t duration = cfs_time_current() -
4334 lfsck->li_time_last_checkpoint;
4335 __u64 checked = ns->ln_objs_checked_phase2 +
4336 com->lc_new_checked;
4337 __u64 speed1 = ns->ln_items_checked;
4338 __u64 speed2 = checked;
4339 __u64 new_checked = com->lc_new_checked *
4340 msecs_to_jiffies(MSEC_PER_SEC);
4341 __u32 rtime = ns->ln_run_time_phase2 +
4342 cfs_duration_sec(duration + HALF_SEC);
4345 do_div(new_checked, duration);
4346 if (ns->ln_run_time_phase1 != 0)
4347 do_div(speed1, ns->ln_run_time_phase1);
4349 do_div(speed2, rtime);
4350 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4352 ns->ln_run_time_phase1, rtime);
4354 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4355 "average_speed_phase2: "LPU64" objs/sec\n"
4356 "real_time_speed_phase1: N/A\n"
4357 "real_time_speed_phase2: "LPU64" objs/sec\n"
4358 "current_position: "DFID"\n",
4362 PFID(&ns->ln_fid_latest_scanned_phase2));
4364 __u64 speed1 = ns->ln_items_checked;
4365 __u64 speed2 = ns->ln_objs_checked_phase2;
4367 if (ns->ln_run_time_phase1 != 0)
4368 do_div(speed1, ns->ln_run_time_phase1);
4369 if (ns->ln_run_time_phase2 != 0)
4370 do_div(speed2, ns->ln_run_time_phase2);
4371 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4372 ns->ln_objs_checked_phase2,
4373 ns->ln_run_time_phase1,
4374 ns->ln_run_time_phase2);
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: N/A\n"
4380 "current_position: N/A\n",
4385 up_read(&com->lc_sem);
4389 static int lfsck_namespace_double_scan(const struct lu_env *env,
4390 struct lfsck_component *com)
4392 struct lfsck_namespace *ns = com->lc_file_ram;
4393 struct lfsck_assistant_data *lad = com->lc_data;
4394 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4395 struct lfsck_tgt_desc *ltd;
4396 struct lfsck_tgt_desc *next;
4399 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4400 if (thread_is_stopped(&lad->lad_thread)) {
4401 LASSERT(list_empty(&lad->lad_req_list));
4402 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4404 spin_lock(<ds->ltd_lock);
4405 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4406 ltd_namespace_phase_list) {
4407 list_del_init(<d->ltd_namespace_phase_list);
4409 spin_unlock(<ds->ltd_lock);
4415 static void lfsck_namespace_data_release(const struct lu_env *env,
4416 struct lfsck_component *com)
4418 struct lfsck_assistant_data *lad = com->lc_data;
4419 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4420 struct lfsck_tgt_desc *ltd;
4421 struct lfsck_tgt_desc *next;
4423 LASSERT(lad != NULL);
4424 LASSERT(thread_is_init(&lad->lad_thread) ||
4425 thread_is_stopped(&lad->lad_thread));
4426 LASSERT(list_empty(&lad->lad_req_list));
4428 com->lc_data = NULL;
4429 lfsck_namespace_release_lmv(env, com);
4431 spin_lock(<ds->ltd_lock);
4432 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4433 ltd_namespace_phase_list) {
4434 list_del_init(<d->ltd_namespace_phase_list);
4436 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4437 ltd_namespace_phase_list) {
4438 list_del_init(<d->ltd_namespace_phase_list);
4440 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4441 ltd_namespace_list) {
4442 list_del_init(<d->ltd_namespace_list);
4444 spin_unlock(<ds->ltd_lock);
4446 if (likely(lad->lad_bitmap != NULL))
4447 CFS_FREE_BITMAP(lad->lad_bitmap);
4452 static void lfsck_namespace_quit(const struct lu_env *env,
4453 struct lfsck_component *com)
4455 struct lfsck_assistant_data *lad = com->lc_data;
4456 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4457 struct lfsck_tgt_desc *ltd;
4458 struct lfsck_tgt_desc *next;
4460 LASSERT(lad != NULL);
4462 lfsck_quit_generic(env, com);
4464 LASSERT(thread_is_init(&lad->lad_thread) ||
4465 thread_is_stopped(&lad->lad_thread));
4466 LASSERT(list_empty(&lad->lad_req_list));
4468 lfsck_namespace_release_lmv(env, com);
4470 spin_lock(<ds->ltd_lock);
4471 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4472 ltd_namespace_phase_list) {
4473 list_del_init(<d->ltd_namespace_phase_list);
4475 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4476 ltd_namespace_phase_list) {
4477 list_del_init(<d->ltd_namespace_phase_list);
4479 spin_unlock(<ds->ltd_lock);
4482 static int lfsck_namespace_in_notify(const struct lu_env *env,
4483 struct lfsck_component *com,
4484 struct lfsck_request *lr,
4487 struct lfsck_instance *lfsck = com->lc_lfsck;
4488 struct lfsck_namespace *ns = com->lc_file_ram;
4489 struct lfsck_assistant_data *lad = com->lc_data;
4490 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4491 struct lfsck_tgt_desc *ltd;
4496 switch (lr->lr_event) {
4497 case LE_SKIP_NLINK_DECLARE: {
4498 struct dt_object *obj;
4499 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4503 LASSERT(th != NULL);
4505 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4506 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4507 fid_cpu_to_be(key, &lr->lr_fid);
4508 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4509 rc = dt_declare_delete(env, obj,
4510 (const struct dt_key *)key, th);
4512 rc = dt_declare_insert(env, obj,
4513 (const struct dt_rec *)&flags,
4514 (const struct dt_key *)key, th);
4515 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4519 case LE_SKIP_NLINK: {
4520 struct dt_object *obj;
4521 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4527 LASSERT(th != NULL);
4529 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4530 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4531 fid_cpu_to_be(key, &lr->lr_fid);
4532 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4533 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4534 (const struct dt_key *)key, BYPASS_CAPA);
4536 if (flags & LNTF_SKIP_NLINK) {
4538 &com->lc_sub_trace_objs[idx].lsto_mutex);
4544 } else if (rc != -ENOENT) {
4548 flags |= LNTF_SKIP_NLINK;
4550 rc = dt_delete(env, obj, (const struct dt_key *)key,
4556 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4557 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4562 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4563 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4564 " to be skipped for namespace double scan: rc = %d\n",
4565 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4568 /* If we cannot record this object in the LFSCK tracing,
4569 * we have to mark the LFSC as LF_INCOMPLETE, then the
4570 * LFSCK will skip nlink attribute verification for
4572 ns->ln_flags |= LF_INCOMPLETE;
4576 case LE_SET_LMV_MASTER: {
4577 struct dt_object *obj;
4579 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4581 RETURN(PTR_ERR(obj));
4583 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4584 lfsck_object_put(env, obj);
4586 RETURN(rc > 0 ? 0 : rc);
4588 case LE_SET_LMV_SLAVE: {
4589 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4590 ns->ln_striped_shards_repaired++;
4592 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4593 LNTF_RECHECK_NAME_HASH, true);
4595 RETURN(rc > 0 ? 0 : rc);
4597 case LE_PHASE1_DONE:
4598 case LE_PHASE2_DONE:
4605 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4606 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4607 lr->lr_index, lr->lr_status, lr->lr_flags2);
4609 spin_lock(<ds->ltd_lock);
4610 ltd = LTD_TGT(ltds, lr->lr_index);
4612 spin_unlock(<ds->ltd_lock);
4617 list_del_init(<d->ltd_namespace_phase_list);
4618 switch (lr->lr_event) {
4619 case LE_PHASE1_DONE:
4620 if (lr->lr_status <= 0) {
4621 ltd->ltd_namespace_done = 1;
4622 list_del_init(<d->ltd_namespace_list);
4623 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4624 "phase1 for namespace LFSCK: rc = %d.\n",
4625 lfsck_lfsck2name(lfsck),
4626 ltd->ltd_index, lr->lr_status);
4627 ns->ln_flags |= LF_INCOMPLETE;
4632 if (lr->lr_flags2 & LF_INCOMPLETE)
4633 ns->ln_flags |= LF_INCOMPLETE;
4635 if (list_empty(<d->ltd_namespace_list))
4636 list_add_tail(<d->ltd_namespace_list,
4637 &lad->lad_mdt_list);
4638 list_add_tail(<d->ltd_namespace_phase_list,
4639 &lad->lad_mdt_phase2_list);
4641 case LE_PHASE2_DONE:
4642 ltd->ltd_namespace_done = 1;
4643 list_del_init(<d->ltd_namespace_list);
4647 ltd->ltd_namespace_done = 1;
4648 list_del_init(<d->ltd_namespace_list);
4649 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4651 "%s: the peer MDT %x exit namespace LFSCK\n",
4652 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4653 ns->ln_flags |= LF_INCOMPLETE;
4659 spin_unlock(<ds->ltd_lock);
4661 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4662 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4664 memset(stop, 0, sizeof(*stop));
4665 stop->ls_status = lr->lr_status;
4666 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4667 lfsck_stop(env, lfsck->li_bottom, stop);
4668 } else if (lfsck_phase2_next_ready(lad)) {
4669 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4675 static int lfsck_namespace_query(const struct lu_env *env,
4676 struct lfsck_component *com)
4678 struct lfsck_namespace *ns = com->lc_file_ram;
4680 return ns->ln_status;
4683 static struct lfsck_operations lfsck_namespace_ops = {
4684 .lfsck_reset = lfsck_namespace_reset,
4685 .lfsck_fail = lfsck_namespace_fail,
4686 .lfsck_close_dir = lfsck_namespace_close_dir,
4687 .lfsck_open_dir = lfsck_namespace_open_dir,
4688 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4689 .lfsck_prep = lfsck_namespace_prep,
4690 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4691 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4692 .lfsck_post = lfsck_namespace_post,
4693 .lfsck_dump = lfsck_namespace_dump,
4694 .lfsck_double_scan = lfsck_namespace_double_scan,
4695 .lfsck_data_release = lfsck_namespace_data_release,
4696 .lfsck_quit = lfsck_namespace_quit,
4697 .lfsck_in_notify = lfsck_namespace_in_notify,
4698 .lfsck_query = lfsck_namespace_query,
4702 * Repair dangling name entry.
4704 * For the name entry with dangling reference, we need to repare the
4705 * inconsistency according to the LFSCK sponsor's requirement:
4707 * 1) Keep the inconsistency there and report the inconsistency case,
4708 * then give the chance to the application to find related issues,
4709 * and the users can make the decision about how to handle it with
4710 * more human knownledge. (by default)
4712 * 2) Re-create the missing MDT-object with the FID information.
4714 * \param[in] env pointer to the thread context
4715 * \param[in] com pointer to the lfsck component
4716 * \param[in] parent pointer to the dir object that contains the dangling
4718 * \param[in] child pointer to the object corresponding to the dangling
4720 * \param[in] lnr pointer to the namespace request that contains the
4721 * name's name, parent object, parent's LMV, and ect.
4723 * \retval positive number if no need to repair
4724 * \retval zero for repaired successfully
4725 * \retval negative error number on failure
4727 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4728 struct lfsck_component *com,
4729 struct dt_object *parent,
4730 struct dt_object *child,
4731 struct lfsck_namespace_req *lnr)
4733 struct lfsck_thread_info *info = lfsck_env_info(env);
4734 struct lu_attr *la = &info->lti_la;
4735 struct dt_allocation_hint *hint = &info->lti_hint;
4736 struct dt_object_format *dof = &info->lti_dof;
4737 struct dt_insert_rec *rec = &info->lti_dt_rec;
4738 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4739 struct dt_object *pobj = NULL;
4740 struct dt_object *cobj = NULL;
4741 const struct lu_name *cname;
4742 struct linkea_data ldata = { NULL };
4743 struct lustre_handle lh = { 0 };
4744 struct lu_buf linkea_buf;
4745 struct lu_buf lmv_buf;
4746 struct lfsck_instance *lfsck = com->lc_lfsck;
4747 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4748 struct dt_device *dev = lfsck->li_next;
4749 struct thandle *th = NULL;
4751 __u16 type = lnr->lnr_type;
4755 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4756 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4761 if (!create || bk->lb_param & LPF_DRYRUN)
4764 /* We may need to create the sub-objects of the @child via LOD,
4765 * so make the modification based on lfsck->li_next. */
4767 pobj = lfsck_object_locate(dev, parent);
4769 GOTO(log, rc = PTR_ERR(pobj));
4771 if (unlikely(!dt_try_as_dir(env, pobj)))
4772 GOTO(log, rc = -ENOTDIR);
4774 cobj = lfsck_object_locate(dev, child);
4776 GOTO(log, rc = PTR_ERR(cobj));
4778 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4782 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(pobj));
4786 rc = lfsck_ibits_lock(env, lfsck, pobj, &lh,
4787 MDS_INODELOCK_UPDATE, LCK_EX);
4791 rc = lfsck_namespace_check_exist(env, pobj, cobj, lnr->lnr_name);
4795 /* Set the ctime as zero, then others can know it is created for
4796 * repairing dangling name entry by LFSCK. And if the LFSCK made
4797 * wrong decision and the real MDT-object has been found later,
4798 * then the LFSCK has chance to fix the incosistency properly. */
4799 memset(la, 0, sizeof(*la));
4800 la->la_mode = (type & S_IFMT) | 0600;
4801 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4802 LA_ATIME | LA_MTIME | LA_CTIME;
4804 cobj->do_ops->do_ah_init(env, hint, pobj, cobj,
4805 la->la_mode & S_IFMT);
4807 memset(dof, 0, sizeof(*dof));
4808 dof->dof_type = dt_mode_to_dft(type);
4809 /* If the target is a regular file, then the LFSCK will only create
4810 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4811 * OST-objects will be created when write open. */
4813 th = dt_trans_create(env, dev);
4815 GOTO(log, rc = PTR_ERR(th));
4817 /* 1a. create child. */
4818 rc = dt_declare_create(env, cobj, la, hint, dof, th);
4822 if (S_ISDIR(type)) {
4823 if (unlikely(!dt_try_as_dir(env, cobj)))
4824 GOTO(stop, rc = -ENOTDIR);
4826 /* 2a. insert dot into child dir */
4827 rec->rec_type = S_IFDIR;
4828 rec->rec_fid = lfsck_dto2fid(cobj);
4829 rc = dt_declare_insert(env, cobj,
4830 (const struct dt_rec *)rec,
4831 (const struct dt_key *)dot, th);
4835 /* 3a. insert dotdot into child dir */
4836 rec->rec_fid = lfsck_dto2fid(pobj);
4837 rc = dt_declare_insert(env, cobj,
4838 (const struct dt_rec *)rec,
4839 (const struct dt_key *)dotdot, th);
4843 /* 4a. increase child nlink */
4844 rc = dt_declare_ref_add(env, cobj, th);
4848 /* 5a. generate slave LMV EA. */
4849 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4852 idx = lfsck_shard_name_to_index(env,
4853 lnr->lnr_name, lnr->lnr_namelen,
4854 type, lfsck_dto2fid(cobj));
4855 if (unlikely(idx < 0))
4856 GOTO(stop, rc = idx);
4858 *lmv2 = lnr->lnr_lmv->ll_lmv;
4859 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4860 lmv2->lmv_master_mdt_index = idx;
4862 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4863 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4864 rc = dt_declare_xattr_set(env, cobj, &lmv_buf,
4865 XATTR_NAME_LMV, 0, th);
4871 /* 6a. insert linkEA for child */
4872 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4873 ldata.ld_leh->leh_len);
4874 rc = dt_declare_xattr_set(env, cobj, &linkea_buf,
4875 XATTR_NAME_LINK, 0, th);
4879 rc = dt_trans_start_local(env, dev, th);
4881 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4883 dt_write_lock(env, cobj, 0);
4884 /* 1b. create child */
4885 rc = dt_create(env, cobj, la, hint, dof, th);
4887 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4889 if (S_ISDIR(type)) {
4890 /* 2b. insert dot into child dir */
4891 rec->rec_type = S_IFDIR;
4892 rec->rec_fid = lfsck_dto2fid(cobj);
4893 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4894 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4898 /* 3b. insert dotdot into child dir */
4899 rec->rec_fid = lfsck_dto2fid(pobj);
4900 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4901 (const struct dt_key *)dotdot, th,
4906 /* 4b. increase child nlink */
4907 rc = dt_ref_add(env, cobj, th);
4911 /* 5b. generate slave LMV EA. */
4912 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4913 rc = dt_xattr_set(env, cobj, &lmv_buf, XATTR_NAME_LMV,
4914 0, th, BYPASS_CAPA);
4920 /* 6b. insert linkEA for child. */
4921 rc = dt_xattr_set(env, cobj, &linkea_buf,
4922 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4927 dt_write_unlock(env, cobj);
4930 dt_trans_stop(env, dev, th);
4933 lfsck_ibits_unlock(&lh, LCK_EX);
4934 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4935 "reference for: parent "DFID", child "DFID", type %u, "
4936 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4937 PFID(&lnr->lnr_lar.lar_fid), PFID(lfsck_dto2fid(child)),
4938 type, cname->ln_name,
4939 create ? "Create the lost OST-object as required" :
4940 "Keep the MDT-object there by default", rc);
4943 struct lfsck_namespace *ns = com->lc_file_ram;
4945 ns->ln_flags |= LF_INCONSISTENT;
4951 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4952 struct lfsck_component *com,
4953 struct lfsck_assistant_req *lar)
4955 struct lfsck_thread_info *info = lfsck_env_info(env);
4956 struct lu_attr *la = &info->lti_la;
4957 struct lfsck_instance *lfsck = com->lc_lfsck;
4958 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4959 struct lfsck_namespace *ns = com->lc_file_ram;
4960 struct linkea_data ldata = { NULL };
4961 const struct lu_name *cname;
4962 struct thandle *handle = NULL;
4963 struct lfsck_namespace_req *lnr =
4964 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
4965 struct dt_object *dir = NULL;
4966 struct dt_object *obj = NULL;
4967 const struct lu_fid *pfid;
4968 struct dt_device *dev = NULL;
4969 struct lustre_handle lh = { 0 };
4970 bool repaired = false;
4971 bool dtlocked = false;
4975 bool bad_hash = false;
4979 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
4982 dir = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
4984 RETURN(PTR_ERR(dir));
4986 if (unlikely(lfsck_is_dead_obj(dir)))
4987 GOTO(put_dir, rc = 0);
4989 if (unlikely(!dt_try_as_dir(env, dir)))
4990 GOTO(put_dir, rc = -ENOTDIR);
4992 pfid = lfsck_dto2fid(dir);
4994 if (lnr->lnr_attr & LUDA_UPGRADE) {
4995 ns->ln_flags |= LF_UPGRADE;
4996 ns->ln_dirent_repaired++;
4998 } else if (lnr->lnr_attr & LUDA_REPAIR) {
4999 ns->ln_flags |= LF_INCONSISTENT;
5000 ns->ln_dirent_repaired++;
5004 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5005 if (strcmp(lnr->lnr_name, dotdot) != 0)
5008 rc = lfsck_namespace_trace_update(env, com, pfid,
5009 LNTF_CHECK_PARENT, true);
5014 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5015 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5016 " for the name entry %.*s under "DFID"\n",
5017 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5018 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5020 if (strcmp(lnr->lnr_name, dotdot) != 0)
5021 /* invalid FID means bad name entry, remove it. */
5022 type = LNIT_BAD_DIRENT;
5024 /* If the parent FID is invalid, we cannot remove
5025 * the ".." entry directly. */
5026 rc = lfsck_namespace_trace_update(env, com, pfid,
5027 LNTF_CHECK_PARENT, true);
5032 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5033 rc = lfsck_namespace_striped_dir_rescan(env, com, dir, lnr);
5038 if (lnr->lnr_name[0] == '.' &&
5039 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
5042 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5043 rc = lfsck_namespace_handle_striped_master(env, com, dir, lnr);
5048 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5050 GOTO(out, rc = idx);
5052 if (idx == lfsck_dev_idx(lfsck)) {
5053 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5056 dev = lfsck->li_bottom;
5058 struct lfsck_tgt_desc *ltd;
5060 /* Usually, some local filesystem consistency verification
5061 * tools can guarantee the local namespace tree consistenct.
5062 * So the LFSCK will only verify the remote directory. */
5063 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5064 rc = lfsck_namespace_trace_update(env, com, pfid,
5065 LNTF_CHECK_PARENT, true);
5070 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5071 if (unlikely(ltd == NULL)) {
5072 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5073 "did not join the namespace LFSCK\n",
5074 lfsck_lfsck2name(lfsck), idx);
5075 lfsck_lad_set_bitmap(env, com, idx);
5077 GOTO(out, rc = -ENODEV);
5083 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5085 GOTO(out, rc = PTR_ERR(obj));
5087 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5088 if (dt_object_exists(obj) == 0) {
5091 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5093 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5094 lnr->lnr_name, lnr->lnr_namelen)) {
5095 type = LNIT_BAD_DIRENT;
5100 type = LNIT_DANGLING;
5101 rc = lfsck_namespace_repair_dangling(env, com, dir,
5110 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5113 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5114 MDS_INODELOCK_UPDATE |
5115 MDS_INODELOCK_XATTR, LCK_EX);
5119 handle = dt_trans_create(env, dev);
5121 GOTO(out, rc = PTR_ERR(handle));
5123 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5127 rc = dt_trans_start_local(env, dev, handle);
5131 dt_write_lock(env, obj, 0);
5135 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5139 rc = lfsck_links_read(env, obj, &ldata);
5140 if (unlikely(rc == -ENOENT)) {
5141 if (handle != NULL) {
5142 dt_write_unlock(env, obj);
5145 dt_trans_stop(env, dev, handle);
5148 lfsck_ibits_unlock(&lh, LCK_EX);
5151 /* It may happen when the remote object has been removed,
5152 * but the local MDT is not aware of that. */
5154 } else if (rc == 0) {
5155 count = ldata.ld_leh->leh_reccount;
5156 rc = linkea_links_find(&ldata, cname, pfid);
5158 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5159 if ((lfsck_object_type(obj) & S_IFMT) !=
5161 ns->ln_flags |= LF_INCONSISTENT;
5162 type = LNIT_BAD_TYPE;
5168 ns->ln_flags |= LF_INCONSISTENT;
5170 /* If the name entry hash does not match the slave striped
5171 * directory, and the name entry does not match also, then
5172 * it is quite possible that name entry is corrupted. */
5173 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5174 lnr->lnr_name, lnr->lnr_namelen)) {
5175 type = LNIT_BAD_DIRENT;
5180 /* If the file type stored in the name entry does not match
5181 * the file type claimed by the object, and the object does
5182 * not recognize the name entry, then it is quite possible
5183 * that the name entry is corrupted. */
5184 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5185 type = LNIT_BAD_DIRENT;
5190 /* For sub-dir object, we cannot make sure whether the sub-dir
5191 * back references the parent via ".." name entry correctly or
5192 * not in the LFSCK first-stage scanning. It may be that the
5193 * (remote) sub-dir ".." name entry has no parent FID after
5194 * file-level backup/restore and its linkEA may be wrong.
5195 * So under such case, we should replace the linkEA according
5196 * to current name entry. But this needs to be done during the
5197 * LFSCK second-stage scanning. The LFSCK will record the name
5198 * entry for further possible using. */
5202 } else if (unlikely(rc == -EINVAL)) {
5203 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5204 type = LNIT_BAD_TYPE;
5207 ns->ln_flags |= LF_INCONSISTENT;
5208 /* The magic crashed, we are not sure whether there are more
5209 * corrupt data in the linkea, so remove all linkea entries. */
5213 } else if (rc == -ENODATA) {
5214 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5215 type = LNIT_BAD_TYPE;
5218 ns->ln_flags |= LF_UPGRADE;
5223 if (bk->lb_param & LPF_DRYRUN) {
5224 ns->ln_linkea_repaired++;
5230 if (!lustre_handle_is_used(&lh))
5236 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5243 rc = linkea_data_new(&ldata,
5244 &lfsck_env_info(env)->lti_linkea_buf);
5249 rc = linkea_add_buf(&ldata, cname, pfid);
5253 rc = lfsck_links_write(env, obj, &ldata, handle);
5254 if (unlikely(rc == -ENOSPC) &&
5255 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5256 if (handle != NULL) {
5257 LASSERT(dt_write_locked(env, obj));
5259 dt_write_unlock(env, obj);
5262 dt_trans_stop(env, dev, handle);
5265 lfsck_ibits_unlock(&lh, LCK_EX);
5268 rc = lfsck_namespace_trace_update(env, com,
5269 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5271 /* If we cannot record this object in the
5272 * LFSCK tracing, we have to mark the LFSCK
5273 * as LF_INCOMPLETE, then the LFSCK will
5274 * skip nlink attribute verification for
5276 ns->ln_flags |= LF_INCOMPLETE;
5284 count = ldata.ld_leh->leh_reccount;
5285 if (!S_ISDIR(lfsck_object_type(obj)) ||
5286 !dt_object_remote(obj)) {
5287 ns->ln_linkea_repaired++;
5297 dt_write_unlock(env, obj);
5299 if (handle != NULL && !IS_ERR(handle))
5300 dt_trans_stop(env, dev, handle);
5303 lfsck_ibits_unlock(&lh, LCK_EX);
5305 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5306 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5307 lnr->lnr_name, lnr->lnr_namelen) &&
5308 type != LNIT_BAD_DIRENT) {
5309 ns->ln_flags |= LF_INCONSISTENT;
5312 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5313 lnr->lnr_lmv, lnr->lnr_name);
5322 rc = lfsck_namespace_repair_dirent(env, com, dir,
5323 obj, lnr->lnr_name, lnr->lnr_name,
5324 lnr->lnr_type, true, false);
5328 case LNIT_BAD_DIRENT:
5330 /* XXX: This is a bad dirent, we do not know whether
5331 * the original name entry reference a regular
5332 * file or a directory, then keep the parent's
5333 * nlink count unchanged here. */
5334 rc = lfsck_namespace_repair_dirent(env, com, dir,
5335 obj, lnr->lnr_name, lnr->lnr_name,
5336 lnr->lnr_type, false, false);
5344 if (count == 1 && S_ISREG(lfsck_object_type(obj)))
5345 dt_attr_get(env, obj, la, BYPASS_CAPA);
5348 down_write(&com->lc_sem);
5350 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5351 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5352 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5353 PFID(lfsck_dto2fid(dir)),
5354 lnr->lnr_namelen, lnr->lnr_name, rc);
5356 lfsck_namespace_record_failure(env, lfsck, ns);
5357 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5358 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5359 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5360 dev != NULL && dev != lfsck->li_bottom)
5361 lfsck_lad_set_bitmap(env, com, idx);
5363 if (!(bk->lb_param & LPF_FAILOUT))
5367 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5368 "repaired the entry: "DFID", parent "DFID
5369 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5370 PFID(&lnr->lnr_fid),
5371 PFID(lfsck_dto2fid(dir)),
5372 lnr->lnr_namelen, lnr->lnr_name);
5375 ns->ln_items_repaired++;
5379 ns->ln_dangling_repaired++;
5382 ns->ln_bad_type_repaired++;
5384 case LNIT_BAD_DIRENT:
5385 ns->ln_dirent_repaired++;
5391 if (bk->lb_param & LPF_DRYRUN &&
5392 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5393 lfsck_pos_fill(env, lfsck,
5394 &ns->ln_pos_first_inconsistent,
5399 ns->ln_name_hash_repaired++;
5401 /* Not count repeatedly. */
5403 ns->ln_items_repaired++;
5405 if (bk->lb_param & LPF_DRYRUN &&
5406 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5407 lfsck_pos_fill(env, lfsck,
5408 &ns->ln_pos_first_inconsistent,
5415 if (count > 1 || la->la_nlink > 1)
5416 ns->ln_mul_linked_checked++;
5418 up_write(&com->lc_sem);
5420 if (obj != NULL && !IS_ERR(obj))
5421 lfsck_object_put(env, obj);
5424 lfsck_object_put(env, dir);
5430 * Handle one orphan under the backend /lost+found directory
5432 * Insert the orphan FID into the namespace LFSCK trace file for further
5433 * processing (via the subsequent namespace LFSCK second-stage scanning).
5434 * At the same time, remove the orphan name entry from backend /lost+found
5435 * directory. There is an interval between the orphan name entry removed
5436 * from the backend /lost+found directory and the orphan FID in the LFSCK
5437 * trace file handled. In such interval, the LFSCK can be reset, then
5438 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5439 * To guarantee that the orphans can be found when LFSCK run next time
5440 * without e2fsck again, when remove the orphan name entry, the LFSCK
5441 * will set the orphan's ctime attribute as 1. Since normal applications
5442 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5443 * next time, it can record the object (that ctime is 1) in the namespace
5444 * LFSCK trace file during the first-stage scanning.
5446 * \param[in] env pointer to the thread context
5447 * \param[in] com pointer to the lfsck component
5448 * \param[in] parent pointer to the object for the backend /lost+found
5449 * \param[in] ent pointer to the name entry for the target under the
5450 * backend /lost+found
5452 * \retval positive for repaired
5453 * \retval 0 if needs to repair nothing
5454 * \retval negative error number on failure
5456 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5457 struct lfsck_component *com,
5458 struct dt_object *parent,
5459 struct lu_dirent *ent)
5461 struct lfsck_thread_info *info = lfsck_env_info(env);
5462 struct lu_fid *key = &info->lti_fid;
5463 struct lu_attr *la = &info->lti_la;
5464 struct lfsck_instance *lfsck = com->lc_lfsck;
5465 struct dt_object *obj;
5466 struct dt_device *dev = lfsck->li_bottom;
5467 struct dt_object *child = NULL;
5468 struct thandle *th = NULL;
5475 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5477 RETURN(PTR_ERR(child));
5479 LASSERT(dt_object_exists(child));
5480 LASSERT(!dt_object_remote(child));
5482 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5483 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5484 fid_cpu_to_be(key, &ent->lde_fid);
5485 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5486 (const struct dt_key *)key, BYPASS_CAPA);
5489 flags |= LNTF_CHECK_ORPHAN;
5490 } else if (rc == -ENOENT) {
5491 flags = LNTF_CHECK_ORPHAN;
5496 th = dt_trans_create(env, dev);
5498 GOTO(out, rc = PTR_ERR(th));
5500 /* a1. remove name entry from backend /lost+found */
5501 rc = dt_declare_delete(env, parent,
5502 (const struct dt_key *)ent->lde_name, th);
5506 if (S_ISDIR(lfsck_object_type(child))) {
5507 /* a2. decrease parent's nlink */
5508 rc = dt_declare_ref_del(env, parent, th);
5514 /* a3. remove child's FID from the LFSCK trace file. */
5515 rc = dt_declare_delete(env, obj,
5516 (const struct dt_key *)key, th);
5520 /* a4. set child's ctime as 1 */
5521 memset(la, 0, sizeof(*la));
5523 la->la_valid = LA_CTIME;
5524 rc = dt_declare_attr_set(env, child, la, th);
5529 /* a5. insert child's FID into the LFSCK trace file. */
5530 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5531 (const struct dt_key *)key, th);
5535 rc = dt_trans_start_local(env, dev, th);
5539 /* b1. remove name entry from backend /lost+found */
5540 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5545 if (S_ISDIR(lfsck_object_type(child))) {
5546 /* b2. decrease parent's nlink */
5547 dt_write_lock(env, parent, 0);
5548 rc = dt_ref_del(env, parent, th);
5549 dt_write_unlock(env, parent);
5555 /* a3. remove child's FID from the LFSCK trace file. */
5556 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5561 /* b4. set child's ctime as 1 */
5562 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5567 /* b5. insert child's FID into the LFSCK trace file. */
5568 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5569 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5571 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5574 dt_trans_stop(env, dev, th);
5577 lfsck_object_put(env, child);
5583 * Handle orphans under the backend /lost+found directory
5585 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5586 * and put them under the backend /lost+found directory that is invisible
5587 * to client. The LFSCK will scan such directory, for the original client
5588 * visible orphans, add their fids into the namespace LFSCK trace file,
5589 * then the subsenquent namespace LFSCK second-stage scanning can handle
5590 * them as other objects to be double scanned: either move back to normal
5591 * namespace, or to the global visible orphan directory:
5592 * /ROOT/.lustre/lost+found/MDTxxxx/
5594 * \param[in] env pointer to the thread context
5595 * \param[in] com pointer to the lfsck component
5597 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5598 struct lfsck_component *com)
5600 struct lfsck_thread_info *info = lfsck_env_info(env);
5601 struct lu_dirent *ent =
5602 (struct lu_dirent *)info->lti_key;
5603 struct lu_seq_range *range = &info->lti_range;
5604 struct lfsck_instance *lfsck = com->lc_lfsck;
5605 struct ptlrpc_thread *thread = &lfsck->li_thread;
5606 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5607 struct lfsck_namespace *ns = com->lc_file_ram;
5608 struct dt_object *parent;
5609 const struct dt_it_ops *iops;
5611 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5613 __u32 idx = lfsck_dev_idx(lfsck);
5618 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5619 &LU_BACKEND_LPF_FID);
5620 if (IS_ERR(parent)) {
5621 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5622 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5626 /* It is normal that the /lost+found does not exist for ZFS backend. */
5627 if (!dt_object_exists(parent))
5630 if (unlikely(!dt_try_as_dir(env, parent)))
5631 GOTO(out, rc = -ENOTDIR);
5633 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5634 lfsck_lfsck2name(lfsck));
5636 com->lc_new_scanned = 0;
5637 iops = &parent->do_index_ops->dio_it;
5638 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5640 GOTO(out, rc = PTR_ERR(di));
5642 rc = iops->load(env, di, 0);
5644 rc = iops->next(env, di);
5649 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5650 unlikely(!thread_is_running(thread)))
5653 rc = iops->rec(env, di, (struct dt_rec *)ent,
5654 LUDA_64BITHASH | LUDA_TYPE);
5656 rc = lfsck_unpack_ent(ent, &cookie, &type);
5658 if (unlikely(rc != 0)) {
5659 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5660 "/lost+found: rc = %d\n",
5661 lfsck_lfsck2name(lfsck), rc);
5666 /* skip dot and dotdot entries */
5667 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5670 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5673 if (fid_is_norm(&ent->lde_fid)) {
5674 fld_range_set_mdt(range);
5675 rc = fld_local_lookup(env, ss->ss_server_fld,
5676 fid_seq(&ent->lde_fid), range);
5679 } else if (idx != 0) {
5680 /* If the returned FID is IGIF, then there are three
5683 * 1) The object is upgraded from old Lustre-1.8 with
5684 * IGIF assigned to such object.
5685 * 2) The object is a backend local object and is
5686 * invisible to client.
5687 * 3) The object lost its LMV EA, and since there is
5688 * no FID-in-dirent for the orphan in the backend
5689 * /lost+found directory, then the low layer will
5690 * return IGIF for such object.
5692 * For MDTx (x != 0), it is either case 2) or case 3),
5693 * but from the LFSCK view, they are indistinguishable.
5694 * To be safe, the LFSCK will keep it there and report
5695 * some message, then the adminstrator can handle that
5698 * For MDT0, it is more possible the case 1). The LFSCK
5699 * will handle the orphan as an upgraded object. */
5700 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5701 "in the backend /lost+found on the MDT %04x, "
5702 "to be safe, skip it.\n",
5703 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5704 ent->lde_name, PFID(&ent->lde_fid), idx);
5708 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5711 down_write(&com->lc_sem);
5712 com->lc_new_scanned++;
5713 ns->ln_local_lpf_scanned++;
5715 ns->ln_local_lpf_moved++;
5717 ns->ln_local_lpf_skipped++;
5719 ns->ln_local_lpf_failed++;
5720 up_write(&com->lc_sem);
5722 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5726 lfsck_control_speed_by_self(com);
5727 if (unlikely(!thread_is_running(thread))) {
5732 rc = iops->next(env, di);
5736 iops->fini(env, di);
5741 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5742 lfsck_lfsck2name(lfsck), rc);
5744 lfsck_object_put(env, parent);
5748 * Rescan the striped directory after the master LMV EA reset.
5750 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5751 * the namespace LFSCK engine scan the striped directory for the first time,
5752 * it will be regarded as a normal directory. As the LFSCK processing, some
5753 * other LFSCK instance on other MDT will find the shard of this striped dir,
5754 * and find that the master MDT-object of the striped directory lost its LMV
5755 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5756 * notify the LFSCK instance on this MDT to rescan the striped directory.
5758 * \param[in] env pointer to the thread context
5759 * \param[in] com pointer to the lfsck component
5760 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5763 * \retval positive number for success
5764 * \retval 0 for LFSCK stopped/paused
5765 * \retval negative error number on failure
5767 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5768 struct lfsck_component *com,
5769 struct lfsck_lmv_unit *llu)
5771 struct lfsck_thread_info *info = lfsck_env_info(env);
5772 struct lfsck_instance *lfsck = com->lc_lfsck;
5773 struct lfsck_assistant_data *lad = com->lc_data;
5774 struct dt_object *dir;
5775 const struct dt_it_ops *iops;
5777 struct lu_dirent *ent =
5778 (struct lu_dirent *)info->lti_key;
5779 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5780 struct ptlrpc_thread *thread = &lfsck->li_thread;
5781 struct lfsck_namespace_req *lnr;
5782 struct lfsck_assistant_req *lar;
5787 LASSERT(list_empty(&lad->lad_req_list));
5789 lfsck->li_lmv = &llu->llu_lmv;
5790 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5791 rc = lfsck_open_dir(env, lfsck, 0);
5795 dir = lfsck->li_obj_dir;
5796 di = lfsck->li_di_dir;
5797 iops = &dir->do_index_ops->dio_it;
5799 rc = iops->rec(env, di, (struct dt_rec *)ent,
5800 lfsck->li_args_dir);
5802 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5806 if (bk->lb_param & LPF_FAILOUT)
5812 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5815 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5817 if (bk->lb_param & LPF_FAILOUT)
5818 GOTO(out, rc = PTR_ERR(lnr));
5823 lar = &lnr->lnr_lar;
5824 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5825 lfsck_namespace_assistant_req_fini(env, lar);
5826 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5829 if (unlikely(!thread_is_running(thread)))
5833 rc = iops->next(env, di);
5837 lfsck_close_dir(env, lfsck, rc);
5841 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5842 if (list_empty(&lad->lad_req_list))
5845 spin_lock(&lad->lad_lock);
5846 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5848 list_del_init(&lar->lar_list);
5849 spin_unlock(&lad->lad_lock);
5851 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5852 lfsck_namespace_assistant_req_fini(env, lar);
5854 RETURN(rc == 0 ? 1 : rc);
5858 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
5859 struct lfsck_component *com,
5860 struct dt_object *obj, bool first)
5862 struct lfsck_instance *lfsck = com->lc_lfsck;
5863 struct ptlrpc_thread *thread = &lfsck->li_thread;
5864 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5865 struct lfsck_namespace *ns = com->lc_file_ram;
5866 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5867 struct dt_object *target;
5875 di = iops->init(env, obj, 0, BYPASS_CAPA);
5877 RETURN(PTR_ERR(di));
5880 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5883 rc = iops->get(env, di, (const struct dt_key *)&fid);
5888 /* The start one either has been processed or does not exist,
5890 rc = iops->next(env, di);
5896 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5897 unlikely(!thread_is_running(thread)))
5900 key = iops->key(env, di);
5909 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5910 if (!fid_is_sane(&fid)) {
5915 target = lfsck_object_find_bottom(env, lfsck, &fid);
5916 if (IS_ERR(target)) {
5917 rc = PTR_ERR(target);
5921 if (dt_object_exists(target)) {
5922 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5924 rc = lfsck_namespace_double_scan_one(env, com,
5931 lfsck_object_put(env, target);
5934 down_write(&com->lc_sem);
5935 com->lc_new_checked++;
5936 com->lc_new_scanned++;
5937 if (rc >= 0 && fid_is_sane(&fid))
5938 ns->ln_fid_latest_scanned_phase2 = fid;
5940 ns->ln_objs_repaired_phase2++;
5942 ns->ln_objs_failed_phase2++;
5943 up_write(&com->lc_sem);
5945 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5948 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
5949 cfs_time_current())) &&
5950 com->lc_new_checked != 0) {
5951 down_write(&com->lc_sem);
5952 ns->ln_run_time_phase2 +=
5953 cfs_duration_sec(cfs_time_current() +
5954 HALF_SEC - com->lc_time_last_checkpoint);
5955 ns->ln_time_last_checkpoint = cfs_time_current_sec();
5956 ns->ln_objs_checked_phase2 += com->lc_new_checked;
5957 com->lc_new_checked = 0;
5958 rc = lfsck_namespace_store(env, com, false);
5959 up_write(&com->lc_sem);
5963 com->lc_time_last_checkpoint = cfs_time_current();
5964 com->lc_time_next_checkpoint =
5965 com->lc_time_last_checkpoint +
5966 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5969 lfsck_control_speed_by_self(com);
5970 if (unlikely(!thread_is_running(thread)))
5973 rc = iops->next(env, di);
5982 iops->fini(env, di);
5987 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5988 struct lfsck_component *com)
5990 struct lfsck_instance *lfsck = com->lc_lfsck;
5991 struct lfsck_namespace *ns = com->lc_file_ram;
5996 while (!list_empty(&lfsck->li_list_lmv)) {
5997 struct lfsck_lmv_unit *llu;
5999 spin_lock(&lfsck->li_lock);
6000 llu = list_entry(lfsck->li_list_lmv.next,
6001 struct lfsck_lmv_unit, llu_link);
6002 list_del_init(&llu->llu_link);
6003 spin_unlock(&lfsck->li_lock);
6005 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6010 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6011 lfsck_lfsck2name(lfsck));
6013 lfsck_namespace_scan_local_lpf(env, com);
6015 com->lc_new_checked = 0;
6016 com->lc_new_scanned = 0;
6017 com->lc_time_last_checkpoint = cfs_time_current();
6018 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6019 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6021 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6022 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6023 com->lc_sub_trace_objs[i].lsto_obj, true);
6024 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6025 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6026 com->lc_sub_trace_objs[i].lsto_obj, false);
6028 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6029 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6034 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6035 struct lfsck_component *com,
6036 struct lfsck_position *pos)
6038 struct lfsck_assistant_data *lad = com->lc_data;
6039 struct lfsck_namespace_req *lnr;
6041 if (list_empty(&lad->lad_req_list))
6044 lnr = list_entry(lad->lad_req_list.next,
6045 struct lfsck_namespace_req,
6047 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6048 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6049 pos->lp_dir_parent = lnr->lnr_lar.lar_fid;
6052 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6053 struct lfsck_component *com,
6056 struct lfsck_instance *lfsck = com->lc_lfsck;
6057 struct lfsck_namespace *ns = com->lc_file_ram;
6059 down_write(&com->lc_sem);
6060 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6061 HALF_SEC - lfsck->li_time_last_checkpoint);
6062 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6063 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6064 com->lc_new_checked = 0;
6067 if (ns->ln_flags & LF_INCOMPLETE)
6068 ns->ln_status = LS_PARTIAL;
6070 ns->ln_status = LS_COMPLETED;
6071 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6072 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6073 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6074 ns->ln_success_count++;
6075 } else if (rc == 0) {
6076 if (lfsck->li_status != 0)
6077 ns->ln_status = lfsck->li_status;
6079 ns->ln_status = LS_STOPPED;
6081 ns->ln_status = LS_FAILED;
6084 rc = lfsck_namespace_store(env, com, false);
6085 up_write(&com->lc_sem);
6091 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6092 struct ptlrpc_request *req,
6096 struct lfsck_async_interpret_args *laia = args;
6097 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6099 ltd->ltd_synced_failures = 1;
6106 * Notify remote LFSCK instances about former failures.
6108 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6109 * some LFSCK verification requests (maybe because of network issues or the MDT
6110 * itself trouble). During the respond gap the MDT may missed some name entries
6111 * verification, then the MDT cannot know whether related MDT-objects have been
6112 * referenced by related name entries or not, then in the second-stage scanning,
6113 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6114 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6115 * name entry for repairing the orphan.
6117 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6118 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6119 * have ever missed some name entries verification and should skip the handling
6120 * for orphan MDT-objects.
6122 * \param[in] env pointer to the thread context
6123 * \param[in] com pointer to the lfsck component
6124 * \param[in] lr pointer to the lfsck request
6126 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6127 struct lfsck_component *com,
6128 struct lfsck_request *lr)
6130 struct lfsck_async_interpret_args *laia =
6131 &lfsck_env_info(env)->lti_laia2;
6132 struct lfsck_assistant_data *lad = com->lc_data;
6133 struct lfsck_namespace *ns = com->lc_file_ram;
6134 struct lfsck_instance *lfsck = com->lc_lfsck;
6135 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6136 struct lfsck_tgt_desc *ltd;
6137 struct ptlrpc_request_set *set;
6142 if (!lad->lad_incomplete)
6145 set = ptlrpc_prep_set();
6147 GOTO(out, rc = -ENOMEM);
6149 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6150 memset(laia, 0, sizeof(*laia));
6151 lad->lad_touch_gen++;
6153 down_read(<ds->ltd_rw_sem);
6154 cfs_foreach_bit(lad->lad_bitmap, idx) {
6155 ltd = LTD_TGT(ltds, idx);
6156 LASSERT(ltd != NULL);
6158 laia->laia_ltd = ltd;
6159 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6160 lfsck_namespace_assistant_sync_failures_interpret,
6161 laia, LFSCK_NOTIFY);
6163 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6164 "to sync failure with MDT %x: rc = %d\n",
6165 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6167 up_read(<ds->ltd_rw_sem);
6169 rc = ptlrpc_set_wait(set);
6170 ptlrpc_set_destroy(set);
6176 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6177 "to sync failure with MDTs, and related MDTs "
6178 "may handle orphan improperly: rc = %d\n",
6179 lfsck_lfsck2name(lfsck), rc);
6184 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6185 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6186 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6187 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6188 .la_double_scan_result = lfsck_namespace_double_scan_result,
6189 .la_req_fini = lfsck_namespace_assistant_req_fini,
6190 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6194 * Verify the specified linkEA entry for the given directory object.
6195 * If the object has no such linkEA entry or it has more other linkEA
6196 * entries, then re-generate the linkEA with the given information.
6198 * \param[in] env pointer to the thread context
6199 * \param[in] obj pointer to the dt_object to be handled
6200 * \param[in] cname the name for the child in the parent directory
6201 * \param[in] pfid the parent directory's FID for the linkEA
6203 * \retval 0 for success
6204 * \retval negative error number on failure
6206 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6207 const struct lu_name *cname, const struct lu_fid *pfid)
6209 struct dt_device *dev = lfsck_obj2dev(obj);
6210 struct linkea_data ldata = { NULL };
6211 struct lu_buf linkea_buf;
6214 int fl = LU_XATTR_CREATE;
6218 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6220 rc = lfsck_links_read(env, obj, &ldata);
6221 if (rc == -ENODATA) {
6223 } else if (rc == 0) {
6224 fl = LU_XATTR_REPLACE;
6225 if (ldata.ld_leh->leh_reccount != 1) {
6228 rc = linkea_links_find(&ldata, cname, pfid);
6237 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6241 rc = linkea_add_buf(&ldata, cname, pfid);
6245 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6246 ldata.ld_leh->leh_len);
6247 th = dt_trans_create(env, dev);
6249 RETURN(PTR_ERR(th));
6251 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6252 XATTR_NAME_LINK, fl, th);
6256 rc = dt_trans_start_local(env, dev, th);
6260 dt_write_lock(env, obj, 0);
6261 rc = dt_xattr_set(env, obj, &linkea_buf,
6262 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6263 dt_write_unlock(env, obj);
6268 dt_trans_stop(env, dev, th);
6273 * Get the name and parent directory's FID from the first linkEA entry.
6275 * \param[in] env pointer to the thread context
6276 * \param[in] obj pointer to the object which get linkEA from
6277 * \param[out] name pointer to the buffer to hold the name
6278 * in the first linkEA entry
6279 * \param[out] pfid pointer to the buffer to hold the parent
6280 * directory's FID in the first linkEA entry
6282 * \retval 0 for success
6283 * \retval negative error number on failure
6285 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6286 char *name, struct lu_fid *pfid)
6288 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6289 struct linkea_data ldata = { NULL };
6292 rc = lfsck_links_read(env, obj, &ldata);
6296 linkea_first_entry(&ldata);
6297 if (ldata.ld_lee == NULL)
6300 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6301 /* To guarantee the 'name' is terminated with '0'. */
6302 memcpy(name, cname->ln_name, cname->ln_namelen);
6303 name[cname->ln_namelen] = 0;
6309 * Update the object's name entry with the given FID.
6311 * \param[in] env pointer to the thread context
6312 * \param[in] lfsck pointer to the lfsck instance
6313 * \param[in] dir pointer to the directory that holds
6315 * \param[in] name the name for the entry to be updated
6316 * \param[in] fid the new FID for the name entry referenced
6317 * \param[in] type the type for the name entry to be updated
6319 * \retval 0 for success
6320 * \retval negative error number on failure
6322 int lfsck_update_name_entry(const struct lu_env *env,
6323 struct lfsck_instance *lfsck,
6324 struct dt_object *dir, const char *name,
6325 const struct lu_fid *fid, __u32 type)
6327 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
6328 struct dt_device *dev = lfsck_obj2dev(dir);
6329 struct lustre_handle lh = { 0 };
6335 rc = lfsck_ibits_lock(env, lfsck, dir, &lh,
6336 MDS_INODELOCK_UPDATE, LCK_EX);
6340 th = dt_trans_create(env, dev);
6342 GOTO(unlock, rc = PTR_ERR(th));
6344 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6348 rec->rec_type = type;
6350 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6351 (const struct dt_key *)name, th);
6355 rc = dt_declare_ref_add(env, dir, th);
6359 rc = dt_trans_start_local(env, dev, th);
6363 rc = dt_delete(env, dir, (const struct dt_key *)name, th,
6365 if (rc == -ENOENT) {
6373 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6374 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6375 if (rc == 0 && S_ISDIR(type) && !exists) {
6376 dt_write_lock(env, dir, 0);
6377 rc = dt_ref_add(env, dir, th);
6378 dt_write_unlock(env, dir);
6384 dt_trans_stop(env, dev, th);
6387 lfsck_ibits_unlock(&lh, LCK_EX);
6388 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6389 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6390 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6395 int lfsck_namespace_setup(const struct lu_env *env,
6396 struct lfsck_instance *lfsck)
6398 struct lfsck_component *com;
6399 struct lfsck_namespace *ns;
6400 struct dt_object *root = NULL;
6401 struct dt_object *obj;
6406 LASSERT(lfsck->li_master);
6412 INIT_LIST_HEAD(&com->lc_link);
6413 INIT_LIST_HEAD(&com->lc_link_dir);
6414 init_rwsem(&com->lc_sem);
6415 atomic_set(&com->lc_ref, 1);
6416 com->lc_lfsck = lfsck;
6417 com->lc_type = LFSCK_TYPE_NAMESPACE;
6418 com->lc_ops = &lfsck_namespace_ops;
6419 com->lc_data = lfsck_assistant_data_init(
6420 &lfsck_namespace_assistant_ops,
6422 if (com->lc_data == NULL)
6423 GOTO(out, rc = -ENOMEM);
6425 com->lc_file_size = sizeof(struct lfsck_namespace);
6426 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6427 if (com->lc_file_ram == NULL)
6428 GOTO(out, rc = -ENOMEM);
6430 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6431 if (com->lc_file_disk == NULL)
6432 GOTO(out, rc = -ENOMEM);
6434 for (i = 0; i < LFSCK_STF_COUNT; i++)
6435 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6437 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6439 GOTO(out, rc = PTR_ERR(root));
6441 if (unlikely(!dt_try_as_dir(env, root)))
6442 GOTO(out, rc = -ENOTDIR);
6444 obj = local_file_find_or_create(env, lfsck->li_los, root,
6446 S_IFREG | S_IRUGO | S_IWUSR);
6448 GOTO(out, rc = PTR_ERR(obj));
6451 rc = lfsck_namespace_load(env, com);
6453 rc = lfsck_namespace_init(env, com);
6455 rc = lfsck_namespace_reset(env, com, true);
6457 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6461 ns = com->lc_file_ram;
6462 switch (ns->ln_status) {
6467 spin_lock(&lfsck->li_lock);
6468 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6469 spin_unlock(&lfsck->li_lock);
6472 CERROR("%s: unknown lfsck_namespace status %d\n",
6473 lfsck_lfsck2name(lfsck), ns->ln_status);
6475 case LS_SCANNING_PHASE1:
6476 case LS_SCANNING_PHASE2:
6477 /* No need to store the status to disk right now.
6478 * If the system crashed before the status stored,
6479 * it will be loaded back when next time. */
6480 ns->ln_status = LS_CRASHED;
6484 spin_lock(&lfsck->li_lock);
6485 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6486 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6487 spin_unlock(&lfsck->li_lock);
6494 if (root != NULL && !IS_ERR(root))
6495 lfsck_object_put(env, root);
6497 lfsck_component_cleanup(env, com);
6498 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6499 lfsck_lfsck2name(lfsck), rc);