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