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