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