pass5.c revision 1.14 1 1.14 xtraeme /* $NetBSD: pass5.c,v 1.14 2005/01/19 19:41:59 xtraeme Exp $ */
2 1.4 perseant
3 1.4 perseant /*-
4 1.12 perseant * Copyright (c) 2000, 2003 The NetBSD Foundation, Inc.
5 1.4 perseant * All rights reserved.
6 1.4 perseant *
7 1.4 perseant * This code is derived from software contributed to The NetBSD Foundation
8 1.4 perseant * by Konrad E. Schroder <perseant (at) hhhh.org>.
9 1.4 perseant *
10 1.4 perseant * Redistribution and use in source and binary forms, with or without
11 1.4 perseant * modification, are permitted provided that the following conditions
12 1.4 perseant * are met:
13 1.4 perseant * 1. Redistributions of source code must retain the above copyright
14 1.4 perseant * notice, this list of conditions and the following disclaimer.
15 1.4 perseant * 2. Redistributions in binary form must reproduce the above copyright
16 1.4 perseant * notice, this list of conditions and the following disclaimer in the
17 1.4 perseant * documentation and/or other materials provided with the distribution.
18 1.4 perseant * 3. All advertising materials mentioning features or use of this software
19 1.4 perseant * must display the following acknowledgement:
20 1.4 perseant * This product includes software developed by the NetBSD
21 1.4 perseant * Foundation, Inc. and its contributors.
22 1.4 perseant * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.4 perseant * contributors may be used to endorse or promote products derived
24 1.4 perseant * from this software without specific prior written permission.
25 1.4 perseant *
26 1.4 perseant * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.4 perseant * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.4 perseant * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.4 perseant * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.4 perseant * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.4 perseant * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.4 perseant * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.4 perseant * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.4 perseant * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.4 perseant * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.4 perseant * POSSIBILITY OF SUCH DAMAGE.
37 1.4 perseant */
38 1.1 perseant
39 1.12 perseant #include <sys/types.h>
40 1.1 perseant #include <sys/param.h>
41 1.1 perseant #include <sys/time.h>
42 1.12 perseant #include <sys/buf.h>
43 1.12 perseant #include <sys/mount.h>
44 1.12 perseant
45 1.12 perseant #include <ufs/ufs/ufsmount.h>
46 1.12 perseant #include <ufs/ufs/inode.h>
47 1.3 perseant #include <ufs/ufs/dir.h>
48 1.12 perseant #define vnode uvnode
49 1.3 perseant #include <ufs/lfs/lfs.h>
50 1.12 perseant #undef vnode
51 1.3 perseant
52 1.1 perseant #include <string.h>
53 1.12 perseant
54 1.12 perseant #include "bufcache.h"
55 1.12 perseant #include "vnode.h"
56 1.12 perseant #include "lfs.h"
57 1.12 perseant
58 1.1 perseant #include "fsck.h"
59 1.1 perseant #include "extern.h"
60 1.3 perseant #include "fsutil.h"
61 1.3 perseant
62 1.12 perseant extern SEGUSE *seg_table;
63 1.12 perseant extern off_t locked_queue_bytes;
64 1.1 perseant
65 1.1 perseant void
66 1.14 xtraeme pass5(void)
67 1.1 perseant {
68 1.12 perseant SEGUSE *su;
69 1.13 yamt struct ubuf *bp;
70 1.12 perseant int i;
71 1.12 perseant unsigned long bb; /* total number of used blocks (lower bound) */
72 1.12 perseant unsigned long ubb; /* upper bound number of used blocks */
73 1.12 perseant unsigned long avail; /* blocks available for writing */
74 1.12 perseant unsigned long dmeta; /* blocks in segsums and inodes */
75 1.12 perseant int nclean; /* clean segments */
76 1.12 perseant size_t labelskew;
77 1.12 perseant int diddirty;
78 1.3 perseant
79 1.3 perseant /*
80 1.3 perseant * Check segment holdings against actual holdings. Check for
81 1.3 perseant * "clean" segments that contain live data.
82 1.3 perseant */
83 1.7 perseant nclean = 0;
84 1.6 perseant avail = 0;
85 1.6 perseant bb = ubb = 0;
86 1.8 perseant dmeta = 0;
87 1.12 perseant for (i = 0; i < fs->lfs_nseg; i++) {
88 1.12 perseant diddirty = 0;
89 1.12 perseant LFS_SEGENTRY(su, fs, i, bp);
90 1.3 perseant if (!(su->su_flags & SEGUSE_DIRTY) &&
91 1.4 perseant seg_table[i].su_nbytes > 0) {
92 1.3 perseant pwarn("%d bytes contained in 'clean' segment %d\n",
93 1.12 perseant seg_table[i].su_nbytes, i);
94 1.12 perseant if (preen || reply("dirty segment")) {
95 1.3 perseant su->su_flags |= SEGUSE_DIRTY;
96 1.12 perseant ++diddirty;
97 1.3 perseant }
98 1.3 perseant }
99 1.3 perseant if ((su->su_flags & SEGUSE_DIRTY) &&
100 1.4 perseant su->su_nbytes != seg_table[i].su_nbytes) {
101 1.5 perseant pwarn("segment %d claims %d bytes but has %d",
102 1.12 perseant i, su->su_nbytes, seg_table[i].su_nbytes);
103 1.12 perseant if ((int32_t)su->su_nbytes >
104 1.12 perseant (int32_t)seg_table[i].su_nbytes)
105 1.5 perseant pwarn(" (high by %d)\n", su->su_nbytes -
106 1.12 perseant seg_table[i].su_nbytes);
107 1.5 perseant else
108 1.12 perseant pwarn(" (low by %d)\n", -su->su_nbytes +
109 1.12 perseant seg_table[i].su_nbytes);
110 1.4 perseant if (preen || reply("fix")) {
111 1.3 perseant su->su_nbytes = seg_table[i].su_nbytes;
112 1.12 perseant ++diddirty;
113 1.1 perseant }
114 1.1 perseant }
115 1.6 perseant if (su->su_flags & SEGUSE_DIRTY) {
116 1.12 perseant bb += btofsb(fs, su->su_nbytes +
117 1.12 perseant su->su_nsums * fs->lfs_sumsize);
118 1.12 perseant ubb += btofsb(fs, su->su_nbytes +
119 1.12 perseant su->su_nsums * fs->lfs_sumsize +
120 1.12 perseant su->su_ninos * fs->lfs_ibsize);
121 1.12 perseant dmeta += btofsb(fs,
122 1.12 perseant fs->lfs_sumsize * su->su_nsums);
123 1.12 perseant dmeta += btofsb(fs,
124 1.12 perseant fs->lfs_ibsize * su->su_ninos);
125 1.6 perseant } else {
126 1.7 perseant nclean++;
127 1.12 perseant avail += segtod(fs, 1);
128 1.6 perseant if (su->su_flags & SEGUSE_SUPERBLOCK)
129 1.12 perseant avail -= btofsb(fs, LFS_SBPAD);
130 1.12 perseant if (i == 0 && fs->lfs_version > 1 &&
131 1.12 perseant fs->lfs_start < btofsb(fs, LFS_LABELPAD))
132 1.12 perseant avail -= btofsb(fs, LFS_LABELPAD) -
133 1.12 perseant fs->lfs_start;
134 1.6 perseant }
135 1.12 perseant if (diddirty)
136 1.12 perseant VOP_BWRITE(bp);
137 1.12 perseant else
138 1.12 perseant brelse(bp);
139 1.6 perseant }
140 1.12 perseant
141 1.6 perseant /* Also may be available bytes in current seg */
142 1.12 perseant i = dtosn(fs, fs->lfs_offset);
143 1.12 perseant avail += sntod(fs, i + 1) - fs->lfs_offset;
144 1.6 perseant /* But do not count minfreesegs */
145 1.12 perseant avail -= segtod(fs, (fs->lfs_minfreeseg -
146 1.12 perseant (fs->lfs_minfreeseg / 2)));
147 1.12 perseant /* Note we may have bytes to write yet */
148 1.12 perseant avail -= btofsb(fs, locked_queue_bytes);
149 1.12 perseant
150 1.12 perseant if (dmeta != fs->lfs_dmeta) {
151 1.12 perseant pwarn("dmeta given as %d, should be %ld\n", fs->lfs_dmeta,
152 1.12 perseant dmeta);
153 1.12 perseant if (preen || reply("fix")) {
154 1.12 perseant fs->lfs_dmeta = dmeta;
155 1.12 perseant sbdirty();
156 1.12 perseant }
157 1.8 perseant }
158 1.13 yamt if (avail != fs->lfs_avail) {
159 1.12 perseant pwarn("avail given as %d, should be %ld\n", fs->lfs_avail,
160 1.12 perseant avail);
161 1.6 perseant if (preen || reply("fix")) {
162 1.12 perseant fs->lfs_avail = avail;
163 1.7 perseant sbdirty();
164 1.7 perseant }
165 1.7 perseant }
166 1.12 perseant if (nclean != fs->lfs_nclean) {
167 1.12 perseant pwarn("nclean given as %d, should be %d\n", fs->lfs_nclean,
168 1.12 perseant nclean);
169 1.7 perseant if (preen || reply("fix")) {
170 1.12 perseant fs->lfs_nclean = nclean;
171 1.6 perseant sbdirty();
172 1.6 perseant }
173 1.6 perseant }
174 1.12 perseant
175 1.11 perseant labelskew = 0;
176 1.12 perseant if (fs->lfs_version > 1 &&
177 1.12 perseant fs->lfs_start < btofsb(fs, LFS_LABELPAD))
178 1.12 perseant labelskew = btofsb(fs, LFS_LABELPAD);
179 1.12 perseant if (fs->lfs_bfree > fs->lfs_dsize - bb - labelskew ||
180 1.12 perseant fs->lfs_bfree < fs->lfs_dsize - ubb - labelskew) {
181 1.6 perseant pwarn("bfree given as %d, should be between %ld and %ld\n",
182 1.12 perseant fs->lfs_bfree, fs->lfs_dsize - ubb - labelskew,
183 1.12 perseant fs->lfs_dsize - bb - labelskew);
184 1.6 perseant if (preen || reply("fix")) {
185 1.12 perseant fs->lfs_bfree = fs->lfs_dsize - labelskew -
186 1.12 perseant (ubb + bb) / 2;
187 1.6 perseant sbdirty();
188 1.6 perseant }
189 1.1 perseant }
190 1.1 perseant }
191