xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision 5203edcdc553fda6caa1da8826a89b1a02dad1bf)
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_mount_t	cd9660_mount;
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_mount =		cd9660_mount,
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 	int error;
148 
149 	if ((error = bdevvp(rootdev, &rootvp))) {
150 		printf("iso_mountroot: can't find rootvp\n");
151 		return (error);
152 	}
153 	args.flags = ISOFSMNT_ROOT;
154 
155 	vn_lock(rootvp, LK_EXCLUSIVE | LK_RETRY, td);
156 	error = VOP_OPEN(rootvp, FREAD, FSCRED, td, -1);
157 	VOP_UNLOCK(rootvp, 0, td);
158 	if (error)
159 		return error;
160 
161 	args.ssector = iso_get_ssector(rootdev, td);
162 
163 	(void)VOP_CLOSE(rootvp, FREAD, NOCRED, td);
164 
165 	if (bootverbose)
166 		printf("iso_mountroot(): using session at block %d\n",
167 		       args.ssector);
168 	if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
169 		return (error);
170 
171 	(void)cd9660_statfs(mp, &mp->mnt_stat, td);
172 	return (0);
173 }
174 
175 /*
176  * VFS Operations.
177  *
178  * mount system call
179  */
180 static int
181 cd9660_mount(mp, path, data, ndp, td)
182 	struct mount *mp;
183 	char *path;
184 	caddr_t data;
185 	struct nameidata *ndp;
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 
195 	if (path == NULL)	/* We are doing the initial root mount */
196 		return (iso_mountroot(mp, td));
197 	if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
198 		return (error);
199 
200 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
201 		return (EROFS);
202 
203 	/*
204 	 * If updating, check whether changing from read-only to
205 	 * read/write; if there is no device name, that's all we do.
206 	 */
207 	if (mp->mnt_flag & MNT_UPDATE) {
208 		imp = VFSTOISOFS(mp);
209 		if (args.fspec == 0)
210 			return (vfs_export(mp, &args.export));
211 	}
212 	/*
213 	 * Not an update, or updating the name: look up the name
214 	 * and verify that it refers to a sensible block device.
215 	 */
216 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
217 	if ((error = namei(ndp)))
218 		return (error);
219 	NDFREE(ndp, NDF_ONLY_PNBUF);
220 	devvp = ndp->ni_vp;
221 
222 	if (!vn_isdisk(devvp, &error)) {
223 		vrele(devvp);
224 		return (error);
225 	}
226 
227 	/*
228 	 * Verify that user has necessary permissions on the device,
229 	 * or has superuser abilities
230 	 */
231 	accessmode = VREAD;
232 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
233 	error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
234 	if (error)
235 		error = suser(td);
236 	if (error) {
237 		vput(devvp);
238 		return (error);
239 	}
240 	VOP_UNLOCK(devvp, 0, td);
241 
242 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
243 		error = iso_mountfs(devvp, mp, td, &args);
244 	} else {
245 		if (devvp != imp->im_devvp)
246 			error = EINVAL;	/* needs translation */
247 		else
248 			vrele(devvp);
249 	}
250 	if (error) {
251 		vrele(devvp);
252 		return error;
253 	}
254 	imp = VFSTOISOFS(mp);
255 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
256 	    &size);
257 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
258 	(void) cd9660_statfs(mp, &mp->mnt_stat, td);
259 	return 0;
260 }
261 
262 /*
263  * Common code for mount and mountroot
264  */
265 static int
266 iso_mountfs(devvp, mp, td, argp)
267 	struct vnode *devvp;
268 	struct mount *mp;
269 	struct thread *td;
270 	struct iso_args *argp;
271 {
272 	struct iso_mnt *isomp = (struct iso_mnt *)0;
273 	struct buf *bp = NULL;
274 	struct buf *pribp = NULL, *supbp = NULL;
275 	struct cdev *dev = devvp->v_rdev;
276 	int error = EINVAL;
277 	int needclose = 0;
278 	int high_sierra = 0;
279 	int iso_bsize;
280 	int iso_blknum;
281 	int joliet_level;
282 	struct iso_volume_descriptor *vdp = 0;
283 	struct iso_primary_descriptor *pri = NULL;
284 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
285 	struct iso_supplementary_descriptor *sup = NULL;
286 	struct iso_directory_record *rootp;
287 	int logical_block_size;
288 
289 	if (!(mp->mnt_flag & MNT_RDONLY))
290 		return EROFS;
291 
292 	/*
293 	 * Disallow multiple mounts of the same device.
294 	 * Disallow mounting of a device that is currently in use
295 	 * (except for root, which might share swap device for miniroot).
296 	 * Flush out any old buffers remaining from a previous use.
297 	 */
298 	if ((error = vfs_mountedon(devvp)))
299 		return error;
300 	if (vcount(devvp) > 1 && devvp != rootvp)
301 		return EBUSY;
302 	if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
303 		return (error);
304 
305 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
306 	error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
307 	VOP_UNLOCK(devvp, 0, td);
308 	if (error)
309 		return error;
310 	if (devvp->v_rdev->si_iosize_max != 0)
311 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
312 	if (mp->mnt_iosize_max > MAXPHYS)
313 		mp->mnt_iosize_max = MAXPHYS;
314 
315 	needclose = 1;
316 
317 	/* This is the "logical sector size".  The standard says this
318 	 * should be 2048 or the physical sector size on the device,
319 	 * whichever is greater.  For now, we'll just use a constant.
320 	 */
321 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
322 
323 	joliet_level = 0;
324 	for (iso_blknum = 16 + argp->ssector;
325 	     iso_blknum < 100 + argp->ssector;
326 	     iso_blknum++) {
327 		if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
328 				  iso_bsize, NOCRED, &bp)) != 0)
329 			goto out;
330 
331 		vdp = (struct iso_volume_descriptor *)bp->b_data;
332 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
333 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
334 				  sizeof vdp->id) != 0) {
335 				error = EINVAL;
336 				goto out;
337 			} else
338 				high_sierra = 1;
339 		}
340 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
341 		case ISO_VD_PRIMARY:
342 			if (pribp == NULL) {
343 				pribp = bp;
344 				bp = NULL;
345 				pri = (struct iso_primary_descriptor *)vdp;
346 				pri_sierra =
347 				  (struct iso_sierra_primary_descriptor *)vdp;
348 			}
349 			break;
350 
351 		case ISO_VD_SUPPLEMENTARY:
352 			if (supbp == NULL) {
353 				supbp = bp;
354 				bp = NULL;
355 				sup = (struct iso_supplementary_descriptor *)vdp;
356 
357 				if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
358 					if (bcmp(sup->escape, "%/@", 3) == 0)
359 						joliet_level = 1;
360 					if (bcmp(sup->escape, "%/C", 3) == 0)
361 						joliet_level = 2;
362 					if (bcmp(sup->escape, "%/E", 3) == 0)
363 						joliet_level = 3;
364 
365 					if ((isonum_711 (sup->flags) & 1) &&
366 					    (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
367 						joliet_level = 0;
368 				}
369 			}
370 			break;
371 
372 		case ISO_VD_END:
373 			goto vd_end;
374 
375 		default:
376 			break;
377 		}
378 		if (bp) {
379 			brelse(bp);
380 			bp = NULL;
381 		}
382 	}
383  vd_end:
384 	if (bp) {
385 		brelse(bp);
386 		bp = NULL;
387 	}
388 
389 	if (pri == NULL) {
390 		error = EINVAL;
391 		goto out;
392 	}
393 
394 	logical_block_size =
395 		isonum_723 (high_sierra?
396 			    pri_sierra->logical_block_size:
397 			    pri->logical_block_size);
398 
399 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
400 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
401 		error = EINVAL;
402 		goto out;
403 	}
404 
405 	rootp = (struct iso_directory_record *)
406 		(high_sierra?
407 		 pri_sierra->root_directory_record:
408 		 pri->root_directory_record);
409 
410 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
411 	isomp->logical_block_size = logical_block_size;
412 	isomp->volume_space_size =
413 		isonum_733 (high_sierra?
414 			    pri_sierra->volume_space_size:
415 			    pri->volume_space_size);
416 	isomp->joliet_level = 0;
417 	/*
418 	 * Since an ISO9660 multi-session CD can also access previous
419 	 * sessions, we have to include them into the space consider-
420 	 * ations.  This doesn't yield a very accurate number since
421 	 * parts of the old sessions might be inaccessible now, but we
422 	 * can't do much better.  This is also important for the NFS
423 	 * filehandle validation.
424 	 */
425 	isomp->volume_space_size += argp->ssector;
426 	bcopy (rootp, isomp->root, sizeof isomp->root);
427 	isomp->root_extent = isonum_733 (rootp->extent);
428 	isomp->root_size = isonum_733 (rootp->size);
429 
430 	isomp->im_bmask = logical_block_size - 1;
431 	isomp->im_bshift = ffs(logical_block_size) - 1;
432 
433 	pribp->b_flags |= B_AGE;
434 	brelse(pribp);
435 	pribp = NULL;
436 
437 	mp->mnt_data = (qaddr_t)isomp;
438 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
439 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
440 	mp->mnt_maxsymlinklen = 0;
441 	mp->mnt_flag |= MNT_LOCAL;
442 	isomp->im_mountp = mp;
443 	isomp->im_dev = dev;
444 	isomp->im_devvp = devvp;
445 
446 	devvp->v_rdev->si_mountpoint = mp;
447 
448 	/* Check the Rock Ridge Extention support */
449 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
450 		if ((error = bread(isomp->im_devvp,
451 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
452 				  (isomp->im_bshift - DEV_BSHIFT),
453 				  isomp->logical_block_size, NOCRED, &bp)) != 0)
454 		    goto out;
455 
456 		rootp = (struct iso_directory_record *)bp->b_data;
457 
458 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
459 		    argp->flags	 |= ISOFSMNT_NORRIP;
460 		} else {
461 		    argp->flags	 &= ~ISOFSMNT_GENS;
462 		}
463 
464 		/*
465 		 * The contents are valid,
466 		 * but they will get reread as part of another vnode, so...
467 		 */
468 		bp->b_flags |= B_AGE;
469 		brelse(bp);
470 		bp = NULL;
471 	}
472 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
473 					 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET |
474 					 ISOFSMNT_KICONV);
475 
476 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
477 		cd9660_iconv->open(argp->cs_local, argp->cs_disk, &isomp->im_d2l);
478 		cd9660_iconv->open(argp->cs_disk, argp->cs_local, &isomp->im_l2d);
479 	} else {
480 		isomp->im_d2l = NULL;
481 		isomp->im_l2d = NULL;
482 	}
483 
484 	if (high_sierra) {
485 		/* this effectively ignores all the mount flags */
486 		log(LOG_INFO, "cd9660: High Sierra Format\n");
487 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
488 	} else
489 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
490 		  default:
491 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
492 			  break;
493 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
494 			  isomp->iso_ftype = ISO_FTYPE_9660;
495 			  break;
496 		  case 0:
497 			  log(LOG_INFO, "cd9660: RockRidge Extension\n");
498 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
499 			  break;
500 		}
501 
502 	/* Decide whether to use the Joliet descriptor */
503 
504 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
505 		log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
506 		rootp = (struct iso_directory_record *)
507 			sup->root_directory_record;
508 		bcopy (rootp, isomp->root, sizeof isomp->root);
509 		isomp->root_extent = isonum_733 (rootp->extent);
510 		isomp->root_size = isonum_733 (rootp->size);
511 		isomp->joliet_level = joliet_level;
512 		supbp->b_flags |= B_AGE;
513 	}
514 
515 	if (supbp) {
516 		brelse(supbp);
517 		supbp = NULL;
518 	}
519 
520 	return 0;
521 out:
522 	devvp->v_rdev->si_mountpoint = NULL;
523 	if (bp)
524 		brelse(bp);
525 	if (pribp)
526 		brelse(pribp);
527 	if (supbp)
528 		brelse(supbp);
529 	if (needclose)
530 		(void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
531 	if (isomp) {
532 		free((caddr_t)isomp, M_ISOFSMNT);
533 		mp->mnt_data = (qaddr_t)0;
534 	}
535 	return error;
536 }
537 
538 /*
539  * unmount system call
540  */
541 static int
542 cd9660_unmount(mp, mntflags, td)
543 	struct mount *mp;
544 	int mntflags;
545 	struct thread *td;
546 {
547 	struct iso_mnt *isomp;
548 	int error, flags = 0;
549 
550 	if (mntflags & MNT_FORCE)
551 		flags |= FORCECLOSE;
552 #if 0
553 	mntflushbuf(mp, 0);
554 	if (mntinvalbuf(mp))
555 		return EBUSY;
556 #endif
557 	if ((error = vflush(mp, 0, flags)))
558 		return (error);
559 
560 	isomp = VFSTOISOFS(mp);
561 
562 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
563 		if (isomp->im_d2l)
564 			cd9660_iconv->close(isomp->im_d2l);
565 		if (isomp->im_l2d)
566 			cd9660_iconv->close(isomp->im_l2d);
567 	}
568 	isomp->im_devvp->v_rdev->si_mountpoint = NULL;
569 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, td);
570 	vrele(isomp->im_devvp);
571 	free((caddr_t)isomp, M_ISOFSMNT);
572 	mp->mnt_data = (qaddr_t)0;
573 	mp->mnt_flag &= ~MNT_LOCAL;
574 	return (error);
575 }
576 
577 /*
578  * Return root of a filesystem
579  */
580 static int
581 cd9660_root(mp, vpp)
582 	struct mount *mp;
583 	struct vnode **vpp;
584 {
585 	struct iso_mnt *imp = VFSTOISOFS(mp);
586 	struct iso_directory_record *dp =
587 	    (struct iso_directory_record *)imp->root;
588 	ino_t ino = isodirino(dp, imp);
589 
590 	/*
591 	 * With RRIP we must use the `.' entry of the root directory.
592 	 * Simply tell vget, that it's a relocated directory.
593 	 */
594 	return (cd9660_vget_internal(mp, ino, LK_EXCLUSIVE, vpp,
595 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
596 }
597 
598 /*
599  * Get filesystem statistics.
600  */
601 static int
602 cd9660_statfs(mp, sbp, td)
603 	struct mount *mp;
604 	struct statfs *sbp;
605 	struct thread *td;
606 {
607 	struct iso_mnt *isomp;
608 
609 	isomp = VFSTOISOFS(mp);
610 
611 	sbp->f_bsize = isomp->logical_block_size;
612 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
613 	sbp->f_blocks = isomp->volume_space_size;
614 	sbp->f_bfree = 0; /* total free blocks */
615 	sbp->f_bavail = 0; /* blocks free for non superuser */
616 	sbp->f_files =	0; /* total files */
617 	sbp->f_ffree = 0; /* free file nodes */
618 	if (sbp != &mp->mnt_stat) {
619 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
620 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
621 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
622 	}
623 	return 0;
624 }
625 
626 /*
627  * File handle to vnode
628  *
629  * Have to be really careful about stale file handles:
630  * - check that the inode number is in range
631  * - call iget() to get the locked inode
632  * - check for an unallocated inode (i_mode == 0)
633  * - check that the generation number matches
634  */
635 
636 struct ifid {
637 	u_short	ifid_len;
638 	u_short	ifid_pad;
639 	int	ifid_ino;
640 	long	ifid_start;
641 };
642 
643 /* ARGSUSED */
644 static int
645 cd9660_fhtovp(mp, fhp, vpp)
646 	struct mount *mp;
647 	struct fid *fhp;
648 	struct vnode **vpp;
649 {
650 	struct ifid *ifhp = (struct ifid *)fhp;
651 	struct iso_node *ip;
652 	struct vnode *nvp;
653 	int error;
654 
655 #ifdef	ISOFS_DBG
656 	printf("fhtovp: ino %d, start %ld\n",
657 	       ifhp->ifid_ino, ifhp->ifid_start);
658 #endif
659 
660 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
661 		*vpp = NULLVP;
662 		return (error);
663 	}
664 	ip = VTOI(nvp);
665 	if (ip->inode.iso_mode == 0) {
666 		vput(nvp);
667 		*vpp = NULLVP;
668 		return (ESTALE);
669 	}
670 	*vpp = nvp;
671 	return (0);
672 }
673 
674 static int
675 cd9660_vget(mp, ino, flags, vpp)
676 	struct mount *mp;
677 	ino_t ino;
678 	int flags;
679 	struct vnode **vpp;
680 {
681 
682 	/*
683 	 * XXXX
684 	 * It would be nice if we didn't always set the `relocated' flag
685 	 * and force the extra read, but I don't want to think about fixing
686 	 * that right now.
687 	 */
688 	return (cd9660_vget_internal(mp, ino, flags, vpp,
689 #if 0
690 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
691 #else
692 	    0,
693 #endif
694 	    (struct iso_directory_record *)0));
695 }
696 
697 int
698 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
699 	struct mount *mp;
700 	ino_t ino;
701 	int flags;
702 	struct vnode **vpp;
703 	int relocated;
704 	struct iso_directory_record *isodir;
705 {
706 	struct iso_mnt *imp;
707 	struct iso_node *ip;
708 	struct buf *bp;
709 	struct vnode *vp;
710 	struct cdev *dev;
711 	int error;
712 
713 	imp = VFSTOISOFS(mp);
714 	dev = imp->im_dev;
715 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0)
716 		return (error);
717 	if (*vpp != NULL)
718 		return (0);
719 
720 	/* Allocate a new vnode/iso_node. */
721 	if ((error = getnewvnode("isofs", mp, cd9660_vnodeop_p, &vp)) != 0) {
722 		*vpp = NULLVP;
723 		return (error);
724 	}
725 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
726 	    M_WAITOK | M_ZERO);
727 	vp->v_data = ip;
728 	ip->i_vnode = vp;
729 	ip->i_dev = dev;
730 	ip->i_number = ino;
731 
732 	/*
733 	 * Check to be sure that it did not show up. We have to put it
734 	 * on the hash chain as the cleanup from vput expects to find
735 	 * it there.
736 	 */
737 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0 ||
738 	    *vpp != NULL) {
739 		cd9660_ihashins(ip);
740 		vput(vp);
741 		return (error);
742 	}
743 
744 	/*
745 	 * Put it onto its hash chain and lock it so that other requests for
746 	 * this inode will block if they arrive while we are sleeping waiting
747 	 * for old data structures to be purged or for the contents of the
748 	 * disk portion of this inode to be read.
749 	 */
750 	cd9660_ihashins(ip);
751 
752 	if (isodir == 0) {
753 		int lbn, off;
754 
755 		lbn = lblkno(imp, ino);
756 		if (lbn >= imp->volume_space_size) {
757 			vput(vp);
758 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
759 			return (ESTALE);
760 		}
761 
762 		off = blkoff(imp, ino);
763 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
764 			vput(vp);
765 			printf("fhtovp: crosses block boundary %d\n",
766 			       off + ISO_DIRECTORY_RECORD_SIZE);
767 			return (ESTALE);
768 		}
769 
770 		error = bread(imp->im_devvp,
771 			      lbn << (imp->im_bshift - DEV_BSHIFT),
772 			      imp->logical_block_size, NOCRED, &bp);
773 		if (error) {
774 			vput(vp);
775 			brelse(bp);
776 			printf("fhtovp: bread error %d\n",error);
777 			return (error);
778 		}
779 		isodir = (struct iso_directory_record *)(bp->b_data + off);
780 
781 		if (off + isonum_711(isodir->length) >
782 		    imp->logical_block_size) {
783 			vput(vp);
784 			if (bp != 0)
785 				brelse(bp);
786 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
787 			       off +isonum_711(isodir->length), off,
788 			       isonum_711(isodir->length));
789 			return (ESTALE);
790 		}
791 
792 #if 0
793 		if (isonum_733(isodir->extent) +
794 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
795 			if (bp != 0)
796 				brelse(bp);
797 			printf("fhtovp: file start miss %d vs %d\n",
798 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
799 			       ifhp->ifid_start);
800 			return (ESTALE);
801 		}
802 #endif
803 	} else
804 		bp = 0;
805 
806 	ip->i_mnt = imp;
807 	ip->i_devvp = imp->im_devvp;
808 	VREF(ip->i_devvp);
809 
810 	if (relocated) {
811 		/*
812 		 * On relocated directories we must
813 		 * read the `.' entry out of a dir.
814 		 */
815 		ip->iso_start = ino >> imp->im_bshift;
816 		if (bp != 0)
817 			brelse(bp);
818 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
819 			vput(vp);
820 			return (error);
821 		}
822 		isodir = (struct iso_directory_record *)bp->b_data;
823 	}
824 
825 	ip->iso_extent = isonum_733(isodir->extent);
826 	ip->i_size = isonum_733(isodir->size);
827 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
828 
829 	/*
830 	 * Setup time stamp, attribute
831 	 */
832 	vp->v_type = VNON;
833 	switch (imp->iso_ftype) {
834 	default:	/* ISO_FTYPE_9660 */
835 	    {
836 		struct buf *bp2;
837 		int off;
838 		if ((imp->im_flags & ISOFSMNT_EXTATT)
839 		    && (off = isonum_711(isodir->ext_attr_length)))
840 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
841 				     &bp2);
842 		else
843 			bp2 = NULL;
844 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
845 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
846 		if (bp2)
847 			brelse(bp2);
848 		break;
849 	    }
850 	case ISO_FTYPE_RRIP:
851 		cd9660_rrip_analyze(isodir, ip, imp);
852 		break;
853 	}
854 
855 	if (bp != 0)
856 		brelse(bp);
857 
858 	/*
859 	 * Initialize the associated vnode
860 	 */
861 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
862 	case VFIFO:
863 		vp->v_op = cd9660_fifoop_p;
864 		break;
865 	case VBLK:
866 		vp->v_op = cd9660_specop_p;
867 		break;
868 	case VCHR:
869 		vp->v_op = cd9660_specop_p;
870 		vp = addaliasu(vp, ip->inode.iso_rdev);
871 		ip->i_vnode = vp;
872 		break;
873 	default:
874 		break;
875 	}
876 
877 	if (ip->iso_extent == imp->root_extent)
878 		vp->v_vflag |= VV_ROOT;
879 
880 	/*
881 	 * XXX need generation number?
882 	 */
883 
884 	*vpp = vp;
885 	return (0);
886 }
887 
888 /*
889  * Vnode pointer to File handle
890  */
891 /* ARGSUSED */
892 static int
893 cd9660_vptofh(vp, fhp)
894 	struct vnode *vp;
895 	struct fid *fhp;
896 {
897 	struct iso_node *ip = VTOI(vp);
898 	struct ifid *ifhp;
899 
900 	ifhp = (struct ifid *)fhp;
901 	ifhp->ifid_len = sizeof(struct ifid);
902 
903 	ifhp->ifid_ino = ip->i_number;
904 	ifhp->ifid_start = ip->iso_start;
905 
906 #ifdef	ISOFS_DBG
907 	printf("vptofh: ino %d, start %ld\n",
908 	       ifhp->ifid_ino,ifhp->ifid_start);
909 #endif
910 	return 0;
911 }
912