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