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