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