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