v7fs_superblock.c revision 1.1 1 1.1 uch /* $NetBSD: v7fs_superblock.c,v 1.1 2011/06/27 11:52:25 uch 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.1 uch #include <sys/cdefs.h>
33 1.1 uch __KERNEL_RCSID(0, "$NetBSD: v7fs_superblock.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
34 1.1 uch #if defined _KERNEL_OPT
35 1.1 uch #include "opt_v7fs.h"
36 1.1 uch #endif
37 1.1 uch
38 1.1 uch #ifdef _KERNEL
39 1.1 uch #include <sys/systm.h>
40 1.1 uch #include <sys/param.h> /* errno */
41 1.1 uch #else
42 1.1 uch #include <stdio.h>
43 1.1 uch #include <string.h>
44 1.1 uch #include <errno.h>
45 1.1 uch #endif
46 1.1 uch
47 1.1 uch #include "v7fs.h"
48 1.1 uch #include "v7fs_impl.h"
49 1.1 uch #include "v7fs_endian.h"
50 1.1 uch #include "v7fs_superblock.h"
51 1.1 uch #include "v7fs_inode.h"
52 1.1 uch #include "v7fs_datablock.h"
53 1.1 uch
54 1.1 uch #ifdef V7FS_SUPERBLOCK_DEBUG
55 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
56 1.1 uch #define DPRINTF_(fmt, args...) printf(fmt, ##args)
57 1.1 uch #else
58 1.1 uch #define DPRINTF(fmt, args...) ((void)0)
59 1.1 uch #define DPRINTF_(fmt, args...) ((void)0)
60 1.1 uch #endif
61 1.1 uch
62 1.1 uch static void v7fs_superblock_endian_convert(struct v7fs_self *,
63 1.1 uch struct v7fs_superblock *, struct v7fs_superblock *);
64 1.1 uch static int v7fs_superblock_sanity(struct v7fs_self *);
65 1.1 uch
66 1.1 uch /* Load superblock from disk. */
67 1.1 uch int
68 1.1 uch v7fs_superblock_load(struct v7fs_self *fs)
69 1.1 uch {
70 1.1 uch struct v7fs_superblock *disksb;
71 1.1 uch void *buf;
72 1.1 uch int error;
73 1.1 uch
74 1.1 uch if (!(buf = scratch_read(fs, V7FS_SUPERBLOCK_SECTOR)))
75 1.1 uch return EIO;
76 1.1 uch disksb = (struct v7fs_superblock *)buf;
77 1.1 uch v7fs_superblock_endian_convert(fs, &fs->superblock, disksb);
78 1.1 uch scratch_free(fs, buf);
79 1.1 uch
80 1.1 uch if ((error = v7fs_superblock_sanity(fs)))
81 1.1 uch return error;
82 1.1 uch
83 1.1 uch return 0;
84 1.1 uch }
85 1.1 uch
86 1.1 uch /* Writeback superblock to disk. */
87 1.1 uch int
88 1.1 uch v7fs_superblock_writeback(struct v7fs_self *fs)
89 1.1 uch {
90 1.1 uch struct v7fs_superblock *memsb = &fs->superblock;
91 1.1 uch struct v7fs_superblock *disksb;
92 1.1 uch void *buf;
93 1.1 uch int error = 0;
94 1.1 uch
95 1.1 uch if (!memsb->modified)
96 1.1 uch return 0;
97 1.1 uch
98 1.1 uch if (!(buf = scratch_read(fs, V7FS_SUPERBLOCK_SECTOR)))
99 1.1 uch return EIO;
100 1.1 uch disksb = (struct v7fs_superblock *)buf;
101 1.1 uch v7fs_superblock_endian_convert(fs, disksb, memsb);
102 1.1 uch if (!fs->io.write(fs->io.cookie, buf, V7FS_SUPERBLOCK_SECTOR))
103 1.1 uch error = EIO;
104 1.1 uch scratch_free(fs, buf);
105 1.1 uch
106 1.1 uch memsb->modified = 0;
107 1.1 uch DPRINTF("done. %d\n", error);
108 1.1 uch
109 1.1 uch return error;
110 1.1 uch }
111 1.1 uch
112 1.1 uch /* Check endian mismatch. */
113 1.1 uch static int
114 1.1 uch v7fs_superblock_sanity(struct v7fs_self *fs)
115 1.1 uch {
116 1.1 uch const struct v7fs_superblock *sb = &fs->superblock;
117 1.1 uch void *buf = 0;
118 1.1 uch
119 1.1 uch if ((sb->volume_size < 128) || /* smaller than 64KB. */
120 1.1 uch (sb->datablock_start_sector > sb->volume_size) ||
121 1.1 uch (sb->nfreeinode > V7FS_MAX_FREEINODE) ||
122 1.1 uch (sb->nfreeblock > V7FS_MAX_FREEBLOCK) ||
123 1.1 uch (sb->update_time < 0) ||
124 1.1 uch (sb->total_freeblock > sb->volume_size) ||
125 1.1 uch ((sb->nfreeinode == 0) && (sb->nfreeblock == 0) &&
126 1.1 uch (sb->total_freeblock == 0) && (sb->total_freeinode == 0)) ||
127 1.1 uch (!(buf = scratch_read(fs, sb->volume_size - 1)))) {
128 1.1 uch DPRINTF("invalid super block.\n");
129 1.1 uch return EINVAL;
130 1.1 uch }
131 1.1 uch if (buf)
132 1.1 uch scratch_free(fs, buf);
133 1.1 uch
134 1.1 uch return 0;
135 1.1 uch }
136 1.1 uch
137 1.1 uch /* Fill free block to superblock cache. */
138 1.1 uch int
139 1.1 uch v7fs_freeblock_update(struct v7fs_self *fs, v7fs_daddr_t blk)
140 1.1 uch {
141 1.1 uch /* Assume superblock is locked by caller. */
142 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
143 1.1 uch struct v7fs_freeblock *fb;
144 1.1 uch void *buf;
145 1.1 uch int error;
146 1.1 uch
147 1.1 uch /* Read next freeblock table from disk. */
148 1.1 uch if (!datablock_number_sanity(fs, blk) || !(buf = scratch_read(fs, blk)))
149 1.1 uch return EIO;
150 1.1 uch
151 1.1 uch /* Update in-core superblock freelist. */
152 1.1 uch fb = (struct v7fs_freeblock *)buf;
153 1.1 uch if ((error = v7fs_freeblock_endian_convert(fs, fb))) {
154 1.1 uch scratch_free(fs, buf);
155 1.1 uch return error;
156 1.1 uch }
157 1.1 uch DPRINTF("freeblock table#%d, nfree=%d\n", blk, fb->nfreeblock);
158 1.1 uch
159 1.1 uch memcpy(sb->freeblock, fb->freeblock, sizeof(blk) * fb->nfreeblock);
160 1.1 uch sb->nfreeblock = fb->nfreeblock;
161 1.1 uch sb->modified = true;
162 1.1 uch scratch_free(fs, buf);
163 1.1 uch
164 1.1 uch return 0;
165 1.1 uch }
166 1.1 uch
167 1.1 uch int
168 1.1 uch v7fs_freeblock_endian_convert(struct v7fs_self *fs __unused,
169 1.1 uch struct v7fs_freeblock *fb __unused)
170 1.1 uch {
171 1.1 uch #ifdef V7FS_EI
172 1.1 uch int i;
173 1.1 uch int16_t nfree;
174 1.1 uch
175 1.1 uch nfree = V7FS_VAL16(fs, fb->nfreeblock);
176 1.1 uch if (nfree <= 0 || nfree > V7FS_MAX_FREEBLOCK) {
177 1.1 uch DPRINTF("invalid freeblock list. %d (max=%d)\n", nfree,
178 1.1 uch V7FS_MAX_FREEBLOCK);
179 1.1 uch return ENOSPC;
180 1.1 uch }
181 1.1 uch fb->nfreeblock = nfree;
182 1.1 uch
183 1.1 uch for (i = 0; i < nfree; i++) {
184 1.1 uch fb->freeblock[i] = V7FS_VAL32(fs, fb->freeblock[i]);
185 1.1 uch }
186 1.1 uch #endif /* V7FS_EI */
187 1.1 uch
188 1.1 uch return 0;
189 1.1 uch }
190 1.1 uch
191 1.1 uch /* Fill free inode to superblock cache. */
192 1.1 uch int
193 1.1 uch v7fs_freeinode_update(struct v7fs_self *fs)
194 1.1 uch {
195 1.1 uch /* Assume superblock is locked by caller. */
196 1.1 uch struct v7fs_superblock *sb = &fs->superblock;
197 1.1 uch v7fs_ino_t *freeinode = sb->freeinode;
198 1.1 uch size_t i, j, k;
199 1.1 uch v7fs_ino_t ino;
200 1.1 uch
201 1.1 uch /* Loop over all inode list. */
202 1.1 uch for (i = V7FS_ILIST_SECTOR, ino = 1/* inode start from 1*/, k = 0;
203 1.1 uch i < sb->datablock_start_sector; i++) {
204 1.1 uch struct v7fs_inode_diskimage *di;
205 1.1 uch void *buf;
206 1.1 uch if (!(buf = scratch_read(fs, i))) {
207 1.1 uch DPRINTF("block %zu I/O error.\n", i);
208 1.1 uch ino += V7FS_INODE_PER_BLOCK;
209 1.1 uch continue;
210 1.1 uch }
211 1.1 uch di = (struct v7fs_inode_diskimage *)buf;
212 1.1 uch
213 1.1 uch for (j = 0;
214 1.1 uch (j < V7FS_INODE_PER_BLOCK) && (k < V7FS_MAX_FREEINODE);
215 1.1 uch j++, di++, ino++) {
216 1.1 uch if (v7fs_inode_allocated(di))
217 1.1 uch continue;
218 1.1 uch DPRINTF("free inode%d\n", ino);
219 1.1 uch freeinode[k++] = ino;
220 1.1 uch }
221 1.1 uch scratch_free(fs, buf);
222 1.1 uch }
223 1.1 uch sb->nfreeinode = k;
224 1.1 uch
225 1.1 uch return 0;
226 1.1 uch }
227 1.1 uch
228 1.1 uch static void
229 1.1 uch v7fs_superblock_endian_convert(struct v7fs_self *fs __unused,
230 1.1 uch struct v7fs_superblock *to, struct v7fs_superblock *from)
231 1.1 uch {
232 1.1 uch #ifdef V7FS_EI
233 1.1 uch #define conv16(m) (to->m = V7FS_VAL16(fs, from->m))
234 1.1 uch #define conv32(m) (to->m = V7FS_VAL32(fs, from->m))
235 1.1 uch int i;
236 1.1 uch
237 1.1 uch conv16(datablock_start_sector);
238 1.1 uch conv32(volume_size);
239 1.1 uch conv16(nfreeblock);
240 1.1 uch v7fs_daddr_t *dfrom = from->freeblock;
241 1.1 uch v7fs_daddr_t *dto = to->freeblock;
242 1.1 uch for (i = 0; i < V7FS_MAX_FREEBLOCK; i++, dfrom++, dto++)
243 1.1 uch *dto = V7FS_VAL32(fs, *dfrom);
244 1.1 uch
245 1.1 uch conv16(nfreeinode);
246 1.1 uch v7fs_ino_t *ifrom = from->freeinode;
247 1.1 uch v7fs_ino_t *ito = to->freeinode;
248 1.1 uch for (i = 0; i < V7FS_MAX_FREEINODE; i++, ifrom++, ito++)
249 1.1 uch *ito = V7FS_VAL16(fs, *ifrom);
250 1.1 uch
251 1.1 uch conv32(update_time);
252 1.1 uch conv32(total_freeblock);
253 1.1 uch conv16(total_freeinode);
254 1.1 uch #undef conv16
255 1.1 uch #undef conv32
256 1.1 uch #else /* V7FS_EI */
257 1.1 uch memcpy(to, from , sizeof(*to));
258 1.1 uch #endif /* V7FS_EI */
259 1.1 uch }
260