Home | History | Annotate | Line # | Download | only in v7fs
v7fs_inode.c revision 1.1
      1  1.1  uch /*	$NetBSD: v7fs_inode.c,v 1.1 2011/06/27 11:52:25 uch Exp $	*/
      2  1.1  uch 
      3  1.1  uch /*-
      4  1.1  uch  * Copyright (c) 2011 The NetBSD Foundation, Inc.
      5  1.1  uch  * All rights reserved.
      6  1.1  uch  *
      7  1.1  uch  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  uch  * by UCHIYAMA Yasushi.
      9  1.1  uch  *
     10  1.1  uch  * Redistribution and use in source and binary forms, with or without
     11  1.1  uch  * modification, are permitted provided that the following conditions
     12  1.1  uch  * are met:
     13  1.1  uch  * 1. Redistributions of source code must retain the above copyright
     14  1.1  uch  *    notice, this list of conditions and the following disclaimer.
     15  1.1  uch  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  uch  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  uch  *    documentation and/or other materials provided with the distribution.
     18  1.1  uch  *
     19  1.1  uch  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  uch  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  uch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  uch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  uch  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  uch  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  uch  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  uch  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  uch  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  uch  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  uch  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  uch  */
     31  1.1  uch 
     32  1.1  uch #include <sys/cdefs.h>
     33  1.1  uch __KERNEL_RCSID(0, "$NetBSD: v7fs_inode.c,v 1.1 2011/06/27 11:52:25 uch Exp $");
     34  1.1  uch #if defined _KERNEL_OPT
     35  1.1  uch #include "opt_v7fs.h"
     36  1.1  uch #endif
     37  1.1  uch 
     38  1.1  uch #ifdef _KERNEL
     39  1.1  uch #include <sys/systm.h>
     40  1.1  uch #include <sys/param.h>
     41  1.1  uch #else
     42  1.1  uch #include <stdio.h>
     43  1.1  uch #include <string.h>
     44  1.1  uch #include <errno.h>
     45  1.1  uch #include <time.h>
     46  1.1  uch #endif
     47  1.1  uch 
     48  1.1  uch #include "v7fs.h"
     49  1.1  uch #include "v7fs_impl.h"
     50  1.1  uch #include "v7fs_endian.h"
     51  1.1  uch #include "v7fs_inode.h"
     52  1.1  uch #include "v7fs_superblock.h"
     53  1.1  uch 
     54  1.1  uch #ifdef V7FS_INODE_DEBUG
     55  1.1  uch #define	DPRINTF(fmt, args...)	printf("%s: " fmt, __func__, ##args)
     56  1.1  uch #else
     57  1.1  uch #define	DPRINTF(fmt, args...)	((void)0)
     58  1.1  uch #endif
     59  1.1  uch 
     60  1.1  uch static void v7fs_inode_setup_disk_image(const struct v7fs_self *,
     61  1.1  uch     struct v7fs_inode *, struct v7fs_inode_diskimage *);
     62  1.1  uch static int v7fs_inode_inquire_disk_location(const struct v7fs_self *,
     63  1.1  uch     v7fs_ino_t, v7fs_daddr_t *, v7fs_daddr_t *);
     64  1.1  uch #ifdef V7FS_INODE_DEBUG
     65  1.1  uch static int v7fs_inode_block_sanity(const struct v7fs_superblock *,
     66  1.1  uch     v7fs_daddr_t);
     67  1.1  uch 
     68  1.1  uch static int
     69  1.1  uch v7fs_inode_block_sanity(const struct v7fs_superblock *sb, v7fs_daddr_t blk)
     70  1.1  uch {
     71  1.1  uch 
     72  1.1  uch 	if ((blk < V7FS_ILIST_SECTOR) || (blk >= sb->datablock_start_sector)) {
     73  1.1  uch 		DPRINTF("invalid inode block#%d (%d-%d)\n", blk,
     74  1.1  uch 		    V7FS_ILIST_SECTOR, sb->datablock_start_sector);
     75  1.1  uch 		return ENOSPC;
     76  1.1  uch 	}
     77  1.1  uch 
     78  1.1  uch 	return 0;
     79  1.1  uch }
     80  1.1  uch #endif /* V7FS_INODE_DEBUG */
     81  1.1  uch 
     82  1.1  uch int
     83  1.1  uch v7fs_inode_number_sanity(const struct v7fs_superblock *sb, v7fs_ino_t ino)
     84  1.1  uch {
     85  1.1  uch 
     86  1.1  uch 	if (ino < V7FS_ROOT_INODE || ((size_t)ino >= V7FS_MAX_INODE(sb))) {
     87  1.1  uch 		DPRINTF("invalid inode#%d (%d-%zu)\n", ino,
     88  1.1  uch 		    V7FS_ROOT_INODE, V7FS_MAX_INODE(sb));
     89  1.1  uch 		return ENOSPC;
     90  1.1  uch 	}
     91  1.1  uch 
     92  1.1  uch 	return 0;
     93  1.1  uch }
     94  1.1  uch 
     95  1.1  uch int
     96  1.1  uch v7fs_inode_allocate(struct v7fs_self *fs, v7fs_ino_t *ino)
     97  1.1  uch {
     98  1.1  uch 	struct v7fs_superblock *sb = &fs->superblock;
     99  1.1  uch 	v7fs_ino_t inode_number;
    100  1.1  uch 	int error = ENOSPC;
    101  1.1  uch 	*ino = 0;
    102  1.1  uch 
    103  1.1  uch 	SUPERB_LOCK(fs);
    104  1.1  uch 	if (sb->total_freeinode == 0) {
    105  1.1  uch 		DPRINTF("inode exhausted!(1)\n");
    106  1.1  uch 		goto errexit;
    107  1.1  uch 	}
    108  1.1  uch 
    109  1.1  uch 	/* If there is no free inode cache, update it. */
    110  1.1  uch 	if (sb->nfreeinode <= 0 && (error = v7fs_freeinode_update(fs))) {
    111  1.1  uch 		DPRINTF("inode exhausted!(2)\n");
    112  1.1  uch 		goto errexit;
    113  1.1  uch 	}
    114  1.1  uch 	/* Get inode from superblock cache. */
    115  1.1  uch 	KDASSERT(sb->nfreeinode <= V7FS_MAX_FREEINODE);
    116  1.1  uch 	inode_number = sb->freeinode[--sb->nfreeinode];
    117  1.1  uch 	sb->total_freeinode--;
    118  1.1  uch 	sb->modified = 1;
    119  1.1  uch 
    120  1.1  uch 	if ((error = v7fs_inode_number_sanity(sb, inode_number))) {
    121  1.1  uch 		DPRINTF("new inode#%d %d %d\n", inode_number, sb->nfreeinode,
    122  1.1  uch 		    sb->total_freeinode);
    123  1.1  uch 		DPRINTF("free inode list corupt\n");
    124  1.1  uch 		goto errexit;
    125  1.1  uch 	}
    126  1.1  uch 	*ino = inode_number;
    127  1.1  uch 
    128  1.1  uch errexit:
    129  1.1  uch 	SUPERB_UNLOCK(fs);
    130  1.1  uch 
    131  1.1  uch 	return error;
    132  1.1  uch }
    133  1.1  uch 
    134  1.1  uch void
    135  1.1  uch v7fs_inode_deallocate(struct v7fs_self *fs, v7fs_ino_t ino)
    136  1.1  uch {
    137  1.1  uch 	struct v7fs_superblock *sb = &fs->superblock;
    138  1.1  uch 	struct v7fs_inode inode;
    139  1.1  uch 
    140  1.1  uch 	memset(&inode, 0, sizeof(inode));
    141  1.1  uch 	inode.inode_number = ino;
    142  1.1  uch 	v7fs_inode_writeback(fs, &inode);
    143  1.1  uch 
    144  1.1  uch 	SUPERB_LOCK(fs);
    145  1.1  uch 	if (sb->nfreeinode < V7FS_MAX_FREEINODE) {
    146  1.1  uch 		/* link to freeinode list. */
    147  1.1  uch 		sb->freeinode[sb->nfreeinode++] = ino;
    148  1.1  uch 	}
    149  1.1  uch 	/* If superblock inode cache is full, this inode charged by
    150  1.1  uch 	   v7fs_freeinode_update() later. */
    151  1.1  uch 	sb->total_freeinode++;
    152  1.1  uch 	sb->modified = true;
    153  1.1  uch 	SUPERB_UNLOCK(fs);
    154  1.1  uch }
    155  1.1  uch 
    156  1.1  uch void
    157  1.1  uch v7fs_inode_setup_memory_image(const struct v7fs_self *fs __unused,
    158  1.1  uch     struct v7fs_inode *mem, struct v7fs_inode_diskimage *disk)
    159  1.1  uch {
    160  1.1  uch #define	conv16(m)	(mem->m = V7FS_VAL16(fs, (disk->m)))
    161  1.1  uch #define	conv32(m)	(mem->m = V7FS_VAL32(fs, (disk->m)))
    162  1.1  uch 	uint32_t addr;
    163  1.1  uch 	int i;
    164  1.1  uch 
    165  1.1  uch 	memset(mem, 0, sizeof(*mem));
    166  1.1  uch 	conv16(mode);
    167  1.1  uch 	conv16(nlink);
    168  1.1  uch 	conv16(uid);
    169  1.1  uch 	conv16(gid);
    170  1.1  uch 	conv32(filesize);
    171  1.1  uch 	conv32(atime);
    172  1.1  uch 	conv32(mtime);
    173  1.1  uch 	conv32(ctime);
    174  1.1  uch 
    175  1.1  uch 	for (i = 0; i < V7FS_NADDR; i++) {
    176  1.1  uch 		int j = i * 3; /* 3 byte each. (v7fs_daddr is 24bit) */
    177  1.1  uch 		/* expand to 4byte with endian conversion. */
    178  1.1  uch 		addr = V7FS_VAL24_READ(fs, &disk->addr[j]);
    179  1.1  uch 		mem->addr[i] = addr;
    180  1.1  uch 	}
    181  1.1  uch 	mem->device = 0;
    182  1.1  uch 	if (v7fs_inode_iscdev(mem) || v7fs_inode_isbdev(mem)) {
    183  1.1  uch 		mem->device = mem->addr[0];
    184  1.1  uch 	}
    185  1.1  uch 
    186  1.1  uch #undef conv16
    187  1.1  uch #undef conv32
    188  1.1  uch }
    189  1.1  uch 
    190  1.1  uch static void
    191  1.1  uch v7fs_inode_setup_disk_image(const struct v7fs_self *fs __unused,
    192  1.1  uch     struct v7fs_inode *mem, struct v7fs_inode_diskimage *disk)
    193  1.1  uch {
    194  1.1  uch #define	conv16(m)	(disk->m = V7FS_VAL16(fs, (mem->m)))
    195  1.1  uch #define	conv32(m)	(disk->m = V7FS_VAL32(fs, (mem->m)))
    196  1.1  uch 
    197  1.1  uch 	conv16(mode);
    198  1.1  uch 	conv16(nlink);
    199  1.1  uch 	conv16(uid);
    200  1.1  uch 	conv16(gid);
    201  1.1  uch 	conv32(filesize);
    202  1.1  uch 	conv32(atime);
    203  1.1  uch 	conv32(mtime);
    204  1.1  uch 	conv32(ctime);
    205  1.1  uch 
    206  1.1  uch 	int i;
    207  1.1  uch 	for (i = 0; i < V7FS_NADDR; i++) {
    208  1.1  uch 		int j = i * 3; /* 3 byte each. */
    209  1.1  uch 		V7FS_VAL24_WRITE(fs, mem->addr[i], disk->addr + j);
    210  1.1  uch 	}
    211  1.1  uch #undef conv16
    212  1.1  uch #undef conv32
    213  1.1  uch }
    214  1.1  uch 
    215  1.1  uch /* Load inode from disk. */
    216  1.1  uch int
    217  1.1  uch v7fs_inode_load(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t n)
    218  1.1  uch {
    219  1.1  uch 	v7fs_daddr_t blk, ofs;
    220  1.1  uch 	struct v7fs_inode_diskimage *di;
    221  1.1  uch 	void *buf;
    222  1.1  uch 
    223  1.1  uch 	if (v7fs_inode_inquire_disk_location(fs, n, &blk, &ofs) != 0)
    224  1.1  uch 		return ENOENT;
    225  1.1  uch 
    226  1.1  uch 	ILIST_LOCK(fs);
    227  1.1  uch 	if (!(buf = scratch_read(fs, blk))) {
    228  1.1  uch 		ILIST_UNLOCK(fs);
    229  1.1  uch 		return EIO;
    230  1.1  uch 	}
    231  1.1  uch 	ILIST_UNLOCK(fs);
    232  1.1  uch 	di = (struct v7fs_inode_diskimage *)buf;
    233  1.1  uch 
    234  1.1  uch 	/* Decode disk address, convert endian. */
    235  1.1  uch 	v7fs_inode_setup_memory_image(fs, p, di + ofs);
    236  1.1  uch 	p->inode_number = n;
    237  1.1  uch 
    238  1.1  uch 	scratch_free(fs, buf);
    239  1.1  uch 
    240  1.1  uch 	return 0;
    241  1.1  uch }
    242  1.1  uch 
    243  1.1  uch /* Write back inode to disk. */
    244  1.1  uch int
    245  1.1  uch v7fs_inode_writeback(struct v7fs_self *fs, struct v7fs_inode *mem)
    246  1.1  uch {
    247  1.1  uch 	struct v7fs_inode_diskimage disk;
    248  1.1  uch 	v7fs_ino_t ino = mem->inode_number;
    249  1.1  uch 	v7fs_daddr_t blk;
    250  1.1  uch 	v7fs_daddr_t ofs;
    251  1.1  uch 	void *buf;
    252  1.1  uch 	int error = 0;
    253  1.1  uch 
    254  1.1  uch 	if (v7fs_inode_inquire_disk_location(fs, ino, &blk, &ofs) != 0)
    255  1.1  uch 		return ENOENT;
    256  1.1  uch 
    257  1.1  uch 	v7fs_inode_setup_disk_image(fs, mem, &disk);
    258  1.1  uch 
    259  1.1  uch 	ILIST_LOCK(fs);
    260  1.1  uch 	if (!(buf = scratch_read(fs, blk))) {
    261  1.1  uch 		ILIST_UNLOCK(fs);
    262  1.1  uch 		return EIO;
    263  1.1  uch 	}
    264  1.1  uch 	struct v7fs_inode_diskimage *di = (struct v7fs_inode_diskimage *)buf;
    265  1.1  uch 	di[ofs] = disk; /* structure copy; */
    266  1.1  uch 	if (!fs->io.write(fs->io.cookie, buf, blk))
    267  1.1  uch 		error = EIO;
    268  1.1  uch 	ILIST_UNLOCK(fs);
    269  1.1  uch 
    270  1.1  uch 	scratch_free(fs, buf);
    271  1.1  uch 
    272  1.1  uch 	return error;
    273  1.1  uch }
    274  1.1  uch 
    275  1.1  uch static int
    276  1.1  uch v7fs_inode_inquire_disk_location(const struct v7fs_self *fs
    277  1.1  uch     __unused, v7fs_ino_t n, v7fs_daddr_t *block,
    278  1.1  uch     v7fs_daddr_t *offset)
    279  1.1  uch {
    280  1.1  uch 	v7fs_daddr_t ofs, blk;
    281  1.1  uch #ifdef V7FS_INODE_DEBUG
    282  1.1  uch 	v7fs_inode_number_sanity(&fs->superblock, n);
    283  1.1  uch #endif
    284  1.1  uch 	ofs = (n - 1/*inode start from 1*/) *
    285  1.1  uch 	    sizeof(struct v7fs_inode_diskimage);
    286  1.1  uch 	blk = ofs >> V7FS_BSHIFT;
    287  1.1  uch 
    288  1.1  uch 	*block = blk + V7FS_ILIST_SECTOR;
    289  1.1  uch 	*offset = (ofs - blk * V7FS_BSIZE) /
    290  1.1  uch 	    sizeof(struct v7fs_inode_diskimage);
    291  1.1  uch #ifdef V7FS_INODE_DEBUG
    292  1.1  uch 	return v7fs_inode_block_sanity(&fs->superblock, *block);
    293  1.1  uch #else
    294  1.1  uch 	return 0;
    295  1.1  uch #endif
    296  1.1  uch }
    297  1.1  uch 
    298