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