xref: /netbsd-src/sys/fs/cd9660/cd9660_vnops.c (revision f7beae882264b90c469495bcb6b1ba9020347c1e)
1 /*	$NetBSD: cd9660_vnops.c,v 1.63 2024/02/02 20:27:26 christos Exp $	*/
2 
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.15 (Berkeley) 5/27/95
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_vnops.c,v 1.63 2024/02/02 20:27:26 christos Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/resourcevar.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/buf.h>
50 #include <sys/proc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/malloc.h>
54 #include <sys/dirent.h>
55 #include <sys/kauth.h>
56 
57 #include <miscfs/fifofs/fifo.h>
58 #include <miscfs/genfs/genfs.h>
59 #include <miscfs/specfs/specdev.h>
60 
61 #include <fs/cd9660/iso.h>
62 #include <fs/cd9660/cd9660_extern.h>
63 #include <fs/cd9660/cd9660_node.h>
64 #include <fs/cd9660/cd9660_mount.h>
65 #include <fs/cd9660/iso_rrip.h>
66 #include <fs/cd9660/cd9660_mount.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 	off_t *cookies;
82 	int ncookies;
83 };
84 
85 int	iso_uiodir(struct isoreaddir *, struct dirent *, off_t);
86 int	iso_shipdir(struct isoreaddir *);
87 
88 static int
cd9660_check_possible(struct vnode * vp,struct iso_node * ip,accmode_t accmode)89 cd9660_check_possible(struct vnode *vp, struct iso_node *ip, accmode_t accmode)
90 {
91 
92 	/*
93 	 * Disallow write attempts unless the file is a socket,
94 	 * fifo, or a block or character device resident on the
95 	 * file system.
96 	 */
97 	if (accmode & VWRITE) {
98 		switch (vp->v_type) {
99 		case VDIR:
100 		case VLNK:
101 		case VREG:
102 			return (EROFS);
103 		default:
104 			break;
105 		}
106 	}
107 
108 	return 0;
109 }
110 
111 /*
112  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
113  * The mode is shifted to select the owner/group/other fields. The
114  * super user is granted all permissions.
115  */
116 static int
cd9660_check_permitted(struct vnode * vp,struct iso_node * ip,accmode_t accmode,kauth_cred_t cred)117 cd9660_check_permitted(struct vnode *vp, struct iso_node *ip, accmode_t accmode,
118     kauth_cred_t cred)
119 {
120 	accmode_t file_mode;
121 	uid_t uid;
122 	gid_t gid;
123 
124 	file_mode = ip->inode.iso_mode & ALLPERMS;
125 	file_mode &= (vp->v_type == VDIR) ? ip->i_mnt->im_dmask : ip->i_mnt->im_fmask;
126 
127 	uid = (ip->i_mnt->im_flags & ISOFSMNT_UID) ?
128 	    ip->i_mnt->im_uid : ip->inode.iso_uid;
129 	gid = (ip->i_mnt->im_flags & ISOFSMNT_GID) ?
130 	    ip->i_mnt->im_gid : ip->inode.iso_gid;
131 
132 	return kauth_authorize_vnode(cred, KAUTH_ACCESS_ACTION(accmode,
133 	    vp->v_type, file_mode), vp, NULL,
134 	    genfs_can_access(vp, cred, uid, gid,
135 	    file_mode, NULL, accmode));
136 }
137 
138 int
cd9660_access(void * v)139 cd9660_access(void *v)
140 {
141 	struct vop_access_args /* {
142 		struct vnode *a_vp;
143 		accmode_t  a_accmode;
144 		kauth_cred_t a_cred;
145 	} */ *ap = v;
146 	struct vnode *vp = ap->a_vp;
147 	struct iso_node *ip = VTOI(vp);
148 	int error;
149 
150 	error = cd9660_check_possible(vp, ip, ap->a_accmode);
151 	if (error)
152 		return error;
153 
154 	error = cd9660_check_permitted(vp, ip, ap->a_accmode, ap->a_cred);
155 
156 	return error;
157 }
158 
159 int
cd9660_getattr(void * v)160 cd9660_getattr(void *v)
161 {
162 	struct vop_getattr_args /* {
163 		struct vnode *a_vp;
164 		struct vattr *a_vap;
165 		kauth_cred_t a_cred;
166 	} */ *ap = v;
167 	struct vnode *vp = ap->a_vp;
168 	struct iso_node *ip = VTOI(vp);
169 	struct vattr *vap = ap->a_vap;
170 
171 	vap->va_fsid	= ip->i_dev;
172 	vap->va_fileid	= ip->i_number;
173 
174 	vap->va_mode	= ip->inode.iso_mode & ALLPERMS;
175 	vap->va_mode &= (vp->v_type == VDIR) ? ip->i_mnt->im_dmask : ip->i_mnt->im_fmask;
176 	vap->va_nlink	= ip->inode.iso_links;
177 	vap->va_uid	= (ip->i_mnt->im_flags & ISOFSMNT_UID) ?
178 	    ip->i_mnt->im_uid : ip->inode.iso_uid;
179 	vap->va_gid	= (ip->i_mnt->im_flags & ISOFSMNT_GID) ?
180 	    ip->i_mnt->im_gid : ip->inode.iso_gid;
181 	vap->va_atime	= ip->inode.iso_atime;
182 	vap->va_mtime	= ip->inode.iso_mtime;
183 	vap->va_ctime	= ip->inode.iso_ctime;
184 	vap->va_rdev	= ip->inode.iso_rdev;
185 
186 	vap->va_size	= (u_quad_t) ip->i_size;
187 	if (ip->i_size == 0 && vp->v_type == VLNK) {
188 		struct vop_readlink_args rdlnk;
189 		struct iovec aiov;
190 		struct uio auio;
191 		char *cp;
192 
193 		cp = (char *)malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
194 		aiov.iov_base = cp;
195 		aiov.iov_len = MAXPATHLEN;
196 		auio.uio_iov = &aiov;
197 		auio.uio_iovcnt = 1;
198 		auio.uio_offset = 0;
199 		auio.uio_rw = UIO_READ;
200 		auio.uio_resid = MAXPATHLEN;
201 		UIO_SETUP_SYSSPACE(&auio);
202 		rdlnk.a_uio = &auio;
203 		rdlnk.a_vp = ap->a_vp;
204 		rdlnk.a_cred = ap->a_cred;
205 		if (cd9660_readlink(&rdlnk) == 0)
206 			vap->va_size = MAXPATHLEN - auio.uio_resid;
207 		free(cp, M_TEMP);
208 	}
209 	vap->va_flags	= 0;
210 	vap->va_gen = 1;
211 	vap->va_blocksize = ip->i_mnt->logical_block_size;
212 	vap->va_bytes	= (u_quad_t) ip->i_size;
213 	vap->va_type	= vp->v_type;
214 	return (0);
215 }
216 
217 /*
218  * Vnode op for reading.
219  */
220 int
cd9660_read(void * v)221 cd9660_read(void *v)
222 {
223 	struct vop_read_args /* {
224 		struct vnode *a_vp;
225 		struct uio *a_uio;
226 		int a_ioflag;
227 		kauth_cred_t a_cred;
228 	} */ *ap = v;
229 	struct vnode *vp = ap->a_vp;
230 	struct uio *uio = ap->a_uio;
231 	struct iso_node *ip = VTOI(vp);
232 	struct iso_mnt *imp;
233 	struct buf *bp;
234 	daddr_t lbn, rablock;
235 	off_t diff;
236 	int rasize, error = 0;
237 	long size, n, on;
238 
239 	if (uio->uio_resid == 0)
240 		return (0);
241 	if (uio->uio_offset < 0)
242 		return (EINVAL);
243 	if (uio->uio_offset >= ip->i_size)
244 		return 0;
245 	ip->i_flag |= IN_ACCESS;
246 	imp = ip->i_mnt;
247 
248 	if (vp->v_type == VREG) {
249 		const int advice = IO_ADV_DECODE(ap->a_ioflag);
250 		error = 0;
251 
252 		while (uio->uio_resid > 0) {
253 			vsize_t bytelen = MIN(ip->i_size - uio->uio_offset,
254 					      uio->uio_resid);
255 
256 			if (bytelen == 0)
257 				break;
258 			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
259 			    UBC_READ | UBC_PARTIALOK | UBC_VNODE_FLAGS(vp));
260 			if (error)
261 				break;
262 		}
263 		goto out;
264 	}
265 
266 	do {
267 		lbn = cd9660_lblkno(imp, uio->uio_offset);
268 		on = cd9660_blkoff(imp, uio->uio_offset);
269 		n = MIN(imp->logical_block_size - on, uio->uio_resid);
270 		diff = (off_t)ip->i_size - uio->uio_offset;
271 		if (diff <= 0)
272 			return (0);
273 		if (diff < n)
274 			n = diff;
275 		size = cd9660_blksize(imp, ip, lbn);
276 		rablock = lbn + 1;
277 		if (cd9660_lblktosize(imp, rablock) < ip->i_size) {
278 			rasize = cd9660_blksize(imp, ip, rablock);
279 			error = breadn(vp, lbn, size, &rablock,
280 				       &rasize, 1, 0, &bp);
281 		} else {
282 			error = bread(vp, lbn, size, 0, &bp);
283 		}
284 		if (error) {
285 			return (error);
286 		}
287 		n = MIN(n, size - bp->b_resid);
288 
289 		error = uiomove((char *)bp->b_data + on, (int)n, uio);
290 		brelse(bp, 0);
291 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
292 
293 out:
294 	return (error);
295 }
296 
297 int
iso_uiodir(struct isoreaddir * idp,struct dirent * dp,off_t off)298 iso_uiodir(struct isoreaddir *idp, struct dirent *dp, off_t off)
299 {
300 	int error;
301 
302 	dp->d_name[dp->d_namlen] = 0;
303 	dp->d_reclen = _DIRENT_SIZE(dp);
304 
305 	if (idp->uio->uio_resid < dp->d_reclen) {
306 		idp->eofflag = 0;
307 		return (-1);
308 	}
309 
310 	if (idp->cookies) {
311 		if (idp->ncookies <= 0) {
312 			idp->eofflag = 0;
313 			return (-1);
314 		}
315 
316 		*idp->cookies++ = off;
317 		--idp->ncookies;
318 	}
319 
320 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
321 		return (error);
322 	idp->uio_off = off;
323 	return (0);
324 }
325 
326 int
iso_shipdir(struct isoreaddir * idp)327 iso_shipdir(struct isoreaddir *idp)
328 {
329 	struct dirent *dp;
330 	int cl, sl, assoc;
331 	int error;
332 	char *cname, *sname;
333 
334 	cl = idp->current.d_namlen;
335 	cname = idp->current.d_name;
336 
337 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
338 	if (assoc) {
339 		cl--;
340 		cname++;
341 	}
342 
343 	dp = &idp->saveent;
344 	sname = dp->d_name;
345 	if (!(sl = dp->d_namlen)) {
346 		dp = &idp->assocent;
347 		sname = dp->d_name + 1;
348 		sl = dp->d_namlen - 1;
349 	}
350 	if (sl > 0) {
351 		if (sl != cl
352 		    || memcmp(sname, cname, sl)) {
353 			if (idp->assocent.d_namlen) {
354 				error = iso_uiodir(idp, &idp->assocent,
355 						   idp->assocoff);
356 				if (error)
357 					return (error);
358 				idp->assocent.d_namlen = 0;
359 			}
360 			if (idp->saveent.d_namlen) {
361 				error = iso_uiodir(idp, &idp->saveent,
362 						   idp->saveoff);
363 				if (error)
364 					return (error);
365 				idp->saveent.d_namlen = 0;
366 			}
367 		}
368 	}
369 	idp->current.d_reclen = _DIRENT_SIZE(&idp->current);
370 	if (assoc) {
371 		idp->assocoff = idp->curroff;
372 		memcpy(&idp->assocent, &idp->current, idp->current.d_reclen);
373 	} else {
374 		idp->saveoff = idp->curroff;
375 		memcpy(&idp->saveent, &idp->current, idp->current.d_reclen);
376 	}
377 	return (0);
378 }
379 
380 /*
381  * Vnode op for readdir
382  */
383 int
cd9660_readdir(void * v)384 cd9660_readdir(void *v)
385 {
386 	struct vop_readdir_args /* {
387 		struct vnode *a_vp;
388 		struct uio *a_uio;
389 		kauth_cred_t a_cred;
390 		int *a_eofflag;
391 		off_t **a_cookies;
392 		int *a_ncookies;
393 	} */ *ap = v;
394 	struct uio *uio = ap->a_uio;
395 	struct isoreaddir *idp;
396 	struct vnode *vdp = ap->a_vp;
397 	struct iso_node *dp;
398 	struct iso_mnt *imp;
399 	struct buf *bp = NULL;
400 	struct iso_directory_record *ep;
401 	int entryoffsetinblock;
402 	doff_t endsearch;
403 	u_long bmask;
404 	int error = 0;
405 	int reclen;
406 	u_short namelen;
407 	off_t *cookies = NULL;
408 	int ncookies = 0;
409 
410 	if (vdp->v_type != VDIR)
411 		return (ENOTDIR);
412 
413 	dp = VTOI(vdp);
414 	imp = dp->i_mnt;
415 	bmask = imp->im_bmask;
416 
417 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK | M_ZERO);
418 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
419 	/*
420 	 * XXX
421 	 * Is it worth trying to figure out the type?
422 	 */
423 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
424 	    DT_UNKNOWN;
425 	idp->uio = uio;
426 	if (ap->a_ncookies == NULL)
427 		idp->cookies = NULL;
428 	else {
429 		ncookies = uio->uio_resid / _DIRENT_MINSIZE((struct dirent *)0);
430 		cookies = malloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
431 		idp->cookies = cookies;
432 		idp->ncookies = ncookies;
433 	}
434 	idp->eofflag = 1;
435 	idp->curroff = uio->uio_offset;
436 
437 	if ((entryoffsetinblock = idp->curroff & bmask) &&
438 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
439 		free(idp, M_TEMP);
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, 0);
453 			error = cd9660_blkatoff(vdp, (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 		KASSERT(bp != NULL);
463 		ep = (struct iso_directory_record *)
464 			((char *)bp->b_data + entryoffsetinblock);
465 
466 		reclen = isonum_711(ep->length);
467 		if (reclen == 0) {
468 			/* skip to next block, if any */
469 			idp->curroff =
470 			    (idp->curroff & ~bmask) + imp->logical_block_size;
471 			continue;
472 		}
473 
474 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
475 			error = EINVAL;
476 			/* illegal entry, stop */
477 			break;
478 		}
479 
480 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
481 			error = EINVAL;
482 			/* illegal directory, so stop looking */
483 			break;
484 		}
485 
486 		idp->current.d_namlen = isonum_711(ep->name_len);
487 
488 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
489 			error = EINVAL;
490 			/* illegal entry, stop */
491 			break;
492 		}
493 
494 		if (isonum_711(ep->flags)&2)
495 			idp->current.d_fileno = isodirino(ep, imp);
496 		else
497 			idp->current.d_fileno = dbtob(bp->b_blkno) +
498 				entryoffsetinblock;
499 
500 		idp->curroff += reclen;
501 
502 		switch (imp->iso_ftype) {
503 		case ISO_FTYPE_RRIP:
504 			cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
505 			    &idp->current.d_fileno, imp);
506 			idp->current.d_namlen = (u_char)namelen;
507 			if (idp->current.d_namlen)
508 				error = iso_uiodir(idp, &idp->current,
509 				    idp->curroff);
510 			break;
511 		default:	/* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 */
512 			isofntrans(ep->name, idp->current.d_namlen,
513 				   idp->current.d_name, &namelen,
514 				   imp->iso_ftype == ISO_FTYPE_9660,
515 				   (imp->im_flags & ISOFSMNT_NOCASETRANS) == 0,
516 				   isonum_711(ep->flags)&4,
517 				   imp->im_joliet_level);
518 			switch (idp->current.d_name[0]) {
519 			case 0:
520 				idp->current.d_name[0] = '.';
521 				idp->current.d_namlen = 1;
522 				error = iso_uiodir(idp, &idp->current,
523 				    idp->curroff);
524 				break;
525 			case 1:
526 				strlcpy(idp->current.d_name, "..",
527 				    sizeof(idp->current.d_name));
528 				idp->current.d_namlen = 2;
529 				error = iso_uiodir(idp, &idp->current,
530 				    idp->curroff);
531 				break;
532 			default:
533 				idp->current.d_namlen = (u_char)namelen;
534 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
535 					error = iso_shipdir(idp);
536 				else
537 					error = iso_uiodir(idp, &idp->current,
538 					    idp->curroff);
539 				break;
540 			}
541 		}
542 		if (error)
543 			break;
544 
545 		entryoffsetinblock += reclen;
546 	}
547 
548 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
549 		idp->current.d_namlen = 0;
550 		error = iso_shipdir(idp);
551 	}
552 	if (error < 0)
553 		error = 0;
554 
555 	if (ap->a_ncookies != NULL) {
556 		if (error)
557 			free(cookies, M_TEMP);
558 		else {
559 			/*
560 			 * Work out the number of cookies actually used.
561 			 */
562 			*ap->a_ncookies = ncookies - idp->ncookies;
563 			*ap->a_cookies = cookies;
564 		}
565 	}
566 
567 	if (bp)
568 		brelse(bp, 0);
569 
570 	uio->uio_offset = idp->uio_off;
571 	*ap->a_eofflag = idp->eofflag;
572 
573 	free(idp, M_TEMP);
574 
575 	return (error);
576 }
577 
578 /*
579  * Return target name of a symbolic link
580  * Shouldn't we get the parent vnode and read the data from there?
581  * This could eventually result in deadlocks in cd9660_lookup.
582  * But otherwise the block read here is in the block buffer two times.
583  */
584 typedef struct iso_directory_record ISODIR;
585 typedef struct iso_node             ISONODE;
586 typedef struct iso_mnt              ISOMNT;
587 
588 int
cd9660_readlink(void * v)589 cd9660_readlink(void *v)
590 {
591 	struct vop_readlink_args /* {
592 		struct vnode *a_vp;
593 		struct uio *a_uio;
594 		kauth_cred_t a_cred;
595 	} */ *ap = v;
596 	ISONODE	*ip;
597 	ISODIR	*dirp;
598 	ISOMNT	*imp;
599 	struct	buf *bp;
600 	struct	uio *uio;
601 	u_short	symlen;
602 	int	error;
603 	char	*symname;
604 	bool use_pnbuf;
605 
606 	ip  = VTOI(ap->a_vp);
607 	imp = ip->i_mnt;
608 	uio = ap->a_uio;
609 
610 	if (imp->iso_ftype != ISO_FTYPE_RRIP)
611 		return (EINVAL);
612 
613 	/*
614 	 * Get parents directory record block that this inode included.
615 	 */
616 	error = bread(imp->im_devvp,
617 		      (ip->i_number >> imp->im_bshift) <<
618 		      (imp->im_bshift - DEV_BSHIFT),
619 		      imp->logical_block_size, 0, &bp);
620 	if (error) {
621 		return (EINVAL);
622 	}
623 
624 	/*
625 	 * Setup the directory pointer for this inode
626 	 */
627 	dirp = (ISODIR *)((char *)bp->b_data + (ip->i_number & imp->im_bmask));
628 
629 	/*
630 	 * Just make sure, we have a right one....
631 	 *   1: Check not cross boundary on block
632 	 */
633 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
634 	    > imp->logical_block_size) {
635 		brelse(bp, 0);
636 		return (EINVAL);
637 	}
638 
639 	/*
640 	 * Now get a buffer
641 	 * Abuse a namei buffer for now.
642 	 */
643 	use_pnbuf = !VMSPACE_IS_KERNEL_P(uio->uio_vmspace) ||
644 	    uio->uio_iov->iov_len < MAXPATHLEN;
645 	if (use_pnbuf) {
646 		symname = PNBUF_GET();
647 	} else {
648 		symname = uio->uio_iov->iov_base;
649 	}
650 
651 	/*
652 	 * Ok, we just gathering a symbolic name in SL record.
653 	 */
654 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
655 		if (use_pnbuf) {
656 			PNBUF_PUT(symname);
657 		}
658 		brelse(bp, 0);
659 		return (EINVAL);
660 	}
661 	/*
662 	 * Don't forget before you leave from home ;-)
663 	 */
664 	brelse(bp, 0);
665 
666 	/*
667 	 * return with the symbolic name to caller's.
668 	 */
669 	if (use_pnbuf) {
670 		error = uiomove(symname, symlen, uio);
671 		PNBUF_PUT(symname);
672 		return (error);
673 	}
674 	uio->uio_resid -= symlen;
675 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
676 	uio->uio_iov->iov_len -= symlen;
677 	return (0);
678 }
679 
680 /*
681  * Calculate the logical to physical mapping if not done already,
682  * then call the device strategy routine.
683  */
684 int
cd9660_strategy(void * v)685 cd9660_strategy(void *v)
686 {
687 	struct vop_strategy_args /* {
688 		struct vnode *a_vp;
689 		struct buf *a_bp;
690 	} */ *ap = v;
691 	struct buf *bp = ap->a_bp;
692 	struct vnode *vp = ap->a_vp;
693 	struct iso_node *ip;
694 	int error;
695 
696 	ip = VTOI(vp);
697 	if (vp->v_type == VBLK || vp->v_type == VCHR)
698 		panic("cd9660_strategy: spec");
699 	if (bp->b_blkno == bp->b_lblkno) {
700 		error = VOP_BMAP(vp, bp->b_lblkno, NULL, &bp->b_blkno, NULL);
701 		if (error) {
702 			bp->b_error = error;
703 			biodone(bp);
704 			return (error);
705 		}
706 		if ((long)bp->b_blkno == -1)
707 			clrbuf(bp);
708 	}
709 	if ((long)bp->b_blkno == -1) {
710 		biodone(bp);
711 		return (0);
712 	}
713 	vp = ip->i_mnt->im_devvp;
714 	return (VOP_STRATEGY(vp, bp));
715 }
716 
717 /*
718  * Print out the contents of an inode.
719  */
720 /*ARGSUSED*/
721 int
cd9660_print(void * v)722 cd9660_print(void *v)
723 {
724 
725 	printf("tag VT_ISOFS, isofs vnode\n");
726 	return (0);
727 }
728 
729 /*
730  * Return POSIX pathconf information applicable to cd9660 filesystems.
731  */
732 int
cd9660_pathconf(void * v)733 cd9660_pathconf(void *v)
734 {
735 	struct vop_pathconf_args /* {
736 		struct vnode *a_vp;
737 		int a_name;
738 		register_t *a_retval;
739 	} */ *ap = v;
740 	switch (ap->a_name) {
741 	case _PC_LINK_MAX:
742 		*ap->a_retval = 1;
743 		return (0);
744 	case _PC_NAME_MAX:
745 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
746 			*ap->a_retval = ISO_MAXNAMLEN;
747 		else
748 			*ap->a_retval = 37;
749 		return (0);
750 	case _PC_PATH_MAX:
751 		*ap->a_retval = PATH_MAX;
752 		return (0);
753 	case _PC_PIPE_BUF:
754 		*ap->a_retval = PIPE_BUF;
755 		return (0);
756 	case _PC_CHOWN_RESTRICTED:
757 		*ap->a_retval = 1;
758 		return (0);
759 	case _PC_NO_TRUNC:
760 		*ap->a_retval = 1;
761 		return (0);
762 	case _PC_SYNC_IO:
763 		*ap->a_retval = 1;
764 		return (0);
765 	case _PC_FILESIZEBITS:
766 		*ap->a_retval = 32;
767 		return (0);
768 	default:
769 		return genfs_pathconf(ap);
770 	}
771 	/* NOTREACHED */
772 }
773 
774 /*
775  * Allow changing the size for special files (and fifos).
776  */
777 int
cd9660_setattr(void * v)778 cd9660_setattr(void *v)
779 {
780 	struct vop_setattr_args /* {
781 		struct vnodeop_desc *a_desc;
782 		struct vnode *a_vp;
783 		struct vattr *a_vap;
784 		kauth_cred_t a_cred;
785 		struct proc *a_p;
786 	} */ *ap = v;
787 	struct vattr *vap = ap->a_vap;
788 	struct vnode *vp = ap->a_vp;
789 
790 	/*
791 	 * Only size is changeable.
792 	 */
793 	if (vap->va_type != VNON
794 	    || vap->va_nlink != (nlink_t)VNOVAL
795 	    || vap->va_fsid != VNOVAL
796 	    || vap->va_fileid != VNOVAL
797 	    || vap->va_blocksize != VNOVAL
798 	    || vap->va_rdev != (dev_t)VNOVAL
799 	    || (int)vap->va_bytes != VNOVAL
800 	    || vap->va_gen != VNOVAL
801 	    || vap->va_flags != VNOVAL
802 	    || vap->va_uid != (uid_t)VNOVAL
803 	    || vap->va_gid != (gid_t)VNOVAL
804 	    || vap->va_atime.tv_sec != VNOVAL
805 	    || vap->va_mtime.tv_sec != VNOVAL
806 	    || vap->va_mode != (mode_t)VNOVAL)
807 		return EOPNOTSUPP;
808 
809 	if (vap->va_size != VNOVAL
810 	    && vp->v_type != VCHR
811 	    && vp->v_type != VBLK
812 	    && vp->v_type != VFIFO)
813 		return EOPNOTSUPP;
814 
815 	return 0;
816 }
817 
818 /*
819  * Global vfs data structures for cd9660
820  */
821 int (**cd9660_vnodeop_p)(void *);
822 const struct vnodeopv_entry_desc cd9660_vnodeop_entries[] = {
823 	{ &vop_default_desc, vn_default_error },
824 	{ &vop_parsepath_desc, genfs_parsepath },	/* parsepath */
825 	{ &vop_lookup_desc, cd9660_lookup },		/* lookup */
826 	{ &vop_create_desc, genfs_eopnotsupp },		/* create */
827 	{ &vop_mknod_desc, genfs_eopnotsupp },		/* mknod */
828 	{ &vop_open_desc, genfs_nullop },		/* open */
829 	{ &vop_close_desc, genfs_nullop },		/* close */
830 	{ &vop_access_desc, cd9660_access },		/* access */
831 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
832 	{ &vop_getattr_desc, cd9660_getattr },		/* getattr */
833 	{ &vop_setattr_desc, cd9660_setattr },		/* setattr */
834 	{ &vop_read_desc, cd9660_read },		/* read */
835 	{ &vop_write_desc, genfs_eopnotsupp },		/* write */
836 	{ &vop_fallocate_desc, genfs_eopnotsupp },	/* fallocate */
837 	{ &vop_fdiscard_desc, genfs_eopnotsupp },	/* fdiscard */
838 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
839 	{ &vop_ioctl_desc, genfs_enoioctl },		/* ioctl */
840 	{ &vop_poll_desc, genfs_poll },			/* poll */
841 	{ &vop_revoke_desc, genfs_revoke },		/* revoke */
842 	{ &vop_mmap_desc, genfs_mmap },			/* mmap */
843 	{ &vop_fsync_desc, genfs_nullop },		/* fsync */
844 	{ &vop_seek_desc, genfs_seek },			/* seek */
845 	{ &vop_remove_desc, genfs_eopnotsupp },		/* remove */
846 	{ &vop_link_desc, genfs_erofs_link },		/* link */
847 	{ &vop_rename_desc, genfs_eopnotsupp },		/* rename */
848 	{ &vop_mkdir_desc, genfs_eopnotsupp },		/* mkdir */
849 	{ &vop_rmdir_desc, genfs_eopnotsupp },		/* rmdir */
850 	{ &vop_symlink_desc, genfs_erofs_symlink },	/* symlink */
851 	{ &vop_readdir_desc, cd9660_readdir },		/* readdir */
852 	{ &vop_readlink_desc, cd9660_readlink },	/* readlink */
853 	{ &vop_abortop_desc, genfs_abortop },		/* abortop */
854 	{ &vop_inactive_desc, cd9660_inactive },	/* inactive */
855 	{ &vop_reclaim_desc, cd9660_reclaim },		/* reclaim */
856 	{ &vop_lock_desc, genfs_lock },			/* lock */
857 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
858 	{ &vop_bmap_desc, cd9660_bmap },		/* bmap */
859 	{ &vop_strategy_desc, cd9660_strategy },	/* strategy */
860 	{ &vop_print_desc, cd9660_print },		/* print */
861 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
862 	{ &vop_pathconf_desc, cd9660_pathconf },	/* pathconf */
863 	{ &vop_advlock_desc, genfs_einval },		/* advlock */
864 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
865 	{ &vop_getpages_desc, genfs_getpages },		/* getpages */
866 	{ &vop_putpages_desc, genfs_putpages },		/* putpages */
867 	{ NULL, NULL }
868 };
869 const struct vnodeopv_desc cd9660_vnodeop_opv_desc =
870 	{ &cd9660_vnodeop_p, cd9660_vnodeop_entries };
871 
872 /*
873  * Special device vnode ops
874  */
875 int (**cd9660_specop_p)(void *);
876 const struct vnodeopv_entry_desc cd9660_specop_entries[] = {
877 	{ &vop_default_desc, vn_default_error },
878 	GENFS_SPECOP_ENTRIES,
879 	{ &vop_close_desc, spec_close },		/* close */
880 	{ &vop_access_desc, cd9660_access },		/* access */
881 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
882 	{ &vop_getattr_desc, cd9660_getattr },		/* getattr */
883 	{ &vop_setattr_desc, cd9660_setattr },		/* setattr */
884 	{ &vop_read_desc, spec_read },			/* read */
885 	{ &vop_write_desc, spec_write },		/* write */
886 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
887 	{ &vop_fsync_desc, spec_fsync },		/* fsync */
888 	{ &vop_inactive_desc, cd9660_inactive },	/* inactive */
889 	{ &vop_reclaim_desc, cd9660_reclaim },		/* reclaim */
890 	{ &vop_lock_desc, genfs_lock },			/* lock */
891 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
892 	{ &vop_print_desc, cd9660_print },		/* print */
893 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
894 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
895 	{ NULL, NULL }
896 };
897 const struct vnodeopv_desc cd9660_specop_opv_desc =
898 	{ &cd9660_specop_p, cd9660_specop_entries };
899 
900 int (**cd9660_fifoop_p)(void *);
901 const struct vnodeopv_entry_desc cd9660_fifoop_entries[] = {
902 	{ &vop_default_desc, vn_default_error },
903 	GENFS_FIFOOP_ENTRIES,
904 	{ &vop_close_desc, vn_fifo_bypass },		/* close */
905 	{ &vop_access_desc, cd9660_access },		/* access */
906 	{ &vop_accessx_desc, genfs_accessx },		/* accessx */
907 	{ &vop_getattr_desc, cd9660_getattr },		/* getattr */
908 	{ &vop_setattr_desc, cd9660_setattr },		/* setattr */
909 	{ &vop_read_desc, vn_fifo_bypass },		/* read */
910 	{ &vop_write_desc, vn_fifo_bypass },		/* write */
911 	{ &vop_fcntl_desc, genfs_fcntl },		/* fcntl */
912 	{ &vop_fsync_desc, vn_fifo_bypass },		/* fsync */
913 	{ &vop_inactive_desc, cd9660_inactive },	/* inactive */
914 	{ &vop_reclaim_desc, cd9660_reclaim },		/* reclaim */
915 	{ &vop_lock_desc, genfs_lock },			/* lock */
916 	{ &vop_unlock_desc, genfs_unlock },		/* unlock */
917 	{ &vop_strategy_desc, vn_fifo_bypass },		/* strategy */
918 	{ &vop_print_desc, cd9660_print },		/* print */
919 	{ &vop_islocked_desc, genfs_islocked },		/* islocked */
920 	{ &vop_bwrite_desc, vn_bwrite },		/* bwrite */
921 	{ NULL, NULL }
922 };
923 const struct vnodeopv_desc cd9660_fifoop_opv_desc =
924 	{ &cd9660_fifoop_p, cd9660_fifoop_entries };
925