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