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