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