xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision 6b3455a7665208c366849f0b2b3bc916fb97516e)
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/proc.h>
44 #include <sys/kernel.h>
45 #include <sys/vnode.h>
46 #include <sys/mount.h>
47 #include <sys/bio.h>
48 #include <sys/buf.h>
49 #include <sys/cdio.h>
50 #include <sys/conf.h>
51 #include <sys/fcntl.h>
52 #include <sys/malloc.h>
53 #include <sys/stat.h>
54 #include <sys/syslog.h>
55 #include <sys/iconv.h>
56 
57 
58 #include <isofs/cd9660/iso.h>
59 #include <isofs/cd9660/iso_rrip.h>
60 #include <isofs/cd9660/cd9660_node.h>
61 #include <isofs/cd9660/cd9660_mount.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_omount_t	cd9660_omount;
69 static vfs_unmount_t	cd9660_unmount;
70 static vfs_root_t	cd9660_root;
71 static vfs_statfs_t	cd9660_statfs;
72 static vfs_vget_t	cd9660_vget;
73 static vfs_fhtovp_t	cd9660_fhtovp;
74 static vfs_vptofh_t	cd9660_vptofh;
75 
76 static struct vfsops cd9660_vfsops = {
77 	.vfs_fhtovp =		cd9660_fhtovp,
78 	.vfs_init =		cd9660_init,
79 	.vfs_omount =		cd9660_omount,
80 	.vfs_root =		cd9660_root,
81 	.vfs_statfs =		cd9660_statfs,
82 	.vfs_uninit =		cd9660_uninit,
83 	.vfs_unmount =		cd9660_unmount,
84 	.vfs_vget =		cd9660_vget,
85 	.vfs_vptofh =		cd9660_vptofh,
86 };
87 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
88 MODULE_VERSION(cd9660, 1);
89 
90 
91 /*
92  * Called by vfs_mountroot when iso is going to be mounted as root.
93  */
94 
95 static int iso_get_ssector(struct cdev *dev, struct thread *td);
96 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
97 		       struct thread *td, struct iso_args *argp);
98 
99 /*
100  * Try to find the start of the last data track on this CD-ROM.  This
101  * is used to mount the last session of a multi-session CD.  Bail out
102  * and return 0 if we fail, this is always a safe bet.
103  */
104 static int
105 iso_get_ssector(dev, td)
106 	struct cdev *dev;
107 	struct thread *td;
108 {
109 	struct ioc_toc_header h;
110 	struct ioc_read_toc_single_entry t;
111 	int i;
112 	struct cdevsw *bd;
113 	d_ioctl_t *ioctlp;
114 
115 	bd = devsw(dev);
116 	ioctlp = bd->d_ioctl;
117 	if (ioctlp == NULL)
118 		return 0;
119 
120 	if (ioctlp(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
121 		return 0;
122 
123 	for (i = h.ending_track; i >= 0; i--) {
124 		t.address_format = CD_LBA_FORMAT;
125 		t.track = i;
126 		if (ioctlp(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0)
127 			return 0;
128 		if ((t.entry.control & 4) != 0)
129 			/* found a data track */
130 			break;
131 	}
132 
133 	if (i < 0)
134 		return 0;
135 
136 	return ntohl(t.entry.addr.lba);
137 }
138 
139 static int iso_mountroot(struct mount *mp, struct thread *td);
140 
141 static int
142 iso_mountroot(mp, td)
143 	struct mount *mp;
144 	struct thread *td;
145 {
146 	struct iso_args args;
147 	struct vnode *rootvp;
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_omount(mp, path, data, td)
183 	struct mount *mp;
184 	char *path;
185 	caddr_t data;
186 	struct thread *td;
187 {
188 	struct vnode *devvp;
189 	struct iso_args args;
190 	size_t size;
191 	int error;
192 	mode_t accessmode;
193 	struct iso_mnt *imp = 0;
194 	struct nameidata ndp;
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 	struct vnode *devvp;
269 	struct mount *mp;
270 	struct thread *td;
271 	struct iso_args *argp;
272 {
273 	struct iso_mnt *isomp = (struct iso_mnt *)0;
274 	struct buf *bp = NULL;
275 	struct buf *pribp = NULL, *supbp = NULL;
276 	struct cdev *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)
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 					 ISOFSMNT_KICONV);
476 
477 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
478 		cd9660_iconv->open(argp->cs_local, argp->cs_disk, &isomp->im_d2l);
479 		cd9660_iconv->open(argp->cs_disk, argp->cs_local, &isomp->im_l2d);
480 	} else {
481 		isomp->im_d2l = NULL;
482 		isomp->im_l2d = NULL;
483 	}
484 
485 	if (high_sierra) {
486 		/* this effectively ignores all the mount flags */
487 		log(LOG_INFO, "cd9660: High Sierra Format\n");
488 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
489 	} else
490 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
491 		  default:
492 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
493 			  break;
494 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
495 			  isomp->iso_ftype = ISO_FTYPE_9660;
496 			  break;
497 		  case 0:
498 			  log(LOG_INFO, "cd9660: RockRidge Extension\n");
499 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
500 			  break;
501 		}
502 
503 	/* Decide whether to use the Joliet descriptor */
504 
505 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
506 		log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
507 		rootp = (struct iso_directory_record *)
508 			sup->root_directory_record;
509 		bcopy (rootp, isomp->root, sizeof isomp->root);
510 		isomp->root_extent = isonum_733 (rootp->extent);
511 		isomp->root_size = isonum_733 (rootp->size);
512 		isomp->joliet_level = joliet_level;
513 		supbp->b_flags |= B_AGE;
514 	}
515 
516 	if (supbp) {
517 		brelse(supbp);
518 		supbp = NULL;
519 	}
520 
521 	return 0;
522 out:
523 	devvp->v_rdev->si_mountpoint = NULL;
524 	if (bp)
525 		brelse(bp);
526 	if (pribp)
527 		brelse(pribp);
528 	if (supbp)
529 		brelse(supbp);
530 	if (needclose)
531 		(void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
532 	if (isomp) {
533 		free((caddr_t)isomp, M_ISOFSMNT);
534 		mp->mnt_data = (qaddr_t)0;
535 	}
536 	return error;
537 }
538 
539 /*
540  * unmount system call
541  */
542 static int
543 cd9660_unmount(mp, mntflags, td)
544 	struct mount *mp;
545 	int mntflags;
546 	struct thread *td;
547 {
548 	struct iso_mnt *isomp;
549 	int error, flags = 0;
550 
551 	if (mntflags & MNT_FORCE)
552 		flags |= FORCECLOSE;
553 #if 0
554 	mntflushbuf(mp, 0);
555 	if (mntinvalbuf(mp))
556 		return EBUSY;
557 #endif
558 	if ((error = vflush(mp, 0, flags, td)))
559 		return (error);
560 
561 	isomp = VFSTOISOFS(mp);
562 
563 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
564 		if (isomp->im_d2l)
565 			cd9660_iconv->close(isomp->im_d2l);
566 		if (isomp->im_l2d)
567 			cd9660_iconv->close(isomp->im_l2d);
568 	}
569 	isomp->im_devvp->v_rdev->si_mountpoint = NULL;
570 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, td);
571 	vrele(isomp->im_devvp);
572 	free((caddr_t)isomp, M_ISOFSMNT);
573 	mp->mnt_data = (qaddr_t)0;
574 	mp->mnt_flag &= ~MNT_LOCAL;
575 	return (error);
576 }
577 
578 /*
579  * Return root of a filesystem
580  */
581 static int
582 cd9660_root(mp, vpp, td)
583 	struct mount *mp;
584 	struct vnode **vpp;
585 	struct thread *td;
586 {
587 	struct iso_mnt *imp = VFSTOISOFS(mp);
588 	struct iso_directory_record *dp =
589 	    (struct iso_directory_record *)imp->root;
590 	ino_t ino = isodirino(dp, imp);
591 
592 	/*
593 	 * With RRIP we must use the `.' entry of the root directory.
594 	 * Simply tell vget, that it's a relocated directory.
595 	 */
596 	return (cd9660_vget_internal(mp, ino, LK_EXCLUSIVE, vpp,
597 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
598 }
599 
600 /*
601  * Get filesystem statistics.
602  */
603 static int
604 cd9660_statfs(mp, sbp, td)
605 	struct mount *mp;
606 	struct statfs *sbp;
607 	struct thread *td;
608 {
609 	struct iso_mnt *isomp;
610 
611 	isomp = VFSTOISOFS(mp);
612 
613 	sbp->f_bsize = isomp->logical_block_size;
614 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
615 	sbp->f_blocks = isomp->volume_space_size;
616 	sbp->f_bfree = 0; /* total free blocks */
617 	sbp->f_bavail = 0; /* blocks free for non superuser */
618 	sbp->f_files =	0; /* total files */
619 	sbp->f_ffree = 0; /* free file nodes */
620 	if (sbp != &mp->mnt_stat) {
621 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
622 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
623 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
624 	}
625 	return 0;
626 }
627 
628 /*
629  * File handle to vnode
630  *
631  * Have to be really careful about stale file handles:
632  * - check that the inode number is in range
633  * - call iget() to get the locked inode
634  * - check for an unallocated inode (i_mode == 0)
635  * - check that the generation number matches
636  */
637 
638 struct ifid {
639 	u_short	ifid_len;
640 	u_short	ifid_pad;
641 	int	ifid_ino;
642 	long	ifid_start;
643 };
644 
645 /* ARGSUSED */
646 static int
647 cd9660_fhtovp(mp, fhp, vpp)
648 	struct mount *mp;
649 	struct fid *fhp;
650 	struct vnode **vpp;
651 {
652 	struct ifid *ifhp = (struct ifid *)fhp;
653 	struct iso_node *ip;
654 	struct vnode *nvp;
655 	int error;
656 
657 #ifdef	ISOFS_DBG
658 	printf("fhtovp: ino %d, start %ld\n",
659 	       ifhp->ifid_ino, ifhp->ifid_start);
660 #endif
661 
662 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
663 		*vpp = NULLVP;
664 		return (error);
665 	}
666 	ip = VTOI(nvp);
667 	if (ip->inode.iso_mode == 0) {
668 		vput(nvp);
669 		*vpp = NULLVP;
670 		return (ESTALE);
671 	}
672 	*vpp = nvp;
673 	return (0);
674 }
675 
676 static int
677 cd9660_vget(mp, ino, flags, vpp)
678 	struct mount *mp;
679 	ino_t ino;
680 	int flags;
681 	struct vnode **vpp;
682 {
683 
684 	/*
685 	 * XXXX
686 	 * It would be nice if we didn't always set the `relocated' flag
687 	 * and force the extra read, but I don't want to think about fixing
688 	 * that right now.
689 	 */
690 	return (cd9660_vget_internal(mp, ino, flags, vpp,
691 #if 0
692 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
693 #else
694 	    0,
695 #endif
696 	    (struct iso_directory_record *)0));
697 }
698 
699 int
700 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
701 	struct mount *mp;
702 	ino_t ino;
703 	int flags;
704 	struct vnode **vpp;
705 	int relocated;
706 	struct iso_directory_record *isodir;
707 {
708 	struct iso_mnt *imp;
709 	struct iso_node *ip;
710 	struct buf *bp;
711 	struct vnode *vp;
712 	struct cdev *dev;
713 	int error;
714 
715 	imp = VFSTOISOFS(mp);
716 	dev = imp->im_dev;
717 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0)
718 		return (error);
719 	if (*vpp != NULL)
720 		return (0);
721 
722 	/* Allocate a new vnode/iso_node. */
723 	if ((error = getnewvnode("isofs", mp, cd9660_vnodeop_p, &vp)) != 0) {
724 		*vpp = NULLVP;
725 		return (error);
726 	}
727 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
728 	    M_WAITOK | M_ZERO);
729 	vp->v_data = ip;
730 	ip->i_vnode = vp;
731 	ip->i_dev = dev;
732 	ip->i_number = ino;
733 
734 	/*
735 	 * Check to be sure that it did not show up. We have to put it
736 	 * on the hash chain as the cleanup from vput expects to find
737 	 * it there.
738 	 */
739 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0 ||
740 	    *vpp != NULL) {
741 		cd9660_ihashins(ip);
742 		vput(vp);
743 		return (error);
744 	}
745 
746 	/*
747 	 * Put it onto its hash chain and lock it so that other requests for
748 	 * this inode will block if they arrive while we are sleeping waiting
749 	 * for old data structures to be purged or for the contents of the
750 	 * disk portion of this inode to be read.
751 	 */
752 	cd9660_ihashins(ip);
753 
754 	if (isodir == 0) {
755 		int lbn, off;
756 
757 		lbn = lblkno(imp, ino);
758 		if (lbn >= imp->volume_space_size) {
759 			vput(vp);
760 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
761 			return (ESTALE);
762 		}
763 
764 		off = blkoff(imp, ino);
765 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
766 			vput(vp);
767 			printf("fhtovp: crosses block boundary %d\n",
768 			       off + ISO_DIRECTORY_RECORD_SIZE);
769 			return (ESTALE);
770 		}
771 
772 		error = bread(imp->im_devvp,
773 			      lbn << (imp->im_bshift - DEV_BSHIFT),
774 			      imp->logical_block_size, NOCRED, &bp);
775 		if (error) {
776 			vput(vp);
777 			brelse(bp);
778 			printf("fhtovp: bread error %d\n",error);
779 			return (error);
780 		}
781 		isodir = (struct iso_directory_record *)(bp->b_data + off);
782 
783 		if (off + isonum_711(isodir->length) >
784 		    imp->logical_block_size) {
785 			vput(vp);
786 			if (bp != 0)
787 				brelse(bp);
788 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
789 			       off +isonum_711(isodir->length), off,
790 			       isonum_711(isodir->length));
791 			return (ESTALE);
792 		}
793 
794 #if 0
795 		if (isonum_733(isodir->extent) +
796 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
797 			if (bp != 0)
798 				brelse(bp);
799 			printf("fhtovp: file start miss %d vs %d\n",
800 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
801 			       ifhp->ifid_start);
802 			return (ESTALE);
803 		}
804 #endif
805 	} else
806 		bp = 0;
807 
808 	ip->i_mnt = imp;
809 	ip->i_devvp = imp->im_devvp;
810 	VREF(ip->i_devvp);
811 
812 	if (relocated) {
813 		/*
814 		 * On relocated directories we must
815 		 * read the `.' entry out of a dir.
816 		 */
817 		ip->iso_start = ino >> imp->im_bshift;
818 		if (bp != 0)
819 			brelse(bp);
820 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
821 			vput(vp);
822 			return (error);
823 		}
824 		isodir = (struct iso_directory_record *)bp->b_data;
825 	}
826 
827 	ip->iso_extent = isonum_733(isodir->extent);
828 	ip->i_size = isonum_733(isodir->size);
829 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
830 
831 	/*
832 	 * Setup time stamp, attribute
833 	 */
834 	vp->v_type = VNON;
835 	switch (imp->iso_ftype) {
836 	default:	/* ISO_FTYPE_9660 */
837 	    {
838 		struct buf *bp2;
839 		int off;
840 		if ((imp->im_flags & ISOFSMNT_EXTATT)
841 		    && (off = isonum_711(isodir->ext_attr_length)))
842 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
843 				     &bp2);
844 		else
845 			bp2 = NULL;
846 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
847 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
848 		if (bp2)
849 			brelse(bp2);
850 		break;
851 	    }
852 	case ISO_FTYPE_RRIP:
853 		cd9660_rrip_analyze(isodir, ip, imp);
854 		break;
855 	}
856 
857 	if (bp != 0)
858 		brelse(bp);
859 
860 	/*
861 	 * Initialize the associated vnode
862 	 */
863 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
864 	case VFIFO:
865 		vp->v_op = cd9660_fifoop_p;
866 		break;
867 	case VBLK:
868 		vp->v_op = cd9660_specop_p;
869 		break;
870 	case VCHR:
871 		vp->v_op = cd9660_specop_p;
872 		vp = addaliasu(vp, ip->inode.iso_rdev);
873 		ip->i_vnode = vp;
874 		break;
875 	default:
876 		break;
877 	}
878 
879 	if (ip->iso_extent == imp->root_extent)
880 		vp->v_vflag |= VV_ROOT;
881 
882 	/*
883 	 * XXX need generation number?
884 	 */
885 
886 	*vpp = vp;
887 	return (0);
888 }
889 
890 /*
891  * Vnode pointer to File handle
892  */
893 /* ARGSUSED */
894 static int
895 cd9660_vptofh(vp, fhp)
896 	struct vnode *vp;
897 	struct fid *fhp;
898 {
899 	struct iso_node *ip = VTOI(vp);
900 	struct ifid *ifhp;
901 
902 	ifhp = (struct ifid *)fhp;
903 	ifhp->ifid_len = sizeof(struct ifid);
904 
905 	ifhp->ifid_ino = ip->i_number;
906 	ifhp->ifid_start = ip->iso_start;
907 
908 #ifdef	ISOFS_DBG
909 	printf("vptofh: ino %d, start %ld\n",
910 	       ifhp->ifid_ino,ifhp->ifid_start);
911 #endif
912 	return 0;
913 }
914