1 1.3 andvar /* $NetBSD: v7fs_inode.c,v 1.3 2024/09/25 20:10:36 andvar 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.2 apb #if HAVE_NBTOOL_CONFIG_H 33 1.2 apb #include "nbtool_config.h" 34 1.2 apb #endif 35 1.2 apb 36 1.1 uch #include <sys/cdefs.h> 37 1.3 andvar __KERNEL_RCSID(0, "$NetBSD: v7fs_inode.c,v 1.3 2024/09/25 20:10:36 andvar Exp $"); 38 1.1 uch #if defined _KERNEL_OPT 39 1.1 uch #include "opt_v7fs.h" 40 1.1 uch #endif 41 1.1 uch 42 1.1 uch #ifdef _KERNEL 43 1.1 uch #include <sys/systm.h> 44 1.1 uch #include <sys/param.h> 45 1.1 uch #else 46 1.1 uch #include <stdio.h> 47 1.1 uch #include <string.h> 48 1.1 uch #include <errno.h> 49 1.1 uch #include <time.h> 50 1.1 uch #endif 51 1.1 uch 52 1.1 uch #include "v7fs.h" 53 1.1 uch #include "v7fs_impl.h" 54 1.1 uch #include "v7fs_endian.h" 55 1.1 uch #include "v7fs_inode.h" 56 1.1 uch #include "v7fs_superblock.h" 57 1.1 uch 58 1.1 uch #ifdef V7FS_INODE_DEBUG 59 1.1 uch #define DPRINTF(fmt, args...) printf("%s: " fmt, __func__, ##args) 60 1.1 uch #else 61 1.1 uch #define DPRINTF(fmt, args...) ((void)0) 62 1.1 uch #endif 63 1.1 uch 64 1.1 uch static void v7fs_inode_setup_disk_image(const struct v7fs_self *, 65 1.1 uch struct v7fs_inode *, struct v7fs_inode_diskimage *); 66 1.1 uch static int v7fs_inode_inquire_disk_location(const struct v7fs_self *, 67 1.1 uch v7fs_ino_t, v7fs_daddr_t *, v7fs_daddr_t *); 68 1.1 uch #ifdef V7FS_INODE_DEBUG 69 1.1 uch static int v7fs_inode_block_sanity(const struct v7fs_superblock *, 70 1.1 uch v7fs_daddr_t); 71 1.1 uch 72 1.1 uch static int 73 1.1 uch v7fs_inode_block_sanity(const struct v7fs_superblock *sb, v7fs_daddr_t blk) 74 1.1 uch { 75 1.1 uch 76 1.1 uch if ((blk < V7FS_ILIST_SECTOR) || (blk >= sb->datablock_start_sector)) { 77 1.1 uch DPRINTF("invalid inode block#%d (%d-%d)\n", blk, 78 1.1 uch V7FS_ILIST_SECTOR, sb->datablock_start_sector); 79 1.1 uch return ENOSPC; 80 1.1 uch } 81 1.1 uch 82 1.1 uch return 0; 83 1.1 uch } 84 1.1 uch #endif /* V7FS_INODE_DEBUG */ 85 1.1 uch 86 1.1 uch int 87 1.1 uch v7fs_inode_number_sanity(const struct v7fs_superblock *sb, v7fs_ino_t ino) 88 1.1 uch { 89 1.1 uch 90 1.1 uch if (ino < V7FS_ROOT_INODE || ((size_t)ino >= V7FS_MAX_INODE(sb))) { 91 1.1 uch DPRINTF("invalid inode#%d (%d-%zu)\n", ino, 92 1.1 uch V7FS_ROOT_INODE, V7FS_MAX_INODE(sb)); 93 1.1 uch return ENOSPC; 94 1.1 uch } 95 1.1 uch 96 1.1 uch return 0; 97 1.1 uch } 98 1.1 uch 99 1.1 uch int 100 1.1 uch v7fs_inode_allocate(struct v7fs_self *fs, v7fs_ino_t *ino) 101 1.1 uch { 102 1.1 uch struct v7fs_superblock *sb = &fs->superblock; 103 1.1 uch v7fs_ino_t inode_number; 104 1.1 uch int error = ENOSPC; 105 1.1 uch *ino = 0; 106 1.1 uch 107 1.1 uch SUPERB_LOCK(fs); 108 1.1 uch if (sb->total_freeinode == 0) { 109 1.1 uch DPRINTF("inode exhausted!(1)\n"); 110 1.1 uch goto errexit; 111 1.1 uch } 112 1.1 uch 113 1.1 uch /* If there is no free inode cache, update it. */ 114 1.1 uch if (sb->nfreeinode <= 0 && (error = v7fs_freeinode_update(fs))) { 115 1.1 uch DPRINTF("inode exhausted!(2)\n"); 116 1.1 uch goto errexit; 117 1.1 uch } 118 1.1 uch /* Get inode from superblock cache. */ 119 1.1 uch KDASSERT(sb->nfreeinode <= V7FS_MAX_FREEINODE); 120 1.1 uch inode_number = sb->freeinode[--sb->nfreeinode]; 121 1.1 uch sb->total_freeinode--; 122 1.1 uch sb->modified = 1; 123 1.1 uch 124 1.1 uch if ((error = v7fs_inode_number_sanity(sb, inode_number))) { 125 1.1 uch DPRINTF("new inode#%d %d %d\n", inode_number, sb->nfreeinode, 126 1.1 uch sb->total_freeinode); 127 1.3 andvar DPRINTF("free inode list corrupt\n"); 128 1.1 uch goto errexit; 129 1.1 uch } 130 1.1 uch *ino = inode_number; 131 1.1 uch 132 1.1 uch errexit: 133 1.1 uch SUPERB_UNLOCK(fs); 134 1.1 uch 135 1.1 uch return error; 136 1.1 uch } 137 1.1 uch 138 1.1 uch void 139 1.1 uch v7fs_inode_deallocate(struct v7fs_self *fs, v7fs_ino_t ino) 140 1.1 uch { 141 1.1 uch struct v7fs_superblock *sb = &fs->superblock; 142 1.1 uch struct v7fs_inode inode; 143 1.1 uch 144 1.1 uch memset(&inode, 0, sizeof(inode)); 145 1.1 uch inode.inode_number = ino; 146 1.1 uch v7fs_inode_writeback(fs, &inode); 147 1.1 uch 148 1.1 uch SUPERB_LOCK(fs); 149 1.1 uch if (sb->nfreeinode < V7FS_MAX_FREEINODE) { 150 1.1 uch /* link to freeinode list. */ 151 1.1 uch sb->freeinode[sb->nfreeinode++] = ino; 152 1.1 uch } 153 1.1 uch /* If superblock inode cache is full, this inode charged by 154 1.1 uch v7fs_freeinode_update() later. */ 155 1.1 uch sb->total_freeinode++; 156 1.1 uch sb->modified = true; 157 1.1 uch SUPERB_UNLOCK(fs); 158 1.1 uch } 159 1.1 uch 160 1.1 uch void 161 1.1 uch v7fs_inode_setup_memory_image(const struct v7fs_self *fs __unused, 162 1.1 uch struct v7fs_inode *mem, struct v7fs_inode_diskimage *disk) 163 1.1 uch { 164 1.1 uch #define conv16(m) (mem->m = V7FS_VAL16(fs, (disk->m))) 165 1.1 uch #define conv32(m) (mem->m = V7FS_VAL32(fs, (disk->m))) 166 1.1 uch uint32_t addr; 167 1.1 uch int i; 168 1.1 uch 169 1.1 uch memset(mem, 0, sizeof(*mem)); 170 1.1 uch conv16(mode); 171 1.1 uch conv16(nlink); 172 1.1 uch conv16(uid); 173 1.1 uch conv16(gid); 174 1.1 uch conv32(filesize); 175 1.1 uch conv32(atime); 176 1.1 uch conv32(mtime); 177 1.1 uch conv32(ctime); 178 1.1 uch 179 1.1 uch for (i = 0; i < V7FS_NADDR; i++) { 180 1.1 uch int j = i * 3; /* 3 byte each. (v7fs_daddr is 24bit) */ 181 1.1 uch /* expand to 4byte with endian conversion. */ 182 1.1 uch addr = V7FS_VAL24_READ(fs, &disk->addr[j]); 183 1.1 uch mem->addr[i] = addr; 184 1.1 uch } 185 1.1 uch mem->device = 0; 186 1.1 uch if (v7fs_inode_iscdev(mem) || v7fs_inode_isbdev(mem)) { 187 1.1 uch mem->device = mem->addr[0]; 188 1.1 uch } 189 1.1 uch 190 1.1 uch #undef conv16 191 1.1 uch #undef conv32 192 1.1 uch } 193 1.1 uch 194 1.1 uch static void 195 1.1 uch v7fs_inode_setup_disk_image(const struct v7fs_self *fs __unused, 196 1.1 uch struct v7fs_inode *mem, struct v7fs_inode_diskimage *disk) 197 1.1 uch { 198 1.1 uch #define conv16(m) (disk->m = V7FS_VAL16(fs, (mem->m))) 199 1.1 uch #define conv32(m) (disk->m = V7FS_VAL32(fs, (mem->m))) 200 1.1 uch 201 1.1 uch conv16(mode); 202 1.1 uch conv16(nlink); 203 1.1 uch conv16(uid); 204 1.1 uch conv16(gid); 205 1.1 uch conv32(filesize); 206 1.1 uch conv32(atime); 207 1.1 uch conv32(mtime); 208 1.1 uch conv32(ctime); 209 1.1 uch 210 1.1 uch int i; 211 1.1 uch for (i = 0; i < V7FS_NADDR; i++) { 212 1.1 uch int j = i * 3; /* 3 byte each. */ 213 1.1 uch V7FS_VAL24_WRITE(fs, mem->addr[i], disk->addr + j); 214 1.1 uch } 215 1.1 uch #undef conv16 216 1.1 uch #undef conv32 217 1.1 uch } 218 1.1 uch 219 1.1 uch /* Load inode from disk. */ 220 1.1 uch int 221 1.1 uch v7fs_inode_load(struct v7fs_self *fs, struct v7fs_inode *p, v7fs_ino_t n) 222 1.1 uch { 223 1.1 uch v7fs_daddr_t blk, ofs; 224 1.1 uch struct v7fs_inode_diskimage *di; 225 1.1 uch void *buf; 226 1.1 uch 227 1.1 uch if (v7fs_inode_inquire_disk_location(fs, n, &blk, &ofs) != 0) 228 1.1 uch return ENOENT; 229 1.1 uch 230 1.1 uch ILIST_LOCK(fs); 231 1.1 uch if (!(buf = scratch_read(fs, blk))) { 232 1.1 uch ILIST_UNLOCK(fs); 233 1.1 uch return EIO; 234 1.1 uch } 235 1.1 uch ILIST_UNLOCK(fs); 236 1.1 uch di = (struct v7fs_inode_diskimage *)buf; 237 1.1 uch 238 1.1 uch /* Decode disk address, convert endian. */ 239 1.1 uch v7fs_inode_setup_memory_image(fs, p, di + ofs); 240 1.1 uch p->inode_number = n; 241 1.1 uch 242 1.1 uch scratch_free(fs, buf); 243 1.1 uch 244 1.1 uch return 0; 245 1.1 uch } 246 1.1 uch 247 1.1 uch /* Write back inode to disk. */ 248 1.1 uch int 249 1.1 uch v7fs_inode_writeback(struct v7fs_self *fs, struct v7fs_inode *mem) 250 1.1 uch { 251 1.1 uch struct v7fs_inode_diskimage disk; 252 1.1 uch v7fs_ino_t ino = mem->inode_number; 253 1.1 uch v7fs_daddr_t blk; 254 1.1 uch v7fs_daddr_t ofs; 255 1.1 uch void *buf; 256 1.1 uch int error = 0; 257 1.1 uch 258 1.1 uch if (v7fs_inode_inquire_disk_location(fs, ino, &blk, &ofs) != 0) 259 1.1 uch return ENOENT; 260 1.1 uch 261 1.1 uch v7fs_inode_setup_disk_image(fs, mem, &disk); 262 1.1 uch 263 1.1 uch ILIST_LOCK(fs); 264 1.1 uch if (!(buf = scratch_read(fs, blk))) { 265 1.1 uch ILIST_UNLOCK(fs); 266 1.1 uch return EIO; 267 1.1 uch } 268 1.1 uch struct v7fs_inode_diskimage *di = (struct v7fs_inode_diskimage *)buf; 269 1.1 uch di[ofs] = disk; /* structure copy; */ 270 1.1 uch if (!fs->io.write(fs->io.cookie, buf, blk)) 271 1.1 uch error = EIO; 272 1.1 uch ILIST_UNLOCK(fs); 273 1.1 uch 274 1.1 uch scratch_free(fs, buf); 275 1.1 uch 276 1.1 uch return error; 277 1.1 uch } 278 1.1 uch 279 1.1 uch static int 280 1.1 uch v7fs_inode_inquire_disk_location(const struct v7fs_self *fs 281 1.1 uch __unused, v7fs_ino_t n, v7fs_daddr_t *block, 282 1.1 uch v7fs_daddr_t *offset) 283 1.1 uch { 284 1.1 uch v7fs_daddr_t ofs, blk; 285 1.1 uch #ifdef V7FS_INODE_DEBUG 286 1.1 uch v7fs_inode_number_sanity(&fs->superblock, n); 287 1.1 uch #endif 288 1.1 uch ofs = (n - 1/*inode start from 1*/) * 289 1.1 uch sizeof(struct v7fs_inode_diskimage); 290 1.1 uch blk = ofs >> V7FS_BSHIFT; 291 1.1 uch 292 1.1 uch *block = blk + V7FS_ILIST_SECTOR; 293 1.1 uch *offset = (ofs - blk * V7FS_BSIZE) / 294 1.1 uch sizeof(struct v7fs_inode_diskimage); 295 1.1 uch #ifdef V7FS_INODE_DEBUG 296 1.1 uch return v7fs_inode_block_sanity(&fs->superblock, *block); 297 1.1 uch #else 298 1.1 uch return 0; 299 1.1 uch #endif 300 1.1 uch } 301 1.1 uch 302