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