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