xref: /netbsd-src/sbin/fsck_lfs/pass5.c (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1 /* $NetBSD: pass5.c,v 1.23 2007/10/08 21:39:49 ad 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the NetBSD
21  *      Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/time.h>
42 #include <sys/buf.h>
43 #include <sys/mount.h>
44 
45 #include <ufs/ufs/ufsmount.h>
46 #include <ufs/ufs/inode.h>
47 #include <ufs/ufs/dir.h>
48 #define vnode uvnode
49 #include <ufs/lfs/lfs.h>
50 #undef vnode
51 
52 #include <string.h>
53 
54 #include "bufcache.h"
55 #include "vnode.h"
56 #include "lfs_user.h"
57 
58 #include "fsck.h"
59 #include "extern.h"
60 #include "fsutil.h"
61 
62 extern SEGUSE *seg_table;
63 extern off_t locked_queue_bytes;
64 
65 void
66 pass5(void)
67 {
68 	SEGUSE *su;
69 	struct ubuf *bp;
70 	int i;
71 	unsigned long bb;	/* total number of used blocks (lower bound) */
72 	unsigned long ubb;	/* upper bound number of used blocks */
73 	unsigned long avail;	/* blocks available for writing */
74 	unsigned long dmeta;	/* blocks in segsums and inodes */
75 	int nclean;		/* clean segments */
76 	size_t labelskew;
77 	int diddirty;
78 
79 	/*
80 	 * Check segment holdings against actual holdings.  Check for
81 	 * "clean" segments that contain live data.  If we are only
82 	 * rolling forward, we can't check the segment holdings, but
83 	 * we can still check the cleanerinfo data.
84 	 */
85 	nclean = 0;
86 	avail = 0;
87 	bb = ubb = 0;
88 	dmeta = 0;
89 	for (i = 0; i < fs->lfs_nseg; i++) {
90 		diddirty = 0;
91 		LFS_SEGENTRY(su, fs, i, bp);
92 		if (!preen && !(su->su_flags & SEGUSE_DIRTY) &&
93 		    seg_table[i].su_nbytes > 0) {
94 			pwarn("CLEAN SEGMENT %d CONTAINS %d BYTES\n",
95 			    i, seg_table[i].su_nbytes);
96 			if (reply("MARK SEGMENT DIRTY")) {
97 				su->su_flags |= SEGUSE_DIRTY;
98 				++diddirty;
99 			}
100 		}
101 		if (!preen && su->su_nbytes != seg_table[i].su_nbytes) {
102 			pwarn("SEGMENT %d CLAIMS %d BYTES BUT HAS %d",
103 			    i, su->su_nbytes, seg_table[i].su_nbytes);
104 			if ((int32_t)su->su_nbytes >
105 			    (int32_t)seg_table[i].su_nbytes)
106 				pwarn(" (HIGH BY %d)\n", su->su_nbytes -
107 				    seg_table[i].su_nbytes);
108 			else
109 				pwarn(" (LOW BY %d)\n", -su->su_nbytes +
110 				    seg_table[i].su_nbytes);
111 			if (reply("FIX")) {
112 				su->su_nbytes = seg_table[i].su_nbytes;
113 				++diddirty;
114 			}
115 		}
116 		if (su->su_flags & SEGUSE_DIRTY) {
117 			bb += btofsb(fs, su->su_nbytes +
118 			    su->su_nsums * fs->lfs_sumsize);
119 			ubb += btofsb(fs, su->su_nbytes +
120 			    su->su_nsums * fs->lfs_sumsize +
121 			    su->su_ninos * fs->lfs_ibsize);
122 			dmeta += btofsb(fs,
123 			    fs->lfs_sumsize * su->su_nsums);
124 			dmeta += btofsb(fs,
125 			    fs->lfs_ibsize * su->su_ninos);
126 		} else {
127 			nclean++;
128 			avail += segtod(fs, 1);
129 			if (su->su_flags & SEGUSE_SUPERBLOCK)
130 				avail -= btofsb(fs, LFS_SBPAD);
131 			if (i == 0 && fs->lfs_version > 1 &&
132 			    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
133 				avail -= btofsb(fs, LFS_LABELPAD) -
134 				    fs->lfs_start;
135 		}
136 		if (diddirty)
137 			VOP_BWRITE(bp);
138 		else
139 			brelse(bp, 0);
140 	}
141 
142 	/* Also may be available bytes in current seg */
143 	i = dtosn(fs, fs->lfs_offset);
144 	avail += sntod(fs, i + 1) - fs->lfs_offset;
145 	/* But do not count minfreesegs */
146 	avail -= segtod(fs, (fs->lfs_minfreeseg -
147 		(fs->lfs_minfreeseg / 2)));
148 	/* Note we may have bytes to write yet */
149 	avail -= btofsb(fs, locked_queue_bytes);
150 
151 	if (idaddr)
152 		pwarn("NOTE: when using -i, expect discrepancies in dmeta,"
153 		      " avail, nclean, bfree\n");
154 	if (dmeta != fs->lfs_dmeta) {
155 		pwarn("DMETA GIVEN AS %d, SHOULD BE %ld\n", fs->lfs_dmeta,
156 		    dmeta);
157 		if (preen || reply("FIX")) {
158 			fs->lfs_dmeta = dmeta;
159 			sbdirty();
160 		}
161 	}
162 	if (avail != fs->lfs_avail) {
163 		pwarn("AVAIL GIVEN AS %d, SHOULD BE %ld\n", fs->lfs_avail,
164 		    avail);
165 		if (preen || reply("FIX")) {
166 			fs->lfs_avail = avail;
167 			sbdirty();
168 		}
169 	}
170 	if (nclean != fs->lfs_nclean) {
171 		pwarn("NCLEAN GIVEN AS %d, SHOULD BE %d\n", fs->lfs_nclean,
172 		    nclean);
173 		if (preen || reply("FIX")) {
174 			fs->lfs_nclean = nclean;
175 			sbdirty();
176 		}
177 	}
178 
179 	labelskew = 0;
180 	if (fs->lfs_version > 1 &&
181 	    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
182 		labelskew = btofsb(fs, LFS_LABELPAD);
183 	if (fs->lfs_bfree > fs->lfs_dsize - bb - labelskew ||
184 	    fs->lfs_bfree < fs->lfs_dsize - ubb - labelskew) {
185 		pwarn("BFREE GIVEN AS %d, SHOULD BE BETWEEN %ld AND %ld\n",
186 		    fs->lfs_bfree, (fs->lfs_dsize - ubb - labelskew),
187 		    fs->lfs_dsize - bb - labelskew);
188 		if (preen || reply("FIX")) {
189 			fs->lfs_bfree =
190 				((fs->lfs_dsize - labelskew - ubb) +
191 				 fs->lfs_dsize - labelskew - bb) / 2;
192 			sbdirty();
193 		}
194 	}
195 }
196