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