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