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);
98 cd9660_cmount(struct mntarg *ma, void *data, uint64_t flags)
100 struct iso_args args;
101 struct export_args exp;
104 error = copyin(data, &args, sizeof args);
107 vfs_oexport_conv(&args.export, &exp);
109 ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
110 ma = mount_arg(ma, "export", &exp, sizeof(exp));
111 ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
112 ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
113 ma = mount_argf(ma, "ssector", "%u", args.ssector);
114 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
115 ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
116 ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
117 ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
119 args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
120 ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
122 error = kernel_mount(ma, flags);
128 cd9660_mount(struct mount *mp)
135 struct nameidata ndp;
136 struct iso_mnt *imp = 0;
141 * Unconditionally mount as read-only.
144 mp->mnt_flag |= MNT_RDONLY;
147 fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
151 imp = VFSTOISOFS(mp);
153 if (mp->mnt_flag & MNT_UPDATE) {
154 if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
158 * Not an update, or updating the name: look up the name
159 * and verify that it refers to a sensible block device.
161 NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
162 if ((error = namei(&ndp)))
164 NDFREE(&ndp, NDF_ONLY_PNBUF);
167 if (!vn_isdisk(devvp, &error)) {
173 * Verify that user has necessary permissions on the device,
174 * or has superuser abilities
177 error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
179 error = priv_check(td, PRIV_VFS_MOUNT_PERM);
185 if ((mp->mnt_flag & MNT_UPDATE) == 0) {
186 error = iso_mountfs(devvp, mp);
190 if (devvp != imp->im_devvp)
191 error = EINVAL; /* needs translation */
196 vfs_mountedfrom(mp, fspec);
201 * Common code for mount and mountroot
204 iso_mountfs(devvp, mp)
208 struct iso_mnt *isomp = NULL;
209 struct buf *bp = NULL;
210 struct buf *pribp = NULL, *supbp = NULL;
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;
231 error = g_vfs_open(devvp, &cp, "cd9660", 0);
234 VOP_UNLOCK(devvp, 0);
237 if (devvp->v_rdev->si_iosize_max != 0)
238 mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
239 if (mp->mnt_iosize_max > MAXPHYS)
240 mp->mnt_iosize_max = MAXPHYS;
242 bo = &devvp->v_bufobj;
244 /* This is the "logical sector size". The standard says this
245 * should be 2048 or the physical sector size on the device,
246 * whichever is greater.
248 if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
253 iso_bsize = cp->provider->sectorsize;
256 if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
258 for (iso_blknum = 16 + ssector;
259 iso_blknum < 100 + ssector;
261 if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
262 iso_bsize, NOCRED, &bp)) != 0)
265 vdp = (struct iso_volume_descriptor *)bp->b_data;
266 if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
267 if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
268 sizeof vdp->id_sierra) != 0) {
274 switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
279 pri = (struct iso_primary_descriptor *)vdp;
281 (struct iso_sierra_primary_descriptor *)vdp;
285 case ISO_VD_SUPPLEMENTARY:
289 sup = (struct iso_supplementary_descriptor *)vdp;
291 if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
292 if (bcmp(sup->escape, "%/@", 3) == 0)
294 if (bcmp(sup->escape, "%/C", 3) == 0)
296 if (bcmp(sup->escape, "%/E", 3) == 0)
299 if ((isonum_711 (sup->flags) & 1) &&
300 !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
329 isonum_723 (high_sierra?
330 pri_sierra->logical_block_size:
331 pri->logical_block_size);
333 if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
334 || (logical_block_size & (logical_block_size - 1)) != 0) {
339 rootp = (struct iso_directory_record *)
341 pri_sierra->root_directory_record:
342 pri->root_directory_record);
344 isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
347 isomp->logical_block_size = logical_block_size;
348 isomp->volume_space_size =
349 isonum_733 (high_sierra?
350 pri_sierra->volume_space_size:
351 pri->volume_space_size);
352 isomp->joliet_level = 0;
354 * Since an ISO9660 multi-session CD can also access previous
355 * sessions, we have to include them into the space consider-
356 * ations. This doesn't yield a very accurate number since
357 * parts of the old sessions might be inaccessible now, but we
358 * can't do much better. This is also important for the NFS
359 * filehandle validation.
361 isomp->volume_space_size += ssector;
362 bcopy (rootp, isomp->root, sizeof isomp->root);
363 isomp->root_extent = isonum_733 (rootp->extent);
364 isomp->root_size = isonum_733 (rootp->size);
366 isomp->im_bmask = logical_block_size - 1;
367 isomp->im_bshift = ffs(logical_block_size) - 1;
369 pribp->b_flags |= B_AGE;
373 mp->mnt_data = isomp;
374 mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
375 mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
376 mp->mnt_maxsymlinklen = 0;
378 mp->mnt_flag |= MNT_LOCAL;
379 mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED |
380 MNTK_EXTENDED_SHARED;
382 isomp->im_mountp = mp;
384 isomp->im_devvp = devvp;
386 vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
387 vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
388 vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
389 vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
390 vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
392 /* Check the Rock Ridge Extension support */
393 if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
394 if ((error = bread(isomp->im_devvp,
395 (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
396 (isomp->im_bshift - DEV_BSHIFT),
397 isomp->logical_block_size, NOCRED, &bp)) != 0)
400 rootp = (struct iso_directory_record *)bp->b_data;
402 if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
403 isomp->im_flags |= ISOFSMNT_NORRIP;
405 isomp->im_flags &= ~ISOFSMNT_GENS;
409 * The contents are valid,
410 * but they will get reread as part of another vnode, so...
412 bp->b_flags |= B_AGE;
417 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
418 cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
421 cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
424 cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
425 cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
427 isomp->im_d2l = NULL;
428 isomp->im_l2d = NULL;
432 /* this effectively ignores all the mount flags */
434 log(LOG_INFO, "cd9660: High Sierra Format\n");
435 isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
437 switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
439 isomp->iso_ftype = ISO_FTYPE_DEFAULT;
441 case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
442 isomp->iso_ftype = ISO_FTYPE_9660;
446 log(LOG_INFO, "cd9660: RockRidge Extension\n");
447 isomp->iso_ftype = ISO_FTYPE_RRIP;
451 /* Decide whether to use the Joliet descriptor */
453 if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
455 log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
457 rootp = (struct iso_directory_record *)
458 sup->root_directory_record;
459 bcopy (rootp, isomp->root, sizeof isomp->root);
460 isomp->root_extent = isonum_733 (rootp->extent);
461 isomp->root_size = isonum_733 (rootp->size);
462 isomp->joliet_level = joliet_level;
463 supbp->b_flags |= B_AGE;
487 free(isomp, M_ISOFSMNT);
495 * unmount system call
498 cd9660_unmount(mp, mntflags)
502 struct iso_mnt *isomp;
503 int error, flags = 0;
505 if (mntflags & MNT_FORCE)
507 if ((error = vflush(mp, 0, flags, curthread)))
510 isomp = VFSTOISOFS(mp);
512 if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
514 cd9660_iconv->close(isomp->im_d2l);
516 cd9660_iconv->close(isomp->im_l2d);
520 g_vfs_close(isomp->im_cp);
523 vrele(isomp->im_devvp);
524 dev_rel(isomp->im_dev);
525 free(isomp, M_ISOFSMNT);
528 mp->mnt_flag &= ~MNT_LOCAL;
534 * Return root of a filesystem
537 cd9660_root(mp, flags, vpp)
542 struct iso_mnt *imp = VFSTOISOFS(mp);
543 struct iso_directory_record *dp =
544 (struct iso_directory_record *)imp->root;
545 ino_t ino = isodirino(dp, imp);
548 * With RRIP we must use the `.' entry of the root directory.
549 * Simply tell vget, that it's a relocated directory.
551 return (cd9660_vget_internal(mp, ino, flags, vpp,
552 imp->iso_ftype == ISO_FTYPE_RRIP, dp));
556 * Get filesystem statistics.
559 cd9660_statfs(mp, sbp)
563 struct iso_mnt *isomp;
565 isomp = VFSTOISOFS(mp);
567 sbp->f_bsize = isomp->logical_block_size;
568 sbp->f_iosize = sbp->f_bsize; /* XXX */
569 sbp->f_blocks = isomp->volume_space_size;
570 sbp->f_bfree = 0; /* total free blocks */
571 sbp->f_bavail = 0; /* blocks free for non superuser */
572 sbp->f_files = 0; /* total files */
573 sbp->f_ffree = 0; /* free file nodes */
578 * File handle to vnode
580 * Have to be really careful about stale file handles:
581 * - check that the inode number is in range
582 * - call iget() to get the locked inode
583 * - check for an unallocated inode (i_mode == 0)
584 * - check that the generation number matches
589 cd9660_fhtovp(mp, fhp, flags, vpp)
600 memcpy(&ifh, fhp, sizeof(ifh));
603 printf("fhtovp: ino %d, start %ld\n",
604 ifh.ifid_ino, ifh.ifid_start);
607 if ((error = VFS_VGET(mp, ifh.ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
612 if (ip->inode.iso_mode == 0) {
618 vnode_create_vobject(*vpp, ip->i_size, curthread);
623 cd9660_vget(mp, ino, flags, vpp)
632 * It would be nice if we didn't always set the `relocated' flag
633 * and force the extra read, but I don't want to think about fixing
636 return (cd9660_vget_internal(mp, ino, flags, vpp,
638 VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
642 (struct iso_directory_record *)0));
646 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
652 struct iso_directory_record *isodir;
663 error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
664 if (error || *vpp != NULL)
668 * We must promote to an exclusive lock for vnode creation. This
669 * can happen if lookup is passed LOCKSHARED.
671 if ((flags & LK_TYPE_MASK) == LK_SHARED) {
672 flags &= ~LK_TYPE_MASK;
673 flags |= LK_EXCLUSIVE;
677 * We do not lock vnode creation as it is believed to be too
678 * expensive for such rare case as simultaneous creation of vnode
679 * for same ino by different processes. We just allow them to race
680 * and check later to decide who wins. Let the race begin!
683 imp = VFSTOISOFS(mp);
686 /* Allocate a new vnode/iso_node. */
687 if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
691 ip = malloc(sizeof(struct iso_node), M_ISOFSNODE,
697 lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
698 error = insmntque(vp, mp);
700 free(ip, M_ISOFSNODE);
704 error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
705 if (error || *vpp != NULL)
711 lbn = lblkno(imp, ino);
712 if (lbn >= imp->volume_space_size) {
714 printf("fhtovp: lbn exceed volume space %d\n", lbn);
718 off = blkoff(imp, ino);
719 if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
721 printf("fhtovp: crosses block boundary %d\n",
722 off + ISO_DIRECTORY_RECORD_SIZE);
726 error = bread(imp->im_devvp,
727 lbn << (imp->im_bshift - DEV_BSHIFT),
728 imp->logical_block_size, NOCRED, &bp);
732 printf("fhtovp: bread error %d\n",error);
735 isodir = (struct iso_directory_record *)(bp->b_data + off);
737 if (off + isonum_711(isodir->length) >
738 imp->logical_block_size) {
742 printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
743 off +isonum_711(isodir->length), off,
744 isonum_711(isodir->length));
749 if (isonum_733(isodir->extent) +
750 isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
753 printf("fhtovp: file start miss %d vs %d\n",
754 isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
766 * On relocated directories we must
767 * read the `.' entry out of a dir.
769 ip->iso_start = ino >> imp->im_bshift;
772 if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
776 isodir = (struct iso_directory_record *)bp->b_data;
779 ip->iso_extent = isonum_733(isodir->extent);
780 ip->i_size = isonum_733(isodir->size);
781 ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
784 * Setup time stamp, attribute
787 switch (imp->iso_ftype) {
788 default: /* ISO_FTYPE_9660 */
792 if ((imp->im_flags & ISOFSMNT_EXTATT)
793 && (off = isonum_711(isodir->ext_attr_length)))
794 cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
798 cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
799 cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
805 cd9660_rrip_analyze(isodir, ip, imp);
813 * Initialize the associated vnode
815 switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
817 vp->v_op = &cd9660_fifoops;
824 if (ip->iso_extent == imp->root_extent)
825 vp->v_vflag |= VV_ROOT;
828 * XXX need generation number?