Home | History | Annotate | Line # | Download | only in sysvbfs
bfs.c revision 1.10.8.1
      1 /*	$NetBSD: bfs.c,v 1.10.8.1 2008/05/18 12:35:02 yamt 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.10.8.1 2008/05/18 12:35:02 yamt 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_NOWAIT)) == 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_NOWAIT)) == 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_NOWAIT)) == 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 	bfs->io->read_n(bfs->io, p, start, n);
    260 	/* last sector */
    261 	n *= DEV_BSIZE;
    262 	bfs->io->read(bfs->io, tmpbuf, end);
    263 	memcpy(p + n, tmpbuf, sz - n);
    264 
    265 	if (read_size)
    266 		*read_size = sz;
    267 
    268 	return 0;
    269 }
    270 
    271 int
    272 bfs_file_write(struct bfs *bfs, const char *fname, void *buf,
    273     size_t bufsz)
    274 {
    275 	struct bfs_fileattr attr;
    276 	struct bfs_dirent *dirent;
    277 	char name[BFS_FILENAME_MAXLEN];
    278 	int err;
    279 
    280 	strncpy(name, fname, BFS_FILENAME_MAXLEN);
    281 
    282 	if (bfs_dirent_lookup_by_name(bfs, name, &dirent)) {
    283 		struct bfs_inode *inode;
    284 		if (!bfs_inode_lookup(bfs, dirent->inode, &inode)) {
    285 			DPRINTF(bfs->debug, "%s: dirent found, but inode "
    286 			    "not found. inconsistent filesystem.\n",
    287 			    __func__);
    288 			return ENOENT;
    289 		}
    290 		attr = inode->attr;	/* copy old attribute */
    291 		bfs_file_delete(bfs, name);
    292 		if ((err = bfs_file_create(bfs, name, buf, bufsz, &attr)) != 0)
    293 			return err;
    294 	} else {
    295 		memset(&attr, 0xff, sizeof attr);	/* Set VNOVAL all */
    296 #ifdef _KERNEL
    297 		attr.atime = time_second;
    298 		attr.ctime = time_second;
    299 		attr.mtime = time_second;
    300 #endif
    301 		if ((err = bfs_file_create(bfs, name, buf, bufsz, &attr)) != 0)
    302 			return err;
    303 	}
    304 
    305 	return 0;
    306 }
    307 
    308 int
    309 bfs_file_delete(struct bfs *bfs, const char *fname)
    310 {
    311 	struct bfs_inode *inode;
    312 	struct bfs_dirent *dirent;
    313 
    314 	if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
    315 		return ENOENT;
    316 
    317 	if (!bfs_inode_lookup(bfs, dirent->inode, &inode))
    318 		return ENOENT;
    319 
    320 	memset(dirent, 0, sizeof *dirent);
    321 	memset(inode, 0, sizeof *inode);
    322 	bfs->n_inode--;
    323 	bfs->n_dirent--;
    324 
    325 	bfs_writeback_dirent(bfs, dirent, false);
    326 	bfs_writeback_inode(bfs, inode);
    327 	DPRINTF(bfs->debug, "%s: \"%s\" deleted.\n", __func__, fname);
    328 
    329 	return 0;
    330 }
    331 
    332 int
    333 bfs_file_rename(struct bfs *bfs, const char *from_name, const char *to_name)
    334 {
    335 	struct bfs_dirent *dirent;
    336 	int err = 0;
    337 
    338 	if (strlen(to_name) > BFS_FILENAME_MAXLEN) {
    339 		err =  ENAMETOOLONG;
    340 		goto out;
    341 	}
    342 	if (!bfs_dirent_lookup_by_name(bfs, from_name, &dirent)) {
    343 		err = ENOENT;
    344 		goto out;
    345 	}
    346 
    347 	bfs_file_delete(bfs, to_name);
    348 	strncpy(dirent->name, to_name, BFS_FILENAME_MAXLEN);
    349 	bfs_writeback_dirent(bfs, dirent, false);
    350 
    351  out:
    352 	DPRINTF(bfs->debug, "%s: \"%s\" -> \"%s\" error=%d.\n", __func__,
    353 	    from_name, to_name, err);
    354 
    355 	return err;
    356 }
    357 
    358 int
    359 bfs_file_create(struct bfs *bfs, const char *fname, void *buf, size_t bufsz,
    360     const struct bfs_fileattr *attr)
    361 {
    362 	struct bfs_inode *inode;
    363 	struct bfs_dirent *file;
    364 	int i, j, n, start;
    365 	uint8_t *p, tmpbuf[DEV_BSIZE];
    366 	int err;
    367 
    368 	/* Find free i-node and data block */
    369 	if ((err = bfs_inode_alloc(bfs, &inode, &j, &start)) != 0)
    370 		return err;
    371 
    372 	/* File size (unit block) */
    373 	n = (ROUND_SECTOR(bufsz) >> DEV_BSHIFT) - 1;
    374 	if (n < 0)	/* bufsz == 0 */
    375 		n = 0;
    376 
    377 	if ((start + n) * DEV_BSIZE >= bfs->data_end) {
    378 		DPRINTF(bfs->debug, "disk full.\n");
    379 		return ENOSPC;
    380 	}
    381 
    382 	/* Find free dirent */
    383 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
    384 		if (file->inode == 0)
    385 			break;
    386 	if (i == bfs->max_dirent) {
    387 		DPRINTF(bfs->debug, "dirent full.\n");
    388 		return ENOSPC;
    389 	}
    390 
    391 	/* i-node */
    392 	memset(inode, 0, sizeof *inode);
    393 	inode->number = j;
    394 	inode->start_sector = start;
    395 	inode->end_sector = start + n;
    396 	inode->eof_offset_byte = start * DEV_BSIZE + bufsz - 1;
    397 	/* i-node attribute */
    398 	inode->attr.type = 1;
    399 	inode->attr.mode = 0;
    400 	inode->attr.nlink = 1;
    401 	bfs_inode_set_attr(bfs, inode, attr);
    402 
    403 	/* Dirent */
    404 	memset(file, 0, sizeof *file);
    405 	file->inode = inode->number;
    406 	strncpy(file->name, fname, BFS_FILENAME_MAXLEN);
    407 
    408 	DPRINTF(bfs->debug, "%s: start %d end %d\n", __func__,
    409 	    inode->start_sector, inode->end_sector);
    410 
    411 	if (buf != 0) {
    412 		p = (uint8_t *)buf;
    413 		/* Data block */
    414 		n = 0;
    415 		for (i = inode->start_sector; i < inode->end_sector; i++) {
    416 			if (!bfs->io->write(bfs->io, p, bfs->start_sector + i))
    417 				return EIO;
    418 			p += DEV_BSIZE;
    419 			n += DEV_BSIZE;
    420 		}
    421 		/* last sector */
    422 		memset(tmpbuf, 0, DEV_BSIZE);
    423 		memcpy(tmpbuf, p, bufsz - n);
    424 		if (!bfs->io->write(bfs->io, tmpbuf, bfs->start_sector + i))
    425 			return EIO;
    426 	}
    427 	/* Update */
    428 	bfs->n_inode++;
    429 	bfs->n_dirent++;
    430 	bfs_writeback_dirent(bfs, file, true);
    431 	bfs_writeback_inode(bfs, inode);
    432 
    433 	return 0;
    434 }
    435 
    436 STATIC bool
    437 bfs_writeback_dirent(const struct bfs *bfs, struct bfs_dirent *dir,
    438     bool create)
    439 {
    440 	struct bfs_dirent *dir_base = bfs->dirent;
    441 	struct bfs_inode *root_inode = bfs->root_inode;
    442 	uintptr_t eof;
    443 	int i;
    444 
    445 	i = ((dir - dir_base) * sizeof *dir) >> DEV_BSHIFT;
    446 
    447 	eof = (uintptr_t)(dir + 1) - 1;
    448 	eof = eof - (uintptr_t)dir_base +
    449 	    (root_inode->start_sector << DEV_BSHIFT);
    450 
    451 	/* update root directory inode */
    452 #if 0
    453 	printf("eof new=%d old=%d\n", eof, root_inode->eof_offset_byte);
    454 #endif
    455 	if (create) {
    456 		if (eof > root_inode->eof_offset_byte) {
    457 			root_inode->eof_offset_byte = eof;
    458 		}
    459 	} else {
    460 		/* delete the last entry */
    461 		if (eof == root_inode->eof_offset_byte) {
    462 			root_inode->eof_offset_byte = eof - sizeof *dir;
    463 		}
    464 	}
    465 	bfs_writeback_inode(bfs, root_inode);
    466 
    467 	/* update dirent */
    468 	return bfs->io->write(bfs->io, (uint8_t *)dir_base + (i << DEV_BSHIFT),
    469 	    bfs->start_sector + bfs->root_inode->start_sector + i);
    470 }
    471 
    472 STATIC bool
    473 bfs_writeback_inode(const struct bfs *bfs, struct bfs_inode *inode)
    474 {
    475 	struct bfs_inode *inode_base = bfs->inode;
    476 	int i;
    477 
    478 	i = ((inode - inode_base) * sizeof *inode) >> DEV_BSHIFT;
    479 
    480 	return bfs->io->write(bfs->io,
    481 	    (uint8_t *)inode_base + (i << DEV_BSHIFT),
    482 	    bfs->start_sector + 1/*super block*/ + i);
    483 }
    484 
    485 bool
    486 bfs_file_lookup(const struct bfs *bfs, const char *fname, int *start, int *end,
    487     size_t *size)
    488 {
    489 	struct bfs_inode *inode;
    490 	struct bfs_dirent *dirent;
    491 
    492 	if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
    493 		return false;
    494 	if (!bfs_inode_lookup(bfs, dirent->inode, &inode))
    495 		return false;
    496 
    497 	if (start)
    498 		*start = inode->start_sector + bfs->start_sector;
    499 	if (end)
    500 		*end = inode->end_sector + bfs->start_sector;
    501 	if (size)
    502 		*size = bfs_file_size(inode);
    503 
    504 	DPRINTF(bfs->debug, "%s: %d + %d -> %d (%zd)\n",
    505 	    fname, bfs->start_sector, inode->start_sector,
    506 	    inode->end_sector, *size);
    507 
    508 	return true;
    509 }
    510 
    511 bool
    512 bfs_dirent_lookup_by_inode(const struct bfs *bfs, int inode,
    513     struct bfs_dirent **dirent)
    514 {
    515 	struct bfs_dirent *file;
    516 	int i;
    517 
    518 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
    519 		if (file->inode == inode)
    520 			break;
    521 
    522 	if (i == bfs->max_dirent)
    523 		return false;
    524 
    525 	*dirent = file;
    526 
    527 	return true;
    528 }
    529 
    530 bool
    531 bfs_dirent_lookup_by_name(const struct bfs *bfs, const char *fname,
    532     struct bfs_dirent **dirent)
    533 {
    534 	struct bfs_dirent *file;
    535 	int i;
    536 
    537 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
    538 		if ((file->inode != 0) &&
    539 		    (strncmp(file->name, fname, BFS_FILENAME_MAXLEN) ==0))
    540 			break;
    541 
    542 	if (i == bfs->max_dirent)
    543 		return false;
    544 
    545 	*dirent = file;
    546 
    547 	return true;
    548 }
    549 
    550 bool
    551 bfs_inode_lookup(const struct bfs *bfs, ino_t n, struct bfs_inode **iinode)
    552 {
    553 	struct bfs_inode *inode;
    554 	int i;
    555 
    556 	for (inode = bfs->inode, i = 0; i < bfs->max_inode; i++, inode++)
    557 		if (inode->number == n)
    558 			break;
    559 
    560 	if (i == bfs->max_inode)
    561 		return false;
    562 
    563 	*iinode = inode;
    564 
    565 	return true;
    566 }
    567 
    568 size_t
    569 bfs_file_size(const struct bfs_inode *inode)
    570 {
    571 
    572 	return inode->eof_offset_byte - inode->start_sector * DEV_BSIZE + 1;
    573 }
    574 
    575 STATIC int
    576 bfs_inode_alloc(const struct bfs *bfs, struct bfs_inode **free_inode,
    577     int *free_inode_number, int *free_block)
    578 {
    579 	struct bfs_inode *jnode, *inode;
    580 	int i, j, start;
    581 
    582 	j = start = 0;
    583 	inode = bfs->inode;
    584 	jnode = 0;
    585 
    586 	for (i = BFS_ROOT_INODE; i < bfs->max_inode; i++, inode++) {
    587 		/* Steal i-node # */
    588 		if (j == 0)
    589 			j = i;
    590 
    591 		/* Get free i-node */
    592 		if (jnode == 0 && (inode->number == 0))
    593 			jnode = inode;
    594 
    595 		/* Get free i-node # and data block */
    596 		if (inode->number != 0) {
    597 			if (inode->end_sector > start)
    598 				start = inode->end_sector;
    599 			if (inode->number == j)
    600 				j = 0;	/* conflict */
    601 		}
    602 	}
    603 	start++;
    604 
    605 	if (jnode ==  0) {
    606 		DPRINTF(bfs->debug, "i-node full.\n");
    607 		return ENOSPC;
    608 	}
    609 
    610 	if (start * DEV_BSIZE >= bfs->data_end) {
    611 		DPRINTF(bfs->debug, "data block full.\n");
    612 		/* compaction here ? */
    613 		return ENOSPC;
    614 	}
    615 	if (free_inode)
    616 		*free_inode = jnode;
    617 	if (free_inode_number)
    618 		*free_inode_number = j;
    619 	if (free_block)
    620 		*free_block = start;
    621 
    622 	return 0;
    623 }
    624 
    625 void
    626 bfs_inode_set_attr(const struct bfs *bfs, struct bfs_inode *inode,
    627     const struct bfs_fileattr *from)
    628 {
    629 	struct bfs_fileattr *to = &inode->attr;
    630 
    631 	if (from != NULL) {
    632 		if (from->uid != (uid_t)-1)
    633 			to->uid = from->uid;
    634 		if (from->gid != (uid_t)-1)
    635 			to->gid = from->gid;
    636 		if (from->mode != (mode_t)-1)
    637 			to->mode = from->mode;
    638 		if (from->atime != -1)
    639 			to->atime = from->atime;
    640 		if (from->ctime != -1)
    641 			to->ctime = from->ctime;
    642 		if (from->mtime != -1)
    643 			to->mtime = from->mtime;
    644 	}
    645 	bfs_writeback_inode(bfs, inode);
    646 }
    647 
    648 STATIC bool
    649 bfs_superblock_valid(const struct bfs_super_block *super)
    650 {
    651 
    652 	return super->header.magic == BFS_MAGIC;
    653 }
    654 
    655 bool
    656 bfs_dump(const struct bfs *bfs)
    657 {
    658 	const struct bfs_super_block_header *h;
    659 	const struct bfs_compaction *compaction;
    660 	const struct bfs_inode *inode;
    661 	struct bfs_dirent *file;
    662 	int i, j, s, e;
    663 	size_t bytes;
    664 
    665 	if (!bfs_superblock_valid(bfs->super_block)) {
    666 		DPRINTF(bfs->debug, "invalid bfs super block.\n");
    667 		return false;
    668 	}
    669 	h = &bfs->super_block->header;
    670 	compaction = &bfs->super_block->compaction;
    671 
    672 	DPRINTF(bfs->debug, "super block %zdbyte, inode %zdbyte, dirent %zdbyte\n",
    673 	    sizeof *bfs->super_block, sizeof *inode, sizeof *file);
    674 
    675 	DPRINTF(bfs->debug, "magic=%x\n", h->magic);
    676 	DPRINTF(bfs->debug, "data_start_byte=0x%x\n", h->data_start_byte);
    677 	DPRINTF(bfs->debug, "data_end_byte=0x%x\n", h->data_end_byte);
    678 	DPRINTF(bfs->debug, "from=%#x\n", compaction->from);
    679 	DPRINTF(bfs->debug, "to=%#x\n", compaction->to);
    680 	DPRINTF(bfs->debug, "from_backup=%#x\n", compaction->from_backup);
    681 	DPRINTF(bfs->debug, "to_backup=%#x\n", compaction->to_backup);
    682 	DPRINTF(bfs->debug, "fsname=%s\n", bfs->super_block->fsname);
    683 	DPRINTF(bfs->debug, "volume=%s\n", bfs->super_block->volume);
    684 
    685 	/* inode list */
    686 	DPRINTF(bfs->debug, "[inode index list]\n");
    687 	for (inode = bfs->inode, i = j = 0; i < bfs->max_inode; inode++, i++) {
    688 		if (inode->number != 0) {
    689 			const struct bfs_fileattr *attr = &inode->attr;
    690 			DPRINTF(bfs->debug, "%3d  %8d %8d %8d (%d) ",
    691 			    inode->number,
    692 			    inode->eof_offset_byte -
    693 			    (inode->start_sector * DEV_BSIZE) + 1,/* file size*/
    694 			    inode->start_sector,
    695 			    inode->end_sector, i);
    696 
    697 			DPRINTF(bfs->debug, "%d %d %d %d %d %08x %08x %08x\n",
    698 			    attr->type, attr->mode, attr->uid, attr->gid,
    699 			    attr->nlink, attr->atime, attr->mtime, attr->ctime);
    700 			j++;
    701 		}
    702 	}
    703 	if (j != bfs->n_inode) {
    704 		DPRINTF(bfs->debug, "inconsistent cached data. (i-node)\n");
    705 		return false;
    706 	}
    707 	DPRINTF(bfs->debug, "total %d i-node.\n", j);
    708 
    709 	/* file list */
    710 	DPRINTF(bfs->debug, "[dirent index list]\n");
    711 	DPRINTF(bfs->debug, "%d file entries.\n", bfs->max_dirent);
    712 	file = bfs->dirent;
    713 	for (i = j = 0; i < bfs->max_dirent; i++, file++) {
    714 		if (file->inode != 0) {
    715 			if (bfs_file_lookup(bfs, file->name, &s, &e, &bytes))
    716 				DPRINTF(bfs->debug, "%3d %14s %8d %8d %8zd\n",
    717 				    file->inode, file->name, s, e, bytes);
    718 			j++;
    719 		}
    720 	}
    721 	if (j != bfs->n_dirent) {
    722 		DPRINTF(bfs->debug, "inconsistent cached data. (dirent)\n");
    723 		return false;
    724 	}
    725 	DPRINTF(bfs->debug, "%d files.\n", j);
    726 
    727 	return true;
    728 }
    729