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 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
55 #include <sys/syslog.h>
56 #include <sys/iconv.h>
58 #include <fs/cd9660/iso.h>
59 #include <fs/cd9660/iso_rrip.h>
60 #include <fs/cd9660/cd9660_node.h>
61 #include <fs/cd9660/cd9660_mount.h>
63 #include <geom/geom.h>
64 #include <geom/geom_vfs.h>
66 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure");
67 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part");
69 struct iconv_functions *cd9660_iconv = NULL;
71 static vfs_mount_t cd9660_mount;
72 static vfs_cmount_t cd9660_cmount;
73 static vfs_unmount_t cd9660_unmount;
74 static vfs_root_t cd9660_root;
75 static vfs_statfs_t cd9660_statfs;
76 static vfs_vget_t cd9660_vget;
77 static vfs_fhtovp_t cd9660_fhtovp;
79 static struct vfsops cd9660_vfsops = {
80 .vfs_fhtovp = cd9660_fhtovp,
81 .vfs_mount = cd9660_mount,
82 .vfs_cmount = cd9660_cmount,
83 .vfs_root = cd9660_root,
84 .vfs_statfs = cd9660_statfs,
85 .vfs_unmount = cd9660_unmount,
86 .vfs_vget = cd9660_vget,
88 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
89 MODULE_VERSION(cd9660, 1);
91 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
99 cd9660_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
101 struct iso_args args;
104 error = copyin(data, &args, sizeof args);
108 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
109 ma = mount_arg(ma, "export", &args.export, sizeof args.export);
110 ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
111 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
112 ma = mount_argf(ma, "ssector", "%u", args.ssector);
113 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
114 ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
115 ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
116 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
118 args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
119 ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
121 error = kernel_mount(ma, flags);
127 cd9660_mount(struct mount *mp, struct thread *td)
133 struct nameidata ndp;
134 struct iso_mnt *imp = 0;
137 * Unconditionally mount as read-only.
140 mp->mnt_flag |= MNT_RDONLY;
143 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
147 imp = VFSTOISOFS(mp);
149 if (mp->mnt_flag & MNT_UPDATE) {
150 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
154 * Not an update, or updating the name: look up the name
155 * and verify that it refers to a sensible block device.
157 NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
158 if ((error = namei(&ndp)))
160 NDFREE(&ndp, NDF_ONLY_PNBUF);
163 if (!vn_isdisk(devvp, &error)) {
169 * Verify that user has necessary permissions on the device,
170 * or has superuser abilities
173 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
174 error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
176 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
181 VOP_UNLOCK(devvp, 0, td);
183 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
184 error = iso_mountfs(devvp, mp, td);
186 if (devvp != imp->im_devvp)
187 error = EINVAL; /* needs translation */
195 vfs_mountedfrom(mp, fspec);
200 * Common code for mount and mountroot
203 iso_mountfs(devvp, mp, td)
208 struct iso_mnt *isomp = (struct iso_mnt *)0;
209 struct buf *bp = NULL;
210 struct buf *pribp = NULL, *supbp = NULL;
211 struct cdev *dev = devvp->v_rdev;
217 struct iso_volume_descriptor *vdp = 0;
218 struct iso_primary_descriptor *pri = NULL;
219 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
220 struct iso_supplementary_descriptor *sup = NULL;
221 struct iso_directory_record *rootp;
222 int logical_block_size, ssector;
223 struct g_consumer *cp;
225 char *cs_local, *cs_disk;
227 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
230 error = g_vfs_open(devvp, &cp, "cd9660", 0);
233 VOP_UNLOCK(devvp, 0, td);
236 if (devvp->v_rdev->si_iosize_max != 0)
237 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
238 if (mp->mnt_iosize_max > MAXPHYS)
239 mp->mnt_iosize_max = MAXPHYS;
241 bo = &devvp->v_bufobj;
243 bo->bo_ops = g_vfs_bufops;
245 /* This is the "logical sector size". The standard says this
246 * should be 2048 or the physical sector size on the device,
247 * whichever is greater.
249 if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
258 iso_bsize = cp->provider->sectorsize;
261 if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
263 for (iso_blknum = 16 + ssector;
264 iso_blknum < 100 + ssector;
266 if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
267 iso_bsize, NOCRED, &bp)) != 0)
270 vdp = (struct iso_volume_descriptor *)bp->b_data;
271 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
272 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
273 sizeof vdp->id) != 0) {
279 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
284 pri = (struct iso_primary_descriptor *)vdp;
286 (struct iso_sierra_primary_descriptor *)vdp;
290 case ISO_VD_SUPPLEMENTARY:
294 sup = (struct iso_supplementary_descriptor *)vdp;
296 if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
297 if (bcmp(sup->escape, "%/@", 3) == 0)
299 if (bcmp(sup->escape, "%/C", 3) == 0)
301 if (bcmp(sup->escape, "%/E", 3) == 0)
304 if ((isonum_711 (sup->flags) & 1) &&
305 !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
334 isonum_723 (high_sierra?
335 pri_sierra->logical_block_size:
336 pri->logical_block_size);
338 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
339 || (logical_block_size & (logical_block_size - 1)) != 0) {
344 rootp = (struct iso_directory_record *)
346 pri_sierra->root_directory_record:
347 pri->root_directory_record);
349 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
352 isomp->logical_block_size = logical_block_size;
353 isomp->volume_space_size =
354 isonum_733 (high_sierra?
355 pri_sierra->volume_space_size:
356 pri->volume_space_size);
357 isomp->joliet_level = 0;
359 * Since an ISO9660 multi-session CD can also access previous
360 * sessions, we have to include them into the space consider-
361 * ations. This doesn't yield a very accurate number since
362 * parts of the old sessions might be inaccessible now, but we
363 * can't do much better. This is also important for the NFS
364 * filehandle validation.
366 isomp->volume_space_size += ssector;
367 bcopy (rootp, isomp->root, sizeof isomp->root);
368 isomp->root_extent = isonum_733 (rootp->extent);
369 isomp->root_size = isonum_733 (rootp->size);
371 isomp->im_bmask = logical_block_size - 1;
372 isomp->im_bshift = ffs(logical_block_size) - 1;
374 pribp->b_flags |= B_AGE;
378 mp->mnt_data = (qaddr_t)isomp;
379 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
380 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
381 mp->mnt_maxsymlinklen = 0;
383 mp->mnt_flag |= MNT_LOCAL;
385 isomp->im_mountp = mp;
387 isomp->im_devvp = devvp;
389 vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
390 vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
391 vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
392 vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
393 vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
395 /* Check the Rock Ridge Extension support */
396 if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
397 if ((error = bread(isomp->im_devvp,
398 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
399 (isomp->im_bshift - DEV_BSHIFT),
400 isomp->logical_block_size, NOCRED, &bp)) != 0)
403 rootp = (struct iso_directory_record *)bp->b_data;
405 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
406 isomp->im_flags |= ISOFSMNT_NORRIP;
408 isomp->im_flags &= ~ISOFSMNT_GENS;
412 * The contents are valid,
413 * but they will get reread as part of another vnode, so...
415 bp->b_flags |= B_AGE;
420 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
421 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
424 cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
427 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
428 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
430 isomp->im_d2l = NULL;
431 isomp->im_l2d = NULL;
435 /* this effectively ignores all the mount flags */
437 log(LOG_INFO, "cd9660: High Sierra Format\n");
438 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
440 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
442 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
444 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
445 isomp->iso_ftype = ISO_FTYPE_9660;
449 log(LOG_INFO, "cd9660: RockRidge Extension\n");
450 isomp->iso_ftype = ISO_FTYPE_RRIP;
454 /* Decide whether to use the Joliet descriptor */
456 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
458 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
460 rootp = (struct iso_directory_record *)
461 sup->root_directory_record;
462 bcopy (rootp, isomp->root, sizeof isomp->root);
463 isomp->root_extent = isonum_733 (rootp->extent);
464 isomp->root_size = isonum_733 (rootp->size);
465 isomp->joliet_level = joliet_level;
466 supbp->b_flags |= B_AGE;
490 free((caddr_t)isomp, M_ISOFSMNT);
491 mp->mnt_data = (qaddr_t)0;
497 * unmount system call
500 cd9660_unmount(mp, mntflags, td)
505 struct iso_mnt *isomp;
506 int error, flags = 0;
508 if (mntflags & MNT_FORCE)
515 if ((error = vflush(mp, 0, flags, td)))
518 isomp = VFSTOISOFS(mp);
520 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
522 cd9660_iconv->close(isomp->im_d2l);
524 cd9660_iconv->close(isomp->im_l2d);
528 g_vfs_close(isomp->im_cp, td);
531 vrele(isomp->im_devvp);
532 free((caddr_t)isomp, M_ISOFSMNT);
533 mp->mnt_data = (qaddr_t)0;
535 mp->mnt_flag &= ~MNT_LOCAL;
541 * Return root of a filesystem
544 cd9660_root(mp, flags, vpp, td)
550 struct iso_mnt *imp = VFSTOISOFS(mp);
551 struct iso_directory_record *dp =
552 (struct iso_directory_record *)imp->root;
553 ino_t ino = isodirino(dp, imp);
556 * With RRIP we must use the `.' entry of the root directory.
557 * Simply tell vget, that it's a relocated directory.
559 return (cd9660_vget_internal(mp, ino, LK_EXCLUSIVE, vpp,
560 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
564 * Get filesystem statistics.
567 cd9660_statfs(mp, sbp, td)
572 struct iso_mnt *isomp;
574 isomp = VFSTOISOFS(mp);
576 sbp->f_bsize = isomp->logical_block_size;
577 sbp->f_iosize = sbp->f_bsize; /* XXX */
578 sbp->f_blocks = isomp->volume_space_size;
579 sbp->f_bfree = 0; /* total free blocks */
580 sbp->f_bavail = 0; /* blocks free for non superuser */
581 sbp->f_files = 0; /* total files */
582 sbp->f_ffree = 0; /* free file nodes */
587 * File handle to vnode
589 * Have to be really careful about stale file handles:
590 * - check that the inode number is in range
591 * - call iget() to get the locked inode
592 * - check for an unallocated inode (i_mode == 0)
593 * - check that the generation number matches
598 cd9660_fhtovp(mp, fhp, vpp)
603 struct ifid *ifhp = (struct ifid *)fhp;
609 printf("fhtovp: ino %d, start %ld\n",
610 ifhp->ifid_ino, ifhp->ifid_start);
613 if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
618 if (ip->inode.iso_mode == 0) {
624 vnode_create_vobject(*vpp, ip->i_size, curthread);
629 cd9660_vget(mp, ino, flags, vpp)
638 * It would be nice if we didn't always set the `relocated' flag
639 * and force the extra read, but I don't want to think about fixing
642 return (cd9660_vget_internal(mp, ino, flags, vpp,
644 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
648 (struct iso_directory_record *)0));
652 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
658 struct iso_directory_record *isodir;
669 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
670 if (error || *vpp != NULL)
673 imp = VFSTOISOFS(mp);
676 /* Allocate a new vnode/iso_node. */
677 if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
681 MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
687 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL, td);
688 error = insmntque(vp, mp);
690 free(ip, M_ISOFSNODE);
694 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
695 if (error || *vpp != NULL)
701 lbn = lblkno(imp, ino);
702 if (lbn >= imp->volume_space_size) {
704 printf("fhtovp: lbn exceed volume space %d\n", lbn);
708 off = blkoff(imp, ino);
709 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
711 printf("fhtovp: crosses block boundary %d\n",
712 off + ISO_DIRECTORY_RECORD_SIZE);
716 error = bread(imp->im_devvp,
717 lbn << (imp->im_bshift - DEV_BSHIFT),
718 imp->logical_block_size, NOCRED, &bp);
722 printf("fhtovp: bread error %d\n",error);
725 isodir = (struct iso_directory_record *)(bp->b_data + off);
727 if (off + isonum_711(isodir->length) >
728 imp->logical_block_size) {
732 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
733 off +isonum_711(isodir->length), off,
734 isonum_711(isodir->length));
739 if (isonum_733(isodir->extent) +
740 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
743 printf("fhtovp: file start miss %d vs %d\n",
744 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
757 * On relocated directories we must
758 * read the `.' entry out of a dir.
760 ip->iso_start = ino >> imp->im_bshift;
763 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
767 isodir = (struct iso_directory_record *)bp->b_data;
770 ip->iso_extent = isonum_733(isodir->extent);
771 ip->i_size = isonum_733(isodir->size);
772 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
775 * Setup time stamp, attribute
778 switch (imp->iso_ftype) {
779 default: /* ISO_FTYPE_9660 */
783 if ((imp->im_flags & ISOFSMNT_EXTATT)
784 && (off = isonum_711(isodir->ext_attr_length)))
785 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
789 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
790 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
796 cd9660_rrip_analyze(isodir, ip, imp);
804 * Initialize the associated vnode
806 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
808 vp->v_op = &cd9660_fifoops;
814 if (ip->iso_extent == imp->root_extent)
815 vp->v_vflag |= VV_ROOT;
818 * XXX need generation number?