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