xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision 02e9120893770924227138ba49df1edb3896112a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley
8  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9  * Support code is derived from software contributed to Berkeley
10  * by Atsushi Murai (amurai@spec.co.jp).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/namei.h>
40 #include <sys/priv.h>
41 #include <sys/proc.h>
42 #include <sys/kernel.h>
43 #include <sys/vnode.h>
44 #include <sys/mount.h>
45 #include <sys/bio.h>
46 #include <sys/buf.h>
47 #include <sys/cdio.h>
48 #include <sys/conf.h>
49 #include <sys/fcntl.h>
50 #include <sys/malloc.h>
51 #include <sys/stat.h>
52 #include <sys/syslog.h>
53 #include <sys/iconv.h>
54 
55 #include <fs/cd9660/iso.h>
56 #include <fs/cd9660/iso_rrip.h>
57 #include <fs/cd9660/cd9660_node.h>
58 #include <fs/cd9660/cd9660_mount.h>
59 
60 #include <geom/geom.h>
61 #include <geom/geom_vfs.h>
62 
63 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure");
64 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part");
65 
66 struct iconv_functions *cd9660_iconv = NULL;
67 
68 static vfs_mount_t	cd9660_mount;
69 static vfs_cmount_t	cd9660_cmount;
70 static vfs_unmount_t	cd9660_unmount;
71 static vfs_root_t	cd9660_root;
72 static vfs_statfs_t	cd9660_statfs;
73 static vfs_vget_t	cd9660_vget;
74 static vfs_fhtovp_t	cd9660_fhtovp;
75 
76 static struct vfsops cd9660_vfsops = {
77 	.vfs_fhtovp =		cd9660_fhtovp,
78 	.vfs_mount =		cd9660_mount,
79 	.vfs_cmount =		cd9660_cmount,
80 	.vfs_root =		cd9660_root,
81 	.vfs_statfs =		cd9660_statfs,
82 	.vfs_unmount =		cd9660_unmount,
83 	.vfs_vget =		cd9660_vget,
84 };
85 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
86 MODULE_VERSION(cd9660, 1);
87 
88 static int cd9660_vfs_hash_cmp(struct vnode *vp, void *pino);
89 static int iso_mountfs(struct vnode *devvp, struct mount *mp);
90 
91 /*
92  * VFS Operations.
93  */
94 
95 static int
96 cd9660_cmount(struct mntarg *ma, void *data, uint64_t flags)
97 {
98 	struct iso_args args;
99 	int error;
100 
101 	error = copyin(data, &args, sizeof args);
102 	if (error)
103 		return (error);
104 
105 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
106 	ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
107 	ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
108 	ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
109 	ma = mount_argf(ma, "ssector", "%u", args.ssector);
110 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
111 	ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
112 	ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
113 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
114 	ma = mount_argb(ma,
115 	    args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
116 	ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
117 
118 	error = kernel_mount(ma, flags);
119 
120 	return (error);
121 }
122 
123 static int
124 cd9660_mount(struct mount *mp)
125 {
126 	struct vnode *devvp;
127 	struct thread *td;
128 	char *fspec;
129 	int error;
130 	accmode_t accmode;
131 	struct nameidata ndp;
132 	struct iso_mnt *imp = NULL;
133 
134 	td = curthread;
135 
136 	/*
137 	 * Unconditionally mount as read-only.
138 	 */
139 	MNT_ILOCK(mp);
140 	mp->mnt_flag |= MNT_RDONLY;
141 	MNT_IUNLOCK(mp);
142 
143 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
144 	if (error)
145 		return (error);
146 
147 	imp = VFSTOISOFS(mp);
148 
149 	if (mp->mnt_flag & MNT_UPDATE) {
150 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
151 			return (0);
152 	}
153 	/*
154 	 * Not an update, or updating the name: look up the name
155 	 * and verify that it refers to a sensible block device.
156 	 */
157 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec);
158 	if ((error = namei(&ndp)))
159 		return (error);
160 	NDFREE_PNBUF(&ndp);
161 	devvp = ndp.ni_vp;
162 
163 	if (!vn_isdisk_error(devvp, &error)) {
164 		vput(devvp);
165 		return (error);
166 	}
167 
168 	/*
169 	 * Verify that user has necessary permissions on the device,
170 	 * or has superuser abilities
171 	 */
172 	accmode = VREAD;
173 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
174 	if (error)
175 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
176 	if (error) {
177 		vput(devvp);
178 		return (error);
179 	}
180 
181 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
182 		error = iso_mountfs(devvp, mp);
183 		if (error)
184 			vrele(devvp);
185 	} else {
186 		if (devvp != imp->im_devvp)
187 			error = EINVAL;	/* needs translation */
188 		vput(devvp);
189 	}
190 	if (error)
191 		return (error);
192 	vfs_mountedfrom(mp, fspec);
193 	return (0);
194 }
195 
196 /*
197  * Common code for mount and mountroot
198  */
199 static int
200 iso_mountfs(struct vnode *devvp, struct mount *mp)
201 {
202 	struct iso_mnt *isomp = NULL;
203 	struct buf *bp = NULL;
204 	struct buf *pribp = NULL, *supbp = NULL;
205 	struct cdev *dev;
206 	int error = EINVAL;
207 	int high_sierra = 0;
208 	int iso_bsize;
209 	int iso_blknum;
210 	int joliet_level;
211 	int isverified = 0;
212 	struct iso_volume_descriptor *vdp = NULL;
213 	struct iso_primary_descriptor *pri = NULL;
214 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
215 	struct iso_supplementary_descriptor *sup = NULL;
216 	struct iso_directory_record *rootp;
217 	int logical_block_size, ssector;
218 	struct g_consumer *cp;
219 	struct bufobj *bo;
220 	char *cs_local, *cs_disk;
221 
222 	dev = devvp->v_rdev;
223 	dev_ref(dev);
224 	g_topology_lock();
225 	error = g_vfs_open(devvp, &cp, "cd9660", 0);
226 	if (error == 0)
227 		g_getattr("MNT::verified", cp, &isverified);
228 	g_topology_unlock();
229 	VOP_UNLOCK(devvp);
230 	if (error)
231 		goto out;
232 	if (devvp->v_rdev->si_iosize_max != 0)
233 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
234 	if (mp->mnt_iosize_max > maxphys)
235 		mp->mnt_iosize_max = maxphys;
236 
237 	bo = &devvp->v_bufobj;
238 
239 	/* This is the "logical sector size".  The standard says this
240 	 * should be 2048 or the physical sector size on the device,
241 	 * whichever is greater.
242 	 */
243 	if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
244 		error = EINVAL;
245 		goto out;
246 	}
247 
248 	iso_bsize = cp->provider->sectorsize;
249 
250 	joliet_level = 0;
251 	if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
252 		ssector = 0;
253 	for (iso_blknum = 16 + ssector;
254 	     iso_blknum < 100 + ssector;
255 	     iso_blknum++) {
256 		if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
257 				  iso_bsize, NOCRED, &bp)) != 0)
258 			goto out;
259 
260 		vdp = (struct iso_volume_descriptor *)bp->b_data;
261 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
262 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
263 				  sizeof vdp->id_sierra) != 0) {
264 				error = EINVAL;
265 				goto out;
266 			} else
267 				high_sierra = 1;
268 		}
269 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
270 		case ISO_VD_PRIMARY:
271 			if (pribp == NULL) {
272 				pribp = bp;
273 				bp = NULL;
274 				pri = (struct iso_primary_descriptor *)vdp;
275 				pri_sierra =
276 				  (struct iso_sierra_primary_descriptor *)vdp;
277 			}
278 			break;
279 
280 		case ISO_VD_SUPPLEMENTARY:
281 			if (supbp == NULL) {
282 				supbp = bp;
283 				bp = NULL;
284 				sup = (struct iso_supplementary_descriptor *)vdp;
285 
286 				if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
287 					if (bcmp(sup->escape, "%/@", 3) == 0)
288 						joliet_level = 1;
289 					if (bcmp(sup->escape, "%/C", 3) == 0)
290 						joliet_level = 2;
291 					if (bcmp(sup->escape, "%/E", 3) == 0)
292 						joliet_level = 3;
293 
294 					if ((isonum_711 (sup->flags) & 1) &&
295 					    !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
296 						joliet_level = 0;
297 				}
298 			}
299 			break;
300 
301 		case ISO_VD_END:
302 			goto vd_end;
303 
304 		default:
305 			break;
306 		}
307 		if (bp != NULL) {
308 			brelse(bp);
309 			bp = NULL;
310 		}
311 	}
312  vd_end:
313 	if (bp != NULL) {
314 		brelse(bp);
315 		bp = NULL;
316 	}
317 
318 	if (pri == NULL) {
319 		error = EINVAL;
320 		goto out;
321 	}
322 
323 	logical_block_size =
324 		isonum_723 (high_sierra?
325 			    pri_sierra->logical_block_size:
326 			    pri->logical_block_size);
327 
328 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
329 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
330 		error = EINVAL;
331 		goto out;
332 	}
333 
334 	if (logical_block_size < cp->provider->sectorsize) {
335 		printf("cd9660: Unsupported logical block size %u\n",
336 		    logical_block_size);
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 	memcpy(isomp->root, rootp, 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 	rootp = NULL;
375 	pri = NULL;
376 	pri_sierra = NULL;
377 
378 	mp->mnt_data = isomp;
379 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
380 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
381 	MNT_ILOCK(mp);
382 	if (isverified)
383 		mp->mnt_flag |= MNT_VERIFIED;
384 	mp->mnt_flag |= MNT_LOCAL;
385 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
386 	MNT_IUNLOCK(mp);
387 	isomp->im_mountp = mp;
388 	isomp->im_dev = dev;
389 	isomp->im_devvp = devvp;
390 
391 	vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
392 	vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
393 	vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
394 	vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
395 	vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
396 
397 	/* Check the Rock Ridge Extension support */
398 	if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
399 		if ((error = bread(isomp->im_devvp, (isomp->root_extent +
400 		    isonum_711(((struct iso_directory_record *)isomp->root)->
401 		    ext_attr_length)) << (isomp->im_bshift - DEV_BSHIFT),
402 		    isomp->logical_block_size, NOCRED, &bp)) != 0)
403 			goto out;
404 
405 		rootp = (struct iso_directory_record *)bp->b_data;
406 
407 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
408 		    isomp->im_flags |= ISOFSMNT_NORRIP;
409 		} else {
410 		    isomp->im_flags &= ~ISOFSMNT_GENS;
411 		}
412 
413 		/*
414 		 * The contents are valid,
415 		 * but they will get reread as part of another vnode, so...
416 		 */
417 		bp->b_flags |= B_AGE;
418 		brelse(bp);
419 		bp = NULL;
420 		rootp = 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 		memcpy(isomp->root, rootp, 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 		sup = NULL;
476 	}
477 
478 	return 0;
479 out:
480 	if (bp != NULL)
481 		brelse(bp);
482 	if (pribp != NULL)
483 		brelse(pribp);
484 	if (supbp != NULL)
485 		brelse(supbp);
486 	if (cp != NULL) {
487 		g_topology_lock();
488 		g_vfs_close(cp);
489 		g_topology_unlock();
490 	}
491 	if (isomp) {
492 		free(isomp, M_ISOFSMNT);
493 		mp->mnt_data = NULL;
494 	}
495 	dev_rel(dev);
496 	return error;
497 }
498 
499 /*
500  * unmount system call
501  */
502 static int
503 cd9660_unmount(struct mount *mp, int mntflags)
504 {
505 	struct iso_mnt *isomp;
506 	int error, flags = 0;
507 
508 	if (mntflags & MNT_FORCE)
509 		flags |= FORCECLOSE;
510 	if ((error = vflush(mp, 0, flags, curthread)))
511 		return (error);
512 
513 	isomp = VFSTOISOFS(mp);
514 
515 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
516 		if (isomp->im_d2l)
517 			cd9660_iconv->close(isomp->im_d2l);
518 		if (isomp->im_l2d)
519 			cd9660_iconv->close(isomp->im_l2d);
520 	}
521 	g_topology_lock();
522 	g_vfs_close(isomp->im_cp);
523 	g_topology_unlock();
524 	vrele(isomp->im_devvp);
525 	dev_rel(isomp->im_dev);
526 	free(isomp, M_ISOFSMNT);
527 	mp->mnt_data = NULL;
528 	return (error);
529 }
530 
531 /*
532  * Return root of a filesystem
533  */
534 static int
535 cd9660_root(struct mount *mp, int flags, struct vnode **vpp)
536 {
537 	struct iso_mnt *imp = VFSTOISOFS(mp);
538 	struct iso_directory_record *dp =
539 	    (struct iso_directory_record *)imp->root;
540 	cd_ino_t ino = isodirino(dp, imp);
541 
542 	/*
543 	 * With RRIP we must use the `.' entry of the root directory.
544 	 * Simply tell vget, that it's a relocated directory.
545 	 */
546 	return (cd9660_vget_internal(mp, ino, flags, vpp,
547 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
548 }
549 
550 /*
551  * Get filesystem statistics.
552  */
553 static int
554 cd9660_statfs(struct mount *mp, struct statfs *sbp)
555 {
556 	struct iso_mnt *isomp;
557 
558 	isomp = VFSTOISOFS(mp);
559 
560 	sbp->f_bsize = isomp->logical_block_size;
561 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
562 	sbp->f_blocks = isomp->volume_space_size;
563 	sbp->f_bfree = 0; /* total free blocks */
564 	sbp->f_bavail = 0; /* blocks free for non superuser */
565 	sbp->f_files =	0; /* total files */
566 	sbp->f_ffree = 0; /* free file nodes */
567 	return 0;
568 }
569 
570 /*
571  * File handle to vnode
572  *
573  * Have to be really careful about stale file handles:
574  * - check that the inode number is in range
575  * - call iget() to get the locked inode
576  * - check for an unallocated inode (i_mode == 0)
577  * - check that the generation number matches
578  */
579 
580 /* ARGSUSED */
581 static int
582 cd9660_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
583 {
584 	struct ifid ifh;
585 	struct iso_node *ip;
586 	struct vnode *nvp;
587 	int error;
588 
589 	memcpy(&ifh, fhp, sizeof(ifh));
590 
591 #ifdef	ISOFS_DBG
592 	printf("fhtovp: ino %d, start %ld\n",
593 	    ifh.ifid_ino, ifh.ifid_start);
594 #endif
595 
596 	if ((error = VFS_VGET(mp, ifh.ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
597 		*vpp = NULLVP;
598 		return (error);
599 	}
600 	ip = VTOI(nvp);
601 	if (ip->inode.iso_mode == 0) {
602 		vput(nvp);
603 		*vpp = NULLVP;
604 		return (ESTALE);
605 	}
606 	*vpp = nvp;
607 	vnode_create_vobject(*vpp, ip->i_size, curthread);
608 	return (0);
609 }
610 
611 /*
612  * Conform to standard VFS interface; can't vget arbitrary inodes beyond 4GB
613  * into media with current inode scheme and 32-bit ino_t.  This shouldn't be
614  * needed for anything other than nfsd, and who exports a mounted DVD over NFS?
615  */
616 static int
617 cd9660_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
618 {
619 
620 	/*
621 	 * XXXX
622 	 * It would be nice if we didn't always set the `relocated' flag
623 	 * and force the extra read, but I don't want to think about fixing
624 	 * that right now.
625 	 */
626 	return (cd9660_vget_internal(mp, ino, flags, vpp,
627 #if 0
628 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
629 #else
630 	    0,
631 #endif
632 	    (struct iso_directory_record *)0));
633 }
634 
635 /* Use special comparator for full 64-bit ino comparison. */
636 static int
637 cd9660_vfs_hash_cmp(struct vnode *vp, void *pino)
638 {
639 	struct iso_node *ip;
640 	cd_ino_t ino;
641 
642 	ip = VTOI(vp);
643 	ino = *(cd_ino_t *)pino;
644 	return (ip->i_number != ino);
645 }
646 
647 int
648 cd9660_vget_internal(struct mount *mp, cd_ino_t ino, int flags,
649     struct vnode **vpp, int relocated, struct iso_directory_record *isodir)
650 {
651 	struct iso_mnt *imp;
652 	struct iso_node *ip;
653 	struct buf *bp;
654 	struct vnode *vp;
655 	int error;
656 	struct thread *td;
657 
658 	td = curthread;
659 	error = vfs_hash_get(mp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
660 	    &ino);
661 	if (error || *vpp != NULL)
662 		return (error);
663 
664 	/*
665 	 * We must promote to an exclusive lock for vnode creation.  This
666 	 * can happen if lookup is passed LOCKSHARED.
667  	 */
668 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
669 		flags &= ~LK_TYPE_MASK;
670 		flags |= LK_EXCLUSIVE;
671 	}
672 
673 	/*
674 	 * We do not lock vnode creation as it is believed to be too
675 	 * expensive for such rare case as simultaneous creation of vnode
676 	 * for same ino by different processes. We just allow them to race
677 	 * and check later to decide who wins. Let the race begin!
678 	 */
679 
680 	imp = VFSTOISOFS(mp);
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 	ip = malloc(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 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
694 	error = insmntque(vp, mp);
695 	if (error != 0) {
696 		free(ip, M_ISOFSNODE);
697 		*vpp = NULLVP;
698 		return (error);
699 	}
700 	error = vfs_hash_insert(vp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
701 	    &ino);
702 	if (error || *vpp != NULL)
703 		return (error);
704 
705 	if (isodir == NULL) {
706 		int lbn, off;
707 
708 		lbn = lblkno(imp, ino);
709 		if (lbn >= imp->volume_space_size) {
710 			vput(vp);
711 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
712 			return (ESTALE);
713 		}
714 
715 		off = blkoff(imp, ino);
716 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
717 			vput(vp);
718 			printf("fhtovp: crosses block boundary %d\n",
719 			       off + ISO_DIRECTORY_RECORD_SIZE);
720 			return (ESTALE);
721 		}
722 
723 		error = bread(imp->im_devvp,
724 			      lbn << (imp->im_bshift - DEV_BSHIFT),
725 			      imp->logical_block_size, NOCRED, &bp);
726 		if (error) {
727 			vput(vp);
728 			printf("fhtovp: bread error %d\n",error);
729 			return (error);
730 		}
731 		isodir = (struct iso_directory_record *)(bp->b_data + off);
732 
733 		if (off + isonum_711(isodir->length) >
734 		    imp->logical_block_size) {
735 			vput(vp);
736 			brelse(bp);
737 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
738 			       off +isonum_711(isodir->length), off,
739 			       isonum_711(isodir->length));
740 			return (ESTALE);
741 		}
742 
743 #if 0
744 		if (isonum_733(isodir->extent) +
745 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
746 			brelse(bp);
747 			printf("fhtovp: file start miss %d vs %d\n",
748 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
749 			       ifhp->ifid_start);
750 			return (ESTALE);
751 		}
752 #endif
753 	} else
754 		bp = NULL;
755 
756 	ip->i_mnt = imp;
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 != NULL)
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 	brelse(bp);
804 
805 	/*
806 	 * Initialize the associated vnode
807 	 */
808 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
809 	case VFIFO:
810 		vp->v_op = &cd9660_fifoops;
811 		break;
812 	default:
813 		VN_LOCK_ASHARE(vp);
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 	vn_set_state(vp, VSTATE_CONSTRUCTED);
825 	*vpp = vp;
826 	return (0);
827 }
828