xref: /netbsd-src/sys/fs/cd9660/cd9660_vfsops.c (revision f82d7874c259b2a6cc59b714f844919f32bf7b51)
1 /*	$NetBSD: cd9660_vfsops.c,v 1.62 2008/05/16 09:21:59 hannken Exp $	*/
2 
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_vfsops.c	8.18 (Berkeley) 5/22/95
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_vfsops.c,v 1.62 2008/05/16 09:21:59 hannken Exp $");
41 
42 #if defined(_KERNEL_OPT)
43 #include "opt_compat_netbsd.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <miscfs/genfs/genfs.h>
54 #include <miscfs/specfs/specdev.h>
55 #include <sys/mount.h>
56 #include <sys/buf.h>
57 #include <sys/file.h>
58 #include <sys/disklabel.h>
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #include <sys/cdio.h>
62 #include <sys/errno.h>
63 #include <sys/malloc.h>
64 #include <sys/pool.h>
65 #include <sys/stat.h>
66 #include <sys/conf.h>
67 #include <sys/dirent.h>
68 #include <sys/kauth.h>
69 #include <sys/module.h>
70 
71 #include <fs/cd9660/iso.h>
72 #include <fs/cd9660/cd9660_extern.h>
73 #include <fs/cd9660/iso_rrip.h>
74 #include <fs/cd9660/cd9660_node.h>
75 #include <fs/cd9660/cd9660_mount.h>
76 
77 MODULE(MODULE_CLASS_VFS, cd9660, NULL);
78 
79 MALLOC_JUSTDEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
80 
81 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc;
82 extern const struct vnodeopv_desc cd9660_specop_opv_desc;
83 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc;
84 
85 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = {
86 	&cd9660_vnodeop_opv_desc,
87 	&cd9660_specop_opv_desc,
88 	&cd9660_fifoop_opv_desc,
89 	NULL,
90 };
91 
92 struct vfsops cd9660_vfsops = {
93 	MOUNT_CD9660,
94 	sizeof (struct iso_args),
95 	cd9660_mount,
96 	cd9660_start,
97 	cd9660_unmount,
98 	cd9660_root,
99 	(void *)eopnotsupp,
100 	cd9660_statvfs,
101 	cd9660_sync,
102 	cd9660_vget,
103 	cd9660_fhtovp,
104 	cd9660_vptofh,
105 	cd9660_init,
106 	cd9660_reinit,
107 	cd9660_done,
108 	cd9660_mountroot,
109 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
110 	vfs_stdextattrctl,
111 	(void *)eopnotsupp,		/* vfs_suspendctl */
112 	genfs_renamelock_enter,
113 	genfs_renamelock_exit,
114 	(void *)eopnotsupp,
115 	cd9660_vnodeopv_descs,
116 	0,	/* refcount */
117 	{ NULL, NULL } /* list */
118 };
119 
120 static const struct genfs_ops cd9660_genfsops = {
121 	.gop_size = genfs_size,
122 };
123 
124 /*
125  * Called by vfs_mountroot when iso is going to be mounted as root.
126  *
127  * Name is updated by mount(8) after booting.
128  */
129 #define ROOTNAME	"root_device"
130 
131 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len);
132 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
133 		struct lwp *l, struct iso_args *argp);
134 
135 static int
136 cd9660_modcmd(modcmd_t cmd, void *arg)
137 {
138 
139 	switch (cmd) {
140 	case MODULE_CMD_INIT:
141 		return vfs_attach(&cd9660_vfsops);
142 	case MODULE_CMD_FINI:
143 		return vfs_detach(&cd9660_vfsops);
144 	default:
145 		return ENOTTY;
146 	}
147 }
148 
149 int
150 cd9660_mountroot(void)
151 {
152 	struct mount *mp;
153 	struct lwp *l = curlwp;
154 	int error;
155 	struct iso_args args;
156 
157 	if (device_class(root_device) != DV_DISK)
158 		return (ENODEV);
159 
160 	if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
161 			!= 0) {
162 		vrele(rootvp);
163 		return (error);
164 	}
165 
166 	args.flags = ISOFSMNT_ROOT;
167 	if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) {
168 		vfs_unbusy(mp, false, NULL);
169 		vfs_destroy(mp);
170 		return (error);
171 	}
172 	mutex_enter(&mountlist_lock);
173 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
174 	mutex_exit(&mountlist_lock);
175 	(void)cd9660_statvfs(mp, &mp->mnt_stat);
176 	vfs_unbusy(mp, false, NULL);
177 	return (0);
178 }
179 
180 /*
181  * VFS Operations.
182  *
183  * mount system call
184  */
185 int
186 cd9660_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
187 {
188 	struct lwp *l = curlwp;
189 	struct nameidata nd;
190 	struct vnode *devvp;
191 	struct iso_args *args = data;
192 	int error;
193 	struct iso_mnt *imp = VFSTOISOFS(mp);
194 
195 	if (*data_len < sizeof *args)
196 		return EINVAL;
197 
198 	if (mp->mnt_flag & MNT_GETARGS) {
199 		if (imp == NULL)
200 			return EIO;
201 		args->fspec = NULL;
202 		args->flags = imp->im_flags;
203 		*data_len = sizeof (*args);
204 		return 0;
205 	}
206 
207 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
208 		return (EROFS);
209 
210 	if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
211 		return EINVAL;
212 
213 	/*
214 	 * Not an update, or updating the name: look up the name
215 	 * and verify that it refers to a sensible block device.
216 	 */
217 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, args->fspec);
218 	if ((error = namei(&nd)) != 0)
219 		return (error);
220 	devvp = nd.ni_vp;
221 
222 	if (devvp->v_type != VBLK) {
223 		vrele(devvp);
224 		return ENOTBLK;
225 	}
226 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
227 		vrele(devvp);
228 		return ENXIO;
229 	}
230 	/*
231 	 * If mount by non-root, then verify that user has necessary
232 	 * permissions on the device.
233 	 */
234 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
235 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
236 		error = VOP_ACCESS(devvp, VREAD, l->l_cred);
237 		VOP_UNLOCK(devvp, 0);
238 		if (error) {
239 			vrele(devvp);
240 			return (error);
241 		}
242 	}
243 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
244 		error = VOP_OPEN(devvp, FREAD, FSCRED);
245 		if (error)
246 			goto fail;
247 		error = iso_mountfs(devvp, mp, l, args);
248 		if (error) {
249 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
250 			(void)VOP_CLOSE(devvp, FREAD, NOCRED);
251 			VOP_UNLOCK(devvp, 0);
252 			goto fail;
253 		}
254 	} else {
255 		vrele(devvp);
256 		if (devvp != imp->im_devvp)
257 			return (EINVAL);	/* needs translation */
258 	}
259 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
260 	    mp->mnt_op->vfs_name, mp, l);
261 
262 fail:
263 	vrele(devvp);
264 	return (error);
265 }
266 
267 /*
268  * Make a mount point from a volume descriptor
269  */
270 static int
271 iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len)
272 {
273 	struct iso_primary_descriptor *pri;
274 	int logical_block_size;
275 	struct iso_directory_record *rootp;
276 
277 	pri = (struct iso_primary_descriptor *)bp->b_data;
278 
279 	logical_block_size = isonum_723 (pri->logical_block_size);
280 
281 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
282 	    || (logical_block_size & (logical_block_size - 1)) != 0)
283 		return -1;
284 
285 	rootp = (struct iso_directory_record *)pri->root_directory_record;
286 
287 	isomp->logical_block_size = logical_block_size;
288 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
289 	memcpy(isomp->root, rootp, sizeof(isomp->root));
290 	isomp->root_extent = isonum_733 (rootp->extent);
291 	isomp->root_size = isonum_733 (rootp->size);
292 	isomp->im_joliet_level = 0;
293 
294 	isomp->im_bmask = logical_block_size - 1;
295 	isomp->im_bshift = 0;
296 	while ((1 << isomp->im_bshift) < isomp->logical_block_size)
297 		isomp->im_bshift++;
298 
299 	if (ea_len != NULL)
300 		*ea_len = isonum_711(rootp->ext_attr_length);
301 
302 	return 0;
303 }
304 
305 /*
306  * Common code for mount and mountroot
307  */
308 static int
309 iso_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l,
310 	struct iso_args *argp)
311 {
312 	struct iso_mnt *isomp = (struct iso_mnt *)0;
313 	struct buf *bp = NULL, *pribp = NULL, *supbp = NULL;
314 	dev_t dev = devvp->v_rdev;
315 	int error = EINVAL;
316 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
317 	int iso_bsize;
318 	int iso_blknum;
319 	int joliet_level;
320 	struct iso_volume_descriptor *vdp;
321 	struct iso_supplementary_descriptor *sup;
322 	int sess = 0;
323 	int ext_attr_length;
324 	struct disklabel label;
325 
326 	if (!ronly)
327 		return EROFS;
328 
329 	/* Flush out any old buffers remaining from a previous use. */
330 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
331 		return (error);
332 
333 	/* This is the "logical sector size".  The standard says this
334 	 * should be 2048 or the physical sector size on the device,
335 	 * whichever is greater.  For now, we'll just use a constant.
336 	 */
337 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
338 
339 	error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED);
340 	if (!error &&
341 	    label.d_partitions[DISKPART(dev)].p_fstype == FS_ISO9660) {
342 		/* XXX more sanity checks? */
343 		sess = label.d_partitions[DISKPART(dev)].p_cdsession;
344 	} else {
345 		/* fallback to old method */
346 		error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED);
347 		if (error)
348 			sess = 0;	/* never mind */
349 	}
350 #ifdef ISO_DEBUG
351 	printf("isofs: session offset (part %d) %d\n", DISKPART(dev), sess);
352 #endif
353 
354 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
355 		if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize),
356 				   iso_bsize, NOCRED, 0, &bp)) != 0)
357 			goto out;
358 
359 		vdp = (struct iso_volume_descriptor *)bp->b_data;
360 		if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) {
361 			error = EINVAL;
362 			goto out;
363 		}
364 
365 		switch (isonum_711(vdp->type)) {
366 		case ISO_VD_PRIMARY:
367 			if (pribp == NULL) {
368 				pribp = bp;
369 				bp = NULL;
370 			}
371 			break;
372 
373 		case ISO_VD_SUPPLEMENTARY:
374 			if (supbp == NULL) {
375 				supbp = bp;
376 				bp = NULL;
377 			}
378 			break;
379 
380 		default:
381 			break;
382 		}
383 
384 		if (isonum_711 (vdp->type) == ISO_VD_END) {
385 			brelse(bp, 0);
386 			bp = NULL;
387 			break;
388 		}
389 
390 		if (bp != NULL) {
391 			brelse(bp, 0);
392 			bp = NULL;
393 		}
394 	}
395 
396 	if (pribp == NULL) {
397 		error = EINVAL;
398 		goto out;
399 	}
400 
401 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
402 	memset(isomp, 0, sizeof *isomp);
403 	if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) {
404 		error = EINVAL;
405 		goto out;
406 	}
407 
408 	isomp->volume_space_size += sess;
409 
410 	brelse(pribp, BC_AGE);
411 	pribp = NULL;
412 
413 	mp->mnt_data = isomp;
414 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
415 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
416 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
417 	mp->mnt_stat.f_namemax = MAXNAMLEN;
418 	mp->mnt_flag |= MNT_LOCAL;
419 	mp->mnt_iflag |= IMNT_MPSAFE;
420 	mp->mnt_dev_bshift = iso_bsize;
421 	mp->mnt_fs_bshift = isomp->im_bshift;
422 	isomp->im_mountp = mp;
423 	isomp->im_dev = dev;
424 	isomp->im_devvp = devvp;
425 
426 	/* Check the Rock Ridge Extension support */
427 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
428 		struct iso_directory_record *rootp;
429 
430 		if ((error = bread(isomp->im_devvp,
431 				   (isomp->root_extent + ext_attr_length) <<
432 				   (isomp->im_bshift - DEV_BSHIFT),
433 				   isomp->logical_block_size, NOCRED,
434 				   0, &bp)) != 0)
435 		    goto out;
436 
437 		rootp = (struct iso_directory_record *)bp->b_data;
438 
439 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
440 		    argp->flags  |= ISOFSMNT_NORRIP;
441 		} else {
442 		    argp->flags  &= ~ISOFSMNT_GENS;
443 		}
444 
445 		/*
446 		 * The contents are valid,
447 		 * but they will get reread as part of another vnode, so...
448 		 */
449 		brelse(bp, BC_AGE);
450 		bp = NULL;
451 	}
452 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
453 		 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
454 
455 	if (isomp->im_flags & ISOFSMNT_GENS)
456 		isomp->iso_ftype = ISO_FTYPE_9660;
457 	else if (isomp->im_flags & ISOFSMNT_NORRIP) {
458 		isomp->iso_ftype = ISO_FTYPE_DEFAULT;
459 		if (argp->flags & ISOFSMNT_NOCASETRANS)
460 			isomp->im_flags |= ISOFSMNT_NOCASETRANS;
461 	} else
462 		isomp->iso_ftype = ISO_FTYPE_RRIP;
463 
464 	/* Check the Joliet Extension support */
465 	if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
466 	    (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
467 	    supbp != NULL) {
468 		joliet_level = 0;
469 		sup = (struct iso_supplementary_descriptor *)supbp->b_data;
470 
471 		if ((isonum_711(sup->flags) & 1) == 0) {
472 			if (memcmp(sup->escape, "%/@", 3) == 0)
473 				joliet_level = 1;
474 			if (memcmp(sup->escape, "%/C", 3) == 0)
475 				joliet_level = 2;
476 			if (memcmp(sup->escape, "%/E", 3) == 0)
477 				joliet_level = 3;
478 		}
479 		if (joliet_level != 0) {
480 			if (iso_makemp(isomp, supbp, NULL) == -1) {
481 				error = EINVAL;
482 				goto out;
483 			}
484 			isomp->im_joliet_level = joliet_level;
485 		}
486 	}
487 
488 	if (supbp != NULL) {
489 		brelse(supbp, 0);
490 		supbp = NULL;
491 	}
492 
493 	devvp->v_specmountpoint = mp;
494 
495 	return 0;
496 out:
497 	if (bp)
498 		brelse(bp, 0);
499 	if (pribp)
500 		brelse(pribp, 0);
501 	if (supbp)
502 		brelse(supbp, 0);
503 	if (isomp) {
504 		free(isomp, M_ISOFSMNT);
505 		mp->mnt_data = NULL;
506 	}
507 	return error;
508 }
509 
510 /*
511  * Make a filesystem operational.
512  * Nothing to do at the moment.
513  */
514 /* ARGSUSED */
515 int
516 cd9660_start(struct mount *mp, int flags)
517 {
518 	return 0;
519 }
520 
521 /*
522  * unmount system call
523  */
524 int
525 cd9660_unmount(struct mount *mp, int mntflags)
526 {
527 	struct iso_mnt *isomp;
528 	int error, flags = 0;
529 
530 	if (mntflags & MNT_FORCE)
531 		flags |= FORCECLOSE;
532 	if ((error = vflush(mp, NULLVP, flags)) != 0)
533 		return (error);
534 
535 	isomp = VFSTOISOFS(mp);
536 
537 	if (isomp->im_devvp->v_type != VBAD)
538 		isomp->im_devvp->v_specmountpoint = NULL;
539 
540 	vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
541 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED);
542 	vput(isomp->im_devvp);
543 	free(isomp, M_ISOFSMNT);
544 	mp->mnt_data = NULL;
545 	mp->mnt_flag &= ~MNT_LOCAL;
546 	return (error);
547 }
548 
549 /*
550  * Return root of a filesystem
551  */
552 int
553 cd9660_root(struct mount *mp, struct vnode **vpp)
554 {
555 	struct iso_mnt *imp = VFSTOISOFS(mp);
556 	struct iso_directory_record *dp =
557 	    (struct iso_directory_record *)imp->root;
558 	ino_t ino = isodirino(dp, imp);
559 
560 	/*
561 	 * With RRIP we must use the `.' entry of the root directory.
562 	 * Simply tell vget, that it's a relocated directory.
563 	 */
564 	return (cd9660_vget_internal(mp, ino, vpp,
565 				     imp->iso_ftype == ISO_FTYPE_RRIP, dp));
566 }
567 
568 /*
569  * Get file system statistics.
570  */
571 int
572 cd9660_statvfs(struct mount *mp, struct statvfs *sbp)
573 {
574 	struct iso_mnt *isomp;
575 
576 	isomp = VFSTOISOFS(mp);
577 
578 	sbp->f_bsize = isomp->logical_block_size;
579 	sbp->f_frsize = sbp->f_bsize;
580 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
581 	sbp->f_blocks = isomp->volume_space_size;
582 	sbp->f_bfree = 0; /* total free blocks */
583 	sbp->f_bavail = 0; /* blocks free for non superuser */
584 	sbp->f_bresvd = 0; /* total reserved blocks */
585 	sbp->f_files =  0; /* total files */
586 	sbp->f_ffree = 0; /* free file nodes */
587 	sbp->f_favail = 0; /* free file nodes for non superuser */
588 	sbp->f_fresvd = 0; /* reserved file nodes */
589 	copy_statvfs_info(sbp, mp);
590 	/* Use the first spare for flags: */
591 	sbp->f_spare[0] = isomp->im_flags;
592 	return 0;
593 }
594 
595 /* ARGSUSED */
596 int
597 cd9660_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
598 {
599 	return 0;
600 }
601 
602 /*
603  * File handle to vnode
604  *
605  * Have to be really careful about stale file handles:
606  * - check that the inode number is in range
607  * - call iget() to get the locked inode
608  * - check for an unallocated inode (i_mode == 0)
609  * - check that the generation number matches
610  */
611 
612 struct ifid {
613 	ushort	ifid_len;
614 	ushort	ifid_pad;
615 	int	ifid_ino;
616 	long	ifid_start;
617 };
618 
619 /* ARGSUSED */
620 int
621 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
622 {
623 	struct ifid ifh;
624 	struct iso_node *ip;
625 	struct vnode *nvp;
626 	int error;
627 
628 	if (fhp->fid_len != sizeof(ifh))
629 		return EINVAL;
630 
631 	memcpy(&ifh, fhp, sizeof(ifh));
632 #ifdef	ISOFS_DBG
633 	printf("fhtovp: ino %d, start %ld\n",
634 	    ifh.ifid_ino, ifh.ifid_start);
635 #endif
636 
637 	if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
638 		*vpp = NULLVP;
639 		return (error);
640 	}
641 	ip = VTOI(nvp);
642 	if (ip->inode.iso_mode == 0) {
643 		vput(nvp);
644 		*vpp = NULLVP;
645 		return (ESTALE);
646 	}
647 	*vpp = nvp;
648 	return (0);
649 }
650 
651 int
652 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
653 {
654 
655 	/*
656 	 * XXXX
657 	 * It would be nice if we didn't always set the `relocated' flag
658 	 * and force the extra read, but I don't want to think about fixing
659 	 * that right now.
660 	 */
661 	return (cd9660_vget_internal(mp, ino, vpp,
662 #if 0
663 				     VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
664 #else
665 				     0,
666 #endif
667 				     NULL));
668 }
669 
670 int
671 cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
672 	int relocated, struct iso_directory_record *isodir)
673 {
674 	struct iso_mnt *imp;
675 	struct iso_node *ip;
676 	struct buf *bp;
677 	struct vnode *vp;
678 	dev_t dev;
679 	int error;
680 
681 	imp = VFSTOISOFS(mp);
682 	dev = imp->im_dev;
683 
684  retry:
685 	if ((*vpp = cd9660_ihashget(dev, ino, LK_EXCLUSIVE)) != NULLVP)
686 		return (0);
687 
688 	/* Allocate a new vnode/iso_node. */
689 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
690 		*vpp = NULLVP;
691 		return (error);
692 	}
693 	ip = pool_get(&cd9660_node_pool, PR_WAITOK);
694 
695 	/*
696 	 * If someone beat us to it, put back the freshly allocated
697 	 * vnode/inode pair and retry.
698 	 */
699 	mutex_enter(&cd9660_hashlock);
700 	if (cd9660_ihashget(dev, ino, 0) != NULL) {
701 		mutex_exit(&cd9660_hashlock);
702 		ungetnewvnode(vp);
703 		pool_put(&cd9660_node_pool, ip);
704 		goto retry;
705 	}
706 
707 	memset(ip, 0, sizeof(struct iso_node));
708 	vp->v_data = ip;
709 	ip->i_vnode = vp;
710 	ip->i_dev = dev;
711 	ip->i_number = ino;
712 	ip->i_mnt = imp;
713 	ip->i_devvp = imp->im_devvp;
714 	genfs_node_init(vp, &cd9660_genfsops);
715 
716 	/*
717 	 * Put it onto its hash chain and lock it so that other requests for
718 	 * this inode will block if they arrive while we are sleeping waiting
719 	 * for old data structures to be purged or for the contents of the
720 	 * disk portion of this inode to be read.
721 	 */
722 	cd9660_ihashins(ip);
723 	mutex_exit(&cd9660_hashlock);
724 
725 	if (isodir == 0) {
726 		int lbn, off;
727 
728 		lbn = lblkno(imp, ino);
729 		if (lbn >= imp->volume_space_size) {
730 			vput(vp);
731 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
732 			return (ESTALE);
733 		}
734 
735 		off = blkoff(imp, ino);
736 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
737 			vput(vp);
738 			printf("fhtovp: crosses block boundary %d\n",
739 			    off + ISO_DIRECTORY_RECORD_SIZE);
740 			return (ESTALE);
741 		}
742 
743 		error = bread(imp->im_devvp,
744 			      lbn << (imp->im_bshift - DEV_BSHIFT),
745 			      imp->logical_block_size, NOCRED, 0, &bp);
746 		if (error) {
747 			vput(vp);
748 			brelse(bp, 0);
749 			printf("fhtovp: bread error %d\n",error);
750 			return (error);
751 		}
752 		isodir = (struct iso_directory_record *)((char *)bp->b_data + off);
753 
754 		if (off + isonum_711(isodir->length) >
755 		    imp->logical_block_size) {
756 			vput(vp);
757 			if (bp != 0)
758 				brelse(bp, 0);
759 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
760 			    off +isonum_711(isodir->length), off,
761 			    isonum_711(isodir->length));
762 			return (ESTALE);
763 		}
764 
765 #if 0
766 		if (isonum_733(isodir->extent) +
767 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
768 			if (bp != 0)
769 				brelse(bp, 0);
770 			printf("fhtovp: file start miss %d vs %d\n",
771 			    isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
772 			    ifhp->ifid_start);
773 			return (ESTALE);
774 		}
775 #endif
776 	} else
777 		bp = 0;
778 
779 	VREF(ip->i_devvp);
780 
781 	if (relocated) {
782 		/*
783 		 * On relocated directories we must
784 		 * read the `.' entry out of a dir.
785 		 */
786 		ip->iso_start = ino >> imp->im_bshift;
787 		if (bp != 0)
788 			brelse(bp, 0);
789 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
790 			vput(vp);
791 			return (error);
792 		}
793 		isodir = (struct iso_directory_record *)bp->b_data;
794 	}
795 
796 	ip->iso_extent = isonum_733(isodir->extent);
797 	ip->i_size = isonum_733(isodir->size);
798 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
799 
800 	/*
801 	 * Setup time stamp, attribute
802 	 */
803 	vp->v_type = VNON;
804 	switch (imp->iso_ftype) {
805 	default:	/* ISO_FTYPE_9660 */
806 	    {
807 		struct buf *bp2;
808 		int off;
809 		if ((imp->im_flags & ISOFSMNT_EXTATT)
810 		    && (off = isonum_711(isodir->ext_attr_length)))
811 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
812 			    NULL, &bp2);
813 		else
814 			bp2 = NULL;
815 		cd9660_defattr(isodir, ip, bp2);
816 		cd9660_deftstamp(isodir, ip, bp2);
817 		if (bp2)
818 			brelse(bp2, 0);
819 		break;
820 	    }
821 	case ISO_FTYPE_RRIP:
822 		cd9660_rrip_analyze(isodir, ip, imp);
823 		break;
824 	}
825 
826 	if (bp != 0)
827 		brelse(bp, 0);
828 
829 	/*
830 	 * Initialize the associated vnode
831 	 */
832 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
833 	case VFIFO:
834 		vp->v_op = cd9660_fifoop_p;
835 		break;
836 	case VCHR:
837 	case VBLK:
838 		/*
839 		 * if device, look at device number table for translation
840 		 */
841 		vp->v_op = cd9660_specop_p;
842 		spec_node_init(vp, ip->inode.iso_rdev);
843 		break;
844 	case VLNK:
845 	case VNON:
846 	case VSOCK:
847 	case VDIR:
848 	case VBAD:
849 		break;
850 	case VREG:
851 		uvm_vnp_setsize(vp, ip->i_size);
852 		break;
853 	}
854 
855 	if (vp->v_type != VREG)
856 		uvm_vnp_setsize(vp, 0);
857 
858 	if (ip->iso_extent == imp->root_extent)
859 		vp->v_vflag |= VV_ROOT;
860 
861 	/*
862 	 * XXX need generation number?
863 	 */
864 
865 	*vpp = vp;
866 	return (0);
867 }
868 
869 /*
870  * Vnode pointer to File handle
871  */
872 /* ARGSUSED */
873 int
874 cd9660_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
875 {
876 	struct iso_node *ip = VTOI(vp);
877 	struct ifid ifh;
878 
879 	if (*fh_size < sizeof(struct ifid)) {
880 		*fh_size = sizeof(struct ifid);
881 		return E2BIG;
882 	}
883 	*fh_size = sizeof(struct ifid);
884 
885 	memset(&ifh, 0, sizeof(ifh));
886 	ifh.ifid_len = sizeof(struct ifid);
887 	ifh.ifid_ino = ip->i_number;
888 	ifh.ifid_start = ip->iso_start;
889 	memcpy(fhp, &ifh, sizeof(ifh));
890 
891 #ifdef	ISOFS_DBG
892 	printf("vptofh: ino %d, start %ld\n",
893 	    ifh.ifid_ino,ifh.ifid_start);
894 #endif
895 	return 0;
896 }
897 
898 SYSCTL_SETUP(sysctl_vfs_cd9660_setup, "sysctl vfs.cd9660 subtree setup")
899 {
900 
901 	sysctl_createv(clog, 0, NULL, NULL,
902 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
903 		       NULL, 0, NULL, 0,
904 		       CTL_VFS, CTL_EOL);
905 	sysctl_createv(clog, 0, NULL, NULL,
906 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
907 		       SYSCTL_DESCR("ISO-9660 file system"),
908 		       NULL, 0, NULL, 0,
909 		       CTL_VFS, 14, CTL_EOL);
910 
911 	sysctl_createv(clog, 0, NULL, NULL,
912 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
913 		       CTLTYPE_INT, "utf8_joliet",
914 		       SYSCTL_DESCR("Encode Joliet file names to UTF-8"),
915 		       NULL, 0, &cd9660_utf8_joliet, 0,
916 		       CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
917 
918 }
919