Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sbin / fsck_ffs / wapbl.c
blobd9de5979c024d3e287cbd5a75601da26f8a3a1f4
1 /* $NetBSD: wapbl.c,v 1.3 2008/11/10 20:12:13 joerg Exp $ */
3 /*-
4 * Copyright (c) 2005,2008 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Wasabi Systems, Inc.
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.
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.
32 /* This file contains fsck support for wapbl
34 #define WAPBL_INTERNAL
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: wapbl.c,v 1.3 2008/11/10 20:12:13 joerg Exp $");
39 #include <sys/stat.h>
40 #include <sys/time.h>
41 #include <sys/uio.h>
43 #include <assert.h>
44 #include <errno.h>
45 #include <inttypes.h>
46 #include <stdio.h>
47 #include <stdbool.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <unistd.h>
52 #include <ufs/ufs/dinode.h>
53 #include <ufs/ufs/dir.h>
54 #include <ufs/ufs/ufs_bswap.h>
55 #include <ufs/ufs/ufs_wapbl.h>
56 #include <ufs/ffs/fs.h>
57 #include <ufs/ffs/ffs_extern.h>
59 #include <sys/wapbl.h>
61 #include "fsck.h"
62 #include "fsutil.h"
63 #include "extern.h"
64 #include "exitvalues.h"
66 int
67 wapbl_write(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
70 WAPBL_PRINTF(WAPBL_PRINT_IO,
71 ("wapbl_write: %zd bytes at block %"PRId64" on fd 0x%x\n",
72 len, pbn, fswritefd));
73 bwrite(fswritefd, data, pbn, len);
74 return 0;
77 int
78 wapbl_read(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
81 WAPBL_PRINTF(WAPBL_PRINT_IO,
82 ("wapbl_read: %zd bytes at block %"PRId64" on fd 0x%x\n",
83 len, pbn, fsreadfd));
84 bread(fsreadfd, data, pbn, len);
85 return 0;
88 struct wapbl_replay *wapbl_replay;
90 void
91 replay_wapbl(void)
93 int error;
95 if (!nflag) {
96 error = wapbl_replay_write(wapbl_replay, 0);
97 if (error) {
98 pfatal("UNABLE TO REPLAY JOURNAL BLOCKS");
99 if (reply("CONTINUE") == 0) {
100 exit(FSCK_EXIT_CHECK_FAILED);
102 } else {
103 wapbl_replay_stop(wapbl_replay);
107 int i;
108 for (i = 0; i < wapbl_replay->wr_inodescnt; i++) {
109 WAPBL_PRINTF(WAPBL_PRINT_REPLAY,("wapbl_replay: "
110 "not cleaning inode %"PRIu32" mode %"PRIo32"\n",
111 wapbl_replay->wr_inodes[i].wr_inumber,
112 wapbl_replay->wr_inodes[i].wr_imode));
117 void
118 cleanup_wapbl(void)
121 if (wapbl_replay) {
122 if (wapbl_replay_isopen(wapbl_replay))
123 wapbl_replay_stop(wapbl_replay);
124 wapbl_replay_free(wapbl_replay);
125 wapbl_replay = 0;
130 read_wapbl(char *buf, long size, daddr_t blk)
133 if (!wapbl_replay || !wapbl_replay_isopen(wapbl_replay))
134 return 0;
135 return wapbl_replay_read(wapbl_replay, buf, blk, size);
139 is_journal_inode(ino_t ino)
141 union dinode *dp;
143 dp = ginode(ino);
144 if ((iswap32(DIP(dp, flags)) & SF_LOG) != 0 &&
145 sblock->fs_journal_version == UFS_WAPBL_VERSION &&
146 sblock->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM &&
147 sblock->fs_journallocs[UFS_WAPBL_INFS_INO] == ino)
148 return 1;
150 return 0;
154 check_wapbl(void)
156 uint64_t addr = 0, count = 0, blksize = 0;
157 int error;
158 int ret = 0;
159 if (debug)
160 wapbl_debug_print = WAPBL_PRINT_ERROR | WAPBL_PRINT_REPLAY;
161 if (debug > 1)
162 wapbl_debug_print |= WAPBL_PRINT_IO;
164 if (sblock->fs_flags & FS_DOWAPBL) {
165 if (sblock->fs_journal_version != UFS_WAPBL_VERSION) {
166 pfatal("INVALID JOURNAL VERSION %d",
167 sblock->fs_journal_version);
168 if (reply("CONTINUE") == 0) {
169 exit(FSCK_EXIT_CHECK_FAILED);
171 pwarn("CLEARING EXISTING JOURNAL\n");
172 sblock->fs_flags &= ~FS_DOWAPBL;
173 sbdirty();
174 ret = FSCK_EXIT_CHECK_FAILED;
175 } else {
176 switch(sblock->fs_journal_location) {
177 case UFS_WAPBL_JOURNALLOC_END_PARTITION:
178 addr =
179 sblock->fs_journallocs[UFS_WAPBL_EPART_ADDR];
180 count =
181 sblock->fs_journallocs[UFS_WAPBL_EPART_COUNT];
182 blksize =
183 sblock->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
184 break;
185 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
186 addr =
187 sblock->fs_journallocs[UFS_WAPBL_INFS_ADDR];
188 count =
189 sblock->fs_journallocs[UFS_WAPBL_INFS_COUNT];
190 blksize =
191 sblock->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
192 break;
193 default:
194 pfatal("INVALID JOURNAL LOCATION %d",
195 sblock->fs_journal_location);
196 if (reply("CONTINUE") == 0) {
197 exit(FSCK_EXIT_CHECK_FAILED);
199 pwarn("CLEARING EXISTING JOURNAL\n");
200 sblock->fs_flags &= ~FS_DOWAPBL;
201 sblock->fs_journal_location =
202 UFS_WAPBL_JOURNALLOC_NONE;
203 sbdirty();
204 ret = FSCK_EXIT_CHECK_FAILED;
205 break;
207 if (sblock->fs_flags & FS_DOWAPBL) {
208 error = wapbl_replay_start(
209 &wapbl_replay, 0, addr, count, blksize);
210 if (error) {
211 pfatal(
212 "UNABLE TO READ JOURNAL FOR REPLAY");
213 if (reply("CONTINUE") == 0) {
214 exit(FSCK_EXIT_CHECK_FAILED);
216 pwarn("CLEARING EXISTING JOURNAL\n");
217 sblock->fs_flags &= ~FS_DOWAPBL;
218 sbdirty();
219 ret = FSCK_EXIT_CHECK_FAILED;
225 * at this time fs_journal_flags can only be 0,
226 * UFS_WAPBL_FLAGS_CREATE_LOG or UFS_WAPBL_FLAGS_CLEAR_LOG.
227 * We can't have both flags at the same time.
229 switch (sblock->fs_journal_flags) {
230 case 0:
231 break;
232 case UFS_WAPBL_FLAGS_CREATE_LOG:
233 case UFS_WAPBL_FLAGS_CLEAR_LOG:
234 switch(sblock->fs_journal_location) {
235 case UFS_WAPBL_JOURNALLOC_END_PARTITION:
236 case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
237 break;
238 case UFS_WAPBL_JOURNALLOC_NONE:
239 if (sblock->fs_journal_flags ==
240 UFS_WAPBL_FLAGS_CLEAR_LOG)
241 break;
242 /* FALLTHROUGH */
243 default:
244 pfatal("INVALID JOURNAL LOCATION %d",
245 sblock->fs_journal_location);
246 if (reply("CONTINUE") == 0) {
247 exit(FSCK_EXIT_CHECK_FAILED);
249 pwarn("CLEARING JOURNAL FLAGS\n");
250 sblock->fs_journal_flags = 0;
251 sblock->fs_journal_location =
252 UFS_WAPBL_JOURNALLOC_NONE;
253 sbdirty();
254 ret = FSCK_EXIT_CHECK_FAILED;
255 break;
257 break;
258 default:
259 pfatal("INVALID JOURNAL FLAGS %d",
260 sblock->fs_journal_flags);
261 if (reply("CONTINUE") == 0) {
262 exit(FSCK_EXIT_CHECK_FAILED);
264 pwarn("CLEARING JOURNAL FLAGS\n");
265 sblock->fs_journal_flags = 0;
266 sblock->fs_journal_location =
267 UFS_WAPBL_JOURNALLOC_NONE;
268 sbdirty();
269 ret = FSCK_EXIT_CHECK_FAILED;
270 break;
272 return ret;