pass5.c revision 1.38 1 /* $NetBSD: pass5.c,v 1.38 2025/09/14 19:14:30 perseant 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 (at) 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
54 pass5(void)
55 {
56 SEGUSE *su;
57 struct ubuf *bp;
58 int i, nsb, curr, labelcorrect, mfs;
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 nsb = curr = labelcorrect = mfs = 0;
78 for (i = 0; i < lfs_sb_getnseg(fs); i++) {
79 diddirty = 0;
80 LFS_SEGENTRY(su, fs, i, bp);
81 if (!preen && !(su->su_flags & SEGUSE_DIRTY) &&
82 seg_table[i].su_nbytes > 0) {
83 pwarn("CLEAN SEGMENT %d CONTAINS %d BYTES\n",
84 i, seg_table[i].su_nbytes);
85 if (reply("MARK SEGMENT DIRTY")) {
86 su->su_flags |= SEGUSE_DIRTY;
87 ++diddirty;
88 }
89 }
90 if (!preen && su->su_nbytes != seg_table[i].su_nbytes) {
91 pwarn("SEGMENT %d CLAIMS %d BYTES BUT HAS %d",
92 i, su->su_nbytes, seg_table[i].su_nbytes);
93 if ((int32_t)su->su_nbytes >
94 (int32_t)seg_table[i].su_nbytes)
95 pwarn(" (HIGH BY %d)\n", su->su_nbytes -
96 seg_table[i].su_nbytes);
97 else
98 pwarn(" (LOW BY %d)\n", -su->su_nbytes +
99 seg_table[i].su_nbytes);
100 if (reply("FIX")) {
101 su->su_nbytes = seg_table[i].su_nbytes;
102 ++diddirty;
103 }
104 }
105 if (su->su_flags & SEGUSE_DIRTY) {
106 bb += lfs_btofsb(fs, su->su_nbytes +
107 su->su_nsums * lfs_sb_getsumsize(fs));
108 ubb += lfs_btofsb(fs, su->su_nbytes +
109 su->su_nsums * lfs_sb_getsumsize(fs) +
110 su->su_ninos * lfs_sb_getibsize(fs));
111 dmeta += lfs_btofsb(fs,
112 lfs_sb_getsumsize(fs) * su->su_nsums);
113 dmeta += lfs_btofsb(fs,
114 lfs_sb_getibsize(fs) * su->su_ninos);
115 } else {
116 nclean++;
117 avail += lfs_segtod(fs, 1);
118 if (su->su_flags & SEGUSE_SUPERBLOCK)
119 ++nsb;
120 if (i == 0 && lfs_sb_getversion(fs) > 1 &&
121 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
122 labelcorrect = lfs_btofsb(fs, LFS_LABELPAD) -
123 lfs_sb_gets0addr(fs);
124 }
125 if (diddirty)
126 VOP_BWRITE(bp);
127 else
128 brelse(bp, 0);
129 }
130
131 /* Also may be available bytes in current seg */
132 i = lfs_dtosn(fs, lfs_sb_getoffset(fs));
133 curr = lfs_sntod(fs, i + 1) - lfs_sb_getoffset(fs);
134 /* But do not count minfreesegs */
135 mfs = lfs_segtod(fs, (lfs_sb_getminfreeseg(fs) -
136 (lfs_sb_getminfreeseg(fs) / 2)));
137
138 avail = nclean * lfs_segtod(fs, 1);
139 avail -= nsb * lfs_btofsb(fs, LFS_SBPAD);
140 avail -= labelcorrect;
141 avail += curr;
142 avail -= mfs;
143
144 /* Note we may have bytes to write yet */
145 avail -= lfs_btofsb(fs, locked_queue_bytes);
146
147 if (debug)
148 pwarn("avail := clean %jd*%jd - sb %jd*%jd - lbl %jd"
149 "+ curr %jd - mfs %jd - locked %jd = %jd\n",
150 (intmax_t)nclean,
151 (intmax_t)lfs_segtod(fs, 1),
152 (intmax_t)nsb,
153 (intmax_t)lfs_btofsb(fs, LFS_SBPAD),
154 (intmax_t)labelcorrect,
155 (intmax_t)curr,
156 (intmax_t)mfs,
157 (intmax_t)lfs_btofsb(fs, locked_queue_bytes),
158 (intmax_t)avail);
159
160 if (idaddr)
161 pwarn("NOTE: when using -i, expect discrepancies in dmeta,"
162 " avail, nclean, bfree\n");
163 if (dmeta != lfs_sb_getdmeta(fs)) {
164 pwarn("DMETA GIVEN AS %d, SHOULD BE %ld\n",
165 lfs_sb_getdmeta(fs), dmeta);
166 if (preen || reply("FIX")) {
167 lfs_sb_setdmeta(fs, dmeta);
168 sbdirty();
169 }
170 }
171 if (avail != lfs_sb_getavail(fs) && !aflag) {
172 pwarn("AVAIL GIVEN AS %jd, SHOULD BE %jd\n",
173 (intmax_t)lfs_sb_getavail(fs), (intmax_t)avail);
174 if (preen || reply("FIX")) {
175 lfs_sb_setavail(fs, avail);
176 sbdirty();
177 }
178 }
179 if (nclean != lfs_sb_getnclean(fs)) {
180 pwarn("NCLEAN GIVEN AS %d, SHOULD BE %d\n", lfs_sb_getnclean(fs),
181 nclean);
182 if (preen || reply("FIX")) {
183 lfs_sb_setnclean(fs, nclean);
184 sbdirty();
185 }
186 }
187
188 labelskew = 0;
189 if (lfs_sb_getversion(fs) > 1 &&
190 lfs_sb_gets0addr(fs) < lfs_btofsb(fs, LFS_LABELPAD))
191 labelskew = lfs_btofsb(fs, LFS_LABELPAD);
192 if (lfs_sb_getbfree(fs) > lfs_sb_getdsize(fs) - bb - labelskew ||
193 lfs_sb_getbfree(fs) < lfs_sb_getdsize(fs) - ubb - labelskew) {
194 pwarn("BFREE GIVEN AS %jd, SHOULD BE BETWEEN %jd AND %jd\n",
195 (intmax_t)lfs_sb_getbfree(fs),
196 (intmax_t)(lfs_sb_getdsize(fs) - ubb - labelskew),
197 (intmax_t)(lfs_sb_getdsize(fs) - bb - labelskew));
198 if (preen || reply("FIX")) {
199 lfs_sb_setbfree(fs,
200 ((lfs_sb_getdsize(fs) - labelskew - ubb) +
201 lfs_sb_getdsize(fs) - labelskew - bb) / 2);
202 sbdirty();
203 }
204 }
205 }
206