wapbl.c revision 1.2 1 1.2 simonb /* $NetBSD: wapbl.c,v 1.2 2008/07/31 05:38:04 simonb 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.2 simonb
35 1.2 simonb #include <sys/cdefs.h>
36 1.2 simonb __KERNEL_RCSID(0, "$NetBSD: wapbl.c,v 1.2 2008/07/31 05:38:04 simonb Exp $");
37 1.2 simonb
38 1.2 simonb #include <sys/stat.h>
39 1.2 simonb #include <sys/time.h>
40 1.2 simonb #include <sys/uio.h>
41 1.2 simonb
42 1.2 simonb #include <assert.h>
43 1.2 simonb #include <errno.h>
44 1.2 simonb #include <inttypes.h>
45 1.2 simonb #include <stdio.h>
46 1.2 simonb #include <stdbool.h>
47 1.2 simonb #include <stdlib.h>
48 1.2 simonb #include <string.h>
49 1.2 simonb #include <unistd.h>
50 1.2 simonb
51 1.2 simonb #include <ufs/ufs/dinode.h>
52 1.2 simonb #include <ufs/ufs/dir.h>
53 1.2 simonb #include <ufs/ufs/ufs_bswap.h>
54 1.2 simonb #include <ufs/ufs/ufs_wapbl.h>
55 1.2 simonb #include <ufs/ffs/fs.h>
56 1.2 simonb #include <ufs/ffs/ffs_extern.h>
57 1.2 simonb
58 1.2 simonb #include <sys/wapbl.h>
59 1.2 simonb
60 1.2 simonb #include "fsck.h"
61 1.2 simonb #include "fsutil.h"
62 1.2 simonb #include "extern.h"
63 1.2 simonb #include "exitvalues.h"
64 1.2 simonb
65 1.2 simonb int
66 1.2 simonb wapbl_write(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
67 1.2 simonb {
68 1.2 simonb
69 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_IO,
70 1.2 simonb ("wapbl_write: %zd bytes at block %"PRId64" on fd 0x%x\n",
71 1.2 simonb len, pbn, fswritefd));
72 1.2 simonb bwrite(fswritefd, data, pbn, len);
73 1.2 simonb return 0;
74 1.2 simonb }
75 1.2 simonb
76 1.2 simonb int
77 1.2 simonb wapbl_read(void *data, size_t len, struct vnode *devvp, daddr_t pbn)
78 1.2 simonb {
79 1.2 simonb
80 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_IO,
81 1.2 simonb ("wapbl_read: %zd bytes at block %"PRId64" on fd 0x%x\n",
82 1.2 simonb len, pbn, fsreadfd));
83 1.2 simonb bread(fsreadfd, data, pbn, len);
84 1.2 simonb return 0;
85 1.2 simonb }
86 1.2 simonb
87 1.2 simonb struct wapbl_replay *wapbl_replay;
88 1.2 simonb
89 1.2 simonb void
90 1.2 simonb replay_wapbl(void)
91 1.2 simonb {
92 1.2 simonb uint64_t addr, count, blksize;
93 1.2 simonb int error;
94 1.2 simonb
95 1.2 simonb if (debug)
96 1.2 simonb wapbl_debug_print = WAPBL_PRINT_ERROR | WAPBL_PRINT_REPLAY;
97 1.2 simonb if (debug > 1)
98 1.2 simonb wapbl_debug_print |= WAPBL_PRINT_IO;
99 1.2 simonb
100 1.2 simonb if (sblock->fs_journal_version != UFS_WAPBL_VERSION) {
101 1.2 simonb pfatal("INVALID JOURNAL VERSION %d",
102 1.2 simonb sblock->fs_journal_version);
103 1.2 simonb if (reply("CONTINUE") == 0) {
104 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
105 1.2 simonb }
106 1.2 simonb return;
107 1.2 simonb }
108 1.2 simonb
109 1.2 simonb switch (sblock->fs_journal_location) {
110 1.2 simonb case UFS_WAPBL_JOURNALLOC_NONE:
111 1.2 simonb pfatal("INVALID JOURNAL LOCATION 'NONE'");
112 1.2 simonb if (reply("CONTINUE") == 0) {
113 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
114 1.2 simonb }
115 1.2 simonb return;
116 1.2 simonb
117 1.2 simonb case UFS_WAPBL_JOURNALLOC_END_PARTITION:
118 1.2 simonb addr = sblock->fs_journallocs[UFS_WAPBL_EPART_ADDR];
119 1.2 simonb count = sblock->fs_journallocs[UFS_WAPBL_EPART_COUNT];
120 1.2 simonb blksize = sblock->fs_journallocs[UFS_WAPBL_EPART_BLKSZ];
121 1.2 simonb break;
122 1.2 simonb
123 1.2 simonb case UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM:
124 1.2 simonb addr = sblock->fs_journallocs[UFS_WAPBL_INFS_ADDR];
125 1.2 simonb count = sblock->fs_journallocs[UFS_WAPBL_INFS_COUNT];
126 1.2 simonb blksize = sblock->fs_journallocs[UFS_WAPBL_INFS_BLKSZ];
127 1.2 simonb break;
128 1.2 simonb
129 1.2 simonb default:
130 1.2 simonb pfatal("INVALID JOURNAL LOCATION %d",
131 1.2 simonb sblock->fs_journal_location);
132 1.2 simonb if (reply("CONTINUE") == 0) {
133 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
134 1.2 simonb }
135 1.2 simonb return;
136 1.2 simonb }
137 1.2 simonb
138 1.2 simonb error = wapbl_replay_start(&wapbl_replay, 0, addr, count, blksize);
139 1.2 simonb if (error) {
140 1.2 simonb pfatal("UNABLE TO READ JOURNAL FOR REPLAY");
141 1.2 simonb if (reply("CONTINUE") == 0) {
142 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
143 1.2 simonb }
144 1.2 simonb return;
145 1.2 simonb }
146 1.2 simonb if (!nflag) {
147 1.2 simonb error = wapbl_replay_write(wapbl_replay, 0);
148 1.2 simonb if (error) {
149 1.2 simonb pfatal("UNABLE TO REPLAY JOURNAL BLOCKS");
150 1.2 simonb if (reply("CONTINUE") == 0) {
151 1.2 simonb exit(FSCK_EXIT_CHECK_FAILED);
152 1.2 simonb }
153 1.2 simonb } else {
154 1.2 simonb wapbl_replay_stop(wapbl_replay);
155 1.2 simonb }
156 1.2 simonb }
157 1.2 simonb {
158 1.2 simonb int i;
159 1.2 simonb for (i = 0; i < wapbl_replay->wr_inodescnt; i++) {
160 1.2 simonb WAPBL_PRINTF(WAPBL_PRINT_REPLAY,("wapbl_replay: "
161 1.2 simonb "not cleaning inode %"PRIu32" mode %"PRIo32"\n",
162 1.2 simonb wapbl_replay->wr_inodes[i].wr_inumber,
163 1.2 simonb wapbl_replay->wr_inodes[i].wr_imode));
164 1.2 simonb }
165 1.2 simonb }
166 1.2 simonb }
167 1.2 simonb
168 1.2 simonb void
169 1.2 simonb cleanup_wapbl(void)
170 1.2 simonb {
171 1.2 simonb
172 1.2 simonb if (wapbl_replay) {
173 1.2 simonb if (wapbl_replay_isopen(wapbl_replay))
174 1.2 simonb wapbl_replay_stop(wapbl_replay);
175 1.2 simonb wapbl_replay_free(wapbl_replay);
176 1.2 simonb wapbl_replay = 0;
177 1.2 simonb }
178 1.2 simonb }
179 1.2 simonb
180 1.2 simonb int
181 1.2 simonb read_wapbl(char *buf, long size, daddr_t blk)
182 1.2 simonb {
183 1.2 simonb
184 1.2 simonb if (!wapbl_replay || !wapbl_replay_isopen(wapbl_replay))
185 1.2 simonb return 0;
186 1.2 simonb return wapbl_replay_read(wapbl_replay, buf, blk, size);
187 1.2 simonb }
188 1.2 simonb
189 1.2 simonb int
190 1.2 simonb is_journal_inode(ino_t ino)
191 1.2 simonb {
192 1.2 simonb union dinode *dp;
193 1.2 simonb
194 1.2 simonb dp = ginode(ino);
195 1.2 simonb if ((iswap32(DIP(dp, flags)) & SF_LOG) != 0 &&
196 1.2 simonb sblock->fs_journal_version == UFS_WAPBL_VERSION &&
197 1.2 simonb sblock->fs_journal_location == UFS_WAPBL_JOURNALLOC_IN_FILESYSTEM &&
198 1.2 simonb sblock->fs_journallocs[UFS_WAPBL_INFS_INO] == ino)
199 1.2 simonb return 1;
200 1.2 simonb
201 1.2 simonb return 0;
202 1.2 simonb }
203