Whamcloud - gitweb
LU-2915 lfsck: LFSCK 1.5 technical debts (3)
[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) 2012, 2013, Intel Corporation.
24  */
25 /*
26  * lustre/lfsck/lfsck_namespace.c
27  *
28  * Author: Fan, Yong <fan.yong@intel.com>
29  */
30
31 #ifndef EXPORT_SYMTAB
32 # define EXPORT_SYMTAB
33 #endif
34 #define DEBUG_SUBSYSTEM S_LFSCK
35
36 #include <lustre/lustre_idl.h>
37 #include <lu_object.h>
38 #include <dt_object.h>
39 #include <lustre_linkea.h>
40 #include <lustre_fid.h>
41 #include <lustre_lib.h>
42 #include <lustre_net.h>
43 #include <lustre/lustre_user.h>
44
45 #include "lfsck_internal.h"
46
47 #define LFSCK_NAMESPACE_MAGIC   0xA0629D03
48
49 static const char lfsck_namespace_name[] = "lfsck_namespace";
50
51 static void lfsck_namespace_le_to_cpu(struct lfsck_namespace *des,
52                                       struct lfsck_namespace *src)
53 {
54         des->ln_magic = le32_to_cpu(src->ln_magic);
55         des->ln_status = le32_to_cpu(src->ln_status);
56         des->ln_flags = le32_to_cpu(src->ln_flags);
57         des->ln_success_count = le32_to_cpu(src->ln_success_count);
58         des->ln_run_time_phase1 = le32_to_cpu(src->ln_run_time_phase1);
59         des->ln_run_time_phase2 = le32_to_cpu(src->ln_run_time_phase2);
60         des->ln_time_last_complete = le64_to_cpu(src->ln_time_last_complete);
61         des->ln_time_latest_start = le64_to_cpu(src->ln_time_latest_start);
62         des->ln_time_last_checkpoint =
63                                 le64_to_cpu(src->ln_time_last_checkpoint);
64         lfsck_position_le_to_cpu(&des->ln_pos_latest_start,
65                                  &src->ln_pos_latest_start);
66         lfsck_position_le_to_cpu(&des->ln_pos_last_checkpoint,
67                                  &src->ln_pos_last_checkpoint);
68         lfsck_position_le_to_cpu(&des->ln_pos_first_inconsistent,
69                                  &src->ln_pos_first_inconsistent);
70         des->ln_items_checked = le64_to_cpu(src->ln_items_checked);
71         des->ln_items_repaired = le64_to_cpu(src->ln_items_repaired);
72         des->ln_items_failed = le64_to_cpu(src->ln_items_failed);
73         des->ln_dirs_checked = le64_to_cpu(src->ln_dirs_checked);
74         des->ln_mlinked_checked = le64_to_cpu(src->ln_mlinked_checked);
75         des->ln_objs_checked_phase2 = le64_to_cpu(src->ln_objs_checked_phase2);
76         des->ln_objs_repaired_phase2 =
77                                 le64_to_cpu(src->ln_objs_repaired_phase2);
78         des->ln_objs_failed_phase2 = le64_to_cpu(src->ln_objs_failed_phase2);
79         des->ln_objs_nlink_repaired = le64_to_cpu(src->ln_objs_nlink_repaired);
80         des->ln_objs_lost_found = le64_to_cpu(src->ln_objs_lost_found);
81         fid_le_to_cpu(&des->ln_fid_latest_scanned_phase2,
82                       &src->ln_fid_latest_scanned_phase2);
83 }
84
85 static void lfsck_namespace_cpu_to_le(struct lfsck_namespace *des,
86                                       struct lfsck_namespace *src)
87 {
88         des->ln_magic = cpu_to_le32(src->ln_magic);
89         des->ln_status = cpu_to_le32(src->ln_status);
90         des->ln_flags = cpu_to_le32(src->ln_flags);
91         des->ln_success_count = cpu_to_le32(src->ln_success_count);
92         des->ln_run_time_phase1 = cpu_to_le32(src->ln_run_time_phase1);
93         des->ln_run_time_phase2 = cpu_to_le32(src->ln_run_time_phase2);
94         des->ln_time_last_complete = cpu_to_le64(src->ln_time_last_complete);
95         des->ln_time_latest_start = cpu_to_le64(src->ln_time_latest_start);
96         des->ln_time_last_checkpoint =
97                                 cpu_to_le64(src->ln_time_last_checkpoint);
98         lfsck_position_cpu_to_le(&des->ln_pos_latest_start,
99                                  &src->ln_pos_latest_start);
100         lfsck_position_cpu_to_le(&des->ln_pos_last_checkpoint,
101                                  &src->ln_pos_last_checkpoint);
102         lfsck_position_cpu_to_le(&des->ln_pos_first_inconsistent,
103                                  &src->ln_pos_first_inconsistent);
104         des->ln_items_checked = cpu_to_le64(src->ln_items_checked);
105         des->ln_items_repaired = cpu_to_le64(src->ln_items_repaired);
106         des->ln_items_failed = cpu_to_le64(src->ln_items_failed);
107         des->ln_dirs_checked = cpu_to_le64(src->ln_dirs_checked);
108         des->ln_mlinked_checked = cpu_to_le64(src->ln_mlinked_checked);
109         des->ln_objs_checked_phase2 = cpu_to_le64(src->ln_objs_checked_phase2);
110         des->ln_objs_repaired_phase2 =
111                                 cpu_to_le64(src->ln_objs_repaired_phase2);
112         des->ln_objs_failed_phase2 = cpu_to_le64(src->ln_objs_failed_phase2);
113         des->ln_objs_nlink_repaired = cpu_to_le64(src->ln_objs_nlink_repaired);
114         des->ln_objs_lost_found = cpu_to_le64(src->ln_objs_lost_found);
115         fid_cpu_to_le(&des->ln_fid_latest_scanned_phase2,
116                       &src->ln_fid_latest_scanned_phase2);
117 }
118
119 /**
120  * \retval +ve: the lfsck_namespace is broken, the caller should reset it.
121  * \retval 0: succeed.
122  * \retval -ve: failed cases.
123  */
124 static int lfsck_namespace_load(const struct lu_env *env,
125                                 struct lfsck_component *com)
126 {
127         int len = com->lc_file_size;
128         int rc;
129
130         rc = dt_xattr_get(env, com->lc_obj,
131                           lfsck_buf_get(env, com->lc_file_disk, len),
132                           XATTR_NAME_LFSCK_NAMESPACE, BYPASS_CAPA);
133         if (rc == len) {
134                 struct lfsck_namespace *ns = com->lc_file_ram;
135
136                 lfsck_namespace_le_to_cpu(ns,
137                                 (struct lfsck_namespace *)com->lc_file_disk);
138                 if (ns->ln_magic != LFSCK_NAMESPACE_MAGIC) {
139                         CWARN("%.16s: invalid lfsck_namespace magic "
140                               "0x%x != 0x%x\n",
141                               lfsck_lfsck2name(com->lc_lfsck),
142                               ns->ln_magic, LFSCK_NAMESPACE_MAGIC);
143                         rc = 1;
144                 } else {
145                         rc = 0;
146                 }
147         } else if (rc != -ENODATA) {
148                 CERROR("%.16s: fail to load lfsck_namespace, expected = %d, "
149                        "rc = %d\n", lfsck_lfsck2name(com->lc_lfsck), len, rc);
150                 if (rc >= 0)
151                         rc = 1;
152         }
153         return rc;
154 }
155
156 static int lfsck_namespace_store(const struct lu_env *env,
157                                  struct lfsck_component *com, bool init)
158 {
159         struct dt_object        *obj    = com->lc_obj;
160         struct lfsck_instance   *lfsck  = com->lc_lfsck;
161         struct thandle          *handle;
162         int                      len    = com->lc_file_size;
163         int                      rc;
164         ENTRY;
165
166         lfsck_namespace_cpu_to_le((struct lfsck_namespace *)com->lc_file_disk,
167                                   (struct lfsck_namespace *)com->lc_file_ram);
168         handle = dt_trans_create(env, lfsck->li_bottom);
169         if (IS_ERR(handle)) {
170                 rc = PTR_ERR(handle);
171                 CERROR("%.16s: fail to create trans for storing "
172                        "lfsck_namespace: %d\n,", lfsck_lfsck2name(lfsck), rc);
173                 RETURN(rc);
174         }
175
176         rc = dt_declare_xattr_set(env, obj,
177                                   lfsck_buf_get(env, com->lc_file_disk, len),
178                                   XATTR_NAME_LFSCK_NAMESPACE, 0, handle);
179         if (rc != 0) {
180                 CERROR("%.16s: fail to declare trans for storing "
181                        "lfsck_namespace: %d\n,", lfsck_lfsck2name(lfsck), rc);
182                 GOTO(out, rc);
183         }
184
185         rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
186         if (rc != 0) {
187                 CERROR("%.16s: fail to start trans for storing "
188                        "lfsck_namespace: %d\n,", lfsck_lfsck2name(lfsck), rc);
189                 GOTO(out, rc);
190         }
191
192         rc = dt_xattr_set(env, obj,
193                           lfsck_buf_get(env, com->lc_file_disk, len),
194                           XATTR_NAME_LFSCK_NAMESPACE,
195                           init ? LU_XATTR_CREATE : LU_XATTR_REPLACE,
196                           handle, BYPASS_CAPA);
197         if (rc != 0)
198                 CERROR("%.16s: fail to store lfsck_namespace, len = %d, "
199                        "rc = %d\n", lfsck_lfsck2name(lfsck), len, rc);
200
201         GOTO(out, rc);
202
203 out:
204         dt_trans_stop(env, lfsck->li_bottom, handle);
205         return rc;
206 }
207
208 static int lfsck_namespace_init(const struct lu_env *env,
209                                 struct lfsck_component *com)
210 {
211         struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
212         int rc;
213
214         memset(ns, 0, sizeof(*ns));
215         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
216         ns->ln_status = LS_INIT;
217         down_write(&com->lc_sem);
218         rc = lfsck_namespace_store(env, com, true);
219         up_write(&com->lc_sem);
220         return rc;
221 }
222
223 static int lfsck_namespace_lookup(const struct lu_env *env,
224                                   struct lfsck_component *com,
225                                   const struct lu_fid *fid, __u8 *flags)
226 {
227         struct lu_fid *key = &lfsck_env_info(env)->lti_fid;
228         int            rc;
229
230         fid_cpu_to_be(key, fid);
231         rc = dt_lookup(env, com->lc_obj, (struct dt_rec *)flags,
232                        (const struct dt_key *)key, BYPASS_CAPA);
233         return rc;
234 }
235
236 static int lfsck_namespace_delete(const struct lu_env *env,
237                                   struct lfsck_component *com,
238                                   const struct lu_fid *fid)
239 {
240         struct lfsck_instance   *lfsck  = com->lc_lfsck;
241         struct lu_fid           *key    = &lfsck_env_info(env)->lti_fid;
242         struct thandle          *handle;
243         struct dt_object        *obj    = com->lc_obj;
244         int                      rc;
245         ENTRY;
246
247         handle = dt_trans_create(env, lfsck->li_bottom);
248         if (IS_ERR(handle))
249                 RETURN(PTR_ERR(handle));
250
251         rc = dt_declare_delete(env, obj, (const struct dt_key *)fid, handle);
252         if (rc != 0)
253                 GOTO(out, rc);
254
255         rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
256         if (rc != 0)
257                 GOTO(out, rc);
258
259         fid_cpu_to_be(key, fid);
260         rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
261                        BYPASS_CAPA);
262
263         GOTO(out, rc);
264
265 out:
266         dt_trans_stop(env, lfsck->li_bottom, handle);
267         return rc;
268 }
269
270 static int lfsck_namespace_update(const struct lu_env *env,
271                                   struct lfsck_component *com,
272                                   const struct lu_fid *fid,
273                                   __u8 flags, bool force)
274 {
275         struct lfsck_instance   *lfsck  = com->lc_lfsck;
276         struct lu_fid           *key    = &lfsck_env_info(env)->lti_fid;
277         struct thandle          *handle;
278         struct dt_object        *obj    = com->lc_obj;
279         int                      rc;
280         bool                     exist  = false;
281         __u8                     tf;
282         ENTRY;
283
284         rc = lfsck_namespace_lookup(env, com, fid, &tf);
285         if (rc != 0 && rc != -ENOENT)
286                 RETURN(rc);
287
288         if (rc == 0) {
289                 if (!force || flags == tf)
290                         RETURN(0);
291
292                 exist = true;
293                 handle = dt_trans_create(env, lfsck->li_bottom);
294                 if (IS_ERR(handle))
295                         RETURN(PTR_ERR(handle));
296
297                 rc = dt_declare_delete(env, obj, (const struct dt_key *)fid,
298                                        handle);
299                 if (rc != 0)
300                         GOTO(out, rc);
301         } else {
302                 handle = dt_trans_create(env, lfsck->li_bottom);
303                 if (IS_ERR(handle))
304                         RETURN(PTR_ERR(handle));
305         }
306
307         rc = dt_declare_insert(env, obj, (const struct dt_rec *)&flags,
308                                (const struct dt_key *)fid, handle);
309         if (rc != 0)
310                 GOTO(out, rc);
311
312         rc = dt_trans_start_local(env, lfsck->li_bottom, handle);
313         if (rc != 0)
314                 GOTO(out, rc);
315
316         fid_cpu_to_be(key, fid);
317         if (exist) {
318                 rc = dt_delete(env, obj, (const struct dt_key *)key, handle,
319                                BYPASS_CAPA);
320                 if (rc != 0) {
321                         CERROR("%s: fail to insert "DFID", rc = %d\n",
322                                lfsck_lfsck2name(com->lc_lfsck), PFID(fid), rc);
323                         GOTO(out, rc);
324                 }
325         }
326
327         rc = dt_insert(env, obj, (const struct dt_rec *)&flags,
328                        (const struct dt_key *)key, handle, BYPASS_CAPA, 1);
329
330         GOTO(out, rc);
331
332 out:
333         dt_trans_stop(env, lfsck->li_bottom, handle);
334         return rc;
335 }
336
337 static int lfsck_namespace_check_exist(const struct lu_env *env,
338                                        struct lfsck_instance *lfsck,
339                                        struct dt_object *obj, const char *name)
340 {
341         struct dt_object *dir = lfsck->li_obj_dir;
342         struct lu_fid    *fid = &lfsck_env_info(env)->lti_fid;
343         int               rc;
344         ENTRY;
345
346         if (unlikely(lfsck_is_dead_obj(obj)))
347                 RETURN(LFSCK_NAMEENTRY_DEAD);
348
349         rc = dt_lookup(env, dir, (struct dt_rec *)fid,
350                        (const struct dt_key *)name, BYPASS_CAPA);
351         if (rc == -ENOENT)
352                 RETURN(LFSCK_NAMEENTRY_REMOVED);
353
354         if (rc < 0)
355                 RETURN(rc);
356
357         if (!lu_fid_eq(fid, lfsck_dto2fid(obj)))
358                 RETURN(LFSCK_NAMEENTRY_RECREATED);
359
360         RETURN(0);
361 }
362
363 static int lfsck_declare_namespace_exec_dir(const struct lu_env *env,
364                                             struct dt_object *obj,
365                                             struct thandle *handle)
366 {
367         int rc;
368
369         /* For destroying all invalid linkEA entries. */
370         rc = dt_declare_xattr_del(env, obj, XATTR_NAME_LINK, handle);
371         if (rc != 0)
372                 return rc;
373
374         /* For insert new linkEA entry. */
375         rc = dt_declare_xattr_set(env, obj,
376                         lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
377                         XATTR_NAME_LINK, 0, handle);
378         return rc;
379 }
380
381 static int lfsck_links_read(const struct lu_env *env, struct dt_object *obj,
382                             struct linkea_data *ldata)
383 {
384         int rc;
385
386         ldata->ld_buf =
387                 lu_buf_check_and_alloc(&lfsck_env_info(env)->lti_linkea_buf,
388                                        CFS_PAGE_SIZE);
389         if (ldata->ld_buf->lb_buf == NULL)
390                 return -ENOMEM;
391
392         if (!dt_object_exists(obj))
393                 return -ENODATA;
394
395         rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK, BYPASS_CAPA);
396         if (rc == -ERANGE) {
397                 /* Buf was too small, figure out what we need. */
398                 lu_buf_free(ldata->ld_buf);
399                 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
400                                   BYPASS_CAPA);
401                 if (rc < 0)
402                         return rc;
403
404                 ldata->ld_buf = lu_buf_check_and_alloc(ldata->ld_buf, rc);
405                 if (ldata->ld_buf->lb_buf == NULL)
406                         return -ENOMEM;
407
408                 rc = dt_xattr_get(env, obj, ldata->ld_buf, XATTR_NAME_LINK,
409                                   BYPASS_CAPA);
410         }
411         if (rc < 0)
412                 return rc;
413
414         linkea_init(ldata);
415
416         return 0;
417 }
418
419 static int lfsck_links_write(const struct lu_env *env, struct dt_object *obj,
420                              struct linkea_data *ldata, struct thandle *handle)
421 {
422         const struct lu_buf *buf = lfsck_buf_get_const(env,
423                                                        ldata->ld_buf->lb_buf,
424                                                        ldata->ld_leh->leh_len);
425
426         return dt_xattr_set(env, obj, buf, XATTR_NAME_LINK, 0, handle,
427                             BYPASS_CAPA);
428 }
429
430 /**
431  * \retval ve: removed entries
432  */
433 static int lfsck_linkea_entry_unpack(struct lfsck_instance *lfsck,
434                                      struct linkea_data *ldata,
435                                      struct lu_name *cname,
436                                      struct lu_fid *pfid)
437 {
438         struct link_ea_entry    *oldlee;
439         int                      oldlen;
440         int                      removed = 0;
441
442         linkea_entry_unpack(ldata->ld_lee, &ldata->ld_reclen, cname, pfid);
443         oldlee = ldata->ld_lee;
444         oldlen = ldata->ld_reclen;
445         linkea_next_entry(ldata);
446         while (ldata->ld_lee != NULL) {
447                 ldata->ld_reclen = (ldata->ld_lee->lee_reclen[0] << 8) |
448                                    ldata->ld_lee->lee_reclen[1];
449                 if (unlikely(ldata->ld_reclen == oldlen &&
450                              memcmp(ldata->ld_lee, oldlee, oldlen) == 0)) {
451                         linkea_del_buf(ldata, cname);
452                         removed++;
453                 } else {
454                         linkea_next_entry(ldata);
455                 }
456         }
457         ldata->ld_lee = oldlee;
458         ldata->ld_reclen = oldlen;
459         return removed;
460 }
461
462 /**
463  * \retval +ve  repaired
464  * \retval 0    no need to repair
465  * \retval -ve  error cases
466  */
467 static int lfsck_namespace_double_scan_one(const struct lu_env *env,
468                                            struct lfsck_component *com,
469                                            struct dt_object *child, __u8 flags)
470 {
471         struct lfsck_thread_info *info    = lfsck_env_info(env);
472         struct lu_attr           *la      = &info->lti_la;
473         struct lu_name           *cname   = &info->lti_name;
474         struct lu_fid            *pfid    = &info->lti_fid;
475         struct lu_fid            *cfid    = &info->lti_fid2;
476         struct lfsck_instance   *lfsck    = com->lc_lfsck;
477         struct lfsck_bookmark   *bk       = &lfsck->li_bookmark_ram;
478         struct lfsck_namespace  *ns       =
479                                 (struct lfsck_namespace *)com->lc_file_ram;
480         struct linkea_data       ldata    = { 0 };
481         struct thandle          *handle   = NULL;
482         bool                     locked   = false;
483         bool                     update   = false;
484         int                      rc;
485         ENTRY;
486
487         if (com->lc_journal) {
488
489 again:
490                 LASSERT(!locked);
491
492                 update = false;
493                 com->lc_journal = 1;
494                 handle = dt_trans_create(env, lfsck->li_next);
495                 if (IS_ERR(handle))
496                         RETURN(rc = PTR_ERR(handle));
497
498                 rc = dt_declare_xattr_set(env, child,
499                         lfsck_buf_get_const(env, NULL, DEFAULT_LINKEA_SIZE),
500                         XATTR_NAME_LINK, 0, handle);
501                 if (rc != 0)
502                         GOTO(stop, rc);
503
504                 rc = dt_trans_start(env, lfsck->li_next, handle);
505                 if (rc != 0)
506                         GOTO(stop, rc);
507
508                 dt_write_lock(env, child, MOR_TGT_CHILD);
509                 locked = true;
510         }
511
512         if (unlikely(lfsck_is_dead_obj(child)))
513                 GOTO(stop, rc = 0);
514
515         rc = dt_attr_get(env, child, la, BYPASS_CAPA);
516         if (rc == 0)
517                 rc = lfsck_links_read(env, child, &ldata);
518         if (rc != 0) {
519                 if ((bk->lb_param & LPF_DRYRUN) &&
520                     (rc == -EINVAL || rc == -ENODATA))
521                         rc = 1;
522
523                 GOTO(stop, rc);
524         }
525
526         linkea_first_entry(&ldata);
527         while (ldata.ld_lee != NULL) {
528                 struct dt_object *parent = NULL;
529
530                 rc = lfsck_linkea_entry_unpack(lfsck, &ldata, cname, pfid);
531                 if (rc > 0)
532                         update = true;
533
534                 if (!fid_is_sane(pfid))
535                         goto shrink;
536
537                 parent = lfsck_object_find(env, lfsck, pfid);
538                 if (parent == NULL)
539                         goto shrink;
540                 else if (IS_ERR(parent))
541                         GOTO(stop, rc = PTR_ERR(parent));
542
543                 if (!dt_object_exists(parent))
544                         goto shrink;
545
546                 /* XXX: Currently, skip remote object, the consistency for
547                  *      remote object will be processed in LFSCK phase III. */
548                 if (dt_object_remote(parent)) {
549                         lfsck_object_put(env, parent);
550                         linkea_next_entry(&ldata);
551                         continue;
552                 }
553
554                 if (unlikely(!dt_try_as_dir(env, parent)))
555                         goto shrink;
556
557                 /* To guarantee the 'name' is terminated with '0'. */
558                 memcpy(info->lti_key, cname->ln_name, cname->ln_namelen);
559                 info->lti_key[cname->ln_namelen] = 0;
560                 cname->ln_name = info->lti_key;
561                 rc = dt_lookup(env, parent, (struct dt_rec *)cfid,
562                                (const struct dt_key *)cname->ln_name,
563                                BYPASS_CAPA);
564                 if (rc != 0 && rc != -ENOENT) {
565                         lfsck_object_put(env, parent);
566                         GOTO(stop, rc);
567                 }
568
569                 if (rc == 0) {
570                         if (lu_fid_eq(cfid, lfsck_dto2fid(child))) {
571                                 lfsck_object_put(env, parent);
572                                 linkea_next_entry(&ldata);
573                                 continue;
574                         }
575
576                         goto shrink;
577                 }
578
579                 /* If there is no name entry in the parent dir and the object
580                  * link count is less than the linkea entries count, then the
581                  * linkea entry should be removed. */
582                 if (ldata.ld_leh->leh_reccount > la->la_nlink)
583                         goto shrink;
584
585                 /* XXX: For the case of there is a linkea entry, but without
586                  *      name entry pointing to the object and its hard links
587                  *      count is not less than the object name entries count,
588                  *      then seems we should add the 'missed' name entry back
589                  *      to namespace, but before LFSCK phase III finished, we
590                  *      do not know whether the object has some inconsistency
591                  *      on other MDTs. So now, do NOT add the name entry back
592                  *      to the namespace, but keep the linkEA entry. LU-2914 */
593                 lfsck_object_put(env, parent);
594                 linkea_next_entry(&ldata);
595                 continue;
596
597 shrink:
598                 if (parent != NULL)
599                         lfsck_object_put(env, parent);
600                 if (bk->lb_param & LPF_DRYRUN)
601                         RETURN(1);
602
603                 CDEBUG(D_LFSCK, "Remove linkEA: "DFID"[%.*s], "DFID"\n",
604                        PFID(lfsck_dto2fid(child)), cname->ln_namelen, cname->ln_name,
605                        PFID(pfid));
606                 linkea_del_buf(&ldata, cname);
607                 update = true;
608         }
609
610         if (update) {
611                 if (!com->lc_journal) {
612                         com->lc_journal = 1;
613                         goto again;
614                 }
615
616                 rc = lfsck_links_write(env, child, &ldata, handle);
617         }
618
619         GOTO(stop, rc);
620
621 stop:
622         if (locked) {
623         /* XXX: For the case linkea entries count does not match the object hard
624          *      links count, we cannot update the later one simply. Before LFSCK
625          *      phase III finished, we cannot know whether there are some remote
626          *      name entries to be repaired or not. LU-2914 */
627                 if (rc == 0 && !lfsck_is_dead_obj(child) &&
628                     ldata.ld_leh != NULL &&
629                     ldata.ld_leh->leh_reccount != la->la_nlink)
630                         CWARN("%.16s: the object "DFID" linkEA entry count %u "
631                               "may not match its hardlink count %u\n",
632                               lfsck_lfsck2name(lfsck), PFID(cfid),
633                               ldata.ld_leh->leh_reccount, la->la_nlink);
634
635                 dt_write_unlock(env, child);
636         }
637
638         if (handle != NULL)
639                 dt_trans_stop(env, lfsck->li_next, handle);
640
641         if (rc == 0 && update) {
642                 ns->ln_objs_nlink_repaired++;
643                 rc = 1;
644         }
645
646         return rc;
647 }
648
649 /* namespace APIs */
650
651 static int lfsck_namespace_reset(const struct lu_env *env,
652                                  struct lfsck_component *com, bool init)
653 {
654         struct lfsck_instance   *lfsck = com->lc_lfsck;
655         struct lfsck_namespace  *ns    =
656                                 (struct lfsck_namespace *)com->lc_file_ram;
657         struct dt_object        *dto;
658         int                      rc;
659         ENTRY;
660
661         down_write(&com->lc_sem);
662         if (init) {
663                 memset(ns, 0, sizeof(*ns));
664         } else {
665                 __u32 count = ns->ln_success_count;
666                 __u64 last_time = ns->ln_time_last_complete;
667
668                 memset(ns, 0, sizeof(*ns));
669                 ns->ln_success_count = count;
670                 ns->ln_time_last_complete = last_time;
671         }
672         ns->ln_magic = LFSCK_NAMESPACE_MAGIC;
673         ns->ln_status = LS_INIT;
674
675         rc = local_object_unlink(env, lfsck->li_bottom, lfsck->li_local_root,
676                                  lfsck_namespace_name);
677         if (rc != 0)
678                 GOTO(out, rc);
679
680         dto = local_index_find_or_create(env, lfsck->li_los, lfsck->li_local_root,
681                                          lfsck_namespace_name,
682                                          S_IFREG | S_IRUGO | S_IWUSR,
683                                          &dt_lfsck_features);
684         if (IS_ERR(dto))
685                 GOTO(out, rc = PTR_ERR(dto));
686
687         com->lc_obj = dto;
688         rc = dto->do_ops->do_index_try(env, dto, &dt_lfsck_features);
689         if (rc != 0)
690                 GOTO(out, rc);
691
692         rc = lfsck_namespace_store(env, com, true);
693
694         GOTO(out, rc);
695
696 out:
697         up_write(&com->lc_sem);
698         return rc;
699 }
700
701 static void
702 lfsck_namespace_fail(const struct lu_env *env, struct lfsck_component *com,
703                      bool new_checked)
704 {
705         struct lfsck_namespace *ns = (struct lfsck_namespace *)com->lc_file_ram;
706
707         down_write(&com->lc_sem);
708         if (new_checked)
709                 com->lc_new_checked++;
710         ns->ln_items_failed++;
711         if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
712                 lfsck_pos_fill(env, com->lc_lfsck,
713                                &ns->ln_pos_first_inconsistent, false);
714         up_write(&com->lc_sem);
715 }
716
717 static int lfsck_namespace_checkpoint(const struct lu_env *env,
718                                       struct lfsck_component *com, bool init)
719 {
720         struct lfsck_instance   *lfsck = com->lc_lfsck;
721         struct lfsck_namespace  *ns    =
722                                 (struct lfsck_namespace *)com->lc_file_ram;
723         int                      rc;
724
725         if (com->lc_new_checked == 0 && !init)
726                 return 0;
727
728         down_write(&com->lc_sem);
729
730         if (init) {
731                 ns->ln_pos_latest_start = lfsck->li_pos_current;
732         } else {
733                 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
734                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
735                                 HALF_SEC - lfsck->li_time_last_checkpoint);
736                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
737                 ns->ln_items_checked += com->lc_new_checked;
738                 com->lc_new_checked = 0;
739         }
740
741         rc = lfsck_namespace_store(env, com, false);
742
743         up_write(&com->lc_sem);
744         return rc;
745 }
746
747 static int lfsck_namespace_prep(const struct lu_env *env,
748                                 struct lfsck_component *com)
749 {
750         struct lfsck_instance   *lfsck  = com->lc_lfsck;
751         struct lfsck_namespace  *ns     =
752                                 (struct lfsck_namespace *)com->lc_file_ram;
753         struct lfsck_position   *pos    = &com->lc_pos_start;
754
755         if (ns->ln_status == LS_COMPLETED) {
756                 int rc;
757
758                 rc = lfsck_namespace_reset(env, com, false);
759                 if (rc != 0)
760                         return rc;
761         }
762
763         down_write(&com->lc_sem);
764
765         ns->ln_time_latest_start = cfs_time_current_sec();
766
767         spin_lock(&lfsck->li_lock);
768         if (ns->ln_flags & LF_SCANNED_ONCE) {
769                 if (!lfsck->li_drop_dryrun ||
770                     lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
771                         ns->ln_status = LS_SCANNING_PHASE2;
772                         cfs_list_del_init(&com->lc_link);
773                         cfs_list_add_tail(&com->lc_link,
774                                           &lfsck->li_list_double_scan);
775                         if (!cfs_list_empty(&com->lc_link_dir))
776                                 cfs_list_del_init(&com->lc_link_dir);
777                         lfsck_pos_set_zero(pos);
778                 } else {
779                         ns->ln_status = LS_SCANNING_PHASE1;
780                         ns->ln_run_time_phase1 = 0;
781                         ns->ln_run_time_phase2 = 0;
782                         ns->ln_items_checked = 0;
783                         ns->ln_items_repaired = 0;
784                         ns->ln_items_failed = 0;
785                         ns->ln_dirs_checked = 0;
786                         ns->ln_mlinked_checked = 0;
787                         ns->ln_objs_checked_phase2 = 0;
788                         ns->ln_objs_repaired_phase2 = 0;
789                         ns->ln_objs_failed_phase2 = 0;
790                         ns->ln_objs_nlink_repaired = 0;
791                         ns->ln_objs_lost_found = 0;
792                         fid_zero(&ns->ln_fid_latest_scanned_phase2);
793                         if (cfs_list_empty(&com->lc_link_dir))
794                                 cfs_list_add_tail(&com->lc_link_dir,
795                                                   &lfsck->li_list_dir);
796                         *pos = ns->ln_pos_first_inconsistent;
797                 }
798         } else {
799                 ns->ln_status = LS_SCANNING_PHASE1;
800                 if (cfs_list_empty(&com->lc_link_dir))
801                         cfs_list_add_tail(&com->lc_link_dir,
802                                           &lfsck->li_list_dir);
803                 if (!lfsck->li_drop_dryrun ||
804                     lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent)) {
805                         *pos = ns->ln_pos_last_checkpoint;
806                         pos->lp_oit_cookie++;
807                 } else {
808                         *pos = ns->ln_pos_first_inconsistent;
809                 }
810         }
811         spin_unlock(&lfsck->li_lock);
812
813         up_write(&com->lc_sem);
814         return 0;
815 }
816
817 static int lfsck_namespace_exec_oit(const struct lu_env *env,
818                                     struct lfsck_component *com,
819                                     struct dt_object *obj)
820 {
821         down_write(&com->lc_sem);
822         com->lc_new_checked++;
823         if (S_ISDIR(lfsck_object_type(obj)))
824                 ((struct lfsck_namespace *)com->lc_file_ram)->ln_dirs_checked++;
825         up_write(&com->lc_sem);
826         return 0;
827 }
828
829 static int lfsck_namespace_exec_dir(const struct lu_env *env,
830                                     struct lfsck_component *com,
831                                     struct dt_object *obj,
832                                     struct lu_dirent *ent)
833 {
834         struct lfsck_thread_info   *info     = lfsck_env_info(env);
835         struct lu_attr             *la       = &info->lti_la;
836         struct lfsck_instance      *lfsck    = com->lc_lfsck;
837         struct lfsck_bookmark      *bk       = &lfsck->li_bookmark_ram;
838         struct lfsck_namespace     *ns       =
839                                 (struct lfsck_namespace *)com->lc_file_ram;
840         struct linkea_data          ldata    = { 0 };
841         const struct lu_fid        *pfid     =
842                                 lu_object_fid(&lfsck->li_obj_dir->do_lu);
843         const struct lu_fid        *cfid     = lfsck_dto2fid(obj);
844         const struct lu_name       *cname;
845         struct thandle             *handle   = NULL;
846         bool                        repaired = false;
847         bool                        locked   = false;
848         bool                        remove;
849         bool                        newdata;
850         int                         count    = 0;
851         int                         rc;
852         ENTRY;
853
854         cname = lfsck_name_get_const(env, ent->lde_name, ent->lde_namelen);
855         down_write(&com->lc_sem);
856         com->lc_new_checked++;
857
858         if (ent->lde_attrs & LUDA_UPGRADE) {
859                 ns->ln_flags |= LF_UPGRADE;
860                 repaired = true;
861         } else if (ent->lde_attrs & LUDA_REPAIR) {
862                 ns->ln_flags |= LF_INCONSISTENT;
863                 repaired = true;
864         }
865
866         if (ent->lde_name[0] == '.' &&
867             (ent->lde_namelen == 1 ||
868              (ent->lde_namelen == 2 && ent->lde_name[1] == '.') ||
869              fid_is_dot_lustre(&ent->lde_fid)))
870                 GOTO(out, rc = 0);
871
872         if (!(bk->lb_param & LPF_DRYRUN) &&
873             (com->lc_journal || repaired)) {
874
875 again:
876                 LASSERT(!locked);
877
878                 com->lc_journal = 1;
879                 handle = dt_trans_create(env, lfsck->li_next);
880                 if (IS_ERR(handle))
881                         GOTO(out, rc = PTR_ERR(handle));
882
883                 rc = lfsck_declare_namespace_exec_dir(env, obj, handle);
884                 if (rc != 0)
885                         GOTO(stop, rc);
886
887                 rc = dt_trans_start(env, lfsck->li_next, handle);
888                 if (rc != 0)
889                         GOTO(stop, rc);
890
891                 dt_write_lock(env, obj, MOR_TGT_CHILD);
892                 locked = true;
893         }
894
895         rc = lfsck_namespace_check_exist(env, lfsck, obj, ent->lde_name);
896         if (rc != 0)
897                 GOTO(stop, rc);
898
899         rc = lfsck_links_read(env, obj, &ldata);
900         if (rc == 0) {
901                 count = ldata.ld_leh->leh_reccount;
902                 rc = linkea_links_find(&ldata, cname, pfid);
903                 if ((rc == 0) &&
904                     (count == 1 || !S_ISDIR(lfsck_object_type(obj))))
905                         goto record;
906
907                 ns->ln_flags |= LF_INCONSISTENT;
908                 /* For dir, if there are more than one linkea entries, or the
909                  * linkea entry does not match the name entry, then remove all
910                  * and add the correct one. */
911                 if (S_ISDIR(lfsck_object_type(obj))) {
912                         remove = true;
913                         newdata = true;
914                 } else {
915                         remove = false;
916                         newdata = false;
917                 }
918                 goto nodata;
919         } else if (unlikely(rc == -EINVAL)) {
920                 count = 1;
921                 ns->ln_flags |= LF_INCONSISTENT;
922                 /* The magic crashed, we are not sure whether there are more
923                  * corrupt data in the linkea, so remove all linkea entries. */
924                 remove = true;
925                 newdata = true;
926                 goto nodata;
927         } else if (rc == -ENODATA) {
928                 count = 1;
929                 ns->ln_flags |= LF_UPGRADE;
930                 remove = false;
931                 newdata = true;
932
933 nodata:
934                 if (bk->lb_param & LPF_DRYRUN) {
935                         repaired = true;
936                         goto record;
937                 }
938
939                 if (!com->lc_journal)
940                         goto again;
941
942                 if (remove) {
943                         LASSERT(newdata);
944
945                         rc = dt_xattr_del(env, obj, XATTR_NAME_LINK, handle,
946                                           BYPASS_CAPA);
947                         if (rc != 0)
948                                 GOTO(stop, rc);
949                 }
950
951                 if (newdata) {
952                         rc = linkea_data_new(&ldata,
953                                         &lfsck_env_info(env)->lti_linkea_buf);
954                         if (rc != 0)
955                                 GOTO(stop, rc);
956                 }
957
958                 rc = linkea_add_buf(&ldata, cname, pfid);
959                 if (rc != 0)
960                         GOTO(stop, rc);
961
962                 rc = lfsck_links_write(env, obj, &ldata, handle);
963                 if (rc != 0)
964                         GOTO(stop, rc);
965
966                 count = ldata.ld_leh->leh_reccount;
967                 repaired = true;
968         } else {
969                 GOTO(stop, rc);
970         }
971
972 record:
973         LASSERT(count > 0);
974
975         rc = dt_attr_get(env, obj, la, BYPASS_CAPA);
976         if (rc != 0)
977                 GOTO(stop, rc);
978
979         if ((count == 1) &&
980             (la->la_nlink == 1 || S_ISDIR(lfsck_object_type(obj))))
981                 /* Usually, it is for single linked object or dir, do nothing.*/
982                 GOTO(stop, rc);
983
984         /* Following modification will be in another transaction.  */
985         if (handle != NULL) {
986                 LASSERT(dt_write_locked(env, obj));
987
988                 dt_write_unlock(env, obj);
989                 locked = false;
990
991                 dt_trans_stop(env, lfsck->li_next, handle);
992                 handle = NULL;
993         }
994
995         ns->ln_mlinked_checked++;
996         rc = lfsck_namespace_update(env, com, cfid,
997                         count != la->la_nlink ? LLF_UNMATCH_NLINKS : 0, false);
998
999         GOTO(out, rc);
1000
1001 stop:
1002         if (locked)
1003                 dt_write_unlock(env, obj);
1004
1005         if (handle != NULL)
1006                 dt_trans_stop(env, lfsck->li_next, handle);
1007
1008 out:
1009         if (rc < 0) {
1010                 ns->ln_items_failed++;
1011                 if (lfsck_pos_is_zero(&ns->ln_pos_first_inconsistent))
1012                         lfsck_pos_fill(env, lfsck,
1013                                        &ns->ln_pos_first_inconsistent, false);
1014                 if (!(bk->lb_param & LPF_FAILOUT))
1015                         rc = 0;
1016         } else {
1017                 if (repaired)
1018                         ns->ln_items_repaired++;
1019                 else
1020                         com->lc_journal = 0;
1021                 rc = 0;
1022         }
1023         up_write(&com->lc_sem);
1024         return rc;
1025 }
1026
1027 static int lfsck_namespace_post(const struct lu_env *env,
1028                                 struct lfsck_component *com,
1029                                 int result, bool init)
1030 {
1031         struct lfsck_instance   *lfsck = com->lc_lfsck;
1032         struct lfsck_namespace  *ns    =
1033                                 (struct lfsck_namespace *)com->lc_file_ram;
1034         int                      rc;
1035
1036         down_write(&com->lc_sem);
1037
1038         spin_lock(&lfsck->li_lock);
1039         if (!init)
1040                 ns->ln_pos_last_checkpoint = lfsck->li_pos_current;
1041         if (result > 0) {
1042                 ns->ln_status = LS_SCANNING_PHASE2;
1043                 ns->ln_flags |= LF_SCANNED_ONCE;
1044                 ns->ln_flags &= ~LF_UPGRADE;
1045                 cfs_list_del_init(&com->lc_link);
1046                 cfs_list_del_init(&com->lc_link_dir);
1047                 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_double_scan);
1048         } else if (result == 0) {
1049                 if (lfsck->li_paused) {
1050                         ns->ln_status = LS_PAUSED;
1051                 } else {
1052                         ns->ln_status = LS_STOPPED;
1053                         cfs_list_del_init(&com->lc_link);
1054                         cfs_list_del_init(&com->lc_link_dir);
1055                         cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1056                 }
1057         } else {
1058                 ns->ln_status = LS_FAILED;
1059                 cfs_list_del_init(&com->lc_link);
1060                 cfs_list_del_init(&com->lc_link_dir);
1061                 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1062         }
1063         spin_unlock(&lfsck->li_lock);
1064
1065         if (!init) {
1066                 ns->ln_run_time_phase1 += cfs_duration_sec(cfs_time_current() +
1067                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1068                 ns->ln_time_last_checkpoint = cfs_time_current_sec();
1069                 ns->ln_items_checked += com->lc_new_checked;
1070                 com->lc_new_checked = 0;
1071         }
1072
1073         rc = lfsck_namespace_store(env, com, false);
1074
1075         up_write(&com->lc_sem);
1076         return rc;
1077 }
1078
1079 static int
1080 lfsck_namespace_dump(const struct lu_env *env, struct lfsck_component *com,
1081                      char *buf, int len)
1082 {
1083         struct lfsck_instance   *lfsck = com->lc_lfsck;
1084         struct lfsck_bookmark   *bk    = &lfsck->li_bookmark_ram;
1085         struct lfsck_namespace  *ns    =
1086                                 (struct lfsck_namespace *)com->lc_file_ram;
1087         int                      save  = len;
1088         int                      ret   = -ENOSPC;
1089         int                      rc;
1090
1091         down_read(&com->lc_sem);
1092         rc = snprintf(buf, len,
1093                       "name: lfsck_namespace\n"
1094                       "magic: 0x%x\n"
1095                       "version: %d\n"
1096                       "status: %s\n",
1097                       ns->ln_magic,
1098                       bk->lb_version,
1099                       lfsck_status_names[ns->ln_status]);
1100         if (rc <= 0)
1101                 goto out;
1102
1103         buf += rc;
1104         len -= rc;
1105         rc = lfsck_bits_dump(&buf, &len, ns->ln_flags, lfsck_flags_names,
1106                              "flags");
1107         if (rc < 0)
1108                 goto out;
1109
1110         rc = lfsck_bits_dump(&buf, &len, bk->lb_param, lfsck_param_names,
1111                              "param");
1112         if (rc < 0)
1113                 goto out;
1114
1115         rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_complete,
1116                              "time_since_last_completed");
1117         if (rc < 0)
1118                 goto out;
1119
1120         rc = lfsck_time_dump(&buf, &len, ns->ln_time_latest_start,
1121                              "time_since_latest_start");
1122         if (rc < 0)
1123                 goto out;
1124
1125         rc = lfsck_time_dump(&buf, &len, ns->ln_time_last_checkpoint,
1126                              "time_since_last_checkpoint");
1127         if (rc < 0)
1128                 goto out;
1129
1130         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_latest_start,
1131                             "latest_start_position");
1132         if (rc < 0)
1133                 goto out;
1134
1135         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_last_checkpoint,
1136                             "last_checkpoint_position");
1137         if (rc < 0)
1138                 goto out;
1139
1140         rc = lfsck_pos_dump(&buf, &len, &ns->ln_pos_first_inconsistent,
1141                             "first_failure_position");
1142         if (rc < 0)
1143                 goto out;
1144
1145         if (ns->ln_status == LS_SCANNING_PHASE1) {
1146                 struct lfsck_position pos;
1147                 const struct dt_it_ops *iops;
1148                 cfs_duration_t duration = cfs_time_current() -
1149                                           lfsck->li_time_last_checkpoint;
1150                 __u64 checked = ns->ln_items_checked + com->lc_new_checked;
1151                 __u64 speed = checked;
1152                 __u64 new_checked = com->lc_new_checked * CFS_HZ;
1153                 __u32 rtime = ns->ln_run_time_phase1 +
1154                               cfs_duration_sec(duration + HALF_SEC);
1155
1156                 if (duration != 0)
1157                         do_div(new_checked, duration);
1158                 if (rtime != 0)
1159                         do_div(speed, rtime);
1160                 rc = snprintf(buf, len,
1161                               "checked_phase1: "LPU64"\n"
1162                               "checked_phase2: "LPU64"\n"
1163                               "updated_phase1: "LPU64"\n"
1164                               "updated_phase2: "LPU64"\n"
1165                               "failed_phase1: "LPU64"\n"
1166                               "failed_phase2: "LPU64"\n"
1167                               "dirs: "LPU64"\n"
1168                               "M-linked: "LPU64"\n"
1169                               "nlinks_repaired: "LPU64"\n"
1170                               "lost_found: "LPU64"\n"
1171                               "success_count: %u\n"
1172                               "run_time_phase1: %u seconds\n"
1173                               "run_time_phase2: %u seconds\n"
1174                               "average_speed_phase1: "LPU64" items/sec\n"
1175                               "average_speed_phase2: N/A\n"
1176                               "real-time_speed_phase1: "LPU64" items/sec\n"
1177                               "real-time_speed_phase2: N/A\n",
1178                               checked,
1179                               ns->ln_objs_checked_phase2,
1180                               ns->ln_items_repaired,
1181                               ns->ln_objs_repaired_phase2,
1182                               ns->ln_items_failed,
1183                               ns->ln_objs_failed_phase2,
1184                               ns->ln_dirs_checked,
1185                               ns->ln_mlinked_checked,
1186                               ns->ln_objs_nlink_repaired,
1187                               ns->ln_objs_lost_found,
1188                               ns->ln_success_count,
1189                               rtime,
1190                               ns->ln_run_time_phase2,
1191                               speed,
1192                               new_checked);
1193                 if (rc <= 0)
1194                         goto out;
1195
1196                 buf += rc;
1197                 len -= rc;
1198
1199                 LASSERT(lfsck->li_di_oit != NULL);
1200
1201                 iops = &lfsck->li_obj_oit->do_index_ops->dio_it;
1202
1203                 /* The low layer otable-based iteration position may NOT
1204                  * exactly match the namespace-based directory traversal
1205                  * cookie. Generally, it is not a serious issue. But the
1206                  * caller should NOT make assumption on that. */
1207                 pos.lp_oit_cookie = iops->store(env, lfsck->li_di_oit);
1208                 if (!lfsck->li_current_oit_processed)
1209                         pos.lp_oit_cookie--;
1210
1211                 spin_lock(&lfsck->li_lock);
1212                 if (lfsck->li_di_dir != NULL) {
1213                         pos.lp_dir_cookie = lfsck->li_cookie_dir;
1214                         if (pos.lp_dir_cookie >= MDS_DIR_END_OFF) {
1215                                 fid_zero(&pos.lp_dir_parent);
1216                                 pos.lp_dir_cookie = 0;
1217                         } else {
1218                                 pos.lp_dir_parent =
1219                                 *lu_object_fid(&lfsck->li_obj_dir->do_lu);
1220                         }
1221                 } else {
1222                         fid_zero(&pos.lp_dir_parent);
1223                         pos.lp_dir_cookie = 0;
1224                 }
1225                 spin_unlock(&lfsck->li_lock);
1226                 rc = lfsck_pos_dump(&buf, &len, &pos, "current_position");
1227                 if (rc <= 0)
1228                         goto out;
1229         } else if (ns->ln_status == LS_SCANNING_PHASE2) {
1230                 cfs_duration_t duration = cfs_time_current() -
1231                                           lfsck->li_time_last_checkpoint;
1232                 __u64 checked = ns->ln_objs_checked_phase2 +
1233                                 com->lc_new_checked;
1234                 __u64 speed1 = ns->ln_items_checked;
1235                 __u64 speed2 = checked;
1236                 __u64 new_checked = com->lc_new_checked * CFS_HZ;
1237                 __u32 rtime = ns->ln_run_time_phase2 +
1238                               cfs_duration_sec(duration + HALF_SEC);
1239
1240                 if (duration != 0)
1241                         do_div(new_checked, duration);
1242                 if (ns->ln_run_time_phase1 != 0)
1243                         do_div(speed1, ns->ln_run_time_phase1);
1244                 if (rtime != 0)
1245                         do_div(speed2, rtime);
1246                 rc = snprintf(buf, len,
1247                               "checked_phase1: "LPU64"\n"
1248                               "checked_phase2: "LPU64"\n"
1249                               "updated_phase1: "LPU64"\n"
1250                               "updated_phase2: "LPU64"\n"
1251                               "failed_phase1: "LPU64"\n"
1252                               "failed_phase2: "LPU64"\n"
1253                               "dirs: "LPU64"\n"
1254                               "M-linked: "LPU64"\n"
1255                               "nlinks_repaired: "LPU64"\n"
1256                               "lost_found: "LPU64"\n"
1257                               "success_count: %u\n"
1258                               "run_time_phase1: %u seconds\n"
1259                               "run_time_phase2: %u seconds\n"
1260                               "average_speed_phase1: "LPU64" items/sec\n"
1261                               "average_speed_phase2: "LPU64" objs/sec\n"
1262                               "real-time_speed_phase1: N/A\n"
1263                               "real-time_speed_phase2: "LPU64" objs/sec\n"
1264                               "current_position: "DFID"\n",
1265                               ns->ln_items_checked,
1266                               checked,
1267                               ns->ln_items_repaired,
1268                               ns->ln_objs_repaired_phase2,
1269                               ns->ln_items_failed,
1270                               ns->ln_objs_failed_phase2,
1271                               ns->ln_dirs_checked,
1272                               ns->ln_mlinked_checked,
1273                               ns->ln_objs_nlink_repaired,
1274                               ns->ln_objs_lost_found,
1275                               ns->ln_success_count,
1276                               ns->ln_run_time_phase1,
1277                               rtime,
1278                               speed1,
1279                               speed2,
1280                               new_checked,
1281                               PFID(&ns->ln_fid_latest_scanned_phase2));
1282                 if (rc <= 0)
1283                         goto out;
1284
1285                 buf += rc;
1286                 len -= rc;
1287         } else {
1288                 __u64 speed1 = ns->ln_items_checked;
1289                 __u64 speed2 = ns->ln_objs_checked_phase2;
1290
1291                 if (ns->ln_run_time_phase1 != 0)
1292                         do_div(speed1, ns->ln_run_time_phase1);
1293                 if (ns->ln_run_time_phase2 != 0)
1294                         do_div(speed2, ns->ln_run_time_phase2);
1295                 rc = snprintf(buf, len,
1296                               "checked_phase1: "LPU64"\n"
1297                               "checked_phase2: "LPU64"\n"
1298                               "updated_phase1: "LPU64"\n"
1299                               "updated_phase2: "LPU64"\n"
1300                               "failed_phase1: "LPU64"\n"
1301                               "failed_phase2: "LPU64"\n"
1302                               "dirs: "LPU64"\n"
1303                               "M-linked: "LPU64"\n"
1304                               "nlinks_repaired: "LPU64"\n"
1305                               "lost_found: "LPU64"\n"
1306                               "success_count: %u\n"
1307                               "run_time_phase1: %u seconds\n"
1308                               "run_time_phase2: %u seconds\n"
1309                               "average_speed_phase1: "LPU64" items/sec\n"
1310                               "average_speed_phase2: "LPU64" objs/sec\n"
1311                               "real-time_speed_phase1: N/A\n"
1312                               "real-time_speed_phase2: N/A\n"
1313                               "current_position: N/A\n",
1314                               ns->ln_items_checked,
1315                               ns->ln_objs_checked_phase2,
1316                               ns->ln_items_repaired,
1317                               ns->ln_objs_repaired_phase2,
1318                               ns->ln_items_failed,
1319                               ns->ln_objs_failed_phase2,
1320                               ns->ln_dirs_checked,
1321                               ns->ln_mlinked_checked,
1322                               ns->ln_objs_nlink_repaired,
1323                               ns->ln_objs_lost_found,
1324                               ns->ln_success_count,
1325                               ns->ln_run_time_phase1,
1326                               ns->ln_run_time_phase2,
1327                               speed1,
1328                               speed2);
1329                 if (rc <= 0)
1330                         goto out;
1331
1332                 buf += rc;
1333                 len -= rc;
1334         }
1335         ret = save - len;
1336
1337 out:
1338         up_read(&com->lc_sem);
1339         return ret;
1340 }
1341
1342 static int lfsck_namespace_double_scan(const struct lu_env *env,
1343                                        struct lfsck_component *com)
1344 {
1345         struct lfsck_instance   *lfsck  = com->lc_lfsck;
1346         struct ptlrpc_thread    *thread = &lfsck->li_thread;
1347         struct lfsck_bookmark   *bk     = &lfsck->li_bookmark_ram;
1348         struct lfsck_namespace  *ns     =
1349                                 (struct lfsck_namespace *)com->lc_file_ram;
1350         struct dt_object        *obj    = com->lc_obj;
1351         const struct dt_it_ops  *iops   = &obj->do_index_ops->dio_it;
1352         struct dt_object        *target;
1353         struct dt_it            *di;
1354         struct dt_key           *key;
1355         struct lu_fid            fid;
1356         int                      rc;
1357         __u8                     flags;
1358         ENTRY;
1359
1360         lfsck->li_new_scanned = 0;
1361         lfsck->li_time_last_checkpoint = cfs_time_current();
1362         lfsck->li_time_next_checkpoint = lfsck->li_time_last_checkpoint +
1363                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1364
1365         di = iops->init(env, obj, 0, BYPASS_CAPA);
1366         if (IS_ERR(di))
1367                 RETURN(PTR_ERR(di));
1368
1369         fid_cpu_to_be(&fid, &ns->ln_fid_latest_scanned_phase2);
1370         rc = iops->get(env, di, (const struct dt_key *)&fid);
1371         if (rc < 0)
1372                 GOTO(fini, rc);
1373
1374         /* Skip the start one, which either has been processed or non-exist. */
1375         rc = iops->next(env, di);
1376         if (rc != 0)
1377                 GOTO(put, rc);
1378
1379         if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_NO_DOUBLESCAN))
1380                 GOTO(put, rc = 0);
1381
1382         do {
1383                 if (OBD_FAIL_CHECK(OBD_FAIL_LFSCK_DELAY3) &&
1384                     cfs_fail_val > 0) {
1385                         struct l_wait_info lwi;
1386
1387                         lwi = LWI_TIMEOUT(cfs_time_seconds(cfs_fail_val),
1388                                           NULL, NULL);
1389                         l_wait_event(thread->t_ctl_waitq,
1390                                      !thread_is_running(thread),
1391                                      &lwi);
1392                 }
1393
1394                 key = iops->key(env, di);
1395                 fid_be_to_cpu(&fid, (const struct lu_fid *)key);
1396                 target = lfsck_object_find(env, lfsck, &fid);
1397                 down_write(&com->lc_sem);
1398                 if (target == NULL) {
1399                         rc = 0;
1400                         goto checkpoint;
1401                 } else if (IS_ERR(target)) {
1402                         rc = PTR_ERR(target);
1403                         goto checkpoint;
1404                 }
1405
1406                 /* XXX: Currently, skip remote object, the consistency for
1407                  *      remote object will be processed in LFSCK phase III. */
1408                 if (dt_object_exists(target) && !dt_object_remote(target)) {
1409                         rc = iops->rec(env, di, (struct dt_rec *)&flags, 0);
1410                         if (rc == 0)
1411                                 rc = lfsck_namespace_double_scan_one(env, com,
1412                                                                 target, flags);
1413                 }
1414
1415                 lfsck_object_put(env, target);
1416
1417 checkpoint:
1418                 lfsck->li_new_scanned++;
1419                 com->lc_new_checked++;
1420                 ns->ln_fid_latest_scanned_phase2 = fid;
1421                 if (rc > 0)
1422                         ns->ln_objs_repaired_phase2++;
1423                 else if (rc < 0)
1424                         ns->ln_objs_failed_phase2++;
1425                 up_write(&com->lc_sem);
1426
1427                 if ((rc == 0) || ((rc > 0) && !(bk->lb_param & LPF_DRYRUN))) {
1428                         lfsck_namespace_delete(env, com, &fid);
1429                 } else if (rc < 0) {
1430                         flags |= LLF_REPAIR_FAILED;
1431                         lfsck_namespace_update(env, com, &fid, flags, true);
1432                 }
1433
1434                 if (rc < 0 && bk->lb_param & LPF_FAILOUT)
1435                         GOTO(put, rc);
1436
1437                 if (unlikely(cfs_time_beforeq(lfsck->li_time_next_checkpoint,
1438                                               cfs_time_current())) &&
1439                     com->lc_new_checked != 0) {
1440                         down_write(&com->lc_sem);
1441                         ns->ln_run_time_phase2 +=
1442                                 cfs_duration_sec(cfs_time_current() +
1443                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1444                         ns->ln_time_last_checkpoint = cfs_time_current_sec();
1445                         ns->ln_objs_checked_phase2 += com->lc_new_checked;
1446                         com->lc_new_checked = 0;
1447                         rc = lfsck_namespace_store(env, com, false);
1448                         up_write(&com->lc_sem);
1449                         if (rc != 0)
1450                                 GOTO(put, rc);
1451
1452                         lfsck->li_time_last_checkpoint = cfs_time_current();
1453                         lfsck->li_time_next_checkpoint =
1454                                 lfsck->li_time_last_checkpoint +
1455                                 cfs_time_seconds(LFSCK_CHECKPOINT_INTERVAL);
1456                 }
1457
1458                 lfsck_control_speed(lfsck);
1459                 if (unlikely(!thread_is_running(thread)))
1460                         GOTO(put, rc = 0);
1461
1462                 rc = iops->next(env, di);
1463         } while (rc == 0);
1464
1465         GOTO(put, rc);
1466
1467 put:
1468         iops->put(env, di);
1469
1470 fini:
1471         iops->fini(env, di);
1472         down_write(&com->lc_sem);
1473
1474         ns->ln_run_time_phase2 += cfs_duration_sec(cfs_time_current() +
1475                                 HALF_SEC - lfsck->li_time_last_checkpoint);
1476         ns->ln_time_last_checkpoint = cfs_time_current_sec();
1477         ns->ln_objs_checked_phase2 += com->lc_new_checked;
1478         com->lc_new_checked = 0;
1479
1480         if (rc > 0) {
1481                 com->lc_journal = 0;
1482                 ns->ln_status = LS_COMPLETED;
1483                 if (!(bk->lb_param & LPF_DRYRUN))
1484                         ns->ln_flags &=
1485                         ~(LF_SCANNED_ONCE | LF_INCONSISTENT | LF_UPGRADE);
1486                 ns->ln_time_last_complete = ns->ln_time_last_checkpoint;
1487                 ns->ln_success_count++;
1488         } else if (rc == 0) {
1489                 if (lfsck->li_paused)
1490                         ns->ln_status = LS_PAUSED;
1491                 else
1492                         ns->ln_status = LS_STOPPED;
1493         } else {
1494                 ns->ln_status = LS_FAILED;
1495         }
1496
1497         if (ns->ln_status != LS_PAUSED) {
1498                 spin_lock(&lfsck->li_lock);
1499                 cfs_list_del_init(&com->lc_link);
1500                 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1501                 spin_unlock(&lfsck->li_lock);
1502         }
1503
1504         rc = lfsck_namespace_store(env, com, false);
1505
1506         up_write(&com->lc_sem);
1507         return rc;
1508 }
1509
1510 static struct lfsck_operations lfsck_namespace_ops = {
1511         .lfsck_reset            = lfsck_namespace_reset,
1512         .lfsck_fail             = lfsck_namespace_fail,
1513         .lfsck_checkpoint       = lfsck_namespace_checkpoint,
1514         .lfsck_prep             = lfsck_namespace_prep,
1515         .lfsck_exec_oit         = lfsck_namespace_exec_oit,
1516         .lfsck_exec_dir         = lfsck_namespace_exec_dir,
1517         .lfsck_post             = lfsck_namespace_post,
1518         .lfsck_dump             = lfsck_namespace_dump,
1519         .lfsck_double_scan      = lfsck_namespace_double_scan,
1520 };
1521
1522 int lfsck_namespace_setup(const struct lu_env *env,
1523                           struct lfsck_instance *lfsck)
1524 {
1525         struct lfsck_component  *com;
1526         struct lfsck_namespace  *ns;
1527         struct dt_object        *obj;
1528         int                      rc;
1529         ENTRY;
1530
1531         LASSERT(lfsck->li_master);
1532
1533         OBD_ALLOC_PTR(com);
1534         if (com == NULL)
1535                 RETURN(-ENOMEM);
1536
1537         CFS_INIT_LIST_HEAD(&com->lc_link);
1538         CFS_INIT_LIST_HEAD(&com->lc_link_dir);
1539         init_rwsem(&com->lc_sem);
1540         atomic_set(&com->lc_ref, 1);
1541         com->lc_lfsck = lfsck;
1542         com->lc_type = LT_NAMESPACE;
1543         com->lc_ops = &lfsck_namespace_ops;
1544         com->lc_file_size = sizeof(struct lfsck_namespace);
1545         OBD_ALLOC(com->lc_file_ram, com->lc_file_size);
1546         if (com->lc_file_ram == NULL)
1547                 GOTO(out, rc = -ENOMEM);
1548
1549         OBD_ALLOC(com->lc_file_disk, com->lc_file_size);
1550         if (com->lc_file_disk == NULL)
1551                 GOTO(out, rc = -ENOMEM);
1552
1553         obj = local_index_find_or_create(env, lfsck->li_los,
1554                                          lfsck->li_local_root,
1555                                          lfsck_namespace_name,
1556                                          S_IFREG | S_IRUGO | S_IWUSR,
1557                                          &dt_lfsck_features);
1558         if (IS_ERR(obj))
1559                 GOTO(out, rc = PTR_ERR(obj));
1560
1561         com->lc_obj = obj;
1562         rc = obj->do_ops->do_index_try(env, obj, &dt_lfsck_features);
1563         if (rc != 0)
1564                 GOTO(out, rc);
1565
1566         rc = lfsck_namespace_load(env, com);
1567         if (rc > 0)
1568                 rc = lfsck_namespace_reset(env, com, true);
1569         else if (rc == -ENODATA)
1570                 rc = lfsck_namespace_init(env, com);
1571         if (rc != 0)
1572                 GOTO(out, rc);
1573
1574         ns = (struct lfsck_namespace *)com->lc_file_ram;
1575         switch (ns->ln_status) {
1576         case LS_INIT:
1577         case LS_COMPLETED:
1578         case LS_FAILED:
1579         case LS_STOPPED:
1580                 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_idle);
1581                 break;
1582         default:
1583                 CERROR("%s: unknown lfsck_namespace status: %u\n",
1584                        lfsck_lfsck2name(lfsck), ns->ln_status);
1585                 /* fall through */
1586         case LS_SCANNING_PHASE1:
1587         case LS_SCANNING_PHASE2:
1588                 /* No need to store the status to disk right now.
1589                  * If the system crashed before the status stored,
1590                  * it will be loaded back when next time. */
1591                 ns->ln_status = LS_CRASHED;
1592                 /* fall through */
1593         case LS_PAUSED:
1594         case LS_CRASHED:
1595                 cfs_list_add_tail(&com->lc_link, &lfsck->li_list_scan);
1596                 cfs_list_add_tail(&com->lc_link_dir, &lfsck->li_list_dir);
1597                 break;
1598         }
1599
1600         GOTO(out, rc = 0);
1601
1602 out:
1603         if (rc != 0)
1604                 lfsck_component_cleanup(env, com);
1605         return rc;
1606 }