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 cfs_bitmap_t *bitmap = NULL;
390 struct thandle *handle;
392 int len = com->lc_file_size;
394 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
395 struct lu_buf tbuf = { &len, sizeof(len) };
400 bitmap = lad->lad_bitmap;
401 nbits = bitmap->size;
404 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
407 ns->ln_bitmap_size = nbits;
408 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
410 handle = dt_trans_create(env, lfsck->li_bottom);
412 GOTO(log, rc = PTR_ERR(handle));
414 rc = dt_declare_xattr_set(env, obj,
415 lfsck_buf_get(env, com->lc_file_disk, len),
416 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
420 if (bitmap != NULL) {
421 rc = dt_declare_xattr_set(env, obj,
422 lfsck_buf_get(env, bitmap->data, nbits >> 3),
423 XATTR_NAME_LFSCK_BITMAP, 0, handle);
428 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
429 /* To be compatible with old Lustre-2.x MDT (x <= 6), generate dummy
430 * XATTR_NAME_LFSCK_NAMESPACE_OLD EA, then when downgrade to Lustre-2.x,
431 * the old LFSCK will find "invalid" XATTR_NAME_LFSCK_NAMESPACE_OLD EA,
432 * then reset the namespace LFSCK trace file. */
434 rc = dt_declare_xattr_set(env, obj, &tbuf,
435 XATTR_NAME_LFSCK_NAMESPACE_OLD,
436 LU_XATTR_CREATE, handle);
442 rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
446 rc = dt_xattr_set(env, obj,
447 lfsck_buf_get(env, com->lc_file_disk, len),
448 XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
449 if (rc == 0 && bitmap != NULL)
450 rc = dt_xattr_set(env, obj,
451 lfsck_buf_get(env, bitmap->data, nbits >> 3),
452 XATTR_NAME_LFSCK_BITMAP, 0, handle,
455 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
457 rc = dt_xattr_set(env, obj, &tbuf,
458 XATTR_NAME_LFSCK_NAMESPACE_OLD,
459 LU_XATTR_CREATE, handle, BYPASS_CAPA);
465 dt_trans_stop(env, lfsck->li_bottom, handle);
469 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
470 lfsck_lfsck2name(lfsck), rc);
474 static struct dt_object *
475 lfsck_namespace_load_one_trace_file(const struct lu_env *env,
476 struct lfsck_component *com,
477 struct dt_object *parent,
479 const struct dt_index_features *ft,
482 struct lfsck_instance *lfsck = com->lc_lfsck;
483 struct dt_object *obj;
487 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
488 if (rc != 0 && rc != -ENOENT)
493 obj = local_index_find_or_create(env, lfsck->li_los, parent,
494 name, S_IFREG | S_IRUGO | S_IWUSR, ft);
496 obj = local_file_find_or_create(env, lfsck->li_los, parent,
497 name, S_IFREG | S_IRUGO | S_IWUSR);
502 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
503 struct lfsck_component *com,
506 char *name = lfsck_env_info(env)->lti_key;
507 struct lfsck_sub_trace_obj *lsto;
508 struct dt_object *obj;
512 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
513 i < LFSCK_STF_COUNT; i++, lsto++) {
514 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
515 if (lsto->lsto_obj != NULL) {
519 lu_object_put(env, &lsto->lsto_obj->do_lu);
520 lsto->lsto_obj = NULL;
523 obj = lfsck_namespace_load_one_trace_file(env, com,
524 com->lc_lfsck->li_lfsck_dir,
525 name, &dt_lfsck_features, reset);
529 lsto->lsto_obj = obj;
530 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
538 static int lfsck_namespace_init(const struct lu_env *env,
539 struct lfsck_component *com)
541 struct lfsck_namespace *ns = com->lc_file_ram;
544 memset(ns, 0, sizeof(*ns));
545 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
546 ns->ln_status = LS_INIT;
547 down_write(&com->lc_sem);
548 rc = lfsck_namespace_store(env, com, true);
549 up_write(&com->lc_sem);
551 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
557 * Update the namespace LFSCK trace file for the given @fid
559 * \param[in] env pointer to the thread context
560 * \param[in] com pointer to the lfsck component
561 * \param[in] fid the fid which flags to be updated in the lfsck
563 * \param[in] add true if add new flags, otherwise remove flags
565 * \retval 0 for succeed or nothing to be done
566 * \retval negative error number on failure
568 int lfsck_namespace_trace_update(const struct lu_env *env,
569 struct lfsck_component *com,
570 const struct lu_fid *fid,
571 const __u8 flags, bool add)
573 struct lfsck_instance *lfsck = com->lc_lfsck;
574 struct dt_object *obj;
575 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
576 struct dt_device *dev = lfsck->li_bottom;
577 struct thandle *th = NULL;
586 if (unlikely(!fid_is_sane(fid)))
589 idx = lfsck_sub_trace_file_fid2idx(fid);
590 obj = com->lc_sub_trace_objs[idx].lsto_obj;
591 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
592 fid_cpu_to_be(key, fid);
593 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
594 (const struct dt_key *)key, BYPASS_CAPA);
597 GOTO(unlock, rc = 0);
601 } else if (rc == 0) {
603 if ((old & flags) == flags)
604 GOTO(unlock, rc = 0);
608 if ((old & flags) == 0)
609 GOTO(unlock, rc = 0);
617 th = dt_trans_create(env, dev);
619 GOTO(log, rc = PTR_ERR(th));
622 rc = dt_declare_delete(env, obj,
623 (const struct dt_key *)key, th);
629 rc = dt_declare_insert(env, obj,
630 (const struct dt_rec *)&new,
631 (const struct dt_key *)key, th);
636 rc = dt_trans_start_local(env, dev, th);
641 rc = dt_delete(env, obj, (const struct dt_key *)key,
648 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
649 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
657 if (th != NULL && !IS_ERR(th))
658 dt_trans_stop(env, dev, th);
660 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
661 "trace file, flags %x, old %x, new %x: rc = %d\n",
662 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
663 (__u32)flags, (__u32)old, (__u32)new, rc);
666 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
671 int lfsck_namespace_check_exist(const struct lu_env *env,
672 struct dt_object *dir,
673 struct dt_object *obj, const char *name)
675 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
679 if (unlikely(lfsck_is_dead_obj(obj)))
680 RETURN(LFSCK_NAMEENTRY_DEAD);
682 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
683 (const struct dt_key *)name, BYPASS_CAPA);
685 RETURN(LFSCK_NAMEENTRY_REMOVED);
690 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
691 RETURN(LFSCK_NAMEENTRY_RECREATED);
696 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
697 struct dt_object *obj,
698 struct thandle *handle)
702 /* For destroying all invalid linkEA entries. */
703 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
707 /* For insert new linkEA entry. */
708 rc = dt_declare_xattr_set(env, obj,
709 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
710 XATTR_NAME_LINK, 0, handle);
714 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
715 struct linkea_data *ldata)
719 if (ldata->ld_buf->lb_buf == NULL)
722 if (!dt_object_exists(obj))
725 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
727 /* Buf was too small, figure out what we need. */
728 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
733 lu_buf_realloc(ldata->ld_buf, rc);
734 if (ldata->ld_buf->lb_buf == NULL)
737 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
742 rc = linkea_init(ldata);
748 * Remove linkEA for the given object.
750 * The caller should take the ldlm lock before the calling.
752 * \param[in] env pointer to the thread context
753 * \param[in] com pointer to the lfsck component
754 * \param[in] obj pointer to the dt_object to be handled
756 * \retval 0 for repaired cases
757 * \retval negative error number on failure
759 static int lfsck_namespace_links_remove(const struct lu_env *env,
760 struct lfsck_component *com,
761 struct dt_object *obj)
763 struct lfsck_instance *lfsck = com->lc_lfsck;
764 struct dt_device *dev = lfsck->li_bottom;
765 struct thandle *th = NULL;
769 LASSERT(dt_object_remote(obj) == 0);
771 th = dt_trans_create(env, dev);
773 GOTO(log, rc = PTR_ERR(th));
775 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
779 rc = dt_trans_start_local(env, dev, th);
783 dt_write_lock(env, obj, 0);
784 if (unlikely(lfsck_is_dead_obj(obj)))
785 GOTO(unlock, rc = -ENOENT);
787 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
788 GOTO(unlock, rc = 0);
790 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
795 dt_write_unlock(env, obj);
798 dt_trans_stop(env, dev, th);
801 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
802 "for the object "DFID": rc = %d\n",
803 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
806 struct lfsck_namespace *ns = com->lc_file_ram;
808 ns->ln_flags |= LF_INCONSISTENT;
814 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
815 struct linkea_data *ldata, struct thandle *handle)
817 const struct lu_buf *buf = lfsck_buf_get_const(env,
818 ldata->ld_buf->lb_buf,
819 ldata->ld_leh->leh_len);
821 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
825 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
826 struct lu_name *cname,
830 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
831 /* To guarantee the 'name' is terminated with '0'. */
832 memcpy(buf, cname->ln_name, cname->ln_namelen);
833 buf[cname->ln_namelen] = 0;
834 cname->ln_name = buf;
837 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
838 struct lu_name *cname,
842 struct link_ea_entry *oldlee;
846 oldlee = ldata->ld_lee;
847 oldlen = ldata->ld_reclen;
848 linkea_next_entry(ldata);
849 while (ldata->ld_lee != NULL) {
850 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
851 ldata->ld_lee->lee_reclen[1];
852 if (unlikely(ldata->ld_reclen == oldlen &&
853 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
858 linkea_del_buf(ldata, cname);
860 linkea_next_entry(ldata);
863 ldata->ld_lee = oldlee;
864 ldata->ld_reclen = oldlen;
870 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
872 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
873 * with the given type to generate the name, the detailed rules for name
874 * have been described as following.
876 * The function also generates the linkEA corresponding to the name entry
877 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
879 * \param[in] env pointer to the thread context
880 * \param[in] com pointer to the lfsck component
881 * \param[in] orphan pointer to the orphan MDT-object
882 * \param[in] infix additional information for the orphan name, such as
883 * the FID for original
884 * \param[in] type the type for describing why the orphan MDT-object is
885 * created. The rules are as following:
887 * type "D": The MDT-object is a directory, it may knows its parent
888 * but because there is no valid linkEA, the LFSCK cannot
889 * know where to put it back to the namespace.
890 * type "O": The MDT-object has no linkEA, and there is no name
891 * entry that references the MDT-object.
893 * type "S": The orphan MDT-object is a shard of a striped directory
895 * \see lfsck_layout_recreate_parent() for more types.
897 * The orphan name will be like:
898 * ${FID}-${infix}-${type}-${conflict_version}
900 * \param[out] count if some others inserted some linkEA entries by race,
901 * then return the linkEA entries count.
903 * \retval positive number for repaired cases
904 * \retval 0 if needs to repair nothing
905 * \retval negative error number on failure
907 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
908 struct lfsck_component *com,
909 struct dt_object *orphan,
910 const char *infix, const char *type,
913 struct lfsck_thread_info *info = lfsck_env_info(env);
914 struct lu_name *cname = &info->lti_name;
915 struct dt_insert_rec *rec = &info->lti_dt_rec;
916 struct lu_attr *la = &info->lti_la3;
917 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
918 const struct lu_fid *pfid;
920 struct lfsck_instance *lfsck = com->lc_lfsck;
921 struct dt_device *dev = lfsck->li_bottom;
922 struct dt_object *parent;
923 struct thandle *th = NULL;
924 struct lustre_handle plh = { 0 };
925 struct lustre_handle clh = { 0 };
926 struct linkea_data ldata = { NULL };
927 struct lu_buf linkea_buf;
934 cname->ln_name = NULL;
935 if (unlikely(lfsck->li_lpf_obj == NULL))
936 GOTO(log, rc = -ENXIO);
938 parent = lfsck->li_lpf_obj;
939 pfid = lfsck_dto2fid(parent);
941 /* Hold update lock on the parent to prevent others to access. */
942 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
943 MDS_INODELOCK_UPDATE, LCK_EX);
948 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
949 PFID(cfid), infix, type, idx++);
950 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
951 (const struct dt_key *)info->lti_key,
953 if (rc != 0 && rc != -ENOENT)
956 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
958 } while (rc == 0 && !exist);
960 cname->ln_name = info->lti_key;
961 cname->ln_namelen = namelen;
962 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
966 rc = linkea_add_buf(&ldata, cname, pfid);
970 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
971 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
976 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
977 ldata.ld_leh->leh_len);
978 th = dt_trans_create(env, dev);
980 GOTO(log, rc = PTR_ERR(th));
982 if (S_ISDIR(lfsck_object_type(orphan))) {
983 rc = dt_declare_delete(env, orphan,
984 (const struct dt_key *)dotdot, th);
988 rec->rec_type = S_IFDIR;
990 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
991 (const struct dt_key *)dotdot, th);
996 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
997 XATTR_NAME_LINK, 0, th);
1002 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1003 rec->rec_fid = cfid;
1004 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1005 (const struct dt_key *)cname->ln_name,
1010 if (S_ISDIR(rec->rec_type)) {
1011 rc = dt_declare_ref_add(env, parent, th);
1017 memset(la, 0, sizeof(*la));
1018 la->la_ctime = cfs_time_current_sec();
1019 la->la_valid = LA_CTIME;
1020 rc = dt_declare_attr_set(env, orphan, la, th);
1024 rc = dt_trans_start_local(env, dev, th);
1028 dt_write_lock(env, orphan, 0);
1029 rc = lfsck_links_read(env, orphan, &ldata);
1030 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
1031 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
1032 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1033 GOTO(unlock, rc = 1);
1035 if (S_ISDIR(lfsck_object_type(orphan))) {
1036 rc = dt_delete(env, orphan,
1037 (const struct dt_key *)dotdot, th,
1042 rec->rec_type = S_IFDIR;
1043 rec->rec_fid = pfid;
1044 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1045 (const struct dt_key *)dotdot, th,
1051 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1054 if (rc == 0 && count != NULL)
1055 *count = ldata.ld_leh->leh_reccount;
1059 dt_write_unlock(env, orphan);
1061 if (rc == 0 && !exist) {
1062 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1063 rec->rec_fid = cfid;
1064 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1065 (const struct dt_key *)cname->ln_name,
1066 th, BYPASS_CAPA, 1);
1067 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1068 dt_write_lock(env, parent, 0);
1069 rc = dt_ref_add(env, parent, th);
1070 dt_write_unlock(env, parent);
1075 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
1077 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1080 dt_write_unlock(env, orphan);
1083 dt_trans_stop(env, dev, th);
1086 lfsck_ibits_unlock(&clh, LCK_EX);
1087 lfsck_ibits_unlock(&plh, LCK_EX);
1088 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1089 "object "DFID", name = %s: rc = %d\n",
1090 lfsck_lfsck2name(lfsck), PFID(cfid),
1091 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1094 struct lfsck_namespace *ns = com->lc_file_ram;
1096 ns->ln_flags |= LF_INCONSISTENT;
1103 * Add the specified name entry back to namespace.
1105 * If there is a linkEA entry that back references a name entry under
1106 * some parent directory, but such parent directory does not have the
1107 * claimed name entry. On the other hand, the linkEA entries count is
1108 * not larger than the MDT-object's hard link count. Under such case,
1109 * it is quite possible that the name entry is lost. Then the LFSCK
1110 * should add the name entry back to the namespace.
1112 * \param[in] env pointer to the thread context
1113 * \param[in] com pointer to the lfsck component
1114 * \param[in] parent pointer to the directory under which the name entry
1115 * will be inserted into
1116 * \param[in] child pointer to the object referenced by the name entry
1117 * that to be inserted into the parent
1118 * \param[in] name the name for the child in the parent directory
1120 * \retval positive number for repaired cases
1121 * \retval 0 if nothing to be repaired
1122 * \retval negative error number on failure
1124 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1125 struct lfsck_component *com,
1126 struct dt_object *parent,
1127 struct dt_object *child,
1130 struct lfsck_thread_info *info = lfsck_env_info(env);
1131 struct lu_attr *la = &info->lti_la;
1132 struct dt_insert_rec *rec = &info->lti_dt_rec;
1133 struct lfsck_instance *lfsck = com->lc_lfsck;
1134 struct dt_device *dev = lfsck->li_next;
1135 struct thandle *th = NULL;
1136 struct lustre_handle lh = { 0 };
1140 if (unlikely(!dt_try_as_dir(env, parent)))
1141 GOTO(log, rc = -ENOTDIR);
1143 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1146 /* Hold update lock on the parent to prevent others to access. */
1147 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1148 MDS_INODELOCK_UPDATE, LCK_EX);
1152 th = dt_trans_create(env, dev);
1154 GOTO(unlock, rc = PTR_ERR(th));
1156 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1157 rec->rec_fid = lfsck_dto2fid(child);
1158 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1159 (const struct dt_key *)name, th);
1163 if (S_ISDIR(rec->rec_type)) {
1164 rc = dt_declare_ref_add(env, parent, th);
1169 memset(la, 0, sizeof(*la));
1170 la->la_ctime = cfs_time_current_sec();
1171 la->la_valid = LA_CTIME;
1172 rc = dt_declare_attr_set(env, parent, la, th);
1176 rc = dt_declare_attr_set(env, child, la, th);
1180 rc = dt_trans_start_local(env, dev, th);
1184 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1185 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1189 if (S_ISDIR(rec->rec_type)) {
1190 dt_write_lock(env, parent, 0);
1191 rc = dt_ref_add(env, parent, th);
1192 dt_write_unlock(env, parent);
1197 la->la_ctime = cfs_time_current_sec();
1198 rc = dt_attr_set(env, parent, la, th, BYPASS_CAPA);
1202 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
1204 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1207 dt_trans_stop(env, dev, th);
1210 lfsck_ibits_unlock(&lh, LCK_EX);
1213 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1214 "the name %s and type %o to the parent "DFID": rc = %d\n",
1215 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1216 lfsck_object_type(child) & S_IFMT,
1217 PFID(lfsck_dto2fid(parent)), rc);
1220 struct lfsck_namespace *ns = com->lc_file_ram;
1222 ns->ln_flags |= LF_INCONSISTENT;
1224 ns->ln_lost_dirent_repaired++;
1231 * Create the specified orphan directory.
1233 * For the case that the parent MDT-object stored in some MDT-object's
1234 * linkEA entry is lost, the LFSCK will re-create the parent object as
1235 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1236 * with the name ${FID}-P-${conflict_version}.
1238 * \param[in] env pointer to the thread context
1239 * \param[in] com pointer to the lfsck component
1240 * \param[in] orphan pointer to the orphan MDT-object to be created
1241 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1243 * \retval positive number for repaired cases
1244 * \retval negative error number on failure
1246 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1247 struct lfsck_component *com,
1248 struct dt_object *orphan,
1249 struct lmv_mds_md_v1 *lmv)
1251 struct lfsck_thread_info *info = lfsck_env_info(env);
1252 struct lu_attr *la = &info->lti_la;
1253 struct dt_allocation_hint *hint = &info->lti_hint;
1254 struct dt_object_format *dof = &info->lti_dof;
1255 struct lu_name *cname = &info->lti_name2;
1256 struct dt_insert_rec *rec = &info->lti_dt_rec;
1257 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1258 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1260 struct lfsck_instance *lfsck = com->lc_lfsck;
1261 struct lfsck_namespace *ns = com->lc_file_ram;
1262 struct dt_device *dev;
1263 struct dt_object *parent = NULL;
1264 struct dt_object *child = NULL;
1265 struct thandle *th = NULL;
1266 struct lustre_handle lh = { 0 };
1267 struct linkea_data ldata = { NULL };
1268 struct lu_buf linkea_buf;
1269 struct lu_buf lmv_buf;
1276 LASSERT(!dt_object_exists(orphan));
1278 cname->ln_name = NULL;
1279 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1282 if (dt_object_remote(orphan)) {
1283 LASSERT(lfsck->li_lpf_root_obj != NULL);
1285 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1287 GOTO(log, rc = idx);
1289 snprintf(name, 8, "MDT%04x", idx);
1290 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1291 (struct dt_rec *)&tfid,
1292 (const struct dt_key *)name, BYPASS_CAPA);
1294 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1296 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1298 GOTO(log, rc = PTR_ERR(parent));
1300 if (unlikely(!dt_try_as_dir(env, parent)))
1301 GOTO(log, rc = -ENOTDIR);
1303 if (unlikely(lfsck->li_lpf_obj == NULL))
1304 GOTO(log, rc = -ENXIO);
1306 parent = lfsck->li_lpf_obj;
1309 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1311 GOTO(log, rc = PTR_ERR(dev));
1313 child = lfsck_object_find_by_dev(env, dev, cfid);
1315 GOTO(log, rc = PTR_ERR(child));
1317 /* Hold update lock on the parent to prevent others to access. */
1318 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1319 MDS_INODELOCK_UPDATE, LCK_EX);
1325 namelen = snprintf(name, 31, DFID"-P-%d",
1327 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1328 (const struct dt_key *)name, BYPASS_CAPA);
1329 if (rc != 0 && rc != -ENOENT)
1333 cname->ln_name = name;
1334 cname->ln_namelen = namelen;
1336 memset(la, 0, sizeof(*la));
1337 la->la_mode = S_IFDIR | 0700;
1338 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1339 LA_ATIME | LA_MTIME | LA_CTIME;
1341 child->do_ops->do_ah_init(env, hint, parent, child,
1342 la->la_mode & S_IFMT);
1344 memset(dof, 0, sizeof(*dof));
1345 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1347 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1351 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
1355 th = dt_trans_create(env, dev);
1357 GOTO(unlock1, rc = PTR_ERR(th));
1359 /* Sync the remote transaction to guarantee that the subsequent
1360 * lock against the @orphan can find the @orphan in time. */
1361 if (dt_object_remote(orphan))
1364 rc = dt_declare_create(env, child, la, hint, dof, th);
1368 if (unlikely(!dt_try_as_dir(env, child)))
1369 GOTO(stop, rc = -ENOTDIR);
1371 rec->rec_type = S_IFDIR;
1372 rec->rec_fid = cfid;
1373 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
1374 (const struct dt_key *)dot, th);
1378 rec->rec_fid = lfsck_dto2fid(parent);
1379 rc = dt_declare_insert(env, child, (const struct dt_rec *)rec,
1380 (const struct dt_key *)dotdot, th);
1382 rc = dt_declare_ref_add(env, child, th);
1387 rc = dt_declare_ref_add(env, child, th);
1392 lmv->lmv_magic = LMV_MAGIC;
1393 lmv->lmv_master_mdt_index = lfsck_dev_idx(dev);
1394 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1395 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1396 rc = dt_declare_xattr_set(env, child, &lmv_buf,
1397 XATTR_NAME_LMV, 0, th);
1402 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1403 ldata.ld_leh->leh_len);
1404 rc = dt_declare_xattr_set(env, child, &linkea_buf,
1405 XATTR_NAME_LINK, 0, th);
1409 rec->rec_fid = cfid;
1410 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1411 (const struct dt_key *)name, th);
1413 rc = dt_declare_ref_add(env, parent, th);
1418 rc = dt_trans_start_local(env, dev, th);
1422 dt_write_lock(env, child, 0);
1423 rc = dt_create(env, child, la, hint, dof, th);
1427 rec->rec_fid = cfid;
1428 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1429 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1433 rec->rec_fid = lfsck_dto2fid(parent);
1434 rc = dt_insert(env, child, (const struct dt_rec *)rec,
1435 (const struct dt_key *)dotdot, th,
1440 rc = dt_ref_add(env, child, th);
1445 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV, 0,
1451 rc = dt_xattr_set(env, child, &linkea_buf,
1452 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1453 dt_write_unlock(env, child);
1457 rec->rec_fid = cfid;
1458 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1459 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1461 dt_write_lock(env, parent, 0);
1462 rc = dt_ref_add(env, parent, th);
1463 dt_write_unlock(env, parent);
1466 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1469 dt_write_unlock(env, child);
1472 dt_trans_stop(env, dev, th);
1475 lfsck_ibits_unlock(&lh, LCK_EX);
1478 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1479 "the object "DFID", name = %s: rc = %d\n",
1480 lfsck_lfsck2name(lfsck), PFID(cfid),
1481 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1483 if (child != NULL && !IS_ERR(child))
1484 lfsck_object_put(env, child);
1486 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1487 lfsck_object_put(env, parent);
1490 ns->ln_flags |= LF_INCONSISTENT;
1496 * Remove the specified entry from the linkEA.
1498 * Locate the linkEA entry with the given @cname and @pfid, then
1499 * remove this entry or the other entries those are repeated with
1502 * \param[in] env pointer to the thread context
1503 * \param[in] com pointer to the lfsck component
1504 * \param[in] obj pointer to the dt_object to be handled
1505 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1506 * \param[in] cname the name for the child in the parent directory
1507 * \param[in] pfid the parent directory's FID for the linkEA
1508 * \param[in] next if true, then remove the first found linkEA
1509 * entry, and move the ldata->ld_lee to next entry
1511 * \retval positive number for repaired cases
1512 * \retval 0 if nothing to be repaired
1513 * \retval negative error number on failure
1515 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1516 struct lfsck_component *com,
1517 struct dt_object *obj,
1518 struct linkea_data *ldata,
1519 struct lu_name *cname,
1520 struct lu_fid *pfid,
1523 struct lfsck_instance *lfsck = com->lc_lfsck;
1524 struct dt_device *dev = lfsck->li_bottom;
1525 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1526 struct thandle *th = NULL;
1527 struct lustre_handle lh = { 0 };
1528 struct linkea_data ldata_new = { NULL };
1529 struct lu_buf linkea_buf;
1533 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1534 MDS_INODELOCK_UPDATE |
1535 MDS_INODELOCK_XATTR, LCK_EX);
1540 linkea_del_buf(ldata, cname);
1542 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1544 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1545 ldata->ld_leh->leh_len);
1548 th = dt_trans_create(env, dev);
1550 GOTO(unlock1, rc = PTR_ERR(th));
1552 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1553 XATTR_NAME_LINK, 0, th);
1557 rc = dt_trans_start_local(env, dev, th);
1561 dt_write_lock(env, obj, 0);
1562 if (unlikely(lfsck_is_dead_obj(obj)))
1563 GOTO(unlock2, rc = -ENOENT);
1565 rc = lfsck_links_read2(env, obj, &ldata_new);
1569 /* The specified linkEA entry has been removed by race. */
1570 rc = linkea_links_find(&ldata_new, cname, pfid);
1572 GOTO(unlock2, rc = 0);
1574 if (bk->lb_param & LPF_DRYRUN)
1575 GOTO(unlock2, rc = 1);
1578 linkea_del_buf(&ldata_new, cname);
1580 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1583 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1584 dt_write_unlock(env, obj);
1585 dt_trans_stop(env, dev, th);
1586 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1587 ldata_new.ld_leh->leh_len);
1591 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1592 ldata_new.ld_leh->leh_len);
1593 rc = dt_xattr_set(env, obj, &linkea_buf,
1594 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1596 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1599 dt_write_unlock(env, obj);
1602 dt_trans_stop(env, dev, th);
1605 lfsck_ibits_unlock(&lh, LCK_EX);
1608 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1609 "for the object: "DFID", parent "DFID", name %.*s\n",
1610 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1611 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1615 struct lfsck_namespace *ns = com->lc_file_ram;
1617 ns->ln_flags |= LF_INCONSISTENT;
1624 * Conditionally remove the specified entry from the linkEA.
1626 * Take the parent lock firstly, then check whether the specified
1627 * name entry exists or not: if yes, do nothing; otherwise, call
1628 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1630 * \param[in] env pointer to the thread context
1631 * \param[in] com pointer to the lfsck component
1632 * \param[in] parent pointer to the parent directory
1633 * \param[in] child pointer to the child object that holds the linkEA
1634 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1635 * \param[in] cname the name for the child in the parent directory
1636 * \param[in] pfid the parent directory's FID for the linkEA
1638 * \retval positive number for repaired cases
1639 * \retval 0 if nothing to be repaired
1640 * \retval negative error number on failure
1642 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1643 struct lfsck_component *com,
1644 struct dt_object *parent,
1645 struct dt_object *child,
1646 struct linkea_data *ldata,
1647 struct lu_name *cname,
1648 struct lu_fid *pfid)
1650 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1651 struct lustre_handle lh = { 0 };
1655 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1656 MDS_INODELOCK_UPDATE, LCK_EX);
1660 dt_read_lock(env, parent, 0);
1661 if (unlikely(lfsck_is_dead_obj(parent))) {
1662 dt_read_unlock(env, parent);
1663 lfsck_ibits_unlock(&lh, LCK_EX);
1664 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1670 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1671 (const struct dt_key *)cname->ln_name,
1673 dt_read_unlock(env, parent);
1675 /* It is safe to release the ldlm lock, because when the logic come
1676 * here, we have got all the needed information above whether the
1677 * linkEA entry is valid or not. It is not important that others
1678 * may add new linkEA entry after the ldlm lock released. If other
1679 * has removed the specified linkEA entry by race, then it is OK,
1680 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1682 lfsck_ibits_unlock(&lh, LCK_EX);
1683 if (rc == -ENOENT) {
1684 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1693 /* The LFSCK just found some internal status of cross-MDTs
1694 * create operation. That is normal. */
1695 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1696 linkea_next_entry(ldata);
1701 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1708 * Conditionally replace name entry in the parent.
1710 * As required, the LFSCK may re-create the lost MDT-object for dangling
1711 * name entry, but such repairing may be wrong because of bad FID in the
1712 * name entry. As the LFSCK processing, the real MDT-object may be found,
1713 * then the LFSCK should check whether the former re-created MDT-object
1714 * has been modified or not, if not, then destroy it and update the name
1715 * entry in the parent to reference the real MDT-object.
1717 * \param[in] env pointer to the thread context
1718 * \param[in] com pointer to the lfsck component
1719 * \param[in] parent pointer to the parent directory
1720 * \param[in] child pointer to the MDT-object that may be the real
1721 * MDT-object corresponding to the name entry in parent
1722 * \param[in] cfid the current FID in the name entry
1723 * \param[in] cname contains the name of the child in the parent directory
1725 * \retval positive number for repaired cases
1726 * \retval 0 if nothing to be repaired
1727 * \retval negative error number on failure
1729 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1730 struct lfsck_component *com,
1731 struct dt_object *parent,
1732 struct dt_object *child,
1733 const struct lu_fid *cfid,
1734 const struct lu_name *cname)
1736 struct lfsck_thread_info *info = lfsck_env_info(env);
1737 struct lu_attr *la = &info->lti_la;
1738 struct dt_insert_rec *rec = &info->lti_dt_rec;
1740 struct lfsck_instance *lfsck = com->lc_lfsck;
1741 struct dt_device *dev = lfsck->li_next;
1742 const char *name = cname->ln_name;
1743 struct dt_object *obj = NULL;
1744 struct lustre_handle plh = { 0 };
1745 struct lustre_handle clh = { 0 };
1746 struct linkea_data ldata = { NULL };
1747 struct thandle *th = NULL;
1752 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
1753 MDS_INODELOCK_UPDATE, LCK_EX);
1757 if (!fid_is_sane(cfid)) {
1762 obj = lfsck_object_find(env, lfsck, cfid);
1765 if (rc == -ENOENT) {
1773 if (!dt_object_exists(obj)) {
1778 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1779 (const struct dt_key *)name, BYPASS_CAPA);
1780 if (rc == -ENOENT) {
1788 /* Someone changed the name entry, cannot replace it. */
1789 if (!lu_fid_eq(cfid, &tfid))
1792 /* lock the object to be destroyed. */
1793 rc = lfsck_ibits_lock(env, lfsck, obj, &clh,
1794 MDS_INODELOCK_UPDATE |
1795 MDS_INODELOCK_XATTR, LCK_EX);
1799 if (unlikely(lfsck_is_dead_obj(obj))) {
1804 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
1808 /* The object has been modified by other(s), or it is not created by
1809 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1810 if (la->la_ctime != 0)
1813 if (S_ISREG(la->la_mode)) {
1814 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LOV,
1816 /* If someone has created related OST-object(s),
1818 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1819 GOTO(log, rc = (rc > 0 ? 0 : rc));
1823 dt_read_lock(env, child, 0);
1824 rc = lfsck_links_read2(env, child, &ldata);
1825 dt_read_unlock(env, child);
1827 /* Someone changed the child, no need to replace. */
1834 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(parent));
1835 /* Someone moved the child, no need to replace. */
1839 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1842 th = dt_trans_create(env, dev);
1844 GOTO(log, rc = PTR_ERR(th));
1847 rc = dt_declare_destroy(env, obj, th);
1852 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1856 rec->rec_type = S_IFDIR;
1857 rec->rec_fid = lfsck_dto2fid(child);
1858 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1859 (const struct dt_key *)name, th);
1863 rc = dt_trans_start(env, dev, th);
1868 rc = dt_destroy(env, obj, th);
1873 /* The old name entry maybe not exist. */
1874 dt_delete(env, parent, (const struct dt_key *)name, th,
1877 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1878 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1880 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1883 dt_trans_stop(env, dev, th);
1886 lfsck_ibits_unlock(&clh, LCK_EX);
1887 lfsck_ibits_unlock(&plh, LCK_EX);
1888 if (obj != NULL && !IS_ERR(obj))
1889 lfsck_object_put(env, obj);
1891 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1892 "object "DFID" because of conflict with the object "DFID
1893 " under the parent "DFID" with name %s: rc = %d\n",
1894 lfsck_lfsck2name(lfsck), PFID(cfid),
1895 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1902 * Overwrite the linkEA for the object with the given ldata.
1904 * The caller should take the ldlm lock before the calling.
1906 * \param[in] env pointer to the thread context
1907 * \param[in] com pointer to the lfsck component
1908 * \param[in] obj pointer to the dt_object to be handled
1909 * \param[in] ldata pointer to the new linkEA data
1911 * \retval positive number for repaired cases
1912 * \retval 0 if nothing to be repaired
1913 * \retval negative error number on failure
1915 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1916 struct lfsck_component *com,
1917 struct dt_object *obj,
1918 struct linkea_data *ldata)
1920 struct lfsck_instance *lfsck = com->lc_lfsck;
1921 struct dt_device *dev = lfsck->li_bottom;
1922 struct thandle *th = NULL;
1923 struct lu_buf linkea_buf;
1927 th = dt_trans_create(env, dev);
1929 GOTO(log, rc = PTR_ERR(th));
1931 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1932 ldata->ld_leh->leh_len);
1933 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1934 XATTR_NAME_LINK, 0, th);
1938 rc = dt_trans_start_local(env, dev, th);
1942 dt_write_lock(env, obj, 0);
1943 if (unlikely(lfsck_is_dead_obj(obj)))
1944 GOTO(unlock, rc = 0);
1946 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1947 GOTO(unlock, rc = 1);
1949 rc = dt_xattr_set(env, obj, &linkea_buf,
1950 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1952 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1955 dt_write_unlock(env, obj);
1958 dt_trans_stop(env, dev, th);
1961 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1962 "object "DFID": rc = %d\n",
1963 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1966 struct lfsck_namespace *ns = com->lc_file_ram;
1968 ns->ln_flags |= LF_INCONSISTENT;
1975 * Repair invalid name entry.
1977 * If the name entry contains invalid information, such as bad file type
1978 * or (and) corrupted object FID, then either remove the name entry or
1979 * udpate the name entry with the given (right) information.
1981 * \param[in] env pointer to the thread context
1982 * \param[in] com pointer to the lfsck component
1983 * \param[in] parent pointer to the parent directory
1984 * \param[in] child pointer to the object referenced by the name entry
1985 * \param[in] name the old name of the child under the parent directory
1986 * \param[in] name2 the new name of the child under the parent directory
1987 * \param[in] type the type claimed by the name entry
1988 * \param[in] update update the name entry if true; otherwise, remove it
1989 * \param[in] dec decrease the parent nlink count if true
1991 * \retval positive number for repaired successfully
1992 * \retval 0 if nothing to be repaired
1993 * \retval negative error number on failure
1995 int lfsck_namespace_repair_dirent(const struct lu_env *env,
1996 struct lfsck_component *com,
1997 struct dt_object *parent,
1998 struct dt_object *child,
1999 const char *name, const char *name2,
2000 __u16 type, bool update, bool dec)
2002 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
2003 const struct lu_fid *cfid = lfsck_dto2fid(child);
2005 struct lfsck_instance *lfsck = com->lc_lfsck;
2006 struct dt_device *dev = lfsck->li_next;
2007 struct thandle *th = NULL;
2008 struct lustre_handle lh = { 0 };
2012 if (unlikely(!dt_try_as_dir(env, parent)))
2013 GOTO(log, rc = -ENOTDIR);
2015 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2016 MDS_INODELOCK_UPDATE, LCK_EX);
2020 th = dt_trans_create(env, dev);
2022 GOTO(unlock1, rc = PTR_ERR(th));
2024 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2029 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2030 rec->rec_fid = cfid;
2031 rc = dt_declare_insert(env, parent,
2032 (const struct dt_rec *)rec,
2033 (const struct dt_key *)name2, th);
2039 rc = dt_declare_ref_del(env, parent, th);
2044 rc = dt_trans_start(env, dev, th);
2048 dt_write_lock(env, parent, 0);
2049 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2050 (const struct dt_key *)name, BYPASS_CAPA);
2051 /* Someone has removed the bad name entry by race. */
2053 GOTO(unlock2, rc = 0);
2058 /* Someone has removed the bad name entry and reused it for other
2059 * object by race. */
2060 if (!lu_fid_eq(&tfid, cfid))
2061 GOTO(unlock2, rc = 0);
2063 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2064 GOTO(unlock2, rc = 1);
2066 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2072 rc = dt_insert(env, parent,
2073 (const struct dt_rec *)rec,
2074 (const struct dt_key *)name2, th,
2081 rc = dt_ref_del(env, parent, th);
2086 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2089 dt_write_unlock(env, parent);
2092 dt_trans_stop(env, dev, th);
2094 /* We are not sure whether the child will become orphan or not.
2095 * Record it in the LFSCK trace file for further checking in
2096 * the second-stage scanning. */
2097 if (!update && !dec && rc == 0)
2098 lfsck_namespace_trace_update(env, com, cfid,
2099 LNTF_CHECK_LINKEA, true);
2102 lfsck_ibits_unlock(&lh, LCK_EX);
2105 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2106 "entry for: parent "DFID", child "DFID", name %s, type "
2107 "in name entry %o, type claimed by child %o. repair it "
2108 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2109 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2110 name, type, update ? lfsck_object_type(child) : 0,
2111 update ? "updating" : "removing", name2, rc);
2114 struct lfsck_namespace *ns = com->lc_file_ram;
2116 ns->ln_flags |= LF_INCONSISTENT;
2123 * Update the ".." name entry for the given object.
2125 * The object's ".." is corrupted, this function will update the ".." name
2126 * entry with the given pfid, and the linkEA with the given ldata.
2128 * The caller should take the ldlm lock before the calling.
2130 * \param[in] env pointer to the thread context
2131 * \param[in] com pointer to the lfsck component
2132 * \param[in] obj pointer to the dt_object to be handled
2133 * \param[in] pfid the new fid for the object's ".." name entry
2134 * \param[in] cname the name for the @obj in the parent directory
2136 * \retval positive number for repaired cases
2137 * \retval 0 if nothing to be repaired
2138 * \retval negative error number on failure
2140 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2141 struct lfsck_component *com,
2142 struct dt_object *obj,
2143 const struct lu_fid *pfid,
2144 struct lu_name *cname)
2146 struct lfsck_thread_info *info = lfsck_env_info(env);
2147 struct dt_insert_rec *rec = &info->lti_dt_rec;
2148 struct lfsck_instance *lfsck = com->lc_lfsck;
2149 struct dt_device *dev = lfsck->li_bottom;
2150 struct thandle *th = NULL;
2151 struct linkea_data ldata = { NULL };
2152 struct lu_buf linkea_buf;
2156 LASSERT(!dt_object_remote(obj));
2157 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2159 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2163 rc = linkea_add_buf(&ldata, cname, pfid);
2167 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2168 ldata.ld_leh->leh_len);
2170 th = dt_trans_create(env, dev);
2172 GOTO(log, rc = PTR_ERR(th));
2174 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2178 rec->rec_type = S_IFDIR;
2179 rec->rec_fid = pfid;
2180 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2181 (const struct dt_key *)dotdot, th);
2185 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2186 XATTR_NAME_LINK, 0, th);
2190 rc = dt_trans_start_local(env, dev, th);
2194 dt_write_lock(env, obj, 0);
2195 if (unlikely(lfsck_is_dead_obj(obj)))
2196 GOTO(unlock, rc = 0);
2198 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2199 GOTO(unlock, rc = 1);
2201 /* The old ".." name entry maybe not exist. */
2202 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2205 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2206 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2210 rc = dt_xattr_set(env, obj, &linkea_buf,
2211 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2213 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2216 dt_write_unlock(env, obj);
2219 dt_trans_stop(env, dev, th);
2222 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2223 "the object "DFID", new parent "DFID": rc = %d\n",
2224 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2228 struct lfsck_namespace *ns = com->lc_file_ram;
2230 ns->ln_flags |= LF_INCONSISTENT;
2237 * Handle orphan @obj during Double Scan Directory.
2239 * Remove the @obj's current (invalid) linkEA entries, and insert
2240 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2241 * ${FID}-${PFID}-D-${conflict_version}
2243 * The caller should take the ldlm lock before the calling.
2245 * \param[in] env pointer to the thread context
2246 * \param[in] com pointer to the lfsck component
2247 * \param[in] obj pointer to the orphan object to be handled
2248 * \param[in] pfid the new fid for the object's ".." name entry
2249 * \param[in,out] lh ldlm lock handler for the given @obj
2250 * \param[out] type to tell the caller what the inconsistency is
2252 * \retval positive number for repaired cases
2253 * \retval 0 if nothing to be repaired
2254 * \retval negative error number on failure
2257 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2258 struct lfsck_component *com,
2259 struct dt_object *obj,
2260 const struct lu_fid *pfid,
2261 struct lustre_handle *lh,
2262 enum lfsck_namespace_inconsistency_type *type)
2264 struct lfsck_thread_info *info = lfsck_env_info(env);
2265 struct lfsck_namespace *ns = com->lc_file_ram;
2269 /* Remove the unrecognized linkEA. */
2270 rc = lfsck_namespace_links_remove(env, com, obj);
2271 lfsck_ibits_unlock(lh, LCK_EX);
2272 if (rc < 0 && rc != -ENODATA)
2275 *type = LNIT_MUL_REF;
2277 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2278 * ever tried to verify some remote MDT-object that resides on this
2279 * MDT, but this MDT failed to respond such request. So means there
2280 * may be some remote name entry on other MDT that references this
2281 * object with another name, so we cannot know whether this linkEA
2282 * is valid or not. So keep it there and maybe resolved when next
2284 if (ns->ln_flags & LF_INCOMPLETE)
2287 /* The unique linkEA is invalid, even if the ".." name entry may be
2288 * valid, we still cannot know via which name entry this directory
2289 * will be referenced. Then handle it as pure orphan. */
2290 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2291 "-"DFID, PFID(pfid));
2292 rc = lfsck_namespace_insert_orphan(env, com, obj,
2293 info->lti_tmpbuf, "D", NULL);
2299 * Double Scan Directory object for single linkEA entry case.
2301 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2302 * then check whether the name is in the namespace or not, if not, add the
2303 * missing name entry back to namespace. If the linkEA entry is invalid,
2304 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2307 * \param[in] env pointer to the thread context
2308 * \param[in] com pointer to the lfsck component
2309 * \param[in] child pointer to the directory to be double scanned
2310 * \param[in] pfid the FID corresponding to the ".." entry
2311 * \param[in] ldata pointer to the linkEA data for the given @child
2312 * \param[in,out] lh ldlm lock handler for the given @child
2313 * \param[out] type to tell the caller what the inconsistency is
2314 * \param[in] retry if found inconsistency, but the caller does not hold
2315 * ldlm lock on the @child, then set @retry as true
2317 * \retval positive number for repaired cases
2318 * \retval 0 if nothing to be repaired
2319 * \retval negative error number on failure
2322 lfsck_namespace_dsd_single(const struct lu_env *env,
2323 struct lfsck_component *com,
2324 struct dt_object *child,
2325 const struct lu_fid *pfid,
2326 struct linkea_data *ldata,
2327 struct lustre_handle *lh,
2328 enum lfsck_namespace_inconsistency_type *type,
2331 struct lfsck_thread_info *info = lfsck_env_info(env);
2332 struct lu_name *cname = &info->lti_name;
2333 const struct lu_fid *cfid = lfsck_dto2fid(child);
2335 struct lfsck_namespace *ns = com->lc_file_ram;
2336 struct lfsck_instance *lfsck = com->lc_lfsck;
2337 struct dt_object *parent = NULL;
2338 struct lmv_mds_md_v1 *lmv;
2342 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid, info->lti_key);
2343 /* The unique linkEA entry with bad parent will be handled as orphan. */
2344 if (!fid_is_sane(&tfid)) {
2345 if (!lustre_handle_is_used(lh) && retry != NULL)
2348 rc = lfsck_namespace_dsd_orphan(env, com, child,
2354 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2356 GOTO(out, rc = PTR_ERR(parent));
2358 /* We trust the unique linkEA entry in spite of whether it matches the
2359 * ".." name entry or not. Because even if the linkEA entry is wrong
2360 * and the ".." name entry is right, we still cannot know via which
2361 * name entry the child will be referenced, since all known entries
2362 * have been verified during the first-stage scanning. */
2363 if (!dt_object_exists(parent)) {
2364 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2365 * has ever tried to verify some remote MDT-object that resides
2366 * on this MDT, but this MDT failed to respond such request. So
2367 * means there may be some remote name entry on other MDT that
2368 * references this object with another name, so we cannot know
2369 * whether this linkEA is valid or not. So keep it there and
2370 * maybe resolved when next LFSCK run. */
2371 if (ns->ln_flags & LF_INCOMPLETE)
2374 if (!lustre_handle_is_used(lh) && retry != NULL) {
2380 lfsck_ibits_unlock(lh, LCK_EX);
2383 lmv = &info->lti_lmv;
2384 rc = lfsck_read_stripe_lmv(env, child, lmv);
2385 if (rc != 0 && rc != -ENODATA)
2388 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2390 } else if (lfsck_shard_name_to_index(env,
2391 cname->ln_name, cname->ln_namelen,
2392 S_IFDIR, cfid) < 0) {
2393 /* It is an invalid name entry, we
2394 * cannot trust the parent also. */
2395 rc = lfsck_namespace_shrink_linkea(env, com, child,
2396 ldata, cname, &tfid, true);
2400 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2401 "-"DFID, PFID(pfid));
2402 rc = lfsck_namespace_insert_orphan(env, com, child,
2403 info->lti_tmpbuf, "S", NULL);
2408 /* Create the lost parent as an orphan. */
2409 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2411 /* Add the missing name entry to the parent. */
2412 rc = lfsck_namespace_insert_normal(env, com, parent,
2413 child, cname->ln_name);
2414 if (unlikely(rc == -EEXIST)) {
2415 /* Unfortunately, someone reused the name
2416 * under the parent by race. So we have
2417 * to remove the linkEA entry from
2418 * current child object. It means that the
2419 * LFSCK cannot recover the system
2420 * totally back to its original status,
2421 * but it is necessary to make the
2422 * current system to be consistent. */
2423 rc = lfsck_namespace_shrink_linkea(env,
2425 cname, &tfid, true);
2427 snprintf(info->lti_tmpbuf,
2428 sizeof(info->lti_tmpbuf),
2429 "-"DFID, PFID(pfid));
2430 rc = lfsck_namespace_insert_orphan(env,
2431 com, child, info->lti_tmpbuf,
2440 /* The unique linkEA entry with bad parent will be handled as orphan. */
2441 if (unlikely(!dt_try_as_dir(env, parent))) {
2442 if (!lustre_handle_is_used(lh) && retry != NULL)
2445 rc = lfsck_namespace_dsd_orphan(env, com, child,
2451 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2452 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2453 if (rc == -ENOENT) {
2454 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2455 * has ever tried to verify some remote MDT-object that resides
2456 * on this MDT, but this MDT failed to respond such request. So
2457 * means there may be some remote name entry on other MDT that
2458 * references this object with another name, so we cannot know
2459 * whether this linkEA is valid or not. So keep it there and
2460 * maybe resolved when next LFSCK run. */
2461 if (ns->ln_flags & LF_INCOMPLETE)
2464 if (!lustre_handle_is_used(lh) && retry != NULL) {
2470 lfsck_ibits_unlock(lh, LCK_EX);
2471 rc = lfsck_namespace_check_name(env, parent, child, cname);
2478 /* It is an invalid name entry, drop it. */
2479 if (unlikely(rc > 0)) {
2480 rc = lfsck_namespace_shrink_linkea(env, com, child,
2481 ldata, cname, &tfid, true);
2483 snprintf(info->lti_tmpbuf,
2484 sizeof(info->lti_tmpbuf),
2485 "-"DFID, PFID(pfid));
2486 rc = lfsck_namespace_insert_orphan(env, com,
2487 child, info->lti_tmpbuf, "D", NULL);
2493 /* Add the missing name entry back to the namespace. */
2494 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2496 if (unlikely(rc == -ESTALE))
2497 /* It may happen when the remote object has been
2498 * removed, but the local MDT is not aware of that. */
2501 if (unlikely(rc == -EEXIST)) {
2502 /* Unfortunately, someone reused the name under the
2503 * parent by race. So we have to remove the linkEA
2504 * entry from current child object. It means that the
2505 * LFSCK cannot recover the system totally back to
2506 * its original status, but it is necessary to make
2507 * the current system to be consistent.
2509 * It also may be because of the LFSCK found some
2510 * internal status of create operation. Under such
2511 * case, nothing to be done. */
2512 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2513 parent, child, ldata, cname, &tfid);
2515 snprintf(info->lti_tmpbuf,
2516 sizeof(info->lti_tmpbuf),
2517 "-"DFID, PFID(pfid));
2518 rc = lfsck_namespace_insert_orphan(env, com,
2519 child, info->lti_tmpbuf, "D", NULL);
2529 if (!lu_fid_eq(&tfid, cfid)) {
2530 if (!lustre_handle_is_used(lh) && retry != NULL) {
2536 lfsck_ibits_unlock(lh, LCK_EX);
2537 /* The name entry references another MDT-object that
2538 * may be created by the LFSCK for repairing dangling
2539 * name entry. Try to replace it. */
2540 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2543 rc = lfsck_namespace_dsd_orphan(env, com, child,
2549 if (fid_is_zero(pfid))
2552 /* The ".." name entry is wrong, update it. */
2553 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2554 if (!lustre_handle_is_used(lh) && retry != NULL) {
2560 *type = LNIT_UNMATCHED_PAIRS;
2561 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2562 lfsck_dto2fid(parent), cname);
2568 if (parent != NULL && !IS_ERR(parent))
2569 lfsck_object_put(env, parent);
2575 * Double Scan Directory object for multiple linkEA entries case.
2577 * The given @child has multiple linkEA entries. There is at most one linkEA
2578 * entry will be valid, all the others will be removed. Firstly, the function
2579 * will try to find out the linkEA entry for which the name entry exists under
2580 * the given parent (@pfid). If there is no linkEA entry that matches the given
2581 * ".." name entry, then tries to find out the first linkEA entry that both the
2582 * parent and the name entry exist to rebuild a new ".." name entry.
2584 * \param[in] env pointer to the thread context
2585 * \param[in] com pointer to the lfsck component
2586 * \param[in] child pointer to the directory to be double scanned
2587 * \param[in] pfid the FID corresponding to the ".." entry
2588 * \param[in] ldata pointer to the linkEA data for the given @child
2589 * \param[in,out] lh ldlm lock handler for the given @child
2590 * \param[out] type to tell the caller what the inconsistency is
2591 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2593 * \retval positive number for repaired cases
2594 * \retval 0 if nothing to be repaired
2595 * \retval negative error number on failure
2598 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2599 struct lfsck_component *com,
2600 struct dt_object *child,
2601 const struct lu_fid *pfid,
2602 struct linkea_data *ldata,
2603 struct lustre_handle *lh,
2604 enum lfsck_namespace_inconsistency_type *type,
2607 struct lfsck_thread_info *info = lfsck_env_info(env);
2608 struct lu_name *cname = &info->lti_name;
2609 const struct lu_fid *cfid = lfsck_dto2fid(child);
2610 struct lu_fid *pfid2 = &info->lti_fid3;
2612 struct lfsck_namespace *ns = com->lc_file_ram;
2613 struct lfsck_instance *lfsck = com->lc_lfsck;
2614 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2615 struct dt_object *parent = NULL;
2616 struct linkea_data ldata_new = { NULL };
2617 int dirent_count = 0;
2618 int linkea_count = 0;
2624 while (ldata->ld_lee != NULL) {
2625 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2627 /* Drop repeated linkEA entries. */
2628 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2629 /* Drop invalid linkEA entry. */
2630 if (!fid_is_sane(&tfid)) {
2631 linkea_del_buf(ldata, cname);
2636 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2637 * then it is possible that: the directry object has ever
2638 * been lost, but its name entry was there. In the former
2639 * LFSCK run, during the first-stage scanning, the LFSCK
2640 * found the dangling name entry, but it did not recreate
2641 * the lost object, and when moved to the second-stage
2642 * scanning, some children objects of the lost directory
2643 * object were found, then the LFSCK recreated such lost
2644 * directory object as an orphan.
2646 * When the LFSCK runs again, if the dangling name is still
2647 * there, the LFSCK should move the orphan directory object
2648 * back to the normal namespace. */
2649 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2650 linkea_next_entry(ldata);
2654 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2656 RETURN(PTR_ERR(parent));
2658 if (!dt_object_exists(parent)) {
2659 lfsck_object_put(env, parent);
2660 if (ldata->ld_leh->leh_reccount > 1) {
2661 /* If it is NOT the last linkEA entry, then
2662 * there is still other chance to make the
2663 * child to be visible via other parent, then
2664 * remove this linkEA entry. */
2665 linkea_del_buf(ldata, cname);
2673 /* The linkEA entry with bad parent will be removed. */
2674 if (unlikely(!dt_try_as_dir(env, parent))) {
2675 lfsck_object_put(env, parent);
2676 linkea_del_buf(ldata, cname);
2681 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2682 (const struct dt_key *)cname->ln_name,
2684 *pfid2 = *lfsck_dto2fid(parent);
2685 if (rc == -ENOENT) {
2686 lfsck_object_put(env, parent);
2687 linkea_next_entry(ldata);
2692 lfsck_object_put(env, parent);
2697 if (lu_fid_eq(&tfid, cfid)) {
2698 lfsck_object_put(env, parent);
2699 if (!lu_fid_eq(pfid, pfid2)) {
2700 *type = LNIT_UNMATCHED_PAIRS;
2701 rc = lfsck_namespace_repair_unmatched_pairs(env,
2702 com, child, pfid2, cname);
2708 /* It is the most common case that we find the
2709 * name entry corresponding to the linkEA entry
2710 * that matches the ".." name entry. */
2711 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2715 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2719 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2724 linkea_del_buf(ldata, cname);
2726 linkea_first_entry(ldata);
2727 /* There may be some invalid dangling name entries under
2728 * other parent directories, remove all of them. */
2729 while (ldata->ld_lee != NULL) {
2730 lfsck_namespace_unpack_linkea_entry(ldata,
2731 cname, &tfid, info->lti_key);
2732 if (!fid_is_sane(&tfid))
2735 parent = lfsck_object_find_bottom(env, lfsck,
2737 if (IS_ERR(parent)) {
2738 rc = PTR_ERR(parent);
2739 if (rc != -ENOENT &&
2740 bk->lb_param & LPF_FAILOUT)
2746 if (!dt_object_exists(parent)) {
2747 lfsck_object_put(env, parent);
2751 rc = lfsck_namespace_repair_dirent(env, com,
2752 parent, child, cname->ln_name,
2753 cname->ln_name, S_IFDIR, false, true);
2754 lfsck_object_put(env, parent);
2756 if (bk->lb_param & LPF_FAILOUT)
2765 linkea_del_buf(ldata, cname);
2768 ns->ln_dirent_repaired += dirent_count;
2773 lfsck_ibits_unlock(lh, LCK_EX);
2774 /* The name entry references another MDT-object that may be
2775 * created by the LFSCK for repairing dangling name entry.
2776 * Try to replace it. */
2777 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2779 lfsck_object_put(env, parent);
2786 linkea_del_buf(ldata, cname);
2789 linkea_first_entry(ldata);
2790 if (ldata->ld_leh->leh_reccount == 1) {
2791 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2794 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2795 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2801 /* All linkEA entries are invalid and removed, then handle the @child
2803 if (ldata->ld_leh->leh_reccount == 0) {
2804 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2810 /* If the dangling name entry for the orphan directory object has
2811 * been remvoed, then just check whether the directory object is
2812 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2818 /* There is no linkEA entry that matches the ".." name entry. Find
2819 * the first linkEA entry that both parent and name entry exist to
2820 * rebuild a new ".." name entry. */
2830 * Repair the object's nlink attribute.
2832 * If all the known name entries have been verified, then the object's hard
2833 * link attribute should match the object's linkEA entries count unless the
2834 * object's has too much hard link to be recorded in the linkEA. Such cases
2835 * should have been marked in the LFSCK trace file. Otherwise, trust the
2836 * linkEA to update the object's nlink attribute.
2838 * \param[in] env pointer to the thread context
2839 * \param[in] com pointer to the lfsck component
2840 * \param[in] obj pointer to the dt_object to be handled
2841 * \param[in,out] la pointer to buffer to object's attribute before
2842 * and after the repairing
2844 * \retval positive number for repaired cases
2845 * \retval 0 if nothing to be repaired
2846 * \retval negative error number on failure
2848 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2849 struct lfsck_component *com,
2850 struct dt_object *obj,
2853 struct lfsck_thread_info *info = lfsck_env_info(env);
2854 struct lu_fid *tfid = &info->lti_fid3;
2855 struct lfsck_namespace *ns = com->lc_file_ram;
2856 struct lfsck_instance *lfsck = com->lc_lfsck;
2857 struct dt_device *dev = lfsck->li_bottom;
2858 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2859 struct dt_object *child = NULL;
2860 struct thandle *th = NULL;
2861 struct linkea_data ldata = { NULL };
2862 struct lustre_handle lh = { 0 };
2863 __u32 old = la->la_nlink;
2869 LASSERT(!dt_object_remote(obj));
2870 LASSERT(S_ISREG(lfsck_object_type(obj)));
2872 child = lfsck_object_find_by_dev(env, dev, cfid);
2874 GOTO(log, rc = PTR_ERR(child));
2876 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
2877 MDS_INODELOCK_UPDATE |
2878 MDS_INODELOCK_XATTR, LCK_EX);
2882 th = dt_trans_create(env, dev);
2884 GOTO(log, rc = PTR_ERR(th));
2886 la->la_valid = LA_NLINK;
2887 rc = dt_declare_attr_set(env, child, la, th);
2891 rc = dt_trans_start_local(env, dev, th);
2895 dt_write_lock(env, child, 0);
2896 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2897 * ever tried to verify some remote MDT-object that resides on this
2898 * MDT, but this MDT failed to respond such request. So means there
2899 * may be some remote name entry on other MDT that references this
2900 * object with another name, so we cannot know whether this linkEA
2901 * is valid or not. So keep it there and maybe resolved when next
2903 if (ns->ln_flags & LF_INCOMPLETE)
2904 GOTO(unlock, rc = 0);
2906 fid_cpu_to_be(tfid, cfid);
2907 idx = lfsck_sub_trace_file_fid2idx(cfid);
2908 rc = dt_lookup(env, com->lc_sub_trace_objs[idx].lsto_obj,
2909 (struct dt_rec *)&flags, (const struct dt_key *)tfid,
2914 if (flags & LNTF_SKIP_NLINK)
2915 GOTO(unlock, rc = 0);
2917 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
2919 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2921 rc = lfsck_links_read2(env, child, &ldata);
2923 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2925 if (la->la_nlink == ldata.ld_leh->leh_reccount ||
2926 unlikely(la->la_nlink == 0))
2927 GOTO(unlock, rc = 0);
2929 la->la_nlink = ldata.ld_leh->leh_reccount;
2930 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2931 GOTO(unlock, rc = 1);
2933 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
2935 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2938 dt_write_unlock(env, child);
2941 dt_trans_stop(env, dev, th);
2944 lfsck_ibits_unlock(&lh, LCK_EX);
2945 if (child != NULL && !IS_ERR(child))
2946 lfsck_object_put(env, child);
2948 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2949 "nlink count from %u to %u: rc = %d\n",
2950 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2953 ns->ln_flags |= LF_INCONSISTENT;
2959 * Double scan the directory object for namespace LFSCK.
2961 * This function will verify the <parent, child> pairs in the namespace tree:
2962 * the parent references the child via some name entry that should be in the
2963 * child's linkEA entry, the child should back references the parent via its
2966 * The LFSCK will scan every linkEA entry in turn until find out the first
2967 * matched pairs. If found, then all other linkEA entries will be dropped.
2968 * If all the linkEA entries cannot match the ".." name entry, then there
2969 * are serveral possible cases:
2971 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2972 * in the linkEA entry is valid.
2974 * 2) If there are multiple linkEA entries, then try to find the linkEA
2975 * that matches the ".." name entry. If found, then all other entries
2976 * are invalid; otherwise, it is quite possible that the ".." name entry
2977 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2978 * entry according to the first valid linkEA entry (both the parent and
2979 * the name entry should exist).
2981 * 3) If the directory object has no (valid) linkEA entry, then the
2982 * directory object will be handled as pure orphan and inserted
2983 * in the .lustre/lost+found/MDTxxxx/ with the name:
2984 * ${self_FID}-${PFID}-D-${conflict_version}
2986 * \param[in] env pointer to the thread context
2987 * \param[in] com pointer to the lfsck component
2988 * \param[in] child pointer to the directory object to be handled
2989 * \param[in] flags to indicate the specical checking on the @child
2991 * \retval positive number for repaired cases
2992 * \retval 0 if nothing to be repaired
2993 * \retval negative error number on failure
2995 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
2996 struct lfsck_component *com,
2997 struct dt_object *child, __u8 flags)
2999 struct lfsck_thread_info *info = lfsck_env_info(env);
3000 const struct lu_fid *cfid = lfsck_dto2fid(child);
3001 struct lu_fid *pfid = &info->lti_fid2;
3002 struct lfsck_namespace *ns = com->lc_file_ram;
3003 struct lfsck_instance *lfsck = com->lc_lfsck;
3004 struct lustre_handle lh = { 0 };
3005 struct linkea_data ldata = { NULL };
3006 bool unknown = false;
3009 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3013 LASSERT(!dt_object_remote(child));
3015 if (flags & LNTF_UNCERTAIN_LMV) {
3016 if (flags & LNTF_RECHECK_NAME_HASH) {
3017 rc = lfsck_namespace_scan_shard(env, com, child);
3021 ns->ln_striped_shards_scanned++;
3023 ns->ln_striped_shards_skipped++;
3027 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3031 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3032 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3033 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3034 "the namespace LFSCK, then the LFSCK cannot guarantee"
3035 "all the name entries have been verified in first-stage"
3036 "scanning. So have to skip orphan related handling for"
3037 "the directory object "DFID" with remote name entry\n",
3038 lfsck_lfsck2name(lfsck), PFID(cfid));
3043 if (unlikely(!dt_try_as_dir(env, child)))
3044 GOTO(out, rc = -ENOTDIR);
3046 /* We only take ldlm lock on the @child when required. When the
3047 * logic comes here for the first time, it is always false. */
3051 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3052 MDS_INODELOCK_UPDATE |
3053 MDS_INODELOCK_XATTR, LCK_EX);
3058 dt_read_lock(env, child, 0);
3059 if (unlikely(lfsck_is_dead_obj(child))) {
3060 dt_read_unlock(env, child);
3065 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3066 (const struct dt_key *)dotdot, BYPASS_CAPA);
3068 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3069 dt_read_unlock(env, child);
3074 if (!lustre_handle_is_used(&lh)) {
3075 dt_read_unlock(env, child);
3080 } else if (lfsck->li_lpf_obj != NULL &&
3081 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3083 } else if (unlikely(!fid_is_sane(pfid))) {
3087 rc = lfsck_links_read(env, child, &ldata);
3088 dt_read_unlock(env, child);
3090 if (rc != -ENODATA && rc != -EINVAL)
3093 if (!lustre_handle_is_used(&lh))
3096 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3097 /* Remove the corrupted linkEA. */
3098 rc = lfsck_namespace_links_remove(env, com, child);
3100 /* Here, because of the crashed linkEA, we
3101 * cannot know whether there is some parent
3102 * that references the child directory via
3103 * some name entry or not. So keep it there,
3104 * when the LFSCK run next time, if there is
3105 * some parent that references this object,
3106 * then the LFSCK can rebuild the linkEA;
3107 * otherwise, this object will be handled
3108 * as orphan as above. */
3111 /* 1. If we have neither ".." nor linkEA,
3112 * then it is an orphan.
3114 * 2. If we only have the ".." name entry,
3115 * but no parent references this child
3116 * directory, then handle it as orphan. */
3117 lfsck_ibits_unlock(&lh, LCK_EX);
3118 type = LNIT_MUL_REF;
3120 /* If the LFSCK is marked as LF_INCOMPLETE,
3121 * then means some MDT has ever tried to
3122 * verify some remote MDT-object that resides
3123 * on this MDT, but this MDT failed to respond
3124 * such request. So means there may be some
3125 * remote name entry on other MDT that
3126 * references this object with another name,
3127 * so we cannot know whether this linkEA is
3128 * valid or not. So keep it there and maybe
3129 * resolved when next LFSCK run. */
3130 if (ns->ln_flags & LF_INCOMPLETE)
3133 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3134 "-"DFID, PFID(pfid));
3135 rc = lfsck_namespace_insert_orphan(env, com, child,
3136 info->lti_tmpbuf, "D", NULL);
3142 linkea_first_entry(&ldata);
3143 /* This is the most common case: the object has unique linkEA entry. */
3144 if (ldata.ld_leh->leh_reccount == 1) {
3145 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3146 &lh, &type, &retry);
3148 LASSERT(!lustre_handle_is_used(&lh));
3157 if (!lustre_handle_is_used(&lh))
3160 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3161 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3167 /* When we come here, the cases usually like that:
3168 * 1) The directory object has a corrupted linkEA entry. During the
3169 * first-stage scanning, the LFSCK cannot know such corruption,
3170 * then it appends the right linkEA entry according to the found
3171 * name entry after the bad one.
3173 * 2) The directory object has a right linkEA entry. During the
3174 * first-stage scanning, the LFSCK finds some bad name entry,
3175 * but the LFSCK cannot aware that at that time, then it adds
3176 * the bad linkEA entry for further processing. */
3177 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3183 lfsck_ibits_unlock(&lh, LCK_EX);
3186 case LNIT_BAD_LINKEA:
3187 ns->ln_linkea_repaired++;
3189 case LNIT_UNMATCHED_PAIRS:
3190 ns->ln_unmatched_pairs_repaired++;
3193 ns->ln_mul_ref_repaired++;
3201 ns->ln_unknown_inconsistency++;
3207 * Double scan the MDT-object for namespace LFSCK.
3209 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3210 * those entries from the linkEA; if the linkEA becomes empty or the object
3211 * has no linkEA, then it is an orphan and will be added into the directory
3212 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3213 * the remote parent; if the name entry corresponding to some linkEA entry
3214 * is lost, then add the name entry back to the namespace.
3216 * \param[in] env pointer to the thread context
3217 * \param[in] com pointer to the lfsck component
3218 * \param[in] child pointer to the dt_object to be handled
3219 * \param[in] flags some hints to indicate how the @child should be handled
3221 * \retval positive number for repaired cases
3222 * \retval 0 if nothing to be repaired
3223 * \retval negative error number on failure
3225 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3226 struct lfsck_component *com,
3227 struct dt_object *child, __u8 flags)
3229 struct lfsck_thread_info *info = lfsck_env_info(env);
3230 struct lu_attr *la = &info->lti_la;
3231 struct lu_name *cname = &info->lti_name;
3232 struct lu_fid *pfid = &info->lti_fid;
3233 struct lu_fid *cfid = &info->lti_fid2;
3234 struct lfsck_instance *lfsck = com->lc_lfsck;
3235 struct lfsck_namespace *ns = com->lc_file_ram;
3236 struct dt_object *parent = NULL;
3237 struct linkea_data ldata = { NULL };
3238 bool repaired = false;
3243 dt_read_lock(env, child, 0);
3244 if (unlikely(lfsck_is_dead_obj(child))) {
3245 dt_read_unlock(env, child);
3250 if (S_ISDIR(lfsck_object_type(child))) {
3251 dt_read_unlock(env, child);
3252 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3257 rc = lfsck_links_read(env, child, &ldata);
3258 dt_read_unlock(env, child);
3262 linkea_first_entry(&ldata);
3263 while (ldata.ld_lee != NULL) {
3264 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3266 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3268 /* Found repeated linkEA entries */
3270 rc = lfsck_namespace_shrink_linkea(env, com, child,
3271 &ldata, cname, pfid, false);
3283 /* Invalid PFID in the linkEA entry. */
3284 if (!fid_is_sane(pfid)) {
3285 rc = lfsck_namespace_shrink_linkea(env, com, child,
3286 &ldata, cname, pfid, true);
3296 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3298 GOTO(out, rc = PTR_ERR(parent));
3300 if (!dt_object_exists(parent)) {
3303 if (ldata.ld_leh->leh_reccount > 1) {
3304 /* If it is NOT the last linkEA entry, then
3305 * there is still other chance to make the
3306 * child to be visible via other parent, then
3307 * remove this linkEA entry. */
3308 rc = lfsck_namespace_shrink_linkea(env, com,
3309 child, &ldata, cname, pfid, true);
3311 /* If the LFSCK is marked as LF_INCOMPLETE,
3312 * then means some MDT has ever tried to
3313 * verify some remote MDT-object that resides
3314 * on this MDT, but this MDT failed to respond
3315 * such request. So means there may be some
3316 * remote name entry on other MDT that
3317 * references this object with another name,
3318 * so we cannot know whether this linkEA is
3319 * valid or not. So keep it there and maybe
3320 * resolved when next LFSCK run. */
3321 if (ns->ln_flags & LF_INCOMPLETE) {
3322 lfsck_object_put(env, parent);
3327 /* Create the lost parent as an orphan. */
3328 rc = lfsck_namespace_create_orphan_dir(env, com,
3331 lfsck_object_put(env, parent);
3339 /* Add the missing name entry to the parent. */
3340 rc = lfsck_namespace_insert_normal(env, com,
3341 parent, child, cname->ln_name);
3342 if (unlikely(rc == -EEXIST))
3343 /* Unfortunately, someone reused the
3344 * name under the parent by race. So we
3345 * have to remove the linkEA entry from
3346 * current child object. It means that
3347 * the LFSCK cannot recover the system
3348 * totally back to its original status,
3349 * but it is necessary to make the
3350 * current system to be consistent. */
3351 rc = lfsck_namespace_shrink_linkea(env,
3355 linkea_next_entry(&ldata);
3358 lfsck_object_put(env, parent);
3368 /* The linkEA entry with bad parent will be removed. */
3369 if (unlikely(!dt_try_as_dir(env, parent))) {
3370 lfsck_object_put(env, parent);
3371 rc = lfsck_namespace_shrink_linkea(env, com, child,
3372 &ldata, cname, pfid, true);
3382 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3383 (const struct dt_key *)cname->ln_name,
3385 if (rc != 0 && rc != -ENOENT) {
3386 lfsck_object_put(env, parent);
3392 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3393 /* It is the most common case that we
3394 * find the name entry corresponding
3395 * to the linkEA entry. */
3396 lfsck_object_put(env, parent);
3397 linkea_next_entry(&ldata);
3399 /* The name entry references another
3400 * MDT-object that may be created by
3401 * the LFSCK for repairing dangling
3402 * name entry. Try to replace it. */
3403 rc = lfsck_namespace_replace_cond(env, com,
3404 parent, child, cfid, cname);
3405 lfsck_object_put(env, parent);
3411 linkea_next_entry(&ldata);
3413 rc = lfsck_namespace_shrink_linkea(env,
3427 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3431 /* If there is no name entry in the parent dir and the object
3432 * link count is less than the linkea entries count, then the
3433 * linkea entry should be removed. */
3434 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3435 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3436 parent, child, &ldata, cname, pfid);
3437 lfsck_object_put(env, parent);
3447 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3448 * MDT has ever tried to verify some remote MDT-object that
3449 * resides on this MDT, but this MDT failed to respond such
3450 * request. So means there may be some remote name entry on
3451 * other MDT that references this object with another name,
3452 * so we cannot know whether this linkEA is valid or not.
3453 * So keep it there and maybe resolved when next LFSCK run. */
3454 if (ns->ln_flags & LF_INCOMPLETE) {
3455 lfsck_object_put(env, parent);
3460 rc = lfsck_namespace_check_name(env, parent, child, cname);
3465 lfsck_object_put(env, parent);
3470 /* It is an invalid name entry, drop it. */
3471 if (unlikely(rc > 0)) {
3472 lfsck_object_put(env, parent);
3473 rc = lfsck_namespace_shrink_linkea(env, com, child,
3474 &ldata, cname, pfid, true);
3484 /* Add the missing name entry back to the namespace. */
3485 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3487 if (unlikely(rc == -ESTALE))
3488 /* It may happen when the remote object has been
3489 * removed, but the local MDT is not aware of that. */
3492 if (unlikely(rc == -EEXIST))
3493 /* Unfortunately, someone reused the name under the
3494 * parent by race. So we have to remove the linkEA
3495 * entry from current child object. It means that the
3496 * LFSCK cannot recover the system totally back to
3497 * its original status, but it is necessary to make
3498 * the current system to be consistent.
3500 * It also may be because of the LFSCK found some
3501 * internal status of create operation. Under such
3502 * case, nothing to be done. */
3503 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3504 parent, child, &ldata, cname, pfid);
3506 linkea_next_entry(&ldata);
3508 lfsck_object_put(env, parent);
3519 if (rc < 0 && rc != -ENODATA)
3523 LASSERT(ldata.ld_leh != NULL);
3525 count = ldata.ld_leh->leh_reccount;
3529 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3530 * MDT has ever tried to verify some remote MDT-object that
3531 * resides on this MDT, but this MDT failed to respond such
3532 * request. So means there may be some remote name entry on
3533 * other MDT that references this object with another name,
3534 * so we cannot know whether this linkEA is valid or not.
3535 * So keep it there and maybe resolved when next LFSCK run. */
3536 if (!(ns->ln_flags & LF_INCOMPLETE)) {
3537 /* If the child becomes orphan, then insert it into
3538 * the global .lustre/lost+found/MDTxxxx directory. */
3539 rc = lfsck_namespace_insert_orphan(env, com, child,
3545 ns->ln_mul_ref_repaired++;
3550 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3554 if (la->la_nlink != 0 && la->la_nlink != count) {
3555 rc = lfsck_namespace_repair_nlink(env, com, child, la);
3557 ns->ln_objs_nlink_repaired++;
3564 if (la->la_nlink > 1)
3565 ns->ln_mul_linked_repaired++;
3574 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3575 struct lfsck_namespace *ns,
3576 __u64 checked_phase1,
3577 __u64 checked_phase2,
3581 seq_printf(m, "checked_phase1: "LPU64"\n"
3582 "checked_phase2: "LPU64"\n"
3583 "updated_phase1: "LPU64"\n"
3584 "updated_phase2: "LPU64"\n"
3585 "failed_phase1: "LPU64"\n"
3586 "failed_phase2: "LPU64"\n"
3587 "directories: "LPU64"\n"
3588 "dirent_repaired: "LPU64"\n"
3589 "linkea_repaired: "LPU64"\n"
3590 "nlinks_repaired: "LPU64"\n"
3591 "multiple_linked_checked: "LPU64"\n"
3592 "multiple_linked_repaired: "LPU64"\n"
3593 "unknown_inconsistency: "LPU64"\n"
3594 "unmatched_pairs_repaired: "LPU64"\n"
3595 "dangling_repaired: "LPU64"\n"
3596 "multiple_referenced_repaired: "LPU64"\n"
3597 "bad_file_type_repaired: "LPU64"\n"
3598 "lost_dirent_repaired: "LPU64"\n"
3599 "local_lost_found_scanned: "LPU64"\n"
3600 "local_lost_found_moved: "LPU64"\n"
3601 "local_lost_found_skipped: "LPU64"\n"
3602 "local_lost_found_failed: "LPU64"\n"
3603 "striped_dirs_scanned: "LPU64"\n"
3604 "striped_dirs_repaired: "LPU64"\n"
3605 "striped_dirs_failed: "LPU64"\n"
3606 "striped_dirs_disabled: "LPU64"\n"
3607 "striped_dirs_skipped: "LPU64"\n"
3608 "striped_shards_scanned: "LPU64"\n"
3609 "striped_shards_repaired: "LPU64"\n"
3610 "striped_shards_failed: "LPU64"\n"
3611 "striped_shards_skipped: "LPU64"\n"
3612 "name_hash_repaired: "LPU64"\n"
3613 "success_count: %u\n"
3614 "run_time_phase1: %u seconds\n"
3615 "run_time_phase2: %u seconds\n",
3618 ns->ln_items_repaired,
3619 ns->ln_objs_repaired_phase2,
3620 ns->ln_items_failed,
3621 ns->ln_objs_failed_phase2,
3622 ns->ln_dirs_checked,
3623 ns->ln_dirent_repaired,
3624 ns->ln_linkea_repaired,
3625 ns->ln_objs_nlink_repaired,
3626 ns->ln_mul_linked_checked,
3627 ns->ln_mul_linked_repaired,
3628 ns->ln_unknown_inconsistency,
3629 ns->ln_unmatched_pairs_repaired,
3630 ns->ln_dangling_repaired,
3631 ns->ln_mul_ref_repaired,
3632 ns->ln_bad_type_repaired,
3633 ns->ln_lost_dirent_repaired,
3634 ns->ln_local_lpf_scanned,
3635 ns->ln_local_lpf_moved,
3636 ns->ln_local_lpf_skipped,
3637 ns->ln_local_lpf_failed,
3638 ns->ln_striped_dirs_scanned,
3639 ns->ln_striped_dirs_repaired,
3640 ns->ln_striped_dirs_failed,
3641 ns->ln_striped_dirs_disabled,
3642 ns->ln_striped_dirs_skipped,
3643 ns->ln_striped_shards_scanned,
3644 ns->ln_striped_shards_repaired,
3645 ns->ln_striped_shards_failed,
3646 ns->ln_striped_shards_skipped,
3647 ns->ln_name_hash_repaired,
3648 ns->ln_success_count,
3653 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3654 struct lfsck_component *com)
3656 struct lfsck_instance *lfsck = com->lc_lfsck;
3657 struct lfsck_namespace *ns = com->lc_file_ram;
3659 while (!list_empty(&lfsck->li_list_lmv)) {
3660 struct lfsck_lmv_unit *llu;
3661 struct lfsck_lmv *llmv;
3663 llu = list_entry(lfsck->li_list_lmv.next,
3664 struct lfsck_lmv_unit, llu_link);
3665 llmv = &llu->llu_lmv;
3667 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3668 "still in using: %u\n",
3669 atomic_read(&llmv->ll_ref));
3671 ns->ln_striped_dirs_skipped++;
3672 lfsck_lmv_put(env, llmv);
3676 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3677 struct lfsck_component *com,
3678 struct dt_object *obj)
3680 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3683 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3687 /* zero-linkEA object may be orphan, but it also maybe because
3688 * of upgrading. Currently, we cannot record it for double scan.
3689 * Because it may cause the LFSCK trace file to be too large. */
3691 /* "la_ctime" == 1 means that it has ever been removed from
3692 * backend /lost+found directory but not been added back to
3693 * the normal namespace yet. */
3695 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3696 unlikely(la->la_ctime == 1))
3697 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3698 LNTF_CHECK_LINKEA, true);
3703 /* namespace APIs */
3705 static int lfsck_namespace_reset(const struct lu_env *env,
3706 struct lfsck_component *com, bool init)
3708 struct lfsck_instance *lfsck = com->lc_lfsck;
3709 struct lfsck_namespace *ns = com->lc_file_ram;
3710 struct lfsck_assistant_data *lad = com->lc_data;
3711 struct dt_object *root;
3712 struct dt_object *dto;
3716 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3718 GOTO(log, rc = PTR_ERR(root));
3720 if (unlikely(!dt_try_as_dir(env, root)))
3721 GOTO(put, rc = -ENOTDIR);
3723 down_write(&com->lc_sem);
3725 memset(ns, 0, sizeof(*ns));
3727 __u32 count = ns->ln_success_count;
3728 __u64 last_time = ns->ln_time_last_complete;
3730 memset(ns, 0, sizeof(*ns));
3731 ns->ln_success_count = count;
3732 ns->ln_time_last_complete = last_time;
3734 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3735 ns->ln_status = LS_INIT;
3737 lfsck_object_put(env, com->lc_obj);
3739 dto = lfsck_namespace_load_one_trace_file(env, com, root,
3740 LFSCK_NAMESPACE, NULL, true);
3742 GOTO(out, rc = PTR_ERR(dto));
3745 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3749 lad->lad_incomplete = 0;
3750 CFS_RESET_BITMAP(lad->lad_bitmap);
3752 rc = lfsck_namespace_store(env, com, true);
3757 up_write(&com->lc_sem);
3760 lu_object_put(env, &root->do_lu);
3762 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3763 lfsck_lfsck2name(lfsck), rc);
3768 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3771 struct lfsck_namespace *ns = com->lc_file_ram;
3773 down_write(&com->lc_sem);
3775 com->lc_new_checked++;
3776 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3777 up_write(&com->lc_sem);
3780 static void lfsck_namespace_close_dir(const struct lu_env *env,
3781 struct lfsck_component *com)
3783 struct lfsck_namespace *ns = com->lc_file_ram;
3784 struct lfsck_assistant_data *lad = com->lc_data;
3785 struct lfsck_instance *lfsck = com->lc_lfsck;
3786 struct lfsck_lmv *llmv = lfsck->li_lmv;
3787 struct lfsck_namespace_req *lnr;
3789 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3790 bool wakeup = false;
3796 OBD_ALLOC(lnr, size);
3798 ns->ln_striped_dirs_skipped++;
3803 /* Generate a dummy request to indicate that all shards' name entry
3804 * in this striped directory has been scanned for the first time. */
3805 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3806 lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3807 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3808 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3809 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3810 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3811 lnr->lnr_size = size;
3813 spin_lock(&lad->lad_lock);
3814 if (lad->lad_assistant_status < 0) {
3815 spin_unlock(&lad->lad_lock);
3816 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3817 ns->ln_striped_dirs_skipped++;
3822 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3823 if (lad->lad_prefetched == 0)
3826 lad->lad_prefetched++;
3827 spin_unlock(&lad->lad_lock);
3829 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3834 static int lfsck_namespace_open_dir(const struct lu_env *env,
3835 struct lfsck_component *com)
3837 struct lfsck_instance *lfsck = com->lc_lfsck;
3838 struct lfsck_namespace *ns = com->lc_file_ram;
3839 struct lfsck_lmv *llmv = lfsck->li_lmv;
3846 if (llmv->ll_lmv_master) {
3847 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3849 if (lmv->lmv_master_mdt_index !=
3850 lfsck_dev_idx(lfsck->li_bottom)) {
3851 lmv->lmv_master_mdt_index =
3852 lfsck_dev_idx(lfsck->li_bottom);
3853 ns->ln_flags |= LF_INCONSISTENT;
3854 llmv->ll_lmv_updated = 1;
3857 rc = lfsck_namespace_verify_stripe_slave(env, com,
3858 lfsck->li_obj_dir, llmv);
3861 RETURN(rc > 0 ? 0 : rc);
3864 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3865 struct lfsck_component *com, bool init)
3867 struct lfsck_instance *lfsck = com->lc_lfsck;
3868 struct lfsck_namespace *ns = com->lc_file_ram;
3872 rc = lfsck_checkpoint_generic(env, com);
3877 down_write(&com->lc_sem);
3879 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3881 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3882 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3883 HALF_SEC - lfsck->li_time_last_checkpoint);
3884 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3885 ns->ln_items_checked += com->lc_new_checked;
3886 com->lc_new_checked = 0;
3889 rc = lfsck_namespace_store(env, com, false);
3890 up_write(&com->lc_sem);
3893 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3894 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3895 lfsck->li_pos_current.lp_oit_cookie,
3896 PFID(&lfsck->li_pos_current.lp_dir_parent),
3897 lfsck->li_pos_current.lp_dir_cookie, rc);
3899 return rc > 0 ? 0 : rc;
3902 static int lfsck_namespace_prep(const struct lu_env *env,
3903 struct lfsck_component *com,
3904 struct lfsck_start_param *lsp)
3906 struct lfsck_instance *lfsck = com->lc_lfsck;
3907 struct lfsck_namespace *ns = com->lc_file_ram;
3908 struct lfsck_position *pos = &com->lc_pos_start;
3911 rc = lfsck_namespace_load_bitmap(env, com);
3912 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3913 rc = lfsck_namespace_reset(env, com, false);
3915 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3918 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3919 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3925 down_write(&com->lc_sem);
3926 ns->ln_time_latest_start = cfs_time_current_sec();
3927 spin_lock(&lfsck->li_lock);
3929 if (ns->ln_flags & LF_SCANNED_ONCE) {
3930 if (!lfsck->li_drop_dryrun ||
3931 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3932 ns->ln_status = LS_SCANNING_PHASE2;
3933 list_move_tail(&com->lc_link,
3934 &lfsck->li_list_double_scan);
3935 if (!list_empty(&com->lc_link_dir))
3936 list_del_init(&com->lc_link_dir);
3937 lfsck_pos_set_zero(pos);
3939 ns->ln_status = LS_SCANNING_PHASE1;
3940 ns->ln_run_time_phase1 = 0;
3941 ns->ln_run_time_phase2 = 0;
3942 ns->ln_items_checked = 0;
3943 ns->ln_items_repaired = 0;
3944 ns->ln_items_failed = 0;
3945 ns->ln_dirs_checked = 0;
3946 ns->ln_objs_checked_phase2 = 0;
3947 ns->ln_objs_repaired_phase2 = 0;
3948 ns->ln_objs_failed_phase2 = 0;
3949 ns->ln_objs_nlink_repaired = 0;
3950 ns->ln_dirent_repaired = 0;
3951 ns->ln_linkea_repaired = 0;
3952 ns->ln_mul_linked_checked = 0;
3953 ns->ln_mul_linked_repaired = 0;
3954 ns->ln_unknown_inconsistency = 0;
3955 ns->ln_unmatched_pairs_repaired = 0;
3956 ns->ln_dangling_repaired = 0;
3957 ns->ln_mul_ref_repaired = 0;
3958 ns->ln_bad_type_repaired = 0;
3959 ns->ln_lost_dirent_repaired = 0;
3960 ns->ln_striped_dirs_scanned = 0;
3961 ns->ln_striped_dirs_repaired = 0;
3962 ns->ln_striped_dirs_failed = 0;
3963 ns->ln_striped_dirs_disabled = 0;
3964 ns->ln_striped_dirs_skipped = 0;
3965 ns->ln_striped_shards_scanned = 0;
3966 ns->ln_striped_shards_repaired = 0;
3967 ns->ln_striped_shards_failed = 0;
3968 ns->ln_striped_shards_skipped = 0;
3969 ns->ln_name_hash_repaired = 0;
3970 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3971 if (list_empty(&com->lc_link_dir))
3972 list_add_tail(&com->lc_link_dir,
3973 &lfsck->li_list_dir);
3974 *pos = ns->ln_pos_first_inconsistent;
3977 ns->ln_status = LS_SCANNING_PHASE1;
3978 if (list_empty(&com->lc_link_dir))
3979 list_add_tail(&com->lc_link_dir,
3980 &lfsck->li_list_dir);
3981 if (!lfsck->li_drop_dryrun ||
3982 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3983 *pos = ns->ln_pos_last_checkpoint;
3984 pos->lp_oit_cookie++;
3986 *pos = ns->ln_pos_first_inconsistent;
3990 spin_unlock(&lfsck->li_lock);
3991 up_write(&com->lc_sem);
3993 rc = lfsck_start_assistant(env, com, lsp);
3995 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
3996 DFID", "LPX64"]: rc = %d\n",
3997 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
3998 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4003 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4004 struct lfsck_component *com,
4005 struct dt_object *obj)
4007 struct lfsck_thread_info *info = lfsck_env_info(env);
4008 struct lfsck_namespace *ns = com->lc_file_ram;
4009 struct lfsck_instance *lfsck = com->lc_lfsck;
4010 const struct lu_fid *fid = lfsck_dto2fid(obj);
4011 struct lu_fid *pfid = &info->lti_fid2;
4012 struct lu_name *cname = &info->lti_name;
4013 struct lu_seq_range *range = &info->lti_range;
4014 struct dt_device *dev = lfsck->li_bottom;
4015 struct seq_server_site *ss =
4016 lu_site2seq(dev->dd_lu_dev.ld_site);
4017 struct linkea_data ldata = { NULL };
4018 __u32 idx = lfsck_dev_idx(dev);
4022 rc = lfsck_links_read(env, obj, &ldata);
4026 /* -EINVAL means crashed linkEA, should be verified. */
4027 if (rc == -EINVAL) {
4028 rc = lfsck_namespace_trace_update(env, com, fid,
4029 LNTF_CHECK_LINKEA, true);
4031 struct lustre_handle lh = { 0 };
4033 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4034 MDS_INODELOCK_UPDATE |
4035 MDS_INODELOCK_XATTR, LCK_EX);
4037 rc = lfsck_namespace_links_remove(env, com,
4039 lfsck_ibits_unlock(&lh, LCK_EX);
4043 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4046 if (rc == -ENODATA) {
4047 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4055 /* Record multiple-linked object. */
4056 if (ldata.ld_leh->leh_reccount > 1) {
4057 rc = lfsck_namespace_trace_update(env, com, fid,
4058 LNTF_CHECK_LINKEA, true);
4063 linkea_first_entry(&ldata);
4064 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4065 if (!fid_is_sane(pfid)) {
4066 rc = lfsck_namespace_trace_update(env, com, fid,
4067 LNTF_CHECK_PARENT, true);
4069 fld_range_set_mdt(range);
4070 rc = fld_local_lookup(env, ss->ss_server_fld,
4071 fid_seq(pfid), range);
4072 if ((rc == -ENOENT) ||
4073 (rc == 0 && range->lsr_index != idx))
4074 rc = lfsck_namespace_trace_update(env, com, fid,
4075 LNTF_CHECK_LINKEA, true);
4077 rc = lfsck_namespace_check_for_double_scan(env, com,
4084 down_write(&com->lc_sem);
4085 com->lc_new_checked++;
4086 if (S_ISDIR(lfsck_object_type(obj)))
4087 ns->ln_dirs_checked++;
4089 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4090 up_write(&com->lc_sem);
4095 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4096 struct lfsck_component *com,
4097 struct lu_dirent *ent, __u16 type)
4099 struct lfsck_assistant_data *lad = com->lc_data;
4100 struct lfsck_instance *lfsck = com->lc_lfsck;
4101 struct lfsck_namespace_req *lnr;
4102 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4103 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4104 struct ptlrpc_thread *athread = &lad->lad_thread;
4105 struct l_wait_info lwi = { 0 };
4106 bool wakeup = false;
4108 l_wait_event(mthread->t_ctl_waitq,
4109 bk->lb_async_windows == 0 ||
4110 lad->lad_prefetched < bk->lb_async_windows ||
4111 !thread_is_running(mthread) ||
4112 thread_is_stopped(athread),
4115 if (unlikely(!thread_is_running(mthread)) ||
4116 thread_is_stopped(athread))
4119 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4122 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4124 struct lfsck_namespace *ns = com->lc_file_ram;
4126 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4127 return PTR_ERR(lnr);
4130 spin_lock(&lad->lad_lock);
4131 if (lad->lad_assistant_status < 0) {
4132 spin_unlock(&lad->lad_lock);
4133 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4134 return lad->lad_assistant_status;
4137 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4138 if (lad->lad_prefetched == 0)
4141 lad->lad_prefetched++;
4142 spin_unlock(&lad->lad_lock);
4144 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4146 down_write(&com->lc_sem);
4147 com->lc_new_checked++;
4148 up_write(&com->lc_sem);
4153 static int lfsck_namespace_post(const struct lu_env *env,
4154 struct lfsck_component *com,
4155 int result, bool init)
4157 struct lfsck_instance *lfsck = com->lc_lfsck;
4158 struct lfsck_namespace *ns = com->lc_file_ram;
4162 lfsck_post_generic(env, com, &result);
4164 down_write(&com->lc_sem);
4165 lfsck_namespace_release_lmv(env, com);
4167 spin_lock(&lfsck->li_lock);
4169 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4171 ns->ln_status = LS_SCANNING_PHASE2;
4172 ns->ln_flags |= LF_SCANNED_ONCE;
4173 ns->ln_flags &= ~LF_UPGRADE;
4174 list_del_init(&com->lc_link_dir);
4175 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4176 } else if (result == 0) {
4177 if (lfsck->li_status != 0)
4178 ns->ln_status = lfsck->li_status;
4180 ns->ln_status = LS_STOPPED;
4181 if (ns->ln_status != LS_PAUSED) {
4182 list_del_init(&com->lc_link_dir);
4183 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4186 ns->ln_status = LS_FAILED;
4187 list_del_init(&com->lc_link_dir);
4188 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4190 spin_unlock(&lfsck->li_lock);
4193 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4194 HALF_SEC - lfsck->li_time_last_checkpoint);
4195 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4196 ns->ln_items_checked += com->lc_new_checked;
4197 com->lc_new_checked = 0;
4200 rc = lfsck_namespace_store(env, com, false);
4201 up_write(&com->lc_sem);
4203 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4204 lfsck_lfsck2name(lfsck), rc);
4210 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4213 struct lfsck_instance *lfsck = com->lc_lfsck;
4214 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4215 struct lfsck_namespace *ns = com->lc_file_ram;
4218 down_read(&com->lc_sem);
4219 seq_printf(m, "name: lfsck_namespace\n"
4225 lfsck_status2names(ns->ln_status));
4227 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4231 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4235 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4236 "time_since_last_completed");
4240 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4241 "time_since_latest_start");
4245 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4246 "time_since_last_checkpoint");
4250 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4251 "latest_start_position");
4255 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4256 "last_checkpoint_position");
4260 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4261 "first_failure_position");
4265 if (ns->ln_status == LS_SCANNING_PHASE1) {
4266 struct lfsck_position pos;
4267 const struct dt_it_ops *iops;
4268 cfs_duration_t duration = cfs_time_current() -
4269 lfsck->li_time_last_checkpoint;
4270 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4271 __u64 speed = checked;
4272 __u64 new_checked = com->lc_new_checked *
4273 msecs_to_jiffies(MSEC_PER_SEC);
4274 __u32 rtime = ns->ln_run_time_phase1 +
4275 cfs_duration_sec(duration + HALF_SEC);
4278 do_div(new_checked, duration);
4280 do_div(speed, rtime);
4281 lfsck_namespace_dump_statistics(m, ns, checked,
4282 ns->ln_objs_checked_phase2,
4283 rtime, ns->ln_run_time_phase2);
4285 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4286 "average_speed_phase2: N/A\n"
4287 "real_time_speed_phase1: "LPU64" items/sec\n"
4288 "real_time_speed_phase2: N/A\n",
4292 LASSERT(lfsck->li_di_oit != NULL);
4294 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4296 /* The low layer otable-based iteration position may NOT
4297 * exactly match the namespace-based directory traversal
4298 * cookie. Generally, it is not a serious issue. But the
4299 * caller should NOT make assumption on that. */
4300 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4301 if (!lfsck->li_current_oit_processed)
4302 pos.lp_oit_cookie--;
4304 spin_lock(&lfsck->li_lock);
4305 if (lfsck->li_di_dir != NULL) {
4306 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4307 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4308 fid_zero(&pos.lp_dir_parent);
4309 pos.lp_dir_cookie = 0;
4312 *lfsck_dto2fid(lfsck->li_obj_dir);
4315 fid_zero(&pos.lp_dir_parent);
4316 pos.lp_dir_cookie = 0;
4318 spin_unlock(&lfsck->li_lock);
4319 lfsck_pos_dump(m, &pos, "current_position");
4320 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4321 cfs_duration_t duration = cfs_time_current() -
4322 lfsck->li_time_last_checkpoint;
4323 __u64 checked = ns->ln_objs_checked_phase2 +
4324 com->lc_new_checked;
4325 __u64 speed1 = ns->ln_items_checked;
4326 __u64 speed2 = checked;
4327 __u64 new_checked = com->lc_new_checked *
4328 msecs_to_jiffies(MSEC_PER_SEC);
4329 __u32 rtime = ns->ln_run_time_phase2 +
4330 cfs_duration_sec(duration + HALF_SEC);
4333 do_div(new_checked, duration);
4334 if (ns->ln_run_time_phase1 != 0)
4335 do_div(speed1, ns->ln_run_time_phase1);
4337 do_div(speed2, rtime);
4338 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4340 ns->ln_run_time_phase1, rtime);
4342 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4343 "average_speed_phase2: "LPU64" objs/sec\n"
4344 "real_time_speed_phase1: N/A\n"
4345 "real_time_speed_phase2: "LPU64" objs/sec\n"
4346 "current_position: "DFID"\n",
4350 PFID(&ns->ln_fid_latest_scanned_phase2));
4352 __u64 speed1 = ns->ln_items_checked;
4353 __u64 speed2 = ns->ln_objs_checked_phase2;
4355 if (ns->ln_run_time_phase1 != 0)
4356 do_div(speed1, ns->ln_run_time_phase1);
4357 if (ns->ln_run_time_phase2 != 0)
4358 do_div(speed2, ns->ln_run_time_phase2);
4359 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4360 ns->ln_objs_checked_phase2,
4361 ns->ln_run_time_phase1,
4362 ns->ln_run_time_phase2);
4364 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4365 "average_speed_phase2: "LPU64" objs/sec\n"
4366 "real_time_speed_phase1: N/A\n"
4367 "real_time_speed_phase2: N/A\n"
4368 "current_position: N/A\n",
4373 up_read(&com->lc_sem);
4377 static int lfsck_namespace_double_scan(const struct lu_env *env,
4378 struct lfsck_component *com)
4380 struct lfsck_namespace *ns = com->lc_file_ram;
4381 struct lfsck_assistant_data *lad = com->lc_data;
4382 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4383 struct lfsck_tgt_desc *ltd;
4384 struct lfsck_tgt_desc *next;
4387 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4388 if (thread_is_stopped(&lad->lad_thread)) {
4389 LASSERT(list_empty(&lad->lad_req_list));
4390 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4392 spin_lock(<ds->ltd_lock);
4393 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4394 ltd_namespace_phase_list) {
4395 list_del_init(<d->ltd_namespace_phase_list);
4397 spin_unlock(<ds->ltd_lock);
4403 static void lfsck_namespace_data_release(const struct lu_env *env,
4404 struct lfsck_component *com)
4406 struct lfsck_assistant_data *lad = com->lc_data;
4407 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4408 struct lfsck_tgt_desc *ltd;
4409 struct lfsck_tgt_desc *next;
4411 LASSERT(lad != NULL);
4412 LASSERT(thread_is_init(&lad->lad_thread) ||
4413 thread_is_stopped(&lad->lad_thread));
4414 LASSERT(list_empty(&lad->lad_req_list));
4416 com->lc_data = NULL;
4417 lfsck_namespace_release_lmv(env, com);
4419 spin_lock(<ds->ltd_lock);
4420 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4421 ltd_namespace_phase_list) {
4422 list_del_init(<d->ltd_namespace_phase_list);
4424 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4425 ltd_namespace_phase_list) {
4426 list_del_init(<d->ltd_namespace_phase_list);
4428 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4429 ltd_namespace_list) {
4430 list_del_init(<d->ltd_namespace_list);
4432 spin_unlock(<ds->ltd_lock);
4434 if (likely(lad->lad_bitmap != NULL))
4435 CFS_FREE_BITMAP(lad->lad_bitmap);
4440 static void lfsck_namespace_quit(const struct lu_env *env,
4441 struct lfsck_component *com)
4443 struct lfsck_assistant_data *lad = com->lc_data;
4444 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4445 struct lfsck_tgt_desc *ltd;
4446 struct lfsck_tgt_desc *next;
4448 LASSERT(lad != NULL);
4450 lfsck_quit_generic(env, com);
4452 LASSERT(thread_is_init(&lad->lad_thread) ||
4453 thread_is_stopped(&lad->lad_thread));
4454 LASSERT(list_empty(&lad->lad_req_list));
4456 lfsck_namespace_release_lmv(env, com);
4458 spin_lock(<ds->ltd_lock);
4459 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4460 ltd_namespace_phase_list) {
4461 list_del_init(<d->ltd_namespace_phase_list);
4463 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4464 ltd_namespace_phase_list) {
4465 list_del_init(<d->ltd_namespace_phase_list);
4467 spin_unlock(<ds->ltd_lock);
4470 static int lfsck_namespace_in_notify(const struct lu_env *env,
4471 struct lfsck_component *com,
4472 struct lfsck_request *lr,
4475 struct lfsck_instance *lfsck = com->lc_lfsck;
4476 struct lfsck_namespace *ns = com->lc_file_ram;
4477 struct lfsck_assistant_data *lad = com->lc_data;
4478 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4479 struct lfsck_tgt_desc *ltd;
4484 switch (lr->lr_event) {
4485 case LE_SKIP_NLINK_DECLARE: {
4486 struct dt_object *obj;
4487 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4491 LASSERT(th != NULL);
4493 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4494 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4495 fid_cpu_to_be(key, &lr->lr_fid);
4496 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4497 rc = dt_declare_delete(env, obj,
4498 (const struct dt_key *)key, th);
4500 rc = dt_declare_insert(env, obj,
4501 (const struct dt_rec *)&flags,
4502 (const struct dt_key *)key, th);
4503 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4507 case LE_SKIP_NLINK: {
4508 struct dt_object *obj;
4509 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4515 LASSERT(th != NULL);
4517 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4518 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4519 fid_cpu_to_be(key, &lr->lr_fid);
4520 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4521 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4522 (const struct dt_key *)key, BYPASS_CAPA);
4524 if (flags & LNTF_SKIP_NLINK) {
4526 &com->lc_sub_trace_objs[idx].lsto_mutex);
4532 } else if (rc != -ENOENT) {
4536 flags |= LNTF_SKIP_NLINK;
4538 rc = dt_delete(env, obj, (const struct dt_key *)key,
4544 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4545 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4550 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4551 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4552 " to be skipped for namespace double scan: rc = %d\n",
4553 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4556 /* If we cannot record this object in the LFSCK tracing,
4557 * we have to mark the LFSC as LF_INCOMPLETE, then the
4558 * LFSCK will skip nlink attribute verification for
4560 ns->ln_flags |= LF_INCOMPLETE;
4564 case LE_SET_LMV_MASTER: {
4565 struct dt_object *obj;
4567 obj = lfsck_object_find_by_dev(env, lfsck->li_bottom,
4570 RETURN(PTR_ERR(obj));
4572 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4573 lfsck_object_put(env, obj);
4575 RETURN(rc > 0 ? 0 : rc);
4577 case LE_SET_LMV_SLAVE: {
4578 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4579 ns->ln_striped_shards_repaired++;
4581 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4582 LNTF_RECHECK_NAME_HASH, true);
4584 RETURN(rc > 0 ? 0 : rc);
4586 case LE_PHASE1_DONE:
4587 case LE_PHASE2_DONE:
4594 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4595 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4596 lr->lr_index, lr->lr_status, lr->lr_flags2);
4598 spin_lock(<ds->ltd_lock);
4599 ltd = LTD_TGT(ltds, lr->lr_index);
4601 spin_unlock(<ds->ltd_lock);
4606 list_del_init(<d->ltd_namespace_phase_list);
4607 switch (lr->lr_event) {
4608 case LE_PHASE1_DONE:
4609 if (lr->lr_status <= 0) {
4610 ltd->ltd_namespace_done = 1;
4611 list_del_init(<d->ltd_namespace_list);
4612 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4613 "phase1 for namespace LFSCK: rc = %d.\n",
4614 lfsck_lfsck2name(lfsck),
4615 ltd->ltd_index, lr->lr_status);
4616 ns->ln_flags |= LF_INCOMPLETE;
4621 if (lr->lr_flags2 & LF_INCOMPLETE)
4622 ns->ln_flags |= LF_INCOMPLETE;
4624 if (list_empty(<d->ltd_namespace_list))
4625 list_add_tail(<d->ltd_namespace_list,
4626 &lad->lad_mdt_list);
4627 list_add_tail(<d->ltd_namespace_phase_list,
4628 &lad->lad_mdt_phase2_list);
4630 case LE_PHASE2_DONE:
4631 ltd->ltd_namespace_done = 1;
4632 list_del_init(<d->ltd_namespace_list);
4636 ltd->ltd_namespace_done = 1;
4637 list_del_init(<d->ltd_namespace_list);
4638 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4640 "%s: the peer MDT %x exit namespace LFSCK\n",
4641 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4642 ns->ln_flags |= LF_INCOMPLETE;
4648 spin_unlock(<ds->ltd_lock);
4650 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4651 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4653 memset(stop, 0, sizeof(*stop));
4654 stop->ls_status = lr->lr_status;
4655 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4656 lfsck_stop(env, lfsck->li_bottom, stop);
4657 } else if (lfsck_phase2_next_ready(lad)) {
4658 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4664 static int lfsck_namespace_query(const struct lu_env *env,
4665 struct lfsck_component *com)
4667 struct lfsck_namespace *ns = com->lc_file_ram;
4669 return ns->ln_status;
4672 static struct lfsck_operations lfsck_namespace_ops = {
4673 .lfsck_reset = lfsck_namespace_reset,
4674 .lfsck_fail = lfsck_namespace_fail,
4675 .lfsck_close_dir = lfsck_namespace_close_dir,
4676 .lfsck_open_dir = lfsck_namespace_open_dir,
4677 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4678 .lfsck_prep = lfsck_namespace_prep,
4679 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4680 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4681 .lfsck_post = lfsck_namespace_post,
4682 .lfsck_dump = lfsck_namespace_dump,
4683 .lfsck_double_scan = lfsck_namespace_double_scan,
4684 .lfsck_data_release = lfsck_namespace_data_release,
4685 .lfsck_quit = lfsck_namespace_quit,
4686 .lfsck_in_notify = lfsck_namespace_in_notify,
4687 .lfsck_query = lfsck_namespace_query,
4691 * Repair dangling name entry.
4693 * For the name entry with dangling reference, we need to repare the
4694 * inconsistency according to the LFSCK sponsor's requirement:
4696 * 1) Keep the inconsistency there and report the inconsistency case,
4697 * then give the chance to the application to find related issues,
4698 * and the users can make the decision about how to handle it with
4699 * more human knownledge. (by default)
4701 * 2) Re-create the missing MDT-object with the FID information.
4703 * \param[in] env pointer to the thread context
4704 * \param[in] com pointer to the lfsck component
4705 * \param[in] parent pointer to the dir object that contains the dangling
4707 * \param[in] child pointer to the object corresponding to the dangling
4709 * \param[in] lnr pointer to the namespace request that contains the
4710 * name's name, parent object, parent's LMV, and ect.
4712 * \retval positive number if no need to repair
4713 * \retval zero for repaired successfully
4714 * \retval negative error number on failure
4716 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4717 struct lfsck_component *com,
4718 struct dt_object *parent,
4719 struct dt_object *child,
4720 struct lfsck_namespace_req *lnr)
4722 struct lfsck_thread_info *info = lfsck_env_info(env);
4723 struct lu_attr *la = &info->lti_la;
4724 struct dt_allocation_hint *hint = &info->lti_hint;
4725 struct dt_object_format *dof = &info->lti_dof;
4726 struct dt_insert_rec *rec = &info->lti_dt_rec;
4727 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4728 const struct lu_name *cname;
4729 struct linkea_data ldata = { NULL };
4730 struct lustre_handle lh = { 0 };
4731 struct lu_buf linkea_buf;
4732 struct lu_buf lmv_buf;
4733 struct lfsck_instance *lfsck = com->lc_lfsck;
4734 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4735 struct dt_device *dev = lfsck_obj2dt_dev(child);
4736 struct thandle *th = NULL;
4738 __u16 type = lnr->lnr_type;
4742 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4743 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4748 if (!create || bk->lb_param & LPF_DRYRUN)
4751 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4755 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
4759 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
4760 MDS_INODELOCK_UPDATE, LCK_EX);
4764 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
4768 th = dt_trans_create(env, dev);
4770 GOTO(log, rc = PTR_ERR(th));
4772 /* Set the ctime as zero, then others can know it is created for
4773 * repairing dangling name entry by LFSCK. And if the LFSCK made
4774 * wrong decision and the real MDT-object has been found later,
4775 * then the LFSCK has chance to fix the incosistency properly. */
4776 memset(la, 0, sizeof(*la));
4777 la->la_mode = (type & S_IFMT) | 0600;
4778 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4779 LA_ATIME | LA_MTIME | LA_CTIME;
4781 child->do_ops->do_ah_init(env, hint, parent, child,
4782 la->la_mode & S_IFMT);
4784 memset(dof, 0, sizeof(*dof));
4785 dof->dof_type = dt_mode_to_dft(type);
4786 /* If the target is a regular file, then the LFSCK will only create
4787 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4788 * OST-objects will be created when write open. */
4790 /* 1a. create child. */
4791 rc = dt_declare_create(env, child, la, hint, dof, th);
4795 if (S_ISDIR(type)) {
4796 if (unlikely(!dt_try_as_dir(env, child)))
4797 GOTO(stop, rc = -ENOTDIR);
4799 /* 2a. insert dot into child dir */
4800 rec->rec_type = S_IFDIR;
4801 rec->rec_fid = lfsck_dto2fid(child);
4802 rc = dt_declare_insert(env, child,
4803 (const struct dt_rec *)rec,
4804 (const struct dt_key *)dot, th);
4808 /* 3a. insert dotdot into child dir */
4809 rec->rec_fid = lfsck_dto2fid(parent);
4810 rc = dt_declare_insert(env, child,
4811 (const struct dt_rec *)rec,
4812 (const struct dt_key *)dotdot, th);
4816 /* 4a. increase child nlink */
4817 rc = dt_declare_ref_add(env, child, th);
4821 /* 5a. generate slave LMV EA. */
4822 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4825 idx = lfsck_shard_name_to_index(env,
4826 lnr->lnr_name, lnr->lnr_namelen,
4827 type, lfsck_dto2fid(child));
4828 if (unlikely(idx < 0))
4829 GOTO(stop, rc = idx);
4831 *lmv2 = lnr->lnr_lmv->ll_lmv;
4832 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4833 lmv2->lmv_master_mdt_index = idx;
4835 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4836 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4837 rc = dt_declare_xattr_set(env, child, &lmv_buf,
4838 XATTR_NAME_LMV, 0, th);
4844 /* 6a. insert linkEA for child */
4845 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4846 ldata.ld_leh->leh_len);
4847 rc = dt_declare_xattr_set(env, child, &linkea_buf,
4848 XATTR_NAME_LINK, 0, th);
4852 rc = dt_trans_start(env, dev, th);
4854 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4856 dt_write_lock(env, child, 0);
4857 /* 1b. create child */
4858 rc = dt_create(env, child, la, hint, dof, th);
4860 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4862 if (S_ISDIR(type)) {
4863 if (unlikely(!dt_try_as_dir(env, child)))
4864 GOTO(unlock, rc = -ENOTDIR);
4866 /* 2b. insert dot into child dir */
4867 rec->rec_type = S_IFDIR;
4868 rec->rec_fid = lfsck_dto2fid(child);
4869 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4870 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4874 /* 3b. insert dotdot into child dir */
4875 rec->rec_fid = lfsck_dto2fid(parent);
4876 rc = dt_insert(env, child, (const struct dt_rec *)rec,
4877 (const struct dt_key *)dotdot, th,
4882 /* 4b. increase child nlink */
4883 rc = dt_ref_add(env, child, th);
4887 /* 5b. generate slave LMV EA. */
4888 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4889 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
4890 0, th, BYPASS_CAPA);
4896 /* 6b. insert linkEA for child. */
4897 rc = dt_xattr_set(env, child, &linkea_buf,
4898 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4903 dt_write_unlock(env, child);
4906 dt_trans_stop(env, dev, th);
4909 lfsck_ibits_unlock(&lh, LCK_EX);
4910 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4911 "reference for: parent "DFID", child "DFID", type %u, "
4912 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4913 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
4914 type, cname->ln_name,
4915 create ? "Create the lost OST-object as required" :
4916 "Keep the MDT-object there by default", rc);
4919 struct lfsck_namespace *ns = com->lc_file_ram;
4921 ns->ln_flags |= LF_INCONSISTENT;
4927 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4928 struct lfsck_component *com,
4929 struct lfsck_assistant_req *lar)
4931 struct lfsck_thread_info *info = lfsck_env_info(env);
4932 struct lu_attr *la = &info->lti_la;
4933 struct lfsck_instance *lfsck = com->lc_lfsck;
4934 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4935 struct lfsck_namespace *ns = com->lc_file_ram;
4936 struct linkea_data ldata = { NULL };
4937 const struct lu_name *cname;
4938 struct thandle *handle = NULL;
4939 struct lfsck_namespace_req *lnr =
4940 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
4941 struct dt_object *dir = NULL;
4942 struct dt_object *obj = NULL;
4943 const struct lu_fid *pfid;
4944 struct dt_device *dev = NULL;
4945 struct lustre_handle lh = { 0 };
4946 bool repaired = false;
4947 bool dtlocked = false;
4951 bool bad_hash = false;
4955 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
4958 dir = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
4960 RETURN(PTR_ERR(dir));
4962 if (unlikely(lfsck_is_dead_obj(dir)))
4963 GOTO(put_dir, rc = 0);
4965 pfid = lfsck_dto2fid(dir);
4967 if (lnr->lnr_attr & LUDA_UPGRADE) {
4968 ns->ln_flags |= LF_UPGRADE;
4969 ns->ln_dirent_repaired++;
4971 } else if (lnr->lnr_attr & LUDA_REPAIR) {
4972 ns->ln_flags |= LF_INCONSISTENT;
4973 ns->ln_dirent_repaired++;
4977 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
4978 if (strcmp(lnr->lnr_name, dotdot) != 0)
4981 rc = lfsck_namespace_trace_update(env, com, pfid,
4982 LNTF_CHECK_PARENT, true);
4987 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
4988 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
4989 " for the name entry %.*s under "DFID"\n",
4990 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
4991 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
4993 if (strcmp(lnr->lnr_name, dotdot) != 0)
4994 /* invalid FID means bad name entry, remove it. */
4995 type = LNIT_BAD_DIRENT;
4997 /* If the parent FID is invalid, we cannot remove
4998 * the ".." entry directly. */
4999 rc = lfsck_namespace_trace_update(env, com, pfid,
5000 LNTF_CHECK_PARENT, true);
5005 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5006 rc = lfsck_namespace_striped_dir_rescan(env, com, dir, lnr);
5011 if (lnr->lnr_name[0] == '.' &&
5012 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
5015 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5016 rc = lfsck_namespace_handle_striped_master(env, com, dir, lnr);
5021 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5023 GOTO(out, rc = idx);
5025 if (idx == lfsck_dev_idx(lfsck->li_bottom)) {
5026 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5029 dev = lfsck->li_bottom;
5031 struct lfsck_tgt_desc *ltd;
5033 /* Usually, some local filesystem consistency verification
5034 * tools can guarantee the local namespace tree consistenct.
5035 * So the LFSCK will only verify the remote directory. */
5036 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5037 rc = lfsck_namespace_trace_update(env, com, pfid,
5038 LNTF_CHECK_PARENT, true);
5043 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5044 if (unlikely(ltd == NULL)) {
5045 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5046 "did not join the namespace LFSCK\n",
5047 lfsck_lfsck2name(lfsck), idx);
5048 lfsck_lad_set_bitmap(env, com, idx);
5050 GOTO(out, rc = -ENODEV);
5056 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5058 GOTO(out, rc = PTR_ERR(obj));
5060 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5061 if (dt_object_exists(obj) == 0) {
5064 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5066 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5067 lnr->lnr_name, lnr->lnr_namelen)) {
5068 type = LNIT_BAD_DIRENT;
5073 type = LNIT_DANGLING;
5074 rc = lfsck_namespace_repair_dangling(env, com, dir,
5083 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5086 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5087 MDS_INODELOCK_UPDATE |
5088 MDS_INODELOCK_XATTR, LCK_EX);
5092 handle = dt_trans_create(env, dev);
5094 GOTO(out, rc = PTR_ERR(handle));
5096 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5100 rc = dt_trans_start(env, dev, handle);
5104 dt_write_lock(env, obj, 0);
5108 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5112 rc = lfsck_links_read(env, obj, &ldata);
5113 if (unlikely(rc == -ENOENT)) {
5114 if (handle != NULL) {
5115 dt_write_unlock(env, obj);
5118 dt_trans_stop(env, dev, handle);
5121 lfsck_ibits_unlock(&lh, LCK_EX);
5124 /* It may happen when the remote object has been removed,
5125 * but the local MDT is not aware of that. */
5127 } else if (rc == 0) {
5128 count = ldata.ld_leh->leh_reccount;
5129 rc = linkea_links_find(&ldata, cname, pfid);
5131 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5132 if ((lfsck_object_type(obj) & S_IFMT) !=
5134 ns->ln_flags |= LF_INCONSISTENT;
5135 type = LNIT_BAD_TYPE;
5141 ns->ln_flags |= LF_INCONSISTENT;
5143 /* If the name entry hash does not match the slave striped
5144 * directory, and the name entry does not match also, then
5145 * it is quite possible that name entry is corrupted. */
5146 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5147 lnr->lnr_name, lnr->lnr_namelen)) {
5148 type = LNIT_BAD_DIRENT;
5153 /* If the file type stored in the name entry does not match
5154 * the file type claimed by the object, and the object does
5155 * not recognize the name entry, then it is quite possible
5156 * that the name entry is corrupted. */
5157 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5158 type = LNIT_BAD_DIRENT;
5163 /* For sub-dir object, we cannot make sure whether the sub-dir
5164 * back references the parent via ".." name entry correctly or
5165 * not in the LFSCK first-stage scanning. It may be that the
5166 * (remote) sub-dir ".." name entry has no parent FID after
5167 * file-level backup/restore and its linkEA may be wrong.
5168 * So under such case, we should replace the linkEA according
5169 * to current name entry. But this needs to be done during the
5170 * LFSCK second-stage scanning. The LFSCK will record the name
5171 * entry for further possible using. */
5175 } else if (unlikely(rc == -EINVAL)) {
5176 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5177 type = LNIT_BAD_TYPE;
5180 ns->ln_flags |= LF_INCONSISTENT;
5181 /* The magic crashed, we are not sure whether there are more
5182 * corrupt data in the linkea, so remove all linkea entries. */
5186 } else if (rc == -ENODATA) {
5187 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5188 type = LNIT_BAD_TYPE;
5191 ns->ln_flags |= LF_UPGRADE;
5196 if (bk->lb_param & LPF_DRYRUN) {
5197 ns->ln_linkea_repaired++;
5203 if (!lustre_handle_is_used(&lh))
5209 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5216 rc = linkea_data_new(&ldata,
5217 &lfsck_env_info(env)->lti_linkea_buf);
5222 rc = linkea_add_buf(&ldata, cname, pfid);
5226 rc = lfsck_links_write(env, obj, &ldata, handle);
5227 if (unlikely(rc == -ENOSPC) &&
5228 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5229 if (handle != NULL) {
5230 LASSERT(dt_write_locked(env, obj));
5232 dt_write_unlock(env, obj);
5235 dt_trans_stop(env, dev, handle);
5238 lfsck_ibits_unlock(&lh, LCK_EX);
5241 rc = lfsck_namespace_trace_update(env, com,
5242 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5244 /* If we cannot record this object in the
5245 * LFSCK tracing, we have to mark the LFSCK
5246 * as LF_INCOMPLETE, then the LFSCK will
5247 * skip nlink attribute verification for
5249 ns->ln_flags |= LF_INCOMPLETE;
5257 count = ldata.ld_leh->leh_reccount;
5258 if (!S_ISDIR(lfsck_object_type(obj)) ||
5259 !dt_object_remote(obj)) {
5260 ns->ln_linkea_repaired++;
5270 dt_write_unlock(env, obj);
5272 if (handle != NULL && !IS_ERR(handle))
5273 dt_trans_stop(env, dev, handle);
5276 lfsck_ibits_unlock(&lh, LCK_EX);
5278 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5279 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5280 lnr->lnr_name, lnr->lnr_namelen) &&
5281 type != LNIT_BAD_DIRENT) {
5282 ns->ln_flags |= LF_INCONSISTENT;
5285 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5286 lnr->lnr_lmv, lnr->lnr_name);
5295 rc = lfsck_namespace_repair_dirent(env, com, dir,
5296 obj, lnr->lnr_name, lnr->lnr_name,
5297 lnr->lnr_type, true, false);
5301 case LNIT_BAD_DIRENT:
5303 /* XXX: This is a bad dirent, we do not know whether
5304 * the original name entry reference a regular
5305 * file or a directory, then keep the parent's
5306 * nlink count unchanged here. */
5307 rc = lfsck_namespace_repair_dirent(env, com, dir,
5308 obj, lnr->lnr_name, lnr->lnr_name,
5309 lnr->lnr_type, false, false);
5317 if (count == 1 && S_ISREG(lfsck_object_type(obj)))
5318 dt_attr_get(env, obj, la, BYPASS_CAPA);
5321 down_write(&com->lc_sem);
5323 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5324 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5325 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5326 PFID(lfsck_dto2fid(dir)),
5327 lnr->lnr_namelen, lnr->lnr_name, rc);
5329 lfsck_namespace_record_failure(env, lfsck, ns);
5330 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5331 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5332 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5333 dev != NULL && dev != lfsck->li_next)
5334 lfsck_lad_set_bitmap(env, com, idx);
5336 if (!(bk->lb_param & LPF_FAILOUT))
5340 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5341 "repaired the entry: "DFID", parent "DFID
5342 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5343 PFID(&lnr->lnr_fid),
5344 PFID(lfsck_dto2fid(dir)),
5345 lnr->lnr_namelen, lnr->lnr_name);
5348 ns->ln_items_repaired++;
5352 ns->ln_dangling_repaired++;
5355 ns->ln_bad_type_repaired++;
5357 case LNIT_BAD_DIRENT:
5358 ns->ln_dirent_repaired++;
5364 if (bk->lb_param & LPF_DRYRUN &&
5365 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5366 lfsck_pos_fill(env, lfsck,
5367 &ns->ln_pos_first_inconsistent,
5372 ns->ln_name_hash_repaired++;
5374 /* Not count repeatedly. */
5376 ns->ln_items_repaired++;
5378 if (bk->lb_param & LPF_DRYRUN &&
5379 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5380 lfsck_pos_fill(env, lfsck,
5381 &ns->ln_pos_first_inconsistent,
5388 if (count > 1 || la->la_nlink > 1)
5389 ns->ln_mul_linked_checked++;
5391 up_write(&com->lc_sem);
5393 if (obj != NULL && !IS_ERR(obj))
5394 lfsck_object_put(env, obj);
5397 lu_object_put(env, &dir->do_lu);
5403 * Handle one orphan under the backend /lost+found directory
5405 * Insert the orphan FID into the namespace LFSCK trace file for further
5406 * processing (via the subsequent namespace LFSCK second-stage scanning).
5407 * At the same time, remove the orphan name entry from backend /lost+found
5408 * directory. There is an interval between the orphan name entry removed
5409 * from the backend /lost+found directory and the orphan FID in the LFSCK
5410 * trace file handled. In such interval, the LFSCK can be reset, then
5411 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5412 * To guarantee that the orphans can be found when LFSCK run next time
5413 * without e2fsck again, when remove the orphan name entry, the LFSCK
5414 * will set the orphan's ctime attribute as 1. Since normal applications
5415 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5416 * next time, it can record the object (that ctime is 1) in the namespace
5417 * LFSCK trace file during the first-stage scanning.
5419 * \param[in] env pointer to the thread context
5420 * \param[in] com pointer to the lfsck component
5421 * \param[in] parent pointer to the object for the backend /lost+found
5422 * \param[in] ent pointer to the name entry for the target under the
5423 * backend /lost+found
5425 * \retval positive for repaired
5426 * \retval 0 if needs to repair nothing
5427 * \retval negative error number on failure
5429 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5430 struct lfsck_component *com,
5431 struct dt_object *parent,
5432 struct lu_dirent *ent)
5434 struct lfsck_thread_info *info = lfsck_env_info(env);
5435 struct lu_fid *key = &info->lti_fid;
5436 struct lu_attr *la = &info->lti_la;
5437 struct lfsck_instance *lfsck = com->lc_lfsck;
5438 struct dt_object *obj;
5439 struct dt_device *dev = lfsck->li_bottom;
5440 struct dt_object *child = NULL;
5441 struct thandle *th = NULL;
5448 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5450 RETURN(PTR_ERR(child));
5452 LASSERT(dt_object_exists(child));
5453 LASSERT(!dt_object_remote(child));
5455 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5456 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5457 fid_cpu_to_be(key, &ent->lde_fid);
5458 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5459 (const struct dt_key *)key, BYPASS_CAPA);
5462 flags |= LNTF_CHECK_ORPHAN;
5463 } else if (rc == -ENOENT) {
5464 flags = LNTF_CHECK_ORPHAN;
5469 th = dt_trans_create(env, dev);
5471 GOTO(out, rc = PTR_ERR(th));
5473 /* a1. remove name entry from backend /lost+found */
5474 rc = dt_declare_delete(env, parent,
5475 (const struct dt_key *)ent->lde_name, th);
5479 if (S_ISDIR(lfsck_object_type(child))) {
5480 /* a2. decrease parent's nlink */
5481 rc = dt_declare_ref_del(env, parent, th);
5487 /* a3. remove child's FID from the LFSCK trace file. */
5488 rc = dt_declare_delete(env, obj,
5489 (const struct dt_key *)key, th);
5493 /* a4. set child's ctime as 1 */
5494 memset(la, 0, sizeof(*la));
5496 la->la_valid = LA_CTIME;
5497 rc = dt_declare_attr_set(env, child, la, th);
5502 /* a5. insert child's FID into the LFSCK trace file. */
5503 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5504 (const struct dt_key *)key, th);
5508 rc = dt_trans_start_local(env, dev, th);
5512 /* b1. remove name entry from backend /lost+found */
5513 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5518 if (S_ISDIR(lfsck_object_type(child))) {
5519 /* b2. decrease parent's nlink */
5520 dt_write_lock(env, parent, 0);
5521 rc = dt_ref_del(env, parent, th);
5522 dt_write_unlock(env, parent);
5528 /* a3. remove child's FID from the LFSCK trace file. */
5529 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5534 /* b4. set child's ctime as 1 */
5535 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5540 /* b5. insert child's FID into the LFSCK trace file. */
5541 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5542 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5544 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5547 dt_trans_stop(env, dev, th);
5550 lu_object_put(env, &child->do_lu);
5556 * Handle orphans under the backend /lost+found directory
5558 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5559 * and put them under the backend /lost+found directory that is invisible
5560 * to client. The LFSCK will scan such directory, for the original client
5561 * visible orphans, add their fids into the namespace LFSCK trace file,
5562 * then the subsenquent namespace LFSCK second-stage scanning can handle
5563 * them as other objects to be double scanned: either move back to normal
5564 * namespace, or to the global visible orphan directory:
5565 * /ROOT/.lustre/lost+found/MDTxxxx/
5567 * \param[in] env pointer to the thread context
5568 * \param[in] com pointer to the lfsck component
5570 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5571 struct lfsck_component *com)
5573 struct lfsck_thread_info *info = lfsck_env_info(env);
5574 struct lu_dirent *ent =
5575 (struct lu_dirent *)info->lti_key;
5576 struct lu_seq_range *range = &info->lti_range;
5577 struct lfsck_instance *lfsck = com->lc_lfsck;
5578 struct ptlrpc_thread *thread = &lfsck->li_thread;
5579 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5580 struct dt_device *dev = lfsck->li_bottom;
5581 struct lfsck_namespace *ns = com->lc_file_ram;
5582 struct dt_object *parent;
5583 const struct dt_it_ops *iops;
5585 struct seq_server_site *ss =
5586 lu_site2seq(dev->dd_lu_dev.ld_site);
5592 parent = lfsck_object_find_by_dev(env, dev, &LU_BACKEND_LPF_FID);
5593 if (IS_ERR(parent)) {
5594 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5595 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5599 /* It is normal that the /lost+found does not exist for ZFS backend. */
5600 if (!dt_object_exists(parent))
5603 if (unlikely(!dt_try_as_dir(env, parent)))
5604 GOTO(out, rc = -ENOTDIR);
5606 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5607 lfsck_lfsck2name(lfsck));
5609 com->lc_new_scanned = 0;
5610 iops = &parent->do_index_ops->dio_it;
5611 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5613 GOTO(out, rc = PTR_ERR(di));
5615 rc = iops->load(env, di, 0);
5617 rc = iops->next(env, di);
5622 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5623 unlikely(!thread_is_running(thread)))
5626 rc = iops->rec(env, di, (struct dt_rec *)ent,
5627 LUDA_64BITHASH | LUDA_TYPE);
5629 rc = lfsck_unpack_ent(ent, &cookie, &type);
5631 if (unlikely(rc != 0)) {
5632 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5633 "/lost+found: rc = %d\n",
5634 lfsck_lfsck2name(lfsck), rc);
5639 /* skip dot and dotdot entries */
5640 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5643 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5646 if (fid_is_norm(&ent->lde_fid)) {
5647 fld_range_set_mdt(range);
5648 rc = fld_local_lookup(env, ss->ss_server_fld,
5649 fid_seq(&ent->lde_fid), range);
5652 } else if (lfsck_dev_idx(dev) != 0) {
5653 /* If the returned FID is IGIF, then there are three
5656 * 1) The object is upgraded from old Lustre-1.8 with
5657 * IGIF assigned to such object.
5658 * 2) The object is a backend local object and is
5659 * invisible to client.
5660 * 3) The object lost its LMV EA, and since there is
5661 * no FID-in-dirent for the orphan in the backend
5662 * /lost+found directory, then the low layer will
5663 * return IGIF for such object.
5665 * For MDTx (x != 0), it is either case 2) or case 3),
5666 * but from the LFSCK view, they are indistinguishable.
5667 * To be safe, the LFSCK will keep it there and report
5668 * some message, then the adminstrator can handle that
5671 * For MDT0, it is more possible the case 1). The LFSCK
5672 * will handle the orphan as an upgraded object. */
5673 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5674 "in the backend /lost+found on the MDT %04x, "
5675 "to be safe, skip it.\n",
5676 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5677 ent->lde_name, PFID(&ent->lde_fid),
5678 lfsck_dev_idx(dev));
5682 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5685 down_write(&com->lc_sem);
5686 com->lc_new_scanned++;
5687 ns->ln_local_lpf_scanned++;
5689 ns->ln_local_lpf_moved++;
5691 ns->ln_local_lpf_skipped++;
5693 ns->ln_local_lpf_failed++;
5694 up_write(&com->lc_sem);
5696 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5700 lfsck_control_speed_by_self(com);
5701 if (unlikely(!thread_is_running(thread))) {
5706 rc = iops->next(env, di);
5710 iops->fini(env, di);
5715 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5716 lfsck_lfsck2name(lfsck), rc);
5718 lu_object_put(env, &parent->do_lu);
5722 * Rescan the striped directory after the master LMV EA reset.
5724 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5725 * the namespace LFSCK engine scan the striped directory for the first time,
5726 * it will be regarded as a normal directory. As the LFSCK processing, some
5727 * other LFSCK instance on other MDT will find the shard of this striped dir,
5728 * and find that the master MDT-object of the striped directory lost its LMV
5729 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5730 * notify the LFSCK instance on this MDT to rescan the striped directory.
5732 * \param[in] env pointer to the thread context
5733 * \param[in] com pointer to the lfsck component
5734 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5737 * \retval positive number for success
5738 * \retval 0 for LFSCK stopped/paused
5739 * \retval negative error number on failure
5741 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5742 struct lfsck_component *com,
5743 struct lfsck_lmv_unit *llu)
5745 struct lfsck_thread_info *info = lfsck_env_info(env);
5746 struct lfsck_instance *lfsck = com->lc_lfsck;
5747 struct lfsck_assistant_data *lad = com->lc_data;
5748 struct dt_object *dir;
5749 const struct dt_it_ops *iops;
5751 struct lu_dirent *ent =
5752 (struct lu_dirent *)info->lti_key;
5753 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5754 struct ptlrpc_thread *thread = &lfsck->li_thread;
5755 struct lfsck_namespace_req *lnr;
5756 struct lfsck_assistant_req *lar;
5761 LASSERT(list_empty(&lad->lad_req_list));
5763 lfsck->li_lmv = &llu->llu_lmv;
5764 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5765 rc = lfsck_open_dir(env, lfsck, 0);
5769 dir = lfsck->li_obj_dir;
5770 di = lfsck->li_di_dir;
5771 iops = &dir->do_index_ops->dio_it;
5773 rc = iops->rec(env, di, (struct dt_rec *)ent,
5774 lfsck->li_args_dir);
5776 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5780 if (bk->lb_param & LPF_FAILOUT)
5786 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5789 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5791 if (bk->lb_param & LPF_FAILOUT)
5792 GOTO(out, rc = PTR_ERR(lnr));
5797 lar = &lnr->lnr_lar;
5798 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5799 lfsck_namespace_assistant_req_fini(env, lar);
5800 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5803 if (unlikely(!thread_is_running(thread)))
5807 rc = iops->next(env, di);
5811 lfsck_close_dir(env, lfsck, rc);
5815 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5816 if (list_empty(&lad->lad_req_list))
5819 spin_lock(&lad->lad_lock);
5820 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5822 list_del_init(&lar->lar_list);
5823 spin_unlock(&lad->lad_lock);
5825 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5826 lfsck_namespace_assistant_req_fini(env, lar);
5828 RETURN(rc == 0 ? 1 : rc);
5832 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
5833 struct lfsck_component *com,
5834 struct dt_object *obj, bool first)
5836 struct lfsck_instance *lfsck = com->lc_lfsck;
5837 struct ptlrpc_thread *thread = &lfsck->li_thread;
5838 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5839 struct lfsck_namespace *ns = com->lc_file_ram;
5840 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5841 struct dt_object *target;
5849 di = iops->init(env, obj, 0, BYPASS_CAPA);
5851 RETURN(PTR_ERR(di));
5854 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5857 rc = iops->get(env, di, (const struct dt_key *)&fid);
5862 /* The start one either has been processed or does not exist,
5864 rc = iops->next(env, di);
5870 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5871 unlikely(!thread_is_running(thread)))
5874 key = iops->key(env, di);
5883 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5884 if (!fid_is_sane(&fid)) {
5889 target = lfsck_object_find_by_dev(env, lfsck->li_bottom, &fid);
5890 if (IS_ERR(target)) {
5891 rc = PTR_ERR(target);
5895 if (dt_object_exists(target)) {
5896 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5898 rc = lfsck_namespace_double_scan_one(env, com,
5905 lfsck_object_put(env, target);
5908 down_write(&com->lc_sem);
5909 com->lc_new_checked++;
5910 com->lc_new_scanned++;
5911 if (rc >= 0 && fid_is_sane(&fid))
5912 ns->ln_fid_latest_scanned_phase2 = fid;
5914 ns->ln_objs_repaired_phase2++;
5916 ns->ln_objs_failed_phase2++;
5917 up_write(&com->lc_sem);
5919 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5922 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
5923 cfs_time_current())) &&
5924 com->lc_new_checked != 0) {
5925 down_write(&com->lc_sem);
5926 ns->ln_run_time_phase2 +=
5927 cfs_duration_sec(cfs_time_current() +
5928 HALF_SEC - com->lc_time_last_checkpoint);
5929 ns->ln_time_last_checkpoint = cfs_time_current_sec();
5930 ns->ln_objs_checked_phase2 += com->lc_new_checked;
5931 com->lc_new_checked = 0;
5932 rc = lfsck_namespace_store(env, com, false);
5933 up_write(&com->lc_sem);
5937 com->lc_time_last_checkpoint = cfs_time_current();
5938 com->lc_time_next_checkpoint =
5939 com->lc_time_last_checkpoint +
5940 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5943 lfsck_control_speed_by_self(com);
5944 if (unlikely(!thread_is_running(thread)))
5947 rc = iops->next(env, di);
5956 iops->fini(env, di);
5961 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5962 struct lfsck_component *com)
5964 struct lfsck_instance *lfsck = com->lc_lfsck;
5965 struct lfsck_namespace *ns = com->lc_file_ram;
5970 while (!list_empty(&lfsck->li_list_lmv)) {
5971 struct lfsck_lmv_unit *llu;
5973 spin_lock(&lfsck->li_lock);
5974 llu = list_entry(lfsck->li_list_lmv.next,
5975 struct lfsck_lmv_unit, llu_link);
5976 list_del_init(&llu->llu_link);
5977 spin_unlock(&lfsck->li_lock);
5979 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
5984 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
5985 lfsck_lfsck2name(lfsck));
5987 lfsck_namespace_scan_local_lpf(env, com);
5989 com->lc_new_checked = 0;
5990 com->lc_new_scanned = 0;
5991 com->lc_time_last_checkpoint = cfs_time_current();
5992 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
5993 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5995 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
5996 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
5997 com->lc_sub_trace_objs[i].lsto_obj, true);
5998 while (rc > 0 && ++i < LFSCK_STF_COUNT)
5999 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6000 com->lc_sub_trace_objs[i].lsto_obj, false);
6002 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6003 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6008 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6009 struct lfsck_component *com,
6010 struct lfsck_position *pos)
6012 struct lfsck_assistant_data *lad = com->lc_data;
6013 struct lfsck_namespace_req *lnr;
6015 if (list_empty(&lad->lad_req_list))
6018 lnr = list_entry(lad->lad_req_list.next,
6019 struct lfsck_namespace_req,
6021 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6022 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6023 pos->lp_dir_parent = lnr->lnr_lar.lar_fid;
6026 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6027 struct lfsck_component *com,
6030 struct lfsck_instance *lfsck = com->lc_lfsck;
6031 struct lfsck_namespace *ns = com->lc_file_ram;
6033 down_write(&com->lc_sem);
6034 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6035 HALF_SEC - lfsck->li_time_last_checkpoint);
6036 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6037 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6038 com->lc_new_checked = 0;
6041 if (ns->ln_flags & LF_INCOMPLETE)
6042 ns->ln_status = LS_PARTIAL;
6044 ns->ln_status = LS_COMPLETED;
6045 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6046 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6047 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6048 ns->ln_success_count++;
6049 } else if (rc == 0) {
6050 if (lfsck->li_status != 0)
6051 ns->ln_status = lfsck->li_status;
6053 ns->ln_status = LS_STOPPED;
6055 ns->ln_status = LS_FAILED;
6058 rc = lfsck_namespace_store(env, com, false);
6059 up_write(&com->lc_sem);
6065 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6066 struct ptlrpc_request *req,
6070 struct lfsck_async_interpret_args *laia = args;
6071 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6073 ltd->ltd_synced_failures = 1;
6080 * Notify remote LFSCK instances about former failures.
6082 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6083 * some LFSCK verification requests (maybe because of network issues or the MDT
6084 * itself trouble). During the respond gap the MDT may missed some name entries
6085 * verification, then the MDT cannot know whether related MDT-objects have been
6086 * referenced by related name entries or not, then in the second-stage scanning,
6087 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6088 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6089 * name entry for repairing the orphan.
6091 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6092 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6093 * have ever missed some name entries verification and should skip the handling
6094 * for orphan MDT-objects.
6096 * \param[in] env pointer to the thread context
6097 * \param[in] com pointer to the lfsck component
6098 * \param[in] lr pointer to the lfsck request
6100 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6101 struct lfsck_component *com,
6102 struct lfsck_request *lr)
6104 struct lfsck_async_interpret_args *laia =
6105 &lfsck_env_info(env)->lti_laia2;
6106 struct lfsck_assistant_data *lad = com->lc_data;
6107 struct lfsck_namespace *ns = com->lc_file_ram;
6108 struct lfsck_instance *lfsck = com->lc_lfsck;
6109 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6110 struct lfsck_tgt_desc *ltd;
6111 struct ptlrpc_request_set *set;
6116 if (!lad->lad_incomplete)
6119 set = ptlrpc_prep_set();
6121 GOTO(out, rc = -ENOMEM);
6123 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6124 memset(laia, 0, sizeof(*laia));
6125 lad->lad_touch_gen++;
6127 down_read(<ds->ltd_rw_sem);
6128 cfs_foreach_bit(lad->lad_bitmap, idx) {
6129 ltd = LTD_TGT(ltds, idx);
6130 LASSERT(ltd != NULL);
6132 laia->laia_ltd = ltd;
6133 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6134 lfsck_namespace_assistant_sync_failures_interpret,
6135 laia, LFSCK_NOTIFY);
6137 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6138 "to sync failure with MDT %x: rc = %d\n",
6139 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6141 up_read(<ds->ltd_rw_sem);
6143 rc = ptlrpc_set_wait(set);
6144 ptlrpc_set_destroy(set);
6150 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6151 "to sync failure with MDTs, and related MDTs "
6152 "may handle orphan improperly: rc = %d\n",
6153 lfsck_lfsck2name(lfsck), rc);
6158 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6159 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6160 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6161 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6162 .la_double_scan_result = lfsck_namespace_double_scan_result,
6163 .la_req_fini = lfsck_namespace_assistant_req_fini,
6164 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6168 * Verify the specified linkEA entry for the given directory object.
6169 * If the object has no such linkEA entry or it has more other linkEA
6170 * entries, then re-generate the linkEA with the given information.
6172 * \param[in] env pointer to the thread context
6173 * \param[in] dev pointer to the dt_device
6174 * \param[in] obj pointer to the dt_object to be handled
6175 * \param[in] cname the name for the child in the parent directory
6176 * \param[in] pfid the parent directory's FID for the linkEA
6178 * \retval 0 for success
6179 * \retval negative error number on failure
6181 int lfsck_verify_linkea(const struct lu_env *env, struct dt_device *dev,
6182 struct dt_object *obj, const struct lu_name *cname,
6183 const struct lu_fid *pfid)
6185 struct linkea_data ldata = { NULL };
6186 struct lu_buf linkea_buf;
6189 int fl = LU_XATTR_CREATE;
6193 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6195 rc = lfsck_links_read(env, obj, &ldata);
6196 if (rc == -ENODATA) {
6198 } else if (rc == 0) {
6199 fl = LU_XATTR_REPLACE;
6200 if (ldata.ld_leh->leh_reccount != 1) {
6203 rc = linkea_links_find(&ldata, cname, pfid);
6212 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6216 rc = linkea_add_buf(&ldata, cname, pfid);
6220 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6221 ldata.ld_leh->leh_len);
6222 th = dt_trans_create(env, dev);
6224 RETURN(PTR_ERR(th));
6226 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6227 XATTR_NAME_LINK, fl, th);
6231 rc = dt_trans_start_local(env, dev, th);
6235 dt_write_lock(env, obj, 0);
6236 rc = dt_xattr_set(env, obj, &linkea_buf,
6237 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6238 dt_write_unlock(env, obj);
6243 dt_trans_stop(env, dev, th);
6248 * Get the name and parent directory's FID from the first linkEA entry.
6250 * \param[in] env pointer to the thread context
6251 * \param[in] obj pointer to the object which get linkEA from
6252 * \param[out] name pointer to the buffer to hold the name
6253 * in the first linkEA entry
6254 * \param[out] pfid pointer to the buffer to hold the parent
6255 * directory's FID in the first linkEA entry
6257 * \retval 0 for success
6258 * \retval negative error number on failure
6260 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6261 char *name, struct lu_fid *pfid)
6263 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6264 struct linkea_data ldata = { NULL };
6267 rc = lfsck_links_read(env, obj, &ldata);
6271 linkea_first_entry(&ldata);
6272 if (ldata.ld_lee == NULL)
6275 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6276 /* To guarantee the 'name' is terminated with '0'. */
6277 memcpy(name, cname->ln_name, cname->ln_namelen);
6278 name[cname->ln_namelen] = 0;
6284 * Update the object's name entry with the given FID.
6286 * \param[in] env pointer to the thread context
6287 * \param[in] lfsck pointer to the lfsck instance
6288 * \param[in] parent pointer to the parent directory that holds
6290 * \param[in] name the name for the entry to be updated
6291 * \param[in] pfid the new PFID for the name entry
6292 * \param[in] type the type for the name entry to be updated
6294 * \retval 0 for success
6295 * \retval negative error number on failure
6297 int lfsck_update_name_entry(const struct lu_env *env,
6298 struct lfsck_instance *lfsck,
6299 struct dt_object *parent, const char *name,
6300 const struct lu_fid *pfid, __u32 type)
6302 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
6303 struct dt_device *dev = lfsck->li_next;
6304 struct lustre_handle lh = { 0 };
6310 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
6311 MDS_INODELOCK_UPDATE, LCK_EX);
6315 th = dt_trans_create(env, dev);
6317 GOTO(unlock, rc = PTR_ERR(th));
6319 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
6323 rec->rec_type = type;
6324 rec->rec_fid = pfid;
6325 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
6326 (const struct dt_key *)name, th);
6330 rc = dt_declare_ref_add(env, parent, th);
6334 rc = dt_trans_start(env, dev, th);
6338 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
6340 if (rc == -ENOENT) {
6348 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
6349 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6350 if (rc == 0 && S_ISDIR(type) && !exists) {
6351 dt_write_lock(env, parent, 0);
6352 rc = dt_ref_add(env, parent, th);
6353 dt_write_unlock(env, parent);
6359 dt_trans_stop(env, dev, th);
6362 lfsck_ibits_unlock(&lh, LCK_EX);
6364 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6365 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6366 PFID(lfsck_dto2fid(parent)), name, PFID(pfid), type, rc);
6371 int lfsck_namespace_setup(const struct lu_env *env,
6372 struct lfsck_instance *lfsck)
6374 struct lfsck_component *com;
6375 struct lfsck_namespace *ns;
6376 struct dt_object *root = NULL;
6377 struct dt_object *obj;
6382 LASSERT(lfsck->li_master);
6388 INIT_LIST_HEAD(&com->lc_link);
6389 INIT_LIST_HEAD(&com->lc_link_dir);
6390 init_rwsem(&com->lc_sem);
6391 atomic_set(&com->lc_ref, 1);
6392 com->lc_lfsck = lfsck;
6393 com->lc_type = LFSCK_TYPE_NAMESPACE;
6394 com->lc_ops = &lfsck_namespace_ops;
6395 com->lc_data = lfsck_assistant_data_init(
6396 &lfsck_namespace_assistant_ops,
6398 if (com->lc_data == NULL)
6399 GOTO(out, rc = -ENOMEM);
6401 com->lc_file_size = sizeof(struct lfsck_namespace);
6402 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6403 if (com->lc_file_ram == NULL)
6404 GOTO(out, rc = -ENOMEM);
6406 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6407 if (com->lc_file_disk == NULL)
6408 GOTO(out, rc = -ENOMEM);
6410 for (i = 0; i < LFSCK_STF_COUNT; i++)
6411 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6413 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6415 GOTO(out, rc = PTR_ERR(root));
6417 if (unlikely(!dt_try_as_dir(env, root)))
6418 GOTO(out, rc = -ENOTDIR);
6420 obj = local_file_find_or_create(env, lfsck->li_los, root,
6422 S_IFREG | S_IRUGO | S_IWUSR);
6424 GOTO(out, rc = PTR_ERR(obj));
6427 rc = lfsck_namespace_load(env, com);
6429 rc = lfsck_namespace_init(env, com);
6431 rc = lfsck_namespace_reset(env, com, true);
6433 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6437 ns = com->lc_file_ram;
6438 switch (ns->ln_status) {
6443 spin_lock(&lfsck->li_lock);
6444 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6445 spin_unlock(&lfsck->li_lock);
6448 CERROR("%s: unknown lfsck_namespace status %d\n",
6449 lfsck_lfsck2name(lfsck), ns->ln_status);
6451 case LS_SCANNING_PHASE1:
6452 case LS_SCANNING_PHASE2:
6453 /* No need to store the status to disk right now.
6454 * If the system crashed before the status stored,
6455 * it will be loaded back when next time. */
6456 ns->ln_status = LS_CRASHED;
6460 spin_lock(&lfsck->li_lock);
6461 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6462 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6463 spin_unlock(&lfsck->li_lock);
6470 if (root != NULL && !IS_ERR(root))
6471 lu_object_put(env, &root->do_lu);
6473 lfsck_component_cleanup(env, com);
6474 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6475 lfsck_lfsck2name(lfsck), rc);