xref: /freebsd/sys/fs/cd9660/cd9660_vnops.c (revision d8b878873e7aa8df1972cc6a642804b17eb61087)
1 /*-
2  * Copyright (c) 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  *	@(#)cd9660_vnops.c	8.19 (Berkeley) 5/27/95
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
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 <fs/fifofs/fifo.h>
51 #include <sys/malloc.h>
52 #include <sys/dirent.h>
53 #include <sys/unistd.h>
54 #include <sys/filio.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 
79 /*
80  * Setattr call. Only allowed for block and character special devices.
81  */
82 static int
83 cd9660_setattr(ap)
84 	struct vop_setattr_args /* {
85 		struct vnodeop_desc *a_desc;
86 		struct vnode *a_vp;
87 		struct vattr *a_vap;
88 		struct ucred *a_cred;
89 	} */ *ap;
90 {
91 	struct vnode *vp = ap->a_vp;
92 	struct vattr *vap = ap->a_vap;
93 
94 	if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
95 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
96 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
97 		return (EROFS);
98 	if (vap->va_size != (u_quad_t)VNOVAL) {
99 		switch (vp->v_type) {
100 		case VDIR:
101 			return (EISDIR);
102 		case VLNK:
103 		case VREG:
104 			return (EROFS);
105 		case VCHR:
106 		case VBLK:
107 		case VSOCK:
108 		case VFIFO:
109 		case VNON:
110 		case VBAD:
111 		case VMARKER:
112 			return (0);
113 		}
114 	}
115 	return (0);
116 }
117 
118 /*
119  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
120  * The mode is shifted to select the owner/group/other fields. The
121  * super user is granted all permissions.
122  */
123 /* ARGSUSED */
124 static int
125 cd9660_access(ap)
126 	struct vop_access_args /* {
127 		struct vnode *a_vp;
128 		accmode_t a_accmode;
129 		struct ucred *a_cred;
130 		struct thread *a_td;
131 	} */ *ap;
132 {
133 	struct vnode *vp = ap->a_vp;
134 	struct iso_node *ip = VTOI(vp);
135 	accmode_t accmode = ap->a_accmode;
136 
137 	if (vp->v_type == VCHR || vp->v_type == VBLK)
138 		return (EOPNOTSUPP);
139 
140 	/*
141 	 * Disallow write attempts unless the file is a socket,
142 	 * fifo, or a block or character device resident on the
143 	 * filesystem.
144 	 */
145 	if (accmode & VWRITE) {
146 		switch (vp->v_type) {
147 		case VDIR:
148 		case VLNK:
149 		case VREG:
150 			return (EROFS);
151 			/* NOT REACHED */
152 		default:
153 			break;
154 		}
155 	}
156 
157 	return (vaccess(vp->v_type, ip->inode.iso_mode, ip->inode.iso_uid,
158 	    ip->inode.iso_gid, ap->a_accmode, ap->a_cred, NULL));
159 }
160 
161 static int
162 cd9660_open(ap)
163 	struct vop_open_args /* {
164 		struct vnode *a_vp;
165 		int a_mode;
166 		struct ucred *a_cred;
167 		struct thread *a_td;
168 		struct file *a_fp;
169 	} */ *ap;
170 {
171 	struct vnode *vp = ap->a_vp;
172 	struct iso_node *ip = VTOI(vp);
173 
174 	if (vp->v_type == VCHR || vp->v_type == VBLK)
175 		return (EOPNOTSUPP);
176 
177 	vnode_create_vobject(vp, ip->i_size, ap->a_td);
178 	return (0);
179 }
180 
181 
182 static int
183 cd9660_getattr(ap)
184 	struct vop_getattr_args /* {
185 		struct vnode *a_vp;
186 		struct vattr *a_vap;
187 		struct ucred *a_cred;
188 	} */ *ap;
189 
190 {
191 	struct vnode *vp = ap->a_vp;
192 	struct vattr *vap = ap->a_vap;
193 	struct iso_node *ip = VTOI(vp);
194 
195 	vap->va_fsid    = dev2udev(ip->i_mnt->im_dev);
196 	vap->va_fileid	= ip->i_number;
197 
198 	vap->va_mode	= ip->inode.iso_mode;
199 	vap->va_nlink	= ip->inode.iso_links;
200 	vap->va_uid	= ip->inode.iso_uid;
201 	vap->va_gid	= ip->inode.iso_gid;
202 	vap->va_atime	= ip->inode.iso_atime;
203 	vap->va_mtime	= ip->inode.iso_mtime;
204 	vap->va_ctime	= ip->inode.iso_ctime;
205 	vap->va_rdev	= ip->inode.iso_rdev;
206 
207 	vap->va_size	= (u_quad_t) ip->i_size;
208 	if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
209 		struct vop_readlink_args rdlnk;
210 		struct iovec aiov;
211 		struct uio auio;
212 		char *cp;
213 
214 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
215 		aiov.iov_base = cp;
216 		aiov.iov_len = MAXPATHLEN;
217 		auio.uio_iov = &aiov;
218 		auio.uio_iovcnt = 1;
219 		auio.uio_offset = 0;
220 		auio.uio_rw = UIO_READ;
221 		auio.uio_segflg = UIO_SYSSPACE;
222 		auio.uio_td = curthread;
223 		auio.uio_resid = MAXPATHLEN;
224 		rdlnk.a_uio = &auio;
225 		rdlnk.a_vp = ap->a_vp;
226 		rdlnk.a_cred = ap->a_cred;
227 		if (cd9660_readlink(&rdlnk) == 0)
228 			vap->va_size = MAXPATHLEN - auio.uio_resid;
229 		free(cp, M_TEMP);
230 	}
231 	vap->va_flags	= 0;
232 	vap->va_gen = 1;
233 	vap->va_blocksize = ip->i_mnt->logical_block_size;
234 	vap->va_bytes	= (u_quad_t) ip->i_size;
235 	vap->va_type	= vp->v_type;
236 	vap->va_filerev	= 0;
237 	return (0);
238 }
239 
240 /*
241  * Vnode op for ioctl.
242  */
243 static int
244 cd9660_ioctl(ap)
245 	struct vop_ioctl_args /* {
246 		struct vnode *a_vp;
247 		u_long  a_command;
248 		caddr_t  a_data;
249 		int  a_fflag;
250 		struct ucred *a_cred;
251 		struct thread *a_td;
252 	} */ *ap;
253 {
254 	struct vnode *vp;
255 	struct iso_node *ip;
256 	int error;
257 
258 	vp = ap->a_vp;
259 	vn_lock(vp, LK_SHARED | LK_RETRY);
260 	if (vp->v_iflag & VI_DOOMED) {
261 		VOP_UNLOCK(vp, 0);
262 		return (EBADF);
263 	}
264 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
265 		VOP_UNLOCK(vp, 0);
266 		return (EOPNOTSUPP);
267 	}
268 
269 	ip = VTOI(vp);
270 	error = 0;
271 
272 	switch (ap->a_command) {
273 	case FIOGETLBA:
274 		*(int *)(ap->a_data) = ip->iso_start;
275 		break;
276 	default:
277 		error = ENOTTY;
278 		break;
279 	}
280 
281 	VOP_UNLOCK(vp, 0);
282 	return (error);
283 }
284 
285 /*
286  * Vnode op for reading.
287  */
288 static int
289 cd9660_read(ap)
290 	struct vop_read_args /* {
291 		struct vnode *a_vp;
292 		struct uio *a_uio;
293 		int a_ioflag;
294 		struct ucred *a_cred;
295 	} */ *ap;
296 {
297 	struct vnode *vp = ap->a_vp;
298 	struct uio *uio = ap->a_uio;
299 	struct iso_node *ip = VTOI(vp);
300 	struct iso_mnt *imp;
301 	struct buf *bp;
302 	daddr_t lbn, rablock;
303 	off_t diff;
304 	int rasize, error = 0;
305 	int seqcount;
306 	long size, n, on;
307 
308 	if (vp->v_type == VCHR || vp->v_type == VBLK)
309 		return (EOPNOTSUPP);
310 
311 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
312 
313 	if (uio->uio_resid == 0)
314 		return (0);
315 	if (uio->uio_offset < 0)
316 		return (EINVAL);
317 	imp = ip->i_mnt;
318 	do {
319 		lbn = lblkno(imp, uio->uio_offset);
320 		on = blkoff(imp, uio->uio_offset);
321 		n = min((u_int)(imp->logical_block_size - on),
322 			uio->uio_resid);
323 		diff = (off_t)ip->i_size - uio->uio_offset;
324 		if (diff <= 0)
325 			return (0);
326 		if (diff < n)
327 			n = diff;
328 		size = blksize(imp, ip, lbn);
329 		rablock = lbn + 1;
330 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
331 			if (lblktosize(imp, rablock) < ip->i_size)
332 				error = cluster_read(vp, (off_t)ip->i_size,
333 					 lbn, size, NOCRED, uio->uio_resid,
334 					 (ap->a_ioflag >> 16), &bp);
335 			else
336 				error = bread(vp, lbn, size, NOCRED, &bp);
337 		} else {
338 			if (seqcount > 1 &&
339 			    lblktosize(imp, rablock) < ip->i_size) {
340 				rasize = blksize(imp, ip, rablock);
341 				error = breadn(vp, lbn, size, &rablock,
342 					       &rasize, 1, NOCRED, &bp);
343 			} else
344 				error = bread(vp, lbn, size, NOCRED, &bp);
345 		}
346 		n = min(n, size - bp->b_resid);
347 		if (error) {
348 			brelse(bp);
349 			return (error);
350 		}
351 
352 		error = uiomove(bp->b_data + on, (int)n, uio);
353 		brelse(bp);
354 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
355 	return (error);
356 }
357 
358 /*
359  * Structure for reading directories
360  */
361 struct isoreaddir {
362 	struct dirent saveent;
363 	struct dirent assocent;
364 	struct dirent current;
365 	off_t saveoff;
366 	off_t assocoff;
367 	off_t curroff;
368 	struct uio *uio;
369 	off_t uio_off;
370 	int eofflag;
371 	u_long *cookies;
372 	int ncookies;
373 };
374 
375 static int
376 iso_uiodir(idp,dp,off)
377 	struct isoreaddir *idp;
378 	struct dirent *dp;
379 	off_t off;
380 {
381 	int error;
382 
383 	dp->d_name[dp->d_namlen] = 0;
384 	dp->d_reclen = GENERIC_DIRSIZ(dp);
385 
386 	if (idp->uio->uio_resid < dp->d_reclen) {
387 		idp->eofflag = 0;
388 		return (-1);
389 	}
390 
391 	if (idp->cookies) {
392 		if (idp->ncookies <= 0) {
393 			idp->eofflag = 0;
394 			return (-1);
395 		}
396 
397 		*idp->cookies++ = off;
398 		--idp->ncookies;
399 	}
400 
401 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
402 		return (error);
403 	idp->uio_off = off;
404 	return (0);
405 }
406 
407 static int
408 iso_shipdir(idp)
409 	struct isoreaddir *idp;
410 {
411 	struct dirent *dp;
412 	int cl, sl, assoc;
413 	int error;
414 	char *cname, *sname;
415 
416 	cl = idp->current.d_namlen;
417 	cname = idp->current.d_name;
418 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
419 	if (assoc) {
420 		cl--;
421 		cname++;
422 	}
423 
424 	dp = &idp->saveent;
425 	sname = dp->d_name;
426 	if (!(sl = dp->d_namlen)) {
427 		dp = &idp->assocent;
428 		sname = dp->d_name + 1;
429 		sl = dp->d_namlen - 1;
430 	}
431 	if (sl > 0) {
432 		if (sl != cl
433 		    || bcmp(sname,cname,sl)) {
434 			if (idp->assocent.d_namlen) {
435 				if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
436 					return (error);
437 				idp->assocent.d_namlen = 0;
438 			}
439 			if (idp->saveent.d_namlen) {
440 				if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
441 					return (error);
442 				idp->saveent.d_namlen = 0;
443 			}
444 		}
445 	}
446 	idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
447 	if (assoc) {
448 		idp->assocoff = idp->curroff;
449 		bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
450 	} else {
451 		idp->saveoff = idp->curroff;
452 		bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
453 	}
454 	return (0);
455 }
456 
457 /*
458  * Vnode op for readdir
459  */
460 static int
461 cd9660_readdir(ap)
462 	struct vop_readdir_args /* {
463 		struct vnode *a_vp;
464 		struct uio *a_uio;
465 		struct ucred *a_cred;
466 		int *a_eofflag;
467 		int *a_ncookies;
468 		u_long **a_cookies;
469 	} */ *ap;
470 {
471 	struct uio *uio = ap->a_uio;
472 	struct isoreaddir *idp;
473 	struct vnode *vdp = ap->a_vp;
474 	struct iso_node *dp;
475 	struct iso_mnt *imp;
476 	struct buf *bp = NULL;
477 	struct iso_directory_record *ep;
478 	int entryoffsetinblock;
479 	doff_t endsearch;
480 	u_long bmask;
481 	int error = 0;
482 	int reclen;
483 	u_short namelen;
484 	int ncookies = 0;
485 	u_long *cookies = NULL;
486 
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 
579 		switch (imp->iso_ftype) {
580 		case ISO_FTYPE_RRIP:
581 			cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
582 					   &idp->current.d_fileno,imp);
583 			idp->current.d_namlen = (u_char)namelen;
584 			if (idp->current.d_namlen)
585 				error = iso_uiodir(idp,&idp->current,idp->curroff);
586 			break;
587 		default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
588 			strcpy(idp->current.d_name,"..");
589 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
590 				idp->current.d_namlen = 1;
591 				error = iso_uiodir(idp,&idp->current,idp->curroff);
592 			} else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
593 				idp->current.d_namlen = 2;
594 				error = iso_uiodir(idp,&idp->current,idp->curroff);
595 			} else {
596 				isofntrans(ep->name,idp->current.d_namlen,
597 					   idp->current.d_name, &namelen,
598 					   imp->iso_ftype == ISO_FTYPE_9660,
599 					   isonum_711(ep->flags)&4,
600 					   imp->joliet_level,
601 					   imp->im_flags,
602 					   imp->im_d2l);
603 				idp->current.d_namlen = (u_char)namelen;
604 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
605 					error = iso_shipdir(idp);
606 				else
607 					error = iso_uiodir(idp,&idp->current,idp->curroff);
608 			}
609 		}
610 		if (error)
611 			break;
612 
613 		entryoffsetinblock += reclen;
614 	}
615 
616 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
617 		idp->current.d_namlen = 0;
618 		error = iso_shipdir(idp);
619 	}
620 	if (error < 0)
621 		error = 0;
622 
623 	if (ap->a_ncookies != NULL) {
624 		if (error)
625 			free(cookies, M_TEMP);
626 		else {
627 			/*
628 			 * Work out the number of cookies actually used.
629 			 */
630 			*ap->a_ncookies = ncookies - idp->ncookies;
631 			*ap->a_cookies = cookies;
632 		}
633 	}
634 
635 	if (bp)
636 		brelse (bp);
637 
638 	uio->uio_offset = idp->uio_off;
639 	*ap->a_eofflag = idp->eofflag;
640 
641 	free(idp, M_TEMP);
642 
643 	return (error);
644 }
645 
646 /*
647  * Return target name of a symbolic link
648  * Shouldn't we get the parent vnode and read the data from there?
649  * This could eventually result in deadlocks in cd9660_lookup.
650  * But otherwise the block read here is in the block buffer two times.
651  */
652 typedef struct iso_directory_record ISODIR;
653 typedef struct iso_node		    ISONODE;
654 typedef struct iso_mnt		    ISOMNT;
655 static int
656 cd9660_readlink(ap)
657 	struct vop_readlink_args /* {
658 		struct vnode *a_vp;
659 		struct uio *a_uio;
660 		struct ucred *a_cred;
661 	} */ *ap;
662 {
663 	ISONODE	*ip;
664 	ISODIR	*dirp;
665 	ISOMNT	*imp;
666 	struct	buf *bp;
667 	struct	uio *uio;
668 	u_short	symlen;
669 	int	error;
670 	char	*symname;
671 
672 	ip  = VTOI(ap->a_vp);
673 	imp = ip->i_mnt;
674 	uio = ap->a_uio;
675 
676 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
677 		return (EINVAL);
678 
679 	/*
680 	 * Get parents directory record block that this inode included.
681 	 */
682 	error = bread(imp->im_devvp,
683 		      (ip->i_number >> imp->im_bshift) <<
684 		      (imp->im_bshift - DEV_BSHIFT),
685 		      imp->logical_block_size, NOCRED, &bp);
686 	if (error) {
687 		brelse(bp);
688 		return (EINVAL);
689 	}
690 
691 	/*
692 	 * Setup the directory pointer for this inode
693 	 */
694 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
695 
696 	/*
697 	 * Just make sure, we have a right one....
698 	 *   1: Check not cross boundary on block
699 	 */
700 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
701 	    > (unsigned)imp->logical_block_size) {
702 		brelse(bp);
703 		return (EINVAL);
704 	}
705 
706 	/*
707 	 * Now get a buffer
708 	 * Abuse a namei buffer for now.
709 	 */
710 	if (uio->uio_segflg == UIO_SYSSPACE)
711 		symname = uio->uio_iov->iov_base;
712 	else
713 		symname = uma_zalloc(namei_zone, M_WAITOK);
714 
715 	/*
716 	 * Ok, we just gathering a symbolic name in SL record.
717 	 */
718 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
719 		if (uio->uio_segflg != UIO_SYSSPACE)
720 			uma_zfree(namei_zone, symname);
721 		brelse(bp);
722 		return (EINVAL);
723 	}
724 	/*
725 	 * Don't forget before you leave from home ;-)
726 	 */
727 	brelse(bp);
728 
729 	/*
730 	 * return with the symbolic name to caller's.
731 	 */
732 	if (uio->uio_segflg != UIO_SYSSPACE) {
733 		error = uiomove(symname, symlen, uio);
734 		uma_zfree(namei_zone, symname);
735 		return (error);
736 	}
737 	uio->uio_resid -= symlen;
738 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
739 	uio->uio_iov->iov_len -= symlen;
740 	return (0);
741 }
742 
743 /*
744  * Calculate the logical to physical mapping if not done already,
745  * then call the device strategy routine.
746  */
747 static int
748 cd9660_strategy(ap)
749 	struct vop_strategy_args /* {
750 		struct buf *a_vp;
751 		struct buf *a_bp;
752 	} */ *ap;
753 {
754 	struct buf *bp = ap->a_bp;
755 	struct vnode *vp = ap->a_vp;
756 	struct iso_node *ip;
757 	struct bufobj *bo;
758 
759 	ip = VTOI(vp);
760 	if (vp->v_type == VBLK || vp->v_type == VCHR)
761 		panic("cd9660_strategy: spec");
762 	if (bp->b_blkno == bp->b_lblkno) {
763 		bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
764 		    (ip->i_mnt->im_bshift - DEV_BSHIFT);
765 	}
766 	bp->b_iooffset = dbtob(bp->b_blkno);
767 	bo = ip->i_mnt->im_bo;
768 	BO_STRATEGY(bo, bp);
769 	return (0);
770 }
771 
772 /*
773  * Return POSIX pathconf information applicable to cd9660 filesystems.
774  */
775 static int
776 cd9660_pathconf(ap)
777 	struct vop_pathconf_args /* {
778 		struct vnode *a_vp;
779 		int a_name;
780 		register_t *a_retval;
781 	} */ *ap;
782 {
783 
784 	switch (ap->a_name) {
785 	case _PC_LINK_MAX:
786 		*ap->a_retval = 1;
787 		return (0);
788 	case _PC_NAME_MAX:
789 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
790 			*ap->a_retval = NAME_MAX;
791 		else
792 			*ap->a_retval = 37;
793 		return (0);
794 	case _PC_PATH_MAX:
795 		*ap->a_retval = PATH_MAX;
796 		return (0);
797 	case _PC_PIPE_BUF:
798 		*ap->a_retval = PIPE_BUF;
799 		return (0);
800 	case _PC_CHOWN_RESTRICTED:
801 		*ap->a_retval = 1;
802 		return (0);
803 	case _PC_NO_TRUNC:
804 		*ap->a_retval = 1;
805 		return (0);
806 	default:
807 		return (EINVAL);
808 	}
809 	/* NOTREACHED */
810 }
811 
812 /*
813  * Vnode pointer to File handle
814  */
815 static int
816 cd9660_vptofh(ap)
817 	struct vop_vptofh_args /* {
818 		struct vnode *a_vp;
819 		struct fid *a_fhp;
820 	} */ *ap;
821 {
822 	struct ifid ifh;
823 	struct iso_node *ip = VTOI(ap->a_vp);
824 
825 	ifh.ifid_len = sizeof(struct ifid);
826 
827 	ifh.ifid_ino = ip->i_number;
828 	ifh.ifid_start = ip->iso_start;
829 	/*
830 	 * This intentionally uses sizeof(ifh) in order to not copy stack
831 	 * garbage on ILP32.
832 	 */
833 	memcpy(ap->a_fhp, &ifh, sizeof(ifh));
834 
835 #ifdef	ISOFS_DBG
836 	printf("vptofh: ino %d, start %ld\n",
837 	    ifh.ifid_ino, ifh.ifid_start);
838 #endif
839 
840 	return (0);
841 }
842 
843 /*
844  * Global vfs data structures for cd9660
845  */
846 struct vop_vector cd9660_vnodeops = {
847 	.vop_default =		&default_vnodeops,
848 	.vop_open =		cd9660_open,
849 	.vop_access =		cd9660_access,
850 	.vop_bmap =		cd9660_bmap,
851 	.vop_cachedlookup =	cd9660_lookup,
852 	.vop_getattr =		cd9660_getattr,
853 	.vop_inactive =		cd9660_inactive,
854 	.vop_ioctl =		cd9660_ioctl,
855 	.vop_lookup =		vfs_cache_lookup,
856 	.vop_pathconf =		cd9660_pathconf,
857 	.vop_read =		cd9660_read,
858 	.vop_readdir =		cd9660_readdir,
859 	.vop_readlink =		cd9660_readlink,
860 	.vop_reclaim =		cd9660_reclaim,
861 	.vop_setattr =		cd9660_setattr,
862 	.vop_strategy =		cd9660_strategy,
863 	.vop_vptofh =		cd9660_vptofh,
864 };
865 
866 /*
867  * Special device vnode ops
868  */
869 
870 struct vop_vector cd9660_fifoops = {
871 	.vop_default =		&fifo_specops,
872 	.vop_access =		cd9660_access,
873 	.vop_getattr =		cd9660_getattr,
874 	.vop_inactive =		cd9660_inactive,
875 	.vop_reclaim =		cd9660_reclaim,
876 	.vop_setattr =		cd9660_setattr,
877 	.vop_vptofh =		cd9660_vptofh,
878 };
879