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