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