xref: /netbsd-src/sbin/fsck_lfs/pass5.c (revision de1dfb1250df962f1ff3a011772cf58e605aed11)
1 /* $NetBSD: pass5.c,v 1.13 2004/05/14 10:41:12 yamt 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.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()
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.
82 	 */
83 	nclean = 0;
84 	avail = 0;
85 	bb = ubb = 0;
86 	dmeta = 0;
87 	for (i = 0; i < fs->lfs_nseg; i++) {
88 		diddirty = 0;
89 		LFS_SEGENTRY(su, fs, i, bp);
90 		if (!(su->su_flags & SEGUSE_DIRTY) &&
91 		    seg_table[i].su_nbytes > 0) {
92 			pwarn("%d bytes contained in 'clean' segment %d\n",
93 			    seg_table[i].su_nbytes, i);
94 			if (preen || reply("dirty segment")) {
95 				su->su_flags |= SEGUSE_DIRTY;
96 				++diddirty;
97 			}
98 		}
99 		if ((su->su_flags & SEGUSE_DIRTY) &&
100 		    su->su_nbytes != seg_table[i].su_nbytes) {
101 			pwarn("segment %d claims %d bytes but has %d",
102 			    i, su->su_nbytes, seg_table[i].su_nbytes);
103 			if ((int32_t)su->su_nbytes >
104 			    (int32_t)seg_table[i].su_nbytes)
105 				pwarn(" (high by %d)\n", su->su_nbytes -
106 				    seg_table[i].su_nbytes);
107 			else
108 				pwarn(" (low by %d)\n", -su->su_nbytes +
109 				    seg_table[i].su_nbytes);
110 			if (preen || reply("fix")) {
111 				su->su_nbytes = seg_table[i].su_nbytes;
112 				++diddirty;
113 			}
114 		}
115 		if (su->su_flags & SEGUSE_DIRTY) {
116 			bb += btofsb(fs, su->su_nbytes +
117 			    su->su_nsums * fs->lfs_sumsize);
118 			ubb += btofsb(fs, su->su_nbytes +
119 			    su->su_nsums * fs->lfs_sumsize +
120 			    su->su_ninos * fs->lfs_ibsize);
121 			dmeta += btofsb(fs,
122 			    fs->lfs_sumsize * su->su_nsums);
123 			dmeta += btofsb(fs,
124 			    fs->lfs_ibsize * su->su_ninos);
125 		} else {
126 			nclean++;
127 			avail += segtod(fs, 1);
128 			if (su->su_flags & SEGUSE_SUPERBLOCK)
129 				avail -= btofsb(fs, LFS_SBPAD);
130 			if (i == 0 && fs->lfs_version > 1 &&
131 			    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
132 				avail -= btofsb(fs, LFS_LABELPAD) -
133 				    fs->lfs_start;
134 		}
135 		if (diddirty)
136 			VOP_BWRITE(bp);
137 		else
138 			brelse(bp);
139 	}
140 
141 	/* Also may be available bytes in current seg */
142 	i = dtosn(fs, fs->lfs_offset);
143 	avail += sntod(fs, i + 1) - fs->lfs_offset;
144 	/* But do not count minfreesegs */
145 	avail -= segtod(fs, (fs->lfs_minfreeseg -
146 		(fs->lfs_minfreeseg / 2)));
147 	/* Note we may have bytes to write yet */
148 	avail -= btofsb(fs, locked_queue_bytes);
149 
150 	if (dmeta != fs->lfs_dmeta) {
151 		pwarn("dmeta given as %d, should be %ld\n", fs->lfs_dmeta,
152 		    dmeta);
153 		if (preen || reply("fix")) {
154 			fs->lfs_dmeta = dmeta;
155 			sbdirty();
156 		}
157 	}
158 	if (avail != fs->lfs_avail) {
159 		pwarn("avail given as %d, should be %ld\n", fs->lfs_avail,
160 		    avail);
161 		if (preen || reply("fix")) {
162 			fs->lfs_avail = avail;
163 			sbdirty();
164 		}
165 	}
166 	if (nclean != fs->lfs_nclean) {
167 		pwarn("nclean given as %d, should be %d\n", fs->lfs_nclean,
168 		    nclean);
169 		if (preen || reply("fix")) {
170 			fs->lfs_nclean = nclean;
171 			sbdirty();
172 		}
173 	}
174 
175 	labelskew = 0;
176 	if (fs->lfs_version > 1 &&
177 	    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
178 		labelskew = btofsb(fs, LFS_LABELPAD);
179 	if (fs->lfs_bfree > fs->lfs_dsize - bb - labelskew ||
180 	    fs->lfs_bfree < fs->lfs_dsize - ubb - labelskew) {
181 		pwarn("bfree given as %d, should be between %ld and %ld\n",
182 		    fs->lfs_bfree, fs->lfs_dsize - ubb - labelskew,
183 		    fs->lfs_dsize - bb - labelskew);
184 		if (preen || reply("fix")) {
185 			fs->lfs_bfree = fs->lfs_dsize - labelskew -
186 			    (ubb + bb) / 2;
187 			sbdirty();
188 		}
189 	}
190 }
191