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