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