xref: /onnv-gate/usr/src/uts/common/fs/udfs/udf_vfsops.c (revision 6855:18f1316f1bed)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51488Srsb  * Common Development and Distribution License (the "License").
61488Srsb  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
226734Sjohnlev  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/time.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/sysmacros.h>
340Sstevel@tonic-gate #include <sys/resource.h>
350Sstevel@tonic-gate #include <sys/signal.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/user.h>
380Sstevel@tonic-gate #include <sys/buf.h>
390Sstevel@tonic-gate #include <sys/vfs.h>
403898Srsb #include <sys/vfs_opreg.h>
410Sstevel@tonic-gate #include <sys/stat.h>
420Sstevel@tonic-gate #include <sys/vnode.h>
430Sstevel@tonic-gate #include <sys/mode.h>
440Sstevel@tonic-gate #include <sys/proc.h>
450Sstevel@tonic-gate #include <sys/disp.h>
460Sstevel@tonic-gate #include <sys/file.h>
470Sstevel@tonic-gate #include <sys/fcntl.h>
480Sstevel@tonic-gate #include <sys/flock.h>
490Sstevel@tonic-gate #include <sys/kmem.h>
500Sstevel@tonic-gate #include <sys/uio.h>
510Sstevel@tonic-gate #include <sys/dnlc.h>
520Sstevel@tonic-gate #include <sys/conf.h>
530Sstevel@tonic-gate #include <sys/errno.h>
540Sstevel@tonic-gate #include <sys/mman.h>
550Sstevel@tonic-gate #include <sys/fbuf.h>
560Sstevel@tonic-gate #include <sys/pathname.h>
570Sstevel@tonic-gate #include <sys/debug.h>
580Sstevel@tonic-gate #include <sys/vmsystm.h>
590Sstevel@tonic-gate #include <sys/cmn_err.h>
600Sstevel@tonic-gate #include <sys/dirent.h>
610Sstevel@tonic-gate #include <sys/errno.h>
620Sstevel@tonic-gate #include <sys/modctl.h>
630Sstevel@tonic-gate #include <sys/statvfs.h>
640Sstevel@tonic-gate #include <sys/mount.h>
650Sstevel@tonic-gate #include <sys/sunddi.h>
660Sstevel@tonic-gate #include <sys/bootconf.h>
670Sstevel@tonic-gate #include <sys/policy.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <vm/hat.h>
700Sstevel@tonic-gate #include <vm/page.h>
710Sstevel@tonic-gate #include <vm/pvn.h>
720Sstevel@tonic-gate #include <vm/as.h>
730Sstevel@tonic-gate #include <vm/seg.h>
740Sstevel@tonic-gate #include <vm/seg_map.h>
750Sstevel@tonic-gate #include <vm/seg_kmem.h>
760Sstevel@tonic-gate #include <vm/seg_vn.h>
770Sstevel@tonic-gate #include <vm/rm.h>
780Sstevel@tonic-gate #include <vm/page.h>
790Sstevel@tonic-gate #include <sys/swap.h>
800Sstevel@tonic-gate #include <sys/mntent.h>
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 
830Sstevel@tonic-gate #include <fs/fs_subr.h>
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 
860Sstevel@tonic-gate #include <sys/fs/udf_volume.h>
870Sstevel@tonic-gate #include <sys/fs/udf_inode.h>
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 
900Sstevel@tonic-gate extern struct vnode *common_specvp(struct vnode *vp);
910Sstevel@tonic-gate 
920Sstevel@tonic-gate extern kmutex_t ud_sync_busy;
930Sstevel@tonic-gate static int32_t ud_mountfs(struct vfs *,
946734Sjohnlev     enum whymountroot, dev_t, char *, struct cred *, int32_t);
950Sstevel@tonic-gate static struct udf_vfs *ud_validate_and_fill_superblock(dev_t,
966734Sjohnlev     int32_t, uint32_t);
970Sstevel@tonic-gate void ud_destroy_fsp(struct udf_vfs *);
980Sstevel@tonic-gate void ud_convert_to_superblock(struct udf_vfs *,
996734Sjohnlev     struct log_vol_int_desc *);
1000Sstevel@tonic-gate void ud_update_superblock(struct vfs *);
1010Sstevel@tonic-gate int32_t ud_get_last_block(dev_t, daddr_t *);
1020Sstevel@tonic-gate static int32_t ud_val_get_vat(struct udf_vfs *,
1036734Sjohnlev     dev_t, daddr_t, struct ud_map *);
1040Sstevel@tonic-gate int32_t ud_read_sparing_tbls(struct udf_vfs *,
1056734Sjohnlev     dev_t, struct ud_map *, struct pmap_typ2 *);
1060Sstevel@tonic-gate uint32_t ud_get_lbsize(dev_t, uint32_t *);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate static int32_t udf_mount(struct vfs *,
1096734Sjohnlev     struct vnode *, struct mounta *, struct cred *);
1100Sstevel@tonic-gate static int32_t udf_unmount(struct vfs *, int, struct cred *);
1110Sstevel@tonic-gate static int32_t udf_root(struct vfs *, struct vnode **);
1120Sstevel@tonic-gate static int32_t udf_statvfs(struct vfs *, struct statvfs64 *);
1130Sstevel@tonic-gate static int32_t udf_sync(struct vfs *, int16_t, struct cred *);
1140Sstevel@tonic-gate static int32_t udf_vget(struct vfs *, struct vnode **, struct fid *);
1150Sstevel@tonic-gate static int32_t udf_mountroot(struct vfs *vfsp, enum whymountroot);
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate static int udfinit(int, char *);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static mntopts_t udfs_mntopts;
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate static vfsdef_t vfw = {
1220Sstevel@tonic-gate 	VFSDEF_VERSION,
1230Sstevel@tonic-gate 	"udfs",
1240Sstevel@tonic-gate 	udfinit,
125*6855Sjohnlev 	VSW_HASPROTO|VSW_CANREMOUNT|VSW_STATS|VSW_CANLOFI,
1260Sstevel@tonic-gate 	&udfs_mntopts
1270Sstevel@tonic-gate };
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate static mntopts_t udfs_mntopts = {
1300Sstevel@tonic-gate 	0,
1310Sstevel@tonic-gate 	NULL
1320Sstevel@tonic-gate };
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate /*
1350Sstevel@tonic-gate  * Module linkage information for the kernel.
1360Sstevel@tonic-gate  */
1370Sstevel@tonic-gate extern struct mod_ops mod_fsops;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate static struct modlfs modlfs = {
1400Sstevel@tonic-gate 	&mod_fsops, "filesystem for UDFS", &vfw
1410Sstevel@tonic-gate };
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static struct modlinkage modlinkage = {
1440Sstevel@tonic-gate 	MODREV_1, (void *)&modlfs, NULL
1450Sstevel@tonic-gate };
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate char _depends_on[] = "fs/specfs";
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate int32_t udf_fstype = -1;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate int
_init()1520Sstevel@tonic-gate _init()
1530Sstevel@tonic-gate {
1540Sstevel@tonic-gate 	return (mod_install(&modlinkage));
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate int
_fini()1580Sstevel@tonic-gate _fini()
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	return (EBUSY);
1610Sstevel@tonic-gate }
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate int
_info(struct modinfo * modinfop)1640Sstevel@tonic-gate _info(struct modinfo *modinfop)
1650Sstevel@tonic-gate {
1660Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /* -------------------- vfs routines -------------------- */
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate /*
1730Sstevel@tonic-gate  * XXX - this appears only to be used by the VM code to handle the case where
1740Sstevel@tonic-gate  * UNIX is running off the mini-root.  That probably wants to be done
1750Sstevel@tonic-gate  * differently.
1760Sstevel@tonic-gate  */
1770Sstevel@tonic-gate struct vnode *rootvp;
1780Sstevel@tonic-gate #ifndef	__lint
1790Sstevel@tonic-gate _NOTE(SCHEME_PROTECTS_DATA("safe sharing", rootvp))
1800Sstevel@tonic-gate #endif
1810Sstevel@tonic-gate static int32_t
udf_mount(struct vfs * vfsp,struct vnode * mvp,struct mounta * uap,struct cred * cr)1820Sstevel@tonic-gate udf_mount(struct vfs *vfsp, struct vnode *mvp,
1830Sstevel@tonic-gate 	struct mounta *uap, struct cred *cr)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	dev_t dev;
1866734Sjohnlev 	struct vnode *lvp = NULL;
1876734Sjohnlev 	struct vnode *svp = NULL;
1880Sstevel@tonic-gate 	struct pathname dpn;
1890Sstevel@tonic-gate 	int32_t error;
1900Sstevel@tonic-gate 	enum whymountroot why;
1910Sstevel@tonic-gate 	int oflag, aflag;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	ud_printf("udf_mount\n");
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	if ((error = secpolicy_fs_mount(cr, mvp, vfsp)) != 0) {
1960Sstevel@tonic-gate 		return (error);
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (mvp->v_type != VDIR) {
2000Sstevel@tonic-gate 		return (ENOTDIR);
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate 	mutex_enter(&mvp->v_lock);
2040Sstevel@tonic-gate 	if ((uap->flags & MS_REMOUNT) == 0 &&
2050Sstevel@tonic-gate 	    (uap->flags & MS_OVERLAY) == 0 &&
2066734Sjohnlev 	    (mvp->v_count != 1 || (mvp->v_flag & VROOT))) {
2076734Sjohnlev 		mutex_exit(&mvp->v_lock);
2086734Sjohnlev 		return (EBUSY);
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 	mutex_exit(&mvp->v_lock);
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (error = pn_get(uap->dir, UIO_USERSPACE, &dpn)) {
2130Sstevel@tonic-gate 		return (error);
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	/*
2176734Sjohnlev 	 * Resolve path name of the file being mounted.
2180Sstevel@tonic-gate 	 */
2190Sstevel@tonic-gate 	if (error = lookupname(uap->spec, UIO_USERSPACE, FOLLOW, NULLVPP,
2206734Sjohnlev 	    &svp)) {
2210Sstevel@tonic-gate 		pn_free(&dpn);
2220Sstevel@tonic-gate 		return (error);
2230Sstevel@tonic-gate 	}
2246734Sjohnlev 
2256734Sjohnlev 	error = vfs_get_lofi(vfsp, &lvp);
2266734Sjohnlev 
2276734Sjohnlev 	if (error > 0) {
2286734Sjohnlev 		if (error == ENOENT)
2296734Sjohnlev 			error = ENODEV;
2300Sstevel@tonic-gate 		goto out;
2316734Sjohnlev 	} else if (error == 0) {
2326734Sjohnlev 		dev = lvp->v_rdev;
2336734Sjohnlev 	} else {
2346734Sjohnlev 		dev = svp->v_rdev;
2356734Sjohnlev 
2366734Sjohnlev 		if (svp->v_type != VBLK) {
2376734Sjohnlev 			error = ENOTBLK;
2386734Sjohnlev 			goto out;
2396734Sjohnlev 		}
2400Sstevel@tonic-gate 	}
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	/*
2430Sstevel@tonic-gate 	 * Ensure that this device isn't already mounted,
2440Sstevel@tonic-gate 	 * unless this is a REMOUNT request
2450Sstevel@tonic-gate 	 */
2460Sstevel@tonic-gate 	if (vfs_devmounting(dev, vfsp)) {
2470Sstevel@tonic-gate 		error = EBUSY;
2480Sstevel@tonic-gate 		goto out;
2490Sstevel@tonic-gate 	}
2500Sstevel@tonic-gate 	if (vfs_devismounted(dev)) {
2510Sstevel@tonic-gate 		if (uap->flags & MS_REMOUNT) {
2520Sstevel@tonic-gate 			why = ROOT_REMOUNT;
2530Sstevel@tonic-gate 		} else {
2540Sstevel@tonic-gate 			error = EBUSY;
2550Sstevel@tonic-gate 			goto out;
2560Sstevel@tonic-gate 		}
2570Sstevel@tonic-gate 	} else {
2580Sstevel@tonic-gate 		why = ROOT_INIT;
2590Sstevel@tonic-gate 	}
2600Sstevel@tonic-gate 	if (getmajor(dev) >= devcnt) {
2610Sstevel@tonic-gate 		error = ENXIO;
2620Sstevel@tonic-gate 		goto out;
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 
2650Sstevel@tonic-gate 	/*
2660Sstevel@tonic-gate 	 * If the device is a tape, mount it read only
2670Sstevel@tonic-gate 	 */
2680Sstevel@tonic-gate 	if (devopsp[getmajor(dev)]->devo_cb_ops->cb_flag & D_TAPE) {
2690Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
2730Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
2740Sstevel@tonic-gate 	}
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * Set mount options.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 	if (uap->flags & MS_RDONLY) {
2800Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RO, NULL, 0);
2810Sstevel@tonic-gate 	}
2820Sstevel@tonic-gate 	if (uap->flags & MS_NOSUID) {
2830Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
2840Sstevel@tonic-gate 	}
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Verify that the caller can open the device special file as
2880Sstevel@tonic-gate 	 * required.  It is not until this moment that we know whether
2890Sstevel@tonic-gate 	 * we're mounting "ro" or not.
2900Sstevel@tonic-gate 	 */
2910Sstevel@tonic-gate 	if ((vfsp->vfs_flag & VFS_RDONLY) != 0) {
2920Sstevel@tonic-gate 		oflag = FREAD;
2930Sstevel@tonic-gate 		aflag = VREAD;
2940Sstevel@tonic-gate 	} else {
2950Sstevel@tonic-gate 		oflag = FREAD | FWRITE;
2960Sstevel@tonic-gate 		aflag = VREAD | VWRITE;
2970Sstevel@tonic-gate 	}
2986734Sjohnlev 
2996734Sjohnlev 	if (lvp == NULL &&
3006734Sjohnlev 	    (error = secpolicy_spec_open(cr, svp, oflag)) != 0)
3010Sstevel@tonic-gate 		goto out;
3026734Sjohnlev 
3036734Sjohnlev 	if ((error = VOP_ACCESS(svp, aflag, 0, cr, NULL)) != 0)
3046734Sjohnlev 		goto out;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/*
3070Sstevel@tonic-gate 	 * Mount the filesystem.
3080Sstevel@tonic-gate 	 */
3090Sstevel@tonic-gate 	error = ud_mountfs(vfsp, why, dev, dpn.pn_path, cr, 0);
3100Sstevel@tonic-gate out:
3116734Sjohnlev 	VN_RELE(svp);
3126734Sjohnlev 	if (lvp != NULL)
3136734Sjohnlev 		VN_RELE(lvp);
3140Sstevel@tonic-gate 	pn_free(&dpn);
3150Sstevel@tonic-gate 	return (error);
3160Sstevel@tonic-gate }
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3210Sstevel@tonic-gate  * unmount the file system pointed
3220Sstevel@tonic-gate  * by vfsp
3230Sstevel@tonic-gate  */
3240Sstevel@tonic-gate /* ARGSUSED */
3250Sstevel@tonic-gate static int32_t
udf_unmount(struct vfs * vfsp,int fflag,struct cred * cr)3260Sstevel@tonic-gate udf_unmount(struct vfs *vfsp, int fflag, struct cred *cr)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
3290Sstevel@tonic-gate 	struct vnode *bvp, *rvp;
3300Sstevel@tonic-gate 	struct ud_inode *rip;
3310Sstevel@tonic-gate 	int32_t flag;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	ud_printf("udf_unmount\n");
3340Sstevel@tonic-gate 
3350Sstevel@tonic-gate 	if (secpolicy_fs_unmount(cr, vfsp) != 0) {
3360Sstevel@tonic-gate 		return (EPERM);
3370Sstevel@tonic-gate 	}
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	/*
3400Sstevel@tonic-gate 	 * forced unmount is not supported by this file system
3410Sstevel@tonic-gate 	 * and thus, ENOTSUP, is being returned.
3420Sstevel@tonic-gate 	 */
3430Sstevel@tonic-gate 	if (fflag & MS_FORCE)
3440Sstevel@tonic-gate 		return (ENOTSUP);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
3470Sstevel@tonic-gate 	flag = !(udf_vfsp->udf_flags & UDF_FL_RDONLY);
3480Sstevel@tonic-gate 	bvp = udf_vfsp->udf_devvp;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	rvp = udf_vfsp->udf_root;
3510Sstevel@tonic-gate 	ASSERT(rvp != NULL);
3520Sstevel@tonic-gate 	rip = VTOI(rvp);
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 	(void) ud_release_cache(udf_vfsp);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	/* Flush all inodes except root */
3580Sstevel@tonic-gate 	if (ud_iflush(vfsp) < 0) {
3590Sstevel@tonic-gate 		return (EBUSY);
3600Sstevel@tonic-gate 	}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 	rw_enter(&rip->i_contents, RW_WRITER);
3630Sstevel@tonic-gate 	(void) ud_syncip(rip, B_INVAL, I_SYNC);
3640Sstevel@tonic-gate 	rw_exit(&rip->i_contents);
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	mutex_enter(&ud_sync_busy);
3670Sstevel@tonic-gate 	if ((udf_vfsp->udf_flags & UDF_FL_RDONLY) == 0) {
3680Sstevel@tonic-gate 		bflush(vfsp->vfs_dev);
3690Sstevel@tonic-gate 		mutex_enter(&udf_vfsp->udf_lock);
3700Sstevel@tonic-gate 		udf_vfsp->udf_clean = UDF_CLEAN;
3710Sstevel@tonic-gate 		mutex_exit(&udf_vfsp->udf_lock);
3720Sstevel@tonic-gate 		ud_update_superblock(vfsp);
3730Sstevel@tonic-gate 	}
3740Sstevel@tonic-gate 	mutex_exit(&ud_sync_busy);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	mutex_destroy(&udf_vfsp->udf_lock);
3770Sstevel@tonic-gate 	mutex_destroy(&udf_vfsp->udf_rename_lck);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	ud_delcache(rip);
3800Sstevel@tonic-gate 	ITIMES(rip);
3810Sstevel@tonic-gate 	VN_RELE(rvp);
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	ud_destroy_fsp(udf_vfsp);
3840Sstevel@tonic-gate 
3855331Samw 	(void) VOP_PUTPAGE(bvp, (offset_t)0, (uint32_t)0, B_INVAL, cr, NULL);
3865331Samw 	(void) VOP_CLOSE(bvp, flag, 1, (offset_t)0, cr, NULL);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	(void) bfinval(vfsp->vfs_dev, 1);
3890Sstevel@tonic-gate 	VN_RELE(bvp);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 	return (0);
3930Sstevel@tonic-gate }
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate /*
3970Sstevel@tonic-gate  * Get the root vp for the
3980Sstevel@tonic-gate  * file system
3990Sstevel@tonic-gate  */
4000Sstevel@tonic-gate static int32_t
udf_root(struct vfs * vfsp,struct vnode ** vpp)4010Sstevel@tonic-gate udf_root(struct vfs *vfsp, struct vnode **vpp)
4020Sstevel@tonic-gate {
4030Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
4040Sstevel@tonic-gate 	struct vnode *vp;
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	ud_printf("udf_root\n");
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 	ASSERT(udf_vfsp != NULL);
4110Sstevel@tonic-gate 	ASSERT(udf_vfsp->udf_root != NULL);
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	vp = udf_vfsp->udf_root;
4140Sstevel@tonic-gate 	VN_HOLD(vp);
4150Sstevel@tonic-gate 	*vpp = vp;
4160Sstevel@tonic-gate 	return (0);
4170Sstevel@tonic-gate }
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate /*
4210Sstevel@tonic-gate  * Get file system statistics.
4220Sstevel@tonic-gate  */
4230Sstevel@tonic-gate static int32_t
udf_statvfs(struct vfs * vfsp,struct statvfs64 * sp)4240Sstevel@tonic-gate udf_statvfs(struct vfs *vfsp, struct statvfs64 *sp)
4250Sstevel@tonic-gate {
4260Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
4270Sstevel@tonic-gate 	struct ud_part *parts;
4280Sstevel@tonic-gate 	dev32_t d32;
4290Sstevel@tonic-gate 	int32_t index;
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	ud_printf("udf_statvfs\n");
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
4340Sstevel@tonic-gate 	(void) bzero(sp, sizeof (struct statvfs64));
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate 	mutex_enter(&udf_vfsp->udf_lock);
4370Sstevel@tonic-gate 	sp->f_bsize = udf_vfsp->udf_lbsize;
4380Sstevel@tonic-gate 	sp->f_frsize = udf_vfsp->udf_lbsize;
4390Sstevel@tonic-gate 	sp->f_blocks = 0;
4400Sstevel@tonic-gate 	sp->f_bfree = 0;
4410Sstevel@tonic-gate 	parts = udf_vfsp->udf_parts;
4420Sstevel@tonic-gate 	for (index = 0; index < udf_vfsp->udf_npart; index++) {
4430Sstevel@tonic-gate 		sp->f_blocks += parts->udp_nblocks;
4440Sstevel@tonic-gate 		sp->f_bfree += parts->udp_nfree;
4450Sstevel@tonic-gate 		parts++;
4460Sstevel@tonic-gate 	}
4470Sstevel@tonic-gate 	sp->f_bavail = sp->f_bfree;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/*
4500Sstevel@tonic-gate 	 * Since there are no real inodes allocated
4510Sstevel@tonic-gate 	 * we will approximate
4520Sstevel@tonic-gate 	 * each new file will occupy :
4530Sstevel@tonic-gate 	 * 38(over head each dent) + MAXNAMLEN / 2 + inode_size(==block size)
4540Sstevel@tonic-gate 	 */
4550Sstevel@tonic-gate 	sp->f_ffree = sp->f_favail =
4566734Sjohnlev 	    (sp->f_bavail * sp->f_bsize) / (146 + sp->f_bsize);
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 	/*
4590Sstevel@tonic-gate 	 * The total number of inodes is
4600Sstevel@tonic-gate 	 * the sum of files + directories + free inodes
4610Sstevel@tonic-gate 	 */
4626734Sjohnlev 	sp->f_files = sp->f_ffree + udf_vfsp->udf_nfiles + udf_vfsp->udf_ndirs;
4630Sstevel@tonic-gate 	(void) cmpldev(&d32, vfsp->vfs_dev);
4640Sstevel@tonic-gate 	sp->f_fsid = d32;
4650Sstevel@tonic-gate 	(void) strcpy(sp->f_basetype, vfssw[vfsp->vfs_fstype].vsw_name);
4660Sstevel@tonic-gate 	sp->f_flag = vf_to_stf(vfsp->vfs_flag);
4670Sstevel@tonic-gate 	sp->f_namemax = MAXNAMLEN;
4680Sstevel@tonic-gate 	(void) strcpy(sp->f_fstr, udf_vfsp->udf_volid);
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 	mutex_exit(&udf_vfsp->udf_lock);
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	return (0);
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 
4760Sstevel@tonic-gate /*
4770Sstevel@tonic-gate  * Flush any pending I/O to file system vfsp.
4780Sstevel@tonic-gate  * The ud_update() routine will only flush *all* udf files.
4790Sstevel@tonic-gate  */
4800Sstevel@tonic-gate /*ARGSUSED*/
4810Sstevel@tonic-gate /* ARGSUSED */
4820Sstevel@tonic-gate static int32_t
udf_sync(struct vfs * vfsp,int16_t flag,struct cred * cr)4830Sstevel@tonic-gate udf_sync(struct vfs *vfsp, int16_t flag, struct cred *cr)
4840Sstevel@tonic-gate {
4850Sstevel@tonic-gate 	ud_printf("udf_sync\n");
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	ud_update(flag);
4880Sstevel@tonic-gate 	return (0);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate /* ARGSUSED */
4940Sstevel@tonic-gate static int32_t
udf_vget(struct vfs * vfsp,struct vnode ** vpp,struct fid * fidp)4950Sstevel@tonic-gate udf_vget(struct vfs *vfsp,
4960Sstevel@tonic-gate 	struct vnode **vpp, struct fid *fidp)
4970Sstevel@tonic-gate {
4980Sstevel@tonic-gate 	int32_t error = 0;
4990Sstevel@tonic-gate 	struct udf_fid *udfid;
5000Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
5010Sstevel@tonic-gate 	struct ud_inode *ip;
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate 	ud_printf("udf_vget\n");
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
5060Sstevel@tonic-gate 	if (udf_vfsp == NULL) {
5070Sstevel@tonic-gate 		*vpp = NULL;
5080Sstevel@tonic-gate 		return (0);
5090Sstevel@tonic-gate 	}
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	udfid = (struct udf_fid *)fidp;
5120Sstevel@tonic-gate 	if ((error = ud_iget(vfsp, udfid->udfid_prn,
5136734Sjohnlev 	    udfid->udfid_icb_lbn, &ip, NULL, CRED())) != 0) {
5140Sstevel@tonic-gate 		*vpp = NULL;
5150Sstevel@tonic-gate 		return (error);
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	rw_enter(&ip->i_contents, RW_READER);
5190Sstevel@tonic-gate 	if ((udfid->udfid_uinq_lo != (ip->i_uniqid & 0xffffffff)) ||
5206734Sjohnlev 	    (udfid->udfid_prn != ip->i_icb_prn)) {
5210Sstevel@tonic-gate 		rw_exit(&ip->i_contents);
5220Sstevel@tonic-gate 		VN_RELE(ITOV(ip));
5230Sstevel@tonic-gate 		*vpp = NULL;
5240Sstevel@tonic-gate 		return (EINVAL);
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 	rw_exit(&ip->i_contents);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate 	*vpp = ITOV(ip);
5290Sstevel@tonic-gate 	return (0);
5300Sstevel@tonic-gate }
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * Mount root file system.
5350Sstevel@tonic-gate  * "why" is ROOT_INIT on initial call, ROOT_REMOUNT if called to
5360Sstevel@tonic-gate  * remount the root file system, and ROOT_UNMOUNT if called to
5370Sstevel@tonic-gate  * unmount the root (e.g., as part of a system shutdown).
5380Sstevel@tonic-gate  *
5390Sstevel@tonic-gate  * XXX - this may be partially machine-dependent; it, along with the VFS_SWAPVP
5400Sstevel@tonic-gate  * operation, goes along with auto-configuration.  A mechanism should be
5410Sstevel@tonic-gate  * provided by which machine-INdependent code in the kernel can say "get me the
5420Sstevel@tonic-gate  * right root file system" and "get me the right initial swap area", and have
5430Sstevel@tonic-gate  * that done in what may well be a machine-dependent fashion.
5440Sstevel@tonic-gate  * Unfortunately, it is also file-system-type dependent (NFS gets it via
5450Sstevel@tonic-gate  * bootparams calls, UFS gets it from various and sundry machine-dependent
5460Sstevel@tonic-gate  * mechanisms, as SPECFS does for swap).
5470Sstevel@tonic-gate  */
5480Sstevel@tonic-gate /* ARGSUSED */
5490Sstevel@tonic-gate static int32_t
udf_mountroot(struct vfs * vfsp,enum whymountroot why)5500Sstevel@tonic-gate udf_mountroot(struct vfs *vfsp, enum whymountroot why)
5510Sstevel@tonic-gate {
5520Sstevel@tonic-gate 	dev_t rootdev;
5530Sstevel@tonic-gate 	static int32_t udf_rootdone = 0;
5540Sstevel@tonic-gate 	struct vnode *vp = NULL;
5550Sstevel@tonic-gate 	int32_t ovflags, error;
5560Sstevel@tonic-gate 	ud_printf("udf_mountroot\n");
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	if (why == ROOT_INIT) {
5590Sstevel@tonic-gate 		if (udf_rootdone++) {
5600Sstevel@tonic-gate 			return (EBUSY);
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 		rootdev = getrootdev();
5630Sstevel@tonic-gate 		if (rootdev == (dev_t)NODEV) {
5640Sstevel@tonic-gate 			return (ENODEV);
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 		vfsp->vfs_dev = rootdev;
5670Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_RDONLY;
5680Sstevel@tonic-gate 	} else if (why == ROOT_REMOUNT) {
5690Sstevel@tonic-gate 		vp = ((struct udf_vfs *)vfsp->vfs_data)->udf_devvp;
5700Sstevel@tonic-gate 		(void) dnlc_purge_vfsp(vfsp, 0);
5710Sstevel@tonic-gate 		vp = common_specvp(vp);
5720Sstevel@tonic-gate 		(void) VOP_PUTPAGE(vp, (offset_t)0,
5736734Sjohnlev 		    (uint32_t)0, B_INVAL, CRED(), NULL);
5740Sstevel@tonic-gate 		binval(vfsp->vfs_dev);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate 		ovflags = vfsp->vfs_flag;
5770Sstevel@tonic-gate 		vfsp->vfs_flag &= ~VFS_RDONLY;
5780Sstevel@tonic-gate 		vfsp->vfs_flag |= VFS_REMOUNT;
5790Sstevel@tonic-gate 		rootdev = vfsp->vfs_dev;
5800Sstevel@tonic-gate 	} else if (why == ROOT_UNMOUNT) {
5810Sstevel@tonic-gate 		ud_update(0);
5820Sstevel@tonic-gate 		vp = ((struct udf_vfs *)vfsp->vfs_data)->udf_devvp;
5830Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, FREAD|FWRITE, 1,
5846734Sjohnlev 		    (offset_t)0, CRED(), NULL);
5850Sstevel@tonic-gate 		return (0);
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if ((error = vfs_lock(vfsp)) != 0) {
5890Sstevel@tonic-gate 		return (error);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	error = ud_mountfs(vfsp, why, rootdev, "/", CRED(), 1);
5930Sstevel@tonic-gate 	if (error) {
5940Sstevel@tonic-gate 		vfs_unlock(vfsp);
5950Sstevel@tonic-gate 		if (why == ROOT_REMOUNT) {
5960Sstevel@tonic-gate 			vfsp->vfs_flag = ovflags;
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 		if (rootvp) {
5990Sstevel@tonic-gate 			VN_RELE(rootvp);
6000Sstevel@tonic-gate 			rootvp = (struct vnode *)0;
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 		return (error);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	if (why == ROOT_INIT) {
6060Sstevel@tonic-gate 		vfs_add((struct vnode *)0, vfsp,
6076734Sjohnlev 		    (vfsp->vfs_flag & VFS_RDONLY) ? MS_RDONLY : 0);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	vfs_unlock(vfsp);
6100Sstevel@tonic-gate 	return (0);
6110Sstevel@tonic-gate }
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 
6140Sstevel@tonic-gate /* ------------------------- local routines ------------------------- */
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate static int32_t
ud_mountfs(struct vfs * vfsp,enum whymountroot why,dev_t dev,char * name,struct cred * cr,int32_t isroot)6180Sstevel@tonic-gate ud_mountfs(struct vfs *vfsp,
6190Sstevel@tonic-gate 	enum whymountroot why, dev_t dev, char *name,
6200Sstevel@tonic-gate 	struct cred *cr, int32_t isroot)
6210Sstevel@tonic-gate {
6220Sstevel@tonic-gate 	struct vnode *devvp = NULL;
6230Sstevel@tonic-gate 	int32_t error = 0;
6240Sstevel@tonic-gate 	int32_t needclose = 0;
6250Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp = NULL;
6260Sstevel@tonic-gate 	struct log_vol_int_desc *lvid;
6270Sstevel@tonic-gate 	struct ud_inode *rip = NULL;
6280Sstevel@tonic-gate 	struct vnode *rvp = NULL;
6290Sstevel@tonic-gate 	int32_t i, lbsize;
6300Sstevel@tonic-gate 	uint32_t avd_loc;
6310Sstevel@tonic-gate 	struct ud_map *map;
6320Sstevel@tonic-gate 	int32_t	desc_len;
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	ud_printf("ud_mountfs\n");
6350Sstevel@tonic-gate 
6360Sstevel@tonic-gate 	if (why == ROOT_INIT) {
6370Sstevel@tonic-gate 		/*
6380Sstevel@tonic-gate 		 * Open the device.
6390Sstevel@tonic-gate 		 */
6400Sstevel@tonic-gate 		devvp = makespecvp(dev, VBLK);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		/*
6430Sstevel@tonic-gate 		 * Open block device mounted on.
6440Sstevel@tonic-gate 		 * When bio is fixed for vnodes this can all be vnode
6450Sstevel@tonic-gate 		 * operations.
6460Sstevel@tonic-gate 		 */
6470Sstevel@tonic-gate 		error = VOP_OPEN(&devvp,
6485331Samw 		    (vfsp->vfs_flag & VFS_RDONLY) ? FREAD : FREAD|FWRITE,
6495331Samw 		    cr, NULL);
6500Sstevel@tonic-gate 		if (error) {
6510Sstevel@tonic-gate 			goto out;
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 		needclose = 1;
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		/*
6560Sstevel@tonic-gate 		 * Refuse to go any further if this
6570Sstevel@tonic-gate 		 * device is being used for swapping.
6580Sstevel@tonic-gate 		 */
6590Sstevel@tonic-gate 		if (IS_SWAPVP(devvp)) {
6600Sstevel@tonic-gate 			error = EBUSY;
6610Sstevel@tonic-gate 			goto out;
6620Sstevel@tonic-gate 		}
6630Sstevel@tonic-gate 	}
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate 	/*
6660Sstevel@tonic-gate 	 * check for dev already mounted on
6670Sstevel@tonic-gate 	 */
6680Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_REMOUNT) {
6690Sstevel@tonic-gate 		struct tag *ttag;
6700Sstevel@tonic-gate 		int32_t index, count;
6710Sstevel@tonic-gate 		struct buf *tpt = 0;
6720Sstevel@tonic-gate 		caddr_t addr;
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		/* cannot remount to RDONLY */
6760Sstevel@tonic-gate 		if (vfsp->vfs_flag & VFS_RDONLY) {
6770Sstevel@tonic-gate 			return (EINVAL);
6780Sstevel@tonic-gate 		}
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 		if (vfsp->vfs_dev != dev) {
6810Sstevel@tonic-gate 			return (EINVAL);
6820Sstevel@tonic-gate 		}
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 		udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
6850Sstevel@tonic-gate 		devvp = udf_vfsp->udf_devvp;
6860Sstevel@tonic-gate 
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * fsck may have altered the file system; discard
6890Sstevel@tonic-gate 		 * as much incore data as possible.  Don't flush
6900Sstevel@tonic-gate 		 * if this is a rw to rw remount; it's just resetting
6910Sstevel@tonic-gate 		 * the options.
6920Sstevel@tonic-gate 		 */
6930Sstevel@tonic-gate 		if (udf_vfsp->udf_flags & UDF_FL_RDONLY) {
6940Sstevel@tonic-gate 			(void) dnlc_purge_vfsp(vfsp, 0);
6950Sstevel@tonic-gate 			(void) VOP_PUTPAGE(devvp, (offset_t)0, (uint_t)0,
6966734Sjohnlev 			    B_INVAL, CRED(), NULL);
6970Sstevel@tonic-gate 			(void) ud_iflush(vfsp);
6980Sstevel@tonic-gate 			bflush(dev);
6990Sstevel@tonic-gate 			binval(dev);
7000Sstevel@tonic-gate 		}
7010Sstevel@tonic-gate 
7020Sstevel@tonic-gate 		/*
7030Sstevel@tonic-gate 		 * We could read UDF1.50 and write UDF1.50 only
7040Sstevel@tonic-gate 		 * disallow mount of any highier version
7050Sstevel@tonic-gate 		 */
7060Sstevel@tonic-gate 		if ((udf_vfsp->udf_miread > UDF_150) ||
7076734Sjohnlev 		    (udf_vfsp->udf_miwrite > UDF_150)) {
7080Sstevel@tonic-gate 			error = EINVAL;
7090Sstevel@tonic-gate 			goto remountout;
7100Sstevel@tonic-gate 		}
7110Sstevel@tonic-gate 
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * read/write to read/write; all done
7140Sstevel@tonic-gate 		 */
7150Sstevel@tonic-gate 		if (udf_vfsp->udf_flags & UDF_FL_RW) {
7160Sstevel@tonic-gate 			goto remountout;
7170Sstevel@tonic-gate 		}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 		/*
7200Sstevel@tonic-gate 		 * Does the media type allow a writable mount
7210Sstevel@tonic-gate 		 */
7220Sstevel@tonic-gate 		if (udf_vfsp->udf_mtype != UDF_MT_OW) {
7230Sstevel@tonic-gate 			error = EINVAL;
7240Sstevel@tonic-gate 			goto remountout;
7250Sstevel@tonic-gate 		}
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 		/*
7280Sstevel@tonic-gate 		 * Read the metadata
7290Sstevel@tonic-gate 		 * and check if it is possible to
7300Sstevel@tonic-gate 		 * mount in rw mode
7310Sstevel@tonic-gate 		 */
7320Sstevel@tonic-gate 		tpt = ud_bread(vfsp->vfs_dev,
7336734Sjohnlev 		    udf_vfsp->udf_iseq_loc << udf_vfsp->udf_l2d_shift,
7346734Sjohnlev 		    udf_vfsp->udf_iseq_len);
7350Sstevel@tonic-gate 		if (tpt->b_flags & B_ERROR) {
7360Sstevel@tonic-gate 			error = EIO;
7370Sstevel@tonic-gate 			goto remountout;
7380Sstevel@tonic-gate 		}
7390Sstevel@tonic-gate 		count = udf_vfsp->udf_iseq_len / DEV_BSIZE;
7400Sstevel@tonic-gate 		addr = tpt->b_un.b_addr;
7410Sstevel@tonic-gate 		for (index = 0; index < count; index ++) {
7420Sstevel@tonic-gate 			ttag = (struct tag *)(addr + index * DEV_BSIZE);
7430Sstevel@tonic-gate 			desc_len = udf_vfsp->udf_iseq_len - (index * DEV_BSIZE);
7440Sstevel@tonic-gate 			if (ud_verify_tag_and_desc(ttag, UD_LOG_VOL_INT,
7450Sstevel@tonic-gate 			    udf_vfsp->udf_iseq_loc +
7460Sstevel@tonic-gate 			    (index >> udf_vfsp->udf_l2d_shift),
7470Sstevel@tonic-gate 			    1, desc_len) == 0) {
7480Sstevel@tonic-gate 				struct log_vol_int_desc *lvid;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 				lvid = (struct log_vol_int_desc *)ttag;
7510Sstevel@tonic-gate 
7520Sstevel@tonic-gate 				if (SWAP_32(lvid->lvid_int_type) !=
7536734Sjohnlev 				    LOG_VOL_CLOSE_INT) {
7540Sstevel@tonic-gate 					error = EINVAL;
7550Sstevel@tonic-gate 					goto remountout;
7560Sstevel@tonic-gate 				}
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 				/*
7590Sstevel@tonic-gate 				 * Copy new data to old data
7600Sstevel@tonic-gate 				 */
7610Sstevel@tonic-gate 				bcopy(udf_vfsp->udf_iseq->b_un.b_addr,
7626734Sjohnlev 				    tpt->b_un.b_addr, udf_vfsp->udf_iseq_len);
7630Sstevel@tonic-gate 				break;
7640Sstevel@tonic-gate 			}
7650Sstevel@tonic-gate 		}
7660Sstevel@tonic-gate 
7670Sstevel@tonic-gate 		udf_vfsp->udf_flags = UDF_FL_RW;
7680Sstevel@tonic-gate 
7690Sstevel@tonic-gate 		mutex_enter(&udf_vfsp->udf_lock);
7700Sstevel@tonic-gate 		ud_sbwrite(udf_vfsp);
7710Sstevel@tonic-gate 		mutex_exit(&udf_vfsp->udf_lock);
7720Sstevel@tonic-gate remountout:
7730Sstevel@tonic-gate 		if (tpt != NULL) {
7740Sstevel@tonic-gate 			tpt->b_flags = B_AGE | B_STALE;
7750Sstevel@tonic-gate 			brelse(tpt);
7760Sstevel@tonic-gate 		}
7770Sstevel@tonic-gate 		return (error);
7780Sstevel@tonic-gate 	}
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	ASSERT(devvp != 0);
7810Sstevel@tonic-gate 	/*
7820Sstevel@tonic-gate 	 * Flush back any dirty pages on the block device to
7830Sstevel@tonic-gate 	 * try and keep the buffer cache in sync with the page
7840Sstevel@tonic-gate 	 * cache if someone is trying to use block devices when
7850Sstevel@tonic-gate 	 * they really should be using the raw device.
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	(void) VOP_PUTPAGE(common_specvp(devvp), (offset_t)0,
7885331Samw 	    (uint32_t)0, B_INVAL, cr, NULL);
7890Sstevel@tonic-gate 
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	/*
7920Sstevel@tonic-gate 	 * Check if the file system
7930Sstevel@tonic-gate 	 * is a valid udfs and fill
7940Sstevel@tonic-gate 	 * the required fields in udf_vfs
7950Sstevel@tonic-gate 	 */
7960Sstevel@tonic-gate #ifndef	__lint
7970Sstevel@tonic-gate 	_NOTE(NO_COMPETING_THREADS_NOW);
7980Sstevel@tonic-gate #endif
7990Sstevel@tonic-gate 
8000Sstevel@tonic-gate 	if ((lbsize = ud_get_lbsize(dev, &avd_loc)) == 0) {
8010Sstevel@tonic-gate 		error = EINVAL;
8020Sstevel@tonic-gate 		goto out;
8030Sstevel@tonic-gate 	}
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	udf_vfsp = ud_validate_and_fill_superblock(dev, lbsize, avd_loc);
8060Sstevel@tonic-gate 	if (udf_vfsp == NULL) {
8070Sstevel@tonic-gate 		error = EINVAL;
8080Sstevel@tonic-gate 		goto out;
8090Sstevel@tonic-gate 	}
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate 	/*
8120Sstevel@tonic-gate 	 * Fill in vfs private data
8130Sstevel@tonic-gate 	 */
8140Sstevel@tonic-gate 	vfsp->vfs_fstype = udf_fstype;
8150Sstevel@tonic-gate 	vfs_make_fsid(&vfsp->vfs_fsid, dev, udf_fstype);
8160Sstevel@tonic-gate 	vfsp->vfs_data = (caddr_t)udf_vfsp;
8170Sstevel@tonic-gate 	vfsp->vfs_dev = dev;
8180Sstevel@tonic-gate 	vfsp->vfs_flag |= VFS_NOTRUNC;
8190Sstevel@tonic-gate 	udf_vfsp->udf_devvp = devvp;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate 	udf_vfsp->udf_fsmnt = kmem_zalloc(strlen(name) + 1, KM_SLEEP);
8220Sstevel@tonic-gate 	(void) strcpy(udf_vfsp->udf_fsmnt, name);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	udf_vfsp->udf_vfs = vfsp;
8250Sstevel@tonic-gate 	udf_vfsp->udf_rdclustsz = udf_vfsp->udf_wrclustsz = maxphys;
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate 	udf_vfsp->udf_mod = 0;
8280Sstevel@tonic-gate 
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate 	lvid = udf_vfsp->udf_lvid;
8310Sstevel@tonic-gate 	if (vfsp->vfs_flag & VFS_RDONLY) {
8320Sstevel@tonic-gate 		/*
8330Sstevel@tonic-gate 		 * We could read only UDF1.50
8340Sstevel@tonic-gate 		 * disallow mount of any highier version
8350Sstevel@tonic-gate 		 */
8360Sstevel@tonic-gate 		if (udf_vfsp->udf_miread > UDF_150) {
8370Sstevel@tonic-gate 			error = EINVAL;
8380Sstevel@tonic-gate 			goto out;
8390Sstevel@tonic-gate 		}
8400Sstevel@tonic-gate 		udf_vfsp->udf_flags = UDF_FL_RDONLY;
8410Sstevel@tonic-gate 		if (SWAP_32(lvid->lvid_int_type) == LOG_VOL_CLOSE_INT) {
8420Sstevel@tonic-gate 			udf_vfsp->udf_clean = UDF_CLEAN;
8430Sstevel@tonic-gate 		} else {
8440Sstevel@tonic-gate 			/* Do we have a VAT at the end of the recorded media */
8450Sstevel@tonic-gate 			map = udf_vfsp->udf_maps;
8460Sstevel@tonic-gate 			for (i = 0; i < udf_vfsp->udf_nmaps; i++) {
8470Sstevel@tonic-gate 				if (map->udm_flags & UDM_MAP_VPM) {
8480Sstevel@tonic-gate 					break;
8490Sstevel@tonic-gate 				}
8500Sstevel@tonic-gate 				map++;
8510Sstevel@tonic-gate 			}
8520Sstevel@tonic-gate 			if (i == udf_vfsp->udf_nmaps) {
8530Sstevel@tonic-gate 				error = ENOSPC;
8540Sstevel@tonic-gate 				goto out;
8550Sstevel@tonic-gate 			}
8560Sstevel@tonic-gate 			udf_vfsp->udf_clean = UDF_CLEAN;
8570Sstevel@tonic-gate 		}
8580Sstevel@tonic-gate 	} else {
8590Sstevel@tonic-gate 		/*
8600Sstevel@tonic-gate 		 * We could read UDF1.50 and write UDF1.50 only
8610Sstevel@tonic-gate 		 * disallow mount of any highier version
8620Sstevel@tonic-gate 		 */
8630Sstevel@tonic-gate 		if ((udf_vfsp->udf_miread > UDF_150) ||
8646734Sjohnlev 		    (udf_vfsp->udf_miwrite > UDF_150)) {
8650Sstevel@tonic-gate 			error = EINVAL;
8660Sstevel@tonic-gate 			goto out;
8670Sstevel@tonic-gate 		}
8680Sstevel@tonic-gate 		/*
8690Sstevel@tonic-gate 		 * Check if the media allows
8700Sstevel@tonic-gate 		 * us to mount read/write
8710Sstevel@tonic-gate 		 */
8720Sstevel@tonic-gate 		if (udf_vfsp->udf_mtype != UDF_MT_OW) {
8730Sstevel@tonic-gate 			error = EACCES;
8740Sstevel@tonic-gate 			goto out;
8750Sstevel@tonic-gate 		}
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 		/*
8780Sstevel@tonic-gate 		 * Check if we have VAT on a writable media
8790Sstevel@tonic-gate 		 * we cannot use the media in presence of VAT
8800Sstevel@tonic-gate 		 * Dent RW mount.
8810Sstevel@tonic-gate 		 */
8820Sstevel@tonic-gate 		map = udf_vfsp->udf_maps;
8830Sstevel@tonic-gate 		ASSERT(map != NULL);
8840Sstevel@tonic-gate 		for (i = 0; i < udf_vfsp->udf_nmaps; i++) {
8850Sstevel@tonic-gate 			if (map->udm_flags & UDM_MAP_VPM) {
8860Sstevel@tonic-gate 				error = EACCES;
8870Sstevel@tonic-gate 				goto out;
8880Sstevel@tonic-gate 			}
8890Sstevel@tonic-gate 			map++;
8900Sstevel@tonic-gate 		}
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate 		/*
8930Sstevel@tonic-gate 		 * Check if the domain Id allows
8940Sstevel@tonic-gate 		 * us to write
8950Sstevel@tonic-gate 		 */
8960Sstevel@tonic-gate 		if (udf_vfsp->udf_lvd->lvd_dom_id.reg_ids[2] & 0x3) {
8970Sstevel@tonic-gate 			error = EACCES;
8980Sstevel@tonic-gate 			goto out;
8990Sstevel@tonic-gate 		}
9000Sstevel@tonic-gate 		udf_vfsp->udf_flags = UDF_FL_RW;
9010Sstevel@tonic-gate 
9020Sstevel@tonic-gate 		if (SWAP_32(lvid->lvid_int_type) == LOG_VOL_CLOSE_INT) {
9030Sstevel@tonic-gate 			udf_vfsp->udf_clean = UDF_CLEAN;
9040Sstevel@tonic-gate 		} else {
9050Sstevel@tonic-gate 			if (isroot) {
9060Sstevel@tonic-gate 				udf_vfsp->udf_clean = UDF_DIRTY;
9070Sstevel@tonic-gate 			} else {
9080Sstevel@tonic-gate 				error = ENOSPC;
9090Sstevel@tonic-gate 				goto out;
9100Sstevel@tonic-gate 			}
9110Sstevel@tonic-gate 		}
9120Sstevel@tonic-gate 	}
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	mutex_init(&udf_vfsp->udf_lock, NULL, MUTEX_DEFAULT, NULL);
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	mutex_init(&udf_vfsp->udf_rename_lck, NULL, MUTEX_DEFAULT, NULL);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate #ifndef	__lint
9190Sstevel@tonic-gate 	_NOTE(COMPETING_THREADS_NOW);
9200Sstevel@tonic-gate #endif
9210Sstevel@tonic-gate 	if (error = ud_iget(vfsp, udf_vfsp->udf_ricb_prn,
9226734Sjohnlev 	    udf_vfsp->udf_ricb_loc, &rip, NULL, cr)) {
9230Sstevel@tonic-gate 		mutex_destroy(&udf_vfsp->udf_lock);
9240Sstevel@tonic-gate 		goto out;
9250Sstevel@tonic-gate 	}
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 	/*
9290Sstevel@tonic-gate 	 * Get the root inode and
9300Sstevel@tonic-gate 	 * initialize the root vnode
9310Sstevel@tonic-gate 	 */
9320Sstevel@tonic-gate 	rvp = ITOV(rip);
9330Sstevel@tonic-gate 	mutex_enter(&rvp->v_lock);
9340Sstevel@tonic-gate 	rvp->v_flag |= VROOT;
9350Sstevel@tonic-gate 	mutex_exit(&rvp->v_lock);
9360Sstevel@tonic-gate 	udf_vfsp->udf_root = rvp;
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 
9390Sstevel@tonic-gate 	if (why == ROOT_INIT && isroot)
9400Sstevel@tonic-gate 		rootvp = devvp;
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate 	ud_vfs_add(udf_vfsp);
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	if (udf_vfsp->udf_flags == UDF_FL_RW) {
9450Sstevel@tonic-gate 		udf_vfsp->udf_clean = UDF_DIRTY;
9460Sstevel@tonic-gate 		ud_update_superblock(vfsp);
9470Sstevel@tonic-gate 	}
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	return (0);
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate out:
9520Sstevel@tonic-gate 	ud_destroy_fsp(udf_vfsp);
9530Sstevel@tonic-gate 	if (needclose) {
9540Sstevel@tonic-gate 		(void) VOP_CLOSE(devvp, (vfsp->vfs_flag & VFS_RDONLY) ?
9556734Sjohnlev 		    FREAD : FREAD|FWRITE, 1, (offset_t)0, cr, NULL);
9560Sstevel@tonic-gate 		bflush(dev);
9570Sstevel@tonic-gate 		binval(dev);
9580Sstevel@tonic-gate 	}
9590Sstevel@tonic-gate 	VN_RELE(devvp);
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	return (error);
9620Sstevel@tonic-gate }
9630Sstevel@tonic-gate 
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate static struct udf_vfs *
ud_validate_and_fill_superblock(dev_t dev,int32_t bsize,uint32_t avd_loc)9660Sstevel@tonic-gate ud_validate_and_fill_superblock(dev_t dev, int32_t bsize, uint32_t avd_loc)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	int32_t error, count, index, shift;
9690Sstevel@tonic-gate 	uint32_t dummy, vds_loc;
9700Sstevel@tonic-gate 	caddr_t addr;
9710Sstevel@tonic-gate 	daddr_t blkno, lblkno;
9720Sstevel@tonic-gate 	struct buf *secbp, *bp;
9730Sstevel@tonic-gate 	struct tag *ttag;
9740Sstevel@tonic-gate 	struct anch_vol_desc_ptr *avdp;
9750Sstevel@tonic-gate 	struct file_set_desc *fsd;
9760Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp = NULL;
9770Sstevel@tonic-gate 	struct pmap_hdr *hdr;
9780Sstevel@tonic-gate 	struct pmap_typ1 *typ1;
9790Sstevel@tonic-gate 	struct pmap_typ2 *typ2;
9800Sstevel@tonic-gate 	struct ud_map *map;
9810Sstevel@tonic-gate 	int32_t	desc_len;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	ud_printf("ud_validate_and_fill_superblock\n");
9840Sstevel@tonic-gate 
9850Sstevel@tonic-gate 	if (bsize < DEV_BSIZE) {
9860Sstevel@tonic-gate 		return (NULL);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 	shift = 0;
9890Sstevel@tonic-gate 	while ((bsize >> shift) > DEV_BSIZE) {
9900Sstevel@tonic-gate 		shift++;
9910Sstevel@tonic-gate 	}
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	/*
9940Sstevel@tonic-gate 	 * Read Anchor Volume Descriptor
9950Sstevel@tonic-gate 	 * Verify it and get the location of
9960Sstevel@tonic-gate 	 * Main Volume Descriptor Sequence
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate 	secbp = ud_bread(dev, avd_loc << shift, ANCHOR_VOL_DESC_LEN);
9990Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
10006734Sjohnlev 		cmn_err(CE_NOTE, "udfs : Could not read Anchor Volume Desc %x",
10016734Sjohnlev 		    error);
10020Sstevel@tonic-gate 		brelse(secbp);
10030Sstevel@tonic-gate 		return (NULL);
10040Sstevel@tonic-gate 	}
10050Sstevel@tonic-gate 	avdp = (struct anch_vol_desc_ptr *)secbp->b_un.b_addr;
10060Sstevel@tonic-gate 	if (ud_verify_tag_and_desc(&avdp->avd_tag, UD_ANCH_VOL_DESC,
10070Sstevel@tonic-gate 	    avd_loc, 1, ANCHOR_VOL_DESC_LEN) != 0) {
10080Sstevel@tonic-gate 		brelse(secbp);
10090Sstevel@tonic-gate 		return (NULL);
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)
10126734Sjohnlev 	    kmem_zalloc(sizeof (struct udf_vfs), KM_SLEEP);
10130Sstevel@tonic-gate 	udf_vfsp->udf_mvds_loc = SWAP_32(avdp->avd_main_vdse.ext_loc);
10140Sstevel@tonic-gate 	udf_vfsp->udf_mvds_len = SWAP_32(avdp->avd_main_vdse.ext_len);
10150Sstevel@tonic-gate 	udf_vfsp->udf_rvds_loc = SWAP_32(avdp->avd_res_vdse.ext_loc);
10160Sstevel@tonic-gate 	udf_vfsp->udf_rvds_len = SWAP_32(avdp->avd_res_vdse.ext_len);
10170Sstevel@tonic-gate 	secbp->b_flags = B_AGE | B_STALE;
10180Sstevel@tonic-gate 	brelse(secbp);
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate 	/*
10210Sstevel@tonic-gate 	 * Read Main Volume Descriptor Sequence
10220Sstevel@tonic-gate 	 * and process it
10230Sstevel@tonic-gate 	 */
10240Sstevel@tonic-gate 	vds_loc = udf_vfsp->udf_mvds_loc;
10250Sstevel@tonic-gate 	secbp = ud_bread(dev, vds_loc << shift,
10266734Sjohnlev 	    udf_vfsp->udf_mvds_len);
10270Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
10280Sstevel@tonic-gate 		brelse(secbp);
10296734Sjohnlev 		cmn_err(CE_NOTE, "udfs : Could not read Main Volume Desc %x",
10306734Sjohnlev 		    error);
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 		vds_loc = udf_vfsp->udf_rvds_loc;
10330Sstevel@tonic-gate 		secbp = ud_bread(dev, vds_loc << shift,
10346734Sjohnlev 		    udf_vfsp->udf_rvds_len);
10350Sstevel@tonic-gate 		if ((error = geterror(secbp)) != 0) {
10360Sstevel@tonic-gate 			brelse(secbp);
10370Sstevel@tonic-gate 			cmn_err(CE_NOTE,
10380Sstevel@tonic-gate 			"udfs : Could not read Res Volume Desc %x", error);
10390Sstevel@tonic-gate 			return (NULL);
10400Sstevel@tonic-gate 		}
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	udf_vfsp->udf_vds = ngeteblk(udf_vfsp->udf_mvds_len);
10440Sstevel@tonic-gate 	bp = udf_vfsp->udf_vds;
10450Sstevel@tonic-gate 	bp->b_edev = dev;
10460Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
10470Sstevel@tonic-gate 	bp->b_blkno = vds_loc << shift;
10480Sstevel@tonic-gate 	bp->b_bcount = udf_vfsp->udf_mvds_len;
10490Sstevel@tonic-gate 	bcopy(secbp->b_un.b_addr, bp->b_un.b_addr, udf_vfsp->udf_mvds_len);
10500Sstevel@tonic-gate 	secbp->b_flags |= B_STALE | B_AGE;
10510Sstevel@tonic-gate 	brelse(secbp);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 
10540Sstevel@tonic-gate 	count = udf_vfsp->udf_mvds_len / DEV_BSIZE;
10550Sstevel@tonic-gate 	addr = bp->b_un.b_addr;
10560Sstevel@tonic-gate 	for (index = 0; index < count; index ++) {
10570Sstevel@tonic-gate 		ttag = (struct tag *)(addr + index * DEV_BSIZE);
10580Sstevel@tonic-gate 		desc_len = udf_vfsp->udf_mvds_len - (index * DEV_BSIZE);
10590Sstevel@tonic-gate 		if (ud_verify_tag_and_desc(ttag, UD_PRI_VOL_DESC,
10600Sstevel@tonic-gate 		    vds_loc + (index >> shift),
10610Sstevel@tonic-gate 		    1, desc_len) == 0) {
10620Sstevel@tonic-gate 			if (udf_vfsp->udf_pvd == NULL) {
10630Sstevel@tonic-gate 				udf_vfsp->udf_pvd =
10646734Sjohnlev 				    (struct pri_vol_desc *)ttag;
10650Sstevel@tonic-gate 			} else {
10660Sstevel@tonic-gate 				struct pri_vol_desc *opvd, *npvd;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 				opvd = udf_vfsp->udf_pvd;
10690Sstevel@tonic-gate 				npvd = (struct pri_vol_desc *)ttag;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 				if ((strncmp(opvd->pvd_vsi,
10726734Sjohnlev 				    npvd->pvd_vsi, 128) == 0) &&
10736734Sjohnlev 				    (strncmp(opvd->pvd_vol_id,
10746734Sjohnlev 				    npvd->pvd_vol_id, 32) == 0) &&
10756734Sjohnlev 				    (strncmp((caddr_t)&opvd->pvd_desc_cs,
10766734Sjohnlev 				    (caddr_t)&npvd->pvd_desc_cs,
10776734Sjohnlev 				    sizeof (charspec_t)) == 0)) {
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 					if (SWAP_32(opvd->pvd_vdsn) <
10806734Sjohnlev 					    SWAP_32(npvd->pvd_vdsn)) {
10810Sstevel@tonic-gate 						udf_vfsp->udf_pvd = npvd;
10820Sstevel@tonic-gate 					}
10830Sstevel@tonic-gate 				} else {
10840Sstevel@tonic-gate 					goto out;
10850Sstevel@tonic-gate 				}
10860Sstevel@tonic-gate 			}
10870Sstevel@tonic-gate 		} else if (ud_verify_tag_and_desc(ttag, UD_LOG_VOL_DESC,
10880Sstevel@tonic-gate 		    vds_loc + (index >> shift),
10890Sstevel@tonic-gate 		    1, desc_len) == 0) {
10900Sstevel@tonic-gate 			struct log_vol_desc *lvd;
10910Sstevel@tonic-gate 
10920Sstevel@tonic-gate 			lvd = (struct log_vol_desc *)ttag;
10930Sstevel@tonic-gate 			if (strncmp(lvd->lvd_dom_id.reg_id,
10946734Sjohnlev 			    UDF_DOMAIN_NAME, 23) != 0) {
10950Sstevel@tonic-gate 				printf("Domain ID in lvd is not valid\n");
10960Sstevel@tonic-gate 				goto out;
10970Sstevel@tonic-gate 			}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 			if (udf_vfsp->udf_lvd == NULL) {
11000Sstevel@tonic-gate 				udf_vfsp->udf_lvd = lvd;
11010Sstevel@tonic-gate 			} else {
11020Sstevel@tonic-gate 				struct log_vol_desc *olvd;
11030Sstevel@tonic-gate 
11040Sstevel@tonic-gate 				olvd = udf_vfsp->udf_lvd;
11050Sstevel@tonic-gate 				if ((strncmp((caddr_t)&olvd->lvd_desc_cs,
11066734Sjohnlev 				    (caddr_t)&lvd->lvd_desc_cs,
11076734Sjohnlev 				    sizeof (charspec_t)) == 0) &&
11086734Sjohnlev 				    (strncmp(olvd->lvd_lvid,
11096734Sjohnlev 				    lvd->lvd_lvid, 128) == 0)) {
11100Sstevel@tonic-gate 					if (SWAP_32(olvd->lvd_vdsn) <
11116734Sjohnlev 					    SWAP_32(lvd->lvd_vdsn)) {
11120Sstevel@tonic-gate 						udf_vfsp->udf_lvd = lvd;
11130Sstevel@tonic-gate 					}
11140Sstevel@tonic-gate 				} else {
11150Sstevel@tonic-gate 					goto out;
11160Sstevel@tonic-gate 				}
11170Sstevel@tonic-gate 			}
11180Sstevel@tonic-gate 		} else if (ud_verify_tag_and_desc(ttag, UD_PART_DESC,
11190Sstevel@tonic-gate 		    vds_loc + (index >> shift),
11200Sstevel@tonic-gate 		    1, desc_len) == 0) {
11210Sstevel@tonic-gate 			int32_t i;
11220Sstevel@tonic-gate 			struct phdr_desc *hdr;
11230Sstevel@tonic-gate 			struct part_desc *pdesc;
11240Sstevel@tonic-gate 			struct ud_part *pnew, *pold, *part;
11250Sstevel@tonic-gate 
11260Sstevel@tonic-gate 			pdesc = (struct part_desc *)ttag;
11270Sstevel@tonic-gate 			pold = udf_vfsp->udf_parts;
11280Sstevel@tonic-gate 			for (i = 0; i < udf_vfsp->udf_npart; i++) {
11296734Sjohnlev 				if (pold->udp_number !=
11306734Sjohnlev 				    SWAP_16(pdesc->pd_pnum)) {
11316734Sjohnlev 					pold++;
11326734Sjohnlev 					continue;
11330Sstevel@tonic-gate 				}
11346734Sjohnlev 
11356734Sjohnlev 				if (SWAP_32(pdesc->pd_vdsn) >
11366734Sjohnlev 				    pold->udp_seqno) {
11376734Sjohnlev 					pold->udp_seqno =
11386734Sjohnlev 					    SWAP_32(pdesc->pd_vdsn);
11396734Sjohnlev 					pold->udp_access =
11406734Sjohnlev 					    SWAP_32(pdesc->pd_acc_type);
11416734Sjohnlev 					pold->udp_start =
11426734Sjohnlev 					    SWAP_32(pdesc->pd_part_start);
11436734Sjohnlev 					pold->udp_length =
11446734Sjohnlev 					    SWAP_32(pdesc->pd_part_length);
11456734Sjohnlev 				}
11466734Sjohnlev 				goto loop_end;
11470Sstevel@tonic-gate 			}
11480Sstevel@tonic-gate 			pold = udf_vfsp->udf_parts;
11490Sstevel@tonic-gate 			udf_vfsp->udf_npart++;
11500Sstevel@tonic-gate 			pnew = kmem_zalloc(udf_vfsp->udf_npart *
11516734Sjohnlev 			    sizeof (struct ud_part), KM_SLEEP);
11520Sstevel@tonic-gate 			udf_vfsp->udf_parts = pnew;
11530Sstevel@tonic-gate 			if (pold) {
11540Sstevel@tonic-gate 				bcopy(pold, pnew,
11556734Sjohnlev 				    sizeof (struct ud_part) *
11566734Sjohnlev 				    (udf_vfsp->udf_npart - 1));
11570Sstevel@tonic-gate 				kmem_free(pold,
11586734Sjohnlev 				    sizeof (struct ud_part) *
11596734Sjohnlev 				    (udf_vfsp->udf_npart - 1));
11600Sstevel@tonic-gate 			}
11610Sstevel@tonic-gate 			part = pnew + (udf_vfsp->udf_npart - 1);
11620Sstevel@tonic-gate 			part->udp_number = SWAP_16(pdesc->pd_pnum);
11630Sstevel@tonic-gate 			part->udp_seqno = SWAP_32(pdesc->pd_vdsn);
11640Sstevel@tonic-gate 			part->udp_access = SWAP_32(pdesc->pd_acc_type);
11650Sstevel@tonic-gate 			part->udp_start = SWAP_32(pdesc->pd_part_start);
11660Sstevel@tonic-gate 			part->udp_length = SWAP_32(pdesc->pd_part_length);
11670Sstevel@tonic-gate 			part->udp_last_alloc = 0;
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 			/*
11700Sstevel@tonic-gate 			 * Figure out space bitmaps
11710Sstevel@tonic-gate 			 * or space tables
11720Sstevel@tonic-gate 			 */
11730Sstevel@tonic-gate 			hdr = (struct phdr_desc *)pdesc->pd_pc_use;
11740Sstevel@tonic-gate 			if (hdr->phdr_ust.sad_ext_len) {
11750Sstevel@tonic-gate 				part->udp_flags = UDP_SPACETBLS;
11760Sstevel@tonic-gate 				part->udp_unall_loc =
11776734Sjohnlev 				    SWAP_32(hdr->phdr_ust.sad_ext_loc);
11780Sstevel@tonic-gate 				part->udp_unall_len =
11796734Sjohnlev 				    SWAP_32(hdr->phdr_ust.sad_ext_len);
11800Sstevel@tonic-gate 				part->udp_freed_loc =
11816734Sjohnlev 				    SWAP_32(hdr->phdr_fst.sad_ext_loc);
11820Sstevel@tonic-gate 				part->udp_freed_len =
11836734Sjohnlev 				    SWAP_32(hdr->phdr_fst.sad_ext_len);
11840Sstevel@tonic-gate 			} else {
11850Sstevel@tonic-gate 				part->udp_flags = UDP_BITMAPS;
11860Sstevel@tonic-gate 				part->udp_unall_loc =
11876734Sjohnlev 				    SWAP_32(hdr->phdr_usb.sad_ext_loc);
11880Sstevel@tonic-gate 				part->udp_unall_len =
11896734Sjohnlev 				    SWAP_32(hdr->phdr_usb.sad_ext_len);
11900Sstevel@tonic-gate 				part->udp_freed_loc =
11916734Sjohnlev 				    SWAP_32(hdr->phdr_fsb.sad_ext_loc);
11920Sstevel@tonic-gate 				part->udp_freed_len =
11936734Sjohnlev 				    SWAP_32(hdr->phdr_fsb.sad_ext_len);
11940Sstevel@tonic-gate 			}
11950Sstevel@tonic-gate 		} else if (ud_verify_tag_and_desc(ttag, UD_TERM_DESC,
11960Sstevel@tonic-gate 		    vds_loc + (index >> shift),
11970Sstevel@tonic-gate 		    1, desc_len) == 0) {
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 			break;
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate loop_end:
12020Sstevel@tonic-gate 		;
12030Sstevel@tonic-gate 	}
12040Sstevel@tonic-gate 	if ((udf_vfsp->udf_pvd == NULL) ||
12056734Sjohnlev 	    (udf_vfsp->udf_lvd == NULL) ||
12066734Sjohnlev 	    (udf_vfsp->udf_parts == NULL)) {
12070Sstevel@tonic-gate 		goto out;
12080Sstevel@tonic-gate 	}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 	/*
12110Sstevel@tonic-gate 	 * Process Primary Volume Descriptor
12120Sstevel@tonic-gate 	 */
12130Sstevel@tonic-gate 	(void) strncpy(udf_vfsp->udf_volid, udf_vfsp->udf_pvd->pvd_vol_id, 32);
12140Sstevel@tonic-gate 	udf_vfsp->udf_volid[31] = '\0';
12150Sstevel@tonic-gate 	udf_vfsp->udf_tsno = SWAP_16(udf_vfsp->udf_pvd->pvd_tag.tag_sno);
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 	/*
12180Sstevel@tonic-gate 	 * Process Logical Volume Descriptor
12190Sstevel@tonic-gate 	 */
12200Sstevel@tonic-gate 	udf_vfsp->udf_lbsize =
12216734Sjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_log_bsize);
12220Sstevel@tonic-gate 	udf_vfsp->udf_lbmask = udf_vfsp->udf_lbsize - 1;
12230Sstevel@tonic-gate 	udf_vfsp->udf_l2d_shift = shift;
12240Sstevel@tonic-gate 	udf_vfsp->udf_l2b_shift = shift + DEV_BSHIFT;
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 	/*
12270Sstevel@tonic-gate 	 * Check if the media is in
12280Sstevel@tonic-gate 	 * proper domain.
12290Sstevel@tonic-gate 	 */
12300Sstevel@tonic-gate 	if (strcmp(udf_vfsp->udf_lvd->lvd_dom_id.reg_id,
12316734Sjohnlev 	    UDF_DOMAIN_NAME) != 0) {
12320Sstevel@tonic-gate 		goto out;
12330Sstevel@tonic-gate 	}
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	/*
12360Sstevel@tonic-gate 	 * AVDS offset does not match with the lbsize
12370Sstevel@tonic-gate 	 * in the lvd
12380Sstevel@tonic-gate 	 */
12390Sstevel@tonic-gate 	if (udf_vfsp->udf_lbsize != bsize) {
12400Sstevel@tonic-gate 		goto out;
12410Sstevel@tonic-gate 	}
12420Sstevel@tonic-gate 
12430Sstevel@tonic-gate 	udf_vfsp->udf_iseq_loc =
12446734Sjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_int_seq_ext.ext_loc);
12450Sstevel@tonic-gate 	udf_vfsp->udf_iseq_len =
12466734Sjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_int_seq_ext.ext_len);
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	udf_vfsp->udf_fsd_prn =
12496734Sjohnlev 	    SWAP_16(udf_vfsp->udf_lvd->lvd_lvcu.lad_ext_prn);
12500Sstevel@tonic-gate 	udf_vfsp->udf_fsd_loc =
12516734Sjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_lvcu.lad_ext_loc);
12520Sstevel@tonic-gate 	udf_vfsp->udf_fsd_len =
12536734Sjohnlev 	    SWAP_32(udf_vfsp->udf_lvd->lvd_lvcu.lad_ext_len);
12540Sstevel@tonic-gate 
12550Sstevel@tonic-gate 
12560Sstevel@tonic-gate 	/*
12570Sstevel@tonic-gate 	 * process paritions
12580Sstevel@tonic-gate 	 */
12590Sstevel@tonic-gate 	udf_vfsp->udf_mtype = udf_vfsp->udf_parts[0].udp_access;
12600Sstevel@tonic-gate 	for (index = 0; index < udf_vfsp->udf_npart; index ++) {
12610Sstevel@tonic-gate 		if (udf_vfsp->udf_parts[index].udp_access <
12626734Sjohnlev 		    udf_vfsp->udf_mtype) {
12630Sstevel@tonic-gate 			udf_vfsp->udf_mtype =
12646734Sjohnlev 			    udf_vfsp->udf_parts[index].udp_access;
12650Sstevel@tonic-gate 		}
12660Sstevel@tonic-gate 	}
12670Sstevel@tonic-gate 	if ((udf_vfsp->udf_mtype < UDF_MT_RO) ||
12686734Sjohnlev 	    (udf_vfsp->udf_mtype > UDF_MT_OW)) {
12690Sstevel@tonic-gate 		udf_vfsp->udf_mtype = UDF_MT_RO;
12700Sstevel@tonic-gate 	}
12710Sstevel@tonic-gate 
12720Sstevel@tonic-gate 	udf_vfsp->udf_nmaps = 0;
12730Sstevel@tonic-gate 	hdr = (struct pmap_hdr *)udf_vfsp->udf_lvd->lvd_pmaps;
12740Sstevel@tonic-gate 	count = SWAP_32(udf_vfsp->udf_lvd->lvd_num_pmaps);
12750Sstevel@tonic-gate 	for (index = 0; index < count; index++) {
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 		if ((hdr->maph_type == MAP_TYPE1) &&
12786734Sjohnlev 		    (hdr->maph_length == MAP_TYPE1_LEN)) {
12790Sstevel@tonic-gate 			typ1 = (struct pmap_typ1 *)hdr;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 			map = udf_vfsp->udf_maps;
12820Sstevel@tonic-gate 			udf_vfsp->udf_maps =
12836734Sjohnlev 			    kmem_zalloc(sizeof (struct ud_map) *
12846734Sjohnlev 			    (udf_vfsp->udf_nmaps + 1), KM_SLEEP);
12850Sstevel@tonic-gate 			if (map != NULL) {
12860Sstevel@tonic-gate 				bcopy(map, udf_vfsp->udf_maps,
12876734Sjohnlev 				    sizeof (struct ud_map) *
12886734Sjohnlev 				    udf_vfsp->udf_nmaps);
12896734Sjohnlev 				kmem_free(map, sizeof (struct ud_map) *
12906734Sjohnlev 				    udf_vfsp->udf_nmaps);
12910Sstevel@tonic-gate 			}
12920Sstevel@tonic-gate 			map = udf_vfsp->udf_maps + udf_vfsp->udf_nmaps;
12930Sstevel@tonic-gate 			map->udm_flags = UDM_MAP_NORM;
12940Sstevel@tonic-gate 			map->udm_vsn = SWAP_16(typ1->map1_vsn);
12950Sstevel@tonic-gate 			map->udm_pn = SWAP_16(typ1->map1_pn);
12960Sstevel@tonic-gate 			udf_vfsp->udf_nmaps ++;
12970Sstevel@tonic-gate 		} else if ((hdr->maph_type == MAP_TYPE2) &&
12986734Sjohnlev 		    (hdr->maph_length == MAP_TYPE2_LEN)) {
12990Sstevel@tonic-gate 			typ2 = (struct pmap_typ2 *)hdr;
13000Sstevel@tonic-gate 
13010Sstevel@tonic-gate 			if (strncmp(typ2->map2_pti.reg_id,
13026734Sjohnlev 			    UDF_VIRT_PART, 23) == 0) {
13030Sstevel@tonic-gate 				/*
13040Sstevel@tonic-gate 				 * Add this to the normal
13050Sstevel@tonic-gate 				 * partition table so that
13060Sstevel@tonic-gate 				 * we donot
13070Sstevel@tonic-gate 				 */
13080Sstevel@tonic-gate 				map = udf_vfsp->udf_maps;
13090Sstevel@tonic-gate 				udf_vfsp->udf_maps =
13106734Sjohnlev 				    kmem_zalloc(sizeof (struct ud_map) *
13116734Sjohnlev 				    (udf_vfsp->udf_nmaps + 1), KM_SLEEP);
13120Sstevel@tonic-gate 				if (map != NULL) {
13130Sstevel@tonic-gate 					bcopy(map, udf_vfsp->udf_maps,
13146734Sjohnlev 					    sizeof (struct ud_map) *
13156734Sjohnlev 					    udf_vfsp->udf_nmaps);
13160Sstevel@tonic-gate 					kmem_free(map,
13176734Sjohnlev 					    sizeof (struct ud_map) *
13186734Sjohnlev 					    udf_vfsp->udf_nmaps);
13190Sstevel@tonic-gate 				}
13200Sstevel@tonic-gate 				map = udf_vfsp->udf_maps + udf_vfsp->udf_nmaps;
13210Sstevel@tonic-gate 				map->udm_flags = UDM_MAP_VPM;
13220Sstevel@tonic-gate 				map->udm_vsn = SWAP_16(typ2->map2_vsn);
13230Sstevel@tonic-gate 				map->udm_pn = SWAP_16(typ2->map2_pn);
13240Sstevel@tonic-gate 				udf_vfsp->udf_nmaps ++;
13250Sstevel@tonic-gate 				if (error = ud_get_last_block(dev, &lblkno)) {
13260Sstevel@tonic-gate 					goto out;
13270Sstevel@tonic-gate 				}
13280Sstevel@tonic-gate 				if (error = ud_val_get_vat(udf_vfsp, dev,
13296734Sjohnlev 				    lblkno, map)) {
13300Sstevel@tonic-gate 					goto out;
13310Sstevel@tonic-gate 				}
13320Sstevel@tonic-gate 			} else if (strncmp(typ2->map2_pti.reg_id,
13336734Sjohnlev 			    UDF_SPAR_PART, 23) == 0) {
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate 				if (SWAP_16(typ2->map2_pl) != 32) {
13360Sstevel@tonic-gate 					printf(
13376734Sjohnlev 					    "Packet Length is not valid %x\n",
13386734Sjohnlev 					    SWAP_16(typ2->map2_pl));
13390Sstevel@tonic-gate 					goto out;
13400Sstevel@tonic-gate 				}
13410Sstevel@tonic-gate 				if ((typ2->map2_nst < 1) ||
13426734Sjohnlev 				    (typ2->map2_nst > 4)) {
13430Sstevel@tonic-gate 					goto out;
13440Sstevel@tonic-gate 				}
13450Sstevel@tonic-gate 				map = udf_vfsp->udf_maps;
13460Sstevel@tonic-gate 				udf_vfsp->udf_maps =
13476734Sjohnlev 				    kmem_zalloc(sizeof (struct ud_map) *
13486734Sjohnlev 				    (udf_vfsp->udf_nmaps + 1),
13496734Sjohnlev 				    KM_SLEEP);
13500Sstevel@tonic-gate 				if (map != NULL) {
13510Sstevel@tonic-gate 					bcopy(map, udf_vfsp->udf_maps,
13526734Sjohnlev 					    sizeof (struct ud_map) *
13536734Sjohnlev 					    udf_vfsp->udf_nmaps);
13540Sstevel@tonic-gate 					kmem_free(map,
13556734Sjohnlev 					    sizeof (struct ud_map) *
13566734Sjohnlev 					    udf_vfsp->udf_nmaps);
13570Sstevel@tonic-gate 				}
13580Sstevel@tonic-gate 				map = udf_vfsp->udf_maps + udf_vfsp->udf_nmaps;
13590Sstevel@tonic-gate 				map->udm_flags = UDM_MAP_SPM;
13600Sstevel@tonic-gate 				map->udm_vsn = SWAP_16(typ2->map2_vsn);
13610Sstevel@tonic-gate 				map->udm_pn = SWAP_16(typ2->map2_pn);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 				udf_vfsp->udf_nmaps ++;
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate 				if (error = ud_read_sparing_tbls(udf_vfsp,
13666734Sjohnlev 				    dev, map, typ2)) {
13670Sstevel@tonic-gate 					goto out;
13680Sstevel@tonic-gate 				}
13690Sstevel@tonic-gate 			} else {
13700Sstevel@tonic-gate 				/*
13710Sstevel@tonic-gate 				 * Unknown type of partition
13720Sstevel@tonic-gate 				 * Bail out
13730Sstevel@tonic-gate 				 */
13740Sstevel@tonic-gate 				goto out;
13750Sstevel@tonic-gate 			}
13760Sstevel@tonic-gate 		} else {
13770Sstevel@tonic-gate 			/*
13780Sstevel@tonic-gate 			 * Unknown type of partition
13790Sstevel@tonic-gate 			 * Bail out
13800Sstevel@tonic-gate 			 */
13810Sstevel@tonic-gate 			goto out;
13820Sstevel@tonic-gate 		}
13830Sstevel@tonic-gate 		hdr = (struct pmap_hdr *)(((uint8_t *)hdr) + hdr->maph_length);
13840Sstevel@tonic-gate 	}
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 
13870Sstevel@tonic-gate 	/*
13880Sstevel@tonic-gate 	 * Read Logical Volume Integrity Sequence
13890Sstevel@tonic-gate 	 * and process it
13900Sstevel@tonic-gate 	 */
13910Sstevel@tonic-gate 	secbp = ud_bread(dev, udf_vfsp->udf_iseq_loc << shift,
13926734Sjohnlev 	    udf_vfsp->udf_iseq_len);
13930Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
13940Sstevel@tonic-gate 		cmn_err(CE_NOTE,
13950Sstevel@tonic-gate 		"udfs : Could not read Logical Volume Integrity Sequence %x",
13966734Sjohnlev 		    error);
13970Sstevel@tonic-gate 		brelse(secbp);
13980Sstevel@tonic-gate 		goto out;
13990Sstevel@tonic-gate 	}
14000Sstevel@tonic-gate 	udf_vfsp->udf_iseq = ngeteblk(udf_vfsp->udf_iseq_len);
14010Sstevel@tonic-gate 	bp = udf_vfsp->udf_iseq;
14020Sstevel@tonic-gate 	bp->b_edev = dev;
14030Sstevel@tonic-gate 	bp->b_dev = cmpdev(dev);
14040Sstevel@tonic-gate 	bp->b_blkno = udf_vfsp->udf_iseq_loc << shift;
14050Sstevel@tonic-gate 	bp->b_bcount = udf_vfsp->udf_iseq_len;
14060Sstevel@tonic-gate 	bcopy(secbp->b_un.b_addr, bp->b_un.b_addr, udf_vfsp->udf_iseq_len);
14070Sstevel@tonic-gate 	secbp->b_flags |= B_STALE | B_AGE;
14080Sstevel@tonic-gate 	brelse(secbp);
14090Sstevel@tonic-gate 
14100Sstevel@tonic-gate 	count = udf_vfsp->udf_iseq_len / DEV_BSIZE;
14110Sstevel@tonic-gate 	addr = bp->b_un.b_addr;
14120Sstevel@tonic-gate 	for (index = 0; index < count; index ++) {
14130Sstevel@tonic-gate 		ttag = (struct tag *)(addr + index * DEV_BSIZE);
14140Sstevel@tonic-gate 		desc_len = udf_vfsp->udf_iseq_len - (index * DEV_BSIZE);
14150Sstevel@tonic-gate 		if (ud_verify_tag_and_desc(ttag, UD_LOG_VOL_INT,
14160Sstevel@tonic-gate 		    udf_vfsp->udf_iseq_loc + (index >> shift),
14170Sstevel@tonic-gate 		    1, desc_len) == 0) {
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 			struct log_vol_int_desc *lvid;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 			lvid = (struct log_vol_int_desc *)ttag;
14220Sstevel@tonic-gate 			udf_vfsp->udf_lvid = lvid;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 			if (SWAP_32(lvid->lvid_int_type) == LOG_VOL_CLOSE_INT) {
14250Sstevel@tonic-gate 				udf_vfsp->udf_clean = UDF_CLEAN;
14260Sstevel@tonic-gate 			} else {
14270Sstevel@tonic-gate 				udf_vfsp->udf_clean = UDF_DIRTY;
14280Sstevel@tonic-gate 			}
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 			/*
14310Sstevel@tonic-gate 			 * update superblock with the metadata
14320Sstevel@tonic-gate 			 */
14330Sstevel@tonic-gate 			ud_convert_to_superblock(udf_vfsp, lvid);
14340Sstevel@tonic-gate 			break;
14350Sstevel@tonic-gate 		}
14360Sstevel@tonic-gate 	}
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate 	if (udf_vfsp->udf_lvid == NULL) {
14390Sstevel@tonic-gate 		goto out;
14400Sstevel@tonic-gate 	}
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 	if ((blkno = ud_xlate_to_daddr(udf_vfsp,
14436734Sjohnlev 	    udf_vfsp->udf_fsd_prn, udf_vfsp->udf_fsd_loc,
14446734Sjohnlev 	    1, &dummy)) == 0) {
14450Sstevel@tonic-gate 		goto out;
14460Sstevel@tonic-gate 	}
14470Sstevel@tonic-gate 	secbp = ud_bread(dev, blkno << shift, udf_vfsp->udf_fsd_len);
14480Sstevel@tonic-gate 	if ((error = geterror(secbp)) != 0) {
14490Sstevel@tonic-gate 		cmn_err(CE_NOTE,
14500Sstevel@tonic-gate 		"udfs : Could not read File Set Descriptor %x", error);
14510Sstevel@tonic-gate 		brelse(secbp);
14520Sstevel@tonic-gate 		goto out;
14530Sstevel@tonic-gate 	}
14540Sstevel@tonic-gate 	fsd = (struct file_set_desc *)secbp->b_un.b_addr;
14550Sstevel@tonic-gate 	if (ud_verify_tag_and_desc(&fsd->fsd_tag, UD_FILE_SET_DESC,
14560Sstevel@tonic-gate 	    udf_vfsp->udf_fsd_loc,
14570Sstevel@tonic-gate 	    1, udf_vfsp->udf_fsd_len) != 0) {
14580Sstevel@tonic-gate 		secbp->b_flags = B_AGE | B_STALE;
14590Sstevel@tonic-gate 		brelse(secbp);
14600Sstevel@tonic-gate 		goto out;
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 	udf_vfsp->udf_ricb_prn = SWAP_16(fsd->fsd_root_icb.lad_ext_prn);
14630Sstevel@tonic-gate 	udf_vfsp->udf_ricb_loc = SWAP_32(fsd->fsd_root_icb.lad_ext_loc);
14640Sstevel@tonic-gate 	udf_vfsp->udf_ricb_len = SWAP_32(fsd->fsd_root_icb.lad_ext_len);
14650Sstevel@tonic-gate 	secbp->b_flags = B_AGE | B_STALE;
14660Sstevel@tonic-gate 	brelse(secbp);
14670Sstevel@tonic-gate 	udf_vfsp->udf_root_blkno = ud_xlate_to_daddr(udf_vfsp,
14686734Sjohnlev 	    udf_vfsp->udf_ricb_prn, udf_vfsp->udf_ricb_loc,
14696734Sjohnlev 	    1, &dummy);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 	return (udf_vfsp);
14720Sstevel@tonic-gate out:
14730Sstevel@tonic-gate 	ud_destroy_fsp(udf_vfsp);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	return (NULL);
14760Sstevel@tonic-gate }
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate /*
14790Sstevel@tonic-gate  * release/free resources from one ud_map; map data was zalloc'd in
14800Sstevel@tonic-gate  * ud_validate_and_fill_superblock() and fields may later point to
14810Sstevel@tonic-gate  * valid data
14820Sstevel@tonic-gate  */
14830Sstevel@tonic-gate static void
ud_free_map(struct ud_map * map)14840Sstevel@tonic-gate ud_free_map(struct ud_map *map)
14850Sstevel@tonic-gate {
14860Sstevel@tonic-gate 	uint32_t n;
14870Sstevel@tonic-gate 
14880Sstevel@tonic-gate 	if (map->udm_flags & UDM_MAP_VPM) {
14890Sstevel@tonic-gate 		if (map->udm_count) {
14900Sstevel@tonic-gate 			kmem_free(map->udm_count,
14910Sstevel@tonic-gate 			    map->udm_nent * sizeof (*map->udm_count));
14920Sstevel@tonic-gate 			map->udm_count = NULL;
14930Sstevel@tonic-gate 		}
14940Sstevel@tonic-gate 		if (map->udm_bp) {
14950Sstevel@tonic-gate 			for (n = 0; n < map->udm_nent; n++) {
14960Sstevel@tonic-gate 				if (map->udm_bp[n])
14970Sstevel@tonic-gate 					brelse(map->udm_bp[n]);
14980Sstevel@tonic-gate 			}
14990Sstevel@tonic-gate 			kmem_free(map->udm_bp,
15000Sstevel@tonic-gate 			    map->udm_nent * sizeof (*map->udm_bp));
15010Sstevel@tonic-gate 			map->udm_bp = NULL;
15020Sstevel@tonic-gate 		}
15030Sstevel@tonic-gate 		if (map->udm_addr) {
15040Sstevel@tonic-gate 			kmem_free(map->udm_addr,
15050Sstevel@tonic-gate 			    map->udm_nent * sizeof (*map->udm_addr));
15060Sstevel@tonic-gate 			map->udm_addr = NULL;
15070Sstevel@tonic-gate 		}
15080Sstevel@tonic-gate 	}
15090Sstevel@tonic-gate 	if (map->udm_flags & UDM_MAP_SPM) {
15100Sstevel@tonic-gate 		for (n = 0; n < MAX_SPM; n++) {
15110Sstevel@tonic-gate 			if (map->udm_sbp[n]) {
15120Sstevel@tonic-gate 				brelse(map->udm_sbp[n]);
15130Sstevel@tonic-gate 				map->udm_sbp[n] = NULL;
15140Sstevel@tonic-gate 				map->udm_spaddr[n] = NULL;
15150Sstevel@tonic-gate 			}
15160Sstevel@tonic-gate 		}
15170Sstevel@tonic-gate 	}
15180Sstevel@tonic-gate }
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate void
ud_destroy_fsp(struct udf_vfs * udf_vfsp)15210Sstevel@tonic-gate ud_destroy_fsp(struct udf_vfs *udf_vfsp)
15220Sstevel@tonic-gate {
15230Sstevel@tonic-gate 	int32_t i;
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 	ud_printf("ud_destroy_fsp\n");
15260Sstevel@tonic-gate 	if (udf_vfsp == NULL)
15270Sstevel@tonic-gate 		return;
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (udf_vfsp->udf_maps) {
15300Sstevel@tonic-gate 		for (i = 0; i < udf_vfsp->udf_nmaps; i++)
15310Sstevel@tonic-gate 			ud_free_map(&udf_vfsp->udf_maps[i]);
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 		kmem_free(udf_vfsp->udf_maps,
15340Sstevel@tonic-gate 		    udf_vfsp->udf_nmaps * sizeof (*udf_vfsp->udf_maps));
15350Sstevel@tonic-gate 	}
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 	if (udf_vfsp->udf_parts) {
15380Sstevel@tonic-gate 		kmem_free(udf_vfsp->udf_parts,
15390Sstevel@tonic-gate 		    udf_vfsp->udf_npart * sizeof (*udf_vfsp->udf_parts));
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 	if (udf_vfsp->udf_iseq) {
15420Sstevel@tonic-gate 		udf_vfsp->udf_iseq->b_flags |= (B_STALE|B_AGE);
15430Sstevel@tonic-gate 		brelse(udf_vfsp->udf_iseq);
15440Sstevel@tonic-gate 	}
15450Sstevel@tonic-gate 	if (udf_vfsp->udf_vds) {
15460Sstevel@tonic-gate 		udf_vfsp->udf_vds->b_flags |= (B_STALE|B_AGE);
15470Sstevel@tonic-gate 		brelse(udf_vfsp->udf_vds);
15480Sstevel@tonic-gate 	}
15490Sstevel@tonic-gate 	if (udf_vfsp->udf_vfs)
15500Sstevel@tonic-gate 		ud_vfs_remove(udf_vfsp);
15510Sstevel@tonic-gate 	if (udf_vfsp->udf_fsmnt) {
15520Sstevel@tonic-gate 		kmem_free(udf_vfsp->udf_fsmnt,
15530Sstevel@tonic-gate 		    strlen(udf_vfsp->udf_fsmnt) + 1);
15540Sstevel@tonic-gate 	}
15550Sstevel@tonic-gate 	kmem_free(udf_vfsp, sizeof (*udf_vfsp));
15560Sstevel@tonic-gate }
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate void
ud_convert_to_superblock(struct udf_vfs * udf_vfsp,struct log_vol_int_desc * lvid)15590Sstevel@tonic-gate ud_convert_to_superblock(struct udf_vfs *udf_vfsp,
15600Sstevel@tonic-gate 	struct log_vol_int_desc *lvid)
15610Sstevel@tonic-gate {
15620Sstevel@tonic-gate 	int32_t i, c;
15630Sstevel@tonic-gate 	uint32_t *temp;
15640Sstevel@tonic-gate 	struct ud_part *ud_part;
15650Sstevel@tonic-gate 	struct lvid_iu *iu;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	udf_vfsp->udf_maxuniq = SWAP_64(lvid->lvid_uniqid);
15680Sstevel@tonic-gate 	temp = lvid->lvid_fst;
15690Sstevel@tonic-gate 	c = SWAP_32(lvid->lvid_npart);
15700Sstevel@tonic-gate 	ud_part = udf_vfsp->udf_parts;
15710Sstevel@tonic-gate 	for (i = 0; i < c; i++) {
15720Sstevel@tonic-gate 		if (i >= udf_vfsp->udf_npart) {
15730Sstevel@tonic-gate 			continue;
15740Sstevel@tonic-gate 		}
15750Sstevel@tonic-gate 		ud_part->udp_nfree =  SWAP_32(temp[i]);
15760Sstevel@tonic-gate 		ud_part->udp_nblocks =  SWAP_32(temp[c + i]);
15770Sstevel@tonic-gate 		udf_vfsp->udf_freeblks += SWAP_32(temp[i]);
15780Sstevel@tonic-gate 		udf_vfsp->udf_totalblks += SWAP_32(temp[c + i]);
15790Sstevel@tonic-gate 		ud_part++;
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	iu = (struct lvid_iu *)(temp + c * 2);
15830Sstevel@tonic-gate 	udf_vfsp->udf_nfiles = SWAP_32(iu->lvidiu_nfiles);
15840Sstevel@tonic-gate 	udf_vfsp->udf_ndirs = SWAP_32(iu->lvidiu_ndirs);
15850Sstevel@tonic-gate 	udf_vfsp->udf_miread = BCD2HEX_16(SWAP_16(iu->lvidiu_mread));
15860Sstevel@tonic-gate 	udf_vfsp->udf_miwrite = BCD2HEX_16(SWAP_16(iu->lvidiu_mwrite));
15870Sstevel@tonic-gate 	udf_vfsp->udf_mawrite = BCD2HEX_16(SWAP_16(iu->lvidiu_maxwr));
15880Sstevel@tonic-gate }
15890Sstevel@tonic-gate 
15900Sstevel@tonic-gate void
ud_update_superblock(struct vfs * vfsp)15910Sstevel@tonic-gate ud_update_superblock(struct vfs *vfsp)
15920Sstevel@tonic-gate {
15930Sstevel@tonic-gate 	struct udf_vfs *udf_vfsp;
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate 	ud_printf("ud_update_superblock\n");
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	udf_vfsp = (struct udf_vfs *)vfsp->vfs_data;
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	mutex_enter(&udf_vfsp->udf_lock);
16000Sstevel@tonic-gate 	ud_sbwrite(udf_vfsp);
16010Sstevel@tonic-gate 	mutex_exit(&udf_vfsp->udf_lock);
16020Sstevel@tonic-gate }
16030Sstevel@tonic-gate 
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate #include <sys/dkio.h>
16060Sstevel@tonic-gate #include <sys/cdio.h>
16070Sstevel@tonic-gate #include <sys/vtoc.h>
16080Sstevel@tonic-gate 
16090Sstevel@tonic-gate /*
16100Sstevel@tonic-gate  * This part of the code is known
16110Sstevel@tonic-gate  * to work with only sparc. It needs
16120Sstevel@tonic-gate  * to be evluated before using it with x86
16130Sstevel@tonic-gate  */
16140Sstevel@tonic-gate int32_t
ud_get_last_block(dev_t dev,daddr_t * blkno)16150Sstevel@tonic-gate ud_get_last_block(dev_t dev, daddr_t *blkno)
16160Sstevel@tonic-gate {
16170Sstevel@tonic-gate 	struct vtoc vtoc;
16180Sstevel@tonic-gate 	struct dk_cinfo dki_info;
16190Sstevel@tonic-gate 	int32_t rval, error;
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	if ((error = cdev_ioctl(dev, DKIOCGVTOC, (intptr_t)&vtoc,
16226734Sjohnlev 	    FKIOCTL|FREAD|FNATIVE, CRED(), &rval)) != 0) {
16230Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Could not get the vtoc information");
16240Sstevel@tonic-gate 		return (error);
16250Sstevel@tonic-gate 	}
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	if (vtoc.v_sanity != VTOC_SANE) {
16280Sstevel@tonic-gate 		return (EINVAL);
16290Sstevel@tonic-gate 	}
16300Sstevel@tonic-gate 	if ((error = cdev_ioctl(dev, DKIOCINFO, (intptr_t)&dki_info,
16316734Sjohnlev 	    FKIOCTL|FREAD|FNATIVE, CRED(), &rval)) != 0) {
16320Sstevel@tonic-gate 		cmn_err(CE_NOTE, "Could not get the slice information");
16330Sstevel@tonic-gate 		return (error);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	if (dki_info.dki_partition > V_NUMPAR) {
16370Sstevel@tonic-gate 		return (EINVAL);
16380Sstevel@tonic-gate 	}
16390Sstevel@tonic-gate 
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	*blkno = vtoc.v_part[dki_info.dki_partition].p_size;
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	return (0);
16440Sstevel@tonic-gate }
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate /* Search sequentially N - 2, N, N - 152, N - 150 for vat icb */
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate  * int32_t ud_sub_blks[] = {2, 0, 152, 150};
16490Sstevel@tonic-gate  */
16500Sstevel@tonic-gate int32_t ud_sub_blks[] = {152, 150, 2, 0};
16510Sstevel@tonic-gate int32_t ud_sub_count = 4;
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate /*
16540Sstevel@tonic-gate  * Validate the VAT ICB
16550Sstevel@tonic-gate  */
16560Sstevel@tonic-gate static int32_t
ud_val_get_vat(struct udf_vfs * udf_vfsp,dev_t dev,daddr_t blkno,struct ud_map * udm)16570Sstevel@tonic-gate ud_val_get_vat(struct udf_vfs *udf_vfsp, dev_t dev,
16580Sstevel@tonic-gate 	daddr_t blkno, struct ud_map *udm)
16590Sstevel@tonic-gate {
16600Sstevel@tonic-gate 	struct buf *secbp;
16610Sstevel@tonic-gate 	struct file_entry *fe;
16620Sstevel@tonic-gate 	int32_t end_loc, i, j, ad_type;
16630Sstevel@tonic-gate 	struct short_ad *sad;
16640Sstevel@tonic-gate 	struct long_ad *lad;
16650Sstevel@tonic-gate 	uint32_t count, blk;
16660Sstevel@tonic-gate 	struct ud_part *ud_part;
16670Sstevel@tonic-gate 	int err = 0;
16680Sstevel@tonic-gate 
16690Sstevel@tonic-gate 	end_loc = (blkno >> udf_vfsp->udf_l2d_shift) - 1;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	for (i = 0; i < ud_sub_count; i++) {
16720Sstevel@tonic-gate 		udm->udm_vat_icb = end_loc - ud_sub_blks[i];
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 		secbp = ud_bread(dev,
16756734Sjohnlev 		    udm->udm_vat_icb << udf_vfsp->udf_l2d_shift,
16766734Sjohnlev 		    udf_vfsp->udf_lbsize);
16770Sstevel@tonic-gate 		ASSERT(secbp->b_un.b_addr);
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate 		fe = (struct file_entry *)secbp->b_un.b_addr;
16800Sstevel@tonic-gate 		if (ud_verify_tag_and_desc(&fe->fe_tag, UD_FILE_ENTRY, 0,
16810Sstevel@tonic-gate 		    0, 0) == 0) {
16820Sstevel@tonic-gate 			if (ud_verify_tag_and_desc(&fe->fe_tag, UD_FILE_ENTRY,
16830Sstevel@tonic-gate 			    SWAP_32(fe->fe_tag.tag_loc),
16840Sstevel@tonic-gate 			    1, udf_vfsp->udf_lbsize) == 0) {
16850Sstevel@tonic-gate 				if (fe->fe_icb_tag.itag_ftype == 0) {
16860Sstevel@tonic-gate 					break;
16870Sstevel@tonic-gate 				}
16880Sstevel@tonic-gate 			}
16890Sstevel@tonic-gate 		}
16900Sstevel@tonic-gate 		secbp->b_flags |= B_AGE | B_STALE;
16910Sstevel@tonic-gate 		brelse(secbp);
16920Sstevel@tonic-gate 	}
16930Sstevel@tonic-gate 	if (i == ud_sub_count) {
16940Sstevel@tonic-gate 		return (EINVAL);
16950Sstevel@tonic-gate 	}
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 	ad_type = SWAP_16(fe->fe_icb_tag.itag_flags) & 0x3;
16980Sstevel@tonic-gate 	if (ad_type == ICB_FLAG_ONE_AD) {
16990Sstevel@tonic-gate 		udm->udm_nent = 1;
17000Sstevel@tonic-gate 	} else if (ad_type == ICB_FLAG_SHORT_AD) {
17010Sstevel@tonic-gate 		udm->udm_nent =
17026734Sjohnlev 		    SWAP_32(fe->fe_len_adesc) / sizeof (struct short_ad);
17030Sstevel@tonic-gate 	} else if (ad_type == ICB_FLAG_LONG_AD) {
17040Sstevel@tonic-gate 		udm->udm_nent =
17056734Sjohnlev 		    SWAP_32(fe->fe_len_adesc) / sizeof (struct long_ad);
17060Sstevel@tonic-gate 	} else {
17070Sstevel@tonic-gate 		err = EINVAL;
17080Sstevel@tonic-gate 		goto end;
17090Sstevel@tonic-gate 	}
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	udm->udm_count = kmem_zalloc(udm->udm_nent * sizeof (*udm->udm_count),
17120Sstevel@tonic-gate 	    KM_SLEEP);
17130Sstevel@tonic-gate 	udm->udm_bp = kmem_zalloc(udm->udm_nent * sizeof (*udm->udm_bp),
17140Sstevel@tonic-gate 	    KM_SLEEP);
17150Sstevel@tonic-gate 	udm->udm_addr = kmem_zalloc(udm->udm_nent * sizeof (*udm->udm_addr),
17160Sstevel@tonic-gate 	    KM_SLEEP);
17170Sstevel@tonic-gate 
17180Sstevel@tonic-gate 	if (ad_type == ICB_FLAG_ONE_AD) {
17190Sstevel@tonic-gate 			udm->udm_count[0] = (SWAP_64(fe->fe_info_len) - 36) /
17206734Sjohnlev 			    sizeof (uint32_t);
17210Sstevel@tonic-gate 			udm->udm_bp[0] = secbp;
17220Sstevel@tonic-gate 			udm->udm_addr[0] = (uint32_t *)
17236734Sjohnlev 			    &fe->fe_spec[SWAP_32(fe->fe_len_ear)];
17240Sstevel@tonic-gate 			return (0);
17250Sstevel@tonic-gate 	}
17260Sstevel@tonic-gate 	for (i = 0; i < udm->udm_nent; i++) {
17270Sstevel@tonic-gate 		if (ad_type == ICB_FLAG_SHORT_AD) {
17280Sstevel@tonic-gate 			sad = (struct short_ad *)
17296734Sjohnlev 			    (fe->fe_spec + SWAP_32(fe->fe_len_ear));
17300Sstevel@tonic-gate 			sad += i;
17310Sstevel@tonic-gate 			count = SWAP_32(sad->sad_ext_len);
17320Sstevel@tonic-gate 			blk = SWAP_32(sad->sad_ext_loc);
17330Sstevel@tonic-gate 		} else {
17340Sstevel@tonic-gate 			lad = (struct long_ad *)
17356734Sjohnlev 			    (fe->fe_spec + SWAP_32(fe->fe_len_ear));
17360Sstevel@tonic-gate 			lad += i;
17370Sstevel@tonic-gate 			count = SWAP_32(lad->lad_ext_len);
17380Sstevel@tonic-gate 			blk = SWAP_32(lad->lad_ext_loc);
17390Sstevel@tonic-gate 			ASSERT(SWAP_16(lad->lad_ext_prn) == udm->udm_pn);
17400Sstevel@tonic-gate 		}
17410Sstevel@tonic-gate 		if ((count & 0x3FFFFFFF) == 0) {
17420Sstevel@tonic-gate 			break;
17430Sstevel@tonic-gate 		}
17440Sstevel@tonic-gate 		if (i < udm->udm_nent - 1) {
17450Sstevel@tonic-gate 			udm->udm_count[i] = count / 4;
17460Sstevel@tonic-gate 		} else {
17470Sstevel@tonic-gate 			udm->udm_count[i] = (count - 36) / 4;
17480Sstevel@tonic-gate 		}
17490Sstevel@tonic-gate 		ud_part = udf_vfsp->udf_parts;
17500Sstevel@tonic-gate 		for (j = 0; j < udf_vfsp->udf_npart; j++) {
17510Sstevel@tonic-gate 			if (udm->udm_pn == ud_part->udp_number) {
17520Sstevel@tonic-gate 				blk = ud_part->udp_start + blk;
17530Sstevel@tonic-gate 				break;
17540Sstevel@tonic-gate 			}
17550Sstevel@tonic-gate 		}
17560Sstevel@tonic-gate 		if (j == udf_vfsp->udf_npart) {
17570Sstevel@tonic-gate 			err = EINVAL;
17580Sstevel@tonic-gate 			break;
17590Sstevel@tonic-gate 		}
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate 		count = (count + DEV_BSIZE - 1) & ~(DEV_BSIZE - 1);
17620Sstevel@tonic-gate 		udm->udm_bp[i] = ud_bread(dev,
17636734Sjohnlev 		    blk << udf_vfsp->udf_l2d_shift, count);
17640Sstevel@tonic-gate 		if ((udm->udm_bp[i]->b_error != 0) ||
17656734Sjohnlev 		    (udm->udm_bp[i]->b_resid)) {
17660Sstevel@tonic-gate 			err = EINVAL;
17670Sstevel@tonic-gate 			break;
17680Sstevel@tonic-gate 		}
17690Sstevel@tonic-gate 		udm->udm_addr[i] = (uint32_t *)udm->udm_bp[i]->b_un.b_addr;
17700Sstevel@tonic-gate 	}
17710Sstevel@tonic-gate 
17720Sstevel@tonic-gate end:
17730Sstevel@tonic-gate 	if (err)
17740Sstevel@tonic-gate 		ud_free_map(udm);
17750Sstevel@tonic-gate 	secbp->b_flags |= B_AGE | B_STALE;
17760Sstevel@tonic-gate 	brelse(secbp);
17770Sstevel@tonic-gate 	return (err);
17780Sstevel@tonic-gate }
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate int32_t
ud_read_sparing_tbls(struct udf_vfs * udf_vfsp,dev_t dev,struct ud_map * map,struct pmap_typ2 * typ2)17810Sstevel@tonic-gate ud_read_sparing_tbls(struct udf_vfs *udf_vfsp,
17820Sstevel@tonic-gate 	dev_t dev, struct ud_map *map, struct pmap_typ2 *typ2)
17830Sstevel@tonic-gate {
17840Sstevel@tonic-gate 	int32_t index, valid = 0;
17850Sstevel@tonic-gate 	uint32_t sz;
17860Sstevel@tonic-gate 	struct buf *bp;
17870Sstevel@tonic-gate 	struct stbl *stbl;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 	map->udm_plen = SWAP_16(typ2->map2_pl);
17900Sstevel@tonic-gate 	map->udm_nspm = typ2->map2_nst;
17910Sstevel@tonic-gate 	map->udm_spsz = SWAP_32(typ2->map2_sest);
17920Sstevel@tonic-gate 	sz = (map->udm_spsz + udf_vfsp->udf_lbmask) & ~udf_vfsp->udf_lbmask;
17930Sstevel@tonic-gate 	if (sz == 0) {
17940Sstevel@tonic-gate 		return (0);
17950Sstevel@tonic-gate 	}
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	for (index = 0; index < map->udm_nspm; index++) {
17980Sstevel@tonic-gate 		map->udm_loc[index] = SWAP_32(typ2->map2_st[index]);
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		bp = ud_bread(dev,
18016734Sjohnlev 		    map->udm_loc[index] << udf_vfsp->udf_l2d_shift, sz);
18020Sstevel@tonic-gate 		if ((bp->b_error != 0) || (bp->b_resid)) {
18030Sstevel@tonic-gate 			brelse(bp);
18040Sstevel@tonic-gate 			continue;
18050Sstevel@tonic-gate 		}
18060Sstevel@tonic-gate 		stbl = (struct stbl *)bp->b_un.b_addr;
18070Sstevel@tonic-gate 		if (strncmp(stbl->stbl_si.reg_id, UDF_SPAR_TBL, 23) != 0) {
18080Sstevel@tonic-gate 			printf("Sparing Identifier does not match\n");
18090Sstevel@tonic-gate 			bp->b_flags |= B_AGE | B_STALE;
18100Sstevel@tonic-gate 			brelse(bp);
18110Sstevel@tonic-gate 			continue;
18120Sstevel@tonic-gate 		}
18130Sstevel@tonic-gate 		map->udm_sbp[index] = bp;
18140Sstevel@tonic-gate 		map->udm_spaddr[index] = bp->b_un.b_addr;
18150Sstevel@tonic-gate #ifdef	UNDEF
18160Sstevel@tonic-gate {
18170Sstevel@tonic-gate 	struct stbl_entry *te;
18180Sstevel@tonic-gate 	int32_t i, tbl_len;
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	te = (struct stbl_entry *)&stbl->stbl_entry;
18210Sstevel@tonic-gate 	tbl_len = SWAP_16(stbl->stbl_len);
18220Sstevel@tonic-gate 
18230Sstevel@tonic-gate 	printf("%x %x\n", tbl_len, SWAP_32(stbl->stbl_seqno));
18240Sstevel@tonic-gate 	printf("%x %x\n", bp->b_un.b_addr, te);
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate 	for (i = 0; i < tbl_len; i++) {
18270Sstevel@tonic-gate 		printf("%x %x\n", SWAP_32(te->sent_ol), SWAP_32(te->sent_ml));
18280Sstevel@tonic-gate 		te ++;
18290Sstevel@tonic-gate 	}
18300Sstevel@tonic-gate }
18310Sstevel@tonic-gate #endif
18320Sstevel@tonic-gate 		valid ++;
18330Sstevel@tonic-gate 	}
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	if (valid) {
18360Sstevel@tonic-gate 		return (0);
18370Sstevel@tonic-gate 	}
18380Sstevel@tonic-gate 	return (EINVAL);
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate uint32_t
ud_get_lbsize(dev_t dev,uint32_t * loc)18420Sstevel@tonic-gate ud_get_lbsize(dev_t dev, uint32_t *loc)
18430Sstevel@tonic-gate {
18440Sstevel@tonic-gate 	int32_t bsize, shift, index, end_index;
18450Sstevel@tonic-gate 	daddr_t last_block;
18460Sstevel@tonic-gate 	uint32_t avd_loc;
18470Sstevel@tonic-gate 	struct buf *bp;
18480Sstevel@tonic-gate 	struct anch_vol_desc_ptr *avdp;
18490Sstevel@tonic-gate 	uint32_t session_offset = 0;
18500Sstevel@tonic-gate 	int32_t rval;
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate 	if (ud_get_last_block(dev, &last_block) != 0) {
18530Sstevel@tonic-gate 		end_index = 1;
18540Sstevel@tonic-gate 	} else {
18550Sstevel@tonic-gate 		end_index = 3;
18560Sstevel@tonic-gate 	}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 	if (cdev_ioctl(dev, CDROMREADOFFSET, (intptr_t)&session_offset,
18596734Sjohnlev 	    FKIOCTL|FREAD|FNATIVE, CRED(), &rval) != 0) {
18600Sstevel@tonic-gate 		session_offset = 0;
18610Sstevel@tonic-gate 	}
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	for (index = 0; index < end_index; index++) {
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 		for (bsize = DEV_BSIZE, shift = 0;
18666734Sjohnlev 		    bsize <= MAXBSIZE; bsize <<= 1, shift++) {
18670Sstevel@tonic-gate 
18680Sstevel@tonic-gate 			if (index == 0) {
18690Sstevel@tonic-gate 				avd_loc = 256;
18700Sstevel@tonic-gate 				if (bsize <= 2048) {
18710Sstevel@tonic-gate 					avd_loc +=
18726734Sjohnlev 					    session_offset * 2048 / bsize;
18730Sstevel@tonic-gate 				} else {
18740Sstevel@tonic-gate 					avd_loc +=
18756734Sjohnlev 					    session_offset / (bsize / 2048);
18760Sstevel@tonic-gate 				}
18770Sstevel@tonic-gate 			} else if (index == 1) {
18780Sstevel@tonic-gate 				avd_loc = last_block - (1 << shift);
18790Sstevel@tonic-gate 			} else {
18800Sstevel@tonic-gate 				avd_loc = last_block - (256 << shift);
18810Sstevel@tonic-gate 			}
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 			bp = ud_bread(dev, avd_loc << shift,
18846734Sjohnlev 			    ANCHOR_VOL_DESC_LEN);
18850Sstevel@tonic-gate 			if (geterror(bp) != 0) {
18860Sstevel@tonic-gate 				brelse(bp);
18870Sstevel@tonic-gate 				continue;
18880Sstevel@tonic-gate 			}
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 			/*
18910Sstevel@tonic-gate 			 * Verify if we have avdp here
18920Sstevel@tonic-gate 			 */
18930Sstevel@tonic-gate 			avdp = (struct anch_vol_desc_ptr *)bp->b_un.b_addr;
18940Sstevel@tonic-gate 			if (ud_verify_tag_and_desc(&avdp->avd_tag,
18950Sstevel@tonic-gate 			    UD_ANCH_VOL_DESC, avd_loc,
18960Sstevel@tonic-gate 			    1, ANCHOR_VOL_DESC_LEN) != 0) {
18970Sstevel@tonic-gate 				bp->b_flags |= B_AGE | B_STALE;
18980Sstevel@tonic-gate 				brelse(bp);
18990Sstevel@tonic-gate 				continue;
19000Sstevel@tonic-gate 			}
19010Sstevel@tonic-gate 			bp->b_flags |= B_AGE | B_STALE;
19020Sstevel@tonic-gate 			brelse(bp);
19030Sstevel@tonic-gate 			*loc = avd_loc;
19040Sstevel@tonic-gate 			return (bsize);
19050Sstevel@tonic-gate 		}
19060Sstevel@tonic-gate 	}
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	/*
19090Sstevel@tonic-gate 	 * Did not find AVD at all the locations
19100Sstevel@tonic-gate 	 */
19110Sstevel@tonic-gate 	return (0);
19120Sstevel@tonic-gate }
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate static int
udfinit(int fstype,char * name)19150Sstevel@tonic-gate udfinit(int fstype, char *name)
19160Sstevel@tonic-gate {
19170Sstevel@tonic-gate 	static const fs_operation_def_t udf_vfsops_template[] = {
19183898Srsb 		VFSNAME_MOUNT,		{ .vfs_mount = udf_mount },
19193898Srsb 		VFSNAME_UNMOUNT,	{ .vfs_unmount = udf_unmount },
19203898Srsb 		VFSNAME_ROOT,		{ .vfs_root = udf_root },
19213898Srsb 		VFSNAME_STATVFS,	{ .vfs_statvfs = udf_statvfs },
19223898Srsb 		VFSNAME_SYNC,		{ .vfs_sync = udf_sync },
19233898Srsb 		VFSNAME_VGET,		{ .vfs_vget = udf_vget },
19243898Srsb 		VFSNAME_MOUNTROOT,	{ .vfs_mountroot = udf_mountroot },
19253898Srsb 		NULL,			NULL
19260Sstevel@tonic-gate 	};
19270Sstevel@tonic-gate 	extern struct vnodeops *udf_vnodeops;
19280Sstevel@tonic-gate 	extern const fs_operation_def_t udf_vnodeops_template[];
19290Sstevel@tonic-gate 	int error;
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 	ud_printf("udfinit\n");
19320Sstevel@tonic-gate 
19330Sstevel@tonic-gate 	error = vfs_setfsops(fstype, udf_vfsops_template, NULL);
19340Sstevel@tonic-gate 	if (error != 0) {
19350Sstevel@tonic-gate 		cmn_err(CE_WARN, "udfinit: bad vfs ops template");
19360Sstevel@tonic-gate 		return (error);
19370Sstevel@tonic-gate 	}
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 	error = vn_make_ops(name, udf_vnodeops_template, &udf_vnodeops);
19400Sstevel@tonic-gate 	if (error != 0) {
19410Sstevel@tonic-gate 		(void) vfs_freevfsops_by_type(fstype);
19420Sstevel@tonic-gate 		cmn_err(CE_WARN, "udfinit: bad vnode ops template");
19430Sstevel@tonic-gate 		return (error);
19440Sstevel@tonic-gate 	}
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	udf_fstype = fstype;
19470Sstevel@tonic-gate 
19480Sstevel@tonic-gate 	ud_init_inodes();
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	return (0);
19510Sstevel@tonic-gate }
1952