xref: /freebsd-13-stable/sys/fs/cd9660/cd9660_vnops.c (revision cd597b4bb194f7a0bd756b5299b40ad3607b1baf)
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 (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.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(ap)85 cd9660_setattr(ap)
86 	struct vop_setattr_args /* {
87 		struct vnodeop_desc *a_desc;
88 		struct vnode *a_vp;
89 		struct vattr *a_vap;
90 		struct ucred *a_cred;
91 	} */ *ap;
92 {
93 	struct vnode *vp = ap->a_vp;
94 	struct vattr *vap = ap->a_vap;
95 
96 	if (vap->va_flags != (u_long)VNOVAL || vap->va_uid != (uid_t)VNOVAL ||
97 	    vap->va_gid != (gid_t)VNOVAL || vap->va_atime.tv_sec != VNOVAL ||
98 	    vap->va_mtime.tv_sec != VNOVAL || vap->va_mode != (mode_t)VNOVAL)
99 		return (EROFS);
100 	if (vap->va_size != (u_quad_t)VNOVAL) {
101 		switch (vp->v_type) {
102 		case VDIR:
103 			return (EISDIR);
104 		case VLNK:
105 		case VREG:
106 			return (EROFS);
107 		case VCHR:
108 		case VBLK:
109 		case VSOCK:
110 		case VFIFO:
111 		case VNON:
112 		case VBAD:
113 		case VMARKER:
114 			return (0);
115 		}
116 	}
117 	return (0);
118 }
119 
120 /*
121  * Check mode permission on inode pointer. Mode is READ, WRITE or EXEC.
122  * The mode is shifted to select the owner/group/other fields. The
123  * super user is granted all permissions.
124  */
125 /* ARGSUSED */
126 static int
cd9660_access(ap)127 cd9660_access(ap)
128 	struct vop_access_args /* {
129 		struct vnode *a_vp;
130 		accmode_t a_accmode;
131 		struct ucred *a_cred;
132 		struct thread *a_td;
133 	} */ *ap;
134 {
135 	struct vnode *vp = ap->a_vp;
136 	struct iso_node *ip = VTOI(vp);
137 	accmode_t accmode = ap->a_accmode;
138 	accmode_t file_mode;
139 	uid_t uid;
140 	gid_t gid;
141 
142 	if (vp->v_type == VCHR || vp->v_type == VBLK)
143 		return (EOPNOTSUPP);
144 
145 	/*
146 	 * Disallow write attempts unless the file is a socket,
147 	 * fifo, or a block or character device resident on the
148 	 * filesystem.
149 	 */
150 	if (accmode & VWRITE) {
151 		switch (vp->v_type) {
152 		case VDIR:
153 		case VLNK:
154 		case VREG:
155 			return (EROFS);
156 			/* NOT REACHED */
157 		default:
158 			break;
159 		}
160 	}
161 
162 	file_mode = ip->inode.iso_mode;
163 	file_mode &= (vp->v_type == VDIR) ? ip->i_mnt->im_dmask : ip->i_mnt->im_fmask;
164 
165 	uid = (ip->i_mnt->im_flags & ISOFSMNT_UID) ?
166 		ip->i_mnt->im_uid : ip->inode.iso_uid;
167 	gid = (ip->i_mnt->im_flags & ISOFSMNT_GID) ?
168 		ip->i_mnt->im_gid : ip->inode.iso_gid;
169 
170 	return (vaccess(vp->v_type, file_mode, uid,
171 	    gid, ap->a_accmode, ap->a_cred));
172 }
173 
174 static int
cd9660_open(ap)175 cd9660_open(ap)
176 	struct vop_open_args /* {
177 		struct vnode *a_vp;
178 		int a_mode;
179 		struct ucred *a_cred;
180 		struct thread *a_td;
181 		struct file *a_fp;
182 	} */ *ap;
183 {
184 	struct vnode *vp = ap->a_vp;
185 	struct iso_node *ip = VTOI(vp);
186 
187 	if (vp->v_type == VCHR || vp->v_type == VBLK)
188 		return (EOPNOTSUPP);
189 
190 	vnode_create_vobject(vp, ip->i_size, ap->a_td);
191 	return (0);
192 }
193 
194 static int
cd9660_getattr(ap)195 cd9660_getattr(ap)
196 	struct vop_getattr_args /* {
197 		struct vnode *a_vp;
198 		struct vattr *a_vap;
199 		struct ucred *a_cred;
200 	} */ *ap;
201 
202 {
203 	struct vnode *vp = ap->a_vp;
204 	struct vattr *vap = ap->a_vap;
205 	struct iso_node *ip = VTOI(vp);
206 
207 	vap->va_fsid    = dev2udev(ip->i_mnt->im_dev);
208 	vap->va_fileid	= ip->i_number;
209 
210 	vap->va_mode	= ip->inode.iso_mode;
211 	vap->va_mode &= (vp->v_type == VDIR) ? ip->i_mnt->im_dmask : ip->i_mnt->im_fmask;
212 
213 	vap->va_nlink	= ip->inode.iso_links;
214 	vap->va_uid	= (ip->i_mnt->im_flags & ISOFSMNT_UID) ?
215 			ip->i_mnt->im_uid : ip->inode.iso_uid;
216 	vap->va_gid	= (ip->i_mnt->im_flags & ISOFSMNT_GID) ?
217 			ip->i_mnt->im_gid : ip->inode.iso_gid;
218 	vap->va_atime	= ip->inode.iso_atime;
219 	vap->va_mtime	= ip->inode.iso_mtime;
220 	vap->va_ctime	= ip->inode.iso_ctime;
221 	vap->va_rdev	= ip->inode.iso_rdev;
222 
223 	vap->va_size	= (u_quad_t) ip->i_size;
224 	if (ip->i_size == 0 && (vap->va_mode & S_IFMT) == S_IFLNK) {
225 		struct vop_readlink_args rdlnk;
226 		struct iovec aiov;
227 		struct uio auio;
228 		char *cp;
229 
230 		cp = malloc(MAXPATHLEN, M_TEMP, M_WAITOK);
231 		aiov.iov_base = cp;
232 		aiov.iov_len = MAXPATHLEN;
233 		auio.uio_iov = &aiov;
234 		auio.uio_iovcnt = 1;
235 		auio.uio_offset = 0;
236 		auio.uio_rw = UIO_READ;
237 		auio.uio_segflg = UIO_SYSSPACE;
238 		auio.uio_td = curthread;
239 		auio.uio_resid = MAXPATHLEN;
240 		rdlnk.a_uio = &auio;
241 		rdlnk.a_vp = ap->a_vp;
242 		rdlnk.a_cred = ap->a_cred;
243 		if (cd9660_readlink(&rdlnk) == 0)
244 			vap->va_size = MAXPATHLEN - auio.uio_resid;
245 		free(cp, M_TEMP);
246 	}
247 	vap->va_flags	= 0;
248 	vap->va_gen = 1;
249 	vap->va_blocksize = ip->i_mnt->logical_block_size;
250 	vap->va_bytes	= (u_quad_t) ip->i_size;
251 	vap->va_type	= vp->v_type;
252 	vap->va_filerev	= 0;
253 	return (0);
254 }
255 
256 /*
257  * Vnode op for ioctl.
258  */
259 static int
cd9660_ioctl(ap)260 cd9660_ioctl(ap)
261 	struct vop_ioctl_args /* {
262 		struct vnode *a_vp;
263 		u_long  a_command;
264 		caddr_t  a_data;
265 		int  a_fflag;
266 		struct ucred *a_cred;
267 		struct thread *a_td;
268 	} */ *ap;
269 {
270 	struct vnode *vp;
271 	struct iso_node *ip;
272 	int error;
273 
274 	vp = ap->a_vp;
275 	vn_lock(vp, LK_SHARED | LK_RETRY);
276 	if (VN_IS_DOOMED(vp)) {
277 		VOP_UNLOCK(vp);
278 		return (EBADF);
279 	}
280 	if (vp->v_type == VCHR || vp->v_type == VBLK) {
281 		VOP_UNLOCK(vp);
282 		return (EOPNOTSUPP);
283 	}
284 
285 	ip = VTOI(vp);
286 	error = 0;
287 
288 	switch (ap->a_command) {
289 	case FIOGETLBA:
290 		*(int *)(ap->a_data) = ip->iso_start;
291 		break;
292 	default:
293 		error = ENOTTY;
294 		break;
295 	}
296 
297 	VOP_UNLOCK(vp);
298 	return (error);
299 }
300 
301 /*
302  * Vnode op for reading.
303  */
304 static int
cd9660_read(ap)305 cd9660_read(ap)
306 	struct vop_read_args /* {
307 		struct vnode *a_vp;
308 		struct uio *a_uio;
309 		int a_ioflag;
310 		struct ucred *a_cred;
311 	} */ *ap;
312 {
313 	struct vnode *vp = ap->a_vp;
314 	struct uio *uio = ap->a_uio;
315 	struct iso_node *ip = VTOI(vp);
316 	struct iso_mnt *imp;
317 	struct buf *bp;
318 	daddr_t lbn, rablock;
319 	off_t diff;
320 	int rasize, error = 0;
321 	int seqcount;
322 	long size, n, on;
323 
324 	if (vp->v_type == VCHR || vp->v_type == VBLK)
325 		return (EOPNOTSUPP);
326 
327 	seqcount = ap->a_ioflag >> IO_SEQSHIFT;
328 
329 	if (uio->uio_resid == 0)
330 		return (0);
331 	if (uio->uio_offset < 0)
332 		return (EINVAL);
333 	imp = ip->i_mnt;
334 	do {
335 		lbn = lblkno(imp, uio->uio_offset);
336 		on = blkoff(imp, uio->uio_offset);
337 		n = MIN(imp->logical_block_size - on, uio->uio_resid);
338 		diff = (off_t)ip->i_size - uio->uio_offset;
339 		if (diff <= 0)
340 			return (0);
341 		if (diff < n)
342 			n = diff;
343 		size = blksize(imp, ip, lbn);
344 		rablock = lbn + 1;
345 		if ((vp->v_mount->mnt_flag & MNT_NOCLUSTERR) == 0) {
346 			if (lblktosize(imp, rablock) < ip->i_size)
347 				error = cluster_read(vp, (off_t)ip->i_size,
348 					 lbn, size, NOCRED, uio->uio_resid,
349 					 (ap->a_ioflag >> 16), 0, &bp);
350 			else
351 				error = bread(vp, lbn, size, NOCRED, &bp);
352 		} else {
353 			if (seqcount > 1 &&
354 			    lblktosize(imp, rablock) < ip->i_size) {
355 				rasize = blksize(imp, ip, rablock);
356 				error = breadn(vp, lbn, size, &rablock,
357 					       &rasize, 1, NOCRED, &bp);
358 			} else
359 				error = bread(vp, lbn, size, NOCRED, &bp);
360 		}
361 		if (error != 0)
362 			return (error);
363 		n = MIN(n, size - bp->b_resid);
364 
365 		error = uiomove(bp->b_data + on, (int)n, uio);
366 		brelse(bp);
367 	} while (error == 0 && uio->uio_resid > 0 && n != 0);
368 	return (error);
369 }
370 
371 /*
372  * Structure for reading directories
373  */
374 struct isoreaddir {
375 	struct dirent saveent;
376 	struct dirent assocent;
377 	struct dirent current;
378 	off_t saveoff;
379 	off_t assocoff;
380 	off_t curroff;
381 	struct uio *uio;
382 	off_t uio_off;
383 	int eofflag;
384 	u_long *cookies;
385 	int ncookies;
386 };
387 
388 static int
iso_uiodir(idp,dp,off)389 iso_uiodir(idp,dp,off)
390 	struct isoreaddir *idp;
391 	struct dirent *dp;
392 	off_t off;
393 {
394 	int error;
395 
396 	dp->d_reclen = GENERIC_DIRSIZ(dp);
397 	dirent_terminate(dp);
398 
399 	if (idp->uio->uio_resid < dp->d_reclen) {
400 		idp->eofflag = 0;
401 		return (-1);
402 	}
403 
404 	if (idp->cookies) {
405 		if (idp->ncookies <= 0) {
406 			idp->eofflag = 0;
407 			return (-1);
408 		}
409 
410 		*idp->cookies++ = off;
411 		--idp->ncookies;
412 	}
413 
414 	if ((error = uiomove(dp, dp->d_reclen, idp->uio)) != 0)
415 		return (error);
416 	idp->uio_off = off;
417 	return (0);
418 }
419 
420 static int
iso_shipdir(idp)421 iso_shipdir(idp)
422 	struct isoreaddir *idp;
423 {
424 	struct dirent *dp;
425 	int cl, sl, assoc;
426 	int error;
427 	char *cname, *sname;
428 
429 	cl = idp->current.d_namlen;
430 	cname = idp->current.d_name;
431 	assoc = (cl > 1) && (*cname == ASSOCCHAR);
432 	if (assoc) {
433 		cl--;
434 		cname++;
435 	}
436 
437 	dp = &idp->saveent;
438 	sname = dp->d_name;
439 	if (!(sl = dp->d_namlen)) {
440 		dp = &idp->assocent;
441 		sname = dp->d_name + 1;
442 		sl = dp->d_namlen - 1;
443 	}
444 	if (sl > 0) {
445 		if (sl != cl
446 		    || bcmp(sname,cname,sl)) {
447 			if (idp->assocent.d_namlen) {
448 				if ((error = iso_uiodir(idp,&idp->assocent,idp->assocoff)) != 0)
449 					return (error);
450 				idp->assocent.d_namlen = 0;
451 			}
452 			if (idp->saveent.d_namlen) {
453 				if ((error = iso_uiodir(idp,&idp->saveent,idp->saveoff)) != 0)
454 					return (error);
455 				idp->saveent.d_namlen = 0;
456 			}
457 		}
458 	}
459 	idp->current.d_reclen = GENERIC_DIRSIZ(&idp->current);
460 	if (assoc) {
461 		idp->assocoff = idp->curroff;
462 		memcpy(&idp->assocent, &idp->current, idp->current.d_reclen);
463 	} else {
464 		idp->saveoff = idp->curroff;
465 		memcpy(&idp->saveent, &idp->current, idp->current.d_reclen);
466 	}
467 	return (0);
468 }
469 
470 /*
471  * Vnode op for readdir
472  */
473 static int
cd9660_readdir(ap)474 cd9660_readdir(ap)
475 	struct vop_readdir_args /* {
476 		struct vnode *a_vp;
477 		struct uio *a_uio;
478 		struct ucred *a_cred;
479 		int *a_eofflag;
480 		int *a_ncookies;
481 		u_long **a_cookies;
482 	} */ *ap;
483 {
484 	struct uio *uio = ap->a_uio;
485 	struct isoreaddir *idp;
486 	struct vnode *vdp = ap->a_vp;
487 	struct iso_node *dp;
488 	struct iso_mnt *imp;
489 	struct buf *bp = NULL;
490 	struct iso_directory_record *ep;
491 	int entryoffsetinblock;
492 	doff_t endsearch;
493 	u_long bmask;
494 	int error = 0;
495 	int reclen;
496 	u_short namelen;
497 	u_int ncookies = 0;
498 	u_long *cookies = NULL;
499 	cd_ino_t ino;
500 
501 	dp = VTOI(vdp);
502 	imp = dp->i_mnt;
503 	bmask = imp->im_bmask;
504 
505 	idp = malloc(sizeof(*idp), M_TEMP, M_WAITOK);
506 	idp->saveent.d_namlen = idp->assocent.d_namlen = 0;
507 	/*
508 	 * XXX
509 	 * Is it worth trying to figure out the type?
510 	 */
511 	idp->saveent.d_type = idp->assocent.d_type = idp->current.d_type =
512 	    DT_UNKNOWN;
513 	idp->uio = uio;
514 	if (ap->a_ncookies == NULL) {
515 		idp->cookies = NULL;
516 	} else {
517 		/*
518 		 * Guess the number of cookies needed.
519 		 */
520 		ncookies = uio->uio_resid / 16;
521 		cookies = malloc(ncookies * sizeof(u_long),
522 		    M_TEMP, M_WAITOK);
523 		idp->cookies = cookies;
524 		idp->ncookies = ncookies;
525 	}
526 	idp->eofflag = 1;
527 	idp->curroff = uio->uio_offset;
528 	idp->uio_off = uio->uio_offset;
529 
530 	if ((entryoffsetinblock = idp->curroff & bmask) &&
531 	    (error = cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp))) {
532 		free(idp, M_TEMP);
533 		return (error);
534 	}
535 	endsearch = dp->i_size;
536 
537 	while (idp->curroff < endsearch) {
538 		/*
539 		 * If offset is on a block boundary,
540 		 * read the next directory block.
541 		 * Release previous if it exists.
542 		 */
543 		if ((idp->curroff & bmask) == 0) {
544 			if (bp != NULL)
545 				brelse(bp);
546 			if ((error =
547 			    cd9660_blkatoff(vdp, (off_t)idp->curroff, NULL, &bp)) != 0)
548 				break;
549 			entryoffsetinblock = 0;
550 		}
551 		/*
552 		 * Get pointer to next entry.
553 		 */
554 		ep = (struct iso_directory_record *)
555 			((char *)bp->b_data + entryoffsetinblock);
556 
557 		reclen = isonum_711(ep->length);
558 		if (reclen == 0) {
559 			/* skip to next block, if any */
560 			idp->curroff =
561 			    (idp->curroff & ~bmask) + imp->logical_block_size;
562 			continue;
563 		}
564 
565 		if (reclen < ISO_DIRECTORY_RECORD_SIZE) {
566 			error = EINVAL;
567 			/* illegal entry, stop */
568 			break;
569 		}
570 
571 		if (entryoffsetinblock + reclen > imp->logical_block_size) {
572 			error = EINVAL;
573 			/* illegal directory, so stop looking */
574 			break;
575 		}
576 
577 		idp->current.d_namlen = isonum_711(ep->name_len);
578 
579 		if (reclen < ISO_DIRECTORY_RECORD_SIZE + idp->current.d_namlen) {
580 			error = EINVAL;
581 			/* illegal entry, stop */
582 			break;
583 		}
584 
585 		if (isonum_711(ep->flags)&2)
586 			idp->current.d_fileno = isodirino(ep, imp);
587 		else
588 			idp->current.d_fileno = dbtob(bp->b_blkno) +
589 				entryoffsetinblock;
590 
591 		idp->curroff += reclen;
592 		/* NOTE: d_off is the offset of *next* entry. */
593 		idp->current.d_off = idp->curroff;
594 
595 		switch (imp->iso_ftype) {
596 		case ISO_FTYPE_RRIP:
597 			ino = idp->current.d_fileno;
598 			cd9660_rrip_getname(ep, idp->current.d_name, &namelen,
599 			    &ino, imp);
600 			idp->current.d_fileno = ino;
601 			idp->current.d_namlen = (u_char)namelen;
602 			if (idp->current.d_namlen)
603 				error = iso_uiodir(idp,&idp->current,idp->curroff);
604 			break;
605 		default: /* ISO_FTYPE_DEFAULT || ISO_FTYPE_9660 || ISO_FTYPE_HIGH_SIERRA*/
606 			strcpy(idp->current.d_name,"..");
607 			if (idp->current.d_namlen == 1 && ep->name[0] == 0) {
608 				idp->current.d_namlen = 1;
609 				error = iso_uiodir(idp,&idp->current,idp->curroff);
610 			} else if (idp->current.d_namlen == 1 && ep->name[0] == 1) {
611 				idp->current.d_namlen = 2;
612 				error = iso_uiodir(idp,&idp->current,idp->curroff);
613 			} else {
614 				isofntrans(ep->name,idp->current.d_namlen,
615 					   idp->current.d_name, &namelen,
616 					   imp->iso_ftype == ISO_FTYPE_9660,
617 					   isonum_711(ep->flags)&4,
618 					   imp->joliet_level,
619 					   imp->im_flags,
620 					   imp->im_d2l);
621 				idp->current.d_namlen = (u_char)namelen;
622 				if (imp->iso_ftype == ISO_FTYPE_DEFAULT)
623 					error = iso_shipdir(idp);
624 				else
625 					error = iso_uiodir(idp,&idp->current,idp->curroff);
626 			}
627 		}
628 		if (error)
629 			break;
630 
631 		entryoffsetinblock += reclen;
632 	}
633 
634 	if (!error && imp->iso_ftype == ISO_FTYPE_DEFAULT) {
635 		idp->current.d_namlen = 0;
636 		error = iso_shipdir(idp);
637 	}
638 	if (error < 0)
639 		error = 0;
640 
641 	if (ap->a_ncookies != NULL) {
642 		if (error)
643 			free(cookies, M_TEMP);
644 		else {
645 			/*
646 			 * Work out the number of cookies actually used.
647 			 */
648 			*ap->a_ncookies = ncookies - idp->ncookies;
649 			*ap->a_cookies = cookies;
650 		}
651 	}
652 
653 	if (bp)
654 		brelse (bp);
655 
656 	uio->uio_offset = idp->uio_off;
657 	*ap->a_eofflag = idp->eofflag;
658 
659 	free(idp, M_TEMP);
660 
661 	return (error);
662 }
663 
664 /*
665  * Return target name of a symbolic link
666  * Shouldn't we get the parent vnode and read the data from there?
667  * This could eventually result in deadlocks in cd9660_lookup.
668  * But otherwise the block read here is in the block buffer two times.
669  */
670 typedef struct iso_directory_record ISODIR;
671 typedef struct iso_node		    ISONODE;
672 typedef struct iso_mnt		    ISOMNT;
673 static int
cd9660_readlink(ap)674 cd9660_readlink(ap)
675 	struct vop_readlink_args /* {
676 		struct vnode *a_vp;
677 		struct uio *a_uio;
678 		struct ucred *a_cred;
679 	} */ *ap;
680 {
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 		return (EINVAL);
706 	}
707 
708 	/*
709 	 * Setup the directory pointer for this inode
710 	 */
711 	dirp = (ISODIR *)(bp->b_data + (ip->i_number & imp->im_bmask));
712 
713 	/*
714 	 * Just make sure, we have a right one....
715 	 *   1: Check not cross boundary on block
716 	 */
717 	if ((ip->i_number & imp->im_bmask) + isonum_711(dirp->length)
718 	    > (unsigned)imp->logical_block_size) {
719 		brelse(bp);
720 		return (EINVAL);
721 	}
722 
723 	/*
724 	 * Now get a buffer
725 	 * Abuse a namei buffer for now.
726 	 */
727 	if (uio->uio_segflg == UIO_SYSSPACE)
728 		symname = uio->uio_iov->iov_base;
729 	else
730 		symname = uma_zalloc(namei_zone, M_WAITOK);
731 
732 	/*
733 	 * Ok, we just gathering a symbolic name in SL record.
734 	 */
735 	if (cd9660_rrip_getsymname(dirp, symname, &symlen, imp) == 0) {
736 		if (uio->uio_segflg != UIO_SYSSPACE)
737 			uma_zfree(namei_zone, symname);
738 		brelse(bp);
739 		return (EINVAL);
740 	}
741 	/*
742 	 * Don't forget before you leave from home ;-)
743 	 */
744 	brelse(bp);
745 
746 	/*
747 	 * return with the symbolic name to caller's.
748 	 */
749 	if (uio->uio_segflg != UIO_SYSSPACE) {
750 		error = uiomove(symname, symlen, uio);
751 		uma_zfree(namei_zone, symname);
752 		return (error);
753 	}
754 	uio->uio_resid -= symlen;
755 	uio->uio_iov->iov_base = (char *)uio->uio_iov->iov_base + symlen;
756 	uio->uio_iov->iov_len -= symlen;
757 	return (0);
758 }
759 
760 /*
761  * Calculate the logical to physical mapping if not done already,
762  * then call the device strategy routine.
763  */
764 static int
cd9660_strategy(ap)765 cd9660_strategy(ap)
766 	struct vop_strategy_args /* {
767 		struct buf *a_vp;
768 		struct buf *a_bp;
769 	} */ *ap;
770 {
771 	struct buf *bp = ap->a_bp;
772 	struct vnode *vp = ap->a_vp;
773 	struct iso_node *ip;
774 	struct bufobj *bo;
775 
776 	ip = VTOI(vp);
777 	if (vp->v_type == VBLK || vp->v_type == VCHR)
778 		panic("cd9660_strategy: spec");
779 	if (bp->b_blkno == bp->b_lblkno) {
780 		bp->b_blkno = (ip->iso_start + bp->b_lblkno) <<
781 		    (ip->i_mnt->im_bshift - DEV_BSHIFT);
782 	}
783 	bp->b_iooffset = dbtob(bp->b_blkno);
784 	bo = ip->i_mnt->im_bo;
785 	BO_STRATEGY(bo, bp);
786 	return (0);
787 }
788 
789 /*
790  * Return POSIX pathconf information applicable to cd9660 filesystems.
791  */
792 static int
cd9660_pathconf(ap)793 cd9660_pathconf(ap)
794 	struct vop_pathconf_args /* {
795 		struct vnode *a_vp;
796 		int a_name;
797 		register_t *a_retval;
798 	} */ *ap;
799 {
800 
801 	switch (ap->a_name) {
802 	case _PC_FILESIZEBITS:
803 		*ap->a_retval = 32;
804 		return (0);
805 	case _PC_LINK_MAX:
806 		*ap->a_retval = 1;
807 		return (0);
808 	case _PC_NAME_MAX:
809 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP)
810 			*ap->a_retval = NAME_MAX;
811 		else
812 			*ap->a_retval = 37;
813 		return (0);
814 	case _PC_SYMLINK_MAX:
815 		if (VTOI(ap->a_vp)->i_mnt->iso_ftype == ISO_FTYPE_RRIP) {
816 			*ap->a_retval = MAXPATHLEN;
817 			return (0);
818 		}
819 		return (EINVAL);
820 	case _PC_NO_TRUNC:
821 		*ap->a_retval = 1;
822 		return (0);
823 	default:
824 		return (vop_stdpathconf(ap));
825 	}
826 	/* NOTREACHED */
827 }
828 
829 _Static_assert(sizeof(struct ifid) <= sizeof(struct fid),
830     "struct ifid must be no larger than struct fid");
831 
832 /*
833  * Vnode pointer to File handle
834  */
835 static int
cd9660_vptofh(ap)836 cd9660_vptofh(ap)
837 	struct vop_vptofh_args /* {
838 		struct vnode *a_vp;
839 		struct fid *a_fhp;
840 	} */ *ap;
841 {
842 	struct ifid ifh;
843 	struct iso_node *ip = VTOI(ap->a_vp);
844 
845 	ifh.ifid_len = sizeof(struct ifid);
846 
847 	ifh.ifid_ino = ip->i_number;
848 	ifh.ifid_start = ip->iso_start;
849 	/*
850 	 * This intentionally uses sizeof(ifh) in order to not copy stack
851 	 * garbage on ILP32.
852 	 */
853 	memcpy(ap->a_fhp, &ifh, sizeof(ifh));
854 
855 #ifdef	ISOFS_DBG
856 	printf("vptofh: ino %jd, start %ld\n",
857 	    (uintmax_t)ifh.ifid_ino, ifh.ifid_start);
858 #endif
859 
860 	return (0);
861 }
862 
863 SYSCTL_NODE(_vfs, OID_AUTO, cd9660, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
864     "cd9660 filesystem");
865 static int use_buf_pager = 1;
866 SYSCTL_INT(_vfs_cd9660, OID_AUTO, use_buf_pager, CTLFLAG_RWTUN,
867     &use_buf_pager, 0,
868     "Use buffer pager instead of bmap");
869 
870 static daddr_t
cd9660_gbp_getblkno(struct vnode * vp,vm_ooffset_t off)871 cd9660_gbp_getblkno(struct vnode *vp, vm_ooffset_t off)
872 {
873 
874 	return (lblkno(VTOI(vp)->i_mnt, off));
875 }
876 
877 static int
cd9660_gbp_getblksz(struct vnode * vp,daddr_t lbn,long * sz)878 cd9660_gbp_getblksz(struct vnode *vp, daddr_t lbn, long *sz)
879 {
880 	struct iso_node *ip;
881 
882 	ip = VTOI(vp);
883 	*sz = blksize(ip->i_mnt, ip, lbn);
884 	return (0);
885 }
886 
887 static int
cd9660_getpages(struct vop_getpages_args * ap)888 cd9660_getpages(struct vop_getpages_args *ap)
889 {
890 	struct vnode *vp;
891 
892 	vp = ap->a_vp;
893 	if (vp->v_type == VCHR || vp->v_type == VBLK)
894 		return (EOPNOTSUPP);
895 
896 	if (use_buf_pager)
897 		return (vfs_bio_getpages(vp, ap->a_m, ap->a_count,
898 		    ap->a_rbehind, ap->a_rahead, cd9660_gbp_getblkno,
899 		    cd9660_gbp_getblksz));
900 	return (vnode_pager_generic_getpages(vp, ap->a_m, ap->a_count,
901 	    ap->a_rbehind, ap->a_rahead, NULL, NULL));
902 }
903 
904 /*
905  * Global vfs data structures for cd9660
906  */
907 struct vop_vector cd9660_vnodeops = {
908 	.vop_default =		&default_vnodeops,
909 	.vop_open =		cd9660_open,
910 	.vop_access =		cd9660_access,
911 	.vop_bmap =		cd9660_bmap,
912 	.vop_cachedlookup =	cd9660_lookup,
913 	.vop_getattr =		cd9660_getattr,
914 	.vop_inactive =		cd9660_inactive,
915 	.vop_ioctl =		cd9660_ioctl,
916 	.vop_lookup =		vfs_cache_lookup,
917 	.vop_pathconf =		cd9660_pathconf,
918 	.vop_read =		cd9660_read,
919 	.vop_readdir =		cd9660_readdir,
920 	.vop_readlink =		cd9660_readlink,
921 	.vop_reclaim =		cd9660_reclaim,
922 	.vop_setattr =		cd9660_setattr,
923 	.vop_strategy =		cd9660_strategy,
924 	.vop_vptofh =		cd9660_vptofh,
925 	.vop_getpages =		cd9660_getpages,
926 };
927 VFS_VOP_VECTOR_REGISTER(cd9660_vnodeops);
928 
929 /*
930  * Special device vnode ops
931  */
932 
933 struct vop_vector cd9660_fifoops = {
934 	.vop_default =		&fifo_specops,
935 	.vop_access =		cd9660_access,
936 	.vop_getattr =		cd9660_getattr,
937 	.vop_inactive =		cd9660_inactive,
938 	.vop_reclaim =		cd9660_reclaim,
939 	.vop_setattr =		cd9660_setattr,
940 	.vop_vptofh =		cd9660_vptofh,
941 };
942 VFS_VOP_VECTOR_REGISTER(cd9660_fifoops);
943