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