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, 2017, Intel Corporation.
26 * lustre/lfsck/lfsck_namespace.c
28 * Author: Fan, Yong <fan.yong@intel.com>
31 #define DEBUG_SUBSYSTEM S_LFSCK
33 #include <lu_object.h>
34 #include <dt_object.h>
35 #include <md_object.h>
36 #include <lustre_fid.h>
37 #include <lustre_lib.h>
38 #include <lustre_net.h>
40 #include "lfsck_internal.h"
42 #define LFSCK_NAMESPACE_MAGIC_V1 0xA0629D03
43 #define LFSCK_NAMESPACE_MAGIC_V2 0xA0621A0B
44 #define LFSCK_NAMESPACE_MAGIC_V3 0xA06249FF
46 /* For Lustre-2.x (x <= 6), the namespace LFSCK used LFSCK_NAMESPACE_MAGIC_V1
47 * as the trace file magic. When downgrade to such old release, the old LFSCK
48 * will not recognize the new LFSCK_NAMESPACE_MAGIC_V2 in the new trace file,
49 * then it will reset the whole LFSCK, and will not cause start failure. The
50 * similar case will happen when upgrade from such old release. */
51 #define LFSCK_NAMESPACE_MAGIC LFSCK_NAMESPACE_MAGIC_V3
53 enum lfsck_nameentry_check {
54 LFSCK_NAMEENTRY_DEAD = 1, /* The object has been unlinked. */
55 LFSCK_NAMEENTRY_REMOVED = 2, /* The entry has been removed. */
56 LFSCK_NAMEENTRY_RECREATED = 3, /* The entry has been recreated. */
59 static struct lfsck_namespace_req *
60 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
61 struct lfsck_assistant_object *lso,
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_parent = lfsck_assistant_object_get(lso);
74 lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
75 lnr->lnr_fid = ent->lde_fid;
76 lnr->lnr_dir_cookie = ent->lde_hash;
77 lnr->lnr_attr = ent->lde_attrs;
80 lnr->lnr_namelen = ent->lde_namelen;
81 memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
86 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
87 struct lfsck_assistant_req *lar)
89 struct lfsck_namespace_req *lnr =
90 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
92 if (lnr->lnr_lmv != NULL)
93 lfsck_lmv_put(env, lnr->lnr_lmv);
95 lfsck_assistant_object_put(env, lar->lar_parent);
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 = le64_to_cpu(src->ln_run_time_phase1);
107 dst->ln_run_time_phase2 = le64_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);
166 dst->ln_time_latest_reset = le64_to_cpu(src->ln_time_latest_reset);
167 dst->ln_linkea_overflow_cleared =
168 le64_to_cpu(src->ln_linkea_overflow_cleared);
169 dst->ln_agent_entries_repaired =
170 le64_to_cpu(src->ln_agent_entries_repaired);
173 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
174 struct lfsck_namespace *src)
176 dst->ln_magic = cpu_to_le32(src->ln_magic);
177 dst->ln_status = cpu_to_le32(src->ln_status);
178 dst->ln_flags = cpu_to_le32(src->ln_flags);
179 dst->ln_success_count = cpu_to_le32(src->ln_success_count);
180 dst->ln_run_time_phase1 = cpu_to_le64(src->ln_run_time_phase1);
181 dst->ln_run_time_phase2 = cpu_to_le64(src->ln_run_time_phase2);
182 dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
183 dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
184 dst->ln_time_last_checkpoint =
185 cpu_to_le64(src->ln_time_last_checkpoint);
186 lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
187 &src->ln_pos_latest_start);
188 lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
189 &src->ln_pos_last_checkpoint);
190 lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
191 &src->ln_pos_first_inconsistent);
192 dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
193 dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
194 dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
195 dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
196 dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
197 dst->ln_objs_repaired_phase2 =
198 cpu_to_le64(src->ln_objs_repaired_phase2);
199 dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
200 dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
201 fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
202 &src->ln_fid_latest_scanned_phase2);
203 dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
204 dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
205 dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
206 dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
207 dst->ln_unknown_inconsistency =
208 cpu_to_le64(src->ln_unknown_inconsistency);
209 dst->ln_unmatched_pairs_repaired =
210 cpu_to_le64(src->ln_unmatched_pairs_repaired);
211 dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
212 dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
213 dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
214 dst->ln_lost_dirent_repaired =
215 cpu_to_le64(src->ln_lost_dirent_repaired);
216 dst->ln_striped_dirs_scanned =
217 cpu_to_le64(src->ln_striped_dirs_scanned);
218 dst->ln_striped_dirs_repaired =
219 cpu_to_le64(src->ln_striped_dirs_repaired);
220 dst->ln_striped_dirs_failed =
221 cpu_to_le64(src->ln_striped_dirs_failed);
222 dst->ln_striped_dirs_disabled =
223 cpu_to_le64(src->ln_striped_dirs_disabled);
224 dst->ln_striped_dirs_skipped =
225 cpu_to_le64(src->ln_striped_dirs_skipped);
226 dst->ln_striped_shards_scanned =
227 cpu_to_le64(src->ln_striped_shards_scanned);
228 dst->ln_striped_shards_repaired =
229 cpu_to_le64(src->ln_striped_shards_repaired);
230 dst->ln_striped_shards_failed =
231 cpu_to_le64(src->ln_striped_shards_failed);
232 dst->ln_striped_shards_skipped =
233 cpu_to_le64(src->ln_striped_shards_skipped);
234 dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
235 dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
236 dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
237 dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
238 dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
239 dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
240 dst->ln_time_latest_reset = cpu_to_le64(src->ln_time_latest_reset);
241 dst->ln_linkea_overflow_cleared =
242 cpu_to_le64(src->ln_linkea_overflow_cleared);
243 dst->ln_agent_entries_repaired =
244 cpu_to_le64(src->ln_agent_entries_repaired);
247 static void lfsck_namespace_record_failure(const struct lu_env *env,
248 struct lfsck_instance *lfsck,
249 struct lfsck_namespace *ns)
251 struct lfsck_position pos;
253 ns->ln_items_failed++;
254 lfsck_pos_fill(env, lfsck, &pos, false);
255 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
256 lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
257 ns->ln_pos_first_inconsistent = pos;
259 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
260 "inconsistency at the pos [%llu, "DFID", %#llx]\n",
261 lfsck_lfsck2name(lfsck),
262 ns->ln_pos_first_inconsistent.lp_oit_cookie,
263 PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
264 ns->ln_pos_first_inconsistent.lp_dir_cookie);
269 * Load the MDT bitmap from the lfsck_namespace trace file.
271 * \param[in] env pointer to the thread context
272 * \param[in] com pointer to the lfsck component
274 * \retval 0 for success
275 * \retval negative error number on failure or data corruption
277 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
278 struct lfsck_component *com)
280 struct dt_object *obj = com->lc_obj;
281 struct lfsck_assistant_data *lad = com->lc_data;
282 struct lfsck_namespace *ns = com->lc_file_ram;
283 struct cfs_bitmap *bitmap = lad->lad_bitmap;
289 if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
291 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
293 nbits = ns->ln_bitmap_size;
295 if (unlikely(nbits < BITS_PER_LONG))
296 nbits = BITS_PER_LONG;
298 if (nbits > bitmap->size) {
299 __u32 new_bits = bitmap->size;
300 struct cfs_bitmap *new_bitmap;
302 while (new_bits < nbits)
305 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
306 if (new_bitmap == NULL)
309 lad->lad_bitmap = new_bitmap;
310 CFS_FREE_BITMAP(bitmap);
314 if (ns->ln_bitmap_size == 0) {
315 lad->lad_incomplete = 0;
316 CFS_RESET_BITMAP(bitmap);
321 size = (ns->ln_bitmap_size + 7) >> 3;
322 rc = dt_xattr_get(env, obj,
323 lfsck_buf_get(env, bitmap->data, size),
324 XATTR_NAME_LFSCK_BITMAP);
326 RETURN(rc >= 0 ? -EINVAL : rc);
328 if (cfs_bitmap_check_empty(bitmap))
329 lad->lad_incomplete = 0;
331 lad->lad_incomplete = 1;
337 * Load namespace LFSCK statistics information from the trace file.
339 * \param[in] env pointer to the thread context
340 * \param[in] com pointer to the lfsck component
342 * \retval 0 for success
343 * \retval negative error number on failure
345 static int lfsck_namespace_load(const struct lu_env *env,
346 struct lfsck_component *com)
348 int len = com->lc_file_size;
351 rc = dt_xattr_get(env, com->lc_obj,
352 lfsck_buf_get(env, com->lc_file_disk, len),
353 XATTR_NAME_LFSCK_NAMESPACE);
355 struct lfsck_namespace *ns = com->lc_file_ram;
357 lfsck_namespace_le_to_cpu(ns,
358 (struct lfsck_namespace *)com->lc_file_disk);
359 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
360 CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
361 "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
362 ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
367 } else if (rc != -ENODATA) {
368 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
369 "expected = %d: rc = %d\n",
370 lfsck_lfsck2name(com->lc_lfsck), len, rc);
378 static int lfsck_namespace_store(const struct lu_env *env,
379 struct lfsck_component *com)
381 struct dt_object *obj = com->lc_obj;
382 struct lfsck_instance *lfsck = com->lc_lfsck;
383 struct lfsck_namespace *ns = com->lc_file_ram;
384 struct lfsck_assistant_data *lad = com->lc_data;
385 struct dt_device *dev = lfsck_obj2dev(obj);
386 struct cfs_bitmap *bitmap = NULL;
387 struct thandle *handle;
389 int len = com->lc_file_size;
394 bitmap = lad->lad_bitmap;
395 nbits = bitmap->size;
398 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
401 ns->ln_bitmap_size = nbits;
402 lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
404 handle = dt_trans_create(env, dev);
406 GOTO(log, rc = PTR_ERR(handle));
408 rc = dt_declare_xattr_set(env, obj,
409 lfsck_buf_get(env, com->lc_file_disk, len),
410 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
414 if (bitmap != NULL) {
415 rc = dt_declare_xattr_set(env, obj,
416 lfsck_buf_get(env, bitmap->data, nbits >> 3),
417 XATTR_NAME_LFSCK_BITMAP, 0, handle);
422 rc = dt_trans_start_local(env, dev, handle);
426 rc = dt_xattr_set(env, obj,
427 lfsck_buf_get(env, com->lc_file_disk, len),
428 XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
429 if (rc == 0 && bitmap != NULL)
430 rc = dt_xattr_set(env, obj,
431 lfsck_buf_get(env, bitmap->data, nbits >> 3),
432 XATTR_NAME_LFSCK_BITMAP, 0, handle);
437 dt_trans_stop(env, dev, handle);
441 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
442 lfsck_lfsck2name(lfsck), rc);
446 static int lfsck_namespace_init(const struct lu_env *env,
447 struct lfsck_component *com)
449 struct lfsck_namespace *ns = com->lc_file_ram;
452 memset(ns, 0, sizeof(*ns));
453 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
454 ns->ln_status = LS_INIT;
455 ns->ln_time_latest_reset = ktime_get_real_seconds();
456 down_write(&com->lc_sem);
457 rc = lfsck_namespace_store(env, com);
459 rc = lfsck_load_sub_trace_files(env, com,
460 &dt_lfsck_namespace_features, LFSCK_NAMESPACE, true);
461 up_write(&com->lc_sem);
467 * Update the namespace LFSCK trace file for the given @fid
469 * \param[in] env pointer to the thread context
470 * \param[in] com pointer to the lfsck component
471 * \param[in] fid the fid which flags to be updated in the lfsck
473 * \param[in] add true if add new flags, otherwise remove flags
475 * \retval 0 for success or nothing to be done
476 * \retval negative error number on failure
478 int lfsck_namespace_trace_update(const struct lu_env *env,
479 struct lfsck_component *com,
480 const struct lu_fid *fid,
481 const __u8 flags, bool add)
483 struct lfsck_instance *lfsck = com->lc_lfsck;
484 struct dt_object *obj;
485 struct lu_fid *key = &lfsck_env_info(env)->lti_fid3;
486 struct dt_device *dev;
487 struct thandle *th = NULL;
496 if (unlikely(!fid_is_sane(fid)))
499 idx = lfsck_sub_trace_file_fid2idx(fid);
500 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
501 obj = com->lc_sub_trace_objs[idx].lsto_obj;
502 if (unlikely(obj == NULL)) {
503 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
507 lfsck_object_get(obj);
508 dev = lfsck_obj2dev(obj);
509 fid_cpu_to_be(key, fid);
510 rc = dt_lookup(env, obj, (struct dt_rec *)&old,
511 (const struct dt_key *)key);
514 GOTO(unlock, rc = 0);
518 } else if (rc == 0) {
520 if ((old & flags) == flags)
521 GOTO(unlock, rc = 0);
525 if ((old & flags) == 0)
526 GOTO(unlock, rc = 0);
534 th = dt_trans_create(env, dev);
536 GOTO(log, rc = PTR_ERR(th));
539 rc = dt_declare_delete(env, obj,
540 (const struct dt_key *)key, th);
546 rc = dt_declare_insert(env, obj,
547 (const struct dt_rec *)&new,
548 (const struct dt_key *)key, th);
553 rc = dt_trans_start_local(env, dev, th);
558 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
564 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
565 (const struct dt_key *)key, th, 1);
573 if (th != NULL && !IS_ERR(th))
574 dt_trans_stop(env, dev, th);
576 CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
577 "trace file, flags %x, old %x, new %x: rc = %d\n",
578 lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
579 (__u32)flags, (__u32)old, (__u32)new, rc);
582 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
583 lfsck_object_put(env, obj);
588 int lfsck_namespace_check_exist(const struct lu_env *env,
589 struct dt_object *dir,
590 struct dt_object *obj, const char *name)
592 struct lu_fid *fid = &lfsck_env_info(env)->lti_fid;
596 if (unlikely(lfsck_is_dead_obj(obj)))
597 RETURN(LFSCK_NAMEENTRY_DEAD);
599 rc = dt_lookup(env, dir, (struct dt_rec *)fid,
600 (const struct dt_key *)name);
602 RETURN(LFSCK_NAMEENTRY_REMOVED);
607 if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
608 RETURN(LFSCK_NAMEENTRY_RECREATED);
613 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
614 struct dt_object *obj,
615 struct thandle *handle)
619 /* For remote updating LINKEA, there may be further LFSCK action
620 * on remote MDT after the updating, so update the LINKEA ASAP. */
621 if (dt_object_remote(obj))
624 /* For destroying all invalid linkEA entries. */
625 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
627 /* For insert new linkEA entry. */
628 rc = dt_declare_xattr_set(env, obj,
629 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
630 XATTR_NAME_LINK, 0, handle);
634 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
635 struct linkea_data *ldata, bool with_rec)
639 if (ldata->ld_buf->lb_buf == NULL)
642 if (!dt_object_exists(obj))
645 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
647 /* Buf was too small, figure out what we need. */
648 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
649 if (unlikely(rc == 0))
655 lu_buf_realloc(ldata->ld_buf, rc);
656 if (ldata->ld_buf->lb_buf == NULL)
659 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
662 if (unlikely(rc == 0))
667 rc = linkea_init_with_rec(ldata);
669 rc = linkea_init(ldata);
676 * Remove linkEA for the given object.
678 * The caller should take the ldlm lock before the calling.
680 * \param[in] env pointer to the thread context
681 * \param[in] com pointer to the lfsck component
682 * \param[in] obj pointer to the dt_object to be handled
684 * \retval 0 for repaired cases
685 * \retval negative error number on failure
687 static int lfsck_namespace_links_remove(const struct lu_env *env,
688 struct lfsck_component *com,
689 struct dt_object *obj)
691 struct lfsck_instance *lfsck = com->lc_lfsck;
692 struct dt_device *dev = lfsck_obj2dev(obj);
693 struct thandle *th = NULL;
697 LASSERT(dt_object_remote(obj) == 0);
699 th = dt_trans_create(env, dev);
701 GOTO(log, rc = PTR_ERR(th));
703 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
707 rc = dt_trans_start_local(env, dev, th);
711 dt_write_lock(env, obj, 0);
712 if (unlikely(lfsck_is_dead_obj(obj)))
713 GOTO(unlock, rc = -ENOENT);
715 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
716 GOTO(unlock, rc = 0);
718 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
723 dt_write_unlock(env, obj);
726 dt_trans_stop(env, dev, th);
729 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
730 "for the object "DFID": rc = %d\n",
731 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
734 struct lfsck_namespace *ns = com->lc_file_ram;
736 ns->ln_flags |= LF_INCONSISTENT;
742 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
743 struct linkea_data *ldata, struct thandle *handle)
748 lfsck_buf_init(&buf, ldata->ld_buf->lb_buf, ldata->ld_leh->leh_len);
751 rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LINK, 0, handle);
752 if (unlikely(rc == -ENOSPC)) {
753 rc = linkea_overflow_shrink(ldata);
754 if (likely(rc > 0)) {
763 static int lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
764 struct lu_name *cname,
766 char *buf, const int buflen)
768 linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
769 if (unlikely(ldata->ld_reclen <= 0 ||
770 ldata->ld_reclen + sizeof(struct link_ea_header) >
771 ldata->ld_leh->leh_len ||
772 cname->ln_namelen <= 0 ||
773 cname->ln_namelen > NAME_MAX ||
774 cname->ln_namelen >= buflen ||
778 /* To guarantee the 'name' is terminated with '0'. */
779 memcpy(buf, cname->ln_name, cname->ln_namelen);
780 buf[cname->ln_namelen] = 0;
781 cname->ln_name = buf;
786 static void lfsck_linkea_del_buf(struct linkea_data *ldata,
787 const struct lu_name *lname)
789 LASSERT(ldata->ld_leh != NULL && ldata->ld_lee != NULL);
791 /* If current record is corrupted, all the subsequent
792 * records will be dropped. */
793 if (unlikely(ldata->ld_reclen <= 0 ||
794 ldata->ld_reclen + sizeof(struct link_ea_header) >
795 ldata->ld_leh->leh_len)) {
796 void *ptr = ldata->ld_lee;
798 ldata->ld_leh->leh_len = sizeof(struct link_ea_header);
799 ldata->ld_leh->leh_reccount = 0;
800 linkea_first_entry(ldata);
801 while (ldata->ld_lee != NULL &&
802 (char *)ldata->ld_lee < (char *)ptr) {
803 int reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
804 ldata->ld_lee->lee_reclen[1];
806 ldata->ld_leh->leh_len += reclen;
807 ldata->ld_leh->leh_reccount++;
808 ldata->ld_lee = (struct link_ea_entry *)
809 ((char *)ldata->ld_lee + reclen);
812 ldata->ld_lee = NULL;
814 linkea_del_buf(ldata, lname);
818 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
819 struct lu_name *cname,
823 struct link_ea_entry *oldlee;
827 oldlee = ldata->ld_lee;
828 oldlen = ldata->ld_reclen;
829 linkea_next_entry(ldata);
830 while (ldata->ld_lee != NULL) {
831 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
832 ldata->ld_lee->lee_reclen[1];
833 if (unlikely(ldata->ld_reclen == oldlen &&
834 memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
839 lfsck_linkea_del_buf(ldata, cname);
841 linkea_next_entry(ldata);
844 ldata->ld_lee = oldlee;
845 ldata->ld_reclen = oldlen;
851 * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
853 * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
854 * with the given type to generate the name, the detailed rules for name
855 * have been described as following.
857 * The function also generates the linkEA corresponding to the name entry
858 * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
860 * \param[in] env pointer to the thread context
861 * \param[in] com pointer to the lfsck component
862 * \param[in] orphan pointer to the orphan MDT-object
863 * \param[in] infix additional information for the orphan name, such as
864 * the FID for original
865 * \param[in] type the type for describing why the orphan MDT-object is
866 * created. The rules are as following:
868 * type "D": The MDT-object is a directory, it may knows its parent
869 * but because there is no valid linkEA, the LFSCK cannot
870 * know where to put it back to the namespace.
871 * type "O": The MDT-object has no linkEA, and there is no name
872 * entry that references the MDT-object.
874 * type "S": The orphan MDT-object is a shard of a striped directory
876 * \see lfsck_layout_recreate_parent() for more types.
878 * The orphan name will be like:
879 * ${FID}-${infix}-${type}-${conflict_version}
881 * \param[out] count if some others inserted some linkEA entries by race,
882 * then return the linkEA entries count.
884 * \retval positive number for repaired cases
885 * \retval 0 if needs to repair nothing
886 * \retval negative error number on failure
888 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
889 struct lfsck_component *com,
890 struct dt_object *orphan,
891 const char *infix, const char *type,
894 struct lfsck_thread_info *info = lfsck_env_info(env);
895 struct lu_name *cname = &info->lti_name;
896 struct dt_insert_rec *rec = &info->lti_dt_rec;
897 struct lu_attr *la = &info->lti_la2;
898 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
899 const struct lu_fid *pfid;
901 struct lfsck_instance *lfsck = com->lc_lfsck;
902 struct dt_device *dev = lfsck_obj2dev(orphan);
903 struct dt_object *parent;
904 struct thandle *th = NULL;
905 struct lfsck_lock_handle *pllh = &info->lti_llh;
906 struct lustre_handle clh = { 0 };
907 struct linkea_data ldata2 = { NULL };
908 struct lu_buf linkea_buf;
915 cname->ln_name = NULL;
916 if (unlikely(lfsck->li_lpf_obj == NULL))
917 GOTO(log, rc = -ENXIO);
919 parent = lfsck->li_lpf_obj;
920 pfid = lfsck_dto2fid(parent);
924 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
925 PFID(cfid), infix, type, idx++);
926 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
927 (const struct dt_key *)info->lti_key);
928 if (rc != 0 && rc != -ENOENT)
931 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
933 } while (rc == 0 && !exist);
935 rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
936 MDS_INODELOCK_UPDATE, LCK_PW);
940 /* Re-check whether the name conflict with othrs after taken
942 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
943 (const struct dt_key *)info->lti_key);
945 if (!lu_fid_eq(cfid, &tfid)) {
952 } else if (rc != -ENOENT) {
958 cname->ln_name = info->lti_key;
959 cname->ln_namelen = namelen;
960 rc = linkea_links_new(&ldata2, &info->lti_linkea_buf2,
965 rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
966 MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
967 MDS_INODELOCK_XATTR, LCK_EX);
971 lfsck_buf_init(&linkea_buf, ldata2.ld_buf->lb_buf,
972 ldata2.ld_leh->leh_len);
973 th = dt_trans_create(env, dev);
975 GOTO(log, rc = PTR_ERR(th));
977 if (S_ISDIR(lfsck_object_type(orphan))) {
978 rc = dt_declare_delete(env, orphan,
979 (const struct dt_key *)dotdot, th);
983 rec->rec_type = S_IFDIR;
985 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
986 (const struct dt_key *)dotdot, th);
991 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
992 XATTR_NAME_LINK, 0, th);
997 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
999 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1000 (const struct dt_key *)cname->ln_name,
1005 if (S_ISDIR(rec->rec_type)) {
1006 rc = dt_declare_ref_add(env, parent, th);
1012 memset(la, 0, sizeof(*la));
1013 la->la_ctime = ktime_get_real_seconds();
1014 la->la_valid = LA_CTIME;
1015 rc = dt_declare_attr_set(env, orphan, la, th);
1019 rc = dt_trans_start_local(env, dev, th);
1023 dt_write_lock(env, orphan, 0);
1024 rc = lfsck_links_read2_with_rec(env, orphan, &ldata2);
1025 if (likely(rc == -ENODATA || rc == -EINVAL)) {
1026 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1027 GOTO(unlock, rc = 1);
1029 if (S_ISDIR(lfsck_object_type(orphan))) {
1030 rc = dt_delete(env, orphan,
1031 (const struct dt_key *)dotdot, th);
1035 rec->rec_type = S_IFDIR;
1036 rec->rec_fid = pfid;
1037 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1038 (const struct dt_key *)dotdot, th, 1);
1043 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1046 if (rc == 0 && count != NULL)
1047 *count = ldata2.ld_leh->leh_reccount;
1051 dt_write_unlock(env, orphan);
1053 if (rc == 0 && !exist) {
1054 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1055 rec->rec_fid = cfid;
1056 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1057 (const struct dt_key *)cname->ln_name, th, 1);
1058 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1059 dt_write_lock(env, parent, 0);
1060 rc = dt_ref_add(env, parent, th);
1061 dt_write_unlock(env, parent);
1066 rc = dt_attr_set(env, orphan, la, th);
1068 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1071 dt_write_unlock(env, orphan);
1074 dt_trans_stop(env, dev, th);
1077 lfsck_ibits_unlock(&clh, LCK_EX);
1079 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1080 "object "DFID", name = %s: rc = %d\n",
1081 lfsck_lfsck2name(lfsck), PFID(cfid),
1082 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1085 struct lfsck_namespace *ns = com->lc_file_ram;
1087 ns->ln_flags |= LF_INCONSISTENT;
1094 * Add the specified name entry back to namespace.
1096 * If there is a linkEA entry that back references a name entry under
1097 * some parent directory, but such parent directory does not have the
1098 * claimed name entry. On the other hand, the linkEA entries count is
1099 * not larger than the MDT-object's hard link count. Under such case,
1100 * it is quite possible that the name entry is lost. Then the LFSCK
1101 * should add the name entry back to the namespace.
1103 * \param[in] env pointer to the thread context
1104 * \param[in] com pointer to the lfsck component
1105 * \param[in] parent pointer to the directory under which the name entry
1106 * will be inserted into
1107 * \param[in] child pointer to the object referenced by the name entry
1108 * that to be inserted into the parent
1109 * \param[in] name the name for the child in the parent directory
1111 * \retval positive number for repaired cases
1112 * \retval 0 if nothing to be repaired
1113 * \retval negative error number on failure
1115 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1116 struct lfsck_component *com,
1117 struct dt_object *parent,
1118 struct dt_object *child,
1121 struct lfsck_thread_info *info = lfsck_env_info(env);
1122 struct lu_attr *la = &info->lti_la;
1123 struct dt_insert_rec *rec = &info->lti_dt_rec;
1124 struct lfsck_instance *lfsck = com->lc_lfsck;
1125 /* The child and its name may be on different MDTs. */
1126 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1127 const struct lu_fid *cfid = lfsck_dto2fid(child);
1128 struct dt_device *dev = lfsck->li_next;
1129 struct thandle *th = NULL;
1130 struct lfsck_lock_handle *llh = &info->lti_llh;
1134 /* @parent/@child may be based on lfsck->li_bottom,
1135 * but here we need the object based on the lfsck->li_next. */
1137 parent = lfsck_object_locate(dev, parent);
1139 GOTO(log, rc = PTR_ERR(parent));
1141 if (unlikely(!dt_try_as_dir(env, parent)))
1142 GOTO(log, rc = -ENOTDIR);
1144 child = lfsck_object_locate(dev, child);
1146 GOTO(log, rc = PTR_ERR(child));
1148 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1151 rc = lfsck_lock(env, lfsck, parent, name, llh,
1152 MDS_INODELOCK_UPDATE, LCK_PW);
1156 th = dt_trans_create(env, dev);
1158 GOTO(unlock, rc = PTR_ERR(th));
1160 rec->rec_type = lfsck_object_type(child) & S_IFMT;
1161 rec->rec_fid = cfid;
1162 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1163 (const struct dt_key *)name, th);
1167 if (S_ISDIR(rec->rec_type)) {
1168 rc = dt_declare_ref_add(env, parent, th);
1173 memset(la, 0, sizeof(*la));
1174 la->la_ctime = ktime_get_real_seconds();
1175 la->la_valid = LA_CTIME;
1176 rc = dt_declare_attr_set(env, parent, la, th);
1180 rc = dt_declare_attr_set(env, child, la, th);
1184 rc = dt_trans_start_local(env, dev, th);
1188 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1189 (const struct dt_key *)name, th, 1);
1193 if (S_ISDIR(rec->rec_type)) {
1194 dt_write_lock(env, parent, 0);
1195 rc = dt_ref_add(env, parent, th);
1196 dt_write_unlock(env, parent);
1201 la->la_ctime = ktime_get_real_seconds();
1202 rc = dt_attr_set(env, parent, la, th);
1206 rc = dt_attr_set(env, child, la, th);
1208 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1211 dt_trans_stop(env, dev, th);
1217 CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1218 "the name %s and type %o to the parent "DFID": rc = %d\n",
1219 lfsck_lfsck2name(lfsck), PFID(cfid), name,
1220 lfsck_object_type(child) & S_IFMT, PFID(pfid), rc);
1223 struct lfsck_namespace *ns = com->lc_file_ram;
1225 ns->ln_flags |= LF_INCONSISTENT;
1227 ns->ln_lost_dirent_repaired++;
1234 * Create the specified orphan directory.
1236 * For the case that the parent MDT-object stored in some MDT-object's
1237 * linkEA entry is lost, the LFSCK will re-create the parent object as
1238 * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1239 * with the name ${FID}-P-${conflict_version}.
1241 * \param[in] env pointer to the thread context
1242 * \param[in] com pointer to the lfsck component
1243 * \param[in] orphan pointer to the orphan MDT-object to be created
1244 * \param[in] lmv pointer to master LMV EA that will be set to the orphan
1246 * \retval positive number for repaired cases
1247 * \retval negative error number on failure
1249 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1250 struct lfsck_component *com,
1251 struct dt_object *orphan,
1252 struct lmv_mds_md_v1 *lmv)
1254 struct lfsck_thread_info *info = lfsck_env_info(env);
1255 struct lu_attr *la = &info->lti_la;
1256 struct dt_allocation_hint *hint = &info->lti_hint;
1257 struct dt_object_format *dof = &info->lti_dof;
1258 struct lu_name *cname = &info->lti_name2;
1259 struct dt_insert_rec *rec = &info->lti_dt_rec;
1260 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
1261 const struct lu_fid *cfid = lfsck_dto2fid(orphan);
1263 struct lfsck_instance *lfsck = com->lc_lfsck;
1264 struct lfsck_namespace *ns = com->lc_file_ram;
1265 struct dt_device *dev = lfsck_obj2dev(orphan);
1266 struct dt_object *parent = NULL;
1267 struct thandle *th = NULL;
1268 struct lfsck_lock_handle *llh = &info->lti_llh;
1269 struct linkea_data ldata = { NULL };
1270 struct lu_buf linkea_buf;
1271 struct lu_buf lmv_buf;
1279 LASSERT(!dt_object_exists(orphan));
1281 cname->ln_name = NULL;
1282 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1285 if (dt_object_remote(orphan)) {
1286 LASSERT(lfsck->li_lpf_root_obj != NULL);
1288 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1290 GOTO(log, rc = idx);
1292 snprintf(name, 8, "MDT%04x", idx);
1293 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1294 (struct dt_rec *)&tfid,
1295 (const struct dt_key *)name);
1297 GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1299 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1301 GOTO(log, rc = PTR_ERR(parent));
1303 if (unlikely(!dt_try_as_dir(env, parent)))
1304 GOTO(log, rc = -ENOTDIR);
1306 if (unlikely(lfsck->li_lpf_obj == NULL))
1307 GOTO(log, rc = -ENXIO);
1309 parent = lfsck->li_lpf_obj;
1312 dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1314 GOTO(log, rc = PTR_ERR(dev));
1320 namelen = snprintf(name, 31, DFID"-P-%d",
1322 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1323 (const struct dt_key *)name);
1324 if (rc != 0 && rc != -ENOENT)
1328 rc = lfsck_lock(env, lfsck, parent, name, llh,
1329 MDS_INODELOCK_UPDATE, LCK_PW);
1333 /* Re-check whether the name conflict with othrs after taken
1335 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1336 (const struct dt_key *)name);
1337 if (unlikely(rc == 0)) {
1345 cname->ln_name = name;
1346 cname->ln_namelen = namelen;
1348 memset(la, 0, sizeof(*la));
1349 la->la_mode = S_IFDIR | 0700;
1350 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1351 LA_ATIME | LA_MTIME | LA_CTIME;
1353 orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1354 la->la_mode & S_IFMT);
1356 memset(dof, 0, sizeof(*dof));
1357 dof->dof_type = dt_mode_to_dft(S_IFDIR);
1359 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
1360 cname, lfsck_dto2fid(parent));
1364 th = dt_trans_create(env, dev);
1366 GOTO(unlock1, rc = PTR_ERR(th));
1368 /* Sync the remote transaction to guarantee that the subsequent
1369 * lock against the @orphan can find the @orphan in time. */
1370 if (dt_object_remote(orphan))
1373 rc = dt_declare_create(env, orphan, la, hint, dof, th);
1377 if (unlikely(!dt_try_as_dir(env, orphan)))
1378 GOTO(stop, rc = -ENOTDIR);
1380 rc = dt_declare_ref_add(env, orphan, th);
1384 rec->rec_type = S_IFDIR;
1385 rec->rec_fid = cfid;
1386 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1387 (const struct dt_key *)dot, th);
1391 rec->rec_fid = lfsck_dto2fid(parent);
1392 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1393 (const struct dt_key *)dotdot, th);
1398 lmv->lmv_magic = LMV_MAGIC;
1399 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1400 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1401 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1402 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1403 XATTR_NAME_LMV, 0, th);
1408 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1409 ldata.ld_leh->leh_len);
1410 rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1411 XATTR_NAME_LINK, 0, th);
1415 rec->rec_fid = cfid;
1416 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1417 (const struct dt_key *)name, th);
1419 rc = dt_declare_ref_add(env, parent, th);
1424 rc = dt_trans_start_local(env, dev, th);
1428 dt_write_lock(env, orphan, 0);
1429 rc = dt_create(env, orphan, la, hint, dof, th);
1433 rc = dt_ref_add(env, orphan, th);
1437 rec->rec_fid = cfid;
1438 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1439 (const struct dt_key *)dot, th, 1);
1443 rec->rec_fid = lfsck_dto2fid(parent);
1444 rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1445 (const struct dt_key *)dotdot, th, 1);
1450 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1455 rc = dt_xattr_set(env, orphan, &linkea_buf,
1456 XATTR_NAME_LINK, 0, th);
1457 dt_write_unlock(env, orphan);
1461 rec->rec_fid = cfid;
1462 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1463 (const struct dt_key *)name, th, 1);
1465 dt_write_lock(env, parent, 0);
1466 rc = dt_ref_add(env, parent, th);
1467 dt_write_unlock(env, parent);
1470 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1473 dt_write_unlock(env, orphan);
1476 rc1 = dt_trans_stop(env, dev, th);
1477 if (rc1 != 0 && rc > 0)
1484 CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1485 "the object "DFID", name = %s: rc = %d\n",
1486 lfsck_lfsck2name(lfsck), PFID(cfid),
1487 cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1489 if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1490 lfsck_object_put(env, parent);
1493 ns->ln_flags |= LF_INCONSISTENT;
1499 * Remove the specified entry from the linkEA.
1501 * Locate the linkEA entry with the given @cname and @pfid, then
1502 * remove this entry or the other entries those are repeated with
1505 * \param[in] env pointer to the thread context
1506 * \param[in] com pointer to the lfsck component
1507 * \param[in] obj pointer to the dt_object to be handled
1508 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1509 * \param[in] cname the name for the child in the parent directory
1510 * \param[in] pfid the parent directory's FID for the linkEA
1511 * \param[in] next if true, then remove the first found linkEA
1512 * entry, and move the ldata->ld_lee to next entry
1514 * \retval positive number for repaired cases
1515 * \retval 0 if nothing to be repaired
1516 * \retval negative error number on failure
1518 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1519 struct lfsck_component *com,
1520 struct dt_object *obj,
1521 struct linkea_data *ldata,
1522 struct lu_name *cname,
1523 struct lu_fid *pfid,
1526 struct lfsck_instance *lfsck = com->lc_lfsck;
1527 struct dt_device *dev = lfsck_obj2dev(obj);
1528 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
1529 struct thandle *th = NULL;
1530 struct lustre_handle lh = { 0 };
1531 struct linkea_data ldata_new = { NULL };
1532 struct lu_buf linkea_buf;
1537 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1538 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1544 lfsck_linkea_del_buf(ldata, cname);
1546 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1548 if (ldata->ld_leh->leh_reccount > 0 ||
1549 unlikely(ldata->ld_leh->leh_overflow_time)) {
1550 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1551 ldata->ld_leh->leh_len);
1552 buflen = linkea_buf.lb_len;
1556 th = dt_trans_create(env, dev);
1558 GOTO(unlock1, rc = PTR_ERR(th));
1561 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1562 XATTR_NAME_LINK, 0, th);
1564 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
1568 rc = dt_trans_start_local(env, dev, th);
1572 dt_write_lock(env, obj, 0);
1573 if (unlikely(lfsck_is_dead_obj(obj)))
1574 GOTO(unlock2, rc = -ENOENT);
1576 rc = lfsck_links_read2_with_rec(env, obj, &ldata_new);
1578 GOTO(unlock2, rc = (rc == -ENODATA ? 0 : rc));
1580 /* The specified linkEA entry has been removed by race. */
1581 rc = linkea_links_find(&ldata_new, cname, pfid);
1583 GOTO(unlock2, rc = 0);
1585 if (bk->lb_param & LPF_DRYRUN)
1586 GOTO(unlock2, rc = 1);
1589 lfsck_linkea_del_buf(&ldata_new, cname);
1591 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1594 if (buflen < ldata_new.ld_leh->leh_len) {
1595 dt_write_unlock(env, obj);
1596 dt_trans_stop(env, dev, th);
1597 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1598 ldata_new.ld_leh->leh_len);
1602 if (ldata_new.ld_leh->leh_reccount > 0 ||
1603 unlikely(ldata->ld_leh->leh_overflow_time))
1604 rc = lfsck_links_write(env, obj, &ldata_new, th);
1606 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
1608 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1611 dt_write_unlock(env, obj);
1614 dt_trans_stop(env, dev, th);
1617 lfsck_ibits_unlock(&lh, LCK_EX);
1620 CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1621 "for the object: "DFID", parent "DFID", name %.*s\n",
1622 lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1623 PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1627 struct lfsck_namespace *ns = com->lc_file_ram;
1629 ns->ln_flags |= LF_INCONSISTENT;
1636 * Conditionally remove the specified entry from the linkEA.
1638 * Take the parent lock firstly, then check whether the specified
1639 * name entry exists or not: if yes, do nothing; otherwise, call
1640 * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1642 * \param[in] env pointer to the thread context
1643 * \param[in] com pointer to the lfsck component
1644 * \param[in] parent pointer to the parent directory
1645 * \param[in] child pointer to the child object that holds the linkEA
1646 * \param[in,out]ldata pointer to the buffer that holds the linkEA
1647 * \param[in] cname the name for the child in the parent directory
1648 * \param[in] pfid the parent directory's FID for the linkEA
1650 * \retval positive number for repaired cases
1651 * \retval 0 if nothing to be repaired
1652 * \retval negative error number on failure
1654 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1655 struct lfsck_component *com,
1656 struct dt_object *parent,
1657 struct dt_object *child,
1658 struct linkea_data *ldata,
1659 struct lu_name *cname,
1660 struct lu_fid *pfid)
1662 struct lfsck_thread_info *info = lfsck_env_info(env);
1663 struct lu_fid *cfid = &info->lti_fid3;
1664 struct lfsck_lock_handle *llh = &info->lti_llh;
1668 rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1669 MDS_INODELOCK_UPDATE, LCK_PR);
1673 dt_read_lock(env, parent, 0);
1674 if (unlikely(lfsck_is_dead_obj(parent))) {
1675 dt_read_unlock(env, parent);
1677 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1683 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1684 (const struct dt_key *)cname->ln_name);
1685 dt_read_unlock(env, parent);
1687 /* It is safe to release the ldlm lock, because when the logic come
1688 * here, we have got all the needed information above whether the
1689 * linkEA entry is valid or not. It is not important that others
1690 * may add new linkEA entry after the ldlm lock released. If other
1691 * has removed the specified linkEA entry by race, then it is OK,
1692 * because the subsequent lfsck_namespace_shrink_linkea() can handle
1695 if (rc == -ENOENT) {
1696 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1705 /* The LFSCK just found some internal status of cross-MDTs
1706 * create operation. That is normal. */
1707 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1708 linkea_next_entry(ldata);
1713 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1720 * Conditionally replace name entry in the parent.
1722 * As required, the LFSCK may re-create the lost MDT-object for dangling
1723 * name entry, but such repairing may be wrong because of bad FID in the
1724 * name entry. As the LFSCK processing, the real MDT-object may be found,
1725 * then the LFSCK should check whether the former re-created MDT-object
1726 * has been modified or not, if not, then destroy it and update the name
1727 * entry in the parent to reference the real MDT-object.
1729 * \param[in] env pointer to the thread context
1730 * \param[in] com pointer to the lfsck component
1731 * \param[in] parent pointer to the parent directory
1732 * \param[in] child pointer to the MDT-object that may be the real
1733 * MDT-object corresponding to the name entry in parent
1734 * \param[in] cfid the current FID in the name entry
1735 * \param[in] cname contains the name of the child in the parent directory
1737 * \retval positive number for repaired cases
1738 * \retval 0 if nothing to be repaired
1739 * \retval negative error number on failure
1741 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1742 struct lfsck_component *com,
1743 struct dt_object *parent,
1744 struct dt_object *child,
1745 const struct lu_fid *cfid,
1746 const struct lu_name *cname)
1748 struct lfsck_thread_info *info = lfsck_env_info(env);
1749 struct lu_attr *la = &info->lti_la;
1750 struct dt_insert_rec *rec = &info->lti_dt_rec;
1752 struct lfsck_instance *lfsck = com->lc_lfsck;
1753 /* The child and its name may be on different MDTs. */
1754 struct dt_device *dev = lfsck->li_next;
1755 const char *name = cname->ln_name;
1756 const struct lu_fid *pfid = lfsck_dto2fid(parent);
1757 struct dt_object *cobj = NULL;
1758 struct lfsck_lock_handle *pllh = &info->lti_llh;
1759 struct lustre_handle clh = { 0 };
1760 struct linkea_data ldata = { NULL };
1761 struct thandle *th = NULL;
1766 /* @parent/@child may be based on lfsck->li_bottom,
1767 * but here we need the object based on the lfsck->li_next. */
1769 parent = lfsck_object_locate(dev, parent);
1771 GOTO(log, rc = PTR_ERR(parent));
1773 if (unlikely(!dt_try_as_dir(env, parent)))
1774 GOTO(log, rc = -ENOTDIR);
1776 rc = lfsck_lock(env, lfsck, parent, name, pllh,
1777 MDS_INODELOCK_UPDATE, LCK_PW);
1781 if (!fid_is_sane(cfid)) {
1786 cobj = lfsck_object_find_by_dev(env, dev, cfid);
1789 if (rc == -ENOENT) {
1797 if (!dt_object_exists(cobj)) {
1802 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1803 (const struct dt_key *)name);
1804 if (rc == -ENOENT) {
1812 /* Someone changed the name entry, cannot replace it. */
1813 if (!lu_fid_eq(cfid, &tfid))
1816 /* lock the object to be destroyed. */
1817 rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1818 MDS_INODELOCK_UPDATE |
1819 MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1824 if (unlikely(lfsck_is_dead_obj(cobj))) {
1829 rc = dt_attr_get(env, cobj, la);
1833 /* The object has been modified by other(s), or it is not created by
1834 * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1835 if (la->la_ctime != 0)
1838 if (S_ISREG(la->la_mode)) {
1839 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
1840 /* If someone has created related OST-object(s),
1842 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1843 GOTO(log, rc = (rc > 0 ? 0 : rc));
1847 dt_read_lock(env, child, 0);
1848 rc = lfsck_links_read2_with_rec(env, child, &ldata);
1849 dt_read_unlock(env, child);
1851 /* Someone changed the child, no need to replace. */
1858 rc = linkea_links_find(&ldata, cname, pfid);
1859 /* Someone moved the child, no need to replace. */
1863 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1866 th = dt_trans_create(env, dev);
1868 GOTO(log, rc = PTR_ERR(th));
1871 rc = dt_declare_destroy(env, cobj, th);
1876 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
1880 rec->rec_type = S_IFDIR;
1881 rec->rec_fid = lfsck_dto2fid(child);
1882 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1883 (const struct dt_key *)name, th);
1887 rc = dt_trans_start_local(env, dev, th);
1892 rc = dt_destroy(env, cobj, th);
1897 /* The old name entry maybe not exist. */
1898 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
1899 if (rc != 0 && rc != -ENOENT)
1902 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1903 (const struct dt_key *)name, th, 1);
1905 GOTO(stop, rc = (rc == 0 ? 1 : rc));
1908 dt_trans_stop(env, dev, th);
1911 lfsck_ibits_unlock(&clh, LCK_EX);
1914 if (cobj != NULL && !IS_ERR(cobj))
1915 lfsck_object_put(env, cobj);
1917 CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1918 "object "DFID" because of conflict with the object "DFID
1919 " under the parent "DFID" with name %s: rc = %d\n",
1920 lfsck_lfsck2name(lfsck), PFID(cfid),
1921 PFID(lfsck_dto2fid(child)), PFID(pfid), name, rc);
1927 * Overwrite the linkEA for the object with the given ldata.
1929 * The caller should take the ldlm lock before the calling.
1931 * \param[in] env pointer to the thread context
1932 * \param[in] com pointer to the lfsck component
1933 * \param[in] obj pointer to the dt_object to be handled
1934 * \param[in] ldata pointer to the new linkEA data
1936 * \retval positive number for repaired cases
1937 * \retval 0 if nothing to be repaired
1938 * \retval negative error number on failure
1940 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1941 struct lfsck_component *com,
1942 struct dt_object *obj,
1943 struct linkea_data *ldata)
1945 struct lfsck_instance *lfsck = com->lc_lfsck;
1946 struct dt_device *dev = lfsck_obj2dev(obj);
1947 struct thandle *th = NULL;
1948 struct lu_buf linkea_buf;
1952 th = dt_trans_create(env, dev);
1954 GOTO(log, rc = PTR_ERR(th));
1956 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1957 ldata->ld_leh->leh_len);
1958 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1959 XATTR_NAME_LINK, 0, th);
1963 rc = dt_trans_start_local(env, dev, th);
1967 dt_write_lock(env, obj, 0);
1968 if (unlikely(lfsck_is_dead_obj(obj)))
1969 GOTO(unlock, rc = 0);
1971 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1972 GOTO(unlock, rc = 1);
1974 rc = dt_xattr_set(env, obj, &linkea_buf,
1975 XATTR_NAME_LINK, 0, th);
1977 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1980 dt_write_unlock(env, obj);
1983 dt_trans_stop(env, dev, th);
1986 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1987 "object "DFID": rc = %d\n",
1988 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1991 struct lfsck_namespace *ns = com->lc_file_ram;
1993 ns->ln_flags |= LF_INCONSISTENT;
2000 * Repair invalid name entry.
2002 * If the name entry contains invalid information, such as bad file type
2003 * or (and) corrupted object FID, then either remove the name entry or
2004 * udpate the name entry with the given (right) information.
2006 * \param[in] env pointer to the thread context
2007 * \param[in] com pointer to the lfsck component
2008 * \param[in] parent pointer to the parent directory
2009 * \param[in] child pointer to the object referenced by the name entry
2010 * \param[in] name the old name of the child under the parent directory
2011 * \param[in] name2 the new name of the child under the parent directory
2012 * \param[in] type the type claimed by the name entry
2013 * \param[in] update update the name entry if true; otherwise, remove it
2014 * \param[in] dec decrease the parent nlink count if true
2016 * \retval positive number for repaired successfully
2017 * \retval 0 if nothing to be repaired
2018 * \retval negative error number on failure
2020 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2021 struct lfsck_component *com,
2022 struct dt_object *parent,
2023 struct dt_object *child,
2024 const char *name, const char *name2,
2025 __u16 type, bool update, bool dec)
2027 struct lfsck_thread_info *info = lfsck_env_info(env);
2028 struct dt_insert_rec *rec = &info->lti_dt_rec;
2029 const struct lu_fid *pfid = lfsck_dto2fid(parent);
2030 const struct lu_fid *cfid = lfsck_dto2fid(child);
2032 struct lfsck_instance *lfsck = com->lc_lfsck;
2033 struct dt_device *dev = lfsck->li_next;
2034 struct thandle *th = NULL;
2035 struct lfsck_lock_handle *llh = &info->lti_llh;
2036 struct lustre_handle lh = { 0 };
2040 parent = lfsck_object_locate(dev, parent);
2042 GOTO(log, rc = PTR_ERR(parent));
2044 if (unlikely(!dt_try_as_dir(env, parent)))
2045 GOTO(log, rc = -ENOTDIR);
2047 if (!update || strcmp(name, name2) == 0)
2048 rc = lfsck_lock(env, lfsck, parent, name, llh,
2049 MDS_INODELOCK_UPDATE, LCK_PW);
2051 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2052 MDS_INODELOCK_UPDATE, LCK_PW);
2056 th = dt_trans_create(env, dev);
2058 GOTO(unlock1, rc = PTR_ERR(th));
2060 rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2065 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2066 rec->rec_fid = cfid;
2067 rc = dt_declare_insert(env, parent,
2068 (const struct dt_rec *)rec,
2069 (const struct dt_key *)name2, th);
2074 if (dec && S_ISDIR(type)) {
2075 rc = dt_declare_ref_del(env, parent, th);
2080 rc = dt_trans_start_local(env, dev, th);
2085 dt_write_lock(env, parent, 0);
2086 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2087 (const struct dt_key *)name);
2088 /* Someone has removed the bad name entry by race. */
2090 GOTO(unlock2, rc = 0);
2095 /* Someone has removed the bad name entry and reused it for other
2096 * object by race. */
2097 if (!lu_fid_eq(&tfid, cfid))
2098 GOTO(unlock2, rc = 0);
2100 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2101 GOTO(unlock2, rc = 1);
2103 rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2108 rc = dt_insert(env, parent,
2109 (const struct dt_rec *)rec,
2110 (const struct dt_key *)name2, th, 1);
2115 if (dec && S_ISDIR(type)) {
2116 rc = dt_ref_del(env, parent, th);
2121 GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2124 dt_write_unlock(env, parent);
2127 dt_trans_stop(env, dev, th);
2129 /* We are not sure whether the child will become orphan or not.
2130 * Record it in the LFSCK trace file for further checking in
2131 * the second-stage scanning. */
2132 if (!update && !dec && rc == 0)
2133 lfsck_namespace_trace_update(env, com, cfid,
2134 LNTF_CHECK_LINKEA, true);
2137 /* It is harmless even if unlock the unused lock_handle */
2138 lfsck_ibits_unlock(&lh, LCK_PW);
2142 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2143 "entry for: parent "DFID", child "DFID", name %s, type "
2144 "in name entry %o, type claimed by child %o. repair it "
2145 "by %s with new name2 %s: rc = %d\n",
2146 lfsck_lfsck2name(lfsck), PFID(pfid), PFID(cfid),
2147 name, type, update ? lfsck_object_type(child) : 0,
2148 update ? "updating" : "removing", name2, rc);
2151 struct lfsck_namespace *ns = com->lc_file_ram;
2153 ns->ln_flags |= LF_INCONSISTENT;
2160 * Update the ".." name entry for the given object.
2162 * The object's ".." is corrupted, this function will update the ".." name
2163 * entry with the given pfid, and the linkEA with the given ldata.
2165 * The caller should take the ldlm lock before the calling.
2167 * \param[in] env pointer to the thread context
2168 * \param[in] com pointer to the lfsck component
2169 * \param[in] obj pointer to the dt_object to be handled
2170 * \param[in] pfid the new fid for the object's ".." name entry
2171 * \param[in] cname the name for the @obj in the parent directory
2173 * \retval positive number for repaired cases
2174 * \retval 0 if nothing to be repaired
2175 * \retval negative error number on failure
2177 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2178 struct lfsck_component *com,
2179 struct dt_object *obj,
2180 const struct lu_fid *pfid,
2181 struct lu_name *cname)
2183 struct lfsck_thread_info *info = lfsck_env_info(env);
2184 struct dt_insert_rec *rec = &info->lti_dt_rec;
2185 struct lfsck_instance *lfsck = com->lc_lfsck;
2186 struct dt_device *dev = lfsck_obj2dev(obj);
2187 struct thandle *th = NULL;
2188 struct linkea_data ldata = { NULL };
2189 struct lu_buf linkea_buf;
2193 LASSERT(!dt_object_remote(obj));
2194 LASSERT(S_ISDIR(lfsck_object_type(obj)));
2196 rc = linkea_links_new(&ldata, &info->lti_big_buf, cname, pfid);
2200 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2201 ldata.ld_leh->leh_len);
2203 th = dt_trans_create(env, dev);
2205 GOTO(log, rc = PTR_ERR(th));
2207 rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2211 rec->rec_type = S_IFDIR;
2212 rec->rec_fid = pfid;
2213 rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2214 (const struct dt_key *)dotdot, th);
2218 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2219 XATTR_NAME_LINK, 0, th);
2223 rc = dt_trans_start_local(env, dev, th);
2227 dt_write_lock(env, obj, 0);
2228 if (unlikely(lfsck_is_dead_obj(obj)))
2229 GOTO(unlock, rc = 0);
2231 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2232 GOTO(unlock, rc = 1);
2234 /* The old ".." name entry maybe not exist. */
2235 dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2237 rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2238 (const struct dt_key *)dotdot, th, 1);
2242 rc = lfsck_links_write(env, obj, &ldata, th);
2244 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2247 dt_write_unlock(env, obj);
2250 dt_trans_stop(env, dev, th);
2253 CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2254 "the object "DFID", new parent "DFID": rc = %d\n",
2255 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2259 struct lfsck_namespace *ns = com->lc_file_ram;
2261 ns->ln_flags |= LF_INCONSISTENT;
2268 * Handle orphan @obj during Double Scan Directory.
2270 * Remove the @obj's current (invalid) linkEA entries, and insert
2271 * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2272 * ${FID}-${PFID}-D-${conflict_version}
2274 * The caller should take the ldlm lock before the calling.
2276 * \param[in] env pointer to the thread context
2277 * \param[in] com pointer to the lfsck component
2278 * \param[in] obj pointer to the orphan object to be handled
2279 * \param[in] pfid the new fid for the object's ".." name entry
2280 * \param[in,out] lh ldlm lock handler for the given @obj
2281 * \param[out] type to tell the caller what the inconsistency is
2283 * \retval positive number for repaired cases
2284 * \retval 0 if nothing to be repaired
2285 * \retval negative error number on failure
2288 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2289 struct lfsck_component *com,
2290 struct dt_object *obj,
2291 const struct lu_fid *pfid,
2292 struct lustre_handle *lh,
2293 enum lfsck_namespace_inconsistency_type *type)
2295 struct lfsck_thread_info *info = lfsck_env_info(env);
2296 struct lfsck_namespace *ns = com->lc_file_ram;
2300 /* Remove the unrecognized linkEA. */
2301 rc = lfsck_namespace_links_remove(env, com, obj);
2302 lfsck_ibits_unlock(lh, LCK_EX);
2303 if (rc < 0 && rc != -ENODATA)
2306 *type = LNIT_MUL_REF;
2308 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2309 * ever tried to verify some remote MDT-object that resides on this
2310 * MDT, but this MDT failed to respond such request. So means there
2311 * may be some remote name entry on other MDT that references this
2312 * object with another name, so we cannot know whether this linkEA
2313 * is valid or not. So keep it there and maybe resolved when next
2315 if (ns->ln_flags & LF_INCOMPLETE)
2318 /* The unique linkEA is invalid, even if the ".." name entry may be
2319 * valid, we still cannot know via which name entry this directory
2320 * will be referenced. Then handle it as pure orphan. */
2321 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2322 "-"DFID, PFID(pfid));
2323 rc = lfsck_namespace_insert_orphan(env, com, obj,
2324 info->lti_tmpbuf, "D", NULL);
2330 * Double Scan Directory object for single linkEA entry case.
2332 * The given @child has unique linkEA entry. If the linkEA entry is valid,
2333 * then check whether the name is in the namespace or not, if not, add the
2334 * missing name entry back to namespace. If the linkEA entry is invalid,
2335 * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2338 * \param[in] env pointer to the thread context
2339 * \param[in] com pointer to the lfsck component
2340 * \param[in] child pointer to the directory to be double scanned
2341 * \param[in] pfid the FID corresponding to the ".." entry
2342 * \param[in] ldata pointer to the linkEA data for the given @child
2343 * \param[in,out] lh ldlm lock handler for the given @child
2344 * \param[out] type to tell the caller what the inconsistency is
2345 * \param[in] retry if found inconsistency, but the caller does not hold
2346 * ldlm lock on the @child, then set @retry as true
2347 * \param[in] unknown set if does not know how to repair the inconsistency
2349 * \retval positive number for repaired cases
2350 * \retval 0 if nothing to be repaired
2351 * \retval negative error number on failure
2354 lfsck_namespace_dsd_single(const struct lu_env *env,
2355 struct lfsck_component *com,
2356 struct dt_object *child,
2357 const struct lu_fid *pfid,
2358 struct linkea_data *ldata,
2359 struct lustre_handle *lh,
2360 enum lfsck_namespace_inconsistency_type *type,
2361 bool *retry, bool *unknown)
2363 struct lfsck_thread_info *info = lfsck_env_info(env);
2364 struct lu_name *cname = &info->lti_name;
2365 const struct lu_fid *cfid = lfsck_dto2fid(child);
2367 struct lfsck_namespace *ns = com->lc_file_ram;
2368 struct lfsck_instance *lfsck = com->lc_lfsck;
2369 struct dt_object *parent = NULL;
2370 struct lmv_mds_md_v1 *lmv;
2374 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2376 sizeof(info->lti_key));
2377 /* The unique linkEA entry with bad parent will be handled as orphan. */
2379 if (!lustre_handle_is_used(lh) && retry != NULL)
2382 rc = lfsck_namespace_dsd_orphan(env, com, child,
2388 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2390 GOTO(out, rc = PTR_ERR(parent));
2392 /* We trust the unique linkEA entry in spite of whether it matches the
2393 * ".." name entry or not. Because even if the linkEA entry is wrong
2394 * and the ".." name entry is right, we still cannot know via which
2395 * name entry the child will be referenced, since all known entries
2396 * have been verified during the first-stage scanning. */
2397 if (!dt_object_exists(parent)) {
2398 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2399 * has ever tried to verify some remote MDT-object that resides
2400 * on this MDT, but this MDT failed to respond such request. So
2401 * means there may be some remote name entry on other MDT that
2402 * references this object with another name, so we cannot know
2403 * whether this linkEA is valid or not. So keep it there and
2404 * maybe resolved when next LFSCK run. */
2405 if (ns->ln_flags & LF_INCOMPLETE)
2408 if (!lustre_handle_is_used(lh) && retry != NULL) {
2414 lfsck_ibits_unlock(lh, LCK_EX);
2417 lmv = &info->lti_lmv;
2418 rc = lfsck_read_stripe_lmv(env, child, lmv);
2419 if (rc != 0 && rc != -ENODATA)
2422 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2424 } else if (lfsck_shard_name_to_index(env,
2425 cname->ln_name, cname->ln_namelen,
2426 S_IFDIR, cfid) < 0) {
2427 /* It is an invalid name entry, we
2428 * cannot trust the parent also. */
2429 rc = lfsck_namespace_shrink_linkea(env, com, child,
2430 ldata, cname, &tfid, true);
2434 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2435 "-"DFID, PFID(pfid));
2436 rc = lfsck_namespace_insert_orphan(env, com, child,
2437 info->lti_tmpbuf, "S", NULL);
2442 /* Create the lost parent as an orphan. */
2443 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2445 /* Add the missing name entry to the parent. */
2446 rc = lfsck_namespace_insert_normal(env, com, parent,
2447 child, cname->ln_name);
2448 if (unlikely(rc == -EEXIST)) {
2449 /* Unfortunately, someone reused the name
2450 * under the parent by race. So we have
2451 * to remove the linkEA entry from
2452 * current child object. It means that the
2453 * LFSCK cannot recover the system
2454 * totally back to its original status,
2455 * but it is necessary to make the
2456 * current system to be consistent. */
2457 rc = lfsck_namespace_shrink_linkea(env,
2459 cname, &tfid, true);
2461 snprintf(info->lti_tmpbuf,
2462 sizeof(info->lti_tmpbuf),
2463 "-"DFID, PFID(pfid));
2464 rc = lfsck_namespace_insert_orphan(env,
2465 com, child, info->lti_tmpbuf,
2472 } /* !dt_object_exists(parent) */
2474 /* The unique linkEA entry with bad parent will be handled as orphan. */
2475 if (unlikely(!dt_try_as_dir(env, parent))) {
2476 if (!lustre_handle_is_used(lh) && retry != NULL)
2479 rc = lfsck_namespace_dsd_orphan(env, com, child,
2485 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2486 (const struct dt_key *)cname->ln_name);
2487 if (rc == -ENOENT) {
2488 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2489 * has ever tried to verify some remote MDT-object that resides
2490 * on this MDT, but this MDT failed to respond such request. So
2491 * means there may be some remote name entry on other MDT that
2492 * references this object with another name, so we cannot know
2493 * whether this linkEA is valid or not. So keep it there and
2494 * maybe resolved when next LFSCK run. */
2495 if (ns->ln_flags & LF_INCOMPLETE)
2498 if (!lustre_handle_is_used(lh) && retry != NULL) {
2504 lfsck_ibits_unlock(lh, LCK_EX);
2505 rc = lfsck_namespace_check_name(env, parent, child, cname);
2512 /* It is an invalid name entry, drop it. */
2513 if (unlikely(rc > 0)) {
2514 rc = lfsck_namespace_shrink_linkea(env, com, child,
2515 ldata, cname, &tfid, true);
2517 snprintf(info->lti_tmpbuf,
2518 sizeof(info->lti_tmpbuf),
2519 "-"DFID, PFID(pfid));
2520 rc = lfsck_namespace_insert_orphan(env, com,
2521 child, info->lti_tmpbuf, "D", NULL);
2527 /* Add the missing name entry back to the namespace. */
2528 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2530 if (unlikely(rc == -ESTALE))
2531 /* It may happen when the remote object has been
2532 * removed, but the local MDT is not aware of that. */
2535 if (unlikely(rc == -EEXIST)) {
2536 /* Unfortunately, someone reused the name under the
2537 * parent by race. So we have to remove the linkEA
2538 * entry from current child object. It means that the
2539 * LFSCK cannot recover the system totally back to
2540 * its original status, but it is necessary to make
2541 * the current system to be consistent.
2543 * It also may be because of the LFSCK found some
2544 * internal status of create operation. Under such
2545 * case, nothing to be done. */
2546 rc = lfsck_namespace_shrink_linkea_cond(env, com,
2547 parent, child, ldata, cname, &tfid);
2549 snprintf(info->lti_tmpbuf,
2550 sizeof(info->lti_tmpbuf),
2551 "-"DFID, PFID(pfid));
2552 rc = lfsck_namespace_insert_orphan(env, com,
2553 child, info->lti_tmpbuf, "D", NULL);
2558 } /* rc == -ENOENT */
2563 if (!lu_fid_eq(&tfid, cfid)) {
2564 if (!lustre_handle_is_used(lh) && retry != NULL) {
2570 lfsck_ibits_unlock(lh, LCK_EX);
2571 /* The name entry references another MDT-object that
2572 * may be created by the LFSCK for repairing dangling
2573 * name entry. Try to replace it. */
2574 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2577 rc = lfsck_namespace_dsd_orphan(env, com, child,
2583 /* Zero FID may because the remote directroy object has invalid linkEA,
2584 * or lost linkEA. Under such case, the LFSCK on this MDT does not know
2585 * how to repair the inconsistency, but the namespace LFSCK on the MDT
2586 * where its name entry resides may has more information (name, FID) to
2587 * repair such inconsistency. So here, keep the inconsistency to avoid
2588 * some imporper repairing. */
2589 if (fid_is_zero(pfid)) {
2596 /* The ".." name entry is wrong, update it. */
2597 if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2598 if (!lustre_handle_is_used(lh) && retry != NULL) {
2604 *type = LNIT_UNMATCHED_PAIRS;
2605 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2606 lfsck_dto2fid(parent), cname);
2612 if (parent != NULL && !IS_ERR(parent))
2613 lfsck_object_put(env, parent);
2619 * Double Scan Directory object for multiple linkEA entries case.
2621 * The given @child has multiple linkEA entries. There is at most one linkEA
2622 * entry will be valid, all the others will be removed. Firstly, the function
2623 * will try to find out the linkEA entry for which the name entry exists under
2624 * the given parent (@pfid). If there is no linkEA entry that matches the given
2625 * ".." name entry, then tries to find out the first linkEA entry that both the
2626 * parent and the name entry exist to rebuild a new ".." name entry.
2628 * \param[in] env pointer to the thread context
2629 * \param[in] com pointer to the lfsck component
2630 * \param[in] child pointer to the directory to be double scanned
2631 * \param[in] pfid the FID corresponding to the ".." entry
2632 * \param[in] ldata pointer to the linkEA data for the given @child
2633 * \param[in,out] lh ldlm lock handler for the given @child
2634 * \param[out] type to tell the caller what the inconsistency is
2635 * \param[in] lpf true if the ".." entry is under lost+found/MDTxxxx/
2636 * \param[in] unknown set if does not know how to repair the inconsistency
2638 * \retval positive number for repaired cases
2639 * \retval 0 if nothing to be repaired
2640 * \retval negative error number on failure
2643 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2644 struct lfsck_component *com,
2645 struct dt_object *child,
2646 const struct lu_fid *pfid,
2647 struct linkea_data *ldata,
2648 struct lustre_handle *lh,
2649 enum lfsck_namespace_inconsistency_type *type,
2650 bool lpf, bool *unknown)
2652 struct lfsck_thread_info *info = lfsck_env_info(env);
2653 struct lu_name *cname = &info->lti_name;
2654 const struct lu_fid *cfid = lfsck_dto2fid(child);
2655 struct lu_fid *pfid2 = &info->lti_fid3;
2657 struct lfsck_namespace *ns = com->lc_file_ram;
2658 struct lfsck_instance *lfsck = com->lc_lfsck;
2659 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
2660 struct dt_object *parent = NULL;
2661 struct linkea_data ldata_new = { NULL };
2662 int dirent_count = 0;
2668 while (ldata->ld_lee != NULL) {
2669 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2671 sizeof(info->lti_key));
2672 /* Drop invalid linkEA entry. */
2674 lfsck_linkea_del_buf(ldata, cname);
2678 /* Drop repeated linkEA entries. */
2679 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2681 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2682 * then it is possible that: the directry object has ever
2683 * been lost, but its name entry was there. In the former
2684 * LFSCK run, during the first-stage scanning, the LFSCK
2685 * found the dangling name entry, but it did not recreate
2686 * the lost object, and when moved to the second-stage
2687 * scanning, some children objects of the lost directory
2688 * object were found, then the LFSCK recreated such lost
2689 * directory object as an orphan.
2691 * When the LFSCK runs again, if the dangling name is still
2692 * there, the LFSCK should move the orphan directory object
2693 * back to the normal namespace. */
2694 if (!lpf && !fid_is_zero(pfid) &&
2695 !lu_fid_eq(pfid, &tfid) && once) {
2696 linkea_next_entry(ldata);
2700 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2702 RETURN(PTR_ERR(parent));
2704 if (!dt_object_exists(parent)) {
2705 lfsck_object_put(env, parent);
2706 if (ldata->ld_leh->leh_reccount > 1) {
2707 /* If it is NOT the last linkEA entry, then
2708 * there is still other chance to make the
2709 * child to be visible via other parent, then
2710 * remove this linkEA entry. */
2711 lfsck_linkea_del_buf(ldata, cname);
2718 /* The linkEA entry with bad parent will be removed. */
2719 if (unlikely(!dt_try_as_dir(env, parent))) {
2720 lfsck_object_put(env, parent);
2721 lfsck_linkea_del_buf(ldata, cname);
2725 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2726 (const struct dt_key *)cname->ln_name);
2727 *pfid2 = *lfsck_dto2fid(parent);
2728 if (rc == -ENOENT) {
2729 lfsck_object_put(env, parent);
2730 linkea_next_entry(ldata);
2735 lfsck_object_put(env, parent);
2740 if (lu_fid_eq(&tfid, cfid)) {
2741 lfsck_object_put(env, parent);
2742 /* If the parent (that is declared via linkEA entry)
2743 * directory contains the specified child, but such
2744 * parent does not match the dotdot name entry, then
2745 * trust the linkEA. */
2746 if (!fid_is_zero(pfid) && !lu_fid_eq(pfid, pfid2)) {
2747 *type = LNIT_UNMATCHED_PAIRS;
2748 rc = lfsck_namespace_repair_unmatched_pairs(env,
2749 com, child, pfid2, cname);
2755 /* It is the most common case that we find the
2756 * name entry corresponding to the linkEA entry
2757 * that matches the ".." name entry. */
2758 rc = linkea_links_new(&ldata_new, &info->lti_big_buf,
2763 rc = lfsck_namespace_rebuild_linkea(env, com, child,
2768 lfsck_linkea_del_buf(ldata, cname);
2769 linkea_first_entry(ldata);
2770 /* There may be some invalid dangling name entries under
2771 * other parent directories, remove all of them. */
2772 while (ldata->ld_lee != NULL) {
2773 rc = lfsck_namespace_unpack_linkea_entry(ldata,
2774 cname, &tfid, info->lti_key,
2775 sizeof(info->lti_key));
2779 parent = lfsck_object_find_bottom(env, lfsck,
2781 if (IS_ERR(parent)) {
2782 rc = PTR_ERR(parent);
2783 if (rc != -ENOENT &&
2784 bk->lb_param & LPF_FAILOUT)
2790 if (!dt_object_exists(parent)) {
2791 lfsck_object_put(env, parent);
2795 rc = lfsck_namespace_repair_dirent(env, com,
2796 parent, child, cname->ln_name,
2797 cname->ln_name, S_IFDIR, false, true);
2798 lfsck_object_put(env, parent);
2800 if (bk->lb_param & LPF_FAILOUT)
2809 lfsck_linkea_del_buf(ldata, cname);
2812 ns->ln_dirent_repaired += dirent_count;
2815 } /* lu_fid_eq(&tfid, lfsck_dto2fid(child)) */
2817 lfsck_ibits_unlock(lh, LCK_EX);
2818 /* The name entry references another MDT-object that may be
2819 * created by the LFSCK for repairing dangling name entry.
2820 * Try to replace it. */
2821 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2823 lfsck_object_put(env, parent);
2830 lfsck_linkea_del_buf(ldata, cname);
2831 } /* while (ldata->ld_lee != NULL) */
2833 /* If there is still linkEA overflow, return. */
2834 if (unlikely(ldata->ld_leh->leh_overflow_time))
2837 linkea_first_entry(ldata);
2838 if (ldata->ld_leh->leh_reccount == 1) {
2839 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2840 lh, type, NULL, unknown);
2845 /* All linkEA entries are invalid and removed, then handle the @child
2847 if (ldata->ld_leh->leh_reccount == 0) {
2848 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2854 /* If the dangling name entry for the orphan directory object has
2855 * been remvoed, then just check whether the directory object is
2856 * still under the .lustre/lost+found/MDTxxxx/ or not. */
2862 /* There is no linkEA entry that matches the ".." name entry. Find
2863 * the first linkEA entry that both parent and name entry exist to
2864 * rebuild a new ".." name entry. */
2874 * Repair the object's nlink attribute.
2876 * If all the known name entries have been verified, then the object's hard
2877 * link attribute should match the object's linkEA entries count unless the
2878 * object's has too many hard link to be recorded in the linkEA. Such cases
2879 * should have been marked in the LFSCK trace file. Otherwise, trust the
2880 * linkEA to update the object's nlink attribute.
2882 * \param[in] env pointer to the thread context
2883 * \param[in] com pointer to the lfsck component
2884 * \param[in] obj pointer to the dt_object to be handled
2885 * \param[in,out] la pointer to buffer to object's attribute before
2886 * and after the repairing
2888 * \retval positive number for repaired cases
2889 * \retval 0 if nothing to be repaired
2890 * \retval negative error number on failure
2892 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2893 struct lfsck_component *com,
2894 struct dt_object *obj,
2897 struct lfsck_namespace *ns = com->lc_file_ram;
2898 struct lfsck_instance *lfsck = com->lc_lfsck;
2899 struct dt_device *dev = lfsck_obj2dev(obj);
2900 const struct lu_fid *cfid = lfsck_dto2fid(obj);
2901 struct thandle *th = NULL;
2902 struct linkea_data ldata = { NULL };
2903 struct lustre_handle lh = { 0 };
2904 __u32 old = la->la_nlink;
2908 LASSERT(!dt_object_remote(obj));
2910 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2911 MDS_INODELOCK_UPDATE, LCK_PW);
2915 th = dt_trans_create(env, dev);
2917 GOTO(log, rc = PTR_ERR(th));
2919 la->la_valid = LA_NLINK;
2920 rc = dt_declare_attr_set(env, obj, la, th);
2924 rc = dt_trans_start_local(env, dev, th);
2928 dt_write_lock(env, obj, 0);
2929 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2930 * ever tried to verify some remote MDT-object that resides on this
2931 * MDT, but this MDT failed to respond such request. So means there
2932 * may be some remote name entry on other MDT that references this
2933 * object with another name, so we cannot know whether this linkEA
2934 * is valid or not. So keep it there and maybe resolved when next
2936 if (ns->ln_flags & LF_INCOMPLETE)
2937 GOTO(unlock, rc = 0);
2939 rc = dt_attr_get(env, obj, la);
2941 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2943 rc = lfsck_links_read2_with_rec(env, obj, &ldata);
2945 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2947 /* XXX: Currently, we only update the nlink attribute if the known
2948 * linkEA entries is larger than the nlink attribute. That is
2950 if (la->la_nlink >= ldata.ld_leh->leh_reccount ||
2951 unlikely(la->la_nlink == 0 ||
2952 ldata.ld_leh->leh_overflow_time))
2953 GOTO(unlock, rc = 0);
2955 la->la_nlink = ldata.ld_leh->leh_reccount;
2956 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2957 GOTO(unlock, rc = 1);
2959 rc = dt_attr_set(env, obj, la, th);
2961 GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2964 dt_write_unlock(env, obj);
2967 dt_trans_stop(env, dev, th);
2970 lfsck_ibits_unlock(&lh, LCK_PW);
2971 CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2972 "nlink count from %u to %u: rc = %d\n",
2973 lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2976 ns->ln_flags |= LF_INCONSISTENT;
2982 * Double scan the directory object for namespace LFSCK.
2984 * This function will verify the <parent, child> pairs in the namespace tree:
2985 * the parent references the child via some name entry that should be in the
2986 * child's linkEA entry, the child should back references the parent via its
2989 * The LFSCK will scan every linkEA entry in turn until find out the first
2990 * matched pairs. If found, then all other linkEA entries will be dropped.
2991 * If all the linkEA entries cannot match the ".." name entry, then there
2992 * are serveral possible cases:
2994 * 1) If there is only one linkEA entry, then trust it as long as the PFID
2995 * in the linkEA entry is valid.
2997 * 2) If there are multiple linkEA entries, then try to find the linkEA
2998 * that matches the ".." name entry. If found, then all other entries
2999 * are invalid; otherwise, it is quite possible that the ".." name entry
3000 * is corrupted. Under such case, the LFSCK will rebuild the ".." name
3001 * entry according to the first valid linkEA entry (both the parent and
3002 * the name entry should exist).
3004 * 3) If the directory object has no (valid) linkEA entry, then the
3005 * directory object will be handled as pure orphan and inserted
3006 * in the .lustre/lost+found/MDTxxxx/ with the name:
3007 * ${self_FID}-${PFID}-D-${conflict_version}
3009 * \param[in] env pointer to the thread context
3010 * \param[in] com pointer to the lfsck component
3011 * \param[in] child pointer to the directory object to be handled
3012 * \param[in] flags to indicate the specical checking on the @child
3014 * \retval positive number for repaired cases
3015 * \retval 0 if nothing to be repaired
3016 * \retval negative error number on failure
3018 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3019 struct lfsck_component *com,
3020 struct dt_object *child, __u8 flags)
3022 struct lfsck_thread_info *info = lfsck_env_info(env);
3023 const struct lu_fid *cfid = lfsck_dto2fid(child);
3024 struct lu_fid *pfid = &info->lti_fid2;
3025 struct lfsck_namespace *ns = com->lc_file_ram;
3026 struct lfsck_instance *lfsck = com->lc_lfsck;
3027 struct lustre_handle lh = { 0 };
3028 struct linkea_data ldata = { NULL };
3029 bool unknown = false;
3032 enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3036 LASSERT(!dt_object_remote(child));
3038 if (flags & LNTF_UNCERTAIN_LMV) {
3039 if (flags & LNTF_RECHECK_NAME_HASH) {
3040 rc = lfsck_namespace_scan_shard(env, com, child);
3044 ns->ln_striped_shards_scanned++;
3046 ns->ln_striped_shards_skipped++;
3050 flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3054 if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3055 !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3056 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3057 "the namespace LFSCK, then the LFSCK cannot guarantee"
3058 "all the name entries have been verified in first-stage"
3059 "scanning. So have to skip orphan related handling for"
3060 "the directory object "DFID" with remote name entry\n",
3061 lfsck_lfsck2name(lfsck), PFID(cfid));
3066 if (unlikely(!dt_try_as_dir(env, child)))
3067 GOTO(out, rc = -ENOTDIR);
3069 /* We only take ldlm lock on the @child when required. When the
3070 * logic comes here for the first time, it is always false. */
3074 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3075 MDS_INODELOCK_UPDATE |
3076 MDS_INODELOCK_XATTR, LCK_EX);
3081 dt_read_lock(env, child, 0);
3082 if (unlikely(lfsck_is_dead_obj(child))) {
3083 dt_read_unlock(env, child);
3088 rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3089 (const struct dt_key *)dotdot);
3091 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3092 dt_read_unlock(env, child);
3097 if (!lustre_handle_is_used(&lh)) {
3098 dt_read_unlock(env, child);
3103 } else if (lfsck->li_lpf_obj != NULL &&
3104 lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3106 } else if (unlikely(!fid_is_sane(pfid))) {
3110 rc = lfsck_links_read(env, child, &ldata);
3111 dt_read_unlock(env, child);
3113 if (rc != -ENODATA && rc != -EINVAL)
3116 if (!lustre_handle_is_used(&lh))
3119 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3120 /* Remove the corrupted linkEA. */
3121 rc = lfsck_namespace_links_remove(env, com, child);
3123 /* Here, because of the crashed linkEA, we
3124 * cannot know whether there is some parent
3125 * that references the child directory via
3126 * some name entry or not. So keep it there,
3127 * when the LFSCK run next time, if there is
3128 * some parent that references this object,
3129 * then the LFSCK can rebuild the linkEA;
3130 * otherwise, this object will be handled
3131 * as orphan as above. */
3134 /* 1. If we have neither ".." nor linkEA,
3135 * then it is an orphan.
3137 * 2. If we only have the ".." name entry,
3138 * but no parent references this child
3139 * directory, then handle it as orphan. */
3140 lfsck_ibits_unlock(&lh, LCK_EX);
3141 type = LNIT_MUL_REF;
3143 /* If the LFSCK is marked as LF_INCOMPLETE,
3144 * then means some MDT has ever tried to
3145 * verify some remote MDT-object that resides
3146 * on this MDT, but this MDT failed to respond
3147 * such request. So means there may be some
3148 * remote name entry on other MDT that
3149 * references this object with another name,
3150 * so we cannot know whether this linkEA is
3151 * valid or not. So keep it there and maybe
3152 * resolved when next LFSCK run. */
3153 if (ns->ln_flags & LF_INCOMPLETE)
3156 snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3157 "-"DFID, PFID(pfid));
3158 rc = lfsck_namespace_insert_orphan(env, com, child,
3159 info->lti_tmpbuf, "D", NULL);
3165 linkea_first_entry(&ldata);
3166 /* This is the most common case: the object has unique linkEA entry. */
3167 if (ldata.ld_leh->leh_reccount == 1) {
3168 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3169 &lh, &type, &retry, &unknown);
3171 LASSERT(!lustre_handle_is_used(&lh));
3180 if (!lustre_handle_is_used(&lh))
3183 if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3184 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3190 /* When we come here, the cases usually like that:
3191 * 1) The directory object has a corrupted linkEA entry. During the
3192 * first-stage scanning, the LFSCK cannot know such corruption,
3193 * then it appends the right linkEA entry according to the found
3194 * name entry after the bad one.
3196 * 2) The directory object has a right linkEA entry. During the
3197 * first-stage scanning, the LFSCK finds some bad name entry,
3198 * but the LFSCK cannot aware that at that time, then it adds
3199 * the bad linkEA entry for further processing. */
3200 rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3201 &lh, &type, lpf, &unknown);
3206 lfsck_ibits_unlock(&lh, LCK_EX);
3209 case LNIT_BAD_LINKEA:
3210 ns->ln_linkea_repaired++;
3212 case LNIT_UNMATCHED_PAIRS:
3213 ns->ln_unmatched_pairs_repaired++;
3216 ns->ln_mul_ref_repaired++;
3224 ns->ln_unknown_inconsistency++;
3230 lfsck_namespace_linkea_stale_overflow(struct linkea_data *ldata,
3231 struct lfsck_namespace *ns)
3233 /* Both the leh_overflow_time and ln_time_latest_reset are
3234 * local time based, so need NOT to care about clock drift
3235 * among the servers. */
3236 return ldata->ld_leh->leh_overflow_time &&
3237 ldata->ld_leh->leh_overflow_time < ns->ln_time_latest_reset;
3241 * Clear the object's linkEA overflow timestamp.
3243 * If the MDT-object has too many hard links as to the linkEA cannot hold
3244 * all of them, then overflow timestamp will be set in the linkEA header.
3245 * If some hard links are removed after that, then it is possible to hold
3246 * other missed linkEA entries. If the namespace LFSCK have added all the
3247 * related linkEA entries, then it will remove the overflow timestamp.
3249 * \param[in] env pointer to the thread context
3250 * \param[in] com pointer to the lfsck component
3251 * \param[in] ldata pointer to the linkEA data for the given @obj
3252 * \param[in] obj pointer to the dt_object to be handled
3254 * \retval positive number for repaired cases
3255 * \retval 0 if nothing to be repaired
3256 * \retval negative error number on failure
3258 static int lfsck_namespace_linkea_clear_overflow(const struct lu_env *env,
3259 struct lfsck_component *com,
3260 struct linkea_data *ldata,
3261 struct dt_object *obj)
3263 struct lfsck_namespace *ns = com->lc_file_ram;
3264 struct lfsck_instance *lfsck = com->lc_lfsck;
3265 struct dt_device *dev = lfsck_obj2dev(obj);
3266 struct thandle *th = NULL;
3267 struct lustre_handle lh = { 0 };
3268 struct lu_buf linkea_buf;
3272 LASSERT(!dt_object_remote(obj));
3274 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3275 MDS_INODELOCK_UPDATE, LCK_PW);
3279 th = dt_trans_create(env, dev);
3281 GOTO(log, rc = PTR_ERR(th));
3283 rc = dt_declare_xattr_set(env, obj,
3284 lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
3285 XATTR_NAME_LINK, 0, th);
3289 rc = dt_trans_start_local(env, dev, th);
3293 dt_write_lock(env, obj, 0);
3294 rc = lfsck_links_read(env, obj, ldata);
3298 if (unlikely(!lfsck_namespace_linkea_stale_overflow(ldata, ns)))
3299 GOTO(unlock, rc = 0);
3301 ldata->ld_leh->leh_overflow_time = 0;
3302 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3303 GOTO(unlock, rc = 1);
3305 /* If all known entries are in the linkEA, then the 'leh_reccount'
3306 * should NOT be zero. */
3307 LASSERT(ldata->ld_leh->leh_reccount > 0);
3309 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
3310 ldata->ld_leh->leh_len);
3311 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK, 0, th);
3312 if (unlikely(rc == -ENOSPC))
3320 dt_write_unlock(env, obj);
3323 dt_trans_stop(env, dev, th);
3326 lfsck_ibits_unlock(&lh, LCK_PW);
3327 CDEBUG(D_LFSCK, "%s: clear linkea overflow timestamp for the object "
3329 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
3335 * Verify the object's agent entry.
3337 * If the object claims to have agent entry but the linkEA does not contain
3338 * remote parent, then remove the agent entry. Otherwise, if the object has
3339 * no agent entry but its linkEA contains remote parent, then will generate
3340 * agent entry for it.
3342 * \param[in] env pointer to the thread context
3343 * \param[in] com pointer to the lfsck component
3344 * \param[in] obj pointer to the dt_object to be handled
3346 * \retval positive number for repaired cases
3347 * \retval 0 if nothing to be repaired
3348 * \retval negative error number on failure
3350 static int lfsck_namespace_check_agent_entry(const struct lu_env *env,
3351 struct lfsck_component *com,
3352 struct dt_object *obj)
3354 struct linkea_data ldata = { NULL };
3355 struct lfsck_thread_info *info = lfsck_env_info(env);
3356 struct lfsck_namespace *ns = com->lc_file_ram;
3357 struct lfsck_instance *lfsck = com->lc_lfsck;
3358 struct lu_fid *pfid = &info->lti_fid2;
3359 struct lu_name *cname = &info->lti_name;
3360 struct lu_seq_range *range = &info->lti_range;
3361 struct seq_server_site *ss = lfsck_dev_site(lfsck);
3362 __u32 idx = lfsck_dev_idx(lfsck);
3364 bool remote = false;
3367 if (!(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT))
3370 rc = lfsck_links_read_with_rec(env, obj, &ldata);
3371 if (rc == -ENOENT || rc == -ENODATA)
3374 if (rc && rc != -EINVAL)
3377 /* We check the agent entry again after verifying the linkEA
3378 * successfully. So invalid linkEA should be dryrun mode. */
3379 if (rc == -EINVAL || unlikely(!ldata.ld_leh->leh_reccount))
3382 linkea_first_entry(&ldata);
3383 while (ldata.ld_lee != NULL && !remote) {
3384 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
3386 /* If parent FID is unknown, not verify agent entry. */
3387 if (!fid_is_sane(pfid))
3390 fld_range_set_mdt(range);
3391 rc = fld_server_lookup(env, ss->ss_server_fld,
3392 fid_seq(pfid), range);
3394 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
3396 if (range->lsr_index != idx)
3399 linkea_next_entry(&ldata);
3402 if ((lu_object_has_agent_entry(&obj->do_lu) && !remote) ||
3403 (!lu_object_has_agent_entry(&obj->do_lu) && remote)) {
3404 struct dt_device *dev = lfsck_obj2dev(obj);
3405 struct linkea_data ldata2 = { NULL };
3406 struct lustre_handle lh = { 0 };
3407 struct lu_buf linkea_buf;
3408 struct thandle *handle;
3410 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
3413 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
3414 MDS_INODELOCK_UPDATE |
3415 MDS_INODELOCK_XATTR, LCK_EX);
3419 handle = dt_trans_create(env, dev);
3421 GOTO(unlock, rc = PTR_ERR(handle));
3423 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
3424 ldata.ld_leh->leh_len);
3425 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
3426 XATTR_NAME_LINK, LU_XATTR_REPLACE, handle);
3430 rc = dt_trans_start_local(env, dev, handle);
3434 dt_write_lock(env, obj, 0);
3435 rc = lfsck_links_read2_with_rec(env, obj, &ldata2);
3437 if (rc == -ENOENT || rc == -ENODATA)
3442 /* If someone changed linkEA by race, then the agent
3443 * entry will be updated by lower layer automatically. */
3444 if (ldata.ld_leh->leh_len != ldata2.ld_leh->leh_len ||
3445 memcmp(ldata.ld_buf->lb_buf, ldata2.ld_buf->lb_buf,
3446 ldata.ld_leh->leh_len) != 0)
3447 GOTO(unlock2, rc = 0);
3449 rc = dt_xattr_set(env, obj, &linkea_buf, XATTR_NAME_LINK,
3450 LU_XATTR_REPLACE, handle);
3457 dt_write_unlock(env, obj);
3459 dt_trans_stop(env, dev, handle);
3461 lfsck_ibits_unlock(&lh, LCK_EX);
3468 ns->ln_agent_entries_repaired++;
3470 CDEBUG(D_LFSCK, "%s: repair agent entry for "DFID": rc = %d\n",
3471 lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
3476 * Double scan the MDT-object for namespace LFSCK.
3478 * If the MDT-object contains invalid or repeated linkEA entries, then drop
3479 * those entries from the linkEA; if the linkEA becomes empty or the object
3480 * has no linkEA, then it is an orphan and will be added into the directory
3481 * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3482 * the remote parent; if the name entry corresponding to some linkEA entry
3483 * is lost, then add the name entry back to the namespace.
3485 * \param[in] env pointer to the thread context
3486 * \param[in] com pointer to the lfsck component
3487 * \param[in] child pointer to the dt_object to be handled
3488 * \param[in] flags some hints to indicate how the @child should be handled
3490 * \retval positive number for repaired cases
3491 * \retval 0 if nothing to be repaired
3492 * \retval negative error number on failure
3494 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3495 struct lfsck_component *com,
3496 struct dt_object *child, __u8 flags)
3498 struct lfsck_thread_info *info = lfsck_env_info(env);
3499 struct lu_attr *la = &info->lti_la;
3500 struct lu_name *cname = &info->lti_name;
3501 struct lu_fid *pfid = &info->lti_fid;
3502 struct lu_fid *cfid = &info->lti_fid2;
3503 struct lfsck_instance *lfsck = com->lc_lfsck;
3504 struct lfsck_namespace *ns = com->lc_file_ram;
3505 struct dt_object *parent = NULL;
3506 struct linkea_data ldata = { NULL };
3507 bool repaired = false;
3512 dt_read_lock(env, child, 0);
3513 if (unlikely(lfsck_is_dead_obj(child))) {
3514 dt_read_unlock(env, child);
3519 if (S_ISDIR(lfsck_object_type(child))) {
3520 dt_read_unlock(env, child);
3521 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3522 if (!rc && flags & LNTF_CHECK_AGENT_ENTRY)
3523 rc = lfsck_namespace_check_agent_entry(env, com, child);
3528 rc = lfsck_links_read(env, child, &ldata);
3529 dt_read_unlock(env, child);
3531 if (rc == -EINVAL) {
3532 struct lustre_handle lh = { 0 };
3534 rc = lfsck_ibits_lock(env, com->lc_lfsck, child, &lh,
3535 MDS_INODELOCK_UPDATE |
3536 MDS_INODELOCK_XATTR, LCK_EX);
3538 rc = lfsck_namespace_links_remove(env, com, child);
3539 lfsck_ibits_unlock(&lh, LCK_EX);
3548 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3549 unlikely(lfsck_namespace_linkea_stale_overflow(&ldata, ns))) {
3550 rc = lfsck_namespace_linkea_clear_overflow(env, com, &ldata,
3556 ns->ln_linkea_overflow_cleared++;
3559 linkea_first_entry(&ldata);
3560 while (ldata.ld_lee != NULL) {
3561 rc = lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3563 sizeof(info->lti_key));
3564 /* Invalid PFID in the linkEA entry. */
3566 rc = lfsck_namespace_shrink_linkea(env, com, child,
3567 &ldata, cname, pfid, true);
3577 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3579 /* Found repeated linkEA entries */
3581 rc = lfsck_namespace_shrink_linkea(env, com, child,
3582 &ldata, cname, pfid, false);
3594 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3596 GOTO(out, rc = PTR_ERR(parent));
3598 if (!dt_object_exists(parent)) {
3601 if (ldata.ld_leh->leh_reccount > 1) {
3602 /* If it is NOT the last linkEA entry, then
3603 * there is still other chance to make the
3604 * child to be visible via other parent, then
3605 * remove this linkEA entry. */
3606 rc = lfsck_namespace_shrink_linkea(env, com,
3607 child, &ldata, cname, pfid, true);
3609 /* If the LFSCK is marked as LF_INCOMPLETE,
3610 * then means some MDT has ever tried to
3611 * verify some remote MDT-object that resides
3612 * on this MDT, but this MDT failed to respond
3613 * such request. So means there may be some
3614 * remote name entry on other MDT that
3615 * references this object with another name,
3616 * so we cannot know whether this linkEA is
3617 * valid or not. So keep it there and maybe
3618 * resolved when next LFSCK run. */
3619 if (ns->ln_flags & LF_INCOMPLETE) {
3620 lfsck_object_put(env, parent);
3625 /* Create the lost parent as an orphan. */
3626 rc = lfsck_namespace_create_orphan_dir(env, com,
3629 lfsck_object_put(env, parent);
3637 /* Add the missing name entry to the parent. */
3638 rc = lfsck_namespace_insert_normal(env, com,
3639 parent, child, cname->ln_name);
3640 if (unlikely(rc == -EEXIST))
3641 /* Unfortunately, someone reused the
3642 * name under the parent by race. So we
3643 * have to remove the linkEA entry from
3644 * current child object. It means that
3645 * the LFSCK cannot recover the system
3646 * totally back to its original status,
3647 * but it is necessary to make the
3648 * current system to be consistent. */
3649 rc = lfsck_namespace_shrink_linkea(env,
3653 linkea_next_entry(&ldata);
3656 lfsck_object_put(env, parent);
3664 } /* !dt_object_exists(parent) */
3666 /* The linkEA entry with bad parent will be removed. */
3667 if (unlikely(!dt_try_as_dir(env, parent))) {
3668 lfsck_object_put(env, parent);
3669 rc = lfsck_namespace_shrink_linkea(env, com, child,
3670 &ldata, cname, pfid, true);
3680 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3681 (const struct dt_key *)cname->ln_name);
3682 if (rc != 0 && rc != -ENOENT) {
3683 lfsck_object_put(env, parent);
3689 if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3690 /* It is the most common case that we
3691 * find the name entry corresponding
3692 * to the linkEA entry. */
3693 lfsck_object_put(env, parent);
3694 linkea_next_entry(&ldata);
3696 /* The name entry references another
3697 * MDT-object that may be created by
3698 * the LFSCK for repairing dangling
3699 * name entry. Try to replace it. */
3700 rc = lfsck_namespace_replace_cond(env, com,
3701 parent, child, cfid, cname);
3702 lfsck_object_put(env, parent);
3708 linkea_next_entry(&ldata);
3710 rc = lfsck_namespace_shrink_linkea(env,
3724 /* The following handles -ENOENT case */
3726 rc = dt_attr_get(env, child, la);
3730 /* If there is no name entry in the parent dir and the object
3731 * link count is fewer than the linkea entries count, then the
3732 * linkea entry should be removed. */
3733 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3734 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3735 parent, child, &ldata, cname, pfid);
3736 lfsck_object_put(env, parent);
3746 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3747 * MDT has ever tried to verify some remote MDT-object that
3748 * resides on this MDT, but this MDT failed to respond such
3749 * request. So means there may be some remote name entry on
3750 * other MDT that references this object with another name,
3751 * so we cannot know whether this linkEA is valid or not.
3752 * So keep it there and maybe resolved when next LFSCK run. */
3753 if (ns->ln_flags & LF_INCOMPLETE) {
3754 lfsck_object_put(env, parent);
3759 rc = lfsck_namespace_check_name(env, parent, child, cname);
3764 lfsck_object_put(env, parent);
3769 /* It is an invalid name entry, drop it. */
3770 if (unlikely(rc > 0)) {
3771 lfsck_object_put(env, parent);
3772 rc = lfsck_namespace_shrink_linkea(env, com, child,
3773 &ldata, cname, pfid, true);
3783 /* Add the missing name entry back to the namespace. */
3784 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3786 if (unlikely(rc == -ESTALE))
3787 /* It may happen when the remote object has been
3788 * removed, but the local MDT is not aware of that. */
3791 if (unlikely(rc == -EEXIST))
3792 /* Unfortunately, someone reused the name under the
3793 * parent by race. So we have to remove the linkEA
3794 * entry from current child object. It means that the
3795 * LFSCK cannot recover the system totally back to
3796 * its original status, but it is necessary to make
3797 * the current system to be consistent.
3799 * It also may be because of the LFSCK found some
3800 * internal status of create operation. Under such
3801 * case, nothing to be done. */
3802 rc = lfsck_namespace_shrink_linkea_cond(env, com,
3803 parent, child, &ldata, cname, pfid);
3805 linkea_next_entry(&ldata);
3807 lfsck_object_put(env, parent);
3818 if (rc < 0 && rc != -ENODATA)
3821 if (rc == 0 && ldata.ld_leh != NULL)
3822 count = ldata.ld_leh->leh_reccount;
3825 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3826 * MDT has ever tried to verify some remote MDT-object that
3827 * resides on this MDT, but this MDT failed to respond such
3828 * request. So means there may be some remote name entry on
3829 * other MDT that references this object with another name,
3830 * so we cannot know whether this linkEA is valid or not.
3831 * So keep it there and maybe resolved when next LFSCK run. */
3832 if (!(ns->ln_flags & LF_INCOMPLETE) &&
3833 (ldata.ld_leh == NULL ||
3834 !ldata.ld_leh->leh_overflow_time)) {
3835 /* If the child becomes orphan, then insert it into
3836 * the global .lustre/lost+found/MDTxxxx directory. */
3837 rc = lfsck_namespace_insert_orphan(env, com, child,
3843 ns->ln_mul_ref_repaired++;
3848 rc = dt_attr_get(env, child, la);
3852 if (la->la_nlink != 0 && la->la_nlink != count) {
3853 if (unlikely(!S_ISREG(lfsck_object_type(child)) &&
3854 !S_ISLNK(lfsck_object_type(child)))) {
3855 CDEBUG(D_LFSCK, "%s: namespace LFSCK finds "
3856 "the object "DFID"'s nlink count %d "
3857 "does not match linkEA count %d, "
3858 "type %o, skip it.\n",
3859 lfsck_lfsck2name(lfsck),
3860 PFID(lfsck_dto2fid(child)),
3861 la->la_nlink, count,
3862 lfsck_object_type(child));
3863 } else if (la->la_nlink < count &&
3864 likely(!ldata.ld_leh->leh_overflow_time)) {
3865 rc = lfsck_namespace_repair_nlink(env, com,
3868 ns->ln_objs_nlink_repaired++;
3876 if (la->la_nlink > 1)
3877 ns->ln_mul_linked_repaired++;
3883 if (!rc && flags & LNTF_CHECK_AGENT_ENTRY)
3884 rc = lfsck_namespace_check_agent_entry(env, com, child);
3889 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3890 struct lfsck_namespace *ns,
3891 __u64 checked_phase1,
3892 __u64 checked_phase2,
3893 time64_t time_phase1,
3894 time64_t time_phase2, bool dryrun)
3896 const char *postfix = dryrun ? "inconsistent" : "repaired";
3898 seq_printf(m, "checked_phase1: %llu\n"
3899 "checked_phase2: %llu\n"
3902 "failed_phase1: %llu\n"
3903 "failed_phase2: %llu\n"
3904 "directories: %llu\n"
3908 "multiple_linked_checked: %llu\n"
3909 "multiple_linked_%s: %llu\n"
3910 "unknown_inconsistency: %llu\n"
3911 "unmatched_pairs_%s: %llu\n"
3912 "dangling_%s: %llu\n"
3913 "multiple_referenced_%s: %llu\n"
3914 "bad_file_type_%s: %llu\n"
3915 "lost_dirent_%s: %llu\n"
3916 "local_lost_found_scanned: %llu\n"
3917 "local_lost_found_moved: %llu\n"
3918 "local_lost_found_skipped: %llu\n"
3919 "local_lost_found_failed: %llu\n"
3920 "striped_dirs_scanned: %llu\n"
3921 "striped_dirs_%s: %llu\n"
3922 "striped_dirs_failed: %llu\n"
3923 "striped_dirs_disabled: %llu\n"
3924 "striped_dirs_skipped: %llu\n"
3925 "striped_shards_scanned: %llu\n"
3926 "striped_shards_%s: %llu\n"
3927 "striped_shards_failed: %llu\n"
3928 "striped_shards_skipped: %llu\n"
3929 "name_hash_%s: %llu\n"
3930 "linkea_overflow_%s: %llu\n"
3931 "agent_entries_%s: %llu\n"
3932 "success_count: %u\n"
3933 "run_time_phase1: %lld seconds\n"
3934 "run_time_phase2: %lld seconds\n",
3937 dryrun ? "inconsistent" : "updated",
3938 ns->ln_items_repaired,
3939 dryrun ? "inconsistent" : "updated",
3940 ns->ln_objs_repaired_phase2,
3941 ns->ln_items_failed,
3942 ns->ln_objs_failed_phase2,
3943 ns->ln_dirs_checked,
3944 postfix, ns->ln_dirent_repaired,
3945 postfix, ns->ln_linkea_repaired,
3946 postfix, ns->ln_objs_nlink_repaired,
3947 ns->ln_mul_linked_checked,
3948 postfix, ns->ln_mul_linked_repaired,
3949 ns->ln_unknown_inconsistency,
3950 postfix, ns->ln_unmatched_pairs_repaired,
3951 postfix, ns->ln_dangling_repaired,
3952 postfix, ns->ln_mul_ref_repaired,
3953 postfix, ns->ln_bad_type_repaired,
3954 postfix, ns->ln_lost_dirent_repaired,
3955 ns->ln_local_lpf_scanned,
3956 ns->ln_local_lpf_moved,
3957 ns->ln_local_lpf_skipped,
3958 ns->ln_local_lpf_failed,
3959 ns->ln_striped_dirs_scanned,
3960 postfix, ns->ln_striped_dirs_repaired,
3961 ns->ln_striped_dirs_failed,
3962 ns->ln_striped_dirs_disabled,
3963 ns->ln_striped_dirs_skipped,
3964 ns->ln_striped_shards_scanned,
3965 postfix, ns->ln_striped_shards_repaired,
3966 ns->ln_striped_shards_failed,
3967 ns->ln_striped_shards_skipped,
3968 postfix, ns->ln_name_hash_repaired,
3969 dryrun ? "inconsistent" : "cleared",
3970 ns->ln_linkea_overflow_cleared,
3971 postfix, ns->ln_agent_entries_repaired,
3972 ns->ln_success_count,
3977 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3978 struct lfsck_component *com)
3980 struct lfsck_instance *lfsck = com->lc_lfsck;
3981 struct lfsck_namespace *ns = com->lc_file_ram;
3983 while (!list_empty(&lfsck->li_list_lmv)) {
3984 struct lfsck_lmv_unit *llu;
3985 struct lfsck_lmv *llmv;
3987 llu = list_entry(lfsck->li_list_lmv.next,
3988 struct lfsck_lmv_unit, llu_link);
3989 llmv = &llu->llu_lmv;
3991 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3992 "still in using: %u\n",
3993 atomic_read(&llmv->ll_ref));
3995 ns->ln_striped_dirs_skipped++;
3996 lfsck_lmv_put(env, llmv);
4000 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
4001 struct lfsck_component *com,
4002 struct dt_object *obj)
4004 struct lu_attr *la = &lfsck_env_info(env)->lti_la;
4007 rc = dt_attr_get(env, obj, la);
4011 /* zero-linkEA object may be orphan, but it also maybe because
4012 * of upgrading. Currently, we cannot record it for double scan.
4013 * Because it may cause the LFSCK trace file to be too large. */
4015 /* "la_ctime" == 1 means that it has ever been removed from
4016 * backend /lost+found directory but not been added back to
4017 * the normal namespace yet. */
4019 if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
4020 unlikely(la->la_ctime == 1))
4021 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
4022 LNTF_CHECK_LINKEA, true);
4027 /* namespace APIs */
4029 static int lfsck_namespace_reset(const struct lu_env *env,
4030 struct lfsck_component *com, bool init)
4032 struct lfsck_instance *lfsck = com->lc_lfsck;
4033 struct lfsck_namespace *ns = com->lc_file_ram;
4034 struct lfsck_assistant_data *lad = com->lc_data;
4035 struct dt_object *root;
4039 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
4041 GOTO(log, rc = PTR_ERR(root));
4043 if (unlikely(!dt_try_as_dir(env, root)))
4044 GOTO(put, rc = -ENOTDIR);
4046 down_write(&com->lc_sem);
4048 memset(ns, 0, sizeof(*ns));
4050 __u32 count = ns->ln_success_count;
4051 time64_t last_time = ns->ln_time_last_complete;
4053 memset(ns, 0, sizeof(*ns));
4054 ns->ln_success_count = count;
4055 ns->ln_time_last_complete = last_time;
4057 ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
4058 ns->ln_status = LS_INIT;
4059 ns->ln_time_latest_reset = ktime_get_real_seconds();
4061 rc = lfsck_load_one_trace_file(env, com, root, &com->lc_obj,
4062 &dt_lfsck_namespace_features,
4063 LFSCK_NAMESPACE, true);
4067 rc = lfsck_load_sub_trace_files(env, com, &dt_lfsck_namespace_features,
4068 LFSCK_NAMESPACE, true);
4072 lad->lad_incomplete = 0;
4073 CFS_RESET_BITMAP(lad->lad_bitmap);
4075 rc = lfsck_namespace_store(env, com);
4080 up_write(&com->lc_sem);
4083 lfsck_object_put(env, root);
4085 CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
4086 lfsck_lfsck2name(lfsck), rc);
4091 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
4094 struct lfsck_namespace *ns = com->lc_file_ram;
4096 down_write(&com->lc_sem);
4098 com->lc_new_checked++;
4099 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4100 up_write(&com->lc_sem);
4103 static void lfsck_namespace_close_dir(const struct lu_env *env,
4104 struct lfsck_component *com)
4106 struct lfsck_namespace *ns = com->lc_file_ram;
4107 struct lfsck_assistant_data *lad = com->lc_data;
4108 struct lfsck_assistant_object *lso = NULL;
4109 struct lfsck_instance *lfsck = com->lc_lfsck;
4110 struct lfsck_lmv *llmv = lfsck->li_lmv;
4111 struct lfsck_namespace_req *lnr;
4113 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
4114 bool wakeup = false;
4120 OBD_ALLOC(lnr, size);
4122 ns->ln_striped_dirs_skipped++;
4127 lso = lfsck_assistant_object_init(env, lfsck_dto2fid(lfsck->li_obj_dir),
4128 NULL, lfsck->li_pos_current.lp_oit_cookie, true);
4130 OBD_FREE(lnr, size);
4131 ns->ln_striped_dirs_skipped++;
4136 /* Generate a dummy request to indicate that all shards' name entry
4137 * in this striped directory has been scanned for the first time. */
4138 INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
4139 lnr->lnr_lar.lar_parent = lso;
4140 lnr->lnr_lmv = lfsck_lmv_get(llmv);
4141 lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
4142 lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
4143 lnr->lnr_size = size;
4145 spin_lock(&lad->lad_lock);
4146 if (lad->lad_assistant_status < 0 ||
4147 unlikely(!thread_is_running(&lfsck->li_thread) ||
4148 !thread_is_running(&lad->lad_thread))) {
4149 spin_unlock(&lad->lad_lock);
4150 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4151 ns->ln_striped_dirs_skipped++;
4156 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4157 if (lad->lad_prefetched == 0)
4160 lad->lad_prefetched++;
4161 spin_unlock(&lad->lad_lock);
4163 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4168 static int lfsck_namespace_open_dir(const struct lu_env *env,
4169 struct lfsck_component *com)
4171 struct lfsck_instance *lfsck = com->lc_lfsck;
4172 struct lfsck_namespace *ns = com->lc_file_ram;
4173 struct lfsck_lmv *llmv = lfsck->li_lmv;
4180 if (llmv->ll_lmv_master) {
4181 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
4183 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
4184 lmv->lmv_master_mdt_index =
4185 lfsck_dev_idx(lfsck);
4186 ns->ln_flags |= LF_INCONSISTENT;
4187 llmv->ll_lmv_updated = 1;
4190 rc = lfsck_namespace_verify_stripe_slave(env, com,
4191 lfsck->li_obj_dir, llmv);
4194 RETURN(rc > 0 ? 0 : rc);
4197 static int lfsck_namespace_checkpoint(const struct lu_env *env,
4198 struct lfsck_component *com, bool init)
4200 struct lfsck_instance *lfsck = com->lc_lfsck;
4201 struct lfsck_namespace *ns = com->lc_file_ram;
4205 rc = lfsck_checkpoint_generic(env, com);
4210 down_write(&com->lc_sem);
4212 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
4214 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4215 ns->ln_run_time_phase1 += ktime_get_seconds() -
4216 lfsck->li_time_last_checkpoint;
4217 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
4218 ns->ln_items_checked += com->lc_new_checked;
4219 com->lc_new_checked = 0;
4222 rc = lfsck_namespace_store(env, com);
4223 up_write(&com->lc_sem);
4226 CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos [%llu"
4227 ", "DFID", %#llx], status = %d: rc = %d\n",
4228 lfsck_lfsck2name(lfsck), lfsck->li_pos_current.lp_oit_cookie,
4229 PFID(&lfsck->li_pos_current.lp_dir_parent),
4230 lfsck->li_pos_current.lp_dir_cookie, ns->ln_status, rc);
4232 return rc > 0 ? 0 : rc;
4235 static int lfsck_namespace_prep(const struct lu_env *env,
4236 struct lfsck_component *com,
4237 struct lfsck_start_param *lsp)
4239 struct lfsck_instance *lfsck = com->lc_lfsck;
4240 struct lfsck_namespace *ns = com->lc_file_ram;
4241 struct lfsck_position *pos = &com->lc_pos_start;
4244 rc = lfsck_namespace_load_bitmap(env, com);
4245 if (rc != 0 || ns->ln_status == LS_COMPLETED) {
4246 rc = lfsck_namespace_reset(env, com, false);
4248 rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
4251 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
4252 "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
4258 down_write(&com->lc_sem);
4259 ns->ln_time_latest_start = ktime_get_real_seconds();
4260 spin_lock(&lfsck->li_lock);
4262 if (ns->ln_flags & LF_SCANNED_ONCE) {
4263 if (!lfsck->li_drop_dryrun ||
4264 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4265 ns->ln_status = LS_SCANNING_PHASE2;
4266 list_move_tail(&com->lc_link,
4267 &lfsck->li_list_double_scan);
4268 if (!list_empty(&com->lc_link_dir))
4269 list_del_init(&com->lc_link_dir);
4270 lfsck_pos_set_zero(pos);
4272 ns->ln_status = LS_SCANNING_PHASE1;
4273 ns->ln_run_time_phase1 = 0;
4274 ns->ln_run_time_phase2 = 0;
4275 ns->ln_items_checked = 0;
4276 ns->ln_items_repaired = 0;
4277 ns->ln_items_failed = 0;
4278 ns->ln_dirs_checked = 0;
4279 ns->ln_objs_checked_phase2 = 0;
4280 ns->ln_objs_repaired_phase2 = 0;
4281 ns->ln_objs_failed_phase2 = 0;
4282 ns->ln_objs_nlink_repaired = 0;
4283 ns->ln_dirent_repaired = 0;
4284 ns->ln_linkea_repaired = 0;
4285 ns->ln_mul_linked_checked = 0;
4286 ns->ln_mul_linked_repaired = 0;
4287 ns->ln_unknown_inconsistency = 0;
4288 ns->ln_unmatched_pairs_repaired = 0;
4289 ns->ln_dangling_repaired = 0;
4290 ns->ln_mul_ref_repaired = 0;
4291 ns->ln_bad_type_repaired = 0;
4292 ns->ln_lost_dirent_repaired = 0;
4293 ns->ln_striped_dirs_scanned = 0;
4294 ns->ln_striped_dirs_repaired = 0;
4295 ns->ln_striped_dirs_failed = 0;
4296 ns->ln_striped_dirs_disabled = 0;
4297 ns->ln_striped_dirs_skipped = 0;
4298 ns->ln_striped_shards_scanned = 0;
4299 ns->ln_striped_shards_repaired = 0;
4300 ns->ln_striped_shards_failed = 0;
4301 ns->ln_striped_shards_skipped = 0;
4302 ns->ln_name_hash_repaired = 0;
4303 fid_zero(&ns->ln_fid_latest_scanned_phase2);
4304 if (list_empty(&com->lc_link_dir))
4305 list_add_tail(&com->lc_link_dir,
4306 &lfsck->li_list_dir);
4307 *pos = ns->ln_pos_first_inconsistent;
4310 ns->ln_status = LS_SCANNING_PHASE1;
4311 if (list_empty(&com->lc_link_dir))
4312 list_add_tail(&com->lc_link_dir,
4313 &lfsck->li_list_dir);
4314 if (!lfsck->li_drop_dryrun ||
4315 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
4316 *pos = ns->ln_pos_last_checkpoint;
4317 pos->lp_oit_cookie++;
4319 *pos = ns->ln_pos_first_inconsistent;
4323 spin_unlock(&lfsck->li_lock);
4324 up_write(&com->lc_sem);
4326 rc = lfsck_start_assistant(env, com, lsp);
4328 CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos [%llu, "
4329 DFID", %#llx]: rc = %d\n",
4330 lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4331 PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4336 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4337 struct lfsck_component *com,
4338 struct dt_object *obj)
4340 struct lfsck_thread_info *info = lfsck_env_info(env);
4341 struct lfsck_namespace *ns = com->lc_file_ram;
4342 struct lfsck_instance *lfsck = com->lc_lfsck;
4343 const struct lu_fid *fid = lfsck_dto2fid(obj);
4344 struct lu_fid *pfid = &info->lti_fid2;
4345 struct lu_name *cname = &info->lti_name;
4346 struct lu_seq_range *range = &info->lti_range;
4347 struct seq_server_site *ss = lfsck_dev_site(lfsck);
4348 struct linkea_data ldata = { NULL };
4349 __u32 idx = lfsck_dev_idx(lfsck);
4351 bool remote = false;
4354 rc = lfsck_links_read(env, obj, &ldata);
4358 /* -EINVAL means crashed linkEA, should be verified. */
4359 if (rc == -EINVAL) {
4360 rc = lfsck_namespace_trace_update(env, com, fid,
4361 LNTF_CHECK_LINKEA, true);
4363 struct lustre_handle lh = { 0 };
4365 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4366 MDS_INODELOCK_UPDATE |
4367 MDS_INODELOCK_XATTR, LCK_EX);
4369 rc = lfsck_namespace_links_remove(env, com,
4371 lfsck_ibits_unlock(&lh, LCK_EX);
4375 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4378 if (rc && rc != -ENODATA)
4381 if (rc == -ENODATA || unlikely(!ldata.ld_leh->leh_reccount)) {
4382 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4387 linkea_first_entry(&ldata);
4388 while (ldata.ld_lee != NULL) {
4389 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen,
4391 if (!fid_is_sane(pfid)) {
4392 rc = lfsck_namespace_trace_update(env, com, fid,
4393 LNTF_CHECK_PARENT, true);
4395 fld_range_set_mdt(range);
4396 rc = fld_server_lookup(env, ss->ss_server_fld,
4397 fid_seq(pfid), range);
4398 if ((rc == -ENOENT) ||
4399 (!rc && range->lsr_index != idx)) {
4407 linkea_next_entry(&ldata);
4410 if ((lu_object_has_agent_entry(&obj->do_lu) && !remote) ||
4411 (!lu_object_has_agent_entry(&obj->do_lu) && remote)) {
4412 rc = lfsck_namespace_trace_update(env, com, fid,
4413 LNTF_CHECK_AGENT_ENTRY, true);
4418 /* Record multiple-linked object. */
4419 if (ldata.ld_leh->leh_reccount > 1) {
4420 rc = lfsck_namespace_trace_update(env, com, fid,
4421 LNTF_CHECK_LINKEA, true);
4427 rc = lfsck_namespace_trace_update(env, com, fid,
4428 LNTF_CHECK_LINKEA, true);
4430 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4435 down_write(&com->lc_sem);
4436 if (S_ISDIR(lfsck_object_type(obj)))
4437 ns->ln_dirs_checked++;
4439 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4440 up_write(&com->lc_sem);
4445 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4446 struct lfsck_component *com,
4447 struct lfsck_assistant_object *lso,
4448 struct lu_dirent *ent, __u16 type)
4450 struct lfsck_assistant_data *lad = com->lc_data;
4451 struct lfsck_instance *lfsck = com->lc_lfsck;
4452 struct lfsck_namespace_req *lnr;
4453 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4454 struct ptlrpc_thread *mthread = &lfsck->li_thread;
4455 struct ptlrpc_thread *athread = &lad->lad_thread;
4456 struct l_wait_info lwi = { 0 };
4457 bool wakeup = false;
4459 l_wait_event(mthread->t_ctl_waitq,
4460 lad->lad_prefetched < bk->lb_async_windows ||
4461 !thread_is_running(mthread) ||
4462 !thread_is_running(athread),
4465 if (unlikely(!thread_is_running(mthread) ||
4466 !thread_is_running(athread)))
4469 if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4472 lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, lso, ent, type);
4474 struct lfsck_namespace *ns = com->lc_file_ram;
4476 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4477 return PTR_ERR(lnr);
4480 spin_lock(&lad->lad_lock);
4481 if (lad->lad_assistant_status < 0 ||
4482 unlikely(!thread_is_running(mthread) ||
4483 !thread_is_running(athread))) {
4484 spin_unlock(&lad->lad_lock);
4485 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4486 return lad->lad_assistant_status;
4489 list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4490 if (lad->lad_prefetched == 0)
4493 lad->lad_prefetched++;
4494 spin_unlock(&lad->lad_lock);
4496 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4498 down_write(&com->lc_sem);
4499 com->lc_new_checked++;
4500 up_write(&com->lc_sem);
4505 static int lfsck_namespace_post(const struct lu_env *env,
4506 struct lfsck_component *com,
4507 int result, bool init)
4509 struct lfsck_instance *lfsck = com->lc_lfsck;
4510 struct lfsck_namespace *ns = com->lc_file_ram;
4514 lfsck_post_generic(env, com, &result);
4516 down_write(&com->lc_sem);
4517 lfsck_namespace_release_lmv(env, com);
4519 spin_lock(&lfsck->li_lock);
4521 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4523 ns->ln_status = LS_SCANNING_PHASE2;
4524 ns->ln_flags |= LF_SCANNED_ONCE;
4525 ns->ln_flags &= ~LF_UPGRADE;
4526 list_del_init(&com->lc_link_dir);
4527 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4528 } else if (result == 0) {
4529 if (lfsck->li_status != 0)
4530 ns->ln_status = lfsck->li_status;
4532 ns->ln_status = LS_STOPPED;
4533 if (ns->ln_status != LS_PAUSED) {
4534 list_del_init(&com->lc_link_dir);
4535 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4538 ns->ln_status = LS_FAILED;
4539 list_del_init(&com->lc_link_dir);
4540 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4542 spin_unlock(&lfsck->li_lock);
4545 ns->ln_run_time_phase1 += ktime_get_seconds() -
4546 lfsck->li_time_last_checkpoint;
4547 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
4548 ns->ln_items_checked += com->lc_new_checked;
4549 com->lc_new_checked = 0;
4552 rc = lfsck_namespace_store(env, com);
4553 up_write(&com->lc_sem);
4555 CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4556 lfsck_lfsck2name(lfsck), rc);
4562 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4565 struct lfsck_instance *lfsck = com->lc_lfsck;
4566 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
4567 struct lfsck_namespace *ns = com->lc_file_ram;
4569 down_read(&com->lc_sem);
4570 seq_printf(m, "name: lfsck_namespace\n"
4576 lfsck_status2name(ns->ln_status));
4578 lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4580 lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4582 lfsck_time_dump(m, ns->ln_time_last_complete, "last_completed");
4584 lfsck_time_dump(m, ns->ln_time_latest_start, "latest_start");
4586 lfsck_time_dump(m, ns->ln_time_last_checkpoint, "last_checkpoint");
4588 lfsck_pos_dump(m, &ns->ln_pos_latest_start, "latest_start_position");
4590 lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4591 "last_checkpoint_position");
4593 lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4594 "first_failure_position");
4596 if (ns->ln_status == LS_SCANNING_PHASE1) {
4597 struct lfsck_position pos;
4598 time64_t duration = ktime_get_seconds() -
4599 lfsck->li_time_last_checkpoint;
4600 u64 checked = ns->ln_items_checked + com->lc_new_checked;
4601 u64 speed = checked;
4602 u64 new_checked = com->lc_new_checked;
4603 time64_t rtime = ns->ln_run_time_phase1 + duration;
4606 new_checked = div64_s64(new_checked, duration);
4609 speed = div64_s64(speed, rtime);
4611 lfsck_namespace_dump_statistics(m, ns, checked, 0, rtime, 0,
4612 bk->lb_param & LPF_DRYRUN);
4613 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4614 "average_speed_phase2: N/A\n"
4615 "average_speed_total: %llu items/sec\n"
4616 "real_time_speed_phase1: %llu items/sec\n"
4617 "real_time_speed_phase2: N/A\n",
4622 if (likely(lfsck->li_di_oit)) {
4623 const struct dt_it_ops *iops =
4624 &lfsck->li_obj_oit->do_index_ops->dio_it;
4626 /* The low layer otable-based iteration position may NOT
4627 * exactly match the namespace-based directory traversal
4628 * cookie. Generally, it is not a serious issue. But the
4629 * caller should NOT make assumption on that. */
4630 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4631 if (!lfsck->li_current_oit_processed)
4632 pos.lp_oit_cookie--;
4634 spin_lock(&lfsck->li_lock);
4635 if (lfsck->li_di_dir) {
4636 pos.lp_dir_cookie = lfsck->li_cookie_dir;
4637 if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4638 fid_zero(&pos.lp_dir_parent);
4639 pos.lp_dir_cookie = 0;
4642 *lfsck_dto2fid(lfsck->li_obj_dir);
4645 fid_zero(&pos.lp_dir_parent);
4646 pos.lp_dir_cookie = 0;
4648 spin_unlock(&lfsck->li_lock);
4650 pos = ns->ln_pos_last_checkpoint;
4653 lfsck_pos_dump(m, &pos, "current_position");
4654 } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4655 time64_t duration = ktime_get_seconds() -
4656 com->lc_time_last_checkpoint;
4657 __u64 checked = ns->ln_objs_checked_phase2 +
4658 com->lc_new_checked;
4659 __u64 speed1 = ns->ln_items_checked;
4660 __u64 speed2 = checked;
4661 __u64 speed0 = speed1 + speed2;
4662 __u64 new_checked = com->lc_new_checked;
4663 time64_t rtime = ns->ln_run_time_phase2 + duration;
4664 time64_t time0 = ns->ln_run_time_phase1 + rtime;
4667 new_checked = div64_s64(new_checked, duration);
4669 if (ns->ln_run_time_phase1 != 0)
4670 speed1 = div64_s64(speed1, ns->ln_run_time_phase1);
4671 else if (ns->ln_items_checked != 0)
4675 speed2 = div64_s64(speed2, rtime);
4676 else if (checked != 0)
4680 speed0 = div64_s64(speed0, time0);
4682 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4684 ns->ln_run_time_phase1, rtime,
4685 bk->lb_param & LPF_DRYRUN);
4686 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4687 "average_speed_phase2: %llu objs/sec\n"
4688 "average_speed_total: %llu items/sec\n"
4689 "real_time_speed_phase1: N/A\n"
4690 "real_time_speed_phase2: %llu objs/sec\n"
4691 "current_position: "DFID"\n",
4696 PFID(&ns->ln_fid_latest_scanned_phase2));
4698 __u64 speed1 = ns->ln_items_checked;
4699 __u64 speed2 = ns->ln_objs_checked_phase2;
4700 __u64 speed0 = speed1 + speed2;
4701 time64_t time0 = ns->ln_run_time_phase1 + ns->ln_run_time_phase2;
4703 if (ns->ln_run_time_phase1 != 0)
4704 speed1 = div64_s64(speed1, ns->ln_run_time_phase1);
4705 else if (ns->ln_items_checked != 0)
4708 if (ns->ln_run_time_phase2 != 0)
4709 speed2 = div64_s64(speed2, ns->ln_run_time_phase2);
4710 else if (ns->ln_objs_checked_phase2 != 0)
4714 speed0 = div64_s64(speed0, time0);
4716 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4717 ns->ln_objs_checked_phase2,
4718 ns->ln_run_time_phase1,
4719 ns->ln_run_time_phase2,
4720 bk->lb_param & LPF_DRYRUN);
4721 seq_printf(m, "average_speed_phase1: %llu items/sec\n"
4722 "average_speed_phase2: %llu objs/sec\n"
4723 "average_speed_total: %llu items/sec\n"
4724 "real_time_speed_phase1: N/A\n"
4725 "real_time_speed_phase2: N/A\n"
4726 "current_position: N/A\n",
4732 up_read(&com->lc_sem);
4735 static int lfsck_namespace_double_scan(const struct lu_env *env,
4736 struct lfsck_component *com)
4738 struct lfsck_namespace *ns = com->lc_file_ram;
4739 struct lfsck_assistant_data *lad = com->lc_data;
4740 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4741 struct lfsck_tgt_desc *ltd;
4742 struct lfsck_tgt_desc *next;
4745 rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4746 if (thread_is_stopped(&lad->lad_thread)) {
4747 LASSERT(list_empty(&lad->lad_req_list));
4748 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4750 spin_lock(<ds->ltd_lock);
4751 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4752 ltd_namespace_phase_list) {
4753 list_del_init(<d->ltd_namespace_phase_list);
4755 spin_unlock(<ds->ltd_lock);
4761 static void lfsck_namespace_data_release(const struct lu_env *env,
4762 struct lfsck_component *com)
4764 struct lfsck_assistant_data *lad = com->lc_data;
4765 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4766 struct lfsck_tgt_desc *ltd;
4767 struct lfsck_tgt_desc *next;
4769 LASSERT(lad != NULL);
4770 LASSERT(thread_is_init(&lad->lad_thread) ||
4771 thread_is_stopped(&lad->lad_thread));
4772 LASSERT(list_empty(&lad->lad_req_list));
4774 com->lc_data = NULL;
4775 lfsck_namespace_release_lmv(env, com);
4777 spin_lock(<ds->ltd_lock);
4778 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4779 ltd_namespace_phase_list) {
4780 list_del_init(<d->ltd_namespace_phase_list);
4782 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4783 ltd_namespace_phase_list) {
4784 list_del_init(<d->ltd_namespace_phase_list);
4786 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4787 ltd_namespace_list) {
4788 list_del_init(<d->ltd_namespace_list);
4790 spin_unlock(<ds->ltd_lock);
4792 if (likely(lad->lad_bitmap != NULL))
4793 CFS_FREE_BITMAP(lad->lad_bitmap);
4798 static void lfsck_namespace_quit(const struct lu_env *env,
4799 struct lfsck_component *com)
4801 struct lfsck_assistant_data *lad = com->lc_data;
4802 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4803 struct lfsck_tgt_desc *ltd;
4804 struct lfsck_tgt_desc *next;
4806 LASSERT(lad != NULL);
4808 lfsck_quit_generic(env, com);
4810 LASSERT(thread_is_init(&lad->lad_thread) ||
4811 thread_is_stopped(&lad->lad_thread));
4812 LASSERT(list_empty(&lad->lad_req_list));
4814 lfsck_namespace_release_lmv(env, com);
4816 spin_lock(<ds->ltd_lock);
4817 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4818 ltd_namespace_phase_list) {
4819 list_del_init(<d->ltd_namespace_phase_list);
4821 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4822 ltd_namespace_phase_list) {
4823 list_del_init(<d->ltd_namespace_phase_list);
4825 spin_unlock(<ds->ltd_lock);
4828 static int lfsck_namespace_in_notify(const struct lu_env *env,
4829 struct lfsck_component *com,
4830 struct lfsck_request *lr)
4832 struct lfsck_instance *lfsck = com->lc_lfsck;
4833 struct lfsck_namespace *ns = com->lc_file_ram;
4834 struct lfsck_assistant_data *lad = com->lc_data;
4835 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
4836 struct lfsck_tgt_desc *ltd;
4841 switch (lr->lr_event) {
4842 case LE_SET_LMV_MASTER: {
4843 struct dt_object *obj;
4845 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4847 RETURN(PTR_ERR(obj));
4849 if (likely(dt_object_exists(obj)))
4850 rc = lfsck_namespace_notify_lmv_master_local(env, com,
4853 lfsck_object_put(env, obj);
4855 RETURN(rc > 0 ? 0 : rc);
4857 case LE_SET_LMV_SLAVE: {
4858 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4859 ns->ln_striped_shards_repaired++;
4861 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4862 LNTF_RECHECK_NAME_HASH, true);
4864 RETURN(rc > 0 ? 0 : rc);
4866 case LE_PHASE1_DONE:
4867 case LE_PHASE2_DONE:
4874 CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4875 "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4876 lr->lr_index, lr->lr_status, lr->lr_flags2);
4878 spin_lock(<ds->ltd_lock);
4879 ltd = lfsck_ltd2tgt(ltds, lr->lr_index);
4881 spin_unlock(<ds->ltd_lock);
4886 list_del_init(<d->ltd_namespace_phase_list);
4887 switch (lr->lr_event) {
4888 case LE_PHASE1_DONE:
4889 if (lr->lr_status <= 0) {
4890 ltd->ltd_namespace_done = 1;
4891 list_del_init(<d->ltd_namespace_list);
4892 CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4893 "phase1 for namespace LFSCK: rc = %d.\n",
4894 lfsck_lfsck2name(lfsck),
4895 ltd->ltd_index, lr->lr_status);
4896 ns->ln_flags |= LF_INCOMPLETE;
4901 if (lr->lr_flags2 & LF_INCOMPLETE)
4902 ns->ln_flags |= LF_INCOMPLETE;
4904 if (list_empty(<d->ltd_namespace_list))
4905 list_add_tail(<d->ltd_namespace_list,
4906 &lad->lad_mdt_list);
4907 list_add_tail(<d->ltd_namespace_phase_list,
4908 &lad->lad_mdt_phase2_list);
4910 case LE_PHASE2_DONE:
4911 ltd->ltd_namespace_done = 1;
4912 list_del_init(<d->ltd_namespace_list);
4916 ltd->ltd_namespace_done = 1;
4917 list_del_init(<d->ltd_namespace_list);
4918 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4920 "%s: the peer MDT %x exit namespace LFSCK\n",
4921 lfsck_lfsck2name(lfsck), ltd->ltd_index);
4922 ns->ln_flags |= LF_INCOMPLETE;
4928 spin_unlock(<ds->ltd_lock);
4930 if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4931 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4933 memset(stop, 0, sizeof(*stop));
4934 stop->ls_status = lr->lr_status;
4935 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4936 lfsck_stop(env, lfsck->li_bottom, stop);
4937 } else if (lfsck_phase2_next_ready(lad)) {
4938 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4944 static void lfsck_namespace_repaired(struct lfsck_namespace *ns, __u64 *count)
4946 *count += ns->ln_objs_nlink_repaired;
4947 *count += ns->ln_dirent_repaired;
4948 *count += ns->ln_linkea_repaired;
4949 *count += ns->ln_mul_linked_repaired;
4950 *count += ns->ln_unmatched_pairs_repaired;
4951 *count += ns->ln_dangling_repaired;
4952 *count += ns->ln_mul_ref_repaired;
4953 *count += ns->ln_bad_type_repaired;
4954 *count += ns->ln_lost_dirent_repaired;
4955 *count += ns->ln_striped_dirs_disabled;
4956 *count += ns->ln_striped_dirs_repaired;
4957 *count += ns->ln_striped_shards_repaired;
4958 *count += ns->ln_name_hash_repaired;
4959 *count += ns->ln_local_lpf_moved;
4962 static int lfsck_namespace_query_all(const struct lu_env *env,
4963 struct lfsck_component *com,
4964 __u32 *mdts_count, __u64 *repaired)
4966 struct lfsck_namespace *ns = com->lc_file_ram;
4967 struct lfsck_tgt_descs *ltds = &com->lc_lfsck->li_mdt_descs;
4968 struct lfsck_tgt_desc *ltd;
4973 rc = lfsck_query_all(env, com);
4977 down_read(<ds->ltd_rw_sem);
4978 cfs_foreach_bit(ltds->ltd_tgts_bitmap, idx) {
4979 ltd = lfsck_ltd2tgt(ltds, idx);
4980 LASSERT(ltd != NULL);
4982 mdts_count[ltd->ltd_namespace_status]++;
4983 *repaired += ltd->ltd_namespace_repaired;
4985 up_read(<ds->ltd_rw_sem);
4987 down_read(&com->lc_sem);
4988 mdts_count[ns->ln_status]++;
4989 lfsck_namespace_repaired(ns, repaired);
4990 up_read(&com->lc_sem);
4995 static int lfsck_namespace_query(const struct lu_env *env,
4996 struct lfsck_component *com,
4997 struct lfsck_request *req,
4998 struct lfsck_reply *rep,
4999 struct lfsck_query *que, int idx)
5001 struct lfsck_namespace *ns = com->lc_file_ram;
5005 LASSERT(com->lc_lfsck->li_master);
5007 rc = lfsck_namespace_query_all(env, com,
5008 que->lu_mdts_count[idx],
5009 &que->lu_repaired[idx]);
5011 down_read(&com->lc_sem);
5012 rep->lr_status = ns->ln_status;
5013 if (req->lr_flags & LEF_QUERY_ALL)
5014 lfsck_namespace_repaired(ns, &rep->lr_repaired);
5015 up_read(&com->lc_sem);
5021 static struct lfsck_operations lfsck_namespace_ops = {
5022 .lfsck_reset = lfsck_namespace_reset,
5023 .lfsck_fail = lfsck_namespace_fail,
5024 .lfsck_close_dir = lfsck_namespace_close_dir,
5025 .lfsck_open_dir = lfsck_namespace_open_dir,
5026 .lfsck_checkpoint = lfsck_namespace_checkpoint,
5027 .lfsck_prep = lfsck_namespace_prep,
5028 .lfsck_exec_oit = lfsck_namespace_exec_oit,
5029 .lfsck_exec_dir = lfsck_namespace_exec_dir,
5030 .lfsck_post = lfsck_namespace_post,
5031 .lfsck_dump = lfsck_namespace_dump,
5032 .lfsck_double_scan = lfsck_namespace_double_scan,
5033 .lfsck_data_release = lfsck_namespace_data_release,
5034 .lfsck_quit = lfsck_namespace_quit,
5035 .lfsck_in_notify = lfsck_namespace_in_notify,
5036 .lfsck_query = lfsck_namespace_query,
5040 * Repair dangling name entry.
5042 * For the name entry with dangling reference, we need to repare the
5043 * inconsistency according to the LFSCK sponsor's requirement:
5045 * 1) Keep the inconsistency there and report the inconsistency case,
5046 * then give the chance to the application to find related issues,
5047 * and the users can make the decision about how to handle it with
5048 * more human knownledge. (by default)
5050 * 2) Re-create the missing MDT-object with the FID information.
5052 * \param[in] env pointer to the thread context
5053 * \param[in] com pointer to the lfsck component
5054 * \param[in] parent pointer to the dir object that contains the dangling
5056 * \param[in] child pointer to the object corresponding to the dangling
5058 * \param[in] lnr pointer to the namespace request that contains the
5059 * name's name, parent object, parent's LMV, and ect.
5061 * \retval positive number if no need to repair
5062 * \retval zero for repaired successfully
5063 * \retval negative error number on failure
5065 int lfsck_namespace_repair_dangling(const struct lu_env *env,
5066 struct lfsck_component *com,
5067 struct dt_object *parent,
5068 struct dt_object *child,
5069 struct lfsck_namespace_req *lnr)
5071 struct lfsck_thread_info *info = lfsck_env_info(env);
5072 struct lu_attr *la = &info->lti_la;
5073 struct dt_allocation_hint *hint = &info->lti_hint;
5074 struct dt_object_format *dof = &info->lti_dof;
5075 struct dt_insert_rec *rec = &info->lti_dt_rec;
5076 struct lmv_mds_md_v1 *lmv2 = &info->lti_lmv2;
5077 const struct lu_name *cname;
5078 const struct lu_fid *pfid = lfsck_dto2fid(parent);
5079 const struct lu_fid *cfid = lfsck_dto2fid(child);
5080 struct linkea_data ldata = { NULL };
5081 struct lfsck_lock_handle *llh = &info->lti_llh;
5082 struct lu_buf linkea_buf;
5083 struct lu_buf lmv_buf;
5084 struct lfsck_instance *lfsck = com->lc_lfsck;
5085 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5086 struct dt_device *dev = lfsck->li_next;
5087 struct thandle *th = NULL;
5089 __u16 type = lnr->lnr_type;
5093 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5094 if (bk->lb_param & LPF_CREATE_MDTOBJ)
5099 if (!create || bk->lb_param & LPF_DRYRUN)
5102 /* We may need to create the sub-objects of the @child via LOD,
5103 * so make the modification based on lfsck->li_next. */
5105 parent = lfsck_object_locate(dev, parent);
5107 GOTO(log, rc = PTR_ERR(parent));
5109 if (unlikely(!dt_try_as_dir(env, parent)))
5110 GOTO(log, rc = -ENOTDIR);
5112 child = lfsck_object_locate(dev, child);
5114 GOTO(log, rc = PTR_ERR(child));
5116 rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
5121 rc = lfsck_lock(env, lfsck, parent, lnr->lnr_name, llh,
5122 MDS_INODELOCK_UPDATE, LCK_PR);
5126 rc = lfsck_namespace_check_exist(env, parent, child, lnr->lnr_name);
5130 /* Set the ctime as zero, then others can know it is created for
5131 * repairing dangling name entry by LFSCK. And if the LFSCK made
5132 * wrong decision and the real MDT-object has been found later,
5133 * then the LFSCK has chance to fix the incosistency properly. */
5134 memset(la, 0, sizeof(*la));
5135 la->la_mode = (type & S_IFMT) | 0600;
5136 la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
5137 LA_ATIME | LA_MTIME | LA_CTIME;
5139 child->do_ops->do_ah_init(env, hint, parent, child,
5140 la->la_mode & S_IFMT);
5142 memset(dof, 0, sizeof(*dof));
5143 dof->dof_type = dt_mode_to_dft(type);
5144 /* If the target is a regular file, then the LFSCK will only create
5145 * the MDT-object without stripes (dof->dof_reg.striped = 0). related
5146 * OST-objects will be created when write open. */
5148 th = dt_trans_create(env, dev);
5150 GOTO(log, rc = PTR_ERR(th));
5152 /* 1a. create child. */
5153 rc = dt_declare_create(env, child, la, hint, dof, th);
5157 if (S_ISDIR(type)) {
5158 if (unlikely(!dt_try_as_dir(env, child)))
5159 GOTO(stop, rc = -ENOTDIR);
5161 /* 2a. increase child nlink */
5162 rc = dt_declare_ref_add(env, child, th);
5166 /* 3a. insert dot into child dir */
5167 rec->rec_type = S_IFDIR;
5168 rec->rec_fid = cfid;
5169 rc = dt_declare_insert(env, child,
5170 (const struct dt_rec *)rec,
5171 (const struct dt_key *)dot, th);
5175 /* 4a. insert dotdot into child dir */
5176 rec->rec_fid = pfid;
5177 rc = dt_declare_insert(env, child,
5178 (const struct dt_rec *)rec,
5179 (const struct dt_key *)dotdot, th);
5183 /* 5a. generate slave LMV EA. */
5184 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5187 idx = lfsck_shard_name_to_index(env,
5188 lnr->lnr_name, lnr->lnr_namelen,
5190 if (unlikely(idx < 0))
5191 GOTO(stop, rc = idx);
5193 *lmv2 = lnr->lnr_lmv->ll_lmv;
5194 lmv2->lmv_magic = LMV_MAGIC_STRIPE;
5195 lmv2->lmv_master_mdt_index = idx;
5197 lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
5198 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
5199 rc = dt_declare_xattr_set(env, child, &lmv_buf,
5200 XATTR_NAME_LMV, 0, th);
5206 /* 6a. insert linkEA for child */
5207 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
5208 ldata.ld_leh->leh_len);
5209 rc = dt_declare_xattr_set(env, child, &linkea_buf,
5210 XATTR_NAME_LINK, 0, th);
5214 rc = dt_trans_start_local(env, dev, th);
5216 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
5218 dt_write_lock(env, child, 0);
5219 /* 1b. create child */
5220 rc = dt_create(env, child, la, hint, dof, th);
5222 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
5224 if (S_ISDIR(type)) {
5225 /* 2b. increase child nlink */
5226 rc = dt_ref_add(env, child, th);
5230 /* 3b. insert dot into child dir */
5231 rec->rec_type = S_IFDIR;
5232 rec->rec_fid = cfid;
5233 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5234 (const struct dt_key *)dot, th, 1);
5238 /* 4b. insert dotdot into child dir */
5239 rec->rec_fid = pfid;
5240 rc = dt_insert(env, child, (const struct dt_rec *)rec,
5241 (const struct dt_key *)dotdot, th, 1);
5245 /* 5b. generate slave LMV EA. */
5246 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5247 rc = dt_xattr_set(env, child, &lmv_buf, XATTR_NAME_LMV,
5254 /* 6b. insert linkEA for child. */
5255 rc = dt_xattr_set(env, child, &linkea_buf,
5256 XATTR_NAME_LINK, 0, th);
5261 dt_write_unlock(env, child);
5264 dt_trans_stop(env, dev, th);
5268 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
5269 "reference for: parent "DFID", child "DFID", type %u, "
5270 "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
5271 PFID(pfid), PFID(cfid), type, cname->ln_name,
5272 create ? "Create the lost MDT-object as required" :
5273 "Keep the MDT-object there by default", rc);
5276 struct lfsck_namespace *ns = com->lc_file_ram;
5278 ns->ln_flags |= LF_INCONSISTENT;
5284 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
5285 struct lfsck_component *com,
5286 struct lfsck_assistant_req *lar)
5288 struct lfsck_thread_info *info = lfsck_env_info(env);
5289 struct lu_attr *la = &info->lti_la;
5290 struct lfsck_instance *lfsck = com->lc_lfsck;
5291 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5292 struct lfsck_namespace *ns = com->lc_file_ram;
5293 struct lfsck_assistant_data *lad = com->lc_data;
5294 struct linkea_data ldata = { NULL };
5295 const struct lu_name *cname;
5296 struct thandle *handle = NULL;
5297 struct lfsck_namespace_req *lnr =
5298 container_of0(lar, struct lfsck_namespace_req, lnr_lar);
5299 struct dt_object *dir = NULL;
5300 struct dt_object *obj = NULL;
5301 struct lfsck_assistant_object *lso = lar->lar_parent;
5302 const struct lu_fid *pfid = &lso->lso_fid;
5303 struct dt_device *dev = NULL;
5304 struct lustre_handle lh = { 0 };
5305 bool repaired = false;
5306 bool dtlocked = false;
5310 bool bad_hash = false;
5311 bool bad_linkea = false;
5315 enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
5322 if (lnr->lnr_attr & LUDA_UPGRADE) {
5323 ns->ln_flags |= LF_UPGRADE;
5324 ns->ln_dirent_repaired++;
5326 } else if (lnr->lnr_attr & LUDA_REPAIR) {
5327 ns->ln_flags |= LF_INCONSISTENT;
5328 ns->ln_dirent_repaired++;
5332 if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5333 if (strcmp(lnr->lnr_name, dotdot) != 0)
5336 rc = lfsck_namespace_trace_update(env, com, pfid,
5337 LNTF_CHECK_PARENT, true);
5342 if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5343 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5344 " for the name entry %.*s under "DFID"\n",
5345 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5346 lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5348 if (strcmp(lnr->lnr_name, dotdot) != 0)
5349 /* invalid FID means bad name entry, remove it. */
5350 type = LNIT_BAD_DIRENT;
5352 /* If the parent FID is invalid, we cannot remove
5353 * the ".." entry directly. */
5354 rc = lfsck_namespace_trace_update(env, com, pfid,
5355 LNTF_CHECK_PARENT, true);
5360 if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5361 rc = lfsck_namespace_striped_dir_rescan(env, com, lnr);
5366 if (fid_seq_is_dot(fid_seq(&lnr->lnr_fid)))
5369 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5370 rc = lfsck_namespace_handle_striped_master(env, com, lnr);
5375 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5377 GOTO(out, rc = idx);
5379 if (idx == lfsck_dev_idx(lfsck)) {
5380 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5383 dev = lfsck->li_bottom;
5385 struct lfsck_tgt_desc *ltd;
5387 /* Usually, some local filesystem consistency verification
5388 * tools can guarantee the local namespace tree consistenct.
5389 * So the LFSCK will only verify the remote directory. */
5390 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5391 rc = lfsck_namespace_trace_update(env, com, pfid,
5392 LNTF_CHECK_PARENT, true);
5397 ltd = lfsck_ltd2tgt(&lfsck->li_mdt_descs, idx);
5398 if (unlikely(ltd == NULL)) {
5399 CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5400 "did not join the namespace LFSCK\n",
5401 lfsck_lfsck2name(lfsck), idx);
5402 lfsck_lad_set_bitmap(env, com, idx);
5404 GOTO(out, rc = -ENODEV);
5410 obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5412 GOTO(out, rc = PTR_ERR(obj));
5414 cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5415 if (dt_object_exists(obj) == 0) {
5419 dir = lfsck_assistant_object_load(env, lfsck, lso);
5423 GOTO(trace, rc == -ENOENT ? 0 : rc);
5427 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5429 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5430 lnr->lnr_name, lnr->lnr_namelen)) {
5431 type = LNIT_BAD_DIRENT;
5436 type = LNIT_DANGLING;
5437 rc = lfsck_namespace_repair_dangling(env, com, dir,
5446 if (!(bk->lb_param & LPF_DRYRUN) && lad->lad_advance_lock) {
5449 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5450 MDS_INODELOCK_UPDATE |
5451 MDS_INODELOCK_XATTR, LCK_EX);
5455 handle = dt_trans_create(env, dev);
5457 GOTO(out, rc = PTR_ERR(handle));
5459 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5463 rc = dt_trans_start_local(env, dev, handle);
5467 dt_write_lock(env, obj, 0);
5471 rc = lfsck_links_read(env, obj, &ldata);
5472 if (unlikely(rc == -ENOENT)) {
5473 if (handle != NULL) {
5474 dt_write_unlock(env, obj);
5477 dt_trans_stop(env, dev, handle);
5480 lfsck_ibits_unlock(&lh, LCK_EX);
5483 /* It may happen when the remote object has been removed,
5484 * but the local MDT is not aware of that. */
5486 } else if (rc == 0) {
5487 count = ldata.ld_leh->leh_reccount;
5488 rc = linkea_links_find(&ldata, cname, pfid);
5490 (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5491 if ((lfsck_object_type(obj) & S_IFMT) !=
5493 ns->ln_flags |= LF_INCONSISTENT;
5494 type = LNIT_BAD_TYPE;
5500 /* If the name entry hash does not match the slave striped
5501 * directory, and the name entry does not match also, then
5502 * it is quite possible that name entry is corrupted. */
5503 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5504 lnr->lnr_name, lnr->lnr_namelen)) {
5505 ns->ln_flags |= LF_INCONSISTENT;
5506 type = LNIT_BAD_DIRENT;
5511 /* If the file type stored in the name entry does not match
5512 * the file type claimed by the object, and the object does
5513 * not recognize the name entry, then it is quite possible
5514 * that the name entry is corrupted. */
5515 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5516 ns->ln_flags |= LF_INCONSISTENT;
5517 type = LNIT_BAD_DIRENT;
5522 /* For sub-dir object, we cannot make sure whether the sub-dir
5523 * back references the parent via ".." name entry correctly or
5524 * not in the LFSCK first-stage scanning. It may be that the
5525 * (remote) sub-dir ".." name entry has no parent FID after
5526 * file-level backup/restore and its linkEA may be wrong.
5527 * So under such case, we should replace the linkEA according
5528 * to current name entry. But this needs to be done during the
5529 * LFSCK second-stage scanning. The LFSCK will record the name
5530 * entry for further possible using. */
5534 } else if (unlikely(rc == -EINVAL)) {
5535 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5536 type = LNIT_BAD_TYPE;
5539 /* The magic crashed, we are not sure whether there are more
5540 * corrupt data in the linkea, so remove all linkea entries. */
5544 } else if (rc == -ENODATA) {
5545 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5546 type = LNIT_BAD_TYPE;
5553 if (bk->lb_param & LPF_DRYRUN) {
5555 ns->ln_flags |= LF_UPGRADE;
5557 ns->ln_flags |= LF_INCONSISTENT;
5558 ns->ln_linkea_repaired++;
5564 if (!lustre_handle_is_used(&lh)) {
5572 LASSERT(handle != NULL);
5575 dir = lfsck_assistant_object_load(env, lfsck, lso);
5579 GOTO(stop, rc == -ENOENT ? 0 : rc);
5583 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5588 if (!remove && newdata)
5589 ns->ln_flags |= LF_UPGRADE;
5590 else if (remove || !(ns->ln_flags & LF_UPGRADE))
5591 ns->ln_flags |= LF_INCONSISTENT;
5596 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle);
5597 if (rc != 0 && rc != -ENOENT && rc != -ENODATA)
5602 rc = linkea_data_new(&ldata,
5603 &lfsck_env_info(env)->lti_linkea_buf);
5608 rc = linkea_add_buf(&ldata, cname, pfid);
5610 rc = lfsck_links_write(env, obj, &ldata, handle);
5614 count = ldata.ld_leh->leh_reccount;
5615 if (!S_ISDIR(lfsck_object_type(obj)) ||
5616 !dt_object_remote(obj)) {
5617 ns->ln_linkea_repaired++;
5627 dt_write_unlock(env, obj);
5629 if (handle != NULL && !IS_ERR(handle))
5630 dt_trans_stop(env, dev, handle);
5633 lfsck_ibits_unlock(&lh, LCK_EX);
5635 if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5636 !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5637 lnr->lnr_name, lnr->lnr_namelen) &&
5638 type != LNIT_BAD_DIRENT) {
5639 ns->ln_flags |= LF_INCONSISTENT;
5643 dir = lfsck_assistant_object_load(env, lfsck, lso);
5647 GOTO(trace, rc == -ENOENT ? 0 : rc);
5651 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5652 lnr->lnr_lmv, lnr->lnr_name);
5658 if (type != LNIT_NONE && dir == NULL) {
5659 dir = lfsck_assistant_object_load(env, lfsck, lso);
5663 GOTO(trace, rc == -ENOENT ? 0 : rc);
5670 rc = lfsck_namespace_repair_dirent(env, com, dir,
5671 obj, lnr->lnr_name, lnr->lnr_name,
5672 lnr->lnr_type, true, false);
5676 case LNIT_BAD_DIRENT:
5678 /* XXX: This is a bad dirent, we do not know whether
5679 * the original name entry reference a regular
5680 * file or a directory, then keep the parent's
5681 * nlink count unchanged here. */
5682 rc = lfsck_namespace_repair_dirent(env, com, dir,
5683 obj, lnr->lnr_name, lnr->lnr_name,
5684 lnr->lnr_type, false, false);
5692 if (obj != NULL && count == 1 &&
5693 S_ISREG(lfsck_object_type(obj)))
5694 dt_attr_get(env, obj, la);
5698 down_write(&com->lc_sem);
5700 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5701 "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5702 lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid), PFID(pfid),
5703 lnr->lnr_namelen, lnr->lnr_name, rc);
5705 lfsck_namespace_record_failure(env, lfsck, ns);
5706 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5707 rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5708 rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5709 dev != NULL && dev != lfsck->li_bottom)
5710 lfsck_lad_set_bitmap(env, com, idx);
5712 if (!(bk->lb_param & LPF_FAILOUT))
5716 ns->ln_items_repaired++;
5718 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5719 "repaired the entry: "DFID", parent "DFID
5720 ", name %.*s, type %d\n",
5721 lfsck_lfsck2name(lfsck),
5722 PFID(&lnr->lnr_fid), PFID(pfid),
5723 lnr->lnr_namelen, lnr->lnr_name, type);
5727 ns->ln_dangling_repaired++;
5730 ns->ln_bad_type_repaired++;
5732 case LNIT_BAD_DIRENT:
5733 ns->ln_dirent_repaired++;
5739 if (bk->lb_param & LPF_DRYRUN &&
5740 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5741 lfsck_pos_fill(env, lfsck,
5742 &ns->ln_pos_first_inconsistent,
5747 ns->ln_name_hash_repaired++;
5749 /* Not count repeatedly. */
5751 ns->ln_items_repaired++;
5753 CDEBUG(D_LFSCK, "%s: namespace LFSCK "
5754 "assistant repaired the entry: "
5757 lfsck_lfsck2name(lfsck),
5758 PFID(&lnr->lnr_fid), PFID(pfid),
5759 lnr->lnr_namelen, lnr->lnr_name);
5762 if (bk->lb_param & LPF_DRYRUN &&
5763 lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5764 lfsck_pos_fill(env, lfsck,
5765 &ns->ln_pos_first_inconsistent,
5772 if (count > 1 || la->la_nlink > 1)
5773 ns->ln_mul_linked_checked++;
5775 up_write(&com->lc_sem);
5777 if (obj != NULL && !IS_ERR(obj))
5778 lfsck_object_put(env, obj);
5780 if (dir != NULL && !IS_ERR(dir))
5781 lfsck_object_put(env, dir);
5783 lad->lad_advance_lock = bad_linkea;
5789 * Handle one orphan under the backend /lost+found directory
5791 * Insert the orphan FID into the namespace LFSCK trace file for further
5792 * processing (via the subsequent namespace LFSCK second-stage scanning).
5793 * At the same time, remove the orphan name entry from backend /lost+found
5794 * directory. There is an interval between the orphan name entry removed
5795 * from the backend /lost+found directory and the orphan FID in the LFSCK
5796 * trace file handled. In such interval, the LFSCK can be reset, then
5797 * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5798 * To guarantee that the orphans can be found when LFSCK run next time
5799 * without e2fsck again, when remove the orphan name entry, the LFSCK
5800 * will set the orphan's ctime attribute as 1. Since normal applications
5801 * cannot change the object's ctime attribute as 1. Then when LFSCK run
5802 * next time, it can record the object (that ctime is 1) in the namespace
5803 * LFSCK trace file during the first-stage scanning.
5805 * \param[in] env pointer to the thread context
5806 * \param[in] com pointer to the lfsck component
5807 * \param[in] parent pointer to the object for the backend /lost+found
5808 * \param[in] ent pointer to the name entry for the target under the
5809 * backend /lost+found
5811 * \retval positive for repaired
5812 * \retval 0 if needs to repair nothing
5813 * \retval negative error number on failure
5815 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5816 struct lfsck_component *com,
5817 struct dt_object *parent,
5818 struct lu_dirent *ent)
5820 struct lfsck_thread_info *info = lfsck_env_info(env);
5821 struct lu_fid *key = &info->lti_fid;
5822 struct lu_attr *la = &info->lti_la;
5823 struct lfsck_instance *lfsck = com->lc_lfsck;
5824 struct dt_object *obj;
5825 struct dt_device *dev = lfsck->li_bottom;
5826 struct dt_object *child = NULL;
5827 struct thandle *th = NULL;
5834 child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5836 RETURN(PTR_ERR(child));
5838 LASSERT(dt_object_exists(child));
5839 LASSERT(!dt_object_remote(child));
5841 idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5842 obj = com->lc_sub_trace_objs[idx].lsto_obj;
5843 fid_cpu_to_be(key, &ent->lde_fid);
5844 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5845 (const struct dt_key *)key);
5848 flags |= LNTF_CHECK_ORPHAN;
5849 } else if (rc == -ENOENT) {
5850 flags = LNTF_CHECK_ORPHAN;
5855 th = dt_trans_create(env, dev);
5857 GOTO(out, rc = PTR_ERR(th));
5859 /* a1. remove name entry from backend /lost+found */
5860 rc = dt_declare_delete(env, parent,
5861 (const struct dt_key *)ent->lde_name, th);
5865 if (S_ISDIR(lfsck_object_type(child))) {
5866 /* a2. decrease parent's nlink */
5867 rc = dt_declare_ref_del(env, parent, th);
5873 /* a3. remove child's FID from the LFSCK trace file. */
5874 rc = dt_declare_delete(env, obj,
5875 (const struct dt_key *)key, th);
5879 /* a4. set child's ctime as 1 */
5880 memset(la, 0, sizeof(*la));
5882 la->la_valid = LA_CTIME;
5883 rc = dt_declare_attr_set(env, child, la, th);
5888 /* a5. insert child's FID into the LFSCK trace file. */
5889 rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5890 (const struct dt_key *)key, th);
5894 rc = dt_trans_start_local(env, dev, th);
5898 /* b1. remove name entry from backend /lost+found */
5899 rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th);
5903 if (S_ISDIR(lfsck_object_type(child))) {
5904 /* b2. decrease parent's nlink */
5905 dt_write_lock(env, parent, 0);
5906 rc = dt_ref_del(env, parent, th);
5907 dt_write_unlock(env, parent);
5913 /* a3. remove child's FID from the LFSCK trace file. */
5914 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
5918 /* b4. set child's ctime as 1 */
5919 rc = dt_attr_set(env, child, la, th);
5924 /* b5. insert child's FID into the LFSCK trace file. */
5925 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5926 (const struct dt_key *)key, th, 1);
5928 GOTO(stop, rc = (rc == 0 ? 1 : rc));
5931 dt_trans_stop(env, dev, th);
5934 lfsck_object_put(env, child);
5940 * Handle orphans under the backend /lost+found directory
5942 * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5943 * and put them under the backend /lost+found directory that is invisible
5944 * to client. The LFSCK will scan such directory, for the original client
5945 * visible orphans, add their fids into the namespace LFSCK trace file,
5946 * then the subsenquent namespace LFSCK second-stage scanning can handle
5947 * them as other objects to be double scanned: either move back to normal
5948 * namespace, or to the global visible orphan directory:
5949 * /ROOT/.lustre/lost+found/MDTxxxx/
5951 * \param[in] env pointer to the thread context
5952 * \param[in] com pointer to the lfsck component
5954 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5955 struct lfsck_component *com)
5957 struct lfsck_thread_info *info = lfsck_env_info(env);
5958 struct lu_dirent *ent =
5959 (struct lu_dirent *)info->lti_key;
5960 struct lu_seq_range *range = &info->lti_range;
5961 struct lfsck_instance *lfsck = com->lc_lfsck;
5962 struct ptlrpc_thread *thread = &lfsck->li_thread;
5963 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
5964 struct lfsck_namespace *ns = com->lc_file_ram;
5965 struct dt_object *parent;
5966 const struct dt_it_ops *iops;
5968 struct seq_server_site *ss = lfsck_dev_site(lfsck);
5970 __u32 idx = lfsck_dev_idx(lfsck);
5975 parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5976 &LU_BACKEND_LPF_FID);
5977 if (IS_ERR(parent)) {
5978 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5979 lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5983 /* It is normal that the /lost+found does not exist for ZFS backend. */
5984 if (!dt_object_exists(parent))
5987 if (unlikely(!dt_try_as_dir(env, parent)))
5988 GOTO(out, rc = -ENOTDIR);
5990 CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5991 lfsck_lfsck2name(lfsck));
5993 com->lc_new_scanned = 0;
5994 iops = &parent->do_index_ops->dio_it;
5995 di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE);
5997 GOTO(out, rc = PTR_ERR(di));
5999 rc = iops->load(env, di, 0);
6001 rc = iops->next(env, di);
6006 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6007 unlikely(!thread_is_running(thread)))
6010 rc = iops->rec(env, di, (struct dt_rec *)ent,
6011 LUDA_64BITHASH | LUDA_TYPE);
6013 rc = lfsck_unpack_ent(ent, &cookie, &type);
6015 if (unlikely(rc != 0)) {
6016 CDEBUG(D_LFSCK, "%s: fail to iterate backend "
6017 "/lost+found: rc = %d\n",
6018 lfsck_lfsck2name(lfsck), rc);
6023 /* skip dot and dotdot entries */
6024 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
6027 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
6030 if (fid_is_norm(&ent->lde_fid)) {
6031 fld_range_set_mdt(range);
6032 rc = fld_local_lookup(env, ss->ss_server_fld,
6033 fid_seq(&ent->lde_fid), range);
6036 } else if (idx != 0) {
6037 /* If the returned FID is IGIF, then there are three
6040 * 1) The object is upgraded from old Lustre-1.8 with
6041 * IGIF assigned to such object.
6042 * 2) The object is a backend local object and is
6043 * invisible to client.
6044 * 3) The object lost its LMV EA, and since there is
6045 * no FID-in-dirent for the orphan in the backend
6046 * /lost+found directory, then the low layer will
6047 * return IGIF for such object.
6049 * For MDTx (x != 0), it is either case 2) or case 3),
6050 * but from the LFSCK view, they are indistinguishable.
6051 * To be safe, the LFSCK will keep it there and report
6052 * some message, then the adminstrator can handle that
6055 * For MDT0, it is more possible the case 1). The LFSCK
6056 * will handle the orphan as an upgraded object. */
6057 CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
6058 "in the backend /lost+found on the MDT %04x, "
6059 "to be safe, skip it.\n",
6060 lfsck_lfsck2name(lfsck), ent->lde_namelen,
6061 ent->lde_name, PFID(&ent->lde_fid), idx);
6065 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
6068 down_write(&com->lc_sem);
6069 com->lc_new_scanned++;
6070 ns->ln_local_lpf_scanned++;
6072 ns->ln_local_lpf_moved++;
6074 ns->ln_local_lpf_skipped++;
6076 ns->ln_local_lpf_failed++;
6077 up_write(&com->lc_sem);
6079 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6083 lfsck_control_speed_by_self(com);
6084 if (unlikely(!thread_is_running(thread))) {
6089 rc = iops->next(env, di);
6093 iops->fini(env, di);
6098 CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
6099 lfsck_lfsck2name(lfsck), rc);
6101 lfsck_object_put(env, parent);
6105 * Rescan the striped directory after the master LMV EA reset.
6107 * Sometimes, the master LMV EA of the striped directory maybe lost, so when
6108 * the namespace LFSCK engine scan the striped directory for the first time,
6109 * it will be regarded as a normal directory. As the LFSCK processing, some
6110 * other LFSCK instance on other MDT will find the shard of this striped dir,
6111 * and find that the master MDT-object of the striped directory lost its LMV
6112 * EA, then such remote LFSCK instance will regenerate the master LMV EA and
6113 * notify the LFSCK instance on this MDT to rescan the striped directory.
6115 * \param[in] env pointer to the thread context
6116 * \param[in] com pointer to the lfsck component
6117 * \param[in] llu the lfsck_lmv_unit that contains the striped directory
6120 * \retval positive number for success
6121 * \retval 0 for LFSCK stopped/paused
6122 * \retval negative error number on failure
6124 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
6125 struct lfsck_component *com,
6126 struct lfsck_lmv_unit *llu)
6128 struct lfsck_thread_info *info = lfsck_env_info(env);
6129 struct lfsck_instance *lfsck = com->lc_lfsck;
6130 struct lfsck_assistant_data *lad = com->lc_data;
6131 struct dt_object *dir;
6132 const struct dt_it_ops *iops;
6134 struct lu_dirent *ent =
6135 (struct lu_dirent *)info->lti_key;
6136 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6137 struct ptlrpc_thread *thread = &lfsck->li_thread;
6138 struct lfsck_assistant_object *lso = NULL;
6139 struct lfsck_namespace_req *lnr;
6140 struct lfsck_assistant_req *lar;
6145 LASSERT(list_empty(&lad->lad_req_list));
6147 lfsck->li_lmv = &llu->llu_lmv;
6148 lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
6149 rc = lfsck_open_dir(env, lfsck, 0);
6153 dir = lfsck->li_obj_dir;
6154 di = lfsck->li_di_dir;
6155 iops = &dir->do_index_ops->dio_it;
6157 rc = iops->rec(env, di, (struct dt_rec *)ent,
6158 lfsck->li_args_dir);
6160 rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
6164 if (bk->lb_param & LPF_FAILOUT)
6170 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
6174 lso = lfsck_assistant_object_init(env,
6175 lfsck_dto2fid(dir), NULL,
6176 lfsck->li_pos_current.lp_oit_cookie, true);
6178 if (bk->lb_param & LPF_FAILOUT)
6179 GOTO(out, rc = PTR_ERR(lso));
6186 lnr = lfsck_namespace_assistant_req_init(lfsck, lso, ent, type);
6188 if (bk->lb_param & LPF_FAILOUT)
6189 GOTO(out, rc = PTR_ERR(lnr));
6194 lar = &lnr->lnr_lar;
6195 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6196 lfsck_namespace_assistant_req_fini(env, lar);
6197 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
6200 if (unlikely(!thread_is_running(thread)))
6204 rc = iops->next(env, di);
6208 if (lso != NULL && !IS_ERR(lso))
6209 lfsck_assistant_object_put(env, lso);
6211 lfsck_close_dir(env, lfsck, rc);
6215 /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
6216 if (list_empty(&lad->lad_req_list))
6219 spin_lock(&lad->lad_lock);
6220 lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
6222 list_del_init(&lar->lar_list);
6223 spin_unlock(&lad->lad_lock);
6225 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
6226 lfsck_namespace_assistant_req_fini(env, lar);
6228 RETURN(rc == 0 ? 1 : rc);
6232 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
6233 struct lfsck_component *com,
6234 struct dt_object *obj, bool first)
6236 struct lfsck_instance *lfsck = com->lc_lfsck;
6237 struct ptlrpc_thread *thread = &lfsck->li_thread;
6238 struct lfsck_bookmark *bk = &lfsck->li_bookmark_ram;
6239 struct lfsck_namespace *ns = com->lc_file_ram;
6240 const struct dt_it_ops *iops = &obj->do_index_ops->dio_it;
6241 struct dt_object *target;
6249 di = iops->init(env, obj, 0);
6251 RETURN(PTR_ERR(di));
6254 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
6257 rc = iops->get(env, di, (const struct dt_key *)&fid);
6262 /* The start one either has been processed or does not exist,
6264 rc = iops->next(env, di);
6270 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
6271 unlikely(!thread_is_running(thread)))
6274 key = iops->key(env, di);
6283 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
6284 if (!fid_is_sane(&fid)) {
6289 target = lfsck_object_find_bottom(env, lfsck, &fid);
6290 if (IS_ERR(target)) {
6291 rc = PTR_ERR(target);
6295 if (dt_object_exists(target)) {
6296 rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
6298 rc = lfsck_namespace_double_scan_one(env, com,
6305 lfsck_object_put(env, target);
6308 down_write(&com->lc_sem);
6309 com->lc_new_checked++;
6310 com->lc_new_scanned++;
6312 ns->ln_fid_latest_scanned_phase2 = fid;
6315 ns->ln_objs_repaired_phase2++;
6317 ns->ln_objs_failed_phase2++;
6318 up_write(&com->lc_sem);
6320 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
6323 if (unlikely(com->lc_time_next_checkpoint <=
6324 ktime_get_seconds()) &&
6325 com->lc_new_checked != 0) {
6326 down_write(&com->lc_sem);
6327 ns->ln_run_time_phase2 += ktime_get_seconds() -
6328 com->lc_time_last_checkpoint;
6329 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
6330 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6331 com->lc_new_checked = 0;
6332 lfsck_namespace_store(env, com);
6333 up_write(&com->lc_sem);
6335 com->lc_time_last_checkpoint = ktime_get_seconds();
6336 com->lc_time_next_checkpoint =
6337 com->lc_time_last_checkpoint +
6338 LFSCK_CHECKPOINT_INTERVAL;
6341 lfsck_control_speed_by_self(com);
6342 if (unlikely(!thread_is_running(thread)))
6345 rc = iops->next(env, di);
6354 iops->fini(env, di);
6359 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
6360 struct lfsck_component *com)
6362 struct lfsck_instance *lfsck = com->lc_lfsck;
6363 struct lfsck_namespace *ns = com->lc_file_ram;
6368 while (!list_empty(&lfsck->li_list_lmv)) {
6369 struct lfsck_lmv_unit *llu;
6371 spin_lock(&lfsck->li_lock);
6372 llu = list_entry(lfsck->li_list_lmv.next,
6373 struct lfsck_lmv_unit, llu_link);
6374 list_del_init(&llu->llu_link);
6375 spin_unlock(&lfsck->li_lock);
6377 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6382 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6383 lfsck_lfsck2name(lfsck));
6385 lfsck_namespace_scan_local_lpf(env, com);
6387 com->lc_new_checked = 0;
6388 com->lc_new_scanned = 0;
6389 com->lc_time_last_checkpoint = ktime_get_seconds();
6390 com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6391 LFSCK_CHECKPOINT_INTERVAL;
6393 i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6394 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6395 com->lc_sub_trace_objs[i].lsto_obj, true);
6396 while (rc > 0 && ++i < LFSCK_STF_COUNT)
6397 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6398 com->lc_sub_trace_objs[i].lsto_obj, false);
6400 CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6401 "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6406 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6407 struct lfsck_component *com,
6408 struct lfsck_position *pos)
6410 struct lfsck_assistant_data *lad = com->lc_data;
6411 struct lfsck_namespace_req *lnr;
6413 if (((struct lfsck_namespace *)(com->lc_file_ram))->ln_status !=
6417 if (list_empty(&lad->lad_req_list))
6420 lnr = list_entry(lad->lad_req_list.next,
6421 struct lfsck_namespace_req,
6423 pos->lp_oit_cookie = lnr->lnr_lar.lar_parent->lso_oit_cookie;
6424 pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6425 pos->lp_dir_parent = lnr->lnr_lar.lar_parent->lso_fid;
6428 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6429 struct lfsck_component *com,
6432 struct lfsck_instance *lfsck = com->lc_lfsck;
6433 struct lfsck_namespace *ns = com->lc_file_ram;
6435 down_write(&com->lc_sem);
6436 ns->ln_run_time_phase2 += ktime_get_seconds() -
6437 com->lc_time_last_checkpoint;
6438 ns->ln_time_last_checkpoint = ktime_get_real_seconds();
6439 ns->ln_objs_checked_phase2 += com->lc_new_checked;
6440 com->lc_new_checked = 0;
6443 if (ns->ln_flags & LF_INCOMPLETE)
6444 ns->ln_status = LS_PARTIAL;
6446 ns->ln_status = LS_COMPLETED;
6447 ns->ln_flags &= ~LF_SCANNED_ONCE;
6448 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6449 ns->ln_flags &= ~LF_INCONSISTENT;
6450 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6451 ns->ln_success_count++;
6452 } else if (rc == 0) {
6453 if (lfsck->li_status != 0)
6454 ns->ln_status = lfsck->li_status;
6456 ns->ln_status = LS_STOPPED;
6458 ns->ln_status = LS_FAILED;
6461 rc = lfsck_namespace_store(env, com);
6462 up_write(&com->lc_sem);
6468 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6469 struct ptlrpc_request *req,
6473 struct lfsck_async_interpret_args *laia = args;
6474 struct lfsck_tgt_desc *ltd = laia->laia_ltd;
6476 ltd->ltd_synced_failures = 1;
6483 * Notify remote LFSCK instances about former failures.
6485 * The local LFSCK instance has recorded which MDTs have ever failed to respond
6486 * some LFSCK verification requests (maybe because of network issues or the MDT
6487 * itself trouble). During the respond gap the MDT may missed some name entries
6488 * verification, then the MDT cannot know whether related MDT-objects have been
6489 * referenced by related name entries or not, then in the second-stage scanning,
6490 * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6491 * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6492 * name entry for repairing the orphan.
6494 * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6495 * it will scan the bitmap for the ever failed MDTs, and notify them that they
6496 * have ever missed some name entries verification and should skip the handling
6497 * for orphan MDT-objects.
6499 * \param[in] env pointer to the thread context
6500 * \param[in] com pointer to the lfsck component
6501 * \param[in] lr pointer to the lfsck request
6503 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6504 struct lfsck_component *com,
6505 struct lfsck_request *lr)
6507 struct lfsck_async_interpret_args *laia =
6508 &lfsck_env_info(env)->lti_laia2;
6509 struct lfsck_assistant_data *lad = com->lc_data;
6510 struct lfsck_namespace *ns = com->lc_file_ram;
6511 struct lfsck_instance *lfsck = com->lc_lfsck;
6512 struct lfsck_tgt_descs *ltds = &lfsck->li_mdt_descs;
6513 struct lfsck_tgt_desc *ltd;
6514 struct ptlrpc_request_set *set;
6519 if (!lad->lad_incomplete)
6522 set = ptlrpc_prep_set();
6524 GOTO(out, rc = -ENOMEM);
6526 lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6527 memset(laia, 0, sizeof(*laia));
6528 lad->lad_touch_gen++;
6530 down_read(<ds->ltd_rw_sem);
6531 cfs_foreach_bit(lad->lad_bitmap, idx) {
6532 ltd = lfsck_ltd2tgt(ltds, idx);
6536 laia->laia_ltd = ltd;
6537 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6538 lfsck_namespace_assistant_sync_failures_interpret,
6539 laia, LFSCK_NOTIFY);
6541 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6542 "to sync failure with MDT %x: rc = %d\n",
6543 lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6545 up_read(<ds->ltd_rw_sem);
6547 rc = ptlrpc_set_wait(set);
6548 ptlrpc_set_destroy(set);
6554 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6555 "to sync failure with MDTs, and related MDTs "
6556 "may handle orphan improperly: rc = %d\n",
6557 lfsck_lfsck2name(lfsck), rc);
6562 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6563 .la_handler_p1 = lfsck_namespace_assistant_handler_p1,
6564 .la_handler_p2 = lfsck_namespace_assistant_handler_p2,
6565 .la_fill_pos = lfsck_namespace_assistant_fill_pos,
6566 .la_double_scan_result = lfsck_namespace_double_scan_result,
6567 .la_req_fini = lfsck_namespace_assistant_req_fini,
6568 .la_sync_failures = lfsck_namespace_assistant_sync_failures,
6572 * Verify the specified linkEA entry for the given directory object.
6573 * If the object has no such linkEA entry or it has more other linkEA
6574 * entries, then re-generate the linkEA with the given information.
6576 * \param[in] env pointer to the thread context
6577 * \param[in] obj pointer to the dt_object to be handled
6578 * \param[in] cname the name for the child in the parent directory
6579 * \param[in] pfid the parent directory's FID for the linkEA
6581 * \retval 0 for success
6582 * \retval negative error number on failure
6584 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6585 const struct lu_name *cname, const struct lu_fid *pfid)
6587 struct dt_device *dev = lfsck_obj2dev(obj);
6588 struct linkea_data ldata = { NULL };
6589 struct lu_buf linkea_buf;
6592 int fl = LU_XATTR_CREATE;
6596 LASSERT(S_ISDIR(lfsck_object_type(obj)));
6598 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6599 if (rc == -ENODATA) {
6601 } else if (rc == 0) {
6602 fl = LU_XATTR_REPLACE;
6603 if (ldata.ld_leh->leh_reccount != 1) {
6606 rc = linkea_links_find(&ldata, cname, pfid);
6615 rc = linkea_links_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf,
6620 lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6621 ldata.ld_leh->leh_len);
6622 th = dt_trans_create(env, dev);
6624 RETURN(PTR_ERR(th));
6626 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6627 XATTR_NAME_LINK, fl, th);
6631 rc = dt_trans_start_local(env, dev, th);
6635 dt_write_lock(env, obj, 0);
6636 rc = dt_xattr_set(env, obj, &linkea_buf,
6637 XATTR_NAME_LINK, fl, th);
6638 dt_write_unlock(env, obj);
6643 dt_trans_stop(env, dev, th);
6648 * Get the name and parent directory's FID from the first linkEA entry.
6650 * \param[in] env pointer to the thread context
6651 * \param[in] obj pointer to the object which get linkEA from
6652 * \param[out] name pointer to the buffer to hold the name
6653 * in the first linkEA entry
6654 * \param[out] pfid pointer to the buffer to hold the parent
6655 * directory's FID in the first linkEA entry
6657 * \retval 0 for success
6658 * \retval negative error number on failure
6660 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6661 char *name, struct lu_fid *pfid)
6663 struct lu_name *cname = &lfsck_env_info(env)->lti_name;
6664 struct linkea_data ldata = { NULL };
6667 rc = lfsck_links_read_with_rec(env, obj, &ldata);
6671 linkea_first_entry(&ldata);
6672 linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6673 /* To guarantee the 'name' is terminated with '0'. */
6674 memcpy(name, cname->ln_name, cname->ln_namelen);
6675 name[cname->ln_namelen] = 0;
6681 * Update the object's name entry with the given FID.
6683 * \param[in] env pointer to the thread context
6684 * \param[in] lfsck pointer to the lfsck instance
6685 * \param[in] dir pointer to the directory that holds
6687 * \param[in] name the name for the entry to be updated
6688 * \param[in] fid the new FID for the name entry referenced
6689 * \param[in] type the type for the name entry to be updated
6691 * \retval 0 for success
6692 * \retval negative error number on failure
6694 int lfsck_update_name_entry(const struct lu_env *env,
6695 struct lfsck_instance *lfsck,
6696 struct dt_object *dir, const char *name,
6697 const struct lu_fid *fid, __u32 type)
6699 struct lfsck_thread_info *info = lfsck_env_info(env);
6700 struct dt_insert_rec *rec = &info->lti_dt_rec;
6701 struct lfsck_lock_handle *llh = &info->lti_llh;
6702 struct dt_device *dev = lfsck_obj2dev(dir);
6708 rc = lfsck_lock(env, lfsck, dir, name, llh,
6709 MDS_INODELOCK_UPDATE, LCK_PW);
6713 th = dt_trans_create(env, dev);
6715 GOTO(unlock, rc = PTR_ERR(th));
6717 rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6721 rec->rec_type = type;
6723 rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6724 (const struct dt_key *)name, th);
6728 rc = dt_declare_ref_add(env, dir, th);
6732 rc = dt_trans_start_local(env, dev, th);
6736 rc = dt_delete(env, dir, (const struct dt_key *)name, th);
6737 if (rc == -ENOENT) {
6745 rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6746 (const struct dt_key *)name, th, 1);
6747 if (rc == 0 && S_ISDIR(type) && !exists) {
6748 dt_write_lock(env, dir, 0);
6749 rc = dt_ref_add(env, dir, th);
6750 dt_write_unlock(env, dir);
6756 dt_trans_stop(env, dev, th);
6760 CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6761 " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6762 PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6767 int lfsck_namespace_setup(const struct lu_env *env,
6768 struct lfsck_instance *lfsck)
6770 struct lfsck_component *com;
6771 struct lfsck_namespace *ns;
6772 struct dt_object *root = NULL;
6773 struct dt_object *obj;
6778 LASSERT(lfsck->li_master);
6784 INIT_LIST_HEAD(&com->lc_link);
6785 INIT_LIST_HEAD(&com->lc_link_dir);
6786 init_rwsem(&com->lc_sem);
6787 atomic_set(&com->lc_ref, 1);
6788 com->lc_lfsck = lfsck;
6789 com->lc_type = LFSCK_TYPE_NAMESPACE;
6790 com->lc_ops = &lfsck_namespace_ops;
6791 com->lc_data = lfsck_assistant_data_init(
6792 &lfsck_namespace_assistant_ops,
6794 if (com->lc_data == NULL)
6795 GOTO(out, rc = -ENOMEM);
6797 com->lc_file_size = sizeof(struct lfsck_namespace);
6798 OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6799 if (com->lc_file_ram == NULL)
6800 GOTO(out, rc = -ENOMEM);
6802 OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6803 if (com->lc_file_disk == NULL)
6804 GOTO(out, rc = -ENOMEM);
6806 for (i = 0; i < LFSCK_STF_COUNT; i++)
6807 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6809 root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6811 GOTO(out, rc = PTR_ERR(root));
6813 if (unlikely(!dt_try_as_dir(env, root)))
6814 GOTO(out, rc = -ENOTDIR);
6816 obj = local_index_find_or_create(env, lfsck->li_los, root,
6818 S_IFREG | S_IRUGO | S_IWUSR,
6819 &dt_lfsck_namespace_features);
6821 GOTO(out, rc = PTR_ERR(obj));
6824 rc = lfsck_namespace_load(env, com);
6826 rc = lfsck_namespace_init(env, com);
6828 rc = lfsck_namespace_reset(env, com, true);
6830 rc = lfsck_load_sub_trace_files(env, com,
6831 &dt_lfsck_namespace_features, LFSCK_NAMESPACE, false);
6835 ns = com->lc_file_ram;
6836 switch (ns->ln_status) {
6841 spin_lock(&lfsck->li_lock);
6842 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6843 spin_unlock(&lfsck->li_lock);
6846 CERROR("%s: unknown lfsck_namespace status %d\n",
6847 lfsck_lfsck2name(lfsck), ns->ln_status);
6849 case LS_SCANNING_PHASE1:
6850 case LS_SCANNING_PHASE2:
6851 /* No need to store the status to disk right now.
6852 * If the system crashed before the status stored,
6853 * it will be loaded back when next time. */
6854 ns->ln_status = LS_CRASHED;
6858 spin_lock(&lfsck->li_lock);
6859 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6860 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6861 spin_unlock(&lfsck->li_lock);
6868 if (root != NULL && !IS_ERR(root))
6869 lfsck_object_put(env, root);
6871 lfsck_component_cleanup(env, com);
6872 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6873 lfsck_lfsck2name(lfsck), rc);