| 1 | /*	$NetBSD: v7fs.h,v 1.4 2022/05/24 06:28:01 andvar Exp $	*/ |
| 2 | |
| 3 | /*- |
| 4 | * Copyright (c) 2011 The NetBSD Foundation, Inc. |
| 5 | * All rights reserved. |
| 6 | * |
| 7 | * This code is derived from software contributed to The NetBSD Foundation |
| 8 | * by UCHIYAMA Yasushi. |
| 9 | * |
| 10 | * Redistribution and use in source and binary forms, with or without |
| 11 | * modification, are permitted provided that the following conditions |
| 12 | * are met: |
| 13 | * 1. Redistributions of source code must retain the above copyright |
| 14 | * notice, this list of conditions and the following disclaimer. |
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
| 16 | * notice, this list of conditions and the following disclaimer in the |
| 17 | * documentation and/or other materials provided with the distribution. |
| 18 | * |
| 19 | * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS |
| 20 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 21 | * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS |
| 23 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | * POSSIBILITY OF SUCH DAMAGE. |
| 30 | */ |
| 31 | |
| 32 | #ifndef _V7FS_H_ |
| 33 | /* 7th Edition of Unix(PDP-11) Filesystem definition. */ |
| 34 | #define	_V7FS_H_ |
| 35 | #include <sys/types.h> |
| 36 | #ifndef _KERNEL |
| 37 | #include <inttypes.h> |
| 38 | #endif |
| 39 | /* |
| 40 | * V7 File System |
| 41 | * |
| 42 | * +------------------ |
| 43 | * |Boot block (512byte)	sector [0] |
| 44 | * | |
| 45 | * +------------------ |
| 46 | * |Super block (512byte)	sector [1] |
| 47 | * | |
| 48 | * +------------------ |
| 49 | * |v7fs_inode(64byte sector [2] |
| 50 | * . |
| 51 | * . |
| 52 | * | |
| 53 | * +------------------ |
| 54 | * |data block sector [datablock_start_sector] |
| 55 | * | |
| 56 | * . |
| 57 | * . |
| 58 | * | |
| 59 | * +------------------ |
| 60 | *				 <- [sector volume_size] |
| 61 | * |
| 62 | * | |
| 63 | * +------------------	volume size. |
| 64 | * |
| 65 | * Max volume size is 8GB (24bit daddr_t) |
| 66 | * Max file size is ~1GB |
| 67 | * |
| 68 | */ |
| 69 | |
| 70 | /* V7 type. */ |
| 71 | typedef uint16_t v7fs_ino_t; |
| 72 | typedef uint32_t v7fs_daddr_t; |
| 73 | typedef int32_t v7fs_time_t; |
| 74 | typedef uint32_t v7fs_off_t; |
| 75 | typedef uint16_t v7fs_dev_t; |
| 76 | typedef uint16_t v7fs_mode_t; |
| 77 | #define	V7FS_DADDR_MAX		0x00ffffff |
| 78 | #define	V7FS_INODE_MAX		0xffff |
| 79 | |
| 80 | #define	V7FS_BSIZE		512 |
| 81 | #define	V7FS_BSHIFT		9 |
| 82 | #define	V7FS_ROUND_BSIZE(x)					\ |
| 83 | 	((((x) + (V7FS_BSIZE - 1)) & ~(V7FS_BSIZE - 1))) |
| 84 | #define	V7FS_TRUNC_BSIZE(x)	((x) & ~(V7FS_BSIZE - 1)) |
| 85 | |
| 86 | #define	V7FS_RESIDUE_BSIZE(x)					\ |
| 87 | 	((x) - ((((x) - 1) >> V7FS_BSHIFT) << V7FS_BSHIFT)) |
| 88 | |
| 89 | /* Disk location. */ |
| 90 | #define	V7FS_BOOTBLOCK_SECTOR	0 |
| 91 | #define	V7FS_SUPERBLOCK_SECTOR	1 |
| 92 | #define	V7FS_ILIST_SECTOR	2 |
| 93 | |
| 94 | /* Superblock */ |
| 95 | /* cache. */ |
| 96 | #define	V7FS_MAX_FREEBLOCK	50 |
| 97 | #define	V7FS_MAX_FREEINODE	100 |
| 98 | struct v7fs_superblock { |
| 99 | 	/* [3 ... (datablock_start_sector-1)]are ilist */ |
| 100 | 	uint16_t datablock_start_sector; |
| 101 | 	v7fs_daddr_t volume_size; |
| 102 | 	int16_t nfreeblock;	/* # of freeblock in superblock cache. */ |
| 103 | 	v7fs_daddr_t freeblock[V7FS_MAX_FREEBLOCK];	/* cache. */ |
| 104 | 	int16_t nfreeinode;	/* # of free inode in superblock cache. */ |
| 105 | 	v7fs_ino_t freeinode[V7FS_MAX_FREEINODE];	/* cache. */ |
| 106 | 	int8_t lock_freeblock; |
| 107 | 	int8_t lock_freeinode; |
| 108 | 	int8_t modified; |
| 109 | 	int8_t readonly; |
| 110 | 	v7fs_time_t update_time; |
| 111 | 	v7fs_daddr_t total_freeblock; |
| 112 | 	v7fs_ino_t total_freeinode; |
| 113 | } __packed; |
| 114 | |
| 115 | /* Datablock */ |
| 116 | #define	V7FS_NADDR		13 |
| 117 | #define	V7FS_NADDR_DIRECT	10 |
| 118 | #define	V7FS_NADDR_INDEX1	10 |
| 119 | #define	V7FS_NADDR_INDEX2	11 |
| 120 | #define	V7FS_NADDR_INDEX3	12 |
| 121 | /* daddr index. */ |
| 122 | #define	V7FS_DADDR_PER_BLOCK	(V7FS_BSIZE / sizeof(v7fs_daddr_t)) |
| 123 | struct v7fs_freeblock { |
| 124 | 	int16_t nfreeblock; |
| 125 | 	v7fs_daddr_t freeblock[V7FS_MAX_FREEBLOCK]; |
| 126 | } __packed; |
| 127 | |
| 128 | |
| 129 | /* Dirent */ |
| 130 | #define	V7FS_NAME_MAX		14 |
| 131 | #define	V7FS_PATH_MAX		PATH_MAX	/* No V7 limit. */ |
| 132 | #define	V7FS_LINK_MAX		LINK_MAX	/* No V7 limit. */ |
| 133 | struct v7fs_dirent { |
| 134 | 	v7fs_ino_t inode_number; |
| 135 | 	char name[V7FS_NAME_MAX]; |
| 136 | } __packed;	/*16byte */ |
| 137 | |
| 138 | /* Inode */ |
| 139 | #define	V7FS_BALBLK_INODE	1	/* monument */ |
| 140 | #define	V7FS_ROOT_INODE		2 |
| 141 | #define	V7FS_MAX_INODE(s)						\ |
| 142 | 	(((s)->datablock_start_sector -	V7FS_ILIST_SECTOR) *		\ |
| 143 | 	V7FS_BSIZE / sizeof(struct v7fs_inode_diskimage)) |
| 144 | #define	V7FS_INODE_PER_BLOCK						\ |
| 145 | 	(V7FS_BSIZE / sizeof(struct v7fs_inode_diskimage)) |
| 146 | #define	V7FS_ILISTBLK_MAX	(V7FS_INODE_MAX / V7FS_INODE_PER_BLOCK) |
| 147 | |
| 148 | struct v7fs_inode_diskimage { |
| 149 | 	int16_t mode; |
| 150 | 	int16_t nlink;	/* [DIR] # of child directories. [REG] link count. */ |
| 151 | 	int16_t uid; |
| 152 | 	int16_t gid; |
| 153 | 	v7fs_off_t filesize;	/* byte */ |
| 154 | #define	V7FS_DINODE_ADDR_LEN	40 |
| 155 | 	/* 39 used; 13 addresses of 3 byte each. */ |
| 156 | 	uint8_t addr[V7FS_DINODE_ADDR_LEN]; |
| 157 | 	/*for device node: addr[0] is major << 8 | minor. */ |
| 158 | 	v7fs_time_t atime; |
| 159 | 	v7fs_time_t mtime; |
| 160 | 	v7fs_time_t ctime; |
| 161 | } __packed;	/*64byte */ |
| 162 | |
| 163 | /* File type */ |
| 164 | #define	V7FS_IFMT	0170000	/* File type mask */ |
| 165 | #define	V7FS_IFCHR	0020000	/* character device */ |
| 166 | #define	V7FS_IFDIR	0040000	/* directory */ |
| 167 | #define	V7FS_IFBLK	0060000	/* block device */ |
| 168 | #define	V7FS_IFREG	0100000	/* file. */ |
| 169 | /* Obsoleted file type. */ |
| 170 | #define	V7FS_IFMPC	0030000	/* multiplexed char special */ |
| 171 | #define	V7FS_IFMPB	0070000	/* multiplexed block special */ |
| 172 | /* Don't appear original V7 filesystem. Found at 2.10BSD. */ |
| 173 | #define	V7FSBSD_IFLNK	0120000	/* symbolic link */ |
| 174 | #define	V7FSBSD_IFSOCK	0140000	/* socket */ |
| 175 | /* Don't appear original V7 filesystem. NetBSD. */ |
| 176 | #define	V7FSBSD_IFFIFO	0010000	/* Named pipe. */ |
| 177 | |
| 178 | #define	V7FSBSD_MAXSYMLINKLEN	V7FS_BSIZE |
| 179 | |
| 180 | #endif	/*!_V7FS_H_ */ |