xref: /freebsd/sys/fs/cd9660/cd9660_vfsops.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
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  */
40 
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/namei.h>
47 #include <sys/proc.h>
48 #include <sys/kernel.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/bio.h>
52 #include <sys/buf.h>
53 #include <sys/cdio.h>
54 #include <sys/conf.h>
55 #include <sys/fcntl.h>
56 #include <sys/malloc.h>
57 #include <sys/stat.h>
58 #include <sys/syslog.h>
59 #include <sys/iconv.h>
60 
61 
62 #include <isofs/cd9660/iso.h>
63 #include <isofs/cd9660/iso_rrip.h>
64 #include <isofs/cd9660/cd9660_node.h>
65 #include <isofs/cd9660/cd9660_mount.h>
66 
67 MALLOC_DEFINE(M_ISOFSMNT, "ISOFS mount", "ISOFS mount structure");
68 MALLOC_DEFINE(M_ISOFSNODE, "ISOFS node", "ISOFS vnode private part");
69 
70 struct iconv_functions *cd9660_iconv = NULL;
71 
72 static vfs_mount_t	cd9660_mount;
73 static vfs_unmount_t	cd9660_unmount;
74 static vfs_root_t	cd9660_root;
75 static vfs_statfs_t	cd9660_statfs;
76 static vfs_vget_t	cd9660_vget;
77 static vfs_fhtovp_t	cd9660_fhtovp;
78 static vfs_vptofh_t	cd9660_vptofh;
79 
80 static struct vfsops cd9660_vfsops = {
81 	.vfs_fhtovp =		cd9660_fhtovp,
82 	.vfs_init =		cd9660_init,
83 	.vfs_mount =		cd9660_mount,
84 	.vfs_root =		cd9660_root,
85 	.vfs_statfs =		cd9660_statfs,
86 	.vfs_uninit =		cd9660_uninit,
87 	.vfs_unmount =		cd9660_unmount,
88 	.vfs_vget =		cd9660_vget,
89 	.vfs_vptofh =		cd9660_vptofh,
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, -1);
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 (path == NULL)	/* We are doing the initial root mount */
200 		return (iso_mountroot(mp, td));
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, &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 	NDINIT(ndp, LOOKUP, FOLLOW, UIO_USERSPACE, args.fspec, td);
221 	if ((error = namei(ndp)))
222 		return (error);
223 	NDFREE(ndp, NDF_ONLY_PNBUF);
224 	devvp = ndp->ni_vp;
225 
226 	if (!vn_isdisk(devvp, &error)) {
227 		vrele(devvp);
228 		return (error);
229 	}
230 
231 	/*
232 	 * Verify that user has necessary permissions on the device,
233 	 * or has superuser abilities
234 	 */
235 	accessmode = VREAD;
236 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
237 	error = VOP_ACCESS(devvp, accessmode, td->td_ucred, td);
238 	if (error)
239 		error = suser(td);
240 	if (error) {
241 		vput(devvp);
242 		return (error);
243 	}
244 	VOP_UNLOCK(devvp, 0, td);
245 
246 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
247 		error = iso_mountfs(devvp, mp, td, &args);
248 	} else {
249 		if (devvp != imp->im_devvp)
250 			error = EINVAL;	/* needs translation */
251 		else
252 			vrele(devvp);
253 	}
254 	if (error) {
255 		vrele(devvp);
256 		return error;
257 	}
258 	imp = VFSTOISOFS(mp);
259 	(void) copyinstr(args.fspec, mp->mnt_stat.f_mntfromname, MNAMELEN - 1,
260 	    &size);
261 	bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
262 	(void) cd9660_statfs(mp, &mp->mnt_stat, td);
263 	return 0;
264 }
265 
266 /*
267  * Common code for mount and mountroot
268  */
269 static int
270 iso_mountfs(devvp, mp, td, argp)
271 	register struct vnode *devvp;
272 	struct mount *mp;
273 	struct thread *td;
274 	struct iso_args *argp;
275 {
276 	register struct iso_mnt *isomp = (struct iso_mnt *)0;
277 	struct buf *bp = NULL;
278 	struct buf *pribp = NULL, *supbp = NULL;
279 	dev_t dev = devvp->v_rdev;
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 = 0;
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 
293 	if (!(mp->mnt_flag & MNT_RDONLY))
294 		return EROFS;
295 
296 	/*
297 	 * Disallow multiple mounts of the same device.
298 	 * Disallow mounting of a device that is currently in use
299 	 * (except for root, which might share swap device for miniroot).
300 	 * Flush out any old buffers remaining from a previous use.
301 	 */
302 	if ((error = vfs_mountedon(devvp)))
303 		return error;
304 	if (vcount(devvp) > 1 && devvp != rootvp)
305 		return EBUSY;
306 	if ((error = vinvalbuf(devvp, V_SAVE, td->td_ucred, td, 0, 0)))
307 		return (error);
308 
309 	vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY, td);
310 	error = VOP_OPEN(devvp, FREAD, FSCRED, td, -1);
311 	VOP_UNLOCK(devvp, 0, td);
312 	if (error)
313 		return error;
314 	if (devvp->v_rdev->si_iosize_max != 0)
315 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
316 	if (mp->mnt_iosize_max > MAXPHYS)
317 		mp->mnt_iosize_max = MAXPHYS;
318 
319 	needclose = 1;
320 
321 	/* This is the "logical sector size".  The standard says this
322 	 * should be 2048 or the physical sector size on the device,
323 	 * whichever is greater.  For now, we'll just use a constant.
324 	 */
325 	iso_bsize = ISO_DEFAULT_BLOCK_SIZE;
326 
327 	joliet_level = 0;
328 	for (iso_blknum = 16 + argp->ssector;
329 	     iso_blknum < 100 + argp->ssector;
330 	     iso_blknum++) {
331 		if ((error = bread(devvp, iso_blknum * btodb(iso_bsize),
332 				  iso_bsize, NOCRED, &bp)) != 0)
333 			goto out;
334 
335 		vdp = (struct iso_volume_descriptor *)bp->b_data;
336 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
337 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
338 				  sizeof vdp->id) != 0) {
339 				error = EINVAL;
340 				goto out;
341 			} else
342 				high_sierra = 1;
343 		}
344 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
345 		case ISO_VD_PRIMARY:
346 			if (pribp == NULL) {
347 				pribp = bp;
348 				bp = NULL;
349 				pri = (struct iso_primary_descriptor *)vdp;
350 				pri_sierra =
351 				  (struct iso_sierra_primary_descriptor *)vdp;
352 			}
353 			break;
354 
355 		case ISO_VD_SUPPLEMENTARY:
356 			if (supbp == NULL) {
357 				supbp = bp;
358 				bp = NULL;
359 				sup = (struct iso_supplementary_descriptor *)vdp;
360 
361 				if (!(argp->flags & ISOFSMNT_NOJOLIET)) {
362 					if (bcmp(sup->escape, "%/@", 3) == 0)
363 						joliet_level = 1;
364 					if (bcmp(sup->escape, "%/C", 3) == 0)
365 						joliet_level = 2;
366 					if (bcmp(sup->escape, "%/E", 3) == 0)
367 						joliet_level = 3;
368 
369 					if ((isonum_711 (sup->flags) & 1) &&
370 					    (argp->flags & ISOFSMNT_BROKENJOLIET) == 0)
371 						joliet_level = 0;
372 				}
373 			}
374 			break;
375 
376 		case ISO_VD_END:
377 			goto vd_end;
378 
379 		default:
380 			break;
381 		}
382 		if (bp) {
383 			brelse(bp);
384 			bp = NULL;
385 		}
386 	}
387  vd_end:
388 	if (bp) {
389 		brelse(bp);
390 		bp = NULL;
391 	}
392 
393 	if (pri == NULL) {
394 		error = EINVAL;
395 		goto out;
396 	}
397 
398 	logical_block_size =
399 		isonum_723 (high_sierra?
400 			    pri_sierra->logical_block_size:
401 			    pri->logical_block_size);
402 
403 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
404 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
405 		error = EINVAL;
406 		goto out;
407 	}
408 
409 	rootp = (struct iso_directory_record *)
410 		(high_sierra?
411 		 pri_sierra->root_directory_record:
412 		 pri->root_directory_record);
413 
414 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
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_rdev->si_mountpoint = 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, NOCRED, &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 					 ISOFSMNT_KICONV);
479 
480 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
481 		cd9660_iconv->open(argp->cs_local, argp->cs_disk, &isomp->im_d2l);
482 		cd9660_iconv->open(argp->cs_disk, argp->cs_local, &isomp->im_l2d);
483 	} else {
484 		isomp->im_d2l = NULL;
485 		isomp->im_l2d = NULL;
486 	}
487 
488 	if (high_sierra) {
489 		/* this effectively ignores all the mount flags */
490 		log(LOG_INFO, "cd9660: High Sierra Format\n");
491 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
492 	} else
493 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
494 		  default:
495 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
496 			  break;
497 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
498 			  isomp->iso_ftype = ISO_FTYPE_9660;
499 			  break;
500 		  case 0:
501 			  log(LOG_INFO, "cd9660: RockRidge Extension\n");
502 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
503 			  break;
504 		}
505 
506 	/* Decide whether to use the Joliet descriptor */
507 
508 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
509 		log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n", joliet_level);
510 		rootp = (struct iso_directory_record *)
511 			sup->root_directory_record;
512 		bcopy (rootp, isomp->root, sizeof isomp->root);
513 		isomp->root_extent = isonum_733 (rootp->extent);
514 		isomp->root_size = isonum_733 (rootp->size);
515 		isomp->joliet_level = joliet_level;
516 		supbp->b_flags |= B_AGE;
517 	}
518 
519 	if (supbp) {
520 		brelse(supbp);
521 		supbp = NULL;
522 	}
523 
524 	return 0;
525 out:
526 	devvp->v_rdev->si_mountpoint = NULL;
527 	if (bp)
528 		brelse(bp);
529 	if (pribp)
530 		brelse(pribp);
531 	if (supbp)
532 		brelse(supbp);
533 	if (needclose)
534 		(void)VOP_CLOSE(devvp, FREAD, NOCRED, td);
535 	if (isomp) {
536 		free((caddr_t)isomp, M_ISOFSMNT);
537 		mp->mnt_data = (qaddr_t)0;
538 	}
539 	return error;
540 }
541 
542 /*
543  * unmount system call
544  */
545 static int
546 cd9660_unmount(mp, mntflags, td)
547 	struct mount *mp;
548 	int mntflags;
549 	struct thread *td;
550 {
551 	register struct iso_mnt *isomp;
552 	int error, flags = 0;
553 
554 	if (mntflags & MNT_FORCE)
555 		flags |= FORCECLOSE;
556 #if 0
557 	mntflushbuf(mp, 0);
558 	if (mntinvalbuf(mp))
559 		return EBUSY;
560 #endif
561 	if ((error = vflush(mp, 0, flags)))
562 		return (error);
563 
564 	isomp = VFSTOISOFS(mp);
565 
566 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
567 		if (isomp->im_d2l)
568 			cd9660_iconv->close(isomp->im_d2l);
569 		if (isomp->im_l2d)
570 			cd9660_iconv->close(isomp->im_l2d);
571 	}
572 	isomp->im_devvp->v_rdev->si_mountpoint = NULL;
573 	error = VOP_CLOSE(isomp->im_devvp, FREAD, NOCRED, td);
574 	vrele(isomp->im_devvp);
575 	free((caddr_t)isomp, M_ISOFSMNT);
576 	mp->mnt_data = (qaddr_t)0;
577 	mp->mnt_flag &= ~MNT_LOCAL;
578 	return (error);
579 }
580 
581 /*
582  * Return root of a filesystem
583  */
584 static int
585 cd9660_root(mp, vpp)
586 	struct mount *mp;
587 	struct vnode **vpp;
588 {
589 	struct iso_mnt *imp = VFSTOISOFS(mp);
590 	struct iso_directory_record *dp =
591 	    (struct iso_directory_record *)imp->root;
592 	ino_t ino = isodirino(dp, imp);
593 
594 	/*
595 	 * With RRIP we must use the `.' entry of the root directory.
596 	 * Simply tell vget, that it's a relocated directory.
597 	 */
598 	return (cd9660_vget_internal(mp, ino, LK_EXCLUSIVE, vpp,
599 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
600 }
601 
602 /*
603  * Get filesystem statistics.
604  */
605 static int
606 cd9660_statfs(mp, sbp, td)
607 	struct mount *mp;
608 	register struct statfs *sbp;
609 	struct thread *td;
610 {
611 	register struct iso_mnt *isomp;
612 
613 	isomp = VFSTOISOFS(mp);
614 
615 	sbp->f_bsize = isomp->logical_block_size;
616 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
617 	sbp->f_blocks = isomp->volume_space_size;
618 	sbp->f_bfree = 0; /* total free blocks */
619 	sbp->f_bavail = 0; /* blocks free for non superuser */
620 	sbp->f_files =	0; /* total files */
621 	sbp->f_ffree = 0; /* free file nodes */
622 	if (sbp != &mp->mnt_stat) {
623 		sbp->f_type = mp->mnt_vfc->vfc_typenum;
624 		bcopy(mp->mnt_stat.f_mntonname, sbp->f_mntonname, MNAMELEN);
625 		bcopy(mp->mnt_stat.f_mntfromname, sbp->f_mntfromname, MNAMELEN);
626 	}
627 	return 0;
628 }
629 
630 /*
631  * File handle to vnode
632  *
633  * Have to be really careful about stale file handles:
634  * - check that the inode number is in range
635  * - call iget() to get the locked inode
636  * - check for an unallocated inode (i_mode == 0)
637  * - check that the generation number matches
638  */
639 
640 struct ifid {
641 	u_short	ifid_len;
642 	u_short	ifid_pad;
643 	int	ifid_ino;
644 	long	ifid_start;
645 };
646 
647 /* ARGSUSED */
648 static int
649 cd9660_fhtovp(mp, fhp, vpp)
650 	register struct mount *mp;
651 	struct fid *fhp;
652 	struct vnode **vpp;
653 {
654 	struct ifid *ifhp = (struct ifid *)fhp;
655 	register struct iso_node *ip;
656 	struct vnode *nvp;
657 	int error;
658 
659 #ifdef	ISOFS_DBG
660 	printf("fhtovp: ino %d, start %ld\n",
661 	       ifhp->ifid_ino, ifhp->ifid_start);
662 #endif
663 
664 	if ((error = VFS_VGET(mp, ifhp->ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
665 		*vpp = NULLVP;
666 		return (error);
667 	}
668 	ip = VTOI(nvp);
669 	if (ip->inode.iso_mode == 0) {
670 		vput(nvp);
671 		*vpp = NULLVP;
672 		return (ESTALE);
673 	}
674 	*vpp = nvp;
675 	return (0);
676 }
677 
678 static int
679 cd9660_vget(mp, ino, flags, vpp)
680 	struct mount *mp;
681 	ino_t ino;
682 	int flags;
683 	struct vnode **vpp;
684 {
685 
686 	/*
687 	 * XXXX
688 	 * It would be nice if we didn't always set the `relocated' flag
689 	 * and force the extra read, but I don't want to think about fixing
690 	 * that right now.
691 	 */
692 	return (cd9660_vget_internal(mp, ino, flags, vpp,
693 #if 0
694 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
695 #else
696 	    0,
697 #endif
698 	    (struct iso_directory_record *)0));
699 }
700 
701 int
702 cd9660_vget_internal(mp, ino, flags, vpp, relocated, isodir)
703 	struct mount *mp;
704 	ino_t ino;
705 	int flags;
706 	struct vnode **vpp;
707 	int relocated;
708 	struct iso_directory_record *isodir;
709 {
710 	struct iso_mnt *imp;
711 	struct iso_node *ip;
712 	struct buf *bp;
713 	struct vnode *vp;
714 	dev_t dev;
715 	int error;
716 
717 	imp = VFSTOISOFS(mp);
718 	dev = imp->im_dev;
719 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0)
720 		return (error);
721 	if (*vpp != NULL)
722 		return (0);
723 
724 	/* Allocate a new vnode/iso_node. */
725 	if ((error = getnewvnode("isofs", mp, cd9660_vnodeop_p, &vp)) != 0) {
726 		*vpp = NULLVP;
727 		return (error);
728 	}
729 	MALLOC(ip, struct iso_node *, sizeof(struct iso_node), M_ISOFSNODE,
730 	    M_WAITOK | M_ZERO);
731 	vp->v_data = ip;
732 	ip->i_vnode = vp;
733 	ip->i_dev = dev;
734 	ip->i_number = ino;
735 
736 	/*
737 	 * Check to be sure that it did not show up. We have to put it
738 	 * on the hash chain as the cleanup from vput expects to find
739 	 * it there.
740 	 */
741 	if ((error = cd9660_ihashget(dev, ino, flags, vpp)) != 0 ||
742 	    *vpp != NULL) {
743 		cd9660_ihashins(ip);
744 		vput(vp);
745 		return (error);
746 	}
747 
748 	/*
749 	 * Put it onto its hash chain and lock it so that other requests for
750 	 * this inode will block if they arrive while we are sleeping waiting
751 	 * for old data structures to be purged or for the contents of the
752 	 * disk portion of this inode to be read.
753 	 */
754 	cd9660_ihashins(ip);
755 
756 	if (isodir == 0) {
757 		int lbn, off;
758 
759 		lbn = lblkno(imp, ino);
760 		if (lbn >= imp->volume_space_size) {
761 			vput(vp);
762 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
763 			return (ESTALE);
764 		}
765 
766 		off = blkoff(imp, ino);
767 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
768 			vput(vp);
769 			printf("fhtovp: crosses block boundary %d\n",
770 			       off + ISO_DIRECTORY_RECORD_SIZE);
771 			return (ESTALE);
772 		}
773 
774 		error = bread(imp->im_devvp,
775 			      lbn << (imp->im_bshift - DEV_BSHIFT),
776 			      imp->logical_block_size, NOCRED, &bp);
777 		if (error) {
778 			vput(vp);
779 			brelse(bp);
780 			printf("fhtovp: bread error %d\n",error);
781 			return (error);
782 		}
783 		isodir = (struct iso_directory_record *)(bp->b_data + off);
784 
785 		if (off + isonum_711(isodir->length) >
786 		    imp->logical_block_size) {
787 			vput(vp);
788 			if (bp != 0)
789 				brelse(bp);
790 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
791 			       off +isonum_711(isodir->length), off,
792 			       isonum_711(isodir->length));
793 			return (ESTALE);
794 		}
795 
796 #if 0
797 		if (isonum_733(isodir->extent) +
798 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
799 			if (bp != 0)
800 				brelse(bp);
801 			printf("fhtovp: file start miss %d vs %d\n",
802 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
803 			       ifhp->ifid_start);
804 			return (ESTALE);
805 		}
806 #endif
807 	} else
808 		bp = 0;
809 
810 	ip->i_mnt = imp;
811 	ip->i_devvp = imp->im_devvp;
812 	VREF(ip->i_devvp);
813 
814 	if (relocated) {
815 		/*
816 		 * On relocated directories we must
817 		 * read the `.' entry out of a dir.
818 		 */
819 		ip->iso_start = ino >> imp->im_bshift;
820 		if (bp != 0)
821 			brelse(bp);
822 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
823 			vput(vp);
824 			return (error);
825 		}
826 		isodir = (struct iso_directory_record *)bp->b_data;
827 	}
828 
829 	ip->iso_extent = isonum_733(isodir->extent);
830 	ip->i_size = isonum_733(isodir->size);
831 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
832 
833 	/*
834 	 * Setup time stamp, attribute
835 	 */
836 	vp->v_type = VNON;
837 	switch (imp->iso_ftype) {
838 	default:	/* ISO_FTYPE_9660 */
839 	    {
840 		struct buf *bp2;
841 		int off;
842 		if ((imp->im_flags & ISOFSMNT_EXTATT)
843 		    && (off = isonum_711(isodir->ext_attr_length)))
844 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
845 				     &bp2);
846 		else
847 			bp2 = NULL;
848 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
849 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
850 		if (bp2)
851 			brelse(bp2);
852 		break;
853 	    }
854 	case ISO_FTYPE_RRIP:
855 		cd9660_rrip_analyze(isodir, ip, imp);
856 		break;
857 	}
858 
859 	if (bp != 0)
860 		brelse(bp);
861 
862 	/*
863 	 * Initialize the associated vnode
864 	 */
865 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
866 	case VFIFO:
867 		vp->v_op = cd9660_fifoop_p;
868 		break;
869 	case VBLK:
870 		vp->v_op = cd9660_specop_p;
871 		break;
872 	case VCHR:
873 		vp->v_op = cd9660_specop_p;
874 		vp = addaliasu(vp, ip->inode.iso_rdev);
875 		ip->i_vnode = vp;
876 		break;
877 	default:
878 		break;
879 	}
880 
881 	if (ip->iso_extent == imp->root_extent)
882 		vp->v_vflag |= VV_ROOT;
883 
884 	/*
885 	 * XXX need generation number?
886 	 */
887 
888 	*vpp = vp;
889 	return (0);
890 }
891 
892 /*
893  * Vnode pointer to File handle
894  */
895 /* ARGSUSED */
896 static int
897 cd9660_vptofh(vp, fhp)
898 	struct vnode *vp;
899 	struct fid *fhp;
900 {
901 	register struct iso_node *ip = VTOI(vp);
902 	register struct ifid *ifhp;
903 
904 	ifhp = (struct ifid *)fhp;
905 	ifhp->ifid_len = sizeof(struct ifid);
906 
907 	ifhp->ifid_ino = ip->i_number;
908 	ifhp->ifid_start = ip->iso_start;
909 
910 #ifdef	ISOFS_DBG
911 	printf("vptofh: ino %d, start %ld\n",
912 	       ifhp->ifid_ino,ifhp->ifid_start);
913 #endif
914 	return 0;
915 }
916