xref: /dragonfly/sys/vfs/isofs/cd9660/cd9660_vfsops.c (revision 43b4d1bd)
1 /*-
2  * Copyright (c) 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley
6  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
7  * Support code is derived from software contributed to Berkeley
8  * by Atsushi Murai (amurai@spec.co.jp).
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)cd9660_vfsops.c	8.18 (Berkeley) 5/22/95
39  * $FreeBSD: src/sys/isofs/cd9660/cd9660_vfsops.c,v 1.74.2.7 2002/04/08 09:39:29 bde Exp $
40  * $DragonFly: src/sys/vfs/isofs/cd9660/cd9660_vfsops.c,v 1.20 2004/08/28 19:02:15 dillon Exp $
41  */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/proc.h>
46 #include <sys/namei.h>
47 #include <sys/kernel.h>
48 #include <sys/vnode.h>
49 #include <sys/mount.h>
50 #include <sys/buf.h>
51 #include <sys/cdio.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/malloc.h>
55 #include <sys/stat.h>
56 #include <sys/syslog.h>
57 
58 #include <vm/vm_zone.h>
59 
60 #include "iso.h"
61 #include "iso_rrip.h"
62 #include "cd9660_node.h"
63 #include "cd9660_mount.h"
64 
65 extern struct vnodeopv_entry_desc cd9660_vnodeop_entries[];
66 extern struct vnodeopv_entry_desc cd9660_specop_entries[];
67 extern struct vnodeopv_entry_desc cd9660_fifoop_entries[];
68 
69 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
70 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
71 
72 static int cd9660_mount (struct mount *,
73 	    char *, caddr_t, struct nameidata *, struct thread *);
74 static int cd9660_unmount (struct mount *, int, struct thread *);
75 static int cd9660_root (struct mount *, struct vnode **);
76 static int cd9660_statfs (struct mount *, struct statfs *, struct thread *);
77 static int cd9660_vget (struct mount *, ino_t, struct vnode **);
78 static int cd9660_fhtovp (struct mount *, struct fid *, struct vnode **);
79 static int cd9660_checkexp (struct mount *, struct sockaddr *,
80 	    int *, struct ucred **);
81 static int cd9660_vptofh (struct vnode *, struct fid *);
82 
83 static struct vfsops cd9660_vfsops = {
84 	cd9660_mount,
85 	vfs_stdstart,
86 	cd9660_unmount,
87 	cd9660_root,
88 	vfs_stdquotactl,
89 	cd9660_statfs,
90 	vfs_stdsync,
91 	cd9660_vget,
92 	cd9660_fhtovp,
93 	cd9660_checkexp,
94 	cd9660_vptofh,
95 	cd9660_init,
96 	cd9660_uninit,
97 	vfs_stdextattrctl,
98 };
99 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
100 MODULE_VERSION(cd9660, 1);
101 
102 
103 /*
104  * Called by vfs_mountroot when iso is going to be mounted as root.
105  */
106 
107 static int iso_get_ssector (dev_t dev, struct thread *td);
108 static int iso_mountfs (struct vnode *devvp, struct mount *mp,
109 			    struct thread *td, struct iso_args *argp);
110 
111 /*
112  * Try to find the start of the last data track on this CD-ROM.  This
113  * is used to mount the last session of a multi-session CD.  Bail out
114  * and return 0 if we fail, this is always a safe bet.
115  */
116 static int
117 iso_get_ssector(dev_t dev, struct thread *td)
118 {
119 	struct ioc_toc_header h;
120 	struct ioc_read_toc_single_entry t;
121 	int i;
122 
123 	if (dev_dioctl(dev, CDIOREADTOCHEADER, (caddr_t)&h, FREAD, td) != 0)
124 		return 0;
125 
126 	for (i = h.ending_track; i >= 0; i--) {
127 		t.address_format = CD_LBA_FORMAT;
128 		t.track = i;
129 		if (dev_dioctl(dev, CDIOREADTOCENTRY, (caddr_t)&t, FREAD, td) != 0) {
130 			return 0;
131 		}
132 		if ((t.entry.control & 4) != 0)
133 			/* found a data track */
134 			break;
135 	}
136 
137 	if (i < 0)
138 		return 0;
139 
140 	return ntohl(t.entry.addr.lba);
141 }
142 
143 static int iso_mountroot (struct mount *mp, struct thread *td);
144 
145 static int
146 iso_mountroot(struct mount *mp, struct thread *td)
147 {
148 	struct iso_args args;
149 	int error;
150 
151 	if ((error = bdevvp(rootdev, &rootvp))) {
152 		printf("iso_mountroot: can't find rootvp\n");
153 		return (error);
154 	}
155 	args.flags = ISOFSMNT_ROOT;
156 
157 	vn_lock(rootvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
158 	error = VOP_OPEN(rootvp, FREAD, FSCRED, td);
159 	VOP_UNLOCK(rootvp, NULL, 0, td);
160 	if (error)
161 		return (error);
162 
163 	args.ssector = iso_get_ssector(rootdev, td);
164 
165 	(void)VOP_CLOSE(rootvp, FREAD, td);
166 
167 	if (bootverbose)
168 		printf("iso_mountroot(): using session at block %d\n",
169 		       args.ssector);
170 	if ((error = iso_mountfs(rootvp, mp, td, &args)) != 0)
171 		return (error);
172 
173 	(void)cd9660_statfs(mp, &mp->mnt_stat, td);
174 	return (0);
175 }
176 
177 /*
178  * VFS Operations.
179  *
180  * mount system call
181  */
182 static int
183 cd9660_mount(struct mount *mp, char *path, caddr_t data, struct nameidata *ndp,
184 	     struct thread *td)
185 {
186 	struct vnode *devvp;
187 	struct iso_args args;
188 	size_t size;
189 	int error;
190 	mode_t accessmode;
191 	struct iso_mnt *imp = 0;
192 
193 	if ((mp->mnt_flag & MNT_ROOTFS) != 0) {
194 		return (iso_mountroot(mp, td));
195 	}
196 	if ((error = copyin(data, (caddr_t)&args, sizeof (struct iso_args))))
197 		return (error);
198 
199 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
200 		return (EROFS);
201 
202 	KKASSERT(td->td_proc);
203 
204 	/*
205 	 * If updating, check whether changing from read-only to
206 	 * read/write; if there is no device name, that's all we do.
207 	 */
208 	if (mp->mnt_flag & MNT_UPDATE) {
209 		imp = VFSTOISOFS(mp);
210 		if (args.fspec == 0)
211 			return (vfs_export(mp, &imp->im_export, &args.export));
212 	}
213 	/*
214 	 * Not an update, or updating the name: look up the name
215 	 * and verify that it refers to a sensible block device.
216 	 */
217 	NDINIT(ndp, NAMEI_LOOKUP, CNP_FOLLOW, UIO_USERSPACE, args.fspec, td);
218 	if ((error = namei(ndp)))
219 		return (error);
220 	NDFREE(ndp, NDF_ONLY_PNBUF);
221 	devvp = ndp->ni_vp;
222 
223 	if (!vn_isdisk(devvp, &error)) {
224 		vrele(devvp);
225 		return (error);
226 	}
227 
228 	/*
229 	 * Verify that user has necessary permissions on the device,
230 	 * or has superuser abilities
231 	 */
232 	accessmode = VREAD;
233 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
234 	error = VOP_ACCESS(devvp, accessmode, td->td_proc->p_ucred, td);
235 	if (error)
236 		error = suser(td);
237 	if (error) {
238 		vput(devvp);
239 		return (error);
240 	}
241 	VOP_UNLOCK(devvp, NULL, 0, td);
242 
243 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
244 		error = iso_mountfs(devvp, mp, td, &args);
245 	} else {
246 		if (devvp != imp->im_devvp)
247 			error = EINVAL;	/* needs translation */
248 		else
249 			vrele(devvp);
250 	}
251 	if (error) {
252 		vrele(devvp);
253 		return error;
254 	}
255 	imp = VFSTOISOFS(mp);
256 	(void) copyinstr(path, mp->mnt_stat.f_mntonname, MNAMELEN - 1, &size);
257 	bzero(mp->mnt_stat.f_mntonname + size, MNAMELEN - size);
258 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
259 	    &size);
260 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
261 	(void) cd9660_statfs(mp, &mp->mnt_stat, td);
262 	return 0;
263 }
264 
265 /*
266  * Common code for mount and mountroot
267  */
268 static int
269 iso_mountfs(struct vnode *devvp, struct mount *mp, struct thread *td,
270 	    struct iso_args *argp)
271 {
272 	struct iso_mnt *isomp = NULL;
273 	struct buf *bp = NULL;
274 	struct buf *pribp = NULL, *supbp = NULL;
275 	dev_t dev;
276 	int error = EINVAL;
277 	int needclose = 0;
278 	int high_sierra = 0;
279 	int iso_bsize;
280 	int iso_blknum;
281 	int joliet_level;
282 	struct iso_volume_descriptor *vdp = 0;
283 	struct iso_primary_descriptor *pri = NULL;
284 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
285 	struct iso_supplementary_descriptor *sup = NULL;
286 	struct iso_directory_record *rootp;
287 	int logical_block_size;
288 
289 	if (!(mp->mnt_flag & MNT_RDONLY))
290 		return EROFS;
291 
292 	/*
293 	 * Disallow multiple mounts of the same device.
294 	 * Disallow mounting of a device that is currently in use
295 	 * (except for root, which might share swap device for miniroot).
296 	 * Flush out any old buffers remaining from a previous use.
297 	 */
298 	if ((error = vfs_mountedon(devvp)))
299 		return error;
300 	if (count_udev(devvp->v_udev) > 0 && devvp != rootvp)
301 		return EBUSY;
302 	if ((error = vinvalbuf(devvp, V_SAVE, td, 0, 0)))
303 		return (error);
304 
305 	vn_lock(devvp, NULL, LK_EXCLUSIVE | LK_RETRY, td);
306 	error = VOP_OPEN(devvp, FREAD, FSCRED, td);
307 	VOP_UNLOCK(devvp, NULL, 0, td);
308 	if (error)
309 		return error;
310 	dev = devvp->v_rdev;
311 	if (dev->si_iosize_max != 0)
312 		mp->mnt_iosize_max = dev->si_iosize_max;
313 	if (mp->mnt_iosize_max > MAXPHYS)
314 		mp->mnt_iosize_max = MAXPHYS;
315 
316 	needclose = 1;
317 
318 	/* This is the "logical sector size".  The standard says this
319 	 * should be 2048 or the physical sector size on the device,
320 	 * whichever is greater.  For now, we'll just use a constant.
321 	 */
322 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
323 
324 	joliet_level = 0;
325 	for (iso_blknum = 16 + argp->ssector;
326 	     iso_blknum < 100 + argp->ssector;
327 	     iso_blknum++) {
328 		if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
329 				  iso_bsize, &bp)) != 0)
330 			goto out;
331 
332 		vdp = (struct iso_volume_descriptor *)bp->b_data;
333 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
334 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
335 				  sizeof vdp->id) != 0) {
336 				error = EINVAL;
337 				goto out;
338 			} else
339 				high_sierra = 1;
340 		}
341 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
342 		case ISO_VD_PRIMARY:
343 			if (pribp == NULL) {
344 				pribp = bp;
345 				bp = NULL;
346 				pri = (struct iso_primary_descriptor *)vdp;
347 				pri_sierra =
348 				  (struct iso_sierra_primary_descriptor *)vdp;
349 			}
350 			break;
351 
352 		case ISO_VD_SUPPLEMENTARY:
353 			if (supbp == NULL) {
354 				supbp = bp;
355 				bp = NULL;
356 				sup = (struct iso_supplementary_descriptor *)vdp;
357 
358 				if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
359 					if (bcmp(sup->escape, "%/@", 3) == 0)
360 						joliet_level = 1;
361 					if (bcmp(sup->escape, "%/C", 3) == 0)
362 						joliet_level = 2;
363 					if (bcmp(sup->escape, "%/E", 3) == 0)
364 						joliet_level = 3;
365 
366 					if ((isonum_711 (sup->flags) & 1) &&
367 					    (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
368 						joliet_level = 0;
369 				}
370 			}
371 			break;
372 
373 		case ISO_VD_END:
374 			goto vd_end;
375 
376 		default:
377 			break;
378 		}
379 		if (bp) {
380 			brelse(bp);
381 			bp = NULL;
382 		}
383 	}
384  vd_end:
385 	if (bp) {
386 		brelse(bp);
387 		bp = NULL;
388 	}
389 
390 	if (pri == NULL) {
391 		error = EINVAL;
392 		goto out;
393 	}
394 
395 	logical_block_size =
396 		isonum_723 (high_sierra?
397 			    pri_sierra->logical_block_size:
398 			    pri->logical_block_size);
399 
400 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
401 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
402 		error = EINVAL;
403 		goto out;
404 	}
405 
406 	rootp = (struct iso_directory_record *)
407 		(high_sierra?
408 		 pri_sierra->root_directory_record:
409 		 pri->root_directory_record);
410 
411 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
412 	isomp->logical_block_size = logical_block_size;
413 	isomp->volume_space_size =
414 		isonum_733 (high_sierra?
415 			    pri_sierra->volume_space_size:
416 			    pri->volume_space_size);
417 	isomp->joliet_level = 0;
418 	/*
419 	 * Since an ISO9660 multi-session CD can also access previous
420 	 * sessions, we have to include them into the space consider-
421 	 * ations.  This doesn't yield a very accurate number since
422 	 * parts of the old sessions might be inaccessible now, but we
423 	 * can't do much better.  This is also important for the NFS
424 	 * filehandle validation.
425 	 */
426 	isomp->volume_space_size += argp->ssector;
427 	bcopy (rootp, isomp->root, sizeof isomp->root);
428 	isomp->root_extent = isonum_733 (rootp->extent);
429 	isomp->root_size = isonum_733 (rootp->size);
430 
431 	isomp->im_bmask = logical_block_size - 1;
432 	isomp->im_bshift = ffs(logical_block_size) - 1;
433 
434 	pribp->b_flags |= B_AGE;
435 	brelse(pribp);
436 	pribp = NULL;
437 
438 	mp->mnt_data = (qaddr_t)isomp;
439 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
440 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
441 	mp->mnt_maxsymlinklen = 0;
442 	mp->mnt_flag |= MNT_LOCAL;
443 	isomp->im_mountp = mp;
444 	isomp->im_dev = dev;
445 	isomp->im_devvp = devvp;
446 
447 	dev->si_mountpoint = mp;
448 
449 	/* Check the Rock Ridge Extention support */
450 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
451 		if ((error = bread(isomp->im_devvp,
452 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
453 				  (isomp->im_bshift - DEV_BSHIFT),
454 				  isomp->logical_block_size, &bp)) != 0)
455 		    goto out;
456 
457 		rootp = (struct iso_directory_record *)bp->b_data;
458 
459 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
460 		    argp->flags	 |= ISOFSMNT_NORRIP;
461 		} else {
462 		    argp->flags	 &= ~ISOFSMNT_GENS;
463 		}
464 
465 		/*
466 		 * The contents are valid,
467 		 * but they will get reread as part of another vnode, so...
468 		 */
469 		bp->b_flags |= B_AGE;
470 		brelse(bp);
471 		bp = NULL;
472 	}
473 	isomp->im_flags = argp->flags & (ISOFSMNT_NORRIP | ISOFSMNT_GENS |
474 					 ISOFSMNT_EXTATT | ISOFSMNT_NOJOLIET);
475 
476 	if (high_sierra) {
477 		/* this effectively ignores all the mount flags */
478 		log(LOG_INFO, "cd9660: High Sierra Format\n");
479 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
480 	} else {
481 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
482 		  default:
483 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
484 			  break;
485 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
486 			  isomp->iso_ftype = ISO_FTYPE_9660;
487 			  break;
488 		  case 0:
489 			  log(LOG_INFO, "cd9660: RockRidge Extension\n");
490 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
491 			  break;
492 		}
493 	}
494 
495 	/* Decide whether to use the Joliet descriptor */
496 
497 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
498 		log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
499 		rootp = (struct iso_directory_record *)
500 			sup->root_directory_record;
501 		bcopy (rootp, isomp->root, sizeof isomp->root);
502 		isomp->root_extent = isonum_733 (rootp->extent);
503 		isomp->root_size = isonum_733 (rootp->size);
504 		isomp->joliet_level = joliet_level;
505 		supbp->b_flags |= B_AGE;
506 	}
507 
508 	if (supbp) {
509 		brelse(supbp);
510 		supbp = NULL;
511 	}
512 
513 	vfs_add_vnodeops(&mp->mnt_vn_ops, cd9660_vnodeop_entries);
514 	vfs_add_vnodeops(&mp->mnt_vn_spec_ops, cd9660_specop_entries);
515 	vfs_add_vnodeops(&mp->mnt_vn_fifo_ops, cd9660_fifoop_entries);
516 
517 	return 0;
518 out:
519 	dev->si_mountpoint = NULL;
520 	if (bp)
521 		brelse(bp);
522 	if (pribp)
523 		brelse(pribp);
524 	if (supbp)
525 		brelse(supbp);
526 	if (needclose)
527 		(void)VOP_CLOSE(devvp, FREAD, td);
528 	if (isomp) {
529 		free((caddr_t)isomp, M_ISOFSMNT);
530 		mp->mnt_data = (qaddr_t)0;
531 	}
532 	return error;
533 }
534 
535 /*
536  * unmount system call
537  */
538 static int
539 cd9660_unmount(struct mount *mp, int mntflags, struct thread *td)
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, 0, flags)))
552 		return (error);
553 
554 	isomp = VFSTOISOFS(mp);
555 
556 	isomp->im_devvp->v_rdev->si_mountpoint = NULL;
557 	error = VOP_CLOSE(isomp->im_devvp, FREAD, td);
558 	vrele(isomp->im_devvp);
559 	free((caddr_t)isomp, M_ISOFSMNT);
560 	mp->mnt_data = (qaddr_t)0;
561 	mp->mnt_flag &= ~MNT_LOCAL;
562 	return (error);
563 }
564 
565 /*
566  * Return root of a filesystem
567  */
568 static int
569 cd9660_root(struct mount *mp, struct vnode **vpp)
570 {
571 	struct iso_mnt *imp = VFSTOISOFS(mp);
572 	struct iso_directory_record *dp =
573 	    (struct iso_directory_record *)imp->root;
574 	ino_t ino = isodirino(dp, imp);
575 
576 	/*
577 	 * With RRIP we must use the `.' entry of the root directory.
578 	 * Simply tell vget, that it's a relocated directory.
579 	 */
580 	return (cd9660_vget_internal(mp, ino, vpp,
581 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
582 }
583 
584 /*
585  * Get file system statistics.
586  */
587 int
588 cd9660_statfs(struct mount *mp, struct statfs *sbp, struct thread *td)
589 {
590 	struct iso_mnt *isomp;
591 
592 	isomp = VFSTOISOFS(mp);
593 
594 	sbp->f_bsize = isomp->logical_block_size;
595 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
596 	sbp->f_blocks = isomp->volume_space_size;
597 	sbp->f_bfree = 0; /* total free blocks */
598 	sbp->f_bavail = 0; /* blocks free for non superuser */
599 	sbp->f_files =	0; /* total files */
600 	sbp->f_ffree = 0; /* free file nodes */
601 	if (sbp != &mp->mnt_stat) {
602 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
603 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
604 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
605 	}
606 	return 0;
607 }
608 
609 /*
610  * File handle to vnode
611  *
612  * Have to be really careful about stale file handles:
613  * - check that the inode number is in range
614  * - call iget() to get the locked inode
615  * - check for an unallocated inode (i_mode == 0)
616  * - check that the generation number matches
617  */
618 
619 struct ifid {
620 	ushort	ifid_len;
621 	ushort	ifid_pad;
622 	int	ifid_ino;
623 	long	ifid_start;
624 };
625 
626 /* ARGSUSED */
627 int
628 cd9660_fhtovp(struct mount *mp, struct fid *fhp, struct vnode **vpp)
629 {
630 	struct ifid *ifhp = (struct ifid *)fhp;
631 	struct iso_node *ip;
632 	struct vnode *nvp;
633 	int error;
634 
635 #ifdef	ISOFS_DBG
636 	printf("fhtovp: ino %d, start %ld\n",
637 	       ifhp->ifid_ino, ifhp->ifid_start);
638 #endif
639 
640 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) != 0) {
641 		*vpp = NULLVP;
642 		return (error);
643 	}
644 	ip = VTOI(nvp);
645 	if (ip->inode.iso_mode == 0) {
646 		vput(nvp);
647 		*vpp = NULLVP;
648 		return (ESTALE);
649 	}
650 	*vpp = nvp;
651 	return (0);
652 }
653 
654 int
655 cd9660_checkexp(struct mount *mp, struct sockaddr *nam, int *exflagsp,
656 		struct ucred **credanonp)
657 {
658 	struct netcred *np;
659 	struct iso_mnt *imp;
660 
661 	imp = VFSTOISOFS(mp);
662 
663 	/*
664 	 * Get the export permission structure for this <mp, client> tuple.
665 	 */
666 	np = vfs_export_lookup(mp, &imp->im_export, nam);
667 	if (np == NULL)
668 		return (EACCES);
669 
670 	*exflagsp = np->netc_exflags;
671 	*credanonp = &np->netc_anon;
672 	return (0);
673 }
674 
675 int
676 cd9660_vget(struct mount *mp, ino_t ino, struct vnode **vpp)
677 {
678 
679 	/*
680 	 * XXXX
681 	 * It would be nice if we didn't always set the `relocated' flag
682 	 * and force the extra read, but I don't want to think about fixing
683 	 * that right now.
684 	 */
685 	return (cd9660_vget_internal(mp, ino, vpp,
686 #if 0
687 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
688 #else
689 	    0,
690 #endif
691 	    (struct iso_directory_record *)0));
692 }
693 
694 int
695 cd9660_vget_internal(struct mount *mp, ino_t ino, struct vnode **vpp,
696 		     int relocated, struct iso_directory_record *isodir)
697 {
698 	struct thread *td = curthread;
699 	struct iso_mnt *imp;
700 	struct iso_node *ip;
701 	struct buf *bp;
702 	struct vnode *vp;
703 	dev_t dev;
704 	int error;
705 
706 	imp = VFSTOISOFS(mp);
707 	dev = imp->im_dev;
708 again:
709 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
710 		return (0);
711 
712 	/* Allocate a new vnode/iso_node. */
713 	error = getnewvnode(VT_ISOFS, mp, mp->mnt_vn_ops, &vp, 0, 0);
714 	if (error) {
715 		*vpp = NULLVP;
716 		return (error);
717 	}
718 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
719 	    M_WAITOK);
720 	bzero((caddr_t)ip, sizeof(struct iso_node));
721 	lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL, td);
722 	ip->i_vnode = vp;
723 	ip->i_dev = dev;
724 	ip->i_number = ino;
725 
726 	/*
727 	 * Insert it into the inode hash table and check for a collision.
728 	 * If a collision occurs, throw away the vnode and try again.
729 	 */
730 	if (cd9660_ihashins(ip) != 0) {
731 		printf("debug: cd9660 ihashins collision, retrying\n");
732 		vput(vp);
733 		free(ip, M_ISOFSNODE);
734 		goto again;
735 	}
736 	vp->v_data = ip;
737 
738 	if (isodir == 0) {
739 		int lbn, off;
740 
741 		lbn = lblkno(imp, ino);
742 		if (lbn >= imp->volume_space_size) {
743 			vput(vp);
744 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
745 			return (ESTALE);
746 		}
747 
748 		off = blkoff(imp, ino);
749 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
750 			vput(vp);
751 			printf("fhtovp: crosses block boundary %d\n",
752 			       off + ISO_DIRECTORY_RECORD_SIZE);
753 			return (ESTALE);
754 		}
755 
756 		error = bread(imp->im_devvp,
757 			      lbn << (imp->im_bshift - DEV_BSHIFT),
758 			      imp->logical_block_size, &bp);
759 		if (error) {
760 			vput(vp);
761 			brelse(bp);
762 			printf("fhtovp: bread error %d\n",error);
763 			return (error);
764 		}
765 		isodir = (struct iso_directory_record *)(bp->b_data + off);
766 
767 		if (off + isonum_711(isodir->length) >
768 		    imp->logical_block_size) {
769 			vput(vp);
770 			if (bp != 0)
771 				brelse(bp);
772 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
773 			       off +isonum_711(isodir->length), off,
774 			       isonum_711(isodir->length));
775 			return (ESTALE);
776 		}
777 
778 #if 0
779 		if (isonum_733(isodir->extent) +
780 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
781 			if (bp != 0)
782 				brelse(bp);
783 			printf("fhtovp: file start miss %d vs %d\n",
784 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
785 			       ifhp->ifid_start);
786 			return (ESTALE);
787 		}
788 #endif
789 	} else
790 		bp = 0;
791 
792 	ip->i_mnt = imp;
793 	ip->i_devvp = imp->im_devvp;
794 	vref(ip->i_devvp);
795 
796 	if (relocated) {
797 		/*
798 		 * On relocated directories we must
799 		 * read the `.' entry out of a dir.
800 		 */
801 		ip->iso_start = ino >> imp->im_bshift;
802 		if (bp != 0)
803 			brelse(bp);
804 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
805 			vput(vp);
806 			return (error);
807 		}
808 		isodir = (struct iso_directory_record *)bp->b_data;
809 	}
810 
811 	ip->iso_extent = isonum_733(isodir->extent);
812 	ip->i_size = isonum_733(isodir->size);
813 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
814 
815 	/*
816 	 * Setup time stamp, attribute
817 	 */
818 	vp->v_type = VNON;
819 	switch (imp->iso_ftype) {
820 	default:	/* ISO_FTYPE_9660 */
821 	    {
822 		struct buf *bp2;
823 		int off;
824 		if ((imp->im_flags & ISOFSMNT_EXTATT)
825 		    && (off = isonum_711(isodir->ext_attr_length)))
826 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
827 				     &bp2);
828 		else
829 			bp2 = NULL;
830 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
831 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
832 		if (bp2)
833 			brelse(bp2);
834 		break;
835 	    }
836 	case ISO_FTYPE_RRIP:
837 		cd9660_rrip_analyze(isodir, ip, imp);
838 		break;
839 	}
840 
841 	if (bp != 0)
842 		brelse(bp);
843 
844 	/*
845 	 * Initialize the associated vnode
846 	 */
847 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
848 	case VFIFO:
849 		vp->v_ops = mp->mnt_vn_fifo_ops;
850 		break;
851 	case VCHR:
852 	case VBLK:
853 		vp->v_ops = mp->mnt_vn_spec_ops;
854 		addaliasu(vp, ip->inode.iso_rdev);
855 		break;
856 	default:
857 		break;
858 	}
859 
860 	if (ip->iso_extent == imp->root_extent)
861 		vp->v_flag |= VROOT;
862 
863 	/*
864 	 * XXX need generation number?
865 	 */
866 
867 	*vpp = vp;
868 	return (0);
869 }
870 
871 /*
872  * Vnode pointer to File handle
873  */
874 /* ARGSUSED */
875 int
876 cd9660_vptofh(struct vnode *vp, struct fid *fhp)
877 {
878 	struct iso_node *ip = VTOI(vp);
879 	struct ifid *ifhp;
880 
881 	ifhp = (struct ifid *)fhp;
882 	ifhp->ifid_len = sizeof(struct ifid);
883 
884 	ifhp->ifid_ino = ip->i_number;
885 	ifhp->ifid_start = ip->iso_start;
886 
887 #ifdef	ISOFS_DBG
888 	printf("vptofh: ino %d, start %ld\n",
889 	       ifhp->ifid_ino,ifhp->ifid_start);
890 #endif
891 	return 0;
892 }
893