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