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,
479 const char *name, bool reset)
481 struct lfsck_instance *lfsck = com->lc_lfsck;
482 struct dt_object *obj;
486 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
487 if (rc != 0 && rc != -ENOENT)
491 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
492 S_IFREG | S_IRUGO | S_IWUSR,
498 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
499 struct lfsck_component *com,
502 char *name = lfsck_env_info(env)->lti_key;
503 struct lfsck_sub_trace_obj *lsto;
504 struct dt_object *obj;
508 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
509 i < LFSCK_STF_COUNT; i++, lsto++) {
510 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
511 if (lsto->lsto_obj != NULL) {
515 lfsck_object_put(env, lsto->lsto_obj);
516 lsto->lsto_obj = NULL;
519 obj = lfsck_namespace_load_one_trace_file(env, com,
520 com->lc_lfsck->li_lfsck_dir, name, reset);
524 lsto->lsto_obj = obj;
525 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
533 static int lfsck_namespace_init(const struct lu_env *env,
534 struct lfsck_component *com)
536 struct lfsck_namespace *ns = com->lc_file_ram;
539 memset(ns, 0, sizeof(*ns));
540 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
541 ns->ln_status = LS_INIT;
542 down_write(&com->lc_sem);
543 rc = lfsck_namespace_store(env, com, true);
544 up_write(&com->lc_sem);
546 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
552 * Update the namespace LFSCK trace file for the given @fid
554 * \param[in] env pointer to the thread context
555 * \param[in] com pointer to the lfsck component
556 * \param[in] fid the fid which flags to be updated in the lfsck
558 * \param[in] add true if add new flags, otherwise remove flags
560 * \retval 0 for succeed or nothing to be done
561 * \retval negative error number on failure
563 int lfsck_namespace_trace_update(const struct lu_env *env,
564 struct lfsck_component *com,
565 const struct lu_fid *fid,
566 const __u8 flags, bool add)
568 struct lfsck_instance *lfsck = com->lc_lfsck;
569 struct dt_object *obj;
570 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
571 struct dt_device *dev;
572 struct thandle *th = NULL;
581 if (unlikely(!fid_is_sane(fid)))
584 idx = lfsck_sub_trace_file_fid2idx(fid);
585 obj = com->lc_sub_trace_objs[idx].lsto_obj;
586 dev = lfsck_obj2dev(obj);
587 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
588 fid_cpu_to_be(key, fid);
589 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
590 (const struct dt_key *)key, BYPASS_CAPA);
593 GOTO(unlock, rc = 0);
597 } else if (rc == 0) {
599 if ((old & flags) == flags)
600 GOTO(unlock, rc = 0);
604 if ((old & flags) == 0)
605 GOTO(unlock, rc = 0);
613 th = dt_trans_create(env, dev);
615 GOTO(log, rc = PTR_ERR(th));
618 rc = dt_declare_delete(env, obj,
619 (const struct dt_key *)key, th);
625 rc = dt_declare_insert(env, obj,
626 (const struct dt_rec *)&new,
627 (const struct dt_key *)key, th);
632 rc = dt_trans_start_local(env, dev, th);
637 rc = dt_delete(env, obj, (const struct dt_key *)key,
644 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
645 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
653 if (th != NULL && !IS_ERR(th))
654 dt_trans_stop(env, dev, th);
656 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
657 "trace file, flags %x, old %x, new %x: rc = %d\n",
658 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
659 (__u32)flags, (__u32)old, (__u32)new, rc);
662 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
667 int lfsck_namespace_check_exist(const struct lu_env *env,
668 struct dt_object *dir,
669 struct dt_object *obj, const char *name)
671 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
675 if (unlikely(lfsck_is_dead_obj(obj)))
676 RETURN(LFSCK_NAMEENTRY_DEAD);
678 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
679 (const struct dt_key *)name, BYPASS_CAPA);
681 RETURN(LFSCK_NAMEENTRY_REMOVED);
686 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
687 RETURN(LFSCK_NAMEENTRY_RECREATED);
692 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
693 struct dt_object *obj,
694 struct thandle *handle)
698 /* For destroying all invalid linkEA entries. */
699 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
703 /* For insert new linkEA entry. */
704 rc = dt_declare_xattr_set(env, obj,
705 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
706 XATTR_NAME_LINK, 0, handle);
710 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
711 struct linkea_data *ldata)
715 if (ldata->ld_buf->lb_buf == NULL)
718 if (!dt_object_exists(obj))
721 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
723 /* Buf was too small, figure out what we need. */
724 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
729 lu_buf_realloc(ldata->ld_buf, rc);
730 if (ldata->ld_buf->lb_buf == NULL)
733 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
738 rc = linkea_init(ldata);
744 * Remove linkEA for the given object.
746 * The caller should take the ldlm lock before the calling.
748 * \param[in] env pointer to the thread context
749 * \param[in] com pointer to the lfsck component
750 * \param[in] obj pointer to the dt_object to be handled
752 * \retval 0 for repaired cases
753 * \retval negative error number on failure
755 static int lfsck_namespace_links_remove(const struct lu_env *env,
756 struct lfsck_component *com,
757 struct dt_object *obj)
759 struct lfsck_instance *lfsck = com->lc_lfsck;
760 struct dt_device *dev = lfsck_obj2dev(obj);
761 struct thandle *th = NULL;
765 LASSERT(dt_object_remote(obj) == 0);
767 th = dt_trans_create(env, dev);
769 GOTO(log, rc = PTR_ERR(th));
771 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
775 rc = dt_trans_start_local(env, dev, th);
779 dt_write_lock(env, obj, 0);
780 if (unlikely(lfsck_is_dead_obj(obj)))
781 GOTO(unlock, rc = -ENOENT);
783 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
784 GOTO(unlock, rc = 0);
786 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
791 dt_write_unlock(env, obj);
794 dt_trans_stop(env, dev, th);
797 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
798 "for the object "DFID": rc = %d\n",
799 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
802 struct lfsck_namespace *ns = com->lc_file_ram;
804 ns->ln_flags |= LF_INCONSISTENT;
810 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
811 struct linkea_data *ldata, struct thandle *handle)
813 const struct lu_buf *buf = lfsck_buf_get_const(env,
814 ldata->ld_buf->lb_buf,
815 ldata->ld_leh->leh_len);
817 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
821 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
822 struct lu_name *cname,
826 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
827 /* To guarantee the 'name' is terminated with '0'. */
828 memcpy(buf, cname->ln_name, cname->ln_namelen);
829 buf[cname->ln_namelen] = 0;
830 cname->ln_name = buf;
833 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
834 struct lu_name *cname,
838 struct link_ea_entry *oldlee;
842 oldlee = ldata->ld_lee;
843 oldlen = ldata->ld_reclen;
844 linkea_next_entry(ldata);
845 while (ldata->ld_lee != NULL) {
846 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
847 ldata->ld_lee->lee_reclen[1];
848 if (unlikely(ldata->ld_reclen == oldlen &&
849 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
854 linkea_del_buf(ldata, cname);
856 linkea_next_entry(ldata);
859 ldata->ld_lee = oldlee;
860 ldata->ld_reclen = oldlen;
866 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
868 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
869 * with the given type to generate the name, the detailed rules for name
870 * have been described as following.
872 * The function also generates the linkEA corresponding to the name entry
873 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
875 * \param[in] env pointer to the thread context
876 * \param[in] com pointer to the lfsck component
877 * \param[in] orphan pointer to the orphan MDT-object
878 * \param[in] infix additional information for the orphan name, such as
879 * the FID for original
880 * \param[in] type the type for describing why the orphan MDT-object is
881 * created. The rules are as following:
883 * type "D": The MDT-object is a directory, it may knows its parent
884 * but because there is no valid linkEA, the LFSCK cannot
885 * know where to put it back to the namespace.
886 * type "O": The MDT-object has no linkEA, and there is no name
887 * entry that references the MDT-object.
889 * type "S": The orphan MDT-object is a shard of a striped directory
891 * \see lfsck_layout_recreate_parent() for more types.
893 * The orphan name will be like:
894 * ${FID}-${infix}-${type}-${conflict_version}
896 * \param[out] count if some others inserted some linkEA entries by race,
897 * then return the linkEA entries count.
899 * \retval positive number for repaired cases
900 * \retval 0 if needs to repair nothing
901 * \retval negative error number on failure
903 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
904 struct lfsck_component *com,
905 struct dt_object *orphan,
906 const char *infix, const char *type,
909 struct lfsck_thread_info *info = lfsck_env_info(env);
910 struct lu_name *cname = &info->lti_name;
911 struct dt_insert_rec *rec = &info->lti_dt_rec;
912 struct lu_attr *la = &info->lti_la3;
913 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
914 const struct lu_fid *pfid;
916 struct lfsck_instance *lfsck = com->lc_lfsck;
917 struct dt_device *dev = lfsck_obj2dev(orphan);
918 struct dt_object *parent;
919 struct thandle *th = NULL;
920 struct lustre_handle plh = { 0 };
921 struct lustre_handle clh = { 0 };
922 struct linkea_data ldata = { NULL };
923 struct lu_buf linkea_buf;
930 cname->ln_name = NULL;
931 if (unlikely(lfsck->li_lpf_obj == NULL))
932 GOTO(log, rc = -ENXIO);
934 parent = lfsck->li_lpf_obj;
935 pfid = lfsck_dto2fid(parent);
937 /* Hold update lock on the parent to prevent others to access. */
938 rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
939 MDS_INODELOCK_UPDATE, LCK_EX);
944 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
945 PFID(cfid), infix, type, idx++);
946 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
947 (const struct dt_key *)info->lti_key,
949 if (rc != 0 && rc != -ENOENT)
952 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
954 } while (rc == 0 && !exist);
956 cname->ln_name = info->lti_key;
957 cname->ln_namelen = namelen;
958 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
962 rc = linkea_add_buf(&ldata, cname, pfid);
966 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
967 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
972 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
973 ldata.ld_leh->leh_len);
974 th = dt_trans_create(env, dev);
976 GOTO(log, rc = PTR_ERR(th));
978 if (S_ISDIR(lfsck_object_type(orphan))) {
979 rc = dt_declare_delete(env, orphan,
980 (const struct dt_key *)dotdot, th);
984 rec->rec_type = S_IFDIR;
986 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
987 (const struct dt_key *)dotdot, th);
992 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
993 XATTR_NAME_LINK, 0, th);
998 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1000 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1001 (const struct dt_key *)cname->ln_name,
1006 if (S_ISDIR(rec->rec_type)) {
1007 rc = dt_declare_ref_add(env, parent, th);
1013 memset(la, 0, sizeof(*la));
1014 la->la_ctime = cfs_time_current_sec();
1015 la->la_valid = LA_CTIME;
1016 rc = dt_declare_attr_set(env, orphan, la, th);
1020 rc = dt_trans_start_local(env, dev, th);
1024 dt_write_lock(env, orphan, 0);
1025 rc = lfsck_links_read(env, orphan, &ldata);
1026 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
1027 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
1028 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1029 GOTO(unlock, rc = 1);
1031 if (S_ISDIR(lfsck_object_type(orphan))) {
1032 rc = dt_delete(env, orphan,
1033 (const struct dt_key *)dotdot, th,
1038 rec->rec_type = S_IFDIR;
1039 rec->rec_fid = pfid;
1040 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1041 (const struct dt_key *)dotdot, th,
1047 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1050 if (rc == 0 && count != NULL)
1051 *count = ldata.ld_leh->leh_reccount;
1055 dt_write_unlock(env, orphan);
1057 if (rc == 0 && !exist) {
1058 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1059 rec->rec_fid = cfid;
1060 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1061 (const struct dt_key *)cname->ln_name,
1062 th, BYPASS_CAPA, 1);
1063 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1064 dt_write_lock(env, parent, 0);
1065 rc = dt_ref_add(env, parent, th);
1066 dt_write_unlock(env, parent);
1071 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
1073 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1076 dt_write_unlock(env, orphan);
1079 dt_trans_stop(env, dev, th);
1082 lfsck_ibits_unlock(&clh, LCK_EX);
1083 lfsck_ibits_unlock(&plh, LCK_EX);
1084 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1085 "object "DFID", name = %s: rc = %d\n",
1086 lfsck_lfsck2name(lfsck), PFID(cfid),
1087 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1090 struct lfsck_namespace *ns = com->lc_file_ram;
1092 ns->ln_flags |= LF_INCONSISTENT;
1099 * Add the specified name entry back to namespace.
1101 * If there is a linkEA entry that back references a name entry under
1102 * some parent directory, but such parent directory does not have the
1103 * claimed name entry. On the other hand, the linkEA entries count is
1104 * not larger than the MDT-object's hard link count. Under such case,
1105 * it is quite possible that the name entry is lost. Then the LFSCK
1106 * should add the name entry back to the namespace.
1108 * \param[in] env pointer to the thread context
1109 * \param[in] com pointer to the lfsck component
1110 * \param[in] parent pointer to the directory under which the name entry
1111 * will be inserted into
1112 * \param[in] child pointer to the object referenced by the name entry
1113 * that to be inserted into the parent
1114 * \param[in] name the name for the child in the parent directory
1116 * \retval positive number for repaired cases
1117 * \retval 0 if nothing to be repaired
1118 * \retval negative error number on failure
1120 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1121 struct lfsck_component *com,
1122 struct dt_object *parent,
1123 struct dt_object *child,
1126 struct lfsck_thread_info *info = lfsck_env_info(env);
1127 struct lu_attr *la = &info->lti_la;
1128 struct dt_insert_rec *rec = &info->lti_dt_rec;
1129 struct lfsck_instance *lfsck = com->lc_lfsck;
1130 /* The child and its name may be on different MDTs. */
1131 struct dt_device *dev = lfsck->li_next;
1132 struct dt_object *pobj = NULL;
1133 struct dt_object *cobj = NULL;
1134 struct thandle *th = NULL;
1135 struct lustre_handle lh = { 0 };
1139 /* @parent/@child may be based on lfsck->li_bottom,
1140 * but here we need the object based on the lfsck->li_next. */
1142 pobj = lfsck_object_locate(dev, parent);
1144 GOTO(log, rc = PTR_ERR(pobj));
1146 if (unlikely(!dt_try_as_dir(env, pobj)))
1147 GOTO(log, rc = -ENOTDIR);
1149 cobj = lfsck_object_locate(dev, child);
1151 GOTO(log, rc = PTR_ERR(cobj));
1153 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1156 /* Hold update lock on the pobj to prevent others to access. */
1157 rc = lfsck_ibits_lock(env, lfsck, pobj, &lh,
1158 MDS_INODELOCK_UPDATE, LCK_EX);
1162 th = dt_trans_create(env, dev);
1164 GOTO(unlock, rc = PTR_ERR(th));
1166 rec->rec_type = lfsck_object_type(cobj) & S_IFMT;
1167 rec->rec_fid = lfsck_dto2fid(cobj);
1168 rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1169 (const struct dt_key *)name, th);
1173 if (S_ISDIR(rec->rec_type)) {
1174 rc = dt_declare_ref_add(env, pobj, th);
1179 memset(la, 0, sizeof(*la));
1180 la->la_ctime = cfs_time_current_sec();
1181 la->la_valid = LA_CTIME;
1182 rc = dt_declare_attr_set(env, pobj, la, th);
1186 rc = dt_declare_attr_set(env, cobj, la, th);
1190 rc = dt_trans_start_local(env, dev, th);
1194 rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1195 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1199 if (S_ISDIR(rec->rec_type)) {
1200 dt_write_lock(env, pobj, 0);
1201 rc = dt_ref_add(env, pobj, th);
1202 dt_write_unlock(env, pobj);
1207 la->la_ctime = cfs_time_current_sec();
1208 rc = dt_attr_set(env, pobj, la, th, BYPASS_CAPA);
1212 rc = dt_attr_set(env, cobj, la, th, BYPASS_CAPA);
1214 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1217 dt_trans_stop(env, dev, th);
1220 lfsck_ibits_unlock(&lh, LCK_EX);
1223 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1224 "the name %s and type %o to the parent "DFID": rc = %d\n",
1225 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1226 lfsck_object_type(child) & S_IFMT,
1227 PFID(lfsck_dto2fid(parent)), rc);
1230 struct lfsck_namespace *ns = com->lc_file_ram;
1232 ns->ln_flags |= LF_INCONSISTENT;
1234 ns->ln_lost_dirent_repaired++;
1241 * Create the specified orphan directory.
1243 * For the case that the parent MDT-object stored in some MDT-object's
1244 * linkEA entry is lost, the LFSCK will re-create the parent object as
1245 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1246 * with the name ${FID}-P-${conflict_version}.
1248 * \param[in] env pointer to the thread context
1249 * \param[in] com pointer to the lfsck component
1250 * \param[in] orphan pointer to the orphan MDT-object to be created
1251 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1253 * \retval positive number for repaired cases
1254 * \retval negative error number on failure
1256 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1257 struct lfsck_component *com,
1258 struct dt_object *orphan,
1259 struct lmv_mds_md_v1 *lmv)
1261 struct lfsck_thread_info *info = lfsck_env_info(env);
1262 struct lu_attr *la = &info->lti_la;
1263 struct dt_allocation_hint *hint = &info->lti_hint;
1264 struct dt_object_format *dof = &info->lti_dof;
1265 struct lu_name *cname = &info->lti_name2;
1266 struct dt_insert_rec *rec = &info->lti_dt_rec;
1267 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1268 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1270 struct lfsck_instance *lfsck = com->lc_lfsck;
1271 struct lfsck_namespace *ns = com->lc_file_ram;
1272 struct dt_device *dev = lfsck_obj2dev(orphan);
1273 struct dt_object *parent = NULL;
1274 struct thandle *th = NULL;
1275 struct lustre_handle lh = { 0 };
1276 struct linkea_data ldata = { NULL };
1277 struct lu_buf linkea_buf;
1278 struct lu_buf lmv_buf;
1285 LASSERT(!dt_object_exists(orphan));
1287 cname->ln_name = NULL;
1288 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1291 if (dt_object_remote(orphan)) {
1292 LASSERT(lfsck->li_lpf_root_obj != NULL);
1294 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1296 GOTO(log, rc = idx);
1298 snprintf(name, 8, "MDT%04x", idx);
1299 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1300 (struct dt_rec *)&tfid,
1301 (const struct dt_key *)name, BYPASS_CAPA);
1303 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1305 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1307 GOTO(log, rc = PTR_ERR(parent));
1309 if (unlikely(!dt_try_as_dir(env, parent)))
1310 GOTO(log, rc = -ENOTDIR);
1312 if (unlikely(lfsck->li_lpf_obj == NULL))
1313 GOTO(log, rc = -ENXIO);
1315 parent = lfsck->li_lpf_obj;
1318 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1320 GOTO(log, rc = PTR_ERR(dev));
1322 /* Hold update lock on the parent to prevent others to access. */
1323 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1324 MDS_INODELOCK_UPDATE, LCK_EX);
1330 namelen = snprintf(name, 31, DFID"-P-%d",
1332 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1333 (const struct dt_key *)name, BYPASS_CAPA);
1334 if (rc != 0 && rc != -ENOENT)
1338 cname->ln_name = name;
1339 cname->ln_namelen = namelen;
1341 memset(la, 0, sizeof(*la));
1342 la->la_mode = S_IFDIR | 0700;
1343 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1344 LA_ATIME | LA_MTIME | LA_CTIME;
1346 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1347 la->la_mode & S_IFMT);
1349 memset(dof, 0, sizeof(*dof));
1350 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1352 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1356 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
1360 th = dt_trans_create(env, dev);
1362 GOTO(unlock1, rc = PTR_ERR(th));
1364 /* Sync the remote transaction to guarantee that the subsequent
1365 * lock against the @orphan can find the @orphan in time. */
1366 if (dt_object_remote(orphan))
1369 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1373 if (unlikely(!dt_try_as_dir(env, orphan)))
1374 GOTO(stop, rc = -ENOTDIR);
1376 rec->rec_type = S_IFDIR;
1377 rec->rec_fid = cfid;
1378 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1379 (const struct dt_key *)dot, th);
1383 rec->rec_fid = lfsck_dto2fid(parent);
1384 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1385 (const struct dt_key *)dotdot, th);
1387 rc = dt_declare_ref_add(env, orphan, th);
1392 rc = dt_declare_ref_add(env, orphan, th);
1397 lmv->lmv_magic = LMV_MAGIC;
1398 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1399 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1400 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1401 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1402 XATTR_NAME_LMV, 0, th);
1407 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1408 ldata.ld_leh->leh_len);
1409 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1410 XATTR_NAME_LINK, 0, th);
1414 rec->rec_fid = cfid;
1415 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1416 (const struct dt_key *)name, th);
1418 rc = dt_declare_ref_add(env, parent, th);
1423 rc = dt_trans_start_local(env, dev, th);
1427 dt_write_lock(env, orphan, 0);
1428 rc = dt_create(env, orphan, la, hint, dof, th);
1432 rec->rec_fid = cfid;
1433 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1434 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1438 rec->rec_fid = lfsck_dto2fid(parent);
1439 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1440 (const struct dt_key *)dotdot, th,
1445 rc = dt_ref_add(env, orphan, th);
1450 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0,
1456 rc = dt_xattr_set(env, orphan, &linkea_buf,
1457 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1458 dt_write_unlock(env, orphan);
1462 rec->rec_fid = cfid;
1463 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1464 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1466 dt_write_lock(env, parent, 0);
1467 rc = dt_ref_add(env, parent, th);
1468 dt_write_unlock(env, parent);
1471 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1474 dt_write_unlock(env, orphan);
1477 dt_trans_stop(env, dev, th);
1480 lfsck_ibits_unlock(&lh, LCK_EX);
1483 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1484 "the object "DFID", name = %s: rc = %d\n",
1485 lfsck_lfsck2name(lfsck), PFID(cfid),
1486 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1488 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1489 lfsck_object_put(env, parent);
1492 ns->ln_flags |= LF_INCONSISTENT;
1498 * Remove the specified entry from the linkEA.
1500 * Locate the linkEA entry with the given @cname and @pfid, then
1501 * remove this entry or the other entries those are repeated with
1504 * \param[in] env pointer to the thread context
1505 * \param[in] com pointer to the lfsck component
1506 * \param[in] obj pointer to the dt_object to be handled
1507 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1508 * \param[in] cname the name for the child in the parent directory
1509 * \param[in] pfid the parent directory's FID for the linkEA
1510 * \param[in] next if true, then remove the first found linkEA
1511 * entry, and move the ldata->ld_lee to next entry
1513 * \retval positive number for repaired cases
1514 * \retval 0 if nothing to be repaired
1515 * \retval negative error number on failure
1517 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1518 struct lfsck_component *com,
1519 struct dt_object *obj,
1520 struct linkea_data *ldata,
1521 struct lu_name *cname,
1522 struct lu_fid *pfid,
1525 struct lfsck_instance *lfsck = com->lc_lfsck;
1526 struct dt_device *dev = lfsck_obj2dev(obj);
1527 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1528 struct thandle *th = NULL;
1529 struct lustre_handle lh = { 0 };
1530 struct linkea_data ldata_new = { NULL };
1531 struct lu_buf linkea_buf;
1535 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1536 MDS_INODELOCK_UPDATE |
1537 MDS_INODELOCK_XATTR, LCK_EX);
1542 linkea_del_buf(ldata, cname);
1544 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1546 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1547 ldata->ld_leh->leh_len);
1550 th = dt_trans_create(env, dev);
1552 GOTO(unlock1, rc = PTR_ERR(th));
1554 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1555 XATTR_NAME_LINK, 0, th);
1559 rc = dt_trans_start_local(env, dev, th);
1563 dt_write_lock(env, obj, 0);
1564 if (unlikely(lfsck_is_dead_obj(obj)))
1565 GOTO(unlock2, rc = -ENOENT);
1567 rc = lfsck_links_read2(env, obj, &ldata_new);
1571 /* The specified linkEA entry has been removed by race. */
1572 rc = linkea_links_find(&ldata_new, cname, pfid);
1574 GOTO(unlock2, rc = 0);
1576 if (bk->lb_param & LPF_DRYRUN)
1577 GOTO(unlock2, rc = 1);
1580 linkea_del_buf(&ldata_new, cname);
1582 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1585 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1586 dt_write_unlock(env, obj);
1587 dt_trans_stop(env, dev, th);
1588 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1589 ldata_new.ld_leh->leh_len);
1593 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1594 ldata_new.ld_leh->leh_len);
1595 rc = dt_xattr_set(env, obj, &linkea_buf,
1596 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1598 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1601 dt_write_unlock(env, obj);
1604 dt_trans_stop(env, dev, th);
1607 lfsck_ibits_unlock(&lh, LCK_EX);
1610 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1611 "for the object: "DFID", parent "DFID", name %.*s\n",
1612 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1613 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1617 struct lfsck_namespace *ns = com->lc_file_ram;
1619 ns->ln_flags |= LF_INCONSISTENT;
1626 * Conditionally remove the specified entry from the linkEA.
1628 * Take the parent lock firstly, then check whether the specified
1629 * name entry exists or not: if yes, do nothing; otherwise, call
1630 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1632 * \param[in] env pointer to the thread context
1633 * \param[in] com pointer to the lfsck component
1634 * \param[in] parent pointer to the parent directory
1635 * \param[in] child pointer to the child object that holds the linkEA
1636 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1637 * \param[in] cname the name for the child in the parent directory
1638 * \param[in] pfid the parent directory's FID for the linkEA
1640 * \retval positive number for repaired cases
1641 * \retval 0 if nothing to be repaired
1642 * \retval negative error number on failure
1644 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1645 struct lfsck_component *com,
1646 struct dt_object *parent,
1647 struct dt_object *child,
1648 struct linkea_data *ldata,
1649 struct lu_name *cname,
1650 struct lu_fid *pfid)
1652 struct lu_fid *cfid = &lfsck_env_info(env)->lti_fid3;
1653 struct lustre_handle lh = { 0 };
1657 rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1658 MDS_INODELOCK_UPDATE, LCK_EX);
1662 dt_read_lock(env, parent, 0);
1663 if (unlikely(lfsck_is_dead_obj(parent))) {
1664 dt_read_unlock(env, parent);
1665 lfsck_ibits_unlock(&lh, LCK_EX);
1666 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1672 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1673 (const struct dt_key *)cname->ln_name,
1675 dt_read_unlock(env, parent);
1677 /* It is safe to release the ldlm lock, because when the logic come
1678 * here, we have got all the needed information above whether the
1679 * linkEA entry is valid or not. It is not important that others
1680 * may add new linkEA entry after the ldlm lock released. If other
1681 * has removed the specified linkEA entry by race, then it is OK,
1682 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1684 lfsck_ibits_unlock(&lh, LCK_EX);
1685 if (rc == -ENOENT) {
1686 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1695 /* The LFSCK just found some internal status of cross-MDTs
1696 * create operation. That is normal. */
1697 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1698 linkea_next_entry(ldata);
1703 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1710 * Conditionally replace name entry in the parent.
1712 * As required, the LFSCK may re-create the lost MDT-object for dangling
1713 * name entry, but such repairing may be wrong because of bad FID in the
1714 * name entry. As the LFSCK processing, the real MDT-object may be found,
1715 * then the LFSCK should check whether the former re-created MDT-object
1716 * has been modified or not, if not, then destroy it and update the name
1717 * entry in the parent to reference the real MDT-object.
1719 * \param[in] env pointer to the thread context
1720 * \param[in] com pointer to the lfsck component
1721 * \param[in] parent pointer to the parent directory
1722 * \param[in] child pointer to the MDT-object that may be the real
1723 * MDT-object corresponding to the name entry in parent
1724 * \param[in] cfid the current FID in the name entry
1725 * \param[in] cname contains the name of the child in the parent directory
1727 * \retval positive number for repaired cases
1728 * \retval 0 if nothing to be repaired
1729 * \retval negative error number on failure
1731 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1732 struct lfsck_component *com,
1733 struct dt_object *parent,
1734 struct dt_object *child,
1735 const struct lu_fid *cfid,
1736 const struct lu_name *cname)
1738 struct lfsck_thread_info *info = lfsck_env_info(env);
1739 struct lu_attr *la = &info->lti_la;
1740 struct dt_insert_rec *rec = &info->lti_dt_rec;
1742 struct lfsck_instance *lfsck = com->lc_lfsck;
1743 /* The child and its name may be on different MDTs. */
1744 struct dt_device *dev = lfsck->li_next;
1745 const char *name = cname->ln_name;
1746 struct dt_object *pobj = NULL;
1747 struct dt_object *cobj = NULL;
1748 struct lustre_handle plh = { 0 };
1749 struct lustre_handle clh = { 0 };
1750 struct linkea_data ldata = { NULL };
1751 struct thandle *th = NULL;
1756 /* @parent/@child may be based on lfsck->li_bottom,
1757 * but here we need the object based on the lfsck->li_next. */
1759 pobj = lfsck_object_locate(dev, parent);
1761 GOTO(log, rc = PTR_ERR(pobj));
1763 if (unlikely(!dt_try_as_dir(env, pobj)))
1764 GOTO(log, rc = -ENOTDIR);
1766 /* Hold update lock on the pobj to prevent others to access. */
1767 rc = lfsck_ibits_lock(env, lfsck, pobj, &plh,
1768 MDS_INODELOCK_UPDATE, LCK_EX);
1772 if (!fid_is_sane(cfid)) {
1777 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1780 if (rc == -ENOENT) {
1788 if (!dt_object_exists(cobj)) {
1793 rc = dt_lookup(env, pobj, (struct dt_rec *)&tfid,
1794 (const struct dt_key *)name, BYPASS_CAPA);
1795 if (rc == -ENOENT) {
1803 /* Someone changed the name entry, cannot replace it. */
1804 if (!lu_fid_eq(cfid, &tfid))
1807 /* lock the object to be destroyed. */
1808 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1809 MDS_INODELOCK_UPDATE |
1810 MDS_INODELOCK_XATTR, LCK_EX);
1814 if (unlikely(lfsck_is_dead_obj(cobj))) {
1819 rc = dt_attr_get(env, cobj, la, BYPASS_CAPA);
1823 /* The object has been modified by other(s), or it is not created by
1824 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1825 if (la->la_ctime != 0)
1828 if (S_ISREG(la->la_mode)) {
1829 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV,
1831 /* If someone has created related OST-object(s),
1833 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1834 GOTO(log, rc = (rc > 0 ? 0 : rc));
1838 dt_read_lock(env, child, 0);
1839 rc = lfsck_links_read2(env, child, &ldata);
1840 dt_read_unlock(env, child);
1842 /* Someone changed the child, no need to replace. */
1849 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(pobj));
1850 /* Someone moved the child, no need to replace. */
1854 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1857 th = dt_trans_create(env, dev);
1859 GOTO(log, rc = PTR_ERR(th));
1862 rc = dt_declare_destroy(env, cobj, th);
1867 rc = dt_declare_delete(env, pobj, (const struct dt_key *)name, th);
1871 rec->rec_type = S_IFDIR;
1872 rec->rec_fid = lfsck_dto2fid(child);
1873 rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1874 (const struct dt_key *)name, th);
1878 rc = dt_trans_start_local(env, dev, th);
1883 rc = dt_destroy(env, cobj, th);
1888 /* The old name entry maybe not exist. */
1889 rc = dt_delete(env, pobj, (const struct dt_key *)name, th, BYPASS_CAPA);
1890 if (rc != 0 && rc != -ENOENT)
1893 rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1894 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1896 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1899 dt_trans_stop(env, dev, th);
1902 lfsck_ibits_unlock(&clh, LCK_EX);
1903 lfsck_ibits_unlock(&plh, LCK_EX);
1905 if (cobj != NULL && !IS_ERR(cobj))
1906 lfsck_object_put(env, cobj);
1908 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1909 "object "DFID" because of conflict with the object "DFID
1910 " under the parent "DFID" with name %s: rc = %d\n",
1911 lfsck_lfsck2name(lfsck), PFID(cfid),
1912 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1919 * Overwrite the linkEA for the object with the given ldata.
1921 * The caller should take the ldlm lock before the calling.
1923 * \param[in] env pointer to the thread context
1924 * \param[in] com pointer to the lfsck component
1925 * \param[in] obj pointer to the dt_object to be handled
1926 * \param[in] ldata pointer to the new linkEA data
1928 * \retval positive number for repaired cases
1929 * \retval 0 if nothing to be repaired
1930 * \retval negative error number on failure
1932 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1933 struct lfsck_component *com,
1934 struct dt_object *obj,
1935 struct linkea_data *ldata)
1937 struct lfsck_instance *lfsck = com->lc_lfsck;
1938 struct dt_device *dev = lfsck_obj2dev(obj);
1939 struct thandle *th = NULL;
1940 struct lu_buf linkea_buf;
1944 th = dt_trans_create(env, dev);
1946 GOTO(log, rc = PTR_ERR(th));
1948 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1949 ldata->ld_leh->leh_len);
1950 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1951 XATTR_NAME_LINK, 0, th);
1955 rc = dt_trans_start_local(env, dev, th);
1959 dt_write_lock(env, obj, 0);
1960 if (unlikely(lfsck_is_dead_obj(obj)))
1961 GOTO(unlock, rc = 0);
1963 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1964 GOTO(unlock, rc = 1);
1966 rc = dt_xattr_set(env, obj, &linkea_buf,
1967 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1969 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1972 dt_write_unlock(env, obj);
1975 dt_trans_stop(env, dev, th);
1978 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1979 "object "DFID": rc = %d\n",
1980 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1983 struct lfsck_namespace *ns = com->lc_file_ram;
1985 ns->ln_flags |= LF_INCONSISTENT;
1992 * Repair invalid name entry.
1994 * If the name entry contains invalid information, such as bad file type
1995 * or (and) corrupted object FID, then either remove the name entry or
1996 * udpate the name entry with the given (right) information.
1998 * \param[in] env pointer to the thread context
1999 * \param[in] com pointer to the lfsck component
2000 * \param[in] parent pointer to the parent directory
2001 * \param[in] child pointer to the object referenced by the name entry
2002 * \param[in] name the old name of the child under the parent directory
2003 * \param[in] name2 the new name of the child under the parent directory
2004 * \param[in] type the type claimed by the name entry
2005 * \param[in] update update the name entry if true; otherwise, remove it
2006 * \param[in] dec decrease the parent nlink count if true
2008 * \retval positive number for repaired successfully
2009 * \retval 0 if nothing to be repaired
2010 * \retval negative error number on failure
2012 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2013 struct lfsck_component *com,
2014 struct dt_object *parent,
2015 struct dt_object *child,
2016 const char *name, const char *name2,
2017 __u16 type, bool update, bool dec)
2019 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
2020 const struct lu_fid *cfid = lfsck_dto2fid(child);
2022 struct lfsck_instance *lfsck = com->lc_lfsck;
2023 struct dt_device *dev = lfsck_obj2dev(parent);
2024 struct thandle *th = NULL;
2025 struct lustre_handle lh = { 0 };
2029 if (unlikely(!dt_try_as_dir(env, parent)))
2030 GOTO(log, rc = -ENOTDIR);
2032 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2033 MDS_INODELOCK_UPDATE, LCK_EX);
2037 th = dt_trans_create(env, dev);
2039 GOTO(unlock1, rc = PTR_ERR(th));
2041 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2046 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2047 rec->rec_fid = cfid;
2048 rc = dt_declare_insert(env, parent,
2049 (const struct dt_rec *)rec,
2050 (const struct dt_key *)name2, th);
2056 rc = dt_declare_ref_del(env, parent, th);
2061 rc = dt_trans_start_local(env, dev, th);
2065 dt_write_lock(env, parent, 0);
2066 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2067 (const struct dt_key *)name, BYPASS_CAPA);
2068 /* Someone has removed the bad name entry by race. */
2070 GOTO(unlock2, rc = 0);
2075 /* Someone has removed the bad name entry and reused it for other
2076 * object by race. */
2077 if (!lu_fid_eq(&tfid, cfid))
2078 GOTO(unlock2, rc = 0);
2080 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2081 GOTO(unlock2, rc = 1);
2083 rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2089 rc = dt_insert(env, parent,
2090 (const struct dt_rec *)rec,
2091 (const struct dt_key *)name2, th,
2098 rc = dt_ref_del(env, parent, th);
2103 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2106 dt_write_unlock(env, parent);
2109 dt_trans_stop(env, dev, th);
2111 /* We are not sure whether the child will become orphan or not.
2112 * Record it in the LFSCK trace file for further checking in
2113 * the second-stage scanning. */
2114 if (!update && !dec && rc == 0)
2115 lfsck_namespace_trace_update(env, com, cfid,
2116 LNTF_CHECK_LINKEA, true);
2119 lfsck_ibits_unlock(&lh, LCK_EX);
2122 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2123 "entry for: parent "DFID", child "DFID", name %s, type "
2124 "in name entry %o, type claimed by child %o. repair it "
2125 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2126 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2127 name, type, update ? lfsck_object_type(child) : 0,
2128 update ? "updating" : "removing", name2, rc);
2131 struct lfsck_namespace *ns = com->lc_file_ram;
2133 ns->ln_flags |= LF_INCONSISTENT;
2140 * Update the ".." name entry for the given object.
2142 * The object's ".." is corrupted, this function will update the ".." name
2143 * entry with the given pfid, and the linkEA with the given ldata.
2145 * The caller should take the ldlm lock before the calling.
2147 * \param[in] env pointer to the thread context
2148 * \param[in] com pointer to the lfsck component
2149 * \param[in] obj pointer to the dt_object to be handled
2150 * \param[in] pfid the new fid for the object's ".." name entry
2151 * \param[in] cname the name for the @obj in the parent directory
2153 * \retval positive number for repaired cases
2154 * \retval 0 if nothing to be repaired
2155 * \retval negative error number on failure
2157 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2158 struct lfsck_component *com,
2159 struct dt_object *obj,
2160 const struct lu_fid *pfid,
2161 struct lu_name *cname)
2163 struct lfsck_thread_info *info = lfsck_env_info(env);
2164 struct dt_insert_rec *rec = &info->lti_dt_rec;
2165 struct lfsck_instance *lfsck = com->lc_lfsck;
2166 struct dt_device *dev = lfsck_obj2dev(obj);
2167 struct thandle *th = NULL;
2168 struct linkea_data ldata = { NULL };
2169 struct lu_buf linkea_buf;
2173 LASSERT(!dt_object_remote(obj));
2174 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2176 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2180 rc = linkea_add_buf(&ldata, cname, pfid);
2184 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2185 ldata.ld_leh->leh_len);
2187 th = dt_trans_create(env, dev);
2189 GOTO(log, rc = PTR_ERR(th));
2191 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2195 rec->rec_type = S_IFDIR;
2196 rec->rec_fid = pfid;
2197 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2198 (const struct dt_key *)dotdot, th);
2202 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2203 XATTR_NAME_LINK, 0, th);
2207 rc = dt_trans_start_local(env, dev, th);
2211 dt_write_lock(env, obj, 0);
2212 if (unlikely(lfsck_is_dead_obj(obj)))
2213 GOTO(unlock, rc = 0);
2215 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2216 GOTO(unlock, rc = 1);
2218 /* The old ".." name entry maybe not exist. */
2219 dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2222 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2223 (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2227 rc = dt_xattr_set(env, obj, &linkea_buf,
2228 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2230 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2233 dt_write_unlock(env, obj);
2236 dt_trans_stop(env, dev, th);
2239 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2240 "the object "DFID", new parent "DFID": rc = %d\n",
2241 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2245 struct lfsck_namespace *ns = com->lc_file_ram;
2247 ns->ln_flags |= LF_INCONSISTENT;
2254 * Handle orphan @obj during Double Scan Directory.
2256 * Remove the @obj's current (invalid) linkEA entries, and insert
2257 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2258 * ${FID}-${PFID}-D-${conflict_version}
2260 * The caller should take the ldlm lock before the calling.
2262 * \param[in] env pointer to the thread context
2263 * \param[in] com pointer to the lfsck component
2264 * \param[in] obj pointer to the orphan object to be handled
2265 * \param[in] pfid the new fid for the object's ".." name entry
2266 * \param[in,out] lh ldlm lock handler for the given @obj
2267 * \param[out] type to tell the caller what the inconsistency is
2269 * \retval positive number for repaired cases
2270 * \retval 0 if nothing to be repaired
2271 * \retval negative error number on failure
2274 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2275 struct lfsck_component *com,
2276 struct dt_object *obj,
2277 const struct lu_fid *pfid,
2278 struct lustre_handle *lh,
2279 enum lfsck_namespace_inconsistency_type *type)
2281 struct lfsck_thread_info *info = lfsck_env_info(env);
2282 struct lfsck_namespace *ns = com->lc_file_ram;
2286 /* Remove the unrecognized linkEA. */
2287 rc = lfsck_namespace_links_remove(env, com, obj);
2288 lfsck_ibits_unlock(lh, LCK_EX);
2289 if (rc < 0 && rc != -ENODATA)
2292 *type = LNIT_MUL_REF;
2294 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2295 * ever tried to verify some remote MDT-object that resides on this
2296 * MDT, but this MDT failed to respond such request. So means there
2297 * may be some remote name entry on other MDT that references this
2298 * object with another name, so we cannot know whether this linkEA
2299 * is valid or not. So keep it there and maybe resolved when next
2301 if (ns->ln_flags & LF_INCOMPLETE)
2304 /* The unique linkEA is invalid, even if the ".." name entry may be
2305 * valid, we still cannot know via which name entry this directory
2306 * will be referenced. Then handle it as pure orphan. */
2307 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2308 "-"DFID, PFID(pfid));
2309 rc = lfsck_namespace_insert_orphan(env, com, obj,
2310 info->lti_tmpbuf, "D", NULL);
2316 * Double Scan Directory object for single linkEA entry case.
2318 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2319 * then check whether the name is in the namespace or not, if not, add the
2320 * missing name entry back to namespace. If the linkEA entry is invalid,
2321 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2324 * \param[in] env pointer to the thread context
2325 * \param[in] com pointer to the lfsck component
2326 * \param[in] child pointer to the directory to be double scanned
2327 * \param[in] pfid the FID corresponding to the ".." entry
2328 * \param[in] ldata pointer to the linkEA data for the given @child
2329 * \param[in,out] lh ldlm lock handler for the given @child
2330 * \param[out] type to tell the caller what the inconsistency is
2331 * \param[in] retry if found inconsistency, but the caller does not hold
2332 * ldlm lock on the @child, then set @retry as true
2334 * \retval positive number for repaired cases
2335 * \retval 0 if nothing to be repaired
2336 * \retval negative error number on failure
2339 lfsck_namespace_dsd_single(const struct lu_env *env,
2340 struct lfsck_component *com,
2341 struct dt_object *child,
2342 const struct lu_fid *pfid,
2343 struct linkea_data *ldata,
2344 struct lustre_handle *lh,
2345 enum lfsck_namespace_inconsistency_type *type,
2348 struct lfsck_thread_info *info = lfsck_env_info(env);
2349 struct lu_name *cname = &info->lti_name;
2350 const struct lu_fid *cfid = lfsck_dto2fid(child);
2352 struct lfsck_namespace *ns = com->lc_file_ram;
2353 struct lfsck_instance *lfsck = com->lc_lfsck;
2354 struct dt_object *parent = NULL;
2355 struct lmv_mds_md_v1 *lmv;
2359 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid, info->lti_key);
2360 /* The unique linkEA entry with bad parent will be handled as orphan. */
2361 if (!fid_is_sane(&tfid)) {
2362 if (!lustre_handle_is_used(lh) && retry != NULL)
2365 rc = lfsck_namespace_dsd_orphan(env, com, child,
2371 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2373 GOTO(out, rc = PTR_ERR(parent));
2375 /* We trust the unique linkEA entry in spite of whether it matches the
2376 * ".." name entry or not. Because even if the linkEA entry is wrong
2377 * and the ".." name entry is right, we still cannot know via which
2378 * name entry the child will be referenced, since all known entries
2379 * have been verified during the first-stage scanning. */
2380 if (!dt_object_exists(parent)) {
2381 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2382 * has ever tried to verify some remote MDT-object that resides
2383 * on this MDT, but this MDT failed to respond such request. So
2384 * means there may be some remote name entry on other MDT that
2385 * references this object with another name, so we cannot know
2386 * whether this linkEA is valid or not. So keep it there and
2387 * maybe resolved when next LFSCK run. */
2388 if (ns->ln_flags & LF_INCOMPLETE)
2391 if (!lustre_handle_is_used(lh) && retry != NULL) {
2397 lfsck_ibits_unlock(lh, LCK_EX);
2400 lmv = &info->lti_lmv;
2401 rc = lfsck_read_stripe_lmv(env, child, lmv);
2402 if (rc != 0 && rc != -ENODATA)
2405 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2407 } else if (lfsck_shard_name_to_index(env,
2408 cname->ln_name, cname->ln_namelen,
2409 S_IFDIR, cfid) < 0) {
2410 /* It is an invalid name entry, we
2411 * cannot trust the parent also. */
2412 rc = lfsck_namespace_shrink_linkea(env, com, child,
2413 ldata, cname, &tfid, true);
2417 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2418 "-"DFID, PFID(pfid));
2419 rc = lfsck_namespace_insert_orphan(env, com, child,
2420 info->lti_tmpbuf, "S", NULL);
2425 /* Create the lost parent as an orphan. */
2426 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2428 /* Add the missing name entry to the parent. */
2429 rc = lfsck_namespace_insert_normal(env, com, parent,
2430 child, cname->ln_name);
2431 if (unlikely(rc == -EEXIST)) {
2432 /* Unfortunately, someone reused the name
2433 * under the parent by race. So we have
2434 * to remove the linkEA entry from
2435 * current child object. It means that the
2436 * LFSCK cannot recover the system
2437 * totally back to its original status,
2438 * but it is necessary to make the
2439 * current system to be consistent. */
2440 rc = lfsck_namespace_shrink_linkea(env,
2442 cname, &tfid, true);
2444 snprintf(info->lti_tmpbuf,
2445 sizeof(info->lti_tmpbuf),
2446 "-"DFID, PFID(pfid));
2447 rc = lfsck_namespace_insert_orphan(env,
2448 com, child, info->lti_tmpbuf,
2457 /* The unique linkEA entry with bad parent will be handled as orphan. */
2458 if (unlikely(!dt_try_as_dir(env, parent))) {
2459 if (!lustre_handle_is_used(lh) && retry != NULL)
2462 rc = lfsck_namespace_dsd_orphan(env, com, child,
2468 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2469 (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2470 if (rc == -ENOENT) {
2471 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2472 * has ever tried to verify some remote MDT-object that resides
2473 * on this MDT, but this MDT failed to respond such request. So
2474 * means there may be some remote name entry on other MDT that
2475 * references this object with another name, so we cannot know
2476 * whether this linkEA is valid or not. So keep it there and
2477 * maybe resolved when next LFSCK run. */
2478 if (ns->ln_flags & LF_INCOMPLETE)
2481 if (!lustre_handle_is_used(lh) && retry != NULL) {
2487 lfsck_ibits_unlock(lh, LCK_EX);
2488 rc = lfsck_namespace_check_name(env, parent, child, cname);
2495 /* It is an invalid name entry, drop it. */
2496 if (unlikely(rc > 0)) {
2497 rc = lfsck_namespace_shrink_linkea(env, com, child,
2498 ldata, cname, &tfid, true);
2500 snprintf(info->lti_tmpbuf,
2501 sizeof(info->lti_tmpbuf),
2502 "-"DFID, PFID(pfid));
2503 rc = lfsck_namespace_insert_orphan(env, com,
2504 child, info->lti_tmpbuf, "D", NULL);
2510 /* Add the missing name entry back to the namespace. */
2511 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2513 if (unlikely(rc == -ESTALE))
2514 /* It may happen when the remote object has been
2515 * removed, but the local MDT is not aware of that. */
2518 if (unlikely(rc == -EEXIST)) {
2519 /* Unfortunately, someone reused the name under the
2520 * parent by race. So we have to remove the linkEA
2521 * entry from current child object. It means that the
2522 * LFSCK cannot recover the system totally back to
2523 * its original status, but it is necessary to make
2524 * the current system to be consistent.
2526 * It also may be because of the LFSCK found some
2527 * internal status of create operation. Under such
2528 * case, nothing to be done. */
2529 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2530 parent, child, ldata, cname, &tfid);
2532 snprintf(info->lti_tmpbuf,
2533 sizeof(info->lti_tmpbuf),
2534 "-"DFID, PFID(pfid));
2535 rc = lfsck_namespace_insert_orphan(env, com,
2536 child, info->lti_tmpbuf, "D", NULL);
2546 if (!lu_fid_eq(&tfid, cfid)) {
2547 if (!lustre_handle_is_used(lh) && retry != NULL) {
2553 lfsck_ibits_unlock(lh, LCK_EX);
2554 /* The name entry references another MDT-object that
2555 * may be created by the LFSCK for repairing dangling
2556 * name entry. Try to replace it. */
2557 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2560 rc = lfsck_namespace_dsd_orphan(env, com, child,
2566 if (fid_is_zero(pfid))
2569 /* The ".." name entry is wrong, update it. */
2570 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2571 if (!lustre_handle_is_used(lh) && retry != NULL) {
2577 *type = LNIT_UNMATCHED_PAIRS;
2578 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2579 lfsck_dto2fid(parent), cname);
2585 if (parent != NULL && !IS_ERR(parent))
2586 lfsck_object_put(env, parent);
2592 * Double Scan Directory object for multiple linkEA entries case.
2594 * The given @child has multiple linkEA entries. There is at most one linkEA
2595 * entry will be valid, all the others will be removed. Firstly, the function
2596 * will try to find out the linkEA entry for which the name entry exists under
2597 * the given parent (@pfid). If there is no linkEA entry that matches the given
2598 * ".." name entry, then tries to find out the first linkEA entry that both the
2599 * parent and the name entry exist to rebuild a new ".." name entry.
2601 * \param[in] env pointer to the thread context
2602 * \param[in] com pointer to the lfsck component
2603 * \param[in] child pointer to the directory to be double scanned
2604 * \param[in] pfid the FID corresponding to the ".." entry
2605 * \param[in] ldata pointer to the linkEA data for the given @child
2606 * \param[in,out] lh ldlm lock handler for the given @child
2607 * \param[out] type to tell the caller what the inconsistency is
2608 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2610 * \retval positive number for repaired cases
2611 * \retval 0 if nothing to be repaired
2612 * \retval negative error number on failure
2615 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2616 struct lfsck_component *com,
2617 struct dt_object *child,
2618 const struct lu_fid *pfid,
2619 struct linkea_data *ldata,
2620 struct lustre_handle *lh,
2621 enum lfsck_namespace_inconsistency_type *type,
2624 struct lfsck_thread_info *info = lfsck_env_info(env);
2625 struct lu_name *cname = &info->lti_name;
2626 const struct lu_fid *cfid = lfsck_dto2fid(child);
2627 struct lu_fid *pfid2 = &info->lti_fid3;
2629 struct lfsck_namespace *ns = com->lc_file_ram;
2630 struct lfsck_instance *lfsck = com->lc_lfsck;
2631 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2632 struct dt_object *parent = NULL;
2633 struct linkea_data ldata_new = { NULL };
2634 int dirent_count = 0;
2635 int linkea_count = 0;
2641 while (ldata->ld_lee != NULL) {
2642 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2644 /* Drop repeated linkEA entries. */
2645 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2646 /* Drop invalid linkEA entry. */
2647 if (!fid_is_sane(&tfid)) {
2648 linkea_del_buf(ldata, cname);
2653 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2654 * then it is possible that: the directry object has ever
2655 * been lost, but its name entry was there. In the former
2656 * LFSCK run, during the first-stage scanning, the LFSCK
2657 * found the dangling name entry, but it did not recreate
2658 * the lost object, and when moved to the second-stage
2659 * scanning, some children objects of the lost directory
2660 * object were found, then the LFSCK recreated such lost
2661 * directory object as an orphan.
2663 * When the LFSCK runs again, if the dangling name is still
2664 * there, the LFSCK should move the orphan directory object
2665 * back to the normal namespace. */
2666 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2667 linkea_next_entry(ldata);
2671 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2673 RETURN(PTR_ERR(parent));
2675 if (!dt_object_exists(parent)) {
2676 lfsck_object_put(env, parent);
2677 if (ldata->ld_leh->leh_reccount > 1) {
2678 /* If it is NOT the last linkEA entry, then
2679 * there is still other chance to make the
2680 * child to be visible via other parent, then
2681 * remove this linkEA entry. */
2682 linkea_del_buf(ldata, cname);
2690 /* The linkEA entry with bad parent will be removed. */
2691 if (unlikely(!dt_try_as_dir(env, parent))) {
2692 lfsck_object_put(env, parent);
2693 linkea_del_buf(ldata, cname);
2698 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2699 (const struct dt_key *)cname->ln_name,
2701 *pfid2 = *lfsck_dto2fid(parent);
2702 if (rc == -ENOENT) {
2703 lfsck_object_put(env, parent);
2704 linkea_next_entry(ldata);
2709 lfsck_object_put(env, parent);
2714 if (lu_fid_eq(&tfid, cfid)) {
2715 lfsck_object_put(env, parent);
2716 if (!lu_fid_eq(pfid, pfid2)) {
2717 *type = LNIT_UNMATCHED_PAIRS;
2718 rc = lfsck_namespace_repair_unmatched_pairs(env,
2719 com, child, pfid2, cname);
2725 /* It is the most common case that we find the
2726 * name entry corresponding to the linkEA entry
2727 * that matches the ".." name entry. */
2728 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2732 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2736 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2741 linkea_del_buf(ldata, cname);
2743 linkea_first_entry(ldata);
2744 /* There may be some invalid dangling name entries under
2745 * other parent directories, remove all of them. */
2746 while (ldata->ld_lee != NULL) {
2747 lfsck_namespace_unpack_linkea_entry(ldata,
2748 cname, &tfid, info->lti_key);
2749 if (!fid_is_sane(&tfid))
2752 parent = lfsck_object_find_bottom(env, lfsck,
2754 if (IS_ERR(parent)) {
2755 rc = PTR_ERR(parent);
2756 if (rc != -ENOENT &&
2757 bk->lb_param & LPF_FAILOUT)
2763 if (!dt_object_exists(parent)) {
2764 lfsck_object_put(env, parent);
2768 rc = lfsck_namespace_repair_dirent(env, com,
2769 parent, child, cname->ln_name,
2770 cname->ln_name, S_IFDIR, false, true);
2771 lfsck_object_put(env, parent);
2773 if (bk->lb_param & LPF_FAILOUT)
2782 linkea_del_buf(ldata, cname);
2785 ns->ln_dirent_repaired += dirent_count;
2790 lfsck_ibits_unlock(lh, LCK_EX);
2791 /* The name entry references another MDT-object that may be
2792 * created by the LFSCK for repairing dangling name entry.
2793 * Try to replace it. */
2794 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2796 lfsck_object_put(env, parent);
2803 linkea_del_buf(ldata, cname);
2806 linkea_first_entry(ldata);
2807 if (ldata->ld_leh->leh_reccount == 1) {
2808 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2811 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2812 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2818 /* All linkEA entries are invalid and removed, then handle the @child
2820 if (ldata->ld_leh->leh_reccount == 0) {
2821 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2827 /* If the dangling name entry for the orphan directory object has
2828 * been remvoed, then just check whether the directory object is
2829 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2835 /* There is no linkEA entry that matches the ".." name entry. Find
2836 * the first linkEA entry that both parent and name entry exist to
2837 * rebuild a new ".." name entry. */
2847 * Repair the object's nlink attribute.
2849 * If all the known name entries have been verified, then the object's hard
2850 * link attribute should match the object's linkEA entries count unless the
2851 * object's has too much hard link to be recorded in the linkEA. Such cases
2852 * should have been marked in the LFSCK trace file. Otherwise, trust the
2853 * linkEA to update the object's nlink attribute.
2855 * \param[in] env pointer to the thread context
2856 * \param[in] com pointer to the lfsck component
2857 * \param[in] obj pointer to the dt_object to be handled
2858 * \param[in,out] la pointer to buffer to object's attribute before
2859 * and after the repairing
2861 * \retval positive number for repaired cases
2862 * \retval 0 if nothing to be repaired
2863 * \retval negative error number on failure
2865 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2866 struct lfsck_component *com,
2867 struct dt_object *obj,
2870 struct lfsck_thread_info *info = lfsck_env_info(env);
2871 struct lu_fid *tfid = &info->lti_fid3;
2872 struct lfsck_namespace *ns = com->lc_file_ram;
2873 struct lfsck_instance *lfsck = com->lc_lfsck;
2874 struct dt_device *dev = lfsck_obj2dev(obj);
2875 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2876 struct thandle *th = NULL;
2877 struct linkea_data ldata = { NULL };
2878 struct lustre_handle lh = { 0 };
2879 __u32 old = la->la_nlink;
2885 LASSERT(!dt_object_remote(obj));
2886 LASSERT(S_ISREG(lfsck_object_type(obj)));
2888 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2889 MDS_INODELOCK_UPDATE |
2890 MDS_INODELOCK_XATTR, LCK_EX);
2894 th = dt_trans_create(env, dev);
2896 GOTO(log, rc = PTR_ERR(th));
2898 la->la_valid = LA_NLINK;
2899 rc = dt_declare_attr_set(env, obj, la, th);
2903 rc = dt_trans_start_local(env, dev, th);
2907 dt_write_lock(env, obj, 0);
2908 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2909 * ever tried to verify some remote MDT-object that resides on this
2910 * MDT, but this MDT failed to respond such request. So means there
2911 * may be some remote name entry on other MDT that references this
2912 * object with another name, so we cannot know whether this linkEA
2913 * is valid or not. So keep it there and maybe resolved when next
2915 if (ns->ln_flags & LF_INCOMPLETE)
2916 GOTO(unlock, rc = 0);
2918 fid_cpu_to_be(tfid, cfid);
2919 idx = lfsck_sub_trace_file_fid2idx(cfid);
2920 rc = dt_lookup(env, com->lc_sub_trace_objs[idx].lsto_obj,
2921 (struct dt_rec *)&flags, (const struct dt_key *)tfid,
2926 if (flags & LNTF_SKIP_NLINK)
2927 GOTO(unlock, rc = 0);
2929 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2931 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2933 rc = lfsck_links_read2(env, obj, &ldata);
2935 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2937 if (la->la_nlink == ldata.ld_leh->leh_reccount ||
2938 unlikely(la->la_nlink == 0))
2939 GOTO(unlock, rc = 0);
2941 la->la_nlink = ldata.ld_leh->leh_reccount;
2942 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2943 GOTO(unlock, rc = 1);
2945 rc = dt_attr_set(env, obj, la, th, BYPASS_CAPA);
2947 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2950 dt_write_unlock(env, obj);
2953 dt_trans_stop(env, dev, th);
2956 lfsck_ibits_unlock(&lh, LCK_EX);
2957 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2958 "nlink count from %u to %u: rc = %d\n",
2959 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2962 ns->ln_flags |= LF_INCONSISTENT;
2968 * Double scan the directory object for namespace LFSCK.
2970 * This function will verify the <parent, child> pairs in the namespace tree:
2971 * the parent references the child via some name entry that should be in the
2972 * child's linkEA entry, the child should back references the parent via its
2975 * The LFSCK will scan every linkEA entry in turn until find out the first
2976 * matched pairs. If found, then all other linkEA entries will be dropped.
2977 * If all the linkEA entries cannot match the ".." name entry, then there
2978 * are serveral possible cases:
2980 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2981 * in the linkEA entry is valid.
2983 * 2) If there are multiple linkEA entries, then try to find the linkEA
2984 * that matches the ".." name entry. If found, then all other entries
2985 * are invalid; otherwise, it is quite possible that the ".." name entry
2986 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
2987 * entry according to the first valid linkEA entry (both the parent and
2988 * the name entry should exist).
2990 * 3) If the directory object has no (valid) linkEA entry, then the
2991 * directory object will be handled as pure orphan and inserted
2992 * in the .lustre/lost+found/MDTxxxx/ with the name:
2993 * ${self_FID}-${PFID}-D-${conflict_version}
2995 * \param[in] env pointer to the thread context
2996 * \param[in] com pointer to the lfsck component
2997 * \param[in] child pointer to the directory object to be handled
2998 * \param[in] flags to indicate the specical checking on the @child
3000 * \retval positive number for repaired cases
3001 * \retval 0 if nothing to be repaired
3002 * \retval negative error number on failure
3004 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3005 struct lfsck_component *com,
3006 struct dt_object *child, __u8 flags)
3008 struct lfsck_thread_info *info = lfsck_env_info(env);
3009 const struct lu_fid *cfid = lfsck_dto2fid(child);
3010 struct lu_fid *pfid = &info->lti_fid2;
3011 struct lfsck_namespace *ns = com->lc_file_ram;
3012 struct lfsck_instance *lfsck = com->lc_lfsck;
3013 struct lustre_handle lh = { 0 };
3014 struct linkea_data ldata = { NULL };
3015 bool unknown = false;
3018 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3022 LASSERT(!dt_object_remote(child));
3024 if (flags & LNTF_UNCERTAIN_LMV) {
3025 if (flags & LNTF_RECHECK_NAME_HASH) {
3026 rc = lfsck_namespace_scan_shard(env, com, child);
3030 ns->ln_striped_shards_scanned++;
3032 ns->ln_striped_shards_skipped++;
3036 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3040 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3041 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3042 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3043 "the namespace LFSCK, then the LFSCK cannot guarantee"
3044 "all the name entries have been verified in first-stage"
3045 "scanning. So have to skip orphan related handling for"
3046 "the directory object "DFID" with remote name entry\n",
3047 lfsck_lfsck2name(lfsck), PFID(cfid));
3052 if (unlikely(!dt_try_as_dir(env, child)))
3053 GOTO(out, rc = -ENOTDIR);
3055 /* We only take ldlm lock on the @child when required. When the
3056 * logic comes here for the first time, it is always false. */
3060 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3061 MDS_INODELOCK_UPDATE |
3062 MDS_INODELOCK_XATTR, LCK_EX);
3067 dt_read_lock(env, child, 0);
3068 if (unlikely(lfsck_is_dead_obj(child))) {
3069 dt_read_unlock(env, child);
3074 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3075 (const struct dt_key *)dotdot, BYPASS_CAPA);
3077 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3078 dt_read_unlock(env, child);
3083 if (!lustre_handle_is_used(&lh)) {
3084 dt_read_unlock(env, child);
3089 } else if (lfsck->li_lpf_obj != NULL &&
3090 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3092 } else if (unlikely(!fid_is_sane(pfid))) {
3096 rc = lfsck_links_read(env, child, &ldata);
3097 dt_read_unlock(env, child);
3099 if (rc != -ENODATA && rc != -EINVAL)
3102 if (!lustre_handle_is_used(&lh))
3105 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3106 /* Remove the corrupted linkEA. */
3107 rc = lfsck_namespace_links_remove(env, com, child);
3109 /* Here, because of the crashed linkEA, we
3110 * cannot know whether there is some parent
3111 * that references the child directory via
3112 * some name entry or not. So keep it there,
3113 * when the LFSCK run next time, if there is
3114 * some parent that references this object,
3115 * then the LFSCK can rebuild the linkEA;
3116 * otherwise, this object will be handled
3117 * as orphan as above. */
3120 /* 1. If we have neither ".." nor linkEA,
3121 * then it is an orphan.
3123 * 2. If we only have the ".." name entry,
3124 * but no parent references this child
3125 * directory, then handle it as orphan. */
3126 lfsck_ibits_unlock(&lh, LCK_EX);
3127 type = LNIT_MUL_REF;
3129 /* If the LFSCK is marked as LF_INCOMPLETE,
3130 * then means some MDT has ever tried to
3131 * verify some remote MDT-object that resides
3132 * on this MDT, but this MDT failed to respond
3133 * such request. So means there may be some
3134 * remote name entry on other MDT that
3135 * references this object with another name,
3136 * so we cannot know whether this linkEA is
3137 * valid or not. So keep it there and maybe
3138 * resolved when next LFSCK run. */
3139 if (ns->ln_flags & LF_INCOMPLETE)
3142 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3143 "-"DFID, PFID(pfid));
3144 rc = lfsck_namespace_insert_orphan(env, com, child,
3145 info->lti_tmpbuf, "D", NULL);
3151 linkea_first_entry(&ldata);
3152 /* This is the most common case: the object has unique linkEA entry. */
3153 if (ldata.ld_leh->leh_reccount == 1) {
3154 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3155 &lh, &type, &retry);
3157 LASSERT(!lustre_handle_is_used(&lh));
3166 if (!lustre_handle_is_used(&lh))
3169 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3170 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3176 /* When we come here, the cases usually like that:
3177 * 1) The directory object has a corrupted linkEA entry. During the
3178 * first-stage scanning, the LFSCK cannot know such corruption,
3179 * then it appends the right linkEA entry according to the found
3180 * name entry after the bad one.
3182 * 2) The directory object has a right linkEA entry. During the
3183 * first-stage scanning, the LFSCK finds some bad name entry,
3184 * but the LFSCK cannot aware that at that time, then it adds
3185 * the bad linkEA entry for further processing. */
3186 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3192 lfsck_ibits_unlock(&lh, LCK_EX);
3195 case LNIT_BAD_LINKEA:
3196 ns->ln_linkea_repaired++;
3198 case LNIT_UNMATCHED_PAIRS:
3199 ns->ln_unmatched_pairs_repaired++;
3202 ns->ln_mul_ref_repaired++;
3210 ns->ln_unknown_inconsistency++;
3216 * Double scan the MDT-object for namespace LFSCK.
3218 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3219 * those entries from the linkEA; if the linkEA becomes empty or the object
3220 * has no linkEA, then it is an orphan and will be added into the directory
3221 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3222 * the remote parent; if the name entry corresponding to some linkEA entry
3223 * is lost, then add the name entry back to the namespace.
3225 * \param[in] env pointer to the thread context
3226 * \param[in] com pointer to the lfsck component
3227 * \param[in] child pointer to the dt_object to be handled
3228 * \param[in] flags some hints to indicate how the @child should be handled
3230 * \retval positive number for repaired cases
3231 * \retval 0 if nothing to be repaired
3232 * \retval negative error number on failure
3234 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3235 struct lfsck_component *com,
3236 struct dt_object *child, __u8 flags)
3238 struct lfsck_thread_info *info = lfsck_env_info(env);
3239 struct lu_attr *la = &info->lti_la;
3240 struct lu_name *cname = &info->lti_name;
3241 struct lu_fid *pfid = &info->lti_fid;
3242 struct lu_fid *cfid = &info->lti_fid2;
3243 struct lfsck_instance *lfsck = com->lc_lfsck;
3244 struct lfsck_namespace *ns = com->lc_file_ram;
3245 struct dt_object *parent = NULL;
3246 struct linkea_data ldata = { NULL };
3247 bool repaired = false;
3252 dt_read_lock(env, child, 0);
3253 if (unlikely(lfsck_is_dead_obj(child))) {
3254 dt_read_unlock(env, child);
3259 if (S_ISDIR(lfsck_object_type(child))) {
3260 dt_read_unlock(env, child);
3261 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3266 rc = lfsck_links_read(env, child, &ldata);
3267 dt_read_unlock(env, child);
3271 linkea_first_entry(&ldata);
3272 while (ldata.ld_lee != NULL) {
3273 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3275 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3277 /* Found repeated linkEA entries */
3279 rc = lfsck_namespace_shrink_linkea(env, com, child,
3280 &ldata, cname, pfid, false);
3292 /* Invalid PFID in the linkEA entry. */
3293 if (!fid_is_sane(pfid)) {
3294 rc = lfsck_namespace_shrink_linkea(env, com, child,
3295 &ldata, cname, pfid, true);
3305 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3307 GOTO(out, rc = PTR_ERR(parent));
3309 if (!dt_object_exists(parent)) {
3312 if (ldata.ld_leh->leh_reccount > 1) {
3313 /* If it is NOT the last linkEA entry, then
3314 * there is still other chance to make the
3315 * child to be visible via other parent, then
3316 * remove this linkEA entry. */
3317 rc = lfsck_namespace_shrink_linkea(env, com,
3318 child, &ldata, cname, pfid, true);
3320 /* If the LFSCK is marked as LF_INCOMPLETE,
3321 * then means some MDT has ever tried to
3322 * verify some remote MDT-object that resides
3323 * on this MDT, but this MDT failed to respond
3324 * such request. So means there may be some
3325 * remote name entry on other MDT that
3326 * references this object with another name,
3327 * so we cannot know whether this linkEA is
3328 * valid or not. So keep it there and maybe
3329 * resolved when next LFSCK run. */
3330 if (ns->ln_flags & LF_INCOMPLETE) {
3331 lfsck_object_put(env, parent);
3336 /* Create the lost parent as an orphan. */
3337 rc = lfsck_namespace_create_orphan_dir(env, com,
3340 lfsck_object_put(env, parent);
3348 /* Add the missing name entry to the parent. */
3349 rc = lfsck_namespace_insert_normal(env, com,
3350 parent, child, cname->ln_name);
3351 if (unlikely(rc == -EEXIST))
3352 /* Unfortunately, someone reused the
3353 * name under the parent by race. So we
3354 * have to remove the linkEA entry from
3355 * current child object. It means that
3356 * the LFSCK cannot recover the system
3357 * totally back to its original status,
3358 * but it is necessary to make the
3359 * current system to be consistent. */
3360 rc = lfsck_namespace_shrink_linkea(env,
3364 linkea_next_entry(&ldata);
3367 lfsck_object_put(env, parent);
3377 /* The linkEA entry with bad parent will be removed. */
3378 if (unlikely(!dt_try_as_dir(env, parent))) {
3379 lfsck_object_put(env, parent);
3380 rc = lfsck_namespace_shrink_linkea(env, com, child,
3381 &ldata, cname, pfid, true);
3391 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3392 (const struct dt_key *)cname->ln_name,
3394 if (rc != 0 && rc != -ENOENT) {
3395 lfsck_object_put(env, parent);
3401 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3402 /* It is the most common case that we
3403 * find the name entry corresponding
3404 * to the linkEA entry. */
3405 lfsck_object_put(env, parent);
3406 linkea_next_entry(&ldata);
3408 /* The name entry references another
3409 * MDT-object that may be created by
3410 * the LFSCK for repairing dangling
3411 * name entry. Try to replace it. */
3412 rc = lfsck_namespace_replace_cond(env, com,
3413 parent, child, cfid, cname);
3414 lfsck_object_put(env, parent);
3420 linkea_next_entry(&ldata);
3422 rc = lfsck_namespace_shrink_linkea(env,
3436 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3440 /* If there is no name entry in the parent dir and the object
3441 * link count is less than the linkea entries count, then the
3442 * linkea entry should be removed. */
3443 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3444 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3445 parent, child, &ldata, cname, pfid);
3446 lfsck_object_put(env, parent);
3456 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3457 * MDT has ever tried to verify some remote MDT-object that
3458 * resides on this MDT, but this MDT failed to respond such
3459 * request. So means there may be some remote name entry on
3460 * other MDT that references this object with another name,
3461 * so we cannot know whether this linkEA is valid or not.
3462 * So keep it there and maybe resolved when next LFSCK run. */
3463 if (ns->ln_flags & LF_INCOMPLETE) {
3464 lfsck_object_put(env, parent);
3469 rc = lfsck_namespace_check_name(env, parent, child, cname);
3474 lfsck_object_put(env, parent);
3479 /* It is an invalid name entry, drop it. */
3480 if (unlikely(rc > 0)) {
3481 lfsck_object_put(env, parent);
3482 rc = lfsck_namespace_shrink_linkea(env, com, child,
3483 &ldata, cname, pfid, true);
3493 /* Add the missing name entry back to the namespace. */
3494 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3496 if (unlikely(rc == -ESTALE))
3497 /* It may happen when the remote object has been
3498 * removed, but the local MDT is not aware of that. */
3501 if (unlikely(rc == -EEXIST))
3502 /* Unfortunately, someone reused the name under the
3503 * parent by race. So we have to remove the linkEA
3504 * entry from current child object. It means that the
3505 * LFSCK cannot recover the system totally back to
3506 * its original status, but it is necessary to make
3507 * the current system to be consistent.
3509 * It also may be because of the LFSCK found some
3510 * internal status of create operation. Under such
3511 * case, nothing to be done. */
3512 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3513 parent, child, &ldata, cname, pfid);
3515 linkea_next_entry(&ldata);
3517 lfsck_object_put(env, parent);
3528 if (rc < 0 && rc != -ENODATA)
3532 LASSERT(ldata.ld_leh != NULL);
3534 count = ldata.ld_leh->leh_reccount;
3538 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3539 * MDT has ever tried to verify some remote MDT-object that
3540 * resides on this MDT, but this MDT failed to respond such
3541 * request. So means there may be some remote name entry on
3542 * other MDT that references this object with another name,
3543 * so we cannot know whether this linkEA is valid or not.
3544 * So keep it there and maybe resolved when next LFSCK run. */
3545 if (!(ns->ln_flags & LF_INCOMPLETE)) {
3546 /* If the child becomes orphan, then insert it into
3547 * the global .lustre/lost+found/MDTxxxx directory. */
3548 rc = lfsck_namespace_insert_orphan(env, com, child,
3554 ns->ln_mul_ref_repaired++;
3559 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3563 if (la->la_nlink != 0 && la->la_nlink != count) {
3564 rc = lfsck_namespace_repair_nlink(env, com, child, la);
3566 ns->ln_objs_nlink_repaired++;
3573 if (la->la_nlink > 1)
3574 ns->ln_mul_linked_repaired++;
3583 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3584 struct lfsck_namespace *ns,
3585 __u64 checked_phase1,
3586 __u64 checked_phase2,
3590 seq_printf(m, "checked_phase1: "LPU64"\n"
3591 "checked_phase2: "LPU64"\n"
3592 "updated_phase1: "LPU64"\n"
3593 "updated_phase2: "LPU64"\n"
3594 "failed_phase1: "LPU64"\n"
3595 "failed_phase2: "LPU64"\n"
3596 "directories: "LPU64"\n"
3597 "dirent_repaired: "LPU64"\n"
3598 "linkea_repaired: "LPU64"\n"
3599 "nlinks_repaired: "LPU64"\n"
3600 "multiple_linked_checked: "LPU64"\n"
3601 "multiple_linked_repaired: "LPU64"\n"
3602 "unknown_inconsistency: "LPU64"\n"
3603 "unmatched_pairs_repaired: "LPU64"\n"
3604 "dangling_repaired: "LPU64"\n"
3605 "multiple_referenced_repaired: "LPU64"\n"
3606 "bad_file_type_repaired: "LPU64"\n"
3607 "lost_dirent_repaired: "LPU64"\n"
3608 "local_lost_found_scanned: "LPU64"\n"
3609 "local_lost_found_moved: "LPU64"\n"
3610 "local_lost_found_skipped: "LPU64"\n"
3611 "local_lost_found_failed: "LPU64"\n"
3612 "striped_dirs_scanned: "LPU64"\n"
3613 "striped_dirs_repaired: "LPU64"\n"
3614 "striped_dirs_failed: "LPU64"\n"
3615 "striped_dirs_disabled: "LPU64"\n"
3616 "striped_dirs_skipped: "LPU64"\n"
3617 "striped_shards_scanned: "LPU64"\n"
3618 "striped_shards_repaired: "LPU64"\n"
3619 "striped_shards_failed: "LPU64"\n"
3620 "striped_shards_skipped: "LPU64"\n"
3621 "name_hash_repaired: "LPU64"\n"
3622 "success_count: %u\n"
3623 "run_time_phase1: %u seconds\n"
3624 "run_time_phase2: %u seconds\n",
3627 ns->ln_items_repaired,
3628 ns->ln_objs_repaired_phase2,
3629 ns->ln_items_failed,
3630 ns->ln_objs_failed_phase2,
3631 ns->ln_dirs_checked,
3632 ns->ln_dirent_repaired,
3633 ns->ln_linkea_repaired,
3634 ns->ln_objs_nlink_repaired,
3635 ns->ln_mul_linked_checked,
3636 ns->ln_mul_linked_repaired,
3637 ns->ln_unknown_inconsistency,
3638 ns->ln_unmatched_pairs_repaired,
3639 ns->ln_dangling_repaired,
3640 ns->ln_mul_ref_repaired,
3641 ns->ln_bad_type_repaired,
3642 ns->ln_lost_dirent_repaired,
3643 ns->ln_local_lpf_scanned,
3644 ns->ln_local_lpf_moved,
3645 ns->ln_local_lpf_skipped,
3646 ns->ln_local_lpf_failed,
3647 ns->ln_striped_dirs_scanned,
3648 ns->ln_striped_dirs_repaired,
3649 ns->ln_striped_dirs_failed,
3650 ns->ln_striped_dirs_disabled,
3651 ns->ln_striped_dirs_skipped,
3652 ns->ln_striped_shards_scanned,
3653 ns->ln_striped_shards_repaired,
3654 ns->ln_striped_shards_failed,
3655 ns->ln_striped_shards_skipped,
3656 ns->ln_name_hash_repaired,
3657 ns->ln_success_count,
3662 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3663 struct lfsck_component *com)
3665 struct lfsck_instance *lfsck = com->lc_lfsck;
3666 struct lfsck_namespace *ns = com->lc_file_ram;
3668 while (!list_empty(&lfsck->li_list_lmv)) {
3669 struct lfsck_lmv_unit *llu;
3670 struct lfsck_lmv *llmv;
3672 llu = list_entry(lfsck->li_list_lmv.next,
3673 struct lfsck_lmv_unit, llu_link);
3674 llmv = &llu->llu_lmv;
3676 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3677 "still in using: %u\n",
3678 atomic_read(&llmv->ll_ref));
3680 ns->ln_striped_dirs_skipped++;
3681 lfsck_lmv_put(env, llmv);
3685 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3686 struct lfsck_component *com,
3687 struct dt_object *obj)
3689 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3692 rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3696 /* zero-linkEA object may be orphan, but it also maybe because
3697 * of upgrading. Currently, we cannot record it for double scan.
3698 * Because it may cause the LFSCK trace file to be too large. */
3700 /* "la_ctime" == 1 means that it has ever been removed from
3701 * backend /lost+found directory but not been added back to
3702 * the normal namespace yet. */
3704 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3705 unlikely(la->la_ctime == 1))
3706 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3707 LNTF_CHECK_LINKEA, true);
3712 /* namespace APIs */
3714 static int lfsck_namespace_reset(const struct lu_env *env,
3715 struct lfsck_component *com, bool init)
3717 struct lfsck_instance *lfsck = com->lc_lfsck;
3718 struct lfsck_namespace *ns = com->lc_file_ram;
3719 struct lfsck_assistant_data *lad = com->lc_data;
3720 struct dt_object *root;
3721 struct dt_object *dto;
3725 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3727 GOTO(log, rc = PTR_ERR(root));
3729 if (unlikely(!dt_try_as_dir(env, root)))
3730 GOTO(put, rc = -ENOTDIR);
3732 down_write(&com->lc_sem);
3734 memset(ns, 0, sizeof(*ns));
3736 __u32 count = ns->ln_success_count;
3737 __u64 last_time = ns->ln_time_last_complete;
3739 memset(ns, 0, sizeof(*ns));
3740 ns->ln_success_count = count;
3741 ns->ln_time_last_complete = last_time;
3743 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3744 ns->ln_status = LS_INIT;
3746 lfsck_object_put(env, com->lc_obj);
3748 dto = lfsck_namespace_load_one_trace_file(env, com, root,
3749 LFSCK_NAMESPACE, true);
3751 GOTO(out, rc = PTR_ERR(dto));
3754 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3758 lad->lad_incomplete = 0;
3759 CFS_RESET_BITMAP(lad->lad_bitmap);
3761 rc = lfsck_namespace_store(env, com, true);
3766 up_write(&com->lc_sem);
3769 lfsck_object_put(env, root);
3771 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3772 lfsck_lfsck2name(lfsck), rc);
3777 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3780 struct lfsck_namespace *ns = com->lc_file_ram;
3782 down_write(&com->lc_sem);
3784 com->lc_new_checked++;
3785 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3786 up_write(&com->lc_sem);
3789 static void lfsck_namespace_close_dir(const struct lu_env *env,
3790 struct lfsck_component *com)
3792 struct lfsck_namespace *ns = com->lc_file_ram;
3793 struct lfsck_assistant_data *lad = com->lc_data;
3794 struct lfsck_instance *lfsck = com->lc_lfsck;
3795 struct lfsck_lmv *llmv = lfsck->li_lmv;
3796 struct lfsck_namespace_req *lnr;
3798 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3799 bool wakeup = false;
3805 OBD_ALLOC(lnr, size);
3807 ns->ln_striped_dirs_skipped++;
3812 /* Generate a dummy request to indicate that all shards' name entry
3813 * in this striped directory has been scanned for the first time. */
3814 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3815 lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3816 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3817 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3818 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3819 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3820 lnr->lnr_size = size;
3822 spin_lock(&lad->lad_lock);
3823 if (lad->lad_assistant_status < 0) {
3824 spin_unlock(&lad->lad_lock);
3825 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3826 ns->ln_striped_dirs_skipped++;
3831 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3832 if (lad->lad_prefetched == 0)
3835 lad->lad_prefetched++;
3836 spin_unlock(&lad->lad_lock);
3838 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3843 static int lfsck_namespace_open_dir(const struct lu_env *env,
3844 struct lfsck_component *com)
3846 struct lfsck_instance *lfsck = com->lc_lfsck;
3847 struct lfsck_namespace *ns = com->lc_file_ram;
3848 struct lfsck_lmv *llmv = lfsck->li_lmv;
3855 if (llmv->ll_lmv_master) {
3856 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3858 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
3859 lmv->lmv_master_mdt_index =
3860 lfsck_dev_idx(lfsck);
3861 ns->ln_flags |= LF_INCONSISTENT;
3862 llmv->ll_lmv_updated = 1;
3865 rc = lfsck_namespace_verify_stripe_slave(env, com,
3866 lfsck->li_obj_dir, llmv);
3869 RETURN(rc > 0 ? 0 : rc);
3872 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3873 struct lfsck_component *com, bool init)
3875 struct lfsck_instance *lfsck = com->lc_lfsck;
3876 struct lfsck_namespace *ns = com->lc_file_ram;
3880 rc = lfsck_checkpoint_generic(env, com);
3885 down_write(&com->lc_sem);
3887 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3889 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3890 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3891 HALF_SEC - lfsck->li_time_last_checkpoint);
3892 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3893 ns->ln_items_checked += com->lc_new_checked;
3894 com->lc_new_checked = 0;
3897 rc = lfsck_namespace_store(env, com, false);
3898 up_write(&com->lc_sem);
3901 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3902 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3903 lfsck->li_pos_current.lp_oit_cookie,
3904 PFID(&lfsck->li_pos_current.lp_dir_parent),
3905 lfsck->li_pos_current.lp_dir_cookie, rc);
3907 return rc > 0 ? 0 : rc;
3910 static int lfsck_namespace_prep(const struct lu_env *env,
3911 struct lfsck_component *com,
3912 struct lfsck_start_param *lsp)
3914 struct lfsck_instance *lfsck = com->lc_lfsck;
3915 struct lfsck_namespace *ns = com->lc_file_ram;
3916 struct lfsck_position *pos = &com->lc_pos_start;
3919 rc = lfsck_namespace_load_bitmap(env, com);
3920 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3921 rc = lfsck_namespace_reset(env, com, false);
3923 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3926 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3927 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3933 down_write(&com->lc_sem);
3934 ns->ln_time_latest_start = cfs_time_current_sec();
3935 spin_lock(&lfsck->li_lock);
3937 if (ns->ln_flags & LF_SCANNED_ONCE) {
3938 if (!lfsck->li_drop_dryrun ||
3939 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3940 ns->ln_status = LS_SCANNING_PHASE2;
3941 list_move_tail(&com->lc_link,
3942 &lfsck->li_list_double_scan);
3943 if (!list_empty(&com->lc_link_dir))
3944 list_del_init(&com->lc_link_dir);
3945 lfsck_pos_set_zero(pos);
3947 ns->ln_status = LS_SCANNING_PHASE1;
3948 ns->ln_run_time_phase1 = 0;
3949 ns->ln_run_time_phase2 = 0;
3950 ns->ln_items_checked = 0;
3951 ns->ln_items_repaired = 0;
3952 ns->ln_items_failed = 0;
3953 ns->ln_dirs_checked = 0;
3954 ns->ln_objs_checked_phase2 = 0;
3955 ns->ln_objs_repaired_phase2 = 0;
3956 ns->ln_objs_failed_phase2 = 0;
3957 ns->ln_objs_nlink_repaired = 0;
3958 ns->ln_dirent_repaired = 0;
3959 ns->ln_linkea_repaired = 0;
3960 ns->ln_mul_linked_checked = 0;
3961 ns->ln_mul_linked_repaired = 0;
3962 ns->ln_unknown_inconsistency = 0;
3963 ns->ln_unmatched_pairs_repaired = 0;
3964 ns->ln_dangling_repaired = 0;
3965 ns->ln_mul_ref_repaired = 0;
3966 ns->ln_bad_type_repaired = 0;
3967 ns->ln_lost_dirent_repaired = 0;
3968 ns->ln_striped_dirs_scanned = 0;
3969 ns->ln_striped_dirs_repaired = 0;
3970 ns->ln_striped_dirs_failed = 0;
3971 ns->ln_striped_dirs_disabled = 0;
3972 ns->ln_striped_dirs_skipped = 0;
3973 ns->ln_striped_shards_scanned = 0;
3974 ns->ln_striped_shards_repaired = 0;
3975 ns->ln_striped_shards_failed = 0;
3976 ns->ln_striped_shards_skipped = 0;
3977 ns->ln_name_hash_repaired = 0;
3978 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3979 if (list_empty(&com->lc_link_dir))
3980 list_add_tail(&com->lc_link_dir,
3981 &lfsck->li_list_dir);
3982 *pos = ns->ln_pos_first_inconsistent;
3985 ns->ln_status = LS_SCANNING_PHASE1;
3986 if (list_empty(&com->lc_link_dir))
3987 list_add_tail(&com->lc_link_dir,
3988 &lfsck->li_list_dir);
3989 if (!lfsck->li_drop_dryrun ||
3990 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3991 *pos = ns->ln_pos_last_checkpoint;
3992 pos->lp_oit_cookie++;
3994 *pos = ns->ln_pos_first_inconsistent;
3998 spin_unlock(&lfsck->li_lock);
3999 up_write(&com->lc_sem);
4001 rc = lfsck_start_assistant(env, com, lsp);
4003 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
4004 DFID", "LPX64"]: rc = %d\n",
4005 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4006 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4011 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4012 struct lfsck_component *com,
4013 struct dt_object *obj)
4015 struct lfsck_thread_info *info = lfsck_env_info(env);
4016 struct lfsck_namespace *ns = com->lc_file_ram;
4017 struct lfsck_instance *lfsck = com->lc_lfsck;
4018 const struct lu_fid *fid = lfsck_dto2fid(obj);
4019 struct lu_fid *pfid = &info->lti_fid2;
4020 struct lu_name *cname = &info->lti_name;
4021 struct lu_seq_range *range = &info->lti_range;
4022 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4023 struct linkea_data ldata = { NULL };
4024 __u32 idx = lfsck_dev_idx(lfsck);
4028 rc = lfsck_links_read(env, obj, &ldata);
4032 /* -EINVAL means crashed linkEA, should be verified. */
4033 if (rc == -EINVAL) {
4034 rc = lfsck_namespace_trace_update(env, com, fid,
4035 LNTF_CHECK_LINKEA, true);
4037 struct lustre_handle lh = { 0 };
4039 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4040 MDS_INODELOCK_UPDATE |
4041 MDS_INODELOCK_XATTR, LCK_EX);
4043 rc = lfsck_namespace_links_remove(env, com,
4045 lfsck_ibits_unlock(&lh, LCK_EX);
4049 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4052 if (rc == -ENODATA) {
4053 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4061 /* Record multiple-linked object. */
4062 if (ldata.ld_leh->leh_reccount > 1) {
4063 rc = lfsck_namespace_trace_update(env, com, fid,
4064 LNTF_CHECK_LINKEA, true);
4069 linkea_first_entry(&ldata);
4070 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4071 if (!fid_is_sane(pfid)) {
4072 rc = lfsck_namespace_trace_update(env, com, fid,
4073 LNTF_CHECK_PARENT, true);
4075 fld_range_set_mdt(range);
4076 rc = fld_local_lookup(env, ss->ss_server_fld,
4077 fid_seq(pfid), range);
4078 if ((rc == -ENOENT) ||
4079 (rc == 0 && range->lsr_index != idx))
4080 rc = lfsck_namespace_trace_update(env, com, fid,
4081 LNTF_CHECK_LINKEA, true);
4083 rc = lfsck_namespace_check_for_double_scan(env, com,
4090 down_write(&com->lc_sem);
4091 com->lc_new_checked++;
4092 if (S_ISDIR(lfsck_object_type(obj)))
4093 ns->ln_dirs_checked++;
4095 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4096 up_write(&com->lc_sem);
4101 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4102 struct lfsck_component *com,
4103 struct lu_dirent *ent, __u16 type)
4105 struct lfsck_assistant_data *lad = com->lc_data;
4106 struct lfsck_instance *lfsck = com->lc_lfsck;
4107 struct lfsck_namespace_req *lnr;
4108 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4109 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4110 struct ptlrpc_thread *athread = &lad->lad_thread;
4111 struct l_wait_info lwi = { 0 };
4112 bool wakeup = false;
4114 l_wait_event(mthread->t_ctl_waitq,
4115 lad->lad_prefetched < bk->lb_async_windows ||
4116 !thread_is_running(mthread) ||
4117 thread_is_stopped(athread),
4120 if (unlikely(!thread_is_running(mthread)) ||
4121 thread_is_stopped(athread))
4124 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4127 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4129 struct lfsck_namespace *ns = com->lc_file_ram;
4131 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4132 return PTR_ERR(lnr);
4135 spin_lock(&lad->lad_lock);
4136 if (lad->lad_assistant_status < 0) {
4137 spin_unlock(&lad->lad_lock);
4138 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4139 return lad->lad_assistant_status;
4142 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4143 if (lad->lad_prefetched == 0)
4146 lad->lad_prefetched++;
4147 spin_unlock(&lad->lad_lock);
4149 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4151 down_write(&com->lc_sem);
4152 com->lc_new_checked++;
4153 up_write(&com->lc_sem);
4158 static int lfsck_namespace_post(const struct lu_env *env,
4159 struct lfsck_component *com,
4160 int result, bool init)
4162 struct lfsck_instance *lfsck = com->lc_lfsck;
4163 struct lfsck_namespace *ns = com->lc_file_ram;
4167 lfsck_post_generic(env, com, &result);
4169 down_write(&com->lc_sem);
4170 lfsck_namespace_release_lmv(env, com);
4172 spin_lock(&lfsck->li_lock);
4174 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4176 ns->ln_status = LS_SCANNING_PHASE2;
4177 ns->ln_flags |= LF_SCANNED_ONCE;
4178 ns->ln_flags &= ~LF_UPGRADE;
4179 list_del_init(&com->lc_link_dir);
4180 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4181 } else if (result == 0) {
4182 if (lfsck->li_status != 0)
4183 ns->ln_status = lfsck->li_status;
4185 ns->ln_status = LS_STOPPED;
4186 if (ns->ln_status != LS_PAUSED) {
4187 list_del_init(&com->lc_link_dir);
4188 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4191 ns->ln_status = LS_FAILED;
4192 list_del_init(&com->lc_link_dir);
4193 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4195 spin_unlock(&lfsck->li_lock);
4198 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4199 HALF_SEC - lfsck->li_time_last_checkpoint);
4200 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4201 ns->ln_items_checked += com->lc_new_checked;
4202 com->lc_new_checked = 0;
4205 rc = lfsck_namespace_store(env, com, false);
4206 up_write(&com->lc_sem);
4208 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4209 lfsck_lfsck2name(lfsck), rc);
4215 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4218 struct lfsck_instance *lfsck = com->lc_lfsck;
4219 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4220 struct lfsck_namespace *ns = com->lc_file_ram;
4223 down_read(&com->lc_sem);
4224 seq_printf(m, "name: lfsck_namespace\n"
4230 lfsck_status2names(ns->ln_status));
4232 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4236 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4240 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4241 "time_since_last_completed");
4245 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4246 "time_since_latest_start");
4250 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4251 "time_since_last_checkpoint");
4255 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4256 "latest_start_position");
4260 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4261 "last_checkpoint_position");
4265 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4266 "first_failure_position");
4270 if (ns->ln_status == LS_SCANNING_PHASE1) {
4271 struct lfsck_position pos;
4272 const struct dt_it_ops *iops;
4273 cfs_duration_t duration = cfs_time_current() -
4274 lfsck->li_time_last_checkpoint;
4275 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4276 __u64 speed = checked;
4277 __u64 new_checked = com->lc_new_checked *
4278 msecs_to_jiffies(MSEC_PER_SEC);
4279 __u32 rtime = ns->ln_run_time_phase1 +
4280 cfs_duration_sec(duration + HALF_SEC);
4283 do_div(new_checked, duration);
4285 do_div(speed, rtime);
4286 lfsck_namespace_dump_statistics(m, ns, checked,
4287 ns->ln_objs_checked_phase2,
4288 rtime, ns->ln_run_time_phase2);
4290 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4291 "average_speed_phase2: N/A\n"
4292 "real_time_speed_phase1: "LPU64" items/sec\n"
4293 "real_time_speed_phase2: N/A\n",
4297 LASSERT(lfsck->li_di_oit != NULL);
4299 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4301 /* The low layer otable-based iteration position may NOT
4302 * exactly match the namespace-based directory traversal
4303 * cookie. Generally, it is not a serious issue. But the
4304 * caller should NOT make assumption on that. */
4305 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4306 if (!lfsck->li_current_oit_processed)
4307 pos.lp_oit_cookie--;
4309 spin_lock(&lfsck->li_lock);
4310 if (lfsck->li_di_dir != NULL) {
4311 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4312 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4313 fid_zero(&pos.lp_dir_parent);
4314 pos.lp_dir_cookie = 0;
4317 *lfsck_dto2fid(lfsck->li_obj_dir);
4320 fid_zero(&pos.lp_dir_parent);
4321 pos.lp_dir_cookie = 0;
4323 spin_unlock(&lfsck->li_lock);
4324 lfsck_pos_dump(m, &pos, "current_position");
4325 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4326 cfs_duration_t duration = cfs_time_current() -
4327 lfsck->li_time_last_checkpoint;
4328 __u64 checked = ns->ln_objs_checked_phase2 +
4329 com->lc_new_checked;
4330 __u64 speed1 = ns->ln_items_checked;
4331 __u64 speed2 = checked;
4332 __u64 new_checked = com->lc_new_checked *
4333 msecs_to_jiffies(MSEC_PER_SEC);
4334 __u32 rtime = ns->ln_run_time_phase2 +
4335 cfs_duration_sec(duration + HALF_SEC);
4338 do_div(new_checked, duration);
4339 if (ns->ln_run_time_phase1 != 0)
4340 do_div(speed1, ns->ln_run_time_phase1);
4342 do_div(speed2, rtime);
4343 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4345 ns->ln_run_time_phase1, rtime);
4347 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4348 "average_speed_phase2: "LPU64" objs/sec\n"
4349 "real_time_speed_phase1: N/A\n"
4350 "real_time_speed_phase2: "LPU64" objs/sec\n"
4351 "current_position: "DFID"\n",
4355 PFID(&ns->ln_fid_latest_scanned_phase2));
4357 __u64 speed1 = ns->ln_items_checked;
4358 __u64 speed2 = ns->ln_objs_checked_phase2;
4360 if (ns->ln_run_time_phase1 != 0)
4361 do_div(speed1, ns->ln_run_time_phase1);
4362 if (ns->ln_run_time_phase2 != 0)
4363 do_div(speed2, ns->ln_run_time_phase2);
4364 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4365 ns->ln_objs_checked_phase2,
4366 ns->ln_run_time_phase1,
4367 ns->ln_run_time_phase2);
4369 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4370 "average_speed_phase2: "LPU64" objs/sec\n"
4371 "real_time_speed_phase1: N/A\n"
4372 "real_time_speed_phase2: N/A\n"
4373 "current_position: N/A\n",
4378 up_read(&com->lc_sem);
4382 static int lfsck_namespace_double_scan(const struct lu_env *env,
4383 struct lfsck_component *com)
4385 struct lfsck_namespace *ns = com->lc_file_ram;
4386 struct lfsck_assistant_data *lad = com->lc_data;
4387 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4388 struct lfsck_tgt_desc *ltd;
4389 struct lfsck_tgt_desc *next;
4392 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4393 if (thread_is_stopped(&lad->lad_thread)) {
4394 LASSERT(list_empty(&lad->lad_req_list));
4395 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4397 spin_lock(<ds->ltd_lock);
4398 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4399 ltd_namespace_phase_list) {
4400 list_del_init(<d->ltd_namespace_phase_list);
4402 spin_unlock(<ds->ltd_lock);
4408 static void lfsck_namespace_data_release(const struct lu_env *env,
4409 struct lfsck_component *com)
4411 struct lfsck_assistant_data *lad = com->lc_data;
4412 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4413 struct lfsck_tgt_desc *ltd;
4414 struct lfsck_tgt_desc *next;
4416 LASSERT(lad != NULL);
4417 LASSERT(thread_is_init(&lad->lad_thread) ||
4418 thread_is_stopped(&lad->lad_thread));
4419 LASSERT(list_empty(&lad->lad_req_list));
4421 com->lc_data = NULL;
4422 lfsck_namespace_release_lmv(env, com);
4424 spin_lock(<ds->ltd_lock);
4425 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4426 ltd_namespace_phase_list) {
4427 list_del_init(<d->ltd_namespace_phase_list);
4429 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4430 ltd_namespace_phase_list) {
4431 list_del_init(<d->ltd_namespace_phase_list);
4433 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4434 ltd_namespace_list) {
4435 list_del_init(<d->ltd_namespace_list);
4437 spin_unlock(<ds->ltd_lock);
4439 if (likely(lad->lad_bitmap != NULL))
4440 CFS_FREE_BITMAP(lad->lad_bitmap);
4445 static void lfsck_namespace_quit(const struct lu_env *env,
4446 struct lfsck_component *com)
4448 struct lfsck_assistant_data *lad = com->lc_data;
4449 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4450 struct lfsck_tgt_desc *ltd;
4451 struct lfsck_tgt_desc *next;
4453 LASSERT(lad != NULL);
4455 lfsck_quit_generic(env, com);
4457 LASSERT(thread_is_init(&lad->lad_thread) ||
4458 thread_is_stopped(&lad->lad_thread));
4459 LASSERT(list_empty(&lad->lad_req_list));
4461 lfsck_namespace_release_lmv(env, com);
4463 spin_lock(<ds->ltd_lock);
4464 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4465 ltd_namespace_phase_list) {
4466 list_del_init(<d->ltd_namespace_phase_list);
4468 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4469 ltd_namespace_phase_list) {
4470 list_del_init(<d->ltd_namespace_phase_list);
4472 spin_unlock(<ds->ltd_lock);
4475 static int lfsck_namespace_in_notify(const struct lu_env *env,
4476 struct lfsck_component *com,
4477 struct lfsck_request *lr,
4480 struct lfsck_instance *lfsck = com->lc_lfsck;
4481 struct lfsck_namespace *ns = com->lc_file_ram;
4482 struct lfsck_assistant_data *lad = com->lc_data;
4483 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4484 struct lfsck_tgt_desc *ltd;
4489 switch (lr->lr_event) {
4490 case LE_SKIP_NLINK_DECLARE: {
4491 struct dt_object *obj;
4492 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4496 LASSERT(th != NULL);
4498 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4499 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4500 fid_cpu_to_be(key, &lr->lr_fid);
4501 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4502 rc = dt_declare_delete(env, obj,
4503 (const struct dt_key *)key, th);
4505 rc = dt_declare_insert(env, obj,
4506 (const struct dt_rec *)&flags,
4507 (const struct dt_key *)key, th);
4508 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4512 case LE_SKIP_NLINK: {
4513 struct dt_object *obj;
4514 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4520 LASSERT(th != NULL);
4522 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4523 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4524 fid_cpu_to_be(key, &lr->lr_fid);
4525 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4526 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4527 (const struct dt_key *)key, BYPASS_CAPA);
4529 if (flags & LNTF_SKIP_NLINK) {
4531 &com->lc_sub_trace_objs[idx].lsto_mutex);
4537 } else if (rc != -ENOENT) {
4541 flags |= LNTF_SKIP_NLINK;
4543 rc = dt_delete(env, obj, (const struct dt_key *)key,
4549 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4550 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4555 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4556 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4557 " to be skipped for namespace double scan: rc = %d\n",
4558 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4561 /* If we cannot record this object in the LFSCK tracing,
4562 * we have to mark the LFSC as LF_INCOMPLETE, then the
4563 * LFSCK will skip nlink attribute verification for
4565 ns->ln_flags |= LF_INCOMPLETE;
4569 case LE_SET_LMV_MASTER: {
4570 struct dt_object *obj;
4572 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4574 RETURN(PTR_ERR(obj));
4576 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4577 lfsck_object_put(env, obj);
4579 RETURN(rc > 0 ? 0 : rc);
4581 case LE_SET_LMV_SLAVE: {
4582 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4583 ns->ln_striped_shards_repaired++;
4585 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4586 LNTF_RECHECK_NAME_HASH, true);
4588 RETURN(rc > 0 ? 0 : rc);
4590 case LE_PHASE1_DONE:
4591 case LE_PHASE2_DONE:
4598 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4599 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4600 lr->lr_index, lr->lr_status, lr->lr_flags2);
4602 spin_lock(<ds->ltd_lock);
4603 ltd = LTD_TGT(ltds, lr->lr_index);
4605 spin_unlock(<ds->ltd_lock);
4610 list_del_init(<d->ltd_namespace_phase_list);
4611 switch (lr->lr_event) {
4612 case LE_PHASE1_DONE:
4613 if (lr->lr_status <= 0) {
4614 ltd->ltd_namespace_done = 1;
4615 list_del_init(<d->ltd_namespace_list);
4616 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4617 "phase1 for namespace LFSCK: rc = %d.\n",
4618 lfsck_lfsck2name(lfsck),
4619 ltd->ltd_index, lr->lr_status);
4620 ns->ln_flags |= LF_INCOMPLETE;
4625 if (lr->lr_flags2 & LF_INCOMPLETE)
4626 ns->ln_flags |= LF_INCOMPLETE;
4628 if (list_empty(<d->ltd_namespace_list))
4629 list_add_tail(<d->ltd_namespace_list,
4630 &lad->lad_mdt_list);
4631 list_add_tail(<d->ltd_namespace_phase_list,
4632 &lad->lad_mdt_phase2_list);
4634 case LE_PHASE2_DONE:
4635 ltd->ltd_namespace_done = 1;
4636 list_del_init(<d->ltd_namespace_list);
4640 ltd->ltd_namespace_done = 1;
4641 list_del_init(<d->ltd_namespace_list);
4642 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4644 "%s: the peer MDT %x exit namespace LFSCK\n",
4645 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4646 ns->ln_flags |= LF_INCOMPLETE;
4652 spin_unlock(<ds->ltd_lock);
4654 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4655 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4657 memset(stop, 0, sizeof(*stop));
4658 stop->ls_status = lr->lr_status;
4659 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4660 lfsck_stop(env, lfsck->li_bottom, stop);
4661 } else if (lfsck_phase2_next_ready(lad)) {
4662 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4668 static int lfsck_namespace_query(const struct lu_env *env,
4669 struct lfsck_component *com)
4671 struct lfsck_namespace *ns = com->lc_file_ram;
4673 return ns->ln_status;
4676 static struct lfsck_operations lfsck_namespace_ops = {
4677 .lfsck_reset = lfsck_namespace_reset,
4678 .lfsck_fail = lfsck_namespace_fail,
4679 .lfsck_close_dir = lfsck_namespace_close_dir,
4680 .lfsck_open_dir = lfsck_namespace_open_dir,
4681 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4682 .lfsck_prep = lfsck_namespace_prep,
4683 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4684 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4685 .lfsck_post = lfsck_namespace_post,
4686 .lfsck_dump = lfsck_namespace_dump,
4687 .lfsck_double_scan = lfsck_namespace_double_scan,
4688 .lfsck_data_release = lfsck_namespace_data_release,
4689 .lfsck_quit = lfsck_namespace_quit,
4690 .lfsck_in_notify = lfsck_namespace_in_notify,
4691 .lfsck_query = lfsck_namespace_query,
4695 * Repair dangling name entry.
4697 * For the name entry with dangling reference, we need to repare the
4698 * inconsistency according to the LFSCK sponsor's requirement:
4700 * 1) Keep the inconsistency there and report the inconsistency case,
4701 * then give the chance to the application to find related issues,
4702 * and the users can make the decision about how to handle it with
4703 * more human knownledge. (by default)
4705 * 2) Re-create the missing MDT-object with the FID information.
4707 * \param[in] env pointer to the thread context
4708 * \param[in] com pointer to the lfsck component
4709 * \param[in] parent pointer to the dir object that contains the dangling
4711 * \param[in] child pointer to the object corresponding to the dangling
4713 * \param[in] lnr pointer to the namespace request that contains the
4714 * name's name, parent object, parent's LMV, and ect.
4716 * \retval positive number if no need to repair
4717 * \retval zero for repaired successfully
4718 * \retval negative error number on failure
4720 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4721 struct lfsck_component *com,
4722 struct dt_object *parent,
4723 struct dt_object *child,
4724 struct lfsck_namespace_req *lnr)
4726 struct lfsck_thread_info *info = lfsck_env_info(env);
4727 struct lu_attr *la = &info->lti_la;
4728 struct dt_allocation_hint *hint = &info->lti_hint;
4729 struct dt_object_format *dof = &info->lti_dof;
4730 struct dt_insert_rec *rec = &info->lti_dt_rec;
4731 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4732 struct dt_object *pobj = NULL;
4733 struct dt_object *cobj = NULL;
4734 const struct lu_name *cname;
4735 struct linkea_data ldata = { NULL };
4736 struct lustre_handle lh = { 0 };
4737 struct lu_buf linkea_buf;
4738 struct lu_buf lmv_buf;
4739 struct lfsck_instance *lfsck = com->lc_lfsck;
4740 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4741 struct dt_device *dev = lfsck->li_next;
4742 struct thandle *th = NULL;
4744 __u16 type = lnr->lnr_type;
4748 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4749 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4754 if (!create || bk->lb_param & LPF_DRYRUN)
4757 /* We may need to create the sub-objects of the @child via LOD,
4758 * so make the modification based on lfsck->li_next. */
4760 pobj = lfsck_object_locate(dev, parent);
4762 GOTO(log, rc = PTR_ERR(pobj));
4764 if (unlikely(!dt_try_as_dir(env, pobj)))
4765 GOTO(log, rc = -ENOTDIR);
4767 cobj = lfsck_object_locate(dev, child);
4769 GOTO(log, rc = PTR_ERR(cobj));
4771 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4775 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(pobj));
4779 rc = lfsck_ibits_lock(env, lfsck, pobj, &lh,
4780 MDS_INODELOCK_UPDATE, LCK_EX);
4784 rc = lfsck_namespace_check_exist(env, pobj, cobj, lnr->lnr_name);
4788 /* Set the ctime as zero, then others can know it is created for
4789 * repairing dangling name entry by LFSCK. And if the LFSCK made
4790 * wrong decision and the real MDT-object has been found later,
4791 * then the LFSCK has chance to fix the incosistency properly. */
4792 memset(la, 0, sizeof(*la));
4793 la->la_mode = (type & S_IFMT) | 0600;
4794 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4795 LA_ATIME | LA_MTIME | LA_CTIME;
4797 cobj->do_ops->do_ah_init(env, hint, pobj, cobj,
4798 la->la_mode & S_IFMT);
4800 memset(dof, 0, sizeof(*dof));
4801 dof->dof_type = dt_mode_to_dft(type);
4802 /* If the target is a regular file, then the LFSCK will only create
4803 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4804 * OST-objects will be created when write open. */
4806 th = dt_trans_create(env, dev);
4808 GOTO(log, rc = PTR_ERR(th));
4810 /* 1a. create child. */
4811 rc = dt_declare_create(env, cobj, la, hint, dof, th);
4815 if (S_ISDIR(type)) {
4816 if (unlikely(!dt_try_as_dir(env, cobj)))
4817 GOTO(stop, rc = -ENOTDIR);
4819 /* 2a. insert dot into child dir */
4820 rec->rec_type = S_IFDIR;
4821 rec->rec_fid = lfsck_dto2fid(cobj);
4822 rc = dt_declare_insert(env, cobj,
4823 (const struct dt_rec *)rec,
4824 (const struct dt_key *)dot, th);
4828 /* 3a. insert dotdot into child dir */
4829 rec->rec_fid = lfsck_dto2fid(pobj);
4830 rc = dt_declare_insert(env, cobj,
4831 (const struct dt_rec *)rec,
4832 (const struct dt_key *)dotdot, th);
4836 /* 4a. increase child nlink */
4837 rc = dt_declare_ref_add(env, cobj, th);
4841 /* 5a. generate slave LMV EA. */
4842 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4845 idx = lfsck_shard_name_to_index(env,
4846 lnr->lnr_name, lnr->lnr_namelen,
4847 type, lfsck_dto2fid(cobj));
4848 if (unlikely(idx < 0))
4849 GOTO(stop, rc = idx);
4851 *lmv2 = lnr->lnr_lmv->ll_lmv;
4852 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4853 lmv2->lmv_master_mdt_index = idx;
4855 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4856 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4857 rc = dt_declare_xattr_set(env, cobj, &lmv_buf,
4858 XATTR_NAME_LMV, 0, th);
4864 /* 6a. insert linkEA for child */
4865 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4866 ldata.ld_leh->leh_len);
4867 rc = dt_declare_xattr_set(env, cobj, &linkea_buf,
4868 XATTR_NAME_LINK, 0, th);
4872 rc = dt_trans_start_local(env, dev, th);
4874 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4876 dt_write_lock(env, cobj, 0);
4877 /* 1b. create child */
4878 rc = dt_create(env, cobj, la, hint, dof, th);
4880 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4882 if (S_ISDIR(type)) {
4883 /* 2b. insert dot into child dir */
4884 rec->rec_type = S_IFDIR;
4885 rec->rec_fid = lfsck_dto2fid(cobj);
4886 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4887 (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4891 /* 3b. insert dotdot into child dir */
4892 rec->rec_fid = lfsck_dto2fid(pobj);
4893 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4894 (const struct dt_key *)dotdot, th,
4899 /* 4b. increase child nlink */
4900 rc = dt_ref_add(env, cobj, th);
4904 /* 5b. generate slave LMV EA. */
4905 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4906 rc = dt_xattr_set(env, cobj, &lmv_buf, XATTR_NAME_LMV,
4907 0, th, BYPASS_CAPA);
4913 /* 6b. insert linkEA for child. */
4914 rc = dt_xattr_set(env, cobj, &linkea_buf,
4915 XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4920 dt_write_unlock(env, cobj);
4923 dt_trans_stop(env, dev, th);
4926 lfsck_ibits_unlock(&lh, LCK_EX);
4927 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4928 "reference for: parent "DFID", child "DFID", type %u, "
4929 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4930 PFID(&lnr->lnr_lar.lar_fid), PFID(lfsck_dto2fid(child)),
4931 type, cname->ln_name,
4932 create ? "Create the lost OST-object as required" :
4933 "Keep the MDT-object there by default", rc);
4936 struct lfsck_namespace *ns = com->lc_file_ram;
4938 ns->ln_flags |= LF_INCONSISTENT;
4944 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4945 struct lfsck_component *com,
4946 struct lfsck_assistant_req *lar)
4948 struct lfsck_thread_info *info = lfsck_env_info(env);
4949 struct lu_attr *la = &info->lti_la;
4950 struct lfsck_instance *lfsck = com->lc_lfsck;
4951 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4952 struct lfsck_namespace *ns = com->lc_file_ram;
4953 struct linkea_data ldata = { NULL };
4954 const struct lu_name *cname;
4955 struct thandle *handle = NULL;
4956 struct lfsck_namespace_req *lnr =
4957 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
4958 struct dt_object *dir = NULL;
4959 struct dt_object *obj = NULL;
4960 const struct lu_fid *pfid;
4961 struct dt_device *dev = NULL;
4962 struct lustre_handle lh = { 0 };
4963 bool repaired = false;
4964 bool dtlocked = false;
4968 bool bad_hash = false;
4972 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
4975 dir = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
4977 RETURN(PTR_ERR(dir));
4979 if (unlikely(lfsck_is_dead_obj(dir)))
4980 GOTO(put_dir, rc = 0);
4982 if (unlikely(!dt_try_as_dir(env, dir)))
4983 GOTO(put_dir, rc = -ENOTDIR);
4985 pfid = lfsck_dto2fid(dir);
4987 if (lnr->lnr_attr & LUDA_UPGRADE) {
4988 ns->ln_flags |= LF_UPGRADE;
4989 ns->ln_dirent_repaired++;
4991 } else if (lnr->lnr_attr & LUDA_REPAIR) {
4992 ns->ln_flags |= LF_INCONSISTENT;
4993 ns->ln_dirent_repaired++;
4997 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
4998 if (strcmp(lnr->lnr_name, dotdot) != 0)
5001 rc = lfsck_namespace_trace_update(env, com, pfid,
5002 LNTF_CHECK_PARENT, true);
5007 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5008 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5009 " for the name entry %.*s under "DFID"\n",
5010 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5011 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5013 if (strcmp(lnr->lnr_name, dotdot) != 0)
5014 /* invalid FID means bad name entry, remove it. */
5015 type = LNIT_BAD_DIRENT;
5017 /* If the parent FID is invalid, we cannot remove
5018 * the ".." entry directly. */
5019 rc = lfsck_namespace_trace_update(env, com, pfid,
5020 LNTF_CHECK_PARENT, true);
5025 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5026 rc = lfsck_namespace_striped_dir_rescan(env, com, dir, lnr);
5031 if (lnr->lnr_name[0] == '.' &&
5032 (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
5035 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5036 rc = lfsck_namespace_handle_striped_master(env, com, dir, lnr);
5041 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5043 GOTO(out, rc = idx);
5045 if (idx == lfsck_dev_idx(lfsck)) {
5046 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5049 dev = lfsck->li_bottom;
5051 struct lfsck_tgt_desc *ltd;
5053 /* Usually, some local filesystem consistency verification
5054 * tools can guarantee the local namespace tree consistenct.
5055 * So the LFSCK will only verify the remote directory. */
5056 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5057 rc = lfsck_namespace_trace_update(env, com, pfid,
5058 LNTF_CHECK_PARENT, true);
5063 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5064 if (unlikely(ltd == NULL)) {
5065 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5066 "did not join the namespace LFSCK\n",
5067 lfsck_lfsck2name(lfsck), idx);
5068 lfsck_lad_set_bitmap(env, com, idx);
5070 GOTO(out, rc = -ENODEV);
5076 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5078 GOTO(out, rc = PTR_ERR(obj));
5080 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5081 if (dt_object_exists(obj) == 0) {
5084 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5086 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5087 lnr->lnr_name, lnr->lnr_namelen)) {
5088 type = LNIT_BAD_DIRENT;
5093 type = LNIT_DANGLING;
5094 rc = lfsck_namespace_repair_dangling(env, com, dir,
5103 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5106 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5107 MDS_INODELOCK_UPDATE |
5108 MDS_INODELOCK_XATTR, LCK_EX);
5112 handle = dt_trans_create(env, dev);
5114 GOTO(out, rc = PTR_ERR(handle));
5116 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5120 rc = dt_trans_start_local(env, dev, handle);
5124 dt_write_lock(env, obj, 0);
5128 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5132 rc = lfsck_links_read(env, obj, &ldata);
5133 if (unlikely(rc == -ENOENT)) {
5134 if (handle != NULL) {
5135 dt_write_unlock(env, obj);
5138 dt_trans_stop(env, dev, handle);
5141 lfsck_ibits_unlock(&lh, LCK_EX);
5144 /* It may happen when the remote object has been removed,
5145 * but the local MDT is not aware of that. */
5147 } else if (rc == 0) {
5148 count = ldata.ld_leh->leh_reccount;
5149 rc = linkea_links_find(&ldata, cname, pfid);
5151 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5152 if ((lfsck_object_type(obj) & S_IFMT) !=
5154 ns->ln_flags |= LF_INCONSISTENT;
5155 type = LNIT_BAD_TYPE;
5161 ns->ln_flags |= LF_INCONSISTENT;
5163 /* If the name entry hash does not match the slave striped
5164 * directory, and the name entry does not match also, then
5165 * it is quite possible that name entry is corrupted. */
5166 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5167 lnr->lnr_name, lnr->lnr_namelen)) {
5168 type = LNIT_BAD_DIRENT;
5173 /* If the file type stored in the name entry does not match
5174 * the file type claimed by the object, and the object does
5175 * not recognize the name entry, then it is quite possible
5176 * that the name entry is corrupted. */
5177 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5178 type = LNIT_BAD_DIRENT;
5183 /* For sub-dir object, we cannot make sure whether the sub-dir
5184 * back references the parent via ".." name entry correctly or
5185 * not in the LFSCK first-stage scanning. It may be that the
5186 * (remote) sub-dir ".." name entry has no parent FID after
5187 * file-level backup/restore and its linkEA may be wrong.
5188 * So under such case, we should replace the linkEA according
5189 * to current name entry. But this needs to be done during the
5190 * LFSCK second-stage scanning. The LFSCK will record the name
5191 * entry for further possible using. */
5195 } else if (unlikely(rc == -EINVAL)) {
5196 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5197 type = LNIT_BAD_TYPE;
5200 ns->ln_flags |= LF_INCONSISTENT;
5201 /* The magic crashed, we are not sure whether there are more
5202 * corrupt data in the linkea, so remove all linkea entries. */
5206 } else if (rc == -ENODATA) {
5207 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5208 type = LNIT_BAD_TYPE;
5211 ns->ln_flags |= LF_UPGRADE;
5216 if (bk->lb_param & LPF_DRYRUN) {
5217 ns->ln_linkea_repaired++;
5223 if (!lustre_handle_is_used(&lh))
5229 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5236 rc = linkea_data_new(&ldata,
5237 &lfsck_env_info(env)->lti_linkea_buf);
5242 rc = linkea_add_buf(&ldata, cname, pfid);
5246 rc = lfsck_links_write(env, obj, &ldata, handle);
5247 if (unlikely(rc == -ENOSPC) &&
5248 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5249 if (handle != NULL) {
5250 LASSERT(dt_write_locked(env, obj));
5252 dt_write_unlock(env, obj);
5255 dt_trans_stop(env, dev, handle);
5258 lfsck_ibits_unlock(&lh, LCK_EX);
5261 rc = lfsck_namespace_trace_update(env, com,
5262 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5264 /* If we cannot record this object in the
5265 * LFSCK tracing, we have to mark the LFSCK
5266 * as LF_INCOMPLETE, then the LFSCK will
5267 * skip nlink attribute verification for
5269 ns->ln_flags |= LF_INCOMPLETE;
5277 count = ldata.ld_leh->leh_reccount;
5278 if (!S_ISDIR(lfsck_object_type(obj)) ||
5279 !dt_object_remote(obj)) {
5280 ns->ln_linkea_repaired++;
5290 dt_write_unlock(env, obj);
5292 if (handle != NULL && !IS_ERR(handle))
5293 dt_trans_stop(env, dev, handle);
5296 lfsck_ibits_unlock(&lh, LCK_EX);
5298 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5299 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5300 lnr->lnr_name, lnr->lnr_namelen) &&
5301 type != LNIT_BAD_DIRENT) {
5302 ns->ln_flags |= LF_INCONSISTENT;
5305 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5306 lnr->lnr_lmv, lnr->lnr_name);
5315 rc = lfsck_namespace_repair_dirent(env, com, dir,
5316 obj, lnr->lnr_name, lnr->lnr_name,
5317 lnr->lnr_type, true, false);
5321 case LNIT_BAD_DIRENT:
5323 /* XXX: This is a bad dirent, we do not know whether
5324 * the original name entry reference a regular
5325 * file or a directory, then keep the parent's
5326 * nlink count unchanged here. */
5327 rc = lfsck_namespace_repair_dirent(env, com, dir,
5328 obj, lnr->lnr_name, lnr->lnr_name,
5329 lnr->lnr_type, false, false);
5337 if (count == 1 && S_ISREG(lfsck_object_type(obj)))
5338 dt_attr_get(env, obj, la, BYPASS_CAPA);
5341 down_write(&com->lc_sem);
5343 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5344 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5345 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5346 PFID(lfsck_dto2fid(dir)),
5347 lnr->lnr_namelen, lnr->lnr_name, rc);
5349 lfsck_namespace_record_failure(env, lfsck, ns);
5350 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5351 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5352 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5353 dev != NULL && dev != lfsck->li_bottom)
5354 lfsck_lad_set_bitmap(env, com, idx);
5356 if (!(bk->lb_param & LPF_FAILOUT))
5360 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5361 "repaired the entry: "DFID", parent "DFID
5362 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5363 PFID(&lnr->lnr_fid),
5364 PFID(lfsck_dto2fid(dir)),
5365 lnr->lnr_namelen, lnr->lnr_name);
5368 ns->ln_items_repaired++;
5372 ns->ln_dangling_repaired++;
5375 ns->ln_bad_type_repaired++;
5377 case LNIT_BAD_DIRENT:
5378 ns->ln_dirent_repaired++;
5384 if (bk->lb_param & LPF_DRYRUN &&
5385 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5386 lfsck_pos_fill(env, lfsck,
5387 &ns->ln_pos_first_inconsistent,
5392 ns->ln_name_hash_repaired++;
5394 /* Not count repeatedly. */
5396 ns->ln_items_repaired++;
5398 if (bk->lb_param & LPF_DRYRUN &&
5399 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5400 lfsck_pos_fill(env, lfsck,
5401 &ns->ln_pos_first_inconsistent,
5408 if (count > 1 || la->la_nlink > 1)
5409 ns->ln_mul_linked_checked++;
5411 up_write(&com->lc_sem);
5413 if (obj != NULL && !IS_ERR(obj))
5414 lfsck_object_put(env, obj);
5417 lfsck_object_put(env, dir);
5423 * Handle one orphan under the backend /lost+found directory
5425 * Insert the orphan FID into the namespace LFSCK trace file for further
5426 * processing (via the subsequent namespace LFSCK second-stage scanning).
5427 * At the same time, remove the orphan name entry from backend /lost+found
5428 * directory. There is an interval between the orphan name entry removed
5429 * from the backend /lost+found directory and the orphan FID in the LFSCK
5430 * trace file handled. In such interval, the LFSCK can be reset, then
5431 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5432 * To guarantee that the orphans can be found when LFSCK run next time
5433 * without e2fsck again, when remove the orphan name entry, the LFSCK
5434 * will set the orphan's ctime attribute as 1. Since normal applications
5435 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5436 * next time, it can record the object (that ctime is 1) in the namespace
5437 * LFSCK trace file during the first-stage scanning.
5439 * \param[in] env pointer to the thread context
5440 * \param[in] com pointer to the lfsck component
5441 * \param[in] parent pointer to the object for the backend /lost+found
5442 * \param[in] ent pointer to the name entry for the target under the
5443 * backend /lost+found
5445 * \retval positive for repaired
5446 * \retval 0 if needs to repair nothing
5447 * \retval negative error number on failure
5449 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5450 struct lfsck_component *com,
5451 struct dt_object *parent,
5452 struct lu_dirent *ent)
5454 struct lfsck_thread_info *info = lfsck_env_info(env);
5455 struct lu_fid *key = &info->lti_fid;
5456 struct lu_attr *la = &info->lti_la;
5457 struct lfsck_instance *lfsck = com->lc_lfsck;
5458 struct dt_object *obj;
5459 struct dt_device *dev = lfsck->li_bottom;
5460 struct dt_object *child = NULL;
5461 struct thandle *th = NULL;
5468 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5470 RETURN(PTR_ERR(child));
5472 LASSERT(dt_object_exists(child));
5473 LASSERT(!dt_object_remote(child));
5475 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5476 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5477 fid_cpu_to_be(key, &ent->lde_fid);
5478 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5479 (const struct dt_key *)key, BYPASS_CAPA);
5482 flags |= LNTF_CHECK_ORPHAN;
5483 } else if (rc == -ENOENT) {
5484 flags = LNTF_CHECK_ORPHAN;
5489 th = dt_trans_create(env, dev);
5491 GOTO(out, rc = PTR_ERR(th));
5493 /* a1. remove name entry from backend /lost+found */
5494 rc = dt_declare_delete(env, parent,
5495 (const struct dt_key *)ent->lde_name, th);
5499 if (S_ISDIR(lfsck_object_type(child))) {
5500 /* a2. decrease parent's nlink */
5501 rc = dt_declare_ref_del(env, parent, th);
5507 /* a3. remove child's FID from the LFSCK trace file. */
5508 rc = dt_declare_delete(env, obj,
5509 (const struct dt_key *)key, th);
5513 /* a4. set child's ctime as 1 */
5514 memset(la, 0, sizeof(*la));
5516 la->la_valid = LA_CTIME;
5517 rc = dt_declare_attr_set(env, child, la, th);
5522 /* a5. insert child's FID into the LFSCK trace file. */
5523 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5524 (const struct dt_key *)key, th);
5528 rc = dt_trans_start_local(env, dev, th);
5532 /* b1. remove name entry from backend /lost+found */
5533 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5538 if (S_ISDIR(lfsck_object_type(child))) {
5539 /* b2. decrease parent's nlink */
5540 dt_write_lock(env, parent, 0);
5541 rc = dt_ref_del(env, parent, th);
5542 dt_write_unlock(env, parent);
5548 /* a3. remove child's FID from the LFSCK trace file. */
5549 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5554 /* b4. set child's ctime as 1 */
5555 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5560 /* b5. insert child's FID into the LFSCK trace file. */
5561 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5562 (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5564 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5567 dt_trans_stop(env, dev, th);
5570 lfsck_object_put(env, child);
5576 * Handle orphans under the backend /lost+found directory
5578 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5579 * and put them under the backend /lost+found directory that is invisible
5580 * to client. The LFSCK will scan such directory, for the original client
5581 * visible orphans, add their fids into the namespace LFSCK trace file,
5582 * then the subsenquent namespace LFSCK second-stage scanning can handle
5583 * them as other objects to be double scanned: either move back to normal
5584 * namespace, or to the global visible orphan directory:
5585 * /ROOT/.lustre/lost+found/MDTxxxx/
5587 * \param[in] env pointer to the thread context
5588 * \param[in] com pointer to the lfsck component
5590 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5591 struct lfsck_component *com)
5593 struct lfsck_thread_info *info = lfsck_env_info(env);
5594 struct lu_dirent *ent =
5595 (struct lu_dirent *)info->lti_key;
5596 struct lu_seq_range *range = &info->lti_range;
5597 struct lfsck_instance *lfsck = com->lc_lfsck;
5598 struct ptlrpc_thread *thread = &lfsck->li_thread;
5599 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5600 struct lfsck_namespace *ns = com->lc_file_ram;
5601 struct dt_object *parent;
5602 const struct dt_it_ops *iops;
5604 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5606 __u32 idx = lfsck_dev_idx(lfsck);
5611 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5612 &LU_BACKEND_LPF_FID);
5613 if (IS_ERR(parent)) {
5614 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5615 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5619 /* It is normal that the /lost+found does not exist for ZFS backend. */
5620 if (!dt_object_exists(parent))
5623 if (unlikely(!dt_try_as_dir(env, parent)))
5624 GOTO(out, rc = -ENOTDIR);
5626 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5627 lfsck_lfsck2name(lfsck));
5629 com->lc_new_scanned = 0;
5630 iops = &parent->do_index_ops->dio_it;
5631 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5633 GOTO(out, rc = PTR_ERR(di));
5635 rc = iops->load(env, di, 0);
5637 rc = iops->next(env, di);
5642 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5643 unlikely(!thread_is_running(thread)))
5646 rc = iops->rec(env, di, (struct dt_rec *)ent,
5647 LUDA_64BITHASH | LUDA_TYPE);
5649 rc = lfsck_unpack_ent(ent, &cookie, &type);
5651 if (unlikely(rc != 0)) {
5652 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5653 "/lost+found: rc = %d\n",
5654 lfsck_lfsck2name(lfsck), rc);
5659 /* skip dot and dotdot entries */
5660 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5663 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5666 if (fid_is_norm(&ent->lde_fid)) {
5667 fld_range_set_mdt(range);
5668 rc = fld_local_lookup(env, ss->ss_server_fld,
5669 fid_seq(&ent->lde_fid), range);
5672 } else if (idx != 0) {
5673 /* If the returned FID is IGIF, then there are three
5676 * 1) The object is upgraded from old Lustre-1.8 with
5677 * IGIF assigned to such object.
5678 * 2) The object is a backend local object and is
5679 * invisible to client.
5680 * 3) The object lost its LMV EA, and since there is
5681 * no FID-in-dirent for the orphan in the backend
5682 * /lost+found directory, then the low layer will
5683 * return IGIF for such object.
5685 * For MDTx (x != 0), it is either case 2) or case 3),
5686 * but from the LFSCK view, they are indistinguishable.
5687 * To be safe, the LFSCK will keep it there and report
5688 * some message, then the adminstrator can handle that
5691 * For MDT0, it is more possible the case 1). The LFSCK
5692 * will handle the orphan as an upgraded object. */
5693 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5694 "in the backend /lost+found on the MDT %04x, "
5695 "to be safe, skip it.\n",
5696 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5697 ent->lde_name, PFID(&ent->lde_fid), idx);
5701 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5704 down_write(&com->lc_sem);
5705 com->lc_new_scanned++;
5706 ns->ln_local_lpf_scanned++;
5708 ns->ln_local_lpf_moved++;
5710 ns->ln_local_lpf_skipped++;
5712 ns->ln_local_lpf_failed++;
5713 up_write(&com->lc_sem);
5715 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5719 lfsck_control_speed_by_self(com);
5720 if (unlikely(!thread_is_running(thread))) {
5725 rc = iops->next(env, di);
5729 iops->fini(env, di);
5734 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5735 lfsck_lfsck2name(lfsck), rc);
5737 lfsck_object_put(env, parent);
5741 * Rescan the striped directory after the master LMV EA reset.
5743 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5744 * the namespace LFSCK engine scan the striped directory for the first time,
5745 * it will be regarded as a normal directory. As the LFSCK processing, some
5746 * other LFSCK instance on other MDT will find the shard of this striped dir,
5747 * and find that the master MDT-object of the striped directory lost its LMV
5748 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5749 * notify the LFSCK instance on this MDT to rescan the striped directory.
5751 * \param[in] env pointer to the thread context
5752 * \param[in] com pointer to the lfsck component
5753 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5756 * \retval positive number for success
5757 * \retval 0 for LFSCK stopped/paused
5758 * \retval negative error number on failure
5760 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5761 struct lfsck_component *com,
5762 struct lfsck_lmv_unit *llu)
5764 struct lfsck_thread_info *info = lfsck_env_info(env);
5765 struct lfsck_instance *lfsck = com->lc_lfsck;
5766 struct lfsck_assistant_data *lad = com->lc_data;
5767 struct dt_object *dir;
5768 const struct dt_it_ops *iops;
5770 struct lu_dirent *ent =
5771 (struct lu_dirent *)info->lti_key;
5772 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5773 struct ptlrpc_thread *thread = &lfsck->li_thread;
5774 struct lfsck_namespace_req *lnr;
5775 struct lfsck_assistant_req *lar;
5780 LASSERT(list_empty(&lad->lad_req_list));
5782 lfsck->li_lmv = &llu->llu_lmv;
5783 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5784 rc = lfsck_open_dir(env, lfsck, 0);
5788 dir = lfsck->li_obj_dir;
5789 di = lfsck->li_di_dir;
5790 iops = &dir->do_index_ops->dio_it;
5792 rc = iops->rec(env, di, (struct dt_rec *)ent,
5793 lfsck->li_args_dir);
5795 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5799 if (bk->lb_param & LPF_FAILOUT)
5805 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5808 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5810 if (bk->lb_param & LPF_FAILOUT)
5811 GOTO(out, rc = PTR_ERR(lnr));
5816 lar = &lnr->lnr_lar;
5817 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5818 lfsck_namespace_assistant_req_fini(env, lar);
5819 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5822 if (unlikely(!thread_is_running(thread)))
5826 rc = iops->next(env, di);
5830 lfsck_close_dir(env, lfsck, rc);
5834 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5835 if (list_empty(&lad->lad_req_list))
5838 spin_lock(&lad->lad_lock);
5839 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5841 list_del_init(&lar->lar_list);
5842 spin_unlock(&lad->lad_lock);
5844 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5845 lfsck_namespace_assistant_req_fini(env, lar);
5847 RETURN(rc == 0 ? 1 : rc);
5851 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
5852 struct lfsck_component *com,
5853 struct dt_object *obj, bool first)
5855 struct lfsck_instance *lfsck = com->lc_lfsck;
5856 struct ptlrpc_thread *thread = &lfsck->li_thread;
5857 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5858 struct lfsck_namespace *ns = com->lc_file_ram;
5859 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5860 struct dt_object *target;
5868 di = iops->init(env, obj, 0, BYPASS_CAPA);
5870 RETURN(PTR_ERR(di));
5873 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5876 rc = iops->get(env, di, (const struct dt_key *)&fid);
5881 /* The start one either has been processed or does not exist,
5883 rc = iops->next(env, di);
5889 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5890 unlikely(!thread_is_running(thread)))
5893 key = iops->key(env, di);
5902 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5903 if (!fid_is_sane(&fid)) {
5908 target = lfsck_object_find_bottom(env, lfsck, &fid);
5909 if (IS_ERR(target)) {
5910 rc = PTR_ERR(target);
5914 if (dt_object_exists(target)) {
5915 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5917 rc = lfsck_namespace_double_scan_one(env, com,
5924 lfsck_object_put(env, target);
5927 down_write(&com->lc_sem);
5928 com->lc_new_checked++;
5929 com->lc_new_scanned++;
5930 if (rc >= 0 && fid_is_sane(&fid))
5931 ns->ln_fid_latest_scanned_phase2 = fid;
5933 ns->ln_objs_repaired_phase2++;
5935 ns->ln_objs_failed_phase2++;
5936 up_write(&com->lc_sem);
5938 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5941 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
5942 cfs_time_current())) &&
5943 com->lc_new_checked != 0) {
5944 down_write(&com->lc_sem);
5945 ns->ln_run_time_phase2 +=
5946 cfs_duration_sec(cfs_time_current() +
5947 HALF_SEC - com->lc_time_last_checkpoint);
5948 ns->ln_time_last_checkpoint = cfs_time_current_sec();
5949 ns->ln_objs_checked_phase2 += com->lc_new_checked;
5950 com->lc_new_checked = 0;
5951 rc = lfsck_namespace_store(env, com, false);
5952 up_write(&com->lc_sem);
5956 com->lc_time_last_checkpoint = cfs_time_current();
5957 com->lc_time_next_checkpoint =
5958 com->lc_time_last_checkpoint +
5959 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5962 lfsck_control_speed_by_self(com);
5963 if (unlikely(!thread_is_running(thread)))
5966 rc = iops->next(env, di);
5975 iops->fini(env, di);
5980 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5981 struct lfsck_component *com)
5983 struct lfsck_instance *lfsck = com->lc_lfsck;
5984 struct lfsck_namespace *ns = com->lc_file_ram;
5989 while (!list_empty(&lfsck->li_list_lmv)) {
5990 struct lfsck_lmv_unit *llu;
5992 spin_lock(&lfsck->li_lock);
5993 llu = list_entry(lfsck->li_list_lmv.next,
5994 struct lfsck_lmv_unit, llu_link);
5995 list_del_init(&llu->llu_link);
5996 spin_unlock(&lfsck->li_lock);
5998 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6003 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6004 lfsck_lfsck2name(lfsck));
6006 lfsck_namespace_scan_local_lpf(env, com);
6008 com->lc_new_checked = 0;
6009 com->lc_new_scanned = 0;
6010 com->lc_time_last_checkpoint = cfs_time_current();
6011 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6012 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6014 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6015 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6016 com->lc_sub_trace_objs[i].lsto_obj, true);
6017 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6018 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6019 com->lc_sub_trace_objs[i].lsto_obj, false);
6021 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6022 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6027 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6028 struct lfsck_component *com,
6029 struct lfsck_position *pos)
6031 struct lfsck_assistant_data *lad = com->lc_data;
6032 struct lfsck_namespace_req *lnr;
6034 if (list_empty(&lad->lad_req_list))
6037 lnr = list_entry(lad->lad_req_list.next,
6038 struct lfsck_namespace_req,
6040 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6041 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6042 pos->lp_dir_parent = lnr->lnr_lar.lar_fid;
6045 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6046 struct lfsck_component *com,
6049 struct lfsck_instance *lfsck = com->lc_lfsck;
6050 struct lfsck_namespace *ns = com->lc_file_ram;
6052 down_write(&com->lc_sem);
6053 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6054 HALF_SEC - lfsck->li_time_last_checkpoint);
6055 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6056 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6057 com->lc_new_checked = 0;
6060 if (ns->ln_flags & LF_INCOMPLETE)
6061 ns->ln_status = LS_PARTIAL;
6063 ns->ln_status = LS_COMPLETED;
6064 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6065 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6066 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6067 ns->ln_success_count++;
6068 } else if (rc == 0) {
6069 if (lfsck->li_status != 0)
6070 ns->ln_status = lfsck->li_status;
6072 ns->ln_status = LS_STOPPED;
6074 ns->ln_status = LS_FAILED;
6077 rc = lfsck_namespace_store(env, com, false);
6078 up_write(&com->lc_sem);
6084 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6085 struct ptlrpc_request *req,
6089 struct lfsck_async_interpret_args *laia = args;
6090 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6092 ltd->ltd_synced_failures = 1;
6099 * Notify remote LFSCK instances about former failures.
6101 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6102 * some LFSCK verification requests (maybe because of network issues or the MDT
6103 * itself trouble). During the respond gap the MDT may missed some name entries
6104 * verification, then the MDT cannot know whether related MDT-objects have been
6105 * referenced by related name entries or not, then in the second-stage scanning,
6106 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6107 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6108 * name entry for repairing the orphan.
6110 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6111 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6112 * have ever missed some name entries verification and should skip the handling
6113 * for orphan MDT-objects.
6115 * \param[in] env pointer to the thread context
6116 * \param[in] com pointer to the lfsck component
6117 * \param[in] lr pointer to the lfsck request
6119 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6120 struct lfsck_component *com,
6121 struct lfsck_request *lr)
6123 struct lfsck_async_interpret_args *laia =
6124 &lfsck_env_info(env)->lti_laia2;
6125 struct lfsck_assistant_data *lad = com->lc_data;
6126 struct lfsck_namespace *ns = com->lc_file_ram;
6127 struct lfsck_instance *lfsck = com->lc_lfsck;
6128 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6129 struct lfsck_tgt_desc *ltd;
6130 struct ptlrpc_request_set *set;
6135 if (!lad->lad_incomplete)
6138 set = ptlrpc_prep_set();
6140 GOTO(out, rc = -ENOMEM);
6142 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6143 memset(laia, 0, sizeof(*laia));
6144 lad->lad_touch_gen++;
6146 down_read(<ds->ltd_rw_sem);
6147 cfs_foreach_bit(lad->lad_bitmap, idx) {
6148 ltd = LTD_TGT(ltds, idx);
6149 LASSERT(ltd != NULL);
6151 laia->laia_ltd = ltd;
6152 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6153 lfsck_namespace_assistant_sync_failures_interpret,
6154 laia, LFSCK_NOTIFY);
6156 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6157 "to sync failure with MDT %x: rc = %d\n",
6158 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6160 up_read(<ds->ltd_rw_sem);
6162 rc = ptlrpc_set_wait(set);
6163 ptlrpc_set_destroy(set);
6169 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6170 "to sync failure with MDTs, and related MDTs "
6171 "may handle orphan improperly: rc = %d\n",
6172 lfsck_lfsck2name(lfsck), rc);
6177 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6178 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6179 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6180 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6181 .la_double_scan_result = lfsck_namespace_double_scan_result,
6182 .la_req_fini = lfsck_namespace_assistant_req_fini,
6183 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6187 * Verify the specified linkEA entry for the given directory object.
6188 * If the object has no such linkEA entry or it has more other linkEA
6189 * entries, then re-generate the linkEA with the given information.
6191 * \param[in] env pointer to the thread context
6192 * \param[in] obj pointer to the dt_object to be handled
6193 * \param[in] cname the name for the child in the parent directory
6194 * \param[in] pfid the parent directory's FID for the linkEA
6196 * \retval 0 for success
6197 * \retval negative error number on failure
6199 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6200 const struct lu_name *cname, const struct lu_fid *pfid)
6202 struct dt_device *dev = lfsck_obj2dev(obj);
6203 struct linkea_data ldata = { NULL };
6204 struct lu_buf linkea_buf;
6207 int fl = LU_XATTR_CREATE;
6211 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6213 rc = lfsck_links_read(env, obj, &ldata);
6214 if (rc == -ENODATA) {
6216 } else if (rc == 0) {
6217 fl = LU_XATTR_REPLACE;
6218 if (ldata.ld_leh->leh_reccount != 1) {
6221 rc = linkea_links_find(&ldata, cname, pfid);
6230 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6234 rc = linkea_add_buf(&ldata, cname, pfid);
6238 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6239 ldata.ld_leh->leh_len);
6240 th = dt_trans_create(env, dev);
6242 RETURN(PTR_ERR(th));
6244 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6245 XATTR_NAME_LINK, fl, th);
6249 rc = dt_trans_start_local(env, dev, th);
6253 dt_write_lock(env, obj, 0);
6254 rc = dt_xattr_set(env, obj, &linkea_buf,
6255 XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6256 dt_write_unlock(env, obj);
6261 dt_trans_stop(env, dev, th);
6266 * Get the name and parent directory's FID from the first linkEA entry.
6268 * \param[in] env pointer to the thread context
6269 * \param[in] obj pointer to the object which get linkEA from
6270 * \param[out] name pointer to the buffer to hold the name
6271 * in the first linkEA entry
6272 * \param[out] pfid pointer to the buffer to hold the parent
6273 * directory's FID in the first linkEA entry
6275 * \retval 0 for success
6276 * \retval negative error number on failure
6278 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6279 char *name, struct lu_fid *pfid)
6281 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6282 struct linkea_data ldata = { NULL };
6285 rc = lfsck_links_read(env, obj, &ldata);
6289 linkea_first_entry(&ldata);
6290 if (ldata.ld_lee == NULL)
6293 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6294 /* To guarantee the 'name' is terminated with '0'. */
6295 memcpy(name, cname->ln_name, cname->ln_namelen);
6296 name[cname->ln_namelen] = 0;
6302 * Update the object's name entry with the given FID.
6304 * \param[in] env pointer to the thread context
6305 * \param[in] lfsck pointer to the lfsck instance
6306 * \param[in] dir pointer to the directory that holds
6308 * \param[in] name the name for the entry to be updated
6309 * \param[in] fid the new FID for the name entry referenced
6310 * \param[in] type the type for the name entry to be updated
6312 * \retval 0 for success
6313 * \retval negative error number on failure
6315 int lfsck_update_name_entry(const struct lu_env *env,
6316 struct lfsck_instance *lfsck,
6317 struct dt_object *dir, const char *name,
6318 const struct lu_fid *fid, __u32 type)
6320 struct dt_insert_rec *rec = &lfsck_env_info(env)->lti_dt_rec;
6321 struct dt_device *dev = lfsck_obj2dev(dir);
6322 struct lustre_handle lh = { 0 };
6328 rc = lfsck_ibits_lock(env, lfsck, dir, &lh,
6329 MDS_INODELOCK_UPDATE, LCK_EX);
6333 th = dt_trans_create(env, dev);
6335 GOTO(unlock, rc = PTR_ERR(th));
6337 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6341 rec->rec_type = type;
6343 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6344 (const struct dt_key *)name, th);
6348 rc = dt_declare_ref_add(env, dir, th);
6352 rc = dt_trans_start_local(env, dev, th);
6356 rc = dt_delete(env, dir, (const struct dt_key *)name, th,
6358 if (rc == -ENOENT) {
6366 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6367 (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6368 if (rc == 0 && S_ISDIR(type) && !exists) {
6369 dt_write_lock(env, dir, 0);
6370 rc = dt_ref_add(env, dir, th);
6371 dt_write_unlock(env, dir);
6377 dt_trans_stop(env, dev, th);
6380 lfsck_ibits_unlock(&lh, LCK_EX);
6381 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6382 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6383 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6388 int lfsck_namespace_setup(const struct lu_env *env,
6389 struct lfsck_instance *lfsck)
6391 struct lfsck_component *com;
6392 struct lfsck_namespace *ns;
6393 struct dt_object *root = NULL;
6394 struct dt_object *obj;
6399 LASSERT(lfsck->li_master);
6405 INIT_LIST_HEAD(&com->lc_link);
6406 INIT_LIST_HEAD(&com->lc_link_dir);
6407 init_rwsem(&com->lc_sem);
6408 atomic_set(&com->lc_ref, 1);
6409 com->lc_lfsck = lfsck;
6410 com->lc_type = LFSCK_TYPE_NAMESPACE;
6411 com->lc_ops = &lfsck_namespace_ops;
6412 com->lc_data = lfsck_assistant_data_init(
6413 &lfsck_namespace_assistant_ops,
6415 if (com->lc_data == NULL)
6416 GOTO(out, rc = -ENOMEM);
6418 com->lc_file_size = sizeof(struct lfsck_namespace);
6419 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6420 if (com->lc_file_ram == NULL)
6421 GOTO(out, rc = -ENOMEM);
6423 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6424 if (com->lc_file_disk == NULL)
6425 GOTO(out, rc = -ENOMEM);
6427 for (i = 0; i < LFSCK_STF_COUNT; i++)
6428 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6430 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6432 GOTO(out, rc = PTR_ERR(root));
6434 if (unlikely(!dt_try_as_dir(env, root)))
6435 GOTO(out, rc = -ENOTDIR);
6437 obj = local_index_find_or_create(env, lfsck->li_los, root,
6439 S_IFREG | S_IRUGO | S_IWUSR,
6440 &dt_lfsck_features);
6442 GOTO(out, rc = PTR_ERR(obj));
6445 rc = lfsck_namespace_load(env, com);
6447 rc = lfsck_namespace_init(env, com);
6449 rc = lfsck_namespace_reset(env, com, true);
6451 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6455 ns = com->lc_file_ram;
6456 switch (ns->ln_status) {
6461 spin_lock(&lfsck->li_lock);
6462 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6463 spin_unlock(&lfsck->li_lock);
6466 CERROR("%s: unknown lfsck_namespace status %d\n",
6467 lfsck_lfsck2name(lfsck), ns->ln_status);
6469 case LS_SCANNING_PHASE1:
6470 case LS_SCANNING_PHASE2:
6471 /* No need to store the status to disk right now.
6472 * If the system crashed before the status stored,
6473 * it will be loaded back when next time. */
6474 ns->ln_status = LS_CRASHED;
6478 spin_lock(&lfsck->li_lock);
6479 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6480 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6481 spin_unlock(&lfsck->li_lock);
6488 if (root != NULL && !IS_ERR(root))
6489 lfsck_object_put(env, root);
6491 lfsck_component_cleanup(env, com);
6492 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6493 lfsck_lfsck2name(lfsck), rc);