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 $
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
46 #include <sys/kernel.h>
47 #include <sys/vnode.h>
48 #include <sys/mount.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
55 #include <sys/syslog.h>
57 #include <vm/vm_zone.h>
59 #include <isofs/cd9660/iso.h>
60 #include <isofs/cd9660/iso_rrip.h>
61 #include <isofs/cd9660/cd9660_node.h>
62 #include <isofs/cd9660/cd9660_mount.h>
64 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
65 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
67 static int cd9660_mount __P((struct mount *,
68 char *, caddr_t, struct nameidata *, struct proc *));
69 static int cd9660_unmount __P((struct mount *, int, struct proc *));
70 static int cd9660_root __P((struct mount *, struct vnode **));
71 static int cd9660_statfs __P((struct mount *, struct statfs *, struct proc *));
72 static int cd9660_vget __P((struct mount *, ino_t, struct vnode **));
73 static int cd9660_fhtovp __P((struct mount *, struct fid *, struct vnode **));
74 static int cd9660_checkexp __P((struct mount *, struct sockaddr *,
75 int *, struct ucred **));
76 static int cd9660_vptofh __P((struct vnode *, struct fid *));
78 static struct vfsops cd9660_vfsops = {
94 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
95 MODULE_VERSION(cd9660, 1);
99 * Called by vfs_mountroot when iso is going to be mounted as root.
102 static int iso_get_ssector __P((dev_t dev, struct proc *p));
103 static int iso_mountfs __P((struct vnode *devvp, struct mount *mp,
104 struct proc *p, struct iso_args *argp));
107 * Try to find the start of the last data track on this CD-ROM. This
108 * is used to mount the last session of a multi-session CD. Bail out
109 * and return 0 if we fail, this is always a safe bet.
112 iso_get_ssector(dev, p)
116 struct ioc_toc_header h;
117 struct ioc_read_toc_single_entry t;
123 ioctlp = bd->d_ioctl;
127 if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, p) != 0)
130 for (i = h.ending_track; i >= 0; i--) {
131 t.address_format = CD_LBA_FORMAT;
133 if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, p) != 0)
135 if ((t.entry.control & 4) != 0)
136 /* found a data track */
143 return ntohl(t.entry.addr.lba);
146 static int iso_mountroot __P((struct mount *mp, struct proc *p));
153 struct iso_args args;
156 if ((error = bdevvp(rootdev, &rootvp))) {
157 printf("iso_mountroot: can't find rootvp\n");
160 args.flags = ISOFSMNT_ROOT;
162 vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, p);
163 error = VOP_OPEN(rootvp, FREAD, FSCRED, p);
164 VOP_UNLOCK(rootvp, 0, p);
168 args.ssector = iso_get_ssector(rootdev, p);
170 (void)VOP_CLOSE(rootvp, FREAD, NOCRED, p);
173 printf("iso_mountroot(): using session at block %d\n",
175 if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0)
178 (void)cd9660_statfs(mp, &mp->mnt_stat, p);
188 cd9660_mount(mp, path, data, ndp, p)
189 register struct mount *mp;
192 struct nameidata *ndp;
196 struct iso_args args;
200 struct iso_mnt *imp = 0;
202 if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
203 return (iso_mountroot(mp, p));
205 if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
208 if ((mp->mnt_flag & MNT_RDONLY) == 0)
212 * If updating, check whether changing from read-only to
213 * read/write; if there is no device name, that's all we do.
215 if (mp->mnt_flag & MNT_UPDATE) {
216 imp = VFSTOISOFS(mp);
218 return (vfs_export(mp, &imp->im_export, &args.export));
221 * Not an update, or updating the name: look up the name
222 * and verify that it refers to a sensible block device.
224 NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
225 if ((error = namei(ndp)))
227 NDFREE(ndp, NDF_ONLY_PNBUF);
230 if (!vn_isdisk(devvp, &error)) {
236 * Verify that user has necessary permissions on the device,
237 * or has superuser abilities
240 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
241 error = VOP_ACCESS(devvp, accessmode, p->p_ucred, p);
248 VOP_UNLOCK(devvp, 0, p);
250 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
251 error = iso_mountfs(devvp, mp, p, &args);
253 if (devvp != imp->im_devvp)
254 error = EINVAL; /* needs translation */
262 imp = VFSTOISOFS(mp);
263 (void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
264 bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
265 (void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
267 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
268 (void) cd9660_statfs(mp, &mp->mnt_stat, p);
273 * Common code for mount and mountroot
276 iso_mountfs(devvp, mp, p, argp)
277 register struct vnode *devvp;
280 struct iso_args *argp;
282 register struct iso_mnt *isomp = (struct iso_mnt *)0;
283 struct buf *bp = NULL;
284 struct buf *pribp = NULL, *supbp = NULL;
285 dev_t dev = devvp->v_rdev;
292 struct iso_volume_descriptor *vdp = 0;
293 struct iso_primary_descriptor *pri = NULL;
294 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
295 struct iso_supplementary_descriptor *sup = NULL;
296 struct iso_directory_record *rootp;
297 int logical_block_size;
299 if (!(mp->mnt_flag & MNT_RDONLY))
303 * Disallow multiple mounts of the same device.
304 * Disallow mounting of a device that is currently in use
305 * (except for root, which might share swap device for miniroot).
306 * Flush out any old buffers remaining from a previous use.
308 if ((error = vfs_mountedon(devvp)))
310 if (vcount(devvp) > 1 && devvp != rootvp)
312 if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)))
315 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
316 error = VOP_OPEN(devvp, FREAD, FSCRED, p);
317 VOP_UNLOCK(devvp, 0, p);
320 if (devvp->v_rdev->si_iosize_max != 0)
321 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
322 if (mp->mnt_iosize_max > MAXPHYS)
323 mp->mnt_iosize_max = MAXPHYS;
327 /* This is the "logical sector size". The standard says this
328 * should be 2048 or the physical sector size on the device,
329 * whichever is greater. For now, we'll just use a constant.
331 iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
334 for (iso_blknum = 16 + argp->ssector;
335 iso_blknum < 100 + argp->ssector;
337 if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
338 iso_bsize, NOCRED, &bp)) != 0)
341 vdp = (struct iso_volume_descriptor *)bp->b_data;
342 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
343 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
344 sizeof vdp->id) != 0) {
350 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
355 pri = (struct iso_primary_descriptor *)vdp;
357 (struct iso_sierra_primary_descriptor *)vdp;
361 case ISO_VD_SUPPLEMENTARY:
365 sup = (struct iso_supplementary_descriptor *)vdp;
367 if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
368 if (bcmp(sup->escape, "%/@", 3) == 0)
370 if (bcmp(sup->escape, "%/C", 3) == 0)
372 if (bcmp(sup->escape, "%/E", 3) == 0)
375 if ((isonum_711 (sup->flags) & 1) &&
376 (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
405 isonum_723 (high_sierra?
406 pri_sierra->logical_block_size:
407 pri->logical_block_size);
409 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
410 || (logical_block_size & (logical_block_size - 1)) != 0) {
415 rootp = (struct iso_directory_record *)
417 pri_sierra->root_directory_record:
418 pri->root_directory_record);
420 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
421 bzero((caddr_t)isomp, sizeof *isomp);
422 isomp->logical_block_size = logical_block_size;
423 isomp->volume_space_size =
424 isonum_733 (high_sierra?
425 pri_sierra->volume_space_size:
426 pri->volume_space_size);
427 isomp->joliet_level = 0;
429 * Since an ISO9660 multi-session CD can also access previous
430 * sessions, we have to include them into the space consider-
431 * ations. This doesn't yield a very accurate number since
432 * parts of the old sessions might be inaccessible now, but we
433 * can't do much better. This is also important for the NFS
434 * filehandle validation.
436 isomp->volume_space_size += argp->ssector;
437 bcopy (rootp, isomp->root, sizeof isomp->root);
438 isomp->root_extent = isonum_733 (rootp->extent);
439 isomp->root_size = isonum_733 (rootp->size);
441 isomp->im_bmask = logical_block_size - 1;
442 isomp->im_bshift = ffs(logical_block_size) - 1;
444 pribp->b_flags |= B_AGE;
448 mp->mnt_data = (qaddr_t)isomp;
449 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
450 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
451 mp->mnt_maxsymlinklen = 0;
452 mp->mnt_flag |= MNT_LOCAL;
453 isomp->im_mountp = mp;
455 isomp->im_devvp = devvp;
457 devvp->v_specmountpoint = mp;
459 /* Check the Rock Ridge Extention support */
460 if (!(argp->flags & ISOFSMNT_NORRIP)) {
461 if ((error = bread(isomp->im_devvp,
462 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
463 (isomp->im_bshift - DEV_BSHIFT),
464 isomp->logical_block_size, NOCRED, &bp)) != 0)
467 rootp = (struct iso_directory_record *)bp->b_data;
469 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
470 argp->flags |= ISOFSMNT_NORRIP;
472 argp->flags &= ~ISOFSMNT_GENS;
476 * The contents are valid,
477 * but they will get reread as part of another vnode, so...
479 bp->b_flags |= B_AGE;
483 isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
484 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
487 /* this effectively ignores all the mount flags */
488 log(LOG_INFO, "cd9660: High Sierra Format\n");
489 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
491 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
493 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
495 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
496 isomp->iso_ftype = ISO_FTYPE_9660;
499 log(LOG_INFO, "cd9660: RockRidge Extension\n");
500 isomp->iso_ftype = ISO_FTYPE_RRIP;
504 /* Decide whether to use the Joliet descriptor */
506 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
507 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
508 rootp = (struct iso_directory_record *)
509 sup->root_directory_record;
510 bcopy (rootp, isomp->root, sizeof isomp->root);
511 isomp->root_extent = isonum_733 (rootp->extent);
512 isomp->root_size = isonum_733 (rootp->size);
513 isomp->joliet_level = joliet_level;
514 supbp->b_flags |= B_AGE;
524 devvp->v_specmountpoint = NULL;
532 (void)VOP_CLOSE(devvp, FREAD, NOCRED, p);
534 free((caddr_t)isomp, M_ISOFSMNT);
535 mp->mnt_data = (qaddr_t)0;
541 * unmount system call
544 cd9660_unmount(mp, mntflags, p)
549 register struct iso_mnt *isomp;
550 int error, flags = 0;
552 if (mntflags & MNT_FORCE)
559 if ((error = vflush(mp, 0, flags)))
562 isomp = VFSTOISOFS(mp);
564 isomp->im_devvp->v_specmountpoint = NULL;
565 error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
566 vrele(isomp->im_devvp);
567 free((caddr_t)isomp, M_ISOFSMNT);
568 mp->mnt_data = (qaddr_t)0;
569 mp->mnt_flag &= ~MNT_LOCAL;
574 * Return root of a filesystem
581 struct iso_mnt *imp = VFSTOISOFS(mp);
582 struct iso_directory_record *dp =
583 (struct iso_directory_record *)imp->root;
584 ino_t ino = isodirino(dp, imp);
587 * With RRIP we must use the `.' entry of the root directory.
588 * Simply tell vget, that it's a relocated directory.
590 return (cd9660_vget_internal(mp, ino, vpp,
591 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
595 * Get file system statistics.
598 cd9660_statfs(mp, sbp, p)
600 register struct statfs *sbp;
603 register struct iso_mnt *isomp;
605 isomp = VFSTOISOFS(mp);
607 sbp->f_bsize = isomp->logical_block_size;
608 sbp->f_iosize = sbp->f_bsize; /* XXX */
609 sbp->f_blocks = isomp->volume_space_size;
610 sbp->f_bfree = 0; /* total free blocks */
611 sbp->f_bavail = 0; /* blocks free for non superuser */
612 sbp->f_files = 0; /* total files */
613 sbp->f_ffree = 0; /* free file nodes */
614 if (sbp != &mp->mnt_stat) {
615 sbp->f_type = mp->mnt_vfc->vfc_typenum;
616 bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
617 bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
623 * File handle to vnode
625 * Have to be really careful about stale file handles:
626 * - check that the inode number is in range
627 * - call iget() to get the locked inode
628 * - check for an unallocated inode (i_mode == 0)
629 * - check that the generation number matches
641 cd9660_fhtovp(mp, fhp, vpp)
642 register struct mount *mp;
646 struct ifid *ifhp = (struct ifid *)fhp;
647 register struct iso_node *ip;
652 printf("fhtovp: ino %d, start %ld\n",
653 ifhp->ifid_ino, ifhp->ifid_start);
656 if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
661 if (ip->inode.iso_mode == 0) {
671 cd9660_checkexp(mp, nam, exflagsp, credanonp)
673 struct sockaddr *nam;
675 struct ucred **credanonp;
677 register struct netcred *np;
678 register struct iso_mnt *imp;
680 imp = VFSTOISOFS(mp);
683 * Get the export permission structure for this <mp, client> tuple.
685 np = vfs_export_lookup(mp, &imp->im_export, nam);
689 *exflagsp = np->netc_exflags;
690 *credanonp = &np->netc_anon;
695 cd9660_vget(mp, ino, vpp)
703 * It would be nice if we didn't always set the `relocated' flag
704 * and force the extra read, but I don't want to think about fixing
707 return (cd9660_vget_internal(mp, ino, vpp,
709 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
713 (struct iso_directory_record *)0));
717 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
722 struct iso_directory_record *isodir;
731 imp = VFSTOISOFS(mp);
733 if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
736 /* Allocate a new vnode/iso_node. */
737 if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
741 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
743 bzero((caddr_t)ip, sizeof(struct iso_node));
744 lockinit(&ip->i_lock, PINOD, "isonode", 0, 0);
751 * Put it onto its hash chain and lock it so that other requests for
752 * this inode will block if they arrive while we are sleeping waiting
753 * for old data structures to be purged or for the contents of the
754 * disk portion of this inode to be read.
761 lbn = lblkno(imp, ino);
762 if (lbn >= imp->volume_space_size) {
764 printf("fhtovp: lbn exceed volume space %d\n", lbn);
768 off = blkoff(imp, ino);
769 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
771 printf("fhtovp: crosses block boundary %d\n",
772 off + ISO_DIRECTORY_RECORD_SIZE);
776 error = bread(imp->im_devvp,
777 lbn << (imp->im_bshift - DEV_BSHIFT),
778 imp->logical_block_size, NOCRED, &bp);
782 printf("fhtovp: bread error %d\n",error);
785 isodir = (struct iso_directory_record *)(bp->b_data + off);
787 if (off + isonum_711(isodir->length) >
788 imp->logical_block_size) {
792 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
793 off +isonum_711(isodir->length), off,
794 isonum_711(isodir->length));
799 if (isonum_733(isodir->extent) +
800 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
803 printf("fhtovp: file start miss %d vs %d\n",
804 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
813 ip->i_devvp = imp->im_devvp;
818 * On relocated directories we must
819 * read the `.' entry out of a dir.
821 ip->iso_start = ino >> imp->im_bshift;
824 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
828 isodir = (struct iso_directory_record *)bp->b_data;
831 ip->iso_extent = isonum_733(isodir->extent);
832 ip->i_size = isonum_733(isodir->size);
833 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
836 * Setup time stamp, attribute
839 switch (imp->iso_ftype) {
840 default: /* ISO_FTYPE_9660 */
844 if ((imp->im_flags & ISOFSMNT_EXTATT)
845 && (off = isonum_711(isodir->ext_attr_length)))
846 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
850 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
851 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
857 cd9660_rrip_analyze(isodir, ip, imp);
865 * Initialize the associated vnode
867 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
869 vp->v_op = cd9660_fifoop_p;
873 vp->v_op = cd9660_specop_p;
874 addaliasu(vp, ip->inode.iso_rdev);
880 if (ip->iso_extent == imp->root_extent)
884 * XXX need generation number?
892 * Vnode pointer to File handle
896 cd9660_vptofh(vp, fhp)
900 register struct iso_node *ip = VTOI(vp);
901 register struct ifid *ifhp;
903 ifhp = (struct ifid *)fhp;
904 ifhp->ifid_len = sizeof(struct ifid);
906 ifhp->ifid_ino = ip->i_number;
907 ifhp->ifid_start = ip->iso_start;
910 printf("vptofh: ino %d, start %ld\n",
911 ifhp->ifid_ino,ifhp->ifid_start);