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