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