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