1 /* $NetBSD: ufs.c,v 1.30 2000/03/30 12:19:49 augustss Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * The Mach Operating System project at Carnegie-Mellon University. 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * 39 * Copyright (c) 1990, 1991 Carnegie Mellon University 40 * All Rights Reserved. 41 * 42 * Author: David Golub 43 * 44 * Permission to use, copy, modify and distribute this software and its 45 * documentation is hereby granted, provided that both the copyright 46 * notice and this permission notice appear in all copies of the 47 * software, derivative works or modified versions, and any portions 48 * thereof, and that both notices appear in supporting documentation. 49 * 50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 52 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53 * 54 * Carnegie Mellon requests users of this software to return to 55 * 56 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57 * School of Computer Science 58 * Carnegie Mellon University 59 * Pittsburgh PA 15213-3890 60 * 61 * any improvements or extensions that they make and grant Carnegie the 62 * rights to redistribute these changes. 63 */ 64 65 /* 66 * XXX NOTE: ufs.c (FFS) and lfs.c (LFS) should eventually use much common 67 * XXX code. until then, the two files should be easily diffable. 68 */ 69 70 /* 71 * Stand-alone file reading package. 72 */ 73 74 #include <sys/param.h> 75 #include <sys/time.h> 76 #include <ufs/ufs/dinode.h> 77 #include <ufs/ufs/dir.h> 78 #include <ufs/ffs/fs.h> 79 #ifdef _STANDALONE 80 #include <lib/libkern/libkern.h> 81 #else 82 #include <string.h> 83 inline u_int 84 max(a, b) 85 u_int a, b; 86 { 87 return (a > b ? a : b); 88 } 89 #endif 90 91 #include "stand.h" 92 #include "ufs.h" 93 94 #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK) 95 #define LIBSA_NO_FS_SYMLINK 96 #endif 97 #if defined(COMPAT_UFS) && defined(LIBSA_NO_COMPAT_UFS) 98 #undef COMPAT_UFS 99 #endif 100 101 102 /* 103 * In-core open file. 104 */ 105 struct file { 106 off_t f_seekp; /* seek pointer */ 107 struct fs *f_fs; /* pointer to super-block */ 108 struct dinode f_di; /* copy of on-disk inode */ 109 unsigned int f_nindir[NIADDR]; 110 /* number of blocks mapped by 111 indirect block at level i */ 112 char *f_blk[NIADDR]; /* buffer for indirect block at 113 level i */ 114 size_t f_blksize[NIADDR]; 115 /* size of buffer */ 116 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */ 117 char *f_buf; /* buffer for data block */ 118 size_t f_buf_size; /* size of data block */ 119 daddr_t f_buf_blkno; /* block number of data block */ 120 }; 121 122 static int read_inode __P((ino_t, struct open_file *)); 123 static int block_map __P((struct open_file *, daddr_t, daddr_t *)); 124 static int buf_read_file __P((struct open_file *, char **, size_t *)); 125 static int search_directory __P((char *, struct open_file *, ino_t *)); 126 #ifdef COMPAT_UFS 127 static void ffs_oldfscompat __P((struct fs *)); 128 #endif 129 130 /* 131 * Read a new inode into a file structure. 132 */ 133 static int 134 read_inode(inumber, f) 135 ino_t inumber; 136 struct open_file *f; 137 { 138 struct file *fp = (struct file *)f->f_fsdata; 139 struct fs *fs = fp->f_fs; 140 char *buf; 141 size_t rsize; 142 int rc; 143 144 /* 145 * Read inode and save it. 146 */ 147 buf = alloc(fs->fs_bsize); 148 #if !defined(LIBSA_NO_TWIDDLE) 149 twiddle(); 150 #endif 151 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 152 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize, 153 buf, &rsize); 154 if (rc) 155 goto out; 156 if (rsize != fs->fs_bsize) { 157 rc = EIO; 158 goto out; 159 } 160 161 { 162 struct dinode *dp; 163 164 dp = (struct dinode *)buf; 165 fp->f_di = dp[ino_to_fsbo(fs, inumber)]; 166 } 167 168 /* 169 * Clear out the old buffers 170 */ 171 { 172 int level; 173 174 for (level = 0; level < NIADDR; level++) 175 fp->f_blkno[level] = -1; 176 fp->f_buf_blkno = -1; 177 } 178 out: 179 free(buf, fs->fs_bsize); 180 return (rc); 181 } 182 183 /* 184 * Given an offset in a file, find the disk block number that 185 * contains that block. 186 */ 187 static int 188 block_map(f, file_block, disk_block_p) 189 struct open_file *f; 190 daddr_t file_block; 191 daddr_t *disk_block_p; /* out */ 192 { 193 struct file *fp = (struct file *)f->f_fsdata; 194 struct fs *fs = fp->f_fs; 195 int level; 196 int idx; 197 daddr_t ind_block_num; 198 daddr_t *ind_p; 199 int rc; 200 201 /* 202 * Index structure of an inode: 203 * 204 * di_db[0..NDADDR-1] hold block numbers for blocks 205 * 0..NDADDR-1 206 * 207 * di_ib[0] index block 0 is the single indirect block 208 * holds block numbers for blocks 209 * NDADDR .. NDADDR + NINDIR(fs)-1 210 * 211 * di_ib[1] index block 1 is the double indirect block 212 * holds block numbers for INDEX blocks for blocks 213 * NDADDR + NINDIR(fs) .. 214 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1 215 * 216 * di_ib[2] index block 2 is the triple indirect block 217 * holds block numbers for double-indirect 218 * blocks for blocks 219 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 .. 220 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 221 * + NINDIR(fs)**3 - 1 222 */ 223 224 if (file_block < NDADDR) { 225 /* Direct block. */ 226 *disk_block_p = fp->f_di.di_db[file_block]; 227 return (0); 228 } 229 230 file_block -= NDADDR; 231 232 /* 233 * nindir[0] = NINDIR 234 * nindir[1] = NINDIR**2 235 * nindir[2] = NINDIR**3 236 * etc 237 */ 238 for (level = 0; level < NIADDR; level++) { 239 if (file_block < fp->f_nindir[level]) 240 break; 241 file_block -= fp->f_nindir[level]; 242 } 243 if (level == NIADDR) { 244 /* Block number too high */ 245 return (EFBIG); 246 } 247 248 ind_block_num = fp->f_di.di_ib[level]; 249 250 for (; level >= 0; level--) { 251 if (ind_block_num == 0) { 252 *disk_block_p = 0; /* missing */ 253 return (0); 254 } 255 256 if (fp->f_blkno[level] != ind_block_num) { 257 if (fp->f_blk[level] == (char *)0) 258 fp->f_blk[level] = 259 alloc(fs->fs_bsize); 260 #if !defined(LIBSA_NO_TWIDDLE) 261 twiddle(); 262 #endif 263 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 264 fsbtodb(fp->f_fs, ind_block_num), 265 fs->fs_bsize, 266 fp->f_blk[level], 267 &fp->f_blksize[level]); 268 if (rc) 269 return (rc); 270 if (fp->f_blksize[level] != fs->fs_bsize) 271 return (EIO); 272 fp->f_blkno[level] = ind_block_num; 273 } 274 275 ind_p = (daddr_t *)fp->f_blk[level]; 276 277 if (level > 0) { 278 idx = file_block / fp->f_nindir[level - 1]; 279 file_block %= fp->f_nindir[level - 1]; 280 } else 281 idx = file_block; 282 283 ind_block_num = ind_p[idx]; 284 } 285 286 *disk_block_p = ind_block_num; 287 288 return (0); 289 } 290 291 /* 292 * Read a portion of a file into an internal buffer. Return 293 * the location in the buffer and the amount in the buffer. 294 */ 295 static int 296 buf_read_file(f, buf_p, size_p) 297 struct open_file *f; 298 char **buf_p; /* out */ 299 size_t *size_p; /* out */ 300 { 301 struct file *fp = (struct file *)f->f_fsdata; 302 struct fs *fs = fp->f_fs; 303 long off; 304 daddr_t file_block; 305 daddr_t disk_block; 306 size_t block_size; 307 int rc; 308 309 off = blkoff(fs, fp->f_seekp); 310 file_block = lblkno(fs, fp->f_seekp); 311 block_size = dblksize(fs, &fp->f_di, file_block); 312 313 if (file_block != fp->f_buf_blkno) { 314 rc = block_map(f, file_block, &disk_block); 315 if (rc) 316 return (rc); 317 318 if (fp->f_buf == (char *)0) 319 fp->f_buf = alloc(fs->fs_bsize); 320 321 if (disk_block == 0) { 322 bzero(fp->f_buf, block_size); 323 fp->f_buf_size = block_size; 324 } else { 325 #if !defined(LIBSA_NO_TWIDDLE) 326 twiddle(); 327 #endif 328 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 329 fsbtodb(fs, disk_block), 330 block_size, fp->f_buf, &fp->f_buf_size); 331 if (rc) 332 return (rc); 333 } 334 335 fp->f_buf_blkno = file_block; 336 } 337 338 /* 339 * Return address of byte in buffer corresponding to 340 * offset, and size of remainder of buffer after that 341 * byte. 342 */ 343 *buf_p = fp->f_buf + off; 344 *size_p = block_size - off; 345 346 /* 347 * But truncate buffer at end of file. 348 */ 349 if (*size_p > fp->f_di.di_size - fp->f_seekp) 350 *size_p = fp->f_di.di_size - fp->f_seekp; 351 352 return (0); 353 } 354 355 /* 356 * Search a directory for a name and return its 357 * i_number. 358 */ 359 static int 360 search_directory(name, f, inumber_p) 361 char *name; 362 struct open_file *f; 363 ino_t *inumber_p; /* out */ 364 { 365 struct file *fp = (struct file *)f->f_fsdata; 366 struct direct *dp; 367 struct direct *edp; 368 char *buf; 369 size_t buf_size; 370 int namlen, length; 371 int rc; 372 373 length = strlen(name); 374 375 fp->f_seekp = 0; 376 while (fp->f_seekp < fp->f_di.di_size) { 377 rc = buf_read_file(f, &buf, &buf_size); 378 if (rc) 379 return (rc); 380 381 dp = (struct direct *)buf; 382 edp = (struct direct *)(buf + buf_size); 383 while (dp < edp) { 384 if (dp->d_ino == (ino_t)0) 385 goto next; 386 #if BYTE_ORDER == LITTLE_ENDIAN 387 if (fp->f_fs->fs_maxsymlinklen <= 0) 388 namlen = dp->d_type; 389 else 390 #endif 391 namlen = dp->d_namlen; 392 if (namlen == length && 393 !strcmp(name, dp->d_name)) { 394 /* found entry */ 395 *inumber_p = dp->d_ino; 396 return (0); 397 } 398 next: 399 dp = (struct direct *)((char *)dp + dp->d_reclen); 400 } 401 fp->f_seekp += buf_size; 402 } 403 return (ENOENT); 404 } 405 406 /* 407 * Open a file. 408 */ 409 int 410 ufs_open(path, f) 411 char *path; 412 struct open_file *f; 413 { 414 #ifndef LIBSA_FS_SINGLECOMPONENT 415 char *cp, *ncp; 416 int c; 417 #endif 418 ino_t inumber; 419 struct file *fp; 420 struct fs *fs; 421 int rc; 422 size_t buf_size; 423 #ifndef LIBSA_NO_FS_SYMLINK 424 ino_t parent_inumber; 425 int nlinks = 0; 426 char namebuf[MAXPATHLEN+1]; 427 char *buf = NULL; 428 #endif 429 430 /* allocate file system specific data structure */ 431 fp = alloc(sizeof(struct file)); 432 bzero(fp, sizeof(struct file)); 433 f->f_fsdata = (void *)fp; 434 435 /* allocate space and read super block */ 436 fs = alloc(SBSIZE); 437 fp->f_fs = fs; 438 #if !defined(LIBSA_NO_TWIDDLE) 439 twiddle(); 440 #endif 441 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ, 442 SBLOCK, SBSIZE, (char *)fs, &buf_size); 443 if (rc) 444 goto out; 445 446 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC || 447 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) { 448 rc = EINVAL; 449 goto out; 450 } 451 #ifdef COMPAT_UFS 452 ffs_oldfscompat(fs); 453 #endif 454 455 /* 456 * Calculate indirect block levels. 457 */ 458 { 459 int mult; 460 int level; 461 462 mult = 1; 463 for (level = 0; level < NIADDR; level++) { 464 mult *= NINDIR(fs); 465 fp->f_nindir[level] = mult; 466 } 467 } 468 469 inumber = ROOTINO; 470 if ((rc = read_inode(inumber, f)) != 0) 471 goto out; 472 473 #ifndef LIBSA_FS_SINGLECOMPONENT 474 cp = path; 475 while (*cp) { 476 477 /* 478 * Remove extra separators 479 */ 480 while (*cp == '/') 481 cp++; 482 if (*cp == '\0') 483 break; 484 485 /* 486 * Check that current node is a directory. 487 */ 488 if ((fp->f_di.di_mode & IFMT) != IFDIR) { 489 rc = ENOTDIR; 490 goto out; 491 } 492 493 /* 494 * Get next component of path name. 495 */ 496 { 497 int len = 0; 498 499 ncp = cp; 500 while ((c = *cp) != '\0' && c != '/') { 501 if (++len > MAXNAMLEN) { 502 rc = ENOENT; 503 goto out; 504 } 505 cp++; 506 } 507 *cp = '\0'; 508 } 509 510 /* 511 * Look up component in current directory. 512 * Save directory inumber in case we find a 513 * symbolic link. 514 */ 515 #ifndef LIBSA_NO_FS_SYMLINK 516 parent_inumber = inumber; 517 #endif 518 rc = search_directory(ncp, f, &inumber); 519 *cp = c; 520 if (rc) 521 goto out; 522 523 /* 524 * Open next component. 525 */ 526 if ((rc = read_inode(inumber, f)) != 0) 527 goto out; 528 529 #ifndef LIBSA_NO_FS_SYMLINK 530 /* 531 * Check for symbolic link. 532 */ 533 if ((fp->f_di.di_mode & IFMT) == IFLNK) { 534 int link_len = fp->f_di.di_size; 535 int len; 536 537 len = strlen(cp); 538 539 if (link_len + len > MAXPATHLEN || 540 ++nlinks > MAXSYMLINKS) { 541 rc = ENOENT; 542 goto out; 543 } 544 545 bcopy(cp, &namebuf[link_len], len + 1); 546 547 if (link_len < fs->fs_maxsymlinklen) { 548 bcopy(fp->f_di.di_shortlink, namebuf, 549 (unsigned) link_len); 550 } else { 551 /* 552 * Read file for symbolic link 553 */ 554 size_t buf_size; 555 daddr_t disk_block; 556 struct fs *fs = fp->f_fs; 557 558 if (!buf) 559 buf = alloc(fs->fs_bsize); 560 rc = block_map(f, (daddr_t)0, &disk_block); 561 if (rc) 562 goto out; 563 564 #if !defined(LIBSA_NO_TWIDDLE) 565 twiddle(); 566 #endif 567 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, 568 F_READ, fsbtodb(fs, disk_block), 569 fs->fs_bsize, buf, &buf_size); 570 if (rc) 571 goto out; 572 573 bcopy((char *)buf, namebuf, (unsigned)link_len); 574 } 575 576 /* 577 * If relative pathname, restart at parent directory. 578 * If absolute pathname, restart at root. 579 */ 580 cp = namebuf; 581 if (*cp != '/') 582 inumber = parent_inumber; 583 else 584 inumber = (ino_t)ROOTINO; 585 586 if ((rc = read_inode(inumber, f)) != 0) 587 goto out; 588 } 589 #endif /* !LIBSA_NO_FS_SYMLINK */ 590 } 591 592 /* 593 * Found terminal component. 594 */ 595 rc = 0; 596 597 #else /* !LIBSA_FS_SINGLECOMPONENT */ 598 599 /* look up component in the current (root) directory */ 600 rc = search_directory(path, f, &inumber); 601 if (rc) 602 goto out; 603 604 /* open it */ 605 rc = read_inode(inumber, f); 606 607 #endif /* !LIBSA_FS_SINGLECOMPONENT */ 608 609 fp->f_seekp = 0; /* reset seek pointer */ 610 611 out: 612 #ifndef LIBSA_NO_FS_SYMLINK 613 if (buf) 614 free(buf, fs->fs_bsize); 615 #endif 616 if (rc) { 617 if (fp->f_buf) 618 free(fp->f_buf, fp->f_fs->fs_bsize); 619 free(fp->f_fs, SBSIZE); 620 free(fp, sizeof(struct file)); 621 } 622 return (rc); 623 } 624 625 #ifndef LIBSA_NO_FS_CLOSE 626 int 627 ufs_close(f) 628 struct open_file *f; 629 { 630 struct file *fp = (struct file *)f->f_fsdata; 631 int level; 632 633 f->f_fsdata = (void *)0; 634 if (fp == (struct file *)0) 635 return (0); 636 637 for (level = 0; level < NIADDR; level++) { 638 if (fp->f_blk[level]) 639 free(fp->f_blk[level], fp->f_fs->fs_bsize); 640 } 641 if (fp->f_buf) 642 free(fp->f_buf, fp->f_fs->fs_bsize); 643 free(fp->f_fs, SBSIZE); 644 free(fp, sizeof(struct file)); 645 return (0); 646 } 647 #endif /* !LIBSA_NO_FS_CLOSE */ 648 649 /* 650 * Copy a portion of a file into kernel memory. 651 * Cross block boundaries when necessary. 652 */ 653 int 654 ufs_read(f, start, size, resid) 655 struct open_file *f; 656 void *start; 657 size_t size; 658 size_t *resid; /* out */ 659 { 660 struct file *fp = (struct file *)f->f_fsdata; 661 size_t csize; 662 char *buf; 663 size_t buf_size; 664 int rc = 0; 665 char *addr = start; 666 667 while (size != 0) { 668 if (fp->f_seekp >= fp->f_di.di_size) 669 break; 670 671 rc = buf_read_file(f, &buf, &buf_size); 672 if (rc) 673 break; 674 675 csize = size; 676 if (csize > buf_size) 677 csize = buf_size; 678 679 bcopy(buf, addr, csize); 680 681 fp->f_seekp += csize; 682 addr += csize; 683 size -= csize; 684 } 685 if (resid) 686 *resid = size; 687 return (rc); 688 } 689 690 /* 691 * Not implemented. 692 */ 693 #ifndef LIBSA_NO_FS_WRITE 694 int 695 ufs_write(f, start, size, resid) 696 struct open_file *f; 697 void *start; 698 size_t size; 699 size_t *resid; /* out */ 700 { 701 702 return (EROFS); 703 } 704 #endif /* !LIBSA_NO_FS_WRITE */ 705 706 #ifndef LIBSA_NO_FS_SEEK 707 off_t 708 ufs_seek(f, offset, where) 709 struct open_file *f; 710 off_t offset; 711 int where; 712 { 713 struct file *fp = (struct file *)f->f_fsdata; 714 715 switch (where) { 716 case SEEK_SET: 717 fp->f_seekp = offset; 718 break; 719 case SEEK_CUR: 720 fp->f_seekp += offset; 721 break; 722 case SEEK_END: 723 fp->f_seekp = fp->f_di.di_size - offset; 724 break; 725 default: 726 return (-1); 727 } 728 return (fp->f_seekp); 729 } 730 #endif /* !LIBSA_NO_FS_SEEK */ 731 732 int 733 ufs_stat(f, sb) 734 struct open_file *f; 735 struct stat *sb; 736 { 737 struct file *fp = (struct file *)f->f_fsdata; 738 739 /* only important stuff */ 740 sb->st_mode = fp->f_di.di_mode; 741 sb->st_uid = fp->f_di.di_uid; 742 sb->st_gid = fp->f_di.di_gid; 743 sb->st_size = fp->f_di.di_size; 744 return (0); 745 } 746 747 #ifdef COMPAT_UFS 748 /* 749 * Sanity checks for old file systems. 750 * 751 * XXX - goes away some day. 752 */ 753 static void 754 ffs_oldfscompat(fs) 755 struct fs *fs; 756 { 757 int i; 758 759 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */ 760 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */ 761 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */ 762 fs->fs_nrpos = 8; /* XXX */ 763 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */ 764 quad_t sizepb = fs->fs_bsize; /* XXX */ 765 /* XXX */ 766 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */ 767 for (i = 0; i < NIADDR; i++) { /* XXX */ 768 sizepb *= NINDIR(fs); /* XXX */ 769 fs->fs_maxfilesize += sizepb; /* XXX */ 770 } /* XXX */ 771 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */ 772 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */ 773 } /* XXX */ 774 } 775 #endif 776