xref: /netbsd-src/sbin/fsck_lfs/pass5.c (revision 274254cdae52594c1aa480a736aef78313d15c9c)
1 /* $NetBSD: pass5.c,v 1.24 2008/04/28 20:23:08 martin 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 #include <ufs/ufs/ufsmount.h>
39 #include <ufs/ufs/inode.h>
40 #include <ufs/ufs/dir.h>
41 #define vnode uvnode
42 #include <ufs/lfs/lfs.h>
43 #undef vnode
44 
45 #include <string.h>
46 
47 #include "bufcache.h"
48 #include "vnode.h"
49 #include "lfs_user.h"
50 
51 #include "fsck.h"
52 #include "extern.h"
53 #include "fsutil.h"
54 
55 extern SEGUSE *seg_table;
56 extern off_t locked_queue_bytes;
57 
58 void
59 pass5(void)
60 {
61 	SEGUSE *su;
62 	struct ubuf *bp;
63 	int i;
64 	unsigned long bb;	/* total number of used blocks (lower bound) */
65 	unsigned long ubb;	/* upper bound number of used blocks */
66 	unsigned long avail;	/* blocks available for writing */
67 	unsigned long dmeta;	/* blocks in segsums and inodes */
68 	int nclean;		/* clean segments */
69 	size_t labelskew;
70 	int diddirty;
71 
72 	/*
73 	 * Check segment holdings against actual holdings.  Check for
74 	 * "clean" segments that contain live data.  If we are only
75 	 * rolling forward, we can't check the segment holdings, but
76 	 * we can still check the cleanerinfo data.
77 	 */
78 	nclean = 0;
79 	avail = 0;
80 	bb = ubb = 0;
81 	dmeta = 0;
82 	for (i = 0; i < fs->lfs_nseg; i++) {
83 		diddirty = 0;
84 		LFS_SEGENTRY(su, fs, i, bp);
85 		if (!preen && !(su->su_flags & SEGUSE_DIRTY) &&
86 		    seg_table[i].su_nbytes > 0) {
87 			pwarn("CLEAN SEGMENT %d CONTAINS %d BYTES\n",
88 			    i, seg_table[i].su_nbytes);
89 			if (reply("MARK SEGMENT DIRTY")) {
90 				su->su_flags |= SEGUSE_DIRTY;
91 				++diddirty;
92 			}
93 		}
94 		if (!preen && su->su_nbytes != seg_table[i].su_nbytes) {
95 			pwarn("SEGMENT %d CLAIMS %d BYTES BUT HAS %d",
96 			    i, su->su_nbytes, seg_table[i].su_nbytes);
97 			if ((int32_t)su->su_nbytes >
98 			    (int32_t)seg_table[i].su_nbytes)
99 				pwarn(" (HIGH BY %d)\n", su->su_nbytes -
100 				    seg_table[i].su_nbytes);
101 			else
102 				pwarn(" (LOW BY %d)\n", -su->su_nbytes +
103 				    seg_table[i].su_nbytes);
104 			if (reply("FIX")) {
105 				su->su_nbytes = seg_table[i].su_nbytes;
106 				++diddirty;
107 			}
108 		}
109 		if (su->su_flags & SEGUSE_DIRTY) {
110 			bb += btofsb(fs, su->su_nbytes +
111 			    su->su_nsums * fs->lfs_sumsize);
112 			ubb += btofsb(fs, su->su_nbytes +
113 			    su->su_nsums * fs->lfs_sumsize +
114 			    su->su_ninos * fs->lfs_ibsize);
115 			dmeta += btofsb(fs,
116 			    fs->lfs_sumsize * su->su_nsums);
117 			dmeta += btofsb(fs,
118 			    fs->lfs_ibsize * su->su_ninos);
119 		} else {
120 			nclean++;
121 			avail += segtod(fs, 1);
122 			if (su->su_flags & SEGUSE_SUPERBLOCK)
123 				avail -= btofsb(fs, LFS_SBPAD);
124 			if (i == 0 && fs->lfs_version > 1 &&
125 			    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
126 				avail -= btofsb(fs, LFS_LABELPAD) -
127 				    fs->lfs_start;
128 		}
129 		if (diddirty)
130 			VOP_BWRITE(bp);
131 		else
132 			brelse(bp, 0);
133 	}
134 
135 	/* Also may be available bytes in current seg */
136 	i = dtosn(fs, fs->lfs_offset);
137 	avail += sntod(fs, i + 1) - fs->lfs_offset;
138 	/* But do not count minfreesegs */
139 	avail -= segtod(fs, (fs->lfs_minfreeseg -
140 		(fs->lfs_minfreeseg / 2)));
141 	/* Note we may have bytes to write yet */
142 	avail -= btofsb(fs, locked_queue_bytes);
143 
144 	if (idaddr)
145 		pwarn("NOTE: when using -i, expect discrepancies in dmeta,"
146 		      " avail, nclean, bfree\n");
147 	if (dmeta != fs->lfs_dmeta) {
148 		pwarn("DMETA GIVEN AS %d, SHOULD BE %ld\n", fs->lfs_dmeta,
149 		    dmeta);
150 		if (preen || reply("FIX")) {
151 			fs->lfs_dmeta = dmeta;
152 			sbdirty();
153 		}
154 	}
155 	if (avail != fs->lfs_avail) {
156 		pwarn("AVAIL GIVEN AS %d, SHOULD BE %ld\n", fs->lfs_avail,
157 		    avail);
158 		if (preen || reply("FIX")) {
159 			fs->lfs_avail = avail;
160 			sbdirty();
161 		}
162 	}
163 	if (nclean != fs->lfs_nclean) {
164 		pwarn("NCLEAN GIVEN AS %d, SHOULD BE %d\n", fs->lfs_nclean,
165 		    nclean);
166 		if (preen || reply("FIX")) {
167 			fs->lfs_nclean = nclean;
168 			sbdirty();
169 		}
170 	}
171 
172 	labelskew = 0;
173 	if (fs->lfs_version > 1 &&
174 	    fs->lfs_start < btofsb(fs, LFS_LABELPAD))
175 		labelskew = btofsb(fs, LFS_LABELPAD);
176 	if (fs->lfs_bfree > fs->lfs_dsize - bb - labelskew ||
177 	    fs->lfs_bfree < fs->lfs_dsize - ubb - labelskew) {
178 		pwarn("BFREE GIVEN AS %d, SHOULD BE BETWEEN %ld AND %ld\n",
179 		    fs->lfs_bfree, (fs->lfs_dsize - ubb - labelskew),
180 		    fs->lfs_dsize - bb - labelskew);
181 		if (preen || reply("FIX")) {
182 			fs->lfs_bfree =
183 				((fs->lfs_dsize - labelskew - ubb) +
184 				 fs->lfs_dsize - labelskew - bb) / 2;
185 			sbdirty();
186 		}
187 	}
188 }
189