1 1.13 rin /* $NetBSD: minixfs3.c,v 1.13 2022/04/29 07:42:07 rin Exp $ */ 2 1.1 christos 3 1.1 christos /*- 4 1.1 christos * Copyright (c) 2012 5 1.1 christos * Vrije Universiteit, Amsterdam, The Netherlands. All rights reserved. 6 1.1 christos * 7 1.1 christos * Author: Evgeniy Ivanov (based on libsa/ext2fs.c). 8 1.1 christos * 9 1.1 christos * This code is derived from src/sys/lib/libsa/ext2fs.c contributed to 10 1.1 christos * The NetBSD Foundation, see copyrights below. 11 1.1 christos * 12 1.1 christos * Redistribution and use in source and binary forms, with or without 13 1.1 christos * modification, are permitted provided that the following conditions 14 1.1 christos * are met: 15 1.1 christos * 1. Redistributions of source code must retain the above copyright 16 1.1 christos * notice, this list of conditions and the following disclaimer. 17 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 18 1.1 christos * notice, this list of conditions and the following disclaimer in the 19 1.1 christos * documentation and/or other materials provided with the distribution. 20 1.1 christos * 21 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS 22 1.1 christos * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 23 1.1 christos * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 1.1 christos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE 25 1.1 christos * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 1.1 christos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 1.1 christos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 1.1 christos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 29 1.1 christos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 30 1.1 christos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 31 1.1 christos * POSSIBILITY OF SUCH DAMAGE. 32 1.1 christos */ 33 1.1 christos 34 1.1 christos /* 35 1.1 christos * Copyright (c) 1997 Manuel Bouyer. 36 1.1 christos * 37 1.1 christos * Redistribution and use in source and binary forms, with or without 38 1.1 christos * modification, are permitted provided that the following conditions 39 1.1 christos * are met: 40 1.1 christos * 1. Redistributions of source code must retain the above copyright 41 1.1 christos * notice, this list of conditions and the following disclaimer. 42 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 43 1.1 christos * notice, this list of conditions and the following disclaimer in the 44 1.1 christos * documentation and/or other materials provided with the distribution. 45 1.1 christos * 46 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 47 1.1 christos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 48 1.1 christos * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 49 1.1 christos * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 50 1.1 christos * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 51 1.1 christos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 52 1.1 christos * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 53 1.1 christos * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 54 1.1 christos * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 55 1.1 christos * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 56 1.1 christos */ 57 1.1 christos 58 1.1 christos /*- 59 1.1 christos * Copyright (c) 1993 60 1.1 christos * The Regents of the University of California. All rights reserved. 61 1.1 christos * 62 1.1 christos * This code is derived from software contributed to Berkeley by 63 1.1 christos * The Mach Operating System project at Carnegie-Mellon University. 64 1.1 christos * 65 1.1 christos * Redistribution and use in source and binary forms, with or without 66 1.1 christos * modification, are permitted provided that the following conditions 67 1.1 christos * are met: 68 1.1 christos * 1. Redistributions of source code must retain the above copyright 69 1.1 christos * notice, this list of conditions and the following disclaimer. 70 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright 71 1.1 christos * notice, this list of conditions and the following disclaimer in the 72 1.1 christos * documentation and/or other materials provided with the distribution. 73 1.1 christos * 3. Neither the name of the University nor the names of its contributors 74 1.1 christos * may be used to endorse or promote products derived from this software 75 1.1 christos * without specific prior written permission. 76 1.1 christos * 77 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 78 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 79 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 80 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 81 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 82 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 83 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 84 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 85 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 86 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 87 1.1 christos * SUCH DAMAGE. 88 1.1 christos * 89 1.1 christos * 90 1.1 christos * Copyright (c) 1990, 1991 Carnegie Mellon University 91 1.1 christos * All Rights Reserved. 92 1.1 christos * 93 1.1 christos * Author: David Golub 94 1.1 christos * 95 1.1 christos * Permission to use, copy, modify and distribute this software and its 96 1.1 christos * documentation is hereby granted, provided that both the copyright 97 1.1 christos * notice and this permission notice appear in all copies of the 98 1.1 christos * software, derivative works or modified versions, and any portions 99 1.1 christos * thereof, and that both notices appear in supporting documentation. 100 1.1 christos * 101 1.1 christos * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 102 1.1 christos * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 103 1.1 christos * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 104 1.1 christos * 105 1.1 christos * Carnegie Mellon requests users of this software to return to 106 1.1 christos * 107 1.1 christos * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU 108 1.1 christos * School of Computer Science 109 1.1 christos * Carnegie Mellon University 110 1.1 christos * Pittsburgh PA 15213-3890 111 1.1 christos * 112 1.1 christos * any improvements or extensions that they make and grant Carnegie the 113 1.1 christos * rights to redistribute these changes. 114 1.1 christos */ 115 1.1 christos 116 1.1 christos /* 117 1.1 christos * Stand-alone file reading package for MFS file system. 118 1.1 christos */ 119 1.1 christos 120 1.1 christos #include <sys/param.h> 121 1.1 christos #include <sys/time.h> 122 1.1 christos #ifdef _STANDALONE 123 1.1 christos #include <lib/libkern/libkern.h> 124 1.1 christos #else 125 1.8 christos #include <stddef.h> 126 1.1 christos #include <string.h> 127 1.1 christos #endif 128 1.1 christos 129 1.1 christos #include "stand.h" 130 1.1 christos #include "minixfs3.h" 131 1.1 christos 132 1.1 christos #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK) 133 1.1 christos #define LIBSA_NO_FS_SYMLINK 134 1.1 christos #endif 135 1.1 christos 136 1.1 christos #if defined(LIBSA_NO_TWIDDLE) 137 1.1 christos #define twiddle() 138 1.1 christos #endif 139 1.1 christos 140 1.1 christos typedef uint32_t ino32_t; 141 1.1 christos #ifndef FSBTODB 142 1.4 dholland #define FSBTODB(fs, indp) MFS_FSBTODB(fs, indp) 143 1.1 christos #endif 144 1.1 christos 145 1.1 christos /* 146 1.1 christos * To avoid having a lot of filesystem-block sized buffers lurking (which 147 1.1 christos * could be 32k) we only keep a few entries of the indirect block map. 148 1.1 christos * With 8k blocks, 2^8 blocks is ~500k so we reread the indirect block 149 1.1 christos * ~13 times pulling in a 6M kernel. 150 1.1 christos * The cache size must be smaller than the smallest filesystem block, 151 1.1 christos * so LN2_IND_CACHE_SZ <= 9 (UFS2 and 4k blocks). 152 1.1 christos */ 153 1.1 christos #define LN2_IND_CACHE_SZ 6 154 1.1 christos #define IND_CACHE_SZ (1 << LN2_IND_CACHE_SZ) 155 1.1 christos #define IND_CACHE_MASK (IND_CACHE_SZ - 1) 156 1.1 christos 157 1.1 christos /* 158 1.1 christos * In-core open file. 159 1.1 christos */ 160 1.1 christos struct file { 161 1.1 christos off_t f_seekp; /* seek pointer */ 162 1.1 christos struct mfs_sblock *f_fs; /* pointer to super-block */ 163 1.1 christos struct mfs_dinode f_di; /* copy of on-disk inode */ 164 1.1 christos uint f_nishift; /* for blocks in indirect block */ 165 1.1 christos block_t f_ind_cache_block; 166 1.1 christos block_t f_ind_cache[IND_CACHE_SZ]; 167 1.1 christos 168 1.1 christos char *f_buf; /* buffer for data block */ 169 1.1 christos size_t f_buf_size; /* size of data block */ 170 1.1 christos daddr_t f_buf_blkno; /* block number of data block */ 171 1.1 christos }; 172 1.1 christos 173 1.1 christos static int read_inode(ino32_t, struct open_file *); 174 1.1 christos static int block_map(struct open_file *, block_t, block_t *); 175 1.1 christos static int buf_read_file(struct open_file *, void *, size_t *); 176 1.1 christos static int search_directory(const char *, int, struct open_file *, ino32_t *); 177 1.1 christos static int read_sblock(struct open_file *, struct mfs_sblock *); 178 1.1 christos 179 1.1 christos /* 180 1.1 christos * Read a new inode into a file structure. 181 1.1 christos */ 182 1.1 christos static int 183 1.1 christos read_inode(ino32_t inumber, struct open_file *f) 184 1.1 christos { 185 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 186 1.1 christos struct mfs_sblock *fs = fp->f_fs; 187 1.1 christos char *buf; 188 1.1 christos size_t rsize; 189 1.1 christos int rc; 190 1.1 christos daddr_t inode_sector; 191 1.1 christos struct mfs_dinode *dip; 192 1.1 christos 193 1.1 christos inode_sector = FSBTODB(fs, ino_to_fsba(fs, inumber)); 194 1.1 christos 195 1.1 christos /* 196 1.1 christos * Read inode and save it. 197 1.1 christos */ 198 1.1 christos buf = fp->f_buf; 199 1.1 christos twiddle(); 200 1.1 christos rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 201 1.1 christos inode_sector, fs->mfs_block_size, buf, &rsize); 202 1.1 christos if (rc) 203 1.1 christos return rc; 204 1.1 christos if (rsize != fs->mfs_block_size) 205 1.1 christos return EIO; 206 1.1 christos 207 1.1 christos dip = (struct mfs_dinode *)(buf + 208 1.1 christos INODE_SIZE * ino_to_fsbo(fs, inumber)); 209 1.1 christos mfs_iload(dip, &fp->f_di); 210 1.1 christos 211 1.1 christos /* 212 1.1 christos * Clear out the old buffers 213 1.1 christos */ 214 1.1 christos fp->f_ind_cache_block = ~0; 215 1.1 christos fp->f_buf_blkno = -1; 216 1.1 christos return rc; 217 1.1 christos } 218 1.1 christos 219 1.1 christos /* 220 1.1 christos * Given an offset in a file, find the disk block number (not zone!) 221 1.1 christos * that contains that block. 222 1.1 christos */ 223 1.1 christos static int 224 1.1 christos block_map(struct open_file *f, block_t file_block, block_t *disk_block_p) 225 1.1 christos { 226 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 227 1.1 christos struct mfs_sblock *fs = fp->f_fs; 228 1.1 christos uint level; 229 1.1 christos block_t ind_cache; 230 1.1 christos block_t ind_block_num; 231 1.1 christos zone_t zone; 232 1.1 christos size_t rsize; 233 1.1 christos int rc; 234 1.1 christos int boff; 235 1.1 christos int scale = fs->mfs_log_zone_size; /* for block-zone conversion */ 236 1.1 christos block_t *buf = (void *)fp->f_buf; 237 1.1 christos 238 1.1 christos /* 239 1.1 christos * Index structure of an inode: 240 1.1 christos * 241 1.1 christos * mdi_blocks[0..NR_DZONES-1] 242 1.1 christos * hold zone numbers for zones 243 1.1 christos * 0..NR_DZONES-1 244 1.1 christos * 245 1.1 christos * mdi_blocks[NR_DZONES+0] 246 1.1 christos * block NDADDR+0 is the single indirect block 247 1.1 christos * holds zone numbers for zones 248 1.2 dholland * NR_DZONES .. NR_DZONES + MFS_NINDIR(fs)-1 249 1.1 christos * 250 1.1 christos * mdi_blocks[NR_DZONES+1] 251 1.1 christos * block NDADDR+1 is the double indirect block 252 1.1 christos * holds zone numbers for INDEX blocks for zones 253 1.2 dholland * NR_DZONES + MFS_NINDIR(fs) .. 254 1.2 dholland * NR_TZONES + MFS_NINDIR(fs) + MFS_NINDIR(fs)**2 - 1 255 1.1 christos */ 256 1.1 christos 257 1.1 christos zone = file_block >> scale; 258 1.1 christos boff = (int) (file_block - (zone << scale) ); /* relative blk in zone */ 259 1.1 christos 260 1.1 christos if (zone < NR_DZONES) { 261 1.1 christos /* Direct zone */ 262 1.1 christos zone_t z = fs2h32(fp->f_di.mdi_zone[zone]); 263 1.1 christos if (z == NO_ZONE) { 264 1.1 christos *disk_block_p = NO_BLOCK; 265 1.1 christos return 0; 266 1.1 christos } 267 1.1 christos *disk_block_p = (block_t) ((z << scale) + boff); 268 1.1 christos return 0; 269 1.1 christos } 270 1.1 christos 271 1.1 christos zone -= NR_DZONES; 272 1.1 christos 273 1.1 christos ind_cache = zone >> LN2_IND_CACHE_SZ; 274 1.1 christos if (ind_cache == fp->f_ind_cache_block) { 275 1.1 christos *disk_block_p = 276 1.1 christos fs2h32(fp->f_ind_cache[zone & IND_CACHE_MASK]); 277 1.1 christos return 0; 278 1.1 christos } 279 1.1 christos 280 1.1 christos for (level = 0;;) { 281 1.1 christos level += fp->f_nishift; 282 1.1 christos 283 1.1 christos if (zone < (block_t)1 << level) 284 1.1 christos break; 285 1.1 christos if (level > NIADDR * fp->f_nishift) 286 1.1 christos /* Zone number too high */ 287 1.1 christos return EFBIG; 288 1.1 christos zone -= (block_t)1 << level; 289 1.1 christos } 290 1.1 christos 291 1.1 christos ind_block_num = 292 1.1 christos fs2h32(fp->f_di.mdi_zone[NR_DZONES + (level / fp->f_nishift - 1)]); 293 1.1 christos 294 1.1 christos for (;;) { 295 1.1 christos level -= fp->f_nishift; 296 1.1 christos if (ind_block_num == 0) { 297 1.1 christos *disk_block_p = NO_BLOCK; /* missing */ 298 1.1 christos return 0; 299 1.1 christos } 300 1.1 christos 301 1.1 christos twiddle(); 302 1.1 christos /* 303 1.1 christos * If we were feeling brave, we could work out the number 304 1.1 christos * of the disk sector and read a single disk sector instead 305 1.1 christos * of a filesystem block. 306 1.1 christos * However we don't do this very often anyway... 307 1.1 christos */ 308 1.1 christos rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 309 1.1 christos FSBTODB(fs, ind_block_num), fs->mfs_block_size, 310 1.1 christos buf, &rsize); 311 1.1 christos if (rc) 312 1.1 christos return rc; 313 1.1 christos if (rsize != fs->mfs_block_size) 314 1.1 christos return EIO; 315 1.1 christos 316 1.1 christos ind_block_num = fs2h32(buf[zone >> level]); 317 1.1 christos if (level == 0) 318 1.1 christos break; 319 1.1 christos zone &= (1 << level) - 1; 320 1.1 christos } 321 1.1 christos 322 1.1 christos /* Save the part of the block that contains this sector */ 323 1.1 christos memcpy(fp->f_ind_cache, &buf[zone & ~IND_CACHE_MASK], 324 1.1 christos IND_CACHE_SZ * sizeof fp->f_ind_cache[0]); 325 1.1 christos fp->f_ind_cache_block = ind_cache; 326 1.1 christos 327 1.1 christos zone = (zone_t)ind_block_num; 328 1.1 christos *disk_block_p = (block_t)((zone << scale) + boff); 329 1.1 christos return 0; 330 1.1 christos } 331 1.1 christos 332 1.1 christos /* 333 1.1 christos * Read a portion of a file into an internal buffer. 334 1.1 christos * Return the location in the buffer and the amount in the buffer. 335 1.1 christos */ 336 1.1 christos static int 337 1.1 christos buf_read_file(struct open_file *f, void *v, size_t *size_p) 338 1.1 christos { 339 1.1 christos char **buf_p = v; 340 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 341 1.1 christos struct mfs_sblock *fs = fp->f_fs; 342 1.1 christos long off; 343 1.1 christos block_t file_block; 344 1.6 christos block_t disk_block = 0; /* XXX: gcc */ 345 1.8 christos size_t block_size, nsz; 346 1.1 christos int rc; 347 1.1 christos 348 1.3 dholland off = mfs_blkoff(fs, fp->f_seekp); 349 1.5 dholland file_block = mfs_lblkno(fs, fp->f_seekp); 350 1.1 christos block_size = fs->mfs_block_size; 351 1.1 christos 352 1.1 christos if (file_block != fp->f_buf_blkno) { 353 1.1 christos rc = block_map(f, file_block, &disk_block); 354 1.1 christos if (rc) 355 1.1 christos return rc; 356 1.1 christos 357 1.1 christos if (disk_block == 0) { 358 1.1 christos memset(fp->f_buf, 0, block_size); 359 1.1 christos fp->f_buf_size = block_size; 360 1.1 christos } else { 361 1.1 christos twiddle(); 362 1.1 christos rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 363 1.1 christos FSBTODB(fs, disk_block), 364 1.1 christos block_size, fp->f_buf, &fp->f_buf_size); 365 1.1 christos if (rc) 366 1.1 christos return rc; 367 1.1 christos } 368 1.1 christos 369 1.1 christos fp->f_buf_blkno = file_block; 370 1.1 christos } 371 1.1 christos 372 1.1 christos /* 373 1.1 christos * Return address of byte in buffer corresponding to 374 1.1 christos * offset, and size of remainder of buffer after that 375 1.1 christos * byte. 376 1.1 christos */ 377 1.1 christos *buf_p = fp->f_buf + off; 378 1.1 christos *size_p = block_size - off; 379 1.1 christos 380 1.1 christos /* 381 1.1 christos * But truncate buffer at end of file. 382 1.1 christos */ 383 1.8 christos nsz = (size_t)(fp->f_di.mdi_size - fp->f_seekp); 384 1.8 christos if (*size_p > nsz) 385 1.8 christos *size_p = nsz; 386 1.1 christos 387 1.1 christos return 0; 388 1.1 christos } 389 1.1 christos 390 1.1 christos /* 391 1.1 christos * Search a directory for a name and return its 392 1.1 christos * inode number. 393 1.1 christos */ 394 1.1 christos static int 395 1.1 christos search_directory(const char *name, int length, struct open_file *f, 396 1.1 christos ino32_t *inumber_p) 397 1.1 christos { 398 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 399 1.1 christos struct mfs_sblock *fs = fp->f_fs; 400 1.1 christos struct mfs_direct *dp; 401 1.1 christos struct mfs_direct *dbuf; 402 1.1 christos size_t buf_size; 403 1.1 christos int namlen; 404 1.1 christos int rc; 405 1.1 christos 406 1.1 christos fp->f_seekp = 0; 407 1.1 christos 408 1.1 christos while (fp->f_seekp < (off_t)fp->f_di.mdi_size) { 409 1.1 christos rc = buf_read_file(f, (void *)&dbuf, &buf_size); 410 1.1 christos if (rc) 411 1.1 christos return rc; 412 1.1 christos if (buf_size == 0) 413 1.1 christos return EIO; 414 1.1 christos 415 1.1 christos /* XXX we assume, that buf_read_file reads an fs block and 416 1.1 christos * doesn't truncate buffer. Currently i_size in MFS doesn't 417 1.1 christos * the same as size of allocated blocks, it makes buf_read_file 418 1.1 christos * to truncate buf_size. 419 1.1 christos */ 420 1.1 christos if (buf_size < fs->mfs_block_size) 421 1.1 christos buf_size = fs->mfs_block_size; 422 1.1 christos 423 1.1 christos for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) { 424 1.1 christos char *cp; 425 1.1 christos if (fs2h32(dp->mfsd_ino) == (ino32_t) 0) 426 1.1 christos continue; 427 1.1 christos /* Compute the length of the name */ 428 1.1 christos cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name)); 429 1.1 christos if (cp == NULL) 430 1.1 christos namlen = sizeof(dp->mfsd_name); 431 1.1 christos else 432 1.1 christos namlen = cp - (dp->mfsd_name); 433 1.1 christos 434 1.1 christos if (namlen == length && 435 1.1 christos !memcmp(name, dp->mfsd_name, length)) { 436 1.1 christos /* found entry */ 437 1.1 christos *inumber_p = fs2h32(dp->mfsd_ino); 438 1.1 christos return 0; 439 1.1 christos } 440 1.1 christos } 441 1.1 christos fp->f_seekp += buf_size; 442 1.1 christos } 443 1.1 christos return ENOENT; 444 1.1 christos } 445 1.1 christos 446 1.1 christos int 447 1.1 christos read_sblock(struct open_file *f, struct mfs_sblock *fs) 448 1.1 christos { 449 1.1 christos static uint8_t sbbuf[MINBSIZE]; 450 1.1 christos size_t buf_size; 451 1.1 christos int rc; 452 1.1 christos 453 1.1 christos /* We must read amount multiple of sector size, hence we can't 454 1.1 christos * read SBSIZE and read MINBSIZE. 455 1.1 christos */ 456 1.1 christos if (SBSIZE > MINBSIZE) 457 1.1 christos return EINVAL; 458 1.1 christos 459 1.1 christos rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 460 1.13 rin SUPER_BLOCK_OFF / GETSECSIZE(f), MINBSIZE, sbbuf, &buf_size); 461 1.1 christos if (rc) 462 1.1 christos return rc; 463 1.1 christos 464 1.1 christos if (buf_size != MINBSIZE) 465 1.1 christos return EIO; 466 1.1 christos 467 1.1 christos mfs_sbload((void *)sbbuf, fs); 468 1.1 christos 469 1.1 christos if (fs->mfs_magic != SUPER_MAGIC) 470 1.1 christos return EINVAL; 471 1.1 christos if (fs->mfs_block_size < MINBSIZE) 472 1.1 christos return EINVAL; 473 1.1 christos if ((fs->mfs_block_size % 512) != 0) 474 1.1 christos return EINVAL; 475 1.1 christos if (SBSIZE > fs->mfs_block_size) 476 1.1 christos return EINVAL; 477 1.1 christos if ((fs->mfs_block_size % INODE_SIZE) != 0) 478 1.1 christos return EINVAL; 479 1.1 christos 480 1.1 christos /* For even larger disks, a similar problem occurs with s_firstdatazone. 481 1.1 christos * If the on-disk field contains zero, we assume that the value was too 482 1.1 christos * large to fit, and compute it on the fly. 483 1.1 christos */ 484 1.1 christos if (fs->mfs_firstdatazone_old == 0) { 485 1.1 christos block_t offset; 486 1.1 christos offset = START_BLOCK + fs->mfs_imap_blocks + fs->mfs_zmap_blocks; 487 1.1 christos offset += (fs->mfs_ninodes + fs->mfs_inodes_per_block - 1) / 488 1.1 christos fs->mfs_inodes_per_block; 489 1.1 christos 490 1.1 christos fs->mfs_firstdatazone = 491 1.1 christos (offset + (1 << fs->mfs_log_zone_size) - 1) >> 492 1.1 christos fs->mfs_log_zone_size; 493 1.1 christos } else { 494 1.1 christos fs->mfs_firstdatazone = (zone_t) fs->mfs_firstdatazone_old; 495 1.1 christos } 496 1.1 christos 497 1.1 christos if (fs->mfs_imap_blocks < 1 || fs->mfs_zmap_blocks < 1 498 1.1 christos || fs->mfs_ninodes < 1 || fs->mfs_zones < 1 499 1.1 christos || fs->mfs_firstdatazone <= 4 500 1.1 christos || fs->mfs_firstdatazone >= fs->mfs_zones 501 1.1 christos || (unsigned) fs->mfs_log_zone_size > 4) 502 1.1 christos return EINVAL; 503 1.1 christos 504 1.1 christos /* compute in-memory mfs_sblock values */ 505 1.1 christos fs->mfs_inodes_per_block = fs->mfs_block_size / INODE_SIZE; 506 1.1 christos 507 1.1 christos 508 1.1 christos { 509 1.1 christos int32_t mult = fs->mfs_block_size >> LOG_MINBSIZE; 510 1.1 christos int ln2 = LOG_MINBSIZE; 511 1.1 christos 512 1.1 christos for (; mult != 1; ln2++) 513 1.1 christos mult >>= 1; 514 1.1 christos 515 1.1 christos fs->mfs_bshift = ln2; 516 1.1 christos /* XXX assume hw bsize = 512 */ 517 1.1 christos fs->mfs_fsbtodb = ln2 - LOG_MINBSIZE + 1; 518 1.1 christos } 519 1.1 christos 520 1.1 christos fs->mfs_qbmask = fs->mfs_block_size - 1; 521 1.1 christos fs->mfs_bmask = ~fs->mfs_qbmask; 522 1.1 christos 523 1.1 christos return 0; 524 1.1 christos } 525 1.1 christos 526 1.1 christos /* 527 1.1 christos * Open a file. 528 1.1 christos */ 529 1.1 christos __compactcall int 530 1.1 christos minixfs3_open(const char *path, struct open_file *f) 531 1.1 christos { 532 1.1 christos #ifndef LIBSA_FS_SINGLECOMPONENT 533 1.1 christos const char *cp, *ncp; 534 1.1 christos int c; 535 1.1 christos #endif 536 1.1 christos ino32_t inumber; 537 1.1 christos struct file *fp; 538 1.1 christos struct mfs_sblock *fs; 539 1.1 christos int rc; 540 1.1 christos #ifndef LIBSA_NO_FS_SYMLINK 541 1.1 christos ino32_t parent_inumber; 542 1.1 christos int nlinks = 0; 543 1.1 christos char namebuf[MAXPATHLEN+1]; 544 1.1 christos char *buf; 545 1.1 christos #endif 546 1.1 christos 547 1.1 christos /* allocate file system specific data structure */ 548 1.1 christos fp = alloc(sizeof(struct file)); 549 1.1 christos memset(fp, 0, sizeof(struct file)); 550 1.1 christos f->f_fsdata = (void *)fp; 551 1.1 christos 552 1.1 christos /* allocate space and read super block */ 553 1.1 christos fs = alloc(sizeof(*fs)); 554 1.1 christos memset(fs, 0, sizeof(*fs)); 555 1.1 christos fp->f_fs = fs; 556 1.1 christos twiddle(); 557 1.1 christos 558 1.1 christos rc = read_sblock(f, fs); 559 1.1 christos if (rc) 560 1.1 christos goto out; 561 1.1 christos 562 1.1 christos /* alloc a block sized buffer used for all fs transfers */ 563 1.1 christos fp->f_buf = alloc(fs->mfs_block_size); 564 1.1 christos 565 1.1 christos /* 566 1.1 christos * Calculate indirect block levels. 567 1.1 christos */ 568 1.1 christos { 569 1.1 christos int32_t mult; 570 1.1 christos int ln2; 571 1.1 christos 572 1.1 christos /* 573 1.1 christos * We note that the number of indirect blocks is always 574 1.1 christos * a power of 2. This lets us use shifts and masks instead 575 1.9 skrll * of divide and remainder and avoids pulling in the 576 1.1 christos * 64bit division routine into the boot code. 577 1.1 christos */ 578 1.2 dholland mult = MFS_NINDIR(fs); 579 1.1 christos #ifdef DEBUG 580 1.1 christos if (!powerof2(mult)) { 581 1.1 christos /* Hummm was't a power of 2 */ 582 1.1 christos rc = EINVAL; 583 1.1 christos goto out; 584 1.1 christos } 585 1.1 christos #endif 586 1.1 christos for (ln2 = 0; mult != 1; ln2++) 587 1.1 christos mult >>= 1; 588 1.1 christos 589 1.1 christos fp->f_nishift = ln2; 590 1.1 christos } 591 1.1 christos 592 1.1 christos inumber = ROOT_INODE; 593 1.1 christos if ((rc = read_inode(inumber, f)) != 0) 594 1.1 christos goto out; 595 1.1 christos 596 1.1 christos #ifndef LIBSA_FS_SINGLECOMPONENT 597 1.1 christos cp = path; 598 1.1 christos while (*cp) { 599 1.1 christos 600 1.1 christos /* 601 1.1 christos * Remove extra separators 602 1.1 christos */ 603 1.1 christos while (*cp == '/') 604 1.1 christos cp++; 605 1.1 christos if (*cp == '\0') 606 1.1 christos break; 607 1.1 christos 608 1.1 christos /* 609 1.1 christos * Check that current node is a directory. 610 1.1 christos */ 611 1.1 christos if ((fp->f_di.mdi_mode & I_TYPE) != I_DIRECTORY) { 612 1.1 christos rc = ENOTDIR; 613 1.1 christos goto out; 614 1.1 christos } 615 1.1 christos 616 1.1 christos /* 617 1.1 christos * Get next component of path name. 618 1.1 christos */ 619 1.1 christos ncp = cp; 620 1.1 christos while ((c = *cp) != '\0' && c != '/') 621 1.1 christos cp++; 622 1.1 christos 623 1.1 christos /* 624 1.1 christos * Look up component in current directory. 625 1.1 christos * Save directory inumber in case we find a 626 1.1 christos * symbolic link. 627 1.1 christos */ 628 1.1 christos #ifndef LIBSA_NO_FS_SYMLINK 629 1.1 christos parent_inumber = inumber; 630 1.1 christos #endif 631 1.1 christos rc = search_directory(ncp, cp - ncp, f, &inumber); 632 1.1 christos if (rc) 633 1.1 christos goto out; 634 1.1 christos 635 1.1 christos /* 636 1.1 christos * Open next component. 637 1.1 christos */ 638 1.1 christos if ((rc = read_inode(inumber, f)) != 0) 639 1.1 christos goto out; 640 1.1 christos 641 1.1 christos #ifndef LIBSA_NO_FS_SYMLINK 642 1.1 christos /* 643 1.1 christos * Check for symbolic link. 644 1.1 christos */ 645 1.1 christos if ((fp->f_di.mdi_mode & I_TYPE) == I_SYMBOLIC_LINK) { 646 1.1 christos int link_len = fp->f_di.mdi_size; 647 1.1 christos int len; 648 1.1 christos size_t buf_size; 649 1.1 christos block_t disk_block; 650 1.1 christos 651 1.1 christos len = strlen(cp); 652 1.1 christos 653 1.1 christos if (link_len + len > MAXPATHLEN || 654 1.1 christos ++nlinks > MAXSYMLINKS) { 655 1.1 christos rc = ENOENT; 656 1.1 christos goto out; 657 1.1 christos } 658 1.1 christos 659 1.1 christos memmove(&namebuf[link_len], cp, len + 1); 660 1.1 christos 661 1.1 christos /* 662 1.1 christos * Read file for symbolic link 663 1.1 christos */ 664 1.1 christos buf = fp->f_buf; 665 1.1 christos rc = block_map(f, (block_t)0, &disk_block); 666 1.1 christos if (rc) 667 1.1 christos goto out; 668 1.1 christos 669 1.1 christos twiddle(); 670 1.1 christos rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, 671 1.1 christos F_READ, FSBTODB(fs, disk_block), 672 1.1 christos fs->mfs_block_size, buf, &buf_size); 673 1.1 christos if (rc) 674 1.1 christos goto out; 675 1.1 christos 676 1.1 christos memcpy(namebuf, buf, link_len); 677 1.1 christos 678 1.1 christos /* 679 1.1 christos * If relative pathname, restart at parent directory. 680 1.1 christos * If absolute pathname, restart at root. 681 1.1 christos */ 682 1.1 christos cp = namebuf; 683 1.1 christos if (*cp != '/') 684 1.1 christos inumber = parent_inumber; 685 1.1 christos else 686 1.1 christos inumber = (ino32_t) ROOT_INODE; 687 1.1 christos 688 1.1 christos if ((rc = read_inode(inumber, f)) != 0) 689 1.1 christos goto out; 690 1.1 christos } 691 1.1 christos #endif /* !LIBSA_NO_FS_SYMLINK */ 692 1.1 christos } 693 1.1 christos 694 1.1 christos /* 695 1.1 christos * Found terminal component. 696 1.1 christos */ 697 1.1 christos rc = 0; 698 1.1 christos 699 1.1 christos #else /* !LIBSA_FS_SINGLECOMPONENT */ 700 1.1 christos 701 1.1 christos /* look up component in the current (root) directory */ 702 1.1 christos rc = search_directory(path, strlen(path), f, &inumber); 703 1.1 christos if (rc) 704 1.1 christos goto out; 705 1.1 christos 706 1.1 christos /* open it */ 707 1.1 christos rc = read_inode(inumber, f); 708 1.1 christos 709 1.1 christos #endif /* !LIBSA_FS_SINGLECOMPONENT */ 710 1.1 christos 711 1.1 christos fp->f_seekp = 0; /* reset seek pointer */ 712 1.1 christos 713 1.1 christos out: 714 1.1 christos if (rc) 715 1.1 christos minixfs3_close(f); 716 1.1 christos 717 1.1 christos return rc; 718 1.1 christos } 719 1.1 christos 720 1.1 christos __compactcall int 721 1.1 christos minixfs3_close(struct open_file *f) 722 1.1 christos { 723 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 724 1.1 christos 725 1.1 christos f->f_fsdata = NULL; 726 1.1 christos if (fp == NULL) 727 1.1 christos return 0; 728 1.1 christos 729 1.1 christos if (fp->f_buf) 730 1.1 christos dealloc(fp->f_buf, fp->f_fs->mfs_block_size); 731 1.1 christos dealloc(fp->f_fs, sizeof(*fp->f_fs)); 732 1.1 christos dealloc(fp, sizeof(struct file)); 733 1.1 christos return 0; 734 1.1 christos } 735 1.1 christos 736 1.1 christos /* 737 1.1 christos * Copy a portion of a file into kernel memory. 738 1.1 christos * Cross block boundaries when necessary. 739 1.1 christos */ 740 1.1 christos __compactcall int 741 1.1 christos minixfs3_read(struct open_file *f, void *start, size_t size, size_t *resid) 742 1.1 christos { 743 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 744 1.1 christos size_t csize; 745 1.1 christos char *buf; 746 1.1 christos size_t buf_size; 747 1.1 christos int rc = 0; 748 1.1 christos char *addr = start; 749 1.1 christos 750 1.1 christos while (size != 0) { 751 1.1 christos if (fp->f_seekp >= (off_t)fp->f_di.mdi_size) 752 1.1 christos break; 753 1.1 christos 754 1.1 christos rc = buf_read_file(f, &buf, &buf_size); 755 1.1 christos if (rc) 756 1.1 christos break; 757 1.1 christos 758 1.1 christos csize = size; 759 1.1 christos if (csize > buf_size) 760 1.1 christos csize = buf_size; 761 1.1 christos 762 1.1 christos memcpy(addr, buf, csize); 763 1.1 christos 764 1.1 christos fp->f_seekp += csize; 765 1.1 christos addr += csize; 766 1.1 christos size -= csize; 767 1.1 christos } 768 1.1 christos 769 1.1 christos if (resid) 770 1.1 christos *resid = size; 771 1.1 christos return rc; 772 1.1 christos } 773 1.1 christos 774 1.1 christos /* 775 1.1 christos * Not implemented. 776 1.1 christos */ 777 1.1 christos #ifndef LIBSA_NO_FS_WRITE 778 1.1 christos __compactcall int 779 1.1 christos minixfs3_write(struct open_file *f, void *start, size_t size, size_t *resid) 780 1.1 christos { 781 1.1 christos 782 1.1 christos return EROFS; 783 1.1 christos } 784 1.1 christos #endif /* !LIBSA_NO_FS_WRITE */ 785 1.1 christos 786 1.1 christos #ifndef LIBSA_NO_FS_SEEK 787 1.1 christos __compactcall off_t 788 1.1 christos minixfs3_seek(struct open_file *f, off_t offset, int where) 789 1.1 christos { 790 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 791 1.1 christos 792 1.1 christos switch (where) { 793 1.1 christos case SEEK_SET: 794 1.1 christos fp->f_seekp = offset; 795 1.1 christos break; 796 1.1 christos case SEEK_CUR: 797 1.1 christos fp->f_seekp += offset; 798 1.1 christos break; 799 1.1 christos case SEEK_END: 800 1.1 christos fp->f_seekp = fp->f_di.mdi_size - offset; 801 1.1 christos break; 802 1.1 christos default: 803 1.1 christos return -1; 804 1.1 christos } 805 1.1 christos return fp->f_seekp; 806 1.1 christos } 807 1.1 christos #endif /* !LIBSA_NO_FS_SEEK */ 808 1.1 christos 809 1.1 christos __compactcall int 810 1.1 christos minixfs3_stat(struct open_file *f, struct stat *sb) 811 1.1 christos { 812 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 813 1.1 christos 814 1.1 christos /* only important stuff */ 815 1.1 christos memset(sb, 0, sizeof *sb); 816 1.1 christos sb->st_mode = fp->f_di.mdi_mode; 817 1.1 christos sb->st_uid = fp->f_di.mdi_uid; 818 1.1 christos sb->st_gid = fp->f_di.mdi_gid; 819 1.1 christos sb->st_size = fp->f_di.mdi_size; 820 1.1 christos return 0; 821 1.1 christos } 822 1.1 christos 823 1.1 christos #if defined(LIBSA_ENABLE_LS_OP) 824 1.7 christos #include "ls.h" 825 1.1 christos __compactcall void 826 1.1 christos minixfs3_ls(struct open_file *f, const char *pattern) 827 1.1 christos { 828 1.1 christos struct file *fp = (struct file *)f->f_fsdata; 829 1.1 christos struct mfs_sblock *fs = fp->f_fs; 830 1.1 christos struct mfs_direct *dp; 831 1.1 christos struct mfs_direct *dbuf; 832 1.1 christos size_t buf_size; 833 1.7 christos lsentry_t *names = 0; 834 1.1 christos 835 1.1 christos fp->f_seekp = 0; 836 1.1 christos while (fp->f_seekp < (off_t)fp->f_di.mdi_size) { 837 1.1 christos int rc = buf_read_file(f, &dbuf, &buf_size); 838 1.1 christos if (rc) 839 1.1 christos goto out; 840 1.1 christos 841 1.1 christos /* XXX we assume, that buf_read_file reads an fs block and 842 1.1 christos * doesn't truncate buffer. Currently i_size in MFS doesn't 843 1.1 christos * the same as size of allocated blocks, it makes buf_read_file 844 1.1 christos * to truncate buf_size. 845 1.1 christos */ 846 1.1 christos if (buf_size < fs->mfs_block_size) 847 1.1 christos buf_size = fs->mfs_block_size; 848 1.1 christos 849 1.1 christos for (dp = dbuf; dp < &dbuf[NR_DIR_ENTRIES(fs)]; dp++) { 850 1.1 christos char *cp; 851 1.1 christos int namlen; 852 1.1 christos 853 1.1 christos if (fs2h32(dp->mfsd_ino) == 0) 854 1.1 christos continue; 855 1.1 christos 856 1.1 christos /* Compute the length of the name, 857 1.1 christos * We don't use strlen and strcpy, because original MFS 858 1.1 christos * code doesn't. 859 1.1 christos */ 860 1.1 christos cp = memchr(dp->mfsd_name, '\0', sizeof(dp->mfsd_name)); 861 1.1 christos if (cp == NULL) 862 1.1 christos namlen = sizeof(dp->mfsd_name); 863 1.1 christos else 864 1.1 christos namlen = cp - (dp->mfsd_name); 865 1.1 christos 866 1.7 christos lsadd(&names, pattern, dp->mfsd_name, namlen, 867 1.7 christos fs2h32(dp->mfsd_ino), "?"); 868 1.1 christos } 869 1.1 christos fp->f_seekp += buf_size; 870 1.1 christos } 871 1.7 christos lsprint(names); 872 1.7 christos out: lsfree(names); 873 1.1 christos } 874 1.1 christos #endif 875 1.1 christos 876 1.1 christos /* 877 1.1 christos * byte swap functions for big endian machines 878 1.1 christos * (mfs is always little endian) 879 1.1 christos */ 880 1.1 christos 881 1.1 christos /* These functions are only needed if native byte order is not big endian */ 882 1.1 christos #if BYTE_ORDER == BIG_ENDIAN 883 1.1 christos void 884 1.1 christos minixfs3_sb_bswap(struct mfs_sblock *old, struct mfs_sblock *new) 885 1.1 christos { 886 1.1 christos new->mfs_ninodes = bswap32(old->mfs_ninodes); 887 1.1 christos new->mfs_nzones = bswap16(old->mfs_nzones); 888 1.1 christos new->mfs_imap_blocks = bswap16(old->mfs_imap_blocks); 889 1.1 christos new->mfs_zmap_blocks = bswap16(old->mfs_zmap_blocks); 890 1.1 christos new->mfs_firstdatazone_old = bswap16(old->mfs_firstdatazone_old); 891 1.1 christos new->mfs_log_zone_size = bswap16(old->mfs_log_zone_size); 892 1.1 christos new->mfs_max_size = bswap32(old->mfs_max_size); 893 1.1 christos new->mfs_zones = bswap32(old->mfs_zones); 894 1.1 christos new->mfs_magic = bswap16(old->mfs_magic); 895 1.1 christos new->mfs_block_size = bswap16(old->mfs_block_size); 896 1.1 christos new->mfs_disk_version = old->mfs_disk_version; 897 1.1 christos } 898 1.1 christos 899 1.1 christos void minixfs3_i_bswap(struct mfs_dinode *old, struct mfs_dinode *new) 900 1.1 christos { 901 1.1 christos int i; 902 1.1 christos 903 1.1 christos new->mdi_mode = bswap16(old->mdi_mode); 904 1.1 christos new->mdi_nlinks = bswap16(old->mdi_nlinks); 905 1.1 christos new->mdi_uid = bswap16(old->mdi_uid); 906 1.1 christos new->mdi_gid = bswap16(old->mdi_gid); 907 1.1 christos new->mdi_size = bswap32(old->mdi_size); 908 1.1 christos new->mdi_atime = bswap32(old->mdi_atime); 909 1.1 christos new->mdi_mtime = bswap32(old->mdi_mtime); 910 1.1 christos new->mdi_ctime = bswap32(old->mdi_ctime); 911 1.1 christos 912 1.1 christos /* We don't swap here, because indirects must be swapped later 913 1.1 christos * anyway, hence everything is done by block_map(). 914 1.1 christos */ 915 1.1 christos for (i = 0; i < NR_TZONES; i++) 916 1.1 christos new->mdi_zone[i] = old->mdi_zone[i]; 917 1.1 christos } 918 1.1 christos #endif 919