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