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.17 2004/06/26 02:15:16 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>
62 #include "cd9660_node.h"
63 #include "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 (struct mount *,
69 char *, caddr_t, struct nameidata *, struct thread *);
70 static int cd9660_unmount (struct mount *, int, struct thread *);
71 static int cd9660_root (struct mount *, struct vnode **);
72 static int cd9660_statfs (struct mount *, struct statfs *, struct thread *);
73 static int cd9660_vget (struct mount *, ino_t, struct vnode **);
74 static int cd9660_fhtovp (struct mount *, struct fid *, struct vnode **);
75 static int cd9660_checkexp (struct mount *, struct sockaddr *,
76 int *, struct ucred **);
77 static int cd9660_vptofh (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 (dev_t dev, struct thread *td);
104 static int iso_mountfs (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;
119 if (dev_dioctl(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
122 for (i = h.ending_track; i >= 0; i--) {
123 t.address_format = CD_LBA_FORMAT;
125 if (dev_dioctl(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0) {
128 if ((t.entry.control & 4) != 0)
129 /* found a data track */
136 return ntohl(t.entry.addr.lba);
139 static int iso_mountroot (struct mount *mp, struct thread *td);
142 iso_mountroot(struct mount *mp, struct thread *td)
144 struct iso_args args;
147 if ((error = bdevvp(rootdev, &rootvp))) {
148 printf("iso_mountroot: can't find rootvp\n");
151 args.flags = ISOFSMNT_ROOT;
153 vn_lock(rootvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
154 error = VOP_OPEN(rootvp, FREAD, FSCRED, td);
155 VOP_UNLOCK(rootvp, NULL, 0, td);
159 args.ssector = iso_get_ssector(rootdev, td);
161 (void)VOP_CLOSE(rootvp, FREAD, td);
164 printf("iso_mountroot(): using session at block %d\n",
166 if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
169 (void)cd9660_statfs(mp, &mp->mnt_stat, td);
179 cd9660_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
183 struct iso_args args;
187 struct iso_mnt *imp = 0;
189 if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
190 return (iso_mountroot(mp, td));
192 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
195 if ((mp->mnt_flag & MNT_RDONLY) == 0)
198 KKASSERT(td->td_proc);
201 * If updating, check whether changing from read-only to
202 * read/write; if there is no device name, that's all we do.
204 if (mp->mnt_flag & MNT_UPDATE) {
205 imp = VFSTOISOFS(mp);
207 return (vfs_export(mp, &imp->im_export, &args.export));
210 * Not an update, or updating the name: look up the name
211 * and verify that it refers to a sensible block device.
213 NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
214 if ((error = namei(ndp)))
216 NDFREE(ndp, NDF_ONLY_PNBUF);
219 if (!vn_isdisk(devvp, &error)) {
225 * Verify that user has necessary permissions on the device,
226 * or has superuser abilities
229 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
230 error = VOP_ACCESS(devvp, accessmode, td->td_proc->p_ucred, td);
237 VOP_UNLOCK(devvp, NULL, 0, td);
239 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
240 error = iso_mountfs(devvp, mp, td, &args);
242 if (devvp != imp->im_devvp)
243 error = EINVAL; /* needs translation */
251 imp = VFSTOISOFS(mp);
252 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
253 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
254 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
256 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
257 (void) cd9660_statfs(mp, &mp->mnt_stat, td);
262 * Common code for mount and mountroot
265 iso_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
266 struct iso_args *argp)
268 struct iso_mnt *isomp = NULL;
269 struct buf *bp = NULL;
270 struct buf *pribp = NULL, *supbp = NULL;
278 struct iso_volume_descriptor *vdp = 0;
279 struct iso_primary_descriptor *pri = NULL;
280 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
281 struct iso_supplementary_descriptor *sup = NULL;
282 struct iso_directory_record *rootp;
283 int logical_block_size;
285 if (!(mp->mnt_flag & MNT_RDONLY))
289 * Disallow multiple mounts of the same device.
290 * Disallow mounting of a device that is currently in use
291 * (except for root, which might share swap device for miniroot).
292 * Flush out any old buffers remaining from a previous use.
294 if ((error = vfs_mountedon(devvp)))
296 if (count_udev(devvp->v_udev) > 0 && devvp != rootvp)
298 if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
301 vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
302 error = VOP_OPEN(devvp, FREAD, FSCRED, td);
303 VOP_UNLOCK(devvp, NULL, 0, td);
307 if (dev->si_iosize_max != 0)
308 mp->mnt_iosize_max = dev->si_iosize_max;
309 if (mp->mnt_iosize_max > MAXPHYS)
310 mp->mnt_iosize_max = MAXPHYS;
314 /* This is the "logical sector size". The standard says this
315 * should be 2048 or the physical sector size on the device,
316 * whichever is greater. For now, we'll just use a constant.
318 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
321 for (iso_blknum = 16 + argp->ssector;
322 iso_blknum < 100 + argp->ssector;
324 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
325 iso_bsize, &bp)) != 0)
328 vdp = (struct iso_volume_descriptor *)bp->b_data;
329 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
330 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
331 sizeof vdp->id) != 0) {
337 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
342 pri = (struct iso_primary_descriptor *)vdp;
344 (struct iso_sierra_primary_descriptor *)vdp;
348 case ISO_VD_SUPPLEMENTARY:
352 sup = (struct iso_supplementary_descriptor *)vdp;
354 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
355 if (bcmp(sup->escape, "%/@", 3) == 0)
357 if (bcmp(sup->escape, "%/C", 3) == 0)
359 if (bcmp(sup->escape, "%/E", 3) == 0)
362 if ((isonum_711 (sup->flags) & 1) &&
363 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
392 isonum_723 (high_sierra?
393 pri_sierra->logical_block_size:
394 pri->logical_block_size);
396 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
397 || (logical_block_size & (logical_block_size - 1)) != 0) {
402 rootp = (struct iso_directory_record *)
404 pri_sierra->root_directory_record:
405 pri->root_directory_record);
407 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
408 isomp->logical_block_size = logical_block_size;
409 isomp->volume_space_size =
410 isonum_733 (high_sierra?
411 pri_sierra->volume_space_size:
412 pri->volume_space_size);
413 isomp->joliet_level = 0;
415 * Since an ISO9660 multi-session CD can also access previous
416 * sessions, we have to include them into the space consider-
417 * ations. This doesn't yield a very accurate number since
418 * parts of the old sessions might be inaccessible now, but we
419 * can't do much better. This is also important for the NFS
420 * filehandle validation.
422 isomp->volume_space_size += argp->ssector;
423 bcopy (rootp, isomp->root, sizeof isomp->root);
424 isomp->root_extent = isonum_733 (rootp->extent);
425 isomp->root_size = isonum_733 (rootp->size);
427 isomp->im_bmask = logical_block_size - 1;
428 isomp->im_bshift = ffs(logical_block_size) - 1;
430 pribp->b_flags |= B_AGE;
434 mp->mnt_data = (qaddr_t)isomp;
435 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
436 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
437 mp->mnt_maxsymlinklen = 0;
438 mp->mnt_flag |= MNT_LOCAL;
439 isomp->im_mountp = mp;
441 isomp->im_devvp = devvp;
443 dev->si_mountpoint = mp;
445 /* Check the Rock Ridge Extention support */
446 if (!(argp->flags & ISOFSMNT_NORRIP)) {
447 if ((error = bread(isomp->im_devvp,
448 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
449 (isomp->im_bshift - DEV_BSHIFT),
450 isomp->logical_block_size, &bp)) != 0)
453 rootp = (struct iso_directory_record *)bp->b_data;
455 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
456 argp->flags |= ISOFSMNT_NORRIP;
458 argp->flags &= ~ISOFSMNT_GENS;
462 * The contents are valid,
463 * but they will get reread as part of another vnode, so...
465 bp->b_flags |= B_AGE;
469 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
470 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
473 /* this effectively ignores all the mount flags */
474 log(LOG_INFO, "cd9660: High Sierra Format\n");
475 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
477 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
479 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
481 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
482 isomp->iso_ftype = ISO_FTYPE_9660;
485 log(LOG_INFO, "cd9660: RockRidge Extension\n");
486 isomp->iso_ftype = ISO_FTYPE_RRIP;
490 /* Decide whether to use the Joliet descriptor */
492 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
493 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
494 rootp = (struct iso_directory_record *)
495 sup->root_directory_record;
496 bcopy (rootp, isomp->root, sizeof isomp->root);
497 isomp->root_extent = isonum_733 (rootp->extent);
498 isomp->root_size = isonum_733 (rootp->size);
499 isomp->joliet_level = joliet_level;
500 supbp->b_flags |= B_AGE;
510 dev->si_mountpoint = NULL;
518 (void)VOP_CLOSE(devvp, FREAD, td);
520 free((caddr_t)isomp, M_ISOFSMNT);
521 mp->mnt_data = (qaddr_t)0;
527 * unmount system call
530 cd9660_unmount(struct mount *mp, int mntflags, struct thread *td)
532 struct iso_mnt *isomp;
533 int error, flags = 0;
535 if (mntflags & MNT_FORCE)
542 if ((error = vflush(mp, 0, flags)))
545 isomp = VFSTOISOFS(mp);
547 isomp->im_devvp->v_rdev->si_mountpoint = NULL;
548 error = VOP_CLOSE(isomp->im_devvp, FREAD, td);
549 vrele(isomp->im_devvp);
550 free((caddr_t)isomp, M_ISOFSMNT);
551 mp->mnt_data = (qaddr_t)0;
552 mp->mnt_flag &= ~MNT_LOCAL;
557 * Return root of a filesystem
560 cd9660_root(struct mount *mp, struct vnode **vpp)
562 struct iso_mnt *imp = VFSTOISOFS(mp);
563 struct iso_directory_record *dp =
564 (struct iso_directory_record *)imp->root;
565 ino_t ino = isodirino(dp, imp);
568 * With RRIP we must use the `.' entry of the root directory.
569 * Simply tell vget, that it's a relocated directory.
571 return (cd9660_vget_internal(mp, ino, vpp,
572 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
576 * Get file system statistics.
579 cd9660_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
581 struct iso_mnt *isomp;
583 isomp = VFSTOISOFS(mp);
585 sbp->f_bsize = isomp->logical_block_size;
586 sbp->f_iosize = sbp->f_bsize; /* XXX */
587 sbp->f_blocks = isomp->volume_space_size;
588 sbp->f_bfree = 0; /* total free blocks */
589 sbp->f_bavail = 0; /* blocks free for non superuser */
590 sbp->f_files = 0; /* total files */
591 sbp->f_ffree = 0; /* free file nodes */
592 if (sbp != &mp->mnt_stat) {
593 sbp->f_type = mp->mnt_vfc->vfc_typenum;
594 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
595 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
601 * File handle to vnode
603 * Have to be really careful about stale file handles:
604 * - check that the inode number is in range
605 * - call iget() to get the locked inode
606 * - check for an unallocated inode (i_mode == 0)
607 * - check that the generation number matches
619 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
621 struct ifid *ifhp = (struct ifid *)fhp;
627 printf("fhtovp: ino %d, start %ld\n",
628 ifhp->ifid_ino, ifhp->ifid_start);
631 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
636 if (ip->inode.iso_mode == 0) {
646 cd9660_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
647 struct ucred **credanonp)
652 imp = VFSTOISOFS(mp);
655 * Get the export permission structure for this <mp, client> tuple.
657 np = vfs_export_lookup(mp, &imp->im_export, nam);
661 *exflagsp = np->netc_exflags;
662 *credanonp = &np->netc_anon;
667 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
672 * It would be nice if we didn't always set the `relocated' flag
673 * and force the extra read, but I don't want to think about fixing
676 return (cd9660_vget_internal(mp, ino, vpp,
678 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
682 (struct iso_directory_record *)0));
686 cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
687 int relocated, struct iso_directory_record *isodir)
696 imp = VFSTOISOFS(mp);
698 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
701 /* Allocate a new vnode/iso_node. */
702 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
706 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
708 bzero((caddr_t)ip, sizeof(struct iso_node));
709 lockinit(&ip->i_lock, 0, "isonode", 0, 0);
716 * Put it onto its hash chain and lock it so that other requests for
717 * this inode will block if they arrive while we are sleeping waiting
718 * for old data structures to be purged or for the contents of the
719 * disk portion of this inode to be read.
726 lbn = lblkno(imp, ino);
727 if (lbn >= imp->volume_space_size) {
729 printf("fhtovp: lbn exceed volume space %d\n", lbn);
733 off = blkoff(imp, ino);
734 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
736 printf("fhtovp: crosses block boundary %d\n",
737 off + ISO_DIRECTORY_RECORD_SIZE);
741 error = bread(imp->im_devvp,
742 lbn << (imp->im_bshift - DEV_BSHIFT),
743 imp->logical_block_size, &bp);
747 printf("fhtovp: bread error %d\n",error);
750 isodir = (struct iso_directory_record *)(bp->b_data + off);
752 if (off + isonum_711(isodir->length) >
753 imp->logical_block_size) {
757 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
758 off +isonum_711(isodir->length), off,
759 isonum_711(isodir->length));
764 if (isonum_733(isodir->extent) +
765 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
768 printf("fhtovp: file start miss %d vs %d\n",
769 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
778 ip->i_devvp = imp->im_devvp;
783 * On relocated directories we must
784 * read the `.' entry out of a dir.
786 ip->iso_start = ino >> imp->im_bshift;
789 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
793 isodir = (struct iso_directory_record *)bp->b_data;
796 ip->iso_extent = isonum_733(isodir->extent);
797 ip->i_size = isonum_733(isodir->size);
798 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
801 * Setup time stamp, attribute
804 switch (imp->iso_ftype) {
805 default: /* ISO_FTYPE_9660 */
809 if ((imp->im_flags & ISOFSMNT_EXTATT)
810 && (off = isonum_711(isodir->ext_attr_length)))
811 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
815 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
816 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
822 cd9660_rrip_analyze(isodir, ip, imp);
830 * Initialize the associated vnode
832 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
834 vp->v_op = cd9660_fifoop_p;
838 vp->v_op = cd9660_specop_p;
839 addaliasu(vp, ip->inode.iso_rdev);
845 if (ip->iso_extent == imp->root_extent)
849 * XXX need generation number?
857 * Vnode pointer to File handle
861 cd9660_vptofh(struct vnode *vp, struct fid *fhp)
863 struct iso_node *ip = VTOI(vp);
866 ifhp = (struct ifid *)fhp;
867 ifhp->ifid_len = sizeof(struct ifid);
869 ifhp->ifid_ino = ip->i_number;
870 ifhp->ifid_start = ip->iso_start;
873 printf("vptofh: ino %d, start %ld\n",
874 ifhp->ifid_ino,ifhp->ifid_start);