xref: /freebsd/sys/fs/cd9660/cd9660_vnops.c (revision 82f2275b73e556580b155ea9bbf7f7e364458fa1)
1df8bae1dSRodney W. Grimes /*-
251369649SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1994
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley
8df8bae1dSRodney W. Grimes  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9df8bae1dSRodney W. Grimes  * Support code is derived from software contributed to Berkeley
10df8bae1dSRodney W. Grimes  * by Atsushi Murai (amurai@spec.co.jp).
11df8bae1dSRodney W. Grimes  *
12df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
13df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
14df8bae1dSRodney W. Grimes  * are met:
15df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
17df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
18df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
19df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
20fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
37df8bae1dSRodney W. Grimes #include <sys/param.h>
38df8bae1dSRodney W. Grimes #include <sys/systm.h>
39df8bae1dSRodney W. Grimes #include <sys/namei.h>
40df8bae1dSRodney W. Grimes #include <sys/kernel.h>
41dfb9f846SPoul-Henning Kamp #include <sys/conf.h>
42df8bae1dSRodney W. Grimes #include <sys/stat.h>
439626b608SPoul-Henning Kamp #include <sys/bio.h>
44df8bae1dSRodney W. Grimes #include <sys/buf.h>
45df8bae1dSRodney W. Grimes #include <sys/mount.h>
46df8bae1dSRodney W. Grimes #include <sys/vnode.h>
47df8bae1dSRodney W. Grimes #include <sys/malloc.h>
48c90607baSBruce Evans #include <sys/dirent.h>
49996c772fSJohn Dyson #include <sys/unistd.h>
50cb2a8dffSSøren Schmidt #include <sys/filio.h>
51c329ee71SKonstantin Belousov #include <sys/sysctl.h>
52df8bae1dSRodney W. Grimes 
53651ae11eSMike Smith #include <vm/vm.h>
54651ae11eSMike Smith #include <vm/vnode_pager.h>
552684b6afSJeff Roberson #include <vm/uma.h>
56675ea6f0SBruce Evans 
57a8d36d0dSCraig Rodrigues #include <fs/cd9660/iso.h>
58a8d36d0dSCraig Rodrigues #include <fs/cd9660/cd9660_node.h>
59*82f2275bSRicardo Branco #include <fs/cd9660/cd9660_mount.h>
60a8d36d0dSCraig Rodrigues #include <fs/cd9660/iso_rrip.h>
61df8bae1dSRodney W. Grimes 
626fde64c7SPoul-Henning Kamp static vop_setattr_t	cd9660_setattr;
6372b3e305SPeter Edwards static vop_open_t	cd9660_open;
646fde64c7SPoul-Henning Kamp static vop_access_t	cd9660_access;
656fde64c7SPoul-Henning Kamp static vop_getattr_t	cd9660_getattr;
666fde64c7SPoul-Henning Kamp static vop_ioctl_t	cd9660_ioctl;
676fde64c7SPoul-Henning Kamp static vop_pathconf_t	cd9660_pathconf;
686fde64c7SPoul-Henning Kamp static vop_read_t	cd9660_read;
6910dd32cdSBruce Evans struct isoreaddir;
7089c9a483SAlfred Perlstein static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
7189c9a483SAlfred Perlstein static int iso_shipdir(struct isoreaddir *idp);
726fde64c7SPoul-Henning Kamp static vop_readdir_t	cd9660_readdir;
736fde64c7SPoul-Henning Kamp static vop_readlink_t	cd9660_readlink;
746fde64c7SPoul-Henning Kamp static vop_strategy_t	cd9660_strategy;
7510bcafe9SPawel Jakub Dawidek static vop_vptofh_t	cd9660_vptofh;
76c329ee71SKonstantin Belousov static vop_getpages_t	cd9660_getpages;
7710dd32cdSBruce Evans 
78df8bae1dSRodney W. Grimes /*
79de8583ceSDavid Greenman  * Setattr call. Only allowed for block and character special devices.
80de8583ceSDavid Greenman  */
8137c84183SPoul-Henning Kamp static int
cd9660_setattr(struct vop_setattr_args * ap)82a5f59e85SEd Maste cd9660_setattr(struct vop_setattr_args *ap)
83de8583ceSDavid Greenman {
84de8583ceSDavid Greenman 	struct vnode *vp = ap->a_vp;
85de8583ceSDavid Greenman 	struct vattr *vap = ap->a_vap;
86de8583ceSDavid Greenman 
8792579404SAlexander Langer 	if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
8895a1574eSNate Williams 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
8995a1574eSNate Williams 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
90de8583ceSDavid Greenman 		return (EROFS);
9192579404SAlexander Langer 	if (vap->va_size != (u_quad_t)VNOVAL) {
92de8583ceSDavid Greenman 		switch (vp->v_type) {
93de8583ceSDavid Greenman 		case VDIR:
94de8583ceSDavid Greenman 			return (EISDIR);
95de8583ceSDavid Greenman 		case VLNK:
96de8583ceSDavid Greenman 		case VREG:
97de8583ceSDavid Greenman 			return (EROFS);
98de8583ceSDavid Greenman 		case VCHR:
99de8583ceSDavid Greenman 		case VBLK:
100de8583ceSDavid Greenman 		case VSOCK:
101de8583ceSDavid Greenman 		case VFIFO:
102d254af07SMatthew Dillon 		case VNON:
103d254af07SMatthew Dillon 		case VBAD:
10482be0a5aSTor Egge 		case VMARKER:
105de8583ceSDavid Greenman 			return (0);
106de8583ceSDavid Greenman 		}
107de8583ceSDavid Greenman 	}
108996c772fSJohn Dyson 	return (0);
109de8583ceSDavid Greenman }
110de8583ceSDavid Greenman 
111de8583ceSDavid Greenman /*
112df8bae1dSRodney W. Grimes  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
113df8bae1dSRodney W. Grimes  * The mode is shifted to select the owner/group/other fields. The
114df8bae1dSRodney W. Grimes  * super user is granted all permissions.
115df8bae1dSRodney W. Grimes  */
116df8bae1dSRodney W. Grimes /* ARGSUSED */
117605e9724SPoul-Henning Kamp static int
cd9660_access(struct vop_access_args * ap)118a5f59e85SEd Maste cd9660_access(struct vop_access_args *ap)
119df8bae1dSRodney W. Grimes {
120996c772fSJohn Dyson 	struct vnode *vp = ap->a_vp;
121996c772fSJohn Dyson 	struct iso_node *ip = VTOI(vp);
12215bc6b2bSEdward Tomasz Napierala 	accmode_t accmode = ap->a_accmode;
123*82f2275bSRicardo Branco 	accmode_t file_mode;
124*82f2275bSRicardo Branco 	uid_t uid;
125*82f2275bSRicardo Branco 	gid_t gid;
126996c772fSJohn Dyson 
127bc710003SPoul-Henning Kamp 	if (vp->v_type == VCHR || vp->v_type == VBLK)
128a3679878SPoul-Henning Kamp 		return (EOPNOTSUPP);
129a3679878SPoul-Henning Kamp 
130ba14c327SDavid Greenman 	/*
131996c772fSJohn Dyson 	 * Disallow write attempts unless the file is a socket,
132996c772fSJohn Dyson 	 * fifo, or a block or character device resident on the
133996c772fSJohn Dyson 	 * filesystem.
134ba14c327SDavid Greenman 	 */
13515bc6b2bSEdward Tomasz Napierala 	if (accmode & VWRITE) {
136996c772fSJohn Dyson 		switch (vp->v_type) {
137ba14c327SDavid Greenman 		case VDIR:
138ba14c327SDavid Greenman 		case VLNK:
139ba14c327SDavid Greenman 		case VREG:
140ba14c327SDavid Greenman 			return (EROFS);
141d254af07SMatthew Dillon 			/* NOT REACHED */
142d254af07SMatthew Dillon 		default:
143d254af07SMatthew Dillon 			break;
144ba14c327SDavid Greenman 		}
145ba14c327SDavid Greenman 	}
146ba14c327SDavid Greenman 
147*82f2275bSRicardo Branco 	file_mode = ip->inode.iso_mode;
148*82f2275bSRicardo Branco 	file_mode &= (vp->v_type == VDIR) ? ip->i_mnt->im_dmask : ip->i_mnt->im_fmask;
149*82f2275bSRicardo Branco 
150*82f2275bSRicardo Branco 	uid = (ip->i_mnt->im_flags & ISOFSMNT_UID) ?
151*82f2275bSRicardo Branco 		ip->i_mnt->im_uid : ip->inode.iso_uid;
152*82f2275bSRicardo Branco 	gid = (ip->i_mnt->im_flags & ISOFSMNT_GID) ?
153*82f2275bSRicardo Branco 		ip->i_mnt->im_gid : ip->inode.iso_gid;
154*82f2275bSRicardo Branco 
155*82f2275bSRicardo Branco 	return (vaccess(vp->v_type, file_mode, uid,
156*82f2275bSRicardo Branco 	    gid, ap->a_accmode, ap->a_cred));
157df8bae1dSRodney W. Grimes }
158df8bae1dSRodney W. Grimes 
159605e9724SPoul-Henning Kamp static int
cd9660_open(struct vop_open_args * ap)160a5f59e85SEd Maste cd9660_open(struct vop_open_args *ap)
16172b3e305SPeter Edwards {
16204c98d46SJohn Baldwin 	struct vnode *vp = ap->a_vp;
16304c98d46SJohn Baldwin 	struct iso_node *ip = VTOI(vp);
16472b3e305SPeter Edwards 
16504c98d46SJohn Baldwin 	if (vp->v_type == VCHR || vp->v_type == VBLK)
16604c98d46SJohn Baldwin 		return (EOPNOTSUPP);
16704c98d46SJohn Baldwin 
16804c98d46SJohn Baldwin 	vnode_create_vobject(vp, ip->i_size, ap->a_td);
16904c98d46SJohn Baldwin 	return (0);
17072b3e305SPeter Edwards }
17172b3e305SPeter Edwards 
17272b3e305SPeter Edwards static int
cd9660_getattr(struct vop_getattr_args * ap)173a5f59e85SEd Maste cd9660_getattr(struct vop_getattr_args *ap)
174df8bae1dSRodney W. Grimes 
175df8bae1dSRodney W. Grimes {
176df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
1778994a245SDag-Erling Smørgrav 	struct vattr *vap = ap->a_vap;
1788994a245SDag-Erling Smørgrav 	struct iso_node *ip = VTOI(vp);
179df8bae1dSRodney W. Grimes 
180e3b803b1SPoul-Henning Kamp 	vap->va_fsid    = dev2udev(ip->i_mnt->im_dev);
18182c0aec8STim J. Robbins 	vap->va_fileid	= ip->i_number;
182df8bae1dSRodney W. Grimes 
183df8bae1dSRodney W. Grimes 	vap->va_mode	= ip->inode.iso_mode;
184*82f2275bSRicardo Branco 	vap->va_mode &= (vp->v_type == VDIR) ? ip->i_mnt->im_dmask : ip->i_mnt->im_fmask;
185*82f2275bSRicardo Branco 
186df8bae1dSRodney W. Grimes 	vap->va_nlink	= ip->inode.iso_links;
187*82f2275bSRicardo Branco 	vap->va_uid	= (ip->i_mnt->im_flags & ISOFSMNT_UID) ?
188*82f2275bSRicardo Branco 			ip->i_mnt->im_uid : ip->inode.iso_uid;
189*82f2275bSRicardo Branco 	vap->va_gid	= (ip->i_mnt->im_flags & ISOFSMNT_GID) ?
190*82f2275bSRicardo Branco 			ip->i_mnt->im_gid : ip->inode.iso_gid;
191df8bae1dSRodney W. Grimes 	vap->va_atime	= ip->inode.iso_atime;
192df8bae1dSRodney W. Grimes 	vap->va_mtime	= ip->inode.iso_mtime;
193df8bae1dSRodney W. Grimes 	vap->va_ctime	= ip->inode.iso_ctime;
194df8bae1dSRodney W. Grimes 	vap->va_rdev	= ip->inode.iso_rdev;
195df8bae1dSRodney W. Grimes 
196df8bae1dSRodney W. Grimes 	vap->va_size	= (u_quad_t) ip->i_size;
197996c772fSJohn Dyson 	if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
198996c772fSJohn Dyson 		struct vop_readlink_args rdlnk;
199996c772fSJohn Dyson 		struct iovec aiov;
200996c772fSJohn Dyson 		struct uio auio;
201996c772fSJohn Dyson 		char *cp;
202996c772fSJohn Dyson 
2031ede983cSDag-Erling Smørgrav 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
204996c772fSJohn Dyson 		aiov.iov_base = cp;
205996c772fSJohn Dyson 		aiov.iov_len = MAXPATHLEN;
206996c772fSJohn Dyson 		auio.uio_iov = &aiov;
207996c772fSJohn Dyson 		auio.uio_iovcnt = 1;
208996c772fSJohn Dyson 		auio.uio_offset = 0;
209996c772fSJohn Dyson 		auio.uio_rw = UIO_READ;
210996c772fSJohn Dyson 		auio.uio_segflg = UIO_SYSSPACE;
2110359a12eSAttilio Rao 		auio.uio_td = curthread;
212996c772fSJohn Dyson 		auio.uio_resid = MAXPATHLEN;
213996c772fSJohn Dyson 		rdlnk.a_uio = &auio;
214996c772fSJohn Dyson 		rdlnk.a_vp = ap->a_vp;
215996c772fSJohn Dyson 		rdlnk.a_cred = ap->a_cred;
216996c772fSJohn Dyson 		if (cd9660_readlink(&rdlnk) == 0)
217996c772fSJohn Dyson 			vap->va_size = MAXPATHLEN - auio.uio_resid;
2181ede983cSDag-Erling Smørgrav 		free(cp, M_TEMP);
219996c772fSJohn Dyson 	}
220df8bae1dSRodney W. Grimes 	vap->va_flags	= 0;
221df8bae1dSRodney W. Grimes 	vap->va_gen = 1;
222df8bae1dSRodney W. Grimes 	vap->va_blocksize = ip->i_mnt->logical_block_size;
223df8bae1dSRodney W. Grimes 	vap->va_bytes	= (u_quad_t) ip->i_size;
224df8bae1dSRodney W. Grimes 	vap->va_type	= vp->v_type;
22594a8606fSDoug Rabson 	vap->va_filerev	= 0;
226df8bae1dSRodney W. Grimes 	return (0);
227df8bae1dSRodney W. Grimes }
228df8bae1dSRodney W. Grimes 
229df8bae1dSRodney W. Grimes /*
230cb2a8dffSSøren Schmidt  * Vnode op for ioctl.
231cb2a8dffSSøren Schmidt  */
232cb2a8dffSSøren Schmidt static int
cd9660_ioctl(struct vop_ioctl_args * ap)233a5f59e85SEd Maste cd9660_ioctl(struct vop_ioctl_args *ap)
234cb2a8dffSSøren Schmidt {
235c4df27d5SKonstantin Belousov 	struct vnode *vp;
236c4df27d5SKonstantin Belousov 	struct iso_node *ip;
237c4df27d5SKonstantin Belousov 	int error;
238cb2a8dffSSøren Schmidt 
239c4df27d5SKonstantin Belousov 	vp = ap->a_vp;
240c4df27d5SKonstantin Belousov 	vn_lock(vp, LK_SHARED | LK_RETRY);
241abd80ddbSMateusz Guzik 	if (VN_IS_DOOMED(vp)) {
242b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
24393bc76dcSKonstantin Belousov 		return (EBADF);
24493bc76dcSKonstantin Belousov 	}
245c4df27d5SKonstantin Belousov 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
246b249ce48SMateusz Guzik 		VOP_UNLOCK(vp);
247a3679878SPoul-Henning Kamp 		return (EOPNOTSUPP);
248c4df27d5SKonstantin Belousov 	}
249c4df27d5SKonstantin Belousov 
250c4df27d5SKonstantin Belousov 	ip = VTOI(vp);
251c4df27d5SKonstantin Belousov 	error = 0;
252a3679878SPoul-Henning Kamp 
253cb2a8dffSSøren Schmidt 	switch (ap->a_command) {
254cb2a8dffSSøren Schmidt 	case FIOGETLBA:
255cb2a8dffSSøren Schmidt 		*(int *)(ap->a_data) = ip->iso_start;
256c4df27d5SKonstantin Belousov 		break;
257cb2a8dffSSøren Schmidt 	default:
258c4df27d5SKonstantin Belousov 		error = ENOTTY;
259c4df27d5SKonstantin Belousov 		break;
260cb2a8dffSSøren Schmidt 	}
261c4df27d5SKonstantin Belousov 
262b249ce48SMateusz Guzik 	VOP_UNLOCK(vp);
263c4df27d5SKonstantin Belousov 	return (error);
264cb2a8dffSSøren Schmidt }
265cb2a8dffSSøren Schmidt 
266cb2a8dffSSøren Schmidt /*
267df8bae1dSRodney W. Grimes  * Vnode op for reading.
268df8bae1dSRodney W. Grimes  */
269605e9724SPoul-Henning Kamp static int
cd9660_read(struct vop_read_args * ap)270a5f59e85SEd Maste cd9660_read(struct vop_read_args *ap)
271df8bae1dSRodney W. Grimes {
272df8bae1dSRodney W. Grimes 	struct vnode *vp = ap->a_vp;
2738994a245SDag-Erling Smørgrav 	struct uio *uio = ap->a_uio;
2748994a245SDag-Erling Smørgrav 	struct iso_node *ip = VTOI(vp);
2758994a245SDag-Erling Smørgrav 	struct iso_mnt *imp;
276df8bae1dSRodney W. Grimes 	struct buf *bp;
2771295d82eSGary Palmer 	daddr_t lbn, rablock;
278df8bae1dSRodney W. Grimes 	off_t diff;
279df8bae1dSRodney W. Grimes 	int rasize, error = 0;
28067ddfcafSMatthew Dillon 	int seqcount;
281df8bae1dSRodney W. Grimes 	long size, n, on;
282df8bae1dSRodney W. Grimes 
283bc710003SPoul-Henning Kamp 	if (vp->v_type == VCHR || vp->v_type == VBLK)
284a3679878SPoul-Henning Kamp 		return (EOPNOTSUPP);
285a3679878SPoul-Henning Kamp 
2866bd39fe9SAlexander Kabaev 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
28767ddfcafSMatthew Dillon 
288df8bae1dSRodney W. Grimes 	if (uio->uio_resid == 0)
289df8bae1dSRodney W. Grimes 		return (0);
290df8bae1dSRodney W. Grimes 	if (uio->uio_offset < 0)
291df8bae1dSRodney W. Grimes 		return (EINVAL);
292df8bae1dSRodney W. Grimes 	imp = ip->i_mnt;
293df8bae1dSRodney W. Grimes 	do {
294996c772fSJohn Dyson 		lbn = lblkno(imp, uio->uio_offset);
295996c772fSJohn Dyson 		on = blkoff(imp, uio->uio_offset);
296ea407244SKonstantin Belousov 		n = MIN(imp->logical_block_size - on, uio->uio_resid);
297df8bae1dSRodney W. Grimes 		diff = (off_t)ip->i_size - uio->uio_offset;
298df8bae1dSRodney W. Grimes 		if (diff <= 0)
299df8bae1dSRodney W. Grimes 			return (0);
300df8bae1dSRodney W. Grimes 		if (diff < n)
301df8bae1dSRodney W. Grimes 			n = diff;
302996c772fSJohn Dyson 		size = blksize(imp, ip, lbn);
303df8bae1dSRodney W. Grimes 		rablock = lbn + 1;
30481bca6ddSKATO Takenori 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
3055a5573fdSBruce Evans 			if (lblktosize(imp, rablock) < ip->i_size)
306996c772fSJohn Dyson 				error = cluster_read(vp, (off_t)ip->i_size,
3078b612c4bSJohn Dyson 					 lbn, size, NOCRED, uio->uio_resid,
308c535690bSKonstantin Belousov 					 (ap->a_ioflag >> 16), 0, &bp);
309df8bae1dSRodney W. Grimes 			else
310df8bae1dSRodney W. Grimes 				error = bread(vp, lbn, size, NOCRED, &bp);
311df8bae1dSRodney W. Grimes 		} else {
31267ddfcafSMatthew Dillon 			if (seqcount > 1 &&
313996c772fSJohn Dyson 			    lblktosize(imp, rablock) < ip->i_size) {
314996c772fSJohn Dyson 				rasize = blksize(imp, ip, rablock);
315df8bae1dSRodney W. Grimes 				error = breadn(vp, lbn, size, &rablock,
316df8bae1dSRodney W. Grimes 					       &rasize, 1, NOCRED, &bp);
317df8bae1dSRodney W. Grimes 			} else
318df8bae1dSRodney W. Grimes 				error = bread(vp, lbn, size, NOCRED, &bp);
319df8bae1dSRodney W. Grimes 		}
320cc4916adSKonstantin Belousov 		if (error != 0)
321df8bae1dSRodney W. Grimes 			return (error);
322cc4916adSKonstantin Belousov 		n = MIN(n, size - bp->b_resid);
323df8bae1dSRodney W. Grimes 
324996c772fSJohn Dyson 		error = uiomove(bp->b_data + on, (int)n, uio);
325df8bae1dSRodney W. Grimes 		brelse(bp);
326df8bae1dSRodney W. Grimes 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
327df8bae1dSRodney W. Grimes 	return (error);
328df8bae1dSRodney W. Grimes }
329df8bae1dSRodney W. Grimes 
330df8bae1dSRodney W. Grimes /*
331df8bae1dSRodney W. Grimes  * Structure for reading directories
332df8bae1dSRodney W. Grimes  */
333df8bae1dSRodney W. Grimes struct isoreaddir {
334df8bae1dSRodney W. Grimes 	struct dirent saveent;
335df8bae1dSRodney W. Grimes 	struct dirent assocent;
336df8bae1dSRodney W. Grimes 	struct dirent current;
337df8bae1dSRodney W. Grimes 	off_t saveoff;
338df8bae1dSRodney W. Grimes 	off_t assocoff;
339df8bae1dSRodney W. Grimes 	off_t curroff;
340df8bae1dSRodney W. Grimes 	struct uio *uio;
341df8bae1dSRodney W. Grimes 	off_t uio_off;
342996c772fSJohn Dyson 	int eofflag;
343b214fcceSAlan Somers 	uint64_t *cookies;
344df8bae1dSRodney W. Grimes 	int ncookies;
345df8bae1dSRodney W. Grimes };
346df8bae1dSRodney W. Grimes 
34737c84183SPoul-Henning Kamp static int
iso_uiodir(struct isoreaddir * idp,struct dirent * dp,off_t off)348a5f59e85SEd Maste iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
349df8bae1dSRodney W. Grimes {
350df8bae1dSRodney W. Grimes 	int error;
351df8bae1dSRodney W. Grimes 
352c90607baSBruce Evans 	dp->d_reclen = GENERIC_DIRSIZ(dp);
3536d2e2df7SMark Johnston 	dirent_terminate(dp);
354df8bae1dSRodney W. Grimes 
355df8bae1dSRodney W. Grimes 	if (idp->uio->uio_resid < dp->d_reclen) {
356996c772fSJohn Dyson 		idp->eofflag = 0;
357996c772fSJohn Dyson 		return (-1);
358df8bae1dSRodney W. Grimes 	}
359df8bae1dSRodney W. Grimes 
360996c772fSJohn Dyson 	if (idp->cookies) {
361df8bae1dSRodney W. Grimes 		if (idp->ncookies <= 0) {
362996c772fSJohn Dyson 			idp->eofflag = 0;
363996c772fSJohn Dyson 			return (-1);
364df8bae1dSRodney W. Grimes 		}
365df8bae1dSRodney W. Grimes 
366996c772fSJohn Dyson 		*idp->cookies++ = off;
367df8bae1dSRodney W. Grimes 		--idp->ncookies;
368df8bae1dSRodney W. Grimes 	}
369df8bae1dSRodney W. Grimes 
370c9524588SDag-Erling Smørgrav 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
371996c772fSJohn Dyson 		return (error);
372df8bae1dSRodney W. Grimes 	idp->uio_off = off;
373996c772fSJohn Dyson 	return (0);
374df8bae1dSRodney W. Grimes }
375df8bae1dSRodney W. Grimes 
37637c84183SPoul-Henning Kamp static int
iso_shipdir(struct isoreaddir * idp)377a5f59e85SEd Maste iso_shipdir(struct isoreaddir *idp)
378df8bae1dSRodney W. Grimes {
379df8bae1dSRodney W. Grimes 	struct dirent *dp;
380df8bae1dSRodney W. Grimes 	int cl, sl, assoc;
381df8bae1dSRodney W. Grimes 	int error;
382df8bae1dSRodney W. Grimes 	char *cname, *sname;
383df8bae1dSRodney W. Grimes 
384df8bae1dSRodney W. Grimes 	cl = idp->current.d_namlen;
385df8bae1dSRodney W. Grimes 	cname = idp->current.d_name;
3861295d82eSGary Palmer 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
3871295d82eSGary Palmer 	if (assoc) {
388df8bae1dSRodney W. Grimes 		cl--;
389df8bae1dSRodney W. Grimes 		cname++;
390df8bae1dSRodney W. Grimes 	}
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes 	dp = &idp->saveent;
393df8bae1dSRodney W. Grimes 	sname = dp->d_name;
394df8bae1dSRodney W. Grimes 	if (!(sl = dp->d_namlen)) {
395df8bae1dSRodney W. Grimes 		dp = &idp->assocent;
396df8bae1dSRodney W. Grimes 		sname = dp->d_name + 1;
397df8bae1dSRodney W. Grimes 		sl = dp->d_namlen - 1;
398df8bae1dSRodney W. Grimes 	}
399df8bae1dSRodney W. Grimes 	if (sl > 0) {
400df8bae1dSRodney W. Grimes 		if (sl != cl
401df8bae1dSRodney W. Grimes 		    || bcmp(sname,cname,sl)) {
402df8bae1dSRodney W. Grimes 			if (idp->assocent.d_namlen) {
403d254af07SMatthew Dillon 				if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
404996c772fSJohn Dyson 					return (error);
405df8bae1dSRodney W. Grimes 				idp->assocent.d_namlen = 0;
406df8bae1dSRodney W. Grimes 			}
407df8bae1dSRodney W. Grimes 			if (idp->saveent.d_namlen) {
408d254af07SMatthew Dillon 				if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
409996c772fSJohn Dyson 					return (error);
410df8bae1dSRodney W. Grimes 				idp->saveent.d_namlen = 0;
411df8bae1dSRodney W. Grimes 			}
412df8bae1dSRodney W. Grimes 		}
413df8bae1dSRodney W. Grimes 	}
414c90607baSBruce Evans 	idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
415df8bae1dSRodney W. Grimes 	if (assoc) {
416df8bae1dSRodney W. Grimes 		idp->assocoff = idp->curroff;
417a6274b81SEd Maste 		memcpy(&idp->assocent, &idp->current, idp->current.d_reclen);
418df8bae1dSRodney W. Grimes 	} else {
419df8bae1dSRodney W. Grimes 		idp->saveoff = idp->curroff;
420a6274b81SEd Maste 		memcpy(&idp->saveent, &idp->current, idp->current.d_reclen);
421df8bae1dSRodney W. Grimes 	}
422996c772fSJohn Dyson 	return (0);
423df8bae1dSRodney W. Grimes }
424df8bae1dSRodney W. Grimes 
425df8bae1dSRodney W. Grimes /*
426df8bae1dSRodney W. Grimes  * Vnode op for readdir
427df8bae1dSRodney W. Grimes  */
428605e9724SPoul-Henning Kamp static int
cd9660_readdir(struct vop_readdir_args * ap)429a5f59e85SEd Maste cd9660_readdir(struct vop_readdir_args *ap)
430df8bae1dSRodney W. Grimes {
4318994a245SDag-Erling Smørgrav 	struct uio *uio = ap->a_uio;
432df8bae1dSRodney W. Grimes 	struct isoreaddir *idp;
433996c772fSJohn Dyson 	struct vnode *vdp = ap->a_vp;
434996c772fSJohn Dyson 	struct iso_node *dp;
435df8bae1dSRodney W. Grimes 	struct iso_mnt *imp;
436df8bae1dSRodney W. Grimes 	struct buf *bp = NULL;
437996c772fSJohn Dyson 	struct iso_directory_record *ep;
438996c772fSJohn Dyson 	int entryoffsetinblock;
439996c772fSJohn Dyson 	doff_t endsearch;
440996c772fSJohn Dyson 	u_long bmask;
441996c772fSJohn Dyson 	int error = 0;
442996c772fSJohn Dyson 	int reclen;
443996c772fSJohn Dyson 	u_short namelen;
444d821d364SPedro F. Giffuni 	u_int ncookies = 0;
445b214fcceSAlan Somers 	uint64_t *cookies = NULL;
446dbaab6e6SConrad Meyer 	cd_ino_t ino;
447df8bae1dSRodney W. Grimes 
448996c772fSJohn Dyson 	dp = VTOI(vdp);
449996c772fSJohn Dyson 	imp = dp->i_mnt;
450996c772fSJohn Dyson 	bmask = imp->im_bmask;
451df8bae1dSRodney W. Grimes 
4521ede983cSDag-Erling Smørgrav 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
453996c772fSJohn Dyson 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
454996c772fSJohn Dyson 	/*
455996c772fSJohn Dyson 	 * XXX
456996c772fSJohn Dyson 	 * Is it worth trying to figure out the type?
457996c772fSJohn Dyson 	 */
458996c772fSJohn Dyson 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
459996c772fSJohn Dyson 	    DT_UNKNOWN;
460df8bae1dSRodney W. Grimes 	idp->uio = uio;
461996c772fSJohn Dyson 	if (ap->a_ncookies == NULL) {
462996c772fSJohn Dyson 		idp->cookies = NULL;
463996c772fSJohn Dyson 	} else {
4649abf4d6eSDoug Rabson 		/*
4659abf4d6eSDoug Rabson 		 * Guess the number of cookies needed.
4669abf4d6eSDoug Rabson 		 */
4679abf4d6eSDoug Rabson 		ncookies = uio->uio_resid / 16;
468b214fcceSAlan Somers 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
469996c772fSJohn Dyson 		idp->cookies = cookies;
470df8bae1dSRodney W. Grimes 		idp->ncookies = ncookies;
471996c772fSJohn Dyson 	}
472996c772fSJohn Dyson 	idp->eofflag = 1;
473df8bae1dSRodney W. Grimes 	idp->curroff = uio->uio_offset;
474ac8b6eddSKonstantin Belousov 	idp->uio_off = uio->uio_offset;
475df8bae1dSRodney W. Grimes 
476996c772fSJohn Dyson 	if ((entryoffsetinblock = idp->curroff & bmask) &&
477cec0f20cSPoul-Henning Kamp 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
4781ede983cSDag-Erling Smørgrav 		free(idp, M_TEMP);
479df8bae1dSRodney W. Grimes 		return (error);
480df8bae1dSRodney W. Grimes 	}
481996c772fSJohn Dyson 	endsearch = dp->i_size;
482df8bae1dSRodney W. Grimes 
483df8bae1dSRodney W. Grimes 	while (idp->curroff < endsearch) {
484df8bae1dSRodney W. Grimes 		/*
485df8bae1dSRodney W. Grimes 		 * If offset is on a block boundary,
486df8bae1dSRodney W. Grimes 		 * read the next directory block.
487df8bae1dSRodney W. Grimes 		 * Release previous if it exists.
488df8bae1dSRodney W. Grimes 		 */
489996c772fSJohn Dyson 		if ((idp->curroff & bmask) == 0) {
490df8bae1dSRodney W. Grimes 			if (bp != NULL)
491df8bae1dSRodney W. Grimes 				brelse(bp);
492d254af07SMatthew Dillon 			if ((error =
493d254af07SMatthew Dillon 			    cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
494df8bae1dSRodney W. Grimes 				break;
495df8bae1dSRodney W. Grimes 			entryoffsetinblock = 0;
496df8bae1dSRodney W. Grimes 		}
497df8bae1dSRodney W. Grimes 		/*
498df8bae1dSRodney W. Grimes 		 * Get pointer to next entry.
499df8bae1dSRodney W. Grimes 		 */
500df8bae1dSRodney W. Grimes 		ep = (struct iso_directory_record *)
501996c772fSJohn Dyson 			((char *)bp->b_data + entryoffsetinblock);
502df8bae1dSRodney W. Grimes 
503df8bae1dSRodney W. Grimes 		reclen = isonum_711(ep->length);
504df8bae1dSRodney W. Grimes 		if (reclen == 0) {
505df8bae1dSRodney W. Grimes 			/* skip to next block, if any */
506996c772fSJohn Dyson 			idp->curroff =
507996c772fSJohn Dyson 			    (idp->curroff & ~bmask) + imp->logical_block_size;
508df8bae1dSRodney W. Grimes 			continue;
509df8bae1dSRodney W. Grimes 		}
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
512df8bae1dSRodney W. Grimes 			error = EINVAL;
513df8bae1dSRodney W. Grimes 			/* illegal entry, stop */
514df8bae1dSRodney W. Grimes 			break;
515df8bae1dSRodney W. Grimes 		}
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
518df8bae1dSRodney W. Grimes 			error = EINVAL;
519df8bae1dSRodney W. Grimes 			/* illegal directory, so stop looking */
520df8bae1dSRodney W. Grimes 			break;
521df8bae1dSRodney W. Grimes 		}
522df8bae1dSRodney W. Grimes 
523996c772fSJohn Dyson 		idp->current.d_namlen = isonum_711(ep->name_len);
524996c772fSJohn Dyson 
525996c772fSJohn Dyson 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
52681ec856aSJoerg Wunsch 			error = EINVAL;
52781ec856aSJoerg Wunsch 			/* illegal entry, stop */
52881ec856aSJoerg Wunsch 			break;
52981ec856aSJoerg Wunsch 		}
53081ec856aSJoerg Wunsch 
53182c0aec8STim J. Robbins 		if (isonum_711(ep->flags)&2)
53282c0aec8STim J. Robbins 			idp->current.d_fileno = isodirino(ep, imp);
53382c0aec8STim J. Robbins 		else
53482c0aec8STim J. Robbins 			idp->current.d_fileno = dbtob(bp->b_blkno) +
53582c0aec8STim J. Robbins 				entryoffsetinblock;
536df8bae1dSRodney W. Grimes 
537df8bae1dSRodney W. Grimes 		idp->curroff += reclen;
5381c4ca778SKonstantin Belousov 		/* NOTE: d_off is the offset of *next* entry. */
5391c4ca778SKonstantin Belousov 		idp->current.d_off = idp->curroff;
540996c772fSJohn Dyson 
541df8bae1dSRodney W. Grimes 		switch (imp->iso_ftype) {
542df8bae1dSRodney W. Grimes 		case ISO_FTYPE_RRIP:
543dbaab6e6SConrad Meyer 			ino = idp->current.d_fileno;
544996c772fSJohn Dyson 			cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
545dbaab6e6SConrad Meyer 			    &ino, imp);
546dbaab6e6SConrad Meyer 			idp->current.d_fileno = ino;
547996c772fSJohn Dyson 			idp->current.d_namlen = (u_char)namelen;
548df8bae1dSRodney W. Grimes 			if (idp->current.d_namlen)
549df8bae1dSRodney W. Grimes 				error = iso_uiodir(idp,&idp->current,idp->curroff);
550df8bae1dSRodney W. Grimes 			break;
551988fa8efSJoerg Wunsch 		default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
552df8bae1dSRodney W. Grimes 			strcpy(idp->current.d_name,"..");
55344e568e2SDaniel C. Sobral 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
554df8bae1dSRodney W. Grimes 				idp->current.d_namlen = 1;
555df8bae1dSRodney W. Grimes 				error = iso_uiodir(idp,&idp->current,idp->curroff);
55644e568e2SDaniel C. Sobral 			} else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
557df8bae1dSRodney W. Grimes 				idp->current.d_namlen = 2;
558df8bae1dSRodney W. Grimes 				error = iso_uiodir(idp,&idp->current,idp->curroff);
55944e568e2SDaniel C. Sobral 			} else {
560df8bae1dSRodney W. Grimes 				isofntrans(ep->name,idp->current.d_namlen,
561996c772fSJohn Dyson 					   idp->current.d_name, &namelen,
562df8bae1dSRodney W. Grimes 					   imp->iso_ftype == ISO_FTYPE_9660,
56344e568e2SDaniel C. Sobral 					   isonum_711(ep->flags)&4,
564c4f02a89SMax Khon 					   imp->joliet_level,
565c4f02a89SMax Khon 					   imp->im_flags,
566c4f02a89SMax Khon 					   imp->im_d2l);
567996c772fSJohn Dyson 				idp->current.d_namlen = (u_char)namelen;
568df8bae1dSRodney W. Grimes 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
569df8bae1dSRodney W. Grimes 					error = iso_shipdir(idp);
570df8bae1dSRodney W. Grimes 				else
571df8bae1dSRodney W. Grimes 					error = iso_uiodir(idp,&idp->current,idp->curroff);
572df8bae1dSRodney W. Grimes 			}
573df8bae1dSRodney W. Grimes 		}
574df8bae1dSRodney W. Grimes 		if (error)
575df8bae1dSRodney W. Grimes 			break;
576df8bae1dSRodney W. Grimes 
577df8bae1dSRodney W. Grimes 		entryoffsetinblock += reclen;
578df8bae1dSRodney W. Grimes 	}
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
581df8bae1dSRodney W. Grimes 		idp->current.d_namlen = 0;
582df8bae1dSRodney W. Grimes 		error = iso_shipdir(idp);
583df8bae1dSRodney W. Grimes 	}
584df8bae1dSRodney W. Grimes 	if (error < 0)
585df8bae1dSRodney W. Grimes 		error = 0;
586df8bae1dSRodney W. Grimes 
5879abf4d6eSDoug Rabson 	if (ap->a_ncookies != NULL) {
5889abf4d6eSDoug Rabson 		if (error)
589996c772fSJohn Dyson 			free(cookies, M_TEMP);
5909abf4d6eSDoug Rabson 		else {
5919abf4d6eSDoug Rabson 			/*
5929abf4d6eSDoug Rabson 			 * Work out the number of cookies actually used.
5939abf4d6eSDoug Rabson 			 */
5949abf4d6eSDoug Rabson 			*ap->a_ncookies = ncookies - idp->ncookies;
5959abf4d6eSDoug Rabson 			*ap->a_cookies = cookies;
5969abf4d6eSDoug Rabson 		}
5979abf4d6eSDoug Rabson 	}
5989abf4d6eSDoug Rabson 
599df8bae1dSRodney W. Grimes 	if (bp)
600df8bae1dSRodney W. Grimes 		brelse (bp);
601df8bae1dSRodney W. Grimes 
602df8bae1dSRodney W. Grimes 	uio->uio_offset = idp->uio_off;
603996c772fSJohn Dyson 	*ap->a_eofflag = idp->eofflag;
604df8bae1dSRodney W. Grimes 
6051ede983cSDag-Erling Smørgrav 	free(idp, M_TEMP);
606df8bae1dSRodney W. Grimes 
607df8bae1dSRodney W. Grimes 	return (error);
608df8bae1dSRodney W. Grimes }
609df8bae1dSRodney W. Grimes 
610df8bae1dSRodney W. Grimes /*
611df8bae1dSRodney W. Grimes  * Return target name of a symbolic link
612df8bae1dSRodney W. Grimes  * Shouldn't we get the parent vnode and read the data from there?
613df8bae1dSRodney W. Grimes  * This could eventually result in deadlocks in cd9660_lookup.
614df8bae1dSRodney W. Grimes  * But otherwise the block read here is in the block buffer two times.
615df8bae1dSRodney W. Grimes  */
616df8bae1dSRodney W. Grimes typedef struct iso_directory_record ISODIR;
617df8bae1dSRodney W. Grimes typedef struct iso_node		    ISONODE;
618df8bae1dSRodney W. Grimes typedef struct iso_mnt		    ISOMNT;
619605e9724SPoul-Henning Kamp static int
cd9660_readlink(struct vop_readlink_args * ap)620a5f59e85SEd Maste cd9660_readlink(struct vop_readlink_args *ap)
621df8bae1dSRodney W. Grimes {
622df8bae1dSRodney W. Grimes 	ISONODE	*ip;
623df8bae1dSRodney W. Grimes 	ISODIR	*dirp;
624df8bae1dSRodney W. Grimes 	ISOMNT	*imp;
625df8bae1dSRodney W. Grimes 	struct	buf *bp;
626996c772fSJohn Dyson 	struct	uio *uio;
627df8bae1dSRodney W. Grimes 	u_short	symlen;
628df8bae1dSRodney W. Grimes 	int	error;
629df8bae1dSRodney W. Grimes 	char	*symname;
630df8bae1dSRodney W. Grimes 
631df8bae1dSRodney W. Grimes 	ip  = VTOI(ap->a_vp);
632df8bae1dSRodney W. Grimes 	imp = ip->i_mnt;
633996c772fSJohn Dyson 	uio = ap->a_uio;
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
636996c772fSJohn Dyson 		return (EINVAL);
637df8bae1dSRodney W. Grimes 
638df8bae1dSRodney W. Grimes 	/*
639df8bae1dSRodney W. Grimes 	 * Get parents directory record block that this inode included.
640df8bae1dSRodney W. Grimes 	 */
641df8bae1dSRodney W. Grimes 	error = bread(imp->im_devvp,
642996c772fSJohn Dyson 		      (ip->i_number >> imp->im_bshift) <<
643996c772fSJohn Dyson 		      (imp->im_bshift - DEV_BSHIFT),
644996c772fSJohn Dyson 		      imp->logical_block_size, NOCRED, &bp);
645df8bae1dSRodney W. Grimes 	if (error) {
646996c772fSJohn Dyson 		return (EINVAL);
647df8bae1dSRodney W. Grimes 	}
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 	/*
650df8bae1dSRodney W. Grimes 	 * Setup the directory pointer for this inode
651df8bae1dSRodney W. Grimes 	 */
652996c772fSJohn Dyson 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
653df8bae1dSRodney W. Grimes 
654df8bae1dSRodney W. Grimes 	/*
655df8bae1dSRodney W. Grimes 	 * Just make sure, we have a right one....
656df8bae1dSRodney W. Grimes 	 *   1: Check not cross boundary on block
657df8bae1dSRodney W. Grimes 	 */
658df8bae1dSRodney W. Grimes 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
65992579404SAlexander Langer 	    > (unsigned)imp->logical_block_size) {
660df8bae1dSRodney W. Grimes 		brelse(bp);
661996c772fSJohn Dyson 		return (EINVAL);
662df8bae1dSRodney W. Grimes 	}
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 	/*
665df8bae1dSRodney W. Grimes 	 * Now get a buffer
666df8bae1dSRodney W. Grimes 	 * Abuse a namei buffer for now.
667df8bae1dSRodney W. Grimes 	 */
668996c772fSJohn Dyson 	if (uio->uio_segflg == UIO_SYSSPACE)
669996c772fSJohn Dyson 		symname = uio->uio_iov->iov_base;
670996c772fSJohn Dyson 	else
671a163d034SWarner Losh 		symname = uma_zalloc(namei_zone, M_WAITOK);
672df8bae1dSRodney W. Grimes 
673df8bae1dSRodney W. Grimes 	/*
674df8bae1dSRodney W. Grimes 	 * Ok, we just gathering a symbolic name in SL record.
675df8bae1dSRodney W. Grimes 	 */
676df8bae1dSRodney W. Grimes 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
677996c772fSJohn Dyson 		if (uio->uio_segflg != UIO_SYSSPACE)
6782684b6afSJeff Roberson 			uma_zfree(namei_zone, symname);
679df8bae1dSRodney W. Grimes 		brelse(bp);
680996c772fSJohn Dyson 		return (EINVAL);
681df8bae1dSRodney W. Grimes 	}
682df8bae1dSRodney W. Grimes 	/*
683df8bae1dSRodney W. Grimes 	 * Don't forget before you leave from home ;-)
684df8bae1dSRodney W. Grimes 	 */
685df8bae1dSRodney W. Grimes 	brelse(bp);
686df8bae1dSRodney W. Grimes 
687df8bae1dSRodney W. Grimes 	/*
688df8bae1dSRodney W. Grimes 	 * return with the symbolic name to caller's.
689df8bae1dSRodney W. Grimes 	 */
690996c772fSJohn Dyson 	if (uio->uio_segflg != UIO_SYSSPACE) {
691996c772fSJohn Dyson 		error = uiomove(symname, symlen, uio);
6922684b6afSJeff Roberson 		uma_zfree(namei_zone, symname);
693996c772fSJohn Dyson 		return (error);
694996c772fSJohn Dyson 	}
695996c772fSJohn Dyson 	uio->uio_resid -= symlen;
6962b7f24d2SMike Barcroft 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
697996c772fSJohn Dyson 	uio->uio_iov->iov_len -= symlen;
698996c772fSJohn Dyson 	return (0);
699df8bae1dSRodney W. Grimes }
700df8bae1dSRodney W. Grimes 
701df8bae1dSRodney W. Grimes /*
702df8bae1dSRodney W. Grimes  * Calculate the logical to physical mapping if not done already,
703df8bae1dSRodney W. Grimes  * then call the device strategy routine.
704df8bae1dSRodney W. Grimes  */
705605e9724SPoul-Henning Kamp static int
cd9660_strategy(struct vop_strategy_args * ap)706a5f59e85SEd Maste cd9660_strategy(struct vop_strategy_args *ap)
707df8bae1dSRodney W. Grimes {
7088994a245SDag-Erling Smørgrav 	struct buf *bp = ap->a_bp;
709d83b7498SPoul-Henning Kamp 	struct vnode *vp = ap->a_vp;
7108994a245SDag-Erling Smørgrav 	struct iso_node *ip;
711bf7e2ae1SPoul-Henning Kamp 	struct bufobj *bo;
712df8bae1dSRodney W. Grimes 
713df8bae1dSRodney W. Grimes 	ip = VTOI(vp);
714df8bae1dSRodney W. Grimes 	if (vp->v_type == VBLK || vp->v_type == VCHR)
715df8bae1dSRodney W. Grimes 		panic("cd9660_strategy: spec");
716df8bae1dSRodney W. Grimes 	if (bp->b_blkno == bp->b_lblkno) {
717e9d19a11SPoul-Henning Kamp 		bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
718e9d19a11SPoul-Henning Kamp 		    (ip->i_mnt->im_bshift - DEV_BSHIFT);
719df8bae1dSRodney W. Grimes 	}
7202c18019fSPoul-Henning Kamp 	bp->b_iooffset = dbtob(bp->b_blkno);
721bf7e2ae1SPoul-Henning Kamp 	bo = ip->i_mnt->im_bo;
7220391e5a1SPoul-Henning Kamp 	BO_STRATEGY(bo, bp);
723df8bae1dSRodney W. Grimes 	return (0);
724df8bae1dSRodney W. Grimes }
725df8bae1dSRodney W. Grimes 
726df8bae1dSRodney W. Grimes /*
727996c772fSJohn Dyson  * Return POSIX pathconf information applicable to cd9660 filesystems.
728996c772fSJohn Dyson  */
729f041a9bdSPoul-Henning Kamp static int
cd9660_pathconf(struct vop_pathconf_args * ap)730a5f59e85SEd Maste cd9660_pathconf(struct vop_pathconf_args *ap)
731996c772fSJohn Dyson {
732996c772fSJohn Dyson 
733996c772fSJohn Dyson 	switch (ap->a_name) {
734eb3d4cccSJohn Baldwin 	case _PC_FILESIZEBITS:
735eb3d4cccSJohn Baldwin 		*ap->a_retval = 32;
736eb3d4cccSJohn Baldwin 		return (0);
737996c772fSJohn Dyson 	case _PC_LINK_MAX:
738996c772fSJohn Dyson 		*ap->a_retval = 1;
739996c772fSJohn Dyson 		return (0);
740996c772fSJohn Dyson 	case _PC_NAME_MAX:
741996c772fSJohn Dyson 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
742996c772fSJohn Dyson 			*ap->a_retval = NAME_MAX;
743996c772fSJohn Dyson 		else
744996c772fSJohn Dyson 			*ap->a_retval = 37;
745996c772fSJohn Dyson 		return (0);
746eb3d4cccSJohn Baldwin 	case _PC_SYMLINK_MAX:
747eb3d4cccSJohn Baldwin 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
748eb3d4cccSJohn Baldwin 			*ap->a_retval = MAXPATHLEN;
749eb3d4cccSJohn Baldwin 			return (0);
750eb3d4cccSJohn Baldwin 		}
751eb3d4cccSJohn Baldwin 		return (EINVAL);
752996c772fSJohn Dyson 	case _PC_NO_TRUNC:
753996c772fSJohn Dyson 		*ap->a_retval = 1;
754996c772fSJohn Dyson 		return (0);
755996c772fSJohn Dyson 	default:
75615a88f81SJohn Baldwin 		return (vop_stdpathconf(ap));
757996c772fSJohn Dyson 	}
758996c772fSJohn Dyson 	/* NOTREACHED */
759df8bae1dSRodney W. Grimes }
760df8bae1dSRodney W. Grimes 
761df8bae1dSRodney W. Grimes /*
76210bcafe9SPawel Jakub Dawidek  * Vnode pointer to File handle
76310bcafe9SPawel Jakub Dawidek  */
76410bcafe9SPawel Jakub Dawidek static int
cd9660_vptofh(struct vop_vptofh_args * ap)765a5f59e85SEd Maste cd9660_vptofh(struct vop_vptofh_args *ap)
76610bcafe9SPawel Jakub Dawidek {
7679251d56fSMarius Strobl 	struct ifid ifh;
76810bcafe9SPawel Jakub Dawidek 	struct iso_node *ip = VTOI(ap->a_vp);
76910bcafe9SPawel Jakub Dawidek 
7709251d56fSMarius Strobl 	ifh.ifid_len = sizeof(struct ifid);
77110bcafe9SPawel Jakub Dawidek 
7729251d56fSMarius Strobl 	ifh.ifid_ino = ip->i_number;
7739251d56fSMarius Strobl 	ifh.ifid_start = ip->iso_start;
7749251d56fSMarius Strobl 	/*
7759251d56fSMarius Strobl 	 * This intentionally uses sizeof(ifh) in order to not copy stack
7769251d56fSMarius Strobl 	 * garbage on ILP32.
7779251d56fSMarius Strobl 	 */
7789251d56fSMarius Strobl 	memcpy(ap->a_fhp, &ifh, sizeof(ifh));
77910bcafe9SPawel Jakub Dawidek 
78010bcafe9SPawel Jakub Dawidek #ifdef	ISOFS_DBG
781dbaab6e6SConrad Meyer 	printf("vptofh: ino %jd, start %ld\n",
782dbaab6e6SConrad Meyer 	    (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
78310bcafe9SPawel Jakub Dawidek #endif
7849251d56fSMarius Strobl 
7859251d56fSMarius Strobl 	return (0);
78610bcafe9SPawel Jakub Dawidek }
78710bcafe9SPawel Jakub Dawidek 
7887029da5cSPawel Biernacki SYSCTL_NODE(_vfs, OID_AUTO, cd9660, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
7897029da5cSPawel Biernacki     "cd9660 filesystem");
790c329ee71SKonstantin Belousov static int use_buf_pager = 1;
791c329ee71SKonstantin Belousov SYSCTL_INT(_vfs_cd9660, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
792c329ee71SKonstantin Belousov     &use_buf_pager, 0,
793c329ee71SKonstantin Belousov     "Use buffer pager instead of bmap");
794c329ee71SKonstantin Belousov 
795c329ee71SKonstantin Belousov static daddr_t
cd9660_gbp_getblkno(struct vnode * vp,vm_ooffset_t off)796c329ee71SKonstantin Belousov cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
797c329ee71SKonstantin Belousov {
798c329ee71SKonstantin Belousov 
799c329ee71SKonstantin Belousov 	return (lblkno(VTOI(vp)->i_mnt, off));
800c329ee71SKonstantin Belousov }
801c329ee71SKonstantin Belousov 
802c329ee71SKonstantin Belousov static int
cd9660_gbp_getblksz(struct vnode * vp,daddr_t lbn,long * sz)803197a4f29SKonstantin Belousov cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
804c329ee71SKonstantin Belousov {
805c329ee71SKonstantin Belousov 	struct iso_node *ip;
806c329ee71SKonstantin Belousov 
807c329ee71SKonstantin Belousov 	ip = VTOI(vp);
808197a4f29SKonstantin Belousov 	*sz = blksize(ip->i_mnt, ip, lbn);
809197a4f29SKonstantin Belousov 	return (0);
810c329ee71SKonstantin Belousov }
811c329ee71SKonstantin Belousov 
812c329ee71SKonstantin Belousov static int
cd9660_getpages(struct vop_getpages_args * ap)813c329ee71SKonstantin Belousov cd9660_getpages(struct vop_getpages_args *ap)
814c329ee71SKonstantin Belousov {
815c329ee71SKonstantin Belousov 	struct vnode *vp;
816c329ee71SKonstantin Belousov 
817c329ee71SKonstantin Belousov 	vp = ap->a_vp;
818c329ee71SKonstantin Belousov 	if (vp->v_type == VCHR || vp->v_type == VBLK)
819c329ee71SKonstantin Belousov 		return (EOPNOTSUPP);
820c329ee71SKonstantin Belousov 
821c329ee71SKonstantin Belousov 	if (use_buf_pager)
822c329ee71SKonstantin Belousov 		return (vfs_bio_getpages(vp, ap->a_m, ap->a_count,
823c329ee71SKonstantin Belousov 		    ap->a_rbehind, ap->a_rahead, cd9660_gbp_getblkno,
824c329ee71SKonstantin Belousov 		    cd9660_gbp_getblksz));
825c329ee71SKonstantin Belousov 	return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
826c329ee71SKonstantin Belousov 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
827c329ee71SKonstantin Belousov }
828c329ee71SKonstantin Belousov 
82910bcafe9SPawel Jakub Dawidek /*
830996c772fSJohn Dyson  * Global vfs data structures for cd9660
831df8bae1dSRodney W. Grimes  */
832aec0fb7bSPoul-Henning Kamp struct vop_vector cd9660_vnodeops = {
833aec0fb7bSPoul-Henning Kamp 	.vop_default =		&default_vnodeops,
83472b3e305SPeter Edwards 	.vop_open =		cd9660_open,
835aec0fb7bSPoul-Henning Kamp 	.vop_access =		cd9660_access,
836aec0fb7bSPoul-Henning Kamp 	.vop_bmap =		cd9660_bmap,
837aec0fb7bSPoul-Henning Kamp 	.vop_cachedlookup =	cd9660_lookup,
838aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		cd9660_getattr,
839aec0fb7bSPoul-Henning Kamp 	.vop_inactive =		cd9660_inactive,
840aec0fb7bSPoul-Henning Kamp 	.vop_ioctl =		cd9660_ioctl,
841aec0fb7bSPoul-Henning Kamp 	.vop_lookup =		vfs_cache_lookup,
842aec0fb7bSPoul-Henning Kamp 	.vop_pathconf =		cd9660_pathconf,
843aec0fb7bSPoul-Henning Kamp 	.vop_read =		cd9660_read,
844aec0fb7bSPoul-Henning Kamp 	.vop_readdir =		cd9660_readdir,
845aec0fb7bSPoul-Henning Kamp 	.vop_readlink =		cd9660_readlink,
846aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		cd9660_reclaim,
847aec0fb7bSPoul-Henning Kamp 	.vop_setattr =		cd9660_setattr,
848aec0fb7bSPoul-Henning Kamp 	.vop_strategy =		cd9660_strategy,
84910bcafe9SPawel Jakub Dawidek 	.vop_vptofh =		cd9660_vptofh,
850c329ee71SKonstantin Belousov 	.vop_getpages =		cd9660_getpages,
851df8bae1dSRodney W. Grimes };
8526fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(cd9660_vnodeops);
853df8bae1dSRodney W. Grimes 
854df8bae1dSRodney W. Grimes /*
855df8bae1dSRodney W. Grimes  * Special device vnode ops
856df8bae1dSRodney W. Grimes  */
857df8bae1dSRodney W. Grimes 
858aec0fb7bSPoul-Henning Kamp struct vop_vector cd9660_fifoops = {
859aec0fb7bSPoul-Henning Kamp 	.vop_default =		&fifo_specops,
860aec0fb7bSPoul-Henning Kamp 	.vop_access =		cd9660_access,
861aec0fb7bSPoul-Henning Kamp 	.vop_getattr =		cd9660_getattr,
862aec0fb7bSPoul-Henning Kamp 	.vop_inactive =		cd9660_inactive,
863aec0fb7bSPoul-Henning Kamp 	.vop_reclaim =		cd9660_reclaim,
864aec0fb7bSPoul-Henning Kamp 	.vop_setattr =		cd9660_setattr,
86510bcafe9SPawel Jakub Dawidek 	.vop_vptofh =		cd9660_vptofh,
866df8bae1dSRodney W. Grimes };
8676fa079fcSMateusz Guzik VFS_VOP_VECTOR_REGISTER(cd9660_fifoops);
868