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