2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley
8 * by Pace Willisson (pace@blitz.com). The Rock Ridge Extension
9 * Support code is derived from software contributed to Berkeley
10 * by Atsushi Murai (amurai@spec.co.jp).
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)cd9660_vfsops.c 8.18 (Berkeley) 5/22/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
54 #include <sys/fcntl.h>
55 #include <sys/malloc.h>
57 #include <sys/syslog.h>
58 #include <sys/iconv.h>
60 #include <fs/cd9660/iso.h>
61 #include <fs/cd9660/iso_rrip.h>
62 #include <fs/cd9660/cd9660_node.h>
63 #include <fs/cd9660/cd9660_mount.h>
65 #include <geom/geom.h>
66 #include <geom/geom_vfs.h>
68 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure");
69 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part");
71 struct iconv_functions *cd9660_iconv = NULL;
73 static vfs_mount_t cd9660_mount;
74 static vfs_cmount_t cd9660_cmount;
75 static vfs_unmount_t cd9660_unmount;
76 static vfs_root_t cd9660_root;
77 static vfs_statfs_t cd9660_statfs;
78 static vfs_vget_t cd9660_vget;
79 static vfs_fhtovp_t cd9660_fhtovp;
81 static struct vfsops cd9660_vfsops = {
82 .vfs_fhtovp = cd9660_fhtovp,
83 .vfs_mount = cd9660_mount,
84 .vfs_cmount = cd9660_cmount,
85 .vfs_root = cd9660_root,
86 .vfs_statfs = cd9660_statfs,
87 .vfs_unmount = cd9660_unmount,
88 .vfs_vget = cd9660_vget,
90 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
91 MODULE_VERSION(cd9660, 1);
93 static int cd9660_vfs_hash_cmp(struct vnode *vp, void *pino);
94 static int iso_mountfs(struct vnode *devvp, struct mount *mp);
101 cd9660_cmount(struct mntarg *ma, void *data, uint64_t flags)
103 struct iso_args args;
104 struct export_args exp;
107 error = copyin(data, &args, sizeof args);
110 vfs_oexport_conv(&args.export, &exp);
112 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
113 ma = mount_arg(ma, "export", &exp, sizeof(exp));
114 ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
115 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
116 ma = mount_argf(ma, "ssector", "%u", args.ssector);
117 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
118 ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
119 ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
120 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
122 args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
123 ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
125 error = kernel_mount(ma, flags);
131 cd9660_mount(struct mount *mp)
138 struct nameidata ndp;
139 struct iso_mnt *imp = NULL;
144 * Unconditionally mount as read-only.
147 mp->mnt_flag |= MNT_RDONLY;
150 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
154 imp = VFSTOISOFS(mp);
156 if (mp->mnt_flag & MNT_UPDATE) {
157 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
161 * Not an update, or updating the name: look up the name
162 * and verify that it refers to a sensible block device.
164 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
165 if ((error = namei(&ndp)))
167 NDFREE(&ndp, NDF_ONLY_PNBUF);
170 if (!vn_isdisk(devvp, &error)) {
176 * Verify that user has necessary permissions on the device,
177 * or has superuser abilities
180 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
182 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
188 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
189 error = iso_mountfs(devvp, mp);
193 if (devvp != imp->im_devvp)
194 error = EINVAL; /* needs translation */
199 vfs_mountedfrom(mp, fspec);
204 * Common code for mount and mountroot
207 iso_mountfs(devvp, mp)
211 struct iso_mnt *isomp = NULL;
212 struct buf *bp = NULL;
213 struct buf *pribp = NULL, *supbp = NULL;
221 struct iso_volume_descriptor *vdp = NULL;
222 struct iso_primary_descriptor *pri = NULL;
223 struct iso_sierra_primary_descriptor *pri_sierra = NULL;
224 struct iso_supplementary_descriptor *sup = NULL;
225 struct iso_directory_record *rootp;
226 int logical_block_size, ssector;
227 struct g_consumer *cp;
229 char *cs_local, *cs_disk;
234 error = g_vfs_open(devvp, &cp, "cd9660", 0);
236 g_getattr("MNT::verified", cp, &isverified);
238 VOP_UNLOCK(devvp, 0);
241 if (devvp->v_rdev->si_iosize_max != 0)
242 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
243 if (mp->mnt_iosize_max > MAXPHYS)
244 mp->mnt_iosize_max = MAXPHYS;
246 bo = &devvp->v_bufobj;
248 /* This is the "logical sector size". The standard says this
249 * should be 2048 or the physical sector size on the device,
250 * whichever is greater.
252 if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
257 iso_bsize = cp->provider->sectorsize;
260 if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
262 for (iso_blknum = 16 + ssector;
263 iso_blknum < 100 + ssector;
265 if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
266 iso_bsize, NOCRED, &bp)) != 0)
269 vdp = (struct iso_volume_descriptor *)bp->b_data;
270 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
271 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
272 sizeof vdp->id_sierra) != 0) {
278 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
283 pri = (struct iso_primary_descriptor *)vdp;
285 (struct iso_sierra_primary_descriptor *)vdp;
289 case ISO_VD_SUPPLEMENTARY:
293 sup = (struct iso_supplementary_descriptor *)vdp;
295 if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
296 if (bcmp(sup->escape, "%/@", 3) == 0)
298 if (bcmp(sup->escape, "%/C", 3) == 0)
300 if (bcmp(sup->escape, "%/E", 3) == 0)
303 if ((isonum_711 (sup->flags) & 1) &&
304 !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
333 isonum_723 (high_sierra?
334 pri_sierra->logical_block_size:
335 pri->logical_block_size);
337 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
338 || (logical_block_size & (logical_block_size - 1)) != 0) {
343 rootp = (struct iso_directory_record *)
345 pri_sierra->root_directory_record:
346 pri->root_directory_record);
348 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
351 isomp->logical_block_size = logical_block_size;
352 isomp->volume_space_size =
353 isonum_733 (high_sierra?
354 pri_sierra->volume_space_size:
355 pri->volume_space_size);
356 isomp->joliet_level = 0;
358 * Since an ISO9660 multi-session CD can also access previous
359 * sessions, we have to include them into the space consider-
360 * ations. This doesn't yield a very accurate number since
361 * parts of the old sessions might be inaccessible now, but we
362 * can't do much better. This is also important for the NFS
363 * filehandle validation.
365 isomp->volume_space_size += ssector;
366 bcopy (rootp, isomp->root, sizeof isomp->root);
367 isomp->root_extent = isonum_733 (rootp->extent);
368 isomp->root_size = isonum_733 (rootp->size);
370 isomp->im_bmask = logical_block_size - 1;
371 isomp->im_bshift = ffs(logical_block_size) - 1;
373 pribp->b_flags |= B_AGE;
380 mp->mnt_data = isomp;
381 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
382 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
383 mp->mnt_maxsymlinklen = 0;
386 mp->mnt_flag |= MNT_VERIFIED;
387 mp->mnt_flag |= MNT_LOCAL;
388 mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
390 isomp->im_mountp = mp;
392 isomp->im_devvp = devvp;
394 vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
395 vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
396 vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
397 vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
398 vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
400 /* Check the Rock Ridge Extension support */
401 if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
402 if ((error = bread(isomp->im_devvp, (isomp->root_extent +
403 isonum_711(((struct iso_directory_record *)isomp->root)->
404 ext_attr_length)) << (isomp->im_bshift - DEV_BSHIFT),
405 isomp->logical_block_size, NOCRED, &bp)) != 0)
408 rootp = (struct iso_directory_record *)bp->b_data;
410 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
411 isomp->im_flags |= ISOFSMNT_NORRIP;
413 isomp->im_flags &= ~ISOFSMNT_GENS;
417 * The contents are valid,
418 * but they will get reread as part of another vnode, so...
420 bp->b_flags |= B_AGE;
426 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
427 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
430 cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
433 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
434 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
436 isomp->im_d2l = NULL;
437 isomp->im_l2d = NULL;
441 /* this effectively ignores all the mount flags */
443 log(LOG_INFO, "cd9660: High Sierra Format\n");
444 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
446 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
448 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
450 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
451 isomp->iso_ftype = ISO_FTYPE_9660;
455 log(LOG_INFO, "cd9660: RockRidge Extension\n");
456 isomp->iso_ftype = ISO_FTYPE_RRIP;
460 /* Decide whether to use the Joliet descriptor */
462 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
464 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
466 rootp = (struct iso_directory_record *)
467 sup->root_directory_record;
468 bcopy (rootp, isomp->root, sizeof isomp->root);
469 isomp->root_extent = isonum_733 (rootp->extent);
470 isomp->root_size = isonum_733 (rootp->size);
471 isomp->joliet_level = joliet_level;
472 supbp->b_flags |= B_AGE;
495 free(isomp, M_ISOFSMNT);
503 * unmount system call
506 cd9660_unmount(mp, mntflags)
510 struct iso_mnt *isomp;
511 int error, flags = 0;
513 if (mntflags & MNT_FORCE)
515 if ((error = vflush(mp, 0, flags, curthread)))
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);
527 g_vfs_close(isomp->im_cp);
529 vrele(isomp->im_devvp);
530 dev_rel(isomp->im_dev);
531 free(isomp, M_ISOFSMNT);
534 mp->mnt_flag &= ~MNT_LOCAL;
540 * Return root of a filesystem
543 cd9660_root(mp, flags, vpp)
548 struct iso_mnt *imp = VFSTOISOFS(mp);
549 struct iso_directory_record *dp =
550 (struct iso_directory_record *)imp->root;
551 cd_ino_t ino = isodirino(dp, imp);
554 * With RRIP we must use the `.' entry of the root directory.
555 * Simply tell vget, that it's a relocated directory.
557 return (cd9660_vget_internal(mp, ino, flags, vpp,
558 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
562 * Get filesystem statistics.
565 cd9660_statfs(mp, sbp)
569 struct iso_mnt *isomp;
571 isomp = VFSTOISOFS(mp);
573 sbp->f_bsize = isomp->logical_block_size;
574 sbp->f_iosize = sbp->f_bsize; /* XXX */
575 sbp->f_blocks = isomp->volume_space_size;
576 sbp->f_bfree = 0; /* total free blocks */
577 sbp->f_bavail = 0; /* blocks free for non superuser */
578 sbp->f_files = 0; /* total files */
579 sbp->f_ffree = 0; /* free file nodes */
584 * File handle to vnode
586 * Have to be really careful about stale file handles:
587 * - check that the inode number is in range
588 * - call iget() to get the locked inode
589 * - check for an unallocated inode (i_mode == 0)
590 * - check that the generation number matches
595 cd9660_fhtovp(mp, fhp, flags, vpp)
606 memcpy(&ifh, fhp, sizeof(ifh));
609 printf("fhtovp: ino %d, start %ld\n",
610 ifh.ifid_ino, ifh.ifid_start);
613 if ((error = VFS_VGET(mp, ifh.ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
618 if (ip->inode.iso_mode == 0) {
624 vnode_create_vobject(*vpp, ip->i_size, curthread);
629 * Conform to standard VFS interface; can't vget arbitrary inodes beyond 4GB
630 * into media with current inode scheme and 32-bit ino_t. This shouldn't be
631 * needed for anything other than nfsd, and who exports a mounted DVD over NFS?
634 cd9660_vget(mp, ino, flags, vpp)
643 * It would be nice if we didn't always set the `relocated' flag
644 * and force the extra read, but I don't want to think about fixing
647 return (cd9660_vget_internal(mp, ino, flags, vpp,
649 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
653 (struct iso_directory_record *)0));
656 /* Use special comparator for full 64-bit ino comparison. */
658 cd9660_vfs_hash_cmp(vp, pino)
666 ino = *(cd_ino_t *)pino;
667 return (ip->i_number != ino);
671 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
677 struct iso_directory_record *isodir;
687 error = vfs_hash_get(mp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
689 if (error || *vpp != NULL)
693 * We must promote to an exclusive lock for vnode creation. This
694 * can happen if lookup is passed LOCKSHARED.
696 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
697 flags &= ~LK_TYPE_MASK;
698 flags |= LK_EXCLUSIVE;
702 * We do not lock vnode creation as it is believed to be too
703 * expensive for such rare case as simultaneous creation of vnode
704 * for same ino by different processes. We just allow them to race
705 * and check later to decide who wins. Let the race begin!
708 imp = VFSTOISOFS(mp);
710 /* Allocate a new vnode/iso_node. */
711 if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
715 ip = malloc(sizeof(struct iso_node), M_ISOFSNODE,
721 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
722 error = insmntque(vp, mp);
724 free(ip, M_ISOFSNODE);
728 error = vfs_hash_insert(vp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
730 if (error || *vpp != NULL)
733 if (isodir == NULL) {
736 lbn = lblkno(imp, ino);
737 if (lbn >= imp->volume_space_size) {
739 printf("fhtovp: lbn exceed volume space %d\n", lbn);
743 off = blkoff(imp, ino);
744 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
746 printf("fhtovp: crosses block boundary %d\n",
747 off + ISO_DIRECTORY_RECORD_SIZE);
751 error = bread(imp->im_devvp,
752 lbn << (imp->im_bshift - DEV_BSHIFT),
753 imp->logical_block_size, NOCRED, &bp);
757 printf("fhtovp: bread error %d\n",error);
760 isodir = (struct iso_directory_record *)(bp->b_data + off);
762 if (off + isonum_711(isodir->length) >
763 imp->logical_block_size) {
766 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
767 off +isonum_711(isodir->length), off,
768 isonum_711(isodir->length));
773 if (isonum_733(isodir->extent) +
774 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
776 printf("fhtovp: file start miss %d vs %d\n",
777 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
789 * On relocated directories we must
790 * read the `.' entry out of a dir.
792 ip->iso_start = ino >> imp->im_bshift;
795 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
799 isodir = (struct iso_directory_record *)bp->b_data;
802 ip->iso_extent = isonum_733(isodir->extent);
803 ip->i_size = isonum_733(isodir->size);
804 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
807 * Setup time stamp, attribute
810 switch (imp->iso_ftype) {
811 default: /* ISO_FTYPE_9660 */
815 if ((imp->im_flags & ISOFSMNT_EXTATT)
816 && (off = isonum_711(isodir->ext_attr_length)))
817 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
821 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
822 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
828 cd9660_rrip_analyze(isodir, ip, imp);
835 * Initialize the associated vnode
837 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
839 vp->v_op = &cd9660_fifoops;
846 if (ip->iso_extent == imp->root_extent)
847 vp->v_vflag |= VV_ROOT;
850 * XXX need generation number?