xref: /openbsd/sys/isofs/cd9660/cd9660_vnops.c (revision 3d8817e4)
1 /*	$OpenBSD: cd9660_vnops.c,v 1.53 2011/04/05 14:14:07 thib Exp $	*/
2 /*	$NetBSD: cd9660_vnops.c,v 1.42 1997/10/16 23:56:57 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley
9  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
10  * Support code is derived from software contributed to Berkeley
11  * by Atsushi Murai (amurai@spec.co.jp).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. Neither the name of the University nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  *	@(#)cd9660_vnops.c	8.15 (Berkeley) 12/5/94
38  */
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/resourcevar.h>
44 #include <sys/kernel.h>
45 #include <sys/file.h>
46 #include <sys/stat.h>
47 #include <sys/buf.h>
48 #include <sys/proc.h>
49 #include <sys/conf.h>
50 #include <sys/mount.h>
51 #include <sys/vnode.h>
52 #include <sys/malloc.h>
53 #include <sys/pool.h>
54 #include <sys/dirent.h>
55 #include <sys/ioctl.h>
56 #include <sys/ioccom.h>
57 #include <sys/cdio.h>
58 #include <sys/poll.h>
59 
60 #include <miscfs/fifofs/fifo.h>
61 #include <miscfs/specfs/specdev.h>
62 
63 #include <isofs/cd9660/iso.h>
64 #include <isofs/cd9660/cd9660_extern.h>
65 #include <isofs/cd9660/cd9660_node.h>
66 #include <isofs/cd9660/iso_rrip.h>
67 
68 /*
69  * Structure for reading directories
70  */
71 struct isoreaddir {
72 	struct dirent saveent;
73 	struct dirent assocent;
74 	struct dirent current;
75 	off_t saveoff;
76 	off_t assocoff;
77 	off_t curroff;
78 	struct uio *uio;
79 	off_t uio_off;
80 	int eofflag;
81 	u_long *cookies;
82 	int ncookies;
83 };
84 
85 int	iso_uiodir(struct isoreaddir *, struct dirent *, off_t);
86 int	iso_shipdir(struct isoreaddir *);
87 
88 #if 0
89 /*
90  * Mknod vnode call
91  *  Actually remap the device number
92  */
93 int
94 cd9660_mknod(ndp, vap, cred, p)
95 	struct nameidata *ndp;
96 	struct ucred *cred;
97 	struct vattr *vap;
98 	struct proc *p;
99 {
100 #ifndef	ISODEVMAP
101 	pool_put(&namei_pool, ndp->ni_pnbuf);
102 	vput(ndp->ni_dvp);
103 	vput(ndp->ni_vp);
104 	return (EINVAL);
105 #else
106 	register struct vnode *vp;
107 	struct iso_node *ip;
108 	struct iso_dnode *dp;
109 	int error;
110 
111 	vp = ndp->ni_vp;
112 	ip = VTOI(vp);
113 
114 	if (ip->i_mnt->iso_ftype != ISO_FTYPE_RRIP
115 	    || vap->va_type != vp->v_type
116 	    || (vap->va_type != VCHR && vap->va_type != VBLK)) {
117 		pool_put(&namei_pool, ndp->ni_pnbuf);
118 		vput(ndp->ni_dvp);
119 		vput(ndp->ni_vp);
120 		return (EINVAL);
121 	}
122 
123 	dp = iso_dmap(ip->i_dev,ip->i_number,1);
124 	if (ip->inode.iso_rdev == vap->va_rdev || vap->va_rdev == VNOVAL) {
125 		/* same as the unmapped one, delete the mapping */
126 		remque(dp);
127 		free(dp, M_CACHE);
128 	} else
129 		/* enter new mapping */
130 		dp->d_dev = vap->va_rdev;
131 
132 	/*
133 	 * Remove inode so that it will be reloaded by iget and
134 	 * checked to see if it is an alias of an existing entry
135 	 * in the inode cache.
136 	 */
137 	vput(vp);
138 	vp->v_type = VNON;
139 	vgone(vp);
140 	return (0);
141 #endif
142 }
143 #endif
144 
145 /*
146  * Setattr call. Only allowed for block and character special devices.
147  */
148 int
149 cd9660_setattr(v)
150 	void *v;
151 {
152 	struct vop_setattr_args *ap = v;
153 	struct vnode *vp = ap->a_vp;
154 	struct vattr *vap = ap->a_vap;
155 
156 	if (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
157 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
158 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
159 		return (EROFS);
160 	if (vap->va_size != VNOVAL) {
161 		switch (vp->v_type) {
162 		case VDIR:
163 			return (EISDIR);
164 		case VLNK:
165 		case VREG:
166 			return (EROFS);
167 		case VCHR:
168 		case VBLK:
169 		case VSOCK:
170 		case VFIFO:
171 			return (0);
172 		default:
173 			return (EINVAL);
174 		}
175 	}
176 
177 	return (EINVAL);
178 }
179 
180 /*
181  * Open called.
182  *
183  * Nothing to do.
184  */
185 /* ARGSUSED */
186 int
187 cd9660_open(v)
188 	void *v;
189 {
190 	return (0);
191 }
192 
193 /*
194  * Close called
195  *
196  * Update the times on the inode on writeable file systems.
197  */
198 /* ARGSUSED */
199 int
200 cd9660_close(v)
201 	void *v;
202 {
203 	return (0);
204 }
205 
206 /*
207  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
208  * The mode is shifted to select the owner/group/other fields. The
209  * super user is granted all permissions.
210  */
211 int
212 cd9660_access(v)
213 	void *v;
214 {
215 	struct vop_access_args *ap = v;
216 	struct iso_node *ip = VTOI(ap->a_vp);
217 
218 	return (vaccess(ap->a_vp->v_type, ip->inode.iso_mode & ALLPERMS,
219 	    ip->inode.iso_uid, ip->inode.iso_gid, ap->a_mode, ap->a_cred));
220 }
221 
222 int
223 cd9660_getattr(v)
224 	void *v;
225 {
226 	struct vop_getattr_args *ap = v;
227 	struct vnode *vp = ap->a_vp;
228 	register struct vattr *vap = ap->a_vap;
229 	register struct iso_node *ip = VTOI(vp);
230 
231 	vap->va_fsid	= ip->i_dev;
232 	vap->va_fileid	= ip->i_number;
233 
234 	vap->va_mode	= ip->inode.iso_mode & ALLPERMS;
235 	vap->va_nlink	= ip->inode.iso_links;
236 	vap->va_uid	= ip->inode.iso_uid;
237 	vap->va_gid	= ip->inode.iso_gid;
238 	vap->va_atime	= ip->inode.iso_atime;
239 	vap->va_mtime	= ip->inode.iso_mtime;
240 	vap->va_ctime	= ip->inode.iso_ctime;
241 	vap->va_rdev	= ip->inode.iso_rdev;
242 
243 	vap->va_size	= (u_quad_t) ip->i_size;
244 	if (ip->i_size == 0 && vp->v_type  == VLNK) {
245 		struct vop_readlink_args rdlnk;
246 		struct iovec aiov;
247 		struct uio auio;
248 		char *cp;
249 
250 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
251 		aiov.iov_base = cp;
252 		aiov.iov_len = MAXPATHLEN;
253 		auio.uio_iov = &aiov;
254 		auio.uio_iovcnt = 1;
255 		auio.uio_offset = 0;
256 		auio.uio_rw = UIO_READ;
257 		auio.uio_segflg = UIO_SYSSPACE;
258 		auio.uio_procp = ap->a_p;
259 		auio.uio_resid = MAXPATHLEN;
260 		rdlnk.a_uio = &auio;
261 		rdlnk.a_vp = ap->a_vp;
262 		rdlnk.a_cred = ap->a_cred;
263 		if (cd9660_readlink(&rdlnk) == 0)
264 			vap->va_size = MAXPATHLEN - auio.uio_resid;
265 		free(cp, M_TEMP);
266 	}
267 	vap->va_flags	= 0;
268 	vap->va_gen = 1;
269 	vap->va_blocksize = ip->i_mnt->logical_block_size;
270 	vap->va_bytes	= (u_quad_t) ip->i_size;
271 	vap->va_type	= vp->v_type;
272 	return (0);
273 }
274 
275 /*
276  * Vnode op for reading.
277  */
278 int
279 cd9660_read(v)
280 	void *v;
281 {
282 	struct vop_read_args *ap = v;
283 	struct vnode *vp = ap->a_vp;
284 	register struct uio *uio = ap->a_uio;
285 	register struct iso_node *ip = VTOI(vp);
286 	register struct iso_mnt *imp;
287 	struct buf *bp;
288 	daddr64_t lbn, rablock;
289 	off_t diff;
290 	int error = 0;
291 	long size, n, on;
292 
293 	if (uio->uio_resid == 0)
294 		return (0);
295 	if (uio->uio_offset < 0)
296 		return (EINVAL);
297 	ip->i_flag |= IN_ACCESS;
298 	imp = ip->i_mnt;
299 	do {
300 		struct cluster_info *ci = &ip->i_ci;
301 
302 		lbn = lblkno(imp, uio->uio_offset);
303 		on = blkoff(imp, uio->uio_offset);
304 		n = min((u_int)(imp->logical_block_size - on),
305 			uio->uio_resid);
306 		diff = (off_t)ip->i_size - uio->uio_offset;
307 		if (diff <= 0)
308 			return (0);
309 		if (diff < n)
310 			n = diff;
311 		size = blksize(imp, ip, lbn);
312 		rablock = lbn + 1;
313 #define MAX_RA 32
314 		if (ci->ci_lastr + 1 == lbn) {
315 			struct ra {
316 				daddr64_t blks[MAX_RA];
317 				int sizes[MAX_RA];
318 			} *ra;
319 			int i;
320 
321 			ra = malloc(sizeof *ra, M_TEMP, M_WAITOK);
322 			for (i = 0; i < MAX_RA &&
323 			    lblktosize(imp, (rablock + i)) < ip->i_size;
324 			    i++) {
325 				ra->blks[i] = rablock + i;
326 				ra->sizes[i] = blksize(imp, ip, rablock + i);
327 			}
328 			error = breadn(vp, lbn, size, ra->blks,
329 			    ra->sizes, i, NOCRED, &bp);
330 			free(ra, M_TEMP);
331 		} else
332 			error = bread(vp, lbn, size, NOCRED, &bp);
333 		ci->ci_lastr = lbn;
334 		n = min(n, size - bp->b_resid);
335 		if (error) {
336 			brelse(bp);
337 			return (error);
338 		}
339 
340 		error = uiomove(bp->b_data + on, (int)n, uio);
341 
342                 if (n + on == imp->logical_block_size ||
343 		    uio->uio_offset == (off_t)ip->i_size)
344 			bp->b_flags |= B_AGE;
345 		brelse(bp);
346 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
347 	return (error);
348 }
349 
350 /* ARGSUSED */
351 int
352 cd9660_ioctl(v)
353 	void *v;
354 {
355 	return (ENOTTY);
356 }
357 
358 /* ARGSUSED */
359 int
360 cd9660_poll(v)
361 	void *v;
362 {
363 	struct vop_poll_args *ap = v;
364 
365 	/*
366 	 * We should really check to see if I/O is possible.
367 	 */
368 	return (ap->a_events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM));
369 }
370 
371 /*
372  * Mmap a file
373  *
374  * NB Currently unsupported.
375  */
376 /* ARGSUSED */
377 int
378 cd9660_mmap(v)
379 	void *v;
380 {
381 
382 	return (EINVAL);
383 }
384 
385 /*
386  * Seek on a file
387  *
388  * Nothing to do, so just return.
389  */
390 /* ARGSUSED */
391 int
392 cd9660_seek(v)
393 	void *v;
394 {
395 	return (0);
396 }
397 
398 int
399 iso_uiodir(idp,dp,off)
400 	struct isoreaddir *idp;
401 	struct dirent *dp;
402 	off_t off;
403 {
404 	int error;
405 
406 	dp->d_name[dp->d_namlen] = 0;
407 	dp->d_reclen = DIRENT_SIZE(dp);
408 
409 	if (idp->uio->uio_resid < dp->d_reclen) {
410 		idp->eofflag = 0;
411 		return (-1);
412 	}
413 
414 	if (idp->cookies) {
415 		if (idp->ncookies <= 0) {
416 			idp->eofflag = 0;
417 			return (-1);
418 		}
419 
420 		*idp->cookies++ = off;
421 		--idp->ncookies;
422 	}
423 
424 	if ((error = uiomove((caddr_t)dp, dp->d_reclen, idp->uio)) != 0)
425 		return (error);
426 	idp->uio_off = off;
427 	return (0);
428 }
429 
430 int
431 iso_shipdir(idp)
432 	struct isoreaddir *idp;
433 {
434 	struct dirent *dp;
435 	int cl, sl, assoc;
436 	int error;
437 	char *cname, *sname;
438 
439 	cl = idp->current.d_namlen;
440 	cname = idp->current.d_name;
441 
442 	if ((assoc = cl > 1 && *cname == ASSOCCHAR)) {
443 		cl--;
444 		cname++;
445 	}
446 
447 	dp = &idp->saveent;
448 	sname = dp->d_name;
449 	if (!(sl = dp->d_namlen)) {
450 		dp = &idp->assocent;
451 		sname = dp->d_name + 1;
452 		sl = dp->d_namlen - 1;
453 	}
454 	if (sl > 0) {
455 		if (sl != cl
456 		    || bcmp(sname,cname,sl)) {
457 			if (idp->assocent.d_namlen) {
458 				error = iso_uiodir(idp, &idp->assocent,
459 						   idp->assocoff);
460 				if (error)
461 					return (error);
462 				idp->assocent.d_namlen = 0;
463 			}
464 			if (idp->saveent.d_namlen) {
465 				error = iso_uiodir(idp, &idp->saveent,
466 						   idp->saveoff);
467 				if (error)
468 					return (error);
469 				idp->saveent.d_namlen = 0;
470 			}
471 		}
472 	}
473 	idp->current.d_reclen = DIRENT_SIZE(&idp->current);
474 	if (assoc) {
475 		idp->assocoff = idp->curroff;
476 		bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
477 	} else {
478 		idp->saveoff = idp->curroff;
479 		bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
480 	}
481 	return (0);
482 }
483 
484 /*
485  * Vnode op for readdir
486  */
487 int
488 cd9660_readdir(v)
489 	void *v;
490 {
491 	struct vop_readdir_args *ap = v;
492 	register struct uio *uio = ap->a_uio;
493 	struct isoreaddir *idp;
494 	struct vnode *vdp = ap->a_vp;
495 	struct iso_node *dp;
496 	struct iso_mnt *imp;
497 	struct buf *bp = NULL;
498 	struct iso_directory_record *ep;
499 	int entryoffsetinblock;
500 	doff_t endsearch;
501 	u_long bmask;
502 	int error = 0;
503 	int reclen;
504 	u_short namelen;
505 	int  ncookies = 0;
506 	u_long *cookies = NULL;
507 
508 	dp = VTOI(vdp);
509 	imp = dp->i_mnt;
510 	bmask = imp->im_bmask;
511 
512 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
513 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
514 	/*
515 	 * XXX
516 	 * Is it worth trying to figure out the type?
517 	 */
518 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
519 	    DT_UNKNOWN;
520 	idp->uio = uio;
521 	if (ap->a_ncookies == NULL) {
522 		idp->cookies = NULL;
523 	} else {
524                /*
525                 * Guess the number of cookies needed.
526                 */
527                ncookies = uio->uio_resid / 16;
528                cookies = malloc(ncookies * sizeof(u_long), M_TEMP, M_WAITOK);
529                idp->cookies = cookies;
530                idp->ncookies = ncookies;
531 	}
532 	idp->eofflag = 1;
533 	idp->curroff = uio->uio_offset;
534 	idp->uio_off = uio->uio_offset;
535 
536 	if ((entryoffsetinblock = idp->curroff & bmask) &&
537 	    (error = cd9660_bufatoff(dp, (off_t)idp->curroff, NULL, &bp))) {
538 		free(idp, M_TEMP);
539 		return (error);
540 	}
541 	endsearch = dp->i_size;
542 
543 	while (idp->curroff < endsearch) {
544 		/*
545 		 * If offset is on a block boundary,
546 		 * read the next directory block.
547 		 * Release previous if it exists.
548 		 */
549 		if ((idp->curroff & bmask) == 0) {
550 			if (bp != NULL)
551 				brelse(bp);
552 			error = cd9660_bufatoff(dp, (off_t)idp->curroff,
553 					     NULL, &bp);
554 			if (error)
555 				break;
556 			entryoffsetinblock = 0;
557 		}
558 		/*
559 		 * Get pointer to next entry.
560 		 */
561 		ep = (struct iso_directory_record *)
562 			((char *)bp->b_data + entryoffsetinblock);
563 
564 		reclen = isonum_711(ep->length);
565 		if (reclen == 0) {
566 			/* skip to next block, if any */
567 			idp->curroff =
568 			    (idp->curroff & ~bmask) + imp->logical_block_size;
569 			continue;
570 		}
571 
572 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
573 			error = EINVAL;
574 			/* illegal entry, stop */
575 			break;
576 		}
577 
578 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
579 			error = EINVAL;
580 			/* illegal directory, so stop looking */
581 			break;
582 		}
583 
584 		idp->current.d_namlen = isonum_711(ep->name_len);
585 
586 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
587 			error = EINVAL;
588 			/* illegal entry, stop */
589 			break;
590 		}
591 
592 		if (isonum_711(ep->flags)&2)
593 			idp->current.d_fileno = isodirino(ep, imp);
594 		else
595 			idp->current.d_fileno = dbtob(bp->b_blkno) +
596 				entryoffsetinblock;
597 
598 		idp->curroff += reclen;
599 
600 		switch (imp->iso_ftype) {
601 		case ISO_FTYPE_RRIP:
602 			cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
603 					   &idp->current.d_fileno,imp);
604 			idp->current.d_namlen = (u_char)namelen;
605 			if (idp->current.d_namlen)
606 				error = iso_uiodir(idp,&idp->current,idp->curroff);
607 			break;
608 		default:	/* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 */
609 			strlcpy(idp->current.d_name,"..",
610 			    sizeof idp->current.d_name);
611 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
612 				idp->current.d_namlen = 1;
613 				error = iso_uiodir(idp,&idp->current,idp->curroff);
614 			} else if (idp->current.d_namlen == 1 &&
615 			    ep->name[0] == 1) {
616 				idp->current.d_namlen = 2;
617 				error = iso_uiodir(idp,&idp->current,idp->curroff);
618 			} else {
619 				isofntrans(ep->name,idp->current.d_namlen,
620 					   idp->current.d_name, &namelen,
621 					   imp->iso_ftype == ISO_FTYPE_9660,
622 					   isonum_711(ep->flags) & 4,
623 					   imp->joliet_level);
624 				idp->current.d_namlen = (u_char)namelen;
625 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
626 					error = iso_shipdir(idp);
627 				else
628 					error = iso_uiodir(idp,&idp->current,idp->curroff);
629 			}
630 		}
631 		if (error)
632 			break;
633 
634 		entryoffsetinblock += reclen;
635 	}
636 
637 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
638 		idp->current.d_namlen = 0;
639 		error = iso_shipdir(idp);
640 	}
641 	if (error < 0)
642 		error = 0;
643 
644 	if (ap->a_ncookies != NULL) {
645 		if (error)
646 			free(cookies, M_TEMP);
647 		else {
648 			/*
649 			 * Work out the number of cookies actually used.
650 			 */
651 			*ap->a_ncookies = ncookies - idp->ncookies;
652 			*ap->a_cookies = cookies;
653 		}
654 	}
655 
656 	if (bp)
657 		brelse (bp);
658 
659 	uio->uio_offset = idp->uio_off;
660 	*ap->a_eofflag = idp->eofflag;
661 
662 	free(idp, M_TEMP);
663 
664 	return (error);
665 }
666 
667 /*
668  * Return target name of a symbolic link
669  * Shouldn't we get the parent vnode and read the data from there?
670  * This could eventually result in deadlocks in cd9660_lookup.
671  * But otherwise the block read here is in the block buffer two times.
672  */
673 typedef struct iso_directory_record ISODIR;
674 typedef struct iso_node             ISONODE;
675 typedef struct iso_mnt              ISOMNT;
676 int
677 cd9660_readlink(v)
678 	void *v;
679 {
680 	struct vop_readlink_args *ap = v;
681 	ISONODE	*ip;
682 	ISODIR	*dirp;
683 	ISOMNT	*imp;
684 	struct	buf *bp;
685 	struct	uio *uio;
686 	u_short	symlen;
687 	int	error;
688 	char	*symname;
689 
690 	ip  = VTOI(ap->a_vp);
691 	imp = ip->i_mnt;
692 	uio = ap->a_uio;
693 
694 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
695 		return (EINVAL);
696 
697 	/*
698 	 * Get parents directory record block that this inode included.
699 	 */
700 	error = bread(imp->im_devvp,
701 		      (ip->i_number >> imp->im_bshift) <<
702 		      (imp->im_bshift - DEV_BSHIFT),
703 		      imp->logical_block_size, NOCRED, &bp);
704 	if (error) {
705 		brelse(bp);
706 		return (EINVAL);
707 	}
708 
709 	/*
710 	 * Setup the directory pointer for this inode
711 	 */
712 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
713 
714 	/*
715 	 * Just make sure, we have a right one....
716 	 *   1: Check not cross boundary on block
717 	 */
718 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
719 	    > imp->logical_block_size) {
720 		brelse(bp);
721 		return (EINVAL);
722 	}
723 
724 	/*
725 	 * Now get a buffer
726 	 * Abuse a namei buffer for now.
727 	 */
728 	if (uio->uio_segflg == UIO_SYSSPACE &&
729 	    uio->uio_iov->iov_len >= MAXPATHLEN)
730 		symname = uio->uio_iov->iov_base;
731 	else
732 		symname = pool_get(&namei_pool, PR_WAITOK);
733 
734 	/*
735 	 * Ok, we just gathering a symbolic name in SL record.
736 	 */
737 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
738 		if (uio->uio_segflg != UIO_SYSSPACE ||
739 		    uio->uio_iov->iov_len < MAXPATHLEN)
740 			pool_put(&namei_pool, symname);
741 		brelse(bp);
742 		return (EINVAL);
743 	}
744 	/*
745 	 * Don't forget before you leave from home ;-)
746 	 */
747 	brelse(bp);
748 
749 	/*
750 	 * return with the symbolic name to caller's.
751 	 */
752 	if (uio->uio_segflg != UIO_SYSSPACE ||
753 	    uio->uio_iov->iov_len < MAXPATHLEN) {
754 		error = uiomove(symname, symlen, uio);
755 		pool_put(&namei_pool, symname);
756 		return (error);
757 	}
758 	uio->uio_resid -= symlen;
759 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
760 	uio->uio_iov->iov_len -= symlen;
761 	return (0);
762 }
763 
764 int
765 cd9660_link(v)
766 	void *v;
767 {
768 	struct vop_link_args *ap = v;
769 
770 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
771 	vput(ap->a_dvp);
772 	return (EROFS);
773 }
774 
775 int
776 cd9660_symlink(v)
777 	void *v;
778 {
779 	struct vop_symlink_args *ap = v;
780 
781 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
782 	vput(ap->a_dvp);
783 	return (EROFS);
784 }
785 
786 /*
787  * Lock an inode.
788  */
789 int
790 cd9660_lock(v)
791 	void *v;
792 {
793 	struct vop_lock_args *ap = v;
794 	struct vnode *vp = ap->a_vp;
795 
796 	return (lockmgr(&VTOI(vp)->i_lock, ap->a_flags, NULL));
797 }
798 
799 /*
800  * Unlock an inode.
801  */
802 int
803 cd9660_unlock(v)
804 	void *v;
805 {
806 	struct vop_unlock_args *ap = v;
807 	struct vnode *vp = ap->a_vp;
808 
809 	return (lockmgr(&VTOI(vp)->i_lock, ap->a_flags | LK_RELEASE, NULL));
810 }
811 
812 /*
813  * Calculate the logical to physical mapping if not done already,
814  * then call the device strategy routine.
815  */
816 int
817 cd9660_strategy(v)
818 	void *v;
819 {
820 	struct vop_strategy_args *ap = v;
821 	struct buf *bp = ap->a_bp;
822 	struct vnode *vp = bp->b_vp;
823 	struct iso_node *ip;
824 	int error;
825 	int s;
826 
827 	ip = VTOI(vp);
828 	if (vp->v_type == VBLK || vp->v_type == VCHR)
829 		panic("cd9660_strategy: spec");
830 	if (bp->b_blkno == bp->b_lblkno) {
831 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
832 		if (error) {
833 			bp->b_error = error;
834 			bp->b_flags |= B_ERROR;
835 			s = splbio();
836 			biodone(bp);
837 			splx(s);
838 			return (error);
839 		}
840 		if ((long)bp->b_blkno == -1)
841 			clrbuf(bp);
842 	}
843 	if ((long)bp->b_blkno == -1) {
844 		s = splbio();
845 		biodone(bp);
846 		splx(s);
847 		return (0);
848 	}
849 	vp = ip->i_devvp;
850 	bp->b_dev = vp->v_rdev;
851 	(vp->v_op->vop_strategy)(ap);
852 	return (0);
853 }
854 
855 /*
856  * Print out the contents of an inode.
857  */
858 /*ARGSUSED*/
859 int
860 cd9660_print(v)
861 	void *v;
862 {
863 	printf("tag VT_ISOFS, isofs vnode\n");
864 	return (0);
865 }
866 
867 /*
868  * Check for a locked inode.
869  */
870 int
871 cd9660_islocked(v)
872 	void *v;
873 {
874 	struct vop_islocked_args *ap = v;
875 
876 	return (lockstatus(&VTOI(ap->a_vp)->i_lock));
877 }
878 
879 /*
880  * Return POSIX pathconf information applicable to cd9660 filesystems.
881  */
882 int
883 cd9660_pathconf(v)
884 	void *v;
885 {
886 	struct vop_pathconf_args *ap = v;
887 	switch (ap->a_name) {
888 	case _PC_LINK_MAX:
889 		*ap->a_retval = 1;
890 		return (0);
891 	case _PC_NAME_MAX:
892 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
893 			*ap->a_retval = NAME_MAX;
894 		else
895 			*ap->a_retval = 37;
896 		return (0);
897 	case _PC_PATH_MAX:
898 		*ap->a_retval = PATH_MAX;
899 		return (0);
900 	case _PC_PIPE_BUF:
901 		*ap->a_retval = PIPE_BUF;
902 		return (0);
903 	case _PC_CHOWN_RESTRICTED:
904 		*ap->a_retval = 1;
905 		return (0);
906 	case _PC_NO_TRUNC:
907 		*ap->a_retval = 1;
908 		return (0);
909 	default:
910 		return (EINVAL);
911 	}
912 	/* NOTREACHED */
913 }
914 
915 /*
916  * Global vfs data structures for isofs
917  */
918 #define	cd9660_create	eopnotsupp
919 #define	cd9660_mknod	eopnotsupp
920 #define	cd9660_write	eopnotsupp
921 #define	cd9660_fsync	nullop
922 #define	cd9660_remove	eopnotsupp
923 #define	cd9660_rename	eopnotsupp
924 #define	cd9660_mkdir	eopnotsupp
925 #define	cd9660_rmdir	eopnotsupp
926 #define	cd9660_advlock	eopnotsupp
927 #define	cd9660_valloc	eopnotsupp
928 #define	cd9660_vfree	eopnotsupp
929 #define	cd9660_truncate	eopnotsupp
930 #define	cd9660_update	eopnotsupp
931 #define	cd9660_bwrite	eopnotsupp
932 #define cd9660_revoke   vop_generic_revoke
933 
934 /* Global vfs data structures for cd9660. */
935 struct vops cd9660_vops = {
936 	.vop_lookup	= cd9660_lookup,
937 	.vop_create	= cd9660_create,
938 	.vop_mknod	= cd9660_mknod,
939 	.vop_open	= cd9660_open,
940 	.vop_close	= cd9660_close,
941 	.vop_access	= cd9660_access,
942 	.vop_getattr	= cd9660_getattr,
943 	.vop_setattr	= cd9660_setattr,
944 	.vop_read	= cd9660_read,
945 	.vop_write	= cd9660_write,
946 	.vop_ioctl	= cd9660_ioctl,
947 	.vop_poll	= cd9660_poll,
948 	.vop_revoke	= cd9660_revoke,
949 	.vop_fsync	= cd9660_fsync,
950 	.vop_remove	= cd9660_remove,
951 	.vop_link	= cd9660_link,
952 	.vop_rename	= cd9660_rename,
953 	.vop_mkdir	= cd9660_mkdir,
954 	.vop_rmdir	= cd9660_rmdir,
955 	.vop_symlink	= cd9660_symlink,
956 	.vop_readdir	= cd9660_readdir,
957 	.vop_readlink	= cd9660_readlink,
958 	.vop_abortop	= vop_generic_abortop,
959 	.vop_inactive	= cd9660_inactive,
960 	.vop_reclaim	= cd9660_reclaim,
961 	.vop_lock	= cd9660_lock,
962 	.vop_unlock	= cd9660_unlock,
963 	.vop_bmap	= cd9660_bmap,
964 	.vop_strategy	= cd9660_strategy,
965 	.vop_print	= cd9660_print,
966 	.vop_islocked	= cd9660_islocked,
967 	.vop_pathconf	= cd9660_pathconf,
968 	.vop_advlock	= cd9660_advlock,
969 	.vop_bwrite	= vop_generic_bwrite
970 };
971 
972 /* Special device vnode ops */
973 struct vops cd9660_specvops = {
974 	.vop_access	= cd9660_access,
975 	.vop_getattr	= cd9660_getattr,
976 	.vop_setattr	= cd9660_setattr,
977 	.vop_inactive	= cd9660_inactive,
978 	.vop_reclaim	= cd9660_reclaim,
979 	.vop_lock	= cd9660_lock,
980 	.vop_unlock	= cd9660_unlock,
981 	.vop_print	= cd9660_print,
982 	.vop_islocked	= cd9660_islocked,
983 
984 	/* XXX: Keep in sync with spec_vops. */
985 	.vop_lookup	= vop_generic_lookup,
986 	.vop_create	= spec_badop,
987 	.vop_mknod	= spec_badop,
988 	.vop_open	= spec_open,
989 	.vop_close	= spec_close,
990 	.vop_read	= spec_read,
991 	.vop_write	= spec_write,
992 	.vop_ioctl	= spec_ioctl,
993 	.vop_poll	= spec_poll,
994 	.vop_kqfilter	= spec_kqfilter,
995 	.vop_revoke	= vop_generic_revoke,
996 	.vop_fsync	= spec_fsync,
997 	.vop_remove	= spec_badop,
998 	.vop_link	= spec_badop,
999 	.vop_rename	= spec_badop,
1000 	.vop_mkdir	= spec_badop,
1001 	.vop_rmdir	= spec_badop,
1002 	.vop_symlink	= spec_badop,
1003 	.vop_readdir	= spec_badop,
1004 	.vop_readlink	= spec_badop,
1005 	.vop_abortop	= spec_badop,
1006 	.vop_bmap	= vop_generic_bmap,
1007 	.vop_strategy	= spec_strategy,
1008 	.vop_pathconf	= spec_pathconf,
1009 	.vop_advlock	= spec_advlock,
1010 	.vop_bwrite	= vop_generic_bwrite,
1011 };
1012 
1013 #ifdef FIFO
1014 struct vops cd9660_fifovops = {
1015 	.vop_access	= cd9660_access,
1016 	.vop_getattr	= cd9660_getattr,
1017 	.vop_setattr	= cd9660_setattr,
1018 	.vop_inactive	= cd9660_inactive,
1019 	.vop_reclaim	= cd9660_reclaim,
1020 	.vop_lock	= cd9660_lock,
1021 	.vop_unlock	= cd9660_unlock,
1022 	.vop_print	= cd9660_print,
1023 	.vop_islocked	= cd9660_islocked,
1024 	.vop_bwrite	= vop_generic_bwrite,
1025 
1026 	/* XXX: Keep in sync with fifo_vops. */
1027 	.vop_lookup	= vop_generic_lookup,
1028 	.vop_create	= fifo_badop,
1029 	.vop_mknod	= fifo_badop,
1030 	.vop_open	= fifo_open,
1031 	.vop_close	= fifo_close,
1032 	.vop_read	= fifo_read,
1033 	.vop_write	= fifo_write,
1034 	.vop_ioctl	= fifo_ioctl,
1035 	.vop_poll	= fifo_poll,
1036 	.vop_kqfilter	= fifo_kqfilter,
1037 	.vop_revoke	= vop_generic_revoke,
1038 	.vop_fsync	= nullop,
1039 	.vop_remove	= fifo_badop,
1040 	.vop_link	= fifo_badop,
1041 	.vop_rename	= fifo_badop,
1042 	.vop_mkdir	= fifo_badop,
1043 	.vop_rmdir	= fifo_badop,
1044 	.vop_symlink	= fifo_badop,
1045 	.vop_readdir	= fifo_badop,
1046 	.vop_readlink	= fifo_badop,
1047 	.vop_abortop	= fifo_badop,
1048 	.vop_bmap	= vop_generic_bmap,
1049 	.vop_strategy	= fifo_badop,
1050 	.vop_pathconf	= fifo_pathconf,
1051 	.vop_advlock	= fifo_advlock,
1052 };
1053 #endif /* FIFO */
1054