xref: /freebsd/sys/fs/cd9660/cd9660_vnops.c (revision bdcbfde31e8e9b343f113a1956384bdf30d1ed62)
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 
37 #include <sys/cdefs.h>
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/namei.h>
41 #include <sys/kernel.h>
42 #include <sys/conf.h>
43 #include <sys/stat.h>
44 #include <sys/bio.h>
45 #include <sys/buf.h>
46 #include <sys/mount.h>
47 #include <sys/vnode.h>
48 #include <sys/malloc.h>
49 #include <sys/dirent.h>
50 #include <sys/unistd.h>
51 #include <sys/filio.h>
52 #include <sys/sysctl.h>
53 
54 #include <vm/vm.h>
55 #include <vm/vnode_pager.h>
56 #include <vm/uma.h>
57 
58 #include <fs/cd9660/iso.h>
59 #include <fs/cd9660/cd9660_node.h>
60 #include <fs/cd9660/iso_rrip.h>
61 
62 static vop_setattr_t	cd9660_setattr;
63 static vop_open_t	cd9660_open;
64 static vop_access_t	cd9660_access;
65 static vop_getattr_t	cd9660_getattr;
66 static vop_ioctl_t	cd9660_ioctl;
67 static vop_pathconf_t	cd9660_pathconf;
68 static vop_read_t	cd9660_read;
69 struct isoreaddir;
70 static int iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off);
71 static int iso_shipdir(struct isoreaddir *idp);
72 static vop_readdir_t	cd9660_readdir;
73 static vop_readlink_t	cd9660_readlink;
74 static vop_strategy_t	cd9660_strategy;
75 static vop_vptofh_t	cd9660_vptofh;
76 static vop_getpages_t	cd9660_getpages;
77 
78 /*
79  * Setattr call. Only allowed for block and character special devices.
80  */
81 static int
82 cd9660_setattr(struct vop_setattr_args *ap)
83 {
84 	struct vnode *vp = ap->a_vp;
85 	struct vattr *vap = ap->a_vap;
86 
87 	if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
88 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
89 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
90 		return (EROFS);
91 	if (vap->va_size != (u_quad_t)VNOVAL) {
92 		switch (vp->v_type) {
93 		case VDIR:
94 			return (EISDIR);
95 		case VLNK:
96 		case VREG:
97 			return (EROFS);
98 		case VCHR:
99 		case VBLK:
100 		case VSOCK:
101 		case VFIFO:
102 		case VNON:
103 		case VBAD:
104 		case VMARKER:
105 			return (0);
106 		}
107 	}
108 	return (0);
109 }
110 
111 /*
112  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
113  * The mode is shifted to select the owner/group/other fields. The
114  * super user is granted all permissions.
115  */
116 /* ARGSUSED */
117 static int
118 cd9660_access(struct vop_access_args *ap)
119 {
120 	struct vnode *vp = ap->a_vp;
121 	struct iso_node *ip = VTOI(vp);
122 	accmode_t accmode = ap->a_accmode;
123 
124 	if (vp->v_type == VCHR || vp->v_type == VBLK)
125 		return (EOPNOTSUPP);
126 
127 	/*
128 	 * Disallow write attempts unless the file is a socket,
129 	 * fifo, or a block or character device resident on the
130 	 * filesystem.
131 	 */
132 	if (accmode & VWRITE) {
133 		switch (vp->v_type) {
134 		case VDIR:
135 		case VLNK:
136 		case VREG:
137 			return (EROFS);
138 			/* NOT REACHED */
139 		default:
140 			break;
141 		}
142 	}
143 
144 	return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
145 	    ip->inode.iso_gid, ap->a_accmode, ap->a_cred));
146 }
147 
148 static int
149 cd9660_open(struct vop_open_args *ap)
150 {
151 	struct vnode *vp = ap->a_vp;
152 	struct iso_node *ip = VTOI(vp);
153 
154 	if (vp->v_type == VCHR || vp->v_type == VBLK)
155 		return (EOPNOTSUPP);
156 
157 	vnode_create_vobject(vp, ip->i_size, ap->a_td);
158 	return (0);
159 }
160 
161 static int
162 cd9660_getattr(struct vop_getattr_args *ap)
163 
164 {
165 	struct vnode *vp = ap->a_vp;
166 	struct vattr *vap = ap->a_vap;
167 	struct iso_node *ip = VTOI(vp);
168 
169 	vap->va_fsid    = dev2udev(ip->i_mnt->im_dev);
170 	vap->va_fileid	= ip->i_number;
171 
172 	vap->va_mode	= ip->inode.iso_mode;
173 	vap->va_nlink	= ip->inode.iso_links;
174 	vap->va_uid	= ip->inode.iso_uid;
175 	vap->va_gid	= ip->inode.iso_gid;
176 	vap->va_atime	= ip->inode.iso_atime;
177 	vap->va_mtime	= ip->inode.iso_mtime;
178 	vap->va_ctime	= ip->inode.iso_ctime;
179 	vap->va_rdev	= ip->inode.iso_rdev;
180 
181 	vap->va_size	= (u_quad_t) ip->i_size;
182 	if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
183 		struct vop_readlink_args rdlnk;
184 		struct iovec aiov;
185 		struct uio auio;
186 		char *cp;
187 
188 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
189 		aiov.iov_base = cp;
190 		aiov.iov_len = MAXPATHLEN;
191 		auio.uio_iov = &aiov;
192 		auio.uio_iovcnt = 1;
193 		auio.uio_offset = 0;
194 		auio.uio_rw = UIO_READ;
195 		auio.uio_segflg = UIO_SYSSPACE;
196 		auio.uio_td = curthread;
197 		auio.uio_resid = MAXPATHLEN;
198 		rdlnk.a_uio = &auio;
199 		rdlnk.a_vp = ap->a_vp;
200 		rdlnk.a_cred = ap->a_cred;
201 		if (cd9660_readlink(&rdlnk) == 0)
202 			vap->va_size = MAXPATHLEN - auio.uio_resid;
203 		free(cp, M_TEMP);
204 	}
205 	vap->va_flags	= 0;
206 	vap->va_gen = 1;
207 	vap->va_blocksize = ip->i_mnt->logical_block_size;
208 	vap->va_bytes	= (u_quad_t) ip->i_size;
209 	vap->va_type	= vp->v_type;
210 	vap->va_filerev	= 0;
211 	return (0);
212 }
213 
214 /*
215  * Vnode op for ioctl.
216  */
217 static int
218 cd9660_ioctl(struct vop_ioctl_args *ap)
219 {
220 	struct vnode *vp;
221 	struct iso_node *ip;
222 	int error;
223 
224 	vp = ap->a_vp;
225 	vn_lock(vp, LK_SHARED | LK_RETRY);
226 	if (VN_IS_DOOMED(vp)) {
227 		VOP_UNLOCK(vp);
228 		return (EBADF);
229 	}
230 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
231 		VOP_UNLOCK(vp);
232 		return (EOPNOTSUPP);
233 	}
234 
235 	ip = VTOI(vp);
236 	error = 0;
237 
238 	switch (ap->a_command) {
239 	case FIOGETLBA:
240 		*(int *)(ap->a_data) = ip->iso_start;
241 		break;
242 	default:
243 		error = ENOTTY;
244 		break;
245 	}
246 
247 	VOP_UNLOCK(vp);
248 	return (error);
249 }
250 
251 /*
252  * Vnode op for reading.
253  */
254 static int
255 cd9660_read(struct vop_read_args *ap)
256 {
257 	struct vnode *vp = ap->a_vp;
258 	struct uio *uio = ap->a_uio;
259 	struct iso_node *ip = VTOI(vp);
260 	struct iso_mnt *imp;
261 	struct buf *bp;
262 	daddr_t lbn, rablock;
263 	off_t diff;
264 	int rasize, error = 0;
265 	int seqcount;
266 	long size, n, on;
267 
268 	if (vp->v_type == VCHR || vp->v_type == VBLK)
269 		return (EOPNOTSUPP);
270 
271 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
272 
273 	if (uio->uio_resid == 0)
274 		return (0);
275 	if (uio->uio_offset < 0)
276 		return (EINVAL);
277 	imp = ip->i_mnt;
278 	do {
279 		lbn = lblkno(imp, uio->uio_offset);
280 		on = blkoff(imp, uio->uio_offset);
281 		n = MIN(imp->logical_block_size - on, uio->uio_resid);
282 		diff = (off_t)ip->i_size - uio->uio_offset;
283 		if (diff <= 0)
284 			return (0);
285 		if (diff < n)
286 			n = diff;
287 		size = blksize(imp, ip, lbn);
288 		rablock = lbn + 1;
289 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
290 			if (lblktosize(imp, rablock) < ip->i_size)
291 				error = cluster_read(vp, (off_t)ip->i_size,
292 					 lbn, size, NOCRED, uio->uio_resid,
293 					 (ap->a_ioflag >> 16), 0, &bp);
294 			else
295 				error = bread(vp, lbn, size, NOCRED, &bp);
296 		} else {
297 			if (seqcount > 1 &&
298 			    lblktosize(imp, rablock) < ip->i_size) {
299 				rasize = blksize(imp, ip, rablock);
300 				error = breadn(vp, lbn, size, &rablock,
301 					       &rasize, 1, NOCRED, &bp);
302 			} else
303 				error = bread(vp, lbn, size, NOCRED, &bp);
304 		}
305 		if (error != 0)
306 			return (error);
307 		n = MIN(n, size - bp->b_resid);
308 
309 		error = uiomove(bp->b_data + on, (int)n, uio);
310 		brelse(bp);
311 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
312 	return (error);
313 }
314 
315 /*
316  * Structure for reading directories
317  */
318 struct isoreaddir {
319 	struct dirent saveent;
320 	struct dirent assocent;
321 	struct dirent current;
322 	off_t saveoff;
323 	off_t assocoff;
324 	off_t curroff;
325 	struct uio *uio;
326 	off_t uio_off;
327 	int eofflag;
328 	uint64_t *cookies;
329 	int ncookies;
330 };
331 
332 static int
333 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
334 {
335 	int error;
336 
337 	dp->d_reclen = GENERIC_DIRSIZ(dp);
338 	dirent_terminate(dp);
339 
340 	if (idp->uio->uio_resid < dp->d_reclen) {
341 		idp->eofflag = 0;
342 		return (-1);
343 	}
344 
345 	if (idp->cookies) {
346 		if (idp->ncookies <= 0) {
347 			idp->eofflag = 0;
348 			return (-1);
349 		}
350 
351 		*idp->cookies++ = off;
352 		--idp->ncookies;
353 	}
354 
355 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
356 		return (error);
357 	idp->uio_off = off;
358 	return (0);
359 }
360 
361 static int
362 iso_shipdir(struct isoreaddir *idp)
363 {
364 	struct dirent *dp;
365 	int cl, sl, assoc;
366 	int error;
367 	char *cname, *sname;
368 
369 	cl = idp->current.d_namlen;
370 	cname = idp->current.d_name;
371 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
372 	if (assoc) {
373 		cl--;
374 		cname++;
375 	}
376 
377 	dp = &idp->saveent;
378 	sname = dp->d_name;
379 	if (!(sl = dp->d_namlen)) {
380 		dp = &idp->assocent;
381 		sname = dp->d_name + 1;
382 		sl = dp->d_namlen - 1;
383 	}
384 	if (sl > 0) {
385 		if (sl != cl
386 		    || bcmp(sname,cname,sl)) {
387 			if (idp->assocent.d_namlen) {
388 				if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
389 					return (error);
390 				idp->assocent.d_namlen = 0;
391 			}
392 			if (idp->saveent.d_namlen) {
393 				if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
394 					return (error);
395 				idp->saveent.d_namlen = 0;
396 			}
397 		}
398 	}
399 	idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
400 	if (assoc) {
401 		idp->assocoff = idp->curroff;
402 		memcpy(&idp->assocent, &idp->current, idp->current.d_reclen);
403 	} else {
404 		idp->saveoff = idp->curroff;
405 		memcpy(&idp->saveent, &idp->current, idp->current.d_reclen);
406 	}
407 	return (0);
408 }
409 
410 /*
411  * Vnode op for readdir
412  */
413 static int
414 cd9660_readdir(struct vop_readdir_args *ap)
415 {
416 	struct uio *uio = ap->a_uio;
417 	struct isoreaddir *idp;
418 	struct vnode *vdp = ap->a_vp;
419 	struct iso_node *dp;
420 	struct iso_mnt *imp;
421 	struct buf *bp = NULL;
422 	struct iso_directory_record *ep;
423 	int entryoffsetinblock;
424 	doff_t endsearch;
425 	u_long bmask;
426 	int error = 0;
427 	int reclen;
428 	u_short namelen;
429 	u_int ncookies = 0;
430 	uint64_t *cookies = NULL;
431 	cd_ino_t ino;
432 
433 	dp = VTOI(vdp);
434 	imp = dp->i_mnt;
435 	bmask = imp->im_bmask;
436 
437 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
438 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
439 	/*
440 	 * XXX
441 	 * Is it worth trying to figure out the type?
442 	 */
443 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
444 	    DT_UNKNOWN;
445 	idp->uio = uio;
446 	if (ap->a_ncookies == NULL) {
447 		idp->cookies = NULL;
448 	} else {
449 		/*
450 		 * Guess the number of cookies needed.
451 		 */
452 		ncookies = uio->uio_resid / 16;
453 		cookies = malloc(ncookies * sizeof(*cookies), M_TEMP, M_WAITOK);
454 		idp->cookies = cookies;
455 		idp->ncookies = ncookies;
456 	}
457 	idp->eofflag = 1;
458 	idp->curroff = uio->uio_offset;
459 	idp->uio_off = uio->uio_offset;
460 
461 	if ((entryoffsetinblock = idp->curroff & bmask) &&
462 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
463 		free(idp, M_TEMP);
464 		return (error);
465 	}
466 	endsearch = dp->i_size;
467 
468 	while (idp->curroff < endsearch) {
469 		/*
470 		 * If offset is on a block boundary,
471 		 * read the next directory block.
472 		 * Release previous if it exists.
473 		 */
474 		if ((idp->curroff & bmask) == 0) {
475 			if (bp != NULL)
476 				brelse(bp);
477 			if ((error =
478 			    cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
479 				break;
480 			entryoffsetinblock = 0;
481 		}
482 		/*
483 		 * Get pointer to next entry.
484 		 */
485 		ep = (struct iso_directory_record *)
486 			((char *)bp->b_data + entryoffsetinblock);
487 
488 		reclen = isonum_711(ep->length);
489 		if (reclen == 0) {
490 			/* skip to next block, if any */
491 			idp->curroff =
492 			    (idp->curroff & ~bmask) + imp->logical_block_size;
493 			continue;
494 		}
495 
496 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
497 			error = EINVAL;
498 			/* illegal entry, stop */
499 			break;
500 		}
501 
502 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
503 			error = EINVAL;
504 			/* illegal directory, so stop looking */
505 			break;
506 		}
507 
508 		idp->current.d_namlen = isonum_711(ep->name_len);
509 
510 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
511 			error = EINVAL;
512 			/* illegal entry, stop */
513 			break;
514 		}
515 
516 		if (isonum_711(ep->flags)&2)
517 			idp->current.d_fileno = isodirino(ep, imp);
518 		else
519 			idp->current.d_fileno = dbtob(bp->b_blkno) +
520 				entryoffsetinblock;
521 
522 		idp->curroff += reclen;
523 		/* NOTE: d_off is the offset of *next* entry. */
524 		idp->current.d_off = idp->curroff;
525 
526 		switch (imp->iso_ftype) {
527 		case ISO_FTYPE_RRIP:
528 			ino = idp->current.d_fileno;
529 			cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
530 			    &ino, imp);
531 			idp->current.d_fileno = ino;
532 			idp->current.d_namlen = (u_char)namelen;
533 			if (idp->current.d_namlen)
534 				error = iso_uiodir(idp,&idp->current,idp->curroff);
535 			break;
536 		default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
537 			strcpy(idp->current.d_name,"..");
538 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
539 				idp->current.d_namlen = 1;
540 				error = iso_uiodir(idp,&idp->current,idp->curroff);
541 			} else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
542 				idp->current.d_namlen = 2;
543 				error = iso_uiodir(idp,&idp->current,idp->curroff);
544 			} else {
545 				isofntrans(ep->name,idp->current.d_namlen,
546 					   idp->current.d_name, &namelen,
547 					   imp->iso_ftype == ISO_FTYPE_9660,
548 					   isonum_711(ep->flags)&4,
549 					   imp->joliet_level,
550 					   imp->im_flags,
551 					   imp->im_d2l);
552 				idp->current.d_namlen = (u_char)namelen;
553 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
554 					error = iso_shipdir(idp);
555 				else
556 					error = iso_uiodir(idp,&idp->current,idp->curroff);
557 			}
558 		}
559 		if (error)
560 			break;
561 
562 		entryoffsetinblock += reclen;
563 	}
564 
565 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
566 		idp->current.d_namlen = 0;
567 		error = iso_shipdir(idp);
568 	}
569 	if (error < 0)
570 		error = 0;
571 
572 	if (ap->a_ncookies != NULL) {
573 		if (error)
574 			free(cookies, M_TEMP);
575 		else {
576 			/*
577 			 * Work out the number of cookies actually used.
578 			 */
579 			*ap->a_ncookies = ncookies - idp->ncookies;
580 			*ap->a_cookies = cookies;
581 		}
582 	}
583 
584 	if (bp)
585 		brelse (bp);
586 
587 	uio->uio_offset = idp->uio_off;
588 	*ap->a_eofflag = idp->eofflag;
589 
590 	free(idp, M_TEMP);
591 
592 	return (error);
593 }
594 
595 /*
596  * Return target name of a symbolic link
597  * Shouldn't we get the parent vnode and read the data from there?
598  * This could eventually result in deadlocks in cd9660_lookup.
599  * But otherwise the block read here is in the block buffer two times.
600  */
601 typedef struct iso_directory_record ISODIR;
602 typedef struct iso_node		    ISONODE;
603 typedef struct iso_mnt		    ISOMNT;
604 static int
605 cd9660_readlink(struct vop_readlink_args *ap)
606 {
607 	ISONODE	*ip;
608 	ISODIR	*dirp;
609 	ISOMNT	*imp;
610 	struct	buf *bp;
611 	struct	uio *uio;
612 	u_short	symlen;
613 	int	error;
614 	char	*symname;
615 
616 	ip  = VTOI(ap->a_vp);
617 	imp = ip->i_mnt;
618 	uio = ap->a_uio;
619 
620 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
621 		return (EINVAL);
622 
623 	/*
624 	 * Get parents directory record block that this inode included.
625 	 */
626 	error = bread(imp->im_devvp,
627 		      (ip->i_number >> imp->im_bshift) <<
628 		      (imp->im_bshift - DEV_BSHIFT),
629 		      imp->logical_block_size, NOCRED, &bp);
630 	if (error) {
631 		return (EINVAL);
632 	}
633 
634 	/*
635 	 * Setup the directory pointer for this inode
636 	 */
637 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
638 
639 	/*
640 	 * Just make sure, we have a right one....
641 	 *   1: Check not cross boundary on block
642 	 */
643 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
644 	    > (unsigned)imp->logical_block_size) {
645 		brelse(bp);
646 		return (EINVAL);
647 	}
648 
649 	/*
650 	 * Now get a buffer
651 	 * Abuse a namei buffer for now.
652 	 */
653 	if (uio->uio_segflg == UIO_SYSSPACE)
654 		symname = uio->uio_iov->iov_base;
655 	else
656 		symname = uma_zalloc(namei_zone, M_WAITOK);
657 
658 	/*
659 	 * Ok, we just gathering a symbolic name in SL record.
660 	 */
661 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
662 		if (uio->uio_segflg != UIO_SYSSPACE)
663 			uma_zfree(namei_zone, symname);
664 		brelse(bp);
665 		return (EINVAL);
666 	}
667 	/*
668 	 * Don't forget before you leave from home ;-)
669 	 */
670 	brelse(bp);
671 
672 	/*
673 	 * return with the symbolic name to caller's.
674 	 */
675 	if (uio->uio_segflg != UIO_SYSSPACE) {
676 		error = uiomove(symname, symlen, uio);
677 		uma_zfree(namei_zone, symname);
678 		return (error);
679 	}
680 	uio->uio_resid -= symlen;
681 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
682 	uio->uio_iov->iov_len -= symlen;
683 	return (0);
684 }
685 
686 /*
687  * Calculate the logical to physical mapping if not done already,
688  * then call the device strategy routine.
689  */
690 static int
691 cd9660_strategy(struct vop_strategy_args *ap)
692 {
693 	struct buf *bp = ap->a_bp;
694 	struct vnode *vp = ap->a_vp;
695 	struct iso_node *ip;
696 	struct bufobj *bo;
697 
698 	ip = VTOI(vp);
699 	if (vp->v_type == VBLK || vp->v_type == VCHR)
700 		panic("cd9660_strategy: spec");
701 	if (bp->b_blkno == bp->b_lblkno) {
702 		bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
703 		    (ip->i_mnt->im_bshift - DEV_BSHIFT);
704 	}
705 	bp->b_iooffset = dbtob(bp->b_blkno);
706 	bo = ip->i_mnt->im_bo;
707 	BO_STRATEGY(bo, bp);
708 	return (0);
709 }
710 
711 /*
712  * Return POSIX pathconf information applicable to cd9660 filesystems.
713  */
714 static int
715 cd9660_pathconf(struct vop_pathconf_args *ap)
716 {
717 
718 	switch (ap->a_name) {
719 	case _PC_FILESIZEBITS:
720 		*ap->a_retval = 32;
721 		return (0);
722 	case _PC_LINK_MAX:
723 		*ap->a_retval = 1;
724 		return (0);
725 	case _PC_NAME_MAX:
726 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
727 			*ap->a_retval = NAME_MAX;
728 		else
729 			*ap->a_retval = 37;
730 		return (0);
731 	case _PC_SYMLINK_MAX:
732 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
733 			*ap->a_retval = MAXPATHLEN;
734 			return (0);
735 		}
736 		return (EINVAL);
737 	case _PC_NO_TRUNC:
738 		*ap->a_retval = 1;
739 		return (0);
740 	default:
741 		return (vop_stdpathconf(ap));
742 	}
743 	/* NOTREACHED */
744 }
745 
746 /*
747  * Vnode pointer to File handle
748  */
749 static int
750 cd9660_vptofh(struct vop_vptofh_args *ap)
751 {
752 	struct ifid ifh;
753 	struct iso_node *ip = VTOI(ap->a_vp);
754 
755 	ifh.ifid_len = sizeof(struct ifid);
756 
757 	ifh.ifid_ino = ip->i_number;
758 	ifh.ifid_start = ip->iso_start;
759 	/*
760 	 * This intentionally uses sizeof(ifh) in order to not copy stack
761 	 * garbage on ILP32.
762 	 */
763 	memcpy(ap->a_fhp, &ifh, sizeof(ifh));
764 
765 #ifdef	ISOFS_DBG
766 	printf("vptofh: ino %jd, start %ld\n",
767 	    (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
768 #endif
769 
770 	return (0);
771 }
772 
773 SYSCTL_NODE(_vfs, OID_AUTO, cd9660, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
774     "cd9660 filesystem");
775 static int use_buf_pager = 1;
776 SYSCTL_INT(_vfs_cd9660, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
777     &use_buf_pager, 0,
778     "Use buffer pager instead of bmap");
779 
780 static daddr_t
781 cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
782 {
783 
784 	return (lblkno(VTOI(vp)->i_mnt, off));
785 }
786 
787 static int
788 cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
789 {
790 	struct iso_node *ip;
791 
792 	ip = VTOI(vp);
793 	*sz = blksize(ip->i_mnt, ip, lbn);
794 	return (0);
795 }
796 
797 static int
798 cd9660_getpages(struct vop_getpages_args *ap)
799 {
800 	struct vnode *vp;
801 
802 	vp = ap->a_vp;
803 	if (vp->v_type == VCHR || vp->v_type == VBLK)
804 		return (EOPNOTSUPP);
805 
806 	if (use_buf_pager)
807 		return (vfs_bio_getpages(vp, ap->a_m, ap->a_count,
808 		    ap->a_rbehind, ap->a_rahead, cd9660_gbp_getblkno,
809 		    cd9660_gbp_getblksz));
810 	return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
811 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
812 }
813 
814 /*
815  * Global vfs data structures for cd9660
816  */
817 struct vop_vector cd9660_vnodeops = {
818 	.vop_default =		&default_vnodeops,
819 	.vop_open =		cd9660_open,
820 	.vop_access =		cd9660_access,
821 	.vop_bmap =		cd9660_bmap,
822 	.vop_cachedlookup =	cd9660_lookup,
823 	.vop_getattr =		cd9660_getattr,
824 	.vop_inactive =		cd9660_inactive,
825 	.vop_ioctl =		cd9660_ioctl,
826 	.vop_lookup =		vfs_cache_lookup,
827 	.vop_pathconf =		cd9660_pathconf,
828 	.vop_read =		cd9660_read,
829 	.vop_readdir =		cd9660_readdir,
830 	.vop_readlink =		cd9660_readlink,
831 	.vop_reclaim =		cd9660_reclaim,
832 	.vop_setattr =		cd9660_setattr,
833 	.vop_strategy =		cd9660_strategy,
834 	.vop_vptofh =		cd9660_vptofh,
835 	.vop_getpages =		cd9660_getpages,
836 };
837 VFS_VOP_VECTOR_REGISTER(cd9660_vnodeops);
838 
839 /*
840  * Special device vnode ops
841  */
842 
843 struct vop_vector cd9660_fifoops = {
844 	.vop_default =		&fifo_specops,
845 	.vop_access =		cd9660_access,
846 	.vop_getattr =		cd9660_getattr,
847 	.vop_inactive =		cd9660_inactive,
848 	.vop_reclaim =		cd9660_reclaim,
849 	.vop_setattr =		cd9660_setattr,
850 	.vop_vptofh =		cd9660_vptofh,
851 };
852 VFS_VOP_VECTOR_REGISTER(cd9660_fifoops);
853