xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision b3aaa0cc21c63d388230c7ef2a80abd631ff20d5)
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, int flags, struct thread *td)
99 {
100 	struct iso_args args;
101 	int error;
102 
103 	error = copyin(data, &args, sizeof args);
104 	if (error)
105 		return (error);
106 
107 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
108 	ma = mount_arg(ma, "export", &args.export, sizeof args.export);
109 	ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
110 	ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
111 	ma = mount_argf(ma, "ssector", "%u", args.ssector);
112 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
113 	ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
114 	ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
115 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
116 	ma = mount_argb(ma,
117 	    args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
118 	ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
119 
120 	error = kernel_mount(ma, flags);
121 
122 	return (error);
123 }
124 
125 static int
126 cd9660_mount(struct mount *mp, struct thread *td)
127 {
128 	struct vnode *devvp;
129 	char *fspec;
130 	int error;
131 	accmode_t accmode;
132 	struct nameidata ndp;
133 	struct iso_mnt *imp = 0;
134 
135 	/*
136 	 * Unconditionally mount as read-only.
137 	 */
138 	MNT_ILOCK(mp);
139 	mp->mnt_flag |= MNT_RDONLY;
140 	MNT_IUNLOCK(mp);
141 
142 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
143 	if (error)
144 		return (error);
145 
146 	imp = VFSTOISOFS(mp);
147 
148 	if (mp->mnt_flag & MNT_UPDATE) {
149 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
150 			return (0);
151 	}
152 	/*
153 	 * Not an update, or updating the name: look up the name
154 	 * and verify that it refers to a sensible block device.
155 	 */
156 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec, td);
157 	if ((error = namei(&ndp)))
158 		return (error);
159 	NDFREE(&ndp, NDF_ONLY_PNBUF);
160 	devvp = ndp.ni_vp;
161 
162 	if (!vn_isdisk(devvp, &error)) {
163 		vput(devvp);
164 		return (error);
165 	}
166 
167 	/*
168 	 * Verify that user has necessary permissions on the device,
169 	 * or has superuser abilities
170 	 */
171 	accmode = VREAD;
172 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
173 	if (error)
174 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
175 	if (error) {
176 		vput(devvp);
177 		return (error);
178 	}
179 
180 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
181 		error = iso_mountfs(devvp, mp);
182 		if (error)
183 			vrele(devvp);
184 	} else {
185 		if (devvp != imp->im_devvp)
186 			error = EINVAL;	/* needs translation */
187 		vput(devvp);
188 	}
189 	if (error)
190 		return (error);
191 	vfs_mountedfrom(mp, fspec);
192 	return (0);
193 }
194 
195 /*
196  * Common code for mount and mountroot
197  */
198 static int
199 iso_mountfs(devvp, mp)
200 	struct vnode *devvp;
201 	struct mount *mp;
202 {
203 	struct iso_mnt *isomp = (struct iso_mnt *)0;
204 	struct buf *bp = NULL;
205 	struct buf *pribp = NULL, *supbp = NULL;
206 	struct cdev *dev;
207 	int error = EINVAL;
208 	int high_sierra = 0;
209 	int iso_bsize;
210 	int iso_blknum;
211 	int joliet_level;
212 	struct iso_volume_descriptor *vdp = 0;
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 	DROP_GIANT();
225 	g_topology_lock();
226 	error = g_vfs_open(devvp, &cp, "cd9660", 0);
227 	g_topology_unlock();
228 	PICKUP_GIANT();
229 	VOP_UNLOCK(devvp, 0);
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) {
308 			brelse(bp);
309 			bp = NULL;
310 		}
311 	}
312  vd_end:
313 	if (bp) {
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 	rootp = (struct iso_directory_record *)
335 		(high_sierra?
336 		 pri_sierra->root_directory_record:
337 		 pri->root_directory_record);
338 
339 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
340 	isomp->im_cp = cp;
341 	isomp->im_bo = bo;
342 	isomp->logical_block_size = logical_block_size;
343 	isomp->volume_space_size =
344 		isonum_733 (high_sierra?
345 			    pri_sierra->volume_space_size:
346 			    pri->volume_space_size);
347 	isomp->joliet_level = 0;
348 	/*
349 	 * Since an ISO9660 multi-session CD can also access previous
350 	 * sessions, we have to include them into the space consider-
351 	 * ations.  This doesn't yield a very accurate number since
352 	 * parts of the old sessions might be inaccessible now, but we
353 	 * can't do much better.  This is also important for the NFS
354 	 * filehandle validation.
355 	 */
356 	isomp->volume_space_size += ssector;
357 	bcopy (rootp, isomp->root, sizeof isomp->root);
358 	isomp->root_extent = isonum_733 (rootp->extent);
359 	isomp->root_size = isonum_733 (rootp->size);
360 
361 	isomp->im_bmask = logical_block_size - 1;
362 	isomp->im_bshift = ffs(logical_block_size) - 1;
363 
364 	pribp->b_flags |= B_AGE;
365 	brelse(pribp);
366 	pribp = NULL;
367 
368 	mp->mnt_data = isomp;
369 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
370 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
371 	mp->mnt_maxsymlinklen = 0;
372 	MNT_ILOCK(mp);
373 	mp->mnt_flag |= MNT_LOCAL;
374 	mp->mnt_kern_flag |= MNTK_MPSAFE | MNTK_LOOKUP_SHARED;
375 	MNT_IUNLOCK(mp);
376 	isomp->im_mountp = mp;
377 	isomp->im_dev = dev;
378 	isomp->im_devvp = devvp;
379 
380 	vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
381 	vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
382 	vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
383 	vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
384 	vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
385 
386 	/* Check the Rock Ridge Extension support */
387 	if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
388 		if ((error = bread(isomp->im_devvp,
389 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
390 				  (isomp->im_bshift - DEV_BSHIFT),
391 				  isomp->logical_block_size, NOCRED, &bp)) != 0)
392 		    goto out;
393 
394 		rootp = (struct iso_directory_record *)bp->b_data;
395 
396 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
397 		    isomp->im_flags |= ISOFSMNT_NORRIP;
398 		} else {
399 		    isomp->im_flags &= ~ISOFSMNT_GENS;
400 		}
401 
402 		/*
403 		 * The contents are valid,
404 		 * but they will get reread as part of another vnode, so...
405 		 */
406 		bp->b_flags |= B_AGE;
407 		brelse(bp);
408 		bp = NULL;
409 	}
410 
411 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
412 		cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
413 		if (error)
414 			goto out;
415 		cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
416 		if (error)
417 			goto out;
418 		cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
419 		cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
420 	} else {
421 		isomp->im_d2l = NULL;
422 		isomp->im_l2d = NULL;
423 	}
424 
425 	if (high_sierra) {
426 		/* this effectively ignores all the mount flags */
427 		if (bootverbose)
428 			log(LOG_INFO, "cd9660: High Sierra Format\n");
429 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
430 	} else
431 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
432 		  default:
433 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
434 			  break;
435 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
436 			  isomp->iso_ftype = ISO_FTYPE_9660;
437 			  break;
438 		  case 0:
439 			  if (bootverbose)
440 			  	  log(LOG_INFO, "cd9660: RockRidge Extension\n");
441 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
442 			  break;
443 		}
444 
445 	/* Decide whether to use the Joliet descriptor */
446 
447 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
448 		if (bootverbose)
449 			log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
450 			    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);
477 		g_topology_unlock();
478 		PICKUP_GIANT();
479 	}
480 	if (isomp) {
481 		free((caddr_t)isomp, M_ISOFSMNT);
482 		mp->mnt_data = NULL;
483 	}
484 	dev_rel(dev);
485 	return error;
486 }
487 
488 /*
489  * unmount system call
490  */
491 static int
492 cd9660_unmount(mp, mntflags, td)
493 	struct mount *mp;
494 	int mntflags;
495 	struct thread *td;
496 {
497 	struct iso_mnt *isomp;
498 	int error, flags = 0;
499 
500 	if (mntflags & MNT_FORCE)
501 		flags |= FORCECLOSE;
502 	if ((error = vflush(mp, 0, flags, td)))
503 		return (error);
504 
505 	isomp = VFSTOISOFS(mp);
506 
507 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
508 		if (isomp->im_d2l)
509 			cd9660_iconv->close(isomp->im_d2l);
510 		if (isomp->im_l2d)
511 			cd9660_iconv->close(isomp->im_l2d);
512 	}
513 	DROP_GIANT();
514 	g_topology_lock();
515 	g_vfs_close(isomp->im_cp);
516 	g_topology_unlock();
517 	PICKUP_GIANT();
518 	vrele(isomp->im_devvp);
519 	dev_rel(isomp->im_dev);
520 	free((caddr_t)isomp, M_ISOFSMNT);
521 	mp->mnt_data = NULL;
522 	MNT_ILOCK(mp);
523 	mp->mnt_flag &= ~MNT_LOCAL;
524 	MNT_IUNLOCK(mp);
525 	return (error);
526 }
527 
528 /*
529  * Return root of a filesystem
530  */
531 static int
532 cd9660_root(mp, flags, vpp, td)
533 	struct mount *mp;
534 	int flags;
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, flags, 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 /* ARGSUSED */
585 static int
586 cd9660_fhtovp(mp, fhp, vpp)
587 	struct mount *mp;
588 	struct fid *fhp;
589 	struct vnode **vpp;
590 {
591 	struct ifid *ifhp = (struct ifid *)fhp;
592 	struct iso_node *ip;
593 	struct vnode *nvp;
594 	int error;
595 
596 #ifdef	ISOFS_DBG
597 	printf("fhtovp: ino %d, start %ld\n",
598 	       ifhp->ifid_ino, ifhp->ifid_start);
599 #endif
600 
601 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
602 		*vpp = NULLVP;
603 		return (error);
604 	}
605 	ip = VTOI(nvp);
606 	if (ip->inode.iso_mode == 0) {
607 		vput(nvp);
608 		*vpp = NULLVP;
609 		return (ESTALE);
610 	}
611 	*vpp = nvp;
612 	vnode_create_vobject(*vpp, ip->i_size, curthread);
613 	return (0);
614 }
615 
616 static int
617 cd9660_vget(mp, ino, flags, vpp)
618 	struct mount *mp;
619 	ino_t ino;
620 	int flags;
621 	struct vnode **vpp;
622 {
623 
624 	/*
625 	 * XXXX
626 	 * It would be nice if we didn't always set the `relocated' flag
627 	 * and force the extra read, but I don't want to think about fixing
628 	 * that right now.
629 	 */
630 	return (cd9660_vget_internal(mp, ino, flags, vpp,
631 #if 0
632 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
633 #else
634 	    0,
635 #endif
636 	    (struct iso_directory_record *)0));
637 }
638 
639 int
640 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
641 	struct mount *mp;
642 	ino_t ino;
643 	int flags;
644 	struct vnode **vpp;
645 	int relocated;
646 	struct iso_directory_record *isodir;
647 {
648 	struct iso_mnt *imp;
649 	struct iso_node *ip;
650 	struct buf *bp;
651 	struct vnode *vp;
652 	struct cdev *dev;
653 	int error;
654 	struct thread *td;
655 
656 	td = curthread;
657 	error = vfs_hash_get(mp, ino, flags, td, vpp, NULL, NULL);
658 	if (error || *vpp != NULL)
659 		return (error);
660 
661 	/*
662 	 * We must promote to an exclusive lock for vnode creation.  This
663 	 * can happen if lookup is passed LOCKSHARED.
664  	 */
665 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
666 		flags &= ~LK_TYPE_MASK;
667 		flags |= LK_EXCLUSIVE;
668 	}
669 
670 	/*
671 	 * We do not lock vnode creation as it is believed to be too
672 	 * expensive for such rare case as simultaneous creation of vnode
673 	 * for same ino by different processes. We just allow them to race
674 	 * and check later to decide who wins. Let the race begin!
675 	 */
676 
677 	imp = VFSTOISOFS(mp);
678 	dev = imp->im_dev;
679 
680 	/* Allocate a new vnode/iso_node. */
681 	if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
682 		*vpp = NULLVP;
683 		return (error);
684 	}
685 	ip = malloc(sizeof(struct iso_node), M_ISOFSNODE,
686 	    M_WAITOK | M_ZERO);
687 	vp->v_data = ip;
688 	ip->i_vnode = vp;
689 	ip->i_number = ino;
690 
691 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
692 	error = insmntque(vp, mp);
693 	if (error != 0) {
694 		free(ip, M_ISOFSNODE);
695 		*vpp = NULLVP;
696 		return (error);
697 	}
698 	error = vfs_hash_insert(vp, ino, flags, td, vpp, NULL, NULL);
699 	if (error || *vpp != NULL)
700 		return (error);
701 
702 	if (isodir == 0) {
703 		int lbn, off;
704 
705 		lbn = lblkno(imp, ino);
706 		if (lbn >= imp->volume_space_size) {
707 			vput(vp);
708 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
709 			return (ESTALE);
710 		}
711 
712 		off = blkoff(imp, ino);
713 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
714 			vput(vp);
715 			printf("fhtovp: crosses block boundary %d\n",
716 			       off + ISO_DIRECTORY_RECORD_SIZE);
717 			return (ESTALE);
718 		}
719 
720 		error = bread(imp->im_devvp,
721 			      lbn << (imp->im_bshift - DEV_BSHIFT),
722 			      imp->logical_block_size, NOCRED, &bp);
723 		if (error) {
724 			vput(vp);
725 			brelse(bp);
726 			printf("fhtovp: bread error %d\n",error);
727 			return (error);
728 		}
729 		isodir = (struct iso_directory_record *)(bp->b_data + off);
730 
731 		if (off + isonum_711(isodir->length) >
732 		    imp->logical_block_size) {
733 			vput(vp);
734 			if (bp != 0)
735 				brelse(bp);
736 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
737 			       off +isonum_711(isodir->length), off,
738 			       isonum_711(isodir->length));
739 			return (ESTALE);
740 		}
741 
742 #if 0
743 		if (isonum_733(isodir->extent) +
744 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
745 			if (bp != 0)
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 = 0;
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 != 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 		VN_LOCK_ASHARE(vp);
815 		break;
816 	}
817 
818 	if (ip->iso_extent == imp->root_extent)
819 		vp->v_vflag |= VV_ROOT;
820 
821 	/*
822 	 * XXX need generation number?
823 	 */
824 
825 	*vpp = vp;
826 	return (0);
827 }
828