xref: /netbsd-src/sys/fs/cd9660/cd9660_vfsops.c (revision 3816d47b2c42fcd6e549e3407f842a5b1a1d23ad)
1 /*	$NetBSD: cd9660_vfsops.c,v 1.69 2010/01/08 11:35:08 pooka Exp $	*/
2 
3 /*-
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley
8  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9  * Support code is derived from software contributed to Berkeley
10  * by Atsushi Murai (amurai@spec.co.jp).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)cd9660_vfsops.c	8.18 (Berkeley) 5/22/95
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: cd9660_vfsops.c,v 1.69 2010/01/08 11:35:08 pooka Exp $");
41 
42 #if defined(_KERNEL_OPT)
43 #include "opt_compat_netbsd.h"
44 #endif
45 
46 #include <sys/param.h>
47 #include <sys/sysctl.h>
48 #include <sys/systm.h>
49 #include <sys/namei.h>
50 #include <sys/proc.h>
51 #include <sys/kernel.h>
52 #include <sys/vnode.h>
53 #include <miscfs/genfs/genfs.h>
54 #include <miscfs/specfs/specdev.h>
55 #include <sys/mount.h>
56 #include <sys/buf.h>
57 #include <sys/file.h>
58 #include <sys/disklabel.h>
59 #include <sys/device.h>
60 #include <sys/ioctl.h>
61 #include <sys/cdio.h>
62 #include <sys/errno.h>
63 #include <sys/malloc.h>
64 #include <sys/pool.h>
65 #include <sys/stat.h>
66 #include <sys/conf.h>
67 #include <sys/dirent.h>
68 #include <sys/kauth.h>
69 #include <sys/module.h>
70 
71 #include <fs/cd9660/iso.h>
72 #include <fs/cd9660/cd9660_extern.h>
73 #include <fs/cd9660/iso_rrip.h>
74 #include <fs/cd9660/cd9660_node.h>
75 #include <fs/cd9660/cd9660_mount.h>
76 
77 MODULE(MODULE_CLASS_VFS, cd9660, NULL);
78 
79 MALLOC_JUSTDEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
80 
81 static struct sysctllog *cd9660_sysctl_log;
82 
83 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc;
84 extern const struct vnodeopv_desc cd9660_specop_opv_desc;
85 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc;
86 
87 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = {
88 	&cd9660_vnodeop_opv_desc,
89 	&cd9660_specop_opv_desc,
90 	&cd9660_fifoop_opv_desc,
91 	NULL,
92 };
93 
94 struct vfsops cd9660_vfsops = {
95 	MOUNT_CD9660,
96 	sizeof (struct iso_args),
97 	cd9660_mount,
98 	cd9660_start,
99 	cd9660_unmount,
100 	cd9660_root,
101 	(void *)eopnotsupp,
102 	cd9660_statvfs,
103 	cd9660_sync,
104 	cd9660_vget,
105 	cd9660_fhtovp,
106 	cd9660_vptofh,
107 	cd9660_init,
108 	cd9660_reinit,
109 	cd9660_done,
110 	cd9660_mountroot,
111 	(int (*)(struct mount *, struct vnode *, struct timespec *)) eopnotsupp,
112 	vfs_stdextattrctl,
113 	(void *)eopnotsupp,		/* vfs_suspendctl */
114 	genfs_renamelock_enter,
115 	genfs_renamelock_exit,
116 	(void *)eopnotsupp,
117 	cd9660_vnodeopv_descs,
118 	0,	/* refcount */
119 	{ NULL, NULL } /* list */
120 };
121 
122 static const struct genfs_ops cd9660_genfsops = {
123 	.gop_size = genfs_size,
124 };
125 
126 /*
127  * Called by vfs_mountroot when iso is going to be mounted as root.
128  *
129  * Name is updated by mount(8) after booting.
130  */
131 #define ROOTNAME	"root_device"
132 
133 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len);
134 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
135 		struct lwp *l, struct iso_args *argp);
136 
137 static int
138 cd9660_modcmd(modcmd_t cmd, void *arg)
139 {
140 	int error;
141 
142 	switch (cmd) {
143 	case MODULE_CMD_INIT:
144 		error = vfs_attach(&cd9660_vfsops);
145 		if (error != 0)
146 			break;
147 		sysctl_createv(&cd9660_sysctl_log, 0, NULL, NULL,
148 			       CTLFLAG_PERMANENT, CTLTYPE_NODE, "vfs", NULL,
149 			       NULL, 0, NULL, 0,
150 			       CTL_VFS, CTL_EOL);
151 		sysctl_createv(&cd9660_sysctl_log, 0, NULL, NULL,
152 			       CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
153 			       SYSCTL_DESCR("ISO-9660 file system"),
154 			       NULL, 0, NULL, 0,
155 			       CTL_VFS, 14, CTL_EOL);
156 		sysctl_createv(&cd9660_sysctl_log, 0, NULL, NULL,
157 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
158 			       CTLTYPE_INT, "utf8_joliet",
159 			       SYSCTL_DESCR("Encode Joliet filenames to UTF-8"),
160 			       NULL, 0, &cd9660_utf8_joliet, 0,
161 			       CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
162 		/*
163 		 * XXX the "14" above could be dynamic, thereby eliminating
164 		 * one more instance of the "number to vfs" mapping problem,
165 		 * but "14" is the order as taken from sys/mount.h
166 		 */
167 		break;
168 	case MODULE_CMD_FINI:
169 		error = vfs_detach(&cd9660_vfsops);
170 		if (error != 0)
171 			break;
172 		sysctl_teardown(&cd9660_sysctl_log);
173 		break;
174 	default:
175 		error = ENOTTY;
176 		break;
177 	}
178 
179 	return (error);
180 }
181 
182 int
183 cd9660_mountroot(void)
184 {
185 	struct mount *mp;
186 	struct lwp *l = curlwp;
187 	int error;
188 	struct iso_args args;
189 
190 	if (device_class(root_device) != DV_DISK)
191 		return (ENODEV);
192 
193 	if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
194 			!= 0) {
195 		vrele(rootvp);
196 		return (error);
197 	}
198 
199 	args.flags = ISOFSMNT_ROOT;
200 	if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) {
201 		vfs_unbusy(mp, false, NULL);
202 		vfs_destroy(mp);
203 		return (error);
204 	}
205 	mutex_enter(&mountlist_lock);
206 	CIRCLEQ_INSERT_TAIL(&mountlist, mp, mnt_list);
207 	mutex_exit(&mountlist_lock);
208 	(void)cd9660_statvfs(mp, &mp->mnt_stat);
209 	vfs_unbusy(mp, false, NULL);
210 	return (0);
211 }
212 
213 /*
214  * VFS Operations.
215  *
216  * mount system call
217  */
218 int
219 cd9660_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
220 {
221 	struct lwp *l = curlwp;
222 	struct vnode *devvp;
223 	struct iso_args *args = data;
224 	int error;
225 	struct iso_mnt *imp = VFSTOISOFS(mp);
226 
227 	if (*data_len < sizeof *args)
228 		return EINVAL;
229 
230 	if (mp->mnt_flag & MNT_GETARGS) {
231 		if (imp == NULL)
232 			return EIO;
233 		args->fspec = NULL;
234 		args->flags = imp->im_flags;
235 		*data_len = sizeof (*args);
236 		return 0;
237 	}
238 
239 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
240 		return (EROFS);
241 
242 	if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
243 		return EINVAL;
244 
245 	/*
246 	 * Not an update, or updating the name: look up the name
247 	 * and verify that it refers to a sensible block device.
248 	 */
249 	error = namei_simple_user(args->fspec,
250 				NSM_FOLLOW_NOEMULROOT, &devvp);
251 	if (error != 0)
252 		return (error);
253 
254 	if (devvp->v_type != VBLK) {
255 		vrele(devvp);
256 		return ENOTBLK;
257 	}
258 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
259 		vrele(devvp);
260 		return ENXIO;
261 	}
262 	/*
263 	 * If mount by non-root, then verify that user has necessary
264 	 * permissions on the device.
265 	 */
266 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
267 	error = genfs_can_mount(devvp, VREAD, l->l_cred);
268 	VOP_UNLOCK(devvp, 0);
269 	if (error) {
270 		vrele(devvp);
271 		return (error);
272 	}
273 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
274 		error = VOP_OPEN(devvp, FREAD, FSCRED);
275 		if (error)
276 			goto fail;
277 		error = iso_mountfs(devvp, mp, l, args);
278 		if (error) {
279 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
280 			(void)VOP_CLOSE(devvp, FREAD, NOCRED);
281 			VOP_UNLOCK(devvp, 0);
282 			goto fail;
283 		}
284 	} else {
285 		vrele(devvp);
286 		if (devvp != imp->im_devvp &&
287 		    devvp->v_rdev != imp->im_devvp->v_rdev)
288 			return (EINVAL);	/* needs translation */
289 	}
290 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
291 	    mp->mnt_op->vfs_name, mp, l);
292 
293 fail:
294 	vrele(devvp);
295 	return (error);
296 }
297 
298 /*
299  * Make a mount point from a volume descriptor
300  */
301 static int
302 iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len)
303 {
304 	struct iso_primary_descriptor *pri;
305 	int logical_block_size;
306 	struct iso_directory_record *rootp;
307 
308 	pri = (struct iso_primary_descriptor *)bp->b_data;
309 
310 	logical_block_size = isonum_723 (pri->logical_block_size);
311 
312 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
313 	    || (logical_block_size & (logical_block_size - 1)) != 0)
314 		return -1;
315 
316 	rootp = (struct iso_directory_record *)pri->root_directory_record;
317 
318 	isomp->logical_block_size = logical_block_size;
319 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
320 	memcpy(isomp->root, rootp, sizeof(isomp->root));
321 	isomp->root_extent = isonum_733 (rootp->extent);
322 	isomp->root_size = isonum_733 (rootp->size);
323 	isomp->im_joliet_level = 0;
324 
325 	isomp->im_bmask = logical_block_size - 1;
326 	isomp->im_bshift = 0;
327 	while ((1 << isomp->im_bshift) < isomp->logical_block_size)
328 		isomp->im_bshift++;
329 
330 	if (ea_len != NULL)
331 		*ea_len = isonum_711(rootp->ext_attr_length);
332 
333 	return 0;
334 }
335 
336 /*
337  * Common code for mount and mountroot
338  */
339 static int
340 iso_mountfs(struct vnode *devvp, struct mount *mp, struct lwp *l,
341 	struct iso_args *argp)
342 {
343 	struct iso_mnt *isomp = (struct iso_mnt *)0;
344 	struct buf *bp = NULL, *pribp = NULL, *supbp = NULL;
345 	dev_t dev = devvp->v_rdev;
346 	int error = EINVAL;
347 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
348 	int iso_bsize;
349 	int iso_blknum;
350 	int joliet_level;
351 	struct iso_volume_descriptor *vdp;
352 	struct iso_supplementary_descriptor *sup;
353 	int sess = 0;
354 	int ext_attr_length;
355 	struct disklabel label;
356 
357 	if (!ronly)
358 		return EROFS;
359 
360 	/* Flush out any old buffers remaining from a previous use. */
361 	if ((error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
362 		return (error);
363 
364 	/* This is the "logical sector size".  The standard says this
365 	 * should be 2048 or the physical sector size on the device,
366 	 * whichever is greater.  For now, we'll just use a constant.
367 	 */
368 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
369 
370 	error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED);
371 	if (!error &&
372 	    label.d_partitions[DISKPART(dev)].p_fstype == FS_ISO9660) {
373 		/* XXX more sanity checks? */
374 		sess = label.d_partitions[DISKPART(dev)].p_cdsession;
375 	} else {
376 		/* fallback to old method */
377 		error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED);
378 		if (error)
379 			sess = 0;	/* never mind */
380 	}
381 #ifdef ISO_DEBUG
382 	printf("isofs: session offset (part %"PRId32") %d\n", DISKPART(dev), sess);
383 #endif
384 
385 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
386 		if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize),
387 				   iso_bsize, NOCRED, 0, &bp)) != 0)
388 			goto out;
389 
390 		vdp = (struct iso_volume_descriptor *)bp->b_data;
391 		if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) {
392 			error = EINVAL;
393 			goto out;
394 		}
395 
396 		switch (isonum_711(vdp->type)) {
397 		case ISO_VD_PRIMARY:
398 			if (pribp == NULL) {
399 				pribp = bp;
400 				bp = NULL;
401 			}
402 			break;
403 
404 		case ISO_VD_SUPPLEMENTARY:
405 			if (supbp == NULL) {
406 				supbp = bp;
407 				bp = NULL;
408 			}
409 			break;
410 
411 		default:
412 			break;
413 		}
414 
415 		if (isonum_711 (vdp->type) == ISO_VD_END) {
416 			brelse(bp, 0);
417 			bp = NULL;
418 			break;
419 		}
420 
421 		if (bp != NULL) {
422 			brelse(bp, 0);
423 			bp = NULL;
424 		}
425 	}
426 
427 	if (pribp == NULL) {
428 		error = EINVAL;
429 		goto out;
430 	}
431 
432 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
433 	memset(isomp, 0, sizeof *isomp);
434 	if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) {
435 		error = EINVAL;
436 		goto out;
437 	}
438 
439 	isomp->volume_space_size += sess;
440 
441 	brelse(pribp, BC_AGE);
442 	pribp = NULL;
443 
444 	mp->mnt_data = isomp;
445 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
446 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
447 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
448 	mp->mnt_stat.f_namemax = MAXNAMLEN;
449 	mp->mnt_flag |= MNT_LOCAL;
450 	mp->mnt_iflag |= IMNT_MPSAFE;
451 	mp->mnt_dev_bshift = iso_bsize;
452 	mp->mnt_fs_bshift = isomp->im_bshift;
453 	isomp->im_mountp = mp;
454 	isomp->im_dev = dev;
455 	isomp->im_devvp = devvp;
456 
457 	/* Check the Rock Ridge Extension support */
458 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
459 		struct iso_directory_record *rootp;
460 
461 		if ((error = bread(isomp->im_devvp,
462 				   (isomp->root_extent + ext_attr_length) <<
463 				   (isomp->im_bshift - DEV_BSHIFT),
464 				   isomp->logical_block_size, NOCRED,
465 				   0, &bp)) != 0)
466 		    goto out;
467 
468 		rootp = (struct iso_directory_record *)bp->b_data;
469 
470 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
471 		    argp->flags  |= ISOFSMNT_NORRIP;
472 		} else {
473 		    argp->flags  &= ~ISOFSMNT_GENS;
474 		}
475 
476 		/*
477 		 * The contents are valid,
478 		 * but they will get reread as part of another vnode, so...
479 		 */
480 		brelse(bp, BC_AGE);
481 		bp = NULL;
482 	}
483 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
484 		 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
485 
486 	if (isomp->im_flags & ISOFSMNT_GENS)
487 		isomp->iso_ftype = ISO_FTYPE_9660;
488 	else if (isomp->im_flags & ISOFSMNT_NORRIP) {
489 		isomp->iso_ftype = ISO_FTYPE_DEFAULT;
490 		if (argp->flags & ISOFSMNT_NOCASETRANS)
491 			isomp->im_flags |= ISOFSMNT_NOCASETRANS;
492 	} else
493 		isomp->iso_ftype = ISO_FTYPE_RRIP;
494 
495 	/* Check the Joliet Extension support */
496 	if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
497 	    (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
498 	    supbp != NULL) {
499 		joliet_level = 0;
500 		sup = (struct iso_supplementary_descriptor *)supbp->b_data;
501 
502 		if ((isonum_711(sup->flags) & 1) == 0) {
503 			if (memcmp(sup->escape, "%/@", 3) == 0)
504 				joliet_level = 1;
505 			if (memcmp(sup->escape, "%/C", 3) == 0)
506 				joliet_level = 2;
507 			if (memcmp(sup->escape, "%/E", 3) == 0)
508 				joliet_level = 3;
509 		}
510 		if (joliet_level != 0) {
511 			if (iso_makemp(isomp, supbp, NULL) == -1) {
512 				error = EINVAL;
513 				goto out;
514 			}
515 			isomp->im_joliet_level = joliet_level;
516 		}
517 	}
518 
519 	if (supbp != NULL) {
520 		brelse(supbp, 0);
521 		supbp = NULL;
522 	}
523 
524 	devvp->v_specmountpoint = mp;
525 
526 	return 0;
527 out:
528 	if (bp)
529 		brelse(bp, 0);
530 	if (pribp)
531 		brelse(pribp, 0);
532 	if (supbp)
533 		brelse(supbp, 0);
534 	if (isomp) {
535 		free(isomp, M_ISOFSMNT);
536 		mp->mnt_data = NULL;
537 	}
538 	return error;
539 }
540 
541 /*
542  * Make a filesystem operational.
543  * Nothing to do at the moment.
544  */
545 /* ARGSUSED */
546 int
547 cd9660_start(struct mount *mp, int flags)
548 {
549 	return 0;
550 }
551 
552 /*
553  * unmount system call
554  */
555 int
556 cd9660_unmount(struct mount *mp, int mntflags)
557 {
558 	struct iso_mnt *isomp;
559 	int error, flags = 0;
560 
561 	if (mntflags & MNT_FORCE)
562 		flags |= FORCECLOSE;
563 	if ((error = vflush(mp, NULLVP, flags)) != 0)
564 		return (error);
565 
566 	isomp = VFSTOISOFS(mp);
567 
568 	if (isomp->im_devvp->v_type != VBAD)
569 		isomp->im_devvp->v_specmountpoint = NULL;
570 
571 	vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
572 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED);
573 	vput(isomp->im_devvp);
574 	free(isomp, M_ISOFSMNT);
575 	mp->mnt_data = NULL;
576 	mp->mnt_flag &= ~MNT_LOCAL;
577 	return (error);
578 }
579 
580 /*
581  * Return root of a filesystem
582  */
583 int
584 cd9660_root(struct mount *mp, struct vnode **vpp)
585 {
586 	struct iso_mnt *imp = VFSTOISOFS(mp);
587 	struct iso_directory_record *dp =
588 	    (struct iso_directory_record *)imp->root;
589 	ino_t ino = isodirino(dp, imp);
590 
591 	/*
592 	 * With RRIP we must use the `.' entry of the root directory.
593 	 * Simply tell vget, that it's a relocated directory.
594 	 */
595 	return (cd9660_vget_internal(mp, ino, vpp,
596 				     imp->iso_ftype == ISO_FTYPE_RRIP, dp));
597 }
598 
599 /*
600  * Get file system statistics.
601  */
602 int
603 cd9660_statvfs(struct mount *mp, struct statvfs *sbp)
604 {
605 	struct iso_mnt *isomp;
606 
607 	isomp = VFSTOISOFS(mp);
608 
609 	sbp->f_bsize = isomp->logical_block_size;
610 	sbp->f_frsize = sbp->f_bsize;
611 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
612 	sbp->f_blocks = isomp->volume_space_size;
613 	sbp->f_bfree = 0; /* total free blocks */
614 	sbp->f_bavail = 0; /* blocks free for non superuser */
615 	sbp->f_bresvd = 0; /* total reserved blocks */
616 	sbp->f_files =  0; /* total files */
617 	sbp->f_ffree = 0; /* free file nodes */
618 	sbp->f_favail = 0; /* free file nodes for non superuser */
619 	sbp->f_fresvd = 0; /* reserved file nodes */
620 	copy_statvfs_info(sbp, mp);
621 	/* Use the first spare for flags: */
622 	sbp->f_spare[0] = isomp->im_flags;
623 	return 0;
624 }
625 
626 /* ARGSUSED */
627 int
628 cd9660_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
629 {
630 	return 0;
631 }
632 
633 /*
634  * File handle to vnode
635  *
636  * Have to be really careful about stale file handles:
637  * - check that the inode number is in range
638  * - call iget() to get the locked inode
639  * - check for an unallocated inode (i_mode == 0)
640  * - check that the generation number matches
641  */
642 
643 struct ifid {
644 	ushort	ifid_len;
645 	ushort	ifid_pad;
646 	int	ifid_ino;
647 	long	ifid_start;
648 };
649 
650 /* ARGSUSED */
651 int
652 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
653 {
654 	struct ifid ifh;
655 	struct iso_node *ip;
656 	struct vnode *nvp;
657 	int error;
658 
659 	if (fhp->fid_len != sizeof(ifh))
660 		return EINVAL;
661 
662 	memcpy(&ifh, fhp, sizeof(ifh));
663 #ifdef	ISOFS_DBG
664 	printf("fhtovp: ino %d, start %ld\n",
665 	    ifh.ifid_ino, ifh.ifid_start);
666 #endif
667 
668 	if ((error = VFS_VGET(mp, ifh.ifid_ino, &nvp)) != 0) {
669 		*vpp = NULLVP;
670 		return (error);
671 	}
672 	ip = VTOI(nvp);
673 	if (ip->inode.iso_mode == 0) {
674 		vput(nvp);
675 		*vpp = NULLVP;
676 		return (ESTALE);
677 	}
678 	*vpp = nvp;
679 	return (0);
680 }
681 
682 int
683 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
684 {
685 
686 	/*
687 	 * XXXX
688 	 * It would be nice if we didn't always set the `relocated' flag
689 	 * and force the extra read, but I don't want to think about fixing
690 	 * that right now.
691 	 */
692 	return (cd9660_vget_internal(mp, ino, vpp,
693 #if 0
694 				     VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
695 #else
696 				     0,
697 #endif
698 				     NULL));
699 }
700 
701 int
702 cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
703 	int relocated, struct iso_directory_record *isodir)
704 {
705 	struct iso_mnt *imp;
706 	struct iso_node *ip;
707 	struct buf *bp;
708 	struct vnode *vp;
709 	dev_t dev;
710 	int error;
711 
712 	imp = VFSTOISOFS(mp);
713 	dev = imp->im_dev;
714 
715  retry:
716 	if ((*vpp = cd9660_ihashget(dev, ino, LK_EXCLUSIVE)) != NULLVP)
717 		return (0);
718 
719 	/* Allocate a new vnode/iso_node. */
720 	if ((error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) != 0) {
721 		*vpp = NULLVP;
722 		return (error);
723 	}
724 	ip = pool_get(&cd9660_node_pool, PR_WAITOK);
725 
726 	/*
727 	 * If someone beat us to it, put back the freshly allocated
728 	 * vnode/inode pair and retry.
729 	 */
730 	mutex_enter(&cd9660_hashlock);
731 	if (cd9660_ihashget(dev, ino, 0) != NULL) {
732 		mutex_exit(&cd9660_hashlock);
733 		ungetnewvnode(vp);
734 		pool_put(&cd9660_node_pool, ip);
735 		goto retry;
736 	}
737 
738 	memset(ip, 0, sizeof(struct iso_node));
739 	vp->v_data = ip;
740 	ip->i_vnode = vp;
741 	ip->i_dev = dev;
742 	ip->i_number = ino;
743 	ip->i_mnt = imp;
744 	ip->i_devvp = imp->im_devvp;
745 	genfs_node_init(vp, &cd9660_genfsops);
746 
747 	/*
748 	 * Put it onto its hash chain and lock it so that other requests for
749 	 * this inode will block if they arrive while we are sleeping waiting
750 	 * for old data structures to be purged or for the contents of the
751 	 * disk portion of this inode to be read.
752 	 */
753 	cd9660_ihashins(ip);
754 	mutex_exit(&cd9660_hashlock);
755 
756 	if (isodir == 0) {
757 		int lbn, off;
758 
759 		lbn = lblkno(imp, ino);
760 		if (lbn >= imp->volume_space_size) {
761 			vput(vp);
762 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
763 			return (ESTALE);
764 		}
765 
766 		off = blkoff(imp, ino);
767 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
768 			vput(vp);
769 			printf("fhtovp: crosses block boundary %d\n",
770 			    off + ISO_DIRECTORY_RECORD_SIZE);
771 			return (ESTALE);
772 		}
773 
774 		error = bread(imp->im_devvp,
775 			      lbn << (imp->im_bshift - DEV_BSHIFT),
776 			      imp->logical_block_size, NOCRED, 0, &bp);
777 		if (error) {
778 			vput(vp);
779 			brelse(bp, 0);
780 			printf("fhtovp: bread error %d\n",error);
781 			return (error);
782 		}
783 		isodir = (struct iso_directory_record *)((char *)bp->b_data + off);
784 
785 		if (off + isonum_711(isodir->length) >
786 		    imp->logical_block_size) {
787 			vput(vp);
788 			if (bp != 0)
789 				brelse(bp, 0);
790 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
791 			    off +isonum_711(isodir->length), off,
792 			    isonum_711(isodir->length));
793 			return (ESTALE);
794 		}
795 
796 #if 0
797 		if (isonum_733(isodir->extent) +
798 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
799 			if (bp != 0)
800 				brelse(bp, 0);
801 			printf("fhtovp: file start miss %d vs %d\n",
802 			    isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
803 			    ifhp->ifid_start);
804 			return (ESTALE);
805 		}
806 #endif
807 	} else
808 		bp = 0;
809 
810 	vref(ip->i_devvp);
811 
812 	if (relocated) {
813 		/*
814 		 * On relocated directories we must
815 		 * read the `.' entry out of a dir.
816 		 */
817 		ip->iso_start = ino >> imp->im_bshift;
818 		if (bp != 0)
819 			brelse(bp, 0);
820 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
821 			vput(vp);
822 			return (error);
823 		}
824 		isodir = (struct iso_directory_record *)bp->b_data;
825 	}
826 
827 	ip->iso_extent = isonum_733(isodir->extent);
828 	ip->i_size = isonum_733(isodir->size);
829 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
830 
831 	/*
832 	 * Setup time stamp, attribute
833 	 */
834 	vp->v_type = VNON;
835 	switch (imp->iso_ftype) {
836 	default:	/* ISO_FTYPE_9660 */
837 	    {
838 		struct buf *bp2;
839 		int off;
840 		if ((imp->im_flags & ISOFSMNT_EXTATT)
841 		    && (off = isonum_711(isodir->ext_attr_length)))
842 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
843 			    NULL, &bp2);
844 		else
845 			bp2 = NULL;
846 		cd9660_defattr(isodir, ip, bp2);
847 		cd9660_deftstamp(isodir, ip, bp2);
848 		if (bp2)
849 			brelse(bp2, 0);
850 		break;
851 	    }
852 	case ISO_FTYPE_RRIP:
853 		cd9660_rrip_analyze(isodir, ip, imp);
854 		break;
855 	}
856 
857 	if (bp != 0)
858 		brelse(bp, 0);
859 
860 	/*
861 	 * Initialize the associated vnode
862 	 */
863 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
864 	case VFIFO:
865 		vp->v_op = cd9660_fifoop_p;
866 		break;
867 	case VCHR:
868 	case VBLK:
869 		/*
870 		 * if device, look at device number table for translation
871 		 */
872 		vp->v_op = cd9660_specop_p;
873 		spec_node_init(vp, ip->inode.iso_rdev);
874 		break;
875 	case VLNK:
876 	case VNON:
877 	case VSOCK:
878 	case VDIR:
879 	case VBAD:
880 		break;
881 	case VREG:
882 		uvm_vnp_setsize(vp, ip->i_size);
883 		break;
884 	}
885 
886 	if (vp->v_type != VREG)
887 		uvm_vnp_setsize(vp, 0);
888 
889 	if (ip->iso_extent == imp->root_extent)
890 		vp->v_vflag |= VV_ROOT;
891 
892 	/*
893 	 * XXX need generation number?
894 	 */
895 
896 	*vpp = vp;
897 	return (0);
898 }
899 
900 /*
901  * Vnode pointer to File handle
902  */
903 /* ARGSUSED */
904 int
905 cd9660_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
906 {
907 	struct iso_node *ip = VTOI(vp);
908 	struct ifid ifh;
909 
910 	if (*fh_size < sizeof(struct ifid)) {
911 		*fh_size = sizeof(struct ifid);
912 		return E2BIG;
913 	}
914 	*fh_size = sizeof(struct ifid);
915 
916 	memset(&ifh, 0, sizeof(ifh));
917 	ifh.ifid_len = sizeof(struct ifid);
918 	ifh.ifid_ino = ip->i_number;
919 	ifh.ifid_start = ip->iso_start;
920 	memcpy(fhp, &ifh, sizeof(ifh));
921 
922 #ifdef	ISOFS_DBG
923 	printf("vptofh: ino %d, start %ld\n",
924 	    ifh.ifid_ino,ifh.ifid_start);
925 #endif
926 	return 0;
927 }
928