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