bfs.c revision 1.12 1 1.12 uch /* $NetBSD: bfs.c,v 1.12 2009/04/26 12:41:59 uch Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*-
4 1.1 tsutsui * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 tsutsui * All rights reserved.
6 1.1 tsutsui *
7 1.1 tsutsui * This code is derived from software contributed to The NetBSD Foundation
8 1.1 tsutsui * by UCHIYAMA Yasushi.
9 1.1 tsutsui *
10 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
11 1.1 tsutsui * modification, are permitted provided that the following conditions
12 1.1 tsutsui * are met:
13 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
14 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
15 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
17 1.1 tsutsui * documentation and/or other materials provided with the distribution.
18 1.1 tsutsui *
19 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 tsutsui * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 tsutsui * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 tsutsui * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 tsutsui * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 tsutsui * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 tsutsui * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 tsutsui * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 tsutsui * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 tsutsui * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 tsutsui * POSSIBILITY OF SUCH DAMAGE.
30 1.1 tsutsui */
31 1.1 tsutsui
32 1.1 tsutsui #include <sys/cdefs.h>
33 1.1 tsutsui
34 1.12 uch __KERNEL_RCSID(0, "$NetBSD: bfs.c,v 1.12 2009/04/26 12:41:59 uch Exp $");
35 1.1 tsutsui #define BFS_DEBUG
36 1.1 tsutsui
37 1.1 tsutsui #include <sys/param.h>
38 1.1 tsutsui #include <sys/kernel.h>
39 1.1 tsutsui #include <sys/types.h>
40 1.1 tsutsui #include <sys/systm.h>
41 1.1 tsutsui #include <sys/errno.h>
42 1.1 tsutsui #include <sys/malloc.h>
43 1.1 tsutsui #include <sys/time.h>
44 1.1 tsutsui
45 1.1 tsutsui #ifdef _KERNEL
46 1.9 pooka MALLOC_JUSTDEFINE(M_BFS, "sysvbfs core", "sysvbfs internal structures");
47 1.1 tsutsui #define __MALLOC(s, t, f) malloc(s, t, f)
48 1.1 tsutsui #define __FREE(a, s, t) free(a, t)
49 1.1 tsutsui #elif defined _STANDALONE
50 1.1 tsutsui #include <lib/libsa/stand.h>
51 1.1 tsutsui #include <lib/libkern/libkern.h>
52 1.1 tsutsui #define __MALLOC(s, t, f) alloc(s)
53 1.2 tsutsui #define __FREE(a, s, t) dealloc(a, s)
54 1.1 tsutsui #else
55 1.1 tsutsui #include "local.h"
56 1.1 tsutsui #define __MALLOC(s, t, f) malloc(s)
57 1.1 tsutsui #define __FREE(a, s, t) free(a)
58 1.1 tsutsui #endif
59 1.1 tsutsui #include <fs/sysvbfs/bfs.h>
60 1.1 tsutsui
61 1.1 tsutsui #ifdef BFS_DEBUG
62 1.1 tsutsui #define DPRINTF(on, fmt, args...) if (on) printf(fmt, ##args)
63 1.1 tsutsui #else
64 1.1 tsutsui #define DPRINTF(arg...) ((void)0)
65 1.1 tsutsui #endif
66 1.1 tsutsui
67 1.1 tsutsui #define ROUND_SECTOR(x) (((x) + 511) & ~511)
68 1.1 tsutsui #define TRUNC_SECTOR(x) ((x) & ~511)
69 1.1 tsutsui
70 1.1 tsutsui #define STATIC
71 1.1 tsutsui
72 1.1 tsutsui STATIC int bfs_init_superblock(struct bfs *, int, size_t *);
73 1.1 tsutsui STATIC int bfs_init_inode(struct bfs *, uint8_t *, size_t *);
74 1.1 tsutsui STATIC int bfs_init_dirent(struct bfs *, uint8_t *);
75 1.1 tsutsui
76 1.1 tsutsui /* super block ops. */
77 1.7 thorpej STATIC bool bfs_superblock_valid(const struct bfs_super_block *);
78 1.7 thorpej STATIC bool bfs_writeback_dirent(const struct bfs *, struct bfs_dirent *,
79 1.7 thorpej bool);
80 1.7 thorpej STATIC bool bfs_writeback_inode(const struct bfs *, struct bfs_inode *);
81 1.1 tsutsui
82 1.1 tsutsui int
83 1.1 tsutsui bfs_init2(struct bfs **bfsp, int bfs_sector, struct sector_io_ops *io,
84 1.7 thorpej bool debug)
85 1.1 tsutsui {
86 1.1 tsutsui struct bfs *bfs;
87 1.1 tsutsui size_t memsize;
88 1.1 tsutsui uint8_t *p;
89 1.1 tsutsui int err;
90 1.1 tsutsui
91 1.1 tsutsui /* 1. */
92 1.1 tsutsui DPRINTF(debug, "bfs sector = %d\n", bfs_sector);
93 1.1 tsutsui if ((bfs = (void *)__MALLOC(sizeof(struct bfs), M_BFS, M_NOWAIT)) == 0)
94 1.1 tsutsui return ENOMEM;
95 1.1 tsutsui memset(bfs, 0, sizeof *bfs);
96 1.1 tsutsui bfs->io = io;
97 1.1 tsutsui bfs->debug = debug;
98 1.1 tsutsui
99 1.1 tsutsui /* 2. */
100 1.1 tsutsui if ((err = bfs_init_superblock(bfs, bfs_sector, &memsize)) != 0) {
101 1.1 tsutsui bfs_fini(bfs);
102 1.1 tsutsui return err;
103 1.1 tsutsui }
104 1.5 martin DPRINTF(debug, "bfs super block + inode area = %zd\n", memsize);
105 1.1 tsutsui bfs->super_block_size = memsize;
106 1.1 tsutsui if ((p = (void *)__MALLOC(memsize, M_BFS, M_NOWAIT)) == 0) {
107 1.1 tsutsui bfs_fini(bfs);
108 1.1 tsutsui return ENOMEM;
109 1.1 tsutsui }
110 1.1 tsutsui /* 3. */
111 1.1 tsutsui if ((err = bfs_init_inode(bfs, p, &memsize)) != 0) {
112 1.1 tsutsui bfs_fini(bfs);
113 1.1 tsutsui return err;
114 1.1 tsutsui }
115 1.5 martin DPRINTF(debug, "bfs dirent area = %zd\n", memsize);
116 1.1 tsutsui bfs->dirent_size = memsize;
117 1.1 tsutsui if ((p = (void *)__MALLOC(memsize, M_BFS, M_NOWAIT)) == 0) {
118 1.1 tsutsui bfs_fini(bfs);
119 1.1 tsutsui return ENOMEM;
120 1.1 tsutsui }
121 1.1 tsutsui /* 4. */
122 1.1 tsutsui if ((err = bfs_init_dirent(bfs, p)) != 0) {
123 1.1 tsutsui bfs_fini(bfs);
124 1.1 tsutsui return err;
125 1.1 tsutsui }
126 1.1 tsutsui
127 1.1 tsutsui #ifdef BFS_DEBUG
128 1.1 tsutsui bfs_dump(bfs);
129 1.1 tsutsui #endif
130 1.1 tsutsui *bfsp = bfs;
131 1.1 tsutsui
132 1.1 tsutsui return 0;
133 1.1 tsutsui }
134 1.1 tsutsui
135 1.1 tsutsui void
136 1.1 tsutsui bfs_fini(struct bfs *bfs)
137 1.1 tsutsui {
138 1.1 tsutsui
139 1.1 tsutsui if (bfs == 0)
140 1.1 tsutsui return;
141 1.1 tsutsui if (bfs->super_block)
142 1.1 tsutsui __FREE(bfs->super_block, bfs->super_block_size, M_BFS);
143 1.1 tsutsui if (bfs->dirent)
144 1.1 tsutsui __FREE(bfs->dirent, bfs->dirent_size, M_BFS);
145 1.1 tsutsui __FREE(bfs, sizeof(struct bfs), M_BFS);
146 1.1 tsutsui }
147 1.1 tsutsui
148 1.1 tsutsui STATIC int
149 1.1 tsutsui bfs_init_superblock(struct bfs *bfs, int bfs_sector, size_t *required_memory)
150 1.1 tsutsui {
151 1.1 tsutsui struct bfs_super_block super;
152 1.1 tsutsui
153 1.1 tsutsui bfs->start_sector = bfs_sector;
154 1.1 tsutsui
155 1.1 tsutsui /* Read super block */
156 1.1 tsutsui if (!bfs->io->read(bfs->io, (uint8_t *)&super, bfs_sector))
157 1.1 tsutsui return EIO;
158 1.1 tsutsui
159 1.1 tsutsui if (!bfs_superblock_valid(&super))
160 1.1 tsutsui return EINVAL;
161 1.1 tsutsui
162 1.1 tsutsui /* i-node table size */
163 1.1 tsutsui bfs->data_start = super.header.data_start_byte;
164 1.1 tsutsui bfs->data_end = super.header.data_end_byte;
165 1.1 tsutsui
166 1.1 tsutsui bfs->max_inode = (bfs->data_start - sizeof(struct bfs_super_block)) /
167 1.1 tsutsui sizeof(struct bfs_inode);
168 1.1 tsutsui
169 1.1 tsutsui *required_memory = ROUND_SECTOR(bfs->data_start);
170 1.1 tsutsui
171 1.1 tsutsui return 0;
172 1.1 tsutsui }
173 1.1 tsutsui
174 1.1 tsutsui STATIC int
175 1.1 tsutsui bfs_init_inode(struct bfs *bfs, uint8_t *p, size_t *required_memory)
176 1.1 tsutsui {
177 1.1 tsutsui struct bfs_inode *inode, *root_inode;
178 1.1 tsutsui int i;
179 1.1 tsutsui
180 1.1 tsutsui if (!bfs->io->read_n(bfs->io, p, bfs->start_sector,
181 1.1 tsutsui bfs->data_start >> DEV_BSHIFT))
182 1.1 tsutsui return EIO;
183 1.1 tsutsui
184 1.1 tsutsui bfs->super_block = (struct bfs_super_block *)p;
185 1.1 tsutsui bfs->inode = (struct bfs_inode *)(p + sizeof(struct bfs_super_block));
186 1.1 tsutsui p += bfs->data_start;
187 1.1 tsutsui
188 1.1 tsutsui bfs->n_inode = 0;
189 1.1 tsutsui inode = bfs->inode;
190 1.1 tsutsui root_inode = 0;
191 1.1 tsutsui for (i = 0; i < bfs->max_inode; i++, inode++) {
192 1.1 tsutsui if (inode->number != 0) {
193 1.1 tsutsui bfs->n_inode++;
194 1.1 tsutsui if (inode->number == BFS_ROOT_INODE)
195 1.1 tsutsui root_inode = inode;
196 1.1 tsutsui }
197 1.1 tsutsui }
198 1.1 tsutsui DPRINTF(bfs->debug, "inode: %d/%d\n", bfs->n_inode, bfs->max_inode);
199 1.1 tsutsui
200 1.1 tsutsui if (root_inode == 0) {
201 1.1 tsutsui DPRINTF(bfs->debug, "no root directory.\n");
202 1.1 tsutsui return ENOTDIR;
203 1.1 tsutsui }
204 1.1 tsutsui /* dirent table size */
205 1.1 tsutsui DPRINTF(bfs->debug, "root inode: %d-%d\n", root_inode->start_sector,
206 1.1 tsutsui root_inode->end_sector);
207 1.1 tsutsui bfs->root_inode = root_inode;
208 1.1 tsutsui
209 1.1 tsutsui *required_memory = (root_inode->end_sector -
210 1.1 tsutsui root_inode->start_sector + 1) << DEV_BSHIFT;
211 1.1 tsutsui
212 1.1 tsutsui return 0;
213 1.1 tsutsui }
214 1.1 tsutsui
215 1.1 tsutsui STATIC int
216 1.1 tsutsui bfs_init_dirent(struct bfs *bfs, uint8_t *p)
217 1.1 tsutsui {
218 1.1 tsutsui struct bfs_dirent *file;
219 1.1 tsutsui struct bfs_inode *inode = bfs->root_inode;
220 1.1 tsutsui int i, n;
221 1.1 tsutsui
222 1.1 tsutsui n = inode->end_sector - inode->start_sector + 1;
223 1.1 tsutsui
224 1.1 tsutsui if (!bfs->io->read_n(bfs->io, p,
225 1.1 tsutsui bfs->start_sector + inode->start_sector, n))
226 1.1 tsutsui return EIO;
227 1.1 tsutsui
228 1.1 tsutsui bfs->dirent = (struct bfs_dirent *)p;
229 1.1 tsutsui bfs->max_dirent = (n << DEV_BSHIFT) / sizeof(struct bfs_dirent);
230 1.1 tsutsui
231 1.1 tsutsui file = bfs->dirent;
232 1.1 tsutsui bfs->n_dirent = 0;
233 1.1 tsutsui for (i = 0; i < bfs->max_dirent; i++, file++)
234 1.1 tsutsui if (file->inode != 0)
235 1.1 tsutsui bfs->n_dirent++;
236 1.1 tsutsui
237 1.1 tsutsui DPRINTF(bfs->debug, "dirent: %d/%d\n", bfs->n_dirent, bfs->max_dirent);
238 1.1 tsutsui
239 1.1 tsutsui return 0;
240 1.1 tsutsui }
241 1.1 tsutsui
242 1.1 tsutsui int
243 1.1 tsutsui bfs_file_read(const struct bfs *bfs, const char *fname, void *buf, size_t bufsz,
244 1.1 tsutsui size_t *read_size)
245 1.1 tsutsui {
246 1.1 tsutsui int start, end, n;
247 1.1 tsutsui size_t sz;
248 1.1 tsutsui uint8_t tmpbuf[DEV_BSIZE];
249 1.1 tsutsui uint8_t *p;
250 1.1 tsutsui
251 1.1 tsutsui if (!bfs_file_lookup(bfs, fname, &start, &end, &sz))
252 1.1 tsutsui return ENOENT;
253 1.1 tsutsui
254 1.1 tsutsui if (sz > bufsz)
255 1.1 tsutsui return ENOMEM;
256 1.1 tsutsui
257 1.1 tsutsui p = buf;
258 1.1 tsutsui n = end - start;
259 1.12 uch if (!bfs->io->read_n(bfs->io, p, start, n))
260 1.12 uch return EIO;
261 1.1 tsutsui /* last sector */
262 1.1 tsutsui n *= DEV_BSIZE;
263 1.12 uch if (!bfs->io->read(bfs->io, tmpbuf, end))
264 1.12 uch return EIO;
265 1.1 tsutsui memcpy(p + n, tmpbuf, sz - n);
266 1.1 tsutsui
267 1.1 tsutsui if (read_size)
268 1.1 tsutsui *read_size = sz;
269 1.1 tsutsui
270 1.1 tsutsui return 0;
271 1.1 tsutsui }
272 1.1 tsutsui
273 1.1 tsutsui int
274 1.1 tsutsui bfs_file_write(struct bfs *bfs, const char *fname, void *buf,
275 1.1 tsutsui size_t bufsz)
276 1.1 tsutsui {
277 1.1 tsutsui struct bfs_fileattr attr;
278 1.1 tsutsui struct bfs_dirent *dirent;
279 1.6 tsutsui char name[BFS_FILENAME_MAXLEN];
280 1.1 tsutsui int err;
281 1.1 tsutsui
282 1.1 tsutsui strncpy(name, fname, BFS_FILENAME_MAXLEN);
283 1.1 tsutsui
284 1.1 tsutsui if (bfs_dirent_lookup_by_name(bfs, name, &dirent)) {
285 1.1 tsutsui struct bfs_inode *inode;
286 1.1 tsutsui if (!bfs_inode_lookup(bfs, dirent->inode, &inode)) {
287 1.1 tsutsui DPRINTF(bfs->debug, "%s: dirent found, but inode "
288 1.1 tsutsui "not found. inconsistent filesystem.\n",
289 1.10 perry __func__);
290 1.1 tsutsui return ENOENT;
291 1.1 tsutsui }
292 1.1 tsutsui attr = inode->attr; /* copy old attribute */
293 1.1 tsutsui bfs_file_delete(bfs, name);
294 1.1 tsutsui if ((err = bfs_file_create(bfs, name, buf, bufsz, &attr)) != 0)
295 1.1 tsutsui return err;
296 1.1 tsutsui } else {
297 1.1 tsutsui memset(&attr, 0xff, sizeof attr); /* Set VNOVAL all */
298 1.1 tsutsui #ifdef _KERNEL
299 1.4 martin attr.atime = time_second;
300 1.4 martin attr.ctime = time_second;
301 1.4 martin attr.mtime = time_second;
302 1.1 tsutsui #endif
303 1.1 tsutsui if ((err = bfs_file_create(bfs, name, buf, bufsz, &attr)) != 0)
304 1.1 tsutsui return err;
305 1.1 tsutsui }
306 1.1 tsutsui
307 1.1 tsutsui return 0;
308 1.1 tsutsui }
309 1.1 tsutsui
310 1.1 tsutsui int
311 1.1 tsutsui bfs_file_delete(struct bfs *bfs, const char *fname)
312 1.1 tsutsui {
313 1.1 tsutsui struct bfs_inode *inode;
314 1.1 tsutsui struct bfs_dirent *dirent;
315 1.1 tsutsui
316 1.1 tsutsui if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
317 1.1 tsutsui return ENOENT;
318 1.1 tsutsui
319 1.1 tsutsui if (!bfs_inode_lookup(bfs, dirent->inode, &inode))
320 1.1 tsutsui return ENOENT;
321 1.1 tsutsui
322 1.1 tsutsui memset(dirent, 0, sizeof *dirent);
323 1.1 tsutsui memset(inode, 0, sizeof *inode);
324 1.1 tsutsui bfs->n_inode--;
325 1.1 tsutsui bfs->n_dirent--;
326 1.1 tsutsui
327 1.8 thorpej bfs_writeback_dirent(bfs, dirent, false);
328 1.1 tsutsui bfs_writeback_inode(bfs, inode);
329 1.10 perry DPRINTF(bfs->debug, "%s: \"%s\" deleted.\n", __func__, fname);
330 1.1 tsutsui
331 1.1 tsutsui return 0;
332 1.1 tsutsui }
333 1.1 tsutsui
334 1.1 tsutsui int
335 1.1 tsutsui bfs_file_rename(struct bfs *bfs, const char *from_name, const char *to_name)
336 1.1 tsutsui {
337 1.1 tsutsui struct bfs_dirent *dirent;
338 1.1 tsutsui int err = 0;
339 1.1 tsutsui
340 1.1 tsutsui if (strlen(to_name) > BFS_FILENAME_MAXLEN) {
341 1.1 tsutsui err = ENAMETOOLONG;
342 1.1 tsutsui goto out;
343 1.1 tsutsui }
344 1.1 tsutsui if (!bfs_dirent_lookup_by_name(bfs, from_name, &dirent)) {
345 1.1 tsutsui err = ENOENT;
346 1.1 tsutsui goto out;
347 1.1 tsutsui }
348 1.1 tsutsui
349 1.1 tsutsui bfs_file_delete(bfs, to_name);
350 1.1 tsutsui strncpy(dirent->name, to_name, BFS_FILENAME_MAXLEN);
351 1.8 thorpej bfs_writeback_dirent(bfs, dirent, false);
352 1.1 tsutsui
353 1.1 tsutsui out:
354 1.10 perry DPRINTF(bfs->debug, "%s: \"%s\" -> \"%s\" error=%d.\n", __func__,
355 1.1 tsutsui from_name, to_name, err);
356 1.1 tsutsui
357 1.1 tsutsui return err;
358 1.1 tsutsui }
359 1.1 tsutsui
360 1.1 tsutsui int
361 1.1 tsutsui bfs_file_create(struct bfs *bfs, const char *fname, void *buf, size_t bufsz,
362 1.1 tsutsui const struct bfs_fileattr *attr)
363 1.1 tsutsui {
364 1.1 tsutsui struct bfs_inode *inode;
365 1.1 tsutsui struct bfs_dirent *file;
366 1.1 tsutsui int i, j, n, start;
367 1.1 tsutsui uint8_t *p, tmpbuf[DEV_BSIZE];
368 1.1 tsutsui int err;
369 1.1 tsutsui
370 1.1 tsutsui /* Find free i-node and data block */
371 1.1 tsutsui if ((err = bfs_inode_alloc(bfs, &inode, &j, &start)) != 0)
372 1.1 tsutsui return err;
373 1.1 tsutsui
374 1.1 tsutsui /* File size (unit block) */
375 1.1 tsutsui n = (ROUND_SECTOR(bufsz) >> DEV_BSHIFT) - 1;
376 1.1 tsutsui if (n < 0) /* bufsz == 0 */
377 1.1 tsutsui n = 0;
378 1.1 tsutsui
379 1.1 tsutsui if ((start + n) * DEV_BSIZE >= bfs->data_end) {
380 1.1 tsutsui DPRINTF(bfs->debug, "disk full.\n");
381 1.1 tsutsui return ENOSPC;
382 1.1 tsutsui }
383 1.1 tsutsui
384 1.1 tsutsui /* Find free dirent */
385 1.1 tsutsui for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
386 1.1 tsutsui if (file->inode == 0)
387 1.1 tsutsui break;
388 1.1 tsutsui if (i == bfs->max_dirent) {
389 1.1 tsutsui DPRINTF(bfs->debug, "dirent full.\n");
390 1.1 tsutsui return ENOSPC;
391 1.1 tsutsui }
392 1.1 tsutsui
393 1.1 tsutsui /* i-node */
394 1.1 tsutsui memset(inode, 0, sizeof *inode);
395 1.1 tsutsui inode->number = j;
396 1.1 tsutsui inode->start_sector = start;
397 1.1 tsutsui inode->end_sector = start + n;
398 1.1 tsutsui inode->eof_offset_byte = start * DEV_BSIZE + bufsz - 1;
399 1.1 tsutsui /* i-node attribute */
400 1.1 tsutsui inode->attr.type = 1;
401 1.1 tsutsui inode->attr.mode = 0;
402 1.1 tsutsui inode->attr.nlink = 1;
403 1.1 tsutsui bfs_inode_set_attr(bfs, inode, attr);
404 1.1 tsutsui
405 1.1 tsutsui /* Dirent */
406 1.1 tsutsui memset(file, 0, sizeof *file);
407 1.1 tsutsui file->inode = inode->number;
408 1.1 tsutsui strncpy(file->name, fname, BFS_FILENAME_MAXLEN);
409 1.1 tsutsui
410 1.10 perry DPRINTF(bfs->debug, "%s: start %d end %d\n", __func__,
411 1.1 tsutsui inode->start_sector, inode->end_sector);
412 1.1 tsutsui
413 1.1 tsutsui if (buf != 0) {
414 1.1 tsutsui p = (uint8_t *)buf;
415 1.1 tsutsui /* Data block */
416 1.1 tsutsui n = 0;
417 1.1 tsutsui for (i = inode->start_sector; i < inode->end_sector; i++) {
418 1.1 tsutsui if (!bfs->io->write(bfs->io, p, bfs->start_sector + i))
419 1.1 tsutsui return EIO;
420 1.1 tsutsui p += DEV_BSIZE;
421 1.1 tsutsui n += DEV_BSIZE;
422 1.1 tsutsui }
423 1.1 tsutsui /* last sector */
424 1.1 tsutsui memset(tmpbuf, 0, DEV_BSIZE);
425 1.1 tsutsui memcpy(tmpbuf, p, bufsz - n);
426 1.1 tsutsui if (!bfs->io->write(bfs->io, tmpbuf, bfs->start_sector + i))
427 1.1 tsutsui return EIO;
428 1.1 tsutsui }
429 1.1 tsutsui /* Update */
430 1.1 tsutsui bfs->n_inode++;
431 1.1 tsutsui bfs->n_dirent++;
432 1.8 thorpej bfs_writeback_dirent(bfs, file, true);
433 1.1 tsutsui bfs_writeback_inode(bfs, inode);
434 1.1 tsutsui
435 1.1 tsutsui return 0;
436 1.1 tsutsui }
437 1.1 tsutsui
438 1.7 thorpej STATIC bool
439 1.1 tsutsui bfs_writeback_dirent(const struct bfs *bfs, struct bfs_dirent *dir,
440 1.7 thorpej bool create)
441 1.1 tsutsui {
442 1.1 tsutsui struct bfs_dirent *dir_base = bfs->dirent;
443 1.1 tsutsui struct bfs_inode *root_inode = bfs->root_inode;
444 1.5 martin uintptr_t eof;
445 1.1 tsutsui int i;
446 1.1 tsutsui
447 1.1 tsutsui i = ((dir - dir_base) * sizeof *dir) >> DEV_BSHIFT;
448 1.1 tsutsui
449 1.5 martin eof = (uintptr_t)(dir + 1) - 1;
450 1.5 martin eof = eof - (uintptr_t)dir_base +
451 1.1 tsutsui (root_inode->start_sector << DEV_BSHIFT);
452 1.1 tsutsui
453 1.1 tsutsui /* update root directory inode */
454 1.1 tsutsui #if 0
455 1.1 tsutsui printf("eof new=%d old=%d\n", eof, root_inode->eof_offset_byte);
456 1.1 tsutsui #endif
457 1.1 tsutsui if (create) {
458 1.1 tsutsui if (eof > root_inode->eof_offset_byte) {
459 1.1 tsutsui root_inode->eof_offset_byte = eof;
460 1.1 tsutsui }
461 1.1 tsutsui } else {
462 1.1 tsutsui /* delete the last entry */
463 1.1 tsutsui if (eof == root_inode->eof_offset_byte) {
464 1.1 tsutsui root_inode->eof_offset_byte = eof - sizeof *dir;
465 1.1 tsutsui }
466 1.1 tsutsui }
467 1.1 tsutsui bfs_writeback_inode(bfs, root_inode);
468 1.1 tsutsui
469 1.1 tsutsui /* update dirent */
470 1.1 tsutsui return bfs->io->write(bfs->io, (uint8_t *)dir_base + (i << DEV_BSHIFT),
471 1.1 tsutsui bfs->start_sector + bfs->root_inode->start_sector + i);
472 1.1 tsutsui }
473 1.1 tsutsui
474 1.7 thorpej STATIC bool
475 1.1 tsutsui bfs_writeback_inode(const struct bfs *bfs, struct bfs_inode *inode)
476 1.1 tsutsui {
477 1.1 tsutsui struct bfs_inode *inode_base = bfs->inode;
478 1.1 tsutsui int i;
479 1.1 tsutsui
480 1.1 tsutsui i = ((inode - inode_base) * sizeof *inode) >> DEV_BSHIFT;
481 1.1 tsutsui
482 1.1 tsutsui return bfs->io->write(bfs->io,
483 1.1 tsutsui (uint8_t *)inode_base + (i << DEV_BSHIFT),
484 1.1 tsutsui bfs->start_sector + 1/*super block*/ + i);
485 1.1 tsutsui }
486 1.1 tsutsui
487 1.7 thorpej bool
488 1.1 tsutsui bfs_file_lookup(const struct bfs *bfs, const char *fname, int *start, int *end,
489 1.1 tsutsui size_t *size)
490 1.1 tsutsui {
491 1.1 tsutsui struct bfs_inode *inode;
492 1.1 tsutsui struct bfs_dirent *dirent;
493 1.1 tsutsui
494 1.1 tsutsui if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
495 1.8 thorpej return false;
496 1.1 tsutsui if (!bfs_inode_lookup(bfs, dirent->inode, &inode))
497 1.8 thorpej return false;
498 1.1 tsutsui
499 1.1 tsutsui if (start)
500 1.1 tsutsui *start = inode->start_sector + bfs->start_sector;
501 1.1 tsutsui if (end)
502 1.1 tsutsui *end = inode->end_sector + bfs->start_sector;
503 1.1 tsutsui if (size)
504 1.1 tsutsui *size = bfs_file_size(inode);
505 1.1 tsutsui
506 1.5 martin DPRINTF(bfs->debug, "%s: %d + %d -> %d (%zd)\n",
507 1.1 tsutsui fname, bfs->start_sector, inode->start_sector,
508 1.1 tsutsui inode->end_sector, *size);
509 1.1 tsutsui
510 1.8 thorpej return true;
511 1.1 tsutsui }
512 1.1 tsutsui
513 1.7 thorpej bool
514 1.1 tsutsui bfs_dirent_lookup_by_inode(const struct bfs *bfs, int inode,
515 1.1 tsutsui struct bfs_dirent **dirent)
516 1.1 tsutsui {
517 1.1 tsutsui struct bfs_dirent *file;
518 1.1 tsutsui int i;
519 1.1 tsutsui
520 1.1 tsutsui for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
521 1.1 tsutsui if (file->inode == inode)
522 1.1 tsutsui break;
523 1.1 tsutsui
524 1.1 tsutsui if (i == bfs->max_dirent)
525 1.8 thorpej return false;
526 1.1 tsutsui
527 1.1 tsutsui *dirent = file;
528 1.1 tsutsui
529 1.8 thorpej return true;
530 1.1 tsutsui }
531 1.1 tsutsui
532 1.7 thorpej bool
533 1.1 tsutsui bfs_dirent_lookup_by_name(const struct bfs *bfs, const char *fname,
534 1.1 tsutsui struct bfs_dirent **dirent)
535 1.1 tsutsui {
536 1.1 tsutsui struct bfs_dirent *file;
537 1.1 tsutsui int i;
538 1.1 tsutsui
539 1.1 tsutsui for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
540 1.1 tsutsui if ((file->inode != 0) &&
541 1.1 tsutsui (strncmp(file->name, fname, BFS_FILENAME_MAXLEN) ==0))
542 1.1 tsutsui break;
543 1.1 tsutsui
544 1.1 tsutsui if (i == bfs->max_dirent)
545 1.8 thorpej return false;
546 1.1 tsutsui
547 1.1 tsutsui *dirent = file;
548 1.1 tsutsui
549 1.8 thorpej return true;
550 1.1 tsutsui }
551 1.1 tsutsui
552 1.7 thorpej bool
553 1.1 tsutsui bfs_inode_lookup(const struct bfs *bfs, ino_t n, struct bfs_inode **iinode)
554 1.1 tsutsui {
555 1.1 tsutsui struct bfs_inode *inode;
556 1.1 tsutsui int i;
557 1.1 tsutsui
558 1.1 tsutsui for (inode = bfs->inode, i = 0; i < bfs->max_inode; i++, inode++)
559 1.1 tsutsui if (inode->number == n)
560 1.1 tsutsui break;
561 1.1 tsutsui
562 1.1 tsutsui if (i == bfs->max_inode)
563 1.8 thorpej return false;
564 1.1 tsutsui
565 1.1 tsutsui *iinode = inode;
566 1.1 tsutsui
567 1.8 thorpej return true;
568 1.1 tsutsui }
569 1.1 tsutsui
570 1.1 tsutsui size_t
571 1.1 tsutsui bfs_file_size(const struct bfs_inode *inode)
572 1.1 tsutsui {
573 1.1 tsutsui
574 1.1 tsutsui return inode->eof_offset_byte - inode->start_sector * DEV_BSIZE + 1;
575 1.1 tsutsui }
576 1.1 tsutsui
577 1.1 tsutsui STATIC int
578 1.1 tsutsui bfs_inode_alloc(const struct bfs *bfs, struct bfs_inode **free_inode,
579 1.1 tsutsui int *free_inode_number, int *free_block)
580 1.1 tsutsui {
581 1.1 tsutsui struct bfs_inode *jnode, *inode;
582 1.1 tsutsui int i, j, start;
583 1.1 tsutsui
584 1.1 tsutsui j = start = 0;
585 1.1 tsutsui inode = bfs->inode;
586 1.1 tsutsui jnode = 0;
587 1.1 tsutsui
588 1.1 tsutsui for (i = BFS_ROOT_INODE; i < bfs->max_inode; i++, inode++) {
589 1.1 tsutsui /* Steal i-node # */
590 1.1 tsutsui if (j == 0)
591 1.1 tsutsui j = i;
592 1.1 tsutsui
593 1.1 tsutsui /* Get free i-node */
594 1.1 tsutsui if (jnode == 0 && (inode->number == 0))
595 1.1 tsutsui jnode = inode;
596 1.1 tsutsui
597 1.1 tsutsui /* Get free i-node # and data block */
598 1.1 tsutsui if (inode->number != 0) {
599 1.1 tsutsui if (inode->end_sector > start)
600 1.1 tsutsui start = inode->end_sector;
601 1.1 tsutsui if (inode->number == j)
602 1.1 tsutsui j = 0; /* conflict */
603 1.1 tsutsui }
604 1.1 tsutsui }
605 1.1 tsutsui start++;
606 1.1 tsutsui
607 1.1 tsutsui if (jnode == 0) {
608 1.1 tsutsui DPRINTF(bfs->debug, "i-node full.\n");
609 1.1 tsutsui return ENOSPC;
610 1.1 tsutsui }
611 1.1 tsutsui
612 1.1 tsutsui if (start * DEV_BSIZE >= bfs->data_end) {
613 1.1 tsutsui DPRINTF(bfs->debug, "data block full.\n");
614 1.1 tsutsui /* compaction here ? */
615 1.1 tsutsui return ENOSPC;
616 1.1 tsutsui }
617 1.1 tsutsui if (free_inode)
618 1.1 tsutsui *free_inode = jnode;
619 1.1 tsutsui if (free_inode_number)
620 1.1 tsutsui *free_inode_number = j;
621 1.1 tsutsui if (free_block)
622 1.1 tsutsui *free_block = start;
623 1.1 tsutsui
624 1.1 tsutsui return 0;
625 1.1 tsutsui }
626 1.1 tsutsui
627 1.1 tsutsui void
628 1.1 tsutsui bfs_inode_set_attr(const struct bfs *bfs, struct bfs_inode *inode,
629 1.1 tsutsui const struct bfs_fileattr *from)
630 1.1 tsutsui {
631 1.1 tsutsui struct bfs_fileattr *to = &inode->attr;
632 1.1 tsutsui
633 1.1 tsutsui if (from != NULL) {
634 1.1 tsutsui if (from->uid != (uid_t)-1)
635 1.1 tsutsui to->uid = from->uid;
636 1.1 tsutsui if (from->gid != (uid_t)-1)
637 1.1 tsutsui to->gid = from->gid;
638 1.1 tsutsui if (from->mode != (mode_t)-1)
639 1.1 tsutsui to->mode = from->mode;
640 1.1 tsutsui if (from->atime != -1)
641 1.1 tsutsui to->atime = from->atime;
642 1.1 tsutsui if (from->ctime != -1)
643 1.1 tsutsui to->ctime = from->ctime;
644 1.1 tsutsui if (from->mtime != -1)
645 1.1 tsutsui to->mtime = from->mtime;
646 1.1 tsutsui }
647 1.1 tsutsui bfs_writeback_inode(bfs, inode);
648 1.1 tsutsui }
649 1.1 tsutsui
650 1.7 thorpej STATIC bool
651 1.1 tsutsui bfs_superblock_valid(const struct bfs_super_block *super)
652 1.1 tsutsui {
653 1.1 tsutsui
654 1.1 tsutsui return super->header.magic == BFS_MAGIC;
655 1.1 tsutsui }
656 1.1 tsutsui
657 1.7 thorpej bool
658 1.1 tsutsui bfs_dump(const struct bfs *bfs)
659 1.1 tsutsui {
660 1.1 tsutsui const struct bfs_super_block_header *h;
661 1.1 tsutsui const struct bfs_compaction *compaction;
662 1.1 tsutsui const struct bfs_inode *inode;
663 1.1 tsutsui struct bfs_dirent *file;
664 1.5 martin int i, j, s, e;
665 1.5 martin size_t bytes;
666 1.1 tsutsui
667 1.1 tsutsui if (!bfs_superblock_valid(bfs->super_block)) {
668 1.1 tsutsui DPRINTF(bfs->debug, "invalid bfs super block.\n");
669 1.8 thorpej return false;
670 1.1 tsutsui }
671 1.1 tsutsui h = &bfs->super_block->header;
672 1.1 tsutsui compaction = &bfs->super_block->compaction;
673 1.1 tsutsui
674 1.5 martin DPRINTF(bfs->debug, "super block %zdbyte, inode %zdbyte, dirent %zdbyte\n",
675 1.1 tsutsui sizeof *bfs->super_block, sizeof *inode, sizeof *file);
676 1.1 tsutsui
677 1.1 tsutsui DPRINTF(bfs->debug, "magic=%x\n", h->magic);
678 1.1 tsutsui DPRINTF(bfs->debug, "data_start_byte=0x%x\n", h->data_start_byte);
679 1.1 tsutsui DPRINTF(bfs->debug, "data_end_byte=0x%x\n", h->data_end_byte);
680 1.1 tsutsui DPRINTF(bfs->debug, "from=%#x\n", compaction->from);
681 1.1 tsutsui DPRINTF(bfs->debug, "to=%#x\n", compaction->to);
682 1.1 tsutsui DPRINTF(bfs->debug, "from_backup=%#x\n", compaction->from_backup);
683 1.1 tsutsui DPRINTF(bfs->debug, "to_backup=%#x\n", compaction->to_backup);
684 1.1 tsutsui DPRINTF(bfs->debug, "fsname=%s\n", bfs->super_block->fsname);
685 1.1 tsutsui DPRINTF(bfs->debug, "volume=%s\n", bfs->super_block->volume);
686 1.1 tsutsui
687 1.1 tsutsui /* inode list */
688 1.1 tsutsui DPRINTF(bfs->debug, "[inode index list]\n");
689 1.1 tsutsui for (inode = bfs->inode, i = j = 0; i < bfs->max_inode; inode++, i++) {
690 1.1 tsutsui if (inode->number != 0) {
691 1.1 tsutsui const struct bfs_fileattr *attr = &inode->attr;
692 1.1 tsutsui DPRINTF(bfs->debug, "%3d %8d %8d %8d (%d) ",
693 1.1 tsutsui inode->number,
694 1.1 tsutsui inode->eof_offset_byte -
695 1.1 tsutsui (inode->start_sector * DEV_BSIZE) + 1,/* file size*/
696 1.1 tsutsui inode->start_sector,
697 1.1 tsutsui inode->end_sector, i);
698 1.1 tsutsui
699 1.1 tsutsui DPRINTF(bfs->debug, "%d %d %d %d %d %08x %08x %08x\n",
700 1.1 tsutsui attr->type, attr->mode, attr->uid, attr->gid,
701 1.1 tsutsui attr->nlink, attr->atime, attr->mtime, attr->ctime);
702 1.1 tsutsui j++;
703 1.1 tsutsui }
704 1.1 tsutsui }
705 1.1 tsutsui if (j != bfs->n_inode) {
706 1.1 tsutsui DPRINTF(bfs->debug, "inconsistent cached data. (i-node)\n");
707 1.8 thorpej return false;
708 1.1 tsutsui }
709 1.1 tsutsui DPRINTF(bfs->debug, "total %d i-node.\n", j);
710 1.1 tsutsui
711 1.1 tsutsui /* file list */
712 1.1 tsutsui DPRINTF(bfs->debug, "[dirent index list]\n");
713 1.1 tsutsui DPRINTF(bfs->debug, "%d file entries.\n", bfs->max_dirent);
714 1.1 tsutsui file = bfs->dirent;
715 1.1 tsutsui for (i = j = 0; i < bfs->max_dirent; i++, file++) {
716 1.1 tsutsui if (file->inode != 0) {
717 1.1 tsutsui if (bfs_file_lookup(bfs, file->name, &s, &e, &bytes))
718 1.5 martin DPRINTF(bfs->debug, "%3d %14s %8d %8d %8zd\n",
719 1.1 tsutsui file->inode, file->name, s, e, bytes);
720 1.1 tsutsui j++;
721 1.1 tsutsui }
722 1.1 tsutsui }
723 1.1 tsutsui if (j != bfs->n_dirent) {
724 1.1 tsutsui DPRINTF(bfs->debug, "inconsistent cached data. (dirent)\n");
725 1.8 thorpej return false;
726 1.1 tsutsui }
727 1.1 tsutsui DPRINTF(bfs->debug, "%d files.\n", j);
728 1.1 tsutsui
729 1.8 thorpej return true;
730 1.1 tsutsui }
731