xref: /netbsd-src/sys/fs/cd9660/cd9660_vfsops.c (revision fad4c9f71477ae11cea2ee75ec82151ac770a534)
1 /*	$NetBSD: cd9660_vfsops.c,v 1.32 2006/05/14 21:31:52 elad 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.32 2006/05/14 21:31:52 elad 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/specfs/specdev.h>
54 #include <sys/mount.h>
55 #include <sys/buf.h>
56 #include <sys/file.h>
57 #include <sys/disklabel.h>
58 #include <sys/device.h>
59 #include <sys/ioctl.h>
60 #include <sys/cdio.h>
61 #include <sys/errno.h>
62 #include <sys/malloc.h>
63 #include <sys/pool.h>
64 #include <sys/stat.h>
65 #include <sys/conf.h>
66 #include <sys/dirent.h>
67 #include <sys/kauth.h>
68 
69 #include <fs/cd9660/iso.h>
70 #include <fs/cd9660/cd9660_extern.h>
71 #include <fs/cd9660/iso_rrip.h>
72 #include <fs/cd9660/cd9660_node.h>
73 #include <fs/cd9660/cd9660_mount.h>
74 
75 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
76 
77 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc;
78 extern const struct vnodeopv_desc cd9660_specop_opv_desc;
79 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc;
80 
81 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = {
82 	&cd9660_vnodeop_opv_desc,
83 	&cd9660_specop_opv_desc,
84 	&cd9660_fifoop_opv_desc,
85 	NULL,
86 };
87 
88 struct vfsops cd9660_vfsops = {
89 	MOUNT_CD9660,
90 	cd9660_mount,
91 	cd9660_start,
92 	cd9660_unmount,
93 	cd9660_root,
94 	cd9660_quotactl,
95 	cd9660_statvfs,
96 	cd9660_sync,
97 	cd9660_vget,
98 	cd9660_fhtovp,
99 	cd9660_vptofh,
100 	cd9660_init,
101 	cd9660_reinit,
102 	cd9660_done,
103 	cd9660_mountroot,
104 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
105 	vfs_stdextattrctl,
106 	cd9660_vnodeopv_descs,
107 };
108 VFS_ATTACH(cd9660_vfsops);
109 
110 static const struct genfs_ops cd9660_genfsops = {
111 	.gop_size = genfs_size,
112 };
113 
114 /*
115  * Called by vfs_mountroot when iso is going to be mounted as root.
116  *
117  * Name is updated by mount(8) after booting.
118  */
119 #define ROOTNAME	"root_device"
120 
121 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len);
122 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
123 		struct lwp *l, struct iso_args *argp);
124 
125 int
126 cd9660_mountroot()
127 {
128 	struct mount *mp;
129 	struct lwp *l = curlwp;	/* XXX */
130 	int error;
131 	struct iso_args args;
132 
133 	if (device_class(root_device) != DV_DISK)
134 		return (ENODEV);
135 
136 	if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
137 			!= 0) {
138 		vrele(rootvp);
139 		return (error);
140 	}
141 
142 	args.flags = ISOFSMNT_ROOT;
143 	if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) {
144 		mp->mnt_op->vfs_refcount--;
145 		vfs_unbusy(mp);
146 		free(mp, M_MOUNT);
147 		return (error);
148 	}
149 	simple_lock(&mountlist_slock);
150 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
151 	simple_unlock(&mountlist_slock);
152 	(void)cd9660_statvfs(mp, &mp->mnt_stat, l);
153 	vfs_unbusy(mp);
154 	return (0);
155 }
156 
157 /*
158  * VFS Operations.
159  *
160  * mount system call
161  */
162 int
163 cd9660_mount(mp, path, data, ndp, l)
164 	struct mount *mp;
165 	const char *path;
166 	void *data;
167 	struct nameidata *ndp;
168 	struct lwp *l;
169 {
170 	struct vnode *devvp;
171 	struct iso_args args;
172 	struct proc *p;
173 	int error;
174 	struct iso_mnt *imp = VFSTOISOFS(mp);
175 
176 	p = l->l_proc;
177 	if (mp->mnt_flag & MNT_GETARGS) {
178 		if (imp == NULL)
179 			return EIO;
180 		args.fspec = NULL;
181 		args.flags = imp->im_flags;
182 		return copyout(&args, data, sizeof(args));
183 	}
184 	error = copyin(data, &args, sizeof (struct iso_args));
185 	if (error)
186 		return (error);
187 
188 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
189 		return (EROFS);
190 
191 	if ((mp->mnt_flag & MNT_UPDATE) && args.fspec == NULL)
192 		return EINVAL;
193 
194 	/*
195 	 * Not an update, or updating the name: look up the name
196 	 * and verify that it refers to a sensible block device.
197 	 */
198 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, l);
199 	if ((error = namei(ndp)) != 0)
200 		return (error);
201 	devvp = ndp->ni_vp;
202 
203 	if (devvp->v_type != VBLK) {
204 		vrele(devvp);
205 		return ENOTBLK;
206 	}
207 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
208 		vrele(devvp);
209 		return ENXIO;
210 	}
211 	/*
212 	 * If mount by non-root, then verify that user has necessary
213 	 * permissions on the device.
214 	 */
215 	if (kauth_authorize_generic(p->p_cred, KAUTH_GENERIC_ISSUSER, NULL) != 0) {
216 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
217 		error = VOP_ACCESS(devvp, VREAD, p->p_cred, l);
218 		VOP_UNLOCK(devvp, 0);
219 		if (error) {
220 			vrele(devvp);
221 			return (error);
222 		}
223 	}
224 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
225 		/*
226 		 * Disallow multiple mounts of the same device.
227 		 * Disallow mounting of a device that is currently in use
228 		 * (except for root, which might share swap device for
229 		 * miniroot).
230 		 */
231 		error = vfs_mountedon(devvp);
232 		if (error)
233 			goto fail;
234 		if (vcount(devvp) > 1 && devvp != rootvp) {
235 			error = EBUSY;
236 			goto fail;
237 		}
238 		error = VOP_OPEN(devvp, FREAD, FSCRED, l);
239 		if (error)
240 			goto fail;
241 		error = iso_mountfs(devvp, mp, l, &args);
242 		if (error) {
243 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
244 			(void)VOP_CLOSE(devvp, FREAD, NOCRED, l);
245 			VOP_UNLOCK(devvp, 0);
246 			goto fail;
247 		}
248 	} else {
249 		vrele(devvp);
250 		if (devvp != imp->im_devvp)
251 			return (EINVAL);	/* needs translation */
252 	}
253 	return set_statvfs_info(path, UIO_USERSPACE, args.fspec, UIO_USERSPACE,
254 	    mp, l);
255 
256 fail:
257 	vrele(devvp);
258 	return (error);
259 }
260 
261 /*
262  * Make a mount point from a volume descriptor
263  */
264 static int
265 iso_makemp(isomp, bp, ea_len)
266 	struct iso_mnt *isomp;
267 	struct buf *bp;
268 	int *ea_len;
269 {
270 	struct iso_primary_descriptor *pri;
271 	int logical_block_size;
272 	struct iso_directory_record *rootp;
273 
274 	pri = (struct iso_primary_descriptor *)bp->b_data;
275 
276 	logical_block_size = isonum_723 (pri->logical_block_size);
277 
278 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
279 	    || (logical_block_size & (logical_block_size - 1)) != 0)
280 		return -1;
281 
282 	rootp = (struct iso_directory_record *)pri->root_directory_record;
283 
284 	isomp->logical_block_size = logical_block_size;
285 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
286 	memcpy(isomp->root, rootp, sizeof(isomp->root));
287 	isomp->root_extent = isonum_733 (rootp->extent);
288 	isomp->root_size = isonum_733 (rootp->size);
289 	isomp->im_joliet_level = 0;
290 
291 	isomp->im_bmask = logical_block_size - 1;
292 	isomp->im_bshift = 0;
293 	while ((1 << isomp->im_bshift) < isomp->logical_block_size)
294 		isomp->im_bshift++;
295 
296 	if (ea_len != NULL)
297 		*ea_len = isonum_711(rootp->ext_attr_length);
298 
299 	return 0;
300 }
301 
302 /*
303  * Common code for mount and mountroot
304  */
305 static int
306 iso_mountfs(devvp, mp, l, argp)
307 	struct vnode *devvp;
308 	struct mount *mp;
309 	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_proc->p_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, l);
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, l);
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, &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);
386 			bp = NULL;
387 			break;
388 		}
389 
390 		if (bp != NULL) {
391 			brelse(bp);
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 	pribp->b_flags |= B_AGE;
411 	brelse(pribp);
412 	pribp = NULL;
413 
414 	mp->mnt_data = isomp;
415 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
416 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
417 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
418 	mp->mnt_stat.f_namemax = MAXNAMLEN;
419 	mp->mnt_flag |= MNT_LOCAL;
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 	devvp->v_specmountpoint = mp;
427 
428 	/* Check the Rock Ridge Extension support */
429 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
430 		struct iso_directory_record *rootp;
431 
432 		if ((error = bread(isomp->im_devvp,
433 				   (isomp->root_extent + ext_attr_length) <<
434 				   (isomp->im_bshift - DEV_BSHIFT),
435 				   isomp->logical_block_size, NOCRED,
436 				   &bp)) != 0)
437 		    goto out;
438 
439 		rootp = (struct iso_directory_record *)bp->b_data;
440 
441 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
442 		    argp->flags  |= ISOFSMNT_NORRIP;
443 		} else {
444 		    argp->flags  &= ~ISOFSMNT_GENS;
445 		}
446 
447 		/*
448 		 * The contents are valid,
449 		 * but they will get reread as part of another vnode, so...
450 		 */
451 		bp->b_flags |= B_AGE;
452 		brelse(bp);
453 		bp = NULL;
454 	}
455 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
456 		 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
457 
458 	if (isomp->im_flags & ISOFSMNT_GENS)
459 		isomp->iso_ftype = ISO_FTYPE_9660;
460 	else if (isomp->im_flags & ISOFSMNT_NORRIP) {
461 		isomp->iso_ftype = ISO_FTYPE_DEFAULT;
462 		if (argp->flags & ISOFSMNT_NOCASETRANS)
463 			isomp->im_flags |= ISOFSMNT_NOCASETRANS;
464 	} else
465 		isomp->iso_ftype = ISO_FTYPE_RRIP;
466 
467 	/* Check the Joliet Extension support */
468 	if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
469 	    (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
470 	    supbp != NULL) {
471 		joliet_level = 0;
472 		sup = (struct iso_supplementary_descriptor *)supbp->b_data;
473 
474 		if ((isonum_711(sup->flags) & 1) == 0) {
475 			if (memcmp(sup->escape, "%/@", 3) == 0)
476 				joliet_level = 1;
477 			if (memcmp(sup->escape, "%/C", 3) == 0)
478 				joliet_level = 2;
479 			if (memcmp(sup->escape, "%/E", 3) == 0)
480 				joliet_level = 3;
481 		}
482 		if (joliet_level != 0) {
483 			if (iso_makemp(isomp, supbp, NULL) == -1) {
484 				error = EINVAL;
485 				goto out;
486 			}
487 			isomp->im_joliet_level = joliet_level;
488 		}
489 	}
490 
491 	if (supbp != NULL) {
492 		brelse(supbp);
493 		supbp = NULL;
494 	}
495 
496 	return 0;
497 out:
498 	if (bp)
499 		brelse(bp);
500 	if (pribp)
501 		brelse(pribp);
502 	if (supbp)
503 		brelse(supbp);
504 	if (isomp) {
505 		free(isomp, M_ISOFSMNT);
506 		mp->mnt_data = NULL;
507 	}
508 	return error;
509 }
510 
511 /*
512  * Make a filesystem operational.
513  * Nothing to do at the moment.
514  */
515 /* ARGSUSED */
516 int
517 cd9660_start(mp, flags, l)
518 	struct mount *mp;
519 	int flags;
520 	struct lwp *l;
521 {
522 	return 0;
523 }
524 
525 /*
526  * unmount system call
527  */
528 int
529 cd9660_unmount(mp, mntflags, l)
530 	struct mount *mp;
531 	int mntflags;
532 	struct lwp *l;
533 {
534 	struct iso_mnt *isomp;
535 	int error, flags = 0;
536 
537 	if (mntflags & MNT_FORCE)
538 		flags |= FORCECLOSE;
539 #if 0
540 	mntflushbuf(mp, 0);
541 	if (mntinvalbuf(mp))
542 		return EBUSY;
543 #endif
544 	if ((error = vflush(mp, NULLVP, flags)) != 0)
545 		return (error);
546 
547 	isomp = VFSTOISOFS(mp);
548 
549 #ifdef	ISODEVMAP
550 	if (isomp->iso_ftype == ISO_FTYPE_RRIP)
551 		iso_dunmap(isomp->im_dev);
552 #endif
553 
554 	if (isomp->im_devvp->v_type != VBAD)
555 		isomp->im_devvp->v_specmountpoint = NULL;
556 
557 	vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
558 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, l);
559 	vput(isomp->im_devvp);
560 	free(isomp, M_ISOFSMNT);
561 	mp->mnt_data = NULL;
562 	mp->mnt_flag &= ~MNT_LOCAL;
563 	return (error);
564 }
565 
566 /*
567  * Return root of a filesystem
568  */
569 int
570 cd9660_root(mp, vpp)
571 	struct mount *mp;
572 	struct vnode **vpp;
573 {
574 	struct iso_mnt *imp = VFSTOISOFS(mp);
575 	struct iso_directory_record *dp =
576 	    (struct iso_directory_record *)imp->root;
577 	ino_t ino = isodirino(dp, imp);
578 
579 	/*
580 	 * With RRIP we must use the `.' entry of the root directory.
581 	 * Simply tell vget, that it's a relocated directory.
582 	 */
583 	return (cd9660_vget_internal(mp, ino, vpp,
584 				     imp->iso_ftype == ISO_FTYPE_RRIP, dp));
585 }
586 
587 /*
588  * Do operations associated with quotas, not supported
589  */
590 /* ARGSUSED */
591 int
592 cd9660_quotactl(mp, cmd, uid, arg, l)
593 	struct mount *mp;
594 	int cmd;
595 	uid_t uid;
596 	void *arg;
597 	struct lwp *l;
598 {
599 
600 	return (EOPNOTSUPP);
601 }
602 
603 /*
604  * Get file system statistics.
605  */
606 int
607 cd9660_statvfs(mp, sbp, l)
608 	struct mount *mp;
609 	struct statvfs *sbp;
610 	struct lwp *l;
611 {
612 	struct iso_mnt *isomp;
613 
614 	isomp = VFSTOISOFS(mp);
615 
616 	sbp->f_bsize = isomp->logical_block_size;
617 	sbp->f_frsize = sbp->f_bsize;
618 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
619 	sbp->f_blocks = isomp->volume_space_size;
620 	sbp->f_bfree = 0; /* total free blocks */
621 	sbp->f_bavail = 0; /* blocks free for non superuser */
622 	sbp->f_bresvd = 0; /* total reserved blocks */
623 	sbp->f_files =  0; /* total files */
624 	sbp->f_ffree = 0; /* free file nodes */
625 	sbp->f_favail = 0; /* free file nodes for non superuser */
626 	sbp->f_fresvd = 0; /* reserved file nodes */
627 	copy_statvfs_info(sbp, mp);
628 	/* Use the first spare for flags: */
629 	sbp->f_spare[0] = isomp->im_flags;
630 	return 0;
631 }
632 
633 /* ARGSUSED */
634 int
635 cd9660_sync(mp, waitfor, cred, l)
636 	struct mount *mp;
637 	int waitfor;
638 	kauth_cred_t cred;
639 	struct lwp *l;
640 {
641 	return (0);
642 }
643 
644 /*
645  * File handle to vnode
646  *
647  * Have to be really careful about stale file handles:
648  * - check that the inode number is in range
649  * - call iget() to get the locked inode
650  * - check for an unallocated inode (i_mode == 0)
651  * - check that the generation number matches
652  */
653 
654 struct ifid {
655 	ushort	ifid_len;
656 	ushort	ifid_pad;
657 	int	ifid_ino;
658 	long	ifid_start;
659 };
660 
661 /* ARGSUSED */
662 int
663 cd9660_fhtovp(mp, fhp, vpp)
664 	struct mount *mp;
665 	struct fid *fhp;
666 	struct vnode **vpp;
667 {
668 	struct ifid *ifhp = (struct ifid *)fhp;
669 	struct iso_node *ip;
670 	struct vnode *nvp;
671 	int error;
672 
673 #ifdef	ISOFS_DBG
674 	printf("fhtovp: ino %d, start %ld\n",
675 	    ifhp->ifid_ino, ifhp->ifid_start);
676 #endif
677 
678 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
679 		*vpp = NULLVP;
680 		return (error);
681 	}
682 	ip = VTOI(nvp);
683 	if (ip->inode.iso_mode == 0) {
684 		vput(nvp);
685 		*vpp = NULLVP;
686 		return (ESTALE);
687 	}
688 	*vpp = nvp;
689 	return (0);
690 }
691 
692 int
693 cd9660_vget(mp, ino, vpp)
694 	struct mount *mp;
695 	ino_t ino;
696 	struct vnode **vpp;
697 {
698 
699 	/*
700 	 * XXXX
701 	 * It would be nice if we didn't always set the `relocated' flag
702 	 * and force the extra read, but I don't want to think about fixing
703 	 * that right now.
704 	 */
705 	return (cd9660_vget_internal(mp, ino, vpp,
706 #if 0
707 				     VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
708 #else
709 				     0,
710 #endif
711 				     NULL));
712 }
713 
714 int
715 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
716 	struct mount *mp;
717 	ino_t ino;
718 	struct vnode **vpp;
719 	int relocated;
720 	struct iso_directory_record *isodir;
721 {
722 	struct iso_mnt *imp;
723 	struct iso_node *ip;
724 #ifdef ISODEVMAP
725 	struct iso_dnode *dp;
726 #endif
727 	struct buf *bp;
728 	struct vnode *vp, *nvp;
729 	dev_t dev;
730 	int error;
731 
732 	imp = VFSTOISOFS(mp);
733 	dev = imp->im_dev;
734 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
735 		return (0);
736 
737 	/* Allocate a new vnode/iso_node. */
738 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
739 		*vpp = NULLVP;
740 		return (error);
741 	}
742 	ip = pool_get(&cd9660_node_pool, PR_WAITOK);
743 	memset(ip, 0, sizeof(struct iso_node));
744 	vp->v_data = ip;
745 	ip->i_vnode = vp;
746 	ip->i_dev = dev;
747 	ip->i_number = ino;
748 
749 	/*
750 	 * Put it onto its hash chain and lock it so that other requests for
751 	 * this inode will block if they arrive while we are sleeping waiting
752 	 * for old data structures to be purged or for the contents of the
753 	 * disk portion of this inode to be read.
754 	 */
755 	cd9660_ihashins(ip);
756 
757 	if (isodir == 0) {
758 		int lbn, off;
759 
760 		lbn = lblkno(imp, ino);
761 		if (lbn >= imp->volume_space_size) {
762 			vput(vp);
763 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
764 			return (ESTALE);
765 		}
766 
767 		off = blkoff(imp, ino);
768 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
769 			vput(vp);
770 			printf("fhtovp: crosses block boundary %d\n",
771 			    off + ISO_DIRECTORY_RECORD_SIZE);
772 			return (ESTALE);
773 		}
774 
775 		error = bread(imp->im_devvp,
776 			      lbn << (imp->im_bshift - DEV_BSHIFT),
777 			      imp->logical_block_size, NOCRED, &bp);
778 		if (error) {
779 			vput(vp);
780 			brelse(bp);
781 			printf("fhtovp: bread error %d\n",error);
782 			return (error);
783 		}
784 		isodir = (struct iso_directory_record *)(bp->b_data + off);
785 
786 		if (off + isonum_711(isodir->length) >
787 		    imp->logical_block_size) {
788 			vput(vp);
789 			if (bp != 0)
790 				brelse(bp);
791 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
792 			    off +isonum_711(isodir->length), off,
793 			    isonum_711(isodir->length));
794 			return (ESTALE);
795 		}
796 
797 #if 0
798 		if (isonum_733(isodir->extent) +
799 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
800 			if (bp != 0)
801 				brelse(bp);
802 			printf("fhtovp: file start miss %d vs %d\n",
803 			    isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
804 			    ifhp->ifid_start);
805 			return (ESTALE);
806 		}
807 #endif
808 	} else
809 		bp = 0;
810 
811 	ip->i_mnt = imp;
812 	ip->i_devvp = imp->im_devvp;
813 	VREF(ip->i_devvp);
814 
815 	if (relocated) {
816 		/*
817 		 * On relocated directories we must
818 		 * read the `.' entry out of a dir.
819 		 */
820 		ip->iso_start = ino >> imp->im_bshift;
821 		if (bp != 0)
822 			brelse(bp);
823 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
824 			vput(vp);
825 			return (error);
826 		}
827 		isodir = (struct iso_directory_record *)bp->b_data;
828 	}
829 
830 	ip->iso_extent = isonum_733(isodir->extent);
831 	ip->i_size = isonum_733(isodir->size);
832 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
833 
834 	/*
835 	 * Setup time stamp, attribute
836 	 */
837 	vp->v_type = VNON;
838 	switch (imp->iso_ftype) {
839 	default:	/* ISO_FTYPE_9660 */
840 	    {
841 		struct buf *bp2;
842 		int off;
843 		if ((imp->im_flags & ISOFSMNT_EXTATT)
844 		    && (off = isonum_711(isodir->ext_attr_length)))
845 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
846 			    NULL, &bp2);
847 		else
848 			bp2 = NULL;
849 		cd9660_defattr(isodir, ip, bp2);
850 		cd9660_deftstamp(isodir, ip, bp2);
851 		if (bp2)
852 			brelse(bp2);
853 		break;
854 	    }
855 	case ISO_FTYPE_RRIP:
856 		cd9660_rrip_analyze(isodir, ip, imp);
857 		break;
858 	}
859 
860 	if (bp != 0)
861 		brelse(bp);
862 
863 	/*
864 	 * Initialize the associated vnode
865 	 */
866 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
867 	case VFIFO:
868 		vp->v_op = cd9660_fifoop_p;
869 		break;
870 	case VCHR:
871 	case VBLK:
872 		/*
873 		 * if device, look at device number table for translation
874 		 */
875 #ifdef	ISODEVMAP
876 		if ((dp = iso_dmap(dev, ino, 0)) != NULL)
877 			ip->inode.iso_rdev = dp->d_dev;
878 #endif
879 		vp->v_op = cd9660_specop_p;
880 		if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
881 			/*
882 			 * Discard unneeded vnode, but save its iso_node.
883 			 * Note that the lock is carried over in the iso_node
884 			 * to the replacement vnode.
885 			 */
886 			nvp->v_data = vp->v_data;
887 			vp->v_data = NULL;
888 			VOP_UNLOCK(vp, 0);
889 			vp->v_op = spec_vnodeop_p;
890 			vrele(vp);
891 			vgone(vp);
892 			lockmgr(&nvp->v_lock, LK_EXCLUSIVE, &nvp->v_interlock);
893 			/*
894 			 * Reinitialize aliased inode.
895 			 */
896 			vp = nvp;
897 			ip->i_vnode = vp;
898 		}
899 		break;
900 	case VLNK:
901 	case VNON:
902 	case VSOCK:
903 	case VDIR:
904 	case VBAD:
905 		break;
906 	case VREG:
907 		uvm_vnp_setsize(vp, ip->i_size);
908 		break;
909 	}
910 
911 	if (ip->iso_extent == imp->root_extent)
912 		vp->v_flag |= VROOT;
913 
914 	/*
915 	 * XXX need generation number?
916 	 */
917 
918 	genfs_node_init(vp, &cd9660_genfsops);
919 	*vpp = vp;
920 	return (0);
921 }
922 
923 /*
924  * Vnode pointer to File handle
925  */
926 /* ARGSUSED */
927 int
928 cd9660_vptofh(vp, fhp)
929 	struct vnode *vp;
930 	struct fid *fhp;
931 {
932 	struct iso_node *ip = VTOI(vp);
933 	struct ifid *ifhp;
934 
935 	ifhp = (struct ifid *)fhp;
936 	ifhp->ifid_len = sizeof(struct ifid);
937 
938 	ifhp->ifid_ino = ip->i_number;
939 	ifhp->ifid_start = ip->iso_start;
940 
941 #ifdef	ISOFS_DBG
942 	printf("vptofh: ino %d, start %ld\n",
943 	    ifhp->ifid_ino,ifhp->ifid_start);
944 #endif
945 	return 0;
946 }
947 
948 SYSCTL_SETUP(sysctl_vfs_cd9660_setup, "sysctl vfs.cd9660 subtree setup")
949 {
950 
951 	sysctl_createv(clog, 0, NULL, NULL,
952 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
953 		       NULL, 0, NULL, 0,
954 		       CTL_VFS, CTL_EOL);
955 	sysctl_createv(clog, 0, NULL, NULL,
956 		       CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
957 		       SYSCTL_DESCR("ISO-9660 file system"),
958 		       NULL, 0, NULL, 0,
959 		       CTL_VFS, 14, CTL_EOL);
960 
961 	sysctl_createv(clog, 0, NULL, NULL,
962 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
963 		       CTLTYPE_INT, "utf8_joliet",
964 		       SYSCTL_DESCR("Encode Joliet file names to UTF-8"),
965 		       NULL, 0, &cd9660_utf8_joliet, 0,
966 		       CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
967 
968 }
969