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