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