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