xref: /titanic_44/usr/src/uts/common/fs/hsfs/hsfs_vfsops.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 2004 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 	int		size = CHECKSUM_SIZE;
470 
471 	secno = hsvp->root_dir.ext_lbn >> hsvp->lbn_secshift;
472 	bp = bread(devvp->v_rdev, secno * 4, size);
473 	error = geterror(bp);
474 	if (!error) {
475 		int *ibuf = (int *)bp->b_un.b_addr;
476 		int isize = size / sizeof (int);
477 		int i;
478 
479 		fsid = 0;
480 
481 		for (i = 0; i < isize; i++)
482 			fsid ^= ibuf[ i ];
483 	} else	/* use creation date */
484 		fsid = hsvp->cre_date.tv_sec;
485 
486 	brelse(bp);
487 
488 	return (fsid);
489 }
490 
491 
492 /*ARGSUSED*/
493 static int
494 hs_mountfs(
495 	struct vfs	*vfsp,
496 	dev_t		dev,
497 	char		*path,
498 	mode_t		mode,
499 	int		mount_flags,
500 	struct cred	*cr,
501 	int		isroot)
502 {
503 	struct vnode	*devvp;
504 	struct hsfs	*tsp;
505 	struct hsfs	*fsp = NULL;
506 	struct vattr	vap;
507 	struct hsnode	*hp;
508 	int		error;
509 	struct timeval	tv;
510 	int		fsid;
511 	int		use_rrip = (mount_flags & HSFSMNT_NORRIP) == 0;
512 
513 	/*
514 	 * Open the device
515 	 */
516 	devvp = makespecvp(dev, VBLK);
517 	ASSERT(devvp != 0);
518 
519 	/*
520 	 * Open the target device (file) for read only.
521 	 */
522 	if (error = VOP_OPEN(&devvp, FREAD, cr)) {
523 		VN_RELE(devvp);
524 		return (error);
525 	}
526 
527 	/*
528 	 * Refuse to go any further if this
529 	 * device is being used for swapping
530 	 */
531 	if (IS_SWAPVP(common_specvp(devvp))) {
532 		error = EBUSY;
533 		goto cleanup;
534 	}
535 
536 	vap.va_mask = AT_SIZE;
537 	if ((error = VOP_GETATTR(devvp, &vap, ATTR_COMM, cr)) != 0) {
538 		cmn_err(CE_NOTE, "Cannot get attributes of the CD-ROM driver");
539 		goto cleanup;
540 	}
541 
542 	/*
543 	 * Make sure we have a nonzero size partition.
544 	 * The current version of the SD driver will *not* fail the open
545 	 * of such a partition so we have to check for it here.
546 	 */
547 	if (vap.va_size == 0) {
548 		error = ENXIO;
549 		goto cleanup;
550 	}
551 
552 	/*
553 	 * Init a new hsfs structure.
554 	 */
555 	fsp = kmem_zalloc(sizeof (*fsp), KM_SLEEP);
556 
557 	/* hardwire perms, uid, gid */
558 	fsp->hsfs_vol.vol_uid = hsfs_default_uid;
559 	fsp->hsfs_vol.vol_gid =  hsfs_default_gid;
560 	fsp->hsfs_vol.vol_prot = hsfs_default_mode;
561 
562 	/*
563 	 * Look for a Standard File Structure Volume Descriptor,
564 	 * of which there must be at least one.
565 	 * If found, check for volume size consistency.
566 	 *
567 	 * XXX - va_size may someday not be large enough to do this correctly.
568 	 */
569 	error = hs_findhsvol(fsp, devvp, &fsp->hsfs_vol);
570 	if (error == EINVAL) /* not in hs format, try iso 9660 format */
571 		error = hs_findisovol(fsp, devvp, &fsp->hsfs_vol);
572 
573 	if (error)
574 		goto cleanup;
575 
576 	/*
577 	 * Generate a file system ID from the CD-ROM,
578 	 * and check it for uniqueness.
579 	 *
580 	 * What we are aiming for is some chance of integrity
581 	 * across disk change.  That is, if a client has an fhandle,
582 	 * it will be valid as long as the same disk is mounted.
583 	 */
584 	fsid = compute_cdrom_id(fsp, devvp);
585 
586 	mutex_enter(&hs_mounttab_lock);
587 
588 	if (fsid == 0 || fsid == -1) {
589 		uniqtime(&tv);
590 		fsid = tv.tv_sec;
591 	} else	/* make sure that the fsid is unique */
592 		for (tsp = hs_mounttab; tsp != NULL; tsp = tsp->hsfs_next) {
593 			if (fsid == tsp->hsfs_vfs->vfs_fsid.val[0]) {
594 				uniqtime(&tv);
595 				fsid = tv.tv_sec;
596 				break;
597 			}
598 		}
599 
600 	fsp->hsfs_next = hs_mounttab;
601 	hs_mounttab = fsp;
602 
603 	fsp->hsfs_devvp = devvp;
604 	fsp->hsfs_vfs = vfsp;
605 	fsp->hsfs_fsmnt = kmem_alloc(strlen(path) + 1, KM_SLEEP);
606 	(void) strcpy(fsp->hsfs_fsmnt, path);
607 
608 	mutex_init(&fsp->hsfs_free_lock, NULL, MUTEX_DEFAULT, NULL);
609 	rw_init(&fsp->hsfs_hash_lock, NULL, RW_DEFAULT, NULL);
610 
611 	vfsp->vfs_data = (caddr_t)fsp;
612 	vfsp->vfs_dev = dev;
613 	vfsp->vfs_fstype = hsfsfstype;
614 	vfsp->vfs_bsize = fsp->hsfs_vol.lbn_size; /* %% */
615 	vfsp->vfs_fsid.val[0] = fsid;
616 	vfsp->vfs_fsid.val[1] =  hsfsfstype;
617 
618 	/*
619 	 * If the root directory does not appear to be
620 	 * valid, use what it points to as "." instead.
621 	 * Some Defense Mapping Agency disks are non-conformant
622 	 * in this way.
623 	 */
624 	if (!hsfs_valid_dir(&fsp->hsfs_vol.root_dir)) {
625 		hs_log_bogus_disk_warning(fsp, HSFS_ERR_BAD_ROOT_DIR, 0);
626 		if (hs_remakenode(fsp->hsfs_vol.root_dir.ext_lbn,
627 			    (uint_t)0, vfsp, &fsp->hsfs_rootvp)) {
628 			error = EINVAL;
629 			goto cleanup;
630 		}
631 	} else {
632 		fsp->hsfs_rootvp = hs_makenode(&fsp->hsfs_vol.root_dir,
633 			fsp->hsfs_vol.root_dir.ext_lbn, 0, vfsp);
634 	}
635 
636 	/* mark vnode as VROOT */
637 	fsp->hsfs_rootvp->v_flag |= VROOT;
638 
639 	/* Here we take care of some special case stuff for mountroot */
640 	if (isroot) {
641 		fsp->hsfs_rootvp->v_rdev = devvp->v_rdev;
642 		rootvp = fsp->hsfs_rootvp;
643 	}
644 
645 	/* XXX - ignore the path table for now */
646 	fsp->hsfs_ptbl = NULL;
647 	hp = VTOH(fsp->hsfs_rootvp);
648 	hp->hs_ptbl_idx = NULL;
649 
650 	if (use_rrip)
651 		hs_check_root_dirent(fsp->hsfs_rootvp, &(hp->hs_dirent));
652 
653 	fsp->hsfs_namemax = IS_RRIP_IMPLEMENTED(fsp)
654 					? RRIP_FILE_NAMELEN
655 					: ISO_FILE_NAMELEN;
656 	/*
657 	 * if RRIP, don't copy NOMAPLCASE or NOTRAILDOT to hsfs_flags
658 	 */
659 	if (IS_RRIP_IMPLEMENTED(fsp))
660 		mount_flags &= ~(HSFSMNT_NOMAPLCASE | HSFSMNT_NOTRAILDOT);
661 
662 	fsp->hsfs_flags = mount_flags;
663 
664 	/* set the magic word */
665 	fsp->hsfs_magic = HSFS_MAGIC;
666 	mutex_exit(&hs_mounttab_lock);
667 
668 	return (0);
669 
670 cleanup:
671 	(void) VOP_CLOSE(devvp, FREAD, 1, (offset_t)0, cr);
672 	VN_RELE(devvp);
673 	if (fsp)
674 		kmem_free(fsp, sizeof (*fsp));
675 	return (error);
676 }
677 
678 /*
679  * hs_findhsvol()
680  *
681  * Locate the Standard File Structure Volume Descriptor and
682  * parse it into an hs_volume structure.
683  *
684  * XXX - May someday want to look for Coded Character Set FSVD, too.
685  */
686 static int
687 hs_findhsvol(struct hsfs *fsp, struct vnode *vp, struct hs_volume *hvp)
688 {
689 	struct buf *secbp;
690 	int i;
691 	uchar_t *volp;
692 	int error;
693 	uint_t secno;
694 
695 	secno = hs_findvoldesc(vp->v_rdev, HS_VOLDESC_SEC);
696 	secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
697 	error = geterror(secbp);
698 
699 	if (error != 0) {
700 		cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)", error);
701 		brelse(secbp);
702 		return (error);
703 	}
704 
705 	volp = (uchar_t *)secbp->b_un.b_addr;
706 
707 	while (HSV_DESC_TYPE(volp) != VD_EOV) {
708 		for (i = 0; i < HSV_ID_STRLEN; i++)
709 			if (HSV_STD_ID(volp)[i] != HSV_ID_STRING[i])
710 				goto cantfind;
711 		if (HSV_STD_VER(volp) != HSV_ID_VER)
712 			goto cantfind;
713 		switch (HSV_DESC_TYPE(volp)) {
714 		case VD_SFS:
715 			/* Standard File Structure */
716 			fsp->hsfs_vol_type = HS_VOL_TYPE_HS;
717 			error = hs_parsehsvol(fsp, volp, hvp);
718 			brelse(secbp);
719 			return (error);
720 
721 		case VD_CCFS:
722 			/* Coded Character File Structure */
723 		case VD_BOOT:
724 		case VD_UNSPEC:
725 		case VD_EOV:
726 			break;
727 		}
728 		brelse(secbp);
729 		++secno;
730 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
731 
732 		error = geterror(secbp);
733 
734 		if (error != 0) {
735 			cmn_err(CE_NOTE, "hs_findhsvol: bread: error=(%d)",
736 				error);
737 			brelse(secbp);
738 			return (error);
739 		}
740 
741 		volp = (uchar_t *)secbp->b_un.b_addr;
742 	}
743 cantfind:
744 	brelse(secbp);
745 	return (EINVAL);
746 }
747 
748 /*
749  * hs_parsehsvol
750  *
751  * Parse the Standard File Structure Volume Descriptor into
752  * an hs_volume structure.  We can't just bcopy it into the
753  * structure because of byte-ordering problems.
754  *
755  */
756 static int
757 hs_parsehsvol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
758 {
759 	hvp->vol_size = HSV_VOL_SIZE(volp);
760 	hvp->lbn_size = HSV_BLK_SIZE(volp);
761 	if (hvp->lbn_size == 0) {
762 		cmn_err(CE_NOTE, "hs_parsehsvol: logical block size in the "
763 			"SFSVD is zero");
764 		return (EINVAL);
765 	}
766 	hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
767 	hvp->lbn_secshift = ffs((long)howmany(HS_SECTOR_SIZE,
768 				(int)hvp->lbn_size)) - 1;
769 	hvp->lbn_maxoffset = hvp->lbn_size - 1;
770 	hs_parse_longdate(HSV_cre_date(volp), &hvp->cre_date);
771 	hs_parse_longdate(HSV_mod_date(volp), &hvp->mod_date);
772 	hvp->file_struct_ver = HSV_FILE_STRUCT_VER(volp);
773 	hvp->ptbl_len = HSV_PTBL_SIZE(volp);
774 	hvp->vol_set_size = (ushort_t)HSV_SET_SIZE(volp);
775 	hvp->vol_set_seq = (ushort_t)HSV_SET_SEQ(volp);
776 #if defined(_LITTLE_ENDIAN)
777 	hvp->ptbl_lbn = HSV_PTBL_MAN_LS(volp);
778 #else
779 	hvp->ptbl_lbn = HSV_PTBL_MAN_MS(volp);
780 #endif
781 	hs_copylabel(hvp, HSV_VOL_ID(volp));
782 
783 	/*
784 	 * Make sure that lbn_size is a power of two and otherwise valid.
785 	 */
786 	if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
787 		cmn_err(CE_NOTE,
788 			"hsfs: %d-byte logical block size not supported",
789 			hvp->lbn_size);
790 		return (EINVAL);
791 	}
792 	return (hs_parsedir(fsp, HSV_ROOT_DIR(volp), &hvp->root_dir,
793 			(char *)NULL, (int *)NULL));
794 }
795 
796 /*
797  * hs_findisovol()
798  *
799  * Locate the Primary Volume Descriptor
800  * parse it into an hs_volume structure.
801  *
802  * XXX - Supplementary, Partition not yet done
803  */
804 static int
805 hs_findisovol(struct hsfs *fsp, struct vnode *vp,
806     struct hs_volume *hvp)
807 {
808 	struct buf *secbp;
809 	int i;
810 	uchar_t *volp;
811 	int error;
812 	uint_t secno;
813 	int foundpvd = 0;
814 
815 	secno = hs_findvoldesc(vp->v_rdev, ISO_VOLDESC_SEC);
816 	secbp = bread(vp->v_rdev, secno * 4, ISO_SECTOR_SIZE);
817 	error = geterror(secbp);
818 
819 	if (error != 0) {
820 		cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)", error);
821 		brelse(secbp);
822 		return (error);
823 	}
824 
825 	volp = (uchar_t *)secbp->b_un.b_addr;
826 
827 	while ((enum iso_voldesc_type) ISO_DESC_TYPE(volp) != ISO_VD_EOV) {
828 		for (i = 0; i < ISO_ID_STRLEN; i++)
829 			if (ISO_STD_ID(volp)[i] != ISO_ID_STRING[i])
830 				goto cantfind;
831 		if (ISO_STD_VER(volp) != ISO_ID_VER)
832 			goto cantfind;
833 		switch (ISO_DESC_TYPE(volp)) {
834 		case ISO_VD_PVD:
835 			/* Standard File Structure */
836 			if (foundpvd != 1) {
837 				fsp->hsfs_vol_type = HS_VOL_TYPE_ISO;
838 				if (error = hs_parseisovol(fsp, volp, hvp)) {
839 					brelse(secbp);
840 					return (error);
841 				}
842 				foundpvd = 1;
843 			}
844 			break;
845 		case ISO_VD_SVD:
846 			/* Supplementary Volume Descriptor */
847 			break;
848 		case ISO_VD_BOOT:
849 			break;
850 		case ISO_VD_VPD:
851 			/* currently cannot handle partition */
852 			break;
853 		case VD_EOV:
854 			break;
855 		}
856 		brelse(secbp);
857 		++secno;
858 		secbp = bread(vp->v_rdev, secno * 4, HS_SECTOR_SIZE);
859 		error = geterror(secbp);
860 
861 		if (error != 0) {
862 			cmn_err(CE_NOTE, "hs_findisovol: bread: error=(%d)",
863 				    error);
864 			brelse(secbp);
865 			return (error);
866 		}
867 
868 		volp = (uchar_t *)secbp->b_un.b_addr;
869 	}
870 	if (foundpvd) {
871 		brelse(secbp);
872 		return (0);
873 	}
874 cantfind:
875 	brelse(secbp);
876 	return (EINVAL);
877 }
878 /*
879  * hs_parseisovol
880  *
881  * Parse the Primary Volume Descriptor into an hs_volume structure.
882  *
883  */
884 static int
885 hs_parseisovol(struct hsfs *fsp, uchar_t *volp, struct hs_volume *hvp)
886 {
887 	hvp->vol_size = ISO_VOL_SIZE(volp);
888 	hvp->lbn_size = ISO_BLK_SIZE(volp);
889 	if (hvp->lbn_size == 0) {
890 		cmn_err(CE_NOTE, "hs_parseisovol: logical block size in the "
891 			"PVD is zero");
892 		return (EINVAL);
893 	}
894 	hvp->lbn_shift = ffs((long)hvp->lbn_size) - 1;
895 	hvp->lbn_secshift = ffs((long)howmany(ISO_SECTOR_SIZE,
896 				(int)hvp->lbn_size)) - 1;
897 	hvp->lbn_maxoffset = hvp->lbn_size - 1;
898 	hs_parse_longdate(ISO_cre_date(volp), &hvp->cre_date);
899 	hs_parse_longdate(ISO_mod_date(volp), &hvp->mod_date);
900 	hvp->file_struct_ver = ISO_FILE_STRUCT_VER(volp);
901 	hvp->ptbl_len = ISO_PTBL_SIZE(volp);
902 	hvp->vol_set_size = (ushort_t)ISO_SET_SIZE(volp);
903 	hvp->vol_set_seq = (ushort_t)ISO_SET_SEQ(volp);
904 #if defined(_LITTLE_ENDIAN)
905 	hvp->ptbl_lbn = ISO_PTBL_MAN_LS(volp);
906 #else
907 	hvp->ptbl_lbn = ISO_PTBL_MAN_MS(volp);
908 #endif
909 	hs_copylabel(hvp, ISO_VOL_ID(volp));
910 
911 	/*
912 	 * Make sure that lbn_size is a power of two and otherwise valid.
913 	 */
914 	if (hvp->lbn_size & ~(1 << hvp->lbn_shift)) {
915 		cmn_err(CE_NOTE,
916 			"hsfs: %d-byte logical block size not supported",
917 			hvp->lbn_size);
918 		return (EINVAL);
919 	}
920 	return (hs_parsedir(fsp, ISO_ROOT_DIR(volp), &hvp->root_dir,
921 			(char *)NULL, (int *)NULL));
922 }
923 
924 /*
925  * Common code for mount and umount.
926  * Check that the user's argument is a reasonable
927  * thing on which to mount, and return the device number if so.
928  */
929 static int
930 hs_getmdev(struct vfs *vfsp, char *fspec, int flags, dev_t *pdev, mode_t *mode,
931     cred_t *cr)
932 {
933 	int error;
934 	struct vnode *vp;
935 	struct vattr vap;
936 	dev_t dev;
937 
938 	/*
939 	 * Get the device to be mounted
940 	 */
941 	error = lookupname(fspec, (flags & MS_SYSSPACE) ?
942 	    UIO_SYSSPACE : UIO_USERSPACE, FOLLOW, NULLVPP, &vp);
943 	if (error) {
944 		if (error == ENOENT) {
945 			return (ENODEV);	/* needs translation */
946 		}
947 		return (error);
948 	}
949 	if (vp->v_type != VBLK) {
950 		VN_RELE(vp);
951 		return (ENOTBLK);
952 	}
953 	/*
954 	 * Can we read from the device?
955 	 */
956 	if ((error = VOP_ACCESS(vp, VREAD, 0, cr)) != 0 ||
957 	    (error = secpolicy_spec_open(cr, vp, FREAD)) != 0) {
958 		VN_RELE(vp);
959 		return (error);
960 	}
961 
962 	vap.va_mask = AT_MODE;		/* get protection mode */
963 	(void) VOP_GETATTR(vp, &vap, 0, CRED());
964 	*mode = vap.va_mode;
965 
966 	dev = *pdev = vp->v_rdev;
967 	VN_RELE(vp);
968 
969 	/*
970 	 * Ensure that this device isn't already mounted,
971 	 * unless this is a REMOUNT request or we are told to suppress
972 	 * mount checks.
973 	 */
974 	if ((flags & MS_NOCHECK) == 0) {
975 		if (vfs_devmounting(dev, vfsp))
976 			return (EBUSY);
977 		if (vfs_devismounted(dev) && !(flags & MS_REMOUNT))
978 			return (EBUSY);
979 	}
980 
981 	if (getmajor(*pdev) >= devcnt)
982 		return (ENXIO);
983 	return (0);
984 }
985 
986 static void
987 hs_copylabel(struct hs_volume *hvp, unsigned char *label)
988 {
989 	/* cdrom volid is at most 32 bytes */
990 	bcopy(label, hvp->vol_id, 32);
991 	hvp->vol_id[31] = NULL;
992 }
993 
994 /*
995  * Mount root file system.
996  * "why" is ROOT_INIT on initial call, ROOT_REMOUNT if called to
997  * remount the root file system, and ROOT_UNMOUNT if called to
998  * unmount the root (e.g., as part of a system shutdown).
999  *
1000  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
1001  * operation, goes along with auto-configuration.  A mechanism should be
1002  * provided by which machine-INdependent code in the kernel can say "get me the
1003  * right root file system" and "get me the right initial swap area", and have
1004  * that done in what may well be a machine-dependent fashion.
1005  * Unfortunately, it is also file-system-type dependent (NFS gets it via
1006  * bootparams calls, UFS gets it from various and sundry machine-dependent
1007  * mechanisms, as SPECFS does for swap).
1008  */
1009 static int
1010 hsfs_mountroot(struct vfs *vfsp, enum whymountroot why)
1011 {
1012 	int error;
1013 	struct hsfs *fsp;
1014 	struct hs_volume *fvolp;
1015 	static int hsfsrootdone = 0;
1016 	dev_t rootdev;
1017 	mode_t mode = 0;
1018 
1019 	if (why == ROOT_INIT) {
1020 		if (hsfsrootdone++)
1021 			return (EBUSY);
1022 		rootdev = getrootdev();
1023 		if (rootdev == (dev_t)NODEV)
1024 			return (ENODEV);
1025 		vfsp->vfs_dev = rootdev;
1026 		vfsp->vfs_flag |= VFS_RDONLY;
1027 	} else if (why == ROOT_REMOUNT) {
1028 		cmn_err(CE_NOTE, "hsfs_mountroot: ROOT_REMOUNT");
1029 		return (0);
1030 	} else if (why == ROOT_UNMOUNT) {
1031 		return (0);
1032 	}
1033 	error = vfs_lock(vfsp);
1034 	if (error) {
1035 		cmn_err(CE_NOTE, "hsfs_mountroot: couldn't get vfs_lock");
1036 		return (error);
1037 	}
1038 
1039 	error = hs_mountfs(vfsp, rootdev, "/", mode, 1, CRED(), 1);
1040 	/*
1041 	 * XXX - assumes root device is not indirect, because we don't set
1042 	 * rootvp.  Is rootvp used for anything?  If so, make another arg
1043 	 * to mountfs.
1044 	 */
1045 	if (error) {
1046 		vfs_unlock(vfsp);
1047 		if (rootvp) {
1048 			VN_RELE(rootvp);
1049 			rootvp = (struct vnode *)0;
1050 		}
1051 		return (error);
1052 	}
1053 	if (why == ROOT_INIT)
1054 		vfs_add((struct vnode *)0, vfsp,
1055 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
1056 	vfs_unlock(vfsp);
1057 	fsp = VFS_TO_HSFS(vfsp);
1058 	fvolp = &fsp->hsfs_vol;
1059 #ifdef HSFS_CLKSET
1060 	if (fvolp->cre_date.tv_sec == 0) {
1061 	    cmn_err(CE_NOTE, "hsfs_mountroot: cre_date.tv_sec == 0");
1062 	    if (fvolp->mod_date.tv_sec == 0) {
1063 		cmn_err(CE_NOTE, "hsfs_mountroot: mod_date.tv_sec == 0");
1064 		cmn_err(CE_NOTE, "hsfs_mountroot: clkset(-1L)");
1065 		clkset(-1L);
1066 	    } else
1067 		clkset(fvolp->mod_date.tv_sec);
1068 	} else
1069 	    clkset(fvolp->mod_date.tv_sec);
1070 #else	/* HSFS_CLKSET */
1071 	clkset(-1L);
1072 #endif	/* HSFS_CLKSET */
1073 	return (0);
1074 }
1075 
1076 /*
1077  * hs_findvoldesc()
1078  *
1079  * Return the sector where the volume descriptor lives.  This is
1080  * a fixed value for "normal" cd-rom's, but can change for
1081  * multisession cd's.
1082  *
1083  * desc_sec is the same for high-sierra and iso 9660 formats, why
1084  * there are two differnt #defines used in the code for this is
1085  * beyond me.  These are standards, cast in concrete, right?
1086  * To be general, however, this function supports passing in different
1087  * values.
1088  */
1089 static int
1090 hs_findvoldesc(dev_t rdev, int desc_sec)
1091 {
1092 	int secno;
1093 	int error;
1094 	int rval;	/* ignored */
1095 
1096 #ifdef CDROMREADOFFSET
1097 	/*
1098 	 * Issue the Read Offset ioctl directly to the
1099 	 * device. Ignore any errors and set starting
1100 	 * secno to the default, otherwise add the
1101 	 * VOLDESC sector number to the offset.
1102 	 */
1103 	error = cdev_ioctl(rdev, CDROMREADOFFSET, (intptr_t)&secno,
1104 	    FNATIVE|FKIOCTL|FREAD, CRED(), &rval);
1105 	if (error) {
1106 		secno = desc_sec;
1107 	} else {
1108 		secno += desc_sec;
1109 	}
1110 #else
1111 	secno = desc_sec;
1112 #endif
1113 
1114 	return (secno);
1115 }
1116