Whamcloud - gitweb
83250b61cbc4e545ac56918ac39a43065c6454d8
[fs/lustre-release.git] / lustre / lfsck / lfsck_namespace.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2013, 2014, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_namespace.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #define DEBUG_SUBSYSTEM S_LFSCK
32
33 #include <lustre/lustre_idl.h>
34 #include <lu_object.h>
35 #include <dt_object.h>
36 #include <md_object.h>
37 #include <lustre_fid.h>
38 #include <lustre_lib.h>
39 #include <lustre_net.h>
40 #include <lustre/lustre_user.h>
41
42 #include "lfsck_internal.h"
43
44 #define LFSCK_NAMESPACE_MAGIC_V1        0xA0629D03
45 #define LFSCK_NAMESPACE_MAGIC_V2        0xA0621A0B
46
47 /* For Lustre-2.x (x <= 6), the namespace LFSCK used LFSCK_NAMESPACE_MAGIC_V1
48  * as the trace file magic. When downgrade to such old release, the old LFSCK
49  * will not recognize the new LFSCK_NAMESPACE_MAGIC_V2 in the new trace file,
50  * then it will reset the whole LFSCK, and will not cause start failure. The
51  * similar case will happen when upgrade from such old release. */
52 #define LFSCK_NAMESPACE_MAGIC           LFSCK_NAMESPACE_MAGIC_V2
53
54 enum lfsck_nameentry_check {
55         LFSCK_NAMEENTRY_DEAD            = 1, /* The object has been unlinked. */
56         LFSCK_NAMEENTRY_REMOVED         = 2, /* The entry has been removed. */
57         LFSCK_NAMEENTRY_RECREATED       = 3, /* The entry has been recreated. */
58 };
59
60 static struct lfsck_namespace_req *
61 lfsck_namespace_assistant_req_init(struct lfsck_instance *lfsck,
62                                    struct lu_dirent *ent, __u16 type)
63 {
64         struct lfsck_namespace_req *lnr;
65         int                         size;
66
67         size = sizeof(*lnr) + (ent->lde_namelen & ~3) + 4;
68         OBD_ALLOC(lnr, size);
69         if (lnr == NULL)
70                 return ERR_PTR(-ENOMEM);
71
72         INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
73         lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
74         lnr->lnr_lmv = lfsck_lmv_get(lfsck->li_lmv);
75         lnr->lnr_fid = ent->lde_fid;
76         lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
77         lnr->lnr_dir_cookie = ent->lde_hash;
78         lnr->lnr_attr = ent->lde_attrs;
79         lnr->lnr_size = size;
80         lnr->lnr_type = type;
81         lnr->lnr_namelen = ent->lde_namelen;
82         memcpy(lnr->lnr_name, ent->lde_name, ent->lde_namelen);
83
84         return lnr;
85 }
86
87 static void lfsck_namespace_assistant_req_fini(const struct lu_env *env,
88                                                struct lfsck_assistant_req *lar)
89 {
90         struct lfsck_namespace_req *lnr =
91                         container_of0(lar, struct lfsck_namespace_req, lnr_lar);
92
93         if (lnr->lnr_lmv != NULL)
94                 lfsck_lmv_put(env, lnr->lnr_lmv);
95
96         OBD_FREE(lnr, lnr->lnr_size);
97 }
98
99 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *dst,
100                                       struct lfsck_namespace *src)
101 {
102         dst->ln_magic = le32_to_cpu(src->ln_magic);
103         dst->ln_status = le32_to_cpu(src->ln_status);
104         dst->ln_flags = le32_to_cpu(src->ln_flags);
105         dst->ln_success_count = le32_to_cpu(src->ln_success_count);
106         dst->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
107         dst->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
108         dst->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
109         dst->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
110         dst->ln_time_last_checkpoint =
111                                 le64_to_cpu(src->ln_time_last_checkpoint);
112         lfsck_position_le_to_cpu(&dst->ln_pos_latest_start,
113                                  &src->ln_pos_latest_start);
114         lfsck_position_le_to_cpu(&dst->ln_pos_last_checkpoint,
115                                  &src->ln_pos_last_checkpoint);
116         lfsck_position_le_to_cpu(&dst->ln_pos_first_inconsistent,
117                                  &src->ln_pos_first_inconsistent);
118         dst->ln_items_checked = le64_to_cpu(src->ln_items_checked);
119         dst->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
120         dst->ln_items_failed = le64_to_cpu(src->ln_items_failed);
121         dst->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
122         dst->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
123         dst->ln_objs_repaired_phase2 =
124                                 le64_to_cpu(src->ln_objs_repaired_phase2);
125         dst->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
126         dst->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
127         fid_le_to_cpu(&dst->ln_fid_latest_scanned_phase2,
128                       &src->ln_fid_latest_scanned_phase2);
129         dst->ln_dirent_repaired = le64_to_cpu(src->ln_dirent_repaired);
130         dst->ln_linkea_repaired = le64_to_cpu(src->ln_linkea_repaired);
131         dst->ln_mul_linked_checked = le64_to_cpu(src->ln_mul_linked_checked);
132         dst->ln_mul_linked_repaired = le64_to_cpu(src->ln_mul_linked_repaired);
133         dst->ln_unknown_inconsistency =
134                                 le64_to_cpu(src->ln_unknown_inconsistency);
135         dst->ln_unmatched_pairs_repaired =
136                                 le64_to_cpu(src->ln_unmatched_pairs_repaired);
137         dst->ln_dangling_repaired = le64_to_cpu(src->ln_dangling_repaired);
138         dst->ln_mul_ref_repaired = le64_to_cpu(src->ln_mul_ref_repaired);
139         dst->ln_bad_type_repaired = le64_to_cpu(src->ln_bad_type_repaired);
140         dst->ln_lost_dirent_repaired =
141                                 le64_to_cpu(src->ln_lost_dirent_repaired);
142         dst->ln_striped_dirs_scanned =
143                                 le64_to_cpu(src->ln_striped_dirs_scanned);
144         dst->ln_striped_dirs_repaired =
145                                 le64_to_cpu(src->ln_striped_dirs_repaired);
146         dst->ln_striped_dirs_failed =
147                                 le64_to_cpu(src->ln_striped_dirs_failed);
148         dst->ln_striped_dirs_disabled =
149                                 le64_to_cpu(src->ln_striped_dirs_disabled);
150         dst->ln_striped_dirs_skipped =
151                                 le64_to_cpu(src->ln_striped_dirs_skipped);
152         dst->ln_striped_shards_scanned =
153                                 le64_to_cpu(src->ln_striped_shards_scanned);
154         dst->ln_striped_shards_repaired =
155                                 le64_to_cpu(src->ln_striped_shards_repaired);
156         dst->ln_striped_shards_failed =
157                                 le64_to_cpu(src->ln_striped_shards_failed);
158         dst->ln_striped_shards_skipped =
159                                 le64_to_cpu(src->ln_striped_shards_skipped);
160         dst->ln_name_hash_repaired = le64_to_cpu(src->ln_name_hash_repaired);
161         dst->ln_local_lpf_scanned = le64_to_cpu(src->ln_local_lpf_scanned);
162         dst->ln_local_lpf_moved = le64_to_cpu(src->ln_local_lpf_moved);
163         dst->ln_local_lpf_skipped = le64_to_cpu(src->ln_local_lpf_skipped);
164         dst->ln_local_lpf_failed = le64_to_cpu(src->ln_local_lpf_failed);
165         dst->ln_bitmap_size = le32_to_cpu(src->ln_bitmap_size);
166 }
167
168 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *dst,
169                                       struct lfsck_namespace *src)
170 {
171         dst->ln_magic = cpu_to_le32(src->ln_magic);
172         dst->ln_status = cpu_to_le32(src->ln_status);
173         dst->ln_flags = cpu_to_le32(src->ln_flags);
174         dst->ln_success_count = cpu_to_le32(src->ln_success_count);
175         dst->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
176         dst->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
177         dst->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
178         dst->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
179         dst->ln_time_last_checkpoint =
180                                 cpu_to_le64(src->ln_time_last_checkpoint);
181         lfsck_position_cpu_to_le(&dst->ln_pos_latest_start,
182                                  &src->ln_pos_latest_start);
183         lfsck_position_cpu_to_le(&dst->ln_pos_last_checkpoint,
184                                  &src->ln_pos_last_checkpoint);
185         lfsck_position_cpu_to_le(&dst->ln_pos_first_inconsistent,
186                                  &src->ln_pos_first_inconsistent);
187         dst->ln_items_checked = cpu_to_le64(src->ln_items_checked);
188         dst->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
189         dst->ln_items_failed = cpu_to_le64(src->ln_items_failed);
190         dst->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
191         dst->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
192         dst->ln_objs_repaired_phase2 =
193                                 cpu_to_le64(src->ln_objs_repaired_phase2);
194         dst->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
195         dst->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
196         fid_cpu_to_le(&dst->ln_fid_latest_scanned_phase2,
197                       &src->ln_fid_latest_scanned_phase2);
198         dst->ln_dirent_repaired = cpu_to_le64(src->ln_dirent_repaired);
199         dst->ln_linkea_repaired = cpu_to_le64(src->ln_linkea_repaired);
200         dst->ln_mul_linked_checked = cpu_to_le64(src->ln_mul_linked_checked);
201         dst->ln_mul_linked_repaired = cpu_to_le64(src->ln_mul_linked_repaired);
202         dst->ln_unknown_inconsistency =
203                                 cpu_to_le64(src->ln_unknown_inconsistency);
204         dst->ln_unmatched_pairs_repaired =
205                                 cpu_to_le64(src->ln_unmatched_pairs_repaired);
206         dst->ln_dangling_repaired = cpu_to_le64(src->ln_dangling_repaired);
207         dst->ln_mul_ref_repaired = cpu_to_le64(src->ln_mul_ref_repaired);
208         dst->ln_bad_type_repaired = cpu_to_le64(src->ln_bad_type_repaired);
209         dst->ln_lost_dirent_repaired =
210                                 cpu_to_le64(src->ln_lost_dirent_repaired);
211         dst->ln_striped_dirs_scanned =
212                                 cpu_to_le64(src->ln_striped_dirs_scanned);
213         dst->ln_striped_dirs_repaired =
214                                 cpu_to_le64(src->ln_striped_dirs_repaired);
215         dst->ln_striped_dirs_failed =
216                                 cpu_to_le64(src->ln_striped_dirs_failed);
217         dst->ln_striped_dirs_disabled =
218                                 cpu_to_le64(src->ln_striped_dirs_disabled);
219         dst->ln_striped_dirs_skipped =
220                                 cpu_to_le64(src->ln_striped_dirs_skipped);
221         dst->ln_striped_shards_scanned =
222                                 cpu_to_le64(src->ln_striped_shards_scanned);
223         dst->ln_striped_shards_repaired =
224                                 cpu_to_le64(src->ln_striped_shards_repaired);
225         dst->ln_striped_shards_failed =
226                                 cpu_to_le64(src->ln_striped_shards_failed);
227         dst->ln_striped_shards_skipped =
228                                 cpu_to_le64(src->ln_striped_shards_skipped);
229         dst->ln_name_hash_repaired = cpu_to_le64(src->ln_name_hash_repaired);
230         dst->ln_local_lpf_scanned = cpu_to_le64(src->ln_local_lpf_scanned);
231         dst->ln_local_lpf_moved = cpu_to_le64(src->ln_local_lpf_moved);
232         dst->ln_local_lpf_skipped = cpu_to_le64(src->ln_local_lpf_skipped);
233         dst->ln_local_lpf_failed = cpu_to_le64(src->ln_local_lpf_failed);
234         dst->ln_bitmap_size = cpu_to_le32(src->ln_bitmap_size);
235 }
236
237 static void lfsck_namespace_record_failure(const struct lu_env *env,
238                                            struct lfsck_instance *lfsck,
239                                            struct lfsck_namespace *ns)
240 {
241         struct lfsck_position pos;
242
243         ns->ln_items_failed++;
244         lfsck_pos_fill(env, lfsck, &pos, false);
245         if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent) ||
246             lfsck_pos_is_eq(&pos, &ns->ln_pos_first_inconsistent) < 0) {
247                 ns->ln_pos_first_inconsistent = pos;
248
249                 CDEBUG(D_LFSCK, "%s: namespace LFSCK hit first non-repaired "
250                        "inconsistency at the pos ["LPU64", "DFID", "LPX64"]\n",
251                        lfsck_lfsck2name(lfsck),
252                        ns->ln_pos_first_inconsistent.lp_oit_cookie,
253                        PFID(&ns->ln_pos_first_inconsistent.lp_dir_parent),
254                        ns->ln_pos_first_inconsistent.lp_dir_cookie);
255         }
256 }
257
258 /**
259  * Load the MDT bitmap from the lfsck_namespace trace file.
260  *
261  * \param[in] env       pointer to the thread context
262  * \param[in] com       pointer to the lfsck component
263  *
264  * \retval              0 for success
265  * \retval              negative error number on failure or data corruption
266  */
267 static int lfsck_namespace_load_bitmap(const struct lu_env *env,
268                                        struct lfsck_component *com)
269 {
270         struct dt_object                *obj    = com->lc_obj;
271         struct lfsck_assistant_data     *lad    = com->lc_data;
272         struct lfsck_namespace          *ns     = com->lc_file_ram;
273         cfs_bitmap_t                    *bitmap = lad->lad_bitmap;
274         ssize_t                          size;
275         __u32                            nbits;
276         int                              rc;
277         ENTRY;
278
279         if (com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size >
280             ns->ln_bitmap_size)
281                 nbits = com->lc_lfsck->li_mdt_descs.ltd_tgts_bitmap->size;
282         else
283                 nbits = ns->ln_bitmap_size;
284
285         if (unlikely(nbits < BITS_PER_LONG))
286                 nbits = BITS_PER_LONG;
287
288         if (nbits > bitmap->size) {
289                 __u32 new_bits = bitmap->size;
290                 cfs_bitmap_t *new_bitmap;
291
292                 while (new_bits < nbits)
293                         new_bits <<= 1;
294
295                 new_bitmap = CFS_ALLOCATE_BITMAP(new_bits);
296                 if (new_bitmap == NULL)
297                         RETURN(-ENOMEM);
298
299                 lad->lad_bitmap = new_bitmap;
300                 CFS_FREE_BITMAP(bitmap);
301                 bitmap = new_bitmap;
302         }
303
304         if (ns->ln_bitmap_size == 0) {
305                 lad->lad_incomplete = 0;
306                 CFS_RESET_BITMAP(bitmap);
307
308                 RETURN(0);
309         }
310
311         size = (ns->ln_bitmap_size + 7) >> 3;
312         rc = dt_xattr_get(env, obj,
313                           lfsck_buf_get(env, bitmap->data, size),
314                           XATTR_NAME_LFSCK_BITMAP, BYPASS_CAPA);
315         if (rc != size)
316                 RETURN(rc >= 0 ? -EINVAL : rc);
317
318         if (cfs_bitmap_check_empty(bitmap))
319                 lad->lad_incomplete = 0;
320         else
321                 lad->lad_incomplete = 1;
322
323         RETURN(0);
324 }
325
326 /**
327  * Load namespace LFSCK statistics information from the trace file.
328  *
329  * For old release (Lustre-2.6 or older), the statistics information was
330  * stored as XATTR_NAME_LFSCK_NAMESPACE_OLD EA. But in Lustre-2.7, we need
331  * more statistics information. To avoid confusing old MDT when downgrade,
332  * Lustre-2.7 stores the namespace LFSCK statistics information as new
333  * XATTR_NAME_LFSCK_NAMESPACE EA.
334  *
335  * \param[in] env       pointer to the thread context
336  * \param[in] com       pointer to the lfsck component
337  *
338  * \retval              0 for success
339  * \retval              negative error number on failure
340  */
341 static int lfsck_namespace_load(const struct lu_env *env,
342                                 struct lfsck_component *com)
343 {
344         int len = com->lc_file_size;
345         int rc;
346
347         rc = dt_xattr_get(env, com->lc_obj,
348                           lfsck_buf_get(env, com->lc_file_disk, len),
349                           XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
350         if (rc == len) {
351                 struct lfsck_namespace *ns = com->lc_file_ram;
352
353                 lfsck_namespace_le_to_cpu(ns,
354                                 (struct lfsck_namespace *)com->lc_file_disk);
355                 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
356                         CDEBUG(D_LFSCK, "%s: invalid lfsck_namespace magic "
357                                "%#x != %#x\n", lfsck_lfsck2name(com->lc_lfsck),
358                                ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
359                         rc = -ESTALE;
360                 } else {
361                         rc = 0;
362                 }
363         } else if (rc != -ENODATA) {
364                 CDEBUG(D_LFSCK, "%s: fail to load lfsck_namespace, "
365                        "expected = %d: rc = %d\n",
366                        lfsck_lfsck2name(com->lc_lfsck), len, rc);
367                 if (rc >= 0)
368                         rc = -ESTALE;
369         } else {
370                 /* Check whether it is old trace file or not.
371                  * If yes, it should be reset via returning -ESTALE. */
372                 rc = dt_xattr_get(env, com->lc_obj,
373                                   lfsck_buf_get(env, com->lc_file_disk, len),
374                                   XATTR_NAME_LFSCK_NAMESPACE_OLD, BYPASS_CAPA);
375                 if (rc >= 0)
376                         rc = -ESTALE;
377         }
378
379         return rc;
380 }
381
382 static int lfsck_namespace_store(const struct lu_env *env,
383                                  struct lfsck_component *com, bool init)
384 {
385         struct dt_object                *obj    = com->lc_obj;
386         struct lfsck_instance           *lfsck  = com->lc_lfsck;
387         struct lfsck_namespace          *ns     = com->lc_file_ram;
388         struct lfsck_assistant_data     *lad    = com->lc_data;
389         struct dt_device                *dev    = lfsck_obj2dev(obj);
390         cfs_bitmap_t                    *bitmap = NULL;
391         struct thandle                  *handle;
392         __u32                            nbits  = 0;
393         int                              len    = com->lc_file_size;
394         int                              rc;
395 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
396         struct lu_buf            tbuf   = { &len, sizeof(len) };
397 #endif
398         ENTRY;
399
400         if (lad != NULL) {
401                 bitmap = lad->lad_bitmap;
402                 nbits = bitmap->size;
403
404                 LASSERT(nbits > 0);
405                 LASSERTF((nbits & 7) == 0, "Invalid nbits %u\n", nbits);
406         }
407
408         ns->ln_bitmap_size = nbits;
409         lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
410                                   ns);
411         handle = dt_trans_create(env, dev);
412         if (IS_ERR(handle))
413                 GOTO(log, rc = PTR_ERR(handle));
414
415         rc = dt_declare_xattr_set(env, obj,
416                                   lfsck_buf_get(env, com->lc_file_disk, len),
417                                   XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
418         if (rc != 0)
419                 GOTO(out, rc);
420
421         if (bitmap != NULL) {
422                 rc = dt_declare_xattr_set(env, obj,
423                                 lfsck_buf_get(env, bitmap->data, nbits >> 3),
424                                 XATTR_NAME_LFSCK_BITMAP, 0, handle);
425                 if (rc != 0)
426                         GOTO(out, rc);
427         }
428
429 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
430         /* To be compatible with old Lustre-2.x MDT (x <= 6), generate dummy
431          * XATTR_NAME_LFSCK_NAMESPACE_OLD EA, then when downgrade to Lustre-2.x,
432          * the old LFSCK will find "invalid" XATTR_NAME_LFSCK_NAMESPACE_OLD EA,
433          * then reset the namespace LFSCK trace file. */
434         if (init) {
435                 rc = dt_declare_xattr_set(env, obj, &tbuf,
436                                           XATTR_NAME_LFSCK_NAMESPACE_OLD,
437                                           LU_XATTR_CREATE, handle);
438                 if (rc != 0)
439                         GOTO(out, rc);
440         }
441 #endif
442
443         rc = dt_trans_start_local(env, dev, handle);
444         if (rc != 0)
445                 GOTO(out, rc);
446
447         rc = dt_xattr_set(env, obj,
448                           lfsck_buf_get(env, com->lc_file_disk, len),
449                           XATTR_NAME_LFSCK_NAMESPACE, 0, handle, BYPASS_CAPA);
450         if (rc == 0 && bitmap != NULL)
451                 rc = dt_xattr_set(env, obj,
452                                   lfsck_buf_get(env, bitmap->data, nbits >> 3),
453                                   XATTR_NAME_LFSCK_BITMAP, 0, handle,
454                                   BYPASS_CAPA);
455
456 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 8, 53, 0)
457         if (rc == 0 && init)
458                 rc = dt_xattr_set(env, obj, &tbuf,
459                                   XATTR_NAME_LFSCK_NAMESPACE_OLD,
460                                   LU_XATTR_CREATE, handle, BYPASS_CAPA);
461 #endif
462
463         GOTO(out, rc);
464
465 out:
466         dt_trans_stop(env, dev, handle);
467
468 log:
469         if (rc != 0)
470                 CDEBUG(D_LFSCK, "%s: fail to store lfsck_namespace: rc = %d\n",
471                        lfsck_lfsck2name(lfsck), rc);
472         return rc;
473 }
474
475 static struct dt_object *
476 lfsck_namespace_load_one_trace_file(const struct lu_env *env,
477                                     struct lfsck_component *com,
478                                     struct dt_object *parent,
479                                     const char *name,
480                                     const struct dt_index_features *ft,
481                                     bool reset)
482 {
483         struct lfsck_instance   *lfsck = com->lc_lfsck;
484         struct dt_object        *obj;
485         int                      rc;
486
487         if (reset) {
488                 rc = local_object_unlink(env, lfsck->li_bottom, parent, name);
489                 if (rc != 0 && rc != -ENOENT)
490                         return ERR_PTR(rc);
491         }
492
493         if (ft != NULL)
494                 obj = local_index_find_or_create(env, lfsck->li_los, parent,
495                                         name, S_IFREG | S_IRUGO | S_IWUSR, ft);
496         else
497                 obj = local_file_find_or_create(env, lfsck->li_los, parent,
498                                         name, S_IFREG | S_IRUGO | S_IWUSR);
499
500         return obj;
501 }
502
503 static int lfsck_namespace_load_sub_trace_files(const struct lu_env *env,
504                                                 struct lfsck_component *com,
505                                                 bool reset)
506 {
507         char                            *name = lfsck_env_info(env)->lti_key;
508         struct lfsck_sub_trace_obj      *lsto;
509         struct dt_object                *obj;
510         int                              rc;
511         int                              i;
512
513         for (i = 0, lsto = &com->lc_sub_trace_objs[0];
514              i < LFSCK_STF_COUNT; i++, lsto++) {
515                 snprintf(name, NAME_MAX, "%s_%02d", LFSCK_NAMESPACE, i);
516                 if (lsto->lsto_obj != NULL) {
517                         if (!reset)
518                                 continue;
519
520                         lfsck_object_put(env, lsto->lsto_obj);
521                         lsto->lsto_obj = NULL;
522                 }
523
524                 obj = lfsck_namespace_load_one_trace_file(env, com,
525                                         com->lc_lfsck->li_lfsck_dir,
526                                         name, &dt_lfsck_features, reset);
527                 if (IS_ERR(obj))
528                         return PTR_ERR(obj);
529
530                 lsto->lsto_obj = obj;
531                 rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
532                 if (rc != 0)
533                         return rc;
534         }
535
536         return 0;
537 }
538
539 static int lfsck_namespace_init(const struct lu_env *env,
540                                 struct lfsck_component *com)
541 {
542         struct lfsck_namespace *ns = com->lc_file_ram;
543         int rc;
544
545         memset(ns, 0, sizeof(*ns));
546         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
547         ns->ln_status = LS_INIT;
548         down_write(&com->lc_sem);
549         rc = lfsck_namespace_store(env, com, true);
550         up_write(&com->lc_sem);
551         if (rc == 0)
552                 rc = lfsck_namespace_load_sub_trace_files(env, com, true);
553
554         return rc;
555 }
556
557 /**
558  * Update the namespace LFSCK trace file for the given @fid
559  *
560  * \param[in] env       pointer to the thread context
561  * \param[in] com       pointer to the lfsck component
562  * \param[in] fid       the fid which flags to be updated in the lfsck
563  *                      trace file
564  * \param[in] add       true if add new flags, otherwise remove flags
565  *
566  * \retval              0 for succeed or nothing to be done
567  * \retval              negative error number on failure
568  */
569 int lfsck_namespace_trace_update(const struct lu_env *env,
570                                  struct lfsck_component *com,
571                                  const struct lu_fid *fid,
572                                  const __u8 flags, bool add)
573 {
574         struct lfsck_instance   *lfsck  = com->lc_lfsck;
575         struct dt_object        *obj;
576         struct lu_fid           *key    = &lfsck_env_info(env)->lti_fid3;
577         struct dt_device        *dev;
578         struct thandle          *th     = NULL;
579         int                      idx;
580         int                      rc     = 0;
581         __u8                     old    = 0;
582         __u8                     new    = 0;
583         ENTRY;
584
585         LASSERT(flags != 0);
586
587         if (unlikely(!fid_is_sane(fid)))
588                 RETURN(0);
589
590         idx = lfsck_sub_trace_file_fid2idx(fid);
591         obj = com->lc_sub_trace_objs[idx].lsto_obj;
592         dev = lfsck_obj2dev(obj);
593         mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
594         fid_cpu_to_be(key, fid);
595         rc = dt_lookup(env, obj, (struct dt_rec *)&old,
596                        (const struct dt_key *)key, BYPASS_CAPA);
597         if (rc == -ENOENT) {
598                 if (!add)
599                         GOTO(unlock, rc = 0);
600
601                 old = 0;
602                 new = flags;
603         } else if (rc == 0) {
604                 if (add) {
605                         if ((old & flags) == flags)
606                                 GOTO(unlock, rc = 0);
607
608                         new = old | flags;
609                 } else {
610                         if ((old & flags) == 0)
611                                 GOTO(unlock, rc = 0);
612
613                         new = old & ~flags;
614                 }
615         } else {
616                 GOTO(log, rc);
617         }
618
619         th = dt_trans_create(env, dev);
620         if (IS_ERR(th))
621                 GOTO(log, rc = PTR_ERR(th));
622
623         if (old != 0) {
624                 rc = dt_declare_delete(env, obj,
625                                        (const struct dt_key *)key, th);
626                 if (rc != 0)
627                         GOTO(log, rc);
628         }
629
630         if (new != 0) {
631                 rc = dt_declare_insert(env, obj,
632                                        (const struct dt_rec *)&new,
633                                        (const struct dt_key *)key, th);
634                 if (rc != 0)
635                         GOTO(log, rc);
636         }
637
638         rc = dt_trans_start_local(env, dev, th);
639         if (rc != 0)
640                 GOTO(log, rc);
641
642         if (old != 0) {
643                 rc = dt_delete(env, obj, (const struct dt_key *)key,
644                                th, BYPASS_CAPA);
645                 if (rc != 0)
646                         GOTO(log, rc);
647         }
648
649         if (new != 0) {
650                 rc = dt_insert(env, obj, (const struct dt_rec *)&new,
651                                (const struct dt_key *)key, th, BYPASS_CAPA, 1);
652                 if (rc != 0)
653                         GOTO(log, rc);
654         }
655
656         GOTO(log, rc);
657
658 log:
659         if (th != NULL && !IS_ERR(th))
660                 dt_trans_stop(env, dev, th);
661
662         CDEBUG(D_LFSCK, "%s: namespace LFSCK %s flags for "DFID" in the "
663                "trace file, flags %x, old %x, new %x: rc = %d\n",
664                lfsck_lfsck2name(lfsck), add ? "add" : "del", PFID(fid),
665                (__u32)flags, (__u32)old, (__u32)new, rc);
666
667 unlock:
668         mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
669
670         return rc;
671 }
672
673 int lfsck_namespace_check_exist(const struct lu_env *env,
674                                 struct dt_object *dir,
675                                 struct dt_object *obj, const char *name)
676 {
677         struct lu_fid    *fid = &lfsck_env_info(env)->lti_fid;
678         int               rc;
679         ENTRY;
680
681         if (unlikely(lfsck_is_dead_obj(obj)))
682                 RETURN(LFSCK_NAMEENTRY_DEAD);
683
684         rc = dt_lookup(env, dir, (struct dt_rec *)fid,
685                        (const struct dt_key *)name, BYPASS_CAPA);
686         if (rc == -ENOENT)
687                 RETURN(LFSCK_NAMEENTRY_REMOVED);
688
689         if (rc < 0)
690                 RETURN(rc);
691
692         if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
693                 RETURN(LFSCK_NAMEENTRY_RECREATED);
694
695         RETURN(0);
696 }
697
698 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
699                                             struct dt_object *obj,
700                                             struct thandle *handle)
701 {
702         int rc;
703
704         /* For destroying all invalid linkEA entries. */
705         rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
706         if (rc != 0)
707                 return rc;
708
709         /* For insert new linkEA entry. */
710         rc = dt_declare_xattr_set(env, obj,
711                         lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
712                         XATTR_NAME_LINK, 0, handle);
713         return rc;
714 }
715
716 int __lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
717                        struct linkea_data *ldata)
718 {
719         int rc;
720
721         if (ldata->ld_buf->lb_buf == NULL)
722                 return -ENOMEM;
723
724         if (!dt_object_exists(obj))
725                 return -ENOENT;
726
727         rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
728         if (rc == -ERANGE) {
729                 /* Buf was too small, figure out what we need. */
730                 rc = dt_xattr_get(env, obj, &LU_BUF_NULL, XATTR_NAME_LINK,
731                                   BYPASS_CAPA);
732                 if (rc <= 0)
733                         return rc;
734
735                 lu_buf_realloc(ldata->ld_buf, rc);
736                 if (ldata->ld_buf->lb_buf == NULL)
737                         return -ENOMEM;
738
739                 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
740                                   BYPASS_CAPA);
741         }
742
743         if (rc > 0)
744                 rc = linkea_init(ldata);
745
746         return rc;
747 }
748
749 /**
750  * Remove linkEA for the given object.
751  *
752  * The caller should take the ldlm lock before the calling.
753  *
754  * \param[in] env       pointer to the thread context
755  * \param[in] com       pointer to the lfsck component
756  * \param[in] obj       pointer to the dt_object to be handled
757  *
758  * \retval              0 for repaired cases
759  * \retval              negative error number on failure
760  */
761 static int lfsck_namespace_links_remove(const struct lu_env *env,
762                                         struct lfsck_component *com,
763                                         struct dt_object *obj)
764 {
765         struct lfsck_instance           *lfsck  = com->lc_lfsck;
766         struct dt_device                *dev    = lfsck_obj2dev(obj);
767         struct thandle                  *th     = NULL;
768         int                              rc     = 0;
769         ENTRY;
770
771         LASSERT(dt_object_remote(obj) == 0);
772
773         th = dt_trans_create(env, dev);
774         if (IS_ERR(th))
775                 GOTO(log, rc = PTR_ERR(th));
776
777         rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, th);
778         if (rc != 0)
779                 GOTO(stop, rc);
780
781         rc = dt_trans_start_local(env, dev, th);
782         if (rc != 0)
783                 GOTO(stop, rc);
784
785         dt_write_lock(env, obj, 0);
786         if (unlikely(lfsck_is_dead_obj(obj)))
787                 GOTO(unlock, rc = -ENOENT);
788
789         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
790                 GOTO(unlock, rc = 0);
791
792         rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, th, BYPASS_CAPA);
793
794         GOTO(unlock, rc);
795
796 unlock:
797         dt_write_unlock(env, obj);
798
799 stop:
800         dt_trans_stop(env, dev, th);
801
802 log:
803         CDEBUG(D_LFSCK, "%s: namespace LFSCK remove invalid linkEA "
804                "for the object "DFID": rc = %d\n",
805                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
806
807         if (rc == 0) {
808                 struct lfsck_namespace *ns = com->lc_file_ram;
809
810                 ns->ln_flags |= LF_INCONSISTENT;
811         }
812
813         return rc;
814 }
815
816 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
817                              struct linkea_data *ldata, struct thandle *handle)
818 {
819         const struct lu_buf *buf = lfsck_buf_get_const(env,
820                                                        ldata->ld_buf->lb_buf,
821                                                        ldata->ld_leh->leh_len);
822
823         return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
824                             BYPASS_CAPA);
825 }
826
827 static void lfsck_namespace_unpack_linkea_entry(struct linkea_data *ldata,
828                                                 struct lu_name *cname,
829                                                 struct lu_fid *pfid,
830                                                 char *buf)
831 {
832         linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
833         /* To guarantee the 'name' is terminated with '0'. */
834         memcpy(buf, cname->ln_name, cname->ln_namelen);
835         buf[cname->ln_namelen] = 0;
836         cname->ln_name = buf;
837 }
838
839 static int lfsck_namespace_filter_linkea_entry(struct linkea_data *ldata,
840                                                struct lu_name *cname,
841                                                struct lu_fid *pfid,
842                                                bool remove)
843 {
844         struct link_ea_entry    *oldlee;
845         int                      oldlen;
846         int                      repeated = 0;
847
848         oldlee = ldata->ld_lee;
849         oldlen = ldata->ld_reclen;
850         linkea_next_entry(ldata);
851         while (ldata->ld_lee != NULL) {
852                 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
853                                    ldata->ld_lee->lee_reclen[1];
854                 if (unlikely(ldata->ld_reclen == oldlen &&
855                              memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
856                         repeated++;
857                         if (!remove)
858                                 break;
859
860                         linkea_del_buf(ldata, cname);
861                 } else {
862                         linkea_next_entry(ldata);
863                 }
864         }
865         ldata->ld_lee = oldlee;
866         ldata->ld_reclen = oldlen;
867
868         return repeated;
869 }
870
871 /**
872  * Insert orphan into .lustre/lost+found/MDTxxxx/ locally.
873  *
874  * Add the specified orphan MDT-object to the .lustre/lost+found/MDTxxxx/
875  * with the given type to generate the name, the detailed rules for name
876  * have been described as following.
877  *
878  * The function also generates the linkEA corresponding to the name entry
879  * under the .lustre/lost+found/MDTxxxx/ for the orphan MDT-object.
880  *
881  * \param[in] env       pointer to the thread context
882  * \param[in] com       pointer to the lfsck component
883  * \param[in] orphan    pointer to the orphan MDT-object
884  * \param[in] infix     additional information for the orphan name, such as
885  *                      the FID for original
886  * \param[in] type      the type for describing why the orphan MDT-object is
887  *                      created. The rules are as following:
888  *
889  *  type "D":           The MDT-object is a directory, it may knows its parent
890  *                      but because there is no valid linkEA, the LFSCK cannot
891  *                      know where to put it back to the namespace.
892  *  type "O":           The MDT-object has no linkEA, and there is no name
893  *                      entry that references the MDT-object.
894  *
895  *  type "S":           The orphan MDT-object is a shard of a striped directory
896  *
897  * \see lfsck_layout_recreate_parent() for more types.
898  *
899  * The orphan name will be like:
900  * ${FID}-${infix}-${type}-${conflict_version}
901  *
902  * \param[out] count    if some others inserted some linkEA entries by race,
903  *                      then return the linkEA entries count.
904  *
905  * \retval              positive number for repaired cases
906  * \retval              0 if needs to repair nothing
907  * \retval              negative error number on failure
908  */
909 static int lfsck_namespace_insert_orphan(const struct lu_env *env,
910                                          struct lfsck_component *com,
911                                          struct dt_object *orphan,
912                                          const char *infix, const char *type,
913                                          int *count)
914 {
915         struct lfsck_thread_info        *info   = lfsck_env_info(env);
916         struct lu_name                  *cname  = &info->lti_name;
917         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
918         struct lu_attr                  *la     = &info->lti_la3;
919         const struct lu_fid             *cfid   = lfsck_dto2fid(orphan);
920         const struct lu_fid             *pfid;
921         struct lu_fid                    tfid;
922         struct lfsck_instance           *lfsck  = com->lc_lfsck;
923         struct dt_device                *dev    = lfsck_obj2dev(orphan);
924         struct dt_object                *parent;
925         struct thandle                  *th     = NULL;
926         struct lustre_handle             plh    = { 0 };
927         struct lustre_handle             clh    = { 0 };
928         struct linkea_data               ldata  = { NULL };
929         struct lu_buf                    linkea_buf;
930         int                              namelen;
931         int                              idx    = 0;
932         int                              rc     = 0;
933         bool                             exist  = false;
934         ENTRY;
935
936         cname->ln_name = NULL;
937         if (unlikely(lfsck->li_lpf_obj == NULL))
938                 GOTO(log, rc = -ENXIO);
939
940         parent = lfsck->li_lpf_obj;
941         pfid = lfsck_dto2fid(parent);
942
943         /* Hold update lock on the parent to prevent others to access. */
944         rc = lfsck_ibits_lock(env, lfsck, parent, &plh,
945                               MDS_INODELOCK_UPDATE, LCK_EX);
946         if (rc != 0)
947                 GOTO(log, rc);
948
949         do {
950                 namelen = snprintf(info->lti_key, NAME_MAX, DFID"%s-%s-%d",
951                                    PFID(cfid), infix, type, idx++);
952                 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
953                                (const struct dt_key *)info->lti_key,
954                                BYPASS_CAPA);
955                 if (rc != 0 && rc != -ENOENT)
956                         GOTO(log, rc);
957
958                 if (unlikely(rc == 0 && lu_fid_eq(cfid, &tfid)))
959                         exist = true;
960         } while (rc == 0 && !exist);
961
962         cname->ln_name = info->lti_key;
963         cname->ln_namelen = namelen;
964         rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
965         if (rc != 0)
966                 GOTO(log, rc);
967
968         rc = linkea_add_buf(&ldata, cname, pfid);
969         if (rc != 0)
970                 GOTO(log, rc);
971
972         rc = lfsck_ibits_lock(env, lfsck, orphan, &clh,
973                               MDS_INODELOCK_UPDATE | MDS_INODELOCK_LOOKUP,
974                               LCK_EX);
975         if (rc != 0)
976                 GOTO(log, rc);
977
978         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
979                        ldata.ld_leh->leh_len);
980         th = dt_trans_create(env, dev);
981         if (IS_ERR(th))
982                 GOTO(log, rc = PTR_ERR(th));
983
984         if (S_ISDIR(lfsck_object_type(orphan))) {
985                 rc = dt_declare_delete(env, orphan,
986                                        (const struct dt_key *)dotdot, th);
987                 if (rc != 0)
988                         GOTO(stop, rc);
989
990                 rec->rec_type = S_IFDIR;
991                 rec->rec_fid = pfid;
992                 rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
993                                        (const struct dt_key *)dotdot, th);
994                 if (rc != 0)
995                         GOTO(stop, rc);
996         }
997
998         rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
999                                   XATTR_NAME_LINK, 0, th);
1000         if (rc != 0)
1001                 GOTO(stop, rc);
1002
1003         if (!exist) {
1004                 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1005                 rec->rec_fid = cfid;
1006                 rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1007                                        (const struct dt_key *)cname->ln_name,
1008                                        th);
1009                 if (rc != 0)
1010                         GOTO(stop, rc);
1011
1012                 if (S_ISDIR(rec->rec_type)) {
1013                         rc = dt_declare_ref_add(env, parent, th);
1014                         if (rc != 0)
1015                                 GOTO(stop, rc);
1016                 }
1017         }
1018
1019         memset(la, 0, sizeof(*la));
1020         la->la_ctime = cfs_time_current_sec();
1021         la->la_valid = LA_CTIME;
1022         rc = dt_declare_attr_set(env, orphan, la, th);
1023         if (rc != 0)
1024                 GOTO(stop, rc);
1025
1026         rc = dt_trans_start_local(env, dev, th);
1027         if (rc != 0)
1028                 GOTO(stop, rc);
1029
1030         dt_write_lock(env, orphan, 0);
1031         rc = lfsck_links_read(env, orphan, &ldata);
1032         if (likely((rc == -ENODATA) || (rc == -EINVAL) ||
1033                    (rc == 0 && ldata.ld_leh->leh_reccount == 0))) {
1034                 if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1035                         GOTO(unlock, rc = 1);
1036
1037                 if (S_ISDIR(lfsck_object_type(orphan))) {
1038                         rc = dt_delete(env, orphan,
1039                                        (const struct dt_key *)dotdot, th,
1040                                        BYPASS_CAPA);
1041                         if (rc != 0)
1042                                 GOTO(unlock, rc);
1043
1044                         rec->rec_type = S_IFDIR;
1045                         rec->rec_fid = pfid;
1046                         rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1047                                        (const struct dt_key *)dotdot, th,
1048                                        BYPASS_CAPA, 1);
1049                         if (rc != 0)
1050                                 GOTO(unlock, rc);
1051                 }
1052
1053                 rc = dt_xattr_set(env, orphan, &linkea_buf, XATTR_NAME_LINK, 0,
1054                                   th, BYPASS_CAPA);
1055         } else {
1056                 if (rc == 0 && count != NULL)
1057                         *count = ldata.ld_leh->leh_reccount;
1058
1059                 GOTO(unlock, rc);
1060         }
1061         dt_write_unlock(env, orphan);
1062
1063         if (rc == 0 && !exist) {
1064                 rec->rec_type = lfsck_object_type(orphan) & S_IFMT;
1065                 rec->rec_fid = cfid;
1066                 rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1067                                (const struct dt_key *)cname->ln_name,
1068                                th, BYPASS_CAPA, 1);
1069                 if (rc == 0 && S_ISDIR(rec->rec_type)) {
1070                         dt_write_lock(env, parent, 0);
1071                         rc = dt_ref_add(env, parent, th);
1072                         dt_write_unlock(env, parent);
1073                 }
1074         }
1075
1076         if (rc == 0)
1077                 rc = dt_attr_set(env, orphan, la, th, BYPASS_CAPA);
1078
1079         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1080
1081 unlock:
1082         dt_write_unlock(env, orphan);
1083
1084 stop:
1085         dt_trans_stop(env, dev, th);
1086
1087 log:
1088         lfsck_ibits_unlock(&clh, LCK_EX);
1089         lfsck_ibits_unlock(&plh, LCK_EX);
1090         CDEBUG(D_LFSCK, "%s: namespace LFSCK insert orphan for the "
1091                "object "DFID", name = %s: rc = %d\n",
1092                lfsck_lfsck2name(lfsck), PFID(cfid),
1093                cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1094
1095         if (rc != 0) {
1096                 struct lfsck_namespace *ns = com->lc_file_ram;
1097
1098                 ns->ln_flags |= LF_INCONSISTENT;
1099         }
1100
1101         return rc;
1102 }
1103
1104 /**
1105  * Add the specified name entry back to namespace.
1106  *
1107  * If there is a linkEA entry that back references a name entry under
1108  * some parent directory, but such parent directory does not have the
1109  * claimed name entry. On the other hand, the linkEA entries count is
1110  * not larger than the MDT-object's hard link count. Under such case,
1111  * it is quite possible that the name entry is lost. Then the LFSCK
1112  * should add the name entry back to the namespace.
1113  *
1114  * \param[in] env       pointer to the thread context
1115  * \param[in] com       pointer to the lfsck component
1116  * \param[in] parent    pointer to the directory under which the name entry
1117  *                      will be inserted into
1118  * \param[in] child     pointer to the object referenced by the name entry
1119  *                      that to be inserted into the parent
1120  * \param[in] name      the name for the child in the parent directory
1121  *
1122  * \retval              positive number for repaired cases
1123  * \retval              0 if nothing to be repaired
1124  * \retval              negative error number on failure
1125  */
1126 static int lfsck_namespace_insert_normal(const struct lu_env *env,
1127                                          struct lfsck_component *com,
1128                                          struct dt_object *parent,
1129                                          struct dt_object *child,
1130                                          const char *name)
1131 {
1132         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1133         struct lu_attr                  *la     = &info->lti_la;
1134         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
1135         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1136         /* The child and its name may be on different MDTs. */
1137         struct dt_device                *dev    = lfsck->li_next;
1138         struct dt_object                *pobj   = NULL;
1139         struct dt_object                *cobj   = NULL;
1140         struct thandle                  *th     = NULL;
1141         struct lustre_handle             lh     = { 0 };
1142         int                              rc     = 0;
1143         ENTRY;
1144
1145         /* @parent/@child may be based on lfsck->li_bottom,
1146          * but here we need the object based on the lfsck->li_next. */
1147
1148         pobj = lfsck_object_locate(dev, parent);
1149         if (IS_ERR(pobj))
1150                 GOTO(log, rc = PTR_ERR(pobj));
1151
1152         if (unlikely(!dt_try_as_dir(env, pobj)))
1153                 GOTO(log, rc = -ENOTDIR);
1154
1155         cobj = lfsck_object_locate(dev, child);
1156         if (IS_ERR(cobj))
1157                 GOTO(log, rc = PTR_ERR(cobj));
1158
1159         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1160                 GOTO(log, rc = 1);
1161
1162         /* Hold update lock on the pobj to prevent others to access. */
1163         rc = lfsck_ibits_lock(env, lfsck, pobj, &lh,
1164                               MDS_INODELOCK_UPDATE, LCK_EX);
1165         if (rc != 0)
1166                 GOTO(log, rc);
1167
1168         th = dt_trans_create(env, dev);
1169         if (IS_ERR(th))
1170                 GOTO(unlock, rc = PTR_ERR(th));
1171
1172         rec->rec_type = lfsck_object_type(cobj) & S_IFMT;
1173         rec->rec_fid = lfsck_dto2fid(cobj);
1174         rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1175                                (const struct dt_key *)name, th);
1176         if (rc != 0)
1177                 GOTO(stop, rc);
1178
1179         if (S_ISDIR(rec->rec_type)) {
1180                 rc = dt_declare_ref_add(env, pobj, th);
1181                 if (rc != 0)
1182                         GOTO(stop, rc);
1183         }
1184
1185         memset(la, 0, sizeof(*la));
1186         la->la_ctime = cfs_time_current_sec();
1187         la->la_valid = LA_CTIME;
1188         rc = dt_declare_attr_set(env, pobj, la, th);
1189         if (rc != 0)
1190                 GOTO(stop, rc);
1191
1192         rc = dt_declare_attr_set(env, cobj, la, th);
1193         if (rc != 0)
1194                 GOTO(stop, rc);
1195
1196         rc = dt_trans_start_local(env, dev, th);
1197         if (rc != 0)
1198                 GOTO(stop, rc);
1199
1200         rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1201                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1202         if (rc != 0)
1203                 GOTO(stop, rc);
1204
1205         if (S_ISDIR(rec->rec_type)) {
1206                 dt_write_lock(env, pobj, 0);
1207                 rc = dt_ref_add(env, pobj, th);
1208                 dt_write_unlock(env, pobj);
1209                 if (rc != 0)
1210                         GOTO(stop, rc);
1211         }
1212
1213         la->la_ctime = cfs_time_current_sec();
1214         rc = dt_attr_set(env, pobj, la, th, BYPASS_CAPA);
1215         if (rc != 0)
1216                 GOTO(stop, rc);
1217
1218         rc = dt_attr_set(env, cobj, la, th, BYPASS_CAPA);
1219
1220         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1221
1222 stop:
1223         dt_trans_stop(env, dev, th);
1224
1225 unlock:
1226         lfsck_ibits_unlock(&lh, LCK_EX);
1227
1228 log:
1229         CDEBUG(D_LFSCK, "%s: namespace LFSCK insert object "DFID" with "
1230                "the name %s and type %o to the parent "DFID": rc = %d\n",
1231                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(child)), name,
1232                lfsck_object_type(child) & S_IFMT,
1233                PFID(lfsck_dto2fid(parent)), rc);
1234
1235         if (rc != 0) {
1236                 struct lfsck_namespace *ns = com->lc_file_ram;
1237
1238                 ns->ln_flags |= LF_INCONSISTENT;
1239                 if (rc > 0)
1240                         ns->ln_lost_dirent_repaired++;
1241         }
1242
1243         return rc;
1244 }
1245
1246 /**
1247  * Create the specified orphan directory.
1248  *
1249  * For the case that the parent MDT-object stored in some MDT-object's
1250  * linkEA entry is lost, the LFSCK will re-create the parent object as
1251  * an orphan and insert it into .lustre/lost+found/MDTxxxx/ directory
1252  * with the name ${FID}-P-${conflict_version}.
1253  *
1254  * \param[in] env       pointer to the thread context
1255  * \param[in] com       pointer to the lfsck component
1256  * \param[in] orphan    pointer to the orphan MDT-object to be created
1257  * \param[in] lmv       pointer to master LMV EA that will be set to the orphan
1258  *
1259  * \retval              positive number for repaired cases
1260  * \retval              negative error number on failure
1261  */
1262 static int lfsck_namespace_create_orphan_dir(const struct lu_env *env,
1263                                              struct lfsck_component *com,
1264                                              struct dt_object *orphan,
1265                                              struct lmv_mds_md_v1 *lmv)
1266 {
1267         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1268         struct lu_attr                  *la     = &info->lti_la;
1269         struct dt_allocation_hint       *hint   = &info->lti_hint;
1270         struct dt_object_format         *dof    = &info->lti_dof;
1271         struct lu_name                  *cname  = &info->lti_name2;
1272         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
1273         struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
1274         const struct lu_fid             *cfid   = lfsck_dto2fid(orphan);
1275         struct lu_fid                    tfid;
1276         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1277         struct lfsck_namespace          *ns     = com->lc_file_ram;
1278         struct dt_device                *dev    = lfsck_obj2dev(orphan);
1279         struct dt_object                *parent = NULL;
1280         struct thandle                  *th     = NULL;
1281         struct lustre_handle             lh     = { 0 };
1282         struct linkea_data               ldata  = { NULL };
1283         struct lu_buf                    linkea_buf;
1284         struct lu_buf                    lmv_buf;
1285         char                             name[32];
1286         int                              namelen;
1287         int                              idx    = 0;
1288         int                              rc     = 0;
1289         ENTRY;
1290
1291         LASSERT(!dt_object_exists(orphan));
1292
1293         cname->ln_name = NULL;
1294         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1295                 GOTO(log, rc = 1);
1296
1297         if (dt_object_remote(orphan)) {
1298                 LASSERT(lfsck->li_lpf_root_obj != NULL);
1299
1300                 idx = lfsck_find_mdt_idx_by_fid(env, lfsck, cfid);
1301                 if (idx < 0)
1302                         GOTO(log, rc = idx);
1303
1304                 snprintf(name, 8, "MDT%04x", idx);
1305                 rc = dt_lookup(env, lfsck->li_lpf_root_obj,
1306                                (struct dt_rec *)&tfid,
1307                                (const struct dt_key *)name, BYPASS_CAPA);
1308                 if (rc != 0)
1309                         GOTO(log, rc = (rc == -ENOENT ? -ENXIO : rc));
1310
1311                 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
1312                 if (IS_ERR(parent))
1313                         GOTO(log, rc = PTR_ERR(parent));
1314
1315                 if (unlikely(!dt_try_as_dir(env, parent)))
1316                         GOTO(log, rc = -ENOTDIR);
1317         } else {
1318                 if (unlikely(lfsck->li_lpf_obj == NULL))
1319                         GOTO(log, rc = -ENXIO);
1320
1321                 parent = lfsck->li_lpf_obj;
1322         }
1323
1324         dev = lfsck_find_dev_by_fid(env, lfsck, cfid);
1325         if (IS_ERR(dev))
1326                 GOTO(log, rc = PTR_ERR(dev));
1327
1328         /* Hold update lock on the parent to prevent others to access. */
1329         rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
1330                               MDS_INODELOCK_UPDATE, LCK_EX);
1331         if (rc != 0)
1332                 GOTO(log, rc);
1333
1334         idx = 0;
1335         do {
1336                 namelen = snprintf(name, 31, DFID"-P-%d",
1337                                    PFID(cfid), idx++);
1338                 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
1339                                (const struct dt_key *)name, BYPASS_CAPA);
1340                 if (rc != 0 && rc != -ENOENT)
1341                         GOTO(unlock1, rc);
1342         } while (rc == 0);
1343
1344         cname->ln_name = name;
1345         cname->ln_namelen = namelen;
1346
1347         memset(la, 0, sizeof(*la));
1348         la->la_mode = S_IFDIR | 0700;
1349         la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
1350                        LA_ATIME | LA_MTIME | LA_CTIME;
1351
1352         orphan->do_ops->do_ah_init(env, hint, parent, orphan,
1353                                    la->la_mode & S_IFMT);
1354
1355         memset(dof, 0, sizeof(*dof));
1356         dof->dof_type = dt_mode_to_dft(S_IFDIR);
1357
1358         rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
1359         if (rc != 0)
1360                 GOTO(unlock1, rc);
1361
1362         rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(parent));
1363         if (rc != 0)
1364                 GOTO(unlock1, rc);
1365
1366         th = dt_trans_create(env, dev);
1367         if (IS_ERR(th))
1368                 GOTO(unlock1, rc = PTR_ERR(th));
1369
1370         /* Sync the remote transaction to guarantee that the subsequent
1371          * lock against the @orphan can find the @orphan in time. */
1372         if (dt_object_remote(orphan))
1373                 th->th_sync = 1;
1374
1375         rc = dt_declare_create(env, orphan, la, hint, dof, th);
1376         if (rc != 0)
1377                 GOTO(stop, rc);
1378
1379         if (unlikely(!dt_try_as_dir(env, orphan)))
1380                 GOTO(stop, rc = -ENOTDIR);
1381
1382         rec->rec_type = S_IFDIR;
1383         rec->rec_fid = cfid;
1384         rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1385                                (const struct dt_key *)dot, th);
1386         if (rc != 0)
1387                 GOTO(stop, rc);
1388
1389         rec->rec_fid = lfsck_dto2fid(parent);
1390         rc = dt_declare_insert(env, orphan, (const struct dt_rec *)rec,
1391                                (const struct dt_key *)dotdot, th);
1392         if (rc == 0)
1393                 rc = dt_declare_ref_add(env, orphan, th);
1394
1395         if (rc != 0)
1396                 GOTO(stop, rc);
1397
1398         rc = dt_declare_ref_add(env, orphan, th);
1399         if (rc != 0)
1400                 GOTO(stop, rc);
1401
1402         if (lmv != NULL) {
1403                 lmv->lmv_magic = LMV_MAGIC;
1404                 lmv->lmv_master_mdt_index = lfsck_dev_idx(lfsck);
1405                 lfsck_lmv_header_cpu_to_le(lmv2, lmv);
1406                 lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
1407                 rc = dt_declare_xattr_set(env, orphan, &lmv_buf,
1408                                           XATTR_NAME_LMV, 0, th);
1409                 if (rc != 0)
1410                         GOTO(stop, rc);
1411         }
1412
1413         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
1414                        ldata.ld_leh->leh_len);
1415         rc = dt_declare_xattr_set(env, orphan, &linkea_buf,
1416                                   XATTR_NAME_LINK, 0, th);
1417         if (rc != 0)
1418                 GOTO(stop, rc);
1419
1420         rec->rec_fid = cfid;
1421         rc = dt_declare_insert(env, parent, (const struct dt_rec *)rec,
1422                                (const struct dt_key *)name, th);
1423         if (rc == 0)
1424                 rc = dt_declare_ref_add(env, parent, th);
1425
1426         if (rc != 0)
1427                 GOTO(stop, rc);
1428
1429         rc = dt_trans_start_local(env, dev, th);
1430         if (rc != 0)
1431                 GOTO(stop, rc);
1432
1433         dt_write_lock(env, orphan, 0);
1434         rc = dt_create(env, orphan, la, hint, dof, th);
1435         if (rc != 0)
1436                 GOTO(unlock2, rc);
1437
1438         rec->rec_fid = cfid;
1439         rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1440                        (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
1441         if (rc != 0)
1442                 GOTO(unlock2, rc);
1443
1444         rec->rec_fid = lfsck_dto2fid(parent);
1445         rc = dt_insert(env, orphan, (const struct dt_rec *)rec,
1446                        (const struct dt_key *)dotdot, th,
1447                        BYPASS_CAPA, 1);
1448         if (rc != 0)
1449                 GOTO(unlock2, rc);
1450
1451         rc = dt_ref_add(env, orphan, th);
1452         if (rc != 0)
1453                 GOTO(unlock2, rc);
1454
1455         if (lmv != NULL) {
1456                 rc = dt_xattr_set(env, orphan, &lmv_buf, XATTR_NAME_LMV, 0,
1457                                   th, BYPASS_CAPA);
1458                 if (rc != 0)
1459                         GOTO(unlock2, rc);
1460         }
1461
1462         rc = dt_xattr_set(env, orphan, &linkea_buf,
1463                           XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1464         dt_write_unlock(env, orphan);
1465         if (rc != 0)
1466                 GOTO(stop, rc);
1467
1468         rec->rec_fid = cfid;
1469         rc = dt_insert(env, parent, (const struct dt_rec *)rec,
1470                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1471         if (rc == 0) {
1472                 dt_write_lock(env, parent, 0);
1473                 rc = dt_ref_add(env, parent, th);
1474                 dt_write_unlock(env, parent);
1475         }
1476
1477         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1478
1479 unlock2:
1480         dt_write_unlock(env, orphan);
1481
1482 stop:
1483         dt_trans_stop(env, dev, th);
1484
1485 unlock1:
1486         lfsck_ibits_unlock(&lh, LCK_EX);
1487
1488 log:
1489         CDEBUG(D_LFSCK, "%s: namespace LFSCK create orphan dir for "
1490                "the object "DFID", name = %s: rc = %d\n",
1491                lfsck_lfsck2name(lfsck), PFID(cfid),
1492                cname->ln_name != NULL ? cname->ln_name : "<NULL>", rc);
1493
1494         if (parent != NULL && !IS_ERR(parent) && parent != lfsck->li_lpf_obj)
1495                 lfsck_object_put(env, parent);
1496
1497         if (rc != 0)
1498                 ns->ln_flags |= LF_INCONSISTENT;
1499
1500         return rc;
1501 }
1502
1503 /**
1504  * Remove the specified entry from the linkEA.
1505  *
1506  * Locate the linkEA entry with the given @cname and @pfid, then
1507  * remove this entry or the other entries those are repeated with
1508  * this entry.
1509  *
1510  * \param[in] env       pointer to the thread context
1511  * \param[in] com       pointer to the lfsck component
1512  * \param[in] obj       pointer to the dt_object to be handled
1513  * \param[in,out]ldata  pointer to the buffer that holds the linkEA
1514  * \param[in] cname     the name for the child in the parent directory
1515  * \param[in] pfid      the parent directory's FID for the linkEA
1516  * \param[in] next      if true, then remove the first found linkEA
1517  *                      entry, and move the ldata->ld_lee to next entry
1518  *
1519  * \retval              positive number for repaired cases
1520  * \retval              0 if nothing to be repaired
1521  * \retval              negative error number on failure
1522  */
1523 static int lfsck_namespace_shrink_linkea(const struct lu_env *env,
1524                                          struct lfsck_component *com,
1525                                          struct dt_object *obj,
1526                                          struct linkea_data *ldata,
1527                                          struct lu_name *cname,
1528                                          struct lu_fid *pfid,
1529                                          bool next)
1530 {
1531         struct lfsck_instance           *lfsck     = com->lc_lfsck;
1532         struct dt_device                *dev       = lfsck_obj2dev(obj);
1533         struct lfsck_bookmark           *bk        = &lfsck->li_bookmark_ram;
1534         struct thandle                  *th        = NULL;
1535         struct lustre_handle             lh        = { 0 };
1536         struct linkea_data               ldata_new = { NULL };
1537         struct lu_buf                    linkea_buf;
1538         int                              rc        = 0;
1539         ENTRY;
1540
1541         rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
1542                               MDS_INODELOCK_UPDATE |
1543                               MDS_INODELOCK_XATTR, LCK_EX);
1544         if (rc != 0)
1545                 GOTO(log, rc);
1546
1547         if (next)
1548                 linkea_del_buf(ldata, cname);
1549         else
1550                 lfsck_namespace_filter_linkea_entry(ldata, cname, pfid,
1551                                                     true);
1552         lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1553                        ldata->ld_leh->leh_len);
1554
1555 again:
1556         th = dt_trans_create(env, dev);
1557         if (IS_ERR(th))
1558                 GOTO(unlock1, rc = PTR_ERR(th));
1559
1560         rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1561                                   XATTR_NAME_LINK, 0, th);
1562         if (rc != 0)
1563                 GOTO(stop, rc);
1564
1565         rc = dt_trans_start_local(env, dev, th);
1566         if (rc != 0)
1567                 GOTO(stop, rc);
1568
1569         dt_write_lock(env, obj, 0);
1570         if (unlikely(lfsck_is_dead_obj(obj)))
1571                 GOTO(unlock2, rc = -ENOENT);
1572
1573         rc = lfsck_links_read2(env, obj, &ldata_new);
1574         if (rc != 0)
1575                 GOTO(unlock2, rc);
1576
1577         /* The specified linkEA entry has been removed by race. */
1578         rc = linkea_links_find(&ldata_new, cname, pfid);
1579         if (rc != 0)
1580                 GOTO(unlock2, rc = 0);
1581
1582         if (bk->lb_param & LPF_DRYRUN)
1583                 GOTO(unlock2, rc = 1);
1584
1585         if (next)
1586                 linkea_del_buf(&ldata_new, cname);
1587         else
1588                 lfsck_namespace_filter_linkea_entry(&ldata_new, cname, pfid,
1589                                                     true);
1590
1591         if (linkea_buf.lb_len < ldata_new.ld_leh->leh_len) {
1592                 dt_write_unlock(env, obj);
1593                 dt_trans_stop(env, dev, th);
1594                 lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1595                                ldata_new.ld_leh->leh_len);
1596                 goto again;
1597         }
1598
1599         lfsck_buf_init(&linkea_buf, ldata_new.ld_buf->lb_buf,
1600                        ldata_new.ld_leh->leh_len);
1601         rc = dt_xattr_set(env, obj, &linkea_buf,
1602                           XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1603
1604         GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
1605
1606 unlock2:
1607         dt_write_unlock(env, obj);
1608
1609 stop:
1610         dt_trans_stop(env, dev, th);
1611
1612 unlock1:
1613         lfsck_ibits_unlock(&lh, LCK_EX);
1614
1615 log:
1616         CDEBUG(D_LFSCK, "%s: namespace LFSCK remove %s linkEA entry "
1617                "for the object: "DFID", parent "DFID", name %.*s\n",
1618                lfsck_lfsck2name(lfsck), next ? "invalid" : "redundant",
1619                PFID(lfsck_dto2fid(obj)), PFID(pfid), cname->ln_namelen,
1620                cname->ln_name);
1621
1622         if (rc != 0) {
1623                 struct lfsck_namespace *ns = com->lc_file_ram;
1624
1625                 ns->ln_flags |= LF_INCONSISTENT;
1626         }
1627
1628         return rc;
1629 }
1630
1631 /**
1632  * Conditionally remove the specified entry from the linkEA.
1633  *
1634  * Take the parent lock firstly, then check whether the specified
1635  * name entry exists or not: if yes, do nothing; otherwise, call
1636  * lfsck_namespace_shrink_linkea() to remove the linkea entry.
1637  *
1638  * \param[in] env       pointer to the thread context
1639  * \param[in] com       pointer to the lfsck component
1640  * \param[in] parent    pointer to the parent directory
1641  * \param[in] child     pointer to the child object that holds the linkEA
1642  * \param[in,out]ldata  pointer to the buffer that holds the linkEA
1643  * \param[in] cname     the name for the child in the parent directory
1644  * \param[in] pfid      the parent directory's FID for the linkEA
1645  *
1646  * \retval              positive number for repaired cases
1647  * \retval              0 if nothing to be repaired
1648  * \retval              negative error number on failure
1649  */
1650 static int lfsck_namespace_shrink_linkea_cond(const struct lu_env *env,
1651                                               struct lfsck_component *com,
1652                                               struct dt_object *parent,
1653                                               struct dt_object *child,
1654                                               struct linkea_data *ldata,
1655                                               struct lu_name *cname,
1656                                               struct lu_fid *pfid)
1657 {
1658         struct lu_fid           *cfid   = &lfsck_env_info(env)->lti_fid3;
1659         struct lustre_handle     lh     = { 0 };
1660         int                      rc;
1661         ENTRY;
1662
1663         rc = lfsck_ibits_lock(env, com->lc_lfsck, parent, &lh,
1664                               MDS_INODELOCK_UPDATE, LCK_EX);
1665         if (rc != 0)
1666                 RETURN(rc);
1667
1668         dt_read_lock(env, parent, 0);
1669         if (unlikely(lfsck_is_dead_obj(parent))) {
1670                 dt_read_unlock(env, parent);
1671                 lfsck_ibits_unlock(&lh, LCK_EX);
1672                 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1673                                                    cname, pfid, true);
1674
1675                 RETURN(rc);
1676         }
1677
1678         rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
1679                        (const struct dt_key *)cname->ln_name,
1680                        BYPASS_CAPA);
1681         dt_read_unlock(env, parent);
1682
1683         /* It is safe to release the ldlm lock, because when the logic come
1684          * here, we have got all the needed information above whether the
1685          * linkEA entry is valid or not. It is not important that others
1686          * may add new linkEA entry after the ldlm lock released. If other
1687          * has removed the specified linkEA entry by race, then it is OK,
1688          * because the subsequent lfsck_namespace_shrink_linkea() can handle
1689          * such case. */
1690         lfsck_ibits_unlock(&lh, LCK_EX);
1691         if (rc == -ENOENT) {
1692                 rc = lfsck_namespace_shrink_linkea(env, com, child, ldata,
1693                                                    cname, pfid, true);
1694
1695                 RETURN(rc);
1696         }
1697
1698         if (rc != 0)
1699                 RETURN(rc);
1700
1701         /* The LFSCK just found some internal status of cross-MDTs
1702          * create operation. That is normal. */
1703         if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
1704                 linkea_next_entry(ldata);
1705
1706                 RETURN(0);
1707         }
1708
1709         rc = lfsck_namespace_shrink_linkea(env, com, child, ldata, cname,
1710                                            pfid, true);
1711
1712         RETURN(rc);
1713 }
1714
1715 /**
1716  * Conditionally replace name entry in the parent.
1717  *
1718  * As required, the LFSCK may re-create the lost MDT-object for dangling
1719  * name entry, but such repairing may be wrong because of bad FID in the
1720  * name entry. As the LFSCK processing, the real MDT-object may be found,
1721  * then the LFSCK should check whether the former re-created MDT-object
1722  * has been modified or not, if not, then destroy it and update the name
1723  * entry in the parent to reference the real MDT-object.
1724  *
1725  * \param[in] env       pointer to the thread context
1726  * \param[in] com       pointer to the lfsck component
1727  * \param[in] parent    pointer to the parent directory
1728  * \param[in] child     pointer to the MDT-object that may be the real
1729  *                      MDT-object corresponding to the name entry in parent
1730  * \param[in] cfid      the current FID in the name entry
1731  * \param[in] cname     contains the name of the child in the parent directory
1732  *
1733  * \retval              positive number for repaired cases
1734  * \retval              0 if nothing to be repaired
1735  * \retval              negative error number on failure
1736  */
1737 static int lfsck_namespace_replace_cond(const struct lu_env *env,
1738                                         struct lfsck_component *com,
1739                                         struct dt_object *parent,
1740                                         struct dt_object *child,
1741                                         const struct lu_fid *cfid,
1742                                         const struct lu_name *cname)
1743 {
1744         struct lfsck_thread_info        *info   = lfsck_env_info(env);
1745         struct lu_attr                  *la     = &info->lti_la;
1746         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
1747         struct lu_fid                    tfid;
1748         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1749         /* The child and its name may be on different MDTs. */
1750         struct dt_device                *dev    = lfsck->li_next;
1751         const char                      *name   = cname->ln_name;
1752         struct dt_object                *pobj   = NULL;
1753         struct dt_object                *cobj   = NULL;
1754         struct lustre_handle             plh    = { 0 };
1755         struct lustre_handle             clh    = { 0 };
1756         struct linkea_data               ldata  = { NULL };
1757         struct thandle                  *th     = NULL;
1758         bool                             exist  = true;
1759         int                              rc     = 0;
1760         ENTRY;
1761
1762         /* @parent/@child may be based on lfsck->li_bottom,
1763          * but here we need the object based on the lfsck->li_next. */
1764
1765         pobj = lfsck_object_locate(dev, parent);
1766         if (IS_ERR(pobj))
1767                 GOTO(log, rc = PTR_ERR(pobj));
1768
1769         if (unlikely(!dt_try_as_dir(env, pobj)))
1770                 GOTO(log, rc = -ENOTDIR);
1771
1772         /* Hold update lock on the pobj to prevent others to access. */
1773         rc = lfsck_ibits_lock(env, lfsck, pobj, &plh,
1774                               MDS_INODELOCK_UPDATE, LCK_EX);
1775         if (rc != 0)
1776                 GOTO(log, rc);
1777
1778         if (!fid_is_sane(cfid)) {
1779                 exist = false;
1780                 goto replace;
1781         }
1782
1783         cobj = lfsck_object_find_by_dev(env, dev, cfid);
1784         if (IS_ERR(cobj)) {
1785                 rc = PTR_ERR(cobj);
1786                 if (rc == -ENOENT) {
1787                         exist = false;
1788                         goto replace;
1789                 }
1790
1791                 GOTO(log, rc);
1792         }
1793
1794         if (!dt_object_exists(cobj)) {
1795                 exist = false;
1796                 goto replace;
1797         }
1798
1799         rc = dt_lookup(env, pobj, (struct dt_rec *)&tfid,
1800                        (const struct dt_key *)name, BYPASS_CAPA);
1801         if (rc == -ENOENT) {
1802                 exist = false;
1803                 goto replace;
1804         }
1805
1806         if (rc != 0)
1807                 GOTO(log, rc);
1808
1809         /* Someone changed the name entry, cannot replace it. */
1810         if (!lu_fid_eq(cfid, &tfid))
1811                 GOTO(log, rc = 0);
1812
1813         /* lock the object to be destroyed. */
1814         rc = lfsck_ibits_lock(env, lfsck, cobj, &clh,
1815                               MDS_INODELOCK_UPDATE |
1816                               MDS_INODELOCK_XATTR, LCK_EX);
1817         if (rc != 0)
1818                 GOTO(log, rc);
1819
1820         if (unlikely(lfsck_is_dead_obj(cobj))) {
1821                 exist = false;
1822                 goto replace;
1823         }
1824
1825         rc = dt_attr_get(env, cobj, la, BYPASS_CAPA);
1826         if (rc != 0)
1827                 GOTO(log, rc);
1828
1829         /* The object has been modified by other(s), or it is not created by
1830          * LFSCK, the two cases are indistinguishable. So cannot replace it. */
1831         if (la->la_ctime != 0)
1832                 GOTO(log, rc);
1833
1834         if (S_ISREG(la->la_mode)) {
1835                 rc = dt_xattr_get(env, cobj, &LU_BUF_NULL, XATTR_NAME_LOV,
1836                                   BYPASS_CAPA);
1837                 /* If someone has created related OST-object(s),
1838                  * then keep it. */
1839                 if ((rc > 0) || (rc < 0 && rc != -ENODATA))
1840                         GOTO(log, rc = (rc > 0 ? 0 : rc));
1841         }
1842
1843 replace:
1844         dt_read_lock(env, child, 0);
1845         rc = lfsck_links_read2(env, child, &ldata);
1846         dt_read_unlock(env, child);
1847
1848         /* Someone changed the child, no need to replace. */
1849         if (rc == -ENODATA)
1850                 GOTO(log, rc = 0);
1851
1852         if (rc != 0)
1853                 GOTO(log, rc);
1854
1855         rc = linkea_links_find(&ldata, cname, lfsck_dto2fid(pobj));
1856         /* Someone moved the child, no need to replace. */
1857         if (rc != 0)
1858                 GOTO(log, rc = 0);
1859
1860         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1861                 GOTO(log, rc = 1);
1862
1863         th = dt_trans_create(env, dev);
1864         if (IS_ERR(th))
1865                 GOTO(log, rc = PTR_ERR(th));
1866
1867         if (exist) {
1868                 rc = dt_declare_destroy(env, cobj, th);
1869                 if (rc != 0)
1870                         GOTO(stop, rc);
1871         }
1872
1873         rc = dt_declare_delete(env, pobj, (const struct dt_key *)name, th);
1874         if (rc != 0)
1875                 GOTO(stop, rc);
1876
1877         rec->rec_type = S_IFDIR;
1878         rec->rec_fid = lfsck_dto2fid(child);
1879         rc = dt_declare_insert(env, pobj, (const struct dt_rec *)rec,
1880                                (const struct dt_key *)name, th);
1881         if (rc != 0)
1882                 GOTO(stop, rc);
1883
1884         rc = dt_trans_start_local(env, dev, th);
1885         if (rc != 0)
1886                 GOTO(stop, rc);
1887
1888         if (exist) {
1889                 rc = dt_destroy(env, cobj, th);
1890                 if (rc != 0)
1891                         GOTO(stop, rc);
1892         }
1893
1894         /* The old name entry maybe not exist. */
1895         rc = dt_delete(env, pobj, (const struct dt_key *)name, th, BYPASS_CAPA);
1896         if (rc != 0 && rc != -ENOENT)
1897                 GOTO(stop, rc);
1898
1899         rc = dt_insert(env, pobj, (const struct dt_rec *)rec,
1900                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
1901
1902         GOTO(stop, rc = (rc == 0 ? 1 : rc));
1903
1904 stop:
1905         dt_trans_stop(env, dev, th);
1906
1907 log:
1908         lfsck_ibits_unlock(&clh, LCK_EX);
1909         lfsck_ibits_unlock(&plh, LCK_EX);
1910
1911         if (cobj != NULL && !IS_ERR(cobj))
1912                 lfsck_object_put(env, cobj);
1913
1914         CDEBUG(D_LFSCK, "%s: namespace LFSCK conditionally destroy the "
1915                "object "DFID" because of conflict with the object "DFID
1916                " under the parent "DFID" with name %s: rc = %d\n",
1917                lfsck_lfsck2name(lfsck), PFID(cfid),
1918                PFID(lfsck_dto2fid(child)), PFID(lfsck_dto2fid(parent)),
1919                name, rc);
1920
1921         return rc;
1922 }
1923
1924 /**
1925  * Overwrite the linkEA for the object with the given ldata.
1926  *
1927  * The caller should take the ldlm lock before the calling.
1928  *
1929  * \param[in] env       pointer to the thread context
1930  * \param[in] com       pointer to the lfsck component
1931  * \param[in] obj       pointer to the dt_object to be handled
1932  * \param[in] ldata     pointer to the new linkEA data
1933  *
1934  * \retval              positive number for repaired cases
1935  * \retval              0 if nothing to be repaired
1936  * \retval              negative error number on failure
1937  */
1938 int lfsck_namespace_rebuild_linkea(const struct lu_env *env,
1939                                    struct lfsck_component *com,
1940                                    struct dt_object *obj,
1941                                    struct linkea_data *ldata)
1942 {
1943         struct lfsck_instance           *lfsck  = com->lc_lfsck;
1944         struct dt_device                *dev    = lfsck_obj2dev(obj);
1945         struct thandle                  *th     = NULL;
1946         struct lu_buf                    linkea_buf;
1947         int                              rc     = 0;
1948         ENTRY;
1949
1950         th = dt_trans_create(env, dev);
1951         if (IS_ERR(th))
1952                 GOTO(log, rc = PTR_ERR(th));
1953
1954         lfsck_buf_init(&linkea_buf, ldata->ld_buf->lb_buf,
1955                        ldata->ld_leh->leh_len);
1956         rc = dt_declare_xattr_set(env, obj, &linkea_buf,
1957                                   XATTR_NAME_LINK, 0, th);
1958         if (rc != 0)
1959                 GOTO(stop, rc);
1960
1961         rc = dt_trans_start_local(env, dev, th);
1962         if (rc != 0)
1963                 GOTO(stop, rc);
1964
1965         dt_write_lock(env, obj, 0);
1966         if (unlikely(lfsck_is_dead_obj(obj)))
1967                 GOTO(unlock, rc = 0);
1968
1969         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
1970                 GOTO(unlock, rc = 1);
1971
1972         rc = dt_xattr_set(env, obj, &linkea_buf,
1973                           XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
1974
1975         GOTO(unlock, rc = (rc == 0 ? 1 : rc));
1976
1977 unlock:
1978         dt_write_unlock(env, obj);
1979
1980 stop:
1981         dt_trans_stop(env, dev, th);
1982
1983 log:
1984         CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild linkEA for the "
1985                "object "DFID": rc = %d\n",
1986                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)), rc);
1987
1988         if (rc != 0) {
1989                 struct lfsck_namespace *ns = com->lc_file_ram;
1990
1991                 ns->ln_flags |= LF_INCONSISTENT;
1992         }
1993
1994         return rc;
1995 }
1996
1997 /**
1998  * Repair invalid name entry.
1999  *
2000  * If the name entry contains invalid information, such as bad file type
2001  * or (and) corrupted object FID, then either remove the name entry or
2002  * udpate the name entry with the given (right) information.
2003  *
2004  * \param[in] env       pointer to the thread context
2005  * \param[in] com       pointer to the lfsck component
2006  * \param[in] parent    pointer to the parent directory
2007  * \param[in] child     pointer to the object referenced by the name entry
2008  * \param[in] name      the old name of the child under the parent directory
2009  * \param[in] name2     the new name of the child under the parent directory
2010  * \param[in] type      the type claimed by the name entry
2011  * \param[in] update    update the name entry if true; otherwise, remove it
2012  * \param[in] dec       decrease the parent nlink count if true
2013  *
2014  * \retval              positive number for repaired successfully
2015  * \retval              0 if nothing to be repaired
2016  * \retval              negative error number on failure
2017  */
2018 int lfsck_namespace_repair_dirent(const struct lu_env *env,
2019                                   struct lfsck_component *com,
2020                                   struct dt_object *parent,
2021                                   struct dt_object *child,
2022                                   const char *name, const char *name2,
2023                                   __u16 type, bool update, bool dec)
2024 {
2025         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
2026         const struct lu_fid     *cfid   = lfsck_dto2fid(child);
2027         struct lu_fid            tfid;
2028         struct lfsck_instance   *lfsck  = com->lc_lfsck;
2029         struct dt_device        *dev    = lfsck_obj2dev(parent);
2030         struct thandle          *th     = NULL;
2031         struct lustre_handle     lh     = { 0 };
2032         int                      rc     = 0;
2033         ENTRY;
2034
2035         if (unlikely(!dt_try_as_dir(env, parent)))
2036                 GOTO(log, rc = -ENOTDIR);
2037
2038         rc = lfsck_ibits_lock(env, lfsck, parent, &lh,
2039                               MDS_INODELOCK_UPDATE, LCK_EX);
2040         if (rc != 0)
2041                 GOTO(log, rc);
2042
2043         th = dt_trans_create(env, dev);
2044         if (IS_ERR(th))
2045                 GOTO(unlock1, rc = PTR_ERR(th));
2046
2047         rc = dt_declare_delete(env, parent, (const struct dt_key *)name, th);
2048         if (rc != 0)
2049                 GOTO(stop, rc);
2050
2051         if (update) {
2052                 rec->rec_type = lfsck_object_type(child) & S_IFMT;
2053                 rec->rec_fid = cfid;
2054                 rc = dt_declare_insert(env, parent,
2055                                        (const struct dt_rec *)rec,
2056                                        (const struct dt_key *)name2, th);
2057                 if (rc != 0)
2058                         GOTO(stop, rc);
2059         }
2060
2061         if (dec) {
2062                 rc = dt_declare_ref_del(env, parent, th);
2063                 if (rc != 0)
2064                         GOTO(stop, rc);
2065         }
2066
2067         rc = dt_trans_start_local(env, dev, th);
2068         if (rc != 0)
2069                 GOTO(stop, rc);
2070
2071         dt_write_lock(env, parent, 0);
2072         rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2073                        (const struct dt_key *)name, BYPASS_CAPA);
2074         /* Someone has removed the bad name entry by race. */
2075         if (rc == -ENOENT)
2076                 GOTO(unlock2, rc = 0);
2077
2078         if (rc != 0)
2079                 GOTO(unlock2, rc);
2080
2081         /* Someone has removed the bad name entry and reused it for other
2082          * object by race. */
2083         if (!lu_fid_eq(&tfid, cfid))
2084                 GOTO(unlock2, rc = 0);
2085
2086         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2087                 GOTO(unlock2, rc = 1);
2088
2089         rc = dt_delete(env, parent, (const struct dt_key *)name, th,
2090                        BYPASS_CAPA);
2091         if (rc != 0)
2092                 GOTO(unlock2, rc);
2093
2094         if (update) {
2095                 rc = dt_insert(env, parent,
2096                                (const struct dt_rec *)rec,
2097                                (const struct dt_key *)name2, th,
2098                                BYPASS_CAPA, 1);
2099                 if (rc != 0)
2100                         GOTO(unlock2, rc);
2101         }
2102
2103         if (dec) {
2104                 rc = dt_ref_del(env, parent, th);
2105                 if (rc != 0)
2106                         GOTO(unlock2, rc);
2107         }
2108
2109         GOTO(unlock2, rc = (rc == 0 ? 1 : rc));
2110
2111 unlock2:
2112         dt_write_unlock(env, parent);
2113
2114 stop:
2115         dt_trans_stop(env, dev, th);
2116
2117         /* We are not sure whether the child will become orphan or not.
2118          * Record it in the LFSCK trace file for further checking in
2119          * the second-stage scanning. */
2120         if (!update && !dec && rc == 0)
2121                 lfsck_namespace_trace_update(env, com, cfid,
2122                                              LNTF_CHECK_LINKEA, true);
2123
2124 unlock1:
2125         lfsck_ibits_unlock(&lh, LCK_EX);
2126
2127 log:
2128         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found bad name "
2129                "entry for: parent "DFID", child "DFID", name %s, type "
2130                "in name entry %o, type claimed by child %o. repair it "
2131                "by %s with new name2 %s: rc = %d\n", lfsck_lfsck2name(lfsck),
2132                PFID(lfsck_dto2fid(parent)), PFID(lfsck_dto2fid(child)),
2133                name, type, update ? lfsck_object_type(child) : 0,
2134                update ? "updating" : "removing", name2, rc);
2135
2136         if (rc != 0) {
2137                 struct lfsck_namespace *ns = com->lc_file_ram;
2138
2139                 ns->ln_flags |= LF_INCONSISTENT;
2140         }
2141
2142         return rc;
2143 }
2144
2145 /**
2146  * Update the ".." name entry for the given object.
2147  *
2148  * The object's ".." is corrupted, this function will update the ".." name
2149  * entry with the given pfid, and the linkEA with the given ldata.
2150  *
2151  * The caller should take the ldlm lock before the calling.
2152  *
2153  * \param[in] env       pointer to the thread context
2154  * \param[in] com       pointer to the lfsck component
2155  * \param[in] obj       pointer to the dt_object to be handled
2156  * \param[in] pfid      the new fid for the object's ".." name entry
2157  * \param[in] cname     the name for the @obj in the parent directory
2158  *
2159  * \retval              positive number for repaired cases
2160  * \retval              0 if nothing to be repaired
2161  * \retval              negative error number on failure
2162  */
2163 static int lfsck_namespace_repair_unmatched_pairs(const struct lu_env *env,
2164                                                   struct lfsck_component *com,
2165                                                   struct dt_object *obj,
2166                                                   const struct lu_fid *pfid,
2167                                                   struct lu_name *cname)
2168 {
2169         struct lfsck_thread_info        *info   = lfsck_env_info(env);
2170         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
2171         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2172         struct dt_device                *dev    = lfsck_obj2dev(obj);
2173         struct thandle                  *th     = NULL;
2174         struct linkea_data               ldata  = { NULL };
2175         struct lu_buf                    linkea_buf;
2176         int                              rc     = 0;
2177         ENTRY;
2178
2179         LASSERT(!dt_object_remote(obj));
2180         LASSERT(S_ISDIR(lfsck_object_type(obj)));
2181
2182         rc = linkea_data_new(&ldata, &info->lti_big_buf);
2183         if (rc != 0)
2184                 GOTO(log, rc);
2185
2186         rc = linkea_add_buf(&ldata, cname, pfid);
2187         if (rc != 0)
2188                 GOTO(log, rc);
2189
2190         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
2191                        ldata.ld_leh->leh_len);
2192
2193         th = dt_trans_create(env, dev);
2194         if (IS_ERR(th))
2195                 GOTO(log, rc = PTR_ERR(th));
2196
2197         rc = dt_declare_delete(env, obj, (const struct dt_key *)dotdot, th);
2198         if (rc != 0)
2199                 GOTO(stop, rc);
2200
2201         rec->rec_type = S_IFDIR;
2202         rec->rec_fid = pfid;
2203         rc = dt_declare_insert(env, obj, (const struct dt_rec *)rec,
2204                                (const struct dt_key *)dotdot, th);
2205         if (rc != 0)
2206                 GOTO(stop, rc);
2207
2208         rc = dt_declare_xattr_set(env, obj, &linkea_buf,
2209                                   XATTR_NAME_LINK, 0, th);
2210         if (rc != 0)
2211                 GOTO(stop, rc);
2212
2213         rc = dt_trans_start_local(env, dev, th);
2214         if (rc != 0)
2215                 GOTO(stop, rc);
2216
2217         dt_write_lock(env, obj, 0);
2218         if (unlikely(lfsck_is_dead_obj(obj)))
2219                 GOTO(unlock, rc = 0);
2220
2221         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2222                 GOTO(unlock, rc = 1);
2223
2224         /* The old ".." name entry maybe not exist. */
2225         dt_delete(env, obj, (const struct dt_key *)dotdot, th,
2226                   BYPASS_CAPA);
2227
2228         rc = dt_insert(env, obj, (const struct dt_rec *)rec,
2229                        (const struct dt_key *)dotdot, th, BYPASS_CAPA, 1);
2230         if (rc != 0)
2231                 GOTO(unlock, rc);
2232
2233         rc = dt_xattr_set(env, obj, &linkea_buf,
2234                           XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
2235
2236         GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2237
2238 unlock:
2239         dt_write_unlock(env, obj);
2240
2241 stop:
2242         dt_trans_stop(env, dev, th);
2243
2244 log:
2245         CDEBUG(D_LFSCK, "%s: namespace LFSCK rebuild dotdot name entry for "
2246                "the object "DFID", new parent "DFID": rc = %d\n",
2247                lfsck_lfsck2name(lfsck), PFID(lfsck_dto2fid(obj)),
2248                PFID(pfid), rc);
2249
2250         if (rc != 0) {
2251                 struct lfsck_namespace *ns = com->lc_file_ram;
2252
2253                 ns->ln_flags |= LF_INCONSISTENT;
2254         }
2255
2256         return rc;
2257 }
2258
2259 /**
2260  * Handle orphan @obj during Double Scan Directory.
2261  *
2262  * Remove the @obj's current (invalid) linkEA entries, and insert
2263  * it in the directory .lustre/lost+found/MDTxxxx/ with the name:
2264  * ${FID}-${PFID}-D-${conflict_version}
2265  *
2266  * The caller should take the ldlm lock before the calling.
2267  *
2268  * \param[in] env       pointer to the thread context
2269  * \param[in] com       pointer to the lfsck component
2270  * \param[in] obj       pointer to the orphan object to be handled
2271  * \param[in] pfid      the new fid for the object's ".." name entry
2272  * \param[in,out] lh    ldlm lock handler for the given @obj
2273  * \param[out] type     to tell the caller what the inconsistency is
2274  *
2275  * \retval              positive number for repaired cases
2276  * \retval              0 if nothing to be repaired
2277  * \retval              negative error number on failure
2278  */
2279 static int
2280 lfsck_namespace_dsd_orphan(const struct lu_env *env,
2281                            struct lfsck_component *com,
2282                            struct dt_object *obj,
2283                            const struct lu_fid *pfid,
2284                            struct lustre_handle *lh,
2285                            enum lfsck_namespace_inconsistency_type *type)
2286 {
2287         struct lfsck_thread_info *info = lfsck_env_info(env);
2288         struct lfsck_namespace   *ns   = com->lc_file_ram;
2289         int                       rc;
2290         ENTRY;
2291
2292         /* Remove the unrecognized linkEA. */
2293         rc = lfsck_namespace_links_remove(env, com, obj);
2294         lfsck_ibits_unlock(lh, LCK_EX);
2295         if (rc < 0 && rc != -ENODATA)
2296                 RETURN(rc);
2297
2298         *type = LNIT_MUL_REF;
2299
2300         /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2301          * ever tried to verify some remote MDT-object that resides on this
2302          * MDT, but this MDT failed to respond such request. So means there
2303          * may be some remote name entry on other MDT that references this
2304          * object with another name, so we cannot know whether this linkEA
2305          * is valid or not. So keep it there and maybe resolved when next
2306          * LFSCK run. */
2307         if (ns->ln_flags & LF_INCOMPLETE)
2308                 RETURN(0);
2309
2310         /* The unique linkEA is invalid, even if the ".." name entry may be
2311          * valid, we still cannot know via which name entry this directory
2312          * will be referenced. Then handle it as pure orphan. */
2313         snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2314                  "-"DFID, PFID(pfid));
2315         rc = lfsck_namespace_insert_orphan(env, com, obj,
2316                                            info->lti_tmpbuf, "D", NULL);
2317
2318         RETURN(rc);
2319 }
2320
2321 /**
2322  * Double Scan Directory object for single linkEA entry case.
2323  *
2324  * The given @child has unique linkEA entry. If the linkEA entry is valid,
2325  * then check whether the name is in the namespace or not, if not, add the
2326  * missing name entry back to namespace. If the linkEA entry is invalid,
2327  * then remove it and insert the @child in the .lustre/lost+found/MDTxxxx/
2328  * as an orphan.
2329  *
2330  * \param[in] env       pointer to the thread context
2331  * \param[in] com       pointer to the lfsck component
2332  * \param[in] child     pointer to the directory to be double scanned
2333  * \param[in] pfid      the FID corresponding to the ".." entry
2334  * \param[in] ldata     pointer to the linkEA data for the given @child
2335  * \param[in,out] lh    ldlm lock handler for the given @child
2336  * \param[out] type     to tell the caller what the inconsistency is
2337  * \param[in] retry     if found inconsistency, but the caller does not hold
2338  *                      ldlm lock on the @child, then set @retry as true
2339  *
2340  * \retval              positive number for repaired cases
2341  * \retval              0 if nothing to be repaired
2342  * \retval              negative error number on failure
2343  */
2344 static int
2345 lfsck_namespace_dsd_single(const struct lu_env *env,
2346                            struct lfsck_component *com,
2347                            struct dt_object *child,
2348                            const struct lu_fid *pfid,
2349                            struct linkea_data *ldata,
2350                            struct lustre_handle *lh,
2351                            enum lfsck_namespace_inconsistency_type *type,
2352                            bool *retry)
2353 {
2354         struct lfsck_thread_info *info          = lfsck_env_info(env);
2355         struct lu_name           *cname         = &info->lti_name;
2356         const struct lu_fid      *cfid          = lfsck_dto2fid(child);
2357         struct lu_fid             tfid;
2358         struct lfsck_namespace   *ns            = com->lc_file_ram;
2359         struct lfsck_instance    *lfsck         = com->lc_lfsck;
2360         struct dt_object         *parent        = NULL;
2361         struct lmv_mds_md_v1     *lmv;
2362         int                       rc            = 0;
2363         ENTRY;
2364
2365         lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid, info->lti_key);
2366         /* The unique linkEA entry with bad parent will be handled as orphan. */
2367         if (!fid_is_sane(&tfid)) {
2368                 if (!lustre_handle_is_used(lh) && retry != NULL)
2369                         *retry = true;
2370                 else
2371                         rc = lfsck_namespace_dsd_orphan(env, com, child,
2372                                                         pfid, lh, type);
2373
2374                 GOTO(out, rc);
2375         }
2376
2377         parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2378         if (IS_ERR(parent))
2379                 GOTO(out, rc = PTR_ERR(parent));
2380
2381         /* We trust the unique linkEA entry in spite of whether it matches the
2382          * ".." name entry or not. Because even if the linkEA entry is wrong
2383          * and the ".." name entry is right, we still cannot know via which
2384          * name entry the child will be referenced, since all known entries
2385          * have been verified during the first-stage scanning. */
2386         if (!dt_object_exists(parent)) {
2387                 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2388                  * has ever tried to verify some remote MDT-object that resides
2389                  * on this MDT, but this MDT failed to respond such request. So
2390                  * means there may be some remote name entry on other MDT that
2391                  * references this object with another name, so we cannot know
2392                  * whether this linkEA is valid or not. So keep it there and
2393                  * maybe resolved when next LFSCK run. */
2394                 if (ns->ln_flags & LF_INCOMPLETE)
2395                         GOTO(out, rc = 0);
2396
2397                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2398                         *retry = true;
2399
2400                         GOTO(out, rc = 0);
2401                 }
2402
2403                 lfsck_ibits_unlock(lh, LCK_EX);
2404
2405 lost_parent:
2406                 lmv = &info->lti_lmv;
2407                 rc = lfsck_read_stripe_lmv(env, child, lmv);
2408                 if (rc != 0 && rc != -ENODATA)
2409                         GOTO(out, rc);
2410
2411                 if (rc == -ENODATA || lmv->lmv_magic != LMV_MAGIC_STRIPE) {
2412                         lmv = NULL;
2413                 } else if (lfsck_shard_name_to_index(env,
2414                                         cname->ln_name, cname->ln_namelen,
2415                                         S_IFDIR, cfid) < 0) {
2416                         /* It is an invalid name entry, we
2417                          * cannot trust the parent also. */
2418                         rc = lfsck_namespace_shrink_linkea(env, com, child,
2419                                                 ldata, cname, &tfid, true);
2420                         if (rc < 0)
2421                                 GOTO(out, rc);
2422
2423                         snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
2424                                  "-"DFID, PFID(pfid));
2425                         rc = lfsck_namespace_insert_orphan(env, com, child,
2426                                                 info->lti_tmpbuf, "S", NULL);
2427
2428                         GOTO(out, rc);
2429                 }
2430
2431                 /* Create the lost parent as an orphan. */
2432                 rc = lfsck_namespace_create_orphan_dir(env, com, parent, lmv);
2433                 if (rc >= 0) {
2434                         /* Add the missing name entry to the parent. */
2435                         rc = lfsck_namespace_insert_normal(env, com, parent,
2436                                                         child, cname->ln_name);
2437                         if (unlikely(rc == -EEXIST)) {
2438                                 /* Unfortunately, someone reused the name
2439                                  * under the parent by race. So we have
2440                                  * to remove the linkEA entry from
2441                                  * current child object. It means that the
2442                                  * LFSCK cannot recover the system
2443                                  * totally back to its original status,
2444                                  * but it is necessary to make the
2445                                  * current system to be consistent. */
2446                                 rc = lfsck_namespace_shrink_linkea(env,
2447                                                 com, child, ldata,
2448                                                 cname, &tfid, true);
2449                                 if (rc >= 0) {
2450                                         snprintf(info->lti_tmpbuf,
2451                                                  sizeof(info->lti_tmpbuf),
2452                                                  "-"DFID, PFID(pfid));
2453                                         rc = lfsck_namespace_insert_orphan(env,
2454                                                 com, child, info->lti_tmpbuf,
2455                                                 "D", NULL);
2456                                 }
2457                         }
2458                 }
2459
2460                 GOTO(out, rc);
2461         }
2462
2463         /* The unique linkEA entry with bad parent will be handled as orphan. */
2464         if (unlikely(!dt_try_as_dir(env, parent))) {
2465                 if (!lustre_handle_is_used(lh) && retry != NULL)
2466                         *retry = true;
2467                 else
2468                         rc = lfsck_namespace_dsd_orphan(env, com, child,
2469                                                         pfid, lh, type);
2470
2471                 GOTO(out, rc);
2472         }
2473
2474         rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2475                        (const struct dt_key *)cname->ln_name, BYPASS_CAPA);
2476         if (rc == -ENOENT) {
2477                 /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT
2478                  * has ever tried to verify some remote MDT-object that resides
2479                  * on this MDT, but this MDT failed to respond such request. So
2480                  * means there may be some remote name entry on other MDT that
2481                  * references this object with another name, so we cannot know
2482                  * whether this linkEA is valid or not. So keep it there and
2483                  * maybe resolved when next LFSCK run. */
2484                 if (ns->ln_flags & LF_INCOMPLETE)
2485                         GOTO(out, rc = 0);
2486
2487                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2488                         *retry = true;
2489
2490                         GOTO(out, rc = 0);
2491                 }
2492
2493                 lfsck_ibits_unlock(lh, LCK_EX);
2494                 rc = lfsck_namespace_check_name(env, parent, child, cname);
2495                 if (rc == -ENOENT)
2496                         goto lost_parent;
2497
2498                 if (rc < 0)
2499                         GOTO(out, rc);
2500
2501                 /* It is an invalid name entry, drop it. */
2502                 if (unlikely(rc > 0)) {
2503                         rc = lfsck_namespace_shrink_linkea(env, com, child,
2504                                                 ldata, cname, &tfid, true);
2505                         if (rc >= 0) {
2506                                 snprintf(info->lti_tmpbuf,
2507                                          sizeof(info->lti_tmpbuf),
2508                                          "-"DFID, PFID(pfid));
2509                                 rc = lfsck_namespace_insert_orphan(env, com,
2510                                         child, info->lti_tmpbuf, "D", NULL);
2511                         }
2512
2513                         GOTO(out, rc);
2514                 }
2515
2516                 /* Add the missing name entry back to the namespace. */
2517                 rc = lfsck_namespace_insert_normal(env, com, parent, child,
2518                                                    cname->ln_name);
2519                 if (unlikely(rc == -ESTALE))
2520                         /* It may happen when the remote object has been
2521                          * removed, but the local MDT is not aware of that. */
2522                         goto lost_parent;
2523
2524                 if (unlikely(rc == -EEXIST)) {
2525                         /* Unfortunately, someone reused the name under the
2526                          * parent by race. So we have to remove the linkEA
2527                          * entry from current child object. It means that the
2528                          * LFSCK cannot recover the system totally back to
2529                          * its original status, but it is necessary to make
2530                          * the current system to be consistent.
2531                          *
2532                          * It also may be because of the LFSCK found some
2533                          * internal status of create operation. Under such
2534                          * case, nothing to be done. */
2535                         rc = lfsck_namespace_shrink_linkea_cond(env, com,
2536                                         parent, child, ldata, cname, &tfid);
2537                         if (rc >= 0) {
2538                                 snprintf(info->lti_tmpbuf,
2539                                          sizeof(info->lti_tmpbuf),
2540                                          "-"DFID, PFID(pfid));
2541                                 rc = lfsck_namespace_insert_orphan(env, com,
2542                                         child, info->lti_tmpbuf, "D", NULL);
2543                         }
2544                 }
2545
2546                 GOTO(out, rc);
2547         }
2548
2549         if (rc != 0)
2550                 GOTO(out, rc);
2551
2552         if (!lu_fid_eq(&tfid, cfid)) {
2553                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2554                         *retry = true;
2555
2556                         GOTO(out, rc = 0);
2557                 }
2558
2559                 lfsck_ibits_unlock(lh, LCK_EX);
2560                 /* The name entry references another MDT-object that
2561                  * may be created by the LFSCK for repairing dangling
2562                  * name entry. Try to replace it. */
2563                 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2564                                                   &tfid, cname);
2565                 if (rc == 0)
2566                         rc = lfsck_namespace_dsd_orphan(env, com, child,
2567                                                         pfid, lh, type);
2568
2569                 GOTO(out, rc);
2570         }
2571
2572         if (fid_is_zero(pfid))
2573                 GOTO(out, rc = 0);
2574
2575         /* The ".." name entry is wrong, update it. */
2576         if (!lu_fid_eq(pfid, lfsck_dto2fid(parent))) {
2577                 if (!lustre_handle_is_used(lh) && retry != NULL) {
2578                         *retry = true;
2579
2580                         GOTO(out, rc = 0);
2581                 }
2582
2583                 *type = LNIT_UNMATCHED_PAIRS;
2584                 rc = lfsck_namespace_repair_unmatched_pairs(env, com, child,
2585                                                 lfsck_dto2fid(parent), cname);
2586         }
2587
2588         GOTO(out, rc);
2589
2590 out:
2591         if (parent != NULL && !IS_ERR(parent))
2592                 lfsck_object_put(env, parent);
2593
2594         return rc;
2595 }
2596
2597 /**
2598  * Double Scan Directory object for multiple linkEA entries case.
2599  *
2600  * The given @child has multiple linkEA entries. There is at most one linkEA
2601  * entry will be valid, all the others will be removed. Firstly, the function
2602  * will try to find out the linkEA entry for which the name entry exists under
2603  * the given parent (@pfid). If there is no linkEA entry that matches the given
2604  * ".." name entry, then tries to find out the first linkEA entry that both the
2605  * parent and the name entry exist to rebuild a new ".." name entry.
2606  *
2607  * \param[in] env       pointer to the thread context
2608  * \param[in] com       pointer to the lfsck component
2609  * \param[in] child     pointer to the directory to be double scanned
2610  * \param[in] pfid      the FID corresponding to the ".." entry
2611  * \param[in] ldata     pointer to the linkEA data for the given @child
2612  * \param[in,out] lh    ldlm lock handler for the given @child
2613  * \param[out] type     to tell the caller what the inconsistency is
2614  * \param[in] lpf       true if the ".." entry is under lost+found/MDTxxxx/
2615  *
2616  * \retval              positive number for repaired cases
2617  * \retval              0 if nothing to be repaired
2618  * \retval              negative error number on failure
2619  */
2620 static int
2621 lfsck_namespace_dsd_multiple(const struct lu_env *env,
2622                              struct lfsck_component *com,
2623                              struct dt_object *child,
2624                              const struct lu_fid *pfid,
2625                              struct linkea_data *ldata,
2626                              struct lustre_handle *lh,
2627                              enum lfsck_namespace_inconsistency_type *type,
2628                              bool lpf)
2629 {
2630         struct lfsck_thread_info *info          = lfsck_env_info(env);
2631         struct lu_name           *cname         = &info->lti_name;
2632         const struct lu_fid      *cfid          = lfsck_dto2fid(child);
2633         struct lu_fid            *pfid2         = &info->lti_fid3;
2634         struct lu_fid             tfid;
2635         struct lfsck_namespace   *ns            = com->lc_file_ram;
2636         struct lfsck_instance    *lfsck         = com->lc_lfsck;
2637         struct lfsck_bookmark    *bk            = &lfsck->li_bookmark_ram;
2638         struct dt_object         *parent        = NULL;
2639         struct linkea_data        ldata_new     = { NULL };
2640         int                       dirent_count  = 0;
2641         int                       linkea_count  = 0;
2642         int                       rc            = 0;
2643         bool                      once          = true;
2644         ENTRY;
2645
2646 again:
2647         while (ldata->ld_lee != NULL) {
2648                 lfsck_namespace_unpack_linkea_entry(ldata, cname, &tfid,
2649                                                     info->lti_key);
2650                 /* Drop repeated linkEA entries. */
2651                 lfsck_namespace_filter_linkea_entry(ldata, cname, &tfid, true);
2652                 /* Drop invalid linkEA entry. */
2653                 if (!fid_is_sane(&tfid)) {
2654                         linkea_del_buf(ldata, cname);
2655                         linkea_count++;
2656                         continue;
2657                 }
2658
2659                 /* If current dotdot is the .lustre/lost+found/MDTxxxx/,
2660                  * then it is possible that: the directry object has ever
2661                  * been lost, but its name entry was there. In the former
2662                  * LFSCK run, during the first-stage scanning, the LFSCK
2663                  * found the dangling name entry, but it did not recreate
2664                  * the lost object, and when moved to the second-stage
2665                  * scanning, some children objects of the lost directory
2666                  * object were found, then the LFSCK recreated such lost
2667                  * directory object as an orphan.
2668                  *
2669                  * When the LFSCK runs again, if the dangling name is still
2670                  * there, the LFSCK should move the orphan directory object
2671                  * back to the normal namespace. */
2672                 if (!lpf && !lu_fid_eq(pfid, &tfid) && once) {
2673                         linkea_next_entry(ldata);
2674                         continue;
2675                 }
2676
2677                 parent = lfsck_object_find_bottom(env, lfsck, &tfid);
2678                 if (IS_ERR(parent))
2679                         RETURN(PTR_ERR(parent));
2680
2681                 if (!dt_object_exists(parent)) {
2682                         lfsck_object_put(env, parent);
2683                         if (ldata->ld_leh->leh_reccount > 1) {
2684                                 /* If it is NOT the last linkEA entry, then
2685                                  * there is still other chance to make the
2686                                  * child to be visible via other parent, then
2687                                  * remove this linkEA entry. */
2688                                 linkea_del_buf(ldata, cname);
2689                                 linkea_count++;
2690                                 continue;
2691                         }
2692
2693                         break;
2694                 }
2695
2696                 /* The linkEA entry with bad parent will be removed. */
2697                 if (unlikely(!dt_try_as_dir(env, parent))) {
2698                         lfsck_object_put(env, parent);
2699                         linkea_del_buf(ldata, cname);
2700                         linkea_count++;
2701                         continue;
2702                 }
2703
2704                 rc = dt_lookup(env, parent, (struct dt_rec *)&tfid,
2705                                (const struct dt_key *)cname->ln_name,
2706                                BYPASS_CAPA);
2707                 *pfid2 = *lfsck_dto2fid(parent);
2708                 if (rc == -ENOENT) {
2709                         lfsck_object_put(env, parent);
2710                         linkea_next_entry(ldata);
2711                         continue;
2712                 }
2713
2714                 if (rc != 0) {
2715                         lfsck_object_put(env, parent);
2716
2717                         RETURN(rc);
2718                 }
2719
2720                 if (lu_fid_eq(&tfid, cfid)) {
2721                         lfsck_object_put(env, parent);
2722                         if (!lu_fid_eq(pfid, pfid2)) {
2723                                 *type = LNIT_UNMATCHED_PAIRS;
2724                                 rc = lfsck_namespace_repair_unmatched_pairs(env,
2725                                                 com, child, pfid2, cname);
2726
2727                                 RETURN(rc);
2728                         }
2729
2730 rebuild:
2731                         /* It is the most common case that we find the
2732                          * name entry corresponding to the linkEA entry
2733                          * that matches the ".." name entry. */
2734                         rc = linkea_data_new(&ldata_new, &info->lti_big_buf);
2735                         if (rc != 0)
2736                                 RETURN(rc);
2737
2738                         rc = linkea_add_buf(&ldata_new, cname, pfid2);
2739                         if (rc != 0)
2740                                 RETURN(rc);
2741
2742                         rc = lfsck_namespace_rebuild_linkea(env, com, child,
2743                                                             &ldata_new);
2744                         if (rc < 0)
2745                                 RETURN(rc);
2746
2747                         linkea_del_buf(ldata, cname);
2748                         linkea_count++;
2749                         linkea_first_entry(ldata);
2750                         /* There may be some invalid dangling name entries under
2751                          * other parent directories, remove all of them. */
2752                         while (ldata->ld_lee != NULL) {
2753                                 lfsck_namespace_unpack_linkea_entry(ldata,
2754                                                 cname, &tfid, info->lti_key);
2755                                 if (!fid_is_sane(&tfid))
2756                                         goto next;
2757
2758                                 parent = lfsck_object_find_bottom(env, lfsck,
2759                                                                   &tfid);
2760                                 if (IS_ERR(parent)) {
2761                                         rc = PTR_ERR(parent);
2762                                         if (rc != -ENOENT &&
2763                                             bk->lb_param & LPF_FAILOUT)
2764                                                 RETURN(rc);
2765
2766                                         goto next;
2767                                 }
2768
2769                                 if (!dt_object_exists(parent)) {
2770                                         lfsck_object_put(env, parent);
2771                                         goto next;
2772                                 }
2773
2774                                 rc = lfsck_namespace_repair_dirent(env, com,
2775                                         parent, child, cname->ln_name,
2776                                         cname->ln_name, S_IFDIR, false, true);
2777                                 lfsck_object_put(env, parent);
2778                                 if (rc < 0) {
2779                                         if (bk->lb_param & LPF_FAILOUT)
2780                                                 RETURN(rc);
2781
2782                                         goto next;
2783                                 }
2784
2785                                 dirent_count += rc;
2786
2787 next:
2788                                 linkea_del_buf(ldata, cname);
2789                         }
2790
2791                         ns->ln_dirent_repaired += dirent_count;
2792
2793                         RETURN(rc);
2794                 }
2795
2796                 lfsck_ibits_unlock(lh, LCK_EX);
2797                 /* The name entry references another MDT-object that may be
2798                  * created by the LFSCK for repairing dangling name entry.
2799                  * Try to replace it. */
2800                 rc = lfsck_namespace_replace_cond(env, com, parent, child,
2801                                                   &tfid, cname);
2802                 lfsck_object_put(env, parent);
2803                 if (rc < 0)
2804                         RETURN(rc);
2805
2806                 if (rc > 0)
2807                         goto rebuild;
2808
2809                 linkea_del_buf(ldata, cname);
2810         }
2811
2812         linkea_first_entry(ldata);
2813         if (ldata->ld_leh->leh_reccount == 1) {
2814                 rc = lfsck_namespace_dsd_single(env, com, child, pfid, ldata,
2815                                                 lh, type, NULL);
2816
2817                 if (rc == 0 && fid_is_zero(pfid) && linkea_count > 0)
2818                         rc = lfsck_namespace_rebuild_linkea(env, com, child,
2819                                                             ldata);
2820
2821                 RETURN(rc);
2822         }
2823
2824         /* All linkEA entries are invalid and removed, then handle the @child
2825          * as an orphan.*/
2826         if (ldata->ld_leh->leh_reccount == 0) {
2827                 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, lh,
2828                                                 type);
2829
2830                 RETURN(rc);
2831         }
2832
2833         /* If the dangling name entry for the orphan directory object has
2834          * been remvoed, then just check whether the directory object is
2835          * still under the .lustre/lost+found/MDTxxxx/ or not. */
2836         if (lpf) {
2837                 lpf = false;
2838                 goto again;
2839         }
2840
2841         /* There is no linkEA entry that matches the ".." name entry. Find
2842          * the first linkEA entry that both parent and name entry exist to
2843          * rebuild a new ".." name entry. */
2844         if (once) {
2845                 once = false;
2846                 goto again;
2847         }
2848
2849         RETURN(rc);
2850 }
2851
2852 /**
2853  * Repair the object's nlink attribute.
2854  *
2855  * If all the known name entries have been verified, then the object's hard
2856  * link attribute should match the object's linkEA entries count unless the
2857  * object's has too much hard link to be recorded in the linkEA. Such cases
2858  * should have been marked in the LFSCK trace file. Otherwise, trust the
2859  * linkEA to update the object's nlink attribute.
2860  *
2861  * \param[in] env       pointer to the thread context
2862  * \param[in] com       pointer to the lfsck component
2863  * \param[in] obj       pointer to the dt_object to be handled
2864  * \param[in,out] la    pointer to buffer to object's attribute before
2865  *                      and after the repairing
2866  *
2867  * \retval              positive number for repaired cases
2868  * \retval              0 if nothing to be repaired
2869  * \retval              negative error number on failure
2870  */
2871 static int lfsck_namespace_repair_nlink(const struct lu_env *env,
2872                                         struct lfsck_component *com,
2873                                         struct dt_object *obj,
2874                                         struct lu_attr *la)
2875 {
2876         struct lfsck_thread_info        *info   = lfsck_env_info(env);
2877         struct lu_fid                   *tfid   = &info->lti_fid3;
2878         struct lfsck_namespace          *ns     = com->lc_file_ram;
2879         struct lfsck_instance           *lfsck  = com->lc_lfsck;
2880         struct dt_device                *dev    = lfsck_obj2dev(obj);
2881         const struct lu_fid             *cfid   = lfsck_dto2fid(obj);
2882         struct thandle                  *th     = NULL;
2883         struct linkea_data               ldata  = { NULL };
2884         struct lustre_handle             lh     = { 0 };
2885         __u32                            old    = la->la_nlink;
2886         int                              idx;
2887         int                              rc     = 0;
2888         __u8                             flags;
2889         ENTRY;
2890
2891         LASSERT(!dt_object_remote(obj));
2892         LASSERT(S_ISREG(lfsck_object_type(obj)));
2893
2894         rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
2895                               MDS_INODELOCK_UPDATE |
2896                               MDS_INODELOCK_XATTR, LCK_EX);
2897         if (rc != 0)
2898                 GOTO(log, rc);
2899
2900         th = dt_trans_create(env, dev);
2901         if (IS_ERR(th))
2902                 GOTO(log, rc = PTR_ERR(th));
2903
2904         la->la_valid = LA_NLINK;
2905         rc = dt_declare_attr_set(env, obj, la, th);
2906         if (rc != 0)
2907                 GOTO(stop, rc);
2908
2909         rc = dt_trans_start_local(env, dev, th);
2910         if (rc != 0)
2911                 GOTO(stop, rc);
2912
2913         dt_write_lock(env, obj, 0);
2914         /* If the LFSCK is marked as LF_INCOMPLETE, then means some MDT has
2915          * ever tried to verify some remote MDT-object that resides on this
2916          * MDT, but this MDT failed to respond such request. So means there
2917          * may be some remote name entry on other MDT that references this
2918          * object with another name, so we cannot know whether this linkEA
2919          * is valid or not. So keep it there and maybe resolved when next
2920          * LFSCK run. */
2921         if (ns->ln_flags & LF_INCOMPLETE)
2922                 GOTO(unlock, rc = 0);
2923
2924         fid_cpu_to_be(tfid, cfid);
2925         idx = lfsck_sub_trace_file_fid2idx(cfid);
2926         rc = dt_lookup(env, com->lc_sub_trace_objs[idx].lsto_obj,
2927                        (struct dt_rec *)&flags, (const struct dt_key *)tfid,
2928                        BYPASS_CAPA);
2929         if (rc != 0)
2930                 GOTO(unlock, rc);
2931
2932         if (flags & LNTF_SKIP_NLINK)
2933                 GOTO(unlock, rc = 0);
2934
2935         rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
2936         if (rc != 0)
2937                 GOTO(unlock, rc = (rc == -ENOENT ? 0 : rc));
2938
2939         rc = lfsck_links_read2(env, obj, &ldata);
2940         if (rc != 0)
2941                 GOTO(unlock, rc = (rc == -ENODATA ? 0 : rc));
2942
2943         if (la->la_nlink == ldata.ld_leh->leh_reccount ||
2944             unlikely(la->la_nlink == 0))
2945                 GOTO(unlock, rc = 0);
2946
2947         la->la_nlink = ldata.ld_leh->leh_reccount;
2948         if (lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN)
2949                 GOTO(unlock, rc = 1);
2950
2951         rc = dt_attr_set(env, obj, la, th, BYPASS_CAPA);
2952
2953         GOTO(unlock, rc = (rc == 0 ? 1 : rc));
2954
2955 unlock:
2956         dt_write_unlock(env, obj);
2957
2958 stop:
2959         dt_trans_stop(env, dev, th);
2960
2961 log:
2962         lfsck_ibits_unlock(&lh, LCK_EX);
2963         CDEBUG(D_LFSCK, "%s: namespace LFSCK repaired the object "DFID"'s "
2964                "nlink count from %u to %u: rc = %d\n",
2965                lfsck_lfsck2name(lfsck), PFID(cfid), old, la->la_nlink, rc);
2966
2967         if (rc != 0)
2968                 ns->ln_flags |= LF_INCONSISTENT;
2969
2970         return rc;
2971 }
2972
2973 /**
2974  * Double scan the directory object for namespace LFSCK.
2975  *
2976  * This function will verify the <parent, child> pairs in the namespace tree:
2977  * the parent references the child via some name entry that should be in the
2978  * child's linkEA entry, the child should back references the parent via its
2979  * ".." name entry.
2980  *
2981  * The LFSCK will scan every linkEA entry in turn until find out the first
2982  * matched pairs. If found, then all other linkEA entries will be dropped.
2983  * If all the linkEA entries cannot match the ".." name entry, then there
2984  * are serveral possible cases:
2985  *
2986  * 1) If there is only one linkEA entry, then trust it as long as the PFID
2987  *    in the linkEA entry is valid.
2988  *
2989  * 2) If there are multiple linkEA entries, then try to find the linkEA
2990  *    that matches the ".." name entry. If found, then all other entries
2991  *    are invalid; otherwise, it is quite possible that the ".." name entry
2992  *    is corrupted. Under such case, the LFSCK will rebuild the ".." name
2993  *    entry according to the first valid linkEA entry (both the parent and
2994  *    the name entry should exist).
2995  *
2996  * 3) If the directory object has no (valid) linkEA entry, then the
2997  *    directory object will be handled as pure orphan and inserted
2998  *    in the .lustre/lost+found/MDTxxxx/ with the name:
2999  *    ${self_FID}-${PFID}-D-${conflict_version}
3000  *
3001  * \param[in] env       pointer to the thread context
3002  * \param[in] com       pointer to the lfsck component
3003  * \param[in] child     pointer to the directory object to be handled
3004  * \param[in] flags     to indicate the specical checking on the @child
3005  *
3006  * \retval              positive number for repaired cases
3007  * \retval              0 if nothing to be repaired
3008  * \retval              negative error number on failure
3009  */
3010 static int lfsck_namespace_double_scan_dir(const struct lu_env *env,
3011                                            struct lfsck_component *com,
3012                                            struct dt_object *child, __u8 flags)
3013 {
3014         struct lfsck_thread_info *info          = lfsck_env_info(env);
3015         const struct lu_fid      *cfid          = lfsck_dto2fid(child);
3016         struct lu_fid            *pfid          = &info->lti_fid2;
3017         struct lfsck_namespace   *ns            = com->lc_file_ram;
3018         struct lfsck_instance    *lfsck         = com->lc_lfsck;
3019         struct lustre_handle      lh            = { 0 };
3020         struct linkea_data        ldata         = { NULL };
3021         bool                      unknown       = false;
3022         bool                      lpf           = false;
3023         bool                      retry         = false;
3024         enum lfsck_namespace_inconsistency_type type = LNIT_BAD_LINKEA;
3025         int                       rc            = 0;
3026         ENTRY;
3027
3028         LASSERT(!dt_object_remote(child));
3029
3030         if (flags & LNTF_UNCERTAIN_LMV) {
3031                 if (flags & LNTF_RECHECK_NAME_HASH) {
3032                         rc = lfsck_namespace_scan_shard(env, com, child);
3033                         if (rc < 0)
3034                                 RETURN(rc);
3035
3036                         ns->ln_striped_shards_scanned++;
3037                 } else {
3038                         ns->ln_striped_shards_skipped++;
3039                 }
3040         }
3041
3042         flags &= ~(LNTF_RECHECK_NAME_HASH | LNTF_UNCERTAIN_LMV);
3043         if (flags == 0)
3044                 RETURN(0);
3045
3046         if (flags & (LNTF_CHECK_LINKEA | LNTF_CHECK_PARENT) &&
3047             !(lfsck->li_bookmark_ram.lb_param & LPF_ALL_TGT)) {
3048                 CDEBUG(D_LFSCK, "%s: some MDT(s) maybe NOT take part in the"
3049                        "the namespace LFSCK, then the LFSCK cannot guarantee"
3050                        "all the name entries have been verified in first-stage"
3051                        "scanning. So have to skip orphan related handling for"
3052                        "the directory object "DFID" with remote name entry\n",
3053                        lfsck_lfsck2name(lfsck), PFID(cfid));
3054
3055                 RETURN(0);
3056         }
3057
3058         if (unlikely(!dt_try_as_dir(env, child)))
3059                 GOTO(out, rc = -ENOTDIR);
3060
3061         /* We only take ldlm lock on the @child when required. When the
3062          * logic comes here for the first time, it is always false. */
3063         if (0) {
3064
3065 lock:
3066                 rc = lfsck_ibits_lock(env, lfsck, child, &lh,
3067                                       MDS_INODELOCK_UPDATE |
3068                                       MDS_INODELOCK_XATTR, LCK_EX);
3069                 if (rc != 0)
3070                         GOTO(out, rc);
3071         }
3072
3073         dt_read_lock(env, child, 0);
3074         if (unlikely(lfsck_is_dead_obj(child))) {
3075                 dt_read_unlock(env, child);
3076
3077                 GOTO(out, rc = 0);
3078         }
3079
3080         rc = dt_lookup(env, child, (struct dt_rec *)pfid,
3081                        (const struct dt_key *)dotdot, BYPASS_CAPA);
3082         if (rc != 0) {
3083                 if (rc != -ENOENT && rc != -ENODATA && rc != -EINVAL) {
3084                         dt_read_unlock(env, child);
3085
3086                         GOTO(out, rc);
3087                 }
3088
3089                 if (!lustre_handle_is_used(&lh)) {
3090                         dt_read_unlock(env, child);
3091                         goto lock;
3092                 }
3093
3094                 fid_zero(pfid);
3095         } else if (lfsck->li_lpf_obj != NULL &&
3096                    lu_fid_eq(pfid, lfsck_dto2fid(lfsck->li_lpf_obj))) {
3097                 lpf = true;
3098         } else if (unlikely(!fid_is_sane(pfid))) {
3099                 fid_zero(pfid);
3100         }
3101
3102         rc = lfsck_links_read(env, child, &ldata);
3103         dt_read_unlock(env, child);
3104         if (rc != 0) {
3105                 if (rc != -ENODATA && rc != -EINVAL)
3106                         GOTO(out, rc);
3107
3108                 if (!lustre_handle_is_used(&lh))
3109                         goto lock;
3110
3111                 if (rc == -EINVAL && !fid_is_zero(pfid)) {
3112                         /* Remove the corrupted linkEA. */
3113                         rc = lfsck_namespace_links_remove(env, com, child);
3114                         if (rc == 0)
3115                                 /* Here, because of the crashed linkEA, we
3116                                  * cannot know whether there is some parent
3117                                  * that references the child directory via
3118                                  * some name entry or not. So keep it there,
3119                                  * when the LFSCK run next time, if there is
3120                                  * some parent that references this object,
3121                                  * then the LFSCK can rebuild the linkEA;
3122                                  * otherwise, this object will be handled
3123                                  * as orphan as above. */
3124                                 unknown = true;
3125                 } else {
3126                         /* 1. If we have neither ".." nor linkEA,
3127                          *    then it is an orphan.
3128                          *
3129                          * 2. If we only have the ".." name entry,
3130                          *    but no parent references this child
3131                          *    directory, then handle it as orphan. */
3132                         lfsck_ibits_unlock(&lh, LCK_EX);
3133                         type = LNIT_MUL_REF;
3134
3135                         /* If the LFSCK is marked as LF_INCOMPLETE,
3136                          * then means some MDT has ever tried to
3137                          * verify some remote MDT-object that resides
3138                          * on this MDT, but this MDT failed to respond
3139                          * such request. So means there may be some
3140                          * remote name entry on other MDT that
3141                          * references this object with another name,
3142                          * so we cannot know whether this linkEA is
3143                          * valid or not. So keep it there and maybe
3144                          * resolved when next LFSCK run. */
3145                         if (ns->ln_flags & LF_INCOMPLETE)
3146                                 GOTO(out, rc = 0);
3147
3148                         snprintf(info->lti_tmpbuf, sizeof(info->lti_tmpbuf),
3149                                  "-"DFID, PFID(pfid));
3150                         rc = lfsck_namespace_insert_orphan(env, com, child,
3151                                                 info->lti_tmpbuf, "D", NULL);
3152                 }
3153
3154                 GOTO(out, rc);
3155         }
3156
3157         linkea_first_entry(&ldata);
3158         /* This is the most common case: the object has unique linkEA entry. */
3159         if (ldata.ld_leh->leh_reccount == 1) {
3160                 rc = lfsck_namespace_dsd_single(env, com, child, pfid, &ldata,
3161                                                 &lh, &type, &retry);
3162                 if (retry) {
3163                         LASSERT(!lustre_handle_is_used(&lh));
3164
3165                         retry = false;
3166                         goto lock;
3167                 }
3168
3169                 GOTO(out, rc);
3170         }
3171
3172         if (!lustre_handle_is_used(&lh))
3173                 goto lock;
3174
3175         if (unlikely(ldata.ld_leh->leh_reccount == 0)) {
3176                 rc = lfsck_namespace_dsd_orphan(env, com, child, pfid, &lh,
3177                                                 &type);
3178
3179                 GOTO(out, rc);
3180         }
3181
3182         /* When we come here, the cases usually like that:
3183          * 1) The directory object has a corrupted linkEA entry. During the
3184          *    first-stage scanning, the LFSCK cannot know such corruption,
3185          *    then it appends the right linkEA entry according to the found
3186          *    name entry after the bad one.
3187          *
3188          * 2) The directory object has a right linkEA entry. During the
3189          *    first-stage scanning, the LFSCK finds some bad name entry,
3190          *    but the LFSCK cannot aware that at that time, then it adds
3191          *    the bad linkEA entry for further processing. */
3192         rc = lfsck_namespace_dsd_multiple(env, com, child, pfid, &ldata,
3193                                           &lh, &type, lpf);
3194
3195         GOTO(out, rc);
3196
3197 out:
3198         lfsck_ibits_unlock(&lh, LCK_EX);
3199         if (rc > 0) {
3200                 switch (type) {
3201                 case LNIT_BAD_LINKEA:
3202                         ns->ln_linkea_repaired++;
3203                         break;
3204                 case LNIT_UNMATCHED_PAIRS:
3205                         ns->ln_unmatched_pairs_repaired++;
3206                         break;
3207                 case LNIT_MUL_REF:
3208                         ns->ln_mul_ref_repaired++;
3209                         break;
3210                 default:
3211                         break;
3212                 }
3213         }
3214
3215         if (unknown)
3216                 ns->ln_unknown_inconsistency++;
3217
3218         return rc;
3219 }
3220
3221 /**
3222  * Double scan the MDT-object for namespace LFSCK.
3223  *
3224  * If the MDT-object contains invalid or repeated linkEA entries, then drop
3225  * those entries from the linkEA; if the linkEA becomes empty or the object
3226  * has no linkEA, then it is an orphan and will be added into the directory
3227  * .lustre/lost+found/MDTxxxx/; if the remote parent is lost, then recreate
3228  * the remote parent; if the name entry corresponding to some linkEA entry
3229  * is lost, then add the name entry back to the namespace.
3230  *
3231  * \param[in] env       pointer to the thread context
3232  * \param[in] com       pointer to the lfsck component
3233  * \param[in] child     pointer to the dt_object to be handled
3234  * \param[in] flags     some hints to indicate how the @child should be handled
3235  *
3236  * \retval              positive number for repaired cases
3237  * \retval              0 if nothing to be repaired
3238  * \retval              negative error number on failure
3239  */
3240 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
3241                                            struct lfsck_component *com,
3242                                            struct dt_object *child, __u8 flags)
3243 {
3244         struct lfsck_thread_info *info     = lfsck_env_info(env);
3245         struct lu_attr           *la       = &info->lti_la;
3246         struct lu_name           *cname    = &info->lti_name;
3247         struct lu_fid            *pfid     = &info->lti_fid;
3248         struct lu_fid            *cfid     = &info->lti_fid2;
3249         struct lfsck_instance    *lfsck    = com->lc_lfsck;
3250         struct lfsck_namespace   *ns       = com->lc_file_ram;
3251         struct dt_object         *parent   = NULL;
3252         struct linkea_data        ldata    = { NULL };
3253         bool                      repaired = false;
3254         int                       count    = 0;
3255         int                       rc;
3256         ENTRY;
3257
3258         dt_read_lock(env, child, 0);
3259         if (unlikely(lfsck_is_dead_obj(child))) {
3260                 dt_read_unlock(env, child);
3261
3262                 RETURN(0);
3263         }
3264
3265         if (S_ISDIR(lfsck_object_type(child))) {
3266                 dt_read_unlock(env, child);
3267                 rc = lfsck_namespace_double_scan_dir(env, com, child, flags);
3268
3269                 RETURN(rc);
3270         }
3271
3272         rc = lfsck_links_read(env, child, &ldata);
3273         dt_read_unlock(env, child);
3274         if (rc != 0)
3275                 GOTO(out, rc);
3276
3277         linkea_first_entry(&ldata);
3278         while (ldata.ld_lee != NULL) {
3279                 lfsck_namespace_unpack_linkea_entry(&ldata, cname, pfid,
3280                                                     info->lti_key);
3281                 rc = lfsck_namespace_filter_linkea_entry(&ldata, cname, pfid,
3282                                                          false);
3283                 /* Found repeated linkEA entries */
3284                 if (rc > 0) {
3285                         rc = lfsck_namespace_shrink_linkea(env, com, child,
3286                                                 &ldata, cname, pfid, false);
3287                         if (rc < 0)
3288                                 GOTO(out, rc);
3289
3290                         if (rc == 0)
3291                                 continue;
3292
3293                         repaired = true;
3294
3295                         /* fall through */
3296                 }
3297
3298                 /* Invalid PFID in the linkEA entry. */
3299                 if (!fid_is_sane(pfid)) {
3300                         rc = lfsck_namespace_shrink_linkea(env, com, child,
3301                                                 &ldata, cname, pfid, true);
3302                         if (rc < 0)
3303                                 GOTO(out, rc);
3304
3305                         if (rc > 0)
3306                                 repaired = true;
3307
3308                         continue;
3309                 }
3310
3311                 parent = lfsck_object_find_bottom(env, lfsck, pfid);
3312                 if (IS_ERR(parent))
3313                         GOTO(out, rc = PTR_ERR(parent));
3314
3315                 if (!dt_object_exists(parent)) {
3316
3317 lost_parent:
3318                         if (ldata.ld_leh->leh_reccount > 1) {
3319                                 /* If it is NOT the last linkEA entry, then
3320                                  * there is still other chance to make the
3321                                  * child to be visible via other parent, then
3322                                  * remove this linkEA entry. */
3323                                 rc = lfsck_namespace_shrink_linkea(env, com,
3324                                         child, &ldata, cname, pfid, true);
3325                         } else {
3326                                 /* If the LFSCK is marked as LF_INCOMPLETE,
3327                                  * then means some MDT has ever tried to
3328                                  * verify some remote MDT-object that resides
3329                                  * on this MDT, but this MDT failed to respond
3330                                  * such request. So means there may be some
3331                                  * remote name entry on other MDT that
3332                                  * references this object with another name,
3333                                  * so we cannot know whether this linkEA is
3334                                  * valid or not. So keep it there and maybe
3335                                  * resolved when next LFSCK run. */
3336                                 if (ns->ln_flags & LF_INCOMPLETE) {
3337                                         lfsck_object_put(env, parent);
3338
3339                                         GOTO(out, rc = 0);
3340                                 }
3341
3342                                 /* Create the lost parent as an orphan. */
3343                                 rc = lfsck_namespace_create_orphan_dir(env, com,
3344                                                                 parent, NULL);
3345                                 if (rc < 0) {
3346                                         lfsck_object_put(env, parent);
3347
3348                                         GOTO(out, rc);
3349                                 }
3350
3351                                 if (rc > 0)
3352                                         repaired = true;
3353
3354                                 /* Add the missing name entry to the parent. */
3355                                 rc = lfsck_namespace_insert_normal(env, com,
3356                                                 parent, child, cname->ln_name);
3357                                 if (unlikely(rc == -EEXIST))
3358                                         /* Unfortunately, someone reused the
3359                                          * name under the parent by race. So we
3360                                          * have to remove the linkEA entry from
3361                                          * current child object. It means that
3362                                          * the LFSCK cannot recover the system
3363                                          * totally back to its original status,
3364                                          * but it is necessary to make the
3365                                          * current system to be consistent. */
3366                                         rc = lfsck_namespace_shrink_linkea(env,
3367                                                         com, child, &ldata,
3368                                                         cname, pfid, true);
3369                                 else
3370                                         linkea_next_entry(&ldata);
3371                         }
3372
3373                         lfsck_object_put(env, parent);
3374                         if (rc < 0)
3375                                 GOTO(out, rc);
3376
3377                         if (rc > 0)
3378                                 repaired = true;
3379
3380                         continue;
3381                 }
3382
3383                 /* The linkEA entry with bad parent will be removed. */
3384                 if (unlikely(!dt_try_as_dir(env, parent))) {
3385                         lfsck_object_put(env, parent);
3386                         rc = lfsck_namespace_shrink_linkea(env, com, child,
3387                                                 &ldata, cname, pfid, true);
3388                         if (rc < 0)
3389                                 GOTO(out, rc);
3390
3391                         if (rc > 0)
3392                                 repaired = true;
3393
3394                         continue;
3395                 }
3396
3397                 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
3398                                (const struct dt_key *)cname->ln_name,
3399                                BYPASS_CAPA);
3400                 if (rc != 0 && rc != -ENOENT) {
3401                         lfsck_object_put(env, parent);
3402
3403                         GOTO(out, rc);
3404                 }
3405
3406                 if (rc == 0) {
3407                         if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
3408                                 /* It is the most common case that we
3409                                  * find the name entry corresponding
3410                                  * to the linkEA entry. */
3411                                 lfsck_object_put(env, parent);
3412                                 linkea_next_entry(&ldata);
3413                         } else {
3414                                 /* The name entry references another
3415                                  * MDT-object that may be created by
3416                                  * the LFSCK for repairing dangling
3417                                  * name entry. Try to replace it. */
3418                                 rc = lfsck_namespace_replace_cond(env, com,
3419                                                 parent, child, cfid, cname);
3420                                 lfsck_object_put(env, parent);
3421                                 if (rc < 0)
3422                                         GOTO(out, rc);
3423
3424                                 if (rc > 0) {
3425                                         repaired = true;
3426                                         linkea_next_entry(&ldata);
3427                                 } else {
3428                                         rc = lfsck_namespace_shrink_linkea(env,
3429                                                         com, child, &ldata,
3430                                                         cname, pfid, true);
3431                                         if (rc < 0)
3432                                                 GOTO(out, rc);
3433
3434                                         if (rc > 0)
3435                                                 repaired = true;
3436                                 }
3437                         }
3438
3439                         continue;
3440                 }
3441
3442                 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3443                 if (rc != 0)
3444                         GOTO(out, rc);
3445
3446                 /* If there is no name entry in the parent dir and the object
3447                  * link count is less than the linkea entries count, then the
3448                  * linkea entry should be removed. */
3449                 if (ldata.ld_leh->leh_reccount > la->la_nlink) {
3450                         rc = lfsck_namespace_shrink_linkea_cond(env, com,
3451                                         parent, child, &ldata, cname, pfid);
3452                         lfsck_object_put(env, parent);
3453                         if (rc < 0)
3454                                 GOTO(out, rc);
3455
3456                         if (rc > 0)
3457                                 repaired = true;
3458
3459                         continue;
3460                 }
3461
3462                 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3463                  * MDT has ever tried to verify some remote MDT-object that
3464                  * resides on this MDT, but this MDT failed to respond such
3465                  * request. So means there may be some remote name entry on
3466                  * other MDT that references this object with another name,
3467                  * so we cannot know whether this linkEA is valid or not.
3468                  * So keep it there and maybe resolved when next LFSCK run. */
3469                 if (ns->ln_flags & LF_INCOMPLETE) {
3470                         lfsck_object_put(env, parent);
3471
3472                         GOTO(out, rc = 0);
3473                 }
3474
3475                 rc = lfsck_namespace_check_name(env, parent, child, cname);
3476                 if (rc == -ENOENT)
3477                         goto lost_parent;
3478
3479                 if (rc < 0) {
3480                         lfsck_object_put(env, parent);
3481
3482                         GOTO(out, rc);
3483                 }
3484
3485                 /* It is an invalid name entry, drop it. */
3486                 if (unlikely(rc > 0)) {
3487                         lfsck_object_put(env, parent);
3488                         rc = lfsck_namespace_shrink_linkea(env, com, child,
3489                                                 &ldata, cname, pfid, true);
3490                         if (rc < 0)
3491                                 GOTO(out, rc);
3492
3493                         if (rc > 0)
3494                                 repaired = true;
3495
3496                         continue;
3497                 }
3498
3499                 /* Add the missing name entry back to the namespace. */
3500                 rc = lfsck_namespace_insert_normal(env, com, parent, child,
3501                                                    cname->ln_name);
3502                 if (unlikely(rc == -ESTALE))
3503                         /* It may happen when the remote object has been
3504                          * removed, but the local MDT is not aware of that. */
3505                         goto lost_parent;
3506
3507                 if (unlikely(rc == -EEXIST))
3508                         /* Unfortunately, someone reused the name under the
3509                          * parent by race. So we have to remove the linkEA
3510                          * entry from current child object. It means that the
3511                          * LFSCK cannot recover the system totally back to
3512                          * its original status, but it is necessary to make
3513                          * the current system to be consistent.
3514                          *
3515                          * It also may be because of the LFSCK found some
3516                          * internal status of create operation. Under such
3517                          * case, nothing to be done. */
3518                         rc = lfsck_namespace_shrink_linkea_cond(env, com,
3519                                         parent, child, &ldata, cname, pfid);
3520                 else
3521                         linkea_next_entry(&ldata);
3522
3523                 lfsck_object_put(env, parent);
3524                 if (rc < 0)
3525                         GOTO(out, rc);
3526
3527                 if (rc > 0)
3528                         repaired = true;
3529         }
3530
3531         GOTO(out, rc = 0);
3532
3533 out:
3534         if (rc < 0 && rc != -ENODATA)
3535                 return rc;
3536
3537         if (rc == 0) {
3538                 LASSERT(ldata.ld_leh != NULL);
3539
3540                 count = ldata.ld_leh->leh_reccount;
3541         }
3542
3543         if (count == 0) {
3544                 /* If the LFSCK is marked as LF_INCOMPLETE, then means some
3545                  * MDT has ever tried to verify some remote MDT-object that
3546                  * resides on this MDT, but this MDT failed to respond such
3547                  * request. So means there may be some remote name entry on
3548                  * other MDT that references this object with another name,
3549                  * so we cannot know whether this linkEA is valid or not.
3550                  * So keep it there and maybe resolved when next LFSCK run. */
3551                 if (!(ns->ln_flags & LF_INCOMPLETE)) {
3552                         /* If the child becomes orphan, then insert it into
3553                          * the global .lustre/lost+found/MDTxxxx directory. */
3554                         rc = lfsck_namespace_insert_orphan(env, com, child,
3555                                                            "", "O", &count);
3556                         if (rc < 0)
3557                                 return rc;
3558
3559                         if (rc > 0) {
3560                                 ns->ln_mul_ref_repaired++;
3561                                 repaired = true;
3562                         }
3563                 }
3564         } else {
3565                 rc = dt_attr_get(env, child, la, BYPASS_CAPA);
3566                 if (rc != 0)
3567                         return rc;
3568
3569                 if (la->la_nlink != 0 && la->la_nlink != count) {
3570                         rc = lfsck_namespace_repair_nlink(env, com, child, la);
3571                         if (rc > 0) {
3572                                 ns->ln_objs_nlink_repaired++;
3573                                 rc = 0;
3574                         }
3575                 }
3576         }
3577
3578         if (repaired) {
3579                 if (la->la_nlink > 1)
3580                         ns->ln_mul_linked_repaired++;
3581
3582                 if (rc == 0)
3583                         rc = 1;
3584         }
3585
3586         return rc;
3587 }
3588
3589 static void lfsck_namespace_dump_statistics(struct seq_file *m,
3590                                             struct lfsck_namespace *ns,
3591                                             __u64 checked_phase1,
3592                                             __u64 checked_phase2,
3593                                             __u32 time_phase1,
3594                                             __u32 time_phase2)
3595 {
3596         seq_printf(m, "checked_phase1: "LPU64"\n"
3597                       "checked_phase2: "LPU64"\n"
3598                       "updated_phase1: "LPU64"\n"
3599                       "updated_phase2: "LPU64"\n"
3600                       "failed_phase1: "LPU64"\n"
3601                       "failed_phase2: "LPU64"\n"
3602                       "directories: "LPU64"\n"
3603                       "dirent_repaired: "LPU64"\n"
3604                       "linkea_repaired: "LPU64"\n"
3605                       "nlinks_repaired: "LPU64"\n"
3606                       "multiple_linked_checked: "LPU64"\n"
3607                       "multiple_linked_repaired: "LPU64"\n"
3608                       "unknown_inconsistency: "LPU64"\n"
3609                       "unmatched_pairs_repaired: "LPU64"\n"
3610                       "dangling_repaired: "LPU64"\n"
3611                       "multiple_referenced_repaired: "LPU64"\n"
3612                       "bad_file_type_repaired: "LPU64"\n"
3613                       "lost_dirent_repaired: "LPU64"\n"
3614                       "local_lost_found_scanned: "LPU64"\n"
3615                       "local_lost_found_moved: "LPU64"\n"
3616                       "local_lost_found_skipped: "LPU64"\n"
3617                       "local_lost_found_failed: "LPU64"\n"
3618                       "striped_dirs_scanned: "LPU64"\n"
3619                       "striped_dirs_repaired: "LPU64"\n"
3620                       "striped_dirs_failed: "LPU64"\n"
3621                       "striped_dirs_disabled: "LPU64"\n"
3622                       "striped_dirs_skipped: "LPU64"\n"
3623                       "striped_shards_scanned: "LPU64"\n"
3624                       "striped_shards_repaired: "LPU64"\n"
3625                       "striped_shards_failed: "LPU64"\n"
3626                       "striped_shards_skipped: "LPU64"\n"
3627                       "name_hash_repaired: "LPU64"\n"
3628                       "success_count: %u\n"
3629                       "run_time_phase1: %u seconds\n"
3630                       "run_time_phase2: %u seconds\n",
3631                       checked_phase1,
3632                       checked_phase2,
3633                       ns->ln_items_repaired,
3634                       ns->ln_objs_repaired_phase2,
3635                       ns->ln_items_failed,
3636                       ns->ln_objs_failed_phase2,
3637                       ns->ln_dirs_checked,
3638                       ns->ln_dirent_repaired,
3639                       ns->ln_linkea_repaired,
3640                       ns->ln_objs_nlink_repaired,
3641                       ns->ln_mul_linked_checked,
3642                       ns->ln_mul_linked_repaired,
3643                       ns->ln_unknown_inconsistency,
3644                       ns->ln_unmatched_pairs_repaired,
3645                       ns->ln_dangling_repaired,
3646                       ns->ln_mul_ref_repaired,
3647                       ns->ln_bad_type_repaired,
3648                       ns->ln_lost_dirent_repaired,
3649                       ns->ln_local_lpf_scanned,
3650                       ns->ln_local_lpf_moved,
3651                       ns->ln_local_lpf_skipped,
3652                       ns->ln_local_lpf_failed,
3653                       ns->ln_striped_dirs_scanned,
3654                       ns->ln_striped_dirs_repaired,
3655                       ns->ln_striped_dirs_failed,
3656                       ns->ln_striped_dirs_disabled,
3657                       ns->ln_striped_dirs_skipped,
3658                       ns->ln_striped_shards_scanned,
3659                       ns->ln_striped_shards_repaired,
3660                       ns->ln_striped_shards_failed,
3661                       ns->ln_striped_shards_skipped,
3662                       ns->ln_name_hash_repaired,
3663                       ns->ln_success_count,
3664                       time_phase1,
3665                       time_phase2);
3666 }
3667
3668 static void lfsck_namespace_release_lmv(const struct lu_env *env,
3669                                         struct lfsck_component *com)
3670 {
3671         struct lfsck_instance           *lfsck  = com->lc_lfsck;
3672         struct lfsck_namespace          *ns     = com->lc_file_ram;
3673
3674         while (!list_empty(&lfsck->li_list_lmv)) {
3675                 struct lfsck_lmv_unit   *llu;
3676                 struct lfsck_lmv        *llmv;
3677
3678                 llu = list_entry(lfsck->li_list_lmv.next,
3679                                  struct lfsck_lmv_unit, llu_link);
3680                 llmv = &llu->llu_lmv;
3681
3682                 LASSERTF(atomic_read(&llmv->ll_ref) == 1,
3683                          "still in using: %u\n",
3684                          atomic_read(&llmv->ll_ref));
3685
3686                 ns->ln_striped_dirs_skipped++;
3687                 lfsck_lmv_put(env, llmv);
3688         }
3689 }
3690
3691 static int lfsck_namespace_check_for_double_scan(const struct lu_env *env,
3692                                                  struct lfsck_component *com,
3693                                                  struct dt_object *obj)
3694 {
3695         struct lu_attr *la = &lfsck_env_info(env)->lti_la;
3696         int             rc;
3697
3698         rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
3699         if (rc != 0)
3700                 return rc;
3701
3702         /* zero-linkEA object may be orphan, but it also maybe because
3703          * of upgrading. Currently, we cannot record it for double scan.
3704          * Because it may cause the LFSCK trace file to be too large. */
3705
3706         /* "la_ctime" == 1 means that it has ever been removed from
3707          * backend /lost+found directory but not been added back to
3708          * the normal namespace yet. */
3709
3710         if ((S_ISREG(lfsck_object_type(obj)) && la->la_nlink > 1) ||
3711             unlikely(la->la_ctime == 1))
3712                 rc = lfsck_namespace_trace_update(env, com, lfsck_dto2fid(obj),
3713                                                   LNTF_CHECK_LINKEA, true);
3714
3715         return rc;
3716 }
3717
3718 /* namespace APIs */
3719
3720 static int lfsck_namespace_reset(const struct lu_env *env,
3721                                  struct lfsck_component *com, bool init)
3722 {
3723         struct lfsck_instance           *lfsck  = com->lc_lfsck;
3724         struct lfsck_namespace          *ns     = com->lc_file_ram;
3725         struct lfsck_assistant_data     *lad    = com->lc_data;
3726         struct dt_object                *root;
3727         struct dt_object                *dto;
3728         int                              rc;
3729         ENTRY;
3730
3731         root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
3732         if (IS_ERR(root))
3733                 GOTO(log, rc = PTR_ERR(root));
3734
3735         if (unlikely(!dt_try_as_dir(env, root)))
3736                 GOTO(put, rc = -ENOTDIR);
3737
3738         down_write(&com->lc_sem);
3739         if (init) {
3740                 memset(ns, 0, sizeof(*ns));
3741         } else {
3742                 __u32 count = ns->ln_success_count;
3743                 __u64 last_time = ns->ln_time_last_complete;
3744
3745                 memset(ns, 0, sizeof(*ns));
3746                 ns->ln_success_count = count;
3747                 ns->ln_time_last_complete = last_time;
3748         }
3749         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
3750         ns->ln_status = LS_INIT;
3751
3752         lfsck_object_put(env, com->lc_obj);
3753         com->lc_obj = NULL;
3754         dto = lfsck_namespace_load_one_trace_file(env, com, root,
3755                                 LFSCK_NAMESPACE, NULL, true);
3756         if (IS_ERR(dto))
3757                 GOTO(out, rc = PTR_ERR(dto));
3758
3759         com->lc_obj = dto;
3760         rc = lfsck_namespace_load_sub_trace_files(env, com, true);
3761         if (rc != 0)
3762                 GOTO(out, rc);
3763
3764         lad->lad_incomplete = 0;
3765         CFS_RESET_BITMAP(lad->lad_bitmap);
3766
3767         rc = lfsck_namespace_store(env, com, true);
3768
3769         GOTO(out, rc);
3770
3771 out:
3772         up_write(&com->lc_sem);
3773
3774 put:
3775         lfsck_object_put(env, root);
3776 log:
3777         CDEBUG(D_LFSCK, "%s: namespace LFSCK reset: rc = %d\n",
3778                lfsck_lfsck2name(lfsck), rc);
3779         return rc;
3780 }
3781
3782 static void
3783 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
3784                      bool new_checked)
3785 {
3786         struct lfsck_namespace *ns = com->lc_file_ram;
3787
3788         down_write(&com->lc_sem);
3789         if (new_checked)
3790                 com->lc_new_checked++;
3791         lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
3792         up_write(&com->lc_sem);
3793 }
3794
3795 static void lfsck_namespace_close_dir(const struct lu_env *env,
3796                                       struct lfsck_component *com)
3797 {
3798         struct lfsck_namespace          *ns     = com->lc_file_ram;
3799         struct lfsck_assistant_data     *lad    = com->lc_data;
3800         struct lfsck_instance           *lfsck  = com->lc_lfsck;
3801         struct lfsck_lmv                *llmv   = lfsck->li_lmv;
3802         struct lfsck_namespace_req      *lnr;
3803         __u32                            size   =
3804                                 sizeof(*lnr) + LFSCK_TMPBUF_LEN;
3805         bool                             wakeup = false;
3806         ENTRY;
3807
3808         if (llmv == NULL)
3809                 RETURN_EXIT;
3810
3811         OBD_ALLOC(lnr, size);
3812         if (lnr == NULL) {
3813                 ns->ln_striped_dirs_skipped++;
3814
3815                 RETURN_EXIT;
3816         }
3817
3818         /* Generate a dummy request to indicate that all shards' name entry
3819          * in this striped directory has been scanned for the first time. */
3820         INIT_LIST_HEAD(&lnr->lnr_lar.lar_list);
3821         lnr->lnr_lar.lar_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3822         lnr->lnr_lmv = lfsck_lmv_get(llmv);
3823         lnr->lnr_fid = *lfsck_dto2fid(lfsck->li_obj_dir);
3824         lnr->lnr_oit_cookie = lfsck->li_pos_current.lp_oit_cookie;
3825         lnr->lnr_dir_cookie = MDS_DIR_END_OFF;
3826         lnr->lnr_size = size;
3827
3828         spin_lock(&lad->lad_lock);
3829         if (lad->lad_assistant_status < 0) {
3830                 spin_unlock(&lad->lad_lock);
3831                 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
3832                 ns->ln_striped_dirs_skipped++;
3833
3834                 RETURN_EXIT;
3835         }
3836
3837         list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
3838         if (lad->lad_prefetched == 0)
3839                 wakeup = true;
3840
3841         lad->lad_prefetched++;
3842         spin_unlock(&lad->lad_lock);
3843         if (wakeup)
3844                 wake_up_all(&lad->lad_thread.t_ctl_waitq);
3845
3846         EXIT;
3847 }
3848
3849 static int lfsck_namespace_open_dir(const struct lu_env *env,
3850                                     struct lfsck_component *com)
3851 {
3852         struct lfsck_instance   *lfsck  = com->lc_lfsck;
3853         struct lfsck_namespace  *ns     = com->lc_file_ram;
3854         struct lfsck_lmv        *llmv   = lfsck->li_lmv;
3855         int                      rc     = 0;
3856         ENTRY;
3857
3858         if (llmv == NULL)
3859                 RETURN(0);
3860
3861         if (llmv->ll_lmv_master) {
3862                 struct lmv_mds_md_v1 *lmv = &llmv->ll_lmv;
3863
3864                 if (lmv->lmv_master_mdt_index != lfsck_dev_idx(lfsck)) {
3865                         lmv->lmv_master_mdt_index =
3866                                 lfsck_dev_idx(lfsck);
3867                         ns->ln_flags |= LF_INCONSISTENT;
3868                         llmv->ll_lmv_updated = 1;
3869                 }
3870         } else {
3871                 rc = lfsck_namespace_verify_stripe_slave(env, com,
3872                                         lfsck->li_obj_dir, llmv);
3873         }
3874
3875         RETURN(rc > 0 ? 0 : rc);
3876 }
3877
3878 static int lfsck_namespace_checkpoint(const struct lu_env *env,
3879                                       struct lfsck_component *com, bool init)
3880 {
3881         struct lfsck_instance   *lfsck = com->lc_lfsck;
3882         struct lfsck_namespace  *ns    = com->lc_file_ram;
3883         int                      rc;
3884
3885         if (!init) {
3886                 rc = lfsck_checkpoint_generic(env, com);
3887                 if (rc != 0)
3888                         goto log;
3889         }
3890
3891         down_write(&com->lc_sem);
3892         if (init) {
3893                 ns->ln_pos_latest_start = lfsck->li_pos_checkpoint;
3894         } else {
3895                 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
3896                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
3897                                 HALF_SEC - lfsck->li_time_last_checkpoint);
3898                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
3899                 ns->ln_items_checked += com->lc_new_checked;
3900                 com->lc_new_checked = 0;
3901         }
3902
3903         rc = lfsck_namespace_store(env, com, false);
3904         up_write(&com->lc_sem);
3905
3906 log:
3907         CDEBUG(D_LFSCK, "%s: namespace LFSCK checkpoint at the pos ["LPU64
3908                ", "DFID", "LPX64"]: rc = %d\n", lfsck_lfsck2name(lfsck),
3909                lfsck->li_pos_current.lp_oit_cookie,
3910                PFID(&lfsck->li_pos_current.lp_dir_parent),
3911                lfsck->li_pos_current.lp_dir_cookie, rc);
3912
3913         return rc > 0 ? 0 : rc;
3914 }
3915
3916 static int lfsck_namespace_prep(const struct lu_env *env,
3917                                 struct lfsck_component *com,
3918                                 struct lfsck_start_param *lsp)
3919 {
3920         struct lfsck_instance   *lfsck  = com->lc_lfsck;
3921         struct lfsck_namespace  *ns     = com->lc_file_ram;
3922         struct lfsck_position   *pos    = &com->lc_pos_start;
3923         int                      rc;
3924
3925         rc = lfsck_namespace_load_bitmap(env, com);
3926         if (rc != 0 || ns->ln_status == LS_COMPLETED) {
3927                 rc = lfsck_namespace_reset(env, com, false);
3928                 if (rc == 0)
3929                         rc = lfsck_set_param(env, lfsck, lsp->lsp_start, true);
3930
3931                 if (rc != 0) {
3932                         CDEBUG(D_LFSCK, "%s: namespace LFSCK prep failed: "
3933                                "rc = %d\n", lfsck_lfsck2name(lfsck), rc);
3934
3935                         return rc;
3936                 }
3937         }
3938
3939         down_write(&com->lc_sem);
3940         ns->ln_time_latest_start = cfs_time_current_sec();
3941         spin_lock(&lfsck->li_lock);
3942
3943         if (ns->ln_flags & LF_SCANNED_ONCE) {
3944                 if (!lfsck->li_drop_dryrun ||
3945                     lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3946                         ns->ln_status = LS_SCANNING_PHASE2;
3947                         list_move_tail(&com->lc_link,
3948                                        &lfsck->li_list_double_scan);
3949                         if (!list_empty(&com->lc_link_dir))
3950                                 list_del_init(&com->lc_link_dir);
3951                         lfsck_pos_set_zero(pos);
3952                 } else {
3953                         ns->ln_status = LS_SCANNING_PHASE1;
3954                         ns->ln_run_time_phase1 = 0;
3955                         ns->ln_run_time_phase2 = 0;
3956                         ns->ln_items_checked = 0;
3957                         ns->ln_items_repaired = 0;
3958                         ns->ln_items_failed = 0;
3959                         ns->ln_dirs_checked = 0;
3960                         ns->ln_objs_checked_phase2 = 0;
3961                         ns->ln_objs_repaired_phase2 = 0;
3962                         ns->ln_objs_failed_phase2 = 0;
3963                         ns->ln_objs_nlink_repaired = 0;
3964                         ns->ln_dirent_repaired = 0;
3965                         ns->ln_linkea_repaired = 0;
3966                         ns->ln_mul_linked_checked = 0;
3967                         ns->ln_mul_linked_repaired = 0;
3968                         ns->ln_unknown_inconsistency = 0;
3969                         ns->ln_unmatched_pairs_repaired = 0;
3970                         ns->ln_dangling_repaired = 0;
3971                         ns->ln_mul_ref_repaired = 0;
3972                         ns->ln_bad_type_repaired = 0;
3973                         ns->ln_lost_dirent_repaired = 0;
3974                         ns->ln_striped_dirs_scanned = 0;
3975                         ns->ln_striped_dirs_repaired = 0;
3976                         ns->ln_striped_dirs_failed = 0;
3977                         ns->ln_striped_dirs_disabled = 0;
3978                         ns->ln_striped_dirs_skipped = 0;
3979                         ns->ln_striped_shards_scanned = 0;
3980                         ns->ln_striped_shards_repaired = 0;
3981                         ns->ln_striped_shards_failed = 0;
3982                         ns->ln_striped_shards_skipped = 0;
3983                         ns->ln_name_hash_repaired = 0;
3984                         fid_zero(&ns->ln_fid_latest_scanned_phase2);
3985                         if (list_empty(&com->lc_link_dir))
3986                                 list_add_tail(&com->lc_link_dir,
3987                                               &lfsck->li_list_dir);
3988                         *pos = ns->ln_pos_first_inconsistent;
3989                 }
3990         } else {
3991                 ns->ln_status = LS_SCANNING_PHASE1;
3992                 if (list_empty(&com->lc_link_dir))
3993                         list_add_tail(&com->lc_link_dir,
3994                                       &lfsck->li_list_dir);
3995                 if (!lfsck->li_drop_dryrun ||
3996                     lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
3997                         *pos = ns->ln_pos_last_checkpoint;
3998                         pos->lp_oit_cookie++;
3999                 } else {
4000                         *pos = ns->ln_pos_first_inconsistent;
4001                 }
4002         }
4003
4004         spin_unlock(&lfsck->li_lock);
4005         up_write(&com->lc_sem);
4006
4007         rc = lfsck_start_assistant(env, com, lsp);
4008
4009         CDEBUG(D_LFSCK, "%s: namespace LFSCK prep done, start pos ["LPU64", "
4010                DFID", "LPX64"]: rc = %d\n",
4011                lfsck_lfsck2name(lfsck), pos->lp_oit_cookie,
4012                PFID(&pos->lp_dir_parent), pos->lp_dir_cookie, rc);
4013
4014         return rc;
4015 }
4016
4017 static int lfsck_namespace_exec_oit(const struct lu_env *env,
4018                                     struct lfsck_component *com,
4019                                     struct dt_object *obj)
4020 {
4021         struct lfsck_thread_info *info  = lfsck_env_info(env);
4022         struct lfsck_namespace   *ns    = com->lc_file_ram;
4023         struct lfsck_instance    *lfsck = com->lc_lfsck;
4024         const struct lu_fid      *fid   = lfsck_dto2fid(obj);
4025         struct lu_fid            *pfid  = &info->lti_fid2;
4026         struct lu_name           *cname = &info->lti_name;
4027         struct lu_seq_range      *range = &info->lti_range;
4028         struct seq_server_site   *ss    = lfsck_dev_site(lfsck);
4029         struct linkea_data        ldata = { NULL };
4030         __u32                     idx   = lfsck_dev_idx(lfsck);
4031         int                       rc;
4032         ENTRY;
4033
4034         rc = lfsck_links_read(env, obj, &ldata);
4035         if (rc == -ENOENT)
4036                 GOTO(out, rc = 0);
4037
4038         /* -EINVAL means crashed linkEA, should be verified. */
4039         if (rc == -EINVAL) {
4040                 rc = lfsck_namespace_trace_update(env, com, fid,
4041                                                   LNTF_CHECK_LINKEA, true);
4042                 if (rc == 0) {
4043                         struct lustre_handle lh = { 0 };
4044
4045                         rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
4046                                               MDS_INODELOCK_UPDATE |
4047                                               MDS_INODELOCK_XATTR, LCK_EX);
4048                         if (rc == 0) {
4049                                 rc = lfsck_namespace_links_remove(env, com,
4050                                                                   obj);
4051                                 lfsck_ibits_unlock(&lh, LCK_EX);
4052                         }
4053                 }
4054
4055                 GOTO(out, rc = (rc == -ENOENT ? 0 : rc));
4056         }
4057
4058         if (rc == -ENODATA) {
4059                 rc = lfsck_namespace_check_for_double_scan(env, com, obj);
4060
4061                 GOTO(out, rc);
4062         }
4063
4064         if (rc != 0)
4065                 GOTO(out, rc);
4066
4067         /* Record multiple-linked object. */
4068         if (ldata.ld_leh->leh_reccount > 1) {
4069                 rc = lfsck_namespace_trace_update(env, com, fid,
4070                                                   LNTF_CHECK_LINKEA, true);
4071
4072                 GOTO(out, rc);
4073         }
4074
4075         linkea_first_entry(&ldata);
4076         linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
4077         if (!fid_is_sane(pfid)) {
4078                 rc = lfsck_namespace_trace_update(env, com, fid,
4079                                                   LNTF_CHECK_PARENT, true);
4080         } else {
4081                 fld_range_set_mdt(range);
4082                 rc = fld_local_lookup(env, ss->ss_server_fld,
4083                                       fid_seq(pfid), range);
4084                 if ((rc == -ENOENT) ||
4085                     (rc == 0 && range->lsr_index != idx))
4086                         rc = lfsck_namespace_trace_update(env, com, fid,
4087                                                 LNTF_CHECK_LINKEA, true);
4088                 else
4089                         rc = lfsck_namespace_check_for_double_scan(env, com,
4090                                                                    obj);
4091         }
4092
4093         GOTO(out, rc);
4094
4095 out:
4096         down_write(&com->lc_sem);
4097         com->lc_new_checked++;
4098         if (S_ISDIR(lfsck_object_type(obj)))
4099                 ns->ln_dirs_checked++;
4100         if (rc != 0)
4101                 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4102         up_write(&com->lc_sem);
4103
4104         return rc;
4105 }
4106
4107 static int lfsck_namespace_exec_dir(const struct lu_env *env,
4108                                     struct lfsck_component *com,
4109                                     struct lu_dirent *ent, __u16 type)
4110 {
4111         struct lfsck_assistant_data     *lad     = com->lc_data;
4112         struct lfsck_instance           *lfsck   = com->lc_lfsck;
4113         struct lfsck_namespace_req      *lnr;
4114         struct lfsck_bookmark           *bk      = &lfsck->li_bookmark_ram;
4115         struct ptlrpc_thread            *mthread = &lfsck->li_thread;
4116         struct ptlrpc_thread            *athread = &lad->lad_thread;
4117         struct l_wait_info               lwi     = { 0 };
4118         bool                             wakeup  = false;
4119
4120         l_wait_event(mthread->t_ctl_waitq,
4121                      lad->lad_prefetched < bk->lb_async_windows ||
4122                      !thread_is_running(mthread) ||
4123                      thread_is_stopped(athread),
4124                      &lwi);
4125
4126         if (unlikely(!thread_is_running(mthread)) ||
4127                      thread_is_stopped(athread))
4128                 return 0;
4129
4130         if (unlikely(lfsck_is_dead_obj(lfsck->li_obj_dir)))
4131                 return 0;
4132
4133         lnr = lfsck_namespace_assistant_req_init(com->lc_lfsck, ent, type);
4134         if (IS_ERR(lnr)) {
4135                 struct lfsck_namespace *ns = com->lc_file_ram;
4136
4137                 lfsck_namespace_record_failure(env, com->lc_lfsck, ns);
4138                 return PTR_ERR(lnr);
4139         }
4140
4141         spin_lock(&lad->lad_lock);
4142         if (lad->lad_assistant_status < 0) {
4143                 spin_unlock(&lad->lad_lock);
4144                 lfsck_namespace_assistant_req_fini(env, &lnr->lnr_lar);
4145                 return lad->lad_assistant_status;
4146         }
4147
4148         list_add_tail(&lnr->lnr_lar.lar_list, &lad->lad_req_list);
4149         if (lad->lad_prefetched == 0)
4150                 wakeup = true;
4151
4152         lad->lad_prefetched++;
4153         spin_unlock(&lad->lad_lock);
4154         if (wakeup)
4155                 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4156
4157         down_write(&com->lc_sem);
4158         com->lc_new_checked++;
4159         up_write(&com->lc_sem);
4160
4161         return 0;
4162 }
4163
4164 static int lfsck_namespace_post(const struct lu_env *env,
4165                                 struct lfsck_component *com,
4166                                 int result, bool init)
4167 {
4168         struct lfsck_instance   *lfsck = com->lc_lfsck;
4169         struct lfsck_namespace  *ns    = com->lc_file_ram;
4170         int                      rc;
4171         ENTRY;
4172
4173         lfsck_post_generic(env, com, &result);
4174
4175         down_write(&com->lc_sem);
4176         lfsck_namespace_release_lmv(env, com);
4177
4178         spin_lock(&lfsck->li_lock);
4179         if (!init)
4180                 ns->ln_pos_last_checkpoint = lfsck->li_pos_checkpoint;
4181         if (result > 0) {
4182                 ns->ln_status = LS_SCANNING_PHASE2;
4183                 ns->ln_flags |= LF_SCANNED_ONCE;
4184                 ns->ln_flags &= ~LF_UPGRADE;
4185                 list_del_init(&com->lc_link_dir);
4186                 list_move_tail(&com->lc_link, &lfsck->li_list_double_scan);
4187         } else if (result == 0) {
4188                 if (lfsck->li_status != 0)
4189                         ns->ln_status = lfsck->li_status;
4190                 else
4191                         ns->ln_status = LS_STOPPED;
4192                 if (ns->ln_status != LS_PAUSED) {
4193                         list_del_init(&com->lc_link_dir);
4194                         list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4195                 }
4196         } else {
4197                 ns->ln_status = LS_FAILED;
4198                 list_del_init(&com->lc_link_dir);
4199                 list_move_tail(&com->lc_link, &lfsck->li_list_idle);
4200         }
4201         spin_unlock(&lfsck->li_lock);
4202
4203         if (!init) {
4204                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
4205                                 HALF_SEC - lfsck->li_time_last_checkpoint);
4206                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
4207                 ns->ln_items_checked += com->lc_new_checked;
4208                 com->lc_new_checked = 0;
4209         }
4210
4211         rc = lfsck_namespace_store(env, com, false);
4212         up_write(&com->lc_sem);
4213
4214         CDEBUG(D_LFSCK, "%s: namespace LFSCK post done: rc = %d\n",
4215                lfsck_lfsck2name(lfsck), rc);
4216
4217         RETURN(rc);
4218 }
4219
4220 static int
4221 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
4222                      struct seq_file *m)
4223 {
4224         struct lfsck_instance   *lfsck = com->lc_lfsck;
4225         struct lfsck_bookmark   *bk    = &lfsck->li_bookmark_ram;
4226         struct lfsck_namespace  *ns    = com->lc_file_ram;
4227         int                      rc;
4228
4229         down_read(&com->lc_sem);
4230         seq_printf(m, "name: lfsck_namespace\n"
4231                    "magic: %#x\n"
4232                    "version: %d\n"
4233                    "status: %s\n",
4234                    ns->ln_magic,
4235                    bk->lb_version,
4236                    lfsck_status2names(ns->ln_status));
4237
4238         rc = lfsck_bits_dump(m, ns->ln_flags, lfsck_flags_names, "flags");
4239         if (rc < 0)
4240                 goto out;
4241
4242         rc = lfsck_bits_dump(m, bk->lb_param, lfsck_param_names, "param");
4243         if (rc < 0)
4244                 goto out;
4245
4246         rc = lfsck_time_dump(m, ns->ln_time_last_complete,
4247                              "time_since_last_completed");
4248         if (rc < 0)
4249                 goto out;
4250
4251         rc = lfsck_time_dump(m, ns->ln_time_latest_start,
4252                              "time_since_latest_start");
4253         if (rc < 0)
4254                 goto out;
4255
4256         rc = lfsck_time_dump(m, ns->ln_time_last_checkpoint,
4257                              "time_since_last_checkpoint");
4258         if (rc < 0)
4259                 goto out;
4260
4261         rc = lfsck_pos_dump(m, &ns->ln_pos_latest_start,
4262                             "latest_start_position");
4263         if (rc < 0)
4264                 goto out;
4265
4266         rc = lfsck_pos_dump(m, &ns->ln_pos_last_checkpoint,
4267                             "last_checkpoint_position");
4268         if (rc < 0)
4269                 goto out;
4270
4271         rc = lfsck_pos_dump(m, &ns->ln_pos_first_inconsistent,
4272                             "first_failure_position");
4273         if (rc < 0)
4274                 goto out;
4275
4276         if (ns->ln_status == LS_SCANNING_PHASE1) {
4277                 struct lfsck_position pos;
4278                 const struct dt_it_ops *iops;
4279                 cfs_duration_t duration = cfs_time_current() -
4280                                           lfsck->li_time_last_checkpoint;
4281                 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
4282                 __u64 speed = checked;
4283                 __u64 new_checked = com->lc_new_checked *
4284                                     msecs_to_jiffies(MSEC_PER_SEC);
4285                 __u32 rtime = ns->ln_run_time_phase1 +
4286                               cfs_duration_sec(duration + HALF_SEC);
4287
4288                 if (duration != 0)
4289                         do_div(new_checked, duration);
4290                 if (rtime != 0)
4291                         do_div(speed, rtime);
4292                 lfsck_namespace_dump_statistics(m, ns, checked,
4293                                                 ns->ln_objs_checked_phase2,
4294                                                 rtime, ns->ln_run_time_phase2);
4295
4296                 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4297                               "average_speed_phase2: N/A\n"
4298                               "real_time_speed_phase1: "LPU64" items/sec\n"
4299                               "real_time_speed_phase2: N/A\n",
4300                               speed,
4301                               new_checked);
4302
4303                 LASSERT(lfsck->li_di_oit != NULL);
4304
4305                 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
4306
4307                 /* The low layer otable-based iteration position may NOT
4308                  * exactly match the namespace-based directory traversal
4309                  * cookie. Generally, it is not a serious issue. But the
4310                  * caller should NOT make assumption on that. */
4311                 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
4312                 if (!lfsck->li_current_oit_processed)
4313                         pos.lp_oit_cookie--;
4314
4315                 spin_lock(&lfsck->li_lock);
4316                 if (lfsck->li_di_dir != NULL) {
4317                         pos.lp_dir_cookie = lfsck->li_cookie_dir;
4318                         if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
4319                                 fid_zero(&pos.lp_dir_parent);
4320                                 pos.lp_dir_cookie = 0;
4321                         } else {
4322                                 pos.lp_dir_parent =
4323                                         *lfsck_dto2fid(lfsck->li_obj_dir);
4324                         }
4325                 } else {
4326                         fid_zero(&pos.lp_dir_parent);
4327                         pos.lp_dir_cookie = 0;
4328                 }
4329                 spin_unlock(&lfsck->li_lock);
4330                 lfsck_pos_dump(m, &pos, "current_position");
4331         } else if (ns->ln_status == LS_SCANNING_PHASE2) {
4332                 cfs_duration_t duration = cfs_time_current() -
4333                                           lfsck->li_time_last_checkpoint;
4334                 __u64 checked = ns->ln_objs_checked_phase2 +
4335                                 com->lc_new_checked;
4336                 __u64 speed1 = ns->ln_items_checked;
4337                 __u64 speed2 = checked;
4338                 __u64 new_checked = com->lc_new_checked *
4339                                     msecs_to_jiffies(MSEC_PER_SEC);
4340                 __u32 rtime = ns->ln_run_time_phase2 +
4341                               cfs_duration_sec(duration + HALF_SEC);
4342
4343                 if (duration != 0)
4344                         do_div(new_checked, duration);
4345                 if (ns->ln_run_time_phase1 != 0)
4346                         do_div(speed1, ns->ln_run_time_phase1);
4347                 if (rtime != 0)
4348                         do_div(speed2, rtime);
4349                 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4350                                                 checked,
4351                                                 ns->ln_run_time_phase1, rtime);
4352
4353                 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4354                               "average_speed_phase2: "LPU64" objs/sec\n"
4355                               "real_time_speed_phase1: N/A\n"
4356                               "real_time_speed_phase2: "LPU64" objs/sec\n"
4357                               "current_position: "DFID"\n",
4358                               speed1,
4359                               speed2,
4360                               new_checked,
4361                               PFID(&ns->ln_fid_latest_scanned_phase2));
4362         } else {
4363                 __u64 speed1 = ns->ln_items_checked;
4364                 __u64 speed2 = ns->ln_objs_checked_phase2;
4365
4366                 if (ns->ln_run_time_phase1 != 0)
4367                         do_div(speed1, ns->ln_run_time_phase1);
4368                 if (ns->ln_run_time_phase2 != 0)
4369                         do_div(speed2, ns->ln_run_time_phase2);
4370                 lfsck_namespace_dump_statistics(m, ns, ns->ln_items_checked,
4371                                                 ns->ln_objs_checked_phase2,
4372                                                 ns->ln_run_time_phase1,
4373                                                 ns->ln_run_time_phase2);
4374
4375                 seq_printf(m, "average_speed_phase1: "LPU64" items/sec\n"
4376                               "average_speed_phase2: "LPU64" objs/sec\n"
4377                               "real_time_speed_phase1: N/A\n"
4378                               "real_time_speed_phase2: N/A\n"
4379                               "current_position: N/A\n",
4380                               speed1,
4381                               speed2);
4382         }
4383 out:
4384         up_read(&com->lc_sem);
4385         return 0;
4386 }
4387
4388 static int lfsck_namespace_double_scan(const struct lu_env *env,
4389                                        struct lfsck_component *com)
4390 {
4391         struct lfsck_namespace          *ns     = com->lc_file_ram;
4392         struct lfsck_assistant_data     *lad    = com->lc_data;
4393         struct lfsck_tgt_descs          *ltds   = &com->lc_lfsck->li_mdt_descs;
4394         struct lfsck_tgt_desc           *ltd;
4395         struct lfsck_tgt_desc           *next;
4396         int                              rc;
4397
4398         rc = lfsck_double_scan_generic(env, com, ns->ln_status);
4399         if (thread_is_stopped(&lad->lad_thread)) {
4400                 LASSERT(list_empty(&lad->lad_req_list));
4401                 LASSERT(list_empty(&lad->lad_mdt_phase1_list));
4402
4403                 spin_lock(&ltds->ltd_lock);
4404                 list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4405                                          ltd_namespace_phase_list) {
4406                         list_del_init(&ltd->ltd_namespace_phase_list);
4407                 }
4408                 spin_unlock(&ltds->ltd_lock);
4409         }
4410
4411         return rc;
4412 }
4413
4414 static void lfsck_namespace_data_release(const struct lu_env *env,
4415                                          struct lfsck_component *com)
4416 {
4417         struct lfsck_assistant_data     *lad    = com->lc_data;
4418         struct lfsck_tgt_descs          *ltds   = &com->lc_lfsck->li_mdt_descs;
4419         struct lfsck_tgt_desc           *ltd;
4420         struct lfsck_tgt_desc           *next;
4421
4422         LASSERT(lad != NULL);
4423         LASSERT(thread_is_init(&lad->lad_thread) ||
4424                 thread_is_stopped(&lad->lad_thread));
4425         LASSERT(list_empty(&lad->lad_req_list));
4426
4427         com->lc_data = NULL;
4428         lfsck_namespace_release_lmv(env, com);
4429
4430         spin_lock(&ltds->ltd_lock);
4431         list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4432                                  ltd_namespace_phase_list) {
4433                 list_del_init(&ltd->ltd_namespace_phase_list);
4434         }
4435         list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4436                                  ltd_namespace_phase_list) {
4437                 list_del_init(&ltd->ltd_namespace_phase_list);
4438         }
4439         list_for_each_entry_safe(ltd, next, &lad->lad_mdt_list,
4440                                  ltd_namespace_list) {
4441                 list_del_init(&ltd->ltd_namespace_list);
4442         }
4443         spin_unlock(&ltds->ltd_lock);
4444
4445         if (likely(lad->lad_bitmap != NULL))
4446                 CFS_FREE_BITMAP(lad->lad_bitmap);
4447
4448         OBD_FREE_PTR(lad);
4449 }
4450
4451 static void lfsck_namespace_quit(const struct lu_env *env,
4452                                  struct lfsck_component *com)
4453 {
4454         struct lfsck_assistant_data     *lad    = com->lc_data;
4455         struct lfsck_tgt_descs          *ltds   = &com->lc_lfsck->li_mdt_descs;
4456         struct lfsck_tgt_desc           *ltd;
4457         struct lfsck_tgt_desc           *next;
4458
4459         LASSERT(lad != NULL);
4460
4461         lfsck_quit_generic(env, com);
4462
4463         LASSERT(thread_is_init(&lad->lad_thread) ||
4464                 thread_is_stopped(&lad->lad_thread));
4465         LASSERT(list_empty(&lad->lad_req_list));
4466
4467         lfsck_namespace_release_lmv(env, com);
4468
4469         spin_lock(&ltds->ltd_lock);
4470         list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase1_list,
4471                                  ltd_namespace_phase_list) {
4472                 list_del_init(&ltd->ltd_namespace_phase_list);
4473         }
4474         list_for_each_entry_safe(ltd, next, &lad->lad_mdt_phase2_list,
4475                                  ltd_namespace_phase_list) {
4476                 list_del_init(&ltd->ltd_namespace_phase_list);
4477         }
4478         spin_unlock(&ltds->ltd_lock);
4479 }
4480
4481 static int lfsck_namespace_in_notify(const struct lu_env *env,
4482                                      struct lfsck_component *com,
4483                                      struct lfsck_request *lr,
4484                                      struct thandle *th)
4485 {
4486         struct lfsck_instance           *lfsck = com->lc_lfsck;
4487         struct lfsck_namespace          *ns    = com->lc_file_ram;
4488         struct lfsck_assistant_data     *lad   = com->lc_data;
4489         struct lfsck_tgt_descs          *ltds  = &lfsck->li_mdt_descs;
4490         struct lfsck_tgt_desc           *ltd;
4491         int                              rc;
4492         bool                             fail  = false;
4493         ENTRY;
4494
4495         switch (lr->lr_event) {
4496         case LE_SKIP_NLINK_DECLARE: {
4497                 struct dt_object        *obj;
4498                 struct lu_fid           *key   = &lfsck_env_info(env)->lti_fid3;
4499                 int                      idx;
4500                 __u8                     flags = 0;
4501
4502                 LASSERT(th != NULL);
4503
4504                 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4505                 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4506                 fid_cpu_to_be(key, &lr->lr_fid);
4507                 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4508                 rc = dt_declare_delete(env, obj,
4509                                        (const struct dt_key *)key, th);
4510                 if (rc == 0)
4511                         rc = dt_declare_insert(env, obj,
4512                                                (const struct dt_rec *)&flags,
4513                                                (const struct dt_key *)key, th);
4514                 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4515
4516                 RETURN(rc);
4517         }
4518         case LE_SKIP_NLINK: {
4519                 struct dt_object        *obj;
4520                 struct lu_fid           *key   = &lfsck_env_info(env)->lti_fid3;
4521                 int                      idx;
4522                 __u8                     flags = 0;
4523                 bool                     exist = false;
4524                 ENTRY;
4525
4526                 LASSERT(th != NULL);
4527
4528                 idx = lfsck_sub_trace_file_fid2idx(&lr->lr_fid);
4529                 obj = com->lc_sub_trace_objs[idx].lsto_obj;
4530                 fid_cpu_to_be(key, &lr->lr_fid);
4531                 mutex_lock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4532                 rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
4533                                (const struct dt_key *)key, BYPASS_CAPA);
4534                 if (rc == 0) {
4535                         if (flags & LNTF_SKIP_NLINK) {
4536                                 mutex_unlock(
4537                                 &com->lc_sub_trace_objs[idx].lsto_mutex);
4538
4539                                 RETURN(0);
4540                         }
4541
4542                         exist = true;
4543                 } else if (rc != -ENOENT) {
4544                         GOTO(log, rc);
4545                 }
4546
4547                 flags |= LNTF_SKIP_NLINK;
4548                 if (exist) {
4549                         rc = dt_delete(env, obj, (const struct dt_key *)key,
4550                                        th, BYPASS_CAPA);
4551                         if (rc != 0)
4552                                 GOTO(log, rc);
4553                 }
4554
4555                 rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
4556                                (const struct dt_key *)key, th, BYPASS_CAPA, 1);
4557
4558                 GOTO(log, rc);
4559
4560 log:
4561                 mutex_unlock(&com->lc_sub_trace_objs[idx].lsto_mutex);
4562                 CDEBUG(D_LFSCK, "%s: RPC service thread mark the "DFID
4563                        " to be skipped for namespace double scan: rc = %d\n",
4564                        lfsck_lfsck2name(com->lc_lfsck), PFID(&lr->lr_fid), rc);
4565
4566                 if (rc != 0)
4567                         /* If we cannot record this object in the LFSCK tracing,
4568                          * we have to mark the LFSC as LF_INCOMPLETE, then the
4569                          * LFSCK will skip nlink attribute verification for
4570                          * all objects. */
4571                         ns->ln_flags |= LF_INCOMPLETE;
4572
4573                 return 0;
4574         }
4575         case LE_SET_LMV_MASTER: {
4576                 struct dt_object        *obj;
4577
4578                 obj = lfsck_object_find_bottom(env, lfsck, &lr->lr_fid);
4579                 if (IS_ERR(obj))
4580                         RETURN(PTR_ERR(obj));
4581
4582                 rc = lfsck_namespace_notify_lmv_master_local(env, com, obj);
4583                 lfsck_object_put(env, obj);
4584
4585                 RETURN(rc > 0 ? 0 : rc);
4586         }
4587         case LE_SET_LMV_SLAVE: {
4588                 if (!(lr->lr_flags & LEF_RECHECK_NAME_HASH))
4589                         ns->ln_striped_shards_repaired++;
4590
4591                 rc = lfsck_namespace_trace_update(env, com, &lr->lr_fid,
4592                                                   LNTF_RECHECK_NAME_HASH, true);
4593
4594                 RETURN(rc > 0 ? 0 : rc);
4595         }
4596         case LE_PHASE1_DONE:
4597         case LE_PHASE2_DONE:
4598         case LE_PEER_EXIT:
4599                 break;
4600         default:
4601                 RETURN(-EINVAL);
4602         }
4603
4604         CDEBUG(D_LFSCK, "%s: namespace LFSCK handles notify %u from MDT %x, "
4605                "status %d, flags %x\n", lfsck_lfsck2name(lfsck), lr->lr_event,
4606                lr->lr_index, lr->lr_status, lr->lr_flags2);
4607
4608         spin_lock(&ltds->ltd_lock);
4609         ltd = LTD_TGT(ltds, lr->lr_index);
4610         if (ltd == NULL) {
4611                 spin_unlock(&ltds->ltd_lock);
4612
4613                 RETURN(-ENXIO);
4614         }
4615
4616         list_del_init(&ltd->ltd_namespace_phase_list);
4617         switch (lr->lr_event) {
4618         case LE_PHASE1_DONE:
4619                 if (lr->lr_status <= 0) {
4620                         ltd->ltd_namespace_done = 1;
4621                         list_del_init(&ltd->ltd_namespace_list);
4622                         CDEBUG(D_LFSCK, "%s: MDT %x failed/stopped at "
4623                                "phase1 for namespace LFSCK: rc = %d.\n",
4624                                lfsck_lfsck2name(lfsck),
4625                                ltd->ltd_index, lr->lr_status);
4626                         ns->ln_flags |= LF_INCOMPLETE;
4627                         fail = true;
4628                         break;
4629                 }
4630
4631                 if (lr->lr_flags2 & LF_INCOMPLETE)
4632                         ns->ln_flags |= LF_INCOMPLETE;
4633
4634                 if (list_empty(&ltd->ltd_namespace_list))
4635                         list_add_tail(&ltd->ltd_namespace_list,
4636                                       &lad->lad_mdt_list);
4637                 list_add_tail(&ltd->ltd_namespace_phase_list,
4638                               &lad->lad_mdt_phase2_list);
4639                 break;
4640         case LE_PHASE2_DONE:
4641                 ltd->ltd_namespace_done = 1;
4642                 list_del_init(&ltd->ltd_namespace_list);
4643                 break;
4644         case LE_PEER_EXIT:
4645                 fail = true;
4646                 ltd->ltd_namespace_done = 1;
4647                 list_del_init(&ltd->ltd_namespace_list);
4648                 if (!(lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT)) {
4649                         CDEBUG(D_LFSCK,
4650                                "%s: the peer MDT %x exit namespace LFSCK\n",
4651                                lfsck_lfsck2name(lfsck), ltd->ltd_index);
4652                         ns->ln_flags |= LF_INCOMPLETE;
4653                 }
4654                 break;
4655         default:
4656                 break;
4657         }
4658         spin_unlock(&ltds->ltd_lock);
4659
4660         if (fail && lfsck->li_bookmark_ram.lb_param & LPF_FAILOUT) {
4661                 struct lfsck_stop *stop = &lfsck_env_info(env)->lti_stop;
4662
4663                 memset(stop, 0, sizeof(*stop));
4664                 stop->ls_status = lr->lr_status;
4665                 stop->ls_flags = lr->lr_param & ~LPF_BROADCAST;
4666                 lfsck_stop(env, lfsck->li_bottom, stop);
4667         } else if (lfsck_phase2_next_ready(lad)) {
4668                 wake_up_all(&lad->lad_thread.t_ctl_waitq);
4669         }
4670
4671         RETURN(0);
4672 }
4673
4674 static int lfsck_namespace_query(const struct lu_env *env,
4675                                  struct lfsck_component *com)
4676 {
4677         struct lfsck_namespace *ns = com->lc_file_ram;
4678
4679         return ns->ln_status;
4680 }
4681
4682 static struct lfsck_operations lfsck_namespace_ops = {
4683         .lfsck_reset            = lfsck_namespace_reset,
4684         .lfsck_fail             = lfsck_namespace_fail,
4685         .lfsck_close_dir        = lfsck_namespace_close_dir,
4686         .lfsck_open_dir         = lfsck_namespace_open_dir,
4687         .lfsck_checkpoint       = lfsck_namespace_checkpoint,
4688         .lfsck_prep             = lfsck_namespace_prep,
4689         .lfsck_exec_oit         = lfsck_namespace_exec_oit,
4690         .lfsck_exec_dir         = lfsck_namespace_exec_dir,
4691         .lfsck_post             = lfsck_namespace_post,
4692         .lfsck_dump             = lfsck_namespace_dump,
4693         .lfsck_double_scan      = lfsck_namespace_double_scan,
4694         .lfsck_data_release     = lfsck_namespace_data_release,
4695         .lfsck_quit             = lfsck_namespace_quit,
4696         .lfsck_in_notify        = lfsck_namespace_in_notify,
4697         .lfsck_query            = lfsck_namespace_query,
4698 };
4699
4700 /**
4701  * Repair dangling name entry.
4702  *
4703  * For the name entry with dangling reference, we need to repare the
4704  * inconsistency according to the LFSCK sponsor's requirement:
4705  *
4706  * 1) Keep the inconsistency there and report the inconsistency case,
4707  *    then give the chance to the application to find related issues,
4708  *    and the users can make the decision about how to handle it with
4709  *    more human knownledge. (by default)
4710  *
4711  * 2) Re-create the missing MDT-object with the FID information.
4712  *
4713  * \param[in] env       pointer to the thread context
4714  * \param[in] com       pointer to the lfsck component
4715  * \param[in] parent    pointer to the dir object that contains the dangling
4716  *                      name entry
4717  * \param[in] child     pointer to the object corresponding to the dangling
4718  *                      name entry
4719  * \param[in] lnr       pointer to the namespace request that contains the
4720  *                      name's name, parent object, parent's LMV, and ect.
4721  *
4722  * \retval              positive number if no need to repair
4723  * \retval              zero for repaired successfully
4724  * \retval              negative error number on failure
4725  */
4726 int lfsck_namespace_repair_dangling(const struct lu_env *env,
4727                                     struct lfsck_component *com,
4728                                     struct dt_object *parent,
4729                                     struct dt_object *child,
4730                                     struct lfsck_namespace_req *lnr)
4731 {
4732         struct lfsck_thread_info        *info   = lfsck_env_info(env);
4733         struct lu_attr                  *la     = &info->lti_la;
4734         struct dt_allocation_hint       *hint   = &info->lti_hint;
4735         struct dt_object_format         *dof    = &info->lti_dof;
4736         struct dt_insert_rec            *rec    = &info->lti_dt_rec;
4737         struct lmv_mds_md_v1            *lmv2   = &info->lti_lmv2;
4738         struct dt_object                *pobj   = NULL;
4739         struct dt_object                *cobj   = NULL;
4740         const struct lu_name            *cname;
4741         struct linkea_data               ldata  = { NULL };
4742         struct lustre_handle             lh     = { 0 };
4743         struct lu_buf                    linkea_buf;
4744         struct lu_buf                    lmv_buf;
4745         struct lfsck_instance           *lfsck  = com->lc_lfsck;
4746         struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
4747         struct dt_device                *dev    = lfsck->li_next;
4748         struct thandle                  *th     = NULL;
4749         int                              rc     = 0;
4750         __u16                            type   = lnr->lnr_type;
4751         bool                             create;
4752         ENTRY;
4753
4754         cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
4755         if (bk->lb_param & LPF_CREATE_MDTOBJ)
4756                 create = true;
4757         else
4758                 create = false;
4759
4760         if (!create || bk->lb_param & LPF_DRYRUN)
4761                 GOTO(log, rc = 0);
4762
4763         /* We may need to create the sub-objects of the @child via LOD,
4764          * so make the modification based on lfsck->li_next. */
4765
4766         pobj = lfsck_object_locate(dev, parent);
4767         if (IS_ERR(pobj))
4768                 GOTO(log, rc = PTR_ERR(pobj));
4769
4770         if (unlikely(!dt_try_as_dir(env, pobj)))
4771                 GOTO(log, rc = -ENOTDIR);
4772
4773         cobj = lfsck_object_locate(dev, child);
4774         if (IS_ERR(cobj))
4775                 GOTO(log, rc = PTR_ERR(cobj));
4776
4777         rc = linkea_data_new(&ldata, &info->lti_linkea_buf2);
4778         if (rc != 0)
4779                 GOTO(log, rc);
4780
4781         rc = linkea_add_buf(&ldata, cname, lfsck_dto2fid(pobj));
4782         if (rc != 0)
4783                 GOTO(log, rc);
4784
4785         rc = lfsck_ibits_lock(env, lfsck, pobj, &lh,
4786                               MDS_INODELOCK_UPDATE, LCK_EX);
4787         if (rc != 0)
4788                 GOTO(log, rc);
4789
4790         rc = lfsck_namespace_check_exist(env, pobj, cobj, lnr->lnr_name);
4791         if (rc != 0)
4792                 GOTO(log, rc);
4793
4794         /* Set the ctime as zero, then others can know it is created for
4795          * repairing dangling name entry by LFSCK. And if the LFSCK made
4796          * wrong decision and the real MDT-object has been found later,
4797          * then the LFSCK has chance to fix the incosistency properly. */
4798         memset(la, 0, sizeof(*la));
4799         la->la_mode = (type & S_IFMT) | 0600;
4800         la->la_valid = LA_TYPE | LA_MODE | LA_UID | LA_GID |
4801                         LA_ATIME | LA_MTIME | LA_CTIME;
4802
4803         cobj->do_ops->do_ah_init(env, hint, pobj, cobj,
4804                                  la->la_mode & S_IFMT);
4805
4806         memset(dof, 0, sizeof(*dof));
4807         dof->dof_type = dt_mode_to_dft(type);
4808         /* If the target is a regular file, then the LFSCK will only create
4809          * the MDT-object without stripes (dof->dof_reg.striped = 0). related
4810          * OST-objects will be created when write open. */
4811
4812         th = dt_trans_create(env, dev);
4813         if (IS_ERR(th))
4814                 GOTO(log, rc = PTR_ERR(th));
4815
4816         /* 1a. create child. */
4817         rc = dt_declare_create(env, cobj, la, hint, dof, th);
4818         if (rc != 0)
4819                 GOTO(stop, rc);
4820
4821         if (S_ISDIR(type)) {
4822                 if (unlikely(!dt_try_as_dir(env, cobj)))
4823                         GOTO(stop, rc = -ENOTDIR);
4824
4825                 /* 2a. insert dot into child dir */
4826                 rec->rec_type = S_IFDIR;
4827                 rec->rec_fid = lfsck_dto2fid(cobj);
4828                 rc = dt_declare_insert(env, cobj,
4829                                        (const struct dt_rec *)rec,
4830                                        (const struct dt_key *)dot, th);
4831                 if (rc != 0)
4832                         GOTO(stop, rc);
4833
4834                 /* 3a. insert dotdot into child dir */
4835                 rec->rec_fid = lfsck_dto2fid(pobj);
4836                 rc = dt_declare_insert(env, cobj,
4837                                        (const struct dt_rec *)rec,
4838                                        (const struct dt_key *)dotdot, th);
4839                 if (rc != 0)
4840                         GOTO(stop, rc);
4841
4842                 /* 4a. increase child nlink */
4843                 rc = dt_declare_ref_add(env, cobj, th);
4844                 if (rc != 0)
4845                         GOTO(stop, rc);
4846
4847                 /* 5a. generate slave LMV EA. */
4848                 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4849                         int idx;
4850
4851                         idx = lfsck_shard_name_to_index(env,
4852                                         lnr->lnr_name, lnr->lnr_namelen,
4853                                         type, lfsck_dto2fid(cobj));
4854                         if (unlikely(idx < 0))
4855                                 GOTO(stop, rc = idx);
4856
4857                         *lmv2 = lnr->lnr_lmv->ll_lmv;
4858                         lmv2->lmv_magic = LMV_MAGIC_STRIPE;
4859                         lmv2->lmv_master_mdt_index = idx;
4860
4861                         lfsck_lmv_header_cpu_to_le(lmv2, lmv2);
4862                         lfsck_buf_init(&lmv_buf, lmv2, sizeof(*lmv2));
4863                         rc = dt_declare_xattr_set(env, cobj, &lmv_buf,
4864                                                   XATTR_NAME_LMV, 0, th);
4865                         if (rc != 0)
4866                                 GOTO(stop, rc);
4867                 }
4868         }
4869
4870         /* 6a. insert linkEA for child */
4871         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
4872                        ldata.ld_leh->leh_len);
4873         rc = dt_declare_xattr_set(env, cobj, &linkea_buf,
4874                                   XATTR_NAME_LINK, 0, th);
4875         if (rc != 0)
4876                 GOTO(stop, rc);
4877
4878         rc = dt_trans_start_local(env, dev, th);
4879         if (rc != 0)
4880                 GOTO(stop, rc = (rc == -EEXIST ? 1 : rc));
4881
4882         dt_write_lock(env, cobj, 0);
4883         /* 1b. create child */
4884         rc = dt_create(env, cobj, la, hint, dof, th);
4885         if (rc != 0)
4886                 GOTO(unlock, rc = (rc == -EEXIST ? 1 : rc));
4887
4888         if (S_ISDIR(type)) {
4889                 /* 2b. insert dot into child dir */
4890                 rec->rec_type = S_IFDIR;
4891                 rec->rec_fid = lfsck_dto2fid(cobj);
4892                 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4893                                (const struct dt_key *)dot, th, BYPASS_CAPA, 1);
4894                 if (rc != 0)
4895                         GOTO(unlock, rc);
4896
4897                 /* 3b. insert dotdot into child dir */
4898                 rec->rec_fid = lfsck_dto2fid(pobj);
4899                 rc = dt_insert(env, cobj, (const struct dt_rec *)rec,
4900                                (const struct dt_key *)dotdot, th,
4901                                BYPASS_CAPA, 1);
4902                 if (rc != 0)
4903                         GOTO(unlock, rc);
4904
4905                 /* 4b. increase child nlink */
4906                 rc = dt_ref_add(env, cobj, th);
4907                 if (rc != 0)
4908                         GOTO(unlock, rc);
4909
4910                 /* 5b. generate slave LMV EA. */
4911                 if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
4912                         rc = dt_xattr_set(env, cobj, &lmv_buf, XATTR_NAME_LMV,
4913                                           0, th, BYPASS_CAPA);
4914                         if (rc != 0)
4915                                 GOTO(unlock, rc);
4916                 }
4917         }
4918
4919         /* 6b. insert linkEA for child. */
4920         rc = dt_xattr_set(env, cobj, &linkea_buf,
4921                           XATTR_NAME_LINK, 0, th, BYPASS_CAPA);
4922
4923         GOTO(unlock, rc);
4924
4925 unlock:
4926         dt_write_unlock(env, cobj);
4927
4928 stop:
4929         dt_trans_stop(env, dev, th);
4930
4931 log:
4932         lfsck_ibits_unlock(&lh, LCK_EX);
4933         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant found dangling "
4934                "reference for: parent "DFID", child "DFID", type %u, "
4935                "name %s. %s: rc = %d\n", lfsck_lfsck2name(lfsck),
4936                PFID(&lnr->lnr_lar.lar_fid), PFID(lfsck_dto2fid(child)),
4937                type, cname->ln_name,
4938                create ? "Create the lost OST-object as required" :
4939                         "Keep the MDT-object there by default", rc);
4940
4941         if (rc <= 0) {
4942                 struct lfsck_namespace *ns = com->lc_file_ram;
4943
4944                 ns->ln_flags |= LF_INCONSISTENT;
4945         }
4946
4947         return rc;
4948 }
4949
4950 static int lfsck_namespace_assistant_handler_p1(const struct lu_env *env,
4951                                                 struct lfsck_component *com,
4952                                                 struct lfsck_assistant_req *lar)
4953 {
4954         struct lfsck_thread_info   *info     = lfsck_env_info(env);
4955         struct lu_attr             *la       = &info->lti_la;
4956         struct lfsck_instance      *lfsck    = com->lc_lfsck;
4957         struct lfsck_bookmark      *bk       = &lfsck->li_bookmark_ram;
4958         struct lfsck_namespace     *ns       = com->lc_file_ram;
4959         struct linkea_data          ldata    = { NULL };
4960         const struct lu_name       *cname;
4961         struct thandle             *handle   = NULL;
4962         struct lfsck_namespace_req *lnr      =
4963                         container_of0(lar, struct lfsck_namespace_req, lnr_lar);
4964         struct dt_object           *dir      = NULL;
4965         struct dt_object           *obj      = NULL;
4966         const struct lu_fid        *pfid;
4967         struct dt_device           *dev      = NULL;
4968         struct lustre_handle        lh       = { 0 };
4969         bool                        repaired = false;
4970         bool                        dtlocked = false;
4971         bool                        remove;
4972         bool                        newdata;
4973         bool                        log      = false;
4974         bool                        bad_hash = false;
4975         int                         idx      = 0;
4976         int                         count    = 0;
4977         int                         rc       = 0;
4978         enum lfsck_namespace_inconsistency_type type = LNIT_NONE;
4979         ENTRY;
4980
4981         dir = lfsck_object_find_bottom(env, lfsck, &lar->lar_fid);
4982         if (IS_ERR(dir))
4983                 RETURN(PTR_ERR(dir));
4984
4985         if (unlikely(lfsck_is_dead_obj(dir)))
4986                 GOTO(put_dir, rc = 0);
4987
4988         if (unlikely(!dt_try_as_dir(env, dir)))
4989                 GOTO(put_dir, rc = -ENOTDIR);
4990
4991         pfid = lfsck_dto2fid(dir);
4992         la->la_nlink = 0;
4993         if (lnr->lnr_attr & LUDA_UPGRADE) {
4994                 ns->ln_flags |= LF_UPGRADE;
4995                 ns->ln_dirent_repaired++;
4996                 repaired = true;
4997         } else if (lnr->lnr_attr & LUDA_REPAIR) {
4998                 ns->ln_flags |= LF_INCONSISTENT;
4999                 ns->ln_dirent_repaired++;
5000                 repaired = true;
5001         }
5002
5003         if (unlikely(fid_is_zero(&lnr->lnr_fid))) {
5004                 if (strcmp(lnr->lnr_name, dotdot) != 0)
5005                         LBUG();
5006                 else
5007                         rc = lfsck_namespace_trace_update(env, com, pfid,
5008                                                 LNTF_CHECK_PARENT, true);
5009
5010                 GOTO(out, rc);
5011         }
5012
5013         if (unlikely(!fid_is_sane(&lnr->lnr_fid))) {
5014                 CDEBUG(D_LFSCK, "%s: dir scan find invalid FID "DFID
5015                        " for the name entry %.*s under "DFID"\n",
5016                        lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5017                        lnr->lnr_namelen, lnr->lnr_name, PFID(pfid));
5018
5019                 if (strcmp(lnr->lnr_name, dotdot) != 0)
5020                         /* invalid FID means bad name entry, remove it. */
5021                         type = LNIT_BAD_DIRENT;
5022                 else
5023                         /* If the parent FID is invalid, we cannot remove
5024                          * the ".." entry directly. */
5025                         rc = lfsck_namespace_trace_update(env, com, pfid,
5026                                                 LNTF_CHECK_PARENT, true);
5027
5028                 GOTO(out, rc);
5029         }
5030
5031         if (unlikely(lnr->lnr_dir_cookie == MDS_DIR_END_OFF)) {
5032                 rc = lfsck_namespace_striped_dir_rescan(env, com, dir, lnr);
5033
5034                 GOTO(put_dir, rc);
5035         }
5036
5037         if (lnr->lnr_name[0] == '.' &&
5038             (lnr->lnr_namelen == 1 || fid_seq_is_dot(fid_seq(&lnr->lnr_fid))))
5039                 GOTO(out, rc = 0);
5040
5041         if (lnr->lnr_lmv != NULL && lnr->lnr_lmv->ll_lmv_master) {
5042                 rc = lfsck_namespace_handle_striped_master(env, com, dir, lnr);
5043
5044                 GOTO(put_dir, rc);
5045         }
5046
5047         idx = lfsck_find_mdt_idx_by_fid(env, lfsck, &lnr->lnr_fid);
5048         if (idx < 0)
5049                 GOTO(out, rc = idx);
5050
5051         if (idx == lfsck_dev_idx(lfsck)) {
5052                 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0))
5053                         GOTO(out, rc = 0);
5054
5055                 dev = lfsck->li_bottom;
5056         } else {
5057                 struct lfsck_tgt_desc *ltd;
5058
5059                 /* Usually, some local filesystem consistency verification
5060                  * tools can guarantee the local namespace tree consistenct.
5061                  * So the LFSCK will only verify the remote directory. */
5062                 if (unlikely(strcmp(lnr->lnr_name, dotdot) == 0)) {
5063                         rc = lfsck_namespace_trace_update(env, com, pfid,
5064                                                 LNTF_CHECK_PARENT, true);
5065
5066                         GOTO(out, rc);
5067                 }
5068
5069                 ltd = LTD_TGT(&lfsck->li_mdt_descs, idx);
5070                 if (unlikely(ltd == NULL)) {
5071                         CDEBUG(D_LFSCK, "%s: cannot talk with MDT %x which "
5072                                "did not join the namespace LFSCK\n",
5073                                lfsck_lfsck2name(lfsck), idx);
5074                         lfsck_lad_set_bitmap(env, com, idx);
5075
5076                         GOTO(out, rc = -ENODEV);
5077                 }
5078
5079                 dev = ltd->ltd_tgt;
5080         }
5081
5082         obj = lfsck_object_find_by_dev(env, dev, &lnr->lnr_fid);
5083         if (IS_ERR(obj))
5084                 GOTO(out, rc = PTR_ERR(obj));
5085
5086         cname = lfsck_name_get_const(env, lnr->lnr_name, lnr->lnr_namelen);
5087         if (dt_object_exists(obj) == 0) {
5088
5089 dangling:
5090                 rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5091                 if (rc == 0) {
5092                         if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5093                                         lnr->lnr_name, lnr->lnr_namelen)) {
5094                                 type = LNIT_BAD_DIRENT;
5095
5096                                 GOTO(out, rc);
5097                         }
5098
5099                         type = LNIT_DANGLING;
5100                         rc = lfsck_namespace_repair_dangling(env, com, dir,
5101                                                              obj, lnr);
5102                         if (rc == 0)
5103                                 repaired = true;
5104                 }
5105
5106                 GOTO(out, rc);
5107         }
5108
5109         if (!(bk->lb_param & LPF_DRYRUN) && repaired) {
5110
5111 again:
5112                 rc = lfsck_ibits_lock(env, lfsck, obj, &lh,
5113                                       MDS_INODELOCK_UPDATE |
5114                                       MDS_INODELOCK_XATTR, LCK_EX);
5115                 if (rc != 0)
5116                         GOTO(out, rc);
5117
5118                 handle = dt_trans_create(env, dev);
5119                 if (IS_ERR(handle))
5120                         GOTO(out, rc = PTR_ERR(handle));
5121
5122                 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
5123                 if (rc != 0)
5124                         GOTO(stop, rc);
5125
5126                 rc = dt_trans_start_local(env, dev, handle);
5127                 if (rc != 0)
5128                         GOTO(stop, rc);
5129
5130                 dt_write_lock(env, obj, 0);
5131                 dtlocked = true;
5132         }
5133
5134         rc = lfsck_namespace_check_exist(env, dir, obj, lnr->lnr_name);
5135         if (rc != 0)
5136                 GOTO(stop, rc);
5137
5138         rc = lfsck_links_read(env, obj, &ldata);
5139         if (unlikely(rc == -ENOENT)) {
5140                 if (handle != NULL) {
5141                         dt_write_unlock(env, obj);
5142                         dtlocked = false;
5143
5144                         dt_trans_stop(env, dev, handle);
5145                         handle = NULL;
5146
5147                         lfsck_ibits_unlock(&lh, LCK_EX);
5148                 }
5149
5150                 /* It may happen when the remote object has been removed,
5151                  * but the local MDT is not aware of that. */
5152                 goto dangling;
5153         } else if (rc == 0) {
5154                 count = ldata.ld_leh->leh_reccount;
5155                 rc = linkea_links_find(&ldata, cname, pfid);
5156                 if ((rc == 0) &&
5157                     (count == 1 || !S_ISDIR(lfsck_object_type(obj)))) {
5158                         if ((lfsck_object_type(obj) & S_IFMT) !=
5159                             lnr->lnr_type) {
5160                                 ns->ln_flags |= LF_INCONSISTENT;
5161                                 type = LNIT_BAD_TYPE;
5162                         }
5163
5164                         goto stop;
5165                 }
5166
5167                 ns->ln_flags |= LF_INCONSISTENT;
5168
5169                 /* If the name entry hash does not match the slave striped
5170                  * directory, and the name entry does not match also, then
5171                  * it is quite possible that name entry is corrupted. */
5172                 if (!lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5173                                         lnr->lnr_name, lnr->lnr_namelen)) {
5174                         type = LNIT_BAD_DIRENT;
5175
5176                         GOTO(stop, rc = 0);
5177                 }
5178
5179                 /* If the file type stored in the name entry does not match
5180                  * the file type claimed by the object, and the object does
5181                  * not recognize the name entry, then it is quite possible
5182                  * that the name entry is corrupted. */
5183                 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type) {
5184                         type = LNIT_BAD_DIRENT;
5185
5186                         GOTO(stop, rc = 0);
5187                 }
5188
5189                 /* For sub-dir object, we cannot make sure whether the sub-dir
5190                  * back references the parent via ".." name entry correctly or
5191                  * not in the LFSCK first-stage scanning. It may be that the
5192                  * (remote) sub-dir ".." name entry has no parent FID after
5193                  * file-level backup/restore and its linkEA may be wrong.
5194                  * So under such case, we should replace the linkEA according
5195                  * to current name entry. But this needs to be done during the
5196                  * LFSCK second-stage scanning. The LFSCK will record the name
5197                  * entry for further possible using. */
5198                 remove = false;
5199                 newdata = false;
5200                 goto nodata;
5201         } else if (unlikely(rc == -EINVAL)) {
5202                 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5203                         type = LNIT_BAD_TYPE;
5204
5205                 count = 1;
5206                 ns->ln_flags |= LF_INCONSISTENT;
5207                 /* The magic crashed, we are not sure whether there are more
5208                  * corrupt data in the linkea, so remove all linkea entries. */
5209                 remove = true;
5210                 newdata = true;
5211                 goto nodata;
5212         } else if (rc == -ENODATA) {
5213                 if ((lfsck_object_type(obj) & S_IFMT) != lnr->lnr_type)
5214                         type = LNIT_BAD_TYPE;
5215
5216                 count = 1;
5217                 ns->ln_flags |= LF_UPGRADE;
5218                 remove = false;
5219                 newdata = true;
5220
5221 nodata:
5222                 if (bk->lb_param & LPF_DRYRUN) {
5223                         ns->ln_linkea_repaired++;
5224                         repaired = true;
5225                         log = true;
5226                         goto stop;
5227                 }
5228
5229                 if (!lustre_handle_is_used(&lh))
5230                         goto again;
5231
5232                 if (remove) {
5233                         LASSERT(newdata);
5234
5235                         rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
5236                                           BYPASS_CAPA);
5237                         if (rc != 0)
5238                                 GOTO(stop, rc);
5239                 }
5240
5241                 if (newdata) {
5242                         rc = linkea_data_new(&ldata,
5243                                         &lfsck_env_info(env)->lti_linkea_buf);
5244                         if (rc != 0)
5245                                 GOTO(stop, rc);
5246                 }
5247
5248                 rc = linkea_add_buf(&ldata, cname, pfid);
5249                 if (rc != 0)
5250                         GOTO(stop, rc);
5251
5252                 rc = lfsck_links_write(env, obj, &ldata, handle);
5253                 if (unlikely(rc == -ENOSPC) &&
5254                     S_ISREG(lfsck_object_type(obj)) && !dt_object_remote(obj)) {
5255                         if (handle != NULL) {
5256                                 LASSERT(dt_write_locked(env, obj));
5257
5258                                 dt_write_unlock(env, obj);
5259                                 dtlocked = false;
5260
5261                                 dt_trans_stop(env, dev, handle);
5262                                 handle = NULL;
5263
5264                                 lfsck_ibits_unlock(&lh, LCK_EX);
5265                         }
5266
5267                         rc = lfsck_namespace_trace_update(env, com,
5268                                         &lnr->lnr_fid, LNTF_SKIP_NLINK, true);
5269                         if (rc != 0)
5270                                 /* If we cannot record this object in the
5271                                  * LFSCK tracing, we have to mark the LFSCK
5272                                  * as LF_INCOMPLETE, then the LFSCK will
5273                                  * skip nlink attribute verification for
5274                                  * all objects. */
5275                                 ns->ln_flags |= LF_INCOMPLETE;
5276
5277                         GOTO(out, rc = 0);
5278                 }
5279
5280                 if (rc != 0)
5281                         GOTO(stop, rc);
5282
5283                 count = ldata.ld_leh->leh_reccount;
5284                 if (!S_ISDIR(lfsck_object_type(obj)) ||
5285                     !dt_object_remote(obj)) {
5286                         ns->ln_linkea_repaired++;
5287                         repaired = true;
5288                         log = true;
5289                 }
5290         } else {
5291                 GOTO(stop, rc);
5292         }
5293
5294 stop:
5295         if (dtlocked)
5296                 dt_write_unlock(env, obj);
5297
5298         if (handle != NULL && !IS_ERR(handle))
5299                 dt_trans_stop(env, dev, handle);
5300
5301 out:
5302         lfsck_ibits_unlock(&lh, LCK_EX);
5303
5304         if (!name_is_dot_or_dotdot(lnr->lnr_name, lnr->lnr_namelen) &&
5305             !lfsck_is_valid_slave_name_entry(env, lnr->lnr_lmv,
5306                                              lnr->lnr_name, lnr->lnr_namelen) &&
5307             type != LNIT_BAD_DIRENT) {
5308                 ns->ln_flags |= LF_INCONSISTENT;
5309
5310                 log = false;
5311                 rc = lfsck_namespace_repair_bad_name_hash(env, com, dir,
5312                                                 lnr->lnr_lmv, lnr->lnr_name);
5313                 if (rc >= 0)
5314                         bad_hash = true;
5315         }
5316
5317         if (rc >= 0) {
5318                 switch (type) {
5319                 case LNIT_BAD_TYPE:
5320                         log = false;
5321                         rc = lfsck_namespace_repair_dirent(env, com, dir,
5322                                         obj, lnr->lnr_name, lnr->lnr_name,
5323                                         lnr->lnr_type, true, false);
5324                         if (rc > 0)
5325                                 repaired = true;
5326                         break;
5327                 case LNIT_BAD_DIRENT:
5328                         log = false;
5329                         /* XXX: This is a bad dirent, we do not know whether
5330                          *      the original name entry reference a regular
5331                          *      file or a directory, then keep the parent's
5332                          *      nlink count unchanged here. */
5333                         rc = lfsck_namespace_repair_dirent(env, com, dir,
5334                                         obj, lnr->lnr_name, lnr->lnr_name,
5335                                         lnr->lnr_type, false, false);
5336                         if (rc > 0)
5337                                 repaired = true;
5338                         break;
5339                 default:
5340                         break;
5341                 }
5342
5343                 if (count == 1 && S_ISREG(lfsck_object_type(obj)))
5344                         dt_attr_get(env, obj, la, BYPASS_CAPA);
5345         }
5346
5347         down_write(&com->lc_sem);
5348         if (rc < 0) {
5349                 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail to handle "
5350                        "the entry: "DFID", parent "DFID", name %.*s: rc = %d\n",
5351                        lfsck_lfsck2name(lfsck), PFID(&lnr->lnr_fid),
5352                        PFID(lfsck_dto2fid(dir)),
5353                        lnr->lnr_namelen, lnr->lnr_name, rc);
5354
5355                 lfsck_namespace_record_failure(env, lfsck, ns);
5356                 if ((rc == -ENOTCONN || rc == -ESHUTDOWN || rc == -EREMCHG ||
5357                      rc == -ETIMEDOUT || rc == -EHOSTDOWN ||
5358                      rc == -EHOSTUNREACH || rc == -EINPROGRESS) &&
5359                     dev != NULL && dev != lfsck->li_bottom)
5360                         lfsck_lad_set_bitmap(env, com, idx);
5361
5362                 if (!(bk->lb_param & LPF_FAILOUT))
5363                         rc = 0;
5364         } else {
5365                 if (log)
5366                         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant "
5367                                "repaired the entry: "DFID", parent "DFID
5368                                ", name %.*s\n", lfsck_lfsck2name(lfsck),
5369                                PFID(&lnr->lnr_fid),
5370                                PFID(lfsck_dto2fid(dir)),
5371                                lnr->lnr_namelen, lnr->lnr_name);
5372
5373                 if (repaired) {
5374                         ns->ln_items_repaired++;
5375
5376                         switch (type) {
5377                         case LNIT_DANGLING:
5378                                 ns->ln_dangling_repaired++;
5379                                 break;
5380                         case LNIT_BAD_TYPE:
5381                                 ns->ln_bad_type_repaired++;
5382                                 break;
5383                         case LNIT_BAD_DIRENT:
5384                                 ns->ln_dirent_repaired++;
5385                                 break;
5386                         default:
5387                                 break;
5388                         }
5389
5390                         if (bk->lb_param & LPF_DRYRUN &&
5391                             lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5392                                 lfsck_pos_fill(env, lfsck,
5393                                                &ns->ln_pos_first_inconsistent,
5394                                                false);
5395                 }
5396
5397                 if (bad_hash) {
5398                         ns->ln_name_hash_repaired++;
5399
5400                         /* Not count repeatedly. */
5401                         if (!repaired)
5402                                 ns->ln_items_repaired++;
5403
5404                         if (bk->lb_param & LPF_DRYRUN &&
5405                             lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
5406                                 lfsck_pos_fill(env, lfsck,
5407                                                &ns->ln_pos_first_inconsistent,
5408                                                false);
5409                 }
5410
5411                 rc = 0;
5412         }
5413
5414         if (count > 1 || la->la_nlink > 1)
5415                 ns->ln_mul_linked_checked++;
5416
5417         up_write(&com->lc_sem);
5418
5419         if (obj != NULL && !IS_ERR(obj))
5420                 lfsck_object_put(env, obj);
5421
5422 put_dir:
5423         lfsck_object_put(env, dir);
5424
5425         return rc;
5426 }
5427
5428 /**
5429  * Handle one orphan under the backend /lost+found directory
5430  *
5431  * Insert the orphan FID into the namespace LFSCK trace file for further
5432  * processing (via the subsequent namespace LFSCK second-stage scanning).
5433  * At the same time, remove the orphan name entry from backend /lost+found
5434  * directory. There is an interval between the orphan name entry removed
5435  * from the backend /lost+found directory and the orphan FID in the LFSCK
5436  * trace file handled. In such interval, the LFSCK can be reset, then
5437  * all the FIDs recorded in the namespace LFSCK trace file will be dropped.
5438  * To guarantee that the orphans can be found when LFSCK run next time
5439  * without e2fsck again, when remove the orphan name entry, the LFSCK
5440  * will set the orphan's ctime attribute as 1. Since normal applications
5441  * cannot change the object's ctime attribute as 1. Then when LFSCK run
5442  * next time, it can record the object (that ctime is 1) in the namespace
5443  * LFSCK trace file during the first-stage scanning.
5444  *
5445  * \param[in] env       pointer to the thread context
5446  * \param[in] com       pointer to the lfsck component
5447  * \param[in] parent    pointer to the object for the backend /lost+found
5448  * \param[in] ent       pointer to the name entry for the target under the
5449  *                      backend /lost+found
5450  *
5451  * \retval              positive for repaired
5452  * \retval              0 if needs to repair nothing
5453  * \retval              negative error number on failure
5454  */
5455 static int lfsck_namespace_scan_local_lpf_one(const struct lu_env *env,
5456                                               struct lfsck_component *com,
5457                                               struct dt_object *parent,
5458                                               struct lu_dirent *ent)
5459 {
5460         struct lfsck_thread_info        *info   = lfsck_env_info(env);
5461         struct lu_fid                   *key    = &info->lti_fid;
5462         struct lu_attr                  *la     = &info->lti_la;
5463         struct lfsck_instance           *lfsck  = com->lc_lfsck;
5464         struct dt_object                *obj;
5465         struct dt_device                *dev    = lfsck->li_bottom;
5466         struct dt_object                *child  = NULL;
5467         struct thandle                  *th     = NULL;
5468         int                              idx;
5469         int                              rc     = 0;
5470         __u8                             flags  = 0;
5471         bool                             exist  = false;
5472         ENTRY;
5473
5474         child = lfsck_object_find_by_dev(env, dev, &ent->lde_fid);
5475         if (IS_ERR(child))
5476                 RETURN(PTR_ERR(child));
5477
5478         LASSERT(dt_object_exists(child));
5479         LASSERT(!dt_object_remote(child));
5480
5481         idx = lfsck_sub_trace_file_fid2idx(&ent->lde_fid);
5482         obj = com->lc_sub_trace_objs[idx].lsto_obj;
5483         fid_cpu_to_be(key, &ent->lde_fid);
5484         rc = dt_lookup(env, obj, (struct dt_rec *)&flags,
5485                        (const struct dt_key *)key, BYPASS_CAPA);
5486         if (rc == 0) {
5487                 exist = true;
5488                 flags |= LNTF_CHECK_ORPHAN;
5489         } else if (rc == -ENOENT) {
5490                 flags = LNTF_CHECK_ORPHAN;
5491         } else {
5492                 GOTO(out, rc);
5493         }
5494
5495         th = dt_trans_create(env, dev);
5496         if (IS_ERR(th))
5497                 GOTO(out, rc = PTR_ERR(th));
5498
5499         /* a1. remove name entry from backend /lost+found */
5500         rc = dt_declare_delete(env, parent,
5501                                (const struct dt_key *)ent->lde_name, th);
5502         if (rc != 0)
5503                 GOTO(stop, rc);
5504
5505         if (S_ISDIR(lfsck_object_type(child))) {
5506                 /* a2. decrease parent's nlink */
5507                 rc = dt_declare_ref_del(env, parent, th);
5508                 if (rc != 0)
5509                         GOTO(stop, rc);
5510         }
5511
5512         if (exist) {
5513                 /* a3. remove child's FID from the LFSCK trace file. */
5514                 rc = dt_declare_delete(env, obj,
5515                                        (const struct dt_key *)key, th);
5516                 if (rc != 0)
5517                         GOTO(stop, rc);
5518         } else {
5519                 /* a4. set child's ctime as 1 */
5520                 memset(la, 0, sizeof(*la));
5521                 la->la_ctime = 1;
5522                 la->la_valid = LA_CTIME;
5523                 rc = dt_declare_attr_set(env, child, la, th);
5524                 if (rc != 0)
5525                         GOTO(stop, rc);
5526         }
5527
5528         /* a5. insert child's FID into the LFSCK trace file. */
5529         rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
5530                                (const struct dt_key *)key, th);
5531         if (rc != 0)
5532                 GOTO(stop, rc);
5533
5534         rc = dt_trans_start_local(env, dev, th);
5535         if (rc != 0)
5536                 GOTO(stop, rc);
5537
5538         /* b1. remove name entry from backend /lost+found */
5539         rc = dt_delete(env, parent, (const struct dt_key *)ent->lde_name, th,
5540                        BYPASS_CAPA);
5541         if (rc != 0)
5542                 GOTO(stop, rc);
5543
5544         if (S_ISDIR(lfsck_object_type(child))) {
5545                 /* b2. decrease parent's nlink */
5546                 dt_write_lock(env, parent, 0);
5547                 rc = dt_ref_del(env, parent, th);
5548                 dt_write_unlock(env, parent);
5549                 if (rc != 0)
5550                         GOTO(stop, rc);
5551         }
5552
5553         if (exist) {
5554                 /* a3. remove child's FID from the LFSCK trace file. */
5555                 rc = dt_delete(env, obj, (const struct dt_key *)key, th,
5556                                BYPASS_CAPA);
5557                 if (rc != 0)
5558                         GOTO(stop, rc);
5559         } else {
5560                 /* b4. set child's ctime as 1 */
5561                 rc = dt_attr_set(env, child, la, th, BYPASS_CAPA);
5562                 if (rc != 0)
5563                         GOTO(stop, rc);
5564         }
5565
5566         /* b5. insert child's FID into the LFSCK trace file. */
5567         rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
5568                        (const struct dt_key *)key, th, BYPASS_CAPA, 1);
5569
5570         GOTO(stop, rc = (rc == 0 ? 1 : rc));
5571
5572 stop:
5573         dt_trans_stop(env, dev, th);
5574
5575 out:
5576         lfsck_object_put(env, child);
5577
5578         return rc;
5579 }
5580
5581 /**
5582  * Handle orphans under the backend /lost+found directory
5583  *
5584  * Some backend checker, such as e2fsck for ldiskfs may find some orphans
5585  * and put them under the backend /lost+found directory that is invisible
5586  * to client. The LFSCK will scan such directory, for the original client
5587  * visible orphans, add their fids into the namespace LFSCK trace file,
5588  * then the subsenquent namespace LFSCK second-stage scanning can handle
5589  * them as other objects to be double scanned: either move back to normal
5590  * namespace, or to the global visible orphan directory:
5591  * /ROOT/.lustre/lost+found/MDTxxxx/
5592  *
5593  * \param[in] env       pointer to the thread context
5594  * \param[in] com       pointer to the lfsck component
5595  */
5596 static void lfsck_namespace_scan_local_lpf(const struct lu_env *env,
5597                                            struct lfsck_component *com)
5598 {
5599         struct lfsck_thread_info        *info   = lfsck_env_info(env);
5600         struct lu_dirent                *ent    =
5601                                         (struct lu_dirent *)info->lti_key;
5602         struct lu_seq_range             *range  = &info->lti_range;
5603         struct lfsck_instance           *lfsck  = com->lc_lfsck;
5604         struct ptlrpc_thread            *thread = &lfsck->li_thread;
5605         struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
5606         struct lfsck_namespace          *ns     = com->lc_file_ram;
5607         struct dt_object                *parent;
5608         const struct dt_it_ops          *iops;
5609         struct dt_it                    *di;
5610         struct seq_server_site          *ss     = lfsck_dev_site(lfsck);
5611         __u64                            cookie;
5612         __u32                            idx    = lfsck_dev_idx(lfsck);
5613         int                              rc     = 0;
5614         __u16                            type;
5615         ENTRY;
5616
5617         parent = lfsck_object_find_by_dev(env, lfsck->li_bottom,
5618                                           &LU_BACKEND_LPF_FID);
5619         if (IS_ERR(parent)) {
5620                 CERROR("%s: fail to find backend /lost+found: rc = %ld\n",
5621                        lfsck_lfsck2name(lfsck), PTR_ERR(parent));
5622                 RETURN_EXIT;
5623         }
5624
5625         /* It is normal that the /lost+found does not exist for ZFS backend. */
5626         if (!dt_object_exists(parent))
5627                 GOTO(out, rc = 0);
5628
5629         if (unlikely(!dt_try_as_dir(env, parent)))
5630                 GOTO(out, rc = -ENOTDIR);
5631
5632         CDEBUG(D_LFSCK, "%s: start to scan backend /lost+found\n",
5633                lfsck_lfsck2name(lfsck));
5634
5635         com->lc_new_scanned = 0;
5636         iops = &parent->do_index_ops->dio_it;
5637         di = iops->init(env, parent, LUDA_64BITHASH | LUDA_TYPE, BYPASS_CAPA);
5638         if (IS_ERR(di))
5639                 GOTO(out, rc = PTR_ERR(di));
5640
5641         rc = iops->load(env, di, 0);
5642         if (rc == 0)
5643                 rc = iops->next(env, di);
5644         else if (rc > 0)
5645                 rc = 0;
5646
5647         while (rc == 0) {
5648                 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5649                     unlikely(!thread_is_running(thread)))
5650                         break;
5651
5652                 rc = iops->rec(env, di, (struct dt_rec *)ent,
5653                                LUDA_64BITHASH | LUDA_TYPE);
5654                 if (rc == 0)
5655                         rc = lfsck_unpack_ent(ent, &cookie, &type);
5656
5657                 if (unlikely(rc != 0)) {
5658                         CDEBUG(D_LFSCK, "%s: fail to iterate backend "
5659                                "/lost+found: rc = %d\n",
5660                                lfsck_lfsck2name(lfsck), rc);
5661
5662                         goto skip;
5663                 }
5664
5665                 /* skip dot and dotdot entries */
5666                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5667                         goto next;
5668
5669                 if (!fid_seq_in_fldb(fid_seq(&ent->lde_fid)))
5670                         goto skip;
5671
5672                 if (fid_is_norm(&ent->lde_fid)) {
5673                         fld_range_set_mdt(range);
5674                         rc = fld_local_lookup(env, ss->ss_server_fld,
5675                                               fid_seq(&ent->lde_fid), range);
5676                         if (rc != 0)
5677                                 goto skip;
5678                 } else if (idx != 0) {
5679                         /* If the returned FID is IGIF, then there are three
5680                          * possible cases:
5681                          *
5682                          * 1) The object is upgraded from old Lustre-1.8 with
5683                          *    IGIF assigned to such object.
5684                          * 2) The object is a backend local object and is
5685                          *    invisible to client.
5686                          * 3) The object lost its LMV EA, and since there is
5687                          *    no FID-in-dirent for the orphan in the backend
5688                          *    /lost+found directory, then the low layer will
5689                          *    return IGIF for such object.
5690                          *
5691                          * For MDTx (x != 0), it is either case 2) or case 3),
5692                          * but from the LFSCK view, they are indistinguishable.
5693                          * To be safe, the LFSCK will keep it there and report
5694                          * some message, then the adminstrator can handle that
5695                          * furtherly.
5696                          *
5697                          * For MDT0, it is more possible the case 1). The LFSCK
5698                          * will handle the orphan as an upgraded object. */
5699                         CDEBUG(D_LFSCK, "%s: the orphan %.*s with IGIF "DFID
5700                                "in the backend /lost+found on the MDT %04x, "
5701                                "to be safe, skip it.\n",
5702                                lfsck_lfsck2name(lfsck), ent->lde_namelen,
5703                                ent->lde_name, PFID(&ent->lde_fid), idx);
5704                         goto skip;
5705                 }
5706
5707                 rc = lfsck_namespace_scan_local_lpf_one(env, com, parent, ent);
5708
5709 skip:
5710                 down_write(&com->lc_sem);
5711                 com->lc_new_scanned++;
5712                 ns->ln_local_lpf_scanned++;
5713                 if (rc > 0)
5714                         ns->ln_local_lpf_moved++;
5715                 else if (rc == 0)
5716                         ns->ln_local_lpf_skipped++;
5717                 else
5718                         ns->ln_local_lpf_failed++;
5719                 up_write(&com->lc_sem);
5720
5721                 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5722                         break;
5723
5724 next:
5725                 lfsck_control_speed_by_self(com);
5726                 if (unlikely(!thread_is_running(thread))) {
5727                         rc = 0;
5728                         break;
5729                 }
5730
5731                 rc = iops->next(env, di);
5732         }
5733
5734         iops->put(env, di);
5735         iops->fini(env, di);
5736
5737         EXIT;
5738
5739 out:
5740         CDEBUG(D_LFSCK, "%s: stop to scan backend /lost+found: rc = %d\n",
5741                lfsck_lfsck2name(lfsck), rc);
5742
5743         lfsck_object_put(env, parent);
5744 }
5745
5746 /**
5747  * Rescan the striped directory after the master LMV EA reset.
5748  *
5749  * Sometimes, the master LMV EA of the striped directory maybe lost, so when
5750  * the namespace LFSCK engine scan the striped directory for the first time,
5751  * it will be regarded as a normal directory. As the LFSCK processing, some
5752  * other LFSCK instance on other MDT will find the shard of this striped dir,
5753  * and find that the master MDT-object of the striped directory lost its LMV
5754  * EA, then such remote LFSCK instance will regenerate the master LMV EA and
5755  * notify the LFSCK instance on this MDT to rescan the striped directory.
5756  *
5757  * \param[in] env       pointer to the thread context
5758  * \param[in] com       pointer to the lfsck component
5759  * \param[in] llu       the lfsck_lmv_unit that contains the striped directory
5760  *                      to be rescanned.
5761  *
5762  * \retval              positive number for success
5763  * \retval              0 for LFSCK stopped/paused
5764  * \retval              negative error number on failure
5765  */
5766 static int lfsck_namespace_rescan_striped_dir(const struct lu_env *env,
5767                                               struct lfsck_component *com,
5768                                               struct lfsck_lmv_unit *llu)
5769 {
5770         struct lfsck_thread_info        *info   = lfsck_env_info(env);
5771         struct lfsck_instance           *lfsck  = com->lc_lfsck;
5772         struct lfsck_assistant_data     *lad    = com->lc_data;
5773         struct dt_object                *dir;
5774         const struct dt_it_ops          *iops;
5775         struct dt_it                    *di;
5776         struct lu_dirent                *ent    =
5777                         (struct lu_dirent *)info->lti_key;
5778         struct lfsck_bookmark           *bk     = &lfsck->li_bookmark_ram;
5779         struct ptlrpc_thread            *thread = &lfsck->li_thread;
5780         struct lfsck_namespace_req      *lnr;
5781         struct lfsck_assistant_req      *lar;
5782         int                              rc;
5783         __u16                            type;
5784         ENTRY;
5785
5786         LASSERT(list_empty(&lad->lad_req_list));
5787
5788         lfsck->li_lmv = &llu->llu_lmv;
5789         lfsck->li_obj_dir = lfsck_object_get(llu->llu_obj);
5790         rc = lfsck_open_dir(env, lfsck, 0);
5791         if (rc != 0)
5792                 RETURN(rc);
5793
5794         dir = lfsck->li_obj_dir;
5795         di = lfsck->li_di_dir;
5796         iops = &dir->do_index_ops->dio_it;
5797         do {
5798                 rc = iops->rec(env, di, (struct dt_rec *)ent,
5799                                lfsck->li_args_dir);
5800                 if (rc == 0)
5801                         rc = lfsck_unpack_ent(ent, &lfsck->li_cookie_dir,
5802                                               &type);
5803
5804                 if (rc != 0) {
5805                         if (bk->lb_param & LPF_FAILOUT)
5806                                 GOTO(out, rc);
5807
5808                         goto next;
5809                 }
5810
5811                 if (name_is_dot_or_dotdot(ent->lde_name, ent->lde_namelen))
5812                         goto next;
5813
5814                 lnr = lfsck_namespace_assistant_req_init(lfsck, ent, type);
5815                 if (IS_ERR(lnr)) {
5816                         if (bk->lb_param & LPF_FAILOUT)
5817                                 GOTO(out, rc = PTR_ERR(lnr));
5818
5819                         goto next;
5820                 }
5821
5822                 lar = &lnr->lnr_lar;
5823                 rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5824                 lfsck_namespace_assistant_req_fini(env, lar);
5825                 if (rc != 0 && bk->lb_param & LPF_FAILOUT)
5826                         GOTO(out, rc);
5827
5828                 if (unlikely(!thread_is_running(thread)))
5829                         GOTO(out, rc = 0);
5830
5831 next:
5832                 rc = iops->next(env, di);
5833         } while (rc == 0);
5834
5835 out:
5836         lfsck_close_dir(env, lfsck, rc);
5837         if (rc <= 0)
5838                 RETURN(rc);
5839
5840         /* The close_dir() may insert a dummy lnr in the lad->lad_req_list. */
5841         if (list_empty(&lad->lad_req_list))
5842                 RETURN(1);
5843
5844         spin_lock(&lad->lad_lock);
5845         lar = list_entry(lad->lad_req_list.next, struct lfsck_assistant_req,
5846                           lar_list);
5847         list_del_init(&lar->lar_list);
5848         spin_unlock(&lad->lad_lock);
5849
5850         rc = lfsck_namespace_assistant_handler_p1(env, com, lar);
5851         lfsck_namespace_assistant_req_fini(env, lar);
5852
5853         RETURN(rc == 0 ? 1 : rc);
5854 }
5855
5856 static int
5857 lfsck_namespace_double_scan_one_trace_file(const struct lu_env *env,
5858                                            struct lfsck_component *com,
5859                                            struct dt_object *obj, bool first)
5860 {
5861         struct lfsck_instance   *lfsck  = com->lc_lfsck;
5862         struct ptlrpc_thread    *thread = &lfsck->li_thread;
5863         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
5864         struct lfsck_namespace  *ns     = com->lc_file_ram;
5865         const struct dt_it_ops  *iops   = &obj->do_index_ops->dio_it;
5866         struct dt_object        *target;
5867         struct dt_it            *di;
5868         struct dt_key           *key;
5869         struct lu_fid            fid;
5870         int                      rc;
5871         __u8                     flags  = 0;
5872         ENTRY;
5873
5874         di = iops->init(env, obj, 0, BYPASS_CAPA);
5875         if (IS_ERR(di))
5876                 RETURN(PTR_ERR(di));
5877
5878         if (first)
5879                 fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
5880         else
5881                 fid_zero(&fid);
5882         rc = iops->get(env, di, (const struct dt_key *)&fid);
5883         if (rc < 0)
5884                 GOTO(fini, rc);
5885
5886         if (first) {
5887                 /* The start one either has been processed or does not exist,
5888                  * skip it. */
5889                 rc = iops->next(env, di);
5890                 if (rc != 0)
5891                         GOTO(put, rc);
5892         }
5893
5894         do {
5895                 if (CFS_FAIL_TIMEOUT(OBD_FAIL_LFSCK_DELAY3, cfs_fail_val) &&
5896                     unlikely(!thread_is_running(thread)))
5897                         GOTO(put, rc = 0);
5898
5899                 key = iops->key(env, di);
5900                 if (IS_ERR(key)) {
5901                         rc = PTR_ERR(key);
5902                         if (rc == -ENOENT)
5903                                 GOTO(put, rc = 1);
5904
5905                         goto checkpoint;
5906                 }
5907
5908                 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
5909                 if (!fid_is_sane(&fid)) {
5910                         rc = 0;
5911                         goto checkpoint;
5912                 }
5913
5914                 target = lfsck_object_find_bottom(env, lfsck, &fid);
5915                 if (IS_ERR(target)) {
5916                         rc = PTR_ERR(target);
5917                         goto checkpoint;
5918                 }
5919
5920                 if (dt_object_exists(target)) {
5921                         rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
5922                         if (rc == 0) {
5923                                 rc = lfsck_namespace_double_scan_one(env, com,
5924                                                                 target, flags);
5925                                 if (rc == -ENOENT)
5926                                         rc = 0;
5927                         }
5928                 }
5929
5930                 lfsck_object_put(env, target);
5931
5932 checkpoint:
5933                 down_write(&com->lc_sem);
5934                 com->lc_new_checked++;
5935                 com->lc_new_scanned++;
5936                 if (rc >= 0 && fid_is_sane(&fid))
5937                         ns->ln_fid_latest_scanned_phase2 = fid;
5938                 if (rc > 0)
5939                         ns->ln_objs_repaired_phase2++;
5940                 else if (rc < 0)
5941                         ns->ln_objs_failed_phase2++;
5942                 up_write(&com->lc_sem);
5943
5944                 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
5945                         GOTO(put, rc);
5946
5947                 if (unlikely(cfs_time_beforeq(com->lc_time_next_checkpoint,
5948                                               cfs_time_current())) &&
5949                     com->lc_new_checked != 0) {
5950                         down_write(&com->lc_sem);
5951                         ns->ln_run_time_phase2 +=
5952                                 cfs_duration_sec(cfs_time_current() +
5953                                 HALF_SEC - com->lc_time_last_checkpoint);
5954                         ns->ln_time_last_checkpoint = cfs_time_current_sec();
5955                         ns->ln_objs_checked_phase2 += com->lc_new_checked;
5956                         com->lc_new_checked = 0;
5957                         rc = lfsck_namespace_store(env, com, false);
5958                         up_write(&com->lc_sem);
5959                         if (rc != 0)
5960                                 GOTO(put, rc);
5961
5962                         com->lc_time_last_checkpoint = cfs_time_current();
5963                         com->lc_time_next_checkpoint =
5964                                 com->lc_time_last_checkpoint +
5965                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
5966                 }
5967
5968                 lfsck_control_speed_by_self(com);
5969                 if (unlikely(!thread_is_running(thread)))
5970                         GOTO(put, rc = 0);
5971
5972                 rc = iops->next(env, di);
5973         } while (rc == 0);
5974
5975         GOTO(put, rc);
5976
5977 put:
5978         iops->put(env, di);
5979
5980 fini:
5981         iops->fini(env, di);
5982
5983         return rc;
5984 }
5985
5986 static int lfsck_namespace_assistant_handler_p2(const struct lu_env *env,
5987                                                 struct lfsck_component *com)
5988 {
5989         struct lfsck_instance   *lfsck  = com->lc_lfsck;
5990         struct lfsck_namespace  *ns     = com->lc_file_ram;
5991         int                      rc;
5992         int                      i;
5993         ENTRY;
5994
5995         while (!list_empty(&lfsck->li_list_lmv)) {
5996                 struct lfsck_lmv_unit *llu;
5997
5998                 spin_lock(&lfsck->li_lock);
5999                 llu = list_entry(lfsck->li_list_lmv.next,
6000                                  struct lfsck_lmv_unit, llu_link);
6001                 list_del_init(&llu->llu_link);
6002                 spin_unlock(&lfsck->li_lock);
6003
6004                 rc = lfsck_namespace_rescan_striped_dir(env, com, llu);
6005                 if (rc <= 0)
6006                         RETURN(rc);
6007         }
6008
6009         CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan start\n",
6010                lfsck_lfsck2name(lfsck));
6011
6012         lfsck_namespace_scan_local_lpf(env, com);
6013
6014         com->lc_new_checked = 0;
6015         com->lc_new_scanned = 0;
6016         com->lc_time_last_checkpoint = cfs_time_current();
6017         com->lc_time_next_checkpoint = com->lc_time_last_checkpoint +
6018                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
6019
6020         i = lfsck_sub_trace_file_fid2idx(&ns->ln_fid_latest_scanned_phase2);
6021         rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6022                                 com->lc_sub_trace_objs[i].lsto_obj, true);
6023         while (rc > 0 && ++i < LFSCK_STF_COUNT)
6024                 rc = lfsck_namespace_double_scan_one_trace_file(env, com,
6025                                 com->lc_sub_trace_objs[i].lsto_obj, false);
6026
6027         CDEBUG(D_LFSCK, "%s: namespace LFSCK phase2 scan stop at the No. %d "
6028                "trace file: rc = %d\n", lfsck_lfsck2name(lfsck), i, rc);
6029
6030         RETURN(rc);
6031 }
6032
6033 static void lfsck_namespace_assistant_fill_pos(const struct lu_env *env,
6034                                                struct lfsck_component *com,
6035                                                struct lfsck_position *pos)
6036 {
6037         struct lfsck_assistant_data     *lad = com->lc_data;
6038         struct lfsck_namespace_req      *lnr;
6039
6040         if (list_empty(&lad->lad_req_list))
6041                 return;
6042
6043         lnr = list_entry(lad->lad_req_list.next,
6044                          struct lfsck_namespace_req,
6045                          lnr_lar.lar_list);
6046         pos->lp_oit_cookie = lnr->lnr_oit_cookie;
6047         pos->lp_dir_cookie = lnr->lnr_dir_cookie - 1;
6048         pos->lp_dir_parent = lnr->lnr_lar.lar_fid;
6049 }
6050
6051 static int lfsck_namespace_double_scan_result(const struct lu_env *env,
6052                                               struct lfsck_component *com,
6053                                               int rc)
6054 {
6055         struct lfsck_instance   *lfsck  = com->lc_lfsck;
6056         struct lfsck_namespace  *ns     = com->lc_file_ram;
6057
6058         down_write(&com->lc_sem);
6059         ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
6060                                 HALF_SEC - lfsck->li_time_last_checkpoint);
6061         ns->ln_time_last_checkpoint = cfs_time_current_sec();
6062         ns->ln_objs_checked_phase2 += com->lc_new_checked;
6063         com->lc_new_checked = 0;
6064
6065         if (rc > 0) {
6066                 if (ns->ln_flags & LF_INCOMPLETE)
6067                         ns->ln_status = LS_PARTIAL;
6068                 else
6069                         ns->ln_status = LS_COMPLETED;
6070                 if (!(lfsck->li_bookmark_ram.lb_param & LPF_DRYRUN))
6071                         ns->ln_flags &= ~(LF_SCANNED_ONCE | LF_INCONSISTENT);
6072                 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
6073                 ns->ln_success_count++;
6074         } else if (rc == 0) {
6075                 if (lfsck->li_status != 0)
6076                         ns->ln_status = lfsck->li_status;
6077                 else
6078                         ns->ln_status = LS_STOPPED;
6079         } else {
6080                 ns->ln_status = LS_FAILED;
6081         }
6082
6083         rc = lfsck_namespace_store(env, com, false);
6084         up_write(&com->lc_sem);
6085
6086         return rc;
6087 }
6088
6089 static int
6090 lfsck_namespace_assistant_sync_failures_interpret(const struct lu_env *env,
6091                                                   struct ptlrpc_request *req,
6092                                                   void *args, int rc)
6093 {
6094         if (rc == 0) {
6095                 struct lfsck_async_interpret_args *laia = args;
6096                 struct lfsck_tgt_desc             *ltd  = laia->laia_ltd;
6097
6098                 ltd->ltd_synced_failures = 1;
6099         }
6100
6101         return 0;
6102 }
6103
6104 /**
6105  * Notify remote LFSCK instances about former failures.
6106  *
6107  * The local LFSCK instance has recorded which MDTs have ever failed to respond
6108  * some LFSCK verification requests (maybe because of network issues or the MDT
6109  * itself trouble). During the respond gap the MDT may missed some name entries
6110  * verification, then the MDT cannot know whether related MDT-objects have been
6111  * referenced by related name entries or not, then in the second-stage scanning,
6112  * these MDT-objects will be regarded as orphan, if the MDT-object contains bad
6113  * linkEA for back reference, then it will misguide the LFSCK to generate wrong
6114  * name entry for repairing the orphan.
6115  *
6116  * To avoid above trouble, when layout LFSCK finishes the first-stage scanning,
6117  * it will scan the bitmap for the ever failed MDTs, and notify them that they
6118  * have ever missed some name entries verification and should skip the handling
6119  * for orphan MDT-objects.
6120  *
6121  * \param[in] env       pointer to the thread context
6122  * \param[in] com       pointer to the lfsck component
6123  * \param[in] lr        pointer to the lfsck request
6124  */
6125 static void lfsck_namespace_assistant_sync_failures(const struct lu_env *env,
6126                                                     struct lfsck_component *com,
6127                                                     struct lfsck_request *lr)
6128 {
6129         struct lfsck_async_interpret_args *laia  =
6130                                 &lfsck_env_info(env)->lti_laia2;
6131         struct lfsck_assistant_data       *lad   = com->lc_data;
6132         struct lfsck_namespace            *ns    = com->lc_file_ram;
6133         struct lfsck_instance             *lfsck = com->lc_lfsck;
6134         struct lfsck_tgt_descs            *ltds  = &lfsck->li_mdt_descs;
6135         struct lfsck_tgt_desc             *ltd;
6136         struct ptlrpc_request_set         *set;
6137         __u32                              idx;
6138         int                                rc    = 0;
6139         ENTRY;
6140
6141         if (!lad->lad_incomplete)
6142                 RETURN_EXIT;
6143
6144         set = ptlrpc_prep_set();
6145         if (set == NULL)
6146                 GOTO(out, rc = -ENOMEM);
6147
6148         lr->lr_flags2 = ns->ln_flags | LF_INCOMPLETE;
6149         memset(laia, 0, sizeof(*laia));
6150         lad->lad_touch_gen++;
6151
6152         down_read(&ltds->ltd_rw_sem);
6153         cfs_foreach_bit(lad->lad_bitmap, idx) {
6154                 ltd = LTD_TGT(ltds, idx);
6155                 LASSERT(ltd != NULL);
6156
6157                 laia->laia_ltd = ltd;
6158                 rc = lfsck_async_request(env, ltd->ltd_exp, lr, set,
6159                         lfsck_namespace_assistant_sync_failures_interpret,
6160                         laia, LFSCK_NOTIFY);
6161                 if (rc != 0)
6162                         CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6163                                "to sync failure with MDT %x: rc = %d\n",
6164                                lfsck_lfsck2name(lfsck), ltd->ltd_index, rc);
6165         }
6166         up_read(&ltds->ltd_rw_sem);
6167
6168         rc = ptlrpc_set_wait(set);
6169         ptlrpc_set_destroy(set);
6170
6171         GOTO(out, rc);
6172
6173 out:
6174         if (rc != 0)
6175                 CDEBUG(D_LFSCK, "%s: namespace LFSCK assistant fail "
6176                        "to sync failure with MDTs, and related MDTs "
6177                        "may handle orphan improperly: rc = %d\n",
6178                        lfsck_lfsck2name(lfsck), rc);
6179
6180         EXIT;
6181 }
6182
6183 struct lfsck_assistant_operations lfsck_namespace_assistant_ops = {
6184         .la_handler_p1          = lfsck_namespace_assistant_handler_p1,
6185         .la_handler_p2          = lfsck_namespace_assistant_handler_p2,
6186         .la_fill_pos            = lfsck_namespace_assistant_fill_pos,
6187         .la_double_scan_result  = lfsck_namespace_double_scan_result,
6188         .la_req_fini            = lfsck_namespace_assistant_req_fini,
6189         .la_sync_failures       = lfsck_namespace_assistant_sync_failures,
6190 };
6191
6192 /**
6193  * Verify the specified linkEA entry for the given directory object.
6194  * If the object has no such linkEA entry or it has more other linkEA
6195  * entries, then re-generate the linkEA with the given information.
6196  *
6197  * \param[in] env       pointer to the thread context
6198  * \param[in] obj       pointer to the dt_object to be handled
6199  * \param[in] cname     the name for the child in the parent directory
6200  * \param[in] pfid      the parent directory's FID for the linkEA
6201  *
6202  * \retval              0 for success
6203  * \retval              negative error number on failure
6204  */
6205 int lfsck_verify_linkea(const struct lu_env *env, struct dt_object *obj,
6206                         const struct lu_name *cname, const struct lu_fid *pfid)
6207 {
6208         struct dt_device        *dev    = lfsck_obj2dev(obj);
6209         struct linkea_data       ldata  = { NULL };
6210         struct lu_buf            linkea_buf;
6211         struct thandle          *th;
6212         int                      rc;
6213         int                      fl     = LU_XATTR_CREATE;
6214         bool                     dirty  = false;
6215         ENTRY;
6216
6217         LASSERT(S_ISDIR(lfsck_object_type(obj)));
6218
6219         rc = lfsck_links_read(env, obj, &ldata);
6220         if (rc == -ENODATA) {
6221                 dirty = true;
6222         } else if (rc == 0) {
6223                 fl = LU_XATTR_REPLACE;
6224                 if (ldata.ld_leh->leh_reccount != 1) {
6225                         dirty = true;
6226                 } else {
6227                         rc = linkea_links_find(&ldata, cname, pfid);
6228                         if (rc != 0)
6229                                 dirty = true;
6230                 }
6231         }
6232
6233         if (!dirty)
6234                 RETURN(rc);
6235
6236         rc = linkea_data_new(&ldata, &lfsck_env_info(env)->lti_linkea_buf);
6237         if (rc != 0)
6238                 RETURN(rc);
6239
6240         rc = linkea_add_buf(&ldata, cname, pfid);
6241         if (rc != 0)
6242                 RETURN(rc);
6243
6244         lfsck_buf_init(&linkea_buf, ldata.ld_buf->lb_buf,
6245                        ldata.ld_leh->leh_len);
6246         th = dt_trans_create(env, dev);
6247         if (IS_ERR(th))
6248                 RETURN(PTR_ERR(th));
6249
6250         rc = dt_declare_xattr_set(env, obj, &linkea_buf,
6251                                   XATTR_NAME_LINK, fl, th);
6252         if (rc != 0)
6253                 GOTO(stop, rc);
6254
6255         rc = dt_trans_start_local(env, dev, th);
6256         if (rc != 0)
6257                 GOTO(stop, rc);
6258
6259         dt_write_lock(env, obj, 0);
6260         rc = dt_xattr_set(env, obj, &linkea_buf,
6261                           XATTR_NAME_LINK, fl, th, BYPASS_CAPA);
6262         dt_write_unlock(env, obj);
6263
6264         GOTO(stop, rc);
6265
6266 stop:
6267         dt_trans_stop(env, dev, th);
6268         return rc;
6269 }
6270
6271 /**
6272  * Get the name and parent directory's FID from the first linkEA entry.
6273  *
6274  * \param[in] env       pointer to the thread context
6275  * \param[in] obj       pointer to the object which get linkEA from
6276  * \param[out] name     pointer to the buffer to hold the name
6277  *                      in the first linkEA entry
6278  * \param[out] pfid     pointer to the buffer to hold the parent
6279  *                      directory's FID in the first linkEA entry
6280  *
6281  * \retval              0 for success
6282  * \retval              negative error number on failure
6283  */
6284 int lfsck_links_get_first(const struct lu_env *env, struct dt_object *obj,
6285                           char *name, struct lu_fid *pfid)
6286 {
6287         struct lu_name           *cname = &lfsck_env_info(env)->lti_name;
6288         struct linkea_data        ldata = { NULL };
6289         int                       rc;
6290
6291         rc = lfsck_links_read(env, obj, &ldata);
6292         if (rc != 0)
6293                 return rc;
6294
6295         linkea_first_entry(&ldata);
6296         if (ldata.ld_lee == NULL)
6297                 return -ENODATA;
6298
6299         linkea_entry_unpack(ldata.ld_lee, &ldata.ld_reclen, cname, pfid);
6300         /* To guarantee the 'name' is terminated with '0'. */
6301         memcpy(name, cname->ln_name, cname->ln_namelen);
6302         name[cname->ln_namelen] = 0;
6303
6304         return 0;
6305 }
6306
6307 /**
6308  * Update the object's name entry with the given FID.
6309  *
6310  * \param[in] env       pointer to the thread context
6311  * \param[in] lfsck     pointer to the lfsck instance
6312  * \param[in] dir       pointer to the directory that holds
6313  *                      the name entry
6314  * \param[in] name      the name for the entry to be updated
6315  * \param[in] fid       the new FID for the name entry referenced
6316  * \param[in] type      the type for the name entry to be updated
6317  *
6318  * \retval              0 for success
6319  * \retval              negative error number on failure
6320  */
6321 int lfsck_update_name_entry(const struct lu_env *env,
6322                             struct lfsck_instance *lfsck,
6323                             struct dt_object *dir, const char *name,
6324                             const struct lu_fid *fid, __u32 type)
6325 {
6326         struct dt_insert_rec    *rec    = &lfsck_env_info(env)->lti_dt_rec;
6327         struct dt_device        *dev    = lfsck_obj2dev(dir);
6328         struct lustre_handle     lh     = { 0 };
6329         struct thandle          *th;
6330         int                      rc;
6331         bool                     exists = true;
6332         ENTRY;
6333
6334         rc = lfsck_ibits_lock(env, lfsck, dir, &lh,
6335                               MDS_INODELOCK_UPDATE, LCK_EX);
6336         if (rc != 0)
6337                 RETURN(rc);
6338
6339         th = dt_trans_create(env, dev);
6340         if (IS_ERR(th))
6341                 GOTO(unlock, rc = PTR_ERR(th));
6342
6343         rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
6344         if (rc != 0)
6345                 GOTO(stop, rc);
6346
6347         rec->rec_type = type;
6348         rec->rec_fid = fid;
6349         rc = dt_declare_insert(env, dir, (const struct dt_rec *)rec,
6350                                (const struct dt_key *)name, th);
6351         if (rc != 0)
6352                 GOTO(stop, rc);
6353
6354         rc = dt_declare_ref_add(env, dir, th);
6355         if (rc != 0)
6356                 GOTO(stop, rc);
6357
6358         rc = dt_trans_start_local(env, dev, th);
6359         if (rc != 0)
6360                 GOTO(stop, rc);
6361
6362         rc = dt_delete(env, dir, (const struct dt_key *)name, th,
6363                        BYPASS_CAPA);
6364         if (rc == -ENOENT) {
6365                 exists = false;
6366                 rc = 0;
6367         }
6368
6369         if (rc != 0)
6370                 GOTO(stop, rc);
6371
6372         rc = dt_insert(env, dir, (const struct dt_rec *)rec,
6373                        (const struct dt_key *)name, th, BYPASS_CAPA, 1);
6374         if (rc == 0 && S_ISDIR(type) && !exists) {
6375                 dt_write_lock(env, dir, 0);
6376                 rc = dt_ref_add(env, dir, th);
6377                 dt_write_unlock(env, dir);
6378         }
6379
6380         GOTO(stop, rc);
6381
6382 stop:
6383         dt_trans_stop(env, dev, th);
6384
6385 unlock:
6386         lfsck_ibits_unlock(&lh, LCK_EX);
6387         CDEBUG(D_LFSCK, "%s: update name entry "DFID"/%s with the FID "DFID
6388                " and the type %o: rc = %d\n", lfsck_lfsck2name(lfsck),
6389                PFID(lfsck_dto2fid(dir)), name, PFID(fid), type, rc);
6390
6391         return rc;
6392 }
6393
6394 int lfsck_namespace_setup(const struct lu_env *env,
6395                           struct lfsck_instance *lfsck)
6396 {
6397         struct lfsck_component  *com;
6398         struct lfsck_namespace  *ns;
6399         struct dt_object        *root = NULL;
6400         struct dt_object        *obj;
6401         int                      i;
6402         int                      rc;
6403         ENTRY;
6404
6405         LASSERT(lfsck->li_master);
6406
6407         OBD_ALLOC_PTR(com);
6408         if (com == NULL)
6409                 RETURN(-ENOMEM);
6410
6411         INIT_LIST_HEAD(&com->lc_link);
6412         INIT_LIST_HEAD(&com->lc_link_dir);
6413         init_rwsem(&com->lc_sem);
6414         atomic_set(&com->lc_ref, 1);
6415         com->lc_lfsck = lfsck;
6416         com->lc_type = LFSCK_TYPE_NAMESPACE;
6417         com->lc_ops = &lfsck_namespace_ops;
6418         com->lc_data = lfsck_assistant_data_init(
6419                         &lfsck_namespace_assistant_ops,
6420                         LFSCK_NAMESPACE);
6421         if (com->lc_data == NULL)
6422                 GOTO(out, rc = -ENOMEM);
6423
6424         com->lc_file_size = sizeof(struct lfsck_namespace);
6425         OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
6426         if (com->lc_file_ram == NULL)
6427                 GOTO(out, rc = -ENOMEM);
6428
6429         OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
6430         if (com->lc_file_disk == NULL)
6431                 GOTO(out, rc = -ENOMEM);
6432
6433         for (i = 0; i < LFSCK_STF_COUNT; i++)
6434                 mutex_init(&com->lc_sub_trace_objs[i].lsto_mutex);
6435
6436         root = dt_locate(env, lfsck->li_bottom, &lfsck->li_local_root_fid);
6437         if (IS_ERR(root))
6438                 GOTO(out, rc = PTR_ERR(root));
6439
6440         if (unlikely(!dt_try_as_dir(env, root)))
6441                 GOTO(out, rc = -ENOTDIR);
6442
6443         obj = local_file_find_or_create(env, lfsck->li_los, root,
6444                                         LFSCK_NAMESPACE,
6445                                         S_IFREG | S_IRUGO | S_IWUSR);
6446         if (IS_ERR(obj))
6447                 GOTO(out, rc = PTR_ERR(obj));
6448
6449         com->lc_obj = obj;
6450         rc = lfsck_namespace_load(env, com);
6451         if (rc == -ENODATA)
6452                 rc = lfsck_namespace_init(env, com);
6453         else if (rc < 0)
6454                 rc = lfsck_namespace_reset(env, com, true);
6455         else
6456                 rc = lfsck_namespace_load_sub_trace_files(env, com, false);
6457         if (rc != 0)
6458                 GOTO(out, rc);
6459
6460         ns = com->lc_file_ram;
6461         switch (ns->ln_status) {
6462         case LS_INIT:
6463         case LS_COMPLETED:
6464         case LS_FAILED:
6465         case LS_STOPPED:
6466                 spin_lock(&lfsck->li_lock);
6467                 list_add_tail(&com->lc_link, &lfsck->li_list_idle);
6468                 spin_unlock(&lfsck->li_lock);
6469                 break;
6470         default:
6471                 CERROR("%s: unknown lfsck_namespace status %d\n",
6472                        lfsck_lfsck2name(lfsck), ns->ln_status);
6473                 /* fall through */
6474         case LS_SCANNING_PHASE1:
6475         case LS_SCANNING_PHASE2:
6476                 /* No need to store the status to disk right now.
6477                  * If the system crashed before the status stored,
6478                  * it will be loaded back when next time. */
6479                 ns->ln_status = LS_CRASHED;
6480                 /* fall through */
6481         case LS_PAUSED:
6482         case LS_CRASHED:
6483                 spin_lock(&lfsck->li_lock);
6484                 list_add_tail(&com->lc_link, &lfsck->li_list_scan);
6485                 list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
6486                 spin_unlock(&lfsck->li_lock);
6487                 break;
6488         }
6489
6490         GOTO(out, rc = 0);
6491
6492 out:
6493         if (root != NULL && !IS_ERR(root))
6494                 lfsck_object_put(env, root);
6495         if (rc != 0) {
6496                 lfsck_component_cleanup(env, com);
6497                 CERROR("%s: fail to init namespace LFSCK component: rc = %d\n",
6498                        lfsck_lfsck2name(lfsck), rc);
6499         }
6500         return rc;
6501 }