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