xref: /freebsd/sys/fs/cd9660/cd9660_vnops.c (revision 924226fba12cc9a228c73b956e1b7fa24c60b055)
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));
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 static int
185 cd9660_getattr(ap)
186 	struct vop_getattr_args /* {
187 		struct vnode *a_vp;
188 		struct vattr *a_vap;
189 		struct ucred *a_cred;
190 	} */ *ap;
191 
192 {
193 	struct vnode *vp = ap->a_vp;
194 	struct vattr *vap = ap->a_vap;
195 	struct iso_node *ip = VTOI(vp);
196 
197 	vap->va_fsid    = dev2udev(ip->i_mnt->im_dev);
198 	vap->va_fileid	= ip->i_number;
199 
200 	vap->va_mode	= ip->inode.iso_mode;
201 	vap->va_nlink	= ip->inode.iso_links;
202 	vap->va_uid	= ip->inode.iso_uid;
203 	vap->va_gid	= ip->inode.iso_gid;
204 	vap->va_atime	= ip->inode.iso_atime;
205 	vap->va_mtime	= ip->inode.iso_mtime;
206 	vap->va_ctime	= ip->inode.iso_ctime;
207 	vap->va_rdev	= ip->inode.iso_rdev;
208 
209 	vap->va_size	= (u_quad_t) ip->i_size;
210 	if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
211 		struct vop_readlink_args rdlnk;
212 		struct iovec aiov;
213 		struct uio auio;
214 		char *cp;
215 
216 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
217 		aiov.iov_base = cp;
218 		aiov.iov_len = MAXPATHLEN;
219 		auio.uio_iov = &aiov;
220 		auio.uio_iovcnt = 1;
221 		auio.uio_offset = 0;
222 		auio.uio_rw = UIO_READ;
223 		auio.uio_segflg = UIO_SYSSPACE;
224 		auio.uio_td = curthread;
225 		auio.uio_resid = MAXPATHLEN;
226 		rdlnk.a_uio = &auio;
227 		rdlnk.a_vp = ap->a_vp;
228 		rdlnk.a_cred = ap->a_cred;
229 		if (cd9660_readlink(&rdlnk) == 0)
230 			vap->va_size = MAXPATHLEN - auio.uio_resid;
231 		free(cp, M_TEMP);
232 	}
233 	vap->va_flags	= 0;
234 	vap->va_gen = 1;
235 	vap->va_blocksize = ip->i_mnt->logical_block_size;
236 	vap->va_bytes	= (u_quad_t) ip->i_size;
237 	vap->va_type	= vp->v_type;
238 	vap->va_filerev	= 0;
239 	return (0);
240 }
241 
242 /*
243  * Vnode op for ioctl.
244  */
245 static int
246 cd9660_ioctl(ap)
247 	struct vop_ioctl_args /* {
248 		struct vnode *a_vp;
249 		u_long  a_command;
250 		caddr_t  a_data;
251 		int  a_fflag;
252 		struct ucred *a_cred;
253 		struct thread *a_td;
254 	} */ *ap;
255 {
256 	struct vnode *vp;
257 	struct iso_node *ip;
258 	int error;
259 
260 	vp = ap->a_vp;
261 	vn_lock(vp, LK_SHARED | LK_RETRY);
262 	if (VN_IS_DOOMED(vp)) {
263 		VOP_UNLOCK(vp);
264 		return (EBADF);
265 	}
266 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
267 		VOP_UNLOCK(vp);
268 		return (EOPNOTSUPP);
269 	}
270 
271 	ip = VTOI(vp);
272 	error = 0;
273 
274 	switch (ap->a_command) {
275 	case FIOGETLBA:
276 		*(int *)(ap->a_data) = ip->iso_start;
277 		break;
278 	default:
279 		error = ENOTTY;
280 		break;
281 	}
282 
283 	VOP_UNLOCK(vp);
284 	return (error);
285 }
286 
287 /*
288  * Vnode op for reading.
289  */
290 static int
291 cd9660_read(ap)
292 	struct vop_read_args /* {
293 		struct vnode *a_vp;
294 		struct uio *a_uio;
295 		int a_ioflag;
296 		struct ucred *a_cred;
297 	} */ *ap;
298 {
299 	struct vnode *vp = ap->a_vp;
300 	struct uio *uio = ap->a_uio;
301 	struct iso_node *ip = VTOI(vp);
302 	struct iso_mnt *imp;
303 	struct buf *bp;
304 	daddr_t lbn, rablock;
305 	off_t diff;
306 	int rasize, error = 0;
307 	int seqcount;
308 	long size, n, on;
309 
310 	if (vp->v_type == VCHR || vp->v_type == VBLK)
311 		return (EOPNOTSUPP);
312 
313 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
314 
315 	if (uio->uio_resid == 0)
316 		return (0);
317 	if (uio->uio_offset < 0)
318 		return (EINVAL);
319 	imp = ip->i_mnt;
320 	do {
321 		lbn = lblkno(imp, uio->uio_offset);
322 		on = blkoff(imp, uio->uio_offset);
323 		n = MIN(imp->logical_block_size - on, uio->uio_resid);
324 		diff = (off_t)ip->i_size - uio->uio_offset;
325 		if (diff <= 0)
326 			return (0);
327 		if (diff < n)
328 			n = diff;
329 		size = blksize(imp, ip, lbn);
330 		rablock = lbn + 1;
331 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
332 			if (lblktosize(imp, rablock) < ip->i_size)
333 				error = cluster_read(vp, (off_t)ip->i_size,
334 					 lbn, size, NOCRED, uio->uio_resid,
335 					 (ap->a_ioflag >> 16), 0, &bp);
336 			else
337 				error = bread(vp, lbn, size, NOCRED, &bp);
338 		} else {
339 			if (seqcount > 1 &&
340 			    lblktosize(imp, rablock) < ip->i_size) {
341 				rasize = blksize(imp, ip, rablock);
342 				error = breadn(vp, lbn, size, &rablock,
343 					       &rasize, 1, NOCRED, &bp);
344 			} else
345 				error = bread(vp, lbn, size, NOCRED, &bp);
346 		}
347 		if (error != 0)
348 			return (error);
349 		n = MIN(n, size - bp->b_resid);
350 
351 		error = uiomove(bp->b_data + on, (int)n, uio);
352 		brelse(bp);
353 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
354 	return (error);
355 }
356 
357 /*
358  * Structure for reading directories
359  */
360 struct isoreaddir {
361 	struct dirent saveent;
362 	struct dirent assocent;
363 	struct dirent current;
364 	off_t saveoff;
365 	off_t assocoff;
366 	off_t curroff;
367 	struct uio *uio;
368 	off_t uio_off;
369 	int eofflag;
370 	uint64_t *cookies;
371 	int ncookies;
372 };
373 
374 static int
375 iso_uiodir(idp,dp,off)
376 	struct isoreaddir *idp;
377 	struct dirent *dp;
378 	off_t off;
379 {
380 	int error;
381 
382 	dp->d_reclen = GENERIC_DIRSIZ(dp);
383 	dirent_terminate(dp);
384 
385 	if (idp->uio->uio_resid < dp->d_reclen) {
386 		idp->eofflag = 0;
387 		return (-1);
388 	}
389 
390 	if (idp->cookies) {
391 		if (idp->ncookies <= 0) {
392 			idp->eofflag = 0;
393 			return (-1);
394 		}
395 
396 		*idp->cookies++ = off;
397 		--idp->ncookies;
398 	}
399 
400 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
401 		return (error);
402 	idp->uio_off = off;
403 	return (0);
404 }
405 
406 static int
407 iso_shipdir(idp)
408 	struct isoreaddir *idp;
409 {
410 	struct dirent *dp;
411 	int cl, sl, assoc;
412 	int error;
413 	char *cname, *sname;
414 
415 	cl = idp->current.d_namlen;
416 	cname = idp->current.d_name;
417 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
418 	if (assoc) {
419 		cl--;
420 		cname++;
421 	}
422 
423 	dp = &idp->saveent;
424 	sname = dp->d_name;
425 	if (!(sl = dp->d_namlen)) {
426 		dp = &idp->assocent;
427 		sname = dp->d_name + 1;
428 		sl = dp->d_namlen - 1;
429 	}
430 	if (sl > 0) {
431 		if (sl != cl
432 		    || bcmp(sname,cname,sl)) {
433 			if (idp->assocent.d_namlen) {
434 				if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
435 					return (error);
436 				idp->assocent.d_namlen = 0;
437 			}
438 			if (idp->saveent.d_namlen) {
439 				if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
440 					return (error);
441 				idp->saveent.d_namlen = 0;
442 			}
443 		}
444 	}
445 	idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
446 	if (assoc) {
447 		idp->assocoff = idp->curroff;
448 		memcpy(&idp->assocent, &idp->current, idp->current.d_reclen);
449 	} else {
450 		idp->saveoff = idp->curroff;
451 		memcpy(&idp->saveent, &idp->current, idp->current.d_reclen);
452 	}
453 	return (0);
454 }
455 
456 /*
457  * Vnode op for readdir
458  */
459 static int
460 cd9660_readdir(ap)
461 	struct vop_readdir_args /* {
462 		struct vnode *a_vp;
463 		struct uio *a_uio;
464 		struct ucred *a_cred;
465 		int *a_eofflag;
466 		int *a_ncookies;
467 		uint64_t **a_cookies;
468 	} */ *ap;
469 {
470 	struct uio *uio = ap->a_uio;
471 	struct isoreaddir *idp;
472 	struct vnode *vdp = ap->a_vp;
473 	struct iso_node *dp;
474 	struct iso_mnt *imp;
475 	struct buf *bp = NULL;
476 	struct iso_directory_record *ep;
477 	int entryoffsetinblock;
478 	doff_t endsearch;
479 	u_long bmask;
480 	int error = 0;
481 	int reclen;
482 	u_short namelen;
483 	u_int ncookies = 0;
484 	uint64_t *cookies = NULL;
485 	cd_ino_t ino;
486 
487 	dp = VTOI(vdp);
488 	imp = dp->i_mnt;
489 	bmask = imp->im_bmask;
490 
491 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
492 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
493 	/*
494 	 * XXX
495 	 * Is it worth trying to figure out the type?
496 	 */
497 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
498 	    DT_UNKNOWN;
499 	idp->uio = uio;
500 	if (ap->a_ncookies == NULL) {
501 		idp->cookies = NULL;
502 	} else {
503 		/*
504 		 * Guess the number of cookies needed.
505 		 */
506 		ncookies = uio->uio_resid / 16;
507 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
508 		idp->cookies = cookies;
509 		idp->ncookies = ncookies;
510 	}
511 	idp->eofflag = 1;
512 	idp->curroff = uio->uio_offset;
513 	idp->uio_off = uio->uio_offset;
514 
515 	if ((entryoffsetinblock = idp->curroff & bmask) &&
516 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
517 		free(idp, M_TEMP);
518 		return (error);
519 	}
520 	endsearch = dp->i_size;
521 
522 	while (idp->curroff < endsearch) {
523 		/*
524 		 * If offset is on a block boundary,
525 		 * read the next directory block.
526 		 * Release previous if it exists.
527 		 */
528 		if ((idp->curroff & bmask) == 0) {
529 			if (bp != NULL)
530 				brelse(bp);
531 			if ((error =
532 			    cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
533 				break;
534 			entryoffsetinblock = 0;
535 		}
536 		/*
537 		 * Get pointer to next entry.
538 		 */
539 		ep = (struct iso_directory_record *)
540 			((char *)bp->b_data + entryoffsetinblock);
541 
542 		reclen = isonum_711(ep->length);
543 		if (reclen == 0) {
544 			/* skip to next block, if any */
545 			idp->curroff =
546 			    (idp->curroff & ~bmask) + imp->logical_block_size;
547 			continue;
548 		}
549 
550 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
551 			error = EINVAL;
552 			/* illegal entry, stop */
553 			break;
554 		}
555 
556 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
557 			error = EINVAL;
558 			/* illegal directory, so stop looking */
559 			break;
560 		}
561 
562 		idp->current.d_namlen = isonum_711(ep->name_len);
563 
564 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
565 			error = EINVAL;
566 			/* illegal entry, stop */
567 			break;
568 		}
569 
570 		if (isonum_711(ep->flags)&2)
571 			idp->current.d_fileno = isodirino(ep, imp);
572 		else
573 			idp->current.d_fileno = dbtob(bp->b_blkno) +
574 				entryoffsetinblock;
575 
576 		idp->curroff += reclen;
577 		/* NOTE: d_off is the offset of *next* entry. */
578 		idp->current.d_off = idp->curroff;
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 		return (EINVAL);
691 	}
692 
693 	/*
694 	 * Setup the directory pointer for this inode
695 	 */
696 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
697 
698 	/*
699 	 * Just make sure, we have a right one....
700 	 *   1: Check not cross boundary on block
701 	 */
702 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
703 	    > (unsigned)imp->logical_block_size) {
704 		brelse(bp);
705 		return (EINVAL);
706 	}
707 
708 	/*
709 	 * Now get a buffer
710 	 * Abuse a namei buffer for now.
711 	 */
712 	if (uio->uio_segflg == UIO_SYSSPACE)
713 		symname = uio->uio_iov->iov_base;
714 	else
715 		symname = uma_zalloc(namei_zone, M_WAITOK);
716 
717 	/*
718 	 * Ok, we just gathering a symbolic name in SL record.
719 	 */
720 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
721 		if (uio->uio_segflg != UIO_SYSSPACE)
722 			uma_zfree(namei_zone, symname);
723 		brelse(bp);
724 		return (EINVAL);
725 	}
726 	/*
727 	 * Don't forget before you leave from home ;-)
728 	 */
729 	brelse(bp);
730 
731 	/*
732 	 * return with the symbolic name to caller's.
733 	 */
734 	if (uio->uio_segflg != UIO_SYSSPACE) {
735 		error = uiomove(symname, symlen, uio);
736 		uma_zfree(namei_zone, symname);
737 		return (error);
738 	}
739 	uio->uio_resid -= symlen;
740 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
741 	uio->uio_iov->iov_len -= symlen;
742 	return (0);
743 }
744 
745 /*
746  * Calculate the logical to physical mapping if not done already,
747  * then call the device strategy routine.
748  */
749 static int
750 cd9660_strategy(ap)
751 	struct vop_strategy_args /* {
752 		struct buf *a_vp;
753 		struct buf *a_bp;
754 	} */ *ap;
755 {
756 	struct buf *bp = ap->a_bp;
757 	struct vnode *vp = ap->a_vp;
758 	struct iso_node *ip;
759 	struct bufobj *bo;
760 
761 	ip = VTOI(vp);
762 	if (vp->v_type == VBLK || vp->v_type == VCHR)
763 		panic("cd9660_strategy: spec");
764 	if (bp->b_blkno == bp->b_lblkno) {
765 		bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
766 		    (ip->i_mnt->im_bshift - DEV_BSHIFT);
767 	}
768 	bp->b_iooffset = dbtob(bp->b_blkno);
769 	bo = ip->i_mnt->im_bo;
770 	BO_STRATEGY(bo, bp);
771 	return (0);
772 }
773 
774 /*
775  * Return POSIX pathconf information applicable to cd9660 filesystems.
776  */
777 static int
778 cd9660_pathconf(ap)
779 	struct vop_pathconf_args /* {
780 		struct vnode *a_vp;
781 		int a_name;
782 		register_t *a_retval;
783 	} */ *ap;
784 {
785 
786 	switch (ap->a_name) {
787 	case _PC_FILESIZEBITS:
788 		*ap->a_retval = 32;
789 		return (0);
790 	case _PC_LINK_MAX:
791 		*ap->a_retval = 1;
792 		return (0);
793 	case _PC_NAME_MAX:
794 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
795 			*ap->a_retval = NAME_MAX;
796 		else
797 			*ap->a_retval = 37;
798 		return (0);
799 	case _PC_SYMLINK_MAX:
800 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
801 			*ap->a_retval = MAXPATHLEN;
802 			return (0);
803 		}
804 		return (EINVAL);
805 	case _PC_NO_TRUNC:
806 		*ap->a_retval = 1;
807 		return (0);
808 	default:
809 		return (vop_stdpathconf(ap));
810 	}
811 	/* NOTREACHED */
812 }
813 
814 /*
815  * Vnode pointer to File handle
816  */
817 static int
818 cd9660_vptofh(ap)
819 	struct vop_vptofh_args /* {
820 		struct vnode *a_vp;
821 		struct fid *a_fhp;
822 	} */ *ap;
823 {
824 	struct ifid ifh;
825 	struct iso_node *ip = VTOI(ap->a_vp);
826 
827 	ifh.ifid_len = sizeof(struct ifid);
828 
829 	ifh.ifid_ino = ip->i_number;
830 	ifh.ifid_start = ip->iso_start;
831 	/*
832 	 * This intentionally uses sizeof(ifh) in order to not copy stack
833 	 * garbage on ILP32.
834 	 */
835 	memcpy(ap->a_fhp, &ifh, sizeof(ifh));
836 
837 #ifdef	ISOFS_DBG
838 	printf("vptofh: ino %jd, start %ld\n",
839 	    (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
840 #endif
841 
842 	return (0);
843 }
844 
845 SYSCTL_NODE(_vfs, OID_AUTO, cd9660, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
846     "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, long *sz)
861 {
862 	struct iso_node *ip;
863 
864 	ip = VTOI(vp);
865 	*sz = blksize(ip->i_mnt, ip, lbn);
866 	return (0);
867 }
868 
869 static int
870 cd9660_getpages(struct vop_getpages_args *ap)
871 {
872 	struct vnode *vp;
873 
874 	vp = ap->a_vp;
875 	if (vp->v_type == VCHR || vp->v_type == VBLK)
876 		return (EOPNOTSUPP);
877 
878 	if (use_buf_pager)
879 		return (vfs_bio_getpages(vp, ap->a_m, ap->a_count,
880 		    ap->a_rbehind, ap->a_rahead, cd9660_gbp_getblkno,
881 		    cd9660_gbp_getblksz));
882 	return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
883 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
884 }
885 
886 /*
887  * Global vfs data structures for cd9660
888  */
889 struct vop_vector cd9660_vnodeops = {
890 	.vop_default =		&default_vnodeops,
891 	.vop_open =		cd9660_open,
892 	.vop_access =		cd9660_access,
893 	.vop_bmap =		cd9660_bmap,
894 	.vop_cachedlookup =	cd9660_lookup,
895 	.vop_getattr =		cd9660_getattr,
896 	.vop_inactive =		cd9660_inactive,
897 	.vop_ioctl =		cd9660_ioctl,
898 	.vop_lookup =		vfs_cache_lookup,
899 	.vop_pathconf =		cd9660_pathconf,
900 	.vop_read =		cd9660_read,
901 	.vop_readdir =		cd9660_readdir,
902 	.vop_readlink =		cd9660_readlink,
903 	.vop_reclaim =		cd9660_reclaim,
904 	.vop_setattr =		cd9660_setattr,
905 	.vop_strategy =		cd9660_strategy,
906 	.vop_vptofh =		cd9660_vptofh,
907 	.vop_getpages =		cd9660_getpages,
908 };
909 VFS_VOP_VECTOR_REGISTER(cd9660_vnodeops);
910 
911 /*
912  * Special device vnode ops
913  */
914 
915 struct vop_vector cd9660_fifoops = {
916 	.vop_default =		&fifo_specops,
917 	.vop_access =		cd9660_access,
918 	.vop_getattr =		cd9660_getattr,
919 	.vop_inactive =		cd9660_inactive,
920 	.vop_reclaim =		cd9660_reclaim,
921 	.vop_setattr =		cd9660_setattr,
922 	.vop_vptofh =		cd9660_vptofh,
923 };
924 VFS_VOP_VECTOR_REGISTER(cd9660_fifoops);
925