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