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