xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)cd9660_vfsops.c	8.18 (Berkeley) 5/22/95
39  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/bio.h>
52 #include <sys/buf.h>
53 #include <sys/cdio.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
57 #include <sys/stat.h>
58 #include <sys/syslog.h>
59 
60 
61 #include <isofs/cd9660/iso.h>
62 #include <isofs/cd9660/iso_rrip.h>
63 #include <isofs/cd9660/cd9660_node.h>
64 #include <isofs/cd9660/cd9660_mount.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 static vfs_mount_t	cd9660_mount;
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 static vfs_vptofh_t	cd9660_vptofh;
76 
77 static struct vfsops cd9660_vfsops = {
78 	.vfs_fhtovp =		cd9660_fhtovp,
79 	.vfs_init =		cd9660_init,
80 	.vfs_mount =		cd9660_mount,
81 	.vfs_root =		cd9660_root,
82 	.vfs_statfs =		cd9660_statfs,
83 	.vfs_uninit =		cd9660_uninit,
84 	.vfs_unmount =		cd9660_unmount,
85 	.vfs_vget =		cd9660_vget,
86 	.vfs_vptofh =		cd9660_vptofh,
87 };
88 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
89 MODULE_VERSION(cd9660, 1);
90 
91 
92 /*
93  * Called by vfs_mountroot when iso is going to be mounted as root.
94  */
95 
96 static int iso_get_ssector(dev_t dev, struct thread *td);
97 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
98 		       struct thread *td, struct iso_args *argp);
99 
100 /*
101  * Try to find the start of the last data track on this CD-ROM.  This
102  * is used to mount the last session of a multi-session CD.  Bail out
103  * and return 0 if we fail, this is always a safe bet.
104  */
105 static int
106 iso_get_ssector(dev, td)
107 	dev_t dev;
108 	struct thread *td;
109 {
110 	struct ioc_toc_header h;
111 	struct ioc_read_toc_single_entry t;
112 	int i;
113 	struct cdevsw *bd;
114 	d_ioctl_t *ioctlp;
115 
116 	bd = devsw(dev);
117 	ioctlp = bd->d_ioctl;
118 	if (ioctlp == NULL)
119 		return 0;
120 
121 	if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
122 		return 0;
123 
124 	for (i = h.ending_track; i >= 0; i--) {
125 		t.address_format = CD_LBA_FORMAT;
126 		t.track = i;
127 		if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0)
128 			return 0;
129 		if ((t.entry.control & 4) != 0)
130 			/* found a data track */
131 			break;
132 	}
133 
134 	if (i < 0)
135 		return 0;
136 
137 	return ntohl(t.entry.addr.lba);
138 }
139 
140 static int iso_mountroot(struct mount *mp, struct thread *td);
141 
142 static int
143 iso_mountroot(mp, td)
144 	struct mount *mp;
145 	struct thread *td;
146 {
147 	struct iso_args args;
148 	int error;
149 
150 	if ((error = bdevvp(rootdev, &rootvp))) {
151 		printf("iso_mountroot: can't find rootvp\n");
152 		return (error);
153 	}
154 	args.flags = ISOFSMNT_ROOT;
155 
156 	vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, td);
157 	error = VOP_OPEN(rootvp, FREAD, FSCRED, td, -1);
158 	VOP_UNLOCK(rootvp, 0, td);
159 	if (error)
160 		return error;
161 
162 	args.ssector = iso_get_ssector(rootdev, td);
163 
164 	(void)VOP_CLOSE(rootvp, FREAD, NOCRED, td);
165 
166 	if (bootverbose)
167 		printf("iso_mountroot(): using session at block %d\n",
168 		       args.ssector);
169 	if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
170 		return (error);
171 
172 	(void)cd9660_statfs(mp, &mp->mnt_stat, td);
173 	return (0);
174 }
175 
176 /*
177  * VFS Operations.
178  *
179  * mount system call
180  */
181 static int
182 cd9660_mount(mp, path, data, ndp, td)
183 	register struct mount *mp;
184 	char *path;
185 	caddr_t data;
186 	struct nameidata *ndp;
187 	struct thread *td;
188 {
189 	struct vnode *devvp;
190 	struct iso_args args;
191 	size_t size;
192 	int error;
193 	mode_t accessmode;
194 	struct iso_mnt *imp = 0;
195 
196 	if (path == NULL)	/* We are doing the initial root mount */
197 		return (iso_mountroot(mp, td));
198 	if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
199 		return (error);
200 
201 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
202 		return (EROFS);
203 
204 	/*
205 	 * If updating, check whether changing from read-only to
206 	 * read/write; if there is no device name, that's all we do.
207 	 */
208 	if (mp->mnt_flag & MNT_UPDATE) {
209 		imp = VFSTOISOFS(mp);
210 		if (args.fspec == 0)
211 			return (vfs_export(mp, &args.export));
212 	}
213 	/*
214 	 * Not an update, or updating the name: look up the name
215 	 * and verify that it refers to a sensible block device.
216 	 */
217 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
218 	if ((error = namei(ndp)))
219 		return (error);
220 	NDFREE(ndp, NDF_ONLY_PNBUF);
221 	devvp = ndp->ni_vp;
222 
223 	if (!vn_isdisk(devvp, &error)) {
224 		vrele(devvp);
225 		return (error);
226 	}
227 
228 	/*
229 	 * Verify that user has necessary permissions on the device,
230 	 * or has superuser abilities
231 	 */
232 	accessmode = VREAD;
233 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
234 	error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
235 	if (error)
236 		error = suser(td);
237 	if (error) {
238 		vput(devvp);
239 		return (error);
240 	}
241 	VOP_UNLOCK(devvp, 0, td);
242 
243 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
244 		error = iso_mountfs(devvp, mp, td, &args);
245 	} else {
246 		if (devvp != imp->im_devvp)
247 			error = EINVAL;	/* needs translation */
248 		else
249 			vrele(devvp);
250 	}
251 	if (error) {
252 		vrele(devvp);
253 		return error;
254 	}
255 	imp = VFSTOISOFS(mp);
256 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
257 	    &size);
258 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
259 	(void) cd9660_statfs(mp, &mp->mnt_stat, td);
260 	return 0;
261 }
262 
263 /*
264  * Common code for mount and mountroot
265  */
266 static int
267 iso_mountfs(devvp, mp, td, argp)
268 	register struct vnode *devvp;
269 	struct mount *mp;
270 	struct thread *td;
271 	struct iso_args *argp;
272 {
273 	register struct iso_mnt *isomp = (struct iso_mnt *)0;
274 	struct buf *bp = NULL;
275 	struct buf *pribp = NULL, *supbp = NULL;
276 	dev_t dev = devvp->v_rdev;
277 	int error = EINVAL;
278 	int needclose = 0;
279 	int high_sierra = 0;
280 	int iso_bsize;
281 	int iso_blknum;
282 	int joliet_level;
283 	struct iso_volume_descriptor *vdp = 0;
284 	struct iso_primary_descriptor *pri = NULL;
285 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
286 	struct iso_supplementary_descriptor *sup = NULL;
287 	struct iso_directory_record *rootp;
288 	int logical_block_size;
289 
290 	if (!(mp->mnt_flag & MNT_RDONLY))
291 		return EROFS;
292 
293 	/*
294 	 * Disallow multiple mounts of the same device.
295 	 * Disallow mounting of a device that is currently in use
296 	 * (except for root, which might share swap device for miniroot).
297 	 * Flush out any old buffers remaining from a previous use.
298 	 */
299 	if ((error = vfs_mountedon(devvp)))
300 		return error;
301 	if (vcount(devvp) > 1 && devvp != rootvp)
302 		return EBUSY;
303 	if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
304 		return (error);
305 
306 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
307 	error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
308 	VOP_UNLOCK(devvp, 0, td);
309 	if (error)
310 		return error;
311 	if (devvp->v_rdev->si_iosize_max != 0)
312 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
313 	if (mp->mnt_iosize_max > MAXPHYS)
314 		mp->mnt_iosize_max = MAXPHYS;
315 
316 	needclose = 1;
317 
318 	/* This is the "logical sector size".  The standard says this
319 	 * should be 2048 or the physical sector size on the device,
320 	 * whichever is greater.  For now, we'll just use a constant.
321 	 */
322 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
323 
324 	joliet_level = 0;
325 	for (iso_blknum = 16 + argp->ssector;
326 	     iso_blknum < 100 + argp->ssector;
327 	     iso_blknum++) {
328 		if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
329 				  iso_bsize, NOCRED, &bp)) != 0)
330 			goto out;
331 
332 		vdp = (struct iso_volume_descriptor *)bp->b_data;
333 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
334 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
335 				  sizeof vdp->id) != 0) {
336 				error = EINVAL;
337 				goto out;
338 			} else
339 				high_sierra = 1;
340 		}
341 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
342 		case ISO_VD_PRIMARY:
343 			if (pribp == NULL) {
344 				pribp = bp;
345 				bp = NULL;
346 				pri = (struct iso_primary_descriptor *)vdp;
347 				pri_sierra =
348 				  (struct iso_sierra_primary_descriptor *)vdp;
349 			}
350 			break;
351 
352 		case ISO_VD_SUPPLEMENTARY:
353 			if (supbp == NULL) {
354 				supbp = bp;
355 				bp = NULL;
356 				sup = (struct iso_supplementary_descriptor *)vdp;
357 
358 				if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
359 					if (bcmp(sup->escape, "%/@", 3) == 0)
360 						joliet_level = 1;
361 					if (bcmp(sup->escape, "%/C", 3) == 0)
362 						joliet_level = 2;
363 					if (bcmp(sup->escape, "%/E", 3) == 0)
364 						joliet_level = 3;
365 
366 					if ((isonum_711 (sup->flags) & 1) &&
367 					    (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
368 						joliet_level = 0;
369 				}
370 			}
371 			break;
372 
373 		case ISO_VD_END:
374 			goto vd_end;
375 
376 		default:
377 			break;
378 		}
379 		if (bp) {
380 			brelse(bp);
381 			bp = NULL;
382 		}
383 	}
384  vd_end:
385 	if (bp) {
386 		brelse(bp);
387 		bp = NULL;
388 	}
389 
390 	if (pri == NULL) {
391 		error = EINVAL;
392 		goto out;
393 	}
394 
395 	logical_block_size =
396 		isonum_723 (high_sierra?
397 			    pri_sierra->logical_block_size:
398 			    pri->logical_block_size);
399 
400 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
401 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
402 		error = EINVAL;
403 		goto out;
404 	}
405 
406 	rootp = (struct iso_directory_record *)
407 		(high_sierra?
408 		 pri_sierra->root_directory_record:
409 		 pri->root_directory_record);
410 
411 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
412 	isomp->logical_block_size = logical_block_size;
413 	isomp->volume_space_size =
414 		isonum_733 (high_sierra?
415 			    pri_sierra->volume_space_size:
416 			    pri->volume_space_size);
417 	isomp->joliet_level = 0;
418 	/*
419 	 * Since an ISO9660 multi-session CD can also access previous
420 	 * sessions, we have to include them into the space consider-
421 	 * ations.  This doesn't yield a very accurate number since
422 	 * parts of the old sessions might be inaccessible now, but we
423 	 * can't do much better.  This is also important for the NFS
424 	 * filehandle validation.
425 	 */
426 	isomp->volume_space_size += argp->ssector;
427 	bcopy (rootp, isomp->root, sizeof isomp->root);
428 	isomp->root_extent = isonum_733 (rootp->extent);
429 	isomp->root_size = isonum_733 (rootp->size);
430 
431 	isomp->im_bmask = logical_block_size - 1;
432 	isomp->im_bshift = ffs(logical_block_size) - 1;
433 
434 	pribp->b_flags |= B_AGE;
435 	brelse(pribp);
436 	pribp = NULL;
437 
438 	mp->mnt_data = (qaddr_t)isomp;
439 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
440 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
441 	mp->mnt_maxsymlinklen = 0;
442 	mp->mnt_flag |= MNT_LOCAL;
443 	isomp->im_mountp = mp;
444 	isomp->im_dev = dev;
445 	isomp->im_devvp = devvp;
446 
447 	devvp->v_rdev->si_mountpoint = mp;
448 
449 	/* Check the Rock Ridge Extention support */
450 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
451 		if ((error = bread(isomp->im_devvp,
452 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
453 				  (isomp->im_bshift - DEV_BSHIFT),
454 				  isomp->logical_block_size, NOCRED, &bp)) != 0)
455 		    goto out;
456 
457 		rootp = (struct iso_directory_record *)bp->b_data;
458 
459 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
460 		    argp->flags	 |= ISOFSMNT_NORRIP;
461 		} else {
462 		    argp->flags	 &= ~ISOFSMNT_GENS;
463 		}
464 
465 		/*
466 		 * The contents are valid,
467 		 * but they will get reread as part of another vnode, so...
468 		 */
469 		bp->b_flags |= B_AGE;
470 		brelse(bp);
471 		bp = NULL;
472 	}
473 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
474 					 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
475 
476 	if (high_sierra) {
477 		/* this effectively ignores all the mount flags */
478 		log(LOG_INFO, "cd9660: High Sierra Format\n");
479 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
480 	} else
481 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
482 		  default:
483 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
484 			  break;
485 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
486 			  isomp->iso_ftype = ISO_FTYPE_9660;
487 			  break;
488 		  case 0:
489 			  log(LOG_INFO, "cd9660: RockRidge Extension\n");
490 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
491 			  break;
492 		}
493 
494 	/* Decide whether to use the Joliet descriptor */
495 
496 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
497 		log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
498 		rootp = (struct iso_directory_record *)
499 			sup->root_directory_record;
500 		bcopy (rootp, isomp->root, sizeof isomp->root);
501 		isomp->root_extent = isonum_733 (rootp->extent);
502 		isomp->root_size = isonum_733 (rootp->size);
503 		isomp->joliet_level = joliet_level;
504 		supbp->b_flags |= B_AGE;
505 	}
506 
507 	if (supbp) {
508 		brelse(supbp);
509 		supbp = NULL;
510 	}
511 
512 	return 0;
513 out:
514 	devvp->v_rdev->si_mountpoint = NULL;
515 	if (bp)
516 		brelse(bp);
517 	if (pribp)
518 		brelse(pribp);
519 	if (supbp)
520 		brelse(supbp);
521 	if (needclose)
522 		(void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
523 	if (isomp) {
524 		free((caddr_t)isomp, M_ISOFSMNT);
525 		mp->mnt_data = (qaddr_t)0;
526 	}
527 	return error;
528 }
529 
530 /*
531  * unmount system call
532  */
533 static int
534 cd9660_unmount(mp, mntflags, td)
535 	struct mount *mp;
536 	int mntflags;
537 	struct thread *td;
538 {
539 	register struct iso_mnt *isomp;
540 	int error, flags = 0;
541 
542 	if (mntflags & MNT_FORCE)
543 		flags |= FORCECLOSE;
544 #if 0
545 	mntflushbuf(mp, 0);
546 	if (mntinvalbuf(mp))
547 		return EBUSY;
548 #endif
549 	if ((error = vflush(mp, 0, flags)))
550 		return (error);
551 
552 	isomp = VFSTOISOFS(mp);
553 
554 	isomp->im_devvp->v_rdev->si_mountpoint = NULL;
555 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, td);
556 	vrele(isomp->im_devvp);
557 	free((caddr_t)isomp, M_ISOFSMNT);
558 	mp->mnt_data = (qaddr_t)0;
559 	mp->mnt_flag &= ~MNT_LOCAL;
560 	return (error);
561 }
562 
563 /*
564  * Return root of a filesystem
565  */
566 static int
567 cd9660_root(mp, vpp)
568 	struct mount *mp;
569 	struct vnode **vpp;
570 {
571 	struct iso_mnt *imp = VFSTOISOFS(mp);
572 	struct iso_directory_record *dp =
573 	    (struct iso_directory_record *)imp->root;
574 	ino_t ino = isodirino(dp, imp);
575 
576 	/*
577 	 * With RRIP we must use the `.' entry of the root directory.
578 	 * Simply tell vget, that it's a relocated directory.
579 	 */
580 	return (cd9660_vget_internal(mp, ino, LK_EXCLUSIVE, vpp,
581 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
582 }
583 
584 /*
585  * Get filesystem statistics.
586  */
587 static int
588 cd9660_statfs(mp, sbp, td)
589 	struct mount *mp;
590 	register struct statfs *sbp;
591 	struct thread *td;
592 {
593 	register struct iso_mnt *isomp;
594 
595 	isomp = VFSTOISOFS(mp);
596 
597 	sbp->f_bsize = isomp->logical_block_size;
598 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
599 	sbp->f_blocks = isomp->volume_space_size;
600 	sbp->f_bfree = 0; /* total free blocks */
601 	sbp->f_bavail = 0; /* blocks free for non superuser */
602 	sbp->f_files =	0; /* total files */
603 	sbp->f_ffree = 0; /* free file nodes */
604 	if (sbp != &mp->mnt_stat) {
605 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
606 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
607 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
608 	}
609 	return 0;
610 }
611 
612 /*
613  * File handle to vnode
614  *
615  * Have to be really careful about stale file handles:
616  * - check that the inode number is in range
617  * - call iget() to get the locked inode
618  * - check for an unallocated inode (i_mode == 0)
619  * - check that the generation number matches
620  */
621 
622 struct ifid {
623 	u_short	ifid_len;
624 	u_short	ifid_pad;
625 	int	ifid_ino;
626 	long	ifid_start;
627 };
628 
629 /* ARGSUSED */
630 static int
631 cd9660_fhtovp(mp, fhp, vpp)
632 	register struct mount *mp;
633 	struct fid *fhp;
634 	struct vnode **vpp;
635 {
636 	struct ifid *ifhp = (struct ifid *)fhp;
637 	register struct iso_node *ip;
638 	struct vnode *nvp;
639 	int error;
640 
641 #ifdef	ISOFS_DBG
642 	printf("fhtovp: ino %d, start %ld\n",
643 	       ifhp->ifid_ino, ifhp->ifid_start);
644 #endif
645 
646 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
647 		*vpp = NULLVP;
648 		return (error);
649 	}
650 	ip = VTOI(nvp);
651 	if (ip->inode.iso_mode == 0) {
652 		vput(nvp);
653 		*vpp = NULLVP;
654 		return (ESTALE);
655 	}
656 	*vpp = nvp;
657 	return (0);
658 }
659 
660 static int
661 cd9660_vget(mp, ino, flags, vpp)
662 	struct mount *mp;
663 	ino_t ino;
664 	int flags;
665 	struct vnode **vpp;
666 {
667 
668 	/*
669 	 * XXXX
670 	 * It would be nice if we didn't always set the `relocated' flag
671 	 * and force the extra read, but I don't want to think about fixing
672 	 * that right now.
673 	 */
674 	return (cd9660_vget_internal(mp, ino, flags, vpp,
675 #if 0
676 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
677 #else
678 	    0,
679 #endif
680 	    (struct iso_directory_record *)0));
681 }
682 
683 int
684 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
685 	struct mount *mp;
686 	ino_t ino;
687 	int flags;
688 	struct vnode **vpp;
689 	int relocated;
690 	struct iso_directory_record *isodir;
691 {
692 	struct iso_mnt *imp;
693 	struct iso_node *ip;
694 	struct buf *bp;
695 	struct vnode *vp;
696 	dev_t dev;
697 	int error;
698 
699 	imp = VFSTOISOFS(mp);
700 	dev = imp->im_dev;
701 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0)
702 		return (error);
703 	if (*vpp != NULL)
704 		return (0);
705 
706 	/* Allocate a new vnode/iso_node. */
707 	if ((error = getnewvnode("isofs", mp, cd9660_vnodeop_p, &vp)) != 0) {
708 		*vpp = NULLVP;
709 		return (error);
710 	}
711 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
712 	    M_WAITOK | M_ZERO);
713 	vp->v_data = ip;
714 	ip->i_vnode = vp;
715 	ip->i_dev = dev;
716 	ip->i_number = ino;
717 
718 	/*
719 	 * Check to be sure that it did not show up. We have to put it
720 	 * on the hash chain as the cleanup from vput expects to find
721 	 * it there.
722 	 */
723 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0 ||
724 	    *vpp != NULL) {
725 		cd9660_ihashins(ip);
726 		vput(vp);
727 		return (error);
728 	}
729 
730 	/*
731 	 * Put it onto its hash chain and lock it so that other requests for
732 	 * this inode will block if they arrive while we are sleeping waiting
733 	 * for old data structures to be purged or for the contents of the
734 	 * disk portion of this inode to be read.
735 	 */
736 	cd9660_ihashins(ip);
737 
738 	if (isodir == 0) {
739 		int lbn, off;
740 
741 		lbn = lblkno(imp, ino);
742 		if (lbn >= imp->volume_space_size) {
743 			vput(vp);
744 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
745 			return (ESTALE);
746 		}
747 
748 		off = blkoff(imp, ino);
749 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
750 			vput(vp);
751 			printf("fhtovp: crosses block boundary %d\n",
752 			       off + ISO_DIRECTORY_RECORD_SIZE);
753 			return (ESTALE);
754 		}
755 
756 		error = bread(imp->im_devvp,
757 			      lbn << (imp->im_bshift - DEV_BSHIFT),
758 			      imp->logical_block_size, NOCRED, &bp);
759 		if (error) {
760 			vput(vp);
761 			brelse(bp);
762 			printf("fhtovp: bread error %d\n",error);
763 			return (error);
764 		}
765 		isodir = (struct iso_directory_record *)(bp->b_data + off);
766 
767 		if (off + isonum_711(isodir->length) >
768 		    imp->logical_block_size) {
769 			vput(vp);
770 			if (bp != 0)
771 				brelse(bp);
772 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
773 			       off +isonum_711(isodir->length), off,
774 			       isonum_711(isodir->length));
775 			return (ESTALE);
776 		}
777 
778 #if 0
779 		if (isonum_733(isodir->extent) +
780 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
781 			if (bp != 0)
782 				brelse(bp);
783 			printf("fhtovp: file start miss %d vs %d\n",
784 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
785 			       ifhp->ifid_start);
786 			return (ESTALE);
787 		}
788 #endif
789 	} else
790 		bp = 0;
791 
792 	ip->i_mnt = imp;
793 	ip->i_devvp = imp->im_devvp;
794 	VREF(ip->i_devvp);
795 
796 	if (relocated) {
797 		/*
798 		 * On relocated directories we must
799 		 * read the `.' entry out of a dir.
800 		 */
801 		ip->iso_start = ino >> imp->im_bshift;
802 		if (bp != 0)
803 			brelse(bp);
804 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
805 			vput(vp);
806 			return (error);
807 		}
808 		isodir = (struct iso_directory_record *)bp->b_data;
809 	}
810 
811 	ip->iso_extent = isonum_733(isodir->extent);
812 	ip->i_size = isonum_733(isodir->size);
813 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
814 
815 	/*
816 	 * Setup time stamp, attribute
817 	 */
818 	vp->v_type = VNON;
819 	switch (imp->iso_ftype) {
820 	default:	/* ISO_FTYPE_9660 */
821 	    {
822 		struct buf *bp2;
823 		int off;
824 		if ((imp->im_flags & ISOFSMNT_EXTATT)
825 		    && (off = isonum_711(isodir->ext_attr_length)))
826 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
827 				     &bp2);
828 		else
829 			bp2 = NULL;
830 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
831 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
832 		if (bp2)
833 			brelse(bp2);
834 		break;
835 	    }
836 	case ISO_FTYPE_RRIP:
837 		cd9660_rrip_analyze(isodir, ip, imp);
838 		break;
839 	}
840 
841 	if (bp != 0)
842 		brelse(bp);
843 
844 	/*
845 	 * Initialize the associated vnode
846 	 */
847 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
848 	case VFIFO:
849 		vp->v_op = cd9660_fifoop_p;
850 		break;
851 	case VCHR:
852 	case VBLK:
853 		vp->v_op = cd9660_specop_p;
854 		vp = addaliasu(vp, ip->inode.iso_rdev);
855 		ip->i_vnode = vp;
856 		break;
857 	default:
858 		break;
859 	}
860 
861 	if (ip->iso_extent == imp->root_extent)
862 		vp->v_vflag |= VV_ROOT;
863 
864 	/*
865 	 * XXX need generation number?
866 	 */
867 
868 	*vpp = vp;
869 	return (0);
870 }
871 
872 /*
873  * Vnode pointer to File handle
874  */
875 /* ARGSUSED */
876 static int
877 cd9660_vptofh(vp, fhp)
878 	struct vnode *vp;
879 	struct fid *fhp;
880 {
881 	register struct iso_node *ip = VTOI(vp);
882 	register struct ifid *ifhp;
883 
884 	ifhp = (struct ifid *)fhp;
885 	ifhp->ifid_len = sizeof(struct ifid);
886 
887 	ifhp->ifid_ino = ip->i_number;
888 	ifhp->ifid_start = ip->iso_start;
889 
890 #ifdef	ISOFS_DBG
891 	printf("vptofh: ino %d, start %ld\n",
892 	       ifhp->ifid_ino,ifhp->ifid_start);
893 #endif
894 	return 0;
895 }
896