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