xref: /titanic_52/usr/src/uts/common/fs/hsfs/hsfs_vfsops.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*
30  * VFS operations for High Sierra filesystem
31  */
32 
33 #include <sys/types.h>
34 #include <sys/isa_defs.h>
35 #include <sys/t_lock.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/sysmacros.h>
39 #include <sys/kmem.h>
40 #include <sys/signal.h>
41 #include <sys/user.h>
42 #include <sys/proc.h>
43 #include <sys/disp.h>
44 #include <sys/buf.h>
45 #include <sys/pathname.h>
46 #include <sys/vfs.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 /*
74  * These are needed for the CDROMREADOFFSET Code
75  */
76 #include <sys/cdio.h>
77 #include <sys/sunddi.h>
78 
79 #define	HSFS_CLKSET
80 
81 #include <sys/modctl.h>
82 
83 /*
84  * Options for mount.
85  */
86 #define	HOPT_GLOBAL	MNTOPT_GLOBAL
87 #define	HOPT_NOGLOBAL	MNTOPT_NOGLOBAL
88 #define	HOPT_MAPLCASE	"maplcase"
89 #define	HOPT_NOMAPLCASE	"nomaplcase"
90 #define	HOPT_NOTRAILDOT	"notraildot"
91 #define	HOPT_TRAILDOT	"traildot"
92 #define	HOPT_NRR	"nrr"
93 #define	HOPT_RR		"rr"
94 #define	HOPT_RO		MNTOPT_RO
95 
96 static char *global_cancel[] = { HOPT_NOGLOBAL, NULL };
97 static char *noglobal_cancel[] = { HOPT_GLOBAL, NULL };
98 static char *mapl_cancel[] = { HOPT_NOMAPLCASE, NULL };
99 static char *nomapl_cancel[] = { HOPT_MAPLCASE, NULL };
100 static char *ro_cancel[] = { MNTOPT_RW, NULL };
101 static char *rr_cancel[] = { HOPT_NRR, NULL };
102 static char *nrr_cancel[] = { HOPT_RR, NULL };
103 static char *trail_cancel[] = { HOPT_NOTRAILDOT, NULL };
104 static char *notrail_cancel[] = { HOPT_TRAILDOT, NULL };
105 
106 static mntopt_t hsfs_options[] = {
107 	{ HOPT_GLOBAL, global_cancel, NULL, 0, NULL },
108 	{ HOPT_NOGLOBAL, noglobal_cancel, NULL, MO_DEFAULT, NULL },
109 	{ HOPT_MAPLCASE, mapl_cancel, NULL, MO_DEFAULT, NULL },
110 	{ HOPT_NOMAPLCASE, nomapl_cancel, NULL, 0, NULL },
111 	{ HOPT_RO, ro_cancel, NULL, MO_DEFAULT, NULL },
112 	{ HOPT_RR, rr_cancel, NULL, MO_DEFAULT, NULL },
113 	{ HOPT_NRR, nrr_cancel, NULL, 0, NULL },
114 	{ HOPT_TRAILDOT, trail_cancel, NULL, MO_DEFAULT, NULL },
115 	{ HOPT_NOTRAILDOT, notrail_cancel, NULL, 0, NULL },
116 };
117 
118 static mntopts_t hsfs_proto_opttbl = {
119 	sizeof (hsfs_options) / sizeof (mntopt_t),
120 	hsfs_options
121 };
122 
123 static int hsfsinit(int, char *);
124 
125 static vfsdef_t vfw = {
126 	VFSDEF_VERSION,
127 	"hsfs",
128 	hsfsinit,
129 	VSW_HASPROTO,	/* We don't suppport remounting */
130 	&hsfs_proto_opttbl
131 };
132 
133 static struct modlfs modlfs = {
134 	&mod_fsops, "filesystem for HSFS", &vfw
135 };
136 
137 static struct modlinkage modlinkage = {
138 	MODREV_1, (void *)&modlfs, NULL
139 };
140 
141 char _depends_on[] = "fs/specfs";
142 
143 int
144 _init()
145 {
146 	return (mod_install(&modlinkage));
147 }
148 
149 int
150 _fini()
151 {
152 	return (EBUSY);
153 }
154 
155 int
156 _info(struct modinfo *modinfop)
157 {
158 	return (mod_info(&modlinkage, modinfop));
159 }
160 
161 #define	BDEVFLAG(dev)	((devopsp[getmajor(dev)])->devo_cb_ops->cb_flag)
162 
163 kmutex_t hs_mounttab_lock;
164 struct hsfs *hs_mounttab = NULL;
165 
166 /* default mode, uid, gid */
167 mode_t hsfs_default_mode = 0555;
168 uid_t hsfs_default_uid = 0;
169 gid_t hsfs_default_gid = 3;
170 
171 static int hsfs_mount(struct vfs *vfsp, struct vnode *mvp,
172 	struct mounta *uap, struct cred *cr);
173 static int hsfs_unmount(struct vfs *vfsp, int, struct cred *cr);
174 static int hsfs_root(struct vfs *vfsp, struct vnode **vpp);
175 static int hsfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp);
176 static int hsfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp);
177 static int hsfs_mountroot(struct vfs *, enum whymountroot);
178 
179 static int hs_mountfs(struct vfs *vfsp, dev_t dev, char *path,
180 	mode_t mode, int flags, struct cred *cr, int isroot);
181 static int hs_findhsvol(struct hsfs *fsp, struct vnode *vp,
182 	struct hs_volume *hvp);
183 static int hs_parsehsvol(struct hsfs *fsp, uchar_t *volp,
184 	struct hs_volume *hvp);
185 static int hs_findisovol(struct hsfs *fsp, struct vnode *vp,
186 	struct hs_volume *hvp);
187 static int hs_parseisovol(struct hsfs *fsp, uchar_t *volp,
188 	struct hs_volume *hvp);
189 static void hs_copylabel(struct hs_volume *, unsigned char *);
190 static int hs_getmdev(struct vfs *, char *fspec, int flags, dev_t *pdev,
191 	mode_t *mode, cred_t *cr);
192 static int hs_findvoldesc(dev_t rdev, int desc_sec);
193 
194 static int hsfsfstype;
195 
196 static int
197 hsfsinit(int fstype, char *name)
198 {
199 	static const fs_operation_def_t hsfs_vfsops_template[] = {
200 		VFSNAME_MOUNT, hsfs_mount,
201 		VFSNAME_UNMOUNT, hsfs_unmount,
202 		VFSNAME_ROOT, hsfs_root,
203 		VFSNAME_STATVFS, hsfs_statvfs,
204 		VFSNAME_VGET, hsfs_vget,
205 		VFSNAME_MOUNTROOT, hsfs_mountroot,
206 		NULL, NULL
207 	};
208 	int error;
209 
210 	error = vfs_setfsops(fstype, hsfs_vfsops_template, NULL);
211 	if (error != 0) {
212 		cmn_err(CE_WARN, "hsfsinit: bad vfs ops template");
213 		return (error);
214 	}
215 
216 	error = vn_make_ops(name, hsfs_vnodeops_template, &hsfs_vnodeops);
217 	if (error != 0) {
218 		(void) vfs_freevfsops_by_type(fstype);
219 		cmn_err(CE_WARN, "hsfsinit: bad vnode ops template");
220 		return (error);
221 	}
222 
223 	hsfsfstype = fstype;
224 	mutex_init(&hs_mounttab_lock, NULL, MUTEX_DEFAULT, NULL);
225 	hs_init_hsnode_cache();
226 	return (0);
227 }
228 
229 /*ARGSUSED*/
230 static int
231 hsfs_mount(struct vfs *vfsp, struct vnode *mvp,
232     struct mounta *uap, struct cred *cr)
233 {
234 	int		vnode_busy;
235 	dev_t		dev;
236 	struct pathname dpn;
237 	int		error;
238 	mode_t		mode;
239 	int		flags;	/* this will hold the mount specific data */
240 
241 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0)
242 		return (error);
243 
244 	if (mvp->v_type != VDIR)
245 		return (ENOTDIR);
246 
247 	/* mount option must be read only, else mount will be rejected */
248 	if (!(uap->flags & MS_RDONLY))
249 		return (EROFS);
250 
251 	/*
252 	 * We already told the framework that we don't support remounting.
253 	 */
254 	ASSERT(!(uap->flags & MS_REMOUNT));
255 
256 	mutex_enter(&mvp->v_lock);
257 	vnode_busy = (mvp->v_count != 1) || (mvp->v_flag & VROOT);
258 	mutex_exit(&mvp->v_lock);
259 
260 	if ((uap->flags & MS_OVERLAY) == 0 && vnode_busy) {
261 		return (EBUSY);
262 	}
263 
264 	/*
265 	 * Check for the options that actually affect things
266 	 * at our level.
267 	 */
268 	flags = 0;
269 	if (vfs_optionisset(vfsp, HOPT_NOMAPLCASE, NULL))
270 	    flags |= HSFSMNT_NOMAPLCASE;
271 	if (vfs_optionisset(vfsp, HOPT_NOTRAILDOT, NULL))
272 	    flags |= HSFSMNT_NOTRAILDOT;
273 	if (vfs_optionisset(vfsp, HOPT_NRR, NULL))
274 	    flags |= HSFSMNT_NORRIP;
275 
276 	error = pn_get(uap->dir, (uap->flags & MS_SYSSPACE) ?
277 	    UIO_SYSSPACE : UIO_USERSPACE, &dpn);
278 	if (error)
279 		return (error);
280 
281 	if ((error = hs_getmdev(vfsp, uap->spec, uap->flags, &dev,
282 		&mode, cr)) != 0) {
283 		pn_free(&dpn);
284 		return (error);
285 	}
286 
287 	/*
288 	 * If the device is a tape, return error
289 	 */
290 	if ((BDEVFLAG(dev) & D_TAPE) == D_TAPE)  {
291 		pn_free(&dpn);
292 		return (ENOTBLK);
293 	}
294 
295 	/*
296 	 * Mount the filesystem.
297 	 */
298 	error = hs_mountfs(vfsp, dev, dpn.pn_path, mode, flags, cr, 0);
299 	pn_free(&dpn);
300 	return (error);
301 }
302 
303 /*ARGSUSED*/
304 static int
305 hsfs_unmount(
306 	struct vfs *vfsp,
307 	int flag,
308 	struct cred *cr)
309 {
310 	struct hsfs **tspp;
311 	struct hsfs *fsp;
312 
313 	if (secpolicy_fs_unmount(cr, vfsp) != 0)
314 		return (EPERM);
315 
316 	/*
317 	 * forced unmount is not supported by this file system
318 	 * and thus, ENOTSUP is being returned.
319 	 */
320 	if (flag & MS_FORCE)
321 		return (ENOTSUP);
322 
323 	fsp = VFS_TO_HSFS(vfsp);
324 
325 	if (fsp->hsfs_rootvp->v_count != 1)
326 		return (EBUSY);
327 
328 	/* destroy all old pages and hsnodes for this vfs */
329 	if (hs_synchash(vfsp))
330 		return (EBUSY);
331 
332 	mutex_enter(&hs_mounttab_lock);
333 	for (tspp = &hs_mounttab; *tspp != NULL; tspp = &(*tspp)->hsfs_next) {
334 		if (*tspp == fsp)
335 			break;
336 	}
337 	if (*tspp == NULL) {
338 		mutex_exit(&hs_mounttab_lock);
339 		panic("hsfs_unmount: vfs not mounted?");
340 		/*NOTREACHED*/
341 	}
342 
343 	*tspp = fsp->hsfs_next;
344 
345 	mutex_exit(&hs_mounttab_lock);
346 
347 	(void) VOP_CLOSE(fsp->hsfs_devvp, FREAD, 1, (offset_t)0, cr);
348 	VN_RELE(fsp->hsfs_devvp);
349 	/* free path table space */
350 	if (fsp->hsfs_ptbl != NULL)
351 		kmem_free(fsp->hsfs_ptbl,
352 			(size_t)fsp->hsfs_vol.ptbl_len);
353 	/* free path table index table */
354 	if (fsp->hsfs_ptbl_idx != NULL)
355 		kmem_free(fsp->hsfs_ptbl_idx, (size_t)
356 			(fsp->hsfs_ptbl_idx_size * sizeof (struct ptable_idx)));
357 
358 	/* free "mounted on" pathame */
359 	if (fsp->hsfs_fsmnt != NULL)
360 		kmem_free(fsp->hsfs_fsmnt, strlen(fsp->hsfs_fsmnt) + 1);
361 
362 	mutex_destroy(&fsp->hsfs_free_lock);
363 	rw_destroy(&fsp->hsfs_hash_lock);
364 
365 	kmem_free(fsp, sizeof (*fsp));
366 	return (0);
367 }
368 
369 /*ARGSUSED*/
370 static int
371 hsfs_root(struct vfs *vfsp, struct vnode **vpp)
372 {
373 	*vpp = (VFS_TO_HSFS(vfsp))->hsfs_rootvp;
374 	VN_HOLD(*vpp);
375 	return (0);
376 }
377 
378 /*ARGSUSED*/
379 static int
380 hsfs_statvfs(struct vfs *vfsp, struct statvfs64 *sbp)
381 {
382 	struct hsfs *fsp;
383 	dev32_t d32;
384 
385 	fsp = VFS_TO_HSFS(vfsp);
386 	if (fsp->hsfs_magic != HSFS_MAGIC)
387 		return (EINVAL);
388 	bzero(sbp, sizeof (*sbp));
389 	sbp->f_bsize = vfsp->vfs_bsize;
390 	sbp->f_frsize = sbp->f_bsize; /* no fragment, same as block size */
391 	sbp->f_blocks = (fsblkcnt64_t)fsp->hsfs_vol.vol_size;
392 
393 	sbp->f_bfree = (fsblkcnt64_t)0;
394 	sbp->f_bavail = (fsblkcnt64_t)0;
395 	sbp->f_files = (fsfilcnt64_t)-1;
396 	sbp->f_ffree = (fsfilcnt64_t)0;
397 	sbp->f_favail = (fsfilcnt64_t)0;
398 	(void) cmpldev(&d32, vfsp->vfs_dev);
399 	sbp->f_fsid = d32;
400 	(void) strcpy(sbp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
401 	sbp->f_flag = vf_to_stf(vfsp->vfs_flag);
402 	sbp->f_namemax = fsp->hsfs_namemax;
403 	(void) strcpy(sbp->f_fstr, fsp->hsfs_vol.vol_id);
404 
405 	return (0);
406 }
407 
408 /*
409  * Previously nodeid was declared as uint32_t. This has been changed
410  * to conform better with the ISO9660 standard. The standard states that
411  * a LBN can be a 32 bit number, as the MAKE_NODEID macro shifts this
412  * LBN 11 places left (LBN_TO_BYTE) and then shifts the result 5 right
413  * (divide by 32) we are left with the potential of an overflow if
414  * confined to a 32 bit value.
415  */
416 
417 static int
418 hsfs_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
419 {
420 	struct hsfid *fid;
421 	struct hsfs *fsp;
422 	ino64_t nodeid;
423 	int error;
424 
425 	fsp = (struct hsfs *)VFS_TO_HSFS(vfsp);
426 	fid = (struct hsfid *)fidp;
427 
428 	/*
429 	 * Look for vnode on hashlist.
430 	 * If found, it's now active and the refcnt was incremented.
431 	 */
432 
433 	rw_enter(&fsp->hsfs_hash_lock, RW_READER);
434 
435 	nodeid = (ino64_t)MAKE_NODEID(fid->hf_dir_lbn, fid->hf_dir_off, vfsp);
436 
437 	if ((*vpp = hs_findhash(nodeid, vfsp)) == NULL) {
438 		/*
439 		 * Not in cache, so we need to remake it.
440 		 * hs_remakenode() will read the directory entry
441 		 * and then check again to see if anyone else has
442 		 * put it in the cache.
443 		 */
444 		rw_exit(&fsp->hsfs_hash_lock);
445 		error = hs_remakenode(fid->hf_dir_lbn, (uint_t)fid->hf_dir_off,
446 		    vfsp, vpp);
447 		return (error);
448 	}
449 	rw_exit(&fsp->hsfs_hash_lock);
450 	return (0);
451 }
452 
453 
454 #define	CHECKSUM_SIZE				(64 * 1024)
455 
456 /*
457  * Compute a CD-ROM fsid by checksumming the first 64K of data on the CD
458  * We use the 'fsp' argument to determine the location of the root
459  * directory entry, and we start reading from there.
460  */
461 static int
462 compute_cdrom_id(struct hsfs *fsp, vnode_t *devvp)
463 {
464 	uint_t		secno;
465 	struct hs_volume *hsvp = &fsp->hsfs_vol;
466 	struct buf	*bp;
467 	int		error;
468 	int		fsid;
469 
470 	secno = hsvp->root_dir.ext_lbn >> hsvp->lbn_secshift;
471 	bp = bread(devvp->v_rdev, secno * 4, CHECKSUM_SIZE);
472 	error = geterror(bp);
473 
474 	/*
475 	 * An error on read or a partial read means we asked
476 	 * for a nonexistant/corrupted piece of the device
477 	 * (including past-the-end of the media). Don't
478 	 * try to use the checksumming method then.
479 	 */
480 	if (!error && bp->b_bcount == CHECKSUM_SIZE) {
481 		int *ibuf = (int *)bp->b_un.b_addr;
482 		int i;
483 
484 		fsid = 0;
485 
486 		for (i = 0; i < CHECKSUM_SIZE / sizeof (int); i++)
487 			fsid ^= ibuf[ i ];
488 	} else {
489 		/*
490 		 * Fallback - use creation date
491 		 */
492 		fsid = hsvp->cre_date.tv_sec;
493 	}
494 
495 	brelse(bp);
496 
497 	return (fsid);
498 }
499 
500 
501 /*ARGSUSED*/
502 static int
503 hs_mountfs(
504 	struct vfs	*vfsp,
505 	dev_t		dev,
506 	char		*path,
507 	mode_t		mode,
508 	int		mount_flags,
509 	struct cred	*cr,
510 	int		isroot)
511 {
512 	struct vnode	*devvp;
513 	struct hsfs	*tsp;
514 	struct hsfs	*fsp = NULL;
515 	struct vattr	vap;
516 	struct hsnode	*hp;
517 	int		error;
518 	struct timeval	tv;
519 	int		fsid;
520 	int		use_rrip = (mount_flags & HSFSMNT_NORRIP) == 0;
521 
522 	/*
523 	 * Open the device
524 	 */
525 	devvp = makespecvp(dev, VBLK);
526 	ASSERT(devvp != 0);
527 
528 	/*
529 	 * Open the target device (file) for read only.
530 	 */
531 	if (error = VOP_OPEN(&devvp, FREAD, cr)) {
532 		VN_RELE(devvp);
533 		return (error);
534 	}
535 
536 	/*
537 	 * Refuse to go any further if this
538 	 * device is being used for swapping
539 	 */
540 	if (IS_SWAPVP(common_specvp(devvp))) {
541 		error = EBUSY;
542 		goto cleanup;
543 	}
544 
545 	vap.va_mask = AT_SIZE;
546 	if ((error = VOP_GETATTR(devvp, &vap, ATTR_COMM, cr)) != 0) {
547 		cmn_err(CE_NOTE, "Cannot get attributes of the CD-ROM driver");
548 		goto cleanup;
549 	}
550 
551 	/*
552 	 * Make sure we have a nonzero size partition.
553 	 * The current version of the SD driver will *not* fail the open
554 	 * of such a partition so we have to check for it here.
555 	 */
556 	if (vap.va_size == 0) {
557 		error = ENXIO;
558 		goto cleanup;
559 	}
560 
561 	/*
562 	 * Init a new hsfs structure.
563 	 */
564 	fsp = kmem_zalloc(sizeof (*fsp), KM_SLEEP);
565 
566 	/* hardwire perms, uid, gid */
567 	fsp->hsfs_vol.vol_uid = hsfs_default_uid;
568 	fsp->hsfs_vol.vol_gid =  hsfs_default_gid;
569 	fsp->hsfs_vol.vol_prot = hsfs_default_mode;
570 
571 	/*
572 	 * Look for a Standard File Structure Volume Descriptor,
573 	 * of which there must be at least one.
574 	 * If found, check for volume size consistency.
575 	 *
576 	 * XXX - va_size may someday not be large enough to do this correctly.
577 	 */
578 	error = hs_findhsvol(fsp, devvp, &fsp->hsfs_vol);
579 	if (error == EINVAL) /* not in hs format, try iso 9660 format */
580 		error = hs_findisovol(fsp, devvp, &fsp->hsfs_vol);
581 
582 	if (error)
583 		goto cleanup;
584 
585 	/*
586 	 * Generate a file system ID from the CD-ROM,
587 	 * and check it for uniqueness.
588 	 *
589 	 * What we are aiming for is some chance of integrity
590 	 * across disk change.  That is, if a client has an fhandle,
591 	 * it will be valid as long as the same disk is mounted.
592 	 */
593 	fsid = compute_cdrom_id(fsp, devvp);
594 
595 	mutex_enter(&hs_mounttab_lock);
596 
597 	if (fsid == 0 || fsid == -1) {
598 		uniqtime(&tv);
599 		fsid = tv.tv_sec;
600 	} else	/* make sure that the fsid is unique */
601 		for (tsp = hs_mounttab; tsp != NULL; tsp = tsp->hsfs_next) {
602 			if (fsid == tsp->hsfs_vfs->vfs_fsid.val[0]) {
603 				uniqtime(&tv);
604 				fsid = tv.tv_sec;
605 				break;
606 			}
607 		}
608 
609 	fsp->hsfs_next = hs_mounttab;
610 	hs_mounttab = fsp;
611 
612 	fsp->hsfs_devvp = devvp;
613 	fsp->hsfs_vfs = vfsp;
614 	fsp->hsfs_fsmnt = kmem_alloc(strlen(path) + 1, KM_SLEEP);
615 	(void) strcpy(fsp->hsfs_fsmnt, path);
616 
617 	mutex_init(&fsp->hsfs_free_lock, NULL, MUTEX_DEFAULT, NULL);
618 	rw_init(&fsp->hsfs_hash_lock, NULL, RW_DEFAULT, NULL);
619 
620 	vfsp->vfs_data = (caddr_t)fsp;
621 	vfsp->vfs_dev = dev;
622 	vfsp->vfs_fstype = hsfsfstype;
623 	vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size; /* %% */
624 	vfsp->vfs_fsid.val[0] = fsid;
625 	vfsp->vfs_fsid.val[1] =  hsfsfstype;
626 
627 	/*
628 	 * If the root directory does not appear to be
629 	 * valid, use what it points to as "." instead.
630 	 * Some Defense Mapping Agency disks are non-conformant
631 	 * in this way.
632 	 */
633 	if (!hsfs_valid_dir(&fsp->hsfs_vol.root_dir)) {
634 		hs_log_bogus_disk_warning(fsp, HSFS_ERR_BAD_ROOT_DIR, 0);
635 		if (hs_remakenode(fsp->hsfs_vol.root_dir.ext_lbn,
636 			    (uint_t)0, vfsp, &fsp->hsfs_rootvp)) {
637 			error = EINVAL;
638 			mutex_exit(&hs_mounttab_lock);
639 			goto cleanup;
640 		}
641 	} else {
642 		fsp->hsfs_rootvp = hs_makenode(&fsp->hsfs_vol.root_dir,
643 			fsp->hsfs_vol.root_dir.ext_lbn, 0, vfsp);
644 	}
645 
646 	/* mark vnode as VROOT */
647 	fsp->hsfs_rootvp->v_flag |= VROOT;
648 
649 	/* Here we take care of some special case stuff for mountroot */
650 	if (isroot) {
651 		fsp->hsfs_rootvp->v_rdev = devvp->v_rdev;
652 		rootvp = fsp->hsfs_rootvp;
653 	}
654 
655 	/* XXX - ignore the path table for now */
656 	fsp->hsfs_ptbl = NULL;
657 	hp = VTOH(fsp->hsfs_rootvp);
658 	hp->hs_ptbl_idx = NULL;
659 
660 	if (use_rrip)
661 		hs_check_root_dirent(fsp->hsfs_rootvp, &(hp->hs_dirent));
662 
663 	fsp->hsfs_namemax = IS_RRIP_IMPLEMENTED(fsp)
664 					? RRIP_FILE_NAMELEN
665 					: ISO_FILE_NAMELEN;
666 	/*
667 	 * if RRIP, don't copy NOMAPLCASE or NOTRAILDOT to hsfs_flags
668 	 */
669 	if (IS_RRIP_IMPLEMENTED(fsp))
670 		mount_flags &= ~(HSFSMNT_NOMAPLCASE | HSFSMNT_NOTRAILDOT);
671 
672 	fsp->hsfs_flags = mount_flags;
673 
674 	/* set the magic word */
675 	fsp->hsfs_magic = HSFS_MAGIC;
676 	mutex_exit(&hs_mounttab_lock);
677 
678 	return (0);
679 
680 cleanup:
681 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, cr);
682 	VN_RELE(devvp);
683 	if (fsp)
684 		kmem_free(fsp, sizeof (*fsp));
685 	return (error);
686 }
687 
688 /*
689  * hs_findhsvol()
690  *
691  * Locate the Standard File Structure Volume Descriptor and
692  * parse it into an hs_volume structure.
693  *
694  * XXX - May someday want to look for Coded Character Set FSVD, too.
695  */
696 static int
697 hs_findhsvol(struct hsfs *fsp, struct vnode *vp, struct hs_volume *hvp)
698 {
699 	struct buf *secbp;
700 	int i;
701 	uchar_t *volp;
702 	int error;
703 	uint_t secno;
704 
705 	secno = hs_findvoldesc(vp->v_rdev, HS_VOLDESC_SEC);
706 	secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
707 	error = geterror(secbp);
708 
709 	if (error != 0) {
710 		cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)", error);
711 		brelse(secbp);
712 		return (error);
713 	}
714 
715 	volp = (uchar_t *)secbp->b_un.b_addr;
716 
717 	while (HSV_DESC_TYPE(volp) != VD_EOV) {
718 		for (i = 0; i < HSV_ID_STRLEN; i++)
719 			if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
720 				goto cantfind;
721 		if (HSV_STD_VER(volp) != HSV_ID_VER)
722 			goto cantfind;
723 		switch (HSV_DESC_TYPE(volp)) {
724 		case VD_SFS:
725 			/* Standard File Structure */
726 			fsp->hsfs_vol_type = HS_VOL_TYPE_HS;
727 			error = hs_parsehsvol(fsp, volp, hvp);
728 			brelse(secbp);
729 			return (error);
730 
731 		case VD_CCFS:
732 			/* Coded Character File Structure */
733 		case VD_BOOT:
734 		case VD_UNSPEC:
735 		case VD_EOV:
736 			break;
737 		}
738 		brelse(secbp);
739 		++secno;
740 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
741 
742 		error = geterror(secbp);
743 
744 		if (error != 0) {
745 			cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)",
746 				error);
747 			brelse(secbp);
748 			return (error);
749 		}
750 
751 		volp = (uchar_t *)secbp->b_un.b_addr;
752 	}
753 cantfind:
754 	brelse(secbp);
755 	return (EINVAL);
756 }
757 
758 /*
759  * hs_parsehsvol
760  *
761  * Parse the Standard File Structure Volume Descriptor into
762  * an hs_volume structure.  We can't just bcopy it into the
763  * structure because of byte-ordering problems.
764  *
765  */
766 static int
767 hs_parsehsvol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
768 {
769 	hvp->vol_size = HSV_VOL_SIZE(volp);
770 	hvp->lbn_size = HSV_BLK_SIZE(volp);
771 	if (hvp->lbn_size == 0) {
772 		cmn_err(CE_NOTE, "hs_parsehsvol: logical block size in the "
773 			"SFSVD is zero");
774 		return (EINVAL);
775 	}
776 	hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
777 	hvp->lbn_secshift = ffs((long)howmany(HS_SECTOR_SIZE,
778 				(int)hvp->lbn_size)) - 1;
779 	hvp->lbn_maxoffset = hvp->lbn_size - 1;
780 	hs_parse_longdate(HSV_cre_date(volp), &hvp->cre_date);
781 	hs_parse_longdate(HSV_mod_date(volp), &hvp->mod_date);
782 	hvp->file_struct_ver = HSV_FILE_STRUCT_VER(volp);
783 	hvp->ptbl_len = HSV_PTBL_SIZE(volp);
784 	hvp->vol_set_size = (ushort_t)HSV_SET_SIZE(volp);
785 	hvp->vol_set_seq = (ushort_t)HSV_SET_SEQ(volp);
786 #if defined(_LITTLE_ENDIAN)
787 	hvp->ptbl_lbn = HSV_PTBL_MAN_LS(volp);
788 #else
789 	hvp->ptbl_lbn = HSV_PTBL_MAN_MS(volp);
790 #endif
791 	hs_copylabel(hvp, HSV_VOL_ID(volp));
792 
793 	/*
794 	 * Make sure that lbn_size is a power of two and otherwise valid.
795 	 */
796 	if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
797 		cmn_err(CE_NOTE,
798 			"hsfs: %d-byte logical block size not supported",
799 			hvp->lbn_size);
800 		return (EINVAL);
801 	}
802 	return (hs_parsedir(fsp, HSV_ROOT_DIR(volp), &hvp->root_dir,
803 			(char *)NULL, (int *)NULL));
804 }
805 
806 /*
807  * hs_findisovol()
808  *
809  * Locate the Primary Volume Descriptor
810  * parse it into an hs_volume structure.
811  *
812  * XXX - Supplementary, Partition not yet done
813  */
814 static int
815 hs_findisovol(struct hsfs *fsp, struct vnode *vp,
816     struct hs_volume *hvp)
817 {
818 	struct buf *secbp;
819 	int i;
820 	uchar_t *volp;
821 	int error;
822 	uint_t secno;
823 	int foundpvd = 0;
824 
825 	secno = hs_findvoldesc(vp->v_rdev, ISO_VOLDESC_SEC);
826 	secbp = bread(vp->v_rdev, secno * 4, ISO_SECTOR_SIZE);
827 	error = geterror(secbp);
828 
829 	if (error != 0) {
830 		cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)", error);
831 		brelse(secbp);
832 		return (error);
833 	}
834 
835 	volp = (uchar_t *)secbp->b_un.b_addr;
836 
837 	while ((enum iso_voldesc_type) ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
838 		for (i = 0; i < ISO_ID_STRLEN; i++)
839 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
840 				goto cantfind;
841 		if (ISO_STD_VER(volp) != ISO_ID_VER)
842 			goto cantfind;
843 		switch (ISO_DESC_TYPE(volp)) {
844 		case ISO_VD_PVD:
845 			/* Standard File Structure */
846 			if (foundpvd != 1) {
847 				fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
848 				if (error = hs_parseisovol(fsp, volp, hvp)) {
849 					brelse(secbp);
850 					return (error);
851 				}
852 				foundpvd = 1;
853 			}
854 			break;
855 		case ISO_VD_SVD:
856 			/* Supplementary Volume Descriptor */
857 			break;
858 		case ISO_VD_BOOT:
859 			break;
860 		case ISO_VD_VPD:
861 			/* currently cannot handle partition */
862 			break;
863 		case VD_EOV:
864 			break;
865 		}
866 		brelse(secbp);
867 		++secno;
868 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
869 		error = geterror(secbp);
870 
871 		if (error != 0) {
872 			cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)",
873 				    error);
874 			brelse(secbp);
875 			return (error);
876 		}
877 
878 		volp = (uchar_t *)secbp->b_un.b_addr;
879 	}
880 	if (foundpvd) {
881 		brelse(secbp);
882 		return (0);
883 	}
884 cantfind:
885 	brelse(secbp);
886 	return (EINVAL);
887 }
888 /*
889  * hs_parseisovol
890  *
891  * Parse the Primary Volume Descriptor into an hs_volume structure.
892  *
893  */
894 static int
895 hs_parseisovol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
896 {
897 	hvp->vol_size = ISO_VOL_SIZE(volp);
898 	hvp->lbn_size = ISO_BLK_SIZE(volp);
899 	if (hvp->lbn_size == 0) {
900 		cmn_err(CE_NOTE, "hs_parseisovol: logical block size in the "
901 			"PVD is zero");
902 		return (EINVAL);
903 	}
904 	hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
905 	hvp->lbn_secshift = ffs((long)howmany(ISO_SECTOR_SIZE,
906 				(int)hvp->lbn_size)) - 1;
907 	hvp->lbn_maxoffset = hvp->lbn_size - 1;
908 	hs_parse_longdate(ISO_cre_date(volp), &hvp->cre_date);
909 	hs_parse_longdate(ISO_mod_date(volp), &hvp->mod_date);
910 	hvp->file_struct_ver = ISO_FILE_STRUCT_VER(volp);
911 	hvp->ptbl_len = ISO_PTBL_SIZE(volp);
912 	hvp->vol_set_size = (ushort_t)ISO_SET_SIZE(volp);
913 	hvp->vol_set_seq = (ushort_t)ISO_SET_SEQ(volp);
914 #if defined(_LITTLE_ENDIAN)
915 	hvp->ptbl_lbn = ISO_PTBL_MAN_LS(volp);
916 #else
917 	hvp->ptbl_lbn = ISO_PTBL_MAN_MS(volp);
918 #endif
919 	hs_copylabel(hvp, ISO_VOL_ID(volp));
920 
921 	/*
922 	 * Make sure that lbn_size is a power of two and otherwise valid.
923 	 */
924 	if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
925 		cmn_err(CE_NOTE,
926 			"hsfs: %d-byte logical block size not supported",
927 			hvp->lbn_size);
928 		return (EINVAL);
929 	}
930 	return (hs_parsedir(fsp, ISO_ROOT_DIR(volp), &hvp->root_dir,
931 			(char *)NULL, (int *)NULL));
932 }
933 
934 /*
935  * Common code for mount and umount.
936  * Check that the user's argument is a reasonable
937  * thing on which to mount, and return the device number if so.
938  */
939 static int
940 hs_getmdev(struct vfs *vfsp, char *fspec, int flags, dev_t *pdev, mode_t *mode,
941     cred_t *cr)
942 {
943 	int error;
944 	struct vnode *vp;
945 	struct vattr vap;
946 	dev_t dev;
947 
948 	/*
949 	 * Get the device to be mounted
950 	 */
951 	error = lookupname(fspec, (flags & MS_SYSSPACE) ?
952 	    UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
953 	if (error) {
954 		if (error == ENOENT) {
955 			return (ENODEV);	/* needs translation */
956 		}
957 		return (error);
958 	}
959 	if (vp->v_type != VBLK) {
960 		VN_RELE(vp);
961 		return (ENOTBLK);
962 	}
963 	/*
964 	 * Can we read from the device?
965 	 */
966 	if ((error = VOP_ACCESS(vp, VREAD, 0, cr)) != 0 ||
967 	    (error = secpolicy_spec_open(cr, vp, FREAD)) != 0) {
968 		VN_RELE(vp);
969 		return (error);
970 	}
971 
972 	vap.va_mask = AT_MODE;		/* get protection mode */
973 	(void) VOP_GETATTR(vp, &vap, 0, CRED());
974 	*mode = vap.va_mode;
975 
976 	dev = *pdev = vp->v_rdev;
977 	VN_RELE(vp);
978 
979 	/*
980 	 * Ensure that this device isn't already mounted,
981 	 * unless this is a REMOUNT request or we are told to suppress
982 	 * mount checks.
983 	 */
984 	if ((flags & MS_NOCHECK) == 0) {
985 		if (vfs_devmounting(dev, vfsp))
986 			return (EBUSY);
987 		if (vfs_devismounted(dev) && !(flags & MS_REMOUNT))
988 			return (EBUSY);
989 	}
990 
991 	if (getmajor(*pdev) >= devcnt)
992 		return (ENXIO);
993 	return (0);
994 }
995 
996 static void
997 hs_copylabel(struct hs_volume *hvp, unsigned char *label)
998 {
999 	/* cdrom volid is at most 32 bytes */
1000 	bcopy(label, hvp->vol_id, 32);
1001 	hvp->vol_id[31] = NULL;
1002 }
1003 
1004 /*
1005  * Mount root file system.
1006  * "why" is ROOT_INIT on initial call, ROOT_REMOUNT if called to
1007  * remount the root file system, and ROOT_UNMOUNT if called to
1008  * unmount the root (e.g., as part of a system shutdown).
1009  *
1010  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
1011  * operation, goes along with auto-configuration.  A mechanism should be
1012  * provided by which machine-INdependent code in the kernel can say "get me the
1013  * right root file system" and "get me the right initial swap area", and have
1014  * that done in what may well be a machine-dependent fashion.
1015  * Unfortunately, it is also file-system-type dependent (NFS gets it via
1016  * bootparams calls, UFS gets it from various and sundry machine-dependent
1017  * mechanisms, as SPECFS does for swap).
1018  */
1019 static int
1020 hsfs_mountroot(struct vfs *vfsp, enum whymountroot why)
1021 {
1022 	int error;
1023 	struct hsfs *fsp;
1024 	struct hs_volume *fvolp;
1025 	static int hsfsrootdone = 0;
1026 	dev_t rootdev;
1027 	mode_t mode = 0;
1028 
1029 	if (why == ROOT_INIT) {
1030 		if (hsfsrootdone++)
1031 			return (EBUSY);
1032 		rootdev = getrootdev();
1033 		if (rootdev == (dev_t)NODEV)
1034 			return (ENODEV);
1035 		vfsp->vfs_dev = rootdev;
1036 		vfsp->vfs_flag |= VFS_RDONLY;
1037 	} else if (why == ROOT_REMOUNT) {
1038 		cmn_err(CE_NOTE, "hsfs_mountroot: ROOT_REMOUNT");
1039 		return (0);
1040 	} else if (why == ROOT_UNMOUNT) {
1041 		return (0);
1042 	}
1043 	error = vfs_lock(vfsp);
1044 	if (error) {
1045 		cmn_err(CE_NOTE, "hsfs_mountroot: couldn't get vfs_lock");
1046 		return (error);
1047 	}
1048 
1049 	error = hs_mountfs(vfsp, rootdev, "/", mode, 1, CRED(), 1);
1050 	/*
1051 	 * XXX - assumes root device is not indirect, because we don't set
1052 	 * rootvp.  Is rootvp used for anything?  If so, make another arg
1053 	 * to mountfs.
1054 	 */
1055 	if (error) {
1056 		vfs_unlock(vfsp);
1057 		if (rootvp) {
1058 			VN_RELE(rootvp);
1059 			rootvp = (struct vnode *)0;
1060 		}
1061 		return (error);
1062 	}
1063 	if (why == ROOT_INIT)
1064 		vfs_add((struct vnode *)0, vfsp,
1065 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1066 	vfs_unlock(vfsp);
1067 	fsp = VFS_TO_HSFS(vfsp);
1068 	fvolp = &fsp->hsfs_vol;
1069 #ifdef HSFS_CLKSET
1070 	if (fvolp->cre_date.tv_sec == 0) {
1071 	    cmn_err(CE_NOTE, "hsfs_mountroot: cre_date.tv_sec == 0");
1072 	    if (fvolp->mod_date.tv_sec == 0) {
1073 		cmn_err(CE_NOTE, "hsfs_mountroot: mod_date.tv_sec == 0");
1074 		cmn_err(CE_NOTE, "hsfs_mountroot: clkset(-1L)");
1075 		clkset(-1L);
1076 	    } else
1077 		clkset(fvolp->mod_date.tv_sec);
1078 	} else
1079 	    clkset(fvolp->mod_date.tv_sec);
1080 #else	/* HSFS_CLKSET */
1081 	clkset(-1L);
1082 #endif	/* HSFS_CLKSET */
1083 	return (0);
1084 }
1085 
1086 /*
1087  * hs_findvoldesc()
1088  *
1089  * Return the sector where the volume descriptor lives.  This is
1090  * a fixed value for "normal" cd-rom's, but can change for
1091  * multisession cd's.
1092  *
1093  * desc_sec is the same for high-sierra and iso 9660 formats, why
1094  * there are two differnt #defines used in the code for this is
1095  * beyond me.  These are standards, cast in concrete, right?
1096  * To be general, however, this function supports passing in different
1097  * values.
1098  */
1099 static int
1100 hs_findvoldesc(dev_t rdev, int desc_sec)
1101 {
1102 	int secno;
1103 	int error;
1104 	int rval;	/* ignored */
1105 
1106 #ifdef CDROMREADOFFSET
1107 	/*
1108 	 * Issue the Read Offset ioctl directly to the
1109 	 * device. Ignore any errors and set starting
1110 	 * secno to the default, otherwise add the
1111 	 * VOLDESC sector number to the offset.
1112 	 */
1113 	error = cdev_ioctl(rdev, CDROMREADOFFSET, (intptr_t)&secno,
1114 	    FNATIVE|FKIOCTL|FREAD, CRED(), &rval);
1115 	if (error) {
1116 		secno = desc_sec;
1117 	} else {
1118 		secno += desc_sec;
1119 	}
1120 #else
1121 	secno = desc_sec;
1122 #endif
1123 
1124 	return (secno);
1125 }
1126