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
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/namei.h>
40 #include <sys/kernel.h>
45 #include <sys/mount.h>
46 #include <sys/vnode.h>
47 #include <sys/malloc.h>
48 #include <sys/dirent.h>
49 #include <sys/unistd.h>
50 #include <sys/filio.h>
51 #include <sys/sysctl.h>
54 #include <vm/vnode_pager.h>
57 #include <fs/cd9660/iso.h>
58 #include <fs/cd9660/cd9660_node.h>
59 #include <fs/cd9660/iso_rrip.h>
61 static vop_setattr_t cd9660_setattr;
62 static vop_open_t cd9660_open;
63 static vop_access_t cd9660_access;
64 static vop_getattr_t cd9660_getattr;
65 static vop_ioctl_t cd9660_ioctl;
66 static vop_pathconf_t cd9660_pathconf;
67 static vop_read_t cd9660_read;
69 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
70 static int iso_shipdir(struct isoreaddir *idp);
71 static vop_readdir_t cd9660_readdir;
72 static vop_readlink_t cd9660_readlink;
73 static vop_strategy_t cd9660_strategy;
74 static vop_vptofh_t cd9660_vptofh;
75 static vop_getpages_t cd9660_getpages;
78 * Setattr call. Only allowed for block and character special devices.
81 cd9660_setattr(struct vop_setattr_args *ap)
83 struct vnode *vp = ap->a_vp;
84 struct vattr *vap = ap->a_vap;
86 if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
87 vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
88 vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
90 if (vap->va_size != (u_quad_t)VNOVAL) {
111 * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
112 * The mode is shifted to select the owner/group/other fields. The
113 * super user is granted all permissions.
117 cd9660_access(struct vop_access_args *ap)
119 struct vnode *vp = ap->a_vp;
120 struct iso_node *ip = VTOI(vp);
121 accmode_t accmode = ap->a_accmode;
123 if (vp->v_type == VCHR || vp->v_type == VBLK)
127 * Disallow write attempts unless the file is a socket,
128 * fifo, or a block or character device resident on the
131 if (accmode & VWRITE) {
132 switch (vp->v_type) {
143 return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
144 ip->inode.iso_gid, ap->a_accmode, ap->a_cred));
148 cd9660_open(struct vop_open_args *ap)
150 struct vnode *vp = ap->a_vp;
151 struct iso_node *ip = VTOI(vp);
153 if (vp->v_type == VCHR || vp->v_type == VBLK)
156 vnode_create_vobject(vp, ip->i_size, ap->a_td);
161 cd9660_getattr(struct vop_getattr_args *ap)
164 struct vnode *vp = ap->a_vp;
165 struct vattr *vap = ap->a_vap;
166 struct iso_node *ip = VTOI(vp);
168 vap->va_fsid = dev2udev(ip->i_mnt->im_dev);
169 vap->va_fileid = ip->i_number;
171 vap->va_mode = ip->inode.iso_mode;
172 vap->va_nlink = ip->inode.iso_links;
173 vap->va_uid = ip->inode.iso_uid;
174 vap->va_gid = ip->inode.iso_gid;
175 vap->va_atime = ip->inode.iso_atime;
176 vap->va_mtime = ip->inode.iso_mtime;
177 vap->va_ctime = ip->inode.iso_ctime;
178 vap->va_rdev = ip->inode.iso_rdev;
180 vap->va_size = (u_quad_t) ip->i_size;
181 if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
182 struct vop_readlink_args rdlnk;
187 cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
189 aiov.iov_len = MAXPATHLEN;
190 auio.uio_iov = &aiov;
193 auio.uio_rw = UIO_READ;
194 auio.uio_segflg = UIO_SYSSPACE;
195 auio.uio_td = curthread;
196 auio.uio_resid = MAXPATHLEN;
198 rdlnk.a_vp = ap->a_vp;
199 rdlnk.a_cred = ap->a_cred;
200 if (cd9660_readlink(&rdlnk) == 0)
201 vap->va_size = MAXPATHLEN - auio.uio_resid;
206 vap->va_blocksize = ip->i_mnt->logical_block_size;
207 vap->va_bytes = (u_quad_t) ip->i_size;
208 vap->va_type = vp->v_type;
214 * Vnode op for ioctl.
217 cd9660_ioctl(struct vop_ioctl_args *ap)
224 vn_lock(vp, LK_SHARED | LK_RETRY);
225 if (VN_IS_DOOMED(vp)) {
229 if (vp->v_type == VCHR || vp->v_type == VBLK) {
237 switch (ap->a_command) {
239 *(int *)(ap->a_data) = ip->iso_start;
251 * Vnode op for reading.
254 cd9660_read(struct vop_read_args *ap)
256 struct vnode *vp = ap->a_vp;
257 struct uio *uio = ap->a_uio;
258 struct iso_node *ip = VTOI(vp);
261 daddr_t lbn, rablock;
263 int rasize, error = 0;
267 if (vp->v_type == VCHR || vp->v_type == VBLK)
270 seqcount = ap->a_ioflag >> IO_SEQSHIFT;
272 if (uio->uio_resid == 0)
274 if (uio->uio_offset < 0)
278 lbn = lblkno(imp, uio->uio_offset);
279 on = blkoff(imp, uio->uio_offset);
280 n = MIN(imp->logical_block_size - on, uio->uio_resid);
281 diff = (off_t)ip->i_size - uio->uio_offset;
286 size = blksize(imp, ip, lbn);
288 if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
289 if (lblktosize(imp, rablock) < ip->i_size)
290 error = cluster_read(vp, (off_t)ip->i_size,
291 lbn, size, NOCRED, uio->uio_resid,
292 (ap->a_ioflag >> 16), 0, &bp);
294 error = bread(vp, lbn, size, NOCRED, &bp);
297 lblktosize(imp, rablock) < ip->i_size) {
298 rasize = blksize(imp, ip, rablock);
299 error = breadn(vp, lbn, size, &rablock,
300 &rasize, 1, NOCRED, &bp);
302 error = bread(vp, lbn, size, NOCRED, &bp);
306 n = MIN(n, size - bp->b_resid);
308 error = uiomove(bp->b_data + on, (int)n, uio);
310 } while (error == 0 && uio->uio_resid > 0 && n != 0);
315 * Structure for reading directories
318 struct dirent saveent;
319 struct dirent assocent;
320 struct dirent current;
332 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
336 dp->d_reclen = GENERIC_DIRSIZ(dp);
337 dirent_terminate(dp);
339 if (idp->uio->uio_resid < dp->d_reclen) {
345 if (idp->ncookies <= 0) {
350 *idp->cookies++ = off;
354 if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
361 iso_shipdir(struct isoreaddir *idp)
368 cl = idp->current.d_namlen;
369 cname = idp->current.d_name;
370 assoc = (cl > 1) && (*cname == ASSOCCHAR);
378 if (!(sl = dp->d_namlen)) {
380 sname = dp->d_name + 1;
381 sl = dp->d_namlen - 1;
385 || bcmp(sname,cname,sl)) {
386 if (idp->assocent.d_namlen) {
387 if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
389 idp->assocent.d_namlen = 0;
391 if (idp->saveent.d_namlen) {
392 if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
394 idp->saveent.d_namlen = 0;
398 idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
400 idp->assocoff = idp->curroff;
401 memcpy(&idp->assocent, &idp->current, idp->current.d_reclen);
403 idp->saveoff = idp->curroff;
404 memcpy(&idp->saveent, &idp->current, idp->current.d_reclen);
410 * Vnode op for readdir
413 cd9660_readdir(struct vop_readdir_args *ap)
415 struct uio *uio = ap->a_uio;
416 struct isoreaddir *idp;
417 struct vnode *vdp = ap->a_vp;
420 struct buf *bp = NULL;
421 struct iso_directory_record *ep;
422 int entryoffsetinblock;
429 uint64_t *cookies = NULL;
434 bmask = imp->im_bmask;
436 idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
437 idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
440 * Is it worth trying to figure out the type?
442 idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
445 if (ap->a_ncookies == NULL) {
449 * Guess the number of cookies needed.
451 ncookies = uio->uio_resid / 16;
452 cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
453 idp->cookies = cookies;
454 idp->ncookies = ncookies;
457 idp->curroff = uio->uio_offset;
458 idp->uio_off = uio->uio_offset;
460 if ((entryoffsetinblock = idp->curroff & bmask) &&
461 (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
465 endsearch = dp->i_size;
467 while (idp->curroff < endsearch) {
469 * If offset is on a block boundary,
470 * read the next directory block.
471 * Release previous if it exists.
473 if ((idp->curroff & bmask) == 0) {
477 cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
479 entryoffsetinblock = 0;
482 * Get pointer to next entry.
484 ep = (struct iso_directory_record *)
485 ((char *)bp->b_data + entryoffsetinblock);
487 reclen = isonum_711(ep->length);
489 /* skip to next block, if any */
491 (idp->curroff & ~bmask) + imp->logical_block_size;
495 if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
497 /* illegal entry, stop */
501 if (entryoffsetinblock + reclen > imp->logical_block_size) {
503 /* illegal directory, so stop looking */
507 idp->current.d_namlen = isonum_711(ep->name_len);
509 if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
511 /* illegal entry, stop */
515 if (isonum_711(ep->flags)&2)
516 idp->current.d_fileno = isodirino(ep, imp);
518 idp->current.d_fileno = dbtob(bp->b_blkno) +
521 idp->curroff += reclen;
522 /* NOTE: d_off is the offset of *next* entry. */
523 idp->current.d_off = idp->curroff;
525 switch (imp->iso_ftype) {
527 ino = idp->current.d_fileno;
528 cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
530 idp->current.d_fileno = ino;
531 idp->current.d_namlen = (u_char)namelen;
532 if (idp->current.d_namlen)
533 error = iso_uiodir(idp,&idp->current,idp->curroff);
535 default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
536 strcpy(idp->current.d_name,"..");
537 if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
538 idp->current.d_namlen = 1;
539 error = iso_uiodir(idp,&idp->current,idp->curroff);
540 } else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
541 idp->current.d_namlen = 2;
542 error = iso_uiodir(idp,&idp->current,idp->curroff);
544 isofntrans(ep->name,idp->current.d_namlen,
545 idp->current.d_name, &namelen,
546 imp->iso_ftype == ISO_FTYPE_9660,
547 isonum_711(ep->flags)&4,
551 idp->current.d_namlen = (u_char)namelen;
552 if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
553 error = iso_shipdir(idp);
555 error = iso_uiodir(idp,&idp->current,idp->curroff);
561 entryoffsetinblock += reclen;
564 if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
565 idp->current.d_namlen = 0;
566 error = iso_shipdir(idp);
571 if (ap->a_ncookies != NULL) {
573 free(cookies, M_TEMP);
576 * Work out the number of cookies actually used.
578 *ap->a_ncookies = ncookies - idp->ncookies;
579 *ap->a_cookies = cookies;
586 uio->uio_offset = idp->uio_off;
587 *ap->a_eofflag = idp->eofflag;
595 * Return target name of a symbolic link
596 * Shouldn't we get the parent vnode and read the data from there?
597 * This could eventually result in deadlocks in cd9660_lookup.
598 * But otherwise the block read here is in the block buffer two times.
600 typedef struct iso_directory_record ISODIR;
601 typedef struct iso_node ISONODE;
602 typedef struct iso_mnt ISOMNT;
604 cd9660_readlink(struct vop_readlink_args *ap)
619 if (imp->iso_ftype != ISO_FTYPE_RRIP)
623 * Get parents directory record block that this inode included.
625 error = bread(imp->im_devvp,
626 (ip->i_number >> imp->im_bshift) <<
627 (imp->im_bshift - DEV_BSHIFT),
628 imp->logical_block_size, NOCRED, &bp);
634 * Setup the directory pointer for this inode
636 dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
639 * Just make sure, we have a right one....
640 * 1: Check not cross boundary on block
642 if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
643 > (unsigned)imp->logical_block_size) {
650 * Abuse a namei buffer for now.
652 if (uio->uio_segflg == UIO_SYSSPACE)
653 symname = uio->uio_iov->iov_base;
655 symname = uma_zalloc(namei_zone, M_WAITOK);
658 * Ok, we just gathering a symbolic name in SL record.
660 if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
661 if (uio->uio_segflg != UIO_SYSSPACE)
662 uma_zfree(namei_zone, symname);
667 * Don't forget before you leave from home ;-)
672 * return with the symbolic name to caller's.
674 if (uio->uio_segflg != UIO_SYSSPACE) {
675 error = uiomove(symname, symlen, uio);
676 uma_zfree(namei_zone, symname);
679 uio->uio_resid -= symlen;
680 uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
681 uio->uio_iov->iov_len -= symlen;
686 * Calculate the logical to physical mapping if not done already,
687 * then call the device strategy routine.
690 cd9660_strategy(struct vop_strategy_args *ap)
692 struct buf *bp = ap->a_bp;
693 struct vnode *vp = ap->a_vp;
698 if (vp->v_type == VBLK || vp->v_type == VCHR)
699 panic("cd9660_strategy: spec");
700 if (bp->b_blkno == bp->b_lblkno) {
701 bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
702 (ip->i_mnt->im_bshift - DEV_BSHIFT);
704 bp->b_iooffset = dbtob(bp->b_blkno);
705 bo = ip->i_mnt->im_bo;
711 * Return POSIX pathconf information applicable to cd9660 filesystems.
714 cd9660_pathconf(struct vop_pathconf_args *ap)
717 switch (ap->a_name) {
718 case _PC_FILESIZEBITS:
725 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
726 *ap->a_retval = NAME_MAX;
730 case _PC_SYMLINK_MAX:
731 if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
732 *ap->a_retval = MAXPATHLEN;
740 return (vop_stdpathconf(ap));
746 * Vnode pointer to File handle
749 cd9660_vptofh(struct vop_vptofh_args *ap)
752 struct iso_node *ip = VTOI(ap->a_vp);
754 ifh.ifid_len = sizeof(struct ifid);
756 ifh.ifid_ino = ip->i_number;
757 ifh.ifid_start = ip->iso_start;
759 * This intentionally uses sizeof(ifh) in order to not copy stack
762 memcpy(ap->a_fhp, &ifh, sizeof(ifh));
765 printf("vptofh: ino %jd, start %ld\n",
766 (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
772 SYSCTL_NODE(_vfs, OID_AUTO, cd9660, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
773 "cd9660 filesystem");
774 static int use_buf_pager = 1;
775 SYSCTL_INT(_vfs_cd9660, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
777 "Use buffer pager instead of bmap");
780 cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
783 return (lblkno(VTOI(vp)->i_mnt, off));
787 cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
792 *sz = blksize(ip->i_mnt, ip, lbn);
797 cd9660_getpages(struct vop_getpages_args *ap)
802 if (vp->v_type == VCHR || vp->v_type == VBLK)
806 return (vfs_bio_getpages(vp, ap->a_m, ap->a_count,
807 ap->a_rbehind, ap->a_rahead, cd9660_gbp_getblkno,
808 cd9660_gbp_getblksz));
809 return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
810 ap->a_rbehind, ap->a_rahead, NULL, NULL));
814 * Global vfs data structures for cd9660
816 struct vop_vector cd9660_vnodeops = {
817 .vop_default = &default_vnodeops,
818 .vop_open = cd9660_open,
819 .vop_access = cd9660_access,
820 .vop_bmap = cd9660_bmap,
821 .vop_cachedlookup = cd9660_lookup,
822 .vop_getattr = cd9660_getattr,
823 .vop_inactive = cd9660_inactive,
824 .vop_ioctl = cd9660_ioctl,
825 .vop_lookup = vfs_cache_lookup,
826 .vop_pathconf = cd9660_pathconf,
827 .vop_read = cd9660_read,
828 .vop_readdir = cd9660_readdir,
829 .vop_readlink = cd9660_readlink,
830 .vop_reclaim = cd9660_reclaim,
831 .vop_setattr = cd9660_setattr,
832 .vop_strategy = cd9660_strategy,
833 .vop_vptofh = cd9660_vptofh,
834 .vop_getpages = cd9660_getpages,
836 VFS_VOP_VECTOR_REGISTER(cd9660_vnodeops);
839 * Special device vnode ops
842 struct vop_vector cd9660_fifoops = {
843 .vop_default = &fifo_specops,
844 .vop_access = cd9660_access,
845 .vop_getattr = cd9660_getattr,
846 .vop_inactive = cd9660_inactive,
847 .vop_reclaim = cd9660_reclaim,
848 .vop_setattr = cd9660_setattr,
849 .vop_vptofh = cd9660_vptofh,
851 VFS_VOP_VECTOR_REGISTER(cd9660_fifoops);