Home | History | Annotate | Line # | Download | only in sysvbfs
      1  1.19      chs /*	$NetBSD: bfs.c,v 1.19 2019/11/10 21:16:38 chs 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.19      chs __KERNEL_RCSID(0, "$NetBSD: bfs.c,v 1.19 2019/11/10 21:16:38 chs 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.19      chs 	if ((bfs = (void *)__MALLOC(sizeof(struct bfs), M_BFS, M_WAITOK)) == 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.19      chs 	if ((p = (void *)__MALLOC(memsize, M_BFS, M_WAITOK)) == 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.19      chs 	if ((p = (void *)__MALLOC(memsize, M_BFS, M_WAITOK)) == 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.17  hannken 		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.17  hannken 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.17  hannken 	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.17  hannken 	bfs_writeback_dirent(bfs, dirent, false);
    325   1.1  tsutsui 
    326  1.17  hannken 	if (!keep_inode) {
    327  1.17  hannken 		memset(inode, 0, sizeof *inode);
    328  1.17  hannken 		bfs->n_inode--;
    329  1.17  hannken 		bfs_writeback_inode(bfs, inode);
    330  1.17  hannken 	}
    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.1  tsutsui 	strncpy(dirent->name, to_name, BFS_FILENAME_MAXLEN);
    348   1.8  thorpej 	bfs_writeback_dirent(bfs, dirent, false);
    349   1.1  tsutsui 
    350   1.1  tsutsui  out:
    351  1.10    perry 	DPRINTF(bfs->debug, "%s: \"%s\" -> \"%s\" error=%d.\n", __func__,
    352   1.1  tsutsui 	    from_name, to_name, err);
    353   1.1  tsutsui 
    354   1.1  tsutsui 	return err;
    355   1.1  tsutsui }
    356   1.1  tsutsui 
    357   1.1  tsutsui int
    358   1.1  tsutsui bfs_file_create(struct bfs *bfs, const char *fname, void *buf, size_t bufsz,
    359   1.1  tsutsui     const struct bfs_fileattr *attr)
    360   1.1  tsutsui {
    361   1.1  tsutsui 	struct bfs_inode *inode;
    362   1.1  tsutsui 	struct bfs_dirent *file;
    363   1.1  tsutsui 	int i, j, n, start;
    364   1.1  tsutsui 	uint8_t *p, tmpbuf[DEV_BSIZE];
    365   1.1  tsutsui 	int err;
    366   1.1  tsutsui 
    367   1.1  tsutsui 	/* Find free i-node and data block */
    368   1.1  tsutsui 	if ((err = bfs_inode_alloc(bfs, &inode, &j, &start)) != 0)
    369   1.1  tsutsui 		return err;
    370   1.1  tsutsui 
    371   1.1  tsutsui 	/* File size (unit block) */
    372   1.1  tsutsui 	n = (ROUND_SECTOR(bufsz) >> DEV_BSHIFT) - 1;
    373   1.1  tsutsui 	if (n < 0)	/* bufsz == 0 */
    374   1.1  tsutsui 		n = 0;
    375   1.1  tsutsui 
    376   1.1  tsutsui 	if ((start + n) * DEV_BSIZE >= bfs->data_end) {
    377   1.1  tsutsui 		DPRINTF(bfs->debug, "disk full.\n");
    378   1.1  tsutsui 		return ENOSPC;
    379   1.1  tsutsui 	}
    380   1.1  tsutsui 
    381   1.1  tsutsui 	/* Find free dirent */
    382   1.1  tsutsui 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
    383   1.1  tsutsui 		if (file->inode == 0)
    384   1.1  tsutsui 			break;
    385   1.1  tsutsui 	if (i == bfs->max_dirent) {
    386   1.1  tsutsui 		DPRINTF(bfs->debug, "dirent full.\n");
    387   1.1  tsutsui 		return ENOSPC;
    388   1.1  tsutsui 	}
    389   1.1  tsutsui 
    390   1.1  tsutsui 	/* i-node */
    391   1.1  tsutsui 	memset(inode, 0, sizeof *inode);
    392   1.1  tsutsui 	inode->number = j;
    393   1.1  tsutsui 	inode->start_sector = start;
    394   1.1  tsutsui 	inode->end_sector = start + n;
    395   1.1  tsutsui 	inode->eof_offset_byte = start * DEV_BSIZE + bufsz - 1;
    396   1.1  tsutsui 	/* i-node attribute */
    397   1.1  tsutsui 	inode->attr.type = 1;
    398   1.1  tsutsui 	inode->attr.mode = 0;
    399   1.1  tsutsui 	inode->attr.nlink = 1;
    400   1.1  tsutsui 	bfs_inode_set_attr(bfs, inode, attr);
    401   1.1  tsutsui 
    402   1.1  tsutsui 	/* Dirent */
    403   1.1  tsutsui 	memset(file, 0, sizeof *file);
    404   1.1  tsutsui 	file->inode = inode->number;
    405   1.1  tsutsui 	strncpy(file->name, fname, BFS_FILENAME_MAXLEN);
    406   1.1  tsutsui 
    407  1.10    perry 	DPRINTF(bfs->debug, "%s: start %d end %d\n", __func__,
    408   1.1  tsutsui 	    inode->start_sector, inode->end_sector);
    409   1.1  tsutsui 
    410   1.1  tsutsui 	if (buf != 0) {
    411   1.1  tsutsui 		p = (uint8_t *)buf;
    412   1.1  tsutsui 		/* Data block */
    413   1.1  tsutsui 		n = 0;
    414   1.1  tsutsui 		for (i = inode->start_sector; i < inode->end_sector; i++) {
    415   1.1  tsutsui 			if (!bfs->io->write(bfs->io, p, bfs->start_sector + i))
    416   1.1  tsutsui 				return EIO;
    417   1.1  tsutsui 			p += DEV_BSIZE;
    418   1.1  tsutsui 			n += DEV_BSIZE;
    419   1.1  tsutsui 		}
    420   1.1  tsutsui 		/* last sector */
    421   1.1  tsutsui 		memset(tmpbuf, 0, DEV_BSIZE);
    422   1.1  tsutsui 		memcpy(tmpbuf, p, bufsz - n);
    423   1.1  tsutsui 		if (!bfs->io->write(bfs->io, tmpbuf, bfs->start_sector + i))
    424   1.1  tsutsui 			return EIO;
    425   1.1  tsutsui 	}
    426   1.1  tsutsui 	/* Update */
    427   1.1  tsutsui 	bfs->n_inode++;
    428   1.1  tsutsui 	bfs->n_dirent++;
    429   1.8  thorpej 	bfs_writeback_dirent(bfs, file, true);
    430   1.1  tsutsui 	bfs_writeback_inode(bfs, inode);
    431   1.1  tsutsui 
    432   1.1  tsutsui 	return 0;
    433   1.1  tsutsui }
    434   1.1  tsutsui 
    435   1.7  thorpej STATIC bool
    436   1.1  tsutsui bfs_writeback_dirent(const struct bfs *bfs, struct bfs_dirent *dir,
    437   1.7  thorpej     bool create)
    438   1.1  tsutsui {
    439   1.1  tsutsui 	struct bfs_dirent *dir_base = bfs->dirent;
    440   1.1  tsutsui 	struct bfs_inode *root_inode = bfs->root_inode;
    441   1.5   martin 	uintptr_t eof;
    442   1.1  tsutsui 	int i;
    443   1.1  tsutsui 
    444   1.1  tsutsui 	i = ((dir - dir_base) * sizeof *dir) >> DEV_BSHIFT;
    445   1.1  tsutsui 
    446   1.5   martin 	eof = (uintptr_t)(dir + 1) - 1;
    447   1.5   martin 	eof = eof - (uintptr_t)dir_base +
    448   1.1  tsutsui 	    (root_inode->start_sector << DEV_BSHIFT);
    449   1.1  tsutsui 
    450   1.1  tsutsui 	/* update root directory inode */
    451   1.1  tsutsui #if 0
    452   1.1  tsutsui 	printf("eof new=%d old=%d\n", eof, root_inode->eof_offset_byte);
    453   1.1  tsutsui #endif
    454   1.1  tsutsui 	if (create) {
    455   1.1  tsutsui 		if (eof > root_inode->eof_offset_byte) {
    456   1.1  tsutsui 			root_inode->eof_offset_byte = eof;
    457   1.1  tsutsui 		}
    458   1.1  tsutsui 	} else {
    459   1.1  tsutsui 		/* delete the last entry */
    460   1.1  tsutsui 		if (eof == root_inode->eof_offset_byte) {
    461   1.1  tsutsui 			root_inode->eof_offset_byte = eof - sizeof *dir;
    462   1.1  tsutsui 		}
    463   1.1  tsutsui 	}
    464   1.1  tsutsui 	bfs_writeback_inode(bfs, root_inode);
    465   1.1  tsutsui 
    466   1.1  tsutsui 	/* update dirent */
    467   1.1  tsutsui 	return bfs->io->write(bfs->io, (uint8_t *)dir_base + (i << DEV_BSHIFT),
    468   1.1  tsutsui 	    bfs->start_sector + bfs->root_inode->start_sector + i);
    469   1.1  tsutsui }
    470   1.1  tsutsui 
    471   1.7  thorpej STATIC bool
    472   1.1  tsutsui bfs_writeback_inode(const struct bfs *bfs, struct bfs_inode *inode)
    473   1.1  tsutsui {
    474   1.1  tsutsui 	struct bfs_inode *inode_base = bfs->inode;
    475   1.1  tsutsui 	int i;
    476   1.1  tsutsui 
    477   1.1  tsutsui 	i = ((inode - inode_base) * sizeof *inode) >> DEV_BSHIFT;
    478   1.1  tsutsui 
    479   1.1  tsutsui 	return bfs->io->write(bfs->io,
    480   1.1  tsutsui 	    (uint8_t *)inode_base + (i << DEV_BSHIFT),
    481   1.1  tsutsui 	    bfs->start_sector + 1/*super block*/ + i);
    482   1.1  tsutsui }
    483   1.1  tsutsui 
    484   1.7  thorpej bool
    485   1.1  tsutsui bfs_file_lookup(const struct bfs *bfs, const char *fname, int *start, int *end,
    486   1.1  tsutsui     size_t *size)
    487   1.1  tsutsui {
    488   1.1  tsutsui 	struct bfs_inode *inode;
    489   1.1  tsutsui 	struct bfs_dirent *dirent;
    490   1.1  tsutsui 
    491   1.1  tsutsui 	if (!bfs_dirent_lookup_by_name(bfs, fname, &dirent))
    492   1.8  thorpej 		return false;
    493   1.1  tsutsui 	if (!bfs_inode_lookup(bfs, dirent->inode, &inode))
    494   1.8  thorpej 		return false;
    495   1.1  tsutsui 
    496   1.1  tsutsui 	if (start)
    497   1.1  tsutsui 		*start = inode->start_sector + bfs->start_sector;
    498   1.1  tsutsui 	if (end)
    499   1.1  tsutsui 		*end = inode->end_sector + bfs->start_sector;
    500   1.1  tsutsui 	if (size)
    501   1.1  tsutsui 		*size = bfs_file_size(inode);
    502   1.1  tsutsui 
    503   1.5   martin 	DPRINTF(bfs->debug, "%s: %d + %d -> %d (%zd)\n",
    504   1.1  tsutsui 	    fname, bfs->start_sector, inode->start_sector,
    505   1.1  tsutsui 	    inode->end_sector, *size);
    506   1.1  tsutsui 
    507   1.8  thorpej 	return true;
    508   1.1  tsutsui }
    509   1.1  tsutsui 
    510   1.7  thorpej bool
    511   1.1  tsutsui bfs_dirent_lookup_by_inode(const struct bfs *bfs, int inode,
    512   1.1  tsutsui     struct bfs_dirent **dirent)
    513   1.1  tsutsui {
    514   1.1  tsutsui 	struct bfs_dirent *file;
    515   1.1  tsutsui 	int i;
    516   1.1  tsutsui 
    517   1.1  tsutsui 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
    518   1.1  tsutsui 		if (file->inode == inode)
    519   1.1  tsutsui 			break;
    520   1.1  tsutsui 
    521   1.1  tsutsui 	if (i == bfs->max_dirent)
    522   1.8  thorpej 		return false;
    523   1.1  tsutsui 
    524   1.1  tsutsui 	*dirent = file;
    525   1.1  tsutsui 
    526   1.8  thorpej 	return true;
    527   1.1  tsutsui }
    528   1.1  tsutsui 
    529   1.7  thorpej bool
    530   1.1  tsutsui bfs_dirent_lookup_by_name(const struct bfs *bfs, const char *fname,
    531   1.1  tsutsui     struct bfs_dirent **dirent)
    532   1.1  tsutsui {
    533   1.1  tsutsui 	struct bfs_dirent *file;
    534   1.1  tsutsui 	int i;
    535   1.1  tsutsui 
    536   1.1  tsutsui 	for (file = bfs->dirent, i = 0; i < bfs->max_dirent; i++, file++)
    537   1.1  tsutsui 		if ((file->inode != 0) &&
    538   1.1  tsutsui 		    (strncmp(file->name, fname, BFS_FILENAME_MAXLEN) ==0))
    539   1.1  tsutsui 			break;
    540   1.1  tsutsui 
    541   1.1  tsutsui 	if (i == bfs->max_dirent)
    542   1.8  thorpej 		return false;
    543   1.1  tsutsui 
    544   1.1  tsutsui 	*dirent = file;
    545   1.1  tsutsui 
    546   1.8  thorpej 	return true;
    547   1.1  tsutsui }
    548   1.1  tsutsui 
    549   1.7  thorpej bool
    550   1.1  tsutsui bfs_inode_lookup(const struct bfs *bfs, ino_t n, struct bfs_inode **iinode)
    551   1.1  tsutsui {
    552   1.1  tsutsui 	struct bfs_inode *inode;
    553   1.1  tsutsui 	int i;
    554   1.1  tsutsui 
    555   1.1  tsutsui 	for (inode = bfs->inode, i = 0; i < bfs->max_inode; i++, inode++)
    556   1.1  tsutsui 		if (inode->number == n)
    557   1.1  tsutsui 			break;
    558   1.1  tsutsui 
    559   1.1  tsutsui 	if (i == bfs->max_inode)
    560   1.8  thorpej 		return false;
    561   1.1  tsutsui 
    562   1.1  tsutsui 	*iinode = inode;
    563   1.1  tsutsui 
    564   1.8  thorpej 	return true;
    565   1.1  tsutsui }
    566   1.1  tsutsui 
    567  1.17  hannken int
    568  1.17  hannken bfs_inode_delete(struct bfs *bfs, ino_t ino)
    569  1.17  hannken {
    570  1.17  hannken 	struct bfs_inode *inode;
    571  1.17  hannken 
    572  1.17  hannken 	if (!bfs_inode_lookup(bfs, ino, &inode))
    573  1.17  hannken 		return ENOENT;
    574  1.17  hannken 
    575  1.17  hannken 	memset(inode, 0, sizeof *inode);
    576  1.17  hannken 	bfs->n_inode--;
    577  1.17  hannken 
    578  1.17  hannken 	bfs_writeback_inode(bfs, inode);
    579  1.17  hannken 	DPRINTF(bfs->debug, "%s: %lld deleted.\n", __func__, (long long)ino);
    580  1.17  hannken 
    581  1.17  hannken 	return 0;
    582  1.17  hannken }
    583  1.17  hannken 
    584  1.17  hannken 
    585   1.1  tsutsui size_t
    586   1.1  tsutsui bfs_file_size(const struct bfs_inode *inode)
    587   1.1  tsutsui {
    588   1.1  tsutsui 
    589   1.1  tsutsui 	return inode->eof_offset_byte - inode->start_sector * DEV_BSIZE + 1;
    590   1.1  tsutsui }
    591   1.1  tsutsui 
    592   1.1  tsutsui STATIC int
    593   1.1  tsutsui bfs_inode_alloc(const struct bfs *bfs, struct bfs_inode **free_inode,
    594   1.1  tsutsui     int *free_inode_number, int *free_block)
    595   1.1  tsutsui {
    596   1.1  tsutsui 	struct bfs_inode *jnode, *inode;
    597   1.1  tsutsui 	int i, j, start;
    598   1.1  tsutsui 
    599   1.1  tsutsui 	j = start = 0;
    600   1.1  tsutsui 	inode = bfs->inode;
    601   1.1  tsutsui 	jnode = 0;
    602   1.1  tsutsui 
    603   1.1  tsutsui 	for (i = BFS_ROOT_INODE; i < bfs->max_inode; i++, inode++) {
    604   1.1  tsutsui 		/* Steal i-node # */
    605   1.1  tsutsui 		if (j == 0)
    606   1.1  tsutsui 			j = i;
    607   1.1  tsutsui 
    608   1.1  tsutsui 		/* Get free i-node */
    609   1.1  tsutsui 		if (jnode == 0 && (inode->number == 0))
    610   1.1  tsutsui 			jnode = inode;
    611   1.1  tsutsui 
    612   1.1  tsutsui 		/* Get free i-node # and data block */
    613   1.1  tsutsui 		if (inode->number != 0) {
    614   1.1  tsutsui 			if (inode->end_sector > start)
    615   1.1  tsutsui 				start = inode->end_sector;
    616   1.1  tsutsui 			if (inode->number == j)
    617   1.1  tsutsui 				j = 0;	/* conflict */
    618   1.1  tsutsui 		}
    619   1.1  tsutsui 	}
    620   1.1  tsutsui 	start++;
    621   1.1  tsutsui 
    622   1.1  tsutsui 	if (jnode ==  0) {
    623   1.1  tsutsui 		DPRINTF(bfs->debug, "i-node full.\n");
    624   1.1  tsutsui 		return ENOSPC;
    625   1.1  tsutsui 	}
    626   1.1  tsutsui 
    627   1.1  tsutsui 	if (start * DEV_BSIZE >= bfs->data_end) {
    628   1.1  tsutsui 		DPRINTF(bfs->debug, "data block full.\n");
    629   1.1  tsutsui 		/* compaction here ? */
    630   1.1  tsutsui 		return ENOSPC;
    631   1.1  tsutsui 	}
    632   1.1  tsutsui 	if (free_inode)
    633   1.1  tsutsui 		*free_inode = jnode;
    634   1.1  tsutsui 	if (free_inode_number)
    635   1.1  tsutsui 		*free_inode_number = j;
    636   1.1  tsutsui 	if (free_block)
    637   1.1  tsutsui 		*free_block = start;
    638   1.1  tsutsui 
    639   1.1  tsutsui 	return 0;
    640   1.1  tsutsui }
    641   1.1  tsutsui 
    642   1.1  tsutsui void
    643   1.1  tsutsui bfs_inode_set_attr(const struct bfs *bfs, struct bfs_inode *inode,
    644   1.1  tsutsui     const struct bfs_fileattr *from)
    645   1.1  tsutsui {
    646   1.1  tsutsui 	struct bfs_fileattr *to = &inode->attr;
    647   1.1  tsutsui 
    648   1.1  tsutsui 	if (from != NULL) {
    649   1.1  tsutsui 		if (from->uid != (uid_t)-1)
    650   1.1  tsutsui 			to->uid = from->uid;
    651  1.16      agc 		if (from->gid != (gid_t)-1)
    652   1.1  tsutsui 			to->gid = from->gid;
    653   1.1  tsutsui 		if (from->mode != (mode_t)-1)
    654   1.1  tsutsui 			to->mode = from->mode;
    655   1.1  tsutsui 		if (from->atime != -1)
    656   1.1  tsutsui 			to->atime = from->atime;
    657   1.1  tsutsui 		if (from->ctime != -1)
    658   1.1  tsutsui 			to->ctime = from->ctime;
    659   1.1  tsutsui 		if (from->mtime != -1)
    660   1.1  tsutsui 			to->mtime = from->mtime;
    661   1.1  tsutsui 	}
    662   1.1  tsutsui 	bfs_writeback_inode(bfs, inode);
    663   1.1  tsutsui }
    664   1.1  tsutsui 
    665   1.7  thorpej STATIC bool
    666   1.1  tsutsui bfs_superblock_valid(const struct bfs_super_block *super)
    667   1.1  tsutsui {
    668   1.1  tsutsui 
    669   1.1  tsutsui 	return super->header.magic == BFS_MAGIC;
    670   1.1  tsutsui }
    671   1.1  tsutsui 
    672   1.7  thorpej bool
    673   1.1  tsutsui bfs_dump(const struct bfs *bfs)
    674   1.1  tsutsui {
    675   1.1  tsutsui 	const struct bfs_super_block_header *h;
    676   1.1  tsutsui 	const struct bfs_compaction *compaction;
    677   1.1  tsutsui 	const struct bfs_inode *inode;
    678   1.1  tsutsui 	struct bfs_dirent *file;
    679   1.5   martin 	int i, j, s, e;
    680   1.5   martin 	size_t bytes;
    681   1.1  tsutsui 
    682   1.1  tsutsui 	if (!bfs_superblock_valid(bfs->super_block)) {
    683   1.1  tsutsui 		DPRINTF(bfs->debug, "invalid bfs super block.\n");
    684   1.8  thorpej 		return false;
    685   1.1  tsutsui 	}
    686   1.1  tsutsui 	h = &bfs->super_block->header;
    687   1.1  tsutsui 	compaction = &bfs->super_block->compaction;
    688   1.1  tsutsui 
    689   1.5   martin 	DPRINTF(bfs->debug, "super block %zdbyte, inode %zdbyte, dirent %zdbyte\n",
    690   1.1  tsutsui 	    sizeof *bfs->super_block, sizeof *inode, sizeof *file);
    691   1.1  tsutsui 
    692   1.1  tsutsui 	DPRINTF(bfs->debug, "magic=%x\n", h->magic);
    693   1.1  tsutsui 	DPRINTF(bfs->debug, "data_start_byte=0x%x\n", h->data_start_byte);
    694   1.1  tsutsui 	DPRINTF(bfs->debug, "data_end_byte=0x%x\n", h->data_end_byte);
    695   1.1  tsutsui 	DPRINTF(bfs->debug, "from=%#x\n", compaction->from);
    696   1.1  tsutsui 	DPRINTF(bfs->debug, "to=%#x\n", compaction->to);
    697   1.1  tsutsui 	DPRINTF(bfs->debug, "from_backup=%#x\n", compaction->from_backup);
    698   1.1  tsutsui 	DPRINTF(bfs->debug, "to_backup=%#x\n", compaction->to_backup);
    699   1.1  tsutsui 	DPRINTF(bfs->debug, "fsname=%s\n", bfs->super_block->fsname);
    700   1.1  tsutsui 	DPRINTF(bfs->debug, "volume=%s\n", bfs->super_block->volume);
    701   1.1  tsutsui 
    702   1.1  tsutsui 	/* inode list */
    703   1.1  tsutsui 	DPRINTF(bfs->debug, "[inode index list]\n");
    704   1.1  tsutsui 	for (inode = bfs->inode, i = j = 0; i < bfs->max_inode; inode++, i++) {
    705   1.1  tsutsui 		if (inode->number != 0) {
    706   1.1  tsutsui 			const struct bfs_fileattr *attr = &inode->attr;
    707   1.1  tsutsui 			DPRINTF(bfs->debug, "%3d  %8d %8d %8d (%d) ",
    708   1.1  tsutsui 			    inode->number,
    709   1.1  tsutsui 			    inode->eof_offset_byte -
    710   1.1  tsutsui 			    (inode->start_sector * DEV_BSIZE) + 1,/* file size*/
    711   1.1  tsutsui 			    inode->start_sector,
    712   1.1  tsutsui 			    inode->end_sector, i);
    713   1.1  tsutsui 
    714   1.1  tsutsui 			DPRINTF(bfs->debug, "%d %d %d %d %d %08x %08x %08x\n",
    715   1.1  tsutsui 			    attr->type, attr->mode, attr->uid, attr->gid,
    716   1.1  tsutsui 			    attr->nlink, attr->atime, attr->mtime, attr->ctime);
    717   1.1  tsutsui 			j++;
    718   1.1  tsutsui 		}
    719   1.1  tsutsui 	}
    720   1.1  tsutsui 	if (j != bfs->n_inode) {
    721   1.1  tsutsui 		DPRINTF(bfs->debug, "inconsistent cached data. (i-node)\n");
    722   1.8  thorpej 		return false;
    723   1.1  tsutsui 	}
    724   1.1  tsutsui 	DPRINTF(bfs->debug, "total %d i-node.\n", j);
    725   1.1  tsutsui 
    726   1.1  tsutsui 	/* file list */
    727   1.1  tsutsui 	DPRINTF(bfs->debug, "[dirent index list]\n");
    728   1.1  tsutsui 	DPRINTF(bfs->debug, "%d file entries.\n", bfs->max_dirent);
    729   1.1  tsutsui 	file = bfs->dirent;
    730   1.1  tsutsui 	for (i = j = 0; i < bfs->max_dirent; i++, file++) {
    731   1.1  tsutsui 		if (file->inode != 0) {
    732   1.1  tsutsui 			if (bfs_file_lookup(bfs, file->name, &s, &e, &bytes))
    733   1.5   martin 				DPRINTF(bfs->debug, "%3d %14s %8d %8d %8zd\n",
    734   1.1  tsutsui 				    file->inode, file->name, s, e, bytes);
    735   1.1  tsutsui 			j++;
    736   1.1  tsutsui 		}
    737   1.1  tsutsui 	}
    738   1.1  tsutsui 	if (j != bfs->n_dirent) {
    739   1.1  tsutsui 		DPRINTF(bfs->debug, "inconsistent cached data. (dirent)\n");
    740   1.8  thorpej 		return false;
    741   1.1  tsutsui 	}
    742   1.1  tsutsui 	DPRINTF(bfs->debug, "%d files.\n", j);
    743   1.1  tsutsui 
    744   1.8  thorpej 	return true;
    745   1.1  tsutsui }
    746