xref: /netbsd-src/sys/fs/cd9660/cd9660_vfsops.c (revision d16b7486a53dcb8072b60ec6fcb4373a2d0c27b7)
1 /*	$NetBSD: cd9660_vfsops.c,v 1.97 2022/05/03 07:33:07 hannken 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.97 2022/05/03 07:33:07 hannken 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 extern const struct vnodeopv_desc cd9660_vnodeop_opv_desc;
82 extern const struct vnodeopv_desc cd9660_specop_opv_desc;
83 extern const struct vnodeopv_desc cd9660_fifoop_opv_desc;
84 
85 const struct vnodeopv_desc * const cd9660_vnodeopv_descs[] = {
86 	&cd9660_vnodeop_opv_desc,
87 	&cd9660_specop_opv_desc,
88 	&cd9660_fifoop_opv_desc,
89 	NULL,
90 };
91 
92 struct vfsops cd9660_vfsops = {
93 	.vfs_name = MOUNT_CD9660,
94 	.vfs_min_mount_data = sizeof (struct iso_args),
95 	.vfs_mount = cd9660_mount,
96 	.vfs_start = cd9660_start,
97 	.vfs_unmount = cd9660_unmount,
98 	.vfs_root = cd9660_root,
99 	.vfs_quotactl = (void *)eopnotsupp,
100 	.vfs_statvfs = cd9660_statvfs,
101 	.vfs_sync = cd9660_sync,
102 	.vfs_vget = cd9660_vget,
103 	.vfs_loadvnode = cd9660_loadvnode,
104 	.vfs_fhtovp = cd9660_fhtovp,
105 	.vfs_vptofh = cd9660_vptofh,
106 	.vfs_init = cd9660_init,
107 	.vfs_reinit = cd9660_reinit,
108 	.vfs_done = cd9660_done,
109 	.vfs_mountroot = cd9660_mountroot,
110 	.vfs_snapshot = (void *)eopnotsupp,
111 	.vfs_extattrctl = vfs_stdextattrctl,
112 	.vfs_suspendctl = genfs_suspendctl,
113 	.vfs_renamelock_enter = genfs_renamelock_enter,
114 	.vfs_renamelock_exit = genfs_renamelock_exit,
115 	.vfs_fsync = (void *)eopnotsupp,
116 	.vfs_opv_descs = cd9660_vnodeopv_descs
117 };
118 
119 static const struct genfs_ops cd9660_genfsops = {
120 	.gop_size = genfs_size,
121 };
122 
123 /*
124  * Called by vfs_mountroot when iso is going to be mounted as root.
125  *
126  * Name is updated by mount(8) after booting.
127  */
128 static int iso_makemp(struct iso_mnt *isomp, struct buf *bp, int *ea_len);
129 static int iso_mountfs(struct vnode *devvp, struct mount *mp,
130 		struct lwp *l, struct iso_args *argp);
131 
132 SYSCTL_SETUP(cd9660_sysctl_setup, "cd9660 sysctl")
133 {
134 
135 		sysctl_createv(clog, 0, NULL, NULL,
136 			       CTLFLAG_PERMANENT, CTLTYPE_NODE, "cd9660",
137 			       SYSCTL_DESCR("ISO-9660 file system"),
138 			       NULL, 0, NULL, 0,
139 			       CTL_VFS, 14, CTL_EOL);
140 		sysctl_createv(clog, 0, NULL, NULL,
141 			       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
142 			       CTLTYPE_INT, "utf8_joliet",
143 			       SYSCTL_DESCR("Encode Joliet filenames to UTF-8"),
144 			       NULL, 0, &cd9660_utf8_joliet, 0,
145 			       CTL_VFS, 14, CD9660_UTF8_JOLIET, CTL_EOL);
146 		/*
147 		 * XXX the "14" above could be dynamic, thereby eliminating
148 		 * one more instance of the "number to vfs" mapping problem,
149 		 * but "14" is the order as taken from sys/mount.h
150 		 */
151 }
152 
153 static int
154 cd9660_modcmd(modcmd_t cmd, void *arg)
155 {
156 	int error;
157 
158 	switch (cmd) {
159 	case MODULE_CMD_INIT:
160 		error = vfs_attach(&cd9660_vfsops);
161 		if (error != 0)
162 			break;
163 		break;
164 	case MODULE_CMD_FINI:
165 		error = vfs_detach(&cd9660_vfsops);
166 		if (error != 0)
167 			break;
168 		break;
169 	default:
170 		error = ENOTTY;
171 		break;
172 	}
173 
174 	return (error);
175 }
176 
177 int
178 cd9660_mountroot(void)
179 {
180 	struct mount *mp;
181 	struct lwp *l = curlwp;
182 	int error;
183 	struct iso_args args;
184 
185 	if (device_class(root_device) != DV_DISK)
186 		return (ENODEV);
187 
188 	if ((error = vfs_rootmountalloc(MOUNT_CD9660, "root_device", &mp))
189 			!= 0) {
190 		vrele(rootvp);
191 		return (error);
192 	}
193 
194 	args.flags = ISOFSMNT_ROOT;
195 	if ((error = iso_mountfs(rootvp, mp, l, &args)) != 0) {
196 		vfs_unbusy(mp);
197 		vfs_rele(mp);
198 		return (error);
199 	}
200 	mountlist_append(mp);
201 	(void)cd9660_statvfs(mp, &mp->mnt_stat);
202 	vfs_unbusy(mp);
203 	return (0);
204 }
205 
206 /*
207  * VFS Operations.
208  *
209  * mount system call
210  */
211 int
212 cd9660_mount(struct mount *mp, const char *path, void *data, size_t *data_len)
213 {
214 	struct lwp *l = curlwp;
215 	struct vnode *devvp;
216 	struct iso_args *args = data;
217 	int error;
218 	struct iso_mnt *imp = VFSTOISOFS(mp);
219 
220 	if (args == NULL)
221 		return EINVAL;
222 	if (*data_len < sizeof *args)
223 		return EINVAL;
224 
225 	if (mp->mnt_flag & MNT_GETARGS) {
226 		if (imp == NULL)
227 			return EIO;
228 		args->fspec = NULL;
229 		args->flags = imp->im_flags;
230 		*data_len = sizeof (*args);
231 		return 0;
232 	}
233 
234 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
235 		return (EROFS);
236 
237 	if ((mp->mnt_flag & MNT_UPDATE) && args->fspec == NULL)
238 		return EINVAL;
239 
240 	/*
241 	 * Not an update, or updating the name: look up the name
242 	 * and verify that it refers to a sensible block device.
243 	 */
244 	error = namei_simple_user(args->fspec,
245 				NSM_FOLLOW_NOEMULROOT, &devvp);
246 	if (error != 0)
247 		return (error);
248 
249 	if (devvp->v_type != VBLK) {
250 		vrele(devvp);
251 		return ENOTBLK;
252 	}
253 	if (bdevsw_lookup(devvp->v_rdev) == NULL) {
254 		vrele(devvp);
255 		return ENXIO;
256 	}
257 	/*
258 	 * If mount by non-root, then verify that user has necessary
259 	 * permissions on the device.
260 	 */
261 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
262 	error = kauth_authorize_system(l->l_cred, KAUTH_SYSTEM_MOUNT,
263 	    KAUTH_REQ_SYSTEM_MOUNT_DEVICE, mp, devvp, KAUTH_ARG(VREAD));
264 	if (error) {
265 		goto fail;
266 	}
267 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
268 		error = VOP_OPEN(devvp, FREAD, FSCRED);
269 		if (error)
270 			goto fail;
271 		VOP_UNLOCK(devvp);
272 		error = iso_mountfs(devvp, mp, l, args);
273 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
274 		if (error) {
275 			(void)VOP_CLOSE(devvp, FREAD, NOCRED);
276 			goto fail;
277 		}
278 		VOP_UNLOCK(devvp);
279 		/* reference to devvp is donated through iso_mountfs */
280 	} else {
281 		if (devvp != imp->im_devvp &&
282 		    devvp->v_rdev != imp->im_devvp->v_rdev) {
283 			error = EINVAL;		/* needs translation */
284 			goto fail;
285 		}
286 		VOP_UNLOCK(devvp);
287 		vrele(devvp);
288 	}
289 	return set_statvfs_info(path, UIO_USERSPACE, args->fspec, UIO_USERSPACE,
290 	    mp->mnt_op->vfs_name, mp, l);
291 
292 fail:
293 	VOP_UNLOCK(devvp);
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 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
362 	error = vinvalbuf(devvp, V_SAVE, l->l_cred, l, 0, 0);
363 	VOP_UNLOCK(devvp);
364 	if (error != 0)
365 		return (error);
366 
367 	/* This is the "logical sector size".  The standard says this
368 	 * should be 2048 or the physical sector size on the device,
369 	 * whichever is greater.  For now, we'll just use a constant.
370 	 */
371 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
372 
373 	error = VOP_IOCTL(devvp, DIOCGDINFO, &label, FREAD, FSCRED);
374 	if (!error) {
375 		/* XXX more sanity checks? */
376 		sess = label.d_partitions[DISKPART(dev)].p_cdsession;
377 	} else {
378 		/* fallback to old method */
379 		error = VOP_IOCTL(devvp, CDIOREADMSADDR, &sess, 0, FSCRED);
380 		if (error)
381 			sess = 0;	/* never mind */
382 	}
383 #ifdef ISO_DEBUG
384 	printf("isofs: session offset (part %"PRId32") %d\n", DISKPART(dev), sess);
385 #endif
386 
387 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
388 		if ((error = bread(devvp, (iso_blknum+sess) * btodb(iso_bsize),
389 				   iso_bsize, 0, &bp)) != 0)
390 			goto out;
391 
392 		vdp = (struct iso_volume_descriptor *)bp->b_data;
393 		if (memcmp(vdp->id, ISO_STANDARD_ID, sizeof(vdp->id)) != 0) {
394 			error = EINVAL;
395 			goto out;
396 		}
397 
398 		switch (isonum_711(vdp->type)) {
399 		case ISO_VD_PRIMARY:
400 			if (pribp == NULL) {
401 				pribp = bp;
402 				bp = NULL;
403 			}
404 			break;
405 
406 		case ISO_VD_SUPPLEMENTARY:
407 			if (supbp == NULL) {
408 				supbp = bp;
409 				bp = NULL;
410 			}
411 			break;
412 
413 		default:
414 			break;
415 		}
416 
417 		if (isonum_711 (vdp->type) == ISO_VD_END) {
418 			brelse(bp, 0);
419 			bp = NULL;
420 			break;
421 		}
422 
423 		if (bp != NULL) {
424 			brelse(bp, 0);
425 			bp = NULL;
426 		}
427 	}
428 
429 	if (pribp == NULL) {
430 		error = EINVAL;
431 		goto out;
432 	}
433 
434 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
435 	memset(isomp, 0, sizeof *isomp);
436 	if (iso_makemp(isomp, pribp, &ext_attr_length) == -1) {
437 		error = EINVAL;
438 		goto out;
439 	}
440 
441 	isomp->volume_space_size += sess;
442 
443 	brelse(pribp, BC_AGE);
444 	pribp = NULL;
445 
446 	mp->mnt_data = isomp;
447 	mp->mnt_stat.f_fsidx.__fsid_val[0] = (long)dev;
448 	mp->mnt_stat.f_fsidx.__fsid_val[1] = makefstype(MOUNT_CD9660);
449 	mp->mnt_stat.f_fsid = mp->mnt_stat.f_fsidx.__fsid_val[0];
450 	mp->mnt_stat.f_namemax = ISO_MAXNAMLEN;
451 	mp->mnt_flag |= MNT_LOCAL;
452 	mp->mnt_iflag |= IMNT_MPSAFE | IMNT_SHRLOOKUP;
453 	mp->mnt_dev_bshift = iso_bsize;
454 	mp->mnt_fs_bshift = isomp->im_bshift;
455 	isomp->im_mountp = mp;
456 	isomp->im_dev = dev;
457 	isomp->im_devvp = devvp;
458 
459 	/* Check the Rock Ridge Extension support */
460 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
461 		struct iso_directory_record *rootp;
462 
463 		if ((error = bread(isomp->im_devvp,
464 				   (isomp->root_extent + ext_attr_length) <<
465 				   (isomp->im_bshift - DEV_BSHIFT),
466 				   isomp->logical_block_size,
467 				   0, &bp)) != 0)
468 		    goto out;
469 
470 		rootp = (struct iso_directory_record *)bp->b_data;
471 
472 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
473 		    argp->flags  |= ISOFSMNT_NORRIP;
474 		} else {
475 		    argp->flags  &= ~ISOFSMNT_GENS;
476 		}
477 
478 		/*
479 		 * The contents are valid,
480 		 * but they will get reread as part of another vnode, so...
481 		 */
482 		brelse(bp, BC_AGE);
483 		bp = NULL;
484 	}
485 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
486 		 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET | ISOFSMNT_RRCASEINS);
487 
488 	if (isomp->im_flags & ISOFSMNT_GENS)
489 		isomp->iso_ftype = ISO_FTYPE_9660;
490 	else if (isomp->im_flags & ISOFSMNT_NORRIP) {
491 		isomp->iso_ftype = ISO_FTYPE_DEFAULT;
492 		if (argp->flags & ISOFSMNT_NOCASETRANS)
493 			isomp->im_flags |= ISOFSMNT_NOCASETRANS;
494 	} else
495 		isomp->iso_ftype = ISO_FTYPE_RRIP;
496 
497 	/* Check the Joliet Extension support */
498 	if ((argp->flags & ISOFSMNT_NORRIP) != 0 &&
499 	    (argp->flags & ISOFSMNT_NOJOLIET) == 0 &&
500 	    supbp != NULL) {
501 		joliet_level = 0;
502 		sup = (struct iso_supplementary_descriptor *)supbp->b_data;
503 
504 		if ((isonum_711(sup->flags) & 1) == 0) {
505 			if (memcmp(sup->escape, "%/@", 3) == 0)
506 				joliet_level = 1;
507 			if (memcmp(sup->escape, "%/C", 3) == 0)
508 				joliet_level = 2;
509 			if (memcmp(sup->escape, "%/E", 3) == 0)
510 				joliet_level = 3;
511 		}
512 		if (joliet_level != 0) {
513 			if (iso_makemp(isomp, supbp, NULL) == -1) {
514 				error = EINVAL;
515 				goto out;
516 			}
517 			isomp->im_joliet_level = joliet_level;
518 		}
519 	}
520 
521 	if (supbp != NULL) {
522 		brelse(supbp, 0);
523 		supbp = NULL;
524 	}
525 
526 	spec_node_setmountedfs(devvp, mp);
527 
528 	return 0;
529 out:
530 	if (bp)
531 		brelse(bp, 0);
532 	if (pribp)
533 		brelse(pribp, 0);
534 	if (supbp)
535 		brelse(supbp, 0);
536 	if (isomp) {
537 		free(isomp, M_ISOFSMNT);
538 		mp->mnt_data = NULL;
539 	}
540 	return error;
541 }
542 
543 /*
544  * Make a filesystem operational.
545  * Nothing to do at the moment.
546  */
547 /* ARGSUSED */
548 int
549 cd9660_start(struct mount *mp, int flags)
550 {
551 	return 0;
552 }
553 
554 /*
555  * unmount system call
556  */
557 int
558 cd9660_unmount(struct mount *mp, int mntflags)
559 {
560 	struct iso_mnt *isomp;
561 	int error, flags = 0;
562 
563 	if (mntflags & MNT_FORCE)
564 		flags |= FORCECLOSE;
565 	if ((error = vflush(mp, NULLVP, flags)) != 0)
566 		return (error);
567 
568 	isomp = VFSTOISOFS(mp);
569 
570 	if (isomp->im_devvp->v_type != VBAD)
571 		spec_node_setmountedfs(isomp->im_devvp, NULL);
572 
573 	vn_lock(isomp->im_devvp, LK_EXCLUSIVE | LK_RETRY);
574 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED);
575 	vput(isomp->im_devvp);
576 	free(isomp, M_ISOFSMNT);
577 	mp->mnt_data = NULL;
578 	mp->mnt_flag &= ~MNT_LOCAL;
579 	return (error);
580 }
581 
582 /*
583  * Return root of a filesystem
584  */
585 int
586 cd9660_root(struct mount *mp, int lktype, struct vnode **vpp)
587 {
588 	struct iso_mnt *imp = VFSTOISOFS(mp);
589 	struct iso_directory_record *dp =
590 	    (struct iso_directory_record *)imp->root;
591 	ino_t ino = isodirino(dp, imp);
592 
593 	return cd9660_vget(mp, ino, lktype, vpp);
594 }
595 
596 /*
597  * Get file system statistics.
598  */
599 int
600 cd9660_statvfs(struct mount *mp, struct statvfs *sbp)
601 {
602 	struct iso_mnt *isomp;
603 
604 	isomp = VFSTOISOFS(mp);
605 
606 	sbp->f_bsize = isomp->logical_block_size;
607 	sbp->f_frsize = sbp->f_bsize;
608 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
609 	sbp->f_blocks = isomp->volume_space_size;
610 	sbp->f_bfree = 0; /* total free blocks */
611 	sbp->f_bavail = 0; /* blocks free for non superuser */
612 	sbp->f_bresvd = 0; /* total reserved blocks */
613 	sbp->f_files =  0; /* total files */
614 	sbp->f_ffree = 0; /* free file nodes */
615 	sbp->f_favail = 0; /* free file nodes for non superuser */
616 	sbp->f_fresvd = 0; /* reserved file nodes */
617 	copy_statvfs_info(sbp, mp);
618 	/* Use the first spare for flags: */
619 	sbp->f_spare[0] = isomp->im_flags;
620 	return 0;
621 }
622 
623 /* ARGSUSED */
624 int
625 cd9660_sync(struct mount *mp, int waitfor, kauth_cred_t cred)
626 {
627 	return 0;
628 }
629 
630 /*
631  * File handle to vnode
632  *
633  * Have to be really careful about stale file handles:
634  * - check that the inode number is in range
635  * - call iget() to get the locked inode
636  * - check for an unallocated inode (i_mode == 0)
637  * - check that the generation number matches
638  */
639 
640 struct ifid {
641 	ushort	ifid_len;
642 	ushort	ifid_pad;
643 	ino_t	ifid_ino;
644 #ifdef	ISOFS_DBG
645 	u_long	ifid_start;
646 #endif
647 };
648 
649 /* ARGSUSED */
650 int
651 cd9660_fhtovp(struct mount *mp, struct fid *fhp, int lktype, struct vnode **vpp)
652 {
653 	struct ifid ifh;
654 	struct iso_node *ip;
655 	struct vnode *nvp;
656 	int error;
657 
658 	if (fhp->fid_len != sizeof(ifh))
659 		return EINVAL;
660 
661 	memcpy(&ifh, fhp, sizeof(ifh));
662 #ifdef	ISOFS_DBG
663 	printf("fhtovp: ino %"PRIu64", start %lu\n",
664 	    ifh.ifid_ino, ifh.ifid_start);
665 #endif
666 
667 	if ((error = VFS_VGET(mp, ifh.ifid_ino, lktype, &nvp)) != 0) {
668 		*vpp = NULLVP;
669 		return (error);
670 	}
671 	ip = VTOI(nvp);
672 	if (ip->inode.iso_mode == 0) {
673 		vput(nvp);
674 		*vpp = NULLVP;
675 		return (ESTALE);
676 	}
677 	*vpp = nvp;
678 	return (0);
679 }
680 
681 int
682 cd9660_vget(struct mount *mp, ino_t ino, int lktype, struct vnode **vpp)
683 {
684 	int error;
685 
686 	error = vcache_get(mp, &ino, sizeof(ino), vpp);
687 	if (error)
688 		return error;
689 	error = vn_lock(*vpp, lktype);
690 	if (error) {
691 		vrele(*vpp);
692 		*vpp = NULL;
693 		return error;
694 	}
695 	return 0;
696 }
697 
698 int
699 cd9660_loadvnode(struct mount *mp, struct vnode *vp,
700     const void *key, size_t key_len, const void **new_key)
701 {
702 	struct iso_mnt *imp;
703 	struct iso_node *ip;
704 	struct iso_directory_record *isodir;
705 	struct buf *bp;
706 	dev_t dev;
707 	ino_t ino;
708 	int lbn, off;
709 	int error;
710 
711 	KASSERT(key_len == sizeof(ino));
712 	memcpy(&ino, key, key_len);
713 	imp = VFSTOISOFS(mp);
714 	dev = imp->im_dev;
715 
716 	ip = pool_get(&cd9660_node_pool, PR_WAITOK);
717 
718 	memset(ip, 0, sizeof(struct iso_node));
719 	ip->i_vnode = vp;
720 	ip->i_dev = dev;
721 	ip->i_number = ino;
722 	ip->i_mnt = imp;
723 	ip->i_devvp = imp->im_devvp;
724 
725 	lbn = cd9660_lblkno(imp, ino);
726 	if (lbn >= imp->volume_space_size) {
727 		pool_put(&cd9660_node_pool, ip);
728 		printf("fhtovp: lbn exceed volume space %d\n", lbn);
729 		return (ESTALE);
730 	}
731 
732 	off = cd9660_blkoff(imp, ino);
733 	if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
734 		pool_put(&cd9660_node_pool, ip);
735 		printf("fhtovp: crosses block boundary %d\n",
736 		    off + ISO_DIRECTORY_RECORD_SIZE);
737 		return (ESTALE);
738 	}
739 
740 	error = bread(imp->im_devvp,
741 		      lbn << (imp->im_bshift - DEV_BSHIFT),
742 		      imp->logical_block_size, 0, &bp);
743 	if (error) {
744 		pool_put(&cd9660_node_pool, ip);
745 		printf("fhtovp: bread error %d\n",error);
746 		return (error);
747 	}
748 	isodir = (struct iso_directory_record *)((char *)bp->b_data + off);
749 
750 	if (off + isonum_711(isodir->length) > imp->logical_block_size) {
751 		pool_put(&cd9660_node_pool, ip);
752 		brelse(bp, 0);
753 		printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
754 		    off +isonum_711(isodir->length), off,
755 		    isonum_711(isodir->length));
756 		return (ESTALE);
757 	}
758 
759 #if 0
760 	if (isonum_733(isodir->extent) +
761 	    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
762 		pool_put(&cd9660_node_pool, ip);
763 		if (bp != 0)
764 			brelse(bp, 0);
765 		printf("fhtovp: file start miss %d vs %d\n",
766 		    isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
767 		    ifhp->ifid_start);
768 		return (ESTALE);
769 	}
770 #endif
771 
772 	ip->iso_extent = isonum_733(isodir->extent);
773 	ip->i_size = isonum_733(isodir->size);
774 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
775 
776 	vp->v_tag = VT_ISOFS;
777 	vp->v_op = cd9660_vnodeop_p;
778 	vp->v_data = ip;
779 	genfs_node_init(vp, &cd9660_genfsops);
780 
781 	/*
782 	 * Setup time stamp, attribute
783 	 */
784 	switch (imp->iso_ftype) {
785 	default:	/* ISO_FTYPE_9660 */
786 	    {
787 		struct buf *bp2;
788 		if ((imp->im_flags & ISOFSMNT_EXTATT)
789 		    && (off = isonum_711(isodir->ext_attr_length)))
790 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift),
791 			    NULL, &bp2);
792 		else
793 			bp2 = NULL;
794 		cd9660_defattr(isodir, ip, bp2);
795 		cd9660_deftstamp(isodir, ip, bp2);
796 		if (bp2)
797 			brelse(bp2, 0);
798 		break;
799 	    }
800 	case ISO_FTYPE_RRIP:
801 		cd9660_rrip_analyze(isodir, ip, imp);
802 		break;
803 	}
804 
805 	brelse(bp, 0);
806 
807 	/*
808 	 * Initialize the associated vnode
809 	 */
810 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
811 	case VFIFO:
812 		vp->v_op = cd9660_fifoop_p;
813 		break;
814 	case VCHR:
815 	case VBLK:
816 		/*
817 		 * if device, look at device number table for translation
818 		 */
819 		vp->v_op = cd9660_specop_p;
820 		spec_node_init(vp, ip->inode.iso_rdev);
821 		break;
822 	case VLNK:
823 	case VNON:
824 	case VSOCK:
825 	case VDIR:
826 	case VBAD:
827 		break;
828 	case VREG:
829 		uvm_vnp_setsize(vp, ip->i_size);
830 		break;
831 	}
832 
833 	if (vp->v_type != VREG)
834 		uvm_vnp_setsize(vp, 0);
835 
836 	if (ip->iso_extent == imp->root_extent)
837 		vp->v_vflag |= VV_ROOT;
838 
839 	/*
840 	 * XXX need generation number?
841 	 */
842 
843 	*new_key = &ip->i_number;
844 	return 0;
845 }
846 
847 /*
848  * Vnode pointer to File handle
849  */
850 /* ARGSUSED */
851 int
852 cd9660_vptofh(struct vnode *vp, struct fid *fhp, size_t *fh_size)
853 {
854 	struct iso_node *ip = VTOI(vp);
855 	struct ifid ifh;
856 
857 	if (*fh_size < sizeof(struct ifid)) {
858 		*fh_size = sizeof(struct ifid);
859 		return E2BIG;
860 	}
861 	*fh_size = sizeof(struct ifid);
862 
863 	memset(&ifh, 0, sizeof(ifh));
864 	ifh.ifid_len = sizeof(struct ifid);
865 	ifh.ifid_ino = ip->i_number;
866 #ifdef	ISOFS_DBG
867 	ifh.ifid_start = ip->iso_start;
868 #endif
869 	memcpy(fhp, &ifh, sizeof(ifh));
870 
871 #ifdef	ISOFS_DBG
872 	printf("vptofh: ino %"PRIu64", start %lu\n",
873 	    ifh.ifid_ino,ifh.ifid_start);
874 #endif
875 	return 0;
876 }
877