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