xref: /openbsd-src/sys/isofs/cd9660/cd9660_vfsops.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1 /*	$OpenBSD: cd9660_vfsops.c,v 1.22 2001/06/23 02:14:22 csapuntz Exp $	*/
2 /*	$NetBSD: cd9660_vfsops.c,v 1.26 1997/06/13 15:38:58 pk Exp $	*/
3 
4 /*-
5  * Copyright (c) 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley
9  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
10  * Support code is derived from software contributed to Berkeley
11  * by Atsushi Murai (amurai@spec.co.jp).
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	@(#)cd9660_vfsops.c	8.9 (Berkeley) 12/5/94
42  */
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 <miscfs/specfs/specdev.h>
51 #include <sys/mount.h>
52 #include <sys/buf.h>
53 #include <sys/file.h>
54 #include <sys/disklabel.h>
55 #include <sys/ioctl.h>
56 #include <sys/errno.h>
57 #include <sys/malloc.h>
58 #include <sys/stat.h>
59 
60 #define	b_cylin	b_resid
61 
62 #include <isofs/cd9660/iso.h>
63 #include <isofs/cd9660/cd9660_extern.h>
64 #include <isofs/cd9660/iso_rrip.h>
65 #include <isofs/cd9660/cd9660_node.h>
66 #include <isofs/cd9660/cd9660_mount.h>
67 
68 struct vfsops cd9660_vfsops = {
69 	cd9660_mount,
70 	cd9660_start,
71 	cd9660_unmount,
72 	cd9660_root,
73 	cd9660_quotactl,
74 	cd9660_statfs,
75 	cd9660_sync,
76 	cd9660_vget,
77 	cd9660_fhtovp,
78 	cd9660_vptofh,
79 	cd9660_init,
80 	cd9660_sysctl,
81 	cd9660_check_export
82 };
83 
84 /*
85  * Called by vfs_mountroot when iso is going to be mounted as root.
86  */
87 
88 static	int iso_mountfs __P((struct vnode *devvp, struct mount *mp,
89     struct proc *p, struct iso_args *argp));
90 int	iso_disklabelspoof __P((dev_t dev, void (*strat) __P((struct buf *)),
91     struct disklabel *lp));
92 
93 int
94 cd9660_mountroot()
95 {
96 	struct mount *mp;
97 	extern struct vnode *rootvp;
98 	struct proc *p = curproc;	/* XXX */
99 	int error;
100 	struct iso_args args;
101 
102 	/*
103 	 * Get vnodes for swapdev and rootdev.
104 	 */
105 	if ((error = bdevvp(swapdev, &swapdev_vp)) ||
106 	    (error = bdevvp(rootdev, &rootvp))) {
107 		printf("cd9660_mountroot: can't setup bdevvp's");
108                 return (error);
109         }
110 
111 	if ((error = vfs_rootmountalloc("cd9660", "root_device", &mp)) != 0)
112 		return (error);
113 	args.flags = ISOFSMNT_ROOT;
114 	if ((error = iso_mountfs(rootvp, mp, p, &args)) != 0) {
115 		mp->mnt_vfc->vfc_refcount--;
116 		vfs_unbusy(mp, p);
117                 free(mp, M_MOUNT);
118                 return (error);
119         }
120 	simple_lock(&mountlist_slock);
121         CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
122 	simple_unlock(&mountlist_slock);
123         (void)cd9660_statfs(mp, &mp->mnt_stat, p);
124 	vfs_unbusy(mp, p);
125         return (0);
126 }
127 
128 /*
129  * VFS Operations.
130  *
131  * mount system call
132  */
133 int
134 cd9660_mount(mp, path, data, ndp, p)
135 	register struct mount *mp;
136 	const char *path;
137 	void *data;
138 	struct nameidata *ndp;
139 	struct proc *p;
140 {
141 	struct vnode *devvp;
142 	struct iso_args args;
143 	size_t size;
144 	int error;
145 	struct iso_mnt *imp = NULL;
146 
147 	error = copyin(data, &args, sizeof (struct iso_args));
148 	if (error)
149 		return (error);
150 
151 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
152 		return (EROFS);
153 
154 	/*
155 	 * If updating, check whether changing from read-only to
156 	 * read/write; if there is no device name, that's all we do.
157 	 */
158 	if (mp->mnt_flag & MNT_UPDATE) {
159 		imp = VFSTOISOFS(mp);
160 		if (args.fspec == 0)
161 			return (vfs_export(mp, &imp->im_export, &args.export));
162 	}
163 	/*
164 	 * Not an update, or updating the name: look up the name
165 	 * and verify that it refers to a sensible block device.
166 	 */
167 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
168 	if ((error = namei(ndp)) != 0)
169 		return (error);
170 	devvp = ndp->ni_vp;
171 
172 	if (devvp->v_type != VBLK) {
173 		vrele(devvp);
174 		return (ENOTBLK);
175 	}
176 	if (major(devvp->v_rdev) >= nblkdev) {
177 		vrele(devvp);
178 		return (ENXIO);
179 	}
180 	/*
181 	 * If mount by non-root, then verify that user has necessary
182 	 * permissions on the device.
183 	 */
184 	if (p->p_ucred->cr_uid != 0) {
185 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, p);
186 		error = VOP_ACCESS(devvp, VREAD, p->p_ucred, p);
187 		if (error) {
188 			vput(devvp);
189 			return (error);
190 		}
191 		VOP_UNLOCK(devvp, 0, p);
192 	}
193 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
194 		error = iso_mountfs(devvp, mp, p, &args);
195 	else {
196 		if (devvp != imp->im_devvp)
197 			error = EINVAL;	/* needs translation */
198 		else
199 			vrele(devvp);
200 	}
201 	if (error) {
202 		vrele(devvp);
203 		return (error);
204 	}
205 	imp = VFSTOISOFS(mp);
206 	(void)copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
207 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
208 	(void)copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
209 	    &size);
210 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
211 	bcopy(&args, &mp->mnt_stat.mount_info.iso_args, sizeof(args));
212 	(void)cd9660_statfs(mp, &mp->mnt_stat, p);
213 	return (0);
214 }
215 
216 /*
217  * Common code for mount and mountroot
218  */
219 static int
220 iso_mountfs(devvp, mp, p, argp)
221 	register struct vnode *devvp;
222 	struct mount *mp;
223 	struct proc *p;
224 	struct iso_args *argp;
225 {
226 	register struct iso_mnt *isomp = (struct iso_mnt *)0;
227 	struct buf *bp = NULL;
228 	struct buf *pribp = NULL, *supbp = NULL;
229 	dev_t dev = devvp->v_rdev;
230 	int error = EINVAL;
231 	int needclose = 0;
232 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
233 	extern struct vnode *rootvp;
234 	int iso_bsize;
235 	int iso_blknum;
236 	int joliet_level;
237 	struct iso_volume_descriptor *vdp;
238 	struct iso_primary_descriptor *pri = NULL;
239 	struct iso_supplementary_descriptor *sup = NULL;
240 	struct iso_directory_record *rootp;
241 	int logical_block_size;
242 
243 	if (!ronly)
244 		return (EROFS);
245 
246 	/*
247 	 * Disallow multiple mounts of the same device.
248 	 * Disallow mounting of a device that is currently in use
249 	 * (except for root, which might share swap device for miniroot).
250 	 * Flush out any old buffers remaining from a previous use.
251 	 */
252 	if ((error = vfs_mountedon(devvp)) != 0)
253 		return (error);
254 	if (vcount(devvp) > 1 && devvp != rootvp)
255 		return (EBUSY);
256 	if ((error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
257 		return (error);
258 
259 	error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p);
260 	if (error)
261 		return (error);
262 	needclose = 1;
263 
264 	/* This is the "logical sector size".  The standard says this
265 	 * should be 2048 or the physical sector size on the device,
266 	 * whichever is greater.  For now, we'll just use a constant.
267 	 */
268 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
269 
270 	joliet_level = 0;
271 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
272 		if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
273 				   iso_bsize, NOCRED, &bp)) != 0)
274 			goto out;
275 
276 		vdp = (struct iso_volume_descriptor *)bp->b_data;
277 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
278 			error = EINVAL;
279 			goto out;
280 		}
281 
282 		switch (isonum_711 (vdp->type)){
283 		case ISO_VD_PRIMARY:
284 			if (pribp == NULL) {
285 				pribp = bp;
286 				bp = NULL;
287 				pri = (struct iso_primary_descriptor *)vdp;
288 			}
289 			break;
290 		case ISO_VD_SUPPLEMENTARY:
291 			if (supbp == NULL) {
292 				supbp = bp;
293 				bp = NULL;
294 				sup = (struct iso_supplementary_descriptor *)vdp;
295 
296 				if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
297 					if (bcmp(sup->escape, "%/@", 3) == 0)
298 						joliet_level = 1;
299 					if (bcmp(sup->escape, "%/C", 3) == 0)
300 						joliet_level = 2;
301 					if (bcmp(sup->escape, "%/E", 3) == 0)
302 						joliet_level = 3;
303 
304 					if (isonum_711 (sup->flags) & 1)
305 						joliet_level = 0;
306 				}
307 			}
308 			break;
309 
310 		case ISO_VD_END:
311 			goto vd_end;
312 
313 		default:
314 			break;
315 		}
316 		if (bp) {
317 			brelse(bp);
318 			bp = NULL;
319 		}
320 	}
321     vd_end:
322 	if (bp) {
323 		brelse(bp);
324 		bp = NULL;
325 	}
326 
327 	if (pri == NULL) {
328 		error = EINVAL;
329 		goto out;
330 	}
331 
332 	logical_block_size = isonum_723 (pri->logical_block_size);
333 
334 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
335 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
336 		error = EINVAL;
337 		goto out;
338 	}
339 
340 	rootp = (struct iso_directory_record *)pri->root_directory_record;
341 
342 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
343 	bzero((caddr_t)isomp, sizeof *isomp);
344 	isomp->logical_block_size = logical_block_size;
345 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
346 	bcopy (rootp, isomp->root, sizeof isomp->root);
347 	isomp->root_extent = isonum_733 (rootp->extent);
348 	isomp->root_size = isonum_733 (rootp->size);
349 	isomp->joliet_level = 0;
350 
351 	isomp->im_bmask = logical_block_size - 1;
352 	isomp->im_bshift = ffs(logical_block_size) - 1;
353 
354 	pribp->b_flags |= B_AGE;
355 	brelse(pribp);
356 	pribp = NULL;
357 
358 	mp->mnt_data = (qaddr_t)isomp;
359 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
360 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
361 	mp->mnt_maxsymlinklen = 0;
362 	mp->mnt_flag |= MNT_LOCAL;
363 	isomp->im_mountp = mp;
364 	isomp->im_dev = dev;
365 	isomp->im_devvp = devvp;
366 
367 	devvp->v_specmountpoint = mp;
368 
369 	/* Check the Rock Ridge Extention support */
370 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
371 		if ((error = bread(isomp->im_devvp, (isomp->root_extent +
372 		    isonum_711(rootp->ext_attr_length)) <<
373 		    (isomp->im_bshift - DEV_BSHIFT),
374 		    isomp->logical_block_size, NOCRED, &bp)) != 0)
375 			goto out;
376 
377 		rootp = (struct iso_directory_record *)bp->b_data;
378 
379 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
380 		    argp->flags  |= ISOFSMNT_NORRIP;
381 		} else {
382 		    argp->flags  &= ~ISOFSMNT_GENS;
383 		}
384 
385 		/*
386 		 * The contents are valid,
387 		 * but they will get reread as part of another vnode, so...
388 		 */
389 		bp->b_flags |= B_AGE;
390 		brelse(bp);
391 		bp = NULL;
392 	}
393 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
394 	    ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
395 	switch (isomp->im_flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS)) {
396 	default:
397 	    isomp->iso_ftype = ISO_FTYPE_DEFAULT;
398 	    break;
399 	case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
400 	    isomp->iso_ftype = ISO_FTYPE_9660;
401 	    break;
402 	case 0:
403 	    isomp->iso_ftype = ISO_FTYPE_RRIP;
404 	    break;
405 	}
406 
407 	/* Decide whether to use the Joliet descriptor */
408 
409 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
410 		rootp = (struct iso_directory_record *)
411 			sup->root_directory_record;
412 		bcopy(rootp, isomp->root, sizeof isomp->root);
413 		isomp->root_extent = isonum_733(rootp->extent);
414 		isomp->root_size = isonum_733(rootp->size);
415 		isomp->joliet_level = joliet_level;
416 		supbp->b_flags |= B_AGE;
417 	}
418 
419 	if (supbp) {
420 		brelse(supbp);
421 		supbp = NULL;
422 	}
423 
424 	return (0);
425 out:
426 	if (bp)
427 		brelse(bp);
428 	if (supbp)
429 		brelse(supbp);
430 	if (needclose)
431 		(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED,
432 		    p);
433 	if (isomp) {
434 		free((caddr_t)isomp, M_ISOFSMNT);
435 		mp->mnt_data = (qaddr_t)0;
436 	}
437 	return (error);
438 }
439 
440 /*
441  * Test to see if the device is an ISOFS filesystem.
442  */
443 int
444 iso_disklabelspoof(dev, strat, lp)
445 	dev_t dev;
446 	void (*strat) __P((struct buf *));
447 	register struct disklabel *lp;
448 {
449 	struct buf *bp = NULL;
450 	struct iso_volume_descriptor *vdp;
451 	struct iso_primary_descriptor *pri;
452 	int logical_block_size;
453 	int error = EINVAL;
454 	int iso_blknum;
455 	int i;
456 
457 	bp = geteblk(ISO_DEFAULT_BLOCK_SIZE);
458 	bp->b_dev = dev;
459 
460 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
461 		bp->b_blkno = iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE);
462 		bp->b_bcount = ISO_DEFAULT_BLOCK_SIZE;
463 		bp->b_flags = B_BUSY | B_READ;
464 		bp->b_cylin = bp->b_blkno / lp->d_secpercyl;
465 
466 		/*printf("d_secsize %d iso_blknum %d b_blkno %d bcount %d\n",
467 		    lp->d_secsize, iso_blknum, bp->b_blkno, bp->b_bcount);*/
468 
469 		(*strat)(bp);
470 
471 		if (biowait(bp))
472 			goto out;
473 
474 		vdp = (struct iso_volume_descriptor *)bp->b_data;
475 		/*printf("%2x%2x%2x type %2x\n", vdp->id[0], vdp->id[1],
476 		    vdp->id[2], isonum_711(vdp->type));*/
477 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0 ||
478 		    isonum_711 (vdp->type) == ISO_VD_END)
479 			goto out;
480 
481 		if (isonum_711 (vdp->type) == ISO_VD_PRIMARY)
482 			break;
483 	}
484 
485 	if (isonum_711 (vdp->type) != ISO_VD_PRIMARY)
486 		goto out;
487 
488 	pri = (struct iso_primary_descriptor *)vdp;
489 	logical_block_size = isonum_723 (pri->logical_block_size);
490 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE ||
491 	    (logical_block_size & (logical_block_size - 1)) != 0)
492 		goto out;
493 
494 	/*
495 	 * build a disklabel for the CD
496 	 */
497 	strncpy(lp->d_typename, pri->volume_id, 16);
498 	strncpy(lp->d_packname, pri->volume_id+16, 16);
499 	for (i = 0; i < MAXPARTITIONS; i++) {
500 		lp->d_partitions[i].p_size = 0;
501 		lp->d_partitions[i].p_offset = 0;
502 	}
503 	lp->d_partitions[0].p_offset = 0;
504 	lp->d_partitions[0].p_size = lp->d_secperunit;
505 	lp->d_partitions[0].p_fstype = FS_ISO9660;
506 	lp->d_partitions[RAW_PART].p_offset = 0;
507 	lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
508 	lp->d_partitions[RAW_PART].p_fstype = FS_ISO9660;
509 	lp->d_npartitions = RAW_PART + 1;
510 	lp->d_bbsize = 8192;		/* fake */
511 	lp->d_sbsize = 64*1024;		/* fake */
512 
513 	lp->d_magic = DISKMAGIC;
514 	lp->d_magic2 = DISKMAGIC;
515 	lp->d_checksum = dkcksum(lp);
516 	error = 0;
517 out:
518 	bp->b_flags |= B_INVAL;
519 	brelse(bp);
520 	return (error);
521 }
522 
523 /*
524  * Make a filesystem operational.
525  * Nothing to do at the moment.
526  */
527 /* ARGSUSED */
528 int
529 cd9660_start(mp, flags, p)
530 	struct mount *mp;
531 	int flags;
532 	struct proc *p;
533 {
534 	return (0);
535 }
536 
537 /*
538  * unmount system call
539  */
540 int
541 cd9660_unmount(mp, mntflags, p)
542 	struct mount *mp;
543 	int mntflags;
544 	struct proc *p;
545 {
546 	register struct iso_mnt *isomp;
547 	int error, flags = 0;
548 
549 	if (mntflags & MNT_FORCE)
550 		flags |= FORCECLOSE;
551 #if 0
552 	mntflushbuf(mp, 0);
553 	if (mntinvalbuf(mp))
554 		return (EBUSY);
555 #endif
556 	if ((error = vflush(mp, NULLVP, flags)) != 0)
557 		return (error);
558 
559 	isomp = VFSTOISOFS(mp);
560 
561 #ifdef	ISODEVMAP
562 	if (isomp->iso_ftype == ISO_FTYPE_RRIP)
563 		iso_dunmap(isomp->im_dev);
564 #endif
565 
566 	isomp->im_devvp->v_specmountpoint = NULL;
567 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
568 	vrele(isomp->im_devvp);
569 	free((caddr_t)isomp, M_ISOFSMNT);
570 	mp->mnt_data = (qaddr_t)0;
571 	mp->mnt_flag &= ~MNT_LOCAL;
572 	return (error);
573 }
574 
575 /*
576  * Return root of a filesystem
577  */
578 int
579 cd9660_root(mp, vpp)
580 	struct mount *mp;
581 	struct vnode **vpp;
582 {
583 	struct iso_mnt *imp = VFSTOISOFS(mp);
584 	struct iso_directory_record *dp =
585 	    (struct iso_directory_record *)imp->root;
586 	ino_t ino = isodirino(dp, imp);
587 
588 	/*
589 	 * With RRIP we must use the `.' entry of the root directory.
590 	 * Simply tell vget, that it's a relocated directory.
591 	 */
592 	return (cd9660_vget_internal(mp, ino, vpp,
593 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
594 }
595 
596 /*
597  * Do operations associated with quotas, not supported
598  */
599 /* ARGSUSED */
600 int
601 cd9660_quotactl(mp, cmd, uid, arg, p)
602 	struct mount *mp;
603 	int cmd;
604 	uid_t uid;
605 	caddr_t arg;
606 	struct proc *p;
607 {
608 
609 	return (EOPNOTSUPP);
610 }
611 
612 /*
613  * Get file system statistics.
614  */
615 int
616 cd9660_statfs(mp, sbp, p)
617 	struct mount *mp;
618 	register struct statfs *sbp;
619 	struct proc *p;
620 {
621 	register struct iso_mnt *isomp;
622 
623 	isomp = VFSTOISOFS(mp);
624 
625 	sbp->f_bsize = isomp->logical_block_size;
626 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
627 	sbp->f_blocks = isomp->volume_space_size;
628 	sbp->f_bfree = 0; /* total free blocks */
629 	sbp->f_bavail = 0; /* blocks free for non superuser */
630 	sbp->f_files =  0; /* total files */
631 	sbp->f_ffree = 0; /* free file nodes */
632 	if (sbp != &mp->mnt_stat) {
633 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
634 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname,
635 		    MNAMELEN);
636 		bcopy(&mp->mnt_stat.mount_info.iso_args,
637 		    &sbp->mount_info.iso_args, sizeof(struct iso_args));
638 	}
639 	/* Use the first spare for flags: */
640 	sbp->f_spare[0] = isomp->im_flags;
641 	return (0);
642 }
643 
644 /* ARGSUSED */
645 int
646 cd9660_sync(mp, waitfor, cred, p)
647 	struct mount *mp;
648 	int waitfor;
649 	struct ucred *cred;
650 	struct proc *p;
651 {
652 	return (0);
653 }
654 
655 /*
656  * File handle to vnode
657  *
658  * Have to be really careful about stale file handles:
659  * - check that the inode number is in range
660  * - call iget() to get the locked inode
661  * - check for an unallocated inode (i_mode == 0)
662  * - check that the generation number matches
663  */
664 
665 struct ifid {
666 	ushort	ifid_len;
667 	ushort	ifid_pad;
668 	int	ifid_ino;
669 	long	ifid_start;
670 };
671 
672 /* ARGSUSED */
673 int
674 cd9660_fhtovp(mp, fhp, vpp)
675 	register struct mount *mp;
676 	struct fid *fhp;
677 	struct vnode **vpp;
678 {
679 	struct ifid *ifhp = (struct ifid *)fhp;
680 	register struct iso_node *ip;
681 	struct vnode *nvp;
682 	int error;
683 
684 #ifdef	ISOFS_DBG
685 	printf("fhtovp: ino %d, start %ld\n", ifhp->ifid_ino,
686 	    ifhp->ifid_start);
687 #endif
688 
689 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
690 		*vpp = NULLVP;
691 		return (error);
692 	}
693 	ip = VTOI(nvp);
694 	if (ip->inode.iso_mode == 0) {
695 		vput(nvp);
696 		*vpp = NULLVP;
697 		return (ESTALE);
698 	}
699 	*vpp = nvp;
700 	return (0);
701 }
702 
703 int
704 cd9660_vget(mp, ino, vpp)
705 	struct mount *mp;
706 	ino_t ino;
707 	struct vnode **vpp;
708 {
709 
710 	/*
711 	 * XXXX
712 	 * It would be nice if we didn't always set the `relocated' flag
713 	 * and force the extra read, but I don't want to think about fixing
714 	 * that right now.
715 	 */
716 	return (cd9660_vget_internal(mp, ino, vpp,
717 #if 0
718 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
719 #else
720 	    0,
721 #endif
722 	    NULL));
723 }
724 
725 int
726 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
727 	struct mount *mp;
728 	ino_t ino;
729 	struct vnode **vpp;
730 	int relocated;
731 	struct iso_directory_record *isodir;
732 {
733 	register struct iso_mnt *imp;
734 	struct iso_node *ip;
735 	struct buf *bp;
736 	struct vnode *vp, *nvp;
737 	dev_t dev;
738 	int error;
739 
740 retry:
741 	imp = VFSTOISOFS(mp);
742 	dev = imp->im_dev;
743 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
744 		return (0);
745 
746 	/* Allocate a new vnode/iso_node. */
747 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
748 		*vpp = NULLVP;
749 		return (error);
750 	}
751 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
752 	    M_WAITOK);
753 	bzero((caddr_t)ip, sizeof(struct iso_node));
754 	lockinit(&ip->i_lock, PINOD, "isoinode", 0, 0);
755 	vp->v_data = ip;
756 	ip->i_vnode = vp;
757 	ip->i_dev = dev;
758 	ip->i_number = ino;
759 
760 	/*
761 	 * Put it onto its hash chain and lock it so that other requests for
762 	 * this inode will block if they arrive while we are sleeping waiting
763 	 * for old data structures to be purged or for the contents of the
764 	 * disk portion of this inode to be read.
765 	 */
766 	error = cd9660_ihashins(ip);
767 
768 	if (error) {
769 		vrele(vp);
770 
771 		if (error == EEXIST)
772 			goto retry;
773 
774 		return (error);
775 	}
776 
777 	if (isodir == 0) {
778 		int lbn, off;
779 
780 		lbn = lblkno(imp, ino);
781 		if (lbn >= imp->volume_space_size) {
782 			vput(vp);
783 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
784 			return (ESTALE);
785 		}
786 
787 		off = blkoff(imp, ino);
788 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size)
789 		    {
790 			vput(vp);
791 			printf("fhtovp: crosses block boundary %d\n",
792 			    off + ISO_DIRECTORY_RECORD_SIZE);
793 			return (ESTALE);
794 		}
795 
796 		error = bread(imp->im_devvp,
797 			      lbn << (imp->im_bshift - DEV_BSHIFT),
798 			      imp->logical_block_size, NOCRED, &bp);
799 		if (error) {
800 			vput(vp);
801 			brelse(bp);
802 			printf("fhtovp: bread error %d\n",error);
803 			return (error);
804 		}
805 		isodir = (struct iso_directory_record *)(bp->b_data + off);
806 
807 		if (off + isonum_711(isodir->length) >
808 		    imp->logical_block_size) {
809 			vput(vp);
810 			if (bp != 0)
811 				brelse(bp);
812 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
813 			    off +isonum_711(isodir->length), off,
814 			    isonum_711(isodir->length));
815 			return (ESTALE);
816 		}
817 
818 #if 0
819 		if (isonum_733(isodir->extent) +
820 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
821 			if (bp != 0)
822 				brelse(bp);
823 			printf("fhtovp: file start miss %d vs %d\n",
824 			    isonum_733(isodir->extent) +
825 			    isonum_711(isodir->ext_attr_length),
826 			    ifhp->ifid_start);
827 			return (ESTALE);
828 		}
829 #endif
830 	} else
831 		bp = 0;
832 
833 	ip->i_mnt = imp;
834 	ip->i_devvp = imp->im_devvp;
835 	VREF(ip->i_devvp);
836 
837 	if (relocated) {
838 		/*
839 		 * On relocated directories we must
840 		 * read the `.' entry out of a dir.
841 		 */
842 		ip->iso_start = ino >> imp->im_bshift;
843 		if (bp != 0)
844 			brelse(bp);
845 		if ((error = cd9660_bufatoff(ip, (off_t)0, NULL, &bp)) != 0) {
846 			vput(vp);
847 			return (error);
848 		}
849 		isodir = (struct iso_directory_record *)bp->b_data;
850 	}
851 
852 	ip->iso_extent = isonum_733(isodir->extent);
853 	ip->i_size = isonum_733(isodir->size);
854 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
855 
856 	/*
857 	 * Setup time stamp, attribute
858 	 */
859 	vp->v_type = VNON;
860 	switch (imp->iso_ftype) {
861 	default:	/* ISO_FTYPE_9660 */
862 	    {
863 		struct buf *bp2;
864 		int off;
865 		if ((imp->im_flags & ISOFSMNT_EXTATT) &&
866 		    (off = isonum_711(isodir->ext_attr_length)))
867 			cd9660_bufatoff(ip, (off_t)-(off << imp->im_bshift),
868 			    NULL, &bp2);
869 		else
870 			bp2 = NULL;
871 		cd9660_defattr(isodir, ip, bp2);
872 		cd9660_deftstamp(isodir, ip, bp2);
873 		if (bp2)
874 			brelse(bp2);
875 		break;
876 	    }
877 	case ISO_FTYPE_RRIP:
878 		cd9660_rrip_analyze(isodir, ip, imp);
879 		break;
880 	}
881 
882 	if (bp != 0)
883 		brelse(bp);
884 
885 	/*
886 	 * Initialize the associated vnode
887 	 */
888 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
889 	case VFIFO:
890 #ifdef	FIFO
891 		vp->v_op = cd9660_fifoop_p;
892 		break;
893 #else
894 		vput(vp);
895 		return (EOPNOTSUPP);
896 #endif	/* FIFO */
897 	case VCHR:
898 	case VBLK:
899 		/*
900 		 * if device, look at device number table for translation
901 		 */
902 #ifdef	ISODEVMAP
903 		if (dp = iso_dmap(dev, ino, 0))
904 			ip->inode.iso_rdev = dp->d_dev;
905 #endif
906 		vp->v_op = cd9660_specop_p;
907 		if ((nvp = checkalias(vp, ip->inode.iso_rdev, mp)) != NULL) {
908 			/*
909 			 * Discard unneeded vnode, but save its iso_node.
910 			 * Note that the lock is carried over in the iso_node
911 			 */
912 			nvp->v_data = vp->v_data;
913 			vp->v_data = NULL;
914 			vp->v_op = spec_vnodeop_p;
915 			vrele(vp);
916 			vgone(vp);
917 			/*
918 			 * Reinitialize aliased inode.
919 			 */
920 			vp = nvp;
921 			ip->i_vnode = vp;
922 		}
923 		break;
924 	case VLNK:
925 	case VNON:
926 	case VSOCK:
927 	case VDIR:
928 	case VBAD:
929 	case VREG:
930 		break;
931 	}
932 
933 	if (ip->iso_extent == imp->root_extent)
934 		vp->v_flag |= VROOT;
935 
936 	/*
937 	 * XXX need generation number?
938 	 */
939 
940 	*vpp = vp;
941 	return (0);
942 }
943 
944 /*
945  * Vnode pointer to File handle
946  */
947 /* ARGSUSED */
948 int
949 cd9660_vptofh(vp, fhp)
950 	struct vnode *vp;
951 	struct fid *fhp;
952 {
953 	register struct iso_node *ip = VTOI(vp);
954 	register struct ifid *ifhp;
955 
956 	ifhp = (struct ifid *)fhp;
957 	ifhp->ifid_len = sizeof(struct ifid);
958 
959 	ifhp->ifid_ino = ip->i_number;
960 	ifhp->ifid_start = ip->iso_start;
961 
962 #ifdef	ISOFS_DBG
963 	printf("vptofh: ino %d, start %ld\n",
964 	    ifhp->ifid_ino,ifhp->ifid_start);
965 #endif
966 	return (0);
967 }
968 
969 /*
970  * Verify a remote client has export rights and return these rights via
971  * exflagsp and credanonp.
972  */
973 int
974 cd9660_check_export(mp, nam, exflagsp, credanonp)
975 	register struct mount *mp;
976 	struct mbuf *nam;
977 	int *exflagsp;
978 	struct ucred **credanonp;
979 {
980 	register struct netcred *np;
981 	register struct iso_mnt *imp = VFSTOISOFS(mp);
982 
983 	/*
984 	 * Get the export permission structure for this <mp, client> tuple.
985 	 */
986 	np = vfs_export_lookup(mp, &imp->im_export, nam);
987 	if (np == NULL)
988 		return (EACCES);
989 
990 	*exflagsp = np->netc_exflags;
991 	*credanonp = &np->netc_anon;
992 	return (0);
993 }
994