xref: /freebsd-14-stable/sys/fs/cd9660/cd9660_vfsops.c (revision 32b7c5e7193ad051b637262b6c7cb315478609d1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley
8  * by Pace Willisson (pace@blitz.com).  The Rock Ridge Extension
9  * Support code is derived from software contributed to Berkeley
10  * by Atsushi Murai (amurai@spec.co.jp).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)cd9660_vfsops.c	8.18 (Berkeley) 5/22/95
37  */
38 
39 #include <sys/cdefs.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/namei.h>
43 #include <sys/priv.h>
44 #include <sys/proc.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/mount.h>
48 #include <sys/bio.h>
49 #include <sys/buf.h>
50 #include <sys/cdio.h>
51 #include <sys/conf.h>
52 #include <sys/fcntl.h>
53 #include <sys/malloc.h>
54 #include <sys/stat.h>
55 #include <sys/syslog.h>
56 #include <sys/iconv.h>
57 
58 #include <fs/cd9660/iso.h>
59 #include <fs/cd9660/iso_rrip.h>
60 #include <fs/cd9660/cd9660_node.h>
61 #include <fs/cd9660/cd9660_mount.h>
62 
63 #include <geom/geom.h>
64 #include <geom/geom_vfs.h>
65 
66 MALLOC_DEFINE(M_ISOFSMNT, "isofs_mount", "ISOFS mount structure");
67 MALLOC_DEFINE(M_ISOFSNODE, "isofs_node", "ISOFS vnode private part");
68 
69 struct iconv_functions *cd9660_iconv = NULL;
70 
71 static vfs_mount_t	cd9660_mount;
72 static vfs_cmount_t	cd9660_cmount;
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 
79 static struct vfsops cd9660_vfsops = {
80 	.vfs_fhtovp =		cd9660_fhtovp,
81 	.vfs_mount =		cd9660_mount,
82 	.vfs_cmount =		cd9660_cmount,
83 	.vfs_root =		cd9660_root,
84 	.vfs_statfs =		cd9660_statfs,
85 	.vfs_unmount =		cd9660_unmount,
86 	.vfs_vget =		cd9660_vget,
87 };
88 VFS_SET(cd9660_vfsops, cd9660, VFCF_READONLY);
89 MODULE_VERSION(cd9660, 1);
90 
91 static int cd9660_vfs_hash_cmp(struct vnode *vp, void *pino);
92 static int iso_mountfs(struct vnode *devvp, struct mount *mp);
93 
94 /*
95  * VFS Operations.
96  */
97 
98 static int
cd9660_cmount(struct mntarg * ma,void * data,uint64_t flags)99 cd9660_cmount(struct mntarg *ma, void *data, uint64_t flags)
100 {
101 	struct iso_args args;
102 	int error;
103 
104 	error = copyin(data, &args, sizeof args);
105 	if (error)
106 		return (error);
107 
108 	ma = mount_argsu(ma, "from", args.fspec, MAXPATHLEN);
109 	ma = mount_arg(ma, "export", &args.export, sizeof(args.export));
110 	if (args.flags & ISOFSMNT_UID)
111 		ma = mount_argf(ma, "uid", "%d", args.uid);
112 	if (args.flags & ISOFSMNT_GID)
113 		ma = mount_argf(ma, "gid", "%d", args.gid);
114 	ma = mount_argf(ma, "mask", "%d", args.fmask);
115 	ma = mount_argf(ma, "dirmask", "%d", args.dmask);
116 	ma = mount_argsu(ma, "cs_disk", args.cs_disk, 64);
117 	ma = mount_argsu(ma, "cs_local", args.cs_local, 64);
118 	ma = mount_argf(ma, "ssector", "%u", args.ssector);
119 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NORRIP), "norrip");
120 	ma = mount_argb(ma, args.flags & ISOFSMNT_GENS, "nogens");
121 	ma = mount_argb(ma, args.flags & ISOFSMNT_EXTATT, "noextatt");
122 	ma = mount_argb(ma, !(args.flags & ISOFSMNT_NOJOLIET), "nojoliet");
123 	ma = mount_argb(ma,
124 	    args.flags & ISOFSMNT_BROKENJOLIET, "nobrokenjoliet");
125 	ma = mount_argb(ma, args.flags & ISOFSMNT_KICONV, "nokiconv");
126 
127 	error = kernel_mount(ma, flags);
128 
129 	return (error);
130 }
131 
132 static int
cd9660_mount(struct mount * mp)133 cd9660_mount(struct mount *mp)
134 {
135 	struct vnode *devvp;
136 	struct thread *td;
137 	char *fspec;
138 	int error;
139 	accmode_t accmode;
140 	struct nameidata ndp;
141 	struct iso_mnt *imp = NULL;
142 
143 	td = curthread;
144 
145 	/*
146 	 * Unconditionally mount as read-only.
147 	 */
148 	MNT_ILOCK(mp);
149 	mp->mnt_flag |= MNT_RDONLY;
150 	MNT_IUNLOCK(mp);
151 
152 	fspec = vfs_getopts(mp->mnt_optnew, "from", &error);
153 	if (error)
154 		return (error);
155 
156 	imp = VFSTOISOFS(mp);
157 
158 	if (mp->mnt_flag & MNT_UPDATE) {
159 		if (vfs_flagopt(mp->mnt_optnew, "export", NULL, 0))
160 			return (0);
161 	}
162 	/*
163 	 * Not an update, or updating the name: look up the name
164 	 * and verify that it refers to a sensible block device.
165 	 */
166 	NDINIT(&ndp, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspec);
167 	if ((error = namei(&ndp)))
168 		return (error);
169 	NDFREE_PNBUF(&ndp);
170 	devvp = ndp.ni_vp;
171 
172 	if (!vn_isdisk_error(devvp, &error)) {
173 		vput(devvp);
174 		return (error);
175 	}
176 
177 	/*
178 	 * Verify that user has necessary permissions on the device,
179 	 * or has superuser abilities
180 	 */
181 	accmode = VREAD;
182 	error = VOP_ACCESS(devvp, accmode, td->td_ucred, td);
183 	if (error)
184 		error = priv_check(td, PRIV_VFS_MOUNT_PERM);
185 	if (error) {
186 		vput(devvp);
187 		return (error);
188 	}
189 
190 	if ((mp->mnt_flag & MNT_UPDATE) == 0) {
191 		error = iso_mountfs(devvp, mp);
192 		if (error)
193 			vrele(devvp);
194 	} else {
195 		if (devvp != imp->im_devvp)
196 			error = EINVAL;	/* needs translation */
197 		vput(devvp);
198 	}
199 	if (error)
200 		return (error);
201 	vfs_mountedfrom(mp, fspec);
202 	return (0);
203 }
204 
205 /*
206  * Common code for mount and mountroot
207  */
208 static int
iso_mountfs(struct vnode * devvp,struct mount * mp)209 iso_mountfs(struct vnode *devvp, struct mount *mp)
210 {
211 	struct iso_mnt *isomp = NULL;
212 	struct buf *bp = NULL;
213 	struct buf *pribp = NULL, *supbp = NULL;
214 	struct cdev *dev;
215 	int error = EINVAL;
216 	int high_sierra = 0;
217 	int iso_bsize;
218 	int iso_blknum;
219 	int joliet_level;
220 	int isverified = 0;
221 	struct iso_volume_descriptor *vdp = NULL;
222 	struct iso_primary_descriptor *pri = NULL;
223 	struct iso_sierra_primary_descriptor *pri_sierra = NULL;
224 	struct iso_supplementary_descriptor *sup = NULL;
225 	struct iso_directory_record *rootp;
226 	int logical_block_size, ssector;
227 	struct g_consumer *cp;
228 	struct bufobj *bo;
229 	char *cs_local, *cs_disk;
230 	int v;
231 
232 	dev = devvp->v_rdev;
233 	dev_ref(dev);
234 	g_topology_lock();
235 	error = g_vfs_open(devvp, &cp, "cd9660", 0);
236 	if (error == 0)
237 		g_getattr("MNT::verified", cp, &isverified);
238 	g_topology_unlock();
239 	VOP_UNLOCK(devvp);
240 	if (error)
241 		goto out;
242 	if (devvp->v_rdev->si_iosize_max != 0)
243 		mp->mnt_iosize_max = devvp->v_rdev->si_iosize_max;
244 	if (mp->mnt_iosize_max > maxphys)
245 		mp->mnt_iosize_max = maxphys;
246 
247 	bo = &devvp->v_bufobj;
248 
249 	/* This is the "logical sector size".  The standard says this
250 	 * should be 2048 or the physical sector size on the device,
251 	 * whichever is greater.
252 	 */
253 	if ((ISO_DEFAULT_BLOCK_SIZE % cp->provider->sectorsize) != 0) {
254 		error = EINVAL;
255 		goto out;
256 	}
257 
258 	iso_bsize = cp->provider->sectorsize;
259 
260 	joliet_level = 0;
261 	if (1 != vfs_scanopt(mp->mnt_optnew, "ssector", "%d", &ssector))
262 		ssector = 0;
263 	for (iso_blknum = 16 + ssector;
264 	     iso_blknum < 100 + ssector;
265 	     iso_blknum++) {
266 		if ((error = bread(devvp, iso_blknum * btodb(ISO_DEFAULT_BLOCK_SIZE),
267 				  iso_bsize, NOCRED, &bp)) != 0)
268 			goto out;
269 
270 		vdp = (struct iso_volume_descriptor *)bp->b_data;
271 		if (bcmp (vdp->id, ISO_STANDARD_ID, sizeof vdp->id) != 0) {
272 			if (bcmp (vdp->id_sierra, ISO_SIERRA_ID,
273 				  sizeof vdp->id_sierra) != 0) {
274 				error = EINVAL;
275 				goto out;
276 			} else
277 				high_sierra = 1;
278 		}
279 		switch (isonum_711 (high_sierra? vdp->type_sierra: vdp->type)){
280 		case ISO_VD_PRIMARY:
281 			if (pribp == NULL) {
282 				pribp = bp;
283 				bp = NULL;
284 				pri = (struct iso_primary_descriptor *)vdp;
285 				pri_sierra =
286 				  (struct iso_sierra_primary_descriptor *)vdp;
287 			}
288 			break;
289 
290 		case ISO_VD_SUPPLEMENTARY:
291 			if (supbp == NULL) {
292 				supbp = bp;
293 				bp = NULL;
294 				sup = (struct iso_supplementary_descriptor *)vdp;
295 
296 				if (!vfs_flagopt(mp->mnt_optnew, "nojoliet", NULL, 0)) {
297 					if (bcmp(sup->escape, "%/@", 3) == 0)
298 						joliet_level = 1;
299 					if (bcmp(sup->escape, "%/C", 3) == 0)
300 						joliet_level = 2;
301 					if (bcmp(sup->escape, "%/E", 3) == 0)
302 						joliet_level = 3;
303 
304 					if ((isonum_711 (sup->flags) & 1) &&
305 					    !vfs_flagopt(mp->mnt_optnew, "brokenjoliet", NULL, 0))
306 						joliet_level = 0;
307 				}
308 			}
309 			break;
310 
311 		case ISO_VD_END:
312 			goto vd_end;
313 
314 		default:
315 			break;
316 		}
317 		if (bp != NULL) {
318 			brelse(bp);
319 			bp = NULL;
320 		}
321 	}
322  vd_end:
323 	if (bp != NULL) {
324 		brelse(bp);
325 		bp = NULL;
326 	}
327 
328 	if (pri == NULL) {
329 		error = EINVAL;
330 		goto out;
331 	}
332 
333 	logical_block_size =
334 		isonum_723 (high_sierra?
335 			    pri_sierra->logical_block_size:
336 			    pri->logical_block_size);
337 
338 	if (logical_block_size < DEV_BSIZE || logical_block_size > MAXBSIZE
339 	    || (logical_block_size & (logical_block_size - 1)) != 0) {
340 		error = EINVAL;
341 		goto out;
342 	}
343 
344 	if (logical_block_size < cp->provider->sectorsize) {
345 		printf("cd9660: Unsupported logical block size %u\n",
346 		    logical_block_size);
347 		error = EINVAL;
348 		goto out;
349 	}
350 
351 	rootp = (struct iso_directory_record *)
352 		(high_sierra?
353 		 pri_sierra->root_directory_record:
354 		 pri->root_directory_record);
355 
356 	isomp = malloc(sizeof *isomp, M_ISOFSMNT, M_WAITOK | M_ZERO);
357 	isomp->im_cp = cp;
358 	isomp->im_bo = bo;
359 	isomp->logical_block_size = logical_block_size;
360 	isomp->volume_space_size =
361 		isonum_733 (high_sierra?
362 			    pri_sierra->volume_space_size:
363 			    pri->volume_space_size);
364 	isomp->joliet_level = 0;
365 	/*
366 	 * Since an ISO9660 multi-session CD can also access previous
367 	 * sessions, we have to include them into the space consider-
368 	 * ations.  This doesn't yield a very accurate number since
369 	 * parts of the old sessions might be inaccessible now, but we
370 	 * can't do much better.  This is also important for the NFS
371 	 * filehandle validation.
372 	 */
373 	isomp->volume_space_size += ssector;
374 	memcpy(isomp->root, rootp, sizeof isomp->root);
375 	isomp->root_extent = isonum_733 (rootp->extent);
376 	isomp->root_size = isonum_733 (rootp->size);
377 
378 	isomp->im_bmask = logical_block_size - 1;
379 	isomp->im_bshift = ffs(logical_block_size) - 1;
380 
381 	pribp->b_flags |= B_AGE;
382 	brelse(pribp);
383 	pribp = NULL;
384 	rootp = NULL;
385 	pri = NULL;
386 	pri_sierra = NULL;
387 
388 	mp->mnt_data = isomp;
389 	mp->mnt_stat.f_fsid.val[0] = dev2udev(dev);
390 	mp->mnt_stat.f_fsid.val[1] = mp->mnt_vfc->vfc_typenum;
391 	MNT_ILOCK(mp);
392 	if (isverified)
393 		mp->mnt_flag |= MNT_VERIFIED;
394 	mp->mnt_flag |= MNT_LOCAL;
395 	mp->mnt_kern_flag |= MNTK_LOOKUP_SHARED | MNTK_EXTENDED_SHARED;
396 	MNT_IUNLOCK(mp);
397 	isomp->im_mountp = mp;
398 	isomp->im_dev = dev;
399 	isomp->im_devvp = devvp;
400 	isomp->im_fmask = isomp->im_dmask = ALLPERMS;
401 
402 	vfs_flagopt(mp->mnt_optnew, "norrip", &isomp->im_flags, ISOFSMNT_NORRIP);
403 	vfs_flagopt(mp->mnt_optnew, "gens", &isomp->im_flags, ISOFSMNT_GENS);
404 	vfs_flagopt(mp->mnt_optnew, "extatt", &isomp->im_flags, ISOFSMNT_EXTATT);
405 	vfs_flagopt(mp->mnt_optnew, "nojoliet", &isomp->im_flags, ISOFSMNT_NOJOLIET);
406 	vfs_flagopt(mp->mnt_optnew, "kiconv", &isomp->im_flags, ISOFSMNT_KICONV);
407 
408 	if (vfs_scanopt(mp->mnt_optnew, "uid", "%d", &v) == 1) {
409 		isomp->im_flags |= ISOFSMNT_UID;
410 		isomp->im_uid = v;
411 	}
412 	if (vfs_scanopt(mp->mnt_optnew, "gid", "%d", &v) == 1) {
413 		isomp->im_flags |= ISOFSMNT_GID;
414 		isomp->im_gid = v;
415 	}
416 	if (vfs_scanopt(mp->mnt_optnew, "mask", "%d", &v) == 1) {
417 		isomp->im_fmask &= v;
418 	}
419 	if (vfs_scanopt(mp->mnt_optnew, "dirmask", "%d", &v) == 1) {
420 		isomp->im_dmask &= v;
421 	}
422 
423 	/* Check the Rock Ridge Extension support */
424 	if (!(isomp->im_flags & ISOFSMNT_NORRIP)) {
425 		if ((error = bread(isomp->im_devvp, (isomp->root_extent +
426 		    isonum_711(((struct iso_directory_record *)isomp->root)->
427 		    ext_attr_length)) << (isomp->im_bshift - DEV_BSHIFT),
428 		    isomp->logical_block_size, NOCRED, &bp)) != 0)
429 			goto out;
430 
431 		rootp = (struct iso_directory_record *)bp->b_data;
432 
433 		if ((isomp->rr_skip = cd9660_rrip_offset(rootp,isomp)) < 0) {
434 		    isomp->im_flags |= ISOFSMNT_NORRIP;
435 		} else {
436 		    isomp->im_flags &= ~ISOFSMNT_GENS;
437 		}
438 
439 		/*
440 		 * The contents are valid,
441 		 * but they will get reread as part of another vnode, so...
442 		 */
443 		bp->b_flags |= B_AGE;
444 		brelse(bp);
445 		bp = NULL;
446 		rootp = NULL;
447 	}
448 
449 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
450 		cs_local = vfs_getopts(mp->mnt_optnew, "cs_local", &error);
451 		if (error)
452 			goto out;
453 		cs_disk = vfs_getopts(mp->mnt_optnew, "cs_disk", &error);
454 		if (error)
455 			goto out;
456 		cd9660_iconv->open(cs_local, cs_disk, &isomp->im_d2l);
457 		cd9660_iconv->open(cs_disk, cs_local, &isomp->im_l2d);
458 	} else {
459 		isomp->im_d2l = NULL;
460 		isomp->im_l2d = NULL;
461 	}
462 
463 	if (high_sierra) {
464 		/* this effectively ignores all the mount flags */
465 		if (bootverbose)
466 			log(LOG_INFO, "cd9660: High Sierra Format\n");
467 		isomp->iso_ftype = ISO_FTYPE_HIGH_SIERRA;
468 	} else
469 		switch (isomp->im_flags&(ISOFSMNT_NORRIP|ISOFSMNT_GENS)) {
470 		  default:
471 			  isomp->iso_ftype = ISO_FTYPE_DEFAULT;
472 			  break;
473 		  case ISOFSMNT_GENS|ISOFSMNT_NORRIP:
474 			  isomp->iso_ftype = ISO_FTYPE_9660;
475 			  break;
476 		  case 0:
477 			  if (bootverbose)
478 			  	  log(LOG_INFO, "cd9660: RockRidge Extension\n");
479 			  isomp->iso_ftype = ISO_FTYPE_RRIP;
480 			  break;
481 		}
482 
483 	/* Decide whether to use the Joliet descriptor */
484 
485 	if (isomp->iso_ftype != ISO_FTYPE_RRIP && joliet_level) {
486 		if (bootverbose)
487 			log(LOG_INFO, "cd9660: Joliet Extension (Level %d)\n",
488 			    joliet_level);
489 		rootp = (struct iso_directory_record *)
490 			sup->root_directory_record;
491 		memcpy(isomp->root, rootp, sizeof isomp->root);
492 		isomp->root_extent = isonum_733 (rootp->extent);
493 		isomp->root_size = isonum_733 (rootp->size);
494 		isomp->joliet_level = joliet_level;
495 		supbp->b_flags |= B_AGE;
496 	}
497 
498 	if (supbp) {
499 		brelse(supbp);
500 		supbp = NULL;
501 		sup = NULL;
502 	}
503 
504 	return 0;
505 out:
506 	if (bp != NULL)
507 		brelse(bp);
508 	if (pribp != NULL)
509 		brelse(pribp);
510 	if (supbp != NULL)
511 		brelse(supbp);
512 	if (cp != NULL) {
513 		g_topology_lock();
514 		g_vfs_close(cp);
515 		g_topology_unlock();
516 	}
517 	if (isomp) {
518 		free(isomp, M_ISOFSMNT);
519 		mp->mnt_data = NULL;
520 	}
521 	dev_rel(dev);
522 	return error;
523 }
524 
525 /*
526  * unmount system call
527  */
528 static int
cd9660_unmount(struct mount * mp,int mntflags)529 cd9660_unmount(struct mount *mp, int mntflags)
530 {
531 	struct iso_mnt *isomp;
532 	int error, flags = 0;
533 
534 	if (mntflags & MNT_FORCE)
535 		flags |= FORCECLOSE;
536 	if ((error = vflush(mp, 0, flags, curthread)))
537 		return (error);
538 
539 	isomp = VFSTOISOFS(mp);
540 
541 	if (isomp->im_flags & ISOFSMNT_KICONV && cd9660_iconv) {
542 		if (isomp->im_d2l)
543 			cd9660_iconv->close(isomp->im_d2l);
544 		if (isomp->im_l2d)
545 			cd9660_iconv->close(isomp->im_l2d);
546 	}
547 	g_topology_lock();
548 	g_vfs_close(isomp->im_cp);
549 	g_topology_unlock();
550 	vrele(isomp->im_devvp);
551 	dev_rel(isomp->im_dev);
552 	free(isomp, M_ISOFSMNT);
553 	mp->mnt_data = NULL;
554 	return (error);
555 }
556 
557 /*
558  * Return root of a filesystem
559  */
560 static int
cd9660_root(struct mount * mp,int flags,struct vnode ** vpp)561 cd9660_root(struct mount *mp, int flags, struct vnode **vpp)
562 {
563 	struct iso_mnt *imp = VFSTOISOFS(mp);
564 	struct iso_directory_record *dp =
565 	    (struct iso_directory_record *)imp->root;
566 	ino_t ino = isodirino(dp, imp);
567 
568 	/*
569 	 * With RRIP we must use the `.' entry of the root directory.
570 	 * Simply tell vget, that it's a relocated directory.
571 	 */
572 	return (cd9660_vget_internal(mp, ino, flags, vpp,
573 	    imp->iso_ftype == ISO_FTYPE_RRIP, dp));
574 }
575 
576 /*
577  * Get filesystem statistics.
578  */
579 static int
cd9660_statfs(struct mount * mp,struct statfs * sbp)580 cd9660_statfs(struct mount *mp, struct statfs *sbp)
581 {
582 	struct iso_mnt *isomp;
583 
584 	isomp = VFSTOISOFS(mp);
585 
586 	sbp->f_bsize = isomp->logical_block_size;
587 	sbp->f_iosize = sbp->f_bsize;	/* XXX */
588 	sbp->f_blocks = isomp->volume_space_size;
589 	sbp->f_bfree = 0; /* total free blocks */
590 	sbp->f_bavail = 0; /* blocks free for non superuser */
591 	sbp->f_files =	0; /* total files */
592 	sbp->f_ffree = 0; /* free file nodes */
593 	return 0;
594 }
595 
596 /*
597  * File handle to vnode
598  *
599  * Have to be really careful about stale file handles:
600  * - check that the inode number is in range
601  * - call iget() to get the locked inode
602  * - check for an unallocated inode (i_mode == 0)
603  * - check that the generation number matches
604  */
605 
606 /* ARGSUSED */
607 static int
cd9660_fhtovp(struct mount * mp,struct fid * fhp,int flags,struct vnode ** vpp)608 cd9660_fhtovp(struct mount *mp, struct fid *fhp, int flags, struct vnode **vpp)
609 {
610 	struct ifid ifh;
611 	struct iso_node *ip;
612 	struct vnode *nvp;
613 	int error;
614 
615 	memcpy(&ifh, fhp, sizeof(ifh));
616 
617 #ifdef	ISOFS_DBG
618 	printf("fhtovp: ino %d, start %ld\n",
619 	    ifh.ifid_ino, ifh.ifid_start);
620 #endif
621 
622 	if ((error = VFS_VGET(mp, ifh.ifid_ino, LK_EXCLUSIVE, &nvp)) != 0) {
623 		*vpp = NULLVP;
624 		return (error);
625 	}
626 	ip = VTOI(nvp);
627 	if (ip->inode.iso_mode == 0) {
628 		vput(nvp);
629 		*vpp = NULLVP;
630 		return (ESTALE);
631 	}
632 	*vpp = nvp;
633 	vnode_create_vobject(*vpp, ip->i_size, curthread);
634 	return (0);
635 }
636 
637 /*
638  * Conform to standard VFS interface; can't vget arbitrary inodes beyond 4GB
639  * into media with current inode scheme and 32-bit ino_t.  This shouldn't be
640  * needed for anything other than nfsd, and who exports a mounted DVD over NFS?
641  */
642 static int
cd9660_vget(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp)643 cd9660_vget(struct mount *mp, ino_t ino, int flags, struct vnode **vpp)
644 {
645 
646 	/*
647 	 * XXXX
648 	 * It would be nice if we didn't always set the `relocated' flag
649 	 * and force the extra read, but I don't want to think about fixing
650 	 * that right now.
651 	 */
652 	return (cd9660_vget_internal(mp, ino, flags, vpp,
653 #if 0
654 	    VFSTOISOFS(mp)->iso_ftype == ISO_FTYPE_RRIP,
655 #else
656 	    0,
657 #endif
658 	    (struct iso_directory_record *)0));
659 }
660 
661 /* Use special comparator for full 64-bit ino comparison. */
662 static int
cd9660_vfs_hash_cmp(struct vnode * vp,void * pino)663 cd9660_vfs_hash_cmp(struct vnode *vp, void *pino)
664 {
665 	struct iso_node *ip;
666 	ino_t ino;
667 
668 	ip = VTOI(vp);
669 	ino = *(ino_t *)pino;
670 	return (ip->i_number != ino);
671 }
672 
673 int
cd9660_vget_internal(struct mount * mp,ino_t ino,int flags,struct vnode ** vpp,int relocated,struct iso_directory_record * isodir)674 cd9660_vget_internal(struct mount *mp, ino_t ino, int flags,
675     struct vnode **vpp, int relocated, struct iso_directory_record *isodir)
676 {
677 	struct iso_mnt *imp;
678 	struct iso_node *ip;
679 	struct buf *bp;
680 	struct vnode *vp;
681 	int error;
682 	struct thread *td;
683 
684 	td = curthread;
685 	error = vfs_hash_get(mp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
686 	    &ino);
687 	if (error || *vpp != NULL)
688 		return (error);
689 
690 	/*
691 	 * We must promote to an exclusive lock for vnode creation.  This
692 	 * can happen if lookup is passed LOCKSHARED.
693  	 */
694 	if ((flags & LK_TYPE_MASK) == LK_SHARED) {
695 		flags &= ~LK_TYPE_MASK;
696 		flags |= LK_EXCLUSIVE;
697 	}
698 
699 	/*
700 	 * We do not lock vnode creation as it is believed to be too
701 	 * expensive for such rare case as simultaneous creation of vnode
702 	 * for same ino by different processes. We just allow them to race
703 	 * and check later to decide who wins. Let the race begin!
704 	 */
705 
706 	imp = VFSTOISOFS(mp);
707 
708 	/* Allocate a new vnode/iso_node. */
709 	if ((error = getnewvnode("isofs", mp, &cd9660_vnodeops, &vp)) != 0) {
710 		*vpp = NULLVP;
711 		return (error);
712 	}
713 	ip = malloc(sizeof(struct iso_node), M_ISOFSNODE,
714 	    M_WAITOK | M_ZERO);
715 	vp->v_data = ip;
716 	ip->i_vnode = vp;
717 	ip->i_number = ino;
718 
719 	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
720 	error = insmntque(vp, mp);
721 	if (error != 0) {
722 		free(ip, M_ISOFSNODE);
723 		*vpp = NULLVP;
724 		return (error);
725 	}
726 	error = vfs_hash_insert(vp, ino, flags, td, vpp, cd9660_vfs_hash_cmp,
727 	    &ino);
728 	if (error || *vpp != NULL)
729 		return (error);
730 
731 	if (isodir == NULL) {
732 		int lbn, off;
733 
734 		lbn = lblkno(imp, ino);
735 		if (lbn >= imp->volume_space_size) {
736 			vput(vp);
737 			printf("fhtovp: lbn exceed volume space %d\n", lbn);
738 			return (ESTALE);
739 		}
740 
741 		off = blkoff(imp, ino);
742 		if (off + ISO_DIRECTORY_RECORD_SIZE > imp->logical_block_size) {
743 			vput(vp);
744 			printf("fhtovp: crosses block boundary %d\n",
745 			       off + ISO_DIRECTORY_RECORD_SIZE);
746 			return (ESTALE);
747 		}
748 
749 		error = bread(imp->im_devvp,
750 			      lbn << (imp->im_bshift - DEV_BSHIFT),
751 			      imp->logical_block_size, NOCRED, &bp);
752 		if (error) {
753 			vput(vp);
754 			printf("fhtovp: bread error %d\n",error);
755 			return (error);
756 		}
757 		isodir = (struct iso_directory_record *)(bp->b_data + off);
758 
759 		if (off + isonum_711(isodir->length) >
760 		    imp->logical_block_size) {
761 			vput(vp);
762 			brelse(bp);
763 			printf("fhtovp: directory crosses block boundary %d[off=%d/len=%d]\n",
764 			       off +isonum_711(isodir->length), off,
765 			       isonum_711(isodir->length));
766 			return (ESTALE);
767 		}
768 
769 #if 0
770 		if (isonum_733(isodir->extent) +
771 		    isonum_711(isodir->ext_attr_length) != ifhp->ifid_start) {
772 			brelse(bp);
773 			printf("fhtovp: file start miss %d vs %d\n",
774 			       isonum_733(isodir->extent) + isonum_711(isodir->ext_attr_length),
775 			       ifhp->ifid_start);
776 			return (ESTALE);
777 		}
778 #endif
779 	} else
780 		bp = NULL;
781 
782 	ip->i_mnt = imp;
783 
784 	if (relocated) {
785 		/*
786 		 * On relocated directories we must
787 		 * read the `.' entry out of a dir.
788 		 */
789 		ip->iso_start = ino >> imp->im_bshift;
790 		if (bp != NULL)
791 			brelse(bp);
792 		if ((error = cd9660_blkatoff(vp, (off_t)0, NULL, &bp)) != 0) {
793 			vput(vp);
794 			return (error);
795 		}
796 		isodir = (struct iso_directory_record *)bp->b_data;
797 	}
798 
799 	ip->iso_extent = isonum_733(isodir->extent);
800 	ip->i_size = isonum_733(isodir->size);
801 	ip->iso_start = isonum_711(isodir->ext_attr_length) + ip->iso_extent;
802 
803 	/*
804 	 * Setup time stamp, attribute
805 	 */
806 	vp->v_type = VNON;
807 	switch (imp->iso_ftype) {
808 	default:	/* ISO_FTYPE_9660 */
809 	    {
810 		struct buf *bp2;
811 		int off;
812 		if ((imp->im_flags & ISOFSMNT_EXTATT)
813 		    && (off = isonum_711(isodir->ext_attr_length)))
814 			cd9660_blkatoff(vp, (off_t)-(off << imp->im_bshift), NULL,
815 				     &bp2);
816 		else
817 			bp2 = NULL;
818 		cd9660_defattr(isodir, ip, bp2, ISO_FTYPE_9660);
819 		cd9660_deftstamp(isodir, ip, bp2, ISO_FTYPE_9660);
820 		if (bp2)
821 			brelse(bp2);
822 		break;
823 	    }
824 	case ISO_FTYPE_RRIP:
825 		cd9660_rrip_analyze(isodir, ip, imp);
826 		break;
827 	}
828 
829 	brelse(bp);
830 
831 	/*
832 	 * Initialize the associated vnode
833 	 */
834 	switch (vp->v_type = IFTOVT(ip->inode.iso_mode)) {
835 	case VFIFO:
836 		vp->v_op = &cd9660_fifoops;
837 		break;
838 	default:
839 		VN_LOCK_ASHARE(vp);
840 		break;
841 	}
842 
843 	if (ip->iso_extent == imp->root_extent)
844 		vp->v_vflag |= VV_ROOT;
845 
846 	/*
847 	 * XXX need generation number?
848 	 */
849 
850 	vn_set_state(vp, VSTATE_CONSTRUCTED);
851 	*vpp = vp;
852 	return (0);
853 }
854