xref: /openbsd/sys/isofs/cd9660/cd9660_vnops.c (revision 5a0ec814)
1 /*	$OpenBSD: cd9660_vnops.c,v 1.97 2024/10/18 05:52:32 miod 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/conf.h>
49 #include <sys/mount.h>
50 #include <sys/vnode.h>
51 #include <sys/lock.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/specdev.h>
58 #include <sys/unistd.h>
59 
60 #include <miscfs/fifofs/fifo.h>
61 
62 #include <isofs/cd9660/iso.h>
63 #include <isofs/cd9660/cd9660_extern.h>
64 #include <isofs/cd9660/cd9660_node.h>
65 #include <isofs/cd9660/iso_rrip.h>
66 
67 int cd9660_kqfilter(void *v);
68 
69 
70 /*
71  * Structure for reading directories
72  */
73 struct isoreaddir {
74 	struct dirent saveent;
75 	struct dirent assocent;
76 	struct dirent current;
77 	off_t saveoff;
78 	off_t assocoff;
79 	off_t curroff;
80 	struct uio *uio;
81 	off_t uio_off;
82 	int eofflag;
83 };
84 
85 int	iso_uiodir(struct isoreaddir *, struct dirent *, off_t);
86 int	iso_shipdir(struct isoreaddir *);
87 
88 /*
89  * Setattr call. Only allowed for block and character special devices.
90  */
91 int
cd9660_setattr(void * v)92 cd9660_setattr(void *v)
93 {
94 	struct vop_setattr_args *ap = v;
95 	struct vnode *vp = ap->a_vp;
96 	struct vattr *vap = ap->a_vap;
97 
98 	if (vap->va_flags != VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
99 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_nsec != VNOVAL ||
100 	    vap->va_mtime.tv_nsec != VNOVAL || vap->va_mode != (mode_t)VNOVAL ||
101 	    (vap->va_vaflags & VA_UTIMES_CHANGE))
102 		return (EROFS);
103 	if (vap->va_size != VNOVAL) {
104 		switch (vp->v_type) {
105 		case VDIR:
106 			return (EISDIR);
107 		case VLNK:
108 		case VREG:
109 			return (EROFS);
110 		case VCHR:
111 		case VBLK:
112 		case VSOCK:
113 		case VFIFO:
114 			return (0);
115 		default:
116 			return (EINVAL);
117 		}
118 	}
119 
120 	return (EINVAL);
121 }
122 
123 /*
124  * Open called.
125  *
126  * Nothing to do.
127  */
128 int
cd9660_open(void * v)129 cd9660_open(void *v)
130 {
131 	return (0);
132 }
133 
134 /*
135  * Close called
136  *
137  * Update the times on the inode on writeable file systems.
138  */
139 int
cd9660_close(void * v)140 cd9660_close(void *v)
141 {
142 	return (0);
143 }
144 
145 /*
146  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
147  * The mode is shifted to select the owner/group/other fields. The
148  * super user is granted all permissions.
149  */
150 int
cd9660_access(void * v)151 cd9660_access(void *v)
152 {
153 	struct vop_access_args *ap = v;
154 	struct iso_node *ip = VTOI(ap->a_vp);
155 
156 	return (vaccess(ap->a_vp->v_type, ip->inode.iso_mode & ALLPERMS,
157 	    ip->inode.iso_uid, ip->inode.iso_gid, ap->a_mode, ap->a_cred));
158 }
159 
160 int
cd9660_getattr(void * v)161 cd9660_getattr(void *v)
162 {
163 	struct vop_getattr_args *ap = v;
164 	struct vnode *vp = ap->a_vp;
165 	struct vattr *vap = ap->a_vap;
166 	struct iso_node *ip = VTOI(vp);
167 
168 	vap->va_fsid	= ip->i_dev;
169 	vap->va_fileid	= ip->i_number;
170 
171 	vap->va_mode	= ip->inode.iso_mode & ALLPERMS;
172 	vap->va_nlink	= ip->inode.iso_links;
173 	vap->va_uid	= ip->inode.iso_uid;
174 	vap->va_gid	= ip->inode.iso_gid;
175 	vap->va_atime	= ip->inode.iso_atime;
176 	vap->va_mtime	= ip->inode.iso_mtime;
177 	vap->va_ctime	= ip->inode.iso_ctime;
178 	vap->va_rdev	= ip->inode.iso_rdev;
179 
180 	vap->va_size	= (u_quad_t) ip->i_size;
181 	if (ip->i_size == 0 && vp->v_type  == VLNK) {
182 		struct vop_readlink_args rdlnk;
183 		struct iovec aiov;
184 		struct uio auio;
185 		char *cp;
186 
187 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
188 		aiov.iov_base = cp;
189 		aiov.iov_len = MAXPATHLEN;
190 		auio.uio_iov = &aiov;
191 		auio.uio_iovcnt = 1;
192 		auio.uio_offset = 0;
193 		auio.uio_rw = UIO_READ;
194 		auio.uio_segflg = UIO_SYSSPACE;
195 		auio.uio_procp = ap->a_p;
196 		auio.uio_resid = MAXPATHLEN;
197 		rdlnk.a_uio = &auio;
198 		rdlnk.a_vp = ap->a_vp;
199 		rdlnk.a_cred = ap->a_cred;
200 		if (cd9660_readlink(&rdlnk) == 0)
201 			vap->va_size = MAXPATHLEN - auio.uio_resid;
202 		free(cp, M_TEMP, 0);
203 	}
204 	vap->va_flags	= 0;
205 	vap->va_gen = 1;
206 	vap->va_blocksize = ip->i_mnt->logical_block_size;
207 	vap->va_bytes	= (u_quad_t) ip->i_size;
208 	vap->va_type	= vp->v_type;
209 	return (0);
210 }
211 
212 /*
213  * Vnode op for reading.
214  */
215 int
cd9660_read(void * v)216 cd9660_read(void *v)
217 {
218 	struct vop_read_args *ap = v;
219 	struct vnode *vp = ap->a_vp;
220 	struct uio *uio = ap->a_uio;
221 	struct iso_node *ip = VTOI(vp);
222 	struct iso_mnt *imp;
223 	struct buf *bp;
224 	daddr_t lbn, rablock;
225 	off_t diff;
226 	int error = 0;
227 	long size, on;
228 	size_t n;
229 
230 	if (uio->uio_resid == 0)
231 		return (0);
232 	if (uio->uio_offset < 0)
233 		return (EINVAL);
234 	ip->i_flag |= IN_ACCESS;
235 	imp = ip->i_mnt;
236 	do {
237 		struct cluster_info *ci = &ip->i_ci;
238 
239 		lbn = lblkno(imp, uio->uio_offset);
240 		on = blkoff(imp, uio->uio_offset);
241 		n = ulmin(imp->logical_block_size - on, uio->uio_resid);
242 		diff = (off_t)ip->i_size - uio->uio_offset;
243 		if (diff <= 0)
244 			return (0);
245 		if (diff < n)
246 			n = diff;
247 		size = blksize(imp, ip, lbn);
248 		rablock = lbn + 1;
249 #define MAX_RA 32
250 		if (ci->ci_lastr + 1 == lbn) {
251 			struct ra {
252 				daddr_t blks[MAX_RA];
253 				int sizes[MAX_RA];
254 			} *ra;
255 			int i;
256 
257 			ra = malloc(sizeof *ra, M_TEMP, M_WAITOK);
258 			for (i = 0; i < MAX_RA &&
259 			    lblktosize(imp, (rablock + i)) < ip->i_size;
260 			    i++) {
261 				ra->blks[i] = rablock + i;
262 				ra->sizes[i] = blksize(imp, ip, rablock + i);
263 			}
264 			error = breadn(vp, lbn, size, ra->blks,
265 			    ra->sizes, i, &bp);
266 			free(ra, M_TEMP, 0);
267 		} else
268 			error = bread(vp, lbn, size, &bp);
269 		ci->ci_lastr = lbn;
270 		n = ulmin(n, size - bp->b_resid);
271 		if (error) {
272 			brelse(bp);
273 			return (error);
274 		}
275 
276 		error = uiomove(bp->b_data + on, n, uio);
277 
278 		brelse(bp);
279 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
280 	return (error);
281 }
282 
283 int
cd9660_ioctl(void * v)284 cd9660_ioctl(void *v)
285 {
286 	return (ENOTTY);
287 }
288 
289 /*
290  * Mmap a file
291  *
292  * NB Currently unsupported.
293  */
294 int
cd9660_mmap(void * v)295 cd9660_mmap(void *v)
296 {
297 
298 	return (EINVAL);
299 }
300 
301 /*
302  * Seek on a file
303  *
304  * Nothing to do, so just return.
305  */
306 int
cd9660_seek(void * v)307 cd9660_seek(void *v)
308 {
309 	return (0);
310 }
311 
312 int
iso_uiodir(struct isoreaddir * idp,struct dirent * dp,off_t off)313 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
314 {
315 	int error;
316 
317 	dp->d_name[dp->d_namlen] = 0;
318 	dp->d_reclen = DIRENT_SIZE(dp);
319 
320 	if (memchr(dp->d_name, '/', dp->d_namlen) != NULL) {
321 		/* illegal file name */
322 		return (EINVAL);
323 	}
324 
325 	if (idp->uio->uio_resid < dp->d_reclen) {
326 		idp->eofflag = 0;
327 		return (-1);
328 	}
329 
330 	dp->d_off = off;
331 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
332 		return (error);
333 	idp->uio_off = off;
334 	return (0);
335 }
336 
337 int
iso_shipdir(struct isoreaddir * idp)338 iso_shipdir(struct isoreaddir *idp)
339 {
340 	struct dirent *dp;
341 	int cl, sl, assoc;
342 	int error;
343 	char *cname, *sname;
344 
345 	cl = idp->current.d_namlen;
346 	cname = idp->current.d_name;
347 
348 	if ((assoc = cl > 1 && *cname == ASSOCCHAR)) {
349 		cl--;
350 		cname++;
351 	}
352 
353 	dp = &idp->saveent;
354 	sname = dp->d_name;
355 	if (!(sl = dp->d_namlen)) {
356 		dp = &idp->assocent;
357 		sname = dp->d_name + 1;
358 		sl = dp->d_namlen - 1;
359 	}
360 	if (sl > 0) {
361 		if (sl != cl
362 		    || bcmp(sname,cname,sl)) {
363 			if (idp->assocent.d_namlen) {
364 				error = iso_uiodir(idp, &idp->assocent,
365 						   idp->assocoff);
366 				if (error)
367 					return (error);
368 				idp->assocent.d_namlen = 0;
369 			}
370 			if (idp->saveent.d_namlen) {
371 				error = iso_uiodir(idp, &idp->saveent,
372 						   idp->saveoff);
373 				if (error)
374 					return (error);
375 				idp->saveent.d_namlen = 0;
376 			}
377 		}
378 	}
379 	idp->current.d_reclen = DIRENT_SIZE(&idp->current);
380 	if (assoc) {
381 		idp->assocoff = idp->curroff;
382 		bcopy(&idp->current,&idp->assocent,idp->current.d_reclen);
383 	} else {
384 		idp->saveoff = idp->curroff;
385 		bcopy(&idp->current,&idp->saveent,idp->current.d_reclen);
386 	}
387 	return (0);
388 }
389 
390 /*
391  * Vnode op for readdir
392  */
393 int
cd9660_readdir(void * v)394 cd9660_readdir(void *v)
395 {
396 	struct vop_readdir_args *ap = v;
397 	struct uio *uio = ap->a_uio;
398 	struct isoreaddir *idp;
399 	struct vnode *vdp = ap->a_vp;
400 	struct iso_node *dp;
401 	struct iso_mnt *imp;
402 	struct buf *bp = NULL;
403 	struct iso_directory_record *ep;
404 	int entryoffsetinblock;
405 	doff_t endsearch;
406 	u_long bmask;
407 	int error = 0;
408 	int reclen;
409 	u_short namelen;
410 	cdino_t ino;
411 
412 	dp = VTOI(vdp);
413 	imp = dp->i_mnt;
414 	bmask = imp->im_bmask;
415 
416 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
417 
418 	/*
419 	 * These are passed to copyout(), so make sure there's no garbage
420 	 * being leaked in padding or after short names.
421 	 */
422 	memset(&idp->saveent, 0, sizeof(idp->saveent));
423 	memset(&idp->assocent, 0, sizeof(idp->assocent));
424 	memset(&idp->current, 0, sizeof(idp->current));
425 
426 	/*
427 	 * XXX
428 	 * Is it worth trying to figure out the type?
429 	 */
430 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
431 	    DT_UNKNOWN;
432 	idp->uio = uio;
433 	idp->eofflag = 1;
434 	idp->curroff = uio->uio_offset;
435 	idp->uio_off = uio->uio_offset;
436 
437 	if ((entryoffsetinblock = idp->curroff & bmask) &&
438 	    (error = cd9660_bufatoff(dp, (off_t)idp->curroff, NULL, &bp))) {
439 		free(idp, M_TEMP, 0);
440 		return (error);
441 	}
442 	endsearch = dp->i_size;
443 
444 	while (idp->curroff < endsearch) {
445 		/*
446 		 * If offset is on a block boundary,
447 		 * read the next directory block.
448 		 * Release previous if it exists.
449 		 */
450 		if ((idp->curroff & bmask) == 0) {
451 			if (bp != NULL)
452 				brelse(bp);
453 			error = cd9660_bufatoff(dp, (off_t)idp->curroff,
454 					     NULL, &bp);
455 			if (error)
456 				break;
457 			entryoffsetinblock = 0;
458 		}
459 		/*
460 		 * Get pointer to next entry.
461 		 */
462 		ep = (struct iso_directory_record *)
463 			((char *)bp->b_data + entryoffsetinblock);
464 
465 		reclen = isonum_711(ep->length);
466 		if (reclen == 0) {
467 			/* skip to next block, if any */
468 			idp->curroff =
469 			    (idp->curroff & ~bmask) + imp->logical_block_size;
470 			continue;
471 		}
472 
473 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
474 			error = EINVAL;
475 			/* illegal entry, stop */
476 			break;
477 		}
478 
479 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
480 			error = EINVAL;
481 			/* illegal directory, so stop looking */
482 			break;
483 		}
484 
485 		idp->current.d_namlen = isonum_711(ep->name_len);
486 
487 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
488 			error = EINVAL;
489 			/* illegal entry, stop */
490 			break;
491 		}
492 
493 		if (isonum_711(ep->flags)&2)
494 			ino = isodirino(ep, imp);
495 		else
496 			ino = dbtob(bp->b_blkno) + entryoffsetinblock;
497 
498 		idp->curroff += reclen;
499 
500 		switch (imp->iso_ftype) {
501 		case ISO_FTYPE_RRIP:
502 			cd9660_rrip_getname(ep,idp->current.d_name, &namelen,
503 					   &ino, imp);
504 			idp->current.d_fileno = ino;
505 			idp->current.d_namlen = (u_char)namelen;
506 			if (idp->current.d_namlen)
507 				error = iso_uiodir(idp,&idp->current,idp->curroff);
508 			break;
509 		default:	/* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 */
510 			idp->current.d_fileno = ino;
511 			strlcpy(idp->current.d_name,"..",
512 			    sizeof idp->current.d_name);
513 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
514 				idp->current.d_namlen = 1;
515 				error = iso_uiodir(idp,&idp->current,idp->curroff);
516 			} else if (idp->current.d_namlen == 1 &&
517 			    ep->name[0] == 1) {
518 				idp->current.d_namlen = 2;
519 				error = iso_uiodir(idp,&idp->current,idp->curroff);
520 			} else {
521 				isofntrans(ep->name,idp->current.d_namlen,
522 					   idp->current.d_name, &namelen,
523 					   imp->iso_ftype == ISO_FTYPE_9660,
524 					   isonum_711(ep->flags) & 4,
525 					   imp->joliet_level);
526 				idp->current.d_namlen = (u_char)namelen;
527 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
528 					error = iso_shipdir(idp);
529 				else
530 					error = iso_uiodir(idp,&idp->current,idp->curroff);
531 			}
532 		}
533 		if (error)
534 			break;
535 
536 		entryoffsetinblock += reclen;
537 	}
538 
539 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
540 		idp->current.d_namlen = 0;
541 		error = iso_shipdir(idp);
542 	}
543 	if (error < 0)
544 		error = 0;
545 
546 	if (bp)
547 		brelse (bp);
548 
549 	uio->uio_offset = idp->uio_off;
550 	*ap->a_eofflag = idp->eofflag;
551 
552 	free(idp, M_TEMP, 0);
553 
554 	return (error);
555 }
556 
557 /*
558  * Return target name of a symbolic link
559  * Shouldn't we get the parent vnode and read the data from there?
560  * This could eventually result in deadlocks in cd9660_lookup.
561  * But otherwise the block read here is in the block buffer two times.
562  */
563 typedef struct iso_directory_record ISODIR;
564 typedef struct iso_node             ISONODE;
565 typedef struct iso_mnt              ISOMNT;
566 int
cd9660_readlink(void * v)567 cd9660_readlink(void *v)
568 {
569 	struct vop_readlink_args *ap = v;
570 	ISONODE	*ip;
571 	ISODIR	*dirp;
572 	ISOMNT	*imp;
573 	struct	buf *bp;
574 	struct	uio *uio;
575 	u_short	symlen;
576 	int	error;
577 	char	*symname;
578 
579 	ip  = VTOI(ap->a_vp);
580 	imp = ip->i_mnt;
581 	uio = ap->a_uio;
582 
583 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
584 		return (EINVAL);
585 
586 	/*
587 	 * Get parents directory record block that this inode included.
588 	 */
589 	error = bread(imp->im_devvp,
590 		      (ip->i_number >> imp->im_bshift) <<
591 		      (imp->im_bshift - DEV_BSHIFT),
592 		      imp->logical_block_size, &bp);
593 	if (error) {
594 		brelse(bp);
595 		return (EINVAL);
596 	}
597 
598 	/*
599 	 * Setup the directory pointer for this inode
600 	 */
601 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
602 
603 	/*
604 	 * Just make sure, we have a right one....
605 	 *   1: Check not cross boundary on block
606 	 */
607 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
608 	    > imp->logical_block_size) {
609 		brelse(bp);
610 		return (EINVAL);
611 	}
612 
613 	/*
614 	 * Now get a buffer
615 	 * Abuse a namei buffer for now.
616 	 */
617 	if (uio->uio_segflg == UIO_SYSSPACE &&
618 	    uio->uio_iov->iov_len >= MAXPATHLEN)
619 		symname = uio->uio_iov->iov_base;
620 	else
621 		symname = pool_get(&namei_pool, PR_WAITOK);
622 
623 	/*
624 	 * Ok, we just gathering a symbolic name in SL record.
625 	 */
626 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
627 		if (uio->uio_segflg != UIO_SYSSPACE ||
628 		    uio->uio_iov->iov_len < MAXPATHLEN)
629 			pool_put(&namei_pool, symname);
630 		brelse(bp);
631 		return (EINVAL);
632 	}
633 	/*
634 	 * Don't forget before you leave from home ;-)
635 	 */
636 	brelse(bp);
637 
638 	/*
639 	 * return with the symbolic name to caller's.
640 	 */
641 	if (uio->uio_segflg != UIO_SYSSPACE ||
642 	    uio->uio_iov->iov_len < MAXPATHLEN) {
643 		error = uiomove(symname, symlen, uio);
644 		pool_put(&namei_pool, symname);
645 		return (error);
646 	}
647 	uio->uio_resid -= symlen;
648 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
649 	uio->uio_iov->iov_len -= symlen;
650 	return (0);
651 }
652 
653 int
cd9660_link(void * v)654 cd9660_link(void *v)
655 {
656 	struct vop_link_args *ap = v;
657 
658 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
659 	vput(ap->a_dvp);
660 	return (EROFS);
661 }
662 
663 int
cd9660_symlink(void * v)664 cd9660_symlink(void *v)
665 {
666 	struct vop_symlink_args *ap = v;
667 
668 	VOP_ABORTOP(ap->a_dvp, ap->a_cnp);
669 	vput(ap->a_dvp);
670 	return (EROFS);
671 }
672 
673 /*
674  * Lock an inode.
675  */
676 int
cd9660_lock(void * v)677 cd9660_lock(void *v)
678 {
679 	struct vop_lock_args *ap = v;
680 	struct vnode *vp = ap->a_vp;
681 
682 	return rrw_enter(&VTOI(vp)->i_lock, ap->a_flags & LK_RWFLAGS);
683 }
684 
685 /*
686  * Unlock an inode.
687  */
688 int
cd9660_unlock(void * v)689 cd9660_unlock(void *v)
690 {
691 	struct vop_unlock_args *ap = v;
692 	struct vnode *vp = ap->a_vp;
693 
694 	rrw_exit(&VTOI(vp)->i_lock);
695 	return 0;
696 }
697 
698 /*
699  * Calculate the logical to physical mapping if not done already,
700  * then call the device strategy routine.
701  */
702 int
cd9660_strategy(void * v)703 cd9660_strategy(void *v)
704 {
705 	struct vop_strategy_args *ap = v;
706 	struct buf *bp = ap->a_bp;
707 	struct vnode *vp = bp->b_vp;
708 	struct iso_node *ip;
709 	int error;
710 	int s;
711 
712 	ip = VTOI(vp);
713 	if (vp->v_type == VBLK || vp->v_type == VCHR)
714 		panic("cd9660_strategy: spec");
715 	if (bp->b_blkno == bp->b_lblkno) {
716 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
717 		if (error) {
718 			bp->b_error = error;
719 			bp->b_flags |= B_ERROR;
720 			s = splbio();
721 			biodone(bp);
722 			splx(s);
723 			return (error);
724 		}
725 		if ((long)bp->b_blkno == -1)
726 			clrbuf(bp);
727 	}
728 	if ((long)bp->b_blkno == -1) {
729 		s = splbio();
730 		biodone(bp);
731 		splx(s);
732 		return (0);
733 	}
734 	vp = ip->i_devvp;
735 	bp->b_dev = vp->v_rdev;
736 	VOP_STRATEGY(vp, bp);
737 	return (0);
738 }
739 
740 /*
741  * Print out the contents of an inode.
742  */
743 int
cd9660_print(void * v)744 cd9660_print(void *v)
745 {
746 #if defined(DEBUG) || defined(DIAGNOSTIC) || defined(VFSLCKDEBUG)
747 	printf("tag VT_ISOFS, isofs vnode\n");
748 #endif
749 	return (0);
750 }
751 
752 /*
753  * Check for a locked inode.
754  */
755 int
cd9660_islocked(void * v)756 cd9660_islocked(void *v)
757 {
758 	struct vop_islocked_args *ap = v;
759 
760 	return rrw_status(&VTOI(ap->a_vp)->i_lock);
761 }
762 
763 /*
764  * Return POSIX pathconf information applicable to cd9660 filesystems.
765  */
766 int
cd9660_pathconf(void * v)767 cd9660_pathconf(void *v)
768 {
769 	struct vop_pathconf_args *ap = v;
770 	int error = 0;
771 
772 	switch (ap->a_name) {
773 	case _PC_LINK_MAX:
774 		*ap->a_retval = 1;
775 		break;
776 	case _PC_NAME_MAX:
777 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
778 			*ap->a_retval = NAME_MAX;
779 		else
780 			*ap->a_retval = 37;
781 		break;
782 	case _PC_CHOWN_RESTRICTED:
783 		*ap->a_retval = 1;
784 		break;
785 	case _PC_NO_TRUNC:
786 		*ap->a_retval = 1;
787 		break;
788 	case _PC_TIMESTAMP_RESOLUTION:
789 		*ap->a_retval = 1000000000;	/* one billion nanoseconds */
790 		break;
791 	default:
792 		error = EINVAL;
793 		break;
794 	}
795 
796 	return (error);
797 }
798 
799 /*
800  * Global vfs data structures for isofs
801  */
802 
803 /* Global vfs data structures for cd9660. */
804 const struct vops cd9660_vops = {
805 	.vop_lookup	= cd9660_lookup,
806 	.vop_create	= eopnotsupp,
807 	.vop_mknod	= eopnotsupp,
808 	.vop_open	= cd9660_open,
809 	.vop_close	= cd9660_close,
810 	.vop_access	= cd9660_access,
811 	.vop_getattr	= cd9660_getattr,
812 	.vop_setattr	= cd9660_setattr,
813 	.vop_read	= cd9660_read,
814 	.vop_write	= eopnotsupp,
815 	.vop_ioctl	= cd9660_ioctl,
816 	.vop_kqfilter	= cd9660_kqfilter,
817 	.vop_revoke	= vop_generic_revoke,
818 	.vop_fsync	= nullop,
819 	.vop_remove	= eopnotsupp,
820 	.vop_link	= cd9660_link,
821 	.vop_rename	= eopnotsupp,
822 	.vop_mkdir	= eopnotsupp,
823 	.vop_rmdir	= eopnotsupp,
824 	.vop_symlink	= cd9660_symlink,
825 	.vop_readdir	= cd9660_readdir,
826 	.vop_readlink	= cd9660_readlink,
827 	.vop_abortop	= vop_generic_abortop,
828 	.vop_inactive	= cd9660_inactive,
829 	.vop_reclaim	= cd9660_reclaim,
830 	.vop_lock	= cd9660_lock,
831 	.vop_unlock	= cd9660_unlock,
832 	.vop_bmap	= cd9660_bmap,
833 	.vop_strategy	= cd9660_strategy,
834 	.vop_print	= cd9660_print,
835 	.vop_islocked	= cd9660_islocked,
836 	.vop_pathconf	= cd9660_pathconf,
837 	.vop_advlock	= eopnotsupp,
838 	.vop_bwrite	= vop_generic_bwrite,
839 };
840 
841 /* Special device vnode ops */
842 const struct vops cd9660_specvops = {
843 	.vop_access	= cd9660_access,
844 	.vop_getattr	= cd9660_getattr,
845 	.vop_setattr	= cd9660_setattr,
846 	.vop_inactive	= cd9660_inactive,
847 	.vop_reclaim	= cd9660_reclaim,
848 	.vop_lock	= cd9660_lock,
849 	.vop_unlock	= cd9660_unlock,
850 	.vop_print	= cd9660_print,
851 	.vop_islocked	= cd9660_islocked,
852 
853 	/* XXX: Keep in sync with spec_vops. */
854 	.vop_lookup	= vop_generic_lookup,
855 	.vop_create	= vop_generic_badop,
856 	.vop_mknod	= vop_generic_badop,
857 	.vop_open	= spec_open,
858 	.vop_close	= spec_close,
859 	.vop_read	= spec_read,
860 	.vop_write	= spec_write,
861 	.vop_ioctl	= spec_ioctl,
862 	.vop_kqfilter	= spec_kqfilter,
863 	.vop_revoke	= vop_generic_revoke,
864 	.vop_fsync	= spec_fsync,
865 	.vop_remove	= vop_generic_badop,
866 	.vop_link	= vop_generic_badop,
867 	.vop_rename	= vop_generic_badop,
868 	.vop_mkdir	= vop_generic_badop,
869 	.vop_rmdir	= vop_generic_badop,
870 	.vop_symlink	= vop_generic_badop,
871 	.vop_readdir	= vop_generic_badop,
872 	.vop_readlink	= vop_generic_badop,
873 	.vop_abortop	= vop_generic_badop,
874 	.vop_bmap	= vop_generic_bmap,
875 	.vop_strategy	= spec_strategy,
876 	.vop_pathconf	= spec_pathconf,
877 	.vop_advlock	= spec_advlock,
878 	.vop_bwrite	= vop_generic_bwrite,
879 };
880 
881 #ifdef FIFO
882 const struct vops cd9660_fifovops = {
883 	.vop_access	= cd9660_access,
884 	.vop_getattr	= cd9660_getattr,
885 	.vop_setattr	= cd9660_setattr,
886 	.vop_inactive	= cd9660_inactive,
887 	.vop_reclaim	= cd9660_reclaim,
888 	.vop_lock	= cd9660_lock,
889 	.vop_unlock	= cd9660_unlock,
890 	.vop_print	= cd9660_print,
891 	.vop_islocked	= cd9660_islocked,
892 	.vop_bwrite	= vop_generic_bwrite,
893 
894 	/* XXX: Keep in sync with fifo_vops. */
895 	.vop_lookup	= vop_generic_lookup,
896 	.vop_create	= vop_generic_badop,
897 	.vop_mknod	= vop_generic_badop,
898 	.vop_open	= fifo_open,
899 	.vop_close	= fifo_close,
900 	.vop_read	= fifo_read,
901 	.vop_write	= fifo_write,
902 	.vop_ioctl	= fifo_ioctl,
903 	.vop_kqfilter	= fifo_kqfilter,
904 	.vop_revoke	= vop_generic_revoke,
905 	.vop_fsync	= nullop,
906 	.vop_remove	= vop_generic_badop,
907 	.vop_link	= vop_generic_badop,
908 	.vop_rename	= vop_generic_badop,
909 	.vop_mkdir	= vop_generic_badop,
910 	.vop_rmdir	= vop_generic_badop,
911 	.vop_symlink	= vop_generic_badop,
912 	.vop_readdir	= vop_generic_badop,
913 	.vop_readlink	= vop_generic_badop,
914 	.vop_abortop	= vop_generic_badop,
915 	.vop_bmap	= vop_generic_bmap,
916 	.vop_strategy	= vop_generic_badop,
917 	.vop_pathconf	= fifo_pathconf,
918 	.vop_advlock	= fifo_advlock,
919 };
920 #endif /* FIFO */
921 
922 void filt_cd9660detach(struct knote *kn);
923 int filt_cd9660read(struct knote *kn, long hint);
924 int filt_cd9660write(struct knote *kn, long hint);
925 int filt_cd9660vnode(struct knote *kn, long hint);
926 
927 const struct filterops cd9660read_filtops = {
928 	.f_flags	= FILTEROP_ISFD,
929 	.f_attach	= NULL,
930 	.f_detach	= filt_cd9660detach,
931 	.f_event	= filt_cd9660read,
932 };
933 
934 const struct filterops cd9660write_filtops = {
935 	.f_flags	= FILTEROP_ISFD,
936 	.f_attach	= NULL,
937 	.f_detach	= filt_cd9660detach,
938 	.f_event	= filt_cd9660write,
939 };
940 
941 const struct filterops cd9660vnode_filtops = {
942 	.f_flags	= FILTEROP_ISFD,
943 	.f_attach	= NULL,
944 	.f_detach	= filt_cd9660detach,
945 	.f_event	= filt_cd9660vnode,
946 };
947 
948 int
cd9660_kqfilter(void * v)949 cd9660_kqfilter(void *v)
950 {
951 	struct vop_kqfilter_args *ap = v;
952 	struct vnode *vp = ap->a_vp;
953 	struct knote *kn = ap->a_kn;
954 
955 	switch (kn->kn_filter) {
956 	case EVFILT_READ:
957 		kn->kn_fop = &cd9660read_filtops;
958 		break;
959 	case EVFILT_WRITE:
960 		kn->kn_fop = &cd9660write_filtops;
961 		break;
962 	case EVFILT_VNODE:
963 		kn->kn_fop = &cd9660vnode_filtops;
964 		break;
965 	default:
966 		return (EINVAL);
967 	}
968 
969 	kn->kn_hook = (caddr_t)vp;
970 
971 	klist_insert_locked(&vp->v_klist, kn);
972 
973 	return (0);
974 }
975 
976 void
filt_cd9660detach(struct knote * kn)977 filt_cd9660detach(struct knote *kn)
978 {
979 	struct vnode *vp = (struct vnode *)kn->kn_hook;
980 
981 	klist_remove_locked(&vp->v_klist, kn);
982 }
983 
984 int
filt_cd9660read(struct knote * kn,long hint)985 filt_cd9660read(struct knote *kn, long hint)
986 {
987 	struct vnode *vp = (struct vnode *)kn->kn_hook;
988 	struct iso_node *node = VTOI(vp);
989 
990 	/*
991 	 * filesystem is gone, so set the EOF flag and schedule
992 	 * the knote for deletion.
993 	 */
994 	if (hint == NOTE_REVOKE) {
995 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
996 		return (1);
997 	}
998 
999 	kn->kn_data = node->i_size - foffset(kn->kn_fp);
1000 	if (kn->kn_data == 0 && kn->kn_sfflags & NOTE_EOF) {
1001 		kn->kn_fflags |= NOTE_EOF;
1002 		return (1);
1003 	}
1004 
1005 	if (kn->kn_flags & (__EV_POLL | __EV_SELECT))
1006 		return (1);
1007 
1008 	return (kn->kn_data != 0);
1009 }
1010 
1011 int
filt_cd9660write(struct knote * kn,long hint)1012 filt_cd9660write(struct knote *kn, long hint)
1013 {
1014 	/*
1015 	 * filesystem is gone, so set the EOF flag and schedule
1016 	 * the knote for deletion.
1017 	 */
1018 	if (hint == NOTE_REVOKE) {
1019 		kn->kn_flags |= (EV_EOF | EV_ONESHOT);
1020 		return (1);
1021 	}
1022 
1023 	kn->kn_data = 0;
1024 	return (1);
1025 }
1026 
1027 int
filt_cd9660vnode(struct knote * kn,long hint)1028 filt_cd9660vnode(struct knote *kn, long hint)
1029 {
1030 	if (kn->kn_sfflags & hint)
1031 		kn->kn_fflags |= hint;
1032 	if (hint == NOTE_REVOKE) {
1033 		kn->kn_flags |= EV_EOF;
1034 		return (1);
1035 	}
1036 	return (kn->kn_fflags != 0);
1037 }
1038