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