xref: /freebsd/sys/fs/cd9660/cd9660_vnops.c (revision 8e3e3a7ae841ccf6f6ac30a2eeab85df5d7f04bc)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
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).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
23  *
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
34  * SUCH DAMAGE.
35  *
36  *	@(#)cd9660_vnops.c	8.19 (Berkeley) 5/27/95
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/kernel.h>
46 #include <sys/conf.h>
47 #include <sys/stat.h>
48 #include <sys/bio.h>
49 #include <sys/buf.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>
57 
58 #include <vm/vm.h>
59 #include <vm/vnode_pager.h>
60 #include <vm/uma.h>
61 
62 #include <fs/cd9660/iso.h>
63 #include <fs/cd9660/cd9660_node.h>
64 #include <fs/cd9660/iso_rrip.h>
65 
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;
73 struct isoreaddir;
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;
81 
82 /*
83  * Setattr call. Only allowed for block and character special devices.
84  */
85 static int
86 cd9660_setattr(ap)
87 	struct vop_setattr_args /* {
88 		struct vnodeop_desc *a_desc;
89 		struct vnode *a_vp;
90 		struct vattr *a_vap;
91 		struct ucred *a_cred;
92 	} */ *ap;
93 {
94 	struct vnode *vp = ap->a_vp;
95 	struct vattr *vap = ap->a_vap;
96 
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)
100 		return (EROFS);
101 	if (vap->va_size != (u_quad_t)VNOVAL) {
102 		switch (vp->v_type) {
103 		case VDIR:
104 			return (EISDIR);
105 		case VLNK:
106 		case VREG:
107 			return (EROFS);
108 		case VCHR:
109 		case VBLK:
110 		case VSOCK:
111 		case VFIFO:
112 		case VNON:
113 		case VBAD:
114 		case VMARKER:
115 			return (0);
116 		}
117 	}
118 	return (0);
119 }
120 
121 /*
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.
125  */
126 /* ARGSUSED */
127 static int
128 cd9660_access(ap)
129 	struct vop_access_args /* {
130 		struct vnode *a_vp;
131 		accmode_t a_accmode;
132 		struct ucred *a_cred;
133 		struct thread *a_td;
134 	} */ *ap;
135 {
136 	struct vnode *vp = ap->a_vp;
137 	struct iso_node *ip = VTOI(vp);
138 	accmode_t accmode = ap->a_accmode;
139 
140 	if (vp->v_type == VCHR || vp->v_type == VBLK)
141 		return (EOPNOTSUPP);
142 
143 	/*
144 	 * Disallow write attempts unless the file is a socket,
145 	 * fifo, or a block or character device resident on the
146 	 * filesystem.
147 	 */
148 	if (accmode & VWRITE) {
149 		switch (vp->v_type) {
150 		case VDIR:
151 		case VLNK:
152 		case VREG:
153 			return (EROFS);
154 			/* NOT REACHED */
155 		default:
156 			break;
157 		}
158 	}
159 
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));
162 }
163 
164 static int
165 cd9660_open(ap)
166 	struct vop_open_args /* {
167 		struct vnode *a_vp;
168 		int a_mode;
169 		struct ucred *a_cred;
170 		struct thread *a_td;
171 		struct file *a_fp;
172 	} */ *ap;
173 {
174 	struct vnode *vp = ap->a_vp;
175 	struct iso_node *ip = VTOI(vp);
176 
177 	if (vp->v_type == VCHR || vp->v_type == VBLK)
178 		return (EOPNOTSUPP);
179 
180 	vnode_create_vobject(vp, ip->i_size, ap->a_td);
181 	return (0);
182 }
183 
184 
185 static int
186 cd9660_getattr(ap)
187 	struct vop_getattr_args /* {
188 		struct vnode *a_vp;
189 		struct vattr *a_vap;
190 		struct ucred *a_cred;
191 	} */ *ap;
192 
193 {
194 	struct vnode *vp = ap->a_vp;
195 	struct vattr *vap = ap->a_vap;
196 	struct iso_node *ip = VTOI(vp);
197 
198 	vap->va_fsid    = dev2udev(ip->i_mnt->im_dev);
199 	vap->va_fileid	= ip->i_number;
200 
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;
209 
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;
213 		struct iovec aiov;
214 		struct uio auio;
215 		char *cp;
216 
217 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
218 		aiov.iov_base = cp;
219 		aiov.iov_len = MAXPATHLEN;
220 		auio.uio_iov = &aiov;
221 		auio.uio_iovcnt = 1;
222 		auio.uio_offset = 0;
223 		auio.uio_rw = UIO_READ;
224 		auio.uio_segflg = UIO_SYSSPACE;
225 		auio.uio_td = curthread;
226 		auio.uio_resid = MAXPATHLEN;
227 		rdlnk.a_uio = &auio;
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;
232 		free(cp, M_TEMP);
233 	}
234 	vap->va_flags	= 0;
235 	vap->va_gen = 1;
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;
239 	vap->va_filerev	= 0;
240 	return (0);
241 }
242 
243 /*
244  * Vnode op for ioctl.
245  */
246 static int
247 cd9660_ioctl(ap)
248 	struct vop_ioctl_args /* {
249 		struct vnode *a_vp;
250 		u_long  a_command;
251 		caddr_t  a_data;
252 		int  a_fflag;
253 		struct ucred *a_cred;
254 		struct thread *a_td;
255 	} */ *ap;
256 {
257 	struct vnode *vp;
258 	struct iso_node *ip;
259 	int error;
260 
261 	vp = ap->a_vp;
262 	vn_lock(vp, LK_SHARED | LK_RETRY);
263 	if (vp->v_iflag & VI_DOOMED) {
264 		VOP_UNLOCK(vp, 0);
265 		return (EBADF);
266 	}
267 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
268 		VOP_UNLOCK(vp, 0);
269 		return (EOPNOTSUPP);
270 	}
271 
272 	ip = VTOI(vp);
273 	error = 0;
274 
275 	switch (ap->a_command) {
276 	case FIOGETLBA:
277 		*(int *)(ap->a_data) = ip->iso_start;
278 		break;
279 	default:
280 		error = ENOTTY;
281 		break;
282 	}
283 
284 	VOP_UNLOCK(vp, 0);
285 	return (error);
286 }
287 
288 /*
289  * Vnode op for reading.
290  */
291 static int
292 cd9660_read(ap)
293 	struct vop_read_args /* {
294 		struct vnode *a_vp;
295 		struct uio *a_uio;
296 		int a_ioflag;
297 		struct ucred *a_cred;
298 	} */ *ap;
299 {
300 	struct vnode *vp = ap->a_vp;
301 	struct uio *uio = ap->a_uio;
302 	struct iso_node *ip = VTOI(vp);
303 	struct iso_mnt *imp;
304 	struct buf *bp;
305 	daddr_t lbn, rablock;
306 	off_t diff;
307 	int rasize, error = 0;
308 	int seqcount;
309 	long size, n, on;
310 
311 	if (vp->v_type == VCHR || vp->v_type == VBLK)
312 		return (EOPNOTSUPP);
313 
314 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
315 
316 	if (uio->uio_resid == 0)
317 		return (0);
318 	if (uio->uio_offset < 0)
319 		return (EINVAL);
320 	imp = ip->i_mnt;
321 	do {
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;
326 		if (diff <= 0)
327 			return (0);
328 		if (diff < n)
329 			n = diff;
330 		size = blksize(imp, ip, lbn);
331 		rablock = lbn + 1;
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);
337 			else
338 				error = bread(vp, lbn, size, NOCRED, &bp);
339 		} else {
340 			if (seqcount > 1 &&
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);
345 			} else
346 				error = bread(vp, lbn, size, NOCRED, &bp);
347 		}
348 		if (error != 0)
349 			return (error);
350 		n = MIN(n, size - bp->b_resid);
351 
352 		error = uiomove(bp->b_data + on, (int)n, uio);
353 		brelse(bp);
354 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
355 	return (error);
356 }
357 
358 /*
359  * Structure for reading directories
360  */
361 struct isoreaddir {
362 	struct dirent saveent;
363 	struct dirent assocent;
364 	struct dirent current;
365 	off_t saveoff;
366 	off_t assocoff;
367 	off_t curroff;
368 	struct uio *uio;
369 	off_t uio_off;
370 	int eofflag;
371 	u_long *cookies;
372 	int ncookies;
373 };
374 
375 static int
376 iso_uiodir(idp,dp,off)
377 	struct isoreaddir *idp;
378 	struct dirent *dp;
379 	off_t off;
380 {
381 	int error;
382 
383 	dp->d_name[dp->d_namlen] = 0;
384 	dp->d_reclen = GENERIC_DIRSIZ(dp);
385 
386 	if (idp->uio->uio_resid < dp->d_reclen) {
387 		idp->eofflag = 0;
388 		return (-1);
389 	}
390 
391 	if (idp->cookies) {
392 		if (idp->ncookies <= 0) {
393 			idp->eofflag = 0;
394 			return (-1);
395 		}
396 
397 		*idp->cookies++ = off;
398 		--idp->ncookies;
399 	}
400 
401 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
402 		return (error);
403 	idp->uio_off = off;
404 	return (0);
405 }
406 
407 static int
408 iso_shipdir(idp)
409 	struct isoreaddir *idp;
410 {
411 	struct dirent *dp;
412 	int cl, sl, assoc;
413 	int error;
414 	char *cname, *sname;
415 
416 	cl = idp->current.d_namlen;
417 	cname = idp->current.d_name;
418 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
419 	if (assoc) {
420 		cl--;
421 		cname++;
422 	}
423 
424 	dp = &idp->saveent;
425 	sname = dp->d_name;
426 	if (!(sl = dp->d_namlen)) {
427 		dp = &idp->assocent;
428 		sname = dp->d_name + 1;
429 		sl = dp->d_namlen - 1;
430 	}
431 	if (sl > 0) {
432 		if (sl != cl
433 		    || bcmp(sname,cname,sl)) {
434 			if (idp->assocent.d_namlen) {
435 				if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
436 					return (error);
437 				idp->assocent.d_namlen = 0;
438 			}
439 			if (idp->saveent.d_namlen) {
440 				if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
441 					return (error);
442 				idp->saveent.d_namlen = 0;
443 			}
444 		}
445 	}
446 	idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
447 	if (assoc) {
448 		idp->assocoff = idp->curroff;
449 		bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
450 	} else {
451 		idp->saveoff = idp->curroff;
452 		bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
453 	}
454 	return (0);
455 }
456 
457 /*
458  * Vnode op for readdir
459  */
460 static int
461 cd9660_readdir(ap)
462 	struct vop_readdir_args /* {
463 		struct vnode *a_vp;
464 		struct uio *a_uio;
465 		struct ucred *a_cred;
466 		int *a_eofflag;
467 		int *a_ncookies;
468 		u_long **a_cookies;
469 	} */ *ap;
470 {
471 	struct uio *uio = ap->a_uio;
472 	struct isoreaddir *idp;
473 	struct vnode *vdp = ap->a_vp;
474 	struct iso_node *dp;
475 	struct iso_mnt *imp;
476 	struct buf *bp = NULL;
477 	struct iso_directory_record *ep;
478 	int entryoffsetinblock;
479 	doff_t endsearch;
480 	u_long bmask;
481 	int error = 0;
482 	int reclen;
483 	u_short namelen;
484 	int ncookies = 0;
485 	u_long *cookies = NULL;
486 	cd_ino_t ino;
487 
488 	dp = VTOI(vdp);
489 	imp = dp->i_mnt;
490 	bmask = imp->im_bmask;
491 
492 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
493 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
494 	/*
495 	 * XXX
496 	 * Is it worth trying to figure out the type?
497 	 */
498 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
499 	    DT_UNKNOWN;
500 	idp->uio = uio;
501 	if (ap->a_ncookies == NULL) {
502 		idp->cookies = NULL;
503 	} else {
504 		/*
505 		 * Guess the number of cookies needed.
506 		 */
507 		ncookies = uio->uio_resid / 16;
508 		cookies = malloc(ncookies * sizeof(u_long),
509 		    M_TEMP, M_WAITOK);
510 		idp->cookies = cookies;
511 		idp->ncookies = ncookies;
512 	}
513 	idp->eofflag = 1;
514 	idp->curroff = uio->uio_offset;
515 	idp->uio_off = uio->uio_offset;
516 
517 	if ((entryoffsetinblock = idp->curroff & bmask) &&
518 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
519 		free(idp, M_TEMP);
520 		return (error);
521 	}
522 	endsearch = dp->i_size;
523 
524 	while (idp->curroff < endsearch) {
525 		/*
526 		 * If offset is on a block boundary,
527 		 * read the next directory block.
528 		 * Release previous if it exists.
529 		 */
530 		if ((idp->curroff & bmask) == 0) {
531 			if (bp != NULL)
532 				brelse(bp);
533 			if ((error =
534 			    cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
535 				break;
536 			entryoffsetinblock = 0;
537 		}
538 		/*
539 		 * Get pointer to next entry.
540 		 */
541 		ep = (struct iso_directory_record *)
542 			((char *)bp->b_data + entryoffsetinblock);
543 
544 		reclen = isonum_711(ep->length);
545 		if (reclen == 0) {
546 			/* skip to next block, if any */
547 			idp->curroff =
548 			    (idp->curroff & ~bmask) + imp->logical_block_size;
549 			continue;
550 		}
551 
552 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
553 			error = EINVAL;
554 			/* illegal entry, stop */
555 			break;
556 		}
557 
558 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
559 			error = EINVAL;
560 			/* illegal directory, so stop looking */
561 			break;
562 		}
563 
564 		idp->current.d_namlen = isonum_711(ep->name_len);
565 
566 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
567 			error = EINVAL;
568 			/* illegal entry, stop */
569 			break;
570 		}
571 
572 		if (isonum_711(ep->flags)&2)
573 			idp->current.d_fileno = isodirino(ep, imp);
574 		else
575 			idp->current.d_fileno = dbtob(bp->b_blkno) +
576 				entryoffsetinblock;
577 
578 		idp->curroff += reclen;
579 
580 		switch (imp->iso_ftype) {
581 		case ISO_FTYPE_RRIP:
582 			ino = idp->current.d_fileno;
583 			cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
584 			    &ino, imp);
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);
589 			break;
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);
598 			} else {
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,
603 					   imp->joliet_level,
604 					   imp->im_flags,
605 					   imp->im_d2l);
606 				idp->current.d_namlen = (u_char)namelen;
607 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
608 					error = iso_shipdir(idp);
609 				else
610 					error = iso_uiodir(idp,&idp->current,idp->curroff);
611 			}
612 		}
613 		if (error)
614 			break;
615 
616 		entryoffsetinblock += reclen;
617 	}
618 
619 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
620 		idp->current.d_namlen = 0;
621 		error = iso_shipdir(idp);
622 	}
623 	if (error < 0)
624 		error = 0;
625 
626 	if (ap->a_ncookies != NULL) {
627 		if (error)
628 			free(cookies, M_TEMP);
629 		else {
630 			/*
631 			 * Work out the number of cookies actually used.
632 			 */
633 			*ap->a_ncookies = ncookies - idp->ncookies;
634 			*ap->a_cookies = cookies;
635 		}
636 	}
637 
638 	if (bp)
639 		brelse (bp);
640 
641 	uio->uio_offset = idp->uio_off;
642 	*ap->a_eofflag = idp->eofflag;
643 
644 	free(idp, M_TEMP);
645 
646 	return (error);
647 }
648 
649 /*
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.
654  */
655 typedef struct iso_directory_record ISODIR;
656 typedef struct iso_node		    ISONODE;
657 typedef struct iso_mnt		    ISOMNT;
658 static int
659 cd9660_readlink(ap)
660 	struct vop_readlink_args /* {
661 		struct vnode *a_vp;
662 		struct uio *a_uio;
663 		struct ucred *a_cred;
664 	} */ *ap;
665 {
666 	ISONODE	*ip;
667 	ISODIR	*dirp;
668 	ISOMNT	*imp;
669 	struct	buf *bp;
670 	struct	uio *uio;
671 	u_short	symlen;
672 	int	error;
673 	char	*symname;
674 
675 	ip  = VTOI(ap->a_vp);
676 	imp = ip->i_mnt;
677 	uio = ap->a_uio;
678 
679 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
680 		return (EINVAL);
681 
682 	/*
683 	 * Get parents directory record block that this inode included.
684 	 */
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);
689 	if (error) {
690 		brelse(bp);
691 		return (EINVAL);
692 	}
693 
694 	/*
695 	 * Setup the directory pointer for this inode
696 	 */
697 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
698 
699 	/*
700 	 * Just make sure, we have a right one....
701 	 *   1: Check not cross boundary on block
702 	 */
703 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
704 	    > (unsigned)imp->logical_block_size) {
705 		brelse(bp);
706 		return (EINVAL);
707 	}
708 
709 	/*
710 	 * Now get a buffer
711 	 * Abuse a namei buffer for now.
712 	 */
713 	if (uio->uio_segflg == UIO_SYSSPACE)
714 		symname = uio->uio_iov->iov_base;
715 	else
716 		symname = uma_zalloc(namei_zone, M_WAITOK);
717 
718 	/*
719 	 * Ok, we just gathering a symbolic name in SL record.
720 	 */
721 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
722 		if (uio->uio_segflg != UIO_SYSSPACE)
723 			uma_zfree(namei_zone, symname);
724 		brelse(bp);
725 		return (EINVAL);
726 	}
727 	/*
728 	 * Don't forget before you leave from home ;-)
729 	 */
730 	brelse(bp);
731 
732 	/*
733 	 * return with the symbolic name to caller's.
734 	 */
735 	if (uio->uio_segflg != UIO_SYSSPACE) {
736 		error = uiomove(symname, symlen, uio);
737 		uma_zfree(namei_zone, symname);
738 		return (error);
739 	}
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;
743 	return (0);
744 }
745 
746 /*
747  * Calculate the logical to physical mapping if not done already,
748  * then call the device strategy routine.
749  */
750 static int
751 cd9660_strategy(ap)
752 	struct vop_strategy_args /* {
753 		struct buf *a_vp;
754 		struct buf *a_bp;
755 	} */ *ap;
756 {
757 	struct buf *bp = ap->a_bp;
758 	struct vnode *vp = ap->a_vp;
759 	struct iso_node *ip;
760 	struct bufobj *bo;
761 
762 	ip = VTOI(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);
768 	}
769 	bp->b_iooffset = dbtob(bp->b_blkno);
770 	bo = ip->i_mnt->im_bo;
771 	BO_STRATEGY(bo, bp);
772 	return (0);
773 }
774 
775 /*
776  * Return POSIX pathconf information applicable to cd9660 filesystems.
777  */
778 static int
779 cd9660_pathconf(ap)
780 	struct vop_pathconf_args /* {
781 		struct vnode *a_vp;
782 		int a_name;
783 		register_t *a_retval;
784 	} */ *ap;
785 {
786 
787 	switch (ap->a_name) {
788 	case _PC_FILESIZEBITS:
789 		*ap->a_retval = 32;
790 		return (0);
791 	case _PC_LINK_MAX:
792 		*ap->a_retval = 1;
793 		return (0);
794 	case _PC_NAME_MAX:
795 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
796 			*ap->a_retval = NAME_MAX;
797 		else
798 			*ap->a_retval = 37;
799 		return (0);
800 	case _PC_SYMLINK_MAX:
801 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
802 			*ap->a_retval = MAXPATHLEN;
803 			return (0);
804 		}
805 		return (EINVAL);
806 	case _PC_NO_TRUNC:
807 		*ap->a_retval = 1;
808 		return (0);
809 	default:
810 		return (vop_stdpathconf(ap));
811 	}
812 	/* NOTREACHED */
813 }
814 
815 /*
816  * Vnode pointer to File handle
817  */
818 static int
819 cd9660_vptofh(ap)
820 	struct vop_vptofh_args /* {
821 		struct vnode *a_vp;
822 		struct fid *a_fhp;
823 	} */ *ap;
824 {
825 	struct ifid ifh;
826 	struct iso_node *ip = VTOI(ap->a_vp);
827 
828 	ifh.ifid_len = sizeof(struct ifid);
829 
830 	ifh.ifid_ino = ip->i_number;
831 	ifh.ifid_start = ip->iso_start;
832 	/*
833 	 * This intentionally uses sizeof(ifh) in order to not copy stack
834 	 * garbage on ILP32.
835 	 */
836 	memcpy(ap->a_fhp, &ifh, sizeof(ifh));
837 
838 #ifdef	ISOFS_DBG
839 	printf("vptofh: ino %jd, start %ld\n",
840 	    (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
841 #endif
842 
843 	return (0);
844 }
845 
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,
849     &use_buf_pager, 0,
850     "Use buffer pager instead of bmap");
851 
852 static daddr_t
853 cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
854 {
855 
856 	return (lblkno(VTOI(vp)->i_mnt, off));
857 }
858 
859 static int
860 cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn)
861 {
862 	struct iso_node *ip;
863 
864 	ip = VTOI(vp);
865 	return (blksize(ip->i_mnt, ip, lbn));
866 }
867 
868 static int
869 cd9660_getpages(struct vop_getpages_args *ap)
870 {
871 	struct vnode *vp;
872 
873 	vp = ap->a_vp;
874 	if (vp->v_type == VCHR || vp->v_type == VBLK)
875 		return (EOPNOTSUPP);
876 
877 	if (use_buf_pager)
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));
883 }
884 
885 /*
886  * Global vfs data structures for cd9660
887  */
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,
907 };
908 
909 /*
910  * Special device vnode ops
911  */
912 
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,
921 };
922