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_vnops.c 8.19 (Berkeley) 5/27/95
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/kernel.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53 #include <sys/dirent.h>
54 #include <sys/unistd.h>
55 #include <sys/filio.h>
56 #include <sys/sysctl.h>
59 #include <vm/vnode_pager.h>
62 #include <fs/cd9660/iso.h>
63 #include <fs/cd9660/cd9660_node.h>
64 #include <fs/cd9660/iso_rrip.h>
66 static vop_setattr_t cd9660_setattr;
67 static vop_open_t cd9660_open;
68 static vop_access_t cd9660_access;
69 static vop_getattr_t cd9660_getattr;
70 static vop_ioctl_t cd9660_ioctl;
71 static vop_pathconf_t cd9660_pathconf;
72 static vop_read_t cd9660_read;
74 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
75 static int iso_shipdir(struct isoreaddir *idp);
76 static vop_readdir_t cd9660_readdir;
77 static vop_readlink_t cd9660_readlink;
78 static vop_strategy_t cd9660_strategy;
79 static vop_vptofh_t cd9660_vptofh;
80 static vop_getpages_t cd9660_getpages;
83 * Setattr call. Only allowed for block and character special devices.
87 struct vop_setattr_args /* {
88 struct vnodeop_desc *a_desc;
94 struct vnode *vp = ap->a_vp;
95 struct vattr *vap = ap->a_vap;
97 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
98 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
99 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
101 if (vap->va_size != (u_quad_t)VNOVAL) {
102 switch (vp->v_type) {
122 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
123 * The mode is shifted to select the owner/group/other fields. The
124 * super user is granted all permissions.
129 struct vop_access_args /* {
132 struct ucred *a_cred;
136 struct vnode *vp = ap->a_vp;
137 struct iso_node *ip = VTOI(vp);
138 accmode_t accmode = ap->a_accmode;
140 if (vp->v_type == VCHR || vp->v_type == VBLK)
144 * Disallow write attempts unless the file is a socket,
145 * fifo, or a block or character device resident on the
148 if (accmode & VWRITE) {
149 switch (vp->v_type) {
160 return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
161 ip->inode.iso_gid, ap->a_accmode, ap->a_cred, NULL));
166 struct vop_open_args /* {
169 struct ucred *a_cred;
174 struct vnode *vp = ap->a_vp;
175 struct iso_node *ip = VTOI(vp);
177 if (vp->v_type == VCHR || vp->v_type == VBLK)
180 vnode_create_vobject(vp, ip->i_size, ap->a_td);
187 struct vop_getattr_args /* {
190 struct ucred *a_cred;
194 struct vnode *vp = ap->a_vp;
195 struct vattr *vap = ap->a_vap;
196 struct iso_node *ip = VTOI(vp);
198 vap->va_fsid = dev2udev(ip->i_mnt->im_dev);
199 vap->va_fileid = ip->i_number;
201 vap->va_mode = ip->inode.iso_mode;
202 vap->va_nlink = ip->inode.iso_links;
203 vap->va_uid = ip->inode.iso_uid;
204 vap->va_gid = ip->inode.iso_gid;
205 vap->va_atime = ip->inode.iso_atime;
206 vap->va_mtime = ip->inode.iso_mtime;
207 vap->va_ctime = ip->inode.iso_ctime;
208 vap->va_rdev = ip->inode.iso_rdev;
210 vap->va_size = (u_quad_t) ip->i_size;
211 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
212 struct vop_readlink_args rdlnk;
217 cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
219 aiov.iov_len = MAXPATHLEN;
220 auio.uio_iov = &aiov;
223 auio.uio_rw = UIO_READ;
224 auio.uio_segflg = UIO_SYSSPACE;
225 auio.uio_td = curthread;
226 auio.uio_resid = MAXPATHLEN;
228 rdlnk.a_vp = ap->a_vp;
229 rdlnk.a_cred = ap->a_cred;
230 if (cd9660_readlink(&rdlnk) == 0)
231 vap->va_size = MAXPATHLEN - auio.uio_resid;
236 vap->va_blocksize = ip->i_mnt->logical_block_size;
237 vap->va_bytes = (u_quad_t) ip->i_size;
238 vap->va_type = vp->v_type;
244 * Vnode op for ioctl.
248 struct vop_ioctl_args /* {
253 struct ucred *a_cred;
262 vn_lock(vp, LK_SHARED | LK_RETRY);
263 if (vp->v_iflag & VI_DOOMED) {
267 if (vp->v_type == VCHR || vp->v_type == VBLK) {
275 switch (ap->a_command) {
277 *(int *)(ap->a_data) = ip->iso_start;
289 * Vnode op for reading.
293 struct vop_read_args /* {
297 struct ucred *a_cred;
300 struct vnode *vp = ap->a_vp;
301 struct uio *uio = ap->a_uio;
302 struct iso_node *ip = VTOI(vp);
305 daddr_t lbn, rablock;
307 int rasize, error = 0;
311 if (vp->v_type == VCHR || vp->v_type == VBLK)
314 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
316 if (uio->uio_resid == 0)
318 if (uio->uio_offset < 0)
322 lbn = lblkno(imp, uio->uio_offset);
323 on = blkoff(imp, uio->uio_offset);
324 n = MIN(imp->logical_block_size - on, uio->uio_resid);
325 diff = (off_t)ip->i_size - uio->uio_offset;
330 size = blksize(imp, ip, lbn);
332 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
333 if (lblktosize(imp, rablock) < ip->i_size)
334 error = cluster_read(vp, (off_t)ip->i_size,
335 lbn, size, NOCRED, uio->uio_resid,
336 (ap->a_ioflag >> 16), 0, &bp);
338 error = bread(vp, lbn, size, NOCRED, &bp);
341 lblktosize(imp, rablock) < ip->i_size) {
342 rasize = blksize(imp, ip, rablock);
343 error = breadn(vp, lbn, size, &rablock,
344 &rasize, 1, NOCRED, &bp);
346 error = bread(vp, lbn, size, NOCRED, &bp);
350 n = MIN(n, size - bp->b_resid);
352 error = uiomove(bp->b_data + on, (int)n, uio);
354 } while (error == 0 && uio->uio_resid > 0 && n != 0);
359 * Structure for reading directories
362 struct dirent saveent;
363 struct dirent assocent;
364 struct dirent current;
376 iso_uiodir(idp,dp,off)
377 struct isoreaddir *idp;
383 dp->d_name[dp->d_namlen] = 0;
384 dp->d_reclen = GENERIC_DIRSIZ(dp);
386 if (idp->uio->uio_resid < dp->d_reclen) {
392 if (idp->ncookies <= 0) {
397 *idp->cookies++ = off;
401 if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
409 struct isoreaddir *idp;
416 cl = idp->current.d_namlen;
417 cname = idp->current.d_name;
418 assoc = (cl > 1) && (*cname == ASSOCCHAR);
426 if (!(sl = dp->d_namlen)) {
428 sname = dp->d_name + 1;
429 sl = dp->d_namlen - 1;
433 || bcmp(sname,cname,sl)) {
434 if (idp->assocent.d_namlen) {
435 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
437 idp->assocent.d_namlen = 0;
439 if (idp->saveent.d_namlen) {
440 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
442 idp->saveent.d_namlen = 0;
446 idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
448 idp->assocoff = idp->curroff;
449 bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
451 idp->saveoff = idp->curroff;
452 bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
458 * Vnode op for readdir
462 struct vop_readdir_args /* {
465 struct ucred *a_cred;
471 struct uio *uio = ap->a_uio;
472 struct isoreaddir *idp;
473 struct vnode *vdp = ap->a_vp;
476 struct buf *bp = NULL;
477 struct iso_directory_record *ep;
478 int entryoffsetinblock;
485 u_long *cookies = NULL;
490 bmask = imp->im_bmask;
492 idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
493 idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
496 * Is it worth trying to figure out the type?
498 idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
501 if (ap->a_ncookies == NULL) {
505 * Guess the number of cookies needed.
507 ncookies = uio->uio_resid / 16;
508 cookies = malloc(ncookies * sizeof(u_long),
510 idp->cookies = cookies;
511 idp->ncookies = ncookies;
514 idp->curroff = uio->uio_offset;
515 idp->uio_off = uio->uio_offset;
517 if ((entryoffsetinblock = idp->curroff & bmask) &&
518 (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
522 endsearch = dp->i_size;
524 while (idp->curroff < endsearch) {
526 * If offset is on a block boundary,
527 * read the next directory block.
528 * Release previous if it exists.
530 if ((idp->curroff & bmask) == 0) {
534 cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
536 entryoffsetinblock = 0;
539 * Get pointer to next entry.
541 ep = (struct iso_directory_record *)
542 ((char *)bp->b_data + entryoffsetinblock);
544 reclen = isonum_711(ep->length);
546 /* skip to next block, if any */
548 (idp->curroff & ~bmask) + imp->logical_block_size;
552 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
554 /* illegal entry, stop */
558 if (entryoffsetinblock + reclen > imp->logical_block_size) {
560 /* illegal directory, so stop looking */
564 idp->current.d_namlen = isonum_711(ep->name_len);
566 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
568 /* illegal entry, stop */
572 if (isonum_711(ep->flags)&2)
573 idp->current.d_fileno = isodirino(ep, imp);
575 idp->current.d_fileno = dbtob(bp->b_blkno) +
578 idp->curroff += reclen;
580 switch (imp->iso_ftype) {
582 ino = idp->current.d_fileno;
583 cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
585 idp->current.d_fileno = ino;
586 idp->current.d_namlen = (u_char)namelen;
587 if (idp->current.d_namlen)
588 error = iso_uiodir(idp,&idp->current,idp->curroff);
590 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
591 strcpy(idp->current.d_name,"..");
592 if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
593 idp->current.d_namlen = 1;
594 error = iso_uiodir(idp,&idp->current,idp->curroff);
595 } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
596 idp->current.d_namlen = 2;
597 error = iso_uiodir(idp,&idp->current,idp->curroff);
599 isofntrans(ep->name,idp->current.d_namlen,
600 idp->current.d_name, &namelen,
601 imp->iso_ftype == ISO_FTYPE_9660,
602 isonum_711(ep->flags)&4,
606 idp->current.d_namlen = (u_char)namelen;
607 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
608 error = iso_shipdir(idp);
610 error = iso_uiodir(idp,&idp->current,idp->curroff);
616 entryoffsetinblock += reclen;
619 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
620 idp->current.d_namlen = 0;
621 error = iso_shipdir(idp);
626 if (ap->a_ncookies != NULL) {
628 free(cookies, M_TEMP);
631 * Work out the number of cookies actually used.
633 *ap->a_ncookies = ncookies - idp->ncookies;
634 *ap->a_cookies = cookies;
641 uio->uio_offset = idp->uio_off;
642 *ap->a_eofflag = idp->eofflag;
650 * Return target name of a symbolic link
651 * Shouldn't we get the parent vnode and read the data from there?
652 * This could eventually result in deadlocks in cd9660_lookup.
653 * But otherwise the block read here is in the block buffer two times.
655 typedef struct iso_directory_record ISODIR;
656 typedef struct iso_node ISONODE;
657 typedef struct iso_mnt ISOMNT;
660 struct vop_readlink_args /* {
663 struct ucred *a_cred;
679 if (imp->iso_ftype != ISO_FTYPE_RRIP)
683 * Get parents directory record block that this inode included.
685 error = bread(imp->im_devvp,
686 (ip->i_number >> imp->im_bshift) <<
687 (imp->im_bshift - DEV_BSHIFT),
688 imp->logical_block_size, NOCRED, &bp);
695 * Setup the directory pointer for this inode
697 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
700 * Just make sure, we have a right one....
701 * 1: Check not cross boundary on block
703 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
704 > (unsigned)imp->logical_block_size) {
711 * Abuse a namei buffer for now.
713 if (uio->uio_segflg == UIO_SYSSPACE)
714 symname = uio->uio_iov->iov_base;
716 symname = uma_zalloc(namei_zone, M_WAITOK);
719 * Ok, we just gathering a symbolic name in SL record.
721 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
722 if (uio->uio_segflg != UIO_SYSSPACE)
723 uma_zfree(namei_zone, symname);
728 * Don't forget before you leave from home ;-)
733 * return with the symbolic name to caller's.
735 if (uio->uio_segflg != UIO_SYSSPACE) {
736 error = uiomove(symname, symlen, uio);
737 uma_zfree(namei_zone, symname);
740 uio->uio_resid -= symlen;
741 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
742 uio->uio_iov->iov_len -= symlen;
747 * Calculate the logical to physical mapping if not done already,
748 * then call the device strategy routine.
752 struct vop_strategy_args /* {
757 struct buf *bp = ap->a_bp;
758 struct vnode *vp = ap->a_vp;
763 if (vp->v_type == VBLK || vp->v_type == VCHR)
764 panic("cd9660_strategy: spec");
765 if (bp->b_blkno == bp->b_lblkno) {
766 bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
767 (ip->i_mnt->im_bshift - DEV_BSHIFT);
769 bp->b_iooffset = dbtob(bp->b_blkno);
770 bo = ip->i_mnt->im_bo;
776 * Return POSIX pathconf information applicable to cd9660 filesystems.
780 struct vop_pathconf_args /* {
783 register_t *a_retval;
787 switch (ap->a_name) {
788 case _PC_FILESIZEBITS:
795 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
796 *ap->a_retval = NAME_MAX;
800 case _PC_SYMLINK_MAX:
801 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
802 *ap->a_retval = MAXPATHLEN;
810 return (vop_stdpathconf(ap));
816 * Vnode pointer to File handle
820 struct vop_vptofh_args /* {
826 struct iso_node *ip = VTOI(ap->a_vp);
828 ifh.ifid_len = sizeof(struct ifid);
830 ifh.ifid_ino = ip->i_number;
831 ifh.ifid_start = ip->iso_start;
833 * This intentionally uses sizeof(ifh) in order to not copy stack
836 memcpy(ap->a_fhp, &ifh, sizeof(ifh));
839 printf("vptofh: ino %jd, start %ld\n",
840 (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
846 SYSCTL_NODE(_vfs, OID_AUTO, cd9660, CTLFLAG_RW, 0, "cd9660 filesystem");
847 static int use_buf_pager = 1;
848 SYSCTL_INT(_vfs_cd9660, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
850 "Use buffer pager instead of bmap");
853 cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
856 return (lblkno(VTOI(vp)->i_mnt, off));
860 cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn)
865 return (blksize(ip->i_mnt, ip, lbn));
869 cd9660_getpages(struct vop_getpages_args *ap)
874 if (vp->v_type == VCHR || vp->v_type == VBLK)
878 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count,
879 ap->a_rbehind, ap->a_rahead, cd9660_gbp_getblkno,
880 cd9660_gbp_getblksz));
881 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
882 ap->a_rbehind, ap->a_rahead, NULL, NULL));
886 * Global vfs data structures for cd9660
888 struct vop_vector cd9660_vnodeops = {
889 .vop_default = &default_vnodeops,
890 .vop_open = cd9660_open,
891 .vop_access = cd9660_access,
892 .vop_bmap = cd9660_bmap,
893 .vop_cachedlookup = cd9660_lookup,
894 .vop_getattr = cd9660_getattr,
895 .vop_inactive = cd9660_inactive,
896 .vop_ioctl = cd9660_ioctl,
897 .vop_lookup = vfs_cache_lookup,
898 .vop_pathconf = cd9660_pathconf,
899 .vop_read = cd9660_read,
900 .vop_readdir = cd9660_readdir,
901 .vop_readlink = cd9660_readlink,
902 .vop_reclaim = cd9660_reclaim,
903 .vop_setattr = cd9660_setattr,
904 .vop_strategy = cd9660_strategy,
905 .vop_vptofh = cd9660_vptofh,
906 .vop_getpages = cd9660_getpages,
910 * Special device vnode ops
913 struct vop_vector cd9660_fifoops = {
914 .vop_default = &fifo_specops,
915 .vop_access = cd9660_access,
916 .vop_getattr = cd9660_getattr,
917 .vop_inactive = cd9660_inactive,
918 .vop_reclaim = cd9660_reclaim,
919 .vop_setattr = cd9660_setattr,
920 .vop_vptofh = cd9660_vptofh,