xref: /freebsd/sys/fs/cd9660/cd9660_vnops.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
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 	u_long *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 		u_long **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 	u_long *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(u_long),
508 		    M_TEMP, M_WAITOK);
509 		idp->cookies = cookies;
510 		idp->ncookies = ncookies;
511 	}
512 	idp->eofflag = 1;
513 	idp->curroff = uio->uio_offset;
514 	idp->uio_off = uio->uio_offset;
515 
516 	if ((entryoffsetinblock = idp->curroff & bmask) &&
517 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
518 		free(idp, M_TEMP);
519 		return (error);
520 	}
521 	endsearch = dp->i_size;
522 
523 	while (idp->curroff < endsearch) {
524 		/*
525 		 * If offset is on a block boundary,
526 		 * read the next directory block.
527 		 * Release previous if it exists.
528 		 */
529 		if ((idp->curroff & bmask) == 0) {
530 			if (bp != NULL)
531 				brelse(bp);
532 			if ((error =
533 			    cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
534 				break;
535 			entryoffsetinblock = 0;
536 		}
537 		/*
538 		 * Get pointer to next entry.
539 		 */
540 		ep = (struct iso_directory_record *)
541 			((char *)bp->b_data + entryoffsetinblock);
542 
543 		reclen = isonum_711(ep->length);
544 		if (reclen == 0) {
545 			/* skip to next block, if any */
546 			idp->curroff =
547 			    (idp->curroff & ~bmask) + imp->logical_block_size;
548 			continue;
549 		}
550 
551 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
552 			error = EINVAL;
553 			/* illegal entry, stop */
554 			break;
555 		}
556 
557 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
558 			error = EINVAL;
559 			/* illegal directory, so stop looking */
560 			break;
561 		}
562 
563 		idp->current.d_namlen = isonum_711(ep->name_len);
564 
565 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
566 			error = EINVAL;
567 			/* illegal entry, stop */
568 			break;
569 		}
570 
571 		if (isonum_711(ep->flags)&2)
572 			idp->current.d_fileno = isodirino(ep, imp);
573 		else
574 			idp->current.d_fileno = dbtob(bp->b_blkno) +
575 				entryoffsetinblock;
576 
577 		idp->curroff += reclen;
578 		/* NOTE: d_off is the offset of *next* entry. */
579 		idp->current.d_off = idp->curroff;
580 
581 		switch (imp->iso_ftype) {
582 		case ISO_FTYPE_RRIP:
583 			ino = idp->current.d_fileno;
584 			cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
585 			    &ino, imp);
586 			idp->current.d_fileno = ino;
587 			idp->current.d_namlen = (u_char)namelen;
588 			if (idp->current.d_namlen)
589 				error = iso_uiodir(idp,&idp->current,idp->curroff);
590 			break;
591 		default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
592 			strcpy(idp->current.d_name,"..");
593 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
594 				idp->current.d_namlen = 1;
595 				error = iso_uiodir(idp,&idp->current,idp->curroff);
596 			} else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
597 				idp->current.d_namlen = 2;
598 				error = iso_uiodir(idp,&idp->current,idp->curroff);
599 			} else {
600 				isofntrans(ep->name,idp->current.d_namlen,
601 					   idp->current.d_name, &namelen,
602 					   imp->iso_ftype == ISO_FTYPE_9660,
603 					   isonum_711(ep->flags)&4,
604 					   imp->joliet_level,
605 					   imp->im_flags,
606 					   imp->im_d2l);
607 				idp->current.d_namlen = (u_char)namelen;
608 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
609 					error = iso_shipdir(idp);
610 				else
611 					error = iso_uiodir(idp,&idp->current,idp->curroff);
612 			}
613 		}
614 		if (error)
615 			break;
616 
617 		entryoffsetinblock += reclen;
618 	}
619 
620 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
621 		idp->current.d_namlen = 0;
622 		error = iso_shipdir(idp);
623 	}
624 	if (error < 0)
625 		error = 0;
626 
627 	if (ap->a_ncookies != NULL) {
628 		if (error)
629 			free(cookies, M_TEMP);
630 		else {
631 			/*
632 			 * Work out the number of cookies actually used.
633 			 */
634 			*ap->a_ncookies = ncookies - idp->ncookies;
635 			*ap->a_cookies = cookies;
636 		}
637 	}
638 
639 	if (bp)
640 		brelse (bp);
641 
642 	uio->uio_offset = idp->uio_off;
643 	*ap->a_eofflag = idp->eofflag;
644 
645 	free(idp, M_TEMP);
646 
647 	return (error);
648 }
649 
650 /*
651  * Return target name of a symbolic link
652  * Shouldn't we get the parent vnode and read the data from there?
653  * This could eventually result in deadlocks in cd9660_lookup.
654  * But otherwise the block read here is in the block buffer two times.
655  */
656 typedef struct iso_directory_record ISODIR;
657 typedef struct iso_node		    ISONODE;
658 typedef struct iso_mnt		    ISOMNT;
659 static int
660 cd9660_readlink(ap)
661 	struct vop_readlink_args /* {
662 		struct vnode *a_vp;
663 		struct uio *a_uio;
664 		struct ucred *a_cred;
665 	} */ *ap;
666 {
667 	ISONODE	*ip;
668 	ISODIR	*dirp;
669 	ISOMNT	*imp;
670 	struct	buf *bp;
671 	struct	uio *uio;
672 	u_short	symlen;
673 	int	error;
674 	char	*symname;
675 
676 	ip  = VTOI(ap->a_vp);
677 	imp = ip->i_mnt;
678 	uio = ap->a_uio;
679 
680 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
681 		return (EINVAL);
682 
683 	/*
684 	 * Get parents directory record block that this inode included.
685 	 */
686 	error = bread(imp->im_devvp,
687 		      (ip->i_number >> imp->im_bshift) <<
688 		      (imp->im_bshift - DEV_BSHIFT),
689 		      imp->logical_block_size, NOCRED, &bp);
690 	if (error) {
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 | CTLFLAG_MPSAFE, 0,
847     "cd9660 filesystem");
848 static int use_buf_pager = 1;
849 SYSCTL_INT(_vfs_cd9660, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
850     &use_buf_pager, 0,
851     "Use buffer pager instead of bmap");
852 
853 static daddr_t
854 cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
855 {
856 
857 	return (lblkno(VTOI(vp)->i_mnt, off));
858 }
859 
860 static int
861 cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn)
862 {
863 	struct iso_node *ip;
864 
865 	ip = VTOI(vp);
866 	return (blksize(ip->i_mnt, ip, lbn));
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