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