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