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