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