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);
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);
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);
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);
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);
455 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
457 rc = dt_xattr_set(env, obj, &tbuf,
458 XATTR_NAME_LFSCK_NAMESPACE_OLD,
459 LU_XATTR_CREATE, handle);
465 dt_trans_stop(env, dev, handle);
469 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
470 lfsck_lfsck2name(lfsck), rc);
474 static struct dt_object *
475 lfsck_namespace_load_one_trace_file(const struct lu_env *env,
476 struct lfsck_component *com,
477 struct dt_object *parent,
478 const char *name, bool reset)
480 struct lfsck_instance *lfsck = com->lc_lfsck;
481 struct dt_object *obj;
485 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
486 if (rc != 0 && rc != -ENOENT)
490 obj = local_index_find_or_create(env, lfsck->li_los, parent, name,
491 S_IFREG | S_IRUGO | S_IWUSR,
497 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
498 struct lfsck_component *com,
501 char *name = lfsck_env_info(env)->lti_key;
502 struct lfsck_sub_trace_obj *lsto;
503 struct dt_object *obj;
507 for (i = 0, lsto = &com->lc_sub_trace_objs[0];
508 i < LFSCK_STF_COUNT; i++, lsto++) {
509 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
510 if (lsto->lsto_obj != NULL) {
514 lfsck_object_put(env, lsto->lsto_obj);
515 lsto->lsto_obj = NULL;
518 obj = lfsck_namespace_load_one_trace_file(env, com,
519 com->lc_lfsck->li_lfsck_dir, name, reset);
523 lsto->lsto_obj = obj;
524 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
532 static int lfsck_namespace_init(const struct lu_env *env,
533 struct lfsck_component *com)
535 struct lfsck_namespace *ns = com->lc_file_ram;
538 memset(ns, 0, sizeof(*ns));
539 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
540 ns->ln_status = LS_INIT;
541 down_write(&com->lc_sem);
542 rc = lfsck_namespace_store(env, com, true);
543 up_write(&com->lc_sem);
545 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
551 * Update the namespace LFSCK trace file for the given @fid
553 * \param[in] env pointer to the thread context
554 * \param[in] com pointer to the lfsck component
555 * \param[in] fid the fid which flags to be updated in the lfsck
557 * \param[in] add true if add new flags, otherwise remove flags
559 * \retval 0 for succeed or nothing to be done
560 * \retval negative error number on failure
562 int lfsck_namespace_trace_update(const struct lu_env *env,
563 struct lfsck_component *com,
564 const struct lu_fid *fid,
565 const __u8 flags, bool add)
567 struct lfsck_instance *lfsck = com->lc_lfsck;
568 struct dt_object *obj;
569 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
570 struct dt_device *dev;
571 struct thandle *th = NULL;
580 if (unlikely(!fid_is_sane(fid)))
583 idx = lfsck_sub_trace_file_fid2idx(fid);
584 obj = com->lc_sub_trace_objs[idx].lsto_obj;
585 dev = lfsck_obj2dev(obj);
586 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
587 fid_cpu_to_be(key, fid);
588 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
589 (const struct dt_key *)key);
592 GOTO(unlock, rc = 0);
596 } else if (rc == 0) {
598 if ((old & flags) == flags)
599 GOTO(unlock, rc = 0);
603 if ((old & flags) == 0)
604 GOTO(unlock, rc = 0);
612 th = dt_trans_create(env, dev);
614 GOTO(log, rc = PTR_ERR(th));
617 rc = dt_declare_delete(env, obj,
618 (const struct dt_key *)key, th);
624 rc = dt_declare_insert(env, obj,
625 (const struct dt_rec *)&new,
626 (const struct dt_key *)key, th);
631 rc = dt_trans_start_local(env, dev, th);
636 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
642 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
643 (const struct dt_key *)key, th, 1);
651 if (th != NULL && !IS_ERR(th))
652 dt_trans_stop(env, dev, th);
654 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
655 "trace file, flags %x, old %x, new %x: rc = %d\n",
656 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
657 (__u32)flags, (__u32)old, (__u32)new, rc);
660 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
665 int lfsck_namespace_check_exist(const struct lu_env *env,
666 struct dt_object *dir,
667 struct dt_object *obj, const char *name)
669 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
673 if (unlikely(lfsck_is_dead_obj(obj)))
674 RETURN(LFSCK_NAMEENTRY_DEAD);
676 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
677 (const struct dt_key *)name);
679 RETURN(LFSCK_NAMEENTRY_REMOVED);
684 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
685 RETURN(LFSCK_NAMEENTRY_RECREATED);
690 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
691 struct dt_object *obj,
692 struct thandle *handle)
696 /* For destroying all invalid linkEA entries. */
697 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
701 /* For insert new linkEA entry. */
702 rc = dt_declare_xattr_set(env, obj,
703 lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
704 XATTR_NAME_LINK, 0, handle);
708 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
709 struct linkea_data *ldata)
713 if (ldata->ld_buf->lb_buf == NULL)
716 if (!dt_object_exists(obj))
719 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
721 /* Buf was too small, figure out what we need. */
722 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
726 lu_buf_realloc(ldata->ld_buf, rc);
727 if (ldata->ld_buf->lb_buf == NULL)
730 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
734 rc = linkea_init(ldata);
740 * Remove linkEA for the given object.
742 * The caller should take the ldlm lock before the calling.
744 * \param[in] env pointer to the thread context
745 * \param[in] com pointer to the lfsck component
746 * \param[in] obj pointer to the dt_object to be handled
748 * \retval 0 for repaired cases
749 * \retval negative error number on failure
751 static int lfsck_namespace_links_remove(const struct lu_env *env,
752 struct lfsck_component *com,
753 struct dt_object *obj)
755 struct lfsck_instance *lfsck = com->lc_lfsck;
756 struct dt_device *dev = lfsck_obj2dev(obj);
757 struct thandle *th = NULL;
761 LASSERT(dt_object_remote(obj) == 0);
763 th = dt_trans_create(env, dev);
765 GOTO(log, rc = PTR_ERR(th));
767 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
771 rc = dt_trans_start_local(env, dev, th);
775 dt_write_lock(env, obj, 0);
776 if (unlikely(lfsck_is_dead_obj(obj)))
777 GOTO(unlock, rc = -ENOENT);
779 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
780 GOTO(unlock, rc = 0);
782 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
787 dt_write_unlock(env, obj);
790 dt_trans_stop(env, dev, th);
793 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
794 "for the object "DFID": rc = %d\n",
795 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
798 struct lfsck_namespace *ns = com->lc_file_ram;
800 ns->ln_flags |= LF_INCONSISTENT;
806 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
807 struct linkea_data *ldata, struct thandle *handle)
809 const struct lu_buf *buf = lfsck_buf_get_const(env,
810 ldata->ld_buf->lb_buf,
811 ldata->ld_leh->leh_len);
813 return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle);
816 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
817 struct lu_name *cname,
821 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
822 /* To guarantee the 'name' is terminated with '0'. */
823 memcpy(buf, cname->ln_name, cname->ln_namelen);
824 buf[cname->ln_namelen] = 0;
825 cname->ln_name = buf;
828 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
829 struct lu_name *cname,
833 struct link_ea_entry *oldlee;
837 oldlee = ldata->ld_lee;
838 oldlen = ldata->ld_reclen;
839 linkea_next_entry(ldata);
840 while (ldata->ld_lee != NULL) {
841 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
842 ldata->ld_lee->lee_reclen[1];
843 if (unlikely(ldata->ld_reclen == oldlen &&
844 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
849 linkea_del_buf(ldata, cname);
851 linkea_next_entry(ldata);
854 ldata->ld_lee = oldlee;
855 ldata->ld_reclen = oldlen;
861 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
863 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
864 * with the given type to generate the name, the detailed rules for name
865 * have been described as following.
867 * The function also generates the linkEA corresponding to the name entry
868 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
870 * \param[in] env pointer to the thread context
871 * \param[in] com pointer to the lfsck component
872 * \param[in] orphan pointer to the orphan MDT-object
873 * \param[in] infix additional information for the orphan name, such as
874 * the FID for original
875 * \param[in] type the type for describing why the orphan MDT-object is
876 * created. The rules are as following:
878 * type "D": The MDT-object is a directory, it may knows its parent
879 * but because there is no valid linkEA, the LFSCK cannot
880 * know where to put it back to the namespace.
881 * type "O": The MDT-object has no linkEA, and there is no name
882 * entry that references the MDT-object.
884 * type "S": The orphan MDT-object is a shard of a striped directory
886 * \see lfsck_layout_recreate_parent() for more types.
888 * The orphan name will be like:
889 * ${FID}-${infix}-${type}-${conflict_version}
891 * \param[out] count if some others inserted some linkEA entries by race,
892 * then return the linkEA entries count.
894 * \retval positive number for repaired cases
895 * \retval 0 if needs to repair nothing
896 * \retval negative error number on failure
898 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
899 struct lfsck_component *com,
900 struct dt_object *orphan,
901 const char *infix, const char *type,
904 struct lfsck_thread_info *info = lfsck_env_info(env);
905 struct lu_name *cname = &info->lti_name;
906 struct dt_insert_rec *rec = &info->lti_dt_rec;
907 struct lu_attr *la = &info->lti_la3;
908 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
909 const struct lu_fid *pfid;
911 struct lfsck_instance *lfsck = com->lc_lfsck;
912 struct dt_device *dev = lfsck_obj2dev(orphan);
913 struct dt_object *parent;
914 struct thandle *th = NULL;
915 struct lfsck_lock_handle *pllh = &info->lti_llh;
916 struct lustre_handle clh = { 0 };
917 struct linkea_data ldata = { NULL };
918 struct lu_buf linkea_buf;
925 cname->ln_name = NULL;
926 if (unlikely(lfsck->li_lpf_obj == NULL))
927 GOTO(log, rc = -ENXIO);
929 parent = lfsck->li_lpf_obj;
930 pfid = lfsck_dto2fid(parent);
934 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
935 PFID(cfid), infix, type, idx++);
936 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
937 (const struct dt_key *)info->lti_key);
938 if (rc != 0 && rc != -ENOENT)
941 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
943 } while (rc == 0 && !exist);
945 rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
946 MDS_INODELOCK_UPDATE, LCK_PW);
950 /* Re-check whether the name conflict with othrs after taken
952 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
953 (const struct dt_key *)info->lti_key);
955 if (!lu_fid_eq(cfid, &tfid)) {
962 } else if (rc != -ENOENT) {
968 cname->ln_name = info->lti_key;
969 cname->ln_namelen = namelen;
970 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
974 rc = linkea_add_buf(&ldata, cname, pfid);
978 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
979 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
980 MDS_INODELOCK_XATTR, LCK_EX);
984 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
985 ldata.ld_leh->leh_len);
986 th = dt_trans_create(env, dev);
988 GOTO(log, rc = PTR_ERR(th));
990 if (S_ISDIR(lfsck_object_type(orphan))) {
991 rc = dt_declare_delete(env, orphan,
992 (const struct dt_key *)dotdot, th);
996 rec->rec_type = S_IFDIR;
998 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
999 (const struct dt_key *)dotdot, th);
1004 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1005 XATTR_NAME_LINK, 0, th);
1010 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1011 rec->rec_fid = cfid;
1012 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1013 (const struct dt_key *)cname->ln_name,
1018 if (S_ISDIR(rec->rec_type)) {
1019 rc = dt_declare_ref_add(env, parent, th);
1025 memset(la, 0, sizeof(*la));
1026 la->la_ctime = cfs_time_current_sec();
1027 la->la_valid = LA_CTIME;
1028 rc = dt_declare_attr_set(env, orphan, la, th);
1032 rc = dt_trans_start_local(env, dev, th);
1036 dt_write_lock(env, orphan, 0);
1037 rc = lfsck_links_read(env, orphan, &ldata);
1038 if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
1039 (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
1040 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1041 GOTO(unlock, rc = 1);
1043 if (S_ISDIR(lfsck_object_type(orphan))) {
1044 rc = dt_delete(env, orphan,
1045 (const struct dt_key *)dotdot, th);
1049 rec->rec_type = S_IFDIR;
1050 rec->rec_fid = pfid;
1051 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1052 (const struct dt_key *)dotdot, th, 1);
1057 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1060 if (rc == 0 && count != NULL)
1061 *count = ldata.ld_leh->leh_reccount;
1065 dt_write_unlock(env, orphan);
1067 if (rc == 0 && !exist) {
1068 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1069 rec->rec_fid = cfid;
1070 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1071 (const struct dt_key *)cname->ln_name, th, 1);
1072 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1073 dt_write_lock(env, parent, 0);
1074 rc = dt_ref_add(env, parent, th);
1075 dt_write_unlock(env, parent);
1080 rc = dt_attr_set(env, orphan, la, th);
1082 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1085 dt_write_unlock(env, orphan);
1088 dt_trans_stop(env, dev, th);
1091 lfsck_ibits_unlock(&clh, LCK_EX);
1093 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1094 "object "DFID", name = %s: rc = %d\n",
1095 lfsck_lfsck2name(lfsck), PFID(cfid),
1096 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1099 struct lfsck_namespace *ns = com->lc_file_ram;
1101 ns->ln_flags |= LF_INCONSISTENT;
1108 * Add the specified name entry back to namespace.
1110 * If there is a linkEA entry that back references a name entry under
1111 * some parent directory, but such parent directory does not have the
1112 * claimed name entry. On the other hand, the linkEA entries count is
1113 * not larger than the MDT-object's hard link count. Under such case,
1114 * it is quite possible that the name entry is lost. Then the LFSCK
1115 * should add the name entry back to the namespace.
1117 * \param[in] env pointer to the thread context
1118 * \param[in] com pointer to the lfsck component
1119 * \param[in] parent pointer to the directory under which the name entry
1120 * will be inserted into
1121 * \param[in] child pointer to the object referenced by the name entry
1122 * that to be inserted into the parent
1123 * \param[in] name the name for the child in the parent directory
1125 * \retval positive number for repaired cases
1126 * \retval 0 if nothing to be repaired
1127 * \retval negative error number on failure
1129 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1130 struct lfsck_component *com,
1131 struct dt_object *parent,
1132 struct dt_object *child,
1135 struct lfsck_thread_info *info = lfsck_env_info(env);
1136 struct lu_attr *la = &info->lti_la;
1137 struct dt_insert_rec *rec = &info->lti_dt_rec;
1138 struct lfsck_instance *lfsck = com->lc_lfsck;
1139 /* The child and its name may be on different MDTs. */
1140 struct dt_device *dev = lfsck->li_next;
1141 struct dt_object *pobj = NULL;
1142 struct dt_object *cobj = NULL;
1143 struct thandle *th = NULL;
1144 struct lfsck_lock_handle *llh = &info->lti_llh;
1148 /* @parent/@child may be based on lfsck->li_bottom,
1149 * but here we need the object based on the lfsck->li_next. */
1151 pobj = lfsck_object_locate(dev, parent);
1153 GOTO(log, rc = PTR_ERR(pobj));
1155 if (unlikely(!dt_try_as_dir(env, pobj)))
1156 GOTO(log, rc = -ENOTDIR);
1158 cobj = lfsck_object_locate(dev, child);
1160 GOTO(log, rc = PTR_ERR(cobj));
1162 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1165 rc = lfsck_lock(env, lfsck, parent, name, llh,
1166 MDS_INODELOCK_UPDATE, LCK_PW);
1170 th = dt_trans_create(env, dev);
1172 GOTO(unlock, rc = PTR_ERR(th));
1174 rec->rec_type = lfsck_object_type(cobj) & S_IFMT;
1175 rec->rec_fid = lfsck_dto2fid(cobj);
1176 rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1177 (const struct dt_key *)name, th);
1181 if (S_ISDIR(rec->rec_type)) {
1182 rc = dt_declare_ref_add(env, pobj, th);
1187 memset(la, 0, sizeof(*la));
1188 la->la_ctime = cfs_time_current_sec();
1189 la->la_valid = LA_CTIME;
1190 rc = dt_declare_attr_set(env, pobj, la, th);
1194 rc = dt_declare_attr_set(env, cobj, la, th);
1198 rc = dt_trans_start_local(env, dev, th);
1202 rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1203 (const struct dt_key *)name, th, 1);
1207 if (S_ISDIR(rec->rec_type)) {
1208 dt_write_lock(env, pobj, 0);
1209 rc = dt_ref_add(env, pobj, th);
1210 dt_write_unlock(env, pobj);
1215 la->la_ctime = cfs_time_current_sec();
1216 rc = dt_attr_set(env, pobj, la, th);
1220 rc = dt_attr_set(env, cobj, la, th);
1222 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1225 dt_trans_stop(env, dev, th);
1231 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1232 "the name %s and type %o to the parent "DFID": rc = %d\n",
1233 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1234 lfsck_object_type(child) & S_IFMT,
1235 PFID(lfsck_dto2fid(parent)), rc);
1238 struct lfsck_namespace *ns = com->lc_file_ram;
1240 ns->ln_flags |= LF_INCONSISTENT;
1242 ns->ln_lost_dirent_repaired++;
1249 * Create the specified orphan directory.
1251 * For the case that the parent MDT-object stored in some MDT-object's
1252 * linkEA entry is lost, the LFSCK will re-create the parent object as
1253 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1254 * with the name ${FID}-P-${conflict_version}.
1256 * \param[in] env pointer to the thread context
1257 * \param[in] com pointer to the lfsck component
1258 * \param[in] orphan pointer to the orphan MDT-object to be created
1259 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1261 * \retval positive number for repaired cases
1262 * \retval negative error number on failure
1264 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1265 struct lfsck_component *com,
1266 struct dt_object *orphan,
1267 struct lmv_mds_md_v1 *lmv)
1269 struct lfsck_thread_info *info = lfsck_env_info(env);
1270 struct lu_attr *la = &info->lti_la;
1271 struct dt_allocation_hint *hint = &info->lti_hint;
1272 struct dt_object_format *dof = &info->lti_dof;
1273 struct lu_name *cname = &info->lti_name2;
1274 struct dt_insert_rec *rec = &info->lti_dt_rec;
1275 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1276 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1278 struct lfsck_instance *lfsck = com->lc_lfsck;
1279 struct lfsck_namespace *ns = com->lc_file_ram;
1280 struct dt_device *dev = lfsck_obj2dev(orphan);
1281 struct dt_object *parent = NULL;
1282 struct thandle *th = NULL;
1283 struct lfsck_lock_handle *llh = &info->lti_llh;
1284 struct linkea_data ldata = { NULL };
1285 struct lu_buf linkea_buf;
1286 struct lu_buf lmv_buf;
1293 LASSERT(!dt_object_exists(orphan));
1295 cname->ln_name = NULL;
1296 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1299 if (dt_object_remote(orphan)) {
1300 LASSERT(lfsck->li_lpf_root_obj != NULL);
1302 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1304 GOTO(log, rc = idx);
1306 snprintf(name, 8, "MDT%04x", idx);
1307 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1308 (struct dt_rec *)&tfid,
1309 (const struct dt_key *)name);
1311 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1313 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1315 GOTO(log, rc = PTR_ERR(parent));
1317 if (unlikely(!dt_try_as_dir(env, parent)))
1318 GOTO(log, rc = -ENOTDIR);
1320 if (unlikely(lfsck->li_lpf_obj == NULL))
1321 GOTO(log, rc = -ENXIO);
1323 parent = lfsck->li_lpf_obj;
1326 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1328 GOTO(log, rc = PTR_ERR(dev));
1334 namelen = snprintf(name, 31, DFID"-P-%d",
1336 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1337 (const struct dt_key *)name);
1338 if (rc != 0 && rc != -ENOENT)
1342 rc = lfsck_lock(env, lfsck, parent, name, llh,
1343 MDS_INODELOCK_UPDATE, LCK_PW);
1347 /* Re-check whether the name conflict with othrs after taken
1349 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1350 (const struct dt_key *)name);
1351 if (unlikely(rc == 0)) {
1359 cname->ln_name = name;
1360 cname->ln_namelen = namelen;
1362 memset(la, 0, sizeof(*la));
1363 la->la_mode = S_IFDIR | 0700;
1364 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1365 LA_ATIME | LA_MTIME | LA_CTIME;
1367 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1368 la->la_mode & S_IFMT);
1370 memset(dof, 0, sizeof(*dof));
1371 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1373 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1377 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
1381 th = dt_trans_create(env, dev);
1383 GOTO(unlock1, rc = PTR_ERR(th));
1385 /* Sync the remote transaction to guarantee that the subsequent
1386 * lock against the @orphan can find the @orphan in time. */
1387 if (dt_object_remote(orphan))
1390 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1394 if (unlikely(!dt_try_as_dir(env, orphan)))
1395 GOTO(stop, rc = -ENOTDIR);
1397 rec->rec_type = S_IFDIR;
1398 rec->rec_fid = cfid;
1399 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1400 (const struct dt_key *)dot, th);
1404 rec->rec_fid = lfsck_dto2fid(parent);
1405 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1406 (const struct dt_key *)dotdot, th);
1408 rc = dt_declare_ref_add(env, orphan, th);
1413 rc = dt_declare_ref_add(env, orphan, th);
1418 lmv->lmv_magic = LMV_MAGIC;
1419 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1420 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1421 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1422 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1423 XATTR_NAME_LMV, 0, th);
1428 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1429 ldata.ld_leh->leh_len);
1430 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1431 XATTR_NAME_LINK, 0, th);
1435 rec->rec_fid = cfid;
1436 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1437 (const struct dt_key *)name, th);
1439 rc = dt_declare_ref_add(env, parent, th);
1444 rc = dt_trans_start_local(env, dev, th);
1448 dt_write_lock(env, orphan, 0);
1449 rc = dt_create(env, orphan, la, hint, dof, th);
1453 rec->rec_fid = cfid;
1454 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1455 (const struct dt_key *)dot, th, 1);
1459 rec->rec_fid = lfsck_dto2fid(parent);
1460 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1461 (const struct dt_key *)dotdot, th, 1);
1465 rc = dt_ref_add(env, orphan, th);
1470 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1475 rc = dt_xattr_set(env, orphan, &linkea_buf,
1476 XATTR_NAME_LINK, 0, th);
1477 dt_write_unlock(env, orphan);
1481 rec->rec_fid = cfid;
1482 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1483 (const struct dt_key *)name, th, 1);
1485 dt_write_lock(env, parent, 0);
1486 rc = dt_ref_add(env, parent, th);
1487 dt_write_unlock(env, parent);
1490 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1493 dt_write_unlock(env, orphan);
1496 dt_trans_stop(env, dev, th);
1502 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1503 "the object "DFID", name = %s: rc = %d\n",
1504 lfsck_lfsck2name(lfsck), PFID(cfid),
1505 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1507 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1508 lfsck_object_put(env, parent);
1511 ns->ln_flags |= LF_INCONSISTENT;
1517 * Remove the specified entry from the linkEA.
1519 * Locate the linkEA entry with the given @cname and @pfid, then
1520 * remove this entry or the other entries those are repeated with
1523 * \param[in] env pointer to the thread context
1524 * \param[in] com pointer to the lfsck component
1525 * \param[in] obj pointer to the dt_object to be handled
1526 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1527 * \param[in] cname the name for the child in the parent directory
1528 * \param[in] pfid the parent directory's FID for the linkEA
1529 * \param[in] next if true, then remove the first found linkEA
1530 * entry, and move the ldata->ld_lee to next entry
1532 * \retval positive number for repaired cases
1533 * \retval 0 if nothing to be repaired
1534 * \retval negative error number on failure
1536 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1537 struct lfsck_component *com,
1538 struct dt_object *obj,
1539 struct linkea_data *ldata,
1540 struct lu_name *cname,
1541 struct lu_fid *pfid,
1544 struct lfsck_instance *lfsck = com->lc_lfsck;
1545 struct dt_device *dev = lfsck_obj2dev(obj);
1546 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1547 struct thandle *th = NULL;
1548 struct lustre_handle lh = { 0 };
1549 struct linkea_data ldata_new = { NULL };
1550 struct lu_buf linkea_buf;
1554 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1555 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1561 linkea_del_buf(ldata, cname);
1563 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1565 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1566 ldata->ld_leh->leh_len);
1569 th = dt_trans_create(env, dev);
1571 GOTO(unlock1, rc = PTR_ERR(th));
1573 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1574 XATTR_NAME_LINK, 0, th);
1578 rc = dt_trans_start_local(env, dev, th);
1582 dt_write_lock(env, obj, 0);
1583 if (unlikely(lfsck_is_dead_obj(obj)))
1584 GOTO(unlock2, rc = -ENOENT);
1586 rc = lfsck_links_read2(env, obj, &ldata_new);
1590 /* The specified linkEA entry has been removed by race. */
1591 rc = linkea_links_find(&ldata_new, cname, pfid);
1593 GOTO(unlock2, rc = 0);
1595 if (bk->lb_param & LPF_DRYRUN)
1596 GOTO(unlock2, rc = 1);
1599 linkea_del_buf(&ldata_new, cname);
1601 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1604 if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1605 dt_write_unlock(env, obj);
1606 dt_trans_stop(env, dev, th);
1607 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1608 ldata_new.ld_leh->leh_len);
1612 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1613 ldata_new.ld_leh->leh_len);
1614 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK, 0, th);
1616 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1619 dt_write_unlock(env, obj);
1622 dt_trans_stop(env, dev, th);
1625 lfsck_ibits_unlock(&lh, LCK_EX);
1628 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1629 "for the object: "DFID", parent "DFID", name %.*s\n",
1630 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1631 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1635 struct lfsck_namespace *ns = com->lc_file_ram;
1637 ns->ln_flags |= LF_INCONSISTENT;
1644 * Conditionally remove the specified entry from the linkEA.
1646 * Take the parent lock firstly, then check whether the specified
1647 * name entry exists or not: if yes, do nothing; otherwise, call
1648 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1650 * \param[in] env pointer to the thread context
1651 * \param[in] com pointer to the lfsck component
1652 * \param[in] parent pointer to the parent directory
1653 * \param[in] child pointer to the child object that holds the linkEA
1654 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1655 * \param[in] cname the name for the child in the parent directory
1656 * \param[in] pfid the parent directory's FID for the linkEA
1658 * \retval positive number for repaired cases
1659 * \retval 0 if nothing to be repaired
1660 * \retval negative error number on failure
1662 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1663 struct lfsck_component *com,
1664 struct dt_object *parent,
1665 struct dt_object *child,
1666 struct linkea_data *ldata,
1667 struct lu_name *cname,
1668 struct lu_fid *pfid)
1670 struct lfsck_thread_info *info = lfsck_env_info(env);
1671 struct lu_fid *cfid = &info->lti_fid3;
1672 struct lfsck_lock_handle *llh = &info->lti_llh;
1676 rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1677 MDS_INODELOCK_UPDATE, LCK_PR);
1681 dt_read_lock(env, parent, 0);
1682 if (unlikely(lfsck_is_dead_obj(parent))) {
1683 dt_read_unlock(env, parent);
1685 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1691 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1692 (const struct dt_key *)cname->ln_name);
1693 dt_read_unlock(env, parent);
1695 /* It is safe to release the ldlm lock, because when the logic come
1696 * here, we have got all the needed information above whether the
1697 * linkEA entry is valid or not. It is not important that others
1698 * may add new linkEA entry after the ldlm lock released. If other
1699 * has removed the specified linkEA entry by race, then it is OK,
1700 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1703 if (rc == -ENOENT) {
1704 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1713 /* The LFSCK just found some internal status of cross-MDTs
1714 * create operation. That is normal. */
1715 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1716 linkea_next_entry(ldata);
1721 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1728 * Conditionally replace name entry in the parent.
1730 * As required, the LFSCK may re-create the lost MDT-object for dangling
1731 * name entry, but such repairing may be wrong because of bad FID in the
1732 * name entry. As the LFSCK processing, the real MDT-object may be found,
1733 * then the LFSCK should check whether the former re-created MDT-object
1734 * has been modified or not, if not, then destroy it and update the name
1735 * entry in the parent to reference the real MDT-object.
1737 * \param[in] env pointer to the thread context
1738 * \param[in] com pointer to the lfsck component
1739 * \param[in] parent pointer to the parent directory
1740 * \param[in] child pointer to the MDT-object that may be the real
1741 * MDT-object corresponding to the name entry in parent
1742 * \param[in] cfid the current FID in the name entry
1743 * \param[in] cname contains the name of the child in the parent directory
1745 * \retval positive number for repaired cases
1746 * \retval 0 if nothing to be repaired
1747 * \retval negative error number on failure
1749 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1750 struct lfsck_component *com,
1751 struct dt_object *parent,
1752 struct dt_object *child,
1753 const struct lu_fid *cfid,
1754 const struct lu_name *cname)
1756 struct lfsck_thread_info *info = lfsck_env_info(env);
1757 struct lu_attr *la = &info->lti_la;
1758 struct dt_insert_rec *rec = &info->lti_dt_rec;
1760 struct lfsck_instance *lfsck = com->lc_lfsck;
1761 /* The child and its name may be on different MDTs. */
1762 struct dt_device *dev = lfsck->li_next;
1763 const char *name = cname->ln_name;
1764 struct dt_object *pobj = NULL;
1765 struct dt_object *cobj = NULL;
1766 struct lfsck_lock_handle *pllh = &info->lti_llh;
1767 struct lustre_handle clh = { 0 };
1768 struct linkea_data ldata = { NULL };
1769 struct thandle *th = NULL;
1774 /* @parent/@child may be based on lfsck->li_bottom,
1775 * but here we need the object based on the lfsck->li_next. */
1777 pobj = lfsck_object_locate(dev, parent);
1779 GOTO(log, rc = PTR_ERR(pobj));
1781 if (unlikely(!dt_try_as_dir(env, pobj)))
1782 GOTO(log, rc = -ENOTDIR);
1784 rc = lfsck_lock(env, lfsck, parent, name, pllh,
1785 MDS_INODELOCK_UPDATE, LCK_PW);
1789 if (!fid_is_sane(cfid)) {
1794 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1797 if (rc == -ENOENT) {
1805 if (!dt_object_exists(cobj)) {
1810 rc = dt_lookup(env, pobj, (struct dt_rec *)&tfid,
1811 (const struct dt_key *)name);
1812 if (rc == -ENOENT) {
1820 /* Someone changed the name entry, cannot replace it. */
1821 if (!lu_fid_eq(cfid, &tfid))
1824 /* lock the object to be destroyed. */
1825 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1826 MDS_INODELOCK_UPDATE |
1827 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1832 if (unlikely(lfsck_is_dead_obj(cobj))) {
1837 rc = dt_attr_get(env, cobj, la);
1841 /* The object has been modified by other(s), or it is not created by
1842 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1843 if (la->la_ctime != 0)
1846 if (S_ISREG(la->la_mode)) {
1847 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
1848 /* If someone has created related OST-object(s),
1850 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1851 GOTO(log, rc = (rc > 0 ? 0 : rc));
1855 dt_read_lock(env, child, 0);
1856 rc = lfsck_links_read2(env, child, &ldata);
1857 dt_read_unlock(env, child);
1859 /* Someone changed the child, no need to replace. */
1866 rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(pobj));
1867 /* Someone moved the child, no need to replace. */
1871 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1874 th = dt_trans_create(env, dev);
1876 GOTO(log, rc = PTR_ERR(th));
1879 rc = dt_declare_destroy(env, cobj, th);
1884 rc = dt_declare_delete(env, pobj, (const struct dt_key *)name, th);
1888 rec->rec_type = S_IFDIR;
1889 rec->rec_fid = lfsck_dto2fid(child);
1890 rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1891 (const struct dt_key *)name, th);
1895 rc = dt_trans_start_local(env, dev, th);
1900 rc = dt_destroy(env, cobj, th);
1905 /* The old name entry maybe not exist. */
1906 rc = dt_delete(env, pobj, (const struct dt_key *)name, th);
1907 if (rc != 0 && rc != -ENOENT)
1910 rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1911 (const struct dt_key *)name, th, 1);
1913 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1916 dt_trans_stop(env, dev, th);
1919 lfsck_ibits_unlock(&clh, LCK_EX);
1922 if (cobj != NULL && !IS_ERR(cobj))
1923 lfsck_object_put(env, cobj);
1925 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1926 "object "DFID" because of conflict with the object "DFID
1927 " under the parent "DFID" with name %s: rc = %d\n",
1928 lfsck_lfsck2name(lfsck), PFID(cfid),
1929 PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1936 * Overwrite the linkEA for the object with the given ldata.
1938 * The caller should take the ldlm lock before the calling.
1940 * \param[in] env pointer to the thread context
1941 * \param[in] com pointer to the lfsck component
1942 * \param[in] obj pointer to the dt_object to be handled
1943 * \param[in] ldata pointer to the new linkEA data
1945 * \retval positive number for repaired cases
1946 * \retval 0 if nothing to be repaired
1947 * \retval negative error number on failure
1949 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1950 struct lfsck_component *com,
1951 struct dt_object *obj,
1952 struct linkea_data *ldata)
1954 struct lfsck_instance *lfsck = com->lc_lfsck;
1955 struct dt_device *dev = lfsck_obj2dev(obj);
1956 struct thandle *th = NULL;
1957 struct lu_buf linkea_buf;
1961 th = dt_trans_create(env, dev);
1963 GOTO(log, rc = PTR_ERR(th));
1965 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1966 ldata->ld_leh->leh_len);
1967 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1968 XATTR_NAME_LINK, 0, th);
1972 rc = dt_trans_start_local(env, dev, th);
1976 dt_write_lock(env, obj, 0);
1977 if (unlikely(lfsck_is_dead_obj(obj)))
1978 GOTO(unlock, rc = 0);
1980 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1981 GOTO(unlock, rc = 1);
1983 rc = dt_xattr_set(env, obj, &linkea_buf,
1984 XATTR_NAME_LINK, 0, th);
1986 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1989 dt_write_unlock(env, obj);
1992 dt_trans_stop(env, dev, th);
1995 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1996 "object "DFID": rc = %d\n",
1997 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
2000 struct lfsck_namespace *ns = com->lc_file_ram;
2002 ns->ln_flags |= LF_INCONSISTENT;
2009 * Repair invalid name entry.
2011 * If the name entry contains invalid information, such as bad file type
2012 * or (and) corrupted object FID, then either remove the name entry or
2013 * udpate the name entry with the given (right) information.
2015 * \param[in] env pointer to the thread context
2016 * \param[in] com pointer to the lfsck component
2017 * \param[in] parent pointer to the parent directory
2018 * \param[in] child pointer to the object referenced by the name entry
2019 * \param[in] name the old name of the child under the parent directory
2020 * \param[in] name2 the new name of the child under the parent directory
2021 * \param[in] type the type claimed by the name entry
2022 * \param[in] update update the name entry if true; otherwise, remove it
2023 * \param[in] dec decrease the parent nlink count if true
2025 * \retval positive number for repaired successfully
2026 * \retval 0 if nothing to be repaired
2027 * \retval negative error number on failure
2029 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2030 struct lfsck_component *com,
2031 struct dt_object *parent,
2032 struct dt_object *child,
2033 const char *name, const char *name2,
2034 __u16 type, bool update, bool dec)
2036 struct lfsck_thread_info *info = lfsck_env_info(env);
2037 struct dt_insert_rec *rec = &info->lti_dt_rec;
2038 const struct lu_fid *cfid = lfsck_dto2fid(child);
2040 struct lfsck_instance *lfsck = com->lc_lfsck;
2041 struct dt_device *dev = lfsck->li_next;
2042 struct thandle *th = NULL;
2043 struct lfsck_lock_handle *llh = &info->lti_llh;
2044 struct lustre_handle lh = { 0 };
2048 if (unlikely(!dt_try_as_dir(env, parent)))
2049 GOTO(log, rc = -ENOTDIR);
2051 if (!update || strcmp(name, name2) == 0)
2052 rc = lfsck_lock(env, lfsck, parent, name, llh,
2053 MDS_INODELOCK_UPDATE, LCK_PW);
2055 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2056 MDS_INODELOCK_UPDATE, LCK_PW);
2060 th = dt_trans_create(env, dev);
2062 GOTO(unlock1, rc = PTR_ERR(th));
2064 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2069 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2070 rec->rec_fid = cfid;
2071 rc = dt_declare_insert(env, parent,
2072 (const struct dt_rec *)rec,
2073 (const struct dt_key *)name2, th);
2079 rc = dt_declare_ref_del(env, parent, th);
2084 rc = dt_trans_start_local(env, dev, th);
2088 dt_write_lock(env, parent, 0);
2089 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2090 (const struct dt_key *)name);
2091 /* Someone has removed the bad name entry by race. */
2093 GOTO(unlock2, rc = 0);
2098 /* Someone has removed the bad name entry and reused it for other
2099 * object by race. */
2100 if (!lu_fid_eq(&tfid, cfid))
2101 GOTO(unlock2, rc = 0);
2103 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2104 GOTO(unlock2, rc = 1);
2106 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2111 rc = dt_insert(env, parent,
2112 (const struct dt_rec *)rec,
2113 (const struct dt_key *)name2, th, 1);
2119 rc = dt_ref_del(env, parent, th);
2124 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2127 dt_write_unlock(env, parent);
2130 dt_trans_stop(env, dev, th);
2132 /* We are not sure whether the child will become orphan or not.
2133 * Record it in the LFSCK trace file for further checking in
2134 * the second-stage scanning. */
2135 if (!update && !dec && rc == 0)
2136 lfsck_namespace_trace_update(env, com, cfid,
2137 LNTF_CHECK_LINKEA, true);
2140 /* It is harmless even if unlock the unused lock_handle */
2141 lfsck_ibits_unlock(&lh, LCK_PW);
2145 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2146 "entry for: parent "DFID", child "DFID", name %s, type "
2147 "in name entry %o, type claimed by child %o. repair it "
2148 "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2149 PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2150 name, type, update ? lfsck_object_type(child) : 0,
2151 update ? "updating" : "removing", name2, rc);
2154 struct lfsck_namespace *ns = com->lc_file_ram;
2156 ns->ln_flags |= LF_INCONSISTENT;
2163 * Update the ".." name entry for the given object.
2165 * The object's ".." is corrupted, this function will update the ".." name
2166 * entry with the given pfid, and the linkEA with the given ldata.
2168 * The caller should take the ldlm lock before the calling.
2170 * \param[in] env pointer to the thread context
2171 * \param[in] com pointer to the lfsck component
2172 * \param[in] obj pointer to the dt_object to be handled
2173 * \param[in] pfid the new fid for the object's ".." name entry
2174 * \param[in] cname the name for the @obj in the parent directory
2176 * \retval positive number for repaired cases
2177 * \retval 0 if nothing to be repaired
2178 * \retval negative error number on failure
2180 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2181 struct lfsck_component *com,
2182 struct dt_object *obj,
2183 const struct lu_fid *pfid,
2184 struct lu_name *cname)
2186 struct lfsck_thread_info *info = lfsck_env_info(env);
2187 struct dt_insert_rec *rec = &info->lti_dt_rec;
2188 struct lfsck_instance *lfsck = com->lc_lfsck;
2189 struct dt_device *dev = lfsck_obj2dev(obj);
2190 struct thandle *th = NULL;
2191 struct linkea_data ldata = { NULL };
2192 struct lu_buf linkea_buf;
2196 LASSERT(!dt_object_remote(obj));
2197 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2199 rc = linkea_data_new(&ldata, &info->lti_big_buf);
2203 rc = linkea_add_buf(&ldata, cname, pfid);
2207 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2208 ldata.ld_leh->leh_len);
2210 th = dt_trans_create(env, dev);
2212 GOTO(log, rc = PTR_ERR(th));
2214 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2218 rec->rec_type = S_IFDIR;
2219 rec->rec_fid = pfid;
2220 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2221 (const struct dt_key *)dotdot, th);
2225 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2226 XATTR_NAME_LINK, 0, th);
2230 rc = dt_trans_start_local(env, dev, th);
2234 dt_write_lock(env, obj, 0);
2235 if (unlikely(lfsck_is_dead_obj(obj)))
2236 GOTO(unlock, rc = 0);
2238 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2239 GOTO(unlock, rc = 1);
2241 /* The old ".." name entry maybe not exist. */
2242 dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2244 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2245 (const struct dt_key *)dotdot, th, 1);
2249 rc = dt_xattr_set(env, obj, &linkea_buf,
2250 XATTR_NAME_LINK, 0, th);
2252 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2255 dt_write_unlock(env, obj);
2258 dt_trans_stop(env, dev, th);
2261 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2262 "the object "DFID", new parent "DFID": rc = %d\n",
2263 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2267 struct lfsck_namespace *ns = com->lc_file_ram;
2269 ns->ln_flags |= LF_INCONSISTENT;
2276 * Handle orphan @obj during Double Scan Directory.
2278 * Remove the @obj's current (invalid) linkEA entries, and insert
2279 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2280 * ${FID}-${PFID}-D-${conflict_version}
2282 * The caller should take the ldlm lock before the calling.
2284 * \param[in] env pointer to the thread context
2285 * \param[in] com pointer to the lfsck component
2286 * \param[in] obj pointer to the orphan object to be handled
2287 * \param[in] pfid the new fid for the object's ".." name entry
2288 * \param[in,out] lh ldlm lock handler for the given @obj
2289 * \param[out] type to tell the caller what the inconsistency is
2291 * \retval positive number for repaired cases
2292 * \retval 0 if nothing to be repaired
2293 * \retval negative error number on failure
2296 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2297 struct lfsck_component *com,
2298 struct dt_object *obj,
2299 const struct lu_fid *pfid,
2300 struct lustre_handle *lh,
2301 enum lfsck_namespace_inconsistency_type *type)
2303 struct lfsck_thread_info *info = lfsck_env_info(env);
2304 struct lfsck_namespace *ns = com->lc_file_ram;
2308 /* Remove the unrecognized linkEA. */
2309 rc = lfsck_namespace_links_remove(env, com, obj);
2310 lfsck_ibits_unlock(lh, LCK_EX);
2311 if (rc < 0 && rc != -ENODATA)
2314 *type = LNIT_MUL_REF;
2316 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2317 * ever tried to verify some remote MDT-object that resides on this
2318 * MDT, but this MDT failed to respond such request. So means there
2319 * may be some remote name entry on other MDT that references this
2320 * object with another name, so we cannot know whether this linkEA
2321 * is valid or not. So keep it there and maybe resolved when next
2323 if (ns->ln_flags & LF_INCOMPLETE)
2326 /* The unique linkEA is invalid, even if the ".." name entry may be
2327 * valid, we still cannot know via which name entry this directory
2328 * will be referenced. Then handle it as pure orphan. */
2329 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2330 "-"DFID, PFID(pfid));
2331 rc = lfsck_namespace_insert_orphan(env, com, obj,
2332 info->lti_tmpbuf, "D", NULL);
2338 * Double Scan Directory object for single linkEA entry case.
2340 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2341 * then check whether the name is in the namespace or not, if not, add the
2342 * missing name entry back to namespace. If the linkEA entry is invalid,
2343 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2346 * \param[in] env pointer to the thread context
2347 * \param[in] com pointer to the lfsck component
2348 * \param[in] child pointer to the directory to be double scanned
2349 * \param[in] pfid the FID corresponding to the ".." entry
2350 * \param[in] ldata pointer to the linkEA data for the given @child
2351 * \param[in,out] lh ldlm lock handler for the given @child
2352 * \param[out] type to tell the caller what the inconsistency is
2353 * \param[in] retry if found inconsistency, but the caller does not hold
2354 * ldlm lock on the @child, then set @retry as true
2356 * \retval positive number for repaired cases
2357 * \retval 0 if nothing to be repaired
2358 * \retval negative error number on failure
2361 lfsck_namespace_dsd_single(const struct lu_env *env,
2362 struct lfsck_component *com,
2363 struct dt_object *child,
2364 const struct lu_fid *pfid,
2365 struct linkea_data *ldata,
2366 struct lustre_handle *lh,
2367 enum lfsck_namespace_inconsistency_type *type,
2370 struct lfsck_thread_info *info = lfsck_env_info(env);
2371 struct lu_name *cname = &info->lti_name;
2372 const struct lu_fid *cfid = lfsck_dto2fid(child);
2374 struct lfsck_namespace *ns = com->lc_file_ram;
2375 struct lfsck_instance *lfsck = com->lc_lfsck;
2376 struct dt_object *parent = NULL;
2377 struct lmv_mds_md_v1 *lmv;
2381 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid, info->lti_key);
2382 /* The unique linkEA entry with bad parent will be handled as orphan. */
2383 if (!fid_is_sane(&tfid)) {
2384 if (!lustre_handle_is_used(lh) && retry != NULL)
2387 rc = lfsck_namespace_dsd_orphan(env, com, child,
2393 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2395 GOTO(out, rc = PTR_ERR(parent));
2397 /* We trust the unique linkEA entry in spite of whether it matches the
2398 * ".." name entry or not. Because even if the linkEA entry is wrong
2399 * and the ".." name entry is right, we still cannot know via which
2400 * name entry the child will be referenced, since all known entries
2401 * have been verified during the first-stage scanning. */
2402 if (!dt_object_exists(parent)) {
2403 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2404 * has ever tried to verify some remote MDT-object that resides
2405 * on this MDT, but this MDT failed to respond such request. So
2406 * means there may be some remote name entry on other MDT that
2407 * references this object with another name, so we cannot know
2408 * whether this linkEA is valid or not. So keep it there and
2409 * maybe resolved when next LFSCK run. */
2410 if (ns->ln_flags & LF_INCOMPLETE)
2413 if (!lustre_handle_is_used(lh) && retry != NULL) {
2419 lfsck_ibits_unlock(lh, LCK_EX);
2422 lmv = &info->lti_lmv;
2423 rc = lfsck_read_stripe_lmv(env, child, lmv);
2424 if (rc != 0 && rc != -ENODATA)
2427 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2429 } else if (lfsck_shard_name_to_index(env,
2430 cname->ln_name, cname->ln_namelen,
2431 S_IFDIR, cfid) < 0) {
2432 /* It is an invalid name entry, we
2433 * cannot trust the parent also. */
2434 rc = lfsck_namespace_shrink_linkea(env, com, child,
2435 ldata, cname, &tfid, true);
2439 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2440 "-"DFID, PFID(pfid));
2441 rc = lfsck_namespace_insert_orphan(env, com, child,
2442 info->lti_tmpbuf, "S", NULL);
2447 /* Create the lost parent as an orphan. */
2448 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2450 /* Add the missing name entry to the parent. */
2451 rc = lfsck_namespace_insert_normal(env, com, parent,
2452 child, cname->ln_name);
2453 if (unlikely(rc == -EEXIST)) {
2454 /* Unfortunately, someone reused the name
2455 * under the parent by race. So we have
2456 * to remove the linkEA entry from
2457 * current child object. It means that the
2458 * LFSCK cannot recover the system
2459 * totally back to its original status,
2460 * but it is necessary to make the
2461 * current system to be consistent. */
2462 rc = lfsck_namespace_shrink_linkea(env,
2464 cname, &tfid, true);
2466 snprintf(info->lti_tmpbuf,
2467 sizeof(info->lti_tmpbuf),
2468 "-"DFID, PFID(pfid));
2469 rc = lfsck_namespace_insert_orphan(env,
2470 com, child, info->lti_tmpbuf,
2479 /* The unique linkEA entry with bad parent will be handled as orphan. */
2480 if (unlikely(!dt_try_as_dir(env, parent))) {
2481 if (!lustre_handle_is_used(lh) && retry != NULL)
2484 rc = lfsck_namespace_dsd_orphan(env, com, child,
2490 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2491 (const struct dt_key *)cname->ln_name);
2492 if (rc == -ENOENT) {
2493 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2494 * has ever tried to verify some remote MDT-object that resides
2495 * on this MDT, but this MDT failed to respond such request. So
2496 * means there may be some remote name entry on other MDT that
2497 * references this object with another name, so we cannot know
2498 * whether this linkEA is valid or not. So keep it there and
2499 * maybe resolved when next LFSCK run. */
2500 if (ns->ln_flags & LF_INCOMPLETE)
2503 if (!lustre_handle_is_used(lh) && retry != NULL) {
2509 lfsck_ibits_unlock(lh, LCK_EX);
2510 rc = lfsck_namespace_check_name(env, parent, child, cname);
2517 /* It is an invalid name entry, drop it. */
2518 if (unlikely(rc > 0)) {
2519 rc = lfsck_namespace_shrink_linkea(env, com, child,
2520 ldata, cname, &tfid, true);
2522 snprintf(info->lti_tmpbuf,
2523 sizeof(info->lti_tmpbuf),
2524 "-"DFID, PFID(pfid));
2525 rc = lfsck_namespace_insert_orphan(env, com,
2526 child, info->lti_tmpbuf, "D", NULL);
2532 /* Add the missing name entry back to the namespace. */
2533 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2535 if (unlikely(rc == -ESTALE))
2536 /* It may happen when the remote object has been
2537 * removed, but the local MDT is not aware of that. */
2540 if (unlikely(rc == -EEXIST)) {
2541 /* Unfortunately, someone reused the name under the
2542 * parent by race. So we have to remove the linkEA
2543 * entry from current child object. It means that the
2544 * LFSCK cannot recover the system totally back to
2545 * its original status, but it is necessary to make
2546 * the current system to be consistent.
2548 * It also may be because of the LFSCK found some
2549 * internal status of create operation. Under such
2550 * case, nothing to be done. */
2551 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2552 parent, child, ldata, cname, &tfid);
2554 snprintf(info->lti_tmpbuf,
2555 sizeof(info->lti_tmpbuf),
2556 "-"DFID, PFID(pfid));
2557 rc = lfsck_namespace_insert_orphan(env, com,
2558 child, info->lti_tmpbuf, "D", NULL);
2568 if (!lu_fid_eq(&tfid, cfid)) {
2569 if (!lustre_handle_is_used(lh) && retry != NULL) {
2575 lfsck_ibits_unlock(lh, LCK_EX);
2576 /* The name entry references another MDT-object that
2577 * may be created by the LFSCK for repairing dangling
2578 * name entry. Try to replace it. */
2579 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2582 rc = lfsck_namespace_dsd_orphan(env, com, child,
2588 if (fid_is_zero(pfid))
2591 /* The ".." name entry is wrong, update it. */
2592 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2593 if (!lustre_handle_is_used(lh) && retry != NULL) {
2599 *type = LNIT_UNMATCHED_PAIRS;
2600 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2601 lfsck_dto2fid(parent), cname);
2607 if (parent != NULL && !IS_ERR(parent))
2608 lfsck_object_put(env, parent);
2614 * Double Scan Directory object for multiple linkEA entries case.
2616 * The given @child has multiple linkEA entries. There is at most one linkEA
2617 * entry will be valid, all the others will be removed. Firstly, the function
2618 * will try to find out the linkEA entry for which the name entry exists under
2619 * the given parent (@pfid). If there is no linkEA entry that matches the given
2620 * ".." name entry, then tries to find out the first linkEA entry that both the
2621 * parent and the name entry exist to rebuild a new ".." name entry.
2623 * \param[in] env pointer to the thread context
2624 * \param[in] com pointer to the lfsck component
2625 * \param[in] child pointer to the directory to be double scanned
2626 * \param[in] pfid the FID corresponding to the ".." entry
2627 * \param[in] ldata pointer to the linkEA data for the given @child
2628 * \param[in,out] lh ldlm lock handler for the given @child
2629 * \param[out] type to tell the caller what the inconsistency is
2630 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2632 * \retval positive number for repaired cases
2633 * \retval 0 if nothing to be repaired
2634 * \retval negative error number on failure
2637 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2638 struct lfsck_component *com,
2639 struct dt_object *child,
2640 const struct lu_fid *pfid,
2641 struct linkea_data *ldata,
2642 struct lustre_handle *lh,
2643 enum lfsck_namespace_inconsistency_type *type,
2646 struct lfsck_thread_info *info = lfsck_env_info(env);
2647 struct lu_name *cname = &info->lti_name;
2648 const struct lu_fid *cfid = lfsck_dto2fid(child);
2649 struct lu_fid *pfid2 = &info->lti_fid3;
2651 struct lfsck_namespace *ns = com->lc_file_ram;
2652 struct lfsck_instance *lfsck = com->lc_lfsck;
2653 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2654 struct dt_object *parent = NULL;
2655 struct linkea_data ldata_new = { NULL };
2656 int dirent_count = 0;
2657 int linkea_count = 0;
2663 while (ldata->ld_lee != NULL) {
2664 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2666 /* Drop repeated linkEA entries. */
2667 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2668 /* Drop invalid linkEA entry. */
2669 if (!fid_is_sane(&tfid)) {
2670 linkea_del_buf(ldata, cname);
2675 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2676 * then it is possible that: the directry object has ever
2677 * been lost, but its name entry was there. In the former
2678 * LFSCK run, during the first-stage scanning, the LFSCK
2679 * found the dangling name entry, but it did not recreate
2680 * the lost object, and when moved to the second-stage
2681 * scanning, some children objects of the lost directory
2682 * object were found, then the LFSCK recreated such lost
2683 * directory object as an orphan.
2685 * When the LFSCK runs again, if the dangling name is still
2686 * there, the LFSCK should move the orphan directory object
2687 * back to the normal namespace. */
2688 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2689 linkea_next_entry(ldata);
2693 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2695 RETURN(PTR_ERR(parent));
2697 if (!dt_object_exists(parent)) {
2698 lfsck_object_put(env, parent);
2699 if (ldata->ld_leh->leh_reccount > 1) {
2700 /* If it is NOT the last linkEA entry, then
2701 * there is still other chance to make the
2702 * child to be visible via other parent, then
2703 * remove this linkEA entry. */
2704 linkea_del_buf(ldata, cname);
2712 /* The linkEA entry with bad parent will be removed. */
2713 if (unlikely(!dt_try_as_dir(env, parent))) {
2714 lfsck_object_put(env, parent);
2715 linkea_del_buf(ldata, cname);
2720 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2721 (const struct dt_key *)cname->ln_name);
2722 *pfid2 = *lfsck_dto2fid(parent);
2723 if (rc == -ENOENT) {
2724 lfsck_object_put(env, parent);
2725 linkea_next_entry(ldata);
2730 lfsck_object_put(env, parent);
2735 if (lu_fid_eq(&tfid, cfid)) {
2736 lfsck_object_put(env, parent);
2737 if (!lu_fid_eq(pfid, pfid2)) {
2738 *type = LNIT_UNMATCHED_PAIRS;
2739 rc = lfsck_namespace_repair_unmatched_pairs(env,
2740 com, child, pfid2, cname);
2746 /* It is the most common case that we find the
2747 * name entry corresponding to the linkEA entry
2748 * that matches the ".." name entry. */
2749 rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2753 rc = linkea_add_buf(&ldata_new, cname, pfid2);
2757 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2762 linkea_del_buf(ldata, cname);
2764 linkea_first_entry(ldata);
2765 /* There may be some invalid dangling name entries under
2766 * other parent directories, remove all of them. */
2767 while (ldata->ld_lee != NULL) {
2768 lfsck_namespace_unpack_linkea_entry(ldata,
2769 cname, &tfid, info->lti_key);
2770 if (!fid_is_sane(&tfid))
2773 parent = lfsck_object_find_bottom(env, lfsck,
2775 if (IS_ERR(parent)) {
2776 rc = PTR_ERR(parent);
2777 if (rc != -ENOENT &&
2778 bk->lb_param & LPF_FAILOUT)
2784 if (!dt_object_exists(parent)) {
2785 lfsck_object_put(env, parent);
2789 rc = lfsck_namespace_repair_dirent(env, com,
2790 parent, child, cname->ln_name,
2791 cname->ln_name, S_IFDIR, false, true);
2792 lfsck_object_put(env, parent);
2794 if (bk->lb_param & LPF_FAILOUT)
2803 linkea_del_buf(ldata, cname);
2806 ns->ln_dirent_repaired += dirent_count;
2811 lfsck_ibits_unlock(lh, LCK_EX);
2812 /* The name entry references another MDT-object that may be
2813 * created by the LFSCK for repairing dangling name entry.
2814 * Try to replace it. */
2815 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2817 lfsck_object_put(env, parent);
2824 linkea_del_buf(ldata, cname);
2827 linkea_first_entry(ldata);
2828 if (ldata->ld_leh->leh_reccount == 1) {
2829 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2832 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2833 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2839 /* All linkEA entries are invalid and removed, then handle the @child
2841 if (ldata->ld_leh->leh_reccount == 0) {
2842 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2848 /* If the dangling name entry for the orphan directory object has
2849 * been remvoed, then just check whether the directory object is
2850 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2856 /* There is no linkEA entry that matches the ".." name entry. Find
2857 * the first linkEA entry that both parent and name entry exist to
2858 * rebuild a new ".." name entry. */
2868 * Repair the object's nlink attribute.
2870 * If all the known name entries have been verified, then the object's hard
2871 * link attribute should match the object's linkEA entries count unless the
2872 * object's has too much hard link to be recorded in the linkEA. Such cases
2873 * should have been marked in the LFSCK trace file. Otherwise, trust the
2874 * linkEA to update the object's nlink attribute.
2876 * \param[in] env pointer to the thread context
2877 * \param[in] com pointer to the lfsck component
2878 * \param[in] obj pointer to the dt_object to be handled
2879 * \param[in,out] la pointer to buffer to object's attribute before
2880 * and after the repairing
2882 * \retval positive number for repaired cases
2883 * \retval 0 if nothing to be repaired
2884 * \retval negative error number on failure
2886 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2887 struct lfsck_component *com,
2888 struct dt_object *obj,
2891 struct lfsck_thread_info *info = lfsck_env_info(env);
2892 struct lu_fid *tfid = &info->lti_fid3;
2893 struct lfsck_namespace *ns = com->lc_file_ram;
2894 struct lfsck_instance *lfsck = com->lc_lfsck;
2895 struct dt_device *dev = lfsck_obj2dev(obj);
2896 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2897 struct thandle *th = NULL;
2898 struct linkea_data ldata = { NULL };
2899 struct lustre_handle lh = { 0 };
2900 __u32 old = la->la_nlink;
2906 LASSERT(!dt_object_remote(obj));
2907 LASSERT(S_ISREG(lfsck_object_type(obj)));
2909 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2910 MDS_INODELOCK_UPDATE, LCK_PW);
2914 th = dt_trans_create(env, dev);
2916 GOTO(log, rc = PTR_ERR(th));
2918 la->la_valid = LA_NLINK;
2919 rc = dt_declare_attr_set(env, obj, la, th);
2923 rc = dt_trans_start_local(env, dev, th);
2927 dt_write_lock(env, obj, 0);
2928 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2929 * ever tried to verify some remote MDT-object that resides on this
2930 * MDT, but this MDT failed to respond such request. So means there
2931 * may be some remote name entry on other MDT that references this
2932 * object with another name, so we cannot know whether this linkEA
2933 * is valid or not. So keep it there and maybe resolved when next
2935 if (ns->ln_flags & LF_INCOMPLETE)
2936 GOTO(unlock, rc = 0);
2938 fid_cpu_to_be(tfid, cfid);
2939 idx = lfsck_sub_trace_file_fid2idx(cfid);
2940 rc = dt_lookup(env, com->lc_sub_trace_objs[idx].lsto_obj,
2941 (struct dt_rec *)&flags, (const struct dt_key *)tfid);
2945 if (flags & LNTF_SKIP_NLINK)
2946 GOTO(unlock, rc = 0);
2948 rc = dt_attr_get(env, obj, la);
2950 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2952 rc = lfsck_links_read2(env, obj, &ldata);
2954 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2956 if (la->la_nlink == ldata.ld_leh->leh_reccount ||
2957 unlikely(la->la_nlink == 0))
2958 GOTO(unlock, rc = 0);
2960 la->la_nlink = ldata.ld_leh->leh_reccount;
2961 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2962 GOTO(unlock, rc = 1);
2964 rc = dt_attr_set(env, obj, la, th);
2966 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2969 dt_write_unlock(env, obj);
2972 dt_trans_stop(env, dev, th);
2975 lfsck_ibits_unlock(&lh, LCK_PW);
2976 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2977 "nlink count from %u to %u: rc = %d\n",
2978 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2981 ns->ln_flags |= LF_INCONSISTENT;
2987 * Double scan the directory object for namespace LFSCK.
2989 * This function will verify the <parent, child> pairs in the namespace tree:
2990 * the parent references the child via some name entry that should be in the
2991 * child's linkEA entry, the child should back references the parent via its
2994 * The LFSCK will scan every linkEA entry in turn until find out the first
2995 * matched pairs. If found, then all other linkEA entries will be dropped.
2996 * If all the linkEA entries cannot match the ".." name entry, then there
2997 * are serveral possible cases:
2999 * 1) If there is only one linkEA entry, then trust it as long as the PFID
3000 * in the linkEA entry is valid.
3002 * 2) If there are multiple linkEA entries, then try to find the linkEA
3003 * that matches the ".." name entry. If found, then all other entries
3004 * are invalid; otherwise, it is quite possible that the ".." name entry
3005 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
3006 * entry according to the first valid linkEA entry (both the parent and
3007 * the name entry should exist).
3009 * 3) If the directory object has no (valid) linkEA entry, then the
3010 * directory object will be handled as pure orphan and inserted
3011 * in the .lustre/lost+found/MDTxxxx/ with the name:
3012 * ${self_FID}-${PFID}-D-${conflict_version}
3014 * \param[in] env pointer to the thread context
3015 * \param[in] com pointer to the lfsck component
3016 * \param[in] child pointer to the directory object to be handled
3017 * \param[in] flags to indicate the specical checking on the @child
3019 * \retval positive number for repaired cases
3020 * \retval 0 if nothing to be repaired
3021 * \retval negative error number on failure
3023 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3024 struct lfsck_component *com,
3025 struct dt_object *child, __u8 flags)
3027 struct lfsck_thread_info *info = lfsck_env_info(env);
3028 const struct lu_fid *cfid = lfsck_dto2fid(child);
3029 struct lu_fid *pfid = &info->lti_fid2;
3030 struct lfsck_namespace *ns = com->lc_file_ram;
3031 struct lfsck_instance *lfsck = com->lc_lfsck;
3032 struct lustre_handle lh = { 0 };
3033 struct linkea_data ldata = { NULL };
3034 bool unknown = false;
3037 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3041 LASSERT(!dt_object_remote(child));
3043 if (flags & LNTF_UNCERTAIN_LMV) {
3044 if (flags & LNTF_RECHECK_NAME_HASH) {
3045 rc = lfsck_namespace_scan_shard(env, com, child);
3049 ns->ln_striped_shards_scanned++;
3051 ns->ln_striped_shards_skipped++;
3055 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3059 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3060 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3061 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3062 "the namespace LFSCK, then the LFSCK cannot guarantee"
3063 "all the name entries have been verified in first-stage"
3064 "scanning. So have to skip orphan related handling for"
3065 "the directory object "DFID" with remote name entry\n",
3066 lfsck_lfsck2name(lfsck), PFID(cfid));
3071 if (unlikely(!dt_try_as_dir(env, child)))
3072 GOTO(out, rc = -ENOTDIR);
3074 /* We only take ldlm lock on the @child when required. When the
3075 * logic comes here for the first time, it is always false. */
3079 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3080 MDS_INODELOCK_UPDATE |
3081 MDS_INODELOCK_XATTR, LCK_EX);
3086 dt_read_lock(env, child, 0);
3087 if (unlikely(lfsck_is_dead_obj(child))) {
3088 dt_read_unlock(env, child);
3093 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3094 (const struct dt_key *)dotdot);
3096 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3097 dt_read_unlock(env, child);
3102 if (!lustre_handle_is_used(&lh)) {
3103 dt_read_unlock(env, child);
3108 } else if (lfsck->li_lpf_obj != NULL &&
3109 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3111 } else if (unlikely(!fid_is_sane(pfid))) {
3115 rc = lfsck_links_read(env, child, &ldata);
3116 dt_read_unlock(env, child);
3118 if (rc != -ENODATA && rc != -EINVAL)
3121 if (!lustre_handle_is_used(&lh))
3124 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3125 /* Remove the corrupted linkEA. */
3126 rc = lfsck_namespace_links_remove(env, com, child);
3128 /* Here, because of the crashed linkEA, we
3129 * cannot know whether there is some parent
3130 * that references the child directory via
3131 * some name entry or not. So keep it there,
3132 * when the LFSCK run next time, if there is
3133 * some parent that references this object,
3134 * then the LFSCK can rebuild the linkEA;
3135 * otherwise, this object will be handled
3136 * as orphan as above. */
3139 /* 1. If we have neither ".." nor linkEA,
3140 * then it is an orphan.
3142 * 2. If we only have the ".." name entry,
3143 * but no parent references this child
3144 * directory, then handle it as orphan. */
3145 lfsck_ibits_unlock(&lh, LCK_EX);
3146 type = LNIT_MUL_REF;
3148 /* If the LFSCK is marked as LF_INCOMPLETE,
3149 * then means some MDT has ever tried to
3150 * verify some remote MDT-object that resides
3151 * on this MDT, but this MDT failed to respond
3152 * such request. So means there may be some
3153 * remote name entry on other MDT that
3154 * references this object with another name,
3155 * so we cannot know whether this linkEA is
3156 * valid or not. So keep it there and maybe
3157 * resolved when next LFSCK run. */
3158 if (ns->ln_flags & LF_INCOMPLETE)
3161 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3162 "-"DFID, PFID(pfid));
3163 rc = lfsck_namespace_insert_orphan(env, com, child,
3164 info->lti_tmpbuf, "D", NULL);
3170 linkea_first_entry(&ldata);
3171 /* This is the most common case: the object has unique linkEA entry. */
3172 if (ldata.ld_leh->leh_reccount == 1) {
3173 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3174 &lh, &type, &retry);
3176 LASSERT(!lustre_handle_is_used(&lh));
3185 if (!lustre_handle_is_used(&lh))
3188 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3189 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3195 /* When we come here, the cases usually like that:
3196 * 1) The directory object has a corrupted linkEA entry. During the
3197 * first-stage scanning, the LFSCK cannot know such corruption,
3198 * then it appends the right linkEA entry according to the found
3199 * name entry after the bad one.
3201 * 2) The directory object has a right linkEA entry. During the
3202 * first-stage scanning, the LFSCK finds some bad name entry,
3203 * but the LFSCK cannot aware that at that time, then it adds
3204 * the bad linkEA entry for further processing. */
3205 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3211 lfsck_ibits_unlock(&lh, LCK_EX);
3214 case LNIT_BAD_LINKEA:
3215 ns->ln_linkea_repaired++;
3217 case LNIT_UNMATCHED_PAIRS:
3218 ns->ln_unmatched_pairs_repaired++;
3221 ns->ln_mul_ref_repaired++;
3229 ns->ln_unknown_inconsistency++;
3235 * Double scan the MDT-object for namespace LFSCK.
3237 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3238 * those entries from the linkEA; if the linkEA becomes empty or the object
3239 * has no linkEA, then it is an orphan and will be added into the directory
3240 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3241 * the remote parent; if the name entry corresponding to some linkEA entry
3242 * is lost, then add the name entry back to the namespace.
3244 * \param[in] env pointer to the thread context
3245 * \param[in] com pointer to the lfsck component
3246 * \param[in] child pointer to the dt_object to be handled
3247 * \param[in] flags some hints to indicate how the @child should be handled
3249 * \retval positive number for repaired cases
3250 * \retval 0 if nothing to be repaired
3251 * \retval negative error number on failure
3253 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3254 struct lfsck_component *com,
3255 struct dt_object *child, __u8 flags)
3257 struct lfsck_thread_info *info = lfsck_env_info(env);
3258 struct lu_attr *la = &info->lti_la;
3259 struct lu_name *cname = &info->lti_name;
3260 struct lu_fid *pfid = &info->lti_fid;
3261 struct lu_fid *cfid = &info->lti_fid2;
3262 struct lfsck_instance *lfsck = com->lc_lfsck;
3263 struct lfsck_namespace *ns = com->lc_file_ram;
3264 struct dt_object *parent = NULL;
3265 struct linkea_data ldata = { NULL };
3266 bool repaired = false;
3271 dt_read_lock(env, child, 0);
3272 if (unlikely(lfsck_is_dead_obj(child))) {
3273 dt_read_unlock(env, child);
3278 if (S_ISDIR(lfsck_object_type(child))) {
3279 dt_read_unlock(env, child);
3280 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3285 rc = lfsck_links_read(env, child, &ldata);
3286 dt_read_unlock(env, child);
3290 linkea_first_entry(&ldata);
3291 while (ldata.ld_lee != NULL) {
3292 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3294 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3296 /* Found repeated linkEA entries */
3298 rc = lfsck_namespace_shrink_linkea(env, com, child,
3299 &ldata, cname, pfid, false);
3311 /* Invalid PFID in the linkEA entry. */
3312 if (!fid_is_sane(pfid)) {
3313 rc = lfsck_namespace_shrink_linkea(env, com, child,
3314 &ldata, cname, pfid, true);
3324 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3326 GOTO(out, rc = PTR_ERR(parent));
3328 if (!dt_object_exists(parent)) {
3331 if (ldata.ld_leh->leh_reccount > 1) {
3332 /* If it is NOT the last linkEA entry, then
3333 * there is still other chance to make the
3334 * child to be visible via other parent, then
3335 * remove this linkEA entry. */
3336 rc = lfsck_namespace_shrink_linkea(env, com,
3337 child, &ldata, cname, pfid, true);
3339 /* If the LFSCK is marked as LF_INCOMPLETE,
3340 * then means some MDT has ever tried to
3341 * verify some remote MDT-object that resides
3342 * on this MDT, but this MDT failed to respond
3343 * such request. So means there may be some
3344 * remote name entry on other MDT that
3345 * references this object with another name,
3346 * so we cannot know whether this linkEA is
3347 * valid or not. So keep it there and maybe
3348 * resolved when next LFSCK run. */
3349 if (ns->ln_flags & LF_INCOMPLETE) {
3350 lfsck_object_put(env, parent);
3355 /* Create the lost parent as an orphan. */
3356 rc = lfsck_namespace_create_orphan_dir(env, com,
3359 lfsck_object_put(env, parent);
3367 /* Add the missing name entry to the parent. */
3368 rc = lfsck_namespace_insert_normal(env, com,
3369 parent, child, cname->ln_name);
3370 if (unlikely(rc == -EEXIST))
3371 /* Unfortunately, someone reused the
3372 * name under the parent by race. So we
3373 * have to remove the linkEA entry from
3374 * current child object. It means that
3375 * the LFSCK cannot recover the system
3376 * totally back to its original status,
3377 * but it is necessary to make the
3378 * current system to be consistent. */
3379 rc = lfsck_namespace_shrink_linkea(env,
3383 linkea_next_entry(&ldata);
3386 lfsck_object_put(env, parent);
3396 /* The linkEA entry with bad parent will be removed. */
3397 if (unlikely(!dt_try_as_dir(env, parent))) {
3398 lfsck_object_put(env, parent);
3399 rc = lfsck_namespace_shrink_linkea(env, com, child,
3400 &ldata, cname, pfid, true);
3410 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3411 (const struct dt_key *)cname->ln_name);
3412 if (rc != 0 && rc != -ENOENT) {
3413 lfsck_object_put(env, parent);
3419 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3420 /* It is the most common case that we
3421 * find the name entry corresponding
3422 * to the linkEA entry. */
3423 lfsck_object_put(env, parent);
3424 linkea_next_entry(&ldata);
3426 /* The name entry references another
3427 * MDT-object that may be created by
3428 * the LFSCK for repairing dangling
3429 * name entry. Try to replace it. */
3430 rc = lfsck_namespace_replace_cond(env, com,
3431 parent, child, cfid, cname);
3432 lfsck_object_put(env, parent);
3438 linkea_next_entry(&ldata);
3440 rc = lfsck_namespace_shrink_linkea(env,
3454 rc = dt_attr_get(env, child, la);
3458 /* If there is no name entry in the parent dir and the object
3459 * link count is less than the linkea entries count, then the
3460 * linkea entry should be removed. */
3461 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3462 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3463 parent, child, &ldata, cname, pfid);
3464 lfsck_object_put(env, parent);
3474 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3475 * MDT has ever tried to verify some remote MDT-object that
3476 * resides on this MDT, but this MDT failed to respond such
3477 * request. So means there may be some remote name entry on
3478 * other MDT that references this object with another name,
3479 * so we cannot know whether this linkEA is valid or not.
3480 * So keep it there and maybe resolved when next LFSCK run. */
3481 if (ns->ln_flags & LF_INCOMPLETE) {
3482 lfsck_object_put(env, parent);
3487 rc = lfsck_namespace_check_name(env, parent, child, cname);
3492 lfsck_object_put(env, parent);
3497 /* It is an invalid name entry, drop it. */
3498 if (unlikely(rc > 0)) {
3499 lfsck_object_put(env, parent);
3500 rc = lfsck_namespace_shrink_linkea(env, com, child,
3501 &ldata, cname, pfid, true);
3511 /* Add the missing name entry back to the namespace. */
3512 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3514 if (unlikely(rc == -ESTALE))
3515 /* It may happen when the remote object has been
3516 * removed, but the local MDT is not aware of that. */
3519 if (unlikely(rc == -EEXIST))
3520 /* Unfortunately, someone reused the name under the
3521 * parent by race. So we have to remove the linkEA
3522 * entry from current child object. It means that the
3523 * LFSCK cannot recover the system totally back to
3524 * its original status, but it is necessary to make
3525 * the current system to be consistent.
3527 * It also may be because of the LFSCK found some
3528 * internal status of create operation. Under such
3529 * case, nothing to be done. */
3530 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3531 parent, child, &ldata, cname, pfid);
3533 linkea_next_entry(&ldata);
3535 lfsck_object_put(env, parent);
3546 if (rc < 0 && rc != -ENODATA)
3550 LASSERT(ldata.ld_leh != NULL);
3552 count = ldata.ld_leh->leh_reccount;
3556 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3557 * MDT has ever tried to verify some remote MDT-object that
3558 * resides on this MDT, but this MDT failed to respond such
3559 * request. So means there may be some remote name entry on
3560 * other MDT that references this object with another name,
3561 * so we cannot know whether this linkEA is valid or not.
3562 * So keep it there and maybe resolved when next LFSCK run. */
3563 if (!(ns->ln_flags & LF_INCOMPLETE)) {
3564 /* If the child becomes orphan, then insert it into
3565 * the global .lustre/lost+found/MDTxxxx directory. */
3566 rc = lfsck_namespace_insert_orphan(env, com, child,
3572 ns->ln_mul_ref_repaired++;
3577 rc = dt_attr_get(env, child, la);
3581 if (la->la_nlink != 0 && la->la_nlink != count) {
3582 rc = lfsck_namespace_repair_nlink(env, com, child, la);
3584 ns->ln_objs_nlink_repaired++;
3591 if (la->la_nlink > 1)
3592 ns->ln_mul_linked_repaired++;
3601 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3602 struct lfsck_namespace *ns,
3603 __u64 checked_phase1,
3604 __u64 checked_phase2,
3608 seq_printf(m, "checked_phase1: "LPU64"\n"
3609 "checked_phase2: "LPU64"\n"
3610 "updated_phase1: "LPU64"\n"
3611 "updated_phase2: "LPU64"\n"
3612 "failed_phase1: "LPU64"\n"
3613 "failed_phase2: "LPU64"\n"
3614 "directories: "LPU64"\n"
3615 "dirent_repaired: "LPU64"\n"
3616 "linkea_repaired: "LPU64"\n"
3617 "nlinks_repaired: "LPU64"\n"
3618 "multiple_linked_checked: "LPU64"\n"
3619 "multiple_linked_repaired: "LPU64"\n"
3620 "unknown_inconsistency: "LPU64"\n"
3621 "unmatched_pairs_repaired: "LPU64"\n"
3622 "dangling_repaired: "LPU64"\n"
3623 "multiple_referenced_repaired: "LPU64"\n"
3624 "bad_file_type_repaired: "LPU64"\n"
3625 "lost_dirent_repaired: "LPU64"\n"
3626 "local_lost_found_scanned: "LPU64"\n"
3627 "local_lost_found_moved: "LPU64"\n"
3628 "local_lost_found_skipped: "LPU64"\n"
3629 "local_lost_found_failed: "LPU64"\n"
3630 "striped_dirs_scanned: "LPU64"\n"
3631 "striped_dirs_repaired: "LPU64"\n"
3632 "striped_dirs_failed: "LPU64"\n"
3633 "striped_dirs_disabled: "LPU64"\n"
3634 "striped_dirs_skipped: "LPU64"\n"
3635 "striped_shards_scanned: "LPU64"\n"
3636 "striped_shards_repaired: "LPU64"\n"
3637 "striped_shards_failed: "LPU64"\n"
3638 "striped_shards_skipped: "LPU64"\n"
3639 "name_hash_repaired: "LPU64"\n"
3640 "success_count: %u\n"
3641 "run_time_phase1: %u seconds\n"
3642 "run_time_phase2: %u seconds\n",
3645 ns->ln_items_repaired,
3646 ns->ln_objs_repaired_phase2,
3647 ns->ln_items_failed,
3648 ns->ln_objs_failed_phase2,
3649 ns->ln_dirs_checked,
3650 ns->ln_dirent_repaired,
3651 ns->ln_linkea_repaired,
3652 ns->ln_objs_nlink_repaired,
3653 ns->ln_mul_linked_checked,
3654 ns->ln_mul_linked_repaired,
3655 ns->ln_unknown_inconsistency,
3656 ns->ln_unmatched_pairs_repaired,
3657 ns->ln_dangling_repaired,
3658 ns->ln_mul_ref_repaired,
3659 ns->ln_bad_type_repaired,
3660 ns->ln_lost_dirent_repaired,
3661 ns->ln_local_lpf_scanned,
3662 ns->ln_local_lpf_moved,
3663 ns->ln_local_lpf_skipped,
3664 ns->ln_local_lpf_failed,
3665 ns->ln_striped_dirs_scanned,
3666 ns->ln_striped_dirs_repaired,
3667 ns->ln_striped_dirs_failed,
3668 ns->ln_striped_dirs_disabled,
3669 ns->ln_striped_dirs_skipped,
3670 ns->ln_striped_shards_scanned,
3671 ns->ln_striped_shards_repaired,
3672 ns->ln_striped_shards_failed,
3673 ns->ln_striped_shards_skipped,
3674 ns->ln_name_hash_repaired,
3675 ns->ln_success_count,
3680 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3681 struct lfsck_component *com)
3683 struct lfsck_instance *lfsck = com->lc_lfsck;
3684 struct lfsck_namespace *ns = com->lc_file_ram;
3686 while (!list_empty(&lfsck->li_list_lmv)) {
3687 struct lfsck_lmv_unit *llu;
3688 struct lfsck_lmv *llmv;
3690 llu = list_entry(lfsck->li_list_lmv.next,
3691 struct lfsck_lmv_unit, llu_link);
3692 llmv = &llu->llu_lmv;
3694 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3695 "still in using: %u\n",
3696 atomic_read(&llmv->ll_ref));
3698 ns->ln_striped_dirs_skipped++;
3699 lfsck_lmv_put(env, llmv);
3703 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3704 struct lfsck_component *com,
3705 struct dt_object *obj)
3707 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3710 rc = dt_attr_get(env, obj, la);
3714 /* zero-linkEA object may be orphan, but it also maybe because
3715 * of upgrading. Currently, we cannot record it for double scan.
3716 * Because it may cause the LFSCK trace file to be too large. */
3718 /* "la_ctime" == 1 means that it has ever been removed from
3719 * backend /lost+found directory but not been added back to
3720 * the normal namespace yet. */
3722 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3723 unlikely(la->la_ctime == 1))
3724 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3725 LNTF_CHECK_LINKEA, true);
3730 /* namespace APIs */
3732 static int lfsck_namespace_reset(const struct lu_env *env,
3733 struct lfsck_component *com, bool init)
3735 struct lfsck_instance *lfsck = com->lc_lfsck;
3736 struct lfsck_namespace *ns = com->lc_file_ram;
3737 struct lfsck_assistant_data *lad = com->lc_data;
3738 struct dt_object *root;
3739 struct dt_object *dto;
3743 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3745 GOTO(log, rc = PTR_ERR(root));
3747 if (unlikely(!dt_try_as_dir(env, root)))
3748 GOTO(put, rc = -ENOTDIR);
3750 down_write(&com->lc_sem);
3752 memset(ns, 0, sizeof(*ns));
3754 __u32 count = ns->ln_success_count;
3755 __u64 last_time = ns->ln_time_last_complete;
3757 memset(ns, 0, sizeof(*ns));
3758 ns->ln_success_count = count;
3759 ns->ln_time_last_complete = last_time;
3761 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3762 ns->ln_status = LS_INIT;
3764 lfsck_object_put(env, com->lc_obj);
3766 dto = lfsck_namespace_load_one_trace_file(env, com, root,
3767 LFSCK_NAMESPACE, true);
3769 GOTO(out, rc = PTR_ERR(dto));
3772 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3776 lad->lad_incomplete = 0;
3777 CFS_RESET_BITMAP(lad->lad_bitmap);
3779 rc = lfsck_namespace_store(env, com, true);
3784 up_write(&com->lc_sem);
3787 lfsck_object_put(env, root);
3789 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3790 lfsck_lfsck2name(lfsck), rc);
3795 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3798 struct lfsck_namespace *ns = com->lc_file_ram;
3800 down_write(&com->lc_sem);
3802 com->lc_new_checked++;
3803 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3804 up_write(&com->lc_sem);
3807 static void lfsck_namespace_close_dir(const struct lu_env *env,
3808 struct lfsck_component *com)
3810 struct lfsck_namespace *ns = com->lc_file_ram;
3811 struct lfsck_assistant_data *lad = com->lc_data;
3812 struct lfsck_instance *lfsck = com->lc_lfsck;
3813 struct lfsck_lmv *llmv = lfsck->li_lmv;
3814 struct lfsck_namespace_req *lnr;
3816 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3817 bool wakeup = false;
3823 OBD_ALLOC(lnr, size);
3825 ns->ln_striped_dirs_skipped++;
3830 /* Generate a dummy request to indicate that all shards' name entry
3831 * in this striped directory has been scanned for the first time. */
3832 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3833 lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3834 lnr->lnr_lmv = lfsck_lmv_get(llmv);
3835 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3836 lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3837 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3838 lnr->lnr_size = size;
3840 spin_lock(&lad->lad_lock);
3841 if (lad->lad_assistant_status < 0) {
3842 spin_unlock(&lad->lad_lock);
3843 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3844 ns->ln_striped_dirs_skipped++;
3849 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3850 if (lad->lad_prefetched == 0)
3853 lad->lad_prefetched++;
3854 spin_unlock(&lad->lad_lock);
3856 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3861 static int lfsck_namespace_open_dir(const struct lu_env *env,
3862 struct lfsck_component *com)
3864 struct lfsck_instance *lfsck = com->lc_lfsck;
3865 struct lfsck_namespace *ns = com->lc_file_ram;
3866 struct lfsck_lmv *llmv = lfsck->li_lmv;
3873 if (llmv->ll_lmv_master) {
3874 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3876 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
3877 lmv->lmv_master_mdt_index =
3878 lfsck_dev_idx(lfsck);
3879 ns->ln_flags |= LF_INCONSISTENT;
3880 llmv->ll_lmv_updated = 1;
3883 rc = lfsck_namespace_verify_stripe_slave(env, com,
3884 lfsck->li_obj_dir, llmv);
3887 RETURN(rc > 0 ? 0 : rc);
3890 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3891 struct lfsck_component *com, bool init)
3893 struct lfsck_instance *lfsck = com->lc_lfsck;
3894 struct lfsck_namespace *ns = com->lc_file_ram;
3898 rc = lfsck_checkpoint_generic(env, com);
3903 down_write(&com->lc_sem);
3905 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3907 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3908 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3909 HALF_SEC - lfsck->li_time_last_checkpoint);
3910 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3911 ns->ln_items_checked += com->lc_new_checked;
3912 com->lc_new_checked = 0;
3915 rc = lfsck_namespace_store(env, com, false);
3916 up_write(&com->lc_sem);
3919 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3920 ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3921 lfsck->li_pos_current.lp_oit_cookie,
3922 PFID(&lfsck->li_pos_current.lp_dir_parent),
3923 lfsck->li_pos_current.lp_dir_cookie, rc);
3925 return rc > 0 ? 0 : rc;
3928 static int lfsck_namespace_prep(const struct lu_env *env,
3929 struct lfsck_component *com,
3930 struct lfsck_start_param *lsp)
3932 struct lfsck_instance *lfsck = com->lc_lfsck;
3933 struct lfsck_namespace *ns = com->lc_file_ram;
3934 struct lfsck_position *pos = &com->lc_pos_start;
3937 rc = lfsck_namespace_load_bitmap(env, com);
3938 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3939 rc = lfsck_namespace_reset(env, com, false);
3941 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3944 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3945 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3951 down_write(&com->lc_sem);
3952 ns->ln_time_latest_start = cfs_time_current_sec();
3953 spin_lock(&lfsck->li_lock);
3955 if (ns->ln_flags & LF_SCANNED_ONCE) {
3956 if (!lfsck->li_drop_dryrun ||
3957 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3958 ns->ln_status = LS_SCANNING_PHASE2;
3959 list_move_tail(&com->lc_link,
3960 &lfsck->li_list_double_scan);
3961 if (!list_empty(&com->lc_link_dir))
3962 list_del_init(&com->lc_link_dir);
3963 lfsck_pos_set_zero(pos);
3965 ns->ln_status = LS_SCANNING_PHASE1;
3966 ns->ln_run_time_phase1 = 0;
3967 ns->ln_run_time_phase2 = 0;
3968 ns->ln_items_checked = 0;
3969 ns->ln_items_repaired = 0;
3970 ns->ln_items_failed = 0;
3971 ns->ln_dirs_checked = 0;
3972 ns->ln_objs_checked_phase2 = 0;
3973 ns->ln_objs_repaired_phase2 = 0;
3974 ns->ln_objs_failed_phase2 = 0;
3975 ns->ln_objs_nlink_repaired = 0;
3976 ns->ln_dirent_repaired = 0;
3977 ns->ln_linkea_repaired = 0;
3978 ns->ln_mul_linked_checked = 0;
3979 ns->ln_mul_linked_repaired = 0;
3980 ns->ln_unknown_inconsistency = 0;
3981 ns->ln_unmatched_pairs_repaired = 0;
3982 ns->ln_dangling_repaired = 0;
3983 ns->ln_mul_ref_repaired = 0;
3984 ns->ln_bad_type_repaired = 0;
3985 ns->ln_lost_dirent_repaired = 0;
3986 ns->ln_striped_dirs_scanned = 0;
3987 ns->ln_striped_dirs_repaired = 0;
3988 ns->ln_striped_dirs_failed = 0;
3989 ns->ln_striped_dirs_disabled = 0;
3990 ns->ln_striped_dirs_skipped = 0;
3991 ns->ln_striped_shards_scanned = 0;
3992 ns->ln_striped_shards_repaired = 0;
3993 ns->ln_striped_shards_failed = 0;
3994 ns->ln_striped_shards_skipped = 0;
3995 ns->ln_name_hash_repaired = 0;
3996 fid_zero(&ns->ln_fid_latest_scanned_phase2);
3997 if (list_empty(&com->lc_link_dir))
3998 list_add_tail(&com->lc_link_dir,
3999 &lfsck->li_list_dir);
4000 *pos = ns->ln_pos_first_inconsistent;
4003 ns->ln_status = LS_SCANNING_PHASE1;
4004 if (list_empty(&com->lc_link_dir))
4005 list_add_tail(&com->lc_link_dir,
4006 &lfsck->li_list_dir);
4007 if (!lfsck->li_drop_dryrun ||
4008 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4009 *pos = ns->ln_pos_last_checkpoint;
4010 pos->lp_oit_cookie++;
4012 *pos = ns->ln_pos_first_inconsistent;
4016 spin_unlock(&lfsck->li_lock);
4017 up_write(&com->lc_sem);
4019 rc = lfsck_start_assistant(env, com, lsp);
4021 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
4022 DFID", "LPX64"]: rc = %d\n",
4023 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4024 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4029 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4030 struct lfsck_component *com,
4031 struct dt_object *obj)
4033 struct lfsck_thread_info *info = lfsck_env_info(env);
4034 struct lfsck_namespace *ns = com->lc_file_ram;
4035 struct lfsck_instance *lfsck = com->lc_lfsck;
4036 const struct lu_fid *fid = lfsck_dto2fid(obj);
4037 struct lu_fid *pfid = &info->lti_fid2;
4038 struct lu_name *cname = &info->lti_name;
4039 struct lu_seq_range *range = &info->lti_range;
4040 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4041 struct linkea_data ldata = { NULL };
4042 __u32 idx = lfsck_dev_idx(lfsck);
4046 rc = lfsck_links_read(env, obj, &ldata);
4050 /* -EINVAL means crashed linkEA, should be verified. */
4051 if (rc == -EINVAL) {
4052 rc = lfsck_namespace_trace_update(env, com, fid,
4053 LNTF_CHECK_LINKEA, true);
4055 struct lustre_handle lh = { 0 };
4057 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4058 MDS_INODELOCK_UPDATE |
4059 MDS_INODELOCK_XATTR, LCK_EX);
4061 rc = lfsck_namespace_links_remove(env, com,
4063 lfsck_ibits_unlock(&lh, LCK_EX);
4067 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4070 if (rc == -ENODATA) {
4071 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4079 /* Record multiple-linked object. */
4080 if (ldata.ld_leh->leh_reccount > 1) {
4081 rc = lfsck_namespace_trace_update(env, com, fid,
4082 LNTF_CHECK_LINKEA, true);
4087 linkea_first_entry(&ldata);
4088 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4089 if (!fid_is_sane(pfid)) {
4090 rc = lfsck_namespace_trace_update(env, com, fid,
4091 LNTF_CHECK_PARENT, true);
4093 fld_range_set_mdt(range);
4094 rc = fld_local_lookup(env, ss->ss_server_fld,
4095 fid_seq(pfid), range);
4096 if ((rc == -ENOENT) ||
4097 (rc == 0 && range->lsr_index != idx))
4098 rc = lfsck_namespace_trace_update(env, com, fid,
4099 LNTF_CHECK_LINKEA, true);
4101 rc = lfsck_namespace_check_for_double_scan(env, com,
4108 down_write(&com->lc_sem);
4109 if (S_ISDIR(lfsck_object_type(obj)))
4110 ns->ln_dirs_checked++;
4112 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4113 up_write(&com->lc_sem);
4118 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4119 struct lfsck_component *com,
4120 struct lu_dirent *ent, __u16 type)
4122 struct lfsck_assistant_data *lad = com->lc_data;
4123 struct lfsck_instance *lfsck = com->lc_lfsck;
4124 struct lfsck_namespace_req *lnr;
4125 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4126 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4127 struct ptlrpc_thread *athread = &lad->lad_thread;
4128 struct l_wait_info lwi = { 0 };
4129 bool wakeup = false;
4131 l_wait_event(mthread->t_ctl_waitq,
4132 lad->lad_prefetched < bk->lb_async_windows ||
4133 !thread_is_running(mthread) ||
4134 thread_is_stopped(athread),
4137 if (unlikely(!thread_is_running(mthread)) ||
4138 thread_is_stopped(athread))
4141 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4144 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4146 struct lfsck_namespace *ns = com->lc_file_ram;
4148 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4149 return PTR_ERR(lnr);
4152 spin_lock(&lad->lad_lock);
4153 if (lad->lad_assistant_status < 0) {
4154 spin_unlock(&lad->lad_lock);
4155 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4156 return lad->lad_assistant_status;
4159 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4160 if (lad->lad_prefetched == 0)
4163 lad->lad_prefetched++;
4164 spin_unlock(&lad->lad_lock);
4166 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4168 down_write(&com->lc_sem);
4169 com->lc_new_checked++;
4170 up_write(&com->lc_sem);
4175 static int lfsck_namespace_post(const struct lu_env *env,
4176 struct lfsck_component *com,
4177 int result, bool init)
4179 struct lfsck_instance *lfsck = com->lc_lfsck;
4180 struct lfsck_namespace *ns = com->lc_file_ram;
4184 lfsck_post_generic(env, com, &result);
4186 down_write(&com->lc_sem);
4187 lfsck_namespace_release_lmv(env, com);
4189 spin_lock(&lfsck->li_lock);
4191 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4193 ns->ln_status = LS_SCANNING_PHASE2;
4194 ns->ln_flags |= LF_SCANNED_ONCE;
4195 ns->ln_flags &= ~LF_UPGRADE;
4196 list_del_init(&com->lc_link_dir);
4197 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4198 } else if (result == 0) {
4199 if (lfsck->li_status != 0)
4200 ns->ln_status = lfsck->li_status;
4202 ns->ln_status = LS_STOPPED;
4203 if (ns->ln_status != LS_PAUSED) {
4204 list_del_init(&com->lc_link_dir);
4205 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4208 ns->ln_status = LS_FAILED;
4209 list_del_init(&com->lc_link_dir);
4210 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4212 spin_unlock(&lfsck->li_lock);
4215 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4216 HALF_SEC - lfsck->li_time_last_checkpoint);
4217 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4218 ns->ln_items_checked += com->lc_new_checked;
4219 com->lc_new_checked = 0;
4222 rc = lfsck_namespace_store(env, com, false);
4223 up_write(&com->lc_sem);
4225 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4226 lfsck_lfsck2name(lfsck), rc);
4232 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4235 struct lfsck_instance *lfsck = com->lc_lfsck;
4236 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4237 struct lfsck_namespace *ns = com->lc_file_ram;
4240 down_read(&com->lc_sem);
4241 seq_printf(m, "name: lfsck_namespace\n"
4247 lfsck_status2names(ns->ln_status));
4249 rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4253 rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4257 rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4262 rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4267 rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4272 rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4273 "latest_start_position");
4277 rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4278 "last_checkpoint_position");
4282 rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4283 "first_failure_position");
4287 if (ns->ln_status == LS_SCANNING_PHASE1) {
4288 struct lfsck_position pos;
4289 const struct dt_it_ops *iops;
4290 cfs_duration_t duration = cfs_time_current() -
4291 lfsck->li_time_last_checkpoint;
4292 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4293 __u64 speed = checked;
4294 __u64 new_checked = com->lc_new_checked *
4295 msecs_to_jiffies(MSEC_PER_SEC);
4296 __u32 rtime = ns->ln_run_time_phase1 +
4297 cfs_duration_sec(duration + HALF_SEC);
4300 do_div(new_checked, duration);
4303 do_div(speed, rtime);
4305 lfsck_namespace_dump_statistics(m, ns, checked, 0, rtime, 0);
4306 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4307 "average_speed_phase2: N/A\n"
4308 "average_speed_total: "LPU64" items/sec\n"
4309 "real_time_speed_phase1: "LPU64" items/sec\n"
4310 "real_time_speed_phase2: N/A\n",
4315 LASSERT(lfsck->li_di_oit != NULL);
4317 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4319 /* The low layer otable-based iteration position may NOT
4320 * exactly match the namespace-based directory traversal
4321 * cookie. Generally, it is not a serious issue. But the
4322 * caller should NOT make assumption on that. */
4323 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4324 if (!lfsck->li_current_oit_processed)
4325 pos.lp_oit_cookie--;
4327 spin_lock(&lfsck->li_lock);
4328 if (lfsck->li_di_dir != NULL) {
4329 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4330 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4331 fid_zero(&pos.lp_dir_parent);
4332 pos.lp_dir_cookie = 0;
4335 *lfsck_dto2fid(lfsck->li_obj_dir);
4338 fid_zero(&pos.lp_dir_parent);
4339 pos.lp_dir_cookie = 0;
4341 spin_unlock(&lfsck->li_lock);
4342 lfsck_pos_dump(m, &pos, "current_position");
4343 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4344 cfs_duration_t duration = cfs_time_current() -
4345 lfsck->li_time_last_checkpoint;
4346 __u64 checked = ns->ln_objs_checked_phase2 +
4347 com->lc_new_checked;
4348 __u64 speed1 = ns->ln_items_checked;
4349 __u64 speed2 = checked;
4350 __u64 speed0 = speed1 + speed2;
4351 __u64 new_checked = com->lc_new_checked *
4352 msecs_to_jiffies(MSEC_PER_SEC);
4353 __u32 rtime = ns->ln_run_time_phase2 +
4354 cfs_duration_sec(duration + HALF_SEC);
4355 __u32 time0 = ns->ln_run_time_phase1 + rtime;
4358 do_div(new_checked, duration);
4360 if (ns->ln_run_time_phase1 != 0)
4361 do_div(speed1, ns->ln_run_time_phase1);
4362 else if (ns->ln_items_checked != 0)
4366 do_div(speed2, rtime);
4367 else if (checked != 0)
4371 do_div(speed0, time0);
4373 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4375 ns->ln_run_time_phase1, rtime);
4376 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4377 "average_speed_phase2: "LPU64" objs/sec\n"
4378 "average_speed_total: "LPU64" items/sec\n"
4379 "real_time_speed_phase1: N/A\n"
4380 "real_time_speed_phase2: "LPU64" objs/sec\n"
4381 "current_position: "DFID"\n",
4386 PFID(&ns->ln_fid_latest_scanned_phase2));
4388 __u64 speed1 = ns->ln_items_checked;
4389 __u64 speed2 = ns->ln_objs_checked_phase2;
4390 __u64 speed0 = speed1 + speed2;
4391 __u32 time0 = ns->ln_run_time_phase1 + ns->ln_run_time_phase2;
4393 if (ns->ln_run_time_phase1 != 0)
4394 do_div(speed1, ns->ln_run_time_phase1);
4395 else if (ns->ln_items_checked != 0)
4398 if (ns->ln_run_time_phase2 != 0)
4399 do_div(speed2, ns->ln_run_time_phase2);
4400 else if (ns->ln_objs_checked_phase2 != 0)
4404 do_div(speed0, time0);
4406 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4407 ns->ln_objs_checked_phase2,
4408 ns->ln_run_time_phase1,
4409 ns->ln_run_time_phase2);
4410 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4411 "average_speed_phase2: "LPU64" objs/sec\n"
4412 "average_speed_total: "LPU64" items/sec\n"
4413 "real_time_speed_phase1: N/A\n"
4414 "real_time_speed_phase2: N/A\n"
4415 "current_position: N/A\n",
4421 up_read(&com->lc_sem);
4425 static int lfsck_namespace_double_scan(const struct lu_env *env,
4426 struct lfsck_component *com)
4428 struct lfsck_namespace *ns = com->lc_file_ram;
4429 struct lfsck_assistant_data *lad = com->lc_data;
4430 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4431 struct lfsck_tgt_desc *ltd;
4432 struct lfsck_tgt_desc *next;
4435 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4436 if (thread_is_stopped(&lad->lad_thread)) {
4437 LASSERT(list_empty(&lad->lad_req_list));
4438 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4440 spin_lock(<ds->ltd_lock);
4441 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4442 ltd_namespace_phase_list) {
4443 list_del_init(<d->ltd_namespace_phase_list);
4445 spin_unlock(<ds->ltd_lock);
4451 static void lfsck_namespace_data_release(const struct lu_env *env,
4452 struct lfsck_component *com)
4454 struct lfsck_assistant_data *lad = com->lc_data;
4455 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4456 struct lfsck_tgt_desc *ltd;
4457 struct lfsck_tgt_desc *next;
4459 LASSERT(lad != NULL);
4460 LASSERT(thread_is_init(&lad->lad_thread) ||
4461 thread_is_stopped(&lad->lad_thread));
4462 LASSERT(list_empty(&lad->lad_req_list));
4464 com->lc_data = NULL;
4465 lfsck_namespace_release_lmv(env, com);
4467 spin_lock(<ds->ltd_lock);
4468 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4469 ltd_namespace_phase_list) {
4470 list_del_init(<d->ltd_namespace_phase_list);
4472 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4473 ltd_namespace_phase_list) {
4474 list_del_init(<d->ltd_namespace_phase_list);
4476 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4477 ltd_namespace_list) {
4478 list_del_init(<d->ltd_namespace_list);
4480 spin_unlock(<ds->ltd_lock);
4482 if (likely(lad->lad_bitmap != NULL))
4483 CFS_FREE_BITMAP(lad->lad_bitmap);
4488 static void lfsck_namespace_quit(const struct lu_env *env,
4489 struct lfsck_component *com)
4491 struct lfsck_assistant_data *lad = com->lc_data;
4492 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4493 struct lfsck_tgt_desc *ltd;
4494 struct lfsck_tgt_desc *next;
4496 LASSERT(lad != NULL);
4498 lfsck_quit_generic(env, com);
4500 LASSERT(thread_is_init(&lad->lad_thread) ||
4501 thread_is_stopped(&lad->lad_thread));
4502 LASSERT(list_empty(&lad->lad_req_list));
4504 lfsck_namespace_release_lmv(env, com);
4506 spin_lock(<ds->ltd_lock);
4507 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4508 ltd_namespace_phase_list) {
4509 list_del_init(<d->ltd_namespace_phase_list);
4511 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4512 ltd_namespace_phase_list) {
4513 list_del_init(<d->ltd_namespace_phase_list);
4515 spin_unlock(<ds->ltd_lock);
4518 static int lfsck_namespace_in_notify(const struct lu_env *env,
4519 struct lfsck_component *com,
4520 struct lfsck_request *lr,
4523 struct lfsck_instance *lfsck = com->lc_lfsck;
4524 struct lfsck_namespace *ns = com->lc_file_ram;
4525 struct lfsck_assistant_data *lad = com->lc_data;
4526 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4527 struct lfsck_tgt_desc *ltd;
4532 switch (lr->lr_event) {
4533 case LE_SKIP_NLINK_DECLARE: {
4534 struct dt_object *obj;
4535 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4539 LASSERT(th != NULL);
4541 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4542 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4543 fid_cpu_to_be(key, &lr->lr_fid);
4544 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4545 rc = dt_declare_delete(env, obj,
4546 (const struct dt_key *)key, th);
4548 rc = dt_declare_insert(env, obj,
4549 (const struct dt_rec *)&flags,
4550 (const struct dt_key *)key, th);
4551 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4555 case LE_SKIP_NLINK: {
4556 struct dt_object *obj;
4557 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
4563 LASSERT(th != NULL);
4565 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4566 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4567 fid_cpu_to_be(key, &lr->lr_fid);
4568 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4569 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4570 (const struct dt_key *)key);
4572 if (flags & LNTF_SKIP_NLINK) {
4574 &com->lc_sub_trace_objs[idx].lsto_mutex);
4580 } else if (rc != -ENOENT) {
4584 flags |= LNTF_SKIP_NLINK;
4586 rc = dt_delete(env, obj, (const struct dt_key *)key,
4592 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4593 (const struct dt_key *)key, th, 1);
4598 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4599 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4600 " to be skipped for namespace double scan: rc = %d\n",
4601 lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4604 /* If we cannot record this object in the LFSCK tracing,
4605 * we have to mark the LFSC as LF_INCOMPLETE, then the
4606 * LFSCK will skip nlink attribute verification for
4608 ns->ln_flags |= LF_INCOMPLETE;
4612 case LE_SET_LMV_MASTER: {
4613 struct dt_object *obj;
4615 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4617 RETURN(PTR_ERR(obj));
4619 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4620 lfsck_object_put(env, obj);
4622 RETURN(rc > 0 ? 0 : rc);
4624 case LE_SET_LMV_SLAVE: {
4625 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4626 ns->ln_striped_shards_repaired++;
4628 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4629 LNTF_RECHECK_NAME_HASH, true);
4631 RETURN(rc > 0 ? 0 : rc);
4633 case LE_PHASE1_DONE:
4634 case LE_PHASE2_DONE:
4641 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4642 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4643 lr->lr_index, lr->lr_status, lr->lr_flags2);
4645 spin_lock(<ds->ltd_lock);
4646 ltd = LTD_TGT(ltds, lr->lr_index);
4648 spin_unlock(<ds->ltd_lock);
4653 list_del_init(<d->ltd_namespace_phase_list);
4654 switch (lr->lr_event) {
4655 case LE_PHASE1_DONE:
4656 if (lr->lr_status <= 0) {
4657 ltd->ltd_namespace_done = 1;
4658 list_del_init(<d->ltd_namespace_list);
4659 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4660 "phase1 for namespace LFSCK: rc = %d.\n",
4661 lfsck_lfsck2name(lfsck),
4662 ltd->ltd_index, lr->lr_status);
4663 ns->ln_flags |= LF_INCOMPLETE;
4668 if (lr->lr_flags2 & LF_INCOMPLETE)
4669 ns->ln_flags |= LF_INCOMPLETE;
4671 if (list_empty(<d->ltd_namespace_list))
4672 list_add_tail(<d->ltd_namespace_list,
4673 &lad->lad_mdt_list);
4674 list_add_tail(<d->ltd_namespace_phase_list,
4675 &lad->lad_mdt_phase2_list);
4677 case LE_PHASE2_DONE:
4678 ltd->ltd_namespace_done = 1;
4679 list_del_init(<d->ltd_namespace_list);
4683 ltd->ltd_namespace_done = 1;
4684 list_del_init(<d->ltd_namespace_list);
4685 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4687 "%s: the peer MDT %x exit namespace LFSCK\n",
4688 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4689 ns->ln_flags |= LF_INCOMPLETE;
4695 spin_unlock(<ds->ltd_lock);
4697 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4698 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4700 memset(stop, 0, sizeof(*stop));
4701 stop->ls_status = lr->lr_status;
4702 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4703 lfsck_stop(env, lfsck->li_bottom, stop);
4704 } else if (lfsck_phase2_next_ready(lad)) {
4705 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4711 static int lfsck_namespace_query(const struct lu_env *env,
4712 struct lfsck_component *com)
4714 struct lfsck_namespace *ns = com->lc_file_ram;
4716 return ns->ln_status;
4719 static struct lfsck_operations lfsck_namespace_ops = {
4720 .lfsck_reset = lfsck_namespace_reset,
4721 .lfsck_fail = lfsck_namespace_fail,
4722 .lfsck_close_dir = lfsck_namespace_close_dir,
4723 .lfsck_open_dir = lfsck_namespace_open_dir,
4724 .lfsck_checkpoint = lfsck_namespace_checkpoint,
4725 .lfsck_prep = lfsck_namespace_prep,
4726 .lfsck_exec_oit = lfsck_namespace_exec_oit,
4727 .lfsck_exec_dir = lfsck_namespace_exec_dir,
4728 .lfsck_post = lfsck_namespace_post,
4729 .lfsck_dump = lfsck_namespace_dump,
4730 .lfsck_double_scan = lfsck_namespace_double_scan,
4731 .lfsck_data_release = lfsck_namespace_data_release,
4732 .lfsck_quit = lfsck_namespace_quit,
4733 .lfsck_in_notify = lfsck_namespace_in_notify,
4734 .lfsck_query = lfsck_namespace_query,
4738 * Repair dangling name entry.
4740 * For the name entry with dangling reference, we need to repare the
4741 * inconsistency according to the LFSCK sponsor's requirement:
4743 * 1) Keep the inconsistency there and report the inconsistency case,
4744 * then give the chance to the application to find related issues,
4745 * and the users can make the decision about how to handle it with
4746 * more human knownledge. (by default)
4748 * 2) Re-create the missing MDT-object with the FID information.
4750 * \param[in] env pointer to the thread context
4751 * \param[in] com pointer to the lfsck component
4752 * \param[in] parent pointer to the dir object that contains the dangling
4754 * \param[in] child pointer to the object corresponding to the dangling
4756 * \param[in] lnr pointer to the namespace request that contains the
4757 * name's name, parent object, parent's LMV, and ect.
4759 * \retval positive number if no need to repair
4760 * \retval zero for repaired successfully
4761 * \retval negative error number on failure
4763 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4764 struct lfsck_component *com,
4765 struct dt_object *parent,
4766 struct dt_object *child,
4767 struct lfsck_namespace_req *lnr)
4769 struct lfsck_thread_info *info = lfsck_env_info(env);
4770 struct lu_attr *la = &info->lti_la;
4771 struct dt_allocation_hint *hint = &info->lti_hint;
4772 struct dt_object_format *dof = &info->lti_dof;
4773 struct dt_insert_rec *rec = &info->lti_dt_rec;
4774 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
4775 struct dt_object *pobj = NULL;
4776 struct dt_object *cobj = NULL;
4777 const struct lu_name *cname;
4778 struct linkea_data ldata = { NULL };
4779 struct lfsck_lock_handle *llh = &info->lti_llh;
4780 struct lu_buf linkea_buf;
4781 struct lu_buf lmv_buf;
4782 struct lfsck_instance *lfsck = com->lc_lfsck;
4783 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4784 struct dt_device *dev = lfsck->li_next;
4785 struct thandle *th = NULL;
4787 __u16 type = lnr->lnr_type;
4791 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4792 if (bk->lb_param & LPF_CREATE_MDTOBJ)
4797 if (!create || bk->lb_param & LPF_DRYRUN)
4800 /* We may need to create the sub-objects of the @child via LOD,
4801 * so make the modification based on lfsck->li_next. */
4803 pobj = lfsck_object_locate(dev, parent);
4805 GOTO(log, rc = PTR_ERR(pobj));
4807 if (unlikely(!dt_try_as_dir(env, pobj)))
4808 GOTO(log, rc = -ENOTDIR);
4810 cobj = lfsck_object_locate(dev, child);
4812 GOTO(log, rc = PTR_ERR(cobj));
4814 rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4818 rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(pobj));
4822 rc = lfsck_lock(env, lfsck, parent, lnr->lnr_name, llh,
4823 MDS_INODELOCK_UPDATE, LCK_PR);
4827 rc = lfsck_namespace_check_exist(env, pobj, cobj, lnr->lnr_name);
4831 /* Set the ctime as zero, then others can know it is created for
4832 * repairing dangling name entry by LFSCK. And if the LFSCK made
4833 * wrong decision and the real MDT-object has been found later,
4834 * then the LFSCK has chance to fix the incosistency properly. */
4835 memset(la, 0, sizeof(*la));
4836 la->la_mode = (type & S_IFMT) | 0600;
4837 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4838 LA_ATIME | LA_MTIME | LA_CTIME;
4840 cobj->do_ops->do_ah_init(env, hint, pobj, cobj,
4841 la->la_mode & S_IFMT);
4843 memset(dof, 0, sizeof(*dof));
4844 dof->dof_type = dt_mode_to_dft(type);
4845 /* If the target is a regular file, then the LFSCK will only create
4846 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4847 * OST-objects will be created when write open. */
4849 th = dt_trans_create(env, dev);
4851 GOTO(log, rc = PTR_ERR(th));
4853 /* 1a. create child. */
4854 rc = dt_declare_create(env, cobj, la, hint, dof, th);
4858 if (S_ISDIR(type)) {
4859 if (unlikely(!dt_try_as_dir(env, cobj)))
4860 GOTO(stop, rc = -ENOTDIR);
4862 /* 2a. insert dot into child dir */
4863 rec->rec_type = S_IFDIR;
4864 rec->rec_fid = lfsck_dto2fid(cobj);
4865 rc = dt_declare_insert(env, cobj,
4866 (const struct dt_rec *)rec,
4867 (const struct dt_key *)dot, th);
4871 /* 3a. insert dotdot into child dir */
4872 rec->rec_fid = lfsck_dto2fid(pobj);
4873 rc = dt_declare_insert(env, cobj,
4874 (const struct dt_rec *)rec,
4875 (const struct dt_key *)dotdot, th);
4879 /* 4a. increase child nlink */
4880 rc = dt_declare_ref_add(env, cobj, th);
4884 /* 5a. generate slave LMV EA. */
4885 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4888 idx = lfsck_shard_name_to_index(env,
4889 lnr->lnr_name, lnr->lnr_namelen,
4890 type, lfsck_dto2fid(cobj));
4891 if (unlikely(idx < 0))
4892 GOTO(stop, rc = idx);
4894 *lmv2 = lnr->lnr_lmv->ll_lmv;
4895 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4896 lmv2->lmv_master_mdt_index = idx;
4898 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4899 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4900 rc = dt_declare_xattr_set(env, cobj, &lmv_buf,
4901 XATTR_NAME_LMV, 0, th);
4907 /* 6a. insert linkEA for child */
4908 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4909 ldata.ld_leh->leh_len);
4910 rc = dt_declare_xattr_set(env, cobj, &linkea_buf,
4911 XATTR_NAME_LINK, 0, th);
4915 rc = dt_trans_start_local(env, dev, th);
4917 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4919 dt_write_lock(env, cobj, 0);
4920 /* 1b. create child */
4921 rc = dt_create(env, cobj, la, hint, dof, th);
4923 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4925 if (S_ISDIR(type)) {
4926 /* 2b. insert dot into child dir */
4927 rec->rec_type = S_IFDIR;
4928 rec->rec_fid = lfsck_dto2fid(cobj);
4929 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4930 (const struct dt_key *)dot, th, 1);
4934 /* 3b. insert dotdot into child dir */
4935 rec->rec_fid = lfsck_dto2fid(pobj);
4936 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4937 (const struct dt_key *)dotdot, th, 1);
4941 /* 4b. increase child nlink */
4942 rc = dt_ref_add(env, cobj, th);
4946 /* 5b. generate slave LMV EA. */
4947 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4948 rc = dt_xattr_set(env, cobj, &lmv_buf, XATTR_NAME_LMV,
4955 /* 6b. insert linkEA for child. */
4956 rc = dt_xattr_set(env, cobj, &linkea_buf,
4957 XATTR_NAME_LINK, 0, th);
4962 dt_write_unlock(env, cobj);
4965 dt_trans_stop(env, dev, th);
4969 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4970 "reference for: parent "DFID", child "DFID", type %u, "
4971 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4972 PFID(&lnr->lnr_lar.lar_fid), PFID(lfsck_dto2fid(child)),
4973 type, cname->ln_name,
4974 create ? "Create the lost OST-object as required" :
4975 "Keep the MDT-object there by default", rc);
4978 struct lfsck_namespace *ns = com->lc_file_ram;
4980 ns->ln_flags |= LF_INCONSISTENT;
4986 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4987 struct lfsck_component *com,
4988 struct lfsck_assistant_req *lar)
4990 struct lfsck_thread_info *info = lfsck_env_info(env);
4991 struct lu_attr *la = &info->lti_la;
4992 struct lfsck_instance *lfsck = com->lc_lfsck;
4993 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4994 struct lfsck_namespace *ns = com->lc_file_ram;
4995 struct linkea_data ldata = { NULL };
4996 const struct lu_name *cname;
4997 struct thandle *handle = NULL;
4998 struct lfsck_namespace_req *lnr =
4999 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5000 struct dt_object *dir = NULL;
5001 struct dt_object *obj = NULL;
5002 const struct lu_fid *pfid;
5003 struct dt_device *dev = NULL;
5004 struct lustre_handle lh = { 0 };
5005 bool repaired = false;
5006 bool dtlocked = false;
5010 bool bad_hash = false;
5014 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5017 dir = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
5019 RETURN(PTR_ERR(dir));
5021 if (unlikely(lfsck_is_dead_obj(dir)))
5022 GOTO(put_dir, rc = 0);
5024 if (unlikely(!dt_try_as_dir(env, dir)))
5025 GOTO(put_dir, rc = -ENOTDIR);
5027 pfid = lfsck_dto2fid(dir);
5029 if (lnr->lnr_attr & LUDA_UPGRADE) {
5030 ns->ln_flags |= LF_UPGRADE;
5031 ns->ln_dirent_repaired++;
5033 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5034 ns->ln_flags |= LF_INCONSISTENT;
5035 ns->ln_dirent_repaired++;
5039 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5040 if (strcmp(lnr->lnr_name, dotdot) != 0)
5043 rc = lfsck_namespace_trace_update(env, com, pfid,
5044 LNTF_CHECK_PARENT, true);
5049 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5050 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5051 " for the name entry %.*s under "DFID"\n",
5052 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5053 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5055 if (strcmp(lnr->lnr_name, dotdot) != 0)
5056 /* invalid FID means bad name entry, remove it. */
5057 type = LNIT_BAD_DIRENT;
5059 /* If the parent FID is invalid, we cannot remove
5060 * the ".." entry directly. */
5061 rc = lfsck_namespace_trace_update(env, com, pfid,
5062 LNTF_CHECK_PARENT, true);
5067 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5068 rc = lfsck_namespace_striped_dir_rescan(env, com, dir, lnr);
5073 if (fid_seq_is_dot(fid_seq(&lnr->lnr_fid)))
5076 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5077 rc = lfsck_namespace_handle_striped_master(env, com, dir, lnr);
5082 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5084 GOTO(out, rc = idx);
5086 if (idx == lfsck_dev_idx(lfsck)) {
5087 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5090 dev = lfsck->li_bottom;
5092 struct lfsck_tgt_desc *ltd;
5094 /* Usually, some local filesystem consistency verification
5095 * tools can guarantee the local namespace tree consistenct.
5096 * So the LFSCK will only verify the remote directory. */
5097 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5098 rc = lfsck_namespace_trace_update(env, com, pfid,
5099 LNTF_CHECK_PARENT, true);
5104 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5105 if (unlikely(ltd == NULL)) {
5106 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5107 "did not join the namespace LFSCK\n",
5108 lfsck_lfsck2name(lfsck), idx);
5109 lfsck_lad_set_bitmap(env, com, idx);
5111 GOTO(out, rc = -ENODEV);
5117 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5119 GOTO(out, rc = PTR_ERR(obj));
5121 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5122 if (dt_object_exists(obj) == 0) {
5125 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5127 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5128 lnr->lnr_name, lnr->lnr_namelen)) {
5129 type = LNIT_BAD_DIRENT;
5134 type = LNIT_DANGLING;
5135 rc = lfsck_namespace_repair_dangling(env, com, dir,
5144 if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5147 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5148 MDS_INODELOCK_UPDATE |
5149 MDS_INODELOCK_XATTR, LCK_EX);
5153 handle = dt_trans_create(env, dev);
5155 GOTO(out, rc = PTR_ERR(handle));
5157 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5161 rc = dt_trans_start_local(env, dev, handle);
5165 dt_write_lock(env, obj, 0);
5169 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5173 rc = lfsck_links_read(env, obj, &ldata);
5174 if (unlikely(rc == -ENOENT)) {
5175 if (handle != NULL) {
5176 dt_write_unlock(env, obj);
5179 dt_trans_stop(env, dev, handle);
5182 lfsck_ibits_unlock(&lh, LCK_EX);
5185 /* It may happen when the remote object has been removed,
5186 * but the local MDT is not aware of that. */
5188 } else if (rc == 0) {
5189 count = ldata.ld_leh->leh_reccount;
5190 rc = linkea_links_find(&ldata, cname, pfid);
5192 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5193 if ((lfsck_object_type(obj) & S_IFMT) !=
5195 ns->ln_flags |= LF_INCONSISTENT;
5196 type = LNIT_BAD_TYPE;
5202 ns->ln_flags |= LF_INCONSISTENT;
5204 /* If the name entry hash does not match the slave striped
5205 * directory, and the name entry does not match also, then
5206 * it is quite possible that name entry is corrupted. */
5207 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5208 lnr->lnr_name, lnr->lnr_namelen)) {
5209 type = LNIT_BAD_DIRENT;
5214 /* If the file type stored in the name entry does not match
5215 * the file type claimed by the object, and the object does
5216 * not recognize the name entry, then it is quite possible
5217 * that the name entry is corrupted. */
5218 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5219 type = LNIT_BAD_DIRENT;
5224 /* For sub-dir object, we cannot make sure whether the sub-dir
5225 * back references the parent via ".." name entry correctly or
5226 * not in the LFSCK first-stage scanning. It may be that the
5227 * (remote) sub-dir ".." name entry has no parent FID after
5228 * file-level backup/restore and its linkEA may be wrong.
5229 * So under such case, we should replace the linkEA according
5230 * to current name entry. But this needs to be done during the
5231 * LFSCK second-stage scanning. The LFSCK will record the name
5232 * entry for further possible using. */
5236 } else if (unlikely(rc == -EINVAL)) {
5237 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5238 type = LNIT_BAD_TYPE;
5241 ns->ln_flags |= LF_INCONSISTENT;
5242 /* The magic crashed, we are not sure whether there are more
5243 * corrupt data in the linkea, so remove all linkea entries. */
5247 } else if (rc == -ENODATA) {
5248 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5249 type = LNIT_BAD_TYPE;
5252 ns->ln_flags |= LF_UPGRADE;
5257 if (bk->lb_param & LPF_DRYRUN) {
5258 ns->ln_linkea_repaired++;
5264 if (!lustre_handle_is_used(&lh))
5270 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle);
5276 rc = linkea_data_new(&ldata,
5277 &lfsck_env_info(env)->lti_linkea_buf);
5282 rc = linkea_add_buf(&ldata, cname, pfid);
5286 rc = lfsck_links_write(env, obj, &ldata, handle);
5287 if (unlikely(rc == -ENOSPC) &&
5288 S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5289 if (handle != NULL) {
5290 LASSERT(dt_write_locked(env, obj));
5292 dt_write_unlock(env, obj);
5295 dt_trans_stop(env, dev, handle);
5298 lfsck_ibits_unlock(&lh, LCK_EX);
5301 rc = lfsck_namespace_trace_update(env, com,
5302 &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5304 /* If we cannot record this object in the
5305 * LFSCK tracing, we have to mark the LFSCK
5306 * as LF_INCOMPLETE, then the LFSCK will
5307 * skip nlink attribute verification for
5309 ns->ln_flags |= LF_INCOMPLETE;
5317 count = ldata.ld_leh->leh_reccount;
5318 if (!S_ISDIR(lfsck_object_type(obj)) ||
5319 !dt_object_remote(obj)) {
5320 ns->ln_linkea_repaired++;
5330 dt_write_unlock(env, obj);
5332 if (handle != NULL && !IS_ERR(handle))
5333 dt_trans_stop(env, dev, handle);
5336 lfsck_ibits_unlock(&lh, LCK_EX);
5338 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5339 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5340 lnr->lnr_name, lnr->lnr_namelen) &&
5341 type != LNIT_BAD_DIRENT) {
5342 ns->ln_flags |= LF_INCONSISTENT;
5345 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5346 lnr->lnr_lmv, lnr->lnr_name);
5355 rc = lfsck_namespace_repair_dirent(env, com, dir,
5356 obj, lnr->lnr_name, lnr->lnr_name,
5357 lnr->lnr_type, true, false);
5361 case LNIT_BAD_DIRENT:
5363 /* XXX: This is a bad dirent, we do not know whether
5364 * the original name entry reference a regular
5365 * file or a directory, then keep the parent's
5366 * nlink count unchanged here. */
5367 rc = lfsck_namespace_repair_dirent(env, com, dir,
5368 obj, lnr->lnr_name, lnr->lnr_name,
5369 lnr->lnr_type, false, false);
5377 if (count == 1 && S_ISREG(lfsck_object_type(obj)))
5378 dt_attr_get(env, obj, la);
5381 down_write(&com->lc_sem);
5383 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5384 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5385 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5386 PFID(lfsck_dto2fid(dir)),
5387 lnr->lnr_namelen, lnr->lnr_name, rc);
5389 lfsck_namespace_record_failure(env, lfsck, ns);
5390 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5391 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5392 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5393 dev != NULL && dev != lfsck->li_bottom)
5394 lfsck_lad_set_bitmap(env, com, idx);
5396 if (!(bk->lb_param & LPF_FAILOUT))
5400 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5401 "repaired the entry: "DFID", parent "DFID
5402 ", name %.*s\n", lfsck_lfsck2name(lfsck),
5403 PFID(&lnr->lnr_fid),
5404 PFID(lfsck_dto2fid(dir)),
5405 lnr->lnr_namelen, lnr->lnr_name);
5408 ns->ln_items_repaired++;
5412 ns->ln_dangling_repaired++;
5415 ns->ln_bad_type_repaired++;
5417 case LNIT_BAD_DIRENT:
5418 ns->ln_dirent_repaired++;
5424 if (bk->lb_param & LPF_DRYRUN &&
5425 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5426 lfsck_pos_fill(env, lfsck,
5427 &ns->ln_pos_first_inconsistent,
5432 ns->ln_name_hash_repaired++;
5434 /* Not count repeatedly. */
5436 ns->ln_items_repaired++;
5438 if (bk->lb_param & LPF_DRYRUN &&
5439 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5440 lfsck_pos_fill(env, lfsck,
5441 &ns->ln_pos_first_inconsistent,
5448 if (count > 1 || la->la_nlink > 1)
5449 ns->ln_mul_linked_checked++;
5451 up_write(&com->lc_sem);
5453 if (obj != NULL && !IS_ERR(obj))
5454 lfsck_object_put(env, obj);
5457 lfsck_object_put(env, dir);
5463 * Handle one orphan under the backend /lost+found directory
5465 * Insert the orphan FID into the namespace LFSCK trace file for further
5466 * processing (via the subsequent namespace LFSCK second-stage scanning).
5467 * At the same time, remove the orphan name entry from backend /lost+found
5468 * directory. There is an interval between the orphan name entry removed
5469 * from the backend /lost+found directory and the orphan FID in the LFSCK
5470 * trace file handled. In such interval, the LFSCK can be reset, then
5471 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5472 * To guarantee that the orphans can be found when LFSCK run next time
5473 * without e2fsck again, when remove the orphan name entry, the LFSCK
5474 * will set the orphan's ctime attribute as 1. Since normal applications
5475 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5476 * next time, it can record the object (that ctime is 1) in the namespace
5477 * LFSCK trace file during the first-stage scanning.
5479 * \param[in] env pointer to the thread context
5480 * \param[in] com pointer to the lfsck component
5481 * \param[in] parent pointer to the object for the backend /lost+found
5482 * \param[in] ent pointer to the name entry for the target under the
5483 * backend /lost+found
5485 * \retval positive for repaired
5486 * \retval 0 if needs to repair nothing
5487 * \retval negative error number on failure
5489 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5490 struct lfsck_component *com,
5491 struct dt_object *parent,
5492 struct lu_dirent *ent)
5494 struct lfsck_thread_info *info = lfsck_env_info(env);
5495 struct lu_fid *key = &info->lti_fid;
5496 struct lu_attr *la = &info->lti_la;
5497 struct lfsck_instance *lfsck = com->lc_lfsck;
5498 struct dt_object *obj;
5499 struct dt_device *dev = lfsck->li_bottom;
5500 struct dt_object *child = NULL;
5501 struct thandle *th = NULL;
5508 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5510 RETURN(PTR_ERR(child));
5512 LASSERT(dt_object_exists(child));
5513 LASSERT(!dt_object_remote(child));
5515 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5516 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5517 fid_cpu_to_be(key, &ent->lde_fid);
5518 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5519 (const struct dt_key *)key);
5522 flags |= LNTF_CHECK_ORPHAN;
5523 } else if (rc == -ENOENT) {
5524 flags = LNTF_CHECK_ORPHAN;
5529 th = dt_trans_create(env, dev);
5531 GOTO(out, rc = PTR_ERR(th));
5533 /* a1. remove name entry from backend /lost+found */
5534 rc = dt_declare_delete(env, parent,
5535 (const struct dt_key *)ent->lde_name, th);
5539 if (S_ISDIR(lfsck_object_type(child))) {
5540 /* a2. decrease parent's nlink */
5541 rc = dt_declare_ref_del(env, parent, th);
5547 /* a3. remove child's FID from the LFSCK trace file. */
5548 rc = dt_declare_delete(env, obj,
5549 (const struct dt_key *)key, th);
5553 /* a4. set child's ctime as 1 */
5554 memset(la, 0, sizeof(*la));
5556 la->la_valid = LA_CTIME;
5557 rc = dt_declare_attr_set(env, child, la, th);
5562 /* a5. insert child's FID into the LFSCK trace file. */
5563 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5564 (const struct dt_key *)key, th);
5568 rc = dt_trans_start_local(env, dev, th);
5572 /* b1. remove name entry from backend /lost+found */
5573 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th);
5577 if (S_ISDIR(lfsck_object_type(child))) {
5578 /* b2. decrease parent's nlink */
5579 dt_write_lock(env, parent, 0);
5580 rc = dt_ref_del(env, parent, th);
5581 dt_write_unlock(env, parent);
5587 /* a3. remove child's FID from the LFSCK trace file. */
5588 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
5592 /* b4. set child's ctime as 1 */
5593 rc = dt_attr_set(env, child, la, th);
5598 /* b5. insert child's FID into the LFSCK trace file. */
5599 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5600 (const struct dt_key *)key, th, 1);
5602 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5605 dt_trans_stop(env, dev, th);
5608 lfsck_object_put(env, child);
5614 * Handle orphans under the backend /lost+found directory
5616 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5617 * and put them under the backend /lost+found directory that is invisible
5618 * to client. The LFSCK will scan such directory, for the original client
5619 * visible orphans, add their fids into the namespace LFSCK trace file,
5620 * then the subsenquent namespace LFSCK second-stage scanning can handle
5621 * them as other objects to be double scanned: either move back to normal
5622 * namespace, or to the global visible orphan directory:
5623 * /ROOT/.lustre/lost+found/MDTxxxx/
5625 * \param[in] env pointer to the thread context
5626 * \param[in] com pointer to the lfsck component
5628 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5629 struct lfsck_component *com)
5631 struct lfsck_thread_info *info = lfsck_env_info(env);
5632 struct lu_dirent *ent =
5633 (struct lu_dirent *)info->lti_key;
5634 struct lu_seq_range *range = &info->lti_range;
5635 struct lfsck_instance *lfsck = com->lc_lfsck;
5636 struct ptlrpc_thread *thread = &lfsck->li_thread;
5637 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5638 struct lfsck_namespace *ns = com->lc_file_ram;
5639 struct dt_object *parent;
5640 const struct dt_it_ops *iops;
5642 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5644 __u32 idx = lfsck_dev_idx(lfsck);
5649 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5650 &LU_BACKEND_LPF_FID);
5651 if (IS_ERR(parent)) {
5652 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5653 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5657 /* It is normal that the /lost+found does not exist for ZFS backend. */
5658 if (!dt_object_exists(parent))
5661 if (unlikely(!dt_try_as_dir(env, parent)))
5662 GOTO(out, rc = -ENOTDIR);
5664 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5665 lfsck_lfsck2name(lfsck));
5667 com->lc_new_scanned = 0;
5668 iops = &parent->do_index_ops->dio_it;
5669 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE);
5671 GOTO(out, rc = PTR_ERR(di));
5673 rc = iops->load(env, di, 0);
5675 rc = iops->next(env, di);
5680 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5681 unlikely(!thread_is_running(thread)))
5684 rc = iops->rec(env, di, (struct dt_rec *)ent,
5685 LUDA_64BITHASH | LUDA_TYPE);
5687 rc = lfsck_unpack_ent(ent, &cookie, &type);
5689 if (unlikely(rc != 0)) {
5690 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5691 "/lost+found: rc = %d\n",
5692 lfsck_lfsck2name(lfsck), rc);
5697 /* skip dot and dotdot entries */
5698 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5701 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5704 if (fid_is_norm(&ent->lde_fid)) {
5705 fld_range_set_mdt(range);
5706 rc = fld_local_lookup(env, ss->ss_server_fld,
5707 fid_seq(&ent->lde_fid), range);
5710 } else if (idx != 0) {
5711 /* If the returned FID is IGIF, then there are three
5714 * 1) The object is upgraded from old Lustre-1.8 with
5715 * IGIF assigned to such object.
5716 * 2) The object is a backend local object and is
5717 * invisible to client.
5718 * 3) The object lost its LMV EA, and since there is
5719 * no FID-in-dirent for the orphan in the backend
5720 * /lost+found directory, then the low layer will
5721 * return IGIF for such object.
5723 * For MDTx (x != 0), it is either case 2) or case 3),
5724 * but from the LFSCK view, they are indistinguishable.
5725 * To be safe, the LFSCK will keep it there and report
5726 * some message, then the adminstrator can handle that
5729 * For MDT0, it is more possible the case 1). The LFSCK
5730 * will handle the orphan as an upgraded object. */
5731 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5732 "in the backend /lost+found on the MDT %04x, "
5733 "to be safe, skip it.\n",
5734 lfsck_lfsck2name(lfsck), ent->lde_namelen,
5735 ent->lde_name, PFID(&ent->lde_fid), idx);
5739 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5742 down_write(&com->lc_sem);
5743 com->lc_new_scanned++;
5744 ns->ln_local_lpf_scanned++;
5746 ns->ln_local_lpf_moved++;
5748 ns->ln_local_lpf_skipped++;
5750 ns->ln_local_lpf_failed++;
5751 up_write(&com->lc_sem);
5753 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5757 lfsck_control_speed_by_self(com);
5758 if (unlikely(!thread_is_running(thread))) {
5763 rc = iops->next(env, di);
5767 iops->fini(env, di);
5772 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5773 lfsck_lfsck2name(lfsck), rc);
5775 lfsck_object_put(env, parent);
5779 * Rescan the striped directory after the master LMV EA reset.
5781 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5782 * the namespace LFSCK engine scan the striped directory for the first time,
5783 * it will be regarded as a normal directory. As the LFSCK processing, some
5784 * other LFSCK instance on other MDT will find the shard of this striped dir,
5785 * and find that the master MDT-object of the striped directory lost its LMV
5786 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5787 * notify the LFSCK instance on this MDT to rescan the striped directory.
5789 * \param[in] env pointer to the thread context
5790 * \param[in] com pointer to the lfsck component
5791 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
5794 * \retval positive number for success
5795 * \retval 0 for LFSCK stopped/paused
5796 * \retval negative error number on failure
5798 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5799 struct lfsck_component *com,
5800 struct lfsck_lmv_unit *llu)
5802 struct lfsck_thread_info *info = lfsck_env_info(env);
5803 struct lfsck_instance *lfsck = com->lc_lfsck;
5804 struct lfsck_assistant_data *lad = com->lc_data;
5805 struct dt_object *dir;
5806 const struct dt_it_ops *iops;
5808 struct lu_dirent *ent =
5809 (struct lu_dirent *)info->lti_key;
5810 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5811 struct ptlrpc_thread *thread = &lfsck->li_thread;
5812 struct lfsck_namespace_req *lnr;
5813 struct lfsck_assistant_req *lar;
5818 LASSERT(list_empty(&lad->lad_req_list));
5820 lfsck->li_lmv = &llu->llu_lmv;
5821 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5822 rc = lfsck_open_dir(env, lfsck, 0);
5826 dir = lfsck->li_obj_dir;
5827 di = lfsck->li_di_dir;
5828 iops = &dir->do_index_ops->dio_it;
5830 rc = iops->rec(env, di, (struct dt_rec *)ent,
5831 lfsck->li_args_dir);
5833 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5837 if (bk->lb_param & LPF_FAILOUT)
5843 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5846 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5848 if (bk->lb_param & LPF_FAILOUT)
5849 GOTO(out, rc = PTR_ERR(lnr));
5854 lar = &lnr->lnr_lar;
5855 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5856 lfsck_namespace_assistant_req_fini(env, lar);
5857 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5860 if (unlikely(!thread_is_running(thread)))
5864 rc = iops->next(env, di);
5868 lfsck_close_dir(env, lfsck, rc);
5872 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5873 if (list_empty(&lad->lad_req_list))
5876 spin_lock(&lad->lad_lock);
5877 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5879 list_del_init(&lar->lar_list);
5880 spin_unlock(&lad->lad_lock);
5882 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5883 lfsck_namespace_assistant_req_fini(env, lar);
5885 RETURN(rc == 0 ? 1 : rc);
5889 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
5890 struct lfsck_component *com,
5891 struct dt_object *obj, bool first)
5893 struct lfsck_instance *lfsck = com->lc_lfsck;
5894 struct ptlrpc_thread *thread = &lfsck->li_thread;
5895 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5896 struct lfsck_namespace *ns = com->lc_file_ram;
5897 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
5898 struct dt_object *target;
5906 di = iops->init(env, obj, 0);
5908 RETURN(PTR_ERR(di));
5911 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5914 rc = iops->get(env, di, (const struct dt_key *)&fid);
5919 /* The start one either has been processed or does not exist,
5921 rc = iops->next(env, di);
5927 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5928 unlikely(!thread_is_running(thread)))
5931 key = iops->key(env, di);
5940 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5941 if (!fid_is_sane(&fid)) {
5946 target = lfsck_object_find_bottom(env, lfsck, &fid);
5947 if (IS_ERR(target)) {
5948 rc = PTR_ERR(target);
5952 if (dt_object_exists(target)) {
5953 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5955 rc = lfsck_namespace_double_scan_one(env, com,
5962 lfsck_object_put(env, target);
5965 down_write(&com->lc_sem);
5966 com->lc_new_checked++;
5967 com->lc_new_scanned++;
5968 if (rc >= 0 && fid_is_sane(&fid))
5969 ns->ln_fid_latest_scanned_phase2 = fid;
5971 ns->ln_objs_repaired_phase2++;
5973 ns->ln_objs_failed_phase2++;
5974 up_write(&com->lc_sem);
5976 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5979 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
5980 cfs_time_current())) &&
5981 com->lc_new_checked != 0) {
5982 down_write(&com->lc_sem);
5983 ns->ln_run_time_phase2 +=
5984 cfs_duration_sec(cfs_time_current() +
5985 HALF_SEC - com->lc_time_last_checkpoint);
5986 ns->ln_time_last_checkpoint = cfs_time_current_sec();
5987 ns->ln_objs_checked_phase2 += com->lc_new_checked;
5988 com->lc_new_checked = 0;
5989 rc = lfsck_namespace_store(env, com, false);
5990 up_write(&com->lc_sem);
5994 com->lc_time_last_checkpoint = cfs_time_current();
5995 com->lc_time_next_checkpoint =
5996 com->lc_time_last_checkpoint +
5997 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6000 lfsck_control_speed_by_self(com);
6001 if (unlikely(!thread_is_running(thread)))
6004 rc = iops->next(env, di);
6013 iops->fini(env, di);
6018 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
6019 struct lfsck_component *com)
6021 struct lfsck_instance *lfsck = com->lc_lfsck;
6022 struct lfsck_namespace *ns = com->lc_file_ram;
6027 while (!list_empty(&lfsck->li_list_lmv)) {
6028 struct lfsck_lmv_unit *llu;
6030 spin_lock(&lfsck->li_lock);
6031 llu = list_entry(lfsck->li_list_lmv.next,
6032 struct lfsck_lmv_unit, llu_link);
6033 list_del_init(&llu->llu_link);
6034 spin_unlock(&lfsck->li_lock);
6036 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6041 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6042 lfsck_lfsck2name(lfsck));
6044 lfsck_namespace_scan_local_lpf(env, com);
6046 com->lc_new_checked = 0;
6047 com->lc_new_scanned = 0;
6048 com->lc_time_last_checkpoint = cfs_time_current();
6049 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6050 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6052 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6053 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6054 com->lc_sub_trace_objs[i].lsto_obj, true);
6055 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6056 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6057 com->lc_sub_trace_objs[i].lsto_obj, false);
6059 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6060 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6065 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6066 struct lfsck_component *com,
6067 struct lfsck_position *pos)
6069 struct lfsck_assistant_data *lad = com->lc_data;
6070 struct lfsck_namespace_req *lnr;
6072 if (list_empty(&lad->lad_req_list))
6075 lnr = list_entry(lad->lad_req_list.next,
6076 struct lfsck_namespace_req,
6078 pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6079 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6080 pos->lp_dir_parent = lnr->lnr_lar.lar_fid;
6083 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6084 struct lfsck_component *com,
6087 struct lfsck_instance *lfsck = com->lc_lfsck;
6088 struct lfsck_namespace *ns = com->lc_file_ram;
6090 down_write(&com->lc_sem);
6091 ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6092 HALF_SEC - lfsck->li_time_last_checkpoint);
6093 ns->ln_time_last_checkpoint = cfs_time_current_sec();
6094 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6095 com->lc_new_checked = 0;
6098 if (ns->ln_flags & LF_INCOMPLETE)
6099 ns->ln_status = LS_PARTIAL;
6101 ns->ln_status = LS_COMPLETED;
6102 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6103 ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6104 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6105 ns->ln_success_count++;
6106 } else if (rc == 0) {
6107 if (lfsck->li_status != 0)
6108 ns->ln_status = lfsck->li_status;
6110 ns->ln_status = LS_STOPPED;
6112 ns->ln_status = LS_FAILED;
6115 rc = lfsck_namespace_store(env, com, false);
6116 up_write(&com->lc_sem);
6122 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6123 struct ptlrpc_request *req,
6127 struct lfsck_async_interpret_args *laia = args;
6128 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6130 ltd->ltd_synced_failures = 1;
6137 * Notify remote LFSCK instances about former failures.
6139 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6140 * some LFSCK verification requests (maybe because of network issues or the MDT
6141 * itself trouble). During the respond gap the MDT may missed some name entries
6142 * verification, then the MDT cannot know whether related MDT-objects have been
6143 * referenced by related name entries or not, then in the second-stage scanning,
6144 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6145 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6146 * name entry for repairing the orphan.
6148 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6149 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6150 * have ever missed some name entries verification and should skip the handling
6151 * for orphan MDT-objects.
6153 * \param[in] env pointer to the thread context
6154 * \param[in] com pointer to the lfsck component
6155 * \param[in] lr pointer to the lfsck request
6157 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6158 struct lfsck_component *com,
6159 struct lfsck_request *lr)
6161 struct lfsck_async_interpret_args *laia =
6162 &lfsck_env_info(env)->lti_laia2;
6163 struct lfsck_assistant_data *lad = com->lc_data;
6164 struct lfsck_namespace *ns = com->lc_file_ram;
6165 struct lfsck_instance *lfsck = com->lc_lfsck;
6166 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6167 struct lfsck_tgt_desc *ltd;
6168 struct ptlrpc_request_set *set;
6173 if (!lad->lad_incomplete)
6176 set = ptlrpc_prep_set();
6178 GOTO(out, rc = -ENOMEM);
6180 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6181 memset(laia, 0, sizeof(*laia));
6182 lad->lad_touch_gen++;
6184 down_read(<ds->ltd_rw_sem);
6185 cfs_foreach_bit(lad->lad_bitmap, idx) {
6186 ltd = LTD_TGT(ltds, idx);
6187 LASSERT(ltd != NULL);
6189 laia->laia_ltd = ltd;
6190 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6191 lfsck_namespace_assistant_sync_failures_interpret,
6192 laia, LFSCK_NOTIFY);
6194 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6195 "to sync failure with MDT %x: rc = %d\n",
6196 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6198 up_read(<ds->ltd_rw_sem);
6200 rc = ptlrpc_set_wait(set);
6201 ptlrpc_set_destroy(set);
6207 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6208 "to sync failure with MDTs, and related MDTs "
6209 "may handle orphan improperly: rc = %d\n",
6210 lfsck_lfsck2name(lfsck), rc);
6215 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6216 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6217 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6218 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6219 .la_double_scan_result = lfsck_namespace_double_scan_result,
6220 .la_req_fini = lfsck_namespace_assistant_req_fini,
6221 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6225 * Verify the specified linkEA entry for the given directory object.
6226 * If the object has no such linkEA entry or it has more other linkEA
6227 * entries, then re-generate the linkEA with the given information.
6229 * \param[in] env pointer to the thread context
6230 * \param[in] obj pointer to the dt_object to be handled
6231 * \param[in] cname the name for the child in the parent directory
6232 * \param[in] pfid the parent directory's FID for the linkEA
6234 * \retval 0 for success
6235 * \retval negative error number on failure
6237 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6238 const struct lu_name *cname, const struct lu_fid *pfid)
6240 struct dt_device *dev = lfsck_obj2dev(obj);
6241 struct linkea_data ldata = { NULL };
6242 struct lu_buf linkea_buf;
6245 int fl = LU_XATTR_CREATE;
6249 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6251 rc = lfsck_links_read(env, obj, &ldata);
6252 if (rc == -ENODATA) {
6254 } else if (rc == 0) {
6255 fl = LU_XATTR_REPLACE;
6256 if (ldata.ld_leh->leh_reccount != 1) {
6259 rc = linkea_links_find(&ldata, cname, pfid);
6268 rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6272 rc = linkea_add_buf(&ldata, cname, pfid);
6276 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6277 ldata.ld_leh->leh_len);
6278 th = dt_trans_create(env, dev);
6280 RETURN(PTR_ERR(th));
6282 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6283 XATTR_NAME_LINK, fl, th);
6287 rc = dt_trans_start_local(env, dev, th);
6291 dt_write_lock(env, obj, 0);
6292 rc = dt_xattr_set(env, obj, &linkea_buf,
6293 XATTR_NAME_LINK, fl, th);
6294 dt_write_unlock(env, obj);
6299 dt_trans_stop(env, dev, th);
6304 * Get the name and parent directory's FID from the first linkEA entry.
6306 * \param[in] env pointer to the thread context
6307 * \param[in] obj pointer to the object which get linkEA from
6308 * \param[out] name pointer to the buffer to hold the name
6309 * in the first linkEA entry
6310 * \param[out] pfid pointer to the buffer to hold the parent
6311 * directory's FID in the first linkEA entry
6313 * \retval 0 for success
6314 * \retval negative error number on failure
6316 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6317 char *name, struct lu_fid *pfid)
6319 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6320 struct linkea_data ldata = { NULL };
6323 rc = lfsck_links_read(env, obj, &ldata);
6327 linkea_first_entry(&ldata);
6328 if (ldata.ld_lee == NULL)
6331 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6332 /* To guarantee the 'name' is terminated with '0'. */
6333 memcpy(name, cname->ln_name, cname->ln_namelen);
6334 name[cname->ln_namelen] = 0;
6340 * Update the object's name entry with the given FID.
6342 * \param[in] env pointer to the thread context
6343 * \param[in] lfsck pointer to the lfsck instance
6344 * \param[in] dir pointer to the directory that holds
6346 * \param[in] name the name for the entry to be updated
6347 * \param[in] fid the new FID for the name entry referenced
6348 * \param[in] type the type for the name entry to be updated
6350 * \retval 0 for success
6351 * \retval negative error number on failure
6353 int lfsck_update_name_entry(const struct lu_env *env,
6354 struct lfsck_instance *lfsck,
6355 struct dt_object *dir, const char *name,
6356 const struct lu_fid *fid, __u32 type)
6358 struct lfsck_thread_info *info = lfsck_env_info(env);
6359 struct dt_insert_rec *rec = &info->lti_dt_rec;
6360 struct lfsck_lock_handle *llh = &info->lti_llh;
6361 struct dt_device *dev = lfsck_obj2dev(dir);
6367 rc = lfsck_lock(env, lfsck, dir, name, llh,
6368 MDS_INODELOCK_UPDATE, LCK_PW);
6372 th = dt_trans_create(env, dev);
6374 GOTO(unlock, rc = PTR_ERR(th));
6376 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6380 rec->rec_type = type;
6382 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6383 (const struct dt_key *)name, th);
6387 rc = dt_declare_ref_add(env, dir, th);
6391 rc = dt_trans_start_local(env, dev, th);
6395 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
6396 if (rc == -ENOENT) {
6404 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6405 (const struct dt_key *)name, th, 1);
6406 if (rc == 0 && S_ISDIR(type) && !exists) {
6407 dt_write_lock(env, dir, 0);
6408 rc = dt_ref_add(env, dir, th);
6409 dt_write_unlock(env, dir);
6415 dt_trans_stop(env, dev, th);
6419 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6420 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6421 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6426 int lfsck_namespace_setup(const struct lu_env *env,
6427 struct lfsck_instance *lfsck)
6429 struct lfsck_component *com;
6430 struct lfsck_namespace *ns;
6431 struct dt_object *root = NULL;
6432 struct dt_object *obj;
6437 LASSERT(lfsck->li_master);
6443 INIT_LIST_HEAD(&com->lc_link);
6444 INIT_LIST_HEAD(&com->lc_link_dir);
6445 init_rwsem(&com->lc_sem);
6446 atomic_set(&com->lc_ref, 1);
6447 com->lc_lfsck = lfsck;
6448 com->lc_type = LFSCK_TYPE_NAMESPACE;
6449 com->lc_ops = &lfsck_namespace_ops;
6450 com->lc_data = lfsck_assistant_data_init(
6451 &lfsck_namespace_assistant_ops,
6453 if (com->lc_data == NULL)
6454 GOTO(out, rc = -ENOMEM);
6456 com->lc_file_size = sizeof(struct lfsck_namespace);
6457 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6458 if (com->lc_file_ram == NULL)
6459 GOTO(out, rc = -ENOMEM);
6461 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6462 if (com->lc_file_disk == NULL)
6463 GOTO(out, rc = -ENOMEM);
6465 for (i = 0; i < LFSCK_STF_COUNT; i++)
6466 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6468 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6470 GOTO(out, rc = PTR_ERR(root));
6472 if (unlikely(!dt_try_as_dir(env, root)))
6473 GOTO(out, rc = -ENOTDIR);
6475 obj = local_index_find_or_create(env, lfsck->li_los, root,
6477 S_IFREG | S_IRUGO | S_IWUSR,
6478 &dt_lfsck_features);
6480 GOTO(out, rc = PTR_ERR(obj));
6483 rc = lfsck_namespace_load(env, com);
6485 rc = lfsck_namespace_init(env, com);
6487 rc = lfsck_namespace_reset(env, com, true);
6489 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6493 ns = com->lc_file_ram;
6494 switch (ns->ln_status) {
6499 spin_lock(&lfsck->li_lock);
6500 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6501 spin_unlock(&lfsck->li_lock);
6504 CERROR("%s: unknown lfsck_namespace status %d\n",
6505 lfsck_lfsck2name(lfsck), ns->ln_status);
6507 case LS_SCANNING_PHASE1:
6508 case LS_SCANNING_PHASE2:
6509 /* No need to store the status to disk right now.
6510 * If the system crashed before the status stored,
6511 * it will be loaded back when next time. */
6512 ns->ln_status = LS_CRASHED;
6516 spin_lock(&lfsck->li_lock);
6517 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6518 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6519 spin_unlock(&lfsck->li_lock);
6526 if (root != NULL && !IS_ERR(root))
6527 lfsck_object_put(env, root);
6529 lfsck_component_cleanup(env, com);
6530 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6531 lfsck_lfsck2name(lfsck), rc);