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