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