wapbl.c revision 1.5 1 1.5 mlelstv /* $NetBSD: wapbl.c,v 1.5 2010/03/06 11:31:40 mlelstv Exp $ */
2 1.2 simonb
3 1.2 simonb /*-
4 1.2 simonb * Copyright (c) 2005,2008 The NetBSD Foundation, Inc.
5 1.2 simonb * All rights reserved.
6 1.2 simonb *
7 1.2 simonb * This code is derived from software contributed to The NetBSD Foundation
8 1.2 simonb * by Wasabi Systems, Inc.
9 1.2 simonb *
10 1.2 simonb * Redistribution and use in source and binary forms, with or without
11 1.2 simonb * modification, are permitted provided that the following conditions
12 1.2 simonb * are met:
13 1.2 simonb * 1. Redistributions of source code must retain the above copyright
14 1.2 simonb * notice, this list of conditions and the following disclaimer.
15 1.2 simonb * 2. Redistributions in binary form must reproduce the above copyright
16 1.2 simonb * notice, this list of conditions and the following disclaimer in the
17 1.2 simonb * documentation and/or other materials provided with the distribution.
18 1.2 simonb *
19 1.2 simonb * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.2 simonb * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.2 simonb * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.2 simonb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.2 simonb * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.2 simonb * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.2 simonb * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.2 simonb * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.2 simonb * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.2 simonb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.2 simonb * POSSIBILITY OF SUCH DAMAGE.
30 1.2 simonb */
31 1.2 simonb
32 1.2 simonb /* This file contains fsck support for wapbl
33 1.2 simonb */
34 1.3 joerg #define WAPBL_INTERNAL
35 1.2 simonb
36 1.2 simonb #include <sys/cdefs.h>
37 1.5 mlelstv __KERNEL_RCSID(0, "$NetBSD: wapbl.c,v 1.5 2010/03/06 11:31:40 mlelstv Exp $");
38 1.2 simonb
39 1.2 simonb #include <sys/stat.h>
40 1.2 simonb #include <sys/time.h>
41 1.2 simonb #include <sys/uio.h>
42 1.2 simonb
43 1.2 simonb #include <assert.h>
44 1.2 simonb #include <errno.h>
45 1.2 simonb #include <inttypes.h>
46 1.2 simonb #include <stdio.h>
47 1.2 simonb #include <stdbool.h>
48 1.2 simonb #include <stdlib.h>
49 1.2 simonb #include <string.h>
50 1.2 simonb #include <unistd.h>
51 1.2 simonb
52 1.2 simonb #include <ufs/ufs/dinode.h>
53 1.2 simonb #include <ufs/ufs/dir.h>
54 1.2 simonb #include <ufs/ufs/ufs_bswap.h>
55 1.2 simonb #include <ufs/ufs/ufs_wapbl.h>
56 1.2 simonb #include <ufs/ffs/fs.h>
57 1.2 simonb #include <ufs/ffs/ffs_extern.h>
58 1.2 simonb
59 1.2 simonb #include <sys/wapbl.h>
60 1.2 simonb
61 1.2 simonb #include "fsck.h"
62 1.2 simonb #include "fsutil.h"
63 1.2 simonb #include "extern.h"
64 1.2 simonb #include "exitvalues.h"
65 1.2 simonb
66 1.2 simonb int
67 1.2 simonb wapbl_write(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
68 1.2 simonb {
69 1.2 simonb
70 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_IO,
71 1.2 simonb ("wapbl_write: %zd bytes at block %"PRId64" on fd 0x%x\n",
72 1.2 simonb len, pbn, fswritefd));
73 1.2 simonb bwrite(fswritefd, data, pbn, len);
74 1.2 simonb return 0;
75 1.2 simonb }
76 1.2 simonb
77 1.2 simonb int
78 1.2 simonb wapbl_read(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
79 1.2 simonb {
80 1.2 simonb
81 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_IO,
82 1.2 simonb ("wapbl_read: %zd bytes at block %"PRId64" on fd 0x%x\n",
83 1.2 simonb len, pbn, fsreadfd));
84 1.2 simonb bread(fsreadfd, data, pbn, len);
85 1.2 simonb return 0;
86 1.2 simonb }
87 1.2 simonb
88 1.2 simonb struct wapbl_replay *wapbl_replay;
89 1.2 simonb
90 1.2 simonb void
91 1.2 simonb replay_wapbl(void)
92 1.2 simonb {
93 1.2 simonb int error;
94 1.2 simonb
95 1.2 simonb if (!nflag) {
96 1.2 simonb error = wapbl_replay_write(wapbl_replay, 0);
97 1.2 simonb if (error) {
98 1.2 simonb pfatal("UNABLE TO REPLAY JOURNAL BLOCKS");
99 1.2 simonb if (reply("CONTINUE") == 0) {
100 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
101 1.2 simonb }
102 1.2 simonb } else {
103 1.2 simonb wapbl_replay_stop(wapbl_replay);
104 1.2 simonb }
105 1.2 simonb }
106 1.2 simonb {
107 1.2 simonb int i;
108 1.2 simonb for (i = 0; i < wapbl_replay->wr_inodescnt; i++) {
109 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_REPLAY,("wapbl_replay: "
110 1.2 simonb "not cleaning inode %"PRIu32" mode %"PRIo32"\n",
111 1.2 simonb wapbl_replay->wr_inodes[i].wr_inumber,
112 1.2 simonb wapbl_replay->wr_inodes[i].wr_imode));
113 1.2 simonb }
114 1.2 simonb }
115 1.2 simonb }
116 1.2 simonb
117 1.2 simonb void
118 1.2 simonb cleanup_wapbl(void)
119 1.2 simonb {
120 1.2 simonb
121 1.2 simonb if (wapbl_replay) {
122 1.2 simonb if (wapbl_replay_isopen(wapbl_replay))
123 1.2 simonb wapbl_replay_stop(wapbl_replay);
124 1.2 simonb wapbl_replay_free(wapbl_replay);
125 1.2 simonb wapbl_replay = 0;
126 1.2 simonb }
127 1.2 simonb }
128 1.2 simonb
129 1.2 simonb int
130 1.2 simonb read_wapbl(char *buf, long size, daddr_t blk)
131 1.2 simonb {
132 1.2 simonb
133 1.2 simonb if (!wapbl_replay || !wapbl_replay_isopen(wapbl_replay))
134 1.2 simonb return 0;
135 1.2 simonb return wapbl_replay_read(wapbl_replay, buf, blk, size);
136 1.2 simonb }
137 1.2 simonb
138 1.2 simonb int
139 1.2 simonb is_journal_inode(ino_t ino)
140 1.2 simonb {
141 1.2 simonb union dinode *dp;
142 1.2 simonb
143 1.2 simonb dp = ginode(ino);
144 1.2 simonb if ((iswap32(DIP(dp, flags)) & SF_LOG) != 0 &&
145 1.2 simonb sblock->fs_journal_version == UFS_WAPBL_VERSION &&
146 1.2 simonb sblock->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM &&
147 1.2 simonb sblock->fs_journallocs[UFS_WAPBL_INFS_INO] == ino)
148 1.2 simonb return 1;
149 1.2 simonb
150 1.2 simonb return 0;
151 1.2 simonb }
152 1.4 bouyer
153 1.4 bouyer int
154 1.4 bouyer check_wapbl(void)
155 1.4 bouyer {
156 1.4 bouyer uint64_t addr = 0, count = 0, blksize = 0;
157 1.4 bouyer int error;
158 1.4 bouyer int ret = 0;
159 1.4 bouyer if (debug)
160 1.4 bouyer wapbl_debug_print = WAPBL_PRINT_ERROR | WAPBL_PRINT_REPLAY;
161 1.4 bouyer if (debug > 1)
162 1.4 bouyer wapbl_debug_print |= WAPBL_PRINT_IO;
163 1.4 bouyer
164 1.4 bouyer if (sblock->fs_flags & FS_DOWAPBL) {
165 1.4 bouyer if (sblock->fs_journal_version != UFS_WAPBL_VERSION) {
166 1.4 bouyer pfatal("INVALID JOURNAL VERSION %d",
167 1.4 bouyer sblock->fs_journal_version);
168 1.4 bouyer if (reply("CONTINUE") == 0) {
169 1.4 bouyer exit(FSCK_EXIT_CHECK_FAILED);
170 1.4 bouyer }
171 1.4 bouyer pwarn("CLEARING EXISTING JOURNAL\n");
172 1.4 bouyer sblock->fs_flags &= ~FS_DOWAPBL;
173 1.5 mlelstv sblock->fs_journal_flags = UFS_WAPBL_FLAGS_CLEAR_LOG;
174 1.4 bouyer sbdirty();
175 1.4 bouyer ret = FSCK_EXIT_CHECK_FAILED;
176 1.4 bouyer } else {
177 1.4 bouyer switch(sblock->fs_journal_location) {
178 1.4 bouyer case UFS_WAPBL_JOURNALLOC_END_PARTITION:
179 1.4 bouyer addr =
180 1.4 bouyer sblock->fs_journallocs[UFS_WAPBL_EPART_ADDR];
181 1.4 bouyer count =
182 1.4 bouyer sblock->fs_journallocs[UFS_WAPBL_EPART_COUNT];
183 1.4 bouyer blksize =
184 1.4 bouyer sblock->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
185 1.4 bouyer break;
186 1.4 bouyer case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
187 1.4 bouyer addr =
188 1.4 bouyer sblock->fs_journallocs[UFS_WAPBL_INFS_ADDR];
189 1.4 bouyer count =
190 1.4 bouyer sblock->fs_journallocs[UFS_WAPBL_INFS_COUNT];
191 1.4 bouyer blksize =
192 1.4 bouyer sblock->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
193 1.4 bouyer break;
194 1.4 bouyer default:
195 1.4 bouyer pfatal("INVALID JOURNAL LOCATION %d",
196 1.4 bouyer sblock->fs_journal_location);
197 1.4 bouyer if (reply("CONTINUE") == 0) {
198 1.4 bouyer exit(FSCK_EXIT_CHECK_FAILED);
199 1.4 bouyer }
200 1.4 bouyer pwarn("CLEARING EXISTING JOURNAL\n");
201 1.4 bouyer sblock->fs_flags &= ~FS_DOWAPBL;
202 1.5 mlelstv sblock->fs_journal_flags = UFS_WAPBL_FLAGS_CLEAR_LOG;
203 1.4 bouyer sblock->fs_journal_location =
204 1.4 bouyer UFS_WAPBL_JOURNALLOC_NONE;
205 1.4 bouyer sbdirty();
206 1.4 bouyer ret = FSCK_EXIT_CHECK_FAILED;
207 1.4 bouyer break;
208 1.4 bouyer }
209 1.4 bouyer if (sblock->fs_flags & FS_DOWAPBL) {
210 1.4 bouyer error = wapbl_replay_start(
211 1.4 bouyer &wapbl_replay, 0, addr, count, blksize);
212 1.4 bouyer if (error) {
213 1.4 bouyer pfatal(
214 1.4 bouyer "UNABLE TO READ JOURNAL FOR REPLAY");
215 1.4 bouyer if (reply("CONTINUE") == 0) {
216 1.4 bouyer exit(FSCK_EXIT_CHECK_FAILED);
217 1.4 bouyer }
218 1.4 bouyer pwarn("CLEARING EXISTING JOURNAL\n");
219 1.4 bouyer sblock->fs_flags &= ~FS_DOWAPBL;
220 1.5 mlelstv sblock->fs_journal_flags = UFS_WAPBL_FLAGS_CLEAR_LOG;
221 1.4 bouyer sbdirty();
222 1.4 bouyer ret = FSCK_EXIT_CHECK_FAILED;
223 1.4 bouyer }
224 1.4 bouyer }
225 1.4 bouyer }
226 1.4 bouyer }
227 1.4 bouyer /*
228 1.4 bouyer * at this time fs_journal_flags can only be 0,
229 1.4 bouyer * UFS_WAPBL_FLAGS_CREATE_LOG or UFS_WAPBL_FLAGS_CLEAR_LOG.
230 1.4 bouyer * We can't have both flags at the same time.
231 1.4 bouyer */
232 1.4 bouyer switch (sblock->fs_journal_flags) {
233 1.4 bouyer case 0:
234 1.4 bouyer break;
235 1.4 bouyer case UFS_WAPBL_FLAGS_CREATE_LOG:
236 1.4 bouyer case UFS_WAPBL_FLAGS_CLEAR_LOG:
237 1.4 bouyer switch(sblock->fs_journal_location) {
238 1.4 bouyer case UFS_WAPBL_JOURNALLOC_END_PARTITION:
239 1.4 bouyer case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
240 1.4 bouyer break;
241 1.4 bouyer case UFS_WAPBL_JOURNALLOC_NONE:
242 1.4 bouyer if (sblock->fs_journal_flags ==
243 1.4 bouyer UFS_WAPBL_FLAGS_CLEAR_LOG)
244 1.4 bouyer break;
245 1.4 bouyer /* FALLTHROUGH */
246 1.4 bouyer default:
247 1.4 bouyer pfatal("INVALID JOURNAL LOCATION %d",
248 1.4 bouyer sblock->fs_journal_location);
249 1.4 bouyer if (reply("CONTINUE") == 0) {
250 1.4 bouyer exit(FSCK_EXIT_CHECK_FAILED);
251 1.4 bouyer }
252 1.4 bouyer pwarn("CLEARING JOURNAL FLAGS\n");
253 1.4 bouyer sblock->fs_journal_flags = 0;
254 1.4 bouyer sblock->fs_journal_location =
255 1.4 bouyer UFS_WAPBL_JOURNALLOC_NONE;
256 1.4 bouyer sbdirty();
257 1.4 bouyer ret = FSCK_EXIT_CHECK_FAILED;
258 1.4 bouyer break;
259 1.4 bouyer }
260 1.4 bouyer break;
261 1.4 bouyer default:
262 1.4 bouyer pfatal("INVALID JOURNAL FLAGS %d",
263 1.4 bouyer sblock->fs_journal_flags);
264 1.4 bouyer if (reply("CONTINUE") == 0) {
265 1.4 bouyer exit(FSCK_EXIT_CHECK_FAILED);
266 1.4 bouyer }
267 1.4 bouyer pwarn("CLEARING JOURNAL FLAGS\n");
268 1.4 bouyer sblock->fs_journal_flags = 0;
269 1.4 bouyer sblock->fs_journal_location =
270 1.4 bouyer UFS_WAPBL_JOURNALLOC_NONE;
271 1.4 bouyer sbdirty();
272 1.4 bouyer ret = FSCK_EXIT_CHECK_FAILED;
273 1.4 bouyer break;
274 1.4 bouyer }
275 1.4 bouyer return ret;
276 1.4 bouyer }
277