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