xref: /titanic_44/usr/src/uts/common/fs/hsfs/hsfs_vfsops.c (revision 0398691684c2596072212e4ca9d7033ad7ccfa54)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  * VFS operations for High Sierra filesystem
30  */
31 
32 #include <sys/types.h>
33 #include <sys/isa_defs.h>
34 #include <sys/t_lock.h>
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysmacros.h>
38 #include <sys/kmem.h>
39 #include <sys/signal.h>
40 #include <sys/user.h>
41 #include <sys/proc.h>
42 #include <sys/disp.h>
43 #include <sys/buf.h>
44 #include <sys/pathname.h>
45 #include <sys/vfs.h>
46 #include <sys/vfs_opreg.h>
47 #include <sys/vnode.h>
48 #include <sys/file.h>
49 #include <sys/uio.h>
50 #include <sys/conf.h>
51 #include <sys/policy.h>
52 
53 #include <vm/page.h>
54 
55 #include <sys/fs/snode.h>
56 #include <sys/fs/hsfs_spec.h>
57 #include <sys/fs/hsfs_isospec.h>
58 #include <sys/fs/hsfs_node.h>
59 #include <sys/fs/hsfs_impl.h>
60 #include <sys/fs/hsfs_susp.h>
61 #include <sys/fs/hsfs_rrip.h>
62 
63 #include <sys/statvfs.h>
64 #include <sys/mount.h>
65 #include <sys/mntent.h>
66 #include <sys/swap.h>
67 #include <sys/errno.h>
68 #include <sys/debug.h>
69 #include "fs/fs_subr.h"
70 #include <sys/cmn_err.h>
71 #include <sys/bootconf.h>
72 
73 #include <sys/sdt.h>
74 
75 /*
76  * These are needed for the CDROMREADOFFSET Code
77  */
78 #include <sys/cdio.h>
79 #include <sys/sunddi.h>
80 
81 #define	HSFS_CLKSET
82 
83 #include <sys/modctl.h>
84 
85 /*
86  * Options for mount.
87  */
88 #define	HOPT_GLOBAL	MNTOPT_GLOBAL
89 #define	HOPT_NOGLOBAL	MNTOPT_NOGLOBAL
90 #define	HOPT_MAPLCASE	"maplcase"
91 #define	HOPT_NOMAPLCASE	"nomaplcase"
92 #define	HOPT_NOTRAILDOT	"notraildot"
93 #define	HOPT_TRAILDOT	"traildot"
94 #define	HOPT_NRR	"nrr"
95 #define	HOPT_RR		"rr"
96 #define	HOPT_JOLIET	"joliet"
97 #define	HOPT_NOJOLIET	"nojoliet"
98 #define	HOPT_JOLIETLONG	"jolietlong"
99 #define	HOPT_VERS2	"vers2"
100 #define	HOPT_NOVERS2	"novers2"
101 #define	HOPT_RO		MNTOPT_RO
102 
103 static char *global_cancel[] = { HOPT_NOGLOBAL, NULL };
104 static char *noglobal_cancel[] = { HOPT_GLOBAL, NULL };
105 static char *mapl_cancel[] = { HOPT_NOMAPLCASE, NULL };
106 static char *nomapl_cancel[] = { HOPT_MAPLCASE, NULL };
107 static char *ro_cancel[] = { MNTOPT_RW, NULL };
108 static char *rr_cancel[] = { HOPT_NRR, NULL };
109 static char *nrr_cancel[] = { HOPT_RR, NULL };
110 static char *joliet_cancel[] = { HOPT_NOJOLIET, NULL };
111 static char *nojoliet_cancel[] = { HOPT_JOLIET, NULL };
112 static char *vers2_cancel[] = { HOPT_NOVERS2, NULL };
113 static char *novers2_cancel[] = { HOPT_VERS2, NULL };
114 static char *trail_cancel[] = { HOPT_NOTRAILDOT, NULL };
115 static char *notrail_cancel[] = { HOPT_TRAILDOT, NULL };
116 
117 static mntopt_t hsfs_options[] = {
118 	{ HOPT_GLOBAL, global_cancel, NULL, 0, NULL },
119 	{ HOPT_NOGLOBAL, noglobal_cancel, NULL, MO_DEFAULT, NULL },
120 	{ HOPT_MAPLCASE, mapl_cancel, NULL, MO_DEFAULT, NULL },
121 	{ HOPT_NOMAPLCASE, nomapl_cancel, NULL, 0, NULL },
122 	{ HOPT_RO, ro_cancel, NULL, MO_DEFAULT, NULL },
123 	{ HOPT_RR, rr_cancel, NULL, MO_DEFAULT, NULL },
124 	{ HOPT_NRR, nrr_cancel, NULL, 0, NULL },
125 	{ HOPT_JOLIET, joliet_cancel, NULL, 0, NULL },
126 	{ HOPT_NOJOLIET, nojoliet_cancel, NULL, 0, NULL },
127 	{ HOPT_JOLIETLONG, NULL, NULL, 0, NULL },
128 	{ HOPT_VERS2, vers2_cancel, NULL, 0, NULL },
129 	{ HOPT_NOVERS2, novers2_cancel, NULL, 0, NULL },
130 	{ HOPT_TRAILDOT, trail_cancel, NULL, MO_DEFAULT, NULL },
131 	{ HOPT_NOTRAILDOT, notrail_cancel, NULL, 0, NULL },
132 	{ "sector", NULL, "0", MO_HASVALUE, NULL},
133 };
134 
135 static mntopts_t hsfs_proto_opttbl = {
136 	sizeof (hsfs_options) / sizeof (mntopt_t),
137 	hsfs_options
138 };
139 
140 static int hsfsfstype;
141 static int hsfsinit(int, char *);
142 
143 static vfsdef_t vfw = {
144 	VFSDEF_VERSION,
145 	"hsfs",
146 	hsfsinit,
147 	VSW_HASPROTO|VSW_STATS,	/* We don't suppport remounting */
148 	&hsfs_proto_opttbl
149 };
150 
151 static struct modlfs modlfs = {
152 	&mod_fsops, "filesystem for HSFS", &vfw
153 };
154 
155 static struct modlinkage modlinkage = {
156 	MODREV_1, (void *)&modlfs, NULL
157 };
158 
159 char _depends_on[] = "fs/specfs";
160 
161 int
162 _init(void)
163 {
164 	return (mod_install(&modlinkage));
165 }
166 
167 int
168 _fini(void)
169 {
170 	int	error;
171 
172 	error = mod_remove(&modlinkage);
173 
174 	DTRACE_PROBE1(mod_remove, int, error);
175 
176 	if (error)
177 		return (error);
178 
179 	mutex_destroy(&hs_mounttab_lock);
180 
181 	/*
182 	 * Tear down the operations vectors
183 	 */
184 	(void) vfs_freevfsops_by_type(hsfsfstype);
185 	vn_freevnodeops(hsfs_vnodeops);
186 
187 	hs_fini_hsnode_cache();
188 	return (0);
189 }
190 
191 int
192 _info(struct modinfo *modinfop)
193 {
194 	return (mod_info(&modlinkage, modinfop));
195 }
196 
197 #define	BDEVFLAG(dev)	((devopsp[getmajor(dev)])->devo_cb_ops->cb_flag)
198 
199 kmutex_t hs_mounttab_lock;
200 struct hsfs *hs_mounttab = NULL;
201 
202 /* default mode, uid, gid */
203 mode_t hsfs_default_mode = 0555;
204 uid_t hsfs_default_uid = 0;
205 gid_t hsfs_default_gid = 3;
206 
207 static int hsfs_mount(struct vfs *vfsp, struct vnode *mvp,
208 	struct mounta *uap, struct cred *cr);
209 static int hsfs_unmount(struct vfs *vfsp, int, struct cred *cr);
210 static int hsfs_root(struct vfs *vfsp, struct vnode **vpp);
211 static int hsfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp);
212 static int hsfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
213 static int hsfs_mountroot(struct vfs *, enum whymountroot);
214 
215 static int hs_mountfs(struct vfs *vfsp, dev_t dev, char *path,
216 	mode_t mode, int flags, struct cred *cr, int isroot);
217 static int hs_getrootvp(struct vfs *vfsp, struct hsfs *fsp, size_t pathsize);
218 static int hs_findhsvol(struct hsfs *fsp, struct vnode *vp,
219 	struct hs_volume *hvp);
220 static int hs_parsehsvol(struct hsfs *fsp, uchar_t *volp,
221 	struct hs_volume *hvp);
222 static int hs_findisovol(struct hsfs *fsp, struct vnode *vp,
223 	struct hs_volume *hvp,
224 	struct hs_volume *svp,
225 	struct hs_volume *jvp);
226 static int hs_joliet_level(uchar_t *volp);
227 static int hs_parseisovol(struct hsfs *fsp, uchar_t *volp,
228 	struct hs_volume *hvp);
229 static void hs_copylabel(struct hs_volume *, unsigned char *, int);
230 static int hs_getmdev(struct vfs *, char *fspec, int flags, dev_t *pdev,
231 	mode_t *mode, cred_t *cr);
232 static int hs_findvoldesc(dev_t rdev, int desc_sec);
233 
234 static int
235 hsfsinit(int fstype, char *name)
236 {
237 	static const fs_operation_def_t hsfs_vfsops_template[] = {
238 		VFSNAME_MOUNT,		{ .vfs_mount = hsfs_mount },
239 		VFSNAME_UNMOUNT,	{ .vfs_unmount = hsfs_unmount },
240 		VFSNAME_ROOT,		{ .vfs_root = hsfs_root },
241 		VFSNAME_STATVFS,	{ .vfs_statvfs = hsfs_statvfs },
242 		VFSNAME_VGET,		{ .vfs_vget = hsfs_vget },
243 		VFSNAME_MOUNTROOT,	{ .vfs_mountroot = hsfs_mountroot },
244 		NULL,			NULL
245 	};
246 	int error;
247 
248 	error = vfs_setfsops(fstype, hsfs_vfsops_template, NULL);
249 	if (error != 0) {
250 		cmn_err(CE_WARN, "hsfsinit: bad vfs ops template");
251 		return (error);
252 	}
253 
254 	error = vn_make_ops(name, hsfs_vnodeops_template, &hsfs_vnodeops);
255 	if (error != 0) {
256 		(void) vfs_freevfsops_by_type(fstype);
257 		cmn_err(CE_WARN, "hsfsinit: bad vnode ops template");
258 		return (error);
259 	}
260 
261 	hsfsfstype = fstype;
262 	mutex_init(&hs_mounttab_lock, NULL, MUTEX_DEFAULT, NULL);
263 	hs_init_hsnode_cache();
264 	return (0);
265 }
266 
267 /*ARGSUSED*/
268 static int
269 hsfs_mount(struct vfs *vfsp, struct vnode *mvp,
270     struct mounta *uap, struct cred *cr)
271 {
272 	int		vnode_busy;
273 	dev_t		dev;
274 	struct pathname dpn;
275 	int		error;
276 	mode_t		mode;
277 	int		flags;	/* this will hold the mount specific data */
278 
279 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
280 		return (error);
281 
282 	if (mvp->v_type != VDIR)
283 		return (ENOTDIR);
284 
285 	/* mount option must be read only, else mount will be rejected */
286 	if (!(uap->flags & MS_RDONLY))
287 		return (EROFS);
288 
289 	/*
290 	 * We already told the framework that we don't support remounting.
291 	 */
292 	ASSERT(!(uap->flags & MS_REMOUNT));
293 
294 	mutex_enter(&mvp->v_lock);
295 	vnode_busy = (mvp->v_count != 1) || (mvp->v_flag & VROOT);
296 	mutex_exit(&mvp->v_lock);
297 
298 	if ((uap->flags & MS_OVERLAY) == 0 && vnode_busy) {
299 		return (EBUSY);
300 	}
301 
302 	/*
303 	 * Check for the options that actually affect things
304 	 * at our level.
305 	 */
306 	flags = 0;
307 	if (vfs_optionisset(vfsp, HOPT_NOMAPLCASE, NULL))
308 		flags |= HSFSMNT_NOMAPLCASE;
309 	if (vfs_optionisset(vfsp, HOPT_NOTRAILDOT, NULL))
310 		flags |= HSFSMNT_NOTRAILDOT;
311 	if (vfs_optionisset(vfsp, HOPT_NRR, NULL))
312 		flags |= HSFSMNT_NORRIP;
313 	if (vfs_optionisset(vfsp, HOPT_NOJOLIET, NULL))
314 		flags |= HSFSMNT_NOJOLIET;
315 	if (vfs_optionisset(vfsp, HOPT_JOLIETLONG, NULL))
316 		flags |= HSFSMNT_JOLIETLONG;
317 	if (vfs_optionisset(vfsp, HOPT_NOVERS2, NULL))
318 		flags |= HSFSMNT_NOVERS2;
319 
320 	error = pn_get(uap->dir, (uap->flags & MS_SYSSPACE) ?
321 	    UIO_SYSSPACE : UIO_USERSPACE, &dpn);
322 	if (error)
323 		return (error);
324 
325 	error = hs_getmdev(vfsp, uap->spec, uap->flags, &dev, &mode, cr);
326 	if (error != 0) {
327 		pn_free(&dpn);
328 		return (error);
329 	}
330 
331 	/*
332 	 * If the device is a tape, return error
333 	 */
334 	if ((BDEVFLAG(dev) & D_TAPE) == D_TAPE)  {
335 		pn_free(&dpn);
336 		return (ENOTBLK);
337 	}
338 
339 	/*
340 	 * Mount the filesystem.
341 	 */
342 	error = hs_mountfs(vfsp, dev, dpn.pn_path, mode, flags, cr, 0);
343 	pn_free(&dpn);
344 	return (error);
345 }
346 
347 /*ARGSUSED*/
348 static int
349 hsfs_unmount(
350 	struct vfs *vfsp,
351 	int flag,
352 	struct cred *cr)
353 {
354 	struct hsfs **tspp;
355 	struct hsfs *fsp;
356 
357 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
358 		return (EPERM);
359 
360 	/*
361 	 * forced unmount is not supported by this file system
362 	 * and thus, ENOTSUP is being returned.
363 	 */
364 	if (flag & MS_FORCE)
365 		return (ENOTSUP);
366 
367 	fsp = VFS_TO_HSFS(vfsp);
368 
369 	if (fsp->hsfs_rootvp->v_count != 1)
370 		return (EBUSY);
371 
372 	/* destroy all old pages and hsnodes for this vfs */
373 	if (hs_synchash(vfsp))
374 		return (EBUSY);
375 
376 	mutex_enter(&hs_mounttab_lock);
377 	for (tspp = &hs_mounttab; *tspp != NULL; tspp = &(*tspp)->hsfs_next) {
378 		if (*tspp == fsp)
379 			break;
380 	}
381 	if (*tspp == NULL) {
382 		mutex_exit(&hs_mounttab_lock);
383 		panic("hsfs_unmount: vfs not mounted?");
384 		/*NOTREACHED*/
385 	}
386 
387 	*tspp = fsp->hsfs_next;
388 
389 	mutex_exit(&hs_mounttab_lock);
390 
391 	(void) VOP_CLOSE(fsp->hsfs_devvp, FREAD, 1, (offset_t)0, cr);
392 	VN_RELE(fsp->hsfs_devvp);
393 	/* free path table space */
394 	if (fsp->hsfs_ptbl != NULL)
395 		kmem_free(fsp->hsfs_ptbl, (size_t)fsp->hsfs_vol.ptbl_len);
396 	/* free path table index table */
397 	if (fsp->hsfs_ptbl_idx != NULL)
398 		kmem_free(fsp->hsfs_ptbl_idx, (size_t)
399 		    (fsp->hsfs_ptbl_idx_size * sizeof (struct ptable_idx)));
400 
401 	/* free "mounted on" pathame */
402 	if (fsp->hsfs_fsmnt != NULL)
403 		kmem_free(fsp->hsfs_fsmnt, strlen(fsp->hsfs_fsmnt) + 1);
404 
405 	mutex_destroy(&fsp->hsfs_free_lock);
406 	rw_destroy(&fsp->hsfs_hash_lock);
407 
408 	kmem_free(fsp, sizeof (*fsp));
409 	return (0);
410 }
411 
412 /*ARGSUSED*/
413 static int
414 hsfs_root(struct vfs *vfsp, struct vnode **vpp)
415 {
416 	*vpp = (VFS_TO_HSFS(vfsp))->hsfs_rootvp;
417 	VN_HOLD(*vpp);
418 	return (0);
419 }
420 
421 /*ARGSUSED*/
422 static int
423 hsfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
424 {
425 	struct hsfs *fsp;
426 	dev32_t d32;
427 
428 	fsp = VFS_TO_HSFS(vfsp);
429 	if (fsp->hsfs_magic != HSFS_MAGIC)
430 		return (EINVAL);
431 	bzero(sbp, sizeof (*sbp));
432 	sbp->f_bsize = vfsp->vfs_bsize;
433 	sbp->f_frsize = sbp->f_bsize; /* no fragment, same as block size */
434 	sbp->f_blocks = (fsblkcnt64_t)fsp->hsfs_vol.vol_size;
435 
436 	sbp->f_bfree = (fsblkcnt64_t)0;
437 	sbp->f_bavail = (fsblkcnt64_t)0;
438 	sbp->f_files = (fsfilcnt64_t)-1;
439 	sbp->f_ffree = (fsfilcnt64_t)0;
440 	sbp->f_favail = (fsfilcnt64_t)0;
441 	(void) cmpldev(&d32, vfsp->vfs_dev);
442 	sbp->f_fsid = d32;
443 	(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
444 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
445 	sbp->f_namemax = fsp->hsfs_namemax;
446 	(void) strcpy(sbp->f_fstr, fsp->hsfs_vol.vol_id);
447 
448 	return (0);
449 }
450 
451 /*
452  * Previously nodeid was declared as uint32_t. This has been changed
453  * to conform better with the ISO9660 standard. The standard states that
454  * a LBN can be a 32 bit number, as the MAKE_NODEID macro shifts this
455  * LBN 11 places left (LBN_TO_BYTE) and then shifts the result 5 right
456  * (divide by 32) we are left with the potential of an overflow if
457  * confined to a 32 bit value.
458  */
459 
460 static int
461 hsfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
462 {
463 	struct hsfid *fid;
464 	struct hsfs *fsp;
465 	ino64_t nodeid;
466 	int error;
467 
468 	fsp = (struct hsfs *)VFS_TO_HSFS(vfsp);
469 	fid = (struct hsfid *)fidp;
470 
471 	/*
472 	 * Look for vnode on hashlist.
473 	 * If found, it's now active and the refcnt was incremented.
474 	 */
475 
476 	rw_enter(&fsp->hsfs_hash_lock, RW_READER);
477 
478 	nodeid = fid->hf_ino;
479 
480 	if ((*vpp = hs_findhash(nodeid, fid->hf_dir_lbn,
481 	    (uint_t)fid->hf_dir_off, vfsp)) == NULL) {
482 		/*
483 		 * Not in cache, so we need to remake it.
484 		 * hs_remakenode() will read the directory entry
485 		 * and then check again to see if anyone else has
486 		 * put it in the cache.
487 		 */
488 		rw_exit(&fsp->hsfs_hash_lock);
489 		error = hs_remakenode(fid->hf_dir_lbn, (uint_t)fid->hf_dir_off,
490 		    vfsp, vpp);
491 		return (error);
492 	}
493 	rw_exit(&fsp->hsfs_hash_lock);
494 	return (0);
495 }
496 
497 
498 #define	CHECKSUM_SIZE				(64 * 1024)
499 
500 /*
501  * Compute a CD-ROM fsid by checksumming the first 64K of data on the CD
502  * We use the 'fsp' argument to determine the location of the root
503  * directory entry, and we start reading from there.
504  */
505 static int
506 compute_cdrom_id(struct hsfs *fsp, vnode_t *devvp)
507 {
508 	uint_t		secno;
509 	struct hs_volume *hsvp = &fsp->hsfs_vol;
510 	struct buf	*bp;
511 	int		error;
512 	int		fsid;
513 
514 	secno = hsvp->root_dir.ext_lbn >> hsvp->lbn_secshift;
515 	bp = bread(devvp->v_rdev, secno * 4, CHECKSUM_SIZE);
516 	error = geterror(bp);
517 
518 	/*
519 	 * An error on read or a partial read means we asked
520 	 * for a nonexistant/corrupted piece of the device
521 	 * (including past-the-end of the media). Don't
522 	 * try to use the checksumming method then.
523 	 */
524 	if (!error && bp->b_bcount == CHECKSUM_SIZE) {
525 		int *ibuf = (int *)bp->b_un.b_addr;
526 		int i;
527 
528 		fsid = 0;
529 
530 		for (i = 0; i < CHECKSUM_SIZE / sizeof (int); i++)
531 			fsid ^= ibuf[ i ];
532 	} else {
533 		/*
534 		 * Fallback - use creation date
535 		 */
536 		fsid = hsvp->cre_date.tv_sec;
537 	}
538 
539 	brelse(bp);
540 
541 	return (fsid);
542 }
543 
544 
545 /*ARGSUSED*/
546 static int
547 hs_mountfs(
548 	struct vfs	*vfsp,
549 	dev_t		dev,
550 	char		*path,
551 	mode_t		mode,
552 	int		mount_flags,
553 	struct cred	*cr,
554 	int		isroot)
555 {
556 	struct vnode	*devvp;
557 	struct hsfs	*tsp;
558 	struct hsfs	*fsp = NULL;
559 	struct vattr	vap;
560 	struct hsnode	*hp;
561 	int		error;
562 	struct timeval	tv;
563 	int		fsid;
564 	int		use_rrip;
565 	int		use_vers2;
566 	int		use_joliet;
567 	int		has_rrip = 0;
568 	int		has_vers2 = 0;
569 	int		has_joliet = 0;
570 	int		force_rrip_off;
571 	int		force_vers2_off;
572 	int		force_joliet_off;
573 	size_t		pathbufsz = strlen(path) + 1;
574 	int		redo_rootvp;
575 
576 	struct hs_volume *svp;		/* Supplemental VD for ISO-9660:1999 */
577 	struct hs_volume *jvp;		/* Joliet VD */
578 
579 	/*
580 	 * The rules for which extension will be used are:
581 	 * 1. No specific mount options given:
582 	 *	- use rrip if available
583 	 *	- use ISO9660:1999 if available
584 	 *	- use joliet if available.
585 	 * 2. rrip/ISO9660:1999/joliet explicitly disabled via mount option:
586 	 *	- use next "lower" extension
587 	 * 3. joliet/ISO9660:1999/rrip explicitly requested via mount option:
588 	 *	- disable rrip support even if available
589 	 *	- disable IOS9660:1999 support even if available
590 	 *
591 	 * We need to adjust these flags as we discover the extensions
592 	 * present. See below. These are just the starting values.
593 	 */
594 	use_rrip = (mount_flags & HSFSMNT_NORRIP) == 0;
595 	use_vers2 = (mount_flags & HSFSMNT_NOVERS2) == 0;
596 	use_joliet = (mount_flags & HSFSMNT_NOJOLIET) == 0;
597 
598 	/*
599 	 * Open the device
600 	 */
601 	devvp = makespecvp(dev, VBLK);
602 	ASSERT(devvp != 0);
603 
604 	/*
605 	 * Open the target device (file) for read only.
606 	 */
607 	if (error = VOP_OPEN(&devvp, FREAD, cr)) {
608 		VN_RELE(devvp);
609 		return (error);
610 	}
611 
612 	/*
613 	 * Refuse to go any further if this
614 	 * device is being used for swapping
615 	 */
616 	if (IS_SWAPVP(common_specvp(devvp))) {
617 		error = EBUSY;
618 		goto cleanup;
619 	}
620 
621 	vap.va_mask = AT_SIZE;
622 	if ((error = VOP_GETATTR(devvp, &vap, ATTR_COMM, cr)) != 0) {
623 		cmn_err(CE_NOTE, "Cannot get attributes of the CD-ROM driver");
624 		goto cleanup;
625 	}
626 
627 	/*
628 	 * Make sure we have a nonzero size partition.
629 	 * The current version of the SD driver will *not* fail the open
630 	 * of such a partition so we have to check for it here.
631 	 */
632 	if (vap.va_size == 0) {
633 		error = ENXIO;
634 		goto cleanup;
635 	}
636 
637 	/*
638 	 * Init a new hsfs structure.
639 	 */
640 	fsp = kmem_zalloc(sizeof (*fsp), KM_SLEEP);
641 	svp = kmem_zalloc(sizeof (*svp), KM_SLEEP);
642 	jvp = kmem_zalloc(sizeof (*jvp), KM_SLEEP);
643 
644 	/* hardwire perms, uid, gid */
645 	fsp->hsfs_vol.vol_uid = hsfs_default_uid;
646 	fsp->hsfs_vol.vol_gid =  hsfs_default_gid;
647 	fsp->hsfs_vol.vol_prot = hsfs_default_mode;
648 	svp->vol_uid = hsfs_default_uid;
649 	svp->vol_gid =  hsfs_default_gid;
650 	svp->vol_prot = hsfs_default_mode;
651 	jvp->vol_uid = hsfs_default_uid;
652 	jvp->vol_gid =  hsfs_default_gid;
653 	jvp->vol_prot = hsfs_default_mode;
654 
655 	/*
656 	 * Look for a Standard File Structure Volume Descriptor,
657 	 * of which there must be at least one.
658 	 * If found, check for volume size consistency.
659 	 *
660 	 * If svp->lbn_size is != 0, we did find a ISO-9660:1999 SVD
661 	 * If jvp->lbn_size is != 0, we did find a Joliet SVD.
662 	 */
663 	fsp->hsfs_namemax = ISO_FILE_NAMELEN;
664 	fsp->hsfs_namelen = ISO_FILE_NAMELEN;
665 	error = hs_findisovol(fsp, devvp, &fsp->hsfs_vol, svp, jvp);
666 	if (error == EINVAL) /* no iso 9660 - try high sierra ... */
667 		error = hs_findhsvol(fsp, devvp, &fsp->hsfs_vol);
668 
669 	if (error)
670 		goto cleanup;
671 
672 	DTRACE_PROBE4(findvol,
673 	    struct hsfs *, fsp,
674 	    struct hs_volume *, &fsp->hsfs_vol,
675 	    struct hs_volume *, svp,
676 	    struct hs_volume *, jvp);
677 
678 	/*
679 	 * Generate a file system ID from the CD-ROM,
680 	 * and check it for uniqueness.
681 	 *
682 	 * What we are aiming for is some chance of integrity
683 	 * across disk change.  That is, if a client has an fhandle,
684 	 * it will be valid as long as the same disk is mounted.
685 	 */
686 	fsid = compute_cdrom_id(fsp, devvp);
687 
688 	mutex_enter(&hs_mounttab_lock);
689 
690 	if (fsid == 0 || fsid == -1) {
691 		uniqtime(&tv);
692 		fsid = tv.tv_sec;
693 	} else	/* make sure that the fsid is unique */
694 		for (tsp = hs_mounttab; tsp != NULL; tsp = tsp->hsfs_next) {
695 			if (fsid == tsp->hsfs_vfs->vfs_fsid.val[0]) {
696 				uniqtime(&tv);
697 				fsid = tv.tv_sec;
698 				break;
699 			}
700 		}
701 
702 	fsp->hsfs_next = hs_mounttab;
703 	hs_mounttab = fsp;
704 
705 	fsp->hsfs_devvp = devvp;
706 	fsp->hsfs_vfs = vfsp;
707 	fsp->hsfs_fsmnt = kmem_alloc(pathbufsz, KM_SLEEP);
708 	(void) strlcpy(fsp->hsfs_fsmnt, path, pathbufsz);
709 
710 	mutex_init(&fsp->hsfs_free_lock, NULL, MUTEX_DEFAULT, NULL);
711 	rw_init(&fsp->hsfs_hash_lock, NULL, RW_DEFAULT, NULL);
712 
713 	vfsp->vfs_data = (caddr_t)fsp;
714 	vfsp->vfs_dev = dev;
715 	vfsp->vfs_fstype = hsfsfstype;
716 	vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size; /* %% */
717 	vfsp->vfs_fsid.val[0] = fsid;
718 	vfsp->vfs_fsid.val[1] =  hsfsfstype;
719 
720 	if (!hs_getrootvp(vfsp, fsp, pathbufsz)) {
721 		DTRACE_PROBE1(rootvp__failed, struct hsfs *, fsp);
722 		error = EINVAL;
723 		goto cleanup;
724 	}
725 	DTRACE_PROBE1(rootvp, struct hsfs *, fsp);
726 
727 	/*
728 	 * Attempt to discover a RR extension.
729 	 */
730 	if (use_rrip) {
731 		hp = VTOH(fsp->hsfs_rootvp);
732 		hs_check_root_dirent(fsp->hsfs_rootvp, &(hp->hs_dirent));
733 	}
734 
735 	has_rrip = IS_RRIP_IMPLEMENTED(fsp);
736 	has_vers2 = (svp->lbn_size != 0);
737 	has_joliet = (jvp->lbn_size != 0);
738 
739 	DTRACE_PROBE4(voltype__suggested, struct hsfs *, fsp,
740 	    int, use_rrip, int, use_vers2, int, use_joliet);
741 
742 	DTRACE_PROBE4(voltype__actual, struct hsfs *, fsp,
743 	    int, has_rrip, int, has_vers2, int, has_joliet);
744 
745 	DTRACE_PROBE4(findvol,
746 	    struct hsfs *, fsp,
747 	    struct hs_volume *, &fsp->hsfs_vol,
748 	    struct hs_volume *, svp,
749 	    struct hs_volume *, jvp);
750 
751 	force_rrip_off = !use_rrip ||
752 	    (vfs_optionisset(vfsp, HOPT_JOLIET, NULL) && has_joliet) ||
753 	    (vfs_optionisset(vfsp, HOPT_VERS2, NULL) && has_vers2);
754 
755 	force_vers2_off = !use_vers2 ||
756 	    (vfs_optionisset(vfsp, HOPT_JOLIET, NULL) && has_joliet);
757 
758 	force_joliet_off = !use_joliet;
759 
760 	DTRACE_PROBE4(voltype__force_off, struct hsfs *, fsp,
761 	    int, force_rrip_off, int, force_vers2_off, int, force_joliet_off);
762 
763 	/*
764 	 * At the moment, we have references of all three possible
765 	 * extensions (RR, ISO9660:1999/v2 and Joliet) if present.
766 	 *
767 	 * The "active" volume descriptor is RRIP (or ISO9660:1988).
768 	 * We now switch to the user-requested one.
769 	 */
770 	redo_rootvp = 0;
771 
772 	if (force_rrip_off || !has_rrip) {
773 		if (has_vers2 && !force_vers2_off) {
774 			VN_RELE(fsp->hsfs_rootvp);
775 			bcopy(svp, &fsp->hsfs_vol, sizeof (struct hs_volume));
776 			fsp->hsfs_vol_type = HS_VOL_TYPE_ISO_V2;
777 			vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size;
778 			redo_rootvp = 1;
779 			has_joliet = 0;
780 		} else if (has_joliet && !force_joliet_off) {
781 			VN_RELE(fsp->hsfs_rootvp);
782 			bcopy(jvp, &fsp->hsfs_vol, sizeof (struct hs_volume));
783 			fsp->hsfs_vol_type = HS_VOL_TYPE_JOLIET;
784 			vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size;
785 			redo_rootvp = 1;
786 			has_vers2 = 0;
787 		}
788 	}
789 
790 	if (redo_rootvp) {
791 		/*
792 		 * Make sure not to use Rock Ridge.
793 		 */
794 		UNSET_IMPL_BIT(fsp, RRIP_BIT);
795 		UNSET_SUSP_BIT(fsp);
796 		has_rrip = 0;
797 
798 		if (!hs_getrootvp(vfsp, fsp, pathbufsz)) {
799 			DTRACE_PROBE1(rootvp__failed, struct hsfs *, fsp);
800 			error = EINVAL;
801 			goto cleanup;
802 		}
803 		DTRACE_PROBE1(rootvp, struct hsfs *, fsp);
804 	}
805 	if (IS_RRIP_IMPLEMENTED(fsp)) {
806 		has_vers2 = 0;
807 		has_joliet = 0;
808 	}
809 	if (force_vers2_off)
810 		has_vers2 = 0;
811 	if (force_joliet_off)
812 		has_joliet = 0;
813 	DTRACE_PROBE4(voltype__taken, struct hsfs *, fsp,
814 	    int, has_rrip, int, has_vers2, int, has_joliet);
815 
816 	/*
817 	 * mark root node as VROOT
818 	 */
819 	fsp->hsfs_rootvp->v_flag |= VROOT;
820 
821 	/* Here we take care of some special case stuff for mountroot */
822 	if (isroot) {
823 		fsp->hsfs_rootvp->v_rdev = devvp->v_rdev;
824 		rootvp = fsp->hsfs_rootvp;
825 	}
826 
827 	if (IS_RRIP_IMPLEMENTED(fsp)) {
828 		/*
829 		 * if RRIP, don't copy NOMAPLCASE or NOTRAILDOT to hsfs_flags
830 		 */
831 		mount_flags &= ~(HSFSMNT_NOMAPLCASE | HSFSMNT_NOTRAILDOT);
832 
833 		fsp->hsfs_namemax = RRIP_FILE_NAMELEN;
834 		fsp->hsfs_namelen = RRIP_FILE_NAMELEN;
835 
836 		ASSERT(vfs_optionisset(vfsp, HOPT_RR, NULL));
837 		vfs_clearmntopt(vfsp, HOPT_VERS2);
838 		vfs_clearmntopt(vfsp, HOPT_JOLIET);
839 
840 	} else switch (fsp->hsfs_vol_type) {
841 
842 	case HS_VOL_TYPE_HS:
843 	case HS_VOL_TYPE_ISO:
844 	default:
845 		/*
846 		 * if iso v1, don't allow trailing spaces in iso file names
847 		 */
848 		mount_flags |= HSFSMNT_NOTRAILSPACE;
849 		fsp->hsfs_namemax = ISO_NAMELEN_V2_MAX;
850 		fsp->hsfs_namelen = ISO_FILE_NAMELEN;
851 		vfs_clearmntopt(vfsp, HOPT_RR);
852 		vfs_clearmntopt(vfsp, HOPT_VERS2);
853 		vfs_clearmntopt(vfsp, HOPT_JOLIET);
854 		break;
855 
856 	case HS_VOL_TYPE_ISO_V2:
857 		/*
858 		 * if iso v2, don't copy NOTRAILDOT to hsfs_flags
859 		 */
860 		mount_flags &= ~HSFSMNT_NOTRAILDOT;
861 		mount_flags |= HSFSMNT_NOMAPLCASE | HSFSMNT_NOVERSION;
862 		fsp->hsfs_namemax = ISO_NAMELEN_V2_MAX;
863 		fsp->hsfs_namelen = ISO_NAMELEN_V2;
864 		vfs_setmntopt(vfsp, HOPT_VERS2, NULL, 0);
865 		vfs_clearmntopt(vfsp, HOPT_RR);
866 		vfs_clearmntopt(vfsp, HOPT_JOLIET);
867 		break;
868 
869 	case HS_VOL_TYPE_JOLIET:
870 		/*
871 		 * if Joliet, don't copy NOMAPLCASE or NOTRAILDOT to hsfs_flags
872 		 */
873 		mount_flags &= ~(HSFSMNT_NOMAPLCASE | HSFSMNT_NOTRAILDOT);
874 		mount_flags |= HSFSMNT_NOMAPLCASE;
875 		if (mount_flags & HSFSMNT_JOLIETLONG)
876 			fsp->hsfs_namemax = JOLIET_NAMELEN_MAX*3; /* UTF-8 */
877 		else
878 			fsp->hsfs_namemax = MAXNAMELEN-1;
879 		fsp->hsfs_namelen = JOLIET_NAMELEN*2;
880 		vfs_setmntopt(vfsp, HOPT_JOLIET, NULL, 0);
881 		vfs_clearmntopt(vfsp, HOPT_RR);
882 		vfs_clearmntopt(vfsp, HOPT_VERS2);
883 		break;
884 	}
885 
886 	/*
887 	 * Add the HSFSMNT_INODE pseudo mount flag to the current mount flags.
888 	 */
889 	fsp->hsfs_flags = mount_flags | (fsp->hsfs_flags & HSFSMNT_INODE);
890 
891 	DTRACE_PROBE1(mount__done, struct hsfs *, fsp);
892 
893 	/*
894 	 * set the magic word
895 	 */
896 	fsp->hsfs_magic = HSFS_MAGIC;
897 	mutex_exit(&hs_mounttab_lock);
898 
899 	kmem_free(svp, sizeof (*svp));
900 	kmem_free(jvp, sizeof (*jvp));
901 
902 	return (0);
903 
904 cleanup:
905 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, cr);
906 	VN_RELE(devvp);
907 	if (fsp)
908 		kmem_free(fsp, sizeof (*fsp));
909 	if (svp)
910 		kmem_free(svp, sizeof (*svp));
911 	if (jvp)
912 		kmem_free(jvp, sizeof (*jvp));
913 	return (error);
914 }
915 
916 /*
917  * Get the rootvp associated with fsp->hsfs_vol
918  */
919 static int
920 hs_getrootvp(
921 	struct vfs	*vfsp,
922 	struct hsfs	*fsp,
923 	size_t		pathsize)
924 {
925 	struct hsnode	*hp;
926 
927 	ASSERT(pathsize == strlen(fsp->hsfs_fsmnt) + 1);
928 
929 	/*
930 	 * If the root directory does not appear to be
931 	 * valid, use what it points to as "." instead.
932 	 * Some Defense Mapping Agency disks are non-conformant
933 	 * in this way.
934 	 */
935 	if (!hsfs_valid_dir(&fsp->hsfs_vol.root_dir)) {
936 		hs_log_bogus_disk_warning(fsp, HSFS_ERR_BAD_ROOT_DIR, 0);
937 		if (hs_remakenode(fsp->hsfs_vol.root_dir.ext_lbn,
938 		    (uint_t)0, vfsp, &fsp->hsfs_rootvp)) {
939 			hs_mounttab = hs_mounttab->hsfs_next;
940 			mutex_destroy(&fsp->hsfs_free_lock);
941 			rw_destroy(&fsp->hsfs_hash_lock);
942 			kmem_free(fsp->hsfs_fsmnt, pathsize);
943 			mutex_exit(&hs_mounttab_lock);
944 			return (0);
945 		}
946 	} else {
947 		fsp->hsfs_rootvp = hs_makenode(&fsp->hsfs_vol.root_dir,
948 		    fsp->hsfs_vol.root_dir.ext_lbn, 0, vfsp);
949 	}
950 
951 	/* XXX - ignore the path table for now */
952 	fsp->hsfs_ptbl = NULL;
953 	hp = VTOH(fsp->hsfs_rootvp);
954 	hp->hs_ptbl_idx = NULL;
955 
956 	return (1);
957 }
958 
959 /*
960  * hs_findhsvol()
961  *
962  * Locate the Standard File Structure Volume Descriptor and
963  * parse it into an hs_volume structure.
964  *
965  * XXX - May someday want to look for Coded Character Set FSVD, too.
966  */
967 static int
968 hs_findhsvol(struct hsfs *fsp, struct vnode *vp, struct hs_volume *hvp)
969 {
970 	struct buf *secbp;
971 	int i;
972 	int n;
973 	uchar_t *volp;
974 	int error;
975 	uint_t secno;
976 
977 	secno = hs_findvoldesc(vp->v_rdev, HS_VOLDESC_SEC);
978 	secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
979 	error = geterror(secbp);
980 
981 	if (error != 0) {
982 		cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)", error);
983 		brelse(secbp);
984 		return (error);
985 	}
986 
987 	volp = (uchar_t *)secbp->b_un.b_addr;
988 
989 	/*
990 	 * To avoid that we read the whole medium in case that someone prepares
991 	 * a malicious "fs image", we read at most 32 blocks.
992 	 */
993 	for (n = 0; n < 32 &&
994 	    HSV_DESC_TYPE(volp) != VD_EOV; n++) {
995 		for (i = 0; i < HSV_ID_STRLEN; i++)
996 			if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
997 				goto cantfind;
998 		if (HSV_STD_VER(volp) != HSV_ID_VER)
999 			goto cantfind;
1000 		switch (HSV_DESC_TYPE(volp)) {
1001 		case VD_SFS:
1002 			/* Standard File Structure */
1003 			fsp->hsfs_vol_type = HS_VOL_TYPE_HS;
1004 			error = hs_parsehsvol(fsp, volp, hvp);
1005 			brelse(secbp);
1006 			return (error);
1007 
1008 		case VD_CCFS:
1009 			/* Coded Character File Structure */
1010 		case VD_BOOT:
1011 		case VD_UNSPEC:
1012 		case VD_EOV:
1013 			break;
1014 		}
1015 		brelse(secbp);
1016 		++secno;
1017 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
1018 
1019 		error = geterror(secbp);
1020 
1021 		if (error != 0) {
1022 			cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)",
1023 			    error);
1024 			brelse(secbp);
1025 			return (error);
1026 		}
1027 
1028 		volp = (uchar_t *)secbp->b_un.b_addr;
1029 	}
1030 cantfind:
1031 	brelse(secbp);
1032 	return (EINVAL);
1033 }
1034 
1035 /*
1036  * hs_parsehsvol
1037  *
1038  * Parse the Standard File Structure Volume Descriptor into
1039  * an hs_volume structure.  We can't just bcopy it into the
1040  * structure because of byte-ordering problems.
1041  *
1042  */
1043 static int
1044 hs_parsehsvol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
1045 {
1046 	hvp->vol_size = HSV_VOL_SIZE(volp);
1047 	hvp->lbn_size = HSV_BLK_SIZE(volp);
1048 	if (hvp->lbn_size == 0) {
1049 		cmn_err(CE_NOTE, "hs_parsehsvol: logical block size in the "
1050 		    "SFSVD is zero");
1051 		return (EINVAL);
1052 	}
1053 	hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
1054 	hvp->lbn_secshift =
1055 	    ffs((long)howmany(HS_SECTOR_SIZE, (int)hvp->lbn_size)) - 1;
1056 	hvp->lbn_maxoffset = hvp->lbn_size - 1;
1057 	hs_parse_longdate(HSV_cre_date(volp), &hvp->cre_date);
1058 	hs_parse_longdate(HSV_mod_date(volp), &hvp->mod_date);
1059 	hvp->file_struct_ver = HSV_FILE_STRUCT_VER(volp);
1060 	hvp->ptbl_len = HSV_PTBL_SIZE(volp);
1061 	hvp->vol_set_size = (ushort_t)HSV_SET_SIZE(volp);
1062 	hvp->vol_set_seq = (ushort_t)HSV_SET_SEQ(volp);
1063 #if defined(_LITTLE_ENDIAN)
1064 	hvp->ptbl_lbn = HSV_PTBL_MAN_LS(volp);
1065 #else
1066 	hvp->ptbl_lbn = HSV_PTBL_MAN_MS(volp);
1067 #endif
1068 	hs_copylabel(hvp, HSV_VOL_ID(volp), 0);
1069 
1070 	/*
1071 	 * Make sure that lbn_size is a power of two and otherwise valid.
1072 	 */
1073 	if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
1074 		cmn_err(CE_NOTE,
1075 		    "hsfs: %d-byte logical block size not supported",
1076 		    hvp->lbn_size);
1077 		return (EINVAL);
1078 	}
1079 	return (hs_parsedir(fsp, HSV_ROOT_DIR(volp), &hvp->root_dir,
1080 	    (char *)NULL, (int *)NULL, HDE_ROOT_DIR_REC_SIZE));
1081 }
1082 
1083 /*
1084  * hs_findisovol()
1085  *
1086  * Locate the Primary Volume Descriptor
1087  * parse it into an hs_volume structure.
1088  *
1089  * XXX - Partition not yet done
1090  *
1091  * Except for fsp->hsfs_vol_type, no fsp member may be modified.
1092  * fsp->hsfs_vol is modified indirectly via the *hvp argument.
1093  */
1094 static int
1095 hs_findisovol(struct hsfs *fsp, struct vnode *vp,
1096     struct hs_volume *hvp,
1097     struct hs_volume *svp,
1098     struct hs_volume *jvp)
1099 {
1100 	struct buf *secbp;
1101 	int i;
1102 	int n;
1103 	uchar_t *volp;
1104 	int error;
1105 	uint_t secno;
1106 	int foundpvd = 0;
1107 	int foundsvd = 0;
1108 	int foundjvd = 0;
1109 	int pvd_sum = 0;
1110 
1111 	secno = hs_findvoldesc(vp->v_rdev, ISO_VOLDESC_SEC);
1112 	secbp = bread(vp->v_rdev, secno * 4, ISO_SECTOR_SIZE);
1113 	error = geterror(secbp);
1114 
1115 	if (error != 0) {
1116 		cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)", error);
1117 		brelse(secbp);
1118 		return (error);
1119 	}
1120 
1121 	volp = (uchar_t *)secbp->b_un.b_addr;
1122 
1123 	/*
1124 	 * To avoid that we read the whole medium in case that someone prepares
1125 	 * a malicious "fs image", we read at most 32 blocks.
1126 	 */
1127 	for (n = 0; n < 32 &&
1128 	    (enum iso_voldesc_type) ISO_DESC_TYPE(volp) != ISO_VD_EOV; n++) {
1129 		for (i = 0; i < ISO_ID_STRLEN; i++)
1130 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
1131 				goto cantfind;
1132 		switch (ISO_DESC_TYPE(volp)) {
1133 		case ISO_VD_PVD:
1134 			/* Standard File Structure */
1135 			if (ISO_STD_VER(volp) != ISO_ID_VER)
1136 				goto cantfind;
1137 			if (foundpvd != 1) {
1138 				fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
1139 				if (error = hs_parseisovol(fsp, volp, hvp)) {
1140 					brelse(secbp);
1141 					return (error);
1142 				}
1143 				foundpvd = 1;
1144 				for (i = 0; i < ISO_SECTOR_SIZE; i++)
1145 					pvd_sum += volp[i];
1146 			}
1147 			break;
1148 		case ISO_VD_SVD:
1149 			/* Supplementary Volume Descriptor */
1150 			if (ISO_STD_VER(volp) == ISO_ID_VER2 &&
1151 			    foundsvd != 1) {
1152 				fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
1153 				if (error = hs_parseisovol(fsp, volp, svp)) {
1154 					brelse(secbp);
1155 					return (error);
1156 				}
1157 				foundsvd = 1;
1158 			}
1159 			if (hs_joliet_level(volp) >= 1 && foundjvd != 1) {
1160 				fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
1161 				if (error = hs_parseisovol(fsp, volp, jvp)) {
1162 					brelse(secbp);
1163 					return (error);
1164 				}
1165 				foundjvd = 1;
1166 			}
1167 			break;
1168 		case ISO_VD_BOOT:
1169 			break;
1170 		case ISO_VD_VPD:
1171 			/* currently cannot handle partition */
1172 			break;
1173 		case VD_EOV:
1174 			break;
1175 		}
1176 		brelse(secbp);
1177 		++secno;
1178 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
1179 		error = geterror(secbp);
1180 
1181 		if (error != 0) {
1182 			cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)",
1183 			    error);
1184 			brelse(secbp);
1185 			return (error);
1186 		}
1187 
1188 		volp = (uchar_t *)secbp->b_un.b_addr;
1189 	}
1190 	for (n = 0; n < 16; n++) {
1191 		brelse(secbp);
1192 		++secno;
1193 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
1194 		error = geterror(secbp);
1195 
1196 		if (error != 0) {
1197 			cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)",
1198 			    error);
1199 			brelse(secbp);
1200 			return (error);
1201 		}
1202 
1203 		/*
1204 		 * Check for the signature from mkisofs that grants that
1205 		 * the current filesystem allows to use the extent lbn as
1206 		 * inode number even in pure ISO9660 mode.
1207 		 */
1208 		volp = (uchar_t *)secbp->b_un.b_addr;
1209 		if (strncmp((char *)volp, "MKI ", 4) == 0) {
1210 			int	sum;
1211 
1212 			sum  = volp[2045];
1213 			sum *= 256;
1214 			sum += volp[2046];
1215 			sum *= 256;
1216 			sum += volp[2047];
1217 			if (sum == pvd_sum)
1218 				fsp->hsfs_flags |= HSFSMNT_INODE;
1219 			break;
1220 		}
1221 	}
1222 	if (foundpvd) {
1223 		brelse(secbp);
1224 		return (0);
1225 	}
1226 cantfind:
1227 	brelse(secbp);
1228 	return (EINVAL);
1229 }
1230 
1231 /*
1232  * Return 0 if no Joliet is found
1233  * else return Joliet Level 1..3
1234  */
1235 static int
1236 hs_joliet_level(uchar_t *volp)
1237 {
1238 	if (ISO_std_ver(volp)[0] == ISO_ID_VER &&
1239 	    ISO_svd_esc(volp)[0] == '%' &&
1240 	    ISO_svd_esc(volp)[1] == '/') {
1241 
1242 		switch (ISO_svd_esc(volp)[2]) {
1243 
1244 		case '@':
1245 			return (1);
1246 		case 'C':
1247 			return (2);
1248 		case 'E':
1249 			return (3);
1250 		}
1251 	}
1252 	return (0);
1253 }
1254 
1255 /*
1256  * hs_parseisovol
1257  *
1258  * Parse the Primary Volume Descriptor into an hs_volume structure.
1259  *
1260  */
1261 static int
1262 hs_parseisovol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
1263 {
1264 	hvp->vol_size = ISO_VOL_SIZE(volp);
1265 	hvp->lbn_size = ISO_BLK_SIZE(volp);
1266 	if (hvp->lbn_size == 0) {
1267 		cmn_err(CE_NOTE, "hs_parseisovol: logical block size in the "
1268 		    "PVD is zero");
1269 		return (EINVAL);
1270 	}
1271 	hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
1272 	hvp->lbn_secshift =
1273 	    ffs((long)howmany(ISO_SECTOR_SIZE, (int)hvp->lbn_size)) - 1;
1274 	hvp->lbn_maxoffset = hvp->lbn_size - 1;
1275 	hs_parse_longdate(ISO_cre_date(volp), &hvp->cre_date);
1276 	hs_parse_longdate(ISO_mod_date(volp), &hvp->mod_date);
1277 	hvp->file_struct_ver = ISO_FILE_STRUCT_VER(volp);
1278 	hvp->ptbl_len = ISO_PTBL_SIZE(volp);
1279 	hvp->vol_set_size = (ushort_t)ISO_SET_SIZE(volp);
1280 	hvp->vol_set_seq = (ushort_t)ISO_SET_SEQ(volp);
1281 #if defined(_LITTLE_ENDIAN)
1282 	hvp->ptbl_lbn = ISO_PTBL_MAN_LS(volp);
1283 #else
1284 	hvp->ptbl_lbn = ISO_PTBL_MAN_MS(volp);
1285 #endif
1286 	hs_copylabel(hvp, ISO_VOL_ID(volp), hs_joliet_level(volp) >= 1);
1287 
1288 	/*
1289 	 * Make sure that lbn_size is a power of two and otherwise valid.
1290 	 */
1291 	if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
1292 		cmn_err(CE_NOTE,
1293 		    "hsfs: %d-byte logical block size not supported",
1294 		    hvp->lbn_size);
1295 		return (EINVAL);
1296 	}
1297 	return (hs_parsedir(fsp, ISO_ROOT_DIR(volp), &hvp->root_dir,
1298 	    (char *)NULL, (int *)NULL, IDE_ROOT_DIR_REC_SIZE));
1299 }
1300 
1301 /*
1302  * Common code for mount and umount.
1303  * Check that the user's argument is a reasonable
1304  * thing on which to mount, and return the device number if so.
1305  */
1306 static int
1307 hs_getmdev(struct vfs *vfsp, char *fspec, int flags, dev_t *pdev, mode_t *mode,
1308     cred_t *cr)
1309 {
1310 	int error;
1311 	struct vnode *vp;
1312 	struct vattr vap;
1313 	dev_t dev;
1314 
1315 	/*
1316 	 * Get the device to be mounted
1317 	 */
1318 	error = lookupname(fspec, (flags & MS_SYSSPACE) ?
1319 	    UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
1320 	if (error) {
1321 		if (error == ENOENT) {
1322 			return (ENODEV);	/* needs translation */
1323 		}
1324 		return (error);
1325 	}
1326 	if (vp->v_type != VBLK) {
1327 		VN_RELE(vp);
1328 		return (ENOTBLK);
1329 	}
1330 	/*
1331 	 * Can we read from the device?
1332 	 */
1333 	if ((error = VOP_ACCESS(vp, VREAD, 0, cr)) != 0 ||
1334 	    (error = secpolicy_spec_open(cr, vp, FREAD)) != 0) {
1335 		VN_RELE(vp);
1336 		return (error);
1337 	}
1338 
1339 	vap.va_mask = AT_MODE;		/* get protection mode */
1340 	(void) VOP_GETATTR(vp, &vap, 0, CRED());
1341 	*mode = vap.va_mode;
1342 
1343 	dev = *pdev = vp->v_rdev;
1344 	VN_RELE(vp);
1345 
1346 	/*
1347 	 * Ensure that this device isn't already mounted,
1348 	 * unless this is a REMOUNT request or we are told to suppress
1349 	 * mount checks.
1350 	 */
1351 	if ((flags & MS_NOCHECK) == 0) {
1352 		if (vfs_devmounting(dev, vfsp))
1353 			return (EBUSY);
1354 		if (vfs_devismounted(dev) && !(flags & MS_REMOUNT))
1355 			return (EBUSY);
1356 	}
1357 
1358 	if (getmajor(*pdev) >= devcnt)
1359 		return (ENXIO);
1360 	return (0);
1361 }
1362 
1363 static void
1364 hs_copylabel(struct hs_volume *hvp, unsigned char *label, int isjoliet)
1365 {
1366 	char	lbuf[64];	/* hs_joliet_cp() creates 48 bytes at most */
1367 
1368 	if (isjoliet) {
1369 		/*
1370 		 * hs_joliet_cp() will output 16..48 bytes.
1371 		 * We need to clear 'lbuf' to avoid junk chars past byte 15.
1372 		 */
1373 		bzero(lbuf, sizeof (lbuf));
1374 		(void) hs_joliet_cp((char *)label, lbuf, 32);
1375 		label = (unsigned char *)lbuf;
1376 	}
1377 	/* cdrom volid is at most 32 bytes */
1378 	bcopy(label, hvp->vol_id, 32);
1379 	hvp->vol_id[31] = NULL;
1380 }
1381 
1382 /*
1383  * Mount root file system.
1384  * "why" is ROOT_INIT on initial call, ROOT_REMOUNT if called to
1385  * remount the root file system, and ROOT_UNMOUNT if called to
1386  * unmount the root (e.g., as part of a system shutdown).
1387  *
1388  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
1389  * operation, goes along with auto-configuration.  A mechanism should be
1390  * provided by which machine-INdependent code in the kernel can say "get me the
1391  * right root file system" and "get me the right initial swap area", and have
1392  * that done in what may well be a machine-dependent fashion.
1393  * Unfortunately, it is also file-system-type dependent (NFS gets it via
1394  * bootparams calls, UFS gets it from various and sundry machine-dependent
1395  * mechanisms, as SPECFS does for swap).
1396  */
1397 static int
1398 hsfs_mountroot(struct vfs *vfsp, enum whymountroot why)
1399 {
1400 	int error;
1401 	struct hsfs *fsp;
1402 	struct hs_volume *fvolp;
1403 	static int hsfsrootdone = 0;
1404 	dev_t rootdev;
1405 	mode_t mode = 0;
1406 
1407 	if (why == ROOT_INIT) {
1408 		if (hsfsrootdone++)
1409 			return (EBUSY);
1410 		rootdev = getrootdev();
1411 		if (rootdev == (dev_t)NODEV)
1412 			return (ENODEV);
1413 		vfsp->vfs_dev = rootdev;
1414 		vfsp->vfs_flag |= VFS_RDONLY;
1415 	} else if (why == ROOT_REMOUNT) {
1416 		cmn_err(CE_NOTE, "hsfs_mountroot: ROOT_REMOUNT");
1417 		return (0);
1418 	} else if (why == ROOT_UNMOUNT) {
1419 		return (0);
1420 	}
1421 	error = vfs_lock(vfsp);
1422 	if (error) {
1423 		cmn_err(CE_NOTE, "hsfs_mountroot: couldn't get vfs_lock");
1424 		return (error);
1425 	}
1426 
1427 	error = hs_mountfs(vfsp, rootdev, "/", mode, 1, CRED(), 1);
1428 	/*
1429 	 * XXX - assumes root device is not indirect, because we don't set
1430 	 * rootvp.  Is rootvp used for anything?  If so, make another arg
1431 	 * to mountfs.
1432 	 */
1433 	if (error) {
1434 		vfs_unlock(vfsp);
1435 		if (rootvp) {
1436 			VN_RELE(rootvp);
1437 			rootvp = (struct vnode *)0;
1438 		}
1439 		return (error);
1440 	}
1441 	if (why == ROOT_INIT)
1442 		vfs_add((struct vnode *)0, vfsp,
1443 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1444 	vfs_unlock(vfsp);
1445 	fsp = VFS_TO_HSFS(vfsp);
1446 	fvolp = &fsp->hsfs_vol;
1447 #ifdef HSFS_CLKSET
1448 	if (fvolp->cre_date.tv_sec == 0) {
1449 		cmn_err(CE_NOTE, "hsfs_mountroot: cre_date.tv_sec == 0");
1450 		if (fvolp->mod_date.tv_sec == 0) {
1451 			cmn_err(CE_NOTE,
1452 			    "hsfs_mountroot: mod_date.tv_sec == 0");
1453 			cmn_err(CE_NOTE, "hsfs_mountroot: clkset(-1L)");
1454 			clkset(-1L);
1455 		} else {
1456 			clkset(fvolp->mod_date.tv_sec);
1457 		}
1458 	} else {
1459 		clkset(fvolp->mod_date.tv_sec);
1460 	}
1461 #else	/* HSFS_CLKSET */
1462 	clkset(-1L);
1463 #endif	/* HSFS_CLKSET */
1464 	return (0);
1465 }
1466 
1467 /*
1468  * hs_findvoldesc()
1469  *
1470  * Return the sector where the volume descriptor lives.  This is
1471  * a fixed value for "normal" cd-rom's, but can change for
1472  * multisession cd's.
1473  *
1474  * desc_sec is the same for high-sierra and iso 9660 formats, why
1475  * there are two differnt #defines used in the code for this is
1476  * beyond me.  These are standards, cast in concrete, right?
1477  * To be general, however, this function supports passing in different
1478  * values.
1479  */
1480 static int
1481 hs_findvoldesc(dev_t rdev, int desc_sec)
1482 {
1483 	int secno;
1484 	int error;
1485 	int rval;	/* ignored */
1486 
1487 #ifdef CDROMREADOFFSET
1488 	/*
1489 	 * Issue the Read Offset ioctl directly to the
1490 	 * device. Ignore any errors and set starting
1491 	 * secno to the default, otherwise add the
1492 	 * VOLDESC sector number to the offset.
1493 	 */
1494 	error = cdev_ioctl(rdev, CDROMREADOFFSET, (intptr_t)&secno,
1495 	    FNATIVE|FKIOCTL|FREAD, CRED(), &rval);
1496 	if (error) {
1497 		secno = desc_sec;
1498 	} else {
1499 		secno += desc_sec;
1500 	}
1501 #else
1502 	secno = desc_sec;
1503 #endif
1504 
1505 	return (secno);
1506 }
1507