1 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 Networks Associates Technology, Inc. 5 * All rights reserved. 6 * 7 * This software was developed for the FreeBSD Project by Marshall 8 * Kirk McKusick and Network Associates Laboratories, the Security 9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR 10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS 11 * research program 12 * 13 * Copyright (c) 1982, 1989, 1993 14 * The Regents of the University of California. All rights reserved. 15 * 16 * This code is derived from software contributed to Berkeley by 17 * The Mach Operating System project at Carnegie-Mellon University. 18 * 19 * Redistribution and use in source and binary forms, with or without 20 * modification, are permitted provided that the following conditions 21 * are met: 22 * 1. Redistributions of source code must retain the above copyright 23 * notice, this list of conditions and the following disclaimer. 24 * 2. Redistributions in binary form must reproduce the above copyright 25 * notice, this list of conditions and the following disclaimer in the 26 * documentation and/or other materials provided with the distribution. 27 * 3. Neither the name of the University nor the names of its contributors 28 * may be used to endorse or promote products derived from this software 29 * without specific prior written permission. 30 * 31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 41 * SUCH DAMAGE. 42 * 43 * 44 * Copyright (c) 1990, 1991 Carnegie Mellon University 45 * All Rights Reserved. 46 * 47 * Author: David Golub 48 * 49 * Permission to use, copy, modify and distribute this software and its 50 * documentation is hereby granted, provided that both the copyright 51 * notice and this permission notice appear in all copies of the 52 * software, derivative works or modified versions, and any portions 53 * thereof, and that both notices appear in supporting documentation. 54 * 55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR 57 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58 * 59 * Carnegie Mellon requests users of this software to return to 60 * 61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62 * School of Computer Science 63 * Carnegie Mellon University 64 * Pittsburgh PA 15213-3890 65 * 66 * any improvements or extensions that they make and grant Carnegie the 67 * rights to redistribute these changes. 68 */ 69 70 #include <sys/cdefs.h> 71 __FBSDID("$FreeBSD$"); 72 73 /* 74 * Stand-alone file reading package. 75 */ 76 77 #include <sys/param.h> 78 #include <sys/disklabel.h> 79 #include <sys/time.h> 80 #include <ufs/ufs/dinode.h> 81 #include <ufs/ufs/dir.h> 82 #include <ufs/ffs/fs.h> 83 #include "stand.h" 84 #include "disk.h" 85 #include "string.h" 86 87 static int ufs_open(const char *path, struct open_file *f); 88 static int ufs_write(struct open_file *f, const void *buf, size_t size, 89 size_t *resid); 90 static int ufs_close(struct open_file *f); 91 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid); 92 static off_t ufs_seek(struct open_file *f, off_t offset, int where); 93 static int ufs_stat(struct open_file *f, struct stat *sb); 94 static int ufs_readdir(struct open_file *f, struct dirent *d); 95 static int ufs_mount(const char *dev, const char *path, void **data); 96 static int ufs_unmount(const char *dev, void *data); 97 98 struct fs_ops ufs_fsops = { 99 .fs_name = "ufs", 100 .fo_open = ufs_open, 101 .fo_close = ufs_close, 102 .fo_read = ufs_read, 103 .fo_write = ufs_write, 104 .fo_seek = ufs_seek, 105 .fo_stat = ufs_stat, 106 .fo_readdir = ufs_readdir, 107 .fo_mount = ufs_mount, 108 .fo_unmount = ufs_unmount 109 }; 110 111 /* 112 * In-core open file. 113 */ 114 struct file { 115 off_t f_seekp; /* seek pointer */ 116 struct fs *f_fs; /* pointer to super-block */ 117 union dinode { 118 struct ufs1_dinode di1; 119 struct ufs2_dinode di2; 120 } f_di; /* copy of on-disk inode */ 121 int f_nindir[UFS_NIADDR]; 122 /* number of blocks mapped by 123 indirect block at level i */ 124 char *f_blk[UFS_NIADDR]; /* buffer for indirect block at 125 level i */ 126 size_t f_blksize[UFS_NIADDR]; 127 /* size of buffer */ 128 ufs2_daddr_t f_blkno[UFS_NIADDR];/* disk address of block in buffer */ 129 ufs2_daddr_t f_buf_blkno; /* block number of data block */ 130 char *f_buf; /* buffer for data block */ 131 size_t f_buf_size; /* size of data block */ 132 int f_inumber; /* inumber */ 133 }; 134 #define DIP(fp, field) \ 135 ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \ 136 (fp)->f_di.di1.field : (fp)->f_di.di2.field) 137 138 typedef struct ufs_mnt { 139 char *um_dev; 140 int um_fd; 141 STAILQ_ENTRY(ufs_mnt) um_link; 142 } ufs_mnt_t; 143 144 typedef STAILQ_HEAD(ufs_mnt_list, ufs_mnt) ufs_mnt_list_t; 145 static ufs_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list); 146 147 static int read_inode(ino_t, struct open_file *); 148 static int block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *); 149 static int buf_read_file(struct open_file *, char **, size_t *); 150 static int buf_write_file(struct open_file *, const char *, size_t *); 151 static int search_directory(char *, struct open_file *, ino_t *); 152 static int ufs_use_sa_read(void *, off_t, void **, int); 153 154 /* from ffs_subr.c */ 155 int ffs_sbget(void *, struct fs **, off_t, char *, 156 int (*)(void *, off_t, void **, int)); 157 158 /* 159 * Read a new inode into a file structure. 160 */ 161 static int 162 read_inode(ino_t inumber, struct open_file *f) 163 { 164 struct file *fp = (struct file *)f->f_fsdata; 165 struct fs *fs = fp->f_fs; 166 char *buf; 167 size_t rsize; 168 int rc; 169 170 if (fs == NULL) 171 panic("fs == NULL"); 172 173 /* 174 * Read inode and save it. 175 */ 176 buf = malloc(fs->fs_bsize); 177 twiddle(1); 178 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 179 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize, 180 buf, &rsize); 181 if (rc) 182 goto out; 183 if (rsize != fs->fs_bsize) { 184 rc = EIO; 185 goto out; 186 } 187 188 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC) 189 fp->f_di.di1 = ((struct ufs1_dinode *)buf) 190 [ino_to_fsbo(fs, inumber)]; 191 else 192 fp->f_di.di2 = ((struct ufs2_dinode *)buf) 193 [ino_to_fsbo(fs, inumber)]; 194 195 /* 196 * Clear out the old buffers 197 */ 198 { 199 int level; 200 201 for (level = 0; level < UFS_NIADDR; level++) 202 fp->f_blkno[level] = -1; 203 fp->f_buf_blkno = -1; 204 } 205 fp->f_seekp = 0; 206 fp->f_inumber = inumber; 207 out: 208 free(buf); 209 return (rc); 210 } 211 212 /* 213 * Given an offset in a file, find the disk block number that 214 * contains that block. 215 */ 216 static int 217 block_map(struct open_file *f, ufs2_daddr_t file_block, 218 ufs2_daddr_t *disk_block_p) 219 { 220 struct file *fp = (struct file *)f->f_fsdata; 221 struct fs *fs = fp->f_fs; 222 int level; 223 int idx; 224 ufs2_daddr_t ind_block_num; 225 int rc; 226 227 /* 228 * Index structure of an inode: 229 * 230 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks 231 * 0..UFS_NDADDR-1 232 * 233 * di_ib[0] index block 0 is the single indirect block 234 * holds block numbers for blocks 235 * UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1 236 * 237 * di_ib[1] index block 1 is the double indirect block 238 * holds block numbers for INDEX blocks for blocks 239 * UFS_NDADDR + NINDIR(fs) .. 240 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1 241 * 242 * di_ib[2] index block 2 is the triple indirect block 243 * holds block numbers for double-indirect 244 * blocks for blocks 245 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 .. 246 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 247 * + NINDIR(fs)**3 - 1 248 */ 249 250 if (file_block < UFS_NDADDR) { 251 /* Direct block. */ 252 *disk_block_p = DIP(fp, di_db[file_block]); 253 return (0); 254 } 255 256 file_block -= UFS_NDADDR; 257 258 /* 259 * nindir[0] = NINDIR 260 * nindir[1] = NINDIR**2 261 * nindir[2] = NINDIR**3 262 * etc 263 */ 264 for (level = 0; level < UFS_NIADDR; level++) { 265 if (file_block < fp->f_nindir[level]) 266 break; 267 file_block -= fp->f_nindir[level]; 268 } 269 if (level == UFS_NIADDR) { 270 /* Block number too high */ 271 return (EFBIG); 272 } 273 274 ind_block_num = DIP(fp, di_ib[level]); 275 276 for (; level >= 0; level--) { 277 if (ind_block_num == 0) { 278 *disk_block_p = 0; /* missing */ 279 return (0); 280 } 281 282 if (fp->f_blkno[level] != ind_block_num) { 283 if (fp->f_blk[level] == (char *)0) 284 fp->f_blk[level] = 285 malloc(fs->fs_bsize); 286 twiddle(1); 287 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 288 fsbtodb(fp->f_fs, ind_block_num), 289 fs->fs_bsize, 290 fp->f_blk[level], 291 &fp->f_blksize[level]); 292 if (rc) 293 return (rc); 294 if (fp->f_blksize[level] != fs->fs_bsize) 295 return (EIO); 296 fp->f_blkno[level] = ind_block_num; 297 } 298 299 if (level > 0) { 300 idx = file_block / fp->f_nindir[level - 1]; 301 file_block %= fp->f_nindir[level - 1]; 302 } else 303 idx = file_block; 304 305 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC) 306 ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx]; 307 else 308 ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx]; 309 } 310 311 *disk_block_p = ind_block_num; 312 313 return (0); 314 } 315 316 /* 317 * Write a portion of a file from an internal buffer. 318 */ 319 static int 320 buf_write_file(struct open_file *f, const char *buf_p, size_t *size_p) 321 { 322 struct file *fp = (struct file *)f->f_fsdata; 323 struct fs *fs = fp->f_fs; 324 long off; 325 ufs_lbn_t file_block; 326 ufs2_daddr_t disk_block; 327 size_t block_size; 328 int rc; 329 330 /* 331 * Calculate the starting block address and offset. 332 */ 333 off = blkoff(fs, fp->f_seekp); 334 file_block = lblkno(fs, fp->f_seekp); 335 block_size = sblksize(fs, DIP(fp, di_size), file_block); 336 337 rc = block_map(f, file_block, &disk_block); 338 if (rc) 339 return (rc); 340 341 if (disk_block == 0) 342 /* Because we can't allocate space on the drive */ 343 return (EFBIG); 344 345 /* 346 * Truncate buffer at end of file, and at the end of 347 * this block. 348 */ 349 if (*size_p > DIP(fp, di_size) - fp->f_seekp) 350 *size_p = DIP(fp, di_size) - fp->f_seekp; 351 if (*size_p > block_size - off) 352 *size_p = block_size - off; 353 354 /* 355 * If we don't entirely occlude the block and it's not 356 * in memory already, read it in first. 357 */ 358 if (((off > 0) || (*size_p + off < block_size)) && 359 (file_block != fp->f_buf_blkno)) { 360 361 if (fp->f_buf == (char *)0) 362 fp->f_buf = malloc(fs->fs_bsize); 363 364 twiddle(4); 365 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 366 fsbtodb(fs, disk_block), 367 block_size, fp->f_buf, &fp->f_buf_size); 368 if (rc) 369 return (rc); 370 371 fp->f_buf_blkno = file_block; 372 } 373 374 /* 375 * Copy the user data into the cached block. 376 */ 377 bcopy(buf_p, fp->f_buf + off, *size_p); 378 379 /* 380 * Write the block out to storage. 381 */ 382 383 twiddle(4); 384 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE, 385 fsbtodb(fs, disk_block), 386 block_size, fp->f_buf, &fp->f_buf_size); 387 return (rc); 388 } 389 390 /* 391 * Read a portion of a file into an internal buffer. Return 392 * the location in the buffer and the amount in the buffer. 393 */ 394 static int 395 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p) 396 { 397 struct file *fp = (struct file *)f->f_fsdata; 398 struct fs *fs = fp->f_fs; 399 long off; 400 ufs_lbn_t file_block; 401 ufs2_daddr_t disk_block; 402 size_t block_size; 403 int rc; 404 405 off = blkoff(fs, fp->f_seekp); 406 file_block = lblkno(fs, fp->f_seekp); 407 block_size = sblksize(fs, DIP(fp, di_size), file_block); 408 409 if (file_block != fp->f_buf_blkno) { 410 if (fp->f_buf == (char *)0) 411 fp->f_buf = malloc(fs->fs_bsize); 412 413 rc = block_map(f, file_block, &disk_block); 414 if (rc) 415 return (rc); 416 417 if (disk_block == 0) { 418 bzero(fp->f_buf, block_size); 419 fp->f_buf_size = block_size; 420 } else { 421 twiddle(4); 422 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, 423 fsbtodb(fs, disk_block), 424 block_size, fp->f_buf, &fp->f_buf_size); 425 if (rc) 426 return (rc); 427 } 428 429 fp->f_buf_blkno = file_block; 430 } 431 432 /* 433 * Return address of byte in buffer corresponding to 434 * offset, and size of remainder of buffer after that 435 * byte. 436 */ 437 *buf_p = fp->f_buf + off; 438 *size_p = block_size - off; 439 440 /* 441 * But truncate buffer at end of file. 442 */ 443 if (*size_p > DIP(fp, di_size) - fp->f_seekp) 444 *size_p = DIP(fp, di_size) - fp->f_seekp; 445 446 return (0); 447 } 448 449 /* 450 * Search a directory for a name and return its 451 * i_number. 452 */ 453 static int 454 search_directory(char *name, struct open_file *f, ino_t *inumber_p) 455 { 456 struct file *fp = (struct file *)f->f_fsdata; 457 struct direct *dp; 458 struct direct *edp; 459 char *buf; 460 size_t buf_size; 461 int namlen, length; 462 int rc; 463 464 length = strlen(name); 465 466 fp->f_seekp = 0; 467 while (fp->f_seekp < DIP(fp, di_size)) { 468 rc = buf_read_file(f, &buf, &buf_size); 469 if (rc) 470 return (rc); 471 472 dp = (struct direct *)buf; 473 edp = (struct direct *)(buf + buf_size); 474 while (dp < edp) { 475 if (dp->d_ino == (ino_t)0) 476 goto next; 477 #if BYTE_ORDER == LITTLE_ENDIAN 478 if (fp->f_fs->fs_maxsymlinklen <= 0) 479 namlen = dp->d_type; 480 else 481 #endif 482 namlen = dp->d_namlen; 483 if (namlen == length && 484 !strcmp(name, dp->d_name)) { 485 /* found entry */ 486 *inumber_p = dp->d_ino; 487 return (0); 488 } 489 next: 490 dp = (struct direct *)((char *)dp + dp->d_reclen); 491 } 492 fp->f_seekp += buf_size; 493 } 494 return (ENOENT); 495 } 496 497 /* 498 * Open a file. 499 */ 500 static int 501 ufs_open(const char *upath, struct open_file *f) 502 { 503 char *cp, *ncp; 504 int c; 505 ino_t inumber, parent_inumber; 506 struct file *fp; 507 struct fs *fs; 508 int rc; 509 int nlinks = 0; 510 char namebuf[MAXPATHLEN+1]; 511 char *buf = NULL; 512 char *path = NULL; 513 const char *dev; 514 ufs_mnt_t *mnt; 515 516 /* allocate file system specific data structure */ 517 errno = 0; 518 fp = calloc(1, sizeof(struct file)); 519 if (fp == NULL) 520 return (errno); 521 f->f_fsdata = (void *)fp; 522 523 dev = disk_fmtdev(f->f_devdata); 524 /* Is this device mounted? */ 525 STAILQ_FOREACH(mnt, &mnt_list, um_link) { 526 if (strcmp(dev, mnt->um_dev) == 0) 527 break; 528 } 529 530 if (mnt == NULL) { 531 /* read super block */ 532 twiddle(1); 533 if ((rc = ffs_sbget(f, &fs, STDSB_NOHASHFAIL, "stand", 534 ufs_use_sa_read)) != 0) { 535 goto out; 536 } 537 } else { 538 struct open_file *sbf; 539 struct file *sfp; 540 541 /* get superblock from mounted file system */ 542 sbf = fd2open_file(mnt->um_fd); 543 sfp = sbf->f_fsdata; 544 fs = sfp->f_fs; 545 } 546 fp->f_fs = fs; 547 548 /* 549 * Calculate indirect block levels. 550 */ 551 { 552 ufs2_daddr_t mult; 553 int level; 554 555 mult = 1; 556 for (level = 0; level < UFS_NIADDR; level++) { 557 mult *= NINDIR(fs); 558 fp->f_nindir[level] = mult; 559 } 560 } 561 562 inumber = UFS_ROOTINO; 563 if ((rc = read_inode(inumber, f)) != 0) 564 goto out; 565 566 cp = path = strdup(upath); 567 if (path == NULL) { 568 rc = ENOMEM; 569 goto out; 570 } 571 while (*cp) { 572 573 /* 574 * Remove extra separators 575 */ 576 while (*cp == '/') 577 cp++; 578 if (*cp == '\0') 579 break; 580 581 /* 582 * Check that current node is a directory. 583 */ 584 if ((DIP(fp, di_mode) & IFMT) != IFDIR) { 585 rc = ENOTDIR; 586 goto out; 587 } 588 589 /* 590 * Get next component of path name. 591 */ 592 { 593 int len = 0; 594 595 ncp = cp; 596 while ((c = *cp) != '\0' && c != '/') { 597 if (++len > UFS_MAXNAMLEN) { 598 rc = ENOENT; 599 goto out; 600 } 601 cp++; 602 } 603 *cp = '\0'; 604 } 605 606 /* 607 * Look up component in current directory. 608 * Save directory inumber in case we find a 609 * symbolic link. 610 */ 611 parent_inumber = inumber; 612 rc = search_directory(ncp, f, &inumber); 613 *cp = c; 614 if (rc) 615 goto out; 616 617 /* 618 * Open next component. 619 */ 620 if ((rc = read_inode(inumber, f)) != 0) 621 goto out; 622 623 /* 624 * Check for symbolic link. 625 */ 626 if ((DIP(fp, di_mode) & IFMT) == IFLNK) { 627 int link_len = DIP(fp, di_size); 628 int len; 629 630 len = strlen(cp); 631 632 if (link_len + len > MAXPATHLEN || 633 ++nlinks > MAXSYMLINKS) { 634 rc = ENOENT; 635 goto out; 636 } 637 638 bcopy(cp, &namebuf[link_len], len + 1); 639 640 if (link_len < fs->fs_maxsymlinklen) { 641 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC) 642 cp = (caddr_t)(fp->f_di.di1.di_db); 643 else 644 cp = (caddr_t)(fp->f_di.di2.di_db); 645 bcopy(cp, namebuf, (unsigned) link_len); 646 } else { 647 /* 648 * Read file for symbolic link 649 */ 650 size_t buf_size; 651 ufs2_daddr_t disk_block; 652 struct fs *fs = fp->f_fs; 653 654 if (!buf) 655 buf = malloc(fs->fs_bsize); 656 rc = block_map(f, (ufs2_daddr_t)0, &disk_block); 657 if (rc) 658 goto out; 659 660 twiddle(1); 661 rc = (f->f_dev->dv_strategy)(f->f_devdata, 662 F_READ, fsbtodb(fs, disk_block), 663 fs->fs_bsize, buf, &buf_size); 664 if (rc) 665 goto out; 666 667 bcopy((char *)buf, namebuf, (unsigned)link_len); 668 } 669 670 /* 671 * If relative pathname, restart at parent directory. 672 * If absolute pathname, restart at root. 673 */ 674 cp = namebuf; 675 if (*cp != '/') 676 inumber = parent_inumber; 677 else 678 inumber = (ino_t)UFS_ROOTINO; 679 680 if ((rc = read_inode(inumber, f)) != 0) 681 goto out; 682 } 683 } 684 685 /* 686 * Found terminal component. 687 */ 688 rc = 0; 689 fp->f_seekp = 0; 690 out: 691 free(buf); 692 free(path); 693 if (rc) { 694 free(fp->f_buf); 695 696 if (mnt == NULL && fp->f_fs != NULL) { 697 free(fp->f_fs->fs_csp); 698 free(fp->f_fs->fs_si); 699 free(fp->f_fs); 700 } 701 free(fp); 702 } 703 return (rc); 704 } 705 706 /* 707 * A read function for use by standalone-layer routines. 708 */ 709 static int 710 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size) 711 { 712 struct open_file *f; 713 size_t buf_size; 714 int error; 715 716 f = (struct open_file *)devfd; 717 if ((*bufp = malloc(size)) == NULL) 718 return (ENOSPC); 719 error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE, 720 size, *bufp, &buf_size); 721 if (error != 0) 722 return (error); 723 if (buf_size != size) 724 return (EIO); 725 return (0); 726 } 727 728 static int 729 ufs_close(struct open_file *f) 730 { 731 ufs_mnt_t *mnt; 732 struct file *fp = (struct file *)f->f_fsdata; 733 int level; 734 char *dev; 735 736 f->f_fsdata = NULL; 737 if (fp == NULL) 738 return (0); 739 740 for (level = 0; level < UFS_NIADDR; level++) { 741 free(fp->f_blk[level]); 742 } 743 free(fp->f_buf); 744 745 dev = disk_fmtdev(f->f_devdata); 746 STAILQ_FOREACH(mnt, &mnt_list, um_link) { 747 if (strcmp(dev, mnt->um_dev) == 0) 748 break; 749 } 750 751 if (mnt == NULL && fp->f_fs != NULL) { 752 free(fp->f_fs->fs_csp); 753 free(fp->f_fs->fs_si); 754 free(fp->f_fs); 755 } 756 757 free(fp); 758 return (0); 759 } 760 761 /* 762 * Copy a portion of a file into kernel memory. 763 * Cross block boundaries when necessary. 764 */ 765 static int 766 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid) 767 { 768 struct file *fp = (struct file *)f->f_fsdata; 769 size_t csize; 770 char *buf; 771 size_t buf_size; 772 int rc = 0; 773 char *addr = start; 774 775 while (size != 0) { 776 if (fp->f_seekp >= DIP(fp, di_size)) 777 break; 778 779 rc = buf_read_file(f, &buf, &buf_size); 780 if (rc) 781 break; 782 783 csize = size; 784 if (csize > buf_size) 785 csize = buf_size; 786 787 bcopy(buf, addr, csize); 788 789 fp->f_seekp += csize; 790 addr += csize; 791 size -= csize; 792 } 793 if (resid) 794 *resid = size; 795 return (rc); 796 } 797 798 /* 799 * Write to a portion of an already allocated file. 800 * Cross block boundaries when necessary. Can not 801 * extend the file. 802 */ 803 static int 804 ufs_write(struct open_file *f, const void *start, size_t size, size_t *resid) 805 { 806 struct file *fp = (struct file *)f->f_fsdata; 807 size_t csize; 808 int rc = 0; 809 const char *addr = start; 810 811 csize = size; 812 while ((size != 0) && (csize != 0)) { 813 if (fp->f_seekp >= DIP(fp, di_size)) 814 break; 815 816 if (csize >= 512) csize = 512; /* XXX */ 817 818 rc = buf_write_file(f, addr, &csize); 819 if (rc) 820 break; 821 822 fp->f_seekp += csize; 823 addr += csize; 824 size -= csize; 825 } 826 if (resid) 827 *resid = size; 828 return (rc); 829 } 830 831 static off_t 832 ufs_seek(struct open_file *f, off_t offset, int where) 833 { 834 struct file *fp = (struct file *)f->f_fsdata; 835 836 switch (where) { 837 case SEEK_SET: 838 fp->f_seekp = offset; 839 break; 840 case SEEK_CUR: 841 fp->f_seekp += offset; 842 break; 843 case SEEK_END: 844 fp->f_seekp = DIP(fp, di_size) - offset; 845 break; 846 default: 847 errno = EINVAL; 848 return (-1); 849 } 850 return (fp->f_seekp); 851 } 852 853 static int 854 ufs_stat(struct open_file *f, struct stat *sb) 855 { 856 struct file *fp = (struct file *)f->f_fsdata; 857 858 /* only important stuff */ 859 sb->st_mode = DIP(fp, di_mode); 860 sb->st_uid = DIP(fp, di_uid); 861 sb->st_gid = DIP(fp, di_gid); 862 sb->st_size = DIP(fp, di_size); 863 sb->st_mtime = DIP(fp, di_mtime); 864 /* 865 * The items below are ufs specific! 866 * Other fs types will need their own solution 867 * if these fields are needed. 868 */ 869 sb->st_ino = fp->f_inumber; 870 /* 871 * We need something to differentiate devs. 872 * fs_id is unique but 64bit, we xor the two 873 * halves to squeeze it into 32bits. 874 */ 875 sb->st_dev = (dev_t)(fp->f_fs->fs_id[0] ^ fp->f_fs->fs_id[1]); 876 877 return (0); 878 } 879 880 static int 881 ufs_readdir(struct open_file *f, struct dirent *d) 882 { 883 struct file *fp = (struct file *)f->f_fsdata; 884 struct direct *dp; 885 char *buf; 886 size_t buf_size; 887 int error; 888 889 /* 890 * assume that a directory entry will not be split across blocks 891 */ 892 again: 893 if (fp->f_seekp >= DIP(fp, di_size)) 894 return (ENOENT); 895 error = buf_read_file(f, &buf, &buf_size); 896 if (error) 897 return (error); 898 dp = (struct direct *)buf; 899 fp->f_seekp += dp->d_reclen; 900 if (dp->d_ino == (ino_t)0) 901 goto again; 902 d->d_type = dp->d_type; 903 strcpy(d->d_name, dp->d_name); 904 return (0); 905 } 906 907 static int 908 ufs_mount(const char *dev, const char *path, void **data) 909 { 910 char *fs; 911 ufs_mnt_t *mnt; 912 struct open_file *f; 913 914 errno = 0; 915 mnt = calloc(1, sizeof(*mnt)); 916 if (mnt == NULL) 917 return (errno); 918 mnt->um_fd = -1; 919 mnt->um_dev = strdup(dev); 920 if (mnt->um_dev == NULL) 921 goto done; 922 923 if (asprintf(&fs, "%s%s", dev, path) < 0) 924 goto done; 925 926 mnt->um_fd = open(fs, O_RDONLY); 927 free(fs); 928 if (mnt->um_fd == -1) 929 goto done; 930 931 /* Is it ufs file system? */ 932 f = fd2open_file(mnt->um_fd); 933 if (strcmp(f->f_ops->fs_name, "ufs") == 0) 934 STAILQ_INSERT_TAIL(&mnt_list, mnt, um_link); 935 else 936 errno = ENXIO; 937 938 done: 939 if (errno != 0) { 940 free(mnt->um_dev); 941 if (mnt->um_fd >= 0) 942 close(mnt->um_fd); 943 free(mnt); 944 } else { 945 *data = mnt; 946 } 947 948 return (errno); 949 } 950 951 static int 952 ufs_unmount(const char *dev __unused, void *data) 953 { 954 ufs_mnt_t *mnt = data; 955 956 STAILQ_REMOVE(&mnt_list, mnt, ufs_mnt, um_link); 957 free(mnt->um_dev); 958 close(mnt->um_fd); 959 free(mnt); 960 return (0); 961 } 962