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