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