xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
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  * 4. 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_vfsops.c	8.18 (Berkeley) 5/22/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/priv.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/cdio.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/stat.h>
55 #include <sys/syslog.h>
56 #include <sys/iconv.h>
57 
58 #include <isofs/cd9660/iso.h>
59 #include <isofs/cd9660/iso_rrip.h>
60 #include <isofs/cd9660/cd9660_node.h>
61 #include <isofs/cd9660/cd9660_mount.h>
62 
63 #include <geom/geom.h>
64 #include <geom/geom_vfs.h>
65 
66 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure");
67 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part");
68 
69 struct iconv_functions *cd9660_iconv = NULL;
70 
71 static vfs_mount_t	cd9660_mount;
72 static vfs_cmount_t	cd9660_cmount;
73 static vfs_unmount_t	cd9660_unmount;
74 static vfs_root_t	cd9660_root;
75 static vfs_statfs_t	cd9660_statfs;
76 static vfs_vget_t	cd9660_vget;
77 static vfs_fhtovp_t	cd9660_fhtovp;
78 static vfs_vptofh_t	cd9660_vptofh;
79 
80 static struct vfsops cd9660_vfsops = {
81 	.vfs_fhtovp =		cd9660_fhtovp,
82 	.vfs_mount =		cd9660_mount,
83 	.vfs_cmount =		cd9660_cmount,
84 	.vfs_root =		cd9660_root,
85 	.vfs_statfs =		cd9660_statfs,
86 	.vfs_unmount =		cd9660_unmount,
87 	.vfs_vget =		cd9660_vget,
88 	.vfs_vptofh =		cd9660_vptofh,
89 };
90 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
91 MODULE_VERSION(cd9660, 1);
92 
93 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
94 		       struct thread *td);
95 
96 /*
97  * VFS Operations.
98  */
99 
100 static int
101 cd9660_cmount(struct mntarg *ma, void *data, int flags, struct thread *td)
102 {
103 	struct iso_args args;
104 	int error;
105 
106 	error = copyin(data, &args, sizeof args);
107 	if (error)
108 		return (error);
109 
110 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
111 	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
112 	ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
113 	ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
114 	ma = mount_argf(ma, "ssector", "%u", args.ssector);
115 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
116 	ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
117 	ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
118 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
119 	ma = mount_argb(ma,
120 	    args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
121 	ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
122 
123 	error = kernel_mount(ma, flags);
124 
125 	return (error);
126 }
127 
128 static int
129 cd9660_mount(struct mount *mp, struct thread *td)
130 {
131 	struct vnode *devvp;
132 	char *fspec;
133 	int error;
134 	mode_t accessmode;
135 	struct nameidata ndp;
136 	struct iso_mnt *imp = 0;
137 
138 	/*
139 	 * Unconditionally mount as read-only.
140 	 */
141 	MNT_ILOCK(mp);
142 	mp->mnt_flag |= MNT_RDONLY;
143 	MNT_IUNLOCK(mp);
144 
145 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
146 	if (error)
147 		return (error);
148 
149 	imp = VFSTOISOFS(mp);
150 
151 	if (mp->mnt_flag & MNT_UPDATE) {
152 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
153 			return (0);
154 	}
155 	/*
156 	 * Not an update, or updating the name: look up the name
157 	 * and verify that it refers to a sensible block device.
158 	 */
159 	NDINIT(&ndp, LOOKUP, FOLLOW, UIO_SYSSPACE, fspec, td);
160 	if ((error = namei(&ndp)))
161 		return (error);
162 	NDFREE(&ndp, NDF_ONLY_PNBUF);
163 	devvp = ndp.ni_vp;
164 
165 	if (!vn_isdisk(devvp, &error)) {
166 		vrele(devvp);
167 		return (error);
168 	}
169 
170 	/*
171 	 * Verify that user has necessary permissions on the device,
172 	 * or has superuser abilities
173 	 */
174 	accessmode = VREAD;
175 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
176 	error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
177 	if (error)
178 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
179 	if (error) {
180 		vput(devvp);
181 		return (error);
182 	}
183 	VOP_UNLOCK(devvp, 0, td);
184 
185 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
186 		error = iso_mountfs(devvp, mp, td);
187 	} else {
188 		if (devvp != imp->im_devvp)
189 			error = EINVAL;	/* needs translation */
190 		else
191 			vrele(devvp);
192 	}
193 	if (error) {
194 		vrele(devvp);
195 		return error;
196 	}
197 	vfs_mountedfrom(mp, fspec);
198 	return 0;
199 }
200 
201 /*
202  * Common code for mount and mountroot
203  */
204 static int
205 iso_mountfs(devvp, mp, td)
206 	struct vnode *devvp;
207 	struct mount *mp;
208 	struct thread *td;
209 {
210 	struct iso_mnt *isomp = (struct iso_mnt *)0;
211 	struct buf *bp = NULL;
212 	struct buf *pribp = NULL, *supbp = NULL;
213 	struct cdev *dev = devvp->v_rdev;
214 	int error = EINVAL;
215 	int high_sierra = 0;
216 	int iso_bsize;
217 	int iso_blknum;
218 	int joliet_level;
219 	struct iso_volume_descriptor *vdp = 0;
220 	struct iso_primary_descriptor *pri = NULL;
221 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
222 	struct iso_supplementary_descriptor *sup = NULL;
223 	struct iso_directory_record *rootp;
224 	int logical_block_size, ssector;
225 	struct g_consumer *cp;
226 	struct bufobj *bo;
227 	char *cs_local, *cs_disk;
228 
229 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
230 	DROP_GIANT();
231 	g_topology_lock();
232 	error = g_vfs_open(devvp, &cp, "cd9660", 0);
233 	g_topology_unlock();
234 	PICKUP_GIANT();
235 	VOP_UNLOCK(devvp, 0, td);
236 	if (error)
237 		return error;
238 	if (devvp->v_rdev->si_iosize_max != 0)
239 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
240 	if (mp->mnt_iosize_max > MAXPHYS)
241 		mp->mnt_iosize_max = MAXPHYS;
242 
243 	bo = &devvp->v_bufobj;
244 	bo->bo_private = cp;
245 	bo->bo_ops = g_vfs_bufops;
246 
247 	/* This is the "logical sector size".  The standard says this
248 	 * should be 2048 or the physical sector size on the device,
249 	 * whichever is greater.
250 	 */
251 	if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
252 		DROP_GIANT();
253 		g_topology_lock();
254 		g_vfs_close(cp, td);
255 		g_topology_unlock();
256                 PICKUP_GIANT();
257 		return (EINVAL);
258 	}
259 
260 	iso_bsize = cp->provider->sectorsize;
261 
262 	joliet_level = 0;
263 	if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
264 		ssector = 0;
265 	for (iso_blknum = 16 + ssector;
266 	     iso_blknum < 100 + ssector;
267 	     iso_blknum++) {
268 		if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
269 				  iso_bsize, NOCRED, &bp)) != 0)
270 			goto out;
271 
272 		vdp = (struct iso_volume_descriptor *)bp->b_data;
273 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
274 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
275 				  sizeof vdp->id) != 0) {
276 				error = EINVAL;
277 				goto out;
278 			} else
279 				high_sierra = 1;
280 		}
281 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
282 		case ISO_VD_PRIMARY:
283 			if (pribp == NULL) {
284 				pribp = bp;
285 				bp = NULL;
286 				pri = (struct iso_primary_descriptor *)vdp;
287 				pri_sierra =
288 				  (struct iso_sierra_primary_descriptor *)vdp;
289 			}
290 			break;
291 
292 		case ISO_VD_SUPPLEMENTARY:
293 			if (supbp == NULL) {
294 				supbp = bp;
295 				bp = NULL;
296 				sup = (struct iso_supplementary_descriptor *)vdp;
297 
298 				if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
299 					if (bcmp(sup->escape, "%/@", 3) == 0)
300 						joliet_level = 1;
301 					if (bcmp(sup->escape, "%/C", 3) == 0)
302 						joliet_level = 2;
303 					if (bcmp(sup->escape, "%/E", 3) == 0)
304 						joliet_level = 3;
305 
306 					if ((isonum_711 (sup->flags) & 1) &&
307 					    !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
308 						joliet_level = 0;
309 				}
310 			}
311 			break;
312 
313 		case ISO_VD_END:
314 			goto vd_end;
315 
316 		default:
317 			break;
318 		}
319 		if (bp) {
320 			brelse(bp);
321 			bp = NULL;
322 		}
323 	}
324  vd_end:
325 	if (bp) {
326 		brelse(bp);
327 		bp = NULL;
328 	}
329 
330 	if (pri == NULL) {
331 		error = EINVAL;
332 		goto out;
333 	}
334 
335 	logical_block_size =
336 		isonum_723 (high_sierra?
337 			    pri_sierra->logical_block_size:
338 			    pri->logical_block_size);
339 
340 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
341 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
342 		error = EINVAL;
343 		goto out;
344 	}
345 
346 	rootp = (struct iso_directory_record *)
347 		(high_sierra?
348 		 pri_sierra->root_directory_record:
349 		 pri->root_directory_record);
350 
351 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
352 	isomp->im_cp = cp;
353 	isomp->im_bo = bo;
354 	isomp->logical_block_size = logical_block_size;
355 	isomp->volume_space_size =
356 		isonum_733 (high_sierra?
357 			    pri_sierra->volume_space_size:
358 			    pri->volume_space_size);
359 	isomp->joliet_level = 0;
360 	/*
361 	 * Since an ISO9660 multi-session CD can also access previous
362 	 * sessions, we have to include them into the space consider-
363 	 * ations.  This doesn't yield a very accurate number since
364 	 * parts of the old sessions might be inaccessible now, but we
365 	 * can't do much better.  This is also important for the NFS
366 	 * filehandle validation.
367 	 */
368 	isomp->volume_space_size += ssector;
369 	bcopy (rootp, isomp->root, sizeof isomp->root);
370 	isomp->root_extent = isonum_733 (rootp->extent);
371 	isomp->root_size = isonum_733 (rootp->size);
372 
373 	isomp->im_bmask = logical_block_size - 1;
374 	isomp->im_bshift = ffs(logical_block_size) - 1;
375 
376 	pribp->b_flags |= B_AGE;
377 	brelse(pribp);
378 	pribp = NULL;
379 
380 	mp->mnt_data = (qaddr_t)isomp;
381 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
382 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
383 	mp->mnt_maxsymlinklen = 0;
384 	MNT_ILOCK(mp);
385 	mp->mnt_flag |= MNT_LOCAL;
386 	MNT_IUNLOCK(mp);
387 	isomp->im_mountp = mp;
388 	isomp->im_dev = dev;
389 	isomp->im_devvp = devvp;
390 
391 	vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
392 	vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
393 	vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
394 	vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
395 	vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
396 
397 	/* Check the Rock Ridge Extension support */
398 	if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
399 		if ((error = bread(isomp->im_devvp,
400 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
401 				  (isomp->im_bshift - DEV_BSHIFT),
402 				  isomp->logical_block_size, NOCRED, &bp)) != 0)
403 		    goto out;
404 
405 		rootp = (struct iso_directory_record *)bp->b_data;
406 
407 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
408 		    isomp->im_flags |= ISOFSMNT_NORRIP;
409 		} else {
410 		    isomp->im_flags &= ~ISOFSMNT_GENS;
411 		}
412 
413 		/*
414 		 * The contents are valid,
415 		 * but they will get reread as part of another vnode, so...
416 		 */
417 		bp->b_flags |= B_AGE;
418 		brelse(bp);
419 		bp = NULL;
420 	}
421 
422 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
423 		cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
424 		if (error)
425 			goto out;
426 		cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
427 		if (error)
428 			goto out;
429 		cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
430 		cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
431 	} else {
432 		isomp->im_d2l = NULL;
433 		isomp->im_l2d = NULL;
434 	}
435 
436 	if (high_sierra) {
437 		/* this effectively ignores all the mount flags */
438 		if (bootverbose)
439 			log(LOG_INFO, "cd9660: High Sierra Format\n");
440 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
441 	} else
442 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
443 		  default:
444 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
445 			  break;
446 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
447 			  isomp->iso_ftype = ISO_FTYPE_9660;
448 			  break;
449 		  case 0:
450 			  if (bootverbose)
451 			  	  log(LOG_INFO, "cd9660: RockRidge Extension\n");
452 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
453 			  break;
454 		}
455 
456 	/* Decide whether to use the Joliet descriptor */
457 
458 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
459 		if (bootverbose)
460 			log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
461 			    joliet_level);
462 		rootp = (struct iso_directory_record *)
463 			sup->root_directory_record;
464 		bcopy (rootp, isomp->root, sizeof isomp->root);
465 		isomp->root_extent = isonum_733 (rootp->extent);
466 		isomp->root_size = isonum_733 (rootp->size);
467 		isomp->joliet_level = joliet_level;
468 		supbp->b_flags |= B_AGE;
469 	}
470 
471 	if (supbp) {
472 		brelse(supbp);
473 		supbp = NULL;
474 	}
475 
476 	return 0;
477 out:
478 	if (bp)
479 		brelse(bp);
480 	if (pribp)
481 		brelse(pribp);
482 	if (supbp)
483 		brelse(supbp);
484 	if (cp != NULL) {
485 		DROP_GIANT();
486 		g_topology_lock();
487 		g_vfs_close(cp, td);
488 		g_topology_unlock();
489 		PICKUP_GIANT();
490 	}
491 	if (isomp) {
492 		free((caddr_t)isomp, M_ISOFSMNT);
493 		mp->mnt_data = (qaddr_t)0;
494 	}
495 	return error;
496 }
497 
498 /*
499  * unmount system call
500  */
501 static int
502 cd9660_unmount(mp, mntflags, td)
503 	struct mount *mp;
504 	int mntflags;
505 	struct thread *td;
506 {
507 	struct iso_mnt *isomp;
508 	int error, flags = 0;
509 
510 	if (mntflags & MNT_FORCE)
511 		flags |= FORCECLOSE;
512 #if 0
513 	mntflushbuf(mp, 0);
514 	if (mntinvalbuf(mp))
515 		return EBUSY;
516 #endif
517 	if ((error = vflush(mp, 0, flags, td)))
518 		return (error);
519 
520 	isomp = VFSTOISOFS(mp);
521 
522 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
523 		if (isomp->im_d2l)
524 			cd9660_iconv->close(isomp->im_d2l);
525 		if (isomp->im_l2d)
526 			cd9660_iconv->close(isomp->im_l2d);
527 	}
528 	DROP_GIANT();
529 	g_topology_lock();
530 	g_vfs_close(isomp->im_cp, td);
531 	g_topology_unlock();
532 	PICKUP_GIANT();
533 	vrele(isomp->im_devvp);
534 	free((caddr_t)isomp, M_ISOFSMNT);
535 	mp->mnt_data = (qaddr_t)0;
536 	MNT_ILOCK(mp);
537 	mp->mnt_flag &= ~MNT_LOCAL;
538 	MNT_IUNLOCK(mp);
539 	return (error);
540 }
541 
542 /*
543  * Return root of a filesystem
544  */
545 static int
546 cd9660_root(mp, flags, vpp, td)
547 	struct mount *mp;
548 	int flags;
549 	struct vnode **vpp;
550 	struct thread *td;
551 {
552 	struct iso_mnt *imp = VFSTOISOFS(mp);
553 	struct iso_directory_record *dp =
554 	    (struct iso_directory_record *)imp->root;
555 	ino_t ino = isodirino(dp, imp);
556 
557 	/*
558 	 * With RRIP we must use the `.' entry of the root directory.
559 	 * Simply tell vget, that it's a relocated directory.
560 	 */
561 	return (cd9660_vget_internal(mp, ino, LK_EXCLUSIVE, vpp,
562 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
563 }
564 
565 /*
566  * Get filesystem statistics.
567  */
568 static int
569 cd9660_statfs(mp, sbp, td)
570 	struct mount *mp;
571 	struct statfs *sbp;
572 	struct thread *td;
573 {
574 	struct iso_mnt *isomp;
575 
576 	isomp = VFSTOISOFS(mp);
577 
578 	sbp->f_bsize = isomp->logical_block_size;
579 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
580 	sbp->f_blocks = isomp->volume_space_size;
581 	sbp->f_bfree = 0; /* total free blocks */
582 	sbp->f_bavail = 0; /* blocks free for non superuser */
583 	sbp->f_files =	0; /* total files */
584 	sbp->f_ffree = 0; /* free file nodes */
585 	return 0;
586 }
587 
588 /*
589  * File handle to vnode
590  *
591  * Have to be really careful about stale file handles:
592  * - check that the inode number is in range
593  * - call iget() to get the locked inode
594  * - check for an unallocated inode (i_mode == 0)
595  * - check that the generation number matches
596  */
597 
598 struct ifid {
599 	u_short	ifid_len;
600 	u_short	ifid_pad;
601 	int	ifid_ino;
602 	long	ifid_start;
603 };
604 
605 /* ARGSUSED */
606 static int
607 cd9660_fhtovp(mp, fhp, vpp)
608 	struct mount *mp;
609 	struct fid *fhp;
610 	struct vnode **vpp;
611 {
612 	struct ifid *ifhp = (struct ifid *)fhp;
613 	struct iso_node *ip;
614 	struct vnode *nvp;
615 	int error;
616 
617 #ifdef	ISOFS_DBG
618 	printf("fhtovp: ino %d, start %ld\n",
619 	       ifhp->ifid_ino, ifhp->ifid_start);
620 #endif
621 
622 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
623 		*vpp = NULLVP;
624 		return (error);
625 	}
626 	ip = VTOI(nvp);
627 	if (ip->inode.iso_mode == 0) {
628 		vput(nvp);
629 		*vpp = NULLVP;
630 		return (ESTALE);
631 	}
632 	*vpp = nvp;
633 	vnode_create_vobject(*vpp, ip->i_size, curthread);
634 	return (0);
635 }
636 
637 static int
638 cd9660_vget(mp, ino, flags, vpp)
639 	struct mount *mp;
640 	ino_t ino;
641 	int flags;
642 	struct vnode **vpp;
643 {
644 
645 	/*
646 	 * XXXX
647 	 * It would be nice if we didn't always set the `relocated' flag
648 	 * and force the extra read, but I don't want to think about fixing
649 	 * that right now.
650 	 */
651 	return (cd9660_vget_internal(mp, ino, flags, vpp,
652 #if 0
653 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
654 #else
655 	    0,
656 #endif
657 	    (struct iso_directory_record *)0));
658 }
659 
660 int
661 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
662 	struct mount *mp;
663 	ino_t ino;
664 	int flags;
665 	struct vnode **vpp;
666 	int relocated;
667 	struct iso_directory_record *isodir;
668 {
669 	struct iso_mnt *imp;
670 	struct iso_node *ip;
671 	struct buf *bp;
672 	struct vnode *vp;
673 	struct cdev *dev;
674 	int error;
675 
676 	error = vfs_hash_get(mp, ino, flags, curthread, vpp, NULL, NULL);
677 	if (error || *vpp != NULL)
678 		return (error);
679 
680 	imp = VFSTOISOFS(mp);
681 	dev = imp->im_dev;
682 
683 	/* Allocate a new vnode/iso_node. */
684 	if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
685 		*vpp = NULLVP;
686 		return (error);
687 	}
688 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
689 	    M_WAITOK | M_ZERO);
690 	vp->v_data = ip;
691 	ip->i_vnode = vp;
692 	ip->i_number = ino;
693 
694 	error = vfs_hash_insert(vp, ino, flags, curthread, vpp, NULL, NULL);
695 	if (error || *vpp != NULL)
696 		return (error);
697 
698 	if (isodir == 0) {
699 		int lbn, off;
700 
701 		lbn = lblkno(imp, ino);
702 		if (lbn >= imp->volume_space_size) {
703 			vput(vp);
704 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
705 			return (ESTALE);
706 		}
707 
708 		off = blkoff(imp, ino);
709 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
710 			vput(vp);
711 			printf("fhtovp: crosses block boundary %d\n",
712 			       off + ISO_DIRECTORY_RECORD_SIZE);
713 			return (ESTALE);
714 		}
715 
716 		error = bread(imp->im_devvp,
717 			      lbn << (imp->im_bshift - DEV_BSHIFT),
718 			      imp->logical_block_size, NOCRED, &bp);
719 		if (error) {
720 			vput(vp);
721 			brelse(bp);
722 			printf("fhtovp: bread error %d\n",error);
723 			return (error);
724 		}
725 		isodir = (struct iso_directory_record *)(bp->b_data + off);
726 
727 		if (off + isonum_711(isodir->length) >
728 		    imp->logical_block_size) {
729 			vput(vp);
730 			if (bp != 0)
731 				brelse(bp);
732 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
733 			       off +isonum_711(isodir->length), off,
734 			       isonum_711(isodir->length));
735 			return (ESTALE);
736 		}
737 
738 #if 0
739 		if (isonum_733(isodir->extent) +
740 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
741 			if (bp != 0)
742 				brelse(bp);
743 			printf("fhtovp: file start miss %d vs %d\n",
744 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
745 			       ifhp->ifid_start);
746 			return (ESTALE);
747 		}
748 #endif
749 	} else
750 		bp = 0;
751 
752 	ip->i_mnt = imp;
753 	VREF(imp->im_devvp);
754 
755 	if (relocated) {
756 		/*
757 		 * On relocated directories we must
758 		 * read the `.' entry out of a dir.
759 		 */
760 		ip->iso_start = ino >> imp->im_bshift;
761 		if (bp != 0)
762 			brelse(bp);
763 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
764 			vput(vp);
765 			return (error);
766 		}
767 		isodir = (struct iso_directory_record *)bp->b_data;
768 	}
769 
770 	ip->iso_extent = isonum_733(isodir->extent);
771 	ip->i_size = isonum_733(isodir->size);
772 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
773 
774 	/*
775 	 * Setup time stamp, attribute
776 	 */
777 	vp->v_type = VNON;
778 	switch (imp->iso_ftype) {
779 	default:	/* ISO_FTYPE_9660 */
780 	    {
781 		struct buf *bp2;
782 		int off;
783 		if ((imp->im_flags & ISOFSMNT_EXTATT)
784 		    && (off = isonum_711(isodir->ext_attr_length)))
785 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
786 				     &bp2);
787 		else
788 			bp2 = NULL;
789 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
790 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
791 		if (bp2)
792 			brelse(bp2);
793 		break;
794 	    }
795 	case ISO_FTYPE_RRIP:
796 		cd9660_rrip_analyze(isodir, ip, imp);
797 		break;
798 	}
799 
800 	if (bp != 0)
801 		brelse(bp);
802 
803 	/*
804 	 * Initialize the associated vnode
805 	 */
806 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
807 	case VFIFO:
808 		vp->v_op = &cd9660_fifoops;
809 		break;
810 	default:
811 		break;
812 	}
813 
814 	if (ip->iso_extent == imp->root_extent)
815 		vp->v_vflag |= VV_ROOT;
816 
817 	/*
818 	 * XXX need generation number?
819 	 */
820 
821 	*vpp = vp;
822 	return (0);
823 }
824 
825 /*
826  * Vnode pointer to File handle
827  */
828 /* ARGSUSED */
829 static int
830 cd9660_vptofh(vp, fhp)
831 	struct vnode *vp;
832 	struct fid *fhp;
833 {
834 	struct iso_node *ip = VTOI(vp);
835 	struct ifid *ifhp;
836 
837 	ifhp = (struct ifid *)fhp;
838 	ifhp->ifid_len = sizeof(struct ifid);
839 
840 	ifhp->ifid_ino = ip->i_number;
841 	ifhp->ifid_start = ip->iso_start;
842 
843 #ifdef	ISOFS_DBG
844 	printf("vptofh: ino %d, start %ld\n",
845 	       ifhp->ifid_ino,ifhp->ifid_start);
846 #endif
847 	return 0;
848 }
849