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.5 2003/06/26 05:55:13 dillon Exp $
43 #include <sys/param.h>
44 #include <sys/systm.h>
46 #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 thread *));
70 static int cd9660_unmount __P((struct mount *, int, struct thread *));
71 static int cd9660_root __P((struct mount *, struct vnode **));
72 static int cd9660_statfs __P((struct mount *, struct statfs *, struct thread *));
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 thread *td));
104 static int iso_mountfs __P((struct vnode *devvp, struct mount *mp,
105 struct thread *td, 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_t dev, struct thread *td)
115 struct ioc_toc_header h;
116 struct ioc_read_toc_single_entry t;
122 ioctlp = bd->d_ioctl;
126 if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
129 for (i = h.ending_track; i >= 0; i--) {
130 t.address_format = CD_LBA_FORMAT;
132 if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0)
134 if ((t.entry.control & 4) != 0)
135 /* found a data track */
142 return ntohl(t.entry.addr.lba);
145 static int iso_mountroot __P((struct mount *mp, struct thread *td));
148 iso_mountroot(struct mount *mp, struct thread *td)
150 struct iso_args args;
153 if ((error = bdevvp(rootdev, &rootvp))) {
154 printf("iso_mountroot: can't find rootvp\n");
157 args.flags = ISOFSMNT_ROOT;
159 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, td);
160 error = VOP_OPEN(rootvp, FREAD, FSCRED, td);
161 VOP_UNLOCK(rootvp, 0, td);
165 args.ssector = iso_get_ssector(rootdev, td);
167 (void)VOP_CLOSE(rootvp, FREAD, td);
170 printf("iso_mountroot(): using session at block %d\n",
172 if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
175 (void)cd9660_statfs(mp, &mp->mnt_stat, td);
185 cd9660_mount(mp, path, data, ndp, td)
186 register struct mount *mp;
189 struct nameidata *ndp;
193 struct iso_args args;
197 struct iso_mnt *imp = 0;
199 if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
200 return (iso_mountroot(mp, td));
202 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
205 if ((mp->mnt_flag & MNT_RDONLY) == 0)
208 KKASSERT(td->td_proc);
211 * If updating, check whether changing from read-only to
212 * read/write; if there is no device name, that's all we do.
214 if (mp->mnt_flag & MNT_UPDATE) {
215 imp = VFSTOISOFS(mp);
217 return (vfs_export(mp, &imp->im_export, &args.export));
220 * Not an update, or updating the name: look up the name
221 * and verify that it refers to a sensible block device.
223 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
224 if ((error = namei(ndp)))
226 NDFREE(ndp, NDF_ONLY_PNBUF);
229 if (!vn_isdisk(devvp, &error)) {
235 * Verify that user has necessary permissions on the device,
236 * or has superuser abilities
239 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
240 error = VOP_ACCESS(devvp, accessmode, td->td_proc->p_ucred, td);
247 VOP_UNLOCK(devvp, 0, td);
249 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
250 error = iso_mountfs(devvp, mp, td, &args);
252 if (devvp != imp->im_devvp)
253 error = EINVAL; /* needs translation */
261 imp = VFSTOISOFS(mp);
262 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
263 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
264 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
266 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
267 (void) cd9660_statfs(mp, &mp->mnt_stat, td);
272 * Common code for mount and mountroot
279 struct iso_args *argp
281 register struct iso_mnt *isomp = (struct iso_mnt *)0;
282 struct buf *bp = NULL;
283 struct buf *pribp = NULL, *supbp = NULL;
284 dev_t dev = devvp->v_rdev;
291 struct iso_volume_descriptor *vdp = 0;
292 struct iso_primary_descriptor *pri = NULL;
293 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
294 struct iso_supplementary_descriptor *sup = NULL;
295 struct iso_directory_record *rootp;
296 int logical_block_size;
298 if (!(mp->mnt_flag & MNT_RDONLY))
302 * Disallow multiple mounts of the same device.
303 * Disallow mounting of a device that is currently in use
304 * (except for root, which might share swap device for miniroot).
305 * Flush out any old buffers remaining from a previous use.
307 if ((error = vfs_mountedon(devvp)))
309 if (vcount(devvp) > 1 && devvp != rootvp)
311 if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
314 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
315 error = VOP_OPEN(devvp, FREAD, FSCRED, td);
316 VOP_UNLOCK(devvp, 0, td);
319 if (devvp->v_rdev->si_iosize_max != 0)
320 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
321 if (mp->mnt_iosize_max > MAXPHYS)
322 mp->mnt_iosize_max = MAXPHYS;
326 /* This is the "logical sector size". The standard says this
327 * should be 2048 or the physical sector size on the device,
328 * whichever is greater. For now, we'll just use a constant.
330 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
333 for (iso_blknum = 16 + argp->ssector;
334 iso_blknum < 100 + argp->ssector;
336 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
337 iso_bsize, &bp)) != 0)
340 vdp = (struct iso_volume_descriptor *)bp->b_data;
341 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
342 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
343 sizeof vdp->id) != 0) {
349 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
354 pri = (struct iso_primary_descriptor *)vdp;
356 (struct iso_sierra_primary_descriptor *)vdp;
360 case ISO_VD_SUPPLEMENTARY:
364 sup = (struct iso_supplementary_descriptor *)vdp;
366 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
367 if (bcmp(sup->escape, "%/@", 3) == 0)
369 if (bcmp(sup->escape, "%/C", 3) == 0)
371 if (bcmp(sup->escape, "%/E", 3) == 0)
374 if ((isonum_711 (sup->flags) & 1) &&
375 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
404 isonum_723 (high_sierra?
405 pri_sierra->logical_block_size:
406 pri->logical_block_size);
408 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
409 || (logical_block_size & (logical_block_size - 1)) != 0) {
414 rootp = (struct iso_directory_record *)
416 pri_sierra->root_directory_record:
417 pri->root_directory_record);
419 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
420 bzero((caddr_t)isomp, sizeof *isomp);
421 isomp->logical_block_size = logical_block_size;
422 isomp->volume_space_size =
423 isonum_733 (high_sierra?
424 pri_sierra->volume_space_size:
425 pri->volume_space_size);
426 isomp->joliet_level = 0;
428 * Since an ISO9660 multi-session CD can also access previous
429 * sessions, we have to include them into the space consider-
430 * ations. This doesn't yield a very accurate number since
431 * parts of the old sessions might be inaccessible now, but we
432 * can't do much better. This is also important for the NFS
433 * filehandle validation.
435 isomp->volume_space_size += argp->ssector;
436 bcopy (rootp, isomp->root, sizeof isomp->root);
437 isomp->root_extent = isonum_733 (rootp->extent);
438 isomp->root_size = isonum_733 (rootp->size);
440 isomp->im_bmask = logical_block_size - 1;
441 isomp->im_bshift = ffs(logical_block_size) - 1;
443 pribp->b_flags |= B_AGE;
447 mp->mnt_data = (qaddr_t)isomp;
448 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
449 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
450 mp->mnt_maxsymlinklen = 0;
451 mp->mnt_flag |= MNT_LOCAL;
452 isomp->im_mountp = mp;
454 isomp->im_devvp = devvp;
456 devvp->v_specmountpoint = mp;
458 /* Check the Rock Ridge Extention support */
459 if (!(argp->flags & ISOFSMNT_NORRIP)) {
460 if ((error = bread(isomp->im_devvp,
461 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
462 (isomp->im_bshift - DEV_BSHIFT),
463 isomp->logical_block_size, &bp)) != 0)
466 rootp = (struct iso_directory_record *)bp->b_data;
468 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
469 argp->flags |= ISOFSMNT_NORRIP;
471 argp->flags &= ~ISOFSMNT_GENS;
475 * The contents are valid,
476 * but they will get reread as part of another vnode, so...
478 bp->b_flags |= B_AGE;
482 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
483 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
486 /* this effectively ignores all the mount flags */
487 log(LOG_INFO, "cd9660: High Sierra Format\n");
488 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
490 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
492 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
494 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
495 isomp->iso_ftype = ISO_FTYPE_9660;
498 log(LOG_INFO, "cd9660: RockRidge Extension\n");
499 isomp->iso_ftype = ISO_FTYPE_RRIP;
503 /* Decide whether to use the Joliet descriptor */
505 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
506 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
507 rootp = (struct iso_directory_record *)
508 sup->root_directory_record;
509 bcopy (rootp, isomp->root, sizeof isomp->root);
510 isomp->root_extent = isonum_733 (rootp->extent);
511 isomp->root_size = isonum_733 (rootp->size);
512 isomp->joliet_level = joliet_level;
513 supbp->b_flags |= B_AGE;
523 devvp->v_specmountpoint = NULL;
531 (void)VOP_CLOSE(devvp, FREAD, td);
533 free((caddr_t)isomp, M_ISOFSMNT);
534 mp->mnt_data = (qaddr_t)0;
540 * unmount system call
543 cd9660_unmount(struct mount *mp, int mntflags, struct thread *td)
545 register struct iso_mnt *isomp;
546 int error, flags = 0;
548 if (mntflags & MNT_FORCE)
555 if ((error = vflush(mp, 0, flags)))
558 isomp = VFSTOISOFS(mp);
560 isomp->im_devvp->v_specmountpoint = NULL;
561 error = VOP_CLOSE(isomp->im_devvp, FREAD, td);
562 vrele(isomp->im_devvp);
563 free((caddr_t)isomp, M_ISOFSMNT);
564 mp->mnt_data = (qaddr_t)0;
565 mp->mnt_flag &= ~MNT_LOCAL;
570 * Return root of a filesystem
577 struct iso_mnt *imp = VFSTOISOFS(mp);
578 struct iso_directory_record *dp =
579 (struct iso_directory_record *)imp->root;
580 ino_t ino = isodirino(dp, imp);
583 * With RRIP we must use the `.' entry of the root directory.
584 * Simply tell vget, that it's a relocated directory.
586 return (cd9660_vget_internal(mp, ino, vpp,
587 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
591 * Get file system statistics.
594 cd9660_statfs(struct mount *mp, register struct statfs *sbp, struct thread *td)
596 struct iso_mnt *isomp;
598 isomp = VFSTOISOFS(mp);
600 sbp->f_bsize = isomp->logical_block_size;
601 sbp->f_iosize = sbp->f_bsize; /* XXX */
602 sbp->f_blocks = isomp->volume_space_size;
603 sbp->f_bfree = 0; /* total free blocks */
604 sbp->f_bavail = 0; /* blocks free for non superuser */
605 sbp->f_files = 0; /* total files */
606 sbp->f_ffree = 0; /* free file nodes */
607 if (sbp != &mp->mnt_stat) {
608 sbp->f_type = mp->mnt_vfc->vfc_typenum;
609 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
610 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
616 * File handle to vnode
618 * Have to be really careful about stale file handles:
619 * - check that the inode number is in range
620 * - call iget() to get the locked inode
621 * - check for an unallocated inode (i_mode == 0)
622 * - check that the generation number matches
634 cd9660_fhtovp(mp, fhp, vpp)
635 register struct mount *mp;
639 struct ifid *ifhp = (struct ifid *)fhp;
640 register struct iso_node *ip;
645 printf("fhtovp: ino %d, start %ld\n",
646 ifhp->ifid_ino, ifhp->ifid_start);
649 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
654 if (ip->inode.iso_mode == 0) {
664 cd9660_checkexp(mp, nam, exflagsp, credanonp)
666 struct sockaddr *nam;
668 struct ucred **credanonp;
670 register struct netcred *np;
671 register struct iso_mnt *imp;
673 imp = VFSTOISOFS(mp);
676 * Get the export permission structure for this <mp, client> tuple.
678 np = vfs_export_lookup(mp, &imp->im_export, nam);
682 *exflagsp = np->netc_exflags;
683 *credanonp = &np->netc_anon;
688 cd9660_vget(mp, ino, vpp)
696 * It would be nice if we didn't always set the `relocated' flag
697 * and force the extra read, but I don't want to think about fixing
700 return (cd9660_vget_internal(mp, ino, vpp,
702 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
706 (struct iso_directory_record *)0));
710 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
715 struct iso_directory_record *isodir;
724 imp = VFSTOISOFS(mp);
726 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
729 /* Allocate a new vnode/iso_node. */
730 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
734 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
736 bzero((caddr_t)ip, sizeof(struct iso_node));
737 lockinit(&ip->i_lock, PINOD, "isonode", 0, 0);
744 * Put it onto its hash chain and lock it so that other requests for
745 * this inode will block if they arrive while we are sleeping waiting
746 * for old data structures to be purged or for the contents of the
747 * disk portion of this inode to be read.
754 lbn = lblkno(imp, ino);
755 if (lbn >= imp->volume_space_size) {
757 printf("fhtovp: lbn exceed volume space %d\n", lbn);
761 off = blkoff(imp, ino);
762 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
764 printf("fhtovp: crosses block boundary %d\n",
765 off + ISO_DIRECTORY_RECORD_SIZE);
769 error = bread(imp->im_devvp,
770 lbn << (imp->im_bshift - DEV_BSHIFT),
771 imp->logical_block_size, &bp);
775 printf("fhtovp: bread error %d\n",error);
778 isodir = (struct iso_directory_record *)(bp->b_data + off);
780 if (off + isonum_711(isodir->length) >
781 imp->logical_block_size) {
785 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
786 off +isonum_711(isodir->length), off,
787 isonum_711(isodir->length));
792 if (isonum_733(isodir->extent) +
793 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
796 printf("fhtovp: file start miss %d vs %d\n",
797 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
806 ip->i_devvp = imp->im_devvp;
811 * On relocated directories we must
812 * read the `.' entry out of a dir.
814 ip->iso_start = ino >> imp->im_bshift;
817 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
821 isodir = (struct iso_directory_record *)bp->b_data;
824 ip->iso_extent = isonum_733(isodir->extent);
825 ip->i_size = isonum_733(isodir->size);
826 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
829 * Setup time stamp, attribute
832 switch (imp->iso_ftype) {
833 default: /* ISO_FTYPE_9660 */
837 if ((imp->im_flags & ISOFSMNT_EXTATT)
838 && (off = isonum_711(isodir->ext_attr_length)))
839 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
843 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
844 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
850 cd9660_rrip_analyze(isodir, ip, imp);
858 * Initialize the associated vnode
860 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
862 vp->v_op = cd9660_fifoop_p;
866 vp->v_op = cd9660_specop_p;
867 addaliasu(vp, ip->inode.iso_rdev);
873 if (ip->iso_extent == imp->root_extent)
877 * XXX need generation number?
885 * Vnode pointer to File handle
889 cd9660_vptofh(vp, fhp)
893 register struct iso_node *ip = VTOI(vp);
894 register struct ifid *ifhp;
896 ifhp = (struct ifid *)fhp;
897 ifhp->ifid_len = sizeof(struct ifid);
899 ifhp->ifid_ino = ip->i_number;
900 ifhp->ifid_start = ip->iso_start;
903 printf("vptofh: ino %d, start %ld\n",
904 ifhp->ifid_ino,ifhp->ifid_start);