3 * The Regents of the University of California. All rights reserved.
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).
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95
39 * $FreeBSD: src/sys/isofs/cd9660/cd9660_vfsops.c,v 1.74.2.7 2002/04/08 09:39:29 bde Exp $
40 * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vfsops.c,v 1.2 2003/06/17 04:28:41 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
56 #include <sys/syslog.h>
58 #include <vm/vm_zone.h>
60 #include <isofs/cd9660/iso.h>
61 #include <isofs/cd9660/iso_rrip.h>
62 #include <isofs/cd9660/cd9660_node.h>
63 #include <isofs/cd9660/cd9660_mount.h>
65 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
66 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
68 static int cd9660_mount __P((struct mount *,
69 char *, caddr_t, struct nameidata *, struct proc *));
70 static int cd9660_unmount __P((struct mount *, int, struct proc *));
71 static int cd9660_root __P((struct mount *, struct vnode **));
72 static int cd9660_statfs __P((struct mount *, struct statfs *, struct proc *));
73 static int cd9660_vget __P((struct mount *, ino_t, struct vnode **));
74 static int cd9660_fhtovp __P((struct mount *, struct fid *, struct vnode **));
75 static int cd9660_checkexp __P((struct mount *, struct sockaddr *,
76 int *, struct ucred **));
77 static int cd9660_vptofh __P((struct vnode *, struct fid *));
79 static struct vfsops cd9660_vfsops = {
95 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
96 MODULE_VERSION(cd9660, 1);
100 * Called by vfs_mountroot when iso is going to be mounted as root.
103 static int iso_get_ssector __P((dev_t dev, struct proc *p));
104 static int iso_mountfs __P((struct vnode *devvp, struct mount *mp,
105 struct proc *p, struct iso_args *argp));
108 * Try to find the start of the last data track on this CD-ROM. This
109 * is used to mount the last session of a multi-session CD. Bail out
110 * and return 0 if we fail, this is always a safe bet.
113 iso_get_ssector(dev, p)
117 struct ioc_toc_header h;
118 struct ioc_read_toc_single_entry t;
124 ioctlp = bd->d_ioctl;
128 if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, p) != 0)
131 for (i = h.ending_track; i >= 0; i--) {
132 t.address_format = CD_LBA_FORMAT;
134 if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, p) != 0)
136 if ((t.entry.control & 4) != 0)
137 /* found a data track */
144 return ntohl(t.entry.addr.lba);
147 static int iso_mountroot __P((struct mount *mp, struct proc *p));
154 struct iso_args args;
157 if ((error = bdevvp(rootdev, &rootvp))) {
158 printf("iso_mountroot: can't find rootvp\n");
161 args.flags = ISOFSMNT_ROOT;
163 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, p);
164 error = VOP_OPEN(rootvp, FREAD, FSCRED, p);
165 VOP_UNLOCK(rootvp, 0, p);
169 args.ssector = iso_get_ssector(rootdev, p);
171 (void)VOP_CLOSE(rootvp, FREAD, NOCRED, p);
174 printf("iso_mountroot(): using session at block %d\n",
176 if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0)
179 (void)cd9660_statfs(mp, &mp->mnt_stat, p);
189 cd9660_mount(mp, path, data, ndp, p)
190 register struct mount *mp;
193 struct nameidata *ndp;
197 struct iso_args args;
201 struct iso_mnt *imp = 0;
203 if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
204 return (iso_mountroot(mp, p));
206 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
209 if ((mp->mnt_flag & MNT_RDONLY) == 0)
213 * If updating, check whether changing from read-only to
214 * read/write; if there is no device name, that's all we do.
216 if (mp->mnt_flag & MNT_UPDATE) {
217 imp = VFSTOISOFS(mp);
219 return (vfs_export(mp, &imp->im_export, &args.export));
222 * Not an update, or updating the name: look up the name
223 * and verify that it refers to a sensible block device.
225 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
226 if ((error = namei(ndp)))
228 NDFREE(ndp, NDF_ONLY_PNBUF);
231 if (!vn_isdisk(devvp, &error)) {
237 * Verify that user has necessary permissions on the device,
238 * or has superuser abilities
241 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
242 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
249 VOP_UNLOCK(devvp, 0, p);
251 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
252 error = iso_mountfs(devvp, mp, p, &args);
254 if (devvp != imp->im_devvp)
255 error = EINVAL; /* needs translation */
263 imp = VFSTOISOFS(mp);
264 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
265 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
266 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
268 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
269 (void) cd9660_statfs(mp, &mp->mnt_stat, p);
274 * Common code for mount and mountroot
277 iso_mountfs(devvp, mp, p, argp)
278 register struct vnode *devvp;
281 struct iso_args *argp;
283 register struct iso_mnt *isomp = (struct iso_mnt *)0;
284 struct buf *bp = NULL;
285 struct buf *pribp = NULL, *supbp = NULL;
286 dev_t dev = devvp->v_rdev;
293 struct iso_volume_descriptor *vdp = 0;
294 struct iso_primary_descriptor *pri = NULL;
295 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
296 struct iso_supplementary_descriptor *sup = NULL;
297 struct iso_directory_record *rootp;
298 int logical_block_size;
300 if (!(mp->mnt_flag & MNT_RDONLY))
304 * Disallow multiple mounts of the same device.
305 * Disallow mounting of a device that is currently in use
306 * (except for root, which might share swap device for miniroot).
307 * Flush out any old buffers remaining from a previous use.
309 if ((error = vfs_mountedon(devvp)))
311 if (vcount(devvp) > 1 && devvp != rootvp)
313 if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)))
316 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
317 error = VOP_OPEN(devvp, FREAD, FSCRED, p);
318 VOP_UNLOCK(devvp, 0, p);
321 if (devvp->v_rdev->si_iosize_max != 0)
322 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
323 if (mp->mnt_iosize_max > MAXPHYS)
324 mp->mnt_iosize_max = MAXPHYS;
328 /* This is the "logical sector size". The standard says this
329 * should be 2048 or the physical sector size on the device,
330 * whichever is greater. For now, we'll just use a constant.
332 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
335 for (iso_blknum = 16 + argp->ssector;
336 iso_blknum < 100 + argp->ssector;
338 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
339 iso_bsize, NOCRED, &bp)) != 0)
342 vdp = (struct iso_volume_descriptor *)bp->b_data;
343 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
344 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
345 sizeof vdp->id) != 0) {
351 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
356 pri = (struct iso_primary_descriptor *)vdp;
358 (struct iso_sierra_primary_descriptor *)vdp;
362 case ISO_VD_SUPPLEMENTARY:
366 sup = (struct iso_supplementary_descriptor *)vdp;
368 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
369 if (bcmp(sup->escape, "%/@", 3) == 0)
371 if (bcmp(sup->escape, "%/C", 3) == 0)
373 if (bcmp(sup->escape, "%/E", 3) == 0)
376 if ((isonum_711 (sup->flags) & 1) &&
377 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
406 isonum_723 (high_sierra?
407 pri_sierra->logical_block_size:
408 pri->logical_block_size);
410 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
411 || (logical_block_size & (logical_block_size - 1)) != 0) {
416 rootp = (struct iso_directory_record *)
418 pri_sierra->root_directory_record:
419 pri->root_directory_record);
421 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
422 bzero((caddr_t)isomp, sizeof *isomp);
423 isomp->logical_block_size = logical_block_size;
424 isomp->volume_space_size =
425 isonum_733 (high_sierra?
426 pri_sierra->volume_space_size:
427 pri->volume_space_size);
428 isomp->joliet_level = 0;
430 * Since an ISO9660 multi-session CD can also access previous
431 * sessions, we have to include them into the space consider-
432 * ations. This doesn't yield a very accurate number since
433 * parts of the old sessions might be inaccessible now, but we
434 * can't do much better. This is also important for the NFS
435 * filehandle validation.
437 isomp->volume_space_size += argp->ssector;
438 bcopy (rootp, isomp->root, sizeof isomp->root);
439 isomp->root_extent = isonum_733 (rootp->extent);
440 isomp->root_size = isonum_733 (rootp->size);
442 isomp->im_bmask = logical_block_size - 1;
443 isomp->im_bshift = ffs(logical_block_size) - 1;
445 pribp->b_flags |= B_AGE;
449 mp->mnt_data = (qaddr_t)isomp;
450 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
451 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
452 mp->mnt_maxsymlinklen = 0;
453 mp->mnt_flag |= MNT_LOCAL;
454 isomp->im_mountp = mp;
456 isomp->im_devvp = devvp;
458 devvp->v_specmountpoint = mp;
460 /* Check the Rock Ridge Extention support */
461 if (!(argp->flags & ISOFSMNT_NORRIP)) {
462 if ((error = bread(isomp->im_devvp,
463 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
464 (isomp->im_bshift - DEV_BSHIFT),
465 isomp->logical_block_size, NOCRED, &bp)) != 0)
468 rootp = (struct iso_directory_record *)bp->b_data;
470 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
471 argp->flags |= ISOFSMNT_NORRIP;
473 argp->flags &= ~ISOFSMNT_GENS;
477 * The contents are valid,
478 * but they will get reread as part of another vnode, so...
480 bp->b_flags |= B_AGE;
484 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
485 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
488 /* this effectively ignores all the mount flags */
489 log(LOG_INFO, "cd9660: High Sierra Format\n");
490 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
492 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
494 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
496 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
497 isomp->iso_ftype = ISO_FTYPE_9660;
500 log(LOG_INFO, "cd9660: RockRidge Extension\n");
501 isomp->iso_ftype = ISO_FTYPE_RRIP;
505 /* Decide whether to use the Joliet descriptor */
507 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
508 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
509 rootp = (struct iso_directory_record *)
510 sup->root_directory_record;
511 bcopy (rootp, isomp->root, sizeof isomp->root);
512 isomp->root_extent = isonum_733 (rootp->extent);
513 isomp->root_size = isonum_733 (rootp->size);
514 isomp->joliet_level = joliet_level;
515 supbp->b_flags |= B_AGE;
525 devvp->v_specmountpoint = NULL;
533 (void)VOP_CLOSE(devvp, FREAD, NOCRED, p);
535 free((caddr_t)isomp, M_ISOFSMNT);
536 mp->mnt_data = (qaddr_t)0;
542 * unmount system call
545 cd9660_unmount(mp, mntflags, p)
550 register struct iso_mnt *isomp;
551 int error, flags = 0;
553 if (mntflags & MNT_FORCE)
560 if ((error = vflush(mp, 0, flags)))
563 isomp = VFSTOISOFS(mp);
565 isomp->im_devvp->v_specmountpoint = NULL;
566 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
567 vrele(isomp->im_devvp);
568 free((caddr_t)isomp, M_ISOFSMNT);
569 mp->mnt_data = (qaddr_t)0;
570 mp->mnt_flag &= ~MNT_LOCAL;
575 * Return root of a filesystem
582 struct iso_mnt *imp = VFSTOISOFS(mp);
583 struct iso_directory_record *dp =
584 (struct iso_directory_record *)imp->root;
585 ino_t ino = isodirino(dp, imp);
588 * With RRIP we must use the `.' entry of the root directory.
589 * Simply tell vget, that it's a relocated directory.
591 return (cd9660_vget_internal(mp, ino, vpp,
592 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
596 * Get file system statistics.
599 cd9660_statfs(mp, sbp, p)
601 register struct statfs *sbp;
604 register struct iso_mnt *isomp;
606 isomp = VFSTOISOFS(mp);
608 sbp->f_bsize = isomp->logical_block_size;
609 sbp->f_iosize = sbp->f_bsize; /* XXX */
610 sbp->f_blocks = isomp->volume_space_size;
611 sbp->f_bfree = 0; /* total free blocks */
612 sbp->f_bavail = 0; /* blocks free for non superuser */
613 sbp->f_files = 0; /* total files */
614 sbp->f_ffree = 0; /* free file nodes */
615 if (sbp != &mp->mnt_stat) {
616 sbp->f_type = mp->mnt_vfc->vfc_typenum;
617 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
618 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
624 * File handle to vnode
626 * Have to be really careful about stale file handles:
627 * - check that the inode number is in range
628 * - call iget() to get the locked inode
629 * - check for an unallocated inode (i_mode == 0)
630 * - check that the generation number matches
642 cd9660_fhtovp(mp, fhp, vpp)
643 register struct mount *mp;
647 struct ifid *ifhp = (struct ifid *)fhp;
648 register struct iso_node *ip;
653 printf("fhtovp: ino %d, start %ld\n",
654 ifhp->ifid_ino, ifhp->ifid_start);
657 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
662 if (ip->inode.iso_mode == 0) {
672 cd9660_checkexp(mp, nam, exflagsp, credanonp)
674 struct sockaddr *nam;
676 struct ucred **credanonp;
678 register struct netcred *np;
679 register struct iso_mnt *imp;
681 imp = VFSTOISOFS(mp);
684 * Get the export permission structure for this <mp, client> tuple.
686 np = vfs_export_lookup(mp, &imp->im_export, nam);
690 *exflagsp = np->netc_exflags;
691 *credanonp = &np->netc_anon;
696 cd9660_vget(mp, ino, vpp)
704 * It would be nice if we didn't always set the `relocated' flag
705 * and force the extra read, but I don't want to think about fixing
708 return (cd9660_vget_internal(mp, ino, vpp,
710 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
714 (struct iso_directory_record *)0));
718 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
723 struct iso_directory_record *isodir;
732 imp = VFSTOISOFS(mp);
734 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
737 /* Allocate a new vnode/iso_node. */
738 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
742 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
744 bzero((caddr_t)ip, sizeof(struct iso_node));
745 lockinit(&ip->i_lock, PINOD, "isonode", 0, 0);
752 * Put it onto its hash chain and lock it so that other requests for
753 * this inode will block if they arrive while we are sleeping waiting
754 * for old data structures to be purged or for the contents of the
755 * disk portion of this inode to be read.
762 lbn = lblkno(imp, ino);
763 if (lbn >= imp->volume_space_size) {
765 printf("fhtovp: lbn exceed volume space %d\n", lbn);
769 off = blkoff(imp, ino);
770 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
772 printf("fhtovp: crosses block boundary %d\n",
773 off + ISO_DIRECTORY_RECORD_SIZE);
777 error = bread(imp->im_devvp,
778 lbn << (imp->im_bshift - DEV_BSHIFT),
779 imp->logical_block_size, NOCRED, &bp);
783 printf("fhtovp: bread error %d\n",error);
786 isodir = (struct iso_directory_record *)(bp->b_data + off);
788 if (off + isonum_711(isodir->length) >
789 imp->logical_block_size) {
793 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
794 off +isonum_711(isodir->length), off,
795 isonum_711(isodir->length));
800 if (isonum_733(isodir->extent) +
801 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
804 printf("fhtovp: file start miss %d vs %d\n",
805 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
814 ip->i_devvp = imp->im_devvp;
819 * On relocated directories we must
820 * read the `.' entry out of a dir.
822 ip->iso_start = ino >> imp->im_bshift;
825 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
829 isodir = (struct iso_directory_record *)bp->b_data;
832 ip->iso_extent = isonum_733(isodir->extent);
833 ip->i_size = isonum_733(isodir->size);
834 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
837 * Setup time stamp, attribute
840 switch (imp->iso_ftype) {
841 default: /* ISO_FTYPE_9660 */
845 if ((imp->im_flags & ISOFSMNT_EXTATT)
846 && (off = isonum_711(isodir->ext_attr_length)))
847 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
851 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
852 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
858 cd9660_rrip_analyze(isodir, ip, imp);
866 * Initialize the associated vnode
868 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
870 vp->v_op = cd9660_fifoop_p;
874 vp->v_op = cd9660_specop_p;
875 addaliasu(vp, ip->inode.iso_rdev);
881 if (ip->iso_extent == imp->root_extent)
885 * XXX need generation number?
893 * Vnode pointer to File handle
897 cd9660_vptofh(vp, fhp)
901 register struct iso_node *ip = VTOI(vp);
902 register struct ifid *ifhp;
904 ifhp = (struct ifid *)fhp;
905 ifhp->ifid_len = sizeof(struct ifid);
907 ifhp->ifid_ino = ip->i_number;
908 ifhp->ifid_start = ip->iso_start;
911 printf("vptofh: ino %d, start %ld\n",
912 ifhp->ifid_ino,ifhp->ifid_start);