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