1 /*- 2 * Copyright (c) 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software contributed to Berkeley 6 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension 7 * Support code is derived from software contributed to Berkeley 8 * by Atsushi Murai (amurai@spec.co.jp). 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 * @(#)cd9660_vfsops.c 8.3 (Berkeley) 1/31/94 39 * $Id: cd9660_vfsops.c,v 1.4 1994/08/20 03:48:45 davidg Exp $ 40 */ 41 42 #include <sys/param.h> 43 #include <sys/systm.h> 44 #include <sys/namei.h> 45 #include <sys/proc.h> 46 #include <sys/kernel.h> 47 #include <sys/vnode.h> 48 #include <miscfs/specfs/specdev.h> 49 #include <sys/mount.h> 50 #include <sys/buf.h> 51 #include <sys/file.h> 52 #include <sys/dkbad.h> 53 #include <sys/disklabel.h> 54 #include <sys/ioctl.h> 55 #include <sys/errno.h> 56 #include <sys/malloc.h> 57 58 #include <isofs/cd9660/iso.h> 59 #include <isofs/cd9660/cd9660_node.h> 60 61 struct vfsops cd9660_vfsops = { 62 cd9660_mount, 63 cd9660_start, 64 cd9660_unmount, 65 cd9660_root, 66 cd9660_quotactl, 67 cd9660_statfs, 68 cd9660_sync, 69 cd9660_vget, 70 cd9660_fhtovp, 71 cd9660_vptofh, 72 cd9660_init, 73 }; 74 75 /* 76 * Called by vfs_mountroot when iso is going to be mounted as root. 77 * 78 * Name is updated by mount(8) after booting. 79 */ 80 #define ROOTNAME "root_device" 81 82 static iso_mountfs(); 83 84 int 85 cd9660_mountroot() 86 { 87 register struct mount *mp; 88 struct proc *p = curproc; /* XXX */ 89 struct iso_mnt *imp; 90 register struct fs *fs; 91 u_int size; 92 int error; 93 struct iso_args args; 94 95 /* 96 * Get vnodes for swapdev and rootdev. 97 */ 98 if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp)) 99 panic("cd9660_mountroot: can't setup bdevvp's"); 100 101 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 102 bzero((char *)mp, (u_long)sizeof(struct mount)); 103 mp->mnt_op = &cd9660_vfsops; 104 mp->mnt_flag = MNT_RDONLY; 105 args.flags = ISOFSMNT_ROOT; 106 if (error = iso_mountfs(rootvp, mp, p, &args)) { 107 free(mp, M_MOUNT); 108 return (error); 109 } 110 if (error = vfs_lock(mp)) { 111 (void)cd9660_unmount(mp, 0, p); 112 free(mp, M_MOUNT); 113 return (error); 114 } 115 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 116 mp->mnt_flag |= MNT_ROOTFS; 117 mp->mnt_vnodecovered = NULLVP; 118 imp = VFSTOISOFS(mp); 119 bzero(imp->im_fsmnt, sizeof(imp->im_fsmnt)); 120 imp->im_fsmnt[0] = '/'; 121 bcopy((caddr_t)imp->im_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 122 MNAMELEN); 123 (void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 124 &size); 125 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 126 (void) cd9660_statfs(mp, &mp->mnt_stat, p); 127 vfs_unlock(mp); 128 return (0); 129 } 130 131 /* 132 * Flag to allow forcible unmounting. 133 */ 134 int iso_doforce = 1; 135 136 /* 137 * VFS Operations. 138 * 139 * mount system call 140 */ 141 int 142 cd9660_mount(mp, path, data, ndp, p) 143 register struct mount *mp; 144 char *path; 145 caddr_t data; 146 struct nameidata *ndp; 147 struct proc *p; 148 { 149 struct vnode *devvp; 150 struct iso_args args; 151 u_int size; 152 int error; 153 struct iso_mnt *imp = 0; 154 155 if (error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))) 156 return (error); 157 158 if ((mp->mnt_flag & MNT_RDONLY) == 0) 159 return (EROFS); 160 161 /* 162 * If updating, check whether changing from read-only to 163 * read/write; if there is no device name, that's all we do. 164 */ 165 if (mp->mnt_flag & MNT_UPDATE) { 166 imp = VFSTOISOFS(mp); 167 if (args.fspec == 0) 168 return (vfs_export(mp, &imp->im_export, &args.export)); 169 } 170 /* 171 * Not an update, or updating the name: look up the name 172 * and verify that it refers to a sensible block device. 173 */ 174 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p); 175 if (error = namei(ndp)) 176 return (error); 177 devvp = ndp->ni_vp; 178 179 if (devvp->v_type != VBLK) { 180 vrele(devvp); 181 return ENOTBLK; 182 } 183 if (major(devvp->v_rdev) >= nblkdev) { 184 vrele(devvp); 185 return ENXIO; 186 } 187 if ((mp->mnt_flag & MNT_UPDATE) == 0) 188 error = iso_mountfs(devvp, mp, p, &args); 189 else { 190 if (devvp != imp->im_devvp) 191 error = EINVAL; /* needs translation */ 192 else 193 vrele(devvp); 194 } 195 if (error) { 196 vrele(devvp); 197 return error; 198 } 199 imp = VFSTOISOFS(mp); 200 (void) copyinstr(path, imp->im_fsmnt, sizeof(imp->im_fsmnt)-1, &size); 201 bzero(imp->im_fsmnt + size, sizeof(imp->im_fsmnt) - size); 202 bcopy((caddr_t)imp->im_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname, 203 MNAMELEN); 204 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 205 &size); 206 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size); 207 (void) cd9660_statfs(mp, &mp->mnt_stat, p); 208 return 0; 209 } 210 211 /* 212 * Common code for mount and mountroot 213 */ 214 static int 215 iso_mountfs(devvp, mp, p, argp) 216 register struct vnode *devvp; 217 struct mount *mp; 218 struct proc *p; 219 struct iso_args *argp; 220 { 221 register struct iso_mnt *isomp = (struct iso_mnt *)0; 222 struct buf *bp = NULL; 223 dev_t dev = devvp->v_rdev; 224 caddr_t base, space; 225 int havepart = 0, blks; 226 int error = EINVAL, i, size; 227 int needclose = 0; 228 int ronly = (mp->mnt_flag & MNT_RDONLY) != 0; 229 int j; 230 int iso_bsize; 231 int iso_blknum; 232 struct iso_volume_descriptor *vdp; 233 struct iso_primary_descriptor *pri; 234 struct iso_directory_record *rootp; 235 int logical_block_size; 236 237 if (!ronly) 238 return EROFS; 239 240 /* 241 * Disallow multiple mounts of the same device. 242 * Disallow mounting of a device that is currently in use 243 * (except for root, which might share swap device for miniroot). 244 * Flush out any old buffers remaining from a previous use. 245 */ 246 if (error = vfs_mountedon(devvp)) 247 return error; 248 if (vcount(devvp) > 1 && devvp != rootvp) 249 return EBUSY; 250 if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) 251 return (error); 252 253 if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p)) 254 return error; 255 needclose = 1; 256 257 /* This is the "logical sector size". The standard says this 258 * should be 2048 or the physical sector size on the device, 259 * whichever is greater. For now, we'll just use a constant. 260 */ 261 iso_bsize = ISO_DEFAULT_BLOCK_SIZE; 262 263 for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) { 264 if (error = bread (devvp, btodb(iso_blknum * iso_bsize), 265 iso_bsize, NOCRED, &bp)) 266 goto out; 267 268 vdp = (struct iso_volume_descriptor *)bp->b_un.b_addr; 269 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) { 270 error = EINVAL; 271 goto out; 272 } 273 274 if (isonum_711 (vdp->type) == ISO_VD_END) { 275 error = EINVAL; 276 goto out; 277 } 278 279 if (isonum_711 (vdp->type) == ISO_VD_PRIMARY) 280 break; 281 brelse(bp); 282 } 283 284 if (isonum_711 (vdp->type) != ISO_VD_PRIMARY) { 285 error = EINVAL; 286 goto out; 287 } 288 289 pri = (struct iso_primary_descriptor *)vdp; 290 291 logical_block_size = isonum_723 (pri->logical_block_size); 292 293 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE 294 || (logical_block_size & (logical_block_size - 1)) != 0) { 295 error = EINVAL; 296 goto out; 297 } 298 299 rootp = (struct iso_directory_record *)pri->root_directory_record; 300 301 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK); 302 bzero((caddr_t)isomp, sizeof *isomp); 303 isomp->logical_block_size = logical_block_size; 304 isomp->volume_space_size = isonum_733 (pri->volume_space_size); 305 bcopy (rootp, isomp->root, sizeof isomp->root); 306 isomp->root_extent = isonum_733 (rootp->extent); 307 isomp->root_size = isonum_733 (rootp->size); 308 309 isomp->im_bmask = logical_block_size - 1; 310 isomp->im_bshift = 0; 311 while ((1 << isomp->im_bshift) < isomp->logical_block_size) 312 isomp->im_bshift++; 313 314 bp->b_flags |= B_AGE; 315 brelse(bp); 316 bp = NULL; 317 318 mp->mnt_data = (qaddr_t)isomp; 319 mp->mnt_stat.f_fsid.val[0] = (long)dev; 320 mp->mnt_stat.f_fsid.val[1] = MOUNT_CD9660; 321 mp->mnt_maxsymlinklen = 0; 322 mp->mnt_flag |= MNT_LOCAL; 323 isomp->im_mountp = mp; 324 isomp->im_dev = dev; 325 isomp->im_devvp = devvp; 326 327 devvp->v_specflags |= SI_MOUNTEDON; 328 329 /* Check the Rock Ridge Extention support */ 330 if (!(argp->flags & ISOFSMNT_NORRIP)) { 331 if (error = bread (isomp->im_devvp, 332 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) 333 * isomp->logical_block_size / DEV_BSIZE, 334 isomp->logical_block_size,NOCRED,&bp)) 335 goto out; 336 337 rootp = (struct iso_directory_record *)bp->b_un.b_addr; 338 339 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) { 340 argp->flags |= ISOFSMNT_NORRIP; 341 } else { 342 argp->flags &= ~ISOFSMNT_GENS; 343 } 344 345 /* 346 * The contents are valid, 347 * but they will get reread as part of another vnode, so... 348 */ 349 bp->b_flags |= B_AGE; 350 brelse(bp); 351 bp = NULL; 352 } 353 isomp->im_flags = argp->flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS|ISOFSMNT_EXTATT); 354 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) { 355 default: 356 isomp->iso_ftype = ISO_FTYPE_DEFAULT; 357 break; 358 case ISOFSMNT_GENS|ISOFSMNT_NORRIP: 359 isomp->iso_ftype = ISO_FTYPE_9660; 360 break; 361 case 0: 362 isomp->iso_ftype = ISO_FTYPE_RRIP; 363 break; 364 } 365 366 return 0; 367 out: 368 if (bp) 369 brelse(bp); 370 if (needclose) 371 (void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p); 372 if (isomp) { 373 free((caddr_t)isomp, M_ISOFSMNT); 374 mp->mnt_data = (qaddr_t)0; 375 } 376 return error; 377 } 378 379 /* 380 * Make a filesystem operational. 381 * Nothing to do at the moment. 382 */ 383 /* ARGSUSED */ 384 int 385 cd9660_start(mp, flags, p) 386 struct mount *mp; 387 int flags; 388 struct proc *p; 389 { 390 return 0; 391 } 392 393 /* 394 * unmount system call 395 */ 396 int 397 cd9660_unmount(mp, mntflags, p) 398 struct mount *mp; 399 int mntflags; 400 struct proc *p; 401 { 402 register struct iso_mnt *isomp; 403 int i, error, ronly, flags = 0; 404 405 if (mntflags & MNT_FORCE) { 406 if (!iso_doforce) 407 return (EINVAL); 408 flags |= FORCECLOSE; 409 } 410 #if 0 411 mntflushbuf(mp, 0); 412 if (mntinvalbuf(mp)) 413 return EBUSY; 414 #endif 415 if (error = vflush(mp, NULLVP, flags)) 416 return (error); 417 418 isomp = VFSTOISOFS(mp); 419 420 #ifdef ISODEVMAP 421 if (isomp->iso_ftype == ISO_FTYPE_RRIP) 422 iso_dunmap(isomp->im_dev); 423 #endif 424 425 isomp->im_devvp->v_specflags &= ~SI_MOUNTEDON; 426 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p); 427 vrele(isomp->im_devvp); 428 free((caddr_t)isomp, M_ISOFSMNT); 429 mp->mnt_data = (qaddr_t)0; 430 mp->mnt_flag &= ~MNT_LOCAL; 431 return (error); 432 } 433 434 /* 435 * Return root of a filesystem 436 */ 437 int 438 cd9660_root(mp, vpp) 439 struct mount *mp; 440 struct vnode **vpp; 441 { 442 register struct iso_node *ip; 443 struct iso_node tip, *nip; 444 struct vnode tvp; 445 int error; 446 struct iso_mnt *imp = VFSTOISOFS (mp); 447 struct iso_directory_record *dp; 448 449 tvp.v_mount = mp; 450 tvp.v_data = &tip; 451 ip = VTOI(&tvp); 452 ip->i_vnode = &tvp; 453 ip->i_dev = imp->im_dev; 454 ip->i_diroff = 0; 455 dp = (struct iso_directory_record *)imp->root; 456 isodirino(&ip->i_number,dp,imp); 457 458 /* 459 * With RRIP we must use the `.' entry of the root directory. 460 * Simply tell iget, that it's a relocated directory. 461 */ 462 error = iso_iget(ip,ip->i_number, 463 imp->iso_ftype == ISO_FTYPE_RRIP, 464 &nip,dp); 465 if (error) 466 return error; 467 *vpp = ITOV(nip); 468 return 0; 469 } 470 471 /* 472 * Do operations associated with quotas, not supported 473 */ 474 /* ARGSUSED */ 475 int 476 cd9660_quotactl(mp, cmd, uid, arg, p) 477 struct mount *mp; 478 int cmd; 479 uid_t uid; 480 caddr_t arg; 481 struct proc *p; 482 { 483 484 return (EOPNOTSUPP); 485 } 486 487 /* 488 * Get file system statistics. 489 */ 490 int 491 cd9660_statfs(mp, sbp, p) 492 struct mount *mp; 493 register struct statfs *sbp; 494 struct proc *p; 495 { 496 register struct iso_mnt *isomp; 497 register struct fs *fs; 498 499 isomp = VFSTOISOFS(mp); 500 501 sbp->f_type = MOUNT_CD9660; 502 sbp->f_bsize = isomp->logical_block_size; 503 sbp->f_iosize = sbp->f_bsize; /* XXX */ 504 sbp->f_blocks = isomp->volume_space_size; 505 sbp->f_bfree = 0; /* total free blocks */ 506 sbp->f_bavail = 0; /* blocks free for non superuser */ 507 sbp->f_files = 0; /* total files */ 508 sbp->f_ffree = 0; /* free file nodes */ 509 if (sbp != &mp->mnt_stat) { 510 bcopy((caddr_t)mp->mnt_stat.f_mntonname, 511 (caddr_t)&sbp->f_mntonname[0], MNAMELEN); 512 bcopy((caddr_t)mp->mnt_stat.f_mntfromname, 513 (caddr_t)&sbp->f_mntfromname[0], MNAMELEN); 514 } 515 /* Use the first spare for flags: */ 516 sbp->f_spare[0] = isomp->im_flags; 517 return 0; 518 } 519 520 /* ARGSUSED */ 521 int 522 cd9660_sync(mp, waitfor, cred, p) 523 struct mount *mp; 524 int waitfor; 525 struct ucred *cred; 526 struct proc *p; 527 { 528 return (0); 529 } 530 531 /* 532 * Flat namespace lookup. 533 * Currently unsupported. 534 */ 535 /* ARGSUSED */ 536 int 537 cd9660_vget(mp, ino, vpp) 538 struct mount *mp; 539 ino_t ino; 540 struct vnode **vpp; 541 { 542 543 return (EOPNOTSUPP); 544 } 545 546 /* 547 * File handle to vnode 548 * 549 * Have to be really careful about stale file handles: 550 * - check that the inode number is in range 551 * - call iget() to get the locked inode 552 * - check for an unallocated inode (i_mode == 0) 553 * - check that the generation number matches 554 */ 555 556 struct ifid { 557 ushort ifid_len; 558 ushort ifid_pad; 559 int ifid_ino; 560 long ifid_start; 561 }; 562 563 /* ARGSUSED */ 564 int 565 cd9660_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp) 566 register struct mount *mp; 567 struct fid *fhp; 568 struct mbuf *nam; 569 struct vnode **vpp; 570 int *exflagsp; 571 struct ucred **credanonp; 572 { 573 struct vnode tvp; 574 int error; 575 int lbn, off; 576 struct ifid *ifhp; 577 struct iso_mnt *imp; 578 struct buf *bp; 579 struct iso_directory_record *dirp; 580 struct iso_node tip, *ip, *nip; 581 struct netcred *np; 582 583 imp = VFSTOISOFS (mp); 584 ifhp = (struct ifid *)fhp; 585 586 #ifdef ISOFS_DBG 587 printf("fhtovp: ino %d, start %ld\n", 588 ifhp->ifid_ino, ifhp->ifid_start); 589 #endif 590 591 np = vfs_export_lookup(mp, &imp->im_export, nam); 592 if (np == NULL) 593 return (EACCES); 594 595 lbn = iso_lblkno(imp, ifhp->ifid_ino); 596 if (lbn >= imp->volume_space_size) { 597 printf("fhtovp: lbn exceed volume space %d\n", lbn); 598 return (ESTALE); 599 } 600 601 off = iso_blkoff(imp, ifhp->ifid_ino); 602 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) { 603 printf("fhtovp: crosses block boundary %d\n", 604 off + ISO_DIRECTORY_RECORD_SIZE); 605 return (ESTALE); 606 } 607 608 error = bread(imp->im_devvp, btodb(lbn * imp->logical_block_size), 609 imp->logical_block_size, NOCRED, &bp); 610 if (error) { 611 printf("fhtovp: bread error %d\n",error); 612 brelse(bp); 613 return (error); 614 } 615 616 dirp = (struct iso_directory_record *)(bp->b_un.b_addr + off); 617 if (off + isonum_711(dirp->length) > imp->logical_block_size) { 618 brelse(bp); 619 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n", 620 off+isonum_711(dirp->length), off, 621 isonum_711(dirp->length)); 622 return (ESTALE); 623 } 624 625 if (isonum_733(dirp->extent) + isonum_711(dirp->ext_attr_length) != 626 ifhp->ifid_start) { 627 brelse(bp); 628 printf("fhtovp: file start miss %d vs %d\n", 629 isonum_733(dirp->extent)+isonum_711(dirp->ext_attr_length), 630 ifhp->ifid_start); 631 return (ESTALE); 632 } 633 brelse(bp); 634 635 ip = &tip; 636 tvp.v_mount = mp; 637 tvp.v_data = ip; 638 ip->i_vnode = &tvp; 639 ip->i_dev = imp->im_dev; 640 if (error = iso_iget(ip, ifhp->ifid_ino, 0, &nip, dirp)) { 641 *vpp = NULLVP; 642 printf("fhtovp: failed to get inode\n"); 643 return (error); 644 } 645 ip = nip; 646 /* 647 * XXX need generation number? 648 */ 649 if (ip->inode.iso_mode == 0) { 650 iso_iput(ip); 651 *vpp = NULLVP; 652 printf("fhtovp: inode mode == 0\n"); 653 return (ESTALE); 654 } 655 *vpp = ITOV(ip); 656 *exflagsp = np->netc_exflags; 657 *credanonp = &np->netc_anon; 658 return 0; 659 } 660 661 /* 662 * Vnode pointer to File handle 663 */ 664 /* ARGSUSED */ 665 int 666 cd9660_vptofh(vp, fhp) 667 struct vnode *vp; 668 struct fid *fhp; 669 { 670 register struct iso_node *ip = VTOI(vp); 671 register struct ifid *ifhp; 672 register struct iso_mnt *mp = ip->i_mnt; 673 674 ifhp = (struct ifid *)fhp; 675 ifhp->ifid_len = sizeof(struct ifid); 676 677 ifhp->ifid_ino = ip->i_number; 678 ifhp->ifid_start = ip->iso_start; 679 680 #ifdef ISOFS_DBG 681 printf("vptofh: ino %d, start %ld\n", 682 ifhp->ifid_ino,ifhp->ifid_start); 683 #endif 684 return 0; 685 } 686