Whamcloud - gitweb
LU-14627 lnet: Ensure ref taken when queueing for discovery
[fs/lustre-release.git] / lustre / lfsck / lfsck_namespace.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9
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.
15
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2017, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_namespace.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
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>
39
40 #include "lfsck_internal.h"
41
42 #define LFSCK_NAMESPACE_MAGIC_V1        0xA0629D03
43 #define LFSCK_NAMESPACE_MAGIC_V2        0xA0621A0B
44 #define LFSCK_NAMESPACE_MAGIC_V3        0xA06249FF
45
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
52
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. */
57 };
58
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)
63 {
64         struct lfsck_namespace_req *lnr;
65         int                         size;
66
67         size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
68         OBD_ALLOC(lnr, size);
69         if (lnr == NULL)
70                 return ERR_PTR(-ENOMEM);
71
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;
78         lnr->lnr_size = size;
79         lnr->lnr_type = type;
80         lnr->lnr_namelen = ent->lde_namelen;
81         memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
82
83         return lnr;
84 }
85
86 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
87                                                struct lfsck_assistant_req *lar)
88 {
89         struct lfsck_namespace_req *lnr =
90                 container_of(lar, struct lfsck_namespace_req, lnr_lar);
91
92         if (lnr->lnr_lmv != NULL)
93                 lfsck_lmv_put(env, lnr->lnr_lmv);
94
95         lfsck_assistant_object_put(env, lar->lar_parent);
96         OBD_FREE(lnr, lnr->lnr_size);
97 }
98
99 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
100                                       struct lfsck_namespace *src)
101 {
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);
171 }
172
173 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
174                                       struct lfsck_namespace *src)
175 {
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);
245 }
246
247 static void lfsck_namespace_record_failure(const struct lu_env *env,
248                                            struct lfsck_instance *lfsck,
249                                            struct lfsck_namespace *ns)
250 {
251         struct lfsck_position pos;
252
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;
258
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);
265         }
266 }
267
268 /**
269  * Load the MDT bitmap from the lfsck_namespace trace file.
270  *
271  * \param[in] env       pointer to the thread context
272  * \param[in] com       pointer to the lfsck component
273  *
274  * \retval              0 for success
275  * \retval              negative error number on failure or data corruption
276  */
277 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
278                                        struct lfsck_component *com)
279 {
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;
284         ssize_t                          size;
285         __u32                            nbits;
286         int                              rc;
287         ENTRY;
288
289         if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
290             ns->ln_bitmap_size)
291                 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
292         else
293                 nbits = ns->ln_bitmap_size;
294
295         if (unlikely(nbits < BITS_PER_LONG))
296                 nbits = BITS_PER_LONG;
297
298         if (nbits > bitmap->size) {
299                 __u32 new_bits = bitmap->size;
300                 struct cfs_bitmap *new_bitmap;
301
302                 while (new_bits < nbits)
303                         new_bits <<= 1;
304
305                 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
306                 if (new_bitmap == NULL)
307                         RETURN(-ENOMEM);
308
309                 lad->lad_bitmap = new_bitmap;
310                 CFS_FREE_BITMAP(bitmap);
311                 bitmap = new_bitmap;
312         }
313
314         if (ns->ln_bitmap_size == 0) {
315                 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
316                 CFS_RESET_BITMAP(bitmap);
317
318                 RETURN(0);
319         }
320
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);
325         if (rc != size)
326                 RETURN(rc >= 0 ? -EINVAL : rc);
327
328         if (cfs_bitmap_check_empty(bitmap))
329                 clear_bit(LAD_INCOMPLETE, &lad->lad_flags);
330         else
331                 set_bit(LAD_INCOMPLETE, &lad->lad_flags);
332
333         RETURN(0);
334 }
335
336 /**
337  * Load namespace LFSCK statistics information from the trace file.
338  *
339  * \param[in] env       pointer to the thread context
340  * \param[in] com       pointer to the lfsck component
341  *
342  * \retval              0 for success
343  * \retval              negative error number on failure
344  */
345 static int lfsck_namespace_load(const struct lu_env *env,
346                                 struct lfsck_component *com)
347 {
348         int len = com->lc_file_size;
349         int rc;
350
351         rc = dt_xattr_get(env, com->lc_obj,
352                           lfsck_buf_get(env, com->lc_file_disk, len),
353                           XATTR_NAME_LFSCK_NAMESPACE);
354         if (rc == len) {
355                 struct lfsck_namespace *ns = com->lc_file_ram;
356
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);
363                         rc = -ESTALE;
364                 } else {
365                         rc = 0;
366                 }
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);
371                 if (rc >= 0)
372                         rc = -ESTALE;
373         }
374
375         return rc;
376 }
377
378 static int lfsck_namespace_store(const struct lu_env *env,
379                                  struct lfsck_component *com)
380 {
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;
388         __u32                            nbits  = 0;
389         int                              len    = com->lc_file_size;
390         int                              rc;
391         ENTRY;
392
393         if (lad != NULL) {
394                 bitmap = lad->lad_bitmap;
395                 nbits = bitmap->size;
396
397                 LASSERT(nbits > 0);
398                 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
399         }
400
401         ns->ln_bitmap_size = nbits;
402         lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
403                                   ns);
404         handle = dt_trans_create(env, dev);
405         if (IS_ERR(handle))
406                 GOTO(log, rc = PTR_ERR(handle));
407
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);
411         if (rc != 0)
412                 GOTO(out, rc);
413
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);
418                 if (rc != 0)
419                         GOTO(out, rc);
420         }
421
422         rc = dt_trans_start_local(env, dev, handle);
423         if (rc != 0)
424                 GOTO(out, rc);
425
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);
433
434         GOTO(out, rc);
435
436 out:
437         dt_trans_stop(env, dev, handle);
438
439 log:
440         if (rc != 0)
441                 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
442                        lfsck_lfsck2name(lfsck), rc);
443         return rc;
444 }
445
446 static int lfsck_namespace_init(const struct lu_env *env,
447                                 struct lfsck_component *com)
448 {
449         struct lfsck_namespace *ns = com->lc_file_ram;
450         int rc;
451
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);
458         if (rc == 0)
459                 rc = lfsck_load_sub_trace_files(env, com,
460                         &dt_lfsck_namespace_features, LFSCK_NAMESPACE, true);
461         up_write(&com->lc_sem);
462
463         return rc;
464 }
465
466 /**
467  * Update the namespace LFSCK trace file for the given @fid
468  *
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
472  *                      trace file
473  * \param[in] add       true if add new flags, otherwise remove flags
474  *
475  * \retval              0 for success or nothing to be done
476  * \retval              negative error number on failure
477  */
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)
482 {
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;
488         int                      idx;
489         int                      rc     = 0;
490         __u8                     old    = 0;
491         __u8                     new    = 0;
492         ENTRY;
493
494         LASSERT(flags != 0);
495
496         if (unlikely(!fid_is_sane(fid)))
497                 RETURN(0);
498
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);
504                 RETURN(0);
505         }
506
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);
512         if (rc == -ENOENT) {
513                 if (!add)
514                         GOTO(unlock, rc = 0);
515
516                 old = 0;
517                 new = flags;
518         } else if (rc == 0) {
519                 if (add) {
520                         if ((old & flags) == flags)
521                                 GOTO(unlock, rc = 0);
522
523                         new = old | flags;
524                 } else {
525                         if ((old & flags) == 0)
526                                 GOTO(unlock, rc = 0);
527
528                         new = old & ~flags;
529                 }
530         } else {
531                 GOTO(log, rc);
532         }
533
534         th = dt_trans_create(env, dev);
535         if (IS_ERR(th))
536                 GOTO(log, rc = PTR_ERR(th));
537
538         if (old != 0) {
539                 rc = dt_declare_delete(env, obj,
540                                        (const struct dt_key *)key, th);
541                 if (rc != 0)
542                         GOTO(log, rc);
543         }
544
545         if (new != 0) {
546                 rc = dt_declare_insert(env, obj,
547                                        (const struct dt_rec *)&new,
548                                        (const struct dt_key *)key, th);
549                 if (rc != 0)
550                         GOTO(log, rc);
551         }
552
553         rc = dt_trans_start_local(env, dev, th);
554         if (rc != 0)
555                 GOTO(log, rc);
556
557         if (old != 0) {
558                 rc = dt_delete(env, obj, (const struct dt_key *)key, th);
559                 if (rc != 0)
560                         GOTO(log, rc);
561         }
562
563         if (new != 0) {
564                 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
565                                (const struct dt_key *)key, th);
566                 if (rc != 0)
567                         GOTO(log, rc);
568         }
569
570         GOTO(log, rc);
571
572 log:
573         if (th != NULL && !IS_ERR(th))
574                 dt_trans_stop(env, dev, th);
575
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);
580
581 unlock:
582         mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
583         lfsck_object_put(env, obj);
584
585         return rc;
586 }
587
588 int lfsck_namespace_check_exist(const struct lu_env *env,
589                                 struct dt_object *dir,
590                                 struct dt_object *obj, const char *name)
591 {
592         struct lu_fid    *fid = &lfsck_env_info(env)->lti_fid;
593         int               rc;
594         ENTRY;
595
596         if (unlikely(lfsck_is_dead_obj(obj)))
597                 RETURN(LFSCK_NAMEENTRY_DEAD);
598
599         rc = dt_lookup_dir(env, dir, name, fid);
600         if (rc == -ENOENT)
601                 RETURN(LFSCK_NAMEENTRY_REMOVED);
602
603         if (rc < 0)
604                 RETURN(rc);
605
606         if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
607                 RETURN(LFSCK_NAMEENTRY_RECREATED);
608
609         RETURN(0);
610 }
611
612 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
613                                             struct dt_object *obj,
614                                             struct thandle *handle)
615 {
616         int rc;
617
618         /* For remote updating LINKEA, there may be further LFSCK action
619          * on remote MDT after the updating, so update the LINKEA ASAP. */
620         if (dt_object_remote(obj))
621                 handle->th_sync = 1;
622
623         /* For destroying all invalid linkEA entries. */
624         rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
625         if (rc == 0)
626                 /* For insert new linkEA entry. */
627                 rc = dt_declare_xattr_set(env, obj,
628                         lfsck_buf_get_const(env, NULL, MAX_LINKEA_SIZE),
629                         XATTR_NAME_LINK, 0, handle);
630         return rc;
631 }
632
633 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
634                        struct linkea_data *ldata, bool with_rec)
635 {
636         int rc;
637
638         if (ldata->ld_buf->lb_buf == NULL)
639                 return -ENOMEM;
640
641         if (!dt_object_exists(obj))
642                 return -ENOENT;
643
644         rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
645         if (rc == -ERANGE) {
646                 /* Buf was too small, figure out what we need. */
647                 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK);
648                 if (unlikely(rc == 0))
649                         return -ENODATA;
650
651                 if (rc < 0)
652                         return rc;
653
654                 lu_buf_realloc(ldata->ld_buf, rc);
655                 if (ldata->ld_buf->lb_buf == NULL)
656                         return -ENOMEM;
657
658                 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK);
659         }
660
661         if (unlikely(rc == 0))
662                 return -ENODATA;
663
664         if (rc > 0) {
665                 if (with_rec)
666                         rc = linkea_init_with_rec(ldata);
667                 else
668                         rc = linkea_init(ldata);
669         }
670
671         return rc;
672 }
673
674 /**
675  * Remove linkEA for the given object.
676  *
677  * The caller should take the ldlm lock before the calling.
678  *
679  * \param[in] env       pointer to the thread context
680  * \param[in] com       pointer to the lfsck component
681  * \param[in] obj       pointer to the dt_object to be handled
682  *
683  * \retval              0 for repaired cases
684  * \retval              negative error number on failure
685  */
686 static int lfsck_namespace_links_remove(const struct lu_env *env,
687                                         struct lfsck_component *com,
688                                         struct dt_object *obj)
689 {
690         struct lfsck_instance           *lfsck  = com->lc_lfsck;
691         struct dt_device                *dev    = lfsck_obj2dev(obj);
692         struct thandle                  *th     = NULL;
693         int                              rc     = 0;
694         ENTRY;
695
696         LASSERT(dt_object_remote(obj) == 0);
697
698         th = dt_trans_create(env, dev);
699         if (IS_ERR(th))
700                 GOTO(log, rc = PTR_ERR(th));
701
702         rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
703         if (rc != 0)
704                 GOTO(stop, rc);
705
706         rc = dt_trans_start_local(env, dev, th);
707         if (rc != 0)
708                 GOTO(stop, rc);
709
710         dt_write_lock(env, obj, 0);
711         if (unlikely(lfsck_is_dead_obj(obj)))
712                 GOTO(unlock, rc = -ENOENT);
713
714         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
715                 GOTO(unlock, rc = 0);
716
717         rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
718
719         GOTO(unlock, rc);
720
721 unlock:
722         dt_write_unlock(env, obj);
723
724 stop:
725         dt_trans_stop(env, dev, th);
726
727 log:
728         CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
729                "for the object "DFID": rc = %d\n",
730                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
731
732         if (rc == 0) {
733                 struct lfsck_namespace *ns = com->lc_file_ram;
734
735                 ns->ln_flags |= LF_INCONSISTENT;
736         }
737
738         return rc;
739 }
740
741 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
742                              struct linkea_data *ldata, struct thandle *handle)
743 {
744         struct lu_buf buf;
745         int rc;
746
747         lfsck_buf_init(&buf, ldata->ld_buf->lb_buf, ldata->ld_leh->leh_len);
748
749 again:
750         rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LINK, 0, handle);
751         if (unlikely(rc == -ENOSPC)) {
752                 rc = linkea_overflow_shrink(ldata);
753                 if (likely(rc > 0)) {
754                         buf.lb_len = rc;
755                         goto again;
756                 }
757         }
758
759         return rc;
760 }
761
762 static inline bool linkea_reclen_is_valid(const struct linkea_data *ldata)
763 {
764         if (ldata->ld_reclen <= 0)
765                 return false;
766
767         if ((char *)ldata->ld_lee + ldata->ld_reclen >
768             (char *)ldata->ld_leh + ldata->ld_leh->leh_len)
769                 return false;
770
771         return true;
772 }
773
774 static inline bool linkea_entry_is_valid(const struct linkea_data *ldata,
775                                          const struct lu_name *cname,
776                                          const struct lu_fid *pfid)
777 {
778         if (!linkea_reclen_is_valid(ldata))
779                 return false;
780
781         if (cname->ln_namelen <= 0 || cname->ln_namelen > NAME_MAX)
782                 return false;
783
784         if (!fid_is_sane(pfid))
785                 return false;
786
787         return true;
788 }
789
790 static int lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
791                                                struct lu_name *cname,
792                                                struct lu_fid *pfid,
793                                                char *buf, const int buflen)
794 {
795         linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
796         if (unlikely(!linkea_entry_is_valid(ldata, cname, pfid)))
797                 return -EINVAL;
798
799         /* To guarantee the 'name' is terminated with '0'. */
800         memcpy(buf, cname->ln_name, cname->ln_namelen);
801         buf[cname->ln_namelen] = 0;
802         cname->ln_name = buf;
803
804         return 0;
805 }
806
807 static void lfsck_linkea_del_buf(struct linkea_data *ldata,
808                                  const struct lu_name *lname)
809 {
810         LASSERT(ldata->ld_leh != NULL && ldata->ld_lee != NULL);
811
812         /* If current record is corrupted, all the subsequent
813          * records will be dropped. */
814         if (unlikely(!linkea_reclen_is_valid(ldata))) {
815                 void *ptr = ldata->ld_lee;
816
817                 ldata->ld_leh->leh_len = sizeof(struct link_ea_header);
818                 ldata->ld_leh->leh_reccount = 0;
819                 linkea_first_entry(ldata);
820                 while (ldata->ld_lee != NULL &&
821                        (char *)ldata->ld_lee < (char *)ptr) {
822                         int reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
823                                      ldata->ld_lee->lee_reclen[1];
824
825                         ldata->ld_leh->leh_len += reclen;
826                         ldata->ld_leh->leh_reccount++;
827                         ldata->ld_lee = (struct link_ea_entry *)
828                                         ((char *)ldata->ld_lee + reclen);
829                 }
830
831                 ldata->ld_lee = NULL;
832         } else {
833                 linkea_del_buf(ldata, lname, false);
834         }
835 }
836
837 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
838                                                struct lu_name *cname,
839                                                struct lu_fid *pfid,
840                                                bool remove)
841 {
842         struct link_ea_entry    *oldlee;
843         int                      oldlen;
844         int                      repeated = 0;
845
846         oldlee = ldata->ld_lee;
847         oldlen = ldata->ld_reclen;
848         linkea_next_entry(ldata);
849         while (ldata->ld_lee != NULL) {
850                 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
851                                    ldata->ld_lee->lee_reclen[1];
852                 if (unlikely(!linkea_reclen_is_valid(ldata))) {
853                         lfsck_linkea_del_buf(ldata, NULL);
854                         LASSERT(ldata->ld_lee == NULL);
855                 } else if (unlikely(ldata->ld_reclen == oldlen &&
856                              memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
857                         repeated++;
858                         if (!remove)
859                                 break;
860
861                         lfsck_linkea_del_buf(ldata, cname);
862                 } else {
863                         linkea_next_entry(ldata);
864                 }
865         }
866         ldata->ld_lee = oldlee;
867         ldata->ld_reclen = oldlen;
868
869         return repeated;
870 }
871
872 /**
873  * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
874  *
875  * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
876  * with the given type to generate the name, the detailed rules for name
877  * have been described as following.
878  *
879  * The function also generates the linkEA corresponding to the name entry
880  * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
881  *
882  * \param[in] env       pointer to the thread context
883  * \param[in] com       pointer to the lfsck component
884  * \param[in] orphan    pointer to the orphan MDT-object
885  * \param[in] infix     additional information for the orphan name, such as
886  *                      the FID for original
887  * \param[in] type      the type for describing why the orphan MDT-object is
888  *                      created. The rules are as following:
889  *
890  *  type "D":           The MDT-object is a directory, it may knows its parent
891  *                      but because there is no valid linkEA, the LFSCK cannot
892  *                      know where to put it back to the namespace.
893  *  type "O":           The MDT-object has no linkEA, and there is no name
894  *                      entry that references the MDT-object.
895  *
896  *  type "S":           The orphan MDT-object is a shard of a striped directory
897  *
898  * \see lfsck_layout_recreate_parent() for more types.
899  *
900  * The orphan name will be like:
901  * ${FID}-${infix}-${type}-${conflict_version}
902  *
903  * \param[out] count    if some others inserted some linkEA entries by race,
904  *                      then return the linkEA entries count.
905  *
906  * \retval              positive number for repaired cases
907  * \retval              0 if needs to repair nothing
908  * \retval              negative error number on failure
909  */
910 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
911                                          struct lfsck_component *com,
912                                          struct dt_object *orphan,
913                                          const char *infix, const char *type,
914                                          int *count)
915 {
916         struct lfsck_thread_info        *info   = lfsck_env_info(env);
917         struct lu_name                  *cname  = &info->lti_name;
918         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
919         struct lu_attr                  *la     = &info->lti_la2;
920         const struct lu_fid             *cfid   = lfsck_dto2fid(orphan);
921         const struct lu_fid             *pfid;
922         struct lu_fid                    tfid;
923         struct lfsck_instance           *lfsck  = com->lc_lfsck;
924         struct dt_device                *dev    = lfsck_obj2dev(orphan);
925         struct dt_object                *parent;
926         struct thandle                  *th     = NULL;
927         struct lfsck_lock_handle        *pllh   = &info->lti_llh;
928         struct lustre_handle             clh    = { 0 };
929         struct linkea_data               ldata2 = { NULL };
930         struct lu_buf                    linkea_buf;
931         int                              namelen;
932         int                              idx    = 0;
933         int                              rc     = 0;
934         bool                             exist  = false;
935         ENTRY;
936
937         cname->ln_name = NULL;
938         if (unlikely(lfsck->li_lpf_obj == NULL))
939                 GOTO(log, rc = -ENXIO);
940
941         parent = lfsck->li_lpf_obj;
942         pfid = lfsck_dto2fid(parent);
943
944 again:
945         do {
946                 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
947                                    PFID(cfid), infix, type, idx++);
948                 rc = dt_lookup_dir(env, parent, info->lti_key, &tfid);
949                 if (rc != 0 && rc != -ENOENT)
950                         GOTO(log, rc);
951
952                 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
953                         exist = true;
954         } while (rc == 0 && !exist);
955
956         rc = lfsck_lock(env, lfsck, parent, info->lti_key, pllh,
957                         MDS_INODELOCK_UPDATE, LCK_PW);
958         if (rc != 0)
959                 GOTO(log, rc);
960
961         /* Re-check whether the name conflict with othrs after taken
962          * the ldlm lock. */
963         rc = dt_lookup_dir(env, parent, info->lti_key, &tfid);
964         if (rc == 0) {
965                 if (!lu_fid_eq(cfid, &tfid)) {
966                         exist = false;
967                         lfsck_unlock(pllh);
968                         goto again;
969                 }
970
971                 exist = true;
972         } else if (rc != -ENOENT) {
973                 GOTO(log, rc);
974         } else {
975                 exist = false;
976         }
977
978         cname->ln_name = info->lti_key;
979         cname->ln_namelen = namelen;
980         rc = linkea_links_new(&ldata2, &info->lti_linkea_buf2,
981                               cname, pfid);
982         if (rc != 0)
983                 GOTO(log, rc);
984
985         rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
986                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP |
987                               MDS_INODELOCK_XATTR, LCK_EX);
988         if (rc != 0)
989                 GOTO(log, rc);
990
991         lfsck_buf_init(&linkea_buf, ldata2.ld_buf->lb_buf,
992                        ldata2.ld_leh->leh_len);
993         th = dt_trans_create(env, dev);
994         if (IS_ERR(th))
995                 GOTO(log, rc = PTR_ERR(th));
996
997         if (S_ISDIR(lfsck_object_type(orphan))) {
998                 rc = dt_declare_delete(env, orphan,
999                                        (const struct dt_key *)dotdot, th);
1000                 if (rc != 0)
1001                         GOTO(stop, rc);
1002
1003                 rec->rec_type = S_IFDIR;
1004                 rec->rec_fid = pfid;
1005                 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1006                                        (const struct dt_key *)dotdot, th);
1007                 if (rc != 0)
1008                         GOTO(stop, rc);
1009         }
1010
1011         rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1012                                   XATTR_NAME_LINK, 0, th);
1013         if (rc != 0)
1014                 GOTO(stop, rc);
1015
1016         if (!exist) {
1017                 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1018                 rec->rec_fid = cfid;
1019                 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1020                                        (const struct dt_key *)cname->ln_name,
1021                                        th);
1022                 if (rc != 0)
1023                         GOTO(stop, rc);
1024
1025                 if (S_ISDIR(rec->rec_type)) {
1026                         rc = dt_declare_ref_add(env, parent, th);
1027                         if (rc != 0)
1028                                 GOTO(stop, rc);
1029                 }
1030         }
1031
1032         memset(la, 0, sizeof(*la));
1033         la->la_ctime = ktime_get_real_seconds();
1034         la->la_valid = LA_CTIME;
1035         rc = dt_declare_attr_set(env, orphan, la, th);
1036         if (rc != 0)
1037                 GOTO(stop, rc);
1038
1039         rc = dt_trans_start_local(env, dev, th);
1040         if (rc != 0)
1041                 GOTO(stop, rc);
1042
1043         dt_write_lock(env, orphan, 0);
1044         rc = lfsck_links_read2_with_rec(env, orphan, &ldata2);
1045         if (likely(rc == -ENODATA || rc == -EINVAL)) {
1046                 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1047                         GOTO(unlock, rc = 1);
1048
1049                 if (S_ISDIR(lfsck_object_type(orphan))) {
1050                         rc = dt_delete(env, orphan,
1051                                        (const struct dt_key *)dotdot, th);
1052                         if (rc != 0)
1053                                 GOTO(unlock, rc);
1054
1055                         rec->rec_type = S_IFDIR;
1056                         rec->rec_fid = pfid;
1057                         rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1058                                        (const struct dt_key *)dotdot, th);
1059                         if (rc != 0)
1060                                 GOTO(unlock, rc);
1061                 }
1062
1063                 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1064                                   th);
1065         } else {
1066                 if (rc == 0 && count != NULL)
1067                         *count = ldata2.ld_leh->leh_reccount;
1068
1069                 GOTO(unlock, rc);
1070         }
1071         dt_write_unlock(env, orphan);
1072
1073         if (rc == 0 && !exist) {
1074                 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1075                 rec->rec_fid = cfid;
1076                 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1077                                (const struct dt_key *)cname->ln_name, th);
1078                 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1079                         dt_write_lock(env, parent, 0);
1080                         rc = dt_ref_add(env, parent, th);
1081                         dt_write_unlock(env, parent);
1082                 }
1083         }
1084
1085         if (rc == 0)
1086                 rc = dt_attr_set(env, orphan, la, th);
1087
1088         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1089
1090 unlock:
1091         dt_write_unlock(env, orphan);
1092
1093 stop:
1094         dt_trans_stop(env, dev, th);
1095
1096 log:
1097         lfsck_ibits_unlock(&clh, LCK_EX);
1098         lfsck_unlock(pllh);
1099         CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1100                "object "DFID", name = %s: rc = %d\n",
1101                lfsck_lfsck2name(lfsck), PFID(cfid),
1102                cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1103
1104         if (rc != 0) {
1105                 struct lfsck_namespace *ns = com->lc_file_ram;
1106
1107                 ns->ln_flags |= LF_INCONSISTENT;
1108         }
1109
1110         return rc;
1111 }
1112
1113 static int lfsck_lmv_set(const struct lu_env *env,
1114                          struct lfsck_instance *lfsck,
1115                          struct dt_object *obj,
1116                          struct lmv_mds_md_v1 *lmv)
1117 {
1118         struct dt_device *dev = lfsck->li_next;
1119         struct thandle *th = NULL;
1120         struct lu_buf buf = { lmv, sizeof(*lmv) };
1121         int rc;
1122
1123         ENTRY;
1124
1125         th = dt_trans_create(env, dev);
1126         if (IS_ERR(th))
1127                 RETURN(PTR_ERR(th));
1128
1129         rc = dt_declare_xattr_set(env, obj, &buf, XATTR_NAME_LMV, 0, th);
1130         if (rc)
1131                 GOTO(stop, rc);
1132
1133         rc = dt_trans_start_local(env, dev, th);
1134         if (rc != 0)
1135                 GOTO(stop, rc);
1136
1137         rc = dt_xattr_set(env, obj, &buf, XATTR_NAME_LMV, 0, th);
1138         if (rc)
1139                 GOTO(stop, rc);
1140
1141         EXIT;
1142 stop:
1143         dt_trans_stop(env, dev, th);
1144
1145         return rc;
1146 }
1147
1148 static int lfsck_lmv_delete(const struct lu_env *env,
1149                             struct lfsck_instance *lfsck,
1150                             struct dt_object *obj)
1151 {
1152         struct dt_device *dev = lfsck->li_next;
1153         struct thandle *th = NULL;
1154         int rc;
1155
1156         ENTRY;
1157
1158         th = dt_trans_create(env, dev);
1159         if (IS_ERR(th))
1160                 RETURN(PTR_ERR(th));
1161
1162         rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LMV, th);
1163         if (rc)
1164                 GOTO(stop, rc);
1165
1166         rc = dt_trans_start_local(env, dev, th);
1167         if (rc != 0)
1168                 GOTO(stop, rc);
1169
1170         rc = dt_xattr_del(env, obj, XATTR_NAME_LMV, th);
1171         if (rc)
1172                 GOTO(stop, rc);
1173
1174         EXIT;
1175 stop:
1176         dt_trans_stop(env, dev, th);
1177
1178         return rc;
1179 }
1180
1181 static inline int lfsck_object_is_shard(const struct lu_env *env,
1182                                         struct lfsck_instance *lfsck,
1183                                         struct dt_object *obj,
1184                                         const struct lu_name *lname)
1185 {
1186         struct lfsck_thread_info *info = lfsck_env_info(env);
1187         struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
1188         int rc;
1189
1190         rc = lfsck_shard_name_to_index(env, lname->ln_name, lname->ln_namelen,
1191                                        lfsck_object_type(obj),
1192                                        lfsck_dto2fid(obj));
1193         if (rc < 0)
1194                 return 0;
1195
1196         rc = lfsck_read_stripe_lmv(env, lfsck, obj, lmv);
1197         if (rc == -ENODATA)
1198                 return 0;
1199
1200         if (!rc && lmv->lmv_magic == LMV_MAGIC_STRIPE)
1201                 return 1;
1202
1203         return rc;
1204 }
1205
1206 /**
1207  * Add the specified name entry back to namespace.
1208  *
1209  * If there is a linkEA entry that back references a name entry under
1210  * some parent directory, but such parent directory does not have the
1211  * claimed name entry. On the other hand, the linkEA entries count is
1212  * not larger than the MDT-object's hard link count. Under such case,
1213  * it is quite possible that the name entry is lost. Then the LFSCK
1214  * should add the name entry back to the namespace.
1215  *
1216  * If \a child is shard, which means \a parent is a striped directory,
1217  * if \a parent has LMV, we need to delete it before insertion because
1218  * now parent's striping is broken and can't be parsed correctly.
1219  *
1220  * \param[in] env       pointer to the thread context
1221  * \param[in] com       pointer to the lfsck component
1222  * \param[in] parent    pointer to the directory under which the name entry
1223  *                      will be inserted into
1224  * \param[in] child     pointer to the object referenced by the name entry
1225  *                      that to be inserted into the parent
1226  * \param[in] lname     the name for the child in the parent directory
1227  *
1228  * \retval              positive number for repaired cases
1229  * \retval              0 if nothing to be repaired
1230  * \retval              negative error number on failure
1231  */
1232 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1233                                          struct lfsck_component *com,
1234                                          struct dt_object *parent,
1235                                          struct dt_object *child,
1236                                          const struct lu_name *lname)
1237 {
1238         struct lfsck_thread_info *info = lfsck_env_info(env);
1239         struct lu_attr *la = &info->lti_la;
1240         struct dt_insert_rec *rec = &info->lti_dt_rec;
1241         struct lfsck_instance *lfsck = com->lc_lfsck;
1242         /* The child and its name may be on different MDTs. */
1243         const struct lu_fid *pfid = lfsck_dto2fid(parent);
1244         const struct lu_fid *cfid = lfsck_dto2fid(child);
1245         struct dt_device *dev = lfsck->li_next;
1246         struct thandle *th = NULL;
1247         struct lfsck_lock_handle *llh = &info->lti_llh;
1248         struct lmv_mds_md_v1 *lmv = &info->lti_lmv;
1249         struct lu_buf buf = { lmv, sizeof(*lmv) };
1250         /* whether parent's LMV is deleted before insertion */
1251         bool parent_lmv_deleted = false;
1252         /* whether parent's LMV is missing */
1253         bool parent_lmv_lost = false;
1254         int rc = 0;
1255
1256         ENTRY;
1257
1258         /* @parent/@child may be based on lfsck->li_bottom,
1259          * but here we need the object based on the lfsck->li_next. */
1260
1261         parent = lfsck_object_locate(dev, parent);
1262         if (IS_ERR(parent))
1263                 GOTO(log, rc = PTR_ERR(parent));
1264
1265         child = lfsck_object_locate(dev, child);
1266         if (IS_ERR(child))
1267                 GOTO(log, rc = PTR_ERR(child));
1268
1269         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1270                 GOTO(log, rc = 1);
1271
1272         rc = lfsck_lock(env, lfsck, parent, lname->ln_name, llh,
1273                         MDS_INODELOCK_LOOKUP | MDS_INODELOCK_UPDATE |
1274                         MDS_INODELOCK_XATTR, LCK_EX);
1275         if (rc)
1276                 GOTO(log, rc);
1277
1278         rc = lfsck_object_is_shard(env, lfsck, child, lname);
1279         if (rc < 0)
1280                 GOTO(unlock, rc);
1281
1282         if (rc == 1) {
1283                 rc = lfsck_read_stripe_lmv(env, lfsck, parent, lmv);
1284                 if (!rc) {
1285                         /*
1286                          * To add a shard, we need to convert parent to a
1287                          * plain directory by deleting its LMV, and after
1288                          * insertion set it back.
1289                          */
1290                         rc = lfsck_lmv_delete(env, lfsck, parent);
1291                         if (rc)
1292                                 GOTO(unlock, rc);
1293                         parent_lmv_deleted = true;
1294                         lmv->lmv_layout_version++;
1295                         lfsck_lmv_header_cpu_to_le(lmv, lmv);
1296                 } else if (rc == -ENODATA) {
1297                         struct lu_seq_range *range = &info->lti_range;
1298                         struct seq_server_site *ss = lfsck_dev_site(lfsck);
1299
1300                         rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
1301                         if (rc)
1302                                 GOTO(unlock, rc);
1303
1304                         fld_range_set_mdt(range);
1305                         rc = fld_server_lookup(env, ss->ss_server_fld,
1306                                        fid_seq(lfsck_dto2fid(parent)), range);
1307                         if (rc)
1308                                 GOTO(unlock, rc);
1309
1310                         parent_lmv_lost = true;
1311                         lmv->lmv_magic = LMV_MAGIC;
1312                         lmv->lmv_master_mdt_index = range->lsr_index;
1313                         lmv->lmv_layout_version++;
1314                         lfsck_lmv_header_cpu_to_le(lmv, lmv);
1315                 } else {
1316                         GOTO(unlock, rc);
1317                 }
1318         }
1319
1320         if (unlikely(!dt_try_as_dir(env, parent)))
1321                 GOTO(unlock, rc = -ENOTDIR);
1322
1323         th = dt_trans_create(env, dev);
1324         if (IS_ERR(th))
1325                 GOTO(unlock, rc = PTR_ERR(th));
1326
1327         rec->rec_type = lfsck_object_type(child) & S_IFMT;
1328         rec->rec_fid = cfid;
1329         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1330                                (const struct dt_key *)lname->ln_name, th);
1331         if (rc != 0)
1332                 GOTO(stop, rc);
1333
1334         if (S_ISDIR(rec->rec_type)) {
1335                 rc = dt_declare_ref_add(env, parent, th);
1336                 if (rc != 0)
1337                         GOTO(stop, rc);
1338         }
1339
1340         if (parent_lmv_lost) {
1341                 rc = dt_declare_xattr_set(env, parent, &buf, XATTR_NAME_LMV,
1342                                           0, th);
1343                 if (rc)
1344                         GOTO(stop, rc);
1345         }
1346
1347         la->la_ctime = ktime_get_real_seconds();
1348         la->la_valid = LA_CTIME;
1349         rc = dt_declare_attr_set(env, parent, la, th);
1350         if (rc != 0)
1351                 GOTO(stop, rc);
1352
1353         rc = dt_declare_attr_set(env, child, la, th);
1354         if (rc != 0)
1355                 GOTO(stop, rc);
1356
1357         rc = dt_trans_start_local(env, dev, th);
1358         if (rc != 0)
1359                 GOTO(stop, rc);
1360
1361         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1362                        (const struct dt_key *)lname->ln_name, th);
1363         if (rc != 0)
1364                 GOTO(stop, rc);
1365
1366         if (S_ISDIR(rec->rec_type)) {
1367                 dt_write_lock(env, parent, 0);
1368                 rc = dt_ref_add(env, parent, th);
1369                 dt_write_unlock(env, parent);
1370                 if (rc != 0)
1371                         GOTO(stop, rc);
1372         }
1373
1374         if (parent_lmv_lost) {
1375                 rc = dt_xattr_set(env, parent, &buf, XATTR_NAME_LMV, 0, th);
1376                 if (rc)
1377                         GOTO(stop, rc);
1378         }
1379
1380         rc = dt_attr_set(env, parent, la, th);
1381         if (rc != 0)
1382                 GOTO(stop, rc);
1383
1384         rc = dt_attr_set(env, child, la, th);
1385
1386         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1387
1388 stop:
1389         dt_trans_stop(env, dev, th);
1390
1391 unlock:
1392         if (parent_lmv_deleted)
1393                 lfsck_lmv_set(env, lfsck, parent, lmv);
1394
1395         lfsck_unlock(llh);
1396
1397 log:
1398         CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1399                "the name %s and type %o to the parent "DFID": rc = %d\n",
1400                lfsck_lfsck2name(lfsck), PFID(cfid), lname->ln_name,
1401                lfsck_object_type(child) & S_IFMT, PFID(pfid), rc);
1402
1403         if (rc != 0) {
1404                 struct lfsck_namespace *ns = com->lc_file_ram;
1405
1406                 ns->ln_flags |= LF_INCONSISTENT;
1407                 if (rc > 0)
1408                         ns->ln_lost_dirent_repaired++;
1409         }
1410
1411         return rc;
1412 }
1413
1414 /**
1415  * Create the specified orphan directory.
1416  *
1417  * For the case that the parent MDT-object stored in some MDT-object's
1418  * linkEA entry is lost, the LFSCK will re-create the parent object as
1419  * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1420  * with the name ${FID}-P-${conflict_version}.
1421  *
1422  * \param[in] env       pointer to the thread context
1423  * \param[in] com       pointer to the lfsck component
1424  * \param[in] orphan    pointer to the orphan MDT-object to be created
1425  * \param[in] lmv       pointer to master LMV EA that will be set to the orphan
1426  *
1427  * \retval              positive number for repaired cases
1428  * \retval              negative error number on failure
1429  */
1430 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1431                                              struct lfsck_component *com,
1432                                              struct dt_object *orphan,
1433                                              struct lmv_mds_md_v1 *lmv)
1434 {
1435         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1436         struct lu_attr                  *la     = &info->lti_la;
1437         struct dt_allocation_hint       *hint   = &info->lti_hint;
1438         struct dt_object_format         *dof    = &info->lti_dof;
1439         struct lu_name                  *cname  = &info->lti_name2;
1440         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
1441         struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
1442         const struct lu_fid             *cfid   = lfsck_dto2fid(orphan);
1443         struct lu_fid                    tfid;
1444         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1445         struct lfsck_namespace          *ns     = com->lc_file_ram;
1446         struct dt_device                *dev    = lfsck_obj2dev(orphan);
1447         struct dt_object                *parent = NULL;
1448         struct thandle                  *th     = NULL;
1449         struct lfsck_lock_handle        *llh    = &info->lti_llh;
1450         struct linkea_data               ldata  = { NULL };
1451         struct lu_buf                    linkea_buf;
1452         struct lu_buf                    lmv_buf;
1453         char                             name[32];
1454         int                              namelen;
1455         int                              idx    = 0;
1456         int                              rc     = 0;
1457         int                              rc1    = 0;
1458         ENTRY;
1459
1460         LASSERT(!dt_object_exists(orphan));
1461
1462         cname->ln_name = NULL;
1463         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1464                 GOTO(log, rc = 1);
1465
1466         if (dt_object_remote(orphan)) {
1467                 LASSERT(lfsck->li_lpf_root_obj != NULL);
1468
1469                 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1470                 if (idx < 0)
1471                         GOTO(log, rc = idx);
1472
1473                 snprintf(name, 8, "MDT%04x", idx);
1474                 rc = dt_lookup_dir(env, lfsck->li_lpf_root_obj, name, &tfid);
1475                 if (rc != 0)
1476                         GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1477
1478                 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1479                 if (IS_ERR(parent))
1480                         GOTO(log, rc = PTR_ERR(parent));
1481
1482                 if (unlikely(!dt_try_as_dir(env, parent)))
1483                         GOTO(log, rc = -ENOTDIR);
1484         } else {
1485                 if (unlikely(lfsck->li_lpf_obj == NULL))
1486                         GOTO(log, rc = -ENXIO);
1487
1488                 parent = lfsck->li_lpf_obj;
1489         }
1490
1491         dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1492         if (IS_ERR(dev))
1493                 GOTO(log, rc = PTR_ERR(dev));
1494
1495         idx = 0;
1496
1497 again:
1498         do {
1499                 namelen = snprintf(name, 31, DFID"-P-%d",
1500                                    PFID(cfid), idx++);
1501                 rc = dt_lookup_dir(env, parent, name, &tfid);
1502                 if (rc != 0 && rc != -ENOENT)
1503                         GOTO(log, rc);
1504         } while (rc == 0);
1505
1506         rc = lfsck_lock(env, lfsck, parent, name, llh,
1507                         MDS_INODELOCK_UPDATE, LCK_PW);
1508         if (rc != 0)
1509                 GOTO(log, rc);
1510
1511         /* Re-check whether the name conflict with othrs after taken
1512          * the ldlm lock. */
1513         rc = dt_lookup_dir(env, parent, name, &tfid);
1514         if (unlikely(rc == 0)) {
1515                 lfsck_unlock(llh);
1516                 goto again;
1517         }
1518
1519         if (rc != -ENOENT)
1520                 GOTO(unlock1, rc);
1521
1522         cname->ln_name = name;
1523         cname->ln_namelen = namelen;
1524
1525         memset(la, 0, sizeof(*la));
1526         la->la_mode = S_IFDIR | 0700;
1527         la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1528                        LA_ATIME | LA_MTIME | LA_CTIME;
1529
1530         orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1531                                    la->la_mode & S_IFMT);
1532
1533         memset(dof, 0, sizeof(*dof));
1534         dof->dof_type = dt_mode_to_dft(S_IFDIR);
1535
1536         rc = linkea_links_new(&ldata, &info->lti_linkea_buf2,
1537                               cname, lfsck_dto2fid(parent));
1538         if (rc != 0)
1539                 GOTO(unlock1, rc);
1540
1541         th = dt_trans_create(env, dev);
1542         if (IS_ERR(th))
1543                 GOTO(unlock1, rc = PTR_ERR(th));
1544
1545         /* Sync the remote transaction to guarantee that the subsequent
1546          * lock against the @orphan can find the @orphan in time. */
1547         if (dt_object_remote(orphan))
1548                 th->th_sync = 1;
1549
1550         rc = dt_declare_create(env, orphan, la, hint, dof, th);
1551         if (rc != 0)
1552                 GOTO(stop, rc);
1553
1554         if (unlikely(!dt_try_as_dir(env, orphan)))
1555                 GOTO(stop, rc = -ENOTDIR);
1556
1557         rc = dt_declare_ref_add(env, orphan, th);
1558         if (rc != 0)
1559                 GOTO(stop, rc);
1560
1561         rec->rec_type = S_IFDIR;
1562         rec->rec_fid = cfid;
1563         rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1564                                (const struct dt_key *)dot, th);
1565         if (rc != 0)
1566                 GOTO(stop, rc);
1567
1568         rec->rec_fid = lfsck_dto2fid(parent);
1569         rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1570                                (const struct dt_key *)dotdot, th);
1571         if (rc != 0)
1572                 GOTO(stop, rc);
1573
1574         if (lmv != NULL) {
1575                 lmv->lmv_magic = LMV_MAGIC;
1576                 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1577                 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1578                 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1579                 rc = dt_declare_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV,
1580                                           0, th);
1581                 if (rc != 0)
1582                         GOTO(stop, rc);
1583         }
1584
1585         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1586                        ldata.ld_leh->leh_len);
1587         rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1588                                   XATTR_NAME_LINK, 0, th);
1589         if (rc != 0)
1590                 GOTO(stop, rc);
1591
1592         rec->rec_fid = cfid;
1593         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1594                                (const struct dt_key *)name, th);
1595         if (rc == 0)
1596                 rc = dt_declare_ref_add(env, parent, th);
1597
1598         if (rc != 0)
1599                 GOTO(stop, rc);
1600
1601         rc = dt_trans_start_local(env, dev, th);
1602         if (rc != 0)
1603                 GOTO(stop, rc);
1604
1605         dt_write_lock(env, orphan, 0);
1606         rc = dt_create(env, orphan, la, hint, dof, th);
1607         if (rc != 0)
1608                 GOTO(unlock2, rc);
1609
1610         rc = dt_ref_add(env, orphan, th);
1611         if (rc != 0)
1612                 GOTO(unlock2, rc);
1613
1614         rec->rec_fid = cfid;
1615         rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1616                        (const struct dt_key *)dot, th);
1617         if (rc != 0)
1618                 GOTO(unlock2, rc);
1619
1620         rec->rec_fid = lfsck_dto2fid(parent);
1621         rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1622                        (const struct dt_key *)dotdot, th);
1623         if (rc != 0)
1624                 GOTO(unlock2, rc);
1625
1626         if (lmv != NULL) {
1627                 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0, th);
1628                 if (rc != 0)
1629                         GOTO(unlock2, rc);
1630         }
1631
1632         rc = dt_xattr_set(env, orphan, &linkea_buf,
1633                           XATTR_NAME_LINK, 0, th);
1634         dt_write_unlock(env, orphan);
1635         if (rc != 0)
1636                 GOTO(stop, rc);
1637
1638         rec->rec_fid = cfid;
1639         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1640                        (const struct dt_key *)name, th);
1641         if (rc == 0) {
1642                 dt_write_lock(env, parent, 0);
1643                 rc = dt_ref_add(env, parent, th);
1644                 dt_write_unlock(env, parent);
1645         }
1646
1647         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1648
1649 unlock2:
1650         dt_write_unlock(env, orphan);
1651
1652 stop:
1653         rc1 = dt_trans_stop(env, dev, th);
1654         if (rc1 != 0 && rc > 0)
1655                 rc = rc1;
1656
1657 unlock1:
1658         lfsck_unlock(llh);
1659
1660 log:
1661         CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1662                "the object "DFID", name = %s: rc = %d\n",
1663                lfsck_lfsck2name(lfsck), PFID(cfid),
1664                cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1665
1666         if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1667                 lfsck_object_put(env, parent);
1668
1669         if (rc != 0)
1670                 ns->ln_flags |= LF_INCONSISTENT;
1671
1672         return rc;
1673 }
1674
1675 /**
1676  * Remove the specified entry from the linkEA.
1677  *
1678  * Locate the linkEA entry with the given @cname and @pfid, then
1679  * remove this entry or the other entries those are repeated with
1680  * this entry.
1681  *
1682  * \param[in] env       pointer to the thread context
1683  * \param[in] com       pointer to the lfsck component
1684  * \param[in] obj       pointer to the dt_object to be handled
1685  * \param[in,out]ldata  pointer to the buffer that holds the linkEA
1686  * \param[in] cname     the name for the child in the parent directory
1687  * \param[in] pfid      the parent directory's FID for the linkEA
1688  * \param[in] next      if true, then remove the first found linkEA
1689  *                      entry, and move the ldata->ld_lee to next entry
1690  *
1691  * \retval              positive number for repaired cases
1692  * \retval              0 if nothing to be repaired
1693  * \retval              negative error number on failure
1694  */
1695 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1696                                          struct lfsck_component *com,
1697                                          struct dt_object *obj,
1698                                          struct linkea_data *ldata,
1699                                          struct lu_name *cname,
1700                                          struct lu_fid *pfid,
1701                                          bool next)
1702 {
1703         struct lfsck_instance           *lfsck     = com->lc_lfsck;
1704         struct dt_device                *dev       = lfsck_obj2dev(obj);
1705         struct lfsck_bookmark           *bk        = &lfsck->li_bookmark_ram;
1706         struct thandle                  *th        = NULL;
1707         struct lustre_handle             lh        = { 0 };
1708         struct linkea_data               ldata_new = { NULL };
1709         struct lu_buf                    linkea_buf;
1710         int                              buflen    = 0;
1711         int                              rc        = 0;
1712         ENTRY;
1713
1714         rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1715                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
1716                               LCK_EX);
1717         if (rc != 0)
1718                 GOTO(log, rc);
1719
1720         if (next)
1721                 lfsck_linkea_del_buf(ldata, cname);
1722         else
1723                 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1724                                                     true);
1725         if (ldata->ld_leh->leh_reccount > 0 ||
1726             unlikely(ldata->ld_leh->leh_overflow_time)) {
1727                 lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1728                                ldata->ld_leh->leh_len);
1729                 buflen = linkea_buf.lb_len;
1730         }
1731
1732 again:
1733         th = dt_trans_create(env, dev);
1734         if (IS_ERR(th))
1735                 GOTO(unlock1, rc = PTR_ERR(th));
1736
1737         if (buflen != 0)
1738                 rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1739                                           XATTR_NAME_LINK, 0, th);
1740         else
1741                 rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
1742         if (rc != 0)
1743                 GOTO(stop, rc);
1744
1745         rc = dt_trans_start_local(env, dev, th);
1746         if (rc != 0)
1747                 GOTO(stop, rc);
1748
1749         dt_write_lock(env, obj, 0);
1750         if (unlikely(lfsck_is_dead_obj(obj)))
1751                 GOTO(unlock2, rc = -ENOENT);
1752
1753         rc = lfsck_links_read2_with_rec(env, obj, &ldata_new);
1754         if (rc)
1755                 GOTO(unlock2, rc = (rc == -ENODATA ? 0 : rc));
1756
1757         /* The specified linkEA entry has been removed by race. */
1758         rc = linkea_links_find(&ldata_new, cname, pfid);
1759         if (rc != 0)
1760                 GOTO(unlock2, rc = 0);
1761
1762         if (bk->lb_param & LPF_DRYRUN)
1763                 GOTO(unlock2, rc = 1);
1764
1765         if (next)
1766                 lfsck_linkea_del_buf(&ldata_new, cname);
1767         else
1768                 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1769                                                     true);
1770
1771         /*
1772          * linkea may change because it doesn't take lock in the first read, if
1773          * it becomes larger, restart from beginning.
1774          */
1775         if ((ldata_new.ld_leh->leh_reccount > 0 ||
1776              unlikely(ldata_new.ld_leh->leh_overflow_time)) &&
1777             buflen < ldata_new.ld_leh->leh_len) {
1778                 dt_write_unlock(env, obj);
1779                 dt_trans_stop(env, dev, th);
1780                 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1781                                ldata_new.ld_leh->leh_len);
1782                 buflen = linkea_buf.lb_len;
1783                 goto again;
1784         }
1785
1786         if (buflen)
1787                 rc = lfsck_links_write(env, obj, &ldata_new, th);
1788         else
1789                 rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th);
1790
1791         GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1792
1793 unlock2:
1794         dt_write_unlock(env, obj);
1795
1796 stop:
1797         dt_trans_stop(env, dev, th);
1798
1799 unlock1:
1800         lfsck_ibits_unlock(&lh, LCK_EX);
1801
1802 log:
1803         CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1804                "for the object: "DFID", parent "DFID", name %.*s\n",
1805                lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1806                PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1807                cname->ln_name);
1808
1809         if (rc != 0) {
1810                 struct lfsck_namespace *ns = com->lc_file_ram;
1811
1812                 ns->ln_flags |= LF_INCONSISTENT;
1813         }
1814
1815         return rc;
1816 }
1817
1818 /**
1819  * Conditionally remove the specified entry from the linkEA.
1820  *
1821  * Take the parent lock firstly, then check whether the specified
1822  * name entry exists or not: if yes, do nothing; otherwise, call
1823  * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1824  *
1825  * \param[in] env       pointer to the thread context
1826  * \param[in] com       pointer to the lfsck component
1827  * \param[in] parent    pointer to the parent directory
1828  * \param[in] child     pointer to the child object that holds the linkEA
1829  * \param[in,out]ldata  pointer to the buffer that holds the linkEA
1830  * \param[in] cname     the name for the child in the parent directory
1831  * \param[in] pfid      the parent directory's FID for the linkEA
1832  *
1833  * \retval              positive number for repaired cases
1834  * \retval              0 if nothing to be repaired
1835  * \retval              negative error number on failure
1836  */
1837 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1838                                               struct lfsck_component *com,
1839                                               struct dt_object *parent,
1840                                               struct dt_object *child,
1841                                               struct linkea_data *ldata,
1842                                               struct lu_name *cname,
1843                                               struct lu_fid *pfid)
1844 {
1845         struct lfsck_thread_info *info  = lfsck_env_info(env);
1846         struct lu_fid            *cfid  = &info->lti_fid3;
1847         struct lfsck_lock_handle *llh   = &info->lti_llh;
1848         int                       rc;
1849         ENTRY;
1850
1851         rc = lfsck_lock(env, com->lc_lfsck, parent, cname->ln_name, llh,
1852                         MDS_INODELOCK_UPDATE, LCK_PR);
1853         if (rc != 0)
1854                 RETURN(rc);
1855
1856         dt_read_lock(env, parent, 0);
1857         if (unlikely(lfsck_is_dead_obj(parent))) {
1858                 dt_read_unlock(env, parent);
1859                 lfsck_unlock(llh);
1860                 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1861                                                    cname, pfid, true);
1862
1863                 RETURN(rc);
1864         }
1865
1866         rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1867                        (const struct dt_key *)cname->ln_name);
1868         dt_read_unlock(env, parent);
1869
1870         /* It is safe to release the ldlm lock, because when the logic come
1871          * here, we have got all the needed information above whether the
1872          * linkEA entry is valid or not. It is not important that others
1873          * may add new linkEA entry after the ldlm lock released. If other
1874          * has removed the specified linkEA entry by race, then it is OK,
1875          * because the subsequent lfsck_namespace_shrink_linkea() can handle
1876          * such case. */
1877         lfsck_unlock(llh);
1878         if (rc == -ENOENT) {
1879                 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1880                                                    cname, pfid, true);
1881
1882                 RETURN(rc);
1883         }
1884
1885         if (rc != 0)
1886                 RETURN(rc);
1887
1888         /* The LFSCK just found some internal status of cross-MDTs
1889          * create operation. That is normal. */
1890         if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1891                 linkea_next_entry(ldata);
1892
1893                 RETURN(0);
1894         }
1895
1896         rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1897                                            pfid, true);
1898
1899         RETURN(rc);
1900 }
1901
1902 /**
1903  * Conditionally replace name entry in the parent.
1904  *
1905  * As required, the LFSCK may re-create the lost MDT-object for dangling
1906  * name entry, but such repairing may be wrong because of bad FID in the
1907  * name entry. As the LFSCK processing, the real MDT-object may be found,
1908  * then the LFSCK should check whether the former re-created MDT-object
1909  * has been modified or not, if not, then destroy it and update the name
1910  * entry in the parent to reference the real MDT-object.
1911  *
1912  * \param[in] env       pointer to the thread context
1913  * \param[in] com       pointer to the lfsck component
1914  * \param[in] parent    pointer to the parent directory
1915  * \param[in] child     pointer to the MDT-object that may be the real
1916  *                      MDT-object corresponding to the name entry in parent
1917  * \param[in] cfid      the current FID in the name entry
1918  * \param[in] cname     contains the name of the child in the parent directory
1919  *
1920  * \retval              positive number for repaired cases
1921  * \retval              0 if nothing to be repaired
1922  * \retval              negative error number on failure
1923  */
1924 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1925                                         struct lfsck_component *com,
1926                                         struct dt_object *parent,
1927                                         struct dt_object *child,
1928                                         const struct lu_fid *cfid,
1929                                         const struct lu_name *cname)
1930 {
1931         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1932         struct lu_attr                  *la     = &info->lti_la;
1933         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
1934         struct lu_fid                    tfid;
1935         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1936         /* The child and its name may be on different MDTs. */
1937         struct dt_device                *dev    = lfsck->li_next;
1938         const char                      *name   = cname->ln_name;
1939         const struct lu_fid             *pfid   = lfsck_dto2fid(parent);
1940         struct dt_object                *cobj   = NULL;
1941         struct lfsck_lock_handle        *pllh   = &info->lti_llh;
1942         struct lustre_handle             clh    = { 0 };
1943         struct linkea_data               ldata  = { NULL };
1944         struct thandle                  *th     = NULL;
1945         bool                             exist  = true;
1946         int                              rc     = 0;
1947         ENTRY;
1948
1949         /* @parent/@child may be based on lfsck->li_bottom,
1950          * but here we need the object based on the lfsck->li_next. */
1951
1952         parent = lfsck_object_locate(dev, parent);
1953         if (IS_ERR(parent))
1954                 GOTO(log, rc = PTR_ERR(parent));
1955
1956         if (unlikely(!dt_try_as_dir(env, parent)))
1957                 GOTO(log, rc = -ENOTDIR);
1958
1959         rc = lfsck_lock(env, lfsck, parent, name, pllh,
1960                         MDS_INODELOCK_UPDATE, LCK_PW);
1961         if (rc != 0)
1962                 GOTO(log, rc);
1963
1964         if (!fid_is_sane(cfid)) {
1965                 exist = false;
1966                 goto replace;
1967         }
1968
1969         cobj = lfsck_object_find_by_dev(env, dev, cfid);
1970         if (IS_ERR(cobj)) {
1971                 rc = PTR_ERR(cobj);
1972                 if (rc == -ENOENT) {
1973                         exist = false;
1974                         goto replace;
1975                 }
1976
1977                 GOTO(log, rc);
1978         }
1979
1980         if (!dt_object_exists(cobj)) {
1981                 exist = false;
1982                 goto replace;
1983         }
1984
1985         rc = dt_lookup_dir(env, parent, name, &tfid);
1986         if (rc == -ENOENT) {
1987                 exist = false;
1988                 goto replace;
1989         }
1990
1991         if (rc != 0)
1992                 GOTO(log, rc);
1993
1994         /* Someone changed the name entry, cannot replace it. */
1995         if (!lu_fid_eq(cfid, &tfid))
1996                 GOTO(log, rc = 0);
1997
1998         /* lock the object to be destroyed. */
1999         rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
2000                               MDS_INODELOCK_UPDATE |
2001                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_XATTR,
2002                               LCK_EX);
2003         if (rc != 0)
2004                 GOTO(log, rc);
2005
2006         if (unlikely(lfsck_is_dead_obj(cobj))) {
2007                 exist = false;
2008                 goto replace;
2009         }
2010
2011         rc = dt_attr_get(env, cobj, la);
2012         if (rc != 0)
2013                 GOTO(log, rc);
2014
2015         /* The object has been modified by other(s), or it is not created by
2016          * LFSCK, the two cases are indistinguishable. So cannot replace it. */
2017         if (la->la_ctime != 0)
2018                 GOTO(log, rc);
2019
2020         if (S_ISREG(la->la_mode)) {
2021                 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV);
2022                 /* If someone has created related OST-object(s),
2023                  * then keep it. */
2024                 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
2025                         GOTO(log, rc = (rc > 0 ? 0 : rc));
2026         }
2027
2028 replace:
2029         dt_read_lock(env, child, 0);
2030         rc = lfsck_links_read2_with_rec(env, child, &ldata);
2031         dt_read_unlock(env, child);
2032
2033         /* Someone changed the child, no need to replace. */
2034         if (rc == -ENODATA)
2035                 GOTO(log, rc = 0);
2036
2037         if (rc != 0)
2038                 GOTO(log, rc);
2039
2040         rc = linkea_links_find(&ldata, cname, pfid);
2041         /* Someone moved the child, no need to replace. */
2042         if (rc != 0)
2043                 GOTO(log, rc = 0);
2044
2045         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2046                 GOTO(log, rc = 1);
2047
2048         th = dt_trans_create(env, dev);
2049         if (IS_ERR(th))
2050                 GOTO(log, rc = PTR_ERR(th));
2051
2052         if (exist) {
2053                 rc = dt_declare_destroy(env, cobj, th);
2054                 if (rc != 0)
2055                         GOTO(stop, rc);
2056         }
2057
2058         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2059         if (rc != 0)
2060                 GOTO(stop, rc);
2061
2062         rec->rec_type = S_IFDIR;
2063         rec->rec_fid = lfsck_dto2fid(child);
2064         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
2065                                (const struct dt_key *)name, th);
2066         if (rc != 0)
2067                 GOTO(stop, rc);
2068
2069         rc = dt_trans_start_local(env, dev, th);
2070         if (rc != 0)
2071                 GOTO(stop, rc);
2072
2073         if (exist) {
2074                 rc = dt_destroy(env, cobj, th);
2075                 if (rc != 0)
2076                         GOTO(stop, rc);
2077         }
2078
2079         /* The old name entry maybe not exist. */
2080         rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2081         if (rc != 0 && rc != -ENOENT)
2082                 GOTO(stop, rc);
2083
2084         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
2085                        (const struct dt_key *)name, th);
2086
2087         GOTO(stop, rc = (rc == 0 ? 1 : rc));
2088
2089 stop:
2090         dt_trans_stop(env, dev, th);
2091
2092 log:
2093         lfsck_ibits_unlock(&clh, LCK_EX);
2094         lfsck_unlock(pllh);
2095
2096         if (cobj != NULL && !IS_ERR(cobj))
2097                 lfsck_object_put(env, cobj);
2098
2099         CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
2100                "object "DFID" because of conflict with the object "DFID
2101                " under the parent "DFID" with name %s: rc = %d\n",
2102                lfsck_lfsck2name(lfsck), PFID(cfid),
2103                PFID(lfsck_dto2fid(child)), PFID(pfid), name, rc);
2104
2105         return rc;
2106 }
2107
2108 /**
2109  * Overwrite the linkEA for the object with the given ldata.
2110  *
2111  * The caller should take the ldlm lock before the calling.
2112  *
2113  * \param[in] env       pointer to the thread context
2114  * \param[in] com       pointer to the lfsck component
2115  * \param[in] obj       pointer to the dt_object to be handled
2116  * \param[in] ldata     pointer to the new linkEA data
2117  *
2118  * \retval              positive number for repaired cases
2119  * \retval              0 if nothing to be repaired
2120  * \retval              negative error number on failure
2121  */
2122 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
2123                                    struct lfsck_component *com,
2124                                    struct dt_object *obj,
2125                                    struct linkea_data *ldata)
2126 {
2127         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2128         struct dt_device                *dev    = lfsck_obj2dev(obj);
2129         struct thandle                  *th     = NULL;
2130         struct lu_buf                    linkea_buf;
2131         int                              rc     = 0;
2132         ENTRY;
2133
2134         th = dt_trans_create(env, dev);
2135         if (IS_ERR(th))
2136                 GOTO(log, rc = PTR_ERR(th));
2137
2138         lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
2139                        ldata->ld_leh->leh_len);
2140         rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2141                                   XATTR_NAME_LINK, 0, th);
2142         if (rc != 0)
2143                 GOTO(stop, rc);
2144
2145         rc = dt_trans_start_local(env, dev, th);
2146         if (rc != 0)
2147                 GOTO(stop, rc);
2148
2149         dt_write_lock(env, obj, 0);
2150         if (unlikely(lfsck_is_dead_obj(obj)))
2151                 GOTO(unlock, rc = 0);
2152
2153         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2154                 GOTO(unlock, rc = 1);
2155
2156         rc = dt_xattr_set(env, obj, &linkea_buf,
2157                           XATTR_NAME_LINK, 0, th);
2158
2159         GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2160
2161 unlock:
2162         dt_write_unlock(env, obj);
2163
2164 stop:
2165         dt_trans_stop(env, dev, th);
2166
2167 log:
2168         CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
2169                "object "DFID": rc = %d\n",
2170                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
2171
2172         if (rc != 0) {
2173                 struct lfsck_namespace *ns = com->lc_file_ram;
2174
2175                 ns->ln_flags |= LF_INCONSISTENT;
2176         }
2177
2178         return rc;
2179 }
2180
2181 /**
2182  * Repair invalid name entry.
2183  *
2184  * If the name entry contains invalid information, such as bad file type
2185  * or (and) corrupted object FID, then either remove the name entry or
2186  * udpate the name entry with the given (right) information.
2187  *
2188  * \param[in] env       pointer to the thread context
2189  * \param[in] com       pointer to the lfsck component
2190  * \param[in] parent    pointer to the parent directory
2191  * \param[in] child     pointer to the object referenced by the name entry
2192  * \param[in] name      the old name of the child under the parent directory
2193  * \param[in] name2     the new name of the child under the parent directory
2194  * \param[in] type      the type claimed by the name entry
2195  * \param[in] update    update the name entry if true; otherwise, remove it
2196  * \param[in] dec       decrease the parent nlink count if true
2197  *
2198  * \retval              positive number for repaired successfully
2199  * \retval              0 if nothing to be repaired
2200  * \retval              negative error number on failure
2201  */
2202 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2203                                   struct lfsck_component *com,
2204                                   struct dt_object *parent,
2205                                   struct dt_object *child,
2206                                   const char *name, const char *name2,
2207                                   __u16 type, bool update, bool dec)
2208 {
2209         struct lfsck_thread_info        *info   = lfsck_env_info(env);
2210         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
2211         const struct lu_fid             *pfid   = lfsck_dto2fid(parent);
2212         struct lu_fid                   cfid    = {0};
2213         struct lu_fid                    tfid;
2214         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2215         struct dt_device                *dev    = lfsck->li_next;
2216         struct thandle                  *th     = NULL;
2217         struct lfsck_lock_handle        *llh    = &info->lti_llh;
2218         struct lustre_handle             lh     = { 0 };
2219         int                              rc     = 0;
2220         ENTRY;
2221
2222         if (child)
2223                 cfid = *lfsck_dto2fid(child);
2224         parent = lfsck_object_locate(dev, parent);
2225         if (IS_ERR(parent))
2226                 GOTO(log, rc = PTR_ERR(parent));
2227
2228         if (unlikely(!dt_try_as_dir(env, parent)))
2229                 GOTO(log, rc = -ENOTDIR);
2230
2231         if (!update || strcmp(name, name2) == 0)
2232                 rc = lfsck_lock(env, lfsck, parent, name, llh,
2233                                 MDS_INODELOCK_UPDATE, LCK_PW);
2234         else
2235                 rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2236                                       MDS_INODELOCK_UPDATE, LCK_PW);
2237         if (rc != 0)
2238                 GOTO(log, rc);
2239
2240         th = dt_trans_create(env, dev);
2241         if (IS_ERR(th))
2242                 GOTO(unlock1, rc = PTR_ERR(th));
2243
2244         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2245         if (rc != 0)
2246                 GOTO(stop, rc);
2247
2248         if (update) {
2249                 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2250                 LASSERT(!fid_is_zero(&cfid));
2251                 rec->rec_fid = &cfid;
2252                 rc = dt_declare_insert(env, parent,
2253                                        (const struct dt_rec *)rec,
2254                                        (const struct dt_key *)name2, th);
2255                 if (rc != 0)
2256                         GOTO(stop, rc);
2257         }
2258
2259         if (dec && S_ISDIR(type)) {
2260                 rc = dt_declare_ref_del(env, parent, th);
2261                 if (rc != 0)
2262                         GOTO(stop, rc);
2263         }
2264
2265         rc = dt_trans_start_local(env, dev, th);
2266         if (rc != 0)
2267                 GOTO(stop, rc);
2268
2269
2270         dt_write_lock(env, parent, 0);
2271         rc = dt_lookup_dir(env, dt_object_child(parent), name, &tfid);
2272         /* Someone has removed the bad name entry by race. */
2273         if (rc == -ENOENT)
2274                 GOTO(unlock2, rc = 0);
2275
2276         if (rc != 0)
2277                 GOTO(unlock2, rc);
2278
2279         /* Someone has removed the bad name entry and reused it for other
2280          * object by race. */
2281         if (!lu_fid_eq(&tfid, &cfid))
2282                 GOTO(unlock2, rc = 0);
2283
2284         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2285                 GOTO(unlock2, rc = 1);
2286
2287         rc = dt_delete(env, parent, (const struct dt_key *)name, th);
2288         if (rc != 0)
2289                 GOTO(unlock2, rc);
2290
2291         if (update) {
2292                 rc = dt_insert(env, parent,
2293                                (const struct dt_rec *)rec,
2294                                (const struct dt_key *)name2, th);
2295                 if (rc != 0)
2296                         GOTO(unlock2, rc);
2297         }
2298
2299         if (dec && S_ISDIR(type)) {
2300                 rc = dt_ref_del(env, parent, th);
2301                 if (rc != 0)
2302                         GOTO(unlock2, rc);
2303         }
2304
2305         GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2306
2307 unlock2:
2308         dt_write_unlock(env, parent);
2309
2310 stop:
2311         dt_trans_stop(env, dev, th);
2312
2313         /* We are not sure whether the child will become orphan or not.
2314          * Record it in the LFSCK trace file for further checking in
2315          * the second-stage scanning. */
2316         if (!update && !dec && child && rc == 0)
2317                 lfsck_namespace_trace_update(env, com, &cfid,
2318                                              LNTF_CHECK_LINKEA, true);
2319
2320 unlock1:
2321         /* It is harmless even if unlock the unused lock_handle */
2322         lfsck_ibits_unlock(&lh, LCK_PW);
2323         lfsck_unlock(llh);
2324
2325 log:
2326         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2327                "entry for: parent "DFID", child "DFID", name %s, type "
2328                "in name entry %o, type claimed by child %o. repair it "
2329                "by %s with new name2 %s: rc = %d\n",
2330                lfsck_lfsck2name(lfsck), PFID(pfid), PFID(&cfid),
2331                name, type, update ? lfsck_object_type(child) : 0,
2332                update ? "updating" : "removing", name2, rc);
2333
2334         if (rc != 0) {
2335                 struct lfsck_namespace *ns = com->lc_file_ram;
2336
2337                 ns->ln_flags |= LF_INCONSISTENT;
2338         }
2339
2340         return rc;
2341 }
2342
2343 /**
2344  * Update the ".." name entry for the given object.
2345  *
2346  * The object's ".." is corrupted, this function will update the ".." name
2347  * entry with the given pfid, and the linkEA with the given ldata.
2348  *
2349  * The caller should take the ldlm lock before the calling.
2350  *
2351  * \param[in] env       pointer to the thread context
2352  * \param[in] com       pointer to the lfsck component
2353  * \param[in] obj       pointer to the dt_object to be handled
2354  * \param[in] pfid      the new fid for the object's ".." name entry
2355  * \param[in] cname     the name for the @obj in the parent directory
2356  *
2357  * \retval              positive number for repaired cases
2358  * \retval              0 if nothing to be repaired
2359  * \retval              negative error number on failure
2360  */
2361 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2362                                                   struct lfsck_component *com,
2363                                                   struct dt_object *obj,
2364                                                   const struct lu_fid *pfid,
2365                                                   struct lu_name *cname)
2366 {
2367         struct lfsck_thread_info        *info   = lfsck_env_info(env);
2368         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
2369         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2370         struct dt_device                *dev    = lfsck_obj2dev(obj);
2371         struct thandle                  *th     = NULL;
2372         struct linkea_data               ldata  = { NULL };
2373         struct lu_buf                    linkea_buf;
2374         int                              rc     = 0;
2375         ENTRY;
2376
2377         LASSERT(!dt_object_remote(obj));
2378         LASSERT(S_ISDIR(lfsck_object_type(obj)));
2379
2380         rc = linkea_links_new(&ldata, &info->lti_big_buf, cname, pfid);
2381         if (rc != 0)
2382                 GOTO(log, rc);
2383
2384         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2385                        ldata.ld_leh->leh_len);
2386
2387         th = dt_trans_create(env, dev);
2388         if (IS_ERR(th))
2389                 GOTO(log, rc = PTR_ERR(th));
2390
2391         rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2392         if (rc != 0)
2393                 GOTO(stop, rc);
2394
2395         rec->rec_type = S_IFDIR;
2396         rec->rec_fid = pfid;
2397         rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2398                                (const struct dt_key *)dotdot, th);
2399         if (rc != 0)
2400                 GOTO(stop, rc);
2401
2402         rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2403                                   XATTR_NAME_LINK, 0, th);
2404         if (rc != 0)
2405                 GOTO(stop, rc);
2406
2407         rc = dt_trans_start_local(env, dev, th);
2408         if (rc != 0)
2409                 GOTO(stop, rc);
2410
2411         dt_write_lock(env, obj, 0);
2412         if (unlikely(lfsck_is_dead_obj(obj)))
2413                 GOTO(unlock, rc = 0);
2414
2415         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2416                 GOTO(unlock, rc = 1);
2417
2418         /* The old ".." name entry maybe not exist. */
2419         dt_delete(env, obj, (const struct dt_key *)dotdot, th);
2420
2421         rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2422                        (const struct dt_key *)dotdot, th);
2423         if (rc != 0)
2424                 GOTO(unlock, rc);
2425
2426         rc = lfsck_links_write(env, obj, &ldata, th);
2427
2428         GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2429
2430 unlock:
2431         dt_write_unlock(env, obj);
2432
2433 stop:
2434         dt_trans_stop(env, dev, th);
2435
2436 log:
2437         CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2438                "the object "DFID", new parent "DFID": rc = %d\n",
2439                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2440                PFID(pfid), rc);
2441
2442         if (rc != 0) {
2443                 struct lfsck_namespace *ns = com->lc_file_ram;
2444
2445                 ns->ln_flags |= LF_INCONSISTENT;
2446         }
2447
2448         return rc;
2449 }
2450
2451 /**
2452  * Handle orphan @obj during Double Scan Directory.
2453  *
2454  * Remove the @obj's current (invalid) linkEA entries, and insert
2455  * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2456  * ${FID}-${PFID}-D-${conflict_version}
2457  *
2458  * The caller should take the ldlm lock before the calling.
2459  *
2460  * \param[in] env       pointer to the thread context
2461  * \param[in] com       pointer to the lfsck component
2462  * \param[in] obj       pointer to the orphan object to be handled
2463  * \param[in] pfid      the new fid for the object's ".." name entry
2464  * \param[in,out] lh    ldlm lock handler for the given @obj
2465  * \param[out] type     to tell the caller what the inconsistency is
2466  *
2467  * \retval              positive number for repaired cases
2468  * \retval              0 if nothing to be repaired
2469  * \retval              negative error number on failure
2470  */
2471 static int
2472 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2473                            struct lfsck_component *com,
2474                            struct dt_object *obj,
2475                            const struct lu_fid *pfid,
2476                            struct lustre_handle *lh,
2477                            enum lfsck_namespace_inconsistency_type *type)
2478 {
2479         struct lfsck_thread_info *info = lfsck_env_info(env);
2480         struct lfsck_namespace   *ns   = com->lc_file_ram;
2481         int                       rc;
2482         ENTRY;
2483
2484         /* Remove the unrecognized linkEA. */
2485         rc = lfsck_namespace_links_remove(env, com, obj);
2486         lfsck_ibits_unlock(lh, LCK_EX);
2487         if (rc < 0 && rc != -ENODATA)
2488                 RETURN(rc);
2489
2490         *type = LNIT_MUL_REF;
2491
2492         /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2493          * ever tried to verify some remote MDT-object that resides on this
2494          * MDT, but this MDT failed to respond such request. So means there
2495          * may be some remote name entry on other MDT that references this
2496          * object with another name, so we cannot know whether this linkEA
2497          * is valid or not. So keep it there and maybe resolved when next
2498          * LFSCK run. */
2499         if (ns->ln_flags & LF_INCOMPLETE)
2500                 RETURN(0);
2501
2502         /* The unique linkEA is invalid, even if the ".." name entry may be
2503          * valid, we still cannot know via which name entry this directory
2504          * will be referenced. Then handle it as pure orphan. */
2505         snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2506                  "-"DFID, PFID(pfid));
2507         rc = lfsck_namespace_insert_orphan(env, com, obj,
2508                                            info->lti_tmpbuf, "D", NULL);
2509
2510         RETURN(rc);
2511 }
2512
2513 /**
2514  * Double Scan Directory object for single linkEA entry case.
2515  *
2516  * The given @child has unique linkEA entry. If the linkEA entry is valid,
2517  * then check whether the name is in the namespace or not, if not, add the
2518  * missing name entry back to namespace. If the linkEA entry is invalid,
2519  * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2520  * as an orphan.
2521  *
2522  * \param[in] env       pointer to the thread context
2523  * \param[in] com       pointer to the lfsck component
2524  * \param[in] child     pointer to the directory to be double scanned
2525  * \param[in] pfid      the FID corresponding to the ".." entry
2526  * \param[in] ldata     pointer to the linkEA data for the given @child
2527  * \param[in,out] lh    ldlm lock handler for the given @child
2528  * \param[out] type     to tell the caller what the inconsistency is
2529  * \param[in] retry     if found inconsistency, but the caller does not hold
2530  *                      ldlm lock on the @child, then set @retry as true
2531  * \param[in] unknown   set if does not know how to repair the inconsistency
2532  *
2533  * \retval              positive number for repaired cases
2534  * \retval              0 if nothing to be repaired
2535  * \retval              negative error number on failure
2536  */
2537 static int
2538 lfsck_namespace_dsd_single(const struct lu_env *env,
2539                            struct lfsck_component *com,
2540                            struct dt_object *child,
2541                            const struct lu_fid *pfid,
2542                            struct linkea_data *ldata,
2543                            struct lustre_handle *lh,
2544                            enum lfsck_namespace_inconsistency_type *type,
2545                            bool *retry, bool *unknown)
2546 {
2547         struct lfsck_thread_info *info          = lfsck_env_info(env);
2548         struct lu_name           *cname         = &info->lti_name;
2549         const struct lu_fid      *cfid          = lfsck_dto2fid(child);
2550         struct lu_fid             tfid;
2551         struct lfsck_namespace   *ns            = com->lc_file_ram;
2552         struct lfsck_instance    *lfsck         = com->lc_lfsck;
2553         struct dt_object         *parent        = NULL;
2554         struct lmv_mds_md_v1     *lmv;
2555         int                       rc            = 0;
2556         ENTRY;
2557
2558         rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2559                                                  info->lti_key,
2560                                                  sizeof(info->lti_key));
2561         /* The unique linkEA entry with bad parent will be handled as orphan. */
2562         if (rc != 0) {
2563                 if (!lustre_handle_is_used(lh) && retry != NULL)
2564                         *retry = true;
2565                 else
2566                         rc = lfsck_namespace_dsd_orphan(env, com, child,
2567                                                         pfid, lh, type);
2568
2569                 GOTO(out, rc);
2570         }
2571
2572         parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2573         if (IS_ERR(parent))
2574                 GOTO(out, rc = PTR_ERR(parent));
2575
2576         /* We trust the unique linkEA entry in spite of whether it matches the
2577          * ".." name entry or not. Because even if the linkEA entry is wrong
2578          * and the ".." name entry is right, we still cannot know via which
2579          * name entry the child will be referenced, since all known entries
2580          * have been verified during the first-stage scanning. */
2581         if (!dt_object_exists(parent)) {
2582                 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2583                  * has ever tried to verify some remote MDT-object that resides
2584                  * on this MDT, but this MDT failed to respond such request. So
2585                  * means there may be some remote name entry on other MDT that
2586                  * references this object with another name, so we cannot know
2587                  * whether this linkEA is valid or not. So keep it there and
2588                  * maybe resolved when next LFSCK run. */
2589                 if (ns->ln_flags & LF_INCOMPLETE)
2590                         GOTO(out, rc = 0);
2591
2592                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2593                         *retry = true;
2594
2595                         GOTO(out, rc = 0);
2596                 }
2597
2598                 lfsck_ibits_unlock(lh, LCK_EX);
2599
2600 lost_parent:
2601                 lmv = &info->lti_lmv;
2602                 rc = lfsck_read_stripe_lmv(env, lfsck, child, lmv);
2603                 if (rc != 0 && rc != -ENODATA)
2604                         GOTO(out, rc);
2605
2606                 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2607                         lmv = NULL;
2608                 } else if (lfsck_shard_name_to_index(env,
2609                                         cname->ln_name, cname->ln_namelen,
2610                                         S_IFDIR, cfid) < 0) {
2611                         /* It is an invalid name entry, we
2612                          * cannot trust the parent also. */
2613                         rc = lfsck_namespace_shrink_linkea(env, com, child,
2614                                                 ldata, cname, &tfid, true);
2615                         if (rc < 0)
2616                                 GOTO(out, rc);
2617
2618                         snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2619                                  "-"DFID, PFID(pfid));
2620                         rc = lfsck_namespace_insert_orphan(env, com, child,
2621                                                 info->lti_tmpbuf, "S", NULL);
2622
2623                         GOTO(out, rc);
2624                 }
2625
2626                 /* Create the lost parent as an orphan. */
2627                 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2628                 if (rc >= 0) {
2629                         /* Add the missing name entry to the parent. */
2630                         rc = lfsck_namespace_insert_normal(env, com, parent,
2631                                                            child, cname);
2632                         if (unlikely(rc == -EEXIST)) {
2633                                 /* Unfortunately, someone reused the name
2634                                  * under the parent by race. So we have
2635                                  * to remove the linkEA entry from
2636                                  * current child object. It means that the
2637                                  * LFSCK cannot recover the system
2638                                  * totally back to its original status,
2639                                  * but it is necessary to make the
2640                                  * current system to be consistent. */
2641                                 rc = lfsck_namespace_shrink_linkea(env,
2642                                                 com, child, ldata,
2643                                                 cname, &tfid, true);
2644                                 if (rc >= 0) {
2645                                         snprintf(info->lti_tmpbuf,
2646                                                  sizeof(info->lti_tmpbuf),
2647                                                  "-"DFID, PFID(pfid));
2648                                         rc = lfsck_namespace_insert_orphan(env,
2649                                                 com, child, info->lti_tmpbuf,
2650                                                 "D", NULL);
2651                                 }
2652                         }
2653                 }
2654
2655                 GOTO(out, rc);
2656         } /* !dt_object_exists(parent) */
2657
2658         /* The unique linkEA entry with bad parent will be handled as orphan. */
2659         if (unlikely(!dt_try_as_dir(env, parent))) {
2660                 if (!lustre_handle_is_used(lh) && retry != NULL)
2661                         *retry = true;
2662                 else
2663                         rc = lfsck_namespace_dsd_orphan(env, com, child,
2664                                                         pfid, lh, type);
2665
2666                 GOTO(out, rc);
2667         }
2668
2669         rc = dt_lookup_dir(env, parent, cname->ln_name, &tfid);
2670         if (rc == -ENOENT) {
2671                 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2672                  * has ever tried to verify some remote MDT-object that resides
2673                  * on this MDT, but this MDT failed to respond such request. So
2674                  * means there may be some remote name entry on other MDT that
2675                  * references this object with another name, so we cannot know
2676                  * whether this linkEA is valid or not. So keep it there and
2677                  * maybe resolved when next LFSCK run. */
2678                 if (ns->ln_flags & LF_INCOMPLETE)
2679                         GOTO(out, rc = 0);
2680
2681                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2682                         *retry = true;
2683
2684                         GOTO(out, rc = 0);
2685                 }
2686
2687                 lfsck_ibits_unlock(lh, LCK_EX);
2688                 rc = lfsck_namespace_check_name(env, lfsck, parent, child,
2689                                                 cname);
2690                 if (rc == -ENOENT)
2691                         goto lost_parent;
2692
2693                 if (rc < 0)
2694                         GOTO(out, rc);
2695
2696                 /* It is an invalid name entry, drop it. */
2697                 if (unlikely(rc > 0)) {
2698                         rc = lfsck_namespace_shrink_linkea(env, com, child,
2699                                                 ldata, cname, &tfid, true);
2700                         if (rc >= 0) {
2701                                 snprintf(info->lti_tmpbuf,
2702                                          sizeof(info->lti_tmpbuf),
2703                                          "-"DFID, PFID(pfid));
2704                                 rc = lfsck_namespace_insert_orphan(env, com,
2705                                         child, info->lti_tmpbuf, "D", NULL);
2706                         }
2707
2708                         GOTO(out, rc);
2709                 }
2710
2711                 /* Add the missing name entry back to the namespace. */
2712                 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2713                                                    cname);
2714                 if (unlikely(rc == -ESTALE))
2715                         /* It may happen when the remote object has been
2716                          * removed, but the local MDT is not aware of that. */
2717                         goto lost_parent;
2718
2719                 if (unlikely(rc == -EEXIST)) {
2720                         /* Unfortunately, someone reused the name under the
2721                          * parent by race. So we have to remove the linkEA
2722                          * entry from current child object. It means that the
2723                          * LFSCK cannot recover the system totally back to
2724                          * its original status, but it is necessary to make
2725                          * the current system to be consistent.
2726                          *
2727                          * It also may be because of the LFSCK found some
2728                          * internal status of create operation. Under such
2729                          * case, nothing to be done. */
2730                         rc = lfsck_namespace_shrink_linkea_cond(env, com,
2731                                         parent, child, ldata, cname, &tfid);
2732                         if (rc >= 0) {
2733                                 snprintf(info->lti_tmpbuf,
2734                                          sizeof(info->lti_tmpbuf),
2735                                          "-"DFID, PFID(pfid));
2736                                 rc = lfsck_namespace_insert_orphan(env, com,
2737                                         child, info->lti_tmpbuf, "D", NULL);
2738                         }
2739                 }
2740
2741                 GOTO(out, rc);
2742         } /* rc == -ENOENT */
2743
2744         if (rc != 0)
2745                 GOTO(out, rc);
2746
2747         if (!lu_fid_eq(&tfid, cfid)) {
2748                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2749                         *retry = true;
2750
2751                         GOTO(out, rc = 0);
2752                 }
2753
2754                 lfsck_ibits_unlock(lh, LCK_EX);
2755                 /* The name entry references another MDT-object that
2756                  * may be created by the LFSCK for repairing dangling
2757                  * name entry. Try to replace it. */
2758                 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2759                                                   &tfid, cname);
2760                 if (rc == 0)
2761                         rc = lfsck_namespace_dsd_orphan(env, com, child,
2762                                                         pfid, lh, type);
2763
2764                 GOTO(out, rc);
2765         }
2766
2767         /* Zero FID may because the remote directroy object has invalid linkEA,
2768          * or lost linkEA. Under such case, the LFSCK on this MDT does not know
2769          * how to repair the inconsistency, but the namespace LFSCK on the MDT
2770          * where its name entry resides may has more information (name, FID) to
2771          * repair such inconsistency. So here, keep the inconsistency to avoid
2772          * some imporper repairing. */
2773         if (fid_is_zero(pfid)) {
2774                 if (unknown)
2775                         *unknown = true;
2776
2777                 GOTO(out, rc = 0);
2778         }
2779
2780         /* The ".." name entry is wrong, update it. */
2781         if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2782                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2783                         *retry = true;
2784
2785                         GOTO(out, rc = 0);
2786                 }
2787
2788                 *type = LNIT_UNMATCHED_PAIRS;
2789                 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2790                                                 lfsck_dto2fid(parent), cname);
2791         }
2792
2793         GOTO(out, rc);
2794
2795 out:
2796         if (parent != NULL && !IS_ERR(parent))
2797                 lfsck_object_put(env, parent);
2798
2799         return rc;
2800 }
2801
2802 /**
2803  * Double Scan Directory object for multiple linkEA entries case.
2804  *
2805  * The given @child has multiple linkEA entries. There is at most one linkEA
2806  * entry will be valid, all the others will be removed. Firstly, the function
2807  * will try to find out the linkEA entry for which the name entry exists under
2808  * the given parent (@pfid). If there is no linkEA entry that matches the given
2809  * ".." name entry, then tries to find out the first linkEA entry that both the
2810  * parent and the name entry exist to rebuild a new ".." name entry.
2811  *
2812  * \param[in] env       pointer to the thread context
2813  * \param[in] com       pointer to the lfsck component
2814  * \param[in] child     pointer to the directory to be double scanned
2815  * \param[in] pfid      the FID corresponding to the ".." entry
2816  * \param[in] ldata     pointer to the linkEA data for the given @child
2817  * \param[in,out] lh    ldlm lock handler for the given @child
2818  * \param[out] type     to tell the caller what the inconsistency is
2819  * \param[in] lpf       true if the ".." entry is under lost+found/MDTxxxx/
2820  * \param[in] unknown   set if does not know how to repair the inconsistency
2821  *
2822  * \retval              positive number for repaired cases
2823  * \retval              0 if nothing to be repaired
2824  * \retval              negative error number on failure
2825  */
2826 static int
2827 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2828                              struct lfsck_component *com,
2829                              struct dt_object *child,
2830                              const struct lu_fid *pfid,
2831                              struct linkea_data *ldata,
2832                              struct lustre_handle *lh,
2833                              enum lfsck_namespace_inconsistency_type *type,
2834                              bool lpf, bool *unknown)
2835 {
2836         struct lfsck_thread_info *info          = lfsck_env_info(env);
2837         struct lu_name           *cname         = &info->lti_name;
2838         const struct lu_fid      *cfid          = lfsck_dto2fid(child);
2839         struct lu_fid            *pfid2         = &info->lti_fid3;
2840         struct lu_fid             tfid;
2841         struct lfsck_namespace   *ns            = com->lc_file_ram;
2842         struct lfsck_instance    *lfsck         = com->lc_lfsck;
2843         struct lfsck_bookmark    *bk            = &lfsck->li_bookmark_ram;
2844         struct dt_object         *parent        = NULL;
2845         struct linkea_data        ldata_new     = { NULL };
2846         int                       dirent_count  = 0;
2847         int                       rc            = 0;
2848         bool                      once          = true;
2849         ENTRY;
2850
2851 again:
2852         while (ldata->ld_lee != NULL) {
2853                 rc = lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2854                                                          info->lti_key,
2855                                                          sizeof(info->lti_key));
2856                 /* Drop invalid linkEA entry. */
2857                 if (rc != 0) {
2858                         lfsck_linkea_del_buf(ldata, cname);
2859                         continue;
2860                 }
2861
2862                 /* Drop repeated linkEA entries. */
2863                 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2864
2865                 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2866                  * then it is possible that: the directry object has ever
2867                  * been lost, but its name entry was there. In the former
2868                  * LFSCK run, during the first-stage scanning, the LFSCK
2869                  * found the dangling name entry, but it did not recreate
2870                  * the lost object, and when moved to the second-stage
2871                  * scanning, some children objects of the lost directory
2872                  * object were found, then the LFSCK recreated such lost
2873                  * directory object as an orphan.
2874                  *
2875                  * When the LFSCK runs again, if the dangling name is still
2876                  * there, the LFSCK should move the orphan directory object
2877                  * back to the normal namespace. */
2878                 if (!lpf && !fid_is_zero(pfid) &&
2879                     !lu_fid_eq(pfid, &tfid) && once) {
2880                         linkea_next_entry(ldata);
2881                         continue;
2882                 }
2883
2884                 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2885                 if (IS_ERR(parent)) {
2886                         rc = PTR_ERR(parent);
2887                         /* if @pfid doesn't have a valid OI mapping, it will
2888                          * trigger OI scrub, and -ENONET is is returned if it's
2889                          * remote, -EINPROGRESS if local.
2890                          */
2891                         if ((rc == -ENOENT || rc == -EINPROGRESS) &&
2892                             ldata->ld_leh->leh_reccount > 1) {
2893                                 lfsck_linkea_del_buf(ldata, cname);
2894                                 continue;
2895                         }
2896
2897                         RETURN(rc);
2898                 }
2899
2900                 if (!dt_object_exists(parent)) {
2901                         lfsck_object_put(env, parent);
2902                         if (ldata->ld_leh->leh_reccount > 1) {
2903                                 /* If it is NOT the last linkEA entry, then
2904                                  * there is still other chance to make the
2905                                  * child to be visible via other parent, then
2906                                  * remove this linkEA entry. */
2907                                 lfsck_linkea_del_buf(ldata, cname);
2908                                 continue;
2909                         }
2910
2911                         break;
2912                 }
2913
2914                 /* The linkEA entry with bad parent will be removed. */
2915                 if (unlikely(!dt_try_as_dir(env, parent))) {
2916                         lfsck_object_put(env, parent);
2917                         lfsck_linkea_del_buf(ldata, cname);
2918                         continue;
2919                 }
2920
2921                 rc = dt_lookup_dir(env, parent, cname->ln_name, &tfid);
2922                 *pfid2 = *lfsck_dto2fid(parent);
2923                 if (rc == -ENOENT) {
2924                         lfsck_object_put(env, parent);
2925                         linkea_next_entry(ldata);
2926                         continue;
2927                 }
2928
2929                 if (rc != 0) {
2930                         lfsck_object_put(env, parent);
2931
2932                         RETURN(rc);
2933                 }
2934
2935                 if (lu_fid_eq(&tfid, cfid)) {
2936                         lfsck_object_put(env, parent);
2937                         /* If the parent (that is declared via linkEA entry)
2938                          * directory contains the specified child, but such
2939                          * parent does not match the dotdot name entry, then
2940                          * trust the linkEA. */
2941                         if (!fid_is_zero(pfid) && !lu_fid_eq(pfid, pfid2)) {
2942                                 *type = LNIT_UNMATCHED_PAIRS;
2943                                 rc = lfsck_namespace_repair_unmatched_pairs(env,
2944                                                 com, child, pfid2, cname);
2945
2946                                 RETURN(rc);
2947                         }
2948
2949 rebuild:
2950                         /* It is the most common case that we find the
2951                          * name entry corresponding to the linkEA entry
2952                          * that matches the ".." name entry. */
2953                         rc = linkea_links_new(&ldata_new, &info->lti_big_buf,
2954                                               cname, pfid2);
2955                         if (rc != 0)
2956                                 RETURN(rc);
2957
2958                         rc = lfsck_namespace_rebuild_linkea(env, com, child,
2959                                                             &ldata_new);
2960                         if (rc < 0)
2961                                 RETURN(rc);
2962
2963                         lfsck_linkea_del_buf(ldata, cname);
2964                         linkea_first_entry(ldata);
2965                         /* There may be some invalid dangling name entries under
2966                          * other parent directories, remove all of them. */
2967                         while (ldata->ld_lee != NULL) {
2968                                 rc = lfsck_namespace_unpack_linkea_entry(ldata,
2969                                                 cname, &tfid, info->lti_key,
2970                                                 sizeof(info->lti_key));
2971                                 if (rc != 0)
2972                                         goto next;
2973
2974                                 parent = lfsck_object_find_bottom(env, lfsck,
2975                                                                   &tfid);
2976                                 if (IS_ERR(parent)) {
2977                                         rc = PTR_ERR(parent);
2978                                         if (rc != -ENOENT &&
2979                                             bk->lb_param & LPF_FAILOUT)
2980                                                 RETURN(rc);
2981
2982                                         goto next;
2983                                 }
2984
2985                                 if (!dt_object_exists(parent)) {
2986                                         lfsck_object_put(env, parent);
2987                                         goto next;
2988                                 }
2989
2990                                 rc = lfsck_namespace_repair_dirent(env, com,
2991                                         parent, child, cname->ln_name,
2992                                         cname->ln_name, S_IFDIR, false, true);
2993                                 lfsck_object_put(env, parent);
2994                                 if (rc < 0) {
2995                                         if (bk->lb_param & LPF_FAILOUT)
2996                                                 RETURN(rc);
2997
2998                                         goto next;
2999                                 }
3000
3001                                 dirent_count += rc;
3002
3003 next:
3004                                 lfsck_linkea_del_buf(ldata, cname);
3005                         }
3006
3007                         ns->ln_dirent_repaired += dirent_count;
3008
3009                         RETURN(rc);
3010                 } /* lu_fid_eq(&tfid, lfsck_dto2fid(child)) */
3011
3012                 lfsck_ibits_unlock(lh, LCK_EX);
3013                 /* The name entry references another MDT-object that may be
3014                  * created by the LFSCK for repairing dangling name entry.
3015                  * Try to replace it. */
3016                 rc = lfsck_namespace_replace_cond(env, com, parent, child,
3017                                                   &tfid, cname);
3018                 lfsck_object_put(env, parent);
3019                 if (rc < 0)
3020                         RETURN(rc);
3021
3022                 if (rc > 0)
3023                         goto rebuild;
3024
3025                 lfsck_linkea_del_buf(ldata, cname);
3026         } /* while (ldata->ld_lee != NULL) */
3027
3028         /* If there is still linkEA overflow, return. */
3029         if (unlikely(ldata->ld_leh->leh_overflow_time))
3030                 RETURN(0);
3031
3032         linkea_first_entry(ldata);
3033         if (ldata->ld_leh->leh_reccount == 1) {
3034                 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
3035                                                 lh, type, NULL, unknown);
3036
3037                 RETURN(rc);
3038         }
3039
3040         /* All linkEA entries are invalid and removed, then handle the @child
3041          * as an orphan.*/
3042         if (ldata->ld_leh->leh_reccount == 0) {
3043                 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,