v7fs_superblock.c revision 1.2 1 1.2 apb /* $NetBSD: v7fs_superblock.c,v 1.2 2011/07/18 21:51:49 apb Exp $ */
2 1.1 uch
3 1.1 uch /*-
4 1.1 uch * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 1.1 uch * All rights reserved.
6 1.1 uch *
7 1.1 uch * This code is derived from software contributed to The NetBSD Foundation
8 1.1 uch * by UCHIYAMA Yasushi.
9 1.1 uch *
10 1.1 uch * Redistribution and use in source and binary forms, with or without
11 1.1 uch * modification, are permitted provided that the following conditions
12 1.1 uch * are met:
13 1.1 uch * 1. Redistributions of source code must retain the above copyright
14 1.1 uch * notice, this list of conditions and the following disclaimer.
15 1.1 uch * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 uch * notice, this list of conditions and the following disclaimer in the
17 1.1 uch * documentation and/or other materials provided with the distribution.
18 1.1 uch *
19 1.1 uch * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 uch * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 uch * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 uch * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 uch * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 uch * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 uch * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 uch * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 uch * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 uch * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 uch * POSSIBILITY OF SUCH DAMAGE.
30 1.1 uch */
31 1.1 uch
32 1.2 apb #if HAVE_NBTOOL_CONFIG_H
33 1.2 apb #include "nbtool_config.h"
34 1.2 apb #endif
35 1.2 apb
36 1.1 uch #include <sys/cdefs.h>
37 1.2 apb __KERNEL_RCSID(0, "$NetBSD: v7fs_superblock.c,v 1.2 2011/07/18 21:51:49 apb Exp $");
38 1.1 uch #if defined _KERNEL_OPT
39 1.1 uch #include "opt_v7fs.h"
40 1.1 uch #endif
41 1.1 uch
42 1.1 uch #ifdef _KERNEL
43 1.1 uch #include <sys/systm.h>
44 1.1 uch #include <sys/param.h> /* errno */
45 1.1 uch #else
46 1.1 uch #include <stdio.h>
47 1.1 uch #include <string.h>
48 1.1 uch #include <errno.h>
49 1.1 uch #endif
50 1.1 uch
51 1.1 uch #include "v7fs.h"
52 1.1 uch #include "v7fs_impl.h"
53 1.1 uch #include "v7fs_endian.h"
54 1.1 uch #include "v7fs_superblock.h"
55 1.1 uch #include "v7fs_inode.h"
56 1.1 uch #include "v7fs_datablock.h"
57 1.1 uch
58 1.1 uch #ifdef V7FS_SUPERBLOCK_DEBUG
59 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
60 1.1 uch #define DPRINTF_(fmt, args...) printf(fmt, ##args)
61 1.1 uch #else
62 1.1 uch #define DPRINTF(fmt, args...) ((void)0)
63 1.1 uch #define DPRINTF_(fmt, args...) ((void)0)
64 1.1 uch #endif
65 1.1 uch
66 1.1 uch static void v7fs_superblock_endian_convert(struct v7fs_self *,
67 1.1 uch struct v7fs_superblock *, struct v7fs_superblock *);
68 1.1 uch static int v7fs_superblock_sanity(struct v7fs_self *);
69 1.1 uch
70 1.1 uch /* Load superblock from disk. */
71 1.1 uch int
72 1.1 uch v7fs_superblock_load(struct v7fs_self *fs)
73 1.1 uch {
74 1.1 uch struct v7fs_superblock *disksb;
75 1.1 uch void *buf;
76 1.1 uch int error;
77 1.1 uch
78 1.1 uch if (!(buf = scratch_read(fs, V7FS_SUPERBLOCK_SECTOR)))
79 1.1 uch return EIO;
80 1.1 uch disksb = (struct v7fs_superblock *)buf;
81 1.1 uch v7fs_superblock_endian_convert(fs, &fs->superblock, disksb);
82 1.1 uch scratch_free(fs, buf);
83 1.1 uch
84 1.1 uch if ((error = v7fs_superblock_sanity(fs)))
85 1.1 uch return error;
86 1.1 uch
87 1.1 uch return 0;
88 1.1 uch }
89 1.1 uch
90 1.1 uch /* Writeback superblock to disk. */
91 1.1 uch int
92 1.1 uch v7fs_superblock_writeback(struct v7fs_self *fs)
93 1.1 uch {
94 1.1 uch struct v7fs_superblock *memsb = &fs->superblock;
95 1.1 uch struct v7fs_superblock *disksb;
96 1.1 uch void *buf;
97 1.1 uch int error = 0;
98 1.1 uch
99 1.1 uch if (!memsb->modified)
100 1.1 uch return 0;
101 1.1 uch
102 1.1 uch if (!(buf = scratch_read(fs, V7FS_SUPERBLOCK_SECTOR)))
103 1.1 uch return EIO;
104 1.1 uch disksb = (struct v7fs_superblock *)buf;
105 1.1 uch v7fs_superblock_endian_convert(fs, disksb, memsb);
106 1.1 uch if (!fs->io.write(fs->io.cookie, buf, V7FS_SUPERBLOCK_SECTOR))
107 1.1 uch error = EIO;
108 1.1 uch scratch_free(fs, buf);
109 1.1 uch
110 1.1 uch memsb->modified = 0;
111 1.1 uch DPRINTF("done. %d\n", error);
112 1.1 uch
113 1.1 uch return error;
114 1.1 uch }
115 1.1 uch
116 1.1 uch /* Check endian mismatch. */
117 1.1 uch static int
118 1.1 uch v7fs_superblock_sanity(struct v7fs_self *fs)
119 1.1 uch {
120 1.1 uch const struct v7fs_superblock *sb = &fs->superblock;
121 1.1 uch void *buf = 0;
122 1.1 uch
123 1.1 uch if ((sb->volume_size < 128) || /* smaller than 64KB. */
124 1.1 uch (sb->datablock_start_sector > sb->volume_size) ||
125 1.1 uch (sb->nfreeinode > V7FS_MAX_FREEINODE) ||
126 1.1 uch (sb->nfreeblock > V7FS_MAX_FREEBLOCK) ||
127 1.1 uch (sb->update_time < 0) ||
128 1.1 uch (sb->total_freeblock > sb->volume_size) ||
129 1.1 uch ((sb->nfreeinode == 0) && (sb->nfreeblock == 0) &&
130 1.1 uch (sb->total_freeblock == 0) && (sb->total_freeinode == 0)) ||
131 1.1 uch (!(buf = scratch_read(fs, sb->volume_size - 1)))) {
132 1.1 uch DPRINTF("invalid super block.\n");
133 1.1 uch return EINVAL;
134 1.1 uch }
135 1.1 uch if (buf)
136 1.1 uch scratch_free(fs, buf);
137 1.1 uch
138 1.1 uch return 0;
139 1.1 uch }
140 1.1 uch
141 1.1 uch /* Fill free block to superblock cache. */
142 1.1 uch int
143 1.1 uch v7fs_freeblock_update(struct v7fs_self *fs, v7fs_daddr_t blk)
144 1.1 uch {
145 1.1 uch /* Assume superblock is locked by caller. */
146 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
147 1.1 uch struct v7fs_freeblock *fb;
148 1.1 uch void *buf;
149 1.1 uch int error;
150 1.1 uch
151 1.1 uch /* Read next freeblock table from disk. */
152 1.1 uch if (!datablock_number_sanity(fs, blk) || !(buf = scratch_read(fs, blk)))
153 1.1 uch return EIO;
154 1.1 uch
155 1.1 uch /* Update in-core superblock freelist. */
156 1.1 uch fb = (struct v7fs_freeblock *)buf;
157 1.1 uch if ((error = v7fs_freeblock_endian_convert(fs, fb))) {
158 1.1 uch scratch_free(fs, buf);
159 1.1 uch return error;
160 1.1 uch }
161 1.1 uch DPRINTF("freeblock table#%d, nfree=%d\n", blk, fb->nfreeblock);
162 1.1 uch
163 1.1 uch memcpy(sb->freeblock, fb->freeblock, sizeof(blk) * fb->nfreeblock);
164 1.1 uch sb->nfreeblock = fb->nfreeblock;
165 1.1 uch sb->modified = true;
166 1.1 uch scratch_free(fs, buf);
167 1.1 uch
168 1.1 uch return 0;
169 1.1 uch }
170 1.1 uch
171 1.1 uch int
172 1.1 uch v7fs_freeblock_endian_convert(struct v7fs_self *fs __unused,
173 1.1 uch struct v7fs_freeblock *fb __unused)
174 1.1 uch {
175 1.1 uch #ifdef V7FS_EI
176 1.1 uch int i;
177 1.1 uch int16_t nfree;
178 1.1 uch
179 1.1 uch nfree = V7FS_VAL16(fs, fb->nfreeblock);
180 1.1 uch if (nfree <= 0 || nfree > V7FS_MAX_FREEBLOCK) {
181 1.1 uch DPRINTF("invalid freeblock list. %d (max=%d)\n", nfree,
182 1.1 uch V7FS_MAX_FREEBLOCK);
183 1.1 uch return ENOSPC;
184 1.1 uch }
185 1.1 uch fb->nfreeblock = nfree;
186 1.1 uch
187 1.1 uch for (i = 0; i < nfree; i++) {
188 1.1 uch fb->freeblock[i] = V7FS_VAL32(fs, fb->freeblock[i]);
189 1.1 uch }
190 1.1 uch #endif /* V7FS_EI */
191 1.1 uch
192 1.1 uch return 0;
193 1.1 uch }
194 1.1 uch
195 1.1 uch /* Fill free inode to superblock cache. */
196 1.1 uch int
197 1.1 uch v7fs_freeinode_update(struct v7fs_self *fs)
198 1.1 uch {
199 1.1 uch /* Assume superblock is locked by caller. */
200 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
201 1.1 uch v7fs_ino_t *freeinode = sb->freeinode;
202 1.1 uch size_t i, j, k;
203 1.1 uch v7fs_ino_t ino;
204 1.1 uch
205 1.1 uch /* Loop over all inode list. */
206 1.1 uch for (i = V7FS_ILIST_SECTOR, ino = 1/* inode start from 1*/, k = 0;
207 1.1 uch i < sb->datablock_start_sector; i++) {
208 1.1 uch struct v7fs_inode_diskimage *di;
209 1.1 uch void *buf;
210 1.1 uch if (!(buf = scratch_read(fs, i))) {
211 1.1 uch DPRINTF("block %zu I/O error.\n", i);
212 1.1 uch ino += V7FS_INODE_PER_BLOCK;
213 1.1 uch continue;
214 1.1 uch }
215 1.1 uch di = (struct v7fs_inode_diskimage *)buf;
216 1.1 uch
217 1.1 uch for (j = 0;
218 1.1 uch (j < V7FS_INODE_PER_BLOCK) && (k < V7FS_MAX_FREEINODE);
219 1.1 uch j++, di++, ino++) {
220 1.1 uch if (v7fs_inode_allocated(di))
221 1.1 uch continue;
222 1.1 uch DPRINTF("free inode%d\n", ino);
223 1.1 uch freeinode[k++] = ino;
224 1.1 uch }
225 1.1 uch scratch_free(fs, buf);
226 1.1 uch }
227 1.1 uch sb->nfreeinode = k;
228 1.1 uch
229 1.1 uch return 0;
230 1.1 uch }
231 1.1 uch
232 1.1 uch static void
233 1.1 uch v7fs_superblock_endian_convert(struct v7fs_self *fs __unused,
234 1.1 uch struct v7fs_superblock *to, struct v7fs_superblock *from)
235 1.1 uch {
236 1.1 uch #ifdef V7FS_EI
237 1.1 uch #define conv16(m) (to->m = V7FS_VAL16(fs, from->m))
238 1.1 uch #define conv32(m) (to->m = V7FS_VAL32(fs, from->m))
239 1.1 uch int i;
240 1.1 uch
241 1.1 uch conv16(datablock_start_sector);
242 1.1 uch conv32(volume_size);
243 1.1 uch conv16(nfreeblock);
244 1.1 uch v7fs_daddr_t *dfrom = from->freeblock;
245 1.1 uch v7fs_daddr_t *dto = to->freeblock;
246 1.1 uch for (i = 0; i < V7FS_MAX_FREEBLOCK; i++, dfrom++, dto++)
247 1.1 uch *dto = V7FS_VAL32(fs, *dfrom);
248 1.1 uch
249 1.1 uch conv16(nfreeinode);
250 1.1 uch v7fs_ino_t *ifrom = from->freeinode;
251 1.1 uch v7fs_ino_t *ito = to->freeinode;
252 1.1 uch for (i = 0; i < V7FS_MAX_FREEINODE; i++, ifrom++, ito++)
253 1.1 uch *ito = V7FS_VAL16(fs, *ifrom);
254 1.1 uch
255 1.1 uch conv32(update_time);
256 1.1 uch conv32(total_freeblock);
257 1.1 uch conv16(total_freeinode);
258 1.1 uch #undef conv16
259 1.1 uch #undef conv32
260 1.1 uch #else /* V7FS_EI */
261 1.1 uch memcpy(to, from , sizeof(*to));
262 1.1 uch #endif /* V7FS_EI */
263 1.1 uch }
264