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