1 /* $NetBSD: pass5.c,v 1.37 2020/04/03 19:36:33 joerg Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Konrad E. Schroder <perseant@hhhh.org>.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/types.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <sys/buf.h>
36 #include <sys/mount.h>
37
38 #define vnode uvnode
39 #include <ufs/lfs/lfs.h>
40 #include <ufs/lfs/lfs_accessors.h>
41 #include <ufs/lfs/lfs_inode.h>
42 #undef vnode
43
44 #include <string.h>
45
46 #include "bufcache.h"
47 #include "lfs_user.h"
48
49 #include "fsck.h"
50 #include "extern.h"
51 #include "fsutil.h"
52
53 void
pass5(void)54 pass5(void)
55 {
56 SEGUSE *su;
57 struct ubuf *bp;
58 int i;
59 daddr_t bb; /* total number of used blocks (lower bound) */
60 daddr_t ubb; /* upper bound number of used blocks */
61 daddr_t avail; /* blocks available for writing */
62 unsigned long dmeta; /* blocks in segsums and inodes */
63 int nclean; /* clean segments */
64 size_t labelskew;
65 int diddirty;
66
67 /*
68 * Check segment holdings against actual holdings. Check for
69 * "clean" segments that contain live data. If we are only
70 * rolling forward, we can't check the segment holdings, but
71 * we can still check the cleanerinfo data.
72 */
73 nclean = 0;
74 avail = 0;
75 bb = ubb = 0;
76 dmeta = 0;
77 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
78 diddirty = 0;
79 LFS_SEGENTRY(su, fs, i, bp);
80 if (!preen && !(su->su_flags & SEGUSE_DIRTY) &&
81 seg_table[i].su_nbytes > 0) {
82 pwarn("CLEAN SEGMENT %d CONTAINS %d BYTES\n",
83 i, seg_table[i].su_nbytes);
84 if (reply("MARK SEGMENT DIRTY")) {
85 su->su_flags |= SEGUSE_DIRTY;
86 ++diddirty;
87 }
88 }
89 if (!preen && su->su_nbytes != seg_table[i].su_nbytes) {
90 pwarn("SEGMENT %d CLAIMS %d BYTES BUT HAS %d",
91 i, su->su_nbytes, seg_table[i].su_nbytes);
92 if ((int32_t)su->su_nbytes >
93 (int32_t)seg_table[i].su_nbytes)
94 pwarn(" (HIGH BY %d)\n", su->su_nbytes -
95 seg_table[i].su_nbytes);
96 else
97 pwarn(" (LOW BY %d)\n", -su->su_nbytes +
98 seg_table[i].su_nbytes);
99 if (reply("FIX")) {
100 su->su_nbytes = seg_table[i].su_nbytes;
101 ++diddirty;
102 }
103 }
104 if (su->su_flags & SEGUSE_DIRTY) {
105 bb += lfs_btofsb(fs, su->su_nbytes +
106 su->su_nsums * lfs_sb_getsumsize(fs));
107 ubb += lfs_btofsb(fs, su->su_nbytes +
108 su->su_nsums * lfs_sb_getsumsize(fs) +
109 su->su_ninos * lfs_sb_getibsize(fs));
110 dmeta += lfs_btofsb(fs,
111 lfs_sb_getsumsize(fs) * su->su_nsums);
112 dmeta += lfs_btofsb(fs,
113 lfs_sb_getibsize(fs) * su->su_ninos);
114 } else {
115 nclean++;
116 avail += lfs_segtod(fs, 1);
117 if (su->su_flags & SEGUSE_SUPERBLOCK)
118 avail -= lfs_btofsb(fs, LFS_SBPAD);
119 if (i == 0 && lfs_sb_getversion(fs) > 1 &&
120 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
121 avail -= lfs_btofsb(fs, LFS_LABELPAD) -
122 lfs_sb_gets0addr(fs);
123 }
124 if (diddirty)
125 VOP_BWRITE(bp);
126 else
127 brelse(bp, 0);
128 }
129
130 /* Also may be available bytes in current seg */
131 i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
132 avail += lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs);
133 /* But do not count minfreesegs */
134 avail -= lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) -
135 (lfs_sb_getminfreeseg(fs) / 2)));
136 /* Note we may have bytes to write yet */
137 avail -= lfs_btofsb(fs, locked_queue_bytes);
138
139 if (idaddr)
140 pwarn("NOTE: when using -i, expect discrepancies in dmeta,"
141 " avail, nclean, bfree\n");
142 if (dmeta != lfs_sb_getdmeta(fs)) {
143 pwarn("DMETA GIVEN AS %d, SHOULD BE %ld\n",
144 lfs_sb_getdmeta(fs), dmeta);
145 if (preen || reply("FIX")) {
146 lfs_sb_setdmeta(fs, dmeta);
147 sbdirty();
148 }
149 }
150 if (avail != lfs_sb_getavail(fs)) {
151 pwarn("AVAIL GIVEN AS %jd, SHOULD BE %jd\n",
152 (intmax_t)lfs_sb_getavail(fs), (intmax_t)avail);
153 if (preen || reply("FIX")) {
154 lfs_sb_setavail(fs, avail);
155 sbdirty();
156 }
157 }
158 if (nclean != lfs_sb_getnclean(fs)) {
159 pwarn("NCLEAN GIVEN AS %d, SHOULD BE %d\n", lfs_sb_getnclean(fs),
160 nclean);
161 if (preen || reply("FIX")) {
162 lfs_sb_setnclean(fs, nclean);
163 sbdirty();
164 }
165 }
166
167 labelskew = 0;
168 if (lfs_sb_getversion(fs) > 1 &&
169 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
170 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
171 if (lfs_sb_getbfree(fs) > lfs_sb_getdsize(fs) - bb - labelskew ||
172 lfs_sb_getbfree(fs) < lfs_sb_getdsize(fs) - ubb - labelskew) {
173 pwarn("BFREE GIVEN AS %jd, SHOULD BE BETWEEN %jd AND %jd\n",
174 (intmax_t)lfs_sb_getbfree(fs),
175 (intmax_t)(lfs_sb_getdsize(fs) - ubb - labelskew),
176 (intmax_t)(lfs_sb_getdsize(fs) - bb - labelskew));
177 if (preen || reply("FIX")) {
178 lfs_sb_setbfree(fs,
179 ((lfs_sb_getdsize(fs) - labelskew - ubb) +
180 lfs_sb_getdsize(fs) - labelskew - bb) / 2);
181 sbdirty();
182 }
183 }
184 }
185