wapbl.c revision 1.3 1 1.3 joerg /* $NetBSD: wapbl.c,v 1.3 2008/11/10 20:12:13 joerg 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.3 joerg __KERNEL_RCSID(0, "$NetBSD: wapbl.c,v 1.3 2008/11/10 20:12:13 joerg 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 uint64_t addr, count, blksize;
94 1.2 simonb int error;
95 1.2 simonb
96 1.2 simonb if (debug)
97 1.2 simonb wapbl_debug_print = WAPBL_PRINT_ERROR | WAPBL_PRINT_REPLAY;
98 1.2 simonb if (debug > 1)
99 1.2 simonb wapbl_debug_print |= WAPBL_PRINT_IO;
100 1.2 simonb
101 1.2 simonb if (sblock->fs_journal_version != UFS_WAPBL_VERSION) {
102 1.2 simonb pfatal("INVALID JOURNAL VERSION %d",
103 1.2 simonb sblock->fs_journal_version);
104 1.2 simonb if (reply("CONTINUE") == 0) {
105 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
106 1.2 simonb }
107 1.2 simonb return;
108 1.2 simonb }
109 1.2 simonb
110 1.2 simonb switch (sblock->fs_journal_location) {
111 1.2 simonb case UFS_WAPBL_JOURNALLOC_NONE:
112 1.2 simonb pfatal("INVALID JOURNAL LOCATION 'NONE'");
113 1.2 simonb if (reply("CONTINUE") == 0) {
114 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
115 1.2 simonb }
116 1.2 simonb return;
117 1.2 simonb
118 1.2 simonb case UFS_WAPBL_JOURNALLOC_END_PARTITION:
119 1.2 simonb addr = sblock->fs_journallocs[UFS_WAPBL_EPART_ADDR];
120 1.2 simonb count = sblock->fs_journallocs[UFS_WAPBL_EPART_COUNT];
121 1.2 simonb blksize = sblock->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
122 1.2 simonb break;
123 1.2 simonb
124 1.2 simonb case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
125 1.2 simonb addr = sblock->fs_journallocs[UFS_WAPBL_INFS_ADDR];
126 1.2 simonb count = sblock->fs_journallocs[UFS_WAPBL_INFS_COUNT];
127 1.2 simonb blksize = sblock->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
128 1.2 simonb break;
129 1.2 simonb
130 1.2 simonb default:
131 1.2 simonb pfatal("INVALID JOURNAL LOCATION %d",
132 1.2 simonb sblock->fs_journal_location);
133 1.2 simonb if (reply("CONTINUE") == 0) {
134 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
135 1.2 simonb }
136 1.2 simonb return;
137 1.2 simonb }
138 1.2 simonb
139 1.2 simonb error = wapbl_replay_start(&wapbl_replay, 0, addr, count, blksize);
140 1.2 simonb if (error) {
141 1.2 simonb pfatal("UNABLE TO READ JOURNAL FOR REPLAY");
142 1.2 simonb if (reply("CONTINUE") == 0) {
143 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
144 1.2 simonb }
145 1.2 simonb return;
146 1.2 simonb }
147 1.2 simonb if (!nflag) {
148 1.2 simonb error = wapbl_replay_write(wapbl_replay, 0);
149 1.2 simonb if (error) {
150 1.2 simonb pfatal("UNABLE TO REPLAY JOURNAL BLOCKS");
151 1.2 simonb if (reply("CONTINUE") == 0) {
152 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
153 1.2 simonb }
154 1.2 simonb } else {
155 1.2 simonb wapbl_replay_stop(wapbl_replay);
156 1.2 simonb }
157 1.2 simonb }
158 1.2 simonb {
159 1.2 simonb int i;
160 1.2 simonb for (i = 0; i < wapbl_replay->wr_inodescnt; i++) {
161 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_REPLAY,("wapbl_replay: "
162 1.2 simonb "not cleaning inode %"PRIu32" mode %"PRIo32"\n",
163 1.2 simonb wapbl_replay->wr_inodes[i].wr_inumber,
164 1.2 simonb wapbl_replay->wr_inodes[i].wr_imode));
165 1.2 simonb }
166 1.2 simonb }
167 1.2 simonb }
168 1.2 simonb
169 1.2 simonb void
170 1.2 simonb cleanup_wapbl(void)
171 1.2 simonb {
172 1.2 simonb
173 1.2 simonb if (wapbl_replay) {
174 1.2 simonb if (wapbl_replay_isopen(wapbl_replay))
175 1.2 simonb wapbl_replay_stop(wapbl_replay);
176 1.2 simonb wapbl_replay_free(wapbl_replay);
177 1.2 simonb wapbl_replay = 0;
178 1.2 simonb }
179 1.2 simonb }
180 1.2 simonb
181 1.2 simonb int
182 1.2 simonb read_wapbl(char *buf, long size, daddr_t blk)
183 1.2 simonb {
184 1.2 simonb
185 1.2 simonb if (!wapbl_replay || !wapbl_replay_isopen(wapbl_replay))
186 1.2 simonb return 0;
187 1.2 simonb return wapbl_replay_read(wapbl_replay, buf, blk, size);
188 1.2 simonb }
189 1.2 simonb
190 1.2 simonb int
191 1.2 simonb is_journal_inode(ino_t ino)
192 1.2 simonb {
193 1.2 simonb union dinode *dp;
194 1.2 simonb
195 1.2 simonb dp = ginode(ino);
196 1.2 simonb if ((iswap32(DIP(dp, flags)) & SF_LOG) != 0 &&
197 1.2 simonb sblock->fs_journal_version == UFS_WAPBL_VERSION &&
198 1.2 simonb sblock->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM &&
199 1.2 simonb sblock->fs_journallocs[UFS_WAPBL_INFS_INO] == ino)
200 1.2 simonb return 1;
201 1.2 simonb
202 1.2 simonb return 0;
203 1.2 simonb }
204