v7fs_inode.c revision 1.1 1 /* $NetBSD: v7fs_inode.c,v 1.1 2011/06/27 11:52:25 uch Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by UCHIYAMA Yasushi.
9 *
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.
18 *
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.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: v7fs_inode.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
34 #if defined _KERNEL_OPT
35 #include "opt_v7fs.h"
36 #endif
37
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #include <sys/param.h>
41 #else
42 #include <stdio.h>
43 #include <string.h>
44 #include <errno.h>
45 #include <time.h>
46 #endif
47
48 #include "v7fs.h"
49 #include "v7fs_impl.h"
50 #include "v7fs_endian.h"
51 #include "v7fs_inode.h"
52 #include "v7fs_superblock.h"
53
54 #ifdef V7FS_INODE_DEBUG
55 #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args)
56 #else
57 #define DPRINTF(fmt, args...) ((void)0)
58 #endif
59
60 static void v7fs_inode_setup_disk_image(const struct v7fs_self *,
61 struct v7fs_inode *, struct v7fs_inode_diskimage *);
62 static int v7fs_inode_inquire_disk_location(const struct v7fs_self *,
63 v7fs_ino_t, v7fs_daddr_t *, v7fs_daddr_t *);
64 #ifdef V7FS_INODE_DEBUG
65 static int v7fs_inode_block_sanity(const struct v7fs_superblock *,
66 v7fs_daddr_t);
67
68 static int
69 v7fs_inode_block_sanity(const struct v7fs_superblock *sb, v7fs_daddr_t blk)
70 {
71
72 if ((blk < V7FS_ILIST_SECTOR) || (blk >= sb->datablock_start_sector)) {
73 DPRINTF("invalid inode block#%d (%d-%d)\n", blk,
74 V7FS_ILIST_SECTOR, sb->datablock_start_sector);
75 return ENOSPC;
76 }
77
78 return 0;
79 }
80 #endif /* V7FS_INODE_DEBUG */
81
82 int
83 v7fs_inode_number_sanity(const struct v7fs_superblock *sb, v7fs_ino_t ino)
84 {
85
86 if (ino < V7FS_ROOT_INODE || ((size_t)ino >= V7FS_MAX_INODE(sb))) {
87 DPRINTF("invalid inode#%d (%d-%zu)\n", ino,
88 V7FS_ROOT_INODE, V7FS_MAX_INODE(sb));
89 return ENOSPC;
90 }
91
92 return 0;
93 }
94
95 int
96 v7fs_inode_allocate(struct v7fs_self *fs, v7fs_ino_t *ino)
97 {
98 struct v7fs_superblock *sb = &fs->superblock;
99 v7fs_ino_t inode_number;
100 int error = ENOSPC;
101 *ino = 0;
102
103 SUPERB_LOCK(fs);
104 if (sb->total_freeinode == 0) {
105 DPRINTF("inode exhausted!(1)\n");
106 goto errexit;
107 }
108
109 /* If there is no free inode cache, update it. */
110 if (sb->nfreeinode <= 0 && (error = v7fs_freeinode_update(fs))) {
111 DPRINTF("inode exhausted!(2)\n");
112 goto errexit;
113 }
114 /* Get inode from superblock cache. */
115 KDASSERT(sb->nfreeinode <= V7FS_MAX_FREEINODE);
116 inode_number = sb->freeinode[--sb->nfreeinode];
117 sb->total_freeinode--;
118 sb->modified = 1;
119
120 if ((error = v7fs_inode_number_sanity(sb, inode_number))) {
121 DPRINTF("new inode#%d %d %d\n", inode_number, sb->nfreeinode,
122 sb->total_freeinode);
123 DPRINTF("free inode list corupt\n");
124 goto errexit;
125 }
126 *ino = inode_number;
127
128 errexit:
129 SUPERB_UNLOCK(fs);
130
131 return error;
132 }
133
134 void
135 v7fs_inode_deallocate(struct v7fs_self *fs, v7fs_ino_t ino)
136 {
137 struct v7fs_superblock *sb = &fs->superblock;
138 struct v7fs_inode inode;
139
140 memset(&inode, 0, sizeof(inode));
141 inode.inode_number = ino;
142 v7fs_inode_writeback(fs, &inode);
143
144 SUPERB_LOCK(fs);
145 if (sb->nfreeinode < V7FS_MAX_FREEINODE) {
146 /* link to freeinode list. */
147 sb->freeinode[sb->nfreeinode++] = ino;
148 }
149 /* If superblock inode cache is full, this inode charged by
150 v7fs_freeinode_update() later. */
151 sb->total_freeinode++;
152 sb->modified = true;
153 SUPERB_UNLOCK(fs);
154 }
155
156 void
157 v7fs_inode_setup_memory_image(const struct v7fs_self *fs __unused,
158 struct v7fs_inode *mem, struct v7fs_inode_diskimage *disk)
159 {
160 #define conv16(m) (mem->m = V7FS_VAL16(fs, (disk->m)))
161 #define conv32(m) (mem->m = V7FS_VAL32(fs, (disk->m)))
162 uint32_t addr;
163 int i;
164
165 memset(mem, 0, sizeof(*mem));
166 conv16(mode);
167 conv16(nlink);
168 conv16(uid);
169 conv16(gid);
170 conv32(filesize);
171 conv32(atime);
172 conv32(mtime);
173 conv32(ctime);
174
175 for (i = 0; i < V7FS_NADDR; i++) {
176 int j = i * 3; /* 3 byte each. (v7fs_daddr is 24bit) */
177 /* expand to 4byte with endian conversion. */
178 addr = V7FS_VAL24_READ(fs, &disk->addr[j]);
179 mem->addr[i] = addr;
180 }
181 mem->device = 0;
182 if (v7fs_inode_iscdev(mem) || v7fs_inode_isbdev(mem)) {
183 mem->device = mem->addr[0];
184 }
185
186 #undef conv16
187 #undef conv32
188 }
189
190 static void
191 v7fs_inode_setup_disk_image(const struct v7fs_self *fs __unused,
192 struct v7fs_inode *mem, struct v7fs_inode_diskimage *disk)
193 {
194 #define conv16(m) (disk->m = V7FS_VAL16(fs, (mem->m)))
195 #define conv32(m) (disk->m = V7FS_VAL32(fs, (mem->m)))
196
197 conv16(mode);
198 conv16(nlink);
199 conv16(uid);
200 conv16(gid);
201 conv32(filesize);
202 conv32(atime);
203 conv32(mtime);
204 conv32(ctime);
205
206 int i;
207 for (i = 0; i < V7FS_NADDR; i++) {
208 int j = i * 3; /* 3 byte each. */
209 V7FS_VAL24_WRITE(fs, mem->addr[i], disk->addr + j);
210 }
211 #undef conv16
212 #undef conv32
213 }
214
215 /* Load inode from disk. */
216 int
217 v7fs_inode_load(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t n)
218 {
219 v7fs_daddr_t blk, ofs;
220 struct v7fs_inode_diskimage *di;
221 void *buf;
222
223 if (v7fs_inode_inquire_disk_location(fs, n, &blk, &ofs) != 0)
224 return ENOENT;
225
226 ILIST_LOCK(fs);
227 if (!(buf = scratch_read(fs, blk))) {
228 ILIST_UNLOCK(fs);
229 return EIO;
230 }
231 ILIST_UNLOCK(fs);
232 di = (struct v7fs_inode_diskimage *)buf;
233
234 /* Decode disk address, convert endian. */
235 v7fs_inode_setup_memory_image(fs, p, di + ofs);
236 p->inode_number = n;
237
238 scratch_free(fs, buf);
239
240 return 0;
241 }
242
243 /* Write back inode to disk. */
244 int
245 v7fs_inode_writeback(struct v7fs_self *fs, struct v7fs_inode *mem)
246 {
247 struct v7fs_inode_diskimage disk;
248 v7fs_ino_t ino = mem->inode_number;
249 v7fs_daddr_t blk;
250 v7fs_daddr_t ofs;
251 void *buf;
252 int error = 0;
253
254 if (v7fs_inode_inquire_disk_location(fs, ino, &blk, &ofs) != 0)
255 return ENOENT;
256
257 v7fs_inode_setup_disk_image(fs, mem, &disk);
258
259 ILIST_LOCK(fs);
260 if (!(buf = scratch_read(fs, blk))) {
261 ILIST_UNLOCK(fs);
262 return EIO;
263 }
264 struct v7fs_inode_diskimage *di = (struct v7fs_inode_diskimage *)buf;
265 di[ofs] = disk; /* structure copy; */
266 if (!fs->io.write(fs->io.cookie, buf, blk))
267 error = EIO;
268 ILIST_UNLOCK(fs);
269
270 scratch_free(fs, buf);
271
272 return error;
273 }
274
275 static int
276 v7fs_inode_inquire_disk_location(const struct v7fs_self *fs
277 __unused, v7fs_ino_t n, v7fs_daddr_t *block,
278 v7fs_daddr_t *offset)
279 {
280 v7fs_daddr_t ofs, blk;
281 #ifdef V7FS_INODE_DEBUG
282 v7fs_inode_number_sanity(&fs->superblock, n);
283 #endif
284 ofs = (n - 1/*inode start from 1*/) *
285 sizeof(struct v7fs_inode_diskimage);
286 blk = ofs >> V7FS_BSHIFT;
287
288 *block = blk + V7FS_ILIST_SECTOR;
289 *offset = (ofs - blk * V7FS_BSIZE) /
290 sizeof(struct v7fs_inode_diskimage);
291 #ifdef V7FS_INODE_DEBUG
292 return v7fs_inode_block_sanity(&fs->superblock, *block);
293 #else
294 return 0;
295 #endif
296 }
297
298