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  * %sccs.include.redist.c%
11  *
12  *	@(#)cd9660_vfsops.c	8.9 (Berkeley) 12/05/94
13  */
14 
15 #include <sys/param.h>
16 #include <sys/systm.h>
17 #include <sys/namei.h>
18 #include <sys/proc.h>
19 #include <sys/kernel.h>
20 #include <sys/vnode.h>
21 #include <miscfs/specfs/specdev.h>
22 #include <sys/mount.h>
23 #include <sys/buf.h>
24 #include <sys/file.h>
25 #include <sys/dkbad.h>
26 #include <sys/disklabel.h>
27 #include <sys/ioctl.h>
28 #include <sys/errno.h>
29 #include <sys/malloc.h>
30 #include <sys/stat.h>
31 
32 #include <isofs/cd9660/iso.h>
33 #include <isofs/cd9660/cd9660_node.h>
34 
35 extern int enodev ();
36 
37 struct vfsops cd9660_vfsops = {
38 	cd9660_mount,
39 	cd9660_start,
40 	cd9660_unmount,
41 	cd9660_root,
42 	cd9660_quotactl,
43 	cd9660_statfs,
44 	cd9660_sync,
45 	cd9660_vget,
46 	cd9660_fhtovp,
47 	cd9660_vptofh,
48 	cd9660_init,
49 };
50 
51 /*
52  * Called by vfs_mountroot when iso is going to be mounted as root.
53  *
54  * Name is updated by mount(8) after booting.
55  */
56 #define ROOTNAME	"root_device"
57 
58 static iso_mountfs();
59 
60 cd9660_mountroot()
61 {
62 	register struct mount *mp;
63 	extern struct vnode *rootvp;
64 	struct proc *p = curproc;	/* XXX */
65 	struct iso_mnt *imp;
66 	register struct fs *fs;
67 	u_int size;
68 	int error;
69 	struct iso_args args;
70 
71 	/*
72 	 * Get vnodes for swapdev and rootdev.
73 	 */
74 	if (bdevvp(swapdev, &swapdev_vp) || bdevvp(rootdev, &rootvp))
75 		panic("cd9660_mountroot: can't setup bdevvp's");
76 
77 	mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK);
78 	bzero((char *)mp, (u_long)sizeof(struct mount));
79 	mp->mnt_op = &cd9660_vfsops;
80 	mp->mnt_flag = MNT_RDONLY;
81 	LIST_INIT(&mp->mnt_vnodelist);
82 	args.flags = ISOFSMNT_ROOT;
83 	if (error = iso_mountfs(rootvp, mp, p, &args)) {
84 		free(mp, M_MOUNT);
85 		return (error);
86 	}
87 	if (error = vfs_lock(mp)) {
88 		(void)cd9660_unmount(mp, 0, p);
89 		free(mp, M_MOUNT);
90 		return (error);
91 	}
92 	TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list);
93 	mp->mnt_flag |= MNT_ROOTFS;
94 	mp->mnt_vnodecovered = NULLVP;
95 	imp = VFSTOISOFS(mp);
96 	bzero(imp->im_fsmnt, sizeof(imp->im_fsmnt));
97 	imp->im_fsmnt[0] = '/';
98 	bcopy((caddr_t)imp->im_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
99 	    MNAMELEN);
100 	(void) copystr(ROOTNAME, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
101 	    &size);
102 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
103 	(void) cd9660_statfs(mp, &mp->mnt_stat, p);
104 	vfs_unlock(mp);
105 	return (0);
106 }
107 
108 /*
109  * VFS Operations.
110  *
111  * mount system call
112  */
113 cd9660_mount(mp, path, data, ndp, p)
114 	register struct mount *mp;
115 	char *path;
116 	caddr_t data;
117 	struct nameidata *ndp;
118 	struct proc *p;
119 {
120 	struct vnode *devvp;
121 	struct iso_args args;
122 	u_int size;
123 	int error;
124 	struct iso_mnt *imp;
125 
126 	if (error = copyin(data, (caddr_t)&args, sizeof (struct iso_args)))
127 		return (error);
128 
129 	if ((mp->mnt_flag & MNT_RDONLY) == 0)
130 		return (EROFS);
131 
132 	/*
133 	 * If updating, check whether changing from read-only to
134 	 * read/write; if there is no device name, that's all we do.
135 	 */
136 	if (mp->mnt_flag & MNT_UPDATE) {
137 		imp = VFSTOISOFS(mp);
138 		if (args.fspec == 0)
139 			return (vfs_export(mp, &imp->im_export, &args.export));
140 	}
141 	/*
142 	 * Not an update, or updating the name: look up the name
143 	 * and verify that it refers to a sensible block device.
144 	 */
145 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, p);
146 	if (error = namei(ndp))
147 		return (error);
148 	devvp = ndp->ni_vp;
149 
150 	if (devvp->v_type != VBLK) {
151 		vrele(devvp);
152 		return ENOTBLK;
153 	}
154 	if (major(devvp->v_rdev) >= nblkdev) {
155 		vrele(devvp);
156 		return ENXIO;
157 	}
158 	if ((mp->mnt_flag & MNT_UPDATE) == 0)
159 		error = iso_mountfs(devvp, mp, p, &args);
160 	else {
161 		if (devvp != imp->im_devvp)
162 			error = EINVAL;	/* needs translation */
163 		else
164 			vrele(devvp);
165 	}
166 	if (error) {
167 		vrele(devvp);
168 		return error;
169 	}
170 	imp = VFSTOISOFS(mp);
171 	(void) copyinstr(path, imp->im_fsmnt, sizeof(imp->im_fsmnt)-1, &size);
172 	bzero(imp->im_fsmnt + size, sizeof(imp->im_fsmnt) - size);
173 	bcopy((caddr_t)imp->im_fsmnt, (caddr_t)mp->mnt_stat.f_mntonname,
174 	    MNAMELEN);
175 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
176 	    &size);
177 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
178 	(void) cd9660_statfs(mp, &mp->mnt_stat, p);
179 	return 0;
180 }
181 
182 /*
183  * Common code for mount and mountroot
184  */
185 static iso_mountfs(devvp, mp, p, argp)
186 	register struct vnode *devvp;
187 	struct mount *mp;
188 	struct proc *p;
189 	struct iso_args *argp;
190 {
191 	register struct iso_mnt *isomp = (struct iso_mnt *)0;
192 	struct buf *bp = NULL;
193 	dev_t dev = devvp->v_rdev;
194 	caddr_t base, space;
195 	int havepart = 0, blks;
196 	int error = EINVAL, i, size;
197 	int needclose = 0;
198 	int ronly = (mp->mnt_flag & MNT_RDONLY) != 0;
199 	extern struct vnode *rootvp;
200 	int j;
201 	int iso_bsize;
202 	int iso_blknum;
203 	struct iso_volume_descriptor *vdp;
204 	struct iso_primary_descriptor *pri;
205 	struct iso_directory_record *rootp;
206 	int logical_block_size;
207 
208 	if (!ronly)
209 		return EROFS;
210 
211 	/*
212 	 * Disallow multiple mounts of the same device.
213 	 * Disallow mounting of a device that is currently in use
214 	 * (except for root, which might share swap device for miniroot).
215 	 * Flush out any old buffers remaining from a previous use.
216 	 */
217 	if (error = vfs_mountedon(devvp))
218 		return error;
219 	if (vcount(devvp) > 1 && devvp != rootvp)
220 		return EBUSY;
221 	if (error = vinvalbuf(devvp, V_SAVE, p->p_ucred, p, 0, 0))
222 		return (error);
223 
224 	if (error = VOP_OPEN(devvp, ronly ? FREAD : FREAD|FWRITE, FSCRED, p))
225 		return error;
226 	needclose = 1;
227 
228 	/* This is the "logical sector size".  The standard says this
229 	 * should be 2048 or the physical sector size on the device,
230 	 * whichever is greater.  For now, we'll just use a constant.
231 	 */
232 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
233 
234 	for (iso_blknum = 16; iso_blknum < 100; iso_blknum++) {
235 		if (error = bread(devvp, iso_blknum * btodb(iso_bsize),
236 				  iso_bsize, NOCRED, &bp))
237 			goto out;
238 
239 		vdp = (struct iso_volume_descriptor *)bp->b_data;
240 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
241 			error = EINVAL;
242 			goto out;
243 		}
244 
245 		if (isonum_711 (vdp->type) == ISO_VD_END) {
246 			error = EINVAL;
247 			goto out;
248 		}
249 
250 		if (isonum_711 (vdp->type) == ISO_VD_PRIMARY)
251 			break;
252 		brelse(bp);
253 	}
254 
255 	if (isonum_711 (vdp->type) != ISO_VD_PRIMARY) {
256 		error = EINVAL;
257 		goto out;
258 	}
259 
260 	pri = (struct iso_primary_descriptor *)vdp;
261 
262 	logical_block_size = isonum_723 (pri->logical_block_size);
263 
264 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
265 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
266 		error = EINVAL;
267 		goto out;
268 	}
269 
270 	rootp = (struct iso_directory_record *)pri->root_directory_record;
271 
272 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK);
273 	bzero((caddr_t)isomp, sizeof *isomp);
274 	isomp->logical_block_size = logical_block_size;
275 	isomp->volume_space_size = isonum_733 (pri->volume_space_size);
276 	bcopy (rootp, isomp->root, sizeof isomp->root);
277 	isomp->root_extent = isonum_733 (rootp->extent);
278 	isomp->root_size = isonum_733 (rootp->size);
279 
280 	isomp->im_bmask = logical_block_size - 1;
281 	isomp->im_bshift = 0;
282 	while ((1 << isomp->im_bshift) < isomp->logical_block_size)
283 		isomp->im_bshift++;
284 
285 	bp->b_flags |= B_AGE;
286 	brelse(bp);
287 	bp = NULL;
288 
289 	mp->mnt_data = (qaddr_t)isomp;
290 	mp->mnt_stat.f_fsid.val[0] = (long)dev;
291 	mp->mnt_stat.f_fsid.val[1] = MOUNT_CD9660;
292 	mp->mnt_maxsymlinklen = 0;
293 	mp->mnt_flag |= MNT_LOCAL;
294 	isomp->im_mountp = mp;
295 	isomp->im_dev = dev;
296 	isomp->im_devvp = devvp;
297 
298 	devvp->v_specflags |= SI_MOUNTEDON;
299 
300 	/* Check the Rock Ridge Extention support */
301 	if (!(argp->flags & ISOFSMNT_NORRIP)) {
302 		if (error = bread(isomp->im_devvp,
303 				  (isomp->root_extent + isonum_711(rootp->ext_attr_length)) <<
304 				  (isomp->im_bshift - DEV_BSHIFT),
305 				  isomp->logical_block_size, NOCRED, &bp))
306 		    goto out;
307 
308 		rootp = (struct iso_directory_record *)bp->b_data;
309 
310 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
311 		    argp->flags  |= ISOFSMNT_NORRIP;
312 		} else {
313 		    argp->flags  &= ~ISOFSMNT_GENS;
314 		}
315 
316 		/*
317 		 * The contents are valid,
318 		 * but they will get reread as part of another vnode, so...
319 		 */
320 		bp->b_flags |= B_AGE;
321 		brelse(bp);
322 		bp = NULL;
323 	}
324 	isomp->im_flags = argp->flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS|ISOFSMNT_EXTATT);
325 	switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
326 	default:
327 	    isomp->iso_ftype = ISO_FTYPE_DEFAULT;
328 	    break;
329 	case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
330 	    isomp->iso_ftype = ISO_FTYPE_9660;
331 	    break;
332 	case 0:
333 	    isomp->iso_ftype = ISO_FTYPE_RRIP;
334 	    break;
335 	}
336 
337 	return 0;
338 out:
339 	if (bp)
340 		brelse(bp);
341 	if (needclose)
342 		(void)VOP_CLOSE(devvp, ronly ? FREAD : FREAD|FWRITE, NOCRED, p);
343 	if (isomp) {
344 		free((caddr_t)isomp, M_ISOFSMNT);
345 		mp->mnt_data = (qaddr_t)0;
346 	}
347 	return error;
348 }
349 
350 /*
351  * Make a filesystem operational.
352  * Nothing to do at the moment.
353  */
354 /* ARGSUSED */
355 cd9660_start(mp, flags, p)
356 	struct mount *mp;
357 	int flags;
358 	struct proc *p;
359 {
360 	return 0;
361 }
362 
363 /*
364  * unmount system call
365  */
366 int
367 cd9660_unmount(mp, mntflags, p)
368 	struct mount *mp;
369 	int mntflags;
370 	struct proc *p;
371 {
372 	register struct iso_mnt *isomp;
373 	int i, error, ronly, flags = 0;
374 
375 	if (mntflags & MNT_FORCE) {
376 		extern int doforce;
377 
378 		if (!doforce || (mp->mnt_flag & MNT_ROOTFS))
379 			return (EINVAL);
380 		flags |= FORCECLOSE;
381 	}
382 #if 0
383 	mntflushbuf(mp, 0);
384 	if (mntinvalbuf(mp))
385 		return EBUSY;
386 #endif
387 	if (error = vflush(mp, NULLVP, flags))
388 		return (error);
389 
390 	isomp = VFSTOISOFS(mp);
391 
392 #ifdef	ISODEVMAP
393 	if (isomp->iso_ftype == ISO_FTYPE_RRIP)
394 		iso_dunmap(isomp->im_dev);
395 #endif
396 
397 	isomp->im_devvp->v_specflags &= ~SI_MOUNTEDON;
398 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, p);
399 	vrele(isomp->im_devvp);
400 	free((caddr_t)isomp, M_ISOFSMNT);
401 	mp->mnt_data = (qaddr_t)0;
402 	mp->mnt_flag &= ~MNT_LOCAL;
403 	return (error);
404 }
405 
406 /*
407  * Return root of a filesystem
408  */
409 cd9660_root(mp, vpp)
410 	struct mount *mp;
411 	struct vnode **vpp;
412 {
413 	struct iso_mnt *imp = VFSTOISOFS(mp);
414 	struct iso_directory_record *dp =
415 	    (struct iso_directory_record *)imp->root;
416 	ino_t ino = isodirino(dp, imp);
417 
418 	/*
419 	 * With RRIP we must use the `.' entry of the root directory.
420 	 * Simply tell vget, that it's a relocated directory.
421 	 */
422 	return (cd9660_vget_internal(mp, ino, vpp,
423 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
424 }
425 
426 /*
427  * Do operations associated with quotas, not supported
428  */
429 /* ARGSUSED */
430 int
431 cd9660_quotactl(mp, cmd, uid, arg, p)
432 	struct mount *mp;
433 	int cmd;
434 	uid_t uid;
435 	caddr_t arg;
436 	struct proc *p;
437 {
438 
439 	return (EOPNOTSUPP);
440 }
441 
442 /*
443  * Get file system statistics.
444  */
445 cd9660_statfs(mp, sbp, p)
446 	struct mount *mp;
447 	register struct statfs *sbp;
448 	struct proc *p;
449 {
450 	register struct iso_mnt *isomp;
451 	register struct fs *fs;
452 
453 	isomp = VFSTOISOFS(mp);
454 
455 #ifdef COMPAT_09
456 	sbp->f_type = 5;
457 #else
458 	sbp->f_type = 0;
459 #endif
460 	sbp->f_bsize = isomp->logical_block_size;
461 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
462 	sbp->f_blocks = isomp->volume_space_size;
463 	sbp->f_bfree = 0; /* total free blocks */
464 	sbp->f_bavail = 0; /* blocks free for non superuser */
465 	sbp->f_files =  0; /* total files */
466 	sbp->f_ffree = 0; /* free file nodes */
467 	if (sbp != &mp->mnt_stat) {
468 		bcopy((caddr_t)mp->mnt_stat.f_mntonname,
469 			(caddr_t)&sbp->f_mntonname[0], MNAMELEN);
470 		bcopy((caddr_t)mp->mnt_stat.f_mntfromname,
471 			(caddr_t)&sbp->f_mntfromname[0], MNAMELEN);
472 	}
473 	/* Use the first spare for flags: */
474 	sbp->f_spare[0] = isomp->im_flags;
475 	return 0;
476 }
477 
478 /* ARGSUSED */
479 int
480 cd9660_sync(mp, waitfor, cred, p)
481 	struct mount *mp;
482 	int waitfor;
483 	struct ucred *cred;
484 	struct proc *p;
485 {
486 	return (0);
487 }
488 
489 /*
490  * File handle to vnode
491  *
492  * Have to be really careful about stale file handles:
493  * - check that the inode number is in range
494  * - call iget() to get the locked inode
495  * - check for an unallocated inode (i_mode == 0)
496  * - check that the generation number matches
497  */
498 
499 struct ifid {
500 	ushort	ifid_len;
501 	ushort	ifid_pad;
502 	int	ifid_ino;
503 	long	ifid_start;
504 };
505 
506 /* ARGSUSED */
507 int
508 cd9660_fhtovp(mp, fhp, nam, vpp, exflagsp, credanonp)
509 	register struct mount *mp;
510 	struct fid *fhp;
511 	struct mbuf *nam;
512 	struct vnode **vpp;
513 	int *exflagsp;
514 	struct ucred **credanonp;
515 {
516 	struct ifid *ifhp = (struct ifid *)fhp;
517 	register struct iso_node *ip;
518 	register struct netcred *np;
519 	register struct iso_mnt *imp = VFSTOISOFS(mp);
520 	struct vnode *nvp;
521 	int error;
522 
523 #ifdef	ISOFS_DBG
524 	printf("fhtovp: ino %d, start %ld\n",
525 	       ifhp->ifid_ino, ifhp->ifid_start);
526 #endif
527 
528 	/*
529 	 * Get the export permission structure for this <mp, client> tuple.
530 	 */
531 	np = vfs_export_lookup(mp, &imp->im_export, nam);
532 	if (np == NULL)
533 		return (EACCES);
534 
535 	if (error = VFS_VGET(mp, ifhp->ifid_ino, &nvp)) {
536 		*vpp = NULLVP;
537 		return (error);
538 	}
539 	ip = VTOI(nvp);
540 	if (ip->inode.iso_mode == 0) {
541 		vput(nvp);
542 		*vpp = NULLVP;
543 		return (ESTALE);
544 	}
545 	*vpp = nvp;
546 	*exflagsp = np->netc_exflags;
547 	*credanonp = &np->netc_anon;
548 	return (0);
549 }
550 
551 int
552 cd9660_vget(mp, ino, vpp)
553 	struct mount *mp;
554 	ino_t ino;
555 	struct vnode **vpp;
556 {
557 
558 	/*
559 	 * XXXX
560 	 * It would be nice if we didn't always set the `relocated' flag
561 	 * and force the extra read, but I don't want to think about fixing
562 	 * that right now.
563 	 */
564 	return (cd9660_vget_internal(mp, ino, vpp,
565 #if 0
566 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
567 #else
568 	    0,
569 #endif
570 	    (struct iso_directory_entry *)0));
571 }
572 
573 int
574 cd9660_vget_internal(mp, ino, vpp, relocated, isodir)
575 	struct mount *mp;
576 	ino_t ino;
577 	struct vnode **vpp;
578 	int relocated;
579 	struct iso_directory_record *isodir;
580 {
581 	register struct iso_mnt *imp;
582 	struct iso_node *ip;
583 	struct buf *bp;
584 	struct vnode *vp, *nvp;
585 	dev_t dev;
586 	int error;
587 
588 	imp = VFSTOISOFS(mp);
589 	dev = imp->im_dev;
590 	if ((*vpp = cd9660_ihashget(dev, ino)) != NULLVP)
591 		return (0);
592 
593 	/* Allocate a new vnode/iso_node. */
594 	if (error = getnewvnode(VT_ISOFS, mp, cd9660_vnodeop_p, &vp)) {
595 		*vpp = NULLVP;
596 		return (error);
597 	}
598 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
599 	    M_WAITOK);
600 	bzero((caddr_t)ip, sizeof(struct iso_node));
601 	vp->v_data = ip;
602 	ip->i_vnode = vp;
603 	ip->i_dev = dev;
604 	ip->i_number = ino;
605 
606 	/*
607 	 * Put it onto its hash chain and lock it so that other requests for
608 	 * this inode will block if they arrive while we are sleeping waiting
609 	 * for old data structures to be purged or for the contents of the
610 	 * disk portion of this inode to be read.
611 	 */
612 	cd9660_ihashins(ip);
613 
614 	if (isodir == 0) {
615 		int lbn, off;
616 
617 		lbn = lblkno(imp, ino);
618 		if (lbn >= imp->volume_space_size) {
619 			vput(vp);
620 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
621 			return (ESTALE);
622 		}
623 
624 		off = blkoff(imp, ino);
625 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
626 			vput(vp);
627 			printf("fhtovp: crosses block boundary %d\n",
628 			       off + ISO_DIRECTORY_RECORD_SIZE);
629 			return (ESTALE);
630 		}
631 
632 		error = bread(imp->im_devvp,
633 			      lbn << (imp->im_bshift - DEV_BSHIFT),
634 			      imp->logical_block_size, NOCRED, &bp);
635 		if (error) {
636 			vput(vp);
637 			brelse(bp);
638 			printf("fhtovp: bread error %d\n",error);
639 			return (error);
640 		}
641 		isodir = (struct iso_directory_record *)(bp->b_data + off);
642 
643 		if (off + isonum_711(isodir->length) >
644 		    imp->logical_block_size) {
645 			vput(vp);
646 			if (bp != 0)
647 				brelse(bp);
648 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
649 			       off +isonum_711(isodir->length), off,
650 			       isonum_711(isodir->length));
651 			return (ESTALE);
652 		}
653 
654 #if 0
655 		if (isonum_733(isodir->extent) +
656 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
657 			if (bp != 0)
658 				brelse(bp);
659 			printf("fhtovp: file start miss %d vs %d\n",
660 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
661 			       ifhp->ifid_start);
662 			return (ESTALE);
663 		}
664 #endif
665 	} else
666 		bp = 0;
667 
668 	ip->i_mnt = imp;
669 	ip->i_devvp = imp->im_devvp;
670 	VREF(ip->i_devvp);
671 
672 	if (relocated) {
673 		/*
674 		 * On relocated directories we must
675 		 * read the `.' entry out of a dir.
676 		 */
677 		ip->iso_start = ino >> imp->im_bshift;
678 		if (bp != 0)
679 			brelse(bp);
680 		if (error = VOP_BLKATOFF(vp, (off_t)0, NULL, &bp)) {
681 			vput(vp);
682 			return (error);
683 		}
684 		isodir = (struct iso_directory_record *)bp->b_data;
685 	}
686 
687 	ip->iso_extent = isonum_733(isodir->extent);
688 	ip->i_size = isonum_733(isodir->size);
689 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
690 
691 	/*
692 	 * Setup time stamp, attribute
693 	 */
694 	vp->v_type = VNON;
695 	switch (imp->iso_ftype) {
696 	default:	/* ISO_FTYPE_9660 */
697 	    {
698 		struct buf *bp2;
699 		int off;
700 		if ((imp->im_flags & ISOFSMNT_EXTATT)
701 		    && (off = isonum_711(isodir->ext_attr_length)))
702 			VOP_BLKATOFF(vp, (off_t)-(off << imp->im_bshift), NULL,
703 				     &bp2);
704 		else
705 			bp2 = NULL;
706 		cd9660_defattr(isodir, ip, bp2);
707 		cd9660_deftstamp(isodir, ip, bp2);
708 		if (bp2)
709 			brelse(bp2);
710 		break;
711 	    }
712 	case ISO_FTYPE_RRIP:
713 		cd9660_rrip_analyze(isodir, ip, imp);
714 		break;
715 	}
716 
717 	if (bp != 0)
718 		brelse(bp);
719 
720 	/*
721 	 * Initialize the associated vnode
722 	 */
723 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
724 	case VFIFO:
725 #ifdef	FIFO
726 		vp->v_op = cd9660_fifoop_p;
727 		break;
728 #else
729 		vput(vp);
730 		return (EOPNOTSUPP);
731 #endif	/* FIFO */
732 	case VCHR:
733 	case VBLK:
734 		/*
735 		 * if device, look at device number table for translation
736 		 */
737 #ifdef	ISODEVMAP
738 		if (dp = iso_dmap(dev, ino, 0))
739 			ip->inode.iso_rdev = dp->d_dev;
740 #endif
741 		vp->v_op = cd9660_specop_p;
742 		if (nvp = checkalias(vp, ip->inode.iso_rdev, mp)) {
743 			/*
744 			 * Discard unneeded vnode, but save its iso_node.
745 			 */
746 			cd9660_ihashrem(ip);
747 			VOP_UNLOCK(vp);
748 			nvp->v_data = vp->v_data;
749 			vp->v_data = NULL;
750 			vp->v_op = spec_vnodeop_p;
751 			vrele(vp);
752 			vgone(vp);
753 			/*
754 			 * Reinitialize aliased inode.
755 			 */
756 			vp = nvp;
757 			ip->i_vnode = vp;
758 			cd9660_ihashins(ip);
759 		}
760 		break;
761 	}
762 
763 	if (ip->iso_extent == imp->root_extent)
764 		vp->v_flag |= VROOT;
765 
766 	/*
767 	 * XXX need generation number?
768 	 */
769 
770 	*vpp = vp;
771 	return (0);
772 }
773 
774 /*
775  * Vnode pointer to File handle
776  */
777 /* ARGSUSED */
778 cd9660_vptofh(vp, fhp)
779 	struct vnode *vp;
780 	struct fid *fhp;
781 {
782 	register struct iso_node *ip = VTOI(vp);
783 	register struct ifid *ifhp;
784 	register struct iso_mnt *mp = ip->i_mnt;
785 
786 	ifhp = (struct ifid *)fhp;
787 	ifhp->ifid_len = sizeof(struct ifid);
788 
789 	ifhp->ifid_ino = ip->i_number;
790 	ifhp->ifid_start = ip->iso_start;
791 
792 #ifdef	ISOFS_DBG
793 	printf("vptofh: ino %d, start %ld\n",
794 	       ifhp->ifid_ino,ifhp->ifid_start);
795 #endif
796 	return 0;
797 }
798