17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55a59a8b3Srsb  * Common Development and Distribution License (the "License").
65a59a8b3Srsb  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
2293239addSjohnlev  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
2489b43686SBayard Bell  * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
25*d9a54dd1SSebastien Roy  * Copyright (c) 2017 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/resource.h>
357c478bd9Sstevel@tonic-gate #include <sys/signal.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/user.h>
387c478bd9Sstevel@tonic-gate #include <sys/buf.h>
397c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
40aa59c4cbSrsb #include <sys/vfs_opreg.h>
417c478bd9Sstevel@tonic-gate #include <sys/stat.h>
427c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
437c478bd9Sstevel@tonic-gate #include <sys/mode.h>
447c478bd9Sstevel@tonic-gate #include <sys/proc.h>
457c478bd9Sstevel@tonic-gate #include <sys/disp.h>
467c478bd9Sstevel@tonic-gate #include <sys/file.h>
477c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
487c478bd9Sstevel@tonic-gate #include <sys/flock.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/uio.h>
517c478bd9Sstevel@tonic-gate #include <sys/dnlc.h>
527c478bd9Sstevel@tonic-gate #include <sys/conf.h>
537c478bd9Sstevel@tonic-gate #include <sys/errno.h>
547c478bd9Sstevel@tonic-gate #include <sys/mman.h>
557c478bd9Sstevel@tonic-gate #include <sys/fbuf.h>
567c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
577c478bd9Sstevel@tonic-gate #include <sys/debug.h>
587c478bd9Sstevel@tonic-gate #include <sys/vmsystm.h>
597c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
607c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
617c478bd9Sstevel@tonic-gate #include <sys/errno.h>
627c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
637c478bd9Sstevel@tonic-gate #include <sys/statvfs.h>
647c478bd9Sstevel@tonic-gate #include <sys/mount.h>
657c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
667c478bd9Sstevel@tonic-gate #include <sys/bootconf.h>
677c478bd9Sstevel@tonic-gate #include <sys/policy.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include <vm/hat.h>
707c478bd9Sstevel@tonic-gate #include <vm/page.h>
717c478bd9Sstevel@tonic-gate #include <vm/pvn.h>
727c478bd9Sstevel@tonic-gate #include <vm/as.h>
737c478bd9Sstevel@tonic-gate #include <vm/seg.h>
747c478bd9Sstevel@tonic-gate #include <vm/seg_map.h>
757c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
767c478bd9Sstevel@tonic-gate #include <vm/seg_vn.h>
777c478bd9Sstevel@tonic-gate #include <vm/rm.h>
787c478bd9Sstevel@tonic-gate #include <vm/page.h>
797c478bd9Sstevel@tonic-gate #include <sys/swap.h>
807c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <fs/fs_subr.h>
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
877c478bd9Sstevel@tonic-gate #include <sys/fs/udf_inode.h>
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate extern struct vnode *common_specvp(struct vnode *vp);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate extern kmutex_t ud_sync_busy;
937c478bd9Sstevel@tonic-gate static int32_t ud_mountfs(struct vfs *,
9493239addSjohnlev     enum whymountroot, dev_t, char *, struct cred *, int32_t);
957c478bd9Sstevel@tonic-gate static struct udf_vfs *ud_validate_and_fill_superblock(dev_t,
9693239addSjohnlev     int32_t, uint32_t);
977c478bd9Sstevel@tonic-gate void ud_destroy_fsp(struct udf_vfs *);
987c478bd9Sstevel@tonic-gate void ud_convert_to_superblock(struct udf_vfs *,
9993239addSjohnlev     struct log_vol_int_desc *);
1007c478bd9Sstevel@tonic-gate void ud_update_superblock(struct vfs *);
1017c478bd9Sstevel@tonic-gate int32_t ud_get_last_block(dev_t, daddr_t *);
1027c478bd9Sstevel@tonic-gate static int32_t ud_val_get_vat(struct udf_vfs *,
10393239addSjohnlev     dev_t, daddr_t, struct ud_map *);
1047c478bd9Sstevel@tonic-gate int32_t ud_read_sparing_tbls(struct udf_vfs *,
10593239addSjohnlev     dev_t, struct ud_map *, struct pmap_typ2 *);
1067c478bd9Sstevel@tonic-gate uint32_t ud_get_lbsize(dev_t, uint32_t *);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate static int32_t udf_mount(struct vfs *,
10993239addSjohnlev     struct vnode *, struct mounta *, struct cred *);
1107c478bd9Sstevel@tonic-gate static int32_t udf_unmount(struct vfs *, int, struct cred *);
1117c478bd9Sstevel@tonic-gate static int32_t udf_root(struct vfs *, struct vnode **);
1127c478bd9Sstevel@tonic-gate static int32_t udf_statvfs(struct vfs *, struct statvfs64 *);
1137c478bd9Sstevel@tonic-gate static int32_t udf_sync(struct vfs *, int16_t, struct cred *);
1147c478bd9Sstevel@tonic-gate static int32_t udf_vget(struct vfs *, struct vnode **, struct fid *);
1157c478bd9Sstevel@tonic-gate static int32_t udf_mountroot(struct vfs *vfsp, enum whymountroot);
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static int udfinit(int, char *);
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static mntopts_t udfs_mntopts;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static vfsdef_t vfw = {
1227c478bd9Sstevel@tonic-gate 	VFSDEF_VERSION,
1237c478bd9Sstevel@tonic-gate 	"udfs",
1247c478bd9Sstevel@tonic-gate 	udfinit,
125*d9a54dd1SSebastien Roy 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS|VSW_CANLOFI|VSW_MOUNTDEV,
1267c478bd9Sstevel@tonic-gate 	&udfs_mntopts
1277c478bd9Sstevel@tonic-gate };
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate static mntopts_t udfs_mntopts = {
1307c478bd9Sstevel@tonic-gate 	0,
1317c478bd9Sstevel@tonic-gate 	NULL
1327c478bd9Sstevel@tonic-gate };
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /*
1357c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
1367c478bd9Sstevel@tonic-gate  */
1377c478bd9Sstevel@tonic-gate extern struct mod_ops mod_fsops;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static struct modlfs modlfs = {
1407c478bd9Sstevel@tonic-gate 	&mod_fsops, "filesystem for UDFS", &vfw
1417c478bd9Sstevel@tonic-gate };
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
1447c478bd9Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
1457c478bd9Sstevel@tonic-gate };
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate int32_t udf_fstype = -1;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate int
_init()1507c478bd9Sstevel@tonic-gate _init()
1517c478bd9Sstevel@tonic-gate {
1527c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate int
_fini()1567c478bd9Sstevel@tonic-gate _fini()
1577c478bd9Sstevel@tonic-gate {
1587c478bd9Sstevel@tonic-gate 	return (EBUSY);
1597c478bd9Sstevel@tonic-gate }
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1627c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
1637c478bd9Sstevel@tonic-gate {
1647c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate /* -------------------- vfs routines -------------------- */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * XXX - this appears only to be used by the VM code to handle the case where
1727c478bd9Sstevel@tonic-gate  * UNIX is running off the mini-root.  That probably wants to be done
1737c478bd9Sstevel@tonic-gate  * differently.
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate struct vnode *rootvp;
1767c478bd9Sstevel@tonic-gate #ifndef	__lint
1777c478bd9Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("safe sharing", rootvp))
1787c478bd9Sstevel@tonic-gate #endif
1797c478bd9Sstevel@tonic-gate static int32_t
udf_mount(struct vfs * vfsp,struct vnode * mvp,struct mounta * uap,struct cred * cr)1807c478bd9Sstevel@tonic-gate udf_mount(struct vfs *vfsp, struct vnode *mvp,
181*d9a54dd1SSebastien Roy     struct mounta *uap, struct cred *cr)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	dev_t dev;
18493239addSjohnlev 	struct vnode *lvp = NULL;
18593239addSjohnlev 	struct vnode *svp = NULL;
1867c478bd9Sstevel@tonic-gate 	struct pathname dpn;
1877c478bd9Sstevel@tonic-gate 	int32_t error;
1887c478bd9Sstevel@tonic-gate 	enum whymountroot why;
1897c478bd9Sstevel@tonic-gate 	int oflag, aflag;
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	ud_printf("udf_mount\n");
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0) {
1947c478bd9Sstevel@tonic-gate 		return (error);
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
1987c478bd9Sstevel@tonic-gate 		return (ENOTDIR);
1997c478bd9Sstevel@tonic-gate 	}
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2027c478bd9Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
2037c478bd9Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
20493239addSjohnlev 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
20593239addSjohnlev 		mutex_exit(&mvp->v_lock);
20693239addSjohnlev 		return (EBUSY);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	if (error = pn_get(uap->dir, UIO_USERSPACE, &dpn)) {
2117c478bd9Sstevel@tonic-gate 		return (error);
2127c478bd9Sstevel@tonic-gate 	}
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate 	/*
21593239addSjohnlev 	 * Resolve path name of the file being mounted.
2167c478bd9Sstevel@tonic-gate 	 */
2177c478bd9Sstevel@tonic-gate 	if (error = lookupname(uap->spec, UIO_USERSPACE, FOLLOW, NULLVPP,
21893239addSjohnlev 	    &svp)) {
2197c478bd9Sstevel@tonic-gate 		pn_free(&dpn);
2207c478bd9Sstevel@tonic-gate 		return (error);
2217c478bd9Sstevel@tonic-gate 	}
22293239addSjohnlev 
22393239addSjohnlev 	error = vfs_get_lofi(vfsp, &lvp);
22493239addSjohnlev 
22593239addSjohnlev 	if (error > 0) {
22693239addSjohnlev 		if (error == ENOENT)
22793239addSjohnlev 			error = ENODEV;
2287c478bd9Sstevel@tonic-gate 		goto out;
22993239addSjohnlev 	} else if (error == 0) {
23093239addSjohnlev 		dev = lvp->v_rdev;
23193239addSjohnlev 	} else {
23293239addSjohnlev 		dev = svp->v_rdev;
23393239addSjohnlev 
23493239addSjohnlev 		if (svp->v_type != VBLK) {
23593239addSjohnlev 			error = ENOTBLK;
23693239addSjohnlev 			goto out;
23793239addSjohnlev 		}
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	/*
2417c478bd9Sstevel@tonic-gate 	 * Ensure that this device isn't already mounted,
2427c478bd9Sstevel@tonic-gate 	 * unless this is a REMOUNT request
2437c478bd9Sstevel@tonic-gate 	 */
2447c478bd9Sstevel@tonic-gate 	if (vfs_devmounting(dev, vfsp)) {
2457c478bd9Sstevel@tonic-gate 		error = EBUSY;
2467c478bd9Sstevel@tonic-gate 		goto out;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 	if (vfs_devismounted(dev)) {
2497c478bd9Sstevel@tonic-gate 		if (uap->flags & MS_REMOUNT) {
2507c478bd9Sstevel@tonic-gate 			why = ROOT_REMOUNT;
2517c478bd9Sstevel@tonic-gate 		} else {
2527c478bd9Sstevel@tonic-gate 			error = EBUSY;
2537c478bd9Sstevel@tonic-gate 			goto out;
2547c478bd9Sstevel@tonic-gate 		}
2557c478bd9Sstevel@tonic-gate 	} else {
2567c478bd9Sstevel@tonic-gate 		why = ROOT_INIT;
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt) {
2597c478bd9Sstevel@tonic-gate 		error = ENXIO;
2607c478bd9Sstevel@tonic-gate 		goto out;
2617c478bd9Sstevel@tonic-gate 	}
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	/*
2647c478bd9Sstevel@tonic-gate 	 * If the device is a tape, mount it read only
2657c478bd9Sstevel@tonic-gate 	 */
2667c478bd9Sstevel@tonic-gate 	if (devopsp[getmajor(dev)]->devo_cb_ops->cb_flag & D_TAPE) {
2677c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
2687c478bd9Sstevel@tonic-gate 	}
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
2717c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * Set mount options.
2767c478bd9Sstevel@tonic-gate 	 */
2777c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
2787c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	if (uap->flags & MS_NOSUID) {
2817c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
2827c478bd9Sstevel@tonic-gate 	}
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	/*
2857c478bd9Sstevel@tonic-gate 	 * Verify that the caller can open the device special file as
2867c478bd9Sstevel@tonic-gate 	 * required.  It is not until this moment that we know whether
2877c478bd9Sstevel@tonic-gate 	 * we're mounting "ro" or not.
2887c478bd9Sstevel@tonic-gate 	 */
2897c478bd9Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0) {
2907c478bd9Sstevel@tonic-gate 		oflag = FREAD;
2917c478bd9Sstevel@tonic-gate 		aflag = VREAD;
2927c478bd9Sstevel@tonic-gate 	} else {
2937c478bd9Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
2947c478bd9Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
2957c478bd9Sstevel@tonic-gate 	}
29693239addSjohnlev 
29793239addSjohnlev 	if (lvp == NULL &&
29893239addSjohnlev 	    (error = secpolicy_spec_open(cr, svp, oflag)) != 0)
29993239addSjohnlev 		goto out;
30093239addSjohnlev 
30193239addSjohnlev 	if ((error = VOP_ACCESS(svp, aflag, 0, cr, NULL)) != 0)
3027c478bd9Sstevel@tonic-gate 		goto out;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 	/*
3057c478bd9Sstevel@tonic-gate 	 * Mount the filesystem.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	error = ud_mountfs(vfsp, why, dev, dpn.pn_path, cr, 0);
3087c478bd9Sstevel@tonic-gate out:
30993239addSjohnlev 	VN_RELE(svp);
31093239addSjohnlev 	if (lvp != NULL)
31193239addSjohnlev 		VN_RELE(lvp);
3127c478bd9Sstevel@tonic-gate 	pn_free(&dpn);
3137c478bd9Sstevel@tonic-gate 	return (error);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * unmount the file system pointed
3207c478bd9Sstevel@tonic-gate  * by vfsp
3217c478bd9Sstevel@tonic-gate  */
3227c478bd9Sstevel@tonic-gate /* ARGSUSED */
3237c478bd9Sstevel@tonic-gate static int32_t
udf_unmount(struct vfs * vfsp,int fflag,struct cred * cr)3247c478bd9Sstevel@tonic-gate udf_unmount(struct vfs *vfsp, int fflag, struct cred *cr)
3257c478bd9Sstevel@tonic-gate {
3267c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
3277c478bd9Sstevel@tonic-gate 	struct vnode *bvp, *rvp;
3287c478bd9Sstevel@tonic-gate 	struct ud_inode *rip;
3297c478bd9Sstevel@tonic-gate 	int32_t flag;
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	ud_printf("udf_unmount\n");
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0) {
3347c478bd9Sstevel@tonic-gate 		return (EPERM);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	/*
3387c478bd9Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
3397c478bd9Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
3407c478bd9Sstevel@tonic-gate 	 */
3417c478bd9Sstevel@tonic-gate 	if (fflag & MS_FORCE)
3427c478bd9Sstevel@tonic-gate 		return (ENOTSUP);
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
3457c478bd9Sstevel@tonic-gate 	flag = !(udf_vfsp->udf_flags & UDF_FL_RDONLY);
3467c478bd9Sstevel@tonic-gate 	bvp = udf_vfsp->udf_devvp;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 	rvp = udf_vfsp->udf_root;
3497c478bd9Sstevel@tonic-gate 	ASSERT(rvp != NULL);
3507c478bd9Sstevel@tonic-gate 	rip = VTOI(rvp);
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	(void) ud_release_cache(udf_vfsp);
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	/* Flush all inodes except root */
3567c478bd9Sstevel@tonic-gate 	if (ud_iflush(vfsp) < 0) {
3577c478bd9Sstevel@tonic-gate 		return (EBUSY);
3587c478bd9Sstevel@tonic-gate 	}
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	rw_enter(&rip->i_contents, RW_WRITER);
3617c478bd9Sstevel@tonic-gate 	(void) ud_syncip(rip, B_INVAL, I_SYNC);
3627c478bd9Sstevel@tonic-gate 	rw_exit(&rip->i_contents);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	mutex_enter(&ud_sync_busy);
3657c478bd9Sstevel@tonic-gate 	if ((udf_vfsp->udf_flags & UDF_FL_RDONLY) == 0) {
3667c478bd9Sstevel@tonic-gate 		bflush(vfsp->vfs_dev);
3677c478bd9Sstevel@tonic-gate 		mutex_enter(&udf_vfsp->udf_lock);
3687c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_clean = UDF_CLEAN;
3697c478bd9Sstevel@tonic-gate 		mutex_exit(&udf_vfsp->udf_lock);
3707c478bd9Sstevel@tonic-gate 		ud_update_superblock(vfsp);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 	mutex_exit(&ud_sync_busy);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	mutex_destroy(&udf_vfsp->udf_lock);
3757c478bd9Sstevel@tonic-gate 	mutex_destroy(&udf_vfsp->udf_rename_lck);
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	ud_delcache(rip);
3787c478bd9Sstevel@tonic-gate 	ITIMES(rip);
3797c478bd9Sstevel@tonic-gate 	VN_RELE(rvp);
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	ud_destroy_fsp(udf_vfsp);
3827c478bd9Sstevel@tonic-gate 
383da6c28aaSamw 	(void) VOP_PUTPAGE(bvp, (offset_t)0, (uint32_t)0, B_INVAL, cr, NULL);
384da6c28aaSamw 	(void) VOP_CLOSE(bvp, flag, 1, (offset_t)0, cr, NULL);
3857c478bd9Sstevel@tonic-gate 
3867c478bd9Sstevel@tonic-gate 	(void) bfinval(vfsp->vfs_dev, 1);
3877c478bd9Sstevel@tonic-gate 	VN_RELE(bvp);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	return (0);
3917c478bd9Sstevel@tonic-gate }
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate /*
3957c478bd9Sstevel@tonic-gate  * Get the root vp for the
3967c478bd9Sstevel@tonic-gate  * file system
3977c478bd9Sstevel@tonic-gate  */
3987c478bd9Sstevel@tonic-gate static int32_t
udf_root(struct vfs * vfsp,struct vnode ** vpp)3997c478bd9Sstevel@tonic-gate udf_root(struct vfs *vfsp, struct vnode **vpp)
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
4027c478bd9Sstevel@tonic-gate 	struct vnode *vp;
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	ud_printf("udf_root\n");
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	ASSERT(udf_vfsp != NULL);
4097c478bd9Sstevel@tonic-gate 	ASSERT(udf_vfsp->udf_root != NULL);
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 	vp = udf_vfsp->udf_root;
4127c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
4137c478bd9Sstevel@tonic-gate 	*vpp = vp;
4147c478bd9Sstevel@tonic-gate 	return (0);
4157c478bd9Sstevel@tonic-gate }
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate /*
4197c478bd9Sstevel@tonic-gate  * Get file system statistics.
4207c478bd9Sstevel@tonic-gate  */
4217c478bd9Sstevel@tonic-gate static int32_t
udf_statvfs(struct vfs * vfsp,struct statvfs64 * sp)4227c478bd9Sstevel@tonic-gate udf_statvfs(struct vfs *vfsp, struct statvfs64 *sp)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
4257c478bd9Sstevel@tonic-gate 	struct ud_part *parts;
4267c478bd9Sstevel@tonic-gate 	dev32_t d32;
4277c478bd9Sstevel@tonic-gate 	int32_t index;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	ud_printf("udf_statvfs\n");
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
4327c478bd9Sstevel@tonic-gate 	(void) bzero(sp, sizeof (struct statvfs64));
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	mutex_enter(&udf_vfsp->udf_lock);
4357c478bd9Sstevel@tonic-gate 	sp->f_bsize = udf_vfsp->udf_lbsize;
4367c478bd9Sstevel@tonic-gate 	sp->f_frsize = udf_vfsp->udf_lbsize;
4377c478bd9Sstevel@tonic-gate 	sp->f_blocks = 0;
4387c478bd9Sstevel@tonic-gate 	sp->f_bfree = 0;
4397c478bd9Sstevel@tonic-gate 	parts = udf_vfsp->udf_parts;
4407c478bd9Sstevel@tonic-gate 	for (index = 0; index < udf_vfsp->udf_npart; index++) {
4417c478bd9Sstevel@tonic-gate 		sp->f_blocks += parts->udp_nblocks;
4427c478bd9Sstevel@tonic-gate 		sp->f_bfree += parts->udp_nfree;
4437c478bd9Sstevel@tonic-gate 		parts++;
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 	sp->f_bavail = sp->f_bfree;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	/*
4487c478bd9Sstevel@tonic-gate 	 * Since there are no real inodes allocated
4497c478bd9Sstevel@tonic-gate 	 * we will approximate
4507c478bd9Sstevel@tonic-gate 	 * each new file will occupy :
4517c478bd9Sstevel@tonic-gate 	 * 38(over head each dent) + MAXNAMLEN / 2 + inode_size(==block size)
4527c478bd9Sstevel@tonic-gate 	 */
4537c478bd9Sstevel@tonic-gate 	sp->f_ffree = sp->f_favail =
45493239addSjohnlev 	    (sp->f_bavail * sp->f_bsize) / (146 + sp->f_bsize);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	/*
4577c478bd9Sstevel@tonic-gate 	 * The total number of inodes is
4587c478bd9Sstevel@tonic-gate 	 * the sum of files + directories + free inodes
4597c478bd9Sstevel@tonic-gate 	 */
46093239addSjohnlev 	sp->f_files = sp->f_ffree + udf_vfsp->udf_nfiles + udf_vfsp->udf_ndirs;
4617c478bd9Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
4627c478bd9Sstevel@tonic-gate 	sp->f_fsid = d32;
4637c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
4647c478bd9Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
4657c478bd9Sstevel@tonic-gate 	sp->f_namemax = MAXNAMLEN;
4667c478bd9Sstevel@tonic-gate 	(void) strcpy(sp->f_fstr, udf_vfsp->udf_volid);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	mutex_exit(&udf_vfsp->udf_lock);
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	return (0);
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate /*
4757c478bd9Sstevel@tonic-gate  * Flush any pending I/O to file system vfsp.
4767c478bd9Sstevel@tonic-gate  * The ud_update() routine will only flush *all* udf files.
4777c478bd9Sstevel@tonic-gate  */
4787c478bd9Sstevel@tonic-gate /*ARGSUSED*/
4797c478bd9Sstevel@tonic-gate /* ARGSUSED */
4807c478bd9Sstevel@tonic-gate static int32_t
udf_sync(struct vfs * vfsp,int16_t flag,struct cred * cr)4817c478bd9Sstevel@tonic-gate udf_sync(struct vfs *vfsp, int16_t flag, struct cred *cr)
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	ud_printf("udf_sync\n");
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	ud_update(flag);
4867c478bd9Sstevel@tonic-gate 	return (0);
4877c478bd9Sstevel@tonic-gate }
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /* ARGSUSED */
4927c478bd9Sstevel@tonic-gate static int32_t
udf_vget(struct vfs * vfsp,struct vnode ** vpp,struct fid * fidp)493*d9a54dd1SSebastien Roy udf_vget(struct vfs *vfsp, struct vnode **vpp, struct fid *fidp)
4947c478bd9Sstevel@tonic-gate {
4957c478bd9Sstevel@tonic-gate 	int32_t error = 0;
4967c478bd9Sstevel@tonic-gate 	struct udf_fid *udfid;
4977c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
4987c478bd9Sstevel@tonic-gate 	struct ud_inode *ip;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	ud_printf("udf_vget\n");
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
5037c478bd9Sstevel@tonic-gate 	if (udf_vfsp == NULL) {
5047c478bd9Sstevel@tonic-gate 		*vpp = NULL;
5057c478bd9Sstevel@tonic-gate 		return (0);
5067c478bd9Sstevel@tonic-gate 	}
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate 	udfid = (struct udf_fid *)fidp;
5097c478bd9Sstevel@tonic-gate 	if ((error = ud_iget(vfsp, udfid->udfid_prn,
51093239addSjohnlev 	    udfid->udfid_icb_lbn, &ip, NULL, CRED())) != 0) {
5117c478bd9Sstevel@tonic-gate 		*vpp = NULL;
5127c478bd9Sstevel@tonic-gate 		return (error);
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
5167c478bd9Sstevel@tonic-gate 	if ((udfid->udfid_uinq_lo != (ip->i_uniqid & 0xffffffff)) ||
51793239addSjohnlev 	    (udfid->udfid_prn != ip->i_icb_prn)) {
5187c478bd9Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
5197c478bd9Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
5207c478bd9Sstevel@tonic-gate 		*vpp = NULL;
5217c478bd9Sstevel@tonic-gate 		return (EINVAL);
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	*vpp = ITOV(ip);
5267c478bd9Sstevel@tonic-gate 	return (0);
5277c478bd9Sstevel@tonic-gate }
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate  * Mount root file system.
5327c478bd9Sstevel@tonic-gate  * "why" is ROOT_INIT on initial call, ROOT_REMOUNT if called to
5337c478bd9Sstevel@tonic-gate  * remount the root file system, and ROOT_UNMOUNT if called to
5347c478bd9Sstevel@tonic-gate  * unmount the root (e.g., as part of a system shutdown).
5357c478bd9Sstevel@tonic-gate  *
5367c478bd9Sstevel@tonic-gate  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
5377c478bd9Sstevel@tonic-gate  * operation, goes along with auto-configuration.  A mechanism should be
5387c478bd9Sstevel@tonic-gate  * provided by which machine-INdependent code in the kernel can say "get me the
5397c478bd9Sstevel@tonic-gate  * right root file system" and "get me the right initial swap area", and have
5407c478bd9Sstevel@tonic-gate  * that done in what may well be a machine-dependent fashion.
5417c478bd9Sstevel@tonic-gate  * Unfortunately, it is also file-system-type dependent (NFS gets it via
5427c478bd9Sstevel@tonic-gate  * bootparams calls, UFS gets it from various and sundry machine-dependent
5437c478bd9Sstevel@tonic-gate  * mechanisms, as SPECFS does for swap).
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate /* ARGSUSED */
5467c478bd9Sstevel@tonic-gate static int32_t
udf_mountroot(struct vfs * vfsp,enum whymountroot why)5477c478bd9Sstevel@tonic-gate udf_mountroot(struct vfs *vfsp, enum whymountroot why)
5487c478bd9Sstevel@tonic-gate {
5497c478bd9Sstevel@tonic-gate 	dev_t rootdev;
5507c478bd9Sstevel@tonic-gate 	static int32_t udf_rootdone = 0;
5517c478bd9Sstevel@tonic-gate 	struct vnode *vp = NULL;
5527c478bd9Sstevel@tonic-gate 	int32_t ovflags, error;
5537c478bd9Sstevel@tonic-gate 	ud_printf("udf_mountroot\n");
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (why == ROOT_INIT) {
5567c478bd9Sstevel@tonic-gate 		if (udf_rootdone++) {
5577c478bd9Sstevel@tonic-gate 			return (EBUSY);
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		rootdev = getrootdev();
5607c478bd9Sstevel@tonic-gate 		if (rootdev == (dev_t)NODEV) {
5617c478bd9Sstevel@tonic-gate 			return (ENODEV);
5627c478bd9Sstevel@tonic-gate 		}
5637c478bd9Sstevel@tonic-gate 		vfsp->vfs_dev = rootdev;
5647c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
5657c478bd9Sstevel@tonic-gate 	} else if (why == ROOT_REMOUNT) {
5667c478bd9Sstevel@tonic-gate 		vp = ((struct udf_vfs *)vfsp->vfs_data)->udf_devvp;
5677c478bd9Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(vfsp, 0);
5687c478bd9Sstevel@tonic-gate 		vp = common_specvp(vp);
5697c478bd9Sstevel@tonic-gate 		(void) VOP_PUTPAGE(vp, (offset_t)0,
57093239addSjohnlev 		    (uint32_t)0, B_INVAL, CRED(), NULL);
5717c478bd9Sstevel@tonic-gate 		binval(vfsp->vfs_dev);
5727c478bd9Sstevel@tonic-gate 
5737c478bd9Sstevel@tonic-gate 		ovflags = vfsp->vfs_flag;
5747c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag &= ~VFS_RDONLY;
5757c478bd9Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_REMOUNT;
5767c478bd9Sstevel@tonic-gate 		rootdev = vfsp->vfs_dev;
5777c478bd9Sstevel@tonic-gate 	} else if (why == ROOT_UNMOUNT) {
5787c478bd9Sstevel@tonic-gate 		ud_update(0);
5797c478bd9Sstevel@tonic-gate 		vp = ((struct udf_vfs *)vfsp->vfs_data)->udf_devvp;
5807c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, FREAD|FWRITE, 1,
58193239addSjohnlev 		    (offset_t)0, CRED(), NULL);
5827c478bd9Sstevel@tonic-gate 		return (0);
5837c478bd9Sstevel@tonic-gate 	}
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if ((error = vfs_lock(vfsp)) != 0) {
5867c478bd9Sstevel@tonic-gate 		return (error);
5877c478bd9Sstevel@tonic-gate 	}
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	error = ud_mountfs(vfsp, why, rootdev, "/", CRED(), 1);
5907c478bd9Sstevel@tonic-gate 	if (error) {
5917c478bd9Sstevel@tonic-gate 		vfs_unlock(vfsp);
5927c478bd9Sstevel@tonic-gate 		if (why == ROOT_REMOUNT) {
5937c478bd9Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 		if (rootvp) {
5967c478bd9Sstevel@tonic-gate 			VN_RELE(rootvp);
5977c478bd9Sstevel@tonic-gate 			rootvp = (struct vnode *)0;
5987c478bd9Sstevel@tonic-gate 		}
5997c478bd9Sstevel@tonic-gate 		return (error);
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if (why == ROOT_INIT) {
6037c478bd9Sstevel@tonic-gate 		vfs_add((struct vnode *)0, vfsp,
60493239addSjohnlev 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 	vfs_unlock(vfsp);
6077c478bd9Sstevel@tonic-gate 	return (0);
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate /* ------------------------- local routines ------------------------- */
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate static int32_t
ud_mountfs(struct vfs * vfsp,enum whymountroot why,dev_t dev,char * name,struct cred * cr,int32_t isroot)615*d9a54dd1SSebastien Roy ud_mountfs(struct vfs *vfsp, enum whymountroot why, dev_t dev, char *name,
616*d9a54dd1SSebastien Roy     struct cred *cr, int32_t isroot)
6177c478bd9Sstevel@tonic-gate {
6187c478bd9Sstevel@tonic-gate 	struct vnode *devvp = NULL;
6197c478bd9Sstevel@tonic-gate 	int32_t error = 0;
6207c478bd9Sstevel@tonic-gate 	int32_t needclose = 0;
6217c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp = NULL;
6227c478bd9Sstevel@tonic-gate 	struct log_vol_int_desc *lvid;
6237c478bd9Sstevel@tonic-gate 	struct ud_inode *rip = NULL;
6247c478bd9Sstevel@tonic-gate 	struct vnode *rvp = NULL;
6257c478bd9Sstevel@tonic-gate 	int32_t i, lbsize;
6267c478bd9Sstevel@tonic-gate 	uint32_t avd_loc;
6277c478bd9Sstevel@tonic-gate 	struct ud_map *map;
6287c478bd9Sstevel@tonic-gate 	int32_t	desc_len;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	ud_printf("ud_mountfs\n");
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 	if (why == ROOT_INIT) {
6337c478bd9Sstevel@tonic-gate 		/*
6347c478bd9Sstevel@tonic-gate 		 * Open the device.
6357c478bd9Sstevel@tonic-gate 		 */
6367c478bd9Sstevel@tonic-gate 		devvp = makespecvp(dev, VBLK);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		/*
6397c478bd9Sstevel@tonic-gate 		 * Open block device mounted on.
6407c478bd9Sstevel@tonic-gate 		 * When bio is fixed for vnodes this can all be vnode
6417c478bd9Sstevel@tonic-gate 		 * operations.
6427c478bd9Sstevel@tonic-gate 		 */
6437c478bd9Sstevel@tonic-gate 		error = VOP_OPEN(&devvp,
644da6c28aaSamw 		    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
645da6c28aaSamw 		    cr, NULL);
6467c478bd9Sstevel@tonic-gate 		if (error) {
6477c478bd9Sstevel@tonic-gate 			goto out;
6487c478bd9Sstevel@tonic-gate 		}
6497c478bd9Sstevel@tonic-gate 		needclose = 1;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		/*
6527c478bd9Sstevel@tonic-gate 		 * Refuse to go any further if this
6537c478bd9Sstevel@tonic-gate 		 * device is being used for swapping.
6547c478bd9Sstevel@tonic-gate 		 */
6557c478bd9Sstevel@tonic-gate 		if (IS_SWAPVP(devvp)) {
6567c478bd9Sstevel@tonic-gate 			error = EBUSY;
6577c478bd9Sstevel@tonic-gate 			goto out;
6587c478bd9Sstevel@tonic-gate 		}
6597c478bd9Sstevel@tonic-gate 	}
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/*
6627c478bd9Sstevel@tonic-gate 	 * check for dev already mounted on
6637c478bd9Sstevel@tonic-gate 	 */
6647c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_REMOUNT) {
6657c478bd9Sstevel@tonic-gate 		struct tag *ttag;
6667c478bd9Sstevel@tonic-gate 		int32_t index, count;
6677c478bd9Sstevel@tonic-gate 		struct buf *tpt = 0;
6687c478bd9Sstevel@tonic-gate 		caddr_t addr;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 		/* cannot remount to RDONLY */
6727c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_flag & VFS_RDONLY) {
6737c478bd9Sstevel@tonic-gate 			return (EINVAL);
6747c478bd9Sstevel@tonic-gate 		}
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 		if (vfsp->vfs_dev != dev) {
6777c478bd9Sstevel@tonic-gate 			return (EINVAL);
6787c478bd9Sstevel@tonic-gate 		}
6797c478bd9Sstevel@tonic-gate 
6807c478bd9Sstevel@tonic-gate 		udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
6817c478bd9Sstevel@tonic-gate 		devvp = udf_vfsp->udf_devvp;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		/*
6847c478bd9Sstevel@tonic-gate 		 * fsck may have altered the file system; discard
6857c478bd9Sstevel@tonic-gate 		 * as much incore data as possible.  Don't flush
6867c478bd9Sstevel@tonic-gate 		 * if this is a rw to rw remount; it's just resetting
6877c478bd9Sstevel@tonic-gate 		 * the options.
6887c478bd9Sstevel@tonic-gate 		 */
6897c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_flags & UDF_FL_RDONLY) {
6907c478bd9Sstevel@tonic-gate 			(void) dnlc_purge_vfsp(vfsp, 0);
6917c478bd9Sstevel@tonic-gate 			(void) VOP_PUTPAGE(devvp, (offset_t)0, (uint_t)0,
69293239addSjohnlev 			    B_INVAL, CRED(), NULL);
6937c478bd9Sstevel@tonic-gate 			(void) ud_iflush(vfsp);
6947c478bd9Sstevel@tonic-gate 			bflush(dev);
6957c478bd9Sstevel@tonic-gate 			binval(dev);
6967c478bd9Sstevel@tonic-gate 		}
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 		/*
6997c478bd9Sstevel@tonic-gate 		 * We could read UDF1.50 and write UDF1.50 only
7007c478bd9Sstevel@tonic-gate 		 * disallow mount of any highier version
7017c478bd9Sstevel@tonic-gate 		 */
7027c478bd9Sstevel@tonic-gate 		if ((udf_vfsp->udf_miread > UDF_150) ||
70393239addSjohnlev 		    (udf_vfsp->udf_miwrite > UDF_150)) {
7047c478bd9Sstevel@tonic-gate 			error = EINVAL;
7057c478bd9Sstevel@tonic-gate 			goto remountout;
7067c478bd9Sstevel@tonic-gate 		}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		/*
7097c478bd9Sstevel@tonic-gate 		 * read/write to read/write; all done
7107c478bd9Sstevel@tonic-gate 		 */
7117c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_flags & UDF_FL_RW) {
7127c478bd9Sstevel@tonic-gate 			goto remountout;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		/*
7167c478bd9Sstevel@tonic-gate 		 * Does the media type allow a writable mount
7177c478bd9Sstevel@tonic-gate 		 */
7187c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_mtype != UDF_MT_OW) {
7197c478bd9Sstevel@tonic-gate 			error = EINVAL;
7207c478bd9Sstevel@tonic-gate 			goto remountout;
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 		/*
7247c478bd9Sstevel@tonic-gate 		 * Read the metadata
7257c478bd9Sstevel@tonic-gate 		 * and check if it is possible to
7267c478bd9Sstevel@tonic-gate 		 * mount in rw mode
7277c478bd9Sstevel@tonic-gate 		 */
7287c478bd9Sstevel@tonic-gate 		tpt = ud_bread(vfsp->vfs_dev,
72993239addSjohnlev 		    udf_vfsp->udf_iseq_loc << udf_vfsp->udf_l2d_shift,
73093239addSjohnlev 		    udf_vfsp->udf_iseq_len);
7317c478bd9Sstevel@tonic-gate 		if (tpt->b_flags & B_ERROR) {
7327c478bd9Sstevel@tonic-gate 			error = EIO;
7337c478bd9Sstevel@tonic-gate 			goto remountout;
7347c478bd9Sstevel@tonic-gate 		}
7357c478bd9Sstevel@tonic-gate 		count = udf_vfsp->udf_iseq_len / DEV_BSIZE;
7367c478bd9Sstevel@tonic-gate 		addr = tpt->b_un.b_addr;
7377c478bd9Sstevel@tonic-gate 		for (index = 0; index < count; index ++) {
7387c478bd9Sstevel@tonic-gate 			ttag = (struct tag *)(addr + index * DEV_BSIZE);
7397c478bd9Sstevel@tonic-gate 			desc_len = udf_vfsp->udf_iseq_len - (index * DEV_BSIZE);
7407c478bd9Sstevel@tonic-gate 			if (ud_verify_tag_and_desc(ttag, UD_LOG_VOL_INT,
7417c478bd9Sstevel@tonic-gate 			    udf_vfsp->udf_iseq_loc +
7427c478bd9Sstevel@tonic-gate 			    (index >> udf_vfsp->udf_l2d_shift),
7437c478bd9Sstevel@tonic-gate 			    1, desc_len) == 0) {
7447c478bd9Sstevel@tonic-gate 				struct log_vol_int_desc *lvid;
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 				lvid = (struct log_vol_int_desc *)ttag;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 				if (SWAP_32(lvid->lvid_int_type) !=
74993239addSjohnlev 				    LOG_VOL_CLOSE_INT) {
7507c478bd9Sstevel@tonic-gate 					error = EINVAL;
7517c478bd9Sstevel@tonic-gate 					goto remountout;
7527c478bd9Sstevel@tonic-gate 				}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 				/*
7557c478bd9Sstevel@tonic-gate 				 * Copy new data to old data
7567c478bd9Sstevel@tonic-gate 				 */
7577c478bd9Sstevel@tonic-gate 				bcopy(udf_vfsp->udf_iseq->b_un.b_addr,
75893239addSjohnlev 				    tpt->b_un.b_addr, udf_vfsp->udf_iseq_len);
7597c478bd9Sstevel@tonic-gate 				break;
7607c478bd9Sstevel@tonic-gate 			}
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_flags = UDF_FL_RW;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 		mutex_enter(&udf_vfsp->udf_lock);
7667c478bd9Sstevel@tonic-gate 		ud_sbwrite(udf_vfsp);
7677c478bd9Sstevel@tonic-gate 		mutex_exit(&udf_vfsp->udf_lock);
7687c478bd9Sstevel@tonic-gate remountout:
7697c478bd9Sstevel@tonic-gate 		if (tpt != NULL) {
7707c478bd9Sstevel@tonic-gate 			tpt->b_flags = B_AGE | B_STALE;
7717c478bd9Sstevel@tonic-gate 			brelse(tpt);
7727c478bd9Sstevel@tonic-gate 		}
7737c478bd9Sstevel@tonic-gate 		return (error);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	ASSERT(devvp != 0);
7777c478bd9Sstevel@tonic-gate 	/*
7787c478bd9Sstevel@tonic-gate 	 * Flush back any dirty pages on the block device to
7797c478bd9Sstevel@tonic-gate 	 * try and keep the buffer cache in sync with the page
7807c478bd9Sstevel@tonic-gate 	 * cache if someone is trying to use block devices when
7817c478bd9Sstevel@tonic-gate 	 * they really should be using the raw device.
7827c478bd9Sstevel@tonic-gate 	 */
7837c478bd9Sstevel@tonic-gate 	(void) VOP_PUTPAGE(common_specvp(devvp), (offset_t)0,
784da6c28aaSamw 	    (uint32_t)0, B_INVAL, cr, NULL);
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	/*
7887c478bd9Sstevel@tonic-gate 	 * Check if the file system
7897c478bd9Sstevel@tonic-gate 	 * is a valid udfs and fill
7907c478bd9Sstevel@tonic-gate 	 * the required fields in udf_vfs
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate #ifndef	__lint
7937c478bd9Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
7947c478bd9Sstevel@tonic-gate #endif
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	if ((lbsize = ud_get_lbsize(dev, &avd_loc)) == 0) {
7977c478bd9Sstevel@tonic-gate 		error = EINVAL;
7987c478bd9Sstevel@tonic-gate 		goto out;
7997c478bd9Sstevel@tonic-gate 	}
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate 	udf_vfsp = ud_validate_and_fill_superblock(dev, lbsize, avd_loc);
8027c478bd9Sstevel@tonic-gate 	if (udf_vfsp == NULL) {
8037c478bd9Sstevel@tonic-gate 		error = EINVAL;
8047c478bd9Sstevel@tonic-gate 		goto out;
8057c478bd9Sstevel@tonic-gate 	}
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	/*
8087c478bd9Sstevel@tonic-gate 	 * Fill in vfs private data
8097c478bd9Sstevel@tonic-gate 	 */
8107c478bd9Sstevel@tonic-gate 	vfsp->vfs_fstype = udf_fstype;
8117c478bd9Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, dev, udf_fstype);
8127c478bd9Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)udf_vfsp;
8137c478bd9Sstevel@tonic-gate 	vfsp->vfs_dev = dev;
8147c478bd9Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_NOTRUNC;
8157c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_devvp = devvp;
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_fsmnt = kmem_zalloc(strlen(name) + 1, KM_SLEEP);
8187c478bd9Sstevel@tonic-gate 	(void) strcpy(udf_vfsp->udf_fsmnt, name);
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_vfs = vfsp;
8217c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_rdclustsz = udf_vfsp->udf_wrclustsz = maxphys;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_mod = 0;
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 	lvid = udf_vfsp->udf_lvid;
8277c478bd9Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_RDONLY) {
8287c478bd9Sstevel@tonic-gate 		/*
8297c478bd9Sstevel@tonic-gate 		 * We could read only UDF1.50
8307c478bd9Sstevel@tonic-gate 		 * disallow mount of any highier version
8317c478bd9Sstevel@tonic-gate 		 */
8327c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_miread > UDF_150) {
8337c478bd9Sstevel@tonic-gate 			error = EINVAL;
8347c478bd9Sstevel@tonic-gate 			goto out;
8357c478bd9Sstevel@tonic-gate 		}
8367c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_flags = UDF_FL_RDONLY;
8377c478bd9Sstevel@tonic-gate 		if (SWAP_32(lvid->lvid_int_type) == LOG_VOL_CLOSE_INT) {
8387c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_clean = UDF_CLEAN;
8397c478bd9Sstevel@tonic-gate 		} else {
8407c478bd9Sstevel@tonic-gate 			/* Do we have a VAT at the end of the recorded media */
8417c478bd9Sstevel@tonic-gate 			map = udf_vfsp->udf_maps;
8427c478bd9Sstevel@tonic-gate 			for (i = 0; i < udf_vfsp->udf_nmaps; i++) {
8437c478bd9Sstevel@tonic-gate 				if (map->udm_flags & UDM_MAP_VPM) {
8447c478bd9Sstevel@tonic-gate 					break;
8457c478bd9Sstevel@tonic-gate 				}
8467c478bd9Sstevel@tonic-gate 				map++;
8477c478bd9Sstevel@tonic-gate 			}
8487c478bd9Sstevel@tonic-gate 			if (i == udf_vfsp->udf_nmaps) {
8497c478bd9Sstevel@tonic-gate 				error = ENOSPC;
8507c478bd9Sstevel@tonic-gate 				goto out;
8517c478bd9Sstevel@tonic-gate 			}
8527c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_clean = UDF_CLEAN;
8537c478bd9Sstevel@tonic-gate 		}
8547c478bd9Sstevel@tonic-gate 	} else {
8557c478bd9Sstevel@tonic-gate 		/*
8567c478bd9Sstevel@tonic-gate 		 * We could read UDF1.50 and write UDF1.50 only
8577c478bd9Sstevel@tonic-gate 		 * disallow mount of any highier version
8587c478bd9Sstevel@tonic-gate 		 */
8597c478bd9Sstevel@tonic-gate 		if ((udf_vfsp->udf_miread > UDF_150) ||
86093239addSjohnlev 		    (udf_vfsp->udf_miwrite > UDF_150)) {
8617c478bd9Sstevel@tonic-gate 			error = EINVAL;
8627c478bd9Sstevel@tonic-gate 			goto out;
8637c478bd9Sstevel@tonic-gate 		}
8647c478bd9Sstevel@tonic-gate 		/*
8657c478bd9Sstevel@tonic-gate 		 * Check if the media allows
8667c478bd9Sstevel@tonic-gate 		 * us to mount read/write
8677c478bd9Sstevel@tonic-gate 		 */
8687c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_mtype != UDF_MT_OW) {
8697c478bd9Sstevel@tonic-gate 			error = EACCES;
8707c478bd9Sstevel@tonic-gate 			goto out;
8717c478bd9Sstevel@tonic-gate 		}
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate 		/*
8747c478bd9Sstevel@tonic-gate 		 * Check if we have VAT on a writable media
8757c478bd9Sstevel@tonic-gate 		 * we cannot use the media in presence of VAT
8767c478bd9Sstevel@tonic-gate 		 * Dent RW mount.
8777c478bd9Sstevel@tonic-gate 		 */
8787c478bd9Sstevel@tonic-gate 		map = udf_vfsp->udf_maps;
8797c478bd9Sstevel@tonic-gate 		ASSERT(map != NULL);
8807c478bd9Sstevel@tonic-gate 		for (i = 0; i < udf_vfsp->udf_nmaps; i++) {
8817c478bd9Sstevel@tonic-gate 			if (map->udm_flags & UDM_MAP_VPM) {
8827c478bd9Sstevel@tonic-gate 				error = EACCES;
8837c478bd9Sstevel@tonic-gate 				goto out;
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 			map++;
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		/*
8897c478bd9Sstevel@tonic-gate 		 * Check if the domain Id allows
8907c478bd9Sstevel@tonic-gate 		 * us to write
8917c478bd9Sstevel@tonic-gate 		 */
8927c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_lvd->lvd_dom_id.reg_ids[2] & 0x3) {
8937c478bd9Sstevel@tonic-gate 			error = EACCES;
8947c478bd9Sstevel@tonic-gate 			goto out;
8957c478bd9Sstevel@tonic-gate 		}
8967c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_flags = UDF_FL_RW;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 		if (SWAP_32(lvid->lvid_int_type) == LOG_VOL_CLOSE_INT) {
8997c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_clean = UDF_CLEAN;
9007c478bd9Sstevel@tonic-gate 		} else {
9017c478bd9Sstevel@tonic-gate 			if (isroot) {
9027c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_clean = UDF_DIRTY;
9037c478bd9Sstevel@tonic-gate 			} else {
9047c478bd9Sstevel@tonic-gate 				error = ENOSPC;
9057c478bd9Sstevel@tonic-gate 				goto out;
9067c478bd9Sstevel@tonic-gate 			}
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	mutex_init(&udf_vfsp->udf_lock, NULL, MUTEX_DEFAULT, NULL);
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	mutex_init(&udf_vfsp->udf_rename_lck, NULL, MUTEX_DEFAULT, NULL);
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate #ifndef	__lint
9157c478bd9Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
9167c478bd9Sstevel@tonic-gate #endif
9177c478bd9Sstevel@tonic-gate 	if (error = ud_iget(vfsp, udf_vfsp->udf_ricb_prn,
91893239addSjohnlev 	    udf_vfsp->udf_ricb_loc, &rip, NULL, cr)) {
9197c478bd9Sstevel@tonic-gate 		mutex_destroy(&udf_vfsp->udf_lock);
9207c478bd9Sstevel@tonic-gate 		goto out;
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	/*
9257c478bd9Sstevel@tonic-gate 	 * Get the root inode and
9267c478bd9Sstevel@tonic-gate 	 * initialize the root vnode
9277c478bd9Sstevel@tonic-gate 	 */
9287c478bd9Sstevel@tonic-gate 	rvp = ITOV(rip);
9297c478bd9Sstevel@tonic-gate 	mutex_enter(&rvp->v_lock);
9307c478bd9Sstevel@tonic-gate 	rvp->v_flag |= VROOT;
9317c478bd9Sstevel@tonic-gate 	mutex_exit(&rvp->v_lock);
9327c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_root = rvp;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if (why == ROOT_INIT && isroot)
9367c478bd9Sstevel@tonic-gate 		rootvp = devvp;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	ud_vfs_add(udf_vfsp);
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_flags == UDF_FL_RW) {
9417c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_clean = UDF_DIRTY;
9427c478bd9Sstevel@tonic-gate 		ud_update_superblock(vfsp);
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	return (0);
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate out:
9487c478bd9Sstevel@tonic-gate 	ud_destroy_fsp(udf_vfsp);
9497c478bd9Sstevel@tonic-gate 	if (needclose) {
9507c478bd9Sstevel@tonic-gate 		(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
95193239addSjohnlev 		    FREAD : FREAD|FWRITE, 1, (offset_t)0, cr, NULL);
9527c478bd9Sstevel@tonic-gate 		bflush(dev);
9537c478bd9Sstevel@tonic-gate 		binval(dev);
9547c478bd9Sstevel@tonic-gate 	}
9557c478bd9Sstevel@tonic-gate 	VN_RELE(devvp);
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	return (error);
9587c478bd9Sstevel@tonic-gate }
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate static struct udf_vfs *
ud_validate_and_fill_superblock(dev_t dev,int32_t bsize,uint32_t avd_loc)9627c478bd9Sstevel@tonic-gate ud_validate_and_fill_superblock(dev_t dev, int32_t bsize, uint32_t avd_loc)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate 	int32_t error, count, index, shift;
9657c478bd9Sstevel@tonic-gate 	uint32_t dummy, vds_loc;
9667c478bd9Sstevel@tonic-gate 	caddr_t addr;
9677c478bd9Sstevel@tonic-gate 	daddr_t blkno, lblkno;
9687c478bd9Sstevel@tonic-gate 	struct buf *secbp, *bp;
9697c478bd9Sstevel@tonic-gate 	struct tag *ttag;
9707c478bd9Sstevel@tonic-gate 	struct anch_vol_desc_ptr *avdp;
9717c478bd9Sstevel@tonic-gate 	struct file_set_desc *fsd;
9727c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp = NULL;
9737c478bd9Sstevel@tonic-gate 	struct pmap_hdr *hdr;
9747c478bd9Sstevel@tonic-gate 	struct pmap_typ1 *typ1;
9757c478bd9Sstevel@tonic-gate 	struct pmap_typ2 *typ2;
9767c478bd9Sstevel@tonic-gate 	struct ud_map *map;
9777c478bd9Sstevel@tonic-gate 	int32_t	desc_len;
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate 	ud_printf("ud_validate_and_fill_superblock\n");
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	if (bsize < DEV_BSIZE) {
9827c478bd9Sstevel@tonic-gate 		return (NULL);
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 	shift = 0;
9857c478bd9Sstevel@tonic-gate 	while ((bsize >> shift) > DEV_BSIZE) {
9867c478bd9Sstevel@tonic-gate 		shift++;
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate 	/*
9907c478bd9Sstevel@tonic-gate 	 * Read Anchor Volume Descriptor
9917c478bd9Sstevel@tonic-gate 	 * Verify it and get the location of
9927c478bd9Sstevel@tonic-gate 	 * Main Volume Descriptor Sequence
9937c478bd9Sstevel@tonic-gate 	 */
9947c478bd9Sstevel@tonic-gate 	secbp = ud_bread(dev, avd_loc << shift, ANCHOR_VOL_DESC_LEN);
9957c478bd9Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
99693239addSjohnlev 		cmn_err(CE_NOTE, "udfs : Could not read Anchor Volume Desc %x",
99793239addSjohnlev 		    error);
9987c478bd9Sstevel@tonic-gate 		brelse(secbp);
9997c478bd9Sstevel@tonic-gate 		return (NULL);
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 	avdp = (struct anch_vol_desc_ptr *)secbp->b_un.b_addr;
10027c478bd9Sstevel@tonic-gate 	if (ud_verify_tag_and_desc(&avdp->avd_tag, UD_ANCH_VOL_DESC,
10037c478bd9Sstevel@tonic-gate 	    avd_loc, 1, ANCHOR_VOL_DESC_LEN) != 0) {
10047c478bd9Sstevel@tonic-gate 		brelse(secbp);
10057c478bd9Sstevel@tonic-gate 		return (NULL);
10067c478bd9Sstevel@tonic-gate 	}
10077c478bd9Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)
100893239addSjohnlev 	    kmem_zalloc(sizeof (struct udf_vfs), KM_SLEEP);
10097c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_mvds_loc = SWAP_32(avdp->avd_main_vdse.ext_loc);
10107c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_mvds_len = SWAP_32(avdp->avd_main_vdse.ext_len);
10117c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_rvds_loc = SWAP_32(avdp->avd_res_vdse.ext_loc);
10127c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_rvds_len = SWAP_32(avdp->avd_res_vdse.ext_len);
10137c478bd9Sstevel@tonic-gate 	secbp->b_flags = B_AGE | B_STALE;
10147c478bd9Sstevel@tonic-gate 	brelse(secbp);
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 	/*
10177c478bd9Sstevel@tonic-gate 	 * Read Main Volume Descriptor Sequence
10187c478bd9Sstevel@tonic-gate 	 * and process it
10197c478bd9Sstevel@tonic-gate 	 */
10207c478bd9Sstevel@tonic-gate 	vds_loc = udf_vfsp->udf_mvds_loc;
10217c478bd9Sstevel@tonic-gate 	secbp = ud_bread(dev, vds_loc << shift,
102293239addSjohnlev 	    udf_vfsp->udf_mvds_len);
10237c478bd9Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
10247c478bd9Sstevel@tonic-gate 		brelse(secbp);
102593239addSjohnlev 		cmn_err(CE_NOTE, "udfs : Could not read Main Volume Desc %x",
102693239addSjohnlev 		    error);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 		vds_loc = udf_vfsp->udf_rvds_loc;
10297c478bd9Sstevel@tonic-gate 		secbp = ud_bread(dev, vds_loc << shift,
103093239addSjohnlev 		    udf_vfsp->udf_rvds_len);
10317c478bd9Sstevel@tonic-gate 		if ((error = geterror(secbp)) != 0) {
10327c478bd9Sstevel@tonic-gate 			brelse(secbp);
10337c478bd9Sstevel@tonic-gate 			cmn_err(CE_NOTE,
10347c478bd9Sstevel@tonic-gate 			"udfs : Could not read Res Volume Desc %x", error);
10357c478bd9Sstevel@tonic-gate 			return (NULL);
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_vds = ngeteblk(udf_vfsp->udf_mvds_len);
10407c478bd9Sstevel@tonic-gate 	bp = udf_vfsp->udf_vds;
10417c478bd9Sstevel@tonic-gate 	bp->b_edev = dev;
10427c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
10437c478bd9Sstevel@tonic-gate 	bp->b_blkno = vds_loc << shift;
10447c478bd9Sstevel@tonic-gate 	bp->b_bcount = udf_vfsp->udf_mvds_len;
10457c478bd9Sstevel@tonic-gate 	bcopy(secbp->b_un.b_addr, bp->b_un.b_addr, udf_vfsp->udf_mvds_len);
10467c478bd9Sstevel@tonic-gate 	secbp->b_flags |= B_STALE | B_AGE;
10477c478bd9Sstevel@tonic-gate 	brelse(secbp);
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	count = udf_vfsp->udf_mvds_len / DEV_BSIZE;
10517c478bd9Sstevel@tonic-gate 	addr = bp->b_un.b_addr;
10527c478bd9Sstevel@tonic-gate 	for (index = 0; index < count; index ++) {
10537c478bd9Sstevel@tonic-gate 		ttag = (struct tag *)(addr + index * DEV_BSIZE);
10547c478bd9Sstevel@tonic-gate 		desc_len = udf_vfsp->udf_mvds_len - (index * DEV_BSIZE);
10557c478bd9Sstevel@tonic-gate 		if (ud_verify_tag_and_desc(ttag, UD_PRI_VOL_DESC,
10567c478bd9Sstevel@tonic-gate 		    vds_loc + (index >> shift),
10577c478bd9Sstevel@tonic-gate 		    1, desc_len) == 0) {
10587c478bd9Sstevel@tonic-gate 			if (udf_vfsp->udf_pvd == NULL) {
10597c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_pvd =
106093239addSjohnlev 				    (struct pri_vol_desc *)ttag;
10617c478bd9Sstevel@tonic-gate 			} else {
10627c478bd9Sstevel@tonic-gate 				struct pri_vol_desc *opvd, *npvd;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 				opvd = udf_vfsp->udf_pvd;
10657c478bd9Sstevel@tonic-gate 				npvd = (struct pri_vol_desc *)ttag;
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 				if ((strncmp(opvd->pvd_vsi,
106893239addSjohnlev 				    npvd->pvd_vsi, 128) == 0) &&
106993239addSjohnlev 				    (strncmp(opvd->pvd_vol_id,
107093239addSjohnlev 				    npvd->pvd_vol_id, 32) == 0) &&
107193239addSjohnlev 				    (strncmp((caddr_t)&opvd->pvd_desc_cs,
107293239addSjohnlev 				    (caddr_t)&npvd->pvd_desc_cs,
107393239addSjohnlev 				    sizeof (charspec_t)) == 0)) {
10747c478bd9Sstevel@tonic-gate 
10757c478bd9Sstevel@tonic-gate 					if (SWAP_32(opvd->pvd_vdsn) <
107693239addSjohnlev 					    SWAP_32(npvd->pvd_vdsn)) {
10777c478bd9Sstevel@tonic-gate 						udf_vfsp->udf_pvd = npvd;
10787c478bd9Sstevel@tonic-gate 					}
10797c478bd9Sstevel@tonic-gate 				} else {
10807c478bd9Sstevel@tonic-gate 					goto out;
10817c478bd9Sstevel@tonic-gate 				}
10827c478bd9Sstevel@tonic-gate 			}
10837c478bd9Sstevel@tonic-gate 		} else if (ud_verify_tag_and_desc(ttag, UD_LOG_VOL_DESC,
10847c478bd9Sstevel@tonic-gate 		    vds_loc + (index >> shift),
10857c478bd9Sstevel@tonic-gate 		    1, desc_len) == 0) {
10867c478bd9Sstevel@tonic-gate 			struct log_vol_desc *lvd;
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate 			lvd = (struct log_vol_desc *)ttag;
10897c478bd9Sstevel@tonic-gate 			if (strncmp(lvd->lvd_dom_id.reg_id,
109093239addSjohnlev 			    UDF_DOMAIN_NAME, 23) != 0) {
10917c478bd9Sstevel@tonic-gate 				printf("Domain ID in lvd is not valid\n");
10927c478bd9Sstevel@tonic-gate 				goto out;
10937c478bd9Sstevel@tonic-gate 			}
10947c478bd9Sstevel@tonic-gate 
10957c478bd9Sstevel@tonic-gate 			if (udf_vfsp->udf_lvd == NULL) {
10967c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_lvd = lvd;
10977c478bd9Sstevel@tonic-gate 			} else {
10987c478bd9Sstevel@tonic-gate 				struct log_vol_desc *olvd;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 				olvd = udf_vfsp->udf_lvd;
11017c478bd9Sstevel@tonic-gate 				if ((strncmp((caddr_t)&olvd->lvd_desc_cs,
110293239addSjohnlev 				    (caddr_t)&lvd->lvd_desc_cs,
110393239addSjohnlev 				    sizeof (charspec_t)) == 0) &&
110493239addSjohnlev 				    (strncmp(olvd->lvd_lvid,
110593239addSjohnlev 				    lvd->lvd_lvid, 128) == 0)) {
11067c478bd9Sstevel@tonic-gate 					if (SWAP_32(olvd->lvd_vdsn) <
110793239addSjohnlev 					    SWAP_32(lvd->lvd_vdsn)) {
11087c478bd9Sstevel@tonic-gate 						udf_vfsp->udf_lvd = lvd;
11097c478bd9Sstevel@tonic-gate 					}
11107c478bd9Sstevel@tonic-gate 				} else {
11117c478bd9Sstevel@tonic-gate 					goto out;
11127c478bd9Sstevel@tonic-gate 				}
11137c478bd9Sstevel@tonic-gate 			}
11147c478bd9Sstevel@tonic-gate 		} else if (ud_verify_tag_and_desc(ttag, UD_PART_DESC,
11157c478bd9Sstevel@tonic-gate 		    vds_loc + (index >> shift),
11167c478bd9Sstevel@tonic-gate 		    1, desc_len) == 0) {
11177c478bd9Sstevel@tonic-gate 			int32_t i;
11187c478bd9Sstevel@tonic-gate 			struct phdr_desc *hdr;
11197c478bd9Sstevel@tonic-gate 			struct part_desc *pdesc;
11207c478bd9Sstevel@tonic-gate 			struct ud_part *pnew, *pold, *part;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 			pdesc = (struct part_desc *)ttag;
11237c478bd9Sstevel@tonic-gate 			pold = udf_vfsp->udf_parts;
11247c478bd9Sstevel@tonic-gate 			for (i = 0; i < udf_vfsp->udf_npart; i++) {
112593239addSjohnlev 				if (pold->udp_number !=
112693239addSjohnlev 				    SWAP_16(pdesc->pd_pnum)) {
112793239addSjohnlev 					pold++;
112893239addSjohnlev 					continue;
112993239addSjohnlev 				}
113093239addSjohnlev 
113193239addSjohnlev 				if (SWAP_32(pdesc->pd_vdsn) >
113293239addSjohnlev 				    pold->udp_seqno) {
113393239addSjohnlev 					pold->udp_seqno =
113493239addSjohnlev 					    SWAP_32(pdesc->pd_vdsn);
113593239addSjohnlev 					pold->udp_access =
113693239addSjohnlev 					    SWAP_32(pdesc->pd_acc_type);
113793239addSjohnlev 					pold->udp_start =
113893239addSjohnlev 					    SWAP_32(pdesc->pd_part_start);
113993239addSjohnlev 					pold->udp_length =
114093239addSjohnlev 					    SWAP_32(pdesc->pd_part_length);
11417c478bd9Sstevel@tonic-gate 				}
114293239addSjohnlev 				goto loop_end;
11437c478bd9Sstevel@tonic-gate 			}
11447c478bd9Sstevel@tonic-gate 			pold = udf_vfsp->udf_parts;
11457c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_npart++;
11467c478bd9Sstevel@tonic-gate 			pnew = kmem_zalloc(udf_vfsp->udf_npart *
114793239addSjohnlev 			    sizeof (struct ud_part), KM_SLEEP);
11487c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_parts = pnew;
11497c478bd9Sstevel@tonic-gate 			if (pold) {
11507c478bd9Sstevel@tonic-gate 				bcopy(pold, pnew,
115193239addSjohnlev 				    sizeof (struct ud_part) *
115293239addSjohnlev 				    (udf_vfsp->udf_npart - 1));
11537c478bd9Sstevel@tonic-gate 				kmem_free(pold,
115493239addSjohnlev 				    sizeof (struct ud_part) *
115593239addSjohnlev 				    (udf_vfsp->udf_npart - 1));
11567c478bd9Sstevel@tonic-gate 			}
11577c478bd9Sstevel@tonic-gate 			part = pnew + (udf_vfsp->udf_npart - 1);
11587c478bd9Sstevel@tonic-gate 			part->udp_number = SWAP_16(pdesc->pd_pnum);
11597c478bd9Sstevel@tonic-gate 			part->udp_seqno = SWAP_32(pdesc->pd_vdsn);
11607c478bd9Sstevel@tonic-gate 			part->udp_access = SWAP_32(pdesc->pd_acc_type);
11617c478bd9Sstevel@tonic-gate 			part->udp_start = SWAP_32(pdesc->pd_part_start);
11627c478bd9Sstevel@tonic-gate 			part->udp_length = SWAP_32(pdesc->pd_part_length);
11637c478bd9Sstevel@tonic-gate 			part->udp_last_alloc = 0;
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 			/*
11667c478bd9Sstevel@tonic-gate 			 * Figure out space bitmaps
11677c478bd9Sstevel@tonic-gate 			 * or space tables
11687c478bd9Sstevel@tonic-gate 			 */
11697c478bd9Sstevel@tonic-gate 			hdr = (struct phdr_desc *)pdesc->pd_pc_use;
11707c478bd9Sstevel@tonic-gate 			if (hdr->phdr_ust.sad_ext_len) {
11717c478bd9Sstevel@tonic-gate 				part->udp_flags = UDP_SPACETBLS;
11727c478bd9Sstevel@tonic-gate 				part->udp_unall_loc =
117393239addSjohnlev 				    SWAP_32(hdr->phdr_ust.sad_ext_loc);
11747c478bd9Sstevel@tonic-gate 				part->udp_unall_len =
117593239addSjohnlev 				    SWAP_32(hdr->phdr_ust.sad_ext_len);
11767c478bd9Sstevel@tonic-gate 				part->udp_freed_loc =
117793239addSjohnlev 				    SWAP_32(hdr->phdr_fst.sad_ext_loc);
11787c478bd9Sstevel@tonic-gate 				part->udp_freed_len =
117993239addSjohnlev 				    SWAP_32(hdr->phdr_fst.sad_ext_len);
11807c478bd9Sstevel@tonic-gate 			} else {
11817c478bd9Sstevel@tonic-gate 				part->udp_flags = UDP_BITMAPS;
11827c478bd9Sstevel@tonic-gate 				part->udp_unall_loc =
118393239addSjohnlev 				    SWAP_32(hdr->phdr_usb.sad_ext_loc);
11847c478bd9Sstevel@tonic-gate 				part->udp_unall_len =
118593239addSjohnlev 				    SWAP_32(hdr->phdr_usb.sad_ext_len);
11867c478bd9Sstevel@tonic-gate 				part->udp_freed_loc =
118793239addSjohnlev 				    SWAP_32(hdr->phdr_fsb.sad_ext_loc);
11887c478bd9Sstevel@tonic-gate 				part->udp_freed_len =
118993239addSjohnlev 				    SWAP_32(hdr->phdr_fsb.sad_ext_len);
11907c478bd9Sstevel@tonic-gate 			}
11917c478bd9Sstevel@tonic-gate 		} else if (ud_verify_tag_and_desc(ttag, UD_TERM_DESC,
11927c478bd9Sstevel@tonic-gate 		    vds_loc + (index >> shift),
11937c478bd9Sstevel@tonic-gate 		    1, desc_len) == 0) {
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 			break;
11967c478bd9Sstevel@tonic-gate 		}
11977c478bd9Sstevel@tonic-gate loop_end:
11987c478bd9Sstevel@tonic-gate 		;
11997c478bd9Sstevel@tonic-gate 	}
12007c478bd9Sstevel@tonic-gate 	if ((udf_vfsp->udf_pvd == NULL) ||
120193239addSjohnlev 	    (udf_vfsp->udf_lvd == NULL) ||
120293239addSjohnlev 	    (udf_vfsp->udf_parts == NULL)) {
12037c478bd9Sstevel@tonic-gate 		goto out;
12047c478bd9Sstevel@tonic-gate 	}
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate 	/*
12077c478bd9Sstevel@tonic-gate 	 * Process Primary Volume Descriptor
12087c478bd9Sstevel@tonic-gate 	 */
12097c478bd9Sstevel@tonic-gate 	(void) strncpy(udf_vfsp->udf_volid, udf_vfsp->udf_pvd->pvd_vol_id, 32);
12107c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_volid[31] = '\0';
12117c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_tsno = SWAP_16(udf_vfsp->udf_pvd->pvd_tag.tag_sno);
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	/*
12147c478bd9Sstevel@tonic-gate 	 * Process Logical Volume Descriptor
12157c478bd9Sstevel@tonic-gate 	 */
12167c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_lbsize =
121793239addSjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_log_bsize);
12187c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_lbmask = udf_vfsp->udf_lbsize - 1;
12197c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_l2d_shift = shift;
12207c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_l2b_shift = shift + DEV_BSHIFT;
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	/*
12237c478bd9Sstevel@tonic-gate 	 * Check if the media is in
12247c478bd9Sstevel@tonic-gate 	 * proper domain.
12257c478bd9Sstevel@tonic-gate 	 */
12267c478bd9Sstevel@tonic-gate 	if (strcmp(udf_vfsp->udf_lvd->lvd_dom_id.reg_id,
122793239addSjohnlev 	    UDF_DOMAIN_NAME) != 0) {
12287c478bd9Sstevel@tonic-gate 		goto out;
12297c478bd9Sstevel@tonic-gate 	}
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate 	/*
12327c478bd9Sstevel@tonic-gate 	 * AVDS offset does not match with the lbsize
12337c478bd9Sstevel@tonic-gate 	 * in the lvd
12347c478bd9Sstevel@tonic-gate 	 */
12357c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_lbsize != bsize) {
12367c478bd9Sstevel@tonic-gate 		goto out;
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 
12397c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_iseq_loc =
124093239addSjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_int_seq_ext.ext_loc);
12417c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_iseq_len =
124293239addSjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_int_seq_ext.ext_len);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_fsd_prn =
124593239addSjohnlev 	    SWAP_16(udf_vfsp->udf_lvd->lvd_lvcu.lad_ext_prn);
12467c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_fsd_loc =
124793239addSjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_lvcu.lad_ext_loc);
12487c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_fsd_len =
124993239addSjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_lvcu.lad_ext_len);
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	/*
12537c478bd9Sstevel@tonic-gate 	 * process paritions
12547c478bd9Sstevel@tonic-gate 	 */
12557c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_mtype = udf_vfsp->udf_parts[0].udp_access;
12567c478bd9Sstevel@tonic-gate 	for (index = 0; index < udf_vfsp->udf_npart; index ++) {
12577c478bd9Sstevel@tonic-gate 		if (udf_vfsp->udf_parts[index].udp_access <
125893239addSjohnlev 		    udf_vfsp->udf_mtype) {
12597c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_mtype =
126093239addSjohnlev 			    udf_vfsp->udf_parts[index].udp_access;
12617c478bd9Sstevel@tonic-gate 		}
12627c478bd9Sstevel@tonic-gate 	}
12637c478bd9Sstevel@tonic-gate 	if ((udf_vfsp->udf_mtype < UDF_MT_RO) ||
126493239addSjohnlev 	    (udf_vfsp->udf_mtype > UDF_MT_OW)) {
12657c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_mtype = UDF_MT_RO;
12667c478bd9Sstevel@tonic-gate 	}
12677c478bd9Sstevel@tonic-gate 
12687c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_nmaps = 0;
12697c478bd9Sstevel@tonic-gate 	hdr = (struct pmap_hdr *)udf_vfsp->udf_lvd->lvd_pmaps;
12707c478bd9Sstevel@tonic-gate 	count = SWAP_32(udf_vfsp->udf_lvd->lvd_num_pmaps);
12717c478bd9Sstevel@tonic-gate 	for (index = 0; index < count; index++) {
12727c478bd9Sstevel@tonic-gate 
12737c478bd9Sstevel@tonic-gate 		if ((hdr->maph_type == MAP_TYPE1) &&
127493239addSjohnlev 		    (hdr->maph_length == MAP_TYPE1_LEN)) {
12757c478bd9Sstevel@tonic-gate 			typ1 = (struct pmap_typ1 *)hdr;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 			map = udf_vfsp->udf_maps;
12787c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_maps =
127993239addSjohnlev 			    kmem_zalloc(sizeof (struct ud_map) *
128093239addSjohnlev 			    (udf_vfsp->udf_nmaps + 1), KM_SLEEP);
12817c478bd9Sstevel@tonic-gate 			if (map != NULL) {
12827c478bd9Sstevel@tonic-gate 				bcopy(map, udf_vfsp->udf_maps,
128393239addSjohnlev 				    sizeof (struct ud_map) *
128493239addSjohnlev 				    udf_vfsp->udf_nmaps);
128593239addSjohnlev 				kmem_free(map, sizeof (struct ud_map) *
128693239addSjohnlev 				    udf_vfsp->udf_nmaps);
12877c478bd9Sstevel@tonic-gate 			}
12887c478bd9Sstevel@tonic-gate 			map = udf_vfsp->udf_maps + udf_vfsp->udf_nmaps;
12897c478bd9Sstevel@tonic-gate 			map->udm_flags = UDM_MAP_NORM;
12907c478bd9Sstevel@tonic-gate 			map->udm_vsn = SWAP_16(typ1->map1_vsn);
12917c478bd9Sstevel@tonic-gate 			map->udm_pn = SWAP_16(typ1->map1_pn);
12927c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_nmaps ++;
12937c478bd9Sstevel@tonic-gate 		} else if ((hdr->maph_type == MAP_TYPE2) &&
129493239addSjohnlev 		    (hdr->maph_length == MAP_TYPE2_LEN)) {
12957c478bd9Sstevel@tonic-gate 			typ2 = (struct pmap_typ2 *)hdr;
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 			if (strncmp(typ2->map2_pti.reg_id,
129893239addSjohnlev 			    UDF_VIRT_PART, 23) == 0) {
12997c478bd9Sstevel@tonic-gate 				/*
13007c478bd9Sstevel@tonic-gate 				 * Add this to the normal
13017c478bd9Sstevel@tonic-gate 				 * partition table so that
13027c478bd9Sstevel@tonic-gate 				 * we donot
13037c478bd9Sstevel@tonic-gate 				 */
13047c478bd9Sstevel@tonic-gate 				map = udf_vfsp->udf_maps;
13057c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_maps =
130693239addSjohnlev 				    kmem_zalloc(sizeof (struct ud_map) *
130793239addSjohnlev 				    (udf_vfsp->udf_nmaps + 1), KM_SLEEP);
13087c478bd9Sstevel@tonic-gate 				if (map != NULL) {
13097c478bd9Sstevel@tonic-gate 					bcopy(map, udf_vfsp->udf_maps,
131093239addSjohnlev 					    sizeof (struct ud_map) *
131193239addSjohnlev 					    udf_vfsp->udf_nmaps);
13127c478bd9Sstevel@tonic-gate 					kmem_free(map,
131393239addSjohnlev 					    sizeof (struct ud_map) *
131493239addSjohnlev 					    udf_vfsp->udf_nmaps);
13157c478bd9Sstevel@tonic-gate 				}
13167c478bd9Sstevel@tonic-gate 				map = udf_vfsp->udf_maps + udf_vfsp->udf_nmaps;
13177c478bd9Sstevel@tonic-gate 				map->udm_flags = UDM_MAP_VPM;
13187c478bd9Sstevel@tonic-gate 				map->udm_vsn = SWAP_16(typ2->map2_vsn);
13197c478bd9Sstevel@tonic-gate 				map->udm_pn = SWAP_16(typ2->map2_pn);
13207c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_nmaps ++;
13217c478bd9Sstevel@tonic-gate 				if (error = ud_get_last_block(dev, &lblkno)) {
13227c478bd9Sstevel@tonic-gate 					goto out;
13237c478bd9Sstevel@tonic-gate 				}
13247c478bd9Sstevel@tonic-gate 				if (error = ud_val_get_vat(udf_vfsp, dev,
132593239addSjohnlev 				    lblkno, map)) {
13267c478bd9Sstevel@tonic-gate 					goto out;
13277c478bd9Sstevel@tonic-gate 				}
13287c478bd9Sstevel@tonic-gate 			} else if (strncmp(typ2->map2_pti.reg_id,
132993239addSjohnlev 			    UDF_SPAR_PART, 23) == 0) {
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 				if (SWAP_16(typ2->map2_pl) != 32) {
13327c478bd9Sstevel@tonic-gate 					printf(
133393239addSjohnlev 					    "Packet Length is not valid %x\n",
133493239addSjohnlev 					    SWAP_16(typ2->map2_pl));
13357c478bd9Sstevel@tonic-gate 					goto out;
13367c478bd9Sstevel@tonic-gate 				}
13377c478bd9Sstevel@tonic-gate 				if ((typ2->map2_nst < 1) ||
133893239addSjohnlev 				    (typ2->map2_nst > 4)) {
13397c478bd9Sstevel@tonic-gate 					goto out;
13407c478bd9Sstevel@tonic-gate 				}
13417c478bd9Sstevel@tonic-gate 				map = udf_vfsp->udf_maps;
13427c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_maps =
134393239addSjohnlev 				    kmem_zalloc(sizeof (struct ud_map) *
134493239addSjohnlev 				    (udf_vfsp->udf_nmaps + 1),
134593239addSjohnlev 				    KM_SLEEP);
13467c478bd9Sstevel@tonic-gate 				if (map != NULL) {
13477c478bd9Sstevel@tonic-gate 					bcopy(map, udf_vfsp->udf_maps,
134893239addSjohnlev 					    sizeof (struct ud_map) *
134993239addSjohnlev 					    udf_vfsp->udf_nmaps);
13507c478bd9Sstevel@tonic-gate 					kmem_free(map,
135193239addSjohnlev 					    sizeof (struct ud_map) *
135293239addSjohnlev 					    udf_vfsp->udf_nmaps);
13537c478bd9Sstevel@tonic-gate 				}
13547c478bd9Sstevel@tonic-gate 				map = udf_vfsp->udf_maps + udf_vfsp->udf_nmaps;
13557c478bd9Sstevel@tonic-gate 				map->udm_flags = UDM_MAP_SPM;
13567c478bd9Sstevel@tonic-gate 				map->udm_vsn = SWAP_16(typ2->map2_vsn);
13577c478bd9Sstevel@tonic-gate 				map->udm_pn = SWAP_16(typ2->map2_pn);
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_nmaps ++;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 				if (error = ud_read_sparing_tbls(udf_vfsp,
136293239addSjohnlev 				    dev, map, typ2)) {
13637c478bd9Sstevel@tonic-gate 					goto out;
13647c478bd9Sstevel@tonic-gate 				}
13657c478bd9Sstevel@tonic-gate 			} else {
13667c478bd9Sstevel@tonic-gate 				/*
13677c478bd9Sstevel@tonic-gate 				 * Unknown type of partition
13687c478bd9Sstevel@tonic-gate 				 * Bail out
13697c478bd9Sstevel@tonic-gate 				 */
13707c478bd9Sstevel@tonic-gate 				goto out;
13717c478bd9Sstevel@tonic-gate 			}
13727c478bd9Sstevel@tonic-gate 		} else {
13737c478bd9Sstevel@tonic-gate 			/*
13747c478bd9Sstevel@tonic-gate 			 * Unknown type of partition
13757c478bd9Sstevel@tonic-gate 			 * Bail out
13767c478bd9Sstevel@tonic-gate 			 */
13777c478bd9Sstevel@tonic-gate 			goto out;
13787c478bd9Sstevel@tonic-gate 		}
13797c478bd9Sstevel@tonic-gate 		hdr = (struct pmap_hdr *)(((uint8_t *)hdr) + hdr->maph_length);
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	/*
13847c478bd9Sstevel@tonic-gate 	 * Read Logical Volume Integrity Sequence
13857c478bd9Sstevel@tonic-gate 	 * and process it
13867c478bd9Sstevel@tonic-gate 	 */
13877c478bd9Sstevel@tonic-gate 	secbp = ud_bread(dev, udf_vfsp->udf_iseq_loc << shift,
138893239addSjohnlev 	    udf_vfsp->udf_iseq_len);
13897c478bd9Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
13907c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
13917c478bd9Sstevel@tonic-gate 		"udfs : Could not read Logical Volume Integrity Sequence %x",
139293239addSjohnlev 		    error);
13937c478bd9Sstevel@tonic-gate 		brelse(secbp);
13947c478bd9Sstevel@tonic-gate 		goto out;
13957c478bd9Sstevel@tonic-gate 	}
13967c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_iseq = ngeteblk(udf_vfsp->udf_iseq_len);
13977c478bd9Sstevel@tonic-gate 	bp = udf_vfsp->udf_iseq;
13987c478bd9Sstevel@tonic-gate 	bp->b_edev = dev;
13997c478bd9Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
14007c478bd9Sstevel@tonic-gate 	bp->b_blkno = udf_vfsp->udf_iseq_loc << shift;
14017c478bd9Sstevel@tonic-gate 	bp->b_bcount = udf_vfsp->udf_iseq_len;
14027c478bd9Sstevel@tonic-gate 	bcopy(secbp->b_un.b_addr, bp->b_un.b_addr, udf_vfsp->udf_iseq_len);
14037c478bd9Sstevel@tonic-gate 	secbp->b_flags |= B_STALE | B_AGE;
14047c478bd9Sstevel@tonic-gate 	brelse(secbp);
14057c478bd9Sstevel@tonic-gate 
14067c478bd9Sstevel@tonic-gate 	count = udf_vfsp->udf_iseq_len / DEV_BSIZE;
14077c478bd9Sstevel@tonic-gate 	addr = bp->b_un.b_addr;
14087c478bd9Sstevel@tonic-gate 	for (index = 0; index < count; index ++) {
14097c478bd9Sstevel@tonic-gate 		ttag = (struct tag *)(addr + index * DEV_BSIZE);
14107c478bd9Sstevel@tonic-gate 		desc_len = udf_vfsp->udf_iseq_len - (index * DEV_BSIZE);
14117c478bd9Sstevel@tonic-gate 		if (ud_verify_tag_and_desc(ttag, UD_LOG_VOL_INT,
14127c478bd9Sstevel@tonic-gate 		    udf_vfsp->udf_iseq_loc + (index >> shift),
14137c478bd9Sstevel@tonic-gate 		    1, desc_len) == 0) {
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 			struct log_vol_int_desc *lvid;
14167c478bd9Sstevel@tonic-gate 
14177c478bd9Sstevel@tonic-gate 			lvid = (struct log_vol_int_desc *)ttag;
14187c478bd9Sstevel@tonic-gate 			udf_vfsp->udf_lvid = lvid;
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 			if (SWAP_32(lvid->lvid_int_type) == LOG_VOL_CLOSE_INT) {
14217c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_clean = UDF_CLEAN;
14227c478bd9Sstevel@tonic-gate 			} else {
14237c478bd9Sstevel@tonic-gate 				udf_vfsp->udf_clean = UDF_DIRTY;
14247c478bd9Sstevel@tonic-gate 			}
14257c478bd9Sstevel@tonic-gate 
14267c478bd9Sstevel@tonic-gate 			/*
14277c478bd9Sstevel@tonic-gate 			 * update superblock with the metadata
14287c478bd9Sstevel@tonic-gate 			 */
14297c478bd9Sstevel@tonic-gate 			ud_convert_to_superblock(udf_vfsp, lvid);
14307c478bd9Sstevel@tonic-gate 			break;
14317c478bd9Sstevel@tonic-gate 		}
14327c478bd9Sstevel@tonic-gate 	}
14337c478bd9Sstevel@tonic-gate 
14347c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_lvid == NULL) {
14357c478bd9Sstevel@tonic-gate 		goto out;
14367c478bd9Sstevel@tonic-gate 	}
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	if ((blkno = ud_xlate_to_daddr(udf_vfsp,
143993239addSjohnlev 	    udf_vfsp->udf_fsd_prn, udf_vfsp->udf_fsd_loc,
144093239addSjohnlev 	    1, &dummy)) == 0) {
14417c478bd9Sstevel@tonic-gate 		goto out;
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	secbp = ud_bread(dev, blkno << shift, udf_vfsp->udf_fsd_len);
14447c478bd9Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
14457c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE,
14467c478bd9Sstevel@tonic-gate 		"udfs : Could not read File Set Descriptor %x", error);
14477c478bd9Sstevel@tonic-gate 		brelse(secbp);
14487c478bd9Sstevel@tonic-gate 		goto out;
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	fsd = (struct file_set_desc *)secbp->b_un.b_addr;
14517c478bd9Sstevel@tonic-gate 	if (ud_verify_tag_and_desc(&fsd->fsd_tag, UD_FILE_SET_DESC,
14527c478bd9Sstevel@tonic-gate 	    udf_vfsp->udf_fsd_loc,
14537c478bd9Sstevel@tonic-gate 	    1, udf_vfsp->udf_fsd_len) != 0) {
14547c478bd9Sstevel@tonic-gate 		secbp->b_flags = B_AGE | B_STALE;
14557c478bd9Sstevel@tonic-gate 		brelse(secbp);
14567c478bd9Sstevel@tonic-gate 		goto out;
14577c478bd9Sstevel@tonic-gate 	}
14587c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_ricb_prn = SWAP_16(fsd->fsd_root_icb.lad_ext_prn);
14597c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_ricb_loc = SWAP_32(fsd->fsd_root_icb.lad_ext_loc);
14607c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_ricb_len = SWAP_32(fsd->fsd_root_icb.lad_ext_len);
14617c478bd9Sstevel@tonic-gate 	secbp->b_flags = B_AGE | B_STALE;
14627c478bd9Sstevel@tonic-gate 	brelse(secbp);
14637c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_root_blkno = ud_xlate_to_daddr(udf_vfsp,
146493239addSjohnlev 	    udf_vfsp->udf_ricb_prn, udf_vfsp->udf_ricb_loc,
146593239addSjohnlev 	    1, &dummy);
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	return (udf_vfsp);
14687c478bd9Sstevel@tonic-gate out:
14697c478bd9Sstevel@tonic-gate 	ud_destroy_fsp(udf_vfsp);
14707c478bd9Sstevel@tonic-gate 
14717c478bd9Sstevel@tonic-gate 	return (NULL);
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate  * release/free resources from one ud_map; map data was zalloc'd in
14767c478bd9Sstevel@tonic-gate  * ud_validate_and_fill_superblock() and fields may later point to
14777c478bd9Sstevel@tonic-gate  * valid data
14787c478bd9Sstevel@tonic-gate  */
14797c478bd9Sstevel@tonic-gate static void
ud_free_map(struct ud_map * map)14807c478bd9Sstevel@tonic-gate ud_free_map(struct ud_map *map)
14817c478bd9Sstevel@tonic-gate {
14827c478bd9Sstevel@tonic-gate 	uint32_t n;
14837c478bd9Sstevel@tonic-gate 
14847c478bd9Sstevel@tonic-gate 	if (map->udm_flags & UDM_MAP_VPM) {
14857c478bd9Sstevel@tonic-gate 		if (map->udm_count) {
14867c478bd9Sstevel@tonic-gate 			kmem_free(map->udm_count,
14877c478bd9Sstevel@tonic-gate 			    map->udm_nent * sizeof (*map->udm_count));
14887c478bd9Sstevel@tonic-gate 			map->udm_count = NULL;
14897c478bd9Sstevel@tonic-gate 		}
14907c478bd9Sstevel@tonic-gate 		if (map->udm_bp) {
14917c478bd9Sstevel@tonic-gate 			for (n = 0; n < map->udm_nent; n++) {
14927c478bd9Sstevel@tonic-gate 				if (map->udm_bp[n])
14937c478bd9Sstevel@tonic-gate 					brelse(map->udm_bp[n]);
14947c478bd9Sstevel@tonic-gate 			}
14957c478bd9Sstevel@tonic-gate 			kmem_free(map->udm_bp,
14967c478bd9Sstevel@tonic-gate 			    map->udm_nent * sizeof (*map->udm_bp));
14977c478bd9Sstevel@tonic-gate 			map->udm_bp = NULL;
14987c478bd9Sstevel@tonic-gate 		}
14997c478bd9Sstevel@tonic-gate 		if (map->udm_addr) {
15007c478bd9Sstevel@tonic-gate 			kmem_free(map->udm_addr,
15017c478bd9Sstevel@tonic-gate 			    map->udm_nent * sizeof (*map->udm_addr));
15027c478bd9Sstevel@tonic-gate 			map->udm_addr = NULL;
15037c478bd9Sstevel@tonic-gate 		}
15047c478bd9Sstevel@tonic-gate 	}
15057c478bd9Sstevel@tonic-gate 	if (map->udm_flags & UDM_MAP_SPM) {
15067c478bd9Sstevel@tonic-gate 		for (n = 0; n < MAX_SPM; n++) {
15077c478bd9Sstevel@tonic-gate 			if (map->udm_sbp[n]) {
15087c478bd9Sstevel@tonic-gate 				brelse(map->udm_sbp[n]);
15097c478bd9Sstevel@tonic-gate 				map->udm_sbp[n] = NULL;
15107c478bd9Sstevel@tonic-gate 				map->udm_spaddr[n] = NULL;
15117c478bd9Sstevel@tonic-gate 			}
15127c478bd9Sstevel@tonic-gate 		}
15137c478bd9Sstevel@tonic-gate 	}
15147c478bd9Sstevel@tonic-gate }
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate void
ud_destroy_fsp(struct udf_vfs * udf_vfsp)15177c478bd9Sstevel@tonic-gate ud_destroy_fsp(struct udf_vfs *udf_vfsp)
15187c478bd9Sstevel@tonic-gate {
15197c478bd9Sstevel@tonic-gate 	int32_t i;
15207c478bd9Sstevel@tonic-gate 
15217c478bd9Sstevel@tonic-gate 	ud_printf("ud_destroy_fsp\n");
15227c478bd9Sstevel@tonic-gate 	if (udf_vfsp == NULL)
15237c478bd9Sstevel@tonic-gate 		return;
15247c478bd9Sstevel@tonic-gate 
15257c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_maps) {
15267c478bd9Sstevel@tonic-gate 		for (i = 0; i < udf_vfsp->udf_nmaps; i++)
15277c478bd9Sstevel@tonic-gate 			ud_free_map(&udf_vfsp->udf_maps[i]);
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 		kmem_free(udf_vfsp->udf_maps,
15307c478bd9Sstevel@tonic-gate 		    udf_vfsp->udf_nmaps * sizeof (*udf_vfsp->udf_maps));
15317c478bd9Sstevel@tonic-gate 	}
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_parts) {
15347c478bd9Sstevel@tonic-gate 		kmem_free(udf_vfsp->udf_parts,
15357c478bd9Sstevel@tonic-gate 		    udf_vfsp->udf_npart * sizeof (*udf_vfsp->udf_parts));
15367c478bd9Sstevel@tonic-gate 	}
15377c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_iseq) {
15387c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_iseq->b_flags |= (B_STALE|B_AGE);
15397c478bd9Sstevel@tonic-gate 		brelse(udf_vfsp->udf_iseq);
15407c478bd9Sstevel@tonic-gate 	}
15417c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_vds) {
15427c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_vds->b_flags |= (B_STALE|B_AGE);
15437c478bd9Sstevel@tonic-gate 		brelse(udf_vfsp->udf_vds);
15447c478bd9Sstevel@tonic-gate 	}
15457c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_vfs)
15467c478bd9Sstevel@tonic-gate 		ud_vfs_remove(udf_vfsp);
15477c478bd9Sstevel@tonic-gate 	if (udf_vfsp->udf_fsmnt) {
15487c478bd9Sstevel@tonic-gate 		kmem_free(udf_vfsp->udf_fsmnt,
15497c478bd9Sstevel@tonic-gate 		    strlen(udf_vfsp->udf_fsmnt) + 1);
15507c478bd9Sstevel@tonic-gate 	}
15517c478bd9Sstevel@tonic-gate 	kmem_free(udf_vfsp, sizeof (*udf_vfsp));
15527c478bd9Sstevel@tonic-gate }
15537c478bd9Sstevel@tonic-gate 
15547c478bd9Sstevel@tonic-gate void
ud_convert_to_superblock(struct udf_vfs * udf_vfsp,struct log_vol_int_desc * lvid)15557c478bd9Sstevel@tonic-gate ud_convert_to_superblock(struct udf_vfs *udf_vfsp,
1556*d9a54dd1SSebastien Roy     struct log_vol_int_desc *lvid)
15577c478bd9Sstevel@tonic-gate {
15587c478bd9Sstevel@tonic-gate 	int32_t i, c;
15597c478bd9Sstevel@tonic-gate 	uint32_t *temp;
15607c478bd9Sstevel@tonic-gate 	struct ud_part *ud_part;
15617c478bd9Sstevel@tonic-gate 	struct lvid_iu *iu;
15627c478bd9Sstevel@tonic-gate 
15637c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_maxuniq = SWAP_64(lvid->lvid_uniqid);
15647c478bd9Sstevel@tonic-gate 	temp = lvid->lvid_fst;
15657c478bd9Sstevel@tonic-gate 	c = SWAP_32(lvid->lvid_npart);
15667c478bd9Sstevel@tonic-gate 	ud_part = udf_vfsp->udf_parts;
15677c478bd9Sstevel@tonic-gate 	for (i = 0; i < c; i++) {
15687c478bd9Sstevel@tonic-gate 		if (i >= udf_vfsp->udf_npart) {
15697c478bd9Sstevel@tonic-gate 			continue;
15707c478bd9Sstevel@tonic-gate 		}
15717c478bd9Sstevel@tonic-gate 		ud_part->udp_nfree =  SWAP_32(temp[i]);
15727c478bd9Sstevel@tonic-gate 		ud_part->udp_nblocks =  SWAP_32(temp[c + i]);
15737c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_freeblks += SWAP_32(temp[i]);
15747c478bd9Sstevel@tonic-gate 		udf_vfsp->udf_totalblks += SWAP_32(temp[c + i]);
15757c478bd9Sstevel@tonic-gate 		ud_part++;
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 	iu = (struct lvid_iu *)(temp + c * 2);
15797c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_nfiles = SWAP_32(iu->lvidiu_nfiles);
15807c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_ndirs = SWAP_32(iu->lvidiu_ndirs);
15817c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_miread = BCD2HEX_16(SWAP_16(iu->lvidiu_mread));
15827c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_miwrite = BCD2HEX_16(SWAP_16(iu->lvidiu_mwrite));
15837c478bd9Sstevel@tonic-gate 	udf_vfsp->udf_mawrite = BCD2HEX_16(SWAP_16(iu->lvidiu_maxwr));
15847c478bd9Sstevel@tonic-gate }
15857c478bd9Sstevel@tonic-gate 
15867c478bd9Sstevel@tonic-gate void
ud_update_superblock(struct vfs * vfsp)15877c478bd9Sstevel@tonic-gate ud_update_superblock(struct vfs *vfsp)
15887c478bd9Sstevel@tonic-gate {
15897c478bd9Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	ud_printf("ud_update_superblock\n");
15927c478bd9Sstevel@tonic-gate 
15937c478bd9Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	mutex_enter(&udf_vfsp->udf_lock);
15967c478bd9Sstevel@tonic-gate 	ud_sbwrite(udf_vfsp);
15977c478bd9Sstevel@tonic-gate 	mutex_exit(&udf_vfsp->udf_lock);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate 
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate #include <sys/dkio.h>
16027c478bd9Sstevel@tonic-gate #include <sys/cdio.h>
16037c478bd9Sstevel@tonic-gate #include <sys/vtoc.h>
16047c478bd9Sstevel@tonic-gate 
16057c478bd9Sstevel@tonic-gate /*
16067c478bd9Sstevel@tonic-gate  * This part of the code is known
16077c478bd9Sstevel@tonic-gate  * to work with only sparc. It needs
16087c478bd9Sstevel@tonic-gate  * to be evluated before using it with x86
16097c478bd9Sstevel@tonic-gate  */
16107c478bd9Sstevel@tonic-gate int32_t
ud_get_last_block(dev_t dev,daddr_t * blkno)16117c478bd9Sstevel@tonic-gate ud_get_last_block(dev_t dev, daddr_t *blkno)
16127c478bd9Sstevel@tonic-gate {
16137c478bd9Sstevel@tonic-gate 	struct vtoc vtoc;
16147c478bd9Sstevel@tonic-gate 	struct dk_cinfo dki_info;
16157c478bd9Sstevel@tonic-gate 	int32_t rval, error;
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	if ((error = cdev_ioctl(dev, DKIOCGVTOC, (intptr_t)&vtoc,
161893239addSjohnlev 	    FKIOCTL|FREAD|FNATIVE, CRED(), &rval)) != 0) {
16197c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Could not get the vtoc information");
16207c478bd9Sstevel@tonic-gate 		return (error);
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	if (vtoc.v_sanity != VTOC_SANE) {
16247c478bd9Sstevel@tonic-gate 		return (EINVAL);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 	if ((error = cdev_ioctl(dev, DKIOCINFO, (intptr_t)&dki_info,
162793239addSjohnlev 	    FKIOCTL|FREAD|FNATIVE, CRED(), &rval)) != 0) {
16287c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Could not get the slice information");
16297c478bd9Sstevel@tonic-gate 		return (error);
16307c478bd9Sstevel@tonic-gate 	}
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if (dki_info.dki_partition > V_NUMPAR) {
16337c478bd9Sstevel@tonic-gate 		return (EINVAL);
16347c478bd9Sstevel@tonic-gate 	}
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	*blkno = vtoc.v_part[dki_info.dki_partition].p_size;
16387c478bd9Sstevel@tonic-gate 
16397c478bd9Sstevel@tonic-gate 	return (0);
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate /* Search sequentially N - 2, N, N - 152, N - 150 for vat icb */
16437c478bd9Sstevel@tonic-gate /*
16447c478bd9Sstevel@tonic-gate  * int32_t ud_sub_blks[] = {2, 0, 152, 150};
16457c478bd9Sstevel@tonic-gate  */
16467c478bd9Sstevel@tonic-gate int32_t ud_sub_blks[] = {152, 150, 2, 0};
16477c478bd9Sstevel@tonic-gate int32_t ud_sub_count = 4;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate /*
16507c478bd9Sstevel@tonic-gate  * Validate the VAT ICB
16517c478bd9Sstevel@tonic-gate  */
16527c478bd9Sstevel@tonic-gate static int32_t
ud_val_get_vat(struct udf_vfs * udf_vfsp,dev_t dev,daddr_t blkno,struct ud_map * udm)16537c478bd9Sstevel@tonic-gate ud_val_get_vat(struct udf_vfs *udf_vfsp, dev_t dev,
1654*d9a54dd1SSebastien Roy     daddr_t blkno, struct ud_map *udm)
16557c478bd9Sstevel@tonic-gate {
16567c478bd9Sstevel@tonic-gate 	struct buf *secbp;
16577c478bd9Sstevel@tonic-gate 	struct file_entry *fe;
16587c478bd9Sstevel@tonic-gate 	int32_t end_loc, i, j, ad_type;
16597c478bd9Sstevel@tonic-gate 	struct short_ad *sad;
16607c478bd9Sstevel@tonic-gate 	struct long_ad *lad;
16617c478bd9Sstevel@tonic-gate 	uint32_t count, blk;
16627c478bd9Sstevel@tonic-gate 	struct ud_part *ud_part;
16637c478bd9Sstevel@tonic-gate 	int err = 0;
16647c478bd9Sstevel@tonic-gate 
16657c478bd9Sstevel@tonic-gate 	end_loc = (blkno >> udf_vfsp->udf_l2d_shift) - 1;
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate 	for (i = 0; i < ud_sub_count; i++) {
16687c478bd9Sstevel@tonic-gate 		udm->udm_vat_icb = end_loc - ud_sub_blks[i];
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 		secbp = ud_bread(dev,
167193239addSjohnlev 		    udm->udm_vat_icb << udf_vfsp->udf_l2d_shift,
167293239addSjohnlev 		    udf_vfsp->udf_lbsize);
16737c478bd9Sstevel@tonic-gate 		ASSERT(secbp->b_un.b_addr);
16747c478bd9Sstevel@tonic-gate 
16757c478bd9Sstevel@tonic-gate 		fe = (struct file_entry *)secbp->b_un.b_addr;
16767c478bd9Sstevel@tonic-gate 		if (ud_verify_tag_and_desc(&fe->fe_tag, UD_FILE_ENTRY, 0,
16777c478bd9Sstevel@tonic-gate 		    0, 0) == 0) {
16787c478bd9Sstevel@tonic-gate 			if (ud_verify_tag_and_desc(&fe->fe_tag, UD_FILE_ENTRY,
16797c478bd9Sstevel@tonic-gate 			    SWAP_32(fe->fe_tag.tag_loc),
16807c478bd9Sstevel@tonic-gate 			    1, udf_vfsp->udf_lbsize) == 0) {
16817c478bd9Sstevel@tonic-gate 				if (fe->fe_icb_tag.itag_ftype == 0) {
16827c478bd9Sstevel@tonic-gate 					break;
16837c478bd9Sstevel@tonic-gate 				}
16847c478bd9Sstevel@tonic-gate 			}
16857c478bd9Sstevel@tonic-gate 		}
16867c478bd9Sstevel@tonic-gate 		secbp->b_flags |= B_AGE | B_STALE;
16877c478bd9Sstevel@tonic-gate 		brelse(secbp);
16887c478bd9Sstevel@tonic-gate 	}
16897c478bd9Sstevel@tonic-gate 	if (i == ud_sub_count) {
16907c478bd9Sstevel@tonic-gate 		return (EINVAL);
16917c478bd9Sstevel@tonic-gate 	}
16927c478bd9Sstevel@tonic-gate 
16937c478bd9Sstevel@tonic-gate 	ad_type = SWAP_16(fe->fe_icb_tag.itag_flags) & 0x3;
16947c478bd9Sstevel@tonic-gate 	if (ad_type == ICB_FLAG_ONE_AD) {
16957c478bd9Sstevel@tonic-gate 		udm->udm_nent = 1;
16967c478bd9Sstevel@tonic-gate 	} else if (ad_type == ICB_FLAG_SHORT_AD) {
16977c478bd9Sstevel@tonic-gate 		udm->udm_nent =
169893239addSjohnlev 		    SWAP_32(fe->fe_len_adesc) / sizeof (struct short_ad);
16997c478bd9Sstevel@tonic-gate 	} else if (ad_type == ICB_FLAG_LONG_AD) {
17007c478bd9Sstevel@tonic-gate 		udm->udm_nent =
170193239addSjohnlev 		    SWAP_32(fe->fe_len_adesc) / sizeof (struct long_ad);
17027c478bd9Sstevel@tonic-gate 	} else {
17037c478bd9Sstevel@tonic-gate 		err = EINVAL;
17047c478bd9Sstevel@tonic-gate 		goto end;
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	udm->udm_count = kmem_zalloc(udm->udm_nent * sizeof (*udm->udm_count),
17087c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
17097c478bd9Sstevel@tonic-gate 	udm->udm_bp = kmem_zalloc(udm->udm_nent * sizeof (*udm->udm_bp),
17107c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
17117c478bd9Sstevel@tonic-gate 	udm->udm_addr = kmem_zalloc(udm->udm_nent * sizeof (*udm->udm_addr),
17127c478bd9Sstevel@tonic-gate 	    KM_SLEEP);
17137c478bd9Sstevel@tonic-gate 
17147c478bd9Sstevel@tonic-gate 	if (ad_type == ICB_FLAG_ONE_AD) {
17157c478bd9Sstevel@tonic-gate 			udm->udm_count[0] = (SWAP_64(fe->fe_info_len) - 36) /
171693239addSjohnlev 			    sizeof (uint32_t);
17177c478bd9Sstevel@tonic-gate 			udm->udm_bp[0] = secbp;
17187c478bd9Sstevel@tonic-gate 			udm->udm_addr[0] = (uint32_t *)
171993239addSjohnlev 			    &fe->fe_spec[SWAP_32(fe->fe_len_ear)];
17207c478bd9Sstevel@tonic-gate 			return (0);
17217c478bd9Sstevel@tonic-gate 	}
17227c478bd9Sstevel@tonic-gate 	for (i = 0; i < udm->udm_nent; i++) {
17237c478bd9Sstevel@tonic-gate 		if (ad_type == ICB_FLAG_SHORT_AD) {
17247c478bd9Sstevel@tonic-gate 			sad = (struct short_ad *)
172593239addSjohnlev 			    (fe->fe_spec + SWAP_32(fe->fe_len_ear));
17267c478bd9Sstevel@tonic-gate 			sad += i;
17277c478bd9Sstevel@tonic-gate 			count = SWAP_32(sad->sad_ext_len);
17287c478bd9Sstevel@tonic-gate 			blk = SWAP_32(sad->sad_ext_loc);
17297c478bd9Sstevel@tonic-gate 		} else {
17307c478bd9Sstevel@tonic-gate 			lad = (struct long_ad *)
173193239addSjohnlev 			    (fe->fe_spec + SWAP_32(fe->fe_len_ear));
17327c478bd9Sstevel@tonic-gate 			lad += i;
17337c478bd9Sstevel@tonic-gate 			count = SWAP_32(lad->lad_ext_len);
17347c478bd9Sstevel@tonic-gate 			blk = SWAP_32(lad->lad_ext_loc);
17357c478bd9Sstevel@tonic-gate 			ASSERT(SWAP_16(lad->lad_ext_prn) == udm->udm_pn);
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 		if ((count & 0x3FFFFFFF) == 0) {
17387c478bd9Sstevel@tonic-gate 			break;
17397c478bd9Sstevel@tonic-gate 		}
17407c478bd9Sstevel@tonic-gate 		if (i < udm->udm_nent - 1) {
17417c478bd9Sstevel@tonic-gate 			udm->udm_count[i] = count / 4;
17427c478bd9Sstevel@tonic-gate 		} else {
17437c478bd9Sstevel@tonic-gate 			udm->udm_count[i] = (count - 36) / 4;
17447c478bd9Sstevel@tonic-gate 		}
17457c478bd9Sstevel@tonic-gate 		ud_part = udf_vfsp->udf_parts;
17467c478bd9Sstevel@tonic-gate 		for (j = 0; j < udf_vfsp->udf_npart; j++) {
17477c478bd9Sstevel@tonic-gate 			if (udm->udm_pn == ud_part->udp_number) {
17487c478bd9Sstevel@tonic-gate 				blk = ud_part->udp_start + blk;
17497c478bd9Sstevel@tonic-gate 				break;
17507c478bd9Sstevel@tonic-gate 			}
17517c478bd9Sstevel@tonic-gate 		}
17527c478bd9Sstevel@tonic-gate 		if (j == udf_vfsp->udf_npart) {
17537c478bd9Sstevel@tonic-gate 			err = EINVAL;
17547c478bd9Sstevel@tonic-gate 			break;
17557c478bd9Sstevel@tonic-gate 		}
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 		count = (count + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
17587c478bd9Sstevel@tonic-gate 		udm->udm_bp[i] = ud_bread(dev,
175993239addSjohnlev 		    blk << udf_vfsp->udf_l2d_shift, count);
17607c478bd9Sstevel@tonic-gate 		if ((udm->udm_bp[i]->b_error != 0) ||
176193239addSjohnlev 		    (udm->udm_bp[i]->b_resid)) {
17627c478bd9Sstevel@tonic-gate 			err = EINVAL;
17637c478bd9Sstevel@tonic-gate 			break;
17647c478bd9Sstevel@tonic-gate 		}
17657c478bd9Sstevel@tonic-gate 		udm->udm_addr[i] = (uint32_t *)udm->udm_bp[i]->b_un.b_addr;
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate end:
17697c478bd9Sstevel@tonic-gate 	if (err)
17707c478bd9Sstevel@tonic-gate 		ud_free_map(udm);
17717c478bd9Sstevel@tonic-gate 	secbp->b_flags |= B_AGE | B_STALE;
17727c478bd9Sstevel@tonic-gate 	brelse(secbp);
17737c478bd9Sstevel@tonic-gate 	return (err);
17747c478bd9Sstevel@tonic-gate }
17757c478bd9Sstevel@tonic-gate 
17767c478bd9Sstevel@tonic-gate int32_t
ud_read_sparing_tbls(struct udf_vfs * udf_vfsp,dev_t dev,struct ud_map * map,struct pmap_typ2 * typ2)17777c478bd9Sstevel@tonic-gate ud_read_sparing_tbls(struct udf_vfs *udf_vfsp,
1778*d9a54dd1SSebastien Roy     dev_t dev, struct ud_map *map, struct pmap_typ2 *typ2)
17797c478bd9Sstevel@tonic-gate {
17807c478bd9Sstevel@tonic-gate 	int32_t index, valid = 0;
17817c478bd9Sstevel@tonic-gate 	uint32_t sz;
17827c478bd9Sstevel@tonic-gate 	struct buf *bp;
17837c478bd9Sstevel@tonic-gate 	struct stbl *stbl;
17847c478bd9Sstevel@tonic-gate 
17857c478bd9Sstevel@tonic-gate 	map->udm_plen = SWAP_16(typ2->map2_pl);
17867c478bd9Sstevel@tonic-gate 	map->udm_nspm = typ2->map2_nst;
17877c478bd9Sstevel@tonic-gate 	map->udm_spsz = SWAP_32(typ2->map2_sest);
17887c478bd9Sstevel@tonic-gate 	sz = (map->udm_spsz + udf_vfsp->udf_lbmask) & ~udf_vfsp->udf_lbmask;
17897c478bd9Sstevel@tonic-gate 	if (sz == 0) {
17907c478bd9Sstevel@tonic-gate 		return (0);
17917c478bd9Sstevel@tonic-gate 	}
17927c478bd9Sstevel@tonic-gate 
17937c478bd9Sstevel@tonic-gate 	for (index = 0; index < map->udm_nspm; index++) {
17947c478bd9Sstevel@tonic-gate 		map->udm_loc[index] = SWAP_32(typ2->map2_st[index]);
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 		bp = ud_bread(dev,
179793239addSjohnlev 		    map->udm_loc[index] << udf_vfsp->udf_l2d_shift, sz);
17987c478bd9Sstevel@tonic-gate 		if ((bp->b_error != 0) || (bp->b_resid)) {
17997c478bd9Sstevel@tonic-gate 			brelse(bp);
18007c478bd9Sstevel@tonic-gate 			continue;
18017c478bd9Sstevel@tonic-gate 		}
18027c478bd9Sstevel@tonic-gate 		stbl = (struct stbl *)bp->b_un.b_addr;
18037c478bd9Sstevel@tonic-gate 		if (strncmp(stbl->stbl_si.reg_id, UDF_SPAR_TBL, 23) != 0) {
18047c478bd9Sstevel@tonic-gate 			printf("Sparing Identifier does not match\n");
18057c478bd9Sstevel@tonic-gate 			bp->b_flags |= B_AGE | B_STALE;
18067c478bd9Sstevel@tonic-gate 			brelse(bp);
18077c478bd9Sstevel@tonic-gate 			continue;
18087c478bd9Sstevel@tonic-gate 		}
18097c478bd9Sstevel@tonic-gate 		map->udm_sbp[index] = bp;
18107c478bd9Sstevel@tonic-gate 		map->udm_spaddr[index] = bp->b_un.b_addr;
18117c478bd9Sstevel@tonic-gate #ifdef	UNDEF
18127c478bd9Sstevel@tonic-gate {
18137c478bd9Sstevel@tonic-gate 	struct stbl_entry *te;
18147c478bd9Sstevel@tonic-gate 	int32_t i, tbl_len;
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 	te = (struct stbl_entry *)&stbl->stbl_entry;
18177c478bd9Sstevel@tonic-gate 	tbl_len = SWAP_16(stbl->stbl_len);
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate 	printf("%x %x\n", tbl_len, SWAP_32(stbl->stbl_seqno));
18207c478bd9Sstevel@tonic-gate 	printf("%x %x\n", bp->b_un.b_addr, te);
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	for (i = 0; i < tbl_len; i++) {
18237c478bd9Sstevel@tonic-gate 		printf("%x %x\n", SWAP_32(te->sent_ol), SWAP_32(te->sent_ml));
18247c478bd9Sstevel@tonic-gate 		te ++;
18257c478bd9Sstevel@tonic-gate 	}
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate #endif
18287c478bd9Sstevel@tonic-gate 		valid ++;
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	if (valid) {
18327c478bd9Sstevel@tonic-gate 		return (0);
18337c478bd9Sstevel@tonic-gate 	}
18347c478bd9Sstevel@tonic-gate 	return (EINVAL);
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18377c478bd9Sstevel@tonic-gate uint32_t
ud_get_lbsize(dev_t dev,uint32_t * loc)18387c478bd9Sstevel@tonic-gate ud_get_lbsize(dev_t dev, uint32_t *loc)
18397c478bd9Sstevel@tonic-gate {
18407c478bd9Sstevel@tonic-gate 	int32_t bsize, shift, index, end_index;
18417c478bd9Sstevel@tonic-gate 	daddr_t last_block;
18427c478bd9Sstevel@tonic-gate 	uint32_t avd_loc;
18437c478bd9Sstevel@tonic-gate 	struct buf *bp;
18447c478bd9Sstevel@tonic-gate 	struct anch_vol_desc_ptr *avdp;
18457c478bd9Sstevel@tonic-gate 	uint32_t session_offset = 0;
18467c478bd9Sstevel@tonic-gate 	int32_t rval;
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	if (ud_get_last_block(dev, &last_block) != 0) {
18497c478bd9Sstevel@tonic-gate 		end_index = 1;
18507c478bd9Sstevel@tonic-gate 	} else {
18517c478bd9Sstevel@tonic-gate 		end_index = 3;
18527c478bd9Sstevel@tonic-gate 	}
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	if (cdev_ioctl(dev, CDROMREADOFFSET, (intptr_t)&session_offset,
185593239addSjohnlev 	    FKIOCTL|FREAD|FNATIVE, CRED(), &rval) != 0) {
18567c478bd9Sstevel@tonic-gate 		session_offset = 0;
18577c478bd9Sstevel@tonic-gate 	}
18587c478bd9Sstevel@tonic-gate 
18597c478bd9Sstevel@tonic-gate 	for (index = 0; index < end_index; index++) {
18607c478bd9Sstevel@tonic-gate 
18617c478bd9Sstevel@tonic-gate 		for (bsize = DEV_BSIZE, shift = 0;
186293239addSjohnlev 		    bsize <= MAXBSIZE; bsize <<= 1, shift++) {
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate 			if (index == 0) {
18657c478bd9Sstevel@tonic-gate 				avd_loc = 256;
18667c478bd9Sstevel@tonic-gate 				if (bsize <= 2048) {
18677c478bd9Sstevel@tonic-gate 					avd_loc +=
186893239addSjohnlev 					    session_offset * 2048 / bsize;
18697c478bd9Sstevel@tonic-gate 				} else {
18707c478bd9Sstevel@tonic-gate 					avd_loc +=
187193239addSjohnlev 					    session_offset / (bsize / 2048);
18727c478bd9Sstevel@tonic-gate 				}
18737c478bd9Sstevel@tonic-gate 			} else if (index == 1) {
18747c478bd9Sstevel@tonic-gate 				avd_loc = last_block - (1 << shift);
18757c478bd9Sstevel@tonic-gate 			} else {
18767c478bd9Sstevel@tonic-gate 				avd_loc = last_block - (256 << shift);
18777c478bd9Sstevel@tonic-gate 			}
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 			bp = ud_bread(dev, avd_loc << shift,
188093239addSjohnlev 			    ANCHOR_VOL_DESC_LEN);
18817c478bd9Sstevel@tonic-gate 			if (geterror(bp) != 0) {
18827c478bd9Sstevel@tonic-gate 				brelse(bp);
18837c478bd9Sstevel@tonic-gate 				continue;
18847c478bd9Sstevel@tonic-gate 			}
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate 			/*
18877c478bd9Sstevel@tonic-gate 			 * Verify if we have avdp here
18887c478bd9Sstevel@tonic-gate 			 */
18897c478bd9Sstevel@tonic-gate 			avdp = (struct anch_vol_desc_ptr *)bp->b_un.b_addr;
18907c478bd9Sstevel@tonic-gate 			if (ud_verify_tag_and_desc(&avdp->avd_tag,
18917c478bd9Sstevel@tonic-gate 			    UD_ANCH_VOL_DESC, avd_loc,
18927c478bd9Sstevel@tonic-gate 			    1, ANCHOR_VOL_DESC_LEN) != 0) {
18937c478bd9Sstevel@tonic-gate 				bp->b_flags |= B_AGE | B_STALE;
18947c478bd9Sstevel@tonic-gate 				brelse(bp);
18957c478bd9Sstevel@tonic-gate 				continue;
18967c478bd9Sstevel@tonic-gate 			}
18977c478bd9Sstevel@tonic-gate 			bp->b_flags |= B_AGE | B_STALE;
18987c478bd9Sstevel@tonic-gate 			brelse(bp);
18997c478bd9Sstevel@tonic-gate 			*loc = avd_loc;
19007c478bd9Sstevel@tonic-gate 			return (bsize);
19017c478bd9Sstevel@tonic-gate 		}
19027c478bd9Sstevel@tonic-gate 	}
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate 	/*
19057c478bd9Sstevel@tonic-gate 	 * Did not find AVD at all the locations
19067c478bd9Sstevel@tonic-gate 	 */
19077c478bd9Sstevel@tonic-gate 	return (0);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate static int
udfinit(int fstype,char * name)19117c478bd9Sstevel@tonic-gate udfinit(int fstype, char *name)
19127c478bd9Sstevel@tonic-gate {
19137c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t udf_vfsops_template[] = {
1914aa59c4cbSrsb 		VFSNAME_MOUNT,		{ .vfs_mount = udf_mount },
1915aa59c4cbSrsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = udf_unmount },
1916aa59c4cbSrsb 		VFSNAME_ROOT,		{ .vfs_root = udf_root },
1917aa59c4cbSrsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = udf_statvfs },
1918aa59c4cbSrsb 		VFSNAME_SYNC,		{ .vfs_sync = udf_sync },
1919aa59c4cbSrsb 		VFSNAME_VGET,		{ .vfs_vget = udf_vget },
1920aa59c4cbSrsb 		VFSNAME_MOUNTROOT,	{ .vfs_mountroot = udf_mountroot },
1921aa59c4cbSrsb 		NULL,			NULL
19227c478bd9Sstevel@tonic-gate 	};
19237c478bd9Sstevel@tonic-gate 	extern struct vnodeops *udf_vnodeops;
19247c478bd9Sstevel@tonic-gate 	extern const fs_operation_def_t udf_vnodeops_template[];
19257c478bd9Sstevel@tonic-gate 	int error;
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 	ud_printf("udfinit\n");
19287c478bd9Sstevel@tonic-gate 
19297c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, udf_vfsops_template, NULL);
19307c478bd9Sstevel@tonic-gate 	if (error != 0) {
19317c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "udfinit: bad vfs ops template");
19327c478bd9Sstevel@tonic-gate 		return (error);
19337c478bd9Sstevel@tonic-gate 	}
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, udf_vnodeops_template, &udf_vnodeops);
19367c478bd9Sstevel@tonic-gate 	if (error != 0) {
19377c478bd9Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
19387c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "udfinit: bad vnode ops template");
19397c478bd9Sstevel@tonic-gate 		return (error);
19407c478bd9Sstevel@tonic-gate 	}
19417c478bd9Sstevel@tonic-gate 
19427c478bd9Sstevel@tonic-gate 	udf_fstype = fstype;
19437c478bd9Sstevel@tonic-gate 
19447c478bd9Sstevel@tonic-gate 	ud_init_inodes();
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	return (0);
19477c478bd9Sstevel@tonic-gate }
1948